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