Document reserved keys
[emacs.git] / lisp / emulation / viper-ex.el
blob347e66f8ff1bde17146b7c42cb047a6a1ade87db
1 ;;; viper-ex.el --- functions implementing the Ex commands for Viper
3 ;; Copyright (C) 1994-1998, 2000-2018 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 <https://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 (char (following-char)))
406 (cond ((= char ?#)
407 (setq ex-token-type 'command)
408 (setq ex-token (char-to-string char))
409 (forward-char 1))
410 ((looking-at "[a-z]") (viper-get-ex-com-subr))
411 ((= char ?.)
412 (forward-char 1)
413 (setq ex-token-type 'dot))
414 ((looking-at "[0-9]")
415 (set-mark (point))
416 (re-search-forward "[0-9]*")
417 (setq ex-token-type
418 (cond ((eq ex-token-type 'plus) 'add-number)
419 ((eq ex-token-type 'minus) 'sub-number)
420 (t 'abs-number)))
421 (setq ex-token
422 (string-to-number (buffer-substring (point) (mark t)))))
423 ((= char ?$)
424 (forward-char 1)
425 (setq ex-token-type 'end))
426 ((= char ?%)
427 (forward-char 1)
428 (setq ex-token-type 'whole))
429 ((= char ?+)
430 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
431 (forward-char 1)
432 (insert "1")
433 (backward-char 1)
434 (setq ex-token-type 'plus))
435 ((looking-at "+[0-9]")
436 (forward-char 1)
437 (setq ex-token-type 'plus))
439 (error viper-BadAddress))))
440 ((= char ?-)
441 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
442 (forward-char 1)
443 (insert "1")
444 (backward-char 1)
445 (setq ex-token-type 'minus))
446 ((looking-at "-[0-9]")
447 (forward-char 1)
448 (setq ex-token-type 'minus))
450 (error viper-BadAddress))))
451 ((= char ?/)
452 (forward-char 1)
453 (set-mark (point))
454 (let ((cont t))
455 (while (and (not (eolp)) cont)
456 ;;(re-search-forward "[^/]*/")
457 (re-search-forward "[^/]*\\(/\\|\n\\)")
458 (if (not (looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"
459 (line-beginning-position 0)))
460 (setq cont nil))))
461 (backward-char 1)
462 (setq ex-token (buffer-substring (point) (mark t)))
463 (when (= (following-char) ?/) (forward-char 1))
464 (setq ex-token-type 'search-forward))
465 ((= char ??)
466 (forward-char 1)
467 (set-mark (point))
468 (let ((cont t))
469 (while (and (not (eolp)) cont)
470 ;;(re-search-forward "[^\\?]*\\?")
471 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
472 (if (not (looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"
473 (line-beginning-position 0)))
474 (setq cont nil))
475 (backward-char 1)
476 (when (/= (following-char) ?\n) (forward-char 1))))
477 (setq ex-token-type 'search-backward)
478 (setq ex-token (buffer-substring (1- (point)) (mark t))))
479 ((= char ?,)
480 (forward-char 1)
481 (setq ex-token-type 'comma))
482 ((= char ?\;)
483 (forward-char 1)
484 (setq ex-token-type 'semi-colon))
485 ((looking-at "[!=><&~]")
486 (setq ex-token-type 'command)
487 (setq ex-token (char-to-string char))
488 (forward-char 1))
489 ((= char ?\')
490 (setq ex-token-type 'goto-mark)
491 (forward-char 1)
492 (cond ((= (following-char) ?\') (setq ex-token nil))
493 ((looking-at "[a-z]") (setq ex-token (following-char)))
494 (t (error "%s" "Marks are ' and a-z")))
495 (forward-char 1))
496 ((= char ?\n)
497 (setq ex-token-type 'end-mark)
498 (setq ex-token "goto"))
500 (error viper-BadExCommand))))))
502 ;; Reads Ex command. Tries to determine if it has to exit because command
503 ;; is complete or invalid. If not, keeps reading command.
504 (defun ex-cmd-read-exit ()
505 (interactive)
506 (setq viper-incomplete-ex-cmd t)
507 (let ((quit-regex1 (concat
508 "\\(" "set[ \t]*"
509 "\\|" "edit[ \t]*"
510 "\\|" "[nN]ext[ \t]*"
511 "\\|" "unm[ \t]*"
512 "\\|" "^[ \t]*rep"
513 "\\)"))
514 (quit-regex2 (concat
515 "[a-zA-Z][ \t]*"
516 "\\(" "!" "\\|" ">>"
517 "\\|" "\\+[0-9]+"
518 "\\)"
519 "*[ \t]*$"))
520 (stay-regex (concat
521 "\\(" "^[ \t]*$"
522 "\\|" "[?/].*"
523 "\\|" "[ktgjmsz][ \t]*$"
524 "\\|" "^[ \t]*ab.*"
525 "\\|" "tr[ansfer \t]*"
526 "\\|" "sr[ \t]*"
527 "\\|" "mo.*"
528 "\\|" "^[ \t]*k?ma[^p]*"
529 "\\|" "^[ \t]*fi.*"
530 "\\|" "v?gl.*"
531 "\\|" "[vg][ \t]*$"
532 "\\|" "jo.*"
533 "\\|" "^[ \t]*ta.*"
534 "\\|" "^[ \t]*una.*"
535 ;; don't jump up in :s command
536 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*su.*"
537 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*s[^a-z].*"
538 "\\|" "['`][a-z][ \t]*"
539 ;; r! assumes that the next one is a shell command
540 "\\|" "\\(r\\|re\\|rea\\|read\\)[ \t]*!"
541 ;; w ! assumes that the next one is a shell command
542 "\\|" "\\(w\\|wr\\|wri\\|writ.?\\)[ \t]+!"
543 "\\|" "![ \t]*[a-zA-Z].*"
544 "\\)"
545 "!*")))
547 (save-window-excursion ;; put cursor at the end of the Ex working buffer
548 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
549 (set-buffer viper-ex-work-buf)
550 (goto-char (point-max)))
551 (cond ((looking-back quit-regex1) (exit-minibuffer))
552 ((looking-back stay-regex) (insert " "))
553 ((looking-back quit-regex2) (exit-minibuffer))
554 (t (insert " ")))))
556 (declare-function viper-tmp-insert-at-eob "viper-cmd" (msg))
558 ;; complete Ex command
559 (defun ex-cmd-complete ()
560 (interactive)
561 (let (save-pos dist compl-list string-to-complete completion-result)
563 (save-excursion
564 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
565 save-pos (point)))
567 (if (or (= dist 0)
568 (looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)"
569 (line-beginning-position))
570 (looking-back
571 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+"
572 (line-beginning-position)))
573 ;; Preceding characters are not the ones allowed in an Ex command
574 ;; or we have typed past command name.
575 ;; Note: we didn't do parsing, so there can be surprises.
576 (if (or (looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*"
577 (line-beginning-position))
578 (looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)"
579 (line-beginning-position))
580 (looking-at "[^ \t\n\C-m]"))
582 (with-output-to-temp-buffer "*Completions*"
583 (display-completion-list
584 (viper-alist-to-list ex-token-alist))))
585 ;; Preceding chars may be part of a command name
586 (setq string-to-complete (buffer-substring save-pos (point)))
587 (setq completion-result
588 (try-completion string-to-complete ex-token-alist))
590 (cond ((eq completion-result t) ; exact match--do nothing
591 (viper-tmp-insert-at-eob " (Sole completion)"))
592 ((eq completion-result nil)
593 (viper-tmp-insert-at-eob " (No match)"))
594 (t ;; partial completion
595 (goto-char save-pos)
596 (delete-region (point) (point-max))
597 (insert completion-result)
598 (let (case-fold-search)
599 (setq compl-list
600 (viper-filter-alist (concat "^" completion-result)
601 ex-token-alist)))
602 (if (> (length compl-list) 1)
603 (with-output-to-temp-buffer "*Completions*"
604 (display-completion-list
605 (viper-alist-to-list (reverse compl-list)))))))
609 (declare-function viper-enlarge-region "viper-cmd" (beg end))
610 (declare-function viper-read-string-with-history "viper-cmd"
611 (prompt &optional viper-initial history-var
612 default keymap init-message))
614 ;; Read Ex commands
615 ;; ARG is a prefix argument. If given, the ex command runs on the region
616 ;;(without the user having to specify the address :a,b
617 ;; STRING is the command to execute. If nil, then Viper asks you to enter the
618 ;; command.
619 (defun viper-ex (arg &optional string)
620 (interactive "P")
621 (or string
622 (setq ex-g-flag nil
623 ex-g-variant nil))
624 (let* ((map (copy-keymap minibuffer-local-map))
625 (address nil)
626 (cont t)
627 (dot (point))
628 reg-beg-line reg-end-line
629 reg-beg reg-end
630 initial-str
631 prev-token-type com-str)
632 (viper-add-keymap viper-ex-cmd-map map)
634 (if arg
635 (progn
636 (viper-enlarge-region (mark t) (point))
637 (if (> (point) (mark t))
638 (setq reg-beg (mark t)
639 reg-end (point))
640 (setq reg-end (mark t)
641 reg-beg (point)))
642 (save-excursion
643 (goto-char reg-beg)
644 (setq reg-beg-line (1+ (count-lines (point-min) (point)))
645 reg-end-line
646 (+ reg-beg-line (count-lines reg-beg reg-end) -1)))))
647 (if reg-beg-line
648 (setq initial-str (format "%d,%d" reg-beg-line reg-end-line)))
650 (setq com-str
651 (if string
652 (concat initial-str string)
653 (viper-read-string-with-history
655 initial-str
656 'viper-ex-history
657 ;; no default when working on region
658 (if initial-str
660 (car viper-ex-history))
662 (if initial-str
663 " [Type command to execute on current region]"))))
664 (save-window-excursion
665 ;; just a precaution
666 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
667 (set-buffer viper-ex-work-buf)
668 (delete-region (point-min) (point-max))
669 (insert com-str "\n")
670 (goto-char (point-min)))
671 (setq ex-token-type nil
672 ex-addresses nil)
673 (while cont
674 (viper-get-ex-token)
675 (cond ((memq ex-token-type '(command end-mark))
676 (if address (setq ex-addresses (cons address ex-addresses)))
677 (viper-deactivate-mark)
678 (let ((cmd (ex-cmd-assoc ex-token ex-token-alist)))
679 (if (null cmd)
680 (error "`%s': %s" ex-token viper-BadExCommand))
681 (ex-cmd-execute cmd)
682 (if (or (ex-cmd-is-mashed-with-args cmd)
683 (ex-cmd-is-one-letter cmd))
684 (setq cont nil)
685 (save-excursion
686 (save-window-excursion
687 (setq viper-ex-work-buf
688 (get-buffer-create viper-ex-work-buf-name))
689 (set-buffer viper-ex-work-buf)
690 (skip-chars-forward " \t")
691 (cond ((= (following-char) ?|)
692 (forward-char 1))
693 ((= (following-char) ?\n)
694 (setq cont nil))
695 (t (error
696 "`%s': %s" ex-token viper-SpuriousText)))
699 ((eq ex-token-type 'non-command)
700 (error "`%s': %s" ex-token viper-BadExCommand))
701 ((eq ex-token-type 'whole)
702 (setq address nil)
703 (setq ex-addresses
704 (if ex-addresses
705 (cons (point-max) ex-addresses)
706 (cons (point-max) (cons (point-min) ex-addresses)))))
707 ((eq ex-token-type 'comma)
708 (if (eq prev-token-type 'whole)
709 (setq address (point-min)))
710 (setq ex-addresses
711 (cons (if (null address) (point) address) ex-addresses)))
712 ((eq ex-token-type 'semi-colon)
713 (if (eq prev-token-type 'whole)
714 (setq address (point-min)))
715 (if address (setq dot address))
716 (setq ex-addresses
717 (cons (if (null address) (point) address) ex-addresses)))
718 (t (let ((ans (viper-get-ex-address-subr address dot)))
719 (if ans (setq address ans)))))
720 (setq prev-token-type ex-token-type))))
723 ;; Get a regular expression and set `ex-variant', if found
724 ;; Viper doesn't parse the substitution or search patterns.
725 ;; In particular, it doesn't expand ~ into the last substitution.
726 (defun viper-get-ex-pat ()
727 (save-window-excursion
728 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
729 (set-buffer viper-ex-work-buf)
730 (skip-chars-forward " \t")
731 (if (looking-at "!")
732 ;; this is probably a variant command r!
733 (progn
734 (setq ex-g-variant (not ex-g-variant)
735 ex-g-flag (not ex-g-flag))
736 (forward-char 1)
737 (skip-chars-forward " \t")))
738 (let ((c (following-char)))
739 (cond ((string-match "[0-9A-Za-z]" (format "%c" c))
740 (error
741 "Global regexp must be inside matching non-alphanumeric chars"))
742 ((= c ??) (error "`?' is not an allowed pattern delimiter here")))
743 (if (looking-at "[^\\\\\n]")
744 (progn
745 (forward-char 1)
746 (set-mark (point))
747 (let ((cont t))
748 ;; the use of eobp instead of eolp permits the use of newlines in
749 ;; pat2 in s/pat1/pat2/
750 (while (and (not (eobp)) cont)
751 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
752 (if (member ex-token '("global" "vglobal"))
753 (error "Missing closing delimiter for global regexp")
754 (goto-char (point-max))))
755 (if (not (looking-back
756 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)
757 (line-beginning-position 0)))
758 (setq cont nil)
759 ;; we are at an escaped delimiter: unescape it and continue
760 (delete-char -2)
761 (insert c)
762 (if (eolp)
763 ;; if at eol, exit loop and go to next line
764 ;; later, delim will be inserted at the end
765 (progn
766 (setq cont nil)
767 (forward-char))))
769 (setq ex-token
770 (if (= (mark t) (point)) ""
771 (buffer-substring (1- (point)) (mark t))))
772 (backward-char 1)
773 ;; if the user didn't insert the final pattern delimiter, we're
774 ;; at newline now. In this case, insert the initial delimiter
775 ;; specified in variable c
776 (if (eolp)
777 (progn
778 (insert c)
779 (backward-char 1)))
781 (setq ex-token nil))
782 c)))
784 ;; Get an Ex option g or c
785 (defun viper-get-ex-opt-gc (c)
786 (save-window-excursion
787 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
788 (set-buffer viper-ex-work-buf)
789 (if (looking-at (format "%c" c)) (forward-char 1))
790 (skip-chars-forward " \t")
791 (cond ((looking-at "g")
792 (setq ex-token "g")
793 (forward-char 1)
795 ((looking-at "c")
796 (setq ex-token "c")
797 (forward-char 1)
799 (t nil))))
801 ;; Compute default addresses. WHOLE-FLAG means use the whole buffer
802 (defun viper-default-ex-addresses (&optional whole-flag)
803 (cond ((null ex-addresses)
804 (setq ex-addresses
805 (if whole-flag
806 (list (point-max) (point-min))
807 (list (point) (point)))))
808 ((null (cdr ex-addresses))
809 (setq ex-addresses
810 (cons (car ex-addresses) ex-addresses)))))
812 ;; Get an ex-address as a marker and set ex-flag if a flag is found
813 (defun viper-get-ex-address ()
814 (let ((address (point-marker))
815 (cont t))
816 (setq ex-token "")
817 (setq ex-flag nil)
818 (while cont
819 (viper-get-ex-token)
820 (cond ((eq ex-token-type 'command)
821 (if (member ex-token '("print" "list" "#"))
822 (progn
823 (setq ex-flag t
824 cont nil))
825 (error "Address expected in this Ex command")))
826 ((eq ex-token-type 'end-mark)
827 (setq cont nil))
828 ((eq ex-token-type 'whole)
829 (error "Trailing address expected"))
830 ((eq ex-token-type 'comma)
831 (error "`%s': %s" ex-token viper-SpuriousText))
832 (t (let ((ans (viper-get-ex-address-subr address (point-marker))))
833 (if ans (setq address ans))))))
834 address))
836 (declare-function viper-register-to-point "viper-cmd"
837 (char &optional enforce-buffer))
839 ;; Returns an address as a point
840 (defun viper-get-ex-address-subr (old-address dot)
841 (let ((address nil))
842 (if (null old-address) (setq old-address dot))
843 (cond ((eq ex-token-type 'dot)
844 (setq address dot))
845 ((eq ex-token-type 'add-number)
846 (save-excursion
847 (goto-char old-address)
848 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
849 (setq address (point-marker))))
850 ((eq ex-token-type 'sub-number)
851 (save-excursion
852 (goto-char old-address)
853 (forward-line (- ex-token))
854 (setq address (point-marker))))
855 ((eq ex-token-type 'abs-number)
856 (save-excursion
857 (goto-char (point-min))
858 (if (= ex-token 0) (setq address 0)
859 (forward-line (1- ex-token))
860 (setq address (point-marker)))))
861 ((eq ex-token-type 'end)
862 (save-excursion
863 (goto-char (1- (point-max)))
864 (setq address (point-marker))))
865 ((eq ex-token-type 'plus) t) ; do nothing
866 ((eq ex-token-type 'minus) t) ; do nothing
867 ((eq ex-token-type 'search-forward)
868 (save-excursion
869 (ex-search-address t)
870 (setq address (point-marker))))
871 ((eq ex-token-type 'search-backward)
872 (save-excursion
873 (ex-search-address nil)
874 (setq address (point-marker))))
875 ((eq ex-token-type 'goto-mark)
876 (save-excursion
877 (if (null ex-token)
878 (exchange-point-and-mark)
879 (goto-char
880 (viper-register-to-point
881 (viper-int-to-char (1+ (- ex-token ?a))) 'enforce-buffer)))
882 (setq address (point-marker)))))
883 address))
886 ;; Search pattern and set address
887 ;; Doesn't wrap around. Should it?
888 (defun ex-search-address (forward)
889 (if (string= ex-token "")
890 (if (null viper-s-string)
891 (error viper-NoPrevSearch)
892 (setq ex-token viper-s-string))
893 (setq viper-s-string ex-token))
894 (if forward
895 (progn
896 (forward-line 1)
897 (re-search-forward ex-token))
898 (forward-line -1)
899 (re-search-backward ex-token)))
901 ;; Get a buffer name and set `ex-count' and `ex-flag' if found
902 (defun viper-get-ex-buffer ()
903 (setq ex-buffer nil)
904 (setq ex-count nil)
905 (setq ex-flag nil)
906 (save-window-excursion
907 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
908 (set-buffer viper-ex-work-buf)
909 (skip-chars-forward " \t")
910 (if (looking-at "[a-zA-Z]")
911 (progn
912 (setq ex-buffer (following-char))
913 (forward-char 1)
914 (skip-chars-forward " \t")))
915 (if (looking-at "[0-9]")
916 (progn
917 (set-mark (point))
918 (re-search-forward "[0-9][0-9]*")
919 (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
920 (skip-chars-forward " \t")))
921 (if (looking-at "[pl#]")
922 (progn
923 (setq ex-flag t)
924 (forward-char 1)))
925 (if (not (looking-at "[\n|]"))
926 (error "`%s': %s" ex-token viper-SpuriousText))))
928 (defun viper-get-ex-count ()
929 (setq ex-variant nil
930 ex-count nil
931 ex-flag nil)
932 (save-window-excursion
933 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
934 (set-buffer viper-ex-work-buf)
935 (skip-chars-forward " \t")
936 (if (looking-at "!")
937 (progn
938 (setq ex-variant t)
939 (forward-char 1)))
940 (skip-chars-forward " \t")
941 (if (looking-at "[0-9]")
942 (progn
943 (set-mark (point))
944 (re-search-forward "[0-9][0-9]*")
945 (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
946 (skip-chars-forward " \t")))
947 (if (looking-at "[pl#]")
948 (progn
949 (setq ex-flag t)
950 (forward-char 1)))
951 (if (not (looking-at "[\n|]"))
952 (error "`%s': %s"
953 (buffer-substring
954 (point-min) (1- (point-max))) viper-BadExCommand))))
956 ;; Expand \% and \# in ex command
957 (defun ex-expand-filsyms (cmd buf)
958 (let (cf pf ret)
959 (with-current-buffer buf
960 (setq cf buffer-file-name)
961 (setq pf (ex-next nil t))) ; this finds alternative file name
962 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
963 (error "No current file to substitute for `%%'"))
964 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
965 (error "No alternate file to substitute for `#'"))
966 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
967 (erase-buffer)
968 (insert cmd)
969 (goto-char (point-min))
970 (while (re-search-forward "%\\|#" nil t)
971 (let ((data (match-data))
972 (char (buffer-substring (match-beginning 0) (match-end 0))))
973 (if (looking-back "\\\\." (- (point) 2))
974 (replace-match char)
975 (store-match-data data)
976 (if (string= char "%")
977 (replace-match cf)
978 (replace-match pf)))))
979 (end-of-line)
980 (setq ret (buffer-substring (point-min) (point)))
981 (message "%s" ret))
982 ret))
984 ;; Get a file name and set `ex-variant', `ex-append' and `ex-offset' if found
985 ;; If it is r!, then get the command name and whatever args
986 (defun viper-get-ex-file ()
987 (let (prompt)
988 (setq ex-file nil
989 ex-variant nil
990 ex-append nil
991 ex-offset nil
992 ex-cmdfile nil
993 ex-cmdfile-args "")
994 (save-excursion
995 (with-current-buffer (setq viper-ex-work-buf
996 (get-buffer-create viper-ex-work-buf-name))
997 (skip-chars-forward " \t")
998 (when (= (following-char) ?!)
999 (if (and (not (memq (preceding-char) '(?\s ?\t)))
1000 ;; read doesn't have a corresponding :r! form, so ! is
1001 ;; immediately interpreted as a shell command.
1002 (not (string= ex-token "read")))
1003 (progn
1004 (setq ex-variant t)
1005 (forward-char 1)
1006 (skip-chars-forward " \t"))
1007 (setq ex-cmdfile t)
1008 (forward-char 1)
1009 (skip-chars-forward " \t")))
1010 (when (looking-at ">>")
1011 (setq ex-append t
1012 ex-variant t)
1013 (forward-char 2)
1014 (skip-chars-forward " \t"))
1015 (when (= (following-char) ?+)
1016 (forward-char 1)
1017 (set-mark (point))
1018 (re-search-forward "[ \t\n]")
1019 (backward-char 1)
1020 (setq ex-offset (buffer-substring (point) (mark t)))
1021 (forward-char 1)
1022 (skip-chars-forward " \t"))
1023 ;; this takes care of :r, :w, etc., when they get file names
1024 ;; from the history list
1025 (if (member ex-token '("read" "write" "edit" "visual" "next"))
1026 (progn
1027 (setq ex-file (buffer-substring (point) (1- (point-max))))
1028 (setq ex-file
1029 ;; For :e, match multiple non-white strings separated
1030 ;; by white. For others, find the first non-white string
1031 (if (string-match
1032 (if (string= ex-token "edit")
1033 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
1034 "[^ \t\n]+")
1035 ex-file)
1036 (progn
1037 ;; if file name comes from history, don't leave
1038 ;; minibuffer when the user types space
1039 (setq viper-incomplete-ex-cmd nil)
1040 (setq ex-cmdfile-args
1041 (substring ex-file (match-end 0) nil))
1042 ;; this must be the last clause in this progn
1043 (substring ex-file (match-beginning 0) (match-end 0))
1045 ""))
1046 ;; this leaves only the command name in the work area
1047 ;; file names are gone
1048 (delete-region (point) (1- (point-max)))
1050 (goto-char (point-max))
1051 (skip-chars-backward " \t\n")
1052 (setq prompt (buffer-substring (point-min) (point)))
1055 (setq viper-last-ex-prompt prompt)
1057 ;; If we just finished reading command, redisplay prompt
1058 (if viper-incomplete-ex-cmd
1059 (setq ex-file (viper-ex-read-file-name (format ":%s " prompt)))
1060 ;; file was typed in-line
1061 (setq ex-file (or ex-file "")))
1065 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
1066 ;; file names, arranges to re-enter the minibuffer.
1067 (defun viper-complete-filename-or-exit ()
1068 (interactive)
1069 (setq viper-keep-reading-filename t)
1070 ;; don't exit if directory---ex-commands don't
1071 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
1072 ;; apparently the argument to an Ex command is
1073 ;; supposed to be a shell command
1074 ((looking-back "^[ \t]*!.*" (line-beginning-position))
1075 (setq ex-cmdfile t)
1076 (insert " "))
1078 (setq ex-cmdfile nil)
1079 (minibuffer-complete-word))))
1081 (defun viper-handle-! ()
1082 (interactive)
1083 (if (and (string=
1084 (buffer-string) (viper-abbreviate-file-name default-directory))
1085 (member ex-token '("read" "write")))
1086 (erase-buffer))
1087 (insert "!"))
1089 (defun ex-cmd-accepts-multiple-files-p (token)
1090 (member token '("edit" "next" "Next")))
1092 ;; Read file name from the minibuffer in an ex command.
1093 ;; If user doesn't enter anything, then "" is returned, i.e., the
1094 ;; prompt-directory is not returned.
1095 (defun viper-ex-read-file-name (prompt)
1096 (let* ((str "")
1097 (minibuffer-local-completion-map
1098 (copy-keymap minibuffer-local-completion-map))
1099 beg end cont val)
1101 (viper-add-keymap ex-read-filename-map
1102 (if (featurep 'emacs)
1103 minibuffer-local-completion-map
1104 read-file-name-map))
1106 (setq cont (setq viper-keep-reading-filename t))
1107 (while cont
1108 (setq viper-keep-reading-filename nil
1109 val (read-file-name (concat prompt str) nil default-directory))
1110 (setq val (expand-file-name val))
1111 (if (and (string-match " " val)
1112 (ex-cmd-accepts-multiple-files-p ex-token))
1113 (setq val (concat "\"" val "\"")))
1114 (setq str (concat str (if (equal val "") "" " ")
1115 val (if (equal val "") "" " ")))
1117 ;; Only edit, next, and Next commands accept multiple files.
1118 ;; viper-keep-reading-filename is set in the anonymous function that is
1119 ;; bound to " " in ex-read-filename-map.
1120 (setq cont (and viper-keep-reading-filename
1121 (ex-cmd-accepts-multiple-files-p ex-token)))
1124 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
1125 end (string-match "[ \t]*$" str)) ; delete trailing blanks
1126 (if (member ex-token '("read" "write"))
1127 (if (string-match "[\t ]*!" str)
1128 ;; this is actually a shell command
1129 (progn
1130 (setq ex-cmdfile t)
1131 (setq beg (1+ beg))
1132 (setq viper-last-ex-prompt
1133 (concat viper-last-ex-prompt " !")))))
1134 (substring str (or beg 0) end)))
1137 (defun viper-undisplayed-files ()
1138 (mapcar
1139 (lambda (b)
1140 (if (null (get-buffer-window b))
1141 (let ((f (buffer-file-name b)))
1142 (if f f
1143 (if ex-cycle-through-non-files
1144 (let ((s (buffer-name b)))
1145 (if (string= " " (substring s 0 1))
1148 nil)))
1149 nil))
1150 (buffer-list)))
1153 (defun ex-args ()
1154 (let ((l (viper-undisplayed-files))
1155 (args "")
1156 (file-count 1))
1157 (while (not (null l))
1158 (if (car l)
1159 (setq args (format "%s %d) %s\n" args file-count (car l))
1160 file-count (1+ file-count)))
1161 (setq l (cdr l)))
1162 (if (string= args "")
1163 (message "All files are already displayed")
1164 (save-excursion
1165 (save-window-excursion
1166 (with-output-to-temp-buffer " *viper-info*"
1167 (princ "\n\nThese files are not displayed in any window.\n")
1168 (princ "\n=============\n")
1169 (princ args)
1170 (princ "\n=============\n")
1171 (princ "\nThe numbers can be given as counts to :next. ")
1172 (princ "\n\nPress any key to continue...\n\n"))
1173 (viper-read-event))))))
1175 ;; Ex cd command. Default directory of this buffer changes
1176 (defun ex-cd ()
1177 (viper-get-ex-file)
1178 (if (string= ex-file "")
1179 (setq ex-file "~"))
1180 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
1182 ;; Ex copy and move command. DEL-FLAG means delete
1183 (defun ex-copy (del-flag)
1184 (viper-default-ex-addresses)
1185 (let ((address (viper-get-ex-address))
1186 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1187 (goto-char end)
1188 (save-excursion
1189 (push-mark beg t)
1190 (viper-enlarge-region (mark t) (point))
1191 (if del-flag
1192 (kill-region (point) (mark t))
1193 (copy-region-as-kill (point) (mark t)))
1194 (if ex-flag
1195 (progn
1196 (with-output-to-temp-buffer " *copy text*"
1197 (princ
1198 (if (or del-flag ex-g-flag ex-g-variant)
1199 (current-kill 0)
1200 (buffer-substring (point) (mark t)))))
1201 (condition-case nil
1202 (progn
1203 (read-string "[Hit return to confirm] ")
1204 (save-excursion (kill-buffer " *copy text*")))
1205 (quit (save-excursion (kill-buffer " *copy text*"))
1206 (signal 'quit nil))))))
1207 (if (= address 0)
1208 (goto-char (point-min))
1209 (goto-char address)
1210 (forward-line 1))
1211 (insert (current-kill 0))))
1213 (declare-function viper-append-to-register "viper-cmd" (reg start end))
1215 ;; Ex delete command
1216 (defun ex-delete ()
1217 (viper-default-ex-addresses)
1218 (viper-get-ex-buffer)
1219 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1220 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1221 (save-excursion
1222 (viper-enlarge-region beg end)
1223 (exchange-point-and-mark)
1224 (if ex-count
1225 (progn
1226 (set-mark (point))
1227 (forward-line (1- ex-count)))
1228 (set-mark end))
1229 (viper-enlarge-region (point) (mark t))
1230 (if ex-flag
1231 ;; show text to be deleted and ask for confirmation
1232 (progn
1233 (with-output-to-temp-buffer " *delete text*"
1234 (princ (buffer-substring (point) (mark t))))
1235 (condition-case nil
1236 (read-string "[Hit return to confirm] ")
1237 (quit
1238 (save-excursion (kill-buffer " *delete text*"))
1239 (error "Viper bell")))
1240 (save-excursion (kill-buffer " *delete text*")))
1241 (if ex-buffer
1242 (cond ((viper-valid-register ex-buffer '(Letter))
1243 (viper-append-to-register
1244 (downcase ex-buffer) (point) (mark t)))
1245 ((viper-valid-register ex-buffer)
1246 (copy-to-register ex-buffer (point) (mark t) nil))
1247 (t (error viper-InvalidRegister ex-buffer))))
1248 (kill-region (point) (mark t))))))
1251 (declare-function viper-change-state-to-vi "viper-cmd" (&rest _))
1253 ;; Ex edit command
1254 ;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1255 ;; asked if current buffer should really be discarded.
1256 ;; This command can take multiple file names. It replaces the current buffer
1257 ;; with the first file in its argument list
1258 (defun ex-edit (&optional file)
1259 (if (not file)
1260 (viper-get-ex-file))
1261 (cond ((and (string= ex-file "") buffer-file-name)
1262 (setq ex-file (viper-abbreviate-file-name (buffer-file-name))))
1263 ((string= ex-file "")
1264 (error viper-NoFileSpecified)))
1266 (let (msg do-edit)
1267 (if buffer-file-name
1268 (cond ((buffer-modified-p)
1269 (setq msg
1270 (format "Buffer %s is modified. Discard changes? "
1271 (buffer-name))
1272 do-edit t))
1273 ((not (verify-visited-file-modtime (current-buffer)))
1274 (setq msg
1275 (format "File %s changed on disk. Reread from disk? "
1276 buffer-file-name)
1277 do-edit t))
1278 (t (setq do-edit nil))))
1280 (if do-edit
1281 (if (yes-or-no-p msg)
1282 (progn
1283 (set-buffer-modified-p nil)
1284 (kill-buffer (current-buffer)))
1285 (message "Buffer %s was left intact" (buffer-name))))
1286 ) ; let
1288 (if (null (setq file (get-file-buffer ex-file)))
1289 (progn
1290 ;; this also does shell-style globbing
1291 (ex-find-file
1292 ;; replace # and % with the previous/current file
1293 (ex-expand-filsyms ex-file (current-buffer)))
1294 (or (eq major-mode 'dired-mode)
1295 (viper-change-state-to-vi))
1296 (goto-char (point-min)))
1297 (switch-to-buffer file))
1298 (if ex-offset
1299 (progn
1300 (with-current-buffer (setq viper-ex-work-buf
1301 (get-buffer-create viper-ex-work-buf-name))
1302 (delete-region (point-min) (point-max))
1303 (insert ex-offset "\n")
1304 (goto-char (point-min)))
1305 (goto-char (viper-get-ex-address))
1306 (beginning-of-line)))
1307 (ex-fixup-history viper-last-ex-prompt ex-file))
1309 ;; Find-file FILESPEC if it appears to specify a single file.
1310 ;; Otherwise, assume that FILESPEC is a wildcard.
1311 ;; In this case, split it into substrings separated by newlines.
1312 ;; Each line is assumed to be a file name.
1313 (defun ex-find-file (filespec)
1314 (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]"))
1315 (cond ((file-exists-p filespec) (find-file filespec))
1316 ((string-match nonstandard-filename-chars filespec)
1317 (mapcar 'find-file (funcall viper-glob-function filespec)))
1318 (t (find-file filespec)))
1322 (declare-function viper-backward-char-carefully "viper-cmd" (&optional arg))
1324 ;; Ex global command
1325 ;; This is executed in response to:
1326 ;; :global "pattern" ex-command
1327 ;; :vglobal "pattern" ex-command
1328 ;; :global executes ex-command on all lines matching <pattern>
1329 ;; :vglobal executes ex-command on all lines that don't match <pattern>
1331 ;; With VARIANT nil, this functions executes :global
1332 ;; With VARIANT t, executes :vglobal
1333 (defun ex-global (variant)
1334 (let ((gcommand ex-token))
1335 (if (or ex-g-flag ex-g-variant)
1336 (error "`%s' within `global' is not allowed" gcommand)
1337 (if variant
1338 (setq ex-g-flag nil
1339 ex-g-variant t)
1340 (setq ex-g-flag t
1341 ex-g-variant nil)))
1342 (viper-get-ex-pat)
1343 (if (null ex-token)
1344 (error "`%s': Missing regular expression" gcommand)))
1346 (if (string= ex-token "")
1347 (if (null viper-s-string)
1348 (error viper-NoPrevSearch)
1349 (setq ex-g-pat viper-s-string))
1350 (setq ex-g-pat ex-token
1351 viper-s-string ex-token))
1352 (if (null ex-addresses)
1353 (setq ex-addresses (list (point-max) (point-min)))
1354 (viper-default-ex-addresses))
1355 (setq ex-g-marks nil)
1356 (let ((mark-count 0)
1357 (end (car ex-addresses))
1358 (beg (car (cdr ex-addresses)))
1359 com-str)
1360 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1361 (save-excursion
1362 (viper-enlarge-region beg end)
1363 (exchange-point-and-mark)
1364 (let ((cont t) (limit (point-marker)))
1365 (exchange-point-and-mark)
1366 ;; skip the last line if empty
1367 (beginning-of-line)
1368 (if (eobp) (viper-backward-char-carefully))
1369 (while (and cont (not (bobp)) (>= (point) limit))
1370 (beginning-of-line)
1371 (set-mark (point))
1372 (end-of-line)
1373 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1374 (if (or (and ex-g-flag found)
1375 (and ex-g-variant (not found)))
1376 (progn
1377 (end-of-line)
1378 (setq mark-count (1+ mark-count))
1379 (setq ex-g-marks (cons (point-marker) ex-g-marks)))))
1380 (beginning-of-line)
1381 (if (bobp) (setq cont nil)
1382 (forward-line -1)
1383 (end-of-line)))))
1384 (with-current-buffer (setq viper-ex-work-buf
1385 (get-buffer-create viper-ex-work-buf-name))
1386 ;; com-str is the command string, i.e., g/pattern/ or v/pattern'
1387 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1388 (while ex-g-marks
1389 (goto-char (car ex-g-marks))
1390 (viper-ex nil com-str)
1391 (setq mark-count (1- mark-count))
1392 (setq ex-g-marks (cdr ex-g-marks)))))
1394 ;; Ex goto command
1395 (defun ex-goto ()
1396 (if (null ex-addresses)
1397 (setq ex-addresses (cons (point) nil)))
1398 (push-mark (point) t)
1399 (goto-char (car ex-addresses))
1400 (beginning-of-line)
1403 ;; Ex line commands. COM is join, shift-right or shift-left
1404 (defun ex-line (com)
1405 (viper-default-ex-addresses)
1406 (viper-get-ex-count)
1407 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1408 (if (> beg end) (error viper-FirstAddrExceedsSecond))
1409 (save-excursion
1410 (viper-enlarge-region beg end)
1411 (exchange-point-and-mark)
1412 (if ex-count
1413 (progn
1414 (set-mark (point))
1415 (forward-line ex-count)))
1416 (if ex-flag
1417 ;; show text to be joined and ask for confirmation
1418 (progn
1419 (with-output-to-temp-buffer " *join text*"
1420 (princ (buffer-substring (point) (mark t))))
1421 (condition-case nil
1422 (progn
1423 (read-string "[Hit return to confirm] ")
1424 (ex-line-subr com (point) (mark t)))
1425 (quit (ding)))
1426 (save-excursion (kill-buffer " *join text*")))
1427 (ex-line-subr com (point) (mark t)))
1428 (setq point (point)))
1429 (goto-char (1- point))
1430 (beginning-of-line)))
1432 (declare-function viper-forward-char-carefully "viper-cmd" (&optional arg))
1434 (defun ex-line-subr (com beg end)
1435 (cond ((string= com "join")
1436 (goto-char (min beg end))
1437 (while (and (not (eobp)) (< (point) (max beg end)))
1438 (end-of-line)
1439 (if (and (<= (point) (max beg end)) (not (eobp)))
1440 (progn
1441 (forward-line 1)
1442 (delete-region (point) (1- (point)))
1443 (if (not ex-variant) (fixup-whitespace))))))
1444 ((or (string= com "right") (string= com "left"))
1445 (indent-rigidly
1446 (min beg end) (max beg end)
1447 (if (string= com "right") viper-shift-width (- viper-shift-width)))
1448 (goto-char (max beg end))
1449 (end-of-line)
1450 (viper-forward-char-carefully))))
1453 ;; Ex mark command
1454 ;; Sets the mark to the current point.
1455 ;; If name is omitted, get the name straight from the work buffer."
1456 (defun ex-mark (&optional name)
1457 (let (char)
1458 (if (null ex-addresses)
1459 (setq ex-addresses
1460 (cons (point) nil)))
1461 (if name
1462 (if (eq 1 (length name))
1463 (setq char (string-to-char name))
1464 (error "`%s': Spurious text \"%s\" after mark name"
1465 name (substring name 1)))
1466 (with-current-buffer (setq viper-ex-work-buf
1467 (get-buffer-create viper-ex-work-buf-name))
1468 (skip-chars-forward " \t")
1469 (if (looking-at "[a-z]")
1470 (progn
1471 (setq char (following-char))
1472 (forward-char 1)
1473 (skip-chars-forward " \t")
1474 (if (not (looking-at "[\n|]"))
1475 (error "`%s': %s" ex-token viper-SpuriousText)))
1476 (error "`%s' requires a following letter" ex-token))))
1477 (save-excursion
1478 (goto-char (car ex-addresses))
1479 (point-to-register (viper-int-to-char (1+ (- char ?a)))))))
1483 ;; Alternate file is the file next to the first one in the buffer ring
1484 (defun ex-next (cycle-other-window &optional find-alt-file)
1485 (catch 'ex-edit
1486 (let (count l)
1487 (if (not find-alt-file)
1488 (progn
1489 (viper-get-ex-file)
1490 (if (or (char-or-string-p ex-offset)
1491 (and (not (string= "" ex-file))
1492 (not (string-match "^[0-9]+$" ex-file))))
1493 (progn
1494 (ex-edit t)
1495 (throw 'ex-edit nil))
1496 (setq count (string-to-number ex-file))
1497 (if (= count 0) (setq count 1))
1498 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1499 (setq count 1))
1500 (setq l (viper-undisplayed-files))
1501 (while (> count 0)
1502 (while (and (not (null l)) (null (car l)))
1503 (setq l (cdr l)))
1504 (setq count (1- count))
1505 (if (> count 0)
1506 (setq l (cdr l))))
1507 (if find-alt-file (car l)
1508 (progn
1509 (if (and (car l) (get-file-buffer (car l)))
1510 (let* ((w (if cycle-other-window
1511 (get-lru-window) (selected-window)))
1512 (b (window-buffer w)))
1513 (set-window-buffer w (get-file-buffer (car l)))
1514 (bury-buffer b)
1515 ;; this puts "next <count>" in the ex-command history
1516 (ex-fixup-history viper-last-ex-prompt ex-file))
1517 (error "Not that many undisplayed files")))))))
1520 (defun ex-next-related-buffer (direction &optional no-recursion)
1522 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction)
1524 (let ((file-or-buffer-name
1525 (viper-current-ring-item viper-related-files-and-buffers-ring))
1526 (old-ring viper-related-files-and-buffers-ring)
1527 (old-win (selected-window))
1528 skip-rest buf wind)
1530 (or (and (ring-p viper-related-files-and-buffers-ring)
1531 (> (ring-length viper-related-files-and-buffers-ring) 0))
1532 (error "This buffer has no related files or buffers"))
1534 (or (stringp file-or-buffer-name)
1535 (error
1536 "File and buffer names must be strings, %S" file-or-buffer-name))
1538 (setq buf (cond ((get-buffer file-or-buffer-name))
1539 ((file-exists-p file-or-buffer-name)
1540 (find-file-noselect file-or-buffer-name))
1543 (if (not (viper-buffer-live-p buf))
1544 (error "Didn't find buffer %S or file %S"
1545 file-or-buffer-name
1546 (viper-abbreviate-file-name
1547 (expand-file-name file-or-buffer-name))))
1549 (if (equal buf (current-buffer))
1550 (or no-recursion
1551 ;; try again
1552 (progn
1553 (setq skip-rest t)
1554 (ex-next-related-buffer direction 'norecursion))))
1556 (if skip-rest
1558 ;; setup buffer
1559 (if (setq wind (viper-get-visible-buffer-window buf))
1561 (setq wind (get-lru-window (if (featurep 'xemacs) nil 'visible)))
1562 (set-window-buffer wind buf))
1564 (if (viper-window-display-p)
1565 (progn
1566 (raise-frame (window-frame wind))
1567 (if (equal (window-frame wind) (window-frame old-win))
1568 (save-window-excursion (select-window wind) (sit-for 1))
1569 (select-window wind)))
1570 (save-window-excursion (select-window wind) (sit-for 1)))
1572 (with-current-buffer buf
1573 (setq viper-related-files-and-buffers-ring old-ring))
1575 (setq viper-local-search-start-marker (point-marker))
1579 ;; Force auto save
1580 (defun ex-preserve ()
1581 (message "Autosaving all buffers that need to be saved...")
1582 (do-auto-save t))
1584 (declare-function viper-Put-back "viper-cmd" (arg))
1585 (declare-function viper-put-back "viper-cmd" (arg))
1587 ;; Ex put
1588 (defun ex-put ()
1589 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1590 (viper-get-ex-buffer)
1591 (setq viper-use-register ex-buffer)
1592 (goto-char point)
1593 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
1595 ;; Ex print working directory
1596 (defun ex-pwd ()
1597 (message "%s" default-directory))
1599 ;; Ex quit command
1600 (defun ex-quit ()
1601 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1602 (with-current-buffer (setq viper-ex-work-buf
1603 (get-buffer-create viper-ex-work-buf-name))
1604 (when (= (following-char) ?!) (forward-char 1)))
1605 (if (< viper-expert-level 3)
1606 (save-buffers-kill-emacs)
1607 (kill-buffer (current-buffer))))
1610 (declare-function viper-add-newline-at-eob-if-necessary "viper-cmd" ())
1612 ;; Ex read command
1613 ;; ex-read doesn't support wildcards, because file completion is a better
1614 ;; mechanism. We also don't support # and % (except in :r <shell-command>
1615 ;; because file history is a better mechanism.
1616 (defun ex-read ()
1617 (viper-get-ex-file)
1618 (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
1619 command)
1620 (goto-char point)
1621 (viper-add-newline-at-eob-if-necessary)
1622 (if (not (or (bobp) (eobp))) (forward-line 1))
1623 (if (and (not ex-variant) (string= ex-file ""))
1624 (progn
1625 (if (null buffer-file-name)
1626 (error viper-NoFileSpecified))
1627 (setq ex-file buffer-file-name)))
1628 (if ex-cmdfile
1629 (progn
1630 (setq command
1631 ;; replace # and % with the previous/current file
1632 (ex-expand-filsyms
1633 (concat (shell-quote-argument ex-file) ex-cmdfile-args)
1634 (current-buffer)))
1635 (shell-command command t))
1636 (insert-file-contents ex-file)))
1637 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args))
1639 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1640 (defun ex-fixup-history (&rest args)
1641 (setq viper-ex-history
1642 (cons (mapconcat 'identity args " ") (cdr viper-ex-history))))
1645 ;; Ex recover from emacs \#file\#
1646 (defun ex-recover ()
1647 (viper-get-ex-file)
1648 (if (or ex-append ex-offset)
1649 (error "`recover': %s" viper-SpuriousText))
1650 (if (string= ex-file "")
1651 (progn
1652 (if (null buffer-file-name)
1653 (error "This buffer isn't visiting any file"))
1654 (setq ex-file buffer-file-name))
1655 (setq ex-file (expand-file-name ex-file)))
1656 (if (and (not (string= ex-file (buffer-file-name)))
1657 (buffer-modified-p)
1658 (not ex-variant))
1659 (error "No write since last change (:rec! overrides)"))
1660 (recover-file ex-file))
1662 ;; Tell that `rewind' is obsolete and to use `:next count' instead
1663 (defun ex-rewind ()
1664 (message
1665 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1668 ;; read variable name for ex-set
1669 (defun ex-set-read-variable ()
1670 (let ((minibuffer-local-completion-map
1671 (copy-keymap minibuffer-local-completion-map))
1672 (cursor-in-echo-area t)
1673 str batch)
1674 (define-key
1675 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1676 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1677 (if (viper-set-unread-command-events
1678 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1679 (progn
1680 (setq batch t)
1681 (viper-set-unread-command-events ?\C-m)))
1682 (message ":set <Variable> [= <Value>]")
1683 (or batch (sit-for 2))
1685 (while (string-match "^[ \\t\\n]*$"
1686 (setq str
1687 (completing-read ":set " ex-variable-alist)))
1688 (message ":set <Variable> [= <Value>]")
1689 ;; if there are unread events, don't wait
1690 (or (viper-set-unread-command-events "") (sit-for 2))
1691 ) ; while
1692 str))
1695 (defun ex-set ()
1696 (let ((var (ex-set-read-variable))
1697 (val 0)
1698 (set-cmd "setq")
1699 (ask-if-save t)
1700 (auto-cmd-label "; don't touch or else...")
1701 (delete-turn-on-auto-fill-pattern
1702 "([ \t]*add-hook[ \t]+'viper-insert-state-hook[ \t]+'turn-on-auto-fill.*)")
1703 actual-lisp-cmd lisp-cmd-del-pattern
1704 val2 orig-var)
1705 (setq orig-var var)
1706 (cond ((string= var "all")
1707 (setq ask-if-save nil
1708 set-cmd nil))
1709 ((member var '("ai" "autoindent"))
1710 (setq var "viper-auto-indent"
1711 set-cmd "setq"
1712 ask-if-save nil
1713 val "t"))
1714 ((member var '("ai-g" "autoindent-global"))
1715 (kill-local-variable 'viper-auto-indent)
1716 (setq var "viper-auto-indent"
1717 set-cmd "setq-default"
1718 val "t"))
1719 ((member var '("noai" "noautoindent"))
1720 (setq var "viper-auto-indent"
1721 ask-if-save nil
1722 val "nil"))
1723 ((member var '("noai-g" "noautoindent-global"))
1724 (kill-local-variable 'viper-auto-indent)
1725 (setq var "viper-auto-indent"
1726 set-cmd "setq-default"
1727 val "nil"))
1728 ((member var '("ic" "ignorecase"))
1729 (setq var "viper-case-fold-search"
1730 val "t"))
1731 ((member var '("noic" "noignorecase"))
1732 (setq var "viper-case-fold-search"
1733 val "nil"))
1734 ((member var '("ma" "magic"))
1735 (setq var "viper-re-search"
1736 val "t"))
1737 ((member var '("noma" "nomagic"))
1738 (setq var "viper-re-search"
1739 val "nil"))
1740 ((member var '("ro" "readonly"))
1741 (setq var "buffer-read-only"
1742 val "t"))
1743 ((member var '("noro" "noreadonly"))
1744 (setq var "buffer-read-only"
1745 val "nil"))
1746 ((member var '("sm" "showmatch"))
1747 (setq var "blink-matching-paren"
1748 val "t"))
1749 ((member var '("nosm" "noshowmatch"))
1750 (setq var "blink-matching-paren"
1751 val "nil"))
1752 ((member var '("ws" "wrapscan"))
1753 (setq var "viper-search-wrap-around"
1754 val "t"))
1755 ((member var '("nows" "nowrapscan"))
1756 (setq var "viper-search-wrap-around"
1757 val "nil")))
1758 (if (and set-cmd (eq val 0)) ; value must be set by the user
1759 (let ((cursor-in-echo-area t))
1760 (message ":set %s = <Value>" var)
1761 ;; if there are unread events, don't wait
1762 (or (viper-set-unread-command-events "") (sit-for 2))
1763 (setq val (read-string (format ":set %s = " var)))
1764 (ex-fixup-history "set" orig-var val)
1766 ;; check numerical values
1767 (if (member var
1768 '("sw" "shiftwidth"
1769 "ts" "tabstop"
1770 "ts-g" "tabstop-global"
1771 "wm" "wrapmargin"))
1772 (condition-case nil
1773 (or (numberp (setq val2 (car (read-from-string val))))
1774 (error "%s: Invalid value, numberp, %S" var val))
1775 (error
1776 (error "%s: Invalid value, numberp, %S" var val))))
1778 (cond
1779 ((member var '("sw" "shiftwidth"))
1780 (setq var "viper-shift-width"))
1781 ((member var '("ts" "tabstop"))
1782 ;; make it take effect in curr buff and new bufs
1783 (setq var "tab-width"
1784 set-cmd "setq"
1785 ask-if-save nil))
1786 ((member var '("ts-g" "tabstop-global"))
1787 (kill-local-variable 'tab-width)
1788 (setq var "tab-width"
1789 set-cmd "setq-default"))
1790 ((member var '("wm" "wrapmargin"))
1791 ;; make it take effect in curr buff and new bufs
1792 (kill-local-variable 'fill-column)
1793 (setq var "fill-column"
1794 val (format "(- (window-width) %s)" val)
1795 set-cmd "setq-default"))
1796 ((member var '("sh" "shell"))
1797 (setq var "explicit-shell-file-name"
1798 val (format "\"%s\"" val)))))
1799 (ex-fixup-history "set" orig-var))
1801 (if set-cmd
1802 (setq actual-lisp-cmd
1803 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label)
1804 lisp-cmd-del-pattern
1805 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1806 set-cmd var auto-cmd-label)))
1808 (if (and ask-if-save
1809 (y-or-n-p (format "Do you want to save this setting in %s "
1810 viper-custom-file-name)))
1811 (progn
1812 (viper-save-string-in-file
1813 actual-lisp-cmd viper-custom-file-name
1814 ;; del pattern
1815 lisp-cmd-del-pattern)
1816 (if (string= var "fill-column")
1817 (if (> val2 0)
1818 (viper-save-string-in-file
1819 (concat
1820 "(add-hook 'viper-insert-state-hook 'turn-on-auto-fill) "
1821 auto-cmd-label)
1822 viper-custom-file-name
1823 delete-turn-on-auto-fill-pattern)
1824 (viper-save-string-in-file
1825 nil viper-custom-file-name delete-turn-on-auto-fill-pattern)
1826 (viper-save-string-in-file
1827 nil viper-custom-file-name
1828 ;; del pattern
1829 lisp-cmd-del-pattern)
1833 (if set-cmd
1834 (message "%s %s %s"
1835 set-cmd var
1836 (if (string-match "^[ \t]*$" val)
1837 (format "%S" val)
1838 val)))
1839 (if actual-lisp-cmd
1840 (eval (car (read-from-string actual-lisp-cmd))))
1841 (if (string= var "fill-column")
1842 (if (> val2 0)
1843 (auto-fill-mode 1)
1844 (auto-fill-mode -1)))
1845 (if (string= var "all") (ex-show-vars))
1848 ;; In inline args, skip regex-forw and (optionally) chars-back.
1849 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1850 ;; special meaning
1851 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1852 (with-current-buffer (setq viper-ex-work-buf
1853 (get-buffer-create viper-ex-work-buf-name))
1854 (goto-char (point-min))
1855 (re-search-forward regex-forw nil t)
1856 (let ((beg (point))
1857 end)
1858 (goto-char (point-max))
1859 (if chars-back
1860 (skip-chars-backward chars-back)
1861 (skip-chars-backward " \t\n\C-m"))
1862 (setq end (point))
1863 ;; replace SPC with `=' to suppress the special meaning SPC has
1864 ;; in Ex commands
1865 (goto-char beg)
1866 (if replace-str
1867 (while (re-search-forward " +" nil t)
1868 (replace-match replace-str nil t)
1869 (viper-forward-char-carefully)))
1870 (goto-char end)
1871 (buffer-substring beg end))))
1874 ;; Ex shell command
1875 (defun ex-shell ()
1876 (shell))
1878 ;; Viper help. Invokes Info
1879 (defun ex-help ()
1880 (condition-case nil
1881 (progn
1882 (pop-to-buffer (get-buffer-create "*info*"))
1883 (info (if (featurep 'xemacs) "viper.info" "viper"))
1884 (message "Type `i' to search for a specific topic"))
1885 (error (beep 1)
1886 (with-output-to-temp-buffer " *viper-info*"
1887 (princ (format "
1888 The Info file for Viper does not seem to be installed.
1890 This file is part of the standard distribution of %sEmacs.
1891 Please contact your system administrator. "
1892 (if (featurep 'xemacs) "X" "")
1893 ))))))
1895 ;; Ex source command.
1896 ;; Loads the file specified as argument or viper-custom-file-name.
1897 (defun ex-source ()
1898 (viper-get-ex-file)
1899 (if (string= ex-file "")
1900 (load viper-custom-file-name)
1901 (load ex-file)))
1903 ;; Ex substitute command
1904 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1905 (defun ex-substitute (&optional repeat r-flag)
1906 (let ((opt-g nil)
1907 (opt-c nil)
1908 (matched-pos nil)
1909 (case-fold-search viper-case-fold-search)
1910 delim pat repl)
1911 (if repeat (setq ex-token nil) (setq delim (viper-get-ex-pat)))
1912 (if (null ex-token)
1913 (progn
1914 (setq pat (if r-flag viper-s-string ex-reg-exp))
1915 (or (stringp pat)
1916 (error "No previous pattern to use in substitution"))
1917 (setq repl ex-repl
1918 delim (string-to-char pat)))
1919 (setq pat (if (string= ex-token "") viper-s-string ex-token))
1920 (setq viper-s-string pat
1921 ex-reg-exp pat)
1922 (setq delim (viper-get-ex-pat))
1923 (if (null ex-token)
1924 (setq ex-token ""
1925 ex-repl "")
1926 (setq repl ex-token
1927 ex-repl ex-token)))
1928 (while (viper-get-ex-opt-gc delim)
1929 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1930 (viper-get-ex-count)
1931 (if ex-count
1932 (save-excursion
1933 (if ex-addresses (goto-char (car ex-addresses)))
1934 (set-mark (point))
1935 (forward-line (1- ex-count))
1936 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1937 (if (null ex-addresses)
1938 (setq ex-addresses (cons (point) (cons (point) nil)))
1939 (if (null (cdr ex-addresses))
1940 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1941 ;(setq G opt-g)
1942 (let ((beg (car ex-addresses))
1943 (end (car (cdr ex-addresses)))
1944 eol-mark)
1945 (save-excursion
1946 (viper-enlarge-region beg end)
1947 (let ((limit (save-excursion
1948 (goto-char (max (point) (mark t)))
1949 (point-marker))))
1950 (goto-char (min (point) (mark t)))
1951 (while (< (point) limit)
1952 (save-excursion
1953 (end-of-line)
1954 ;; This move allows the use of newline as the last character in
1955 ;; the substitution pattern
1956 (viper-forward-char-carefully)
1957 (setq eol-mark (point-marker)))
1958 (beginning-of-line)
1959 (if opt-g
1960 (progn
1961 (while (and (not (eolp))
1962 (re-search-forward pat eol-mark t))
1963 (if (or (not opt-c)
1964 (progn
1965 (viper-put-on-search-overlay (match-beginning 0)
1966 (match-end 0))
1967 (y-or-n-p "Replace? ")))
1968 (progn
1969 (viper-hide-search-overlay)
1970 (setq matched-pos (point))
1971 (if (not (stringp repl))
1972 (error "Can't perform Ex substitution: No previous replacement pattern"))
1973 (replace-match repl t))))
1974 (end-of-line)
1975 (viper-forward-char-carefully))
1976 (if (null pat)
1977 (error
1978 "Can't repeat Ex substitution: No previous regular expression"))
1979 (if (and (re-search-forward pat eol-mark t)
1980 (or (not opt-c)
1981 (progn
1982 (viper-put-on-search-overlay (match-beginning 0)
1983 (match-end 0))
1984 (y-or-n-p "Replace? "))))
1985 (progn
1986 (viper-hide-search-overlay)
1987 (setq matched-pos (point))
1988 (if (not (stringp repl))
1989 (error "Can't perform Ex substitution: No previous replacement pattern"))
1990 (replace-match repl t)))
1991 ;;(end-of-line)
1992 ;;(viper-forward-char-carefully)
1993 (goto-char eol-mark)
1994 )))))
1995 (if matched-pos (goto-char matched-pos))
1996 (beginning-of-line)
1997 (if opt-c (message "done"))))
1999 (declare-function viper-change-state-to-emacs "viper-cmd" (&rest _))
2001 ;; Ex tag command
2002 (defun ex-tag ()
2003 (let (tag)
2004 (with-current-buffer (setq viper-ex-work-buf
2005 (get-buffer-create viper-ex-work-buf-name))
2006 (skip-chars-forward " \t")
2007 (set-mark (point))
2008 (skip-chars-forward "^ |\t\n")
2009 (setq tag (buffer-substring (mark t) (point))))
2010 (if (not (string= tag "")) (setq ex-tag tag))
2011 (viper-change-state-to-emacs)
2012 (condition-case conds
2013 (progn
2014 (if (string= tag "")
2015 (find-tag ex-tag t)
2016 (find-tag-other-window ex-tag))
2017 (viper-change-state-to-vi))
2018 (error
2019 (viper-change-state-to-vi)
2020 (viper-message-conditions conds)))))
2022 ;; Ex write command
2023 ;; ex-write doesn't support wildcards, because file completion is a better
2024 ;; mechanism. We also don't support # and %
2025 ;; because file history is a better mechanism.
2026 (defun ex-write (q-flag)
2027 (viper-default-ex-addresses t)
2028 (viper-get-ex-file)
2029 (let ((end (car ex-addresses))
2030 (beg (car (cdr ex-addresses)))
2031 (orig-buf (current-buffer))
2032 ;;(orig-buf-file-name (buffer-file-name))
2033 ;;(orig-buf-name (buffer-name))
2034 ;;(buff-changed-p (buffer-modified-p))
2035 temp-buf writing-same-file region
2036 file-exists writing-whole-file)
2037 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2038 (if ex-cmdfile
2039 (progn
2040 (viper-enlarge-region beg end)
2041 (shell-command-on-region (point) (mark t)
2042 (concat ex-file ex-cmdfile-args)))
2043 (if (and (string= ex-file "") (not (buffer-file-name)))
2044 (setq ex-file
2045 (read-file-name
2046 (format "Buffer %s isn't visiting any file. File to save in: "
2047 (buffer-name)))))
2049 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
2050 ex-file (if (string= ex-file "")
2051 (buffer-file-name)
2052 (expand-file-name ex-file)))
2053 ;; if ex-file is a directory use the file portion of the buffer file name
2054 (if (and (file-directory-p ex-file)
2055 buffer-file-name
2056 (not (file-directory-p buffer-file-name)))
2057 (setq ex-file
2058 (concat (file-name-as-directory ex-file)
2059 (file-name-nondirectory buffer-file-name))))
2061 (setq file-exists (file-exists-p ex-file)
2062 writing-same-file (string= ex-file (buffer-file-name)))
2064 ;; do actual writing
2065 (if (and writing-whole-file writing-same-file)
2066 ;; saving whole buffer in visited file
2067 (if (not (buffer-modified-p))
2068 (message "(No changes need to be saved)")
2069 (viper-maybe-checkout (current-buffer))
2070 (save-buffer)
2071 (save-restriction
2072 (widen)
2073 (ex-write-info file-exists ex-file (point-min) (point-max))
2075 ;; writing to non-visited file and it already exists
2076 (if (and file-exists (not writing-same-file)
2077 (not (yes-or-no-p
2078 (format "File %s exists. Overwrite? " ex-file))))
2079 (error "Quit"))
2080 ;; writing a region or whole buffer to non-visited file
2081 (unwind-protect
2082 (save-excursion
2083 (viper-enlarge-region beg end)
2084 (setq region (buffer-substring (point) (mark t)))
2085 ;; create temp buffer for the region
2086 (setq temp-buf (get-buffer-create " *ex-write*"))
2087 (set-buffer temp-buf)
2088 (if (featurep 'xemacs)
2089 (set-visited-file-name ex-file)
2090 (set-visited-file-name ex-file 'noquery))
2091 (erase-buffer)
2092 (if (and file-exists ex-append)
2093 (insert-file-contents ex-file))
2094 (goto-char (point-max))
2095 (insert region)
2096 ;; ask user
2097 (viper-maybe-checkout (current-buffer))
2098 (setq selective-display nil)
2099 (save-buffer)
2100 (ex-write-info
2101 file-exists ex-file (point-min) (point-max))
2103 ;; this must be under unwind-protect so that
2104 ;; temp-buf will be deleted in case of an error
2105 (set-buffer temp-buf)
2106 (set-buffer-modified-p nil)
2107 (kill-buffer temp-buf)
2108 ;; buffer/region has been written, now take care of details
2109 (set-buffer orig-buf)))
2110 ;; set the right file modification time
2111 (if (and (buffer-file-name) writing-same-file)
2112 (set-visited-file-modtime))
2113 ;; prevent loss of data if saving part of the buffer in visited file
2114 (or writing-whole-file
2115 (not writing-same-file)
2116 (progn
2117 (sit-for 2)
2118 (message "Warning: you have saved only part of the buffer!")
2119 (set-buffer-modified-p t)))
2120 (if q-flag
2121 (if (< viper-expert-level 2)
2122 (save-buffers-kill-emacs)
2123 (kill-buffer (current-buffer))))
2127 (defun ex-write-info (exists file-name beg end)
2128 (message "`%s'%s %d lines, %d characters"
2129 (viper-abbreviate-file-name file-name)
2130 (if exists "" " [New file]")
2131 (count-lines beg (min (1+ end) (point-max)))
2132 (- end beg)))
2134 ;; Ex yank command
2135 (defun ex-yank ()
2136 (viper-default-ex-addresses)
2137 (viper-get-ex-buffer)
2138 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2139 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2140 (save-excursion
2141 (viper-enlarge-region beg end)
2142 (exchange-point-and-mark)
2143 (if (or ex-g-flag ex-g-variant)
2144 (error "Can't execute `yank' within `global'"))
2145 (if ex-count
2146 (progn
2147 (set-mark (point))
2148 (forward-line (1- ex-count)))
2149 (set-mark end))
2150 (viper-enlarge-region (point) (mark t))
2151 (if ex-flag (error "`yank': %s" viper-SpuriousText))
2152 (if ex-buffer
2153 (cond ((viper-valid-register ex-buffer '(Letter))
2154 (viper-append-to-register
2155 (downcase ex-buffer) (point) (mark t)))
2156 ((viper-valid-register ex-buffer)
2157 (copy-to-register ex-buffer (point) (mark t) nil))
2158 (t (error viper-InvalidRegister ex-buffer))))
2159 (copy-region-as-kill (point) (mark t)))))
2161 ;; Execute shell command
2162 (defun ex-command ()
2163 (let (command)
2164 (with-current-buffer (setq viper-ex-work-buf
2165 (get-buffer-create viper-ex-work-buf-name))
2166 (skip-chars-forward " \t")
2167 (setq command (buffer-substring (point) (point-max)))
2168 (end-of-line))
2169 ;; replace # and % with the previous/current file
2170 (setq command (ex-expand-filsyms command (current-buffer)))
2171 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
2172 (if viper-ex-last-shell-com
2173 (setq command
2174 (concat viper-ex-last-shell-com (substring command 1)))
2175 (error "No previous shell command")))
2176 (setq viper-ex-last-shell-com command)
2177 (if (null ex-addresses)
2178 (shell-command command)
2179 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
2180 (if (null beg) (setq beg end))
2181 (save-excursion
2182 (goto-char beg)
2183 (set-mark end)
2184 (viper-enlarge-region (point) (mark t))
2185 (shell-command-on-region (point) (mark t) command t t))
2186 (goto-char beg)))))
2188 (defun ex-compile ()
2189 "Reads args from the command line, then runs make with the args.
2190 If no args are given, then it runs the last compile command.
2191 Type `mak ' (including the space) to run make with no args."
2192 (let (args)
2193 (with-current-buffer (setq viper-ex-work-buf
2194 (get-buffer-create viper-ex-work-buf-name))
2195 (setq args (buffer-substring (point) (point-max)))
2196 (end-of-line))
2197 ;; Remove the newline that may (will?) be at the end of the args
2198 (if (string= "\n" (substring args (1- (length args))))
2199 (setq args (substring args 0 (1- (length args)))))
2200 ;; Run last command if no args given, else construct a new command.
2201 (setq args
2202 (if (string= "" args)
2203 (if (boundp 'compile-command)
2204 compile-command
2205 ex-compile-command)
2206 (concat ex-compile-command " " args)))
2207 (compile args)
2210 ;; Print line number
2211 (defun ex-line-no ()
2212 (message "%d"
2213 (1+ (count-lines
2214 (point-min)
2215 (if (null ex-addresses) (point-max) (car ex-addresses))))))
2217 ;; Give information on the file visited by the current buffer
2218 (defun viper-info-on-file ()
2219 (interactive)
2220 (let ((pos1 (viper-line-pos 'start))
2221 (pos2 (viper-line-pos 'end))
2222 lines file info)
2223 (setq lines (count-lines (point-min) (viper-line-pos 'end))
2224 file (cond ((buffer-file-name)
2225 (concat (viper-abbreviate-file-name (buffer-file-name)) ":"))
2226 ((buffer-file-name (buffer-base-buffer))
2227 (concat (viper-abbreviate-file-name (buffer-file-name (buffer-base-buffer))) " (indirect buffer):"))
2228 (t (concat (buffer-name) " [Not visiting any file]:")))
2229 info (format "line=%d/%d pos=%d/%d col=%d %s"
2230 (if (= pos1 pos2)
2231 (1+ lines)
2232 lines)
2233 (count-lines (point-min) (point-max))
2234 (point) (1- (point-max))
2235 (1+ (current-column))
2236 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
2237 (if (< (+ 1 (length info) (length file))
2238 (window-width (minibuffer-window)))
2239 (message "%s" (concat file " " info))
2240 (save-window-excursion
2241 (with-output-to-temp-buffer " *viper-info*"
2242 (princ (concat "\n" file "\n\n\t" info "\n\n")))
2243 (let ((inhibit-quit t))
2244 (viper-set-unread-command-events (viper-read-event)))
2245 (kill-buffer " *viper-info*")))
2249 ;; Without arguments displays info on file. With an arg, sets the visited file
2250 ;; name to that arg
2251 (defun ex-set-visited-file-name ()
2252 (viper-get-ex-file)
2253 (if (string= ex-file "")
2254 (viper-info-on-file)
2255 ;; If ex-file is a directory, use the file portion of the buffer
2256 ;; file name (like ex-write). Do this even if ex-file is a
2257 ;; non-existent directory, since set-visited-file-name signals an
2258 ;; error on this condition, too.
2259 (if (and (string= (file-name-nondirectory ex-file) "")
2260 buffer-file-name
2261 (not (file-directory-p buffer-file-name)))
2262 (setq ex-file (concat (file-name-as-directory ex-file)
2263 (file-name-nondirectory buffer-file-name))))
2264 (set-visited-file-name ex-file)))
2267 ;; display all variables set through :set
2268 (defun ex-show-vars ()
2269 (with-output-to-temp-buffer " *viper-info*"
2270 (princ (if viper-auto-indent
2271 "autoindent (local)\n" "noautoindent (local)\n"))
2272 (princ (if (default-value 'viper-auto-indent)
2273 "autoindent (global) \n" "noautoindent (global) \n"))
2274 (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n"))
2275 (princ (if viper-re-search "magic\n" "nomagic\n"))
2276 (princ (if buffer-read-only "readonly\n" "noreadonly\n"))
2277 (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n"))
2278 (princ (if viper-search-wrap-around "wrapscan\n" "nowrapscan\n"))
2279 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width))
2280 (princ (format "tabstop (local) \t= %S\n" tab-width))
2281 (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width)))
2282 (princ (format "wrapmargin (local) \t= %S\n"
2283 (- (window-width) fill-column)))
2284 (princ (format "wrapmargin (global) \t= %S\n"
2285 (- (window-width) (default-value 'fill-column))))
2286 (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name)
2287 explicit-shell-file-name
2288 'none)))
2291 (defun ex-print ()
2292 (viper-default-ex-addresses)
2293 (let ((end (car ex-addresses))
2294 (beg (car (cdr ex-addresses))))
2295 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2296 (save-excursion
2297 (viper-enlarge-region beg end)
2298 (if (or ex-g-flag ex-g-variant)
2299 ;; When executing a global command, collect output of each
2300 ;; print in viper-ex-print-buf.
2301 (progn
2302 (append-to-buffer viper-ex-print-buf (point) (mark t))
2303 ;; Is this the last mark for the global command?
2304 (unless (cdr ex-g-marks)
2305 (with-current-buffer viper-ex-print-buf
2306 (ex-print-display-lines (buffer-string))
2307 (erase-buffer))))
2308 (ex-print-display-lines (buffer-substring (point) (mark t)))))))
2310 (defun ex-print-display-lines (lines)
2311 (cond
2312 ;; String doesn't contain a newline.
2313 ((not (string-match "\n" lines))
2314 (message "%s" lines))
2315 ;; String contains only one newline at the end. Strip it off.
2316 ((= (string-match "\n" lines) (1- (length lines)))
2317 (message "%s" (substring lines 0 -1)))
2318 ;; String spans more than one line. Use a temporary buffer.
2320 (save-current-buffer
2321 (with-output-to-temp-buffer " *viper-info*"
2322 (princ lines))))))
2324 ;;; viper-ex.el ends here