1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994, 1995, 1996 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.
28 (defvar read-file-name-map)
29 ;; end compiler pacifier
33 (defconst vip-ex-work-buf-name " *ex-working-space*")
34 (defconst vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
35 (defconst vip-ex-tmp-buf-name " *ex-tmp*")
38 ;;; Variable completion in :set command
40 ;; The list of Ex commands. Used for completing command names.
41 (defconst ex-token-alist
42 '(("!") ("=") (">") ("&") ("~")
43 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
44 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
45 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
46 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
47 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
48 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
49 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
50 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
52 ;; A-list of Ex variables that can be set using the :set command.
53 (defconst ex-variable-alist
54 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
55 ("global-tabstop") ("gts") ("tabstop") ("ts")
56 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
58 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
59 ("noreadonly") ("noro") ("nomagic") ("noma")
60 ("noignorecase") ("noic")
61 ("global-noautoindent") ("gnoai") ("noautoindent") ("noai")
62 ("magic") ("ma") ("ignorecase") ("ic")
63 ("global-autoindent") ("gai") ("autoindent") ("ai")
68 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
72 ;; If non-nil, gives type of address; if nil, it is a command.
73 (defvar ex-token-type nil)
75 ;; List of addresses passed to Ex command
76 (defvar ex-addresses nil)
78 ;; It seems that this flag is used only for `#', `print', and `list', which
79 ;; aren't implemented. Check later.
82 ;; "buffer" where Ex commands keep deleted data.
83 ;; In Emacs terms, this is a register.
84 (defvar ex-buffer nil)
89 ;; Flag for global command.
90 (defvar ex-g-flag nil)
92 ;; If t, global command is executed on lines not matching ex-g-pat.
93 (defvar ex-g-variant nil)
95 ;; Save reg-exp used in substitute.
96 (defvar ex-reg-exp nil)
99 ;; Replace pattern for substitute.
102 ;; Pattern for global command.
103 (defvar ex-g-pat nil)
106 (defvar ex-unix-type-shell
107 (let ((case-fold-search t))
108 (and (stringp shell-file-name)
118 "[^a-z]sh$\\|[^a-z]sh.exe$"
123 "Is the user using a unix-type shell?")
125 (defvar ex-unix-type-shell-options
126 (let ((case-fold-search t))
127 (if ex-unix-type-shell
128 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name)
129 "-f") ; csh: do it fast
130 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name)
131 "-noprofile") ; bash: ignore .profile
133 "Options to pass to the Unix-style shell.
134 Don't put `-c' here, as it is added automatically.")
136 (defvar ex-nontrivial-find-file-function
137 (cond (ex-unix-type-shell 'vip-ex-nontrivial-find-file-unix)
138 ((eq system-type 'emx) 'vip-ex-nontrivial-find-file-ms) ; OS/2
139 (vip-ms-style-os-p 'vip-ex-nontrivial-find-file-ms) ; a Microsoft OS
140 (vip-vms-os-p 'vip-ex-nontrivial-find-file-unix) ; VMS
141 (t 'vip-ex-nontrivial-find-file-unix) ; presumably UNIX
144 ;; Remembers the previous Ex tag.
147 ;; file used by Ex commands like :r, :w, :n
150 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
151 (defvar ex-variant nil)
153 ;; Specified the offset of an Ex command, such as :read.
154 (defvar ex-offset nil)
156 ;; Tells Ex that this is a w>> command.
157 (defvar ex-append nil)
159 ;; File containing the shell command to be executed at Ex prompt,
161 (defvar ex-cmdfile nil)
163 ;; flag used in vip-ex-read-file-name to indicate that we may be reading
164 ;; multiple file names. Used for :edit and :next
165 (defvar vip-keep-reading-filename nil)
167 (defconst ex-cycle-other-window t
168 "*If t, :n and :b cycles through files and buffers in other window.
169 Then :N and :B cycles in the current window. If nil, this behavior is
172 (defconst ex-cycle-through-non-files nil
173 "*Cycle through *scratch* and other buffers that don't visit any file.")
175 ;; Last shell command executed with :! command.
176 (defvar vip-ex-last-shell-com nil)
178 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
179 (defvar vip-incomplete-ex-cmd nil)
181 ;; Remembers the last ex-command prompt.
182 (defvar vip-last-ex-prompt "")
187 ;; Check if ex-token is an initial segment of STR
188 (defun vip-check-sub (str)
189 (let ((length (length ex-token)))
190 (if (and (<= length (length str))
191 (string= ex-token (substring str 0 length)))
193 (setq ex-token-type 'non-command))))
195 ;; Get a complete ex command
196 (defun vip-get-ex-com-subr ()
197 (let (case-fold-search)
199 (re-search-forward "[a-zA-Z][a-zA-Z]*")
200 (setq ex-token-type 'command)
201 (setq ex-token (buffer-substring (point) (mark t)))
202 (exchange-point-and-mark)
203 (cond ((looking-at "a")
204 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
205 ((looking-at "ar") (vip-check-sub "args"))
206 (t (vip-check-sub "append"))))
207 ((looking-at "h") (vip-check-sub "help"))
209 (cond ((looking-at "cd") (vip-check-sub "cd"))
210 ((looking-at "ch") (vip-check-sub "chdir"))
211 ((looking-at "co") (vip-check-sub "copy"))
212 (t (vip-check-sub "change"))))
213 ((looking-at "d") (vip-check-sub "delete"))
214 ((looking-at "b") (vip-check-sub "buffer"))
215 ((looking-at "B") (vip-check-sub "Buffer"))
217 (if (looking-at "ex") (vip-check-sub "ex")
218 (vip-check-sub "edit")))
219 ((looking-at "f") (vip-check-sub "file"))
220 ((looking-at "g") (vip-check-sub "global"))
221 ((looking-at "i") (vip-check-sub "insert"))
222 ((looking-at "j") (vip-check-sub "join"))
223 ((looking-at "l") (vip-check-sub "list"))
225 (cond ((looking-at "map") (vip-check-sub "map"))
226 ((looking-at "mar") (vip-check-sub "mark"))
227 (t (vip-check-sub "move"))))
228 ((looking-at "k[a-z][^a-z]")
229 (setq ex-token "kmark")
231 (exchange-point-and-mark)) ; this is canceled out by another
232 ; exchange-point-and-mark at the end
233 ((looking-at "k") (vip-check-sub "kmark"))
234 ((looking-at "n") (if (looking-at "nu")
235 (vip-check-sub "number")
236 (vip-check-sub "next")))
237 ((looking-at "N") (vip-check-sub "Next"))
238 ((looking-at "o") (vip-check-sub "open"))
240 (cond ((looking-at "pre") (vip-check-sub "preserve"))
241 ((looking-at "pu") (vip-check-sub "put"))
242 ((looking-at "pw") (vip-check-sub "pwd"))
243 (t (vip-check-sub "print"))))
244 ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
245 ((looking-at "R") (vip-check-sub "RelatedFile"))
246 ((looking-at "q") (vip-check-sub "quit"))
248 (cond ((looking-at "rec") (vip-check-sub "recover"))
249 ((looking-at "rew") (vip-check-sub "rewind"))
250 (t (vip-check-sub "read"))))
252 (cond ((looking-at "se") (vip-check-sub "set"))
253 ((looking-at "sh") (vip-check-sub "shell"))
254 ((looking-at "so") (vip-check-sub "source"))
255 ((looking-at "sr") (vip-check-sub "sr"))
256 ((looking-at "st") (vip-check-sub "stop"))
257 ((looking-at "sus") (vip-check-sub "suspend"))
258 ((looking-at "subm") (vip-check-sub "submitReport"))
259 (t (vip-check-sub "substitute"))))
261 (if (looking-at "ta") (vip-check-sub "tag")
262 (vip-check-sub "transfer")))
264 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
265 ((looking-at "unm") (vip-check-sub "unmap"))
266 (t (vip-check-sub "undo"))))
268 (cond ((looking-at "ve") (vip-check-sub "version"))
269 ((looking-at "vi") (vip-check-sub "visual"))
270 (t (vip-check-sub "vglobal"))))
272 (if (looking-at "wq") (vip-check-sub "wq")
273 (vip-check-sub "write")))
275 (if (looking-at "WW")
276 (vip-check-sub "WWrite")
277 (vip-check-sub "Write")))
278 ((looking-at "x") (vip-check-sub "xit"))
279 ((looking-at "y") (vip-check-sub "yank"))
280 ((looking-at "z") (vip-check-sub "z")))
281 (exchange-point-and-mark)
284 ;; Get an ex-token which is either an address or a command.
285 ;; A token has a type, \(command, address, end-mark\), and a value
286 (defun vip-get-ex-token ()
287 (save-window-excursion
288 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
289 (set-buffer vip-ex-work-buf)
290 (skip-chars-forward " \t|")
291 (cond ((looking-at "#")
292 (setq ex-token-type 'command)
293 (setq ex-token (char-to-string (following-char)))
295 ((looking-at "[a-z]") (vip-get-ex-com-subr))
298 (setq ex-token-type 'dot))
299 ((looking-at "[0-9]")
301 (re-search-forward "[0-9]*")
303 (cond ((eq ex-token-type 'plus) 'add-number)
304 ((eq ex-token-type 'minus) 'sub-number)
306 (setq ex-token (string-to-int (buffer-substring (point) (mark t)))))
309 (setq ex-token-type 'end))
312 (setq ex-token-type 'whole))
314 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
318 (setq ex-token-type 'plus))
319 ((looking-at "+[0-9]")
321 (setq ex-token-type 'plus))
323 (error vip-BadAddress))))
325 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
329 (setq ex-token-type 'minus))
330 ((looking-at "-[0-9]")
332 (setq ex-token-type 'minus))
334 (error vip-BadAddress))))
339 (while (and (not (eolp)) cont)
340 ;;(re-search-forward "[^/]*/")
341 (re-search-forward "[^/]*\\(/\\|\n\\)")
342 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
345 (setq ex-token (buffer-substring (point) (mark t)))
346 (if (looking-at "/") (forward-char 1))
347 (setq ex-token-type 'search-forward))
352 (while (and (not (eolp)) cont)
353 ;;(re-search-forward "[^\\?]*\\?")
354 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
355 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
358 (if (not (looking-at "\n")) (forward-char 1))))
359 (setq ex-token-type 'search-backward)
360 (setq ex-token (buffer-substring (1- (point)) (mark t))))
363 (setq ex-token-type 'comma))
366 (setq ex-token-type 'semi-colon))
367 ((looking-at "[!=><&~]")
368 (setq ex-token-type 'command)
369 (setq ex-token (char-to-string (following-char)))
372 (setq ex-token-type 'goto-mark)
374 (cond ((looking-at "'") (setq ex-token nil))
375 ((looking-at "[a-z]") (setq ex-token (following-char)))
376 (t (error "Marks are ' and a-z")))
379 (setq ex-token-type 'end-mark)
380 (setq ex-token "goto"))
382 (error vip-BadExCommand)))))
384 ;; Reads Ex command. Tries to determine if it has to exit because command
385 ;; is complete or invalid. If not, keeps reading command.
386 (defun ex-cmd-read-exit ()
388 (setq vip-incomplete-ex-cmd t)
389 (let ((quit-regex1 (concat
392 "\\|" "[nN]ext[ \t]*"
405 "\\|" "[ktgjmsz][ \t]*$"
407 "\\|" "tr[ansfer \t]*"
410 "\\|" "^[ \t]*k?ma[^p]*"
419 "\\|" "![ \t]*[a-zA-Z].*"
423 (save-window-excursion ;; put cursor at the end of the Ex working buffer
424 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
425 (set-buffer vip-ex-work-buf)
426 (goto-char (point-max)))
427 (cond ((vip-looking-back quit-regex1) (exit-minibuffer))
428 ((vip-looking-back stay-regex) (insert " "))
429 ((vip-looking-back quit-regex2) (exit-minibuffer))
432 ;; complete Ex command
433 (defun ex-cmd-complete ()
435 (let (save-pos dist compl-list string-to-complete completion-result)
438 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
442 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
444 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
445 ;; Preceding characters are not the ones allowed in an Ex command
446 ;; or we have typed past command name.
447 ;; Note: we didn't do parsing, so there may be surprises.
448 (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
449 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
450 (looking-at "[^ \t\n\C-m]"))
452 (with-output-to-temp-buffer "*Completions*"
453 (display-completion-list
454 (vip-alist-to-list ex-token-alist))))
455 ;; Preceding chars may be part of a command name
456 (setq string-to-complete (buffer-substring save-pos (point)))
457 (setq completion-result
458 (try-completion string-to-complete ex-token-alist))
460 (cond ((eq completion-result t) ; exact match--do nothing
461 (vip-tmp-insert-at-eob " (Sole completion)"))
462 ((eq completion-result nil)
463 (vip-tmp-insert-at-eob " (No match)"))
464 (t ;; partial completion
466 (delete-region (point) (point-max))
467 (insert completion-result)
468 (let (case-fold-search)
470 (vip-filter-alist (concat "^" completion-result)
472 (if (> (length compl-list) 1)
473 (with-output-to-temp-buffer "*Completions*"
474 (display-completion-list
475 (vip-alist-to-list (reverse compl-list)))))))
480 ;; Ex commands themselves are implemented in viper-ex.el
481 (defun vip-ex (&optional string)
486 (let* ((map (copy-keymap minibuffer-local-map))
490 prev-token-type com-str)
492 (vip-add-keymap vip-ex-cmd-map map)
494 (setq com-str (or string (vip-read-string-with-history
500 (save-window-excursion
502 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
503 (set-buffer vip-ex-work-buf)
504 (delete-region (point-min) (point-max))
505 (insert com-str "\n")
506 (goto-char (point-min)))
507 (setq ex-token-type nil
511 (cond ((memq ex-token-type '(command end-mark))
512 (if address (setq ex-addresses (cons address ex-addresses)))
513 (cond ((string= ex-token "global")
516 ((string= ex-token "vglobal")
520 (vip-execute-ex-command)
521 (save-window-excursion
522 (setq vip-ex-work-buf
523 (get-buffer-create vip-ex-work-buf-name))
524 (set-buffer vip-ex-work-buf)
525 (skip-chars-forward " \t")
526 (cond ((looking-at "|")
530 (t (error "`%s': %s" ex-token vip-SpuriousText)))
533 ((eq ex-token-type 'non-command)
534 (error "`%s': %s" ex-token vip-BadExCommand))
535 ((eq ex-token-type 'whole)
539 (cons (point-max) ex-addresses)
540 (cons (point-max) (cons (point-min) ex-addresses)))))
541 ((eq ex-token-type 'comma)
542 (if (eq prev-token-type 'whole)
543 (setq address (point-min)))
545 (cons (if (null address) (point) address) ex-addresses)))
546 ((eq ex-token-type 'semi-colon)
547 (if (eq prev-token-type 'whole)
548 (setq address (point-min)))
549 (if address (setq dot address))
551 (cons (if (null address) (point) address) ex-addresses)))
552 (t (let ((ans (vip-get-ex-address-subr address dot)))
553 (if ans (setq address ans)))))
554 (setq prev-token-type ex-token-type))))
557 ;; Get a regular expression and set `ex-variant', if found
558 (defun vip-get-ex-pat ()
559 (save-window-excursion
560 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
561 (set-buffer vip-ex-work-buf)
562 (skip-chars-forward " \t")
565 (setq ex-g-variant (not ex-g-variant)
566 ex-g-flag (not ex-g-flag))
568 (skip-chars-forward " \t")))
569 (let ((c (following-char)))
570 (if (string-match "[0-9A-Za-z]" (format "%c" c))
572 "Global regexp must be inside matching non-alphanumeric chars"))
573 (if (looking-at "[^\\\\\n]")
578 (while (and (not (eolp)) cont)
579 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
580 (if (member ex-token '("global" "vglobal"))
582 "Missing closing delimiter for global regexp")
583 (goto-char (point-max))))
584 (if (not (vip-looking-back
585 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
588 (if (= (mark t) (point)) ""
589 (buffer-substring (1- (point)) (mark t))))
595 (defun vip-get-ex-command ()
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 (if (looking-at "/") (forward-char 1))
600 (skip-chars-forward " \t")
601 (cond ((looking-at "[a-z]")
602 (vip-get-ex-com-subr)
603 (if (eq ex-token-type 'non-command)
604 (error "`%s': %s" ex-token vip-BadExCommand)))
605 ((looking-at "[!=><&~]")
606 (setq ex-token (char-to-string (following-char)))
608 (t (error vip-BadExCommand)))))
610 ;; Get an Ex option g or c
611 (defun vip-get-ex-opt-gc (c)
612 (save-window-excursion
613 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
614 (set-buffer vip-ex-work-buf)
615 (if (looking-at (format "%c" c)) (forward-char 1))
616 (skip-chars-forward " \t")
617 (cond ((looking-at "g")
627 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
628 (defun vip-default-ex-addresses (&optional whole-flag)
629 (cond ((null ex-addresses)
632 (cons (point-max) (cons (point-min) nil))
633 (cons (point) (cons (point) nil)))))
634 ((null (cdr ex-addresses))
636 (cons (car ex-addresses) ex-addresses)))))
638 ;; Get an ex-address as a marker and set ex-flag if a flag is found
639 (defun vip-get-ex-address ()
640 (let ((address (point-marker)) (cont t))
645 (cond ((eq ex-token-type 'command)
646 (if (member ex-token '("print" "list" "#"))
650 (error "Address expected in this Ex command")))
651 ((eq ex-token-type 'end-mark)
653 ((eq ex-token-type 'whole)
654 (error "Trailing address expected"))
655 ((eq ex-token-type 'comma)
656 (error "`%s': %s" ex-token vip-SpuriousText))
657 (t (let ((ans (vip-get-ex-address-subr address (point-marker))))
658 (if ans (setq address ans))))))
661 ;; Returns an address as a point
662 (defun vip-get-ex-address-subr (old-address dot)
664 (if (null old-address) (setq old-address dot))
665 (cond ((eq ex-token-type 'dot)
667 ((eq ex-token-type 'add-number)
669 (goto-char old-address)
670 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
671 (setq address (point-marker))))
672 ((eq ex-token-type 'sub-number)
674 (goto-char old-address)
675 (forward-line (- ex-token))
676 (setq address (point-marker))))
677 ((eq ex-token-type 'abs-number)
679 (goto-char (point-min))
680 (if (= ex-token 0) (setq address 0)
681 (forward-line (1- ex-token))
682 (setq address (point-marker)))))
683 ((eq ex-token-type 'end)
684 (setq address (point-max-marker)))
685 ((eq ex-token-type 'plus) t) ; do nothing
686 ((eq ex-token-type 'minus) t) ; do nothing
687 ((eq ex-token-type 'search-forward)
689 (ex-search-address t)
690 (setq address (point-marker))))
691 ((eq ex-token-type 'search-backward)
693 (ex-search-address nil)
694 (setq address (point-marker))))
695 ((eq ex-token-type 'goto-mark)
698 (exchange-point-and-mark)
699 (goto-char (vip-register-to-point
700 (1+ (- ex-token ?a)) 'enforce-buffer)))
701 (setq address (point-marker)))))
705 ;; Search pattern and set address
706 (defun ex-search-address (forward)
707 (if (string= ex-token "")
708 (if (null vip-s-string)
709 (error vip-NoPrevSearch)
710 (setq ex-token vip-s-string))
711 (setq vip-s-string ex-token))
715 (re-search-forward ex-token))
717 (re-search-backward ex-token)))
719 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
720 (defun vip-get-ex-buffer ()
724 (save-window-excursion
725 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
726 (set-buffer vip-ex-work-buf)
727 (skip-chars-forward " \t")
728 (if (looking-at "[a-zA-Z]")
730 (setq ex-buffer (following-char))
732 (skip-chars-forward " \t")))
733 (if (looking-at "[0-9]")
736 (re-search-forward "[0-9][0-9]*")
737 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
738 (skip-chars-forward " \t")))
739 (if (looking-at "[pl#]")
743 (if (not (looking-at "[\n|]"))
744 (error "`%s': %s" ex-token vip-SpuriousText))))
746 (defun vip-get-ex-count ()
750 (save-window-excursion
751 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
752 (set-buffer vip-ex-work-buf)
753 (skip-chars-forward " \t")
758 (skip-chars-forward " \t")
759 (if (looking-at "[0-9]")
762 (re-search-forward "[0-9][0-9]*")
763 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
764 (skip-chars-forward " \t")))
765 (if (looking-at "[pl#]")
769 (if (not (looking-at "[\n|]"))
771 (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand))))
773 ;; Expand \% and \# in ex command
774 (defun ex-expand-filsyms (cmd buf)
778 (setq cf buffer-file-name)
779 (setq pf (ex-next nil t))) ; this finds alternative file name
780 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
781 (error "No current file to substitute for `%%'"))
782 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
783 (error "No alternate file to substitute for `#'"))
785 (set-buffer (get-buffer-create vip-ex-tmp-buf-name))
788 (goto-char (point-min))
789 (while (re-search-forward "%\\|#" nil t)
790 (let ((data (match-data))
791 (char (buffer-substring (match-beginning 0) (match-end 0))))
792 (if (vip-looking-back (concat "\\\\" char))
794 (store-match-data data)
795 (if (string= char "%")
797 (replace-match pf)))))
799 (setq ret (buffer-substring (point-min) (point)))
803 ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
804 (defun vip-get-ex-file ()
812 (save-window-excursion
813 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
814 (set-buffer vip-ex-work-buf)
815 (skip-chars-forward " \t")
817 (if (and (not (vip-looking-back "[ \t]"))
818 ;; read doesn't have a corresponding :r! form, so ! is
819 ;; immediately interpreted as a shell command.
820 (not (string= ex-token "read")))
824 (skip-chars-forward " \t"))
827 (skip-chars-forward " \t")))
828 (if (looking-at ">>")
833 (skip-chars-forward " \t")))
838 (re-search-forward "[ \t\n]")
840 (setq ex-offset (buffer-substring (point) (mark t)))
842 (skip-chars-forward " \t")))
843 ;; this takes care of :r, :w, etc., when they get file names
844 ;; from the history list
845 (if (member ex-token '("read" "write" "edit" "visual" "next"))
847 (setq ex-file (buffer-substring (point) (1- (point-max))))
849 ;; For :e, match multiple non-white strings separated
850 ;; by white. For others, find the first non-white string
852 (if (string= ex-token "edit")
853 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
857 ;; if file name comes from history, don't leave
858 ;; minibuffer when the user types space
859 (setq vip-incomplete-ex-cmd nil)
860 ;; this must be the last clause in this progn
861 (substring ex-file (match-beginning 0) (match-end 0))
864 ;; this leaves only the command name in the work area
865 ;; file names are gone
866 (delete-region (point) (1- (point-max)))
868 (goto-char (point-max))
869 (skip-chars-backward " \t\n")
870 (setq prompt (buffer-substring (point-min) (point)))
873 (setq vip-last-ex-prompt prompt)
875 ;; If we just finished reading command, redisplay prompt
876 (if vip-incomplete-ex-cmd
877 (setq ex-file (vip-ex-read-file-name (format ":%s " prompt)))
878 ;; file was typed in-line
879 (setq ex-file (or ex-file "")))
883 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
884 ;; file names, arranges to re-enter the minibuffer.
885 (defun vip-complete-filename-or-exit ()
887 (setq vip-keep-reading-filename t)
888 ;; don't exit if directory---ex-commands don't
889 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
890 ;; apparently the argument to an Ex command is
891 ;; supposed to be a shell command
892 ((vip-looking-back "^[ \t]*!.*")
896 (setq ex-cmdfile nil)
897 (minibuffer-complete-word))))
899 (defun vip-handle-! ()
902 (buffer-string) (vip-abbreviate-file-name default-directory))
903 (member ex-token '("read" "write")))
907 (defun ex-cmd-accepts-multiple-files-p (token)
908 (member token '("edit" "next" "Next")))
910 ;; If user doesn't enter anything, then "" is returned, i.e., the
911 ;; prompt-directory is not returned.
912 (defun vip-ex-read-file-name (prompt)
914 (minibuffer-local-completion-map
915 (copy-keymap minibuffer-local-completion-map))
918 (vip-add-keymap ex-read-filename-map
920 minibuffer-local-completion-map
923 (setq cont (setq vip-keep-reading-filename t))
925 (setq vip-keep-reading-filename nil
926 val (read-file-name (concat prompt str) nil default-directory))
927 (if (string-match " " val)
928 (setq val (concat "\\\"" val "\\\"")))
929 (setq str (concat str (if (equal val "") "" " ")
930 val (if (equal val "") "" " ")))
932 ;; Only edit, next, and Next commands accept multiple files.
933 ;; vip-keep-reading-filename is set in the anonymous function that is
934 ;; bound to " " in ex-read-filename-map.
935 (setq cont (and vip-keep-reading-filename
936 (ex-cmd-accepts-multiple-files-p ex-token)))
939 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
940 end (string-match "[ \t]*$" str)) ; delete trailing blanks
941 (if (member ex-token '("read" "write"))
942 (if (string-match "[\t ]*!" str)
943 ;; this is actually a shell command
947 (setq vip-last-ex-prompt (concat vip-last-ex-prompt " !")))))
948 (substring str (or beg 0) end)))
950 ;; Execute ex command using the value of addresses
951 (defun vip-execute-ex-command ()
952 (vip-deactivate-mark)
953 (cond ((string= ex-token "args") (ex-args))
954 ((string= ex-token "copy") (ex-copy nil))
955 ((string= ex-token "cd") (ex-cd))
956 ((string= ex-token "chdir") (ex-cd))
957 ((string= ex-token "delete") (ex-delete))
958 ((string= ex-token "edit") (ex-edit))
959 ((string= ex-token "file") (vip-info-on-file))
960 ((string= ex-token "goto") (ex-goto))
961 ((string= ex-token "help") (ex-help))
962 ((string= ex-token "join") (ex-line "join"))
963 ((string= ex-token "kmark") (ex-mark))
964 ((string= ex-token "mark") (ex-mark))
965 ((string= ex-token "map") (ex-map))
966 ((string= ex-token "move") (ex-copy t))
967 ((string= ex-token "next") (ex-next ex-cycle-other-window))
968 ((string= ex-token "Next") (ex-next (not ex-cycle-other-window)))
969 ((string= ex-token "RelatedFile") (ex-next-related-buffer 1))
970 ((string= ex-token "put") (ex-put))
971 ((string= ex-token "pwd") (ex-pwd))
972 ((string= ex-token "preserve") (ex-preserve))
973 ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1))
974 ((string= ex-token "quit") (ex-quit))
975 ((string= ex-token "read") (ex-read))
976 ((string= ex-token "recover") (ex-recover))
977 ((string= ex-token "rewind") (ex-rewind))
978 ((string= ex-token "submitReport") (vip-submit-report))
979 ((string= ex-token "set") (ex-set))
980 ((string= ex-token "shell") (ex-shell))
981 ((string= ex-token "source") (ex-source))
982 ((string= ex-token "sr") (ex-substitute t t))
983 ((string= ex-token "substitute") (ex-substitute))
984 ((string= ex-token "suspend") (suspend-emacs))
985 ((string= ex-token "stop") (suspend-emacs))
986 ((string= ex-token "transfer") (ex-copy nil))
987 ((string= ex-token "buffer") (if ex-cycle-other-window
988 (vip-switch-to-buffer-other-window)
989 (vip-switch-to-buffer)))
990 ((string= ex-token "Buffer") (if ex-cycle-other-window
991 (vip-switch-to-buffer)
992 (vip-switch-to-buffer-other-window)))
993 ((string= ex-token "tag") (ex-tag))
994 ((string= ex-token "undo") (vip-undo))
995 ((string= ex-token "unmap") (ex-unmap))
996 ((string= ex-token "version") (vip-version))
997 ((string= ex-token "visual") (ex-edit))
998 ((string= ex-token "write") (ex-write nil))
999 ((string= ex-token "Write") (save-some-buffers))
1000 ((string= ex-token "wq") (ex-write t))
1001 ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask
1002 ((string= ex-token "xit") (ex-write t))
1003 ((string= ex-token "yank") (ex-yank))
1004 ((string= ex-token "!") (ex-command))
1005 ((string= ex-token "=") (ex-line-no))
1006 ((string= ex-token ">") (ex-line "right"))
1007 ((string= ex-token "<") (ex-line "left"))
1008 ((string= ex-token "&") (ex-substitute t))
1009 ((string= ex-token "~") (ex-substitute t t))
1010 ((or (string= ex-token "append")
1011 (string= ex-token "change")
1012 (string= ex-token "insert")
1013 (string= ex-token "open"))
1014 (error "`%s': Obsolete command, not supported by Viper" ex-token))
1015 ((or (string= ex-token "abbreviate")
1016 (string= ex-token "unabbreviate"))
1018 "`%s': Vi abbrevs are obsolete. Use the more powerful Emacs abbrevs"
1020 ((or (string= ex-token "list")
1021 (string= ex-token "print")
1022 (string= ex-token "z")
1023 (string= ex-token "#"))
1024 (error "`%s': Command not implemented in Viper" ex-token))
1025 (t (error "`%s': %s" ex-token vip-BadExCommand))))
1027 (defun vip-undisplayed-files ()
1031 (if (null (get-buffer-window b))
1032 (let ((f (buffer-file-name b)))
1034 (if ex-cycle-through-non-files
1035 (let ((s (buffer-name b)))
1036 (if (string= " " (substring s 0 1))
1045 (let ((l (vip-undisplayed-files))
1048 (while (not (null l))
1050 (setq args (format "%s %d) %s\n" args file-count (car l))
1051 file-count (1+ file-count)))
1053 (if (string= args "")
1054 (message "All files are already displayed")
1056 (save-window-excursion
1057 (with-output-to-temp-buffer " *vip-info*"
1058 (princ "\n\nThese files are not displayed in any window.\n")
1059 (princ "\n=============\n")
1061 (princ "\n=============\n")
1062 (princ "\nThe numbers can be given as counts to :next. ")
1063 (princ "\n\nPress any key to continue...\n\n"))
1064 (vip-read-event))))))
1066 ;; Ex cd command. Default directory of this buffer changes
1069 (if (string= ex-file "")
1071 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
1073 ;; Ex copy and move command. DEL-FLAG means delete
1074 (defun ex-copy (del-flag)
1075 (vip-default-ex-addresses)
1076 (let ((address (vip-get-ex-address))
1077 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1081 (vip-enlarge-region (mark t) (point))
1083 (kill-region (point) (mark t))
1084 (copy-region-as-kill (point) (mark t)))
1087 (with-output-to-temp-buffer "*copy text*"
1089 (if (or del-flag ex-g-flag ex-g-variant)
1091 (buffer-substring (point) (mark t)))))
1094 (read-string "[Hit return to continue] ")
1095 (save-excursion (kill-buffer "*copy text*")))
1096 (quit (save-excursion (kill-buffer "*copy text*"))
1097 (signal 'quit nil))))))
1099 (goto-char (point-min))
1102 (insert (current-kill 0))))
1104 ;; Ex delete command
1106 (vip-default-ex-addresses)
1108 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1109 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1111 (vip-enlarge-region beg end)
1112 (exchange-point-and-mark)
1116 (forward-line (1- ex-count)))
1118 (vip-enlarge-region (point) (mark t))
1120 ;; show text to be deleted and ask for confirmation
1122 (with-output-to-temp-buffer " *delete text*"
1123 (princ (buffer-substring (point) (mark t))))
1125 (read-string "[Hit return to continue] ")
1127 (save-excursion (kill-buffer " *delete text*"))
1129 (save-excursion (kill-buffer " *delete text*")))
1131 (cond ((vip-valid-register ex-buffer '(Letter))
1132 (vip-append-to-register
1133 (downcase ex-buffer) (point) (mark t)))
1134 ((vip-valid-register ex-buffer)
1135 (copy-to-register ex-buffer (point) (mark t) nil))
1136 (t (error vip-InvalidRegister ex-buffer))))
1137 (kill-region (point) (mark t))))))
1142 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1143 ;; asked if current buffer should really be discarded.
1144 ;; This command can take multiple file names. It replaces the current buffer
1145 ;; with the first file in its argument list
1146 (defun ex-edit (&optional file)
1149 (cond ((and (string= ex-file "") buffer-file-name)
1150 (setq ex-file (vip-abbreviate-file-name (buffer-file-name))))
1151 ((string= ex-file "")
1152 (error vip-NoFileSpecified)))
1155 (if buffer-file-name
1156 (cond ((buffer-modified-p)
1158 (format "Buffer %s is modified. Discard changes? "
1161 ((not (verify-visited-file-modtime (current-buffer)))
1163 (format "File %s changed on disk. Reread from disk? "
1166 (t (setq do-edit nil))))
1169 (if (yes-or-no-p msg)
1171 (set-buffer-modified-p nil)
1172 (kill-buffer (current-buffer)))
1173 (message "Buffer %s was left intact" (buffer-name))))
1176 (if (null (setq file (get-file-buffer ex-file)))
1178 (ex-find-file ex-file)
1179 (or (eq major-mode 'dired-mode)
1180 (vip-change-state-to-vi))
1181 (goto-char (point-min)))
1182 (switch-to-buffer file))
1185 (save-window-excursion
1186 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
1187 (set-buffer vip-ex-work-buf)
1188 (delete-region (point-min) (point-max))
1189 (insert ex-offset "\n")
1190 (goto-char (point-min)))
1191 (goto-char (vip-get-ex-address))
1192 (beginning-of-line)))
1193 (ex-fixup-history vip-last-ex-prompt ex-file))
1195 ;; Find-file FILESPEC if it appears to specify a single file.
1196 ;; Otherwise, assume that FILES{EC is a wildcard.
1197 ;; In this case, split it into substrings separated by newlines.
1198 ;; Each line is assumed to be a file name. find-file's each file thus obtained.
1199 (defun ex-find-file (filespec)
1200 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1201 (cond ((file-exists-p filespec) (find-file filespec))
1202 ((string-match nonstandard-filename-chars filespec)
1203 (funcall ex-nontrivial-find-file-function filespec))
1204 (t (find-file filespec)))
1208 ;; Ex global command
1209 (defun ex-global (variant)
1210 (let ((gcommand ex-token))
1211 (if (or ex-g-flag ex-g-variant)
1212 (error "`%s' within `global' is not allowed" gcommand)
1220 (error "`%s': Missing regular expression" gcommand)))
1222 (if (string= ex-token "")
1223 (if (null vip-s-string)
1224 (error vip-NoPrevSearch)
1225 (setq ex-g-pat vip-s-string))
1226 (setq ex-g-pat ex-token
1227 vip-s-string ex-token))
1228 (if (null ex-addresses)
1229 (setq ex-addresses (list (point-max) (point-min)))
1230 (vip-default-ex-addresses))
1231 (let ((marks nil) (mark-count 0)
1232 com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1233 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1235 (vip-enlarge-region beg end)
1236 (exchange-point-and-mark)
1237 (let ((cont t) (limit (point-marker)))
1238 (exchange-point-and-mark)
1239 ;; skip the last line if empty
1241 (if (eobp) (vip-backward-char-carefully))
1242 (while (and cont (not (bobp)) (>= (point) limit))
1246 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1247 (if (or (and ex-g-flag found)
1248 (and ex-g-variant (not found)))
1251 (setq mark-count (1+ mark-count))
1252 (setq marks (cons (point-marker) marks)))))
1254 (if (bobp) (setq cont nil)
1257 (save-window-excursion
1258 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
1259 (set-buffer vip-ex-work-buf)
1260 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1262 (goto-char (car marks))
1264 (setq mark-count (1- mark-count))
1265 (setq marks (cdr marks)))))
1269 (if (null ex-addresses)
1270 (setq ex-addresses (cons (point) nil)))
1271 (push-mark (point) t)
1272 (goto-char (car ex-addresses))
1273 (beginning-of-line))
1275 ;; Ex line commands. COM is join, shift-right or shift-left
1276 (defun ex-line (com)
1277 (vip-default-ex-addresses)
1279 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1280 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1282 (vip-enlarge-region beg end)
1283 (exchange-point-and-mark)
1287 (forward-line ex-count)))
1289 ;; show text to be joined and ask for confirmation
1291 (with-output-to-temp-buffer " *text*"
1292 (princ (buffer-substring (point) (mark t))))
1295 (read-string "[Hit return to continue] ")
1296 (ex-line-subr com (point) (mark t)))
1298 (save-excursion (kill-buffer " *text*")))
1299 (ex-line-subr com (point) (mark t)))
1300 (setq point (point)))
1301 (goto-char (1- point))
1302 (beginning-of-line)))
1304 (defun ex-line-subr (com beg end)
1305 (cond ((string= com "join")
1306 (goto-char (min beg end))
1307 (while (and (not (eobp)) (< (point) (max beg end)))
1309 (if (and (<= (point) (max beg end)) (not (eobp)))
1312 (delete-region (point) (1- (point)))
1313 (if (not ex-variant) (fixup-whitespace))))))
1314 ((or (string= com "right") (string= com "left"))
1316 (min beg end) (max beg end)
1317 (if (string= com "right") vip-shift-width (- vip-shift-width)))
1318 (goto-char (max beg end))
1320 (vip-forward-char-carefully))))
1326 (if (null ex-addresses)
1328 (cons (point) nil)))
1329 (save-window-excursion
1330 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
1331 (set-buffer vip-ex-work-buf)
1332 (skip-chars-forward " \t")
1333 (if (looking-at "[a-z]")
1335 (setq char (following-char))
1337 (skip-chars-forward " \t")
1338 (if (not (looking-at "[\n|]"))
1339 (error "`%s': %s" ex-token vip-SpuriousText)))
1340 (error "`%s' requires a following letter" ex-token)))
1342 (goto-char (car ex-addresses))
1343 (point-to-register (1+ (- char ?a))))))
1347 ;; Alternate file is the file next to the first one in the buffer ring
1348 (defun ex-next (cycle-other-window &optional find-alt-file)
1351 (if (not find-alt-file)
1354 (if (or (char-or-string-p ex-offset)
1355 (and (not (string= "" ex-file))
1356 (not (string-match "^[0-9]+$" ex-file))))
1359 (throw 'ex-edit nil))
1360 (setq count (string-to-int ex-file))
1361 (if (= count 0) (setq count 1))
1362 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1364 (setq l (vip-undisplayed-files))
1366 (while (and (not (null l)) (null (car l)))
1368 (setq count (1- count))
1371 (if find-alt-file (car l)
1373 (if (and (car l) (get-file-buffer (car l)))
1374 (let* ((w (if cycle-other-window
1375 (get-lru-window) (selected-window)))
1376 (b (window-buffer w)))
1377 (set-window-buffer w (get-file-buffer (car l)))
1379 ;; this puts "next <count>" in the ex-command history
1380 (ex-fixup-history vip-last-ex-prompt ex-file))
1381 (error "Not that many undisplayed files")))))))
1384 (defun ex-next-related-buffer (direction &optional no-recursion)
1386 (vip-ring-rotate1 vip-related-files-and-buffers-ring direction)
1388 (let ((file-or-buffer-name
1389 (vip-current-ring-item vip-related-files-and-buffers-ring))
1390 (old-ring vip-related-files-and-buffers-ring)
1391 (old-win (selected-window))
1394 (or (and (ring-p vip-related-files-and-buffers-ring)
1395 (> (ring-length vip-related-files-and-buffers-ring) 0))
1396 (error "This buffer has no related files or buffers"))
1398 (or (stringp file-or-buffer-name)
1400 "File and buffer names must be strings, %S" file-or-buffer-name))
1402 (setq buf (cond ((get-buffer file-or-buffer-name))
1403 ((file-exists-p file-or-buffer-name)
1404 (find-file-noselect file-or-buffer-name))
1407 (if (not (vip-buffer-live-p buf))
1408 (error "Didn't find buffer %S or file %S"
1410 (vip-abbreviate-file-name
1411 (expand-file-name file-or-buffer-name))))
1413 (if (equal buf (current-buffer))
1418 (ex-next-related-buffer direction 'norecursion))))
1423 (if (setq wind (vip-get-visible-buffer-window buf))
1425 (setq wind (get-lru-window (if vip-xemacs-p nil 'visible)))
1426 (set-window-buffer wind buf))
1428 (if (vip-window-display-p)
1430 (raise-frame (window-frame wind))
1431 (if (equal (window-frame wind) (window-frame old-win))
1432 (save-window-excursion (select-window wind) (sit-for 1))
1433 (select-window wind)))
1434 (save-window-excursion (select-window wind) (sit-for 1)))
1438 (setq vip-related-files-and-buffers-ring old-ring))
1440 (setq vip-local-search-start-marker (point-marker))
1445 (defun ex-preserve ()
1446 (message "Autosaving all buffers that need to be saved...")
1451 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1453 (setq vip-use-register ex-buffer)
1455 (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
1457 ;; Ex print working directory
1459 (message default-directory))
1463 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1465 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
1466 (set-buffer vip-ex-work-buf)
1467 (if (looking-at "!") (forward-char 1)))
1468 (if (< vip-expert-level 3)
1469 (save-buffers-kill-emacs)
1470 (kill-buffer (current-buffer))))
1476 (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
1479 (vip-add-newline-at-eob-if-necessary)
1480 (if (not (or (bobp) (eobp))) (forward-line 1))
1481 (if (and (not ex-variant) (string= ex-file ""))
1483 (if (null buffer-file-name)
1484 (error vip-NoFileSpecified))
1485 (setq ex-file buffer-file-name)))
1488 (setq command (ex-expand-filsyms ex-file (current-buffer)))
1489 (shell-command command t))
1490 (insert-file-contents ex-file)))
1491 (ex-fixup-history vip-last-ex-prompt ex-file))
1493 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1494 (defun ex-fixup-history (&rest args)
1495 (setq vip-ex-history
1496 (cons (mapconcat 'identity args " ") (cdr vip-ex-history))))
1499 ;; Ex recover from emacs \#file\#
1500 (defun ex-recover ()
1502 (if (or ex-append ex-offset)
1503 (error "`recover': %s" vip-SpuriousText))
1504 (if (string= ex-file "")
1506 (if (null buffer-file-name)
1507 (error "This buffer isn't visiting any file"))
1508 (setq ex-file buffer-file-name))
1509 (setq ex-file (expand-file-name ex-file)))
1510 (if (and (not (string= ex-file (buffer-file-name)))
1513 (error "No write since last change \(:rec! overrides\)"))
1514 (recover-file ex-file))
1516 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1519 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1522 ;; read variable name for ex-set
1523 (defun ex-set-read-variable ()
1524 (let ((minibuffer-local-completion-map
1525 (copy-keymap minibuffer-local-completion-map))
1526 (cursor-in-echo-area t)
1529 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1530 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1531 (if (vip-set-unread-command-events
1532 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1535 (vip-set-unread-command-events ?\C-m)))
1536 (message ":set <Variable> [= <Value>]")
1537 (or batch (sit-for 2))
1539 (while (string-match "^[ \\t\\n]*$"
1541 (completing-read ":set " ex-variable-alist)))
1542 (message ":set <Variable> ")
1543 ;; if there are unread events, don't wait
1544 (or (vip-set-unread-command-events "") (sit-for 2))
1550 (let ((var (ex-set-read-variable))
1554 (auto-cmd-label "; don't touch or else...")
1555 (delete-turn-on-auto-fill-pattern
1556 "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1557 actual-lisp-cmd lisp-cmd-del-pattern
1560 (cond ((member var '("ai" "autoindent"))
1561 (setq var "vip-auto-indent"
1565 ((member var '("gai" "global-autoindent"))
1566 (kill-local-variable 'vip-auto-indent)
1567 (setq var "vip-auto-indent"
1568 set-cmd "setq-default"
1570 ((member var '("noai" "noautoindent"))
1571 (setq var "vip-auto-indent"
1574 ((member var '("gnoai" "global-noautoindent"))
1575 (kill-local-variable 'vip-auto-indent)
1576 (setq var "vip-auto-indent"
1577 set-cmd "setq-default"
1579 ((member var '("ic" "ignorecase"))
1580 (setq var "vip-case-fold-search"
1582 ((member var '("noic" "noignorecase"))
1583 (setq var "vip-case-fold-search"
1585 ((member var '("ma" "magic"))
1586 (setq var "vip-re-search"
1588 ((member var '("noma" "nomagic"))
1589 (setq var "vip-re-search"
1591 ((member var '("ro" "readonly"))
1592 (setq var "buffer-read-only"
1594 ((member var '("noro" "noreadonly"))
1595 (setq var "buffer-read-only"
1597 ((member var '("sm" "showmatch"))
1598 (setq var "blink-matching-paren"
1600 ((member var '("nosm" "noshowmatch"))
1601 (setq var "blink-matching-paren"
1603 ((member var '("ws" "wrapscan"))
1604 (setq var "vip-search-wrap-around-t"
1606 ((member var '("nows" "nowrapscan"))
1607 (setq var "vip-search-wrap-around-t"
1609 (if (eq val 0) ; value must be set by the user
1610 (let ((cursor-in-echo-area t))
1611 (message ":set %s = <Value>" var)
1612 ;; if there are unread events, don't wait
1613 (or (vip-set-unread-command-events "") (sit-for 2))
1614 (setq val (read-string (format ":set %s = " var)))
1615 (ex-fixup-history "set" orig-var val)
1617 ;; check numerical values
1621 "gts" "global-tabstop"
1624 (or (numberp (setq val2 (car (read-from-string val))))
1625 (error "%s: Invalid value, numberp, %S" var val))
1627 (error "%s: Invalid value, numberp, %S" var val))))
1630 ((member var '("sw" "shiftwidth"))
1631 (setq var "vip-shift-width"))
1632 ((member var '("ts" "tabstop"))
1633 ;; make it take effect in curr buff and new bufs
1634 (setq var "tab-width"
1637 ((member var '("gts" "global-tabstop"))
1638 (kill-local-variable 'tab-width)
1639 (setq var "tab-width"
1640 set-cmd "setq-default"))
1641 ((member var '("wm" "wrapmargin"))
1642 ;; make it take effect in curr buff and new bufs
1643 (kill-local-variable 'fill-column)
1644 (setq var "fill-column"
1645 val (format "(- (window-width) %s)" val)
1646 set-cmd "setq-default"))
1647 ((member var '("sh" "shell"))
1648 (setq var "explicit-shell-file-name"
1649 val (format "\"%s\"" val)))))
1650 (ex-fixup-history "set" orig-var))
1652 (setq actual-lisp-cmd (format "\n(%s %s %s) %s"
1653 set-cmd var val auto-cmd-label))
1654 (setq lisp-cmd-del-pattern
1655 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1656 set-cmd var auto-cmd-label))
1658 (if (and ask-if-save
1659 (y-or-n-p (format "Do you want to save this setting in %s "
1660 vip-custom-file-name)))
1662 (vip-save-string-in-file
1663 actual-lisp-cmd vip-custom-file-name
1665 lisp-cmd-del-pattern)
1666 (if (string= var "fill-column")
1668 (vip-save-string-in-file
1670 "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
1672 vip-custom-file-name
1673 delete-turn-on-auto-fill-pattern)
1674 (vip-save-string-in-file
1675 nil vip-custom-file-name delete-turn-on-auto-fill-pattern)
1676 (vip-save-string-in-file
1677 nil vip-custom-file-name
1679 lisp-cmd-del-pattern)
1683 (message "%s %s %s" set-cmd var (if (string-match "^[ \t]*$" val)
1686 (eval (car (read-from-string actual-lisp-cmd)))
1687 (if (string= var "fill-column")
1690 (auto-fill-mode -1)))
1694 ;; In inline args, skip regex-forw and (optionally) chars-back.
1695 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1697 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1699 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
1700 (set-buffer vip-ex-work-buf)
1701 (goto-char (point-min))
1702 (re-search-forward regex-forw nil t)
1705 (goto-char (point-max))
1707 (skip-chars-backward chars-back)
1708 (skip-chars-backward " \t\n\C-m"))
1710 ;; replace SPC with `=' to suppress the special meaning SPC has
1714 (while (re-search-forward " +" nil t)
1715 (replace-match replace-str nil t)
1716 (vip-forward-char-carefully)))
1718 (buffer-substring beg end))))
1725 ;; Viper help. Invokes Info
1729 (pop-to-buffer (get-buffer-create "*info*"))
1730 (info (if vip-xemacs-p "viper.info" "viper"))
1731 (message "Type `i' to search for a specific topic"))
1733 (with-output-to-temp-buffer " *vip-info*"
1735 The Info file for Viper does not seem to be installed.
1737 This file is part of the standard distribution of %sEmacs.
1738 Please contact your system administrator. "
1739 (if vip-xemacs-p "X" "")
1742 ;; Ex source command. Loads the file specified as argument or `~/.vip'
1745 (if (string= ex-file "")
1746 (load vip-custom-file-name)
1749 ;; Ex substitute command
1750 ;; If REPEAT use previous regexp which is ex-reg-exp or vip-s-string
1751 (defun ex-substitute (&optional repeat r-flag)
1755 (case-fold-search vip-case-fold-search)
1757 (if repeat (setq ex-token nil) (setq delim (vip-get-ex-pat)))
1760 (setq pat (if r-flag vip-s-string ex-reg-exp))
1762 (error "No previous pattern to use in substitution"))
1764 delim (string-to-char pat)))
1765 (setq pat (if (string= ex-token "") vip-s-string ex-token))
1766 (setq vip-s-string pat
1768 (setq delim (vip-get-ex-pat))
1774 (while (vip-get-ex-opt-gc delim)
1775 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1779 (if ex-addresses (goto-char (car ex-addresses)))
1781 (forward-line (1- ex-count))
1782 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1783 (if (null ex-addresses)
1784 (setq ex-addresses (cons (point) (cons (point) nil)))
1785 (if (null (cdr ex-addresses))
1786 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1788 (let ((beg (car ex-addresses))
1789 (end (car (cdr ex-addresses)))
1792 (vip-enlarge-region beg end)
1793 (let ((limit (save-excursion
1794 (goto-char (max (point) (mark t)))
1796 (goto-char (min (point) (mark t)))
1797 (while (< (point) limit)
1799 (setq eol-mark (point-marker))
1803 (while (and (not (eolp))
1804 (re-search-forward pat eol-mark t))
1805 (if (or (not opt-c) (y-or-n-p "Replace? "))
1807 (setq matched-pos (point))
1808 (if (not (stringp repl))
1809 (error "Can't perform Ex substitution: No previous replacement pattern"))
1810 (replace-match repl t))))
1812 (vip-forward-char-carefully))
1815 "Can't repeat Ex substitution: No previous regular expression"))
1816 (if (and (re-search-forward pat eol-mark t)
1817 (or (not opt-c) (y-or-n-p "Replace? ")))
1819 (setq matched-pos (point))
1820 (if (not (stringp repl))
1821 (error "Can't perform Ex substitution: No previous replacement pattern"))
1822 (replace-match repl t)))
1824 (vip-forward-char-carefully))))))
1825 (if matched-pos (goto-char matched-pos))
1827 (if opt-c (message "done"))))
1832 (save-window-excursion
1833 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
1834 (set-buffer vip-ex-work-buf)
1835 (skip-chars-forward " \t")
1837 (skip-chars-forward "^ |\t\n")
1838 (setq tag (buffer-substring (mark t) (point))))
1839 (if (not (string= tag "")) (setq ex-tag tag))
1840 (vip-change-state-to-emacs)
1841 (condition-case conds
1843 (if (string= tag "")
1845 (find-tag-other-window ex-tag))
1846 (vip-change-state-to-vi))
1848 (vip-change-state-to-vi)
1849 (vip-message-conditions conds)))))
1852 (defun ex-write (q-flag)
1853 (vip-default-ex-addresses t)
1855 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))
1856 temp-buf writing-same-file region
1857 file-exists writing-whole-file)
1858 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1861 (vip-enlarge-region beg end)
1862 (shell-command-on-region (point) (mark t) ex-file))
1863 (if (and (string= ex-file "") (not (buffer-file-name)))
1866 (format "Buffer %s isn't visiting any file. File to save in: "
1869 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
1870 ex-file (if (string= ex-file "")
1872 (expand-file-name ex-file)))
1873 ;; if ex-file is a directory use the file portion of the buffer file name
1874 (if (and (file-directory-p ex-file)
1876 (not (file-directory-p buffer-file-name)))
1878 (concat ex-file (file-name-nondirectory buffer-file-name))))
1880 (setq file-exists (file-exists-p ex-file)
1881 writing-same-file (string= ex-file (buffer-file-name)))
1883 (if (and writing-whole-file writing-same-file)
1884 (if (not (buffer-modified-p))
1885 (message "(No changes need to be saved)")
1887 (ex-write-info file-exists ex-file beg end))
1888 ;; writing some other file or portion of the currents
1889 ;; file---create temp buffer for it
1890 ;; disable undo in that buffer, for efficiency
1891 (buffer-disable-undo (setq temp-buf (create-file-buffer ex-file)))
1894 (if (and file-exists
1895 (not writing-same-file)
1897 (format "File %s exists. Overwrite? " ex-file))))
1899 (vip-enlarge-region beg end)
1900 (setq region (buffer-substring (point) (mark t)))
1901 (set-buffer temp-buf)
1902 (set-visited-file-name ex-file)
1904 (if (and file-exists ex-append)
1905 (insert-file-contents ex-file))
1906 (goto-char (point-max))
1909 (ex-write-info file-exists ex-file (point-min) (point-max))
1911 (set-buffer temp-buf)
1912 (set-buffer-modified-p nil)
1913 (kill-buffer temp-buf)
1916 ;; this prevents the loss of data if writing part of the buffer
1917 (if (and (buffer-file-name) writing-same-file)
1918 (set-visited-file-modtime))
1919 (or writing-whole-file
1920 (not writing-same-file)
1921 (set-buffer-modified-p t))
1923 (if (< vip-expert-level 2)
1924 (save-buffers-kill-emacs)
1925 (kill-buffer (current-buffer))))
1929 (defun ex-write-info (exists file-name beg end)
1930 (message "`%s'%s %d lines, %d characters"
1931 (vip-abbreviate-file-name file-name)
1932 (if exists "" " [New file]")
1933 (count-lines beg (min (1+ end) (point-max)))
1938 (vip-default-ex-addresses)
1940 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1941 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1943 (vip-enlarge-region beg end)
1944 (exchange-point-and-mark)
1945 (if (or ex-g-flag ex-g-variant)
1946 (error "Can't execute `yank' within `global'"))
1950 (forward-line (1- ex-count)))
1952 (vip-enlarge-region (point) (mark t))
1953 (if ex-flag (error "`yank': %s" vip-SpuriousText))
1955 (cond ((vip-valid-register ex-buffer '(Letter))
1956 (vip-append-to-register
1957 (downcase ex-buffer) (point) (mark t)))
1958 ((vip-valid-register ex-buffer)
1959 (copy-to-register ex-buffer (point) (mark t) nil))
1960 (t (error vip-InvalidRegister ex-buffer))))
1961 (copy-region-as-kill (point) (mark t)))))
1963 ;; Execute shell command
1964 (defun ex-command ()
1966 (save-window-excursion
1967 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
1968 (set-buffer vip-ex-work-buf)
1969 (skip-chars-forward " \t")
1970 (setq command (buffer-substring (point) (point-max)))
1972 (setq command (ex-expand-filsyms command (current-buffer)))
1973 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
1974 (if vip-ex-last-shell-com
1975 (setq command (concat vip-ex-last-shell-com (substring command 1)))
1976 (error "No previous shell command")))
1977 (setq vip-ex-last-shell-com command)
1978 (if (null ex-addresses)
1979 (shell-command command)
1980 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1981 (if (null beg) (setq beg end))
1985 (vip-enlarge-region (point) (mark t))
1986 (shell-command-on-region (point) (mark t) command t))
1989 ;; Print line number
1990 (defun ex-line-no ()
1994 (if (null ex-addresses) (point-max) (car ex-addresses))))))
1996 ;; Give information on the file visited by the current buffer
1997 (defun vip-info-on-file ()
1999 (let ((pos1 (vip-line-pos 'start))
2000 (pos2 (vip-line-pos 'end))
2002 (setq lines (count-lines (point-min) (vip-line-pos 'end))
2003 file (if (buffer-file-name)
2004 (concat (vip-abbreviate-file-name (buffer-file-name)) ":")
2005 (concat (buffer-name) " [Not visiting any file]:"))
2006 info (format "line=%d/%d pos=%d/%d col=%d %s"
2010 (count-lines (point-min) (point-max))
2011 (point) (1- (point-max))
2012 (1+ (current-column))
2013 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2014 (if (< (+ 1 (length info) (length file))
2015 (window-width (minibuffer-window)))
2016 (message (concat file " " info))
2017 (save-window-excursion
2018 (with-output-to-temp-buffer " *vip-info*"
2021 "\n\n\nPress any key to continue...\n\n")))
2023 (kill-buffer " *vip-info*")))
2029 ;;; viper-ex.el ends here