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