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