comparison lisp/emulation/viper-ex.el @ 49598:0d8b17d428b5

Trailing whitepace deleted.
author Juanma Barranquero <lekktu@gmail.com>
date Tue, 04 Feb 2003 13:24:35 +0000
parents 4884a0e6069c
children 695cf19ef79e d7ddb3e565de
comparison
equal deleted inserted replaced
49597:e88404e8f2cf 49598:0d8b17d428b5
148 ("su" "substitute") 148 ("su" "substitute")
149 ("sub" "substitute") 149 ("sub" "substitute")
150 ("set" (ex-set)) 150 ("set" (ex-set))
151 ("shell" (ex-shell)) 151 ("shell" (ex-shell))
152 ("source" (ex-source)) 152 ("source" (ex-source))
153 ("stop" (suspend-emacs)) 153 ("stop" (suspend-emacs))
154 ("sr" (ex-substitute t t)) 154 ("sr" (ex-substitute t t))
155 ("submitReport" (viper-submit-report)) 155 ("submitReport" (viper-submit-report))
156 ("substitute" (ex-substitute) is-mashed) 156 ("substitute" (ex-substitute) is-mashed)
157 ("suspend" (suspend-emacs)) 157 ("suspend" (suspend-emacs))
158 ("t" "transfer") 158 ("t" "transfer")
163 ("undo" (viper-undo)) 163 ("undo" (viper-undo))
164 ("unmap" (ex-unmap)) 164 ("unmap" (ex-unmap))
165 ("v" "vglobal") 165 ("v" "vglobal")
166 ("version" (viper-version)) 166 ("version" (viper-version))
167 ("vglobal" (ex-global t) is-mashed) 167 ("vglobal" (ex-global t) is-mashed)
168 ("visual" (ex-edit)) 168 ("visual" (ex-edit))
169 ("w" "write") 169 ("w" "write")
170 ("wq" (ex-write t)) 170 ("wq" (ex-write t))
171 ("write" (ex-write nil)) 171 ("write" (ex-write nil))
172 ("xit" (ex-write t)) 172 ("xit" (ex-write t))
173 ("yank" (ex-yank)) 173 ("yank" (ex-yank))
236 result 236 result
237 )) 237 ))
238 238
239 239
240 ;; A-list of Ex variables that can be set using the :set command. 240 ;; A-list of Ex variables that can be set using the :set command.
241 (defconst ex-variable-alist 241 (defconst ex-variable-alist
242 '(("wrapscan") ("ws") ("wrapmargin") ("wm") 242 '(("wrapscan") ("ws") ("wrapmargin") ("wm")
243 ("tabstop-global") ("ts-g") ("tabstop") ("ts") 243 ("tabstop-global") ("ts-g") ("tabstop") ("ts")
244 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh") 244 ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh")
245 ("readonly") ("ro") 245 ("readonly") ("ro")
246 ("nowrapscan") ("nows") ("noshowmatch") ("nosm") 246 ("nowrapscan") ("nows") ("noshowmatch") ("nosm")
247 ("noreadonly") ("noro") ("nomagic") ("noma") 247 ("noreadonly") ("noro") ("nomagic") ("noma")
248 ("noignorecase") ("noic") 248 ("noignorecase") ("noic")
249 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai") 249 ("noautoindent-global") ("noai-g") ("noautoindent") ("noai")
250 ("magic") ("ma") ("ignorecase") ("ic") 250 ("magic") ("ma") ("ignorecase") ("ic")
251 ("autoindent-global") ("ai-g") ("autoindent") ("ai") 251 ("autoindent-global") ("ai-g") ("autoindent") ("ai")
252 ("all") 252 ("all")
253 )) 253 ))
254 254
255 255
256 256
257 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma") 257 ;; Token recognized during parsing of Ex commands (e.g., "read", "comma")
258 (defvar ex-token nil) 258 (defvar ex-token nil)
259 259
260 ;; Type of token. 260 ;; Type of token.
261 ;; If non-nil, gives type of address; if nil, it is a command. 261 ;; If non-nil, gives type of address; if nil, it is a command.
262 (defvar ex-token-type nil) 262 (defvar ex-token-type nil)
263 263
264 ;; List of addresses passed to Ex command 264 ;; List of addresses passed to Ex command
265 (defvar ex-addresses nil) 265 (defvar ex-addresses nil)
321 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name) 321 (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name)
322 "-f") ; csh: do it fast 322 "-f") ; csh: do it fast
323 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name) 323 ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name)
324 "-noprofile") ; bash: ignore .profile 324 "-noprofile") ; bash: ignore .profile
325 ))) 325 )))
326 "Options to pass to the Unix-style shell. 326 "Options to pass to the Unix-style shell.
327 Don't put `-c' here, as it is added automatically." 327 Don't put `-c' here, as it is added automatically."
328 :type '(choice (const nil) string) 328 :type '(choice (const nil) string)
329 :group 'viper-ex) 329 :group 'viper-ex)
330 330
331 (defcustom ex-compile-command "make" 331 (defcustom ex-compile-command "make"
342 ) 342 )
343 "Expand the file spec containing wildcard symbols. 343 "Expand the file spec containing wildcard symbols.
344 The default tries to set this variable to work with Unix, Windows, 344 The default tries to set this variable to work with Unix, Windows,
345 OS/2, and VMS. 345 OS/2, and VMS.
346 346
347 However, if it doesn't work right for some types of Unix shells or some OS, 347 However, if it doesn't work right for some types of Unix shells or some OS,
348 the user should supply the appropriate function and set this variable to the 348 the user should supply the appropriate function and set this variable to the
349 corresponding function symbol." 349 corresponding function symbol."
350 :type 'symbol 350 :type 'symbol
351 :group 'viper-ex) 351 :group 'viper-ex)
352 352
368 368
369 ;; File containing the shell command to be executed at Ex prompt, 369 ;; File containing the shell command to be executed at Ex prompt,
370 ;; e.g., :r !date 370 ;; e.g., :r !date
371 (defvar ex-cmdfile nil) 371 (defvar ex-cmdfile nil)
372 (defvar ex-cmdfile-args "") 372 (defvar ex-cmdfile-args "")
373 373
374 ;; flag used in viper-ex-read-file-name to indicate that we may be reading 374 ;; flag used in viper-ex-read-file-name to indicate that we may be reading
375 ;; multiple file names. Used for :edit and :next 375 ;; multiple file names. Used for :edit and :next
376 (defvar viper-keep-reading-filename nil) 376 (defvar viper-keep-reading-filename nil)
377 377
378 (defcustom ex-cycle-other-window t 378 (defcustom ex-cycle-other-window t
387 :type 'boolean 387 :type 'boolean
388 :group 'viper-ex) 388 :group 'viper-ex)
389 389
390 ;; Last shell command executed with :! command. 390 ;; Last shell command executed with :! command.
391 (defvar viper-ex-last-shell-com nil) 391 (defvar viper-ex-last-shell-com nil)
392 392
393 ;; Indicates if Minibuffer was exited temporarily in Ex-command. 393 ;; Indicates if Minibuffer was exited temporarily in Ex-command.
394 (defvar viper-incomplete-ex-cmd nil) 394 (defvar viper-incomplete-ex-cmd nil)
395 395
396 ;; Remembers the last ex-command prompt. 396 ;; Remembers the last ex-command prompt.
397 (defvar viper-last-ex-prompt "") 397 (defvar viper-last-ex-prompt "")
398 398
399 399
400 ;; Get a complete ex command 400 ;; Get a complete ex command
412 412
413 ;; Get an ex-token which is either an address or a command. 413 ;; Get an ex-token which is either an address or a command.
414 ;; A token has a type, \(command, address, end-mark\), and a value 414 ;; A token has a type, \(command, address, end-mark\), and a value
415 (defun viper-get-ex-token () 415 (defun viper-get-ex-token ()
416 (save-window-excursion 416 (save-window-excursion
417 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 417 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
418 (set-buffer viper-ex-work-buf) 418 (set-buffer viper-ex-work-buf)
419 (skip-chars-forward " \t|") 419 (skip-chars-forward " \t|")
420 (let ((case-fold-search t)) 420 (let ((case-fold-search t))
421 (cond ((looking-at "#") 421 (cond ((looking-at "#")
422 (setq ex-token-type 'command) 422 (setq ex-token-type 'command)
554 ;; w ! assumes that the next one is a shell command 554 ;; w ! assumes that the next one is a shell command
555 "\\|" "\\(w\\|wr\\|wri\\|writ.?\\)[ \t]+!" 555 "\\|" "\\(w\\|wr\\|wri\\|writ.?\\)[ \t]+!"
556 "\\|" "![ \t]*[a-zA-Z].*" 556 "\\|" "![ \t]*[a-zA-Z].*"
557 "\\)" 557 "\\)"
558 "!*"))) 558 "!*")))
559 559
560 (save-window-excursion ;; put cursor at the end of the Ex working buffer 560 (save-window-excursion ;; put cursor at the end of the Ex working buffer
561 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 561 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
562 (set-buffer viper-ex-work-buf) 562 (set-buffer viper-ex-work-buf)
563 (goto-char (point-max))) 563 (goto-char (point-max)))
564 (cond ((viper-looking-back quit-regex1) (exit-minibuffer)) 564 (cond ((viper-looking-back quit-regex1) (exit-minibuffer))
565 ((viper-looking-back stay-regex) (insert " ")) 565 ((viper-looking-back stay-regex) (insert " "))
566 ((viper-looking-back quit-regex2) (exit-minibuffer)) 566 ((viper-looking-back quit-regex2) (exit-minibuffer))
567 (t (insert " "))))) 567 (t (insert " ")))))
568 568
569 ;; complete Ex command 569 ;; complete Ex command
570 (defun ex-cmd-complete () 570 (defun ex-cmd-complete ()
571 (interactive) 571 (interactive)
572 (let (save-pos dist compl-list string-to-complete completion-result) 572 (let (save-pos dist compl-list string-to-complete completion-result)
573 573
574 (save-excursion 574 (save-excursion
575 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]") 575 (setq dist (skip-chars-backward "[a-zA-Z!=>&~]")
576 save-pos (point))) 576 save-pos (point)))
577 577
578 (if (or (= dist 0) 578 (if (or (= dist 0)
579 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)") 579 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
580 (viper-looking-back 580 (viper-looking-back
581 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+")) 581 "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*[ \t]+[a-zA-Z!=>&~]+"))
582 ;; Preceding characters are not the ones allowed in an Ex command 582 ;; Preceding characters are not the ones allowed in an Ex command
584 ;; Note: we didn't do parsing, so there can be surprises. 584 ;; Note: we didn't do parsing, so there can be surprises.
585 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*") 585 (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*")
586 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)") 586 (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)")
587 (looking-at "[^ \t\n\C-m]")) 587 (looking-at "[^ \t\n\C-m]"))
588 nil 588 nil
589 (with-output-to-temp-buffer "*Completions*" 589 (with-output-to-temp-buffer "*Completions*"
590 (display-completion-list 590 (display-completion-list
591 (viper-alist-to-list ex-token-alist)))) 591 (viper-alist-to-list ex-token-alist))))
592 ;; Preceding chars may be part of a command name 592 ;; Preceding chars may be part of a command name
593 (setq string-to-complete (buffer-substring save-pos (point))) 593 (setq string-to-complete (buffer-substring save-pos (point)))
594 (setq completion-result 594 (setq completion-result
595 (try-completion string-to-complete ex-token-alist)) 595 (try-completion string-to-complete ex-token-alist))
596 596
597 (cond ((eq completion-result t) ; exact match--do nothing 597 (cond ((eq completion-result t) ; exact match--do nothing
598 (viper-tmp-insert-at-eob " (Sole completion)")) 598 (viper-tmp-insert-at-eob " (Sole completion)"))
599 ((eq completion-result nil) 599 ((eq completion-result nil)
600 (viper-tmp-insert-at-eob " (No match)")) 600 (viper-tmp-insert-at-eob " (No match)"))
601 (t ;; partial completion 601 (t ;; partial completion
605 (let (case-fold-search) 605 (let (case-fold-search)
606 (setq compl-list 606 (setq compl-list
607 (viper-filter-alist (concat "^" completion-result) 607 (viper-filter-alist (concat "^" completion-result)
608 ex-token-alist))) 608 ex-token-alist)))
609 (if (> (length compl-list) 1) 609 (if (> (length compl-list) 1)
610 (with-output-to-temp-buffer "*Completions*" 610 (with-output-to-temp-buffer "*Completions*"
611 (display-completion-list 611 (display-completion-list
612 (viper-alist-to-list (reverse compl-list))))))) 612 (viper-alist-to-list (reverse compl-list)))))))
613 ))) 613 )))
614 614
615 615
616 ;; Read Ex commands 616 ;; Read Ex commands
617 ;; ARG is a prefix argument. If given, the ex command runs on the region 617 ;; ARG is a prefix argument. If given, the ex command runs on the region
618 ;;(without the user having to specify the address :a,b 618 ;;(without the user having to specify the address :a,b
619 ;; STRING is the command to execute. If nil, then Viper asks you to enter the 619 ;; STRING is the command to execute. If nil, then Viper asks you to enter the
620 ;; command. 620 ;; command.
621 (defun viper-ex (arg &optional string) 621 (defun viper-ex (arg &optional string)
622 (interactive "P") 622 (interactive "P")
623 (or string 623 (or string
624 (setq ex-g-flag nil 624 (setq ex-g-flag nil
625 ex-g-variant nil)) 625 ex-g-variant nil))
646 (setq reg-beg-line (1+ (count-lines (point-min) (point))) 646 (setq reg-beg-line (1+ (count-lines (point-min) (point)))
647 reg-end-line 647 reg-end-line
648 (+ reg-beg-line (count-lines reg-beg reg-end) -1))))) 648 (+ reg-beg-line (count-lines reg-beg reg-end) -1)))))
649 (if reg-beg-line 649 (if reg-beg-line
650 (setq initial-str (format "%d,%d" reg-beg-line reg-end-line))) 650 (setq initial-str (format "%d,%d" reg-beg-line reg-end-line)))
651 651
652 (setq com-str 652 (setq com-str
653 (or string (viper-read-string-with-history 653 (or string (viper-read-string-with-history
654 ":" 654 ":"
655 initial-str 655 initial-str
656 'viper-ex-history 656 'viper-ex-history
657 ;; no default when working on region 657 ;; no default when working on region
658 (if initial-str 658 (if initial-str
659 nil 659 nil
661 map 661 map
662 (if initial-str 662 (if initial-str
663 " [Type command to execute on current region]")))) 663 " [Type command to execute on current region]"))))
664 (save-window-excursion 664 (save-window-excursion
665 ;; just a precaution 665 ;; just a precaution
666 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 666 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
667 (set-buffer viper-ex-work-buf) 667 (set-buffer viper-ex-work-buf)
668 (delete-region (point-min) (point-max)) 668 (delete-region (point-min) (point-max))
669 (insert com-str "\n") 669 (insert com-str "\n")
670 (goto-char (point-min))) 670 (goto-char (point-min)))
671 (setq ex-token-type nil 671 (setq ex-token-type nil
716 (setq ex-addresses 716 (setq ex-addresses
717 (cons (if (null address) (point) address) ex-addresses))) 717 (cons (if (null address) (point) address) ex-addresses)))
718 (t (let ((ans (viper-get-ex-address-subr address dot))) 718 (t (let ((ans (viper-get-ex-address-subr address dot)))
719 (if ans (setq address ans))))) 719 (if ans (setq address ans)))))
720 (setq prev-token-type ex-token-type)))) 720 (setq prev-token-type ex-token-type))))
721 721
722 722
723 ;; Get a regular expression and set `ex-variant', if found 723 ;; Get a regular expression and set `ex-variant', if found
724 ;; Viper doesn't parse the substitution or search patterns. 724 ;; Viper doesn't parse the substitution or search patterns.
725 ;; In particular, it doesn't expand ~ into the last substitution. 725 ;; In particular, it doesn't expand ~ into the last substitution.
726 (defun viper-get-ex-pat () 726 (defun viper-get-ex-pat ()
781 c))) 781 c)))
782 782
783 ;; Get an Ex option g or c 783 ;; Get an Ex option g or c
784 (defun viper-get-ex-opt-gc (c) 784 (defun viper-get-ex-opt-gc (c)
785 (save-window-excursion 785 (save-window-excursion
786 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 786 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
787 (set-buffer viper-ex-work-buf) 787 (set-buffer viper-ex-work-buf)
788 (if (looking-at (format "%c" c)) (forward-char 1)) 788 (if (looking-at (format "%c" c)) (forward-char 1))
789 (skip-chars-forward " \t") 789 (skip-chars-forward " \t")
790 (cond ((looking-at "g") 790 (cond ((looking-at "g")
791 (setq ex-token "g") 791 (setq ex-token "g")
870 (setq address (point-marker)))) 870 (setq address (point-marker))))
871 ((eq ex-token-type 'goto-mark) 871 ((eq ex-token-type 'goto-mark)
872 (save-excursion 872 (save-excursion
873 (if (null ex-token) 873 (if (null ex-token)
874 (exchange-point-and-mark) 874 (exchange-point-and-mark)
875 (goto-char 875 (goto-char
876 (viper-register-to-point 876 (viper-register-to-point
877 (viper-int-to-char (1+ (- ex-token ?a))) 'enforce-buffer))) 877 (viper-int-to-char (1+ (- ex-token ?a))) 'enforce-buffer)))
878 (setq address (point-marker))))) 878 (setq address (point-marker)))))
879 address)) 879 address))
880 880
898 (defun viper-get-ex-buffer () 898 (defun viper-get-ex-buffer ()
899 (setq ex-buffer nil) 899 (setq ex-buffer nil)
900 (setq ex-count nil) 900 (setq ex-count nil)
901 (setq ex-flag nil) 901 (setq ex-flag nil)
902 (save-window-excursion 902 (save-window-excursion
903 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 903 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
904 (set-buffer viper-ex-work-buf) 904 (set-buffer viper-ex-work-buf)
905 (skip-chars-forward " \t") 905 (skip-chars-forward " \t")
906 (if (looking-at "[a-zA-Z]") 906 (if (looking-at "[a-zA-Z]")
907 (progn 907 (progn
908 (setq ex-buffer (following-char)) 908 (setq ex-buffer (following-char))
924 (defun viper-get-ex-count () 924 (defun viper-get-ex-count ()
925 (setq ex-variant nil 925 (setq ex-variant nil
926 ex-count nil 926 ex-count nil
927 ex-flag nil) 927 ex-flag nil)
928 (save-window-excursion 928 (save-window-excursion
929 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 929 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
930 (set-buffer viper-ex-work-buf) 930 (set-buffer viper-ex-work-buf)
931 (skip-chars-forward " \t") 931 (skip-chars-forward " \t")
932 (if (looking-at "!") 932 (if (looking-at "!")
933 (progn 933 (progn
934 (setq ex-variant t) 934 (setq ex-variant t)
950 (point-min) (1- (point-max))) viper-BadExCommand)))) 950 (point-min) (1- (point-max))) viper-BadExCommand))))
951 951
952 ;; Expand \% and \# in ex command 952 ;; Expand \% and \# in ex command
953 (defun ex-expand-filsyms (cmd buf) 953 (defun ex-expand-filsyms (cmd buf)
954 (let (cf pf ret) 954 (let (cf pf ret)
955 (save-excursion 955 (save-excursion
956 (set-buffer buf) 956 (set-buffer buf)
957 (setq cf buffer-file-name) 957 (setq cf buffer-file-name)
958 (setq pf (ex-next nil t))) ; this finds alternative file name 958 (setq pf (ex-next nil t))) ; this finds alternative file name
959 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd)) 959 (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd))
960 (error "No current file to substitute for `%%'")) 960 (error "No current file to substitute for `%%'"))
964 (set-buffer (get-buffer-create viper-ex-tmp-buf-name)) 964 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
965 (erase-buffer) 965 (erase-buffer)
966 (insert cmd) 966 (insert cmd)
967 (goto-char (point-min)) 967 (goto-char (point-min))
968 (while (re-search-forward "%\\|#" nil t) 968 (while (re-search-forward "%\\|#" nil t)
969 (let ((data (match-data)) 969 (let ((data (match-data))
970 (char (buffer-substring (match-beginning 0) (match-end 0)))) 970 (char (buffer-substring (match-beginning 0) (match-end 0))))
971 (if (viper-looking-back (concat "\\\\" char)) 971 (if (viper-looking-back (concat "\\\\" char))
972 (replace-match char) 972 (replace-match char)
973 (store-match-data data) 973 (store-match-data data)
974 (if (string= char "%") 974 (if (string= char "%")
989 ex-offset nil 989 ex-offset nil
990 ex-cmdfile nil 990 ex-cmdfile nil
991 ex-cmdfile-args "") 991 ex-cmdfile-args "")
992 (save-excursion 992 (save-excursion
993 (save-window-excursion 993 (save-window-excursion
994 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 994 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
995 (set-buffer viper-ex-work-buf) 995 (set-buffer viper-ex-work-buf)
996 (skip-chars-forward " \t") 996 (skip-chars-forward " \t")
997 (if (looking-at "!") 997 (if (looking-at "!")
998 (if (and (not (viper-looking-back "[ \t]")) 998 (if (and (not (viper-looking-back "[ \t]"))
999 ;; read doesn't have a corresponding :r! form, so ! is 999 ;; read doesn't have a corresponding :r! form, so ! is
1050 )) 1050 ))
1051 (goto-char (point-max)) 1051 (goto-char (point-max))
1052 (skip-chars-backward " \t\n") 1052 (skip-chars-backward " \t\n")
1053 (setq prompt (buffer-substring (point-min) (point))) 1053 (setq prompt (buffer-substring (point-min) (point)))
1054 )) 1054 ))
1055 1055
1056 (setq viper-last-ex-prompt prompt) 1056 (setq viper-last-ex-prompt prompt)
1057 1057
1058 ;; If we just finished reading command, redisplay prompt 1058 ;; If we just finished reading command, redisplay prompt
1059 (if viper-incomplete-ex-cmd 1059 (if viper-incomplete-ex-cmd
1060 (setq ex-file (viper-ex-read-file-name (format ":%s " prompt))) 1060 (setq ex-file (viper-ex-read-file-name (format ":%s " prompt)))
1061 ;; file was typed in-line 1061 ;; file was typed in-line
1062 (setq ex-file (or ex-file ""))) 1062 (setq ex-file (or ex-file "")))
1065 1065
1066 ;; Completes file name or exits minibuffer. If Ex command accepts multiple 1066 ;; Completes file name or exits minibuffer. If Ex command accepts multiple
1067 ;; file names, arranges to re-enter the minibuffer. 1067 ;; file names, arranges to re-enter the minibuffer.
1068 (defun viper-complete-filename-or-exit () 1068 (defun viper-complete-filename-or-exit ()
1069 (interactive) 1069 (interactive)
1070 (setq viper-keep-reading-filename t) 1070 (setq viper-keep-reading-filename t)
1071 ;; don't exit if directory---ex-commands don't 1071 ;; don't exit if directory---ex-commands don't
1072 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer)) 1072 (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer))
1073 ;; apparently the argument to an Ex command is 1073 ;; apparently the argument to an Ex command is
1074 ;; supposed to be a shell command 1074 ;; supposed to be a shell command
1075 ((viper-looking-back "^[ \t]*!.*") 1075 ((viper-looking-back "^[ \t]*!.*")
1076 (setq ex-cmdfile t) 1076 (setq ex-cmdfile t)
1096 (defun viper-ex-read-file-name (prompt) 1096 (defun viper-ex-read-file-name (prompt)
1097 (let* ((str "") 1097 (let* ((str "")
1098 (minibuffer-local-completion-map 1098 (minibuffer-local-completion-map
1099 (copy-keymap minibuffer-local-completion-map)) 1099 (copy-keymap minibuffer-local-completion-map))
1100 beg end cont val) 1100 beg end cont val)
1101 1101
1102 (viper-add-keymap ex-read-filename-map 1102 (viper-add-keymap ex-read-filename-map
1103 (if viper-emacs-p 1103 (if viper-emacs-p
1104 minibuffer-local-completion-map 1104 minibuffer-local-completion-map
1105 read-file-name-map)) 1105 read-file-name-map))
1106 1106
1107 (setq cont (setq viper-keep-reading-filename t)) 1107 (setq cont (setq viper-keep-reading-filename t))
1108 (while cont 1108 (while cont
1109 (setq viper-keep-reading-filename nil 1109 (setq viper-keep-reading-filename nil
1110 val (read-file-name (concat prompt str) nil default-directory)) 1110 val (read-file-name (concat prompt str) nil default-directory))
1111 (setq val (expand-file-name val)) 1111 (setq val (expand-file-name val))
1112 (if (and (string-match " " val) 1112 (if (and (string-match " " val)
1113 (ex-cmd-accepts-multiple-files-p ex-token)) 1113 (ex-cmd-accepts-multiple-files-p ex-token))
1114 (setq val (concat "\"" val "\""))) 1114 (setq val (concat "\"" val "\"")))
1115 (setq str (concat str (if (equal val "") "" " ") 1115 (setq str (concat str (if (equal val "") "" " ")
1116 val (if (equal val "") "" " "))) 1116 val (if (equal val "") "" " ")))
1117 1117
1118 ;; Only edit, next, and Next commands accept multiple files. 1118 ;; Only edit, next, and Next commands accept multiple files.
1119 ;; viper-keep-reading-filename is set in the anonymous function that is 1119 ;; viper-keep-reading-filename is set in the anonymous function that is
1120 ;; bound to " " in ex-read-filename-map. 1120 ;; bound to " " in ex-read-filename-map.
1121 (setq cont (and viper-keep-reading-filename 1121 (setq cont (and viper-keep-reading-filename
1122 (ex-cmd-accepts-multiple-files-p ex-token))) 1122 (ex-cmd-accepts-multiple-files-p ex-token)))
1123 ) 1123 )
1124 1124
1125 (setq beg (string-match "[^ \t]" str) ; delete leading blanks 1125 (setq beg (string-match "[^ \t]" str) ; delete leading blanks
1126 end (string-match "[ \t]*$" str)) ; delete trailing blanks 1126 end (string-match "[ \t]*$" str)) ; delete trailing blanks
1127 (if (member ex-token '("read" "write")) 1127 (if (member ex-token '("read" "write"))
1128 (if (string-match "[\t ]*!" str) 1128 (if (string-match "[\t ]*!" str)
1129 ;; this is actually a shell command 1129 ;; this is actually a shell command
1135 (substring str (or beg 0) end))) 1135 (substring str (or beg 0) end)))
1136 1136
1137 1137
1138 (defun viper-undisplayed-files () 1138 (defun viper-undisplayed-files ()
1139 (mapcar 1139 (mapcar
1140 (lambda (b) 1140 (lambda (b)
1141 (if (null (get-buffer-window b)) 1141 (if (null (get-buffer-window b))
1142 (let ((f (buffer-file-name b))) 1142 (let ((f (buffer-file-name b)))
1143 (if f f 1143 (if f f
1144 (if ex-cycle-through-non-files 1144 (if ex-cycle-through-non-files
1145 (let ((s (buffer-name b))) 1145 (let ((s (buffer-name b)))
1146 (if (string= " " (substring s 0 1)) 1146 (if (string= " " (substring s 0 1))
1147 nil 1147 nil
1148 s)) 1148 s))
1149 nil))) 1149 nil)))
1154 (defun ex-args () 1154 (defun ex-args ()
1155 (let ((l (viper-undisplayed-files)) 1155 (let ((l (viper-undisplayed-files))
1156 (args "") 1156 (args "")
1157 (file-count 1)) 1157 (file-count 1))
1158 (while (not (null l)) 1158 (while (not (null l))
1159 (if (car l) 1159 (if (car l)
1160 (setq args (format "%s %d) %s\n" args file-count (car l)) 1160 (setq args (format "%s %d) %s\n" args file-count (car l))
1161 file-count (1+ file-count))) 1161 file-count (1+ file-count)))
1162 (setq l (cdr l))) 1162 (setq l (cdr l)))
1163 (if (string= args "") 1163 (if (string= args "")
1164 (message "All files are already displayed") 1164 (message "All files are already displayed")
1258 (viper-get-ex-file)) 1258 (viper-get-ex-file))
1259 (cond ((and (string= ex-file "") buffer-file-name) 1259 (cond ((and (string= ex-file "") buffer-file-name)
1260 (setq ex-file (viper-abbreviate-file-name (buffer-file-name)))) 1260 (setq ex-file (viper-abbreviate-file-name (buffer-file-name))))
1261 ((string= ex-file "") 1261 ((string= ex-file "")
1262 (error viper-NoFileSpecified))) 1262 (error viper-NoFileSpecified)))
1263 1263
1264 (let (msg do-edit) 1264 (let (msg do-edit)
1265 (if buffer-file-name 1265 (if buffer-file-name
1266 (cond ((buffer-modified-p) 1266 (cond ((buffer-modified-p)
1267 (setq msg 1267 (setq msg
1268 (format "Buffer %s is modified. Discard changes? " 1268 (format "Buffer %s is modified. Discard changes? "
1272 (setq msg 1272 (setq msg
1273 (format "File %s changed on disk. Reread from disk? " 1273 (format "File %s changed on disk. Reread from disk? "
1274 buffer-file-name) 1274 buffer-file-name)
1275 do-edit t)) 1275 do-edit t))
1276 (t (setq do-edit nil)))) 1276 (t (setq do-edit nil))))
1277 1277
1278 (if do-edit 1278 (if do-edit
1279 (if (yes-or-no-p msg) 1279 (if (yes-or-no-p msg)
1280 (progn 1280 (progn
1281 (set-buffer-modified-p nil) 1281 (set-buffer-modified-p nil)
1282 (kill-buffer (current-buffer))) 1282 (kill-buffer (current-buffer)))
1283 (message "Buffer %s was left intact" (buffer-name)))) 1283 (message "Buffer %s was left intact" (buffer-name))))
1284 ) ; let 1284 ) ; let
1285 1285
1286 (if (null (setq file (get-file-buffer ex-file))) 1286 (if (null (setq file (get-file-buffer ex-file)))
1287 (progn 1287 (progn
1288 ;; this also does shell-style globbing 1288 ;; this also does shell-style globbing
1289 (ex-find-file 1289 (ex-find-file
1290 ;; replace # and % with the previous/current file 1290 ;; replace # and % with the previous/current file
1291 (ex-expand-filsyms ex-file (current-buffer))) 1291 (ex-expand-filsyms ex-file (current-buffer)))
1292 (or (eq major-mode 'dired-mode) 1292 (or (eq major-mode 'dired-mode)
1294 (goto-char (point-min))) 1294 (goto-char (point-min)))
1295 (switch-to-buffer file)) 1295 (switch-to-buffer file))
1296 (if ex-offset 1296 (if ex-offset
1297 (progn 1297 (progn
1298 (save-window-excursion 1298 (save-window-excursion
1299 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 1299 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1300 (set-buffer viper-ex-work-buf) 1300 (set-buffer viper-ex-work-buf)
1301 (delete-region (point-min) (point-max)) 1301 (delete-region (point-min) (point-max))
1302 (insert ex-offset "\n") 1302 (insert ex-offset "\n")
1303 (goto-char (point-min))) 1303 (goto-char (point-min)))
1304 (goto-char (viper-get-ex-address)) 1304 (goto-char (viper-get-ex-address))
1337 (setq ex-g-flag t 1337 (setq ex-g-flag t
1338 ex-g-variant nil))) 1338 ex-g-variant nil)))
1339 (viper-get-ex-pat) 1339 (viper-get-ex-pat)
1340 (if (null ex-token) 1340 (if (null ex-token)
1341 (error "`%s': Missing regular expression" gcommand))) 1341 (error "`%s': Missing regular expression" gcommand)))
1342 1342
1343 (if (string= ex-token "") 1343 (if (string= ex-token "")
1344 (if (null viper-s-string) 1344 (if (null viper-s-string)
1345 (error viper-NoPrevSearch) 1345 (error viper-NoPrevSearch)
1346 (setq ex-g-pat viper-s-string)) 1346 (setq ex-g-pat viper-s-string))
1347 (setq ex-g-pat ex-token 1347 (setq ex-g-pat ex-token
1377 (beginning-of-line) 1377 (beginning-of-line)
1378 (if (bobp) (setq cont nil) 1378 (if (bobp) (setq cont nil)
1379 (forward-line -1) 1379 (forward-line -1)
1380 (end-of-line))))) 1380 (end-of-line)))))
1381 (save-window-excursion 1381 (save-window-excursion
1382 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 1382 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1383 (set-buffer viper-ex-work-buf) 1383 (set-buffer viper-ex-work-buf)
1384 ;; com-str is the command string, i.e., g/pattern/ or v/pattern' 1384 ;; com-str is the command string, i.e., g/pattern/ or v/pattern'
1385 (setq com-str (buffer-substring (1+ (point)) (1- (point-max))))) 1385 (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
1386 (while ex-g-marks 1386 (while ex-g-marks
1387 (goto-char (car ex-g-marks)) 1387 (goto-char (car ex-g-marks))
1458 (if (eq 1 (length name)) 1458 (if (eq 1 (length name))
1459 (setq char (string-to-char name)) 1459 (setq char (string-to-char name))
1460 (error "`%s': Spurious text \"%s\" after mark name" 1460 (error "`%s': Spurious text \"%s\" after mark name"
1461 name (substring name 1) viper-SpuriousText)) 1461 name (substring name 1) viper-SpuriousText))
1462 (save-window-excursion 1462 (save-window-excursion
1463 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 1463 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1464 (set-buffer viper-ex-work-buf) 1464 (set-buffer viper-ex-work-buf)
1465 (skip-chars-forward " \t") 1465 (skip-chars-forward " \t")
1466 (if (looking-at "[a-z]") 1466 (if (looking-at "[a-z]")
1467 (progn 1467 (progn
1468 (setq char (following-char)) 1468 (setq char (following-char))
1473 (error "`%s' requires a following letter" ex-token)))) 1473 (error "`%s' requires a following letter" ex-token))))
1474 (save-excursion 1474 (save-excursion
1475 (goto-char (car ex-addresses)) 1475 (goto-char (car ex-addresses))
1476 (point-to-register (viper-int-to-char (1+ (- char ?a))))))) 1476 (point-to-register (viper-int-to-char (1+ (- char ?a)))))))
1477 1477
1478 1478
1479 1479
1480 ;; Alternate file is the file next to the first one in the buffer ring 1480 ;; Alternate file is the file next to the first one in the buffer ring
1481 (defun ex-next (cycle-other-window &optional find-alt-file) 1481 (defun ex-next (cycle-other-window &optional find-alt-file)
1482 (catch 'ex-edit 1482 (catch 'ex-edit
1483 (let (count l) 1483 (let (count l)
1484 (if (not find-alt-file) 1484 (if (not find-alt-file)
1485 (progn 1485 (progn
1486 (viper-get-ex-file) 1486 (viper-get-ex-file)
1487 (if (or (char-or-string-p ex-offset) 1487 (if (or (char-or-string-p ex-offset)
1488 (and (not (string= "" ex-file)) 1488 (and (not (string= "" ex-file))
1489 (not (string-match "^[0-9]+$" ex-file)))) 1489 (not (string-match "^[0-9]+$" ex-file))))
1490 (progn 1490 (progn
1491 (ex-edit t) 1491 (ex-edit t)
1492 (throw 'ex-edit nil)) 1492 (throw 'ex-edit nil))
1493 (setq count (string-to-int ex-file)) 1493 (setq count (string-to-int ex-file))
1513 (ex-fixup-history viper-last-ex-prompt ex-file)) 1513 (ex-fixup-history viper-last-ex-prompt ex-file))
1514 (error "Not that many undisplayed files"))))))) 1514 (error "Not that many undisplayed files")))))))
1515 1515
1516 1516
1517 (defun ex-next-related-buffer (direction &optional no-recursion) 1517 (defun ex-next-related-buffer (direction &optional no-recursion)
1518 1518
1519 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction) 1519 (viper-ring-rotate1 viper-related-files-and-buffers-ring direction)
1520 1520
1521 (let ((file-or-buffer-name 1521 (let ((file-or-buffer-name
1522 (viper-current-ring-item viper-related-files-and-buffers-ring)) 1522 (viper-current-ring-item viper-related-files-and-buffers-ring))
1523 (old-ring viper-related-files-and-buffers-ring) 1523 (old-ring viper-related-files-and-buffers-ring)
1524 (old-win (selected-window)) 1524 (old-win (selected-window))
1525 skip-rest buf wind) 1525 skip-rest buf wind)
1526 1526
1527 (or (and (ring-p viper-related-files-and-buffers-ring) 1527 (or (and (ring-p viper-related-files-and-buffers-ring)
1528 (> (ring-length viper-related-files-and-buffers-ring) 0)) 1528 (> (ring-length viper-related-files-and-buffers-ring) 0))
1529 (error "This buffer has no related files or buffers")) 1529 (error "This buffer has no related files or buffers"))
1530 1530
1531 (or (stringp file-or-buffer-name) 1531 (or (stringp file-or-buffer-name)
1532 (error 1532 (error
1533 "File and buffer names must be strings, %S" file-or-buffer-name)) 1533 "File and buffer names must be strings, %S" file-or-buffer-name))
1534 1534
1535 (setq buf (cond ((get-buffer file-or-buffer-name)) 1535 (setq buf (cond ((get-buffer file-or-buffer-name))
1536 ((file-exists-p file-or-buffer-name) 1536 ((file-exists-p file-or-buffer-name)
1537 (find-file-noselect file-or-buffer-name)) 1537 (find-file-noselect file-or-buffer-name))
1538 )) 1538 ))
1539 1539
1540 (if (not (viper-buffer-live-p buf)) 1540 (if (not (viper-buffer-live-p buf))
1541 (error "Didn't find buffer %S or file %S" 1541 (error "Didn't find buffer %S or file %S"
1542 file-or-buffer-name 1542 file-or-buffer-name
1543 (viper-abbreviate-file-name 1543 (viper-abbreviate-file-name
1544 (expand-file-name file-or-buffer-name)))) 1544 (expand-file-name file-or-buffer-name))))
1545 1545
1546 (if (equal buf (current-buffer)) 1546 (if (equal buf (current-buffer))
1547 (or no-recursion 1547 (or no-recursion
1548 ;; try again 1548 ;; try again
1549 (progn 1549 (progn
1550 (setq skip-rest t) 1550 (setq skip-rest t)
1551 (ex-next-related-buffer direction 'norecursion)))) 1551 (ex-next-related-buffer direction 'norecursion))))
1552 1552
1553 (if skip-rest 1553 (if skip-rest
1554 () 1554 ()
1555 ;; setup buffer 1555 ;; setup buffer
1556 (if (setq wind (viper-get-visible-buffer-window buf)) 1556 (if (setq wind (viper-get-visible-buffer-window buf))
1557 () 1557 ()
1558 (setq wind (get-lru-window (if viper-xemacs-p nil 'visible))) 1558 (setq wind (get-lru-window (if viper-xemacs-p nil 'visible)))
1559 (set-window-buffer wind buf)) 1559 (set-window-buffer wind buf))
1560 1560
1561 (if (viper-window-display-p) 1561 (if (viper-window-display-p)
1562 (progn 1562 (progn
1563 (raise-frame (window-frame wind)) 1563 (raise-frame (window-frame wind))
1564 (if (equal (window-frame wind) (window-frame old-win)) 1564 (if (equal (window-frame wind) (window-frame old-win))
1565 (save-window-excursion (select-window wind) (sit-for 1)) 1565 (save-window-excursion (select-window wind) (sit-for 1))
1566 (select-window wind))) 1566 (select-window wind)))
1567 (save-window-excursion (select-window wind) (sit-for 1))) 1567 (save-window-excursion (select-window wind) (sit-for 1)))
1568 1568
1569 (save-excursion 1569 (save-excursion
1570 (set-buffer buf) 1570 (set-buffer buf)
1571 (setq viper-related-files-and-buffers-ring old-ring)) 1571 (setq viper-related-files-and-buffers-ring old-ring))
1572 1572
1573 (setq viper-local-search-start-marker (point-marker)) 1573 (setq viper-local-search-start-marker (point-marker))
1574 ))) 1574 )))
1575 1575
1576 1576
1577 ;; Force auto save 1577 ;; Force auto save
1578 (defun ex-preserve () 1578 (defun ex-preserve ()
1579 (message "Autosaving all buffers that need to be saved...") 1579 (message "Autosaving all buffers that need to be saved...")
1580 (do-auto-save t)) 1580 (do-auto-save t))
1581 1581
1593 1593
1594 ;; Ex quit command 1594 ;; Ex quit command
1595 (defun ex-quit () 1595 (defun ex-quit ()
1596 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc. 1596 ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc.
1597 (save-excursion 1597 (save-excursion
1598 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 1598 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1599 (set-buffer viper-ex-work-buf) 1599 (set-buffer viper-ex-work-buf)
1600 (if (looking-at "!") (forward-char 1))) 1600 (if (looking-at "!") (forward-char 1)))
1601 (if (< viper-expert-level 3) 1601 (if (< viper-expert-level 3)
1602 (save-buffers-kill-emacs) 1602 (save-buffers-kill-emacs)
1603 (kill-buffer (current-buffer)))) 1603 (kill-buffer (current-buffer))))
1619 (if (null buffer-file-name) 1619 (if (null buffer-file-name)
1620 (error viper-NoFileSpecified)) 1620 (error viper-NoFileSpecified))
1621 (setq ex-file buffer-file-name))) 1621 (setq ex-file buffer-file-name)))
1622 (if ex-cmdfile 1622 (if ex-cmdfile
1623 (progn 1623 (progn
1624 (setq command 1624 (setq command
1625 ;; replace # and % with the previous/current file 1625 ;; replace # and % with the previous/current file
1626 (ex-expand-filsyms (concat ex-file ex-cmdfile-args) 1626 (ex-expand-filsyms (concat ex-file ex-cmdfile-args)
1627 (current-buffer))) 1627 (current-buffer)))
1628 (shell-command command t)) 1628 (shell-command command t))
1629 (insert-file-contents ex-file))) 1629 (insert-file-contents ex-file)))
1630 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args)) 1630 (ex-fixup-history viper-last-ex-prompt ex-file ex-cmdfile-args))
1631 1631
1632 ;; this function fixes ex-history for some commands like ex-read, ex-edit 1632 ;; this function fixes ex-history for some commands like ex-read, ex-edit
1633 (defun ex-fixup-history (&rest args) 1633 (defun ex-fixup-history (&rest args)
1634 (setq viper-ex-history 1634 (setq viper-ex-history
1635 (cons (mapconcat 'identity args " ") (cdr viper-ex-history)))) 1635 (cons (mapconcat 'identity args " ") (cdr viper-ex-history))))
1636 1636
1637 1637
1638 ;; Ex recover from emacs \#file\# 1638 ;; Ex recover from emacs \#file\#
1639 (defun ex-recover () 1639 (defun ex-recover ()
1640 (viper-get-ex-file) 1640 (viper-get-ex-file)
1641 (if (or ex-append ex-offset) 1641 (if (or ex-append ex-offset)
1672 (progn 1672 (progn
1673 (setq batch t) 1673 (setq batch t)
1674 (viper-set-unread-command-events ?\C-m))) 1674 (viper-set-unread-command-events ?\C-m)))
1675 (message ":set <Variable> [= <Value>]") 1675 (message ":set <Variable> [= <Value>]")
1676 (or batch (sit-for 2)) 1676 (or batch (sit-for 2))
1677 1677
1678 (while (string-match "^[ \\t\\n]*$" 1678 (while (string-match "^[ \\t\\n]*$"
1679 (setq str 1679 (setq str
1680 (completing-read ":set " ex-variable-alist))) 1680 (completing-read ":set " ex-variable-alist)))
1681 (message ":set <Variable> [= <Value>]") 1681 (message ":set <Variable> [= <Value>]")
1682 ;; if there are unread events, don't wait 1682 ;; if there are unread events, don't wait
1753 (message ":set %s = <Value>" var) 1753 (message ":set %s = <Value>" var)
1754 ;; if there are unread events, don't wait 1754 ;; if there are unread events, don't wait
1755 (or (viper-set-unread-command-events "") (sit-for 2)) 1755 (or (viper-set-unread-command-events "") (sit-for 2))
1756 (setq val (read-string (format ":set %s = " var))) 1756 (setq val (read-string (format ":set %s = " var)))
1757 (ex-fixup-history "set" orig-var val) 1757 (ex-fixup-history "set" orig-var val)
1758 1758
1759 ;; check numerical values 1759 ;; check numerical values
1760 (if (member var 1760 (if (member var
1761 '("sw" "shiftwidth" 1761 '("sw" "shiftwidth"
1762 "ts" "tabstop" 1762 "ts" "tabstop"
1763 "ts-g" "tabstop-global" 1763 "ts-g" "tabstop-global"
1764 "wm" "wrapmargin")) 1764 "wm" "wrapmargin"))
1765 (condition-case nil 1765 (condition-case nil
1766 (or (numberp (setq val2 (car (read-from-string val)))) 1766 (or (numberp (setq val2 (car (read-from-string val))))
1767 (error "%s: Invalid value, numberp, %S" var val)) 1767 (error "%s: Invalid value, numberp, %S" var val))
1768 (error 1768 (error
1769 (error "%s: Invalid value, numberp, %S" var val)))) 1769 (error "%s: Invalid value, numberp, %S" var val))))
1770 1770
1771 (cond 1771 (cond
1772 ((member var '("sw" "shiftwidth")) 1772 ((member var '("sw" "shiftwidth"))
1773 (setq var "viper-shift-width")) 1773 (setq var "viper-shift-width"))
1774 ((member var '("ts" "tabstop")) 1774 ((member var '("ts" "tabstop"))
1775 ;; make it take effect in curr buff and new bufs 1775 ;; make it take effect in curr buff and new bufs
1780 (kill-local-variable 'tab-width) 1780 (kill-local-variable 'tab-width)
1781 (setq var "tab-width" 1781 (setq var "tab-width"
1782 set-cmd "setq-default")) 1782 set-cmd "setq-default"))
1783 ((member var '("wm" "wrapmargin")) 1783 ((member var '("wm" "wrapmargin"))
1784 ;; make it take effect in curr buff and new bufs 1784 ;; make it take effect in curr buff and new bufs
1785 (kill-local-variable 'fill-column) 1785 (kill-local-variable 'fill-column)
1786 (setq var "fill-column" 1786 (setq var "fill-column"
1787 val (format "(- (window-width) %s)" val) 1787 val (format "(- (window-width) %s)" val)
1788 set-cmd "setq-default")) 1788 set-cmd "setq-default"))
1789 ((member var '("sh" "shell")) 1789 ((member var '("sh" "shell"))
1790 (setq var "explicit-shell-file-name" 1790 (setq var "explicit-shell-file-name"
1791 val (format "\"%s\"" val))))) 1791 val (format "\"%s\"" val)))))
1792 (ex-fixup-history "set" orig-var)) 1792 (ex-fixup-history "set" orig-var))
1793 1793
1794 (if set-cmd 1794 (if set-cmd
1795 (setq actual-lisp-cmd 1795 (setq actual-lisp-cmd
1796 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label) 1796 (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label)
1797 lisp-cmd-del-pattern 1797 lisp-cmd-del-pattern
1798 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s" 1798 (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s"
1799 set-cmd var auto-cmd-label))) 1799 set-cmd var auto-cmd-label)))
1800 1800
1801 (if (and ask-if-save 1801 (if (and ask-if-save
1802 (y-or-n-p (format "Do you want to save this setting in %s " 1802 (y-or-n-p (format "Do you want to save this setting in %s "
1803 viper-custom-file-name))) 1803 viper-custom-file-name)))
1804 (progn 1804 (progn
1805 (viper-save-string-in-file 1805 (viper-save-string-in-file
1806 actual-lisp-cmd viper-custom-file-name 1806 actual-lisp-cmd viper-custom-file-name
1807 ;; del pattern 1807 ;; del pattern
1808 lisp-cmd-del-pattern) 1808 lisp-cmd-del-pattern)
1809 (if (string= var "fill-column") 1809 (if (string= var "fill-column")
1810 (if (> val2 0) 1810 (if (> val2 0)
1820 nil viper-custom-file-name 1820 nil viper-custom-file-name
1821 ;; del pattern 1821 ;; del pattern
1822 lisp-cmd-del-pattern) 1822 lisp-cmd-del-pattern)
1823 )) 1823 ))
1824 )) 1824 ))
1825 1825
1826 (if set-cmd 1826 (if set-cmd
1827 (message "%s %s %s" 1827 (message "%s %s %s"
1828 set-cmd var 1828 set-cmd var
1829 (if (string-match "^[ \t]*$" val) 1829 (if (string-match "^[ \t]*$" val)
1830 (format "%S" val) 1830 (format "%S" val)
1841 ;; In inline args, skip regex-forw and (optionally) chars-back. 1841 ;; In inline args, skip regex-forw and (optionally) chars-back.
1842 ;; Optional 3d arg is a string that should replace ' ' to prevent its 1842 ;; Optional 3d arg is a string that should replace ' ' to prevent its
1843 ;; special meaning 1843 ;; special meaning
1844 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str) 1844 (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str)
1845 (save-excursion 1845 (save-excursion
1846 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 1846 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1847 (set-buffer viper-ex-work-buf) 1847 (set-buffer viper-ex-work-buf)
1848 (goto-char (point-min)) 1848 (goto-char (point-min))
1849 (re-search-forward regex-forw nil t) 1849 (re-search-forward regex-forw nil t)
1850 (let ((beg (point)) 1850 (let ((beg (point))
1851 end) 1851 end)
1866 1866
1867 1867
1868 ;; Ex shell command 1868 ;; Ex shell command
1869 (defun ex-shell () 1869 (defun ex-shell ()
1870 (shell)) 1870 (shell))
1871 1871
1872 ;; Viper help. Invokes Info 1872 ;; Viper help. Invokes Info
1873 (defun ex-help () 1873 (defun ex-help ()
1874 (condition-case nil 1874 (condition-case nil
1875 (progn 1875 (progn
1876 (pop-to-buffer (get-buffer-create "*info*")) 1876 (pop-to-buffer (get-buffer-create "*info*"))
1893 (load viper-custom-file-name) 1893 (load viper-custom-file-name)
1894 (load ex-file))) 1894 (load ex-file)))
1895 1895
1896 ;; Ex substitute command 1896 ;; Ex substitute command
1897 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string 1897 ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string
1898 (defun ex-substitute (&optional repeat r-flag) 1898 (defun ex-substitute (&optional repeat r-flag)
1899 (let ((opt-g nil) 1899 (let ((opt-g nil)
1900 (opt-c nil) 1900 (opt-c nil)
1901 (matched-pos nil) 1901 (matched-pos nil)
1902 (case-fold-search viper-case-fold-search) 1902 (case-fold-search viper-case-fold-search)
1903 delim pat repl) 1903 delim pat repl)
1991 1991
1992 ;; Ex tag command 1992 ;; Ex tag command
1993 (defun ex-tag () 1993 (defun ex-tag ()
1994 (let (tag) 1994 (let (tag)
1995 (save-window-excursion 1995 (save-window-excursion
1996 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 1996 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
1997 (set-buffer viper-ex-work-buf) 1997 (set-buffer viper-ex-work-buf)
1998 (skip-chars-forward " \t") 1998 (skip-chars-forward " \t")
1999 (set-mark (point)) 1999 (set-mark (point))
2000 (skip-chars-forward "^ |\t\n") 2000 (skip-chars-forward "^ |\t\n")
2001 (setq tag (buffer-substring (mark t) (point)))) 2001 (setq tag (buffer-substring (mark t) (point))))
2011 (viper-change-state-to-vi) 2011 (viper-change-state-to-vi)
2012 (viper-message-conditions conds))))) 2012 (viper-message-conditions conds)))))
2013 2013
2014 ;; Ex write command 2014 ;; Ex write command
2015 ;; ex-write doesn't support wildcards, because file completion is a better 2015 ;; ex-write doesn't support wildcards, because file completion is a better
2016 ;; mechanism. We also don't support # and % 2016 ;; mechanism. We also don't support # and %
2017 ;; because file history is a better mechanism. 2017 ;; because file history is a better mechanism.
2018 (defun ex-write (q-flag) 2018 (defun ex-write (q-flag)
2019 (viper-default-ex-addresses t) 2019 (viper-default-ex-addresses t)
2020 (viper-get-ex-file) 2020 (viper-get-ex-file)
2021 (let ((end (car ex-addresses)) 2021 (let ((end (car ex-addresses))
2022 (beg (car (cdr ex-addresses))) 2022 (beg (car (cdr ex-addresses)))
2023 (orig-buf (current-buffer)) 2023 (orig-buf (current-buffer))
2024 ;;(orig-buf-file-name (buffer-file-name)) 2024 ;;(orig-buf-file-name (buffer-file-name))
2025 ;;(orig-buf-name (buffer-name)) 2025 ;;(orig-buf-name (buffer-name))
2026 ;;(buff-changed-p (buffer-modified-p)) 2026 ;;(buff-changed-p (buffer-modified-p))
2027 temp-buf writing-same-file region 2027 temp-buf writing-same-file region
2028 file-exists writing-whole-file) 2028 file-exists writing-whole-file)
2029 (if (> beg end) (error viper-FirstAddrExceedsSecond)) 2029 (if (> beg end) (error viper-FirstAddrExceedsSecond))
2030 (if ex-cmdfile 2030 (if ex-cmdfile
2031 (progn 2031 (progn
2032 (viper-enlarge-region beg end) 2032 (viper-enlarge-region beg end)
2033 (shell-command-on-region (point) (mark t) 2033 (shell-command-on-region (point) (mark t)
2034 (concat ex-file ex-cmdfile-args))) 2034 (concat ex-file ex-cmdfile-args)))
2035 (if (and (string= ex-file "") (not (buffer-file-name))) 2035 (if (and (string= ex-file "") (not (buffer-file-name)))
2036 (setq ex-file 2036 (setq ex-file
2037 (read-file-name 2037 (read-file-name
2038 (format "Buffer %s isn't visiting any file. File to save in: " 2038 (format "Buffer %s isn't visiting any file. File to save in: "
2039 (buffer-name))))) 2039 (buffer-name)))))
2040 2040
2041 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end)) 2041 (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end))
2042 ex-file (if (string= ex-file "") 2042 ex-file (if (string= ex-file "")
2043 (buffer-file-name) 2043 (buffer-file-name)
2044 (expand-file-name ex-file))) 2044 (expand-file-name ex-file)))
2045 ;; if ex-file is a directory use the file portion of the buffer file name 2045 ;; if ex-file is a directory use the file portion of the buffer file name
2047 buffer-file-name 2047 buffer-file-name
2048 (not (file-directory-p buffer-file-name))) 2048 (not (file-directory-p buffer-file-name)))
2049 (setq ex-file 2049 (setq ex-file
2050 (concat (file-name-as-directory ex-file) 2050 (concat (file-name-as-directory ex-file)
2051 (file-name-nondirectory buffer-file-name)))) 2051 (file-name-nondirectory buffer-file-name))))
2052 2052
2053 (setq file-exists (file-exists-p ex-file) 2053 (setq file-exists (file-exists-p ex-file)
2054 writing-same-file (string= ex-file (buffer-file-name))) 2054 writing-same-file (string= ex-file (buffer-file-name)))
2055 2055
2056 ;; do actual writing 2056 ;; do actual writing
2057 (if (and writing-whole-file writing-same-file) 2057 (if (and writing-whole-file writing-same-file)
2068 (if (and file-exists (not writing-same-file) 2068 (if (and file-exists (not writing-same-file)
2069 (not (yes-or-no-p 2069 (not (yes-or-no-p
2070 (format "File %s exists. Overwrite? " ex-file)))) 2070 (format "File %s exists. Overwrite? " ex-file))))
2071 (error "Quit")) 2071 (error "Quit"))
2072 ;; writing a region or whole buffer to non-visited file 2072 ;; writing a region or whole buffer to non-visited file
2073 (unwind-protect 2073 (unwind-protect
2074 (save-excursion 2074 (save-excursion
2075 (viper-enlarge-region beg end) 2075 (viper-enlarge-region beg end)
2076 (setq region (buffer-substring (point) (mark t))) 2076 (setq region (buffer-substring (point) (mark t)))
2077 ;; create temp buffer for the region 2077 ;; create temp buffer for the region
2078 (setq temp-buf (get-buffer-create " *ex-write*")) 2078 (setq temp-buf (get-buffer-create " *ex-write*"))
2102 (set-buffer orig-buf))) 2102 (set-buffer orig-buf)))
2103 ;; set the right file modification time 2103 ;; set the right file modification time
2104 (if (and (buffer-file-name) writing-same-file) 2104 (if (and (buffer-file-name) writing-same-file)
2105 (set-visited-file-modtime)) 2105 (set-visited-file-modtime))
2106 ;; prevent loss of data if saving part of the buffer in visited file 2106 ;; prevent loss of data if saving part of the buffer in visited file
2107 (or writing-whole-file 2107 (or writing-whole-file
2108 (not writing-same-file) 2108 (not writing-same-file)
2109 (progn 2109 (progn
2110 (sit-for 2) 2110 (sit-for 2)
2111 (message "Warning: you have saved only part of the buffer!") 2111 (message "Warning: you have saved only part of the buffer!")
2112 (set-buffer-modified-p t))) 2112 (set-buffer-modified-p t)))
2113 (if q-flag 2113 (if q-flag
2114 (if (< viper-expert-level 2) 2114 (if (< viper-expert-level 2)
2115 (save-buffers-kill-emacs) 2115 (save-buffers-kill-emacs)
2116 (kill-buffer (current-buffer)))) 2116 (kill-buffer (current-buffer))))
2117 ))) 2117 )))
2118 2118
2119 2119
2120 (defun ex-write-info (exists file-name beg end) 2120 (defun ex-write-info (exists file-name beg end)
2121 (message "`%s'%s %d lines, %d characters" 2121 (message "`%s'%s %d lines, %d characters"
2122 (viper-abbreviate-file-name file-name) 2122 (viper-abbreviate-file-name file-name)
2123 (if exists "" " [New file]") 2123 (if exists "" " [New file]")
2153 2153
2154 ;; Execute shell command 2154 ;; Execute shell command
2155 (defun ex-command () 2155 (defun ex-command ()
2156 (let (command) 2156 (let (command)
2157 (save-window-excursion 2157 (save-window-excursion
2158 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) 2158 (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name))
2159 (set-buffer viper-ex-work-buf) 2159 (set-buffer viper-ex-work-buf)
2160 (skip-chars-forward " \t") 2160 (skip-chars-forward " \t")
2161 (setq command (buffer-substring (point) (point-max))) 2161 (setq command (buffer-substring (point) (point-max)))
2162 (end-of-line)) 2162 (end-of-line))
2163 ;; replace # and % with the previous/current file 2163 ;; replace # and % with the previous/current file
2260 ;; display all variables set through :set 2260 ;; display all variables set through :set
2261 (defun ex-show-vars () 2261 (defun ex-show-vars ()
2262 (with-output-to-temp-buffer " *viper-info*" 2262 (with-output-to-temp-buffer " *viper-info*"
2263 (princ (if viper-auto-indent 2263 (princ (if viper-auto-indent
2264 "autoindent (local)\n" "noautoindent (local)\n")) 2264 "autoindent (local)\n" "noautoindent (local)\n"))
2265 (princ (if (default-value 'viper-auto-indent) 2265 (princ (if (default-value 'viper-auto-indent)
2266 "autoindent (global) \n" "noautoindent (global) \n")) 2266 "autoindent (global) \n" "noautoindent (global) \n"))
2267 (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n")) 2267 (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n"))
2268 (princ (if viper-re-search "magic\n" "nomagic\n")) 2268 (princ (if viper-re-search "magic\n" "nomagic\n"))
2269 (princ (if buffer-read-only "readonly\n" "noreadonly\n")) 2269 (princ (if buffer-read-only "readonly\n" "noreadonly\n"))
2270 (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n")) 2270 (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n"))