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