Fix bug in handling GnuPG's TRUST_MARGINAL status
[emacs.git] / lisp / emulation / viper-ex.el
blobca067033e6347e8d35c40b90bfde7f4a9fa69c0f
1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994-1998, 2000-2017 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
6 ;; Package: viper
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (provide 'viper-ex)
29 ;; Compiler pacifier
30 (defvar read-file-name-map)
31 (defvar viper-use-register)
32 (defvar viper-s-string)
33 (defvar viper-shift-width)
34 (defvar viper-ex-history)
35 (defvar viper-related-files-and-buffers-ring)
36 (defvar viper-local-search-start-marker)
37 (defvar viper-expert-level)
38 (defvar viper-custom-file-name)
39 (defvar viper-case-fold-search)
40 (defvar explicit-shell-file-name)
41 (defvar compile-command)
42 (require 'viper-keym)
43 ;; end pacifier
45 (require 'viper-util)
47 (defgroup viper-ex nil
48 "Viper support for Ex commands."
49 :prefix "ex-"
50 :group 'viper)
54 ;;; Variables
56 (defconst viper-ex-work-buf-name " *ex-working-space*")
57 (defvar viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
58 (defconst viper-ex-tmp-buf-name " *ex-tmp*")
59 (defconst viper-ex-print-buf-name " *ex-print*")
60 (defvar viper-ex-print-buf (get-buffer-create viper-ex-print-buf-name))
63 ;;; ex-commands...
65 (defun ex-cmd-obsolete (name)
66 (error "`%s': Obsolete command, not supported by Viper" name))
68 (defun ex-cmd-not-yet (name)
69 (error "`%s': Command not implemented in Viper" name))
71 ;; alist entries: name (in any order), command, cont(??)
72 ;; If command is a string, then that is an alias to the real command
73 ;; to execute (for instance, ":m" -> ":move").
74 ;; command attributes:
75 ;; is-mashed: the command's args may be jammed right up against the command
76 ;; one-letter: this is a one-letter token. Any text appearing after
77 ;; the name gets appended as an argument for the command
78 ;; i.e. ":kabc" gets turned into (ex-mark "abc")
79 (defconst ex-token-alist '(
80 ("!" (ex-command))
81 ("&" (ex-substitute t))
82 ("=" (ex-line-no))
83 (">" (ex-line "right"))
84 ("<" (ex-line "left"))
85 ("Buffer" (if ex-cycle-other-window
86 (viper-switch-to-buffer)
87 (viper-switch-to-buffer-other-window)))
88 ("Next" (ex-next (not ex-cycle-other-window)))
89 ("PreviousRelatedFile" (ex-next-related-buffer -1))
90 ("RelatedFile" (ex-next-related-buffer 1))
91 ("W" "Write")
92 ("WWrite" (save-some-buffers t))
93 ("Write" (save-some-buffers))
94 ("a" "append")
95 ("args" (ex-args))
96 ("buffer" (if ex-cycle-other-window
97 (viper-switch-to-buffer-other-window)
98 (viper-switch-to-buffer)))
99 ("c" "change")
100 ;; ch should be "change" but maintain old viper compatibility
101 ("ch" "chdir")
102 ("cd" (ex-cd))
103 ("chdir" (ex-cd))
104 ("copy" (ex-copy nil))
105 ("customize" (customize-group "viper"))
106 ("delete" (ex-delete))
107 ("edit" (ex-edit))
108 ("file" (ex-set-visited-file-name))
109 ("g" "global")
110 ("global" (ex-global nil) is-mashed)
111 ("goto" (ex-goto))
112 ("help" (ex-help))
113 ("join" (ex-line "join"))
114 ("k" (ex-mark) one-letter)
115 ("kmark" (ex-mark))
116 ("m" "move")
117 ("make" (ex-compile))
118 ; old viper doesn't specify a default for "ma" so leave it undefined
119 ("map" (ex-map))
120 ("mark" (ex-mark))
121 ("move" (ex-copy t))
122 ("next" (ex-next ex-cycle-other-window))
123 ("p" "print")
124 ("preserve" (ex-preserve))
125 ("print" (ex-print))
126 ("put" (ex-put))
127 ("pwd" (ex-pwd))
128 ("quit" (ex-quit))
129 ("r" "read")
130 ("re" "read")
131 ("read" (ex-read))
132 ("recover" (ex-recover))
133 ("rewind" (ex-rewind))
134 ("s" "substitute")
135 ("su" "substitute")
136 ("sub" "substitute")
137 ("set" (ex-set))
138 ("shell" (ex-shell))
139 ("source" (ex-source))
140 ("stop" (suspend-emacs))
141 ("sr" (ex-substitute t t))
142 ("submitReport" (viper-submit-report))
143 ("substitute" (ex-substitute) is-mashed)
144 ("suspend" (suspend-emacs))
145 ("t" "transfer")
146 ("tag" (ex-tag))
147 ("transfer" (ex-copy nil))
148 ("u" "undo")
149 ("un" "undo")
150 ("undo" (viper-undo))
151 ("unmap" (ex-unmap))
152 ("v" "vglobal")
153 ("version" (viper-version))
154 ("vglobal" (ex-global t) is-mashed)
155 ("visual" (ex-edit))
156 ("w" "write")
157 ("wq" (ex-write t))
158 ("write" (ex-write nil))
159 ("xit" (ex-write t))
160 ("yank" (ex-yank))
161 ("~" (ex-substitute t t))
163 ("append" (ex-cmd-obsolete "append"))
164 ("change" (ex-cmd-obsolete "change"))
165 ("insert" (ex-cmd-obsolete "insert"))
166 ("open" (ex-cmd-obsolete "open"))
168 ("list" (ex-cmd-not-yet "list"))
169 ("z" (ex-cmd-not-yet "z"))
170 ("#" (ex-cmd-not-yet "#"))
172 ("abbreviate" (error "`%s': Vi abbreviations are obsolete. Use the more powerful Emacs abbrevs" ex-token))
173 ("unabbreviate" (error "`%s': Vi abbreviations are obsolete. Use the more powerful Emacs abbrevs" ex-token))
176 ;; No code should touch anything in the alist entry! (other than the name,
177 ;; "car entry", of course) This way, changing this data structure
178 ;; requires changing only the following ex-cmd functions...
180 ;; Returns cmd if the command may be jammed right up against its
181 ;; arguments, nil if there must be a space.
182 ;; examples of mashable commands: g// g!// v// s// sno// sm//
183 (defun ex-cmd-is-mashed-with-args (cmd)
184 (if (eq 'is-mashed (car (nthcdr 2 cmd))) cmd))
186 ;; Returns true if this is a one-letter command that may be followed
187 ;; by anything, no whitespace needed. This is a special-case for ":k".
188 (defun ex-cmd-is-one-letter (cmd)
189 (if (eq 'one-letter (car (nthcdr 2 cmd))) cmd))
191 ;; Executes the function associated with the command
192 (defun ex-cmd-execute (cmd)
193 (eval (cadr cmd)))
195 ;; If this is a one-letter magic command, splice in args.
196 (defun ex-splice-args-in-1-letr-cmd (key list)
197 (let ((oneletter (ex-cmd-is-one-letter (assoc (substring key 0 1) list))))
198 (if oneletter
199 (list key
200 (append (cadr oneletter)
201 (if (< 1 (length key)) (list (substring key 1))))
202 (car (cdr (cdr oneletter))) ))
206 ;; Returns the alist entry for the appropriate key.
207 ;; Tries to complete the key before using it in the alist.
208 ;; If there is no appropriate key (no match or duplicate matches) return nil
209 (defun ex-cmd-assoc (key list)
210 (let ((entry (try-completion key list))
211 result)
212 (setq result (cond
213 ((eq entry t) (assoc key list))
214 ((stringp entry) (or (ex-splice-args-in-1-letr-cmd key list)
215 (assoc entry list)))
216 ((eq entry nil) (ex-splice-args-in-1-letr-cmd key list))
217 (t nil)
219 ;; If we end up with an alias, look up the alias...
220 (if (stringp (cadr result))
221 (setq result (ex-cmd-assoc (cadr result) list)))
222 ;; and return the corresponding alist entry
223 result
227 ;; A-list of Ex variables that can be set using the :set command.
228 (defconst ex-variable-alist
229 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
230 ("tabstop-global") ("ts-g") ("tabstop") ("ts")
231 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
232 ("readonly") ("ro")
233 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
234 ("noreadonly") ("noro") ("nomagic") ("noma")
235 ("noignorecase") ("noic")
236 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai")
237 ("magic") ("ma") ("ignorecase") ("ic")
238 ("autoindent-global") ("ai-g") ("autoindent") ("ai")
239 ("all")
244 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
245 (defvar ex-token nil)
247 ;; Type of token.
248 ;; If non-nil, gives type of address; if nil, it is a command.
249 (defvar ex-token-type nil)
251 ;; List of addresses passed to Ex command
252 (defvar ex-addresses nil)
254 ;; This flag is supposed to be set only by `#', `print', and `list',
255 ;; none of which is implemented. So, it and the pieces of the code it
256 ;; controls are dead weight. We keep it just in case this might be
257 ;; needed in the future.
258 (defvar ex-flag nil)
260 ;; "buffer" where Ex commands keep deleted data.
261 ;; In Emacs terms, this is a register.
262 (defvar ex-buffer nil)
264 ;; Value of ex count.
265 (defvar ex-count nil)
267 ;; Flag indicating that :global Ex command is being executed.
268 (defvar ex-g-flag nil)
269 ;; Flag indicating that :vglobal Ex command is being executed.
270 (defvar ex-g-variant nil)
271 ;; Marks to operate on during a :global Ex command.
272 (defvar ex-g-marks nil)
274 ;; Save reg-exp used in substitute.
275 (defvar ex-reg-exp nil)
278 ;; Replace pattern for substitute.
279 (defvar ex-repl nil)
281 ;; Pattern for global command.
282 (defvar ex-g-pat nil)
284 (defcustom ex-unix-type-shell
285 (let ((case-fold-search t))
286 (and (stringp shell-file-name)
287 (string-match
288 (concat
289 "\\("
290 "csh$\\|csh.exe$"
291 "\\|"
292 "ksh$\\|ksh.exe$"
293 "\\|"
294 "^sh$\\|sh.exe$"
295 "\\|"
296 "[^a-z]sh$\\|[^a-z]sh.exe$"
297 "\\|"
298 "bash$\\|bash.exe$"
299 "\\)")
300 shell-file-name)))
301 "Is the user using a unix-type shell under a non-OS?"
302 :type 'boolean
303 :group 'viper-ex)
305 (defcustom ex-unix-type-shell-options
306 (let ((case-fold-search t))
307 (if ex-unix-type-shell
308 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name)
309 "-f") ; csh: do it fast
310 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name)
311 "-noprofile") ; bash: ignore .profile
313 "Options to pass to the Unix-style shell.
314 Don't put `-c' here, as it is added automatically."
315 :type '(choice (const nil) string)
316 :group 'viper-ex)
318 (defcustom ex-compile-command "make"
319 "The command to run when the user types :make."
320 :type 'string
321 :group 'viper-ex)
323 (defcustom viper-glob-function
324 (cond (ex-unix-type-shell 'viper-glob-unix-files)
325 (viper-ms-style-os-p 'viper-glob-mswindows-files) ; Microsoft OS
326 (t 'viper-glob-unix-files) ; presumably UNIX
328 "Expand the file spec containing wildcard symbols.
329 The default tries to set this variable to work with Unix or MS Windows.
331 However, if it doesn't work right for some types of Unix shells or some OS,
332 the user should supply the appropriate function and set this variable to the
333 corresponding function symbol."
334 :type 'symbol
335 :group 'viper-ex)
338 ;; Remembers the previous Ex tag.
339 (defvar ex-tag nil)
341 ;; file used by Ex commands like :r, :w, :n
342 (defvar ex-file nil)
344 ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
345 (defvar ex-variant nil)
347 ;; Specified the offset of an Ex command, such as :read.
348 (defvar ex-offset nil)
350 ;; Tells Ex that this is a w>> command.
351 (defvar ex-append nil)
353 ;; File containing the shell command to be executed at Ex prompt,
354 ;; e.g., :r !date
355 (defvar ex-cmdfile nil)
356 (defvar ex-cmdfile-args "")
358 ;; flag used in viper-ex-read-file-name to indicate that we may be reading
359 ;; multiple file names. Used for :edit and :next
360 (defvar viper-keep-reading-filename nil)
362 (defcustom ex-cycle-other-window t
363 "If t, :n and :b cycles through files and buffers in other window.
364 Then :N and :B cycles in the current window. If nil, this behavior is
365 reversed."
366 :type 'boolean
367 :group 'viper-ex)
369 (defcustom ex-cycle-through-non-files nil
370 "Cycle through *scratch* and other buffers that don't visit any file."
371 :type 'boolean
372 :group 'viper-ex)
374 ;; Last shell command executed with :! command.
375 (defvar viper-ex-last-shell-com nil)
377 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
378 (defvar viper-incomplete-ex-cmd nil)
380 ;; Remembers the last ex-command prompt.
381 (defvar viper-last-ex-prompt "")
384 ;; Get a complete ex command
385 (defun viper-get-ex-com-subr ()
386 (let (cmd case-fold-search)
387 (set-mark (point))
388 (re-search-forward "[a-zA-Z][a-zA-Z]*")
389 (setq ex-token-type 'command)
390 (setq ex-token (buffer-substring (point) (mark t)))
391 (setq cmd (ex-cmd-assoc ex-token ex-token-alist))
392 (if cmd
393 (setq ex-token (car cmd))
394 (setq ex-token-type 'non-command))
397 ;; Get an ex-token which is either an address or a command.
398 ;; A token has a type, (command, address, end-mark), and a value
399 (defun viper-get-ex-token ()
400 (save-window-excursion
401 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
402 (set-buffer viper-ex-work-buf)
403 (skip-chars-forward " \t|")
404 (let ((case-fold-search t))
405 (cond ((looking-at "#")
406 (setq ex-token-type 'command)
407 (setq ex-token (char-to-string (following-char)))
408 (forward-char 1))
409 ((looking-at "[a-z]") (viper-get-ex-com-subr))
410 ((looking-at "\\.")
411 (forward-char 1)
412 (setq ex-token-type 'dot))
413 ((looking-at "[0-9]")
414 (set-mark (point))
415 (re-search-forward "[0-9]*")
416 (setq ex-token-type
417 (cond ((eq ex-token-type 'plus) 'add-number)
418 ((eq ex-token-type 'minus) 'sub-number)
419 (t 'abs-number)))
420 (setq ex-token
421 (string-to-number (buffer-substring (point) (mark t)))))
422 ((looking-at "\\$")
423 (forward-char 1)
424 (setq ex-token-type 'end))
425 ((looking-at "%")
426 (forward-char 1)
427 (setq ex-token-type 'whole))
428 ((looking-at "+")
429 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
430 (forward-char 1)
431 (insert "1")
432 (backward-char 1)
433 (setq ex-token-type 'plus))
434 ((looking-at "+[0-9]")
435 (forward-char 1)
436 (setq ex-token-type 'plus))
438 (error viper-BadAddress))))
439 ((looking-at "-")
440 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
441 (forward-char 1)
442 (insert "1")
443 (backward-char 1)
444 (setq ex-token-type 'minus))
445 ((looking-at "-[0-9]")
446 (forward-char 1)
447 (setq ex-token-type 'minus))
449 (error viper-BadAddress))))
450 ((looking-at "/")
451 (forward-char 1)
452 (set-mark (point))
453 (let ((cont t))
454 (while (and (not (eolp)) cont)
455 ;;(re-search-forward "[^/]*/")
456 (re-search-forward "[^/]*\\(/\\|\n\\)")
457 (if (not (looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"
458 (line-beginning-position 0)))
459 (setq cont nil))))
460 (backward-char 1)
461 (setq ex-token (buffer-substring (point) (mark t)))
462 (if (looking-at "/") (forward-char 1))
463 (setq ex-token-type 'search-forward))
464 ((looking-at "\\?")
465 (forward-char 1)
466 (set-mark (point))
467 (let ((cont t))
468 (while (and (not (eolp)) cont)
469 ;;(re-search-forward "[^\\?]*\\?")
470 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
471 (if (not (looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"
472 (line-beginning-position 0)))
473 (setq cont nil))
474 (backward-char 1)
475 (if (not (looking-at "\n")) (forward-char 1))))
476 (setq ex-token-type 'search-backward)
477 (setq ex-token (buffer-substring (1- (point)) (mark t))))
478 ((looking-at ",")
479 (forward-char 1)
480 (setq ex-token-type 'comma))
481 ((looking-at ";")
482 (forward-char 1)
483 (setq ex-token-type 'semi-colon))
484 ((looking-at "[!=><&~]")
485 (setq ex-token-type 'command)
486 (setq ex-token (char-to-string (following-char)))
487 (forward-char 1))
488 ((looking-at "'")
489 (setq ex-token-type 'goto-mark)
490 (forward-char 1)
491 (cond ((looking-at "'") (setq ex-token nil))
492 ((looking-at "[a-z]") (setq ex-token (following-char)))
493 (t (error "%s" "Marks are ' and a-z")))
494 (forward-char 1))
495 ((looking-at "\n")
496 (setq ex-token-type 'end-mark)
497 (setq ex-token "goto"))
499 (error viper-BadExCommand))))))
501 ;; Reads Ex command. Tries to determine if it has to exit because command
502 ;; is complete or invalid. If not, keeps reading command.
503 (defun ex-cmd-read-exit ()
504 (interactive)
505 (setq viper-incomplete-ex-cmd t)
506 (let ((quit-regex1 (concat
507 "\\(" "set[ \t]*"
508 "\\|" "edit[ \t]*"
509 "\\|" "[nN]ext[ \t]*"
510 "\\|" "unm[ \t]*"
511 "\\|" "^[ \t]*rep"
512 "\\)"))
513 (quit-regex2 (concat
514 "[a-zA-Z][ \t]*"
515 "\\(" "!" "\\|" ">>"
516 "\\|" "\\+[0-9]+"
517 "\\)"
518 "*[ \t]*$"))
519 (stay-regex (concat
520 "\\(" "^[ \t]*$"
521 "\\|" "[?/].*"
522 "\\|" "[ktgjmsz][ \t]*$"
523 "\\|" "^[ \t]*ab.*"
524 "\\|" "tr[ansfer \t]*"
525 "\\|" "sr[ \t]*"
526 "\\|" "mo.*"
527 "\\|" "^[ \t]*k?ma[^p]*"
528 "\\|" "^[ \t]*fi.*"
529 "\\|" "v?gl.*"
530 "\\|" "[vg][ \t]*$"
531 "\\|" "jo.*"
532 "\\|" "^[ \t]*ta.*"
533 "\\|" "^[ \t]*una.*"
534 ;; don't jump up in :s command
535 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*su.*"
536 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*s[^a-z].*"
537 "\\|" "['`][a-z][ \t]*"
538 ;; r! assumes that the next one is a shell command
539 "\\|" "\\(r\\|re\\|rea\\|read\\)[ \t]*!"
540 ;; w ! assumes that the next one is a shell command
541 "\\|" "\\(w\\|wr\\|wri\\|writ.?\\)[ \t]+!"
542 "\\|" "![ \t]*[a-zA-Z].*"
543 "\\)"
544 "!*")))
546 (save-window-excursion ;; put cursor at the end of the Ex working buffer
547 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
548 (set-buffer viper-ex-work-buf)
549 (goto-char (point-max)))
550 (cond ((looking-back quit-regex1) (exit-minibuffer))
551 ((looking-back stay-regex) (insert " "))
552 ((looking-back quit-regex2) (exit-minibuffer))
553 (t (insert " ")))))
555 (declare-function viper-tmp-insert-at-eob "viper-cmd" (msg))
557 ;; complete Ex command
558 (defun ex-cmd-complete ()
559 (interactive)
560 (let (save-pos dist compl-list string-to-complete completion-result)
562 (save-excursion
563 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
564 save-pos (point)))
566 (if (or (= dist 0)
567 (looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)"
568 (line-beginning-position))
569 (looking-back
570 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+"
571 (line-beginning-position)))
572 ;; Preceding characters are not the ones allowed in an Ex command
573 ;; or we have typed past command name.
574 ;; Note: we didn't do parsing, so there can be surprises.
575 (if (or (looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*"
576 (line-beginning-position))
577 (looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)"
578 (line-beginning-position))
579 (looking-at "[^ \t\n\C-m]"))
581 (with-output-to-temp-buffer "*Completions*"
582 (display-completion-list
583 (viper-alist-to-list ex-token-alist))))
584 ;; Preceding chars may be part of a command name
585 (setq string-to-complete (buffer-substring save-pos (point)))
586 (setq completion-result
587 (try-completion string-to-complete ex-token-alist))
589 (cond ((eq completion-result t) ; exact match--do nothing
590 (viper-tmp-insert-at-eob " (Sole completion)"))
591 ((eq completion-result nil)
592 (viper-tmp-insert-at-eob " (No match)"))
593 (t ;; partial completion
594 (goto-char save-pos)
595 (delete-region (point) (point-max))
596 (insert completion-result)
597 (let (case-fold-search)
598 (setq compl-list
599 (viper-filter-alist (concat "^" completion-result)
600 ex-token-alist)))
601 (if (> (length compl-list) 1)
602 (with-output-to-temp-buffer "*Completions*"
603 (display-completion-list
604 (viper-alist-to-list (reverse compl-list)))))))
608 (declare-function viper-enlarge-region "viper-cmd" (beg end))
609 (declare-function viper-read-string-with-history "viper-cmd"
610 (prompt &optional viper-initial history-var
611 default keymap init-message))
613 ;; Read Ex commands
614 ;; ARG is a prefix argument. If given, the ex command runs on the region
615 ;;(without the user having to specify the address :a,b
616 ;; STRING is the command to execute. If nil, then Viper asks you to enter the
617 ;; command.
618 (defun viper-ex (arg &optional string)
619 (interactive "P")
620 (or string
621 (setq ex-g-flag nil
622 ex-g-variant nil))
623 (let* ((map (copy-keymap minibuffer-local-map))
624 (address nil)
625 (cont t)
626 (dot (point))
627 reg-beg-line reg-end-line
628 reg-beg reg-end
629 initial-str
630 prev-token-type com-str)
631 (viper-add-keymap viper-ex-cmd-map map)
633 (if arg
634 (progn
635 (viper-enlarge-region (mark t) (point))
636 (if (> (point) (mark t))
637 (setq reg-beg (mark t)
638 reg-end (point))
639 (setq reg-end (mark t)
640 reg-beg (point)))
641 (save-excursion
642 (goto-char reg-beg)
643 (setq reg-beg-line (1+ (count-lines (point-min) (point)))
644 reg-end-line
645 (+ reg-beg-line (count-lines reg-beg reg-end) -1)))))
646 (if reg-beg-line
647 (setq initial-str (format "%d,%d" reg-beg-line reg-end-line)))
649 (setq com-str
650 (if string
651 (concat initial-str string)
652 (viper-read-string-with-history
654 initial-str
655 'viper-ex-history
656 ;; no default when working on region
657 (if initial-str
659 (car viper-ex-history))
661 (if initial-str
662 " [Type command to execute on current region]"))))
663 (save-window-excursion
664 ;; just a precaution
665 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
666 (set-buffer viper-ex-work-buf)
667 (delete-region (point-min) (point-max))
668 (insert com-str "\n")
669 (goto-char (point-min)))
670 (setq ex-token-type nil
671 ex-addresses nil)
672 (while cont
673 (viper-get-ex-token)
674 (cond ((memq ex-token-type '(command end-mark))
675 (if address (setq ex-addresses (cons address ex-addresses)))
676 (viper-deactivate-mark)
677 (let ((cmd (ex-cmd-assoc ex-token ex-token-alist)))
678 (if (null cmd)
679 (error "`%s': %s" ex-token viper-BadExCommand))
680 (ex-cmd-execute cmd)
681 (if (or (ex-cmd-is-mashed-with-args cmd)
682 (ex-cmd-is-one-letter cmd))
683 (setq cont nil)
684 (save-excursion
685 (save-window-excursion
686 (setq viper-ex-work-buf
687 (get-buffer-create viper-ex-work-buf-name))
688 (set-buffer viper-ex-work-buf)
689 (skip-chars-forward " \t")
690 (cond ((looking-at "|")
691 (forward-char 1))
692 ((looking-at "\n")
693 (setq cont nil))
694 (t (error
695 "`%s': %s" ex-token viper-SpuriousText)))
698 ((eq ex-token-type 'non-command)
699 (error "`%s': %s" ex-token viper-BadExCommand))
700 ((eq ex-token-type 'whole)
701 (setq address nil)
702 (setq ex-addresses
703 (if ex-addresses
704 (cons (point-max) ex-addresses)
705 (cons (point-max) (cons (point-min) ex-addresses)))))
706 ((eq ex-token-type 'comma)
707 (if (eq prev-token-type 'whole)
708 (setq address (point-min)))
709 (setq ex-addresses
710 (cons (if (null address) (point) address) ex-addresses)))
711 ((eq ex-token-type 'semi-colon)
712 (if (eq prev-token-type 'whole)
713 (setq address (point-min)))
714 (if address (setq dot address))
715 (setq ex-addresses
716 (cons (if (null address) (point) address) ex-addresses)))
717 (t (let ((ans (viper-get-ex-address-subr address dot)))
718 (if ans (setq address ans)))))
719 (setq prev-token-type ex-token-type))))
722 ;; Get a regular expression and set `ex-variant', if found
723 ;; Viper doesn't parse the substitution or search patterns.
724 ;; In particular, it doesn't expand ~ into the last substitution.
725 (defun viper-get-ex-pat ()
726 (save-window-excursion
727 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
728 (set-buffer viper-ex-work-buf)
729 (skip-chars-forward " \t")
730 (if (looking-at "!")
731 ;; this is probably a variant command r!
732 (progn
733 (setq ex-g-variant (not ex-g-variant)
734 ex-g-flag (not ex-g-flag))
735 (forward-char 1)
736 (skip-chars-forward " \t")))
737 (let ((c (following-char)))
738 (cond ((string-match "[0-9A-Za-z]" (format "%c" c))
739 (error
740 "Global regexp must be inside matching non-alphanumeric chars"))
741 ((= c ??) (error "`?' is not an allowed pattern delimiter here")))
742 (if (looking-at "[^\\\\\n]")
743 (progn
744 (forward-char 1)
745 (set-mark (point))
746 (let ((cont t))
747 ;; the use of eobp instead of eolp permits the use of newlines in
748 ;; pat2 in s/pat1/pat2/
749 (while (and (not (eobp)) cont)
750 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
751 (if (member ex-token '("global" "vglobal"))
752 (error "Missing closing delimiter for global regexp")
753 (goto-char (point-max))))
754 (if (not (looking-back
755 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)
756 (line-beginning-position 0)))
757 (setq cont nil)
758 ;; we are at an escaped delimiter: unescape it and continue
759 (delete-char -2)
760 (insert c)
761 (if (eolp)
762 ;; if at eol, exit loop and go to next line
763 ;; later, delim will be inserted at the end
764 (progn
765 (setq cont nil)
766 (forward-char))))
768 (setq ex-token
769 (if (= (mark t) (point)) ""
770 (buffer-substring (1- (point)) (mark t))))
771 (backward-char 1)
772 ;; if the user didn't insert the final pattern delimiter, we're
773 ;; at newline now. In this case, insert the initial delimiter
774 ;; specified in variable c
775 (if (eolp)
776 (progn
777 (insert c)
778 (backward-char 1)))
780 (setq ex-token nil))
781 c)))
783 ;; Get an Ex option g or c
784 (defun viper-get-ex-opt-gc (c)
785 (save-window-excursion
786 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
787 (set-buffer viper-ex-work-buf)
788 (if (looking-at (format "%c" c)) (forward-char 1))
789 (skip-chars-forward " \t")
790 (cond ((looking-at "g")
791 (setq ex-token "g")
792 (forward-char 1)
794 ((looking-at "c")
795 (setq ex-token "c")
796 (forward-char 1)
798 (t nil))))
800 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
801 (defun viper-default-ex-addresses (&optional whole-flag)
802 (cond ((null ex-addresses)
803 (setq ex-addresses
804 (if whole-flag
805 (list (point-max) (point-min))
806 (list (point) (point)))))
807 ((null (cdr ex-addresses))
808 (setq ex-addresses
809 (cons (car ex-addresses) ex-addresses)))))
811 ;; Get an ex-address as a marker and set ex-flag if a flag is found
812 (defun viper-get-ex-address ()
813 (let ((address (point-marker))
814 (cont t))
815 (setq ex-token "")
816 (setq ex-flag nil)
817 (while cont
818 (viper-get-ex-token)
819 (cond ((eq ex-token-type 'command)
820 (if (member ex-token '("print" "list" "#"))
821 (progn
822 (setq ex-flag t
823 cont nil))
824 (error "Address expected in this Ex command")))
825 ((eq ex-token-type 'end-mark)
826 (setq cont nil))
827 ((eq ex-token-type 'whole)
828 (error "Trailing address expected"))
829 ((eq ex-token-type 'comma)
830 (error "`%s': %s" ex-token viper-SpuriousText))
831 (t (let ((ans (viper-get-ex-address-subr address (point-marker))))
832 (if ans (setq address ans))))))
833 address))
835 (declare-function viper-register-to-point "viper-cmd"
836 (char &optional enforce-buffer))
838 ;; Returns an address as a point
839 (defun viper-get-ex-address-subr (old-address dot)
840 (let ((address nil))
841 (if (null old-address) (setq old-address dot))
842 (cond ((eq ex-token-type 'dot)
843 (setq address dot))
844 ((eq ex-token-type 'add-number)
845 (save-excursion
846 (goto-char old-address)
847 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
848 (setq address (point-marker))))
849 ((eq ex-token-type 'sub-number)
850 (save-excursion
851 (goto-char old-address)
852 (forward-line (- ex-token))
853 (setq address (point-marker))))
854 ((eq ex-token-type 'abs-number)
855 (save-excursion
856 (goto-char (point-min))
857 (if (= ex-token 0) (setq address 0)
858 (forward-line (1- ex-token))
859 (setq address (point-marker)))))
860 ((eq ex-token-type 'end)
861 (save-excursion
862 (goto-char (1- (point-max)))
863 (setq address (point-marker))))
864 ((eq ex-token-type 'plus) t) ; do nothing
865 ((eq ex-token-type 'minus) t) ; do nothing
866 ((eq ex-token-type 'search-forward)
867 (save-excursion
868 (ex-search-address t)
869 (setq address (point-marker))))
870 ((eq ex-token-type 'search-backward)
871 (save-excursion
872 (ex-search-address nil)
873 (setq address (point-marker))))
874 ((eq ex-token-type 'goto-mark)
875 (save-excursion
876 (if (null ex-token)
877 (exchange-point-and-mark)
878 (goto-char
879 (viper-register-to-point
880 (viper-int-to-char (1+ (- ex-token ?a))) 'enforce-buffer)))
881 (setq address (point-marker)))))
882 address))
885 ;; Search pattern and set address
886 ;; Doesn't wrap around. Should it?
887 (defun ex-search-address (forward)
888 (if (string= ex-token "")
889 (if (null viper-s-string)
890 (error viper-NoPrevSearch)
891 (setq ex-token viper-s-string))
892 (setq viper-s-string ex-token))
893 (if forward
894 (progn
895 (forward-line 1)
896 (re-search-forward ex-token))
897 (forward-line -1)
898 (re-search-backward ex-token)))
900 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
901 (defun viper-get-ex-buffer ()
902 (setq ex-buffer nil)
903 (setq ex-count nil)
904 (setq ex-flag nil)
905 (save-window-excursion
906 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
907 (set-buffer viper-ex-work-buf)
908 (skip-chars-forward " \t")
909 (if (looking-at "[a-zA-Z]")
910 (progn
911 (setq ex-buffer (following-char))
912 (forward-char 1)
913 (skip-chars-forward " \t")))
914 (if (looking-at "[0-9]")
915 (progn
916 (set-mark (point))
917 (re-search-forward "[0-9][0-9]*")
918 (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
919 (skip-chars-forward " \t")))
920 (if (looking-at "[pl#]")
921 (progn
922 (setq ex-flag t)
923 (forward-char 1)))
924 (if (not (looking-at "[\n|]"))
925 (error "`%s': %s" ex-token viper-SpuriousText))))
927 (defun viper-get-ex-count ()
928 (setq ex-variant nil
929 ex-count nil
930 ex-flag nil)
931 (save-window-excursion
932 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
933 (set-buffer viper-ex-work-buf)
934 (skip-chars-forward " \t")
935 (if (looking-at "!")
936 (progn
937 (setq ex-variant t)
938 (forward-char 1)))
939 (skip-chars-forward " \t")
940 (if (looking-at "[0-9]")
941 (progn
942 (set-mark (point))
943 (re-search-forward "[0-9][0-9]*")
944 (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
945 (skip-chars-forward " \t")))
946 (if (looking-at "[pl#]")
947 (progn
948 (setq ex-flag t)
949 (forward-char 1)))
950 (if (not (looking-at "[\n|]"))
951 (error "`%s': %s"
952 (buffer-substring
953 (point-min) (1- (point-max))) viper-BadExCommand))))
955 ;; Expand \% and \# in ex command
956 (defun ex-expand-filsyms (cmd buf)
957 (let (cf pf ret)
958 (with-current-buffer buf
959 (setq cf buffer-file-name)
960 (setq pf (ex-next nil t))) ; this finds alternative file name
961 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
962 (error "No current file to substitute for `%%'"))
963 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
964 (error "No alternate file to substitute for `#'"))
965 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
966 (erase-buffer)
967 (insert cmd)
968 (goto-char (point-min))
969 (while (re-search-forward "%\\|#" nil t)
970 (let ((data (match-data))
971 (char (buffer-substring (match-beginning 0) (match-end 0))))
972 (if (looking-back "\\\\." (- (point) 2))
973 (replace-match char)
974 (store-match-data data)
975 (if (string= char "%")
976 (replace-match cf)
977 (replace-match pf)))))
978 (end-of-line)
979 (setq ret (buffer-substring (point-min) (point)))
980 (message "%s" ret))
981 ret))
983 ;; Get a file name and set `ex-variant', `ex-append' and `ex-offset' if found
984 ;; If it is r!, then get the command name and whatever args
985 (defun viper-get-ex-file ()
986 (let (prompt)
987 (setq ex-file nil
988 ex-variant nil
989 ex-append nil
990 ex-offset nil
991 ex-cmdfile nil
992 ex-cmdfile-args "")
993 (save-excursion
994 (with-current-buffer (setq viper-ex-work-buf
995 (get-buffer-create viper-ex-work-buf-name))
996 (skip-chars-forward " \t")
997 (if (looking-at "!")
998 (if (and (not (looking-back "[ \t]" (1- (point))))
999 ;; read doesn't have a corresponding :r! form, so ! is
1000 ;; immediately interpreted as a shell command.
1001 (not (string= ex-token "read")))
1002 (progn
1003 (setq ex-variant t)
1004 (forward-char 1)
1005 (skip-chars-forward " \t"))
1006 (setq ex-cmdfile t)
1007 (forward-char 1)
1008 (skip-chars-forward " \t")))
1009 (if (looking-at ">>")
1010 (progn
1011 (setq ex-append t
1012 ex-variant t)
1013 (forward-char 2)
1014 (skip-chars-forward " \t")))
1015 (if (looking-at "+")
1016 (progn
1017 (forward-char 1)
1018 (set-mark (point))
1019 (re-search-forward "[ \t\n]")
1020 (backward-char 1)
1021 (setq ex-offset (buffer-substring (point) (mark t)))
1022 (forward-char 1)
1023 (skip-chars-forward " \t")))
1024 ;; this takes care of :r, :w, etc., when they get file names
1025 ;; from the history list
1026 (if (member ex-token '("read" "write" "edit" "visual" "next"))
1027 (progn
1028 (setq ex-file (buffer-substring (point) (1- (point-max))))
1029 (setq ex-file
1030 ;; For :e, match multiple non-white strings separated
1031 ;; by white. For others, find the first non-white string
1032 (if (string-match
1033 (if (string= ex-token "edit")
1034 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
1035 "[^ \t\n]+")
1036 ex-file)
1037 (progn
1038 ;; if file name comes from history, don't leave
1039 ;; minibuffer when the user types space
1040 (setq viper-incomplete-ex-cmd nil)
1041 (setq ex-cmdfile-args
1042 (substring ex-file (match-end 0) nil))
1043 ;; this must be the last clause in this progn
1044 (substring ex-file (match-beginning 0) (match-end 0))
1046 ""))
1047 ;; this leaves only the command name in the work area
1048 ;; file names are gone
1049 (delete-region (point) (1- (point-max)))
1051 (goto-char (point-max))
1052 (skip-chars-backward " \t\n")
1053 (setq prompt (buffer-substring (point-min) (point)))
1056 (setq viper-last-ex-prompt prompt)
1058 ;; If we just finished reading command, redisplay prompt
1059 (if viper-incomplete-ex-cmd
1060 (setq ex-file (viper-ex-read-file-name (format ":%s " prompt)))
1061 ;; file was typed in-line
1062 (setq ex-file (or ex-file "")))
1066 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
1067 ;; file names, arranges to re-enter the minibuffer.
1068 (defun viper-complete-filename-or-exit ()
1069 (interactive)
1070 (setq viper-keep-reading-filename t)
1071 ;; don't exit if directory---ex-commands don't
1072 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
1073 ;; apparently the argument to an Ex command is
1074 ;; supposed to be a shell command
1075 ((looking-back "^[ \t]*!.*" (line-beginning-position))
1076 (setq ex-cmdfile t)
1077 (insert " "))
1079 (setq ex-cmdfile nil)
1080 (minibuffer-complete-word))))
1082 (defun viper-handle-! ()
1083 (interactive)
1084 (if (and (string=
1085 (buffer-string) (viper-abbreviate-file-name default-directory))
1086 (member ex-token '("read" "write")))
1087 (erase-buffer))
1088 (insert "!"))
1090 (defun ex-cmd-accepts-multiple-files-p (token)
1091 (member token '("edit" "next" "Next")))
1093 ;; Read file name from the minibuffer in an ex command.
1094 ;; If user doesn't enter anything, then "" is returned, i.e., the
1095 ;; prompt-directory is not returned.
1096 (defun viper-ex-read-file-name (prompt)
1097 (let* ((str "")
1098 (minibuffer-local-completion-map
1099 (copy-keymap minibuffer-local-completion-map))
1100 beg end cont val)
1102 (viper-add-keymap ex-read-filename-map
1103 (if (featurep 'emacs)
1104 minibuffer-local-completion-map
1105 read-file-name-map))
1107 (setq cont (setq viper-keep-reading-filename t))
1108 (while cont
1109 (setq viper-keep-reading-filename nil
1110 val (read-file-name (concat prompt str) nil default-directory))
1111 (setq val (expand-file-name val))
1112 (if (and (string-match " " val)
1113 (ex-cmd-accepts-multiple-files-p ex-token))
1114 (setq val (concat "\"" val "\"")))
1115 (setq str (concat str (if (equal val "") "" " ")
1116 val (if (equal val "") "" " ")))
1118 ;; Only edit, next, and Next commands accept multiple files.
1119 ;; viper-keep-reading-filename is set in the anonymous function that is
1120 ;; bound to " " in ex-read-filename-map.
1121 (setq cont (and viper-keep-reading-filename
1122 (ex-cmd-accepts-multiple-files-p ex-token)))
1125 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
1126 end (string-match "[ \t]*$" str)) ; delete trailing blanks
1127 (if (member ex-token '("read" "write"))
1128 (if (string-match "[\t ]*!" str)
1129 ;; this is actually a shell command
1130 (progn
1131 (setq ex-cmdfile t)
1132 (setq beg (1+ beg))
1133 (setq viper-last-ex-prompt
1134 (concat viper-last-ex-prompt " !")))))
1135 (substring str (or beg 0) end)))
1138 (defun viper-undisplayed-files ()
1139 (mapcar
1140 (lambda (b)
1141 (if (null (get-buffer-window b))
1142 (let ((f (buffer-file-name b)))
1143 (if f f
1144 (if ex-cycle-through-non-files
1145 (let ((s (buffer-name b)))
1146 (if (string= " " (substring s 0 1))
1149 nil)))
1150 nil))
1151 (buffer-list)))
1154 (defun ex-args ()
1155 (let ((l (viper-undisplayed-files))
1156 (args "")
1157 (file-count 1))
1158 (while (not (null l))
1159 (if (car l)
1160 (setq args (format "%s %d) %s\n" args file-count (car l))
1161 file-count (1+ file-count)))
1162 (setq l (cdr l)))
1163 (if (string= args "")
1164 (message "All files are already displayed")
1165 (save-excursion
1166 (save-window-excursion
1167 (with-output-to-temp-buffer " *viper-info*"
1168 (princ "\n\nThese files are not displayed in any window.\n")
1169 (princ "\n=============\n")
1170 (princ args)
1171 (princ "\n=============\n")
1172 (princ "\nThe numbers can be given as counts to :next. ")
1173 (princ "\n\nPress any key to continue...\n\n"))
1174 (viper-read-event))))))
1176 ;; Ex cd command. Default directory of this buffer changes
1177 (defun ex-cd ()
1178 (viper-get-ex-file)
1179 (if (string= ex-file "")
1180 (setq ex-file "~"))
1181 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
1183 ;; Ex copy and move command. DEL-FLAG means delete
1184 (defun ex-copy (del-flag)
1185 (viper-default-ex-addresses)
1186 (let ((address (viper-get-ex-address))
1187 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1188 (goto-char end)
1189 (save-excursion
1190 (push-mark beg t)
1191 (viper-enlarge-region (mark t) (point))
1192 (if del-flag
1193 (kill-region (point) (mark t))
1194 (copy-region-as-kill (point) (mark t)))
1195 (if ex-flag
1196 (progn
1197 (with-output-to-temp-buffer " *copy text*"
1198 (princ
1199 (if (or del-flag ex-g-flag ex-g-variant)
1200 (current-kill 0)
1201 (buffer-substring (point) (mark t)))))
1202 (condition-case nil
1203 (progn
1204 (read-string "[Hit return to confirm] ")
1205 (save-excursion (kill-buffer " *copy text*")))
1206 (quit (save-excursion (kill-buffer " *copy text*"))
1207 (signal 'quit nil))))))
1208 (if (= address 0)
1209 (goto-char (point-min))
1210 (goto-char address)
1211 (forward-line 1))
1212 (insert (current-kill 0))))
1214 (declare-function viper-append-to-register "viper-cmd" (reg start end))
1216 ;; Ex delete command
1217 (defun ex-delete ()
1218 (viper-default-ex-addresses)
1219 (viper-get-ex-buffer)
1220 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1221 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1222 (save-excursion
1223 (viper-enlarge-region beg end)
1224 (exchange-point-and-mark)
1225 (if ex-count
1226 (progn
1227 (set-mark (point))
1228 (forward-line (1- ex-count)))
1229 (set-mark end))
1230 (viper-enlarge-region (point) (mark t))
1231 (if ex-flag
1232 ;; show text to be deleted and ask for confirmation
1233 (progn
1234 (with-output-to-temp-buffer " *delete text*"
1235 (princ (buffer-substring (point) (mark t))))
1236 (condition-case nil
1237 (read-string "[Hit return to confirm] ")
1238 (quit
1239 (save-excursion (kill-buffer " *delete text*"))
1240 (error "Viper bell")))
1241 (save-excursion (kill-buffer " *delete text*")))
1242 (if ex-buffer
1243 (cond ((viper-valid-register ex-buffer '(Letter))
1244 (viper-append-to-register
1245 (downcase ex-buffer) (point) (mark t)))
1246 ((viper-valid-register ex-buffer)
1247 (copy-to-register ex-buffer (point) (mark t) nil))
1248 (t (error viper-InvalidRegister ex-buffer))))
1249 (kill-region (point) (mark t))))))
1252 (declare-function viper-change-state-to-vi "viper-cmd" (&rest _))
1254 ;; Ex edit command
1255 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1256 ;; asked if current buffer should really be discarded.
1257 ;; This command can take multiple file names. It replaces the current buffer
1258 ;; with the first file in its argument list
1259 (defun ex-edit (&optional file)
1260 (if (not file)
1261 (viper-get-ex-file))
1262 (cond ((and (string= ex-file "") buffer-file-name)
1263 (setq ex-file (viper-abbreviate-file-name (buffer-file-name))))
1264 ((string= ex-file "")
1265 (error viper-NoFileSpecified)))
1267 (let (msg do-edit)
1268 (if buffer-file-name
1269 (cond ((buffer-modified-p)
1270 (setq msg
1271 (format "Buffer %s is modified. Discard changes? "
1272 (buffer-name))
1273 do-edit t))
1274 ((not (verify-visited-file-modtime (current-buffer)))
1275 (setq msg
1276 (format "File %s changed on disk. Reread from disk? "
1277 buffer-file-name)
1278 do-edit t))
1279 (t (setq do-edit nil))))
1281 (if do-edit
1282 (if (yes-or-no-p msg)
1283 (progn
1284 (set-buffer-modified-p nil)
1285 (kill-buffer (current-buffer)))
1286 (message "Buffer %s was left intact" (buffer-name))))
1287 ) ; let
1289 (if (null (setq file (get-file-buffer ex-file)))
1290 (progn
1291 ;; this also does shell-style globbing
1292 (ex-find-file
1293 ;; replace # and % with the previous/current file
1294 (ex-expand-filsyms ex-file (current-buffer)))
1295 (or (eq major-mode 'dired-mode)
1296 (viper-change-state-to-vi))
1297 (goto-char (point-min)))
1298 (switch-to-buffer file))
1299 (if ex-offset
1300 (progn
1301 (with-current-buffer (setq viper-ex-work-buf
1302 (get-buffer-create viper-ex-work-buf-name))
1303 (delete-region (point-min) (point-max))
1304 (insert ex-offset "\n")
1305 (goto-char (point-min)))
1306 (goto-char (viper-get-ex-address))
1307 (beginning-of-line)))
1308 (ex-fixup-history viper-last-ex-prompt ex-file))
1310 ;; Find-file FILESPEC if it appears to specify a single file.
1311 ;; Otherwise, assume that FILESPEC is a wildcard.
1312 ;; In this case, split it into substrings separated by newlines.
1313 ;; Each line is assumed to be a file name.
1314 (defun ex-find-file (filespec)
1315 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1316 (cond ((file-exists-p filespec) (find-file filespec))
1317 ((string-match nonstandard-filename-chars filespec)
1318 (mapcar 'find-file (funcall viper-glob-function filespec)))
1319 (t (find-file filespec)))
1323 (declare-function viper-backward-char-carefully "viper-cmd" (&optional arg))
1325 ;; Ex global command
1326 ;; This is executed in response to:
1327 ;; :global "pattern" ex-command
1328 ;; :vglobal "pattern" ex-command
1329 ;; :global executes ex-command on all lines matching <pattern>
1330 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1332 ;; With VARIANT nil, this functions executes :global
1333 ;; With VARIANT t, executes :vglobal
1334 (defun ex-global (variant)
1335 (let ((gcommand ex-token))
1336 (if (or ex-g-flag ex-g-variant)
1337 (error "`%s' within `global' is not allowed" gcommand)
1338 (if variant
1339 (setq ex-g-flag nil
1340 ex-g-variant t)
1341 (setq ex-g-flag t
1342 ex-g-variant nil)))
1343 (viper-get-ex-pat)
1344 (if (null ex-token)
1345 (error "`%s': Missing regular expression" gcommand)))
1347 (if (string= ex-token "")
1348 (if (null viper-s-string)
1349 (error viper-NoPrevSearch)
1350 (setq ex-g-pat viper-s-string))
1351 (setq ex-g-pat ex-token
1352 viper-s-string ex-token))
1353 (if (null ex-addresses)
1354 (setq ex-addresses (list (point-max) (point-min)))
1355 (viper-default-ex-addresses))
1356 (setq ex-g-marks nil)
1357 (let ((mark-count 0)
1358 (end (car ex-addresses))
1359 (beg (car (cdr ex-addresses)))
1360 com-str)
1361 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1362 (save-excursion
1363 (viper-enlarge-region beg end)
1364 (exchange-point-and-mark)
1365 (let ((cont t) (limit (point-marker)))
1366 (exchange-point-and-mark)
1367 ;; skip the last line if empty
1368 (beginning-of-line)
1369 (if (eobp) (viper-backward-char-carefully))
1370 (while (and cont (not (bobp)) (>= (point) limit))
1371 (beginning-of-line)
1372 (set-mark (point))
1373 (end-of-line)
1374 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1375 (if (or (and ex-g-flag found)
1376 (and ex-g-variant (not found)))
1377 (progn
1378 (end-of-line)
1379 (setq mark-count (1+ mark-count))
1380 (setq ex-g-marks (cons (point-marker) ex-g-marks)))))
1381 (beginning-of-line)
1382 (if (bobp) (setq cont nil)
1383 (forward-line -1)
1384 (end-of-line)))))
1385 (with-current-buffer (setq viper-ex-work-buf
1386 (get-buffer-create viper-ex-work-buf-name))
1387 ;; com-str is the command string, i.e., g/pattern/ or v/pattern'
1388 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1389 (while ex-g-marks
1390 (goto-char (car ex-g-marks))
1391 (viper-ex nil com-str)
1392 (setq mark-count (1- mark-count))
1393 (setq ex-g-marks (cdr ex-g-marks)))))
1395 ;; Ex goto command
1396 (defun ex-goto ()
1397 (if (null ex-addresses)
1398 (setq ex-addresses (cons (point) nil)))
1399 (push-mark (point) t)
1400 (goto-char (car ex-addresses))
1401 (beginning-of-line)
1404 ;; Ex line commands. COM is join, shift-right or shift-left
1405 (defun ex-line (com)
1406 (viper-default-ex-addresses)
1407 (viper-get-ex-count)
1408 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1409 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1410 (save-excursion
1411 (viper-enlarge-region beg end)
1412 (exchange-point-and-mark)
1413 (if ex-count
1414 (progn
1415 (set-mark (point))
1416 (forward-line ex-count)))
1417 (if ex-flag
1418 ;; show text to be joined and ask for confirmation
1419 (progn
1420 (with-output-to-temp-buffer " *join text*"
1421 (princ (buffer-substring (point) (mark t))))
1422 (condition-case nil
1423 (progn
1424 (read-string "[Hit return to confirm] ")
1425 (ex-line-subr com (point) (mark t)))
1426 (quit (ding)))
1427 (save-excursion (kill-buffer " *join text*")))
1428 (ex-line-subr com (point) (mark t)))
1429 (setq point (point)))
1430 (goto-char (1- point))
1431 (beginning-of-line)))
1433 (declare-function viper-forward-char-carefully "viper-cmd" (&optional arg))
1435 (defun ex-line-subr (com beg end)
1436 (cond ((string= com "join")
1437 (goto-char (min beg end))
1438 (while (and (not (eobp)) (< (point) (max beg end)))
1439 (end-of-line)
1440 (if (and (<= (point) (max beg end)) (not (eobp)))
1441 (progn
1442 (forward-line 1)
1443 (delete-region (point) (1- (point)))
1444 (if (not ex-variant) (fixup-whitespace))))))
1445 ((or (string= com "right") (string= com "left"))
1446 (indent-rigidly
1447 (min beg end) (max beg end)
1448 (if (string= com "right") viper-shift-width (- viper-shift-width)))
1449 (goto-char (max beg end))
1450 (end-of-line)
1451 (viper-forward-char-carefully))))
1454 ;; Ex mark command
1455 ;; Sets the mark to the current point.
1456 ;; If name is omitted, get the name straight from the work buffer."
1457 (defun ex-mark (&optional name)
1458 (let (char)
1459 (if (null ex-addresses)
1460 (setq ex-addresses
1461 (cons (point) nil)))
1462 (if name
1463 (if (eq 1 (length name))
1464 (setq char (string-to-char name))
1465 (error "`%s': Spurious text \"%s\" after mark name"
1466 name (substring name 1)))
1467 (with-current-buffer (setq viper-ex-work-buf
1468 (get-buffer-create viper-ex-work-buf-name))
1469 (skip-chars-forward " \t")
1470 (if (looking-at "[a-z]")
1471 (progn
1472 (setq char (following-char))
1473 (forward-char 1)
1474 (skip-chars-forward " \t")
1475 (if (not (looking-at "[\n|]"))
1476 (error "`%s': %s" ex-token viper-SpuriousText)))
1477 (error "`%s' requires a following letter" ex-token))))
1478 (save-excursion
1479 (goto-char (car ex-addresses))
1480 (point-to-register (viper-int-to-char (1+ (- char ?a)))))))
1484 ;; Alternate file is the file next to the first one in the buffer ring
1485 (defun ex-next (cycle-other-window &optional find-alt-file)
1486 (catch 'ex-edit
1487 (let (count l)
1488 (if (not find-alt-file)
1489 (progn
1490 (viper-get-ex-file)
1491 (if (or (char-or-string-p ex-offset)
1492 (and (not (string= "" ex-file))
1493 (not (string-match "^[0-9]+$" ex-file))))
1494 (progn
1495 (ex-edit t)
1496 (throw 'ex-edit nil))
1497 (setq count (string-to-number ex-file))
1498 (if (= count 0) (setq count 1))
1499 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1500 (setq count 1))
1501 (setq l (viper-undisplayed-files))
1502 (while (> count 0)
1503 (while (and (not (null l)) (null (car l)))
1504 (setq l (cdr l)))
1505 (setq count (1- count))
1506 (if (> count 0)
1507 (setq l (cdr l))))
1508 (if find-alt-file (car l)
1509 (progn
1510 (if (and (car l) (get-file-buffer (car l)))
1511 (let* ((w (if cycle-other-window
1512 (get-lru-window) (selected-window)))
1513 (b (window-buffer w)))
1514 (set-window-buffer w (get-file-buffer (car l)))
1515 (bury-buffer b)
1516 ;; this puts "next <count>" in the ex-command history
1517 (ex-fixup-history viper-last-ex-prompt ex-file))
1518 (error "Not that many undisplayed files")))))))
1521 (defun ex-next-related-buffer (direction &optional no-recursion)
1523 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction)
1525 (let ((file-or-buffer-name
1526 (viper-current-ring-item viper-related-files-and-buffers-ring))
1527 (old-ring viper-related-files-and-buffers-ring)
1528 (old-win (selected-window))
1529 skip-rest buf wind)
1531 (or (and (ring-p viper-related-files-and-buffers-ring)
1532 (> (ring-length viper-related-files-and-buffers-ring) 0))
1533 (error "This buffer has no related files or buffers"))
1535 (or (stringp file-or-buffer-name)
1536 (error
1537 "File and buffer names must be strings, %S" file-or-buffer-name))
1539 (setq buf (cond ((get-buffer file-or-buffer-name))
1540 ((file-exists-p file-or-buffer-name)
1541 (find-file-noselect file-or-buffer-name))
1544 (if (not (viper-buffer-live-p buf))
1545 (error "Didn't find buffer %S or file %S"
1546 file-or-buffer-name
1547 (viper-abbreviate-file-name
1548 (expand-file-name file-or-buffer-name))))
1550 (if (equal buf (current-buffer))
1551 (or no-recursion
1552 ;; try again
1553 (progn
1554 (setq skip-rest t)
1555 (ex-next-related-buffer direction 'norecursion))))
1557 (if skip-rest
1559 ;; setup buffer
1560 (if (setq wind (viper-get-visible-buffer-window buf))
1562 (setq wind (get-lru-window (if (featurep 'xemacs) nil 'visible)))
1563 (set-window-buffer wind buf))
1565 (if (viper-window-display-p)
1566 (progn
1567 (raise-frame (window-frame wind))
1568 (if (equal (window-frame wind) (window-frame old-win))
1569 (save-window-excursion (select-window wind) (sit-for 1))
1570 (select-window wind)))
1571 (save-window-excursion (select-window wind) (sit-for 1)))
1573 (with-current-buffer buf
1574 (setq viper-related-files-and-buffers-ring old-ring))
1576 (setq viper-local-search-start-marker (point-marker))
1580 ;; Force auto save
1581 (defun ex-preserve ()
1582 (message "Autosaving all buffers that need to be saved...")
1583 (do-auto-save t))
1585 (declare-function viper-Put-back "viper-cmd" (arg))
1586 (declare-function viper-put-back "viper-cmd" (arg))
1588 ;; Ex put
1589 (defun ex-put ()
1590 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1591 (viper-get-ex-buffer)
1592 (setq viper-use-register ex-buffer)
1593 (goto-char point)
1594 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
1596 ;; Ex print working directory
1597 (defun ex-pwd ()
1598 (message "%s" default-directory))
1600 ;; Ex quit command
1601 (defun ex-quit ()
1602 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1603 (with-current-buffer (setq viper-ex-work-buf
1604 (get-buffer-create viper-ex-work-buf-name))
1605 (if (looking-at "!") (forward-char 1)))
1606 (if (< viper-expert-level 3)
1607 (save-buffers-kill-emacs)
1608 (kill-buffer (current-buffer))))
1611 (declare-function viper-add-newline-at-eob-if-necessary "viper-cmd" ())
1613 ;; Ex read command
1614 ;; ex-read doesn't support wildcards, because file completion is a better
1615 ;; mechanism. We also don't support # and % (except in :r <shell-command>
1616 ;; because file history is a better mechanism.
1617 (defun ex-read ()
1618 (viper-get-ex-file)
1619 (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
1620 command)
1621 (goto-char point)
1622 (viper-add-newline-at-eob-if-necessary)
1623 (if (not (or (bobp) (eobp))) (forward-line 1))
1624 (if (and (not ex-variant) (string= ex-file ""))
1625 (progn
1626 (if (null buffer-file-name)
1627 (error viper-NoFileSpecified))
1628 (setq ex-file buffer-file-name)))
1629 (if ex-cmdfile
1630 (progn
1631 (setq command
1632 ;; replace # and % with the previous/current file
1633 (ex-expand-filsyms
1634 (concat (shell-quote-argument ex-file) ex-cmdfile-args)
1635 (current-buffer)))
1636 (shell-command command t))
1637 (insert-file-contents ex-file)))
1638 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args))
1640 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1641 (defun ex-fixup-history (&rest args)
1642 (setq viper-ex-history
1643 (cons (mapconcat 'identity args " ") (cdr viper-ex-history))))
1646 ;; Ex recover from emacs \#file\#
1647 (defun ex-recover ()
1648 (viper-get-ex-file)
1649 (if (or ex-append ex-offset)
1650 (error "`recover': %s" viper-SpuriousText))
1651 (if (string= ex-file "")
1652 (progn
1653 (if (null buffer-file-name)
1654 (error "This buffer isn't visiting any file"))
1655 (setq ex-file buffer-file-name))
1656 (setq ex-file (expand-file-name ex-file)))
1657 (if (and (not (string= ex-file (buffer-file-name)))
1658 (buffer-modified-p)
1659 (not ex-variant))
1660 (error "No write since last change (:rec! overrides)"))
1661 (recover-file ex-file))
1663 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1664 (defun ex-rewind ()
1665 (message
1666 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1669 ;; read variable name for ex-set
1670 (defun ex-set-read-variable ()
1671 (let ((minibuffer-local-completion-map
1672 (copy-keymap minibuffer-local-completion-map))
1673 (cursor-in-echo-area t)
1674 str batch)
1675 (define-key
1676 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1677 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1678 (if (viper-set-unread-command-events
1679 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1680 (progn
1681 (setq batch t)
1682 (viper-set-unread-command-events ?\C-m)))
1683 (message ":set <Variable> [= <Value>]")
1684 (or batch (sit-for 2))
1686 (while (string-match "^[ \\t\\n]*$"
1687 (setq str
1688 (completing-read ":set " ex-variable-alist)))
1689 (message ":set <Variable> [= <Value>]")
1690 ;; if there are unread events, don't wait
1691 (or (viper-set-unread-command-events "") (sit-for 2))
1692 ) ; while
1693 str))
1696 (defun ex-set ()
1697 (let ((var (ex-set-read-variable))
1698 (val 0)
1699 (set-cmd "setq")
1700 (ask-if-save t)
1701 (auto-cmd-label "; don't touch or else...")
1702 (delete-turn-on-auto-fill-pattern
1703 "([ \t]*add-hook[ \t]+'viper-insert-state-hook[ \t]+'turn-on-auto-fill.*)")
1704 actual-lisp-cmd lisp-cmd-del-pattern
1705 val2 orig-var)
1706 (setq orig-var var)
1707 (cond ((string= var "all")
1708 (setq ask-if-save nil
1709 set-cmd nil))
1710 ((member var '("ai" "autoindent"))
1711 (setq var "viper-auto-indent"
1712 set-cmd "setq"
1713 ask-if-save nil
1714 val "t"))
1715 ((member var '("ai-g" "autoindent-global"))
1716 (kill-local-variable 'viper-auto-indent)
1717 (setq var "viper-auto-indent"
1718 set-cmd "setq-default"
1719 val "t"))
1720 ((member var '("noai" "noautoindent"))
1721 (setq var "viper-auto-indent"
1722 ask-if-save nil
1723 val "nil"))
1724 ((member var '("noai-g" "noautoindent-global"))
1725 (kill-local-variable 'viper-auto-indent)
1726 (setq var "viper-auto-indent"
1727 set-cmd "setq-default"
1728 val "nil"))
1729 ((member var '("ic" "ignorecase"))
1730 (setq var "viper-case-fold-search"
1731 val "t"))
1732 ((member var '("noic" "noignorecase"))
1733 (setq var "viper-case-fold-search"
1734 val "nil"))
1735 ((member var '("ma" "magic"))
1736 (setq var "viper-re-search"
1737 val "t"))
1738 ((member var '("noma" "nomagic"))
1739 (setq var "viper-re-search"
1740 val "nil"))
1741 ((member var '("ro" "readonly"))
1742 (setq var "buffer-read-only"
1743 val "t"))
1744 ((member var '("noro" "noreadonly"))
1745 (setq var "buffer-read-only"
1746 val "nil"))
1747 ((member var '("sm" "showmatch"))
1748 (setq var "blink-matching-paren"
1749 val "t"))
1750 ((member var '("nosm" "noshowmatch"))
1751 (setq var "blink-matching-paren"
1752 val "nil"))
1753 ((member var '("ws" "wrapscan"))
1754 (setq var "viper-search-wrap-around"
1755 val "t"))
1756 ((member var '("nows" "nowrapscan"))
1757 (setq var "viper-search-wrap-around"
1758 val "nil")))
1759 (if (and set-cmd (eq val 0)) ; value must be set by the user
1760 (let ((cursor-in-echo-area t))
1761 (message ":set %s = <Value>" var)
1762 ;; if there are unread events, don't wait
1763 (or (viper-set-unread-command-events "") (sit-for 2))
1764 (setq val (read-string (format ":set %s = " var)))
1765 (ex-fixup-history "set" orig-var val)
1767 ;; check numerical values
1768 (if (member var
1769 '("sw" "shiftwidth"
1770 "ts" "tabstop"
1771 "ts-g" "tabstop-global"
1772 "wm" "wrapmargin"))
1773 (condition-case nil
1774 (or (numberp (setq val2 (car (read-from-string val))))
1775 (error "%s: Invalid value, numberp, %S" var val))
1776 (error
1777 (error "%s: Invalid value, numberp, %S" var val))))
1779 (cond
1780 ((member var '("sw" "shiftwidth"))
1781 (setq var "viper-shift-width"))
1782 ((member var '("ts" "tabstop"))
1783 ;; make it take effect in curr buff and new bufs
1784 (setq var "tab-width"
1785 set-cmd "setq"
1786 ask-if-save nil))
1787 ((member var '("ts-g" "tabstop-global"))
1788 (kill-local-variable 'tab-width)
1789 (setq var "tab-width"
1790 set-cmd "setq-default"))
1791 ((member var '("wm" "wrapmargin"))
1792 ;; make it take effect in curr buff and new bufs
1793 (kill-local-variable 'fill-column)
1794 (setq var "fill-column"
1795 val (format "(- (window-width) %s)" val)
1796 set-cmd "setq-default"))
1797 ((member var '("sh" "shell"))
1798 (setq var "explicit-shell-file-name"
1799 val (format "\"%s\"" val)))))
1800 (ex-fixup-history "set" orig-var))
1802 (if set-cmd
1803 (setq actual-lisp-cmd
1804 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label)
1805 lisp-cmd-del-pattern
1806 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1807 set-cmd var auto-cmd-label)))
1809 (if (and ask-if-save
1810 (y-or-n-p (format "Do you want to save this setting in %s "
1811 viper-custom-file-name)))
1812 (progn
1813 (viper-save-string-in-file
1814 actual-lisp-cmd viper-custom-file-name
1815 ;; del pattern
1816 lisp-cmd-del-pattern)
1817 (if (string= var "fill-column")
1818 (if (> val2 0)
1819 (viper-save-string-in-file
1820 (concat
1821 "(add-hook 'viper-insert-state-hook 'turn-on-auto-fill) "
1822 auto-cmd-label)
1823 viper-custom-file-name
1824 delete-turn-on-auto-fill-pattern)
1825 (viper-save-string-in-file
1826 nil viper-custom-file-name delete-turn-on-auto-fill-pattern)
1827 (viper-save-string-in-file
1828 nil viper-custom-file-name
1829 ;; del pattern
1830 lisp-cmd-del-pattern)
1834 (if set-cmd
1835 (message "%s %s %s"
1836 set-cmd var
1837 (if (string-match "^[ \t]*$" val)
1838 (format "%S" val)
1839 val)))
1840 (if actual-lisp-cmd
1841 (eval (car (read-from-string actual-lisp-cmd))))
1842 (if (string= var "fill-column")
1843 (if (> val2 0)
1844 (auto-fill-mode 1)
1845 (auto-fill-mode -1)))
1846 (if (string= var "all") (ex-show-vars))
1849 ;; In inline args, skip regex-forw and (optionally) chars-back.
1850 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1851 ;; special meaning
1852 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1853 (with-current-buffer (setq viper-ex-work-buf
1854 (get-buffer-create viper-ex-work-buf-name))
1855 (goto-char (point-min))
1856 (re-search-forward regex-forw nil t)
1857 (let ((beg (point))
1858 end)
1859 (goto-char (point-max))
1860 (if chars-back
1861 (skip-chars-backward chars-back)
1862 (skip-chars-backward " \t\n\C-m"))
1863 (setq end (point))
1864 ;; replace SPC with `=' to suppress the special meaning SPC has
1865 ;; in Ex commands
1866 (goto-char beg)
1867 (if replace-str
1868 (while (re-search-forward " +" nil t)
1869 (replace-match replace-str nil t)
1870 (viper-forward-char-carefully)))
1871 (goto-char end)
1872 (buffer-substring beg end))))
1875 ;; Ex shell command
1876 (defun ex-shell ()
1877 (shell))
1879 ;; Viper help. Invokes Info
1880 (defun ex-help ()
1881 (condition-case nil
1882 (progn
1883 (pop-to-buffer (get-buffer-create "*info*"))
1884 (info (if (featurep 'xemacs) "viper.info" "viper"))
1885 (message "Type `i' to search for a specific topic"))
1886 (error (beep 1)
1887 (with-output-to-temp-buffer " *viper-info*"
1888 (princ (format "
1889 The Info file for Viper does not seem to be installed.
1891 This file is part of the standard distribution of %sEmacs.
1892 Please contact your system administrator. "
1893 (if (featurep 'xemacs) "X" "")
1894 ))))))
1896 ;; Ex source command.
1897 ;; Loads the file specified as argument or viper-custom-file-name.
1898 (defun ex-source ()
1899 (viper-get-ex-file)
1900 (if (string= ex-file "")
1901 (load viper-custom-file-name)
1902 (load ex-file)))
1904 ;; Ex substitute command
1905 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1906 (defun ex-substitute (&optional repeat r-flag)
1907 (let ((opt-g nil)
1908 (opt-c nil)
1909 (matched-pos nil)
1910 (case-fold-search viper-case-fold-search)
1911 delim pat repl)
1912 (if repeat (setq ex-token nil) (setq delim (viper-get-ex-pat)))
1913 (if (null ex-token)
1914 (progn
1915 (setq pat (if r-flag viper-s-string ex-reg-exp))
1916 (or (stringp pat)
1917 (error "No previous pattern to use in substitution"))
1918 (setq repl ex-repl
1919 delim (string-to-char pat)))
1920 (setq pat (if (string= ex-token "") viper-s-string ex-token))
1921 (setq viper-s-string pat
1922 ex-reg-exp pat)
1923 (setq delim (viper-get-ex-pat))
1924 (if (null ex-token)
1925 (setq ex-token ""
1926 ex-repl "")
1927 (setq repl ex-token
1928 ex-repl ex-token)))
1929 (while (viper-get-ex-opt-gc delim)
1930 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1931 (viper-get-ex-count)
1932 (if ex-count
1933 (save-excursion
1934 (if ex-addresses (goto-char (car ex-addresses)))
1935 (set-mark (point))
1936 (forward-line (1- ex-count))
1937 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1938 (if (null ex-addresses)
1939 (setq ex-addresses (cons (point) (cons (point) nil)))
1940 (if (null (cdr ex-addresses))
1941 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1942 ;(setq G opt-g)
1943 (let ((beg (car ex-addresses))
1944 (end (car (cdr ex-addresses)))
1945 eol-mark)
1946 (save-excursion
1947 (viper-enlarge-region beg end)
1948 (let ((limit (save-excursion
1949 (goto-char (max (point) (mark t)))
1950 (point-marker))))
1951 (goto-char (min (point) (mark t)))
1952 (while (< (point) limit)
1953 (save-excursion
1954 (end-of-line)
1955 ;; This move allows the use of newline as the last character in
1956 ;; the substitution pattern
1957 (viper-forward-char-carefully)
1958 (setq eol-mark (point-marker)))
1959 (beginning-of-line)
1960 (if opt-g
1961 (progn
1962 (while (and (not (eolp))
1963 (re-search-forward pat eol-mark t))
1964 (if (or (not opt-c)
1965 (progn
1966 (viper-put-on-search-overlay (match-beginning 0)
1967 (match-end 0))
1968 (y-or-n-p "Replace? ")))
1969 (progn
1970 (viper-hide-search-overlay)
1971 (setq matched-pos (point))
1972 (if (not (stringp repl))
1973 (error "Can't perform Ex substitution: No previous replacement pattern"))
1974 (replace-match repl t))))
1975 (end-of-line)
1976 (viper-forward-char-carefully))
1977 (if (null pat)
1978 (error
1979 "Can't repeat Ex substitution: No previous regular expression"))
1980 (if (and (re-search-forward pat eol-mark t)
1981 (or (not opt-c)
1982 (progn
1983 (viper-put-on-search-overlay (match-beginning 0)
1984 (match-end 0))
1985 (y-or-n-p "Replace? "))))
1986 (progn
1987 (viper-hide-search-overlay)
1988 (setq matched-pos (point))
1989 (if (not (stringp repl))
1990 (error "Can't perform Ex substitution: No previous replacement pattern"))
1991 (replace-match repl t)))
1992 ;;(end-of-line)
1993 ;;(viper-forward-char-carefully)
1994 (goto-char eol-mark)
1995 )))))
1996 (if matched-pos (goto-char matched-pos))
1997 (beginning-of-line)
1998 (if opt-c (message "done"))))
2000 (declare-function viper-change-state-to-emacs "viper-cmd" (&rest _))
2002 ;; Ex tag command
2003 (defun ex-tag ()
2004 (let (tag)
2005 (with-current-buffer (setq viper-ex-work-buf
2006 (get-buffer-create viper-ex-work-buf-name))
2007 (skip-chars-forward " \t")
2008 (set-mark (point))
2009 (skip-chars-forward "^ |\t\n")
2010 (setq tag (buffer-substring (mark t) (point))))
2011 (if (not (string= tag "")) (setq ex-tag tag))
2012 (viper-change-state-to-emacs)
2013 (condition-case conds
2014 (progn
2015 (if (string= tag "")
2016 (find-tag ex-tag t)
2017 (find-tag-other-window ex-tag))
2018 (viper-change-state-to-vi))
2019 (error
2020 (viper-change-state-to-vi)
2021 (viper-message-conditions conds)))))
2023 ;; Ex write command
2024 ;; ex-write doesn't support wildcards, because file completion is a better
2025 ;; mechanism. We also don't support # and %
2026 ;; because file history is a better mechanism.
2027 (defun ex-write (q-flag)
2028 (viper-default-ex-addresses t)
2029 (viper-get-ex-file)
2030 (let ((end (car ex-addresses))
2031 (beg (car (cdr ex-addresses)))
2032 (orig-buf (current-buffer))
2033 ;;(orig-buf-file-name (buffer-file-name))
2034 ;;(orig-buf-name (buffer-name))
2035 ;;(buff-changed-p (buffer-modified-p))
2036 temp-buf writing-same-file region
2037 file-exists writing-whole-file)
2038 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2039 (if ex-cmdfile
2040 (progn
2041 (viper-enlarge-region beg end)
2042 (shell-command-on-region (point) (mark t)
2043 (concat ex-file ex-cmdfile-args)))
2044 (if (and (string= ex-file "") (not (buffer-file-name)))
2045 (setq ex-file
2046 (read-file-name
2047 (format "Buffer %s isn't visiting any file. File to save in: "
2048 (buffer-name)))))
2050 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
2051 ex-file (if (string= ex-file "")
2052 (buffer-file-name)
2053 (expand-file-name ex-file)))
2054 ;; if ex-file is a directory use the file portion of the buffer file name
2055 (if (and (file-directory-p ex-file)
2056 buffer-file-name
2057 (not (file-directory-p buffer-file-name)))
2058 (setq ex-file
2059 (concat (file-name-as-directory ex-file)
2060 (file-name-nondirectory buffer-file-name))))
2062 (setq file-exists (file-exists-p ex-file)
2063 writing-same-file (string= ex-file (buffer-file-name)))
2065 ;; do actual writing
2066 (if (and writing-whole-file writing-same-file)
2067 ;; saving whole buffer in visited file
2068 (if (not (buffer-modified-p))
2069 (message "(No changes need to be saved)")
2070 (viper-maybe-checkout (current-buffer))
2071 (save-buffer)
2072 (save-restriction
2073 (widen)
2074 (ex-write-info file-exists ex-file (point-min) (point-max))
2076 ;; writing to non-visited file and it already exists
2077 (if (and file-exists (not writing-same-file)
2078 (not (yes-or-no-p
2079 (format "File %s exists. Overwrite? " ex-file))))
2080 (error "Quit"))
2081 ;; writing a region or whole buffer to non-visited file
2082 (unwind-protect
2083 (save-excursion
2084 (viper-enlarge-region beg end)
2085 (setq region (buffer-substring (point) (mark t)))
2086 ;; create temp buffer for the region
2087 (setq temp-buf (get-buffer-create " *ex-write*"))
2088 (set-buffer temp-buf)
2089 (if (featurep 'xemacs)
2090 (set-visited-file-name ex-file)
2091 (set-visited-file-name ex-file 'noquery))
2092 (erase-buffer)
2093 (if (and file-exists ex-append)
2094 (insert-file-contents ex-file))
2095 (goto-char (point-max))
2096 (insert region)
2097 ;; ask user
2098 (viper-maybe-checkout (current-buffer))
2099 (setq selective-display nil)
2100 (save-buffer)
2101 (ex-write-info
2102 file-exists ex-file (point-min) (point-max))
2104 ;; this must be under unwind-protect so that
2105 ;; temp-buf will be deleted in case of an error
2106 (set-buffer temp-buf)
2107 (set-buffer-modified-p nil)
2108 (kill-buffer temp-buf)
2109 ;; buffer/region has been written, now take care of details
2110 (set-buffer orig-buf)))
2111 ;; set the right file modification time
2112 (if (and (buffer-file-name) writing-same-file)
2113 (set-visited-file-modtime))
2114 ;; prevent loss of data if saving part of the buffer in visited file
2115 (or writing-whole-file
2116 (not writing-same-file)
2117 (progn
2118 (sit-for 2)
2119 (message "Warning: you have saved only part of the buffer!")
2120 (set-buffer-modified-p t)))
2121 (if q-flag
2122 (if (< viper-expert-level 2)
2123 (save-buffers-kill-emacs)
2124 (kill-buffer (current-buffer))))
2128 (defun ex-write-info (exists file-name beg end)
2129 (message "`%s'%s %d lines, %d characters"
2130 (viper-abbreviate-file-name file-name)
2131 (if exists "" " [New file]")
2132 (count-lines beg (min (1+ end) (point-max)))
2133 (- end beg)))
2135 ;; Ex yank command
2136 (defun ex-yank ()
2137 (viper-default-ex-addresses)
2138 (viper-get-ex-buffer)
2139 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2140 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2141 (save-excursion
2142 (viper-enlarge-region beg end)
2143 (exchange-point-and-mark)
2144 (if (or ex-g-flag ex-g-variant)
2145 (error "Can't execute `yank' within `global'"))
2146 (if ex-count
2147 (progn
2148 (set-mark (point))
2149 (forward-line (1- ex-count)))
2150 (set-mark end))
2151 (viper-enlarge-region (point) (mark t))
2152 (if ex-flag (error "`yank': %s" viper-SpuriousText))
2153 (if ex-buffer
2154 (cond ((viper-valid-register ex-buffer '(Letter))
2155 (viper-append-to-register
2156 (downcase ex-buffer) (point) (mark t)))
2157 ((viper-valid-register ex-buffer)
2158 (copy-to-register ex-buffer (point) (mark t) nil))
2159 (t (error viper-InvalidRegister ex-buffer))))
2160 (copy-region-as-kill (point) (mark t)))))
2162 ;; Execute shell command
2163 (defun ex-command ()
2164 (let (command)
2165 (with-current-buffer (setq viper-ex-work-buf
2166 (get-buffer-create viper-ex-work-buf-name))
2167 (skip-chars-forward " \t")
2168 (setq command (buffer-substring (point) (point-max)))
2169 (end-of-line))
2170 ;; replace # and % with the previous/current file
2171 (setq command (ex-expand-filsyms command (current-buffer)))
2172 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
2173 (if viper-ex-last-shell-com
2174 (setq command
2175 (concat viper-ex-last-shell-com (substring command 1)))
2176 (error "No previous shell command")))
2177 (setq viper-ex-last-shell-com command)
2178 (if (null ex-addresses)
2179 (shell-command command)
2180 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2181 (if (null beg) (setq beg end))
2182 (save-excursion
2183 (goto-char beg)
2184 (set-mark end)
2185 (viper-enlarge-region (point) (mark t))
2186 (shell-command-on-region (point) (mark t) command t t))
2187 (goto-char beg)))))
2189 (defun ex-compile ()
2190 "Reads args from the command line, then runs make with the args.
2191 If no args are given, then it runs the last compile command.
2192 Type `mak ' (including the space) to run make with no args."
2193 (let (args)
2194 (with-current-buffer (setq viper-ex-work-buf
2195 (get-buffer-create viper-ex-work-buf-name))
2196 (setq args (buffer-substring (point) (point-max)))
2197 (end-of-line))
2198 ;; Remove the newline that may (will?) be at the end of the args
2199 (if (string= "\n" (substring args (1- (length args))))
2200 (setq args (substring args 0 (1- (length args)))))
2201 ;; Run last command if no args given, else construct a new command.
2202 (setq args
2203 (if (string= "" args)
2204 (if (boundp 'compile-command)
2205 compile-command
2206 ex-compile-command)
2207 (concat ex-compile-command " " args)))
2208 (compile args)
2211 ;; Print line number
2212 (defun ex-line-no ()
2213 (message "%d"
2214 (1+ (count-lines
2215 (point-min)
2216 (if (null ex-addresses) (point-max) (car ex-addresses))))))
2218 ;; Give information on the file visited by the current buffer
2219 (defun viper-info-on-file ()
2220 (interactive)
2221 (let ((pos1 (viper-line-pos 'start))
2222 (pos2 (viper-line-pos 'end))
2223 lines file info)
2224 (setq lines (count-lines (point-min) (viper-line-pos 'end))
2225 file (cond ((buffer-file-name)
2226 (concat (viper-abbreviate-file-name (buffer-file-name)) ":"))
2227 ((buffer-file-name (buffer-base-buffer))
2228 (concat (viper-abbreviate-file-name (buffer-file-name (buffer-base-buffer))) " (indirect buffer):"))
2229 (t (concat (buffer-name) " [Not visiting any file]:")))
2230 info (format "line=%d/%d pos=%d/%d col=%d %s"
2231 (if (= pos1 pos2)
2232 (1+ lines)
2233 lines)
2234 (count-lines (point-min) (point-max))
2235 (point) (1- (point-max))
2236 (1+ (current-column))
2237 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2238 (if (< (+ 1 (length info) (length file))
2239 (window-width (minibuffer-window)))
2240 (message "%s" (concat file " " info))
2241 (save-window-excursion
2242 (with-output-to-temp-buffer " *viper-info*"
2243 (princ (concat "\n" file "\n\n\t" info "\n\n")))
2244 (let ((inhibit-quit t))
2245 (viper-set-unread-command-events (viper-read-event)))
2246 (kill-buffer " *viper-info*")))
2250 ;; Without arguments displays info on file. With an arg, sets the visited file
2251 ;; name to that arg
2252 (defun ex-set-visited-file-name ()
2253 (viper-get-ex-file)
2254 (if (string= ex-file "")
2255 (viper-info-on-file)
2256 ;; If ex-file is a directory, use the file portion of the buffer
2257 ;; file name (like ex-write). Do this even if ex-file is a
2258 ;; non-existent directory, since set-visited-file-name signals an
2259 ;; error on this condition, too.
2260 (if (and (string= (file-name-nondirectory ex-file) "")
2261 buffer-file-name
2262 (not (file-directory-p buffer-file-name)))
2263 (setq ex-file (concat (file-name-as-directory ex-file)
2264 (file-name-nondirectory buffer-file-name))))
2265 (set-visited-file-name ex-file)))
2268 ;; display all variables set through :set
2269 (defun ex-show-vars ()
2270 (with-output-to-temp-buffer " *viper-info*"
2271 (princ (if viper-auto-indent
2272 "autoindent (local)\n" "noautoindent (local)\n"))
2273 (princ (if (default-value 'viper-auto-indent)
2274 "autoindent (global) \n" "noautoindent (global) \n"))
2275 (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n"))
2276 (princ (if viper-re-search "magic\n" "nomagic\n"))
2277 (princ (if buffer-read-only "readonly\n" "noreadonly\n"))
2278 (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n"))
2279 (princ (if viper-search-wrap-around "wrapscan\n" "nowrapscan\n"))
2280 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width))
2281 (princ (format "tabstop (local) \t= %S\n" tab-width))
2282 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width)))
2283 (princ (format "wrapmargin (local) \t= %S\n"
2284 (- (window-width) fill-column)))
2285 (princ (format "wrapmargin (global) \t= %S\n"
2286 (- (window-width) (default-value 'fill-column))))
2287 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name)
2288 explicit-shell-file-name
2289 'none)))
2292 (defun ex-print ()
2293 (viper-default-ex-addresses)
2294 (let ((end (car ex-addresses))
2295 (beg (car (cdr ex-addresses))))
2296 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2297 (save-excursion
2298 (viper-enlarge-region beg end)
2299 (if (or ex-g-flag ex-g-variant)
2300 ;; When executing a global command, collect output of each
2301 ;; print in viper-ex-print-buf.
2302 (progn
2303 (append-to-buffer viper-ex-print-buf (point) (mark t))
2304 ;; Is this the last mark for the global command?
2305 (unless (cdr ex-g-marks)
2306 (with-current-buffer viper-ex-print-buf
2307 (ex-print-display-lines (buffer-string))
2308 (erase-buffer))))
2309 (ex-print-display-lines (buffer-substring (point) (mark t)))))))
2311 (defun ex-print-display-lines (lines)
2312 (cond
2313 ;; String doesn't contain a newline.
2314 ((not (string-match "\n" lines))
2315 (message "%s" lines))
2316 ;; String contains only one newline at the end. Strip it off.
2317 ((= (string-match "\n" lines) (1- (length lines)))
2318 (message "%s" (substring lines 0 -1)))
2319 ;; String spans more than one line. Use a temporary buffer.
2321 (save-current-buffer
2322 (with-output-to-temp-buffer " *viper-info*"
2323 (princ lines))))))
2329 ;;; viper-ex.el ends here