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