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