sam.ms 92 KB

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