Make read1 more reentrant
[emacs.git] / lisp / emulation / viper.el
blobc5dac55522a41fe30557b54660f4908af46f5cff
1 ;;; viper.el --- A full-featured Vi emulator for Emacs and XEmacs, -*-lexical-binding:t -*-
2 ;; a VI Plan for Emacs Rescue,
3 ;; and a venomous VI PERil.
4 ;; Viper Is also a Package for Emacs Rebels.
6 ;; Copyright (C) 1994-2016 Free Software Foundation, Inc.
8 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
9 ;; Keywords: emulations
10 ;; Version: 3.14.1
12 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
13 ;; file on 20/3/2008, and the maintainer agreed that when a bug is
14 ;; filed in the Emacs bug reporting system against this file, a copy
15 ;; of the bug report be sent to the maintainer's email address.
17 (defconst viper-version "3.14.2 of July 4, 2013"
18 "The current version of Viper")
20 ;; This file is part of GNU Emacs.
22 ;; GNU Emacs is free software: you can redistribute it and/or modify
23 ;; it under the terms of the GNU General Public License as published by
24 ;; the Free Software Foundation, either version 3 of the License, or
25 ;; (at your option) any later version.
27 ;; GNU Emacs is distributed in the hope that it will be useful,
28 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
29 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 ;; GNU General Public License for more details.
32 ;; You should have received a copy of the GNU General Public License
33 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
35 ;;; Commentary:
37 ;; Viper is a full-featured Vi emulator for Emacs and XEmacs. It emulates and
38 ;; improves upon the standard features of Vi and, at the same time, allows
39 ;; full access to all Emacs facilities. Viper supports multiple undo,
40 ;; file name completion, command, file, and search history and it extends
41 ;; Vi in many other ways. Viper is highly customizable through the various
42 ;; hooks, user variables, and keymaps. It is implemented as a collection
43 ;; of minor modes and it is designed to provide full access to all Emacs
44 ;; major and minor modes.
46 ;;; History:
48 ;; Viper is a new name for a package formerly known as VIP-19,
49 ;; which was a successor of VIP version 3.5 by Masahiko Sato
50 ;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
51 ;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
52 ;; were also shamelessly plagiarized.
54 ;; Viper maintains some degree of compatibility with these older
55 ;; packages. See the documentation for customization.
57 ;; The main difference between Viper and these older packages are:
59 ;; 1. Viper emulates Vi at several levels, from almost complete conformity
60 ;; to a rather loose Vi-compliance.
62 ;; 2. Viper provides full access to all major and minor modes of Emacs
63 ;; without the need to type extra keys.
64 ;; The older versions of VIP (and other Vi emulators) do not work with
65 ;; some major and minor modes.
67 ;; 3. Viper supports vi-style undo.
69 ;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
71 ;; 5. Viper has a better interface to ex, including command, variable, and
72 ;; file name completion.
74 ;; 6. Viper uses native Emacs history and completion features; it doesn't
75 ;; rely on other packages (such as gmhist.el and completer.el) to provide
76 ;; these features.
78 ;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
79 ;; user to switch from Insert state to Vi state to Replace state, etc.
81 ;; 8. Viper keeps history of recently inserted pieces of text and recently
82 ;; executed Vi-style destructive commands, such as `i', `d', etc.
83 ;; These pieces of text can be inserted in later insertion commands;
84 ;; the previous destructive commands can be re-executed.
86 ;; 9. Viper has Vi-style keyboard macros, which enhances the similar
87 ;; facility in the original Vi.
88 ;; First, one can execute any Emacs command while defining a
89 ;; macro, not just the Vi commands. Second, macros are defined in a
90 ;; WYSYWYG mode, using an interface to Emacs's WYSIWYG style of defining
91 ;; macros. Third, in Viper, one can define macros that are specific to
92 ;; a given buffer, a given major mode, or macros defined for all buffers.
93 ;; The same macro name can have several different definitions:
94 ;; one global, several definitions for various major modes, and
95 ;; definitions for specific buffers.
96 ;; Buffer-specific definitions override mode-specific
97 ;; definitions, which, in turn, override global definitions.
100 ;;; Installation:
101 ;; -------------
103 ;; (require 'viper)
106 ;;; Acknowledgments:
107 ;; ----------------
108 ;; Bug reports and ideas contributed by many users have helped
109 ;; improve Viper and the various versions of VIP.
110 ;; See the manual for a complete list of contributors.
113 ;;; Notes:
115 ;; 1. Major modes.
116 ;; In most cases, Viper handles major modes correctly, i.e., they come up
117 ;; in the right state (either vi-state or emacs-state). For instance, text
118 ;; files come up in vi-state, while, say, Dired appears in emacs-state by
119 ;; default.
120 ;; However, some modes do not appear in the right mode in the beginning,
121 ;; usually because they neglect to follow Emacs conventions (e.g., they don't
122 ;; use kill-all-local-variables when they start). Some major modes
123 ;; may fail to come up in emacs-state if they call hooks, such as
124 ;; text-hook, for no good reason.
126 ;; As an immediate solution, you can hit C-z to bring about the right mode.
127 ;; An interim solution is to add an appropriate hook to the mode like this:
129 ;; (add-hook 'your-favorite-mode #'viper-mode)
130 ;; or
131 ;; (add-hook 'your-favorite-mode #'viper-change-state-to-emacs)
133 ;; whichever applies. The right thing to do, however, is to complain to the
134 ;; author of the respective package. (Sometimes they also neglect to equip
135 ;; their modes with hooks, which is one more reason for complaining.)
137 ;; 2. Keymap handling
138 ;; Each Viper state (insert, vi, replace) is implemented as a collection of
139 ;; several minor modes, each with its own keymap.
141 ;; Viper's Vi state consists of seven minor modes:
143 ;; viper-vi-intercept-minor-mode
144 ;; viper-vi-local-user-minor-mode
145 ;; viper-vi-global-user-minor-mode
146 ;; viper-vi-kbd-minor-mode
147 ;; viper-vi-state-modifier-minor-mode
148 ;; viper-vi-diehard-minor-mode
149 ;; viper-vi-basic-minor-mode
151 ;; Bindings done to the keymap of the first mode overshadow those done to
152 ;; the second, which, in turn, overshadows those done to the third, etc.
154 ;; The last viper-vi-basic-minor-mode contains most of the usual Vi bindings
155 ;; in its edit mode. This mode provides access to all Emacs facilities.
156 ;; Novice users, however, may want to set their viper-expert-level to 1 in
157 ;; their viper-custom-file-name. This will enable viper-vi-diehard-minor-mode.
158 ;; This minor mode's bindings make Viper simulate the usual Vi very closely.
159 ;; For instance, C-c will not have its standard Emacs binding
160 ;; and so many of the goodies of Emacs are not available.
162 ;; A skilled user should set viper-expert-level to at least 3. This will
163 ;; enable `C-c' and many Emacs facilities will become available.
164 ;; In this case, viper-vi-diehard-minor-mode is inactive.
166 ;; Viper gurus should have at least
167 ;; (setq viper-expert-level 4)
168 ;; in their viper-custom-file-name. This will unsuppress all Emacs keys
169 ;; that are not essential for VI-style editing.
170 ;; Pick-and-choose users may want to put
171 ;; (setq viper-expert-level 5)
172 ;; in viper-custom-file-name. Viper will then leave it up to the user to
173 ;; set the variables viper-want-* See viper-set-expert-level for details.
175 ;; The very first minor mode, viper-vi-intercept-minor-mode, is of no
176 ;; concern for the user. It is needed to bind Viper's vital keys, such as
177 ;; ESC and C-z.
179 ;; The second mode, viper-vi-local-user-minor-mode, usually has an
180 ;; empty keymap. However, the user can set bindings in this keymap, which
181 ;; will overshadow the corresponding bindings in the other two minor
182 ;; modes. This is useful, for example, for setting up ZZ in gnus,
183 ;; rmail, mh-e, etc., to send message instead of saving it in a file.
184 ;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
185 ;; that would visit the next or the previous file in the Dired buffer.
186 ;; Setting local keys is tricky, so don't do it directly. Instead, use
187 ;; viper-add-local-keys function (see its doc).
189 ;; The third minor mode, viper-vi-global-user-minor-mode, is also intended
190 ;; for the users but, unlike viper-vi-local-user-minor-mode, its key
191 ;; bindings are seen in all Viper buffers. This mode keys can be done
192 ;; with define-key command.
194 ;; The fourth minor mode, viper-vi-kbd-minor-mode, is used by keyboard
195 ;; macros. Users are NOT supposed to modify this keymap directly.
197 ;; The fifth mode, viper-vi-state-modifier-minor-mode, can be used to set
198 ;; key bindings that are visible in some major modes but not in others.
200 ;; Users are allowed to modify keymaps that belong to
201 ;; viper-vi-local-user-minor-mode, viper-vi-global-user-minor-mode,
202 ;; and viper-vi-state-modifier-minor-mode only.
204 ;; Viper's Insert state also has seven minor modes:
206 ;; viper-insert-intercept-minor-mode
207 ;; viper-insert-local-user-minor-mode
208 ;; viper-insert-global-user-minor-mode
209 ;; viper-insert-kbd-minor-mode
210 ;; viper-insert-state-modifier-minor-mode
211 ;; viper-insert-diehard-minor-mode
212 ;; viper-insert-basic-minor-mode
214 ;; As with VI's editing modes, the first mode,
215 ;; viper-insert-intercept-minor-mode is used to bind vital keys that are not
216 ;; to be changed by the user.
218 ;; The next mode, viper-insert-local-user-minor-mode, is used to customize
219 ;; bindings in the insert state of Viper. The third mode,
220 ;; viper-insert-global-user-minor-mode is like
221 ;; viper-insert-local-user-minor-mode, except that its bindings are seen in
222 ;; all Viper buffers. As with viper-vi-local-user-minor-mode, its bindings
223 ;; should be done via the function viper-add-local-keys. Bindings for
224 ;; viper-insert-global-user-minor-mode can be set with the define-key command.
226 ;; The next minor mode, viper-insert-kbd-minor-mode,
227 ;; is used for keyboard VI-style macros defined with :map!.
229 ;; The fifth minor mode, viper-insert-state-modifier-minor-mode, is like
230 ;; viper-vi-state-modifier-minor-mode, except that it is used in the Insert
231 ;; state; it can be used to modify keys in a mode-specific fashion.
233 ;; The minor mode viper-insert-diehard-minor-mode is in effect when
234 ;; the user wants a high degree of Vi compatibility (a bad idea, really!).
235 ;; The last minor mode, viper-insert-basic-minor-mode, is always in effect
236 ;; when Viper is in insert state. It binds a small number of keys needed for
237 ;; Viper's operation.
239 ;; Finally, Viper provides minor modes for overriding bindings set by Emacs
240 ;; modes when Viper is in Emacs state:
242 ;; viper-emacs-local-user-minor-mode
243 ;; viper-emacs-global-user-minor-mode
244 ;; viper-emacs-kbd-minor-mode
245 ;; viper-emacs-state-modifier-minor-mode
247 ;; These minor modes are in effect when Viper is in Emacs state. The keymap
248 ;; associated with viper-emacs-global-user-minor-mode,
249 ;; viper-emacs-global-user-map, overrides the global and local keymaps as
250 ;; well as the minor mode keymaps set by other modes. The keymap of
251 ;; viper-emacs-local-user-minor-mode, viper-emacs-local-user-map, overrides
252 ;; everything, but it is used on a per buffer basis.
253 ;; The keymap associated with viper-emacs-state-modifier-minor-mode
254 ;; overrides keys on a per-major-mode basis. The mode
255 ;; viper-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
256 ;; state.
258 ;; 3. There is also one minor mode that is used when Viper is in its
259 ;; replace-state (used for commands like cw, C, etc.). This mode is
260 ;; called
262 ;; viper-replace-minor-mode
264 ;; and its keymap is viper-replace-map. Replace minor mode is always
265 ;; used in conjunction with the minor modes for insert-state, and its
266 ;; keymap overshadows the keymaps for insert minor modes.
268 ;; 4. Defining buffer-local bindings in Vi and Insert modes.
269 ;; As mentioned before, sometimes, it is convenient to have
270 ;; buffer-specific of mode-specific key bindings in Vi and insert modes.
271 ;; Viper provides a special function, viper-add-local-keys, to do precisely
272 ;; this. For instance, is you need to add couple of mode-specific bindings
273 ;; to Insert mode, you can put
275 ;; (viper-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
277 ;; somewhere in a hook of this major mode. If you put something like this
278 ;; in your own elisp function, this will define bindings specific to the
279 ;; buffer that was current at the time of the call to viper-add-local-keys.
280 ;; The only thing to make sure here is that the major mode of this buffer
281 ;; is written according to Emacs conventions, which includes a call to
282 ;; (kill-all-local-variables). See viper-add-local-keys for more details.
285 ;; TO DO (volunteers?):
287 ;; 1. Some of the code that is inherited from VIP-3.5 is rather
288 ;; convoluted. Instead of viper-command-argument, keymaps should bind the
289 ;; actual commands. E.g., "dw" should be bound to a generic command
290 ;; viper-delete that will delete things based on the value of
291 ;; last-command-event. This would greatly simplify the logic and the code.
293 ;; 2. Somebody should venture to write a customization package a la
294 ;; options.el that would allow the user to change values of variables
295 ;; that meet certain specs (e.g., match a regexp) and whose doc string
296 ;; starts with a '*'. Then, the user should be offered to save
297 ;; variables that were changed. This will make user's customization job
298 ;; much easier.
301 ;;; Code:
303 ;; compiler pacifier
304 (defvar mark-even-if-inactive)
305 (defvar quail-mode)
306 (defvar viper-expert-level)
307 (defvar viper-mode-string)
308 (defvar viper-major-mode-modifier-list)
309 ;; end pacifier
311 (require 'viper-init)
312 (require 'viper-keym)
314 ;; better be defined before Viper custom group.
315 (defvar viper-custom-file-name (locate-user-emacs-file "viper" ".viper")
316 "Viper customization file.
317 If set by the user, this must be done _before_ Viper is loaded in `~/.emacs'.")
319 (defgroup viper nil
320 "Vi emulation within Emacs.
321 NOTE: Viper customization should be saved in `viper-custom-file-name'."
322 :prefix "viper-"
323 :group 'emulations)
325 (require 'viper-cmd)
327 (defgroup viper-misc nil
328 "Miscellaneous Viper customization."
329 :prefix "viper-"
330 :group 'viper)
333 (defcustom viper-always t
334 "Non-nil means, arrange for vi-state to be a default when appropriate.
335 This is different from `viper-mode' variable in that `viper-mode' determines
336 whether to use Viper in the first place, while `viper-always', if nil, lets
337 user decide when to invoke Viper in a major mode."
338 :type 'boolean
339 :tag "Always Invoke Viper")
341 ;; Non-viper variables that need to be saved in case the user decides to
342 ;; de-viperize emacs.
343 (defvar viper-saved-non-viper-variables nil)
345 (defcustom viper-mode (cond (noninteractive nil)
346 (t 'ask))
347 "To Viperize or not to Viperize.
348 If t, viperize Emacs. If nil -- don't. If `ask', ask the user.
349 This variable is used primarily when Viper is being loaded.
351 Must be set in your init file before Viper is loaded.
352 DO NOT set this variable interactively, unless you are using the customization
353 widget."
354 :type '(choice (const nil) (const t) (const ask))
355 :tag "Set Viper Mode on Loading")
357 (defcustom viper-vi-state-mode-list
358 '(fundamental-mode
359 makefile-mode
361 awk-mode
362 m4-mode
363 xrdb-mode
364 winmgr-mode
365 autoconf-mode
366 cvs-edit-mode
368 html-mode html-helper-mode
369 emacs-lisp-mode lisp-mode lisp-interaction-mode
371 jde-mode java-mode
372 cc-mode c-mode c++-mode objc-mode
373 fortran-mode f90-mode
374 basic-mode
375 bat-mode
376 asm-mode
377 prolog-mode
378 flora-mode
379 sql-mode
381 text-mode indented-text-mode
382 tex-mode latex-mode bibtex-mode
383 ps-mode
385 ;; completion-list-mode
386 diff-mode
387 idl-mode
389 perl-mode
390 cperl-mode
391 javascript-mode
392 tcl-mode
393 python-mode
395 sh-mode ksh-mode csh-mode
397 gnus-article-mode
398 mh-show-mode
400 "Major modes that require Vi command state."
401 :type '(repeat symbol))
403 (defcustom viper-emacs-state-mode-list
404 '(Custom-mode
406 dired-mode
407 efs-mode
408 tar-mode
409 egg-status-buffer-mode
411 browse-kill-ring-mode
412 recentf-mode
413 recentf-dialog-mode
414 occur-mode
416 mh-folder-mode
417 gnus-group-mode
418 gnus-summary-mode
420 completion-list-mode
421 help-mode
423 Info-mode
424 Buffer-menu-mode
425 compilation-mode
427 rcirc-mode
429 jde-javadoc-checker-report-mode
431 view-mode
432 vm-mode
433 vm-summary-mode)
434 "A list of major modes that should come up in Emacs state.
435 Normally, Viper would bring buffers up in Emacs state, unless the corresponding
436 major mode has been placed on `viper-vi-state-mode-list' or
437 `viper-insert-state-mode-list'. So, don't place a new mode on this list,
438 unless it is coming up in a wrong Viper state."
439 :type '(repeat symbol))
441 (defcustom viper-insert-state-mode-list
442 '(internal-ange-ftp-mode
443 comint-mode
444 gud-mode
445 inferior-emacs-lisp-mode
446 erc-mode
447 eshell-mode
448 shell-mode)
449 "A list of major modes that should come up in Vi Insert state."
450 :type '(repeat symbol))
453 ;; used to set viper-major-mode-modifier-list in defcustom
454 (defun viper-apply-major-mode-modifiers (&optional symbol value)
455 (if symbol
456 (set symbol value))
457 (mapc (lambda (triple)
458 (viper-modify-major-mode
459 (nth 0 triple) (nth 1 triple) (symbol-value (nth 2 triple))))
460 viper-major-mode-modifier-list))
462 ;; We change standard bindings in some major modes, making them slightly
463 ;; different than in "normal" vi/insert/emacs states
464 (defcustom viper-major-mode-modifier-list
465 '((help-mode emacs-state viper-slash-and-colon-map)
466 (comint-mode insert-state viper-comint-mode-modifier-map)
467 (comint-mode vi-state viper-comint-mode-modifier-map)
468 (gud-mode insert-state viper-comint-mode-modifier-map)
469 (shell-mode insert-state viper-comint-mode-modifier-map)
470 (inferior-emacs-lisp-mode insert-state viper-comint-mode-modifier-map)
471 (shell-mode vi-state viper-comint-mode-modifier-map)
472 (ange-ftp-shell-mode insert-state viper-comint-mode-modifier-map)
473 (ange-ftp-shell-mode vi-state viper-comint-mode-modifier-map)
474 (internal-ange-ftp-mode insert-state viper-comint-mode-modifier-map)
475 (internal-ange-ftp-mode vi-state viper-comint-mode-modifier-map)
476 (dired-mode emacs-state viper-dired-modifier-map)
477 (tar-mode emacs-state viper-slash-and-colon-map)
478 (mh-folder-mode emacs-state viper-slash-and-colon-map)
479 (gnus-group-mode emacs-state viper-gnus-modifier-map)
480 (gnus-summary-mode emacs-state viper-gnus-modifier-map)
481 (Info-mode emacs-state viper-slash-and-colon-map)
482 (Buffer-menu-mode emacs-state viper-slash-and-colon-map)
483 (erc-mode insert-state viper-comint-mode-modifier-map)
484 (erc-mode vi-state viper-comint-mode-modifier-map)
486 "List specifying how to modify the various major modes to enable some Viperisms.
487 The list has the structure: ((mode viper-state keymap) (mode viper-state
488 keymap) ...). If `mode' is on the list, the `keymap' will be made active (on
489 the minor-mode-map-alist) in the specified viper state.
490 If you change this list, have to restart Emacs for the change to take effect.
491 However, if you did the change through the customization widget, then Emacs
492 needs to be restarted only if you deleted a triple mode-state-keymap from the
493 list. No need to restart Emacs in case of insertion or modification of an
494 existing triple."
495 :type '(repeat
496 (list symbol
497 (choice (const emacs-state)
498 (const vi-state)
499 (const insert-state))
500 symbol))
501 :set #'viper-apply-major-mode-modifiers)
505 ;;;###autoload
506 (defun toggle-viper-mode ()
507 "Toggle Viper on/off.
508 If Viper is enabled, turn it off. Otherwise, turn it on."
509 (interactive)
510 (if (eq viper-mode t)
511 (viper-go-away)
512 (setq viper-mode nil)
513 (viper-mode)))
515 ;;;###autoload
516 (defun viper-mode ()
517 "Turn on Viper emulation of Vi in Emacs. See Info node `(viper)Top'."
518 (interactive)
519 (if (not noninteractive)
520 (progn
521 ;; if the user requested viper-mode explicitly
522 (if viper-mode
524 (setq viper-mode t)
525 ;; FIXME: Don't reload!
526 (load-library "viper"))
528 (if viper-first-time ; Important check. Prevents mix-up of startup
529 (progn ; and expert-level msgs when viper-mode recurses
530 (setq viper-first-time nil)
531 (if (not viper-inhibit-startup-message)
532 (save-window-excursion
533 (setq viper-inhibit-startup-message t)
534 (delete-other-windows)
535 (switch-to-buffer "Viper Startup Message")
536 (erase-buffer)
537 (insert
538 (substitute-command-keys
539 "Viper Is a Package for Emacs Rebels,
540 a VI Plan for Emacs Rescue, and a venomous VI PERil.
542 Incidentally, Viper emulates Vi under Emacs/XEmacs 20.
543 It supports all of what is good in Vi and Ex, while extending
544 and improving upon much of it.
546 1. Viper supports Vi at several levels. Level 1 is the closest to Vi,
547 level 5 provides the most flexibility to depart from many Vi conventions.
549 You will be asked to specify your user level in a following screen.
551 If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will behave
552 as in VI, to smooth transition to Viper for the beginners. However, to
553 use Emacs productively, you are advised to reach user level 3 or higher.
555 At user level 2 or higher, ^X and ^C have Emacs, not Vi, bindings;
556 ^Z toggles Vi/Emacs states; ^G is Emacs's keyboard-quit (like ^C in Vi).
558 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
559 do not cause Emacs to quit, except at user level 1 (for a novice).
560 3. ^X^C EXITS EMACS.
561 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
562 undo. Another `u' changes direction.
564 6. Emacs Meta key is `C-\\' (in all modes) or `\\ ESC' (in Vi command mode).
565 On a window system, the best way is to use the Meta-key on your keyboard.
566 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if
567 something funny happens. This would abort the current editing command.
569 For more information on Viper:
571 a. Type `:help' in Vi command mode
572 b. Print Viper manual, found in ./etc/viper.dvi
573 c. Print the Quick Reference, found in ./etc/viperCard.dvi
575 To submit a bug report or to contact the author, type :submitReport in Vi
576 command mode. To shoo Viper away and return to pure Emacs (horror!), type:
578 M-x viper-go-away
580 This startup message appears whenever you load Viper, unless you type `y' now."
582 (goto-char (point-min))
583 (if (y-or-n-p "Inhibit Viper startup message? ")
584 (viper-save-setting
585 'viper-inhibit-startup-message
586 "Viper startup message inhibited"
587 viper-custom-file-name t))
588 ;;(kill-buffer (current-buffer))
589 (message
590 "The last message is in buffer `Viper Startup Message'")
591 (sit-for 4)
593 (viper-set-expert-level 'dont-change-unless)))
595 (or (memq major-mode viper-emacs-state-mode-list) ; don't switch to Vi
596 (memq major-mode viper-insert-state-mode-list) ; don't switch
597 (viper-change-state-to-vi))
600 (if (eq major-mode 'viper-mode)
601 (setq major-mode 'fundamental-mode))
605 ;; Apply a little heuristic to invoke vi state on major-modes
606 ;; that are not listed in viper-vi-state-mode-list
607 (defun this-major-mode-requires-vi-state (mode)
608 (cond ((memq mode viper-vi-state-mode-list) t)
609 ((memq mode viper-emacs-state-mode-list) nil)
610 ((memq mode viper-insert-state-mode-list) nil)
611 (t (and (eq (key-binding "a") 'self-insert-command)
612 (eq (key-binding " ") 'self-insert-command)))))
615 ;; This hook designed to enable Vi-style editing in comint-based modes."
616 (defun viper-comint-mode-hook ()
617 (set (make-local-variable 'require-final-newline) nil)
618 (setq viper-ex-style-editing nil
619 viper-ex-style-motion nil)
620 (viper-change-state-to-insert))
623 ;; remove viper hooks from SYMBOL
624 (defun viper-remove-hooks (symbol)
625 (cond ((not (boundp symbol)) nil)
626 ((not (listp (symbol-value symbol))) nil)
627 ((string-match "-hook" (symbol-name symbol))
628 (remove-hook symbol #'viper-mode)
629 (remove-hook symbol #'viper-change-state-to-emacs)
630 (remove-hook symbol #'viper-change-state-to-insert)
631 (remove-hook symbol #'viper-change-state-to-vi)
632 (remove-hook symbol #'viper-minibuffer-post-command-hook)
633 (remove-hook symbol #'viper-minibuffer-setup-sentinel)
634 (remove-hook symbol #'viper-major-mode-change-sentinel)
635 (remove-hook symbol #'set-viper-state-in-major-mode)
636 (remove-hook symbol #'viper-post-command-sentinel)
639 ;; Remove local value in all existing buffers
640 ;; This doesn't delocalize vars (which would have been desirable)
641 (defun viper-delocalize-var (symbol)
642 (dolist (buf (buffer-list))
643 (with-current-buffer buf
644 (kill-local-variable symbol))))
646 (defvar viper--advice-list nil)
648 (defun viper--advice-add (function where advice)
649 (advice-add function where advice)
650 (push (cons function advice) viper--advice-list))
652 (defun viper--deactivate-advice-list ()
653 (mapc (lambda (n)
654 (advice-remove
655 (car n)
656 (cdr n)))
657 viper--advice-list)
658 (setq viper--advice-list nil))
660 (defun viper-go-away ()
661 "De-Viperize Emacs.
662 This function tries to do as good a job as possible. However, it may undo some
663 user customization, unrelated to Viper. For instance, if the user advised
664 `read-file-name', `describe-key', and some others, then this advice will be
665 undone.
666 It also can't undo some Viper settings."
667 (interactive)
668 (viper-setup-ESC-to-escape nil)
669 ;; restore non-viper vars
670 (setq-default
671 next-line-add-newlines
672 (viper-standard-value
673 'next-line-add-newlines viper-saved-non-viper-variables)
674 require-final-newline
675 (viper-standard-value
676 'require-final-newline viper-saved-non-viper-variables)
677 scroll-step
678 (viper-standard-value 'scroll-step viper-saved-non-viper-variables)
679 mode-line-buffer-identification
680 (viper-standard-value
681 'mode-line-buffer-identification viper-saved-non-viper-variables)
682 global-mode-string
683 (delq 'viper-mode-string global-mode-string))
685 (setq-default major-mode
686 (viper-standard-value 'major-mode
687 viper-saved-non-viper-variables))
689 (if (featurep 'emacs)
690 (setq-default
691 mark-even-if-inactive
692 (viper-standard-value
693 'mark-even-if-inactive viper-saved-non-viper-variables)))
695 ;; Ideally, we would like to be able to de-localize local variables
696 (unless
697 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
698 (viper-delocalize-var 'minor-mode-map-alist))
699 (viper-delocalize-var 'require-final-newline)
700 (if (featurep 'xemacs) (viper-delocalize-var 'bar-cursor))
703 ;; deactivate all advices done by Viper.
704 (viper--deactivate-advice-list)
706 (setq viper-mode nil)
708 (when (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
709 (setq emulation-mode-map-alists
710 (delq 'viper--intercept-key-maps
711 (delq 'viper--key-maps emulation-mode-map-alists))
714 (viper-delocalize-var 'viper-vi-minibuffer-minor-mode)
715 (viper-delocalize-var 'viper-insert-minibuffer-minor-mode)
716 (viper-delocalize-var 'viper-vi-intercept-minor-mode)
717 (viper-delocalize-var 'viper-insert-intercept-minor-mode)
719 (viper-delocalize-var 'viper-vi-local-user-minor-mode)
720 (viper-delocalize-var 'viper-vi-kbd-minor-mode)
721 (viper-delocalize-var 'viper-vi-global-user-minor-mode)
722 (viper-delocalize-var 'viper-vi-state-modifier-minor-mode)
723 (viper-delocalize-var 'viper-vi-diehard-minor-mode)
724 (viper-delocalize-var 'viper-vi-basic-minor-mode)
726 (viper-delocalize-var 'viper-replace-minor-mode)
728 (viper-delocalize-var 'viper-insert-local-user-minor-mode)
729 (viper-delocalize-var 'viper-insert-kbd-minor-mode)
730 (viper-delocalize-var 'viper-insert-global-user-minor-mode)
731 (viper-delocalize-var 'viper-insert-state-modifier-minor-mode)
732 (viper-delocalize-var 'viper-insert-diehard-minor-mode)
733 (viper-delocalize-var 'viper-insert-basic-minor-mode)
735 (viper-delocalize-var 'viper-emacs-intercept-minor-mode)
736 (viper-delocalize-var 'viper-emacs-local-user-minor-mode)
737 (viper-delocalize-var 'viper-emacs-kbd-minor-mode)
738 (viper-delocalize-var 'viper-emacs-global-user-minor-mode)
739 (viper-delocalize-var 'viper-emacs-state-modifier-minor-mode)
741 (viper-delocalize-var 'viper-current-state)
742 (viper-delocalize-var 'viper-mode-string)
744 (setq-default viper-vi-minibuffer-minor-mode nil
745 viper-insert-minibuffer-minor-mode nil
746 viper-vi-intercept-minor-mode nil
747 viper-insert-intercept-minor-mode nil
749 viper-vi-local-user-minor-mode nil
750 viper-vi-kbd-minor-mode nil
751 viper-vi-global-user-minor-mode nil
752 viper-vi-state-modifier-minor-mode nil
753 viper-vi-diehard-minor-mode nil
754 viper-vi-basic-minor-mode nil
756 viper-replace-minor-mode nil
758 viper-insert-local-user-minor-mode nil
759 viper-insert-kbd-minor-mode nil
760 viper-insert-global-user-minor-mode nil
761 viper-insert-state-modifier-minor-mode nil
762 viper-insert-diehard-minor-mode nil
763 viper-insert-basic-minor-mode nil
765 viper-emacs-intercept-minor-mode nil
766 viper-emacs-local-user-minor-mode nil
767 viper-emacs-kbd-minor-mode nil
768 viper-emacs-global-user-minor-mode nil
769 viper-emacs-state-modifier-minor-mode nil
771 viper-current-state 'emacs-state
772 viper-mode-string viper-emacs-state-id
775 ;; remove all hooks set by viper
776 (mapatoms #'viper-remove-hooks)
777 (remove-hook 'comint-mode-hook #'viper-comint-mode-hook)
778 (remove-hook 'erc-mode-hook #'viper-comint-mode-hook)
779 (remove-hook 'change-major-mode-hook #'viper-major-mode-change-sentinel)
781 ;; unbind Viper mouse bindings
782 (viper-unbind-mouse-search-key)
783 (viper-unbind-mouse-insert-key)
784 ;; In emacs, we have to advice handle-switch-frame
785 ;; This advice is undone earlier, when all advices matching "viper-" are
786 ;; deactivated.
787 (if (featurep 'xemacs)
788 (remove-hook 'mouse-leave-frame-hook #'viper-remember-current-frame))
789 ) ; end viper-go-away
792 ;; list of buffers that just changed their major mode
793 ;; used in a hack that triggers vi command mode whenever needed
794 (defvar viper-new-major-mode-buffer-list nil)
796 ;; set appropriate Viper state in buffers that changed major mode
797 (defun set-viper-state-in-major-mode ()
798 (mapc
799 (lambda (buf)
800 (if (viper-buffer-live-p buf)
801 (with-current-buffer buf
802 (cond ((and (this-major-mode-requires-vi-state major-mode)
803 (eq viper-current-state 'emacs-state))
804 (viper-mode))
805 ((memq major-mode viper-emacs-state-mode-list)
806 ;; not checking (eq viper-current-state 'emacs-state)
807 ;; because viper-current-state could have gotten it by
808 ;; default. We need viper-change-state-to-emacs here to have
809 ;; the keymaps take effect.
810 (viper-change-state-to-emacs))
811 ((and (memq major-mode viper-insert-state-mode-list)
812 (not (eq viper-current-state 'insert-state)))
813 (viper-change-state-to-insert))
814 )) ; with-current-buffer
815 )) ; function
816 viper-new-major-mode-buffer-list)
817 ;; clear the list of bufs that changed major mode
818 (setq viper-new-major-mode-buffer-list nil)
819 ;; change the global value of hook
820 (remove-hook 'viper-post-command-hooks #'set-viper-state-in-major-mode))
822 ;; sets up post-command-hook to turn viper-mode, if the current mode is
823 ;; fundamental
824 (defun viper-major-mode-change-sentinel ()
825 (save-match-data
826 (or (string-match "\\*Minibuf-" (buffer-name))
827 (setq viper-new-major-mode-buffer-list
828 (cons (current-buffer) viper-new-major-mode-buffer-list))))
829 ;; change the global value of hook
830 (add-hook 'viper-post-command-hooks #'set-viper-state-in-major-mode t))
833 ;;; Handling of tty's ESC event
835 ;; On a tty, an ESC event can either be the user hitting the escape key, or
836 ;; some element of a byte sequence used to encode for example cursor keys.
837 ;; So we try to recognize those events that correspond to the escape key and
838 ;; turn them into `escape' events (same as used under GUIs). The heuristic we
839 ;; use to distinguish the two cases is based, as usual, on a timeout, and on
840 ;; the fact that the special ESC=>escape mapping only takes place if the whole
841 ;; last key-sequence so far is just [?\e], i.e. either we're still in
842 ;; read-key-sequence, or the last read-key-sequence only read [?\e], which
843 ;; should ideally never happen because it should have been mapped to [escape].
845 (defun viper--tty-ESC-filter (map)
846 (if (and (equal (this-single-command-keys) [?\e])
847 (sit-for (/ viper-fast-keyseq-timeout 1000)))
848 [escape] map))
850 (defun viper--lookup-key (map key)
851 "Kind of like `lookup-key'.
852 Two differences:
853 - KEY is a single key, not a sequence.
854 - the result is the \"raw\" binding, so it can be a `menu-item', rather than the
855 binding contained in that menu item."
856 (catch 'found
857 (map-keymap (lambda (k b) (if (equal key k) (throw 'found b))) map)))
859 (defun viper-catch-tty-ESC ()
860 "Setup key mappings of current terminal to turn a tty's ESC into `escape'."
861 (when (memq (terminal-live-p (frame-terminal)) '(t pc))
862 (let ((esc-binding (viper-uncatch-tty-ESC)))
863 (define-key input-decode-map
864 [?\e] `(menu-item "" ,esc-binding :filter viper--tty-ESC-filter)))))
866 (defun viper-uncatch-tty-ESC ()
867 "Don't hack ESC into `escape' any more."
868 (let ((b (viper--lookup-key input-decode-map ?\e)))
869 (and (eq 'menu-item (car-safe b))
870 (eq 'viper--tty-ESC-filter (nth 4 b))
871 (define-key input-decode-map [?\e] (setq b (nth 2 b))))
874 (defun viper-setup-ESC-to-escape (enable)
875 (if enable
876 (add-hook 'tty-setup-hook #'viper-catch-tty-ESC)
877 (remove-hook 'tty-setup-hook #'viper-catch-tty-ESC))
878 (let ((seen ()))
879 (dolist (frame (frame-list))
880 (let ((terminal (frame-terminal frame)))
881 (unless (memq terminal seen)
882 (push terminal seen)
883 (with-selected-frame frame
884 (if enable (viper-catch-tty-ESC) (viper-uncatch-tty-ESC))))))))
886 ;; This sets major mode hooks to make them come up in vi-state.
887 (defun viper-set-hooks ()
888 ;; It is of course a misnomer to call viper-mode a `major mode'.
889 ;; However, this has the effect that if the user didn't specify the
890 ;; default mode, new buffers that fall back on the default will come up
891 ;; in Fundamental Mode and Vi state.
892 ;; When viper-mode is executed in such a case, it will set the major mode
893 ;; back to fundamental-mode.
894 (if (eq (default-value 'major-mode) #'fundamental-mode)
895 ;; FIXME: We should use after-change-major-mode-hook instead!
896 (setq-default major-mode #'viper-mode))
898 (viper-setup-ESC-to-escape t)
900 (add-hook 'change-major-mode-hook #'viper-major-mode-change-sentinel)
901 (add-hook 'find-file-hooks #'set-viper-state-in-major-mode)
903 ;; keep this because many modes we don't know about use this hook
904 (defvar text-mode-hook)
905 (add-hook 'text-mode-hook #'viper-mode)
907 (defvar emerge-startup-hook)
908 (add-hook 'emerge-startup-hook #'viper-change-state-to-emacs)
910 ;; if viper is started from .emacs, it might be impossible to get certain
911 ;; info about the display and windows until emacs initialization is complete
912 ;; So do it via the window-setup-hook
913 (add-hook 'window-setup-hook
914 (lambda ()
915 (modify-frame-parameters
916 (selected-frame)
917 (list (cons 'viper-vi-state-cursor-color
918 (viper-get-cursor-color))))
919 (setq viper-vi-state-cursor-color (viper-get-cursor-color))
922 ;; Tell vc-diff to put *vc* in Vi mode
923 (viper--advice-add 'vc-diff :after #'viper-change-state-to-vi)
924 (viper--advice-add 'emerge-quit :after #'viper-change-state-to-vi)
926 ;; passwd.el sets up its own buffer, which turns up in Vi mode,
927 ;; thus overriding the local map. We don't need Vi mode here.
928 (viper--advice-add 'read-passwd-1 :before #'viper-change-state-to-emacs)
930 (viper--advice-add 'self-insert-command :around
931 (lambda (orig-fun &rest args)
932 ;; FIXME: Use remapping?
933 (if (and (eq viper-current-state 'vi-state)
934 ;; Do not use called-interactively-p here. XEmacs does not have it
935 ;; and interactive-p is just fine.
936 ;; (called-interactively-p 'interactive))
937 (interactive-p))
938 (beep 1)
939 (apply orig-fun args))))
941 (viper--advice-add 'set-cursor-color :after
942 (lambda (color-name)
943 "Change cursor color in VI state."
944 (modify-frame-parameters
945 (selected-frame)
946 (list (cons 'viper-vi-state-cursor-color color-name)))
947 (setq viper-vi-state-cursor-color color-name)))
950 (when (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
951 ;; needs to be as early as possible
952 (add-to-ordered-list
953 'emulation-mode-map-alists 'viper--intercept-key-maps 100)
954 ;; needs to be after cua-mode
955 (add-to-ordered-list 'emulation-mode-map-alists 'viper--key-maps 500)
958 ;; Emacs shell, ange-ftp, and comint-based modes
959 (add-hook 'comint-mode-hook #'viper-comint-mode-hook) ; comint
960 (add-hook 'erc-mode-hook #'viper-comint-mode-hook) ; ERC
962 (add-hook 'eshell-mode-hook
963 (lambda () (setq viper-auto-indent nil)))
965 (viper-set-emacs-state-searchstyle-macros nil 'dired-mode) ; dired
966 (viper-set-emacs-state-searchstyle-macros nil 'tar-mode) ; tar
967 (viper-set-emacs-state-searchstyle-macros nil 'mh-folder-mode) ; mhe
968 (viper-set-emacs-state-searchstyle-macros nil 'gnus-group-mode) ; gnus
969 (viper-set-emacs-state-searchstyle-macros nil 'gnus-summary-mode)
970 (viper-set-emacs-state-searchstyle-macros nil 'Info-mode) ; info
971 (viper-set-emacs-state-searchstyle-macros nil 'Buffer-menu-mode) ;buffer-menu
973 ;; Modify major modes according to viper-major-mode-modifier-list
974 (viper-apply-major-mode-modifiers)
976 ;; For RMAIL users.
977 ;; Put buf in Emacs state after edit.
978 (viper--advice-add 'rmail-cease-edit :after #'viper-change-state-to-emacs)
980 ;; ISO accents.
981 (viper--advice-add 'iso-accents-mode :after
982 (lambda (arg &rest _)
983 "Set viper-automatic-iso-accents to iso-accents-mode."
984 (defvar iso-accents-mode)
985 (setq viper-automatic-iso-accents
986 (if (eq viper-current-state 'vi-state)
987 (if arg
988 ;; if iso-accents-mode was called with positive arg, turn
989 ;; accents on
990 (> (prefix-numeric-value arg) 0)
991 ;; else: toggle viper-automatic-iso-accents
992 (not viper-automatic-iso-accents))
993 ;; other states: accept what iso-accents-mode has done
994 iso-accents-mode))
995 ;; turn off ISO accents in vi-state
996 (if (eq viper-current-state 'vi-state)
997 (viper-set-iso-accents-mode nil))
998 (if (memq viper-current-state '(vi-state insert-state replace-state))
999 (message "Viper ISO accents mode: %s"
1000 (if viper-automatic-iso-accents "on" "off")))
1003 ;; International input methods
1004 (if nil ;; (featurep 'emacs) ;;The hooks should now work!
1005 (progn
1006 (viper--advice-add 'deactivate-input-method :after
1007 #'viper-deactivate-input-method-action)
1008 (viper--advice-add 'activate-input-method :after
1009 #'viper-activate-input-method-action))
1010 ;; XEmacs Although these hooks exist in Emacs, they don't seem to be always
1011 ;; called on input-method activation/deactivation, so we the above advise
1012 ;; functions instead.
1013 (eval-after-load "mule-cmds"
1014 '(progn
1015 (add-hook 'input-method-activate-hook
1016 #'viper-activate-input-method-action t)
1017 (add-hook 'input-method-deactivate-hook
1018 #'viper-deactivate-input-method-action t)))
1020 (viper--advice-add 'toggle-input-method :around
1021 (lambda (orig-fun &rest args)
1022 "Adjust input-method toggling in vi-state."
1023 (if (and viper-special-input-method (eq viper-current-state 'vi-state))
1024 (viper-deactivate-input-method)
1025 (apply orig-fun args))))
1027 ) ; viper-set-hooks
1030 ;; these are primarily advices and Vi-ish variable settings
1031 (defun viper-non-hook-settings ()
1033 ;;;; Viper changes the default mode-line-buffer-identification
1034 ;;(setq-default mode-line-buffer-identification '(" %b"))
1036 ;; setup emacs-supported vi-style feel
1037 (setq next-line-add-newlines nil
1038 require-final-newline t)
1040 ;; don't bark when mark is inactive
1041 (if (featurep 'emacs)
1042 (setq mark-even-if-inactive t))
1044 (setq scroll-step 1)
1046 ;; Variable displaying the current Viper state in the mode line.
1047 (or (memq 'viper-mode-string global-mode-string)
1048 (setq global-mode-string
1049 (append '("" viper-mode-string) (cdr global-mode-string))))
1051 (if (featurep 'xemacs)
1052 ;; XEmacs
1053 (defadvice describe-key (before viper-describe-key-ad protect activate)
1054 "Force to read key via `viper-read-key-sequence'."
1055 (interactive (list (viper-read-key-sequence "Describe key: "))))
1056 ;; Emacs
1057 (viper--advice-add 'describe-key :before
1058 (lambda (&rest _)
1059 "Force to read key via `viper-read-key-sequence'."
1060 (interactive (let ((key (viper-read-key-sequence
1061 "Describe key (or click or menu item): ")))
1062 (list key
1063 (prefix-numeric-value current-prefix-arg)
1064 ;; If KEY is a down-event, read also the
1065 ;; corresponding up-event.
1066 (and (vectorp key)
1067 (let ((last-idx (1- (length key))))
1068 (and (eventp (aref key last-idx))
1069 (memq 'down (event-modifiers
1070 (aref key last-idx)))))
1071 (or (and (eventp (aref key 0))
1072 (memq 'down (event-modifiers
1073 (aref key 0)))
1074 ;; For the C-down-mouse-2 popup menu,
1075 ;; there is no subsequent up-event
1076 (= (length key) 1))
1077 (and (> (length key) 1)
1078 (eventp (aref key 1))
1079 (memq 'down (event-modifiers (aref key 1)))))
1080 (read-event)))))
1081 nil))
1083 ) ; (if (featurep 'xemacs)
1085 (if (featurep 'xemacs)
1086 ;; XEmacs
1087 (defadvice describe-key-briefly
1088 (before viper-describe-key-briefly-ad protect activate)
1089 "Force to read key via `viper-read-key-sequence'."
1090 (interactive (list (viper-read-key-sequence "Describe key briefly: "))))
1091 ;; Emacs
1092 (viper--advice-add 'describe-key-briefly :before
1093 (lambda (&rest _)
1094 "Force to read key via `viper-read-key-sequence'."
1095 (interactive (let ((key (viper-read-key-sequence
1096 "Describe key (or click or menu item): ")))
1097 ;; If KEY is a down-event, read and discard the
1098 ;; corresponding up-event.
1099 (and (vectorp key)
1100 (let ((last-idx (1- (length key))))
1101 (and (eventp (aref key last-idx))
1102 (memq 'down (event-modifiers (aref key last-idx)))))
1103 (read-event))
1104 (list key
1105 (if current-prefix-arg
1106 (prefix-numeric-value current-prefix-arg))
1107 1)))
1108 nil))
1109 ) ; (if (featurep 'xemacs)
1111 ;; FIXME: The default already uses read-file-name, so it looks like this
1112 ;; advice is not needed any more.
1113 ;; (defadvice find-file (before viper-add-suffix-advice activate)
1114 ;; "Use `read-file-name' for reading arguments."
1115 ;; (interactive (cons (read-file-name "Find file: " nil default-directory)
1116 ;; ;; XEmacs: if Mule & prefix arg, ask for coding system
1117 ;; (cond ((and (featurep 'xemacs) (featurep 'mule))
1118 ;; (list
1119 ;; (and current-prefix-arg
1120 ;; (read-coding-system "Coding-system: "))))
1121 ;; ;; Emacs: do wildcards
1122 ;; ((and (featurep 'emacs) (boundp 'find-file-wildcards))
1123 ;; (list find-file-wildcards))))
1124 ;; ))
1125 ;; (defadvice find-file-other-window (before viper-add-suffix-advice activate)
1126 ;; "Use `read-file-name' for reading arguments."
1127 ;; (interactive (cons (read-file-name "Find file in other window: "
1128 ;; nil default-directory)
1129 ;; ;; XEmacs: if Mule & prefix arg, ask for coding system
1130 ;; (cond ((and (featurep 'xemacs) (featurep 'mule))
1131 ;; (list
1132 ;; (and current-prefix-arg
1133 ;; (read-coding-system "Coding-system: "))))
1134 ;; ;; Emacs: do wildcards
1135 ;; ((and (featurep 'emacs) (boundp 'find-file-wildcards))
1136 ;; (list find-file-wildcards))))
1137 ;; ))
1138 ;; (defadvice find-file-other-frame (before viper-add-suffix-advice activate)
1139 ;; "Use `read-file-name' for reading arguments."
1140 ;; (interactive (cons (read-file-name "Find file in other frame: "
1141 ;; nil default-directory)
1142 ;; ;; XEmacs: if Mule & prefix arg, ask for coding system
1143 ;; (cond ((and (featurep 'xemacs) (featurep 'mule))
1144 ;; (list
1145 ;; (and current-prefix-arg
1146 ;; (read-coding-system "Coding-system: "))))
1147 ;; ;; Emacs: do wildcards
1148 ;; ((and (featurep 'emacs) (boundp 'find-file-wildcards))
1149 ;; (list find-file-wildcards))))
1150 ;; ))
1153 (viper--advice-add 'read-file-name :around
1154 (lambda (orig-fun &rest args)
1155 "Tell `exit-minibuffer' to run `viper-file-add-suffix' as a hook."
1156 (let ((viper-minibuffer-exit-hook
1157 (append viper-minibuffer-exit-hook
1158 '(viper-minibuffer-trim-tail viper-file-add-suffix))))
1159 (apply orig-fun args))))
1161 (viper--advice-add 'start-kbd-macro :after
1162 (lambda (&rest _)
1163 "Remove Viper's intercepting bindings for C-x ).
1164 This may be needed if the previous `:map' command terminated abnormally."
1165 (define-key viper-vi-intercept-map "\C-x)" nil)
1166 (define-key viper-insert-intercept-map "\C-x)" nil)
1167 (define-key viper-emacs-intercept-map "\C-x)" nil)))
1169 (viper--advice-add 'add-minor-mode :after
1170 (lambda (&rest _)
1171 "Run viper-normalize-minor-mode-map-alist after adding a minor mode."
1172 (viper-normalize-minor-mode-map-alist)
1173 (unless
1174 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
1175 (setq-default minor-mode-map-alist minor-mode-map-alist))))
1177 ;; catch frame switching event
1178 (if (viper-window-display-p)
1179 (if (featurep 'xemacs)
1180 (add-hook 'mouse-leave-frame-hook
1181 #'viper-remember-current-frame)
1182 (viper--advice-add 'handle-switch-frame :before
1183 (lambda (&rest _)
1184 "Remember the selected frame before the switch-frame event."
1185 (viper-remember-current-frame (selected-frame))))))
1187 ) ; end viper-non-hook-settings
1191 ;; Ask only if this-command/last-command are nil, i.e., when loading
1192 (cond ((and (eq viper-mode 'ask) (null this-command) (null last-command))
1193 (save-window-excursion
1194 (with-output-to-temp-buffer " *viper-info*"
1195 (princ "
1196 You have loaded Viper, and are about to Viperize your Emacs!
1198 Viper is a Package for Emacs Rebels and a venomous VI PERil,
1200 It's time to decide: to Viperize or not to Viperize...
1202 If you wish to Viperize AND make this your way of life, please put
1204 (setq viper-mode t)
1205 (require 'viper)
1207 in your init file (preferably, close to the top).
1208 These two lines must come in the order given."))
1209 (if (y-or-n-p "Viperize? ")
1210 (setq viper-mode t)
1211 (setq viper-mode nil))
1212 (message "")
1213 (kill-buffer " *viper-info*")))
1215 ;; If viper-mode is t, then just continue. Viper will kick in.
1216 ((eq viper-mode t))
1217 ;; Otherwise, it was asking Viper was not loaded through .emacs
1218 ;; In this case, it was either through M-x viper-mode or via something
1219 ;; else, like the custom widget. If Viper was loaded through
1220 ;; M-x viper-mode, then viper will kick in anyway.
1221 (t (setq viper-mode nil)))
1223 (defun viper-load-custom-file ()
1224 (if (and (file-exists-p viper-custom-file-name)
1225 (not noninteractive))
1226 (load viper-custom-file-name)))
1233 ;; save non-viper vars that Viper might change
1234 (if (null viper-saved-non-viper-variables)
1235 (setq viper-saved-non-viper-variables
1236 (list
1237 (cons 'major-mode (list (default-value 'major-mode)))
1238 (cons 'next-line-add-newlines (list next-line-add-newlines))
1239 (cons 'require-final-newline (list require-final-newline))
1240 (cons 'scroll-step (list scroll-step))
1241 (cons 'mode-line-buffer-identification
1242 (list (default-value 'mode-line-buffer-identification)))
1243 (cons 'global-mode-string (list global-mode-string))
1244 (if (featurep 'emacs)
1245 (cons 'mark-even-if-inactive (list mark-even-if-inactive)))
1249 ;; Set some useful macros, advices
1250 ;; These must be BEFORE viper-custom-file-name is loaded,
1251 ;; so the user can unrecord them in viper-custom-file-name.
1252 (if viper-mode
1253 (progn
1254 ;; set advices and some variables that give emacs Vi look.
1255 (viper-non-hook-settings)
1257 ;; repeat the 2nd previous command without rotating the command history
1258 (viper-record-kbd-macro
1259 (vector viper-repeat-from-history-key '\1) 'vi-state
1260 [(meta x) v i p e r - r e p e a t - f r o m - h i s t o r y return] 't)
1261 ;; repeat the 3d previous command without rotating the command history
1262 (viper-record-kbd-macro
1263 (vector viper-repeat-from-history-key '\2) 'vi-state
1264 [(meta x) v i p e r - r e p e a t - f r o m - h i s t o r y return] 't)
1266 ;; set macros for toggling case sensitivity and regexp search
1267 (viper-set-searchstyle-toggling-macros nil)
1268 ;; Make %%% toggle parsing comments for matching parentheses
1269 (viper-set-parsing-style-toggling-macro nil)
1271 ;; viper-custom-file-name is loaded if exists
1272 (viper-load-custom-file)
1274 ;; should be after loading custom file to avoid the pesky msg that
1275 ;; mouse-search/insert keys are already bound
1276 (viper-bind-mouse-search-key)
1277 (viper-bind-mouse-insert-key)
1282 ;; Applying Viper customization -- runs after (load viper-custom-file-name)
1284 ;; Save user settings or Viper defaults for vars controlled by
1285 ;; viper-expert-level
1286 (if (null viper-saved-user-settings)
1287 (setq viper-saved-user-settings
1288 (list (cons 'viper-want-ctl-h-help (list viper-want-ctl-h-help))
1289 (cons 'viper-always (list viper-always))
1290 (cons 'viper-no-multiple-ESC (list viper-no-multiple-ESC))
1291 (cons 'viper-ex-style-motion (list viper-ex-style-motion))
1292 (cons 'viper-ex-style-editing
1293 (list viper-ex-style-editing))
1294 (cons 'viper-want-emacs-keys-in-vi
1295 (list viper-want-emacs-keys-in-vi))
1296 (cons 'viper-electric-mode (list viper-electric-mode))
1297 (cons 'viper-want-emacs-keys-in-insert
1298 (list viper-want-emacs-keys-in-insert))
1299 (cons 'viper-re-search (list viper-re-search)))))
1303 ;; Intercept maps could go in viper-keym.el
1304 ;; We keep them here in case someone redefines them in viper-custom-file-name
1306 (define-key viper-vi-intercept-map viper-ESC-key #'viper-intercept-ESC-key)
1307 (define-key viper-insert-intercept-map viper-ESC-key #'viper-intercept-ESC-key)
1309 ;; This is taken care of by viper-insert-global-user-map.
1310 ;;(define-key viper-replace-map viper-ESC-key #'viper-intercept-ESC-key)
1313 ;; The default viper-toggle-key is \C-z; for the novice, it suspends or
1314 ;; iconifies Emacs
1315 (define-key viper-vi-intercept-map viper-toggle-key 'viper-toggle-key-action)
1316 (define-key
1317 viper-emacs-intercept-map viper-toggle-key #'viper-change-state-to-vi)
1319 ;; Removed to avoid bad interaction with cua-mode.
1320 ;; Escape from Emacs and Insert modes to Vi for one command
1321 ;;(define-key viper-emacs-intercept-map "\C-c\\" 'viper-escape-to-vi)
1322 ;;(define-key viper-insert-intercept-map "\C-c\\" 'viper-escape-to-vi)
1324 (when viper-mode
1325 (viper-set-minibuffer-style)
1326 (if viper-buffer-search-char
1327 (viper-buffer-search-enable))
1328 (viper-update-syntax-classes 'set-default)
1330 ;; Familiarize Viper with some minor modes that have their own keymaps
1331 (viper-harness-minor-mode "compile")
1332 (viper-harness-minor-mode "outline")
1333 (viper-harness-minor-mode "allout")
1334 (viper-harness-minor-mode "xref")
1335 (viper-harness-minor-mode "lmenu")
1336 (viper-harness-minor-mode "vc")
1337 (viper-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX, which
1338 (viper-harness-minor-mode "latex") ; sits in one of these two files
1339 (viper-harness-minor-mode "cyrillic")
1340 (viper-harness-minor-mode "russian")
1341 (viper-harness-minor-mode "view-less")
1342 (viper-harness-minor-mode "view")
1343 (viper-harness-minor-mode "reftex")
1344 (viper-harness-minor-mode "flyspell")
1346 (setq-default viper-emacs-intercept-minor-mode t
1347 viper-emacs-local-user-minor-mode t
1348 viper-emacs-global-user-minor-mode t
1349 viper-emacs-kbd-minor-mode t
1350 viper-emacs-state-modifier-minor-mode t)
1351 (if (eq viper-current-state 'emacs-state)
1352 (setq viper-emacs-intercept-minor-mode t
1353 viper-emacs-local-user-minor-mode t
1354 viper-emacs-global-user-minor-mode t
1355 viper-emacs-kbd-minor-mode t
1356 viper-emacs-state-modifier-minor-mode t))
1359 (if (or viper-always
1360 (and (< viper-expert-level 5) (> viper-expert-level 0)))
1361 (viper-set-hooks))
1363 ;; Let all minor modes take effect after loading.
1364 ;; This may not be enough, so we also set default minor-mode-alist.
1365 ;; Without setting the default, new buffers that come up in emacs mode have
1366 ;; minor-mode-map-alist = nil, unless we call viper-change-state-*
1367 (when (eq viper-current-state 'emacs-state)
1368 (viper-change-state-to-emacs)
1369 (unless
1370 (and (fboundp 'add-to-ordered-list)
1371 (boundp 'emulation-mode-map-alists))
1372 (setq-default minor-mode-map-alist minor-mode-map-alist))
1375 (if (this-major-mode-requires-vi-state major-mode)
1376 (viper-mode))
1378 (add-function :after initial-major-mode #'set-viper-state-in-major-mode))
1382 (run-hooks 'viper-load-hook) ; the last chance to change something
1384 (provide 'viper)
1387 ;; Local Variables:
1388 ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1389 ;; End:
1391 ;;; viper.el ends here