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