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