Blob


1 .TH SAM 1
2 .ds a \fR*\ \fP
3 .SH NAME
4 sam, B, E, sam.save, samterm, samsave \- screen editor with structural regular expressions
5 .SH SYNOPSIS
6 .B sam
7 [
8 .I option ...
9 ] [
10 .I files
11 ]
12 .PP
13 .B sam
14 .B -r
15 .I machine
16 .PP
17 .B sam.save
18 .PP
19 .B B
20 .IB file \fR[\fP: line \fR]
21 \&...
22 .PP
23 .B E
24 .I file
25 .SH DESCRIPTION
26 .I Sam
27 is a multi-file editor.
28 It modifies a local copy of an external file.
29 The copy is here called a
30 .IR file .
31 The files are listed in a menu available through mouse button 3
32 or the
33 .B n
34 command.
35 Each file has an associated name, usually the name of the
36 external file from which it was read, and a `modified' bit that indicates whether
37 the editor's file agrees with the external file.
38 The external file is not read into
39 the editor's file until it first becomes the current file\(emthat to
40 which editing commands apply\(emwhereupon its menu entry is printed.
41 The options are
42 .TF -rmachine
43 .TP
44 .B -d
45 Do not `download' the terminal part of
46 .IR sam .
47 Editing will be done with the command language only, as in
48 .IR ed (1).
49 .TP
50 .BI -r " machine
51 Run the host part remotely
52 on the specified machine, the terminal part locally.
53 .TP
54 .BI -s " path
55 Start the host part from the specified file on the remote host.
56 Only meaningful with the
57 .BI -r
58 option.
59 .TP
60 .BI -t " path
61 Start the terminal part from the specified file. Useful
62 for debugging.
63 .PD
64 .SS Regular expressions
65 Regular expressions are as in
66 .IR regexp (7)
67 with the addition of
68 .BR \en
69 to represent newlines.
70 A regular expression may never contain a literal newline character.
71 The empty
72 regular expression stands for the last complete expression encountered.
73 A regular expression in
74 .I sam
75 matches the longest leftmost substring formally
76 matched by the expression.
77 Searching in the reverse direction is equivalent
78 to searching backwards with the catenation operations reversed in
79 the expression.
80 .SS Addresses
81 An address identifies a substring in a file.
82 In the following, `character
83 .IR n '
84 means the null string
85 after the
86 .IR n -th
87 character in the file, with 1 the
88 first character in the file.
89 `Line
90 .IR n '
91 means the
92 .IR n -th
93 match,
94 starting at the beginning of the file, of the regular expression
95 .LR .*\en? .
96 All files always have a current substring, called dot,
97 that is the default address.
98 .SS Simple Addresses
99 .PD 0
100 .TP
101 .BI # n
102 The empty string after character
103 .IR n ;
104 .B #0
105 is the beginning of the file.
106 .TP
107 .I n
108 Line
109 .IR n ;
110 .B 0
111 is the beginning of the file.
112 .TP
113 .BI / regexp /
114 .PD 0
115 .TP
116 .BI ? regexp ?
117 The substring that matches the regular expression,
118 found by looking toward the end
119 .RB ( / )
120 or beginning
121 .RB ( ? )
122 of the file,
123 and if necessary continuing the search from the other end to the
124 starting point of the search.
125 The matched substring may straddle
126 the starting point.
127 When entering a pattern containing a literal question mark
128 for a backward search, the question mark should be
129 specified as a member of a class.
130 .PD
131 .TP
132 .B 0
133 The string before the first full line.
134 This is not necessarily
135 the null string; see
136 .B +
137 and
138 .B -
139 below.
140 .TP
141 .B $
142 The null string at the end of the file.
143 .TP
144 .B .
145 Dot.
146 .TP
147 .B \&'
148 The mark in the file (see the
149 .B k
150 command below).
151 .TP
152 \fB"\f2regexp\fB"\f1\f1
153 Preceding a simple address (default
154 .BR . ),
155 refers to the address evaluated in the unique file whose menu line
156 matches the regular expression.
157 .PD
158 .SS Compound Addresses
159 In the following,
160 .I a1
161 and
162 .I a2
163 are addresses.
164 .TF a1+a2
165 .TP
166 .IB a1 + a2
167 The address
168 .I a2
169 evaluated starting at the end of
170 .IR a1 .
171 .TP
172 .IB a1 - a2
173 The address
174 .I a2
175 evaluated looking in the reverse direction
176 starting at the beginning of
177 .IR a1 .
178 .TP
179 .IB a1 , a2
180 The substring from the beginning of
181 .I a1
182 to the end of
183 .IR a2 .
184 If
185 .I a1
186 is missing,
187 .B 0
188 is substituted.
189 If
190 .I a2
191 is missing,
192 .B $
193 is substituted.
194 .TP
195 .IB a1 ; a2
196 Like
197 .IB a1 , a2\f1,
198 but with
199 .I a2
200 evaluated at the end of, and dot set to,
201 .IR a1 .
202 .PD
203 .PP
204 The operators
205 .B +
206 and
207 .B -
208 are high precedence, while
209 .B ,
210 and
211 .B ;
212 are low precedence.
213 .PP
214 In both
215 .B +
216 and
217 .B -
218 forms, if
219 .I a2
220 is a line or character address with a missing
221 number, the number defaults to 1.
222 If
223 .I a1
224 is missing,
225 .L .
226 is substituted.
227 If both
228 .I a1
229 and
230 .I a2
231 are present and distinguishable,
232 .B +
233 may be elided.
234 .I a2
235 may be a regular
236 expression; if it is delimited by
237 .LR ? 's,
238 the effect of the
239 .B +
240 or
241 .B -
242 is reversed.
243 .PP
244 It is an error for a compound address to represent a malformed substring.
245 Some useful idioms:
246 .IB a1 +-
247 \%(\f2a1\fB-+\f1)
248 selects the line containing
249 the end (beginning) of a1.
250 .BI 0/ regexp /
251 locates the first match of the expression in the file.
252 (The form
253 .B 0;//
254 sets dot unnecessarily.)
255 .BI ./ regexp ///
256 finds the second following occurrence of the expression,
257 and
258 .BI .,/ regexp /
259 extends dot.
260 .SS Commands
261 In the following, text demarcated by slashes represents text delimited
262 by any printable
263 character except alphanumerics.
264 Any number of
265 trailing delimiters may be elided, with multiple elisions then representing
266 null strings, but the first delimiter must always
267 be present.
268 In any delimited text,
269 newline may not appear literally;
270 .B \en
271 may be typed for newline; and
272 .B \e/
273 quotes the delimiter, here
274 .LR / .
275 Backslash is otherwise interpreted literally, except in
276 .B s
277 commands.
278 .PP
279 Most commands may be prefixed by an address to indicate their range
280 of operation.
281 Those that may not are marked with a
282 .L *
283 below.
284 If a command takes
285 an address and none is supplied, dot is used.
286 The sole exception is
287 the
288 .B w
289 command, which defaults to
290 .BR 0,$ .
291 In the description, `range' is used
292 to represent whatever address is supplied.
293 Many commands set the
294 value of dot as a side effect.
295 If so, it is always set to the `result'
296 of the change: the empty string for a deletion, the new text for an
297 insertion, etc. (but see the
298 .B s
299 and
300 .B e
301 commands).
302 .br
303 .ne 1.2i
304 .SS Text commands
305 .PD 0
306 .TP
307 .BI a/ text /
308 .TP
309 or
310 .TP
311 .B a
312 .TP
313 .I lines of text
314 .TP
315 .B .
316 Insert the text into the file after the range.
317 Set dot.
318 .PD
319 .TP
320 .B c\fP
321 .br
322 .ns
323 .TP
324 .B i\fP
325 Same as
326 .BR a ,
327 but
328 .B c
329 replaces the text, while
330 .B i
331 inserts
332 .I before
333 the range.
334 .TP
335 .B d
336 Delete the text in the range.
337 Set dot.
338 .TP
339 .BI s/ regexp / text /
340 Substitute
341 .I text
342 for the first match to the regular expression in the range.
343 Set dot to the modified range.
344 In
345 .I text
346 the character
347 .B &
348 stands for the string
349 that matched the expression.
350 Backslash behaves as usual unless followed by
351 a digit:
352 .BI \e d
353 stands for the string that matched the
354 subexpression begun by the
355 .IR d -th
356 left parenthesis.
357 If
358 .I s
359 is followed immediately by a
360 number
361 .IR n ,
362 as in
363 .BR s2/x/y/ ,
364 the
365 .IR n -th
366 match in the range is substituted.
367 If the
368 command is followed by a
369 .BR g ,
370 as in
371 .BR s/x/y/g ,
372 all matches in the range
373 are substituted.
374 .TP
375 .BI m " a1
376 .br
377 .ns
378 .TP
379 .BI t " a1
380 Move
381 .RB ( m )
382 or copy
383 .RB ( t )
384 the range to after
385 .IR a1 .
386 Set dot.
387 .SS Display commands
388 .PD 0
389 .TP
390 .B p
391 Print the text in the range.
392 Set dot.
393 .TP
394 .B =
395 Print the line address and character address of the range.
396 .TP
397 .B =#
398 Print just the character address of the range.
399 .PD
400 .SS File commands
401 .PD 0
402 .TP
403 .BI \*ab " file-list
404 Set the current file to the first file named in the list
405 that
406 .I sam
407 also has in its menu.
408 The list may be expressed
409 .BI < "Plan 9 command"
410 in which case the file names are taken as words (in the shell sense)
411 generated by the Plan 9 command.
412 .TP
413 .BI \*aB " file-list
414 Same as
415 .BR b ,
416 except that file names not in the menu are entered there,
417 and all file names in the list are examined.
418 .TP
419 .B \*an
420 Print a menu of files.
421 The format is:
422 .RS
423 .TP 11
424 .BR ' " or blank
425 indicating the file is modified or clean,
426 .TP 11
427 .BR - " or \&" +
428 indicating the file is unread or has been read
429 (in the terminal,
430 .B *
431 means more than one window is open),
432 .TP 11
433 .BR . " or blank
434 indicating the current file,
435 .TP 11
436 a blank,
437 .TP 11
438 and the file name.
439 .RE
440 .TP 0
441 .BI \*aD " file-list
442 Delete the named files from the menu.
443 If no files are named, the current file is deleted.
444 It is an error to
445 .B D
446 a modified file, but a subsequent
447 .B D
448 will delete such a file.
449 .PD
450 .SS I/O Commands
451 .PD 0
452 .TP
453 .BI \*ae " filename
454 Replace the file by the contents of the named external file.
455 Set dot to the beginning of the file.
456 .TP
457 .BI r " filename
458 Replace the text in the range by the contents of the named external file.
459 Set dot.
460 .TP
461 .BI w " filename
462 Write the range (default
463 .BR 0,$ )
464 to the named external file.
465 .TP
466 .BI \*af " filename
467 Set the file name and print the resulting menu entry.
468 .PP
469 If the file name is absent from any of these, the current file name is used.
470 .B e
471 always sets the file name;
472 .B r
473 and
474 .B w
475 do so if the file has no name.
476 .TP
477 .BI < " Plan 9-command
478 Replace the range by the standard output of the
479 Plan 9 command.
480 .TP
481 .BI > " Plan 9-command
482 Send the range to the standard input of the
483 Plan 9 command.
484 .TP
485 .BI | " Plan 9-command
486 Send the range to the standard input, and replace it by
487 the standard output, of the
488 Plan 9 command.
489 .TP
490 .BI \*a! " Plan 9-command
491 Run the
492 Plan 9 command.
493 .TP
494 .BI \*acd " directory
495 Change working directory.
496 If no directory is specified,
497 .B $home
498 is used.
499 .PD
500 .PP
501 In any of
502 .BR < ,
503 .BR > ,
504 .B |
505 or
506 .BR ! ,
507 if the
508 .I Plan 9 command
509 is omitted the last
510 .I Plan 9 command
511 (of any type) is substituted.
512 If
513 .I sam
514 is
515 .I downloaded
516 (using the mouse and raster display, i.e. not using option
517 .BR -d ),
518 .B !
519 sets standard input to
520 .BR /dev/null ,
521 and otherwise
522 unassigned output
523 .RB ( stdout
524 for
525 .B !
526 and
527 .BR > ,
528 .B stderr
529 for all) is placed in
530 .B /tmp/sam.err
531 and the first few lines are printed.
532 .SS Loops and Conditionals
533 .PD 0
534 .TP
535 .BI x/ regexp / " command
536 For each match of the regular expression in the range, run the command
537 with dot set to the match.
538 Set dot to the last match.
539 If the regular
540 expression and its slashes are omitted,
541 .L /.*\en/
542 is assumed.
543 Null string matches potentially occur before every character
544 of the range and at the end of the range.
545 .TP
546 .BI y/ regexp / " command
547 Like
548 .BR x ,
549 but run the command for each substring that lies before, between,
550 or after
551 the matches that would be generated by
552 .BR x .
553 There is no default regular expression.
554 Null substrings potentially occur before every character
555 in the range.
556 .TP
557 .BI \*aX/ regexp / " command
558 For each file whose menu entry matches the regular expression,
559 make that the current file and
560 run the command.
561 If the expression is omitted, the command is run
562 in every file.
563 .TP
564 .BI \*aY/ regexp / " command
565 Same as
566 .BR X ,
567 but for files that do not match the regular expression,
568 and the expression is required.
569 .TP
570 .BI g/ regexp / " command
571 .br
572 .ns
573 .TP
574 .BI v/ regexp / " command
575 If the range contains
576 .RB ( g )
577 or does not contain
578 .RB ( v )
579 a match for the expression,
580 set dot to the range and run the command.
581 .PP
582 These may be nested arbitrarily deeply, but only one instance of either
583 .B X
584 or
585 .B Y
586 may appear in a \%single command.
587 An empty command in an
588 .B x
589 or
590 .B y
591 defaults to
592 .BR p ;
593 an empty command in
594 .B X
595 or
596 .B Y
597 defaults to
598 .BR f .
599 .B g
600 and
601 .B v
602 do not have defaults.
603 .PD
604 .SS Miscellany
605 .TF (empty)
606 .TP
607 .B k
608 Set the current file's mark to the range. Does not set dot.
609 .TP
610 .B \*aq
611 Quit.
612 It is an error to quit with modified files, but a second
613 .B q
614 will succeed.
615 .TP
616 .BI \*au " n
617 Undo the last
618 .I n
619 (default 1)
620 top-level commands that changed the contents or name of the
621 current file, and any other file whose most recent change was simultaneous
622 with the current file's change.
623 Successive
624 .BR u 's
625 move further back in time.
626 The only commands for which u is ineffective are
627 .BR cd ,
628 .BR u ,
629 .BR q ,
630 .B w
631 and
632 .BR D .
633 If
634 .I n
635 is negative,
636 .B u
637 `redoes,' undoing the undo, going forwards in time again.
638 .TP
639 (empty)
640 If the range is explicit, set dot to the range.
641 If
642 .I sam
643 is downloaded, the resulting dot is selected on the screen;
644 otherwise it is printed.
645 If no address is specified (the
646 command is a newline) dot is extended in either direction to
647 line boundaries and printed.
648 If dot is thereby unchanged, it is set to
649 .B .+1
650 and printed.
651 .PD
652 .SS Grouping and multiple changes
653 Commands may be grouped by enclosing them in braces
654 .BR {} .
655 Commands within the braces must appear on separate lines (no backslashes are
656 required between commands).
657 Semantically, an opening brace is like a command:
658 it takes an (optional) address and sets dot for each sub-command.
659 Commands within the braces are executed sequentially, but changes made
660 by one command are not visible to other commands (see the next
661 paragraph).
662 Braces may be nested arbitrarily.
663 .PP
664 When a command makes a number of changes to a file, as in
665 .BR x/re/c/text/ ,
666 the addresses of all changes to the file are computed in the original file.
667 If the changes are in sequence,
668 they are applied to the file.
669 Successive insertions at the same address are catenated into a single
670 insertion composed of the several insertions in the order applied.
671 .SS The terminal
672 What follows refers to behavior of
673 .I sam
674 when downloaded, that is, when
675 operating as a display editor on a raster display.
676 This is the default
677 behavior; invoking
678 .I sam
679 with the
680 .B -d
681 (no download) option provides access
682 to the command language only.
683 .PP
684 Each file may have zero or more windows open.
685 Each window is equivalent
686 and is updated simultaneously with changes in other windows on the same file.
687 Each window has an independent value of dot, indicated by a highlighted
688 substring on the display.
689 Dot may be in a region not within
690 the window.
691 There is usually a `current window',
692 marked with a dark border, to which typed text and editing
693 commands apply.
694 Text may be typed and edited as in
695 .IR rio (1);
696 also the escape key (ESC) selects (sets dot to) text typed
697 since the last mouse button hit.
698 .PP
699 The button 3 menu controls window operations.
700 The top of the menu
701 provides the following operators, each of which uses one or
702 more
703 .IR rio -like
704 cursors to prompt for selection of a window or sweeping
705 of a rectangle.
706 `Sweeping' a null rectangle gets a large window, disjoint
707 from the command window or the whole screen, depending on
708 where the null rectangle is.
709 .TF resize
710 .TP
711 .B new
712 Create a new, empty file.
713 .TP
714 .B zerox
715 Create a copy of an existing window.
716 .TP
717 .B resize
718 As in
719 .IR rio .
720 .TP
721 .B close
722 Delete the window.
723 In the last window of a file,
724 .B close
725 is equivalent to a
726 .B D
727 for the file.
728 .TP
729 .B write
730 Equivalent to a
731 .B w
732 for the file.
733 .PD
734 .PP
735 Below these operators is a list of available files, starting with
736 .BR ~~sam~~ ,
737 the command window.
738 Selecting a file from the list makes the most recently
739 used window on that file current, unless it is already current, in which
740 case selections cycle through the open windows.
741 If no windows are open
742 on the file, the user is prompted to open one.
743 Files other than
744 .B ~~sam~~
745 are marked with one of the characters
746 .B -+*
747 according as zero, one, or more windows
748 are open on the file.
749 A further mark
750 .L .
751 appears on the file in the current window and
752 a single quote,
753 .BR ' ,
754 on a file modified since last write.
755 .PP
756 The command window, created automatically when
757 .B sam
758 starts, is an ordinary window except that text typed to it
759 is interpreted as commands for the editor rather than passive text,
760 and text printed by editor commands appears in it.
761 The behavior is like
762 .IR rio ,
763 with an `output point' that separates commands being typed from
764 previous output.
765 Commands typed in the command window apply to the
766 current open file\(emthe file in the most recently
767 current window.
768 .SS Manipulating text
769 Button 1 changes selection, much like
770 .IR rio .
771 Pointing to a non-current window with button 1 makes it current;
772 within the current window, button 1 selects text, thus setting dot.
773 Double-clicking selects text to the boundaries of words, lines,
774 quoted strings or bracketed strings, depending on the text at the click.
775 .PP
776 Button 2 provides a menu of editing commands:
777 .TF /regexp
778 .TP
779 .B cut
780 Delete dot and save the deleted text in the snarf buffer.
781 .TP
782 .B paste
783 Replace the text in dot by the contents of the snarf buffer.
784 .TP
785 .B snarf
786 Save the text in dot in the snarf buffer.
787 .TP
788 .B plumb
789 Send the text in the selection as a plumb
790 message. If the selection is empty,
791 the white-space-delimited block of text is sent as a plumb message
792 with a
793 .B click
794 attribute defining where the selection lies (see
795 .IR plumb (7)).
796 .TP
797 .B look
798 Search forward for the next occurrence of the literal text in dot.
799 If dot is the null string, the text in the snarf buffer is
800 used.
801 The snarf buffer is unaffected.
802 .TP
803 .B <rio>
804 Exchange snarf buffers with
805 .IR rio .
806 .TP
807 .BI / regexp
808 Search forward for the next match of the last regular expression
809 typed in a command.
810 (Not in command window.)
811 .TP
812 .B send
813 Send the text in dot, or the snarf buffer if
814 dot is the null string, as if it were typed to the command window.
815 Saves the sent text in the snarf buffer.
816 (Command window only.)
817 .PD
818 .SS External communication
819 .I Sam
820 listens to the
821 .B edit
822 plumb port.
823 If plumbing is not active,
824 on invocation
825 .I sam
826 creates a named pipe
827 .BI /srv/sam. user
828 which acts as an additional source of commands. Characters written to
829 the named pipe are treated as if they had been typed in the command window.
830 .PP
831 .I B
832 is a shell-level command that causes an instance of
833 .I sam
834 running on the same terminal to load the named
835 .IR files .
836 .I B
837 uses either plumbing or the named pipe, whichever service is available.
838 If plumbing is not enabled,
839 the option allows a line number to be specified for
840 the initial position to display in the last named file
841 (plumbing provides a more general mechanism for this ability).
842 .PP
843 .I E
844 is a shell-level command that can be used as
845 .B $EDITOR
846 in a Unix environment.
847 It runs
848 .I B
849 on
850 .I file
851 and then does not exit until
852 .I file
853 is changed, which is taken as a signal that
854 .I file
855 is done being edited.
856 .SS Abnormal termination
857 If
858 .I sam
859 terminates other than by a
860 .B q
861 command (by hangup, deleting its window, etc.), modified
862 files are saved in an
863 executable file,
864 .BR $HOME/sam.save .
865 This program, when executed, asks whether to write
866 each file back to a external file.
867 The answer
868 .L y
869 causes writing; anything else skips the file.
870 .SH FILES
871 .TF $HOME/sam.save
872 .TP
873 .B $HOME/sam.save
874 .TP
875 .B $HOME/sam.err
876 .TP
877 .B \*9/bin/samsave
878 the program called to unpack
879 .BR $HOME/sam.save .
880 .SH SOURCE
881 .TF \*9/src/cmd/samterm
882 .TP
883 .B \*9/src/cmd/sam
884 source for
885 .I sam
886 itself
887 .TP
888 .B \*9/src/cmd/samterm
889 source for the separate terminal part
890 .TP
891 .B \*9/bin/B
892 .TP
893 .B \*9/bin/E
894 .SH SEE ALSO
895 .IR ed (1),
896 .IR sed (1),
897 .IR grep (1),
898 .IR rio (1),
899 .IR regexp (7).
900 .PP
901 Rob Pike,
902 ``The text editor sam''.