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