1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994, 1995, 1996, 1997 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.
27 (defvar read-file-name-map)
28 (defvar viper-use-register)
29 (defvar viper-s-string)
30 (defvar viper-shift-width)
31 (defvar viper-ex-history)
32 (defvar viper-related-files-and-buffers-ring)
33 (defvar viper-local-search-start-marker)
34 (defvar viper-expert-level)
35 (defvar viper-custom-file-name)
36 (defvar viper-case-fold-search)
37 (defvar explicit-shell-file-name)
39 ;; loading happens only in non-interactive compilation
40 ;; in order to spare non-viperized emacs from being viperized
43 (let ((load-path (cons (expand-file-name ".") load-path)))
44 (or (featurep 'viper-util)
45 (load "viper-util.el" nil nil 'nosuffix))
46 (or (featurep 'viper-keym)
47 (load "viper-keym.el" nil nil 'nosuffix))
48 (or (featurep 'viper-cmd)
49 (load "viper-cmd.el" nil nil 'nosuffix))
55 (defgroup viper-ex nil
56 "Viper support for Ex commands"
64 (defconst viper-ex-work-buf-name " *ex-working-space*")
65 (defconst viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
66 (defconst viper-ex-tmp-buf-name " *ex-tmp*")
69 ;;; Variable completion in :set command
71 ;; The list of Ex commands. Used for completing command names.
72 (defconst ex-token-alist
73 '(("!") ("=") (">") ("&") ("~")
74 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
75 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
76 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
77 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
78 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
79 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
80 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
81 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
83 ;; A-list of Ex variables that can be set using the :set command.
84 (defconst ex-variable-alist
85 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
86 ("tabstop-global") ("ts-g") ("tabstop") ("ts")
87 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
89 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
90 ("noreadonly") ("noro") ("nomagic") ("noma")
91 ("noignorecase") ("noic")
92 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai")
93 ("magic") ("ma") ("ignorecase") ("ic")
94 ("autoindent-global") ("ai-g") ("autoindent") ("ai")
100 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
101 (defvar ex-token nil)
104 ;; If non-nil, gives type of address; if nil, it is a command.
105 (defvar ex-token-type nil)
107 ;; List of addresses passed to Ex command
108 (defvar ex-addresses nil)
110 ;; This flag is supposed to be set only by `#', `print', and `list',
111 ;; none of which is implemented. So, it and the pices of the code it
112 ;; controls are dead weight. We keep it just in case this might be
113 ;; needed in the future.
116 ;; "buffer" where Ex commands keep deleted data.
117 ;; In Emacs terms, this is a register.
118 (defvar ex-buffer nil)
120 ;; Value of ex count.
121 (defvar ex-count nil)
123 ;; Flag indicating that :global Ex command is being executed.
124 (defvar ex-g-flag nil)
125 ;; Flag indicating that :vglobal Ex command is being executed.
126 (defvar ex-g-variant nil)
128 ;; Save reg-exp used in substitute.
129 (defvar ex-reg-exp nil)
132 ;; Replace pattern for substitute.
135 ;; Pattern for global command.
136 (defvar ex-g-pat nil)
138 (defcustom ex-unix-type-shell
139 (let ((case-fold-search t))
140 (and (stringp shell-file-name)
150 "[^a-z]sh$\\|[^a-z]sh.exe$"
155 "Is the user using a unix-type shell under a non-OS?"
159 (defcustom ex-unix-type-shell-options
160 (let ((case-fold-search t))
161 (if ex-unix-type-shell
162 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name)
163 "-f") ; csh: do it fast
164 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name)
165 "-noprofile") ; bash: ignore .profile
167 "Options to pass to the Unix-style shell.
168 Don't put `-c' here, as it is added automatically."
172 (defvar ex-nontrivial-find-file-function
173 (cond (ex-unix-type-shell 'viper-ex-nontrivial-find-file-unix)
174 ((eq system-type 'emx) 'viper-ex-nontrivial-find-file-ms) ; OS/2
175 (viper-ms-style-os-p 'viper-ex-nontrivial-find-file-ms) ; Microsoft OS
176 (viper-vms-os-p 'viper-ex-nontrivial-find-file-unix) ; VMS
177 (t 'viper-ex-nontrivial-find-file-unix) ; presumably UNIX
180 ;; Remembers the previous Ex tag.
183 ;; file used by Ex commands like :r, :w, :n
186 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
187 (defvar ex-variant nil)
189 ;; Specified the offset of an Ex command, such as :read.
190 (defvar ex-offset nil)
192 ;; Tells Ex that this is a w>> command.
193 (defvar ex-append nil)
195 ;; File containing the shell command to be executed at Ex prompt,
197 (defvar ex-cmdfile nil)
199 ;; flag used in viper-ex-read-file-name to indicate that we may be reading
200 ;; multiple file names. Used for :edit and :next
201 (defvar viper-keep-reading-filename nil)
203 (defcustom ex-cycle-other-window t
204 "*If t, :n and :b cycles through files and buffers in other window.
205 Then :N and :B cycles in the current window. If nil, this behavior is
210 (defcustom ex-cycle-through-non-files nil
211 "*Cycle through *scratch* and other buffers that don't visit any file."
215 ;; Last shell command executed with :! command.
216 (defvar viper-ex-last-shell-com nil)
218 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
219 (defvar viper-incomplete-ex-cmd nil)
221 ;; Remembers the last ex-command prompt.
222 (defvar viper-last-ex-prompt "")
227 ;; Check if ex-token is an initial segment of STR
228 (defun viper-check-sub (str)
229 (let ((length (length ex-token)))
230 (if (and (<= length (length str))
231 (string= ex-token (substring str 0 length)))
233 (setq ex-token-type 'non-command))))
235 ;; Get a complete ex command
236 (defun viper-get-ex-com-subr ()
237 (let (case-fold-search)
239 (re-search-forward "[a-zA-Z][a-zA-Z]*")
240 (setq ex-token-type 'command)
241 (setq ex-token (buffer-substring (point) (mark t)))
242 (exchange-point-and-mark)
243 (cond ((looking-at "a")
244 (cond ((looking-at "ab") (viper-check-sub "abbreviate"))
245 ((looking-at "ar") (viper-check-sub "args"))
246 (t (viper-check-sub "append"))))
247 ((looking-at "h") (viper-check-sub "help"))
249 (cond ((looking-at "cd") (viper-check-sub "cd"))
250 ((looking-at "ch") (viper-check-sub "chdir"))
251 ((looking-at "co") (viper-check-sub "copy"))
252 (t (viper-check-sub "change"))))
253 ((looking-at "d") (viper-check-sub "delete"))
254 ((looking-at "b") (viper-check-sub "buffer"))
255 ((looking-at "B") (viper-check-sub "Buffer"))
257 (if (looking-at "ex") (viper-check-sub "ex")
258 (viper-check-sub "edit")))
259 ((looking-at "f") (viper-check-sub "file"))
260 ((looking-at "g") (viper-check-sub "global"))
261 ((looking-at "i") (viper-check-sub "insert"))
262 ((looking-at "j") (viper-check-sub "join"))
263 ((looking-at "l") (viper-check-sub "list"))
265 (cond ((looking-at "map") (viper-check-sub "map"))
266 ((looking-at "mar") (viper-check-sub "mark"))
267 (t (viper-check-sub "move"))))
268 ((looking-at "k[a-z][^a-z]")
269 (setq ex-token "kmark")
271 (exchange-point-and-mark)) ; this is canceled out by another
272 ; exchange-point-and-mark at the end
273 ((looking-at "k") (viper-check-sub "kmark"))
274 ((looking-at "n") (if (looking-at "nu")
275 (viper-check-sub "number")
276 (viper-check-sub "next")))
277 ((looking-at "N") (viper-check-sub "Next"))
278 ((looking-at "o") (viper-check-sub "open"))
280 (cond ((looking-at "pre") (viper-check-sub "preserve"))
281 ((looking-at "pu") (viper-check-sub "put"))
282 ((looking-at "pw") (viper-check-sub "pwd"))
283 (t (viper-check-sub "print"))))
284 ((looking-at "P") (viper-check-sub "PreviousRelatedFile"))
285 ((looking-at "R") (viper-check-sub "RelatedFile"))
286 ((looking-at "q") (viper-check-sub "quit"))
288 (cond ((looking-at "rec") (viper-check-sub "recover"))
289 ((looking-at "rew") (viper-check-sub "rewind"))
290 (t (viper-check-sub "read"))))
292 (cond ((looking-at "se") (viper-check-sub "set"))
293 ((looking-at "sh") (viper-check-sub "shell"))
294 ((looking-at "so") (viper-check-sub "source"))
295 ((looking-at "sr") (viper-check-sub "sr"))
296 ((looking-at "st") (viper-check-sub "stop"))
297 ((looking-at "sus") (viper-check-sub "suspend"))
298 ((looking-at "subm") (viper-check-sub "submitReport"))
299 (t (viper-check-sub "substitute"))))
301 (if (looking-at "ta") (viper-check-sub "tag")
302 (viper-check-sub "transfer")))
304 (cond ((looking-at "una") (viper-check-sub "unabbreviate"))
305 ((looking-at "unm") (viper-check-sub "unmap"))
306 (t (viper-check-sub "undo"))))
308 (cond ((looking-at "ve") (viper-check-sub "version"))
309 ((looking-at "vi") (viper-check-sub "visual"))
310 (t (viper-check-sub "vglobal"))))
312 (if (looking-at "wq") (viper-check-sub "wq")
313 (viper-check-sub "write")))
315 (if (looking-at "WW")
316 (viper-check-sub "WWrite")
317 (viper-check-sub "Write")))
318 ((looking-at "x") (viper-check-sub "xit"))
319 ((looking-at "y") (viper-check-sub "yank"))
320 ((looking-at "z") (viper-check-sub "z")))
321 (exchange-point-and-mark)
324 ;; Get an ex-token which is either an address or a command.
325 ;; A token has a type, \(command, address, end-mark\), and a value
326 (defun viper-get-ex-token ()
327 (save-window-excursion
328 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
329 (set-buffer viper-ex-work-buf)
330 (skip-chars-forward " \t|")
331 (let ((case-fold-search t))
332 (cond ((looking-at "#")
333 (setq ex-token-type 'command)
334 (setq ex-token (char-to-string (following-char)))
336 ((looking-at "[a-z]") (viper-get-ex-com-subr))
339 (setq ex-token-type 'dot))
340 ((looking-at "[0-9]")
342 (re-search-forward "[0-9]*")
344 (cond ((eq ex-token-type 'plus) 'add-number)
345 ((eq ex-token-type 'minus) 'sub-number)
348 (string-to-int (buffer-substring (point) (mark t)))))
351 (setq ex-token-type 'end))
354 (setq ex-token-type 'whole))
356 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
360 (setq ex-token-type 'plus))
361 ((looking-at "+[0-9]")
363 (setq ex-token-type 'plus))
365 (error viper-BadAddress))))
367 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
371 (setq ex-token-type 'minus))
372 ((looking-at "-[0-9]")
374 (setq ex-token-type 'minus))
376 (error viper-BadAddress))))
381 (while (and (not (eolp)) cont)
382 ;;(re-search-forward "[^/]*/")
383 (re-search-forward "[^/]*\\(/\\|\n\\)")
384 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
387 (setq ex-token (buffer-substring (point) (mark t)))
388 (if (looking-at "/") (forward-char 1))
389 (setq ex-token-type 'search-forward))
394 (while (and (not (eolp)) cont)
395 ;;(re-search-forward "[^\\?]*\\?")
396 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
397 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
400 (if (not (looking-at "\n")) (forward-char 1))))
401 (setq ex-token-type 'search-backward)
402 (setq ex-token (buffer-substring (1- (point)) (mark t))))
405 (setq ex-token-type 'comma))
408 (setq ex-token-type 'semi-colon))
409 ((looking-at "[!=><&~]")
410 (setq ex-token-type 'command)
411 (setq ex-token (char-to-string (following-char)))
414 (setq ex-token-type 'goto-mark)
416 (cond ((looking-at "'") (setq ex-token nil))
417 ((looking-at "[a-z]") (setq ex-token (following-char)))
418 (t (error "Marks are ' and a-z")))
421 (setq ex-token-type 'end-mark)
422 (setq ex-token "goto"))
424 (error viper-BadExCommand))))))
426 ;; Reads Ex command. Tries to determine if it has to exit because command
427 ;; is complete or invalid. If not, keeps reading command.
428 (defun ex-cmd-read-exit ()
430 (setq viper-incomplete-ex-cmd t)
431 (let ((quit-regex1 (concat
434 "\\|" "[nN]ext[ \t]*"
447 "\\|" "[ktgjmsz][ \t]*$"
449 "\\|" "tr[ansfer \t]*"
452 "\\|" "^[ \t]*k?ma[^p]*"
461 "\\|" "![ \t]*[a-zA-Z].*"
465 (save-window-excursion ;; put cursor at the end of the Ex working buffer
466 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
467 (set-buffer viper-ex-work-buf)
468 (goto-char (point-max)))
469 (cond ((viper-looking-back quit-regex1) (exit-minibuffer))
470 ((viper-looking-back stay-regex) (insert " "))
471 ((viper-looking-back quit-regex2) (exit-minibuffer))
474 ;; complete Ex command
475 (defun ex-cmd-complete ()
477 (let (save-pos dist compl-list string-to-complete completion-result)
480 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
484 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
486 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
487 ;; Preceding characters are not the ones allowed in an Ex command
488 ;; or we have typed past command name.
489 ;; Note: we didn't do parsing, so there may be surprises.
490 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
491 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
492 (looking-at "[^ \t\n\C-m]"))
494 (with-output-to-temp-buffer "*Completions*"
495 (display-completion-list
496 (viper-alist-to-list ex-token-alist))))
497 ;; Preceding chars may be part of a command name
498 (setq string-to-complete (buffer-substring save-pos (point)))
499 (setq completion-result
500 (try-completion string-to-complete ex-token-alist))
502 (cond ((eq completion-result t) ; exact match--do nothing
503 (viper-tmp-insert-at-eob " (Sole completion)"))
504 ((eq completion-result nil)
505 (viper-tmp-insert-at-eob " (No match)"))
506 (t ;; partial completion
508 (delete-region (point) (point-max))
509 (insert completion-result)
510 (let (case-fold-search)
512 (viper-filter-alist (concat "^" completion-result)
514 (if (> (length compl-list) 1)
515 (with-output-to-temp-buffer "*Completions*"
516 (display-completion-list
517 (viper-alist-to-list (reverse compl-list)))))))
522 (defun viper-ex (&optional string)
527 (let* ((map (copy-keymap minibuffer-local-map))
531 prev-token-type com-str)
533 (viper-add-keymap viper-ex-cmd-map map)
535 (setq com-str (or string (viper-read-string-with-history
539 (car viper-ex-history)
541 (save-window-excursion
543 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
544 (set-buffer viper-ex-work-buf)
545 (delete-region (point-min) (point-max))
546 (insert com-str "\n")
547 (goto-char (point-min)))
548 (setq ex-token-type nil
552 (cond ((memq ex-token-type '(command end-mark))
553 (if address (setq ex-addresses (cons address ex-addresses)))
554 (cond ((string= ex-token "global")
557 ((string= ex-token "vglobal")
561 (viper-execute-ex-command)
562 (save-window-excursion
563 (setq viper-ex-work-buf
564 (get-buffer-create viper-ex-work-buf-name))
565 (set-buffer viper-ex-work-buf)
566 (skip-chars-forward " \t")
567 (cond ((looking-at "|")
571 (t (error "`%s': %s" ex-token viper-SpuriousText)))
574 ((eq ex-token-type 'non-command)
575 (error "`%s': %s" ex-token viper-BadExCommand))
576 ((eq ex-token-type 'whole)
580 (cons (point-max) ex-addresses)
581 (cons (point-max) (cons (point-min) ex-addresses)))))
582 ((eq ex-token-type 'comma)
583 (if (eq prev-token-type 'whole)
584 (setq address (point-min)))
586 (cons (if (null address) (point) address) ex-addresses)))
587 ((eq ex-token-type 'semi-colon)
588 (if (eq prev-token-type 'whole)
589 (setq address (point-min)))
590 (if address (setq dot address))
592 (cons (if (null address) (point) address) ex-addresses)))
593 (t (let ((ans (viper-get-ex-address-subr address dot)))
594 (if ans (setq address ans)))))
595 (setq prev-token-type ex-token-type))))
598 ;; Get a regular expression and set `ex-variant', if found
599 (defun viper-get-ex-pat ()
600 (save-window-excursion
601 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
602 (set-buffer viper-ex-work-buf)
603 (skip-chars-forward " \t")
606 (setq ex-g-variant (not ex-g-variant)
607 ex-g-flag (not ex-g-flag))
609 (skip-chars-forward " \t")))
610 (let ((c (following-char)))
611 (if (string-match "[0-9A-Za-z]" (format "%c" c))
613 "Global regexp must be inside matching non-alphanumeric chars"))
614 (if (looking-at "[^\\\\\n]")
619 (while (and (not (eolp)) cont)
620 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
621 (if (member ex-token '("global" "vglobal"))
623 "Missing closing delimiter for global regexp")
624 (goto-char (point-max))))
625 (if (not (viper-looking-back
626 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
629 (if (= (mark t) (point)) ""
630 (buffer-substring (1- (point)) (mark t))))
632 ;; if the user doesn't specify the final pattern delimiter, we're
633 ;; at newline now. In this case, insert the initial delimiter
634 ;; specified in variable c
635 (if (looking-at "\n")
644 (defun viper-get-ex-command ()
645 (save-window-excursion
646 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
647 (set-buffer viper-ex-work-buf)
648 (if (looking-at "/") (forward-char 1))
649 (skip-chars-forward " \t")
650 (cond ((looking-at "[a-z]")
651 (viper-get-ex-com-subr)
652 (if (eq ex-token-type 'non-command)
653 (error "`%s': %s" ex-token viper-BadExCommand)))
654 ((looking-at "[!=><&~]")
655 (setq ex-token (char-to-string (following-char)))
657 (t (error viper-BadExCommand)))))
659 ;; Get an Ex option g or c
660 (defun viper-get-ex-opt-gc (c)
661 (save-window-excursion
662 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
663 (set-buffer viper-ex-work-buf)
664 (if (looking-at (format "%c" c)) (forward-char 1))
665 (skip-chars-forward " \t")
666 (cond ((looking-at "g")
676 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
677 (defun viper-default-ex-addresses (&optional whole-flag)
678 (cond ((null ex-addresses)
681 (cons (point-max) (cons (point-min) nil))
682 (cons (point) (cons (point) nil)))))
683 ((null (cdr ex-addresses))
685 (cons (car ex-addresses) ex-addresses)))))
687 ;; Get an ex-address as a marker and set ex-flag if a flag is found
688 (defun viper-get-ex-address ()
689 (let ((address (point-marker))
695 (cond ((eq ex-token-type 'command)
696 (if (member ex-token '("print" "list" "#"))
700 (error "Address expected in this Ex command")))
701 ((eq ex-token-type 'end-mark)
703 ((eq ex-token-type 'whole)
704 (error "Trailing address expected"))
705 ((eq ex-token-type 'comma)
706 (error "`%s': %s" ex-token viper-SpuriousText))
707 (t (let ((ans (viper-get-ex-address-subr address (point-marker))))
708 (if ans (setq address ans))))))
711 ;; Returns an address as a point
712 (defun viper-get-ex-address-subr (old-address dot)
714 (if (null old-address) (setq old-address dot))
715 (cond ((eq ex-token-type 'dot)
717 ((eq ex-token-type 'add-number)
719 (goto-char old-address)
720 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
721 (setq address (point-marker))))
722 ((eq ex-token-type 'sub-number)
724 (goto-char old-address)
725 (forward-line (- ex-token))
726 (setq address (point-marker))))
727 ((eq ex-token-type 'abs-number)
729 (goto-char (point-min))
730 (if (= ex-token 0) (setq address 0)
731 (forward-line (1- ex-token))
732 (setq address (point-marker)))))
733 ((eq ex-token-type 'end)
734 (setq address (point-max-marker)))
735 ((eq ex-token-type 'plus) t) ; do nothing
736 ((eq ex-token-type 'minus) t) ; do nothing
737 ((eq ex-token-type 'search-forward)
739 (ex-search-address t)
740 (setq address (point-marker))))
741 ((eq ex-token-type 'search-backward)
743 (ex-search-address nil)
744 (setq address (point-marker))))
745 ((eq ex-token-type 'goto-mark)
748 (exchange-point-and-mark)
749 (goto-char (viper-register-to-point
750 (1+ (- ex-token ?a)) 'enforce-buffer)))
751 (setq address (point-marker)))))
755 ;; Search pattern and set address
756 (defun ex-search-address (forward)
757 (if (string= ex-token "")
758 (if (null viper-s-string)
759 (error viper-NoPrevSearch)
760 (setq ex-token viper-s-string))
761 (setq viper-s-string ex-token))
765 (re-search-forward ex-token))
767 (re-search-backward ex-token)))
769 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
770 (defun viper-get-ex-buffer ()
774 (save-window-excursion
775 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
776 (set-buffer viper-ex-work-buf)
777 (skip-chars-forward " \t")
778 (if (looking-at "[a-zA-Z]")
780 (setq ex-buffer (following-char))
782 (skip-chars-forward " \t")))
783 (if (looking-at "[0-9]")
786 (re-search-forward "[0-9][0-9]*")
787 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
788 (skip-chars-forward " \t")))
789 (if (looking-at "[pl#]")
793 (if (not (looking-at "[\n|]"))
794 (error "`%s': %s" ex-token viper-SpuriousText))))
796 (defun viper-get-ex-count ()
800 (save-window-excursion
801 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
802 (set-buffer viper-ex-work-buf)
803 (skip-chars-forward " \t")
808 (skip-chars-forward " \t")
809 (if (looking-at "[0-9]")
812 (re-search-forward "[0-9][0-9]*")
813 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
814 (skip-chars-forward " \t")))
815 (if (looking-at "[pl#]")
819 (if (not (looking-at "[\n|]"))
822 (point-min) (1- (point-max))) viper-BadExCommand))))
824 ;; Expand \% and \# in ex command
825 (defun ex-expand-filsyms (cmd buf)
829 (setq cf buffer-file-name)
830 (setq pf (ex-next nil t))) ; this finds alternative file name
831 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
832 (error "No current file to substitute for `%%'"))
833 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
834 (error "No alternate file to substitute for `#'"))
836 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
839 (goto-char (point-min))
840 (while (re-search-forward "%\\|#" nil t)
841 (let ((data (match-data))
842 (char (buffer-substring (match-beginning 0) (match-end 0))))
843 (if (viper-looking-back (concat "\\\\" char))
845 (store-match-data data)
846 (if (string= char "%")
848 (replace-match pf)))))
850 (setq ret (buffer-substring (point-min) (point)))
854 ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
855 (defun viper-get-ex-file ()
863 (save-window-excursion
864 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
865 (set-buffer viper-ex-work-buf)
866 (skip-chars-forward " \t")
868 (if (and (not (viper-looking-back "[ \t]"))
869 ;; read doesn't have a corresponding :r! form, so ! is
870 ;; immediately interpreted as a shell command.
871 (not (string= ex-token "read")))
875 (skip-chars-forward " \t"))
878 (skip-chars-forward " \t")))
879 (if (looking-at ">>")
884 (skip-chars-forward " \t")))
889 (re-search-forward "[ \t\n]")
891 (setq ex-offset (buffer-substring (point) (mark t)))
893 (skip-chars-forward " \t")))
894 ;; this takes care of :r, :w, etc., when they get file names
895 ;; from the history list
896 (if (member ex-token '("read" "write" "edit" "visual" "next"))
898 (setq ex-file (buffer-substring (point) (1- (point-max))))
900 ;; For :e, match multiple non-white strings separated
901 ;; by white. For others, find the first non-white string
903 (if (string= ex-token "edit")
904 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
908 ;; if file name comes from history, don't leave
909 ;; minibuffer when the user types space
910 (setq viper-incomplete-ex-cmd nil)
911 ;; this must be the last clause in this progn
912 (substring ex-file (match-beginning 0) (match-end 0))
915 ;; this leaves only the command name in the work area
916 ;; file names are gone
917 (delete-region (point) (1- (point-max)))
919 (goto-char (point-max))
920 (skip-chars-backward " \t\n")
921 (setq prompt (buffer-substring (point-min) (point)))
924 (setq viper-last-ex-prompt prompt)
926 ;; If we just finished reading command, redisplay prompt
927 (if viper-incomplete-ex-cmd
928 (setq ex-file (viper-ex-read-file-name (format ":%s " prompt)))
929 ;; file was typed in-line
930 (setq ex-file (or ex-file "")))
934 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
935 ;; file names, arranges to re-enter the minibuffer.
936 (defun viper-complete-filename-or-exit ()
938 (setq viper-keep-reading-filename t)
939 ;; don't exit if directory---ex-commands don't
940 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
941 ;; apparently the argument to an Ex command is
942 ;; supposed to be a shell command
943 ((viper-looking-back "^[ \t]*!.*")
947 (setq ex-cmdfile nil)
948 (minibuffer-complete-word))))
950 (defun viper-handle-! ()
953 (buffer-string) (viper-abbreviate-file-name default-directory))
954 (member ex-token '("read" "write")))
958 (defun ex-cmd-accepts-multiple-files-p (token)
959 (member token '("edit" "next" "Next")))
961 ;; If user doesn't enter anything, then "" is returned, i.e., the
962 ;; prompt-directory is not returned.
963 (defun viper-ex-read-file-name (prompt)
965 (minibuffer-local-completion-map
966 (copy-keymap minibuffer-local-completion-map))
969 (viper-add-keymap ex-read-filename-map
971 minibuffer-local-completion-map
974 (setq cont (setq viper-keep-reading-filename t))
976 (setq viper-keep-reading-filename nil
977 val (read-file-name (concat prompt str) nil default-directory))
978 (if (string-match " " val)
979 (setq val (concat "\\\"" val "\\\"")))
980 (setq str (concat str (if (equal val "") "" " ")
981 val (if (equal val "") "" " ")))
983 ;; Only edit, next, and Next commands accept multiple files.
984 ;; viper-keep-reading-filename is set in the anonymous function that is
985 ;; bound to " " in ex-read-filename-map.
986 (setq cont (and viper-keep-reading-filename
987 (ex-cmd-accepts-multiple-files-p ex-token)))
990 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
991 end (string-match "[ \t]*$" str)) ; delete trailing blanks
992 (if (member ex-token '("read" "write"))
993 (if (string-match "[\t ]*!" str)
994 ;; this is actually a shell command
998 (setq viper-last-ex-prompt
999 (concat viper-last-ex-prompt " !")))))
1000 (substring str (or beg 0) end)))
1002 ;; Execute ex command using the value of addresses
1003 (defun viper-execute-ex-command ()
1004 (viper-deactivate-mark)
1005 (cond ((string= ex-token "args") (ex-args))
1006 ((string= ex-token "copy") (ex-copy nil))
1007 ((string= ex-token "cd") (ex-cd))
1008 ((string= ex-token "chdir") (ex-cd))
1009 ((string= ex-token "delete") (ex-delete))
1010 ((string= ex-token "edit") (ex-edit))
1011 ((string= ex-token "file") (viper-info-on-file))
1012 ((string= ex-token "goto") (ex-goto))
1013 ((string= ex-token "help") (ex-help))
1014 ((string= ex-token "join") (ex-line "join"))
1015 ((string= ex-token "kmark") (ex-mark))
1016 ((string= ex-token "mark") (ex-mark))
1017 ((string= ex-token "map") (ex-map))
1018 ((string= ex-token "move") (ex-copy t))
1019 ((string= ex-token "next") (ex-next ex-cycle-other-window))
1020 ((string= ex-token "Next") (ex-next (not ex-cycle-other-window)))
1021 ((string= ex-token "RelatedFile") (ex-next-related-buffer 1))
1022 ((string= ex-token "put") (ex-put))
1023 ((string= ex-token "pwd") (ex-pwd))
1024 ((string= ex-token "preserve") (ex-preserve))
1025 ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1))
1026 ((string= ex-token "quit") (ex-quit))
1027 ((string= ex-token "read") (ex-read))
1028 ((string= ex-token "recover") (ex-recover))
1029 ((string= ex-token "rewind") (ex-rewind))
1030 ((string= ex-token "submitReport") (viper-submit-report))
1031 ((string= ex-token "set") (ex-set))
1032 ((string= ex-token "shell") (ex-shell))
1033 ((string= ex-token "source") (ex-source))
1034 ((string= ex-token "sr") (ex-substitute t t))
1035 ((string= ex-token "substitute") (ex-substitute))
1036 ((string= ex-token "suspend") (suspend-emacs))
1037 ((string= ex-token "stop") (suspend-emacs))
1038 ((string= ex-token "transfer") (ex-copy nil))
1039 ((string= ex-token "buffer") (if ex-cycle-other-window
1040 (viper-switch-to-buffer-other-window)
1041 (viper-switch-to-buffer)))
1042 ((string= ex-token "Buffer") (if ex-cycle-other-window
1043 (viper-switch-to-buffer)
1044 (viper-switch-to-buffer-other-window)))
1045 ((string= ex-token "tag") (ex-tag))
1046 ((string= ex-token "undo") (viper-undo))
1047 ((string= ex-token "unmap") (ex-unmap))
1048 ((string= ex-token "version") (viper-version))
1049 ((string= ex-token "visual") (ex-edit))
1050 ((string= ex-token "write") (ex-write nil))
1051 ((string= ex-token "Write") (save-some-buffers))
1052 ((string= ex-token "wq") (ex-write t))
1053 ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask
1054 ((string= ex-token "xit") (ex-write t))
1055 ((string= ex-token "yank") (ex-yank))
1056 ((string= ex-token "!") (ex-command))
1057 ((string= ex-token "=") (ex-line-no))
1058 ((string= ex-token ">") (ex-line "right"))
1059 ((string= ex-token "<") (ex-line "left"))
1060 ((string= ex-token "&") (ex-substitute t))
1061 ((string= ex-token "~") (ex-substitute t t))
1062 ((or (string= ex-token "append")
1063 (string= ex-token "change")
1064 (string= ex-token "insert")
1065 (string= ex-token "open"))
1066 (error "`%s': Obsolete command, not supported by Viper" ex-token))
1067 ((or (string= ex-token "abbreviate")
1068 (string= ex-token "unabbreviate"))
1070 "`%s': Vi abbrevs are obsolete. Use the more powerful Emacs abbrevs"
1072 ((or (string= ex-token "list")
1073 (string= ex-token "print")
1074 (string= ex-token "z")
1075 (string= ex-token "#"))
1076 (error "`%s': Command not implemented in Viper" ex-token))
1077 (t (error "`%s': %s" ex-token viper-BadExCommand))))
1079 (defun viper-undisplayed-files ()
1083 (if (null (get-buffer-window b))
1084 (let ((f (buffer-file-name b)))
1086 (if ex-cycle-through-non-files
1087 (let ((s (buffer-name b)))
1088 (if (string= " " (substring s 0 1))
1097 (let ((l (viper-undisplayed-files))
1100 (while (not (null l))
1102 (setq args (format "%s %d) %s\n" args file-count (car l))
1103 file-count (1+ file-count)))
1105 (if (string= args "")
1106 (message "All files are already displayed")
1108 (save-window-excursion
1109 (with-output-to-temp-buffer " *viper-info*"
1110 (princ "\n\nThese files are not displayed in any window.\n")
1111 (princ "\n=============\n")
1113 (princ "\n=============\n")
1114 (princ "\nThe numbers can be given as counts to :next. ")
1115 (princ "\n\nPress any key to continue...\n\n"))
1116 (viper-read-event))))))
1118 ;; Ex cd command. Default directory of this buffer changes
1121 (if (string= ex-file "")
1123 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
1125 ;; Ex copy and move command. DEL-FLAG means delete
1126 (defun ex-copy (del-flag)
1127 (viper-default-ex-addresses)
1128 (let ((address (viper-get-ex-address))
1129 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1133 (viper-enlarge-region (mark t) (point))
1135 (kill-region (point) (mark t))
1136 (copy-region-as-kill (point) (mark t)))
1139 (with-output-to-temp-buffer " *copy text*"
1141 (if (or del-flag ex-g-flag ex-g-variant)
1143 (buffer-substring (point) (mark t)))))
1146 (read-string "[Hit return to confirm] ")
1147 (save-excursion (kill-buffer " *copy text*")))
1148 (quit (save-excursion (kill-buffer " *copy text*"))
1149 (signal 'quit nil))))))
1151 (goto-char (point-min))
1154 (insert (current-kill 0))))
1156 ;; Ex delete command
1158 (viper-default-ex-addresses)
1159 (viper-get-ex-buffer)
1160 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1161 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1163 (viper-enlarge-region beg end)
1164 (exchange-point-and-mark)
1168 (forward-line (1- ex-count)))
1170 (viper-enlarge-region (point) (mark t))
1172 ;; show text to be deleted and ask for confirmation
1174 (with-output-to-temp-buffer " *delete text*"
1175 (princ (buffer-substring (point) (mark t))))
1177 (read-string "[Hit return to confirm] ")
1179 (save-excursion (kill-buffer " *delete text*"))
1181 (save-excursion (kill-buffer " *delete text*")))
1183 (cond ((viper-valid-register ex-buffer '(Letter))
1184 (viper-append-to-register
1185 (downcase ex-buffer) (point) (mark t)))
1186 ((viper-valid-register ex-buffer)
1187 (copy-to-register ex-buffer (point) (mark t) nil))
1188 (t (error viper-InvalidRegister ex-buffer))))
1189 (kill-region (point) (mark t))))))
1194 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1195 ;; asked if current buffer should really be discarded.
1196 ;; This command can take multiple file names. It replaces the current buffer
1197 ;; with the first file in its argument list
1198 (defun ex-edit (&optional file)
1200 (viper-get-ex-file))
1201 (cond ((and (string= ex-file "") buffer-file-name)
1202 (setq ex-file (viper-abbreviate-file-name (buffer-file-name))))
1203 ((string= ex-file "")
1204 (error viper-NoFileSpecified)))
1207 (if buffer-file-name
1208 (cond ((buffer-modified-p)
1210 (format "Buffer %s is modified. Discard changes? "
1213 ((not (verify-visited-file-modtime (current-buffer)))
1215 (format "File %s changed on disk. Reread from disk? "
1218 (t (setq do-edit nil))))
1221 (if (yes-or-no-p msg)
1223 (set-buffer-modified-p nil)
1224 (kill-buffer (current-buffer)))
1225 (message "Buffer %s was left intact" (buffer-name))))
1228 (if (null (setq file (get-file-buffer ex-file)))
1230 (ex-find-file ex-file)
1231 (or (eq major-mode 'dired-mode)
1232 (viper-change-state-to-vi))
1233 (goto-char (point-min)))
1234 (switch-to-buffer file))
1237 (save-window-excursion
1238 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1239 (set-buffer viper-ex-work-buf)
1240 (delete-region (point-min) (point-max))
1241 (insert ex-offset "\n")
1242 (goto-char (point-min)))
1243 (goto-char (viper-get-ex-address))
1244 (beginning-of-line)))
1245 (ex-fixup-history viper-last-ex-prompt ex-file))
1247 ;; Find-file FILESPEC if it appears to specify a single file.
1248 ;; Otherwise, assume that FILES{EC is a wildcard.
1249 ;; In this case, split it into substrings separated by newlines.
1250 ;; Each line is assumed to be a file name. find-file's each file thus obtained.
1251 (defun ex-find-file (filespec)
1252 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1253 (cond ((file-exists-p filespec) (find-file filespec))
1254 ((string-match nonstandard-filename-chars filespec)
1255 (funcall ex-nontrivial-find-file-function filespec))
1256 (t (find-file filespec)))
1260 ;; Ex global command
1261 ;; This is executed in response to:
1262 ;; :global "pattern" ex-command
1263 ;; :vglobal "pattern" ex-command
1264 ;; :global executes ex-command on all lines matching <pattern>
1265 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1267 ;; With VARIANT nil, this functions executes :global
1268 ;; With VARIANT t, executes :vglobal
1269 (defun ex-global (variant)
1270 (let ((gcommand ex-token))
1271 (if (or ex-g-flag ex-g-variant)
1272 (error "`%s' within `global' is not allowed" gcommand)
1280 (error "`%s': Missing regular expression" gcommand)))
1282 (if (string= ex-token "")
1283 (if (null viper-s-string)
1284 (error viper-NoPrevSearch)
1285 (setq ex-g-pat viper-s-string))
1286 (setq ex-g-pat ex-token
1287 viper-s-string ex-token))
1288 (if (null ex-addresses)
1289 (setq ex-addresses (list (point-max) (point-min)))
1290 (viper-default-ex-addresses))
1293 (end (car ex-addresses))
1294 (beg (car (cdr ex-addresses)))
1296 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1298 (viper-enlarge-region beg end)
1299 (exchange-point-and-mark)
1300 (let ((cont t) (limit (point-marker)))
1301 (exchange-point-and-mark)
1302 ;; skip the last line if empty
1304 (if (eobp) (viper-backward-char-carefully))
1305 (while (and cont (not (bobp)) (>= (point) limit))
1309 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1310 (if (or (and ex-g-flag found)
1311 (and ex-g-variant (not found)))
1314 (setq mark-count (1+ mark-count))
1315 (setq marks (cons (point-marker) marks)))))
1317 (if (bobp) (setq cont nil)
1320 (save-window-excursion
1321 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1322 (set-buffer viper-ex-work-buf)
1323 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1325 (goto-char (car marks))
1327 (setq mark-count (1- mark-count))
1328 (setq marks (cdr marks)))))
1332 (if (null ex-addresses)
1333 (setq ex-addresses (cons (point) nil)))
1334 (push-mark (point) t)
1335 (goto-char (car ex-addresses))
1336 (beginning-of-line))
1338 ;; Ex line commands. COM is join, shift-right or shift-left
1339 (defun ex-line (com)
1340 (viper-default-ex-addresses)
1341 (viper-get-ex-count)
1342 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1343 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1345 (viper-enlarge-region beg end)
1346 (exchange-point-and-mark)
1350 (forward-line ex-count)))
1352 ;; show text to be joined and ask for confirmation
1354 (with-output-to-temp-buffer " *join text*"
1355 (princ (buffer-substring (point) (mark t))))
1358 (read-string "[Hit return to confirm] ")
1359 (ex-line-subr com (point) (mark t)))
1361 (save-excursion (kill-buffer " *join text*")))
1362 (ex-line-subr com (point) (mark t)))
1363 (setq point (point)))
1364 (goto-char (1- point))
1365 (beginning-of-line)))
1367 (defun ex-line-subr (com beg end)
1368 (cond ((string= com "join")
1369 (goto-char (min beg end))
1370 (while (and (not (eobp)) (< (point) (max beg end)))
1372 (if (and (<= (point) (max beg end)) (not (eobp)))
1375 (delete-region (point) (1- (point)))
1376 (if (not ex-variant) (fixup-whitespace))))))
1377 ((or (string= com "right") (string= com "left"))
1379 (min beg end) (max beg end)
1380 (if (string= com "right") viper-shift-width (- viper-shift-width)))
1381 (goto-char (max beg end))
1383 (viper-forward-char-carefully))))
1389 (if (null ex-addresses)
1391 (cons (point) nil)))
1392 (save-window-excursion
1393 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1394 (set-buffer viper-ex-work-buf)
1395 (skip-chars-forward " \t")
1396 (if (looking-at "[a-z]")
1398 (setq char (following-char))
1400 (skip-chars-forward " \t")
1401 (if (not (looking-at "[\n|]"))
1402 (error "`%s': %s" ex-token viper-SpuriousText)))
1403 (error "`%s' requires a following letter" ex-token)))
1405 (goto-char (car ex-addresses))
1406 (point-to-register (1+ (- char ?a))))))
1410 ;; Alternate file is the file next to the first one in the buffer ring
1411 (defun ex-next (cycle-other-window &optional find-alt-file)
1414 (if (not find-alt-file)
1417 (if (or (char-or-string-p ex-offset)
1418 (and (not (string= "" ex-file))
1419 (not (string-match "^[0-9]+$" ex-file))))
1422 (throw 'ex-edit nil))
1423 (setq count (string-to-int ex-file))
1424 (if (= count 0) (setq count 1))
1425 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1427 (setq l (viper-undisplayed-files))
1429 (while (and (not (null l)) (null (car l)))
1431 (setq count (1- count))
1434 (if find-alt-file (car l)
1436 (if (and (car l) (get-file-buffer (car l)))
1437 (let* ((w (if cycle-other-window
1438 (get-lru-window) (selected-window)))
1439 (b (window-buffer w)))
1440 (set-window-buffer w (get-file-buffer (car l)))
1442 ;; this puts "next <count>" in the ex-command history
1443 (ex-fixup-history viper-last-ex-prompt ex-file))
1444 (error "Not that many undisplayed files")))))))
1447 (defun ex-next-related-buffer (direction &optional no-recursion)
1449 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction)
1451 (let ((file-or-buffer-name
1452 (viper-current-ring-item viper-related-files-and-buffers-ring))
1453 (old-ring viper-related-files-and-buffers-ring)
1454 (old-win (selected-window))
1457 (or (and (ring-p viper-related-files-and-buffers-ring)
1458 (> (ring-length viper-related-files-and-buffers-ring) 0))
1459 (error "This buffer has no related files or buffers"))
1461 (or (stringp file-or-buffer-name)
1463 "File and buffer names must be strings, %S" file-or-buffer-name))
1465 (setq buf (cond ((get-buffer file-or-buffer-name))
1466 ((file-exists-p file-or-buffer-name)
1467 (find-file-noselect file-or-buffer-name))
1470 (if (not (viper-buffer-live-p buf))
1471 (error "Didn't find buffer %S or file %S"
1473 (viper-abbreviate-file-name
1474 (expand-file-name file-or-buffer-name))))
1476 (if (equal buf (current-buffer))
1481 (ex-next-related-buffer direction 'norecursion))))
1486 (if (setq wind (viper-get-visible-buffer-window buf))
1488 (setq wind (get-lru-window (if viper-xemacs-p nil 'visible)))
1489 (set-window-buffer wind buf))
1491 (if (viper-window-display-p)
1493 (raise-frame (window-frame wind))
1494 (if (equal (window-frame wind) (window-frame old-win))
1495 (save-window-excursion (select-window wind) (sit-for 1))
1496 (select-window wind)))
1497 (save-window-excursion (select-window wind) (sit-for 1)))
1501 (setq viper-related-files-and-buffers-ring old-ring))
1503 (setq viper-local-search-start-marker (point-marker))
1508 (defun ex-preserve ()
1509 (message "Autosaving all buffers that need to be saved...")
1514 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1515 (viper-get-ex-buffer)
1516 (setq viper-use-register ex-buffer)
1518 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
1520 ;; Ex print working directory
1522 (message default-directory))
1526 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1528 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1529 (set-buffer viper-ex-work-buf)
1530 (if (looking-at "!") (forward-char 1)))
1531 (if (< viper-expert-level 3)
1532 (save-buffers-kill-emacs)
1533 (kill-buffer (current-buffer))))
1539 (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
1542 (viper-add-newline-at-eob-if-necessary)
1543 (if (not (or (bobp) (eobp))) (forward-line 1))
1544 (if (and (not ex-variant) (string= ex-file ""))
1546 (if (null buffer-file-name)
1547 (error viper-NoFileSpecified))
1548 (setq ex-file buffer-file-name)))
1551 (setq command (ex-expand-filsyms ex-file (current-buffer)))
1552 (shell-command command t))
1553 (insert-file-contents ex-file)))
1554 (ex-fixup-history viper-last-ex-prompt ex-file))
1556 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1557 (defun ex-fixup-history (&rest args)
1558 (setq viper-ex-history
1559 (cons (mapconcat 'identity args " ") (cdr viper-ex-history))))
1562 ;; Ex recover from emacs \#file\#
1563 (defun ex-recover ()
1565 (if (or ex-append ex-offset)
1566 (error "`recover': %s" viper-SpuriousText))
1567 (if (string= ex-file "")
1569 (if (null buffer-file-name)
1570 (error "This buffer isn't visiting any file"))
1571 (setq ex-file buffer-file-name))
1572 (setq ex-file (expand-file-name ex-file)))
1573 (if (and (not (string= ex-file (buffer-file-name)))
1576 (error "No write since last change \(:rec! overrides\)"))
1577 (recover-file ex-file))
1579 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1582 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1585 ;; read variable name for ex-set
1586 (defun ex-set-read-variable ()
1587 (let ((minibuffer-local-completion-map
1588 (copy-keymap minibuffer-local-completion-map))
1589 (cursor-in-echo-area t)
1592 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1593 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1594 (if (viper-set-unread-command-events
1595 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1598 (viper-set-unread-command-events ?\C-m)))
1599 (message ":set <Variable> [= <Value>]")
1600 (or batch (sit-for 2))
1602 (while (string-match "^[ \\t\\n]*$"
1604 (completing-read ":set " ex-variable-alist)))
1605 (message ":set <Variable> [= <Value>]")
1606 ;; if there are unread events, don't wait
1607 (or (viper-set-unread-command-events "") (sit-for 2))
1613 (let ((var (ex-set-read-variable))
1617 (auto-cmd-label "; don't touch or else...")
1618 (delete-turn-on-auto-fill-pattern
1619 "([ \t]*add-hook[ \t]+'viper-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1620 actual-lisp-cmd lisp-cmd-del-pattern
1623 (cond ((string= var "all")
1624 (setq ask-if-save nil
1626 ((member var '("ai" "autoindent"))
1627 (setq var "viper-auto-indent"
1631 ((member var '("ai-g" "autoindent-global"))
1632 (kill-local-variable 'viper-auto-indent)
1633 (setq var "viper-auto-indent"
1634 set-cmd "setq-default"
1636 ((member var '("noai" "noautoindent"))
1637 (setq var "viper-auto-indent"
1640 ((member var '("noai-g" "noautoindent-global"))
1641 (kill-local-variable 'viper-auto-indent)
1642 (setq var "viper-auto-indent"
1643 set-cmd "setq-default"
1645 ((member var '("ic" "ignorecase"))
1646 (setq var "viper-case-fold-search"
1648 ((member var '("noic" "noignorecase"))
1649 (setq var "viper-case-fold-search"
1651 ((member var '("ma" "magic"))
1652 (setq var "viper-re-search"
1654 ((member var '("noma" "nomagic"))
1655 (setq var "viper-re-search"
1657 ((member var '("ro" "readonly"))
1658 (setq var "buffer-read-only"
1660 ((member var '("noro" "noreadonly"))
1661 (setq var "buffer-read-only"
1663 ((member var '("sm" "showmatch"))
1664 (setq var "blink-matching-paren"
1666 ((member var '("nosm" "noshowmatch"))
1667 (setq var "blink-matching-paren"
1669 ((member var '("ws" "wrapscan"))
1670 (setq var "viper-search-wrap-around-t"
1672 ((member var '("nows" "nowrapscan"))
1673 (setq var "viper-search-wrap-around-t"
1675 (if (and set-cmd (eq val 0)) ; value must be set by the user
1676 (let ((cursor-in-echo-area t))
1677 (message ":set %s = <Value>" var)
1678 ;; if there are unread events, don't wait
1679 (or (viper-set-unread-command-events "") (sit-for 2))
1680 (setq val (read-string (format ":set %s = " var)))
1681 (ex-fixup-history "set" orig-var val)
1683 ;; check numerical values
1687 "ts-g" "tabstop-global"
1690 (or (numberp (setq val2 (car (read-from-string val))))
1691 (error "%s: Invalid value, numberp, %S" var val))
1693 (error "%s: Invalid value, numberp, %S" var val))))
1696 ((member var '("sw" "shiftwidth"))
1697 (setq var "viper-shift-width"))
1698 ((member var '("ts" "tabstop"))
1699 ;; make it take effect in curr buff and new bufs
1700 (setq var "tab-width"
1703 ((member var '("ts-g" "tabstop-global"))
1704 (kill-local-variable 'tab-width)
1705 (setq var "tab-width"
1706 set-cmd "setq-default"))
1707 ((member var '("wm" "wrapmargin"))
1708 ;; make it take effect in curr buff and new bufs
1709 (kill-local-variable 'fill-column)
1710 (setq var "fill-column"
1711 val (format "(- (window-width) %s)" val)
1712 set-cmd "setq-default"))
1713 ((member var '("sh" "shell"))
1714 (setq var "explicit-shell-file-name"
1715 val (format "\"%s\"" val)))))
1716 (ex-fixup-history "set" orig-var))
1719 (setq actual-lisp-cmd
1720 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label)
1721 lisp-cmd-del-pattern
1722 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1723 set-cmd var auto-cmd-label)))
1725 (if (and ask-if-save
1726 (y-or-n-p (format "Do you want to save this setting in %s "
1727 viper-custom-file-name)))
1729 (viper-save-string-in-file
1730 actual-lisp-cmd viper-custom-file-name
1732 lisp-cmd-del-pattern)
1733 (if (string= var "fill-column")
1735 (viper-save-string-in-file
1737 "(add-hook 'viper-insert-state-hooks 'turn-on-auto-fill) "
1739 viper-custom-file-name
1740 delete-turn-on-auto-fill-pattern)
1741 (viper-save-string-in-file
1742 nil viper-custom-file-name delete-turn-on-auto-fill-pattern)
1743 (viper-save-string-in-file
1744 nil viper-custom-file-name
1746 lisp-cmd-del-pattern)
1753 (if (string-match "^[ \t]*$" val)
1757 (eval (car (read-from-string actual-lisp-cmd))))
1758 (if (string= var "fill-column")
1761 (auto-fill-mode -1)))
1762 (if (string= var "all") (ex-show-vars))
1765 ;; In inline args, skip regex-forw and (optionally) chars-back.
1766 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1768 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1770 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1771 (set-buffer viper-ex-work-buf)
1772 (goto-char (point-min))
1773 (re-search-forward regex-forw nil t)
1776 (goto-char (point-max))
1778 (skip-chars-backward chars-back)
1779 (skip-chars-backward " \t\n\C-m"))
1781 ;; replace SPC with `=' to suppress the special meaning SPC has
1785 (while (re-search-forward " +" nil t)
1786 (replace-match replace-str nil t)
1787 (viper-forward-char-carefully)))
1789 (buffer-substring beg end))))
1796 ;; Viper help. Invokes Info
1800 (pop-to-buffer (get-buffer-create "*info*"))
1801 (info (if viper-xemacs-p "viper.info" "viper"))
1802 (message "Type `i' to search for a specific topic"))
1804 (with-output-to-temp-buffer " *viper-info*"
1806 The Info file for Viper does not seem to be installed.
1808 This file is part of the standard distribution of %sEmacs.
1809 Please contact your system administrator. "
1810 (if viper-xemacs-p "X" "")
1813 ;; Ex source command. Loads the file specified as argument or `~/.viper'
1816 (if (string= ex-file "")
1817 (load viper-custom-file-name)
1820 ;; Ex substitute command
1821 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1822 (defun ex-substitute (&optional repeat r-flag)
1826 (case-fold-search viper-case-fold-search)
1828 (if repeat (setq ex-token nil) (setq delim (viper-get-ex-pat)))
1831 (setq pat (if r-flag viper-s-string ex-reg-exp))
1833 (error "No previous pattern to use in substitution"))
1835 delim (string-to-char pat)))
1836 (setq pat (if (string= ex-token "") viper-s-string ex-token))
1837 (setq viper-s-string pat
1839 (setq delim (viper-get-ex-pat))
1845 (while (viper-get-ex-opt-gc delim)
1846 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1847 (viper-get-ex-count)
1850 (if ex-addresses (goto-char (car ex-addresses)))
1852 (forward-line (1- ex-count))
1853 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1854 (if (null ex-addresses)
1855 (setq ex-addresses (cons (point) (cons (point) nil)))
1856 (if (null (cdr ex-addresses))
1857 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1859 (let ((beg (car ex-addresses))
1860 (end (car (cdr ex-addresses)))
1863 (viper-enlarge-region beg end)
1864 (let ((limit (save-excursion
1865 (goto-char (max (point) (mark t)))
1867 (goto-char (min (point) (mark t)))
1868 (while (< (point) limit)
1870 (setq eol-mark (point-marker))
1874 (while (and (not (eolp))
1875 (re-search-forward pat eol-mark t))
1876 (if (or (not opt-c) (y-or-n-p "Replace? "))
1878 (setq matched-pos (point))
1879 (if (not (stringp repl))
1880 (error "Can't perform Ex substitution: No previous replacement pattern"))
1881 (replace-match repl t))))
1883 (viper-forward-char-carefully))
1886 "Can't repeat Ex substitution: No previous regular expression"))
1887 (if (and (re-search-forward pat eol-mark t)
1888 (or (not opt-c) (y-or-n-p "Replace? ")))
1890 (setq matched-pos (point))
1891 (if (not (stringp repl))
1892 (error "Can't perform Ex substitution: No previous replacement pattern"))
1893 (replace-match repl t)))
1895 (viper-forward-char-carefully))))))
1896 (if matched-pos (goto-char matched-pos))
1898 (if opt-c (message "done"))))
1903 (save-window-excursion
1904 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1905 (set-buffer viper-ex-work-buf)
1906 (skip-chars-forward " \t")
1908 (skip-chars-forward "^ |\t\n")
1909 (setq tag (buffer-substring (mark t) (point))))
1910 (if (not (string= tag "")) (setq ex-tag tag))
1911 (viper-change-state-to-emacs)
1912 (condition-case conds
1914 (if (string= tag "")
1916 (find-tag-other-window ex-tag))
1917 (viper-change-state-to-vi))
1919 (viper-change-state-to-vi)
1920 (viper-message-conditions conds)))))
1923 (defun ex-write (q-flag)
1924 (viper-default-ex-addresses t)
1926 (let ((end (car ex-addresses))
1927 (beg (car (cdr ex-addresses)))
1928 (orig-buf (current-buffer))
1929 (orig-buf-file-name (buffer-file-name))
1930 (orig-buf-name (buffer-name))
1931 (buff-changed-p (buffer-modified-p))
1932 temp-buf writing-same-file region
1933 file-exists writing-whole-file)
1934 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1937 (viper-enlarge-region beg end)
1938 (shell-command-on-region (point) (mark t) ex-file))
1939 (if (and (string= ex-file "") (not (buffer-file-name)))
1942 (format "Buffer %s isn't visiting any file. File to save in: "
1945 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
1946 ex-file (if (string= ex-file "")
1948 (expand-file-name ex-file)))
1949 ;; if ex-file is a directory use the file portion of the buffer file name
1950 (if (and (file-directory-p ex-file)
1952 (not (file-directory-p buffer-file-name)))
1954 (concat (file-name-as-directory ex-file)
1955 (file-name-nondirectory buffer-file-name))))
1957 (setq file-exists (file-exists-p ex-file)
1958 writing-same-file (string= ex-file (buffer-file-name)))
1960 (if (and writing-whole-file writing-same-file)
1961 (if (not (buffer-modified-p))
1962 (message "(No changes need to be saved)")
1966 (ex-write-info file-exists ex-file (point-min) (point-max))
1968 ;; writing some other file or portion of the current file
1969 (cond ((and file-exists
1970 (not writing-same-file)
1972 (format "File %s exists. Overwrite? " ex-file))))
1974 ((and writing-whole-file (not ex-append))
1977 (set-visited-file-name ex-file)
1978 (set-buffer-modified-p t)
1980 ;; restore the buffer file name
1981 (set-visited-file-name orig-buf-file-name)
1982 (set-buffer-modified-p buff-changed-p)
1983 ;; If the buffer wasn't visiting a file, restore buffer name.
1984 ;; Name could've been changed by packages such as uniquify.
1985 (or orig-buf-file-name
1988 (rename-buffer orig-buf-name))))
1992 file-exists ex-file (point-min) (point-max))))
1993 (t ; writing a region
1996 (viper-enlarge-region beg end)
1997 (setq region (buffer-substring (point) (mark t)))
1998 ;; create temp buffer for the region
1999 (setq temp-buf (get-buffer-create " *ex-write*"))
2000 (set-buffer temp-buf)
2001 (set-visited-file-name ex-file 'noquerry)
2003 (if (and file-exists ex-append)
2004 (insert-file-contents ex-file))
2005 (goto-char (point-max))
2009 file-exists ex-file (point-min) (point-max))
2011 (set-buffer temp-buf)
2012 (set-buffer-modified-p nil)
2013 (kill-buffer temp-buf))
2015 (set-buffer orig-buf)
2016 ;; this prevents the loss of data if writing part of the buffer
2017 (if (and (buffer-file-name) writing-same-file)
2018 (set-visited-file-modtime))
2019 (or writing-whole-file
2020 (not writing-same-file)
2021 (set-buffer-modified-p t))
2023 (if (< viper-expert-level 2)
2024 (save-buffers-kill-emacs)
2025 (kill-buffer (current-buffer))))
2029 (defun ex-write-info (exists file-name beg end)
2030 (message "`%s'%s %d lines, %d characters"
2031 (viper-abbreviate-file-name file-name)
2032 (if exists "" " [New file]")
2033 (count-lines beg (min (1+ end) (point-max)))
2038 (viper-default-ex-addresses)
2039 (viper-get-ex-buffer)
2040 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2041 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2043 (viper-enlarge-region beg end)
2044 (exchange-point-and-mark)
2045 (if (or ex-g-flag ex-g-variant)
2046 (error "Can't execute `yank' within `global'"))
2050 (forward-line (1- ex-count)))
2052 (viper-enlarge-region (point) (mark t))
2053 (if ex-flag (error "`yank': %s" viper-SpuriousText))
2055 (cond ((viper-valid-register ex-buffer '(Letter))
2056 (viper-append-to-register
2057 (downcase ex-buffer) (point) (mark t)))
2058 ((viper-valid-register ex-buffer)
2059 (copy-to-register ex-buffer (point) (mark t) nil))
2060 (t (error viper-InvalidRegister ex-buffer))))
2061 (copy-region-as-kill (point) (mark t)))))
2063 ;; Execute shell command
2064 (defun ex-command ()
2066 (save-window-excursion
2067 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
2068 (set-buffer viper-ex-work-buf)
2069 (skip-chars-forward " \t")
2070 (setq command (buffer-substring (point) (point-max)))
2072 (setq command (ex-expand-filsyms command (current-buffer)))
2073 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
2074 (if viper-ex-last-shell-com
2076 (concat viper-ex-last-shell-com (substring command 1)))
2077 (error "No previous shell command")))
2078 (setq viper-ex-last-shell-com command)
2079 (if (null ex-addresses)
2080 (shell-command command)
2081 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2082 (if (null beg) (setq beg end))
2086 (viper-enlarge-region (point) (mark t))
2087 (shell-command-on-region (point) (mark t) command t))
2090 ;; Print line number
2091 (defun ex-line-no ()
2095 (if (null ex-addresses) (point-max) (car ex-addresses))))))
2097 ;; Give information on the file visited by the current buffer
2098 (defun viper-info-on-file ()
2100 (let ((pos1 (viper-line-pos 'start))
2101 (pos2 (viper-line-pos 'end))
2103 (setq lines (count-lines (point-min) (viper-line-pos 'end))
2104 file (if (buffer-file-name)
2105 (concat (viper-abbreviate-file-name (buffer-file-name)) ":")
2106 (concat (buffer-name) " [Not visiting any file]:"))
2107 info (format "line=%d/%d pos=%d/%d col=%d %s"
2111 (count-lines (point-min) (point-max))
2112 (point) (1- (point-max))
2113 (1+ (current-column))
2114 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2115 (if (< (+ 1 (length info) (length file))
2116 (window-width (minibuffer-window)))
2117 (message (concat file " " info))
2118 (save-window-excursion
2119 (with-output-to-temp-buffer " *viper-info*"
2120 (princ (concat "\n" file "\n\n\t" info "\n\n")))
2121 (let ((inhibit-quit t))
2122 (viper-set-unread-command-events (viper-read-event)))
2123 (kill-buffer " *viper-info*")))
2126 ;; display all variables set through :set
2127 (defun ex-show-vars ()
2128 (with-output-to-temp-buffer " *viper-info*"
2129 (princ (if viper-auto-indent
2130 "autoindent (local)\n" "noautoindent (local)\n"))
2131 (princ (if (default-value 'viper-auto-indent)
2132 "autoindent (global) \n" "noautoindent (global) \n"))
2133 (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n"))
2134 (princ (if viper-re-search "magic\n" "nomagic\n"))
2135 (princ (if buffer-read-only "readonly\n" "noreadonly\n"))
2136 (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n"))
2137 (princ (if viper-search-wrap-around-t "wrapscan\n" "nowrapscan\n"))
2138 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width))
2139 (princ (format "tabstop (local) \t= %S\n" tab-width))
2140 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width)))
2141 (princ (format "wrapmargin (local) \t= %S\n"
2142 (- (window-width) fill-column)))
2143 (princ (format "wrapmargin (global) \t= %S\n"
2144 (- (window-width) (default-value 'fill-column))))
2145 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name)
2146 explicit-shell-file-name
2154 ;;; viper-ex.el ends here