1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (defvar read-file-name-map
)
31 (defvar viper-use-register
)
32 (defvar viper-s-string
)
33 (defvar viper-shift-width
)
34 (defvar viper-ex-history
)
35 (defvar viper-related-files-and-buffers-ring
)
36 (defvar viper-local-search-start-marker
)
37 (defvar viper-expert-level
)
38 (defvar viper-custom-file-name
)
39 (defvar viper-case-fold-search
)
40 (defvar explicit-shell-file-name
)
41 (defvar compile-command
)
43 ;; loading happens only in non-interactive compilation
44 ;; in order to spare non-viperized emacs from being viperized
47 (if (not (featurep 'viper-cmd
))
54 (defgroup viper-ex nil
55 "Viper support for Ex commands."
63 (defconst viper-ex-work-buf-name
" *ex-working-space*")
64 (defvar viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
65 (defconst viper-ex-tmp-buf-name
" *ex-tmp*")
66 (defconst viper-ex-print-buf-name
" *ex-print*")
67 (defvar viper-ex-print-buf
(get-buffer-create viper-ex-print-buf-name
))
72 (defun ex-cmd-obsolete (name)
73 (error "`%s': Obsolete command, not supported by Viper" name
))
75 (defun ex-cmd-not-yet (name)
76 (error "`%s': Command not implemented in Viper" name
))
78 ;; alist entries: name (in any order), command, cont(??)
79 ;; If command is a string, then that is an alias to the real command
80 ;; to execute (for instance, ":m" -> ":move").
81 ;; command attributes:
82 ;; is-mashed: the command's args may be jammed right up against the command
83 ;; one-letter: this is a one-letter token. Any text appearing after
84 ;; the name gets appended as an argument for the command
85 ;; i.e. ":kabc" gets turned into (ex-mark "abc")
86 (defconst ex-token-alist
'(
88 ("&" (ex-substitute t
))
90 (">" (ex-line "right"))
91 ("<" (ex-line "left"))
92 ("Buffer" (if ex-cycle-other-window
93 (viper-switch-to-buffer)
94 (viper-switch-to-buffer-other-window)))
95 ("Next" (ex-next (not ex-cycle-other-window
)))
96 ("PreviousRelatedFile" (ex-next-related-buffer -
1))
97 ("RelatedFile" (ex-next-related-buffer 1))
99 ("WWrite" (save-some-buffers t
))
100 ("Write" (save-some-buffers))
103 ("buffer" (if ex-cycle-other-window
104 (viper-switch-to-buffer-other-window)
105 (viper-switch-to-buffer)))
107 ;; ch should be "change" but maintain old viper compatibility
111 ("copy" (ex-copy nil
))
112 ("customize" (customize-group "viper"))
113 ("delete" (ex-delete))
115 ("file" (ex-set-visited-file-name))
117 ("global" (ex-global nil
) is-mashed
)
120 ("join" (ex-line "join"))
121 ("k" (ex-mark) one-letter
)
124 ("make" (ex-compile))
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))
139 ("recover" (ex-recover))
140 ("rewind" (ex-rewind))
146 ("source" (ex-source))
147 ("stop" (suspend-emacs))
148 ("sr" (ex-substitute t t
))
149 ("submitReport" (viper-submit-report))
150 ("substitute" (ex-substitute) is-mashed
)
151 ("suspend" (suspend-emacs))
154 ("transfer" (ex-copy nil
))
157 ("undo" (viper-undo))
160 ("version" (viper-version))
161 ("vglobal" (ex-global t
) is-mashed
)
165 ("write" (ex-write nil
))
168 ("~" (ex-substitute t t
))
170 ("append" (ex-cmd-obsolete "append"))
171 ("change" (ex-cmd-obsolete "change"))
172 ("insert" (ex-cmd-obsolete "insert"))
173 ("open" (ex-cmd-obsolete "open"))
175 ("list" (ex-cmd-not-yet "list"))
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 ((oneletter (ex-cmd-is-one-letter (assoc (substring key
0 1) list
))))
207 (append (cadr oneletter
)
208 (if (< 1 (length key
)) (list (substring key
1))))
209 (car (cdr (cdr oneletter
))) ))
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
)
278 ;; Marks to operate on during a :global Ex command.
279 (defvar ex-g-marks nil
)
281 ;; Save reg-exp used in substitute.
282 (defvar ex-reg-exp nil
)
285 ;; Replace pattern for substitute.
288 ;; Pattern for global command.
289 (defvar ex-g-pat nil
)
291 (defcustom ex-unix-type-shell
292 (let ((case-fold-search t
))
293 (and (stringp shell-file-name
)
303 "[^a-z]sh$\\|[^a-z]sh.exe$"
308 "Is the user using a unix-type shell under a non-OS?"
312 (defcustom ex-unix-type-shell-options
313 (let ((case-fold-search t
))
314 (if ex-unix-type-shell
315 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name
)
316 "-f") ; csh: do it fast
317 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name
)
318 "-noprofile") ; bash: ignore .profile
320 "Options to pass to the Unix-style shell.
321 Don't put `-c' here, as it is added automatically."
322 :type
'(choice (const nil
) string
)
325 (defcustom ex-compile-command
"make"
326 "The command to run when the user types :make."
330 (defcustom viper-glob-function
331 (cond (ex-unix-type-shell 'viper-glob-unix-files
)
332 ((eq system-type
'emx
) 'viper-glob-mswindows-files
) ; OS/2
333 (viper-ms-style-os-p 'viper-glob-mswindows-files
) ; Microsoft OS
334 (viper-vms-os-p 'viper-glob-unix-files
) ; VMS
335 (t 'viper-glob-unix-files
) ; presumably UNIX
337 "Expand the file spec containing wildcard symbols.
338 The default tries to set this variable to work with Unix, Windows,
341 However, if it doesn't work right for some types of Unix shells or some OS,
342 the user should supply the appropriate function and set this variable to the
343 corresponding function symbol."
348 ;; Remembers the previous Ex tag.
351 ;; file used by Ex commands like :r, :w, :n
354 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
355 (defvar ex-variant nil
)
357 ;; Specified the offset of an Ex command, such as :read.
358 (defvar ex-offset nil
)
360 ;; Tells Ex that this is a w>> command.
361 (defvar ex-append nil
)
363 ;; File containing the shell command to be executed at Ex prompt,
365 (defvar ex-cmdfile nil
)
366 (defvar ex-cmdfile-args
"")
368 ;; flag used in viper-ex-read-file-name to indicate that we may be reading
369 ;; multiple file names. Used for :edit and :next
370 (defvar viper-keep-reading-filename nil
)
372 (defcustom ex-cycle-other-window t
373 "*If t, :n and :b cycles through files and buffers in other window.
374 Then :N and :B cycles in the current window. If nil, this behavior is
379 (defcustom ex-cycle-through-non-files nil
380 "*Cycle through *scratch* and other buffers that don't visit any file."
384 ;; Last shell command executed with :! command.
385 (defvar viper-ex-last-shell-com nil
)
387 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
388 (defvar viper-incomplete-ex-cmd nil
)
390 ;; Remembers the last ex-command prompt.
391 (defvar viper-last-ex-prompt
"")
394 ;; Get a complete ex command
395 (defun viper-get-ex-com-subr ()
396 (let (cmd case-fold-search
)
398 (re-search-forward "[a-zA-Z][a-zA-Z]*")
399 (setq ex-token-type
'command
)
400 (setq ex-token
(buffer-substring (point) (mark t
)))
401 (setq cmd
(ex-cmd-assoc ex-token ex-token-alist
))
403 (setq ex-token
(car cmd
))
404 (setq ex-token-type
'non-command
))
407 ;; Get an ex-token which is either an address or a command.
408 ;; A token has a type, \(command, address, end-mark\), and a value
409 (defun viper-get-ex-token ()
410 (save-window-excursion
411 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
412 (set-buffer viper-ex-work-buf
)
413 (skip-chars-forward " \t|")
414 (let ((case-fold-search t
))
415 (cond ((looking-at "#")
416 (setq ex-token-type
'command
)
417 (setq ex-token
(char-to-string (following-char)))
419 ((looking-at "[a-z]") (viper-get-ex-com-subr))
422 (setq ex-token-type
'dot
))
423 ((looking-at "[0-9]")
425 (re-search-forward "[0-9]*")
427 (cond ((eq ex-token-type
'plus
) 'add-number
)
428 ((eq ex-token-type
'minus
) 'sub-number
)
431 (string-to-number (buffer-substring (point) (mark t
)))))
434 (setq ex-token-type
'end
))
437 (setq ex-token-type
'whole
))
439 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
443 (setq ex-token-type
'plus
))
444 ((looking-at "+[0-9]")
446 (setq ex-token-type
'plus
))
448 (error viper-BadAddress
))))
450 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
454 (setq ex-token-type
'minus
))
455 ((looking-at "-[0-9]")
457 (setq ex-token-type
'minus
))
459 (error viper-BadAddress
))))
464 (while (and (not (eolp)) cont
)
465 ;;(re-search-forward "[^/]*/")
466 (re-search-forward "[^/]*\\(/\\|\n\\)")
467 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
470 (setq ex-token
(buffer-substring (point) (mark t
)))
471 (if (looking-at "/") (forward-char 1))
472 (setq ex-token-type
'search-forward
))
477 (while (and (not (eolp)) cont
)
478 ;;(re-search-forward "[^\\?]*\\?")
479 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
480 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
483 (if (not (looking-at "\n")) (forward-char 1))))
484 (setq ex-token-type
'search-backward
)
485 (setq ex-token
(buffer-substring (1- (point)) (mark t
))))
488 (setq ex-token-type
'comma
))
491 (setq ex-token-type
'semi-colon
))
492 ((looking-at "[!=><&~]")
493 (setq ex-token-type
'command
)
494 (setq ex-token
(char-to-string (following-char)))
497 (setq ex-token-type
'goto-mark
)
499 (cond ((looking-at "'") (setq ex-token nil
))
500 ((looking-at "[a-z]") (setq ex-token
(following-char)))
501 (t (error "Marks are ' and a-z")))
504 (setq ex-token-type
'end-mark
)
505 (setq ex-token
"goto"))
507 (error viper-BadExCommand
))))))
509 ;; Reads Ex command. Tries to determine if it has to exit because command
510 ;; is complete or invalid. If not, keeps reading command.
511 (defun ex-cmd-read-exit ()
513 (setq viper-incomplete-ex-cmd t
)
514 (let ((quit-regex1 (concat
517 "\\|" "[nN]ext[ \t]*"
530 "\\|" "[ktgjmsz][ \t]*$"
532 "\\|" "tr[ansfer \t]*"
535 "\\|" "^[ \t]*k?ma[^p]*"
542 ;; don't jump up in :s command
543 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*su.*"
544 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*s[^a-z].*"
545 "\\|" "['`][a-z][ \t]*"
546 ;; r! assumes that the next one is a shell command
547 "\\|" "\\(r\\|re\\|rea\\|read\\)[ \t]*!"
548 ;; w ! assumes that the next one is a shell command
549 "\\|" "\\(w\\|wr\\|wri\\|writ.?\\)[ \t]+!"
550 "\\|" "![ \t]*[a-zA-Z].*"
554 (save-window-excursion ;; put cursor at the end of the Ex working buffer
555 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
556 (set-buffer viper-ex-work-buf
)
557 (goto-char (point-max)))
558 (cond ((viper-looking-back quit-regex1
) (exit-minibuffer))
559 ((viper-looking-back stay-regex
) (insert " "))
560 ((viper-looking-back quit-regex2
) (exit-minibuffer))
563 ;; complete Ex command
564 (defun ex-cmd-complete ()
566 (let (save-pos dist compl-list string-to-complete completion-result
)
569 (setq dist
(skip-chars-backward "[a-zA-Z!=>&~]")
573 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
575 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+"))
576 ;; Preceding characters are not the ones allowed in an Ex command
577 ;; or we have typed past command name.
578 ;; Note: we didn't do parsing, so there can be surprises.
579 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
580 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
581 (looking-at "[^ \t\n\C-m]"))
583 (with-output-to-temp-buffer "*Completions*"
584 (display-completion-list
585 (viper-alist-to-list ex-token-alist
))))
586 ;; Preceding chars may be part of a command name
587 (setq string-to-complete
(buffer-substring save-pos
(point)))
588 (setq completion-result
589 (try-completion string-to-complete ex-token-alist
))
591 (cond ((eq completion-result t
) ; exact match--do nothing
592 (viper-tmp-insert-at-eob " (Sole completion)"))
593 ((eq completion-result nil
)
594 (viper-tmp-insert-at-eob " (No match)"))
595 (t ;; partial completion
597 (delete-region (point) (point-max))
598 (insert completion-result
)
599 (let (case-fold-search)
601 (viper-filter-alist (concat "^" completion-result
)
603 (if (> (length compl-list
) 1)
604 (with-output-to-temp-buffer "*Completions*"
605 (display-completion-list
606 (viper-alist-to-list (reverse compl-list
)))))))
611 ;; ARG is a prefix argument. If given, the ex command runs on the region
612 ;;(without the user having to specify the address :a,b
613 ;; STRING is the command to execute. If nil, then Viper asks you to enter the
615 (defun viper-ex (arg &optional string
)
620 (let* ((map (copy-keymap minibuffer-local-map
))
624 reg-beg-line reg-end-line
627 prev-token-type com-str
)
628 (viper-add-keymap viper-ex-cmd-map map
)
632 (viper-enlarge-region (mark t
) (point))
633 (if (> (point) (mark t
))
634 (setq reg-beg
(mark t
)
636 (setq reg-end
(mark t
)
640 (setq reg-beg-line
(1+ (count-lines (point-min) (point)))
642 (+ reg-beg-line
(count-lines reg-beg reg-end
) -
1)))))
644 (setq initial-str
(format "%d,%d" reg-beg-line reg-end-line
)))
648 (concat initial-str string
)
649 (viper-read-string-with-history
653 ;; no default when working on region
656 (car viper-ex-history
))
659 " [Type command to execute on current region]"))))
660 (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 (delete-region (point-min) (point-max))
665 (insert com-str
"\n")
666 (goto-char (point-min)))
667 (setq ex-token-type nil
671 (cond ((memq ex-token-type
'(command end-mark
))
672 (if address
(setq ex-addresses
(cons address ex-addresses
)))
673 (viper-deactivate-mark)
674 (let ((cmd (ex-cmd-assoc ex-token ex-token-alist
)))
676 (error "`%s': %s" ex-token viper-BadExCommand
))
678 (if (or (ex-cmd-is-mashed-with-args cmd
)
679 (ex-cmd-is-one-letter cmd
))
682 (save-window-excursion
683 (setq viper-ex-work-buf
684 (get-buffer-create viper-ex-work-buf-name
))
685 (set-buffer viper-ex-work-buf
)
686 (skip-chars-forward " \t")
687 (cond ((looking-at "|")
692 "`%s': %s" ex-token viper-SpuriousText
)))
695 ((eq ex-token-type
'non-command
)
696 (error "`%s': %s" ex-token viper-BadExCommand
))
697 ((eq ex-token-type
'whole
)
701 (cons (point-max) ex-addresses
)
702 (cons (point-max) (cons (point-min) ex-addresses
)))))
703 ((eq ex-token-type
'comma
)
704 (if (eq prev-token-type
'whole
)
705 (setq address
(point-min)))
707 (cons (if (null address
) (point) address
) ex-addresses
)))
708 ((eq ex-token-type
'semi-colon
)
709 (if (eq prev-token-type
'whole
)
710 (setq address
(point-min)))
711 (if address
(setq dot address
))
713 (cons (if (null address
) (point) address
) ex-addresses
)))
714 (t (let ((ans (viper-get-ex-address-subr address dot
)))
715 (if ans
(setq address ans
)))))
716 (setq prev-token-type ex-token-type
))))
719 ;; Get a regular expression and set `ex-variant', if found
720 ;; Viper doesn't parse the substitution or search patterns.
721 ;; In particular, it doesn't expand ~ into the last substitution.
722 (defun viper-get-ex-pat ()
723 (save-window-excursion
724 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
725 (set-buffer viper-ex-work-buf
)
726 (skip-chars-forward " \t")
728 ;; this is probably a variant command r!
730 (setq ex-g-variant
(not ex-g-variant
)
731 ex-g-flag
(not ex-g-flag
))
733 (skip-chars-forward " \t")))
734 (let ((c (following-char)))
735 (cond ((string-match "[0-9A-Za-z]" (format "%c" c
))
737 "Global regexp must be inside matching non-alphanumeric chars"))
738 ((= c ??
) (error "`?' is not an allowed pattern delimiter here")))
739 (if (looking-at "[^\\\\\n]")
744 ;; the use of eobp instead of eolp permits the use of newlines in
745 ;; pat2 in s/pat1/pat2/
746 (while (and (not (eobp)) cont
)
747 (if (not (re-search-forward (format "[^%c]*%c" c c
) nil t
))
748 (if (member ex-token
'("global" "vglobal"))
749 (error "Missing closing delimiter for global regexp")
750 (goto-char (point-max))))
751 (if (not (viper-looking-back
752 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c
)))
754 ;; we are at an escaped delimiter: unescape it and continue
755 (delete-backward-char 2)
758 ;; if at eol, exit loop and go to next line
759 ;; later, delim will be inserted at the end
765 (if (= (mark t
) (point)) ""
766 (buffer-substring (1- (point)) (mark t
))))
768 ;; if the user didn't insert the final pattern delimiter, we're
769 ;; at newline now. In this case, insert the initial delimiter
770 ;; specified in variable c
779 ;; Get an Ex option g or c
780 (defun viper-get-ex-opt-gc (c)
781 (save-window-excursion
782 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
783 (set-buffer viper-ex-work-buf
)
784 (if (looking-at (format "%c" c
)) (forward-char 1))
785 (skip-chars-forward " \t")
786 (cond ((looking-at "g")
796 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
797 (defun viper-default-ex-addresses (&optional whole-flag
)
798 (cond ((null ex-addresses
)
801 (list (point-max) (point-min))
802 (list (point) (point)))))
803 ((null (cdr ex-addresses
))
805 (cons (car ex-addresses
) ex-addresses
)))))
807 ;; Get an ex-address as a marker and set ex-flag if a flag is found
808 (defun viper-get-ex-address ()
809 (let ((address (point-marker))
815 (cond ((eq ex-token-type
'command
)
816 (if (member ex-token
'("print" "list" "#"))
820 (error "Address expected in this Ex command")))
821 ((eq ex-token-type
'end-mark
)
823 ((eq ex-token-type
'whole
)
824 (error "Trailing address expected"))
825 ((eq ex-token-type
'comma
)
826 (error "`%s': %s" ex-token viper-SpuriousText
))
827 (t (let ((ans (viper-get-ex-address-subr address
(point-marker))))
828 (if ans
(setq address ans
))))))
831 ;; Returns an address as a point
832 (defun viper-get-ex-address-subr (old-address dot
)
834 (if (null old-address
) (setq old-address dot
))
835 (cond ((eq ex-token-type
'dot
)
837 ((eq ex-token-type
'add-number
)
839 (goto-char old-address
)
840 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
841 (setq address
(point-marker))))
842 ((eq ex-token-type
'sub-number
)
844 (goto-char old-address
)
845 (forward-line (- ex-token
))
846 (setq address
(point-marker))))
847 ((eq ex-token-type
'abs-number
)
849 (goto-char (point-min))
850 (if (= ex-token
0) (setq address
0)
851 (forward-line (1- ex-token
))
852 (setq address
(point-marker)))))
853 ((eq ex-token-type
'end
)
855 (goto-char (1- (point-max)))
856 (setq address
(point-marker))))
857 ((eq ex-token-type
'plus
) t
) ; do nothing
858 ((eq ex-token-type
'minus
) t
) ; do nothing
859 ((eq ex-token-type
'search-forward
)
861 (ex-search-address t
)
862 (setq address
(point-marker))))
863 ((eq ex-token-type
'search-backward
)
865 (ex-search-address nil
)
866 (setq address
(point-marker))))
867 ((eq ex-token-type
'goto-mark
)
870 (exchange-point-and-mark)
872 (viper-register-to-point
873 (viper-int-to-char (1+ (- ex-token ?a
))) 'enforce-buffer
)))
874 (setq address
(point-marker)))))
878 ;; Search pattern and set address
879 ;; Doesn't wrap around. Should it?
880 (defun ex-search-address (forward)
881 (if (string= ex-token
"")
882 (if (null viper-s-string
)
883 (error viper-NoPrevSearch
)
884 (setq ex-token viper-s-string
))
885 (setq viper-s-string ex-token
))
889 (re-search-forward ex-token
))
891 (re-search-backward ex-token
)))
893 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
894 (defun viper-get-ex-buffer ()
898 (save-window-excursion
899 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
900 (set-buffer viper-ex-work-buf
)
901 (skip-chars-forward " \t")
902 (if (looking-at "[a-zA-Z]")
904 (setq ex-buffer
(following-char))
906 (skip-chars-forward " \t")))
907 (if (looking-at "[0-9]")
910 (re-search-forward "[0-9][0-9]*")
911 (setq ex-count
(string-to-number (buffer-substring (point) (mark t
))))
912 (skip-chars-forward " \t")))
913 (if (looking-at "[pl#]")
917 (if (not (looking-at "[\n|]"))
918 (error "`%s': %s" ex-token viper-SpuriousText
))))
920 (defun viper-get-ex-count ()
924 (save-window-excursion
925 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
926 (set-buffer viper-ex-work-buf
)
927 (skip-chars-forward " \t")
932 (skip-chars-forward " \t")
933 (if (looking-at "[0-9]")
936 (re-search-forward "[0-9][0-9]*")
937 (setq ex-count
(string-to-number (buffer-substring (point) (mark t
))))
938 (skip-chars-forward " \t")))
939 (if (looking-at "[pl#]")
943 (if (not (looking-at "[\n|]"))
946 (point-min) (1- (point-max))) viper-BadExCommand
))))
948 ;; Expand \% and \# in ex command
949 (defun ex-expand-filsyms (cmd buf
)
953 (setq cf buffer-file-name
)
954 (setq pf
(ex-next nil t
))) ; this finds alternative file name
955 (if (and (null cf
) (string-match "[^\\]%\\|\\`%" cmd
))
956 (error "No current file to substitute for `%%'"))
957 (if (and (null pf
) (string-match "[^\\]#\\|\\`#" cmd
))
958 (error "No alternate file to substitute for `#'"))
960 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
963 (goto-char (point-min))
964 (while (re-search-forward "%\\|#" nil t
)
965 (let ((data (match-data))
966 (char (buffer-substring (match-beginning 0) (match-end 0))))
967 (if (viper-looking-back (concat "\\\\" char
))
969 (store-match-data data
)
970 (if (string= char
"%")
972 (replace-match pf
)))))
974 (setq ret
(buffer-substring (point-min) (point)))
978 ;; Get a file name and set `ex-variant', `ex-append' and `ex-offset' if found
979 ;; If it is r!, then get the command name and whatever args
980 (defun viper-get-ex-file ()
989 (save-window-excursion
990 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
991 (set-buffer viper-ex-work-buf
)
992 (skip-chars-forward " \t")
994 (if (and (not (viper-looking-back "[ \t]"))
995 ;; read doesn't have a corresponding :r! form, so ! is
996 ;; immediately interpreted as a shell command.
997 (not (string= ex-token
"read")))
1001 (skip-chars-forward " \t"))
1004 (skip-chars-forward " \t")))
1005 (if (looking-at ">>")
1010 (skip-chars-forward " \t")))
1011 (if (looking-at "+")
1015 (re-search-forward "[ \t\n]")
1017 (setq ex-offset
(buffer-substring (point) (mark t
)))
1019 (skip-chars-forward " \t")))
1020 ;; this takes care of :r, :w, etc., when they get file names
1021 ;; from the history list
1022 (if (member ex-token
'("read" "write" "edit" "visual" "next"))
1024 (setq ex-file
(buffer-substring (point) (1- (point-max))))
1026 ;; For :e, match multiple non-white strings separated
1027 ;; by white. For others, find the first non-white string
1029 (if (string= ex-token
"edit")
1030 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
1034 ;; if file name comes from history, don't leave
1035 ;; minibuffer when the user types space
1036 (setq viper-incomplete-ex-cmd nil
)
1037 (setq ex-cmdfile-args
1038 (substring ex-file
(match-end 0) nil
))
1039 ;; this must be the last clause in this progn
1040 (substring ex-file
(match-beginning 0) (match-end 0))
1043 ;; this leaves only the command name in the work area
1044 ;; file names are gone
1045 (delete-region (point) (1- (point-max)))
1047 (goto-char (point-max))
1048 (skip-chars-backward " \t\n")
1049 (setq prompt
(buffer-substring (point-min) (point)))
1052 (setq viper-last-ex-prompt prompt
)
1054 ;; If we just finished reading command, redisplay prompt
1055 (if viper-incomplete-ex-cmd
1056 (setq ex-file
(viper-ex-read-file-name (format ":%s " prompt
)))
1057 ;; file was typed in-line
1058 (setq ex-file
(or ex-file
"")))
1062 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
1063 ;; file names, arranges to re-enter the minibuffer.
1064 (defun viper-complete-filename-or-exit ()
1066 (setq viper-keep-reading-filename t
)
1067 ;; don't exit if directory---ex-commands don't
1068 (cond ((ex-cmd-accepts-multiple-files-p ex-token
) (exit-minibuffer))
1069 ;; apparently the argument to an Ex command is
1070 ;; supposed to be a shell command
1071 ((viper-looking-back "^[ \t]*!.*")
1075 (setq ex-cmdfile nil
)
1076 (minibuffer-complete-word))))
1078 (defun viper-handle-! ()
1081 (buffer-string) (viper-abbreviate-file-name default-directory
))
1082 (member ex-token
'("read" "write")))
1086 (defun ex-cmd-accepts-multiple-files-p (token)
1087 (member token
'("edit" "next" "Next")))
1089 ;; Read file name from the minibuffer in an ex command.
1090 ;; If user doesn't enter anything, then "" is returned, i.e., the
1091 ;; prompt-directory is not returned.
1092 (defun viper-ex-read-file-name (prompt)
1094 (minibuffer-local-completion-map
1095 (copy-keymap minibuffer-local-completion-map
))
1098 (viper-add-keymap ex-read-filename-map
1099 (if (featurep 'emacs
)
1100 minibuffer-local-completion-map
1101 read-file-name-map
))
1103 (setq cont
(setq viper-keep-reading-filename t
))
1105 (setq viper-keep-reading-filename nil
1106 val
(read-file-name (concat prompt str
) nil default-directory
))
1107 (setq val
(expand-file-name val
))
1108 (if (and (string-match " " val
)
1109 (ex-cmd-accepts-multiple-files-p ex-token
))
1110 (setq val
(concat "\"" val
"\"")))
1111 (setq str
(concat str
(if (equal val
"") "" " ")
1112 val
(if (equal val
"") "" " ")))
1114 ;; Only edit, next, and Next commands accept multiple files.
1115 ;; viper-keep-reading-filename is set in the anonymous function that is
1116 ;; bound to " " in ex-read-filename-map.
1117 (setq cont
(and viper-keep-reading-filename
1118 (ex-cmd-accepts-multiple-files-p ex-token
)))
1121 (setq beg
(string-match "[^ \t]" str
) ; delete leading blanks
1122 end
(string-match "[ \t]*$" str
)) ; delete trailing blanks
1123 (if (member ex-token
'("read" "write"))
1124 (if (string-match "[\t ]*!" str
)
1125 ;; this is actually a shell command
1129 (setq viper-last-ex-prompt
1130 (concat viper-last-ex-prompt
" !")))))
1131 (substring str
(or beg
0) end
)))
1134 (defun viper-undisplayed-files ()
1137 (if (null (get-buffer-window b
))
1138 (let ((f (buffer-file-name b
)))
1140 (if ex-cycle-through-non-files
1141 (let ((s (buffer-name b
)))
1142 (if (string= " " (substring s
0 1))
1151 (let ((l (viper-undisplayed-files))
1154 (while (not (null l
))
1156 (setq args
(format "%s %d) %s\n" args file-count
(car l
))
1157 file-count
(1+ file-count
)))
1159 (if (string= args
"")
1160 (message "All files are already displayed")
1162 (save-window-excursion
1163 (with-output-to-temp-buffer " *viper-info*"
1164 (princ "\n\nThese files are not displayed in any window.\n")
1165 (princ "\n=============\n")
1167 (princ "\n=============\n")
1168 (princ "\nThe numbers can be given as counts to :next. ")
1169 (princ "\n\nPress any key to continue...\n\n"))
1170 (viper-read-event))))))
1172 ;; Ex cd command. Default directory of this buffer changes
1175 (if (string= ex-file
"")
1177 (setq default-directory
(file-name-as-directory (expand-file-name ex-file
))))
1179 ;; Ex copy and move command. DEL-FLAG means delete
1180 (defun ex-copy (del-flag)
1181 (viper-default-ex-addresses)
1182 (let ((address (viper-get-ex-address))
1183 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1187 (viper-enlarge-region (mark t
) (point))
1189 (kill-region (point) (mark t
))
1190 (copy-region-as-kill (point) (mark t
)))
1193 (with-output-to-temp-buffer " *copy text*"
1195 (if (or del-flag ex-g-flag ex-g-variant
)
1197 (buffer-substring (point) (mark t
)))))
1200 (read-string "[Hit return to confirm] ")
1201 (save-excursion (kill-buffer " *copy text*")))
1202 (quit (save-excursion (kill-buffer " *copy text*"))
1203 (signal 'quit nil
))))))
1205 (goto-char (point-min))
1208 (insert (current-kill 0))))
1210 ;; Ex delete command
1212 (viper-default-ex-addresses)
1213 (viper-get-ex-buffer)
1214 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1215 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1217 (viper-enlarge-region beg end
)
1218 (exchange-point-and-mark)
1222 (forward-line (1- ex-count
)))
1224 (viper-enlarge-region (point) (mark t
))
1226 ;; show text to be deleted and ask for confirmation
1228 (with-output-to-temp-buffer " *delete text*"
1229 (princ (buffer-substring (point) (mark t
))))
1231 (read-string "[Hit return to confirm] ")
1233 (save-excursion (kill-buffer " *delete text*"))
1234 (error "Viper bell")))
1235 (save-excursion (kill-buffer " *delete text*")))
1237 (cond ((viper-valid-register ex-buffer
'(Letter))
1238 (viper-append-to-register
1239 (downcase ex-buffer
) (point) (mark t
)))
1240 ((viper-valid-register ex-buffer
)
1241 (copy-to-register ex-buffer
(point) (mark t
) nil
))
1242 (t (error viper-InvalidRegister ex-buffer
))))
1243 (kill-region (point) (mark t
))))))
1248 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1249 ;; asked if current buffer should really be discarded.
1250 ;; This command can take multiple file names. It replaces the current buffer
1251 ;; with the first file in its argument list
1252 (defun ex-edit (&optional file
)
1254 (viper-get-ex-file))
1255 (cond ((and (string= ex-file
"") buffer-file-name
)
1256 (setq ex-file
(viper-abbreviate-file-name (buffer-file-name))))
1257 ((string= ex-file
"")
1258 (error viper-NoFileSpecified
)))
1261 (if buffer-file-name
1262 (cond ((buffer-modified-p)
1264 (format "Buffer %s is modified. Discard changes? "
1267 ((not (verify-visited-file-modtime (current-buffer)))
1269 (format "File %s changed on disk. Reread from disk? "
1272 (t (setq do-edit nil
))))
1275 (if (yes-or-no-p msg
)
1277 (set-buffer-modified-p nil
)
1278 (kill-buffer (current-buffer)))
1279 (message "Buffer %s was left intact" (buffer-name))))
1282 (if (null (setq file
(get-file-buffer ex-file
)))
1284 ;; this also does shell-style globbing
1286 ;; replace # and % with the previous/current file
1287 (ex-expand-filsyms ex-file
(current-buffer)))
1288 (or (eq major-mode
'dired-mode
)
1289 (viper-change-state-to-vi))
1290 (goto-char (point-min)))
1291 (switch-to-buffer file
))
1294 (save-window-excursion
1295 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1296 (set-buffer viper-ex-work-buf
)
1297 (delete-region (point-min) (point-max))
1298 (insert ex-offset
"\n")
1299 (goto-char (point-min)))
1300 (goto-char (viper-get-ex-address))
1301 (beginning-of-line)))
1302 (ex-fixup-history viper-last-ex-prompt ex-file
))
1304 ;; Find-file FILESPEC if it appears to specify a single file.
1305 ;; Otherwise, assume that FILESPEC is a wildcard.
1306 ;; In this case, split it into substrings separated by newlines.
1307 ;; Each line is assumed to be a file name.
1308 (defun ex-find-file (filespec)
1309 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1310 (cond ((file-exists-p filespec
) (find-file filespec
))
1311 ((string-match nonstandard-filename-chars filespec
)
1312 (mapcar 'find-file
(funcall viper-glob-function filespec
)))
1313 (t (find-file filespec
)))
1317 ;; Ex global command
1318 ;; This is executed in response to:
1319 ;; :global "pattern" ex-command
1320 ;; :vglobal "pattern" ex-command
1321 ;; :global executes ex-command on all lines matching <pattern>
1322 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1324 ;; With VARIANT nil, this functions executes :global
1325 ;; With VARIANT t, executes :vglobal
1326 (defun ex-global (variant)
1327 (let ((gcommand ex-token
))
1328 (if (or ex-g-flag ex-g-variant
)
1329 (error "`%s' within `global' is not allowed" gcommand
)
1337 (error "`%s': Missing regular expression" gcommand
)))
1339 (if (string= ex-token
"")
1340 (if (null viper-s-string
)
1341 (error viper-NoPrevSearch
)
1342 (setq ex-g-pat viper-s-string
))
1343 (setq ex-g-pat ex-token
1344 viper-s-string ex-token
))
1345 (if (null ex-addresses
)
1346 (setq ex-addresses
(list (point-max) (point-min)))
1347 (viper-default-ex-addresses))
1348 (setq ex-g-marks nil
)
1349 (let ((mark-count 0)
1350 (end (car ex-addresses
))
1351 (beg (car (cdr ex-addresses
)))
1353 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1355 (viper-enlarge-region beg end
)
1356 (exchange-point-and-mark)
1357 (let ((cont t
) (limit (point-marker)))
1358 (exchange-point-and-mark)
1359 ;; skip the last line if empty
1361 (if (eobp) (viper-backward-char-carefully))
1362 (while (and cont
(not (bobp)) (>= (point) limit
))
1366 (let ((found (re-search-backward ex-g-pat
(mark t
) t
)))
1367 (if (or (and ex-g-flag found
)
1368 (and ex-g-variant
(not found
)))
1371 (setq mark-count
(1+ mark-count
))
1372 (setq ex-g-marks
(cons (point-marker) ex-g-marks
)))))
1374 (if (bobp) (setq cont nil
)
1377 (save-window-excursion
1378 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1379 (set-buffer viper-ex-work-buf
)
1380 ;; com-str is the command string, i.e., g/pattern/ or v/pattern'
1381 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
1383 (goto-char (car ex-g-marks
))
1384 (viper-ex nil com-str
)
1385 (setq mark-count
(1- mark-count
))
1386 (setq ex-g-marks
(cdr ex-g-marks
)))))
1390 (if (null ex-addresses
)
1391 (setq ex-addresses
(cons (point) nil
)))
1392 (push-mark (point) t
)
1393 (goto-char (car ex-addresses
))
1397 ;; Ex line commands. COM is join, shift-right or shift-left
1398 (defun ex-line (com)
1399 (viper-default-ex-addresses)
1400 (viper-get-ex-count)
1401 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
1402 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1404 (viper-enlarge-region beg end
)
1405 (exchange-point-and-mark)
1409 (forward-line ex-count
)))
1411 ;; show text to be joined and ask for confirmation
1413 (with-output-to-temp-buffer " *join text*"
1414 (princ (buffer-substring (point) (mark t
))))
1417 (read-string "[Hit return to confirm] ")
1418 (ex-line-subr com
(point) (mark t
)))
1420 (save-excursion (kill-buffer " *join text*")))
1421 (ex-line-subr com
(point) (mark t
)))
1422 (setq point
(point)))
1423 (goto-char (1- point
))
1424 (beginning-of-line)))
1426 (defun ex-line-subr (com beg end
)
1427 (cond ((string= com
"join")
1428 (goto-char (min beg end
))
1429 (while (and (not (eobp)) (< (point) (max beg end
)))
1431 (if (and (<= (point) (max beg end
)) (not (eobp)))
1434 (delete-region (point) (1- (point)))
1435 (if (not ex-variant
) (fixup-whitespace))))))
1436 ((or (string= com
"right") (string= com
"left"))
1438 (min beg end
) (max beg end
)
1439 (if (string= com
"right") viper-shift-width
(- viper-shift-width
)))
1440 (goto-char (max beg end
))
1442 (viper-forward-char-carefully))))
1446 ;; Sets the mark to the current point.
1447 ;; If name is omitted, get the name straight from the work buffer."
1448 (defun ex-mark (&optional name
)
1450 (if (null ex-addresses
)
1452 (cons (point) nil
)))
1454 (if (eq 1 (length name
))
1455 (setq char
(string-to-char name
))
1456 (error "`%s': Spurious text \"%s\" after mark name"
1457 name
(substring name
1)))
1458 (save-window-excursion
1459 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1460 (set-buffer viper-ex-work-buf
)
1461 (skip-chars-forward " \t")
1462 (if (looking-at "[a-z]")
1464 (setq char
(following-char))
1466 (skip-chars-forward " \t")
1467 (if (not (looking-at "[\n|]"))
1468 (error "`%s': %s" ex-token viper-SpuriousText
)))
1469 (error "`%s' requires a following letter" ex-token
))))
1471 (goto-char (car ex-addresses
))
1472 (point-to-register (viper-int-to-char (1+ (- char ?a
)))))))
1476 ;; Alternate file is the file next to the first one in the buffer ring
1477 (defun ex-next (cycle-other-window &optional find-alt-file
)
1480 (if (not find-alt-file
)
1483 (if (or (char-or-string-p ex-offset
)
1484 (and (not (string= "" ex-file
))
1485 (not (string-match "^[0-9]+$" ex-file
))))
1488 (throw 'ex-edit nil
))
1489 (setq count
(string-to-number ex-file
))
1490 (if (= count
0) (setq count
1))
1491 (if (< count
0) (error "Usage: `next <count>' (count >= 0)"))))
1493 (setq l
(viper-undisplayed-files))
1495 (while (and (not (null l
)) (null (car l
)))
1497 (setq count
(1- count
))
1500 (if find-alt-file
(car l
)
1502 (if (and (car l
) (get-file-buffer (car l
)))
1503 (let* ((w (if cycle-other-window
1504 (get-lru-window) (selected-window)))
1505 (b (window-buffer w
)))
1506 (set-window-buffer w
(get-file-buffer (car l
)))
1508 ;; this puts "next <count>" in the ex-command history
1509 (ex-fixup-history viper-last-ex-prompt ex-file
))
1510 (error "Not that many undisplayed files")))))))
1513 (defun ex-next-related-buffer (direction &optional no-recursion
)
1515 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction
)
1517 (let ((file-or-buffer-name
1518 (viper-current-ring-item viper-related-files-and-buffers-ring
))
1519 (old-ring viper-related-files-and-buffers-ring
)
1520 (old-win (selected-window))
1523 (or (and (ring-p viper-related-files-and-buffers-ring
)
1524 (> (ring-length viper-related-files-and-buffers-ring
) 0))
1525 (error "This buffer has no related files or buffers"))
1527 (or (stringp file-or-buffer-name
)
1529 "File and buffer names must be strings, %S" file-or-buffer-name
))
1531 (setq buf
(cond ((get-buffer file-or-buffer-name
))
1532 ((file-exists-p file-or-buffer-name
)
1533 (find-file-noselect file-or-buffer-name
))
1536 (if (not (viper-buffer-live-p buf
))
1537 (error "Didn't find buffer %S or file %S"
1539 (viper-abbreviate-file-name
1540 (expand-file-name file-or-buffer-name
))))
1542 (if (equal buf
(current-buffer))
1547 (ex-next-related-buffer direction
'norecursion
))))
1552 (if (setq wind
(viper-get-visible-buffer-window buf
))
1554 (setq wind
(get-lru-window (if (featurep 'xemacs
) nil
'visible
)))
1555 (set-window-buffer wind buf
))
1557 (if (viper-window-display-p)
1559 (raise-frame (window-frame wind
))
1560 (if (equal (window-frame wind
) (window-frame old-win
))
1561 (save-window-excursion (select-window wind
) (sit-for 1))
1562 (select-window wind
)))
1563 (save-window-excursion (select-window wind
) (sit-for 1)))
1567 (setq viper-related-files-and-buffers-ring old-ring
))
1569 (setq viper-local-search-start-marker
(point-marker))
1574 (defun ex-preserve ()
1575 (message "Autosaving all buffers that need to be saved...")
1580 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
1581 (viper-get-ex-buffer)
1582 (setq viper-use-register ex-buffer
)
1584 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
1586 ;; Ex print working directory
1588 (message "%s" default-directory
))
1592 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1594 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1595 (set-buffer viper-ex-work-buf
)
1596 (if (looking-at "!") (forward-char 1)))
1597 (if (< viper-expert-level
3)
1598 (save-buffers-kill-emacs)
1599 (kill-buffer (current-buffer))))
1603 ;; ex-read doesn't support wildcards, because file completion is a better
1604 ;; mechanism. We also don't support # and % (except in :r <shell-command>
1605 ;; because file history is a better mechanism.
1608 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
1611 (viper-add-newline-at-eob-if-necessary)
1612 (if (not (or (bobp) (eobp))) (forward-line 1))
1613 (if (and (not ex-variant
) (string= ex-file
""))
1615 (if (null buffer-file-name
)
1616 (error viper-NoFileSpecified
))
1617 (setq ex-file buffer-file-name
)))
1621 ;; replace # and % with the previous/current file
1623 (concat (shell-quote-argument ex-file
) ex-cmdfile-args
)
1625 (shell-command command t
))
1626 (insert-file-contents ex-file
)))
1627 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args
))
1629 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1630 (defun ex-fixup-history (&rest args
)
1631 (setq viper-ex-history
1632 (cons (mapconcat 'identity args
" ") (cdr viper-ex-history
))))
1635 ;; Ex recover from emacs \#file\#
1636 (defun ex-recover ()
1638 (if (or ex-append ex-offset
)
1639 (error "`recover': %s" viper-SpuriousText
))
1640 (if (string= ex-file
"")
1642 (if (null buffer-file-name
)
1643 (error "This buffer isn't visiting any file"))
1644 (setq ex-file buffer-file-name
))
1645 (setq ex-file
(expand-file-name ex-file
)))
1646 (if (and (not (string= ex-file
(buffer-file-name)))
1649 (error "No write since last change \(:rec! overrides\)"))
1650 (recover-file ex-file
))
1652 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1655 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1658 ;; read variable name for ex-set
1659 (defun ex-set-read-variable ()
1660 (let ((minibuffer-local-completion-map
1661 (copy-keymap minibuffer-local-completion-map
))
1662 (cursor-in-echo-area t
)
1665 minibuffer-local-completion-map
" " 'minibuffer-complete-and-exit
)
1666 (define-key minibuffer-local-completion-map
"=" 'exit-minibuffer
)
1667 (if (viper-set-unread-command-events
1668 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil
"\C-m"))
1671 (viper-set-unread-command-events ?\C-m
)))
1672 (message ":set <Variable> [= <Value>]")
1673 (or batch
(sit-for 2))
1675 (while (string-match "^[ \\t\\n]*$"
1677 (completing-read ":set " ex-variable-alist
)))
1678 (message ":set <Variable> [= <Value>]")
1679 ;; if there are unread events, don't wait
1680 (or (viper-set-unread-command-events "") (sit-for 2))
1686 (let ((var (ex-set-read-variable))
1690 (auto-cmd-label "; don't touch or else...")
1691 (delete-turn-on-auto-fill-pattern
1692 "([ \t]*add-hook[ \t]+'viper-insert-state-hook[ \t]+'turn-on-auto-fill.*)")
1693 actual-lisp-cmd lisp-cmd-del-pattern
1696 (cond ((string= var
"all")
1697 (setq ask-if-save nil
1699 ((member var
'("ai" "autoindent"))
1700 (setq var
"viper-auto-indent"
1704 ((member var
'("ai-g" "autoindent-global"))
1705 (kill-local-variable 'viper-auto-indent
)
1706 (setq var
"viper-auto-indent"
1707 set-cmd
"setq-default"
1709 ((member var
'("noai" "noautoindent"))
1710 (setq var
"viper-auto-indent"
1713 ((member var
'("noai-g" "noautoindent-global"))
1714 (kill-local-variable 'viper-auto-indent
)
1715 (setq var
"viper-auto-indent"
1716 set-cmd
"setq-default"
1718 ((member var
'("ic" "ignorecase"))
1719 (setq var
"viper-case-fold-search"
1721 ((member var
'("noic" "noignorecase"))
1722 (setq var
"viper-case-fold-search"
1724 ((member var
'("ma" "magic"))
1725 (setq var
"viper-re-search"
1727 ((member var
'("noma" "nomagic"))
1728 (setq var
"viper-re-search"
1730 ((member var
'("ro" "readonly"))
1731 (setq var
"buffer-read-only"
1733 ((member var
'("noro" "noreadonly"))
1734 (setq var
"buffer-read-only"
1736 ((member var
'("sm" "showmatch"))
1737 (setq var
"blink-matching-paren"
1739 ((member var
'("nosm" "noshowmatch"))
1740 (setq var
"blink-matching-paren"
1742 ((member var
'("ws" "wrapscan"))
1743 (setq var
"viper-search-wrap-around"
1745 ((member var
'("nows" "nowrapscan"))
1746 (setq var
"viper-search-wrap-around"
1748 (if (and set-cmd
(eq val
0)) ; value must be set by the user
1749 (let ((cursor-in-echo-area t
))
1750 (message ":set %s = <Value>" var
)
1751 ;; if there are unread events, don't wait
1752 (or (viper-set-unread-command-events "") (sit-for 2))
1753 (setq val
(read-string (format ":set %s = " var
)))
1754 (ex-fixup-history "set" orig-var val
)
1756 ;; check numerical values
1760 "ts-g" "tabstop-global"
1763 (or (numberp (setq val2
(car (read-from-string val
))))
1764 (error "%s: Invalid value, numberp, %S" var val
))
1766 (error "%s: Invalid value, numberp, %S" var val
))))
1769 ((member var
'("sw" "shiftwidth"))
1770 (setq var
"viper-shift-width"))
1771 ((member var
'("ts" "tabstop"))
1772 ;; make it take effect in curr buff and new bufs
1773 (setq var
"tab-width"
1776 ((member var
'("ts-g" "tabstop-global"))
1777 (kill-local-variable 'tab-width
)
1778 (setq var
"tab-width"
1779 set-cmd
"setq-default"))
1780 ((member var
'("wm" "wrapmargin"))
1781 ;; make it take effect in curr buff and new bufs
1782 (kill-local-variable 'fill-column
)
1783 (setq var
"fill-column"
1784 val
(format "(- (window-width) %s)" val
)
1785 set-cmd
"setq-default"))
1786 ((member var
'("sh" "shell"))
1787 (setq var
"explicit-shell-file-name"
1788 val
(format "\"%s\"" val
)))))
1789 (ex-fixup-history "set" orig-var
))
1792 (setq actual-lisp-cmd
1793 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label
)
1794 lisp-cmd-del-pattern
1795 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1796 set-cmd var auto-cmd-label
)))
1798 (if (and ask-if-save
1799 (y-or-n-p (format "Do you want to save this setting in %s "
1800 viper-custom-file-name
)))
1802 (viper-save-string-in-file
1803 actual-lisp-cmd viper-custom-file-name
1805 lisp-cmd-del-pattern
)
1806 (if (string= var
"fill-column")
1808 (viper-save-string-in-file
1810 "(add-hook 'viper-insert-state-hook 'turn-on-auto-fill) "
1812 viper-custom-file-name
1813 delete-turn-on-auto-fill-pattern
)
1814 (viper-save-string-in-file
1815 nil viper-custom-file-name delete-turn-on-auto-fill-pattern
)
1816 (viper-save-string-in-file
1817 nil viper-custom-file-name
1819 lisp-cmd-del-pattern
)
1826 (if (string-match "^[ \t]*$" val
)
1830 (eval (car (read-from-string actual-lisp-cmd
))))
1831 (if (string= var
"fill-column")
1834 (auto-fill-mode -
1)))
1835 (if (string= var
"all") (ex-show-vars))
1838 ;; In inline args, skip regex-forw and (optionally) chars-back.
1839 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1841 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str
)
1843 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1844 (set-buffer viper-ex-work-buf
)
1845 (goto-char (point-min))
1846 (re-search-forward regex-forw nil t
)
1849 (goto-char (point-max))
1851 (skip-chars-backward chars-back
)
1852 (skip-chars-backward " \t\n\C-m"))
1854 ;; replace SPC with `=' to suppress the special meaning SPC has
1858 (while (re-search-forward " +" nil t
)
1859 (replace-match replace-str nil t
)
1860 (viper-forward-char-carefully)))
1862 (buffer-substring beg end
))))
1869 ;; Viper help. Invokes Info
1873 (pop-to-buffer (get-buffer-create "*info*"))
1874 (info (if (featurep 'xemacs
) "viper.info" "viper"))
1875 (message "Type `i' to search for a specific topic"))
1877 (with-output-to-temp-buffer " *viper-info*"
1879 The Info file for Viper does not seem to be installed.
1881 This file is part of the standard distribution of %sEmacs.
1882 Please contact your system administrator. "
1883 (if (featurep 'xemacs
) "X" "")
1886 ;; Ex source command. Loads the file specified as argument or `~/.viper'
1889 (if (string= ex-file
"")
1890 (load viper-custom-file-name
)
1893 ;; Ex substitute command
1894 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1895 (defun ex-substitute (&optional repeat r-flag
)
1899 (case-fold-search viper-case-fold-search
)
1901 (if repeat
(setq ex-token nil
) (setq delim
(viper-get-ex-pat)))
1904 (setq pat
(if r-flag viper-s-string ex-reg-exp
))
1906 (error "No previous pattern to use in substitution"))
1908 delim
(string-to-char pat
)))
1909 (setq pat
(if (string= ex-token
"") viper-s-string ex-token
))
1910 (setq viper-s-string pat
1912 (setq delim
(viper-get-ex-pat))
1918 (while (viper-get-ex-opt-gc delim
)
1919 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
1920 (viper-get-ex-count)
1923 (if ex-addresses
(goto-char (car ex-addresses
)))
1925 (forward-line (1- ex-count
))
1926 (setq ex-addresses
(cons (point) (cons (mark t
) nil
))))
1927 (if (null ex-addresses
)
1928 (setq ex-addresses
(cons (point) (cons (point) nil
)))
1929 (if (null (cdr ex-addresses
))
1930 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
1932 (let ((beg (car ex-addresses
))
1933 (end (car (cdr ex-addresses
)))
1936 (viper-enlarge-region beg end
)
1937 (let ((limit (save-excursion
1938 (goto-char (max (point) (mark t
)))
1940 (goto-char (min (point) (mark t
)))
1941 (while (< (point) limit
)
1944 ;; This move allows the use of newline as the last character in
1945 ;; the substitution pattern
1946 (viper-forward-char-carefully)
1947 (setq eol-mark
(point-marker)))
1951 (while (and (not (eolp))
1952 (re-search-forward pat eol-mark t
))
1955 (viper-put-on-search-overlay (match-beginning 0)
1957 (y-or-n-p "Replace? ")))
1959 (viper-hide-search-overlay)
1960 (setq matched-pos
(point))
1961 (if (not (stringp repl
))
1962 (error "Can't perform Ex substitution: No previous replacement pattern"))
1963 (replace-match repl t
))))
1965 (viper-forward-char-carefully))
1968 "Can't repeat Ex substitution: No previous regular expression"))
1969 (if (and (re-search-forward pat eol-mark t
)
1972 (viper-put-on-search-overlay (match-beginning 0)
1974 (y-or-n-p "Replace? "))))
1976 (viper-hide-search-overlay)
1977 (setq matched-pos
(point))
1978 (if (not (stringp repl
))
1979 (error "Can't perform Ex substitution: No previous replacement pattern"))
1980 (replace-match repl t
)))
1982 ;;(viper-forward-char-carefully)
1983 (goto-char eol-mark
)
1985 (if matched-pos
(goto-char matched-pos
))
1987 (if opt-c
(message "done"))))
1992 (save-window-excursion
1993 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1994 (set-buffer viper-ex-work-buf
)
1995 (skip-chars-forward " \t")
1997 (skip-chars-forward "^ |\t\n")
1998 (setq tag
(buffer-substring (mark t
) (point))))
1999 (if (not (string= tag
"")) (setq ex-tag tag
))
2000 (viper-change-state-to-emacs)
2001 (condition-case conds
2003 (if (string= tag
"")
2005 (find-tag-other-window ex-tag
))
2006 (viper-change-state-to-vi))
2008 (viper-change-state-to-vi)
2009 (viper-message-conditions conds
)))))
2012 ;; ex-write doesn't support wildcards, because file completion is a better
2013 ;; mechanism. We also don't support # and %
2014 ;; because file history is a better mechanism.
2015 (defun ex-write (q-flag)
2016 (viper-default-ex-addresses t
)
2018 (let ((end (car ex-addresses
))
2019 (beg (car (cdr ex-addresses
)))
2020 (orig-buf (current-buffer))
2021 ;;(orig-buf-file-name (buffer-file-name))
2022 ;;(orig-buf-name (buffer-name))
2023 ;;(buff-changed-p (buffer-modified-p))
2024 temp-buf writing-same-file region
2025 file-exists writing-whole-file
)
2026 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
2029 (viper-enlarge-region beg end
)
2030 (shell-command-on-region (point) (mark t
)
2031 (concat ex-file ex-cmdfile-args
)))
2032 (if (and (string= ex-file
"") (not (buffer-file-name)))
2035 (format "Buffer %s isn't visiting any file. File to save in: "
2038 (setq writing-whole-file
(and (= (point-min) beg
) (= (point-max) end
))
2039 ex-file
(if (string= ex-file
"")
2041 (expand-file-name ex-file
)))
2042 ;; if ex-file is a directory use the file portion of the buffer file name
2043 (if (and (file-directory-p ex-file
)
2045 (not (file-directory-p buffer-file-name
)))
2047 (concat (file-name-as-directory ex-file
)
2048 (file-name-nondirectory buffer-file-name
))))
2050 (setq file-exists
(file-exists-p ex-file
)
2051 writing-same-file
(string= ex-file
(buffer-file-name)))
2053 ;; do actual writing
2054 (if (and writing-whole-file writing-same-file
)
2055 ;; saving whole buffer in visited file
2056 (if (not (buffer-modified-p))
2057 (message "(No changes need to be saved)")
2058 (viper-maybe-checkout (current-buffer))
2062 (ex-write-info file-exists ex-file
(point-min) (point-max))
2064 ;; writing to non-visited file and it already exists
2065 (if (and file-exists
(not writing-same-file
)
2067 (format "File %s exists. Overwrite? " ex-file
))))
2069 ;; writing a region or whole buffer to non-visited file
2072 (viper-enlarge-region beg end
)
2073 (setq region
(buffer-substring (point) (mark t
)))
2074 ;; create temp buffer for the region
2075 (setq temp-buf
(get-buffer-create " *ex-write*"))
2076 (set-buffer temp-buf
)
2077 (if (featurep 'xemacs
)
2078 (set-visited-file-name ex-file
)
2079 (set-visited-file-name ex-file
'noquery
))
2081 (if (and file-exists ex-append
)
2082 (insert-file-contents ex-file
))
2083 (goto-char (point-max))
2086 (viper-maybe-checkout (current-buffer))
2087 (setq selective-display nil
)
2090 file-exists ex-file
(point-min) (point-max))
2092 ;; this must be under unwind-protect so that
2093 ;; temp-buf will be deleted in case of an error
2094 (set-buffer temp-buf
)
2095 (set-buffer-modified-p nil
)
2096 (kill-buffer temp-buf
)
2097 ;; buffer/region has been written, now take care of details
2098 (set-buffer orig-buf
)))
2099 ;; set the right file modification time
2100 (if (and (buffer-file-name) writing-same-file
)
2101 (set-visited-file-modtime))
2102 ;; prevent loss of data if saving part of the buffer in visited file
2103 (or writing-whole-file
2104 (not writing-same-file
)
2107 (message "Warning: you have saved only part of the buffer!")
2108 (set-buffer-modified-p t
)))
2110 (if (< viper-expert-level
2)
2111 (save-buffers-kill-emacs)
2112 (kill-buffer (current-buffer))))
2116 (defun ex-write-info (exists file-name beg end
)
2117 (message "`%s'%s %d lines, %d characters"
2118 (viper-abbreviate-file-name file-name
)
2119 (if exists
"" " [New file]")
2120 (count-lines beg
(min (1+ end
) (point-max)))
2125 (viper-default-ex-addresses)
2126 (viper-get-ex-buffer)
2127 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2128 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
2130 (viper-enlarge-region beg end
)
2131 (exchange-point-and-mark)
2132 (if (or ex-g-flag ex-g-variant
)
2133 (error "Can't execute `yank' within `global'"))
2137 (forward-line (1- ex-count
)))
2139 (viper-enlarge-region (point) (mark t
))
2140 (if ex-flag
(error "`yank': %s" viper-SpuriousText
))
2142 (cond ((viper-valid-register ex-buffer
'(Letter))
2143 (viper-append-to-register
2144 (downcase ex-buffer
) (point) (mark t
)))
2145 ((viper-valid-register ex-buffer
)
2146 (copy-to-register ex-buffer
(point) (mark t
) nil
))
2147 (t (error viper-InvalidRegister ex-buffer
))))
2148 (copy-region-as-kill (point) (mark t
)))))
2150 ;; Execute shell command
2151 (defun ex-command ()
2153 (save-window-excursion
2154 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
2155 (set-buffer viper-ex-work-buf
)
2156 (skip-chars-forward " \t")
2157 (setq command
(buffer-substring (point) (point-max)))
2159 ;; replace # and % with the previous/current file
2160 (setq command
(ex-expand-filsyms command
(current-buffer)))
2161 (if (and (> (length command
) 0) (string= "!" (substring command
0 1)))
2162 (if viper-ex-last-shell-com
2164 (concat viper-ex-last-shell-com
(substring command
1)))
2165 (error "No previous shell command")))
2166 (setq viper-ex-last-shell-com command
)
2167 (if (null ex-addresses
)
2168 (shell-command command
)
2169 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2170 (if (null beg
) (setq beg end
))
2174 (viper-enlarge-region (point) (mark t
))
2175 (shell-command-on-region (point) (mark t
) command t
))
2178 (defun ex-compile ()
2179 "Reads args from the command line, then runs make with the args.
2180 If no args are given, then it runs the last compile command.
2181 Type 'mak ' (including the space) to run make with no args."
2183 (save-window-excursion
2184 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
2185 (set-buffer viper-ex-work-buf
)
2186 (setq args
(buffer-substring (point) (point-max)))
2188 ;; Remove the newline that may (will?) be at the end of the args
2189 (if (string= "\n" (substring args
(1- (length args
))))
2190 (setq args
(substring args
0 (1- (length args
)))))
2191 ;; Run last command if no args given, else construct a new command.
2193 (if (string= "" args
)
2194 (if (boundp 'compile-command
)
2197 (concat ex-compile-command
" " args
)))
2201 ;; Print line number
2202 (defun ex-line-no ()
2206 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
2208 ;; Give information on the file visited by the current buffer
2209 (defun viper-info-on-file ()
2211 (let ((pos1 (viper-line-pos 'start
))
2212 (pos2 (viper-line-pos 'end
))
2214 (setq lines
(count-lines (point-min) (viper-line-pos 'end
))
2215 file
(cond ((buffer-file-name)
2216 (concat (viper-abbreviate-file-name (buffer-file-name)) ":"))
2217 ((buffer-file-name (buffer-base-buffer))
2218 (concat (viper-abbreviate-file-name (buffer-file-name (buffer-base-buffer))) " (indirect buffer):"))
2219 (t (concat (buffer-name) " [Not visiting any file]:")))
2220 info
(format "line=%d/%d pos=%d/%d col=%d %s"
2224 (count-lines (point-min) (point-max))
2225 (point) (1- (point-max))
2226 (1+ (current-column))
2227 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2228 (if (< (+ 1 (length info
) (length file
))
2229 (window-width (minibuffer-window)))
2230 (message "%s" (concat file
" " info
))
2231 (save-window-excursion
2232 (with-output-to-temp-buffer " *viper-info*"
2233 (princ (concat "\n" file
"\n\n\t" info
"\n\n")))
2234 (let ((inhibit-quit t
))
2235 (viper-set-unread-command-events (viper-read-event)))
2236 (kill-buffer " *viper-info*")))
2240 ;; Without arguments displays info on file. With an arg, sets the visited file
2242 (defun ex-set-visited-file-name ()
2244 (if (string= ex-file
"")
2245 (viper-info-on-file)
2246 ;; If ex-file is a directory, use the file portion of the buffer
2247 ;; file name (like ex-write). Do this even if ex-file is a
2248 ;; non-existent directory, since set-visited-file-name signals an
2249 ;; error on this condition, too.
2250 (if (and (string= (file-name-nondirectory ex-file
) "")
2252 (not (file-directory-p buffer-file-name
)))
2253 (setq ex-file
(concat (file-name-as-directory ex-file
)
2254 (file-name-nondirectory buffer-file-name
))))
2255 (set-visited-file-name ex-file
)))
2258 ;; display all variables set through :set
2259 (defun ex-show-vars ()
2260 (with-output-to-temp-buffer " *viper-info*"
2261 (princ (if viper-auto-indent
2262 "autoindent (local)\n" "noautoindent (local)\n"))
2263 (princ (if (default-value 'viper-auto-indent
)
2264 "autoindent (global) \n" "noautoindent (global) \n"))
2265 (princ (if viper-case-fold-search
"ignorecase\n" "noignorecase\n"))
2266 (princ (if viper-re-search
"magic\n" "nomagic\n"))
2267 (princ (if buffer-read-only
"readonly\n" "noreadonly\n"))
2268 (princ (if blink-matching-paren
"showmatch\n" "noshowmatch\n"))
2269 (princ (if viper-search-wrap-around
"wrapscan\n" "nowrapscan\n"))
2270 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width
))
2271 (princ (format "tabstop (local) \t= %S\n" tab-width
))
2272 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width
)))
2273 (princ (format "wrapmargin (local) \t= %S\n"
2274 (- (window-width) fill-column
)))
2275 (princ (format "wrapmargin (global) \t= %S\n"
2276 (- (window-width) (default-value 'fill-column
))))
2277 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name
)
2278 explicit-shell-file-name
2283 (viper-default-ex-addresses)
2284 (let ((end (car ex-addresses
))
2285 (beg (car (cdr ex-addresses
))))
2286 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
2288 (viper-enlarge-region beg end
)
2289 (if (or ex-g-flag ex-g-variant
)
2290 ;; When executing a global command, collect output of each
2291 ;; print in viper-ex-print-buf.
2293 (append-to-buffer viper-ex-print-buf
(point) (mark t
))
2294 ;; Is this the last mark for the global command?
2295 (unless (cdr ex-g-marks
)
2296 (with-current-buffer viper-ex-print-buf
2297 (ex-print-display-lines (buffer-string))
2299 (ex-print-display-lines (buffer-substring (point) (mark t
)))))))
2301 (defun ex-print-display-lines (lines)
2303 ;; String doesn't contain a newline.
2304 ((not (string-match "\n" lines
))
2305 (message "%s" lines
))
2306 ;; String contains only one newline at the end. Strip it off.
2307 ((= (string-match "\n" lines
) (1- (length lines
)))
2308 (message "%s" (substring lines
0 -
1)))
2309 ;; String spans more than one line. Use a temporary buffer.
2311 (save-current-buffer
2312 (with-output-to-temp-buffer " *viper-info*"
2319 ;; arch-tag: 56b80d36-f880-4d10-bd66-85ad91a295db
2320 ;;; viper-ex.el ends here