Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / emulation / viper-ex.el
CommitLineData
52fa07ba 1;;; viper-ex.el --- functions implementing the Ex commands for Viper
be010748 2
5fd6d89f 3;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2003,
8b72699e 4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6c2e12f4 5
50a07e18 6;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
02f34c70 7
6c2e12f4
KH
8;; This file is part of GNU Emacs.
9
ed0f493f 10;; GNU Emacs is free software: you can redistribute it and/or modify
6c2e12f4 11;; it under the terms of the GNU General Public License as published by
ed0f493f
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
6c2e12f4
KH
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
ed0f493f 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6c2e12f4 22
60370d40
PJ
23;;; Commentary:
24
25;;; Code:
03fc1246 26
9b70a748 27(provide 'viper-ex)
6c2e12f4 28
03fc1246 29;; Compiler pacifier
52fa07ba 30(defvar read-file-name-map)
8626cfa2
MK
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)
1e70790f 37(defvar viper-expert-level)
8626cfa2
MK
38(defvar viper-custom-file-name)
39(defvar viper-case-fold-search)
e36a387d 40(defvar explicit-shell-file-name)
50a07e18 41(defvar compile-command)
9b70a748 42
726e270f
MK
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
2d84cc27
MK
47 (if (not (featurep 'viper-cmd))
48 (require 'viper-cmd))
2ee00512 49 ))
9b70a748
MK
50;; end pacifier
51
52(require 'viper-util)
53
1e70790f 54(defgroup viper-ex nil
87c81c89 55 "Viper support for Ex commands."
1e70790f
MK
56 :prefix "ex-"
57 :group 'viper)
58
59
03fc1246 60
6c2e12f4 61;;; Variables
bbe6126c 62
8626cfa2 63(defconst viper-ex-work-buf-name " *ex-working-space*")
e07ff078 64(defvar viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 65(defconst viper-ex-tmp-buf-name " *ex-tmp*")
241d963d 66(defconst viper-ex-print-buf-name " *ex-print*")
e07ff078 67(defvar viper-ex-print-buf (get-buffer-create viper-ex-print-buf-name))
52fa07ba
MK
68
69
4986c2c6
MK
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))
b9fe4732 115 ("file" (ex-set-visited-file-name))
4986c2c6
MK
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")
4960e757 124 ("make" (ex-compile))
4986c2c6
MK
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))
241d963d 132 ("print" (ex-print))
4986c2c6
MK
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))
a1506d29 147 ("stop" (suspend-emacs))
4986c2c6
MK
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)
a1506d29 162 ("visual" (ex-edit))
4986c2c6
MK
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"))
4986c2c6
MK
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)
83f49acb
MK
204 (let ((oneletter (ex-cmd-is-one-letter (assoc (substring key 0 1) list))))
205 (if oneletter
4986c2c6 206 (list key
83f49acb 207 (append (cadr oneletter)
4986c2c6 208 (if (< 1 (length key)) (list (substring key 1))))
83f49acb 209 (car (cdr (cdr oneletter))) ))
4986c2c6
MK
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))
4960e757 218 result)
4986c2c6
MK
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
52fa07ba
MK
233
234;; A-list of Ex variables that can be set using the :set command.
a1506d29 235(defconst ex-variable-alist
52fa07ba 236 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
e36a387d 237 ("tabstop-global") ("ts-g") ("tabstop") ("ts")
52fa07ba 238 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
a1506d29 239 ("readonly") ("ro")
52fa07ba
MK
240 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
241 ("noreadonly") ("noro") ("nomagic") ("noma")
242 ("noignorecase") ("noic")
e36a387d 243 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai")
52fa07ba 244 ("magic") ("ma") ("ignorecase") ("ic")
a1506d29
JB
245 ("autoindent-global") ("ai-g") ("autoindent") ("ai")
246 ("all")
52fa07ba 247 ))
bbe6126c 248
a1506d29 249
bbe6126c 250
52fa07ba
MK
251;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
252(defvar ex-token nil)
253
a1506d29 254;; Type of token.
52fa07ba
MK
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
8e41a31c 261;; This flag is supposed to be set only by `#', `print', and `list',
3af0304a
MK
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
8e41a31c 264;; needed in the future.
52fa07ba
MK
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
1e70790f 274;; Flag indicating that :global Ex command is being executed.
52fa07ba 275(defvar ex-g-flag nil)
1e70790f 276;; Flag indicating that :vglobal Ex command is being executed.
52fa07ba 277(defvar ex-g-variant nil)
241d963d
MK
278;; Marks to operate on during a :global Ex command.
279(defvar ex-g-marks nil)
52fa07ba
MK
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
1e70790f 291(defcustom ex-unix-type-shell
52fa07ba
MK
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)))
1e70790f 308 "Is the user using a unix-type shell under a non-OS?"
5ee3742d 309 :type 'boolean
1e70790f 310 :group 'viper-ex)
52fa07ba 311
1e70790f 312(defcustom ex-unix-type-shell-options
52fa07ba
MK
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 )))
a1506d29 320 "Options to pass to the Unix-style shell.
1e70790f 321Don't put `-c' here, as it is added automatically."
ee6a3436 322 :type '(choice (const nil) string)
1e70790f 323 :group 'viper-ex)
52fa07ba 324
4960e757 325(defcustom ex-compile-command "make"
c760d040 326 "The command to run when the user types :make."
4960e757
MK
327 :type 'string
328 :group 'viper-ex)
329
3af0304a
MK
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.
338The default tries to set this variable to work with Unix, Windows,
339OS/2, and VMS.
340
a1506d29 341However, if it doesn't work right for some types of Unix shells or some OS,
3af0304a
MK
342the user should supply the appropriate function and set this variable to the
343corresponding function symbol."
344 :type 'symbol
345 :group 'viper-ex)
346
bbe6126c 347
52fa07ba
MK
348;; Remembers the previous Ex tag.
349(defvar ex-tag nil)
bbe6126c 350
52fa07ba
MK
351;; file used by Ex commands like :r, :w, :n
352(defvar ex-file nil)
bbe6126c 353
52fa07ba
MK
354;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
355(defvar ex-variant nil)
bbe6126c 356
52fa07ba
MK
357;; Specified the offset of an Ex command, such as :read.
358(defvar ex-offset nil)
bbe6126c 359
52fa07ba
MK
360;; Tells Ex that this is a w>> command.
361(defvar ex-append nil)
bbe6126c 362
52fa07ba
MK
363;; File containing the shell command to be executed at Ex prompt,
364;; e.g., :r !date
365(defvar ex-cmdfile nil)
328b4b70 366(defvar ex-cmdfile-args "")
a1506d29 367
8626cfa2 368;; flag used in viper-ex-read-file-name to indicate that we may be reading
3af0304a 369;; multiple file names. Used for :edit and :next
8626cfa2 370(defvar viper-keep-reading-filename nil)
bbe6126c 371
1e70790f 372(defcustom ex-cycle-other-window t
52fa07ba 373 "*If t, :n and :b cycles through files and buffers in other window.
3af0304a 374Then :N and :B cycles in the current window. If nil, this behavior is
1e70790f
MK
375reversed."
376 :type 'boolean
377 :group 'viper-ex)
bbe6126c 378
1e70790f
MK
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)
bbe6126c 383
52fa07ba 384;; Last shell command executed with :! command.
8626cfa2 385(defvar viper-ex-last-shell-com nil)
a1506d29 386
52fa07ba 387;; Indicates if Minibuffer was exited temporarily in Ex-command.
8626cfa2 388(defvar viper-incomplete-ex-cmd nil)
a1506d29 389
52fa07ba 390;; Remembers the last ex-command prompt.
8626cfa2 391(defvar viper-last-ex-prompt "")
bbe6126c 392
bbe6126c 393
52fa07ba 394;; Get a complete ex command
8626cfa2 395(defun viper-get-ex-com-subr ()
4986c2c6 396 (let (cmd case-fold-search)
52fa07ba
MK
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)))
4986c2c6
MK
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))
52fa07ba 405 ))
bbe6126c 406
52fa07ba
MK
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
8626cfa2 409(defun viper-get-ex-token ()
52fa07ba 410 (save-window-excursion
a1506d29 411 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 412 (set-buffer viper-ex-work-buf)
52fa07ba 413 (skip-chars-forward " \t|")
96dffd25
MK
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
027a4b6b 431 (string-to-number (buffer-substring (point) (mark t)))))
96dffd25
MK
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)
52fa07ba 443 (setq ex-token-type 'plus))
96dffd25
MK
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))))))
bbe6126c 508
3af0304a
MK
509;; Reads Ex command. Tries to determine if it has to exit because command
510;; is complete or invalid. If not, keeps reading command.
52fa07ba
MK
511(defun ex-cmd-read-exit ()
512 (interactive)
8626cfa2 513 (setq viper-incomplete-ex-cmd t)
52fa07ba
MK
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]*$"
1e70790f 529 "\\|" "[?/].*"
52fa07ba
MK
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.*"
2eb4bdca
MK
542 ;; don't jump up in :s command
543 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*su.*"
544 "\\|" "^[ \t]*\\([`'][a-z]\\|[.,%]\\)*[ \t]*s[^a-z].*"
328b4b70
MK
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]+!"
52fa07ba
MK
550 "\\|" "![ \t]*[a-zA-Z].*"
551 "\\)"
552 "!*")))
a1506d29 553
52fa07ba 554 (save-window-excursion ;; put cursor at the end of the Ex working buffer
a1506d29 555 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 556 (set-buffer viper-ex-work-buf)
52fa07ba 557 (goto-char (point-max)))
8626cfa2
MK
558 (cond ((viper-looking-back quit-regex1) (exit-minibuffer))
559 ((viper-looking-back stay-regex) (insert " "))
560 ((viper-looking-back quit-regex2) (exit-minibuffer))
52fa07ba 561 (t (insert " ")))))
a1506d29 562
52fa07ba
MK
563;; complete Ex command
564(defun ex-cmd-complete ()
565 (interactive)
566 (let (save-pos dist compl-list string-to-complete completion-result)
a1506d29 567
52fa07ba
MK
568 (save-excursion
569 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
570 save-pos (point)))
a1506d29 571
52fa07ba 572 (if (or (= dist 0)
8626cfa2
MK
573 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
574 (viper-looking-back
3af0304a 575 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+"))
52fa07ba
MK
576 ;; Preceding characters are not the ones allowed in an Ex command
577 ;; or we have typed past command name.
3af0304a 578 ;; Note: we didn't do parsing, so there can be surprises.
8626cfa2
MK
579 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
580 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
52fa07ba
MK
581 (looking-at "[^ \t\n\C-m]"))
582 nil
a1506d29 583 (with-output-to-temp-buffer "*Completions*"
52fa07ba 584 (display-completion-list
8626cfa2 585 (viper-alist-to-list ex-token-alist))))
52fa07ba
MK
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))
a1506d29 590
52fa07ba 591 (cond ((eq completion-result t) ; exact match--do nothing
8626cfa2 592 (viper-tmp-insert-at-eob " (Sole completion)"))
52fa07ba 593 ((eq completion-result nil)
8626cfa2 594 (viper-tmp-insert-at-eob " (No match)"))
52fa07ba
MK
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
8626cfa2 601 (viper-filter-alist (concat "^" completion-result)
52fa07ba
MK
602 ex-token-alist)))
603 (if (> (length compl-list) 1)
a1506d29 604 (with-output-to-temp-buffer "*Completions*"
52fa07ba 605 (display-completion-list
8626cfa2 606 (viper-alist-to-list (reverse compl-list)))))))
52fa07ba 607 )))
bbe6126c 608
a1506d29
JB
609
610;; Read Ex commands
3af0304a
MK
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
a1506d29 614;; command.
c004db97
MK
615(defun viper-ex (arg &optional string)
616 (interactive "P")
52fa07ba
MK
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))
c004db97
MK
624 reg-beg-line reg-end-line
625 reg-beg reg-end
626 initial-str
52fa07ba 627 prev-token-type com-str)
8626cfa2 628 (viper-add-keymap viper-ex-cmd-map map)
c004db97
MK
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)))
a1506d29
JB
645
646 (setq com-str
33468a59
MK
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]"))))
52fa07ba
MK
660 (save-window-excursion
661 ;; just a precaution
a1506d29 662 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 663 (set-buffer viper-ex-work-buf)
52fa07ba
MK
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
8626cfa2 670 (viper-get-ex-token)
52fa07ba
MK
671 (cond ((memq ex-token-type '(command end-mark))
672 (if address (setq ex-addresses (cons address ex-addresses)))
4986c2c6
MK
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 ))
52fa07ba 695 ((eq ex-token-type 'non-command)
8626cfa2 696 (error "`%s': %s" ex-token viper-BadExCommand))
52fa07ba
MK
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)))
8626cfa2 714 (t (let ((ans (viper-get-ex-address-subr address dot)))
52fa07ba
MK
715 (if ans (setq address ans)))))
716 (setq prev-token-type ex-token-type))))
a1506d29 717
bbe6126c 718
52fa07ba 719;; Get a regular expression and set `ex-variant', if found
2d341681
MK
720;; Viper doesn't parse the substitution or search patterns.
721;; In particular, it doesn't expand ~ into the last substitution.
8626cfa2 722(defun viper-get-ex-pat ()
52fa07ba 723 (save-window-excursion
8626cfa2
MK
724 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
725 (set-buffer viper-ex-work-buf)
52fa07ba
MK
726 (skip-chars-forward " \t")
727 (if (looking-at "!")
2eb4bdca 728 ;; this is probably a variant command r!
52fa07ba
MK
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)))
2eb4bdca
MK
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")))
52fa07ba
MK
739 (if (looking-at "[^\\\\\n]")
740 (progn
741 (forward-char 1)
742 (set-mark (point))
743 (let ((cont t))
2eb4bdca
MK
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)
52fa07ba
MK
747 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
748 (if (member ex-token '("global" "vglobal"))
2eb4bdca 749 (error "Missing closing delimiter for global regexp")
52fa07ba 750 (goto-char (point-max))))
8626cfa2 751 (if (not (viper-looking-back
52fa07ba 752 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
2eb4bdca
MK
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 ))
52fa07ba
MK
764 (setq ex-token
765 (if (= (mark t) (point)) ""
766 (buffer-substring (1- (point)) (mark t))))
1e70790f 767 (backward-char 1)
2eb4bdca 768 ;; if the user didn't insert the final pattern delimiter, we're
3af0304a 769 ;; at newline now. In this case, insert the initial delimiter
1e70790f 770 ;; specified in variable c
2eb4bdca 771 (if (eolp)
1e70790f 772 (progn
2eb4bdca
MK
773 (insert c)
774 (backward-char 1)))
1e70790f 775 )
52fa07ba
MK
776 (setq ex-token nil))
777 c)))
bbe6126c 778
52fa07ba 779;; Get an Ex option g or c
8626cfa2 780(defun viper-get-ex-opt-gc (c)
52fa07ba 781 (save-window-excursion
a1506d29 782 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 783 (set-buffer viper-ex-work-buf)
52fa07ba
MK
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
8626cfa2 797(defun viper-default-ex-addresses (&optional whole-flag)
52fa07ba
MK
798 (cond ((null ex-addresses)
799 (setq ex-addresses
800 (if whole-flag
2eb4bdca
MK
801 (list (point-max) (point-min))
802 (list (point) (point)))))
52fa07ba
MK
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
8626cfa2 808(defun viper-get-ex-address ()
9b70a748
MK
809 (let ((address (point-marker))
810 (cont t))
52fa07ba
MK
811 (setq ex-token "")
812 (setq ex-flag nil)
813 (while cont
8626cfa2 814 (viper-get-ex-token)
52fa07ba
MK
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)
8626cfa2
MK
826 (error "`%s': %s" ex-token viper-SpuriousText))
827 (t (let ((ans (viper-get-ex-address-subr address (point-marker))))
52fa07ba
MK
828 (if ans (setq address ans))))))
829 address))
830
831;; Returns an address as a point
8626cfa2 832(defun viper-get-ex-address-subr (old-address dot)
52fa07ba
MK
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)
2d341681
MK
854 (save-excursion
855 (goto-char (1- (point-max)))
856 (setq address (point-marker))))
52fa07ba
MK
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)
a1506d29 871 (goto-char
4960e757
MK
872 (viper-register-to-point
873 (viper-int-to-char (1+ (- ex-token ?a))) 'enforce-buffer)))
52fa07ba
MK
874 (setq address (point-marker)))))
875 address))
876
877
878;; Search pattern and set address
2d341681 879;; Doesn't wrap around. Should it?
52fa07ba
MK
880(defun ex-search-address (forward)
881 (if (string= ex-token "")
8626cfa2
MK
882 (if (null viper-s-string)
883 (error viper-NoPrevSearch)
884 (setq ex-token viper-s-string))
885 (setq viper-s-string ex-token))
52fa07ba
MK
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
8626cfa2 894(defun viper-get-ex-buffer ()
52fa07ba
MK
895 (setq ex-buffer nil)
896 (setq ex-count nil)
897 (setq ex-flag nil)
898 (save-window-excursion
a1506d29 899 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 900 (set-buffer viper-ex-work-buf)
52fa07ba
MK
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]*")
027a4b6b 911 (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
52fa07ba
MK
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|]"))
8626cfa2 918 (error "`%s': %s" ex-token viper-SpuriousText))))
52fa07ba 919
8626cfa2 920(defun viper-get-ex-count ()
52fa07ba
MK
921 (setq ex-variant nil
922 ex-count nil
923 ex-flag nil)
924 (save-window-excursion
a1506d29 925 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 926 (set-buffer viper-ex-work-buf)
52fa07ba
MK
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]*")
027a4b6b 937 (setq ex-count (string-to-number (buffer-substring (point) (mark t))))
52fa07ba
MK
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"
8626cfa2
MK
945 (buffer-substring
946 (point-min) (1- (point-max))) viper-BadExCommand))))
52fa07ba
MK
947
948;; Expand \% and \# in ex command
949(defun ex-expand-filsyms (cmd buf)
950 (let (cf pf ret)
a1506d29 951 (save-excursion
52fa07ba
MK
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
8626cfa2 960 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
52fa07ba
MK
961 (erase-buffer)
962 (insert cmd)
963 (goto-char (point-min))
964 (while (re-search-forward "%\\|#" nil t)
a1506d29 965 (let ((data (match-data))
52fa07ba 966 (char (buffer-substring (match-beginning 0) (match-end 0))))
8626cfa2 967 (if (viper-looking-back (concat "\\\\" char))
52fa07ba 968 (replace-match char)
2eb4bdca 969 (store-match-data data)
52fa07ba
MK
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
328b4b70
MK
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
8626cfa2 980(defun viper-get-ex-file ()
52fa07ba
MK
981 (let (prompt)
982 (setq ex-file nil
983 ex-variant nil
984 ex-append nil
985 ex-offset nil
328b4b70
MK
986 ex-cmdfile nil
987 ex-cmdfile-args "")
52fa07ba
MK
988 (save-excursion
989 (save-window-excursion
a1506d29 990 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 991 (set-buffer viper-ex-work-buf)
52fa07ba
MK
992 (skip-chars-forward " \t")
993 (if (looking-at "!")
8626cfa2 994 (if (and (not (viper-looking-back "[ \t]"))
52fa07ba
MK
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
3af0304a 1027 ;; by white. For others, find the first non-white string
52fa07ba
MK
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
8626cfa2 1036 (setq viper-incomplete-ex-cmd nil)
328b4b70
MK
1037 (setq ex-cmdfile-args
1038 (substring ex-file (match-end 0) nil))
52fa07ba
MK
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 ))
a1506d29 1051
8626cfa2 1052 (setq viper-last-ex-prompt prompt)
a1506d29 1053
52fa07ba 1054 ;; If we just finished reading command, redisplay prompt
8626cfa2
MK
1055 (if viper-incomplete-ex-cmd
1056 (setq ex-file (viper-ex-read-file-name (format ":%s " prompt)))
52fa07ba
MK
1057 ;; file was typed in-line
1058 (setq ex-file (or ex-file "")))
1059 ))
bbe6126c 1060
bbe6126c 1061
3af0304a 1062;; Completes file name or exits minibuffer. If Ex command accepts multiple
52fa07ba 1063;; file names, arranges to re-enter the minibuffer.
8626cfa2 1064(defun viper-complete-filename-or-exit ()
52fa07ba 1065 (interactive)
a1506d29
JB
1066 (setq viper-keep-reading-filename t)
1067 ;; don't exit if directory---ex-commands don't
52fa07ba
MK
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
8626cfa2 1071 ((viper-looking-back "^[ \t]*!.*")
52fa07ba
MK
1072 (setq ex-cmdfile t)
1073 (insert " "))
1074 (t
1075 (setq ex-cmdfile nil)
1076 (minibuffer-complete-word))))
bbe6126c 1077
8626cfa2 1078(defun viper-handle-! ()
52fa07ba
MK
1079 (interactive)
1080 (if (and (string=
8626cfa2 1081 (buffer-string) (viper-abbreviate-file-name default-directory))
52fa07ba
MK
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
328b4b70 1089;; Read file name from the minibuffer in an ex command.
52fa07ba
MK
1090;; If user doesn't enter anything, then "" is returned, i.e., the
1091;; prompt-directory is not returned.
8626cfa2 1092(defun viper-ex-read-file-name (prompt)
52fa07ba
MK
1093 (let* ((str "")
1094 (minibuffer-local-completion-map
1095 (copy-keymap minibuffer-local-completion-map))
1096 beg end cont val)
a1506d29 1097
8626cfa2 1098 (viper-add-keymap ex-read-filename-map
e83d1fe8 1099 (if (featurep 'emacs)
52fa07ba 1100 minibuffer-local-completion-map
a1506d29
JB
1101 read-file-name-map))
1102
8626cfa2 1103 (setq cont (setq viper-keep-reading-filename t))
52fa07ba 1104 (while cont
8626cfa2 1105 (setq viper-keep-reading-filename nil
52fa07ba 1106 val (read-file-name (concat prompt str) nil default-directory))
2eb4bdca
MK
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 "\"")))
52fa07ba
MK
1111 (setq str (concat str (if (equal val "") "" " ")
1112 val (if (equal val "") "" " ")))
a1506d29 1113
52fa07ba 1114 ;; Only edit, next, and Next commands accept multiple files.
8626cfa2 1115 ;; viper-keep-reading-filename is set in the anonymous function that is
52fa07ba 1116 ;; bound to " " in ex-read-filename-map.
8626cfa2 1117 (setq cont (and viper-keep-reading-filename
52fa07ba
MK
1118 (ex-cmd-accepts-multiple-files-p ex-token)))
1119 )
a1506d29 1120
52fa07ba
MK
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))
8626cfa2
MK
1129 (setq viper-last-ex-prompt
1130 (concat viper-last-ex-prompt " !")))))
52fa07ba 1131 (substring str (or beg 0) end)))
bbe6126c 1132
52fa07ba 1133
8626cfa2 1134(defun viper-undisplayed-files ()
52fa07ba 1135 (mapcar
a1506d29 1136 (lambda (b)
3af0304a
MK
1137 (if (null (get-buffer-window b))
1138 (let ((f (buffer-file-name b)))
1139 (if f f
a1506d29 1140 (if ex-cycle-through-non-files
3af0304a
MK
1141 (let ((s (buffer-name b)))
1142 (if (string= " " (substring s 0 1))
1143 nil
1144 s))
1145 nil)))
1146 nil))
52fa07ba
MK
1147 (buffer-list)))
1148
1149
1150(defun ex-args ()
8626cfa2 1151 (let ((l (viper-undisplayed-files))
52fa07ba
MK
1152 (args "")
1153 (file-count 1))
1154 (while (not (null l))
a1506d29 1155 (if (car l)
52fa07ba
MK
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
8626cfa2 1163 (with-output-to-temp-buffer " *viper-info*"
52fa07ba
MK
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"))
8626cfa2 1170 (viper-read-event))))))
52fa07ba 1171
3af0304a 1172;; Ex cd command. Default directory of this buffer changes
52fa07ba 1173(defun ex-cd ()
8626cfa2 1174 (viper-get-ex-file)
52fa07ba
MK
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)
8626cfa2
MK
1181 (viper-default-ex-addresses)
1182 (let ((address (viper-get-ex-address))
52fa07ba
MK
1183 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1184 (goto-char end)
1185 (save-excursion
1186 (push-mark beg t)
8626cfa2 1187 (viper-enlarge-region (mark t) (point))
52fa07ba
MK
1188 (if del-flag
1189 (kill-region (point) (mark t))
1190 (copy-region-as-kill (point) (mark t)))
1191 (if ex-flag
1192 (progn
8e41a31c 1193 (with-output-to-temp-buffer " *copy text*"
52fa07ba
MK
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
8e41a31c
MK
1200 (read-string "[Hit return to confirm] ")
1201 (save-excursion (kill-buffer " *copy text*")))
1202 (quit (save-excursion (kill-buffer " *copy text*"))
52fa07ba
MK
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 ()
8626cfa2
MK
1212 (viper-default-ex-addresses)
1213 (viper-get-ex-buffer)
52fa07ba 1214 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
8626cfa2 1215 (if (> beg end) (error viper-FirstAddrExceedsSecond))
52fa07ba 1216 (save-excursion
8626cfa2 1217 (viper-enlarge-region beg end)
52fa07ba
MK
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))
8626cfa2 1224 (viper-enlarge-region (point) (mark t))
52fa07ba
MK
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
8e41a31c 1231 (read-string "[Hit return to confirm] ")
52fa07ba
MK
1232 (quit
1233 (save-excursion (kill-buffer " *delete text*"))
69441214 1234 (error "Viper bell")))
52fa07ba
MK
1235 (save-excursion (kill-buffer " *delete text*")))
1236 (if ex-buffer
8626cfa2
MK
1237 (cond ((viper-valid-register ex-buffer '(Letter))
1238 (viper-append-to-register
52fa07ba 1239 (downcase ex-buffer) (point) (mark t)))
8626cfa2 1240 ((viper-valid-register ex-buffer)
52fa07ba 1241 (copy-to-register ex-buffer (point) (mark t) nil))
8626cfa2 1242 (t (error viper-InvalidRegister ex-buffer))))
52fa07ba
MK
1243 (kill-region (point) (mark t))))))
1244
1245
1246
1247;; Ex edit command
3af0304a 1248;; In Viper, `e' and `e!' behave identically. In both cases, the user is
52fa07ba 1249;; asked if current buffer should really be discarded.
3af0304a 1250;; This command can take multiple file names. It replaces the current buffer
52fa07ba
MK
1251;; with the first file in its argument list
1252(defun ex-edit (&optional file)
1253 (if (not file)
8626cfa2 1254 (viper-get-ex-file))
52fa07ba 1255 (cond ((and (string= ex-file "") buffer-file-name)
8626cfa2 1256 (setq ex-file (viper-abbreviate-file-name (buffer-file-name))))
52fa07ba 1257 ((string= ex-file "")
8626cfa2 1258 (error viper-NoFileSpecified)))
a1506d29 1259
2d341681
MK
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))))
a1506d29 1273
2d341681
MK
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
a1506d29 1281
52fa07ba 1282 (if (null (setq file (get-file-buffer ex-file)))
a1506d29 1283 (progn
3af0304a
MK
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)))
ab124470 1288 (or (eq major-mode 'dired-mode)
8626cfa2 1289 (viper-change-state-to-vi))
52fa07ba
MK
1290 (goto-char (point-min)))
1291 (switch-to-buffer file))
1292 (if ex-offset
1293 (progn
1294 (save-window-excursion
a1506d29 1295 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 1296 (set-buffer viper-ex-work-buf)
52fa07ba
MK
1297 (delete-region (point-min) (point-max))
1298 (insert ex-offset "\n")
1299 (goto-char (point-min)))
8626cfa2 1300 (goto-char (viper-get-ex-address))
52fa07ba 1301 (beginning-of-line)))
8626cfa2 1302 (ex-fixup-history viper-last-ex-prompt ex-file))
52fa07ba 1303
ab124470 1304;; Find-file FILESPEC if it appears to specify a single file.
2eb4bdca 1305;; Otherwise, assume that FILESPEC is a wildcard.
ab124470 1306;; In this case, split it into substrings separated by newlines.
3af0304a 1307;; Each line is assumed to be a file name.
52fa07ba 1308(defun ex-find-file (filespec)
ab124470
MK
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)
3af0304a 1312 (mapcar 'find-file (funcall viper-glob-function filespec)))
ab124470 1313 (t (find-file filespec)))
52fa07ba 1314 ))
bbe6126c 1315
6c2e12f4 1316
52fa07ba 1317;; Ex global command
1e70790f
MK
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
52fa07ba
MK
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)))
8626cfa2 1335 (viper-get-ex-pat)
52fa07ba
MK
1336 (if (null ex-token)
1337 (error "`%s': Missing regular expression" gcommand)))
a1506d29 1338
52fa07ba 1339 (if (string= ex-token "")
8626cfa2
MK
1340 (if (null viper-s-string)
1341 (error viper-NoPrevSearch)
1342 (setq ex-g-pat viper-s-string))
52fa07ba 1343 (setq ex-g-pat ex-token
8626cfa2 1344 viper-s-string ex-token))
52fa07ba
MK
1345 (if (null ex-addresses)
1346 (setq ex-addresses (list (point-max) (point-min)))
8626cfa2 1347 (viper-default-ex-addresses))
241d963d
MK
1348 (setq ex-g-marks nil)
1349 (let ((mark-count 0)
1e70790f
MK
1350 (end (car ex-addresses))
1351 (beg (car (cdr ex-addresses)))
1352 com-str)
8626cfa2 1353 (if (> beg end) (error viper-FirstAddrExceedsSecond))
52fa07ba 1354 (save-excursion
8626cfa2 1355 (viper-enlarge-region beg end)
52fa07ba
MK
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)
8626cfa2 1361 (if (eobp) (viper-backward-char-carefully))
52fa07ba
MK
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))
241d963d 1372 (setq ex-g-marks (cons (point-marker) ex-g-marks)))))
52fa07ba
MK
1373 (beginning-of-line)
1374 (if (bobp) (setq cont nil)
1375 (forward-line -1)
1376 (end-of-line)))))
1377 (save-window-excursion
a1506d29 1378 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 1379 (set-buffer viper-ex-work-buf)
3af0304a 1380 ;; com-str is the command string, i.e., g/pattern/ or v/pattern'
52fa07ba 1381 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
241d963d
MK
1382 (while ex-g-marks
1383 (goto-char (car ex-g-marks))
3af0304a 1384 (viper-ex nil com-str)
52fa07ba 1385 (setq mark-count (1- mark-count))
241d963d 1386 (setq ex-g-marks (cdr ex-g-marks)))))
52fa07ba
MK
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))
3af0304a
MK
1394 (beginning-of-line)
1395 )
52fa07ba
MK
1396
1397;; Ex line commands. COM is join, shift-right or shift-left
1398(defun ex-line (com)
8626cfa2
MK
1399 (viper-default-ex-addresses)
1400 (viper-get-ex-count)
52fa07ba 1401 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
8626cfa2 1402 (if (> beg end) (error viper-FirstAddrExceedsSecond))
52fa07ba 1403 (save-excursion
8626cfa2 1404 (viper-enlarge-region beg end)
52fa07ba
MK
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
8e41a31c 1413 (with-output-to-temp-buffer " *join text*"
52fa07ba
MK
1414 (princ (buffer-substring (point) (mark t))))
1415 (condition-case nil
1416 (progn
8e41a31c 1417 (read-string "[Hit return to confirm] ")
52fa07ba
MK
1418 (ex-line-subr com (point) (mark t)))
1419 (quit (ding)))
8e41a31c 1420 (save-excursion (kill-buffer " *join text*")))
52fa07ba
MK
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)
8626cfa2 1439 (if (string= com "right") viper-shift-width (- viper-shift-width)))
52fa07ba
MK
1440 (goto-char (max beg end))
1441 (end-of-line)
8626cfa2 1442 (viper-forward-char-carefully))))
52fa07ba
MK
1443
1444
1445;; Ex mark command
4986c2c6
MK
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)
52fa07ba
MK
1449 (let (char)
1450 (if (null ex-addresses)
1451 (setq ex-addresses
1452 (cons (point) nil)))
4986c2c6
MK
1453 (if name
1454 (if (eq 1 (length name))
1455 (setq char (string-to-char name))
60370d40 1456 (error "`%s': Spurious text \"%s\" after mark name"
0b762d9a 1457 name (substring name 1)))
52fa07ba 1458 (save-window-excursion
a1506d29 1459 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 1460 (set-buffer viper-ex-work-buf)
52fa07ba
MK
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|]"))
8626cfa2 1468 (error "`%s': %s" ex-token viper-SpuriousText)))
4986c2c6 1469 (error "`%s' requires a following letter" ex-token))))
52fa07ba
MK
1470 (save-excursion
1471 (goto-char (car ex-addresses))
4960e757 1472 (point-to-register (viper-int-to-char (1+ (- char ?a)))))))
6c2e12f4 1473
a1506d29
JB
1474
1475
52fa07ba
MK
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)
a1506d29 1480 (if (not find-alt-file)
bbe6126c 1481 (progn
8626cfa2 1482 (viper-get-ex-file)
52fa07ba 1483 (if (or (char-or-string-p ex-offset)
a1506d29 1484 (and (not (string= "" ex-file))
52fa07ba 1485 (not (string-match "^[0-9]+$" ex-file))))
bbe6126c 1486 (progn
52fa07ba
MK
1487 (ex-edit t)
1488 (throw 'ex-edit nil))
027a4b6b 1489 (setq count (string-to-number ex-file))
52fa07ba
MK
1490 (if (= count 0) (setq count 1))
1491 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1492 (setq count 1))
8626cfa2 1493 (setq l (viper-undisplayed-files))
52fa07ba
MK
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
8626cfa2 1509 (ex-fixup-history viper-last-ex-prompt ex-file))
52fa07ba
MK
1510 (error "Not that many undisplayed files")))))))
1511
1512
1513(defun ex-next-related-buffer (direction &optional no-recursion)
a1506d29 1514
8626cfa2 1515 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction)
a1506d29
JB
1516
1517 (let ((file-or-buffer-name
8626cfa2
MK
1518 (viper-current-ring-item viper-related-files-and-buffers-ring))
1519 (old-ring viper-related-files-and-buffers-ring)
52fa07ba
MK
1520 (old-win (selected-window))
1521 skip-rest buf wind)
a1506d29 1522
8626cfa2
MK
1523 (or (and (ring-p viper-related-files-and-buffers-ring)
1524 (> (ring-length viper-related-files-and-buffers-ring) 0))
52fa07ba 1525 (error "This buffer has no related files or buffers"))
a1506d29 1526
52fa07ba
MK
1527 (or (stringp file-or-buffer-name)
1528 (error
1529 "File and buffer names must be strings, %S" file-or-buffer-name))
a1506d29 1530
52fa07ba
MK
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 ))
a1506d29 1535
8626cfa2 1536 (if (not (viper-buffer-live-p buf))
52fa07ba
MK
1537 (error "Didn't find buffer %S or file %S"
1538 file-or-buffer-name
8626cfa2 1539 (viper-abbreviate-file-name
52fa07ba 1540 (expand-file-name file-or-buffer-name))))
a1506d29 1541
52fa07ba
MK
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))))
a1506d29 1548
52fa07ba
MK
1549 (if skip-rest
1550 ()
1551 ;; setup buffer
8626cfa2 1552 (if (setq wind (viper-get-visible-buffer-window buf))
52fa07ba 1553 ()
e83d1fe8 1554 (setq wind (get-lru-window (if (featurep 'xemacs) nil 'visible)))
52fa07ba 1555 (set-window-buffer wind buf))
a1506d29 1556
8626cfa2 1557 (if (viper-window-display-p)
52fa07ba
MK
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)))
a1506d29 1564
52fa07ba
MK
1565 (save-excursion
1566 (set-buffer buf)
8626cfa2 1567 (setq viper-related-files-and-buffers-ring old-ring))
a1506d29 1568
8626cfa2 1569 (setq viper-local-search-start-marker (point-marker))
52fa07ba 1570 )))
a1506d29
JB
1571
1572
52fa07ba
MK
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))))
8626cfa2
MK
1581 (viper-get-ex-buffer)
1582 (setq viper-use-register ex-buffer)
52fa07ba 1583 (goto-char point)
8626cfa2 1584 (if (bobp) (viper-Put-back 1) (viper-put-back 1))))
52fa07ba
MK
1585
1586;; Ex print working directory
1587(defun ex-pwd ()
7b8a295e 1588 (message "%s" default-directory))
52fa07ba
MK
1589
1590;; Ex quit command
1591(defun ex-quit ()
3af0304a 1592 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
52fa07ba 1593 (save-excursion
a1506d29 1594 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 1595 (set-buffer viper-ex-work-buf)
52fa07ba 1596 (if (looking-at "!") (forward-char 1)))
1e70790f 1597 (if (< viper-expert-level 3)
52fa07ba
MK
1598 (save-buffers-kill-emacs)
1599 (kill-buffer (current-buffer))))
6c2e12f4 1600
6c2e12f4 1601
52fa07ba 1602;; Ex read command
3af0304a
MK
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.
52fa07ba 1606(defun ex-read ()
8626cfa2 1607 (viper-get-ex-file)
52fa07ba
MK
1608 (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
1609 command)
1610 (goto-char point)
8626cfa2 1611 (viper-add-newline-at-eob-if-necessary)
52fa07ba
MK
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)
8626cfa2 1616 (error viper-NoFileSpecified))
52fa07ba
MK
1617 (setq ex-file buffer-file-name)))
1618 (if ex-cmdfile
1619 (progn
a1506d29 1620 (setq command
3af0304a 1621 ;; replace # and % with the previous/current file
7eb605c7
MK
1622 (ex-expand-filsyms
1623 (concat (shell-quote-argument ex-file) ex-cmdfile-args)
1624 (current-buffer)))
52fa07ba
MK
1625 (shell-command command t))
1626 (insert-file-contents ex-file)))
328b4b70 1627 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args))
a1506d29 1628
52fa07ba 1629;; this function fixes ex-history for some commands like ex-read, ex-edit
a1506d29 1630(defun ex-fixup-history (&rest args)
8626cfa2
MK
1631 (setq viper-ex-history
1632 (cons (mapconcat 'identity args " ") (cdr viper-ex-history))))
a1506d29 1633
52fa07ba
MK
1634
1635;; Ex recover from emacs \#file\#
1636(defun ex-recover ()
8626cfa2 1637 (viper-get-ex-file)
52fa07ba 1638 (if (or ex-append ex-offset)
8626cfa2 1639 (error "`recover': %s" viper-SpuriousText))
52fa07ba
MK
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
3af0304a 1655 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
52fa07ba
MK
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)
8626cfa2 1667 (if (viper-set-unread-command-events
52fa07ba
MK
1668 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1669 (progn
1670 (setq batch t)
8626cfa2 1671 (viper-set-unread-command-events ?\C-m)))
52fa07ba
MK
1672 (message ":set <Variable> [= <Value>]")
1673 (or batch (sit-for 2))
a1506d29 1674
52fa07ba
MK
1675 (while (string-match "^[ \\t\\n]*$"
1676 (setq str
1677 (completing-read ":set " ex-variable-alist)))
e36a387d 1678 (message ":set <Variable> [= <Value>]")
52fa07ba 1679 ;; if there are unread events, don't wait
8626cfa2 1680 (or (viper-set-unread-command-events "") (sit-for 2))
52fa07ba
MK
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
2eb4bdca 1692 "([ \t]*add-hook[ \t]+'viper-insert-state-hook[ \t]+'turn-on-auto-fill.*)")
52fa07ba
MK
1693 actual-lisp-cmd lisp-cmd-del-pattern
1694 val2 orig-var)
1695 (setq orig-var var)
e36a387d
MK
1696 (cond ((string= var "all")
1697 (setq ask-if-save nil
1698 set-cmd nil))
1699 ((member var '("ai" "autoindent"))
8626cfa2 1700 (setq var "viper-auto-indent"
52fa07ba
MK
1701 set-cmd "setq"
1702 ask-if-save nil
1703 val "t"))
e36a387d 1704 ((member var '("ai-g" "autoindent-global"))
8626cfa2
MK
1705 (kill-local-variable 'viper-auto-indent)
1706 (setq var "viper-auto-indent"
52fa07ba
MK
1707 set-cmd "setq-default"
1708 val "t"))
1709 ((member var '("noai" "noautoindent"))
8626cfa2 1710 (setq var "viper-auto-indent"
52fa07ba
MK
1711 ask-if-save nil
1712 val "nil"))
e36a387d 1713 ((member var '("noai-g" "noautoindent-global"))
8626cfa2
MK
1714 (kill-local-variable 'viper-auto-indent)
1715 (setq var "viper-auto-indent"
52fa07ba
MK
1716 set-cmd "setq-default"
1717 val "nil"))
1718 ((member var '("ic" "ignorecase"))
8626cfa2 1719 (setq var "viper-case-fold-search"
52fa07ba
MK
1720 val "t"))
1721 ((member var '("noic" "noignorecase"))
8626cfa2 1722 (setq var "viper-case-fold-search"
52fa07ba
MK
1723 val "nil"))
1724 ((member var '("ma" "magic"))
8626cfa2 1725 (setq var "viper-re-search"
52fa07ba 1726 val "t"))
e36a387d 1727 ((member var '("noma" "nomagic"))
8626cfa2 1728 (setq var "viper-re-search"
52fa07ba
MK
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"))
a5254f37 1743 (setq var "viper-search-wrap-around"
52fa07ba
MK
1744 val "t"))
1745 ((member var '("nows" "nowrapscan"))
a5254f37 1746 (setq var "viper-search-wrap-around"
52fa07ba 1747 val "nil")))
e36a387d 1748 (if (and set-cmd (eq val 0)) ; value must be set by the user
52fa07ba
MK
1749 (let ((cursor-in-echo-area t))
1750 (message ":set %s = <Value>" var)
1751 ;; if there are unread events, don't wait
8626cfa2 1752 (or (viper-set-unread-command-events "") (sit-for 2))
52fa07ba
MK
1753 (setq val (read-string (format ":set %s = " var)))
1754 (ex-fixup-history "set" orig-var val)
a1506d29 1755
52fa07ba
MK
1756 ;; check numerical values
1757 (if (member var
1758 '("sw" "shiftwidth"
1759 "ts" "tabstop"
e36a387d 1760 "ts-g" "tabstop-global"
a1506d29 1761 "wm" "wrapmargin"))
52fa07ba
MK
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))))
a1506d29 1767
52fa07ba
MK
1768 (cond
1769 ((member var '("sw" "shiftwidth"))
8626cfa2 1770 (setq var "viper-shift-width"))
52fa07ba
MK
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))
e36a387d 1776 ((member var '("ts-g" "tabstop-global"))
52fa07ba
MK
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
a1506d29
JB
1782 (kill-local-variable 'fill-column)
1783 (setq var "fill-column"
52fa07ba
MK
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))
a1506d29 1790
e36a387d
MK
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)))
a1506d29 1797
52fa07ba
MK
1798 (if (and ask-if-save
1799 (y-or-n-p (format "Do you want to save this setting in %s "
8626cfa2 1800 viper-custom-file-name)))
52fa07ba 1801 (progn
a1506d29 1802 (viper-save-string-in-file
8626cfa2 1803 actual-lisp-cmd viper-custom-file-name
52fa07ba
MK
1804 ;; del pattern
1805 lisp-cmd-del-pattern)
1806 (if (string= var "fill-column")
1807 (if (> val2 0)
8626cfa2 1808 (viper-save-string-in-file
52fa07ba 1809 (concat
2eb4bdca 1810 "(add-hook 'viper-insert-state-hook 'turn-on-auto-fill) "
52fa07ba 1811 auto-cmd-label)
8626cfa2 1812 viper-custom-file-name
52fa07ba 1813 delete-turn-on-auto-fill-pattern)
8626cfa2
MK
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
52fa07ba
MK
1818 ;; del pattern
1819 lisp-cmd-del-pattern)
1820 ))
1821 ))
a1506d29 1822
e36a387d
MK
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))
52fa07ba 1836 ))
6c2e12f4 1837
52fa07ba
MK
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
a1506d29 1843 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 1844 (set-buffer viper-ex-work-buf)
52fa07ba
MK
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)
8626cfa2 1860 (viper-forward-char-carefully)))
52fa07ba
MK
1861 (goto-char end)
1862 (buffer-substring beg end))))
1863
1864
1865;; Ex shell command
1866(defun ex-shell ()
1867 (shell))
a1506d29 1868
3af0304a 1869;; Viper help. Invokes Info
52fa07ba
MK
1870(defun ex-help ()
1871 (condition-case nil
1872 (progn
1873 (pop-to-buffer (get-buffer-create "*info*"))
e83d1fe8 1874 (info (if (featurep 'xemacs) "viper.info" "viper"))
52fa07ba
MK
1875 (message "Type `i' to search for a specific topic"))
1876 (error (beep 1)
8626cfa2 1877 (with-output-to-temp-buffer " *viper-info*"
52fa07ba
MK
1878 (princ (format "
1879The Info file for Viper does not seem to be installed.
1880
1881This file is part of the standard distribution of %sEmacs.
1882Please contact your system administrator. "
e83d1fe8 1883 (if (featurep 'xemacs) "X" "")
52fa07ba
MK
1884 ))))))
1885
3af0304a 1886;; Ex source command. Loads the file specified as argument or `~/.viper'
52fa07ba 1887(defun ex-source ()
8626cfa2 1888 (viper-get-ex-file)
52fa07ba 1889 (if (string= ex-file "")
8626cfa2 1890 (load viper-custom-file-name)
52fa07ba
MK
1891 (load ex-file)))
1892
1893;; Ex substitute command
8626cfa2 1894;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
a1506d29 1895(defun ex-substitute (&optional repeat r-flag)
52fa07ba
MK
1896 (let ((opt-g nil)
1897 (opt-c nil)
1898 (matched-pos nil)
8626cfa2 1899 (case-fold-search viper-case-fold-search)
52fa07ba 1900 delim pat repl)
8626cfa2 1901 (if repeat (setq ex-token nil) (setq delim (viper-get-ex-pat)))
52fa07ba
MK
1902 (if (null ex-token)
1903 (progn
8626cfa2 1904 (setq pat (if r-flag viper-s-string ex-reg-exp))
52fa07ba
MK
1905 (or (stringp pat)
1906 (error "No previous pattern to use in substitution"))
1907 (setq repl ex-repl
1908 delim (string-to-char pat)))
8626cfa2
MK
1909 (setq pat (if (string= ex-token "") viper-s-string ex-token))
1910 (setq viper-s-string pat
52fa07ba 1911 ex-reg-exp pat)
8626cfa2 1912 (setq delim (viper-get-ex-pat))
52fa07ba
MK
1913 (if (null ex-token)
1914 (setq ex-token ""
1915 ex-repl "")
1916 (setq repl ex-token
1917 ex-repl ex-token)))
8626cfa2 1918 (while (viper-get-ex-opt-gc delim)
52fa07ba 1919 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
8626cfa2 1920 (viper-get-ex-count)
52fa07ba
MK
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
8626cfa2 1936 (viper-enlarge-region beg end)
52fa07ba
MK
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)
2eb4bdca
MK
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)))
52fa07ba
MK
1948 (beginning-of-line)
1949 (if opt-g
1950 (progn
1951 (while (and (not (eolp))
1952 (re-search-forward pat eol-mark t))
d35bee0e
MK
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? ")))
52fa07ba 1958 (progn
d35bee0e 1959 (viper-hide-search-overlay)
52fa07ba
MK
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)
8626cfa2 1965 (viper-forward-char-carefully))
52fa07ba
MK
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)
d35bee0e
MK
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? "))))
52fa07ba 1975 (progn
d35bee0e 1976 (viper-hide-search-overlay)
52fa07ba
MK
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)))
2eb4bdca
MK
1981 ;;(end-of-line)
1982 ;;(viper-forward-char-carefully)
1983 (goto-char eol-mark)
1984 )))))
52fa07ba
MK
1985 (if matched-pos (goto-char matched-pos))
1986 (beginning-of-line)
1987 (if opt-c (message "done"))))
8f2685cb 1988
52fa07ba
MK
1989;; Ex tag command
1990(defun ex-tag ()
1991 (let (tag)
1992 (save-window-excursion
a1506d29 1993 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 1994 (set-buffer viper-ex-work-buf)
52fa07ba
MK
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))
8626cfa2 2000 (viper-change-state-to-emacs)
52fa07ba
MK
2001 (condition-case conds
2002 (progn
2003 (if (string= tag "")
2004 (find-tag ex-tag t)
2005 (find-tag-other-window ex-tag))
8626cfa2 2006 (viper-change-state-to-vi))
52fa07ba 2007 (error
8626cfa2
MK
2008 (viper-change-state-to-vi)
2009 (viper-message-conditions conds)))))
bbe6126c 2010
52fa07ba 2011;; Ex write command
3af0304a 2012;; ex-write doesn't support wildcards, because file completion is a better
a1506d29 2013;; mechanism. We also don't support # and %
3af0304a 2014;; because file history is a better mechanism.
52fa07ba 2015(defun ex-write (q-flag)
8626cfa2
MK
2016 (viper-default-ex-addresses t)
2017 (viper-get-ex-file)
9b70a748 2018 (let ((end (car ex-addresses))
a1506d29 2019 (beg (car (cdr ex-addresses)))
9b70a748 2020 (orig-buf (current-buffer))
50a07e18
MK
2021 ;;(orig-buf-file-name (buffer-file-name))
2022 ;;(orig-buf-name (buffer-name))
2023 ;;(buff-changed-p (buffer-modified-p))
52fa07ba
MK
2024 temp-buf writing-same-file region
2025 file-exists writing-whole-file)
8626cfa2 2026 (if (> beg end) (error viper-FirstAddrExceedsSecond))
52fa07ba
MK
2027 (if ex-cmdfile
2028 (progn
8626cfa2 2029 (viper-enlarge-region beg end)
a1506d29 2030 (shell-command-on-region (point) (mark t)
328b4b70 2031 (concat ex-file ex-cmdfile-args)))
52fa07ba
MK
2032 (if (and (string= ex-file "") (not (buffer-file-name)))
2033 (setq ex-file
2034 (read-file-name
3af0304a 2035 (format "Buffer %s isn't visiting any file. File to save in: "
52fa07ba 2036 (buffer-name)))))
a1506d29 2037
52fa07ba
MK
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
9b70a748
MK
2047 (concat (file-name-as-directory ex-file)
2048 (file-name-nondirectory buffer-file-name))))
a1506d29 2049
52fa07ba
MK
2050 (setq file-exists (file-exists-p ex-file)
2051 writing-same-file (string= ex-file (buffer-file-name)))
2052
2eb4bdca 2053 ;; do actual writing
52fa07ba 2054 (if (and writing-whole-file writing-same-file)
2eb4bdca 2055 ;; saving whole buffer in visited file
52fa07ba
MK
2056 (if (not (buffer-modified-p))
2057 (message "(No changes need to be saved)")
2eb4bdca 2058 (viper-maybe-checkout (current-buffer))
52fa07ba 2059 (save-buffer)
9b70a748
MK
2060 (save-restriction
2061 (widen)
2062 (ex-write-info file-exists ex-file (point-min) (point-max))
2063 ))
2eb4bdca
MK
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
3af0304a 2067 (format "File %s exists. Overwrite? " ex-file))))
2eb4bdca
MK
2068 (error "Quit"))
2069 ;; writing a region or whole buffer to non-visited file
a1506d29 2070 (unwind-protect
2eb4bdca
MK
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)
2ee00512
MK
2077 (if (featurep 'xemacs)
2078 (set-visited-file-name ex-file)
b94f99c8 2079 (set-visited-file-name ex-file 'noquery))
2eb4bdca
MK
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))
fc290d1d 2087 (setq selective-display nil)
2eb4bdca
MK
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
52fa07ba
MK
2100 (if (and (buffer-file-name) writing-same-file)
2101 (set-visited-file-modtime))
2eb4bdca 2102 ;; prevent loss of data if saving part of the buffer in visited file
a1506d29 2103 (or writing-whole-file
52fa07ba 2104 (not writing-same-file)
2eb4bdca
MK
2105 (progn
2106 (sit-for 2)
2107 (message "Warning: you have saved only part of the buffer!")
2108 (set-buffer-modified-p t)))
52fa07ba 2109 (if q-flag
1e70790f 2110 (if (< viper-expert-level 2)
52fa07ba
MK
2111 (save-buffers-kill-emacs)
2112 (kill-buffer (current-buffer))))
2113 )))
a1506d29 2114
bbe6126c 2115
52fa07ba
MK
2116(defun ex-write-info (exists file-name beg end)
2117 (message "`%s'%s %d lines, %d characters"
8626cfa2 2118 (viper-abbreviate-file-name file-name)
52fa07ba
MK
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 ()
8626cfa2
MK
2125 (viper-default-ex-addresses)
2126 (viper-get-ex-buffer)
52fa07ba 2127 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
8626cfa2 2128 (if (> beg end) (error viper-FirstAddrExceedsSecond))
52fa07ba 2129 (save-excursion
8626cfa2 2130 (viper-enlarge-region beg end)
52fa07ba
MK
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
bbe6126c 2135 (progn
52fa07ba
MK
2136 (set-mark (point))
2137 (forward-line (1- ex-count)))
2138 (set-mark end))
8626cfa2
MK
2139 (viper-enlarge-region (point) (mark t))
2140 (if ex-flag (error "`yank': %s" viper-SpuriousText))
52fa07ba 2141 (if ex-buffer
8626cfa2
MK
2142 (cond ((viper-valid-register ex-buffer '(Letter))
2143 (viper-append-to-register
52fa07ba 2144 (downcase ex-buffer) (point) (mark t)))
8626cfa2 2145 ((viper-valid-register ex-buffer)
52fa07ba 2146 (copy-to-register ex-buffer (point) (mark t) nil))
8626cfa2 2147 (t (error viper-InvalidRegister ex-buffer))))
52fa07ba
MK
2148 (copy-region-as-kill (point) (mark t)))))
2149
2150;; Execute shell command
2151(defun ex-command ()
2152 (let (command)
2153 (save-window-excursion
a1506d29 2154 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
8626cfa2 2155 (set-buffer viper-ex-work-buf)
52fa07ba
MK
2156 (skip-chars-forward " \t")
2157 (setq command (buffer-substring (point) (point-max)))
2158 (end-of-line))
3af0304a 2159 ;; replace # and % with the previous/current file
52fa07ba
MK
2160 (setq command (ex-expand-filsyms command (current-buffer)))
2161 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
8626cfa2
MK
2162 (if viper-ex-last-shell-com
2163 (setq command
2164 (concat viper-ex-last-shell-com (substring command 1)))
52fa07ba 2165 (error "No previous shell command")))
8626cfa2 2166 (setq viper-ex-last-shell-com command)
52fa07ba
MK
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))
6c2e12f4 2171 (save-excursion
52fa07ba
MK
2172 (goto-char beg)
2173 (set-mark end)
8626cfa2 2174 (viper-enlarge-region (point) (mark t))
52fa07ba
MK
2175 (shell-command-on-region (point) (mark t) command t))
2176 (goto-char beg)))))
2177
4960e757
MK
2178(defun ex-compile ()
2179 "Reads args from the command line, then runs make with the args.
2180If no args are given, then it runs the last compile command.
2181Type '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
52fa07ba
MK
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
8626cfa2 2209(defun viper-info-on-file ()
6c2e12f4 2210 (interactive)
8626cfa2
MK
2211 (let ((pos1 (viper-line-pos 'start))
2212 (pos2 (viper-line-pos 'end))
52fa07ba 2213 lines file info)
8626cfa2 2214 (setq lines (count-lines (point-min) (viper-line-pos 'end))
65efc538
MK
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]:")))
52fa07ba
MK
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)))
7b8a295e 2230 (message "%s" (concat file " " info))
52fa07ba 2231 (save-window-excursion
8626cfa2 2232 (with-output-to-temp-buffer " *viper-info*"
8e41a31c
MK
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)))
8626cfa2 2236 (kill-buffer " *viper-info*")))
fad2477b 2237 ))
6c2e12f4 2238
b9fe4732
MK
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
e36a387d
MK
2258;; display all variables set through :set
2259(defun ex-show-vars ()
8626cfa2
MK
2260 (with-output-to-temp-buffer " *viper-info*"
2261 (princ (if viper-auto-indent
e36a387d 2262 "autoindent (local)\n" "noautoindent (local)\n"))
a1506d29 2263 (princ (if (default-value 'viper-auto-indent)
e36a387d 2264 "autoindent (global) \n" "noautoindent (global) \n"))
8626cfa2
MK
2265 (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n"))
2266 (princ (if viper-re-search "magic\n" "nomagic\n"))
e36a387d
MK
2267 (princ (if buffer-read-only "readonly\n" "noreadonly\n"))
2268 (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n"))
a5254f37 2269 (princ (if viper-search-wrap-around "wrapscan\n" "nowrapscan\n"))
8626cfa2 2270 (princ (format "shiftwidth \t\t= %S\n" viper-shift-width))
e36a387d
MK
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
241d963d
MK
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
e36a387d
MK
2315
2316
2317
6c2e12f4 2318
cbee283d 2319;; arch-tag: 56b80d36-f880-4d10-bd66-85ad91a295db
60370d40 2320;;; viper-ex.el ends here