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