1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994, 1995 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.
26 (defconst vip-ex-work-buf-name
" *ex-working-space*")
27 (defconst vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
28 (defconst vip-ex-tmp-buf-name
" *ex-tmp*")
31 ;;; Variable completion in :set command
33 ;; The list of Ex commands. Used for completing command names.
34 (defconst ex-token-alist
35 '(("!") ("=") (">") ("&") ("~")
36 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
37 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
38 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
39 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
40 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
41 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
42 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
43 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
45 ;; A-list of Ex variables that can be set using the :set command.
46 (defconst ex-variable-alist
47 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
48 ("global-tabstop") ("gts") ("tabstop") ("ts")
49 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
51 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
52 ("noreadonly") ("noro") ("nomagic") ("noma")
53 ("noignorecase") ("noic")
54 ("global-noautoindent") ("gnoai") ("noautoindent") ("noai")
55 ("magic") ("ma") ("ignorecase") ("ic")
56 ("global-autoindent") ("gai") ("autoindent") ("ai")
61 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
65 ;; If non-nil, gives type of address; if nil, it is a command.
66 (defvar ex-token-type nil
)
68 ;; List of addresses passed to Ex command
69 (defvar ex-addresses nil
)
71 ;; It seems that this flag is used only for `#', `print', and `list', which
72 ;; aren't implemented. Check later.
75 ;; "buffer" where Ex commands keep deleted data.
76 ;; In Emacs terms, this is a register.
77 (defvar ex-buffer nil
)
82 ;; Flag for global command.
83 (defvar ex-g-flag nil
)
85 ;; If t, global command is executed on lines not matching ex-g-pat.
86 (defvar ex-g-variant nil
)
88 ;; Save reg-exp used in substitute.
89 (defvar ex-reg-exp nil
)
92 ;; Replace pattern for substitute.
95 ;; Pattern for global command.
98 ;; `sh' doesn't seem to expand wildcards, like `*'
99 (defconst ex-find-file-shell
"csh"
100 "Shell in which to interpret wildcards. Must be csh, tcsh, or similar.
101 Bourne shell doesn't seem to work here.")
102 (defvar ex-find-file-shell-options
"-f"
103 "*Options to pass to `ex-find-file-shell'.")
105 ;; Remembers the previous Ex tag.
108 ;; file used by Ex commands like :r, :w, :n
111 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
112 (defvar ex-variant nil
)
114 ;; Specified the offset of an Ex command, such as :read.
115 (defvar ex-offset nil
)
117 ;; Tells Ex that this is a w>> command.
118 (defvar ex-append nil
)
120 ;; File containing the shell command to be executed at Ex prompt,
122 (defvar ex-cmdfile nil
)
124 ;; flag used in vip-ex-read-file-name to indicate that we may be reading
125 ;; multiple file names. Used for :edit and :next
126 (defvar vip-keep-reading-filename nil
)
128 (defconst ex-cycle-other-window t
129 "*If t, :n and :b cycles through files and buffers in other window.
130 Then :N and :B cycles in the current window. If nil, this behavior is
133 (defconst ex-cycle-through-non-files nil
134 "*Cycle through *scratch* and other buffers that don't visit any file.")
136 ;; Last shell command executed with :! command.
137 (defvar vip-ex-last-shell-com nil
)
139 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
140 (defvar vip-incomplete-ex-cmd nil
)
142 ;; Remembers the last ex-command prompt.
143 (defvar vip-last-ex-prompt
"")
148 ;; Check if ex-token is an initial segment of STR
149 (defun vip-check-sub (str)
150 (let ((length (length ex-token
)))
151 (if (and (<= length
(length str
))
152 (string= ex-token
(substring str
0 length
)))
154 (setq ex-token-type
'non-command
))))
156 ;; Get a complete ex command
157 (defun vip-get-ex-com-subr ()
158 (let (case-fold-search)
160 (re-search-forward "[a-zA-Z][a-zA-Z]*")
161 (setq ex-token-type
'command
)
162 (setq ex-token
(buffer-substring (point) (mark t
)))
163 (exchange-point-and-mark)
164 (cond ((looking-at "a")
165 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
166 ((looking-at "ar") (vip-check-sub "args"))
167 (t (vip-check-sub "append"))))
168 ((looking-at "h") (vip-check-sub "help"))
170 (cond ((looking-at "cd") (vip-check-sub "cd"))
171 ((looking-at "ch") (vip-check-sub "chdir"))
172 ((looking-at "co") (vip-check-sub "copy"))
173 (t (vip-check-sub "change"))))
174 ((looking-at "d") (vip-check-sub "delete"))
175 ((looking-at "b") (vip-check-sub "buffer"))
176 ((looking-at "B") (vip-check-sub "Buffer"))
178 (if (looking-at "ex") (vip-check-sub "ex")
179 (vip-check-sub "edit")))
180 ((looking-at "f") (vip-check-sub "file"))
181 ((looking-at "g") (vip-check-sub "global"))
182 ((looking-at "i") (vip-check-sub "insert"))
183 ((looking-at "j") (vip-check-sub "join"))
184 ((looking-at "l") (vip-check-sub "list"))
186 (cond ((looking-at "map") (vip-check-sub "map"))
187 ((looking-at "mar") (vip-check-sub "mark"))
188 (t (vip-check-sub "move"))))
189 ((looking-at "k[a-z][^a-z]")
190 (setq ex-token
"kmark")
192 (exchange-point-and-mark)) ; this is canceled out by another
193 ; exchange-point-and-mark at the end
194 ((looking-at "k") (vip-check-sub "kmark"))
195 ((looking-at "n") (if (looking-at "nu")
196 (vip-check-sub "number")
197 (vip-check-sub "next")))
198 ((looking-at "N") (vip-check-sub "Next"))
199 ((looking-at "o") (vip-check-sub "open"))
201 (cond ((looking-at "pre") (vip-check-sub "preserve"))
202 ((looking-at "pu") (vip-check-sub "put"))
203 ((looking-at "pw") (vip-check-sub "pwd"))
204 (t (vip-check-sub "print"))))
205 ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
206 ((looking-at "R") (vip-check-sub "RelatedFile"))
207 ((looking-at "q") (vip-check-sub "quit"))
209 (cond ((looking-at "rec") (vip-check-sub "recover"))
210 ((looking-at "rew") (vip-check-sub "rewind"))
211 (t (vip-check-sub "read"))))
213 (cond ((looking-at "se") (vip-check-sub "set"))
214 ((looking-at "sh") (vip-check-sub "shell"))
215 ((looking-at "so") (vip-check-sub "source"))
216 ((looking-at "sr") (vip-check-sub "sr"))
217 ((looking-at "st") (vip-check-sub "stop"))
218 ((looking-at "sus") (vip-check-sub "suspend"))
219 ((looking-at "subm") (vip-check-sub "submitReport"))
220 (t (vip-check-sub "substitute"))))
222 (if (looking-at "ta") (vip-check-sub "tag")
223 (vip-check-sub "transfer")))
225 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
226 ((looking-at "unm") (vip-check-sub "unmap"))
227 (t (vip-check-sub "undo"))))
229 (cond ((looking-at "ve") (vip-check-sub "version"))
230 ((looking-at "vi") (vip-check-sub "visual"))
231 (t (vip-check-sub "vglobal"))))
233 (if (looking-at "wq") (vip-check-sub "wq")
234 (vip-check-sub "write")))
236 (if (looking-at "WW")
237 (vip-check-sub "WWrite")
238 (vip-check-sub "Write")))
239 ((looking-at "x") (vip-check-sub "xit"))
240 ((looking-at "y") (vip-check-sub "yank"))
241 ((looking-at "z") (vip-check-sub "z")))
242 (exchange-point-and-mark)
245 ;; Get an ex-token which is either an address or a command.
246 ;; A token has a type, \(command, address, end-mark\), and a value
247 (defun vip-get-ex-token ()
248 (save-window-excursion
249 (set-buffer vip-ex-work-buf
)
250 (skip-chars-forward " \t|")
251 (cond ((looking-at "#")
252 (setq ex-token-type
'command
)
253 (setq ex-token
(char-to-string (following-char)))
255 ((looking-at "[a-z]") (vip-get-ex-com-subr))
258 (setq ex-token-type
'dot
))
259 ((looking-at "[0-9]")
261 (re-search-forward "[0-9]*")
263 (cond ((eq ex-token-type
'plus
) 'add-number
)
264 ((eq ex-token-type
'minus
) 'sub-number
)
266 (setq ex-token
(string-to-int (buffer-substring (point) (mark t
)))))
269 (setq ex-token-type
'end
))
272 (setq ex-token-type
'whole
))
274 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
278 (setq ex-token-type
'plus
))
279 ((looking-at "+[0-9]")
281 (setq ex-token-type
'plus
))
283 (error vip-BadAddress
))))
285 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
289 (setq ex-token-type
'minus
))
290 ((looking-at "-[0-9]")
292 (setq ex-token-type
'minus
))
294 (error vip-BadAddress
))))
299 (while (and (not (eolp)) cont
)
300 ;;(re-search-forward "[^/]*/")
301 (re-search-forward "[^/]*\\(/\\|\n\\)")
302 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
305 (setq ex-token
(buffer-substring (point) (mark t
)))
306 (if (looking-at "/") (forward-char 1))
307 (setq ex-token-type
'search-forward
))
312 (while (and (not (eolp)) cont
)
313 ;;(re-search-forward "[^\\?]*\\?")
314 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
315 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
318 (if (not (looking-at "\n")) (forward-char 1))))
319 (setq ex-token-type
'search-backward
)
320 (setq ex-token
(buffer-substring (1- (point)) (mark t
))))
323 (setq ex-token-type
'comma
))
326 (setq ex-token-type
'semi-colon
))
327 ((looking-at "[!=><&~]")
328 (setq ex-token-type
'command
)
329 (setq ex-token
(char-to-string (following-char)))
332 (setq ex-token-type
'goto-mark
)
334 (cond ((looking-at "'") (setq ex-token nil
))
335 ((looking-at "[a-z]") (setq ex-token
(following-char)))
336 (t (error "Marks are ' and a-z")))
339 (setq ex-token-type
'end-mark
)
340 (setq ex-token
"goto"))
342 (error vip-BadExCommand
)))))
344 ;; Reads Ex command. Tries to determine if it has to exit because command
345 ;; is complete or invalid. If not, keeps reading command.
346 (defun ex-cmd-read-exit ()
348 (setq vip-incomplete-ex-cmd t
)
349 (let ((quit-regex1 (concat
352 "\\|" "[nN]ext[ \t]*"
365 "\\|" "[ktgjmsz][ \t]*$"
367 "\\|" "tr[ansfer \t]*"
370 "\\|" "^[ \t]*k?ma[^p]*"
379 "\\|" "![ \t]*[a-zA-Z].*"
383 (save-window-excursion ;; put cursor at the end of the Ex working buffer
384 (set-buffer vip-ex-work-buf
)
385 (goto-char (point-max)))
386 (cond ((vip-looking-back quit-regex1
) (exit-minibuffer))
387 ((vip-looking-back stay-regex
) (insert " "))
388 ((vip-looking-back quit-regex2
) (exit-minibuffer))
391 ;; complete Ex command
392 (defun ex-cmd-complete ()
394 (let (save-pos dist compl-list string-to-complete completion-result
)
397 (setq dist
(skip-chars-backward "[a-zA-Z!=>&~]")
401 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
403 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
404 ;; Preceding characters are not the ones allowed in an Ex command
405 ;; or we have typed past command name.
406 ;; Note: we didn't do parsing, so there may be surprises.
407 (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
408 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
409 (looking-at "[^ \t\n\C-m]"))
411 (with-output-to-temp-buffer "*Completions*"
412 (display-completion-list
413 (vip-alist-to-list ex-token-alist
))))
414 ;; Preceding chars may be part of a command name
415 (setq string-to-complete
(buffer-substring save-pos
(point)))
416 (setq completion-result
417 (try-completion string-to-complete ex-token-alist
))
419 (cond ((eq completion-result t
) ; exact match--do nothing
420 (vip-tmp-insert-at-eob " (Sole completion)"))
421 ((eq completion-result nil
)
422 (vip-tmp-insert-at-eob " (No match)"))
423 (t ;; partial completion
425 (delete-region (point) (point-max))
426 (insert completion-result
)
427 (let (case-fold-search)
429 (vip-filter-alist (concat "^" completion-result
)
431 (if (> (length compl-list
) 1)
432 (with-output-to-temp-buffer "*Completions*"
433 (display-completion-list
434 (vip-alist-to-list (reverse compl-list
)))))))
439 ;; Ex commands themselves are implemented in viper-ex.el
440 (defun vip-ex (&optional string
)
445 (let* ((map (copy-keymap minibuffer-local-map
))
449 prev-token-type com-str
)
451 (vip-add-keymap vip-ex-cmd-map map
)
453 (setq com-str
(or string
(vip-read-string-with-history
459 (save-window-excursion
461 (or (vip-buffer-live-p vip-ex-work-buf
)
462 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
)))
463 (set-buffer vip-ex-work-buf
)
464 (delete-region (point-min) (point-max))
465 (insert com-str
"\n")
466 (goto-char (point-min)))
467 (setq ex-token-type nil
471 (cond ((memq ex-token-type
'(command end-mark
))
472 (if address
(setq ex-addresses
(cons address ex-addresses
)))
473 (cond ((string= ex-token
"global")
476 ((string= ex-token
"vglobal")
480 (vip-execute-ex-command)
481 (save-window-excursion
482 (set-buffer vip-ex-work-buf
)
483 (skip-chars-forward " \t")
484 (cond ((looking-at "|")
488 (t (error "`%s': %s" ex-token vip-SpuriousText
)))
491 ((eq ex-token-type
'non-command
)
492 (error (format "`%s': %s" ex-token vip-BadExCommand
)))
493 ((eq ex-token-type
'whole
)
497 (cons (point-max) ex-addresses
)
498 (cons (point-max) (cons (point-min) ex-addresses
)))))
499 ((eq ex-token-type
'comma
)
500 (if (eq prev-token-type
'whole
)
501 (setq address
(point-min)))
503 (cons (if (null address
) (point) address
) ex-addresses
)))
504 ((eq ex-token-type
'semi-colon
)
505 (if (eq prev-token-type
'whole
)
506 (setq address
(point-min)))
507 (if address
(setq dot address
))
509 (cons (if (null address
) (point) address
) ex-addresses
)))
510 (t (let ((ans (vip-get-ex-address-subr address dot
)))
511 (if ans
(setq address ans
)))))
512 (setq prev-token-type ex-token-type
))))
515 ;; Get a regular expression and set `ex-variant', if found
516 (defun vip-get-ex-pat ()
517 (save-window-excursion
518 (set-buffer vip-ex-work-buf
)
519 (skip-chars-forward " \t")
522 (setq ex-g-variant
(not ex-g-variant
)
523 ex-g-flag
(not ex-g-flag
))
525 (skip-chars-forward " \t")))
526 (let ((c (following-char)))
527 (if (string-match "[0-9A-Za-z]" (format "%c" c
))
529 "Global regexp must be inside matching non-alphanumeric chars"))
530 (if (looking-at "[^\\\\\n]")
535 (while (and (not (eolp)) cont
)
536 (if (not (re-search-forward (format "[^%c]*%c" c c
) nil t
))
537 (if (member ex-token
'("global" "vglobal"))
539 "Missing closing delimiter for global regexp")
540 (goto-char (point-max))))
541 (if (not (vip-looking-back
542 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c
)))
545 (if (= (mark t
) (point)) ""
546 (buffer-substring (1- (point)) (mark t
))))
552 (defun vip-get-ex-command ()
553 (save-window-excursion
554 (set-buffer vip-ex-work-buf
)
555 (if (looking-at "/") (forward-char 1))
556 (skip-chars-forward " \t")
557 (cond ((looking-at "[a-z]")
558 (vip-get-ex-com-subr)
559 (if (eq ex-token-type
'non-command
)
560 (error "`%s': %s" ex-token vip-BadExCommand
)))
561 ((looking-at "[!=><&~]")
562 (setq ex-token
(char-to-string (following-char)))
564 (t (error vip-BadExCommand
)))))
566 ;; Get an Ex option g or c
567 (defun vip-get-ex-opt-gc (c)
568 (save-window-excursion
569 (set-buffer vip-ex-work-buf
)
570 (if (looking-at (format "%c" c
)) (forward-char 1))
571 (skip-chars-forward " \t")
572 (cond ((looking-at "g")
582 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
583 (defun vip-default-ex-addresses (&optional whole-flag
)
584 (cond ((null ex-addresses
)
587 (cons (point-max) (cons (point-min) nil
))
588 (cons (point) (cons (point) nil
)))))
589 ((null (cdr ex-addresses
))
591 (cons (car ex-addresses
) ex-addresses
)))))
593 ;; Get an ex-address as a marker and set ex-flag if a flag is found
594 (defun vip-get-ex-address ()
595 (let ((address (point-marker)) (cont t
))
600 (cond ((eq ex-token-type
'command
)
601 (if (member ex-token
'("print" "list" "#"))
605 (error "Address expected in this Ex command")))
606 ((eq ex-token-type
'end-mark
)
608 ((eq ex-token-type
'whole
)
609 (error "Trailing address expected"))
610 ((eq ex-token-type
'comma
)
611 (error "`%s': %s" ex-token vip-SpuriousText
))
612 (t (let ((ans (vip-get-ex-address-subr address
(point-marker))))
613 (if ans
(setq address ans
))))))
616 ;; Returns an address as a point
617 (defun vip-get-ex-address-subr (old-address dot
)
619 (if (null old-address
) (setq old-address dot
))
620 (cond ((eq ex-token-type
'dot
)
622 ((eq ex-token-type
'add-number
)
624 (goto-char old-address
)
625 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
626 (setq address
(point-marker))))
627 ((eq ex-token-type
'sub-number
)
629 (goto-char old-address
)
630 (forward-line (- ex-token
))
631 (setq address
(point-marker))))
632 ((eq ex-token-type
'abs-number
)
634 (goto-char (point-min))
635 (if (= ex-token
0) (setq address
0)
636 (forward-line (1- ex-token
))
637 (setq address
(point-marker)))))
638 ((eq ex-token-type
'end
)
639 (setq address
(point-max-marker)))
640 ((eq ex-token-type
'plus
) t
) ; do nothing
641 ((eq ex-token-type
'minus
) t
) ; do nothing
642 ((eq ex-token-type
'search-forward
)
644 (ex-search-address t
)
645 (setq address
(point-marker))))
646 ((eq ex-token-type
'search-backward
)
648 (ex-search-address nil
)
649 (setq address
(point-marker))))
650 ((eq ex-token-type
'goto-mark
)
653 (exchange-point-and-mark)
654 (goto-char (vip-register-to-point
655 (1+ (- ex-token ?a
)) 'enforce-buffer
)))
656 (setq address
(point-marker)))))
660 ;; Search pattern and set address
661 (defun ex-search-address (forward)
662 (if (string= ex-token
"")
663 (if (null vip-s-string
)
664 (error vip-NoPrevSearch
)
665 (setq ex-token vip-s-string
))
666 (setq vip-s-string ex-token
))
670 (re-search-forward ex-token
))
672 (re-search-backward ex-token
)))
674 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
675 (defun vip-get-ex-buffer ()
679 (save-window-excursion
680 (set-buffer vip-ex-work-buf
)
681 (skip-chars-forward " \t")
682 (if (looking-at "[a-zA-Z]")
684 (setq ex-buffer
(following-char))
686 (skip-chars-forward " \t")))
687 (if (looking-at "[0-9]")
690 (re-search-forward "[0-9][0-9]*")
691 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
692 (skip-chars-forward " \t")))
693 (if (looking-at "[pl#]")
697 (if (not (looking-at "[\n|]"))
698 (error "`%s': %s" ex-token vip-SpuriousText
))))
700 (defun vip-get-ex-count ()
704 (save-window-excursion
705 (set-buffer vip-ex-work-buf
)
706 (skip-chars-forward " \t")
711 (skip-chars-forward " \t")
712 (if (looking-at "[0-9]")
715 (re-search-forward "[0-9][0-9]*")
716 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
717 (skip-chars-forward " \t")))
718 (if (looking-at "[pl#]")
722 (if (not (looking-at "[\n|]"))
724 (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand
))))
726 ;; Expand \% and \# in ex command
727 (defun ex-expand-filsyms (cmd buf
)
731 (setq cf buffer-file-name
)
732 (setq pf
(ex-next nil t
))) ; this finds alternative file name
733 (if (and (null cf
) (string-match "[^\\]%\\|\\`%" cmd
))
734 (error "No current file to substitute for `%%'"))
735 (if (and (null pf
) (string-match "[^\\]#\\|\\`#" cmd
))
736 (error "No alternate file to substitute for `#'"))
738 (set-buffer (get-buffer-create vip-ex-tmp-buf-name
))
741 (goto-char (point-min))
742 (while (re-search-forward "%\\|#" nil t
)
743 (let ((data (match-data))
744 (char (buffer-substring (match-beginning 0) (match-end 0))))
745 (if (vip-looking-back (concat "\\\\" char
))
747 (store-match-data data
)
748 (if (string= char
"%")
750 (replace-match pf
)))))
752 (setq ret
(buffer-substring (point-min) (point)))
756 ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
757 (defun vip-get-ex-file ()
765 (save-window-excursion
766 (set-buffer vip-ex-work-buf
)
767 (skip-chars-forward " \t")
769 (if (and (not (vip-looking-back "[ \t]"))
770 ;; read doesn't have a corresponding :r! form, so ! is
771 ;; immediately interpreted as a shell command.
772 (not (string= ex-token
"read")))
776 (skip-chars-forward " \t"))
779 (skip-chars-forward " \t")))
780 (if (looking-at ">>")
785 (skip-chars-forward " \t")))
790 (re-search-forward "[ \t\n]")
792 (setq ex-offset
(buffer-substring (point) (mark t
)))
794 (skip-chars-forward " \t")))
795 ;; this takes care of :r, :w, etc., when they get file names
796 ;; from the history list
797 (if (member ex-token
'("read" "write" "edit" "visual" "next"))
799 (setq ex-file
(buffer-substring (point) (1- (point-max))))
801 ;; For :e, match multiple non-white strings separated
802 ;; by white. For others, find the first non-white string
804 (if (string= ex-token
"edit")
805 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
809 ;; if file name comes from history, don't leave
810 ;; minibuffer when the user types space
811 (setq vip-incomplete-ex-cmd nil
)
812 ;; this must be the last clause in this progn
813 (substring ex-file
(match-beginning 0) (match-end 0))
816 ;; this leaves only the command name in the work area
817 ;; file names are gone
818 (delete-region (point) (1- (point-max)))
820 (goto-char (point-max))
821 (skip-chars-backward " \t\n")
822 (setq prompt
(buffer-substring (point-min) (point)))
825 (setq vip-last-ex-prompt prompt
)
827 ;; If we just finished reading command, redisplay prompt
828 (if vip-incomplete-ex-cmd
829 (setq ex-file
(vip-ex-read-file-name (format ":%s " prompt
)))
830 ;; file was typed in-line
831 (setq ex-file
(or ex-file
"")))
835 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
836 ;; file names, arranges to re-enter the minibuffer.
837 (defun vip-complete-filename-or-exit ()
839 (setq vip-keep-reading-filename t
)
840 ;; don't exit if directory---ex-commands don't
841 (cond ((ex-cmd-accepts-multiple-files-p ex-token
) (exit-minibuffer))
842 ;; apparently the argument to an Ex command is
843 ;; supposed to be a shell command
844 ((vip-looking-back "^[ \t]*!.*")
848 (setq ex-cmdfile nil
)
849 (minibuffer-complete-word))))
851 (defun vip-handle-! ()
854 (buffer-string) (vip-abbreviate-file-name default-directory
))
855 (member ex-token
'("read" "write")))
859 (defun ex-cmd-accepts-multiple-files-p (token)
860 (member token
'("edit" "next" "Next")))
862 ;; If user doesn't enter anything, then "" is returned, i.e., the
863 ;; prompt-directory is not returned.
864 (defun vip-ex-read-file-name (prompt)
866 (minibuffer-local-completion-map
867 (copy-keymap minibuffer-local-completion-map
))
870 (vip-add-keymap ex-read-filename-map
872 minibuffer-local-completion-map
875 (setq cont
(setq vip-keep-reading-filename t
))
877 (setq vip-keep-reading-filename nil
878 val
(read-file-name (concat prompt str
) nil default-directory
)
879 str
(concat str
(if (equal val
"") "" " ")
880 val
(if (equal val
"") "" " ")))
882 ;; Only edit, next, and Next commands accept multiple files.
883 ;; vip-keep-reading-filename is set in the anonymous function that is
884 ;; bound to " " in ex-read-filename-map.
885 (setq cont
(and vip-keep-reading-filename
886 (ex-cmd-accepts-multiple-files-p ex-token
)))
889 (setq beg
(string-match "[^ \t]" str
) ; delete leading blanks
890 end
(string-match "[ \t]*$" str
)) ; delete trailing blanks
891 (if (member ex-token
'("read" "write"))
892 (if (string-match "[\t ]*!" str
)
893 ;; this is actually a shell command
897 (setq vip-last-ex-prompt
(concat vip-last-ex-prompt
" !")))))
898 (substring str
(or beg
0) end
)))
900 ;; Execute ex command using the value of addresses
901 (defun vip-execute-ex-command ()
902 (vip-deactivate-mark)
903 (cond ((string= ex-token
"args") (ex-args))
904 ((string= ex-token
"copy") (ex-copy nil
))
905 ((string= ex-token
"cd") (ex-cd))
906 ((string= ex-token
"chdir") (ex-cd))
907 ((string= ex-token
"delete") (ex-delete))
908 ((string= ex-token
"edit") (ex-edit))
909 ((string= ex-token
"file") (vip-info-on-file))
910 ((string= ex-token
"goto") (ex-goto))
911 ((string= ex-token
"help") (ex-help))
912 ((string= ex-token
"join") (ex-line "join"))
913 ((string= ex-token
"kmark") (ex-mark))
914 ((string= ex-token
"mark") (ex-mark))
915 ((string= ex-token
"map") (ex-map))
916 ((string= ex-token
"move") (ex-copy t
))
917 ((string= ex-token
"next") (ex-next ex-cycle-other-window
))
918 ((string= ex-token
"Next") (ex-next (not ex-cycle-other-window
)))
919 ((string= ex-token
"RelatedFile") (ex-next-related-buffer 1))
920 ((string= ex-token
"put") (ex-put))
921 ((string= ex-token
"pwd") (ex-pwd))
922 ((string= ex-token
"preserve") (ex-preserve))
923 ((string= ex-token
"PreviousRelatedFile") (ex-next-related-buffer -
1))
924 ((string= ex-token
"quit") (ex-quit))
925 ((string= ex-token
"read") (ex-read))
926 ((string= ex-token
"recover") (ex-recover))
927 ((string= ex-token
"rewind") (ex-rewind))
928 ((string= ex-token
"submitReport") (vip-submit-report))
929 ((string= ex-token
"set") (ex-set))
930 ((string= ex-token
"shell") (ex-shell))
931 ((string= ex-token
"source") (ex-source))
932 ((string= ex-token
"sr") (ex-substitute t t
))
933 ((string= ex-token
"substitute") (ex-substitute))
934 ((string= ex-token
"suspend") (suspend-emacs))
935 ((string= ex-token
"stop") (suspend-emacs))
936 ((string= ex-token
"transfer") (ex-copy nil
))
937 ((string= ex-token
"buffer") (if ex-cycle-other-window
938 (vip-switch-to-buffer-other-window)
939 (vip-switch-to-buffer)))
940 ((string= ex-token
"Buffer") (if ex-cycle-other-window
941 (vip-switch-to-buffer)
942 (vip-switch-to-buffer-other-window)))
943 ((string= ex-token
"tag") (ex-tag))
944 ((string= ex-token
"undo") (vip-undo))
945 ((string= ex-token
"unmap") (ex-unmap))
946 ((string= ex-token
"version") (vip-version))
947 ((string= ex-token
"visual") (ex-edit))
948 ((string= ex-token
"write") (ex-write nil
))
949 ((string= ex-token
"Write") (save-some-buffers))
950 ((string= ex-token
"wq") (ex-write t
))
951 ((string= ex-token
"WWrite") (save-some-buffers t
)) ; don't ask
952 ((string= ex-token
"xit") (ex-write t
))
953 ((string= ex-token
"yank") (ex-yank))
954 ((string= ex-token
"!") (ex-command))
955 ((string= ex-token
"=") (ex-line-no))
956 ((string= ex-token
">") (ex-line "right"))
957 ((string= ex-token
"<") (ex-line "left"))
958 ((string= ex-token
"&") (ex-substitute t
))
959 ((string= ex-token
"~") (ex-substitute t t
))
960 ((or (string= ex-token
"append")
961 (string= ex-token
"change")
962 (string= ex-token
"insert")
963 (string= ex-token
"open"))
965 (format "`%s': Obsolete command, not supported by Viper"
967 ((or (string= ex-token
"abbreviate")
968 (string= ex-token
"unabbreviate"))
971 "`%s': Vi-style abbrevs are obsolete. Use the more powerful Emacs abbrevs"
973 ((or (string= ex-token
"list")
974 (string= ex-token
"print")
975 (string= ex-token
"z")
976 (string= ex-token
"#"))
978 (format "`%s': Command not implemented in Viper" ex-token
)))
979 (t (error (format "`%s': %s" ex-token vip-BadExCommand
)))))
981 (defun vip-undisplayed-files ()
985 (if (null (get-buffer-window b
))
986 (let ((f (buffer-file-name b
)))
988 (if ex-cycle-through-non-files
989 (let ((s (buffer-name b
)))
990 (if (string= " " (substring s
0 1))
999 (let ((l (vip-undisplayed-files))
1002 (while (not (null l
))
1004 (setq args
(format "%s %d) %s\n" args file-count
(car l
))
1005 file-count
(1+ file-count
)))
1007 (if (string= args
"")
1008 (message "All files are already displayed")
1010 (save-window-excursion
1011 (with-output-to-temp-buffer " *vip-info*"
1012 (princ "\n\nThese files are not displayed in any window.\n")
1013 (princ "\n=============\n")
1015 (princ "\n=============\n")
1016 (princ "\nThe numbers can be given as counts to :next. ")
1017 (princ "\n\nPress any key to continue...\n\n"))
1018 (vip-read-event))))))
1020 ;; Ex cd command. Default directory of this buffer changes
1023 (if (string= ex-file
"")
1025 (setq default-directory
(file-name-as-directory (expand-file-name ex-file
))))
1027 ;; Ex copy and move command. DEL-FLAG means delete
1028 (defun ex-copy (del-flag)
1029 (vip-default-ex-addresses)
1030 (let ((address (vip-get-ex-address))
1031 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1035 (vip-enlarge-region (mark t
) (point))
1037 (kill-region (point) (mark t
))
1038 (copy-region-as-kill (point) (mark t
)))
1041 (with-output-to-temp-buffer "*copy text*"
1043 (if (or del-flag ex-g-flag ex-g-variant
)
1045 (buffer-substring (point) (mark t
)))))
1048 (read-string "[Hit return to continue] ")
1049 (save-excursion (kill-buffer "*copy text*")))
1050 (quit (save-excursion (kill-buffer "*copy text*"))
1051 (signal 'quit nil
))))))
1053 (goto-char (point-min))
1056 (insert (current-kill 0))))
1058 ;; Ex delete command
1060 (vip-default-ex-addresses)
1062 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1063 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1065 (vip-enlarge-region beg end
)
1066 (exchange-point-and-mark)
1070 (forward-line (1- ex-count
)))
1072 (vip-enlarge-region (point) (mark t
))
1074 ;; show text to be deleted and ask for confirmation
1076 (with-output-to-temp-buffer " *delete text*"
1077 (princ (buffer-substring (point) (mark t
))))
1079 (read-string "[Hit return to continue] ")
1081 (save-excursion (kill-buffer " *delete text*"))
1083 (save-excursion (kill-buffer " *delete text*")))
1085 (cond ((vip-valid-register ex-buffer
'(Letter))
1086 (vip-append-to-register
1087 (downcase ex-buffer
) (point) (mark t
)))
1088 ((vip-valid-register ex-buffer
)
1089 (copy-to-register ex-buffer
(point) (mark t
) nil
))
1090 (t (error vip-InvalidRegister ex-buffer
))))
1091 (kill-region (point) (mark t
))))))
1096 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1097 ;; asked if current buffer should really be discarded.
1098 ;; This command can take multiple file names. It replaces the current buffer
1099 ;; with the first file in its argument list
1100 (defun ex-edit (&optional file
)
1103 (cond ((and (string= ex-file
"") buffer-file-name
)
1104 (setq ex-file
(vip-abbreviate-file-name (buffer-file-name))))
1105 ((string= ex-file
"")
1106 (error vip-NoFileSpecified
)))
1109 (if buffer-file-name
1110 (cond ((buffer-modified-p)
1112 (format "Buffer %s is modified. Discard changes? "
1115 ((not (verify-visited-file-modtime (current-buffer)))
1117 (format "File %s changed on disk. Reread from disk? "
1120 (t (setq do-edit nil
))))
1123 (if (yes-or-no-p msg
)
1125 (set-buffer-modified-p nil
)
1126 (kill-buffer (current-buffer)))
1127 (message "Buffer %s was left intact" (buffer-name))))
1130 (if (null (setq file
(get-file-buffer ex-file
)))
1132 (ex-find-file ex-file
)
1133 (vip-change-state-to-vi)
1134 (goto-char (point-min)))
1135 (switch-to-buffer file
))
1138 (save-window-excursion
1139 (set-buffer vip-ex-work-buf
)
1140 (delete-region (point-min) (point-max))
1141 (insert ex-offset
"\n")
1142 (goto-char (point-min)))
1143 (goto-char (vip-get-ex-address))
1144 (beginning-of-line)))
1145 (ex-fixup-history vip-last-ex-prompt ex-file
))
1147 ;; splits the string FILESPEC into substrings separated by newlines `\012'
1148 ;; each line assumed to be a file name. find-file's each file thus obtained.
1149 (defun ex-find-file (filespec)
1150 (let (f filebuf tmp-buf status
)
1151 (if (string-match "[^a-zA-Z0-9_.-/]" filespec
)
1154 (set-buffer (setq tmp-buf
(get-buffer-create vip-ex-tmp-buf-name
)))
1157 (call-process ex-find-file-shell nil t nil
1158 ex-find-file-shell-options
1160 (format "echo %s | tr ' ' '\\012'" filespec
)))
1161 (goto-char (point-min))
1162 ;; Issue an error, if no match.
1165 (skip-chars-forward " \t\n\j")
1166 (if (looking-at "echo:")
1167 (vip-forward-word 1))
1170 (buffer-substring (point) (vip-line-pos 'end
)))
1172 (reverse-region (point-min) (point-max))
1173 (goto-char (point-min))
1175 (setq f
(buffer-substring (point) (vip-line-pos 'end
)))
1176 (setq filebuf
(find-file f
))
1177 (set-buffer tmp-buf
) ; otherwise it'll be in f.
1178 (forward-to-indentation 1))
1180 (setq filebuf
(find-file-noselect (setq f filespec
))))
1181 (switch-to-buffer filebuf
)
1184 ;; Ex global command
1185 (defun ex-global (variant)
1186 (let ((gcommand ex-token
))
1187 (if (or ex-g-flag ex-g-variant
)
1188 (error "`%s' within `global' is not allowed" gcommand
)
1196 (error "`%s': Missing regular expression" gcommand
)))
1198 (if (string= ex-token
"")
1199 (if (null vip-s-string
)
1200 (error vip-NoPrevSearch
)
1201 (setq ex-g-pat vip-s-string
))
1202 (setq ex-g-pat ex-token
1203 vip-s-string ex-token
))
1204 (if (null ex-addresses
)
1205 (setq ex-addresses
(list (point-max) (point-min)))
1206 (vip-default-ex-addresses))
1207 (let ((marks nil
) (mark-count 0)
1208 com-str
(end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1209 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1211 (vip-enlarge-region beg end
)
1212 (exchange-point-and-mark)
1213 (let ((cont t
) (limit (point-marker)))
1214 (exchange-point-and-mark)
1215 ;; skip the last line if empty
1217 (if (eobp) (vip-backward-char-carefully))
1218 (while (and cont
(not (bobp)) (>= (point) limit
))
1222 (let ((found (re-search-backward ex-g-pat
(mark t
) t
)))
1223 (if (or (and ex-g-flag found
)
1224 (and ex-g-variant
(not found
)))
1227 (setq mark-count
(1+ mark-count
))
1228 (setq marks
(cons (point-marker) marks
)))))
1230 (if (bobp) (setq cont nil
)
1233 (save-window-excursion
1234 (set-buffer vip-ex-work-buf
)
1235 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
1237 (goto-char (car marks
))
1239 (setq mark-count
(1- mark-count
))
1240 (setq marks
(cdr marks
)))))
1244 (if (null ex-addresses
)
1245 (setq ex-addresses
(cons (point) nil
)))
1246 (push-mark (point) t
)
1247 (goto-char (car ex-addresses
))
1248 (beginning-of-line))
1250 ;; Ex line commands. COM is join, shift-right or shift-left
1251 (defun ex-line (com)
1252 (vip-default-ex-addresses)
1254 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
1255 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1257 (vip-enlarge-region beg end
)
1258 (exchange-point-and-mark)
1262 (forward-line ex-count
)))
1264 ;; show text to be joined and ask for confirmation
1266 (with-output-to-temp-buffer " *text*"
1267 (princ (buffer-substring (point) (mark t
))))
1270 (read-string "[Hit return to continue] ")
1271 (ex-line-subr com
(point) (mark t
)))
1273 (save-excursion (kill-buffer " *text*")))
1274 (ex-line-subr com
(point) (mark t
)))
1275 (setq point
(point)))
1276 (goto-char (1- point
))
1277 (beginning-of-line)))
1279 (defun ex-line-subr (com beg end
)
1280 (cond ((string= com
"join")
1281 (goto-char (min beg end
))
1282 (while (and (not (eobp)) (< (point) (max beg end
)))
1284 (if (and (<= (point) (max beg end
)) (not (eobp)))
1287 (delete-region (point) (1- (point)))
1288 (if (not ex-variant
) (fixup-whitespace))))))
1289 ((or (string= com
"right") (string= com
"left"))
1291 (min beg end
) (max beg end
)
1292 (if (string= com
"right") vip-shift-width
(- vip-shift-width
)))
1293 (goto-char (max beg end
))
1295 (vip-forward-char-carefully))))
1301 (if (null ex-addresses
)
1303 (cons (point) nil
)))
1304 (save-window-excursion
1305 (set-buffer vip-ex-work-buf
)
1306 (skip-chars-forward " \t")
1307 (if (looking-at "[a-z]")
1309 (setq char
(following-char))
1311 (skip-chars-forward " \t")
1312 (if (not (looking-at "[\n|]"))
1313 (error "`%s': %s" ex-token vip-SpuriousText
)))
1314 (error "`%s' requires a following letter" ex-token
)))
1316 (goto-char (car ex-addresses
))
1317 (point-to-register (1+ (- char ?a
))))))
1321 ;; Alternate file is the file next to the first one in the buffer ring
1322 (defun ex-next (cycle-other-window &optional find-alt-file
)
1325 (if (not find-alt-file
)
1328 (if (or (char-or-string-p ex-offset
)
1329 (and (not (string= "" ex-file
))
1330 (not (string-match "^[0-9]+$" ex-file
))))
1333 (throw 'ex-edit nil
))
1334 (setq count
(string-to-int ex-file
))
1335 (if (= count
0) (setq count
1))
1336 (if (< count
0) (error "Usage: `next <count>' (count >= 0)"))))
1338 (setq l
(vip-undisplayed-files))
1340 (while (and (not (null l
)) (null (car l
)))
1342 (setq count
(1- count
))
1345 (if find-alt-file
(car l
)
1348 (let* ((w (if cycle-other-window
1349 (get-lru-window) (selected-window)))
1350 (b (window-buffer w
)))
1351 (set-window-buffer w
(get-file-buffer (car l
)))
1353 ;; this puts "next <count>" in the ex-command history
1354 (ex-fixup-history vip-last-ex-prompt ex-file
))
1355 (error "Not that many undisplayed files")))))))
1358 (defun ex-next-related-buffer (direction &optional no-recursion
)
1360 (vip-ring-rotate1 vip-related-files-and-buffers-ring direction
)
1362 (let ((file-or-buffer-name
1363 (vip-current-ring-item vip-related-files-and-buffers-ring
))
1364 (old-ring vip-related-files-and-buffers-ring
)
1365 (old-win (selected-window))
1368 (or (and (ring-p vip-related-files-and-buffers-ring
)
1369 (> (ring-length vip-related-files-and-buffers-ring
) 0))
1370 (error "This buffer has no related files or buffers"))
1372 (or (stringp file-or-buffer-name
)
1374 "File and buffer names must be strings, %S" file-or-buffer-name
))
1376 (setq buf
(cond ((get-buffer file-or-buffer-name
))
1377 ((file-exists-p file-or-buffer-name
)
1378 (find-file-noselect file-or-buffer-name
))
1381 (if (not (vip-buffer-live-p buf
))
1382 (error "Didn't find buffer %S or file %S"
1384 (vip-abbreviate-file-name
1385 (expand-file-name file-or-buffer-name
))))
1387 (if (equal buf
(current-buffer))
1392 (ex-next-related-buffer direction
'norecursion
))))
1397 (if (setq wind
(vip-get-visible-buffer-window buf
))
1399 (setq wind
(get-lru-window (if vip-xemacs-p nil
'visible
)))
1400 (set-window-buffer wind buf
))
1402 (if (vip-window-display-p)
1404 (raise-frame (window-frame wind
))
1405 (if (equal (window-frame wind
) (window-frame old-win
))
1406 (save-window-excursion (select-window wind
) (sit-for 1))
1407 (select-window wind
)))
1408 (save-window-excursion (select-window wind
) (sit-for 1)))
1412 (setq vip-related-files-and-buffers-ring old-ring
))
1414 (setq vip-local-search-start-marker
(point-marker))
1419 (defun ex-preserve ()
1420 (message "Autosaving all buffers that need to be saved...")
1425 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
1427 (setq vip-use-register ex-buffer
)
1429 (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
1431 ;; Ex print working directory
1433 (message default-directory
))
1437 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1439 (set-buffer vip-ex-work-buf
)
1440 (if (looking-at "!") (forward-char 1)))
1441 (if (< vip-expert-level
3)
1442 (save-buffers-kill-emacs)
1443 (kill-buffer (current-buffer))))
1449 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
1452 (vip-add-newline-at-eob-if-necessary)
1453 (if (not (or (bobp) (eobp))) (forward-line 1))
1454 (if (and (not ex-variant
) (string= ex-file
""))
1456 (if (null buffer-file-name
)
1457 (error vip-NoFileSpecified
))
1458 (setq ex-file buffer-file-name
)))
1461 (setq command
(ex-expand-filsyms ex-file
(current-buffer)))
1462 (shell-command command t
))
1463 (insert-file-contents ex-file
)))
1464 (ex-fixup-history vip-last-ex-prompt ex-file
))
1466 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1467 (defun ex-fixup-history (&rest args
)
1468 (setq vip-ex-history
1469 (cons (mapconcat 'identity args
" ") (cdr vip-ex-history
))))
1472 ;; Ex recover from emacs \#file\#
1473 (defun ex-recover ()
1475 (if (or ex-append ex-offset
)
1476 (error "`recover': %s" vip-SpuriousText
))
1477 (if (string= ex-file
"")
1479 (if (null buffer-file-name
)
1480 (error "This buffer isn't visiting any file"))
1481 (setq ex-file buffer-file-name
))
1482 (setq ex-file
(expand-file-name ex-file
)))
1483 (if (and (not (string= ex-file
(buffer-file-name)))
1486 (error "No write since last change \(:rec! overrides\)"))
1487 (recover-file ex-file
))
1489 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1492 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1495 ;; read variable name for ex-set
1496 (defun ex-set-read-variable ()
1497 (let ((minibuffer-local-completion-map
1498 (copy-keymap minibuffer-local-completion-map
))
1499 (cursor-in-echo-area t
)
1502 minibuffer-local-completion-map
" " 'minibuffer-complete-and-exit
)
1503 (define-key minibuffer-local-completion-map
"=" 'exit-minibuffer
)
1504 (if (vip-set-unread-command-events
1505 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil
"\C-m"))
1508 (vip-set-unread-command-events ?\C-m
)))
1509 (message ":set <Variable> [= <Value>]")
1510 (or batch
(sit-for 2))
1512 (while (string-match "^[ \\t\\n]*$"
1514 (completing-read ":set " ex-variable-alist
)))
1515 (message ":set <Variable> ")
1516 ;; if there are unread events, don't wait
1517 (or (vip-set-unread-command-events "") (sit-for 2))
1523 (let ((var (ex-set-read-variable))
1527 (auto-cmd-label "; don't touch or else...")
1528 (delete-turn-on-auto-fill-pattern
1529 "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1530 actual-lisp-cmd lisp-cmd-del-pattern
1533 (cond ((member var
'("ai" "autoindent"))
1534 (setq var
"vip-auto-indent"
1538 ((member var
'("gai" "global-autoindent"))
1539 (kill-local-variable 'vip-auto-indent
)
1540 (setq var
"vip-auto-indent"
1541 set-cmd
"setq-default"
1543 ((member var
'("noai" "noautoindent"))
1544 (setq var
"vip-auto-indent"
1547 ((member var
'("gnoai" "global-noautoindent"))
1548 (kill-local-variable 'vip-auto-indent
)
1549 (setq var
"vip-auto-indent"
1550 set-cmd
"setq-default"
1552 ((member var
'("ic" "ignorecase"))
1553 (setq var
"vip-case-fold-search"
1555 ((member var
'("noic" "noignorecase"))
1556 (setq var
"vip-case-fold-search"
1558 ((member var
'("ma" "magic"))
1559 (setq var
"vip-re-search"
1561 ((member var
'("noma" "nomagic"))
1562 (setq var
"vip-re-search"
1564 ((member var
'("ro" "readonly"))
1565 (setq var
"buffer-read-only"
1567 ((member var
'("noro" "noreadonly"))
1568 (setq var
"buffer-read-only"
1570 ((member var
'("sm" "showmatch"))
1571 (setq var
"blink-matching-paren"
1573 ((member var
'("nosm" "noshowmatch"))
1574 (setq var
"blink-matching-paren"
1576 ((member var
'("ws" "wrapscan"))
1577 (setq var
"vip-search-wrap-around-t"
1579 ((member var
'("nows" "nowrapscan"))
1580 (setq var
"vip-search-wrap-around-t"
1582 (if (eq val
0) ; value must be set by the user
1583 (let ((cursor-in-echo-area t
))
1584 (message (format ":set %s = <Value>" var
))
1585 ;; if there are unread events, don't wait
1586 (or (vip-set-unread-command-events "") (sit-for 2))
1587 (setq val
(read-string (format ":set %s = " var
)))
1588 (ex-fixup-history "set" orig-var val
)
1590 ;; check numerical values
1594 "gts" "global-tabstop"
1597 (or (numberp (setq val2
(car (read-from-string val
))))
1598 (error "%s: Invalid value, numberp, %S" var val
))
1600 (error "%s: Invalid value, numberp, %S" var val
))))
1603 ((member var
'("sw" "shiftwidth"))
1604 (setq var
"vip-shift-width"))
1605 ((member var
'("ts" "tabstop"))
1606 ;; make it take effect in curr buff and new bufs
1607 (setq var
"tab-width"
1610 ((member var
'("gts" "global-tabstop"))
1611 (kill-local-variable 'tab-width
)
1612 (setq var
"tab-width"
1613 set-cmd
"setq-default"))
1614 ((member var
'("wm" "wrapmargin"))
1615 ;; make it take effect in curr buff and new bufs
1616 (kill-local-variable 'fill-column
)
1617 (setq var
"fill-column"
1618 val
(format "(- (window-width) %s)" val
)
1619 set-cmd
"setq-default"))
1620 ((member var
'("sh" "shell"))
1621 (setq var
"explicit-shell-file-name"
1622 val
(format "\"%s\"" val
)))))
1623 (ex-fixup-history "set" orig-var
))
1625 (setq actual-lisp-cmd
(format "\n(%s %s %s) %s"
1626 set-cmd var val auto-cmd-label
))
1627 (setq lisp-cmd-del-pattern
1628 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1629 set-cmd var auto-cmd-label
))
1631 (if (and ask-if-save
1632 (y-or-n-p (format "Do you want to save this setting in %s "
1633 vip-custom-file-name
)))
1635 (vip-save-string-in-file
1636 actual-lisp-cmd vip-custom-file-name
1638 lisp-cmd-del-pattern
)
1639 (if (string= var
"fill-column")
1641 (vip-save-string-in-file
1643 "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
1645 vip-custom-file-name
1646 delete-turn-on-auto-fill-pattern
)
1647 (vip-save-string-in-file
1648 nil vip-custom-file-name delete-turn-on-auto-fill-pattern
)
1649 (vip-save-string-in-file
1650 nil vip-custom-file-name
1652 lisp-cmd-del-pattern
)
1656 (message (format "%s %s %s" set-cmd var
(if (string-match "^[ \t]*$" val
)
1659 (eval (car (read-from-string actual-lisp-cmd
)))
1660 (if (string= var
"fill-column")
1663 (auto-fill-mode -
1)))
1667 ;; In inline args, skip regex-forw and (optionally) chars-back.
1668 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1670 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str
)
1672 (set-buffer vip-ex-work-buf
)
1673 (goto-char (point-min))
1674 (re-search-forward regex-forw nil t
)
1677 (goto-char (point-max))
1679 (skip-chars-backward chars-back
)
1680 (skip-chars-backward " \t\n\C-m"))
1682 ;; replace SPC with `=' to suppress the special meaning SPC has
1686 (while (re-search-forward " +" nil t
)
1687 (replace-match replace-str nil t
)
1688 (vip-forward-char-carefully)))
1690 (buffer-substring beg end
))))
1697 ;; Viper help. Invokes Info
1701 (pop-to-buffer (get-buffer-create "*info*"))
1702 (info (if vip-xemacs-p
"viper.info" "viper"))
1703 (message "Type `i' to search for a specific topic"))
1705 (with-output-to-temp-buffer " *vip-info*"
1707 The Info file for Viper does not seem to be installed.
1709 This file is part of the standard distribution of %sEmacs.
1710 Please contact your system administrator. "
1711 (if vip-xemacs-p
"X" "")
1714 ;; Ex source command. Loads the file specified as argument or `~/.vip'
1717 (if (string= ex-file
"")
1718 (load vip-custom-file-name
)
1721 ;; Ex substitute command
1722 ;; If REPEAT use previous regexp which is ex-reg-exp or vip-s-string
1723 (defun ex-substitute (&optional repeat r-flag
)
1727 (case-fold-search vip-case-fold-search
)
1729 (if repeat
(setq ex-token nil
) (setq delim
(vip-get-ex-pat)))
1732 (setq pat
(if r-flag vip-s-string ex-reg-exp
))
1734 (error "No previous pattern to use in substitution"))
1736 delim
(string-to-char pat
)))
1737 (setq pat
(if (string= ex-token
"") vip-s-string ex-token
))
1738 (setq vip-s-string pat
1740 (setq delim
(vip-get-ex-pat))
1746 (while (vip-get-ex-opt-gc delim
)
1747 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
1751 (if ex-addresses
(goto-char (car ex-addresses
)))
1753 (forward-line (1- ex-count
))
1754 (setq ex-addresses
(cons (point) (cons (mark t
) nil
))))
1755 (if (null ex-addresses
)
1756 (setq ex-addresses
(cons (point) (cons (point) nil
)))
1757 (if (null (cdr ex-addresses
))
1758 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
1760 (let ((beg (car ex-addresses
))
1761 (end (car (cdr ex-addresses
)))
1764 (vip-enlarge-region beg end
)
1765 (let ((limit (save-excursion
1766 (goto-char (max (point) (mark t
)))
1768 (goto-char (min (point) (mark t
)))
1769 (while (< (point) limit
)
1771 (setq eol-mark
(point-marker))
1775 (while (and (not (eolp))
1776 (re-search-forward pat eol-mark t
))
1777 (if (or (not opt-c
) (y-or-n-p "Replace? "))
1779 (setq matched-pos
(point))
1780 (if (not (stringp repl
))
1781 (error "Can't perform Ex substitution: No previous replacement pattern"))
1782 (replace-match repl t
))))
1784 (vip-forward-char-carefully))
1787 "Can't repeat Ex substitution: No previous regular expression"))
1788 (if (and (re-search-forward pat eol-mark t
)
1789 (or (not opt-c
) (y-or-n-p "Replace? ")))
1791 (setq matched-pos
(point))
1792 (if (not (stringp repl
))
1793 (error "Can't perform Ex substitution: No previous replacement pattern"))
1794 (replace-match repl t
)))
1796 (vip-forward-char-carefully))))))
1797 (if matched-pos
(goto-char matched-pos
))
1799 (if opt-c
(message "done"))))
1804 (save-window-excursion
1805 (set-buffer vip-ex-work-buf
)
1806 (skip-chars-forward " \t")
1808 (skip-chars-forward "^ |\t\n")
1809 (setq tag
(buffer-substring (mark t
) (point))))
1810 (if (not (string= tag
"")) (setq ex-tag tag
))
1811 (vip-change-state-to-emacs)
1812 (condition-case conds
1814 (if (string= tag
"")
1816 (find-tag-other-window ex-tag
))
1817 (vip-change-state-to-vi))
1819 (vip-change-state-to-vi)
1820 (vip-message-conditions conds
)))))
1823 (defun ex-write (q-flag)
1824 (vip-default-ex-addresses t
)
1826 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
)))
1827 temp-buf writing-same-file region
1828 file-exists writing-whole-file
)
1829 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1832 (vip-enlarge-region beg end
)
1833 (shell-command-on-region (point) (mark t
) ex-file
))
1834 (if (and (string= ex-file
"") (not (buffer-file-name)))
1837 (format "Buffer %s isn't visiting any file. File to save in: "
1840 (setq writing-whole-file
(and (= (point-min) beg
) (= (point-max) end
))
1841 ex-file
(if (string= ex-file
"")
1843 (expand-file-name ex-file
)))
1844 ;; if ex-file is a directory use the file portion of the buffer file name
1845 (if (and (file-directory-p ex-file
)
1847 (not (file-directory-p buffer-file-name
)))
1849 (concat ex-file
(file-name-nondirectory buffer-file-name
))))
1851 (setq file-exists
(file-exists-p ex-file
)
1852 writing-same-file
(string= ex-file
(buffer-file-name)))
1854 (if (and writing-whole-file writing-same-file
)
1855 (if (not (buffer-modified-p))
1856 (message "(No changes need to be saved)")
1858 (ex-write-info file-exists ex-file beg end
))
1859 ;; writing some other file or portion of the currents
1860 ;; file---create temp buffer for it
1861 ;; disable undo in that buffer, for efficiency
1862 (buffer-disable-undo (setq temp-buf
(create-file-buffer ex-file
)))
1865 (if (and file-exists
1866 (not writing-same-file
)
1868 (format "File %s exists. Overwrite? " ex-file
))))
1870 (vip-enlarge-region beg end
)
1871 (setq region
(buffer-substring (point) (mark t
)))
1872 (set-buffer temp-buf
)
1873 (set-visited-file-name ex-file
)
1875 (if (and file-exists ex-append
)
1876 (insert-file-contents ex-file
))
1877 (goto-char (point-max))
1880 (ex-write-info file-exists ex-file
(point-min) (point-max))
1882 (set-buffer temp-buf
)
1883 (set-buffer-modified-p nil
)
1884 (kill-buffer temp-buf
)
1887 ;; this prevents the loss of data if writing part of the buffer
1888 (if (and (buffer-file-name) writing-same-file
)
1889 (set-visited-file-modtime))
1890 (or writing-whole-file
1891 (not writing-same-file
)
1892 (set-buffer-modified-p t
))
1894 (if (< vip-expert-level
2)
1895 (save-buffers-kill-emacs)
1896 (kill-buffer (current-buffer))))
1900 (defun ex-write-info (exists file-name beg end
)
1901 (message "`%s'%s %d lines, %d characters"
1902 (vip-abbreviate-file-name file-name
)
1903 (if exists
"" " [New file]")
1904 (count-lines beg
(min (1+ end
) (point-max)))
1909 (vip-default-ex-addresses)
1911 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1912 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1914 (vip-enlarge-region beg end
)
1915 (exchange-point-and-mark)
1916 (if (or ex-g-flag ex-g-variant
)
1917 (error "Can't execute `yank' within `global'"))
1921 (forward-line (1- ex-count
)))
1923 (vip-enlarge-region (point) (mark t
))
1924 (if ex-flag
(error "`yank': %s" vip-SpuriousText
))
1926 (cond ((vip-valid-register ex-buffer
'(Letter))
1927 (vip-append-to-register
1928 (downcase ex-buffer
) (point) (mark t
)))
1929 ((vip-valid-register ex-buffer
)
1930 (copy-to-register ex-buffer
(point) (mark t
) nil
))
1931 (t (error vip-InvalidRegister ex-buffer
))))
1932 (copy-region-as-kill (point) (mark t
)))))
1934 ;; Execute shell command
1935 (defun ex-command ()
1937 (save-window-excursion
1938 (set-buffer vip-ex-work-buf
)
1939 (skip-chars-forward " \t")
1940 (setq command
(buffer-substring (point) (point-max)))
1942 (setq command
(ex-expand-filsyms command
(current-buffer)))
1943 (if (and (> (length command
) 0) (string= "!" (substring command
0 1)))
1944 (if vip-ex-last-shell-com
1945 (setq command
(concat vip-ex-last-shell-com
(substring command
1)))
1946 (error "No previous shell command")))
1947 (setq vip-ex-last-shell-com command
)
1948 (if (null ex-addresses
)
1949 (shell-command command
)
1950 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1951 (if (null beg
) (setq beg end
))
1955 (vip-enlarge-region (point) (mark t
))
1956 (shell-command-on-region (point) (mark t
) command t
))
1959 ;; Print line number
1960 (defun ex-line-no ()
1964 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
1966 ;; Give information on the file visited by the current buffer
1967 (defun vip-info-on-file ()
1969 (let ((pos1 (vip-line-pos 'start
))
1970 (pos2 (vip-line-pos 'end
))
1972 (setq lines
(count-lines (point-min) (vip-line-pos 'end
))
1973 file
(if (buffer-file-name)
1974 (concat (vip-abbreviate-file-name (buffer-file-name)) ":")
1975 (concat (buffer-name) " [Not visiting any file]:"))
1976 info
(format "line=%d/%d pos=%d/%d col=%d %s"
1980 (count-lines (point-min) (point-max))
1981 (point) (1- (point-max))
1982 (1+ (current-column))
1983 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
1984 (if (< (+ 1 (length info
) (length file
))
1985 (window-width (minibuffer-window)))
1986 (message (concat file
" " info
))
1987 (save-window-excursion
1988 (with-output-to-temp-buffer " *vip-info*"
1991 "\n\n\nPress any key to continue...\n\n")))
1993 (kill-buffer " *vip-info*")))
1999 ;;; viper-ex.el ends here