comparison lisp/viper/viper.el @ 78:c7528f8e288d r20-0b34

Import from CVS: tag r20-0b34
author cvs
date Mon, 13 Aug 2007 09:05:42 +0200
parents 131b0175ea99
children 1ce6082ce73f
comparison
equal deleted inserted replaced
77:6cb4f478e7bc 78:c7528f8e288d
6 ;; Keywords: emulations 6 ;; Keywords: emulations
7 ;; Author: Michael Kifer <kifer@cs.sunysb.edu> 7 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
8 8
9 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc. 9 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
10 10
11 (defconst viper-version "2.90 of June 19, 1996" 11 (defconst viper-version "2.91 of August 5, 1996"
12 "The current version of Viper") 12 "The current version of Viper")
13 13
14 ;; This file is part of GNU Emacs. 14 ;; This file is part of GNU Emacs.
15 15
16 ;; GNU Emacs is free software; you can redistribute it and/or modify 16 ;; GNU Emacs is free software; you can redistribute it and/or modify
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details. 24 ;; GNU General Public License for more details.
25 25
26 ;; You should have received a copy of the GNU General Public License 26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to 27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
29 30
30 ;;; Commentary: 31 ;;; Commentary:
31 32
32 ;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and 33 ;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and
33 ;; improves upon the standard features of Vi and, at the same time, allows 34 ;; improves upon the standard features of Vi and, at the same time, allows
86 ;; macros. Third, in Viper, one can define macros that are specific to 87 ;; macros. Third, in Viper, one can define macros that are specific to
87 ;; a given buffer, a given major mode, or macros defined for all buffers. 88 ;; a given buffer, a given major mode, or macros defined for all buffers.
88 ;; The same macro name can have several different definitions: 89 ;; The same macro name can have several different definitions:
89 ;; one global, several definitions for various major modes, and 90 ;; one global, several definitions for various major modes, and
90 ;; definitions for specific buffers. 91 ;; definitions for specific buffers.
91 ;; Bffer-specific definitions override mode-specific 92 ;; Buffer-specific definitions override mode-specific
92 ;; definitions, which, in turn, override global definitions. 93 ;; definitions, which, in turn, override global definitions.
93 ;; 94 ;;
94 ;; 95 ;;
95 ;;; Installation: 96 ;;; Installation:
96 ;; ------------- 97 ;; -------------
98 ;; (require 'viper) 99 ;; (require 'viper)
99 ;; 100 ;;
100 101
101 ;;; Acknowledgements: 102 ;;; Acknowledgements:
102 ;; ----------------- 103 ;; -----------------
103 ;; Bug reports and ideas contributed by the following users 104 ;; Bug reports and ideas contributed by many users have helped
104 ;; have helped improve Viper and the various versions of VIP. 105 ;; improve Viper and the various versions of VIP.
105 ;; See the on-line manual for a complete list of contributors. 106 ;; See the on-line manual for a complete list of contributors.
106 ;; 107 ;;
107 ;; 108 ;;
108 ;;; Notes: 109 ;;; Notes:
109 ;; 110 ;;
153 ;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This 154 ;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This
154 ;; minor mode's bindings make Viper simulate the usual Vi very closely. 155 ;; minor mode's bindings make Viper simulate the usual Vi very closely.
155 ;; For instance, C-c will not have its standard Emacs binding 156 ;; For instance, C-c will not have its standard Emacs binding
156 ;; and so many of the goodies of Emacs are not available. 157 ;; and so many of the goodies of Emacs are not available.
157 ;; 158 ;;
158 ;; An skilled user, should set vip-expert-level to at least 3. This will 159 ;; A skilled user should set vip-expert-level to at least 3. This will
159 ;; enable ;; C-c and many Emacs facilities will become available. 160 ;; enable `C-c; and many Emacs facilities will become available.
160 ;; In this case, vip-vi-diehard-minor-mode is inactive. 161 ;; In this case, vip-vi-diehard-minor-mode is inactive.
161 ;; 162 ;;
162 ;; Viper gurus should have at least 163 ;; Viper gurus should have at least
163 ;; (setq vip-expert-level 4) 164 ;; (setq vip-expert-level 4)
164 ;; in their ~/.vip files. This will unsuppress all Emacs keys that are not 165 ;; in their ~/.vip files. This will unsuppress all Emacs keys that are not
334 (defvar vip-saved-user-settings nil) 335 (defvar vip-saved-user-settings nil)
335 336
336 337
337 ;;; Viper minor modes 338 ;;; Viper minor modes
338 339
339 ;; This is not local in Emacs, so we make it local. 340 ;; This must be local because although the stack of minor modes can be the same
341 ;; for all buffers, the associated *keymaps* can be different. In Viper,
342 ;; vip-vi-local-user-map, vip-insert-local-user-map, and others can have
343 ;; different keymaps for different buffers.
344 ;; Also, the keymaps associated with vip-vi/insert-state-modifier-minor-mode
345 ;; can be different.
340 (make-variable-buffer-local 'minor-mode-map-alist) 346 (make-variable-buffer-local 'minor-mode-map-alist)
341 347
342 ;; Mode for vital things like \e, C-z. 348 ;; Mode for vital things like \e, C-z.
343 (vip-deflocalvar vip-vi-intercept-minor-mode nil) 349 (vip-deflocalvar vip-vi-intercept-minor-mode nil)
344 350
471 (vip-deflocalvar vip-began-as-replace nil "") 477 (vip-deflocalvar vip-began-as-replace nil "")
472 478
473 (defvar vip-replace-overlay-cursor-color "Red" 479 (defvar vip-replace-overlay-cursor-color "Red"
474 "*Cursor color to use in Replace state") 480 "*Cursor color to use in Replace state")
475 481
476 482 (defvar vip-insert-state-cursor-color nil
483 "Cursor color for Viper insert state.")
484 (put 'vip-insert-state-cursor-color 'permanent-local t)
485 ;; place to save cursor colow when switching to insert mode
486 (vip-deflocalvar vip-saved-cursor-color nil "")
487
477 (vip-deflocalvar vip-replace-overlay nil "") 488 (vip-deflocalvar vip-replace-overlay nil "")
478 (put 'vip-replace-overlay 'permanent-local t) 489 (put 'vip-replace-overlay 'permanent-local t)
479 490
480 (if (vip-has-face-support-p) 491 (defvar vip-replace-overlay-pixmap "gray3"
481 (progn 492 "Pixmap to use for search face on non-color displays.")
482 (make-face 'vip-replace-overlay-face) 493 (defvar vip-search-face-pixmap "gray3"
483 (vip-hide-face 'vip-replace-overlay-face) 494 "Pixmap to use for search face on non-color displays.")
484 (or (face-differs-from-default-p 'vip-replace-overlay-face) 495
485 (progn 496
486 (if (vip-can-use-colors "darkseagreen2" "Black") 497 (defun vip-set-replace-overlay-face ()
487 (progn 498 (if (vip-has-face-support-p)
488 (set-face-background 499 (defvar vip-replace-overlay-face
489 'vip-replace-overlay-face "darkseagreen2") 500 (progn
490 (set-face-foreground 'vip-replace-overlay-face "Black"))) 501 (make-face 'vip-replace-overlay-face)
491 (set-face-underline-p 'vip-replace-overlay-face t)) 502 (vip-hide-face 'vip-replace-overlay-face)
492 ))) 503 (or (face-differs-from-default-p 'vip-replace-overlay-face)
493 504 (progn
494 (defvar vip-replace-overlay-face 'vip-replace-overlay-face 505 (if (vip-can-use-colors "darkseagreen2" "Black")
495 "*Face for highlighting replace regions on a window display.") 506 (progn
507 (set-face-background
508 'vip-replace-overlay-face "darkseagreen2")
509 (set-face-foreground 'vip-replace-overlay-face "Black")))
510 (set-face-underline-p 'vip-replace-overlay-face t)
511 (vip-set-face-pixmap
512 'vip-replace-overlay-face vip-replace-overlay-pixmap)))
513 'vip-replace-overlay-face)
514 "*Face for highlighting replace regions on a window display.")
515 ))
496 516
497 (defvar vip-replace-region-end-delimiter "$" 517 (defvar vip-replace-region-end-delimiter "$"
498 "A string marking the end of replacement regions. 518 "A string marking the end of replacement regions.
499 It is used only with TTYs or if `vip-use-replace-region-delimiters' 519 It is used only with TTYs or if `vip-use-replace-region-delimiters'
500 is non-nil.") 520 is non-nil.")
501 (defvar vip-replace-region-start-delimiter "" 521 (defvar vip-replace-region-start-delimiter ""
502 "A string marking the beginning of replacement regions. 522 "A string marking the beginning of replacement regions.
503 It is used only with TTYs or if `vip-use-replace-region-delimiters' 523 It is used only with TTYs or if `vip-use-replace-region-delimiters'
504 is non-nil.") 524 is non-nil.")
505 (defvar vip-use-replace-region-delimiters 525 (defvar vip-use-replace-region-delimiters (not (vip-has-face-support-p))
506 (or (not (vip-has-face-support-p)) (not (vip-color-display-p)))
507 "*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and 526 "*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and
508 `vip-replace-region-start-delimiter' to delimit replacement regions, even on 527 `vip-replace-region-start-delimiter' to delimit replacement regions, even on
509 color displays. By default, the delimiters are used only on TTYs or 528 color displays. By default, the delimiters are used only on TTYs.")
510 monochrome displays.")
511 529
512 ;; XEmacs requires glyphs 530 ;; XEmacs requires glyphs
513 (if vip-xemacs-p 531 (if vip-xemacs-p
514 (progn 532 (progn
515 (or (glyphp vip-replace-region-end-delimiter) 533 (or (glyphp vip-replace-region-end-delimiter)
560 ;; Modes and related variables 578 ;; Modes and related variables
561 579
562 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state' 580 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
563 (vip-deflocalvar vip-current-state 'emacs-state) 581 (vip-deflocalvar vip-current-state 'emacs-state)
564 582
565
566 (defvar vip-toggle-key "\C-z"
567 "The key used to change states from emacs to Vi and back.
568 In insert mode, this key also functions as Meta.
569 Must be set in .vip file or prior to loading Viper.
570 This setting cannot be changed interactively.")
571
572 (defvar vip-ESC-key "\e"
573 "Key used to ESC.
574 Must be set in .vip file or prior to loading Viper.
575 This setting cannot be changed interactively.")
576
577 (defvar vip-no-multiple-ESC t 583 (defvar vip-no-multiple-ESC t
578 "*If true, multiple ESC in Vi mode will cause bell to ring. 584 "*If true, multiple ESC in Vi mode will cause bell to ring.
579 \_ is then mapped to Meta.
580 This is set to t on a windowing terminal and to 'twice on a dumb 585 This is set to t on a windowing terminal and to 'twice on a dumb
581 terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this 586 terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
582 enables cursor keys and is generally more convenient, as terminals usually 587 enables cursor keys and is generally more convenient, as terminals usually
583 don't have a convenient Meta key. 588 don't have a convenient Meta key.
584 Setting vip-no-multiple-ESC to nil will allow as many multiple ESC, 589 Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
695 700
696 ;; If t, search is forward. 701 ;; If t, search is forward.
697 (defvar vip-s-forward nil) 702 (defvar vip-s-forward nil)
698 703
699 (defconst vip-case-fold-search nil 704 (defconst vip-case-fold-search nil
700 "*If t, search ignores cases.") 705 "*If not nil, search ignores case.")
701 706
702 (defconst vip-re-search t 707 (defconst vip-re-search t
703 "*If t, search is reg-exp search, otherwise vanilla search.") 708 "*If not nil, search is reg-exp search, otherwise vanilla search.")
709
710 (defvar vip-adjust-window-after-search t
711 "*If not nil, pull the window up or down, depending on the direction of the
712 search, if search ends up near the bottom or near the top of the window.")
704 713
705 (defconst vip-re-query-replace t 714 (defconst vip-re-query-replace t
706 "*If t then do regexp replace, if nil then do string replace.") 715 "*If t then do regexp replace, if nil then do string replace.")
707 716
708 (defconst vip-re-replace t 717 (defconst vip-re-replace t
818 "Whether Viper startup message should be inhibited.") 827 "Whether Viper startup message should be inhibited.")
819 828
820 (defvar vip-always t 829 (defvar vip-always t
821 "t means, arrange that vi-state will be a default.") 830 "t means, arrange that vi-state will be a default.")
822 831
823 (defvar vip-ms-style-os-p (memq system-type '(ms-dos windows-nt windows-95)) 832 (defvar vip-custom-file-name (vip-convert-standard-file-name "~/.vip")
824 "Tells is Emacs is running under an MS-style OS: ms-dos, window-nt, W95.")
825 (defvar vip-vms-os-p (memq system-type '(vax-vms axp-vms))
826 "Tells if Emacs is running under VMS.")
827
828 (defvar vip-custom-file-name (cond (vip-vms-os-p "sys$login:.vip")
829 ((memq system-type '(emx ms-dos))
830 "/_vip")
831 ((memq system-type '(windows-nt windows-95))
832 "~/_vip")
833 (t ; Unix
834 "~/.vip"))
835 "Viper customisation file. 833 "Viper customisation file.
836 This variable must be set _before_ loading Viper.") 834 This variable must be set _before_ loading Viper.")
837 835
838 836
839 (defvar vip-spell-function 'ispell-region 837 (defvar vip-spell-function 'ispell-region
934 ?; ?, ?0 ?? ?/ 932 ?; ?, ?0 ?? ?/
935 ) 933 )
936 "Movement commands") 934 "Movement commands")
937 (vip-test-com-defun vip-movement-command) 935 (vip-test-com-defun vip-movement-command)
938 936
939 ;; Commands that can be repeated by .(dotted) 937 ;; Commands that can be repeated by . (dotted)
940 (defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<)) 938 (defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<))
941 (vip-test-com-defun vip-dotable-command) 939 (vip-test-com-defun vip-dotable-command)
942 940
943 ;; Commands that can follow a # 941 ;; Commands that can follow a #
944 (defconst vip-hash-cmds '(?c ?C ?g ?q ?S)) 942 (defconst vip-hash-cmds '(?c ?C ?g ?q ?S))
946 944
947 ;; Commands that may have registers as prefix 945 ;; Commands that may have registers as prefix
948 (defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X)) 946 (defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
949 (vip-test-com-defun vip-regsuffix-command) 947 (vip-test-com-defun vip-regsuffix-command)
950 948
951 949 (defconst vip-vi-commands (append vip-movement-commands
950 vip-dotable-commands
951 vip-charpair-commands
952 vip-hash-cmds
953 vip-prefix-commands
954 vip-regsuffix-commands)
955 "The list of all commands in Vi-state.")
956 (vip-test-com-defun vip-vi-command)
952 957
953 ;;; Arrange the keymaps 958 ;;; Arrange the keymaps
954 (require 'viper-keym) 959 (require 'viper-keym)
955 960
956 961
983 (defsubst vip-insert-state-post-command-sentinel () 988 (defsubst vip-insert-state-post-command-sentinel ()
984 (if (and (memq vip-current-state '(insert-state replace-state)) 989 (if (and (memq vip-current-state '(insert-state replace-state))
985 vip-insert-point 990 vip-insert-point
986 (>= (point) vip-insert-point)) 991 (>= (point) vip-insert-point))
987 (setq vip-last-posn-while-in-insert-state (point-marker))) 992 (setq vip-last-posn-while-in-insert-state (point-marker)))
993 (if (eq vip-current-state 'insert-state)
994 (progn
995 (or (stringp vip-saved-cursor-color)
996 (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
997 (setq vip-saved-cursor-color (vip-get-cursor-color)))
998 (if (stringp vip-saved-cursor-color)
999 (vip-change-cursor-color vip-insert-state-cursor-color))
1000 ))
988 (if (and (eq this-command 'dabbrev-expand) 1001 (if (and (eq this-command 'dabbrev-expand)
989 (integerp vip-pre-command-point) 1002 (integerp vip-pre-command-point)
990 (> vip-insert-point vip-pre-command-point)) 1003 (> vip-insert-point vip-pre-command-point))
991 (move-marker vip-insert-point vip-pre-command-point)) 1004 (move-marker vip-insert-point vip-pre-command-point))
992 ) 1005 )
993 1006
994 (defsubst vip-insert-state-pre-command-sentinel () 1007 (defsubst vip-insert-state-pre-command-sentinel ()
1008 (or (memq this-command '(self-insert-command))
1009 (memq (vip-event-key last-command-event)
1010 '(up down left right (meta f) (meta b)
1011 (control n) (control p) (control f) (control b)))
1012 (vip-restore-cursor-color-after-insert))
995 (if (and (eq this-command 'dabbrev-expand) 1013 (if (and (eq this-command 'dabbrev-expand)
996 (markerp vip-insert-point) 1014 (markerp vip-insert-point)
997 (marker-position vip-insert-point)) 1015 (marker-position vip-insert-point))
998 (setq vip-pre-command-point (marker-position vip-insert-point)))) 1016 (setq vip-pre-command-point (marker-position vip-insert-point))))
999 1017
1005 ;; the point outside the replacement region, then the cursor color will 1023 ;; the point outside the replacement region, then the cursor color will
1006 ;; remain red. Restoring the default, below, prevents this. 1024 ;; remain red. Restoring the default, below, prevents this.
1007 (if (and (<= (vip-replace-start) (point)) 1025 (if (and (<= (vip-replace-start) (point))
1008 (<= (point) (vip-replace-end))) 1026 (<= (point) (vip-replace-end)))
1009 (vip-change-cursor-color vip-replace-overlay-cursor-color) 1027 (vip-change-cursor-color vip-replace-overlay-cursor-color)
1010 (vip-restore-cursor-color) 1028 (vip-restore-cursor-color-after-replace)
1011 )) 1029 ))
1012 1030
1013 ;; to speed up, don't change cursor color before self-insert 1031 ;; to speed up, don't change cursor color before self-insert
1014 ;; and common move commands 1032 ;; and common move commands
1015 (defsubst vip-replace-state-pre-command-sentinel () 1033 (defsubst vip-replace-state-pre-command-sentinel ()
1029 ;; 1047 ;;
1030 ;; We optimize for self-insert-command's here, since they either don't change 1048 ;; We optimize for self-insert-command's here, since they either don't change
1031 ;; cursor color or, if they terminate replace mode, the color will be changed 1049 ;; cursor color or, if they terminate replace mode, the color will be changed
1032 ;; in vip-finish-change 1050 ;; in vip-finish-change
1033 (or (memq this-command '(self-insert-command)) 1051 (or (memq this-command '(self-insert-command))
1034 (vip-restore-cursor-color)) 1052 (vip-restore-cursor-color-after-replace))
1035 (cond 1053 (cond
1036 ((eq vip-current-state 'replace-state) 1054 ((eq vip-current-state 'replace-state)
1037 ;; delete characters to compensate for inserted chars. 1055 ;; delete characters to compensate for inserted chars.
1038 (let ((replace-boundary (vip-replace-end))) 1056 (let ((replace-boundary (vip-replace-end)))
1039 (save-excursion 1057 (save-excursion
1148 (setq vip-insert-diehard-minor-mode 1166 (setq vip-insert-diehard-minor-mode
1149 (not vip-want-emacs-keys-in-insert)) 1167 (not vip-want-emacs-keys-in-insert))
1150 1168
1151 (if vip-want-ctl-h-help 1169 (if vip-want-ctl-h-help
1152 (progn 1170 (progn
1153 (define-key vip-insert-basic-map "\C-h" 'help-command) 1171 (define-key vip-insert-basic-map [(control h)] 'help-command)
1154 (define-key vip-replace-map "\C-h" 'help-command)) 1172 (define-key vip-replace-map [(control h)] 'help-command))
1155 (define-key vip-insert-basic-map 1173 (define-key vip-insert-basic-map
1156 "\C-h" 'vip-del-backward-char-in-insert) 1174 [(control h)] 'vip-del-backward-char-in-insert)
1157 (define-key vip-replace-map 1175 (define-key vip-replace-map
1158 "\C-h" 'vip-del-backward-char-in-replace))) 1176 [(control h)] 'vip-del-backward-char-in-replace)))
1159 1177
1160 (t 1178 (t ; Vi state
1161 (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi)) 1179 (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi))
1162 (if vip-want-ctl-h-help 1180 (if vip-want-ctl-h-help
1163 (define-key vip-vi-basic-map "\C-h" 'help-command) 1181 (define-key vip-vi-basic-map [(control h)] 'help-command)
1164 (define-key vip-vi-basic-map "\C-h" 'vip-backward-char))) 1182 (define-key vip-vi-basic-map [(control h)] 'vip-backward-char)))
1165 )) 1183 ))
1166 1184
1167 1185
1168 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first. 1186 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
1169 ;; This ensures that Viper bindings are in effect, regardless of which minor 1187 ;; This ensures that Viper bindings are in effect, regardless of which minor
1279 do not cause Emacs to quit, except at user level 1 (a novice). 1297 do not cause Emacs to quit, except at user level 1 (a novice).
1280 3. ^X^C EXITS EMACS. 1298 3. ^X^C EXITS EMACS.
1281 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat 1299 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
1282 undo. Another `u' changes direction. 1300 undo. Another `u' changes direction.
1283 1301
1284 6. Emacs Meta functions are invoked by typing `_' or `\\ ESC'. 1302 6. Emacs Meta functions are invoked by typing `C-\\' or `\\ ESC'.
1285 On a window system, the best way is to use the Meta-key. 1303 On a window system, the best way is to use the Meta-key.
1286 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if 1304 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if
1287 something funny happens. This would abort the current editing command. 1305 something funny happens. This would abort the current editing command.
1288 1306
1289 You can get more information on Viper by: 1307 You can get more information on Viper by:
1429 1447
1430 ;; always turn off iso-accents-mode, or else we won't be able to use the 1448 ;; always turn off iso-accents-mode, or else we won't be able to use the
1431 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions. 1449 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
1432 (if (and (boundp 'iso-accents-mode) iso-accents-mode) 1450 (if (and (boundp 'iso-accents-mode) iso-accents-mode)
1433 (iso-accents-mode -1)) 1451 (iso-accents-mode -1))
1434 1452
1453 (vip-restore-cursor-color-after-insert)
1454
1435 ;; Protection against user errors in hooks 1455 ;; Protection against user errors in hooks
1436 (condition-case conds 1456 (condition-case conds
1437 (run-hooks 'vip-vi-state-hook) 1457 (run-hooks 'vip-vi-state-hook)
1438 (error 1458 (error
1439 (vip-message-conditions conds))))) 1459 (vip-message-conditions conds)))))
1443 (interactive) 1463 (interactive)
1444 (vip-change-state 'insert-state) 1464 (vip-change-state 'insert-state)
1445 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode)) 1465 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
1446 (iso-accents-mode 1)) ; turn iso accents on 1466 (iso-accents-mode 1)) ; turn iso accents on
1447 1467
1468 (or (stringp vip-saved-cursor-color)
1469 (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
1470 (setq vip-saved-cursor-color (vip-get-cursor-color)))
1471 ;; Commented out, because if vip-change-state-to-insert is executed
1472 ;; non-interactively then the old cursor color may get lost. Same old Emacs
1473 ;; bug related to local variables?
1474 ;;;(if (stringp vip-saved-cursor-color)
1475 ;;; (vip-change-cursor-color vip-insert-state-cursor-color))
1448 ;; Protection against user errors in hooks 1476 ;; Protection against user errors in hooks
1449 (condition-case conds 1477 (condition-case conds
1450 (run-hooks 'vip-insert-state-hook) 1478 (run-hooks 'vip-insert-state-hook)
1451 (error 1479 (error
1452 (vip-message-conditions conds)))) 1480 (vip-message-conditions conds))))
1494 (defun vip-escape-to-emacs (arg &optional events) 1522 (defun vip-escape-to-emacs (arg &optional events)
1495 "Escape to Emacs state from Vi state for one Emacs command. 1523 "Escape to Emacs state from Vi state for one Emacs command.
1496 ARG is used as the prefix value for the executed command. If 1524 ARG is used as the prefix value for the executed command. If
1497 EVENTS is a list of events, which become the beginning of the command." 1525 EVENTS is a list of events, which become the beginning of the command."
1498 (interactive "P") 1526 (interactive "P")
1527 (if (= last-command-char ?\\)
1528 (message "Switched to EMACS state for the next command..."))
1499 (vip-escape-to-state arg events 'emacs-state)) 1529 (vip-escape-to-state arg events 'emacs-state))
1500 1530
1501 ;; escape to Vi mode termporarily 1531 ;; escape to Vi mode termporarily
1502 (defun vip-escape-to-vi () 1532 (defun vip-escape-to-vi (arg)
1503 "Escape from Emacs state to Vi state for one Vi 1-character command. 1533 "Escape from Emacs state to Vi state for one Vi 1-character command.
1504 This doesn't work with prefix arguments or most complex commands like 1534 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
1505 cw, dw, etc. But it does work with some 2-character commands, 1535 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
1506 like dd or dr." 1536 `vip-escape-to-vi' is used."
1507 (interactive) 1537 (interactive "P")
1508 (vip-escape-to-state nil nil 'vi-state)) 1538 (message "Switched to VI state for the next command...")
1509 1539 (vip-escape-to-state arg nil 'vi-state))
1540
1510 ;; Escape to STATE mode for one Emacs command. 1541 ;; Escape to STATE mode for one Emacs command.
1511 (defun vip-escape-to-state (arg events state) 1542 (defun vip-escape-to-state (arg events state)
1512 (let (com key prefix-arg) 1543 ;;(let (com key prefix-arg)
1544 (let (com key)
1513 ;; this temporarily turns off Viper's minor mode keymaps 1545 ;; this temporarily turns off Viper's minor mode keymaps
1514 (vip-set-mode-vars-for state) 1546 (vip-set-mode-vars-for state)
1515 (vip-normalize-minor-mode-map-alist) 1547 (vip-normalize-minor-mode-map-alist)
1516 (if events (vip-set-unread-command-events events)) 1548 (if events (vip-set-unread-command-events events))
1517 1549
1518 ;; protect against keyboard quit and other errors 1550 ;; protect against keyboard quit and other errors
1519 (condition-case nil 1551 (condition-case nil
1520 (progn 1552 (let (vip-vi-kbd-minor-mode
1553 vip-insert-kbd-minor-mode
1554 vip-emacs-kbd-minor-mode)
1521 (unwind-protect 1555 (unwind-protect
1522 (progn 1556 (progn
1523 (setq com (key-binding (setq key 1557 (setq com (key-binding (setq key
1524 (if vip-xemacs-p 1558 (if vip-xemacs-p
1525 (read-key-sequence nil) 1559 (read-key-sequence nil)
1526 (read-key-sequence nil t))))) 1560 (read-key-sequence nil t)))))
1527 ;; In case of indirection--chase definitions. 1561 ;; In case of binding indirection--chase definitions.
1528 ;; Have to do it here because we execute this command under 1562 ;; Have to do it here because we execute this command under
1529 ;; different keymaps, so command-execute may not do the 1563 ;; different keymaps, so command-execute may not do the
1530 ;; right thing there 1564 ;; right thing there
1531 (while (vectorp com) (setq com (key-binding com)))) 1565 (while (vectorp com) (setq com (key-binding com))))
1532 nil) 1566 nil)
1533 ;; exec command in the right Viper state 1567 ;; Execute command com in the original Viper state, not in state
1534 ;; otherwise, if we switch buffers in the escaped command, 1568 ;; `state'. Otherwise, if we switch buffers while executing the
1535 ;; Viper's mode vars will remain those of `state'. When we return 1569 ;; escaped to command, Viper's mode vars will remain those of
1536 ;; to the orig buffer, the bindings will be screwed up. 1570 ;; `state'. When we return to the orig buffer, the bindings will be
1571 ;; screwed up.
1537 (vip-set-mode-vars-for vip-current-state) 1572 (vip-set-mode-vars-for vip-current-state)
1538 1573
1539 ;; this-command, last-command-char, last-command-event 1574 ;; this-command, last-command-char, last-command-event
1540 (setq this-command com) 1575 (setq this-command com)
1541 (if vip-xemacs-p ; XEmacs represents key sequences as vectors 1576 (if vip-xemacs-p ; XEmacs represents key sequences as vectors
1545 (setq last-command-event (vip-seq-last-elt key) 1580 (setq last-command-event (vip-seq-last-elt key)
1546 last-command-char last-command-event)) 1581 last-command-char last-command-event))
1547 1582
1548 (if (commandp com) 1583 (if (commandp com)
1549 (progn 1584 (progn
1550 (setq prefix-arg arg) 1585 (setq prefix-arg (or prefix-arg arg))
1551 (command-execute com))) 1586 (command-execute com)))
1552 ) 1587 )
1553 (quit (ding)) 1588 (quit (ding))
1554 (error (beep 1)))) 1589 (error (beep 1))))
1555 (vip-set-mode-vars-for vip-current-state)) ; set state in new buffer 1590 ;; set state in the new buffer
1591 (vip-set-mode-vars-for vip-current-state))
1556 1592
1593 (defun vip-exec-form-in-vi (form)
1594 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
1595 (let ((buff (current-buffer))
1596 result)
1597 (vip-set-mode-vars-for 'vi-state)
1598 (setq result (eval form))
1599 (if (not (equal buff (current-buffer))) ; cmd switched buffer
1600 (save-excursion
1601 (set-buffer buff)
1602 (vip-set-mode-vars-for vip-current-state)))
1603 (vip-set-mode-vars-for vip-current-state)
1604 result))
1605
1557 (defun vip-exec-form-in-emacs (form) 1606 (defun vip-exec-form-in-emacs (form)
1558 "Execute FORM in Emacs, temporarily disabling Viper's minor modes. 1607 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
1559 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes." 1608 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
1560 (let ((buff (current-buffer)) 1609 (let ((buff (current-buffer))
1561 result) 1610 result)
1594 ) 1643 )
1595 1644
1596 1645
1597 (defun vip-ESC (arg) 1646 (defun vip-ESC (arg)
1598 "Emulate ESC key in Emacs. 1647 "Emulate ESC key in Emacs.
1599 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true. In that 1648 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true.
1600 case \@ will be bound to ESC. If vip-no-multiple-ESC is 'twice double ESC 1649 If vip-no-multiple-ESC is 'twice double ESC would ding in vi-state.
1601 would dings in vi-state. Other ESC sequences are emulated via the current 1650 Other ESC sequences are emulated via the current Emacs's major mode
1602 Emacs's major mode keymap. This is more convenient on dumb terminals and in 1651 keymap. This is more convenient on TTYs, since this won't block
1603 Emacs -nw, since this won't block functional keys such as up,down, 1652 function keys such as up,down, etc. ESC will also will also work as
1604 etc. Meta key also will work. When vip-no-multiple-ESC is nil, ESC key 1653 a Meta key in this case. When vip-no-multiple-ESC is nil, ESC functions
1605 behaves as in Emacs, any number of multiple escapes is allowed." 1654 as a Meta key and any number of multiple escapes is allowed."
1606 (interactive "P") 1655 (interactive "P")
1607 (let (char) 1656 (let (char)
1608 (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state)) 1657 (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state))
1609 (setq char (vip-read-char-exclusive)) 1658 (setq char (vip-read-char-exclusive))
1610 (vip-escape-to-emacs arg (list ?\e char) )) 1659 (vip-escape-to-emacs arg (list ?\e char) ))
1615 (ding) 1664 (ding)
1616 (vip-escape-to-emacs arg (list ?\e char) ))) 1665 (vip-escape-to-emacs arg (list ?\e char) )))
1617 (t (ding))) 1666 (t (ding)))
1618 )) 1667 ))
1619 1668
1620 (defun vip-alternate-ESC (arg) 1669 (defun vip-alternate-Meta-key (arg)
1621 "ESC key without checking for multiple keystrokes." 1670 "Simulate Emacs Meta key."
1622 (interactive "P") 1671 (interactive "P")
1623 (vip-escape-to-emacs arg '(?\e))) 1672 (vip-escape-to-emacs arg '(?\e)))
1624 1673
1674 (defun vip-toggle-key-action ()
1675 "Action bound to `vip-toggle-key'."
1676 (interactive)
1677 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
1678 (if (vip-window-display-p)
1679 (vip-iconify)
1680 (suspend-emacs))
1681 (vip-change-state-to-emacs)))
1625 1682
1626 ;; Intercept ESC sequences on dumb terminals. 1683 ;; Intercept ESC sequences on dumb terminals.
1627 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es> 1684 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
1628 1685
1629 ;; Check if last key was ESC and if so try to reread it as a function key. 1686 ;; Check if last key was ESC and if so try to reread it as a function key.
1790 ((equal com ?y) ?Y) 1847 ((equal com ?y) ?Y)
1791 (t com)))) 1848 (t com))))
1792 1849
1793 1850
1794 ;; Compute numeric prefix arg value. 1851 ;; Compute numeric prefix arg value.
1795 ;; Invoked by CHAR. COM is the command part obtained so far. 1852 ;; Invoked by EVENT. COM is the command part obtained so far.
1796 (defun vip-prefix-arg-value (event com) 1853 (defun vip-prefix-arg-value (event com)
1797 (let (value) 1854 (let (value func)
1798 ;; read while number 1855 ;; read while number
1799 (while (and (vip-characterp event) (>= event ?0) (<= event ?9)) 1856 (while (and (vip-characterp event) (>= event ?0) (<= event ?9))
1800 (setq value (+ (* (if (vip-characterp value) value 0) 10) (- event ?0))) 1857 (setq value (+ (* (if (vip-characterp value) value 0) 10) (- event ?0)))
1801 (setq event (vip-read-event-convert-to-char))) 1858 (setq event (vip-read-event-convert-to-char)))
1802 1859
1803 (setq prefix-arg value) 1860 (setq prefix-arg value)
1804 (if com (setq prefix-arg (cons prefix-arg com))) 1861 (if com (setq prefix-arg (cons prefix-arg com)))
1805 (while (eq event ?U) 1862 (while (eq event ?U)
1806 (vip-describe-arg prefix-arg) 1863 (vip-describe-arg prefix-arg)
1807 (setq event (vip-read-event-convert-to-char))) 1864 (setq event (vip-read-event-convert-to-char)))
1808 (vip-set-unread-command-events event))) 1865
1866 (if (or com (and (not (eq vip-current-state 'vi-state))
1867 ;; make sure it is a Vi command
1868 (vip-characterp event) (vip-vi-command-p event)
1869 ))
1870 ;; If appears to be one of the vi commands,
1871 ;; then execute it with funcall and clear prefix-arg in order to not
1872 ;; confuse subsequent commands
1873 (progn
1874 ;; last-command-char is the char we want emacs to think was typed
1875 ;; last. If com is not nil, the vip-digit-argument command was called
1876 ;; from within vip-prefix-arg command, such as `d', `w', etc., i.e.,
1877 ;; the user typed, say, d2. In this case, `com' would be `d', `w',
1878 ;; etc.
1879 ;; If vip-digit-argument was invoked by vip-escape-to-vi (which is
1880 ;; indicated by the fact that the current state is not vi-state,
1881 ;; then `event' represents the vi command to be executed (e.g., `d',
1882 ;; `w', etc. Again, last-command-char must make emacs believe that
1883 ;; this is the command we typed.
1884 (setq last-command-char (or com event))
1885 (setq func (vip-exec-form-in-vi
1886 (` (key-binding (char-to-string (, event))))))
1887 (funcall func prefix-arg)
1888 (setq prefix-arg nil))
1889 ;; some other command -- let emacs do it in its own way
1890 (vip-set-unread-command-events event))
1891 ))
1892
1809 1893
1810 ;; Vi operator as prefix argument." 1894 ;; Vi operator as prefix argument."
1811 (defun vip-prefix-arg-com (char value com) 1895 (defun vip-prefix-arg-com (char value com)
1812 (let ((cont t)) 1896 (let ((cont t)
1897 cmd-info mv-or-digit-cmd)
1813 (while (and cont 1898 (while (and cont
1814 (memq char 1899 (memq char
1815 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\" 1900 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1816 vip-buffer-search-char))) 1901 vip-buffer-search-char)))
1817 (if com 1902 (if com
1841 (setq vip-use-register reg) 1926 (setq vip-use-register reg)
1842 (error "")) 1927 (error ""))
1843 (setq char (read-char)))) 1928 (setq char (read-char))))
1844 (t 1929 (t
1845 (setq com char) 1930 (setq com char)
1846 (setq char (vip-read-char-exclusive))))))) 1931 (setq char (vip-read-char-exclusive))))))
1847 (if (atom com) 1932 (if (atom com)
1848 ;; com is a single char, so we construct prefix-arg 1933 ;; `com' is a single char, so we construct the command argument
1849 ;; and if char is ?, describe prefix arg, otherwise exit by 1934 ;; and if `char' is `?', we describe the arg; otherwise
1850 ;; pushing the char back into vip-set-unread-command-events 1935 ;; we prepare the command that will be executed at the end.
1851 ;; Since char is a command, the command will execute with the prefix
1852 ;; argument that we just constructed.
1853 (progn 1936 (progn
1854 (setq prefix-arg (cons value com)) 1937 (setq cmd-info (cons value com))
1855 (while (= char ?U) 1938 (while (= char ?U)
1856 (vip-describe-arg prefix-arg) 1939 (vip-describe-arg cmd-info)
1857 (setq char (read-char))) 1940 (setq char (read-char)))
1858 (vip-set-unread-command-events char) 1941 ;; `char' is a movement command or a digit arg command---so we execute
1859 ) 1942 ;; it at the very end
1943 (setq mv-or-digit-cmd
1944 (vip-exec-form-in-vi
1945 (` (key-binding (char-to-string (, char)))))))
1860 ;; as com is non-nil, this means that we have a command to execute 1946 ;; as com is non-nil, this means that we have a command to execute
1861 (if (memq (car com) '(?r ?R)) 1947 (if (memq (car com) '(?r ?R))
1862 ;; execute apropriate region command. 1948 ;; execute apropriate region command.
1863 (let ((char (car com)) (com (cdr com))) 1949 (let ((char (car com)) (com (cdr com)))
1864 (setq prefix-arg (cons value com)) 1950 (setq prefix-arg (cons value com))
1875 ((equal com '(?y . ?y)) (vip-line (cons value ?Y))) 1961 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1876 ((equal com '(?< . ?<)) (vip-line (cons value ?<))) 1962 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1877 ((equal com '(?> . ?>)) (vip-line (cons value ?>))) 1963 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1878 ((equal com '(?! . ?!)) (vip-line (cons value ?!))) 1964 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1879 ((equal com '(?= . ?=)) (vip-line (cons value ?=))) 1965 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1880 (t (error "")))))) 1966 (t (error "")))))
1967
1968 (if mv-or-digit-cmd
1969 (progn
1970 (setq last-command-char char)
1971 (funcall mv-or-digit-cmd cmd-info)))
1972 ))
1881 1973
1882 (defun vip-describe-arg (arg) 1974 (defun vip-describe-arg (arg)
1883 (let (val com) 1975 (let (val com)
1884 (setq val (vip-P-val arg) 1976 (setq val (vip-P-val arg)
1885 com (vip-getcom arg)) 1977 com (vip-getcom arg))
1893 1985
1894 (defun vip-digit-argument (arg) 1986 (defun vip-digit-argument (arg)
1895 "Begin numeric argument for the next command." 1987 "Begin numeric argument for the next command."
1896 (interactive "P") 1988 (interactive "P")
1897 (vip-leave-region-active) 1989 (vip-leave-region-active)
1898 (vip-prefix-arg-value last-command-char 1990 (vip-prefix-arg-value
1899 (if (consp arg) (cdr arg) nil))) 1991 last-command-char (if (consp arg) (cdr arg) nil)))
1900 1992
1901 (defun vip-command-argument (arg) 1993 (defun vip-command-argument (arg)
1902 "Accept a motion command as an argument." 1994 "Accept a motion command as an argument."
1903 (interactive "P") 1995 (interactive "P")
1904 (condition-case nil 1996 (condition-case nil
2478 (forward-line 1))) 2570 (forward-line 1)))
2479 2571
2480 ;; Tells whether BEG is on the same line as END. 2572 ;; Tells whether BEG is on the same line as END.
2481 ;; If one of the args is nil, it'll return nil. 2573 ;; If one of the args is nil, it'll return nil.
2482 (defun vip-same-line (beg end) 2574 (defun vip-same-line (beg end)
2483 (let ((selective-display nil)) 2575 (let ((selective-display nil)
2484 (cond ((and beg end) 2576 (incr 0)
2485 ;; This 'if' is needed because Emacs treats the next empty line 2577 temp)
2486 ;; as part of the previous line. 2578 (if (and beg end (> beg end))
2487 (if (or (> beg (point-max)) (> end (point-max))) ; out of range 2579 (setq temp beg
2488 () 2580 beg end
2489 (if (and (> end beg) (= (vip-line-pos 'start) end)) 2581 end temp))
2490 (setq end (min (1+ end) (point-max)))) 2582 (if (and beg end)
2491 (if (and (> beg end) (= (vip-line-pos 'start) beg)) 2583 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
2492 (setq beg (min (1+ beg) (point-max)))) 2584 nil)
2493 (<= (count-lines beg end) 1) )) 2585 (t
2494 2586 ;; This 'if' is needed because Emacs treats the next empty line
2495 (t nil)) 2587 ;; as part of the previous line.
2496 )) 2588 (if (= (vip-line-pos 'start) end)
2497 2589 (setq incr 1))
2498 2590 (<= (+ incr (count-lines beg end)) 1))))
2591 ))
2592
2593
2499 ;; Check if the string ends with a newline. 2594 ;; Check if the string ends with a newline.
2500 (defun vip-end-with-a-newline-p (string) 2595 (defun vip-end-with-a-newline-p (string)
2501 (or (string= string "") 2596 (or (string= string "")
2502 (= (vip-seq-last-elt string) ?\n))) 2597 (= (vip-seq-last-elt string) ?\n)))
2503 2598
2544 ;; face wasn't set in .vip or .Xdefaults 2639 ;; face wasn't set in .vip or .Xdefaults
2545 (if (vip-can-use-colors "Black" "khaki") 2640 (if (vip-can-use-colors "Black" "khaki")
2546 (progn 2641 (progn
2547 (set-face-background 'vip-search-face "khaki") 2642 (set-face-background 'vip-search-face "khaki")
2548 (set-face-foreground 'vip-search-face "Black")) 2643 (set-face-foreground 'vip-search-face "Black"))
2549 (copy-face 'italic 'vip-search-face) 2644 (set-face-underline-p 'vip-search-face t)
2550 (set-face-underline-p 'vip-search-face t))) 2645 (vip-set-face-pixmap 'vip-search-face vip-search-face-pixmap)))
2551 'vip-search-face) 2646 'vip-search-face)
2552 "*Face used to flash out the search pattern.") 2647 "*Face used to flash out the search pattern.")
2553 )) 2648 ))
2554 2649
2555 2650
2779 (if vip-auto-indent 2874 (if vip-auto-indent
2780 (progn 2875 (progn
2781 (setq vip-cted t) 2876 (setq vip-cted t)
2782 (if vip-electric-mode 2877 (if vip-electric-mode
2783 (indent-according-to-mode) 2878 (indent-according-to-mode)
2784 (indent-to col)) 2879 (indent-to col))))
2785 )) 2880 (vip-change-state-to-insert)))))
2786 (vip-change-state-to-insert)
2787 ))))
2788 2881
2789 (defun vip-Open-line (arg) 2882 (defun vip-Open-line (arg)
2790 "Open line above." 2883 "Open line above."
2791 (interactive "P") 2884 (interactive "P")
2792 (vip-set-complex-command-for-undo) 2885 (vip-set-complex-command-for-undo)
2958 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before) 3051 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2959 (vip-remove-hook 'vip-post-command-hooks 3052 (vip-remove-hook 'vip-post-command-hooks
2960 'vip-replace-state-post-command-sentinel) 3053 'vip-replace-state-post-command-sentinel)
2961 (vip-remove-hook 3054 (vip-remove-hook
2962 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel) 3055 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2963 (vip-restore-cursor-color) 3056 (vip-restore-cursor-color-after-replace)
2964 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it 3057 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2965 (save-excursion 3058 (save-excursion
2966 (if (and 3059 (if (and
2967 vip-replace-overlay 3060 vip-replace-overlay
2968 (>= posn (vip-replace-start)) 3061 (>= posn (vip-replace-start))
3774 (defun vip-line-to-bottom (arg) 3867 (defun vip-line-to-bottom (arg)
3775 "Put current line on the last line." 3868 "Put current line on the last line."
3776 (interactive "p") 3869 (interactive "p")
3777 (recenter (- (window-height) (1+ arg)))) 3870 (recenter (- (window-height) (1+ arg))))
3778 3871
3872 ;; If vip-adjust-window-after-search is t, scroll up or down 1/4 of window
3873 ;; height, depending on whether we are at the bottom or at the top of the
3874 ;; window. This function is called by vip-search (which is called from
3875 ;; vip-search-forward/backward/next)
3876 (defun vip-adjust-window ()
3877 (let ((win-height (if vip-emacs-p
3878 (1- (window-height)) ; adjust for modeline
3879 (window-displayed-height)))
3880 (pt (point))
3881 at-top-p at-bottom-p
3882 min-scroll direction)
3883 (save-excursion
3884 (move-to-window-line 0) ; top
3885 (setq at-top-p (<= (count-lines pt (point)) 2))
3886 (move-to-window-line -1) ; bottom
3887 (setq at-bottom-p (<= (count-lines pt (point)) 2))
3888 )
3889 (cond (at-top-p (setq min-scroll 1
3890 direction 1))
3891 (at-bottom-p (setq min-scroll 2
3892 direction -1)))
3893 (if (and vip-adjust-window-after-search min-scroll)
3894 (recenter
3895 (* (max min-scroll (/ win-height 7)) direction)))
3896 ))
3897
3779 3898
3780 ;; paren match 3899 ;; paren match
3781 ;; must correct this to only match ( to ) etc. On the other hand 3900 ;; must correct this to only match ( to ) etc. On the other hand
3782 ;; it is good that paren match gets confused, because that way you 3901 ;; it is good that paren match gets confused, because that way you
3783 ;; catch _all_ imbalances. 3902 ;; catch _all_ imbalances.
3784 3903
3785 (defun vip-paren-match (arg) 3904 (defun vip-paren-match (arg)
3786 "Go to the matching parenthesis." 3905 "Go to the matching parenthesis."
3787 (interactive "P") 3906 (interactive "P")
3907 (vip-leave-region-active)
3788 (let ((com (vip-getcom arg)) 3908 (let ((com (vip-getcom arg))
3789 anchor-point) 3909 parse-sexp-ignore-comments anchor-point)
3790 (if (integerp arg) 3910 (if (integerp arg)
3791 (if (or (> arg 99) (< arg 1)) 3911 (if (or (> arg 99) (< arg 1))
3792 (error "Prefix must be between 1 and 99") 3912 (error "Prefix must be between 1 and 99")
3793 (goto-char 3913 (goto-char
3794 (if (> (point-max) 80000) 3914 (if (> (point-max) 80000)
3912 4032
3913 ;; scrolling 4033 ;; scrolling
3914 4034
3915 (setq scroll-step 1) 4035 (setq scroll-step 1)
3916 4036
3917 (defun vip-scroll (arg) 4037 (defun vip-scroll-screen (arg)
3918 "Scroll to next screen." 4038 "Scroll to next screen."
3919 (interactive "p") 4039 (interactive "p")
3920 (if (> arg 0) 4040 (condition-case nil
3921 (while (> arg 0) 4041 (if (> arg 0)
3922 (scroll-up) 4042 (while (> arg 0)
3923 (setq arg (1- arg))) 4043 (scroll-up)
3924 (while (> 0 arg) 4044 (setq arg (1- arg)))
3925 (scroll-down) 4045 (while (> 0 arg)
3926 (setq arg (1+ arg))))) 4046 (scroll-down)
3927 4047 (setq arg (1+ arg))))
3928 (defun vip-scroll-back (arg) 4048 (error (beep 1)
4049 (if (> arg 0)
4050 (progn
4051 (message "End of buffer")
4052 (goto-char (point-max)))
4053 (message "Beginning of buffer")
4054 (goto-char (point-min))))
4055 ))
4056
4057 (defun vip-scroll-screen-back (arg)
3929 "Scroll to previous screen." 4058 "Scroll to previous screen."
3930 (interactive "p") 4059 (interactive "p")
3931 (vip-scroll (- arg))) 4060 (vip-scroll-screen (- arg)))
3932 4061
3933 (defun vip-scroll-down (arg) 4062 (defun vip-scroll-down (arg)
3934 "Pull down half screen." 4063 "Pull down half screen."
3935 (interactive "P") 4064 (interactive "P")
3936 (condition-case nil 4065 (condition-case nil
4015 (setq msg "Search becomes vanilla-style"))) 4144 (setq msg "Search becomes vanilla-style")))
4016 (t 4145 (t
4017 (setq msg "Search style remains unchanged"))) 4146 (setq msg "Search style remains unchanged")))
4018 (prin1 msg t))) 4147 (prin1 msg t)))
4019 4148
4149 (defun vip-set-vi-search-style-macros (unset)
4150 "Set the macros for toggling the search style in Viper's vi-state.
4151 The macro that toggles case sensitivity is bound to `//', and the one that
4152 toggles regexp search is bound to `///'.
4153 With a prefix argument, this function unsets the macros. "
4154 (interactive "P")
4155 (or noninteractive
4156 (if (not unset)
4157 (progn
4158 ;; toggle case sensitivity in search
4159 (vip-record-kbd-macro
4160 "//" 'vi-state
4161 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
4162 't)
4163 ;; toggle regexp/vanila search
4164 (vip-record-kbd-macro
4165 "///" 'vi-state
4166 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
4167 't)
4168 (if (interactive-p)
4169 (message
4170 "// and /// now toggle case-sensitivity and regexp search.")))
4171 (vip-unrecord-kbd-macro "//" 'vi-state)
4172 (sit-for 2)
4173 (vip-unrecord-kbd-macro "///" 'vi-state))))
4174
4175 (defun vip-set-emacs-search-style-macros (unset &optional arg-majormode)
4176 "Set the macros for toggling the search style in Viper's emacs-state.
4177 The macro that toggles case sensitivity is bound to `//', and the one that
4178 toggles regexp search is bound to `///'.
4179 With a prefix argument, this function unsets the macros.
4180 If the optional prefix argument is non-nil and specifies a valid major mode,
4181 this sets the macros only in the macros in that major mode. Otherwise,
4182 the macros are set in the current major mode.
4183 \(When unsetting the macros, the second argument has no effect.\)"
4184 (interactive "P")
4185 (or noninteractive
4186 (if (not unset)
4187 (progn
4188 ;; toggle case sensitivity in search
4189 (vip-record-kbd-macro
4190 "//" 'emacs-state
4191 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
4192 (or arg-majormode major-mode))
4193 ;; toggle regexp/vanila search
4194 (vip-record-kbd-macro
4195 "///" 'emacs-state
4196 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
4197 (or arg-majormode major-mode))
4198 (if (interactive-p)
4199 (message
4200 "// and /// now toggle case-sensitivity and regexp search.")))
4201 (vip-unrecord-kbd-macro "//" 'emacs-state)
4202 (sit-for 2)
4203 (vip-unrecord-kbd-macro "///" 'emacs-state))))
4204
4020 4205
4021 (defun vip-search-forward (arg) 4206 (defun vip-search-forward (arg)
4022 "Search a string forward. 4207 "Search a string forward.
4023 ARG is used to find the ARG's occurrence of the string. 4208 ARG is used to find the ARG's occurrence of the string.
4024 Null string will repeat previous search." 4209 Null string will repeat previous search."
4083 (progn 4268 (progn
4084 (re-search-forward string nil nil val) 4269 (re-search-forward string nil nil val)
4085 (re-search-backward string)) 4270 (re-search-backward string))
4086 (search-forward string nil nil val) 4271 (search-forward string nil nil val)
4087 (search-backward string)) 4272 (search-backward string))
4088 ;; don't wait and don't flash in macros
4089 (or executing-kbd-macro
4090 (vip-flash-search-pattern))
4091 (if (not (equal start-point (point))) 4273 (if (not (equal start-point (point)))
4092 (push-mark start-point t))) 4274 (push-mark start-point t)))
4093 (search-failed 4275 (search-failed
4094 (if (and (not fail-if-not-found) vip-search-wrap-around-t) 4276 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4095 (progn 4277 (progn
4096 (message "Search wrapped around end of buffer") 4278 (message "Search wrapped around BOTTOM of buffer")
4097 (goto-char (point-min)) 4279 (goto-char (point-min))
4098 (vip-search string forward (cons 1 com) t start-point 'fail) 4280 (vip-search string forward (cons 1 com) t start-point 'fail)
4099 ;; don't wait in macros 4281 ;; don't wait in macros
4100 (or executing-kbd-macro (sit-for 2)) 4282 (or executing-kbd-macro (sit-for 2))
4101 ;; delete the wrap-around message 4283 ;; delete the wrap-around message
4110 (condition-case nil 4292 (condition-case nil
4111 (progn 4293 (progn
4112 (if vip-re-search 4294 (if vip-re-search
4113 (re-search-backward string nil nil val) 4295 (re-search-backward string nil nil val)
4114 (search-backward string nil nil val)) 4296 (search-backward string nil nil val))
4115 ;; don't wait and don't flash in macros
4116 (or executing-kbd-macro
4117 (vip-flash-search-pattern))
4118 (if (not (equal start-point (point))) 4297 (if (not (equal start-point (point)))
4119 (push-mark start-point t))) 4298 (push-mark start-point t)))
4120 (search-failed 4299 (search-failed
4121 (if (and (not fail-if-not-found) vip-search-wrap-around-t) 4300 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4122 (progn 4301 (progn
4123 (message "Search wrapped around beginning of buffer") 4302 (message "Search wrapped around TOP of buffer")
4124 (goto-char (point-max)) 4303 (goto-char (point-max))
4125 (vip-search string forward (cons 1 com) t start-point 'fail) 4304 (vip-search string forward (cons 1 com) t start-point 'fail)
4126 ;; don't wait in macros 4305 ;; don't wait in macros
4127 (or executing-kbd-macro (sit-for 2)) 4306 (or executing-kbd-macro (sit-for 2))
4128 ;; delete the wrap-around message 4307 ;; delete the wrap-around message
4130 ) 4309 )
4131 (goto-char start-point) 4310 (goto-char start-point)
4132 (error "`%s': %s not found" 4311 (error "`%s': %s not found"
4133 string 4312 string
4134 (if vip-re-search "Pattern" "String")) 4313 (if vip-re-search "Pattern" "String"))
4135 ))))))) 4314 ))))
4315 ;; pull up or down if at top/bottom of window
4316 (vip-adjust-window)
4317 ;; highlight the result of search
4318 ;; don't wait and don't highlight in macros
4319 (or executing-kbd-macro
4320 (vip-flash-search-pattern))
4321 )))
4136 4322
4137 (defun vip-search-next (arg) 4323 (defun vip-search-next (arg)
4138 "Repeat previous search." 4324 "Repeat previous search."
4139 (interactive "P") 4325 (interactive "P")
4140 (let ((val (vip-p-val arg)) 4326 (let ((val (vip-p-val arg))
4336 (error vip-EmptyRegister reg)) 4522 (error vip-EmptyRegister reg))
4337 (error ""))) 4523 (error "")))
4338 (setq vip-use-register nil) 4524 (setq vip-use-register nil)
4339 (if (vip-end-with-a-newline-p text) 4525 (if (vip-end-with-a-newline-p text)
4340 (progn 4526 (progn
4527 (end-of-line)
4341 (if (eobp) 4528 (if (eobp)
4342 (insert "\n") 4529 (insert "\n")
4343 (forward-line 1)) 4530 (forward-line 1))
4344 (beginning-of-line)) 4531 (beginning-of-line))
4345 (if (not (eolp)) (vip-forward-char-carefully))) 4532 (if (not (eolp)) (vip-forward-char-carefully)))
4750 ((and (not skip-white) (= char ?`)) 4937 ((and (not skip-white) (= char ?`))
4751 (if com (vip-move-marker-locally 'vip-com-point (point))) 4938 (if com (vip-move-marker-locally 'vip-com-point (point)))
4752 (if (and (vip-same-line (point) vip-last-jump) 4939 (if (and (vip-same-line (point) vip-last-jump)
4753 (= (point) vip-last-jump-ignore)) 4940 (= (point) vip-last-jump-ignore))
4754 (goto-char vip-last-jump)) 4941 (goto-char vip-last-jump))
4942 (if (null (mark t)) (error "Mark is not set in this buffer"))
4755 (if (= (point) (mark t)) (pop-mark)) 4943 (if (= (point) (mark t)) (pop-mark))
4756 (exchange-point-and-mark) 4944 (exchange-point-and-mark)
4757 (setq vip-last-jump (point-marker) 4945 (setq vip-last-jump (point-marker)
4758 vip-last-jump-ignore 0) 4946 vip-last-jump-ignore 0)
4759 (if com (vip-execute-com 'vip-goto-mark nil com))) 4947 (if com (vip-execute-com 'vip-goto-mark nil com)))
4826 (if (memq last-command 5014 (if (memq last-command
4827 '(vip-autoindent 5015 '(vip-autoindent
4828 vip-open-line vip-Open-line 5016 vip-open-line vip-Open-line
4829 vip-replace-state-exit-cmd)) 5017 vip-replace-state-exit-cmd))
4830 (indent-to-left-margin)) 5018 (indent-to-left-margin))
4831 (newline 1) 5019 ;; use \n instead of newline, or else <Return> will move the insert point
5020 ;;(newline 1)
5021 (insert "\n")
4832 (if vip-auto-indent 5022 (if vip-auto-indent
4833 (progn 5023 (progn
4834 (setq vip-cted t) 5024 (setq vip-cted t)
4835 (if vip-electric-mode 5025 (if vip-electric-mode
4836 (indent-according-to-mode) 5026 (indent-according-to-mode)
4950 (setq vip-always t 5140 (setq vip-always t
4951 vip-ex-style-motion t 5141 vip-ex-style-motion t
4952 vip-ex-style-editing-in-insert t 5142 vip-ex-style-editing-in-insert t
4953 vip-want-ctl-h-help nil) 5143 vip-want-ctl-h-help nil)
4954 5144
4955 (cond 5145 (cond ((eq vip-expert-level 1) ; novice or beginner
4956 ;; a novice or a beginner 5146 (global-set-key ; in emacs-state
4957 ((eq vip-expert-level 1) 5147 vip-toggle-key
4958 (global-set-key vip-toggle-key ;; in emacs-state 5148 (if (vip-window-display-p) 'vip-iconify 'suspend-emacs))
4959 (if (vip-window-display-p)
4960 'vip-iconify
4961 'suspend-emacs))
4962 (setq vip-no-multiple-ESC t 5149 (setq vip-no-multiple-ESC t
4963 vip-re-search t 5150 vip-re-search t
4964 vip-vi-style-in-minibuffer t 5151 vip-vi-style-in-minibuffer t
4965 vip-search-wrap-around-t t 5152 vip-search-wrap-around-t t
4966 vip-want-emacs-keys-in-vi nil 5153 vip-want-emacs-keys-in-vi nil
4967 vip-want-emacs-keys-in-insert nil)) 5154 vip-want-emacs-keys-in-insert nil))
4968 5155
4969 ;; an intermediate to guru
4970 ((and (> vip-expert-level 1) (< vip-expert-level 5)) 5156 ((and (> vip-expert-level 1) (< vip-expert-level 5))
5157 ;; an intermediate to guru
4971 (setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice) 5158 (setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice)
4972 vip-want-emacs-keys-in-vi t 5159 vip-want-emacs-keys-in-vi t
4973 vip-want-emacs-keys-in-insert (> vip-expert-level 2)) 5160 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4974 5161
4975 (if (eq vip-expert-level 4) ; respect user's ex-style motions 5162 (if (eq vip-expert-level 4) ; respect user's ex-style motions
4990 (cdr (assoc 'vip-re-search vip-saved-user-settings)) 5177 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4991 vip-no-multiple-ESC 5178 vip-no-multiple-ESC
4992 (cdr (assoc 'vip-no-multiple-ESC 5179 (cdr (assoc 'vip-no-multiple-ESC
4993 vip-saved-user-settings)))))) 5180 vip-saved-user-settings))))))
4994 5181
4995 ;; A wizard 5182 ;; A wizard!!
4996 ;; Ideally, if 5 is selected, a buffer should pop up to let the 5183 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4997 ;; user toggle variable values. 5184 ;; user toggle the values of variables.
4998 (t (setq-default vip-ex-style-editing-in-insert 5185 (t (setq-default vip-ex-style-editing-in-insert
4999 (cdr (assoc 'vip-ex-style-editing-in-insert 5186 (cdr (assoc 'vip-ex-style-editing-in-insert
5000 vip-saved-user-settings)) 5187 vip-saved-user-settings))
5001 vip-ex-style-motion 5188 vip-ex-style-motion
5002 (cdr (assoc 'vip-ex-style-motion 5189 (cdr (assoc 'vip-ex-style-motion
5372 5559
5373 ;;; Load .vip and set up hooks 5560 ;;; Load .vip and set up hooks
5374 5561
5375 ;; This hook designed to enable Vi-style editing in comint-based modes." 5562 ;; This hook designed to enable Vi-style editing in comint-based modes."
5376 (defun vip-comint-mode-hook () 5563 (defun vip-comint-mode-hook ()
5377 (setq require-final-newline nil) 5564 (setq require-final-newline nil
5378 (setq vip-ex-style-editing-in-insert nil 5565 vip-ex-style-editing-in-insert nil
5379 vip-ex-style-motion nil) 5566 vip-ex-style-motion nil)
5380 (vip-add-local-keys 'vi-state 5567 (vip-change-state-to-insert))
5381 '(("\C-m" . comint-send-input) ; return 5568
5382 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5383 (vip-add-local-keys 'insert-state
5384 '(("\C-m" . comint-send-input) ; return
5385 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5386 )
5387
5388 5569
5389 ;; This sets major mode hooks to make them come up in vi-state. 5570 ;; This sets major mode hooks to make them come up in vi-state.
5390 (defun vip-set-hooks () 5571 (defun vip-set-hooks ()
5391 5572
5392 ;; It is of course a misnomer to call viper-mode a `major mode'. 5573 ;; It is of course a misnomer to call viper-mode a `major mode'.
5411 5592
5412 (defvar html-mode-hook) 5593 (defvar html-mode-hook)
5413 (add-hook 'html-mode-hook 'viper-mode) 5594 (add-hook 'html-mode-hook 'viper-mode)
5414 (defvar html-helper-mode-hook) 5595 (defvar html-helper-mode-hook)
5415 (add-hook 'html-helper-mode-hook 'viper-mode) 5596 (add-hook 'html-helper-mode-hook 'viper-mode)
5416 5597 (defvar java-mode-hook)
5598 (add-hook 'java-mode-hook 'viper-mode)
5599
5417 (defvar emacs-lisp-mode-hook) 5600 (defvar emacs-lisp-mode-hook)
5418 (add-hook 'emacs-lisp-mode-hook 'viper-mode) 5601 (add-hook 'emacs-lisp-mode-hook 'viper-mode)
5419 5602
5420 (defvar lisp-mode-hook) 5603 (defvar lisp-mode-hook)
5421 (add-hook 'lisp-mode-hook 'viper-mode) 5604 (add-hook 'lisp-mode-hook 'viper-mode)
5436 (add-hook 'lisp-interaction-mode-hook 'viper-mode) 5619 (add-hook 'lisp-interaction-mode-hook 'viper-mode)
5437 5620
5438 (defvar fortran-mode-hook) 5621 (defvar fortran-mode-hook)
5439 (add-hook 'fortran-mode-hook 'vip-mode) 5622 (add-hook 'fortran-mode-hook 'vip-mode)
5440 5623
5624 (defvar basic-mode-hook)
5625 (add-hook 'basic-mode-hook 'vip-mode)
5626 (defvar bat-mode-hook)
5627 (add-hook 'bat-mode-hook 'vip-mode)
5628
5441 (defvar text-mode-hook) 5629 (defvar text-mode-hook)
5442 (add-hook 'text-mode-hook 'viper-mode) 5630 (add-hook 'text-mode-hook 'viper-mode)
5443 5631
5444 (add-hook 'completion-list-mode-hook 'viper-mode) 5632 (add-hook 'completion-list-mode-hook 'viper-mode)
5445 (add-hook 'compilation-mode-hook 'viper-mode) 5633 (add-hook 'compilation-mode-hook 'viper-mode)
5447 (add-hook 'perl-mode-hook 'viper-mode) 5635 (add-hook 'perl-mode-hook 'viper-mode)
5448 (add-hook 'tcl-mode-hook 'viper-mode) 5636 (add-hook 'tcl-mode-hook 'viper-mode)
5449 5637
5450 (defvar emerge-startup-hook) 5638 (defvar emerge-startup-hook)
5451 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs) 5639 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
5452 ;; Run vip-change-state-to-vi after quitting emerge. 5640
5641 ;; Tell vc-diff to put *vc* in Vi mode
5642 (if (featurep 'vc)
5643 (defadvice vc-diff (after vip-vc-ad activate)
5644 "Force Vi state in VC diff buffer."
5645 (vip-change-state-to-vi))
5646 (vip-eval-after-load
5647 "vc"
5648 '(defadvice vc-diff (after vip-vc-ad activate)
5649 "Force Vi state in VC diff buffer."
5650 (vip-change-state-to-vi))))
5651
5453 (vip-eval-after-load 5652 (vip-eval-after-load
5454 "emerge" 5653 "emerge"
5455 '(defadvice emerge-quit (after vip-emerge-advice activate) 5654 '(defadvice emerge-quit (after vip-emerge-advice activate)
5456 "Run `vip-change-state-to-vi' after quitting emerge." 5655 "Run `vip-change-state-to-vi' after quitting emerge."
5457 (vip-change-state-to-vi))) 5656 (vip-change-state-to-vi)))
5480 "Switch to Vi state in Prolog mode." 5679 "Switch to Vi state in Prolog mode."
5481 (vip-change-state-to-vi))) 5680 (vip-change-state-to-vi)))
5482 5681
5483 ;; Emacs shell, ange-ftp, and comint-based modes 5682 ;; Emacs shell, ange-ftp, and comint-based modes
5484 (defvar comint-mode-hook) 5683 (defvar comint-mode-hook)
5485 (add-hook 'comint-mode-hook 'vip-change-state-to-insert) 5684 (vip-modify-major-mode
5685 'comint-mode 'insert-state vip-comint-mode-modifier-map)
5686 (vip-modify-major-mode
5687 'comint-mode 'vi-state vip-comint-mode-modifier-map)
5688 (vip-modify-major-mode
5689 'shell-mode 'insert-state vip-comint-mode-modifier-map)
5690 (vip-modify-major-mode
5691 'shell-mode 'vi-state vip-comint-mode-modifier-map)
5692 ;; ange-ftp in XEmacs
5693 (vip-modify-major-mode
5694 'ange-ftp-shell-mode 'insert-state vip-comint-mode-modifier-map)
5695 (vip-modify-major-mode
5696 'ange-ftp-shell-mode 'vi-state vip-comint-mode-modifier-map)
5697 ;; ange-ftp in Emacs
5698 (vip-modify-major-mode
5699 'internal-ange-ftp-mode 'insert-state vip-comint-mode-modifier-map)
5700 (vip-modify-major-mode
5701 'internal-ange-ftp-mode 'vi-state vip-comint-mode-modifier-map)
5702 ;; set hook
5486 (add-hook 'comint-mode-hook 'vip-comint-mode-hook) 5703 (add-hook 'comint-mode-hook 'vip-comint-mode-hook)
5487 5704
5488 ;; Shell scripts 5705 ;; Shell scripts
5489 (defvar sh-mode-hook) 5706 (defvar sh-mode-hook)
5490 (add-hook 'sh-mode-hook 'viper-mode) 5707 (add-hook 'sh-mode-hook 'viper-mode)
5491 (defvar ksh-mode-hook) 5708 (defvar ksh-mode-hook)
5492 (add-hook 'ksh-mode-hook 'viper-mode) 5709 (add-hook 'ksh-mode-hook 'viper-mode)
5493 5710
5494 ;; Dired 5711 ;; Dired
5495 ;; This is only necessary when the user uses vip-modify-major-mode 5712 (vip-modify-major-mode 'dired-mode 'emacs-state vip-dired-modifier-map)
5713 (vip-set-emacs-search-style-macros nil 'dired-mode)
5496 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs) 5714 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
5497 5715
5716 ;; Tar
5717 (vip-modify-major-mode 'tar-mode 'emacs-state vip-slash-and-colon-map)
5718 (vip-set-emacs-search-style-macros nil 'tar-mode)
5719
5720 ;; MH-E
5721 (vip-modify-major-mode 'mh-folder-mode 'emacs-state vip-slash-and-colon-map)
5722 (vip-set-emacs-search-style-macros nil 'mh-folder-mode)
5723 ;; changing state to emacs is needed so the preceding will take hold
5724 (add-hook 'mh-folder-mode-hook 'vip-change-state-to-emacs)
5725 (add-hook 'mh-show-mode-hook 'viper-mode)
5726
5727 ;; Gnus
5728 (vip-modify-major-mode 'gnus-group-mode 'emacs-state vip-slash-and-colon-map)
5729 (vip-set-emacs-search-style-macros nil 'gnus-group-mode)
5730 (vip-modify-major-mode
5731 'gnus-summary-mode 'emacs-state vip-slash-and-colon-map)
5732 (vip-set-emacs-search-style-macros nil 'gnus-summary-mode)
5733 ;; changing state to emacs is needed so the preceding will take hold
5734 (add-hook 'gnus-group-mode-hook 'vip-change-state-to-emacs)
5735 (add-hook 'gnus-summary-mode-hook 'vip-change-state-to-emacs)
5736 (add-hook 'gnus-article-mode-hook 'viper-mode)
5737
5738 ;; Info
5739 (vip-modify-major-mode 'Info-mode 'emacs-state vip-slash-and-colon-map)
5740 (vip-set-emacs-search-style-macros nil 'Info-mode)
5741 ;; Switching to emacs is needed so the above will take hold
5742 (defadvice Info-mode (after vip-Info-ad activate)
5743 "Switch to emacs mode."
5744 (vip-change-state-to-emacs))
5745
5746 ;; Buffer menu
5747 (vip-modify-major-mode
5748 'Buffer-menu-mode 'emacs-state vip-slash-and-colon-map)
5749 (vip-set-emacs-search-style-macros nil 'Buffer-menu-mode)
5750 ;; Switching to emacs is needed so the above will take hold
5751 (defadvice Buffer-menu-mode (after vip-Buffer-menu-ad activate)
5752 "Switch to emacs mode."
5753 (vip-change-state-to-emacs))
5754
5755 ;; View mode
5498 (if vip-emacs-p 5756 (if vip-emacs-p
5499 (progn 5757 (progn
5500 (defvar view-mode-hook) 5758 (defvar view-mode-hook)
5501 (add-hook 'view-mode-hook 'vip-change-state-to-emacs)) 5759 (add-hook 'view-mode-hook 'vip-change-state-to-emacs))
5502 (defadvice view-minor-mode (after vip-view-ad activate) 5760 (defadvice view-minor-mode (after vip-view-ad activate)
5535 ;; repeat the 3d previous command without rotating the command history 5793 ;; repeat the 3d previous command without rotating the command history
5536 (vip-record-kbd-macro 5794 (vip-record-kbd-macro
5537 (vector vip-repeat-from-history-key '\2) 'vi-state 5795 (vector vip-repeat-from-history-key '\2) 'vi-state
5538 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't) 5796 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5539 5797
5540 ;; toggle case sensitivity in search 5798 ;; set the toggle case sensitivity and regexp search macros
5541 (vip-record-kbd-macro 5799 (vip-set-vi-search-style-macros nil)
5542 "//" 'vi-state 5800
5543 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't)
5544 ;; toggle regexp/vanila search
5545 (vip-record-kbd-macro
5546 "///" 'vi-state
5547 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return] 't)
5548
5549 5801
5550 ;; ~/.vip is loaded if it exists 5802 ;; ~/.vip is loaded if it exists
5551 (if (and (file-exists-p vip-custom-file-name) 5803 (if (and (file-exists-p vip-custom-file-name)
5552 (not noninteractive)) 5804 (not noninteractive))
5553 (load vip-custom-file-name)) 5805 (load vip-custom-file-name))
5573 5825
5574 5826
5575 (vip-set-minibuffer-style) 5827 (vip-set-minibuffer-style)
5576 (vip-set-minibuffer-faces) 5828 (vip-set-minibuffer-faces)
5577 (vip-set-search-face) 5829 (vip-set-search-face)
5830 (vip-set-replace-overlay-face)
5578 (if vip-buffer-search-char 5831 (if vip-buffer-search-char
5579 (vip-buffer-search-enable)) 5832 (vip-buffer-search-enable))
5580 (vip-update-alphanumeric-class) 5833 (vip-update-alphanumeric-class)
5581 5834
5582 ;;; Familiarize Viper with some minor modes that have their own keymaps 5835 ;;; Familiarize Viper with some minor modes that have their own keymaps
5601 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key) 5854 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5602 5855
5603 ;; This is taken care of by vip-insert-global-user-map. 5856 ;; This is taken care of by vip-insert-global-user-map.
5604 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key) 5857 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
5605 5858
5606 (define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC) 5859
5607 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or 5860 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
5608 ;; iconifies Emacs 5861 ;; iconifies Emacs
5609 (define-key vip-vi-intercept-map vip-toggle-key 5862 (define-key vip-vi-intercept-map vip-toggle-key 'vip-toggle-key-action)
5610 '(lambda () (interactive)
5611 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
5612 (if (vip-window-display-p) (vip-iconify) (suspend-emacs))
5613 (vip-change-state-to-emacs))))
5614
5615 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi) 5863 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
5616 5864
5617 5865
5618 (if (or vip-always 5866 (if (or vip-always
5619 (and (< vip-expert-level 5) (> vip-expert-level 0))) 5867 (and (< vip-expert-level 5) (> vip-expert-level 0)))