new version
[emacs.git] / lisp / emulation / viper.el
blob6a8e9e4e717bf2b84f6df76298af919361a12156
1 ;;; viper.el --- A full-featured Vi emulator for GNU Emacs 19 and XEmacs 19,
2 ;; a VI Plan for Emacs Rescue,
3 ;; and a venomous VI PERil.
4 ;; Viper Is also a Package for Emacs Rebels.
5 ;;
6 ;; Keywords: emulations
7 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
9 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
11 (defconst viper-version "2.94 of June 2, 1997"
12 "The current version of Viper")
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
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 the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
31 ;;; Commentary:
33 ;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and
34 ;; improves upon the standard features of Vi and, at the same time, allows
35 ;; full access to all Emacs facilities. Viper supports multiple undo,
36 ;; file name completion, command, file, and search history and it extends
37 ;; Vi in many other ways. Viper is highly customizable through the various
38 ;; hooks, user variables, and keymaps. It is implemented as a collection
39 ;; of minor modes and it is designed to provide full access to all Emacs
40 ;; major and minor modes.
42 ;;; History
44 ;; Viper is a new name for a package formerly known as VIP-19,
45 ;; which was a successor of VIP version 3.5 by Masahiko Sato
46 ;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
47 ;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
48 ;; were also shamelessly plagiarized.
50 ;; Viper maintains some degree of compatibility with these older
51 ;; packages. See the documentation for customization.
53 ;; The main difference between Viper and these older packages are:
55 ;; 1. Viper emulates Vi at several levels, from almost complete conformity
56 ;; to a rather loose Vi-compliance.
58 ;; 2. Viper provides full access to all major and minor modes of Emacs
59 ;; without the need to type extra keys.
60 ;; The older versions of VIP (and other Vi emulators) do not work with
61 ;; some major and minor modes.
63 ;; 3. Viper supports vi-style undo.
65 ;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
67 ;; 5. Viper has a better interface to ex, including command, variable, and
68 ;; file name completion.
70 ;; 6. Viper uses native Emacs history and completion features; it doesn't
71 ;; rely on other packages (such as gmhist.el and completer.el) to provide
72 ;; these features.
74 ;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
75 ;; user to switch from Insert state to Vi state to Replace state, etc.
77 ;; 8. Viper keeps history of recently inserted pieces of text and recently
78 ;; executed Vi-style destructive commands, such as `i', `d', etc.
79 ;; These pieces of text can be inserted in later insertion commands;
80 ;; the previous destructive commands can be re-executed.
82 ;; 9. Viper has Vi-style keyboard macros, which enhances the similar
83 ;; facility in the original Vi.
84 ;; First, one can execute any Emacs command while defining a
85 ;; macro, not just the Vi commands. Second, macros are defined in a
86 ;; WYSYWYG mode, using an interface to Emacs' WYSIWYG style of defining
87 ;; macros. Third, in Viper, one can define macros that are specific to
88 ;; a given buffer, a given major mode, or macros defined for all buffers.
89 ;; The same macro name can have several different definitions:
90 ;; one global, several definitions for various major modes, and
91 ;; definitions for specific buffers.
92 ;; Buffer-specific definitions override mode-specific
93 ;; definitions, which, in turn, override global definitions.
96 ;;; Installation:
97 ;; -------------
99 ;; (require 'viper)
102 ;;; Acknowledgements:
103 ;; -----------------
104 ;; Bug reports and ideas contributed by many users have helped
105 ;; improve Viper and the various versions of VIP.
106 ;; See the on-line manual for a complete list of contributors.
109 ;;; Notes:
111 ;; 1. Major modes.
112 ;; In most cases, Viper handles major modes correctly, i.e., they come up
113 ;; in the right state (either vi-state or emacs-state). For instance, text
114 ;; files come up in vi-state, while, say, Dired appears in emacs-state by
115 ;; default.
116 ;; However, some modes do not appear in the right mode in the beginning,
117 ;; usually because they neglect to follow Emacs conventions (e.g., they don't
118 ;; use kill-all-local-variables when they start). Some major modes
119 ;; may fail to come up in emacs-state if they call hooks, such as
120 ;; text-hook, for no good reason.
122 ;; As an immediate solution, you can hit C-z to bring about the right mode.
123 ;; An interim solution is to add an appropriate hook to the mode like this:
125 ;; (add-hook 'your-favorite-mode 'viper-mode)
126 ;; or
127 ;; (add-hook 'your-favorite-mode 'vip-change-state-to-emacs)
129 ;; whichever applies. The right thing to do, however, is to complain to the
130 ;; author of the respective package. (Sometimes they also neglect to equip
131 ;; their modes with hooks, which is one more reason for complaining.)
133 ;; 2. Keymap handling
134 ;; Because Emacs 19 has an elegant mechanism for turning minor mode keymaps
135 ;; on and off, implementation of Viper has been greatly simplified. Viper
136 ;; has several minor modes.
138 ;; Viper's Vi state consists of seven minor modes:
140 ;; vip-vi-intercept-minor-mode
141 ;; vip-vi-local-user-minor-mode
142 ;; vip-vi-global-user-minor-mode
143 ;; vip-vi-kbd-minor-mode
144 ;; vip-vi-state-modifier-minor-mode
145 ;; vip-vi-diehard-minor-mode
146 ;; vip-vi-basic-minor-mode
148 ;; Bindings done to the keymap of the first mode overshadow those done to
149 ;; the second, which, in turn, overshadows those done to the third, etc.
151 ;; The last vip-vi-basic-minor-mode contains most of the usual Vi bindings
152 ;; in its edit mode. This mode provides access to all Emacs facilities.
153 ;; Novice users, however, may want to set their vip-expert-level to 1
154 ;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This
155 ;; minor mode's bindings make Viper simulate the usual Vi very closely.
156 ;; For instance, C-c will not have its standard Emacs binding
157 ;; and so many of the goodies of Emacs are not available.
159 ;; A skilled user should set vip-expert-level to at least 3. This will
160 ;; enable `C-c' and many Emacs facilities will become available.
161 ;; In this case, vip-vi-diehard-minor-mode is inactive.
163 ;; Viper gurus should have at least
164 ;; (setq vip-expert-level 4)
165 ;; in their ~/.vip files. This will unsuppress all Emacs keys that are not
166 ;; essential for VI-style editing.
167 ;; Pick-and-choose users may want to put
168 ;; (setq vip-expert-level 5)
169 ;; in ~/.vip. Viper will then leave it up to the user to set the variables
170 ;; vip-want-* See vip-set-expert-level for details.
172 ;; The very first minor mode, vip-vi-intercept-minor-mode, is of no
173 ;; concern for the user. It is needed to bind Viper's vital keys, such as
174 ;; ESC and C-z.
176 ;; The second mode, vip-vi-local-user-minor-mode, usually has an
177 ;; empty keymap. However, the user can set bindings in this keymap, which
178 ;; will overshadow the corresponding bindings in the other two minor
179 ;; modes. This is useful, for example, for setting up ZZ in gnus,
180 ;; rmail, mh-e, etc., to send message instead of saving it in a file.
181 ;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
182 ;; that would visit the next or the previous file in the Dired buffer.
183 ;; Setting local keys is tricky, so don't do it directly. Instead, use
184 ;; vip-add-local-keys function (see its doc).
186 ;; The third minor mode, vip-vi-global-user-minor-mode, is also intended
187 ;; for the users but, unlike vip-vi-local-user-minor-mode, its key
188 ;; bindings are seen in all Viper buffers. This mode keys can be done
189 ;; with define-key command.
191 ;; The fourth minor mode, vip-vi-kbd-minor-mode, is used by keyboard
192 ;; macros. Users are NOT supposed to modify this keymap directly.
194 ;; The fifth mode, vip-vi-state-modifier-minor-mode, can be used to set
195 ;; key bindings that are visible in some major modes but not in others.
197 ;; Users are allowed to modify keymaps that belong to
198 ;; vip-vi-local-user-minor-mode, vip-vi-global-user-minor-mode,
199 ;; and vip-vi-state-modifier-minor-mode only.
201 ;; Viper's Insert state also has seven minor modes:
203 ;; vip-insert-intercept-minor-mode
204 ;; vip-insert-local-user-minor-mode
205 ;; vip-insert-global-user-minor-mode
206 ;; vip-insert-kbd-minor-mode
207 ;; vip-insert-state-modifier-minor-mode
208 ;; vip-insert-diehard-minor-mode
209 ;; vip-insert-basic-minor-mode
211 ;; As with VI's editing modes, the first mode, vip-insert-intercept-minor-mode
212 ;; is used to bind vital keys that are not to be changed by the user.
214 ;; The next mode, vip-insert-local-user-minor-mode, is used to customize
215 ;; bindings in the insert state of Viper. The third mode,
216 ;; vip-insert-global-user-minor-mode is like
217 ;; vip-insert-local-user-minor-mode, except that its bindings are seen in
218 ;; all Viper buffers. As with vip-vi-local-user-minor-mode, its bindings
219 ;; should be done via the function vip-add-local-keys. Bindings for
220 ;; vip-insert-global-user-minor-mode can be set with the define-key command.
222 ;; The next minor mode, vip-insert-kbd-minor-mode,
223 ;; is used for keyboard VI-style macros defined with :map!.
225 ;; The fifth minor mode, vip-insert-state-modifier-minor-mode, is like
226 ;; vip-vi-state-modifier-minor-mode, except that it is used in the Insert
227 ;; state; it can be used to modify keys in a mode-specific fashion.
229 ;; The minor mode vip-insert-diehard-minor-mode is in effect when
230 ;; the user wants a high degree of Vi compatibility (a bad idea, really!).
231 ;; The last minor mode, vip-insert-basic-minor-mode, is always in effect
232 ;; when Viper is in insert state. It binds a small number of keys needed for
233 ;; Viper's operation.
235 ;; Finally, Viper provides minor modes for overriding bindings set by Emacs
236 ;; modes when Viper is in Emacs state:
238 ;; vip-emacs-local-user-minor-mode
239 ;; vip-emacs-global-user-minor-mode
240 ;; vip-emacs-kbd-minor-mode
241 ;; vip-emacs-state-modifier-minor-mode
243 ;; These minor modes are in effect when Viper is in Emacs state. The keymap
244 ;; associated with vip-emacs-global-user-minor-mode,
245 ;; vip-emacs-global-user-map, overrides the global and local keymaps as
246 ;; well as the minor mode keymaps set by other modes. The keymap of
247 ;; vip-emacs-local-user-minor-mode, vip-emacs-local-user-map, overrides
248 ;; everything, but it is used on a per buffer basis.
249 ;; The keymap associated with vip-emacs-state-modifier-minor-mode
250 ;; overrides keys on a per-major-mode basis. The mode
251 ;; vip-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
252 ;; state.
254 ;; 3. There is also one minor mode that is used when Viper is in its
255 ;; replace-state (used for commands like cw, C, etc.). This mode is
256 ;; called
258 ;; vip-replace-minor-mode
260 ;; and its keymap is vip-replace-map. Replace minor mode is always
261 ;; used in conjunction with the minor modes for insert-state, and its
262 ;; keymap overshadows the keymaps for insert minor modes.
264 ;; 4. Defining buffer-local bindings in Vi and Insert modes.
265 ;; As mentioned before, sometimes, it is convenient to have
266 ;; buffer-specific of mode-specific key bindings in Vi and insert modes.
267 ;; Viper provides a special function, vip-add-local-keys, to do precisely
268 ;; this. For instance, is you need to add couple of mode-specific bindings
269 ;; to Insert mode, you can put
271 ;; (vip-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
273 ;; somewhere in a hook of this major mode. If you put something like this
274 ;; in your own elisp function, this will define bindings specific to the
275 ;; buffer that was current at the time of the call to vip-add-local-keys.
276 ;; The only thing to make sure here is that the major mode of this buffer
277 ;; is written according to Emacs conventions, which includes a call to
278 ;; (kill-all-local-variables). See vip-add-local-keys for more details.
281 ;; TO DO (volunteers?):
283 ;; 1. Some of the code that is inherited from VIP-3.5 is rather
284 ;; convoluted. Instead of vip-command-argument, keymaps should bind the
285 ;; actual commands. E.g., "dw" should be bound to a generic command
286 ;; vip-delete that will delete things based on the value of
287 ;; last-command-char. This would greatly simplify the logic and the code.
289 ;; 2. Somebody should venture to write a customization package a la
290 ;; options.el that would allow the user to change values of variables
291 ;; that meet certain specs (e.g., match a regexp) and whose doc string
292 ;; starts with a '*'. Then, the user should be offered to save
293 ;; variables that were changed. This will make user's customization job
294 ;; much easier.
297 ;; Code
299 (require 'advice)
300 (require 'cl)
301 (require 'ring)
303 (eval-when-compile
304 (let ((load-path (cons (expand-file-name ".") load-path)))
305 (or (featurep 'viper-cmd)
306 (load "viper-cmd.el" nil nil 'nosuffix))
309 (require 'viper-cmd)
311 ;; Viper version
312 (defun viper-version ()
313 (interactive)
314 (message "Viper version is %s" viper-version))
316 (defalias 'vip-version 'viper-version)
319 ;; The following is provided for compatibility with older VIP's
321 (defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
322 (defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert)
323 (defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs)
326 ;; Viper changes the default mode-line-buffer-identification
327 (setq-default mode-line-buffer-identification '(" %b"))
329 ;; Variable displaying the current Viper state in the mode line.
330 (vip-deflocalvar vip-mode-string vip-emacs-state-id)
331 (or (memq 'vip-mode-string global-mode-string)
332 (setq global-mode-string
333 (append '("" vip-mode-string) (cdr global-mode-string))))
337 ;;; Load set up hooks then load .vip
339 ;; This hook designed to enable Vi-style editing in comint-based modes."
340 (defun vip-comint-mode-hook ()
341 (setq require-final-newline nil
342 vip-ex-style-editing-in-insert nil
343 vip-ex-style-motion nil)
344 (vip-change-state-to-insert))
347 ;; This sets major mode hooks to make them come up in vi-state.
348 (defun vip-set-hooks ()
350 ;; It is of course a misnomer to call viper-mode a `major mode'.
351 ;; However, this has the effect that if the user didn't specify the
352 ;; default mode, new buffers that fall back on the default will come up
353 ;; in Fundamental Mode and Vi state.
354 (setq default-major-mode 'viper-mode)
356 ;; The following major modes should come up in vi-state
357 (defadvice fundamental-mode (after vip-fundamental-mode-ad activate)
358 "Run `vip-change-state-to-vi' on entry."
359 (vip-change-state-to-vi))
361 (defvar makefile-mode-hook)
362 (add-hook 'makefile-mode-hook 'viper-mode)
364 (defvar help-mode-hook)
365 (add-hook 'help-mode-hook 'viper-mode)
366 (vip-modify-major-mode 'help-mode 'vi-state vip-help-modifier-map)
368 (defvar awk-mode-hook)
369 (add-hook 'awk-mode-hook 'viper-mode)
371 (defvar html-mode-hook)
372 (add-hook 'html-mode-hook 'viper-mode)
373 (defvar html-helper-mode-hook)
374 (add-hook 'html-helper-mode-hook 'viper-mode)
375 (defvar java-mode-hook)
376 (add-hook 'java-mode-hook 'viper-mode)
378 (defvar emacs-lisp-mode-hook)
379 (add-hook 'emacs-lisp-mode-hook 'viper-mode)
381 (defvar lisp-mode-hook)
382 (add-hook 'lisp-mode-hook 'viper-mode)
384 (defvar bibtex-mode-hook)
385 (add-hook 'bibtex-mode-hook 'viper-mode)
387 (defvar cc-mode-hook)
388 (add-hook 'cc-mode-hook 'viper-mode)
390 (defvar c-mode-hook)
391 (add-hook 'c-mode-hook 'viper-mode)
393 (defvar c++-mode-hook)
394 (add-hook 'c++-mode-hook 'viper-mode)
396 (defvar lisp-interaction-mode-hook)
397 (add-hook 'lisp-interaction-mode-hook 'viper-mode)
399 (defvar fortran-mode-hook)
400 (add-hook 'fortran-mode-hook 'vip-mode)
402 (defvar basic-mode-hook)
403 (add-hook 'basic-mode-hook 'vip-mode)
404 (defvar bat-mode-hook)
405 (add-hook 'bat-mode-hook 'vip-mode)
407 (defvar text-mode-hook)
408 (add-hook 'text-mode-hook 'viper-mode)
410 (add-hook 'completion-list-mode-hook 'viper-mode)
411 (add-hook 'compilation-mode-hook 'viper-mode)
413 (add-hook 'perl-mode-hook 'viper-mode)
414 (add-hook 'tcl-mode-hook 'viper-mode)
416 (defvar emerge-startup-hook)
417 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
419 ;; Tell vc-diff to put *vc* in Vi mode
420 (if (featurep 'vc)
421 (defadvice vc-diff (after vip-vc-ad activate)
422 "Force Vi state in VC diff buffer."
423 (vip-change-state-to-vi))
424 (vip-eval-after-load
425 "vc"
426 '(defadvice vc-diff (after vip-vc-ad activate)
427 "Force Vi state in VC diff buffer."
428 (vip-change-state-to-vi))))
430 (vip-eval-after-load
431 "emerge"
432 '(defadvice emerge-quit (after vip-emerge-advice activate)
433 "Run `vip-change-state-to-vi' after quitting emerge."
434 (vip-change-state-to-vi)))
435 ;; In case Emerge was loaded before Viper.
436 (defadvice emerge-quit (after vip-emerge-advice activate)
437 "Run `vip-change-state-to-vi' after quitting emerge."
438 (vip-change-state-to-vi))
440 (vip-eval-after-load
441 "asm-mode"
442 '(defadvice asm-mode (after vip-asm-mode-ad activate)
443 "Run `vip-change-state-to-vi' on entry."
444 (vip-change-state-to-vi)))
446 ;; passwd.el sets up its own buffer, which turns up in Vi mode,
447 ;; thus overriding the local map. We don't need Vi mode here.
448 (vip-eval-after-load
449 "passwd"
450 '(defadvice read-passwd-1 (before vip-passwd-ad activate)
451 "Switch to emacs state while reading password."
452 (vip-change-state-to-emacs)))
454 (vip-eval-after-load
455 "prolog"
456 '(defadvice prolog-mode (after vip-prolog-ad activate)
457 "Switch to Vi state in Prolog mode."
458 (vip-change-state-to-vi)))
460 ;; Emacs shell, ange-ftp, and comint-based modes
461 (defvar comint-mode-hook)
462 (vip-modify-major-mode
463 'comint-mode 'insert-state vip-comint-mode-modifier-map)
464 (vip-modify-major-mode
465 'comint-mode 'vi-state vip-comint-mode-modifier-map)
466 (vip-modify-major-mode
467 'shell-mode 'insert-state vip-comint-mode-modifier-map)
468 (vip-modify-major-mode
469 'shell-mode 'vi-state vip-comint-mode-modifier-map)
470 ;; ange-ftp in XEmacs
471 (vip-modify-major-mode
472 'ange-ftp-shell-mode 'insert-state vip-comint-mode-modifier-map)
473 (vip-modify-major-mode
474 'ange-ftp-shell-mode 'vi-state vip-comint-mode-modifier-map)
475 ;; ange-ftp in Emacs
476 (vip-modify-major-mode
477 'internal-ange-ftp-mode 'insert-state vip-comint-mode-modifier-map)
478 (vip-modify-major-mode
479 'internal-ange-ftp-mode 'vi-state vip-comint-mode-modifier-map)
480 ;; set hook
481 (add-hook 'comint-mode-hook 'vip-comint-mode-hook)
483 ;; Shell scripts
484 (defvar sh-mode-hook)
485 (add-hook 'sh-mode-hook 'viper-mode)
486 (defvar ksh-mode-hook)
487 (add-hook 'ksh-mode-hook 'viper-mode)
489 ;; Dired
490 (vip-modify-major-mode 'dired-mode 'emacs-state vip-dired-modifier-map)
491 (vip-set-emacs-search-style-macros nil 'dired-mode)
492 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
494 ;; Tar
495 (vip-modify-major-mode 'tar-mode 'emacs-state vip-slash-and-colon-map)
496 (vip-set-emacs-search-style-macros nil 'tar-mode)
498 ;; MH-E
499 (vip-modify-major-mode 'mh-folder-mode 'emacs-state vip-slash-and-colon-map)
500 (vip-set-emacs-search-style-macros nil 'mh-folder-mode)
501 ;; changing state to emacs is needed so the preceding will take hold
502 (add-hook 'mh-folder-mode-hook 'vip-change-state-to-emacs)
503 (add-hook 'mh-show-mode-hook 'viper-mode)
505 ;; Gnus
506 (vip-modify-major-mode 'gnus-group-mode 'emacs-state vip-slash-and-colon-map)
507 (vip-set-emacs-search-style-macros nil 'gnus-group-mode)
508 (vip-modify-major-mode
509 'gnus-summary-mode 'emacs-state vip-slash-and-colon-map)
510 (vip-set-emacs-search-style-macros nil 'gnus-summary-mode)
511 ;; changing state to emacs is needed so the preceding will take hold
512 (add-hook 'gnus-group-mode-hook 'vip-change-state-to-emacs)
513 (add-hook 'gnus-summary-mode-hook 'vip-change-state-to-emacs)
514 (add-hook 'gnus-article-mode-hook 'viper-mode)
516 ;; Info
517 (vip-modify-major-mode 'Info-mode 'emacs-state vip-slash-and-colon-map)
518 (vip-set-emacs-search-style-macros nil 'Info-mode)
519 ;; Switching to emacs is needed so the above will take hold
520 (defadvice Info-mode (after vip-Info-ad activate)
521 "Switch to emacs mode."
522 (vip-change-state-to-emacs))
524 ;; Buffer menu
525 (vip-modify-major-mode
526 'Buffer-menu-mode 'emacs-state vip-slash-and-colon-map)
527 (vip-set-emacs-search-style-macros nil 'Buffer-menu-mode)
528 ;; Switching to emacs is needed so the above will take hold
529 (defadvice Buffer-menu-mode (after vip-Buffer-menu-ad activate)
530 "Switch to emacs mode."
531 (vip-change-state-to-emacs))
533 ;; View mode
534 (if vip-emacs-p
535 (progn
536 (defvar view-mode-hook)
537 (add-hook 'view-mode-hook 'vip-change-state-to-emacs))
538 (defadvice view-minor-mode (after vip-view-ad activate)
539 "Switch to Emacs state in View mode."
540 (vip-change-state-to-emacs))
541 (defvar view-hook)
542 (add-hook 'view-hook 'vip-change-state-to-emacs))
544 ;; For VM users.
545 ;; Put summary and other VM buffers in Emacs state.
546 (defvar vm-mode-hooks)
547 (defvar vm-summary-mode-hooks)
548 (add-hook 'vm-mode-hooks 'vip-change-state-to-emacs)
549 (add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs)
551 ;; For RMAIL users.
552 ;; Put buf in Emacs state after edit.
553 (vip-eval-after-load
554 "rmailedit"
555 '(defadvice rmail-cease-edit (after vip-rmail-advice activate)
556 "Switch to emacs state when done editing message."
557 (vip-change-state-to-emacs)))
558 ;; In case RMAIL was loaded before Viper.
559 (defadvice rmail-cease-edit (after vip-rmail-advice activate)
560 "Switch to emacs state when done editing message."
561 (vip-change-state-to-emacs))
562 ) ; vip-set-hooks
564 ;; Set some useful macros
565 ;; These must be before we load .vip, so the user could unrecord them.
567 ;; repeat the 2nd previous command without rotating the command history
568 (vip-record-kbd-macro
569 (vector vip-repeat-from-history-key '\1) 'vi-state
570 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
571 ;; repeat the 3d previous command without rotating the command history
572 (vip-record-kbd-macro
573 (vector vip-repeat-from-history-key '\2) 'vi-state
574 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
576 ;; set the toggle case sensitivity and regexp search macros
577 (vip-set-vi-search-style-macros nil)
579 ;; Make %%% toggle parsing comments for matching parentheses
580 (vip-record-kbd-macro
581 "%%%" 'vi-state
582 [(meta x) v i p - t o g g l e - p a r s e - s e x p - i g n o r e - c o m m e n t s return]
586 ;; ~/.vip is loaded if it exists
587 (if (and (file-exists-p vip-custom-file-name)
588 (not noninteractive))
589 (load vip-custom-file-name))
591 ;; VIP compatibility: merge whatever the user has in vip-mode-map into
592 ;; Viper's basic map.
593 (vip-add-keymap vip-mode-map vip-vi-global-user-map)
596 ;; Applying Viper customization -- runs after (load .vip)
598 ;; Save user settings or Viper defaults for vars controled by vip-expert-level
599 (setq vip-saved-user-settings
600 (list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help)
601 (cons 'vip-always vip-always)
602 (cons 'vip-no-multiple-ESC vip-no-multiple-ESC)
603 (cons 'vip-ex-style-motion vip-ex-style-motion)
604 (cons 'vip-ex-style-editing-in-insert
605 vip-ex-style-editing-in-insert)
606 (cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi)
607 (cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert)
608 (cons 'vip-re-search vip-re-search)))
611 (vip-set-minibuffer-style)
612 (if vip-buffer-search-char
613 (vip-buffer-search-enable))
614 (vip-update-alphanumeric-class)
617 ;;; Familiarize Viper with some minor modes that have their own keymaps
618 (vip-harness-minor-mode "compile")
619 (vip-harness-minor-mode "outline")
620 (vip-harness-minor-mode "allout")
621 (vip-harness-minor-mode "xref")
622 (vip-harness-minor-mode "lmenu")
623 (vip-harness-minor-mode "vc")
624 (vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX
625 (vip-harness-minor-mode "latex") ; which is in one of these two files
626 (vip-harness-minor-mode "cyrillic")
627 (vip-harness-minor-mode "russian")
628 (vip-harness-minor-mode "view-less")
629 (vip-harness-minor-mode "view")
632 ;; Intercept maps could go in viper-keym.el
633 ;; We keep them here in case someone redefines them in ~/.vip
635 (define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
636 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
638 ;; This is taken care of by vip-insert-global-user-map.
639 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
642 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
643 ;; iconifies Emacs
644 (define-key vip-vi-intercept-map vip-toggle-key 'vip-toggle-key-action)
645 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
648 (if (or vip-always
649 (and (< vip-expert-level 5) (> vip-expert-level 0)))
650 (vip-set-hooks))
652 ;; Let all minor modes take effect after loading
653 ;; this may not be enough, so we also set default minor-mode-alist.
654 ;; Without setting the default, new buffers that come up in emacs mode have
655 ;; minor-mode-map-alist = nil, unless we call vip-change-state-*
656 (if (eq vip-current-state 'emacs-state)
657 (progn
658 (vip-change-state-to-emacs)
659 (setq-default minor-mode-map-alist minor-mode-map-alist)
664 (run-hooks 'vip-load-hook) ; the last chance to change something
666 (provide 'vip)
667 (provide 'viper)
669 ;;; viper.el ends here