sam.html 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291
  1. <html>
  2. <title>
  3. -
  4. </title>
  5. <body BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#330088" ALINK="#FF0044">
  6. <H1>The Text Editor <TT>sam</TT>
  7. </H1>
  8. <DL><DD><I>Rob Pike<br>
  9. rob@plan9.bell-labs.com<br>
  10. </I></DL>
  11. <DL><DD><H4>ABSTRACT</H4>
  12. <br>&#32;<br>
  13. <TT>Sam</TT>
  14. is an interactive multi-file text editor intended for
  15. bitmap displays.
  16. A textual command language
  17. supplements the mouse-driven, cut-and-paste interface
  18. to make complex or
  19. repetitive editing tasks easy to specify.
  20. The language is characterized by the composition of regular expressions
  21. to describe the structure of the text being modified.
  22. The treatment of files as a database, with changes logged
  23. as atomic transactions, guides the implementation and
  24. makes a general `undo' mechanism straightforward.
  25. <P>
  26. <TT>Sam</TT>
  27. is implemented as two processes connected by a low-bandwidth stream,
  28. one process handling the display and the other the editing
  29. algorithms. Therefore it can run with the display process
  30. in a bitmap terminal and the editor on a local host,
  31. with both processes on a bitmap-equipped host, or with
  32. the display process in the terminal and the editor in a
  33. remote host.
  34. By suppressing the display process,
  35. it can even run without a bitmap terminal.
  36. </P>
  37. <P>
  38. This paper is reprinted from Software&#173;Practice and Experience,
  39. Vol 17, number 11, pp. 813-845, November 1987.
  40. The paper has not been updated for the Plan 9 manuals. Although
  41. <TT>Sam</TT>
  42. has not changed much since the paper was written, the system around it certainly has.
  43. Nonetheless, the description here still stands as the best introduction to the editor.
  44. </DL>
  45. </P>
  46. <H4>Introduction
  47. </H4>
  48. <br>&#32;<br>
  49. <TT>Sam</TT>
  50. is an interactive text editor that combines cut-and-paste interactive editing with
  51. an unusual command language based on the composition of regular expressions.
  52. It is written as two programs: one, the `host part,' runs on a UNIX system
  53. and implements the command language and provides file access; the other, the
  54. `terminal part,' runs asynchronously
  55. on a machine with a mouse and bitmap display
  56. and supports the display and interactive editing.
  57. The host part may be even run in isolation on an ordinary terminal
  58. to edit text using the command
  59. language, much like a traditional line editor,
  60. without assistance from a mouse or display.
  61. Most often,
  62. the terminal part runs on a Blit<sup>1</sup> terminal
  63. (actually on a Teletype DMD 5620, the production version of the Blit), whose
  64. host connection is an ordinary 9600 bps RS232 link;
  65. on the SUN computer the host and display processes run on a single machine,
  66. connected by a pipe.
  67. <P>
  68. <TT>Sam</TT>
  69. edits uninterpreted
  70. ASCII text.
  71. It has no facilities for multiple fonts, graphics or tables,
  72. unlike MacWrite,<sup>2</sup> Bravo,<sup>3</sup> Tioga<sup>4</sup>
  73. or Lara.<sup>5</sup>
  74. Also unlike them, it has a rich command language.
  75. (Throughout this paper, the phrase
  76. command language
  77. refers to
  78. textual commands; commands activated from the mouse form the
  79. <I>mouse</I>
  80. <I>language.</I>)
  81. <TT>Sam</TT>
  82. developed as an editor for use by programmers, and tries to join
  83. the styles of the UNIX text editor
  84. <TT>ed</TT><sup>6,7</sup>
  85. with that of interactive cut-and-paste editors by
  86. providing a comfortable mouse-driven interface
  87. to a program with a solid command language driven by regular expressions.
  88. The command language developed more than the mouse language, and
  89. acquired a notation for describing the structure of files
  90. more richly than as a sequence of lines,
  91. using a dataflow-like syntax for specifying changes.
  92. </P>
  93. <P>
  94. The interactive style was influenced by
  95. <TT>jim</TT>,<sup>1</sup>
  96. an early cut-and-paste editor for the Blit, and by
  97. <TT>mux</TT>,<sup>8</sup>
  98. the Blit window system.
  99. <TT>Mux</TT>
  100. merges the original Blit window system,
  101. <TT>mpx</TT>,<sup>1</sup>
  102. with cut-and-paste editing, forming something like a
  103. multiplexed version of
  104. <TT>jim</TT>
  105. that edits the output of (and input to) command sessions rather than files.
  106. </P>
  107. <P>
  108. The first part of this paper describes the command language, then the mouse
  109. language, and explains how they interact.
  110. That is followed by a description of the implementation,
  111. first of the host part, then of the terminal part.
  112. A principle that influenced the design of
  113. <TT>sam</TT>
  114. is that it should have no explicit limits, such as upper limits on
  115. file size or line length.
  116. A secondary consideration is that it be efficient.
  117. To honor these two goals together requires a method for efficiently
  118. manipulating
  119. huge strings (files) without breaking them into lines,
  120. perhaps while making thousands of changes
  121. under control of the command language.
  122. <TT>Sam</TT>'s
  123. method is to
  124. treat the file as a transaction database, implementing changes as atomic
  125. updates. These updates may be unwound easily to `undo' changes.
  126. Efficiency is achieved through a collection of caches that minimizes
  127. disc traffic and data motion, both within the two parts of the program
  128. and between them.
  129. </P>
  130. <P>
  131. The terminal part of
  132. <TT>sam</TT>
  133. is fairly straightforward.
  134. More interesting is how the two halves of the editor stay
  135. synchronized when either half may initiate a change.
  136. This is achieved through a data structure that organizes the
  137. communications and is maintained in parallel by both halves.
  138. </P>
  139. <P>
  140. The last part of the paper chronicles the writing of
  141. <TT>sam</TT>
  142. and discusses the lessons that were learned through its development and use.
  143. </P>
  144. <P>
  145. The paper is long, but is composed largely of two papers of reasonable length:
  146. a description of the user interface of
  147. <TT>sam</TT>
  148. and a discussion of its implementation.
  149. They are combined because the implementation is strongly influenced by
  150. the user interface, and vice versa.
  151. </P>
  152. <H4>The Interface
  153. </H4>
  154. <br>&#32;<br>
  155. <TT>Sam</TT>
  156. is a text editor for multiple files.
  157. File names may be provided when it is invoked:
  158. <DL><DT><DD><TT><PRE>
  159. sam file1 file2 ...
  160. </PRE></TT></DL>
  161. and there are commands
  162. to add new files and discard unneeded ones.
  163. Files are not read until necessary
  164. to complete some command.
  165. Editing operations apply to an internal copy
  166. made when the file is read; the UNIX file associated with the copy
  167. is changed only by an explicit command.
  168. To simplify the discussion, the internal copy is here called a
  169. <I>file</I>,
  170. while the disc-resident original is called a
  171. disc file.
  172. <P>
  173. <TT>Sam</TT>
  174. is usually connected to a bitmap display that presents a cut-and-paste
  175. editor driven by the mouse.
  176. In this mode, the command language is still available:
  177. text typed in a special window, called the
  178. <TT>sam</TT>
  179. <I>window,</I>
  180. is interpreted
  181. as commands to be executed in the current file.
  182. Cut-and-paste editing may be used in any window &#173; even in the
  183. <TT>sam</TT>
  184. window to construct commands.
  185. The other mode of operation, invoked by starting
  186. <TT>sam</TT>
  187. with the option
  188. <TT>-d</TT>
  189. (for `no download'),
  190. does not use the mouse or bitmap display, but still permits
  191. editing using the textual command language, even on an ordinary terminal,
  192. interactively or from a script.
  193. </P>
  194. <P>
  195. The following sections describe first the command language (under
  196. <TT>sam -d</TT>
  197. and in the
  198. <TT>sam</TT>
  199. window), and then the mouse interface.
  200. These two languages are nearly independent, but connect through the
  201. <I>current</I>
  202. <I>text,</I>
  203. described below.
  204. </P>
  205. <H4>The Command Language
  206. </H4>
  207. <br>&#32;<br>
  208. A file consists of its contents, which are an array of characters
  209. (that is, a string); the
  210. <I>name</I>
  211. of the associated disc file; the
  212. modified bit
  213. that states whether the contents match those of
  214. the disc file;
  215. and a substring of the contents, called the
  216. current text
  217. or
  218. <I>dot</I>
  219. (see Figures 1 and 2).
  220. If the current text is a null string, dot falls between characters.
  221. The
  222. <I>value</I>
  223. of dot is the location of the current text; the
  224. <I>contents</I>
  225. of dot are the characters it contains.
  226. <TT>Sam</TT>
  227. imparts to the text no two-dimensional interpretation such as columns
  228. or fields; text is always one-dimensional.
  229. Even the idea of a `line' of text as understood by most UNIX programs
  230. &#173; a sequence of characters terminated by a newline character &#173;
  231. is only weakly supported.
  232. <P>
  233. The
  234. current file
  235. is the file to which editing commands refer.
  236. The current text is therefore dot in the current file.
  237. If a command doesn't explicitly name a particular file or piece of text,
  238. the command is assumed to apply to the current text.
  239. For the moment, ignore the presence of multiple files and consider
  240. editing a single file.
  241. <br><img src="fig1.ps.11760.gif"><br>
  242. <br>
  243. <I>Figure 1. A typical
  244. </I><TT>sam</TT><I>
  245. screen, with the editing menu presented.
  246. The
  247. </I><TT>sam</TT><I>
  248. (command language) window is in the middle, with file windows above and below.
  249. (The user interface makes it easy to create these abutting windows.)
  250. The partially obscured window is a third file window.
  251. The uppermost window is that to which typing and mouse operations apply,
  252. as indicated by its heavy border.
  253. Each window has its current text highlighted in reverse video.
  254. The
  255. </I><TT>sam</TT><I>
  256. window's current text is the null string on the last visible line,
  257. indicated by a vertical bar.
  258. See also Figure 2.
  259. <br>
  260. <DL><DT><DD><TT><PRE>
  261. </I><br>&#32;<br>
  262. </PRE></TT></DL>
  263. </P>
  264. <P>
  265. Commands have one-letter names.
  266. Except for non-editing commands such as writing
  267. the file to disc, most commands make some change
  268. to the text in dot and leave dot set to the text resulting from the change.
  269. For example, the delete command,
  270. <TT>d</TT>,
  271. deletes the text in dot, replacing it by the null string and setting dot
  272. to the result.
  273. The change command,
  274. <TT>c</TT>,
  275. replaces dot by text delimited by an arbitrary punctuation character,
  276. conventionally
  277. a slash. Thus,
  278. <DL><DT><DD><TT><PRE>
  279. c/Peter/
  280. </PRE></TT></DL>
  281. replaces the text in dot by the string
  282. <TT>Peter</TT>.
  283. Similarly,
  284. <DL><DT><DD><TT><PRE>
  285. a/Peter/
  286. </PRE></TT></DL>
  287. (append) adds the string after dot, and
  288. <DL><DT><DD><TT><PRE>
  289. i/Peter/
  290. </PRE></TT></DL>
  291. (insert) inserts before dot.
  292. All three leave dot set to the new text,
  293. <TT>Peter</TT>.
  294. </P>
  295. <P>
  296. Newlines are part of the syntax of commands:
  297. the newline character lexically terminates a command.
  298. Within the inserted text, however, newlines are never implicit.
  299. But since it is often convenient to insert multiple lines of text,
  300. <TT>sam</TT>
  301. has a special
  302. syntax for that case:
  303. <DL><DT><DD><TT><PRE>
  304. a
  305. some lines of text
  306. to be inserted in the file,
  307. terminated by a period
  308. on a line by itself
  309. .
  310. </PRE></TT></DL>
  311. In the one-line syntax, a newline character may be specified by a C-like
  312. escape, so
  313. <DL><DT><DD><TT><PRE>
  314. c/\n/
  315. </PRE></TT></DL>
  316. replaces dot by a single newline character.
  317. </P>
  318. <P>
  319. <TT>Sam</TT>
  320. also has a substitute command,
  321. <TT>s</TT>:
  322. <DL><DT><DD><TT><PRE>
  323. s/<I>expression</I>/<I>replacement</I>/
  324. </PRE></TT></DL>
  325. substitutes the replacement text for the first match, in dot,
  326. of the regular expression.
  327. Thus, if dot is the string
  328. <TT>Peter</TT>,
  329. the command
  330. <DL><DT><DD><TT><PRE>
  331. s/t/st/
  332. </PRE></TT></DL>
  333. changes it to
  334. <TT>Pester</TT>.
  335. In general,
  336. <TT>s</TT>
  337. is unnecessary, but it was inherited from
  338. <TT>ed</TT>
  339. and it has some convenient variations.
  340. For instance, the replacement text may include the matched text,
  341. specified by
  342. <TT>&</TT>:
  343. <DL><DT><DD><TT><PRE>
  344. s/Peter/Oh, &amp;, &amp;, &amp;, &amp;!/
  345. </PRE></TT></DL>
  346. </P>
  347. <P>
  348. There are also three commands that apply programs
  349. to text:
  350. <DL><DT><DD><TT><PRE>
  351. &#60; <I>UNIX program</I>
  352. </PRE></TT></DL>
  353. replaces dot by the output of the UNIX program.
  354. Similarly, the
  355. <TT>></TT>
  356. command
  357. runs the program with dot as its standard input, and
  358. <TT>|</TT>
  359. does both. For example,
  360. <DL><DT><DD><TT><PRE>
  361. | sort
  362. </PRE></TT></DL>
  363. replaces dot by the result of applying the standard sorting utility to it.
  364. Again, newlines have no special significance for these
  365. <TT>sam</TT>
  366. commands.
  367. The text acted upon and resulting from these commands is not necessarily
  368. bounded by newlines, although for connection with UNIX programs,
  369. newlines may be necessary to obey conventions.
  370. </P>
  371. <P>
  372. One more command:
  373. <TT>p</TT>
  374. prints the contents of dot.
  375. Table I summarizes
  376. <TT>sam</TT>'s
  377. commands.
  378. <br><img src="-.11761.gif"><br>
  379. <br>&#32;<br>
  380. </P>
  381. <P>
  382. The value of dot may be changed by
  383. specifying an
  384. <I>address</I>
  385. for the command.
  386. The simplest address is a line number:
  387. <DL><DT><DD><TT><PRE>
  388. 3
  389. </PRE></TT></DL>
  390. refers to the third line of the file, so
  391. <DL><DT><DD><TT><PRE>
  392. 3d
  393. </PRE></TT></DL>
  394. deletes the third line of the file, and implicitly renumbers
  395. the lines so the old line 4 is now numbered 3.
  396. (This is one of the few places where
  397. <TT>sam</TT>
  398. deals with lines directly.)
  399. Line
  400. <TT>0</TT>
  401. is the null string at the beginning of the file.
  402. If a command consists of only an address, a
  403. <TT>p</TT>
  404. command is assumed, so typing an unadorned
  405. <TT>3</TT>
  406. prints line 3 on the terminal.
  407. There are a couple of other basic addresses:
  408. a period addresses dot itself; and
  409. a dollar sign
  410. (<TT>$</TT>)
  411. addresses the null string at the end of the file.
  412. </P>
  413. <P>
  414. An address is always a single substring of the file.
  415. Thus, the address
  416. <TT>3</TT>
  417. addresses the characters
  418. after the second newline of
  419. the file through the third newline of the file.
  420. A
  421. compound address
  422. is constructed by the comma operator
  423. <DL><DT><DD><TT><PRE>
  424. <I>address1</I>,<I>address2</I>
  425. </PRE></TT></DL>
  426. and addresses the substring of the file from the beginning of
  427. <I>address1</I>
  428. to the end of
  429. <I>address2</I>.
  430. For example, the command
  431. <TT>3,5p</TT>
  432. prints the third through fifth lines of the file and
  433. <TT>.,$d</TT>
  434. deletes the text from the beginning of dot to the end of the file.
  435. </P>
  436. <P>
  437. These addresses are all absolute positions in the file, but
  438. <TT>sam</TT>
  439. also has relative addresses, indicated by
  440. <TT>+</TT>
  441. or
  442. <TT>-</TT>.
  443. For example,
  444. <DL><DT><DD><TT><PRE>
  445. $-3
  446. </PRE></TT></DL>
  447. is the third line before the end of the file and
  448. <DL><DT><DD><TT><PRE>
  449. .+1
  450. </PRE></TT></DL>
  451. is the line after dot.
  452. If no address appears to the left of the
  453. <TT>+</TT>
  454. or
  455. <TT>-</TT>,
  456. dot is assumed;
  457. if nothing appears to the right,
  458. <TT>1</TT>
  459. is assumed.
  460. Therefore,
  461. <TT>.+1</TT>
  462. may be abbreviated to just a plus sign.
  463. </P>
  464. <P>
  465. The
  466. <TT>+</TT>
  467. operator acts relative to the end of its first argument, while the
  468. <TT>-</TT>
  469. operator acts relative to the beginning. Thus
  470. <TT>.+1</TT>
  471. addresses the first line after dot,
  472. <TT>.-</TT>
  473. addresses the first line before dot, and
  474. <TT>+-</TT>
  475. refers to the line containing the end of dot. (Dot may span multiple lines, and
  476. <TT>+</TT>
  477. selects the line after the end of dot, then
  478. <TT>-</TT>
  479. backs up one line.)
  480. </P>
  481. <P>
  482. The final type of address is a regular expression, which addresses the
  483. text matched by the expression. The expression is enclosed in slashes, as in
  484. <DL><DT><DD><TT><PRE>
  485. /<I>expression</I>/
  486. </PRE></TT></DL>
  487. The expressions are the same as those in the UNIX program
  488. <TT>egrep</TT>,<sup>6,7</sup>
  489. and include closures, alternations, and so on.
  490. They find the
  491. leftmost longest
  492. string that matches the expression, that is,
  493. the first match after the point where the search is started,
  494. and if more than one match begins at the same spot, the longest such match.
  495. (I assume familiarity with the syntax for regular expressions in UNIX programs.<sup>9</sup>)
  496. For example,
  497. <DL><DT><DD><TT><PRE>
  498. /x/
  499. </PRE></TT></DL>
  500. matches the next
  501. <TT>x</TT>
  502. character in the file,
  503. <DL><DT><DD><TT><PRE>
  504. /xx*/
  505. </PRE></TT></DL>
  506. matches the next run of one or more
  507. <TT>x</TT>'s,
  508. and
  509. <DL><DT><DD><TT><PRE>
  510. /x|Peter/
  511. </PRE></TT></DL>
  512. matches the next
  513. <TT>x</TT>
  514. or
  515. <TT>Peter</TT>.
  516. For compatibility with other UNIX programs, the `any character' operator,
  517. a period,
  518. does not match a newline, so
  519. <DL><DT><DD><TT><PRE>
  520. /.*/
  521. </PRE></TT></DL>
  522. matches the text from dot to the end of the line, but excludes the newline
  523. and so will not match across
  524. the line boundary.
  525. </P>
  526. <P>
  527. Regular expressions are always relative addresses.
  528. The direction is forwards by default,
  529. so
  530. <TT>/Peter/</TT>
  531. is really an abbreviation for
  532. <TT>+/Peter/</TT>.
  533. The search can be reversed with a minus sign, so
  534. <DL><DT><DD><TT><PRE>
  535. <TT>-/Peter/</TT>
  536. </PRE></TT></DL>
  537. finds the first
  538. <TT>Peter</TT>
  539. before dot.
  540. Regular expressions may be used with other address forms, so
  541. <TT>0+/Peter/</TT>
  542. finds the first
  543. <TT>Peter</TT>
  544. in the file and
  545. <TT>$-/Peter/</TT>
  546. finds the last.
  547. Table II summarizes
  548. <TT>sam</TT>'s
  549. addresses.
  550. <br><img src="-.11762.gif"><br>
  551. <br>&#32;<br>
  552. </P>
  553. <P>
  554. The language discussed so far will not seem novel
  555. to people who use UNIX text editors
  556. such as
  557. <TT>ed</TT>
  558. or
  559. <TT>vi</TT>.<sup>9</sup>
  560. Moreover, the kinds of editing operations these commands allow, with the exception
  561. of regular expressions and line numbers,
  562. are clearly more conveniently handled by a mouse-based interface.
  563. Indeed,
  564. <TT>sam</TT>'s
  565. mouse language (discussed at length below) is the means by which
  566. simple changes are usually made.
  567. For large or repetitive changes, however, a textual language
  568. outperforms a manual interface.
  569. </P>
  570. <P>
  571. Imagine that, instead of deleting just one occurrence of the string
  572. <TT>Peter</TT>,
  573. we wanted to eliminate every
  574. <TT>Peter</TT>.
  575. What's needed is an iterator that runs a command for each occurrence of some
  576. text.
  577. <TT>Sam</TT>'s
  578. iterator is called
  579. <TT>x</TT>,
  580. for extract:
  581. <DL><DT><DD><TT><PRE>
  582. x/<I>expression</I>/ <I>command</I>
  583. </PRE></TT></DL>
  584. finds all matches in dot of the specified expression, and for each
  585. such match, sets dot to the text matched and runs the command.
  586. So to delete all the
  587. <TT>Peters:</TT>
  588. <DL><DT><DD><TT><PRE>
  589. 0,$ x/Peter/ d
  590. </PRE></TT></DL>
  591. (Blanks in these examples are to improve readability;
  592. <TT>sam</TT>
  593. neither requires nor interprets them.)
  594. This searches the entire file
  595. (<TT>0,$</TT>)
  596. for occurrences of the string
  597. <TT>Peter</TT>,
  598. and runs the
  599. <TT>d</TT>
  600. command with dot set to each such occurrence.
  601. (By contrast, the comparable
  602. <TT>ed</TT>
  603. command would delete all
  604. <I>lines</I>
  605. containing
  606. <TT>Peter</TT>;
  607. <TT>sam</TT>
  608. deletes only the
  609. <TT>Peters</TT>.)
  610. The address
  611. <TT>0,$</TT>
  612. is commonly used, and may be abbreviated to just a comma.
  613. As another example,
  614. <DL><DT><DD><TT><PRE>
  615. , x/Peter/ p
  616. </PRE></TT></DL>
  617. prints a list of
  618. <TT>Peters,</TT>
  619. one for each appearance in the file, with no intervening text (not even newlines
  620. to separate the instances).
  621. </P>
  622. <P>
  623. Of course, the text extracted by
  624. <TT>x</TT>
  625. may be selected by a regular expression,
  626. which complicates deciding what set of matches is chosen &#173;
  627. matches may overlap. This is resolved by generating the matches
  628. starting from the beginning of dot using the leftmost-longest rule,
  629. and searching for each match starting from the end of the previous one.
  630. Regular expressions may also match null strings, but a null match
  631. adjacent to a non-null match is never selected; at least one character
  632. must intervene.
  633. For example,
  634. <DL><DT><DD><TT><PRE>
  635. , c/AAA/
  636. x/B*/ c/-/
  637. , p
  638. </PRE></TT></DL>
  639. produces as output
  640. <DL><DT><DD><TT><PRE>
  641. -A-A-A-
  642. </PRE></TT></DL>
  643. because the pattern
  644. <TT>B*</TT>
  645. matches the null strings separating the
  646. <TT>A</TT>'s.
  647. </P>
  648. <P>
  649. The
  650. <TT>x</TT>
  651. command has a complement,
  652. <TT>y</TT>,
  653. with similar syntax, that executes the command with dot set to the text
  654. <I>between</I>
  655. the matches of the expression.
  656. For example,
  657. <DL><DT><DD><TT><PRE>
  658. , c/AAA/
  659. y/A/ c/-/
  660. , p
  661. </PRE></TT></DL>
  662. produces the same result as the example above.
  663. </P>
  664. <P>
  665. The
  666. <TT>x</TT>
  667. and
  668. <TT>y</TT>
  669. commands are looping constructs, and
  670. <TT>sam</TT>
  671. has a pair of conditional commands to go with them.
  672. They have similar syntax:
  673. <DL><DT><DD><TT><PRE>
  674. g/<I>expression</I>/ <I>command</I>
  675. </PRE></TT></DL>
  676. (guard)
  677. runs the command exactly once if dot contains a match of the expression.
  678. This is different from
  679. <TT>x</TT>,
  680. which runs the command for
  681. <I>each</I>
  682. match:
  683. <TT>x</TT>
  684. loops;
  685. <TT>g</TT>
  686. merely tests, without changing the value of dot.
  687. Thus,
  688. <DL><DT><DD><TT><PRE>
  689. , x/Peter/ d
  690. </PRE></TT></DL>
  691. deletes all occurrences of
  692. <TT>Peter</TT>,
  693. but
  694. <DL><DT><DD><TT><PRE>
  695. , g/Peter/ d
  696. </PRE></TT></DL>
  697. deletes the whole file (reduces it to a null string) if
  698. <TT>Peter</TT>
  699. occurs anywhere in the text.
  700. The complementary conditional is
  701. <TT>v</TT>,
  702. which runs the command if there is
  703. <I>no</I>
  704. match of the expression.
  705. </P>
  706. <P>
  707. These control-structure-like commands may be composed to construct more
  708. involved operations. For example, to print those lines of text that
  709. contain the string
  710. <TT>Peter</TT>:
  711. <DL><DT><DD><TT><PRE>
  712. , x/.*\n/ g/Peter/ p
  713. </PRE></TT></DL>
  714. The
  715. <TT>x</TT>
  716. breaks the file into lines, the
  717. <TT>g</TT>
  718. selects those lines containing
  719. <TT>Peter</TT>,
  720. and the
  721. <TT>p</TT>
  722. prints them.
  723. This command gives an address for the
  724. <TT>x</TT>
  725. command (the whole file), but because
  726. <TT>g</TT>
  727. does not have an explicit address, it applies to the value of
  728. dot produced by the
  729. <TT>x</TT>
  730. command, that is, to each line.
  731. All commands in
  732. <TT>sam</TT>
  733. except for the command to write a file to disc use dot for the
  734. default address.
  735. </P>
  736. <P>
  737. Composition may be continued indefinitely.
  738. <DL><DT><DD><TT><PRE>
  739. , x/.*\n/ g/Peter/ v/SaltPeter/ p
  740. </PRE></TT></DL>
  741. prints those lines containing
  742. <TT>Peter</TT>
  743. but
  744. <I>not</I>
  745. those containing
  746. <TT>SaltPeter</TT>.
  747. </P>
  748. <H4>Structural Regular Expressions
  749. </H4>
  750. <br>&#32;<br>
  751. Unlike other UNIX text editors,
  752. including the non-interactive ones such as
  753. <TT>sed</TT>
  754. and
  755. <TT>awk</TT>,<sup>7</sup>
  756. <TT>sam</TT>
  757. is good for manipulating files with multi-line `records.'
  758. An example is an on-line phone book composed of records,
  759. separated by blank lines, of the form
  760. <DL><DT><DD><TT><PRE>
  761. Herbert Tic
  762. 44 Turnip Ave., Endive, NJ
  763. 201-5555642
  764. Norbert Twinge
  765. 16 Potato St., Cabbagetown, NJ
  766. 201-5553145
  767. ...
  768. </PRE></TT></DL>
  769. The format may be encoded as a regular expression:
  770. <DL><DT><DD><TT><PRE>
  771. (.+\n)+
  772. </PRE></TT></DL>
  773. that is, a sequence of one or more non-blank lines.
  774. The command to print Mr. Tic's entire record is then
  775. <DL><DT><DD><TT><PRE>
  776. , x/(.+\n)+/ g/^Herbert Tic$/ p
  777. </PRE></TT></DL>
  778. and that to extract just the phone number is
  779. <DL><DT><DD><TT><PRE>
  780. , x/(.+\n)+/ g/^Herbert Tic$/ x/^[0-9]*-[0-9]*\n/ p
  781. </PRE></TT></DL>
  782. The latter command breaks the file into records,
  783. chooses Mr. Tic's record,
  784. extracts the phone number from the record,
  785. and finally prints the number.
  786. <P>
  787. A more involved problem is that of
  788. renaming a particular variable, say
  789. <TT>n</TT>,
  790. to
  791. <TT>num</TT>
  792. in a C program.
  793. The obvious first attempt,
  794. <DL><DT><DD><TT><PRE>
  795. , x/n/ c/num/
  796. </PRE></TT></DL>
  797. is badly flawed: it changes not only the variable
  798. <TT>n</TT>
  799. but any letter
  800. <TT>n</TT>
  801. that appears.
  802. We need to extract all the variables, and select those that match
  803. <TT>n</TT>
  804. and only
  805. <TT>n</TT>:
  806. <DL><DT><DD><TT><PRE>
  807. , x/[A-Za-z_][A-Za-z_0-9]*/ g/n/ v/../ c/num/
  808. </PRE></TT></DL>
  809. The pattern
  810. <TT>[A-Za-z_][A-Za-z_0-9]*</TT>
  811. matches C identifiers.
  812. Next
  813. <TT>g/n/</TT>
  814. selects those containing an
  815. <TT>n</TT>.
  816. Then
  817. <TT>v/../</TT>
  818. rejects those containing two (or more) characters, and finally
  819. <TT>c/num/</TT>
  820. changes the remainder (identifiers
  821. <TT>n</TT>)
  822. to
  823. <TT>num</TT>.
  824. This version clearly works much better, but there may still be problems.
  825. For example, in C character and string constants, the sequence
  826. <TT>0fP
  827. is interpreted as a newline character, and we don't want to change it to
  828. </TT><TT>0m.</TT><TT>
  829. This problem can be forestalled with a
  830. </TT><TT>y</TT><TT>
  831. command:
  832. <DL><DT><DD><TT><PRE>
  833. , y/\\n/ x/[A-Za-z_][A-Za-z_0-9]*/ g/n/ v/../ c/num/
  834. </PRE></TT></DL>
  835. (the second
  836. </TT><TT>\fP
  837. is necessary because of lexical conventions in regular expressions),
  838. or we could even reject character constants and strings outright:
  839. <DL><DT><DD><TT><PRE>
  840. ,y/'[^']*'/ y/"[^"]*"/ x/[A-Za-z_][A-Za-z_0-9]*/ g/n/ v/../ c/num/
  841. </PRE></TT></DL>
  842. The
  843. </TT><TT>y</TT><TT>
  844. commands in this version exclude from consideration all character constants
  845. and strings.
  846. The only remaining problem is to deal with the possible occurrence of
  847. </TT><TT>'</TT><TT>
  848. or
  849. </TT><TT>
  850. within these sequences, but it's easy to see how to resolve this difficulty.
  851. </P>
  852. </TT><P>
  853. The point of these composed commands is successive refinement.
  854. A simple version of the command is tried, and if it's not good enough,
  855. it can be honed by adding a clause or two.
  856. (Mistakes can be undone; see below.
  857. Also, the mouse language makes it unnecessary to retype the command each time.)
  858. The resulting chains of commands are somewhat reminiscent of
  859. shell pipelines.<sup>7</sup>
  860. Unlike pipelines, though, which pass along modified
  861. <I>data</I>,
  862. <TT>sam</TT>
  863. commands pass a
  864. <I>view</I>
  865. of the data.
  866. The text at each step of the command is the same, but which pieces
  867. are selected is refined step by step until the correct piece is
  868. available to the final step of the command line, which ultimately makes the change.
  869. </P>
  870. <P>
  871. In other UNIX programs, regular expressions are used only for selection,
  872. as in the
  873. <TT>sam</TT>
  874. <TT>g</TT>
  875. command, never for extraction as in the
  876. <TT>x</TT>
  877. or
  878. <TT>y</TT>
  879. command.
  880. For example, patterns in
  881. <TT>awk</TT><sup>7</sup>
  882. are used to select lines to be operated on, but cannot be used
  883. to describe the format of the input text, or to handle newline-free text.
  884. The use of regular expressions to describe the structure of a piece
  885. of text rather than its contents, as in the
  886. <TT>x</TT>
  887. command,
  888. has been given a name:
  889. structural regular expressions.
  890. When they are composed, as in the above example,
  891. they are pleasantly expressive.
  892. Their use is discussed at greater length elsewhere.<sup>10</sup>
  893. </P>
  894. <P>
  895. </P>
  896. <H4>Multiple files
  897. </H4>
  898. <br>&#32;<br>
  899. <TT>Sam</TT>
  900. has a few other commands, mostly relating to input and output.
  901. <DL><DT><DD><TT><PRE>
  902. e discfilename
  903. </PRE></TT></DL>
  904. replaces the contents and name of the current file with those of the named
  905. disc file;
  906. <DL><DT><DD><TT><PRE>
  907. w discfilename
  908. </PRE></TT></DL>
  909. writes the contents to the named disc file; and
  910. <DL><DT><DD><TT><PRE>
  911. r discfilename
  912. </PRE></TT></DL>
  913. replaces dot with the contents of the named disc file.
  914. All these commands use the current file's name if none is specified.
  915. Finally,
  916. <DL><DT><DD><TT><PRE>
  917. f discfilename
  918. </PRE></TT></DL>
  919. changes the name associated with the file and displays the result:
  920. <DL><DT><DD><TT><PRE>
  921. '-. discfilename
  922. </PRE></TT></DL>
  923. This output is called the file's
  924. menu line,
  925. because it is the contents of the file's line in the button 3 menu (described
  926. in the
  927. next section).
  928. The first three characters are a concise notation for the state of the file.
  929. The apostrophe signifies that the file is modified.
  930. The minus sign indicates the number of windows
  931. open on the file (see the next section):
  932. <TT>-</TT>
  933. means none,
  934. <TT>+</TT>
  935. means one, and
  936. <TT>*</TT>
  937. means more than one.
  938. Finally, the period indicates that this is the current file.
  939. These characters are useful for controlling the
  940. <TT>X</TT>
  941. command, described shortly.
  942. <P>
  943. <TT>Sam</TT>
  944. may be started with a set of disc files (such as all the source for
  945. a program) by invoking it with a list of file names as arguments, and
  946. more may be added or deleted on demand.
  947. <DL><DT><DD><TT><PRE>
  948. B discfile1 discfile2 ...
  949. </PRE></TT></DL>
  950. adds the named files to
  951. <TT>sam</TT>'s
  952. list, and
  953. <DL><DT><DD><TT><PRE>
  954. D discfile1 discfile2 ...
  955. </PRE></TT></DL>
  956. removes them from
  957. <TT>sam</TT>'s
  958. memory (without effect on associated disc files).
  959. Both these commands have a syntax for using the shell<sup>7</sup>
  960. (the UNIX command interpreter) to generate the lists:
  961. <DL><DT><DD><TT><PRE>
  962. B &#60;echo *.c
  963. </PRE></TT></DL>
  964. will add all C source files, and
  965. <DL><DT><DD><TT><PRE>
  966. B &#60;grep -l variable *.c
  967. </PRE></TT></DL>
  968. will add all C source files referencing a particular variable
  969. (the UNIX command
  970. <TT>grep -l</TT>
  971. lists all files in its arguments that contain matches of
  972. the specified regular expression).
  973. Finally,
  974. <TT>D</TT>
  975. without arguments deletes the current file.
  976. </P>
  977. <P>
  978. There are two ways to change which file is current:
  979. <DL><DT><DD><TT><PRE>
  980. b filename
  981. </PRE></TT></DL>
  982. makes the named file current.
  983. The
  984. <TT>B</TT>
  985. command
  986. does the same, but also adds any new files to
  987. <TT>sam</TT>'s
  988. list.
  989. (In practice, of course, the current file
  990. is usually chosen by mouse actions, not by textual commands.)
  991. The other way is to use a form of address that refers to files:
  992. <DL><DT><DD><TT><PRE>
  993. "<I>expression</I>" <I>address</I>
  994. </PRE></TT></DL>
  995. refers to the address evaluated in the file whose menu line
  996. matches the expression (there must be exactly one match).
  997. For example,
  998. <DL><DT><DD><TT><PRE>
  999. "peter.c" 3
  1000. </PRE></TT></DL>
  1001. refers to the third line of the file whose name matches
  1002. <TT>peter.c</TT>.
  1003. This is most useful in the move
  1004. (<TT>m</TT>)
  1005. and copy
  1006. (<TT>t</TT>)
  1007. commands:
  1008. <DL><DT><DD><TT><PRE>
  1009. 0,$ t "peter.c" 0
  1010. </PRE></TT></DL>
  1011. makes a copy of the current file at the beginning of
  1012. <TT>peter.c</TT>.
  1013. </P>
  1014. <P>
  1015. The
  1016. <TT>X</TT>
  1017. command
  1018. is a looping construct, like
  1019. <TT>x</TT>,
  1020. that refers to files instead of strings:
  1021. <DL><DT><DD><TT><PRE>
  1022. X/<I>expression</I>/ <I>command</I>
  1023. </PRE></TT></DL>
  1024. runs the command in all
  1025. files whose menu lines match the expression. The best example is
  1026. <DL><DT><DD><TT><PRE>
  1027. X/'/ w
  1028. </PRE></TT></DL>
  1029. which writes to disc all modified files.
  1030. <TT>Y</TT>
  1031. is the complement of
  1032. <TT>X</TT>:
  1033. it runs the command on all files whose menu lines don't match the expression:
  1034. <DL><DT><DD><TT><PRE>
  1035. Y/\.c/ D
  1036. </PRE></TT></DL>
  1037. deletes all files that don't have
  1038. <TT>.c</TT>
  1039. in their names, that is, it keeps all C source files and deletes the rest.
  1040. </P>
  1041. <P>
  1042. Braces allow commands to be grouped, so
  1043. <DL><DT><DD><TT><PRE>
  1044. {
  1045. <I>command1</I>
  1046. <I>command2</I>
  1047. }
  1048. </PRE></TT></DL>
  1049. is syntactically a single command that runs two commands.
  1050. Thus,
  1051. <DL><DT><DD><TT><PRE>
  1052. X/\.c/ ,g/variable/ {
  1053. f
  1054. , x/.*\n/ g/variable/ p
  1055. }
  1056. </PRE></TT></DL>
  1057. finds all occurrences of
  1058. <TT>variable</TT>
  1059. in C source files, and prints
  1060. out the file names and lines of each match.
  1061. The precise semantics of compound operations is discussed in the implementation
  1062. sections below.
  1063. </P>
  1064. <P>
  1065. Finally,
  1066. the undo command,
  1067. <TT>u</TT>,
  1068. undoes the last command,
  1069. no matter how many files were affected.
  1070. Multiple undo operations move further back in time, so
  1071. <DL><DT><DD><TT><PRE>
  1072. u
  1073. u
  1074. </PRE></TT></DL>
  1075. (which may be abbreviated
  1076. <TT>u2</TT>)
  1077. undoes the last two commands. An undo may not be undone, however, nor
  1078. may any command that adds or deletes files.
  1079. Everything else is undoable, though, including for example
  1080. <TT>e</TT>
  1081. commands:
  1082. <DL><DT><DD><TT><PRE>
  1083. e filename
  1084. u
  1085. </PRE></TT></DL>
  1086. restores the state of the file completely, including its name, dot,
  1087. and modified bit. Because of the undo, potentially dangerous commands
  1088. are not guarded by confirmations. Only
  1089. <TT>D</TT>,
  1090. which destroys the information necessary to restore itself, is protected.
  1091. It will not delete a modified file, but a second
  1092. <TT>D</TT>
  1093. of the same file will succeed regardless.
  1094. The
  1095. <TT>q</TT>
  1096. command, which exits
  1097. <TT>sam</TT>,
  1098. is similarly guarded.
  1099. </P>
  1100. <H4>Mouse Interface
  1101. </H4>
  1102. <br>&#32;<br>
  1103. <TT>Sam</TT>
  1104. is most commonly run
  1105. connected to a bitmap display and mouse for interactive editing.
  1106. The only difference in the command language
  1107. between regular, mouse-driven
  1108. <TT>sam</TT>
  1109. and
  1110. <TT>sam -d</TT>
  1111. is that if an address
  1112. is provided without a command,
  1113. <TT>sam -d</TT>
  1114. will print the text referenced by the address, but
  1115. regular
  1116. <TT>sam</TT>
  1117. will highlight it on the screen &#173; in fact,
  1118. dot is always highlighted (see Figure 2).
  1119. <br><img src="fig3.ps.11763.gif"><br>
  1120. <br>
  1121. <I>Figure 2. A
  1122. </I><TT>sam</TT><I>
  1123. window. The scroll bar down the left
  1124. represents the file, with the bubble showing the fraction
  1125. visible in the window.
  1126. The scroll bar may be manipulated by the mouse for convenient browsing.
  1127. The current text,
  1128. which is highlighted, need not fit on a line. Here it consists of one partial
  1129. line, one complete line, and final partial line.
  1130. <br>
  1131. <DL><DT><DD><TT><PRE>
  1132. </I><br>&#32;<br>
  1133. </PRE></TT></DL>
  1134. <P>
  1135. Each file may have zero or more windows open on the display.
  1136. At any time, only one window in all of
  1137. <TT>sam</TT>
  1138. is the
  1139. current window,
  1140. that is, the window to which typing and mouse actions refer;
  1141. this may be the
  1142. <TT>sam</TT>
  1143. window (that in which commands may be typed)
  1144. or one of the file windows.
  1145. When a file has multiple windows, the image of the file in each window
  1146. is always kept up to date.
  1147. The current file is the last file affected by a command,
  1148. so if the
  1149. <TT>sam</TT>
  1150. window is current,
  1151. the current window is not a window on the current file.
  1152. However, each window on a file has its own value of dot,
  1153. and when switching between windows on a single file,
  1154. the file's value of dot is changed to that of the window.
  1155. Thus, flipping between windows behaves in the obvious, convenient way.
  1156. </P>
  1157. <P>
  1158. The mouse on the Blit has three buttons, numbered left to right.
  1159. Button 3 has a list of commands to manipulate windows,
  1160. followed by a list of `menu lines' exactly as printed by the
  1161. <TT>f</TT>
  1162. command, one per file (not one per window).
  1163. These menu lines are sorted by file name.
  1164. If the list is long, the Blit menu software will make it more manageable
  1165. by generating a scrolling menu instead of an unwieldy long list.
  1166. Using the menu to select a file from the list makes that file the current
  1167. file, and the most recently current window in that file the current window.
  1168. But if that file is already current, selecting it in the menu cycles through
  1169. the windows on the file; this simple trick avoids a special menu to
  1170. choose windows on a file.
  1171. If there is no window open on the file,
  1172. <TT>sam</TT>
  1173. changes the mouse cursor to prompt the user to create one.
  1174. </P>
  1175. <P>
  1176. The commands on the button 3 menu are straightforward (see Figure 3), and
  1177. are like the commands to manipulate windows in
  1178. <TT>mux</TT>,<sup>8</sup>
  1179. the Blit's window system.
  1180. <TT>New</TT>
  1181. makes a new file, and gives it one empty window, whose size is determined
  1182. by a rectangle swept by the mouse.
  1183. <TT>Zerox</TT>
  1184. prompts for a window to be selected, and
  1185. makes a clone of that window; this is how multiple windows are created on one file.
  1186. <TT>Reshape</TT>
  1187. changes the size of the indicated window, and
  1188. <TT>close</TT>
  1189. deletes it. If that is the last window open on the file,
  1190. <TT>close</TT>
  1191. first does a
  1192. <TT>D</TT>
  1193. command on the file.
  1194. <TT>Write</TT>
  1195. is identical to a
  1196. <TT>w</TT>
  1197. command on the file; it is in the menu purely for convenience.
  1198. Finally,
  1199. <TT>~~sam~~</TT>
  1200. is a menu item that appears between the commands and the file names.
  1201. Selecting it makes the
  1202. <TT>sam</TT>
  1203. window the current window,
  1204. causing subsequent typing to be interpreted as commands.
  1205. <br><img src="fig2.ps.11764.gif"><br>
  1206. <br>
  1207. <I>Figure 3. The menu on button 3.
  1208. The black rectangle on the left is a scroll bar; the menu is limited to
  1209. the length shown to prevent its becoming unwieldy.
  1210. Above the
  1211. </I><TT>~~sam~~</TT><I>
  1212. line is a list of commands;
  1213. beneath it is a list of files, presented exactly as with the
  1214. </I><TT>f</TT><I>
  1215. command.
  1216. <br>
  1217. <DL><DT><DD><TT><PRE>
  1218. </I><br>&#32;<br>
  1219. </PRE></TT></DL>
  1220. </P>
  1221. <P>
  1222. When
  1223. <TT>sam</TT>
  1224. requests that a window be swept, in response to
  1225. <TT>new</TT>,
  1226. <TT>zerox</TT>
  1227. or
  1228. <TT>reshape</TT>,
  1229. it changes the mouse cursor from the usual arrow to a box with
  1230. a small arrow.
  1231. In this state, the mouse may be used to indicate an arbitrary rectangle by
  1232. pressing button 3 at one corner and releasing it at the opposite corner.
  1233. More conveniently,
  1234. button 3 may simply be clicked,
  1235. whereupon
  1236. <TT>sam</TT>
  1237. creates the maximal rectangle that contains the cursor
  1238. and abuts the
  1239. <TT>sam</TT>
  1240. window.
  1241. By placing the
  1242. <TT>sam</TT>
  1243. window in the middle of the screen, the user can define two regions (one above,
  1244. one below) in which stacked fully-overlapping
  1245. windows can be created with minimal fuss (see Figure 1).
  1246. This simple user interface trick makes window creation noticeably easier.
  1247. </P>
  1248. <P>
  1249. The cut-and-paste editor is essentially the same as that in Smalltalk-80.<sup>11</sup>
  1250. The text in dot is always highlighted on the screen.
  1251. When a character is typed it replaces dot, and sets dot to the null
  1252. string after the character. Thus, ordinary typing inserts text.
  1253. Button 1 is used for selection:
  1254. pressing the button, moving the mouse, and lifting the button
  1255. selects (sets dot to) the text between the points where the
  1256. button was pressed and released.
  1257. Pressing and releasing at the same point selects a null string; this
  1258. is called clicking. Clicking twice quickly, or
  1259. double clicking,
  1260. selects larger objects;
  1261. for example, double clicking in a word selects the word,
  1262. double clicking just inside an opening bracket selects the text
  1263. contained in the brackets (handling nested brackets correctly),
  1264. and similarly for
  1265. parentheses, quotes, and so on.
  1266. The double-clicking rules reflect a bias toward
  1267. programmers.
  1268. If
  1269. <TT>sam</TT>
  1270. were intended more for word processing, double-clicks would probably
  1271. select linguistic structures such as sentences.
  1272. </P>
  1273. <P>
  1274. If button 1 is pressed outside the current window, it makes the indicated
  1275. window current.
  1276. This is the easiest way to switch between windows and files.
  1277. </P>
  1278. <P>
  1279. Pressing button 2 brings up a menu of editing functions (see Figure 4).
  1280. These mostly apply to the selected text:
  1281. <TT>cut</TT>
  1282. deletes the selected text, and remembers it in a hidden buffer called the
  1283. snarf buffer,
  1284. <TT>paste</TT>
  1285. replaces the selected text by the contents of the snarf buffer,
  1286. <TT>snarf</TT>
  1287. just copies the selected text to the snarf buffer,
  1288. <TT>look</TT>
  1289. searches forward for the next literal occurrence of the selected text, and
  1290. <TT><mux></TT>
  1291. exchanges snarf buffers with the window system in which
  1292. <TT>sam</TT>
  1293. is running.
  1294. Finally, the last regular expression used appears as a menu entry
  1295. to search
  1296. forward for the next occurrence of a match for the expression.
  1297. <br><img src="fig4.ps.11765.gif"><br>
  1298. <br>
  1299. <I>Figure 4. The menu on button 2.
  1300. The bottom entry tracks the most recently used regular expression, which may
  1301. be literal text.
  1302. <br>
  1303. <DL><DT><DD><TT><PRE>
  1304. </I><br>&#32;<br>
  1305. </PRE></TT></DL>
  1306. </P>
  1307. <P>
  1308. The relationship between the command language and the mouse language is
  1309. entirely due to the equality of dot and the selected text chosen
  1310. with button 1 on the mouse.
  1311. For example, to make a set of changes in a C subroutine, dot can be
  1312. set by double clicking on the left brace that begins the subroutine,
  1313. which sets dot for the command language.
  1314. An address-free command then typed in the
  1315. <TT>sam</TT>
  1316. window will apply only to the text between the opening and closing
  1317. braces of the function.
  1318. The idea is to select what you want, and then say what you want
  1319. to do with it, whether invoked by a menu selection or by a typed command.
  1320. And of course, the value of dot is highlighted on
  1321. the display after the command completes.
  1322. This relationship between mouse interface and command language
  1323. is clumsy to explain, but comfortable, even natural, in practice.
  1324. </P>
  1325. <H4>The Implementation
  1326. </H4>
  1327. <br>&#32;<br>
  1328. The next few sections describe how
  1329. <TT>sam</TT>
  1330. is put together, first the host part,
  1331. then the inter-component communication,
  1332. then the terminal part.
  1333. After explaining how the command language is implemented,
  1334. the discussion follows (roughly) the path of a character
  1335. from the temporary file on disc to the screen.
  1336. The presentation centers on the data structures,
  1337. because that is how the program was designed and because
  1338. the algorithms are easy to provide, given the right data
  1339. structures.
  1340. <H4>Parsing and execution
  1341. </H4>
  1342. <br>&#32;<br>
  1343. The command language is interpreted by parsing each command with a
  1344. table-driven recursive
  1345. descent parser, and when a complete command is assembled, invoking a top-down
  1346. executor.
  1347. Most editors instead employ a simple character-at-a-time
  1348. lexical scanner.
  1349. Use of a parser makes it
  1350. easy and unambiguous to detect when a command is complete,
  1351. which has two advantages.
  1352. First, escape conventions such as backslashes to quote
  1353. multiple-line commands are unnecessary; if the command isn't finished,
  1354. the parser keeps reading. For example, a multiple-line append driven by an
  1355. <TT>x</TT>
  1356. command is straightforward:
  1357. <DL><DT><DD><TT><PRE>
  1358. x/.*\n/ g/Peter/ a
  1359. one line about Peter
  1360. another line about Peter
  1361. .
  1362. </PRE></TT></DL>
  1363. Other UNIX editors would require a backslash after all but the last line.
  1364. <P>
  1365. The other advantage is specific to the two-process structure of
  1366. <TT>sam</TT>.
  1367. The host process must decide when a command is completed so the
  1368. command interpreter can be called. This problem is easily resolved
  1369. by having the lexical analyzer read the single stream of events from the
  1370. terminal, directly executing all typing and mouse commands,
  1371. but passing to the parser characters typed to the
  1372. <TT>sam</TT>
  1373. command window.
  1374. This scheme is slightly complicated by the availability of cut-and-paste
  1375. editing in the
  1376. <TT>sam</TT>
  1377. window, but that difficulty is resolved by applying the rules
  1378. used in
  1379. <TT>mux</TT>:
  1380. when a newline is typed to the
  1381. <TT>sam</TT>
  1382. window, all text between the newline and the previously typed newline
  1383. is made available to the parser.
  1384. This permits arbitrary editing to be done to a command before
  1385. typing newline and thereby requesting execution.
  1386. </P>
  1387. <P>
  1388. The parser is driven by a table because the syntax of addresses
  1389. and commands is regular enough
  1390. to be encoded compactly. There are few special cases, such as the
  1391. replacement text in a substitution, so the syntax of almost all commands
  1392. can be encoded with a few flags.
  1393. These include whether the command allows an address (for example,
  1394. <TT>e</TT>
  1395. does not), whether it takes a regular expression (as in
  1396. <TT>x</TT>
  1397. and
  1398. <TT>s</TT>),
  1399. whether it takes replacement text (as in
  1400. <TT>c</TT>
  1401. or
  1402. <TT>i</TT>),
  1403. which may be multi-line, and so on.
  1404. The internal syntax of regular expressions is handled by a separate
  1405. parser; a regular expression is a leaf of the command parse tree.
  1406. Regular expressions are discussed fully in the next section.
  1407. </P>
  1408. <P>
  1409. The parser table also has information about defaults, so the interpreter
  1410. is always called with a complete tree. For example, the parser fills in
  1411. the implicit
  1412. <TT>0</TT>
  1413. and
  1414. <TT>$</TT>
  1415. in the abbreviated address
  1416. <TT>,</TT>
  1417. (comma),
  1418. inserts a
  1419. <TT>+</TT>
  1420. to the left of an unadorned regular expression in an address,
  1421. and provides the usual default address
  1422. <TT>.</TT>
  1423. (dot) for commands that expect an address but are not given one.
  1424. </P>
  1425. <P>
  1426. Once a complete command is parsed, the evaluation is easy.
  1427. The address is evaluated left-to-right starting from the value of dot,
  1428. with a mostly ordinary expression evaluator.
  1429. Addresses, like many of the data structures in
  1430. <TT>sam</TT>,
  1431. are held in a C structure and passed around by value:
  1432. <DL><DT><DD><TT><PRE>
  1433. typedef long Posn; /* Position in a file */
  1434. typedef struct Range{
  1435. Posn p1, p2;
  1436. }Range;
  1437. typedef struct Address{
  1438. Range r;
  1439. File *f;
  1440. }Address;
  1441. </PRE></TT></DL>
  1442. An address is encoded as a substring (character positions
  1443. <TT>p1</TT>
  1444. to
  1445. <TT>p2</TT>)
  1446. in a file
  1447. <TT>f</TT>.
  1448. (The data type
  1449. <TT>File</TT>
  1450. is described in detail below.)
  1451. </P>
  1452. <P>
  1453. The address interpreter is an
  1454. <TT>Address</TT>-valued
  1455. function that traverses the parse tree describing an address (the
  1456. parse tree for the address has type
  1457. <TT>Addrtree</TT>):
  1458. <DL><DT><DD><TT><PRE>
  1459. Address
  1460. address(ap, a, sign)
  1461. Addrtree *ap;
  1462. Address a;
  1463. int sign;
  1464. {
  1465. Address a2;
  1466. do
  1467. switch(ap-&#62;type){
  1468. case '.':
  1469. a=a.f-&#62;dot;
  1470. break;
  1471. case '$':
  1472. a.r.p1=a.r.p2=a.f-&#62;nbytes;
  1473. break;
  1474. case '"':
  1475. a=matchfile(a, ap-&#62;aregexp)-&#62;dot;
  1476. break;
  1477. case ',':
  1478. a2=address(ap-&#62;right, a, 0);
  1479. a=address(ap-&#62;left, a, 0);
  1480. if(a.f!=a2.f || a2.r.p2&#60;a.r.p1)
  1481. error(Eorder);
  1482. a.r.p2=a2.r.p2;
  1483. return a;
  1484. /* and so on */
  1485. }
  1486. while((ap=ap-&#62;right)!=0);
  1487. return a;
  1488. }
  1489. </PRE></TT></DL>
  1490. </P>
  1491. <P>
  1492. Throughout, errors are handled by a non-local
  1493. <TT>goto</TT>
  1494. (a
  1495. <TT>setjmp/longjmp</TT>
  1496. in C terminology)
  1497. hidden in a routine called
  1498. <TT>error</TT>
  1499. that immediately aborts the execution, retracts any
  1500. partially made changes (see the section below on `undoing'), and
  1501. returns to the top level of the parser.
  1502. The argument to
  1503. <TT>error</TT>
  1504. is an enumeration type that
  1505. is translated to a terse but possibly helpful
  1506. message such as `?addresses out of order.'
  1507. Very common messages are kept short; for example the message for
  1508. a failed regular expression search is `?search.'
  1509. </P>
  1510. <P>
  1511. Character addresses such as
  1512. <TT>#3</TT>
  1513. are trivial to implement, as the
  1514. <TT>File</TT>
  1515. data structure is accessible by character number.
  1516. However,
  1517. <TT>sam</TT>
  1518. keeps no information about the position of newlines &#173; it is too
  1519. expensive to track dynamically &#173; so line addresses are computed by reading
  1520. the file, counting newlines. Except in very large files, this has proven
  1521. acceptable: file access is fast enough to make the technique practical,
  1522. and lines are not central to the structure of the command language.
  1523. </P>
  1524. <P>
  1525. The command interpreter, called
  1526. <TT>cmdexec</TT>,
  1527. is also straightforward. The parse table includes a
  1528. function to call to interpret a particular command. That function
  1529. receives as arguments
  1530. the calculated address
  1531. for the command
  1532. and the command tree (of type
  1533. <TT>Cmdtree</TT>),
  1534. which may contain information such as the subtree for compound commands.
  1535. Here, for example, is the function for the
  1536. <TT>g</TT>
  1537. and
  1538. <TT>v</TT>
  1539. commands:
  1540. <DL><DT><DD><TT><PRE>
  1541. int
  1542. g_cmd(a, cp)
  1543. Address a;
  1544. Cmdtree *cp;
  1545. {
  1546. compile(cp-&#62;regexp);
  1547. if(execute(a.f, a.r.p1, a.r.p2)!=(cp-&#62;cmdchar=='v')){
  1548. a.f-&#62;dot=a;
  1549. return cmdexec(a, cp-&#62;subcmd);
  1550. }
  1551. return TRUE; /* cause execution to continue */
  1552. }
  1553. </PRE></TT></DL>
  1554. (<TT>Compile</TT>
  1555. and
  1556. <TT>execute</TT>
  1557. are part of the regular expression code, described in the next section.)
  1558. Because the parser and the
  1559. <TT>File</TT>
  1560. data structure do most of the work, most commands
  1561. are similarly brief.
  1562. </P>
  1563. <H4>Regular expressions
  1564. </H4>
  1565. <br>&#32;<br>
  1566. The regular expression code in
  1567. <TT>sam</TT>
  1568. is an interpreted, rather than compiled on-the-fly, implementation of Thompson's
  1569. non-deterministic finite automaton algorithm.<sup>12</sup>
  1570. The syntax and semantics of the expressions are as in the UNIX program
  1571. <TT>egrep</TT>,
  1572. including alternation, closures, character classes, and so on.
  1573. The only changes in the notation are two additions:
  1574. <TT>0fP
  1575. is translated to, and matches, a newline character, and
  1576. </TT><TT>@</TT><TT>
  1577. matches any character. In
  1578. </TT><TT>egrep</TT><TT>,
  1579. the character
  1580. </TT><TT>.</TT><TT>
  1581. matches any character except newline, and in
  1582. </TT><TT>sam</TT><TT>
  1583. the same rule seemed safest, to prevent idioms like
  1584. </TT><TT>.*</TT><TT>
  1585. from spanning newlines.
  1586. </TT><TT>Egrep</TT><TT>
  1587. expressions are arguably too complicated for an interactive editor &#173;
  1588. certainly it would make sense if all the special characters were two-character
  1589. sequences, so that most of the punctuation characters wouldn't have
  1590. peculiar meanings &#173; but for an interesting command language, full
  1591. regular expressions are necessary, and
  1592. </TT><TT>egrep</TT><TT>
  1593. defines the full regular expression syntax for UNIX programs.
  1594. Also, it seemed superfluous to define a new syntax, since various UNIX programs
  1595. (</TT><TT>ed</TT><TT>,
  1596. </TT><TT>egrep</TT><TT>
  1597. and
  1598. </TT><TT>vi</TT><TT>)
  1599. define too many already.
  1600. </TT><P>
  1601. The expressions are compiled by a routine,
  1602. <TT>compile</TT>,
  1603. that generates the description of the non-deterministic finite state machine.
  1604. A second routine,
  1605. <TT>execute</TT>,
  1606. interprets the machine to generate the leftmost-longest match of the
  1607. expression in a substring of the file.
  1608. The algorithm is described elsewhere.<sup>12,13</sup>
  1609. <TT>Execute</TT>
  1610. reports
  1611. whether a match was found, and sets a global variable,
  1612. of type
  1613. <TT>Range</TT>,
  1614. to the substring matched.
  1615. </P>
  1616. <P>
  1617. A trick is required to evaluate the expression in reverse, such as when
  1618. searching backwards for an expression.
  1619. For example,
  1620. <DL><DT><DD><TT><PRE>
  1621. -/P.*r/
  1622. </PRE></TT></DL>
  1623. looks backwards through the file for a match of the expression.
  1624. The expression, however, is defined for a forward search.
  1625. The solution is to construct a machine identical to the machine
  1626. for a forward search except for a reversal of all the concatenation
  1627. operators (the other operators are symmetric under direction reversal),
  1628. to exchange the meaning of the operators
  1629. <TT>^</TT>
  1630. and
  1631. <TT>$</TT>,
  1632. and then to read the file backwards, looking for the
  1633. usual earliest longest match.
  1634. </P>
  1635. <P>
  1636. <TT>Execute</TT>
  1637. generates only one match each time it is called.
  1638. To interpret looping constructs such as the
  1639. <TT>x</TT>
  1640. command,
  1641. <TT>sam</TT>
  1642. must therefore synchronize between
  1643. calls of
  1644. <TT>execute</TT>
  1645. to avoid
  1646. problems with null matches.
  1647. For example, even given the leftmost-longest rule,
  1648. the expression
  1649. <TT>a*</TT>
  1650. matches three times in the string
  1651. <TT>ab</TT>
  1652. (the character
  1653. <TT>a</TT>,
  1654. the null string between the
  1655. <TT>a</TT>
  1656. and
  1657. <TT>b</TT>,
  1658. and the final null string).
  1659. After returning a match for the
  1660. <TT>a</TT>,
  1661. <TT>sam</TT>
  1662. must not match the null string before the
  1663. <TT>b</TT>.
  1664. The algorithm starts
  1665. <TT>execute</TT>
  1666. at the end of its previous match, and
  1667. if the match it returns
  1668. is null and abuts the previous match, rejects the match and advances
  1669. the initial position one character.
  1670. </P>
  1671. <H4>Memory allocation
  1672. </H4>
  1673. <br>&#32;<br>
  1674. The C language has no memory allocation primitives, although a standard
  1675. library routine,
  1676. <TT>malloc</TT>,
  1677. provides adequate service for simple programs.
  1678. For specific uses, however,
  1679. it can be better to write a custom allocator.
  1680. The allocator (or rather, pair of allocators) described here
  1681. work in both the terminal and host parts of
  1682. <TT>sam</TT>.
  1683. They are designed for efficient manipulation of strings,
  1684. which are allocated and freed frequently and vary in length from essentially
  1685. zero to 32 Kbytes (very large strings are written to disc).
  1686. More important, strings may be large and change size often,
  1687. so to minimize memory usage it is helpful to reclaim and to coalesce the
  1688. unused portions of strings when they are truncated.
  1689. <P>
  1690. Objects to be allocated in
  1691. <TT>sam</TT>
  1692. are of two flavors:
  1693. the first is C
  1694. <TT>structs</TT>,
  1695. which are small and often addressed by pointer variables;
  1696. the second is variable-sized arrays of characters
  1697. or integers whose
  1698. base pointer is always used to access them.
  1699. The memory allocator in
  1700. <TT>sam</TT>
  1701. is therefore in two parts:
  1702. first, a traditional first-fit allocator that provides fixed storage for
  1703. <TT>structs</TT>;
  1704. and second, a garbage-compacting allocator that reduces storage
  1705. overhead for variable-sized objects, at the cost of some bookkeeping.
  1706. The two types of objects are allocated from adjoining arenas, with
  1707. the garbage-compacting allocator controlling the arena with higher addresses.
  1708. Separating into two arenas simplifies compaction and prevents fragmentation due
  1709. to immovable objects.
  1710. The access rules for garbage-compactable objects
  1711. (discussed in the next paragraph) allow them to be relocated, so when
  1712. the first-fit arena needs space, it moves the garbage-compacted arena
  1713. to higher addresses to make room. Storage is therefore created only
  1714. at successively higher addresses, either when more garbage-compacted
  1715. space is needed or when the first-fit arena pushes up the other arena.
  1716. </P>
  1717. <P>
  1718. Objects that may be compacted declare to the
  1719. allocator a cell that is guaranteed to be the sole repository of the
  1720. address of the object whenever a compaction can occur.
  1721. The compactor can then update the address when the object is moved.
  1722. For example, the implementation of type
  1723. <TT>List</TT>
  1724. (really a variable-length array)
  1725. is:
  1726. <DL><DT><DD><TT><PRE>
  1727. typedef struct List{
  1728. int nused;
  1729. long *ptr;
  1730. }List;
  1731. </PRE></TT></DL>
  1732. The
  1733. <TT>ptr</TT>
  1734. cell must always be used directly, and never copied. When a
  1735. <TT>List</TT>
  1736. is to be created the
  1737. <TT>List</TT>
  1738. structure is allocated in the ordinary first-fit arena
  1739. and its
  1740. <TT>ptr</TT>
  1741. is allocated in the garbage-compacted arena.
  1742. A similar data type for strings, called
  1743. <TT>String</TT>,
  1744. stores variable-length character arrays of up to 32767 elements.
  1745. </P>
  1746. <P>
  1747. A related matter of programming style:
  1748. <TT>sam</TT>
  1749. frequently passes structures by value, which
  1750. simplifies the code.
  1751. Traditionally, C programs have
  1752. passed structures by reference, but implicit allocation on
  1753. the stack is easier to use.
  1754. Structure passing is a relatively new feature of C
  1755. (it is not in the
  1756. standard reference manual for C<sup>14</sup>), and is poorly supported in most
  1757. commercial C compilers.
  1758. It's convenient and expressive, though,
  1759. and simplifies memory management by
  1760. avoiding the allocator altogether
  1761. and eliminating pointer aliases.
  1762. </P>
  1763. <H4>Data structures for manipulating files
  1764. </H4>
  1765. <br>&#32;<br>
  1766. Experience with
  1767. <TT>jim</TT>
  1768. showed that the requirements
  1769. of the file data structure were few, but strict.
  1770. First, files need to be read and written quickly;
  1771. adding a fresh file must be painless.
  1772. Second, the implementation must place no arbitrary upper limit on
  1773. the number or sizes of files. (It should be practical to edit many files,
  1774. and files up to megabytes in length should be handled gracefully.)
  1775. This implies that files be stored on disc, not in main memory.
  1776. (Aficionados of virtual memory may argue otherwise, but the
  1777. implementation of virtual
  1778. memory in our system is not something to depend on
  1779. for good performance.)
  1780. Third, changes to files need be made by only two primitives:
  1781. deletion and insertion.
  1782. These are inverses of each other,
  1783. which simplifies the implementation of the undo operation.
  1784. Finally,
  1785. it must be easy and efficient to access the file, either
  1786. forwards or backwards, a byte at a time.
  1787. <P>
  1788. The
  1789. <TT>File</TT>
  1790. data type is constructed from three simpler data structures that hold arrays
  1791. of characters.
  1792. Each of these types has an insertion and deletion operator, and the
  1793. insertion and deletion operators of the
  1794. <TT>File</TT>
  1795. type itself are constructed from them.
  1796. </P>
  1797. <P>
  1798. The simplest type is the
  1799. <TT>String</TT>,
  1800. which is used to hold strings in main memory.
  1801. The code that manages
  1802. <TT>Strings</TT>
  1803. guarantees that they will never be longer
  1804. than some moderate size, and in practice they are rarely larger than 8 Kbytes.
  1805. <TT>Strings</TT>
  1806. have two purposes: they hold short strings like file names with little overhead,
  1807. and because they are deliberately small, they are efficient to modify.
  1808. They are therefore used as the data structure for in-memory caches.
  1809. </P>
  1810. <P>
  1811. The disc copy of the file is managed by a data structure called a
  1812. <TT>Disc</TT>,
  1813. which corresponds to a temporary file. A
  1814. <TT>Disc</TT>
  1815. has no storage in main memory other than bookkeeping information;
  1816. the actual data being held is all on the disc.
  1817. To reduce the number of open files needed,
  1818. <TT>sam</TT>
  1819. opens a dozen temporary UNIX files and multiplexes the
  1820. <TT>Discs</TT>
  1821. upon them.
  1822. This permits many files to
  1823. be edited; the entire
  1824. <TT>sam</TT>
  1825. source (48 files) may be edited comfortably with a single
  1826. instance of
  1827. <TT>sam</TT>.
  1828. Allocating one temporary file per
  1829. <TT>Disc</TT>
  1830. would strain the operating system's limit on the number of open files.
  1831. Also, spreading the traffic among temporary files keeps the files shorter,
  1832. and shorter files are more efficiently implemented by the UNIX
  1833. I/O subsystem.
  1834. </P>
  1835. <P>
  1836. A
  1837. <TT>Disc</TT>
  1838. is an array of fixed-length blocks, each of which contains
  1839. between 1 and 4096 characters of active data.
  1840. (The block size of our UNIX file system is 4096 bytes.)
  1841. The block addresses within the temporary file and the length of each
  1842. block are stored in a
  1843. <TT>List</TT>.
  1844. When changes are made the live part of blocks may change size.
  1845. Blocks are created and coalesced when necessary to try to keep the sizes
  1846. between 2048 and 4096 bytes.
  1847. An actively changing part of the
  1848. <TT>Disc</TT>
  1849. therefore typically has about a kilobyte of slop that can be
  1850. inserted or deleted
  1851. without changing more than one block or affecting the block order.
  1852. When an insertion would overflow a block, the block is split, a new one
  1853. is allocated to receive the overflow, and the memory-resident list of blocks
  1854. is rearranged to reflect the insertion of the new block.
  1855. </P>
  1856. <P>
  1857. Obviously, going to the disc for every modification to the file is
  1858. prohibitively expensive.
  1859. The data type
  1860. <TT>Buffer</TT>
  1861. consists of a
  1862. <TT>Disc</TT>
  1863. to hold the data and a
  1864. <TT>String</TT>
  1865. that acts as a cache.
  1866. This is the first of a series of caches throughout the data structures in
  1867. <TT>sam.</TT>
  1868. The caches not only improve performance, they provide a way to organize
  1869. the flow of data, particularly in the communication between the host
  1870. and terminal.
  1871. This idea is developed below, in the section on communications.
  1872. </P>
  1873. <P>
  1874. To reduce disc traffic, changes to a
  1875. <TT>Buffer</TT>
  1876. are mediated by a variable-length string, in memory, that acts as a cache.
  1877. When an insertion or deletion is made to a
  1878. <TT>Buffer</TT>,
  1879. if the change can be accommodated by the cache, it is done there.
  1880. If the cache becomes bigger than a block because of an insertion,
  1881. some of it is written to the
  1882. <TT>Disc</TT>
  1883. and deleted from the cache.
  1884. If the change does not intersect the cache, the cache is flushed.
  1885. The cache is only loaded at the new position if the change is smaller than a block;
  1886. otherwise, it is sent directly to the
  1887. <TT>Disc</TT>.
  1888. This is because
  1889. large changes are typically sequential,
  1890. whereupon the next change is unlikely to overlap the current one.
  1891. </P>
  1892. <P>
  1893. A
  1894. <TT>File</TT>
  1895. comprises a
  1896. <TT>String</TT>
  1897. to hold the file name and some ancillary data such as dot and the modified bit.
  1898. The most important components, though, are a pair of
  1899. <TT>Buffers</TT>,
  1900. one called the transcript and the other the contents.
  1901. Their use is described in the next section.
  1902. </P>
  1903. <P>
  1904. The overall structure is shown in Figure 5.
  1905. Although it may seem that the data is touched many times on its
  1906. way from the
  1907. <TT>Disc</TT>,
  1908. it is read (by one UNIX system call) directly into the cache of the
  1909. associated
  1910. <TT>Buffer</TT>;
  1911. no extra copy is done.
  1912. Similarly, when flushing the cache, the text is written
  1913. directly from the cache to disc.
  1914. Most operations act directly on the text in the cache.
  1915. A principle applied throughout
  1916. <TT>sam</TT>
  1917. is that the fewer times the data is copied, the faster the program will run
  1918. (see also the paper by Waite<sup>15</sup>).
  1919. <DL><DT><DD><TT><PRE>
  1920. <br><img src="-.11766.gif"><br>
  1921. <br>
  1922. </PRE></TT></DL>
  1923. <I>Figure 5. File data structures.
  1924. The temporary files are stored in the standard repository for such files
  1925. on the host system.
  1926. <br>
  1927. <DL><DT><DD><TT><PRE>
  1928. </I><br>&#32;<br>
  1929. </PRE></TT></DL>
  1930. </P>
  1931. <P>
  1932. The contents of a
  1933. <TT>File</TT>
  1934. are accessed by a routine that
  1935. copies to a buffer a substring of a file starting at a specified offset.
  1936. To read a byte at a time, a
  1937. per-<TT>File</TT>
  1938. array is loaded starting from a specified initial position,
  1939. and bytes may then be read from the array.
  1940. The implementation is done by a macro similar to the C standard I/O
  1941. <TT>getc</TT>
  1942. macro.<sup>14</sup>
  1943. Because the reading may be done at any address, a minor change to the
  1944. macro allows the file to be read backwards.
  1945. This array is read-only; there is no
  1946. <TT>putc</TT>.
  1947. </P>
  1948. <H4>Doing and undoing
  1949. </H4>
  1950. <br>&#32;<br>
  1951. <TT>Sam</TT>
  1952. has an unusual method for managing changes to files.
  1953. The command language makes it easy to specify multiple variable-length changes
  1954. to a file millions of bytes long, and such changes
  1955. must be made efficiently if the editor is to be practical.
  1956. The usual techniques for inserting and deleting strings
  1957. are inadequate under these conditions.
  1958. The
  1959. <TT>Buffer</TT>
  1960. and
  1961. <TT>Disc</TT>
  1962. data structures are designed for efficient random access to long strings,
  1963. but care must be taken to avoid super-linear behavior when making
  1964. many changes simultaneously.
  1965. <P>
  1966. <TT>Sam</TT>
  1967. uses a two-pass algorithm for making changes, and treats each file as a database
  1968. against which transactions are registered.
  1969. Changes are not made directly to the contents.
  1970. Instead, when a command is started, a `mark' containing
  1971. a sequence number is placed in the transcript
  1972. <TT>Buffer</TT>,
  1973. and each change made to the file, either an insertion or deletion
  1974. or a change to the file name,
  1975. is appended to the end of the transcript.
  1976. When the command is complete, the transcript is rewound to the
  1977. mark and applied to the contents.
  1978. </P>
  1979. <P>
  1980. One reason for separating evaluation from
  1981. application in this way is to simplify tracking the addresses of changes
  1982. made in the middle of a long sequence.
  1983. The two-pass algorithm also allows all changes to apply to the
  1984. <I>original</I>
  1985. data: no change can affect another change made in the same command.
  1986. This is particularly important when evaluating an
  1987. <TT>x</TT>
  1988. command because it prevents regular expression matches
  1989. from stumbling over changes made earlier in the execution.
  1990. Also, the two-pass
  1991. algorithm is cleaner than the way other UNIX editors allow changes to
  1992. affect each other;
  1993. for example,
  1994. <TT>ed</TT>'s
  1995. idioms to do things like delete every other line
  1996. depend critically on the implementation.
  1997. Instead,
  1998. <TT>sam</TT>'s
  1999. simple model, in which all changes in a command occur effectively
  2000. simultaneously, is easy to explain and to understand.
  2001. </P>
  2002. <P>
  2003. The records in the transcript are of the form ``delete substring from
  2004. locations
  2005. 123 to 456'' and ``insert 11 characters `hello there' at location 789.''
  2006. (It is an error if the changes are not at monotonically greater
  2007. positions through the file.)
  2008. While the update is occurring, these numbers must be
  2009. offset by earlier changes, but that is straightforward and
  2010. local to the update routine;
  2011. moreover, all the numbers have been computed
  2012. before the first is examined.
  2013. </P>
  2014. <P>
  2015. Treating the file as a transaction system has another advantage:
  2016. undo is trivial.
  2017. All it takes is to invert the transcript after it has been
  2018. implemented, converting insertions
  2019. into deletions and vice versa, and saving them in a holding
  2020. <TT>Buffer</TT>.
  2021. The `do' transcript can then be deleted from
  2022. the transcript
  2023. <TT>Buffer</TT>
  2024. and replaced by the `undo' transcript.
  2025. If an undo is requested, the transcript is rewound and the undo transcript
  2026. executed.
  2027. Because the transcript
  2028. <TT>Buffer</TT>
  2029. is not truncated after each command, it accumulates
  2030. successive changes.
  2031. A sequence of undo commands
  2032. can therefore back up the file arbitrarily,
  2033. which is more helpful than the more commonly implemented self-inverse form of undo.
  2034. (<TT>Sam</TT>
  2035. provides no way to undo an undo, but if it were desired,
  2036. it would be easy to provide by re-interpreting the `do' transcript.)
  2037. Each mark in the transcript contains a sequence number and the offset into
  2038. the transcript of the previous mark, to aid in unwinding the transcript.
  2039. Marks also contain the value of dot and the modified bit so these can be
  2040. restored easily.
  2041. Undoing multiple files is easy; it merely demands undoing all files whose
  2042. latest change has the same sequence number as the current file.
  2043. </P>
  2044. <P>
  2045. Another benefit of having a transcript is that errors encountered in the middle
  2046. of a complicated command need not leave the files in an intermediate state.
  2047. By rewinding the transcript to the mark beginning the command,
  2048. the partial command can be trivially undone.
  2049. </P>
  2050. <P>
  2051. When the update algorithm was first implemented, it was unacceptably slow,
  2052. so a cache was added to coalesce nearby changes,
  2053. replacing multiple small changes by a single larger one.
  2054. This reduced the number
  2055. of insertions into the transaction
  2056. <TT>Buffer</TT>,
  2057. and made a dramatic improvement in performance,
  2058. but made it impossible
  2059. to handle changes in non-monotonic order in the file; the caching method
  2060. only works if changes don't overlap.
  2061. Before the cache was added, the transaction could in principle be sorted
  2062. if the changes were out of order, although
  2063. this was never done.
  2064. The current status is therefore acceptable performance with a minor
  2065. restriction on global changes, which is sometimes, but rarely, an annoyance.
  2066. </P>
  2067. <P>
  2068. The update algorithm obviously paws the data more than simpler
  2069. algorithms, but it is not prohibitively expensive;
  2070. the caches help.
  2071. (The principle of avoiding copying the data is still honored here,
  2072. although not as piously:
  2073. the data is moved from contents' cache to
  2074. the transcript's all at once and through only one internal buffer.)
  2075. Performance figures confirm the efficiency.
  2076. To read from a dead start a hundred kilobyte file on a VAX-11/750
  2077. takes 1.4 seconds of user time, 2.5 seconds of system time,
  2078. and 5 seconds of real time.
  2079. Reading the same file in
  2080. <TT>ed</TT>
  2081. takes 6.0 seconds of user time, 1.7 seconds of system time,
  2082. and 8 seconds of real time.
  2083. <TT>Sam</TT>
  2084. uses about half the CPU time.
  2085. A more interesting example is the one stated above:
  2086. inserting a character between every pair of characters in the file.
  2087. The
  2088. <TT>sam</TT>
  2089. command is
  2090. <DL><DT><DD><TT><PRE>
  2091. ,y/@/ a/x/
  2092. </PRE></TT></DL>
  2093. and takes 3 CPU seconds per kilobyte of input file, of which
  2094. about a third is spent in the regular expression code.
  2095. This translates to about 500 changes per second.
  2096. <TT>Ed</TT>
  2097. takes 1.5 seconds per kilobyte to make a similar change (ignoring newlines),
  2098. but cannot undo it.
  2099. The same example in
  2100. <TT>ex</TT>,<sup>9</sup>
  2101. a variant of
  2102. <TT>ed</TT>
  2103. done at the University of California at Berkeley,
  2104. which allows one level of undoing, again takes 3 seconds.
  2105. In summary,
  2106. <TT>sam</TT>'s
  2107. performance is comparable to that of other UNIX editors, although it solves
  2108. a harder problem.
  2109. </P>
  2110. <H4>Communications
  2111. </H4>
  2112. <br>&#32;<br>
  2113. The discussion so far has described the implementation of the host part of
  2114. <TT>sam</TT>;
  2115. the next few sections explain how a machine with mouse and bitmap display
  2116. can be engaged to improve interaction.
  2117. <TT>Sam</TT>
  2118. is not the first editor to be written as two processes,<sup>16</sup>
  2119. but its implementation
  2120. has some unusual aspects.
  2121. <P>
  2122. There are several ways
  2123. <TT>sam</TT>'s
  2124. host and terminal parts may be connected.
  2125. The first and simplest is to forgo the terminal part and use the host
  2126. part's command language to edit text on an ordinary terminal.
  2127. This mode is invoked by starting
  2128. <TT>sam</TT>
  2129. with the
  2130. <TT>-d</TT>
  2131. option.
  2132. With no options,
  2133. <TT>sam</TT>
  2134. runs separate host and terminal programs,
  2135. communicating with a message protocol over the physical
  2136. connection that joins them.
  2137. Typically, the connection is an RS-232 link between a Blit
  2138. (the prototypical display for
  2139. <TT>sam</TT>)
  2140. and a host running
  2141. the Ninth Edition of the UNIX operating system.<sup>8</sup>
  2142. (This is the version of the system used in the Computing Sciences Research
  2143. Center at AT&amp;T Bell Laboratories [now Lucent Technologies, Bell Labs], where I work. Its relevant
  2144. aspects are discussed in the Blit paper.<sup>1</sup>)
  2145. The implementation of
  2146. <TT>sam</TT>
  2147. for the SUN computer runs both processes on the same machine and
  2148. connects them by a pipe.
  2149. </P>
  2150. <P>
  2151. The low bandwidth of an RS-232 link
  2152. necessitated the split between
  2153. the two programs.
  2154. The division is a mixed blessing:
  2155. a program in two parts is much harder to write and to debug
  2156. than a self-contained one,
  2157. but the split makes several unusual configurations possible.
  2158. The terminal may be physically separated from the host, allowing the conveniences
  2159. of a mouse and bitmap display to be taken home while leaving the files at work.
  2160. It is also possible to run the host part on a remote machine:
  2161. <DL><DT><DD><TT><PRE>
  2162. sam -r host
  2163. </PRE></TT></DL>
  2164. connects to the terminal in the usual way, and then makes a call
  2165. across the network to establish the host part of
  2166. <TT>sam</TT>
  2167. on the named machine.
  2168. Finally, it cross-connects the I/O to join the two parts.
  2169. This allows
  2170. <TT>sam</TT>
  2171. to be run on machines that do not support bitmap displays;
  2172. for example,
  2173. <TT>sam</TT>
  2174. is the editor of choice on our Cray X-MP/24.
  2175. <TT>Sam</TT>
  2176. <TT>-r</TT>
  2177. involves
  2178. <I>three</I>
  2179. machines: the remote host, the terminal, and the local host.
  2180. The local host's job is simple but vital: it passes the data
  2181. between the remote host and terminal.
  2182. </P>
  2183. <P>
  2184. The host and terminal exchange messages asynchronously
  2185. (rather than, say, as remote procedure calls) but there is no
  2186. error detection or correction
  2187. because, whatever the configuration, the connection is reliable.
  2188. Because the terminal handles mundane interaction tasks such as
  2189. popping up menus and interpreting the responses, the messages are about
  2190. data, not actions.
  2191. For example, the host knows nothing about what is displayed on the screen,
  2192. and when the user types a character, the message sent to the host says
  2193. ``insert a one-byte string at location 123 in file 7,'' not ``a character
  2194. was typed at the current position in the current file.''
  2195. In other words, the messages look very much like the transaction records
  2196. in the transcripts.
  2197. </P>
  2198. <P>
  2199. Either the host or terminal part of
  2200. <TT>sam</TT>
  2201. may initiate a change to a file.
  2202. The command language operates on the host, while typing and some
  2203. mouse operations are executed directly in the terminal to optimize response.
  2204. Changes initiated by the host program must be transmitted to the terminal,
  2205. and
  2206. vice versa.
  2207. (A token is exchanged to determine which end is in control,
  2208. which means that characters typed while a time-consuming command runs
  2209. must be buffered and do not appear until the command is complete.)
  2210. To maintain consistent information,
  2211. the host and terminal track changes through a per-file
  2212. data structure that records what portions of the file
  2213. the terminal has received.
  2214. The data structure, called a
  2215. <TT>Rasp</TT>
  2216. (a weak pun: it's a file with holes)
  2217. is held and updated by both the host and terminal.
  2218. A
  2219. <TT>Rasp</TT>
  2220. is a list of
  2221. <TT>Strings</TT>
  2222. holding those parts of the file known to the terminal,
  2223. separated by counts of the number of bytes in the interstices.
  2224. Of course, the host doesn't keep a separate copy of the data (it only needs
  2225. the lengths of the various pieces),
  2226. but the structure is the same on both ends.
  2227. </P>
  2228. <P>
  2229. The
  2230. <TT>Rasp</TT>
  2231. in the terminal doubles as a cache.
  2232. Since the terminal keeps the text for portions of the file it has displayed,
  2233. it need not request data from the host when revisiting old parts of the file
  2234. or redrawing obscured windows, which speeds things up considerably
  2235. over low-speed links.
  2236. </P>
  2237. <P>
  2238. It's trivial for the terminal to maintain its
  2239. <TT>Rasp</TT>,
  2240. because all changes made on the terminal apply to parts of the file
  2241. already loaded there.
  2242. Changes made by the host are compared against the
  2243. <TT>Rasp</TT>
  2244. during the update sequence after each command.
  2245. Small changes to pieces of the file loaded in the terminal
  2246. are sent in their entirety.
  2247. Larger changes, and changes that fall entirely in the holes,
  2248. are transmitted as messages without literal data:
  2249. only the lengths of the deleted and inserted strings are transmitted.
  2250. When a command is completed, the terminal examines its visible
  2251. windows to see if any holes in their
  2252. <TT>Rasps</TT>
  2253. intersect the visible portion of the file.
  2254. It then requests the missing data from the host,
  2255. along with up to 512 bytes of surrounding data, to minimize
  2256. the number of messages when visiting a new portion of the file.
  2257. This technique provides a kind of two-level lazy evaluation for the terminal.
  2258. The first level sends a minimum of information about
  2259. parts of the file not being edited interactively;
  2260. the second level waits until a change is displayed before
  2261. transmitting the new data.
  2262. Of course,
  2263. performance is also helped by having the terminal respond immediately to typing
  2264. and simple mouse requests.
  2265. Except for small changes to active pieces of the file, which are
  2266. transmitted to the terminal without negotiation,
  2267. the terminal is wholly responsible for deciding what is displayed;
  2268. the host uses the
  2269. <TT>Rasp</TT>
  2270. only to tell the terminal what might be relevant.
  2271. </P>
  2272. <P>
  2273. When a change is initiated by the host,
  2274. the messages to the terminal describing the change
  2275. are generated by the routine that applies the transcript of the changes
  2276. to the contents of the
  2277. <TT>File</TT>.
  2278. Since changes are undone by the same update routine,
  2279. undoing requires
  2280. no extra code in the communications;
  2281. the usual messages describing changes to the file are sufficient
  2282. to back up the screen image.
  2283. </P>
  2284. <P>
  2285. The
  2286. <TT>Rasp</TT>
  2287. is a particularly good example of the way caches are used in
  2288. <TT>sam</TT>.
  2289. First, it facilitates access to the active portion of the text by placing
  2290. the busy text in main memory.
  2291. In so doing, it provides efficient access
  2292. to a large data structure that does not fit in memory.
  2293. Since the form of data is to be imposed by the user, not by the program,
  2294. and because characters will frequently be scanned sequentially,
  2295. files are stored as flat objects.
  2296. Caches help keep performance good and linear when working with such
  2297. data.
  2298. </P>
  2299. <P>
  2300. Second, the
  2301. <TT>Rasp</TT>
  2302. and several of the other caches have some
  2303. <I>read-ahead;</I>
  2304. that is, the cache is loaded with more information than is needed for
  2305. the job immediately at hand.
  2306. When manipulating linear structures, the accesses are usually sequential,
  2307. and read-ahead can significantly reduce the average time to access the
  2308. next element of the object.
  2309. Sequential access is a common mode for people as well as programs;
  2310. consider scrolling through a document while looking for something.
  2311. </P>
  2312. <P>
  2313. Finally, like any good data structure,
  2314. the cache guides the algorithm, or at least the implementation.
  2315. The
  2316. <TT>Rasp</TT>
  2317. was actually invented to control the communications between the host and
  2318. terminal parts, but I realized very early that it was also a form of
  2319. cache. Other caches were more explicitly intended to serve a double
  2320. purpose: for example, the caches in
  2321. <TT>Files</TT>
  2322. that coalesce updates not only reduce traffic to the
  2323. transcript and contents
  2324. <TT>Buffers</TT>,
  2325. they also clump screen updates so that complicated changes to the
  2326. screen are achieved in
  2327. just a few messages to the terminal.
  2328. This saved me considerable work: I did not need to write special
  2329. code to optimize the message traffic to the
  2330. terminal.
  2331. Caches pay off in surprising ways.
  2332. Also, they tend to be independent, so their performance improvements
  2333. are multiplicative.
  2334. </P>
  2335. <H4>Data structures in the terminal
  2336. </H4>
  2337. <br>&#32;<br>
  2338. The terminal's job is to display and to maintain a consistent image of
  2339. pieces of the files being edited.
  2340. Because the text is always in memory, the data structures are
  2341. considerably simpler than those in the host part.
  2342. <P>
  2343. <TT>Sam</TT>
  2344. typically has far more windows than does
  2345. <TT>mux</TT>,
  2346. the window system within which its Blit implementation runs.
  2347. <TT>Mux</TT>
  2348. has a fairly small number of asynchronously updated windows;
  2349. <TT>sam</TT>
  2350. needs a large number of synchronously updated windows that are
  2351. usually static and often fully obscured.
  2352. The different tradeoffs guided
  2353. <TT>sam</TT>
  2354. away from the memory-intensive implementation of windows, called
  2355. <TT>Layers</TT>,<sup>17</sup>
  2356. used in
  2357. <TT>mux.</TT>
  2358. Rather than depending on a complete bitmap image of the display for each window,
  2359. <TT>sam</TT>
  2360. regenerates the image from its in-memory text
  2361. (stored in the
  2362. <TT>Rasp</TT>)
  2363. when necessary, although it will use such an image if it is available.
  2364. Like
  2365. <TT>Layers</TT>,
  2366. though,
  2367. <TT>sam</TT>
  2368. uses the screen bitmap as active storage in which to update the image using
  2369. <TT>bitblt</TT>.<sup>18,19</sup>
  2370. The resulting organization, pictured in Figure 6,
  2371. has a global array of windows, called
  2372. <TT>Flayers</TT>,
  2373. each of which holds an image of a piece of text held in a data structure
  2374. called a
  2375. <TT>Frame</TT>,
  2376. which in turn represents
  2377. a rectangular window full of text displayed in some
  2378. <TT>Bitmap</TT>.
  2379. Each
  2380. <TT>Flayer</TT>
  2381. appears in a global list that orders them all front-to-back
  2382. on the display, and simultaneously as an element of a per-file array
  2383. that holds all the open windows for that file.
  2384. The complement in the terminal of the
  2385. <TT>File</TT>
  2386. on the host is called a
  2387. <TT>Text</TT>;
  2388. each connects its
  2389. <TT>Flayers</TT>
  2390. to the associated
  2391. <TT>Rasp</TT>.
  2392. <DL><DT><DD><TT><PRE>
  2393. <br><img src="-.11767.gif"><br>
  2394. <br>
  2395. </PRE></TT></DL>
  2396. <I>Figure 6. Data structures in the terminal.
  2397. </I><TT>Flayers</TT><I>
  2398. are also linked together into a front-to-back list.
  2399. </I><TT>Boxes</TT><I>
  2400. are discussed in the next section.
  2401. <br>
  2402. <DL><DT><DD><TT><PRE>
  2403. </I><br>&#32;<br>
  2404. </PRE></TT></DL>
  2405. </P>
  2406. <P>
  2407. The
  2408. <TT>Bitmap</TT>
  2409. for a
  2410. <TT>Frame</TT>
  2411. contains the image of the text.
  2412. For a fully visible window, the
  2413. <TT>Bitmap</TT>
  2414. will be the screen (or at least the
  2415. <TT>Layer</TT>
  2416. in which
  2417. <TT>sam</TT>
  2418. is being run),
  2419. while for partially obscured windows the
  2420. <TT>Bitmap</TT>
  2421. will be off-screen.
  2422. If the window is fully obscured, the
  2423. <TT>Bitmap</TT>
  2424. will be null.
  2425. </P>
  2426. <P>
  2427. The
  2428. <TT>Bitmap</TT>
  2429. is a kind of cache.
  2430. When making changes to the display, most of the original image will
  2431. look the same in the final image, and the update algorithms exploit this.
  2432. The
  2433. <TT>Frame</TT>
  2434. software updates the image in the
  2435. <TT>Bitmap</TT>
  2436. incrementally; the
  2437. <TT>Bitmap</TT>
  2438. is not just an image, it is a data structure.<sup>18,19</sup>
  2439. The job of the software that updates the display is therefore
  2440. to use as much as possible of the existing image (converting the
  2441. text from ASCII characters to pixels is expensive) in a sort of two-dimensional
  2442. string insertion algorithm.
  2443. The details of this process are described in the next section.
  2444. </P>
  2445. <P>
  2446. The
  2447. <TT>Frame</TT>
  2448. software has no code to support overlapping windows;
  2449. its job is to keep a single
  2450. <TT>Bitmap</TT>
  2451. up to date.
  2452. It falls to the
  2453. <TT>Flayer</TT>
  2454. software to multiplex the various
  2455. <TT>Bitmaps</TT>
  2456. onto the screen.
  2457. The problem of maintaining overlapping
  2458. <TT>Flayers</TT>
  2459. is easier than for
  2460. <TT>Layers</TT><sup>17</sup>
  2461. because changes are made synchronously and because the contents of the window
  2462. can be reconstructed from the data stored in the
  2463. <TT>Frame</TT>;
  2464. the
  2465. <TT>Layers</TT>
  2466. software
  2467. makes no such assumptions.
  2468. In
  2469. <TT>sam</TT>,
  2470. the window being changed is almost always fully visible, because the current
  2471. window is always fully visible, by construction.
  2472. However, when multi-file changes are being made, or when
  2473. more than one window is open on a file,
  2474. it may be necessary to update partially obscured windows.
  2475. </P>
  2476. <P>
  2477. There are three cases: the window is
  2478. fully visible, invisible (fully obscured), or partially visible.
  2479. If fully visible, the
  2480. <TT>Bitmap</TT>
  2481. is part of the screen, so when the
  2482. <TT>Flayer</TT>
  2483. update routine calls the
  2484. <TT>Frame</TT>
  2485. update routine, the screen will be updated directly.
  2486. If the window is invisible,
  2487. there is no associated
  2488. <TT>Bitmap</TT>,
  2489. and all that is necessary is to update the
  2490. <TT>Frame</TT>
  2491. data structure, not the image.
  2492. If the window is partially visible, the
  2493. <TT>Frame</TT>
  2494. routine is called to update the image in the off-screen
  2495. <TT>Bitmap</TT>,
  2496. which may require regenerating it from the text of the window.
  2497. The
  2498. <TT>Flayer</TT>
  2499. code then clips this
  2500. <TT>Bitmap</TT>
  2501. against the
  2502. <TT>Bitmaps</TT>
  2503. of all
  2504. <TT>Frames</TT>
  2505. in front of the
  2506. <TT>Frame</TT>
  2507. being modified, and the remainder is copied to the display.
  2508. </P>
  2509. <P>
  2510. This is much faster than recreating the image off-screen
  2511. for every change, or clipping all the changes made to the image
  2512. during its update.
  2513. Unfortunately, these caches can also consume prohibitive amounts of
  2514. memory, so they are freed fairly liberally &#173; after every change to the
  2515. front-to-back order of the
  2516. <TT>Flayers</TT>.
  2517. The result is that
  2518. the off-screen
  2519. <TT>Bitmaps</TT>
  2520. exist only while multi-window changes are occurring,
  2521. which is the only time the performance improvement they provide is needed.
  2522. Also, the user interface causes fully-obscured windows to be the
  2523. easiest to make &#173;
  2524. creating a canonically sized and placed window requires only a button click
  2525. &#173; which reduces the need for caching still further.
  2526. </P>
  2527. <P>
  2528. </P>
  2529. <H4>Screen update
  2530. </H4>
  2531. <br>&#32;<br>
  2532. Only two low-level primitives are needed for incremental update:
  2533. <TT>bitblt</TT>,
  2534. which copies rectangles of pixels, and
  2535. <TT>string</TT>
  2536. (which in turn calls
  2537. <TT>bitblt</TT>),
  2538. which draws a null-terminated character string in a
  2539. <TT>Bitmap</TT>.
  2540. A
  2541. <TT>Frame</TT>
  2542. contains a list of
  2543. <TT>Boxes</TT>,
  2544. each of which defines a horizontal strip of text in the window
  2545. (see Figure 7).
  2546. A
  2547. <TT>Box</TT>
  2548. has a character string
  2549. <TT>str</TT>,
  2550. and a
  2551. <TT>Rectangle</TT>
  2552. <TT>rect</TT>
  2553. that defines the location of the strip in the window.
  2554. (The text in
  2555. <TT>str</TT>
  2556. is stored in the
  2557. <TT>Box</TT>
  2558. separately from the
  2559. <TT>Rasp</TT>
  2560. associated with the window's file, so
  2561. <TT>Boxes</TT>
  2562. are self-contained.)
  2563. The invariant is that
  2564. the image of the
  2565. <TT>Box</TT>
  2566. can be reproduced by calling
  2567. <TT>string</TT>
  2568. with argument
  2569. <TT>str</TT>
  2570. to draw the string in
  2571. <TT>rect</TT>,
  2572. and the resulting picture fits perfectly within
  2573. <TT>rect</TT>.
  2574. In other words, the
  2575. <TT>Boxes</TT>
  2576. define the tiling of the window.
  2577. The tiling may be complicated by long lines of text, which
  2578. are folded onto the next line.
  2579. Some editors use horizontal scrolling to avoid this complication,
  2580. but to be comfortable this technique requires that lines not be
  2581. <I>too</I>
  2582. long;
  2583. <TT>sam</TT>
  2584. has no such restriction.
  2585. Also, and perhaps more importantly, UNIX programs and terminals traditionally fold
  2586. long lines to make their contents fully visible.
  2587. <P>
  2588. Two special kinds of
  2589. <TT>Boxes</TT>
  2590. contain a single
  2591. character: either a newline or a tab.
  2592. Newlines and tabs are white space.
  2593. A newline
  2594. <TT>Box</TT>
  2595. always extends to the right edge of the window,
  2596. forcing the following
  2597. <TT>Box</TT>
  2598. to the next line.
  2599. The width of a tab depends on where it is located:
  2600. it forces the next
  2601. <TT>Box</TT>
  2602. to begin at a tab location.
  2603. Tabs also
  2604. have a minimum width equivalent to a blank (blanks are
  2605. drawn by
  2606. <TT>string</TT>
  2607. and are not treated specially); newlines have a minimum width of zero.
  2608. <DL><DT><DD><TT><PRE>
  2609. <br><img src="-.11768.gif"><br>
  2610. <br>&#32;<br>
  2611. <br>
  2612. </PRE></TT></DL>
  2613. <I>Figure 7. A line of text showing its
  2614. </I><TT>Boxes</TT><I>.
  2615. The first two blank
  2616. </I><TT>Boxes</TT><I>
  2617. contain tabs; the last contains a newline.
  2618. Spaces are handled as ordinary characters.
  2619. <br>
  2620. <DL><DT><DD><TT><PRE>
  2621. </I><br>&#32;<br>
  2622. </PRE></TT></DL>
  2623. </P>
  2624. <P>
  2625. The update algorithms always use the
  2626. <TT>Bitmap</TT>
  2627. image of the text (either the display or cache
  2628. <TT>Bitmap</TT>);
  2629. they never examine the characters within a
  2630. <TT>Box</TT>
  2631. except when the
  2632. <TT>Box</TT>
  2633. needs to be split in two.
  2634. Before a change, the window consists of a tiling of
  2635. <TT>Boxes</TT>;
  2636. after the change the window is tiled differently.
  2637. The update algorithms rearrange the tiles in place, without
  2638. backup storage.
  2639. The algorithms are not strictly optimal &#173; for example, they can
  2640. clear a pixel that is later going to be written upon &#173;
  2641. but they never move a tile that doesn't need to be moved,
  2642. and they move each tile at most once.
  2643. <TT>Frinsert</TT>
  2644. on a Blit can absorb over a thousand characters a second if the strings
  2645. being inserted are a few tens of characters long.
  2646. </P>
  2647. <P>
  2648. Consider
  2649. <TT>frdelete</TT>.
  2650. Its job is to delete a substring from a
  2651. <TT>Frame</TT>
  2652. and restore the image of the
  2653. <TT>Frame</TT>.
  2654. The image of a substring has a peculiar shape (see Figure 2) comprising
  2655. possibly a partial line,
  2656. zero or more full lines,
  2657. and possibly a final partial line.
  2658. For reference, call this the
  2659. Z-shape.
  2660. <TT>Frdelete</TT>
  2661. begins by splitting, if necessary, the
  2662. <TT>Boxes</TT>
  2663. containing the ends of
  2664. the substring so the substring begins and ends on
  2665. <TT>Box</TT>
  2666. boundaries.
  2667. Because the substring is being deleted, its image is not needed,
  2668. so the Z-shape is then cleared.
  2669. Then, tiles (that is, the images of
  2670. <TT>Boxes</TT>)
  2671. are copied, using
  2672. <TT>bitblt</TT>,
  2673. from immediately after the Z-shape to
  2674. the beginning of the Z-shape,
  2675. resulting in a new Z-shape.
  2676. (<TT>Boxes</TT>
  2677. whose contents would span two lines in the new position must first be split.)
  2678. </P>
  2679. <P>
  2680. Copying the remainder of the
  2681. <TT>Frame</TT>
  2682. tile by tile
  2683. this way will clearly accomplish the deletion but eventually,
  2684. typically when the copying algorithm encounters a tab or newline,
  2685. the old and new
  2686. <TT>x</TT>
  2687. coordinates of the tile
  2688. to be copied are the same.
  2689. This correspondence implies
  2690. that the Z-shape has its beginning and ending edges aligned
  2691. vertically, and a sequence of at most two
  2692. <TT>bitblts</TT>
  2693. can be used to copy the remaining tiles.
  2694. The last step is to clear out the resulting empty space at the bottom
  2695. of the window;
  2696. the number of lines to be cleared is the number of complete lines in the
  2697. Z-shape closed by the final
  2698. <TT>bitblts.</TT>
  2699. The final step is to merge horizontally adjacent
  2700. <TT>Boxes</TT>
  2701. of plain text.
  2702. The complete source to
  2703. <TT>frdelete</TT>
  2704. is less than 100 lines of C.
  2705. </P>
  2706. <P>
  2707. <TT>frinsert</TT>
  2708. is more complicated because it must do four passes:
  2709. one to construct the
  2710. <TT>Box</TT>
  2711. list for the inserted string,
  2712. one to reconnoitre,
  2713. one to copy (in opposite order to
  2714. <TT>frdelete</TT>)
  2715. the
  2716. <TT>Boxes</TT>
  2717. to make the hole for the new text,
  2718. and finally one to copy the new text into place.
  2719. Overall, though,
  2720. <TT>frinsert</TT>
  2721. has a similar flavor to
  2722. <TT>frdelete</TT>,
  2723. and needn't be described further.
  2724. <TT>Frinsert</TT>
  2725. and its subsidiary routines comprise 211 lines of C.
  2726. </P>
  2727. <P>
  2728. The terminal source code is 3024 lines of C,
  2729. and the host source is 5797 lines.
  2730. </P>
  2731. <H4>Discussion
  2732. </H4>
  2733. <H4>History
  2734. </H4>
  2735. <br>&#32;<br>
  2736. The immediate ancestor of
  2737. <TT>sam</TT>
  2738. was the original text editor for the Blit, called
  2739. <TT>jim</TT>.
  2740. <TT>Sam</TT>
  2741. inherited
  2742. <TT>jim</TT>'s
  2743. two-process structure and mouse language almost unchanged, but
  2744. <TT>jim</TT>
  2745. suffered from several drawbacks that were addressed in the design of
  2746. <TT>sam</TT>.
  2747. The most important of these was the lack of a command language.
  2748. Although
  2749. <TT>jim</TT>
  2750. was easy to use for simple editing, it provided no direct help with
  2751. large or repetitive editing tasks. Instead, it provided a command to pass
  2752. selected text through a shell pipeline,
  2753. but this was no more satisfactory than could be expected of a stopgap measure.
  2754. <P>
  2755. <TT>Jim</TT>
  2756. was written primarily as a vehicle for experimenting with a mouse-based
  2757. interface to text, and the experiment was successful.
  2758. <TT>Jim</TT>
  2759. had some spin-offs:
  2760. <TT>mux</TT>,
  2761. the second window system for the Blit, is essentially a multiplexed
  2762. version of the terminal part of
  2763. <TT>jim</TT>;
  2764. and the debugger
  2765. <TT>pi</TT>'s
  2766. user interface<sup>20</sup> was closely modeled on
  2767. <TT>jim</TT>'s.
  2768. But after a couple of years,
  2769. <TT>jim</TT>
  2770. had become difficult to maintain and limiting to use,
  2771. and its replacement was overdue.
  2772. </P>
  2773. <P>
  2774. I began the design of
  2775. <TT>sam</TT>
  2776. by asking
  2777. <TT>jim</TT>
  2778. customers what they wanted.
  2779. This was probably a mistake; the answers were essentially a list of features
  2780. to be found in other editors, which did not provide any of the
  2781. guiding principles I was seeking.
  2782. For instance, one common request was for a ``global substitute,''
  2783. but no one suggested how to provide it within a cut-and-paste editor.
  2784. I was looking for a scheme that would
  2785. support such specialized features comfortably in the context of some
  2786. general command language.
  2787. Ideas were not forthcoming, though, particularly given my insistence
  2788. on removing all limits on file sizes, line lengths and so on.
  2789. Even worse, I recognized that, since the mouse could easily
  2790. indicate a region of the screen that was not an integral number of lines,
  2791. the command language would best forget about newlines altogether,
  2792. and that meant the command language had to treat the file as a single
  2793. string, not an array of lines.
  2794. </P>
  2795. <P>
  2796. Eventually, I decided that thinking was not getting me very far and it was
  2797. time to try building.
  2798. I knew that the terminal part could be built easily &#173;
  2799. that part of
  2800. <TT>jim</TT>
  2801. behaved acceptably well &#173; and that most of the hard work was going
  2802. to be in the host part: the file interface, command interpreter and so on.
  2803. Moreover, I had some ideas about how the architecture of
  2804. <TT>jim</TT>
  2805. could be improved without destroying its basic structure, which I liked
  2806. in principle but which hadn't worked out as well as I had hoped.
  2807. So I began by designing the file data structure,
  2808. starting with the way
  2809. <TT>jim</TT>
  2810. worked &#173; comparable to a single structure merging
  2811. <TT>Disc</TT>
  2812. and
  2813. <TT>Buffer</TT>,
  2814. which I split to make the cache more general
  2815. &#173; and thinking about how global substitute could be implemented.
  2816. The answer was clearly that it had to be done in two passes,
  2817. and the transcript-oriented implementation fell out naturally.
  2818. </P>
  2819. <P>
  2820. <TT>Sam</TT>
  2821. was written bottom-up,
  2822. starting from the data structures and algorithms for manipulating text,
  2823. through the command language and up to the code for maintaining
  2824. the display.
  2825. In retrospect, it turned out well, but this implementation method is
  2826. not recommended in general.
  2827. There were several times when I had a large body of interesting code
  2828. assembled and no clue how to proceed with it.
  2829. The command language, in particular, took almost a year to figure out,
  2830. but can be implemented (given what was there at the beginning of that year)
  2831. in a day or two. Similarly, inventing the
  2832. <TT>Rasp</TT>
  2833. data structure delayed the
  2834. connection of the host and terminal pieces by another few months.
  2835. <TT>Sam</TT>
  2836. took about two years to write, although only about four months were
  2837. spent actually working on it.
  2838. </P>
  2839. <P>
  2840. Part of the design process was unusual:
  2841. the subset of the protocol that maintains the
  2842. <TT>Rasp</TT>
  2843. was simulated, debugged
  2844. and verified by an automatic protocol analyzer,<sup>21</sup> and was bug-free
  2845. from the start.
  2846. The rest of the protocol, concerned mostly
  2847. with keeping menus up to date,
  2848. was unfortunately too unwieldy for such analysis,
  2849. and was debugged by more traditional methods, primarily
  2850. by logging in a file all messages in and out of the host.
  2851. </P>
  2852. <H4>Reflections
  2853. </H4>
  2854. <br>&#32;<br>
  2855. <TT>Sam</TT>
  2856. is essentially the only interactive editor used by the sixty or so members of
  2857. the computing science research center in which I work.
  2858. The same could not be said of
  2859. <TT>jim</TT>;
  2860. the lack of a command language kept some people from adopting it.
  2861. The union of a user interface as comfortable as
  2862. <TT>jim</TT>'s
  2863. with a command language as powerful as
  2864. <TT>ed</TT>'s&#191;
  2865. <DL>
  2866. <DT><DT>&#32;<DD>
  2867. NOTE:<I> &#191;The people who criticize
  2868. <TT>ed</TT>
  2869. as an interactive program often forget that it and its close relative
  2870. <TT>sed</TT><sup>7</sup>
  2871. still thrive as programmable editors. The strength of these programs is
  2872. independent of their convenience for interactive editing.
  2873. <br>
  2874. </I><DT>&#32;<DD></dl>
  2875. <br>
  2876. is essential to
  2877. <TT>sam</TT>'s
  2878. success.
  2879. When
  2880. <TT>sam</TT>
  2881. was first made available to the
  2882. <TT>jim</TT>
  2883. community,
  2884. almost everyone switched to it within two or three days.
  2885. In the months that followed, even people who had never adopted
  2886. <TT>jim</TT>
  2887. started using
  2888. <TT>sam</TT>
  2889. exclusively.
  2890. <P>
  2891. To be honest,
  2892. <TT>ed</TT>
  2893. still gets occasional use, but usually when
  2894. something quick needs to be done and the overhead of
  2895. downloading the terminal part of
  2896. <TT>sam</TT>
  2897. isn't worth the trouble.
  2898. Also, as a `line' editor,
  2899. <TT>sam</TT>
  2900. <TT>-d</TT>
  2901. is a bit odd;
  2902. when using a good old ASCII terminal, it's comforting to have
  2903. a true line editor.
  2904. But it is fair to say that
  2905. <TT>sam</TT>'s
  2906. command language has displaced
  2907. <TT>ed</TT>'s
  2908. for most of the complicated editing that has kept line editors
  2909. (that is, command-driven editors) with us.
  2910. </P>
  2911. <P>
  2912. <TT>Sam</TT>'s
  2913. command language is even fancier than
  2914. <TT>ed</TT>'s,
  2915. and most
  2916. <TT>sam</TT>
  2917. customers don't come near to using all its capabilities.
  2918. Does it need to be so sophisticated?
  2919. I think the answer is yes, for two reasons.
  2920. </P>
  2921. <P>
  2922. First, the
  2923. <I>model</I>
  2924. for
  2925. <TT>sam</TT>'s
  2926. command language is really relatively simple, and certainly simpler than that of
  2927. <TT>ed</TT>.
  2928. For instance, there is only one kind of textual loop in
  2929. <TT>sam</TT>
  2930. &#173; the
  2931. <TT>x</TT>
  2932. command &#173;
  2933. while
  2934. <TT>ed</TT>
  2935. has three (the
  2936. <TT>g</TT>
  2937. command, the global flag on substitutions, and the implicit loop over
  2938. lines in multi-line substitutions).
  2939. Also,
  2940. <TT>ed</TT>'s
  2941. substitute command is necessary to make changes within lines, but in
  2942. <TT>sam</TT>
  2943. the
  2944. <TT>s</TT>
  2945. command is more of a familiar convenience than a necessity;
  2946. <TT>c</TT>
  2947. and
  2948. <TT>t</TT>
  2949. can do all the work.
  2950. </P>
  2951. <P>
  2952. Second,
  2953. given a community that expects an editor to be about as powerful as
  2954. <TT>ed</TT>,
  2955. it's hard to see how
  2956. <TT>sam</TT>
  2957. could really be much simpler and still satisfy that expectation.
  2958. People want to do ``global substitutes,'' and most are content
  2959. to have the recipe for that and a few other fancy changes.
  2960. The sophistication of the command language is really just a veneer
  2961. over a design that makes it possible to do global substitutes
  2962. in a screen editor.
  2963. Some people will always want something more, however, and it's gratifying to
  2964. be able to provide it.
  2965. The real power of
  2966. <TT>sam</TT>'s
  2967. command language comes from composability of the operators, which is by
  2968. nature orthogonal to the underlying model.
  2969. In other words,
  2970. <TT>sam</TT>
  2971. is not itself complex, but it makes complex things possible.
  2972. If you don't want to do anything complex, you can ignore the
  2973. complexity altogether, and many people do so.
  2974. </P>
  2975. <P>
  2976. Sometimes I am asked the opposite question: why didn't I just make
  2977. <TT>sam</TT>
  2978. a real programmable editor, with macros and variables and so on?
  2979. The main reason is a matter of taste: I like the editor
  2980. to be the same every time I use it.
  2981. There is one technical reason, though:
  2982. programmability in editors is largely a workaround for insufficient
  2983. interactivity.
  2984. Programmable editors are used to make particular, usually short-term,
  2985. things easy to do, such as by providing shorthands for common actions.
  2986. If things are generally easy to do in the first place,
  2987. shorthands are not as helpful.
  2988. <TT>Sam</TT>
  2989. makes common editing operations very easy, and the solutions to
  2990. complex editing problems seem commensurate with the problems themselves.
  2991. Also, the ability to edit the
  2992. <TT>sam</TT>
  2993. window makes it easy to repeat commands &#173; it only takes a mouse button click
  2994. to execute a command again.
  2995. </P>
  2996. <H4>Pros and cons
  2997. </H4>
  2998. <br>&#32;<br>
  2999. <TT>Sam</TT>
  3000. has several other good points,
  3001. and its share of problems.
  3002. Among the good things is the idea of
  3003. structural regular expressions,
  3004. whose usefulness has only begun to be explored.
  3005. They were arrived at serendipitously when I attempted to distill the essence of
  3006. <TT>ed</TT>'s
  3007. way of doing global substitution and recognized that the looping command in
  3008. <TT>ed</TT>
  3009. was implicitly imposing a structure (an array of lines) on the file.
  3010. <P>
  3011. Another of
  3012. <TT>sam</TT>'s
  3013. good things is its undo capability.
  3014. I had never before used an editor with a true undo,
  3015. but I would never go back now.
  3016. Undo
  3017. <I>must</I>
  3018. be done well, but if it is, it can be relied on.
  3019. For example,
  3020. it's safe to experiment if you're not sure how to write some intricate command,
  3021. because if you make a mistake, it can be fixed simply and reliably.
  3022. I learned two things about undo from writing
  3023. <TT>sam</TT>:
  3024. first, it's easy to provide if you design it in from the beginning, and
  3025. second, it's necessary, particularly if the system has some subtle
  3026. properties that may be unfamiliar or error-prone for users.
  3027. </P>
  3028. <P>
  3029. <TT>Sam</TT>'s
  3030. lack of internal limits and sizes is a virtue.
  3031. Because it avoids all fixed-size tables and data structures,
  3032. <TT>sam</TT>
  3033. is able to make global changes to files that some of our other
  3034. tools cannot even read.
  3035. Moreover, the design keeps the performance linear when doing such
  3036. operations, although I must admit
  3037. <TT>sam</TT>
  3038. does get slow when editing a huge file.
  3039. </P>
  3040. <P>
  3041. Now, the problems.
  3042. Externally, the most obvious is that it is poorly integrated into the
  3043. surrounding window system.
  3044. By design, the user interface in
  3045. <TT>sam</TT>
  3046. feels almost identical to that of
  3047. <TT>mux</TT>,
  3048. but a thick wall separates text in
  3049. <TT>sam</TT>
  3050. from the programs running in
  3051. <TT>mux</TT>.
  3052. For instance, the `snarf buffer' in
  3053. <TT>sam</TT>
  3054. must be maintained separately from that in
  3055. <TT>mux</TT>.
  3056. This is regrettable, but probably necessary given the unusual configuration
  3057. of the system, with a programmable terminal on the far end of an RS-232 link.
  3058. </P>
  3059. <P>
  3060. <TT>Sam</TT>
  3061. is reliable; otherwise, people wouldn't use it.
  3062. But it was written over such a long time, and has so many new (to me)
  3063. ideas in it, that I would like to see it done over again to clean
  3064. up the code and remove many of the lingering problems in the implementation.
  3065. The worst part is in the interconnection of the host and terminal parts,
  3066. which might even be able to go away in a redesign for a more
  3067. conventional window system.
  3068. The program must be split in two to use the terminal effectively,
  3069. but the low bandwidth of the connection forces the separation to
  3070. occur in an inconvenient part of the design if performance is to be acceptable.
  3071. A simple remote procedure call
  3072. protocol driven by the host, emitting only graphics
  3073. commands, would be easy to write but wouldn't have nearly the
  3074. necessary responsiveness. On the other hand, if the terminal were in control
  3075. and requested much simpler file services from the host, regular expression
  3076. searches would require that the terminal read the entire file over its RS-232
  3077. link, which would be unreasonably slow.
  3078. A compromise in which either end can take control is necessary.
  3079. In retrospect, the communications protocol should have been
  3080. designed and verified formally, although I do not know of any tool
  3081. that can adequately relate the protocol to
  3082. its implementation.
  3083. </P>
  3084. <P>
  3085. Not all of
  3086. <TT>sam</TT>'s
  3087. users are comfortable with its command language, and few are adept.
  3088. Some (venerable) people use a sort of
  3089. <TT>ed</TT>
  3090. ``
  3091. subset'' of
  3092. <TT>sam</TT>'s
  3093. command language,
  3094. and even ask why
  3095. <TT>sam</TT>'s
  3096. command language is not exactly
  3097. <TT>ed</TT>'s.
  3098. (The reason, of course, is that
  3099. <TT>sam</TT>'s
  3100. model for text does not include newlines, which are central to
  3101. <TT>ed</TT>.
  3102. Making the text an array of newlines to the command language would
  3103. be too much of a break from the seamless model provided by the mouse.
  3104. Some editors, such as
  3105. <TT>vi</TT>,
  3106. are willing to make this break, though.)
  3107. The difficulty is that
  3108. <TT>sam</TT>'s
  3109. syntax is so close to
  3110. <TT>ed</TT>'s
  3111. that people believe it
  3112. <I>should</I>
  3113. be the same.
  3114. I thought, with some justification in hindsight,
  3115. that making
  3116. <TT>sam</TT>
  3117. similar to
  3118. <TT>ed</TT>
  3119. would make it easier to learn and to accept.
  3120. But I may have overstepped and raised the users'
  3121. expectations too much.
  3122. It's hard to decide which way to resolve this problem.
  3123. </P>
  3124. <P>
  3125. Finally, there is a tradeoff in
  3126. <TT>sam</TT>
  3127. that was decided by the environment in which it runs:
  3128. <TT>sam</TT>
  3129. is a multi-file editor, although in a different system there might instead be
  3130. multiple single-file editors.
  3131. The decision was made primarily because starting a new program in a Blit is
  3132. time-consuming.
  3133. If the choice could be made freely, however, I would
  3134. still choose the multi-file architecture, because it allows
  3135. groups of files to be handled as a unit;
  3136. the usefulness of the multi-file commands is incontrovertible.
  3137. It is delightful to have the source to an entire program
  3138. available at your fingertips.
  3139. </P>
  3140. <H4>Acknowledgements
  3141. </H4>
  3142. <br>&#32;<br>
  3143. Tom Cargill suggested the idea behind the
  3144. <TT>Rasp</TT>
  3145. data structure.
  3146. Norman Wilson and Ken Thompson influenced the command language.
  3147. This paper was improved by comments from
  3148. Al Aho,
  3149. Jon Bentley,
  3150. Chris Fraser,
  3151. Gerard Holzmann,
  3152. Brian Kernighan,
  3153. Ted Kowalski,
  3154. Doug McIlroy
  3155. and
  3156. Dennis Ritchie.
  3157. <H4>REFERENCES
  3158. </H4>
  3159. <P>
  3160. </P>
  3161. <DL COMPACT>
  3162. <DT> 1.<DD>
  3163. R. Pike,
  3164. `The Blit: a multiplexed graphics terminal,'
  3165. AT&amp;T Bell Labs. Tech. J.,
  3166. <B>63</B>,
  3167. (8),
  3168. 1607-1631 (1984).
  3169. <DT> 2.<DD>
  3170. L. Johnson,
  3171. <I>MacWrite,</I>
  3172. Apple Computer Inc., Cupertino, Calif. 1983.
  3173. <DT> 3.<DD>
  3174. B. Lampson,
  3175. `Bravo Manual,'
  3176. in
  3177. Alto User's Handbook,
  3178. pp. 31-62,
  3179. Xerox Palo Alto Research Center,
  3180. Palo Alto, Calif.
  3181. 1979.
  3182. <DT> 4.<DD>
  3183. W. Teitelman,
  3184. `A tour through Cedar,'
  3185. IEEE Software,
  3186. <B>1</B>
  3187. (2), 44-73 (1984).
  3188. <DT> 5.<DD>
  3189. J. Gutknecht,
  3190. `Concepts of the text editor Lara,'
  3191. Comm. ACM,
  3192. <B>28</B>,
  3193. (9),
  3194. 942-960 (1985).
  3195. <DT> 6.<DD>
  3196. Bell Telephone Laboratories,
  3197. UNIX Programmer's Manual,
  3198. Holt, Rinehart and Winston, New York 1983.
  3199. <DT> 7.<DD>
  3200. B. W. Kernighan and R. Pike,
  3201. The Unix Programming Environment,
  3202. Prentice-Hall, Englewood Cliffs, New Jersey 1984.
  3203. <DT> 8.<DD>
  3204. Unix Time-Sharing System Programmer's Manual, Research Version, Ninth Edition,
  3205. Volume 1,
  3206. AT&amp;T Bell Laboratories, Murray Hill, New Jersey 1986.
  3207. <DT> 9.<DD>
  3208. Unix Time-Sharing System Programmer's Manual, 4.1 Berkeley Software Distribution,
  3209. Volumes 1 and 2C,
  3210. University of California, Berkeley, Calif. 1981.
  3211. <DT>10.<DD>
  3212. R. Pike,
  3213. `Structural Regular Expressions,'
  3214. Proc. EUUG Spring Conf., Helsinki 1987,
  3215. Eur. Unix User's Group, Buntingford, Herts, UK 1987.
  3216. <DT>11.<DD>
  3217. A. Goldberg,
  3218. Smalltalk-80 &#191; The Interactive Programming Environment,
  3219. Addison-Wesley, Reading, Mass. 1984.
  3220. <DT>12.<DD>
  3221. K. Thompson,
  3222. `Regular expression search algorithm,'
  3223. Comm. ACM,
  3224. <B>11</B>,
  3225. (6),
  3226. 419-422 (1968).
  3227. <DT>13.<DD>
  3228. A. V. Aho, J. E. Hopcroft and J. D. Ullman,
  3229. The Design and Analysis of Computer Algorithms,
  3230. Addison-Wesley, Reading, Mass. 1974.
  3231. <DT>14.<DD>
  3232. B. W. Kernighan and D. M. Ritchie,
  3233. The C Programming Language,
  3234. Prentice-Hall, Englewood Cliffs, New Jersey 1978.
  3235. <DT>15.<DD>
  3236. W. M. Waite,
  3237. `The cost of lexical analysis,'
  3238. Softw. Pract. Exp.,
  3239. <B>16</B>,
  3240. (5),
  3241. 473-488 (1986).
  3242. <DT>16.<DD>
  3243. C. W. Fraser,
  3244. `A generalized text editor,'
  3245. Comm. ACM,
  3246. <B>23</B>,
  3247. (3),
  3248. 154-158 (1980).
  3249. <DT>17.<DD>
  3250. R. Pike,
  3251. `Graphics in overlapping bitmap layers,'
  3252. ACM Trans. on Graph.,
  3253. <B>2</B>,
  3254. (2)
  3255. 135-160 (1983).
  3256. <DT>18.<DD>
  3257. L. J. Guibas and J. Stolfi,
  3258. `A language for bitmap manipulation,'
  3259. ACM Trans. on Graph.,
  3260. <B>1</B>,
  3261. (3),
  3262. 191-214 (1982).
  3263. <DT>19.<DD>
  3264. R. Pike, B. Locanthi and J. Reiser,
  3265. `Hardware/software trade-offs for bitmap graphics on the Blit,'
  3266. Softw. Pract. Exp.,
  3267. <B>15</B>,
  3268. (2),
  3269. 131-151 (1985).
  3270. <DT>20.<DD>
  3271. T. A. Cargill,
  3272. `The feel of Pi,'
  3273. Winter USENIX Conference Proceedings,
  3274. Denver 1986,
  3275. 62-71,
  3276. USENIX Assoc., El Cerrito, CA.
  3277. <DT>21.<DD>
  3278. G. J. Holzmann,
  3279. `Tracing protocols,'
  3280. AT&amp;T Tech. J.,
  3281. <B>64</B>,
  3282. (10),
  3283. 2413-2434 (1985).
  3284. </dl>
  3285. <br>&#32;<br>
  3286. <A href=http://www.lucent.com/copyright.html>
  3287. Copyright</A> &#169; 2000 Lucent Technologies Inc. All rights reserved.
  3288. </body></html>