1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
27 (defvar read-file-name-map
)
28 (defvar viper-use-register
)
29 (defvar viper-s-string
)
30 (defvar viper-shift-width
)
31 (defvar viper-ex-history
)
32 (defvar viper-related-files-and-buffers-ring
)
33 (defvar viper-local-search-start-marker
)
34 (defvar viper-expert-level
)
35 (defvar viper-custom-file-name
)
36 (defvar viper-case-fold-search
)
37 (defvar explicit-shell-file-name
)
39 ;; loading happens only in non-interactive compilation
40 ;; in order to spare non-viperized emacs from being viperized
43 (let ((load-path (cons (expand-file-name ".") load-path
)))
44 (or (featurep 'viper-util
)
45 (load "viper-util.el" nil nil
'nosuffix
))
46 (or (featurep 'viper-keym
)
47 (load "viper-keym.el" nil nil
'nosuffix
))
48 (or (featurep 'viper-cmd
)
49 (load "viper-cmd.el" nil nil
'nosuffix
))
55 (defgroup viper-ex nil
56 "Viper support for Ex commands"
64 (defconst viper-ex-work-buf-name
" *ex-working-space*")
65 (defconst viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
66 (defconst viper-ex-tmp-buf-name
" *ex-tmp*")
69 ;;; Variable completion in :set command
71 ;; The list of Ex commands. Used for completing command names.
72 (defconst ex-token-alist
73 '(("!") ("=") (">") ("&") ("~")
74 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
75 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
76 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
77 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
78 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
79 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
80 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
81 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
83 ;; A-list of Ex variables that can be set using the :set command.
84 (defconst ex-variable-alist
85 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
86 ("tabstop-global") ("ts-g") ("tabstop") ("ts")
87 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
89 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
90 ("noreadonly") ("noro") ("nomagic") ("noma")
91 ("noignorecase") ("noic")
92 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai")
93 ("magic") ("ma") ("ignorecase") ("ic")
94 ("autoindent-global") ("ai-g") ("autoindent") ("ai")
100 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
101 (defvar ex-token nil
)
104 ;; If non-nil, gives type of address; if nil, it is a command.
105 (defvar ex-token-type nil
)
107 ;; List of addresses passed to Ex command
108 (defvar ex-addresses nil
)
110 ;; It seems that this flag is used only for `#', `print', and `list', which
111 ;; aren't implemented. Check later.
114 ;; "buffer" where Ex commands keep deleted data.
115 ;; In Emacs terms, this is a register.
116 (defvar ex-buffer nil
)
118 ;; Value of ex count.
119 (defvar ex-count nil
)
121 ;; Flag indicating that :global Ex command is being executed.
122 (defvar ex-g-flag nil
)
123 ;; Flag indicating that :vglobal Ex command is being executed.
124 (defvar ex-g-variant nil
)
126 ;; Save reg-exp used in substitute.
127 (defvar ex-reg-exp nil
)
130 ;; Replace pattern for substitute.
133 ;; Pattern for global command.
134 (defvar ex-g-pat nil
)
136 (defcustom ex-unix-type-shell
137 (let ((case-fold-search t
))
138 (and (stringp shell-file-name
)
148 "[^a-z]sh$\\|[^a-z]sh.exe$"
153 "Is the user using a unix-type shell under a non-OS?"
157 (defcustom ex-unix-type-shell-options
158 (let ((case-fold-search t
))
159 (if ex-unix-type-shell
160 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name
)
161 "-f") ; csh: do it fast
162 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name
)
163 "-noprofile") ; bash: ignore .profile
165 "Options to pass to the Unix-style shell.
166 Don't put `-c' here, as it is added automatically."
170 (defvar ex-nontrivial-find-file-function
171 (cond (ex-unix-type-shell 'viper-ex-nontrivial-find-file-unix
)
172 ((eq system-type
'emx
) 'viper-ex-nontrivial-find-file-ms
) ; OS/2
173 (viper-ms-style-os-p 'viper-ex-nontrivial-find-file-ms
) ; Microsoft OS
174 (viper-vms-os-p 'viper-ex-nontrivial-find-file-unix
) ; VMS
175 (t 'viper-ex-nontrivial-find-file-unix
) ; presumably UNIX
178 ;; Remembers the previous Ex tag.
181 ;; file used by Ex commands like :r, :w, :n
184 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
185 (defvar ex-variant nil
)
187 ;; Specified the offset of an Ex command, such as :read.
188 (defvar ex-offset nil
)
190 ;; Tells Ex that this is a w>> command.
191 (defvar ex-append nil
)
193 ;; File containing the shell command to be executed at Ex prompt,
195 (defvar ex-cmdfile nil
)
197 ;; flag used in viper-ex-read-file-name to indicate that we may be reading
198 ;; multiple file names. Used for :edit and :next
199 (defvar viper-keep-reading-filename nil
)
201 (defcustom ex-cycle-other-window t
202 "*If t, :n and :b cycles through files and buffers in other window.
203 Then :N and :B cycles in the current window. If nil, this behavior is
208 (defcustom ex-cycle-through-non-files nil
209 "*Cycle through *scratch* and other buffers that don't visit any file."
213 ;; Last shell command executed with :! command.
214 (defvar viper-ex-last-shell-com nil
)
216 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
217 (defvar viper-incomplete-ex-cmd nil
)
219 ;; Remembers the last ex-command prompt.
220 (defvar viper-last-ex-prompt
"")
225 ;; Check if ex-token is an initial segment of STR
226 (defun viper-check-sub (str)
227 (let ((length (length ex-token
)))
228 (if (and (<= length
(length str
))
229 (string= ex-token
(substring str
0 length
)))
231 (setq ex-token-type
'non-command
))))
233 ;; Get a complete ex command
234 (defun viper-get-ex-com-subr ()
235 (let (case-fold-search)
237 (re-search-forward "[a-zA-Z][a-zA-Z]*")
238 (setq ex-token-type
'command
)
239 (setq ex-token
(buffer-substring (point) (mark t
)))
240 (exchange-point-and-mark)
241 (cond ((looking-at "a")
242 (cond ((looking-at "ab") (viper-check-sub "abbreviate"))
243 ((looking-at "ar") (viper-check-sub "args"))
244 (t (viper-check-sub "append"))))
245 ((looking-at "h") (viper-check-sub "help"))
247 (cond ((looking-at "cd") (viper-check-sub "cd"))
248 ((looking-at "ch") (viper-check-sub "chdir"))
249 ((looking-at "co") (viper-check-sub "copy"))
250 (t (viper-check-sub "change"))))
251 ((looking-at "d") (viper-check-sub "delete"))
252 ((looking-at "b") (viper-check-sub "buffer"))
253 ((looking-at "B") (viper-check-sub "Buffer"))
255 (if (looking-at "ex") (viper-check-sub "ex")
256 (viper-check-sub "edit")))
257 ((looking-at "f") (viper-check-sub "file"))
258 ((looking-at "g") (viper-check-sub "global"))
259 ((looking-at "i") (viper-check-sub "insert"))
260 ((looking-at "j") (viper-check-sub "join"))
261 ((looking-at "l") (viper-check-sub "list"))
263 (cond ((looking-at "map") (viper-check-sub "map"))
264 ((looking-at "mar") (viper-check-sub "mark"))
265 (t (viper-check-sub "move"))))
266 ((looking-at "k[a-z][^a-z]")
267 (setq ex-token
"kmark")
269 (exchange-point-and-mark)) ; this is canceled out by another
270 ; exchange-point-and-mark at the end
271 ((looking-at "k") (viper-check-sub "kmark"))
272 ((looking-at "n") (if (looking-at "nu")
273 (viper-check-sub "number")
274 (viper-check-sub "next")))
275 ((looking-at "N") (viper-check-sub "Next"))
276 ((looking-at "o") (viper-check-sub "open"))
278 (cond ((looking-at "pre") (viper-check-sub "preserve"))
279 ((looking-at "pu") (viper-check-sub "put"))
280 ((looking-at "pw") (viper-check-sub "pwd"))
281 (t (viper-check-sub "print"))))
282 ((looking-at "P") (viper-check-sub "PreviousRelatedFile"))
283 ((looking-at "R") (viper-check-sub "RelatedFile"))
284 ((looking-at "q") (viper-check-sub "quit"))
286 (cond ((looking-at "rec") (viper-check-sub "recover"))
287 ((looking-at "rew") (viper-check-sub "rewind"))
288 (t (viper-check-sub "read"))))
290 (cond ((looking-at "se") (viper-check-sub "set"))
291 ((looking-at "sh") (viper-check-sub "shell"))
292 ((looking-at "so") (viper-check-sub "source"))
293 ((looking-at "sr") (viper-check-sub "sr"))
294 ((looking-at "st") (viper-check-sub "stop"))
295 ((looking-at "sus") (viper-check-sub "suspend"))
296 ((looking-at "subm") (viper-check-sub "submitReport"))
297 (t (viper-check-sub "substitute"))))
299 (if (looking-at "ta") (viper-check-sub "tag")
300 (viper-check-sub "transfer")))
302 (cond ((looking-at "una") (viper-check-sub "unabbreviate"))
303 ((looking-at "unm") (viper-check-sub "unmap"))
304 (t (viper-check-sub "undo"))))
306 (cond ((looking-at "ve") (viper-check-sub "version"))
307 ((looking-at "vi") (viper-check-sub "visual"))
308 (t (viper-check-sub "vglobal"))))
310 (if (looking-at "wq") (viper-check-sub "wq")
311 (viper-check-sub "write")))
313 (if (looking-at "WW")
314 (viper-check-sub "WWrite")
315 (viper-check-sub "Write")))
316 ((looking-at "x") (viper-check-sub "xit"))
317 ((looking-at "y") (viper-check-sub "yank"))
318 ((looking-at "z") (viper-check-sub "z")))
319 (exchange-point-and-mark)
322 ;; Get an ex-token which is either an address or a command.
323 ;; A token has a type, \(command, address, end-mark\), and a value
324 (defun viper-get-ex-token ()
325 (save-window-excursion
326 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
327 (set-buffer viper-ex-work-buf
)
328 (skip-chars-forward " \t|")
329 (let ((case-fold-search t
))
330 (cond ((looking-at "#")
331 (setq ex-token-type
'command
)
332 (setq ex-token
(char-to-string (following-char)))
334 ((looking-at "[a-z]") (viper-get-ex-com-subr))
337 (setq ex-token-type
'dot
))
338 ((looking-at "[0-9]")
340 (re-search-forward "[0-9]*")
342 (cond ((eq ex-token-type
'plus
) 'add-number
)
343 ((eq ex-token-type
'minus
) 'sub-number
)
346 (string-to-int (buffer-substring (point) (mark t
)))))
349 (setq ex-token-type
'end
))
352 (setq ex-token-type
'whole
))
354 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
358 (setq ex-token-type
'plus
))
359 ((looking-at "+[0-9]")
361 (setq ex-token-type
'plus
))
363 (error viper-BadAddress
))))
365 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
369 (setq ex-token-type
'minus
))
370 ((looking-at "-[0-9]")
372 (setq ex-token-type
'minus
))
374 (error viper-BadAddress
))))
379 (while (and (not (eolp)) cont
)
380 ;;(re-search-forward "[^/]*/")
381 (re-search-forward "[^/]*\\(/\\|\n\\)")
382 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
385 (setq ex-token
(buffer-substring (point) (mark t
)))
386 (if (looking-at "/") (forward-char 1))
387 (setq ex-token-type
'search-forward
))
392 (while (and (not (eolp)) cont
)
393 ;;(re-search-forward "[^\\?]*\\?")
394 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
395 (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
398 (if (not (looking-at "\n")) (forward-char 1))))
399 (setq ex-token-type
'search-backward
)
400 (setq ex-token
(buffer-substring (1- (point)) (mark t
))))
403 (setq ex-token-type
'comma
))
406 (setq ex-token-type
'semi-colon
))
407 ((looking-at "[!=><&~]")
408 (setq ex-token-type
'command
)
409 (setq ex-token
(char-to-string (following-char)))
412 (setq ex-token-type
'goto-mark
)
414 (cond ((looking-at "'") (setq ex-token nil
))
415 ((looking-at "[a-z]") (setq ex-token
(following-char)))
416 (t (error "Marks are ' and a-z")))
419 (setq ex-token-type
'end-mark
)
420 (setq ex-token
"goto"))
422 (error viper-BadExCommand
))))))
424 ;; Reads Ex command. Tries to determine if it has to exit because command
425 ;; is complete or invalid. If not, keeps reading command.
426 (defun ex-cmd-read-exit ()
428 (setq viper-incomplete-ex-cmd t
)
429 (let ((quit-regex1 (concat
432 "\\|" "[nN]ext[ \t]*"
445 "\\|" "[ktgjmsz][ \t]*$"
447 "\\|" "tr[ansfer \t]*"
450 "\\|" "^[ \t]*k?ma[^p]*"
459 "\\|" "![ \t]*[a-zA-Z].*"
463 (save-window-excursion ;; put cursor at the end of the Ex working buffer
464 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
465 (set-buffer viper-ex-work-buf
)
466 (goto-char (point-max)))
467 (cond ((viper-looking-back quit-regex1
) (exit-minibuffer))
468 ((viper-looking-back stay-regex
) (insert " "))
469 ((viper-looking-back quit-regex2
) (exit-minibuffer))
472 ;; complete Ex command
473 (defun ex-cmd-complete ()
475 (let (save-pos dist compl-list string-to-complete completion-result
)
478 (setq dist
(skip-chars-backward "[a-zA-Z!=>&~]")
482 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
484 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
485 ;; Preceding characters are not the ones allowed in an Ex command
486 ;; or we have typed past command name.
487 ;; Note: we didn't do parsing, so there may be surprises.
488 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
489 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
490 (looking-at "[^ \t\n\C-m]"))
492 (with-output-to-temp-buffer "*Completions*"
493 (display-completion-list
494 (viper-alist-to-list ex-token-alist
))))
495 ;; Preceding chars may be part of a command name
496 (setq string-to-complete
(buffer-substring save-pos
(point)))
497 (setq completion-result
498 (try-completion string-to-complete ex-token-alist
))
500 (cond ((eq completion-result t
) ; exact match--do nothing
501 (viper-tmp-insert-at-eob " (Sole completion)"))
502 ((eq completion-result nil
)
503 (viper-tmp-insert-at-eob " (No match)"))
504 (t ;; partial completion
506 (delete-region (point) (point-max))
507 (insert completion-result
)
508 (let (case-fold-search)
510 (viper-filter-alist (concat "^" completion-result
)
512 (if (> (length compl-list
) 1)
513 (with-output-to-temp-buffer "*Completions*"
514 (display-completion-list
515 (viper-alist-to-list (reverse compl-list
)))))))
520 (defun viper-ex (&optional string
)
525 (let* ((map (copy-keymap minibuffer-local-map
))
529 prev-token-type com-str
)
531 (viper-add-keymap viper-ex-cmd-map map
)
533 (setq com-str
(or string
(viper-read-string-with-history
537 (car viper-ex-history
)
539 (save-window-excursion
541 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
542 (set-buffer viper-ex-work-buf
)
543 (delete-region (point-min) (point-max))
544 (insert com-str
"\n")
545 (goto-char (point-min)))
546 (setq ex-token-type nil
550 (cond ((memq ex-token-type
'(command end-mark
))
551 (if address
(setq ex-addresses
(cons address ex-addresses
)))
552 (cond ((string= ex-token
"global")
555 ((string= ex-token
"vglobal")
559 (viper-execute-ex-command)
560 (save-window-excursion
561 (setq viper-ex-work-buf
562 (get-buffer-create viper-ex-work-buf-name
))
563 (set-buffer viper-ex-work-buf
)
564 (skip-chars-forward " \t")
565 (cond ((looking-at "|")
569 (t (error "`%s': %s" ex-token viper-SpuriousText
)))
572 ((eq ex-token-type
'non-command
)
573 (error "`%s': %s" ex-token viper-BadExCommand
))
574 ((eq ex-token-type
'whole
)
578 (cons (point-max) ex-addresses
)
579 (cons (point-max) (cons (point-min) ex-addresses
)))))
580 ((eq ex-token-type
'comma
)
581 (if (eq prev-token-type
'whole
)
582 (setq address
(point-min)))
584 (cons (if (null address
) (point) address
) ex-addresses
)))
585 ((eq ex-token-type
'semi-colon
)
586 (if (eq prev-token-type
'whole
)
587 (setq address
(point-min)))
588 (if address
(setq dot address
))
590 (cons (if (null address
) (point) address
) ex-addresses
)))
591 (t (let ((ans (viper-get-ex-address-subr address dot
)))
592 (if ans
(setq address ans
)))))
593 (setq prev-token-type ex-token-type
))))
596 ;; Get a regular expression and set `ex-variant', if found
597 (defun viper-get-ex-pat ()
598 (save-window-excursion
599 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
600 (set-buffer viper-ex-work-buf
)
601 (skip-chars-forward " \t")
604 (setq ex-g-variant
(not ex-g-variant
)
605 ex-g-flag
(not ex-g-flag
))
607 (skip-chars-forward " \t")))
608 (let ((c (following-char)))
609 (if (string-match "[0-9A-Za-z]" (format "%c" c
))
611 "Global regexp must be inside matching non-alphanumeric chars"))
612 (if (looking-at "[^\\\\\n]")
617 (while (and (not (eolp)) cont
)
618 (if (not (re-search-forward (format "[^%c]*%c" c c
) nil t
))
619 (if (member ex-token
'("global" "vglobal"))
621 "Missing closing delimiter for global regexp")
622 (goto-char (point-max))))
623 (if (not (viper-looking-back
624 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c
)))
627 (if (= (mark t
) (point)) ""
628 (buffer-substring (1- (point)) (mark t
))))
630 ;; if the user doesn't specify the final pattern delimiter, we're
631 ;; at newline now. In this case, insert the initial delimiter
632 ;; specified in variable c
633 (if (looking-at "\n")
642 (defun viper-get-ex-command ()
643 (save-window-excursion
644 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
645 (set-buffer viper-ex-work-buf
)
646 (if (looking-at "/") (forward-char 1))
647 (skip-chars-forward " \t")
648 (cond ((looking-at "[a-z]")
649 (viper-get-ex-com-subr)
650 (if (eq ex-token-type
'non-command
)
651 (error "`%s': %s" ex-token viper-BadExCommand
)))
652 ((looking-at "[!=><&~]")
653 (setq ex-token
(char-to-string (following-char)))
655 (t (error viper-BadExCommand
)))))
657 ;; Get an Ex option g or c
658 (defun viper-get-ex-opt-gc (c)
659 (save-window-excursion
660 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
661 (set-buffer viper-ex-work-buf
)
662 (if (looking-at (format "%c" c
)) (forward-char 1))
663 (skip-chars-forward " \t")
664 (cond ((looking-at "g")
674 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
675 (defun viper-default-ex-addresses (&optional whole-flag
)
676 (cond ((null ex-addresses
)
679 (cons (point-max) (cons (point-min) nil
))
680 (cons (point) (cons (point) nil
)))))
681 ((null (cdr ex-addresses
))
683 (cons (car ex-addresses
) ex-addresses
)))))
685 ;; Get an ex-address as a marker and set ex-flag if a flag is found
686 (defun viper-get-ex-address ()
687 (let ((address (point-marker))
693 (cond ((eq ex-token-type
'command
)
694 (if (member ex-token
'("print" "list" "#"))
698 (error "Address expected in this Ex command")))
699 ((eq ex-token-type
'end-mark
)
701 ((eq ex-token-type
'whole
)
702 (error "Trailing address expected"))
703 ((eq ex-token-type
'comma
)
704 (error "`%s': %s" ex-token viper-SpuriousText
))
705 (t (let ((ans (viper-get-ex-address-subr address
(point-marker))))
706 (if ans
(setq address ans
))))))
709 ;; Returns an address as a point
710 (defun viper-get-ex-address-subr (old-address dot
)
712 (if (null old-address
) (setq old-address dot
))
713 (cond ((eq ex-token-type
'dot
)
715 ((eq ex-token-type
'add-number
)
717 (goto-char old-address
)
718 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
719 (setq address
(point-marker))))
720 ((eq ex-token-type
'sub-number
)
722 (goto-char old-address
)
723 (forward-line (- ex-token
))
724 (setq address
(point-marker))))
725 ((eq ex-token-type
'abs-number
)
727 (goto-char (point-min))
728 (if (= ex-token
0) (setq address
0)
729 (forward-line (1- ex-token
))
730 (setq address
(point-marker)))))
731 ((eq ex-token-type
'end
)
732 (setq address
(point-max-marker)))
733 ((eq ex-token-type
'plus
) t
) ; do nothing
734 ((eq ex-token-type
'minus
) t
) ; do nothing
735 ((eq ex-token-type
'search-forward
)
737 (ex-search-address t
)
738 (setq address
(point-marker))))
739 ((eq ex-token-type
'search-backward
)
741 (ex-search-address nil
)
742 (setq address
(point-marker))))
743 ((eq ex-token-type
'goto-mark
)
746 (exchange-point-and-mark)
747 (goto-char (viper-register-to-point
748 (1+ (- ex-token ?a
)) 'enforce-buffer
)))
749 (setq address
(point-marker)))))
753 ;; Search pattern and set address
754 (defun ex-search-address (forward)
755 (if (string= ex-token
"")
756 (if (null viper-s-string
)
757 (error viper-NoPrevSearch
)
758 (setq ex-token viper-s-string
))
759 (setq viper-s-string ex-token
))
763 (re-search-forward ex-token
))
765 (re-search-backward ex-token
)))
767 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
768 (defun viper-get-ex-buffer ()
772 (save-window-excursion
773 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
774 (set-buffer viper-ex-work-buf
)
775 (skip-chars-forward " \t")
776 (if (looking-at "[a-zA-Z]")
778 (setq ex-buffer
(following-char))
780 (skip-chars-forward " \t")))
781 (if (looking-at "[0-9]")
784 (re-search-forward "[0-9][0-9]*")
785 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
786 (skip-chars-forward " \t")))
787 (if (looking-at "[pl#]")
791 (if (not (looking-at "[\n|]"))
792 (error "`%s': %s" ex-token viper-SpuriousText
))))
794 (defun viper-get-ex-count ()
798 (save-window-excursion
799 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
800 (set-buffer viper-ex-work-buf
)
801 (skip-chars-forward " \t")
806 (skip-chars-forward " \t")
807 (if (looking-at "[0-9]")
810 (re-search-forward "[0-9][0-9]*")
811 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
812 (skip-chars-forward " \t")))
813 (if (looking-at "[pl#]")
817 (if (not (looking-at "[\n|]"))
820 (point-min) (1- (point-max))) viper-BadExCommand
))))
822 ;; Expand \% and \# in ex command
823 (defun ex-expand-filsyms (cmd buf
)
827 (setq cf buffer-file-name
)
828 (setq pf
(ex-next nil t
))) ; this finds alternative file name
829 (if (and (null cf
) (string-match "[^\\]%\\|\\`%" cmd
))
830 (error "No current file to substitute for `%%'"))
831 (if (and (null pf
) (string-match "[^\\]#\\|\\`#" cmd
))
832 (error "No alternate file to substitute for `#'"))
834 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
837 (goto-char (point-min))
838 (while (re-search-forward "%\\|#" nil t
)
839 (let ((data (match-data))
840 (char (buffer-substring (match-beginning 0) (match-end 0))))
841 (if (viper-looking-back (concat "\\\\" char
))
843 (store-match-data data
)
844 (if (string= char
"%")
846 (replace-match pf
)))))
848 (setq ret
(buffer-substring (point-min) (point)))
852 ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
853 (defun viper-get-ex-file ()
861 (save-window-excursion
862 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
863 (set-buffer viper-ex-work-buf
)
864 (skip-chars-forward " \t")
866 (if (and (not (viper-looking-back "[ \t]"))
867 ;; read doesn't have a corresponding :r! form, so ! is
868 ;; immediately interpreted as a shell command.
869 (not (string= ex-token
"read")))
873 (skip-chars-forward " \t"))
876 (skip-chars-forward " \t")))
877 (if (looking-at ">>")
882 (skip-chars-forward " \t")))
887 (re-search-forward "[ \t\n]")
889 (setq ex-offset
(buffer-substring (point) (mark t
)))
891 (skip-chars-forward " \t")))
892 ;; this takes care of :r, :w, etc., when they get file names
893 ;; from the history list
894 (if (member ex-token
'("read" "write" "edit" "visual" "next"))
896 (setq ex-file
(buffer-substring (point) (1- (point-max))))
898 ;; For :e, match multiple non-white strings separated
899 ;; by white. For others, find the first non-white string
901 (if (string= ex-token
"edit")
902 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
906 ;; if file name comes from history, don't leave
907 ;; minibuffer when the user types space
908 (setq viper-incomplete-ex-cmd nil
)
909 ;; this must be the last clause in this progn
910 (substring ex-file
(match-beginning 0) (match-end 0))
913 ;; this leaves only the command name in the work area
914 ;; file names are gone
915 (delete-region (point) (1- (point-max)))
917 (goto-char (point-max))
918 (skip-chars-backward " \t\n")
919 (setq prompt
(buffer-substring (point-min) (point)))
922 (setq viper-last-ex-prompt prompt
)
924 ;; If we just finished reading command, redisplay prompt
925 (if viper-incomplete-ex-cmd
926 (setq ex-file
(viper-ex-read-file-name (format ":%s " prompt
)))
927 ;; file was typed in-line
928 (setq ex-file
(or ex-file
"")))
932 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
933 ;; file names, arranges to re-enter the minibuffer.
934 (defun viper-complete-filename-or-exit ()
936 (setq viper-keep-reading-filename t
)
937 ;; don't exit if directory---ex-commands don't
938 (cond ((ex-cmd-accepts-multiple-files-p ex-token
) (exit-minibuffer))
939 ;; apparently the argument to an Ex command is
940 ;; supposed to be a shell command
941 ((viper-looking-back "^[ \t]*!.*")
945 (setq ex-cmdfile nil
)
946 (minibuffer-complete-word))))
948 (defun viper-handle-! ()
951 (buffer-string) (viper-abbreviate-file-name default-directory
))
952 (member ex-token
'("read" "write")))
956 (defun ex-cmd-accepts-multiple-files-p (token)
957 (member token
'("edit" "next" "Next")))
959 ;; If user doesn't enter anything, then "" is returned, i.e., the
960 ;; prompt-directory is not returned.
961 (defun viper-ex-read-file-name (prompt)
963 (minibuffer-local-completion-map
964 (copy-keymap minibuffer-local-completion-map
))
967 (viper-add-keymap ex-read-filename-map
969 minibuffer-local-completion-map
972 (setq cont
(setq viper-keep-reading-filename t
))
974 (setq viper-keep-reading-filename nil
975 val
(read-file-name (concat prompt str
) nil default-directory
))
976 (if (string-match " " val
)
977 (setq val
(concat "\\\"" val
"\\\"")))
978 (setq str
(concat str
(if (equal val
"") "" " ")
979 val
(if (equal val
"") "" " ")))
981 ;; Only edit, next, and Next commands accept multiple files.
982 ;; viper-keep-reading-filename is set in the anonymous function that is
983 ;; bound to " " in ex-read-filename-map.
984 (setq cont
(and viper-keep-reading-filename
985 (ex-cmd-accepts-multiple-files-p ex-token
)))
988 (setq beg
(string-match "[^ \t]" str
) ; delete leading blanks
989 end
(string-match "[ \t]*$" str
)) ; delete trailing blanks
990 (if (member ex-token
'("read" "write"))
991 (if (string-match "[\t ]*!" str
)
992 ;; this is actually a shell command
996 (setq viper-last-ex-prompt
997 (concat viper-last-ex-prompt
" !")))))
998 (substring str
(or beg
0) end
)))
1000 ;; Execute ex command using the value of addresses
1001 (defun viper-execute-ex-command ()
1002 (viper-deactivate-mark)
1003 (cond ((string= ex-token
"args") (ex-args))
1004 ((string= ex-token
"copy") (ex-copy nil
))
1005 ((string= ex-token
"cd") (ex-cd))
1006 ((string= ex-token
"chdir") (ex-cd))
1007 ((string= ex-token
"delete") (ex-delete))
1008 ((string= ex-token
"edit") (ex-edit))
1009 ((string= ex-token
"file") (viper-info-on-file))
1010 ((string= ex-token
"goto") (ex-goto))
1011 ((string= ex-token
"help") (ex-help))
1012 ((string= ex-token
"join") (ex-line "join"))
1013 ((string= ex-token
"kmark") (ex-mark))
1014 ((string= ex-token
"mark") (ex-mark))
1015 ((string= ex-token
"map") (ex-map))
1016 ((string= ex-token
"move") (ex-copy t
))
1017 ((string= ex-token
"next") (ex-next ex-cycle-other-window
))
1018 ((string= ex-token
"Next") (ex-next (not ex-cycle-other-window
)))
1019 ((string= ex-token
"RelatedFile") (ex-next-related-buffer 1))
1020 ((string= ex-token
"put") (ex-put))
1021 ((string= ex-token
"pwd") (ex-pwd))
1022 ((string= ex-token
"preserve") (ex-preserve))
1023 ((string= ex-token
"PreviousRelatedFile") (ex-next-related-buffer -
1))
1024 ((string= ex-token
"quit") (ex-quit))
1025 ((string= ex-token
"read") (ex-read))
1026 ((string= ex-token
"recover") (ex-recover))
1027 ((string= ex-token
"rewind") (ex-rewind))
1028 ((string= ex-token
"submitReport") (viper-submit-report))
1029 ((string= ex-token
"set") (ex-set))
1030 ((string= ex-token
"shell") (ex-shell))
1031 ((string= ex-token
"source") (ex-source))
1032 ((string= ex-token
"sr") (ex-substitute t t
))
1033 ((string= ex-token
"substitute") (ex-substitute))
1034 ((string= ex-token
"suspend") (suspend-emacs))
1035 ((string= ex-token
"stop") (suspend-emacs))
1036 ((string= ex-token
"transfer") (ex-copy nil
))
1037 ((string= ex-token
"buffer") (if ex-cycle-other-window
1038 (viper-switch-to-buffer-other-window)
1039 (viper-switch-to-buffer)))
1040 ((string= ex-token
"Buffer") (if ex-cycle-other-window
1041 (viper-switch-to-buffer)
1042 (viper-switch-to-buffer-other-window)))
1043 ((string= ex-token
"tag") (ex-tag))
1044 ((string= ex-token
"undo") (viper-undo))
1045 ((string= ex-token
"unmap") (ex-unmap))
1046 ((string= ex-token
"version") (viper-version))
1047 ((string= ex-token
"visual") (ex-edit))
1048 ((string= ex-token
"write") (ex-write nil
))
1049 ((string= ex-token
"Write") (save-some-buffers))
1050 ((string= ex-token
"wq") (ex-write t
))
1051 ((string= ex-token
"WWrite") (save-some-buffers t
)) ; don't ask
1052 ((string= ex-token
"xit") (ex-write t
))
1053 ((string= ex-token
"yank") (ex-yank))
1054 ((string= ex-token
"!") (ex-command))
1055 ((string= ex-token
"=") (ex-line-no))
1056 ((string= ex-token
">") (ex-line "right"))
1057 ((string= ex-token
"<") (ex-line "left"))
1058 ((string= ex-token
"&") (ex-substitute t
))
1059 ((string= ex-token
"~") (ex-substitute t t
))
1060 ((or (string= ex-token
"append")
1061 (string= ex-token
"change")
1062 (string= ex-token
"insert")
1063 (string= ex-token
"open"))
1064 (error "`%s': Obsolete command, not supported by Viper" ex-token
))
1065 ((or (string= ex-token
"abbreviate")
1066 (string= ex-token
"unabbreviate"))
1068 "`%s': Vi abbrevs are obsolete. Use the more powerful Emacs abbrevs"
1070 ((or (string= ex-token
"list")
1071 (string= ex-token
"print")
1072 (string= ex-token
"z")
1073 (string= ex-token
"#"))
1074 (error "`%s': Command not implemented in Viper" ex-token
))
1075 (t (error "`%s': %s" ex-token viper-BadExCommand
))))
1077 (defun viper-undisplayed-files ()
1081 (if (null (get-buffer-window b
))
1082 (let ((f (buffer-file-name b
)))
1084 (if ex-cycle-through-non-files
1085 (let ((s (buffer-name b
)))
1086 (if (string= " " (substring s
0 1))
1095 (let ((l (viper-undisplayed-files))
1098 (while (not (null l
))
1100 (setq args
(format "%s %d) %s\n" args file-count
(car l
))
1101 file-count
(1+ file-count
)))
1103 (if (string= args
"")
1104 (message "All files are already displayed")
1106 (save-window-excursion
1107 (with-output-to-temp-buffer " *viper-info*"
1108 (princ "\n\nThese files are not displayed in any window.\n")
1109 (princ "\n=============\n")
1111 (princ "\n=============\n")
1112 (princ "\nThe numbers can be given as counts to :next. ")
1113 (princ "\n\nPress any key to continue...\n\n"))
1114 (viper-read-event))))))
1116 ;; Ex cd command. Default directory of this buffer changes
1119 (if (string= ex-file
"")
1121 (setq default-directory
(file-name-as-directory (expand-file-name ex-file
))))
1123 ;; Ex copy and move command. DEL-FLAG means delete
1124 (defun ex-copy (del-flag)
1125 (viper-default-ex-addresses)
1126 (let ((address (viper-get-ex-address))
1127 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1131 (viper-enlarge-region (mark t
) (point))
1133 (kill-region (point) (mark t
))
1134 (copy-region-as-kill (point) (mark t
)))
1137 (with-output-to-temp-buffer "*copy text*"
1139 (if (or del-flag ex-g-flag ex-g-variant
)
1141 (buffer-substring (point) (mark t
)))))
1144 (read-string "[Hit return to continue] ")
1145 (save-excursion (kill-buffer "*copy text*")))
1146 (quit (save-excursion (kill-buffer "*copy text*"))
1147 (signal 'quit nil
))))))
1149 (goto-char (point-min))
1152 (insert (current-kill 0))))
1154 ;; Ex delete command
1156 (viper-default-ex-addresses)
1157 (viper-get-ex-buffer)
1158 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1159 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1161 (viper-enlarge-region beg end
)
1162 (exchange-point-and-mark)
1166 (forward-line (1- ex-count
)))
1168 (viper-enlarge-region (point) (mark t
))
1170 ;; show text to be deleted and ask for confirmation
1172 (with-output-to-temp-buffer " *delete text*"
1173 (princ (buffer-substring (point) (mark t
))))
1175 (read-string "[Hit return to continue] ")
1177 (save-excursion (kill-buffer " *delete text*"))
1179 (save-excursion (kill-buffer " *delete text*")))
1181 (cond ((viper-valid-register ex-buffer
'(Letter))
1182 (viper-append-to-register
1183 (downcase ex-buffer
) (point) (mark t
)))
1184 ((viper-valid-register ex-buffer
)
1185 (copy-to-register ex-buffer
(point) (mark t
) nil
))
1186 (t (error viper-InvalidRegister ex-buffer
))))
1187 (kill-region (point) (mark t
))))))
1192 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1193 ;; asked if current buffer should really be discarded.
1194 ;; This command can take multiple file names. It replaces the current buffer
1195 ;; with the first file in its argument list
1196 (defun ex-edit (&optional file
)
1198 (viper-get-ex-file))
1199 (cond ((and (string= ex-file
"") buffer-file-name
)
1200 (setq ex-file
(viper-abbreviate-file-name (buffer-file-name))))
1201 ((string= ex-file
"")
1202 (error viper-NoFileSpecified
)))
1205 (if buffer-file-name
1206 (cond ((buffer-modified-p)
1208 (format "Buffer %s is modified. Discard changes? "
1211 ((not (verify-visited-file-modtime (current-buffer)))
1213 (format "File %s changed on disk. Reread from disk? "
1216 (t (setq do-edit nil
))))
1219 (if (yes-or-no-p msg
)
1221 (set-buffer-modified-p nil
)
1222 (kill-buffer (current-buffer)))
1223 (message "Buffer %s was left intact" (buffer-name))))
1226 (if (null (setq file
(get-file-buffer ex-file
)))
1228 (ex-find-file ex-file
)
1229 (or (eq major-mode
'dired-mode
)
1230 (viper-change-state-to-vi))
1231 (goto-char (point-min)))
1232 (switch-to-buffer file
))
1235 (save-window-excursion
1236 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1237 (set-buffer viper-ex-work-buf
)
1238 (delete-region (point-min) (point-max))
1239 (insert ex-offset
"\n")
1240 (goto-char (point-min)))
1241 (goto-char (viper-get-ex-address))
1242 (beginning-of-line)))
1243 (ex-fixup-history viper-last-ex-prompt ex-file
))
1245 ;; Find-file FILESPEC if it appears to specify a single file.
1246 ;; Otherwise, assume that FILES{EC is a wildcard.
1247 ;; In this case, split it into substrings separated by newlines.
1248 ;; Each line is assumed to be a file name. find-file's each file thus obtained.
1249 (defun ex-find-file (filespec)
1250 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1251 (cond ((file-exists-p filespec
) (find-file filespec
))
1252 ((string-match nonstandard-filename-chars filespec
)
1253 (funcall ex-nontrivial-find-file-function filespec
))
1254 (t (find-file filespec
)))
1258 ;; Ex global command
1259 ;; This is executed in response to:
1260 ;; :global "pattern" ex-command
1261 ;; :vglobal "pattern" ex-command
1262 ;; :global executes ex-command on all lines matching <pattern>
1263 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1265 ;; With VARIANT nil, this functions executes :global
1266 ;; With VARIANT t, executes :vglobal
1267 (defun ex-global (variant)
1268 (let ((gcommand ex-token
))
1269 (if (or ex-g-flag ex-g-variant
)
1270 (error "`%s' within `global' is not allowed" gcommand
)
1278 (error "`%s': Missing regular expression" gcommand
)))
1280 (if (string= ex-token
"")
1281 (if (null viper-s-string
)
1282 (error viper-NoPrevSearch
)
1283 (setq ex-g-pat viper-s-string
))
1284 (setq ex-g-pat ex-token
1285 viper-s-string ex-token
))
1286 (if (null ex-addresses
)
1287 (setq ex-addresses
(list (point-max) (point-min)))
1288 (viper-default-ex-addresses))
1291 (end (car ex-addresses
))
1292 (beg (car (cdr ex-addresses
)))
1294 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1296 (viper-enlarge-region beg end
)
1297 (exchange-point-and-mark)
1298 (let ((cont t
) (limit (point-marker)))
1299 (exchange-point-and-mark)
1300 ;; skip the last line if empty
1302 (if (eobp) (viper-backward-char-carefully))
1303 (while (and cont
(not (bobp)) (>= (point) limit
))
1307 (let ((found (re-search-backward ex-g-pat
(mark t
) t
)))
1308 (if (or (and ex-g-flag found
)
1309 (and ex-g-variant
(not found
)))
1312 (setq mark-count
(1+ mark-count
))
1313 (setq marks
(cons (point-marker) marks
)))))
1315 (if (bobp) (setq cont nil
)
1318 (save-window-excursion
1319 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1320 (set-buffer viper-ex-work-buf
)
1321 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
1323 (goto-char (car marks
))
1325 (setq mark-count
(1- mark-count
))
1326 (setq marks
(cdr marks
)))))
1330 (if (null ex-addresses
)
1331 (setq ex-addresses
(cons (point) nil
)))
1332 (push-mark (point) t
)
1333 (goto-char (car ex-addresses
))
1334 (beginning-of-line))
1336 ;; Ex line commands. COM is join, shift-right or shift-left
1337 (defun ex-line (com)
1338 (viper-default-ex-addresses)
1339 (viper-get-ex-count)
1340 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
1341 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1343 (viper-enlarge-region beg end
)
1344 (exchange-point-and-mark)
1348 (forward-line ex-count
)))
1350 ;; show text to be joined and ask for confirmation
1352 (with-output-to-temp-buffer " *text*"
1353 (princ (buffer-substring (point) (mark t
))))
1356 (read-string "[Hit return to continue] ")
1357 (ex-line-subr com
(point) (mark t
)))
1359 (save-excursion (kill-buffer " *text*")))
1360 (ex-line-subr com
(point) (mark t
)))
1361 (setq point
(point)))
1362 (goto-char (1- point
))
1363 (beginning-of-line)))
1365 (defun ex-line-subr (com beg end
)
1366 (cond ((string= com
"join")
1367 (goto-char (min beg end
))
1368 (while (and (not (eobp)) (< (point) (max beg end
)))
1370 (if (and (<= (point) (max beg end
)) (not (eobp)))
1373 (delete-region (point) (1- (point)))
1374 (if (not ex-variant
) (fixup-whitespace))))))
1375 ((or (string= com
"right") (string= com
"left"))
1377 (min beg end
) (max beg end
)
1378 (if (string= com
"right") viper-shift-width
(- viper-shift-width
)))
1379 (goto-char (max beg end
))
1381 (viper-forward-char-carefully))))
1387 (if (null ex-addresses
)
1389 (cons (point) nil
)))
1390 (save-window-excursion
1391 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1392 (set-buffer viper-ex-work-buf
)
1393 (skip-chars-forward " \t")
1394 (if (looking-at "[a-z]")
1396 (setq char
(following-char))
1398 (skip-chars-forward " \t")
1399 (if (not (looking-at "[\n|]"))
1400 (error "`%s': %s" ex-token viper-SpuriousText
)))
1401 (error "`%s' requires a following letter" ex-token
)))
1403 (goto-char (car ex-addresses
))
1404 (point-to-register (1+ (- char ?a
))))))
1408 ;; Alternate file is the file next to the first one in the buffer ring
1409 (defun ex-next (cycle-other-window &optional find-alt-file
)
1412 (if (not find-alt-file
)
1415 (if (or (char-or-string-p ex-offset
)
1416 (and (not (string= "" ex-file
))
1417 (not (string-match "^[0-9]+$" ex-file
))))
1420 (throw 'ex-edit nil
))
1421 (setq count
(string-to-int ex-file
))
1422 (if (= count
0) (setq count
1))
1423 (if (< count
0) (error "Usage: `next <count>' (count >= 0)"))))
1425 (setq l
(viper-undisplayed-files))
1427 (while (and (not (null l
)) (null (car l
)))
1429 (setq count
(1- count
))
1432 (if find-alt-file
(car l
)
1434 (if (and (car l
) (get-file-buffer (car l
)))
1435 (let* ((w (if cycle-other-window
1436 (get-lru-window) (selected-window)))
1437 (b (window-buffer w
)))
1438 (set-window-buffer w
(get-file-buffer (car l
)))
1440 ;; this puts "next <count>" in the ex-command history
1441 (ex-fixup-history viper-last-ex-prompt ex-file
))
1442 (error "Not that many undisplayed files")))))))
1445 (defun ex-next-related-buffer (direction &optional no-recursion
)
1447 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction
)
1449 (let ((file-or-buffer-name
1450 (viper-current-ring-item viper-related-files-and-buffers-ring
))
1451 (old-ring viper-related-files-and-buffers-ring
)
1452 (old-win (selected-window))
1455 (or (and (ring-p viper-related-files-and-buffers-ring
)
1456 (> (ring-length viper-related-files-and-buffers-ring
) 0))
1457 (error "This buffer has no related files or buffers"))
1459 (or (stringp file-or-buffer-name
)
1461 "File and buffer names must be strings, %S" file-or-buffer-name
))
1463 (setq buf
(cond ((get-buffer file-or-buffer-name
))
1464 ((file-exists-p file-or-buffer-name
)
1465 (find-file-noselect file-or-buffer-name
))
1468 (if (not (viper-buffer-live-p buf
))
1469 (error "Didn't find buffer %S or file %S"
1471 (viper-abbreviate-file-name
1472 (expand-file-name file-or-buffer-name
))))
1474 (if (equal buf
(current-buffer))
1479 (ex-next-related-buffer direction
'norecursion
))))
1484 (if (setq wind
(viper-get-visible-buffer-window buf
))
1486 (setq wind
(get-lru-window (if viper-xemacs-p nil
'visible
)))
1487 (set-window-buffer wind buf
))
1489 (if (viper-window-display-p)
1491 (raise-frame (window-frame wind
))
1492 (if (equal (window-frame wind
) (window-frame old-win
))
1493 (save-window-excursion (select-window wind
) (sit-for 1))
1494 (select-window wind
)))
1495 (save-window-excursion (select-window wind
) (sit-for 1)))
1499 (setq viper-related-files-and-buffers-ring old-ring
))
1501 (setq viper-local-search-start-marker
(point-marker))
1506 (defun ex-preserve ()
1507 (message "Autosaving all buffers that need to be saved...")
1512 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
1513 (viper-get-ex-buffer)
1514 (setq viper-use-register ex-buffer
)
1516 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
1518 ;; Ex print working directory
1520 (message default-directory
))
1524 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1526 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1527 (set-buffer viper-ex-work-buf
)
1528 (if (looking-at "!") (forward-char 1)))
1529 (if (< viper-expert-level
3)
1530 (save-buffers-kill-emacs)
1531 (kill-buffer (current-buffer))))
1537 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
1540 (viper-add-newline-at-eob-if-necessary)
1541 (if (not (or (bobp) (eobp))) (forward-line 1))
1542 (if (and (not ex-variant
) (string= ex-file
""))
1544 (if (null buffer-file-name
)
1545 (error viper-NoFileSpecified
))
1546 (setq ex-file buffer-file-name
)))
1549 (setq command
(ex-expand-filsyms ex-file
(current-buffer)))
1550 (shell-command command t
))
1551 (insert-file-contents ex-file
)))
1552 (ex-fixup-history viper-last-ex-prompt ex-file
))
1554 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1555 (defun ex-fixup-history (&rest args
)
1556 (setq viper-ex-history
1557 (cons (mapconcat 'identity args
" ") (cdr viper-ex-history
))))
1560 ;; Ex recover from emacs \#file\#
1561 (defun ex-recover ()
1563 (if (or ex-append ex-offset
)
1564 (error "`recover': %s" viper-SpuriousText
))
1565 (if (string= ex-file
"")
1567 (if (null buffer-file-name
)
1568 (error "This buffer isn't visiting any file"))
1569 (setq ex-file buffer-file-name
))
1570 (setq ex-file
(expand-file-name ex-file
)))
1571 (if (and (not (string= ex-file
(buffer-file-name)))
1574 (error "No write since last change \(:rec! overrides\)"))
1575 (recover-file ex-file
))
1577 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1580 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1583 ;; read variable name for ex-set
1584 (defun ex-set-read-variable ()
1585 (let ((minibuffer-local-completion-map
1586 (copy-keymap minibuffer-local-completion-map
))
1587 (cursor-in-echo-area t
)
1590 minibuffer-local-completion-map
" " 'minibuffer-complete-and-exit
)
1591 (define-key minibuffer-local-completion-map
"=" 'exit-minibuffer
)
1592 (if (viper-set-unread-command-events
1593 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil
"\C-m"))
1596 (viper-set-unread-command-events ?\C-m
)))
1597 (message ":set <Variable> [= <Value>]")
1598 (or batch
(sit-for 2))
1600 (while (string-match "^[ \\t\\n]*$"
1602 (completing-read ":set " ex-variable-alist
)))
1603 (message ":set <Variable> [= <Value>]")
1604 ;; if there are unread events, don't wait
1605 (or (viper-set-unread-command-events "") (sit-for 2))
1611 (let ((var (ex-set-read-variable))
1615 (auto-cmd-label "; don't touch or else...")
1616 (delete-turn-on-auto-fill-pattern
1617 "([ \t]*add-hook[ \t]+'viper-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1618 actual-lisp-cmd lisp-cmd-del-pattern
1621 (cond ((string= var
"all")
1622 (setq ask-if-save nil
1624 ((member var
'("ai" "autoindent"))
1625 (setq var
"viper-auto-indent"
1629 ((member var
'("ai-g" "autoindent-global"))
1630 (kill-local-variable 'viper-auto-indent
)
1631 (setq var
"viper-auto-indent"
1632 set-cmd
"setq-default"
1634 ((member var
'("noai" "noautoindent"))
1635 (setq var
"viper-auto-indent"
1638 ((member var
'("noai-g" "noautoindent-global"))
1639 (kill-local-variable 'viper-auto-indent
)
1640 (setq var
"viper-auto-indent"
1641 set-cmd
"setq-default"
1643 ((member var
'("ic" "ignorecase"))
1644 (setq var
"viper-case-fold-search"
1646 ((member var
'("noic" "noignorecase"))
1647 (setq var
"viper-case-fold-search"
1649 ((member var
'("ma" "magic"))
1650 (setq var
"viper-re-search"
1652 ((member var
'("noma" "nomagic"))
1653 (setq var
"viper-re-search"
1655 ((member var
'("ro" "readonly"))
1656 (setq var
"buffer-read-only"
1658 ((member var
'("noro" "noreadonly"))
1659 (setq var
"buffer-read-only"
1661 ((member var
'("sm" "showmatch"))
1662 (setq var
"blink-matching-paren"
1664 ((member var
'("nosm" "noshowmatch"))
1665 (setq var
"blink-matching-paren"
1667 ((member var
'("ws" "wrapscan"))
1668 (setq var
"viper-search-wrap-around-t"
1670 ((member var
'("nows" "nowrapscan"))
1671 (setq var
"viper-search-wrap-around-t"
1673 (if (and set-cmd
(eq val
0)) ; value must be set by the user
1674 (let ((cursor-in-echo-area t
))
1675 (message ":set %s = <Value>" var
)
1676 ;; if there are unread events, don't wait
1677 (or (viper-set-unread-command-events "") (sit-for 2))
1678 (setq val
(read-string (format ":set %s = " var
)))
1679 (ex-fixup-history "set" orig-var val
)
1681 ;; check numerical values
1685 "ts-g" "tabstop-global"
1688 (or (numberp (setq val2
(car (read-from-string val
))))
1689 (error "%s: Invalid value, numberp, %S" var val
))
1691 (error "%s: Invalid value, numberp, %S" var val
))))
1694 ((member var
'("sw" "shiftwidth"))
1695 (setq var
"viper-shift-width"))
1696 ((member var
'("ts" "tabstop"))
1697 ;; make it take effect in curr buff and new bufs
1698 (setq var
"tab-width"
1701 ((member var
'("ts-g" "tabstop-global"))
1702 (kill-local-variable 'tab-width
)
1703 (setq var
"tab-width"
1704 set-cmd
"setq-default"))
1705 ((member var
'("wm" "wrapmargin"))
1706 ;; make it take effect in curr buff and new bufs
1707 (kill-local-variable 'fill-column
)
1708 (setq var
"fill-column"
1709 val
(format "(- (window-width) %s)" val
)
1710 set-cmd
"setq-default"))
1711 ((member var
'("sh" "shell"))
1712 (setq var
"explicit-shell-file-name"
1713 val
(format "\"%s\"" val
)))))
1714 (ex-fixup-history "set" orig-var
))
1717 (setq actual-lisp-cmd
1718 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label
)
1719 lisp-cmd-del-pattern
1720 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1721 set-cmd var auto-cmd-label
)))
1723 (if (and ask-if-save
1724 (y-or-n-p (format "Do you want to save this setting in %s "
1725 viper-custom-file-name
)))
1727 (viper-save-string-in-file
1728 actual-lisp-cmd viper-custom-file-name
1730 lisp-cmd-del-pattern
)
1731 (if (string= var
"fill-column")
1733 (viper-save-string-in-file
1735 "(add-hook 'viper-insert-state-hooks 'turn-on-auto-fill) "
1737 viper-custom-file-name
1738 delete-turn-on-auto-fill-pattern
)
1739 (viper-save-string-in-file
1740 nil viper-custom-file-name delete-turn-on-auto-fill-pattern
)
1741 (viper-save-string-in-file
1742 nil viper-custom-file-name
1744 lisp-cmd-del-pattern
)
1751 (if (string-match "^[ \t]*$" val
)
1755 (eval (car (read-from-string actual-lisp-cmd
))))
1756 (if (string= var
"fill-column")
1759 (auto-fill-mode -
1)))
1760 (if (string= var
"all") (ex-show-vars))
1763 ;; In inline args, skip regex-forw and (optionally) chars-back.
1764 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1766 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str
)
1768 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1769 (set-buffer viper-ex-work-buf
)
1770 (goto-char (point-min))
1771 (re-search-forward regex-forw nil t
)
1774 (goto-char (point-max))
1776 (skip-chars-backward chars-back
)
1777 (skip-chars-backward " \t\n\C-m"))
1779 ;; replace SPC with `=' to suppress the special meaning SPC has
1783 (while (re-search-forward " +" nil t
)
1784 (replace-match replace-str nil t
)
1785 (viper-forward-char-carefully)))
1787 (buffer-substring beg end
))))
1794 ;; Viper help. Invokes Info
1798 (pop-to-buffer (get-buffer-create "*info*"))
1799 (info (if viper-xemacs-p
"viper.info" "viper"))
1800 (message "Type `i' to search for a specific topic"))
1802 (with-output-to-temp-buffer " *viper-info*"
1804 The Info file for Viper does not seem to be installed.
1806 This file is part of the standard distribution of %sEmacs.
1807 Please contact your system administrator. "
1808 (if viper-xemacs-p
"X" "")
1811 ;; Ex source command. Loads the file specified as argument or `~/.viper'
1814 (if (string= ex-file
"")
1815 (load viper-custom-file-name
)
1818 ;; Ex substitute command
1819 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1820 (defun ex-substitute (&optional repeat r-flag
)
1824 (case-fold-search viper-case-fold-search
)
1826 (if repeat
(setq ex-token nil
) (setq delim
(viper-get-ex-pat)))
1829 (setq pat
(if r-flag viper-s-string ex-reg-exp
))
1831 (error "No previous pattern to use in substitution"))
1833 delim
(string-to-char pat
)))
1834 (setq pat
(if (string= ex-token
"") viper-s-string ex-token
))
1835 (setq viper-s-string pat
1837 (setq delim
(viper-get-ex-pat))
1843 (while (viper-get-ex-opt-gc delim
)
1844 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
1845 (viper-get-ex-count)
1848 (if ex-addresses
(goto-char (car ex-addresses
)))
1850 (forward-line (1- ex-count
))
1851 (setq ex-addresses
(cons (point) (cons (mark t
) nil
))))
1852 (if (null ex-addresses
)
1853 (setq ex-addresses
(cons (point) (cons (point) nil
)))
1854 (if (null (cdr ex-addresses
))
1855 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
1857 (let ((beg (car ex-addresses
))
1858 (end (car (cdr ex-addresses
)))
1861 (viper-enlarge-region beg end
)
1862 (let ((limit (save-excursion
1863 (goto-char (max (point) (mark t
)))
1865 (goto-char (min (point) (mark t
)))
1866 (while (< (point) limit
)
1868 (setq eol-mark
(point-marker))
1872 (while (and (not (eolp))
1873 (re-search-forward pat eol-mark t
))
1874 (if (or (not opt-c
) (y-or-n-p "Replace? "))
1876 (setq matched-pos
(point))
1877 (if (not (stringp repl
))
1878 (error "Can't perform Ex substitution: No previous replacement pattern"))
1879 (replace-match repl t
))))
1881 (viper-forward-char-carefully))
1884 "Can't repeat Ex substitution: No previous regular expression"))
1885 (if (and (re-search-forward pat eol-mark t
)
1886 (or (not opt-c
) (y-or-n-p "Replace? ")))
1888 (setq matched-pos
(point))
1889 (if (not (stringp repl
))
1890 (error "Can't perform Ex substitution: No previous replacement pattern"))
1891 (replace-match repl t
)))
1893 (viper-forward-char-carefully))))))
1894 (if matched-pos
(goto-char matched-pos
))
1896 (if opt-c
(message "done"))))
1901 (save-window-excursion
1902 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
1903 (set-buffer viper-ex-work-buf
)
1904 (skip-chars-forward " \t")
1906 (skip-chars-forward "^ |\t\n")
1907 (setq tag
(buffer-substring (mark t
) (point))))
1908 (if (not (string= tag
"")) (setq ex-tag tag
))
1909 (viper-change-state-to-emacs)
1910 (condition-case conds
1912 (if (string= tag
"")
1914 (find-tag-other-window ex-tag
))
1915 (viper-change-state-to-vi))
1917 (viper-change-state-to-vi)
1918 (viper-message-conditions conds
)))))
1921 (defun ex-write (q-flag)
1922 (viper-default-ex-addresses t
)
1924 (let ((end (car ex-addresses
))
1925 (beg (car (cdr ex-addresses
)))
1926 (orig-buf (current-buffer))
1927 (orig-buf-file-name (buffer-file-name))
1928 (orig-buf-name (buffer-name))
1929 (buff-changed-p (buffer-modified-p))
1930 temp-buf writing-same-file region
1931 file-exists writing-whole-file
)
1932 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
1935 (viper-enlarge-region beg end
)
1936 (shell-command-on-region (point) (mark t
) ex-file
))
1937 (if (and (string= ex-file
"") (not (buffer-file-name)))
1940 (format "Buffer %s isn't visiting any file. File to save in: "
1943 (setq writing-whole-file
(and (= (point-min) beg
) (= (point-max) end
))
1944 ex-file
(if (string= ex-file
"")
1946 (expand-file-name ex-file
)))
1947 ;; if ex-file is a directory use the file portion of the buffer file name
1948 (if (and (file-directory-p ex-file
)
1950 (not (file-directory-p buffer-file-name
)))
1952 (concat (file-name-as-directory ex-file
)
1953 (file-name-nondirectory buffer-file-name
))))
1955 (setq file-exists
(file-exists-p ex-file
)
1956 writing-same-file
(string= ex-file
(buffer-file-name)))
1958 (if (and writing-whole-file writing-same-file
)
1959 (if (not (buffer-modified-p))
1960 (message "(No changes need to be saved)")
1964 (ex-write-info file-exists ex-file
(point-min) (point-max))
1966 ;; writing some other file or portion of the current file
1967 (cond ((and file-exists
1968 (not writing-same-file
)
1970 (format "File %s exists. Overwrite? " ex-file
))))
1972 ((and writing-whole-file
(not ex-append
))
1975 (set-visited-file-name ex-file
)
1976 (set-buffer-modified-p t
)
1978 ;; restore the buffer file name
1979 (set-visited-file-name orig-buf-file-name
)
1980 (set-buffer-modified-p buff-changed-p
)
1981 ;; If the buffer wasn't visiting a file, restore buffer name.
1982 ;; Name could've been changed by packages such as uniquify.
1983 (or orig-buf-file-name
1986 (rename-buffer orig-buf-name
))))
1990 file-exists ex-file
(point-min) (point-max))))
1991 (t ; writing a region
1994 (viper-enlarge-region beg end
)
1995 (setq region
(buffer-substring (point) (mark t
)))
1996 ;; create temp buffer for the region
1997 (setq temp-buf
(get-buffer-create " *ex-write*"))
1998 (set-buffer temp-buf
)
1999 (set-visited-file-name ex-file
'noquerry
)
2001 (if (and file-exists ex-append
)
2002 (insert-file-contents ex-file
))
2003 (goto-char (point-max))
2007 file-exists ex-file
(point-min) (point-max))
2009 (set-buffer temp-buf
)
2010 (set-buffer-modified-p nil
)
2011 (kill-buffer temp-buf
))
2013 (set-buffer orig-buf
)
2014 ;; this prevents the loss of data if writing part of the buffer
2015 (if (and (buffer-file-name) writing-same-file
)
2016 (set-visited-file-modtime))
2017 (or writing-whole-file
2018 (not writing-same-file
)
2019 (set-buffer-modified-p t
))
2021 (if (< viper-expert-level
2)
2022 (save-buffers-kill-emacs)
2023 (kill-buffer (current-buffer))))
2027 (defun ex-write-info (exists file-name beg end
)
2028 (message "`%s'%s %d lines, %d characters"
2029 (viper-abbreviate-file-name file-name
)
2030 (if exists
"" " [New file]")
2031 (count-lines beg
(min (1+ end
) (point-max)))
2036 (viper-default-ex-addresses)
2037 (viper-get-ex-buffer)
2038 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2039 (if (> beg end
) (error viper-FirstAddrExceedsSecond
))
2041 (viper-enlarge-region beg end
)
2042 (exchange-point-and-mark)
2043 (if (or ex-g-flag ex-g-variant
)
2044 (error "Can't execute `yank' within `global'"))
2048 (forward-line (1- ex-count
)))
2050 (viper-enlarge-region (point) (mark t
))
2051 (if ex-flag
(error "`yank': %s" viper-SpuriousText
))
2053 (cond ((viper-valid-register ex-buffer
'(Letter))
2054 (viper-append-to-register
2055 (downcase ex-buffer
) (point) (mark t
)))
2056 ((viper-valid-register ex-buffer
)
2057 (copy-to-register ex-buffer
(point) (mark t
) nil
))
2058 (t (error viper-InvalidRegister ex-buffer
))))
2059 (copy-region-as-kill (point) (mark t
)))))
2061 ;; Execute shell command
2062 (defun ex-command ()
2064 (save-window-excursion
2065 (setq viper-ex-work-buf
(get-buffer-create viper-ex-work-buf-name
))
2066 (set-buffer viper-ex-work-buf
)
2067 (skip-chars-forward " \t")
2068 (setq command
(buffer-substring (point) (point-max)))
2070 (setq command
(ex-expand-filsyms command
(current-buffer)))
2071 (if (and (> (length command
) 0) (string= "!" (substring command
0 1)))
2072 (if viper-ex-last-shell-com
2074 (concat viper-ex-last-shell-com
(substring command
1)))
2075 (error "No previous shell command")))
2076 (setq viper-ex-last-shell-com command
)
2077 (if (null ex-addresses
)
2078 (shell-command command
)
2079 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2080 (if (null beg
) (setq beg end
))
2084 (viper-enlarge-region (point) (mark t
))
2085 (shell-command-on-region (point) (mark t
) command t
))
2088 ;; Print line number
2089 (defun ex-line-no ()
2093 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
2095 ;; Give information on the file visited by the current buffer
2096 (defun viper-info-on-file ()
2098 (let ((pos1 (viper-line-pos 'start
))
2099 (pos2 (viper-line-pos 'end
))
2101 (setq lines
(count-lines (point-min) (viper-line-pos 'end
))
2102 file
(if (buffer-file-name)
2103 (concat (viper-abbreviate-file-name (buffer-file-name)) ":")
2104 (concat (buffer-name) " [Not visiting any file]:"))
2105 info
(format "line=%d/%d pos=%d/%d col=%d %s"
2109 (count-lines (point-min) (point-max))
2110 (point) (1- (point-max))
2111 (1+ (current-column))
2112 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2113 (if (< (+ 1 (length info
) (length file
))
2114 (window-width (minibuffer-window)))
2115 (message (concat file
" " info
))
2116 (save-window-excursion
2117 (with-output-to-temp-buffer " *viper-info*"
2120 "\n\n\nPress any key to continue...\n\n")))
2122 (kill-buffer " *viper-info*")))
2125 ;; display all variables set through :set
2126 (defun ex-show-vars ()
2127 (with-output-to-temp-buffer " *viper-info*"
2128 (princ (if viper-auto-indent
2129 "autoindent (local)\n" "noautoindent (local)\n"))
2130 (princ (if (default-value 'viper-auto-indent
)
2131 "autoindent (global) \n" "noautoindent (global) \n"))
2132 (princ (if viper-case-fold-search
"ignorecase\n" "noignorecase\n"))
2133 (princ (if viper-re-search
"magic\n" "nomagic\n"))
2134 (princ (if buffer-read-only
"readonly\n" "noreadonly\n"))
2135 (princ (if blink-matching-paren
"showmatch\n" "noshowmatch\n"))
2136 (princ (if viper-search-wrap-around-t
"wrapscan\n" "nowrapscan\n"))
2137 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width
))
2138 (princ (format "tabstop (local) \t= %S\n" tab-width
))
2139 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width
)))
2140 (princ (format "wrapmargin (local) \t= %S\n"
2141 (- (window-width) fill-column
)))
2142 (princ (format "wrapmargin (global) \t= %S\n"
2143 (- (window-width) (default-value 'fill-column
))))
2144 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name
)
2145 explicit-shell-file-name
2153 ;;; viper-ex.el ends here