*** empty log message ***
[emacs.git] / lisp / emulation / viper.el
blobbc59c077213385311a4a393bcdc6f530eaeac289
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 (defconst viper-version "2.85 of February 14, 1996"
10 "The current version of Viper")
12 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
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
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30 ;;; Commentary:
32 ;; 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 ;; full access to all Emacs facilities. Viper supports multiple undo,
35 ;; file name completion, command, file, and search history and it extends
36 ;; Vi in many other ways. Viper is highly customizable through the various
37 ;; hooks, user variables, and keymaps. It is implemented as a collection
38 ;; of minor modes and it is designed to provide full access to all Emacs
39 ;; major and minor modes.
41 ;;; History
43 ;; Viper is a new name for a package formerly known as VIP-19,
44 ;; which was a successor of VIP version 3.5 by Masahiko Sato
45 ;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
46 ;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
47 ;; were also shamelessly plagiarized.
49 ;; Viper maintains some degree of compatibility with these older
50 ;; packages. See the documentation for customization.
52 ;; The main difference between Viper and these older packages are:
54 ;; 1. Viper emulates Vi at several levels, from almost complete conformity
55 ;; to a rather loose Vi-compliance.
57 ;; 2. Viper provides full access to all major and minor modes of Emacs
58 ;; without the need to type extra keys.
59 ;; The older versions of VIP (and other Vi emulators) do not work with
60 ;; some major and minor modes.
62 ;; 3. Viper supports vi-style undo.
64 ;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
66 ;; 5. Viper has a better interface to ex, including command, variable, and
67 ;; file name completion.
69 ;; 6. Viper uses native Emacs history and completion features; it doesn't
70 ;; rely on other packages (such as gmhist.el and completer.el) to provide
71 ;; these features.
73 ;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
74 ;; user to switch from Insert state to Vi state to Replace state, etc.
76 ;; 8. Viper keeps history of recently inserted pieces of text and recently
77 ;; executed Vi-style destructive commands, such as `i', `d', etc.
78 ;; These pieces of text can be inserted in later insertion commands;
79 ;; the previous destructive commands can be re-executed.
81 ;; 9. Viper has Vi-style keyboard macros, which enhances the similar
82 ;; facility in the original Vi.
83 ;; First, one can execute any Emacs command while defining a
84 ;; macro, not just the Vi commands. Second, macros are defined in a
85 ;; WYSYWYG mode, using an interface to Emacs' WYSIWYG style of defining
86 ;; 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 ;; The same macro name can have several different definitions:
89 ;; one global, several definitions for various major modes, and
90 ;; definitions for specific buffers.
91 ;; Bffer-specific definitions override mode-specific
92 ;; definitions, which, in turn, override global definitions.
95 ;;; Installation:
96 ;; -------------
98 ;; (require 'viper)
101 ;;; Acknowledgements:
102 ;; -----------------
103 ;; Bug reports and ideas contributed by the following users
104 ;; have helped improve Viper and the various versions of VIP.
105 ;; See the on-line manual for a complete list of contributors.
108 ;;; Notes:
110 ;; 1. Major modes.
111 ;; In most cases, Viper handles major modes correctly, i.e., they come up
112 ;; in the right state (either vi-state or emacs-state). For instance, text
113 ;; files come up in vi-state, while, say, Dired appears in emacs-state by
114 ;; default.
115 ;; However, some modes do not appear in the right mode in the beginning,
116 ;; usually because they neglect to follow Emacs conventions (e.g., they don't
117 ;; use kill-all-local-variables when they start). Some major modes
118 ;; may fail to come up in emacs-state if they call hooks, such as
119 ;; text-hook, for no good reason.
121 ;; As an immediate solution, you can hit C-z to bring about the right mode.
122 ;; An interim solution is to add an appropriate hook to the mode like this:
124 ;; (add-hook 'your-favorite-mode 'viper-mode)
125 ;; or
126 ;; (add-hook 'your-favorite-mode 'vip-change-state-to-emacs)
128 ;; whichever applies. The right thing to do, however, is to complain to the
129 ;; author of the respective package. (Sometimes they also neglect to equip
130 ;; their modes with hooks, which is one more reason for complaining.)
132 ;; 2. Keymap handling
133 ;; Because Emacs 19 has an elegant mechanism for turning minor mode keymaps
134 ;; on and off, implementation of Viper has been greatly simplified. Viper
135 ;; has several minor modes.
137 ;; Viper's Vi state consists of seven minor modes:
139 ;; vip-vi-intercept-minor-mode
140 ;; vip-vi-local-user-minor-mode
141 ;; vip-vi-global-user-minor-mode
142 ;; vip-vi-kbd-minor-mode
143 ;; vip-vi-state-modifier-minor-mode
144 ;; vip-vi-diehard-minor-mode
145 ;; vip-vi-basic-minor-mode
147 ;; Bindings done to the keymap of the first mode overshadow those done to
148 ;; the second, which, in turn, overshadows those done to the third, etc.
150 ;; The last vip-vi-basic-minor-mode contains most of the usual Vi bindings
151 ;; in its edit mode. This mode provides access to all Emacs facilities.
152 ;; Novice users, however, may want to set their vip-expert-level to 1
153 ;; 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 ;; For instance, C-c will not have its standard Emacs binding
156 ;; and so many of the goodies of Emacs are not available.
158 ;; An 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 ;; In this case, vip-vi-diehard-minor-mode is inactive.
162 ;; Viper gurus should have at least
163 ;; (setq vip-expert-level 4)
164 ;; in their ~/.vip files. This will unsuppress all Emacs keys that are not
165 ;; essential for VI-style editing.
166 ;; Pick-and-choose users may want to put
167 ;; (setq vip-expert-level 5)
168 ;; in ~/.vip. Viper will then leave it up to the user to set the variables
169 ;; vip-want-* See vip-set-expert-level for details.
171 ;; The very first minor mode, vip-vi-intercept-minor-mode, is of no
172 ;; concern for the user. It is needed to bind Viper's vital keys, such as
173 ;; ESC and C-z.
175 ;; The second mode, vip-vi-local-user-minor-mode, usually has an
176 ;; empty keymap. However, the user can set bindings in this keymap, which
177 ;; will overshadow the corresponding bindings in the other two minor
178 ;; modes. This is useful, for example, for setting up ZZ in gnus,
179 ;; rmail, mh-e, etc., to send message instead of saving it in a file.
180 ;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
181 ;; that would visit the next or the previous file in the Dired buffer.
182 ;; Setting local keys is tricky, so don't do it directly. Instead, use
183 ;; vip-add-local-keys function (see its doc).
185 ;; The third minor mode, vip-vi-global-user-minor-mode, is also intended
186 ;; for the users but, unlike vip-vi-local-user-minor-mode, its key
187 ;; bindings are seen in all Viper buffers. This mode keys can be done
188 ;; with define-key command.
190 ;; The fourth minor mode, vip-vi-kbd-minor-mode, is used by keyboard
191 ;; macros. Users are NOT supposed to modify this keymap directly.
193 ;; The fifth mode, vip-vi-state-modifier-minor-mode, can be used to set
194 ;; key bindings that are visible in some major modes but not in others.
196 ;; Users are allowed to modify keymaps that belong to
197 ;; vip-vi-local-user-minor-mode, vip-vi-global-user-minor-mode,
198 ;; and vip-vi-state-modifier-minor-mode only.
200 ;; Viper's Insert state also has seven minor modes:
202 ;; vip-insert-intercept-minor-mode
203 ;; vip-insert-local-user-minor-mode
204 ;; vip-insert-global-user-minor-mode
205 ;; vip-insert-kbd-minor-mode
206 ;; vip-insert-state-modifier-minor-mode
207 ;; vip-insert-diehard-minor-mode
208 ;; vip-insert-basic-minor-mode
210 ;; As with VI's editing modes, the first mode, vip-insert-intercept-minor-mode
211 ;; is used to bind vital keys that are not to be changed by the user.
213 ;; The next mode, vip-insert-local-user-minor-mode, is used to customize
214 ;; bindings in the insert state of Viper. The third mode,
215 ;; vip-insert-global-user-minor-mode is like
216 ;; vip-insert-local-user-minor-mode, except that its bindings are seen in
217 ;; all Viper buffers. As with vip-vi-local-user-minor-mode, its bindings
218 ;; should be done via the function vip-add-local-keys. Bindings for
219 ;; vip-insert-global-user-minor-mode can be set with the define-key command.
221 ;; The next minor mode, vip-insert-kbd-minor-mode,
222 ;; is used for keyboard VI-style macros defined with :map!.
224 ;; The fifth minor mode, vip-insert-state-modifier-minor-mode, is like
225 ;; vip-vi-state-modifier-minor-mode, except that it is used in the Insert
226 ;; state; it can be used to modify keys in a mode-specific fashion.
228 ;; The minor mode vip-insert-diehard-minor-mode is in effect when
229 ;; the user wants a high degree of Vi compatibility (a bad idea, really!).
230 ;; The last minor mode, vip-insert-basic-minor-mode, is always in effect
231 ;; when Viper is in insert state. It binds a small number of keys needed for
232 ;; Viper's operation.
234 ;; Finally, Viper provides minor modes for overriding bindings set by Emacs
235 ;; modes when Viper is in Emacs state:
237 ;; vip-emacs-local-user-minor-mode
238 ;; vip-emacs-global-user-minor-mode
239 ;; vip-emacs-kbd-minor-mode
240 ;; vip-emacs-state-modifier-minor-mode
242 ;; These minor modes are in effect when Viper is in Emacs state. The keymap
243 ;; associated with vip-emacs-global-user-minor-mode,
244 ;; vip-emacs-global-user-map, overrides the global and local keymaps as
245 ;; well as the minor mode keymaps set by other modes. The keymap of
246 ;; vip-emacs-local-user-minor-mode, vip-emacs-local-user-map, overrides
247 ;; everything, but it is used on a per buffer basis.
248 ;; The keymap associated with vip-emacs-state-modifier-minor-mode
249 ;; overrides keys on a per-major-mode basis. The mode
250 ;; vip-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
251 ;; state.
253 ;; 3. There is also one minor mode that is used when Viper is in its
254 ;; replace-state (used for commands like cw, C, etc.). This mode is
255 ;; called
257 ;; vip-replace-minor-mode
259 ;; and its keymap is vip-replace-map. Replace minor mode is always
260 ;; used in conjunction with the minor modes for insert-state, and its
261 ;; keymap overshadows the keymaps for insert minor modes.
263 ;; 4. Defining buffer-local bindings in Vi and Insert modes.
264 ;; As mentioned before, sometimes, it is convenient to have
265 ;; buffer-specific of mode-specific key bindings in Vi and insert modes.
266 ;; Viper provides a special function, vip-add-local-keys, to do precisely
267 ;; this. For instance, is you need to add couple of mode-specific bindings
268 ;; to Insert mode, you can put
270 ;; (vip-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
272 ;; somewhere in a hook of this major mode. If you put something like this
273 ;; in your own elisp function, this will define bindings specific to the
274 ;; buffer that was current at the time of the call to vip-add-local-keys.
275 ;; The only thing to make sure here is that the major mode of this buffer
276 ;; is written according to Emacs conventions, which includes a call to
277 ;; (kill-all-local-variables). See vip-add-local-keys for more details.
280 ;; TO DO (volunteers?):
282 ;; 1. Some of the code that is inherited from VIP-3.5 is rather
283 ;; convoluted. Instead of vip-command-argument, keymaps should bind the
284 ;; actual commands. E.g., "dw" should be bound to a generic command
285 ;; vip-delete that will delete things based on the value of
286 ;; last-command-char. This would greatly simplify the logic and the code.
288 ;; 2. Somebody should venture to write a customization package a la
289 ;; options.el that would allow the user to change values of variables
290 ;; that meet certain specs (e.g., match a regexp) and whose doc string
291 ;; starts with a '*'. Then, the user should be offered to save
292 ;; variables that were changed. This will make user's customization job
293 ;; much easier.
297 (require 'advice)
298 (require 'cl)
299 (require 'ring)
301 (require 'viper-util)
304 ;;; Variables
306 ;; Is t until viper-mode executes for the very first time.
307 ;; Prevents recursive descend into startup messages.
308 (defvar vip-first-time t)
310 (defvar vip-expert-level 0
311 "User's expert level.
312 The minor mode vip-vi-diehard-minor-mode is in effect when
313 vip-expert-level is 1 or 2 or when vip-want-emacs-keys-in-vi is t.
314 The minor mode vip-insert-diehard-minor-mode is in effect when
315 vip-expert-level is 1 or 2 or if vip-want-emacs-keys-in-insert is t.
316 Use `M-x vip-set-expert-level' to change this.")
318 ;; Max expert level supported by Viper. This is NOT a user option.
319 ;; It is here to make it hard for the user from resetting it.
320 (defconst vip-max-expert-level 5)
322 ;; Contains user settings for vars affected by vip-set-expert-level function.
323 ;; Not a user option.
324 (defvar vip-saved-user-settings nil)
327 ;;; Viper minor modes
329 ;; This is not local in Emacs, so we make it local.
330 (make-variable-buffer-local 'minor-mode-map-alist)
332 ;; Mode for vital things like \e, C-z.
333 (vip-deflocalvar vip-vi-intercept-minor-mode nil)
335 (vip-deflocalvar vip-vi-basic-minor-mode nil
336 "Viper's minor mode for Vi bindings.")
338 (vip-deflocalvar vip-vi-local-user-minor-mode nil
339 "Auxiliary minor mode for user-defined local bindings in Vi state.")
341 (vip-deflocalvar vip-vi-global-user-minor-mode nil
342 "Auxiliary minor mode for user-defined global bindings in Vi state.")
344 (vip-deflocalvar vip-vi-state-modifier-minor-mode nil
345 "Minor mode used to make major-mode-specific modification to Vi state.")
347 (vip-deflocalvar vip-vi-diehard-minor-mode nil
348 "This minor mode is in effect when the user wants Viper to be Vi.")
350 (vip-deflocalvar vip-vi-kbd-minor-mode nil
351 "Minor mode for Ex command macros in Vi state.
352 The corresponding keymap stores key bindings of Vi macros defined with
353 the Ex command :map.")
355 ;; Mode for vital things like \e, C-z.
356 (vip-deflocalvar vip-insert-intercept-minor-mode nil)
358 (vip-deflocalvar vip-insert-basic-minor-mode nil
359 "Viper's minor mode for bindings in Insert mode.")
361 (vip-deflocalvar vip-insert-local-user-minor-mode nil
362 "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
363 This is a way to overshadow normal Insert mode bindings locally to certain
364 designated buffers.")
366 (vip-deflocalvar vip-insert-global-user-minor-mode nil
367 "Auxiliary minor mode for global user-defined bindings in Insert state.")
369 (vip-deflocalvar vip-insert-state-modifier-minor-mode nil
370 "Minor mode used to make major-mode-specific modification to Insert state.")
372 (vip-deflocalvar vip-insert-diehard-minor-mode nil
373 "Minor mode that simulates Vi very closely.
374 Not recommened, except for the novice user.")
376 (vip-deflocalvar vip-insert-kbd-minor-mode nil
377 "Minor mode for Ex command macros Insert state.
378 The corresponding keymap stores key bindings of Vi macros defined with
379 the Ex command :map!.")
381 (vip-deflocalvar vip-replace-minor-mode nil
382 "Minor mode in effect in replace state (cw, C, and the like commands).")
384 ;; Mode for vital things like \C-z and \C-x)
385 ;; This is t, by default. So, any new buffer will have C-z defined as
386 ;; switch to Vi, unless we switched states in this buffer
387 (vip-deflocalvar vip-emacs-intercept-minor-mode t)
389 (vip-deflocalvar vip-emacs-local-user-minor-mode t
390 "Minor mode for local user bindings effective in Emacs state.
391 Users can use it to override Emacs bindings when Viper is in its Emacs
392 state.")
394 (vip-deflocalvar vip-emacs-global-user-minor-mode t
395 "Minor mode for global user bindings in effect in Emacs state.
396 Users can use it to override Emacs bindings when Viper is in its Emacs
397 state.")
399 (vip-deflocalvar vip-emacs-kbd-minor-mode t
400 "Minor mode for Vi style macros in Emacs state.
401 The corresponding keymap stores key bindings of Vi macros defined with
402 `vip-record-kbd-macro' command. There is no Ex-level command to do this
403 interactively.")
405 (vip-deflocalvar vip-emacs-state-modifier-minor-mode t
406 "Minor mode used to make major-mode-specific modification to Emacs state.
407 For instance, a Vi purist may want to bind `dd' in Dired mode to a function
408 that deletes a file.")
412 ;;; ISO characters
414 (vip-deflocalvar vip-automatic-iso-accents nil
415 "*If non-nil, ISO accents will be turned on in insert/replace emacs states and turned off in vi-state.
416 For some users, this behavior may be too primitive. In this case, use
417 insert/emacs/vi state hooks.")
420 ;;; Emacs keys in other states.
422 (defvar vip-want-emacs-keys-in-insert t
423 "*Set to nil if you want complete Vi compatibility in insert mode.
424 Complete compatibility with Vi is not recommended for power use of Viper.")
426 (defvar vip-want-emacs-keys-in-vi t
427 "*Set to nil if you want complete Vi compatibility in Vi mode.
428 Full Vi compatibility is not recommended for power use of Viper.")
432 ;; VI-style Undo
434 ;; Used to 'undo' complex commands, such as replace and insert commands.
435 (vip-deflocalvar vip-undo-needs-adjustment nil)
436 (put 'vip-undo-needs-adjustment 'permanent-local t)
438 ;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
439 ;; complex command that must be undone atomically. If inserted, it is
440 ;; erased by vip-change-state-to-vi and vip-repeat.
441 (defconst vip-buffer-undo-list-mark 'viper)
443 (defvar vip-keep-point-on-undo nil
444 "*Non-nil means not to move point while undoing commands.
445 This style is different from Emacs and Vi. Try it to see if
446 it better fits your working style.")
448 ;; Replace mode and changing text
450 ;; Viper's own after/before change functions, which get vip-add-hook'ed to
451 ;; Emacs's
452 (vip-deflocalvar vip-after-change-functions nil "")
453 (vip-deflocalvar vip-before-change-functions nil "")
454 (vip-deflocalvar vip-post-command-hooks nil "")
455 (vip-deflocalvar vip-pre-command-hooks nil "")
457 ;; Can be used to pass global states around for short period of time
458 (vip-deflocalvar vip-intermediate-command nil "")
460 ;; Indicates that the current destructive command has started in replace mode.
461 (vip-deflocalvar vip-began-as-replace nil "")
463 (defvar vip-replace-overlay-cursor-color "Red"
464 "*Cursor color to use in Replace state")
467 (vip-deflocalvar vip-replace-overlay nil "")
468 (put 'vip-replace-overlay 'permanent-local t)
470 (if (vip-has-face-support-p)
471 (progn
472 (make-face 'vip-replace-overlay-face)
473 (vip-hide-face 'vip-replace-overlay-face)
474 (or (face-differs-from-default-p 'vip-replace-overlay-face)
475 (progn
476 (if (vip-can-use-colors "darkseagreen2" "Black")
477 (progn
478 (set-face-background
479 'vip-replace-overlay-face "darkseagreen2")
480 (set-face-foreground 'vip-replace-overlay-face "Black")))
481 (set-face-underline-p 'vip-replace-overlay-face t))
484 (defvar vip-replace-overlay-face 'vip-replace-overlay-face
485 "*Face for highlighting replace regions on a window display.")
487 (defvar vip-replace-region-end-delimiter "$"
488 "A string marking the end of replacement regions.
489 It is used only with TTYs or if `vip-use-replace-region-delimiters'
490 is non-nil.")
491 (defvar vip-replace-region-start-delimiter ""
492 "A string marking the beginning of replacement regions.
493 It is used only with TTYs or if `vip-use-replace-region-delimiters'
494 is non-nil.")
495 (defvar vip-use-replace-region-delimiters
496 (or (not (vip-has-face-support-p)) (not (vip-color-display-p)))
497 "*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and
498 `vip-replace-region-start-delimiter' to delimit replacement regions, even on
499 color displays. By default, the delimiters are used only on TTYs or
500 monochrome displays.")
502 ;; XEmacs requires glyphs
503 (if vip-xemacs-p
504 (progn
505 (or (glyphp vip-replace-region-end-delimiter)
506 (setq vip-replace-region-end-delimiter
507 (make-glyph vip-replace-region-end-delimiter)))
508 (or (glyphp vip-replace-region-start-delimiter)
509 (setq vip-replace-region-start-delimiter
510 (make-glyph vip-replace-region-start-delimiter)))
514 ;; These are local marker that must be initialized to nil and moved with
515 ;; `vip-move-marker-locally'
517 ;; Remember the last position inside the replace region.
518 (vip-deflocalvar vip-last-posn-in-replace-region nil)
519 ;; Remember the last position while inserting
520 (vip-deflocalvar vip-last-posn-while-in-insert-state nil)
521 (put 'vip-last-posn-in-replace-region 'permanent-local t)
522 (put 'vip-last-posn-while-in-insert-state 'permanent-local t)
524 (vip-deflocalvar vip-sitting-in-replace nil "")
525 (put 'vip-sitting-in-replace 'permanent-local t)
527 ;; Remember the number of characters that have to be deleted in replace
528 ;; mode to compensate for the inserted characters.
529 (vip-deflocalvar vip-replace-chars-to-delete 0 "")
530 (vip-deflocalvar vip-replace-chars-deleted 0 "")
532 ;; Insertion ring and command ring
533 (defvar vip-insertion-ring-size 14
534 "The size of the insertion ring.")
535 ;; The insertion ring.
536 (defvar vip-insertion-ring nil)
537 ;; This is temp insertion ring. Used to do rotation for display purposes.
538 ;; When rotation just started, it is initialized to vip-insertion-ring.
539 (defvar vip-temp-insertion-ring nil)
540 (defvar vip-last-inserted-string-from-insertion-ring "")
542 (defvar vip-command-ring-size 14
543 "The size of the command ring.")
544 ;; The command ring.
545 (defvar vip-command-ring nil)
546 ;; This is temp command ring. Used to do rotation for display purposes.
547 ;; When rotation just started, it is initialized to vip-command-ring.
548 (defvar vip-temp-command-ring nil)
550 ;; Modes and related variables
552 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
553 (vip-deflocalvar vip-current-state 'emacs-state)
556 (defvar vip-toggle-key "\C-z"
557 "The key used to change states from emacs to Vi and back.
558 In insert mode, this key also functions as Meta.
559 Must be set in .vip file or prior to loading Viper.
560 This setting cannot be changed interactively.")
562 (defvar vip-ESC-key "\e"
563 "Key used to ESC.
564 Must be set in .vip file or prior to loading Viper.
565 This setting cannot be changed interactively.")
567 (defvar vip-no-multiple-ESC t
568 "*If true, multiple ESC in Vi mode will cause bell to ring.
569 \_ is then mapped to Meta.
570 This is set to t on a windowing terminal and to 'twice on a dumb
571 terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
572 enables cursor keys and is generally more convenient, as terminals usually
573 don't have a convenient Meta key.
574 Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
575 as is allowed by the major mode in effect.")
578 (defvar vip-want-ctl-h-help nil
579 "*If t then C-h is bound to help-command in insert mode, if nil then it is
580 bound to delete-backward-char.")
582 ;; Autoindent in insert
584 ;; Variable that keeps track of whether C-t has been pressed.
585 (vip-deflocalvar vip-cted nil "")
587 ;; Preserve the indent value, used by C-d in insert mode.
588 (vip-deflocalvar vip-current-indent 0)
590 ;; Whether to preserve the indent, used by C-d in insert mode.
591 (vip-deflocalvar vip-preserve-indent nil)
593 (vip-deflocalvar vip-auto-indent nil
594 "*Autoindent if t.")
595 (vip-deflocalvar vip-electric-mode t
596 "*If t, enable electric behavior.
597 Currently only enables auto-indentation `according to mode'.")
599 (defconst vip-shift-width 8
600 "*The shiftwidth variable.")
602 ;; Variables for repeating destructive commands
604 (defconst vip-keep-point-on-repeat t
605 "*If t, don't move point when repeating previous command.
606 This is useful for doing repeated changes with the '.' key.
607 The user can change this to nil, if she likes when the cursor moves
608 to a new place after repeating previous Vi command.")
610 ;; Remember insert point as a marker. This is a local marker that must be
611 ;; initialized to nil and moved with `vip-move-marker-locally'.
612 (vip-deflocalvar vip-insert-point nil)
613 (put 'vip-insert-point 'permanent-local t)
615 ;; This remembers the point before dabbrev-expand was called.
616 ;; If vip-insert-point turns out to be bigger than that, it is reset
617 ;; back to vip-pre-command-point.
618 ;; The reason this is needed is because dabbrev-expand (and possibly
619 ;; others) may jump to before the insertion point, delete something and
620 ;; then reinsert a bigger piece. For instance: bla^blo
621 ;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point,
622 ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
623 ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
624 ;; will insert the expansion, and we get: blablo^
625 ;; Whatever we insert next goes before the ^, i.e., before the
626 ;; vip-insert-point marker. So, Viper will think that nothing was
627 ;; inserted. Remembering the orig position of the marker circumvents the
628 ;; problem.
629 ;; We don't know of any command, except dabbrev-expand, that has the same
630 ;; problem. However, the same trick can be used if such a command is
631 ;; discovered later.
633 (vip-deflocalvar vip-pre-command-point nil)
634 (put 'vip-pre-command-point 'permanent-local t) ; this is probably an overkill
636 ;; This is used for saving inserted text.
637 (defvar vip-last-insertion nil)
639 ;; Remembers the last replaced region.
640 (defvar vip-last-replace-region "")
642 ;; Remember com point as a marker.
643 ;; This is a local marker. Should be moved with `vip-move-marker-locally'
644 (vip-deflocalvar vip-com-point nil)
646 ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
647 ;; It is used to re-execute last destructive command.
648 ;; M-COM is a Lisp symbol representing the function to be executed.
649 ;; VAL is the prefix argument that was used with that command.
650 ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
651 ;; additional information on how the function in M-COM is to be handled.
652 ;; REG is the register used by command
653 ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
654 ;; commands).
655 ;; COMMAND-KEYS are the keys that were typed to invoke the command.
656 (defvar vip-d-com nil)
658 ;; The character remembered by the Vi `r' command.
659 (defvar vip-d-char nil)
661 ;; Name of register to store deleted or yanked strings
662 (defvar vip-use-register nil)
666 ;; Variables for Moves and Searches
668 ;; For use by `;' command.
669 (defvar vip-f-char nil)
671 ;; For use by `.' command.
672 (defvar vip-F-char nil)
674 ;; For use by `;' command.
675 (defvar vip-f-forward nil)
677 ;; For use by `;' command.
678 (defvar vip-f-offset nil)
680 ;; Last search string
681 (defvar vip-s-string "")
683 (defvar vip-quote-string "> "
684 "String inserted at the beginning of quoted region.")
686 ;; If t, search is forward.
687 (defvar vip-s-forward nil)
689 (defconst vip-case-fold-search nil
690 "*If t, search ignores cases.")
692 (defconst vip-re-search t
693 "*If t, search is reg-exp search, otherwise vanilla search.")
695 (defconst vip-re-query-replace t
696 "*If t then do regexp replace, if nil then do string replace.")
698 (defconst vip-re-replace t
699 "*If t, do regexp replace. nil means do string replace.")
701 (vip-deflocalvar vip-ex-style-motion t
702 "*Ex-style: the commands l,h do not cross lines, etc.")
704 (vip-deflocalvar vip-ex-style-editing-in-insert t
705 "*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc.
706 Note: this doesn't preclude ^H and ^? from deleting characters by moving
707 past the insertion point. This is a feature, not a bug. ")
709 (vip-deflocalvar vip-delete-backwards-in-replace nil
710 "*If t, DEL key will delete characters while moving the cursor backwards.
711 If nil, the cursor will move backwards without deleting anything.")
713 (defconst vip-buffer-search-char nil
714 "*Key bound for buffer-searching.")
716 (defconst vip-search-wrap-around-t t
717 "*If t, search wraps around.")
719 (vip-deflocalvar vip-related-files-and-buffers-ring nil
720 "*Ring of file and buffer names that are considered to be related to the
721 current buffer.
722 These buffers can be cycled through via :R and :P commands.")
723 (put 'vip-related-files-and-buffers-ring 'permanent-local t)
725 ;; Used to find out if we are done with searching the current buffer.
726 (vip-deflocalvar vip-local-search-start-marker nil)
727 ;; As above, but global
728 (defvar vip-search-start-marker (make-marker))
730 ;; the search overlay
731 (vip-deflocalvar vip-search-overlay nil)
734 (defvar vip-heading-start
735 (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
736 "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
737 "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
738 "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
739 "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
740 "^.+:-") ; prolog
741 "*Regexps for Headings. Used by \[\[ and \]\].")
743 (defvar vip-heading-end
744 (concat "^}\\|" ; C/C++
745 "^\\\\end{\\|" ; latex
746 "^@end \\|" ; texinfo
747 ")\n\n[ \t\n]*\\|" ; lisp
748 "\\.\\s-*$") ; prolog
749 "*Regexps to end Headings/Sections. Used by \[\].")
752 ;; These two vars control the interaction of jumps performed by ' and `.
753 ;; In this new version, '' doesn't erase the marks set by ``, so one can
754 ;; use both kinds of jumps interchangeably and without loosing positions
755 ;; inside the lines.
757 ;; Remembers position of the last jump done using ``'.
758 (vip-deflocalvar vip-last-jump nil)
759 ;; Remembers position of the last jump done using `''.
760 (vip-deflocalvar vip-last-jump-ignore 0)
762 ;; Some common error messages
764 (defconst vip-SpuriousText "Spurious text after command" "")
765 (defconst vip-BadExCommand "Not an editor command" "")
766 (defconst vip-InvalidCommandArgument "Invalid command argument" "")
767 (defconst vip-NoPrevSearch "No previous search string" "")
768 (defconst vip-EmptyRegister "`%c': Nothing in this register" "")
769 (defconst vip-InvalidRegister "`%c': Invalid register" "")
770 (defconst vip-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
771 (defconst vip-InvalidTextmarker "`%c': Invalid text marker" "")
772 (defconst vip-InvalidViCommand "Invalid command" "")
773 (defconst vip-BadAddress "Ill-formed address" "")
774 (defconst vip-FirstAddrExceedsSecond "First address exceeds second" "")
775 (defconst vip-NoFileSpecified "No file specified" "")
778 ;; History variables
780 ;; History of search strings.
781 (defvar vip-search-history (list ""))
782 ;; History of query-replace strings used as a source.
783 (defvar vip-replace1-history nil)
784 ;; History of query-replace strings used as replacement.
785 (defvar vip-replace2-history nil)
786 ;; History of region quoting strings.
787 (defvar vip-quote-region-history (list vip-quote-string))
788 ;; History of Ex-style commands.
789 (defvar vip-ex-history nil)
790 ;; History of shell commands.
791 (defvar vip-shell-history nil)
794 ;; Last shell command. There are two of these, one for Ex (in viper-ex)
795 ;; and one for Vi.
797 ;; Last shell command executed with ! command.
798 (defvar vip-last-shell-com nil)
802 ;;; Miscellaneous
804 ;; don't bark when mark is inactive
805 (setq mark-even-if-inactive t)
807 (defvar vip-inhibit-startup-message nil
808 "Whether Viper startup message should be inhibited.")
810 (defvar vip-always t
811 "t means, arrange that vi-state will be a default.")
813 (defvar vip-custom-file-name (cond ((memq system-type '(vax-vms axp-vms))
814 "sys$login:.vip")
815 ((memq system-type '(emx ms-dos))
816 "/_vip")
817 ((memq system-type '(windows-nt windows-95))
818 "~/_vip")
819 (t ; Unix
820 "~/.vip"))
821 "Viper customisation file.
822 This variable must be set _before_ loading Viper.")
824 (defvar vip-spell-function 'ispell-region
825 "Spell function used by #s<move> command to spell.")
827 (defvar vip-tags-file-name "TAGS"
828 "The tags file used by Viper.")
830 ;; Minibuffer
832 (defvar vip-vi-style-in-minibuffer t
833 "If t, use vi-style editing in minibuffer.
834 Should be set in `~/.vip' file.")
836 ;; overlay used in the minibuffer to indicate which state it is in
837 (vip-deflocalvar vip-minibuffer-overlay nil)
839 ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
840 ;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
841 ;; *after* exiting the minibuffer
842 (defvar vip-minibuffer-exit-hook nil)
844 (vip-deflocalvar vip-vi-minibuffer-minor-mode nil
845 "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
846 (vip-deflocalvar vip-insert-minibuffer-minor-mode nil
847 "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
849 ;; setup emacs-supported vi-style feel
850 (setq next-line-add-newlines nil
851 require-final-newline t)
853 (make-variable-buffer-local 'require-final-newline)
856 ;; Mode line
857 (defconst vip-vi-state-id "<V> "
858 "Mode line tag identifying the Vi mode of Viper.")
859 (defconst vip-emacs-state-id "<E> "
860 "Mode line tag identifying the Emacs mode of Viper.")
861 (defconst vip-insert-state-id "<I> "
862 "Mode line tag identifying the Insert mode of Viper.")
863 (defconst vip-replace-state-id "<R> "
864 "Mode line tag identifying the Replace mode of Viper.")
866 ;; Viper changes the default mode-line-buffer-identification
867 (setq-default mode-line-buffer-identification '(" %b"))
869 ;; Variable displaying the current Viper state in the mode line.
870 (vip-deflocalvar vip-mode-string vip-emacs-state-id)
871 (or (memq 'vip-mode-string global-mode-string)
872 (setq global-mode-string
873 (append '("" vip-mode-string) (cdr global-mode-string))))
876 (defvar vip-vi-state-hook nil
877 "*Hooks run just before the switch to Vi mode is completed.")
878 (defvar vip-insert-state-hook nil
879 "*Hooks run just before the switch to Insert mode is completed.")
880 (defvar vip-replace-state-hook nil
881 "*Hooks run just before the switch to Replace mode is completed.")
882 (defvar vip-emacs-state-hook nil
883 "*Hooks run just before the switch to Emacs mode is completed.")
885 (defvar vip-load-hook nil
886 "Hooks run just after loading Viper.")
889 ;; Generic predicates
891 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
893 ;; generate test functions
894 ;; given symbol foo, foo-p is the test function, foos is the set of
895 ;; Viper command keys
896 ;; (macroexpand '(vip-test-com-defun foo))
897 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
899 (defmacro vip-test-com-defun (name)
900 (let* ((snm (symbol-name name))
901 (nm-p (intern (concat snm "-p")))
902 (nms (intern (concat snm "s"))))
903 (` (defun (, nm-p) (com)
904 (consp (memq (if (< com 0) (- com) com) (, nms)))))))
906 ;; Variables for defining VI commands
908 ;; Modifying commands that can be prefixes to movement commands
909 (defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
910 (vip-test-com-defun vip-prefix-command)
912 ;; Commands that are pairs eg. dd. r and R here are a hack
913 (defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
914 (vip-test-com-defun vip-charpair-command)
916 (defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
917 ?H ?M ?n ?t ?T ?w ?W ?$ ?%
918 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
919 ?; ?, ?0 ?? ?/
921 "Movement commands")
922 (vip-test-com-defun vip-movement-command)
924 ;; Commands that can be repeated by .(dotted)
925 (defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<))
926 (vip-test-com-defun vip-dotable-command)
928 ;; Commands that can follow a #
929 (defconst vip-hash-cmds '(?c ?C ?g ?q ?S))
930 (vip-test-com-defun vip-hash-cmd)
932 ;; Commands that may have registers as prefix
933 (defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
934 (vip-test-com-defun vip-regsuffix-command)
938 ;;; Arrange the keymaps
939 (require 'viper-keym)
942 ;;; CODE
944 ;; sentinels
946 ;; Runs vip-after-change-functions inside after-change-functions
947 (defun vip-after-change-sentinel (beg end len)
948 (let ((list vip-after-change-functions))
949 (while list
950 (funcall (car list) beg end len)
951 (setq list (cdr list)))))
953 ;; Runs vip-before-change-functions inside before-change-functions
954 (defun vip-before-change-sentinel (beg end)
955 (let ((list vip-before-change-functions))
956 (while list
957 (funcall (car list) beg end)
958 (setq list (cdr list)))))
960 (defsubst vip-post-command-sentinel ()
961 (run-hooks 'vip-post-command-hooks))
963 (defsubst vip-pre-command-sentinel ()
964 (run-hooks 'vip-pre-command-hooks))
966 ;; Needed so that Viper will be able to figure the last inserted
967 ;; chunk of text with reasonable accuracy.
968 (defsubst vip-insert-state-post-command-sentinel ()
969 (if (and (memq vip-current-state '(insert-state replace-state))
970 vip-insert-point
971 (>= (point) vip-insert-point))
972 (setq vip-last-posn-while-in-insert-state (point-marker)))
973 (if (and (eq this-command 'dabbrev-expand)
974 (integerp vip-pre-command-point)
975 (> vip-insert-point vip-pre-command-point))
976 (move-marker vip-insert-point vip-pre-command-point))
979 (defsubst vip-insert-state-pre-command-sentinel ()
980 (if (and (eq this-command 'dabbrev-expand)
981 (markerp vip-insert-point)
982 (marker-position vip-insert-point))
983 (setq vip-pre-command-point (marker-position vip-insert-point))))
985 (defsubst vip-R-state-post-command-sentinel ()
986 ;; Restoring cursor color is needed despite
987 ;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
988 ;; another frame, the pre-command hook won't change cursor color to default
989 ;; in that other frame. So, if the second frame cursor was red and we set
990 ;; the point outside the replacement region, then the cursor color will
991 ;; remain red. Restoring the default, below, prevents this.
992 (if (and (<= (vip-replace-start) (point))
993 (<= (point) (vip-replace-end)))
994 (vip-change-cursor-color vip-replace-overlay-cursor-color)
995 (vip-restore-cursor-color)
998 ;; to speed up, don't change cursor color before self-insert
999 ;; and common move commands
1000 (defsubst vip-replace-state-pre-command-sentinel ()
1001 (or (memq this-command '(self-insert-command))
1002 (memq (vip-event-key last-command-event)
1003 '(up down left right (meta f) (meta b)
1004 (control n) (control p) (control f) (control b)))
1005 (vip-restore-cursor-color)))
1007 (defun vip-replace-state-post-command-sentinel ()
1008 ;; Restoring cursor color is needed despite
1009 ;; vip-replace-state-pre-command-sentinel: When one jumps to another buffer
1010 ;; in another frame, the pre-command hook won't change cursor color to
1011 ;; default in that other frame. So, if the second frame cursor was red and
1012 ;; we set the point outside the replacement region, then the cursor color
1013 ;; will remain red. Restoring the default, below, fixes this problem.
1015 ;; We optimize for self-insert-command's here, since they either don't change
1016 ;; cursor color or, if they terminate replace mode, the color will be changed
1017 ;; in vip-finish-change
1018 (or (memq this-command '(self-insert-command))
1019 (vip-restore-cursor-color))
1020 (cond
1021 ((eq vip-current-state 'replace-state)
1022 ;; delete characters to compensate for inserted chars.
1023 (let ((replace-boundary (vip-replace-end)))
1024 (save-excursion
1025 (goto-char vip-last-posn-in-replace-region)
1026 (delete-char vip-replace-chars-to-delete)
1027 (setq vip-replace-chars-to-delete 0
1028 vip-replace-chars-deleted 0)
1029 ;; terminate replace mode if reached replace limit
1030 (if (= vip-last-posn-in-replace-region
1031 (vip-replace-end))
1032 (vip-finish-change vip-last-posn-in-replace-region)))
1034 (if (and (<= (vip-replace-start) (point))
1035 (<= (point) replace-boundary))
1036 (progn
1037 ;; the state may have changed in vip-finish-change above
1038 (if (eq vip-current-state 'replace-state)
1039 (vip-change-cursor-color vip-replace-overlay-cursor-color))
1040 (setq vip-last-posn-in-replace-region (point-marker))))
1043 (t ;; terminate replace mode if changed Viper states.
1044 (vip-finish-change vip-last-posn-in-replace-region))))
1047 ;; changing mode
1049 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
1050 (defun vip-change-state (new-state)
1051 ;; Keep vip-post/pre-command-hooks fresh.
1052 ;; We remove then add vip-post/pre-command-sentinel since it is very
1053 ;; desirable that vip-pre-command-sentinel is the last hook and
1054 ;; vip-post-command-sentinel is the first hook.
1055 (remove-hook 'post-command-hook 'vip-post-command-sentinel)
1056 (add-hook 'post-command-hook 'vip-post-command-sentinel)
1057 (remove-hook 'pre-command-hook 'vip-pre-command-sentinel)
1058 (add-hook 'pre-command-hook 'vip-pre-command-sentinel t)
1059 ;; These hooks will be added back if switching to insert/replace mode
1060 (vip-remove-hook 'vip-post-command-hooks
1061 'vip-insert-state-post-command-sentinel)
1062 (vip-remove-hook 'vip-pre-command-hooks
1063 'vip-insert-state-pre-command-sentinel)
1064 (cond ((eq new-state 'vi-state)
1065 (cond ((member vip-current-state '(insert-state replace-state))
1067 ;; move vip-last-posn-while-in-insert-state
1068 ;; This is a normal hook that is executed in insert/replace
1069 ;; states after each command. In Vi/Emacs state, it does
1070 ;; nothing. We need to execute it here to make sure that
1071 ;; the last posn was recorded when we hit ESC.
1072 ;; It may be left unrecorded if the last thing done in
1073 ;; insert/repl state was dabbrev-expansion or abbrev
1074 ;; expansion caused by hitting ESC
1075 (vip-insert-state-post-command-sentinel)
1077 (condition-case conds
1078 (progn
1079 (vip-save-last-insertion
1080 vip-insert-point
1081 vip-last-posn-while-in-insert-state)
1082 (if vip-began-as-replace
1083 (setq vip-began-as-replace nil)
1084 ;; repeat insert commands if numerical arg > 1
1085 (save-excursion
1086 (vip-repeat-insert-command))))
1087 (error
1088 (vip-message-conditions conds)))
1090 (if (> (length vip-last-insertion) 0)
1091 (vip-push-onto-ring vip-last-insertion
1092 'vip-insertion-ring))
1094 (if vip-ex-style-editing-in-insert
1095 (or (bolp) (backward-char 1))))
1098 ;; insert or replace
1099 ((memq new-state '(insert-state replace-state))
1100 (if (memq vip-current-state '(emacs-state vi-state))
1101 (vip-move-marker-locally 'vip-insert-point (point)))
1102 (vip-move-marker-locally 'vip-last-posn-while-in-insert-state (point))
1103 (vip-add-hook 'vip-post-command-hooks
1104 'vip-insert-state-post-command-sentinel t)
1105 (vip-add-hook 'vip-pre-command-hooks
1106 'vip-insert-state-pre-command-sentinel t))
1107 ) ; outermost cond
1109 ;; Nothing needs to be done to switch to emacs mode! Just set some
1110 ;; variables, which is already done in vip-change-state-to-emacs!
1112 (setq vip-current-state new-state)
1113 (vip-normalize-minor-mode-map-alist)
1114 (vip-adjust-keys-for new-state)
1115 (vip-set-mode-vars-for new-state)
1116 (vip-refresh-mode-line)
1121 (defun vip-adjust-keys-for (state)
1122 "Make necessary adjustments to keymaps before entering STATE."
1123 (cond ((memq state '(insert-state replace-state))
1124 (if vip-auto-indent
1125 (progn
1126 (define-key vip-insert-basic-map "\C-m" 'vip-autoindent)
1127 (if vip-want-emacs-keys-in-insert
1128 ;; expert
1129 (define-key vip-insert-basic-map "\C-j" nil)
1130 ;; novice
1131 (define-key vip-insert-basic-map "\C-j" 'vip-autoindent))))
1133 (setq vip-insert-diehard-minor-mode
1134 (not vip-want-emacs-keys-in-insert))
1136 (if vip-want-ctl-h-help
1137 (progn
1138 (define-key vip-insert-basic-map "\C-h" 'help-command)
1139 (define-key vip-replace-map "\C-h" 'help-command))
1140 (define-key vip-insert-basic-map
1141 "\C-h" 'vip-del-backward-char-in-insert)
1142 (define-key vip-replace-map
1143 "\C-h" 'vip-del-backward-char-in-replace)))
1146 (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi))
1147 (if vip-want-ctl-h-help
1148 (define-key vip-vi-basic-map "\C-h" 'help-command)
1149 (define-key vip-vi-basic-map "\C-h" 'vip-backward-char)))
1153 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
1154 ;; This ensures that Viper bindings are in effect, regardless of which minor
1155 ;; modes were turned on by the user or by other packages.
1156 (defun vip-normalize-minor-mode-map-alist ()
1157 (setq minor-mode-map-alist
1158 (vip-append-filter-alist
1159 (list
1160 (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map)
1161 (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map)
1162 (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map)
1163 (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map)
1164 (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map)
1165 (cons 'vip-vi-state-modifier-minor-mode
1166 (if (keymapp
1167 (cdr (assoc major-mode vip-vi-state-modifier-alist)))
1168 (cdr (assoc major-mode vip-vi-state-modifier-alist))
1169 vip-empty-keymap))
1170 (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map)
1171 (cons 'vip-vi-basic-minor-mode vip-vi-basic-map)
1172 (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map)
1173 (cons 'vip-replace-minor-mode vip-replace-map)
1174 ;; vip-insert-minibuffer-minor-mode must come after
1175 ;; vip-replace-minor-mode
1176 (cons 'vip-insert-minibuffer-minor-mode
1177 vip-minibuffer-map)
1178 (cons 'vip-insert-local-user-minor-mode
1179 vip-insert-local-user-map)
1180 (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map)
1181 (cons 'vip-insert-global-user-minor-mode
1182 vip-insert-global-user-map)
1183 (cons 'vip-insert-state-modifier-minor-mode
1184 (if (keymapp
1185 (cdr
1186 (assoc major-mode vip-insert-state-modifier-alist)))
1187 (cdr
1188 (assoc major-mode vip-insert-state-modifier-alist))
1189 vip-empty-keymap))
1190 (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map)
1191 (cons 'vip-insert-basic-minor-mode vip-insert-basic-map)
1192 (cons 'vip-emacs-intercept-minor-mode
1193 vip-emacs-intercept-map)
1194 (cons 'vip-emacs-local-user-minor-mode
1195 vip-emacs-local-user-map)
1196 (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map)
1197 (cons 'vip-emacs-global-user-minor-mode
1198 vip-emacs-global-user-map)
1199 (cons 'vip-emacs-state-modifier-minor-mode
1200 (if (keymapp
1201 (cdr
1202 (assoc major-mode vip-emacs-state-modifier-alist)))
1203 (cdr
1204 (assoc major-mode vip-emacs-state-modifier-alist))
1205 vip-empty-keymap))
1207 minor-mode-map-alist)))
1213 ;; Viper mode-changing commands and utilities
1215 ;; Modifies mode-line-buffer-identification.
1216 (defun vip-refresh-mode-line ()
1217 (setq vip-mode-string
1218 (cond ((eq vip-current-state 'emacs-state) vip-emacs-state-id)
1219 ((eq vip-current-state 'vi-state) vip-vi-state-id)
1220 ((eq vip-current-state 'replace-state) vip-replace-state-id)
1221 ((eq vip-current-state 'insert-state) vip-insert-state-id)))
1223 ;; Sets Viper mode string in global-mode-string
1224 (force-mode-line-update))
1226 ;;;###autoload
1227 (defun viper-mode ()
1228 "Turn on Viper emulation of Vi."
1229 (interactive)
1230 (if (not noninteractive)
1231 (progn
1232 (if vip-first-time ; This check is important. Without it, startup and
1233 (progn ; expert-level msgs mix up when viper-mode recurses
1234 (setq vip-first-time nil)
1235 (if (not vip-inhibit-startup-message)
1236 (save-window-excursion
1237 (setq vip-inhibit-startup-message t)
1238 (delete-other-windows)
1239 (switch-to-buffer "Viper Startup Message")
1240 (erase-buffer)
1241 (insert
1242 (substitute-command-keys
1243 "Viper Is a Package for Emacs Rebels.
1244 It is also a VI Plan for Emacs Rescue and a venomous VI PERil.
1246 Technically speaking, Viper is a Vi emulation package for GNU Emacs 19 and
1247 XEmacs 19. It supports virtually all of Vi and Ex functionality, extending
1248 and improving upon much of it.
1250 1. Viper supports Vi at several levels. Level 1 is the closest to
1251 Vi, level 5 provides the most flexibility to depart from many Vi
1252 conventions.
1254 You will be asked to specify your user level in a following screen.
1256 If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will
1257 behave as in VI, to smooth transition to Viper for the beginners.
1258 However, to use Emacs productively, you are advised to reach user
1259 level 3 or higher.
1261 If your user level is 2 or higher, ^X and ^C will invoke Emacs
1262 functions,as usual in Emacs; ^Z will toggle vi/emacs modes, and
1263 ^G will be the usual Emacs's keyboard-quit (something like ^C in VI).
1265 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
1266 do not cause Emacs to quit, except at user level 1 (a novice).
1267 3. ^X^C EXITS EMACS.
1268 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
1269 undo. Another `u' changes direction.
1271 6. Emacs Meta functions are invoked by typing `_' or `\\ ESC'.
1272 On a window system, the best way is to use the Meta-key.
1273 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,
1274 if something funny happens. This would abort the current editing
1275 command.
1277 You can get more information on Viper by:
1279 a. Typing `:help' in Vi state
1280 b. Printing Viper manual, found in ./etc/viper.dvi
1281 c. Printing ViperCard, the Quick Reference, found in ./etc/viperCard.dvi
1283 This startup message appears whenever you load Viper, unless you type `y' now."
1285 (goto-char (point-min))
1286 (if (y-or-n-p "Inhibit Viper startup message? ")
1287 (vip-save-setting
1288 'vip-inhibit-startup-message
1289 "Viper startup message inhibited"
1290 vip-custom-file-name t))
1291 (kill-buffer (current-buffer))))
1292 (message " ")
1293 (vip-set-expert-level 'dont-change-unless)))
1294 (vip-change-state-to-vi))))
1296 ;;;###autoload
1297 (defalias 'vip-mode 'viper-mode)
1300 ;; Switch from Insert state to Vi state.
1301 (defun vip-exit-insert-state ()
1302 (interactive)
1303 (vip-change-state-to-vi))
1305 (defun vip-set-mode-vars-for (state)
1306 "Sets Viper minor mode variables to put Viper's state STATE in effect."
1308 ;; Emacs state
1309 (setq vip-vi-minibuffer-minor-mode nil
1310 vip-insert-minibuffer-minor-mode nil
1311 vip-vi-intercept-minor-mode nil
1312 vip-insert-intercept-minor-mode nil
1314 vip-vi-local-user-minor-mode nil
1315 vip-vi-kbd-minor-mode nil
1316 vip-vi-global-user-minor-mode nil
1317 vip-vi-state-modifier-minor-mode nil
1318 vip-vi-diehard-minor-mode nil
1319 vip-vi-basic-minor-mode nil
1321 vip-replace-minor-mode nil
1323 vip-insert-local-user-minor-mode nil
1324 vip-insert-kbd-minor-mode nil
1325 vip-insert-global-user-minor-mode nil
1326 vip-insert-state-modifier-minor-mode nil
1327 vip-insert-diehard-minor-mode nil
1328 vip-insert-basic-minor-mode nil
1329 vip-emacs-intercept-minor-mode t
1330 vip-emacs-local-user-minor-mode t
1331 vip-emacs-kbd-minor-mode (not (vip-is-in-minibuffer))
1332 vip-emacs-global-user-minor-mode t
1333 vip-emacs-state-modifier-minor-mode t
1336 ;; Vi state
1337 (if (eq state 'vi-state) ; adjust for vi-state
1338 (setq
1339 vip-vi-intercept-minor-mode t
1340 vip-vi-minibuffer-minor-mode (vip-is-in-minibuffer)
1341 vip-vi-local-user-minor-mode t
1342 vip-vi-kbd-minor-mode (not (vip-is-in-minibuffer))
1343 vip-vi-global-user-minor-mode t
1344 vip-vi-state-modifier-minor-mode t
1345 ;; don't let the diehard keymap block command completion
1346 ;; and other things in the minibuffer
1347 vip-vi-diehard-minor-mode (not
1348 (or vip-want-emacs-keys-in-vi
1349 (vip-is-in-minibuffer)))
1350 vip-vi-basic-minor-mode t
1351 vip-emacs-intercept-minor-mode nil
1352 vip-emacs-local-user-minor-mode nil
1353 vip-emacs-kbd-minor-mode nil
1354 vip-emacs-global-user-minor-mode nil
1355 vip-emacs-state-modifier-minor-mode nil
1358 ;; Insert and Replace states
1359 (if (member state '(insert-state replace-state))
1360 (setq
1361 vip-insert-intercept-minor-mode t
1362 vip-replace-minor-mode (eq state 'replace-state)
1363 vip-insert-minibuffer-minor-mode (vip-is-in-minibuffer)
1364 vip-insert-local-user-minor-mode t
1365 vip-insert-kbd-minor-mode (not (vip-is-in-minibuffer))
1366 vip-insert-global-user-minor-mode t
1367 vip-insert-state-modifier-minor-mode t
1368 ;; don't let the diehard keymap block command completion
1369 ;; and other things in the minibuffer
1370 vip-insert-diehard-minor-mode (not
1371 (or vip-want-emacs-keys-in-insert
1372 (vip-is-in-minibuffer)))
1373 vip-insert-basic-minor-mode t
1374 vip-emacs-intercept-minor-mode nil
1375 vip-emacs-local-user-minor-mode nil
1376 vip-emacs-kbd-minor-mode nil
1377 vip-emacs-global-user-minor-mode nil
1378 vip-emacs-state-modifier-minor-mode nil
1381 ;; minibuffer faces
1382 (if (vip-has-face-support-p)
1383 (setq vip-minibuffer-current-face
1384 (cond ((eq state 'emacs-state) vip-minibuffer-emacs-face)
1385 ((eq state 'vi-state) vip-minibuffer-vi-face)
1386 ((memq state '(insert-state replace-state))
1387 vip-minibuffer-insert-face))))
1389 (if (vip-is-in-minibuffer)
1390 (vip-set-minibuffer-overlay))
1393 ;; This also takes care of the annoying incomplete lines in files.
1394 ;; Also, this fixes `undo' to work vi-style for complex commands.
1395 (defun vip-change-state-to-vi ()
1396 "Change Viper state to Vi."
1397 (interactive)
1398 (if (and vip-first-time (not (vip-is-in-minibuffer)))
1399 (viper-mode)
1400 (if overwrite-mode (overwrite-mode nil))
1401 (if abbrev-mode (expand-abbrev))
1402 (if (and auto-fill-function (> (current-column) fill-column))
1403 (funcall auto-fill-function))
1404 ;; don't leave whitespace lines around
1405 (if (and (memq last-command
1406 '(vip-autoindent
1407 vip-open-line vip-Open-line
1408 vip-replace-state-exit-cmd))
1409 (vip-over-whitespace-line))
1410 (indent-to-left-margin))
1411 (vip-add-newline-at-eob-if-necessary)
1412 (if vip-undo-needs-adjustment (vip-adjust-undo))
1413 (vip-change-state 'vi-state)
1415 ;; always turn off iso-accents-mode, or else we won't be able to use the
1416 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
1417 (if (and (boundp 'iso-accents-mode) iso-accents-mode)
1418 (iso-accents-mode -1))
1420 ;; Protection against user errors in hooks
1421 (condition-case conds
1422 (run-hooks 'vip-vi-state-hook)
1423 (error
1424 (vip-message-conditions conds)))))
1426 (defun vip-change-state-to-insert ()
1427 "Change Viper state to Insert."
1428 (interactive)
1429 (vip-change-state 'insert-state)
1430 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
1431 (iso-accents-mode 1)) ; turn iso accents on
1433 ;; Protection against user errors in hooks
1434 (condition-case conds
1435 (run-hooks 'vip-insert-state-hook)
1436 (error
1437 (vip-message-conditions conds))))
1439 (defsubst vip-downgrade-to-insert ()
1440 (setq vip-current-state 'insert-state
1441 vip-replace-minor-mode nil)
1446 ;; Change to replace state. When the end of replacement region is reached,
1447 ;; replace state changes to insert state.
1448 (defun vip-change-state-to-replace (&optional non-R-cmd)
1449 (vip-change-state 'replace-state)
1450 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
1451 (iso-accents-mode 1)) ; turn iso accents on
1452 ;; Run insert-state-hook
1453 (condition-case conds
1454 (run-hooks 'vip-insert-state-hook 'vip-replace-state-hook)
1455 (error
1456 (vip-message-conditions conds)))
1458 (if non-R-cmd
1459 (vip-start-replace)
1460 ;; 'R' is implemented using Emacs's overwrite-mode
1461 (vip-start-R-mode))
1465 (defun vip-change-state-to-emacs ()
1466 "Change Viper state to Emacs."
1467 (interactive)
1468 (vip-change-state 'emacs-state)
1469 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
1470 (iso-accents-mode 1)) ; turn iso accents on
1472 ;; Protection agains user errors in hooks
1473 (condition-case conds
1474 (run-hooks 'vip-emacs-state-hook)
1475 (error
1476 (vip-message-conditions conds))))
1478 ;; escape to emacs mode termporarily
1479 (defun vip-escape-to-emacs (arg &optional events)
1480 "Escape to Emacs state from Vi state for one Emacs command.
1481 ARG is used as the prefix value for the executed command. If
1482 EVENTS is a list of events, which become the beginning of the command."
1483 (interactive "P")
1484 (vip-escape-to-state arg events 'emacs-state))
1486 ;; escape to Vi mode termporarily
1487 (defun vip-escape-to-vi ()
1488 "Escape from Emacs state to Vi state for one Vi 1-character command.
1489 This doesn't work with prefix arguments or most complex commands like
1490 cw, dw, etc. But it does work with some 2-character commands,
1491 like dd or dr."
1492 (interactive)
1493 (vip-escape-to-state nil nil 'vi-state))
1495 ;; Escape to STATE mode for one Emacs command.
1496 (defun vip-escape-to-state (arg events state)
1497 (let (com key prefix-arg)
1498 ;; this temporarily turns off Viper's minor mode keymaps
1499 (vip-set-mode-vars-for state)
1500 (vip-normalize-minor-mode-map-alist)
1501 (if events (vip-set-unread-command-events events))
1503 ;; protect against keyboard quit and other errors
1504 (condition-case nil
1505 (progn
1506 (unwind-protect
1507 (progn
1508 (setq com (key-binding (setq key
1509 (if vip-xemacs-p
1510 (read-key-sequence nil)
1511 (read-key-sequence nil t)))))
1512 ;; In case of indirection--chase definitions.
1513 ;; Have to do it here because we execute this command under
1514 ;; different keymaps, so command-execute may not do the
1515 ;; right thing there
1516 (while (vectorp com) (setq com (key-binding com))))
1517 nil)
1518 ;; exec command in the right Viper state
1519 ;; otherwise, if we switch buffers in the escaped command,
1520 ;; Viper's mode vars will remain those of `state'. When we return
1521 ;; to the orig buffer, the bindings will be screwed up.
1522 (vip-set-mode-vars-for vip-current-state)
1524 ;; this-command, last-command-char, last-command-event
1525 (setq this-command com)
1526 (if vip-xemacs-p ; XEmacs represents key sequences as vectors
1527 (setq last-command-event (vip-seq-last-elt key)
1528 last-command-char (event-to-character last-command-event))
1529 ;; Emacs represents them as sequences (str or vec)
1530 (setq last-command-event (vip-seq-last-elt key)
1531 last-command-char last-command-event))
1533 (if (commandp com)
1534 (progn
1535 (setq prefix-arg arg)
1536 (command-execute com)))
1538 (quit (ding))
1539 (error (beep 1))))
1540 (vip-set-mode-vars-for vip-current-state)) ; set state in new buffer
1542 (defun vip-exec-form-in-emacs (form)
1543 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
1544 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
1545 (let ((buff (current-buffer))
1546 result)
1547 (vip-set-mode-vars-for 'emacs-state)
1548 (setq result (eval form))
1549 (if (not (equal buff (current-buffer))) ; cmd switched buffer
1550 (save-excursion
1551 (set-buffer buff)
1552 (vip-set-mode-vars-for vip-current-state)))
1553 (vip-set-mode-vars-for vip-current-state)
1554 result))
1557 ;; This is needed because minor modes sometimes override essential Viper
1558 ;; bindings. By letting Viper know which files these modes are in, it will
1559 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
1560 (defun vip-harness-minor-mode (load-file)
1561 "Familiarize Viper with a minor mode defined in LOAD_FILE.
1562 Minor modes that have their own keymaps may overshadow Viper keymaps.
1563 This function is designed to make Viper aware of the packages that define
1564 such minor modes.
1565 Usage:
1566 (vip-harness-minor-mode load-file)
1568 LOAD-FILE is a name of the file where the specific minor mode is defined.
1569 Suffixes such as .el or .elc should be stripped."
1571 (interactive "sEnter name of the load file: ")
1573 (vip-eval-after-load load-file '(vip-normalize-minor-mode-map-alist))
1575 ;; Change the default for minor-mode-map-alist each time a harnessed minor
1576 ;; mode adds its own keymap to the a-list.
1577 (vip-eval-after-load
1578 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist))
1582 (defun vip-ESC (arg)
1583 "Emulate ESC key in Emacs.
1584 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true. In that
1585 case \@ will be bound to ESC. If vip-no-multiple-ESC is 'twice double ESC
1586 would dings in vi-state. Other ESC sequences are emulated via the current
1587 Emacs's major mode keymap. This is more convenient on dumb terminals and in
1588 Emacs -nw, since this won't block functional keys such as up,down,
1589 etc. Meta key also will work. When vip-no-multiple-ESC is nil, ESC key
1590 behaves as in Emacs, any number of multiple escapes is allowed."
1591 (interactive "P")
1592 (let (char)
1593 (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state))
1594 (setq char (vip-read-char-exclusive))
1595 (vip-escape-to-emacs arg (list ?\e char) ))
1596 ((and (eq vip-no-multiple-ESC 'twice)
1597 (eq vip-current-state 'vi-state))
1598 (setq char (vip-read-char-exclusive))
1599 (if (= char (string-to-char vip-ESC-key))
1600 (ding)
1601 (vip-escape-to-emacs arg (list ?\e char) )))
1602 (t (ding)))
1605 (defun vip-alternate-ESC (arg)
1606 "ESC key without checking for multiple keystrokes."
1607 (interactive "P")
1608 (vip-escape-to-emacs arg '(?\e)))
1611 ;; Intercept ESC sequences on dumb terminals.
1612 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
1614 ;; Check if last key was ESC and if so try to reread it as a function key.
1615 ;; But only if there are characters to read during a very short time.
1616 ;; Returns the last event, if any.
1617 (defun vip-envelop-ESC-key ()
1618 (let ((event last-input-event)
1619 (keyseq [nil])
1620 inhibit-quit)
1621 (if (vip-ESC-event-p event)
1622 (progn
1623 (if (vip-fast-keysequence-p)
1624 (progn
1625 (let (minor-mode-map-alist)
1626 (vip-set-unread-command-events event)
1627 (setq keyseq
1628 (funcall
1629 (ad-get-orig-definition 'read-key-sequence) nil))
1630 ) ; let
1631 ;; If keyseq translates into something that still has ESC
1632 ;; at the beginning, separate ESC from the rest of the seq.
1633 ;; In XEmacs we check for events that are keypress meta-key
1634 ;; and convert them into [escape key]
1636 ;; This is needed for the following reason:
1637 ;; If ESC is the first symbol, we interpret it as if the
1638 ;; user typed ESC and then quickly some other symbols.
1639 ;; If ESC is not the first one, then the key sequence
1640 ;; entered was apparently translated into a function key or
1641 ;; something (e.g., one may have
1642 ;; (define-key function-key-map "\e[192z" [f11])
1643 ;; which would translate the escape-sequence generated by
1644 ;; f11 in an xterm window into the symbolic key f11.
1646 ;; If `first-key' is not an ESC event, we make it into the
1647 ;; last-command-event in order to pretend that this key was
1648 ;; pressed. This is needed to allow arrow keys to be bound to
1649 ;; macros. Otherwise, vip-exec-mapped-kbd-macro will think that
1650 ;; the last event was ESC and so it'll execute whatever is
1651 ;; bound to ESC. (Viper macros can't be bound to
1652 ;; ESC-sequences).
1653 (let* ((first-key (elt keyseq 0))
1654 (key-mod (event-modifiers first-key)))
1655 (cond ((vip-ESC-event-p first-key)
1656 ;; put keys following ESC on the unread list
1657 ;; and return ESC as the key-sequence
1658 (vip-set-unread-command-events (subseq keyseq 1))
1659 (setq last-input-event event
1660 keyseq (if vip-emacs-p
1661 "\e"
1662 (vector (character-to-event ?\e)))))
1663 ((and vip-xemacs-p
1664 (key-press-event-p first-key)
1665 (equal '(meta) key-mod))
1666 (vip-set-unread-command-events
1667 (vconcat (vector
1668 (character-to-event (event-key first-key)))
1669 (subseq keyseq 1)))
1670 (setq last-input-event event
1671 keyseq (vector (character-to-event ?\e))))
1672 ((eventp first-key)
1673 (setq last-command-event first-key))
1675 ) ; end progn
1677 ;; this is escape event with nothing after it
1678 ;; put in unread-command-event and then re-read
1679 (vip-set-unread-command-events event)
1680 (setq keyseq
1681 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
1683 ;; not an escape event
1684 (setq keyseq (vector event)))
1685 keyseq))
1689 (defadvice read-key-sequence (around vip-read-keyseq-ad activate)
1690 "Harness to work for Viper. This advice is harmless---don't worry!"
1691 (let (inhibit-quit event keyseq)
1692 (setq keyseq ad-do-it)
1693 (setq event (if vip-xemacs-p
1694 (elt keyseq 0) ; XEmacs returns vector of events
1695 (elt (listify-key-sequence keyseq) 0)))
1696 (if (vip-ESC-event-p event)
1697 (let (unread-command-events)
1698 (vip-set-unread-command-events keyseq)
1699 (if (vip-fast-keysequence-p)
1700 (let ((vip-vi-global-user-minor-mode nil)
1701 (vip-vi-local-user-minor-mode nil)
1702 (vip-replace-minor-mode nil) ; actually unnecessary
1703 (vip-insert-global-user-minor-mode nil)
1704 (vip-insert-local-user-minor-mode nil))
1705 (setq keyseq ad-do-it))
1706 (setq keyseq ad-do-it))))
1707 keyseq))
1709 (defadvice describe-key (before vip-read-keyseq-ad protect activate)
1710 "Force to read key via `read-key-sequence'."
1711 (interactive (list (vip-events-to-keys
1712 (read-key-sequence "Describe key: ")))))
1714 (defadvice describe-key-briefly (before vip-read-keyseq-ad protect activate)
1715 "Force to read key via `read-key-sequence'."
1716 (interactive (list (vip-events-to-keys
1717 (read-key-sequence "Describe key briefly: ")))))
1719 ;; Listen to ESC key.
1720 ;; If a sequence of keys starting with ESC is issued with very short delays,
1721 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
1722 (defun vip-intercept-ESC-key ()
1723 "Function that implements ESC key in Viper emulation of Vi."
1724 (interactive)
1725 (let ((cmd (or (key-binding (vip-envelop-ESC-key))
1726 '(lambda () (interactive) (error "")))))
1728 ;; call the actual function to execute ESC (if no other symbols followed)
1729 ;; or the key bound to the ESC sequence (if the sequence was issued
1730 ;; with very short delay between characters.
1731 (if (eq cmd 'vip-intercept-ESC-key)
1732 (setq cmd
1733 (cond ((eq vip-current-state 'vi-state)
1734 'vip-ESC)
1735 ((eq vip-current-state 'insert-state)
1736 'vip-exit-insert-state)
1737 ((eq vip-current-state 'replace-state)
1738 'vip-replace-state-exit-cmd)
1739 (t 'vip-change-state-to-vi)
1741 (call-interactively cmd)))
1745 ;; prefix argument for Vi mode
1747 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
1748 ;; represents the numeric value of the prefix argument and COM represents
1749 ;; command prefix such as "c", "d", "m" and "y".
1751 ;; Get value part of prefix-argument ARG.
1752 (defsubst vip-p-val (arg)
1753 (cond ((null arg) 1)
1754 ((consp arg)
1755 (if (or (null (car arg)) (equal (car arg) '(nil)))
1756 1 (car arg)))
1757 (t arg)))
1759 ;; Get raw value part of prefix-argument ARG.
1760 (defsubst vip-P-val (arg)
1761 (cond ((consp arg) (car arg))
1762 (t arg)))
1764 ;; Get com part of prefix-argument ARG.
1765 (defsubst vip-getcom (arg)
1766 (cond ((null arg) nil)
1767 ((consp arg) (cdr arg))
1768 (t nil)))
1770 ;; Get com part of prefix-argument ARG and modify it.
1771 (defun vip-getCom (arg)
1772 (let ((com (vip-getcom arg)))
1773 (cond ((equal com ?c) ?C)
1774 ((equal com ?d) ?D)
1775 ((equal com ?y) ?Y)
1776 (t com))))
1779 ;; Compute numeric prefix arg value.
1780 ;; Invoked by CHAR. COM is the command part obtained so far.
1781 (defun vip-prefix-arg-value (event com)
1782 (let (value)
1783 ;; read while number
1784 (while (and (numberp event) (>= event ?0) (<= event ?9))
1785 (setq value (+ (* (if (numberp value) value 0) 10) (- event ?0)))
1786 (setq event (vip-read-event-convert-to-char)))
1788 (setq prefix-arg value)
1789 (if com (setq prefix-arg (cons prefix-arg com)))
1790 (while (eq event ?U)
1791 (vip-describe-arg prefix-arg)
1792 (setq event (vip-read-event-convert-to-char)))
1793 (vip-set-unread-command-events event)))
1795 ;; Vi operator as prefix argument."
1796 (defun vip-prefix-arg-com (char value com)
1797 (let ((cont t))
1798 (while (and cont
1799 (memq char
1800 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1801 vip-buffer-search-char)))
1802 (if com
1803 ;; this means that we already have a command character, so we
1804 ;; construct a com list and exit while. however, if char is "
1805 ;; it is an error.
1806 (progn
1807 ;; new com is (CHAR . OLDCOM)
1808 (if (memq char '(?# ?\")) (error ""))
1809 (setq com (cons char com))
1810 (setq cont nil))
1811 ;; If com is nil we set com as char, and read more. Again, if char
1812 ;; is ", we read the name of register and store it in vip-use-register.
1813 ;; if char is !, =, or #, a complete com is formed so we exit the
1814 ;; while loop.
1815 (cond ((memq char '(?! ?=))
1816 (setq com char)
1817 (setq char (read-char))
1818 (setq cont nil))
1819 ((= char ?#)
1820 ;; read a char and encode it as com
1821 (setq com (+ 128 (read-char)))
1822 (setq char (read-char)))
1823 ((= char ?\")
1824 (let ((reg (read-char)))
1825 (if (vip-valid-register reg)
1826 (setq vip-use-register reg)
1827 (error ""))
1828 (setq char (read-char))))
1830 (setq com char)
1831 (setq char (vip-read-char-exclusive)))))))
1832 (if (atom com)
1833 ;; com is a single char, so we construct prefix-arg
1834 ;; and if char is ?, describe prefix arg, otherwise exit by
1835 ;; pushing the char back into vip-set-unread-command-events
1836 ;; Since char is a command, the command will execute with the prefix
1837 ;; argument that we just constructed.
1838 (progn
1839 (setq prefix-arg (cons value com))
1840 (while (= char ?U)
1841 (vip-describe-arg prefix-arg)
1842 (setq char (read-char)))
1843 (vip-set-unread-command-events char)
1845 ;; as com is non-nil, this means that we have a command to execute
1846 (if (memq (car com) '(?r ?R))
1847 ;; execute apropriate region command.
1848 (let ((char (car com)) (com (cdr com)))
1849 (setq prefix-arg (cons value com))
1850 (if (= char ?r) (vip-region prefix-arg)
1851 (vip-Region prefix-arg))
1852 ;; reset prefix-arg
1853 (setq prefix-arg nil))
1854 ;; otherwise, reset prefix arg and call appropriate command
1855 (setq value (if (null value) 1 value))
1856 (setq prefix-arg nil)
1857 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1858 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1859 ((equal com '(?d . ?y)) (vip-yank-defun))
1860 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1861 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1862 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1863 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1864 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1865 (t (error ""))))))
1867 (defun vip-describe-arg (arg)
1868 (let (val com)
1869 (setq val (vip-P-val arg)
1870 com (vip-getcom arg))
1871 (if (null val)
1872 (if (null com)
1873 (message "Value is nil, and command is nil")
1874 (message "Value is nil, and command is `%c'" com))
1875 (if (null com)
1876 (message "Value is `%d', and command is nil" val)
1877 (message "Value is `%d', and command is `%c'" val com)))))
1879 (defun vip-digit-argument (arg)
1880 "Begin numeric argument for the next command."
1881 (interactive "P")
1882 (vip-leave-region-active)
1883 (vip-prefix-arg-value last-command-char
1884 (if (consp arg) (cdr arg) nil)))
1886 (defun vip-command-argument (arg)
1887 "Accept a motion command as an argument."
1888 (interactive "P")
1889 (condition-case nil
1890 (vip-prefix-arg-com
1891 last-command-char
1892 (cond ((null arg) nil)
1893 ((consp arg) (car arg))
1894 ((numberp arg) arg)
1895 (t (error vip-InvalidCommandArgument)))
1896 (cond ((null arg) nil)
1897 ((consp arg) (cdr arg))
1898 ((numberp arg) nil)
1899 (t (error vip-InvalidCommandArgument))))
1900 (quit (setq vip-use-register nil)
1901 (signal 'quit nil)))
1902 (vip-deactivate-mark))
1905 ;; repeat last destructive command
1907 ;; Append region to text in register REG.
1908 ;; START and END are buffer positions indicating what to append.
1909 (defsubst vip-append-to-register (reg start end)
1910 (set-register reg (concat (if (stringp (get-register reg))
1911 (get-register reg) "")
1912 (buffer-substring start end))))
1914 ;; Saves last inserted text for possible use by vip-repeat command.
1915 (defun vip-save-last-insertion (beg end)
1916 (setq vip-last-insertion (buffer-substring beg end))
1917 (or (< (length vip-d-com) 5)
1918 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
1919 (or (null vip-command-ring)
1920 (ring-empty-p vip-command-ring)
1921 (progn
1922 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
1923 vip-last-insertion)
1924 ;; del most recent elt, if identical to the second most-recent
1925 (vip-cleanup-ring vip-command-ring)))
1928 (defsubst vip-yank-last-insertion ()
1929 "Inserts the text saved by the previous vip-save-last-insertion command."
1930 (condition-case nil
1931 (insert vip-last-insertion)
1932 (error nil)))
1935 ;; define functions to be executed
1937 ;; invoked by the `C' command
1938 (defun vip-exec-change (m-com com)
1939 ;; handle C cmd at the eol and at eob.
1940 (if (or (and (eolp) (= vip-com-point (point)))
1941 (= vip-com-point (point-max)))
1942 (progn
1943 (insert " ")(backward-char 1)))
1944 (if (= vip-com-point (point))
1945 (vip-forward-char-carefully))
1946 (if (= com ?c)
1947 (vip-change vip-com-point (point))
1948 (vip-change-subr vip-com-point (point))))
1950 ;; this is invoked by vip-substitute-line
1951 (defun vip-exec-Change (m-com com)
1952 (save-excursion
1953 (set-mark vip-com-point)
1954 (vip-enlarge-region (mark t) (point))
1955 (if vip-use-register
1956 (progn
1957 (cond ((vip-valid-register vip-use-register '(letter digit))
1958 ;;(vip-valid-register vip-use-register '(letter)
1959 (copy-to-register
1960 vip-use-register (mark t) (point) nil))
1961 ((vip-valid-register vip-use-register '(Letter))
1962 (vip-append-to-register
1963 (downcase vip-use-register) (mark t) (point)))
1964 (t (setq vip-use-register nil)
1965 (error vip-InvalidRegister vip-use-register)))
1966 (setq vip-use-register nil)))
1967 (delete-region (mark t) (point)))
1968 (open-line 1)
1969 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
1971 (defun vip-exec-delete (m-com com)
1972 (if vip-use-register
1973 (progn
1974 (cond ((vip-valid-register vip-use-register '(letter digit))
1975 ;;(vip-valid-register vip-use-register '(letter))
1976 (copy-to-register
1977 vip-use-register vip-com-point (point) nil))
1978 ((vip-valid-register vip-use-register '(Letter))
1979 (vip-append-to-register
1980 (downcase vip-use-register) vip-com-point (point)))
1981 (t (setq vip-use-register nil)
1982 (error vip-InvalidRegister vip-use-register)))
1983 (setq vip-use-register nil)))
1984 (setq last-command
1985 (if (eq last-command 'd-command) 'kill-region nil))
1986 (kill-region vip-com-point (point))
1987 (setq this-command 'd-command)
1988 (if vip-ex-style-motion
1989 (if (and (eolp) (not (bolp))) (backward-char 1))))
1991 (defun vip-exec-Delete (m-com com)
1992 (save-excursion
1993 (set-mark vip-com-point)
1994 (vip-enlarge-region (mark t) (point))
1995 (if vip-use-register
1996 (progn
1997 (cond ((vip-valid-register vip-use-register '(letter digit))
1998 ;;(vip-valid-register vip-use-register '(letter))
1999 (copy-to-register
2000 vip-use-register (mark t) (point) nil))
2001 ((vip-valid-register vip-use-register '(Letter))
2002 (vip-append-to-register
2003 (downcase vip-use-register) (mark t) (point)))
2004 (t (setq vip-use-register nil)
2005 (error vip-InvalidRegister vip-use-register)))
2006 (setq vip-use-register nil)))
2007 (setq last-command
2008 (if (eq last-command 'D-command) 'kill-region nil))
2009 (kill-region (mark t) (point))
2010 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
2011 (back-to-indentation))
2013 (defun vip-exec-yank (m-com com)
2014 (if vip-use-register
2015 (progn
2016 (cond ((vip-valid-register vip-use-register '(letter digit))
2017 ;; (vip-valid-register vip-use-register '(letter))
2018 (copy-to-register
2019 vip-use-register vip-com-point (point) nil))
2020 ((vip-valid-register vip-use-register '(Letter))
2021 (vip-append-to-register
2022 (downcase vip-use-register) vip-com-point (point)))
2023 (t (setq vip-use-register nil)
2024 (error vip-InvalidRegister vip-use-register)))
2025 (setq vip-use-register nil)))
2026 (setq last-command nil)
2027 (copy-region-as-kill vip-com-point (point))
2028 (goto-char vip-com-point))
2030 (defun vip-exec-Yank (m-com com)
2031 (save-excursion
2032 (set-mark vip-com-point)
2033 (vip-enlarge-region (mark t) (point))
2034 (if vip-use-register
2035 (progn
2036 (cond ((vip-valid-register vip-use-register '(letter digit))
2037 (copy-to-register
2038 vip-use-register (mark t) (point) nil))
2039 ((vip-valid-register vip-use-register '(Letter))
2040 (vip-append-to-register
2041 (downcase vip-use-register) (mark t) (point)))
2042 (t (setq vip-use-register nil)
2043 (error vip-InvalidRegister vip-use-register)))
2044 (setq vip-use-register nil)))
2045 (setq last-command nil)
2046 (copy-region-as-kill (mark t) (point)))
2047 (vip-deactivate-mark)
2048 (goto-char vip-com-point))
2050 (defun vip-exec-bang (m-com com)
2051 (save-excursion
2052 (set-mark vip-com-point)
2053 (vip-enlarge-region (mark t) (point))
2054 (shell-command-on-region
2055 (mark t) (point)
2056 (if (= com ?!)
2057 (setq vip-last-shell-com
2058 (vip-read-string-with-history
2061 'vip-shell-history
2062 (car vip-shell-history)
2064 vip-last-shell-com)
2065 t)))
2067 (defun vip-exec-equals (m-com com)
2068 (save-excursion
2069 (set-mark vip-com-point)
2070 (vip-enlarge-region (mark t) (point))
2071 (if (> (mark t) (point)) (exchange-point-and-mark))
2072 (indent-region (mark t) (point) nil)))
2074 (defun vip-exec-shift (m-com com)
2075 (save-excursion
2076 (set-mark vip-com-point)
2077 (vip-enlarge-region (mark t) (point))
2078 (if (> (mark t) (point)) (exchange-point-and-mark))
2079 (indent-rigidly (mark t) (point)
2080 (if (= com ?>)
2081 vip-shift-width
2082 (- vip-shift-width))))
2083 ;; return point to where it was before shift
2084 (goto-char vip-com-point))
2086 ;; this is needed because some commands fake com by setting it to ?r, which
2087 ;; denotes repeated insert command.
2088 (defsubst vip-exec-dummy (m-com com)
2089 nil)
2091 (defun vip-exec-buffer-search (m-com com)
2092 (setq vip-s-string (buffer-substring (point) vip-com-point))
2093 (setq vip-s-forward t)
2094 (setq vip-search-history (cons vip-s-string vip-search-history))
2095 (vip-search vip-s-string vip-s-forward 1))
2097 (defvar vip-exec-array (make-vector 128 nil))
2099 ;; Using a dispatch array allows adding functions like buffer search
2100 ;; without affecting other functions. Buffer search can now be bound
2101 ;; to any character.
2103 (aset vip-exec-array ?c 'vip-exec-change)
2104 (aset vip-exec-array ?C 'vip-exec-Change)
2105 (aset vip-exec-array ?d 'vip-exec-delete)
2106 (aset vip-exec-array ?D 'vip-exec-Delete)
2107 (aset vip-exec-array ?y 'vip-exec-yank)
2108 (aset vip-exec-array ?Y 'vip-exec-Yank)
2109 (aset vip-exec-array ?r 'vip-exec-dummy)
2110 (aset vip-exec-array ?! 'vip-exec-bang)
2111 (aset vip-exec-array ?< 'vip-exec-shift)
2112 (aset vip-exec-array ?> 'vip-exec-shift)
2113 (aset vip-exec-array ?= 'vip-exec-equals)
2117 ;; This function is called by various movement commands to execute a
2118 ;; destructive command on the region specified by the movement command. For
2119 ;; instance, if the user types cw, then the command vip-forward-word will
2120 ;; call vip-execute-com to execute vip-exec-change, which eventually will
2121 ;; call vip-change to invoke the replace mode on the region.
2123 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
2124 ;; vip-d-com for later use by vip-repeat.
2125 (defun vip-execute-com (m-com val com)
2126 (let ((reg vip-use-register))
2127 ;; this is the special command `#'
2128 (if (> com 128)
2129 (vip-special-prefix-com (- com 128))
2130 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
2131 (if (null fn)
2132 (error "%c: %s" com vip-InvalidViCommand)
2133 (funcall fn m-com com))))
2134 (if (vip-dotable-command-p com)
2135 (vip-set-destructive-command
2136 (list m-com val
2137 (if (memq com (list ?c ?C ?!)) (- com) com)
2138 reg nil nil)))
2142 (defun vip-repeat (arg)
2143 "Re-execute last destructive command.
2144 Use the info in vip-d-com, which has the form
2145 \(com val ch reg inserted-text command-keys\),
2146 where `com' is the command to be re-executed, `val' is the
2147 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
2148 if it exists, it is the name of the register for `com'.
2149 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
2150 (interactive "P")
2151 (let ((save-point (point)) ; save point before repeating prev cmd
2152 ;; Pass along that we are repeating a destructive command
2153 ;; This tells vip-set-destructive-command not to update
2154 ;; vip-command-ring
2155 (vip-intermediate-command 'vip-repeat))
2156 (if (eq last-command 'vip-undo)
2157 ;; if the last command was vip-undo, then undo-more
2158 (vip-undo-more)
2159 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
2160 ;; its prefix value is used as new prefix value for the command.
2161 (let ((m-com (car vip-d-com))
2162 (val (vip-P-val arg))
2163 (com (nth 2 vip-d-com))
2164 (reg (nth 3 vip-d-com)))
2165 (if (null val) (setq val (nth 1 vip-d-com)))
2166 (if (null m-com) (error "No previous command to repeat."))
2167 (setq vip-use-register reg)
2168 (if (nth 4 vip-d-com) ; text inserted by command
2169 (setq vip-last-insertion (nth 4 vip-d-com)
2170 vip-d-char (nth 4 vip-d-com)))
2171 (funcall m-com (cons val com))
2172 (if (and vip-keep-point-on-repeat (< save-point (point)))
2173 (goto-char save-point)) ; go back to before repeat.
2174 (if (and (eolp) (not (bolp)))
2175 (backward-char 1))
2177 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
2178 ;; If the prev cmd was rotating the command ring, this means that `.' has
2179 ;; just executed a command from that ring. So, push it on the ring again.
2180 ;; If we are just executing previous command , then don't push vip-d-com
2181 ;; because vip-d-com is not fully constructed in this case (its keys and
2182 ;; the inserted text may be nil). Besides, in this case, the command
2183 ;; executed by `.' is already on the ring.
2184 (if (eq last-command 'vip-display-current-destructive-command)
2185 (vip-push-onto-ring vip-d-com 'vip-command-ring))
2186 (vip-deactivate-mark)
2189 (defun vip-repeat-from-history ()
2190 "Repeat a destructive command from history.
2191 Doesn't change vip-command-ring in any way, so `.' will work as before
2192 executing this command.
2193 This command is supposed to be bound to a two-character Vi macro where
2194 the second character is a digit 0 to 9. The digit indicates which
2195 history command to execute. `<char>0' is equivalent to `.', `<char>1'
2196 invokes the command before that, etc."
2197 (interactive)
2198 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
2199 (idx (cond (vip-this-kbd-macro
2200 (string-to-number
2201 (symbol-name (elt vip-this-kbd-macro 1))))
2202 (t 0)))
2203 (num idx)
2204 (vip-d-com vip-d-com))
2206 (or (and (numberp num) (<= 0 num) (<= num 9))
2207 (progn
2208 (setq idx 0
2209 num 0)
2210 (message
2211 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
2212 (while (< 0 num)
2213 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
2214 (setq num (1- num)))
2215 (vip-repeat nil)
2216 (while (> idx num)
2217 (vip-special-ring-rotate1 vip-command-ring 1)
2218 (setq num (1+ num)))
2222 ;; This command is invoked interactively by the key sequence #<char>
2223 (defun vip-special-prefix-com (char)
2224 (cond ((= char ?c)
2225 (downcase-region (min vip-com-point (point))
2226 (max vip-com-point (point))))
2227 ((= char ?C)
2228 (upcase-region (min vip-com-point (point))
2229 (max vip-com-point (point))))
2230 ((= char ?g)
2231 (push-mark vip-com-point t)
2232 (vip-global-execute))
2233 ((= char ?q)
2234 (push-mark vip-com-point t)
2235 (vip-quote-region))
2236 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
2237 (t (error "#%c: %s" char vip-InvalidViCommand))))
2240 ;; undoing
2242 (defun vip-undo ()
2243 "Undo previous change."
2244 (interactive)
2245 (message "undo!")
2246 (let ((modified (buffer-modified-p))
2247 (before-undo-pt (point-marker))
2248 (after-change-functions after-change-functions)
2249 undo-beg-posn undo-end-posn)
2251 ;; no need to remove this hook, since this var has scope inside a let.
2252 (add-hook 'after-change-functions
2253 '(lambda (beg end len)
2254 (setq undo-beg-posn beg
2255 undo-end-posn (or end beg))))
2257 (undo-start)
2258 (undo-more 2)
2259 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
2260 undo-end-posn (or undo-end-posn undo-beg-posn))
2262 (goto-char undo-beg-posn)
2263 (sit-for 0)
2264 (if (and vip-keep-point-on-undo
2265 (pos-visible-in-window-p before-undo-pt))
2266 (progn
2267 (push-mark (point-marker) t)
2268 (vip-sit-for-short 300)
2269 (goto-char undo-end-posn)
2270 (vip-sit-for-short 300)
2271 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
2272 (> (abs (- undo-end-posn before-undo-pt)) 1))
2273 (goto-char before-undo-pt)
2274 (goto-char undo-beg-posn)))
2275 (push-mark before-undo-pt t))
2276 (if (and (eolp) (not (bolp))) (backward-char 1))
2277 (if (not modified) (set-buffer-modified-p t)))
2278 (setq this-command 'vip-undo))
2280 ;; Continue undoing previous changes.
2281 (defun vip-undo-more ()
2282 (message "undo more!")
2283 (condition-case nil
2284 (undo-more 1)
2285 (error (beep)
2286 (message "No further undo information in this buffer")))
2287 (if (and (eolp) (not (bolp))) (backward-char 1))
2288 (setq this-command 'vip-undo))
2290 ;; The following two functions are used to set up undo properly.
2291 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
2292 ;; they are undone all at once.
2293 (defun vip-adjust-undo ()
2294 (let ((inhibit-quit t)
2295 tmp tmp2)
2296 (setq vip-undo-needs-adjustment nil)
2297 (if (listp buffer-undo-list)
2298 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
2299 (progn
2300 (setq tmp2 (cdr tmp)) ; the part after mark
2302 ;; cut tail from buffer-undo-list temporarily by direct
2303 ;; manipulation with pointers in buffer-undo-list
2304 (setcdr tmp nil)
2306 (setq buffer-undo-list (delq nil buffer-undo-list))
2307 (setq buffer-undo-list
2308 (delq vip-buffer-undo-list-mark buffer-undo-list))
2309 ;; restore tail of buffer-undo-list
2310 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
2311 (setq buffer-undo-list (delq nil buffer-undo-list))))))
2314 (defun vip-set-complex-command-for-undo ()
2315 (if (listp buffer-undo-list)
2316 (if (not vip-undo-needs-adjustment)
2317 (let ((inhibit-quit t))
2318 (setq buffer-undo-list
2319 (cons vip-buffer-undo-list-mark buffer-undo-list))
2320 (setq vip-undo-needs-adjustment t)))))
2325 (defun vip-display-current-destructive-command ()
2326 (let ((text (nth 4 vip-d-com))
2327 (keys (nth 5 vip-d-com))
2328 (max-text-len 30))
2330 (setq this-command 'vip-display-current-destructive-command)
2332 (message " `.' runs %s%s"
2333 (concat "`" (vip-array-to-string keys) "'")
2334 (vip-abbreviate-string text max-text-len
2335 " inserting `" "'" " ......."))
2339 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
2340 ;; or in some other way (non-interactively).
2341 (defun vip-set-destructive-command (list)
2342 (or (eq vip-intermediate-command 'vip-repeat)
2343 (progn
2344 (setq vip-d-com list)
2345 (setcar (nthcdr 5 vip-d-com)
2346 (vip-array-to-string (this-command-keys)))
2347 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
2349 (defun vip-prev-destructive-command (next)
2350 "Find previous destructive command in the history of destructive commands.
2351 With prefix argument, find next destructive command."
2352 (interactive "P")
2353 (let (cmd vip-intermediate-command)
2354 (if (eq last-command 'vip-display-current-destructive-command)
2355 ;; repeated search through command history
2356 (setq vip-intermediate-command 'repeating-display-destructive-command)
2357 ;; first search through command history--set temp ring
2358 (setq vip-temp-command-ring (copy-list vip-command-ring)))
2359 (setq cmd (if next
2360 (vip-special-ring-rotate1 vip-temp-command-ring 1)
2361 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
2362 (if (null cmd)
2364 (setq vip-d-com cmd))
2365 (vip-display-current-destructive-command)))
2367 (defun vip-next-destructive-command ()
2368 "Find next destructive command in the history of destructive commands."
2369 (interactive)
2370 (vip-prev-destructive-command 'next))
2372 (defun vip-insert-prev-from-insertion-ring (arg)
2373 "Cycle through insertion ring in the direction of older insertions.
2374 Undoes previous insertion and inserts new.
2375 With prefix argument, cycles in the direction of newer elements.
2376 In minibuffer, this command executes whatever the invocation key is bound
2377 to in the global map, instead of cycling through the insertion ring."
2378 (interactive "P")
2379 (let (vip-intermediate-command)
2380 (if (eq last-command 'vip-insert-from-insertion-ring)
2381 (progn ; repeated search through insertion history
2382 (setq vip-intermediate-command 'repeating-insertion-from-ring)
2383 (if (eq vip-current-state 'replace-state)
2384 (undo 1)
2385 (if vip-last-inserted-string-from-insertion-ring
2386 (backward-delete-char
2387 (length vip-last-inserted-string-from-insertion-ring))))
2389 ;;first search through insertion history
2390 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
2391 (setq this-command 'vip-insert-from-insertion-ring)
2392 ;; so that things will be undone properly
2393 (setq buffer-undo-list (cons nil buffer-undo-list))
2394 (setq vip-last-inserted-string-from-insertion-ring
2395 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
2397 ;; this change of vip-intermediate-command must come after
2398 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
2399 ;; insertion.
2400 (setq vip-intermediate-command nil)
2401 (if vip-last-inserted-string-from-insertion-ring
2402 (insert vip-last-inserted-string-from-insertion-ring))
2405 (defun vip-insert-next-from-insertion-ring ()
2406 "Cycle through insertion ring in the direction of older insertions.
2407 Undo previous insertion and inserts new."
2408 (interactive)
2409 (vip-insert-prev-from-insertion-ring 'next))
2412 ;; some region utilities
2414 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
2415 (defun vip-add-newline-at-eob-if-necessary ()
2416 (save-excursion
2417 (end-of-line)
2418 ;; make sure all lines end with newline, unless in the minibuffer or
2419 ;; when requested otherwise (require-final-newline is nil)
2420 (if (and
2421 (eobp)
2422 (not (bolp))
2423 require-final-newline
2424 (not (vip-is-in-minibuffer)))
2425 (insert "\n"))))
2427 (defun vip-yank-defun ()
2428 (mark-defun)
2429 (copy-region-as-kill (point) (mark t)))
2431 ;; Enlarge region between BEG and END.
2432 (defun vip-enlarge-region (beg end)
2433 (or beg (setq beg end)) ; if beg is nil, set to end
2434 (or end (setq end beg)) ; if end is nil, set to beg
2436 (if (< beg end)
2437 (progn (goto-char beg) (set-mark end))
2438 (goto-char end)
2439 (set-mark beg))
2440 (beginning-of-line)
2441 (exchange-point-and-mark)
2442 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
2443 (if (not (eobp)) (beginning-of-line))
2444 (if (> beg end) (exchange-point-and-mark)))
2447 ;; Quote region by each line with a user supplied string.
2448 (defun vip-quote-region ()
2449 (setq vip-quote-string
2450 (vip-read-string-with-history
2451 "Quote string: "
2453 'vip-quote-region-history
2454 vip-quote-string))
2455 (vip-enlarge-region (point) (mark t))
2456 (if (> (point) (mark t)) (exchange-point-and-mark))
2457 (insert vip-quote-string)
2458 (beginning-of-line)
2459 (forward-line 1)
2460 (while (and (< (point) (mark t)) (bolp))
2461 (insert vip-quote-string)
2462 (beginning-of-line)
2463 (forward-line 1)))
2465 ;; Tells whether BEG is on the same line as END.
2466 ;; If one of the args is nil, it'll return nil.
2467 (defun vip-same-line (beg end)
2468 (let ((selective-display nil))
2469 (cond ((and beg end)
2470 ;; This 'if' is needed because Emacs treats the next empty line
2471 ;; as part of the previous line.
2472 (if (or (> beg (point-max)) (> end (point-max))) ; out of range
2474 (if (and (> end beg) (= (vip-line-pos 'start) end))
2475 (setq end (min (1+ end) (point-max))))
2476 (if (and (> beg end) (= (vip-line-pos 'start) beg))
2477 (setq beg (min (1+ beg) (point-max))))
2478 (<= (count-lines beg end) 1) ))
2480 (t nil))
2484 ;; Check if the string ends with a newline.
2485 (defun vip-end-with-a-newline-p (string)
2486 (or (string= string "")
2487 (= (vip-seq-last-elt string) ?\n)))
2489 (defun vip-tmp-insert-at-eob (msg)
2490 (let ((savemax (point-max)))
2491 (goto-char savemax)
2492 (insert msg)
2493 (sit-for 2)
2494 (goto-char savemax) (delete-region (point) (point-max))
2499 ;;; Minibuffer business
2501 (defsubst vip-set-minibuffer-style ()
2502 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
2505 (defun vip-minibuffer-setup-sentinel ()
2506 (let ((hook (if vip-vi-style-in-minibuffer
2507 'vip-change-state-to-insert
2508 'vip-change-state-to-emacs)))
2509 (funcall hook)
2512 ;; Interpret last event in the local map
2513 (defun vip-exit-minibuffer ()
2514 (interactive)
2515 (let (command)
2516 (setq command (local-key-binding (char-to-string last-command-char)))
2517 (if command
2518 (command-execute command)
2519 (exit-minibuffer))))
2522 (defun vip-set-search-face ()
2523 (if (vip-has-face-support-p)
2524 (defvar vip-search-face
2525 (progn
2526 (make-face 'vip-search-face)
2527 (vip-hide-face 'vip-search-face)
2528 (or (face-differs-from-default-p 'vip-search-face)
2529 ;; face wasn't set in .vip or .Xdefaults
2530 (if (vip-can-use-colors "Black" "khaki")
2531 (progn
2532 (set-face-background 'vip-search-face "khaki")
2533 (set-face-foreground 'vip-search-face "Black"))
2534 (copy-face 'italic 'vip-search-face)
2535 (set-face-underline-p 'vip-search-face t)))
2536 'vip-search-face)
2537 "*Face used to flash out the search pattern.")
2541 (defun vip-set-minibuffer-faces ()
2542 (if (not (vip-has-face-support-p))
2544 (defvar vip-minibuffer-emacs-face
2545 (progn
2546 (make-face 'vip-minibuffer-emacs-face)
2547 (vip-hide-face 'vip-minibuffer-emacs-face)
2548 (or (face-differs-from-default-p 'vip-minibuffer-emacs-face)
2549 ;; face wasn't set in .vip or .Xdefaults
2550 (if vip-vi-style-in-minibuffer
2551 ;; emacs state is an exception in the minibuffer
2552 (if (vip-can-use-colors "darkseagreen2" "Black")
2553 (progn
2554 (set-face-background
2555 'vip-minibuffer-emacs-face "darkseagreen2")
2556 (set-face-foreground
2557 'vip-minibuffer-emacs-face "Black"))
2558 (copy-face 'modeline 'vip-minibuffer-emacs-face))
2559 ;; emacs state is the main state in the minibuffer
2560 (if (vip-can-use-colors "Black" "pink")
2561 (progn
2562 (set-face-background 'vip-minibuffer-emacs-face "pink")
2563 (set-face-foreground
2564 'vip-minibuffer-emacs-face "Black"))
2565 (copy-face 'italic 'vip-minibuffer-emacs-face))
2567 'vip-minibuffer-emacs-face)
2568 "Face used in the Minibuffer when it is in Emacs state.")
2570 (defvar vip-minibuffer-insert-face
2571 (progn
2572 (make-face 'vip-minibuffer-insert-face)
2573 (vip-hide-face 'vip-minibuffer-insert-face)
2574 (or (face-differs-from-default-p 'vip-minibuffer-insert-face)
2575 (if vip-vi-style-in-minibuffer
2576 (if (vip-can-use-colors "Black" "pink")
2577 (progn
2578 (set-face-background 'vip-minibuffer-insert-face "pink")
2579 (set-face-foreground
2580 'vip-minibuffer-insert-face "Black"))
2581 (copy-face 'italic 'vip-minibuffer-insert-face))
2582 ;; If Insert state is an exception
2583 (if (vip-can-use-colors "darkseagreen2" "Black")
2584 (progn
2585 (set-face-background
2586 'vip-minibuffer-insert-face "darkseagreen2")
2587 (set-face-foreground
2588 'vip-minibuffer-insert-face "Black"))
2589 (copy-face 'modeline 'vip-minibuffer-insert-face))
2590 (vip-italicize-face 'vip-minibuffer-insert-face)))
2591 'vip-minibuffer-insert-face)
2592 "Face used in the Minibuffer when it is in Insert state.")
2594 (defvar vip-minibuffer-vi-face
2595 (progn
2596 (make-face 'vip-minibuffer-vi-face)
2597 (vip-hide-face 'vip-minibuffer-vi-face)
2598 (or (face-differs-from-default-p 'vip-minibuffer-vi-face)
2599 (if vip-vi-style-in-minibuffer
2600 (if (vip-can-use-colors "Black" "grey")
2601 (progn
2602 (set-face-background 'vip-minibuffer-vi-face "grey")
2603 (set-face-foreground 'vip-minibuffer-vi-face "Black"))
2604 (copy-face 'bold 'vip-minibuffer-vi-face))
2605 (copy-face 'bold 'vip-minibuffer-vi-face)
2606 (invert-face 'vip-minibuffer-vi-face)))
2607 'vip-minibuffer-vi-face)
2608 "Face used in the Minibuffer when it is in Vi state.")
2610 ;; the current face used in the minibuffer
2611 (vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "")
2616 ;;; Reading string with history
2618 (defun vip-read-string-with-history (prompt &optional initial
2619 history-var default keymap)
2620 ;; Read string, prompting with PROMPT and inserting the INITIAL
2621 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2622 ;; input is an empty string. Use KEYMAP, if given, or the
2623 ;; minibuffer-local-map.
2624 ;; Default value is displayed until the user types something in the
2625 ;; minibuffer.
2626 (let ((minibuffer-setup-hook
2627 '(lambda ()
2628 (if (stringp initial)
2629 (progn
2630 ;; don't wait if we have unread events or in kbd macro
2631 (or unread-command-events
2632 executing-kbd-macro
2633 (sit-for 840))
2634 (erase-buffer)
2635 (insert initial)))
2636 (vip-minibuffer-setup-sentinel)))
2637 (val "")
2638 (padding "")
2639 temp-msg)
2641 (setq keymap (or keymap minibuffer-local-map)
2642 initial (or initial "")
2643 temp-msg (if default
2644 (format "(default: %s) " default)
2645 ""))
2647 (setq vip-incomplete-ex-cmd nil)
2648 (setq val (read-from-minibuffer prompt
2649 (concat temp-msg initial val padding)
2650 keymap nil history-var))
2651 (setq minibuffer-setup-hook nil
2652 padding (vip-array-to-string (this-command-keys))
2653 temp-msg "")
2654 ;; the following tries to be smart about what to put in history
2655 (if (not (string= val (car (eval history-var))))
2656 (set history-var (cons val (eval history-var))))
2657 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2658 (string= (nth 0 (eval history-var)) ""))
2659 (set history-var (cdr (eval history-var))))
2660 ;; If the user enters nothing but the prev cmd wasn't vip-ex,
2661 ;; vip-command-argument, or `! shell-command', this probably means
2662 ;; that the user typed something then erased. Return "" in this case, not
2663 ;; the default---the default is too confusing in this case.
2664 (cond ((and (string= val "")
2665 (not (string= prompt "!")) ; was a `! shell-command'
2666 (not (memq last-command
2667 '(vip-ex
2668 vip-command-argument
2672 ((string= val "") (or default ""))
2673 (t val))
2678 ;; insertion commands
2680 ;; Called when state changes from Insert Vi command mode.
2681 ;; Repeats the insertion command if Insert state was entered with prefix
2682 ;; argument > 1.
2683 (defun vip-repeat-insert-command ()
2684 (let ((i-com (car vip-d-com))
2685 (val (nth 1 vip-d-com))
2686 (char (nth 2 vip-d-com)))
2687 (if (and val (> val 1)) ; first check that val is non-nil
2688 (progn
2689 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
2690 (vip-repeat nil)
2691 (setq vip-d-com (list i-com val char nil nil nil))
2692 ))))
2694 (defun vip-insert (arg)
2695 "Insert before point."
2696 (interactive "P")
2697 (vip-set-complex-command-for-undo)
2698 (let ((val (vip-p-val arg))
2699 (com (vip-getcom arg)))
2700 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
2701 (if com
2702 (vip-loop val (vip-yank-last-insertion))
2703 (vip-change-state-to-insert))))
2705 (defun vip-append (arg)
2706 "Append after point."
2707 (interactive "P")
2708 (vip-set-complex-command-for-undo)
2709 (let ((val (vip-p-val arg))
2710 (com (vip-getcom arg)))
2711 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
2712 (if (not (eolp)) (forward-char))
2713 (if (equal com ?r)
2714 (vip-loop val (vip-yank-last-insertion))
2715 (vip-change-state-to-insert))))
2717 (defun vip-Append (arg)
2718 "Append at end of line."
2719 (interactive "P")
2720 (vip-set-complex-command-for-undo)
2721 (let ((val (vip-p-val arg))
2722 (com (vip-getcom arg)))
2723 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
2724 (end-of-line)
2725 (if (equal com ?r)
2726 (vip-loop val (vip-yank-last-insertion))
2727 (vip-change-state-to-insert))))
2729 (defun vip-Insert (arg)
2730 "Insert before first non-white."
2731 (interactive "P")
2732 (vip-set-complex-command-for-undo)
2733 (let ((val (vip-p-val arg))
2734 (com (vip-getcom arg)))
2735 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
2736 (back-to-indentation)
2737 (if (equal com ?r)
2738 (vip-loop val (vip-yank-last-insertion))
2739 (vip-change-state-to-insert))))
2741 (defun vip-open-line (arg)
2742 "Open line below."
2743 (interactive "P")
2744 (vip-set-complex-command-for-undo)
2745 (let ((val (vip-p-val arg))
2746 (com (vip-getcom arg)))
2747 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
2748 (let ((col (current-indentation)))
2749 (if (equal com ?r)
2750 (vip-loop val
2751 (progn
2752 (end-of-line)
2753 (newline 1)
2754 (if vip-auto-indent
2755 (progn
2756 (setq vip-cted t)
2757 (if vip-electric-mode
2758 (indent-according-to-mode)
2759 (indent-to col))
2761 (vip-yank-last-insertion)))
2762 (end-of-line)
2763 (newline 1)
2764 (if vip-auto-indent
2765 (progn
2766 (setq vip-cted t)
2767 (if vip-electric-mode
2768 (indent-according-to-mode)
2769 (indent-to col))
2771 (vip-change-state-to-insert)
2772 ))))
2774 (defun vip-Open-line (arg)
2775 "Open line above."
2776 (interactive "P")
2777 (vip-set-complex-command-for-undo)
2778 (let ((val (vip-p-val arg))
2779 (com (vip-getcom arg)))
2780 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
2781 (let ((col (current-indentation)))
2782 (if (equal com ?r)
2783 (vip-loop val
2784 (progn
2785 (beginning-of-line)
2786 (open-line 1)
2787 (if vip-auto-indent
2788 (progn
2789 (setq vip-cted t)
2790 (if vip-electric-mode
2791 (indent-according-to-mode)
2792 (indent-to col))
2794 (vip-yank-last-insertion)))
2795 (beginning-of-line)
2796 (open-line 1)
2797 (if vip-auto-indent
2798 (progn
2799 (setq vip-cted t)
2800 (if vip-electric-mode
2801 (indent-according-to-mode)
2802 (indent-to col))
2804 (vip-change-state-to-insert)))))
2806 (defun vip-open-line-at-point (arg)
2807 "Open line at point."
2808 (interactive "P")
2809 (vip-set-complex-command-for-undo)
2810 (let ((val (vip-p-val arg))
2811 (com (vip-getcom arg)))
2812 (vip-set-destructive-command
2813 (list 'vip-open-line-at-point val ?r nil nil nil))
2814 (if (equal com ?r)
2815 (vip-loop val
2816 (progn
2817 (open-line 1)
2818 (vip-yank-last-insertion)))
2819 (open-line 1)
2820 (vip-change-state-to-insert))))
2822 (defun vip-substitute (arg)
2823 "Substitute characters."
2824 (interactive "P")
2825 (let ((val (vip-p-val arg))
2826 (com (vip-getcom arg)))
2827 (push-mark nil t)
2828 (forward-char val)
2829 (if (equal com ?r)
2830 (vip-change-subr (mark t) (point))
2831 (vip-change (mark t) (point)))
2832 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
2835 (defun vip-substitute-line (arg)
2836 "Substitute lines."
2837 (interactive "p")
2838 (vip-set-complex-command-for-undo)
2839 (vip-line (cons arg ?C)))
2841 ;; Prepare for replace
2842 (defun vip-start-replace ()
2843 (setq vip-began-as-replace t
2844 vip-sitting-in-replace t
2845 vip-replace-chars-to-delete 0
2846 vip-replace-chars-deleted 0)
2847 (vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
2848 (vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
2849 ;; this will get added repeatedly, but no harm
2850 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
2851 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
2852 (vip-move-marker-locally 'vip-last-posn-in-replace-region
2853 (vip-replace-start))
2854 (vip-add-hook
2855 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
2856 (vip-add-hook
2857 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2861 ;; checks how many chars were deleted by the last change
2862 (defun vip-replace-mode-spy-before (beg end)
2863 (setq vip-replace-chars-deleted
2864 (- end beg
2865 (max 0 (- end (vip-replace-end)))
2866 (max 0 (- (vip-replace-start) beg))
2869 ;; Invoked as an after-change-function to set up parameters of the last change
2870 (defun vip-replace-mode-spy-after (beg end length)
2871 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
2872 (progn
2873 (setq vip-replace-chars-to-delete 0)
2874 (vip-move-marker-locally
2875 'vip-last-posn-in-replace-region (point)))
2877 (let (beg-col end-col real-end chars-to-delete)
2878 (setq real-end (min end (vip-replace-end)))
2879 (save-excursion
2880 (goto-char beg)
2881 (setq beg-col (current-column))
2882 (goto-char real-end)
2883 (setq end-col (current-column)))
2885 ;; If beg of change is outside the replacement region, then don't
2886 ;; delete anything in the repl region (set chars-to-delete to 0).
2888 ;; This works fine except that we have to take special care of
2889 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
2890 ;; sometimes simply shifts the repl region rightwards, without
2891 ;; deleting an equal amount of characters.
2893 ;; The reason why new-dabbrev.el causes this are this:
2894 ;; if one dinamically completes a partial word that starts before the
2895 ;; replacement region (but ends inside) then new-dabbrev.el first
2896 ;; moves cursor backwards, to the beginning of the word to be
2897 ;; completed (say, pt A). Then it inserts the
2898 ;; completed word and then deletes the old, incomplete part.
2899 ;; Since the complete word is inserted at position before the repl
2900 ;; region, the next If-statement would have set chars-to-delete to 0
2901 ;; unless we check for the current command, which must be
2902 ;; dabbrev-expand.
2904 ;; In fact, it might be also useful to have overlays for insert
2905 ;; regions as well, since this will let us capture the situation when
2906 ;; dabbrev-expand goes back past the insertion point to find the
2907 ;; beginning of the word to be expanded.
2908 (if (or (and (<= (vip-replace-start) beg)
2909 (<= beg (vip-replace-end)))
2910 (and (= length 0) (eq this-command 'dabbrev-expand)))
2911 (setq chars-to-delete
2912 (max (- end-col beg-col) (- real-end beg) 0))
2913 (setq chars-to-delete 0))
2915 ;; if beg = last change position, it means that we are within the
2916 ;; same command that does multiple changes. Moreover, it means
2917 ;; that we have two subsequent changes (insert/delete) that
2918 ;; complement each other.
2919 (if (= beg (marker-position vip-last-posn-in-replace-region))
2920 (setq vip-replace-chars-to-delete
2921 (- (+ chars-to-delete vip-replace-chars-to-delete)
2922 vip-replace-chars-deleted))
2923 (setq vip-replace-chars-to-delete chars-to-delete))
2925 (vip-move-marker-locally
2926 'vip-last-posn-in-replace-region
2927 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
2928 (or (marker-position vip-last-posn-in-replace-region)
2929 (vip-replace-start))
2932 (setq vip-replace-chars-to-delete
2933 (max 0 (min vip-replace-chars-to-delete
2934 (- (vip-replace-end)
2935 vip-last-posn-in-replace-region))))
2939 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
2940 ;; posn is within the overlay.
2941 (defun vip-finish-change (posn)
2942 (vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
2943 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2944 (vip-remove-hook 'vip-post-command-hooks
2945 'vip-replace-state-post-command-sentinel)
2946 (vip-remove-hook
2947 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2948 (vip-restore-cursor-color)
2949 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2950 (save-excursion
2951 (if (and
2952 vip-replace-overlay
2953 (>= posn (vip-replace-start))
2954 (< posn (vip-replace-end)))
2955 (delete-region posn (vip-replace-end)))
2958 (if (eq vip-current-state 'replace-state)
2959 (vip-downgrade-to-insert))
2960 ;; replace mode ended => nullify vip-last-posn-in-replace-region
2961 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
2962 (vip-hide-replace-overlay)
2963 (vip-refresh-mode-line)
2964 (vip-put-string-on-kill-ring vip-last-replace-region)
2967 ;; Make STRING be the first element of the kill ring.
2968 (defun vip-put-string-on-kill-ring (string)
2969 (setq kill-ring (cons string kill-ring))
2970 (if (> (length kill-ring) kill-ring-max)
2971 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2972 (setq kill-ring-yank-pointer kill-ring))
2974 (defun vip-finish-R-mode ()
2975 (vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2976 (vip-remove-hook
2977 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2978 (vip-downgrade-to-insert))
2980 (defun vip-start-R-mode ()
2981 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2982 (overwrite-mode 1)
2983 (vip-add-hook
2984 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
2985 (vip-add-hook
2986 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2991 (defun vip-replace-state-exit-cmd ()
2992 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2993 These keys are ESC, RET, and LineFeed"
2994 (interactive)
2995 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2996 (vip-finish-R-mode)
2997 (vip-finish-change vip-last-posn-in-replace-region))
2998 (let (com)
2999 (if (eq this-command 'vip-intercept-ESC-key)
3000 (setq com 'vip-exit-insert-state)
3001 (vip-set-unread-command-events last-input-char)
3002 (setq com (key-binding (read-key-sequence nil))))
3004 (condition-case conds
3005 (command-execute com)
3006 (error
3007 (vip-message-conditions conds)))
3009 (vip-hide-replace-overlay))
3012 ;; This is the function bound to 'R'---unlimited replace.
3013 ;; Similar to Emacs's own overwrite-mode.
3014 (defun vip-overwrite (arg)
3015 "Begin overwrite mode."
3016 (interactive "P")
3017 (let ((val (vip-p-val arg))
3018 (com (vip-getcom arg)) (len))
3019 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
3020 (if com
3021 (progn
3022 ;; Viper saves inserted text in vip-last-insertion
3023 (setq len (length vip-last-insertion))
3024 (delete-char len)
3025 (vip-loop val (vip-yank-last-insertion)))
3026 (setq last-command 'vip-overwrite)
3027 (vip-set-complex-command-for-undo)
3028 (vip-set-replace-overlay (point) (vip-line-pos 'end))
3029 (vip-change-state-to-replace)
3033 ;; line commands
3035 (defun vip-line (arg)
3036 (let ((val (car arg))
3037 (com (cdr arg)))
3038 (vip-move-marker-locally 'vip-com-point (point))
3039 (if (not (eobp))
3040 (vip-next-line-carefully (1- val)))
3041 ;; this ensures that dd, cc, D, yy will do the right thing on the last
3042 ;; line of buffer when this line has no \n.
3043 (vip-add-newline-at-eob-if-necessary)
3044 (vip-execute-com 'vip-line val com))
3045 (if (and (eobp) (not (bobp))) (forward-line -1))
3048 (defun vip-yank-line (arg)
3049 "Yank ARG lines (in Vi's sense)."
3050 (interactive "P")
3051 (let ((val (vip-p-val arg)))
3052 (vip-line (cons val ?Y))))
3055 ;; region commands
3057 (defun vip-region (arg)
3058 "Execute command on a region."
3059 (interactive "P")
3060 (let ((val (vip-P-val arg))
3061 (com (vip-getcom arg)))
3062 (vip-move-marker-locally 'vip-com-point (point))
3063 (exchange-point-and-mark)
3064 (vip-execute-com 'vip-region val com)))
3066 (defun vip-Region (arg)
3067 "Execute command on a Region."
3068 (interactive "P")
3069 (let ((val (vip-P-val arg))
3070 (com (vip-getCom arg)))
3071 (vip-move-marker-locally 'vip-com-point (point))
3072 (exchange-point-and-mark)
3073 (vip-execute-com 'vip-Region val com)))
3075 (defun vip-replace-char (arg)
3076 "Replace the following ARG chars by the character read."
3077 (interactive "P")
3078 (if (and (eolp) (bolp)) (error "No character to replace here"))
3079 (let ((val (vip-p-val arg))
3080 (com (vip-getcom arg)))
3081 (vip-replace-char-subr com val)
3082 (if (and (eolp) (not (bolp))) (forward-char 1))
3083 (vip-set-destructive-command
3084 (list 'vip-replace-char val ?r nil vip-d-char nil))
3087 (defun vip-replace-char-subr (com arg)
3088 (let ((take-care-of-iso-accents
3089 (and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
3090 char)
3091 (setq char (if (equal com ?r)
3092 vip-d-char
3093 (read-char)))
3094 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
3095 ;; get European characters
3096 (progn
3097 (iso-accents-mode 1)
3098 (vip-set-unread-command-events char)
3099 (setq char (aref (read-key-sequence nil) 0))
3100 (iso-accents-mode -1)))
3101 (delete-char arg t)
3102 (setq vip-d-char char)
3103 (vip-loop (if (> arg 0) arg (- arg))
3104 (if (eq char ?\C-m) (insert "\n") (insert char)))
3105 (backward-char arg)))
3108 ;; basic cursor movement. j, k, l, h commands.
3110 (defun vip-forward-char (arg)
3111 "Move point right ARG characters (left if ARG negative).
3112 On reaching end of line, stop and signal error."
3113 (interactive "P")
3114 (vip-leave-region-active)
3115 (let ((val (vip-p-val arg))
3116 (com (vip-getcom arg)))
3117 (if com (vip-move-marker-locally 'vip-com-point (point)))
3118 (if vip-ex-style-motion
3119 (progn
3120 ;; the boundary condition check gets weird here because
3121 ;; forward-char may be the parameter of a delete, and 'dl' works
3122 ;; just like 'x' for the last char on a line, so we have to allow
3123 ;; the forward motion before the 'vip-execute-com', but, of
3124 ;; course, 'dl' doesn't work on an empty line, so we have to
3125 ;; catch that condition before 'vip-execute-com'
3126 (if (and (eolp) (bolp)) (error "") (forward-char val))
3127 (if com (vip-execute-com 'vip-forward-char val com))
3128 (if (eolp) (progn (backward-char 1) (error ""))))
3129 (forward-char val)
3130 (if com (vip-execute-com 'vip-forward-char val com)))))
3132 (defun vip-backward-char (arg)
3133 "Move point left ARG characters (right if ARG negative).
3134 On reaching beginning of line, stop and signal error."
3135 (interactive "P")
3136 (vip-leave-region-active)
3137 (let ((val (vip-p-val arg))
3138 (com (vip-getcom arg)))
3139 (if com (vip-move-marker-locally 'vip-com-point (point)))
3140 (if vip-ex-style-motion
3141 (progn
3142 (if (bolp) (error "") (backward-char val))
3143 (if com (vip-execute-com 'vip-backward-char val com)))
3144 (backward-char val)
3145 (if com (vip-execute-com 'vip-backward-char val com)))))
3147 ;; Like forward-char, but doesn't move at end of buffer.
3148 (defun vip-forward-char-carefully (&optional arg)
3149 (setq arg (or arg 1))
3150 (if (>= (point-max) (+ (point) arg))
3151 (forward-char arg)
3152 (goto-char (point-max))))
3154 ;; Like backward-char, but doesn't move at end of buffer.
3155 (defun vip-backward-char-carefully (&optional arg)
3156 (setq arg (or arg 1))
3157 (if (<= (point-min) (- (point) arg))
3158 (backward-char arg)
3159 (goto-char (point-min))))
3161 (defun vip-next-line-carefully (arg)
3162 (condition-case nil
3163 (next-line arg)
3164 (error nil)))
3168 ;;; Word command
3170 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
3171 ;; for word movement. When executed with a destructive command, \n is
3172 ;; usually left untouched for the last word.
3173 ;; Viper uses syntax table to determine what is a word and what is a
3174 ;; separator. However, \n is always a separator. Also, if vip-syntax-preference
3175 ;; is 'vi, then `_' is part of the word.
3177 ;; skip only one \n
3178 (defun vip-skip-separators (forward)
3179 (if forward
3180 (progn
3181 (vip-skip-all-separators-forward 'within-line)
3182 (if (looking-at "\n")
3183 (progn
3184 (forward-char)
3185 (vip-skip-all-separators-forward 'within-line))))
3186 (vip-skip-all-separators-backward 'within-line)
3187 (backward-char)
3188 (if (looking-at "\n")
3189 (vip-skip-all-separators-backward 'within-line)
3190 (forward-char))))
3192 (defun vip-forward-word-kernel (val)
3193 (while (> val 0)
3194 (cond ((vip-looking-at-alpha)
3195 (vip-skip-alpha-forward "_")
3196 (vip-skip-separators t))
3197 ((vip-looking-at-separator)
3198 (vip-skip-separators t))
3199 ((not (vip-looking-at-alphasep))
3200 (vip-skip-nonalphasep-forward)
3201 (vip-skip-separators t)))
3202 (setq val (1- val))))
3204 ;; first search backward for pat. Then skip chars backwards using aux-pat
3205 (defun vip-fwd-skip (pat aux-pat lim)
3206 (if (and (save-excursion
3207 (re-search-backward pat lim t))
3208 (= (point) (match-end 0)))
3209 (goto-char (match-beginning 0)))
3210 (skip-chars-backward aux-pat lim)
3211 (if (= (point) lim)
3212 (vip-forward-char-carefully))
3216 (defun vip-forward-word (arg)
3217 "Forward word."
3218 (interactive "P")
3219 (vip-leave-region-active)
3220 (let ((val (vip-p-val arg))
3221 (com (vip-getcom arg)))
3222 (if com (vip-move-marker-locally 'vip-com-point (point)))
3223 (vip-forward-word-kernel val)
3224 (if com (progn
3225 (cond ((memq com (list ?c (- ?c)))
3226 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3227 ;; Yank words including the whitespace, but not newline
3228 ((memq com (list ?y (- ?y)))
3229 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3230 ((vip-dotable-command-p com)
3231 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3232 (vip-execute-com 'vip-forward-word val com)))))
3235 (defun vip-forward-Word (arg)
3236 "Forward word delimited by white characters."
3237 (interactive "P")
3238 (vip-leave-region-active)
3239 (let ((val (vip-p-val arg))
3240 (com (vip-getcom arg)))
3241 (if com (vip-move-marker-locally 'vip-com-point (point)))
3242 (vip-loop val
3243 (progn
3244 (vip-skip-nonseparators 'forward)
3245 (vip-skip-separators t)))
3246 (if com (progn
3247 (cond ((memq com (list ?c (- ?c)))
3248 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3249 ;; Yank words including the whitespace, but not newline
3250 ((memq com (list ?y (- ?y)))
3251 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3252 ((vip-dotable-command-p com)
3253 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3254 (vip-execute-com 'vip-forward-Word val com)))))
3257 ;; this is a bit different from Vi, but Vi's end of word
3258 ;; makes no sense whatsoever
3259 (defun vip-end-of-word-kernel ()
3260 (if (vip-end-of-word-p) (forward-char))
3261 (if (vip-looking-at-separator)
3262 (vip-skip-all-separators-forward))
3264 (cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
3265 ((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
3266 (vip-backward-char-carefully))
3268 (defun vip-end-of-word-p ()
3269 (or (eobp)
3270 (save-excursion
3271 (cond ((vip-looking-at-alpha)
3272 (forward-char)
3273 (not (vip-looking-at-alpha)))
3274 ((not (vip-looking-at-alphasep))
3275 (forward-char)
3276 (vip-looking-at-alphasep))))))
3279 (defun vip-end-of-word (arg &optional careful)
3280 "Move point to end of current word."
3281 (interactive "P")
3282 (vip-leave-region-active)
3283 (let ((val (vip-p-val arg))
3284 (com (vip-getcom arg)))
3285 (if com (vip-move-marker-locally 'vip-com-point (point)))
3286 (vip-loop val (vip-end-of-word-kernel))
3287 (if com
3288 (progn
3289 (forward-char)
3290 (vip-execute-com 'vip-end-of-word val com)))))
3292 (defun vip-end-of-Word (arg)
3293 "Forward to end of word delimited by white character."
3294 (interactive "P")
3295 (vip-leave-region-active)
3296 (let ((val (vip-p-val arg))
3297 (com (vip-getcom arg)))
3298 (if com (vip-move-marker-locally 'vip-com-point (point)))
3299 (vip-loop val
3300 (progn
3301 (vip-end-of-word-kernel)
3302 (vip-skip-nonseparators 'forward)
3303 (backward-char)))
3304 (if com
3305 (progn
3306 (forward-char)
3307 (vip-execute-com 'vip-end-of-Word val com)))))
3309 (defun vip-backward-word-kernel (val)
3310 (while (> val 0)
3311 (backward-char)
3312 (cond ((vip-looking-at-alpha)
3313 (vip-skip-alpha-backward "_"))
3314 ((vip-looking-at-separator)
3315 (forward-char)
3316 (vip-skip-separators nil)
3317 (backward-char)
3318 (cond ((vip-looking-at-alpha)
3319 (vip-skip-alpha-backward "_"))
3320 ((not (vip-looking-at-alphasep))
3321 (vip-skip-nonalphasep-backward))
3322 (t (forward-char))))
3323 ((not (vip-looking-at-alphasep))
3324 (vip-skip-nonalphasep-backward)))
3325 (setq val (1- val))))
3327 (defun vip-backward-word (arg)
3328 "Backward word."
3329 (interactive "P")
3330 (vip-leave-region-active)
3331 (let ((val (vip-p-val arg))
3332 (com (vip-getcom arg)))
3333 (if com
3334 (let (i)
3335 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3336 (backward-char))
3337 (vip-move-marker-locally 'vip-com-point (point))
3338 (if i (forward-char))))
3339 (vip-backward-word-kernel val)
3340 (if com (vip-execute-com 'vip-backward-word val com))))
3342 (defun vip-backward-Word (arg)
3343 "Backward word delimited by white character."
3344 (interactive "P")
3345 (vip-leave-region-active)
3346 (let ((val (vip-p-val arg))
3347 (com (vip-getcom arg)))
3348 (if com
3349 (let (i)
3350 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3351 (backward-char))
3352 (vip-move-marker-locally 'vip-com-point (point))
3353 (if i (forward-char))))
3354 (vip-loop val
3355 (progn
3356 (vip-skip-separators nil)
3357 (vip-skip-nonseparators 'backward)))
3358 (if com (vip-execute-com 'vip-backward-Word val com))))
3362 ;; line commands
3364 (defun vip-beginning-of-line (arg)
3365 "Go to beginning of line."
3366 (interactive "P")
3367 (vip-leave-region-active)
3368 (let ((val (vip-p-val arg))
3369 (com (vip-getcom arg)))
3370 (if com (vip-move-marker-locally 'vip-com-point (point)))
3371 (beginning-of-line val)
3372 (if com (vip-execute-com 'vip-beginning-of-line val com))))
3374 (defun vip-bol-and-skip-white (arg)
3375 "Beginning of line at first non-white character."
3376 (interactive "P")
3377 (vip-leave-region-active)
3378 (let ((val (vip-p-val arg))
3379 (com (vip-getcom arg)))
3380 (if com (vip-move-marker-locally 'vip-com-point (point)))
3381 (forward-to-indentation (1- val))
3382 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
3384 (defun vip-goto-eol (arg)
3385 "Go to end of line."
3386 (interactive "P")
3387 (vip-leave-region-active)
3388 (let ((val (vip-p-val arg))
3389 (com (vip-getcom arg)))
3390 (if com (vip-move-marker-locally 'vip-com-point (point)))
3391 (end-of-line val)
3392 (if com (vip-execute-com 'vip-goto-eol val com))
3393 (if vip-ex-style-motion
3394 (if (and (eolp) (not (bolp))
3395 ;; a fix for vip-change-to-eol
3396 (not (equal vip-current-state 'insert-state)))
3397 (backward-char 1)
3398 ))))
3401 (defun vip-goto-col (arg)
3402 "Go to ARG's column."
3403 (interactive "P")
3404 (vip-leave-region-active)
3405 (let ((val (vip-p-val arg))
3406 (com (vip-getcom arg)))
3407 (save-excursion
3408 (end-of-line)
3409 (if (> val (1+ (current-column))) (error "")))
3410 (if com (vip-move-marker-locally 'vip-com-point (point)))
3411 (beginning-of-line)
3412 (forward-char (1- val))
3413 (if com (vip-execute-com 'vip-goto-col val com))))
3416 (defun vip-next-line (arg)
3417 "Go to next line."
3418 (interactive "P")
3419 (vip-leave-region-active)
3420 (let ((val (vip-p-val arg))
3421 (com (vip-getCom arg)))
3422 (if com (vip-move-marker-locally 'vip-com-point (point)))
3423 (next-line val)
3424 (if vip-ex-style-motion
3425 (if (and (eolp) (not (bolp))) (backward-char 1)))
3426 (setq this-command 'next-line)
3427 (if com (vip-execute-com 'vip-next-line val com))))
3429 (defun vip-next-line-at-bol (arg)
3430 "Next line at beginning of line."
3431 (interactive "P")
3432 (vip-leave-region-active)
3433 (save-excursion
3434 (end-of-line)
3435 (if (eobp) (error "Last line in buffer")))
3436 (let ((val (vip-p-val arg))
3437 (com (vip-getCom arg)))
3438 (if com (vip-move-marker-locally 'vip-com-point (point)))
3439 (forward-line val)
3440 (back-to-indentation)
3441 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
3443 (defun vip-previous-line (arg)
3444 "Go to previous line."
3445 (interactive "P")
3446 (vip-leave-region-active)
3447 (let ((val (vip-p-val arg))
3448 (com (vip-getCom arg)))
3449 (if com (vip-move-marker-locally 'vip-com-point (point)))
3450 (previous-line val)
3451 (if vip-ex-style-motion
3452 (if (and (eolp) (not (bolp))) (backward-char 1)))
3453 (setq this-command 'previous-line)
3454 (if com (vip-execute-com 'vip-previous-line val com))))
3457 (defun vip-previous-line-at-bol (arg)
3458 "Previous line at beginning of line."
3459 (interactive "P")
3460 (vip-leave-region-active)
3461 (save-excursion
3462 (beginning-of-line)
3463 (if (bobp) (error "First line in buffer")))
3464 (let ((val (vip-p-val arg))
3465 (com (vip-getCom arg)))
3466 (if com (vip-move-marker-locally 'vip-com-point (point)))
3467 (forward-line (- val))
3468 (back-to-indentation)
3469 (if com (vip-execute-com 'vip-previous-line val com))))
3471 (defun vip-change-to-eol (arg)
3472 "Change to end of line."
3473 (interactive "P")
3474 (vip-goto-eol (cons arg ?c)))
3476 (defun vip-kill-line (arg)
3477 "Delete line."
3478 (interactive "P")
3479 (vip-goto-eol (cons arg ?d)))
3481 (defun vip-erase-line (arg)
3482 "Erase line."
3483 (interactive "P")
3484 (vip-beginning-of-line (cons arg ?d)))
3487 ;;; Moving around
3489 (defun vip-goto-line (arg)
3490 "Go to ARG's line. Without ARG go to end of buffer."
3491 (interactive "P")
3492 (let ((val (vip-P-val arg))
3493 (com (vip-getCom arg)))
3494 (vip-move-marker-locally 'vip-com-point (point))
3495 (vip-deactivate-mark)
3496 (push-mark nil t)
3497 (if (null val)
3498 (goto-char (point-max))
3499 (goto-char (point-min))
3500 (forward-line (1- val)))
3502 ;; positioning is done twice: before and after command execution
3503 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3504 (back-to-indentation)
3506 (if com (vip-execute-com 'vip-goto-line val com))
3508 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3509 (back-to-indentation)
3512 ;; Find ARG's occurrence of CHAR on the current line.
3513 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
3514 ;; adjust point after search.
3515 (defun vip-find-char (arg char forward offset)
3516 (or (char-or-string-p char) (error ""))
3517 (let ((arg (if forward arg (- arg)))
3518 (cmd (if (eq vip-intermediate-command 'vip-repeat)
3519 (nth 5 vip-d-com)
3520 (vip-array-to-string (this-command-keys))))
3521 point)
3522 (save-excursion
3523 (save-restriction
3524 (if (> arg 0)
3525 (narrow-to-region
3526 ;; forward search begins here
3527 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
3528 ;; forward search ends here
3529 (progn (end-of-line) (point)))
3530 (narrow-to-region
3531 ;; backward search begins from here
3532 (if (bolp)
3533 (error "Command `%s': At beginning of line" cmd) (point))
3534 ;; backward search ends here
3535 (progn (beginning-of-line) (point))))
3536 ;; if arg > 0, point is forwarded before search.
3537 (if (> arg 0) (goto-char (1+ (point-min)))
3538 (goto-char (point-max)))
3539 (if (let ((case-fold-search nil))
3540 (search-forward (char-to-string char) nil 0 arg))
3541 (setq point (point))
3542 (error "Command `%s': `%c' not found" cmd char))))
3543 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
3545 (defun vip-find-char-forward (arg)
3546 "Find char on the line.
3547 If called interactively read the char to find from the terminal, and if
3548 called from vip-repeat, the char last used is used. This behaviour is
3549 controlled by the sign of prefix numeric value."
3550 (interactive "P")
3551 (let ((val (vip-p-val arg))
3552 (com (vip-getcom arg)))
3553 (if (> val 0)
3554 ;; this means that the function was called interactively
3555 (setq vip-f-char (read-char)
3556 vip-f-forward t
3557 vip-f-offset nil)
3558 ;; vip-repeat --- set vip-F-char from command-keys
3559 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3560 (vip-seq-last-elt (nth 5 vip-d-com))
3561 vip-F-char)
3562 vip-f-char vip-F-char)
3563 (setq val (- val)))
3564 (if com (vip-move-marker-locally 'vip-com-point (point)))
3565 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
3566 (setq val (- val))
3567 (if com
3568 (progn
3569 (setq vip-F-char vip-f-char) ; set new vip-F-char
3570 (forward-char)
3571 (vip-execute-com 'vip-find-char-forward val com)))))
3573 (defun vip-goto-char-forward (arg)
3574 "Go up to char ARG forward on line."
3575 (interactive "P")
3576 (let ((val (vip-p-val arg))
3577 (com (vip-getcom arg)))
3578 (if (> val 0)
3579 ;; this means that the function was called interactively
3580 (setq vip-f-char (read-char)
3581 vip-f-forward t
3582 vip-f-offset t)
3583 ;; vip-repeat --- set vip-F-char from command-keys
3584 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3585 (vip-seq-last-elt (nth 5 vip-d-com))
3586 vip-F-char)
3587 vip-f-char vip-F-char)
3588 (setq val (- val)))
3589 (if com (vip-move-marker-locally 'vip-com-point (point)))
3590 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
3591 (setq val (- val))
3592 (if com
3593 (progn
3594 (setq vip-F-char vip-f-char) ; set new vip-F-char
3595 (forward-char)
3596 (vip-execute-com 'vip-goto-char-forward val com)))))
3598 (defun vip-find-char-backward (arg)
3599 "Find char ARG on line backward."
3600 (interactive "P")
3601 (let ((val (vip-p-val arg))
3602 (com (vip-getcom arg)))
3603 (if (> val 0)
3604 ;; this means that the function was called interactively
3605 (setq vip-f-char (read-char)
3606 vip-f-forward nil
3607 vip-f-offset nil)
3608 ;; vip-repeat --- set vip-F-char from command-keys
3609 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3610 (vip-seq-last-elt (nth 5 vip-d-com))
3611 vip-F-char)
3612 vip-f-char vip-F-char)
3613 (setq val (- val)))
3614 (if com (vip-move-marker-locally 'vip-com-point (point)))
3615 (vip-find-char
3616 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
3617 (setq val (- val))
3618 (if com
3619 (progn
3620 (setq vip-F-char vip-f-char) ; set new vip-F-char
3621 (vip-execute-com 'vip-find-char-backward val com)))))
3623 (defun vip-goto-char-backward (arg)
3624 "Go up to char ARG backward on line."
3625 (interactive "P")
3626 (let ((val (vip-p-val arg))
3627 (com (vip-getcom arg)))
3628 (if (> val 0)
3629 ;; this means that the function was called interactively
3630 (setq vip-f-char (read-char)
3631 vip-f-forward nil
3632 vip-f-offset t)
3633 ;; vip-repeat --- set vip-F-char from command-keys
3634 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3635 (vip-seq-last-elt (nth 5 vip-d-com))
3636 vip-F-char)
3637 vip-f-char vip-F-char)
3638 (setq val (- val)))
3639 (if com (vip-move-marker-locally 'vip-com-point (point)))
3640 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
3641 (setq val (- val))
3642 (if com
3643 (progn
3644 (setq vip-F-char vip-f-char) ; set new vip-F-char
3645 (vip-execute-com 'vip-goto-char-backward val com)))))
3647 (defun vip-repeat-find (arg)
3648 "Repeat previous find command."
3649 (interactive "P")
3650 (let ((val (vip-p-val arg))
3651 (com (vip-getcom arg)))
3652 (vip-deactivate-mark)
3653 (if com (vip-move-marker-locally 'vip-com-point (point)))
3654 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
3655 (if com
3656 (progn
3657 (if vip-f-forward (forward-char))
3658 (vip-execute-com 'vip-repeat-find val com)))))
3660 (defun vip-repeat-find-opposite (arg)
3661 "Repeat previous find command in the opposite direction."
3662 (interactive "P")
3663 (let ((val (vip-p-val arg))
3664 (com (vip-getcom arg)))
3665 (vip-deactivate-mark)
3666 (if com (vip-move-marker-locally 'vip-com-point (point)))
3667 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
3668 (if com
3669 (progn
3670 (if vip-f-forward (forward-char))
3671 (vip-execute-com 'vip-repeat-find-opposite val com)))))
3674 ;; window scrolling etc.
3676 (defun vip-other-window (arg)
3677 "Switch to other window."
3678 (interactive "p")
3679 (other-window arg)
3680 (or (not (eq vip-current-state 'emacs-state))
3681 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
3682 (vip-change-state-to-vi)))
3684 (defun vip-window-top (arg)
3685 "Go to home window line."
3686 (interactive "P")
3687 (let ((val (vip-p-val arg))
3688 (com (vip-getCom arg)))
3689 (if com (vip-move-marker-locally 'vip-com-point (point)))
3690 (push-mark nil t)
3691 (move-to-window-line (1- val))
3693 ;; positioning is done twice: before and after command execution
3694 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3695 (back-to-indentation)
3697 (if com (vip-execute-com 'vip-window-top val com))
3699 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3700 (back-to-indentation)
3703 (defun vip-window-middle (arg)
3704 "Go to middle window line."
3705 (interactive "P")
3706 (let ((val (vip-p-val arg))
3707 (com (vip-getCom arg))
3708 lines)
3709 (if com (vip-move-marker-locally 'vip-com-point (point)))
3710 (push-mark nil t)
3711 (if (not (pos-visible-in-window-p (point-max)))
3712 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3713 (setq lines (count-lines (window-start) (point-max)))
3714 (move-to-window-line (+ (/ lines 2) (1- val))))
3716 ;; positioning is done twice: before and after command execution
3717 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3718 (back-to-indentation)
3720 (if com (vip-execute-com 'vip-window-middle val com))
3722 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3723 (back-to-indentation)
3726 (defun vip-window-bottom (arg)
3727 "Go to last window line."
3728 (interactive "P")
3729 (let ((val (vip-p-val arg))
3730 (com (vip-getCom arg)))
3731 (if com (vip-move-marker-locally 'vip-com-point (point)))
3732 (push-mark nil t)
3733 (move-to-window-line (- val))
3735 ;; positioning is done twice: before and after command execution
3736 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3737 (back-to-indentation)
3739 (if com (vip-execute-com 'vip-window-bottom val com))
3741 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3742 (back-to-indentation)
3745 (defun vip-line-to-top (arg)
3746 "Put current line on the home line."
3747 (interactive "p")
3748 (recenter (1- arg)))
3750 (defun vip-line-to-middle (arg)
3751 "Put current line on the middle line."
3752 (interactive "p")
3753 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3755 (defun vip-line-to-bottom (arg)
3756 "Put current line on the last line."
3757 (interactive "p")
3758 (recenter (- (window-height) (1+ arg))))
3761 ;; paren match
3762 ;; must correct this to only match ( to ) etc. On the other hand
3763 ;; it is good that paren match gets confused, because that way you
3764 ;; catch _all_ imbalances.
3766 (defun vip-paren-match (arg)
3767 "Go to the matching parenthesis."
3768 (interactive "P")
3769 (let ((com (vip-getcom arg)))
3770 (if (numberp arg)
3771 (if (or (> arg 99) (< arg 1))
3772 (error "Prefix must be between 1 and 99")
3773 (goto-char
3774 (if (> (point-max) 80000)
3775 (* (/ (point-max) 100) arg)
3776 (/ (* (point-max) arg) 100)))
3777 (back-to-indentation))
3778 (let (lim)
3779 (if (and (eolp) (not (bolp))) (forward-char -1))
3780 (save-excursion
3781 (end-of-line)
3782 (setq lim (point)))
3783 (if (re-search-forward "[][(){}]" lim t)
3784 (backward-char)
3785 (error "No matching character on line")))
3786 (cond ((looking-at "[\(\[{]")
3787 (if com (vip-move-marker-locally 'vip-com-point (point)))
3788 (forward-sexp 1)
3789 (if com
3790 (vip-execute-com 'vip-paren-match nil com)
3791 (backward-char)))
3792 ((looking-at "[])}]")
3793 (forward-char)
3794 (if com (vip-move-marker-locally 'vip-com-point (point)))
3795 (backward-sexp 1)
3796 (if com (vip-execute-com 'vip-paren-match nil com)))
3797 (t (error ""))))))
3800 ;; sentence ,paragraph and heading
3802 (defun vip-forward-sentence (arg)
3803 "Forward sentence."
3804 (interactive "P")
3805 (push-mark nil t)
3806 (let ((val (vip-p-val arg))
3807 (com (vip-getcom arg)))
3808 (if com (vip-move-marker-locally 'vip-com-point (point)))
3809 (forward-sentence val)
3810 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3812 (defun vip-backward-sentence (arg)
3813 "Backward sentence."
3814 (interactive "P")
3815 (push-mark nil t)
3816 (let ((val (vip-p-val arg))
3817 (com (vip-getcom arg)))
3818 (if com (vip-move-marker-locally 'vip-com-point (point)))
3819 (backward-sentence val)
3820 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3822 (defun vip-forward-paragraph (arg)
3823 "Forward paragraph."
3824 (interactive "P")
3825 (push-mark nil t)
3826 (let ((val (vip-p-val arg))
3827 (com (vip-getCom arg)))
3828 (if com (vip-move-marker-locally 'vip-com-point (point)))
3829 (forward-paragraph val)
3830 (if com
3831 (progn
3832 (backward-char 1)
3833 (vip-execute-com 'vip-forward-paragraph nil com)))))
3835 (defun vip-backward-paragraph (arg)
3836 "Backward paragraph."
3837 (interactive "P")
3838 (push-mark nil t)
3839 (let ((val (vip-p-val arg))
3840 (com (vip-getCom arg)))
3841 (if com (vip-move-marker-locally 'vip-com-point (point)))
3842 (backward-paragraph val)
3843 (if com
3844 (progn
3845 (forward-char 1)
3846 (vip-execute-com 'vip-backward-paragraph nil com)
3847 (backward-char 1)))))
3849 ;; should be mode-specific etc.
3851 (defun vip-prev-heading (arg)
3852 (interactive "P")
3853 (let ((val (vip-p-val arg))
3854 (com (vip-getCom arg)))
3855 (if com (vip-move-marker-locally 'vip-com-point (point)))
3856 (re-search-backward vip-heading-start nil t val)
3857 (goto-char (match-beginning 0))
3858 (if com (vip-execute-com 'vip-prev-heading nil com))))
3860 (defun vip-heading-end (arg)
3861 (interactive "P")
3862 (let ((val (vip-p-val arg))
3863 (com (vip-getCom arg)))
3864 (if com (vip-move-marker-locally 'vip-com-point (point)))
3865 (re-search-forward vip-heading-end nil t val)
3866 (goto-char (match-beginning 0))
3867 (if com (vip-execute-com 'vip-heading-end nil com))))
3869 (defun vip-next-heading (arg)
3870 (interactive "P")
3871 (let ((val (vip-p-val arg))
3872 (com (vip-getCom arg)))
3873 (if com (vip-move-marker-locally 'vip-com-point (point)))
3874 (end-of-line)
3875 (re-search-forward vip-heading-start nil t val)
3876 (goto-char (match-beginning 0))
3877 (if com (vip-execute-com 'vip-next-heading nil com))))
3880 ;; scrolling
3882 (setq scroll-step 1)
3884 (defun vip-scroll (arg)
3885 "Scroll to next screen."
3886 (interactive "p")
3887 (if (> arg 0)
3888 (while (> arg 0)
3889 (scroll-up)
3890 (setq arg (1- arg)))
3891 (while (> 0 arg)
3892 (scroll-down)
3893 (setq arg (1+ arg)))))
3895 (defun vip-scroll-back (arg)
3896 "Scroll to previous screen."
3897 (interactive "p")
3898 (vip-scroll (- arg)))
3900 (defun vip-scroll-down (arg)
3901 "Pull down half screen."
3902 (interactive "P")
3903 (condition-case nil
3904 (if (null arg)
3905 (scroll-down (/ (window-height) 2))
3906 (scroll-down arg))
3907 (error (beep 1)
3908 (message "Beginning of buffer")
3909 (goto-char (point-min)))))
3911 (defun vip-scroll-down-one (arg)
3912 "Scroll up one line."
3913 (interactive "p")
3914 (scroll-down arg))
3916 (defun vip-scroll-up (arg)
3917 "Pull up half screen."
3918 (interactive "P")
3919 (condition-case nil
3920 (if (null arg)
3921 (scroll-up (/ (window-height) 2))
3922 (scroll-up arg))
3923 (error (beep 1)
3924 (message "End of buffer")
3925 (goto-char (point-max)))))
3927 (defun vip-scroll-up-one (arg)
3928 "Scroll down one line."
3929 (interactive "p")
3930 (scroll-up arg))
3933 ;; searching
3935 (defun vip-if-string (prompt)
3936 (let ((s (vip-read-string-with-history
3937 prompt
3938 nil ; no initial
3939 'vip-search-history
3940 (car vip-search-history))))
3941 (if (not (string= s ""))
3942 (setq vip-s-string s))))
3945 (defun vip-toggle-search-style (arg)
3946 "Toggle the value of vip-case-fold-search/vip-re-search.
3947 Without prefix argument, will ask which search style to toggle. With prefix
3948 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
3950 Although this function is bound to \\[vip-toggle-search-style], the most
3951 convenient way to use it is to bind `//' to the macro
3952 `1 M-x vip-toggle-search-style' and `///' to
3953 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
3954 toggle case-fold-search and hitting `/' three times witth toggle regexp
3955 search. Macros are more convenient in this case because they don't affect
3956 the Emacs binding of `/'."
3957 (interactive "P")
3958 (let (msg)
3959 (cond ((or (eq arg 1)
3960 (and (null arg)
3961 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3962 (if vip-case-fold-search
3963 "case-insensitive" "case-sensitive")
3964 (if vip-case-fold-search
3965 "case-sensitive"
3966 "case-insensitive")))))
3967 (setq vip-case-fold-search (null vip-case-fold-search))
3968 (if vip-case-fold-search
3969 (setq msg "Search becomes case-insensitive")
3970 (setq msg "Search becomes case-sensitive")))
3971 ((or (eq arg 2)
3972 (and (null arg)
3973 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3974 (if vip-re-search
3975 "regexp-search" "vanilla-search")
3976 (if vip-re-search
3977 "vanilla-search"
3978 "regexp-search")))))
3979 (setq vip-re-search (null vip-re-search))
3980 (if vip-re-search
3981 (setq msg "Search becomes regexp-style")
3982 (setq msg "Search becomes vanilla-style")))
3984 (setq msg "Search style remains unchanged")))
3985 (prin1 msg t)))
3988 (defun vip-search-forward (arg)
3989 "Search a string forward.
3990 ARG is used to find the ARG's occurrence of the string.
3991 Null string will repeat previous search."
3992 (interactive "P")
3993 (let ((val (vip-P-val arg))
3994 (com (vip-getcom arg))
3995 (old-str vip-s-string))
3996 (setq vip-s-forward t)
3997 (vip-if-string "/")
3998 ;; this is not used at present, but may be used later
3999 (if (or (not (equal old-str vip-s-string))
4000 (not (markerp vip-local-search-start-marker))
4001 (not (marker-buffer vip-local-search-start-marker)))
4002 (setq vip-local-search-start-marker (point-marker)))
4003 (vip-search vip-s-string t val)
4004 (if com
4005 (progn
4006 (vip-move-marker-locally 'vip-com-point (mark t))
4007 (vip-execute-com 'vip-search-next val com)))))
4009 (defun vip-search-backward (arg)
4010 "Search a string backward.
4011 ARG is used to find the ARG's occurrence of the string.
4012 Null string will repeat previous search."
4013 (interactive "P")
4014 (let ((val (vip-P-val arg))
4015 (com (vip-getcom arg))
4016 (old-str vip-s-string))
4017 (setq vip-s-forward nil)
4018 (vip-if-string "?")
4019 ;; this is not used at present, but may be used later
4020 (if (or (not (equal old-str vip-s-string))
4021 (not (markerp vip-local-search-start-marker))
4022 (not (marker-buffer vip-local-search-start-marker)))
4023 (setq vip-local-search-start-marker (point-marker)))
4024 (vip-search vip-s-string nil val)
4025 (if com
4026 (progn
4027 (vip-move-marker-locally 'vip-com-point (mark t))
4028 (vip-execute-com 'vip-search-next val com)))))
4031 ;; Search for COUNT's occurrence of STRING.
4032 ;; Search is forward if FORWARD is non-nil, otherwise backward.
4033 ;; INIT-POINT is the position where search is to start.
4034 ;; Arguments:
4035 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
4036 (defun vip-search (string forward arg
4037 &optional no-offset init-point fail-if-not-found)
4038 (if (not (equal string ""))
4039 (let ((val (vip-p-val arg))
4040 (com (vip-getcom arg))
4041 (offset (not no-offset))
4042 (case-fold-search vip-case-fold-search)
4043 (start-point (or init-point (point))))
4044 (vip-deactivate-mark)
4045 (if forward
4046 (condition-case nil
4047 (progn
4048 (if offset (vip-forward-char-carefully))
4049 (if vip-re-search
4050 (progn
4051 (re-search-forward string nil nil val)
4052 (re-search-backward string))
4053 (search-forward string nil nil val)
4054 (search-backward string))
4055 ;; don't wait and don't flash in macros
4056 (or executing-kbd-macro
4057 (vip-flash-search-pattern))
4058 (if (not (equal start-point (point)))
4059 (push-mark start-point t)))
4060 (search-failed
4061 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4062 (progn
4063 (message "Search wrapped around end of buffer")
4064 (goto-char (point-min))
4065 (vip-search string forward (cons 1 com) t start-point 'fail)
4066 ;; don't wait in macros
4067 (or executing-kbd-macro (sit-for 2))
4068 ;; delete the wrap-around message
4069 (message "")
4071 (goto-char start-point)
4072 (error "`%s': %s not found"
4073 string
4074 (if vip-re-search "Pattern" "String"))
4076 ;; backward
4077 (condition-case nil
4078 (progn
4079 (if vip-re-search
4080 (re-search-backward string nil nil val)
4081 (search-backward string nil nil val))
4082 ;; don't wait and don't flash in macros
4083 (or executing-kbd-macro
4084 (vip-flash-search-pattern))
4085 (if (not (equal start-point (point)))
4086 (push-mark start-point t)))
4087 (search-failed
4088 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4089 (progn
4090 (message "Search wrapped around beginning of buffer")
4091 (goto-char (point-max))
4092 (vip-search string forward (cons 1 com) t start-point 'fail)
4093 ;; don't wait in macros
4094 (or executing-kbd-macro (sit-for 2))
4095 ;; delete the wrap-around message
4096 (message "")
4098 (goto-char start-point)
4099 (error "`%s': %s not found"
4100 string
4101 (if vip-re-search "Pattern" "String"))
4102 )))))))
4104 (defun vip-search-next (arg)
4105 "Repeat previous search."
4106 (interactive "P")
4107 (let ((val (vip-p-val arg))
4108 (com (vip-getcom arg)))
4109 (if (null vip-s-string) (error vip-NoPrevSearch))
4110 (vip-search vip-s-string vip-s-forward arg)
4111 (if com
4112 (progn
4113 (vip-move-marker-locally 'vip-com-point (mark t))
4114 (vip-execute-com 'vip-search-next val com)))))
4116 (defun vip-search-Next (arg)
4117 "Repeat previous search in the reverse direction."
4118 (interactive "P")
4119 (let ((val (vip-p-val arg))
4120 (com (vip-getcom arg)))
4121 (if (null vip-s-string) (error vip-NoPrevSearch))
4122 (vip-search vip-s-string (not vip-s-forward) arg)
4123 (if com
4124 (progn
4125 (vip-move-marker-locally 'vip-com-point (mark t))
4126 (vip-execute-com 'vip-search-Next val com)))))
4129 ;; Search contents of buffer defined by one of Viper's motion commands.
4130 ;; Repeatable via `n' and `N'.
4131 (defun vip-buffer-search-enable (&optional c)
4132 (cond (c (setq vip-buffer-search-char c))
4133 ((null vip-buffer-search-char)
4134 (setq vip-buffer-search-char ?g)))
4135 (define-key vip-vi-basic-map
4136 (char-to-string vip-buffer-search-char) 'vip-command-argument)
4137 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
4138 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
4140 ;; This is a Viper wraper for isearch-forward.
4141 (defun vip-isearch-forward (arg)
4142 "Do incremental search forward."
4143 (interactive "P")
4144 ;; emacs bug workaround
4145 (if (listp arg) (setq arg (car arg)))
4146 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
4148 ;; This is a Viper wraper for isearch-backward."
4149 (defun vip-isearch-backward (arg)
4150 "Do incremental search backward."
4151 (interactive "P")
4152 ;; emacs bug workaround
4153 (if (listp arg) (setq arg (car arg)))
4154 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
4157 ;; visiting and killing files, buffers
4159 (defun vip-switch-to-buffer ()
4160 "Switch to buffer in the current window."
4161 (interactive)
4162 (let (buffer)
4163 (setq buffer
4164 (read-buffer
4165 (format "Switch to buffer in this window \(%s\): "
4166 (buffer-name (other-buffer (current-buffer))))))
4167 (switch-to-buffer buffer)
4170 (defun vip-switch-to-buffer-other-window ()
4171 "Switch to buffer in another window."
4172 (interactive)
4173 (let (buffer)
4174 (setq buffer
4175 (read-buffer
4176 (format "Switch to buffer in another window \(%s\): "
4177 (buffer-name (other-buffer (current-buffer))))))
4178 (switch-to-buffer-other-window buffer)
4181 (defun vip-kill-buffer ()
4182 "Kill a buffer."
4183 (interactive)
4184 (let (buffer buffer-name)
4185 (setq buffer-name
4186 (read-buffer
4187 (format "Kill buffer \(%s\): "
4188 (buffer-name (current-buffer)))))
4189 (setq buffer
4190 (if (null buffer-name)
4191 (current-buffer)
4192 (get-buffer buffer-name)))
4193 (if (null buffer) (error "`%s': No such buffer" buffer-name))
4194 (if (or (not (buffer-modified-p buffer))
4195 (y-or-n-p
4196 (format
4197 "Buffer `%s' is modified, are you sure you want to kill it? "
4198 buffer-name)))
4199 (kill-buffer buffer)
4200 (error "Buffer not killed"))))
4203 (defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p")
4204 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
4205 This is useful when you the current directory contains files with the same
4206 prefix and many different suffixes. Usually, only one of the suffixes
4207 represents an editable file. However, file completion will stop at the `.'
4208 The smart suffix feature lets you hit RET in such a case, and Viper will
4209 select the appropriate suffix.
4211 Suffixes are tried in the order given and the first suffix for which a
4212 corresponding file exists is selected. If no file exists for any of the
4213 suffixes, the user is asked to confirm.
4215 To turn this feature off, set this variable to nil.")
4217 ;; Try to add suffix to files ending with a `.'
4218 ;; Useful when the user hits RET on a non-completed file name.
4219 (defun vip-file-add-suffix ()
4220 (let ((count 0)
4221 (len (length vip-smart-suffix-list))
4222 (file (buffer-string))
4223 found key cmd suff)
4224 (goto-char (point-max))
4225 (if (and vip-smart-suffix-list (string-match "\\.$" file))
4226 (progn
4227 (while (and (not found) (< count len))
4228 (setq suff (nth count vip-smart-suffix-list)
4229 count (1+ count))
4230 (if (file-exists-p (format "%s%s" file suff))
4231 (progn
4232 (setq found t)
4233 (insert suff))))
4235 (if found
4237 (vip-tmp-insert-at-eob " [Please complete file name]")
4238 (unwind-protect
4239 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
4240 (setq cmd
4241 (key-binding (setq key (read-key-sequence nil))))
4242 (cond ((eq cmd 'self-insert-command)
4243 (if vip-xemacs-p
4244 (insert (events-to-keys key))
4245 (insert key)))
4246 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
4247 nil)
4248 (t (command-execute cmd)))
4254 ;; Advice for use in find-file and read-file-name commands.
4255 (defadvice exit-minibuffer (before vip-exit-minibuffer-advice activate)
4256 "Run `vip-minibuffer-exit-hook' just before exiting the minibuffer."
4257 (run-hooks 'vip-minibuffer-exit-hook))
4259 (defadvice find-file (before vip-add-suffix-advice activate)
4260 "Use `read-file-name' for reading arguments."
4261 (interactive (list (read-file-name "Find file: "
4262 nil default-directory))))
4264 (defadvice find-file-other-window (before vip-add-suffix-advice activate)
4265 "Use `read-file-name' for reading arguments."
4266 (interactive (list (read-file-name "Find file in other window: "
4267 nil default-directory))))
4269 (defadvice find-file-other-frame (before vip-add-suffix-advice activate)
4270 "Use `read-file-name' for reading arguments."
4271 (interactive (list (read-file-name "Find file in other frame: "
4272 nil default-directory))))
4274 (defadvice read-file-name (around vip-suffix-advice activate)
4275 "Tell `exit-minibuffer' to run `vip-file-add-suffix' as a hook."
4276 (let ((vip-minibuffer-exit-hook 'vip-file-add-suffix))
4277 ad-do-it))
4281 ;; yank and pop
4283 (defsubst vip-yank (text)
4284 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
4285 (insert text)
4286 (setq this-command 'yank))
4288 (defun vip-put-back (arg)
4289 "Put back after point/below line."
4290 (interactive "P")
4291 (let ((val (vip-p-val arg))
4292 (text (if vip-use-register
4293 (cond ((vip-valid-register vip-use-register '(digit))
4294 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4295 ((vip-valid-register vip-use-register)
4296 (get-register (downcase vip-use-register)))
4297 (t (error vip-InvalidRegister vip-use-register)))
4298 (current-kill 0))))
4299 (if (null text)
4300 (if vip-use-register
4301 (let ((reg vip-use-register))
4302 (setq vip-use-register nil)
4303 (error vip-EmptyRegister reg))
4304 (error "")))
4305 (setq vip-use-register nil)
4306 (if (vip-end-with-a-newline-p text)
4307 (progn
4308 (if (eobp)
4309 (insert "\n")
4310 (forward-line 1))
4311 (beginning-of-line))
4312 (if (not (eolp)) (vip-forward-char-carefully)))
4313 (set-marker (vip-mark-marker) (point) (current-buffer))
4314 (vip-set-destructive-command
4315 (list 'vip-put-back val nil vip-use-register nil nil))
4316 (vip-loop val (vip-yank text)))
4317 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4318 ;; newline; it leaves the cursor at the beginning when the text contains
4319 ;; a newline
4320 (if (vip-same-line (point) (mark))
4321 (or (= (point) (mark)) (vip-backward-char-carefully))
4322 (exchange-point-and-mark)
4323 (if (bolp)
4324 (back-to-indentation)))
4325 (vip-deactivate-mark))
4327 (defun vip-Put-back (arg)
4328 "Put back at point/above line."
4329 (interactive "P")
4330 (let ((val (vip-p-val arg))
4331 (text (if vip-use-register
4332 (cond ((vip-valid-register vip-use-register '(digit))
4333 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4334 ((vip-valid-register vip-use-register)
4335 (get-register (downcase vip-use-register)))
4336 (t (error vip-InvalidRegister vip-use-register)))
4337 (current-kill 0))))
4338 (if (null text)
4339 (if vip-use-register
4340 (let ((reg vip-use-register))
4341 (setq vip-use-register nil)
4342 (error vip-EmptyRegister reg))
4343 (error "")))
4344 (setq vip-use-register nil)
4345 (if (vip-end-with-a-newline-p text) (beginning-of-line))
4346 (vip-set-destructive-command
4347 (list 'vip-Put-back val nil vip-use-register nil nil))
4348 (set-marker (vip-mark-marker) (point) (current-buffer))
4349 (vip-loop val (vip-yank text)))
4350 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4351 ;; newline; it leaves the cursor at the beginning when the text contains
4352 ;; a newline
4353 (if (vip-same-line (point) (mark))
4354 (or (= (point) (mark)) (vip-backward-char-carefully))
4355 (exchange-point-and-mark)
4356 (if (bolp)
4357 (back-to-indentation)))
4358 (vip-deactivate-mark))
4361 ;; Copy region to kill-ring.
4362 ;; If BEG and END do not belong to the same buffer, copy empty region.
4363 (defun vip-copy-region-as-kill (beg end)
4364 (condition-case nil
4365 (copy-region-as-kill beg end)
4366 (error (copy-region-as-kill beg beg))))
4369 (defun vip-delete-char (arg)
4370 "Delete character."
4371 (interactive "P")
4372 (let ((val (vip-p-val arg)))
4373 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
4374 (if (> val 1)
4375 (save-excursion
4376 (let ((here (point)))
4377 (end-of-line)
4378 (if (> val (- (point) here))
4379 (setq val (- (point) here))))))
4380 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
4381 (if (and vip-ex-style-motion (eolp))
4382 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
4383 (if vip-use-register
4384 (progn
4385 (cond ((vip-valid-register vip-use-register '((Letter)))
4386 (vip-append-to-register
4387 (downcase vip-use-register) (point) (- (point) val)))
4388 ((vip-valid-register vip-use-register)
4389 (copy-to-register
4390 vip-use-register (point) (- (point) val) nil))
4391 (t (error vip-InvalidRegister vip-use-register)))
4392 (setq vip-use-register nil)))
4393 (if vip-ex-style-motion
4394 (progn
4395 (delete-char val t)
4396 (if (and (eolp) (not (bolp))) (backward-char 1)))
4397 (if (eolp)
4398 (delete-backward-char val t)
4399 (delete-char val t)))))
4401 (defun vip-delete-backward-char (arg)
4402 "Delete previous character. On reaching beginning of line, stop and beep."
4403 (interactive "P")
4404 (let ((val (vip-p-val arg)))
4405 (vip-set-destructive-command
4406 (list 'vip-delete-backward-char val nil nil nil nil))
4407 (if (> val 1)
4408 (save-excursion
4409 (let ((here (point)))
4410 (beginning-of-line)
4411 (if (> val (- here (point)))
4412 (setq val (- here (point)))))))
4413 (if vip-use-register
4414 (progn
4415 (cond ((vip-valid-register vip-use-register '(Letter))
4416 (vip-append-to-register
4417 (downcase vip-use-register) (point) (+ (point) val)))
4418 ((vip-valid-register vip-use-register)
4419 (copy-to-register
4420 vip-use-register (point) (+ (point) val) nil))
4421 (t (error vip-InvalidRegister vip-use-register)))
4422 (setq vip-use-register nil)))
4423 (if (bolp) (ding)
4424 (delete-backward-char val t))))
4426 (defun vip-del-backward-char-in-insert ()
4427 "Delete 1 char backwards while in insert mode."
4428 (interactive)
4429 (if (and vip-ex-style-editing-in-insert (bolp))
4430 (beep 1)
4431 (delete-backward-char 1 t)))
4433 (defun vip-del-backward-char-in-replace ()
4434 "Delete one character in replace mode.
4435 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
4436 charecters. If it is nil, then the cursor just moves backwards, similarly
4437 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
4438 cursor move past the beginning of line."
4439 (interactive)
4440 (cond (vip-delete-backwards-in-replace
4441 (cond ((not (bolp))
4442 (delete-backward-char 1 t))
4443 (vip-ex-style-editing-in-insert
4444 (beep 1))
4445 ((bobp)
4446 (beep 1))
4448 (delete-backward-char 1 t))))
4449 (vip-ex-style-editing-in-insert
4450 (if (bolp)
4451 (beep 1)
4452 (backward-char 1)))
4454 (backward-char 1))))
4458 ;; join lines.
4460 (defun vip-join-lines (arg)
4461 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4462 (interactive "*P")
4463 (let ((val (vip-P-val arg)))
4464 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
4465 (vip-loop (if (null val) 1 (1- val))
4466 (progn
4467 (end-of-line)
4468 (if (not (eobp))
4469 (progn
4470 (forward-line 1)
4471 (delete-region (point) (1- (point)))
4472 (fixup-whitespace)))))))
4475 ;; Replace state
4477 (defun vip-change (beg end)
4478 (if (markerp beg) (setq beg (marker-position beg)))
4479 (if (markerp end) (setq end (marker-position end)))
4480 ;; beg is sometimes (mark t), which may be nil
4481 (or beg (setq beg end))
4483 (vip-set-complex-command-for-undo)
4484 (if vip-use-register
4485 (progn
4486 (copy-to-register vip-use-register beg end nil)
4487 (setq vip-use-register nil)))
4488 (vip-set-replace-overlay beg end)
4489 (setq last-command nil) ; separate repl text from prev kills
4491 (if (= (vip-replace-start) (point-max))
4492 (error "End of buffer"))
4494 (setq vip-last-replace-region
4495 (buffer-substring (vip-replace-start)
4496 (vip-replace-end)))
4498 ;; protect against error while inserting "@" and other disasters
4499 ;; (e.g., read-only buff)
4500 (condition-case conds
4501 (if (vip-same-line (vip-replace-start)
4502 (vip-replace-end))
4503 (progn
4504 ;; tabs cause problems in replace, so untabify
4505 (goto-char (vip-replace-end))
4506 (insert-before-markers "@") ; put placeholder after the TAB
4507 (untabify (vip-replace-start) (point))
4508 ;; del @, don't put on kill ring
4509 (delete-backward-char 1)
4511 (vip-set-replace-overlay-glyphs
4512 vip-replace-region-start-delimiter
4513 vip-replace-region-end-delimiter)
4514 ;; this move takes care of the last posn in the overlay, which
4515 ;; has to be shifted because of insert. We can't simply insert
4516 ;; "$" before-markers because then overlay-start will shift the
4517 ;; beginning of the overlay in case we are replacing a single
4518 ;; character. This fixes the bug with `s' and `cl' commands.
4519 (vip-move-replace-overlay (vip-replace-start) (point))
4520 (goto-char (vip-replace-start))
4521 (vip-change-state-to-replace t))
4522 (kill-region (vip-replace-start)
4523 (vip-replace-end))
4524 (vip-restore-cursor-color)
4525 (vip-change-state-to-insert))
4526 (error ;; make sure that the overlay doesn't stay.
4527 ;; go back to the original point
4528 (goto-char (vip-replace-start))
4529 (vip-hide-replace-overlay)
4530 (vip-message-conditions conds))))
4533 (defun vip-change-subr (beg end)
4534 ;; beg is sometimes (mark t), which may be nil
4535 (or beg (setq beg end))
4537 (if vip-use-register
4538 (progn
4539 (copy-to-register vip-use-register beg end nil)
4540 (setq vip-use-register nil)))
4541 (kill-region beg end)
4542 (setq this-command 'vip-change)
4543 (vip-yank-last-insertion))
4545 (defun vip-toggle-case (arg)
4546 "Toggle character case."
4547 (interactive "P")
4548 (let ((val (vip-p-val arg)) (c))
4549 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
4550 (while (> val 0)
4551 (setq c (following-char))
4552 (delete-char 1 nil)
4553 (if (eq c (upcase c))
4554 (insert-char (downcase c) 1)
4555 (insert-char (upcase c) 1))
4556 (if (eolp) (backward-char 1))
4557 (setq val (1- val)))))
4560 ;; query replace
4562 (defun vip-query-replace ()
4563 "Query replace.
4564 If a null string is suplied as the string to be replaced,
4565 the query replace mode will toggle between string replace
4566 and regexp replace."
4567 (interactive)
4568 (let (str)
4569 (setq str (vip-read-string-with-history
4570 (if vip-re-query-replace "Query replace regexp: "
4571 "Query replace: ")
4572 nil ; no initial
4573 'vip-replace1-history
4574 (car vip-replace1-history) ; default
4576 (if (string= str "")
4577 (progn
4578 (setq vip-re-query-replace (not vip-re-query-replace))
4579 (message "Query replace mode changed to %s"
4580 (if vip-re-query-replace "regexp replace"
4581 "string replace")))
4582 (if vip-re-query-replace
4583 (query-replace-regexp
4585 (vip-read-string-with-history
4586 (format "Query replace regexp `%s' with: " str)
4587 nil ; no initial
4588 'vip-replace1-history
4589 (car vip-replace1-history) ; default
4591 (query-replace
4593 (vip-read-string-with-history
4594 (format "Query replace `%s' with: " str)
4595 nil ; no initial
4596 'vip-replace1-history
4597 (car vip-replace1-history) ; default
4598 ))))))
4601 ;; marking
4603 (defun vip-mark-beginning-of-buffer ()
4604 "Mark beginning of buffer."
4605 (interactive)
4606 (push-mark (point))
4607 (goto-char (point-min))
4608 (exchange-point-and-mark)
4609 (message "Mark set at the beginning of buffer"))
4611 (defun vip-mark-end-of-buffer ()
4612 "Mark end of buffer."
4613 (interactive)
4614 (push-mark (point))
4615 (goto-char (point-max))
4616 (exchange-point-and-mark)
4617 (message "Mark set at the end of buffer"))
4619 (defun vip-mark-point ()
4620 "Set mark at point of buffer."
4621 (interactive)
4622 (let ((char (vip-read-char-exclusive)))
4623 (cond ((and (<= ?a char) (<= char ?z))
4624 (point-to-register (1+ (- char ?a))))
4625 ((= char ?<) (vip-mark-beginning-of-buffer))
4626 ((= char ?>) (vip-mark-end-of-buffer))
4627 ((= char ?.) (vip-set-mark-if-necessary))
4628 ((= char ?,) (vip-cycle-through-mark-ring))
4629 ((= char ?D) (mark-defun))
4630 (t (error ""))
4633 ;; Algorithm: If first invocation of this command save mark on ring, goto
4634 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4635 ;; making it into the new mark, M1.
4636 ;; Push this mark back and set mark to the original point position, p1.
4637 ;; So, if you hit '' or `` then you can return to p1.
4639 ;; If repeated command, pop top elt from the ring into mark and
4640 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4641 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4642 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4643 ;; etc.
4644 (defun vip-cycle-through-mark-ring ()
4645 "Visit previous locations on the mark ring.
4646 One can use `` and '' to temporarily jump 1 step back."
4647 (let* ((sv-pt (point)))
4648 ;; if repeated `m,' command, pop the previously saved mark.
4649 ;; Prev saved mark is actually prev saved point. It is used if the
4650 ;; user types `` or '' and is discarded
4651 ;; from the mark ring by the next `m,' command.
4652 ;; In any case, go to the previous or previously saved mark.
4653 ;; Then push the current mark (popped off the ring) and set current
4654 ;; point to be the mark. Current pt as mark is discarded by the next
4655 ;; m, command.
4656 (if (eq last-command 'vip-cycle-through-mark-ring)
4658 ;; save current mark if the first iteration
4659 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4660 (if (mark t)
4661 (push-mark (mark t) t)) )
4662 (pop-mark)
4663 (set-mark-command 1)
4664 ;; don't duplicate mark on the ring
4665 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4666 (push-mark sv-pt t)
4667 (vip-deactivate-mark)
4668 (setq this-command 'vip-cycle-through-mark-ring)
4672 (defun vip-goto-mark (arg)
4673 "Go to mark."
4674 (interactive "P")
4675 (let ((char (read-char))
4676 (com (vip-getcom arg)))
4677 (vip-goto-mark-subr char com nil)))
4679 (defun vip-goto-mark-and-skip-white (arg)
4680 "Go to mark and skip to first non-white character on line."
4681 (interactive "P")
4682 (let ((char (read-char))
4683 (com (vip-getCom arg)))
4684 (vip-goto-mark-subr char com t)))
4686 (defun vip-goto-mark-subr (char com skip-white)
4687 (if (eobp)
4688 (if (bobp)
4689 (error "Empty buffer")
4690 (backward-char 1)))
4691 (cond ((vip-valid-register char '(letter))
4692 (let* ((buff (current-buffer))
4693 (reg (1+ (- char ?a)))
4694 (text-marker (get-register reg)))
4695 (if com (vip-move-marker-locally 'vip-com-point (point)))
4696 (if (not (vip-valid-marker text-marker))
4697 (error (format vip-EmptyTextmarker char)))
4698 (if (and (vip-same-line (point) vip-last-jump)
4699 (= (point) vip-last-jump-ignore))
4700 (push-mark vip-last-jump t)
4701 (push-mark nil t)) ; no msg
4702 (vip-register-to-point reg)
4703 (setq vip-last-jump (point-marker))
4704 (cond (skip-white
4705 (back-to-indentation)
4706 (setq vip-last-jump-ignore (point))))
4707 (if com
4708 (if (equal buff (current-buffer))
4709 (vip-execute-com (if skip-white
4710 'vip-goto-mark-and-skip-white
4711 'vip-goto-mark)
4712 nil com)
4713 (switch-to-buffer buff)
4714 (goto-char vip-com-point)
4715 (vip-change-state-to-vi)
4716 (error "")))))
4717 ((and (not skip-white) (= char ?`))
4718 (if com (vip-move-marker-locally 'vip-com-point (point)))
4719 (if (and (vip-same-line (point) vip-last-jump)
4720 (= (point) vip-last-jump-ignore))
4721 (goto-char vip-last-jump))
4722 (if (= (point) (mark t)) (pop-mark))
4723 (exchange-point-and-mark)
4724 (setq vip-last-jump (point-marker)
4725 vip-last-jump-ignore 0)
4726 (if com (vip-execute-com 'vip-goto-mark nil com)))
4727 ((and skip-white (= char ?'))
4728 (if com (vip-move-marker-locally 'vip-com-point (point)))
4729 (if (and (vip-same-line (point) vip-last-jump)
4730 (= (point) vip-last-jump-ignore))
4731 (goto-char vip-last-jump))
4732 (if (= (point) (mark t)) (pop-mark))
4733 (exchange-point-and-mark)
4734 (setq vip-last-jump (point))
4735 (back-to-indentation)
4736 (setq vip-last-jump-ignore (point))
4737 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
4738 (t (error vip-InvalidTextmarker char))))
4740 (defun vip-insert-tab ()
4741 (interactive)
4742 (insert-tab))
4744 (defun vip-exchange-point-and-mark ()
4745 (interactive)
4746 (exchange-point-and-mark)
4747 (back-to-indentation))
4749 ;; Input Mode Indentation
4751 ;; Returns t, if the string before point matches the regexp STR.
4752 (defsubst vip-looking-back (str)
4753 (and (save-excursion (re-search-backward str nil t))
4754 (= (point) (match-end 0))))
4757 (defun vip-forward-indent ()
4758 "Indent forward -- `C-t' in Vi."
4759 (interactive)
4760 (setq vip-cted t)
4761 (indent-to (+ (current-column) vip-shift-width)))
4763 (defun vip-backward-indent ()
4764 "Backtab, C-d in VI"
4765 (interactive)
4766 (if vip-cted
4767 (let ((p (point)) (c (current-column)) bol (indent t))
4768 (if (vip-looking-back "[0^]")
4769 (progn
4770 (if (eq ?^ (preceding-char))
4771 (setq vip-preserve-indent t))
4772 (delete-backward-char 1)
4773 (setq p (point))
4774 (setq indent nil)))
4775 (save-excursion
4776 (beginning-of-line)
4777 (setq bol (point)))
4778 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4779 (delete-region (point) p)
4780 (if indent
4781 (indent-to (- c vip-shift-width)))
4782 (if (or (bolp) (vip-looking-back "[^ \t]"))
4783 (setq vip-cted nil)))))
4785 (defun vip-autoindent ()
4786 "Auto Indentation, Vi-style."
4787 (interactive)
4788 (let ((col (current-indentation)))
4789 (if vip-preserve-indent
4790 (setq vip-preserve-indent nil)
4791 (setq vip-current-indent col))
4792 ;; don't leave whitespace lines around
4793 (if (memq last-command
4794 '(vip-autoindent
4795 vip-open-line vip-Open-line
4796 vip-replace-state-exit-cmd))
4797 (indent-to-left-margin))
4798 (newline 1)
4799 (if vip-auto-indent
4800 (progn
4801 (setq vip-cted t)
4802 (if vip-electric-mode
4803 (indent-according-to-mode)
4804 (indent-to vip-current-indent))
4809 ;; Viewing registers
4811 (defun vip-ket-function (arg)
4812 "Function called by \], the ket. View registers and call \]\]."
4813 (interactive "P")
4814 (let ((reg (read-char)))
4815 (cond ((vip-valid-register reg '(letter Letter))
4816 (view-register (downcase reg)))
4817 ((vip-valid-register reg '(digit))
4818 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4819 (save-excursion
4820 (set-buffer (get-buffer-create "*Output*"))
4821 (delete-region (point-min) (point-max))
4822 (insert (format "Register %c contains the string:\n" reg))
4823 (insert text)
4824 (goto-char (point-min)))
4825 (display-buffer "*Output*")))
4826 ((= ?\] reg)
4827 (vip-next-heading arg))
4828 (t (error
4829 vip-InvalidRegister reg)))))
4831 (defun vip-brac-function (arg)
4832 "Function called by \[, the brac. View textmarkers and call \[\["
4833 (interactive "P")
4834 (let ((reg (read-char)))
4835 (cond ((= ?\[ reg)
4836 (vip-prev-heading arg))
4837 ((= ?\] reg)
4838 (vip-heading-end arg))
4839 ((vip-valid-register reg '(letter))
4840 (let* ((val (get-register (1+ (- reg ?a))))
4841 (buf (if (not val)
4842 (error vip-EmptyTextmarker reg)
4843 (marker-buffer val)))
4844 (pos (marker-position val))
4845 line-no text (s pos) (e pos))
4846 (save-excursion
4847 (set-buffer (get-buffer-create "*Output*"))
4848 (delete-region (point-min) (point-max))
4849 (if (and buf pos)
4850 (progn
4851 (save-excursion
4852 (set-buffer buf)
4853 (setq line-no (1+ (count-lines (point-min) val)))
4854 (goto-char pos)
4855 (beginning-of-line)
4856 (if (re-search-backward "[^ \t]" nil t)
4857 (progn
4858 (beginning-of-line)
4859 (setq s (point))))
4860 (goto-char pos)
4861 (forward-line 1)
4862 (if (re-search-forward "[^ \t]" nil t)
4863 (progn
4864 (end-of-line)
4865 (setq e (point))))
4866 (setq text (buffer-substring s e))
4867 (setq text (format "%s<%c>%s"
4868 (substring text 0 (- pos s))
4869 reg (substring text (- pos s)))))
4870 (insert
4871 (format
4872 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4873 reg (buffer-name buf) line-no))
4874 (insert (format "Here is some text around %c:\n\n %s"
4875 reg text)))
4876 (insert (format vip-EmptyTextmarker reg)))
4877 (goto-char (point-min)))
4878 (display-buffer "*Output*")))
4879 (t (error vip-InvalidTextmarker reg)))))
4883 ;; commands in insertion mode
4885 (defun vip-delete-backward-word (arg)
4886 "Delete previous word."
4887 (interactive "p")
4888 (save-excursion
4889 (push-mark nil t)
4890 (backward-word arg)
4891 (delete-region (point) (mark t))
4892 (pop-mark)))
4895 (defun vip-set-expert-level (&optional dont-change-unless)
4896 "Sets the expert level for a Viper user.
4897 Can be called interactively to change (temporarily or permanently) the
4898 current expert level.
4900 The optional argument DONT-CHANGE-UNLESS if not nil, says that
4901 the level should not be changed, unless its current value is
4902 meaningless (i.e., not one of 1,2,3,4,5).
4904 User level determines the setting of Viper variables that are most
4905 sensitive for VI-style look-and-feel."
4907 (interactive)
4909 (if (not (numberp vip-expert-level)) (setq vip-expert-level 0))
4911 (save-window-excursion
4912 (delete-other-windows)
4913 ;; if 0 < vip-expert-level < vip-max-expert-level
4914 ;; & dont-change-unless = t -- use it; else ask
4915 (vip-ask-level dont-change-unless))
4917 (setq vip-always t
4918 vip-ex-style-motion t
4919 vip-ex-style-editing-in-insert t
4920 vip-want-ctl-h-help nil)
4922 (cond
4923 ;; a novice or a beginner
4924 ((eq vip-expert-level 1)
4925 (global-set-key vip-toggle-key ;; in emacs-state
4926 (if (vip-window-display-p)
4927 'vip-iconify
4928 'suspend-emacs))
4929 (setq vip-no-multiple-ESC t
4930 vip-re-search t
4931 vip-vi-style-in-minibuffer t
4932 vip-search-wrap-around-t t
4933 vip-want-emacs-keys-in-vi nil
4934 vip-want-emacs-keys-in-insert nil))
4936 ;; an intermediate to guru
4937 ((and (> vip-expert-level 1) (< vip-expert-level 5))
4938 (setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice)
4939 vip-want-emacs-keys-in-vi t
4940 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4942 (if (eq vip-expert-level 4) ; respect user's ex-style motions
4943 ; and vip-no-multiple-ESC
4944 (progn
4945 (setq-default vip-ex-style-editing-in-insert
4946 (cdr (assoc 'vip-ex-style-editing-in-insert
4947 vip-saved-user-settings))
4948 vip-ex-style-motion
4949 (cdr (assoc 'vip-ex-style-motion
4950 vip-saved-user-settings)))
4951 (setq vip-ex-style-motion
4952 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4953 vip-ex-style-editing-in-insert
4954 (cdr (assoc 'vip-ex-style-editing-in-insert
4955 vip-saved-user-settings))
4956 vip-re-search
4957 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4958 vip-no-multiple-ESC
4959 (cdr (assoc 'vip-no-multiple-ESC
4960 vip-saved-user-settings))))))
4962 ;; A wizard
4963 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4964 ;; user toggle variable values.
4965 (t (setq-default vip-ex-style-editing-in-insert
4966 (cdr (assoc 'vip-ex-style-editing-in-insert
4967 vip-saved-user-settings))
4968 vip-ex-style-motion
4969 (cdr (assoc 'vip-ex-style-motion
4970 vip-saved-user-settings)))
4971 (setq vip-want-ctl-h-help
4972 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
4973 vip-always
4974 (cdr (assoc 'vip-always vip-saved-user-settings))
4975 vip-no-multiple-ESC
4976 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
4977 vip-ex-style-motion
4978 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4979 vip-ex-style-editing-in-insert
4980 (cdr (assoc 'vip-ex-style-editing-in-insert
4981 vip-saved-user-settings))
4982 vip-re-search
4983 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4984 vip-want-emacs-keys-in-vi
4985 (cdr (assoc 'vip-want-emacs-keys-in-vi
4986 vip-saved-user-settings))
4987 vip-want-emacs-keys-in-insert
4988 (cdr (assoc 'vip-want-emacs-keys-in-insert
4989 vip-saved-user-settings)))))
4990 (vip-set-mode-vars-for vip-current-state)
4991 (if (or vip-always
4992 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
4993 (vip-set-hooks)))
4995 ;; Ask user expert level.
4996 (defun vip-ask-level (dont-change-unless)
4997 (let ((ask-buffer " *vip-ask-level*")
4998 level-changed repeated)
4999 (save-window-excursion
5000 (switch-to-buffer ask-buffer)
5002 (or (eq this-command 'vip-set-expert-level)
5003 (and
5004 (<= vip-expert-level vip-max-expert-level)
5005 (>= vip-expert-level 1))
5006 (progn
5007 (insert "
5009 *** Important Notice for VIP users***
5011 This is VIPER
5013 @joke
5014 Viper Is a Package for Emacs Rebels,
5015 a VI Plan for Emacs Rescue,
5016 and a venomous VI PERil.
5017 @end joke
5019 Technically speaking, Viper is a new Vi emulator that replaces
5020 the old VIP package.
5022 Viper emulates Vi much better than VIP. It also significantly
5023 extends and improves upon Vi in many useful ways.
5025 Although many VIP settings in your ~/.vip are compatible with Viper,
5026 you may have to change some of them. Please refer to the documentation,
5027 which can be obtained by executing
5029 :help
5031 when Viper is in Vi state.
5033 If you will be so lucky as to find a bug, report it via the command
5035 :submitReport
5037 Type any key to continue... ")
5039 (read-char)
5040 (erase-buffer)))
5042 (while (or (> vip-expert-level vip-max-expert-level)
5043 (< vip-expert-level 1)
5044 (null dont-change-unless))
5045 (erase-buffer)
5046 (if repeated
5047 (progn
5048 (message "Invalid user level")
5049 (beep 1))
5050 (setq repeated t))
5051 (setq dont-change-unless t
5052 level-changed t)
5053 (insert "
5054 Please specify your level of familiarity with the venomous VI PERil
5055 (and the VI Plan for Emacs Rescue).
5056 You can change it at any time by typing `M-x vip-set-expert-level RET'
5058 1 -- BEGINNER: Almost all Emacs features are suppressed.
5059 Feels almost like straight Vi. File name completion and
5060 command history in the minibuffer are thrown in as a bonus.
5061 To use Emacs productively, you must reach level 3 or higher.
5062 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
5063 so most Emacs commands can be used when Viper is in Vi state.
5064 Good progress---you are well on the way to level 3!
5065 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
5066 in Viper's insert state.
5067 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
5068 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
5069 variables. Adjust these settings to your taste.
5070 5 -- WIZARD: Like 4, but user settings are also respected for vip-always,
5071 vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and
5072 vip-want-emacs-keys-in-insert. Adjust these to your taste.
5074 Please, specify your level now: ")
5076 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
5077 ) ; end while
5079 ;; tell the user if level was changed
5080 (and level-changed
5081 (progn
5082 (insert
5083 (format "\n\n\n\n\n\t\tYou have selected user level %d"
5084 vip-expert-level))
5085 (if (y-or-n-p "Do you wish to make this change permanent? ")
5086 ;; save the setting for vip-expert-level
5087 (vip-save-setting
5088 'vip-expert-level
5089 (format "Saving user level %d ..." vip-expert-level)
5090 vip-custom-file-name))
5092 (bury-buffer) ; remove ask-buffer from screen
5093 (message "")
5097 (defun viper-version ()
5098 (interactive)
5099 (message "Viper version is %s" viper-version))
5101 (defalias 'vip-version 'viper-version)
5103 (defun vip-nil ()
5104 (interactive)
5105 (beep 1))
5108 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
5109 (defun vip-register-to-point (char &optional enforce-buffer)
5110 "Like jump-to-register, but switches to another buffer in another window."
5111 (interactive "cViper register to point: ")
5112 (let ((val (get-register char)))
5113 (cond
5114 ((and (fboundp 'frame-configuration-p)
5115 (frame-configuration-p val))
5116 (set-frame-configuration val))
5117 ((window-configuration-p val)
5118 (set-window-configuration val))
5119 ((vip-valid-marker val)
5120 (if (and enforce-buffer
5121 (not (equal (current-buffer) (marker-buffer val))))
5122 (error (concat vip-EmptyTextmarker " in this buffer")
5123 (1- (+ char ?a))))
5124 (pop-to-buffer (marker-buffer val))
5125 (goto-char val))
5126 ((and (consp val) (eq (car val) 'file))
5127 (find-file (cdr val)))
5129 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
5132 (defun vip-save-kill-buffer ()
5133 "Save then kill current buffer. "
5134 (interactive)
5135 (if (< vip-expert-level 2)
5136 (save-buffers-kill-emacs)
5137 (save-buffer)
5138 (kill-buffer (current-buffer))))
5142 ;;; Bug Report
5144 (defun vip-submit-report ()
5145 "Submit bug report on Viper."
5146 (interactive)
5147 (let ((reporter-prompt-for-summary-p t)
5148 (vip-device-type (vip-device-type))
5149 color-display-p frame-parameters
5150 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
5151 varlist salutation window-config)
5153 ;; If mode info is needed, add variable to `let' and then set it below,
5154 ;; like we did with color-display-p.
5155 (setq color-display-p (if (vip-window-display-p)
5156 (vip-color-display-p)
5157 'non-x)
5158 minibuffer-vi-face (if (vip-has-face-support-p)
5159 (vip-get-face vip-minibuffer-vi-face)
5160 'non-x)
5161 minibuffer-insert-face (if (vip-has-face-support-p)
5162 (vip-get-face vip-minibuffer-insert-face)
5163 'non-x)
5164 minibuffer-emacs-face (if (vip-has-face-support-p)
5165 (vip-get-face vip-minibuffer-emacs-face)
5166 'non-x)
5167 frame-parameters (if (fboundp 'frame-parameters)
5168 (frame-parameters (selected-frame))))
5170 (setq varlist (list 'vip-vi-minibuffer-minor-mode
5171 'vip-insert-minibuffer-minor-mode
5172 'vip-vi-intercept-minor-mode
5173 'vip-vi-local-user-minor-mode
5174 'vip-vi-kbd-minor-mode
5175 'vip-vi-global-user-minor-mode
5176 'vip-vi-state-modifier-minor-mode
5177 'vip-vi-diehard-minor-mode
5178 'vip-vi-basic-minor-mode
5179 'vip-replace-minor-mode
5180 'vip-insert-intercept-minor-mode
5181 'vip-insert-local-user-minor-mode
5182 'vip-insert-kbd-minor-mode
5183 'vip-insert-global-user-minor-mode
5184 'vip-insert-state-modifier-minor-mode
5185 'vip-insert-diehard-minor-mode
5186 'vip-insert-basic-minor-mode
5187 'vip-emacs-intercept-minor-mode
5188 'vip-emacs-local-user-minor-mode
5189 'vip-emacs-kbd-minor-mode
5190 'vip-emacs-global-user-minor-mode
5191 'vip-emacs-state-modifier-minor-mode
5192 'vip-automatic-iso-accents
5193 'vip-want-emacs-keys-in-insert
5194 'vip-want-emacs-keys-in-vi
5195 'vip-keep-point-on-undo
5196 'vip-no-multiple-ESC
5197 'vip-ESC-key
5198 'vip-want-ctl-h-help
5199 'vip-ex-style-editing-in-insert
5200 'vip-delete-backwards-in-replace
5201 'vip-vi-style-in-minibuffer
5202 'vip-vi-state-hook
5203 'vip-insert-state-hook
5204 'vip-replace-state-hook
5205 'vip-emacs-state-hook
5206 'ex-cycle-other-window
5207 'ex-cycle-through-non-files
5208 'vip-expert-level
5209 'major-mode
5210 'vip-device-type
5211 'color-display-p
5212 'frame-parameters
5213 'minibuffer-vi-face
5214 'minibuffer-insert-face
5215 'minibuffer-emacs-face
5217 (setq salutation "
5218 Congratulations! You may have unearthed a bug in Viper!
5219 Please mail a concise, accurate summary of the problem to the address above.
5221 -------------------------------------------------------------------")
5222 (setq window-config (current-window-configuration))
5223 (with-output-to-temp-buffer " *vip-info*"
5224 (switch-to-buffer " *vip-info*")
5225 (delete-other-windows)
5226 (princ "
5227 PLEASE FOLLOW THESE PROCEDURES
5228 ------------------------------
5230 Before reporting a bug, please verify that it is related to Viper, and is
5231 not cause by other packages you are using.
5233 Don't report compilation warnings, unless you are certain that there is a
5234 problem. These warnings are normal and unavoidable.
5236 Please note that users should not modify variables and keymaps other than
5237 those advertised in the manual. Such `customization' is likely to crash
5238 Viper, as it would any other improperly customized Emacs package.
5240 If you are reporting an error message received while executing one of the
5241 Viper commands, type:
5243 M-x set-variable <Return> debug-on-error <Return> t <Return>
5245 Then reproduce the error. The above command will cause Emacs to produce a
5246 back trace of the execution that leads to the error. Please include this
5247 trace in your bug report.
5249 If you believe that one of Viper's commands goes into an infinite loop
5250 \(e.g., Emacs freezes\), type:
5252 M-x set-variable <Return> debug-on-quit <Return> t <Return>
5254 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
5255 the current command. Include the resulting back trace in the bug report.
5257 Mail anyway (y or n)? ")
5258 (if (y-or-n-p "Mail anyway? ")
5260 (set-window-configuration window-config)
5261 (error "Bug report aborted")))
5263 (require 'reporter)
5264 (set-window-configuration window-config)
5266 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
5267 (vip-version)
5268 varlist
5269 nil 'delete-other-windows
5270 salutation)
5276 ;; Smoothes out the difference between Emacs' unread-command-events
5277 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
5278 ;; events or a sequence of keys.
5280 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
5281 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
5282 ;; into an event. Below, we delete nil from event lists, since nil is the most
5283 ;; common symbol that might appear in this wrong context.
5284 (defun vip-set-unread-command-events (arg)
5285 (if vip-emacs-p
5286 (setq
5287 unread-command-events
5288 (let ((new-events
5289 (cond ((eventp arg) (list arg))
5290 ((listp arg) arg)
5291 ((sequencep arg)
5292 (listify-key-sequence arg))
5293 (t (error
5294 "vip-set-unread-command-events: Invalid argument, %S"
5295 arg)))))
5296 (if (not (eventp nil))
5297 (setq new-events (delq nil new-events)))
5298 (append new-events unread-command-events)))
5299 ;; XEmacs
5300 (setq
5301 unread-command-events
5302 (append
5303 (cond ((numberp arg) (list (character-to-event arg)))
5304 ((eventp arg) (list arg))
5305 ((stringp arg) (mapcar 'character-to-event arg))
5306 ((vectorp arg) (append arg nil)) ; turn into list
5307 ((listp arg) (vip-eventify-list-xemacs arg))
5308 (t (error
5309 "vip-set-unread-command-events: Invalid argument, %S" arg)))
5310 unread-command-events))))
5312 ;; list is assumed to be a list of events of characters
5313 (defun vip-eventify-list-xemacs (lis)
5314 (mapcar
5315 (function (lambda (elt)
5316 (cond ((numberp elt) (character-to-event elt))
5317 ((eventp elt) elt)
5318 (t (error
5319 "vip-eventify-list-xemacs: can't convert to event, %S"
5320 elt)))))
5321 lis))
5325 ;;; Bring in the rest of the files
5326 (require 'viper-mous)
5327 (require 'viper-macs)
5328 (require 'viper-ex)
5332 ;; The following is provided for compatibility with older VIP's
5334 (defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
5335 (defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert)
5336 (defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs)
5340 ;;; Load .vip and set up hooks
5342 ;; This hook designed to enable Vi-style editing in comint-based modes."
5343 (defun vip-comint-mode-hook ()
5344 (setq require-final-newline nil)
5345 (setq vip-ex-style-editing-in-insert nil
5346 vip-ex-style-motion nil)
5347 (vip-add-local-keys 'vi-state
5348 '(("\C-m" . comint-send-input) ; return
5349 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5350 (vip-add-local-keys 'insert-state
5351 '(("\C-m" . comint-send-input) ; return
5352 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5356 ;; This sets major mode hooks to make them come up in vi-state.
5357 (defun vip-set-hooks ()
5359 ;; It is of course a misnomer to call viper-mode a `major mode'.
5360 ;; However, this has the effect that if the user didn't specify the
5361 ;; default mode, new buffers that fall back on the default will come up
5362 ;; in Fundamental Mode and Vi state.
5363 (setq default-major-mode 'viper-mode)
5365 ;; The following major modes should come up in vi-state
5366 (defadvice fundamental-mode (after vip-fundamental-mode-ad activate)
5367 "Run `vip-change-state-to-vi' on entry."
5368 (vip-change-state-to-vi))
5370 (defvar makefile-mode-hook nil)
5371 (add-hook 'makefile-mode-hook 'viper-mode)
5373 (defvar help-mode-hook nil)
5374 (add-hook 'help-mode-hook 'viper-mode)
5376 (defvar awk-mode-hook nil)
5377 (add-hook 'awk-mode-hook 'viper-mode)
5379 (defvar html-mode-hook nil)
5380 (add-hook 'html-mode-hook 'viper-mode)
5381 (defvar html-helper-mode-hook nil)
5382 (add-hook 'html-helper-mode-hook 'viper-mode)
5384 (defvar emacs-lisp-mode-hook nil)
5385 (add-hook 'emacs-lisp-mode-hook 'viper-mode)
5387 (defvar lisp-mode-hook nil)
5388 (add-hook 'lisp-mode-hook 'viper-mode)
5390 (defvar bibtex-mode-hook nil)
5391 (add-hook 'bibtex-mode-hook 'viper-mode)
5393 (defvar cc-mode-hook nil)
5394 (add-hook 'cc-mode-hook 'viper-mode)
5396 (defvar c-mode-hook nil)
5397 (add-hook 'c-mode-hook 'viper-mode)
5399 (defvar c++-mode-hook nil)
5400 (add-hook 'c++-mode-hook 'viper-mode)
5402 (defvar lisp-interaction-mode-hook nil)
5403 (add-hook 'lisp-interaction-mode-hook 'viper-mode)
5405 (defvar text-mode-hook nil)
5406 (add-hook 'text-mode-hook 'viper-mode)
5408 (add-hook 'completion-list-mode-hook 'viper-mode)
5409 (add-hook 'compilation-mode-hook 'viper-mode)
5411 (add-hook 'perl-mode-hook 'viper-mode)
5412 (add-hook 'tcl-mode-hook 'viper-mode)
5414 (defvar emerge-startup-hook nil)
5415 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
5416 ;; Run vip-change-state-to-vi after quitting emerge.
5417 (vip-eval-after-load
5418 "emerge"
5419 '(defadvice emerge-quit (after vip-emerge-advice activate)
5420 "Run `vip-change-state-to-vi' after quitting emerge."
5421 (vip-change-state-to-vi)))
5422 ;; In case Emerge was loaded before Viper.
5423 (defadvice emerge-quit (after vip-emerge-advice activate)
5424 "Run `vip-change-state-to-vi' after quitting emerge."
5425 (vip-change-state-to-vi))
5427 (vip-eval-after-load
5428 "asm-mode"
5429 '(defadvice asm-mode (after vip-asm-mode-ad activate)
5430 "Run `vip-change-state-to-vi' on entry."
5431 (vip-change-state-to-vi)))
5433 ;; passwd.el sets up its own buffer, which turns up in Vi mode,
5434 ;; thus overriding the local map. We don't need Vi mode here.
5435 (vip-eval-after-load
5436 "passwd"
5437 '(defadvice read-passwd-1 (before vip-passwd-ad activate)
5438 "Switch to emacs state while reading password."
5439 (vip-change-state-to-emacs)))
5441 (vip-eval-after-load
5442 "prolog"
5443 '(defadvice prolog-mode (after vip-prolog-ad activate)
5444 "Switch to Vi state in Prolog mode."
5445 (vip-change-state-to-vi)))
5447 ;; Emacs shell, ange-ftp, and comint-based modes
5448 (defvar comint-mode-hook nil)
5449 (add-hook 'comint-mode-hook 'vip-change-state-to-insert)
5450 (add-hook 'comint-mode-hook 'vip-comint-mode-hook)
5452 ;; Shell scripts
5453 (defvar sh-mode-hook nil)
5454 (add-hook 'sh-mode-hook 'viper-mode)
5455 (defvar ksh-mode-hook nil)
5456 (add-hook 'ksh-mode-hook 'viper-mode)
5458 ;; Dired
5459 ;; This is only necessary when the user uses vip-modify-major-mode
5460 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
5462 (if vip-emacs-p
5463 (progn
5464 (defvar view-mode-hook nil
5465 "View hook. Run after view mode.")
5466 (add-hook 'view-mode-hook 'vip-change-state-to-emacs))
5467 (defadvice view-minor-mode (after vip-view-ad activate)
5468 "Switch to Emacs state in View mode."
5469 (vip-change-state-to-emacs))
5470 (defvar view-hook nil
5471 "View hook. Run after view mode.")
5472 (add-hook 'view-hook 'vip-change-state-to-emacs))
5474 ;; For VM users.
5475 ;; Put summary and other VM buffers in Emacs state.
5476 (defvar vm-mode-hooks nil
5477 "This hook is run after vm is started.")
5478 (defvar vm-summary-mode-hooks nil
5479 "This hook is run after vm switches to summary mode.")
5480 (add-hook 'vm-mode-hooks 'vip-change-state-to-emacs)
5481 (add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs)
5483 ;; For RMAIL users.
5484 ;; Put buf in Emacs state after edit.
5485 (vip-eval-after-load
5486 "rmailedit"
5487 '(defadvice rmail-cease-edit (after vip-rmail-advice activate)
5488 "Switch to emacs state when done editing message."
5489 (vip-change-state-to-emacs)))
5490 ;; In case RMAIL was loaded before Viper.
5491 (defadvice rmail-cease-edit (after vip-rmail-advice activate)
5492 "Switch to emacs state when done editing message."
5493 (vip-change-state-to-emacs))
5494 ) ; vip-set-hooks
5496 ;; Set some useful macros
5497 ;; These must be before we load .vip, so the user can unrecord them.
5499 ;; repeat the 2nd previous command without rotating the command history
5500 (vip-record-kbd-macro
5501 (vector vip-repeat-from-history-key '\1) 'vi-state
5502 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5503 ;; repeat the 3d previous command without rotating the command history
5504 (vip-record-kbd-macro
5505 (vector vip-repeat-from-history-key '\2) 'vi-state
5506 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5508 ;; toggle case sensitivity in search
5509 (vip-record-kbd-macro
5510 "//" 'vi-state
5511 [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)
5512 ;; toggle regexp/vanila search
5513 (vip-record-kbd-macro
5514 "///" 'vi-state
5515 [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)
5518 ;; ~/.vip is loaded if it exists
5519 (if (and (file-exists-p vip-custom-file-name)
5520 (not noninteractive))
5521 (load vip-custom-file-name))
5523 ;; VIP compatibility: merge whatever the user has in vip-mode-map into
5524 ;; Viper's basic map.
5525 (vip-add-keymap vip-mode-map vip-vi-global-user-map)
5528 ;; Applying Viper customization -- runs after (load .vip)
5530 ;; Save user settings or Viper defaults for vars controled by vip-expert-level
5531 (setq vip-saved-user-settings
5532 (list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help)
5533 (cons 'vip-always vip-always)
5534 (cons 'vip-no-multiple-ESC vip-no-multiple-ESC)
5535 (cons 'vip-ex-style-motion vip-ex-style-motion)
5536 (cons 'vip-ex-style-editing-in-insert
5537 vip-ex-style-editing-in-insert)
5538 (cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi)
5539 (cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert)
5540 (cons 'vip-re-search vip-re-search)))
5543 (vip-set-minibuffer-style)
5544 (vip-set-minibuffer-faces)
5545 (vip-set-search-face)
5546 (if vip-buffer-search-char
5547 (vip-buffer-search-enable))
5548 (vip-update-alphanumeric-class)
5550 ;;; Familiarize Viper with some minor modes that have their own keymaps
5551 (vip-harness-minor-mode "compile")
5552 (vip-harness-minor-mode "outline")
5553 (vip-harness-minor-mode "allout")
5554 (vip-harness-minor-mode "xref")
5555 (vip-harness-minor-mode "lmenu")
5556 (vip-harness-minor-mode "vc")
5557 (vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX
5558 (vip-harness-minor-mode "latex") ; which is in one of these two files
5559 (vip-harness-minor-mode "cyrillic")
5560 (vip-harness-minor-mode "russian")
5561 (vip-harness-minor-mode "view-less")
5562 (vip-harness-minor-mode "view")
5565 ;; Intercept maps could go in viper-keym.el
5566 ;; We keep them here in case someone redefines them in ~/.vip
5568 (define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5569 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5571 ;; This is taken care of by vip-insert-global-user-map.
5572 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
5574 (define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC)
5575 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
5576 ;; iconifies Emacs
5577 (define-key vip-vi-intercept-map vip-toggle-key
5578 '(lambda () (interactive)
5579 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
5580 (if (vip-window-display-p) (vip-iconify) (suspend-emacs))
5581 (vip-change-state-to-emacs))))
5583 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
5586 (if (or vip-always
5587 (and (< vip-expert-level 5) (> vip-expert-level 0)))
5588 (vip-set-hooks))
5590 ;; Let all minor modes take effect after loading
5591 ;; this may not be enough, so we also set default minor-mode-alist.
5592 ;; Without setting the default, new buffers that come up in emacs mode have
5593 ;; minor-mode-map-alist = nil, unless we call vip-change-state-*
5594 (if (eq vip-current-state 'emacs-state)
5595 (progn
5596 (vip-change-state-to-emacs)
5597 (setq-default minor-mode-map-alist minor-mode-map-alist)
5601 (run-hooks 'vip-load-hook) ; the last chance to change something
5603 (provide 'viper)
5604 (provide 'vip19)
5605 (provide 'vip)
5607 ;;; viper.el ends here