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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
32 (defvar read-file-name-map
)
33 (defvar viper-use-register
)
34 (defvar viper-s-string
)
35 (defvar viper-shift-width
)
36 (defvar viper-ex-history
)
37 (defvar viper-related-files-and-buffers-ring
)
38 (defvar viper-local-search-start-marker
)
39 (defvar viper-expert-level
)
40 (defvar viper-custom-file-name
)
41 (defvar viper-case-fold-search
)
42 (defvar explicit-shell-file-name
)
43 (defvar compile-command
)
45 ;; loading happens only in non-interactive compilation
46 ;; in order to spare non-viperized emacs from being viperized
55 (defgroup viper-ex nil
56 "Viper support for Ex commands."
64 (defconst viper-ex-work-buf-name
" *ex-working-space*")
65 (defvar viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
66 (defconst viper-ex-tmp-buf-name
" *ex-tmp*")
67 (defconst viper-ex-print-buf-name
" *ex-print*")
68 (defvar viper-ex-print-buf
(get-buffer-create viper-ex-print-buf-name
))
73 (defun ex-cmd-obsolete (name)
74 (error "`%s': Obsolete command, not supported by Viper" name
))
76 (defun ex-cmd-not-yet (name)
77 (error "`%s': Command not implemented in Viper" name
))
79 ;; alist entries: name (in any order), command, cont(??)
80 ;; If command is a string, then that is an alias to the real command
81 ;; to execute (for instance, ":m" -> ":move").
82 ;; command attributes:
83 ;; is-mashed: the command's args may be jammed right up against the command
84 ;; one-letter: this is a one-letter token. Any text appearing after
85 ;; the name gets appended as an argument for the command
86 ;; i.e. ":kabc" gets turned into (ex-mark "abc")
87 (defconst ex-token-alist
'(
89 ("&" (ex-substitute t
))
91 (">" (ex-line "right"))
92 ("<" (ex-line "left"))
93 ("Buffer" (if ex-cycle-other-window
94 (viper-switch-to-buffer)
95 (viper-switch-to-buffer-other-window)))
96 ("Next" (ex-next (not ex-cycle-other-window
)))
97 ("PreviousRelatedFile" (ex-next-related-buffer -
1))
98 ("RelatedFile" (ex-next-related-buffer 1))
100 ("WWrite" (save-some-buffers t
))
101 ("Write" (save-some-buffers))
104 ("buffer" (if ex-cycle-other-window
105 (viper-switch-to-buffer-other-window)
106 (viper-switch-to-buffer)))
108 ;; ch should be "change" but maintain old viper compatibility
112 ("copy" (ex-copy nil
))
113 ("customize" (customize-group "viper"))
114 ("delete" (ex-delete))
116 ("file" (ex-set-visited-file-name))
118 ("global" (ex-global nil
) is-mashed
)
121 ("join" (ex-line "join"))
122 ("k" (ex-mark) one-letter
)
125 ("make" (ex-compile))
126 ; old viper doesn't specify a default for "ma" so leave it undefined
130 ("next" (ex-next ex-cycle-other-window
))
132 ("preserve" (ex-preserve))
140 ("recover" (ex-recover))
141 ("rewind" (ex-rewind))
147 ("source" (ex-source))
148 ("stop" (suspend-emacs))
149 ("sr" (ex-substitute t t
))
150 ("submitReport" (viper-submit-report))
151 ("substitute" (ex-substitute) is-mashed
)
152 ("suspend" (suspend-emacs))
155 ("transfer" (ex-copy nil
))
158 ("undo" (viper-undo))
161 ("version" (viper-version))
162 ("vglobal" (ex-global t
) is-mashed
)
166 ("write" (ex-write nil
))
169 ("~" (ex-substitute t t
))
171 ("append" (ex-cmd-obsolete "append"))
172 ("change" (ex-cmd-obsolete "change"))
173 ("insert" (ex-cmd-obsolete "insert"))
174 ("open" (ex-cmd-obsolete "open"))
176 ("list" (ex-cmd-not-yet "list"))
177 ("z" (ex-cmd-not-yet "z"))
178 ("#" (ex-cmd-not-yet "#"))
180 ("abbreviate" (error "`%s': Vi abbreviations are obsolete. Use the more powerful Emacs abbrevs" ex-token
))
181 ("unabbreviate" (error "`%s': Vi abbreviations are obsolete. Use the more powerful Emacs abbrevs" ex-token
))
184 ;; No code should touch anything in the alist entry! (other than the name,
185 ;; "car entry", of course) This way, changing this data structure
186 ;; requires changing only the following ex-cmd functions...
188 ;; Returns cmd if the command may be jammed right up against its
189 ;; arguments, nil if there must be a space.
190 ;; examples of mashable commands: g// g!// v// s// sno// sm//
191 (defun ex-cmd-is-mashed-with-args (cmd)
192 (if (eq 'is-mashed
(car (nthcdr 2 cmd
))) cmd
))
194 ;; Returns true if this is a one-letter command that may be followed
195 ;; by anything, no whitespace needed. This is a special-case for ":k".
196 (defun ex-cmd-is-one-letter (cmd)
197 (if (eq 'one-letter
(car (nthcdr 2 cmd
))) cmd
))
199 ;; Executes the function associated with the command
200 (defun ex-cmd-execute (cmd)
203 ;; If this is a one-letter magic command, splice in args.
204 (defun ex-splice-args-in-1-letr-cmd (key list
)
205 (let ((oneletter (ex-cmd-is-one-letter (assoc (substring key
0 1) list
))))
208 (append (cadr oneletter
)
209 (if (< 1 (length key
)) (list (substring key
1))))
210 (car (cdr (cdr oneletter
))) ))
214 ;; Returns the alist entry for the appropriate key.
215 ;; Tries to complete the key before using it in the alist.
216 ;; If there is no appropriate key (no match or duplicate matches) return nil
217 (defun ex-cmd-assoc (key list
)
218 (let ((entry (try-completion key list
))
221 ((eq entry t
) (assoc key list
))
222 ((stringp entry
) (or (ex-splice-args-in-1-letr-cmd key list
)
224 ((eq entry nil
) (ex-splice-args-in-1-letr-cmd key list
))
227 ;; If we end up with an alias, look up the alias...
228 (if (stringp (cadr result
))
229 (setq result
(ex-cmd-assoc (cadr result
) list
)))
230 ;; and return the corresponding alist entry
235 ;; A-list of Ex variables that can be set using the :set command.
236 (defconst ex-variable-alist
237 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
238 ("tabstop-global") ("ts-g") ("tabstop") ("ts")
239 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
241 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
242 ("noreadonly") ("noro") ("nomagic") ("noma")
243 ("noignorecase") ("noic")
244 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai")
245 ("magic") ("ma") ("ignorecase") ("ic")
246 ("autoindent-global") ("ai-g") ("autoindent") ("ai")
252 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
253 (defvar ex-token nil
)
256 ;; If non-nil, gives type of address; if nil, it is a command.
257 (defvar ex-token-type nil
)
259 ;; List of addresses passed to Ex command
260 (defvar ex-addresses nil
)
262 ;; This flag is supposed to be set only by `#', `print', and `list',
263 ;; none of which is implemented. So, it and the pices of the code it
264 ;; controls are dead weight. We keep it just in case this might be
265 ;; needed in the future.
268 ;; "buffer" where Ex commands keep deleted data.
269 ;; In Emacs terms, this is a register.
270 (defvar ex-buffer nil
)
272 ;; Value of ex count.
273 (defvar ex-count nil
)
275 ;; Flag indicating that :global Ex command is being executed.
276 (defvar ex-g-flag nil
)
277 ;; Flag indicating that :vglobal Ex command is being executed.
278 (defvar ex-g-variant nil
)
279 ;; Marks to operate on during a :global Ex command.
280 (defvar ex-g-marks nil
)
282 ;; Save reg-exp used in substitute.
283 (defvar ex-reg-exp nil
)
286 ;; Replace pattern for substitute.
289 ;; Pattern for global command.
290 (defvar ex-g-pat nil
)
292 (defcustom ex-unix-type-shell
293 (let ((case-fold-search t
))
294 (and (stringp shell-file-name
)
304 "[^a-z]sh$\\|[^a-z]sh.exe$"
309 "Is the user using a unix-type shell under a non-OS?"
313 (defcustom ex-unix-type-shell-options
314 (let ((case-fold-search t
))
315 (if ex-unix-type-shell
316 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name
)
317 "-f") ; csh: do it fast
318 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name
)
319 "-noprofile") ; bash: ignore .profile
321 "Options to pass to the Unix-style shell.
322 Don't put `-c' here, as it is added automatically."
323 :type
'(choice (const nil
) string
)
326 (defcustom ex-compile-command
"make"
327 "The command to run when the user types :make."
331 (defcustom viper-glob-function
332 (cond (ex-unix-type-shell 'viper-glob-unix-files
)
333 ((eq system-type
'emx
) 'viper-glob-mswindows-files
) ; OS/2
334 (viper-ms-style-os-p 'viper-glob-mswindows-files
) ; Microsoft OS
335 (viper-vms-os-p 'viper-glob-unix-files
) ; VMS
336 (t 'viper-glob-unix-files
) ; presumably UNIX
338 "Expand the file spec containing wildcard symbols.
339 The default tries to set this variable to work with Unix, Windows,
342 However, if it doesn't work right for some types of Unix shells or some OS,
343 the user should supply the appropriate function and set this variable to the
344 corresponding function symbol."
349 ;; Remembers the previous Ex tag.
352 ;; file used by Ex commands like :r, :w, :n
355 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
356 (defvar ex-variant nil
)
358 ;; Specified the offset of an Ex command, such as :read.
359 (defvar ex-offset nil
)
361 ;; Tells Ex that this is a w>> command.
362 (defvar ex-append nil
)
364 ;; File containing the shell command to be executed at Ex prompt,
366 (defvar ex-cmdfile nil
)
367 (defvar ex-cmdfile-args
"")
369 ;; flag used in viper-ex-read-file-name to indicate that we may be reading
370 ;; multiple file names. Used for :edit and :next
371 (defvar viper-keep-reading-filename nil
)
373 (defcustom ex-cycle-other-window t
374 "*If t, :n and :b cycles through files and buffers in other window.
375 Then :N and :B cycles in the current window. If nil, this behavior is
380 (defcustom ex-cycle-through-non-files nil
381 "*Cycle through *scratch* and other buffers that don't visit any file."
385 ;; Last shell command executed with :! command.
386 (defvar viper-ex-last-shell-com nil
)
388 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
389 (defvar viper-incomplete-ex-cmd nil
)
391 ;; Remembers the last ex-command prompt.
392 (defvar viper-last-ex-prompt
"")
395 ;; Get a complete ex command
396 (defun viper-get-ex-com-subr ()
397 (let (cmd case-fold-search
)
399 (re-search-forward "[a-zA-Z][a-zA-Z]*")
400 (setq ex-token-type
'command
)
401 (setq ex-token
(buffer-substring (point) (mark t
)))
402 (setq cmd
(ex-cmd-assoc ex-token ex-token-alist
))
404 (setq ex-token
(car cmd
))
405 (setq ex-token-type
'non-command
))
408 ;; Get an ex-token which is either an address or a command.
409 ;; A token has a type, \(command, address, end-mark\), and a value
410 (defun viper-get-ex-token ()
411 (save-window-excursion
412 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
413 (set-buffer viper-ex-work-buf
)
414 (skip-chars-forward " \t|")
415 (let ((case-fold-search t
))
416 (cond ((looking-at "#")
417 (setq ex-token-type
'command
)
418 (setq ex-token
(char-to-string (following-char)))
420 ((looking-at "[a-z]") (viper-get-ex-com-subr))
423 (setq ex-token-type
'dot
))
424 ((looking-at "[0-9]")
426 (re-search-forward "[0-9]*")
428 (cond ((eq ex-token-type
'plus
) 'add-number
)
429 ((eq ex-token-type
'minus
) 'sub-number
)
432 (string-to-number (buffer-substring (point) (mark t
)))))
435 (setq ex-token-type
'end
))
438 (setq ex-token-type
'whole
))
440 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
444 (setq ex-token-type
'plus
))
445 ((looking-at "+[0-9]")
447 (setq ex-token-type
'plus
))
449 (error viper-BadAddress
))))
451 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
455 (setq ex-token-type
'minus
))
456 ((looking-at "-[0-9]")
458 (setq ex-token-type
'minus
))
460 (error viper-BadAddress
))))
465 (while (and (not (eolp)) cont
)
466 ;;(re-search-forward "[^/]*/")
467 (re-search-forward "[^/]*\\(/\\|\n\\)")
468 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
471 (setq ex-token
(buffer-substring (point) (mark t
)))
472 (if (looking-at "/") (forward-char 1))
473 (setq ex-token-type
'search-forward
))
478 (while (and (not (eolp)) cont
)
479 ;;(re-search-forward "[^\\?]*\\?")
480 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
481 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
484 (if (not (looking-at "\n")) (forward-char 1))))
485 (setq ex-token-type
'search-backward
)
486 (setq ex-token
(buffer-substring (1- (point)) (mark t
))))
489 (setq ex-token-type
'comma
))
492 (setq ex-token-type
'semi-colon
))
493 ((looking-at "[!=><&~]")
494 (setq ex-token-type
'command
)
495 (setq ex-token
(char-to-string (following-char)))
498 (setq ex-token-type
'goto-mark
)
500 (cond ((looking-at "'") (setq ex-token nil
))
501 ((looking-at "[a-z]") (setq ex-token
(following-char)))
502 (t (error "Marks are ' and a-z")))
505 (setq ex-token-type
'end-mark
)
506 (setq ex-token
"goto"))
508 (error viper-BadExCommand
))))))
510 ;; Reads Ex command. Tries to determine if it has to exit because command
511 ;; is complete or invalid. If not, keeps reading command.
512 (defun ex-cmd-read-exit ()
514 (setq viper-incomplete-ex-cmd t
)
515 (let ((quit-regex1 (concat
518 "\\|" "[nN]ext[ \t]*"
531 "\\|" "[ktgjmsz][ \t]*$"
533 "\\|" "tr[ansfer \t]*"
536 "\\|" "^[ \t]*k?ma[^p]*"
543 ;; don't jump up in :s command
544 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*su.*"
545 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*s[^a-z].*"
546 "\\|" "['`][a-z][ \t]*"
547 ;; r! assumes that the next one is a shell command
548 "\\|" "\\(r\\|re\\|rea\\|read\\)[ \t]*!"
549 ;; w ! assumes that the next one is a shell command
550 "\\|" "\\(w\\|wr\\|wri\\|writ.?\\)[ \t]+!"
551 "\\|" "![ \t]*[a-zA-Z].*"
555 (save-window-excursion ;; put cursor at the end of the Ex working buffer
556 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
557 (set-buffer viper-ex-work-buf
)
558 (goto-char (point-max)))
559 (cond ((viper-looking-back quit-regex1
) (exit-minibuffer))
560 ((viper-looking-back stay-regex
) (insert " "))
561 ((viper-looking-back quit-regex2
) (exit-minibuffer))
564 ;; complete Ex command
565 (defun ex-cmd-complete ()
567 (let (save-pos dist compl-list string-to-complete completion-result
)
570 (setq dist
(skip-chars-backward "[a-zA-Z!=>&~]")
574 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
576 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+"))
577 ;; Preceding characters are not the ones allowed in an Ex command
578 ;; or we have typed past command name.
579 ;; Note: we didn't do parsing, so there can be surprises.
580 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
581 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
582 (looking-at "[^ \t\n\C-m]"))
584 (with-output-to-temp-buffer "*Completions*"
585 (display-completion-list
586 (viper-alist-to-list ex-token-alist
))))
587 ;; Preceding chars may be part of a command name
588 (setq string-to-complete
(buffer-substring save-pos
(point)))
589 (setq completion-result
590 (try-completion string-to-complete ex-token-alist
))
592 (cond ((eq completion-result t
) ; exact match--do nothing
593 (viper-tmp-insert-at-eob " (Sole completion)"))
594 ((eq completion-result nil
)
595 (viper-tmp-insert-at-eob " (No match)"))
596 (t ;; partial completion
598 (delete-region (point) (point-max))
599 (insert completion-result
)
600 (let (case-fold-search)
602 (viper-filter-alist (concat "^" completion-result
)
604 (if (> (length compl-list
) 1)
605 (with-output-to-temp-buffer "*Completions*"
606 (display-completion-list
607 (viper-alist-to-list (reverse compl-list
)))))))
612 ;; ARG is a prefix argument. If given, the ex command runs on the region
613 ;;(without the user having to specify the address :a,b
614 ;; STRING is the command to execute. If nil, then Viper asks you to enter the
616 (defun viper-ex (arg &optional string
)
621 (let* ((map (copy-keymap minibuffer-local-map
))
625 reg-beg-line reg-end-line
628 prev-token-type com-str
)
629 (viper-add-keymap viper-ex-cmd-map map
)
633 (viper-enlarge-region (mark t
) (point))
634 (if (> (point) (mark t
))
635 (setq reg-beg
(mark t
)
637 (setq reg-end
(mark t
)
641 (setq reg-beg-line
(1+ (count-lines (point-min) (point)))
643 (+ reg-beg-line
(count-lines reg-beg reg-end
) -
1)))))
645 (setq initial-str
(format "%d,%d" reg-beg-line reg-end-line
)))
649 (concat initial-str string
)
650 (viper-read-string-with-history
654 ;; no default when working on region
657 (car viper-ex-history
))
660 " [Type command to execute on current region]"))))
661 (save-window-excursion
663 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
664 (set-buffer viper-ex-work-buf
)
665 (delete-region (point-min) (point-max))
666 (insert com-str
"\n")
667 (goto-char (point-min)))
668 (setq ex-token-type nil
672 (cond ((memq ex-token-type
'(command end-mark
))
673 (if address
(setq ex-addresses
(cons address ex-addresses
)))
674 (viper-deactivate-mark)
675 (let ((cmd (ex-cmd-assoc ex-token ex-token-alist
)))
677 (error "`%s': %s" ex-token viper-BadExCommand
))
679 (if (or (ex-cmd-is-mashed-with-args cmd
)
680 (ex-cmd-is-one-letter cmd
))
683 (save-window-excursion
684 (setq viper-ex-work-buf
685 (get-buffer-create viper-ex-work-buf-name
))
686 (set-buffer viper-ex-work-buf
)
687 (skip-chars-forward " \t")
688 (cond ((looking-at "|")
693 "`%s': %s" ex-token viper-SpuriousText
)))
696 ((eq ex-token-type
'non-command
)
697 (error "`%s': %s" ex-token viper-BadExCommand
))
698 ((eq ex-token-type
'whole
)
702 (cons (point-max) ex-addresses
)
703 (cons (point-max) (cons (point-min) ex-addresses
)))))
704 ((eq ex-token-type
'comma
)
705 (if (eq prev-token-type
'whole
)
706 (setq address
(point-min)))
708 (cons (if (null address
) (point) address
) ex-addresses
)))
709 ((eq ex-token-type
'semi-colon
)
710 (if (eq prev-token-type
'whole
)
711 (setq address
(point-min)))
712 (if address
(setq dot address
))
714 (cons (if (null address
) (point) address
) ex-addresses
)))
715 (t (let ((ans (viper-get-ex-address-subr address dot
)))
716 (if ans
(setq address ans
)))))
717 (setq prev-token-type ex-token-type
))))
720 ;; Get a regular expression and set `ex-variant', if found
721 ;; Viper doesn't parse the substitution or search patterns.
722 ;; In particular, it doesn't expand ~ into the last substitution.
723 (defun viper-get-ex-pat ()
724 (save-window-excursion
725 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
726 (set-buffer viper-ex-work-buf
)
727 (skip-chars-forward " \t")
729 ;; this is probably a variant command r!
731 (setq ex-g-variant
(not ex-g-variant
)
732 ex-g-flag
(not ex-g-flag
))
734 (skip-chars-forward " \t")))
735 (let ((c (following-char)))
736 (cond ((string-match "[0-9A-Za-z]" (format "%c" c
))
738 "Global regexp must be inside matching non-alphanumeric chars"))
739 ((= c ??
) (error "`?' is not an allowed pattern delimiter here")))
740 (if (looking-at "[^\\\\\n]")
745 ;; the use of eobp instead of eolp permits the use of newlines in
746 ;; pat2 in s/pat1/pat2/
747 (while (and (not (eobp)) cont
)
748 (if (not (re-search-forward (format "[^%c]*%c" c c
) nil t
))
749 (if (member ex-token
'("global" "vglobal"))
750 (error "Missing closing delimiter for global regexp")
751 (goto-char (point-max))))
752 (if (not (viper-looking-back
753 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c
)))
755 ;; we are at an escaped delimiter: unescape it and continue
756 (delete-backward-char 2)
759 ;; if at eol, exit loop and go to next line
760 ;; later, delim will be inserted at the end
766 (if (= (mark t
) (point)) ""
767 (buffer-substring (1- (point)) (mark t
))))
769 ;; if the user didn't insert the final pattern delimiter, we're
770 ;; at newline now. In this case, insert the initial delimiter
771 ;; specified in variable c
780 ;; Get an Ex option g or c
781 (defun viper-get-ex-opt-gc (c)
782 (save-window-excursion
783 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
784 (set-buffer viper-ex-work-buf
)
785 (if (looking-at (format "%c" c
)) (forward-char 1))
786 (skip-chars-forward " \t")
787 (cond ((looking-at "g")
797 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
798 (defun viper-default-ex-addresses (&optional whole-flag
)
799 (cond ((null ex-addresses
)
802 (list (point-max) (point-min))
803 (list (point) (point)))))
804 ((null (cdr ex-addresses
))
806 (cons (car ex-addresses
) ex-addresses
)))))
808 ;; Get an ex-address as a marker and set ex-flag if a flag is found
809 (defun viper-get-ex-address ()
810 (let ((address (point-marker))
816 (cond ((eq ex-token-type
'command
)
817 (if (member ex-token
'("print" "list" "#"))
821 (error "Address expected in this Ex command")))
822 ((eq ex-token-type
'end-mark
)
824 ((eq ex-token-type
'whole
)
825 (error "Trailing address expected"))
826 ((eq ex-token-type
'comma
)
827 (error "`%s': %s" ex-token viper-SpuriousText
))
828 (t (let ((ans (viper-get-ex-address-subr address
(point-marker))))
829 (if ans
(setq address ans
))))))
832 ;; Returns an address as a point
833 (defun viper-get-ex-address-subr (old-address dot
)
835 (if (null old-address
) (setq old-address dot
))
836 (cond ((eq ex-token-type
'dot
)
838 ((eq ex-token-type
'add-number
)
840 (goto-char old-address
)
841 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
842 (setq address
(point-marker))))
843 ((eq ex-token-type
'sub-number
)
845 (goto-char old-address
)
846 (forward-line (- ex-token
))
847 (setq address
(point-marker))))
848 ((eq ex-token-type
'abs-number
)
850 (goto-char (point-min))
851 (if (= ex-token
0) (setq address
0)
852 (forward-line (1- ex-token
))
853 (setq address
(point-marker)))))
854 ((eq ex-token-type
'end
)
856 (goto-char (1- (point-max)))
857 (setq address
(point-marker))))
858 ((eq ex-token-type
'plus
) t
) ; do nothing
859 ((eq ex-token-type
'minus
) t
) ; do nothing
860 ((eq ex-token-type
'search-forward
)
862 (ex-search-address t
)
863 (setq address
(point-marker))))
864 ((eq ex-token-type
'search-backward
)
866 (ex-search-address nil
)
867 (setq address
(point-marker))))
868 ((eq ex-token-type
'goto-mark
)
871 (exchange-point-and-mark)
873 (viper-register-to-point
874 (viper-int-to-char (1+ (- ex-token ?a
))) 'enforce-buffer
)))
875 (setq address
(point-marker)))))
879 ;; Search pattern and set address
880 ;; Doesn't wrap around. Should it?
881 (defun ex-search-address (forward)
882 (if (string= ex-token
"")
883 (if (null viper-s-string
)
884 (error viper-NoPrevSearch
)
885 (setq ex-token viper-s-string
))
886 (setq viper-s-string ex-token
))
890 (re-search-forward ex-token
))
892 (re-search-backward ex-token
)))
894 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
895 (defun viper-get-ex-buffer ()
899 (save-window-excursion
900 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
901 (set-buffer viper-ex-work-buf
)
902 (skip-chars-forward " \t")
903 (if (looking-at "[a-zA-Z]")
905 (setq ex-buffer
(following-char))
907 (skip-chars-forward " \t")))
908 (if (looking-at "[0-9]")
911 (re-search-forward "[0-9][0-9]*")
912 (setq ex-count
(string-to-number (buffer-substring (point) (mark t
))))
913 (skip-chars-forward " \t")))
914 (if (looking-at "[pl#]")
918 (if (not (looking-at "[\n|]"))
919 (error "`%s': %s" ex-token viper-SpuriousText
))))
921 (defun viper-get-ex-count ()
925 (save-window-excursion
926 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
927 (set-buffer viper-ex-work-buf
)
928 (skip-chars-forward " \t")
933 (skip-chars-forward " \t")
934 (if (looking-at "[0-9]")
937 (re-search-forward "[0-9][0-9]*")
938 (setq ex-count
(string-to-number (buffer-substring (point) (mark t
))))
939 (skip-chars-forward " \t")))
940 (if (looking-at "[pl#]")
944 (if (not (looking-at "[\n|]"))
947 (point-min) (1- (point-max))) viper-BadExCommand
))))
949 ;; Expand \% and \# in ex command
950 (defun ex-expand-filsyms (cmd buf
)
954 (setq cf buffer-file-name
)
955 (setq pf
(ex-next nil t
))) ; this finds alternative file name
956 (if (and (null cf
) (string-match "[^\\]%\\|\\`%" cmd
))
957 (error "No current file to substitute for `%%'"))
958 (if (and (null pf
) (string-match "[^\\]#\\|\\`#" cmd
))
959 (error "No alternate file to substitute for `#'"))
961 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
964 (goto-char (point-min))
965 (while (re-search-forward "%\\|#" nil t
)
966 (let ((data (match-data))
967 (char (buffer-substring (match-beginning 0) (match-end 0))))
968 (if (viper-looking-back (concat "\\\\" char
))
970 (store-match-data data
)
971 (if (string= char
"%")
973 (replace-match pf
)))))
975 (setq ret
(buffer-substring (point-min) (point)))
979 ;; Get a file name and set `ex-variant', `ex-append' and `ex-offset' if found
980 ;; If it is r!, then get the command name and whatever args
981 (defun viper-get-ex-file ()
990 (save-window-excursion
991 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
992 (set-buffer viper-ex-work-buf
)
993 (skip-chars-forward " \t")
995 (if (and (not (viper-looking-back "[ \t]"))
996 ;; read doesn't have a corresponding :r! form, so ! is
997 ;; immediately interpreted as a shell command.
998 (not (string= ex-token
"read")))
1002 (skip-chars-forward " \t"))
1005 (skip-chars-forward " \t")))
1006 (if (looking-at ">>")
1011 (skip-chars-forward " \t")))
1012 (if (looking-at "+")
1016 (re-search-forward "[ \t\n]")
1018 (setq ex-offset
(buffer-substring (point) (mark t
)))
1020 (skip-chars-forward " \t")))
1021 ;; this takes care of :r, :w, etc., when they get file names
1022 ;; from the history list
1023 (if (member ex-token
'("read" "write" "edit" "visual" "next"))
1025 (setq ex-file
(buffer-substring (point) (1- (point-max))))
1027 ;; For :e, match multiple non-white strings separated
1028 ;; by white. For others, find the first non-white string
1030 (if (string= ex-token
"edit")
1031 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
1035 ;; if file name comes from history, don't leave
1036 ;; minibuffer when the user types space
1037 (setq viper-incomplete-ex-cmd nil
)
1038 (setq ex-cmdfile-args
1039 (substring ex-file
(match-end 0) nil
))
1040 ;; this must be the last clause in this progn
1041 (substring ex-file
(match-beginning 0) (match-end 0))
1044 ;; this leaves only the command name in the work area
1045 ;; file names are gone
1046 (delete-region (point) (1- (point-max)))
1048 (goto-char (point-max))
1049 (skip-chars-backward " \t\n")
1050 (setq prompt
(buffer-substring (point-min) (point)))
1053 (setq viper-last-ex-prompt prompt
)
1055 ;; If we just finished reading command, redisplay prompt
1056 (if viper-incomplete-ex-cmd
1057 (setq ex-file
(viper-ex-read-file-name (format ":%s " prompt
)))
1058 ;; file was typed in-line
1059 (setq ex-file
(or ex-file
"")))
1063 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
1064 ;; file names, arranges to re-enter the minibuffer.
1065 (defun viper-complete-filename-or-exit ()
1067 (setq viper-keep-reading-filename t
)
1068 ;; don't exit if directory---ex-commands don't
1069 (cond ((ex-cmd-accepts-multiple-files-p ex-token
) (exit-minibuffer))
1070 ;; apparently the argument to an Ex command is
1071 ;; supposed to be a shell command
1072 ((viper-looking-back "^[ \t]*!.*")
1076 (setq ex-cmdfile nil
)
1077 (minibuffer-complete-word))))
1079 (defun viper-handle-! ()
1082 (buffer-string) (viper-abbreviate-file-name default-directory
))
1083 (member ex-token
'("read" "write")))
1087 (defun ex-cmd-accepts-multiple-files-p (token)
1088 (member token
'("edit" "next" "Next")))
1090 ;; Read file name from the minibuffer in an ex command.
1091 ;; If user doesn't enter anything, then "" is returned, i.e., the
1092 ;; prompt-directory is not returned.
1093 (defun viper-ex-read-file-name (prompt)
1095 (minibuffer-local-completion-map
1096 (copy-keymap minibuffer-local-completion-map
))
1099 (viper-add-keymap ex-read-filename-map
1100 (if (featurep 'emacs
)
1101 minibuffer-local-completion-map
1102 read-file-name-map
))
1104 (setq cont
(setq viper-keep-reading-filename t
))
1106 (setq viper-keep-reading-filename nil
1107 val
(read-file-name (concat prompt str
) nil default-directory
))
1108 (setq val
(expand-file-name val
))
1109 (if (and (string-match " " val
)
1110 (ex-cmd-accepts-multiple-files-p ex-token
))
1111 (setq val
(concat "\"" val
"\"")))
1112 (setq str
(concat str
(if (equal val
"") "" " ")
1113 val
(if (equal val
"") "" " ")))
1115 ;; Only edit, next, and Next commands accept multiple files.
1116 ;; viper-keep-reading-filename is set in the anonymous function that is
1117 ;; bound to " " in ex-read-filename-map.
1118 (setq cont
(and viper-keep-reading-filename
1119 (ex-cmd-accepts-multiple-files-p ex-token
)))
1122 (setq beg
(string-match "[^ \t]" str
) ; delete leading blanks
1123 end
(string-match "[ \t]*$" str
)) ; delete trailing blanks
1124 (if (member ex-token
'("read" "write"))
1125 (if (string-match "[\t ]*!" str
)
1126 ;; this is actually a shell command
1130 (setq viper-last-ex-prompt
1131 (concat viper-last-ex-prompt
" !")))))
1132 (substring str
(or beg
0) end
)))
1135 (defun viper-undisplayed-files ()
1138 (if (null (get-buffer-window b
))
1139 (let ((f (buffer-file-name b
)))
1141 (if ex-cycle-through-non-files
1142 (let ((s (buffer-name b
)))
1143 (if (string= " " (substring s
0 1))
1152 (let ((l (viper-undisplayed-files))
1155 (while (not (null l
))
1157 (setq args
(format "%s %d) %s\n" args file-count
(car l
))
1158 file-count
(1+ file-count
)))
1160 (if (string= args
"")
1161 (message "All files are already displayed")
1163 (save-window-excursion
1164 (with-output-to-temp-buffer " *viper-info*"
1165 (princ "\n\nThese files are not displayed in any window.\n")
1166 (princ "\n=============\n")
1168 (princ "\n=============\n")
1169 (princ "\nThe numbers can be given as counts to :next. ")
1170 (princ "\n\nPress any key to continue...\n\n"))
1171 (viper-read-event))))))
1173 ;; Ex cd command. Default directory of this buffer changes
1176 (if (string= ex-file
"")
1178 (setq default-directory
(file-name-as-directory (expand-file-name ex-file
))))
1180 ;; Ex copy and move command. DEL-FLAG means delete
1181 (defun ex-copy (del-flag)
1182 (viper-default-ex-addresses)
1183 (let ((address (viper-get-ex-address))
1184 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1188 (viper-enlarge-region (mark t
) (point))
1190 (kill-region (point) (mark t
))
1191 (copy-region-as-kill (point) (mark t
)))
1194 (with-output-to-temp-buffer " *copy text*"
1196 (if (or del-flag ex-g-flag ex-g-variant
)
1198 (buffer-substring (point) (mark t
)))))
1201 (read-string "[Hit return to confirm] ")
1202 (save-excursion (kill-buffer " *copy text*")))
1203 (quit (save-excursion (kill-buffer " *copy text*"))
1204 (signal 'quit nil
))))))
1206 (goto-char (point-min))
1209 (insert (current-kill 0))))
1211 ;; Ex delete command
1213 (viper-default-ex-addresses)
1214 (viper-get-ex-buffer)
1215 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1216 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1218 (viper-enlarge-region beg end
)
1219 (exchange-point-and-mark)
1223 (forward-line (1- ex-count
)))
1225 (viper-enlarge-region (point) (mark t
))
1227 ;; show text to be deleted and ask for confirmation
1229 (with-output-to-temp-buffer " *delete text*"
1230 (princ (buffer-substring (point) (mark t
))))
1232 (read-string "[Hit return to confirm] ")
1234 (save-excursion (kill-buffer " *delete text*"))
1235 (error "Viper bell")))
1236 (save-excursion (kill-buffer " *delete text*")))
1238 (cond ((viper-valid-register ex-buffer
'(Letter))
1239 (viper-append-to-register
1240 (downcase ex-buffer
) (point) (mark t
)))
1241 ((viper-valid-register ex-buffer
)
1242 (copy-to-register ex-buffer
(point) (mark t
) nil
))
1243 (t (error viper-InvalidRegister ex-buffer
))))
1244 (kill-region (point) (mark t
))))))
1249 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1250 ;; asked if current buffer should really be discarded.
1251 ;; This command can take multiple file names. It replaces the current buffer
1252 ;; with the first file in its argument list
1253 (defun ex-edit (&optional file
)
1255 (viper-get-ex-file))
1256 (cond ((and (string= ex-file
"") buffer-file-name
)
1257 (setq ex-file
(viper-abbreviate-file-name (buffer-file-name))))
1258 ((string= ex-file
"")
1259 (error viper-NoFileSpecified
)))
1262 (if buffer-file-name
1263 (cond ((buffer-modified-p)
1265 (format "Buffer %s is modified. Discard changes? "
1268 ((not (verify-visited-file-modtime (current-buffer)))
1270 (format "File %s changed on disk. Reread from disk? "
1273 (t (setq do-edit nil
))))
1276 (if (yes-or-no-p msg
)
1278 (set-buffer-modified-p nil
)
1279 (kill-buffer (current-buffer)))
1280 (message "Buffer %s was left intact" (buffer-name))))
1283 (if (null (setq file
(get-file-buffer ex-file
)))
1285 ;; this also does shell-style globbing
1287 ;; replace # and % with the previous/current file
1288 (ex-expand-filsyms ex-file
(current-buffer)))
1289 (or (eq major-mode
'dired-mode
)
1290 (viper-change-state-to-vi))
1291 (goto-char (point-min)))
1292 (switch-to-buffer file
))
1295 (save-window-excursion
1296 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1297 (set-buffer viper-ex-work-buf
)
1298 (delete-region (point-min) (point-max))
1299 (insert ex-offset
"\n")
1300 (goto-char (point-min)))
1301 (goto-char (viper-get-ex-address))
1302 (beginning-of-line)))
1303 (ex-fixup-history viper-last-ex-prompt ex-file
))
1305 ;; Find-file FILESPEC if it appears to specify a single file.
1306 ;; Otherwise, assume that FILESPEC is a wildcard.
1307 ;; In this case, split it into substrings separated by newlines.
1308 ;; Each line is assumed to be a file name.
1309 (defun ex-find-file (filespec)
1310 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1311 (cond ((file-exists-p filespec
) (find-file filespec
))
1312 ((string-match nonstandard-filename-chars filespec
)
1313 (mapcar 'find-file
(funcall viper-glob-function filespec
)))
1314 (t (find-file filespec
)))
1318 ;; Ex global command
1319 ;; This is executed in response to:
1320 ;; :global "pattern" ex-command
1321 ;; :vglobal "pattern" ex-command
1322 ;; :global executes ex-command on all lines matching <pattern>
1323 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1325 ;; With VARIANT nil, this functions executes :global
1326 ;; With VARIANT t, executes :vglobal
1327 (defun ex-global (variant)
1328 (let ((gcommand ex-token
))
1329 (if (or ex-g-flag ex-g-variant
)
1330 (error "`%s' within `global' is not allowed" gcommand
)
1338 (error "`%s': Missing regular expression" gcommand
)))
1340 (if (string= ex-token
"")
1341 (if (null viper-s-string
)
1342 (error viper-NoPrevSearch
)
1343 (setq ex-g-pat viper-s-string
))
1344 (setq ex-g-pat ex-token
1345 viper-s-string ex-token
))
1346 (if (null ex-addresses
)
1347 (setq ex-addresses
(list (point-max) (point-min)))
1348 (viper-default-ex-addresses))
1349 (setq ex-g-marks nil
)
1350 (let ((mark-count 0)
1351 (end (car ex-addresses
))
1352 (beg (car (cdr ex-addresses
)))
1354 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1356 (viper-enlarge-region beg end
)
1357 (exchange-point-and-mark)
1358 (let ((cont t
) (limit (point-marker)))
1359 (exchange-point-and-mark)
1360 ;; skip the last line if empty
1362 (if (eobp) (viper-backward-char-carefully))
1363 (while (and cont
(not (bobp)) (>= (point) limit
))
1367 (let ((found (re-search-backward ex-g-pat
(mark t
) t
)))
1368 (if (or (and ex-g-flag found
)
1369 (and ex-g-variant
(not found
)))
1372 (setq mark-count
(1+ mark-count
))
1373 (setq ex-g-marks
(cons (point-marker) ex-g-marks
)))))
1375 (if (bobp) (setq cont nil
)
1378 (save-window-excursion
1379 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1380 (set-buffer viper-ex-work-buf
)
1381 ;; com-str is the command string, i.e., g/pattern/ or v/pattern'
1382 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
1384 (goto-char (car ex-g-marks
))
1385 (viper-ex nil com-str
)
1386 (setq mark-count
(1- mark-count
))
1387 (setq ex-g-marks
(cdr ex-g-marks
)))))
1391 (if (null ex-addresses
)
1392 (setq ex-addresses
(cons (point) nil
)))
1393 (push-mark (point) t
)
1394 (goto-char (car ex-addresses
))
1398 ;; Ex line commands. COM is join, shift-right or shift-left
1399 (defun ex-line (com)
1400 (viper-default-ex-addresses)
1401 (viper-get-ex-count)
1402 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
1403 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1405 (viper-enlarge-region beg end
)
1406 (exchange-point-and-mark)
1410 (forward-line ex-count
)))
1412 ;; show text to be joined and ask for confirmation
1414 (with-output-to-temp-buffer " *join text*"
1415 (princ (buffer-substring (point) (mark t
))))
1418 (read-string "[Hit return to confirm] ")
1419 (ex-line-subr com
(point) (mark t
)))
1421 (save-excursion (kill-buffer " *join text*")))
1422 (ex-line-subr com
(point) (mark t
)))
1423 (setq point
(point)))
1424 (goto-char (1- point
))
1425 (beginning-of-line)))
1427 (defun ex-line-subr (com beg end
)
1428 (cond ((string= com
"join")
1429 (goto-char (min beg end
))
1430 (while (and (not (eobp)) (< (point) (max beg end
)))
1432 (if (and (<= (point) (max beg end
)) (not (eobp)))
1435 (delete-region (point) (1- (point)))
1436 (if (not ex-variant
) (fixup-whitespace))))))
1437 ((or (string= com
"right") (string= com
"left"))
1439 (min beg end
) (max beg end
)
1440 (if (string= com
"right") viper-shift-width
(- viper-shift-width
)))
1441 (goto-char (max beg end
))
1443 (viper-forward-char-carefully))))
1447 ;; Sets the mark to the current point.
1448 ;; If name is omitted, get the name straight from the work buffer."
1449 (defun ex-mark (&optional name
)
1451 (if (null ex-addresses
)
1453 (cons (point) nil
)))
1455 (if (eq 1 (length name
))
1456 (setq char
(string-to-char name
))
1457 (error "`%s': Spurious text \"%s\" after mark name"
1458 name
(substring name
1)))
1459 (save-window-excursion
1460 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1461 (set-buffer viper-ex-work-buf
)
1462 (skip-chars-forward " \t")
1463 (if (looking-at "[a-z]")
1465 (setq char
(following-char))
1467 (skip-chars-forward " \t")
1468 (if (not (looking-at "[\n|]"))
1469 (error "`%s': %s" ex-token viper-SpuriousText
)))
1470 (error "`%s' requires a following letter" ex-token
))))
1472 (goto-char (car ex-addresses
))
1473 (point-to-register (viper-int-to-char (1+ (- char ?a
)))))))
1477 ;; Alternate file is the file next to the first one in the buffer ring
1478 (defun ex-next (cycle-other-window &optional find-alt-file
)
1481 (if (not find-alt-file
)
1484 (if (or (char-or-string-p ex-offset
)
1485 (and (not (string= "" ex-file
))
1486 (not (string-match "^[0-9]+$" ex-file
))))
1489 (throw 'ex-edit nil
))
1490 (setq count
(string-to-number ex-file
))
1491 (if (= count
0) (setq count
1))
1492 (if (< count
0) (error "Usage: `next <count>' (count >= 0)"))))
1494 (setq l
(viper-undisplayed-files))
1496 (while (and (not (null l
)) (null (car l
)))
1498 (setq count
(1- count
))
1501 (if find-alt-file
(car l
)
1503 (if (and (car l
) (get-file-buffer (car l
)))
1504 (let* ((w (if cycle-other-window
1505 (get-lru-window) (selected-window)))
1506 (b (window-buffer w
)))
1507 (set-window-buffer w
(get-file-buffer (car l
)))
1509 ;; this puts "next <count>" in the ex-command history
1510 (ex-fixup-history viper-last-ex-prompt ex-file
))
1511 (error "Not that many undisplayed files")))))))
1514 (defun ex-next-related-buffer (direction &optional no-recursion
)
1516 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction
)
1518 (let ((file-or-buffer-name
1519 (viper-current-ring-item viper-related-files-and-buffers-ring
))
1520 (old-ring viper-related-files-and-buffers-ring
)
1521 (old-win (selected-window))
1524 (or (and (ring-p viper-related-files-and-buffers-ring
)
1525 (> (ring-length viper-related-files-and-buffers-ring
) 0))
1526 (error "This buffer has no related files or buffers"))
1528 (or (stringp file-or-buffer-name
)
1530 "File and buffer names must be strings, %S" file-or-buffer-name
))
1532 (setq buf
(cond ((get-buffer file-or-buffer-name
))
1533 ((file-exists-p file-or-buffer-name
)
1534 (find-file-noselect file-or-buffer-name
))
1537 (if (not (viper-buffer-live-p buf
))
1538 (error "Didn't find buffer %S or file %S"
1540 (viper-abbreviate-file-name
1541 (expand-file-name file-or-buffer-name
))))
1543 (if (equal buf
(current-buffer))
1548 (ex-next-related-buffer direction
'norecursion
))))
1553 (if (setq wind
(viper-get-visible-buffer-window buf
))
1555 (setq wind
(get-lru-window (if (featurep 'xemacs
) nil
'visible
)))
1556 (set-window-buffer wind buf
))
1558 (if (viper-window-display-p)
1560 (raise-frame (window-frame wind
))
1561 (if (equal (window-frame wind
) (window-frame old-win
))
1562 (save-window-excursion (select-window wind
) (sit-for 1))
1563 (select-window wind
)))
1564 (save-window-excursion (select-window wind
) (sit-for 1)))
1568 (setq viper-related-files-and-buffers-ring old-ring
))
1570 (setq viper-local-search-start-marker
(point-marker))
1575 (defun ex-preserve ()
1576 (message "Autosaving all buffers that need to be saved...")
1581 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
1582 (viper-get-ex-buffer)
1583 (setq viper-use-register ex-buffer
)
1585 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
1587 ;; Ex print working directory
1589 (message "%s" default-directory
))
1593 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1595 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1596 (set-buffer viper-ex-work-buf
)
1597 (if (looking-at "!") (forward-char 1)))
1598 (if (< viper-expert-level
3)
1599 (save-buffers-kill-emacs)
1600 (kill-buffer (current-buffer))))
1604 ;; ex-read doesn't support wildcards, because file completion is a better
1605 ;; mechanism. We also don't support # and % (except in :r <shell-command>
1606 ;; because file history is a better mechanism.
1609 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
1612 (viper-add-newline-at-eob-if-necessary)
1613 (if (not (or (bobp) (eobp))) (forward-line 1))
1614 (if (and (not ex-variant
) (string= ex-file
""))
1616 (if (null buffer-file-name
)
1617 (error viper-NoFileSpecified
))
1618 (setq ex-file buffer-file-name
)))
1622 ;; replace # and % with the previous/current file
1624 (concat (shell-quote-argument ex-file
) ex-cmdfile-args
)
1626 (shell-command command t
))
1627 (insert-file-contents ex-file
)))
1628 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args
))
1630 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1631 (defun ex-fixup-history (&rest args
)
1632 (setq viper-ex-history
1633 (cons (mapconcat 'identity args
" ") (cdr viper-ex-history
))))
1636 ;; Ex recover from emacs \#file\#
1637 (defun ex-recover ()
1639 (if (or ex-append ex-offset
)
1640 (error "`recover': %s" viper-SpuriousText
))
1641 (if (string= ex-file
"")
1643 (if (null buffer-file-name
)
1644 (error "This buffer isn't visiting any file"))
1645 (setq ex-file buffer-file-name
))
1646 (setq ex-file
(expand-file-name ex-file
)))
1647 (if (and (not (string= ex-file
(buffer-file-name)))
1650 (error "No write since last change \(:rec! overrides\)"))
1651 (recover-file ex-file
))
1653 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1656 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1659 ;; read variable name for ex-set
1660 (defun ex-set-read-variable ()
1661 (let ((minibuffer-local-completion-map
1662 (copy-keymap minibuffer-local-completion-map
))
1663 (cursor-in-echo-area t
)
1666 minibuffer-local-completion-map
" " 'minibuffer-complete-and-exit
)
1667 (define-key minibuffer-local-completion-map
"=" 'exit-minibuffer
)
1668 (if (viper-set-unread-command-events
1669 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil
"\C-m"))
1672 (viper-set-unread-command-events ?\C-m
)))
1673 (message ":set <Variable> [= <Value>]")
1674 (or batch
(sit-for 2))
1676 (while (string-match "^[ \\t\\n]*$"
1678 (completing-read ":set " ex-variable-alist
)))
1679 (message ":set <Variable> [= <Value>]")
1680 ;; if there are unread events, don't wait
1681 (or (viper-set-unread-command-events "") (sit-for 2))
1687 (let ((var (ex-set-read-variable))
1691 (auto-cmd-label "; don't touch or else...")
1692 (delete-turn-on-auto-fill-pattern
1693 "([ \t]*add-hook[ \t]+'viper-insert-state-hook[ \t]+'turn-on-auto-fill.*)")
1694 actual-lisp-cmd lisp-cmd-del-pattern
1697 (cond ((string= var
"all")
1698 (setq ask-if-save nil
1700 ((member var
'("ai" "autoindent"))
1701 (setq var
"viper-auto-indent"
1705 ((member var
'("ai-g" "autoindent-global"))
1706 (kill-local-variable 'viper-auto-indent
)
1707 (setq var
"viper-auto-indent"
1708 set-cmd
"setq-default"
1710 ((member var
'("noai" "noautoindent"))
1711 (setq var
"viper-auto-indent"
1714 ((member var
'("noai-g" "noautoindent-global"))
1715 (kill-local-variable 'viper-auto-indent
)
1716 (setq var
"viper-auto-indent"
1717 set-cmd
"setq-default"
1719 ((member var
'("ic" "ignorecase"))
1720 (setq var
"viper-case-fold-search"
1722 ((member var
'("noic" "noignorecase"))
1723 (setq var
"viper-case-fold-search"
1725 ((member var
'("ma" "magic"))
1726 (setq var
"viper-re-search"
1728 ((member var
'("noma" "nomagic"))
1729 (setq var
"viper-re-search"
1731 ((member var
'("ro" "readonly"))
1732 (setq var
"buffer-read-only"
1734 ((member var
'("noro" "noreadonly"))
1735 (setq var
"buffer-read-only"
1737 ((member var
'("sm" "showmatch"))
1738 (setq var
"blink-matching-paren"
1740 ((member var
'("nosm" "noshowmatch"))
1741 (setq var
"blink-matching-paren"
1743 ((member var
'("ws" "wrapscan"))
1744 (setq var
"viper-search-wrap-around-t"
1746 ((member var
'("nows" "nowrapscan"))
1747 (setq var
"viper-search-wrap-around-t"
1749 (if (and set-cmd
(eq val
0)) ; value must be set by the user
1750 (let ((cursor-in-echo-area t
))
1751 (message ":set %s = <Value>" var
)
1752 ;; if there are unread events, don't wait
1753 (or (viper-set-unread-command-events "") (sit-for 2))
1754 (setq val
(read-string (format ":set %s = " var
)))
1755 (ex-fixup-history "set" orig-var val
)
1757 ;; check numerical values
1761 "ts-g" "tabstop-global"
1764 (or (numberp (setq val2
(car (read-from-string val
))))
1765 (error "%s: Invalid value, numberp, %S" var val
))
1767 (error "%s: Invalid value, numberp, %S" var val
))))
1770 ((member var
'("sw" "shiftwidth"))
1771 (setq var
"viper-shift-width"))
1772 ((member var
'("ts" "tabstop"))
1773 ;; make it take effect in curr buff and new bufs
1774 (setq var
"tab-width"
1777 ((member var
'("ts-g" "tabstop-global"))
1778 (kill-local-variable 'tab-width
)
1779 (setq var
"tab-width"
1780 set-cmd
"setq-default"))
1781 ((member var
'("wm" "wrapmargin"))
1782 ;; make it take effect in curr buff and new bufs
1783 (kill-local-variable 'fill-column
)
1784 (setq var
"fill-column"
1785 val
(format "(- (window-width) %s)" val
)
1786 set-cmd
"setq-default"))
1787 ((member var
'("sh" "shell"))
1788 (setq var
"explicit-shell-file-name"
1789 val
(format "\"%s\"" val
)))))
1790 (ex-fixup-history "set" orig-var
))
1793 (setq actual-lisp-cmd
1794 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label
)
1795 lisp-cmd-del-pattern
1796 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1797 set-cmd var auto-cmd-label
)))
1799 (if (and ask-if-save
1800 (y-or-n-p (format "Do you want to save this setting in %s "
1801 viper-custom-file-name
)))
1803 (viper-save-string-in-file
1804 actual-lisp-cmd viper-custom-file-name
1806 lisp-cmd-del-pattern
)
1807 (if (string= var
"fill-column")
1809 (viper-save-string-in-file
1811 "(add-hook 'viper-insert-state-hook 'turn-on-auto-fill) "
1813 viper-custom-file-name
1814 delete-turn-on-auto-fill-pattern
)
1815 (viper-save-string-in-file
1816 nil viper-custom-file-name delete-turn-on-auto-fill-pattern
)
1817 (viper-save-string-in-file
1818 nil viper-custom-file-name
1820 lisp-cmd-del-pattern
)
1827 (if (string-match "^[ \t]*$" val
)
1831 (eval (car (read-from-string actual-lisp-cmd
))))
1832 (if (string= var
"fill-column")
1835 (auto-fill-mode -
1)))
1836 (if (string= var
"all") (ex-show-vars))
1839 ;; In inline args, skip regex-forw and (optionally) chars-back.
1840 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1842 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str
)
1844 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1845 (set-buffer viper-ex-work-buf
)
1846 (goto-char (point-min))
1847 (re-search-forward regex-forw nil t
)
1850 (goto-char (point-max))
1852 (skip-chars-backward chars-back
)
1853 (skip-chars-backward " \t\n\C-m"))
1855 ;; replace SPC with `=' to suppress the special meaning SPC has
1859 (while (re-search-forward " +" nil t
)
1860 (replace-match replace-str nil t
)
1861 (viper-forward-char-carefully)))
1863 (buffer-substring beg end
))))
1870 ;; Viper help. Invokes Info
1874 (pop-to-buffer (get-buffer-create "*info*"))
1875 (info (if (featurep 'xemacs
) "viper.info" "viper"))
1876 (message "Type `i' to search for a specific topic"))
1878 (with-output-to-temp-buffer " *viper-info*"
1880 The Info file for Viper does not seem to be installed.
1882 This file is part of the standard distribution of %sEmacs.
1883 Please contact your system administrator. "
1884 (if (featurep 'xemacs
) "X" "")
1887 ;; Ex source command. Loads the file specified as argument or `~/.viper'
1890 (if (string= ex-file
"")
1891 (load viper-custom-file-name
)
1894 ;; Ex substitute command
1895 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1896 (defun ex-substitute (&optional repeat r-flag
)
1900 (case-fold-search viper-case-fold-search
)
1902 (if repeat
(setq ex-token nil
) (setq delim
(viper-get-ex-pat)))
1905 (setq pat
(if r-flag viper-s-string ex-reg-exp
))
1907 (error "No previous pattern to use in substitution"))
1909 delim
(string-to-char pat
)))
1910 (setq pat
(if (string= ex-token
"") viper-s-string ex-token
))
1911 (setq viper-s-string pat
1913 (setq delim
(viper-get-ex-pat))
1919 (while (viper-get-ex-opt-gc delim
)
1920 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
1921 (viper-get-ex-count)
1924 (if ex-addresses
(goto-char (car ex-addresses
)))
1926 (forward-line (1- ex-count
))
1927 (setq ex-addresses
(cons (point) (cons (mark t
) nil
))))
1928 (if (null ex-addresses
)
1929 (setq ex-addresses
(cons (point) (cons (point) nil
)))
1930 (if (null (cdr ex-addresses
))
1931 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
1933 (let ((beg (car ex-addresses
))
1934 (end (car (cdr ex-addresses
)))
1937 (viper-enlarge-region beg end
)
1938 (let ((limit (save-excursion
1939 (goto-char (max (point) (mark t
)))
1941 (goto-char (min (point) (mark t
)))
1942 (while (< (point) limit
)
1945 ;; This move allows the use of newline as the last character in
1946 ;; the substitution pattern
1947 (viper-forward-char-carefully)
1948 (setq eol-mark
(point-marker)))
1952 (while (and (not (eolp))
1953 (re-search-forward pat eol-mark t
))
1956 (viper-put-on-search-overlay (match-beginning 0)
1958 (y-or-n-p "Replace? ")))
1960 (viper-hide-search-overlay)
1961 (setq matched-pos
(point))
1962 (if (not (stringp repl
))
1963 (error "Can't perform Ex substitution: No previous replacement pattern"))
1964 (replace-match repl t
))))
1966 (viper-forward-char-carefully))
1969 "Can't repeat Ex substitution: No previous regular expression"))
1970 (if (and (re-search-forward pat eol-mark t
)
1973 (viper-put-on-search-overlay (match-beginning 0)
1975 (y-or-n-p "Replace? "))))
1977 (viper-hide-search-overlay)
1978 (setq matched-pos
(point))
1979 (if (not (stringp repl
))
1980 (error "Can't perform Ex substitution: No previous replacement pattern"))
1981 (replace-match repl t
)))
1983 ;;(viper-forward-char-carefully)
1984 (goto-char eol-mark
)
1986 (if matched-pos
(goto-char matched-pos
))
1988 (if opt-c
(message "done"))))
1993 (save-window-excursion
1994 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1995 (set-buffer viper-ex-work-buf
)
1996 (skip-chars-forward " \t")
1998 (skip-chars-forward "^ |\t\n")
1999 (setq tag
(buffer-substring (mark t
) (point))))
2000 (if (not (string= tag
"")) (setq ex-tag tag
))
2001 (viper-change-state-to-emacs)
2002 (condition-case conds
2004 (if (string= tag
"")
2006 (find-tag-other-window ex-tag
))
2007 (viper-change-state-to-vi))
2009 (viper-change-state-to-vi)
2010 (viper-message-conditions conds
)))))
2013 ;; ex-write doesn't support wildcards, because file completion is a better
2014 ;; mechanism. We also don't support # and %
2015 ;; because file history is a better mechanism.
2016 (defun ex-write (q-flag)
2017 (viper-default-ex-addresses t
)
2019 (let ((end (car ex-addresses
))
2020 (beg (car (cdr ex-addresses
)))
2021 (orig-buf (current-buffer))
2022 ;;(orig-buf-file-name (buffer-file-name))
2023 ;;(orig-buf-name (buffer-name))
2024 ;;(buff-changed-p (buffer-modified-p))
2025 temp-buf writing-same-file region
2026 file-exists writing-whole-file
)
2027 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
2030 (viper-enlarge-region beg end
)
2031 (shell-command-on-region (point) (mark t
)
2032 (concat ex-file ex-cmdfile-args
)))
2033 (if (and (string= ex-file
"") (not (buffer-file-name)))
2036 (format "Buffer %s isn't visiting any file. File to save in: "
2039 (setq writing-whole-file
(and (= (point-min) beg
) (= (point-max) end
))
2040 ex-file
(if (string= ex-file
"")
2042 (expand-file-name ex-file
)))
2043 ;; if ex-file is a directory use the file portion of the buffer file name
2044 (if (and (file-directory-p ex-file
)
2046 (not (file-directory-p buffer-file-name
)))
2048 (concat (file-name-as-directory ex-file
)
2049 (file-name-nondirectory buffer-file-name
))))
2051 (setq file-exists
(file-exists-p ex-file
)
2052 writing-same-file
(string= ex-file
(buffer-file-name)))
2054 ;; do actual writing
2055 (if (and writing-whole-file writing-same-file
)
2056 ;; saving whole buffer in visited file
2057 (if (not (buffer-modified-p))
2058 (message "(No changes need to be saved)")
2059 (viper-maybe-checkout (current-buffer))
2063 (ex-write-info file-exists ex-file
(point-min) (point-max))
2065 ;; writing to non-visited file and it already exists
2066 (if (and file-exists
(not writing-same-file
)
2068 (format "File %s exists. Overwrite? " ex-file
))))
2070 ;; writing a region or whole buffer to non-visited file
2073 (viper-enlarge-region beg end
)
2074 (setq region
(buffer-substring (point) (mark t
)))
2075 ;; create temp buffer for the region
2076 (setq temp-buf
(get-buffer-create " *ex-write*"))
2077 (set-buffer temp-buf
)
2078 (if (featurep 'xemacs
)
2079 (set-visited-file-name ex-file
)
2080 (set-visited-file-name ex-file
'noquery
))
2082 (if (and file-exists ex-append
)
2083 (insert-file-contents ex-file
))
2084 (goto-char (point-max))
2087 (viper-maybe-checkout (current-buffer))
2088 (setq selective-display nil
)
2091 file-exists ex-file
(point-min) (point-max))
2093 ;; this must be under unwind-protect so that
2094 ;; temp-buf will be deleted in case of an error
2095 (set-buffer temp-buf
)
2096 (set-buffer-modified-p nil
)
2097 (kill-buffer temp-buf
)
2098 ;; buffer/region has been written, now take care of details
2099 (set-buffer orig-buf
)))
2100 ;; set the right file modification time
2101 (if (and (buffer-file-name) writing-same-file
)
2102 (set-visited-file-modtime))
2103 ;; prevent loss of data if saving part of the buffer in visited file
2104 (or writing-whole-file
2105 (not writing-same-file
)
2108 (message "Warning: you have saved only part of the buffer!")
2109 (set-buffer-modified-p t
)))
2111 (if (< viper-expert-level
2)
2112 (save-buffers-kill-emacs)
2113 (kill-buffer (current-buffer))))
2117 (defun ex-write-info (exists file-name beg end
)
2118 (message "`%s'%s %d lines, %d characters"
2119 (viper-abbreviate-file-name file-name
)
2120 (if exists
"" " [New file]")
2121 (count-lines beg
(min (1+ end
) (point-max)))
2126 (viper-default-ex-addresses)
2127 (viper-get-ex-buffer)
2128 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2129 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
2131 (viper-enlarge-region beg end
)
2132 (exchange-point-and-mark)
2133 (if (or ex-g-flag ex-g-variant
)
2134 (error "Can't execute `yank' within `global'"))
2138 (forward-line (1- ex-count
)))
2140 (viper-enlarge-region (point) (mark t
))
2141 (if ex-flag
(error "`yank': %s" viper-SpuriousText
))
2143 (cond ((viper-valid-register ex-buffer
'(Letter))
2144 (viper-append-to-register
2145 (downcase ex-buffer
) (point) (mark t
)))
2146 ((viper-valid-register ex-buffer
)
2147 (copy-to-register ex-buffer
(point) (mark t
) nil
))
2148 (t (error viper-InvalidRegister ex-buffer
))))
2149 (copy-region-as-kill (point) (mark t
)))))
2151 ;; Execute shell command
2152 (defun ex-command ()
2154 (save-window-excursion
2155 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
2156 (set-buffer viper-ex-work-buf
)
2157 (skip-chars-forward " \t")
2158 (setq command
(buffer-substring (point) (point-max)))
2160 ;; replace # and % with the previous/current file
2161 (setq command
(ex-expand-filsyms command
(current-buffer)))
2162 (if (and (> (length command
) 0) (string= "!" (substring command
0 1)))
2163 (if viper-ex-last-shell-com
2165 (concat viper-ex-last-shell-com
(substring command
1)))
2166 (error "No previous shell command")))
2167 (setq viper-ex-last-shell-com command
)
2168 (if (null ex-addresses
)
2169 (shell-command command
)
2170 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2171 (if (null beg
) (setq beg end
))
2175 (viper-enlarge-region (point) (mark t
))
2176 (shell-command-on-region (point) (mark t
) command t
))
2179 (defun ex-compile ()
2180 "Reads args from the command line, then runs make with the args.
2181 If no args are given, then it runs the last compile command.
2182 Type 'mak ' (including the space) to run make with no args."
2184 (save-window-excursion
2185 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
2186 (set-buffer viper-ex-work-buf
)
2187 (setq args
(buffer-substring (point) (point-max)))
2189 ;; Remove the newline that may (will?) be at the end of the args
2190 (if (string= "\n" (substring args
(1- (length args
))))
2191 (setq args
(substring args
0 (1- (length args
)))))
2192 ;; Run last command if no args given, else construct a new command.
2194 (if (string= "" args
)
2195 (if (boundp 'compile-command
)
2198 (concat ex-compile-command
" " args
)))
2202 ;; Print line number
2203 (defun ex-line-no ()
2207 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
2209 ;; Give information on the file visited by the current buffer
2210 (defun viper-info-on-file ()
2212 (let ((pos1 (viper-line-pos 'start
))
2213 (pos2 (viper-line-pos 'end
))
2215 (setq lines
(count-lines (point-min) (viper-line-pos 'end
))
2216 file
(cond ((buffer-file-name)
2217 (concat (viper-abbreviate-file-name (buffer-file-name)) ":"))
2218 ((buffer-file-name (buffer-base-buffer))
2219 (concat (viper-abbreviate-file-name (buffer-file-name (buffer-base-buffer))) " (indirect buffer):"))
2220 (t (concat (buffer-name) " [Not visiting any file]:")))
2221 info
(format "line=%d/%d pos=%d/%d col=%d %s"
2225 (count-lines (point-min) (point-max))
2226 (point) (1- (point-max))
2227 (1+ (current-column))
2228 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2229 (if (< (+ 1 (length info
) (length file
))
2230 (window-width (minibuffer-window)))
2231 (message "%s" (concat file
" " info
))
2232 (save-window-excursion
2233 (with-output-to-temp-buffer " *viper-info*"
2234 (princ (concat "\n" file
"\n\n\t" info
"\n\n")))
2235 (let ((inhibit-quit t
))
2236 (viper-set-unread-command-events (viper-read-event)))
2237 (kill-buffer " *viper-info*")))
2241 ;; Without arguments displays info on file. With an arg, sets the visited file
2243 (defun ex-set-visited-file-name ()
2245 (if (string= ex-file
"")
2246 (viper-info-on-file)
2247 ;; If ex-file is a directory, use the file portion of the buffer
2248 ;; file name (like ex-write). Do this even if ex-file is a
2249 ;; non-existent directory, since set-visited-file-name signals an
2250 ;; error on this condition, too.
2251 (if (and (string= (file-name-nondirectory ex-file
) "")
2253 (not (file-directory-p buffer-file-name
)))
2254 (setq ex-file
(concat (file-name-as-directory ex-file
)
2255 (file-name-nondirectory buffer-file-name
))))
2256 (set-visited-file-name ex-file
)))
2259 ;; display all variables set through :set
2260 (defun ex-show-vars ()
2261 (with-output-to-temp-buffer " *viper-info*"
2262 (princ (if viper-auto-indent
2263 "autoindent (local)\n" "noautoindent (local)\n"))
2264 (princ (if (default-value 'viper-auto-indent
)
2265 "autoindent (global) \n" "noautoindent (global) \n"))
2266 (princ (if viper-case-fold-search
"ignorecase\n" "noignorecase\n"))
2267 (princ (if viper-re-search
"magic\n" "nomagic\n"))
2268 (princ (if buffer-read-only
"readonly\n" "noreadonly\n"))
2269 (princ (if blink-matching-paren
"showmatch\n" "noshowmatch\n"))
2270 (princ (if viper-search-wrap-around-t
"wrapscan\n" "nowrapscan\n"))
2271 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width
))
2272 (princ (format "tabstop (local) \t= %S\n" tab-width
))
2273 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width
)))
2274 (princ (format "wrapmargin (local) \t= %S\n"
2275 (- (window-width) fill-column
)))
2276 (princ (format "wrapmargin (global) \t= %S\n"
2277 (- (window-width) (default-value 'fill-column
))))
2278 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name
)
2279 explicit-shell-file-name
2284 (viper-default-ex-addresses)
2285 (let ((end (car ex-addresses
))
2286 (beg (car (cdr ex-addresses
))))
2287 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
2289 (viper-enlarge-region beg end
)
2290 (if (or ex-g-flag ex-g-variant
)
2291 ;; When executing a global command, collect output of each
2292 ;; print in viper-ex-print-buf.
2294 (append-to-buffer viper-ex-print-buf
(point) (mark t
))
2295 ;; Is this the last mark for the global command?
2296 (unless (cdr ex-g-marks
)
2297 (with-current-buffer viper-ex-print-buf
2298 (ex-print-display-lines (buffer-string))
2300 (ex-print-display-lines (buffer-substring (point) (mark t
)))))))
2302 (defun ex-print-display-lines (lines)
2304 ;; String doesn't contain a newline.
2305 ((not (string-match "\n" lines
))
2306 (message "%s" lines
))
2307 ;; String contains only one newline at the end. Strip it off.
2308 ((= (string-match "\n" lines
) (1- (length lines
)))
2309 (message "%s" (substring lines
0 -
1)))
2310 ;; String spans more than one line. Use a temporary buffer.
2312 (save-current-buffer
2313 (with-output-to-temp-buffer " *viper-info*"
2320 ;;; arch-tag: 56b80d36-f880-4d10-bd66-85ad91a295db
2321 ;;; viper-ex.el ends here