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