sam.html 246 KB

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