1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
27 (defvar read-file-name-map
)
28 (defvar vip-use-register
)
30 (defvar vip-shift-width
)
31 (defvar vip-ex-history
)
32 (defvar vip-related-files-and-buffers-ring
)
33 (defvar vip-local-search-start-marker
)
34 (defvar vip-expert-level
)
35 (defvar vip-custom-file-name
)
36 (defvar vip-case-fold-search
)
38 ;; loading happens only in non-interactive compilation
39 ;; in order to spare non-viperized emacs from being viperized
42 (let ((load-path (cons (expand-file-name ".") load-path
)))
43 (or (featurep 'viper-util
)
44 (load "viper-util.el" nil nil
'nosuffix
))
45 (or (featurep 'viper-keym
)
46 (load "viper-keym.el" nil nil
'nosuffix
))
47 (or (featurep 'viper-cmd
)
48 (load "viper-cmd.el" nil nil
'nosuffix
))
57 (defconst vip-ex-work-buf-name
" *ex-working-space*")
58 (defconst vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
59 (defconst vip-ex-tmp-buf-name
" *ex-tmp*")
62 ;;; Variable completion in :set command
64 ;; The list of Ex commands. Used for completing command names.
65 (defconst ex-token-alist
66 '(("!") ("=") (">") ("&") ("~")
67 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
68 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
69 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
70 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
71 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
72 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
73 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
74 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
76 ;; A-list of Ex variables that can be set using the :set command.
77 (defconst ex-variable-alist
78 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
79 ("global-tabstop") ("gts") ("tabstop") ("ts")
80 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
82 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
83 ("noreadonly") ("noro") ("nomagic") ("noma")
84 ("noignorecase") ("noic")
85 ("global-noautoindent") ("gnoai") ("noautoindent") ("noai")
86 ("magic") ("ma") ("ignorecase") ("ic")
87 ("global-autoindent") ("gai") ("autoindent") ("ai")
92 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
96 ;; If non-nil, gives type of address; if nil, it is a command.
97 (defvar ex-token-type nil
)
99 ;; List of addresses passed to Ex command
100 (defvar ex-addresses nil
)
102 ;; It seems that this flag is used only for `#', `print', and `list', which
103 ;; aren't implemented. Check later.
106 ;; "buffer" where Ex commands keep deleted data.
107 ;; In Emacs terms, this is a register.
108 (defvar ex-buffer nil
)
110 ;; Value of ex count.
111 (defvar ex-count nil
)
113 ;; Flag for global command.
114 (defvar ex-g-flag nil
)
116 ;; If t, global command is executed on lines not matching ex-g-pat.
117 (defvar ex-g-variant nil
)
119 ;; Save reg-exp used in substitute.
120 (defvar ex-reg-exp nil
)
123 ;; Replace pattern for substitute.
126 ;; Pattern for global command.
127 (defvar ex-g-pat nil
)
130 (defvar ex-unix-type-shell
131 (let ((case-fold-search t
))
132 (and (stringp shell-file-name
)
142 "[^a-z]sh$\\|[^a-z]sh.exe$"
147 "Is the user using a unix-type shell?")
149 (defvar ex-unix-type-shell-options
150 (let ((case-fold-search t
))
151 (if ex-unix-type-shell
152 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name
)
153 "-f") ; csh: do it fast
154 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name
)
155 "-noprofile") ; bash: ignore .profile
157 "Options to pass to the Unix-style shell.
158 Don't put `-c' here, as it is added automatically.")
160 (defvar ex-nontrivial-find-file-function
161 (cond (ex-unix-type-shell 'vip-ex-nontrivial-find-file-unix
)
162 ((eq system-type
'emx
) 'vip-ex-nontrivial-find-file-ms
) ; OS/2
163 (vip-ms-style-os-p 'vip-ex-nontrivial-find-file-ms
) ; a Microsoft OS
164 (vip-vms-os-p 'vip-ex-nontrivial-find-file-unix
) ; VMS
165 (t 'vip-ex-nontrivial-find-file-unix
) ; presumably UNIX
168 ;; Remembers the previous Ex tag.
171 ;; file used by Ex commands like :r, :w, :n
174 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
175 (defvar ex-variant nil
)
177 ;; Specified the offset of an Ex command, such as :read.
178 (defvar ex-offset nil
)
180 ;; Tells Ex that this is a w>> command.
181 (defvar ex-append nil
)
183 ;; File containing the shell command to be executed at Ex prompt,
185 (defvar ex-cmdfile nil
)
187 ;; flag used in vip-ex-read-file-name to indicate that we may be reading
188 ;; multiple file names. Used for :edit and :next
189 (defvar vip-keep-reading-filename nil
)
191 (defconst ex-cycle-other-window t
192 "*If t, :n and :b cycles through files and buffers in other window.
193 Then :N and :B cycles in the current window. If nil, this behavior is
196 (defconst ex-cycle-through-non-files nil
197 "*Cycle through *scratch* and other buffers that don't visit any file.")
199 ;; Last shell command executed with :! command.
200 (defvar vip-ex-last-shell-com nil
)
202 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
203 (defvar vip-incomplete-ex-cmd nil
)
205 ;; Remembers the last ex-command prompt.
206 (defvar vip-last-ex-prompt
"")
211 ;; Check if ex-token is an initial segment of STR
212 (defun vip-check-sub (str)
213 (let ((length (length ex-token
)))
214 (if (and (<= length
(length str
))
215 (string= ex-token
(substring str
0 length
)))
217 (setq ex-token-type
'non-command
))))
219 ;; Get a complete ex command
220 (defun vip-get-ex-com-subr ()
221 (let (case-fold-search)
223 (re-search-forward "[a-zA-Z][a-zA-Z]*")
224 (setq ex-token-type
'command
)
225 (setq ex-token
(buffer-substring (point) (mark t
)))
226 (exchange-point-and-mark)
227 (cond ((looking-at "a")
228 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
229 ((looking-at "ar") (vip-check-sub "args"))
230 (t (vip-check-sub "append"))))
231 ((looking-at "h") (vip-check-sub "help"))
233 (cond ((looking-at "cd") (vip-check-sub "cd"))
234 ((looking-at "ch") (vip-check-sub "chdir"))
235 ((looking-at "co") (vip-check-sub "copy"))
236 (t (vip-check-sub "change"))))
237 ((looking-at "d") (vip-check-sub "delete"))
238 ((looking-at "b") (vip-check-sub "buffer"))
239 ((looking-at "B") (vip-check-sub "Buffer"))
241 (if (looking-at "ex") (vip-check-sub "ex")
242 (vip-check-sub "edit")))
243 ((looking-at "f") (vip-check-sub "file"))
244 ((looking-at "g") (vip-check-sub "global"))
245 ((looking-at "i") (vip-check-sub "insert"))
246 ((looking-at "j") (vip-check-sub "join"))
247 ((looking-at "l") (vip-check-sub "list"))
249 (cond ((looking-at "map") (vip-check-sub "map"))
250 ((looking-at "mar") (vip-check-sub "mark"))
251 (t (vip-check-sub "move"))))
252 ((looking-at "k[a-z][^a-z]")
253 (setq ex-token
"kmark")
255 (exchange-point-and-mark)) ; this is canceled out by another
256 ; exchange-point-and-mark at the end
257 ((looking-at "k") (vip-check-sub "kmark"))
258 ((looking-at "n") (if (looking-at "nu")
259 (vip-check-sub "number")
260 (vip-check-sub "next")))
261 ((looking-at "N") (vip-check-sub "Next"))
262 ((looking-at "o") (vip-check-sub "open"))
264 (cond ((looking-at "pre") (vip-check-sub "preserve"))
265 ((looking-at "pu") (vip-check-sub "put"))
266 ((looking-at "pw") (vip-check-sub "pwd"))
267 (t (vip-check-sub "print"))))
268 ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
269 ((looking-at "R") (vip-check-sub "RelatedFile"))
270 ((looking-at "q") (vip-check-sub "quit"))
272 (cond ((looking-at "rec") (vip-check-sub "recover"))
273 ((looking-at "rew") (vip-check-sub "rewind"))
274 (t (vip-check-sub "read"))))
276 (cond ((looking-at "se") (vip-check-sub "set"))
277 ((looking-at "sh") (vip-check-sub "shell"))
278 ((looking-at "so") (vip-check-sub "source"))
279 ((looking-at "sr") (vip-check-sub "sr"))
280 ((looking-at "st") (vip-check-sub "stop"))
281 ((looking-at "sus") (vip-check-sub "suspend"))
282 ((looking-at "subm") (vip-check-sub "submitReport"))
283 (t (vip-check-sub "substitute"))))
285 (if (looking-at "ta") (vip-check-sub "tag")
286 (vip-check-sub "transfer")))
288 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
289 ((looking-at "unm") (vip-check-sub "unmap"))
290 (t (vip-check-sub "undo"))))
292 (cond ((looking-at "ve") (vip-check-sub "version"))
293 ((looking-at "vi") (vip-check-sub "visual"))
294 (t (vip-check-sub "vglobal"))))
296 (if (looking-at "wq") (vip-check-sub "wq")
297 (vip-check-sub "write")))
299 (if (looking-at "WW")
300 (vip-check-sub "WWrite")
301 (vip-check-sub "Write")))
302 ((looking-at "x") (vip-check-sub "xit"))
303 ((looking-at "y") (vip-check-sub "yank"))
304 ((looking-at "z") (vip-check-sub "z")))
305 (exchange-point-and-mark)
308 ;; Get an ex-token which is either an address or a command.
309 ;; A token has a type, \(command, address, end-mark\), and a value
310 (defun vip-get-ex-token ()
311 (save-window-excursion
312 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
313 (set-buffer vip-ex-work-buf
)
314 (skip-chars-forward " \t|")
315 (cond ((looking-at "#")
316 (setq ex-token-type
'command
)
317 (setq ex-token
(char-to-string (following-char)))
319 ((looking-at "[a-z]") (vip-get-ex-com-subr))
322 (setq ex-token-type
'dot
))
323 ((looking-at "[0-9]")
325 (re-search-forward "[0-9]*")
327 (cond ((eq ex-token-type
'plus
) 'add-number
)
328 ((eq ex-token-type
'minus
) 'sub-number
)
330 (setq ex-token
(string-to-int (buffer-substring (point) (mark t
)))))
333 (setq ex-token-type
'end
))
336 (setq ex-token-type
'whole
))
338 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
342 (setq ex-token-type
'plus
))
343 ((looking-at "+[0-9]")
345 (setq ex-token-type
'plus
))
347 (error vip-BadAddress
))))
349 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
353 (setq ex-token-type
'minus
))
354 ((looking-at "-[0-9]")
356 (setq ex-token-type
'minus
))
358 (error vip-BadAddress
))))
363 (while (and (not (eolp)) cont
)
364 ;;(re-search-forward "[^/]*/")
365 (re-search-forward "[^/]*\\(/\\|\n\\)")
366 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
369 (setq ex-token
(buffer-substring (point) (mark t
)))
370 (if (looking-at "/") (forward-char 1))
371 (setq ex-token-type
'search-forward
))
376 (while (and (not (eolp)) cont
)
377 ;;(re-search-forward "[^\\?]*\\?")
378 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
379 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
382 (if (not (looking-at "\n")) (forward-char 1))))
383 (setq ex-token-type
'search-backward
)
384 (setq ex-token
(buffer-substring (1- (point)) (mark t
))))
387 (setq ex-token-type
'comma
))
390 (setq ex-token-type
'semi-colon
))
391 ((looking-at "[!=><&~]")
392 (setq ex-token-type
'command
)
393 (setq ex-token
(char-to-string (following-char)))
396 (setq ex-token-type
'goto-mark
)
398 (cond ((looking-at "'") (setq ex-token nil
))
399 ((looking-at "[a-z]") (setq ex-token
(following-char)))
400 (t (error "Marks are ' and a-z")))
403 (setq ex-token-type
'end-mark
)
404 (setq ex-token
"goto"))
406 (error vip-BadExCommand
)))))
408 ;; Reads Ex command. Tries to determine if it has to exit because command
409 ;; is complete or invalid. If not, keeps reading command.
410 (defun ex-cmd-read-exit ()
412 (setq vip-incomplete-ex-cmd t
)
413 (let ((quit-regex1 (concat
416 "\\|" "[nN]ext[ \t]*"
429 "\\|" "[ktgjmsz][ \t]*$"
431 "\\|" "tr[ansfer \t]*"
434 "\\|" "^[ \t]*k?ma[^p]*"
443 "\\|" "![ \t]*[a-zA-Z].*"
447 (save-window-excursion ;; put cursor at the end of the Ex working buffer
448 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
449 (set-buffer vip-ex-work-buf
)
450 (goto-char (point-max)))
451 (cond ((vip-looking-back quit-regex1
) (exit-minibuffer))
452 ((vip-looking-back stay-regex
) (insert " "))
453 ((vip-looking-back quit-regex2
) (exit-minibuffer))
456 ;; complete Ex command
457 (defun ex-cmd-complete ()
459 (let (save-pos dist compl-list string-to-complete completion-result
)
462 (setq dist
(skip-chars-backward "[a-zA-Z!=>&~]")
466 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
468 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
469 ;; Preceding characters are not the ones allowed in an Ex command
470 ;; or we have typed past command name.
471 ;; Note: we didn't do parsing, so there may be surprises.
472 (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
473 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
474 (looking-at "[^ \t\n\C-m]"))
476 (with-output-to-temp-buffer "*Completions*"
477 (display-completion-list
478 (vip-alist-to-list ex-token-alist
))))
479 ;; Preceding chars may be part of a command name
480 (setq string-to-complete
(buffer-substring save-pos
(point)))
481 (setq completion-result
482 (try-completion string-to-complete ex-token-alist
))
484 (cond ((eq completion-result t
) ; exact match--do nothing
485 (vip-tmp-insert-at-eob " (Sole completion)"))
486 ((eq completion-result nil
)
487 (vip-tmp-insert-at-eob " (No match)"))
488 (t ;; partial completion
490 (delete-region (point) (point-max))
491 (insert completion-result
)
492 (let (case-fold-search)
494 (vip-filter-alist (concat "^" completion-result
)
496 (if (> (length compl-list
) 1)
497 (with-output-to-temp-buffer "*Completions*"
498 (display-completion-list
499 (vip-alist-to-list (reverse compl-list
)))))))
504 ;; Ex commands themselves are implemented in viper-ex.el
505 (defun vip-ex (&optional string
)
510 (let* ((map (copy-keymap minibuffer-local-map
))
514 prev-token-type com-str
)
516 (vip-add-keymap vip-ex-cmd-map map
)
518 (setq com-str
(or string
(vip-read-string-with-history
524 (save-window-excursion
526 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
527 (set-buffer vip-ex-work-buf
)
528 (delete-region (point-min) (point-max))
529 (insert com-str
"\n")
530 (goto-char (point-min)))
531 (setq ex-token-type nil
535 (cond ((memq ex-token-type
'(command end-mark
))
536 (if address
(setq ex-addresses
(cons address ex-addresses
)))
537 (cond ((string= ex-token
"global")
540 ((string= ex-token
"vglobal")
544 (vip-execute-ex-command)
545 (save-window-excursion
546 (setq vip-ex-work-buf
547 (get-buffer-create vip-ex-work-buf-name
))
548 (set-buffer vip-ex-work-buf
)
549 (skip-chars-forward " \t")
550 (cond ((looking-at "|")
554 (t (error "`%s': %s" ex-token vip-SpuriousText
)))
557 ((eq ex-token-type
'non-command
)
558 (error "`%s': %s" ex-token vip-BadExCommand
))
559 ((eq ex-token-type
'whole
)
563 (cons (point-max) ex-addresses
)
564 (cons (point-max) (cons (point-min) ex-addresses
)))))
565 ((eq ex-token-type
'comma
)
566 (if (eq prev-token-type
'whole
)
567 (setq address
(point-min)))
569 (cons (if (null address
) (point) address
) ex-addresses
)))
570 ((eq ex-token-type
'semi-colon
)
571 (if (eq prev-token-type
'whole
)
572 (setq address
(point-min)))
573 (if address
(setq dot address
))
575 (cons (if (null address
) (point) address
) ex-addresses
)))
576 (t (let ((ans (vip-get-ex-address-subr address dot
)))
577 (if ans
(setq address ans
)))))
578 (setq prev-token-type ex-token-type
))))
581 ;; Get a regular expression and set `ex-variant', if found
582 (defun vip-get-ex-pat ()
583 (save-window-excursion
584 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
585 (set-buffer vip-ex-work-buf
)
586 (skip-chars-forward " \t")
589 (setq ex-g-variant
(not ex-g-variant
)
590 ex-g-flag
(not ex-g-flag
))
592 (skip-chars-forward " \t")))
593 (let ((c (following-char)))
594 (if (string-match "[0-9A-Za-z]" (format "%c" c
))
596 "Global regexp must be inside matching non-alphanumeric chars"))
597 (if (looking-at "[^\\\\\n]")
602 (while (and (not (eolp)) cont
)
603 (if (not (re-search-forward (format "[^%c]*%c" c c
) nil t
))
604 (if (member ex-token
'("global" "vglobal"))
606 "Missing closing delimiter for global regexp")
607 (goto-char (point-max))))
608 (if (not (vip-looking-back
609 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c
)))
612 (if (= (mark t
) (point)) ""
613 (buffer-substring (1- (point)) (mark t
))))
619 (defun vip-get-ex-command ()
620 (save-window-excursion
621 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
622 (set-buffer vip-ex-work-buf
)
623 (if (looking-at "/") (forward-char 1))
624 (skip-chars-forward " \t")
625 (cond ((looking-at "[a-z]")
626 (vip-get-ex-com-subr)
627 (if (eq ex-token-type
'non-command
)
628 (error "`%s': %s" ex-token vip-BadExCommand
)))
629 ((looking-at "[!=><&~]")
630 (setq ex-token
(char-to-string (following-char)))
632 (t (error vip-BadExCommand
)))))
634 ;; Get an Ex option g or c
635 (defun vip-get-ex-opt-gc (c)
636 (save-window-excursion
637 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
638 (set-buffer vip-ex-work-buf
)
639 (if (looking-at (format "%c" c
)) (forward-char 1))
640 (skip-chars-forward " \t")
641 (cond ((looking-at "g")
651 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
652 (defun vip-default-ex-addresses (&optional whole-flag
)
653 (cond ((null ex-addresses
)
656 (cons (point-max) (cons (point-min) nil
))
657 (cons (point) (cons (point) nil
)))))
658 ((null (cdr ex-addresses
))
660 (cons (car ex-addresses
) ex-addresses
)))))
662 ;; Get an ex-address as a marker and set ex-flag if a flag is found
663 (defun vip-get-ex-address ()
664 (let ((address (point-marker))
670 (cond ((eq ex-token-type
'command
)
671 (if (member ex-token
'("print" "list" "#"))
675 (error "Address expected in this Ex command")))
676 ((eq ex-token-type
'end-mark
)
678 ((eq ex-token-type
'whole
)
679 (error "Trailing address expected"))
680 ((eq ex-token-type
'comma
)
681 (error "`%s': %s" ex-token vip-SpuriousText
))
682 (t (let ((ans (vip-get-ex-address-subr address
(point-marker))))
683 (if ans
(setq address ans
))))))
686 ;; Returns an address as a point
687 (defun vip-get-ex-address-subr (old-address dot
)
689 (if (null old-address
) (setq old-address dot
))
690 (cond ((eq ex-token-type
'dot
)
692 ((eq ex-token-type
'add-number
)
694 (goto-char old-address
)
695 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
696 (setq address
(point-marker))))
697 ((eq ex-token-type
'sub-number
)
699 (goto-char old-address
)
700 (forward-line (- ex-token
))
701 (setq address
(point-marker))))
702 ((eq ex-token-type
'abs-number
)
704 (goto-char (point-min))
705 (if (= ex-token
0) (setq address
0)
706 (forward-line (1- ex-token
))
707 (setq address
(point-marker)))))
708 ((eq ex-token-type
'end
)
709 (setq address
(point-max-marker)))
710 ((eq ex-token-type
'plus
) t
) ; do nothing
711 ((eq ex-token-type
'minus
) t
) ; do nothing
712 ((eq ex-token-type
'search-forward
)
714 (ex-search-address t
)
715 (setq address
(point-marker))))
716 ((eq ex-token-type
'search-backward
)
718 (ex-search-address nil
)
719 (setq address
(point-marker))))
720 ((eq ex-token-type
'goto-mark
)
723 (exchange-point-and-mark)
724 (goto-char (vip-register-to-point
725 (1+ (- ex-token ?a
)) 'enforce-buffer
)))
726 (setq address
(point-marker)))))
730 ;; Search pattern and set address
731 (defun ex-search-address (forward)
732 (if (string= ex-token
"")
733 (if (null vip-s-string
)
734 (error vip-NoPrevSearch
)
735 (setq ex-token vip-s-string
))
736 (setq vip-s-string ex-token
))
740 (re-search-forward ex-token
))
742 (re-search-backward ex-token
)))
744 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
745 (defun vip-get-ex-buffer ()
749 (save-window-excursion
750 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
751 (set-buffer vip-ex-work-buf
)
752 (skip-chars-forward " \t")
753 (if (looking-at "[a-zA-Z]")
755 (setq ex-buffer
(following-char))
757 (skip-chars-forward " \t")))
758 (if (looking-at "[0-9]")
761 (re-search-forward "[0-9][0-9]*")
762 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
763 (skip-chars-forward " \t")))
764 (if (looking-at "[pl#]")
768 (if (not (looking-at "[\n|]"))
769 (error "`%s': %s" ex-token vip-SpuriousText
))))
771 (defun vip-get-ex-count ()
775 (save-window-excursion
776 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
777 (set-buffer vip-ex-work-buf
)
778 (skip-chars-forward " \t")
783 (skip-chars-forward " \t")
784 (if (looking-at "[0-9]")
787 (re-search-forward "[0-9][0-9]*")
788 (setq ex-count
(string-to-int (buffer-substring (point) (mark t
))))
789 (skip-chars-forward " \t")))
790 (if (looking-at "[pl#]")
794 (if (not (looking-at "[\n|]"))
796 (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand
))))
798 ;; Expand \% and \# in ex command
799 (defun ex-expand-filsyms (cmd buf
)
803 (setq cf buffer-file-name
)
804 (setq pf
(ex-next nil t
))) ; this finds alternative file name
805 (if (and (null cf
) (string-match "[^\\]%\\|\\`%" cmd
))
806 (error "No current file to substitute for `%%'"))
807 (if (and (null pf
) (string-match "[^\\]#\\|\\`#" cmd
))
808 (error "No alternate file to substitute for `#'"))
810 (set-buffer (get-buffer-create vip-ex-tmp-buf-name
))
813 (goto-char (point-min))
814 (while (re-search-forward "%\\|#" nil t
)
815 (let ((data (match-data))
816 (char (buffer-substring (match-beginning 0) (match-end 0))))
817 (if (vip-looking-back (concat "\\\\" char
))
819 (store-match-data data
)
820 (if (string= char
"%")
822 (replace-match pf
)))))
824 (setq ret
(buffer-substring (point-min) (point)))
828 ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
829 (defun vip-get-ex-file ()
837 (save-window-excursion
838 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
839 (set-buffer vip-ex-work-buf
)
840 (skip-chars-forward " \t")
842 (if (and (not (vip-looking-back "[ \t]"))
843 ;; read doesn't have a corresponding :r! form, so ! is
844 ;; immediately interpreted as a shell command.
845 (not (string= ex-token
"read")))
849 (skip-chars-forward " \t"))
852 (skip-chars-forward " \t")))
853 (if (looking-at ">>")
858 (skip-chars-forward " \t")))
863 (re-search-forward "[ \t\n]")
865 (setq ex-offset
(buffer-substring (point) (mark t
)))
867 (skip-chars-forward " \t")))
868 ;; this takes care of :r, :w, etc., when they get file names
869 ;; from the history list
870 (if (member ex-token
'("read" "write" "edit" "visual" "next"))
872 (setq ex-file
(buffer-substring (point) (1- (point-max))))
874 ;; For :e, match multiple non-white strings separated
875 ;; by white. For others, find the first non-white string
877 (if (string= ex-token
"edit")
878 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
882 ;; if file name comes from history, don't leave
883 ;; minibuffer when the user types space
884 (setq vip-incomplete-ex-cmd nil
)
885 ;; this must be the last clause in this progn
886 (substring ex-file
(match-beginning 0) (match-end 0))
889 ;; this leaves only the command name in the work area
890 ;; file names are gone
891 (delete-region (point) (1- (point-max)))
893 (goto-char (point-max))
894 (skip-chars-backward " \t\n")
895 (setq prompt
(buffer-substring (point-min) (point)))
898 (setq vip-last-ex-prompt prompt
)
900 ;; If we just finished reading command, redisplay prompt
901 (if vip-incomplete-ex-cmd
902 (setq ex-file
(vip-ex-read-file-name (format ":%s " prompt
)))
903 ;; file was typed in-line
904 (setq ex-file
(or ex-file
"")))
908 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
909 ;; file names, arranges to re-enter the minibuffer.
910 (defun vip-complete-filename-or-exit ()
912 (setq vip-keep-reading-filename t
)
913 ;; don't exit if directory---ex-commands don't
914 (cond ((ex-cmd-accepts-multiple-files-p ex-token
) (exit-minibuffer))
915 ;; apparently the argument to an Ex command is
916 ;; supposed to be a shell command
917 ((vip-looking-back "^[ \t]*!.*")
921 (setq ex-cmdfile nil
)
922 (minibuffer-complete-word))))
924 (defun vip-handle-! ()
927 (buffer-string) (vip-abbreviate-file-name default-directory
))
928 (member ex-token
'("read" "write")))
932 (defun ex-cmd-accepts-multiple-files-p (token)
933 (member token
'("edit" "next" "Next")))
935 ;; If user doesn't enter anything, then "" is returned, i.e., the
936 ;; prompt-directory is not returned.
937 (defun vip-ex-read-file-name (prompt)
939 (minibuffer-local-completion-map
940 (copy-keymap minibuffer-local-completion-map
))
943 (vip-add-keymap ex-read-filename-map
945 minibuffer-local-completion-map
948 (setq cont
(setq vip-keep-reading-filename t
))
950 (setq vip-keep-reading-filename nil
951 val
(read-file-name (concat prompt str
) nil default-directory
))
952 (if (string-match " " val
)
953 (setq val
(concat "\\\"" val
"\\\"")))
954 (setq str
(concat str
(if (equal val
"") "" " ")
955 val
(if (equal val
"") "" " ")))
957 ;; Only edit, next, and Next commands accept multiple files.
958 ;; vip-keep-reading-filename is set in the anonymous function that is
959 ;; bound to " " in ex-read-filename-map.
960 (setq cont
(and vip-keep-reading-filename
961 (ex-cmd-accepts-multiple-files-p ex-token
)))
964 (setq beg
(string-match "[^ \t]" str
) ; delete leading blanks
965 end
(string-match "[ \t]*$" str
)) ; delete trailing blanks
966 (if (member ex-token
'("read" "write"))
967 (if (string-match "[\t ]*!" str
)
968 ;; this is actually a shell command
972 (setq vip-last-ex-prompt
(concat vip-last-ex-prompt
" !")))))
973 (substring str
(or beg
0) end
)))
975 ;; Execute ex command using the value of addresses
976 (defun vip-execute-ex-command ()
977 (vip-deactivate-mark)
978 (cond ((string= ex-token
"args") (ex-args))
979 ((string= ex-token
"copy") (ex-copy nil
))
980 ((string= ex-token
"cd") (ex-cd))
981 ((string= ex-token
"chdir") (ex-cd))
982 ((string= ex-token
"delete") (ex-delete))
983 ((string= ex-token
"edit") (ex-edit))
984 ((string= ex-token
"file") (vip-info-on-file))
985 ((string= ex-token
"goto") (ex-goto))
986 ((string= ex-token
"help") (ex-help))
987 ((string= ex-token
"join") (ex-line "join"))
988 ((string= ex-token
"kmark") (ex-mark))
989 ((string= ex-token
"mark") (ex-mark))
990 ((string= ex-token
"map") (ex-map))
991 ((string= ex-token
"move") (ex-copy t
))
992 ((string= ex-token
"next") (ex-next ex-cycle-other-window
))
993 ((string= ex-token
"Next") (ex-next (not ex-cycle-other-window
)))
994 ((string= ex-token
"RelatedFile") (ex-next-related-buffer 1))
995 ((string= ex-token
"put") (ex-put))
996 ((string= ex-token
"pwd") (ex-pwd))
997 ((string= ex-token
"preserve") (ex-preserve))
998 ((string= ex-token
"PreviousRelatedFile") (ex-next-related-buffer -
1))
999 ((string= ex-token
"quit") (ex-quit))
1000 ((string= ex-token
"read") (ex-read))
1001 ((string= ex-token
"recover") (ex-recover))
1002 ((string= ex-token
"rewind") (ex-rewind))
1003 ((string= ex-token
"submitReport") (vip-submit-report))
1004 ((string= ex-token
"set") (ex-set))
1005 ((string= ex-token
"shell") (ex-shell))
1006 ((string= ex-token
"source") (ex-source))
1007 ((string= ex-token
"sr") (ex-substitute t t
))
1008 ((string= ex-token
"substitute") (ex-substitute))
1009 ((string= ex-token
"suspend") (suspend-emacs))
1010 ((string= ex-token
"stop") (suspend-emacs))
1011 ((string= ex-token
"transfer") (ex-copy nil
))
1012 ((string= ex-token
"buffer") (if ex-cycle-other-window
1013 (vip-switch-to-buffer-other-window)
1014 (vip-switch-to-buffer)))
1015 ((string= ex-token
"Buffer") (if ex-cycle-other-window
1016 (vip-switch-to-buffer)
1017 (vip-switch-to-buffer-other-window)))
1018 ((string= ex-token
"tag") (ex-tag))
1019 ((string= ex-token
"undo") (vip-undo))
1020 ((string= ex-token
"unmap") (ex-unmap))
1021 ((string= ex-token
"version") (vip-version))
1022 ((string= ex-token
"visual") (ex-edit))
1023 ((string= ex-token
"write") (ex-write nil
))
1024 ((string= ex-token
"Write") (save-some-buffers))
1025 ((string= ex-token
"wq") (ex-write t
))
1026 ((string= ex-token
"WWrite") (save-some-buffers t
)) ; don't ask
1027 ((string= ex-token
"xit") (ex-write t
))
1028 ((string= ex-token
"yank") (ex-yank))
1029 ((string= ex-token
"!") (ex-command))
1030 ((string= ex-token
"=") (ex-line-no))
1031 ((string= ex-token
">") (ex-line "right"))
1032 ((string= ex-token
"<") (ex-line "left"))
1033 ((string= ex-token
"&") (ex-substitute t
))
1034 ((string= ex-token
"~") (ex-substitute t t
))
1035 ((or (string= ex-token
"append")
1036 (string= ex-token
"change")
1037 (string= ex-token
"insert")
1038 (string= ex-token
"open"))
1039 (error "`%s': Obsolete command, not supported by Viper" ex-token
))
1040 ((or (string= ex-token
"abbreviate")
1041 (string= ex-token
"unabbreviate"))
1043 "`%s': Vi abbrevs are obsolete. Use the more powerful Emacs abbrevs"
1045 ((or (string= ex-token
"list")
1046 (string= ex-token
"print")
1047 (string= ex-token
"z")
1048 (string= ex-token
"#"))
1049 (error "`%s': Command not implemented in Viper" ex-token
))
1050 (t (error "`%s': %s" ex-token vip-BadExCommand
))))
1052 (defun vip-undisplayed-files ()
1056 (if (null (get-buffer-window b
))
1057 (let ((f (buffer-file-name b
)))
1059 (if ex-cycle-through-non-files
1060 (let ((s (buffer-name b
)))
1061 (if (string= " " (substring s
0 1))
1070 (let ((l (vip-undisplayed-files))
1073 (while (not (null l
))
1075 (setq args
(format "%s %d) %s\n" args file-count
(car l
))
1076 file-count
(1+ file-count
)))
1078 (if (string= args
"")
1079 (message "All files are already displayed")
1081 (save-window-excursion
1082 (with-output-to-temp-buffer " *vip-info*"
1083 (princ "\n\nThese files are not displayed in any window.\n")
1084 (princ "\n=============\n")
1086 (princ "\n=============\n")
1087 (princ "\nThe numbers can be given as counts to :next. ")
1088 (princ "\n\nPress any key to continue...\n\n"))
1089 (vip-read-event))))))
1091 ;; Ex cd command. Default directory of this buffer changes
1094 (if (string= ex-file
"")
1096 (setq default-directory
(file-name-as-directory (expand-file-name ex-file
))))
1098 ;; Ex copy and move command. DEL-FLAG means delete
1099 (defun ex-copy (del-flag)
1100 (vip-default-ex-addresses)
1101 (let ((address (vip-get-ex-address))
1102 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1106 (vip-enlarge-region (mark t
) (point))
1108 (kill-region (point) (mark t
))
1109 (copy-region-as-kill (point) (mark t
)))
1112 (with-output-to-temp-buffer "*copy text*"
1114 (if (or del-flag ex-g-flag ex-g-variant
)
1116 (buffer-substring (point) (mark t
)))))
1119 (read-string "[Hit return to continue] ")
1120 (save-excursion (kill-buffer "*copy text*")))
1121 (quit (save-excursion (kill-buffer "*copy text*"))
1122 (signal 'quit nil
))))))
1124 (goto-char (point-min))
1127 (insert (current-kill 0))))
1129 ;; Ex delete command
1131 (vip-default-ex-addresses)
1133 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1134 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1136 (vip-enlarge-region beg end
)
1137 (exchange-point-and-mark)
1141 (forward-line (1- ex-count
)))
1143 (vip-enlarge-region (point) (mark t
))
1145 ;; show text to be deleted and ask for confirmation
1147 (with-output-to-temp-buffer " *delete text*"
1148 (princ (buffer-substring (point) (mark t
))))
1150 (read-string "[Hit return to continue] ")
1152 (save-excursion (kill-buffer " *delete text*"))
1154 (save-excursion (kill-buffer " *delete text*")))
1156 (cond ((vip-valid-register ex-buffer
'(Letter))
1157 (vip-append-to-register
1158 (downcase ex-buffer
) (point) (mark t
)))
1159 ((vip-valid-register ex-buffer
)
1160 (copy-to-register ex-buffer
(point) (mark t
) nil
))
1161 (t (error vip-InvalidRegister ex-buffer
))))
1162 (kill-region (point) (mark t
))))))
1167 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1168 ;; asked if current buffer should really be discarded.
1169 ;; This command can take multiple file names. It replaces the current buffer
1170 ;; with the first file in its argument list
1171 (defun ex-edit (&optional file
)
1174 (cond ((and (string= ex-file
"") buffer-file-name
)
1175 (setq ex-file
(vip-abbreviate-file-name (buffer-file-name))))
1176 ((string= ex-file
"")
1177 (error vip-NoFileSpecified
)))
1180 (if buffer-file-name
1181 (cond ((buffer-modified-p)
1183 (format "Buffer %s is modified. Discard changes? "
1186 ((not (verify-visited-file-modtime (current-buffer)))
1188 (format "File %s changed on disk. Reread from disk? "
1191 (t (setq do-edit nil
))))
1194 (if (yes-or-no-p msg
)
1196 (set-buffer-modified-p nil
)
1197 (kill-buffer (current-buffer)))
1198 (message "Buffer %s was left intact" (buffer-name))))
1201 (if (null (setq file
(get-file-buffer ex-file
)))
1203 (ex-find-file ex-file
)
1204 (or (eq major-mode
'dired-mode
)
1205 (vip-change-state-to-vi))
1206 (goto-char (point-min)))
1207 (switch-to-buffer file
))
1210 (save-window-excursion
1211 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1212 (set-buffer vip-ex-work-buf
)
1213 (delete-region (point-min) (point-max))
1214 (insert ex-offset
"\n")
1215 (goto-char (point-min)))
1216 (goto-char (vip-get-ex-address))
1217 (beginning-of-line)))
1218 (ex-fixup-history vip-last-ex-prompt ex-file
))
1220 ;; Find-file FILESPEC if it appears to specify a single file.
1221 ;; Otherwise, assume that FILES{EC is a wildcard.
1222 ;; In this case, split it into substrings separated by newlines.
1223 ;; Each line is assumed to be a file name. find-file's each file thus obtained.
1224 (defun ex-find-file (filespec)
1225 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1226 (cond ((file-exists-p filespec
) (find-file filespec
))
1227 ((string-match nonstandard-filename-chars filespec
)
1228 (funcall ex-nontrivial-find-file-function filespec
))
1229 (t (find-file filespec
)))
1233 ;; Ex global command
1234 (defun ex-global (variant)
1235 (let ((gcommand ex-token
))
1236 (if (or ex-g-flag ex-g-variant
)
1237 (error "`%s' within `global' is not allowed" gcommand
)
1245 (error "`%s': Missing regular expression" gcommand
)))
1247 (if (string= ex-token
"")
1248 (if (null vip-s-string
)
1249 (error vip-NoPrevSearch
)
1250 (setq ex-g-pat vip-s-string
))
1251 (setq ex-g-pat ex-token
1252 vip-s-string ex-token
))
1253 (if (null ex-addresses
)
1254 (setq ex-addresses
(list (point-max) (point-min)))
1255 (vip-default-ex-addresses))
1256 (let ((marks nil
) (mark-count 0)
1257 com-str
(end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1258 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1260 (vip-enlarge-region beg end
)
1261 (exchange-point-and-mark)
1262 (let ((cont t
) (limit (point-marker)))
1263 (exchange-point-and-mark)
1264 ;; skip the last line if empty
1266 (if (eobp) (vip-backward-char-carefully))
1267 (while (and cont
(not (bobp)) (>= (point) limit
))
1271 (let ((found (re-search-backward ex-g-pat
(mark t
) t
)))
1272 (if (or (and ex-g-flag found
)
1273 (and ex-g-variant
(not found
)))
1276 (setq mark-count
(1+ mark-count
))
1277 (setq marks
(cons (point-marker) marks
)))))
1279 (if (bobp) (setq cont nil
)
1282 (save-window-excursion
1283 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1284 (set-buffer vip-ex-work-buf
)
1285 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
1287 (goto-char (car marks
))
1289 (setq mark-count
(1- mark-count
))
1290 (setq marks
(cdr marks
)))))
1294 (if (null ex-addresses
)
1295 (setq ex-addresses
(cons (point) nil
)))
1296 (push-mark (point) t
)
1297 (goto-char (car ex-addresses
))
1298 (beginning-of-line))
1300 ;; Ex line commands. COM is join, shift-right or shift-left
1301 (defun ex-line (com)
1302 (vip-default-ex-addresses)
1304 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
1305 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1307 (vip-enlarge-region beg end
)
1308 (exchange-point-and-mark)
1312 (forward-line ex-count
)))
1314 ;; show text to be joined and ask for confirmation
1316 (with-output-to-temp-buffer " *text*"
1317 (princ (buffer-substring (point) (mark t
))))
1320 (read-string "[Hit return to continue] ")
1321 (ex-line-subr com
(point) (mark t
)))
1323 (save-excursion (kill-buffer " *text*")))
1324 (ex-line-subr com
(point) (mark t
)))
1325 (setq point
(point)))
1326 (goto-char (1- point
))
1327 (beginning-of-line)))
1329 (defun ex-line-subr (com beg end
)
1330 (cond ((string= com
"join")
1331 (goto-char (min beg end
))
1332 (while (and (not (eobp)) (< (point) (max beg end
)))
1334 (if (and (<= (point) (max beg end
)) (not (eobp)))
1337 (delete-region (point) (1- (point)))
1338 (if (not ex-variant
) (fixup-whitespace))))))
1339 ((or (string= com
"right") (string= com
"left"))
1341 (min beg end
) (max beg end
)
1342 (if (string= com
"right") vip-shift-width
(- vip-shift-width
)))
1343 (goto-char (max beg end
))
1345 (vip-forward-char-carefully))))
1351 (if (null ex-addresses
)
1353 (cons (point) nil
)))
1354 (save-window-excursion
1355 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1356 (set-buffer vip-ex-work-buf
)
1357 (skip-chars-forward " \t")
1358 (if (looking-at "[a-z]")
1360 (setq char
(following-char))
1362 (skip-chars-forward " \t")
1363 (if (not (looking-at "[\n|]"))
1364 (error "`%s': %s" ex-token vip-SpuriousText
)))
1365 (error "`%s' requires a following letter" ex-token
)))
1367 (goto-char (car ex-addresses
))
1368 (point-to-register (1+ (- char ?a
))))))
1372 ;; Alternate file is the file next to the first one in the buffer ring
1373 (defun ex-next (cycle-other-window &optional find-alt-file
)
1376 (if (not find-alt-file
)
1379 (if (or (char-or-string-p ex-offset
)
1380 (and (not (string= "" ex-file
))
1381 (not (string-match "^[0-9]+$" ex-file
))))
1384 (throw 'ex-edit nil
))
1385 (setq count
(string-to-int ex-file
))
1386 (if (= count
0) (setq count
1))
1387 (if (< count
0) (error "Usage: `next <count>' (count >= 0)"))))
1389 (setq l
(vip-undisplayed-files))
1391 (while (and (not (null l
)) (null (car l
)))
1393 (setq count
(1- count
))
1396 (if find-alt-file
(car l
)
1398 (if (and (car l
) (get-file-buffer (car l
)))
1399 (let* ((w (if cycle-other-window
1400 (get-lru-window) (selected-window)))
1401 (b (window-buffer w
)))
1402 (set-window-buffer w
(get-file-buffer (car l
)))
1404 ;; this puts "next <count>" in the ex-command history
1405 (ex-fixup-history vip-last-ex-prompt ex-file
))
1406 (error "Not that many undisplayed files")))))))
1409 (defun ex-next-related-buffer (direction &optional no-recursion
)
1411 (vip-ring-rotate1 vip-related-files-and-buffers-ring direction
)
1413 (let ((file-or-buffer-name
1414 (vip-current-ring-item vip-related-files-and-buffers-ring
))
1415 (old-ring vip-related-files-and-buffers-ring
)
1416 (old-win (selected-window))
1419 (or (and (ring-p vip-related-files-and-buffers-ring
)
1420 (> (ring-length vip-related-files-and-buffers-ring
) 0))
1421 (error "This buffer has no related files or buffers"))
1423 (or (stringp file-or-buffer-name
)
1425 "File and buffer names must be strings, %S" file-or-buffer-name
))
1427 (setq buf
(cond ((get-buffer file-or-buffer-name
))
1428 ((file-exists-p file-or-buffer-name
)
1429 (find-file-noselect file-or-buffer-name
))
1432 (if (not (vip-buffer-live-p buf
))
1433 (error "Didn't find buffer %S or file %S"
1435 (vip-abbreviate-file-name
1436 (expand-file-name file-or-buffer-name
))))
1438 (if (equal buf
(current-buffer))
1443 (ex-next-related-buffer direction
'norecursion
))))
1448 (if (setq wind
(vip-get-visible-buffer-window buf
))
1450 (setq wind
(get-lru-window (if vip-xemacs-p nil
'visible
)))
1451 (set-window-buffer wind buf
))
1453 (if (vip-window-display-p)
1455 (raise-frame (window-frame wind
))
1456 (if (equal (window-frame wind
) (window-frame old-win
))
1457 (save-window-excursion (select-window wind
) (sit-for 1))
1458 (select-window wind
)))
1459 (save-window-excursion (select-window wind
) (sit-for 1)))
1463 (setq vip-related-files-and-buffers-ring old-ring
))
1465 (setq vip-local-search-start-marker
(point-marker))
1470 (defun ex-preserve ()
1471 (message "Autosaving all buffers that need to be saved...")
1476 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
1478 (setq vip-use-register ex-buffer
)
1480 (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
1482 ;; Ex print working directory
1484 (message default-directory
))
1488 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1490 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1491 (set-buffer vip-ex-work-buf
)
1492 (if (looking-at "!") (forward-char 1)))
1493 (if (< vip-expert-level
3)
1494 (save-buffers-kill-emacs)
1495 (kill-buffer (current-buffer))))
1501 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
1504 (vip-add-newline-at-eob-if-necessary)
1505 (if (not (or (bobp) (eobp))) (forward-line 1))
1506 (if (and (not ex-variant
) (string= ex-file
""))
1508 (if (null buffer-file-name
)
1509 (error vip-NoFileSpecified
))
1510 (setq ex-file buffer-file-name
)))
1513 (setq command
(ex-expand-filsyms ex-file
(current-buffer)))
1514 (shell-command command t
))
1515 (insert-file-contents ex-file
)))
1516 (ex-fixup-history vip-last-ex-prompt ex-file
))
1518 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1519 (defun ex-fixup-history (&rest args
)
1520 (setq vip-ex-history
1521 (cons (mapconcat 'identity args
" ") (cdr vip-ex-history
))))
1524 ;; Ex recover from emacs \#file\#
1525 (defun ex-recover ()
1527 (if (or ex-append ex-offset
)
1528 (error "`recover': %s" vip-SpuriousText
))
1529 (if (string= ex-file
"")
1531 (if (null buffer-file-name
)
1532 (error "This buffer isn't visiting any file"))
1533 (setq ex-file buffer-file-name
))
1534 (setq ex-file
(expand-file-name ex-file
)))
1535 (if (and (not (string= ex-file
(buffer-file-name)))
1538 (error "No write since last change \(:rec! overrides\)"))
1539 (recover-file ex-file
))
1541 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1544 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1547 ;; read variable name for ex-set
1548 (defun ex-set-read-variable ()
1549 (let ((minibuffer-local-completion-map
1550 (copy-keymap minibuffer-local-completion-map
))
1551 (cursor-in-echo-area t
)
1554 minibuffer-local-completion-map
" " 'minibuffer-complete-and-exit
)
1555 (define-key minibuffer-local-completion-map
"=" 'exit-minibuffer
)
1556 (if (vip-set-unread-command-events
1557 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil
"\C-m"))
1560 (vip-set-unread-command-events ?\C-m
)))
1561 (message ":set <Variable> [= <Value>]")
1562 (or batch
(sit-for 2))
1564 (while (string-match "^[ \\t\\n]*$"
1566 (completing-read ":set " ex-variable-alist
)))
1567 (message ":set <Variable> ")
1568 ;; if there are unread events, don't wait
1569 (or (vip-set-unread-command-events "") (sit-for 2))
1575 (let ((var (ex-set-read-variable))
1579 (auto-cmd-label "; don't touch or else...")
1580 (delete-turn-on-auto-fill-pattern
1581 "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1582 actual-lisp-cmd lisp-cmd-del-pattern
1585 (cond ((member var
'("ai" "autoindent"))
1586 (setq var
"vip-auto-indent"
1590 ((member var
'("gai" "global-autoindent"))
1591 (kill-local-variable 'vip-auto-indent
)
1592 (setq var
"vip-auto-indent"
1593 set-cmd
"setq-default"
1595 ((member var
'("noai" "noautoindent"))
1596 (setq var
"vip-auto-indent"
1599 ((member var
'("gnoai" "global-noautoindent"))
1600 (kill-local-variable 'vip-auto-indent
)
1601 (setq var
"vip-auto-indent"
1602 set-cmd
"setq-default"
1604 ((member var
'("ic" "ignorecase"))
1605 (setq var
"vip-case-fold-search"
1607 ((member var
'("noic" "noignorecase"))
1608 (setq var
"vip-case-fold-search"
1610 ((member var
'("ma" "magic"))
1611 (setq var
"vip-re-search"
1613 ((member var
'("noma" "nomagic"))
1614 (setq var
"vip-re-search"
1616 ((member var
'("ro" "readonly"))
1617 (setq var
"buffer-read-only"
1619 ((member var
'("noro" "noreadonly"))
1620 (setq var
"buffer-read-only"
1622 ((member var
'("sm" "showmatch"))
1623 (setq var
"blink-matching-paren"
1625 ((member var
'("nosm" "noshowmatch"))
1626 (setq var
"blink-matching-paren"
1628 ((member var
'("ws" "wrapscan"))
1629 (setq var
"vip-search-wrap-around-t"
1631 ((member var
'("nows" "nowrapscan"))
1632 (setq var
"vip-search-wrap-around-t"
1634 (if (eq val
0) ; value must be set by the user
1635 (let ((cursor-in-echo-area t
))
1636 (message ":set %s = <Value>" var
)
1637 ;; if there are unread events, don't wait
1638 (or (vip-set-unread-command-events "") (sit-for 2))
1639 (setq val
(read-string (format ":set %s = " var
)))
1640 (ex-fixup-history "set" orig-var val
)
1642 ;; check numerical values
1646 "gts" "global-tabstop"
1649 (or (numberp (setq val2
(car (read-from-string val
))))
1650 (error "%s: Invalid value, numberp, %S" var val
))
1652 (error "%s: Invalid value, numberp, %S" var val
))))
1655 ((member var
'("sw" "shiftwidth"))
1656 (setq var
"vip-shift-width"))
1657 ((member var
'("ts" "tabstop"))
1658 ;; make it take effect in curr buff and new bufs
1659 (setq var
"tab-width"
1662 ((member var
'("gts" "global-tabstop"))
1663 (kill-local-variable 'tab-width
)
1664 (setq var
"tab-width"
1665 set-cmd
"setq-default"))
1666 ((member var
'("wm" "wrapmargin"))
1667 ;; make it take effect in curr buff and new bufs
1668 (kill-local-variable 'fill-column
)
1669 (setq var
"fill-column"
1670 val
(format "(- (window-width) %s)" val
)
1671 set-cmd
"setq-default"))
1672 ((member var
'("sh" "shell"))
1673 (setq var
"explicit-shell-file-name"
1674 val
(format "\"%s\"" val
)))))
1675 (ex-fixup-history "set" orig-var
))
1677 (setq actual-lisp-cmd
(format "\n(%s %s %s) %s"
1678 set-cmd var val auto-cmd-label
))
1679 (setq lisp-cmd-del-pattern
1680 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1681 set-cmd var auto-cmd-label
))
1683 (if (and ask-if-save
1684 (y-or-n-p (format "Do you want to save this setting in %s "
1685 vip-custom-file-name
)))
1687 (vip-save-string-in-file
1688 actual-lisp-cmd vip-custom-file-name
1690 lisp-cmd-del-pattern
)
1691 (if (string= var
"fill-column")
1693 (vip-save-string-in-file
1695 "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
1697 vip-custom-file-name
1698 delete-turn-on-auto-fill-pattern
)
1699 (vip-save-string-in-file
1700 nil vip-custom-file-name delete-turn-on-auto-fill-pattern
)
1701 (vip-save-string-in-file
1702 nil vip-custom-file-name
1704 lisp-cmd-del-pattern
)
1708 (message "%s %s %s" set-cmd var
(if (string-match "^[ \t]*$" val
)
1711 (eval (car (read-from-string actual-lisp-cmd
)))
1712 (if (string= var
"fill-column")
1715 (auto-fill-mode -
1)))
1719 ;; In inline args, skip regex-forw and (optionally) chars-back.
1720 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1722 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str
)
1724 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1725 (set-buffer vip-ex-work-buf
)
1726 (goto-char (point-min))
1727 (re-search-forward regex-forw nil t
)
1730 (goto-char (point-max))
1732 (skip-chars-backward chars-back
)
1733 (skip-chars-backward " \t\n\C-m"))
1735 ;; replace SPC with `=' to suppress the special meaning SPC has
1739 (while (re-search-forward " +" nil t
)
1740 (replace-match replace-str nil t
)
1741 (vip-forward-char-carefully)))
1743 (buffer-substring beg end
))))
1750 ;; Viper help. Invokes Info
1754 (pop-to-buffer (get-buffer-create "*info*"))
1755 (info (if vip-xemacs-p
"viper.info" "viper"))
1756 (message "Type `i' to search for a specific topic"))
1758 (with-output-to-temp-buffer " *vip-info*"
1760 The Info file for Viper does not seem to be installed.
1762 This file is part of the standard distribution of %sEmacs.
1763 Please contact your system administrator. "
1764 (if vip-xemacs-p
"X" "")
1767 ;; Ex source command. Loads the file specified as argument or `~/.vip'
1770 (if (string= ex-file
"")
1771 (load vip-custom-file-name
)
1774 ;; Ex substitute command
1775 ;; If REPEAT use previous regexp which is ex-reg-exp or vip-s-string
1776 (defun ex-substitute (&optional repeat r-flag
)
1780 (case-fold-search vip-case-fold-search
)
1782 (if repeat
(setq ex-token nil
) (setq delim
(vip-get-ex-pat)))
1785 (setq pat
(if r-flag vip-s-string ex-reg-exp
))
1787 (error "No previous pattern to use in substitution"))
1789 delim
(string-to-char pat
)))
1790 (setq pat
(if (string= ex-token
"") vip-s-string ex-token
))
1791 (setq vip-s-string pat
1793 (setq delim
(vip-get-ex-pat))
1799 (while (vip-get-ex-opt-gc delim
)
1800 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
1804 (if ex-addresses
(goto-char (car ex-addresses
)))
1806 (forward-line (1- ex-count
))
1807 (setq ex-addresses
(cons (point) (cons (mark t
) nil
))))
1808 (if (null ex-addresses
)
1809 (setq ex-addresses
(cons (point) (cons (point) nil
)))
1810 (if (null (cdr ex-addresses
))
1811 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
1813 (let ((beg (car ex-addresses
))
1814 (end (car (cdr ex-addresses
)))
1817 (vip-enlarge-region beg end
)
1818 (let ((limit (save-excursion
1819 (goto-char (max (point) (mark t
)))
1821 (goto-char (min (point) (mark t
)))
1822 (while (< (point) limit
)
1824 (setq eol-mark
(point-marker))
1828 (while (and (not (eolp))
1829 (re-search-forward pat eol-mark t
))
1830 (if (or (not opt-c
) (y-or-n-p "Replace? "))
1832 (setq matched-pos
(point))
1833 (if (not (stringp repl
))
1834 (error "Can't perform Ex substitution: No previous replacement pattern"))
1835 (replace-match repl t
))))
1837 (vip-forward-char-carefully))
1840 "Can't repeat Ex substitution: No previous regular expression"))
1841 (if (and (re-search-forward pat eol-mark t
)
1842 (or (not opt-c
) (y-or-n-p "Replace? ")))
1844 (setq matched-pos
(point))
1845 (if (not (stringp repl
))
1846 (error "Can't perform Ex substitution: No previous replacement pattern"))
1847 (replace-match repl t
)))
1849 (vip-forward-char-carefully))))))
1850 (if matched-pos
(goto-char matched-pos
))
1852 (if opt-c
(message "done"))))
1857 (save-window-excursion
1858 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
1859 (set-buffer vip-ex-work-buf
)
1860 (skip-chars-forward " \t")
1862 (skip-chars-forward "^ |\t\n")
1863 (setq tag
(buffer-substring (mark t
) (point))))
1864 (if (not (string= tag
"")) (setq ex-tag tag
))
1865 (vip-change-state-to-emacs)
1866 (condition-case conds
1868 (if (string= tag
"")
1870 (find-tag-other-window ex-tag
))
1871 (vip-change-state-to-vi))
1873 (vip-change-state-to-vi)
1874 (vip-message-conditions conds
)))))
1877 (defun ex-write (q-flag)
1878 (vip-default-ex-addresses t
)
1880 (let ((end (car ex-addresses
))
1881 (beg (car (cdr ex-addresses
)))
1882 (orig-buf (current-buffer))
1883 (orig-buf-file-name (buffer-file-name))
1884 (orig-buf-name (buffer-name))
1885 (buff-changed-p (buffer-modified-p))
1886 temp-buf writing-same-file region
1887 file-exists writing-whole-file
)
1888 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1891 (vip-enlarge-region beg end
)
1892 (shell-command-on-region (point) (mark t
) ex-file
))
1893 (if (and (string= ex-file
"") (not (buffer-file-name)))
1896 (format "Buffer %s isn't visiting any file. File to save in: "
1899 (setq writing-whole-file
(and (= (point-min) beg
) (= (point-max) end
))
1900 ex-file
(if (string= ex-file
"")
1902 (expand-file-name ex-file
)))
1903 ;; if ex-file is a directory use the file portion of the buffer file name
1904 (if (and (file-directory-p ex-file
)
1906 (not (file-directory-p buffer-file-name
)))
1908 (concat (file-name-as-directory ex-file
)
1909 (file-name-nondirectory buffer-file-name
))))
1911 (setq file-exists
(file-exists-p ex-file
)
1912 writing-same-file
(string= ex-file
(buffer-file-name)))
1914 (if (and writing-whole-file writing-same-file
)
1915 (if (not (buffer-modified-p))
1916 (message "(No changes need to be saved)")
1920 (ex-write-info file-exists ex-file
(point-min) (point-max))
1922 ;; writing some other file or portion of the current file
1923 (cond ((and file-exists
1924 (not writing-same-file
)
1926 (format "File %s exists. Overwrite? " ex-file
))))
1928 ((and writing-whole-file
(not ex-append
))
1931 (set-visited-file-name ex-file
)
1932 (set-buffer-modified-p t
)
1934 ;; restore the buffer file name
1935 (set-visited-file-name orig-buf-file-name
)
1936 (set-buffer-modified-p buff-changed-p
)
1937 ;; If the buffer wasn't visiting a file, restore buffer name.
1938 ;; Name could've been changed by packages such as uniquify.
1939 (or orig-buf-file-name
1942 (rename-buffer orig-buf-name
))))
1946 file-exists ex-file
(point-min) (point-max))))
1947 (t ; writing a region
1950 (vip-enlarge-region beg end
)
1951 (setq region
(buffer-substring (point) (mark t
)))
1952 ;; create temp buffer for the region
1953 (setq temp-buf
(get-buffer-create " *ex-write*"))
1954 (set-buffer temp-buf
)
1955 (set-visited-file-name ex-file
'noquerry
)
1957 (if (and file-exists ex-append
)
1958 (insert-file-contents ex-file
))
1959 (goto-char (point-max))
1963 file-exists ex-file
(point-min) (point-max))
1965 (set-buffer temp-buf
)
1966 (set-buffer-modified-p nil
)
1967 (kill-buffer temp-buf
))
1969 (set-buffer orig-buf
)
1970 ;; this prevents the loss of data if writing part of the buffer
1971 (if (and (buffer-file-name) writing-same-file
)
1972 (set-visited-file-modtime))
1973 (or writing-whole-file
1974 (not writing-same-file
)
1975 (set-buffer-modified-p t
))
1977 (if (< vip-expert-level
2)
1978 (save-buffers-kill-emacs)
1979 (kill-buffer (current-buffer))))
1983 (defun ex-write-info (exists file-name beg end
)
1984 (message "`%s'%s %d lines, %d characters"
1985 (vip-abbreviate-file-name file-name
)
1986 (if exists
"" " [New file]")
1987 (count-lines beg
(min (1+ end
) (point-max)))
1992 (vip-default-ex-addresses)
1994 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
1995 (if (> beg end
) (error vip-FirstAddrExceedsSecond
))
1997 (vip-enlarge-region beg end
)
1998 (exchange-point-and-mark)
1999 (if (or ex-g-flag ex-g-variant
)
2000 (error "Can't execute `yank' within `global'"))
2004 (forward-line (1- ex-count
)))
2006 (vip-enlarge-region (point) (mark t
))
2007 (if ex-flag
(error "`yank': %s" vip-SpuriousText
))
2009 (cond ((vip-valid-register ex-buffer
'(Letter))
2010 (vip-append-to-register
2011 (downcase ex-buffer
) (point) (mark t
)))
2012 ((vip-valid-register ex-buffer
)
2013 (copy-to-register ex-buffer
(point) (mark t
) nil
))
2014 (t (error vip-InvalidRegister ex-buffer
))))
2015 (copy-region-as-kill (point) (mark t
)))))
2017 ;; Execute shell command
2018 (defun ex-command ()
2020 (save-window-excursion
2021 (setq vip-ex-work-buf
(get-buffer-create vip-ex-work-buf-name
))
2022 (set-buffer vip-ex-work-buf
)
2023 (skip-chars-forward " \t")
2024 (setq command
(buffer-substring (point) (point-max)))
2026 (setq command
(ex-expand-filsyms command
(current-buffer)))
2027 (if (and (> (length command
) 0) (string= "!" (substring command
0 1)))
2028 (if vip-ex-last-shell-com
2029 (setq command
(concat vip-ex-last-shell-com
(substring command
1)))
2030 (error "No previous shell command")))
2031 (setq vip-ex-last-shell-com command
)
2032 (if (null ex-addresses
)
2033 (shell-command command
)
2034 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2035 (if (null beg
) (setq beg end
))
2039 (vip-enlarge-region (point) (mark t
))
2040 (shell-command-on-region (point) (mark t
) command t
))
2043 ;; Print line number
2044 (defun ex-line-no ()
2048 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
2050 ;; Give information on the file visited by the current buffer
2051 (defun vip-info-on-file ()
2053 (let ((pos1 (vip-line-pos 'start
))
2054 (pos2 (vip-line-pos 'end
))
2056 (setq lines
(count-lines (point-min) (vip-line-pos 'end
))
2057 file
(if (buffer-file-name)
2058 (concat (vip-abbreviate-file-name (buffer-file-name)) ":")
2059 (concat (buffer-name) " [Not visiting any file]:"))
2060 info
(format "line=%d/%d pos=%d/%d col=%d %s"
2064 (count-lines (point-min) (point-max))
2065 (point) (1- (point-max))
2066 (1+ (current-column))
2067 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2068 (if (< (+ 1 (length info
) (length file
))
2069 (window-width (minibuffer-window)))
2070 (message (concat file
" " info
))
2071 (save-window-excursion
2072 (with-output-to-temp-buffer " *vip-info*"
2075 "\n\n\nPress any key to continue...\n\n")))
2077 (kill-buffer " *vip-info*")))
2081 ;;; viper-ex.el ends here