1 ;;; viper-ex.el -- functions implementing the Ex commands for Viper
2 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 (defconst vip-ex-work-buf-name " *ex-working-space*")
26 (defconst vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
29 ;;; Variable completion in :set command
31 ;; The list of Ex commands. Used for completing command names.
32 (defconst ex-token-alist
33 '(("!") ("=") (">") ("&") ("~")
34 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
35 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
36 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
37 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
38 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
39 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
40 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
41 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
43 ;; A-list of Ex variables that can be set using the :set command.
44 (defconst ex-variable-alist
45 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
46 ("tab-stop-local") ("tsl") ("tabstop") ("ts")
47 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
49 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
50 ("noreadonly") ("noro") ("nomagic") ("noma")
51 ("noignorecase") ("noic") ("noautoindent") ("noai")
52 ("magic") ("ma") ("ignorecase") ("ic") ("autoindent") ("ai")
57 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
61 ;; If non-nil, gives type of address; if nil, it is a command.
62 (defvar ex-token-type nil)
64 ;; List of addresses passed to Ex command
65 (defvar ex-addresses nil)
67 ;; It seems that this flag is used only for `#', `print', and `list', which
68 ;; aren't implemented. Check later.
71 ;; "buffer" where Ex commands keep deleted data.
72 ;; In Emacs terms, this is a register.
73 (defvar ex-buffer nil)
78 ;; Flag for global command.
79 (defvar ex-g-flag nil)
81 ;; If t, global command is executed on lines not matching ex-g-pat.
82 (defvar ex-g-variant nil)
84 ;; Save reg-exp used in substitute.
85 (defvar ex-reg-exp nil)
88 ;; Replace pattern for substitute.
91 ;; Pattern for global command.
94 ;; `sh' doesn't seem to expand wildcards, like `*'
95 (defconst ex-find-file-shell "csh"
96 "Shell in which to interpret wildcards.")
97 (defvar ex-find-file-shell-options "-f"
98 "*Options to pass to `ex-find-file-shell'.")
100 ;; Remembers the previous Ex tag.
103 ;; file used by Ex commands like :r, :w, :n
106 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
107 (defvar ex-variant nil)
109 ;; Specified the offset of an Ex command, such as :read.
110 (defvar ex-offset nil)
112 ;; Tells Ex that this is a w>> command.
113 (defvar ex-append nil)
115 ;; File containing the shell command to be executed at Ex prompt,
117 (defvar ex-cmdfile nil)
119 ;; flag used in vip-ex-read-file-name to indicate that we may be reading
120 ;; multiple file names. Used for :edit and :next
121 (defvar vip-keep-reading-filename nil)
123 (defconst ex-cycle-other-window t
124 "*If t, :n and :b cycles through files and buffers in other window.
125 Then :N and :B cycles in the current window. If nil, this behavior is
128 (defconst ex-cycle-through-non-files nil
129 "*Cycle through *scratch* and other buffers that don't visit any file.")
131 ;; Last shell command executed with :! command.
132 (defvar vip-ex-last-shell-com nil)
134 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
135 (defvar vip-incomplete-ex-cmd nil)
137 ;; Remembers the last ex-command prompt.
138 (defvar vip-last-ex-prompt "")
143 (defun vip-check-sub (str)
144 "Check if ex-token is an initial segment of STR."
145 (let ((length (length ex-token)))
146 (if (and (<= length (length str))
147 (string= ex-token (substring str 0 length)))
149 (setq ex-token-type 'non-command))))
151 (defun vip-get-ex-com-subr ()
152 "Get a complete ex command."
153 (let (case-fold-search)
155 (re-search-forward "[a-zA-Z][a-zA-Z]*")
156 (setq ex-token-type 'command)
157 (setq ex-token (buffer-substring (point) (mark t)))
158 (exchange-point-and-mark)
159 (cond ((looking-at "a")
160 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
161 ((looking-at "ar") (vip-check-sub "args"))
162 (t (vip-check-sub "append"))))
163 ((looking-at "h") (vip-check-sub "help"))
165 (cond ((looking-at "cd") (vip-check-sub "cd"))
166 ((looking-at "ch") (vip-check-sub "chdir"))
167 ((looking-at "co") (vip-check-sub "copy"))
168 (t (vip-check-sub "change"))))
169 ((looking-at "d") (vip-check-sub "delete"))
170 ((looking-at "b") (vip-check-sub "buffer"))
171 ((looking-at "B") (vip-check-sub "Buffer"))
173 (if (looking-at "ex") (vip-check-sub "ex")
174 (vip-check-sub "edit")))
175 ((looking-at "f") (vip-check-sub "file"))
176 ((looking-at "g") (vip-check-sub "global"))
177 ((looking-at "i") (vip-check-sub "insert"))
178 ((looking-at "j") (vip-check-sub "join"))
179 ((looking-at "l") (vip-check-sub "list"))
181 (cond ((looking-at "map") (vip-check-sub "map"))
182 ((looking-at "mar") (vip-check-sub "mark"))
183 (t (vip-check-sub "move"))))
184 ((looking-at "k[a-z][^a-z]")
185 (setq ex-token "kmark")
187 (exchange-point-and-mark)) ; this is canceled out by another
188 ; exchange-point-and-mark at the end
189 ((looking-at "k") (vip-check-sub "kmark"))
190 ((looking-at "n") (if (looking-at "nu")
191 (vip-check-sub "number")
192 (vip-check-sub "next")))
193 ((looking-at "N") (vip-check-sub "Next"))
194 ((looking-at "o") (vip-check-sub "open"))
196 (cond ((looking-at "pre") (vip-check-sub "preserve"))
197 ((looking-at "pu") (vip-check-sub "put"))
198 ((looking-at "pw") (vip-check-sub "pwd"))
199 (t (vip-check-sub "print"))))
200 ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
201 ((looking-at "R") (vip-check-sub "RelatedFile"))
202 ((looking-at "q") (vip-check-sub "quit"))
204 (cond ((looking-at "rec") (vip-check-sub "recover"))
205 ((looking-at "rew") (vip-check-sub "rewind"))
206 (t (vip-check-sub "read"))))
208 (cond ((looking-at "se") (vip-check-sub "set"))
209 ((looking-at "sh") (vip-check-sub "shell"))
210 ((looking-at "so") (vip-check-sub "source"))
211 ((looking-at "sr") (vip-check-sub "sr"))
212 ((looking-at "st") (vip-check-sub "stop"))
213 ((looking-at "sus") (vip-check-sub "suspend"))
214 ((looking-at "subm") (vip-check-sub "submitReport"))
215 (t (vip-check-sub "substitute"))))
217 (if (looking-at "ta") (vip-check-sub "tag")
218 (vip-check-sub "transfer")))
220 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
221 ((looking-at "unm") (vip-check-sub "unmap"))
222 (t (vip-check-sub "undo"))))
224 (cond ((looking-at "ve") (vip-check-sub "version"))
225 ((looking-at "vi") (vip-check-sub "visual"))
226 (t (vip-check-sub "vglobal"))))
228 (if (looking-at "wq") (vip-check-sub "wq")
229 (vip-check-sub "write")))
231 (if (looking-at "WW")
232 (vip-check-sub "WWrite")
233 (vip-check-sub "Write")))
234 ((looking-at "x") (vip-check-sub "xit"))
235 ((looking-at "y") (vip-check-sub "yank"))
236 ((looking-at "z") (vip-check-sub "z")))
237 (exchange-point-and-mark)
240 (defun vip-get-ex-token ()
241 "Get an ex-token which is either an address or a command.
242 A token has a type, \(command, address, end-mark\), and a value."
243 (save-window-excursion
244 (set-buffer vip-ex-work-buf)
245 (skip-chars-forward " \t|")
246 (cond ((looking-at "#")
247 (setq ex-token-type 'command)
248 (setq ex-token (char-to-string (following-char)))
250 ((looking-at "[a-z]") (vip-get-ex-com-subr))
253 (setq ex-token-type 'dot))
254 ((looking-at "[0-9]")
256 (re-search-forward "[0-9]*")
258 (cond ((eq ex-token-type 'plus) 'add-number)
259 ((eq ex-token-type 'minus) 'sub-number)
261 (setq ex-token (string-to-int (buffer-substring (point) (mark t)))))
264 (setq ex-token-type 'end))
267 (setq ex-token-type 'whole))
269 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
273 (setq ex-token-type 'plus))
274 ((looking-at "+[0-9]")
276 (setq ex-token-type 'plus))
278 (error vip-BadAddress))))
280 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
284 (setq ex-token-type 'minus))
285 ((looking-at "-[0-9]")
287 (setq ex-token-type 'minus))
289 (error vip-BadAddress))))
294 (while (and (not (eolp)) cont)
295 ;;(re-search-forward "[^/]*/")
296 (re-search-forward "[^/]*\\(/\\|\n\\)")
297 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
300 (setq ex-token (buffer-substring (point) (mark t)))
301 (if (looking-at "/") (forward-char 1))
302 (setq ex-token-type 'search-forward))
307 (while (and (not (eolp)) cont)
308 ;;(re-search-forward "[^\\?]*\\?")
309 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
310 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
313 (if (not (looking-at "\n")) (forward-char 1))))
314 (setq ex-token-type 'search-backward)
315 (setq ex-token (buffer-substring (1- (point)) (mark t))))
318 (setq ex-token-type 'comma))
321 (setq ex-token-type 'semi-colon))
322 ((looking-at "[!=><&~]")
323 (setq ex-token-type 'command)
324 (setq ex-token (char-to-string (following-char)))
327 (setq ex-token-type 'goto-mark)
329 (cond ((looking-at "'") (setq ex-token nil))
330 ((looking-at "[a-z]") (setq ex-token (following-char)))
331 (t (error "Marks are ' and a-z")))
334 (setq ex-token-type 'end-mark)
335 (setq ex-token "goto"))
337 (error vip-BadExCommand)))))
339 ;; Reads Ex command. Tries to determine if it has to exit because command
340 ;; is complete or invalid. If not, keeps reading command.
341 (defun ex-cmd-read-exit ()
343 (setq vip-incomplete-ex-cmd t)
344 (let ((quit-regex1 (concat
346 "set[ \t]*" "\\|" "edit[ \t]*" "\\|" "[nN]ext[ \t]*"
347 "\\|" "unm[ \t]*" "\\|" "^[ \t]*rep"
357 "^[ \t]*$" "\\|" "[ktgjmsz][ \t]*$" "\\|" "^[ \t]*ab.*"
358 "\\|" "tr[ansfer \t]*" "\\|" "sr[ \t]*"
359 "\\|" "mo.*" "\\|" "^[ \t]*k?ma[^p]*"
360 "\\|" "^[ \t]*fi.*" "\\|" "v?gl.*" "\\|" "[vg][ \t]*$"
361 "\\|" "jo.*" "\\|" "^[ \t]*ta.*" "\\|" "^[ \t]*una.*"
362 "\\|" "^[ \t]*su.*" "\\|['`][a-z][ \t]*"
363 "\\|" "![ \t]*[a-zA-Z].*"
367 (save-window-excursion ;; put cursor at the end of the Ex working buffer
368 (set-buffer vip-ex-work-buf)
369 (goto-char (point-max)))
370 (cond ((vip-looking-back quit-regex1) (exit-minibuffer))
371 ((vip-looking-back stay-regex) (insert " "))
372 ((vip-looking-back quit-regex2) (exit-minibuffer))
375 ;; complete Ex command
376 (defun ex-cmd-complete ()
378 (let (save-pos dist compl-list string-to-complete completion-result)
381 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
385 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
387 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
388 ;; Preceding characters are not the ones allowed in an Ex command
389 ;; or we have typed past command name.
390 ;; Note: we didn't do parsing, so there may be surprises.
391 (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
392 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
393 (looking-at "[^ \t\n\C-m]"))
395 (with-output-to-temp-buffer "*Completions*"
396 (display-completion-list
397 (vip-alist-to-list ex-token-alist))))
398 ;; Preceding chars may be part of a command name
399 (setq string-to-complete (buffer-substring save-pos (point)))
400 (setq completion-result
401 (try-completion string-to-complete ex-token-alist))
403 (cond ((eq completion-result t) ; exact match--do nothing
404 (vip-tmp-insert-at-eob " (Sole completion)"))
405 ((eq completion-result nil)
406 (vip-tmp-insert-at-eob " (No match)"))
407 (t ;; partial completion
409 (delete-region (point) (point-max))
410 (insert completion-result)
411 (let (case-fold-search)
413 (vip-filter-alist (concat "^" completion-result)
415 (if (> (length compl-list) 1)
416 (with-output-to-temp-buffer "*Completions*"
417 (display-completion-list
418 (vip-alist-to-list (reverse compl-list)))))))
421 (defun vip-ex (&optional string)
422 "Ex commands within Viper."
427 (let* ((map (copy-keymap minibuffer-local-map))
433 (vip-add-keymap vip-ex-cmd-map map)
435 (setq com-str (or string (vip-read-string-with-history
441 (save-window-excursion
443 (or (vip-buffer-live-p vip-ex-work-buf)
444 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name)))
445 (set-buffer vip-ex-work-buf)
446 (delete-region (point-min) (point-max))
447 (insert com-str "\n")
448 (goto-char (point-min)))
449 (setq ex-token-type nil
453 (cond ((memq ex-token-type '(command end-mark))
454 (if address (setq ex-addresses (cons address ex-addresses)))
455 (cond ((string= ex-token "global")
458 ((string= ex-token "vglobal")
462 (vip-execute-ex-command)
463 (save-window-excursion
464 (set-buffer vip-ex-work-buf)
465 (skip-chars-forward " \t")
466 (cond ((looking-at "|")
470 (t (error "`%s': %s" ex-token vip-SpuriousText)))
473 ((eq ex-token-type 'non-command)
474 (error (format "`%s': %s" ex-token vip-BadExCommand)))
475 ((eq ex-token-type 'whole)
477 (cons (point-max) (cons (point-min) ex-addresses))))
478 ((eq ex-token-type 'comma)
480 (cons (if (null address) (point) address) ex-addresses)))
481 ((eq ex-token-type 'semi-colon)
482 (if address (setq dot address))
484 (cons (if (null address) (point) address) ex-addresses)))
485 (t (let ((ans (vip-get-ex-address-subr address dot)))
486 (if ans (setq address ans))))))))
488 (defun vip-get-ex-pat ()
489 "Get a regular expression and set `ex-variant', if found."
490 (save-window-excursion
491 (set-buffer vip-ex-work-buf)
492 (skip-chars-forward " \t")
495 (setq ex-g-variant (not ex-g-variant)
496 ex-g-flag (not ex-g-flag))
498 (skip-chars-forward " \t")))
499 (let ((c (following-char)))
500 (if (string-match "[0-9A-Za-z]" (format "%c" c))
502 "Global regexp must be inside matching non-alphanumeric chars"))
503 (if (looking-at "[^\\\\\n]")
508 (while (and (not (eolp)) cont)
509 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
510 (if (member ex-token '("global" "vglobal"))
512 "Missing closing delimiter for global regexp")
513 (goto-char (point-max))))
514 (if (not (vip-looking-back
515 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
518 (if (= (mark t) (point)) ""
519 (buffer-substring (1- (point)) (mark t))))
524 (defun vip-get-ex-command ()
526 (save-window-excursion
527 (set-buffer vip-ex-work-buf)
528 (if (looking-at "/") (forward-char 1))
529 (skip-chars-forward " \t")
530 (cond ((looking-at "[a-z]")
531 (vip-get-ex-com-subr)
532 (if (eq ex-token-type 'non-command)
533 (error "`%s': %s" ex-token vip-BadExCommand)))
534 ((looking-at "[!=><&~]")
535 (setq ex-token (char-to-string (following-char)))
537 (t (error vip-BadExCommand)))))
539 (defun vip-get-ex-opt-gc (c)
540 "Get an Ex option g or c."
541 (save-window-excursion
542 (set-buffer vip-ex-work-buf)
543 (if (looking-at (format "%c" c)) (forward-char 1))
544 (skip-chars-forward " \t")
545 (cond ((looking-at "g")
555 (defun vip-default-ex-addresses (&optional whole-flag)
556 "Compute default addresses. WHOLE-FLAG means use the whole buffer."
557 (cond ((null ex-addresses)
560 (cons (point-max) (cons (point-min) nil))
561 (cons (point) (cons (point) nil)))))
562 ((null (cdr ex-addresses))
564 (cons (car ex-addresses) ex-addresses)))))
566 (defun vip-get-ex-address ()
567 "Get an ex-address as a marker and set ex-flag if a flag is found."
568 (let ((address (point-marker)) (cont t))
573 (cond ((eq ex-token-type 'command)
574 (if (member ex-token '("print" "list" "#"))
578 (error "Address expected in this Ex command")))
579 ((eq ex-token-type 'end-mark)
581 ((eq ex-token-type 'whole)
582 (error "Trailing address expected"))
583 ((eq ex-token-type 'comma)
584 (error "`%s': %s" ex-token vip-SpuriousText))
585 (t (let ((ans (vip-get-ex-address-subr address (point-marker))))
586 (if ans (setq address ans))))))
589 (defun vip-get-ex-address-subr (old-address dot)
590 "Returns an address as a point."
592 (if (null old-address) (setq old-address dot))
593 (cond ((eq ex-token-type 'dot)
595 ((eq ex-token-type 'add-number)
597 (goto-char old-address)
598 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
599 (setq address (point-marker))))
600 ((eq ex-token-type 'sub-number)
602 (goto-char old-address)
603 (forward-line (- ex-token))
604 (setq address (point-marker))))
605 ((eq ex-token-type 'abs-number)
607 (goto-char (point-min))
608 (if (= ex-token 0) (setq address 0)
609 (forward-line (1- ex-token))
610 (setq address (point-marker)))))
611 ((eq ex-token-type 'end)
612 (setq address (point-max-marker)))
613 ((eq ex-token-type 'plus) t) ; do nothing
614 ((eq ex-token-type 'minus) t) ; do nothing
615 ((eq ex-token-type 'search-forward)
617 (ex-search-address t)
618 (setq address (point-marker))))
619 ((eq ex-token-type 'search-backward)
621 (ex-search-address nil)
622 (setq address (point-marker))))
623 ((eq ex-token-type 'goto-mark)
626 (exchange-point-and-mark)
627 (goto-char (vip-register-to-point
628 (1+ (- ex-token ?a)) 'enforce-buffer)))
629 (setq address (point-marker)))))
633 (defun ex-search-address (forward)
634 "Search pattern and set address."
635 (if (string= ex-token "")
636 (if (null vip-s-string)
637 (error vip-NoPrevSearch)
638 (setq ex-token vip-s-string))
639 (setq vip-s-string ex-token))
643 (re-search-forward ex-token))
645 (re-search-backward ex-token)))
647 (defun vip-get-ex-buffer ()
648 "Get a buffer name and set `ex-count' and `ex-flag' if found."
652 (save-window-excursion
653 (set-buffer vip-ex-work-buf)
654 (skip-chars-forward " \t")
655 (if (looking-at "[a-zA-Z]")
657 (setq ex-buffer (following-char))
659 (skip-chars-forward " \t")))
660 (if (looking-at "[0-9]")
663 (re-search-forward "[0-9][0-9]*")
664 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
665 (skip-chars-forward " \t")))
666 (if (looking-at "[pl#]")
670 (if (not (looking-at "[\n|]"))
671 (error "`%s': %s" ex-token vip-SpuriousText))))
673 (defun vip-get-ex-count ()
677 (save-window-excursion
678 (set-buffer vip-ex-work-buf)
679 (skip-chars-forward " \t")
684 (skip-chars-forward " \t")
685 (if (looking-at "[0-9]")
688 (re-search-forward "[0-9][0-9]*")
689 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
690 (skip-chars-forward " \t")))
691 (if (looking-at "[pl#]")
695 (if (not (looking-at "[\n|]"))
697 (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand))))
699 (defun ex-expand-filsyms (cmd buf)
700 "Expand \% and \# in ex command."
704 (setq cf buffer-file-name)
705 (setq pf (ex-next nil t))) ; this finds alternative file name
706 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
707 (error "No current file to substitute for `\%'"))
708 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
709 (error "No alternate file to substitute for `#'"))
711 (set-buffer (get-buffer-create " *ex-tmp*"))
714 (goto-char (point-min))
715 (while (re-search-forward "%\\|#" nil t)
716 (let ((data (match-data))
717 (char (buffer-substring (match-beginning 0) (match-end 0))))
718 (if (vip-looking-back (concat "\\\\" char))
720 (store-match-data data)
721 (if (string= char "%")
723 (replace-match pf)))))
725 (setq ret (buffer-substring (point-min) (point)))
729 (defun vip-get-ex-file ()
730 "Get a file name and set ex-variant, `ex-append' and `ex-offset' if found."
738 (save-window-excursion
739 (set-buffer vip-ex-work-buf)
740 (skip-chars-forward " \t")
742 (if (not (vip-looking-back "[ \t]"))
746 (skip-chars-forward " \t"))
749 (skip-chars-forward " \t")))
750 (if (looking-at ">>")
755 (skip-chars-forward " \t")))
760 (re-search-forward "[ \t\n]")
762 (setq ex-offset (buffer-substring (point) (mark t)))
764 (skip-chars-forward " \t")))
765 ;; this takes care of :r, :w, etc., when they get file names
766 ;; from the history list
767 (if (member ex-token '("read" "write" "edit" "visual" "next"))
769 (setq ex-file (buffer-substring (point) (1- (point-max))))
771 ;; For :e, match multiple non-white strings separated
772 ;; by white. For others, find the first non-white string
774 (if (string= ex-token "edit")
775 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
779 ;; if file name comes from history, don't leave
780 ;; minibuffer when the user types space
781 (setq vip-incomplete-ex-cmd nil)
782 ;; this must be the last clause in this progn
783 (substring ex-file (match-beginning 0) (match-end 0))
786 ;; this leaves only the command name in the work area
787 ;; file names are gone
788 (delete-region (point) (1- (point-max)))
790 (goto-char (point-max))
791 (skip-chars-backward " \t\n")
792 (setq prompt (buffer-substring (point-min) (point)))
795 (setq vip-last-ex-prompt prompt)
797 ;; If we just finished reading command, redisplay prompt
798 (if vip-incomplete-ex-cmd
799 (setq ex-file (vip-ex-read-file-name (format ":%s " prompt)))
800 ;; file was typed in-line
801 (setq ex-file (or ex-file "")))
805 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
806 ;; file names, arranges to re-enter the minibuffer.
807 (defun vip-complete-filename-or-exit ()
809 (setq vip-keep-reading-filename t)
810 ;; don't exit if directory---ex-commands don't
811 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
812 (t (minibuffer-complete-word))))
815 (defun ex-cmd-accepts-multiple-files-p (token)
816 (member token '("edit" "next" "Next")))
818 ;; If user doesn't enter anything, then "" is returned, i.e., the
819 ;; prompt-directory is not returned.
820 (defun vip-ex-read-file-name (prompt)
822 (minibuffer-local-completion-map
823 (copy-keymap minibuffer-local-completion-map))
826 (vip-add-keymap ex-read-filename-map minibuffer-local-completion-map)
828 (setq cont (setq vip-keep-reading-filename t))
830 (setq vip-keep-reading-filename nil
831 val (read-file-name (concat prompt str) nil default-directory)
832 str (concat str (if (equal val "") "" " ")
833 val (if (equal val "") "" " ")))
835 ;; Only edit, next, and Next commands accept multiple files.
836 ;; vip-keep-reading-filename is set in the anonymous function that is
837 ;; bound to " " in ex-read-filename-map.
838 (setq cont (and vip-keep-reading-filename
839 (ex-cmd-accepts-multiple-files-p ex-token)))
842 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
843 end (string-match "[ \t]*$" str)) ; delete trailing blanks
844 (if (member ex-token '("read" "write"))
845 (if (string-match "[\t ]*!" str)
846 ;; this is actually a shell command
850 (setq vip-last-ex-prompt (concat vip-last-ex-prompt " !")))))
851 (substring str (or beg 0) end)))
853 (defun vip-execute-ex-command ()
854 "Execute ex command using the value of addresses."
855 (vip-deactivate-mark)
856 (cond ((string= ex-token "args") (ex-args))
857 ((string= ex-token "copy") (ex-copy nil))
858 ((string= ex-token "cd") (ex-cd))
859 ((string= ex-token "chdir") (ex-cd))
860 ((string= ex-token "delete") (ex-delete))
861 ((string= ex-token "edit") (ex-edit))
862 ((string= ex-token "file") (vip-info-on-file))
863 ((string= ex-token "goto") (ex-goto))
864 ((string= ex-token "help") (ex-help))
865 ((string= ex-token "join") (ex-line "join"))
866 ((string= ex-token "kmark") (ex-mark))
867 ((string= ex-token "mark") (ex-mark))
868 ((string= ex-token "map") (ex-map))
869 ((string= ex-token "move") (ex-copy t))
870 ((string= ex-token "next") (ex-next ex-cycle-other-window))
871 ((string= ex-token "Next") (ex-next (not ex-cycle-other-window)))
872 ((string= ex-token "RelatedFile") (ex-next-related-buffer 1))
873 ((string= ex-token "put") (ex-put))
874 ((string= ex-token "pwd") (ex-pwd))
875 ((string= ex-token "preserve") (ex-preserve))
876 ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1))
877 ((string= ex-token "quit") (ex-quit))
878 ((string= ex-token "read") (ex-read))
879 ((string= ex-token "recover") (ex-recover))
880 ((string= ex-token "rewind") (ex-rewind))
881 ((string= ex-token "submitReport") (vip-submit-report))
882 ((string= ex-token "set") (ex-set))
883 ((string= ex-token "shell") (ex-shell))
884 ((string= ex-token "source") (ex-source))
885 ((string= ex-token "sr") (ex-substitute t t))
886 ((string= ex-token "substitute") (ex-substitute))
887 ((string= ex-token "suspend") (suspend-emacs))
888 ((string= ex-token "stop") (suspend-emacs))
889 ((string= ex-token "transfer") (ex-copy nil))
890 ((string= ex-token "buffer") (if ex-cycle-other-window
891 (vip-switch-to-buffer-other-window)
892 (vip-switch-to-buffer)))
893 ((string= ex-token "Buffer") (if ex-cycle-other-window
894 (vip-switch-to-buffer)
895 (vip-switch-to-buffer-other-window)))
896 ((string= ex-token "tag") (ex-tag))
897 ((string= ex-token "undo") (vip-undo))
898 ((string= ex-token "unmap") (ex-unmap))
899 ((string= ex-token "version") (vip-version))
900 ((string= ex-token "visual") (ex-edit))
901 ((string= ex-token "write") (ex-write nil))
902 ((string= ex-token "Write") (save-some-buffers))
903 ((string= ex-token "wq") (ex-write t))
904 ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask
905 ((string= ex-token "xit") (ex-write t))
906 ((string= ex-token "yank") (ex-yank))
907 ((string= ex-token "!") (ex-command))
908 ((string= ex-token "=") (ex-line-no))
909 ((string= ex-token ">") (ex-line "right"))
910 ((string= ex-token "<") (ex-line "left"))
911 ((string= ex-token "&") (ex-substitute t))
912 ((string= ex-token "~") (ex-substitute t t))
913 ((or (string= ex-token "append")
914 (string= ex-token "change")
915 (string= ex-token "insert")
916 (string= ex-token "open"))
918 (format "`%s': Obsolete command, not supported by Viper"
920 ((or (string= ex-token "abbreviate")
921 (string= ex-token "unabbreviate"))
924 "`%s': Vi's abbrevs are obsolete. Use more powerful Emacs' abbrevs"
926 ((or (string= ex-token "list")
927 (string= ex-token "print")
928 (string= ex-token "z")
929 (string= ex-token "#"))
931 (format "`%s': Command not implemented in Viper" ex-token)))
932 (t (error (format "`%s': %s" ex-token vip-BadExCommand)))))
934 (defun vip-undisplayed-files ()
938 (if (null (get-buffer-window b))
939 (let ((f (buffer-file-name b)))
941 (if ex-cycle-through-non-files
942 (let ((s (buffer-name b)))
943 (if (string= " " (substring s 0 1))
952 (let ((l (vip-undisplayed-files))
955 (while (not (null l))
957 (setq args (format "%s %d) %s\n" args file-count (car l))
958 file-count (1+ file-count)))
960 (if (string= args "")
961 (message "All files are already displayed")
963 (save-window-excursion
964 (with-output-to-temp-buffer " *vip-info*"
965 (princ "\n\nThese files are not displayed in any window.\n")
966 (princ "\n=============\n")
968 (princ "\n=============\n")
969 (princ "\nThe numbers can be given as counts to :next. ")
970 (princ "\n\nPress any key to continue...\n\n"))
971 (vip-read-event))))))
974 "Ex cd command. Default directory of this buffer changes."
976 (if (string= ex-file "")
978 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
980 (defun ex-copy (del-flag)
981 "Ex copy and move command. DEL-FLAG means delete."
982 (vip-default-ex-addresses)
983 (let ((address (vip-get-ex-address))
984 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
988 (vip-enlarge-region (mark t) (point))
990 (kill-region (point) (mark t))
991 (copy-region-as-kill (point) (mark t)))
994 (with-output-to-temp-buffer "*copy text*"
996 (if (or del-flag ex-g-flag ex-g-variant)
998 (buffer-substring (point) (mark t)))))
1001 (read-string "[Hit return to continue] ")
1002 (save-excursion (kill-buffer "*copy text*")))
1003 (quit (save-excursion (kill-buffer "*copy text*"))
1004 (signal 'quit nil))))))
1006 (goto-char (point-min))
1009 (insert (current-kill 0))))
1012 "Ex delete command."
1013 (vip-default-ex-addresses)
1015 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1016 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1018 (vip-enlarge-region beg end)
1019 (exchange-point-and-mark)
1023 (forward-line (1- ex-count)))
1025 (vip-enlarge-region (point) (mark t))
1027 ;; show text to be deleted and ask for confirmation
1029 (with-output-to-temp-buffer " *delete text*"
1030 (princ (buffer-substring (point) (mark t))))
1032 (read-string "[Hit return to continue] ")
1034 (save-excursion (kill-buffer " *delete text*"))
1036 (save-excursion (kill-buffer " *delete text*")))
1038 (cond ((vip-valid-register ex-buffer '(Letter))
1039 (vip-append-to-register
1040 (downcase ex-buffer) (point) (mark t)))
1041 ((vip-valid-register ex-buffer)
1042 (copy-to-register ex-buffer (point) (mark t) nil))
1043 (t (error vip-InvalidRegister ex-buffer))))
1044 (kill-region (point) (mark t))))))
1048 (defun ex-edit (&optional file)
1050 In Viper, `e' and `e!' behave identically. In both cases, the user is
1051 asked if current buffer should really be discarded.
1052 This command can take multiple file names. It replaces the current buffer
1053 with the first file in its argument list."
1056 (cond ((and (string= ex-file "") buffer-file-name)
1057 (setq ex-file (abbreviate-file-name (buffer-file-name))))
1058 ((string= ex-file "")
1059 (error vip-NoFileSpecified)))
1062 (if buffer-file-name
1063 (cond ((buffer-modified-p)
1065 (format "Buffer %s is modified. Edit buffer? "
1068 ((not (verify-visited-file-modtime (current-buffer)))
1070 (format "File %s changed on disk. Reread from disk? "
1073 (t (setq do-edit nil))))
1076 (if (yes-or-no-p msg)
1078 (set-buffer-modified-p nil)
1079 (kill-buffer (current-buffer)))
1080 (message "Buffer %s was left intact" (buffer-name))))
1083 (if (null (setq file (get-file-buffer ex-file)))
1085 (ex-find-file ex-file)
1086 (vip-change-state-to-vi)
1087 (goto-char (point-min)))
1088 (switch-to-buffer file))
1091 (save-window-excursion
1092 (set-buffer vip-ex-work-buf)
1093 (delete-region (point-min) (point-max))
1094 (insert ex-offset "\n")
1095 (goto-char (point-min)))
1096 (goto-char (vip-get-ex-address))
1097 (beginning-of-line)))
1098 (ex-fixup-history vip-last-ex-prompt ex-file))
1100 ;; splits the string FILESPEC into substrings separated by newlines `\012'
1101 ;; each line assumed to be a file name. find-file's each file thus obtained.
1102 (defun ex-find-file (filespec)
1103 (let (s f filebuf status)
1104 (if (string-match "[^a-zA-Z0-9_.-/]" filespec)
1107 (set-buffer (get-buffer-create " *ex-tmp*"))
1110 (call-process ex-find-file-shell nil t nil
1111 ex-find-file-shell-options
1113 (format "echo %s | tr ' ' '\\012'" filespec)))
1114 (goto-char (point-min))
1115 ;; Give an error, if no match.
1118 (skip-chars-forward " \t\n\j")
1119 (if (looking-at "echo:")
1120 (vip-forward-word 1))
1123 (buffer-substring (point) (vip-line-pos 'end)))
1128 (setq f (buffer-substring s (point)))
1129 (setq filebuf (find-file-noselect f))
1130 (forward-to-indentation 1))
1132 (setq filebuf (find-file-noselect (setq f filespec))))
1133 (switch-to-buffer filebuf)
1136 (defun ex-global (variant)
1137 "Ex global command."
1138 (let ((gcommand ex-token))
1139 (if (or ex-g-flag ex-g-variant)
1140 (error "`%s' within `global' is not allowed" gcommand)
1148 (error "`%s': Missing regular expression" gcommand)))
1150 (if (string= ex-token "")
1151 (if (null vip-s-string)
1152 (error vip-NoPrevSearch)
1153 (setq ex-g-pat vip-s-string))
1154 (setq ex-g-pat ex-token
1155 vip-s-string ex-token))
1156 (if (null ex-addresses)
1157 (setq ex-addresses (list (point-max) (point-min)))
1158 (vip-default-ex-addresses))
1159 (let ((marks nil) (mark-count 0)
1160 com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1161 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1163 (vip-enlarge-region beg end)
1164 (exchange-point-and-mark)
1165 (let ((cont t) (limit (point-marker)))
1166 (exchange-point-and-mark)
1167 ;; skip the last line if empty
1169 (if (eobp) (vip-backward-char-carefully))
1170 (while (and cont (not (bobp)) (>= (point) limit))
1174 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1175 (if (or (and ex-g-flag found)
1176 (and ex-g-variant (not found)))
1179 (setq mark-count (1+ mark-count))
1180 (setq marks (cons (point-marker) marks)))))
1182 (if (bobp) (setq cont nil)
1185 (save-window-excursion
1186 (set-buffer vip-ex-work-buf)
1187 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1189 (goto-char (car marks))
1191 (setq mark-count (1- mark-count))
1192 (setq marks (cdr marks)))))
1196 (if (null ex-addresses)
1197 (setq ex-addresses (cons (point) nil)))
1198 (push-mark (point) t)
1199 (goto-char (car ex-addresses))
1200 (beginning-of-line))
1202 (defun ex-line (com)
1203 "Ex line commands. COM is join, shift-right or shift-left."
1204 (vip-default-ex-addresses)
1206 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1207 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1209 (vip-enlarge-region beg end)
1210 (exchange-point-and-mark)
1214 (forward-line ex-count)))
1216 ;; show text to be joined and ask for confirmation
1218 (with-output-to-temp-buffer " *text*"
1219 (princ (buffer-substring (point) (mark t))))
1222 (read-string "[Hit return to continue] ")
1223 (ex-line-subr com (point) (mark t)))
1225 (save-excursion (kill-buffer " *text*")))
1226 (ex-line-subr com (point) (mark t)))
1227 (setq point (point)))
1228 (goto-char (1- point))
1229 (beginning-of-line)))
1231 (defun ex-line-subr (com beg end)
1232 (cond ((string= com "join")
1233 (goto-char (min beg end))
1234 (while (and (not (eobp)) (< (point) (max beg end)))
1236 (if (and (<= (point) (max beg end)) (not (eobp)))
1239 (delete-region (point) (1- (point)))
1240 (if (not ex-variant) (fixup-whitespace))))))
1241 ((or (string= com "right") (string= com "left"))
1243 (min beg end) (max beg end)
1244 (if (string= com "right") vip-shift-width (- vip-shift-width)))
1245 (goto-char (max beg end))
1247 (vip-forward-char-carefully))))
1253 (if (null ex-addresses)
1255 (cons (point) nil)))
1256 (save-window-excursion
1257 (set-buffer vip-ex-work-buf)
1258 (skip-chars-forward " \t")
1259 (if (looking-at "[a-z]")
1261 (setq char (following-char))
1263 (skip-chars-forward " \t")
1264 (if (not (looking-at "[\n|]"))
1265 (error "`%s': %s" ex-token vip-SpuriousText)))
1266 (error "`%s' requires a following letter" ex-token)))
1268 (goto-char (car ex-addresses))
1269 (point-to-register (1+ (- char ?a))))))
1273 ;; Alternate file is the file next to the first one in the buffer ring
1274 (defun ex-next (cycle-other-window &optional find-alt-file)
1277 (if (not find-alt-file)
1280 (if (or (char-or-string-p ex-offset)
1281 (and (not (string= "" ex-file))
1282 (not (string-match "^[0-9]+$" ex-file))))
1285 (throw 'ex-edit nil))
1286 (setq count (string-to-int ex-file))
1287 (if (= count 0) (setq count 1))
1288 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1290 (setq l (vip-undisplayed-files))
1292 (while (and (not (null l)) (null (car l)))
1294 (setq count (1- count))
1297 (if find-alt-file (car l)
1300 (let* ((w (if cycle-other-window
1301 (get-lru-window) (selected-window)))
1302 (b (window-buffer w)))
1303 (set-window-buffer w (get-file-buffer (car l)))
1305 ;; this puts "next <count>" in the ex-command history
1306 (ex-fixup-history vip-last-ex-prompt ex-file))
1307 (error "Not that many undisplayed files")))))))
1310 (defun ex-next-related-buffer (direction &optional no-recursion)
1312 (vip-ring-rotate1 vip-related-files-and-buffers-ring direction)
1314 (let ((file-or-buffer-name
1315 (vip-current-ring-item vip-related-files-and-buffers-ring))
1316 (old-ring vip-related-files-and-buffers-ring)
1317 (old-win (selected-window))
1320 (or (and (ring-p vip-related-files-and-buffers-ring)
1321 (> (ring-length vip-related-files-and-buffers-ring) 0))
1322 (error "This buffer has no related files or buffers"))
1324 (or (stringp file-or-buffer-name)
1326 "File and buffer names must be strings, %S" file-or-buffer-name))
1328 (setq buf (cond ((get-buffer file-or-buffer-name))
1329 ((file-exists-p file-or-buffer-name)
1330 (find-file-noselect file-or-buffer-name))
1333 (if (not (vip-buffer-live-p buf))
1334 (error "Didn't find buffer %S or file %S"
1336 (abbreviate-file-name (expand-file-name file-or-buffer-name))))
1338 (if (equal buf (current-buffer))
1342 (ex-next-related-buffer direction 'norecursion)))
1347 (if (setq wind (vip-get-visible-buffer-window buf))
1349 (setq wind (get-lru-window (if vip-xemacs-p nil 'visible)))
1350 (set-window-buffer wind buf))
1354 (vip-raise-frame (vip-window-frame wind))
1355 (if (equal (vip-window-frame wind) (vip-window-frame old-win))
1356 (save-window-excursion (select-window wind) (sit-for 1))
1357 (select-window wind)))
1358 (save-window-excursion (select-window wind) (sit-for 1)))
1362 (setq vip-related-files-and-buffers-ring old-ring))
1364 (setq vip-local-search-start-marker (point-marker))
1368 (defun ex-preserve ()
1370 (message "Autosaving all buffers that need to be saved...")
1375 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1377 (setq vip-use-register ex-buffer)
1379 (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
1382 "Ex print working directory."
1383 (message default-directory))
1387 (if (< vip-expert-level 3)
1388 (save-buffers-kill-emacs)
1389 (kill-buffer (current-buffer))))
1395 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1397 (vip-add-newline-at-eob-if-necessary)
1398 (if (not (or (bobp) (eobp))) (forward-line 1))
1399 (if (and (not ex-variant) (string= ex-file ""))
1401 (if (null buffer-file-name)
1402 (error vip-NoFileSpecified))
1403 (setq ex-file buffer-file-name)))
1405 (shell-command ex-file t)
1406 (insert-file-contents ex-file)))
1407 (ex-fixup-history vip-last-ex-prompt ex-file))
1409 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1410 (defun ex-fixup-history (&rest args)
1411 (setq vip-ex-history
1412 (cons (mapconcat 'identity args " ") (cdr vip-ex-history))))
1415 (defun ex-recover ()
1416 "Ex recover from emacs \#file\#."
1418 (if (or ex-append ex-offset)
1419 (error "`recover': %s" vip-SpuriousText))
1420 (if (string= ex-file "")
1422 (if (null buffer-file-name)
1423 (error "This buffer isn't visiting any file"))
1424 (setq ex-file buffer-file-name))
1425 (setq ex-file (expand-file-name ex-file)))
1426 (if (and (not (string= ex-file (buffer-file-name)))
1429 (error "No write since last change \(:rec! overrides\)"))
1430 (recover-file ex-file))
1433 "Tell that `rewind' is obsolete and that one should use `:next count'"
1435 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1438 ;; read variable name for ex-set
1439 (defun ex-set-read-variable ()
1440 (let ((minibuffer-local-completion-map
1441 (copy-keymap minibuffer-local-completion-map))
1442 (cursor-in-echo-area t)
1445 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1446 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1447 (if (vip-set-unread-command-events
1448 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1451 (vip-set-unread-command-events ?\C-m)))
1452 (message ":set <Variable> [= <Value>]")
1453 (or batch (sit-for 2))
1455 (while (string-match "^[ \\t\\n]*$"
1457 (completing-read ":set " ex-variable-alist)))
1458 (message ":set <Variable> ")
1459 ;; if there are unread events, don't wait
1460 (or (vip-set-unread-command-events "") (sit-for 2))
1466 (let ((var (ex-set-read-variable))
1470 (auto-cmd-label "; don't touch or else...")
1471 (delete-turn-on-auto-fill-pattern
1472 "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1473 actual-lisp-cmd lisp-cmd-del-pattern
1476 (cond ((member var '("ai" "autoindent"))
1477 (setq var "vip-auto-indent"
1479 ((member var '("noai" "noautoindent"))
1480 (setq var "vip-auto-indent"
1482 ((member var '("ic" "ignorecase"))
1483 (setq var "vip-case-fold-search"
1485 ((member var '("noic" "noignorecase"))
1486 (setq var "vip-case-fold-search"
1488 ((member var '("ma" "magic"))
1489 (setq var "vip-re-search"
1491 ((member var '("noma" "nomagic"))
1492 (setq var "vip-re-search"
1494 ((member var '("ro" "readonly"))
1495 (setq var "buffer-read-only"
1497 ((member var '("noro" "noreadonly"))
1498 (setq var "buffer-read-only"
1500 ((member var '("sm" "showmatch"))
1501 (setq var "blink-matching-paren"
1503 ((member var '("nosm" "noshowmatch"))
1504 (setq var "blink-matching-paren"
1506 ((member var '("ws" "wrapscan"))
1507 (setq var "vip-search-wrap-around-t"
1509 ((member var '("nows" "nowrapscan"))
1510 (setq var "vip-search-wrap-around-t"
1512 (if (eq val 0) ; value must be set by the user
1513 (let ((cursor-in-echo-area t))
1514 (message (format ":set %s = <Value>" var))
1515 ;; if there are unread events, don't wait
1516 (or (vip-set-unread-command-events "") (sit-for 2))
1517 (setq val (read-string (format ":set %s = " var)))
1518 (ex-fixup-history "set" orig-var val)
1520 ;; check numerical values
1522 '("sw" "shiftwidth" "ts" "tabstop" "wm" "wrapmargin"))
1524 (or (numberp (setq val2 (car (read-from-string val))))
1525 (error "%s: Invalid value, numberp, %S" var val))
1527 (error "%s: Invalid value, numberp, %S" var val))))
1530 ((member var '("sw" "shiftwidth"))
1531 (setq var "vip-shift-width"))
1532 ((member var '("ts" "tabstop"))
1533 ;; make it take effect in curr buff and new bufs
1534 (kill-local-variable 'tab-width)
1535 (setq var "tab-width"
1536 set-cmd "setq-default"))
1537 ((member var '("tsl" "tab-stop-local"))
1538 (setq var "tab-width"
1541 ((member var '("wm" "wrapmargin"))
1542 ;; make it take effect in curr buff and new bufs
1543 (kill-local-variable 'fill-column)
1544 (setq var "fill-column"
1545 val (format "(- (window-width) %s)" val)
1546 set-cmd "setq-default"))
1547 ((member var '("sh" "shell"))
1548 (setq var "explicit-shell-file-name"
1549 val (format "\"%s\"" val)))))
1550 (ex-fixup-history "set" orig-var))
1552 (setq actual-lisp-cmd (format "\n(%s %s %s) %s"
1553 set-cmd var val auto-cmd-label))
1554 (setq lisp-cmd-del-pattern
1555 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1556 set-cmd var auto-cmd-label))
1558 (if (and ask-if-save
1559 (y-or-n-p (format "Do you want to save this setting in %s "
1560 vip-custom-file-name)))
1562 (vip-save-string-in-file
1563 actual-lisp-cmd vip-custom-file-name
1565 lisp-cmd-del-pattern)
1566 (if (string= var "fill-column")
1568 (vip-save-string-in-file
1570 "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
1572 vip-custom-file-name
1573 delete-turn-on-auto-fill-pattern)
1574 (vip-save-string-in-file
1575 nil vip-custom-file-name delete-turn-on-auto-fill-pattern)
1576 (vip-save-string-in-file
1577 nil vip-custom-file-name
1579 lisp-cmd-del-pattern)
1583 (message (format "%s %s %s" set-cmd var (if (string-match "^[ \t]*$" val)
1586 (eval (car (read-from-string actual-lisp-cmd)))
1587 (if (string= var "fill-column")
1590 (auto-fill-mode -1)))
1594 ;; In inline args, skip regex-forw and (optionally) chars-back.
1595 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1597 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1599 (set-buffer vip-ex-work-buf)
1600 (goto-char (point-min))
1601 (re-search-forward regex-forw nil t)
1604 (goto-char (point-max))
1606 (skip-chars-backward chars-back)
1607 (skip-chars-backward " \t\n\C-m"))
1609 ;; replace SPC with `=' to suppress the special meaning SPC has
1613 (while (re-search-forward " +" nil t)
1614 (replace-match replace-str nil t)
1615 (vip-forward-char-carefully)))
1617 (buffer-substring beg end))))
1625 "Viper help. Invokes Info."
1628 (pop-to-buffer (get-buffer-create "*info*"))
1629 (info vip-info-file-name)
1630 (message "Type `i' to search for a specific topic"))
1632 (with-output-to-temp-buffer " *vip-info*"
1633 (princ "The Info file for Viper does not seem to be installed.
1635 This file is part of the distribution of Viper. If you do not
1636 have the full distribution, please obtain it from the `anonymous'
1637 FTP account at `archive.cis.ohio-state.edu':
1639 /pub/gnu/emacs/elisp-archive/modes/viper.shar
1641 The Info files for Viper should be installed as <name>, <name>-1, etc.,
1642 where <name> is the value of `vip-info-file-name'.")))))
1645 "Ex source command. Loads the file specified as argument or `~/.vip'."
1647 (if (string= ex-file "")
1648 (load vip-custom-file-name)
1651 (defun ex-substitute (&optional repeat r-flag)
1652 "Ex substitute command.
1653 If REPEAT use previous regexp which is ex-reg-exp or vip-s-string"
1657 (case-fold-search vip-case-fold-search)
1659 (if repeat (setq ex-token nil) (setq delim (vip-get-ex-pat)))
1662 (setq pat (if r-flag vip-s-string ex-reg-exp))
1664 (error "No previous pattern to use in substitution"))
1666 delim (string-to-char pat)))
1667 (setq pat (if (string= ex-token "") vip-s-string ex-token))
1668 (setq vip-s-string pat
1670 (setq delim (vip-get-ex-pat))
1676 (while (vip-get-ex-opt-gc delim)
1677 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1681 (if ex-addresses (goto-char (car ex-addresses)))
1683 (forward-line (1- ex-count))
1684 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1685 (if (null ex-addresses)
1686 (setq ex-addresses (cons (point) (cons (point) nil)))
1687 (if (null (cdr ex-addresses))
1688 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1690 (let ((beg (car ex-addresses))
1691 (end (car (cdr ex-addresses)))
1694 (vip-enlarge-region beg end)
1695 (let ((limit (save-excursion
1696 (goto-char (max (point) (mark t)))
1698 (goto-char (min (point) (mark t)))
1699 (while (< (point) limit)
1701 (setq eol-mark (point-marker))
1705 (while (and (not (eolp))
1706 (re-search-forward pat eol-mark t))
1707 (if (or (not opt-c) (y-or-n-p "Replace? "))
1709 (setq matched-pos (point))
1710 (if (not (stringp repl))
1711 (error "Can't perform Ex substitution: No previous replacement pattern"))
1712 (replace-match repl t t))))
1714 (vip-forward-char-carefully))
1717 "Can't repeat Ex substitution: No previous regular expression"))
1718 (if (and (re-search-forward pat eol-mark t)
1719 (or (not opt-c) (y-or-n-p "Replace? ")))
1721 (setq matched-pos (point))
1722 (if (not (stringp repl))
1723 (error "Can't perform Ex substitution: No previous replacement pattern"))
1724 (replace-match repl t t)))
1726 (vip-forward-char-carefully))))))
1727 (if matched-pos (goto-char matched-pos))
1729 (if opt-c (message "done"))))
1734 (save-window-excursion
1735 (set-buffer vip-ex-work-buf)
1736 (skip-chars-forward " \t")
1738 (skip-chars-forward "^ |\t\n")
1739 (setq tag (buffer-substring (mark t) (point))))
1740 (if (not (string= tag "")) (setq ex-tag tag))
1741 (vip-change-state-to-emacs)
1742 (condition-case conds
1744 (if (string= tag "")
1746 (find-tag-other-window ex-tag))
1747 (vip-change-state-to-vi))
1749 (vip-change-state-to-vi)
1750 (vip-message-conditions conds)))))
1752 (defun ex-write (q-flag)
1754 (vip-default-ex-addresses t)
1756 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))
1757 temp-buf writing-same-file region
1758 file-exists writing-whole-file)
1759 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1762 (vip-enlarge-region beg end)
1763 (shell-command-on-region (point) (mark t) ex-file))
1764 (if (and (string= ex-file "") (not (buffer-file-name)))
1767 (format "Buffer %s isn't visiting any file. File to save in: "
1770 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
1771 ex-file (if (string= ex-file "")
1773 (expand-file-name ex-file))
1774 file-exists (file-exists-p ex-file)
1775 writing-same-file (string= ex-file (buffer-file-name)))
1776 (if (and writing-whole-file writing-same-file)
1777 (if (not (buffer-modified-p))
1778 (message "(No changes need to be saved)")
1780 (ex-write-info file-exists ex-file beg end))
1781 ;; writing some other file or portion of the currents
1782 ;; file---create temp buffer for it
1783 ;; disable undo in that buffer, for efficiency
1784 (buffer-disable-undo (setq temp-buf (create-file-buffer ex-file)))
1787 (if (and file-exists
1788 (not writing-same-file)
1790 (format "File %s exists. Overwrite? " ex-file))))
1792 (vip-enlarge-region beg end)
1793 (setq region (buffer-substring (point) (mark t)))
1794 (set-buffer temp-buf)
1795 (set-visited-file-name ex-file)
1797 (if (and file-exists ex-append)
1798 (insert-file-contents ex-file))
1799 (goto-char (point-max))
1802 (ex-write-info file-exists ex-file (point-min) (point-max))
1804 (set-buffer temp-buf)
1805 (set-buffer-modified-p nil)
1806 (kill-buffer temp-buf)
1809 ;; this prevents the loss of data if writing part of the buffer
1810 (if (and (buffer-file-name) writing-same-file)
1811 (set-visited-file-modtime))
1812 (or writing-whole-file
1813 (not writing-same-file)
1814 (set-buffer-modified-p t))
1816 (if (< vip-expert-level 2)
1817 (save-buffers-kill-emacs)
1818 (kill-buffer (current-buffer))))
1822 (defun ex-write-info (exists file-name beg end)
1823 (message "`%s'%s %d lines, %d characters"
1824 (abbreviate-file-name file-name)
1825 (if exists "" " [New file]")
1826 (count-lines beg (min (1+ end) (point-max)))
1831 (vip-default-ex-addresses)
1833 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1834 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1836 (vip-enlarge-region beg end)
1837 (exchange-point-and-mark)
1838 (if (or ex-g-flag ex-g-variant)
1839 (error "Can't execute `yank' within `global'"))
1843 (forward-line (1- ex-count)))
1845 (vip-enlarge-region (point) (mark t))
1846 (if ex-flag (error "`yank': %s" vip-SpuriousText))
1848 (cond ((vip-valid-register ex-buffer '(Letter))
1849 (vip-append-to-register
1850 (downcase ex-buffer) (point) (mark t)))
1851 ((vip-valid-register ex-buffer)
1852 (copy-to-register ex-buffer (point) (mark t) nil))
1853 (t (error vip-InvalidRegister ex-buffer))))
1854 (copy-region-as-kill (point) (mark t)))))
1856 (defun ex-command ()
1857 "Execute shell command."
1859 (save-window-excursion
1860 (set-buffer vip-ex-work-buf)
1861 (skip-chars-forward " \t")
1862 (setq command (buffer-substring (point) (point-max)))
1864 (setq command (ex-expand-filsyms command (current-buffer)))
1865 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
1866 (if vip-ex-last-shell-com
1867 (setq command (concat vip-ex-last-shell-com (substring command 1)))
1868 (error "No previous shell command")))
1869 (setq vip-ex-last-shell-com command)
1870 (if (null ex-addresses)
1871 (shell-command command)
1872 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1873 (if (null beg) (setq beg end))
1877 (vip-enlarge-region (point) (mark t))
1878 (shell-command-on-region (point) (mark t) command t))
1881 (defun ex-line-no ()
1882 "Print line number."
1886 (if (null ex-addresses) (point-max) (car ex-addresses))))))
1888 (defun vip-info-on-file ()
1889 "Give information on the file visited by the current buffer."
1892 (setq file (if (buffer-file-name)
1893 (concat (abbreviate-file-name (buffer-file-name)) ":")
1894 (concat (buffer-name) " [Not visiting any file]:"))
1895 info (format "line=%d/%d pos=%d/%d col=%d %s"
1896 (count-lines (point-min) (vip-line-pos 'end))
1897 (count-lines (point-min) (point-max))
1898 (point) (1- (point-max))
1899 (1+ (current-column))
1900 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
1901 (if (< (+ 1 (length info) (length file))
1902 (window-width (minibuffer-window)))
1903 (message (concat file " " info))
1904 (save-window-excursion
1905 (with-output-to-temp-buffer " *vip-info*"
1908 "\n\n\nPress any key to continue...\n\n")))
1915 ;;; viper-ex.el ends here