3241 lines · 92.4 KB · Text
RawDownload
.HTML "The Text Editor sam.Vx 17 11 November 87 1 32 "ROB PIKE" "THE TEXT EDITOR SAM".ds DY "31 May 1987.ds DR "Revised 1 July 1987.de CW		\" puts first arg in CW font, same as UL; maintains font\%\&\\$3\f(CW\\$1\fP\&\\$2...de Cs.br.fi.ft 2.ps -2.vs -2...de Ce.br.nf.ft 1.ps.vs.sp...de XP.ie h .html - <center><img src="\\$1.gif" /></center>.el .BP \\$1.ps \\$2...TLThe Text Editor \&\f(CWsam\fP.AURob Pikerob@plan9.bell-labs.com.AB.LP.CW Samis an interactive multi-file text editor intended forbitmap displays.A textual command languagesupplements the mouse-driven, cut-and-paste interfaceto make complex orrepetitive editing tasks easy to specify.The language is characterized by the composition of regular expressionsto describe the structure of the text being modified.The treatment of files as a database, with changes loggedas atomic transactions, guides the implementation andmakes a general `undo' mechanism straightforward..PP.CW Samis implemented as two processes connected by a low-bandwidth stream,one process handling the display and the other the editingalgorithms.  Therefore it can run with the display processin a bitmap terminal and the editor on a local host,with both processes on a bitmap-equipped host, or withthe display process in the terminal and the editor in aremote host.By suppressing the display process,it can even run without a bitmap terminal..PPThis paper is reprinted from Software\(emPractice and Experience,Vol 17, number 11, pp. 813-845, November 1987.The paper has not been updated for the Plan 9 manuals.  Although.CW Samhas not changed much since the paper was written, the system around it certainly has.Nonetheless, the description here still stands as the best introduction to the editor..AE.SHIntroduction.LP.CW Samis an interactive text editor that combines cut-and-paste interactive editing withan unusual command language based on the composition of regular expressions.It is written as two programs: one, the `host part,' runs on a UNIX systemand implements the command language and provides file access; the other, the`terminal part,' runs asynchronouslyon a machine with a mouse and bitmap displayand supports the display and interactive editing.The host part may be even run in isolation on an ordinary terminalto edit text using the commandlanguage, much like a traditional line editor,without assistance from a mouse or display.Most often,the terminal part runs on a Blit\u\s-4\&1\s+4\d terminal(actually on a Teletype DMD 5620, the production version of the Blit), whosehost connection is an ordinary 9600 bps RS232 link;on the SUN computer the host and display processes run on a single machine,connected by a pipe..PP.CW Samedits uninterpretedASCII text.It has no facilities for multiple fonts, graphics or tables,unlike MacWrite,\u\s-4\&2\s+4\d Bravo,\u\s-4\&3\s+4\d Tioga\u\s-4\&4\s+4\dor Lara.\u\s-4\&5\s+4\dAlso unlike them, it has a rich command language.(Throughout this paper, the phrase.Icommand language.Rrefers totextual commands; commands activated from the mouse form the.I mouse.I language. ).CW Samdeveloped as an editor for use by programmers, and tries to jointhe styles of the UNIX text editor.CW ed \u\s-4\&6,7\s+4\dwith that of interactive cut-and-paste editors byproviding a comfortable mouse-driven interfaceto a program with a solid command language driven by regular expressions.The command language developed more than the mouse language, andacquired a notation for describing the structure of filesmore richly than as a sequence of lines,using a dataflow-like syntax for specifying changes..PPThe interactive style was influenced by.CW jim ,\u\s-4\&1\s+4\dan early cut-and-paste editor for the Blit, and by.CW mux ,\u\s-4\&8\s+4\dthe Blit window system..CW Muxmerges the original Blit window system,.CW mpx ,\u\s-4\&1\s+4\dwith cut-and-paste editing, forming something like amultiplexed version of.CW jimthat edits the output of (and input to) command sessions rather than files..PPThe first part of this paper describes the command language, then the mouselanguage, and explains how they interact.That is followed by a description of the implementation,first of the host part, then of the terminal part.A principle that influenced the design of.CW samis that it should have no explicit limits, such as upper limits onfile size or line length.A secondary consideration is that it be efficient.To honor these two goals together requires a method for efficientlymanipulatinghuge strings (files) without breaking them into lines,perhaps while making thousands of changesunder control of the command language..CW Sam 'smethod is totreat the file as a transaction database, implementing changes as atomicupdates.  These updates may be unwound easily to `undo' changes.Efficiency is achieved through a collection of caches that minimizesdisc traffic and data motion, both within the two parts of the programand between them..PPThe terminal part of.CW samis fairly straightforward.More interesting is how the two halves of the editor staysynchronized when either half may initiate a change.This is achieved through a data structure that organizes thecommunications and is maintained in parallel by both halves..PPThe last part of the paper chronicles the writing of.CW samand discusses the lessons that were learned through its development and use..PPThe paper is long, but is composed largely of two papers of reasonable length:a description of the user interface of.CW samand a discussion of its implementation.They are combined because the implementation is strongly influenced bythe user interface, and vice versa..SHThe Interface.LP.CW Samis a text editor for multiple files.File names may be provided when it is invoked:.P1sam file1 file2 ....P2and there are commandsto add new files and discard unneeded ones.Files are not read until necessaryto complete some command.Editing operations apply to an internal copymade when the file is read; the UNIX file associated with the copyis changed only by an explicit command.To simplify the discussion, the internal copy is here called a.I file ,while the disc-resident original is called a.Idisc file..R.PP.CW Samis usually connected to a bitmap display that presents a cut-and-pasteeditor driven by the mouse.In this mode, the command language is still available:text typed in a special window, called the.CW sam.I window,is interpretedas commands to be executed in the current file.Cut-and-paste editing may be used in any window \(em even in the.CW samwindow to construct commands.The other mode of operation, invoked by starting.CW samwith the option.CW -d(for `no download'),does not use the mouse or bitmap display, but still permitsediting using the textual command language, even on an ordinary terminal,interactively or from a script..PPThe following sections describe first the command language (under.CW sam\ -dand in the.CW samwindow), and then the mouse interface.These two languages are nearly independent, but connect through the.I current.I text,described below..SH 2The Command Language.LPA file consists of its contents, which are an array of characters(that is, a string); the.I nameof the associated disc file; the.Imodified bit.Rthat states whether the contents match those ofthe disc file;and a substring of the contents, called the.Icurrent text.Ror.I dot(see Figures 1 and 2).If the current text is a null string, dot falls between characters.The.I valueof dot is the location of the current text; the.I contentsof dot are the characters it contains..CW Samimparts to the text no two-dimensional interpretation such as columnsor fields; text is always one-dimensional.Even the idea of a `line' of text as understood by most UNIX programs\(em a sequence of characters terminated by a newline character \(emis only weakly supported..PPThe.Icurrent file.Ris the file to which editing commands refer.The current text is therefore dot in the current file.If a command doesn't explicitly name a particular file or piece of text,the command is assumed to apply to the current text.For the moment, ignore the presence of multiple files and considerediting a single file..KF L.XP fig1 3.5i.CsFigure 1. A typical.CW samscreen, with the editing menu presented.The.CW sam(command language) window is in the middle, with file windows above and below.(The user interface makes it easy to create these abutting windows.)The partially obscured window is a third file window.The uppermost window is that to which typing and mouse operations apply,as indicated by its heavy border.Each window has its current text highlighted in reverse video.The.CW samwindow's current text is the null string on the last visible line,indicated by a vertical bar.See also Figure 2..Ce.KE.PPCommands have one-letter names.Except for non-editing commands such as writingthe file to disc, most commands make some changeto the text in dot and leave dot set to the text resulting from the change.For example, the delete command,.CW d ,deletes the text in dot, replacing it by the null string and setting dotto the result.The change command,.CW c ,replaces dot by text delimited by an arbitrary punctuation character,conventionallya slash.  Thus,.P1c/Peter/.P2replaces the text in dot by the string.CW Peter .Similarly,.P1a/Peter/.P2(append) adds the string after dot, and.P1i/Peter/.P2(insert) inserts before dot.All three leave dot set to the new text,.CW Peter ..PPNewlines are part of the syntax of commands:the newline character lexically terminates a command.Within the inserted text, however, newlines are never implicit.But since it is often convenient to insert multiple lines of text,.CW samhas a specialsyntax for that case:.P1asome lines of textto be inserted in the file,terminated by a periodon a line by itself\&..P2In the one-line syntax, a newline character may be specified by a C-likeescape, so.P1c/\en/.P2replaces dot by a single newline character..PP.CW Samalso has a substitute command,.CW s :.P1s/\f2expression\fP/\f2replacement\fP/.P2substitutes the replacement text for the first match, in dot,of the regular expression.Thus, if dot is the string.CW Peter ,the command.P1s/t/st/.P2changes it to.CW Pester .In general,.CW sis unnecessary, but it was inherited from.CW edand it has some convenient variations.For instance, the replacement text may include the matched text,specified by.CW & :.P1s/Peter/Oh, &, &, &, &!/.P2.PPThere are also three commands that apply programsto text:.P1< \f2UNIX program\fP.P2replaces dot by the output of the UNIX program.Similarly, the.CW >commandruns the program with dot as its standard input, and.CW |does both.  For example,.P1| sort.P2replaces dot by the result of applying the standard sorting utility to it.Again, newlines have no special significance for these.CW samcommands.The text acted upon and resulting from these commands is not necessarilybounded by newlines, although for connection with UNIX programs,newlines may be necessary to obey conventions..PPOne more command:.CW pprints the contents of dot.Table I summarizes.CW sam 'scommands..KF.TScenter;c slfCW l.Table I. \f(CWSam\fP commands.sp .4.ft CW_.ft.sp .4\f1Text commands\fP	.sp .4_.sp .4a/\f2text\fP/	Append text after dotc/\f2text\fP/	Change text in doti/\f2text\fP/	Insert text before dotd	Delete text in dots/\f2regexp\fP/\f2text\fP/	Substitute text for match of regular expression in dotm \f2address\fP	Move text in dot after addresst \f2address\fP	Copy text in dot after address.sp .4_.sp .4\f1Display commands\fP	.sp .4_.sp .2p	Print contents of dot\&=	Print value (line numbers and character numbers) of dot.sp .4_.sp .4\f1File commands\fP.sp .4_.sp .2b \f2file-list\fP	Set current file to first file in list that \f(CWsam\fP has in menuB \f2file-list\fP	Same as \f(CWb\fP, but load new filesn	Print menu lines of all filesD \f2file-list\fP	Delete named files from \f(CWsam\fP.sp .4_.sp .4\f1I/O commands\fP	.sp .4_.sp .2e \f2filename\fP	Replace file with named disc filer \f2filename\fP	Replace dot by contents of named disc filew \f2filename\fP	Write file to named disc filef \f2filename\fP	Set file name and print new menu line< \f2UNIX-command\fP	Replace dot by standard output of command> \f2UNIX-command\fP	Send dot to standard input of command| \f2UNIX-command\fP	Replace dot by result of command applied to dot! \f2UNIX-command\fP	Run the command.sp .4_.sp .4\f1Loops and conditionals\fP	.sp .4_.sp .2x/\f2regexp\fP/ \f2command\fP	For each match of regexp, set dot and run commandy/\f2regexp\fP/ \f2command\fP	Between adjacent matches of regexp, set dot and run commandX/\f2regexp\fP/ \f2command\fP	Run command in each file whose menu line matches regexpY/\f2regexp\fP/ \f2command\fP	Run command in each file whose menu line does not matchg/\f2regexp\fP/ \f2command\fP	If dot contains a match of regexp, run commandv/\f2regexp\fP/ \f2command\fP	If dot does not contain a match of regexp, run command.sp .4_.sp .4\f1Miscellany\fP	.sp .4_.sp .2k	Set address mark to value of dotq	Quitu \f2n\fP	Undo last \f2n\fP (default 1) changes{ }	Braces group commands.sp .3.ft CW_.ft.TE.sp.KE.PPThe value of dot may be changed byspecifying an.I addressfor the command.The simplest address is a line number:.P13.P2refers to the third line of the file, so.P13d.P2deletes the third line of the file, and implicitly renumbersthe lines so the old line 4 is now numbered 3.(This is one of the few places where.CW samdeals with lines directly.)Line.CW 0

Lines 1–500 of 3241.