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.
6 ;; Keywords: emulations
7 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
9 (defconst viper-version "2.85 of January 24, 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)
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.
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.
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
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.
101 ;;; Acknowledgements:
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.
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
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)
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
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
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
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
301 (require 'viper-util)
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
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
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
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.")
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.")
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
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-window-display-p)
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)
476 (if (vip-can-use-colors "darkseagreen2" "Black")
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'
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'
495 (defvar vip-use-replace-region-delimiters
496 (or (not (vip-window-display-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
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.")
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"
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
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
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
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
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
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
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
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" "")
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)
797 ;; Last shell command executed with ! command.
798 (defvar vip-last-shell-com nil)
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.")
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))
815 ((memq system-type '(emx ms-dos))
817 ((memq system-type '(windows-nt windows-95))
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.")
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)
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 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
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)
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))
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))
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))
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))
1021 ((eq vip-current-state 'replace-state)
1022 ;; delete characters to compensate for inserted chars.
1023 (let ((replace-boundary (vip-replace-end)))
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
1032 (vip-finish-change vip-last-posn-in-replace-region)))
1034 (if (and (<= (vip-replace-start) (point))
1035 (<= (point) replace-boundary))
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))))
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
1079 (vip-save-last-insertion
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
1086 (vip-repeat-insert-command))))
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))
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))
1126 (define-key vip-insert-basic-map "\C-m" 'vip-autoindent)
1127 (if vip-want-emacs-keys-in-insert
1129 (define-key vip-insert-basic-map "\C-j" nil)
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
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
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
1167 (cdr (assoc major-mode vip-vi-state-modifier-alist)))
1168 (cdr (assoc major-mode vip-vi-state-modifier-alist))
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
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
1186 (assoc major-mode vip-insert-state-modifier-alist)))
1188 (assoc major-mode vip-insert-state-modifier-alist))
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
1202 (assoc major-mode vip-emacs-state-modifier-alist)))
1204 (assoc major-mode vip-emacs-state-modifier-alist))
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))
1227 (defun viper-mode ()
1228 "Turn on Viper emulation of Vi."
1230 (if (not noninteractive)
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")
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
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
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
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? ")
1288 'vip-inhibit-startup-message
1289 "Viper startup message inhibited"
1290 vip-custom-file-name t))
1291 (kill-buffer (current-buffer))))
1293 (vip-set-expert-level 'dont-change-unless)))
1294 (vip-change-state-to-vi))))
1297 (defalias 'vip-mode 'viper-mode)
1300 ;; Switch from Insert state to Vi state.
1301 (defun vip-exit-insert-state ()
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."
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
1337 (if (eq state 'vi-state) ; adjust for vi-state
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))
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
1382 (if (vip-window-display-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."
1398 (if (and vip-first-time (not (vip-is-in-minibuffer)))
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
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)
1424 (vip-message-conditions conds)))))
1426 (defun vip-change-state-to-insert ()
1427 "Change Viper state to Insert."
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)
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)
1456 (vip-message-conditions conds)))
1460 ;; 'R' is implemented using Emacs's overwrite-mode
1465 (defun vip-change-state-to-emacs ()
1466 "Change Viper state to Emacs."
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)
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."
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,
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
1508 (setq com (key-binding (setq key
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))))
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))
1535 (setq prefix-arg arg)
1536 (command-execute com)))
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))
1547 (vip-set-mode-vars-for 'emacs-state)
1548 (setq result (eval form))
1549 (if (not (equal buff (current-buffer))) ; cmd switched buffer
1552 (vip-set-mode-vars-for vip-current-state)))
1553 (vip-set-mode-vars-for vip-current-state)
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
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."
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))
1601 (vip-escape-to-emacs arg (list ?\e char) )))
1605 (defun vip-alternate-ESC (arg)
1606 "ESC key without checking for multiple keystrokes."
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)
1621 (if (vip-ESC-event-p event)
1623 (if (vip-fast-keysequence-p)
1625 (let (minor-mode-map-alist)
1626 (vip-set-unread-command-events event)
1629 (ad-get-orig-definition 'read-key-sequence) nil))
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
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
1662 (vector (character-to-event ?\e)))))
1664 (key-press-event-p first-key)
1665 (equal '(meta) key-mod))
1666 (vip-set-unread-command-events
1668 (character-to-event (event-key first-key)))
1670 (setq last-input-event event
1671 keyseq (vector (character-to-event ?\e))))
1673 (setq last-command-event first-key))
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)
1681 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
1683 ;; not an escape event
1684 (setq keyseq (vector event)))
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))))
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."
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)
1733 (cond ((eq vip-current-state 'vi-state)
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)
1755 (if (or (null (car arg)) (equal (car arg) '(nil)))
1759 ;; Get raw value part of prefix-argument ARG.
1760 (defsubst vip-P-val (arg)
1761 (cond ((consp arg) (car arg))
1764 ;; Get com part of prefix-argument ARG.
1765 (defsubst vip-getcom (arg)
1766 (cond ((null arg) nil)
1767 ((consp arg) (cdr arg))
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)
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)
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)
1800 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1801 vip-buffer-search-char)))
1803 ;; this means that we already have a command character, so we
1804 ;; construct a com list and exit while. however, if char is "
1807 ;; new com is (CHAR . OLDCOM)
1808 (if (memq char '(?# ?\")) (error ""))
1809 (setq com (cons char com))
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
1815 (cond ((memq char '(?! ?=))
1817 (setq char (read-char))
1820 ;; read a char and encode it as com
1821 (setq com (+ 128 (read-char)))
1822 (setq char (read-char)))
1824 (let ((reg (read-char)))
1825 (if (vip-valid-register reg)
1826 (setq vip-use-register reg)
1828 (setq char (read-char))))
1831 (setq char (vip-read-char-exclusive)))))))
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
1837 (setq prefix-arg (cons value com))
1839 (vip-describe-arg prefix-arg)
1840 (setq char (read-char)))
1841 (vip-set-unread-command-events char)
1843 ;; as com is non-nil, this means that we have a command to execute
1844 (if (memq (car com) '(?r ?R))
1845 ;; execute apropriate region command.
1846 (let ((char (car com)) (com (cdr com)))
1847 (setq prefix-arg (cons value com))
1848 (if (= char ?r) (vip-region prefix-arg)
1849 (vip-Region prefix-arg))
1851 (setq prefix-arg nil))
1852 ;; otherwise, reset prefix arg and call appropriate command
1853 (setq value (if (null value) 1 value))
1854 (setq prefix-arg nil)
1855 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1856 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1857 ((equal com '(?d . ?y)) (vip-yank-defun))
1858 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1859 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1860 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1861 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1862 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1865 (defun vip-describe-arg (arg)
1867 (setq val (vip-P-val arg)
1868 com (vip-getcom arg))
1871 (message "Value is nil, and command is nil")
1872 (message "Value is nil, and command is `%c'" com))
1874 (message "Value is `%d', and command is nil" val)
1875 (message "Value is `%d', and command is `%c'" val com)))))
1877 (defun vip-digit-argument (arg)
1878 "Begin numeric argument for the next command."
1880 (vip-leave-region-active)
1881 (vip-prefix-arg-value last-command-char
1882 (if (consp arg) (cdr arg) nil)))
1884 (defun vip-command-argument (arg)
1885 "Accept a motion command as an argument."
1890 (cond ((null arg) nil)
1891 ((consp arg) (car arg))
1893 (t (error vip-InvalidCommandArgument)))
1894 (cond ((null arg) nil)
1895 ((consp arg) (cdr arg))
1897 (t (error vip-InvalidCommandArgument))))
1898 (quit (setq vip-use-register nil)
1899 (signal 'quit nil)))
1900 (vip-deactivate-mark))
1903 ;; repeat last destructive command
1905 ;; Append region to text in register REG.
1906 ;; START and END are buffer positions indicating what to append.
1907 (defsubst vip-append-to-register (reg start end)
1908 (set-register reg (concat (if (stringp (get-register reg))
1909 (get-register reg) "")
1910 (buffer-substring start end))))
1912 ;; Saves last inserted text for possible use by vip-repeat command.
1913 (defun vip-save-last-insertion (beg end)
1914 (setq vip-last-insertion (buffer-substring beg end))
1915 (or (< (length vip-d-com) 5)
1916 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
1917 (or (null vip-command-ring)
1918 (ring-empty-p vip-command-ring)
1920 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
1922 ;; del most recent elt, if identical to the second most-recent
1923 (vip-cleanup-ring vip-command-ring)))
1926 (defsubst vip-yank-last-insertion ()
1927 "Inserts the text saved by the previous vip-save-last-insertion command."
1929 (insert vip-last-insertion)
1933 ;; define functions to be executed
1935 ;; invoked by the `C' command
1936 (defun vip-exec-change (m-com com)
1937 ;; handle C cmd at the eol and at eob.
1938 (if (or (and (eolp) (= vip-com-point (point)))
1939 (= vip-com-point (point-max)))
1941 (insert " ")(backward-char 1)))
1942 (if (= vip-com-point (point))
1943 (vip-forward-char-carefully))
1945 (vip-change vip-com-point (point))
1946 (vip-change-subr vip-com-point (point))))
1948 ;; this is invoked by vip-substitute-line
1949 (defun vip-exec-Change (m-com com)
1951 (set-mark vip-com-point)
1952 (vip-enlarge-region (mark t) (point))
1953 (if vip-use-register
1955 (cond ((vip-valid-register vip-use-register '(letter digit))
1956 ;;(vip-valid-register vip-use-register '(letter)
1958 vip-use-register (mark t) (point) nil))
1959 ((vip-valid-register vip-use-register '(Letter))
1960 (vip-append-to-register
1961 (downcase vip-use-register) (mark t) (point)))
1962 (t (setq vip-use-register nil)
1963 (error vip-InvalidRegister vip-use-register)))
1964 (setq vip-use-register nil)))
1965 (delete-region (mark t) (point)))
1967 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
1969 (defun vip-exec-delete (m-com com)
1970 (if vip-use-register
1972 (cond ((vip-valid-register vip-use-register '(letter digit))
1973 ;;(vip-valid-register vip-use-register '(letter))
1975 vip-use-register vip-com-point (point) nil))
1976 ((vip-valid-register vip-use-register '(Letter))
1977 (vip-append-to-register
1978 (downcase vip-use-register) vip-com-point (point)))
1979 (t (setq vip-use-register nil)
1980 (error vip-InvalidRegister vip-use-register)))
1981 (setq vip-use-register nil)))
1983 (if (eq last-command 'd-command) 'kill-region nil))
1984 (kill-region vip-com-point (point))
1985 (setq this-command 'd-command)
1986 (if vip-ex-style-motion
1987 (if (and (eolp) (not (bolp))) (backward-char 1))))
1989 (defun vip-exec-Delete (m-com com)
1991 (set-mark vip-com-point)
1992 (vip-enlarge-region (mark t) (point))
1993 (if vip-use-register
1995 (cond ((vip-valid-register vip-use-register '(letter digit))
1996 ;;(vip-valid-register vip-use-register '(letter))
1998 vip-use-register (mark t) (point) nil))
1999 ((vip-valid-register vip-use-register '(Letter))
2000 (vip-append-to-register
2001 (downcase vip-use-register) (mark t) (point)))
2002 (t (setq vip-use-register nil)
2003 (error vip-InvalidRegister vip-use-register)))
2004 (setq vip-use-register nil)))
2006 (if (eq last-command 'D-command) 'kill-region nil))
2007 (kill-region (mark t) (point))
2008 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
2009 (back-to-indentation))
2011 (defun vip-exec-yank (m-com com)
2012 (if vip-use-register
2014 (cond ((vip-valid-register vip-use-register '(letter digit))
2015 ;; (vip-valid-register vip-use-register '(letter))
2017 vip-use-register vip-com-point (point) nil))
2018 ((vip-valid-register vip-use-register '(Letter))
2019 (vip-append-to-register
2020 (downcase vip-use-register) vip-com-point (point)))
2021 (t (setq vip-use-register nil)
2022 (error vip-InvalidRegister vip-use-register)))
2023 (setq vip-use-register nil)))
2024 (setq last-command nil)
2025 (copy-region-as-kill vip-com-point (point))
2026 (goto-char vip-com-point))
2028 (defun vip-exec-Yank (m-com com)
2030 (set-mark vip-com-point)
2031 (vip-enlarge-region (mark t) (point))
2032 (if vip-use-register
2034 (cond ((vip-valid-register vip-use-register '(letter digit))
2036 vip-use-register (mark t) (point) nil))
2037 ((vip-valid-register vip-use-register '(Letter))
2038 (vip-append-to-register
2039 (downcase vip-use-register) (mark t) (point)))
2040 (t (setq vip-use-register nil)
2041 (error vip-InvalidRegister vip-use-register)))
2042 (setq vip-use-register nil)))
2043 (setq last-command nil)
2044 (copy-region-as-kill (mark t) (point)))
2045 (vip-deactivate-mark)
2046 (goto-char vip-com-point))
2048 (defun vip-exec-bang (m-com com)
2050 (set-mark vip-com-point)
2051 (vip-enlarge-region (mark t) (point))
2052 (shell-command-on-region
2055 (setq vip-last-shell-com
2056 (vip-read-string-with-history
2060 (car vip-shell-history)
2065 (defun vip-exec-equals (m-com com)
2067 (set-mark vip-com-point)
2068 (vip-enlarge-region (mark t) (point))
2069 (if (> (mark t) (point)) (exchange-point-and-mark))
2070 (indent-region (mark t) (point) nil)))
2072 (defun vip-exec-shift (m-com com)
2074 (set-mark vip-com-point)
2075 (vip-enlarge-region (mark t) (point))
2076 (if (> (mark t) (point)) (exchange-point-and-mark))
2077 (indent-rigidly (mark t) (point)
2080 (- vip-shift-width))))
2081 ;; return point to where it was before shift
2082 (goto-char vip-com-point))
2084 ;; this is needed because some commands fake com by setting it to ?r, which
2085 ;; denotes repeated insert command.
2086 (defsubst vip-exec-dummy (m-com com)
2089 (defun vip-exec-buffer-search (m-com com)
2090 (setq vip-s-string (buffer-substring (point) vip-com-point))
2091 (setq vip-s-forward t)
2092 (setq vip-search-history (cons vip-s-string vip-search-history))
2093 (vip-search vip-s-string vip-s-forward 1))
2095 (defvar vip-exec-array (make-vector 128 nil))
2097 ;; Using a dispatch array allows adding functions like buffer search
2098 ;; without affecting other functions. Buffer search can now be bound
2099 ;; to any character.
2101 (aset vip-exec-array ?c 'vip-exec-change)
2102 (aset vip-exec-array ?C 'vip-exec-Change)
2103 (aset vip-exec-array ?d 'vip-exec-delete)
2104 (aset vip-exec-array ?D 'vip-exec-Delete)
2105 (aset vip-exec-array ?y 'vip-exec-yank)
2106 (aset vip-exec-array ?Y 'vip-exec-Yank)
2107 (aset vip-exec-array ?r 'vip-exec-dummy)
2108 (aset vip-exec-array ?! 'vip-exec-bang)
2109 (aset vip-exec-array ?< 'vip-exec-shift)
2110 (aset vip-exec-array ?> 'vip-exec-shift)
2111 (aset vip-exec-array ?= 'vip-exec-equals)
2115 ;; This function is called by various movement commands to execute a
2116 ;; destructive command on the region specified by the movement command. For
2117 ;; instance, if the user types cw, then the command vip-forward-word will
2118 ;; call vip-execute-com to execute vip-exec-change, which eventually will
2119 ;; call vip-change to invoke the replace mode on the region.
2121 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
2122 ;; vip-d-com for later use by vip-repeat.
2123 (defun vip-execute-com (m-com val com)
2124 (let ((reg vip-use-register))
2125 ;; this is the special command `#'
2127 (vip-special-prefix-com (- com 128))
2128 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
2130 (error "%c: %s" com vip-InvalidViCommand)
2131 (funcall fn m-com com))))
2132 (if (vip-dotable-command-p com)
2133 (vip-set-destructive-command
2135 (if (memq com (list ?c ?C ?!)) (- com) com)
2140 (defun vip-repeat (arg)
2141 "Re-execute last destructive command.
2142 Use the info in vip-d-com, which has the form
2143 \(com val ch reg inserted-text command-keys\),
2144 where `com' is the command to be re-executed, `val' is the
2145 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
2146 if it exists, it is the name of the register for `com'.
2147 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
2149 (let ((save-point (point)) ; save point before repeating prev cmd
2150 ;; Pass along that we are repeating a destructive command
2151 ;; This tells vip-set-destructive-command not to update
2153 (vip-intermediate-command 'vip-repeat))
2154 (if (eq last-command 'vip-undo)
2155 ;; if the last command was vip-undo, then undo-more
2157 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
2158 ;; its prefix value is used as new prefix value for the command.
2159 (let ((m-com (car vip-d-com))
2160 (val (vip-P-val arg))
2161 (com (nth 2 vip-d-com))
2162 (reg (nth 3 vip-d-com)))
2163 (if (null val) (setq val (nth 1 vip-d-com)))
2164 (if (null m-com) (error "No previous command to repeat."))
2165 (setq vip-use-register reg)
2166 (if (nth 4 vip-d-com) ; text inserted by command
2167 (setq vip-last-insertion (nth 4 vip-d-com)
2168 vip-d-char (nth 4 vip-d-com)))
2169 (funcall m-com (cons val com))
2170 (if (and vip-keep-point-on-repeat (< save-point (point)))
2171 (goto-char save-point)) ; go back to before repeat.
2172 (if (and (eolp) (not (bolp)))
2175 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
2176 ;; If the prev cmd was rotating the command ring, this means that `.' has
2177 ;; just executed a command from that ring. So, push it on the ring again.
2178 ;; If we are just executing previous command , then don't push vip-d-com
2179 ;; because vip-d-com is not fully constructed in this case (its keys and
2180 ;; the inserted text may be nil). Besides, in this case, the command
2181 ;; executed by `.' is already on the ring.
2182 (if (eq last-command 'vip-display-current-destructive-command)
2183 (vip-push-onto-ring vip-d-com 'vip-command-ring))
2184 (vip-deactivate-mark)
2187 (defun vip-repeat-from-history ()
2188 "Repeat a destructive command from history.
2189 Doesn't change vip-command-ring in any way, so `.' will work as before
2190 executing this command.
2191 This command is supposed to be bound to a two-character Vi macro where
2192 the second character is a digit 0 to 9. The digit indicates which
2193 history command to execute. `<char>0' is equivalent to `.', `<char>1'
2194 invokes the command before that, etc."
2196 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
2197 (idx (cond (vip-this-kbd-macro
2199 (symbol-name (elt vip-this-kbd-macro 1))))
2202 (vip-d-com vip-d-com))
2204 (or (and (numberp num) (<= 0 num) (<= num 9))
2209 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
2211 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
2212 (setq num (1- num)))
2215 (vip-special-ring-rotate1 vip-command-ring 1)
2216 (setq num (1+ num)))
2220 ;; This command is invoked interactively by the key sequence #<char>
2221 (defun vip-special-prefix-com (char)
2223 (downcase-region (min vip-com-point (point))
2224 (max vip-com-point (point))))
2226 (upcase-region (min vip-com-point (point))
2227 (max vip-com-point (point))))
2229 (push-mark vip-com-point t)
2230 (vip-global-execute))
2232 (push-mark vip-com-point t)
2234 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
2235 (t (error "#%c: %s" char vip-InvalidViCommand))))
2241 "Undo previous change."
2244 (let ((modified (buffer-modified-p))
2245 (before-undo-pt (point-marker))
2246 (after-change-functions after-change-functions)
2247 undo-beg-posn undo-end-posn)
2249 ;; no need to remove this hook, since this var has scope inside a let.
2250 (add-hook 'after-change-functions
2251 '(lambda (beg end len)
2252 (setq undo-beg-posn beg
2253 undo-end-posn (or end beg))))
2257 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
2258 undo-end-posn (or undo-end-posn undo-beg-posn))
2260 (goto-char undo-beg-posn)
2262 (if (and vip-keep-point-on-undo
2263 (pos-visible-in-window-p before-undo-pt))
2265 (push-mark (point-marker) t)
2266 (vip-sit-for-short 300)
2267 (goto-char undo-end-posn)
2268 (vip-sit-for-short 300)
2269 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
2270 (> (abs (- undo-end-posn before-undo-pt)) 1))
2271 (goto-char before-undo-pt)
2272 (goto-char undo-beg-posn)))
2273 (push-mark before-undo-pt t))
2274 (if (and (eolp) (not (bolp))) (backward-char 1))
2275 (if (not modified) (set-buffer-modified-p t)))
2276 (setq this-command 'vip-undo))
2278 ;; Continue undoing previous changes.
2279 (defun vip-undo-more ()
2280 (message "undo more!")
2284 (message "No further undo information in this buffer")))
2285 (if (and (eolp) (not (bolp))) (backward-char 1))
2286 (setq this-command 'vip-undo))
2288 ;; The following two functions are used to set up undo properly.
2289 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
2290 ;; they are undone all at once.
2291 (defun vip-adjust-undo ()
2292 (let ((inhibit-quit t)
2294 (setq vip-undo-needs-adjustment nil)
2295 (if (listp buffer-undo-list)
2296 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
2298 (setq tmp2 (cdr tmp)) ; the part after mark
2300 ;; cut tail from buffer-undo-list temporarily by direct
2301 ;; manipulation with pointers in buffer-undo-list
2304 (setq buffer-undo-list (delq nil buffer-undo-list))
2305 (setq buffer-undo-list
2306 (delq vip-buffer-undo-list-mark buffer-undo-list))
2307 ;; restore tail of buffer-undo-list
2308 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
2309 (setq buffer-undo-list (delq nil buffer-undo-list))))))
2312 (defun vip-set-complex-command-for-undo ()
2313 (if (listp buffer-undo-list)
2314 (if (not vip-undo-needs-adjustment)
2315 (let ((inhibit-quit t))
2316 (setq buffer-undo-list
2317 (cons vip-buffer-undo-list-mark buffer-undo-list))
2318 (setq vip-undo-needs-adjustment t)))))
2323 (defun vip-display-current-destructive-command ()
2324 (let ((text (nth 4 vip-d-com))
2325 (keys (nth 5 vip-d-com))
2328 (setq this-command 'vip-display-current-destructive-command)
2330 (message " `.' runs %s%s"
2331 (concat "`" (vip-array-to-string keys) "'")
2332 (vip-abbreviate-string text max-text-len
2333 " inserting `" "'" " ......."))
2337 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
2338 ;; or in some other way (non-interactively).
2339 (defun vip-set-destructive-command (list)
2340 (or (eq vip-intermediate-command 'vip-repeat)
2342 (setq vip-d-com list)
2343 (setcar (nthcdr 5 vip-d-com)
2344 (vip-array-to-string (this-command-keys)))
2345 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
2347 (defun vip-prev-destructive-command (next)
2348 "Find previous destructive command in the history of destructive commands.
2349 With prefix argument, find next destructive command."
2351 (let (cmd vip-intermediate-command)
2352 (if (eq last-command 'vip-display-current-destructive-command)
2353 ;; repeated search through command history
2354 (setq vip-intermediate-command 'repeating-display-destructive-command)
2355 ;; first search through command history--set temp ring
2356 (setq vip-temp-command-ring (copy-list vip-command-ring)))
2358 (vip-special-ring-rotate1 vip-temp-command-ring 1)
2359 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
2362 (setq vip-d-com cmd))
2363 (vip-display-current-destructive-command)))
2365 (defun vip-next-destructive-command ()
2366 "Find next destructive command in the history of destructive commands."
2368 (vip-prev-destructive-command 'next))
2370 (defun vip-insert-prev-from-insertion-ring (arg)
2371 "Cycle through insertion ring in the direction of older insertions.
2372 Undoes previous insertion and inserts new.
2373 With prefix argument, cycles in the direction of newer elements.
2374 In minibuffer, this command executes whatever the invocation key is bound
2375 to in the global map, instead of cycling through the insertion ring."
2377 (let (vip-intermediate-command)
2378 (if (eq last-command 'vip-insert-from-insertion-ring)
2379 (progn ; repeated search through insertion history
2380 (setq vip-intermediate-command 'repeating-insertion-from-ring)
2381 (if (eq vip-current-state 'replace-state)
2383 (if vip-last-inserted-string-from-insertion-ring
2384 (backward-delete-char
2385 (length vip-last-inserted-string-from-insertion-ring))))
2387 ;;first search through insertion history
2388 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
2389 (setq this-command 'vip-insert-from-insertion-ring)
2390 ;; so that things will be undone properly
2391 (setq buffer-undo-list (cons nil buffer-undo-list))
2392 (setq vip-last-inserted-string-from-insertion-ring
2393 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
2395 ;; this change of vip-intermediate-command must come after
2396 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
2398 (setq vip-intermediate-command nil)
2399 (if vip-last-inserted-string-from-insertion-ring
2400 (insert vip-last-inserted-string-from-insertion-ring))
2403 (defun vip-insert-next-from-insertion-ring ()
2404 "Cycle through insertion ring in the direction of older insertions.
2405 Undo previous insertion and inserts new."
2407 (vip-insert-prev-from-insertion-ring 'next))
2410 ;; some region utilities
2412 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
2413 (defun vip-add-newline-at-eob-if-necessary ()
2416 ;; make sure all lines end with newline, unless in the minibuffer or
2417 ;; when requested otherwise (require-final-newline is nil)
2421 require-final-newline
2422 (not (vip-is-in-minibuffer)))
2425 (defun vip-yank-defun ()
2427 (copy-region-as-kill (point) (mark t)))
2429 ;; Enlarge region between BEG and END.
2430 (defun vip-enlarge-region (beg end)
2431 (or beg (setq beg end)) ; if beg is nil, set to end
2432 (or end (setq end beg)) ; if end is nil, set to beg
2435 (progn (goto-char beg) (set-mark end))
2439 (exchange-point-and-mark)
2440 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
2441 (if (not (eobp)) (beginning-of-line))
2442 (if (> beg end) (exchange-point-and-mark)))
2445 ;; Quote region by each line with a user supplied string.
2446 (defun vip-quote-region ()
2447 (setq vip-quote-string
2448 (vip-read-string-with-history
2451 'vip-quote-region-history
2453 (vip-enlarge-region (point) (mark t))
2454 (if (> (point) (mark t)) (exchange-point-and-mark))
2455 (insert vip-quote-string)
2458 (while (and (< (point) (mark t)) (bolp))
2459 (insert vip-quote-string)
2463 ;; Tells whether BEG is on the same line as END.
2464 ;; If one of the args is nil, it'll return nil.
2465 (defun vip-same-line (beg end)
2466 (let ((selective-display nil))
2467 (cond ((and beg end)
2468 ;; This 'if' is needed because Emacs treats the next empty line
2469 ;; as part of the previous line.
2470 (if (or (> beg (point-max)) (> end (point-max))) ; out of range
2472 (if (and (> end beg) (= (vip-line-pos 'start) end))
2473 (setq end (min (1+ end) (point-max))))
2474 (if (and (> beg end) (= (vip-line-pos 'start) beg))
2475 (setq beg (min (1+ beg) (point-max))))
2476 (<= (count-lines beg end) 1) ))
2482 ;; Check if the string ends with a newline.
2483 (defun vip-end-with-a-newline-p (string)
2484 (or (string= string "")
2485 (= (vip-seq-last-elt string) ?\n)))
2487 (defun vip-tmp-insert-at-eob (msg)
2488 (let ((savemax (point-max)))
2492 (goto-char savemax) (delete-region (point) (point-max))
2497 ;;; Minibuffer business
2499 (defsubst vip-set-minibuffer-style ()
2500 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
2503 (defun vip-minibuffer-setup-sentinel ()
2504 (let ((hook (if vip-vi-style-in-minibuffer
2505 'vip-change-state-to-insert
2506 'vip-change-state-to-emacs)))
2510 ;; Interpret last event in the local map
2511 (defun vip-exit-minibuffer ()
2514 (setq command (local-key-binding (char-to-string last-command-char)))
2516 (command-execute command)
2517 (exit-minibuffer))))
2520 (defun vip-set-search-face ()
2521 (if (not (vip-window-display-p))
2523 (defvar vip-search-face
2525 (make-face 'vip-search-face)
2526 (vip-hide-face 'vip-search-face)
2527 (or (face-differs-from-default-p 'vip-search-face)
2528 ;; face wasn't set in .vip or .Xdefaults
2529 (if (vip-can-use-colors "Black" "khaki")
2531 (set-face-background 'vip-search-face "khaki")
2532 (set-face-foreground 'vip-search-face "Black"))
2533 (copy-face 'italic 'vip-search-face)
2534 (set-face-underline-p 'vip-search-face t)))
2536 "*Face used to flash out the search pattern.")
2540 (defun vip-set-minibuffer-faces ()
2541 (if (not (vip-window-display-p))
2543 (defvar vip-minibuffer-emacs-face
2545 (make-face 'vip-minibuffer-emacs-face)
2546 (vip-hide-face 'vip-minibuffer-emacs-face)
2547 (or (face-differs-from-default-p 'vip-minibuffer-emacs-face)
2548 ;; face wasn't set in .vip or .Xdefaults
2549 (if vip-vi-style-in-minibuffer
2550 ;; emacs state is an exception in the minibuffer
2551 (if (vip-can-use-colors "darkseagreen2" "Black")
2553 (set-face-background
2554 'vip-minibuffer-emacs-face "darkseagreen2")
2555 (set-face-foreground
2556 'vip-minibuffer-emacs-face "Black"))
2557 (copy-face 'highlight 'vip-minibuffer-emacs-face))
2558 ;; emacs state is the main state in the minibuffer
2559 (if (vip-can-use-colors "Black" "pink")
2561 (set-face-background 'vip-minibuffer-emacs-face "pink")
2562 (set-face-foreground
2563 'vip-minibuffer-emacs-face "Black"))
2564 (copy-face 'italic 'vip-minibuffer-emacs-face))
2566 'vip-minibuffer-emacs-face)
2567 "Face used in the Minibuffer when it is in Emacs state.")
2569 (defvar vip-minibuffer-insert-face
2571 (make-face 'vip-minibuffer-insert-face)
2572 (vip-hide-face 'vip-minibuffer-insert-face)
2573 (or (face-differs-from-default-p 'vip-minibuffer-insert-face)
2574 (if vip-vi-style-in-minibuffer
2575 (if (vip-can-use-colors "Black" "pink")
2577 (set-face-background 'vip-minibuffer-insert-face "pink")
2578 (set-face-foreground
2579 'vip-minibuffer-insert-face "Black"))
2580 (copy-face 'italic 'vip-minibuffer-insert-face))
2581 ;; If Insert state is an exception
2582 (if (vip-can-use-colors "darkseagreen2" "Black")
2584 (set-face-background
2585 'vip-minibuffer-insert-face "darkseagreen2")
2586 (set-face-foreground
2587 'vip-minibuffer-insert-face "Black"))
2588 (copy-face 'highlight 'vip-minibuffer-insert-face))
2589 (vip-italicize-face 'vip-minibuffer-insert-face)))
2590 'vip-minibuffer-insert-face)
2591 "Face used in the Minibuffer when it is in Insert state.")
2593 (defvar vip-minibuffer-vi-face
2595 (make-face 'vip-minibuffer-vi-face)
2596 (vip-hide-face 'vip-minibuffer-vi-face)
2597 (or (face-differs-from-default-p 'vip-minibuffer-vi-face)
2598 (if vip-vi-style-in-minibuffer
2599 (if (vip-can-use-colors "Black" "grey")
2601 (set-face-background 'vip-minibuffer-vi-face "grey")
2602 (set-face-foreground 'vip-minibuffer-vi-face "Black"))
2603 (copy-face 'bold 'vip-minibuffer-vi-face))
2604 (copy-face 'bold 'vip-minibuffer-vi-face)
2605 (invert-face 'vip-minibuffer-vi-face)))
2606 'vip-minibuffer-vi-face)
2607 "Face used in the Minibuffer when it is in Vi state.")
2609 ;; the current face used in the minibuffer
2610 (vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "")
2615 ;;; Reading string with history
2617 (defun vip-read-string-with-history (prompt &optional initial
2618 history-var default keymap)
2619 ;; Read string, prompting with PROMPT and inserting the INITIAL
2620 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2621 ;; input is an empty string. Use KEYMAP, if given, or the
2622 ;; minibuffer-local-map.
2623 ;; Default value is displayed until the user types something in the
2625 (let ((minibuffer-setup-hook
2627 (if (stringp initial)
2629 ;; don't wait if we have unread events or in kbd macro
2630 (or unread-command-events
2635 (vip-minibuffer-setup-sentinel)))
2640 (setq keymap (or keymap minibuffer-local-map)
2641 initial (or initial "")
2642 temp-msg (if default
2643 (format "(default: %s) " default)
2646 (setq vip-incomplete-ex-cmd nil)
2647 (setq val (read-from-minibuffer prompt
2648 (concat temp-msg initial val padding)
2649 keymap nil history-var))
2650 (setq minibuffer-setup-hook nil
2651 padding (vip-array-to-string (this-command-keys))
2653 ;; the following tries to be smart about what to put in history
2654 (if (not (string= val (car (eval history-var))))
2655 (set history-var (cons val (eval history-var))))
2656 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2657 (string= (nth 0 (eval history-var)) ""))
2658 (set history-var (cdr (eval history-var))))
2659 ;; If the user enters nothing but the prev cmd wasn't vip-ex,
2660 ;; vip-command-argument, or `! shell-command', this probably means
2661 ;; that the user typed something then erased. Return "" in this case, not
2662 ;; the default---the default is too confusing in this case.
2663 (cond ((and (string= val "")
2664 (not (string= prompt "!")) ; was a `! shell-command'
2665 (not (memq last-command
2667 vip-command-argument
2671 ((string= val "") (or default ""))
2677 ;; insertion commands
2679 ;; Called when state changes from Insert Vi command mode.
2680 ;; Repeats the insertion command if Insert state was entered with prefix
2682 (defun vip-repeat-insert-command ()
2683 (let ((i-com (car vip-d-com))
2684 (val (nth 1 vip-d-com))
2685 (char (nth 2 vip-d-com)))
2686 (if (and val (> val 1)) ; first check that val is non-nil
2688 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
2690 (setq vip-d-com (list i-com val char nil nil nil))
2693 (defun vip-insert (arg)
2694 "Insert before point."
2696 (vip-set-complex-command-for-undo)
2697 (let ((val (vip-p-val arg))
2698 (com (vip-getcom arg)))
2699 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
2701 (vip-loop val (vip-yank-last-insertion))
2702 (vip-change-state-to-insert))))
2704 (defun vip-append (arg)
2705 "Append after point."
2707 (vip-set-complex-command-for-undo)
2708 (let ((val (vip-p-val arg))
2709 (com (vip-getcom arg)))
2710 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
2711 (if (not (eolp)) (forward-char))
2713 (vip-loop val (vip-yank-last-insertion))
2714 (vip-change-state-to-insert))))
2716 (defun vip-Append (arg)
2717 "Append at end of line."
2719 (vip-set-complex-command-for-undo)
2720 (let ((val (vip-p-val arg))
2721 (com (vip-getcom arg)))
2722 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
2725 (vip-loop val (vip-yank-last-insertion))
2726 (vip-change-state-to-insert))))
2728 (defun vip-Insert (arg)
2729 "Insert before first non-white."
2731 (vip-set-complex-command-for-undo)
2732 (let ((val (vip-p-val arg))
2733 (com (vip-getcom arg)))
2734 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
2735 (back-to-indentation)
2737 (vip-loop val (vip-yank-last-insertion))
2738 (vip-change-state-to-insert))))
2740 (defun vip-open-line (arg)
2743 (vip-set-complex-command-for-undo)
2744 (let ((val (vip-p-val arg))
2745 (com (vip-getcom arg)))
2746 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
2747 (let ((col (current-indentation)))
2756 (if vip-electric-mode
2757 (indent-according-to-mode)
2760 (vip-yank-last-insertion)))
2766 (if vip-electric-mode
2767 (indent-according-to-mode)
2770 (vip-change-state-to-insert)
2773 (defun vip-Open-line (arg)
2776 (vip-set-complex-command-for-undo)
2777 (let ((val (vip-p-val arg))
2778 (com (vip-getcom arg)))
2779 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
2780 (let ((col (current-indentation)))
2789 (if vip-electric-mode
2790 (indent-according-to-mode)
2793 (vip-yank-last-insertion)))
2799 (if vip-electric-mode
2800 (indent-according-to-mode)
2803 (vip-change-state-to-insert)))))
2805 (defun vip-open-line-at-point (arg)
2806 "Open line at point."
2808 (vip-set-complex-command-for-undo)
2809 (let ((val (vip-p-val arg))
2810 (com (vip-getcom arg)))
2811 (vip-set-destructive-command
2812 (list 'vip-open-line-at-point val ?r nil nil nil))
2817 (vip-yank-last-insertion)))
2819 (vip-change-state-to-insert))))
2821 (defun vip-substitute (arg)
2822 "Substitute characters."
2824 (let ((val (vip-p-val arg))
2825 (com (vip-getcom arg)))
2829 (vip-change-subr (mark t) (point))
2830 (vip-change (mark t) (point)))
2831 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
2834 (defun vip-substitute-line (arg)
2837 (vip-set-complex-command-for-undo)
2838 (vip-line (cons arg ?C)))
2840 ;; Prepare for replace
2841 (defun vip-start-replace ()
2842 (setq vip-began-as-replace t
2843 vip-sitting-in-replace t
2844 vip-replace-chars-to-delete 0
2845 vip-replace-chars-deleted 0)
2846 (vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
2847 (vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
2848 ;; this will get added repeatedly, but no harm
2849 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
2850 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
2851 (vip-move-marker-locally 'vip-last-posn-in-replace-region
2852 (vip-replace-start))
2854 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
2856 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2860 ;; checks how many chars were deleted by the last change
2861 (defun vip-replace-mode-spy-before (beg end)
2862 (setq vip-replace-chars-deleted
2864 (max 0 (- end (vip-replace-end)))
2865 (max 0 (- (vip-replace-start) beg))
2868 ;; Invoked as an after-change-function to set up parameters of the last change
2869 (defun vip-replace-mode-spy-after (beg end length)
2870 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
2872 (setq vip-replace-chars-to-delete 0)
2873 (vip-move-marker-locally
2874 'vip-last-posn-in-replace-region (point)))
2876 (let (beg-col end-col real-end chars-to-delete)
2877 (setq real-end (min end (vip-replace-end)))
2880 (setq beg-col (current-column))
2881 (goto-char real-end)
2882 (setq end-col (current-column)))
2884 ;; If beg of change is outside the replacement region, then don't
2885 ;; delete anything in the repl region (set chars-to-delete to 0).
2887 ;; This works fine except that we have to take special care of
2888 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
2889 ;; sometimes simply shifts the repl region rightwards, without
2890 ;; deleting an equal amount of characters.
2892 ;; The reason why new-dabbrev.el causes this are this:
2893 ;; if one dinamically completes a partial word that starts before the
2894 ;; replacement region (but ends inside) then new-dabbrev.el first
2895 ;; moves cursor backwards, to the beginning of the word to be
2896 ;; completed (say, pt A). Then it inserts the
2897 ;; completed word and then deletes the old, incomplete part.
2898 ;; Since the complete word is inserted at position before the repl
2899 ;; region, the next If-statement would have set chars-to-delete to 0
2900 ;; unless we check for the current command, which must be
2903 ;; In fact, it might be also useful to have overlays for insert
2904 ;; regions as well, since this will let us capture the situation when
2905 ;; dabbrev-expand goes back past the insertion point to find the
2906 ;; beginning of the word to be expanded.
2907 (if (or (and (<= (vip-replace-start) beg)
2908 (<= beg (vip-replace-end)))
2909 (and (= length 0) (eq this-command 'dabbrev-expand)))
2910 (setq chars-to-delete
2911 (max (- end-col beg-col) (- real-end beg) 0))
2912 (setq chars-to-delete 0))
2914 ;; if beg = last change position, it means that we are within the
2915 ;; same command that does multiple changes. Moreover, it means
2916 ;; that we have two subsequent changes (insert/delete) that
2917 ;; complement each other.
2918 (if (= beg (marker-position vip-last-posn-in-replace-region))
2919 (setq vip-replace-chars-to-delete
2920 (- (+ chars-to-delete vip-replace-chars-to-delete)
2921 vip-replace-chars-deleted))
2922 (setq vip-replace-chars-to-delete chars-to-delete))
2924 (vip-move-marker-locally
2925 'vip-last-posn-in-replace-region
2926 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
2927 (or (marker-position vip-last-posn-in-replace-region)
2928 (vip-replace-start))
2931 (setq vip-replace-chars-to-delete
2932 (max 0 (min vip-replace-chars-to-delete
2933 (- (vip-replace-end)
2934 vip-last-posn-in-replace-region))))
2938 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
2939 ;; posn is within the overlay.
2940 (defun vip-finish-change (posn)
2941 (vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
2942 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2943 (vip-remove-hook 'vip-post-command-hooks
2944 'vip-replace-state-post-command-sentinel)
2946 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2947 (vip-restore-cursor-color)
2948 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2952 (>= posn (vip-replace-start))
2953 (< posn (vip-replace-end)))
2954 (delete-region posn (vip-replace-end)))
2957 (if (eq vip-current-state 'replace-state)
2958 (vip-downgrade-to-insert))
2959 ;; replace mode ended => nullify vip-last-posn-in-replace-region
2960 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
2961 (vip-hide-replace-overlay)
2962 (vip-refresh-mode-line)
2963 (vip-put-string-on-kill-ring vip-last-replace-region)
2966 ;; Make STRING be the first element of the kill ring.
2967 (defun vip-put-string-on-kill-ring (string)
2968 (setq kill-ring (cons string kill-ring))
2969 (if (> (length kill-ring) kill-ring-max)
2970 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2971 (setq kill-ring-yank-pointer kill-ring))
2973 (defun vip-finish-R-mode ()
2974 (vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2976 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2977 (vip-downgrade-to-insert))
2979 (defun vip-start-R-mode ()
2980 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2983 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
2985 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2990 (defun vip-replace-state-exit-cmd ()
2991 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2992 These keys are ESC, RET, and LineFeed"
2994 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2996 (vip-finish-change vip-last-posn-in-replace-region))
2998 (if (eq this-command 'vip-intercept-ESC-key)
2999 (setq com 'vip-exit-insert-state)
3000 (vip-set-unread-command-events last-input-char)
3001 (setq com (key-binding (read-key-sequence nil))))
3003 (condition-case conds
3004 (command-execute com)
3006 (vip-message-conditions conds)))
3008 (vip-hide-replace-overlay))
3011 ;; This is the function bound to 'R'---unlimited replace.
3012 ;; Similar to Emacs's own overwrite-mode.
3013 (defun vip-overwrite (arg)
3014 "Begin overwrite mode."
3016 (let ((val (vip-p-val arg))
3017 (com (vip-getcom arg)) (len))
3018 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
3021 ;; Viper saves inserted text in vip-last-insertion
3022 (setq len (length vip-last-insertion))
3024 (vip-loop val (vip-yank-last-insertion)))
3025 (setq last-command 'vip-overwrite)
3026 (vip-set-complex-command-for-undo)
3027 (vip-set-replace-overlay (point) (vip-line-pos 'end))
3028 (vip-change-state-to-replace)
3034 (defun vip-line (arg)
3035 (let ((val (car arg))
3037 (vip-move-marker-locally 'vip-com-point (point))
3039 (vip-next-line-carefully (1- val)))
3040 ;; this ensures that dd, cc, D, yy will do the right thing on the last
3041 ;; line of buffer when this line has no \n.
3042 (vip-add-newline-at-eob-if-necessary)
3043 (vip-execute-com 'vip-line val com))
3044 (if (and (eobp) (not (bobp))) (forward-line -1))
3047 (defun vip-yank-line (arg)
3048 "Yank ARG lines (in Vi's sense)."
3050 (let ((val (vip-p-val arg)))
3051 (vip-line (cons val ?Y))))
3056 (defun vip-region (arg)
3057 "Execute command on a region."
3059 (let ((val (vip-P-val arg))
3060 (com (vip-getcom arg)))
3061 (vip-move-marker-locally 'vip-com-point (point))
3062 (exchange-point-and-mark)
3063 (vip-execute-com 'vip-region val com)))
3065 (defun vip-Region (arg)
3066 "Execute command on a Region."
3068 (let ((val (vip-P-val arg))
3069 (com (vip-getCom arg)))
3070 (vip-move-marker-locally 'vip-com-point (point))
3071 (exchange-point-and-mark)
3072 (vip-execute-com 'vip-Region val com)))
3074 (defun vip-replace-char (arg)
3075 "Replace the following ARG chars by the character read."
3077 (if (and (eolp) (bolp)) (error "No character to replace here"))
3078 (let ((val (vip-p-val arg))
3079 (com (vip-getcom arg)))
3080 (vip-replace-char-subr com val)
3081 (if (and (eolp) (not (bolp))) (forward-char 1))
3082 (vip-set-destructive-command
3083 (list 'vip-replace-char val ?r nil vip-d-char nil))
3086 (defun vip-replace-char-subr (com arg)
3087 (let ((take-care-of-iso-accents
3088 (and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
3090 (setq char (if (equal com ?r)
3093 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
3094 ;; get European characters
3096 (iso-accents-mode 1)
3097 (vip-set-unread-command-events char)
3098 (setq char (aref (read-key-sequence nil) 0))
3099 (iso-accents-mode -1)))
3101 (setq vip-d-char char)
3102 (vip-loop (if (> arg 0) arg (- arg))
3103 (if (eq char ?\C-m) (insert "\n") (insert char)))
3104 (backward-char arg)))
3107 ;; basic cursor movement. j, k, l, h commands.
3109 (defun vip-forward-char (arg)
3110 "Move point right ARG characters (left if ARG negative).
3111 On reaching end of line, stop and signal error."
3113 (vip-leave-region-active)
3114 (let ((val (vip-p-val arg))
3115 (com (vip-getcom arg)))
3116 (if com (vip-move-marker-locally 'vip-com-point (point)))
3117 (if vip-ex-style-motion
3119 ;; the boundary condition check gets weird here because
3120 ;; forward-char may be the parameter of a delete, and 'dl' works
3121 ;; just like 'x' for the last char on a line, so we have to allow
3122 ;; the forward motion before the 'vip-execute-com', but, of
3123 ;; course, 'dl' doesn't work on an empty line, so we have to
3124 ;; catch that condition before 'vip-execute-com'
3125 (if (and (eolp) (bolp)) (error "") (forward-char val))
3126 (if com (vip-execute-com 'vip-forward-char val com))
3127 (if (eolp) (progn (backward-char 1) (error ""))))
3129 (if com (vip-execute-com 'vip-forward-char val com)))))
3131 (defun vip-backward-char (arg)
3132 "Move point left ARG characters (right if ARG negative).
3133 On reaching beginning of line, stop and signal error."
3135 (vip-leave-region-active)
3136 (let ((val (vip-p-val arg))
3137 (com (vip-getcom arg)))
3138 (if com (vip-move-marker-locally 'vip-com-point (point)))
3139 (if vip-ex-style-motion
3141 (if (bolp) (error "") (backward-char val))
3142 (if com (vip-execute-com 'vip-backward-char val com)))
3144 (if com (vip-execute-com 'vip-backward-char val com)))))
3146 ;; Like forward-char, but doesn't move at end of buffer.
3147 (defun vip-forward-char-carefully (&optional arg)
3148 (setq arg (or arg 1))
3149 (if (>= (point-max) (+ (point) arg))
3151 (goto-char (point-max))))
3153 ;; Like backward-char, but doesn't move at end of buffer.
3154 (defun vip-backward-char-carefully (&optional arg)
3155 (setq arg (or arg 1))
3156 (if (<= (point-min) (- (point) arg))
3158 (goto-char (point-min))))
3160 (defun vip-next-line-carefully (arg)
3169 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
3170 ;; for word movement. When executed with a destructive command, \n is
3171 ;; usually left untouched for the last word.
3172 ;; Viper uses syntax table to determine what is a word and what is a
3173 ;; separator. However, \n is always a separator. Also, if vip-syntax-preference
3174 ;; is 'vi, then `_' is part of the word.
3177 (defun vip-skip-separators (forward)
3180 (vip-skip-all-separators-forward 'within-line)
3181 (if (looking-at "\n")
3184 (vip-skip-all-separators-forward 'within-line))))
3185 (vip-skip-all-separators-backward 'within-line)
3187 (if (looking-at "\n")
3188 (vip-skip-all-separators-backward 'within-line)
3191 (defun vip-forward-word-kernel (val)
3193 (cond ((vip-looking-at-alpha)
3194 (vip-skip-alpha-forward "_")
3195 (vip-skip-separators t))
3196 ((vip-looking-at-separator)
3197 (vip-skip-separators t))
3198 ((not (vip-looking-at-alphasep))
3199 (vip-skip-nonalphasep-forward)
3200 (vip-skip-separators t)))
3201 (setq val (1- val))))
3203 ;; first search backward for pat. Then skip chars backwards using aux-pat
3204 (defun vip-fwd-skip (pat aux-pat lim)
3205 (if (and (save-excursion
3206 (re-search-backward pat lim t))
3207 (= (point) (match-end 0)))
3208 (goto-char (match-beginning 0)))
3209 (skip-chars-backward aux-pat lim)
3211 (vip-forward-char-carefully))
3215 (defun vip-forward-word (arg)
3218 (vip-leave-region-active)
3219 (let ((val (vip-p-val arg))
3220 (com (vip-getcom arg)))
3221 (if com (vip-move-marker-locally 'vip-com-point (point)))
3222 (vip-forward-word-kernel val)
3224 (cond ((memq com (list ?c (- ?c)))
3225 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3226 ;; Yank words including the whitespace, but not newline
3227 ((memq com (list ?y (- ?y)))
3228 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3229 ((vip-dotable-command-p com)
3230 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3231 (vip-execute-com 'vip-forward-word val com)))))
3234 (defun vip-forward-Word (arg)
3235 "Forward word delimited by white characters."
3237 (vip-leave-region-active)
3238 (let ((val (vip-p-val arg))
3239 (com (vip-getcom arg)))
3240 (if com (vip-move-marker-locally 'vip-com-point (point)))
3243 (vip-skip-nonseparators 'forward)
3244 (vip-skip-separators t)))
3246 (cond ((memq com (list ?c (- ?c)))
3247 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3248 ;; Yank words including the whitespace, but not newline
3249 ((memq com (list ?y (- ?y)))
3250 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3251 ((vip-dotable-command-p com)
3252 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3253 (vip-execute-com 'vip-forward-Word val com)))))
3256 ;; this is a bit different from Vi, but Vi's end of word
3257 ;; makes no sense whatsoever
3258 (defun vip-end-of-word-kernel ()
3259 (if (vip-end-of-word-p) (forward-char))
3260 (if (vip-looking-at-separator)
3261 (vip-skip-all-separators-forward))
3263 (cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
3264 ((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
3265 (vip-backward-char-carefully))
3267 (defun vip-end-of-word-p ()
3270 (cond ((vip-looking-at-alpha)
3272 (not (vip-looking-at-alpha)))
3273 ((not (vip-looking-at-alphasep))
3275 (vip-looking-at-alphasep))))))
3278 (defun vip-end-of-word (arg &optional careful)
3279 "Move point to end of current word."
3281 (vip-leave-region-active)
3282 (let ((val (vip-p-val arg))
3283 (com (vip-getcom arg)))
3284 (if com (vip-move-marker-locally 'vip-com-point (point)))
3285 (vip-loop val (vip-end-of-word-kernel))
3289 (vip-execute-com 'vip-end-of-word val com)))))
3291 (defun vip-end-of-Word (arg)
3292 "Forward to end of word delimited by white character."
3294 (vip-leave-region-active)
3295 (let ((val (vip-p-val arg))
3296 (com (vip-getcom arg)))
3297 (if com (vip-move-marker-locally 'vip-com-point (point)))
3300 (vip-end-of-word-kernel)
3301 (vip-skip-nonseparators 'forward)
3306 (vip-execute-com 'vip-end-of-Word val com)))))
3308 (defun vip-backward-word-kernel (val)
3311 (cond ((vip-looking-at-alpha)
3312 (vip-skip-alpha-backward "_"))
3313 ((vip-looking-at-separator)
3315 (vip-skip-separators nil)
3317 (cond ((vip-looking-at-alpha)
3318 (vip-skip-alpha-backward "_"))
3319 ((not (vip-looking-at-alphasep))
3320 (vip-skip-nonalphasep-backward))
3321 (t (forward-char))))
3322 ((not (vip-looking-at-alphasep))
3323 (vip-skip-nonalphasep-backward)))
3324 (setq val (1- val))))
3326 (defun vip-backward-word (arg)
3329 (vip-leave-region-active)
3330 (let ((val (vip-p-val arg))
3331 (com (vip-getcom arg)))
3334 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3336 (vip-move-marker-locally 'vip-com-point (point))
3337 (if i (forward-char))))
3338 (vip-backward-word-kernel val)
3339 (if com (vip-execute-com 'vip-backward-word val com))))
3341 (defun vip-backward-Word (arg)
3342 "Backward word delimited by white character."
3344 (vip-leave-region-active)
3345 (let ((val (vip-p-val arg))
3346 (com (vip-getcom arg)))
3349 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3351 (vip-move-marker-locally 'vip-com-point (point))
3352 (if i (forward-char))))
3355 (vip-skip-separators nil)
3356 (vip-skip-nonseparators 'backward)))
3357 (if com (vip-execute-com 'vip-backward-Word val com))))
3363 (defun vip-beginning-of-line (arg)
3364 "Go to beginning of line."
3366 (vip-leave-region-active)
3367 (let ((val (vip-p-val arg))
3368 (com (vip-getcom arg)))
3369 (if com (vip-move-marker-locally 'vip-com-point (point)))
3370 (beginning-of-line val)
3371 (if com (vip-execute-com 'vip-beginning-of-line val com))))
3373 (defun vip-bol-and-skip-white (arg)
3374 "Beginning of line at first non-white character."
3376 (vip-leave-region-active)
3377 (let ((val (vip-p-val arg))
3378 (com (vip-getcom arg)))
3379 (if com (vip-move-marker-locally 'vip-com-point (point)))
3380 (forward-to-indentation (1- val))
3381 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
3383 (defun vip-goto-eol (arg)
3384 "Go to end of line."
3386 (vip-leave-region-active)
3387 (let ((val (vip-p-val arg))
3388 (com (vip-getcom arg)))
3389 (if com (vip-move-marker-locally 'vip-com-point (point)))
3391 (if com (vip-execute-com 'vip-goto-eol val com))
3392 (if vip-ex-style-motion
3393 (if (and (eolp) (not (bolp))
3394 ;; a fix for vip-change-to-eol
3395 (not (equal vip-current-state 'insert-state)))
3400 (defun vip-goto-col (arg)
3401 "Go to ARG's column."
3403 (vip-leave-region-active)
3404 (let ((val (vip-p-val arg))
3405 (com (vip-getcom arg)))
3408 (if (> val (1+ (current-column))) (error "")))
3409 (if com (vip-move-marker-locally 'vip-com-point (point)))
3411 (forward-char (1- val))
3412 (if com (vip-execute-com 'vip-goto-col val com))))
3415 (defun vip-next-line (arg)
3418 (vip-leave-region-active)
3419 (let ((val (vip-p-val arg))
3420 (com (vip-getCom arg)))
3421 (if com (vip-move-marker-locally 'vip-com-point (point)))
3423 (if vip-ex-style-motion
3424 (if (and (eolp) (not (bolp))) (backward-char 1)))
3425 (setq this-command 'next-line)
3426 (if com (vip-execute-com 'vip-next-line val com))))
3428 (defun vip-next-line-at-bol (arg)
3429 "Next line at beginning of line."
3431 (vip-leave-region-active)
3434 (if (eobp) (error "Last line in buffer")))
3435 (let ((val (vip-p-val arg))
3436 (com (vip-getCom arg)))
3437 (if com (vip-move-marker-locally 'vip-com-point (point)))
3439 (back-to-indentation)
3440 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
3442 (defun vip-previous-line (arg)
3443 "Go to previous line."
3445 (vip-leave-region-active)
3446 (let ((val (vip-p-val arg))
3447 (com (vip-getCom arg)))
3448 (if com (vip-move-marker-locally 'vip-com-point (point)))
3450 (if vip-ex-style-motion
3451 (if (and (eolp) (not (bolp))) (backward-char 1)))
3452 (setq this-command 'previous-line)
3453 (if com (vip-execute-com 'vip-previous-line val com))))
3456 (defun vip-previous-line-at-bol (arg)
3457 "Previous line at beginning of line."
3459 (vip-leave-region-active)
3462 (if (bobp) (error "First line in buffer")))
3463 (let ((val (vip-p-val arg))
3464 (com (vip-getCom arg)))
3465 (if com (vip-move-marker-locally 'vip-com-point (point)))
3466 (forward-line (- val))
3467 (back-to-indentation)
3468 (if com (vip-execute-com 'vip-previous-line val com))))
3470 (defun vip-change-to-eol (arg)
3471 "Change to end of line."
3473 (vip-goto-eol (cons arg ?c)))
3475 (defun vip-kill-line (arg)
3478 (vip-goto-eol (cons arg ?d)))
3480 (defun vip-erase-line (arg)
3483 (vip-beginning-of-line (cons arg ?d)))
3488 (defun vip-goto-line (arg)
3489 "Go to ARG's line. Without ARG go to end of buffer."
3491 (let ((val (vip-P-val arg))
3492 (com (vip-getCom arg)))
3493 (vip-move-marker-locally 'vip-com-point (point))
3494 (vip-deactivate-mark)
3497 (goto-char (point-max))
3498 (goto-char (point-min))
3499 (forward-line (1- val)))
3501 ;; positioning is done twice: before and after command execution
3502 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3503 (back-to-indentation)
3505 (if com (vip-execute-com 'vip-goto-line val com))
3507 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3508 (back-to-indentation)
3511 ;; Find ARG's occurrence of CHAR on the current line.
3512 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
3513 ;; adjust point after search.
3514 (defun vip-find-char (arg char forward offset)
3515 (or (char-or-string-p char) (error ""))
3516 (let ((arg (if forward arg (- arg)))
3517 (cmd (if (eq vip-intermediate-command 'vip-repeat)
3519 (vip-array-to-string (this-command-keys))))
3525 ;; forward search begins here
3526 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
3527 ;; forward search ends here
3528 (progn (end-of-line) (point)))
3530 ;; backward search begins from here
3532 (error "Command `%s': At beginning of line" cmd) (point))
3533 ;; backward search ends here
3534 (progn (beginning-of-line) (point))))
3535 ;; if arg > 0, point is forwarded before search.
3536 (if (> arg 0) (goto-char (1+ (point-min)))
3537 (goto-char (point-max)))
3538 (if (let ((case-fold-search nil))
3539 (search-forward (char-to-string char) nil 0 arg))
3540 (setq point (point))
3541 (error "Command `%s': `%c' not found" cmd char))))
3542 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
3544 (defun vip-find-char-forward (arg)
3545 "Find char on the line.
3546 If called interactively read the char to find from the terminal, and if
3547 called from vip-repeat, the char last used is used. This behaviour is
3548 controlled by the sign of prefix numeric value."
3550 (let ((val (vip-p-val arg))
3551 (com (vip-getcom arg)))
3553 ;; this means that the function was called interactively
3554 (setq vip-f-char (read-char)
3557 ;; vip-repeat --- set vip-F-char from command-keys
3558 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3559 (vip-seq-last-elt (nth 5 vip-d-com))
3561 vip-f-char vip-F-char)
3563 (if com (vip-move-marker-locally 'vip-com-point (point)))
3564 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
3568 (setq vip-F-char vip-f-char) ; set new vip-F-char
3570 (vip-execute-com 'vip-find-char-forward val com)))))
3572 (defun vip-goto-char-forward (arg)
3573 "Go up to char ARG forward on line."
3575 (let ((val (vip-p-val arg))
3576 (com (vip-getcom arg)))
3578 ;; this means that the function was called interactively
3579 (setq vip-f-char (read-char)
3582 ;; vip-repeat --- set vip-F-char from command-keys
3583 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3584 (vip-seq-last-elt (nth 5 vip-d-com))
3586 vip-f-char vip-F-char)
3588 (if com (vip-move-marker-locally 'vip-com-point (point)))
3589 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
3593 (setq vip-F-char vip-f-char) ; set new vip-F-char
3595 (vip-execute-com 'vip-goto-char-forward val com)))))
3597 (defun vip-find-char-backward (arg)
3598 "Find char ARG on line backward."
3600 (let ((val (vip-p-val arg))
3601 (com (vip-getcom arg)))
3603 ;; this means that the function was called interactively
3604 (setq vip-f-char (read-char)
3607 ;; vip-repeat --- set vip-F-char from command-keys
3608 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3609 (vip-seq-last-elt (nth 5 vip-d-com))
3611 vip-f-char vip-F-char)
3613 (if com (vip-move-marker-locally 'vip-com-point (point)))
3615 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
3619 (setq vip-F-char vip-f-char) ; set new vip-F-char
3620 (vip-execute-com 'vip-find-char-backward val com)))))
3622 (defun vip-goto-char-backward (arg)
3623 "Go up to char ARG backward on line."
3625 (let ((val (vip-p-val arg))
3626 (com (vip-getcom arg)))
3628 ;; this means that the function was called interactively
3629 (setq vip-f-char (read-char)
3632 ;; vip-repeat --- set vip-F-char from command-keys
3633 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3634 (vip-seq-last-elt (nth 5 vip-d-com))
3636 vip-f-char vip-F-char)
3638 (if com (vip-move-marker-locally 'vip-com-point (point)))
3639 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
3643 (setq vip-F-char vip-f-char) ; set new vip-F-char
3644 (vip-execute-com 'vip-goto-char-backward val com)))))
3646 (defun vip-repeat-find (arg)
3647 "Repeat previous find command."
3649 (let ((val (vip-p-val arg))
3650 (com (vip-getcom arg)))
3651 (vip-deactivate-mark)
3652 (if com (vip-move-marker-locally 'vip-com-point (point)))
3653 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
3656 (if vip-f-forward (forward-char))
3657 (vip-execute-com 'vip-repeat-find val com)))))
3659 (defun vip-repeat-find-opposite (arg)
3660 "Repeat previous find command in the opposite direction."
3662 (let ((val (vip-p-val arg))
3663 (com (vip-getcom arg)))
3664 (vip-deactivate-mark)
3665 (if com (vip-move-marker-locally 'vip-com-point (point)))
3666 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
3669 (if vip-f-forward (forward-char))
3670 (vip-execute-com 'vip-repeat-find-opposite val com)))))
3673 ;; window scrolling etc.
3675 (defun vip-other-window (arg)
3676 "Switch to other window."
3679 (or (not (eq vip-current-state 'emacs-state))
3680 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
3681 (vip-change-state-to-vi)))
3683 (defun vip-window-top (arg)
3684 "Go to home window line."
3686 (let ((val (vip-p-val arg))
3687 (com (vip-getCom arg)))
3688 (if com (vip-move-marker-locally 'vip-com-point (point)))
3690 (move-to-window-line (1- val))
3692 ;; positioning is done twice: before and after command execution
3693 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3694 (back-to-indentation)
3696 (if com (vip-execute-com 'vip-window-top val com))
3698 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3699 (back-to-indentation)
3702 (defun vip-window-middle (arg)
3703 "Go to middle window line."
3705 (let ((val (vip-p-val arg))
3706 (com (vip-getCom arg))
3708 (if com (vip-move-marker-locally 'vip-com-point (point)))
3710 (if (not (pos-visible-in-window-p (point-max)))
3711 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3712 (setq lines (count-lines (window-start) (point-max)))
3713 (move-to-window-line (+ (/ lines 2) (1- val))))
3715 ;; positioning is done twice: before and after command execution
3716 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3717 (back-to-indentation)
3719 (if com (vip-execute-com 'vip-window-middle val com))
3721 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3722 (back-to-indentation)
3725 (defun vip-window-bottom (arg)
3726 "Go to last window line."
3728 (let ((val (vip-p-val arg))
3729 (com (vip-getCom arg)))
3730 (if com (vip-move-marker-locally 'vip-com-point (point)))
3732 (move-to-window-line (- val))
3734 ;; positioning is done twice: before and after command execution
3735 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3736 (back-to-indentation)
3738 (if com (vip-execute-com 'vip-window-bottom val com))
3740 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3741 (back-to-indentation)
3744 (defun vip-line-to-top (arg)
3745 "Put current line on the home line."
3747 (recenter (1- arg)))
3749 (defun vip-line-to-middle (arg)
3750 "Put current line on the middle line."
3752 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3754 (defun vip-line-to-bottom (arg)
3755 "Put current line on the last line."
3757 (recenter (- (window-height) (1+ arg))))
3761 ;; must correct this to only match ( to ) etc. On the other hand
3762 ;; it is good that paren match gets confused, because that way you
3763 ;; catch _all_ imbalances.
3765 (defun vip-paren-match (arg)
3766 "Go to the matching parenthesis."
3768 (let ((com (vip-getcom arg)))
3770 (if (or (> arg 99) (< arg 1))
3771 (error "Prefix must be between 1 and 99")
3773 (if (> (point-max) 80000)
3774 (* (/ (point-max) 100) arg)
3775 (/ (* (point-max) arg) 100)))
3776 (back-to-indentation))
3778 (if (and (eolp) (not (bolp))) (forward-char -1))
3782 (if (re-search-forward "[][(){}]" lim t)
3784 (error "No matching character on line")))
3785 (cond ((looking-at "[\(\[{]")
3786 (if com (vip-move-marker-locally 'vip-com-point (point)))
3789 (vip-execute-com 'vip-paren-match nil com)
3791 ((looking-at "[])}]")
3793 (if com (vip-move-marker-locally 'vip-com-point (point)))
3795 (if com (vip-execute-com 'vip-paren-match nil com)))
3799 ;; sentence ,paragraph and heading
3801 (defun vip-forward-sentence (arg)
3805 (let ((val (vip-p-val arg))
3806 (com (vip-getcom arg)))
3807 (if com (vip-move-marker-locally 'vip-com-point (point)))
3808 (forward-sentence val)
3809 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3811 (defun vip-backward-sentence (arg)
3812 "Backward sentence."
3815 (let ((val (vip-p-val arg))
3816 (com (vip-getcom arg)))
3817 (if com (vip-move-marker-locally 'vip-com-point (point)))
3818 (backward-sentence val)
3819 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3821 (defun vip-forward-paragraph (arg)
3822 "Forward paragraph."
3825 (let ((val (vip-p-val arg))
3826 (com (vip-getCom arg)))
3827 (if com (vip-move-marker-locally 'vip-com-point (point)))
3828 (forward-paragraph val)
3829 (if com (vip-execute-com 'vip-forward-paragraph nil com))))
3831 (defun vip-backward-paragraph (arg)
3832 "Backward paragraph."
3835 (let ((val (vip-p-val arg))
3836 (com (vip-getCom arg)))
3837 (if com (vip-move-marker-locally 'vip-com-point (point)))
3838 (backward-paragraph val)
3839 (if com (vip-execute-com 'vip-backward-paragraph nil com))))
3841 ;; should be mode-specific etc.
3843 (defun vip-prev-heading (arg)
3845 (let ((val (vip-p-val arg))
3846 (com (vip-getCom arg)))
3847 (if com (vip-move-marker-locally 'vip-com-point (point)))
3848 (re-search-backward vip-heading-start nil t val)
3849 (goto-char (match-beginning 0))
3850 (if com (vip-execute-com 'vip-prev-heading nil com))))
3852 (defun vip-heading-end (arg)
3854 (let ((val (vip-p-val arg))
3855 (com (vip-getCom arg)))
3856 (if com (vip-move-marker-locally 'vip-com-point (point)))
3857 (re-search-forward vip-heading-end nil t val)
3858 (goto-char (match-beginning 0))
3859 (if com (vip-execute-com 'vip-heading-end nil com))))
3861 (defun vip-next-heading (arg)
3863 (let ((val (vip-p-val arg))
3864 (com (vip-getCom arg)))
3865 (if com (vip-move-marker-locally 'vip-com-point (point)))
3867 (re-search-forward vip-heading-start nil t val)
3868 (goto-char (match-beginning 0))
3869 (if com (vip-execute-com 'vip-next-heading nil com))))
3874 (setq scroll-step 1)
3876 (defun vip-scroll (arg)
3877 "Scroll to next screen."
3882 (setq arg (1- arg)))
3885 (setq arg (1+ arg)))))
3887 (defun vip-scroll-back (arg)
3888 "Scroll to previous screen."
3890 (vip-scroll (- arg)))
3892 (defun vip-scroll-down (arg)
3893 "Pull down half screen."
3897 (scroll-down (/ (window-height) 2))
3900 (message "Beginning of buffer")
3901 (goto-char (point-min)))))
3903 (defun vip-scroll-down-one (arg)
3904 "Scroll up one line."
3908 (defun vip-scroll-up (arg)
3909 "Pull up half screen."
3913 (scroll-up (/ (window-height) 2))
3916 (message "End of buffer")
3917 (goto-char (point-max)))))
3919 (defun vip-scroll-up-one (arg)
3920 "Scroll down one line."
3927 (defun vip-if-string (prompt)
3928 (let ((s (vip-read-string-with-history
3932 (car vip-search-history))))
3933 (if (not (string= s ""))
3934 (setq vip-s-string s))))
3937 (defun vip-toggle-search-style (arg)
3938 "Toggle the value of vip-case-fold-search/vip-re-search.
3939 Without prefix argument, will ask which search style to toggle. With prefix
3940 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
3942 Although this function is bound to \\[vip-toggle-search-style], the most
3943 convenient way to use it is to bind `//' to the macro
3944 `1 M-x vip-toggle-search-style' and `///' to
3945 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
3946 toggle case-fold-search and hitting `/' three times witth toggle regexp
3947 search. Macros are more convenient in this case because they don't affect
3948 the Emacs binding of `/'."
3951 (cond ((or (eq arg 1)
3953 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3954 (if vip-case-fold-search
3955 "case-insensitive" "case-sensitive")
3956 (if vip-case-fold-search
3958 "case-insensitive")))))
3959 (setq vip-case-fold-search (null vip-case-fold-search))
3960 (if vip-case-fold-search
3961 (setq msg "Search becomes case-insensitive")
3962 (setq msg "Search becomes case-sensitive")))
3965 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3967 "regexp-search" "vanilla-search")
3970 "regexp-search")))))
3971 (setq vip-re-search (null vip-re-search))
3973 (setq msg "Search becomes regexp-style")
3974 (setq msg "Search becomes vanilla-style")))
3976 (setq msg "Search style remains unchanged")))
3980 (defun vip-search-forward (arg)
3981 "Search a string forward.
3982 ARG is used to find the ARG's occurrence of the string.
3983 Null string will repeat previous search."
3985 (let ((val (vip-P-val arg))
3986 (com (vip-getcom arg))
3987 (old-str vip-s-string))
3988 (setq vip-s-forward t)
3990 ;; this is not used at present, but may be used later
3991 (if (or (not (equal old-str vip-s-string))
3992 (not (markerp vip-local-search-start-marker))
3993 (not (marker-buffer vip-local-search-start-marker)))
3994 (setq vip-local-search-start-marker (point-marker)))
3995 (vip-search vip-s-string t val)
3998 (vip-move-marker-locally 'vip-com-point (mark t))
3999 (vip-execute-com 'vip-search-next val com)))))
4001 (defun vip-search-backward (arg)
4002 "Search a string backward.
4003 ARG is used to find the ARG's occurrence of the string.
4004 Null string will repeat previous search."
4006 (let ((val (vip-P-val arg))
4007 (com (vip-getcom arg))
4008 (old-str vip-s-string))
4009 (setq vip-s-forward nil)
4011 ;; this is not used at present, but may be used later
4012 (if (or (not (equal old-str vip-s-string))
4013 (not (markerp vip-local-search-start-marker))
4014 (not (marker-buffer vip-local-search-start-marker)))
4015 (setq vip-local-search-start-marker (point-marker)))
4016 (vip-search vip-s-string nil val)
4019 (vip-move-marker-locally 'vip-com-point (mark t))
4020 (vip-execute-com 'vip-search-next val com)))))
4023 ;; Search for COUNT's occurrence of STRING.
4024 ;; Search is forward if FORWARD is non-nil, otherwise backward.
4025 ;; INIT-POINT is the position where search is to start.
4027 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
4028 (defun vip-search (string forward arg
4029 &optional no-offset init-point fail-if-not-found)
4030 (if (not (equal string ""))
4031 (let ((val (vip-p-val arg))
4032 (com (vip-getcom arg))
4033 (offset (not no-offset))
4034 (case-fold-search vip-case-fold-search)
4035 (start-point (or init-point (point))))
4036 (vip-deactivate-mark)
4040 (if offset (vip-forward-char-carefully))
4043 (re-search-forward string nil nil val)
4044 (re-search-backward string))
4045 (search-forward string nil nil val)
4046 (search-backward string))
4047 ;; don't wait and don't flash in macros
4048 (or executing-kbd-macro
4049 (vip-flash-search-pattern))
4050 (if (not (equal start-point (point)))
4051 (push-mark start-point t)))
4053 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4055 (message "Search wrapped around end of buffer")
4056 (goto-char (point-min))
4057 (vip-search string forward (cons 1 com) t start-point 'fail)
4058 ;; don't wait in macros
4059 (or executing-kbd-macro (sit-for 2))
4060 ;; delete the wrap-around message
4063 (goto-char start-point)
4064 (error "`%s': %s not found"
4066 (if vip-re-search "Pattern" "String"))
4072 (re-search-backward string nil nil val)
4073 (search-backward string nil nil val))
4074 ;; don't wait and don't flash in macros
4075 (or executing-kbd-macro
4076 (vip-flash-search-pattern))
4077 (if (not (equal start-point (point)))
4078 (push-mark start-point t)))
4080 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4082 (message "Search wrapped around beginning of buffer")
4083 (goto-char (point-max))
4084 (vip-search string forward (cons 1 com) t start-point 'fail)
4085 ;; don't wait in macros
4086 (or executing-kbd-macro (sit-for 2))
4087 ;; delete the wrap-around message
4090 (goto-char start-point)
4091 (error "`%s': %s not found"
4093 (if vip-re-search "Pattern" "String"))
4096 (defun vip-search-next (arg)
4097 "Repeat previous search."
4099 (let ((val (vip-p-val arg))
4100 (com (vip-getcom arg)))
4101 (if (null vip-s-string) (error vip-NoPrevSearch))
4102 (vip-search vip-s-string vip-s-forward arg)
4105 (vip-move-marker-locally 'vip-com-point (mark t))
4106 (vip-execute-com 'vip-search-next val com)))))
4108 (defun vip-search-Next (arg)
4109 "Repeat previous search in the reverse direction."
4111 (let ((val (vip-p-val arg))
4112 (com (vip-getcom arg)))
4113 (if (null vip-s-string) (error vip-NoPrevSearch))
4114 (vip-search vip-s-string (not vip-s-forward) arg)
4117 (vip-move-marker-locally 'vip-com-point (mark t))
4118 (vip-execute-com 'vip-search-Next val com)))))
4121 ;; Search contents of buffer defined by one of Viper's motion commands.
4122 ;; Repeatable via `n' and `N'.
4123 (defun vip-buffer-search-enable (&optional c)
4124 (cond (c (setq vip-buffer-search-char c))
4125 ((null vip-buffer-search-char)
4126 (setq vip-buffer-search-char ?g)))
4127 (define-key vip-vi-basic-map
4128 (char-to-string vip-buffer-search-char) 'vip-command-argument)
4129 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
4130 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
4132 ;; This is a Viper wraper for isearch-forward.
4133 (defun vip-isearch-forward (arg)
4134 "Do incremental search forward."
4136 ;; emacs bug workaround
4137 (if (listp arg) (setq arg (car arg)))
4138 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
4140 ;; This is a Viper wraper for isearch-backward."
4141 (defun vip-isearch-backward (arg)
4142 "Do incremental search backward."
4144 ;; emacs bug workaround
4145 (if (listp arg) (setq arg (car arg)))
4146 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
4149 ;; visiting and killing files, buffers
4151 (defun vip-switch-to-buffer ()
4152 "Switch to buffer in the current window."
4157 (format "Switch to buffer in this window \(%s\): "
4158 (buffer-name (other-buffer (current-buffer))))))
4159 (switch-to-buffer buffer)
4162 (defun vip-switch-to-buffer-other-window ()
4163 "Switch to buffer in another window."
4168 (format "Switch to buffer in another window \(%s\): "
4169 (buffer-name (other-buffer (current-buffer))))))
4170 (switch-to-buffer-other-window buffer)
4173 (defun vip-kill-buffer ()
4176 (let (buffer buffer-name)
4179 (format "Kill buffer \(%s\): "
4180 (buffer-name (current-buffer)))))
4182 (if (null buffer-name)
4184 (get-buffer buffer-name)))
4185 (if (null buffer) (error "`%s': No such buffer" buffer-name))
4186 (if (or (not (buffer-modified-p buffer))
4189 "Buffer `%s' is modified, are you sure you want to kill it? "
4191 (kill-buffer buffer)
4192 (error "Buffer not killed"))))
4195 (defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p")
4196 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
4197 This is useful when you the current directory contains files with the same
4198 prefix and many different suffixes. Usually, only one of the suffixes
4199 represents an editable file. However, file completion will stop at the `.'
4200 The smart suffix feature lets you hit RET in such a case, and Viper will
4201 select the appropriate suffix.
4203 Suffixes are tried in the order given and the first suffix for which a
4204 corresponding file exists is selected. If no file exists for any of the
4205 suffixes, the user is asked to confirm.
4207 To turn this feature off, set this variable to nil.")
4209 ;; Try to add suffix to files ending with a `.'
4210 ;; Useful when the user hits RET on a non-completed file name.
4211 (defun vip-file-add-suffix ()
4213 (len (length vip-smart-suffix-list))
4214 (file (buffer-string))
4216 (goto-char (point-max))
4217 (if (and vip-smart-suffix-list (string-match "\\.$" file))
4219 (while (and (not found) (< count len))
4220 (setq suff (nth count vip-smart-suffix-list)
4222 (if (file-exists-p (format "%s%s" file suff))
4229 (vip-tmp-insert-at-eob " [Please complete file name]")
4231 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
4233 (key-binding (setq key (read-key-sequence nil))))
4234 (cond ((eq cmd 'self-insert-command)
4236 (insert (events-to-keys key))
4238 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
4240 (t (command-execute cmd)))
4246 ;; Advice for use in find-file and read-file-name commands.
4247 (defadvice exit-minibuffer (before vip-exit-minibuffer-advice activate)
4248 "Run `vip-minibuffer-exit-hook' just before exiting the minibuffer."
4249 (run-hooks 'vip-minibuffer-exit-hook))
4251 (defadvice find-file (before vip-add-suffix-advice activate)
4252 "Use `read-file-name' for reading arguments."
4253 (interactive (list (read-file-name "Find file: "
4254 nil default-directory))))
4256 (defadvice find-file-other-window (before vip-add-suffix-advice activate)
4257 "Use `read-file-name' for reading arguments."
4258 (interactive (list (read-file-name "Find file in other window: "
4259 nil default-directory))))
4261 (defadvice find-file-other-frame (before vip-add-suffix-advice activate)
4262 "Use `read-file-name' for reading arguments."
4263 (interactive (list (read-file-name "Find file in other frame: "
4264 nil default-directory))))
4266 (defadvice read-file-name (around vip-suffix-advice activate)
4267 "Tell `exit-minibuffer' to run `vip-file-add-suffix' as a hook."
4268 (let ((vip-minibuffer-exit-hook 'vip-file-add-suffix))
4275 (defsubst vip-yank (text)
4276 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
4278 (setq this-command 'yank))
4280 (defun vip-put-back (arg)
4281 "Put back after point/below line."
4283 (let ((val (vip-p-val arg))
4284 (text (if vip-use-register
4285 (cond ((vip-valid-register vip-use-register '(digit))
4286 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4287 ((vip-valid-register vip-use-register)
4288 (get-register (downcase vip-use-register)))
4289 (t (error vip-InvalidRegister vip-use-register)))
4292 (if vip-use-register
4293 (let ((reg vip-use-register))
4294 (setq vip-use-register nil)
4295 (error vip-EmptyRegister reg))
4297 (setq vip-use-register nil)
4298 (if (vip-end-with-a-newline-p text)
4303 (beginning-of-line))
4304 (if (not (eolp)) (vip-forward-char-carefully)))
4305 (set-marker (vip-mark-marker) (point) (current-buffer))
4306 (vip-set-destructive-command
4307 (list 'vip-put-back val nil vip-use-register nil nil))
4308 (vip-loop val (vip-yank text)))
4309 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4310 ;; newline; it leaves the cursor at the beginning when the text contains
4312 (if (vip-same-line (point) (mark))
4313 (or (= (point) (mark)) (vip-backward-char-carefully))
4314 (exchange-point-and-mark)
4316 (back-to-indentation)))
4317 (vip-deactivate-mark))
4319 (defun vip-Put-back (arg)
4320 "Put back at point/above line."
4322 (let ((val (vip-p-val arg))
4323 (text (if vip-use-register
4324 (cond ((vip-valid-register vip-use-register '(digit))
4325 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4326 ((vip-valid-register vip-use-register)
4327 (get-register (downcase vip-use-register)))
4328 (t (error vip-InvalidRegister vip-use-register)))
4331 (if vip-use-register
4332 (let ((reg vip-use-register))
4333 (setq vip-use-register nil)
4334 (error vip-EmptyRegister reg))
4336 (setq vip-use-register nil)
4337 (if (vip-end-with-a-newline-p text) (beginning-of-line))
4338 (vip-set-destructive-command
4339 (list 'vip-Put-back val nil vip-use-register nil nil))
4340 (set-marker (vip-mark-marker) (point) (current-buffer))
4341 (vip-loop val (vip-yank text)))
4342 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4343 ;; newline; it leaves the cursor at the beginning when the text contains
4345 (if (vip-same-line (point) (mark))
4346 (or (= (point) (mark)) (vip-backward-char-carefully))
4347 (exchange-point-and-mark)
4349 (back-to-indentation)))
4350 (vip-deactivate-mark))
4353 ;; Copy region to kill-ring.
4354 ;; If BEG and END do not belong to the same buffer, copy empty region.
4355 (defun vip-copy-region-as-kill (beg end)
4357 (copy-region-as-kill beg end)
4358 (error (copy-region-as-kill beg beg))))
4361 (defun vip-delete-char (arg)
4364 (let ((val (vip-p-val arg)))
4365 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
4368 (let ((here (point)))
4370 (if (> val (- (point) here))
4371 (setq val (- (point) here))))))
4372 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
4373 (if (and vip-ex-style-motion (eolp))
4374 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
4375 (if vip-use-register
4377 (cond ((vip-valid-register vip-use-register '((Letter)))
4378 (vip-append-to-register
4379 (downcase vip-use-register) (point) (- (point) val)))
4380 ((vip-valid-register vip-use-register)
4382 vip-use-register (point) (- (point) val) nil))
4383 (t (error vip-InvalidRegister vip-use-register)))
4384 (setq vip-use-register nil)))
4385 (if vip-ex-style-motion
4388 (if (and (eolp) (not (bolp))) (backward-char 1)))
4390 (delete-backward-char val t)
4391 (delete-char val t)))))
4393 (defun vip-delete-backward-char (arg)
4394 "Delete previous character. On reaching beginning of line, stop and beep."
4396 (let ((val (vip-p-val arg)))
4397 (vip-set-destructive-command
4398 (list 'vip-delete-backward-char val nil nil nil nil))
4401 (let ((here (point)))
4403 (if (> val (- here (point)))
4404 (setq val (- here (point)))))))
4405 (if vip-use-register
4407 (cond ((vip-valid-register vip-use-register '(Letter))
4408 (vip-append-to-register
4409 (downcase vip-use-register) (point) (+ (point) val)))
4410 ((vip-valid-register vip-use-register)
4412 vip-use-register (point) (+ (point) val) nil))
4413 (t (error vip-InvalidRegister vip-use-register)))
4414 (setq vip-use-register nil)))
4416 (delete-backward-char val t))))
4418 (defun vip-del-backward-char-in-insert ()
4419 "Delete 1 char backwards while in insert mode."
4421 (if (and vip-ex-style-editing-in-insert (bolp))
4423 (delete-backward-char 1 t)))
4425 (defun vip-del-backward-char-in-replace ()
4426 "Delete one character in replace mode.
4427 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
4428 charecters. If it is nil, then the cursor just moves backwards, similarly
4429 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
4430 cursor move past the beginning of line."
4432 (cond (vip-delete-backwards-in-replace
4434 (delete-backward-char 1 t))
4435 (vip-ex-style-editing-in-insert
4440 (delete-backward-char 1 t))))
4441 (vip-ex-style-editing-in-insert
4446 (backward-char 1))))
4452 (defun vip-join-lines (arg)
4453 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4455 (let ((val (vip-P-val arg)))
4456 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
4457 (vip-loop (if (null val) 1 (1- val))
4463 (delete-region (point) (1- (point)))
4464 (fixup-whitespace)))))))
4469 (defun vip-change (beg end)
4470 (if (markerp beg) (setq beg (marker-position beg)))
4471 (if (markerp end) (setq end (marker-position end)))
4472 ;; beg is sometimes (mark t), which may be nil
4473 (or beg (setq beg end))
4475 (vip-set-complex-command-for-undo)
4476 (if vip-use-register
4478 (copy-to-register vip-use-register beg end nil)
4479 (setq vip-use-register nil)))
4480 (vip-set-replace-overlay beg end)
4481 (setq last-command nil) ; separate repl text from prev kills
4483 (if (= (vip-replace-start) (point-max))
4484 (error "End of buffer"))
4486 (setq vip-last-replace-region
4487 (buffer-substring (vip-replace-start)
4490 ;; protect against error while inserting "@" and other disasters
4491 ;; (e.g., read-only buff)
4492 (condition-case conds
4493 (if (vip-same-line (vip-replace-start)
4496 ;; tabs cause problems in replace, so untabify
4497 (goto-char (vip-replace-end))
4498 (insert-before-markers "@") ; put placeholder after the TAB
4499 (untabify (vip-replace-start) (point))
4500 ;; del @, don't put on kill ring
4501 (delete-backward-char 1)
4503 (vip-set-replace-overlay-glyphs
4504 vip-replace-region-start-delimiter
4505 vip-replace-region-end-delimiter)
4506 ;; this move takes care of the last posn in the overlay, which
4507 ;; has to be shifted because of insert. We can't simply insert
4508 ;; "$" before-markers because then overlay-start will shift the
4509 ;; beginning of the overlay in case we are replacing a single
4510 ;; character. This fixes the bug with `s' and `cl' commands.
4511 (vip-move-replace-overlay (vip-replace-start) (point))
4512 (goto-char (vip-replace-start))
4513 (vip-change-state-to-replace t))
4514 (kill-region (vip-replace-start)
4516 (vip-restore-cursor-color)
4517 (vip-change-state-to-insert))
4518 (error ;; make sure that the overlay doesn't stay.
4519 ;; go back to the original point
4520 (goto-char (vip-replace-start))
4521 (vip-hide-replace-overlay)
4522 (vip-message-conditions conds))))
4525 (defun vip-change-subr (beg end)
4526 ;; beg is sometimes (mark t), which may be nil
4527 (or beg (setq beg end))
4529 (if vip-use-register
4531 (copy-to-register vip-use-register beg end nil)
4532 (setq vip-use-register nil)))
4533 (kill-region beg end)
4534 (setq this-command 'vip-change)
4535 (vip-yank-last-insertion))
4537 (defun vip-toggle-case (arg)
4538 "Toggle character case."
4540 (let ((val (vip-p-val arg)) (c))
4541 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
4543 (setq c (following-char))
4545 (if (eq c (upcase c))
4546 (insert-char (downcase c) 1)
4547 (insert-char (upcase c) 1))
4548 (if (eolp) (backward-char 1))
4549 (setq val (1- val)))))
4554 (defun vip-query-replace ()
4556 If a null string is suplied as the string to be replaced,
4557 the query replace mode will toggle between string replace
4558 and regexp replace."
4561 (setq str (vip-read-string-with-history
4562 (if vip-re-query-replace "Query replace regexp: "
4565 'vip-replace1-history
4566 (car vip-replace1-history) ; default
4568 (if (string= str "")
4570 (setq vip-re-query-replace (not vip-re-query-replace))
4571 (message "Query replace mode changed to %s"
4572 (if vip-re-query-replace "regexp replace"
4574 (if vip-re-query-replace
4575 (query-replace-regexp
4577 (vip-read-string-with-history
4578 (format "Query replace regexp `%s' with: " str)
4580 'vip-replace1-history
4581 (car vip-replace1-history) ; default
4585 (vip-read-string-with-history
4586 (format "Query replace `%s' with: " str)
4588 'vip-replace1-history
4589 (car vip-replace1-history) ; default
4595 (defun vip-mark-beginning-of-buffer ()
4596 "Mark beginning of buffer."
4599 (goto-char (point-min))
4600 (exchange-point-and-mark)
4601 (message "Mark set at the beginning of buffer"))
4603 (defun vip-mark-end-of-buffer ()
4604 "Mark end of buffer."
4607 (goto-char (point-max))
4608 (exchange-point-and-mark)
4609 (message "Mark set at the end of buffer"))
4611 (defun vip-mark-point ()
4612 "Set mark at point of buffer."
4614 (let ((char (vip-read-char-exclusive)))
4615 (cond ((and (<= ?a char) (<= char ?z))
4616 (point-to-register (1+ (- char ?a))))
4617 ((= char ?<) (vip-mark-beginning-of-buffer))
4618 ((= char ?>) (vip-mark-end-of-buffer))
4619 ((= char ?.) (vip-set-mark-if-necessary))
4620 ((= char ?,) (vip-cycle-through-mark-ring))
4621 ((= char ?D) (mark-defun))
4625 ;; Algorithm: If first invocation of this command save mark on ring, goto
4626 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4627 ;; making it into the new mark, M1.
4628 ;; Push this mark back and set mark to the original point position, p1.
4629 ;; So, if you hit '' or `` then you can return to p1.
4631 ;; If repeated command, pop top elt from the ring into mark and
4632 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4633 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4634 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4636 (defun vip-cycle-through-mark-ring ()
4637 "Visit previous locations on the mark ring.
4638 One can use `` and '' to temporarily jump 1 step back."
4639 (let* ((sv-pt (point)))
4640 ;; if repeated `m,' command, pop the previously saved mark.
4641 ;; Prev saved mark is actually prev saved point. It is used if the
4642 ;; user types `` or '' and is discarded
4643 ;; from the mark ring by the next `m,' command.
4644 ;; In any case, go to the previous or previously saved mark.
4645 ;; Then push the current mark (popped off the ring) and set current
4646 ;; point to be the mark. Current pt as mark is discarded by the next
4648 (if (eq last-command 'vip-cycle-through-mark-ring)
4650 ;; save current mark if the first iteration
4651 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4653 (push-mark (mark t) t)) )
4655 (set-mark-command 1)
4656 ;; don't duplicate mark on the ring
4657 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4659 (vip-deactivate-mark)
4660 (setq this-command 'vip-cycle-through-mark-ring)
4664 (defun vip-goto-mark (arg)
4667 (let ((char (read-char))
4668 (com (vip-getcom arg)))
4669 (vip-goto-mark-subr char com nil)))
4671 (defun vip-goto-mark-and-skip-white (arg)
4672 "Go to mark and skip to first non-white character on line."
4674 (let ((char (read-char))
4675 (com (vip-getCom arg)))
4676 (vip-goto-mark-subr char com t)))
4678 (defun vip-goto-mark-subr (char com skip-white)
4681 (error "Empty buffer")
4683 (cond ((vip-valid-register char '(letter))
4684 (let* ((buff (current-buffer))
4685 (reg (1+ (- char ?a)))
4686 (text-marker (get-register reg)))
4687 (if com (vip-move-marker-locally 'vip-com-point (point)))
4688 (if (not (vip-valid-marker text-marker))
4689 (error (format vip-EmptyTextmarker char)))
4690 (if (and (vip-same-line (point) vip-last-jump)
4691 (= (point) vip-last-jump-ignore))
4692 (push-mark vip-last-jump t)
4693 (push-mark nil t)) ; no msg
4694 (vip-register-to-point reg)
4695 (setq vip-last-jump (point-marker))
4697 (back-to-indentation)
4698 (setq vip-last-jump-ignore (point))))
4700 (if (equal buff (current-buffer))
4701 (vip-execute-com (if skip-white
4702 'vip-goto-mark-and-skip-white
4705 (switch-to-buffer buff)
4706 (goto-char vip-com-point)
4707 (vip-change-state-to-vi)
4709 ((and (not skip-white) (= char ?`))
4710 (if com (vip-move-marker-locally 'vip-com-point (point)))
4711 (if (and (vip-same-line (point) vip-last-jump)
4712 (= (point) vip-last-jump-ignore))
4713 (goto-char vip-last-jump))
4714 (if (= (point) (mark t)) (pop-mark))
4715 (exchange-point-and-mark)
4716 (setq vip-last-jump (point-marker)
4717 vip-last-jump-ignore 0)
4718 (if com (vip-execute-com 'vip-goto-mark nil com)))
4719 ((and skip-white (= char ?'))
4720 (if com (vip-move-marker-locally 'vip-com-point (point)))
4721 (if (and (vip-same-line (point) vip-last-jump)
4722 (= (point) vip-last-jump-ignore))
4723 (goto-char vip-last-jump))
4724 (if (= (point) (mark t)) (pop-mark))
4725 (exchange-point-and-mark)
4726 (setq vip-last-jump (point))
4727 (back-to-indentation)
4728 (setq vip-last-jump-ignore (point))
4729 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
4730 (t (error vip-InvalidTextmarker char))))
4732 (defun vip-insert-tab ()
4736 (defun vip-exchange-point-and-mark ()
4738 (exchange-point-and-mark)
4739 (back-to-indentation))
4741 ;; Input Mode Indentation
4743 ;; Returns t, if the string before point matches the regexp STR.
4744 (defsubst vip-looking-back (str)
4745 (and (save-excursion (re-search-backward str nil t))
4746 (= (point) (match-end 0))))
4749 (defun vip-forward-indent ()
4750 "Indent forward -- `C-t' in Vi."
4753 (indent-to (+ (current-column) vip-shift-width)))
4755 (defun vip-backward-indent ()
4756 "Backtab, C-d in VI"
4759 (let ((p (point)) (c (current-column)) bol (indent t))
4760 (if (vip-looking-back "[0^]")
4762 (if (eq ?^ (preceding-char))
4763 (setq vip-preserve-indent t))
4764 (delete-backward-char 1)
4770 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4771 (delete-region (point) p)
4773 (indent-to (- c vip-shift-width)))
4774 (if (or (bolp) (vip-looking-back "[^ \t]"))
4775 (setq vip-cted nil)))))
4777 (defun vip-autoindent ()
4778 "Auto Indentation, Vi-style."
4780 (let ((col (current-indentation)))
4781 (if vip-preserve-indent
4782 (setq vip-preserve-indent nil)
4783 (setq vip-current-indent col))
4784 ;; don't leave whitespace lines around
4785 (if (memq last-command
4787 vip-open-line vip-Open-line
4788 vip-replace-state-exit-cmd))
4789 (indent-to-left-margin))
4794 (if vip-electric-mode
4795 (indent-according-to-mode)
4796 (indent-to vip-current-indent))
4801 ;; Viewing registers
4803 (defun vip-ket-function (arg)
4804 "Function called by \], the ket. View registers and call \]\]."
4806 (let ((reg (read-char)))
4807 (cond ((vip-valid-register reg '(letter Letter))
4808 (view-register (downcase reg)))
4809 ((vip-valid-register reg '(digit))
4810 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4812 (set-buffer (get-buffer-create "*Output*"))
4813 (delete-region (point-min) (point-max))
4814 (insert (format "Register %c contains the string:\n" reg))
4816 (goto-char (point-min)))
4817 (display-buffer "*Output*")))
4819 (vip-next-heading arg))
4821 vip-InvalidRegister reg)))))
4823 (defun vip-brac-function (arg)
4824 "Function called by \[, the brac. View textmarkers and call \[\["
4826 (let ((reg (read-char)))
4828 (vip-prev-heading arg))
4830 (vip-heading-end arg))
4831 ((vip-valid-register reg '(letter))
4832 (let* ((val (get-register (1+ (- reg ?a))))
4835 (format vip-EmptyTextmarker reg))
4836 (marker-buffer val)))
4837 (pos (marker-position val))
4838 line-no text (s pos) (e pos))
4840 (set-buffer (get-buffer-create "*Output*"))
4841 (delete-region (point-min) (point-max))
4846 (setq line-no (1+ (count-lines (point-min) val)))
4849 (if (re-search-backward "[^ \t]" nil t)
4855 (if (re-search-forward "[^ \t]" nil t)
4859 (setq text (buffer-substring s e))
4860 (setq text (format "%s<%c>%s"
4861 (substring text 0 (- pos s))
4862 reg (substring text (- pos s)))))
4865 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4866 reg (buffer-name buf) line-no))
4867 (insert (format "Here is some text around %c:\n\n %s"
4869 (insert (format vip-EmptyTextmarker reg)))
4870 (goto-char (point-min)))
4871 (display-buffer "*Output*")))
4872 (t (error vip-InvalidTextmarker reg)))))
4876 ;; commands in insertion mode
4878 (defun vip-delete-backward-word (arg)
4879 "Delete previous word."
4884 (delete-region (point) (mark t))
4888 (defun vip-set-expert-level (&optional dont-change-unless)
4889 "Sets the expert level for a Viper user.
4890 Can be called interactively to change (temporarily or permanently) the
4891 current expert level.
4893 The optional argument DONT-CHANGE-UNLESS if not nil, says that
4894 the level should not be changed, unless its current value is
4895 meaningless (i.e., not one of 1,2,3,4,5).
4897 User level determines the setting of Viper variables that are most
4898 sensitive for VI-style look-and-feel."
4902 (if (not (numberp vip-expert-level)) (setq vip-expert-level 0))
4904 (save-window-excursion
4905 (delete-other-windows)
4906 ;; if 0 < vip-expert-level < vip-max-expert-level
4907 ;; & dont-change-unless = t -- use it; else ask
4908 (vip-ask-level dont-change-unless))
4911 vip-ex-style-motion t
4912 vip-ex-style-editing-in-insert t
4913 vip-want-ctl-h-help nil)
4916 ;; a novice or a beginner
4917 ((eq vip-expert-level 1)
4918 (global-set-key vip-toggle-key ;; in emacs-state
4919 (if (vip-window-display-p)
4922 (setq vip-no-multiple-ESC t
4924 vip-vi-style-in-minibuffer t
4925 vip-search-wrap-around-t t
4926 vip-want-emacs-keys-in-vi nil
4927 vip-want-emacs-keys-in-insert nil))
4929 ;; an intermediate to guru
4930 ((and (> vip-expert-level 1) (< vip-expert-level 5))
4931 (setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice)
4932 vip-want-emacs-keys-in-vi t
4933 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4935 (if (eq vip-expert-level 4) ; respect user's ex-style motions
4936 ; and vip-no-multiple-ESC
4938 (setq-default vip-ex-style-editing-in-insert
4939 (cdr (assoc 'vip-ex-style-editing-in-insert
4940 vip-saved-user-settings))
4942 (cdr (assoc 'vip-ex-style-motion
4943 vip-saved-user-settings)))
4944 (setq vip-ex-style-motion
4945 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4946 vip-ex-style-editing-in-insert
4947 (cdr (assoc 'vip-ex-style-editing-in-insert
4948 vip-saved-user-settings))
4950 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4952 (cdr (assoc 'vip-no-multiple-ESC
4953 vip-saved-user-settings))))))
4956 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4957 ;; user toggle variable values.
4958 (t (setq-default vip-ex-style-editing-in-insert
4959 (cdr (assoc 'vip-ex-style-editing-in-insert
4960 vip-saved-user-settings))
4962 (cdr (assoc 'vip-ex-style-motion
4963 vip-saved-user-settings)))
4964 (setq vip-want-ctl-h-help
4965 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
4967 (cdr (assoc 'vip-always vip-saved-user-settings))
4969 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
4971 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4972 vip-ex-style-editing-in-insert
4973 (cdr (assoc 'vip-ex-style-editing-in-insert
4974 vip-saved-user-settings))
4976 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4977 vip-want-emacs-keys-in-vi
4978 (cdr (assoc 'vip-want-emacs-keys-in-vi
4979 vip-saved-user-settings))
4980 vip-want-emacs-keys-in-insert
4981 (cdr (assoc 'vip-want-emacs-keys-in-insert
4982 vip-saved-user-settings)))))
4983 (vip-set-mode-vars-for vip-current-state)
4985 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
4988 ;; Ask user expert level.
4989 (defun vip-ask-level (dont-change-unless)
4990 (let ((ask-buffer " *vip-ask-level*")
4991 level-changed repeated)
4992 (save-window-excursion
4993 (switch-to-buffer ask-buffer)
4995 (or (eq this-command 'vip-set-expert-level)
4997 (<= vip-expert-level vip-max-expert-level)
4998 (>= vip-expert-level 1))
5002 *** Important Notice for VIP users***
5007 Viper Is a Package for Emacs Rebels,
5008 a VI Plan for Emacs Rescue,
5009 and a venomous VI PERil.
5012 Technically speaking, Viper is a new Vi emulator that replaces
5013 the old VIP package.
5015 Viper emulates Vi much better than VIP. It also significantly
5016 extends and improves upon Vi in many useful ways.
5018 Although many VIP settings in your ~/.vip are compatible with Viper,
5019 you may have to change some of them. Please refer to the documentation,
5020 which can be obtained by executing
5024 when Viper is in Vi state.
5026 If you will be so lucky as to find a bug, report it via the command
5030 Type any key to continue... ")
5035 (while (or (> vip-expert-level vip-max-expert-level)
5036 (< vip-expert-level 1)
5037 (null dont-change-unless))
5041 (message "Invalid user level")
5044 (setq dont-change-unless t
5047 Please specify your level of familiarity with the venomous VI PERil
5048 (and the VI Plan for Emacs Rescue).
5049 You can change it at any time by typing `M-x vip-set-expert-level RET'
5051 1 -- BEGINNER: Almost all Emacs features are suppressed.
5052 Feels almost like straight Vi. File name completion and
5053 command history in the minibuffer are thrown in as a bonus.
5054 To use Emacs productively, you must reach level 3 or higher.
5055 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
5056 so most Emacs commands can be used when Viper is in Vi state.
5057 Good progress---you are well on the way to level 3!
5058 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
5059 in Viper's insert state.
5060 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
5061 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
5062 variables. Adjust these settings to your taste.
5063 5 -- WIZARD: Like 4, but user settings are also respected for vip-always,
5064 vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and
5065 vip-want-emacs-keys-in-insert. Adjust these to your taste.
5067 Please, specify your level now: ")
5069 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
5072 ;; tell the user if level was changed
5076 (format "\n\n\n\n\n\t\tYou have selected user level %d"
5078 (if (y-or-n-p "Do you wish to make this change permanent? ")
5079 ;; save the setting for vip-expert-level
5082 (format "Saving user level %d ..." vip-expert-level)
5083 vip-custom-file-name))
5085 (bury-buffer) ; remove ask-buffer from screen
5090 (defun viper-version ()
5092 (message "Viper version is %s" viper-version))
5094 (defalias 'vip-version 'viper-version)
5101 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
5102 (defun vip-register-to-point (char &optional enforce-buffer)
5103 "Like jump-to-register, but switches to another buffer in another window."
5104 (interactive "cViper register to point: ")
5105 (let ((val (get-register char)))
5107 ((and (fboundp 'frame-configuration-p)
5108 (frame-configuration-p val))
5109 (set-frame-configuration val))
5110 ((window-configuration-p val)
5111 (set-window-configuration val))
5112 ((vip-valid-marker val)
5113 (if (and enforce-buffer
5114 (not (equal (current-buffer) (marker-buffer val))))
5115 (error (concat vip-EmptyTextmarker " in this buffer")
5117 (pop-to-buffer (marker-buffer val))
5119 ((and (consp val) (eq (car val) 'file))
5120 (find-file (cdr val)))
5122 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
5125 (defun vip-save-kill-buffer ()
5126 "Save then kill current buffer. "
5128 (if (< vip-expert-level 2)
5129 (save-buffers-kill-emacs)
5131 (kill-buffer (current-buffer))))
5137 (defun vip-submit-report ()
5138 "Submit bug report on Viper."
5140 (let ((reporter-prompt-for-summary-p t)
5141 (vip-device-type (vip-device-type))
5142 color-display-p frame-parameters
5143 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
5144 varlist salutation window-config)
5146 ;; If mode info is needed, add variable to `let' and then set it below,
5147 ;; like we did with color-display-p.
5148 (setq color-display-p (if (vip-window-display-p)
5149 (vip-color-display-p)
5151 minibuffer-vi-face (if (vip-window-display-p)
5152 (vip-get-face vip-minibuffer-vi-face)
5154 minibuffer-insert-face (if (vip-window-display-p)
5155 (vip-get-face vip-minibuffer-insert-face)
5157 minibuffer-emacs-face (if (vip-window-display-p)
5158 (vip-get-face vip-minibuffer-emacs-face)
5160 frame-parameters (if (fboundp 'frame-parameters)
5161 (frame-parameters (selected-frame))))
5163 (setq varlist (list 'vip-vi-minibuffer-minor-mode
5164 'vip-insert-minibuffer-minor-mode
5165 'vip-vi-intercept-minor-mode
5166 'vip-vi-local-user-minor-mode
5167 'vip-vi-kbd-minor-mode
5168 'vip-vi-global-user-minor-mode
5169 'vip-vi-state-modifier-minor-mode
5170 'vip-vi-diehard-minor-mode
5171 'vip-vi-basic-minor-mode
5172 'vip-replace-minor-mode
5173 'vip-insert-intercept-minor-mode
5174 'vip-insert-local-user-minor-mode
5175 'vip-insert-kbd-minor-mode
5176 'vip-insert-global-user-minor-mode
5177 'vip-insert-state-modifier-minor-mode
5178 'vip-insert-diehard-minor-mode
5179 'vip-insert-basic-minor-mode
5180 'vip-emacs-intercept-minor-mode
5181 'vip-emacs-local-user-minor-mode
5182 'vip-emacs-kbd-minor-mode
5183 'vip-emacs-global-user-minor-mode
5184 'vip-emacs-state-modifier-minor-mode
5185 'vip-automatic-iso-accents
5186 'vip-want-emacs-keys-in-insert
5187 'vip-want-emacs-keys-in-vi
5188 'vip-keep-point-on-undo
5189 'vip-no-multiple-ESC
5191 'vip-want-ctl-h-help
5192 'vip-ex-style-editing-in-insert
5193 'vip-delete-backwards-in-replace
5194 'vip-vi-style-in-minibuffer
5196 'vip-insert-state-hook
5197 'vip-replace-state-hook
5198 'vip-emacs-state-hook
5199 'ex-cycle-other-window
5200 'ex-cycle-through-non-files
5207 'minibuffer-insert-face
5208 'minibuffer-emacs-face
5211 Congratulations! You may have unearthed a bug in Viper!
5212 Please mail a concise, accurate summary of the problem to the address above.
5214 -------------------------------------------------------------------")
5215 (setq window-config (current-window-configuration))
5216 (with-output-to-temp-buffer " *vip-info*"
5217 (switch-to-buffer " *vip-info*")
5218 (delete-other-windows)
5220 PLEASE FOLLOW THESE PROCEDURES
5221 ------------------------------
5223 Before reporting a bug, please verify that it is related to Viper, and is
5224 not cause by other packages you are using.
5226 Don't report compilation warnings, unless you are certain that there is a
5227 problem. These warnings are normal and unavoidable.
5229 Please note that users should not modify variables and keymaps other than
5230 those advertised in the manual. Such `customization' is likely to crash
5231 Viper, as it would any other improperly customized Emacs package.
5233 If you are reporting an error message received while executing one of the
5234 Viper commands, type:
5236 M-x set-variable <Return> debug-on-error <Return> t <Return>
5238 Then reproduce the error. The above command will cause Emacs to produce a
5239 back trace of the execution that leads to the error. Please include this
5240 trace in your bug report.
5242 If you believe that one of Viper's commands goes into an infinite loop
5243 \(e.g., Emacs freezes\), type:
5245 M-x set-variable <Return> debug-on-quit <Return> t <Return>
5247 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
5248 the current command. Include the resulting back trace in the bug report.
5250 Mail anyway (y or n)? ")
5251 (if (y-or-n-p "Mail anyway? ")
5253 (set-window-configuration window-config)
5254 (error "Bug report aborted")))
5257 (set-window-configuration window-config)
5259 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
5262 nil 'delete-other-windows
5269 ;; Smoothes out the difference between Emacs' unread-command-events
5270 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
5271 ;; events or a sequence of keys.
5273 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
5274 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
5275 ;; into an event. Below, we delete nil from event lists, since nil is the most
5276 ;; common symbol that might appear in this wrong context.
5277 (defun vip-set-unread-command-events (arg)
5280 unread-command-events
5282 (cond ((eventp arg) (list arg))
5285 (listify-key-sequence arg))
5287 "vip-set-unread-command-events: Invalid argument, %S"
5289 (if (not (eventp nil))
5290 (setq new-events (delq nil new-events)))
5291 (append new-events unread-command-events)))
5294 unread-command-events
5296 (cond ((numberp arg) (list (character-to-event arg)))
5297 ((eventp arg) (list arg))
5298 ((stringp arg) (mapcar 'character-to-event arg))
5299 ((vectorp arg) (append arg nil)) ; turn into list
5300 ((listp arg) (vip-eventify-list-xemacs arg))
5302 "vip-set-unread-command-events: Invalid argument, %S" arg)))
5303 unread-command-events))))
5305 ;; list is assumed to be a list of events of characters
5306 (defun vip-eventify-list-xemacs (lis)
5308 (function (lambda (elt)
5309 (cond ((numberp elt) (character-to-event elt))
5312 "vip-eventify-list-xemacs: can't convert to event, %S"
5318 ;;; Bring in the rest of the files
5319 (require 'viper-mous)
5320 (require 'viper-macs)
5325 ;; The following is provided for compatibility with older VIP's
5327 (defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
5328 (defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert)
5329 (defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs)
5333 ;;; Load .vip and set up hooks
5335 ;; This hook designed to enable Vi-style editing in comint-based modes."
5336 (defun vip-comint-mode-hook ()
5337 (setq require-final-newline nil)
5338 (setq vip-ex-style-editing-in-insert nil
5339 vip-ex-style-motion nil)
5340 (vip-add-local-keys 'vi-state
5341 '(("\C-m" . comint-send-input) ; return
5342 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5343 (vip-add-local-keys 'insert-state
5344 '(("\C-m" . comint-send-input) ; return
5345 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5349 ;; This sets major mode hooks to make them come up in vi-state.
5350 (defun vip-set-hooks ()
5352 ;; It is of course a misnomer to call viper-mode a `major mode'.
5353 ;; However, this has the effect that if the user didn't specify the
5354 ;; default mode, new buffers that fall back on the default will come up
5355 ;; in Fundamental Mode and Vi state.
5356 (setq default-major-mode 'viper-mode)
5358 ;; The following major modes should come up in vi-state
5359 (defadvice fundamental-mode (after vip-fundamental-mode-ad activate)
5360 "Run `vip-change-state-to-vi' on entry."
5361 (vip-change-state-to-vi))
5363 (defvar makefile-mode-hook nil)
5364 (add-hook 'makefile-mode-hook 'viper-mode)
5366 (defvar help-mode-hook nil)
5367 (add-hook 'help-mode-hook 'viper-mode)
5369 (defvar awk-mode-hook nil)
5370 (add-hook 'awk-mode-hook 'viper-mode)
5372 (defvar html-mode-hook nil)
5373 (add-hook 'html-mode-hook 'viper-mode)
5374 (defvar html-helper-mode-hook nil)
5375 (add-hook 'html-helper-mode-hook 'viper-mode)
5377 (defvar emacs-lisp-mode-hook nil)
5378 (add-hook 'emacs-lisp-mode-hook 'viper-mode)
5380 (defvar lisp-mode-hook nil)
5381 (add-hook 'lisp-mode-hook 'viper-mode)
5383 (defvar bibtex-mode-hook nil)
5384 (add-hook 'bibtex-mode-hook 'viper-mode)
5386 (defvar cc-mode-hook nil)
5387 (add-hook 'cc-mode-hook 'viper-mode)
5389 (defvar c-mode-hook nil)
5390 (add-hook 'c-mode-hook 'viper-mode)
5392 (defvar c++-mode-hook nil)
5393 (add-hook 'c++-mode-hook 'viper-mode)
5395 (defvar lisp-interaction-mode-hook nil)
5396 (add-hook 'lisp-interaction-mode-hook 'viper-mode)
5398 (defvar text-mode-hook nil)
5399 (add-hook 'text-mode-hook 'viper-mode)
5401 (add-hook 'completion-list-mode-hook 'viper-mode)
5402 (add-hook 'compilation-mode-hook 'viper-mode)
5404 (add-hook 'perl-mode-hook 'viper-mode)
5406 (defvar emerge-startup-hook nil)
5407 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
5408 ;; Run vip-change-state-to-vi after quitting emerge.
5409 (vip-eval-after-load
5411 '(defadvice emerge-quit (after vip-emerge-advice activate)
5412 "Run `vip-change-state-to-vi' after quitting emerge."
5413 (vip-change-state-to-vi)))
5414 ;; In case Emerge was loaded before Viper.
5415 (defadvice emerge-quit (after vip-emerge-advice activate)
5416 "Run `vip-change-state-to-vi' after quitting emerge."
5417 (vip-change-state-to-vi))
5419 (vip-eval-after-load
5421 '(defadvice asm-mode (after vip-asm-mode-ad activate)
5422 "Run `vip-change-state-to-vi' on entry."
5423 (vip-change-state-to-vi)))
5425 ;; passwd.el sets up its own buffer, which turns up in Vi mode,
5426 ;; thus overriding the local map. We don't need Vi mode here.
5427 (vip-eval-after-load
5429 '(defadvice read-passwd-1 (before vip-passwd-ad activate)
5430 "Switch to emacs state while reading password."
5431 (vip-change-state-to-emacs)))
5433 (vip-eval-after-load
5435 '(defadvice prolog-mode (after vip-prolog-ad activate)
5436 "Switch to Vi state in Prolog mode."
5437 (vip-change-state-to-vi)))
5439 ;; Emacs shell, ange-ftp, and comint-based modes
5440 (defvar comint-mode-hook nil)
5441 (add-hook 'comint-mode-hook 'vip-change-state-to-insert)
5442 (add-hook 'comint-mode-hook 'vip-comint-mode-hook)
5445 (defvar sh-mode-hook nil)
5446 (add-hook 'sh-mode-hook 'viper-mode)
5447 (defvar ksh-mode-hook nil)
5448 (add-hook 'ksh-mode-hook 'viper-mode)
5451 ;; This is only necessary when the user uses vip-modify-major-mode
5452 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
5456 (defvar view-mode-hook nil
5457 "View hook. Run after view mode.")
5458 (add-hook 'view-mode-hook 'vip-change-state-to-emacs))
5459 (defadvice view-minor-mode (after vip-view-ad activate)
5460 "Switch to Emacs state in View mode."
5461 (vip-change-state-to-emacs))
5462 (defvar view-hook nil
5463 "View hook. Run after view mode.")
5464 (add-hook 'view-hook 'vip-change-state-to-emacs))
5467 ;; Put summary and other VM buffers in Emacs state.
5468 (defvar vm-mode-hooks nil
5469 "This hook is run after vm is started.")
5470 (defvar vm-summary-mode-hooks nil
5471 "This hook is run after vm switches to summary mode.")
5472 (add-hook 'vm-mode-hooks 'vip-change-state-to-emacs)
5473 (add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs)
5476 ;; Put buf in Emacs state after edit.
5477 (vip-eval-after-load
5479 '(defadvice rmail-cease-edit (after vip-rmail-advice activate)
5480 "Switch to emacs state when done editing message."
5481 (vip-change-state-to-emacs)))
5482 ;; In case RMAIL was loaded before Viper.
5483 (defadvice rmail-cease-edit (after vip-rmail-advice activate)
5484 "Switch to emacs state when done editing message."
5485 (vip-change-state-to-emacs))
5488 ;; Set some useful macros
5489 ;; These must be before we load .vip, so the user can unrecord them.
5491 ;; repeat the 2nd previous command without rotating the command history
5492 (vip-record-kbd-macro
5493 (vector vip-repeat-from-history-key '\1) 'vi-state
5494 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5495 ;; repeat the 3d previous command without rotating the command history
5496 (vip-record-kbd-macro
5497 (vector vip-repeat-from-history-key '\2) 'vi-state
5498 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5500 ;; toggle case sensitivity in search
5501 (vip-record-kbd-macro
5503 [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)
5504 ;; toggle regexp/vanila search
5505 (vip-record-kbd-macro
5507 [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)
5510 ;; ~/.vip is loaded if it exists
5511 (if (and (file-exists-p vip-custom-file-name)
5512 (not noninteractive))
5513 (load vip-custom-file-name))
5515 ;; VIP compatibility: merge whatever the user has in vip-mode-map into
5516 ;; Viper's basic map.
5517 (vip-add-keymap vip-mode-map vip-vi-global-user-map)
5520 ;; Applying Viper customization -- runs after (load .vip)
5522 ;; Save user settings or Viper defaults for vars controled by vip-expert-level
5523 (setq vip-saved-user-settings
5524 (list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help)
5525 (cons 'vip-always vip-always)
5526 (cons 'vip-no-multiple-ESC vip-no-multiple-ESC)
5527 (cons 'vip-ex-style-motion vip-ex-style-motion)
5528 (cons 'vip-ex-style-editing-in-insert
5529 vip-ex-style-editing-in-insert)
5530 (cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi)
5531 (cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert)
5532 (cons 'vip-re-search vip-re-search)))
5535 (vip-set-minibuffer-style)
5536 (vip-set-minibuffer-faces)
5537 (vip-set-search-face)
5538 (if vip-buffer-search-char
5539 (vip-buffer-search-enable))
5540 (vip-update-alphanumeric-class)
5542 ;;; Familiarize Viper with some minor modes that have their own keymaps
5543 (vip-harness-minor-mode "compile")
5544 (vip-harness-minor-mode "outline")
5545 (vip-harness-minor-mode "allout")
5546 (vip-harness-minor-mode "xref")
5547 (vip-harness-minor-mode "lmenu")
5548 (vip-harness-minor-mode "vc")
5549 (vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX
5550 (vip-harness-minor-mode "latex") ; which is in one of these two files
5551 (vip-harness-minor-mode "cyrillic")
5552 (vip-harness-minor-mode "russian")
5553 (vip-harness-minor-mode "view-less")
5554 (vip-harness-minor-mode "view")
5557 ;; Intercept maps could go in viper-keym.el
5558 ;; We keep them here in case someone redefines them in ~/.vip
5560 (define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5561 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5563 ;; This is taken care of by vip-insert-global-user-map.
5564 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
5566 (define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC)
5567 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
5569 (define-key vip-vi-intercept-map vip-toggle-key
5570 '(lambda () (interactive)
5571 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
5572 (if (vip-window-display-p) (vip-iconify) (suspend-emacs))
5573 (vip-change-state-to-emacs))))
5575 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
5579 (and (< vip-expert-level 5) (> vip-expert-level 0)))
5582 ;; Let all minor modes take effect after loading
5583 ;; this may not be enough, so we also set default minor-mode-alist.
5584 ;; Without setting the default, new buffers that come up in emacs mode have
5585 ;; minor-mode-map-alist = nil, unless we call vip-change-state-*
5586 (if (eq vip-current-state 'emacs-state)
5588 (vip-change-state-to-emacs)
5589 (setq-default minor-mode-map-alist minor-mode-map-alist)
5593 (run-hooks 'vip-load-hook) ; the last chance to change something
5599 ;;; viper.el ends here