Update FSF's address.
[bpt/emacs.git] / lisp / emulation / viper-ex.el
CommitLineData
be010748
RS
1;;; viper-ex.el --- functions implementing the Ex commands for Viper
2
b0a2e3aa 3;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
6c2e12f4
KH
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 2, or (at your option)
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
b578f267
EN
18;; along with GNU Emacs; see the file COPYING. If not, write to the
19;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20;; Boston, MA 02111-1307, USA.
6c2e12f4
KH
21
22(require 'viper-util)
23
24;;; Variables
25
26(defconst vip-ex-work-buf-name " *ex-working-space*")
27(defconst vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name))
8f2685cb 28(defconst vip-ex-tmp-buf-name " *ex-tmp*")
6c2e12f4
KH
29
30
fad2477b 31;;; Variable completion in :set command
6c2e12f4
KH
32
33;; The list of Ex commands. Used for completing command names.
34(defconst ex-token-alist
35 '(("!") ("=") (">") ("&") ("~")
36 ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual")
37 ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend")
38 ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell")
39 ("set") ("rewind") ("recover") ("read") ("quit") ("pwd")
40 ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile")
41 ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join")
42 ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy")
43 ("chdir") ("cd") ("Buffer") ("buffer") ("args")) )
44
45;; A-list of Ex variables that can be set using the :set command.
46(defconst ex-variable-alist
47 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
48 ("tab-stop-local") ("tsl") ("tabstop") ("ts")
49 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
50 ("readonly") ("ro")
51 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
52 ("noreadonly") ("noro") ("nomagic") ("noma")
53 ("noignorecase") ("noic") ("noautoindent") ("noai")
54 ("magic") ("ma") ("ignorecase") ("ic") ("autoindent") ("ai")
55 ))
56
57
58
59;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
60(defvar ex-token nil)
61
62;; Type of token.
63;; If non-nil, gives type of address; if nil, it is a command.
64(defvar ex-token-type nil)
65
66;; List of addresses passed to Ex command
67(defvar ex-addresses nil)
68
69;; It seems that this flag is used only for `#', `print', and `list', which
70;; aren't implemented. Check later.
71(defvar ex-flag nil)
72
73;; "buffer" where Ex commands keep deleted data.
74;; In Emacs terms, this is a register.
75(defvar ex-buffer nil)
76
77;; Value of ex count.
78(defvar ex-count nil)
79
80;; Flag for global command.
81(defvar ex-g-flag nil)
82
83;; If t, global command is executed on lines not matching ex-g-pat.
84(defvar ex-g-variant nil)
85
86;; Save reg-exp used in substitute.
87(defvar ex-reg-exp nil)
88
89
90;; Replace pattern for substitute.
91(defvar ex-repl nil)
92
93;; Pattern for global command.
94(defvar ex-g-pat nil)
95
96;; `sh' doesn't seem to expand wildcards, like `*'
97(defconst ex-find-file-shell "csh"
8f2685cb
KH
98 "Shell in which to interpret wildcards. Must be csh, tcsh, or similar.
99Bourne shell doesn't seem to work here.")
6c2e12f4
KH
100(defvar ex-find-file-shell-options "-f"
101 "*Options to pass to `ex-find-file-shell'.")
102
103;; Remembers the previous Ex tag.
104(defvar ex-tag nil)
105
106;; file used by Ex commands like :r, :w, :n
107(defvar ex-file nil)
108
109;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc.
110(defvar ex-variant nil)
111
112;; Specified the offset of an Ex command, such as :read.
113(defvar ex-offset nil)
114
115;; Tells Ex that this is a w>> command.
116(defvar ex-append nil)
117
118;; File containing the shell command to be executed at Ex prompt,
119;; e.g., :r !date
120(defvar ex-cmdfile nil)
121
122;; flag used in vip-ex-read-file-name to indicate that we may be reading
123;; multiple file names. Used for :edit and :next
124(defvar vip-keep-reading-filename nil)
125
126(defconst ex-cycle-other-window t
127 "*If t, :n and :b cycles through files and buffers in other window.
128Then :N and :B cycles in the current window. If nil, this behavior is
129reversed.")
130
131(defconst ex-cycle-through-non-files nil
132 "*Cycle through *scratch* and other buffers that don't visit any file.")
133
134;; Last shell command executed with :! command.
135(defvar vip-ex-last-shell-com nil)
136
137;; Indicates if Minibuffer was exited temporarily in Ex-command.
138(defvar vip-incomplete-ex-cmd nil)
139
140;; Remembers the last ex-command prompt.
141(defvar vip-last-ex-prompt "")
142
143
144;;; Code
145
8f2685cb 146;; Check if ex-token is an initial segment of STR
6c2e12f4 147(defun vip-check-sub (str)
6c2e12f4
KH
148 (let ((length (length ex-token)))
149 (if (and (<= length (length str))
150 (string= ex-token (substring str 0 length)))
151 (setq ex-token str)
152 (setq ex-token-type 'non-command))))
153
8f2685cb 154;; Get a complete ex command
6c2e12f4 155(defun vip-get-ex-com-subr ()
6c2e12f4
KH
156 (let (case-fold-search)
157 (set-mark (point))
158 (re-search-forward "[a-zA-Z][a-zA-Z]*")
159 (setq ex-token-type 'command)
160 (setq ex-token (buffer-substring (point) (mark t)))
161 (exchange-point-and-mark)
162 (cond ((looking-at "a")
163 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
164 ((looking-at "ar") (vip-check-sub "args"))
165 (t (vip-check-sub "append"))))
166 ((looking-at "h") (vip-check-sub "help"))
167 ((looking-at "c")
168 (cond ((looking-at "cd") (vip-check-sub "cd"))
169 ((looking-at "ch") (vip-check-sub "chdir"))
170 ((looking-at "co") (vip-check-sub "copy"))
171 (t (vip-check-sub "change"))))
172 ((looking-at "d") (vip-check-sub "delete"))
173 ((looking-at "b") (vip-check-sub "buffer"))
174 ((looking-at "B") (vip-check-sub "Buffer"))
175 ((looking-at "e")
176 (if (looking-at "ex") (vip-check-sub "ex")
177 (vip-check-sub "edit")))
178 ((looking-at "f") (vip-check-sub "file"))
179 ((looking-at "g") (vip-check-sub "global"))
180 ((looking-at "i") (vip-check-sub "insert"))
181 ((looking-at "j") (vip-check-sub "join"))
182 ((looking-at "l") (vip-check-sub "list"))
183 ((looking-at "m")
184 (cond ((looking-at "map") (vip-check-sub "map"))
185 ((looking-at "mar") (vip-check-sub "mark"))
186 (t (vip-check-sub "move"))))
187 ((looking-at "k[a-z][^a-z]")
188 (setq ex-token "kmark")
189 (forward-char 1)
fad2477b
KH
190 (exchange-point-and-mark)) ; this is canceled out by another
191 ; exchange-point-and-mark at the end
6c2e12f4
KH
192 ((looking-at "k") (vip-check-sub "kmark"))
193 ((looking-at "n") (if (looking-at "nu")
194 (vip-check-sub "number")
195 (vip-check-sub "next")))
196 ((looking-at "N") (vip-check-sub "Next"))
197 ((looking-at "o") (vip-check-sub "open"))
198 ((looking-at "p")
199 (cond ((looking-at "pre") (vip-check-sub "preserve"))
200 ((looking-at "pu") (vip-check-sub "put"))
201 ((looking-at "pw") (vip-check-sub "pwd"))
202 (t (vip-check-sub "print"))))
203 ((looking-at "P") (vip-check-sub "PreviousRelatedFile"))
204 ((looking-at "R") (vip-check-sub "RelatedFile"))
205 ((looking-at "q") (vip-check-sub "quit"))
206 ((looking-at "r")
207 (cond ((looking-at "rec") (vip-check-sub "recover"))
208 ((looking-at "rew") (vip-check-sub "rewind"))
209 (t (vip-check-sub "read"))))
210 ((looking-at "s")
211 (cond ((looking-at "se") (vip-check-sub "set"))
212 ((looking-at "sh") (vip-check-sub "shell"))
213 ((looking-at "so") (vip-check-sub "source"))
214 ((looking-at "sr") (vip-check-sub "sr"))
215 ((looking-at "st") (vip-check-sub "stop"))
216 ((looking-at "sus") (vip-check-sub "suspend"))
217 ((looking-at "subm") (vip-check-sub "submitReport"))
218 (t (vip-check-sub "substitute"))))
219 ((looking-at "t")
220 (if (looking-at "ta") (vip-check-sub "tag")
221 (vip-check-sub "transfer")))
222 ((looking-at "u")
223 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
224 ((looking-at "unm") (vip-check-sub "unmap"))
225 (t (vip-check-sub "undo"))))
226 ((looking-at "v")
227 (cond ((looking-at "ve") (vip-check-sub "version"))
228 ((looking-at "vi") (vip-check-sub "visual"))
229 (t (vip-check-sub "vglobal"))))
230 ((looking-at "w")
231 (if (looking-at "wq") (vip-check-sub "wq")
232 (vip-check-sub "write")))
233 ((looking-at "W")
234 (if (looking-at "WW")
235 (vip-check-sub "WWrite")
236 (vip-check-sub "Write")))
237 ((looking-at "x") (vip-check-sub "xit"))
238 ((looking-at "y") (vip-check-sub "yank"))
239 ((looking-at "z") (vip-check-sub "z")))
240 (exchange-point-and-mark)
241 ))
242
8f2685cb
KH
243;; Get an ex-token which is either an address or a command.
244;; A token has a type, \(command, address, end-mark\), and a value
6c2e12f4 245(defun vip-get-ex-token ()
6c2e12f4
KH
246 (save-window-excursion
247 (set-buffer vip-ex-work-buf)
248 (skip-chars-forward " \t|")
249 (cond ((looking-at "#")
250 (setq ex-token-type 'command)
251 (setq ex-token (char-to-string (following-char)))
252 (forward-char 1))
253 ((looking-at "[a-z]") (vip-get-ex-com-subr))
254 ((looking-at "\\.")
255 (forward-char 1)
256 (setq ex-token-type 'dot))
257 ((looking-at "[0-9]")
258 (set-mark (point))
259 (re-search-forward "[0-9]*")
260 (setq ex-token-type
261 (cond ((eq ex-token-type 'plus) 'add-number)
262 ((eq ex-token-type 'minus) 'sub-number)
263 (t 'abs-number)))
264 (setq ex-token (string-to-int (buffer-substring (point) (mark t)))))
265 ((looking-at "\\$")
266 (forward-char 1)
267 (setq ex-token-type 'end))
268 ((looking-at "%")
269 (forward-char 1)
270 (setq ex-token-type 'whole))
271 ((looking-at "+")
272 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
273 (forward-char 1)
274 (insert "1")
275 (backward-char 1)
276 (setq ex-token-type 'plus))
277 ((looking-at "+[0-9]")
278 (forward-char 1)
279 (setq ex-token-type 'plus))
280 (t
281 (error vip-BadAddress))))
282 ((looking-at "-")
283 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
284 (forward-char 1)
285 (insert "1")
286 (backward-char 1)
287 (setq ex-token-type 'minus))
288 ((looking-at "-[0-9]")
289 (forward-char 1)
290 (setq ex-token-type 'minus))
291 (t
292 (error vip-BadAddress))))
293 ((looking-at "/")
294 (forward-char 1)
295 (set-mark (point))
296 (let ((cont t))
297 (while (and (not (eolp)) cont)
298 ;;(re-search-forward "[^/]*/")
299 (re-search-forward "[^/]*\\(/\\|\n\\)")
300 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
301 (setq cont nil))))
302 (backward-char 1)
303 (setq ex-token (buffer-substring (point) (mark t)))
304 (if (looking-at "/") (forward-char 1))
305 (setq ex-token-type 'search-forward))
306 ((looking-at "\\?")
307 (forward-char 1)
308 (set-mark (point))
309 (let ((cont t))
310 (while (and (not (eolp)) cont)
311 ;;(re-search-forward "[^\\?]*\\?")
312 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
313 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
314 (setq cont nil))
315 (backward-char 1)
316 (if (not (looking-at "\n")) (forward-char 1))))
317 (setq ex-token-type 'search-backward)
318 (setq ex-token (buffer-substring (1- (point)) (mark t))))
319 ((looking-at ",")
320 (forward-char 1)
321 (setq ex-token-type 'comma))
322 ((looking-at ";")
323 (forward-char 1)
324 (setq ex-token-type 'semi-colon))
325 ((looking-at "[!=><&~]")
326 (setq ex-token-type 'command)
327 (setq ex-token (char-to-string (following-char)))
328 (forward-char 1))
329 ((looking-at "'")
330 (setq ex-token-type 'goto-mark)
331 (forward-char 1)
332 (cond ((looking-at "'") (setq ex-token nil))
333 ((looking-at "[a-z]") (setq ex-token (following-char)))
334 (t (error "Marks are ' and a-z")))
335 (forward-char 1))
336 ((looking-at "\n")
337 (setq ex-token-type 'end-mark)
338 (setq ex-token "goto"))
339 (t
340 (error vip-BadExCommand)))))
341
342;; Reads Ex command. Tries to determine if it has to exit because command
343;; is complete or invalid. If not, keeps reading command.
344(defun ex-cmd-read-exit ()
345 (interactive)
346 (setq vip-incomplete-ex-cmd t)
347 (let ((quit-regex1 (concat
348 "\\("
349 "set[ \t]*" "\\|" "edit[ \t]*" "\\|" "[nN]ext[ \t]*"
350 "\\|" "unm[ \t]*" "\\|" "^[ \t]*rep"
351 "\\)"))
352 (quit-regex2 (concat
353 "[a-zA-Z][ \t]*"
354 "\\(" "!" "\\|" ">>"
355 "\\|" "\\+[0-9]+"
356 "\\)"
357 "*[ \t]*$"))
358 (stay-regex (concat
359 "\\("
360 "^[ \t]*$" "\\|" "[ktgjmsz][ \t]*$" "\\|" "^[ \t]*ab.*"
361 "\\|" "tr[ansfer \t]*" "\\|" "sr[ \t]*"
362 "\\|" "mo.*" "\\|" "^[ \t]*k?ma[^p]*"
363 "\\|" "^[ \t]*fi.*" "\\|" "v?gl.*" "\\|" "[vg][ \t]*$"
364 "\\|" "jo.*" "\\|" "^[ \t]*ta.*" "\\|" "^[ \t]*una.*"
365 "\\|" "^[ \t]*su.*" "\\|['`][a-z][ \t]*"
366 "\\|" "![ \t]*[a-zA-Z].*"
367 "\\)"
368 "!*")))
369
370 (save-window-excursion ;; put cursor at the end of the Ex working buffer
371 (set-buffer vip-ex-work-buf)
372 (goto-char (point-max)))
373 (cond ((vip-looking-back quit-regex1) (exit-minibuffer))
374 ((vip-looking-back stay-regex) (insert " "))
375 ((vip-looking-back quit-regex2) (exit-minibuffer))
376 (t (insert " ")))))
377
378;; complete Ex command
379(defun ex-cmd-complete ()
380 (interactive)
381 (let (save-pos dist compl-list string-to-complete completion-result)
382
383 (save-excursion
384 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
385 save-pos (point)))
386
387 (if (or (= dist 0)
388 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
389 (vip-looking-back
390 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+"))
391 ;; Preceding characters are not the ones allowed in an Ex command
392 ;; or we have typed past command name.
393 ;; Note: we didn't do parsing, so there may be surprises.
394 (if (or (vip-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
395 (vip-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
396 (looking-at "[^ \t\n\C-m]"))
397 nil
398 (with-output-to-temp-buffer "*Completions*"
399 (display-completion-list
400 (vip-alist-to-list ex-token-alist))))
401 ;; Preceding chars may be part of a command name
402 (setq string-to-complete (buffer-substring save-pos (point)))
403 (setq completion-result
404 (try-completion string-to-complete ex-token-alist))
405
fad2477b 406 (cond ((eq completion-result t) ; exact match--do nothing
6c2e12f4
KH
407 (vip-tmp-insert-at-eob " (Sole completion)"))
408 ((eq completion-result nil)
409 (vip-tmp-insert-at-eob " (No match)"))
410 (t ;; partial completion
411 (goto-char save-pos)
412 (delete-region (point) (point-max))
413 (insert completion-result)
414 (let (case-fold-search)
415 (setq compl-list
416 (vip-filter-alist (concat "^" completion-result)
417 ex-token-alist)))
418 (if (> (length compl-list) 1)
419 (with-output-to-temp-buffer "*Completions*"
420 (display-completion-list
421 (vip-alist-to-list (reverse compl-list)))))))
422 )))
423
8f2685cb
KH
424
425;; Read Ex commands
426;; Ex commands themselves are implemented in viper-ex.el
6c2e12f4 427(defun vip-ex (&optional string)
6c2e12f4
KH
428 (interactive)
429 (or string
430 (setq ex-g-flag nil
431 ex-g-variant nil))
432 (let* ((map (copy-keymap minibuffer-local-map))
433 (address nil)
434 (cont t)
435 (dot (point))
8f2685cb 436 prev-token-type com-str)
6c2e12f4
KH
437
438 (vip-add-keymap vip-ex-cmd-map map)
439
440 (setq com-str (or string (vip-read-string-with-history
441 ":"
442 nil
443 'vip-ex-history
444 (car vip-ex-history)
445 map)))
446 (save-window-excursion
447 ;; just a precaution
448 (or (vip-buffer-live-p vip-ex-work-buf)
449 (setq vip-ex-work-buf (get-buffer-create vip-ex-work-buf-name)))
450 (set-buffer vip-ex-work-buf)
451 (delete-region (point-min) (point-max))
452 (insert com-str "\n")
453 (goto-char (point-min)))
454 (setq ex-token-type nil
455 ex-addresses nil)
456 (while cont
457 (vip-get-ex-token)
458 (cond ((memq ex-token-type '(command end-mark))
459 (if address (setq ex-addresses (cons address ex-addresses)))
460 (cond ((string= ex-token "global")
461 (ex-global nil)
462 (setq cont nil))
463 ((string= ex-token "vglobal")
464 (ex-global t)
465 (setq cont nil))
466 (t
467 (vip-execute-ex-command)
468 (save-window-excursion
469 (set-buffer vip-ex-work-buf)
470 (skip-chars-forward " \t")
471 (cond ((looking-at "|")
472 (forward-char 1))
473 ((looking-at "\n")
474 (setq cont nil))
475 (t (error "`%s': %s" ex-token vip-SpuriousText)))
476 ))
477 ))
478 ((eq ex-token-type 'non-command)
479 (error (format "`%s': %s" ex-token vip-BadExCommand)))
480 ((eq ex-token-type 'whole)
8f2685cb 481 (setq address nil)
6c2e12f4 482 (setq ex-addresses
8f2685cb
KH
483 (if ex-addresses
484 (cons (point-max) ex-addresses)
485 (cons (point-max) (cons (point-min) ex-addresses)))))
6c2e12f4 486 ((eq ex-token-type 'comma)
8f2685cb
KH
487 (if (eq prev-token-type 'whole)
488 (setq address (point-min)))
6c2e12f4
KH
489 (setq ex-addresses
490 (cons (if (null address) (point) address) ex-addresses)))
491 ((eq ex-token-type 'semi-colon)
8f2685cb
KH
492 (if (eq prev-token-type 'whole)
493 (setq address (point-min)))
6c2e12f4
KH
494 (if address (setq dot address))
495 (setq ex-addresses
496 (cons (if (null address) (point) address) ex-addresses)))
497 (t (let ((ans (vip-get-ex-address-subr address dot)))
8f2685cb
KH
498 (if ans (setq address ans)))))
499 (setq prev-token-type ex-token-type))))
500
6c2e12f4 501
8f2685cb 502;; Get a regular expression and set `ex-variant', if found
6c2e12f4 503(defun vip-get-ex-pat ()
6c2e12f4
KH
504 (save-window-excursion
505 (set-buffer vip-ex-work-buf)
506 (skip-chars-forward " \t")
507 (if (looking-at "!")
508 (progn
509 (setq ex-g-variant (not ex-g-variant)
510 ex-g-flag (not ex-g-flag))
511 (forward-char 1)
512 (skip-chars-forward " \t")))
513 (let ((c (following-char)))
514 (if (string-match "[0-9A-Za-z]" (format "%c" c))
515 (error
516 "Global regexp must be inside matching non-alphanumeric chars"))
517 (if (looking-at "[^\\\\\n]")
518 (progn
519 (forward-char 1)
520 (set-mark (point))
521 (let ((cont t))
522 (while (and (not (eolp)) cont)
523 (if (not (re-search-forward (format "[^%c]*%c" c c) nil t))
524 (if (member ex-token '("global" "vglobal"))
525 (error
526 "Missing closing delimiter for global regexp")
527 (goto-char (point-max))))
528 (if (not (vip-looking-back
529 (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c)))
530 (setq cont nil))))
531 (setq ex-token
532 (if (= (mark t) (point)) ""
533 (buffer-substring (1- (point)) (mark t))))
534 (backward-char 1))
535 (setq ex-token nil))
536 c)))
537
8f2685cb 538;; get an ex command
6c2e12f4 539(defun vip-get-ex-command ()
6c2e12f4
KH
540 (save-window-excursion
541 (set-buffer vip-ex-work-buf)
542 (if (looking-at "/") (forward-char 1))
543 (skip-chars-forward " \t")
544 (cond ((looking-at "[a-z]")
545 (vip-get-ex-com-subr)
546 (if (eq ex-token-type 'non-command)
547 (error "`%s': %s" ex-token vip-BadExCommand)))
548 ((looking-at "[!=><&~]")
549 (setq ex-token (char-to-string (following-char)))
550 (forward-char 1))
551 (t (error vip-BadExCommand)))))
552
8f2685cb 553;; Get an Ex option g or c
6c2e12f4 554(defun vip-get-ex-opt-gc (c)
6c2e12f4
KH
555 (save-window-excursion
556 (set-buffer vip-ex-work-buf)
557 (if (looking-at (format "%c" c)) (forward-char 1))
558 (skip-chars-forward " \t")
559 (cond ((looking-at "g")
560 (setq ex-token "g")
561 (forward-char 1)
562 t)
563 ((looking-at "c")
564 (setq ex-token "c")
565 (forward-char 1)
566 t)
567 (t nil))))
568
8f2685cb 569;; Compute default addresses. WHOLE-FLAG means use the whole buffer
6c2e12f4 570(defun vip-default-ex-addresses (&optional whole-flag)
6c2e12f4
KH
571 (cond ((null ex-addresses)
572 (setq ex-addresses
573 (if whole-flag
574 (cons (point-max) (cons (point-min) nil))
575 (cons (point) (cons (point) nil)))))
576 ((null (cdr ex-addresses))
577 (setq ex-addresses
578 (cons (car ex-addresses) ex-addresses)))))
579
8f2685cb 580;; Get an ex-address as a marker and set ex-flag if a flag is found
6c2e12f4 581(defun vip-get-ex-address ()
6c2e12f4
KH
582 (let ((address (point-marker)) (cont t))
583 (setq ex-token "")
584 (setq ex-flag nil)
585 (while cont
586 (vip-get-ex-token)
587 (cond ((eq ex-token-type 'command)
588 (if (member ex-token '("print" "list" "#"))
589 (progn
590 (setq ex-flag t
591 cont nil))
592 (error "Address expected in this Ex command")))
593 ((eq ex-token-type 'end-mark)
594 (setq cont nil))
595 ((eq ex-token-type 'whole)
596 (error "Trailing address expected"))
597 ((eq ex-token-type 'comma)
598 (error "`%s': %s" ex-token vip-SpuriousText))
599 (t (let ((ans (vip-get-ex-address-subr address (point-marker))))
600 (if ans (setq address ans))))))
601 address))
602
8f2685cb 603;; Returns an address as a point
6c2e12f4 604(defun vip-get-ex-address-subr (old-address dot)
6c2e12f4
KH
605 (let ((address nil))
606 (if (null old-address) (setq old-address dot))
607 (cond ((eq ex-token-type 'dot)
608 (setq address dot))
609 ((eq ex-token-type 'add-number)
610 (save-excursion
611 (goto-char old-address)
612 (forward-line (if (= old-address 0) (1- ex-token) ex-token))
613 (setq address (point-marker))))
614 ((eq ex-token-type 'sub-number)
615 (save-excursion
616 (goto-char old-address)
617 (forward-line (- ex-token))
618 (setq address (point-marker))))
619 ((eq ex-token-type 'abs-number)
620 (save-excursion
621 (goto-char (point-min))
622 (if (= ex-token 0) (setq address 0)
623 (forward-line (1- ex-token))
624 (setq address (point-marker)))))
625 ((eq ex-token-type 'end)
626 (setq address (point-max-marker)))
fad2477b
KH
627 ((eq ex-token-type 'plus) t) ; do nothing
628 ((eq ex-token-type 'minus) t) ; do nothing
6c2e12f4
KH
629 ((eq ex-token-type 'search-forward)
630 (save-excursion
631 (ex-search-address t)
632 (setq address (point-marker))))
633 ((eq ex-token-type 'search-backward)
634 (save-excursion
635 (ex-search-address nil)
636 (setq address (point-marker))))
637 ((eq ex-token-type 'goto-mark)
638 (save-excursion
639 (if (null ex-token)
640 (exchange-point-and-mark)
641 (goto-char (vip-register-to-point
642 (1+ (- ex-token ?a)) 'enforce-buffer)))
643 (setq address (point-marker)))))
644 address))
645
646
8f2685cb 647;; Search pattern and set address
6c2e12f4 648(defun ex-search-address (forward)
6c2e12f4
KH
649 (if (string= ex-token "")
650 (if (null vip-s-string)
651 (error vip-NoPrevSearch)
652 (setq ex-token vip-s-string))
653 (setq vip-s-string ex-token))
654 (if forward
655 (progn
656 (forward-line 1)
657 (re-search-forward ex-token))
658 (forward-line -1)
659 (re-search-backward ex-token)))
660
8f2685cb 661;; Get a buffer name and set `ex-count' and `ex-flag' if found
6c2e12f4 662(defun vip-get-ex-buffer ()
6c2e12f4
KH
663 (setq ex-buffer nil)
664 (setq ex-count nil)
665 (setq ex-flag nil)
666 (save-window-excursion
667 (set-buffer vip-ex-work-buf)
668 (skip-chars-forward " \t")
669 (if (looking-at "[a-zA-Z]")
670 (progn
671 (setq ex-buffer (following-char))
672 (forward-char 1)
673 (skip-chars-forward " \t")))
674 (if (looking-at "[0-9]")
675 (progn
676 (set-mark (point))
677 (re-search-forward "[0-9][0-9]*")
678 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
679 (skip-chars-forward " \t")))
680 (if (looking-at "[pl#]")
681 (progn
682 (setq ex-flag t)
683 (forward-char 1)))
684 (if (not (looking-at "[\n|]"))
685 (error "`%s': %s" ex-token vip-SpuriousText))))
686
687(defun vip-get-ex-count ()
688 (setq ex-variant nil
689 ex-count nil
690 ex-flag nil)
691 (save-window-excursion
692 (set-buffer vip-ex-work-buf)
693 (skip-chars-forward " \t")
694 (if (looking-at "!")
695 (progn
696 (setq ex-variant t)
697 (forward-char 1)))
698 (skip-chars-forward " \t")
699 (if (looking-at "[0-9]")
700 (progn
701 (set-mark (point))
702 (re-search-forward "[0-9][0-9]*")
703 (setq ex-count (string-to-int (buffer-substring (point) (mark t))))
704 (skip-chars-forward " \t")))
705 (if (looking-at "[pl#]")
706 (progn
707 (setq ex-flag t)
708 (forward-char 1)))
709 (if (not (looking-at "[\n|]"))
710 (error "`%s': %s"
711 (buffer-substring (point-min) (1- (point-max))) vip-BadExCommand))))
712
8f2685cb 713;; Expand \% and \# in ex command
6c2e12f4 714(defun ex-expand-filsyms (cmd buf)
6c2e12f4
KH
715 (let (cf pf ret)
716 (save-excursion
717 (set-buffer buf)
718 (setq cf buffer-file-name)
fad2477b 719 (setq pf (ex-next nil t))) ; this finds alternative file name
6c2e12f4
KH
720 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
721 (error "No current file to substitute for `\%'"))
722 (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd))
723 (error "No alternate file to substitute for `#'"))
724 (save-excursion
8f2685cb 725 (set-buffer (get-buffer-create vip-ex-tmp-buf-name))
fad2477b 726 (erase-buffer)
6c2e12f4
KH
727 (insert cmd)
728 (goto-char (point-min))
729 (while (re-search-forward "%\\|#" nil t)
730 (let ((data (match-data))
731 (char (buffer-substring (match-beginning 0) (match-end 0))))
732 (if (vip-looking-back (concat "\\\\" char))
733 (replace-match char)
734 (store-match-data data)
735 (if (string= char "%")
736 (replace-match cf)
737 (replace-match pf)))))
738 (end-of-line)
739 (setq ret (buffer-substring (point-min) (point)))
6c2e12f4
KH
740 (message "%s" ret))
741 ret))
742
8f2685cb 743;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found
6c2e12f4 744(defun vip-get-ex-file ()
6c2e12f4
KH
745 (let (prompt)
746 (setq ex-file nil
747 ex-variant nil
748 ex-append nil
749 ex-offset nil
750 ex-cmdfile nil)
751 (save-excursion
752 (save-window-excursion
753 (set-buffer vip-ex-work-buf)
754 (skip-chars-forward " \t")
755 (if (looking-at "!")
c6b52c46
MK
756 (if (and (not (vip-looking-back "[ \t]"))
757 ;; read doesn't have a corresponding :r! form, so ! is
758 ;; immediately interpreted as a shell command.
759 (not (string= ex-token "read")))
6c2e12f4
KH
760 (progn
761 (setq ex-variant t)
762 (forward-char 1)
763 (skip-chars-forward " \t"))
764 (setq ex-cmdfile t)
765 (forward-char 1)
766 (skip-chars-forward " \t")))
767 (if (looking-at ">>")
768 (progn
769 (setq ex-append t
770 ex-variant t)
771 (forward-char 2)
772 (skip-chars-forward " \t")))
773 (if (looking-at "+")
774 (progn
775 (forward-char 1)
776 (set-mark (point))
777 (re-search-forward "[ \t\n]")
778 (backward-char 1)
779 (setq ex-offset (buffer-substring (point) (mark t)))
780 (forward-char 1)
781 (skip-chars-forward " \t")))
782 ;; this takes care of :r, :w, etc., when they get file names
783 ;; from the history list
fad2477b 784 (if (member ex-token '("read" "write" "edit" "visual" "next"))
6c2e12f4
KH
785 (progn
786 (setq ex-file (buffer-substring (point) (1- (point-max))))
787 (setq ex-file
788 ;; For :e, match multiple non-white strings separated
789 ;; by white. For others, find the first non-white string
790 (if (string-match
791 (if (string= ex-token "edit")
792 "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*"
793 "[^ \t\n]+")
794 ex-file)
795 (progn
796 ;; if file name comes from history, don't leave
797 ;; minibuffer when the user types space
798 (setq vip-incomplete-ex-cmd nil)
799 ;; this must be the last clause in this progn
800 (substring ex-file (match-beginning 0) (match-end 0))
801 )
802 ""))
803 ;; this leaves only the command name in the work area
804 ;; file names are gone
805 (delete-region (point) (1- (point-max)))
806 ))
807 (goto-char (point-max))
808 (skip-chars-backward " \t\n")
809 (setq prompt (buffer-substring (point-min) (point)))
810 ))
811
812 (setq vip-last-ex-prompt prompt)
813
814 ;; If we just finished reading command, redisplay prompt
815 (if vip-incomplete-ex-cmd
816 (setq ex-file (vip-ex-read-file-name (format ":%s " prompt)))
817 ;; file was typed in-line
818 (setq ex-file (or ex-file "")))
819 ))
820
821
822;; Completes file name or exits minibuffer. If Ex command accepts multiple
823;; file names, arranges to re-enter the minibuffer.
824(defun vip-complete-filename-or-exit ()
825 (interactive)
826 (setq vip-keep-reading-filename t)
827 ;; don't exit if directory---ex-commands don't
828 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
829 (t (minibuffer-complete-word))))
830
831
832(defun ex-cmd-accepts-multiple-files-p (token)
833 (member token '("edit" "next" "Next")))
834
835;; If user doesn't enter anything, then "" is returned, i.e., the
836;; prompt-directory is not returned.
837(defun vip-ex-read-file-name (prompt)
838 (let* ((str "")
839 (minibuffer-local-completion-map
840 (copy-keymap minibuffer-local-completion-map))
841 beg end cont val)
842
843 (vip-add-keymap ex-read-filename-map minibuffer-local-completion-map)
844
845 (setq cont (setq vip-keep-reading-filename t))
846 (while cont
847 (setq vip-keep-reading-filename nil
848 val (read-file-name (concat prompt str) nil default-directory)
849 str (concat str (if (equal val "") "" " ")
850 val (if (equal val "") "" " ")))
851
852 ;; Only edit, next, and Next commands accept multiple files.
853 ;; vip-keep-reading-filename is set in the anonymous function that is
854 ;; bound to " " in ex-read-filename-map.
855 (setq cont (and vip-keep-reading-filename
856 (ex-cmd-accepts-multiple-files-p ex-token)))
857 )
858
fad2477b
KH
859 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
860 end (string-match "[ \t]*$" str)) ; delete trailing blanks
6c2e12f4
KH
861 (if (member ex-token '("read" "write"))
862 (if (string-match "[\t ]*!" str)
863 ;; this is actually a shell command
864 (progn
865 (setq ex-cmdfile t)
866 (setq beg (1+ beg))
867 (setq vip-last-ex-prompt (concat vip-last-ex-prompt " !")))))
868 (substring str (or beg 0) end)))
869
8f2685cb 870;; Execute ex command using the value of addresses
6c2e12f4 871(defun vip-execute-ex-command ()
6c2e12f4
KH
872 (vip-deactivate-mark)
873 (cond ((string= ex-token "args") (ex-args))
874 ((string= ex-token "copy") (ex-copy nil))
875 ((string= ex-token "cd") (ex-cd))
876 ((string= ex-token "chdir") (ex-cd))
877 ((string= ex-token "delete") (ex-delete))
878 ((string= ex-token "edit") (ex-edit))
879 ((string= ex-token "file") (vip-info-on-file))
880 ((string= ex-token "goto") (ex-goto))
881 ((string= ex-token "help") (ex-help))
882 ((string= ex-token "join") (ex-line "join"))
883 ((string= ex-token "kmark") (ex-mark))
884 ((string= ex-token "mark") (ex-mark))
885 ((string= ex-token "map") (ex-map))
886 ((string= ex-token "move") (ex-copy t))
887 ((string= ex-token "next") (ex-next ex-cycle-other-window))
888 ((string= ex-token "Next") (ex-next (not ex-cycle-other-window)))
889 ((string= ex-token "RelatedFile") (ex-next-related-buffer 1))
890 ((string= ex-token "put") (ex-put))
891 ((string= ex-token "pwd") (ex-pwd))
892 ((string= ex-token "preserve") (ex-preserve))
893 ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1))
894 ((string= ex-token "quit") (ex-quit))
895 ((string= ex-token "read") (ex-read))
896 ((string= ex-token "recover") (ex-recover))
897 ((string= ex-token "rewind") (ex-rewind))
898 ((string= ex-token "submitReport") (vip-submit-report))
899 ((string= ex-token "set") (ex-set))
900 ((string= ex-token "shell") (ex-shell))
901 ((string= ex-token "source") (ex-source))
902 ((string= ex-token "sr") (ex-substitute t t))
903 ((string= ex-token "substitute") (ex-substitute))
904 ((string= ex-token "suspend") (suspend-emacs))
905 ((string= ex-token "stop") (suspend-emacs))
906 ((string= ex-token "transfer") (ex-copy nil))
907 ((string= ex-token "buffer") (if ex-cycle-other-window
908 (vip-switch-to-buffer-other-window)
909 (vip-switch-to-buffer)))
910 ((string= ex-token "Buffer") (if ex-cycle-other-window
911 (vip-switch-to-buffer)
912 (vip-switch-to-buffer-other-window)))
913 ((string= ex-token "tag") (ex-tag))
914 ((string= ex-token "undo") (vip-undo))
915 ((string= ex-token "unmap") (ex-unmap))
916 ((string= ex-token "version") (vip-version))
917 ((string= ex-token "visual") (ex-edit))
918 ((string= ex-token "write") (ex-write nil))
919 ((string= ex-token "Write") (save-some-buffers))
920 ((string= ex-token "wq") (ex-write t))
921 ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask
922 ((string= ex-token "xit") (ex-write t))
923 ((string= ex-token "yank") (ex-yank))
924 ((string= ex-token "!") (ex-command))
925 ((string= ex-token "=") (ex-line-no))
926 ((string= ex-token ">") (ex-line "right"))
927 ((string= ex-token "<") (ex-line "left"))
928 ((string= ex-token "&") (ex-substitute t))
929 ((string= ex-token "~") (ex-substitute t t))
930 ((or (string= ex-token "append")
931 (string= ex-token "change")
932 (string= ex-token "insert")
933 (string= ex-token "open"))
934 (error
935 (format "`%s': Obsolete command, not supported by Viper"
936 ex-token)))
937 ((or (string= ex-token "abbreviate")
938 (string= ex-token "unabbreviate"))
939 (error
940 (format
8f2685cb 941 "`%s': Vi-style abbrevs are obsolete. Use the more powerful Emacs abbrevs"
6c2e12f4
KH
942 ex-token)))
943 ((or (string= ex-token "list")
944 (string= ex-token "print")
945 (string= ex-token "z")
946 (string= ex-token "#"))
947 (error
948 (format "`%s': Command not implemented in Viper" ex-token)))
949 (t (error (format "`%s': %s" ex-token vip-BadExCommand)))))
950
951(defun vip-undisplayed-files ()
952 (mapcar
953 (function
954 (lambda (b)
955 (if (null (get-buffer-window b))
956 (let ((f (buffer-file-name b)))
957 (if f f
958 (if ex-cycle-through-non-files
959 (let ((s (buffer-name b)))
960 (if (string= " " (substring s 0 1))
961 nil
962 s))
963 nil)))
964 nil)))
965 (buffer-list)))
966
967
968(defun ex-args ()
969 (let ((l (vip-undisplayed-files))
970 (args "")
971 (file-count 1))
972 (while (not (null l))
973 (if (car l)
974 (setq args (format "%s %d) %s\n" args file-count (car l))
975 file-count (1+ file-count)))
976 (setq l (cdr l)))
977 (if (string= args "")
978 (message "All files are already displayed")
979 (save-excursion
980 (save-window-excursion
981 (with-output-to-temp-buffer " *vip-info*"
982 (princ "\n\nThese files are not displayed in any window.\n")
983 (princ "\n=============\n")
984 (princ args)
985 (princ "\n=============\n")
986 (princ "\nThe numbers can be given as counts to :next. ")
987 (princ "\n\nPress any key to continue...\n\n"))
fad2477b 988 (vip-read-event))))))
6c2e12f4 989
8f2685cb 990;; Ex cd command. Default directory of this buffer changes
6c2e12f4 991(defun ex-cd ()
6c2e12f4
KH
992 (vip-get-ex-file)
993 (if (string= ex-file "")
994 (setq ex-file "~"))
995 (setq default-directory (file-name-as-directory (expand-file-name ex-file))))
996
8f2685cb 997;; Ex copy and move command. DEL-FLAG means delete
6c2e12f4 998(defun ex-copy (del-flag)
6c2e12f4
KH
999 (vip-default-ex-addresses)
1000 (let ((address (vip-get-ex-address))
1001 (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1002 (goto-char end)
1003 (save-excursion
1004 (push-mark beg t)
1005 (vip-enlarge-region (mark t) (point))
1006 (if del-flag
1007 (kill-region (point) (mark t))
1008 (copy-region-as-kill (point) (mark t)))
1009 (if ex-flag
1010 (progn
1011 (with-output-to-temp-buffer "*copy text*"
1012 (princ
1013 (if (or del-flag ex-g-flag ex-g-variant)
1014 (current-kill 0)
1015 (buffer-substring (point) (mark t)))))
1016 (condition-case nil
1017 (progn
1018 (read-string "[Hit return to continue] ")
1019 (save-excursion (kill-buffer "*copy text*")))
1020 (quit (save-excursion (kill-buffer "*copy text*"))
1021 (signal 'quit nil))))))
1022 (if (= address 0)
1023 (goto-char (point-min))
1024 (goto-char address)
1025 (forward-line 1))
1026 (insert (current-kill 0))))
1027
8f2685cb 1028;; Ex delete command
6c2e12f4 1029(defun ex-delete ()
6c2e12f4
KH
1030 (vip-default-ex-addresses)
1031 (vip-get-ex-buffer)
1032 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1033 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1034 (save-excursion
1035 (vip-enlarge-region beg end)
1036 (exchange-point-and-mark)
1037 (if ex-count
1038 (progn
1039 (set-mark (point))
1040 (forward-line (1- ex-count)))
1041 (set-mark end))
1042 (vip-enlarge-region (point) (mark t))
1043 (if ex-flag
1044 ;; show text to be deleted and ask for confirmation
1045 (progn
1046 (with-output-to-temp-buffer " *delete text*"
1047 (princ (buffer-substring (point) (mark t))))
1048 (condition-case nil
1049 (read-string "[Hit return to continue] ")
1050 (quit
1051 (save-excursion (kill-buffer " *delete text*"))
1052 (error "")))
1053 (save-excursion (kill-buffer " *delete text*")))
1054 (if ex-buffer
1055 (cond ((vip-valid-register ex-buffer '(Letter))
1056 (vip-append-to-register
1057 (downcase ex-buffer) (point) (mark t)))
1058 ((vip-valid-register ex-buffer)
1059 (copy-to-register ex-buffer (point) (mark t) nil))
1060 (t (error vip-InvalidRegister ex-buffer))))
1061 (kill-region (point) (mark t))))))
1062
1063
1064
8f2685cb
KH
1065;; Ex edit command
1066;; In Viper, `e' and `e!' behave identically. In both cases, the user is
1067;; asked if current buffer should really be discarded.
1068;; This command can take multiple file names. It replaces the current buffer
1069;; with the first file in its argument list
6c2e12f4 1070(defun ex-edit (&optional file)
6c2e12f4
KH
1071 (if (not file)
1072 (vip-get-ex-file))
1073 (cond ((and (string= ex-file "") buffer-file-name)
1074 (setq ex-file (abbreviate-file-name (buffer-file-name))))
1075 ((string= ex-file "")
1076 (error vip-NoFileSpecified)))
1077
1078 (let (msg do-edit)
1079 (if buffer-file-name
1080 (cond ((buffer-modified-p)
1081 (setq msg
c6b52c46 1082 (format "Buffer %s is modified. Discard changes? "
6c2e12f4
KH
1083 (buffer-name))
1084 do-edit t))
1085 ((not (verify-visited-file-modtime (current-buffer)))
1086 (setq msg
1087 (format "File %s changed on disk. Reread from disk? "
1088 buffer-file-name)
1089 do-edit t))
1090 (t (setq do-edit nil))))
1091
1092 (if do-edit
1093 (if (yes-or-no-p msg)
1094 (progn
1095 (set-buffer-modified-p nil)
1096 (kill-buffer (current-buffer)))
1097 (message "Buffer %s was left intact" (buffer-name))))
1098 ) ; let
1099
1100 (if (null (setq file (get-file-buffer ex-file)))
1101 (progn
1102 (ex-find-file ex-file)
1103 (vip-change-state-to-vi)
1104 (goto-char (point-min)))
1105 (switch-to-buffer file))
1106 (if ex-offset
1107 (progn
1108 (save-window-excursion
1109 (set-buffer vip-ex-work-buf)
1110 (delete-region (point-min) (point-max))
1111 (insert ex-offset "\n")
1112 (goto-char (point-min)))
1113 (goto-char (vip-get-ex-address))
1114 (beginning-of-line)))
1115 (ex-fixup-history vip-last-ex-prompt ex-file))
1116
1117;; splits the string FILESPEC into substrings separated by newlines `\012'
1118;; each line assumed to be a file name. find-file's each file thus obtained.
1119(defun ex-find-file (filespec)
8f2685cb 1120 (let (f filebuf tmp-buf status)
6c2e12f4
KH
1121 (if (string-match "[^a-zA-Z0-9_.-/]" filespec)
1122 (progn
1123 (save-excursion
8f2685cb 1124 (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
fad2477b
KH
1125 (erase-buffer)
1126 (setq status
1127 (call-process ex-find-file-shell nil t nil
1128 ex-find-file-shell-options
1129 "-c"
1130 (format "echo %s | tr ' ' '\\012'" filespec)))
6c2e12f4 1131 (goto-char (point-min))
8f2685cb 1132 ;; Issue an error, if no match.
fad2477b
KH
1133 (if (> status 0)
1134 (save-excursion
1135 (skip-chars-forward " \t\n\j")
1136 (if (looking-at "echo:")
1137 (vip-forward-word 1))
1138 (error "%S%s"
1139 filespec
1140 (buffer-substring (point) (vip-line-pos 'end)))
1141 ))
8f2685cb
KH
1142 (reverse-region (point-min) (point-max))
1143 (goto-char (point-min))
6c2e12f4 1144 (while (not (eobp))
8f2685cb
KH
1145 (setq f (buffer-substring (point) (vip-line-pos 'end)))
1146 (setq filebuf (find-file f))
1147 (set-buffer tmp-buf) ; otherwise it'll be in f.
6c2e12f4 1148 (forward-to-indentation 1))
fad2477b 1149 ))
6c2e12f4
KH
1150 (setq filebuf (find-file-noselect (setq f filespec))))
1151 (switch-to-buffer filebuf)
1152 ))
1153
8f2685cb 1154;; Ex global command
6c2e12f4 1155(defun ex-global (variant)
6c2e12f4
KH
1156 (let ((gcommand ex-token))
1157 (if (or ex-g-flag ex-g-variant)
1158 (error "`%s' within `global' is not allowed" gcommand)
1159 (if variant
1160 (setq ex-g-flag nil
1161 ex-g-variant t)
1162 (setq ex-g-flag t
1163 ex-g-variant nil)))
1164 (vip-get-ex-pat)
1165 (if (null ex-token)
1166 (error "`%s': Missing regular expression" gcommand)))
1167
1168 (if (string= ex-token "")
1169 (if (null vip-s-string)
1170 (error vip-NoPrevSearch)
1171 (setq ex-g-pat vip-s-string))
1172 (setq ex-g-pat ex-token
1173 vip-s-string ex-token))
1174 (if (null ex-addresses)
1175 (setq ex-addresses (list (point-max) (point-min)))
1176 (vip-default-ex-addresses))
1177 (let ((marks nil) (mark-count 0)
1178 com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1179 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1180 (save-excursion
1181 (vip-enlarge-region beg end)
1182 (exchange-point-and-mark)
1183 (let ((cont t) (limit (point-marker)))
1184 (exchange-point-and-mark)
1185 ;; skip the last line if empty
1186 (beginning-of-line)
1187 (if (eobp) (vip-backward-char-carefully))
1188 (while (and cont (not (bobp)) (>= (point) limit))
1189 (beginning-of-line)
1190 (set-mark (point))
1191 (end-of-line)
1192 (let ((found (re-search-backward ex-g-pat (mark t) t)))
1193 (if (or (and ex-g-flag found)
1194 (and ex-g-variant (not found)))
1195 (progn
1196 (end-of-line)
1197 (setq mark-count (1+ mark-count))
1198 (setq marks (cons (point-marker) marks)))))
1199 (beginning-of-line)
1200 (if (bobp) (setq cont nil)
1201 (forward-line -1)
1202 (end-of-line)))))
1203 (save-window-excursion
1204 (set-buffer vip-ex-work-buf)
1205 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1206 (while marks
1207 (goto-char (car marks))
1208 (vip-ex com-str)
1209 (setq mark-count (1- mark-count))
1210 (setq marks (cdr marks)))))
1211
8f2685cb 1212;; Ex goto command
6c2e12f4 1213(defun ex-goto ()
6c2e12f4
KH
1214 (if (null ex-addresses)
1215 (setq ex-addresses (cons (point) nil)))
1216 (push-mark (point) t)
1217 (goto-char (car ex-addresses))
1218 (beginning-of-line))
1219
8f2685cb 1220;; Ex line commands. COM is join, shift-right or shift-left
6c2e12f4 1221(defun ex-line (com)
6c2e12f4
KH
1222 (vip-default-ex-addresses)
1223 (vip-get-ex-count)
1224 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
1225 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1226 (save-excursion
1227 (vip-enlarge-region beg end)
1228 (exchange-point-and-mark)
1229 (if ex-count
1230 (progn
1231 (set-mark (point))
1232 (forward-line ex-count)))
1233 (if ex-flag
1234 ;; show text to be joined and ask for confirmation
1235 (progn
1236 (with-output-to-temp-buffer " *text*"
1237 (princ (buffer-substring (point) (mark t))))
1238 (condition-case nil
1239 (progn
1240 (read-string "[Hit return to continue] ")
1241 (ex-line-subr com (point) (mark t)))
1242 (quit (ding)))
1243 (save-excursion (kill-buffer " *text*")))
1244 (ex-line-subr com (point) (mark t)))
1245 (setq point (point)))
1246 (goto-char (1- point))
1247 (beginning-of-line)))
1248
1249(defun ex-line-subr (com beg end)
1250 (cond ((string= com "join")
1251 (goto-char (min beg end))
1252 (while (and (not (eobp)) (< (point) (max beg end)))
1253 (end-of-line)
1254 (if (and (<= (point) (max beg end)) (not (eobp)))
1255 (progn
1256 (forward-line 1)
1257 (delete-region (point) (1- (point)))
1258 (if (not ex-variant) (fixup-whitespace))))))
1259 ((or (string= com "right") (string= com "left"))
1260 (indent-rigidly
1261 (min beg end) (max beg end)
1262 (if (string= com "right") vip-shift-width (- vip-shift-width)))
1263 (goto-char (max beg end))
1264 (end-of-line)
1265 (vip-forward-char-carefully))))
1266
1267
8f2685cb 1268;; Ex mark command
6c2e12f4 1269(defun ex-mark ()
6c2e12f4
KH
1270 (let (char)
1271 (if (null ex-addresses)
1272 (setq ex-addresses
1273 (cons (point) nil)))
1274 (save-window-excursion
1275 (set-buffer vip-ex-work-buf)
1276 (skip-chars-forward " \t")
1277 (if (looking-at "[a-z]")
1278 (progn
1279 (setq char (following-char))
1280 (forward-char 1)
1281 (skip-chars-forward " \t")
1282 (if (not (looking-at "[\n|]"))
1283 (error "`%s': %s" ex-token vip-SpuriousText)))
1284 (error "`%s' requires a following letter" ex-token)))
1285 (save-excursion
1286 (goto-char (car ex-addresses))
1287 (point-to-register (1+ (- char ?a))))))
1288
1289
1290
1291;; Alternate file is the file next to the first one in the buffer ring
1292(defun ex-next (cycle-other-window &optional find-alt-file)
1293 (catch 'ex-edit
1294 (let (count l)
1295 (if (not find-alt-file)
1296 (progn
1297 (vip-get-ex-file)
1298 (if (or (char-or-string-p ex-offset)
fad2477b
KH
1299 (and (not (string= "" ex-file))
1300 (not (string-match "^[0-9]+$" ex-file))))
6c2e12f4
KH
1301 (progn
1302 (ex-edit t)
1303 (throw 'ex-edit nil))
1304 (setq count (string-to-int ex-file))
1305 (if (= count 0) (setq count 1))
1306 (if (< count 0) (error "Usage: `next <count>' (count >= 0)"))))
1307 (setq count 1))
1308 (setq l (vip-undisplayed-files))
1309 (while (> count 0)
1310 (while (and (not (null l)) (null (car l)))
1311 (setq l (cdr l)))
1312 (setq count (1- count))
1313 (if (> count 0)
1314 (setq l (cdr l))))
1315 (if find-alt-file (car l)
1316 (progn
1317 (if (car l)
1318 (let* ((w (if cycle-other-window
1319 (get-lru-window) (selected-window)))
1320 (b (window-buffer w)))
1321 (set-window-buffer w (get-file-buffer (car l)))
fad2477b
KH
1322 (bury-buffer b)
1323 ;; this puts "next <count>" in the ex-command history
1324 (ex-fixup-history vip-last-ex-prompt ex-file))
6c2e12f4
KH
1325 (error "Not that many undisplayed files")))))))
1326
1327
1328(defun ex-next-related-buffer (direction &optional no-recursion)
1329
1330 (vip-ring-rotate1 vip-related-files-and-buffers-ring direction)
1331
1332 (let ((file-or-buffer-name
1333 (vip-current-ring-item vip-related-files-and-buffers-ring))
1334 (old-ring vip-related-files-and-buffers-ring)
1335 (old-win (selected-window))
1336 skip-rest buf wind)
1337
1338 (or (and (ring-p vip-related-files-and-buffers-ring)
1339 (> (ring-length vip-related-files-and-buffers-ring) 0))
1340 (error "This buffer has no related files or buffers"))
1341
1342 (or (stringp file-or-buffer-name)
1343 (error
1344 "File and buffer names must be strings, %S" file-or-buffer-name))
1345
1346 (setq buf (cond ((get-buffer file-or-buffer-name))
1347 ((file-exists-p file-or-buffer-name)
1348 (find-file-noselect file-or-buffer-name))
1349 ))
1350
1351 (if (not (vip-buffer-live-p buf))
1352 (error "Didn't find buffer %S or file %S"
1353 file-or-buffer-name
1354 (abbreviate-file-name (expand-file-name file-or-buffer-name))))
1355
1356 (if (equal buf (current-buffer))
1357 (or no-recursion
1358 ;; try again
1359 (setq skip-rest t)
1360 (ex-next-related-buffer direction 'norecursion)))
1361
1362 (if skip-rest
1363 ()
1364 ;; setup buffer
1365 (if (setq wind (vip-get-visible-buffer-window buf))
1366 ()
1367 (setq wind (get-lru-window (if vip-xemacs-p nil 'visible)))
1368 (set-window-buffer wind buf))
1369
8f2685cb 1370 (if (vip-window-display-p)
6c2e12f4 1371 (progn
8f2685cb
KH
1372 (raise-frame (window-frame wind))
1373 (if (equal (window-frame wind) (window-frame old-win))
6c2e12f4
KH
1374 (save-window-excursion (select-window wind) (sit-for 1))
1375 (select-window wind)))
1376 (save-window-excursion (select-window wind) (sit-for 1)))
1377
1378 (save-excursion
1379 (set-buffer buf)
1380 (setq vip-related-files-and-buffers-ring old-ring))
1381
1382 (setq vip-local-search-start-marker (point-marker))
1383 )))
1384
1385
8f2685cb 1386;; Force auto save
6c2e12f4 1387(defun ex-preserve ()
6c2e12f4
KH
1388 (message "Autosaving all buffers that need to be saved...")
1389 (do-auto-save t))
1390
8f2685cb 1391;; Ex put
6c2e12f4 1392(defun ex-put ()
6c2e12f4
KH
1393 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1394 (vip-get-ex-buffer)
1395 (setq vip-use-register ex-buffer)
1396 (goto-char point)
1397 (if (bobp) (vip-Put-back 1) (vip-put-back 1))))
1398
8f2685cb 1399;; Ex print working directory
6c2e12f4 1400(defun ex-pwd ()
6c2e12f4
KH
1401 (message default-directory))
1402
8f2685cb 1403;; Ex quit command
6c2e12f4 1404(defun ex-quit ()
8f2685cb
KH
1405 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1406 (save-excursion
1407 (set-buffer vip-ex-work-buf)
1408 (if (looking-at "!") (forward-char 1)))
6c2e12f4
KH
1409 (if (< vip-expert-level 3)
1410 (save-buffers-kill-emacs)
1411 (kill-buffer (current-buffer))))
1412
1413
8f2685cb 1414;; Ex read command
6c2e12f4 1415(defun ex-read ()
6c2e12f4
KH
1416 (vip-get-ex-file)
1417 (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
1418 (goto-char point)
1419 (vip-add-newline-at-eob-if-necessary)
1420 (if (not (or (bobp) (eobp))) (forward-line 1))
1421 (if (and (not ex-variant) (string= ex-file ""))
1422 (progn
1423 (if (null buffer-file-name)
1424 (error vip-NoFileSpecified))
1425 (setq ex-file buffer-file-name)))
1426 (if ex-cmdfile
1427 (shell-command ex-file t)
1428 (insert-file-contents ex-file)))
1429 (ex-fixup-history vip-last-ex-prompt ex-file))
1430
1431;; this function fixes ex-history for some commands like ex-read, ex-edit
1432(defun ex-fixup-history (&rest args)
1433 (setq vip-ex-history
1434 (cons (mapconcat 'identity args " ") (cdr vip-ex-history))))
1435
1436
8f2685cb 1437;; Ex recover from emacs \#file\#
6c2e12f4 1438(defun ex-recover ()
6c2e12f4
KH
1439 (vip-get-ex-file)
1440 (if (or ex-append ex-offset)
1441 (error "`recover': %s" vip-SpuriousText))
1442 (if (string= ex-file "")
1443 (progn
1444 (if (null buffer-file-name)
1445 (error "This buffer isn't visiting any file"))
1446 (setq ex-file buffer-file-name))
1447 (setq ex-file (expand-file-name ex-file)))
1448 (if (and (not (string= ex-file (buffer-file-name)))
1449 (buffer-modified-p)
1450 (not ex-variant))
1451 (error "No write since last change \(:rec! overrides\)"))
1452 (recover-file ex-file))
1453
8f2685cb 1454;; Tell that `rewind' is obsolete and to use `:next count' instead
6c2e12f4 1455(defun ex-rewind ()
6c2e12f4
KH
1456 (message
1457 "Use `:n <count>' instead. Counts are obtained from the `:args' command"))
1458
1459
1460;; read variable name for ex-set
1461(defun ex-set-read-variable ()
1462 (let ((minibuffer-local-completion-map
1463 (copy-keymap minibuffer-local-completion-map))
1464 (cursor-in-echo-area t)
1465 str batch)
1466 (define-key
1467 minibuffer-local-completion-map " " 'minibuffer-complete-and-exit)
1468 (define-key minibuffer-local-completion-map "=" 'exit-minibuffer)
1469 (if (vip-set-unread-command-events
1470 (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m"))
1471 (progn
1472 (setq batch t)
1473 (vip-set-unread-command-events ?\C-m)))
1474 (message ":set <Variable> [= <Value>]")
1475 (or batch (sit-for 2))
1476
1477 (while (string-match "^[ \\t\\n]*$"
1478 (setq str
1479 (completing-read ":set " ex-variable-alist)))
1480 (message ":set <Variable> ")
1481 ;; if there are unread events, don't wait
1482 (or (vip-set-unread-command-events "") (sit-for 2))
1483 ) ; while
1484 str))
1485
1486
1487(defun ex-set ()
1488 (let ((var (ex-set-read-variable))
1489 (val 0)
1490 (set-cmd "setq")
1491 (ask-if-save t)
1492 (auto-cmd-label "; don't touch or else...")
1493 (delete-turn-on-auto-fill-pattern
1494 "([ \t]*add-hook[ \t]+'vip-insert-state-hooks[ \t]+'turn-on-auto-fill.*)")
1495 actual-lisp-cmd lisp-cmd-del-pattern
1496 val2 orig-var)
1497 (setq orig-var var)
1498 (cond ((member var '("ai" "autoindent"))
1499 (setq var "vip-auto-indent"
1500 val "t"))
1501 ((member var '("noai" "noautoindent"))
1502 (setq var "vip-auto-indent"
1503 val "nil"))
1504 ((member var '("ic" "ignorecase"))
1505 (setq var "vip-case-fold-search"
1506 val "t"))
1507 ((member var '("noic" "noignorecase"))
1508 (setq var "vip-case-fold-search"
1509 val "nil"))
1510 ((member var '("ma" "magic"))
1511 (setq var "vip-re-search"
1512 val "t"))
1513 ((member var '("noma" "nomagic"))
1514 (setq var "vip-re-search"
1515 val "nil"))
1516 ((member var '("ro" "readonly"))
1517 (setq var "buffer-read-only"
1518 val "t"))
1519 ((member var '("noro" "noreadonly"))
1520 (setq var "buffer-read-only"
1521 val "nil"))
1522 ((member var '("sm" "showmatch"))
1523 (setq var "blink-matching-paren"
1524 val "t"))
1525 ((member var '("nosm" "noshowmatch"))
1526 (setq var "blink-matching-paren"
1527 val "nil"))
1528 ((member var '("ws" "wrapscan"))
1529 (setq var "vip-search-wrap-around-t"
1530 val "t"))
1531 ((member var '("nows" "nowrapscan"))
1532 (setq var "vip-search-wrap-around-t"
1533 val "nil")))
fad2477b 1534 (if (eq val 0) ; value must be set by the user
6c2e12f4
KH
1535 (let ((cursor-in-echo-area t))
1536 (message (format ":set %s = <Value>" var))
1537 ;; if there are unread events, don't wait
1538 (or (vip-set-unread-command-events "") (sit-for 2))
1539 (setq val (read-string (format ":set %s = " var)))
1540 (ex-fixup-history "set" orig-var val)
1541
1542 ;; check numerical values
1543 (if (member var
1544 '("sw" "shiftwidth" "ts" "tabstop" "wm" "wrapmargin"))
1545 (condition-case nil
1546 (or (numberp (setq val2 (car (read-from-string val))))
1547 (error "%s: Invalid value, numberp, %S" var val))
1548 (error
1549 (error "%s: Invalid value, numberp, %S" var val))))
1550
1551 (cond
1552 ((member var '("sw" "shiftwidth"))
1553 (setq var "vip-shift-width"))
1554 ((member var '("ts" "tabstop"))
1555 ;; make it take effect in curr buff and new bufs
1556 (kill-local-variable 'tab-width)
1557 (setq var "tab-width"
1558 set-cmd "setq-default"))
1559 ((member var '("tsl" "tab-stop-local"))
1560 (setq var "tab-width"
1561 set-cmd "setq"
1562 ask-if-save nil))
1563 ((member var '("wm" "wrapmargin"))
1564 ;; make it take effect in curr buff and new bufs
1565 (kill-local-variable 'fill-column)
1566 (setq var "fill-column"
1567 val (format "(- (window-width) %s)" val)
1568 set-cmd "setq-default"))
1569 ((member var '("sh" "shell"))
1570 (setq var "explicit-shell-file-name"
1571 val (format "\"%s\"" val)))))
1572 (ex-fixup-history "set" orig-var))
1573
1574 (setq actual-lisp-cmd (format "\n(%s %s %s) %s"
1575 set-cmd var val auto-cmd-label))
1576 (setq lisp-cmd-del-pattern
1577 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1578 set-cmd var auto-cmd-label))
1579
1580 (if (and ask-if-save
1581 (y-or-n-p (format "Do you want to save this setting in %s "
1582 vip-custom-file-name)))
1583 (progn
1584 (vip-save-string-in-file
1585 actual-lisp-cmd vip-custom-file-name
1586 ;; del pattern
1587 lisp-cmd-del-pattern)
1588 (if (string= var "fill-column")
1589 (if (> val2 0)
1590 (vip-save-string-in-file
1591 (concat
1592 "(add-hook 'vip-insert-state-hooks 'turn-on-auto-fill) "
1593 auto-cmd-label)
1594 vip-custom-file-name
1595 delete-turn-on-auto-fill-pattern)
1596 (vip-save-string-in-file
1597 nil vip-custom-file-name delete-turn-on-auto-fill-pattern)
1598 (vip-save-string-in-file
1599 nil vip-custom-file-name
1600 ;; del pattern
1601 lisp-cmd-del-pattern)
1602 ))
1603 ))
1604
1605 (message (format "%s %s %s" set-cmd var (if (string-match "^[ \t]*$" val)
1606 (format "%S" val)
1607 val)))
1608 (eval (car (read-from-string actual-lisp-cmd)))
1609 (if (string= var "fill-column")
1610 (if (> val2 0)
1611 (auto-fill-mode 1)
1612 (auto-fill-mode -1)))
1613
1614 ))
1615
1616;; In inline args, skip regex-forw and (optionally) chars-back.
1617;; Optional 3d arg is a string that should replace ' ' to prevent its
1618;; special meaning
1619(defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1620 (save-excursion
1621 (set-buffer vip-ex-work-buf)
1622 (goto-char (point-min))
1623 (re-search-forward regex-forw nil t)
1624 (let ((beg (point))
1625 end)
1626 (goto-char (point-max))
1627 (if chars-back
1628 (skip-chars-backward chars-back)
1629 (skip-chars-backward " \t\n\C-m"))
1630 (setq end (point))
1631 ;; replace SPC with `=' to suppress the special meaning SPC has
1632 ;; in Ex commands
1633 (goto-char beg)
1634 (if replace-str
1635 (while (re-search-forward " +" nil t)
1636 (replace-match replace-str nil t)
1637 (vip-forward-char-carefully)))
1638 (goto-char end)
1639 (buffer-substring beg end))))
1640
1641
8f2685cb 1642;; Ex shell command
6c2e12f4 1643(defun ex-shell ()
6c2e12f4
KH
1644 (shell))
1645
8f2685cb 1646;; Viper help. Invokes Info
6c2e12f4 1647(defun ex-help ()
6c2e12f4
KH
1648 (condition-case nil
1649 (progn
1650 (pop-to-buffer (get-buffer-create "*info*"))
6dce1984 1651 (info "viper.info")
6c2e12f4
KH
1652 (message "Type `i' to search for a specific topic"))
1653 (error (beep 1)
1654 (with-output-to-temp-buffer " *vip-info*"
8f2685cb
KH
1655 (princ (format "
1656The Info file for Viper does not seem to be installed.
6c2e12f4 1657
8f2685cb
KH
1658This file is part of the standard distribution of %sEmacs.
1659Please contact your system administrator. "
1660 (if vip-xemacs-p "X" "")
1661 ))))))
6c2e12f4 1662
8f2685cb 1663;; Ex source command. Loads the file specified as argument or `~/.vip'
6c2e12f4 1664(defun ex-source ()
6c2e12f4
KH
1665 (vip-get-ex-file)
1666 (if (string= ex-file "")
1667 (load vip-custom-file-name)
1668 (load ex-file)))
1669
8f2685cb
KH
1670;; Ex substitute command
1671;; If REPEAT use previous regexp which is ex-reg-exp or vip-s-string
6c2e12f4 1672(defun ex-substitute (&optional repeat r-flag)
6c2e12f4
KH
1673 (let ((opt-g nil)
1674 (opt-c nil)
1675 (matched-pos nil)
1676 (case-fold-search vip-case-fold-search)
1677 delim pat repl)
1678 (if repeat (setq ex-token nil) (setq delim (vip-get-ex-pat)))
1679 (if (null ex-token)
fad2477b
KH
1680 (progn
1681 (setq pat (if r-flag vip-s-string ex-reg-exp))
1682 (or (stringp pat)
1683 (error "No previous pattern to use in substitution"))
1684 (setq repl ex-repl
1685 delim (string-to-char pat)))
6c2e12f4
KH
1686 (setq pat (if (string= ex-token "") vip-s-string ex-token))
1687 (setq vip-s-string pat
1688 ex-reg-exp pat)
1689 (setq delim (vip-get-ex-pat))
1690 (if (null ex-token)
1691 (setq ex-token ""
1692 ex-repl "")
1693 (setq repl ex-token
1694 ex-repl ex-token)))
1695 (while (vip-get-ex-opt-gc delim)
1696 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
1697 (vip-get-ex-count)
1698 (if ex-count
1699 (save-excursion
1700 (if ex-addresses (goto-char (car ex-addresses)))
1701 (set-mark (point))
1702 (forward-line (1- ex-count))
1703 (setq ex-addresses (cons (point) (cons (mark t) nil))))
1704 (if (null ex-addresses)
1705 (setq ex-addresses (cons (point) (cons (point) nil)))
1706 (if (null (cdr ex-addresses))
1707 (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
1708 ;(setq G opt-g)
1709 (let ((beg (car ex-addresses))
1710 (end (car (cdr ex-addresses)))
1711 eol-mark)
1712 (save-excursion
1713 (vip-enlarge-region beg end)
1714 (let ((limit (save-excursion
1715 (goto-char (max (point) (mark t)))
1716 (point-marker))))
1717 (goto-char (min (point) (mark t)))
1718 (while (< (point) limit)
1719 (end-of-line)
1720 (setq eol-mark (point-marker))
1721 (beginning-of-line)
1722 (if opt-g
1723 (progn
1724 (while (and (not (eolp))
1725 (re-search-forward pat eol-mark t))
1726 (if (or (not opt-c) (y-or-n-p "Replace? "))
1727 (progn
1728 (setq matched-pos (point))
1729 (if (not (stringp repl))
1730 (error "Can't perform Ex substitution: No previous replacement pattern"))
1731 (replace-match repl t t))))
1732 (end-of-line)
1733 (vip-forward-char-carefully))
1734 (if (null pat)
1735 (error
1736 "Can't repeat Ex substitution: No previous regular expression"))
1737 (if (and (re-search-forward pat eol-mark t)
1738 (or (not opt-c) (y-or-n-p "Replace? ")))
1739 (progn
1740 (setq matched-pos (point))
1741 (if (not (stringp repl))
1742 (error "Can't perform Ex substitution: No previous replacement pattern"))
1743 (replace-match repl t t)))
1744 (end-of-line)
1745 (vip-forward-char-carefully))))))
1746 (if matched-pos (goto-char matched-pos))
1747 (beginning-of-line)
1748 (if opt-c (message "done"))))
1749
8f2685cb 1750;; Ex tag command
6c2e12f4 1751(defun ex-tag ()
6c2e12f4
KH
1752 (let (tag)
1753 (save-window-excursion
1754 (set-buffer vip-ex-work-buf)
1755 (skip-chars-forward " \t")
1756 (set-mark (point))
1757 (skip-chars-forward "^ |\t\n")
1758 (setq tag (buffer-substring (mark t) (point))))
1759 (if (not (string= tag "")) (setq ex-tag tag))
1760 (vip-change-state-to-emacs)
1761 (condition-case conds
1762 (progn
1763 (if (string= tag "")
1764 (find-tag ex-tag t)
1765 (find-tag-other-window ex-tag))
1766 (vip-change-state-to-vi))
1767 (error
1768 (vip-change-state-to-vi)
1769 (vip-message-conditions conds)))))
1770
8f2685cb 1771;; Ex write command
6c2e12f4 1772(defun ex-write (q-flag)
6c2e12f4
KH
1773 (vip-default-ex-addresses t)
1774 (vip-get-ex-file)
1775 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))
1776 temp-buf writing-same-file region
1777 file-exists writing-whole-file)
1778 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1779 (if ex-cmdfile
1780 (progn
1781 (vip-enlarge-region beg end)
1782 (shell-command-on-region (point) (mark t) ex-file))
1783 (if (and (string= ex-file "") (not (buffer-file-name)))
1784 (setq ex-file
1785 (read-file-name
1786 (format "Buffer %s isn't visiting any file. File to save in: "
1787 (buffer-name)))))
1788
1789 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
1790 ex-file (if (string= ex-file "")
1791 (buffer-file-name)
c6b52c46
MK
1792 (expand-file-name ex-file)))
1793 ;; if ex-file is a directory use the file portion of the buffer file name
1794 (if (and (file-directory-p ex-file)
1795 buffer-file-name
1796 (not (file-directory-p buffer-file-name)))
1797 (setq ex-file
1798 (concat ex-file (file-name-nondirectory buffer-file-name))))
1799
1800 (setq file-exists (file-exists-p ex-file)
6c2e12f4 1801 writing-same-file (string= ex-file (buffer-file-name)))
c6b52c46 1802
6c2e12f4
KH
1803 (if (and writing-whole-file writing-same-file)
1804 (if (not (buffer-modified-p))
1805 (message "(No changes need to be saved)")
1806 (save-buffer)
1807 (ex-write-info file-exists ex-file beg end))
1808 ;; writing some other file or portion of the currents
1809 ;; file---create temp buffer for it
1810 ;; disable undo in that buffer, for efficiency
1811 (buffer-disable-undo (setq temp-buf (create-file-buffer ex-file)))
1812 (unwind-protect
1813 (save-excursion
1814 (if (and file-exists
1815 (not writing-same-file)
1816 (not (yes-or-no-p
1817 (format "File %s exists. Overwrite? " ex-file))))
1818 (error "Quit")
1819 (vip-enlarge-region beg end)
1820 (setq region (buffer-substring (point) (mark t)))
1821 (set-buffer temp-buf)
1822 (set-visited-file-name ex-file)
1823 (erase-buffer)
1824 (if (and file-exists ex-append)
1825 (insert-file-contents ex-file))
1826 (goto-char (point-max))
1827 (insert region)
1828 (save-buffer)
1829 (ex-write-info file-exists ex-file (point-min) (point-max))
1830 )
1831 (set-buffer temp-buf)
1832 (set-buffer-modified-p nil)
1833 (kill-buffer temp-buf)
c6b52c46
MK
1834 ))
1835 )
6c2e12f4
KH
1836 ;; this prevents the loss of data if writing part of the buffer
1837 (if (and (buffer-file-name) writing-same-file)
1838 (set-visited-file-modtime))
1839 (or writing-whole-file
1840 (not writing-same-file)
1841 (set-buffer-modified-p t))
1842 (if q-flag
1843 (if (< vip-expert-level 2)
1844 (save-buffers-kill-emacs)
1845 (kill-buffer (current-buffer))))
1846 )))
1847
1848
1849(defun ex-write-info (exists file-name beg end)
1850 (message "`%s'%s %d lines, %d characters"
1851 (abbreviate-file-name file-name)
1852 (if exists "" " [New file]")
1853 (count-lines beg (min (1+ end) (point-max)))
1854 (- end beg)))
1855
8f2685cb 1856;; Ex yank command
6c2e12f4 1857(defun ex-yank ()
6c2e12f4
KH
1858 (vip-default-ex-addresses)
1859 (vip-get-ex-buffer)
1860 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1861 (if (> beg end) (error vip-FirstAddrExceedsSecond))
1862 (save-excursion
1863 (vip-enlarge-region beg end)
1864 (exchange-point-and-mark)
1865 (if (or ex-g-flag ex-g-variant)
1866 (error "Can't execute `yank' within `global'"))
1867 (if ex-count
1868 (progn
1869 (set-mark (point))
1870 (forward-line (1- ex-count)))
1871 (set-mark end))
1872 (vip-enlarge-region (point) (mark t))
1873 (if ex-flag (error "`yank': %s" vip-SpuriousText))
1874 (if ex-buffer
1875 (cond ((vip-valid-register ex-buffer '(Letter))
1876 (vip-append-to-register
1877 (downcase ex-buffer) (point) (mark t)))
1878 ((vip-valid-register ex-buffer)
1879 (copy-to-register ex-buffer (point) (mark t) nil))
1880 (t (error vip-InvalidRegister ex-buffer))))
1881 (copy-region-as-kill (point) (mark t)))))
1882
8f2685cb 1883;; Execute shell command
6c2e12f4 1884(defun ex-command ()
6c2e12f4
KH
1885 (let (command)
1886 (save-window-excursion
1887 (set-buffer vip-ex-work-buf)
1888 (skip-chars-forward " \t")
1889 (setq command (buffer-substring (point) (point-max)))
1890 (end-of-line))
1891 (setq command (ex-expand-filsyms command (current-buffer)))
1892 (if (and (> (length command) 0) (string= "!" (substring command 0 1)))
1893 (if vip-ex-last-shell-com
1894 (setq command (concat vip-ex-last-shell-com (substring command 1)))
1895 (error "No previous shell command")))
1896 (setq vip-ex-last-shell-com command)
1897 (if (null ex-addresses)
1898 (shell-command command)
1899 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
1900 (if (null beg) (setq beg end))
1901 (save-excursion
1902 (goto-char beg)
1903 (set-mark end)
1904 (vip-enlarge-region (point) (mark t))
1905 (shell-command-on-region (point) (mark t) command t))
1906 (goto-char beg)))))
1907
8f2685cb 1908;; Print line number
6c2e12f4 1909(defun ex-line-no ()
6c2e12f4
KH
1910 (message "%d"
1911 (1+ (count-lines
1912 (point-min)
1913 (if (null ex-addresses) (point-max) (car ex-addresses))))))
1914
8f2685cb 1915;; Give information on the file visited by the current buffer
6c2e12f4 1916(defun vip-info-on-file ()
6c2e12f4 1917 (interactive)
fad2477b
KH
1918 (let (file info)
1919 (setq file (if (buffer-file-name)
1920 (concat (abbreviate-file-name (buffer-file-name)) ":")
1921 (concat (buffer-name) " [Not visiting any file]:"))
1922 info (format "line=%d/%d pos=%d/%d col=%d %s"
1923 (count-lines (point-min) (vip-line-pos 'end))
1924 (count-lines (point-min) (point-max))
1925 (point) (1- (point-max))
1926 (1+ (current-column))
1927 (if (buffer-modified-p) "[Modified]" "[Unchanged]")))
1928 (if (< (+ 1 (length info) (length file))
1929 (window-width (minibuffer-window)))
1930 (message (concat file " " info))
1931 (save-window-excursion
1932 (with-output-to-temp-buffer " *vip-info*"
1933 (princ (concat "\n"
1934 file "\n\n\t" info
1935 "\n\n\nPress any key to continue...\n\n")))
1936 (vip-read-event)))
1937 ))
6c2e12f4
KH
1938
1939
1940(provide 'viper-ex)
1941
1942;;; viper-ex.el ends here