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 vip-use-register
)
30 (defvar vip-shift-width
)
31 (defvar vip-ex-history
)
32 (defvar vip-related-files-and-buffers-ring
)
33 (defvar vip-local-search-start-marker
)
34 (defvar viper-expert-level
)
35 (defvar vip-custom-file-name
)
36 (defvar vip-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 vip-ex-work-buf-name
" *ex-working-space*")
65 (defconst vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
66 (defconst vip-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 'vip-ex-nontrivial-find-file-unix
)
172 ((eq system-type
'emx
) 'vip-ex-nontrivial-find-file-ms
) ; OS/2
173 (vip-ms-style-os-p 'vip-ex-nontrivial-find-file-ms
) ; a Microsoft OS
174 (vip-vms-os-p 'vip-ex-nontrivial-find-file-unix
) ; VMS
175 (t 'vip-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 vip-ex-read-file-name to indicate that we may be reading
198 ;; multiple file names. Used for :edit and :next
199 (defvar vip-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 vip-ex-last-shell-com nil
)
216 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
217 (defvar vip-incomplete-ex-cmd nil
)
219 ;; Remembers the last ex-command prompt.
220 (defvar vip-last-ex-prompt
"")
225 ;; Check if ex-token is an initial segment of STR
226 (defun vip-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 vip-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") (vip-check-sub "abbreviate"))
243 ((looking-at "ar") (vip-check-sub "args"))
244 (t (vip-check-sub "append"))))
245 ((looking-at "h") (vip-check-sub "help"))
247 (cond ((looking-at "cd") (vip-check-sub "cd"))
248 ((looking-at "ch") (vip-check-sub "chdir"))
249 ((looking-at "co") (vip-check-sub "copy"))
250 (t (vip-check-sub "change"))))
251 ((looking-at "d") (vip-check-sub "delete"))
252 ((looking-at "b") (vip-check-sub "buffer"))
253 ((looking-at "B") (vip-check-sub "Buffer"))
255 (if (looking-at "ex") (vip-check-sub "ex")
256 (vip-check-sub "edit")))
257 ((looking-at "f") (vip-check-sub "file"))
258 ((looking-at "g") (vip-check-sub "global"))
259 ((looking-at "i") (vip-check-sub "insert"))
260 ((looking-at "j") (vip-check-sub "join"))
261 ((looking-at "l") (vip-check-sub "list"))
263 (cond ((looking-at "map") (vip-check-sub "map"))
264 ((looking-at "mar") (vip-check-sub "mark"))
265 (t (vip-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") (vip-check-sub "kmark"))
272 ((looking-at "n") (if (looking-at "nu")
273 (vip-check-sub "number")
274 (vip-check-sub "next")))
275 ((looking-at "N") (vip-check-sub "Next"))
276 ((looking-at "o") (vip-check-sub "open"))
278 (cond ((looking-at "pre") (vip-check-sub "preserve"))
279 ((looking-at "pu") (vip-check-sub "put"))
280 ((looking-at "pw") (vip-check-sub "pwd"))
281 (t (vip-check-sub "print"))))
282 ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
283 ((looking-at "R") (vip-check-sub "RelatedFile"))
284 ((looking-at "q") (vip-check-sub "quit"))
286 (cond ((looking-at "rec") (vip-check-sub "recover"))
287 ((looking-at "rew") (vip-check-sub "rewind"))
288 (t (vip-check-sub "read"))))
290 (cond ((looking-at "se") (vip-check-sub "set"))
291 ((looking-at "sh") (vip-check-sub "shell"))
292 ((looking-at "so") (vip-check-sub "source"))
293 ((looking-at "sr") (vip-check-sub "sr"))
294 ((looking-at "st") (vip-check-sub "stop"))
295 ((looking-at "sus") (vip-check-sub "suspend"))
296 ((looking-at "subm") (vip-check-sub "submitReport"))
297 (t (vip-check-sub "substitute"))))
299 (if (looking-at "ta") (vip-check-sub "tag")
300 (vip-check-sub "transfer")))
302 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
303 ((looking-at "unm") (vip-check-sub "unmap"))
304 (t (vip-check-sub "undo"))))
306 (cond ((looking-at "ve") (vip-check-sub "version"))
307 ((looking-at "vi") (vip-check-sub "visual"))
308 (t (vip-check-sub "vglobal"))))
310 (if (looking-at "wq") (vip-check-sub "wq")
311 (vip-check-sub "write")))
313 (if (looking-at "WW")
314 (vip-check-sub "WWrite")
315 (vip-check-sub "Write")))
316 ((looking-at "x") (vip-check-sub "xit"))
317 ((looking-at "y") (vip-check-sub "yank"))
318 ((looking-at "z") (vip-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 vip-get-ex-token ()
325 (save-window-excursion
326 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
327 (set-buffer vip-ex-work-buf
)
328 (skip-chars-forward " \t|")
329 (cond ((looking-at "#")
330 (setq ex-token-type
'command
)
331 (setq ex-token
(char-to-string (following-char)))
333 ((looking-at "[a-z]") (vip-get-ex-com-subr))
336 (setq ex-token-type
'dot
))
337 ((looking-at "[0-9]")
339 (re-search-forward "[0-9]*")
341 (cond ((eq ex-token-type
'plus
) 'add-number
)
342 ((eq ex-token-type
'minus
) 'sub-number
)
344 (setq ex-token
(string-to-int (buffer-substring (point) (mark t
)))))
347 (setq ex-token-type
'end
))
350 (setq ex-token-type
'whole
))
352 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
356 (setq ex-token-type
'plus
))
357 ((looking-at "+[0-9]")
359 (setq ex-token-type
'plus
))
361 (error vip-BadAddress
))))
363 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
367 (setq ex-token-type
'minus
))
368 ((looking-at "-[0-9]")
370 (setq ex-token-type
'minus
))
372 (error vip-BadAddress
))))
377 (while (and (not (eolp)) cont
)
378 ;;(re-search-forward "[^/]*/")
379 (re-search-forward "[^/]*\\(/\\|\n\\)")
380 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
383 (setq ex-token
(buffer-substring (point) (mark t
)))
384 (if (looking-at "/") (forward-char 1))
385 (setq ex-token-type
'search-forward
))
390 (while (and (not (eolp)) cont
)
391 ;;(re-search-forward "[^\\?]*\\?")
392 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
393 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
396 (if (not (looking-at "\n")) (forward-char 1))))
397 (setq ex-token-type
'search-backward
)
398 (setq ex-token
(buffer-substring (1- (point)) (mark t
))))
401 (setq ex-token-type
'comma
))
404 (setq ex-token-type
'semi-colon
))
405 ((looking-at "[!=><&~]")
406 (setq ex-token-type
'command
)
407 (setq ex-token
(char-to-string (following-char)))
410 (setq ex-token-type
'goto-mark
)
412 (cond ((looking-at "'") (setq ex-token nil
))
413 ((looking-at "[a-z]") (setq ex-token
(following-char)))
414 (t (error "Marks are ' and a-z")))
417 (setq ex-token-type
'end-mark
)
418 (setq ex-token
"goto"))
420 (error vip-BadExCommand
)))))
422 ;; Reads Ex command. Tries to determine if it has to exit because command
423 ;; is complete or invalid. If not, keeps reading command.
424 (defun ex-cmd-read-exit ()
426 (setq vip-incomplete-ex-cmd t
)
427 (let ((quit-regex1 (concat
430 "\\|" "[nN]ext[ \t]*"
443 "\\|" "[ktgjmsz][ \t]*$"
445 "\\|" "tr[ansfer \t]*"
448 "\\|" "^[ \t]*k?ma[^p]*"
457 "\\|" "![ \t]*[a-zA-Z].*"
461 (save-window-excursion ;; put cursor at the end of the Ex working buffer
462 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
463 (set-buffer vip-ex-work-buf
)
464 (goto-char (point-max)))
465 (cond ((vip-looking-back quit-regex1
) (exit-minibuffer))
466 ((vip-looking-back stay-regex
) (insert " "))
467 ((vip-looking-back quit-regex2
) (exit-minibuffer))
470 ;; complete Ex command
471 (defun ex-cmd-complete ()
473 (let (save-pos dist compl-list string-to-complete completion-result
)
476 (setq dist
(skip-chars-backward "[a-zA-Z!=>&~]")
480 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
482 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
483 ;; Preceding characters are not the ones allowed in an Ex command
484 ;; or we have typed past command name.
485 ;; Note: we didn't do parsing, so there may be surprises.
486 (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
487 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
488 (looking-at "[^ \t\n\C-m]"))
490 (with-output-to-temp-buffer "*Completions*"
491 (display-completion-list
492 (vip-alist-to-list ex-token-alist
))))
493 ;; Preceding chars may be part of a command name
494 (setq string-to-complete
(buffer-substring save-pos
(point)))
495 (setq completion-result
496 (try-completion string-to-complete ex-token-alist
))
498 (cond ((eq completion-result t
) ; exact match--do nothing
499 (vip-tmp-insert-at-eob " (Sole completion)"))
500 ((eq completion-result nil
)
501 (vip-tmp-insert-at-eob " (No match)"))
502 (t ;; partial completion
504 (delete-region (point) (point-max))
505 (insert completion-result
)
506 (let (case-fold-search)
508 (vip-filter-alist (concat "^" completion-result
)
510 (if (> (length compl-list
) 1)
511 (with-output-to-temp-buffer "*Completions*"
512 (display-completion-list
513 (vip-alist-to-list (reverse compl-list
)))))))
518 (defun vip-ex (&optional string
)
523 (let* ((map (copy-keymap minibuffer-local-map
))
527 prev-token-type com-str
)
529 (vip-add-keymap vip-ex-cmd-map map
)
531 (setq com-str
(or string
(vip-read-string-with-history
537 (save-window-excursion
539 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
540 (set-buffer vip-ex-work-buf
)
541 (delete-region (point-min) (point-max))
542 (insert com-str
"\n")
543 (goto-char (point-min)))
544 (setq ex-token-type nil
548 (cond ((memq ex-token-type
'(command end-mark
))
549 (if address
(setq ex-addresses
(cons address ex-addresses
)))
550 (cond ((string= ex-token
"global")
553 ((string= ex-token
"vglobal")
557 (vip-execute-ex-command)
558 (save-window-excursion
559 (setq vip-ex-work-buf
560 (get-buffer-create vip-ex-work-buf-name
))
561 (set-buffer vip-ex-work-buf
)
562 (skip-chars-forward " \t")
563 (cond ((looking-at "|")
567 (t (error "`%s': %s" ex-token vip-SpuriousText
)))
570 ((eq ex-token-type
'non-command
)
571 (error "`%s': %s" ex-token vip-BadExCommand
))
572 ((eq ex-token-type
'whole
)
576 (cons (point-max) ex-addresses
)
577 (cons (point-max) (cons (point-min) ex-addresses
)))))
578 ((eq ex-token-type
'comma
)
579 (if (eq prev-token-type
'whole
)
580 (setq address
(point-min)))
582 (cons (if (null address
) (point) address
) ex-addresses
)))
583 ((eq ex-token-type
'semi-colon
)
584 (if (eq prev-token-type
'whole
)
585 (setq address
(point-min)))
586 (if address
(setq dot address
))
588 (cons (if (null address
) (point) address
) ex-addresses
)))
589 (t (let ((ans (vip-get-ex-address-subr address dot
)))
590 (if ans
(setq address ans
)))))
591 (setq prev-token-type ex-token-type
))))
594 ;; Get a regular expression and set `ex-variant', if found
595 (defun vip-get-ex-pat ()
596 (save-window-excursion
597 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
598 (set-buffer vip-ex-work-buf
)
599 (skip-chars-forward " \t")
602 (setq ex-g-variant
(not ex-g-variant
)
603 ex-g-flag
(not ex-g-flag
))
605 (skip-chars-forward " \t")))
606 (let ((c (following-char)))
607 (if (string-match "[0-9A-Za-z]" (format "%c" c
))
609 "Global regexp must be inside matching non-alphanumeric chars"))
610 (if (looking-at "[^\\\\\n]")
615 (while (and (not (eolp)) cont
)
616 (if (not (re-search-forward (format "[^%c]*%c" c c
) nil t
))
617 (if (member ex-token
'("global" "vglobal"))
619 "Missing closing delimiter for global regexp")
620 (goto-char (point-max))))
621 (if (not (vip-looking-back
622 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c
)))
625 (if (= (mark t
) (point)) ""
626 (buffer-substring (1- (point)) (mark t
))))
628 ;; if the user doesn't specify the final pattern delimiter, we're
629 ;; at newline now. In this case, insert the initial delimiter
630 ;; specified in variable c
631 (if (looking-at "\n")
640 (defun vip-get-ex-command ()
641 (save-window-excursion
642 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
643 (set-buffer vip-ex-work-buf
)
644 (if (looking-at "/") (forward-char 1))
645 (skip-chars-forward " \t")
646 (cond ((looking-at "[a-z]")
647 (vip-get-ex-com-subr)
648 (if (eq ex-token-type
'non-command
)
649 (error "`%s': %s" ex-token vip-BadExCommand
)))
650 ((looking-at "[!=><&~]")
651 (setq ex-token
(char-to-string (following-char)))
653 (t (error vip-BadExCommand
)))))
655 ;; Get an Ex option g or c
656 (defun vip-get-ex-opt-gc (c)
657 (save-window-excursion
658 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
659 (set-buffer vip-ex-work-buf
)
660 (if (looking-at (format "%c" c
)) (forward-char 1))
661 (skip-chars-forward " \t")
662 (cond ((looking-at "g")
672 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
673 (defun vip-default-ex-addresses (&optional whole-flag
)
674 (cond ((null ex-addresses
)
677 (cons (point-max) (cons (point-min) nil
))
678 (cons (point) (cons (point) nil
)))))
679 ((null (cdr ex-addresses
))
681 (cons (car ex-addresses
) ex-addresses
)))))
683 ;; Get an ex-address as a marker and set ex-flag if a flag is found
684 (defun vip-get-ex-address ()
685 (let ((address (point-marker))
691 (cond ((eq ex-token-type
'command
)
692 (if (member ex-token
'("print" "list" "#"))
696 (error "Address expected in this Ex command")))
697 ((eq ex-token-type
'end-mark
)
699 ((eq ex-token-type
'whole
)
700 (error "Trailing address expected"))
701 ((eq ex-token-type
'comma
)
702 (error "`%s': %s" ex-token vip-SpuriousText
))
703 (t (let ((ans (vip-get-ex-address-subr address
(point-marker))))
704 (if ans
(setq address ans
))))))
707 ;; Returns an address as a point
708 (defun vip-get-ex-address-subr (old-address dot
)
710 (if (null old-address
) (setq old-address dot
))
711 (cond ((eq ex-token-type
'dot
)
713 ((eq ex-token-type
'add-number
)
715 (goto-char old-address
)
716 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
717 (setq address
(point-marker))))
718 ((eq ex-token-type
'sub-number
)
720 (goto-char old-address
)
721 (forward-line (- ex-token
))
722 (setq address
(point-marker))))
723 ((eq ex-token-type
'abs-number
)
725 (goto-char (point-min))
726 (if (= ex-token
0) (setq address
0)
727 (forward-line (1- ex-token
))
728 (setq address
(point-marker)))))
729 ((eq ex-token-type
'end
)
730 (setq address
(point-max-marker)))
731 ((eq ex-token-type
'plus
) t
) ; do nothing
732 ((eq ex-token-type
'minus
) t
) ; do nothing
733 ((eq ex-token-type
'search-forward
)
735 (ex-search-address t
)
736 (setq address
(point-marker))))
737 ((eq ex-token-type
'search-backward
)
739 (ex-search-address nil
)
740 (setq address
(point-marker))))
741 ((eq ex-token-type
'goto-mark
)
744 (exchange-point-and-mark)
745 (goto-char (vip-register-to-point
746 (1+ (- ex-token ?a
)) 'enforce-buffer
)))
747 (setq address
(point-marker)))))
751 ;; Search pattern and set address
752 (defun ex-search-address (forward)
753 (if (string= ex-token
"")
754 (if (null vip-s-string
)
755 (error vip-NoPrevSearch
)
756 (setq ex-token vip-s-string
))
757 (setq vip-s-string ex-token
))
761 (re-search-forward ex-token
))
763 (re-search-backward ex-token
)))
765 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
766 (defun vip-get-ex-buffer ()
770 (save-window-excursion
771 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
772 (set-buffer vip-ex-work-buf
)
773 (skip-chars-forward " \t")
774 (if (looking-at "[a-zA-Z]")
776 (setq ex-buffer
(following-char))
778 (skip-chars-forward " \t")))
779 (if (looking-at "[0-9]")
782 (re-search-forward "[0-9][0-9]*")
783 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
784 (skip-chars-forward " \t")))
785 (if (looking-at "[pl#]")
789 (if (not (looking-at "[\n|]"))
790 (error "`%s': %s" ex-token vip-SpuriousText
))))
792 (defun vip-get-ex-count ()
796 (save-window-excursion
797 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
798 (set-buffer vip-ex-work-buf
)
799 (skip-chars-forward " \t")
804 (skip-chars-forward " \t")
805 (if (looking-at "[0-9]")
808 (re-search-forward "[0-9][0-9]*")
809 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
810 (skip-chars-forward " \t")))
811 (if (looking-at "[pl#]")
815 (if (not (looking-at "[\n|]"))
817 (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand
))))
819 ;; Expand \% and \# in ex command
820 (defun ex-expand-filsyms (cmd buf
)
824 (setq cf buffer-file-name
)
825 (setq pf
(ex-next nil t
))) ; this finds alternative file name
826 (if (and (null cf
) (string-match "[^\\]%\\|\\`%" cmd
))
827 (error "No current file to substitute for `%%'"))
828 (if (and (null pf
) (string-match "[^\\]#\\|\\`#" cmd
))
829 (error "No alternate file to substitute for `#'"))
831 (set-buffer (get-buffer-create vip-ex-tmp-buf-name
))
834 (goto-char (point-min))
835 (while (re-search-forward "%\\|#" nil t
)
836 (let ((data (match-data))
837 (char (buffer-substring (match-beginning 0) (match-end 0))))
838 (if (vip-looking-back (concat "\\\\" char
))
840 (store-match-data data
)
841 (if (string= char
"%")
843 (replace-match pf
)))))
845 (setq ret
(buffer-substring (point-min) (point)))
849 ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
850 (defun vip-get-ex-file ()
858 (save-window-excursion
859 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
860 (set-buffer vip-ex-work-buf
)
861 (skip-chars-forward " \t")
863 (if (and (not (vip-looking-back "[ \t]"))
864 ;; read doesn't have a corresponding :r! form, so ! is
865 ;; immediately interpreted as a shell command.
866 (not (string= ex-token
"read")))
870 (skip-chars-forward " \t"))
873 (skip-chars-forward " \t")))
874 (if (looking-at ">>")
879 (skip-chars-forward " \t")))
884 (re-search-forward "[ \t\n]")
886 (setq ex-offset
(buffer-substring (point) (mark t
)))
888 (skip-chars-forward " \t")))
889 ;; this takes care of :r, :w, etc., when they get file names
890 ;; from the history list
891 (if (member ex-token
'("read" "write" "edit" "visual" "next"))
893 (setq ex-file
(buffer-substring (point) (1- (point-max))))
895 ;; For :e, match multiple non-white strings separated
896 ;; by white. For others, find the first non-white string
898 (if (string= ex-token
"edit")
899 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
903 ;; if file name comes from history, don't leave
904 ;; minibuffer when the user types space
905 (setq vip-incomplete-ex-cmd nil
)
906 ;; this must be the last clause in this progn
907 (substring ex-file
(match-beginning 0) (match-end 0))
910 ;; this leaves only the command name in the work area
911 ;; file names are gone
912 (delete-region (point) (1- (point-max)))
914 (goto-char (point-max))
915 (skip-chars-backward " \t\n")
916 (setq prompt
(buffer-substring (point-min) (point)))
919 (setq vip-last-ex-prompt prompt
)
921 ;; If we just finished reading command, redisplay prompt
922 (if vip-incomplete-ex-cmd
923 (setq ex-file
(vip-ex-read-file-name (format ":%s " prompt
)))
924 ;; file was typed in-line
925 (setq ex-file
(or ex-file
"")))
929 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
930 ;; file names, arranges to re-enter the minibuffer.
931 (defun vip-complete-filename-or-exit ()
933 (setq vip-keep-reading-filename t
)
934 ;; don't exit if directory---ex-commands don't
935 (cond ((ex-cmd-accepts-multiple-files-p ex-token
) (exit-minibuffer))
936 ;; apparently the argument to an Ex command is
937 ;; supposed to be a shell command
938 ((vip-looking-back "^[ \t]*!.*")
942 (setq ex-cmdfile nil
)
943 (minibuffer-complete-word))))
945 (defun vip-handle-! ()
948 (buffer-string) (vip-abbreviate-file-name default-directory
))
949 (member ex-token
'("read" "write")))
953 (defun ex-cmd-accepts-multiple-files-p (token)
954 (member token
'("edit" "next" "Next")))
956 ;; If user doesn't enter anything, then "" is returned, i.e., the
957 ;; prompt-directory is not returned.
958 (defun vip-ex-read-file-name (prompt)
960 (minibuffer-local-completion-map
961 (copy-keymap minibuffer-local-completion-map
))
964 (vip-add-keymap ex-read-filename-map
966 minibuffer-local-completion-map
969 (setq cont
(setq vip-keep-reading-filename t
))
971 (setq vip-keep-reading-filename nil
972 val
(read-file-name (concat prompt str
) nil default-directory
))
973 (if (string-match " " val
)
974 (setq val
(concat "\\\"" val
"\\\"")))
975 (setq str
(concat str
(if (equal val
"") "" " ")
976 val
(if (equal val
"") "" " ")))
978 ;; Only edit, next, and Next commands accept multiple files.
979 ;; vip-keep-reading-filename is set in the anonymous function that is
980 ;; bound to " " in ex-read-filename-map.
981 (setq cont
(and vip-keep-reading-filename
982 (ex-cmd-accepts-multiple-files-p ex-token
)))
985 (setq beg
(string-match "[^ \t]" str
) ; delete leading blanks
986 end
(string-match "[ \t]*$" str
)) ; delete trailing blanks
987 (if (member ex-token
'("read" "write"))
988 (if (string-match "[\t ]*!" str
)
989 ;; this is actually a shell command
993 (setq vip-last-ex-prompt
(concat vip-last-ex-prompt
" !")))))
994 (substring str
(or beg
0) end
)))
996 ;; Execute ex command using the value of addresses
997 (defun vip-execute-ex-command ()
998 (vip-deactivate-mark)
999 (cond ((string= ex-token
"args") (ex-args))
1000 ((string= ex-token
"copy") (ex-copy nil
))
1001 ((string= ex-token
"cd") (ex-cd))
1002 ((string= ex-token
"chdir") (ex-cd))
1003 ((string= ex-token
"delete") (ex-delete))
1004 ((string= ex-token
"edit") (ex-edit))
1005 ((string= ex-token
"file") (vip-info-on-file))
1006 ((string= ex-token
"goto") (ex-goto))
1007 ((string= ex-token
"help") (ex-help))
1008 ((string= ex-token
"join") (ex-line "join"))
1009 ((string= ex-token
"kmark") (ex-mark))
1010 ((string= ex-token
"mark") (ex-mark))
1011 ((string= ex-token
"map") (ex-map))
1012 ((string= ex-token
"move") (ex-copy t
))
1013 ((string= ex-token
"next") (ex-next ex-cycle-other-window
))
1014 ((string= ex-token
"Next") (ex-next (not ex-cycle-other-window
)))
1015 ((string= ex-token
"RelatedFile") (ex-next-related-buffer 1))
1016 ((string= ex-token
"put") (ex-put))
1017 ((string= ex-token
"pwd") (ex-pwd))
1018 ((string= ex-token
"preserve") (ex-preserve))
1019 ((string= ex-token
"PreviousRelatedFile") (ex-next-related-buffer -
1))
1020 ((string= ex-token
"quit") (ex-quit))
1021 ((string= ex-token
"read") (ex-read))
1022 ((string= ex-token
"recover") (ex-recover))
1023 ((string= ex-token
"rewind") (ex-rewind))
1024 ((string= ex-token
"submitReport") (vip-submit-report))
1025 ((string= ex-token
"set") (ex-set))
1026 ((string= ex-token
"shell") (ex-shell))
1027 ((string= ex-token
"source") (ex-source))
1028 ((string= ex-token
"sr") (ex-substitute t t
))
1029 ((string= ex-token
"substitute") (ex-substitute))
1030 ((string= ex-token
"suspend") (suspend-emacs))
1031 ((string= ex-token
"stop") (suspend-emacs))
1032 ((string= ex-token
"transfer") (ex-copy nil
))
1033 ((string= ex-token
"buffer") (if ex-cycle-other-window
1034 (vip-switch-to-buffer-other-window)
1035 (vip-switch-to-buffer)))
1036 ((string= ex-token
"Buffer") (if ex-cycle-other-window
1037 (vip-switch-to-buffer)
1038 (vip-switch-to-buffer-other-window)))
1039 ((string= ex-token
"tag") (ex-tag))
1040 ((string= ex-token
"undo") (vip-undo))
1041 ((string= ex-token
"unmap") (ex-unmap))
1042 ((string= ex-token
"version") (vip-version))
1043 ((string= ex-token
"visual") (ex-edit))
1044 ((string= ex-token
"write") (ex-write nil
))
1045 ((string= ex-token
"Write") (save-some-buffers))
1046 ((string= ex-token
"wq") (ex-write t
))
1047 ((string= ex-token
"WWrite") (save-some-buffers t
)) ; don't ask
1048 ((string= ex-token
"xit") (ex-write t
))
1049 ((string= ex-token
"yank") (ex-yank))
1050 ((string= ex-token
"!") (ex-command))
1051 ((string= ex-token
"=") (ex-line-no))
1052 ((string= ex-token
">") (ex-line "right"))
1053 ((string= ex-token
"<") (ex-line "left"))
1054 ((string= ex-token
"&") (ex-substitute t
))
1055 ((string= ex-token
"~") (ex-substitute t t
))
1056 ((or (string= ex-token
"append")
1057 (string= ex-token
"change")
1058 (string= ex-token
"insert")
1059 (string= ex-token
"open"))
1060 (error "`%s': Obsolete command, not supported by Viper" ex-token
))
1061 ((or (string= ex-token
"abbreviate")
1062 (string= ex-token
"unabbreviate"))
1064 "`%s': Vi abbrevs are obsolete. Use the more powerful Emacs abbrevs"
1066 ((or (string= ex-token
"list")
1067 (string= ex-token
"print")
1068 (string= ex-token
"z")
1069 (string= ex-token
"#"))
1070 (error "`%s': Command not implemented in Viper" ex-token
))
1071 (t (error "`%s': %s" ex-token vip-BadExCommand
))))
1073 (defun vip-undisplayed-files ()
1077 (if (null (get-buffer-window b
))
1078 (let ((f (buffer-file-name b
)))
1080 (if ex-cycle-through-non-files
1081 (let ((s (buffer-name b
)))
1082 (if (string= " " (substring s
0 1))
1091 (let ((l (vip-undisplayed-files))
1094 (while (not (null l
))
1096 (setq args
(format "%s %d) %s\n" args file-count
(car l
))
1097 file-count
(1+ file-count
)))
1099 (if (string= args
"")
1100 (message "All files are already displayed")
1102 (save-window-excursion
1103 (with-output-to-temp-buffer " *vip-info*"
1104 (princ "\n\nThese files are not displayed in any window.\n")
1105 (princ "\n=============\n")
1107 (princ "\n=============\n")
1108 (princ "\nThe numbers can be given as counts to :next. ")
1109 (princ "\n\nPress any key to continue...\n\n"))
1110 (vip-read-event))))))
1112 ;; Ex cd command. Default directory of this buffer changes
1115 (if (string= ex-file
"")
1117 (setq default-directory
(file-name-as-directory (expand-file-name ex-file
))))
1119 ;; Ex copy and move command. DEL-FLAG means delete
1120 (defun ex-copy (del-flag)
1121 (vip-default-ex-addresses)
1122 (let ((address (vip-get-ex-address))
1123 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1127 (vip-enlarge-region (mark t
) (point))
1129 (kill-region (point) (mark t
))
1130 (copy-region-as-kill (point) (mark t
)))
1133 (with-output-to-temp-buffer "*copy text*"
1135 (if (or del-flag ex-g-flag ex-g-variant
)
1137 (buffer-substring (point) (mark t
)))))
1140 (read-string "[Hit return to continue] ")
1141 (save-excursion (kill-buffer "*copy text*")))
1142 (quit (save-excursion (kill-buffer "*copy text*"))
1143 (signal 'quit nil
))))))
1145 (goto-char (point-min))
1148 (insert (current-kill 0))))
1150 ;; Ex delete command
1152 (vip-default-ex-addresses)
1154 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1155 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1157 (vip-enlarge-region beg end
)
1158 (exchange-point-and-mark)
1162 (forward-line (1- ex-count
)))
1164 (vip-enlarge-region (point) (mark t
))
1166 ;; show text to be deleted and ask for confirmation
1168 (with-output-to-temp-buffer " *delete text*"
1169 (princ (buffer-substring (point) (mark t
))))
1171 (read-string "[Hit return to continue] ")
1173 (save-excursion (kill-buffer " *delete text*"))
1175 (save-excursion (kill-buffer " *delete text*")))
1177 (cond ((vip-valid-register ex-buffer
'(Letter))
1178 (vip-append-to-register
1179 (downcase ex-buffer
) (point) (mark t
)))
1180 ((vip-valid-register ex-buffer
)
1181 (copy-to-register ex-buffer
(point) (mark t
) nil
))
1182 (t (error vip-InvalidRegister ex-buffer
))))
1183 (kill-region (point) (mark t
))))))
1188 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1189 ;; asked if current buffer should really be discarded.
1190 ;; This command can take multiple file names. It replaces the current buffer
1191 ;; with the first file in its argument list
1192 (defun ex-edit (&optional file
)
1195 (cond ((and (string= ex-file
"") buffer-file-name
)
1196 (setq ex-file
(vip-abbreviate-file-name (buffer-file-name))))
1197 ((string= ex-file
"")
1198 (error vip-NoFileSpecified
)))
1201 (if buffer-file-name
1202 (cond ((buffer-modified-p)
1204 (format "Buffer %s is modified. Discard changes? "
1207 ((not (verify-visited-file-modtime (current-buffer)))
1209 (format "File %s changed on disk. Reread from disk? "
1212 (t (setq do-edit nil
))))
1215 (if (yes-or-no-p msg
)
1217 (set-buffer-modified-p nil
)
1218 (kill-buffer (current-buffer)))
1219 (message "Buffer %s was left intact" (buffer-name))))
1222 (if (null (setq file
(get-file-buffer ex-file
)))
1224 (ex-find-file ex-file
)
1225 (or (eq major-mode
'dired-mode
)
1226 (vip-change-state-to-vi))
1227 (goto-char (point-min)))
1228 (switch-to-buffer file
))
1231 (save-window-excursion
1232 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1233 (set-buffer vip-ex-work-buf
)
1234 (delete-region (point-min) (point-max))
1235 (insert ex-offset
"\n")
1236 (goto-char (point-min)))
1237 (goto-char (vip-get-ex-address))
1238 (beginning-of-line)))
1239 (ex-fixup-history vip-last-ex-prompt ex-file
))
1241 ;; Find-file FILESPEC if it appears to specify a single file.
1242 ;; Otherwise, assume that FILES{EC is a wildcard.
1243 ;; In this case, split it into substrings separated by newlines.
1244 ;; Each line is assumed to be a file name. find-file's each file thus obtained.
1245 (defun ex-find-file (filespec)
1246 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1247 (cond ((file-exists-p filespec
) (find-file filespec
))
1248 ((string-match nonstandard-filename-chars filespec
)
1249 (funcall ex-nontrivial-find-file-function filespec
))
1250 (t (find-file filespec
)))
1254 ;; Ex global command
1255 ;; This is executed in response to:
1256 ;; :global "pattern" ex-command
1257 ;; :vglobal "pattern" ex-command
1258 ;; :global executes ex-command on all lines matching <pattern>
1259 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1261 ;; With VARIANT nil, this functions executes :global
1262 ;; With VARIANT t, executes :vglobal
1263 (defun ex-global (variant)
1264 (let ((gcommand ex-token
))
1265 (if (or ex-g-flag ex-g-variant
)
1266 (error "`%s' within `global' is not allowed" gcommand
)
1274 (error "`%s': Missing regular expression" gcommand
)))
1276 (if (string= ex-token
"")
1277 (if (null vip-s-string
)
1278 (error vip-NoPrevSearch
)
1279 (setq ex-g-pat vip-s-string
))
1280 (setq ex-g-pat ex-token
1281 vip-s-string ex-token
))
1282 (if (null ex-addresses
)
1283 (setq ex-addresses
(list (point-max) (point-min)))
1284 (vip-default-ex-addresses))
1287 (end (car ex-addresses
))
1288 (beg (car (cdr ex-addresses
)))
1290 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1292 (vip-enlarge-region beg end
)
1293 (exchange-point-and-mark)
1294 (let ((cont t
) (limit (point-marker)))
1295 (exchange-point-and-mark)
1296 ;; skip the last line if empty
1298 (if (eobp) (vip-backward-char-carefully))
1299 (while (and cont
(not (bobp)) (>= (point) limit
))
1303 (let ((found (re-search-backward ex-g-pat
(mark t
) t
)))
1304 (if (or (and ex-g-flag found
)
1305 (and ex-g-variant
(not found
)))
1308 (setq mark-count
(1+ mark-count
))
1309 (setq marks
(cons (point-marker) marks
)))))
1311 (if (bobp) (setq cont nil
)
1314 (save-window-excursion
1315 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1316 (set-buffer vip-ex-work-buf
)
1317 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
1319 (goto-char (car marks
))
1321 (setq mark-count
(1- mark-count
))
1322 (setq marks
(cdr marks
)))))
1326 (if (null ex-addresses
)
1327 (setq ex-addresses
(cons (point) nil
)))
1328 (push-mark (point) t
)
1329 (goto-char (car ex-addresses
))
1330 (beginning-of-line))
1332 ;; Ex line commands. COM is join, shift-right or shift-left
1333 (defun ex-line (com)
1334 (vip-default-ex-addresses)
1336 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
1337 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1339 (vip-enlarge-region beg end
)
1340 (exchange-point-and-mark)
1344 (forward-line ex-count
)))
1346 ;; show text to be joined and ask for confirmation
1348 (with-output-to-temp-buffer " *text*"
1349 (princ (buffer-substring (point) (mark t
))))
1352 (read-string "[Hit return to continue] ")
1353 (ex-line-subr com
(point) (mark t
)))
1355 (save-excursion (kill-buffer " *text*")))
1356 (ex-line-subr com
(point) (mark t
)))
1357 (setq point
(point)))
1358 (goto-char (1- point
))
1359 (beginning-of-line)))
1361 (defun ex-line-subr (com beg end
)
1362 (cond ((string= com
"join")
1363 (goto-char (min beg end
))
1364 (while (and (not (eobp)) (< (point) (max beg end
)))
1366 (if (and (<= (point) (max beg end
)) (not (eobp)))
1369 (delete-region (point) (1- (point)))
1370 (if (not ex-variant
) (fixup-whitespace))))))
1371 ((or (string= com
"right") (string= com
"left"))
1373 (min beg end
) (max beg end
)
1374 (if (string= com
"right") vip-shift-width
(- vip-shift-width
)))
1375 (goto-char (max beg end
))
1377 (vip-forward-char-carefully))))
1383 (if (null ex-addresses
)
1385 (cons (point) nil
)))
1386 (save-window-excursion
1387 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1388 (set-buffer vip-ex-work-buf
)
1389 (skip-chars-forward " \t")
1390 (if (looking-at "[a-z]")
1392 (setq char
(following-char))
1394 (skip-chars-forward " \t")
1395 (if (not (looking-at "[\n|]"))
1396 (error "`%s': %s" ex-token vip-SpuriousText
)))
1397 (error "`%s' requires a following letter" ex-token
)))
1399 (goto-char (car ex-addresses
))
1400 (point-to-register (1+ (- char ?a
))))))
1404 ;; Alternate file is the file next to the first one in the buffer ring
1405 (defun ex-next (cycle-other-window &optional find-alt-file
)
1408 (if (not find-alt-file
)
1411 (if (or (char-or-string-p ex-offset
)
1412 (and (not (string= "" ex-file
))
1413 (not (string-match "^[0-9]+$" ex-file
))))
1416 (throw 'ex-edit nil
))
1417 (setq count
(string-to-int ex-file
))
1418 (if (= count
0) (setq count
1))
1419 (if (< count
0) (error "Usage: `next <count>' (count >= 0)"))))
1421 (setq l
(vip-undisplayed-files))
1423 (while (and (not (null l
)) (null (car l
)))
1425 (setq count
(1- count
))
1428 (if find-alt-file
(car l
)
1430 (if (and (car l
) (get-file-buffer (car l
)))
1431 (let* ((w (if cycle-other-window
1432 (get-lru-window) (selected-window)))
1433 (b (window-buffer w
)))
1434 (set-window-buffer w
(get-file-buffer (car l
)))
1436 ;; this puts "next <count>" in the ex-command history
1437 (ex-fixup-history vip-last-ex-prompt ex-file
))
1438 (error "Not that many undisplayed files")))))))
1441 (defun ex-next-related-buffer (direction &optional no-recursion
)
1443 (vip-ring-rotate1 vip-related-files-and-buffers-ring direction
)
1445 (let ((file-or-buffer-name
1446 (vip-current-ring-item vip-related-files-and-buffers-ring
))
1447 (old-ring vip-related-files-and-buffers-ring
)
1448 (old-win (selected-window))
1451 (or (and (ring-p vip-related-files-and-buffers-ring
)
1452 (> (ring-length vip-related-files-and-buffers-ring
) 0))
1453 (error "This buffer has no related files or buffers"))
1455 (or (stringp file-or-buffer-name
)
1457 "File and buffer names must be strings, %S" file-or-buffer-name
))
1459 (setq buf
(cond ((get-buffer file-or-buffer-name
))
1460 ((file-exists-p file-or-buffer-name
)
1461 (find-file-noselect file-or-buffer-name
))
1464 (if (not (vip-buffer-live-p buf
))
1465 (error "Didn't find buffer %S or file %S"
1467 (vip-abbreviate-file-name
1468 (expand-file-name file-or-buffer-name
))))
1470 (if (equal buf
(current-buffer))
1475 (ex-next-related-buffer direction
'norecursion
))))
1480 (if (setq wind
(vip-get-visible-buffer-window buf
))
1482 (setq wind
(get-lru-window (if vip-xemacs-p nil
'visible
)))
1483 (set-window-buffer wind buf
))
1485 (if (vip-window-display-p)
1487 (raise-frame (window-frame wind
))
1488 (if (equal (window-frame wind
) (window-frame old-win
))
1489 (save-window-excursion (select-window wind
) (sit-for 1))
1490 (select-window wind
)))
1491 (save-window-excursion (select-window wind
) (sit-for 1)))
1495 (setq vip-related-files-and-buffers-ring old-ring
))
1497 (setq vip-local-search-start-marker
(point-marker))
1502 (defun ex-preserve ()
1503 (message "Autosaving all buffers that need to be saved...")
1508 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
1510 (setq vip-use-register ex-buffer
)
1512 (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
1514 ;; Ex print working directory
1516 (message default-directory
))
1520 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1522 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1523 (set-buffer vip-ex-work-buf
)
1524 (if (looking-at "!") (forward-char 1)))
1525 (if (< viper-expert-level
3)
1526 (save-buffers-kill-emacs)
1527 (kill-buffer (current-buffer))))
1533 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
1536 (vip-add-newline-at-eob-if-necessary)
1537 (if (not (or (bobp) (eobp))) (forward-line 1))
1538 (if (and (not ex-variant
) (string= ex-file
""))
1540 (if (null buffer-file-name
)
1541 (error vip-NoFileSpecified
))
1542 (setq ex-file buffer-file-name
)))
1545 (setq command
(ex-expand-filsyms ex-file
(current-buffer)))
1546 (shell-command command t
))
1547 (insert-file-contents ex-file
)))
1548 (ex-fixup-history vip-last-ex-prompt ex-file
))
1550 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1551 (defun ex-fixup-history (&rest args
)
1552 (setq vip-ex-history
1553 (cons (mapconcat 'identity args
" ") (cdr vip-ex-history
))))
1556 ;; Ex recover from emacs \#file\#
1557 (defun ex-recover ()
1559 (if (or ex-append ex-offset
)
1560 (error "`recover': %s" vip-SpuriousText
))
1561 (if (string= ex-file
"")
1563 (if (null buffer-file-name
)
1564 (error "This buffer isn't visiting any file"))
1565 (setq ex-file buffer-file-name
))
1566 (setq ex-file
(expand-file-name ex-file
)))
1567 (if (and (not (string= ex-file
(buffer-file-name)))
1570 (error "No write since last change \(:rec! overrides\)"))
1571 (recover-file ex-file
))
1573 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1576 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1579 ;; read variable name for ex-set
1580 (defun ex-set-read-variable ()
1581 (let ((minibuffer-local-completion-map
1582 (copy-keymap minibuffer-local-completion-map
))
1583 (cursor-in-echo-area t
)
1586 minibuffer-local-completion-map
" " 'minibuffer-complete-and-exit
)
1587 (define-key minibuffer-local-completion-map
"=" 'exit-minibuffer
)
1588 (if (vip-set-unread-command-events
1589 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil
"\C-m"))
1592 (vip-set-unread-command-events ?\C-m
)))
1593 (message ":set <Variable> [= <Value>]")
1594 (or batch
(sit-for 2))
1596 (while (string-match "^[ \\t\\n]*$"
1598 (completing-read ":set " ex-variable-alist
)))
1599 (message ":set <Variable> [= <Value>]")
1600 ;; if there are unread events, don't wait
1601 (or (vip-set-unread-command-events "") (sit-for 2))
1607 (let ((var (ex-set-read-variable))
1611 (auto-cmd-label "; don't touch or else...")
1612 (delete-turn-on-auto-fill-pattern
1613 "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1614 actual-lisp-cmd lisp-cmd-del-pattern
1617 (cond ((string= var
"all")
1618 (setq ask-if-save nil
1620 ((member var
'("ai" "autoindent"))
1621 (setq var
"vip-auto-indent"
1625 ((member var
'("ai-g" "autoindent-global"))
1626 (kill-local-variable 'vip-auto-indent
)
1627 (setq var
"vip-auto-indent"
1628 set-cmd
"setq-default"
1630 ((member var
'("noai" "noautoindent"))
1631 (setq var
"vip-auto-indent"
1634 ((member var
'("noai-g" "noautoindent-global"))
1635 (kill-local-variable 'vip-auto-indent
)
1636 (setq var
"vip-auto-indent"
1637 set-cmd
"setq-default"
1639 ((member var
'("ic" "ignorecase"))
1640 (setq var
"vip-case-fold-search"
1642 ((member var
'("noic" "noignorecase"))
1643 (setq var
"vip-case-fold-search"
1645 ((member var
'("ma" "magic"))
1646 (setq var
"vip-re-search"
1648 ((member var
'("noma" "nomagic"))
1649 (setq var
"vip-re-search"
1651 ((member var
'("ro" "readonly"))
1652 (setq var
"buffer-read-only"
1654 ((member var
'("noro" "noreadonly"))
1655 (setq var
"buffer-read-only"
1657 ((member var
'("sm" "showmatch"))
1658 (setq var
"blink-matching-paren"
1660 ((member var
'("nosm" "noshowmatch"))
1661 (setq var
"blink-matching-paren"
1663 ((member var
'("ws" "wrapscan"))
1664 (setq var
"vip-search-wrap-around-t"
1666 ((member var
'("nows" "nowrapscan"))
1667 (setq var
"vip-search-wrap-around-t"
1669 (if (and set-cmd
(eq val
0)) ; value must be set by the user
1670 (let ((cursor-in-echo-area t
))
1671 (message ":set %s = <Value>" var
)
1672 ;; if there are unread events, don't wait
1673 (or (vip-set-unread-command-events "") (sit-for 2))
1674 (setq val
(read-string (format ":set %s = " var
)))
1675 (ex-fixup-history "set" orig-var val
)
1677 ;; check numerical values
1681 "ts-g" "tabstop-global"
1684 (or (numberp (setq val2
(car (read-from-string val
))))
1685 (error "%s: Invalid value, numberp, %S" var val
))
1687 (error "%s: Invalid value, numberp, %S" var val
))))
1690 ((member var
'("sw" "shiftwidth"))
1691 (setq var
"vip-shift-width"))
1692 ((member var
'("ts" "tabstop"))
1693 ;; make it take effect in curr buff and new bufs
1694 (setq var
"tab-width"
1697 ((member var
'("ts-g" "tabstop-global"))
1698 (kill-local-variable 'tab-width
)
1699 (setq var
"tab-width"
1700 set-cmd
"setq-default"))
1701 ((member var
'("wm" "wrapmargin"))
1702 ;; make it take effect in curr buff and new bufs
1703 (kill-local-variable 'fill-column
)
1704 (setq var
"fill-column"
1705 val
(format "(- (window-width) %s)" val
)
1706 set-cmd
"setq-default"))
1707 ((member var
'("sh" "shell"))
1708 (setq var
"explicit-shell-file-name"
1709 val
(format "\"%s\"" val
)))))
1710 (ex-fixup-history "set" orig-var
))
1713 (setq actual-lisp-cmd
1714 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label
)
1715 lisp-cmd-del-pattern
1716 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1717 set-cmd var auto-cmd-label
)))
1719 (if (and ask-if-save
1720 (y-or-n-p (format "Do you want to save this setting in %s "
1721 vip-custom-file-name
)))
1723 (vip-save-string-in-file
1724 actual-lisp-cmd vip-custom-file-name
1726 lisp-cmd-del-pattern
)
1727 (if (string= var
"fill-column")
1729 (vip-save-string-in-file
1731 "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
1733 vip-custom-file-name
1734 delete-turn-on-auto-fill-pattern
)
1735 (vip-save-string-in-file
1736 nil vip-custom-file-name delete-turn-on-auto-fill-pattern
)
1737 (vip-save-string-in-file
1738 nil vip-custom-file-name
1740 lisp-cmd-del-pattern
)
1747 (if (string-match "^[ \t]*$" val
)
1751 (eval (car (read-from-string actual-lisp-cmd
))))
1752 (if (string= var
"fill-column")
1755 (auto-fill-mode -
1)))
1756 (if (string= var
"all") (ex-show-vars))
1759 ;; In inline args, skip regex-forw and (optionally) chars-back.
1760 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1762 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str
)
1764 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1765 (set-buffer vip-ex-work-buf
)
1766 (goto-char (point-min))
1767 (re-search-forward regex-forw nil t
)
1770 (goto-char (point-max))
1772 (skip-chars-backward chars-back
)
1773 (skip-chars-backward " \t\n\C-m"))
1775 ;; replace SPC with `=' to suppress the special meaning SPC has
1779 (while (re-search-forward " +" nil t
)
1780 (replace-match replace-str nil t
)
1781 (vip-forward-char-carefully)))
1783 (buffer-substring beg end
))))
1790 ;; Viper help. Invokes Info
1794 (pop-to-buffer (get-buffer-create "*info*"))
1795 (info (if vip-xemacs-p
"viper.info" "viper"))
1796 (message "Type `i' to search for a specific topic"))
1798 (with-output-to-temp-buffer " *vip-info*"
1800 The Info file for Viper does not seem to be installed.
1802 This file is part of the standard distribution of %sEmacs.
1803 Please contact your system administrator. "
1804 (if vip-xemacs-p
"X" "")
1807 ;; Ex source command. Loads the file specified as argument or `~/.vip'
1810 (if (string= ex-file
"")
1811 (load vip-custom-file-name
)
1814 ;; Ex substitute command
1815 ;; If REPEAT use previous regexp which is ex-reg-exp or vip-s-string
1816 (defun ex-substitute (&optional repeat r-flag
)
1820 (case-fold-search vip-case-fold-search
)
1822 (if repeat
(setq ex-token nil
) (setq delim
(vip-get-ex-pat)))
1825 (setq pat
(if r-flag vip-s-string ex-reg-exp
))
1827 (error "No previous pattern to use in substitution"))
1829 delim
(string-to-char pat
)))
1830 (setq pat
(if (string= ex-token
"") vip-s-string ex-token
))
1831 (setq vip-s-string pat
1833 (setq delim
(vip-get-ex-pat))
1839 (while (vip-get-ex-opt-gc delim
)
1840 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
1844 (if ex-addresses
(goto-char (car ex-addresses
)))
1846 (forward-line (1- ex-count
))
1847 (setq ex-addresses
(cons (point) (cons (mark t
) nil
))))
1848 (if (null ex-addresses
)
1849 (setq ex-addresses
(cons (point) (cons (point) nil
)))
1850 (if (null (cdr ex-addresses
))
1851 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
1853 (let ((beg (car ex-addresses
))
1854 (end (car (cdr ex-addresses
)))
1857 (vip-enlarge-region beg end
)
1858 (let ((limit (save-excursion
1859 (goto-char (max (point) (mark t
)))
1861 (goto-char (min (point) (mark t
)))
1862 (while (< (point) limit
)
1864 (setq eol-mark
(point-marker))
1868 (while (and (not (eolp))
1869 (re-search-forward pat eol-mark t
))
1870 (if (or (not opt-c
) (y-or-n-p "Replace? "))
1872 (setq matched-pos
(point))
1873 (if (not (stringp repl
))
1874 (error "Can't perform Ex substitution: No previous replacement pattern"))
1875 (replace-match repl t
))))
1877 (vip-forward-char-carefully))
1880 "Can't repeat Ex substitution: No previous regular expression"))
1881 (if (and (re-search-forward pat eol-mark t
)
1882 (or (not opt-c
) (y-or-n-p "Replace? ")))
1884 (setq matched-pos
(point))
1885 (if (not (stringp repl
))
1886 (error "Can't perform Ex substitution: No previous replacement pattern"))
1887 (replace-match repl t
)))
1889 (vip-forward-char-carefully))))))
1890 (if matched-pos
(goto-char matched-pos
))
1892 (if opt-c
(message "done"))))
1897 (save-window-excursion
1898 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1899 (set-buffer vip-ex-work-buf
)
1900 (skip-chars-forward " \t")
1902 (skip-chars-forward "^ |\t\n")
1903 (setq tag
(buffer-substring (mark t
) (point))))
1904 (if (not (string= tag
"")) (setq ex-tag tag
))
1905 (vip-change-state-to-emacs)
1906 (condition-case conds
1908 (if (string= tag
"")
1910 (find-tag-other-window ex-tag
))
1911 (vip-change-state-to-vi))
1913 (vip-change-state-to-vi)
1914 (vip-message-conditions conds
)))))
1917 (defun ex-write (q-flag)
1918 (vip-default-ex-addresses t
)
1920 (let ((end (car ex-addresses
))
1921 (beg (car (cdr ex-addresses
)))
1922 (orig-buf (current-buffer))
1923 (orig-buf-file-name (buffer-file-name))
1924 (orig-buf-name (buffer-name))
1925 (buff-changed-p (buffer-modified-p))
1926 temp-buf writing-same-file region
1927 file-exists writing-whole-file
)
1928 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1931 (vip-enlarge-region beg end
)
1932 (shell-command-on-region (point) (mark t
) ex-file
))
1933 (if (and (string= ex-file
"") (not (buffer-file-name)))
1936 (format "Buffer %s isn't visiting any file. File to save in: "
1939 (setq writing-whole-file
(and (= (point-min) beg
) (= (point-max) end
))
1940 ex-file
(if (string= ex-file
"")
1942 (expand-file-name ex-file
)))
1943 ;; if ex-file is a directory use the file portion of the buffer file name
1944 (if (and (file-directory-p ex-file
)
1946 (not (file-directory-p buffer-file-name
)))
1948 (concat (file-name-as-directory ex-file
)
1949 (file-name-nondirectory buffer-file-name
))))
1951 (setq file-exists
(file-exists-p ex-file
)
1952 writing-same-file
(string= ex-file
(buffer-file-name)))
1954 (if (and writing-whole-file writing-same-file
)
1955 (if (not (buffer-modified-p))
1956 (message "(No changes need to be saved)")
1960 (ex-write-info file-exists ex-file
(point-min) (point-max))
1962 ;; writing some other file or portion of the current file
1963 (cond ((and file-exists
1964 (not writing-same-file
)
1966 (format "File %s exists. Overwrite? " ex-file
))))
1968 ((and writing-whole-file
(not ex-append
))
1971 (set-visited-file-name ex-file
)
1972 (set-buffer-modified-p t
)
1974 ;; restore the buffer file name
1975 (set-visited-file-name orig-buf-file-name
)
1976 (set-buffer-modified-p buff-changed-p
)
1977 ;; If the buffer wasn't visiting a file, restore buffer name.
1978 ;; Name could've been changed by packages such as uniquify.
1979 (or orig-buf-file-name
1982 (rename-buffer orig-buf-name
))))
1986 file-exists ex-file
(point-min) (point-max))))
1987 (t ; writing a region
1990 (vip-enlarge-region beg end
)
1991 (setq region
(buffer-substring (point) (mark t
)))
1992 ;; create temp buffer for the region
1993 (setq temp-buf
(get-buffer-create " *ex-write*"))
1994 (set-buffer temp-buf
)
1995 (set-visited-file-name ex-file
'noquerry
)
1997 (if (and file-exists ex-append
)
1998 (insert-file-contents ex-file
))
1999 (goto-char (point-max))
2003 file-exists ex-file
(point-min) (point-max))
2005 (set-buffer temp-buf
)
2006 (set-buffer-modified-p nil
)
2007 (kill-buffer temp-buf
))
2009 (set-buffer orig-buf
)
2010 ;; this prevents the loss of data if writing part of the buffer
2011 (if (and (buffer-file-name) writing-same-file
)
2012 (set-visited-file-modtime))
2013 (or writing-whole-file
2014 (not writing-same-file
)
2015 (set-buffer-modified-p t
))
2017 (if (< viper-expert-level
2)
2018 (save-buffers-kill-emacs)
2019 (kill-buffer (current-buffer))))
2023 (defun ex-write-info (exists file-name beg end
)
2024 (message "`%s'%s %d lines, %d characters"
2025 (vip-abbreviate-file-name file-name
)
2026 (if exists
"" " [New file]")
2027 (count-lines beg
(min (1+ end
) (point-max)))
2032 (vip-default-ex-addresses)
2034 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2035 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
2037 (vip-enlarge-region beg end
)
2038 (exchange-point-and-mark)
2039 (if (or ex-g-flag ex-g-variant
)
2040 (error "Can't execute `yank' within `global'"))
2044 (forward-line (1- ex-count
)))
2046 (vip-enlarge-region (point) (mark t
))
2047 (if ex-flag
(error "`yank': %s" vip-SpuriousText
))
2049 (cond ((vip-valid-register ex-buffer
'(Letter))
2050 (vip-append-to-register
2051 (downcase ex-buffer
) (point) (mark t
)))
2052 ((vip-valid-register ex-buffer
)
2053 (copy-to-register ex-buffer
(point) (mark t
) nil
))
2054 (t (error vip-InvalidRegister ex-buffer
))))
2055 (copy-region-as-kill (point) (mark t
)))))
2057 ;; Execute shell command
2058 (defun ex-command ()
2060 (save-window-excursion
2061 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
2062 (set-buffer vip-ex-work-buf
)
2063 (skip-chars-forward " \t")
2064 (setq command
(buffer-substring (point) (point-max)))
2066 (setq command
(ex-expand-filsyms command
(current-buffer)))
2067 (if (and (> (length command
) 0) (string= "!" (substring command
0 1)))
2068 (if vip-ex-last-shell-com
2069 (setq command
(concat vip-ex-last-shell-com
(substring command
1)))
2070 (error "No previous shell command")))
2071 (setq vip-ex-last-shell-com command
)
2072 (if (null ex-addresses
)
2073 (shell-command command
)
2074 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2075 (if (null beg
) (setq beg end
))
2079 (vip-enlarge-region (point) (mark t
))
2080 (shell-command-on-region (point) (mark t
) command t
))
2083 ;; Print line number
2084 (defun ex-line-no ()
2088 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
2090 ;; Give information on the file visited by the current buffer
2091 (defun vip-info-on-file ()
2093 (let ((pos1 (vip-line-pos 'start
))
2094 (pos2 (vip-line-pos 'end
))
2096 (setq lines
(count-lines (point-min) (vip-line-pos 'end
))
2097 file
(if (buffer-file-name)
2098 (concat (vip-abbreviate-file-name (buffer-file-name)) ":")
2099 (concat (buffer-name) " [Not visiting any file]:"))
2100 info
(format "line=%d/%d pos=%d/%d col=%d %s"
2104 (count-lines (point-min) (point-max))
2105 (point) (1- (point-max))
2106 (1+ (current-column))
2107 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2108 (if (< (+ 1 (length info
) (length file
))
2109 (window-width (minibuffer-window)))
2110 (message (concat file
" " info
))
2111 (save-window-excursion
2112 (with-output-to-temp-buffer " *vip-info*"
2115 "\n\n\nPress any key to continue...\n\n")))
2117 (kill-buffer " *vip-info*")))
2120 ;; display all variables set through :set
2121 (defun ex-show-vars ()
2122 (with-output-to-temp-buffer " *vip-info*"
2123 (princ (if vip-auto-indent
2124 "autoindent (local)\n" "noautoindent (local)\n"))
2125 (princ (if (default-value 'vip-auto-indent
)
2126 "autoindent (global) \n" "noautoindent (global) \n"))
2127 (princ (if vip-case-fold-search
"ignorecase\n" "noignorecase\n"))
2128 (princ (if vip-re-search
"magic\n" "nomagic\n"))
2129 (princ (if buffer-read-only
"readonly\n" "noreadonly\n"))
2130 (princ (if blink-matching-paren
"showmatch\n" "noshowmatch\n"))
2131 (princ (if vip-search-wrap-around-t
"wrapscan\n" "nowrapscan\n"))
2132 (princ (format "shiftwidth \t\t= %S\n" vip-shift-width
))
2133 (princ (format "tabstop (local) \t= %S\n" tab-width
))
2134 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width
)))
2135 (princ (format "wrapmargin (local) \t= %S\n"
2136 (- (window-width) fill-column
)))
2137 (princ (format "wrapmargin (global) \t= %S\n"
2138 (- (window-width) (default-value 'fill-column
))))
2139 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name
)
2140 explicit-shell-file-name
2148 ;;; viper-ex.el ends here