1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
29 (defvar read-file-name-map)
30 (defvar viper-use-register)
31 (defvar viper-s-string)
32 (defvar viper-shift-width)
33 (defvar viper-ex-history)
34 (defvar viper-related-files-and-buffers-ring)
35 (defvar viper-local-search-start-marker)
36 (defvar viper-expert-level)
37 (defvar viper-custom-file-name)
38 (defvar viper-case-fold-search)
39 (defvar explicit-shell-file-name)
41 ;; loading happens only in non-interactive compilation
42 ;; in order to spare non-viperized emacs from being viperized
45 (let ((load-path (cons (expand-file-name ".") load-path)))
46 (or (featurep 'viper-util)
47 (load "viper-util.el" nil nil 'nosuffix))
48 (or (featurep 'viper-keym)
49 (load "viper-keym.el" nil nil 'nosuffix))
50 (or (featurep 'viper-cmd)
51 (load "viper-cmd.el" nil nil 'nosuffix))
57 (defgroup viper-ex nil
58 "Viper support for Ex commands"
66 (defconst viper-ex-work-buf-name " *ex-working-space*")
67 (defconst viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
68 (defconst viper-ex-tmp-buf-name " *ex-tmp*")
73 (defun ex-cmd-obsolete (name)
74 (error "`%s': Obsolete command, not supported by Viper" name))
76 (defun ex-cmd-not-yet (name)
77 (error "`%s': Command not implemented in Viper" name))
79 ;; alist entries: name (in any order), command, cont(??)
80 ;; If command is a string, then that is an alias to the real command
81 ;; to execute (for instance, ":m" -> ":move").
82 ;; command attributes:
83 ;; is-mashed: the command's args may be jammed right up against the command
84 ;; one-letter: this is a one-letter token. Any text appearing after
85 ;; the name gets appended as an argument for the command
86 ;; i.e. ":kabc" gets turned into (ex-mark "abc")
87 (defconst ex-token-alist '(
89 ("&" (ex-substitute t))
91 (">" (ex-line "right"))
92 ("<" (ex-line "left"))
93 ("Buffer" (if ex-cycle-other-window
94 (viper-switch-to-buffer)
95 (viper-switch-to-buffer-other-window)))
96 ("Next" (ex-next (not ex-cycle-other-window)))
97 ("PreviousRelatedFile" (ex-next-related-buffer -1))
98 ("RelatedFile" (ex-next-related-buffer 1))
100 ("WWrite" (save-some-buffers t))
101 ("Write" (save-some-buffers))
104 ("buffer" (if ex-cycle-other-window
105 (viper-switch-to-buffer-other-window)
106 (viper-switch-to-buffer)))
108 ;; ch should be "change" but maintain old viper compatibility
112 ("copy" (ex-copy nil))
113 ("customize" (customize-group "viper"))
114 ("delete" (ex-delete))
116 ("file" (viper-info-on-file))
118 ("global" (ex-global nil) is-mashed)
121 ("join" (ex-line "join"))
122 ("k" (ex-mark) one-letter)
125 ; old viper doesn't specify a default for "ma" so leave it undefined
129 ("next" (ex-next ex-cycle-other-window))
131 ("preserve" (ex-preserve))
138 ("recover" (ex-recover))
139 ("rewind" (ex-rewind))
145 ("source" (ex-source))
146 ("stop" (suspend-emacs))
147 ("sr" (ex-substitute t t))
148 ("submitReport" (viper-submit-report))
149 ("substitute" (ex-substitute) is-mashed)
150 ("suspend" (suspend-emacs))
153 ("transfer" (ex-copy nil))
156 ("undo" (viper-undo))
159 ("version" (viper-version))
160 ("vglobal" (ex-global t) is-mashed)
164 ("write" (ex-write nil))
167 ("~" (ex-substitute t t))
169 ("append" (ex-cmd-obsolete "append"))
170 ("change" (ex-cmd-obsolete "change"))
171 ("insert" (ex-cmd-obsolete "insert"))
172 ("open" (ex-cmd-obsolete "open"))
174 ("list" (ex-cmd-not-yet "list"))
175 ("print" (ex-cmd-not-yet "print"))
176 ("z" (ex-cmd-not-yet "z"))
177 ("#" (ex-cmd-not-yet "#"))
179 ("abbreviate" (error "`%s': Vi abbreviations are obsolete. Use the more powerful Emacs abbrevs" ex-token))
180 ("unabbreviate" (error "`%s': Vi abbreviations are obsolete. Use the more powerful Emacs abbrevs" ex-token))
183 ;; No code should touch anything in the alist entry! (other than the name,
184 ;; "car entry", of course) This way, changing this data structure
185 ;; requires changing only the following ex-cmd functions...
187 ;; Returns cmd if the command may be jammed right up against its
188 ;; arguments, nil if there must be a space.
189 ;; examples of mashable commands: g// g!// v// s// sno// sm//
190 (defun ex-cmd-is-mashed-with-args (cmd)
191 (if (eq 'is-mashed (car (nthcdr 2 cmd))) cmd))
193 ;; Returns true if this is a one-letter command that may be followed
194 ;; by anything, no whitespace needed. This is a special-case for ":k".
195 (defun ex-cmd-is-one-letter (cmd)
196 (if (eq 'one-letter (car (nthcdr 2 cmd))) cmd))
198 ;; Executes the function associated with the command
199 (defun ex-cmd-execute (cmd)
202 ;; If this is a one-letter magic command, splice in args.
203 (defun ex-splice-args-in-1-letr-cmd (key list)
204 (let ((onelet (ex-cmd-is-one-letter (assoc (substring key 0 1) list))))
207 (append (cadr onelet)
208 (if (< 1 (length key)) (list (substring key 1))))
213 ;; Returns the alist entry for the appropriate key.
214 ;; Tries to complete the key before using it in the alist.
215 ;; If there is no appropriate key (no match or duplicate matches) return nil
216 (defun ex-cmd-assoc (key list)
217 (let ((entry (try-completion key list))
220 ((eq entry t) (assoc key list))
221 ((stringp entry) (or (ex-splice-args-in-1-letr-cmd key list)
223 ((eq entry nil) (ex-splice-args-in-1-letr-cmd key list))
226 ;; If we end up with an alias, look up the alias...
227 (if (stringp (cadr result))
228 (setq result (ex-cmd-assoc (cadr result) list)))
229 ;; and return the corresponding alist entry
234 ;; A-list of Ex variables that can be set using the :set command.
235 (defconst ex-variable-alist
236 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
237 ("tabstop-global") ("ts-g") ("tabstop") ("ts")
238 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
240 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
241 ("noreadonly") ("noro") ("nomagic") ("noma")
242 ("noignorecase") ("noic")
243 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai")
244 ("magic") ("ma") ("ignorecase") ("ic")
245 ("autoindent-global") ("ai-g") ("autoindent") ("ai")
251 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
252 (defvar ex-token nil)
255 ;; If non-nil, gives type of address; if nil, it is a command.
256 (defvar ex-token-type nil)
258 ;; List of addresses passed to Ex command
259 (defvar ex-addresses nil)
261 ;; This flag is supposed to be set only by `#', `print', and `list',
262 ;; none of which is implemented. So, it and the pices of the code it
263 ;; controls are dead weight. We keep it just in case this might be
264 ;; needed in the future.
267 ;; "buffer" where Ex commands keep deleted data.
268 ;; In Emacs terms, this is a register.
269 (defvar ex-buffer nil)
271 ;; Value of ex count.
272 (defvar ex-count nil)
274 ;; Flag indicating that :global Ex command is being executed.
275 (defvar ex-g-flag nil)
276 ;; Flag indicating that :vglobal Ex command is being executed.
277 (defvar ex-g-variant nil)
279 ;; Save reg-exp used in substitute.
280 (defvar ex-reg-exp nil)
283 ;; Replace pattern for substitute.
286 ;; Pattern for global command.
287 (defvar ex-g-pat nil)
289 (defcustom ex-unix-type-shell
290 (let ((case-fold-search t))
291 (and (stringp shell-file-name)
301 "[^a-z]sh$\\|[^a-z]sh.exe$"
306 "Is the user using a unix-type shell under a non-OS?"
310 (defcustom ex-unix-type-shell-options
311 (let ((case-fold-search t))
312 (if ex-unix-type-shell
313 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name)
314 "-f") ; csh: do it fast
315 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name)
316 "-noprofile") ; bash: ignore .profile
318 "Options to pass to the Unix-style shell.
319 Don't put `-c' here, as it is added automatically."
320 :type '(choice (const nil) string)
323 (defcustom viper-glob-function
324 (cond (ex-unix-type-shell 'viper-glob-unix-files)
325 ((eq system-type 'emx) 'viper-glob-mswindows-files) ; OS/2
326 (viper-ms-style-os-p 'viper-glob-mswindows-files) ; Microsoft OS
327 (viper-vms-os-p 'viper-glob-unix-files) ; VMS
328 (t 'viper-glob-unix-files) ; presumably UNIX
330 "Expand the file spec containing wildcard symbols.
331 The default tries to set this variable to work with Unix, Windows,
334 However, if it doesn't work right for some types of Unix shells or some OS,
335 the user should supply the appropriate function and set this variable to the
336 corresponding function symbol."
341 ;; Remembers the previous Ex tag.
344 ;; file used by Ex commands like :r, :w, :n
347 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
348 (defvar ex-variant nil)
350 ;; Specified the offset of an Ex command, such as :read.
351 (defvar ex-offset nil)
353 ;; Tells Ex that this is a w>> command.
354 (defvar ex-append nil)
356 ;; File containing the shell command to be executed at Ex prompt,
358 (defvar ex-cmdfile nil)
359 (defvar ex-cmdfile-args "")
361 ;; flag used in viper-ex-read-file-name to indicate that we may be reading
362 ;; multiple file names. Used for :edit and :next
363 (defvar viper-keep-reading-filename nil)
365 (defcustom ex-cycle-other-window t
366 "*If t, :n and :b cycles through files and buffers in other window.
367 Then :N and :B cycles in the current window. If nil, this behavior is
372 (defcustom ex-cycle-through-non-files nil
373 "*Cycle through *scratch* and other buffers that don't visit any file."
377 ;; Last shell command executed with :! command.
378 (defvar viper-ex-last-shell-com nil)
380 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
381 (defvar viper-incomplete-ex-cmd nil)
383 ;; Remembers the last ex-command prompt.
384 (defvar viper-last-ex-prompt "")
387 ;; Get a complete ex command
388 (defun viper-get-ex-com-subr ()
389 (let (cmd case-fold-search)
391 (re-search-forward "[a-zA-Z][a-zA-Z]*")
392 (setq ex-token-type 'command)
393 (setq ex-token (buffer-substring (point) (mark t)))
394 (setq cmd (ex-cmd-assoc ex-token ex-token-alist))
396 (setq ex-token (car cmd))
397 (setq ex-token-type 'non-command))
400 ;; Get an ex-token which is either an address or a command.
401 ;; A token has a type, \(command, address, end-mark\), and a value
402 (defun viper-get-ex-token ()
403 (save-window-excursion
404 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
405 (set-buffer viper-ex-work-buf)
406 (skip-chars-forward " \t|")
407 (let ((case-fold-search t))
408 (cond ((looking-at "#")
409 (setq ex-token-type 'command)
410 (setq ex-token (char-to-string (following-char)))
412 ((looking-at "[a-z]") (viper-get-ex-com-subr))
415 (setq ex-token-type 'dot))
416 ((looking-at "[0-9]")
418 (re-search-forward "[0-9]*")
420 (cond ((eq ex-token-type 'plus) 'add-number)
421 ((eq ex-token-type 'minus) 'sub-number)
424 (string-to-int (buffer-substring (point) (mark t)))))
427 (setq ex-token-type 'end))
430 (setq ex-token-type 'whole))
432 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
436 (setq ex-token-type 'plus))
437 ((looking-at "+[0-9]")
439 (setq ex-token-type 'plus))
441 (error viper-BadAddress))))
443 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
447 (setq ex-token-type 'minus))
448 ((looking-at "-[0-9]")
450 (setq ex-token-type 'minus))
452 (error viper-BadAddress))))
457 (while (and (not (eolp)) cont)
458 ;;(re-search-forward "[^/]*/")
459 (re-search-forward "[^/]*\\(/\\|\n\\)")
460 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
463 (setq ex-token (buffer-substring (point) (mark t)))
464 (if (looking-at "/") (forward-char 1))
465 (setq ex-token-type 'search-forward))
470 (while (and (not (eolp)) cont)
471 ;;(re-search-forward "[^\\?]*\\?")
472 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
473 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
476 (if (not (looking-at "\n")) (forward-char 1))))
477 (setq ex-token-type 'search-backward)
478 (setq ex-token (buffer-substring (1- (point)) (mark t))))
481 (setq ex-token-type 'comma))
484 (setq ex-token-type 'semi-colon))
485 ((looking-at "[!=><&~]")
486 (setq ex-token-type 'command)
487 (setq ex-token (char-to-string (following-char)))
490 (setq ex-token-type 'goto-mark)
492 (cond ((looking-at "'") (setq ex-token nil))
493 ((looking-at "[a-z]") (setq ex-token (following-char)))
494 (t (error "Marks are ' and a-z")))
497 (setq ex-token-type 'end-mark)
498 (setq ex-token "goto"))
500 (error viper-BadExCommand))))))
502 ;; Reads Ex command. Tries to determine if it has to exit because command
503 ;; is complete or invalid. If not, keeps reading command.
504 (defun ex-cmd-read-exit ()
506 (setq viper-incomplete-ex-cmd t)
507 (let ((quit-regex1 (concat
510 "\\|" "[nN]ext[ \t]*"
523 "\\|" "[ktgjmsz][ \t]*$"
525 "\\|" "tr[ansfer \t]*"
528 "\\|" "^[ \t]*k?ma[^p]*"
535 ;; don't jump up in :s command
536 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*su.*"
537 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*s[^a-z].*"
538 "\\|" "['`][a-z][ \t]*"
539 ;; r! assumes that the next one is a shell command
540 "\\|" "\\(r\\|re\\|rea\\|read\\)[ \t]*!"
541 ;; w ! assumes that the next one is a shell command
542 "\\|" "\\(w\\|wr\\|wri\\|writ.?\\)[ \t]+!"
543 "\\|" "![ \t]*[a-zA-Z].*"
547 (save-window-excursion ;; put cursor at the end of the Ex working buffer
548 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
549 (set-buffer viper-ex-work-buf)
550 (goto-char (point-max)))
551 (cond ((viper-looking-back quit-regex1) (exit-minibuffer))
552 ((viper-looking-back stay-regex) (insert " "))
553 ((viper-looking-back quit-regex2) (exit-minibuffer))
556 ;; complete Ex command
557 (defun ex-cmd-complete ()
559 (let (save-pos dist compl-list string-to-complete completion-result)
562 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
566 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
568 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+"))
569 ;; Preceding characters are not the ones allowed in an Ex command
570 ;; or we have typed past command name.
571 ;; Note: we didn't do parsing, so there can be surprises.
572 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
573 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
574 (looking-at "[^ \t\n\C-m]"))
576 (with-output-to-temp-buffer "*Completions*"
577 (display-completion-list
578 (viper-alist-to-list ex-token-alist))))
579 ;; Preceding chars may be part of a command name
580 (setq string-to-complete (buffer-substring save-pos (point)))
581 (setq completion-result
582 (try-completion string-to-complete ex-token-alist))
584 (cond ((eq completion-result t) ; exact match--do nothing
585 (viper-tmp-insert-at-eob " (Sole completion)"))
586 ((eq completion-result nil)
587 (viper-tmp-insert-at-eob " (No match)"))
588 (t ;; partial completion
590 (delete-region (point) (point-max))
591 (insert completion-result)
592 (let (case-fold-search)
594 (viper-filter-alist (concat "^" completion-result)
596 (if (> (length compl-list) 1)
597 (with-output-to-temp-buffer "*Completions*"
598 (display-completion-list
599 (viper-alist-to-list (reverse compl-list)))))))
604 ;; ARG is a prefix argument. If given, the ex command runs on the region
605 ;;(without the user having to specify the address :a,b
606 ;; STRING is the command to execute. If nil, then Viper asks you to enter the
608 (defun viper-ex (arg &optional string)
613 (let* ((map (copy-keymap minibuffer-local-map))
617 reg-beg-line reg-end-line
620 prev-token-type com-str)
621 (viper-add-keymap viper-ex-cmd-map map)
625 (viper-enlarge-region (mark t) (point))
626 (if (> (point) (mark t))
627 (setq reg-beg (mark t)
629 (setq reg-end (mark t)
633 (setq reg-beg-line (1+ (count-lines (point-min) (point)))
635 (+ reg-beg-line (count-lines reg-beg reg-end) -1)))))
637 (setq initial-str (format "%d,%d" reg-beg-line reg-end-line)))
640 (or string (viper-read-string-with-history
644 ;; no default when working on region
647 (car viper-ex-history))
650 " [Type command to execute on current region]"))))
651 (save-window-excursion
653 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
654 (set-buffer viper-ex-work-buf)
655 (delete-region (point-min) (point-max))
656 (insert com-str "\n")
657 (goto-char (point-min)))
658 (setq ex-token-type nil
662 (cond ((memq ex-token-type '(command end-mark))
663 (if address (setq ex-addresses (cons address ex-addresses)))
664 (viper-deactivate-mark)
665 (let ((cmd (ex-cmd-assoc ex-token ex-token-alist)))
667 (error "`%s': %s" ex-token viper-BadExCommand))
669 (if (or (ex-cmd-is-mashed-with-args cmd)
670 (ex-cmd-is-one-letter cmd))
673 (save-window-excursion
674 (setq viper-ex-work-buf
675 (get-buffer-create viper-ex-work-buf-name))
676 (set-buffer viper-ex-work-buf)
677 (skip-chars-forward " \t")
678 (cond ((looking-at "|")
683 "`%s': %s" ex-token viper-SpuriousText)))
686 ((eq ex-token-type 'non-command)
687 (error "`%s': %s" ex-token viper-BadExCommand))
688 ((eq ex-token-type 'whole)
692 (cons (point-max) ex-addresses)
693 (cons (point-max) (cons (point-min) ex-addresses)))))
694 ((eq ex-token-type 'comma)
695 (if (eq prev-token-type 'whole)
696 (setq address (point-min)))
698 (cons (if (null address) (point) address) ex-addresses)))
699 ((eq ex-token-type 'semi-colon)
700 (if (eq prev-token-type 'whole)
701 (setq address (point-min)))
702 (if address (setq dot address))
704 (cons (if (null address) (point) address) ex-addresses)))
705 (t (let ((ans (viper-get-ex-address-subr address dot)))
706 (if ans (setq address ans)))))
707 (setq prev-token-type ex-token-type))))
710 ;; Get a regular expression and set `ex-variant', if found
711 (defun viper-get-ex-pat ()
712 (save-window-excursion
713 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
714 (set-buffer viper-ex-work-buf)
715 (skip-chars-forward " \t")
717 ;; this is probably a variant command r!
719 (setq ex-g-variant (not ex-g-variant)
720 ex-g-flag (not ex-g-flag))
722 (skip-chars-forward " \t")))
723 (let ((c (following-char)))
724 (cond ((string-match "[0-9A-Za-z]" (format "%c" c))
726 "Global regexp must be inside matching non-alphanumeric chars"))
727 ((= c ??) (error "`?' is not an allowed pattern delimiter here")))
728 (if (looking-at "[^\\\\\n]")
733 ;; the use of eobp instead of eolp permits the use of newlines in
734 ;; pat2 in s/pat1/pat2/
735 (while (and (not (eobp)) cont)
736 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
737 (if (member ex-token '("global" "vglobal"))
738 (error "Missing closing delimiter for global regexp")
739 (goto-char (point-max))))
740 (if (not (viper-looking-back
741 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
743 ;; we are at an escaped delimiter: unescape it and continue
744 (delete-backward-char 2)
747 ;; if at eol, exit loop and go to next line
748 ;; later, delim will be inserted at the end
754 (if (= (mark t) (point)) ""
755 (buffer-substring (1- (point)) (mark t))))
757 ;; if the user didn't insert the final pattern delimiter, we're
758 ;; at newline now. In this case, insert the initial delimiter
759 ;; specified in variable c
768 ;; Get an Ex option g or c
769 (defun viper-get-ex-opt-gc (c)
770 (save-window-excursion
771 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
772 (set-buffer viper-ex-work-buf)
773 (if (looking-at (format "%c" c)) (forward-char 1))
774 (skip-chars-forward " \t")
775 (cond ((looking-at "g")
785 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
786 (defun viper-default-ex-addresses (&optional whole-flag)
787 (cond ((null ex-addresses)
790 (list (point-max) (point-min))
791 (list (point) (point)))))
792 ((null (cdr ex-addresses))
794 (cons (car ex-addresses) ex-addresses)))))
796 ;; Get an ex-address as a marker and set ex-flag if a flag is found
797 (defun viper-get-ex-address ()
798 (let ((address (point-marker))
804 (cond ((eq ex-token-type 'command)
805 (if (member ex-token '("print" "list" "#"))
809 (error "Address expected in this Ex command")))
810 ((eq ex-token-type 'end-mark)
812 ((eq ex-token-type 'whole)
813 (error "Trailing address expected"))
814 ((eq ex-token-type 'comma)
815 (error "`%s': %s" ex-token viper-SpuriousText))
816 (t (let ((ans (viper-get-ex-address-subr address (point-marker))))
817 (if ans (setq address ans))))))
820 ;; Returns an address as a point
821 (defun viper-get-ex-address-subr (old-address dot)
823 (if (null old-address) (setq old-address dot))
824 (cond ((eq ex-token-type 'dot)
826 ((eq ex-token-type 'add-number)
828 (goto-char old-address)
829 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
830 (setq address (point-marker))))
831 ((eq ex-token-type 'sub-number)
833 (goto-char old-address)
834 (forward-line (- ex-token))
835 (setq address (point-marker))))
836 ((eq ex-token-type 'abs-number)
838 (goto-char (point-min))
839 (if (= ex-token 0) (setq address 0)
840 (forward-line (1- ex-token))
841 (setq address (point-marker)))))
842 ((eq ex-token-type 'end)
843 (setq address (point-max-marker)))
844 ((eq ex-token-type 'plus) t) ; do nothing
845 ((eq ex-token-type 'minus) t) ; do nothing
846 ((eq ex-token-type 'search-forward)
848 (ex-search-address t)
849 (setq address (point-marker))))
850 ((eq ex-token-type 'search-backward)
852 (ex-search-address nil)
853 (setq address (point-marker))))
854 ((eq ex-token-type 'goto-mark)
857 (exchange-point-and-mark)
858 (goto-char (viper-register-to-point
859 (1+ (- ex-token ?a)) 'enforce-buffer)))
860 (setq address (point-marker)))))
864 ;; Search pattern and set address
865 (defun ex-search-address (forward)
866 (if (string= ex-token "")
867 (if (null viper-s-string)
868 (error viper-NoPrevSearch)
869 (setq ex-token viper-s-string))
870 (setq viper-s-string ex-token))
874 (re-search-forward ex-token))
876 (re-search-backward ex-token)))
878 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
879 (defun viper-get-ex-buffer ()
883 (save-window-excursion
884 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
885 (set-buffer viper-ex-work-buf)
886 (skip-chars-forward " \t")
887 (if (looking-at "[a-zA-Z]")
889 (setq ex-buffer (following-char))
891 (skip-chars-forward " \t")))
892 (if (looking-at "[0-9]")
895 (re-search-forward "[0-9][0-9]*")
896 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
897 (skip-chars-forward " \t")))
898 (if (looking-at "[pl#]")
902 (if (not (looking-at "[\n|]"))
903 (error "`%s': %s" ex-token viper-SpuriousText))))
905 (defun viper-get-ex-count ()
909 (save-window-excursion
910 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
911 (set-buffer viper-ex-work-buf)
912 (skip-chars-forward " \t")
917 (skip-chars-forward " \t")
918 (if (looking-at "[0-9]")
921 (re-search-forward "[0-9][0-9]*")
922 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
923 (skip-chars-forward " \t")))
924 (if (looking-at "[pl#]")
928 (if (not (looking-at "[\n|]"))
931 (point-min) (1- (point-max))) viper-BadExCommand))))
933 ;; Expand \% and \# in ex command
934 (defun ex-expand-filsyms (cmd buf)
938 (setq cf buffer-file-name)
939 (setq pf (ex-next nil t))) ; this finds alternative file name
940 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
941 (error "No current file to substitute for `%%'"))
942 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
943 (error "No alternate file to substitute for `#'"))
945 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
948 (goto-char (point-min))
949 (while (re-search-forward "%\\|#" nil t)
950 (let ((data (match-data))
951 (char (buffer-substring (match-beginning 0) (match-end 0))))
952 (if (viper-looking-back (concat "\\\\" char))
954 (store-match-data data)
955 (if (string= char "%")
957 (replace-match pf)))))
959 (setq ret (buffer-substring (point-min) (point)))
963 ;; Get a file name and set `ex-variant', `ex-append' and `ex-offset' if found
964 ;; If it is r!, then get the command name and whatever args
965 (defun viper-get-ex-file ()
974 (save-window-excursion
975 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
976 (set-buffer viper-ex-work-buf)
977 (skip-chars-forward " \t")
979 (if (and (not (viper-looking-back "[ \t]"))
980 ;; read doesn't have a corresponding :r! form, so ! is
981 ;; immediately interpreted as a shell command.
982 (not (string= ex-token "read")))
986 (skip-chars-forward " \t"))
989 (skip-chars-forward " \t")))
990 (if (looking-at ">>")
995 (skip-chars-forward " \t")))
1000 (re-search-forward "[ \t\n]")
1002 (setq ex-offset (buffer-substring (point) (mark t)))
1004 (skip-chars-forward " \t")))
1005 ;; this takes care of :r, :w, etc., when they get file names
1006 ;; from the history list
1007 (if (member ex-token '("read" "write" "edit" "visual" "next"))
1009 (setq ex-file (buffer-substring (point) (1- (point-max))))
1011 ;; For :e, match multiple non-white strings separated
1012 ;; by white. For others, find the first non-white string
1014 (if (string= ex-token "edit")
1015 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
1019 ;; if file name comes from history, don't leave
1020 ;; minibuffer when the user types space
1021 (setq viper-incomplete-ex-cmd nil)
1022 (setq ex-cmdfile-args
1023 (substring ex-file (match-end 0) nil))
1024 ;; this must be the last clause in this progn
1025 (substring ex-file (match-beginning 0) (match-end 0))
1028 ;; this leaves only the command name in the work area
1029 ;; file names are gone
1030 (delete-region (point) (1- (point-max)))
1032 (goto-char (point-max))
1033 (skip-chars-backward " \t\n")
1034 (setq prompt (buffer-substring (point-min) (point)))
1037 (setq viper-last-ex-prompt prompt)
1039 ;; If we just finished reading command, redisplay prompt
1040 (if viper-incomplete-ex-cmd
1041 (setq ex-file (viper-ex-read-file-name (format ":%s " prompt)))
1042 ;; file was typed in-line
1043 (setq ex-file (or ex-file "")))
1047 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
1048 ;; file names, arranges to re-enter the minibuffer.
1049 (defun viper-complete-filename-or-exit ()
1051 (setq viper-keep-reading-filename t)
1052 ;; don't exit if directory---ex-commands don't
1053 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
1054 ;; apparently the argument to an Ex command is
1055 ;; supposed to be a shell command
1056 ((viper-looking-back "^[ \t]*!.*")
1060 (setq ex-cmdfile nil)
1061 (minibuffer-complete-word))))
1063 (defun viper-handle-! ()
1066 (buffer-string) (viper-abbreviate-file-name default-directory))
1067 (member ex-token '("read" "write")))
1071 (defun ex-cmd-accepts-multiple-files-p (token)
1072 (member token '("edit" "next" "Next")))
1074 ;; Read file name from the minibuffer in an ex command.
1075 ;; If user doesn't enter anything, then "" is returned, i.e., the
1076 ;; prompt-directory is not returned.
1077 (defun viper-ex-read-file-name (prompt)
1079 (minibuffer-local-completion-map
1080 (copy-keymap minibuffer-local-completion-map))
1083 (viper-add-keymap ex-read-filename-map
1085 minibuffer-local-completion-map
1086 read-file-name-map))
1088 (setq cont (setq viper-keep-reading-filename t))
1090 (setq viper-keep-reading-filename nil
1091 val (read-file-name (concat prompt str) nil default-directory))
1092 (setq val (expand-file-name val))
1093 (if (and (string-match " " val)
1094 (ex-cmd-accepts-multiple-files-p ex-token))
1095 (setq val (concat "\"" val "\"")))
1096 (setq str (concat str (if (equal val "") "" " ")
1097 val (if (equal val "") "" " ")))
1099 ;; Only edit, next, and Next commands accept multiple files.
1100 ;; viper-keep-reading-filename is set in the anonymous function that is
1101 ;; bound to " " in ex-read-filename-map.
1102 (setq cont (and viper-keep-reading-filename
1103 (ex-cmd-accepts-multiple-files-p ex-token)))
1106 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
1107 end (string-match "[ \t]*$" str)) ; delete trailing blanks
1108 (if (member ex-token '("read" "write"))
1109 (if (string-match "[\t ]*!" str)
1110 ;; this is actually a shell command
1114 (setq viper-last-ex-prompt
1115 (concat viper-last-ex-prompt " !")))))
1116 (substring str (or beg 0) end)))
1119 (defun viper-undisplayed-files ()
1122 (if (null (get-buffer-window b))
1123 (let ((f (buffer-file-name b)))
1125 (if ex-cycle-through-non-files
1126 (let ((s (buffer-name b)))
1127 (if (string= " " (substring s 0 1))
1136 (let ((l (viper-undisplayed-files))
1139 (while (not (null l))
1141 (setq args (format "%s %d) %s\n" args file-count (car l))
1142 file-count (1+ file-count)))
1144 (if (string= args "")
1145 (message "All files are already displayed")
1147 (save-window-excursion
1148 (with-output-to-temp-buffer " *viper-info*"
1149 (princ "\n\nThese files are not displayed in any window.\n")
1150 (princ "\n=============\n")
1152 (princ "\n=============\n")
1153 (princ "\nThe numbers can be given as counts to :next. ")
1154 (princ "\n\nPress any key to continue...\n\n"))
1155 (viper-read-event))))))
1157 ;; Ex cd command. Default directory of this buffer changes
1160 (if (string= ex-file "")
1162 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
1164 ;; Ex copy and move command. DEL-FLAG means delete
1165 (defun ex-copy (del-flag)
1166 (viper-default-ex-addresses)
1167 (let ((address (viper-get-ex-address))
1168 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1172 (viper-enlarge-region (mark t) (point))
1174 (kill-region (point) (mark t))
1175 (copy-region-as-kill (point) (mark t)))
1178 (with-output-to-temp-buffer " *copy text*"
1180 (if (or del-flag ex-g-flag ex-g-variant)
1182 (buffer-substring (point) (mark t)))))
1185 (read-string "[Hit return to confirm] ")
1186 (save-excursion (kill-buffer " *copy text*")))
1187 (quit (save-excursion (kill-buffer " *copy text*"))
1188 (signal 'quit nil))))))
1190 (goto-char (point-min))
1193 (insert (current-kill 0))))
1195 ;; Ex delete command
1197 (viper-default-ex-addresses)
1198 (viper-get-ex-buffer)
1199 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1200 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1202 (viper-enlarge-region beg end)
1203 (exchange-point-and-mark)
1207 (forward-line (1- ex-count)))
1209 (viper-enlarge-region (point) (mark t))
1211 ;; show text to be deleted and ask for confirmation
1213 (with-output-to-temp-buffer " *delete text*"
1214 (princ (buffer-substring (point) (mark t))))
1216 (read-string "[Hit return to confirm] ")
1218 (save-excursion (kill-buffer " *delete text*"))
1220 (save-excursion (kill-buffer " *delete text*")))
1222 (cond ((viper-valid-register ex-buffer '(Letter))
1223 (viper-append-to-register
1224 (downcase ex-buffer) (point) (mark t)))
1225 ((viper-valid-register ex-buffer)
1226 (copy-to-register ex-buffer (point) (mark t) nil))
1227 (t (error viper-InvalidRegister ex-buffer))))
1228 (kill-region (point) (mark t))))))
1233 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1234 ;; asked if current buffer should really be discarded.
1235 ;; This command can take multiple file names. It replaces the current buffer
1236 ;; with the first file in its argument list
1237 (defun ex-edit (&optional file)
1239 (viper-get-ex-file))
1240 (cond ((and (string= ex-file "") buffer-file-name)
1241 (setq ex-file (viper-abbreviate-file-name (buffer-file-name))))
1242 ((string= ex-file "")
1243 (error viper-NoFileSpecified)))
1245 ;;; (let (msg do-edit)
1246 ;;; (if buffer-file-name
1247 ;;; (cond ((buffer-modified-p)
1249 ;;; (format "Buffer %s is modified. Discard changes? "
1252 ;;; ((not (verify-visited-file-modtime (current-buffer)))
1254 ;;; (format "File %s changed on disk. Reread from disk? "
1255 ;;; buffer-file-name)
1257 ;;; (t (setq do-edit nil))))
1260 ;;; (if (yes-or-no-p msg)
1262 ;;; (set-buffer-modified-p nil)
1263 ;;; (kill-buffer (current-buffer)))
1264 ;;; (message "Buffer %s was left intact" (buffer-name))))
1267 (if (null (setq file (get-file-buffer ex-file)))
1269 ;; this also does shell-style globbing
1271 ;; replace # and % with the previous/current file
1272 (ex-expand-filsyms ex-file (current-buffer)))
1273 (or (eq major-mode 'dired-mode)
1274 (viper-change-state-to-vi))
1275 (goto-char (point-min)))
1276 (switch-to-buffer file))
1279 (save-window-excursion
1280 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1281 (set-buffer viper-ex-work-buf)
1282 (delete-region (point-min) (point-max))
1283 (insert ex-offset "\n")
1284 (goto-char (point-min)))
1285 (goto-char (viper-get-ex-address))
1286 (beginning-of-line)))
1287 (ex-fixup-history viper-last-ex-prompt ex-file))
1289 ;; Find-file FILESPEC if it appears to specify a single file.
1290 ;; Otherwise, assume that FILESPEC is a wildcard.
1291 ;; In this case, split it into substrings separated by newlines.
1292 ;; Each line is assumed to be a file name.
1293 (defun ex-find-file (filespec)
1294 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1295 (cond ((file-exists-p filespec) (find-file filespec))
1296 ((string-match nonstandard-filename-chars filespec)
1297 (mapcar 'find-file (funcall viper-glob-function filespec)))
1298 (t (find-file filespec)))
1302 ;; Ex global command
1303 ;; This is executed in response to:
1304 ;; :global "pattern" ex-command
1305 ;; :vglobal "pattern" ex-command
1306 ;; :global executes ex-command on all lines matching <pattern>
1307 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1309 ;; With VARIANT nil, this functions executes :global
1310 ;; With VARIANT t, executes :vglobal
1311 (defun ex-global (variant)
1312 (let ((gcommand ex-token))
1313 (if (or ex-g-flag ex-g-variant)
1314 (error "`%s' within `global' is not allowed" gcommand)
1322 (error "`%s': Missing regular expression" gcommand)))
1324 (if (string= ex-token "")
1325 (if (null viper-s-string)
1326 (error viper-NoPrevSearch)
1327 (setq ex-g-pat viper-s-string))
1328 (setq ex-g-pat ex-token
1329 viper-s-string ex-token))
1330 (if (null ex-addresses)
1331 (setq ex-addresses (list (point-max) (point-min)))
1332 (viper-default-ex-addresses))
1335 (end (car ex-addresses))
1336 (beg (car (cdr ex-addresses)))
1338 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1340 (viper-enlarge-region beg end)
1341 (exchange-point-and-mark)
1342 (let ((cont t) (limit (point-marker)))
1343 (exchange-point-and-mark)
1344 ;; skip the last line if empty
1346 (if (eobp) (viper-backward-char-carefully))
1347 (while (and cont (not (bobp)) (>= (point) limit))
1351 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1352 (if (or (and ex-g-flag found)
1353 (and ex-g-variant (not found)))
1356 (setq mark-count (1+ mark-count))
1357 (setq marks (cons (point-marker) marks)))))
1359 (if (bobp) (setq cont nil)
1362 (save-window-excursion
1363 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1364 (set-buffer viper-ex-work-buf)
1365 ;; com-str is the command string, i.e., g/pattern/ or v/pattern'
1366 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1368 (goto-char (car marks))
1369 (viper-ex nil com-str)
1370 (setq mark-count (1- mark-count))
1371 (setq marks (cdr marks)))))
1375 (if (null ex-addresses)
1376 (setq ex-addresses (cons (point) nil)))
1377 (push-mark (point) t)
1378 (goto-char (car ex-addresses))
1382 ;; Ex line commands. COM is join, shift-right or shift-left
1383 (defun ex-line (com)
1384 (viper-default-ex-addresses)
1385 (viper-get-ex-count)
1386 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1387 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1389 (viper-enlarge-region beg end)
1390 (exchange-point-and-mark)
1394 (forward-line ex-count)))
1396 ;; show text to be joined and ask for confirmation
1398 (with-output-to-temp-buffer " *join text*"
1399 (princ (buffer-substring (point) (mark t))))
1402 (read-string "[Hit return to confirm] ")
1403 (ex-line-subr com (point) (mark t)))
1405 (save-excursion (kill-buffer " *join text*")))
1406 (ex-line-subr com (point) (mark t)))
1407 (setq point (point)))
1408 (goto-char (1- point))
1409 (beginning-of-line)))
1411 (defun ex-line-subr (com beg end)
1412 (cond ((string= com "join")
1413 (goto-char (min beg end))
1414 (while (and (not (eobp)) (< (point) (max beg end)))
1416 (if (and (<= (point) (max beg end)) (not (eobp)))
1419 (delete-region (point) (1- (point)))
1420 (if (not ex-variant) (fixup-whitespace))))))
1421 ((or (string= com "right") (string= com "left"))
1423 (min beg end) (max beg end)
1424 (if (string= com "right") viper-shift-width (- viper-shift-width)))
1425 (goto-char (max beg end))
1427 (viper-forward-char-carefully))))
1431 ;; Sets the mark to the current point.
1432 ;; If name is omitted, get the name straight from the work buffer."
1433 (defun ex-mark (&optional name)
1435 (if (null ex-addresses)
1437 (cons (point) nil)))
1439 (if (eq 1 (length name))
1440 (setq char (string-to-char name))
1441 (error "`%s': Spurious text \"%s\" after mark name"
1442 name (substring name 1) viper-SpuriousText))
1443 (save-window-excursion
1444 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1445 (set-buffer viper-ex-work-buf)
1446 (skip-chars-forward " \t")
1447 (if (looking-at "[a-z]")
1449 (setq char (following-char))
1451 (skip-chars-forward " \t")
1452 (if (not (looking-at "[\n|]"))
1453 (error "`%s': %s" ex-token viper-SpuriousText)))
1454 (error "`%s' requires a following letter" ex-token))))
1456 (goto-char (car ex-addresses))
1457 (point-to-register (1+ (- char ?a))))))
1461 ;; Alternate file is the file next to the first one in the buffer ring
1462 (defun ex-next (cycle-other-window &optional find-alt-file)
1465 (if (not find-alt-file)
1468 (if (or (char-or-string-p ex-offset)
1469 (and (not (string= "" ex-file))
1470 (not (string-match "^[0-9]+$" ex-file))))
1473 (throw 'ex-edit nil))
1474 (setq count (string-to-int ex-file))
1475 (if (= count 0) (setq count 1))
1476 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1478 (setq l (viper-undisplayed-files))
1480 (while (and (not (null l)) (null (car l)))
1482 (setq count (1- count))
1485 (if find-alt-file (car l)
1487 (if (and (car l) (get-file-buffer (car l)))
1488 (let* ((w (if cycle-other-window
1489 (get-lru-window) (selected-window)))
1490 (b (window-buffer w)))
1491 (set-window-buffer w (get-file-buffer (car l)))
1493 ;; this puts "next <count>" in the ex-command history
1494 (ex-fixup-history viper-last-ex-prompt ex-file))
1495 (error "Not that many undisplayed files")))))))
1498 (defun ex-next-related-buffer (direction &optional no-recursion)
1500 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction)
1502 (let ((file-or-buffer-name
1503 (viper-current-ring-item viper-related-files-and-buffers-ring))
1504 (old-ring viper-related-files-and-buffers-ring)
1505 (old-win (selected-window))
1508 (or (and (ring-p viper-related-files-and-buffers-ring)
1509 (> (ring-length viper-related-files-and-buffers-ring) 0))
1510 (error "This buffer has no related files or buffers"))
1512 (or (stringp file-or-buffer-name)
1514 "File and buffer names must be strings, %S" file-or-buffer-name))
1516 (setq buf (cond ((get-buffer file-or-buffer-name))
1517 ((file-exists-p file-or-buffer-name)
1518 (find-file-noselect file-or-buffer-name))
1521 (if (not (viper-buffer-live-p buf))
1522 (error "Didn't find buffer %S or file %S"
1524 (viper-abbreviate-file-name
1525 (expand-file-name file-or-buffer-name))))
1527 (if (equal buf (current-buffer))
1532 (ex-next-related-buffer direction 'norecursion))))
1537 (if (setq wind (viper-get-visible-buffer-window buf))
1539 (setq wind (get-lru-window (if viper-xemacs-p nil 'visible)))
1540 (set-window-buffer wind buf))
1542 (if (viper-window-display-p)
1544 (raise-frame (window-frame wind))
1545 (if (equal (window-frame wind) (window-frame old-win))
1546 (save-window-excursion (select-window wind) (sit-for 1))
1547 (select-window wind)))
1548 (save-window-excursion (select-window wind) (sit-for 1)))
1552 (setq viper-related-files-and-buffers-ring old-ring))
1554 (setq viper-local-search-start-marker (point-marker))
1559 (defun ex-preserve ()
1560 (message "Autosaving all buffers that need to be saved...")
1565 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1566 (viper-get-ex-buffer)
1567 (setq viper-use-register ex-buffer)
1569 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
1571 ;; Ex print working directory
1573 (message default-directory))
1577 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1579 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1580 (set-buffer viper-ex-work-buf)
1581 (if (looking-at "!") (forward-char 1)))
1582 (if (< viper-expert-level 3)
1583 (save-buffers-kill-emacs)
1584 (kill-buffer (current-buffer))))
1588 ;; ex-read doesn't support wildcards, because file completion is a better
1589 ;; mechanism. We also don't support # and % (except in :r <shell-command>
1590 ;; because file history is a better mechanism.
1593 (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
1596 (viper-add-newline-at-eob-if-necessary)
1597 (if (not (or (bobp) (eobp))) (forward-line 1))
1598 (if (and (not ex-variant) (string= ex-file ""))
1600 (if (null buffer-file-name)
1601 (error viper-NoFileSpecified))
1602 (setq ex-file buffer-file-name)))
1606 ;; replace # and % with the previous/current file
1607 (ex-expand-filsyms (concat ex-file ex-cmdfile-args)
1609 (shell-command command t))
1610 (insert-file-contents ex-file)))
1611 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args))
1613 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1614 (defun ex-fixup-history (&rest args)
1615 (setq viper-ex-history
1616 (cons (mapconcat 'identity args " ") (cdr viper-ex-history))))
1619 ;; Ex recover from emacs \#file\#
1620 (defun ex-recover ()
1622 (if (or ex-append ex-offset)
1623 (error "`recover': %s" viper-SpuriousText))
1624 (if (string= ex-file "")
1626 (if (null buffer-file-name)
1627 (error "This buffer isn't visiting any file"))
1628 (setq ex-file buffer-file-name))
1629 (setq ex-file (expand-file-name ex-file)))
1630 (if (and (not (string= ex-file (buffer-file-name)))
1633 (error "No write since last change \(:rec! overrides\)"))
1634 (recover-file ex-file))
1636 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1639 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1642 ;; read variable name for ex-set
1643 (defun ex-set-read-variable ()
1644 (let ((minibuffer-local-completion-map
1645 (copy-keymap minibuffer-local-completion-map))
1646 (cursor-in-echo-area t)
1649 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1650 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1651 (if (viper-set-unread-command-events
1652 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1655 (viper-set-unread-command-events ?\C-m)))
1656 (message ":set <Variable> [= <Value>]")
1657 (or batch (sit-for 2))
1659 (while (string-match "^[ \\t\\n]*$"
1661 (completing-read ":set " ex-variable-alist)))
1662 (message ":set <Variable> [= <Value>]")
1663 ;; if there are unread events, don't wait
1664 (or (viper-set-unread-command-events "") (sit-for 2))
1670 (let ((var (ex-set-read-variable))
1674 (auto-cmd-label "; don't touch or else...")
1675 (delete-turn-on-auto-fill-pattern
1676 "([ \t]*add-hook[ \t]+'viper-insert-state-hook[ \t]+'turn-on-auto-fill.*)")
1677 actual-lisp-cmd lisp-cmd-del-pattern
1680 (cond ((string= var "all")
1681 (setq ask-if-save nil
1683 ((member var '("ai" "autoindent"))
1684 (setq var "viper-auto-indent"
1688 ((member var '("ai-g" "autoindent-global"))
1689 (kill-local-variable 'viper-auto-indent)
1690 (setq var "viper-auto-indent"
1691 set-cmd "setq-default"
1693 ((member var '("noai" "noautoindent"))
1694 (setq var "viper-auto-indent"
1697 ((member var '("noai-g" "noautoindent-global"))
1698 (kill-local-variable 'viper-auto-indent)
1699 (setq var "viper-auto-indent"
1700 set-cmd "setq-default"
1702 ((member var '("ic" "ignorecase"))
1703 (setq var "viper-case-fold-search"
1705 ((member var '("noic" "noignorecase"))
1706 (setq var "viper-case-fold-search"
1708 ((member var '("ma" "magic"))
1709 (setq var "viper-re-search"
1711 ((member var '("noma" "nomagic"))
1712 (setq var "viper-re-search"
1714 ((member var '("ro" "readonly"))
1715 (setq var "buffer-read-only"
1717 ((member var '("noro" "noreadonly"))
1718 (setq var "buffer-read-only"
1720 ((member var '("sm" "showmatch"))
1721 (setq var "blink-matching-paren"
1723 ((member var '("nosm" "noshowmatch"))
1724 (setq var "blink-matching-paren"
1726 ((member var '("ws" "wrapscan"))
1727 (setq var "viper-search-wrap-around-t"
1729 ((member var '("nows" "nowrapscan"))
1730 (setq var "viper-search-wrap-around-t"
1732 (if (and set-cmd (eq val 0)) ; value must be set by the user
1733 (let ((cursor-in-echo-area t))
1734 (message ":set %s = <Value>" var)
1735 ;; if there are unread events, don't wait
1736 (or (viper-set-unread-command-events "") (sit-for 2))
1737 (setq val (read-string (format ":set %s = " var)))
1738 (ex-fixup-history "set" orig-var val)
1740 ;; check numerical values
1744 "ts-g" "tabstop-global"
1747 (or (numberp (setq val2 (car (read-from-string val))))
1748 (error "%s: Invalid value, numberp, %S" var val))
1750 (error "%s: Invalid value, numberp, %S" var val))))
1753 ((member var '("sw" "shiftwidth"))
1754 (setq var "viper-shift-width"))
1755 ((member var '("ts" "tabstop"))
1756 ;; make it take effect in curr buff and new bufs
1757 (setq var "tab-width"
1760 ((member var '("ts-g" "tabstop-global"))
1761 (kill-local-variable 'tab-width)
1762 (setq var "tab-width"
1763 set-cmd "setq-default"))
1764 ((member var '("wm" "wrapmargin"))
1765 ;; make it take effect in curr buff and new bufs
1766 (kill-local-variable 'fill-column)
1767 (setq var "fill-column"
1768 val (format "(- (window-width) %s)" val)
1769 set-cmd "setq-default"))
1770 ((member var '("sh" "shell"))
1771 (setq var "explicit-shell-file-name"
1772 val (format "\"%s\"" val)))))
1773 (ex-fixup-history "set" orig-var))
1776 (setq actual-lisp-cmd
1777 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label)
1778 lisp-cmd-del-pattern
1779 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1780 set-cmd var auto-cmd-label)))
1782 (if (and ask-if-save
1783 (y-or-n-p (format "Do you want to save this setting in %s "
1784 viper-custom-file-name)))
1786 (viper-save-string-in-file
1787 actual-lisp-cmd viper-custom-file-name
1789 lisp-cmd-del-pattern)
1790 (if (string= var "fill-column")
1792 (viper-save-string-in-file
1794 "(add-hook 'viper-insert-state-hook 'turn-on-auto-fill) "
1796 viper-custom-file-name
1797 delete-turn-on-auto-fill-pattern)
1798 (viper-save-string-in-file
1799 nil viper-custom-file-name delete-turn-on-auto-fill-pattern)
1800 (viper-save-string-in-file
1801 nil viper-custom-file-name
1803 lisp-cmd-del-pattern)
1810 (if (string-match "^[ \t]*$" val)
1814 (eval (car (read-from-string actual-lisp-cmd))))
1815 (if (string= var "fill-column")
1818 (auto-fill-mode -1)))
1819 (if (string= var "all") (ex-show-vars))
1822 ;; In inline args, skip regex-forw and (optionally) chars-back.
1823 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1825 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1827 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1828 (set-buffer viper-ex-work-buf)
1829 (goto-char (point-min))
1830 (re-search-forward regex-forw nil t)
1833 (goto-char (point-max))
1835 (skip-chars-backward chars-back)
1836 (skip-chars-backward " \t\n\C-m"))
1838 ;; replace SPC with `=' to suppress the special meaning SPC has
1842 (while (re-search-forward " +" nil t)
1843 (replace-match replace-str nil t)
1844 (viper-forward-char-carefully)))
1846 (buffer-substring beg end))))
1853 ;; Viper help. Invokes Info
1857 (pop-to-buffer (get-buffer-create "*info*"))
1858 (info (if viper-xemacs-p "viper.info" "viper"))
1859 (message "Type `i' to search for a specific topic"))
1861 (with-output-to-temp-buffer " *viper-info*"
1863 The Info file for Viper does not seem to be installed.
1865 This file is part of the standard distribution of %sEmacs.
1866 Please contact your system administrator. "
1867 (if viper-xemacs-p "X" "")
1870 ;; Ex source command. Loads the file specified as argument or `~/.viper'
1873 (if (string= ex-file "")
1874 (load viper-custom-file-name)
1877 ;; Ex substitute command
1878 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1879 (defun ex-substitute (&optional repeat r-flag)
1883 (case-fold-search viper-case-fold-search)
1885 (if repeat (setq ex-token nil) (setq delim (viper-get-ex-pat)))
1888 (setq pat (if r-flag viper-s-string ex-reg-exp))
1890 (error "No previous pattern to use in substitution"))
1892 delim (string-to-char pat)))
1893 (setq pat (if (string= ex-token "") viper-s-string ex-token))
1894 (setq viper-s-string pat
1896 (setq delim (viper-get-ex-pat))
1902 (while (viper-get-ex-opt-gc delim)
1903 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1904 (viper-get-ex-count)
1907 (if ex-addresses (goto-char (car ex-addresses)))
1909 (forward-line (1- ex-count))
1910 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1911 (if (null ex-addresses)
1912 (setq ex-addresses (cons (point) (cons (point) nil)))
1913 (if (null (cdr ex-addresses))
1914 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1916 (let ((beg (car ex-addresses))
1917 (end (car (cdr ex-addresses)))
1920 (viper-enlarge-region beg end)
1921 (let ((limit (save-excursion
1922 (goto-char (max (point) (mark t)))
1924 (goto-char (min (point) (mark t)))
1925 (while (< (point) limit)
1928 ;; This move allows the use of newline as the last character in
1929 ;; the substitution pattern
1930 (viper-forward-char-carefully)
1931 (setq eol-mark (point-marker)))
1935 (while (and (not (eolp))
1936 (re-search-forward pat eol-mark t))
1939 (viper-put-on-search-overlay (match-beginning 0)
1941 (y-or-n-p "Replace? ")))
1943 (viper-hide-search-overlay)
1944 (setq matched-pos (point))
1945 (if (not (stringp repl))
1946 (error "Can't perform Ex substitution: No previous replacement pattern"))
1947 (replace-match repl t))))
1949 (viper-forward-char-carefully))
1952 "Can't repeat Ex substitution: No previous regular expression"))
1953 (if (and (re-search-forward pat eol-mark t)
1956 (viper-put-on-search-overlay (match-beginning 0)
1958 (y-or-n-p "Replace? "))))
1960 (viper-hide-search-overlay)
1961 (setq matched-pos (point))
1962 (if (not (stringp repl))
1963 (error "Can't perform Ex substitution: No previous replacement pattern"))
1964 (replace-match repl t)))
1966 ;;(viper-forward-char-carefully)
1967 (goto-char eol-mark)
1969 (if matched-pos (goto-char matched-pos))
1971 (if opt-c (message "done"))))
1976 (save-window-excursion
1977 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1978 (set-buffer viper-ex-work-buf)
1979 (skip-chars-forward " \t")
1981 (skip-chars-forward "^ |\t\n")
1982 (setq tag (buffer-substring (mark t) (point))))
1983 (if (not (string= tag "")) (setq ex-tag tag))
1984 (viper-change-state-to-emacs)
1985 (condition-case conds
1987 (if (string= tag "")
1989 (find-tag-other-window ex-tag))
1990 (viper-change-state-to-vi))
1992 (viper-change-state-to-vi)
1993 (viper-message-conditions conds)))))
1996 ;; ex-write doesn't support wildcards, because file completion is a better
1997 ;; mechanism. We also don't support # and %
1998 ;; because file history is a better mechanism.
1999 (defun ex-write (q-flag)
2000 (viper-default-ex-addresses t)
2002 (let ((end (car ex-addresses))
2003 (beg (car (cdr ex-addresses)))
2004 (orig-buf (current-buffer))
2005 (orig-buf-file-name (buffer-file-name))
2006 (orig-buf-name (buffer-name))
2007 (buff-changed-p (buffer-modified-p))
2008 temp-buf writing-same-file region
2009 file-exists writing-whole-file)
2010 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2013 (viper-enlarge-region beg end)
2014 (shell-command-on-region (point) (mark t)
2015 (concat ex-file ex-cmdfile-args)))
2016 (if (and (string= ex-file "") (not (buffer-file-name)))
2019 (format "Buffer %s isn't visiting any file. File to save in: "
2022 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
2023 ex-file (if (string= ex-file "")
2025 (expand-file-name ex-file)))
2026 ;; if ex-file is a directory use the file portion of the buffer file name
2027 (if (and (file-directory-p ex-file)
2029 (not (file-directory-p buffer-file-name)))
2031 (concat (file-name-as-directory ex-file)
2032 (file-name-nondirectory buffer-file-name))))
2034 (setq file-exists (file-exists-p ex-file)
2035 writing-same-file (string= ex-file (buffer-file-name)))
2037 ;; do actual writing
2038 (if (and writing-whole-file writing-same-file)
2039 ;; saving whole buffer in visited file
2040 (if (not (buffer-modified-p))
2041 (message "(No changes need to be saved)")
2042 (viper-maybe-checkout (current-buffer))
2046 (ex-write-info file-exists ex-file (point-min) (point-max))
2048 ;; writing to non-visited file and it already exists
2049 (if (and file-exists (not writing-same-file)
2051 (format "File %s exists. Overwrite? " ex-file))))
2053 ;; writing a region or whole buffer to non-visited file
2056 (viper-enlarge-region beg end)
2057 (setq region (buffer-substring (point) (mark t)))
2058 ;; create temp buffer for the region
2059 (setq temp-buf (get-buffer-create " *ex-write*"))
2060 (set-buffer temp-buf)
2062 (set-visited-file-name ex-file)
2063 (set-visited-file-name ex-file 'noquerry))
2065 (if (and file-exists ex-append)
2066 (insert-file-contents ex-file))
2067 (goto-char (point-max))
2070 (viper-maybe-checkout (current-buffer))
2071 (setq selective-display nil)
2074 file-exists ex-file (point-min) (point-max))
2076 ;; this must be under unwind-protect so that
2077 ;; temp-buf will be deleted in case of an error
2078 (set-buffer temp-buf)
2079 (set-buffer-modified-p nil)
2080 (kill-buffer temp-buf)
2081 ;; buffer/region has been written, now take care of details
2082 (set-buffer orig-buf)))
2083 ;; set the right file modification time
2084 (if (and (buffer-file-name) writing-same-file)
2085 (set-visited-file-modtime))
2086 ;; prevent loss of data if saving part of the buffer in visited file
2087 (or writing-whole-file
2088 (not writing-same-file)
2091 (message "Warning: you have saved only part of the buffer!")
2092 (set-buffer-modified-p t)))
2094 (if (< viper-expert-level 2)
2095 (save-buffers-kill-emacs)
2096 (kill-buffer (current-buffer))))
2100 (defun ex-write-info (exists file-name beg end)
2101 (message "`%s'%s %d lines, %d characters"
2102 (viper-abbreviate-file-name file-name)
2103 (if exists "" " [New file]")
2104 (count-lines beg (min (1+ end) (point-max)))
2109 (viper-default-ex-addresses)
2110 (viper-get-ex-buffer)
2111 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2112 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2114 (viper-enlarge-region beg end)
2115 (exchange-point-and-mark)
2116 (if (or ex-g-flag ex-g-variant)
2117 (error "Can't execute `yank' within `global'"))
2121 (forward-line (1- ex-count)))
2123 (viper-enlarge-region (point) (mark t))
2124 (if ex-flag (error "`yank': %s" viper-SpuriousText))
2126 (cond ((viper-valid-register ex-buffer '(Letter))
2127 (viper-append-to-register
2128 (downcase ex-buffer) (point) (mark t)))
2129 ((viper-valid-register ex-buffer)
2130 (copy-to-register ex-buffer (point) (mark t) nil))
2131 (t (error viper-InvalidRegister ex-buffer))))
2132 (copy-region-as-kill (point) (mark t)))))
2134 ;; Execute shell command
2135 (defun ex-command ()
2137 (save-window-excursion
2138 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
2139 (set-buffer viper-ex-work-buf)
2140 (skip-chars-forward " \t")
2141 (setq command (buffer-substring (point) (point-max)))
2143 ;; replace # and % with the previous/current file
2144 (setq command (ex-expand-filsyms command (current-buffer)))
2145 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
2146 (if viper-ex-last-shell-com
2148 (concat viper-ex-last-shell-com (substring command 1)))
2149 (error "No previous shell command")))
2150 (setq viper-ex-last-shell-com command)
2151 (if (null ex-addresses)
2152 (shell-command command)
2153 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2154 (if (null beg) (setq beg end))
2158 (viper-enlarge-region (point) (mark t))
2159 (shell-command-on-region (point) (mark t) command t))
2162 ;; Print line number
2163 (defun ex-line-no ()
2167 (if (null ex-addresses) (point-max) (car ex-addresses))))))
2169 ;; Give information on the file visited by the current buffer
2170 (defun viper-info-on-file ()
2172 (let ((pos1 (viper-line-pos 'start))
2173 (pos2 (viper-line-pos 'end))
2175 (setq lines (count-lines (point-min) (viper-line-pos 'end))
2176 file (if (buffer-file-name)
2177 (concat (viper-abbreviate-file-name (buffer-file-name)) ":")
2178 (concat (buffer-name) " [Not visiting any file]:"))
2179 info (format "line=%d/%d pos=%d/%d col=%d %s"
2183 (count-lines (point-min) (point-max))
2184 (point) (1- (point-max))
2185 (1+ (current-column))
2186 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2187 (if (< (+ 1 (length info) (length file))
2188 (window-width (minibuffer-window)))
2189 (message (concat file " " info))
2190 (save-window-excursion
2191 (with-output-to-temp-buffer " *viper-info*"
2192 (princ (concat "\n" file "\n\n\t" info "\n\n")))
2193 (let ((inhibit-quit t))
2194 (viper-set-unread-command-events (viper-read-event)))
2195 (kill-buffer " *viper-info*")))
2198 ;; display all variables set through :set
2199 (defun ex-show-vars ()
2200 (with-output-to-temp-buffer " *viper-info*"
2201 (princ (if viper-auto-indent
2202 "autoindent (local)\n" "noautoindent (local)\n"))
2203 (princ (if (default-value 'viper-auto-indent)
2204 "autoindent (global) \n" "noautoindent (global) \n"))
2205 (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n"))
2206 (princ (if viper-re-search "magic\n" "nomagic\n"))
2207 (princ (if buffer-read-only "readonly\n" "noreadonly\n"))
2208 (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n"))
2209 (princ (if viper-search-wrap-around-t "wrapscan\n" "nowrapscan\n"))
2210 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width))
2211 (princ (format "tabstop (local) \t= %S\n" tab-width))
2212 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width)))
2213 (princ (format "wrapmargin (local) \t= %S\n"
2214 (- (window-width) fill-column)))
2215 (princ (format "wrapmargin (global) \t= %S\n"
2216 (- (window-width) (default-value 'fill-column))))
2217 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name)
2218 explicit-shell-file-name
2226 ;;; viper-ex.el ends here