1 ;;; viper-ex.el -- functions implementing the Ex commands for Viper
3 ;; This file is part of GNU Emacs.
5 ;; GNU Emacs is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with GNU Emacs; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 (defconst vip-ex-work-buf-name " *ex-working-space*")
25 (defconst vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
28 ;;; Variable completion in :set command
30 ;; The list of Ex commands. Used for completing command names.
31 (defconst ex-token-alist
32 '(("!") ("=") (">") ("&") ("~")
33 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
34 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
35 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
36 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
37 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
38 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
39 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
40 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
42 ;; A-list of Ex variables that can be set using the :set command.
43 (defconst ex-variable-alist
44 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
45 ("tab-stop-local") ("tsl") ("tabstop") ("ts")
46 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
48 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
49 ("noreadonly") ("noro") ("nomagic") ("noma")
50 ("noignorecase") ("noic") ("noautoindent") ("noai")
51 ("magic") ("ma") ("ignorecase") ("ic") ("autoindent") ("ai")
56 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
60 ;; If non-nil, gives type of address; if nil, it is a command.
61 (defvar ex-token-type nil)
63 ;; List of addresses passed to Ex command
64 (defvar ex-addresses nil)
66 ;; It seems that this flag is used only for `#', `print', and `list', which
67 ;; aren't implemented. Check later.
70 ;; "buffer" where Ex commands keep deleted data.
71 ;; In Emacs terms, this is a register.
72 (defvar ex-buffer nil)
77 ;; Flag for global command.
78 (defvar ex-g-flag nil)
80 ;; If t, global command is executed on lines not matching ex-g-pat.
81 (defvar ex-g-variant nil)
83 ;; Save reg-exp used in substitute.
84 (defvar ex-reg-exp nil)
87 ;; Replace pattern for substitute.
90 ;; Pattern for global command.
93 ;; `sh' doesn't seem to expand wildcards, like `*'
94 (defconst ex-find-file-shell "csh"
95 "Shell in which to interpret wildcards.")
96 (defvar ex-find-file-shell-options "-f"
97 "*Options to pass to `ex-find-file-shell'.")
99 ;; Remembers the previous Ex tag.
102 ;; file used by Ex commands like :r, :w, :n
105 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
106 (defvar ex-variant nil)
108 ;; Specified the offset of an Ex command, such as :read.
109 (defvar ex-offset nil)
111 ;; Tells Ex that this is a w>> command.
112 (defvar ex-append nil)
114 ;; File containing the shell command to be executed at Ex prompt,
116 (defvar ex-cmdfile nil)
118 ;; flag used in vip-ex-read-file-name to indicate that we may be reading
119 ;; multiple file names. Used for :edit and :next
120 (defvar vip-keep-reading-filename nil)
122 (defconst ex-cycle-other-window t
123 "*If t, :n and :b cycles through files and buffers in other window.
124 Then :N and :B cycles in the current window. If nil, this behavior is
127 (defconst ex-cycle-through-non-files nil
128 "*Cycle through *scratch* and other buffers that don't visit any file.")
130 ;; Last shell command executed with :! command.
131 (defvar vip-ex-last-shell-com nil)
133 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
134 (defvar vip-incomplete-ex-cmd nil)
136 ;; Remembers the last ex-command prompt.
137 (defvar vip-last-ex-prompt "")
142 (defun vip-check-sub (str)
143 "Check if ex-token is an initial segment of STR."
144 (let ((length (length ex-token)))
145 (if (and (<= length (length str))
146 (string= ex-token (substring str 0 length)))
148 (setq ex-token-type 'non-command))))
150 (defun vip-get-ex-com-subr ()
151 "Get a complete ex command."
152 (let (case-fold-search)
154 (re-search-forward "[a-zA-Z][a-zA-Z]*")
155 (setq ex-token-type 'command)
156 (setq ex-token (buffer-substring (point) (mark t)))
157 (exchange-point-and-mark)
158 (cond ((looking-at "a")
159 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
160 ((looking-at "ar") (vip-check-sub "args"))
161 (t (vip-check-sub "append"))))
162 ((looking-at "h") (vip-check-sub "help"))
164 (cond ((looking-at "cd") (vip-check-sub "cd"))
165 ((looking-at "ch") (vip-check-sub "chdir"))
166 ((looking-at "co") (vip-check-sub "copy"))
167 (t (vip-check-sub "change"))))
168 ((looking-at "d") (vip-check-sub "delete"))
169 ((looking-at "b") (vip-check-sub "buffer"))
170 ((looking-at "B") (vip-check-sub "Buffer"))
172 (if (looking-at "ex") (vip-check-sub "ex")
173 (vip-check-sub "edit")))
174 ((looking-at "f") (vip-check-sub "file"))
175 ((looking-at "g") (vip-check-sub "global"))
176 ((looking-at "i") (vip-check-sub "insert"))
177 ((looking-at "j") (vip-check-sub "join"))
178 ((looking-at "l") (vip-check-sub "list"))
180 (cond ((looking-at "map") (vip-check-sub "map"))
181 ((looking-at "mar") (vip-check-sub "mark"))
182 (t (vip-check-sub "move"))))
183 ((looking-at "k[a-z][^a-z]")
184 (setq ex-token "kmark")
186 (exchange-point-and-mark)) ; this is canceled out by another
187 ; exchange-point-and-mark at the end
188 ((looking-at "k") (vip-check-sub "kmark"))
189 ((looking-at "n") (if (looking-at "nu")
190 (vip-check-sub "number")
191 (vip-check-sub "next")))
192 ((looking-at "N") (vip-check-sub "Next"))
193 ((looking-at "o") (vip-check-sub "open"))
195 (cond ((looking-at "pre") (vip-check-sub "preserve"))
196 ((looking-at "pu") (vip-check-sub "put"))
197 ((looking-at "pw") (vip-check-sub "pwd"))
198 (t (vip-check-sub "print"))))
199 ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
200 ((looking-at "R") (vip-check-sub "RelatedFile"))
201 ((looking-at "q") (vip-check-sub "quit"))
203 (cond ((looking-at "rec") (vip-check-sub "recover"))
204 ((looking-at "rew") (vip-check-sub "rewind"))
205 (t (vip-check-sub "read"))))
207 (cond ((looking-at "se") (vip-check-sub "set"))
208 ((looking-at "sh") (vip-check-sub "shell"))
209 ((looking-at "so") (vip-check-sub "source"))
210 ((looking-at "sr") (vip-check-sub "sr"))
211 ((looking-at "st") (vip-check-sub "stop"))
212 ((looking-at "sus") (vip-check-sub "suspend"))
213 ((looking-at "subm") (vip-check-sub "submitReport"))
214 (t (vip-check-sub "substitute"))))
216 (if (looking-at "ta") (vip-check-sub "tag")
217 (vip-check-sub "transfer")))
219 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
220 ((looking-at "unm") (vip-check-sub "unmap"))
221 (t (vip-check-sub "undo"))))
223 (cond ((looking-at "ve") (vip-check-sub "version"))
224 ((looking-at "vi") (vip-check-sub "visual"))
225 (t (vip-check-sub "vglobal"))))
227 (if (looking-at "wq") (vip-check-sub "wq")
228 (vip-check-sub "write")))
230 (if (looking-at "WW")
231 (vip-check-sub "WWrite")
232 (vip-check-sub "Write")))
233 ((looking-at "x") (vip-check-sub "xit"))
234 ((looking-at "y") (vip-check-sub "yank"))
235 ((looking-at "z") (vip-check-sub "z")))
236 (exchange-point-and-mark)
239 (defun vip-get-ex-token ()
240 "Get an ex-token which is either an address or a command.
241 A token has a type, \(command, address, end-mark\), and a value."
242 (save-window-excursion
243 (set-buffer vip-ex-work-buf)
244 (skip-chars-forward " \t|")
245 (cond ((looking-at "#")
246 (setq ex-token-type 'command)
247 (setq ex-token (char-to-string (following-char)))
249 ((looking-at "[a-z]") (vip-get-ex-com-subr))
252 (setq ex-token-type 'dot))
253 ((looking-at "[0-9]")
255 (re-search-forward "[0-9]*")
257 (cond ((eq ex-token-type 'plus) 'add-number)
258 ((eq ex-token-type 'minus) 'sub-number)
260 (setq ex-token (string-to-int (buffer-substring (point) (mark t)))))
263 (setq ex-token-type 'end))
266 (setq ex-token-type 'whole))
268 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
272 (setq ex-token-type 'plus))
273 ((looking-at "+[0-9]")
275 (setq ex-token-type 'plus))
277 (error vip-BadAddress))))
279 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
283 (setq ex-token-type 'minus))
284 ((looking-at "-[0-9]")
286 (setq ex-token-type 'minus))
288 (error vip-BadAddress))))
293 (while (and (not (eolp)) cont)
294 ;;(re-search-forward "[^/]*/")
295 (re-search-forward "[^/]*\\(/\\|\n\\)")
296 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
299 (setq ex-token (buffer-substring (point) (mark t)))
300 (if (looking-at "/") (forward-char 1))
301 (setq ex-token-type 'search-forward))
306 (while (and (not (eolp)) cont)
307 ;;(re-search-forward "[^\\?]*\\?")
308 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
309 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
312 (if (not (looking-at "\n")) (forward-char 1))))
313 (setq ex-token-type 'search-backward)
314 (setq ex-token (buffer-substring (1- (point)) (mark t))))
317 (setq ex-token-type 'comma))
320 (setq ex-token-type 'semi-colon))
321 ((looking-at "[!=><&~]")
322 (setq ex-token-type 'command)
323 (setq ex-token (char-to-string (following-char)))
326 (setq ex-token-type 'goto-mark)
328 (cond ((looking-at "'") (setq ex-token nil))
329 ((looking-at "[a-z]") (setq ex-token (following-char)))
330 (t (error "Marks are ' and a-z")))
333 (setq ex-token-type 'end-mark)
334 (setq ex-token "goto"))
336 (error vip-BadExCommand)))))
338 ;; Reads Ex command. Tries to determine if it has to exit because command
339 ;; is complete or invalid. If not, keeps reading command.
340 (defun ex-cmd-read-exit ()
342 (setq vip-incomplete-ex-cmd t)
343 (let ((quit-regex1 (concat
345 "set[ \t]*" "\\|" "edit[ \t]*" "\\|" "[nN]ext[ \t]*"
346 "\\|" "unm[ \t]*" "\\|" "^[ \t]*rep"
356 "^[ \t]*$" "\\|" "[ktgjmsz][ \t]*$" "\\|" "^[ \t]*ab.*"
357 "\\|" "tr[ansfer \t]*" "\\|" "sr[ \t]*"
358 "\\|" "mo.*" "\\|" "^[ \t]*k?ma[^p]*"
359 "\\|" "^[ \t]*fi.*" "\\|" "v?gl.*" "\\|" "[vg][ \t]*$"
360 "\\|" "jo.*" "\\|" "^[ \t]*ta.*" "\\|" "^[ \t]*una.*"
361 "\\|" "^[ \t]*su.*" "\\|['`][a-z][ \t]*"
362 "\\|" "![ \t]*[a-zA-Z].*"
366 (save-window-excursion ;; put cursor at the end of the Ex working buffer
367 (set-buffer vip-ex-work-buf)
368 (goto-char (point-max)))
369 (cond ((vip-looking-back quit-regex1) (exit-minibuffer))
370 ((vip-looking-back stay-regex) (insert " "))
371 ((vip-looking-back quit-regex2) (exit-minibuffer))
374 ;; complete Ex command
375 (defun ex-cmd-complete ()
377 (let (save-pos dist compl-list string-to-complete completion-result)
380 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
384 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
386 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
387 ;; Preceding characters are not the ones allowed in an Ex command
388 ;; or we have typed past command name.
389 ;; Note: we didn't do parsing, so there may be surprises.
390 (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
391 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
392 (looking-at "[^ \t\n\C-m]"))
394 (with-output-to-temp-buffer "*Completions*"
395 (display-completion-list
396 (vip-alist-to-list ex-token-alist))))
397 ;; Preceding chars may be part of a command name
398 (setq string-to-complete (buffer-substring save-pos (point)))
399 (setq completion-result
400 (try-completion string-to-complete ex-token-alist))
402 (cond ((eq completion-result t) ; exact match--do nothing
403 (vip-tmp-insert-at-eob " (Sole completion)"))
404 ((eq completion-result nil)
405 (vip-tmp-insert-at-eob " (No match)"))
406 (t ;; partial completion
408 (delete-region (point) (point-max))
409 (insert completion-result)
410 (let (case-fold-search)
412 (vip-filter-alist (concat "^" completion-result)
414 (if (> (length compl-list) 1)
415 (with-output-to-temp-buffer "*Completions*"
416 (display-completion-list
417 (vip-alist-to-list (reverse compl-list)))))))
420 (defun vip-ex (&optional string)
421 "Ex commands within Viper."
426 (let* ((map (copy-keymap minibuffer-local-map))
432 (vip-add-keymap vip-ex-cmd-map map)
434 (setq com-str (or string (vip-read-string-with-history
440 (save-window-excursion
442 (or (vip-buffer-live-p vip-ex-work-buf)
443 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name)))
444 (set-buffer vip-ex-work-buf)
445 (delete-region (point-min) (point-max))
446 (insert com-str "\n")
447 (goto-char (point-min)))
448 (setq ex-token-type nil
452 (cond ((memq ex-token-type '(command end-mark))
453 (if address (setq ex-addresses (cons address ex-addresses)))
454 (cond ((string= ex-token "global")
457 ((string= ex-token "vglobal")
461 (vip-execute-ex-command)
462 (save-window-excursion
463 (set-buffer vip-ex-work-buf)
464 (skip-chars-forward " \t")
465 (cond ((looking-at "|")
469 (t (error "`%s': %s" ex-token vip-SpuriousText)))
472 ((eq ex-token-type 'non-command)
473 (error (format "`%s': %s" ex-token vip-BadExCommand)))
474 ((eq ex-token-type 'whole)
476 (cons (point-max) (cons (point-min) ex-addresses))))
477 ((eq ex-token-type 'comma)
479 (cons (if (null address) (point) address) ex-addresses)))
480 ((eq ex-token-type 'semi-colon)
481 (if address (setq dot address))
483 (cons (if (null address) (point) address) ex-addresses)))
484 (t (let ((ans (vip-get-ex-address-subr address dot)))
485 (if ans (setq address ans))))))))
487 (defun vip-get-ex-pat ()
488 "Get a regular expression and set `ex-variant', if found."
489 (save-window-excursion
490 (set-buffer vip-ex-work-buf)
491 (skip-chars-forward " \t")
494 (setq ex-g-variant (not ex-g-variant)
495 ex-g-flag (not ex-g-flag))
497 (skip-chars-forward " \t")))
498 (let ((c (following-char)))
499 (if (string-match "[0-9A-Za-z]" (format "%c" c))
501 "Global regexp must be inside matching non-alphanumeric chars"))
502 (if (looking-at "[^\\\\\n]")
507 (while (and (not (eolp)) cont)
508 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
509 (if (member ex-token '("global" "vglobal"))
511 "Missing closing delimiter for global regexp")
512 (goto-char (point-max))))
513 (if (not (vip-looking-back
514 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
517 (if (= (mark t) (point)) ""
518 (buffer-substring (1- (point)) (mark t))))
523 (defun vip-get-ex-command ()
525 (save-window-excursion
526 (set-buffer vip-ex-work-buf)
527 (if (looking-at "/") (forward-char 1))
528 (skip-chars-forward " \t")
529 (cond ((looking-at "[a-z]")
530 (vip-get-ex-com-subr)
531 (if (eq ex-token-type 'non-command)
532 (error "`%s': %s" ex-token vip-BadExCommand)))
533 ((looking-at "[!=><&~]")
534 (setq ex-token (char-to-string (following-char)))
536 (t (error vip-BadExCommand)))))
538 (defun vip-get-ex-opt-gc (c)
539 "Get an Ex option g or c."
540 (save-window-excursion
541 (set-buffer vip-ex-work-buf)
542 (if (looking-at (format "%c" c)) (forward-char 1))
543 (skip-chars-forward " \t")
544 (cond ((looking-at "g")
554 (defun vip-default-ex-addresses (&optional whole-flag)
555 "Compute default addresses. WHOLE-FLAG means use the whole buffer."
556 (cond ((null ex-addresses)
559 (cons (point-max) (cons (point-min) nil))
560 (cons (point) (cons (point) nil)))))
561 ((null (cdr ex-addresses))
563 (cons (car ex-addresses) ex-addresses)))))
565 (defun vip-get-ex-address ()
566 "Get an ex-address as a marker and set ex-flag if a flag is found."
567 (let ((address (point-marker)) (cont t))
572 (cond ((eq ex-token-type 'command)
573 (if (member ex-token '("print" "list" "#"))
577 (error "Address expected in this Ex command")))
578 ((eq ex-token-type 'end-mark)
580 ((eq ex-token-type 'whole)
581 (error "Trailing address expected"))
582 ((eq ex-token-type 'comma)
583 (error "`%s': %s" ex-token vip-SpuriousText))
584 (t (let ((ans (vip-get-ex-address-subr address (point-marker))))
585 (if ans (setq address ans))))))
588 (defun vip-get-ex-address-subr (old-address dot)
589 "Returns an address as a point."
591 (if (null old-address) (setq old-address dot))
592 (cond ((eq ex-token-type 'dot)
594 ((eq ex-token-type 'add-number)
596 (goto-char old-address)
597 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
598 (setq address (point-marker))))
599 ((eq ex-token-type 'sub-number)
601 (goto-char old-address)
602 (forward-line (- ex-token))
603 (setq address (point-marker))))
604 ((eq ex-token-type 'abs-number)
606 (goto-char (point-min))
607 (if (= ex-token 0) (setq address 0)
608 (forward-line (1- ex-token))
609 (setq address (point-marker)))))
610 ((eq ex-token-type 'end)
611 (setq address (point-max-marker)))
612 ((eq ex-token-type 'plus) t) ; do nothing
613 ((eq ex-token-type 'minus) t) ; do nothing
614 ((eq ex-token-type 'search-forward)
616 (ex-search-address t)
617 (setq address (point-marker))))
618 ((eq ex-token-type 'search-backward)
620 (ex-search-address nil)
621 (setq address (point-marker))))
622 ((eq ex-token-type 'goto-mark)
625 (exchange-point-and-mark)
626 (goto-char (vip-register-to-point
627 (1+ (- ex-token ?a)) 'enforce-buffer)))
628 (setq address (point-marker)))))
632 (defun ex-search-address (forward)
633 "Search pattern and set address."
634 (if (string= ex-token "")
635 (if (null vip-s-string)
636 (error vip-NoPrevSearch)
637 (setq ex-token vip-s-string))
638 (setq vip-s-string ex-token))
642 (re-search-forward ex-token))
644 (re-search-backward ex-token)))
646 (defun vip-get-ex-buffer ()
647 "Get a buffer name and set `ex-count' and `ex-flag' if found."
651 (save-window-excursion
652 (set-buffer vip-ex-work-buf)
653 (skip-chars-forward " \t")
654 (if (looking-at "[a-zA-Z]")
656 (setq ex-buffer (following-char))
658 (skip-chars-forward " \t")))
659 (if (looking-at "[0-9]")
662 (re-search-forward "[0-9][0-9]*")
663 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
664 (skip-chars-forward " \t")))
665 (if (looking-at "[pl#]")
669 (if (not (looking-at "[\n|]"))
670 (error "`%s': %s" ex-token vip-SpuriousText))))
672 (defun vip-get-ex-count ()
676 (save-window-excursion
677 (set-buffer vip-ex-work-buf)
678 (skip-chars-forward " \t")
683 (skip-chars-forward " \t")
684 (if (looking-at "[0-9]")
687 (re-search-forward "[0-9][0-9]*")
688 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
689 (skip-chars-forward " \t")))
690 (if (looking-at "[pl#]")
694 (if (not (looking-at "[\n|]"))
696 (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand))))
698 (defun ex-expand-filsyms (cmd buf)
699 "Expand \% and \# in ex command."
703 (setq cf buffer-file-name)
704 (setq pf (ex-next nil t))) ; this finds alternative file name
705 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
706 (error "No current file to substitute for `\%'"))
707 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
708 (error "No alternate file to substitute for `#'"))
710 (set-buffer (get-buffer-create " *ex-tmp*"))
713 (goto-char (point-min))
714 (while (re-search-forward "%\\|#" nil t)
715 (let ((data (match-data))
716 (char (buffer-substring (match-beginning 0) (match-end 0))))
717 (if (vip-looking-back (concat "\\\\" char))
719 (store-match-data data)
720 (if (string= char "%")
722 (replace-match pf)))))
724 (setq ret (buffer-substring (point-min) (point)))
728 (defun vip-get-ex-file ()
729 "Get a file name and set ex-variant, `ex-append' and `ex-offset' if found."
737 (save-window-excursion
738 (set-buffer vip-ex-work-buf)
739 (skip-chars-forward " \t")
741 (if (not (vip-looking-back "[ \t]"))
745 (skip-chars-forward " \t"))
748 (skip-chars-forward " \t")))
749 (if (looking-at ">>")
754 (skip-chars-forward " \t")))
759 (re-search-forward "[ \t\n]")
761 (setq ex-offset (buffer-substring (point) (mark t)))
763 (skip-chars-forward " \t")))
764 ;; this takes care of :r, :w, etc., when they get file names
765 ;; from the history list
766 (if (member ex-token '("read" "write" "edit" "visual" "next"))
768 (setq ex-file (buffer-substring (point) (1- (point-max))))
770 ;; For :e, match multiple non-white strings separated
771 ;; by white. For others, find the first non-white string
773 (if (string= ex-token "edit")
774 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
778 ;; if file name comes from history, don't leave
779 ;; minibuffer when the user types space
780 (setq vip-incomplete-ex-cmd nil)
781 ;; this must be the last clause in this progn
782 (substring ex-file (match-beginning 0) (match-end 0))
785 ;; this leaves only the command name in the work area
786 ;; file names are gone
787 (delete-region (point) (1- (point-max)))
789 (goto-char (point-max))
790 (skip-chars-backward " \t\n")
791 (setq prompt (buffer-substring (point-min) (point)))
794 (setq vip-last-ex-prompt prompt)
796 ;; If we just finished reading command, redisplay prompt
797 (if vip-incomplete-ex-cmd
798 (setq ex-file (vip-ex-read-file-name (format ":%s " prompt)))
799 ;; file was typed in-line
800 (setq ex-file (or ex-file "")))
804 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
805 ;; file names, arranges to re-enter the minibuffer.
806 (defun vip-complete-filename-or-exit ()
808 (setq vip-keep-reading-filename t)
809 ;; don't exit if directory---ex-commands don't
810 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
811 (t (minibuffer-complete-word))))
814 (defun ex-cmd-accepts-multiple-files-p (token)
815 (member token '("edit" "next" "Next")))
817 ;; If user doesn't enter anything, then "" is returned, i.e., the
818 ;; prompt-directory is not returned.
819 (defun vip-ex-read-file-name (prompt)
821 (minibuffer-local-completion-map
822 (copy-keymap minibuffer-local-completion-map))
825 (vip-add-keymap ex-read-filename-map minibuffer-local-completion-map)
827 (setq cont (setq vip-keep-reading-filename t))
829 (setq vip-keep-reading-filename nil
830 val (read-file-name (concat prompt str) nil default-directory)
831 str (concat str (if (equal val "") "" " ")
832 val (if (equal val "") "" " ")))
834 ;; Only edit, next, and Next commands accept multiple files.
835 ;; vip-keep-reading-filename is set in the anonymous function that is
836 ;; bound to " " in ex-read-filename-map.
837 (setq cont (and vip-keep-reading-filename
838 (ex-cmd-accepts-multiple-files-p ex-token)))
841 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
842 end (string-match "[ \t]*$" str)) ; delete trailing blanks
843 (if (member ex-token '("read" "write"))
844 (if (string-match "[\t ]*!" str)
845 ;; this is actually a shell command
849 (setq vip-last-ex-prompt (concat vip-last-ex-prompt " !")))))
850 (substring str (or beg 0) end)))
852 (defun vip-execute-ex-command ()
853 "Execute ex command using the value of addresses."
854 (vip-deactivate-mark)
855 (cond ((string= ex-token "args") (ex-args))
856 ((string= ex-token "copy") (ex-copy nil))
857 ((string= ex-token "cd") (ex-cd))
858 ((string= ex-token "chdir") (ex-cd))
859 ((string= ex-token "delete") (ex-delete))
860 ((string= ex-token "edit") (ex-edit))
861 ((string= ex-token "file") (vip-info-on-file))
862 ((string= ex-token "goto") (ex-goto))
863 ((string= ex-token "help") (ex-help))
864 ((string= ex-token "join") (ex-line "join"))
865 ((string= ex-token "kmark") (ex-mark))
866 ((string= ex-token "mark") (ex-mark))
867 ((string= ex-token "map") (ex-map))
868 ((string= ex-token "move") (ex-copy t))
869 ((string= ex-token "next") (ex-next ex-cycle-other-window))
870 ((string= ex-token "Next") (ex-next (not ex-cycle-other-window)))
871 ((string= ex-token "RelatedFile") (ex-next-related-buffer 1))
872 ((string= ex-token "put") (ex-put))
873 ((string= ex-token "pwd") (ex-pwd))
874 ((string= ex-token "preserve") (ex-preserve))
875 ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1))
876 ((string= ex-token "quit") (ex-quit))
877 ((string= ex-token "read") (ex-read))
878 ((string= ex-token "recover") (ex-recover))
879 ((string= ex-token "rewind") (ex-rewind))
880 ((string= ex-token "submitReport") (vip-submit-report))
881 ((string= ex-token "set") (ex-set))
882 ((string= ex-token "shell") (ex-shell))
883 ((string= ex-token "source") (ex-source))
884 ((string= ex-token "sr") (ex-substitute t t))
885 ((string= ex-token "substitute") (ex-substitute))
886 ((string= ex-token "suspend") (suspend-emacs))
887 ((string= ex-token "stop") (suspend-emacs))
888 ((string= ex-token "transfer") (ex-copy nil))
889 ((string= ex-token "buffer") (if ex-cycle-other-window
890 (vip-switch-to-buffer-other-window)
891 (vip-switch-to-buffer)))
892 ((string= ex-token "Buffer") (if ex-cycle-other-window
893 (vip-switch-to-buffer)
894 (vip-switch-to-buffer-other-window)))
895 ((string= ex-token "tag") (ex-tag))
896 ((string= ex-token "undo") (vip-undo))
897 ((string= ex-token "unmap") (ex-unmap))
898 ((string= ex-token "version") (vip-version))
899 ((string= ex-token "visual") (ex-edit))
900 ((string= ex-token "write") (ex-write nil))
901 ((string= ex-token "Write") (save-some-buffers))
902 ((string= ex-token "wq") (ex-write t))
903 ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask
904 ((string= ex-token "xit") (ex-write t))
905 ((string= ex-token "yank") (ex-yank))
906 ((string= ex-token "!") (ex-command))
907 ((string= ex-token "=") (ex-line-no))
908 ((string= ex-token ">") (ex-line "right"))
909 ((string= ex-token "<") (ex-line "left"))
910 ((string= ex-token "&") (ex-substitute t))
911 ((string= ex-token "~") (ex-substitute t t))
912 ((or (string= ex-token "append")
913 (string= ex-token "change")
914 (string= ex-token "insert")
915 (string= ex-token "open"))
917 (format "`%s': Obsolete command, not supported by Viper"
919 ((or (string= ex-token "abbreviate")
920 (string= ex-token "unabbreviate"))
923 "`%s': Vi's abbrevs are obsolete. Use more powerful Emacs' abbrevs"
925 ((or (string= ex-token "list")
926 (string= ex-token "print")
927 (string= ex-token "z")
928 (string= ex-token "#"))
930 (format "`%s': Command not implemented in Viper" ex-token)))
931 (t (error (format "`%s': %s" ex-token vip-BadExCommand)))))
933 (defun vip-undisplayed-files ()
937 (if (null (get-buffer-window b))
938 (let ((f (buffer-file-name b)))
940 (if ex-cycle-through-non-files
941 (let ((s (buffer-name b)))
942 (if (string= " " (substring s 0 1))
951 (let ((l (vip-undisplayed-files))
954 (while (not (null l))
956 (setq args (format "%s %d) %s\n" args file-count (car l))
957 file-count (1+ file-count)))
959 (if (string= args "")
960 (message "All files are already displayed")
962 (save-window-excursion
963 (with-output-to-temp-buffer " *vip-info*"
964 (princ "\n\nThese files are not displayed in any window.\n")
965 (princ "\n=============\n")
967 (princ "\n=============\n")
968 (princ "\nThe numbers can be given as counts to :next. ")
969 (princ "\n\nPress any key to continue...\n\n"))
970 (vip-read-event))))))
973 "Ex cd command. Default directory of this buffer changes."
975 (if (string= ex-file "")
977 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
979 (defun ex-copy (del-flag)
980 "Ex copy and move command. DEL-FLAG means delete."
981 (vip-default-ex-addresses)
982 (let ((address (vip-get-ex-address))
983 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
987 (vip-enlarge-region (mark t) (point))
989 (kill-region (point) (mark t))
990 (copy-region-as-kill (point) (mark t)))
993 (with-output-to-temp-buffer "*copy text*"
995 (if (or del-flag ex-g-flag ex-g-variant)
997 (buffer-substring (point) (mark t)))))
1000 (read-string "[Hit return to continue] ")
1001 (save-excursion (kill-buffer "*copy text*")))
1002 (quit (save-excursion (kill-buffer "*copy text*"))
1003 (signal 'quit nil))))))
1005 (goto-char (point-min))
1008 (insert (current-kill 0))))
1011 "Ex delete command."
1012 (vip-default-ex-addresses)
1014 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1015 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1017 (vip-enlarge-region beg end)
1018 (exchange-point-and-mark)
1022 (forward-line (1- ex-count)))
1024 (vip-enlarge-region (point) (mark t))
1026 ;; show text to be deleted and ask for confirmation
1028 (with-output-to-temp-buffer " *delete text*"
1029 (princ (buffer-substring (point) (mark t))))
1031 (read-string "[Hit return to continue] ")
1033 (save-excursion (kill-buffer " *delete text*"))
1035 (save-excursion (kill-buffer " *delete text*")))
1037 (cond ((vip-valid-register ex-buffer '(Letter))
1038 (vip-append-to-register
1039 (downcase ex-buffer) (point) (mark t)))
1040 ((vip-valid-register ex-buffer)
1041 (copy-to-register ex-buffer (point) (mark t) nil))
1042 (t (error vip-InvalidRegister ex-buffer))))
1043 (kill-region (point) (mark t))))))
1047 (defun ex-edit (&optional file)
1049 In Viper, `e' and `e!' behave identically. In both cases, the user is
1050 asked if current buffer should really be discarded.
1051 This command can take multiple file names. It replaces the current buffer
1052 with the first file in its argument list."
1055 (cond ((and (string= ex-file "") buffer-file-name)
1056 (setq ex-file (abbreviate-file-name (buffer-file-name))))
1057 ((string= ex-file "")
1058 (error vip-NoFileSpecified)))
1061 (if buffer-file-name
1062 (cond ((buffer-modified-p)
1064 (format "Buffer %s is modified. Edit buffer? "
1067 ((not (verify-visited-file-modtime (current-buffer)))
1069 (format "File %s changed on disk. Reread from disk? "
1072 (t (setq do-edit nil))))
1075 (if (yes-or-no-p msg)
1077 (set-buffer-modified-p nil)
1078 (kill-buffer (current-buffer)))
1079 (message "Buffer %s was left intact" (buffer-name))))
1082 (if (null (setq file (get-file-buffer ex-file)))
1084 (ex-find-file ex-file)
1085 (vip-change-state-to-vi)
1086 (goto-char (point-min)))
1087 (switch-to-buffer file))
1090 (save-window-excursion
1091 (set-buffer vip-ex-work-buf)
1092 (delete-region (point-min) (point-max))
1093 (insert ex-offset "\n")
1094 (goto-char (point-min)))
1095 (goto-char (vip-get-ex-address))
1096 (beginning-of-line)))
1097 (ex-fixup-history vip-last-ex-prompt ex-file))
1099 ;; splits the string FILESPEC into substrings separated by newlines `\012'
1100 ;; each line assumed to be a file name. find-file's each file thus obtained.
1101 (defun ex-find-file (filespec)
1102 (let (s f filebuf status)
1103 (if (string-match "[^a-zA-Z0-9_.-/]" filespec)
1106 (set-buffer (get-buffer-create " *ex-tmp*"))
1109 (call-process ex-find-file-shell nil t nil
1110 ex-find-file-shell-options
1112 (format "echo %s | tr ' ' '\\012'" filespec)))
1113 (goto-char (point-min))
1114 ;; Give an error, if no match.
1117 (skip-chars-forward " \t\n\j")
1118 (if (looking-at "echo:")
1119 (vip-forward-word 1))
1122 (buffer-substring (point) (vip-line-pos 'end)))
1127 (setq f (buffer-substring s (point)))
1128 (setq filebuf (find-file-noselect f))
1129 (forward-to-indentation 1))
1131 (setq filebuf (find-file-noselect (setq f filespec))))
1132 (switch-to-buffer filebuf)
1135 (defun ex-global (variant)
1136 "Ex global command."
1137 (let ((gcommand ex-token))
1138 (if (or ex-g-flag ex-g-variant)
1139 (error "`%s' within `global' is not allowed" gcommand)
1147 (error "`%s': Missing regular expression" gcommand)))
1149 (if (string= ex-token "")
1150 (if (null vip-s-string)
1151 (error vip-NoPrevSearch)
1152 (setq ex-g-pat vip-s-string))
1153 (setq ex-g-pat ex-token
1154 vip-s-string ex-token))
1155 (if (null ex-addresses)
1156 (setq ex-addresses (list (point-max) (point-min)))
1157 (vip-default-ex-addresses))
1158 (let ((marks nil) (mark-count 0)
1159 com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1160 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1162 (vip-enlarge-region beg end)
1163 (exchange-point-and-mark)
1164 (let ((cont t) (limit (point-marker)))
1165 (exchange-point-and-mark)
1166 ;; skip the last line if empty
1168 (if (eobp) (vip-backward-char-carefully))
1169 (while (and cont (not (bobp)) (>= (point) limit))
1173 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1174 (if (or (and ex-g-flag found)
1175 (and ex-g-variant (not found)))
1178 (setq mark-count (1+ mark-count))
1179 (setq marks (cons (point-marker) marks)))))
1181 (if (bobp) (setq cont nil)
1184 (save-window-excursion
1185 (set-buffer vip-ex-work-buf)
1186 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1188 (goto-char (car marks))
1190 (setq mark-count (1- mark-count))
1191 (setq marks (cdr marks)))))
1195 (if (null ex-addresses)
1196 (setq ex-addresses (cons (point) nil)))
1197 (push-mark (point) t)
1198 (goto-char (car ex-addresses))
1199 (beginning-of-line))
1201 (defun ex-line (com)
1202 "Ex line commands. COM is join, shift-right or shift-left."
1203 (vip-default-ex-addresses)
1205 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1206 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1208 (vip-enlarge-region beg end)
1209 (exchange-point-and-mark)
1213 (forward-line ex-count)))
1215 ;; show text to be joined and ask for confirmation
1217 (with-output-to-temp-buffer " *text*"
1218 (princ (buffer-substring (point) (mark t))))
1221 (read-string "[Hit return to continue] ")
1222 (ex-line-subr com (point) (mark t)))
1224 (save-excursion (kill-buffer " *text*")))
1225 (ex-line-subr com (point) (mark t)))
1226 (setq point (point)))
1227 (goto-char (1- point))
1228 (beginning-of-line)))
1230 (defun ex-line-subr (com beg end)
1231 (cond ((string= com "join")
1232 (goto-char (min beg end))
1233 (while (and (not (eobp)) (< (point) (max beg end)))
1235 (if (and (<= (point) (max beg end)) (not (eobp)))
1238 (delete-region (point) (1- (point)))
1239 (if (not ex-variant) (fixup-whitespace))))))
1240 ((or (string= com "right") (string= com "left"))
1242 (min beg end) (max beg end)
1243 (if (string= com "right") vip-shift-width (- vip-shift-width)))
1244 (goto-char (max beg end))
1246 (vip-forward-char-carefully))))
1252 (if (null ex-addresses)
1254 (cons (point) nil)))
1255 (save-window-excursion
1256 (set-buffer vip-ex-work-buf)
1257 (skip-chars-forward " \t")
1258 (if (looking-at "[a-z]")
1260 (setq char (following-char))
1262 (skip-chars-forward " \t")
1263 (if (not (looking-at "[\n|]"))
1264 (error "`%s': %s" ex-token vip-SpuriousText)))
1265 (error "`%s' requires a following letter" ex-token)))
1267 (goto-char (car ex-addresses))
1268 (point-to-register (1+ (- char ?a))))))
1272 ;; Alternate file is the file next to the first one in the buffer ring
1273 (defun ex-next (cycle-other-window &optional find-alt-file)
1276 (if (not find-alt-file)
1279 (if (or (char-or-string-p ex-offset)
1280 (and (not (string= "" ex-file))
1281 (not (string-match "^[0-9]+$" ex-file))))
1284 (throw 'ex-edit nil))
1285 (setq count (string-to-int ex-file))
1286 (if (= count 0) (setq count 1))
1287 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1289 (setq l (vip-undisplayed-files))
1291 (while (and (not (null l)) (null (car l)))
1293 (setq count (1- count))
1296 (if find-alt-file (car l)
1299 (let* ((w (if cycle-other-window
1300 (get-lru-window) (selected-window)))
1301 (b (window-buffer w)))
1302 (set-window-buffer w (get-file-buffer (car l)))
1304 ;; this puts "next <count>" in the ex-command history
1305 (ex-fixup-history vip-last-ex-prompt ex-file))
1306 (error "Not that many undisplayed files")))))))
1309 (defun ex-next-related-buffer (direction &optional no-recursion)
1311 (vip-ring-rotate1 vip-related-files-and-buffers-ring direction)
1313 (let ((file-or-buffer-name
1314 (vip-current-ring-item vip-related-files-and-buffers-ring))
1315 (old-ring vip-related-files-and-buffers-ring)
1316 (old-win (selected-window))
1319 (or (and (ring-p vip-related-files-and-buffers-ring)
1320 (> (ring-length vip-related-files-and-buffers-ring) 0))
1321 (error "This buffer has no related files or buffers"))
1323 (or (stringp file-or-buffer-name)
1325 "File and buffer names must be strings, %S" file-or-buffer-name))
1327 (setq buf (cond ((get-buffer file-or-buffer-name))
1328 ((file-exists-p file-or-buffer-name)
1329 (find-file-noselect file-or-buffer-name))
1332 (if (not (vip-buffer-live-p buf))
1333 (error "Didn't find buffer %S or file %S"
1335 (abbreviate-file-name (expand-file-name file-or-buffer-name))))
1337 (if (equal buf (current-buffer))
1341 (ex-next-related-buffer direction 'norecursion)))
1346 (if (setq wind (vip-get-visible-buffer-window buf))
1348 (setq wind (get-lru-window (if vip-xemacs-p nil 'visible)))
1349 (set-window-buffer wind buf))
1353 (vip-raise-frame (vip-window-frame wind))
1354 (if (equal (vip-window-frame wind) (vip-window-frame old-win))
1355 (save-window-excursion (select-window wind) (sit-for 1))
1356 (select-window wind)))
1357 (save-window-excursion (select-window wind) (sit-for 1)))
1361 (setq vip-related-files-and-buffers-ring old-ring))
1363 (setq vip-local-search-start-marker (point-marker))
1367 (defun ex-preserve ()
1369 (message "Autosaving all buffers that need to be saved...")
1374 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1376 (setq vip-use-register ex-buffer)
1378 (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
1381 "Ex print working directory."
1382 (message default-directory))
1386 (if (< vip-expert-level 3)
1387 (save-buffers-kill-emacs)
1388 (kill-buffer (current-buffer))))
1394 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1396 (vip-add-newline-at-eob-if-necessary)
1397 (if (not (or (bobp) (eobp))) (forward-line 1))
1398 (if (and (not ex-variant) (string= ex-file ""))
1400 (if (null buffer-file-name)
1401 (error vip-NoFileSpecified))
1402 (setq ex-file buffer-file-name)))
1404 (shell-command ex-file t)
1405 (insert-file-contents ex-file)))
1406 (ex-fixup-history vip-last-ex-prompt ex-file))
1408 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1409 (defun ex-fixup-history (&rest args)
1410 (setq vip-ex-history
1411 (cons (mapconcat 'identity args " ") (cdr vip-ex-history))))
1414 (defun ex-recover ()
1415 "Ex recover from emacs \#file\#."
1417 (if (or ex-append ex-offset)
1418 (error "`recover': %s" vip-SpuriousText))
1419 (if (string= ex-file "")
1421 (if (null buffer-file-name)
1422 (error "This buffer isn't visiting any file"))
1423 (setq ex-file buffer-file-name))
1424 (setq ex-file (expand-file-name ex-file)))
1425 (if (and (not (string= ex-file (buffer-file-name)))
1428 (error "No write since last change \(:rec! overrides\)"))
1429 (recover-file ex-file))
1432 "Tell that `rewind' is obsolete and that one should use `:next count'"
1434 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1437 ;; read variable name for ex-set
1438 (defun ex-set-read-variable ()
1439 (let ((minibuffer-local-completion-map
1440 (copy-keymap minibuffer-local-completion-map))
1441 (cursor-in-echo-area t)
1444 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1445 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1446 (if (vip-set-unread-command-events
1447 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1450 (vip-set-unread-command-events ?\C-m)))
1451 (message ":set <Variable> [= <Value>]")
1452 (or batch (sit-for 2))
1454 (while (string-match "^[ \\t\\n]*$"
1456 (completing-read ":set " ex-variable-alist)))
1457 (message ":set <Variable> ")
1458 ;; if there are unread events, don't wait
1459 (or (vip-set-unread-command-events "") (sit-for 2))
1465 (let ((var (ex-set-read-variable))
1469 (auto-cmd-label "; don't touch or else...")
1470 (delete-turn-on-auto-fill-pattern
1471 "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1472 actual-lisp-cmd lisp-cmd-del-pattern
1475 (cond ((member var '("ai" "autoindent"))
1476 (setq var "vip-auto-indent"
1478 ((member var '("noai" "noautoindent"))
1479 (setq var "vip-auto-indent"
1481 ((member var '("ic" "ignorecase"))
1482 (setq var "vip-case-fold-search"
1484 ((member var '("noic" "noignorecase"))
1485 (setq var "vip-case-fold-search"
1487 ((member var '("ma" "magic"))
1488 (setq var "vip-re-search"
1490 ((member var '("noma" "nomagic"))
1491 (setq var "vip-re-search"
1493 ((member var '("ro" "readonly"))
1494 (setq var "buffer-read-only"
1496 ((member var '("noro" "noreadonly"))
1497 (setq var "buffer-read-only"
1499 ((member var '("sm" "showmatch"))
1500 (setq var "blink-matching-paren"
1502 ((member var '("nosm" "noshowmatch"))
1503 (setq var "blink-matching-paren"
1505 ((member var '("ws" "wrapscan"))
1506 (setq var "vip-search-wrap-around-t"
1508 ((member var '("nows" "nowrapscan"))
1509 (setq var "vip-search-wrap-around-t"
1511 (if (eq val 0) ; value must be set by the user
1512 (let ((cursor-in-echo-area t))
1513 (message (format ":set %s = <Value>" var))
1514 ;; if there are unread events, don't wait
1515 (or (vip-set-unread-command-events "") (sit-for 2))
1516 (setq val (read-string (format ":set %s = " var)))
1517 (ex-fixup-history "set" orig-var val)
1519 ;; check numerical values
1521 '("sw" "shiftwidth" "ts" "tabstop" "wm" "wrapmargin"))
1523 (or (numberp (setq val2 (car (read-from-string val))))
1524 (error "%s: Invalid value, numberp, %S" var val))
1526 (error "%s: Invalid value, numberp, %S" var val))))
1529 ((member var '("sw" "shiftwidth"))
1530 (setq var "vip-shift-width"))
1531 ((member var '("ts" "tabstop"))
1532 ;; make it take effect in curr buff and new bufs
1533 (kill-local-variable 'tab-width)
1534 (setq var "tab-width"
1535 set-cmd "setq-default"))
1536 ((member var '("tsl" "tab-stop-local"))
1537 (setq var "tab-width"
1540 ((member var '("wm" "wrapmargin"))
1541 ;; make it take effect in curr buff and new bufs
1542 (kill-local-variable 'fill-column)
1543 (setq var "fill-column"
1544 val (format "(- (window-width) %s)" val)
1545 set-cmd "setq-default"))
1546 ((member var '("sh" "shell"))
1547 (setq var "explicit-shell-file-name"
1548 val (format "\"%s\"" val)))))
1549 (ex-fixup-history "set" orig-var))
1551 (setq actual-lisp-cmd (format "\n(%s %s %s) %s"
1552 set-cmd var val auto-cmd-label))
1553 (setq lisp-cmd-del-pattern
1554 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1555 set-cmd var auto-cmd-label))
1557 (if (and ask-if-save
1558 (y-or-n-p (format "Do you want to save this setting in %s "
1559 vip-custom-file-name)))
1561 (vip-save-string-in-file
1562 actual-lisp-cmd vip-custom-file-name
1564 lisp-cmd-del-pattern)
1565 (if (string= var "fill-column")
1567 (vip-save-string-in-file
1569 "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
1571 vip-custom-file-name
1572 delete-turn-on-auto-fill-pattern)
1573 (vip-save-string-in-file
1574 nil vip-custom-file-name delete-turn-on-auto-fill-pattern)
1575 (vip-save-string-in-file
1576 nil vip-custom-file-name
1578 lisp-cmd-del-pattern)
1582 (message (format "%s %s %s" set-cmd var (if (string-match "^[ \t]*$" val)
1585 (eval (car (read-from-string actual-lisp-cmd)))
1586 (if (string= var "fill-column")
1589 (auto-fill-mode -1)))
1593 ;; In inline args, skip regex-forw and (optionally) chars-back.
1594 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1596 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1598 (set-buffer vip-ex-work-buf)
1599 (goto-char (point-min))
1600 (re-search-forward regex-forw nil t)
1603 (goto-char (point-max))
1605 (skip-chars-backward chars-back)
1606 (skip-chars-backward " \t\n\C-m"))
1608 ;; replace SPC with `=' to suppress the special meaning SPC has
1612 (while (re-search-forward " +" nil t)
1613 (replace-match replace-str nil t)
1614 (vip-forward-char-carefully)))
1616 (buffer-substring beg end))))
1624 "Viper help. Invokes Info."
1627 (pop-to-buffer (get-buffer-create "*info*"))
1628 (info vip-info-file-name)
1629 (message "Type `i' to search for a specific topic"))
1631 (with-output-to-temp-buffer " *vip-info*"
1632 (princ "The Info file for Viper does not seem to be installed.
1634 This file is part of the distribution of Viper. If you do not
1635 have the full distribution, please obtain it from the `anonymous'
1636 FTP account at `archive.cis.ohio-state.edu':
1638 /pub/gnu/emacs/elisp-archive/modes/viper.shar
1640 The Info files for Viper should be installed as <name>, <name>-1, etc.,
1641 where <name> is the value of `vip-info-file-name'.")))))
1644 "Ex source command. Loads the file specified as argument or `~/.vip'."
1646 (if (string= ex-file "")
1647 (load vip-custom-file-name)
1650 (defun ex-substitute (&optional repeat r-flag)
1651 "Ex substitute command.
1652 If REPEAT use previous regexp which is ex-reg-exp or vip-s-string"
1656 (case-fold-search vip-case-fold-search)
1658 (if repeat (setq ex-token nil) (setq delim (vip-get-ex-pat)))
1661 (setq pat (if r-flag vip-s-string ex-reg-exp))
1663 (error "No previous pattern to use in substitution"))
1665 delim (string-to-char pat)))
1666 (setq pat (if (string= ex-token "") vip-s-string ex-token))
1667 (setq vip-s-string pat
1669 (setq delim (vip-get-ex-pat))
1675 (while (vip-get-ex-opt-gc delim)
1676 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1680 (if ex-addresses (goto-char (car ex-addresses)))
1682 (forward-line (1- ex-count))
1683 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1684 (if (null ex-addresses)
1685 (setq ex-addresses (cons (point) (cons (point) nil)))
1686 (if (null (cdr ex-addresses))
1687 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1689 (let ((beg (car ex-addresses))
1690 (end (car (cdr ex-addresses)))
1693 (vip-enlarge-region beg end)
1694 (let ((limit (save-excursion
1695 (goto-char (max (point) (mark t)))
1697 (goto-char (min (point) (mark t)))
1698 (while (< (point) limit)
1700 (setq eol-mark (point-marker))
1704 (while (and (not (eolp))
1705 (re-search-forward pat eol-mark t))
1706 (if (or (not opt-c) (y-or-n-p "Replace? "))
1708 (setq matched-pos (point))
1709 (if (not (stringp repl))
1710 (error "Can't perform Ex substitution: No previous replacement pattern"))
1711 (replace-match repl t t))))
1713 (vip-forward-char-carefully))
1716 "Can't repeat Ex substitution: No previous regular expression"))
1717 (if (and (re-search-forward pat eol-mark t)
1718 (or (not opt-c) (y-or-n-p "Replace? ")))
1720 (setq matched-pos (point))
1721 (if (not (stringp repl))
1722 (error "Can't perform Ex substitution: No previous replacement pattern"))
1723 (replace-match repl t t)))
1725 (vip-forward-char-carefully))))))
1726 (if matched-pos (goto-char matched-pos))
1728 (if opt-c (message "done"))))
1733 (save-window-excursion
1734 (set-buffer vip-ex-work-buf)
1735 (skip-chars-forward " \t")
1737 (skip-chars-forward "^ |\t\n")
1738 (setq tag (buffer-substring (mark t) (point))))
1739 (if (not (string= tag "")) (setq ex-tag tag))
1740 (vip-change-state-to-emacs)
1741 (condition-case conds
1743 (if (string= tag "")
1745 (find-tag-other-window ex-tag))
1746 (vip-change-state-to-vi))
1748 (vip-change-state-to-vi)
1749 (vip-message-conditions conds)))))
1751 (defun ex-write (q-flag)
1753 (vip-default-ex-addresses t)
1755 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))
1756 temp-buf writing-same-file region
1757 file-exists writing-whole-file)
1758 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1761 (vip-enlarge-region beg end)
1762 (shell-command-on-region (point) (mark t) ex-file))
1763 (if (and (string= ex-file "") (not (buffer-file-name)))
1766 (format "Buffer %s isn't visiting any file. File to save in: "
1769 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
1770 ex-file (if (string= ex-file "")
1772 (expand-file-name ex-file))
1773 file-exists (file-exists-p ex-file)
1774 writing-same-file (string= ex-file (buffer-file-name)))
1775 (if (and writing-whole-file writing-same-file)
1776 (if (not (buffer-modified-p))
1777 (message "(No changes need to be saved)")
1779 (ex-write-info file-exists ex-file beg end))
1780 ;; writing some other file or portion of the currents
1781 ;; file---create temp buffer for it
1782 ;; disable undo in that buffer, for efficiency
1783 (buffer-disable-undo (setq temp-buf (create-file-buffer ex-file)))
1786 (if (and file-exists
1787 (not writing-same-file)
1789 (format "File %s exists. Overwrite? " ex-file))))
1791 (vip-enlarge-region beg end)
1792 (setq region (buffer-substring (point) (mark t)))
1793 (set-buffer temp-buf)
1794 (set-visited-file-name ex-file)
1796 (if (and file-exists ex-append)
1797 (insert-file-contents ex-file))
1798 (goto-char (point-max))
1801 (ex-write-info file-exists ex-file (point-min) (point-max))
1803 (set-buffer temp-buf)
1804 (set-buffer-modified-p nil)
1805 (kill-buffer temp-buf)
1808 ;; this prevents the loss of data if writing part of the buffer
1809 (if (and (buffer-file-name) writing-same-file)
1810 (set-visited-file-modtime))
1811 (or writing-whole-file
1812 (not writing-same-file)
1813 (set-buffer-modified-p t))
1815 (if (< vip-expert-level 2)
1816 (save-buffers-kill-emacs)
1817 (kill-buffer (current-buffer))))
1821 (defun ex-write-info (exists file-name beg end)
1822 (message "`%s'%s %d lines, %d characters"
1823 (abbreviate-file-name file-name)
1824 (if exists "" " [New file]")
1825 (count-lines beg (min (1+ end) (point-max)))
1830 (vip-default-ex-addresses)
1832 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1833 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1835 (vip-enlarge-region beg end)
1836 (exchange-point-and-mark)
1837 (if (or ex-g-flag ex-g-variant)
1838 (error "Can't execute `yank' within `global'"))
1842 (forward-line (1- ex-count)))
1844 (vip-enlarge-region (point) (mark t))
1845 (if ex-flag (error "`yank': %s" vip-SpuriousText))
1847 (cond ((vip-valid-register ex-buffer '(Letter))
1848 (vip-append-to-register
1849 (downcase ex-buffer) (point) (mark t)))
1850 ((vip-valid-register ex-buffer)
1851 (copy-to-register ex-buffer (point) (mark t) nil))
1852 (t (error vip-InvalidRegister ex-buffer))))
1853 (copy-region-as-kill (point) (mark t)))))
1855 (defun ex-command ()
1856 "Execute shell command."
1858 (save-window-excursion
1859 (set-buffer vip-ex-work-buf)
1860 (skip-chars-forward " \t")
1861 (setq command (buffer-substring (point) (point-max)))
1863 (setq command (ex-expand-filsyms command (current-buffer)))
1864 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
1865 (if vip-ex-last-shell-com
1866 (setq command (concat vip-ex-last-shell-com (substring command 1)))
1867 (error "No previous shell command")))
1868 (setq vip-ex-last-shell-com command)
1869 (if (null ex-addresses)
1870 (shell-command command)
1871 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1872 (if (null beg) (setq beg end))
1876 (vip-enlarge-region (point) (mark t))
1877 (shell-command-on-region (point) (mark t) command t))
1880 (defun ex-line-no ()
1881 "Print line number."
1885 (if (null ex-addresses) (point-max) (car ex-addresses))))))
1887 (defun vip-info-on-file ()
1888 "Give information on the file visited by the current buffer."
1891 (setq file (if (buffer-file-name)
1892 (concat (abbreviate-file-name (buffer-file-name)) ":")
1893 (concat (buffer-name) " [Not visiting any file]:"))
1894 info (format "line=%d/%d pos=%d/%d col=%d %s"
1895 (count-lines (point-min) (vip-line-pos 'end))
1896 (count-lines (point-min) (point-max))
1897 (point) (1- (point-max))
1898 (1+ (current-column))
1899 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
1900 (if (< (+ 1 (length info) (length file))
1901 (window-width (minibuffer-window)))
1902 (message (concat file " " info))
1903 (save-window-excursion
1904 (with-output-to-temp-buffer " *vip-info*"
1907 "\n\n\nPress any key to continue...\n\n")))
1914 ;;; viper-ex.el ends here