1 ;;; viper.el --- A full-featured Vi emulator.
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.82 of October 12, 1995"
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 (or (face-differs-from-default-p 'vip-replace-overlay-face
)
475 (if (vip-can-use-colors "darkseagreen2" "Black")
478 'vip-replace-overlay-face
"darkseagreen2")
479 (set-face-foreground 'vip-replace-overlay-face
"Black")))
480 (set-face-underline-p 'vip-replace-overlay-face t
))
483 (defvar vip-replace-overlay-face
'vip-replace-overlay-face
484 "*Face for highlighting replace regions on a window display.")
486 (defvar vip-replace-region-end-delimiter
"$"
487 "A string marking the end of replacement regions.
488 It is used only with TTYs or if `vip-use-replace-region-delimiters'
490 (defvar vip-replace-region-start-delimiter
""
491 "A string marking the beginning of replacement regions.
492 It is used only with TTYs or if `vip-use-replace-region-delimiters'
494 (defvar vip-use-replace-region-delimiters
495 (or (not (vip-window-display-p)) (not (vip-color-display-p)))
496 "*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and
497 `vip-replace-region-start-delimiter' to delimit replacement regions, even on
498 color displays. By default, the delimiters are used only on TTYs or
499 monochrome displays.")
501 ;; XEmacs requires glyphs
504 (or (glyphp vip-replace-region-end-delimiter
)
505 (setq vip-replace-region-end-delimiter
506 (make-glyph vip-replace-region-end-delimiter
)))
507 (or (glyphp vip-replace-region-start-delimiter
)
508 (setq vip-replace-region-start-delimiter
509 (make-glyph vip-replace-region-start-delimiter
)))
513 ;; These are local marker that must be initialized to nil and moved with
514 ;; `vip-move-marker-locally'
516 ;; Remember the last position inside the replace region.
517 (vip-deflocalvar vip-last-posn-in-replace-region nil
)
518 ;; Remember the last position while inserting
519 (vip-deflocalvar vip-last-posn-while-in-insert-state nil
)
520 (put 'vip-last-posn-in-replace-region
'permanent-local t
)
521 (put 'vip-last-posn-while-in-insert-state
'permanent-local t
)
523 (vip-deflocalvar vip-sitting-in-replace nil
"")
524 (put 'vip-sitting-in-replace
'permanent-local t
)
526 ;; Remember the number of characters that have to be deleted in replace
527 ;; mode to compensate for the inserted characters.
528 (vip-deflocalvar vip-replace-chars-to-delete
0 "")
529 (vip-deflocalvar vip-replace-chars-deleted
0 "")
531 ;; Insertion ring and command ring
532 (defvar vip-insertion-ring-size
14
533 "The size of the insertion ring.")
534 ;; The insertion ring.
535 (defvar vip-insertion-ring nil
)
536 ;; This is temp insertion ring. Used to do rotation for display purposes.
537 ;; When rotation just started, it is initialized to vip-insertion-ring.
538 (defvar vip-temp-insertion-ring nil
)
539 (defvar vip-last-inserted-string-from-insertion-ring
"")
541 (defvar vip-command-ring-size
14
542 "The size of the command ring.")
544 (defvar vip-command-ring nil
)
545 ;; This is temp command ring. Used to do rotation for display purposes.
546 ;; When rotation just started, it is initialized to vip-command-ring.
547 (defvar vip-temp-command-ring nil
)
549 ;; Modes and related variables
551 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
552 (vip-deflocalvar vip-current-state
'emacs-state
)
555 (defvar vip-toggle-key
"\C-z"
556 "The key used to change states from emacs to Vi and back.
557 In insert mode, this key also functions as Meta.
558 Must be set in .vip file or prior to loading Viper.
559 This setting cannot be changed interactively.")
561 (defvar vip-ESC-key
"\e"
563 Must be set in .vip file or prior to loading Viper.
564 This setting cannot be changed interactively.")
566 (defvar vip-no-multiple-ESC t
567 "*If true, multiple ESC in Vi mode will cause bell to ring.
568 \_ is then mapped to Meta.
569 This is set to t on a windowing terminal and to 'twice on a dumb
570 terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
571 enables cursor keys and is generally more convenient, as terminals usually
572 don't have a convenient Meta key.
573 Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
574 as is allowed by the major mode in effect.")
577 (defvar vip-want-ctl-h-help nil
578 "*If t then C-h is bound to help-command in insert mode, if nil then it is
579 bound to delete-backward-char.")
581 ;; Autoindent in insert
583 ;; Variable that keeps track of whether C-t has been pressed.
584 (vip-deflocalvar vip-cted nil
"")
586 ;; Preserve the indent value, used by C-d in insert mode.
587 (vip-deflocalvar vip-current-indent
0)
589 ;; Whether to preserve the indent, used by C-d in insert mode.
590 (vip-deflocalvar vip-preserve-indent nil
)
592 (defconst vip-auto-indent nil
595 (defconst vip-shift-width
8
596 "*The shiftwidth variable.")
598 ;; Variables for repeating destructive commands
600 (defconst vip-keep-point-on-repeat t
601 "*If t, don't move point when repeating previous command.
602 This is useful for doing repeated changes with the '.' key.
603 The user can change this to nil, if she likes when the cursor moves
604 to a new place after repeating previous Vi command.")
606 ;; Remember insert point as a marker. This is a local marker that must be
607 ;; initialized to nil and moved with `vip-move-marker-locally'.
608 (vip-deflocalvar vip-insert-point nil
)
609 (put 'vip-insert-point
'permanent-local t
)
611 ;; This remembers the point before dabbrev-expand was called.
612 ;; If vip-insert-point turns out to be bigger than that, it is reset
613 ;; back to vip-pre-command-point.
614 ;; The reason this is needed is because dabbrev-expand (and possibly
615 ;; others) may jump to before the insertion point, delete something and
616 ;; then reinsert a bigger piece. For instance: bla^blo
617 ;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point,
618 ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
619 ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
620 ;; will insert the expansion, and we get: blablo^
621 ;; Whatever we insert next goes before the ^, i.e., before the
622 ;; vip-insert-point marker. So, Viper will think that nothing was
623 ;; inserted. Remembering the orig position of the marker circumvents the
625 ;; We don't know of any command, except dabbrev-expand, that has the same
626 ;; problem. However, the same trick can be used if such a command is
629 (vip-deflocalvar vip-pre-command-point nil
)
630 (put 'vip-pre-command-point
'permanent-local t
) ; this is probably an overkill
632 ;; This is used for saving inserted text.
633 (defvar vip-last-insertion nil
)
635 ;; Remembers the last replaced region.
636 (defvar vip-last-replace-region
"")
638 ;; Remember com point as a marker.
639 ;; This is a local marker. Should be moved with `vip-move-marker-locally'
640 (vip-deflocalvar vip-com-point nil
)
642 ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
643 ;; It is used to re-execute last destructive command.
644 ;; M-COM is a Lisp symbol representing the function to be executed.
645 ;; VAL is the prefix argument that was used with that command.
646 ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
647 ;; additional information on how the function in M-COM is to be handled.
648 ;; REG is the register used by command
649 ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
651 ;; COMMAND-KEYS are the keys that were typed to invoke the command.
652 (defvar vip-d-com nil
)
654 ;; The character remembered by the Vi `r' command.
655 (defvar vip-d-char nil
)
657 ;; Name of register to store deleted or yanked strings
658 (defvar vip-use-register nil
)
662 ;; Variables for Moves and Searches
664 ;; For use by `;' command.
665 (defvar vip-f-char nil
)
667 ;; For use by `.' command.
668 (defvar vip-F-char nil
)
670 ;; For use by `;' command.
671 (defvar vip-f-forward nil
)
673 ;; For use by `;' command.
674 (defvar vip-f-offset nil
)
676 ;; Last search string
677 (defvar vip-s-string
"")
679 (defvar vip-quote-string
"> "
680 "String inserted at the beginning of quoted region.")
682 ;; If t, search is forward.
683 (defvar vip-s-forward nil
)
685 (defconst vip-case-fold-search nil
686 "*If t, search ignores cases.")
688 (defconst vip-re-search t
689 "*If t, search is reg-exp search, otherwise vanilla search.")
691 (defconst vip-re-query-replace t
692 "*If t then do regexp replace, if nil then do string replace.")
694 (defconst vip-re-replace t
695 "*If t, do regexp replace. nil means do string replace.")
697 (vip-deflocalvar vip-ex-style-motion t
698 "*Ex-style: the commands l,h do not cross lines, etc.")
700 (vip-deflocalvar vip-ex-style-editing-in-insert t
701 "*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc.
702 Note: this doesn't preclude ^H and ^? from deleting characters by moving
703 past the insertion point. This is a feature, not a bug. ")
705 (vip-deflocalvar vip-delete-backwards-in-replace nil
706 "*If t, DEL key will delete characters while moving the cursor backwards.
707 If nil, the cursor will move backwards without deleting anything.")
709 (defconst vip-buffer-search-char nil
710 "*Key bound for buffer-searching.")
712 (defconst vip-search-wrap-around-t t
713 "*If t, search wraps around.")
715 (vip-deflocalvar vip-related-files-and-buffers-ring nil
716 "*Ring of file and buffer names that are considered to be related to the
718 These buffers can be cycled through via :R and :P commands.")
719 (put 'vip-related-files-and-buffers-ring
'permanent-local t
)
721 ;; Used to find out if we are done with searching the current buffer.
722 (vip-deflocalvar vip-local-search-start-marker nil
)
723 ;; As above, but global
724 (defvar vip-search-start-marker
(make-marker))
726 ;; the search overlay
727 (vip-deflocalvar vip-search-overlay nil
)
730 (defvar vip-heading-start
731 (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
732 "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
733 "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
734 "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
735 "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
737 "*Regexps for Headings. Used by \[\[ and \]\].")
739 (defvar vip-heading-end
740 (concat "^}\\|" ; C/C++
741 "^\\\\end{\\|" ; latex
742 "^@end \\|" ; texinfo
743 ")\n\n[ \t\n]*\\|" ; lisp
744 "\\.\\s-*$") ; prolog
745 "*Regexps to end Headings/Sections. Used by \[\].")
748 ;; These two vars control the interaction of jumps performed by ' and `.
749 ;; In this new version, '' doesn't erase the marks set by ``, so one can
750 ;; use both kinds of jumps interchangeably and without loosing positions
753 ;; Remembers position of the last jump done using ``'.
754 (vip-deflocalvar vip-last-jump nil
)
755 ;; Remembers position of the last jump done using `''.
756 (vip-deflocalvar vip-last-jump-ignore
0)
758 ;; Some common error messages
760 (defconst vip-SpuriousText
"Spurious text after command" "")
761 (defconst vip-BadExCommand
"Not an editor command" "")
762 (defconst vip-InvalidCommandArgument
"Invalid command argument" "")
763 (defconst vip-NoPrevSearch
"No previous search string" "")
764 (defconst vip-EmptyRegister
"`%c': Nothing in this register" "")
765 (defconst vip-InvalidRegister
"`%c': Invalid register" "")
766 (defconst vip-EmptyTextmarker
"`%c': Text marker doesn't point anywhere" "")
767 (defconst vip-InvalidTextmarker
"`%c': Invalid text marker" "")
768 (defconst vip-InvalidViCommand
"Invalid command" "")
769 (defconst vip-BadAddress
"Ill-formed address" "")
770 (defconst vip-FirstAddrExceedsSecond
"First address exceeds second" "")
771 (defconst vip-NoFileSpecified
"No file specified" "")
776 ;; History of search strings.
777 (defvar vip-search-history
(list ""))
778 ;; History of query-replace strings used as a source.
779 (defvar vip-replace1-history nil
)
780 ;; History of query-replace strings used as replacement.
781 (defvar vip-replace2-history nil
)
782 ;; History of region quoting strings.
783 (defvar vip-quote-region-history
(list vip-quote-string
))
784 ;; History of Ex-style commands.
785 (defvar vip-ex-history nil
)
786 ;; History of shell commands.
787 (defvar vip-shell-history nil
)
790 ;; Last shell command. There are two of these, one for Ex (in viper-ex)
793 ;; Last shell command executed with ! command.
794 (defvar vip-last-shell-com nil
)
800 ;; don't bark when mark is inactive
801 (setq mark-even-if-inactive t
)
803 (defvar vip-inhibit-startup-message nil
804 "Whether Viper startup message should be inhibited.")
807 "t means, arrange that vi-state will be a default.")
809 (defvar vip-custom-file-name
"~/.vip"
810 "Viper customisation file.
811 This variable must be set _before_ loading Viper.")
813 (defvar vip-spell-function
'ispell-region
814 "Spell function used by #s<move> command to spell.")
816 (defvar vip-tags-file-name
"TAGS"
817 "The tags file used by Viper.")
821 (defvar vip-vi-style-in-minibuffer t
822 "If t, use vi-style editing in minibuffer.
823 Should be set in `~/.vip' file.")
825 ;; overlay used in the minibuffer to indicate which state it is in
826 (vip-deflocalvar vip-minibuffer-overlay nil
)
828 ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
829 ;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
830 ;; *after* exiting the minibuffer
831 (defvar vip-minibuffer-exit-hook nil
)
833 (vip-deflocalvar vip-vi-minibuffer-minor-mode nil
834 "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
835 (vip-deflocalvar vip-insert-minibuffer-minor-mode nil
836 "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
838 ;; setup emacs-supported vi-style feel
839 (setq next-line-add-newlines nil
840 require-final-newline t
)
842 (make-variable-buffer-local 'require-final-newline
)
846 (defconst vip-vi-state-id
"<V> "
847 "Mode line tag identifying the Vi mode of Viper.")
848 (defconst vip-emacs-state-id
"<E> "
849 "Mode line tag identifying the Emacs mode of Viper.")
850 (defconst vip-insert-state-id
"<I> "
851 "Mode line tag identifying the Insert mode of Viper.")
852 (defconst vip-replace-state-id
"<R> "
853 "Mode line tag identifying the Replace mode of Viper.")
855 ;; Viper changes the default mode-line-buffer-identification
856 (setq-default mode-line-buffer-identification
'(" %b"))
858 ;; Variable displaying the current Viper state in the mode line.
859 (vip-deflocalvar vip-mode-string vip-emacs-state-id
)
860 (or (memq 'vip-mode-string global-mode-string
)
861 (setq global-mode-string
862 (append '("" vip-mode-string
) (cdr global-mode-string
))))
865 (defvar vip-vi-state-hook nil
866 "*Hooks run just before the switch to Vi mode is completed.")
867 (defvar vip-insert-state-hook nil
868 "*Hooks run just before the switch to Insert mode is completed.")
869 (defvar vip-replace-state-hook nil
870 "*Hooks run just before the switch to Replace mode is completed.")
871 (defvar vip-emacs-state-hook nil
872 "*Hooks run just before the switch to Emacs mode is completed.")
874 (defvar vip-load-hook nil
875 "Hooks run just after loading Viper.")
878 ;; Generic predicates
880 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
882 ;; generate test functions
883 ;; given symbol foo, foo-p is the test function, foos is the set of
884 ;; Viper command keys
885 ;; (macroexpand '(vip-test-com-defun foo))
886 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
888 (defmacro vip-test-com-defun
(name)
889 (let* ((snm (symbol-name name
))
890 (nm-p (intern (concat snm
"-p")))
891 (nms (intern (concat snm
"s"))))
892 (` (defun (, nm-p
) (com)
893 (consp (memq (if (< com
0) (- com
) com
) (, nms
)))))))
895 ;; Variables for defining VI commands
897 ;; Modifying commands that can be prefixes to movement commands
898 (defconst vip-prefix-commands
'(?c ?d ?y ?
! ?
= ?
# ?
< ?
> ?
\"))
899 (vip-test-com-defun vip-prefix-command)
901 ;; Commands that are pairs eg. dd. r and R here are a hack
902 (defconst vip-charpair-commands
'(?c ?d ?y ?
! ?
= ?
< ?
> ?r ?R
))
903 (vip-test-com-defun vip-charpair-command)
905 (defconst vip-movement-commands
'(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
906 ?H ?M ?n ?t ?T ?w ?W ?$ ?%
907 ?^ ?
( ?
) ?- ?
+ ?| ?
{ ?
} ?
[ ?
] ?
' ?
`
911 (vip-test-com-defun vip-movement-command)
913 ;; Commands that can be repeated by .(dotted)
914 (defconst vip-dotable-commands
'(?c ?d ?C ?D ?
> ?
<))
915 (vip-test-com-defun vip-dotable-command)
917 ;; Commands that can follow a #
918 (defconst vip-hash-cmds
'(?c ?C ?g ?q ?S
))
919 (vip-test-com-defun vip-hash-cmd)
921 ;; Commands that may have registers as prefix
922 (defconst vip-regsuffix-commands
'(?d ?y ?Y ?D ?p ?P ?x ?X
))
923 (vip-test-com-defun vip-regsuffix-command)
927 ;;; Arrange the keymaps
928 (require 'viper-keym
)
935 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
936 (defun vip-change-state (new-state)
937 ;; Keep vip-post/pre-command-hooks fresh.
938 ;; We remove then add vip-post/pre-command-sentinel since it is very
939 ;; desirable that noone gets in-between
940 (remove-hook 'post-command-hook
'vip-post-command-sentinel
)
941 (add-hook 'post-command-hook
'vip-post-command-sentinel
)
942 (remove-hook 'pre-command-hook
'vip-pre-command-sentinel
)
943 (add-hook 'pre-command-hook
'vip-pre-command-sentinel t
)
944 ;; These hooks will be added back if switching to insert/replace mode
945 (vip-remove-hook 'vip-post-command-hooks
946 'vip-insert-state-post-command-sentinel
)
947 (vip-remove-hook 'vip-pre-command-hooks
948 'vip-insert-state-pre-command-sentinel
)
949 (cond ((eq new-state
'vi-state
)
950 (cond ((member vip-current-state
'(insert-state replace-state
))
952 ;; move vip-last-posn-while-in-insert-state
953 ;; This is a normal hook that is executed in insert/replace
954 ;; states after each command. In Vi/Emacs state, it does
955 ;; nothing. We need to execute it here to make sure that
956 ;; the last posn was recorded when we hit ESC.
957 ;; It may be left unrecorded if the last thing done in
958 ;; insert/repl state was dabbrev-expansion or abbrev
959 ;; expansion caused by hitting ESC
960 (vip-insert-state-post-command-sentinel)
962 (condition-case conds
964 (vip-save-last-insertion
966 vip-last-posn-while-in-insert-state
)
967 (if vip-began-as-replace
968 (setq vip-began-as-replace nil
)
969 ;; repeat insert commands if numerical arg > 1
971 (vip-repeat-insert-command))))
973 (vip-message-conditions conds
)))
975 (if (> (length vip-last-insertion
) 0)
976 (vip-push-onto-ring vip-last-insertion
977 'vip-insertion-ring
))
979 (if vip-ex-style-editing-in-insert
980 (or (bolp) (backward-char 1))))
984 ((memq new-state
'(insert-state replace-state
))
985 (if (memq vip-current-state
'(emacs-state vi-state
))
986 (vip-move-marker-locally 'vip-insert-point
(point)))
987 (vip-move-marker-locally 'vip-last-posn-while-in-insert-state
(point))
988 (vip-add-hook 'vip-post-command-hooks
989 'vip-insert-state-post-command-sentinel t
)
990 (vip-add-hook 'vip-pre-command-hooks
991 'vip-insert-state-pre-command-sentinel t
))
994 ;; Nothing needs to be done to switch to emacs mode! Just set some
995 ;; variables, which is already done in vip-change-state-to-emacs!
997 (setq vip-current-state new-state
)
998 (vip-normalize-minor-mode-map-alist)
999 (vip-adjust-keys-for new-state
)
1000 (vip-set-mode-vars-for new-state
)
1001 (vip-refresh-mode-line)
1006 (defun vip-adjust-keys-for (state)
1007 "Make necessary adjustments to keymaps before entering STATE."
1008 (cond ((memq state
'(insert-state replace-state
))
1011 (define-key vip-insert-basic-map
"\C-m" 'vip-autoindent
)
1012 (if vip-want-emacs-keys-in-insert
1014 (define-key vip-insert-basic-map
"\C-j" nil
)
1016 (define-key vip-insert-basic-map
"\C-j" 'vip-autoindent
))))
1018 (setq vip-insert-diehard-minor-mode
1019 (not vip-want-emacs-keys-in-insert
))
1021 (if vip-want-ctl-h-help
1023 (define-key vip-insert-basic-map
"\C-h" 'help-command
)
1024 (define-key vip-replace-map
"\C-h" 'help-command
))
1025 (define-key vip-insert-basic-map
1026 "\C-h" 'vip-del-backward-char-in-insert
)
1027 (define-key vip-replace-map
1028 "\C-h" 'vip-del-backward-char-in-replace
)))
1031 (setq vip-vi-diehard-minor-mode
(not vip-want-emacs-keys-in-vi
))
1032 (if vip-want-ctl-h-help
1033 (define-key vip-vi-basic-map
"\C-h" 'help-command
)
1034 (define-key vip-vi-basic-map
"\C-h" 'vip-backward-char
)))
1038 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
1039 ;; This ensures that Viper bindings are in effect, regardless of which minor
1040 ;; modes were turned on by the user or by other packages.
1041 (defun vip-normalize-minor-mode-map-alist ()
1042 (setq minor-mode-map-alist
1043 (vip-append-filter-alist
1045 (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map
)
1046 (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map
)
1047 (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map
)
1048 (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map
)
1049 (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map
)
1050 (cons 'vip-vi-state-modifier-minor-mode
1052 (cdr (assoc major-mode vip-vi-state-modifier-alist
)))
1053 (cdr (assoc major-mode vip-vi-state-modifier-alist
))
1055 (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map
)
1056 (cons 'vip-vi-basic-minor-mode vip-vi-basic-map
)
1057 (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map
)
1058 (cons 'vip-replace-minor-mode vip-replace-map
)
1059 ;; vip-insert-minibuffer-minor-mode must come after
1060 ;; vip-replace-minor-mode
1061 (cons 'vip-insert-minibuffer-minor-mode
1063 (cons 'vip-insert-local-user-minor-mode
1064 vip-insert-local-user-map
)
1065 (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map
)
1066 (cons 'vip-insert-global-user-minor-mode
1067 vip-insert-global-user-map
)
1068 (cons 'vip-insert-state-modifier-minor-mode
1071 (assoc major-mode vip-insert-state-modifier-alist
)))
1073 (assoc major-mode vip-insert-state-modifier-alist
))
1075 (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map
)
1076 (cons 'vip-insert-basic-minor-mode vip-insert-basic-map
)
1077 (cons 'vip-emacs-intercept-minor-mode
1078 vip-emacs-intercept-map
)
1079 (cons 'vip-emacs-local-user-minor-mode
1080 vip-emacs-local-user-map
)
1081 (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map
)
1082 (cons 'vip-emacs-global-user-minor-mode
1083 vip-emacs-global-user-map
)
1084 (cons 'vip-emacs-state-modifier-minor-mode
1087 (assoc major-mode vip-emacs-state-modifier-alist
)))
1089 (assoc major-mode vip-emacs-state-modifier-alist
))
1092 minor-mode-map-alist
)))
1098 ;; Viper mode-changing commands and utilities
1100 ;; Modifies mode-line-buffer-identification.
1101 (defun vip-refresh-mode-line ()
1102 (setq vip-mode-string
1103 (cond ((eq vip-current-state
'emacs-state
) vip-emacs-state-id
)
1104 ((eq vip-current-state
'vi-state
) vip-vi-state-id
)
1105 ((eq vip-current-state
'replace-state
) vip-replace-state-id
)
1106 ((eq vip-current-state
'insert-state
) vip-insert-state-id
)))
1108 ;; Sets Viper mode string in global-mode-string
1109 (force-mode-line-update))
1112 (defun viper-mode ()
1113 "Turn on Viper emulation of Vi."
1115 (if (not noninteractive
)
1117 (if vip-first-time
; This check is important. Without it, startup and
1118 (progn ; expert-level msgs mix up when viper-mode recurses
1119 (setq vip-first-time nil
)
1120 (if (not vip-inhibit-startup-message
)
1121 (save-window-excursion
1122 (setq vip-inhibit-startup-message t
)
1123 (delete-other-windows)
1124 (switch-to-buffer "Viper Startup Message")
1127 (substitute-command-keys
1128 "Viper Is a Package for Emacs Rebels.
1129 It is also a VI Plan for Emacs Rescue and a venomous VI PERil.
1131 Technically speaking, Viper is a Vi emulation package for GNU Emacs 19 and
1132 XEmacs 19. It supports virtually all of Vi and Ex functionality, extending
1133 and improving upon much of it.
1135 1. Viper supports Vi at several levels. Level 1 is the closest to
1136 Vi, level 5 provides the most flexibility to depart from many Vi
1139 You will be asked to specify your user level in a following screen.
1141 If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will
1142 behave as in VI, to smooth transition to Viper for the beginners.
1143 However, to use Emacs productively, you are advised to reach user
1146 If your user level is 2 or higher, ^X and ^C will invoke Emacs
1147 functions,as usual in Emacs; ^Z will toggle vi/emacs modes, and
1148 ^G will be the usual Emacs's keyboard-quit (something like ^C in VI).
1150 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
1151 do not cause Emacs to quit, except at user level 1 (a novice).
1152 3. ^X^C EXITS EMACS.
1153 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
1154 undo. Another `u' changes direction.
1156 6. Emacs Meta functions are invoked by typing `_' or `\\ ESC'.
1157 On a window system, the best way is to use the Meta-key.
1158 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,
1159 if something funny happens. This would abort the current editing
1162 You can get more information on Viper by:
1164 a. Typing `:help' in Vi state
1165 b. Printing Viper manual, found in ./etc/viper.dvi
1166 c. Printing ViperCard, the Quick Reference, found in ./etc/viperCard.dvi
1168 This startup message appears whenever you load Viper, unless you type `y' now."
1170 (goto-char (point-min))
1171 (if (y-or-n-p "Inhibit Viper startup message? ")
1173 'vip-inhibit-startup-message
1174 "Viper startup message inhibited"
1175 vip-custom-file-name t
))
1176 (kill-buffer (current-buffer))))
1178 (vip-set-expert-level 'dont-change-unless
)))
1179 (vip-change-state-to-vi))))
1182 (defalias 'vip-mode
'viper-mode
)
1185 ;; Switch from Insert state to Vi state.
1186 (defun vip-exit-insert-state ()
1188 (vip-change-state-to-vi))
1190 (defun vip-set-mode-vars-for (state)
1191 "Sets Viper minor mode variables to put Viper's state STATE in effect."
1194 (setq vip-vi-minibuffer-minor-mode nil
1195 vip-insert-minibuffer-minor-mode nil
1196 vip-vi-intercept-minor-mode nil
1197 vip-insert-intercept-minor-mode nil
1199 vip-vi-local-user-minor-mode nil
1200 vip-vi-kbd-minor-mode nil
1201 vip-vi-global-user-minor-mode nil
1202 vip-vi-state-modifier-minor-mode nil
1203 vip-vi-diehard-minor-mode nil
1204 vip-vi-basic-minor-mode nil
1206 vip-replace-minor-mode nil
1208 vip-insert-local-user-minor-mode nil
1209 vip-insert-kbd-minor-mode nil
1210 vip-insert-global-user-minor-mode nil
1211 vip-insert-state-modifier-minor-mode nil
1212 vip-insert-diehard-minor-mode nil
1213 vip-insert-basic-minor-mode nil
1214 vip-emacs-intercept-minor-mode t
1215 vip-emacs-local-user-minor-mode t
1216 vip-emacs-kbd-minor-mode
(not (vip-is-in-minibuffer))
1217 vip-emacs-global-user-minor-mode t
1218 vip-emacs-state-modifier-minor-mode t
1222 (if (eq state
'vi-state
) ; adjust for vi-state
1224 vip-vi-intercept-minor-mode t
1225 vip-vi-minibuffer-minor-mode
(vip-is-in-minibuffer)
1226 vip-vi-local-user-minor-mode t
1227 vip-vi-kbd-minor-mode
(not (vip-is-in-minibuffer))
1228 vip-vi-global-user-minor-mode t
1229 vip-vi-state-modifier-minor-mode t
1230 ;; don't let the diehard keymap block command completion
1231 ;; and other things in the minibuffer
1232 vip-vi-diehard-minor-mode
(not
1233 (or vip-want-emacs-keys-in-vi
1234 (vip-is-in-minibuffer)))
1235 vip-vi-basic-minor-mode t
1236 vip-emacs-intercept-minor-mode nil
1237 vip-emacs-local-user-minor-mode nil
1238 vip-emacs-kbd-minor-mode nil
1239 vip-emacs-global-user-minor-mode nil
1240 vip-emacs-state-modifier-minor-mode nil
1243 ;; Insert and Replace states
1244 (if (member state
'(insert-state replace-state
))
1246 vip-insert-intercept-minor-mode t
1247 vip-replace-minor-mode
(eq state
'replace-state
)
1248 vip-insert-minibuffer-minor-mode
(vip-is-in-minibuffer)
1249 vip-insert-local-user-minor-mode t
1250 vip-insert-kbd-minor-mode
(not (vip-is-in-minibuffer))
1251 vip-insert-global-user-minor-mode t
1252 vip-insert-state-modifier-minor-mode t
1253 ;; don't let the diehard keymap block command completion
1254 ;; and other things in the minibuffer
1255 vip-insert-diehard-minor-mode
(not
1256 (or vip-want-emacs-keys-in-insert
1257 (vip-is-in-minibuffer)))
1258 vip-insert-basic-minor-mode t
1259 vip-emacs-intercept-minor-mode nil
1260 vip-emacs-local-user-minor-mode nil
1261 vip-emacs-kbd-minor-mode nil
1262 vip-emacs-global-user-minor-mode nil
1263 vip-emacs-state-modifier-minor-mode nil
1267 (if (vip-window-display-p)
1268 (setq vip-minibuffer-current-face
1269 (cond ((eq state
'emacs-state
) vip-minibuffer-emacs-face
)
1270 ((eq state
'vi-state
) vip-minibuffer-vi-face
)
1271 ((memq state
'(insert-state replace-state
))
1272 vip-minibuffer-insert-face
))))
1274 (if (vip-is-in-minibuffer)
1275 (vip-set-minibuffer-overlay))
1278 ;; This also takes care of the annoying incomplete lines in files.
1279 ;; Also, this fixes `undo' to work vi-style for complex commands.
1280 (defun vip-change-state-to-vi ()
1281 "Change Viper state to Vi."
1283 (if (and vip-first-time
(not (vip-is-in-minibuffer)))
1285 (if overwrite-mode
(overwrite-mode nil
))
1286 (if abbrev-mode
(expand-abbrev))
1287 (if (and auto-fill-function
(> (current-column) fill-column
))
1288 (funcall auto-fill-function
))
1289 ;; don't leave whitespace lines around
1290 (if (and (memq last-command
1292 vip-open-line vip-Open-line
1293 vip-replace-state-exit-cmd
))
1294 (vip-over-whitespace-line))
1295 (indent-to-left-margin))
1296 (vip-add-newline-at-eob-if-necessary)
1297 (if vip-undo-needs-adjustment
(vip-adjust-undo))
1298 (vip-change-state 'vi-state
)
1300 ;; always turn off iso-accents-mode, or else we won't be able to use the
1301 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
1302 (if (and (boundp 'iso-accents-mode
) iso-accents-mode
)
1303 (iso-accents-mode -
1))
1305 ;; Protection against user errors in hooks
1306 (condition-case conds
1307 (run-hooks 'vip-vi-state-hook
)
1309 (vip-message-conditions conds
)))))
1311 (defun vip-change-state-to-insert ()
1312 "Change Viper state to Insert."
1314 (vip-change-state 'insert-state
)
1315 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
1316 (iso-accents-mode 1)) ; turn iso accents on
1318 ;; Protection against user errors in hooks
1319 (condition-case conds
1320 (run-hooks 'vip-insert-state-hook
)
1322 (vip-message-conditions conds
))))
1324 (defsubst vip-downgrade-to-insert
()
1325 (setq vip-current-state
'insert-state
1326 vip-replace-minor-mode nil
)
1331 ;; Change to replace state. When the end of replacement region is reached,
1332 ;; replace state changes to insert state.
1333 (defun vip-change-state-to-replace (&optional non-R-cmd
)
1334 (vip-change-state 'replace-state
)
1335 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
1336 (iso-accents-mode 1)) ; turn iso accents on
1337 ;; Run insert-state-hook
1338 (condition-case conds
1339 (run-hooks 'vip-insert-state-hook
'vip-replace-state-hook
)
1341 (vip-message-conditions conds
)))
1345 ;; 'R' is implemented using Emacs's overwrite-mode
1350 (defun vip-change-state-to-emacs ()
1351 "Change Viper state to Emacs."
1353 (vip-change-state 'emacs-state
)
1354 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
1355 (iso-accents-mode 1)) ; turn iso accents on
1357 ;; Protection agains user errors in hooks
1358 (condition-case conds
1359 (run-hooks 'vip-emacs-state-hook
)
1361 (vip-message-conditions conds
))))
1363 ;; escape to emacs mode termporarily
1364 (defun vip-escape-to-emacs (arg &optional events
)
1365 "Escape to Emacs state from Vi state for one Emacs command.
1366 ARG is used as the prefix value for the executed command. If
1367 EVENTS is a list of events, which become the beginning of the command."
1369 (vip-escape-to-state arg events
'emacs-state
))
1371 ;; escape to Vi mode termporarily
1372 (defun vip-escape-to-vi ()
1373 "Escape from Emacs state to Vi state for one Vi 1-character command.
1374 This doesn't work with prefix arguments or most complex commands like
1375 cw, dw, etc. But it does work with some 2-character commands,
1378 (vip-escape-to-state nil nil
'vi-state
))
1380 ;; Escape to STATE mode for one Emacs command.
1381 (defun vip-escape-to-state (arg events state
)
1382 (let (com key prefix-arg
)
1383 ;; this temporarily turns off Viper's minor mode keymaps
1384 (vip-set-mode-vars-for state
)
1385 (vip-normalize-minor-mode-map-alist)
1386 (if events
(vip-set-unread-command-events events
))
1388 ;; protect against keyboard quit and other errors
1393 (setq com
(key-binding (setq key
1395 (read-key-sequence nil
)
1396 (read-key-sequence nil t
)))))
1397 ;; In case of indirection--chase definitions.
1398 ;; Have to do it here because we execute this command under
1399 ;; different keymaps, so command-execute may not do the
1400 ;; right thing there
1401 (while (vectorp com
) (setq com
(key-binding com
))))
1403 ;; exec command in the right Viper state
1404 ;; otherwise, if we switch buffers in the escaped command,
1405 ;; Viper's mode vars will remain those of `state'. When we return
1406 ;; to the orig buffer, the bindings will be screwed up.
1407 (vip-set-mode-vars-for vip-current-state
)
1409 ;; this-command, last-command-char, last-command-event
1410 (setq this-command com
)
1411 (if vip-xemacs-p
; XEmacs represents key sequences as vectors
1412 (setq last-command-event
(vip-seq-last-elt key
)
1413 last-command-char
(event-to-character last-command-event
))
1414 ;; Emacs represents them as sequences (str or vec)
1415 (setq last-command-event
(vip-seq-last-elt key
)
1416 last-command-char last-command-event
))
1420 (setq prefix-arg arg
)
1421 (command-execute com
)))
1425 (vip-set-mode-vars-for vip-current-state
)) ; set state in new buffer
1427 (defun vip-exec-form-in-emacs (form)
1428 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
1429 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
1430 (let ((buff (current-buffer))
1432 (vip-set-mode-vars-for 'emacs-state
)
1433 (setq result
(eval form
))
1434 (if (not (equal buff
(current-buffer))) ; cmd switched buffer
1437 (vip-set-mode-vars-for vip-current-state
)))
1438 (vip-set-mode-vars-for vip-current-state
)
1442 ;; This is needed because minor modes sometimes override essential Viper
1443 ;; bindings. By letting Viper know which files these modes are in, it will
1444 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
1445 (defun vip-harness-minor-mode (load-file)
1446 "Familiarize Viper with a minor mode defined in LOAD_FILE.
1447 Minor modes that have their own keymaps may overshadow Viper keymaps.
1448 This function is designed to make Viper aware of the packages that define
1451 (vip-harness-minor-mode load-file)
1453 LOAD-FILE is a name of the file where the specific minor mode is defined.
1454 Suffixes such as .el or .elc should be stripped."
1456 (interactive "sEnter name of the load file: ")
1458 (vip-eval-after-load load-file
'(vip-normalize-minor-mode-map-alist))
1460 ;; Change the default for minor-mode-map-alist each time a harnessed minor
1461 ;; mode adds its own keymap to the a-list.
1462 (vip-eval-after-load
1463 load-file
'(setq-default minor-mode-map-alist minor-mode-map-alist
))
1467 (defun vip-ESC (arg)
1468 "Emulate ESC key in Emacs.
1469 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true. In that
1470 case \@ will be bound to ESC. If vip-no-multiple-ESC is 'twice double ESC
1471 would dings in vi-state. Other ESC sequences are emulated via the current
1472 Emacs's major mode keymap. This is more convenient on dumb terminals and in
1473 Emacs -nw, since this won't block functional keys such as up,down,
1474 etc. Meta key also will work. When vip-no-multiple-ESC is nil, ESC key
1475 behaves as in Emacs, any number of multiple escapes is allowed."
1478 (cond ((and (not vip-no-multiple-ESC
) (eq vip-current-state
'vi-state
))
1479 (setq char
(vip-read-char-exclusive))
1480 (vip-escape-to-emacs arg
(list ?\e char
) ))
1481 ((and (eq vip-no-multiple-ESC
'twice
)
1482 (eq vip-current-state
'vi-state
))
1483 (setq char
(vip-read-char-exclusive))
1484 (if (= char
(string-to-char vip-ESC-key
))
1486 (vip-escape-to-emacs arg
(list ?\e char
) )))
1490 (defun vip-alternate-ESC (arg)
1491 "ESC key without checking for multiple keystrokes."
1493 (vip-escape-to-emacs arg
'(?\e
)))
1496 ;; Intercept ESC sequences on dumb terminals.
1497 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
1499 ;; Check if last key was ESC and if so try to reread it as a function key.
1500 ;; But only if there are characters to read during a very short time.
1501 ;; Returns the last event, if any.
1502 (defun vip-envelop-ESC-key ()
1503 (let ((event last-input-event
)
1506 (if (vip-ESC-event-p event)
1508 (if (vip-fast-keysequence-p)
1510 (let (minor-mode-map-alist)
1511 (vip-set-unread-command-events event)
1514 (ad-get-orig-definition 'read-key-sequence) nil))
1516 ;; If keyseq translates into something that still has ESC
1517 ;; at the beginning, separate ESC from the rest of the seq.
1518 ;; In XEmacs we check for events that are keypress meta-key
1519 ;; and convert them into [escape key]
1521 ;; This is needed for the following reason:
1522 ;; If ESC is the first symbol, we interpret it as if the
1523 ;; user typed ESC and then quickly some other symbols.
1524 ;; If ESC is not the first one, then the key sequence
1525 ;; entered was apparently translated into a function key or
1526 ;; something (e.g., one may have
1527 ;; (define-key function-key-map "\e[192z" [f11])
1528 ;; which would translate the escape-sequence generated by
1529 ;; f11 in an xterm window into the symbolic key f11.
1531 ;; If `first-key' is not an ESC event, we make it into the
1532 ;; last-command-event in order to pretend that this key was
1533 ;; pressed. This is needed to allow arrow keys to be bound to
1534 ;; macros. Otherwise, vip-exec-mapped-kbd-macro will think that
1535 ;; the last event was ESC and so it'll execute whatever is
1536 ;; bound to ESC. (Viper macros can't be bound to
1538 (let* ((first-key (elt keyseq 0))
1539 (key-mod (event-modifiers first-key)))
1540 (cond ((vip-ESC-event-p first-key)
1541 ;; put keys following ESC on the unread list
1542 ;; and return ESC as the key-sequence
1543 (vip-set-unread-command-events (subseq keyseq 1))
1544 (setq last-input-event event
1545 keyseq (if vip-emacs-p
1547 (vector (character-to-event ?\e)))))
1549 (key-press-event-p first-key)
1550 (equal '(meta) key-mod))
1551 (vip-set-unread-command-events
1553 (character-to-event (event-key first-key)))
1555 (setq last-input-event event
1556 keyseq (vector (character-to-event ?\e))))
1558 (setq last-command-event first-key))
1562 ;; this is escape event with nothing after it
1563 ;; put in unread-command-event and then re-read
1564 (vip-set-unread-command-events event)
1566 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
1568 ;; not an escape event
1569 (setq keyseq (vector event)))
1574 (defadvice read-key-sequence (around vip-read-keyseq-ad activate)
1575 "Harness to work for Viper. This advice is harmless---don't worry!"
1576 (let (inhibit-quit event keyseq)
1577 (setq keyseq ad-do-it)
1578 (setq event (if vip-xemacs-p
1579 (elt keyseq 0) ; XEmacs returns vector of events
1580 (elt (listify-key-sequence keyseq) 0)))
1581 (if (vip-ESC-event-p event)
1582 (let (unread-command-events)
1583 (vip-set-unread-command-events keyseq)
1584 (if (vip-fast-keysequence-p)
1585 (let ((vip-vi-global-user-minor-mode nil)
1586 (vip-vi-local-user-minor-mode nil)
1587 (vip-replace-minor-mode nil) ; actually unnecessary
1588 (vip-insert-global-user-minor-mode nil)
1589 (vip-insert-local-user-minor-mode nil))
1590 (setq keyseq ad-do-it))
1591 (setq keyseq ad-do-it))))
1594 (defadvice describe-key (before vip-read-keyseq-ad protect activate)
1595 "Force to read key via `read-key-sequence'."
1596 (interactive (list (vip-events-to-keys
1597 (read-key-sequence "Describe key: ")))))
1599 (defadvice describe-key-briefly (before vip-read-keyseq-ad protect activate)
1600 "Force to read key via `read-key-sequence'."
1601 (interactive (list (vip-events-to-keys
1602 (read-key-sequence "Describe key briefly: ")))))
1604 ;; Listen to ESC key.
1605 ;; If a sequence of keys starting with ESC is issued with very short delays,
1606 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
1607 (defun vip-intercept-ESC-key ()
1608 "Function that implements ESC key in Viper emulation of Vi."
1610 (let ((cmd (or (key-binding (vip-envelop-ESC-key))
1611 '(lambda () (interactive) (error "")))))
1613 ;; call the actual function to execute ESC (if no other symbols followed)
1614 ;; or the key bound to the ESC sequence (if the sequence was issued
1615 ;; with very short delay between characters.
1616 (if (eq cmd 'vip-intercept-ESC-key)
1618 (cond ((eq vip-current-state 'vi-state)
1620 ((eq vip-current-state 'insert-state)
1621 'vip-exit-insert-state)
1622 ((eq vip-current-state 'replace-state)
1623 'vip-replace-state-exit-cmd)
1624 (t 'vip-change-state-to-vi)
1626 (call-interactively cmd)))
1630 ;; prefix argument for Vi mode
1632 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
1633 ;; represents the numeric value of the prefix argument and COM represents
1634 ;; command prefix such as "c", "d", "m" and "y".
1636 ;; Compute numeric prefix arg value.
1637 ;; Invoked by CHAR. COM is the command part obtained so far.
1638 (defun vip-prefix-arg-value (event com)
1640 ;; read while number
1641 (while (and (numberp event) (>= event ?0) (<= event ?9))
1642 (setq value (+ (* (if (numberp value) value 0) 10) (- event ?0)))
1643 (setq event (vip-read-event-convert-to-char)))
1645 (setq prefix-arg value)
1646 (if com (setq prefix-arg (cons prefix-arg com)))
1647 (while (eq event ?U)
1648 (vip-describe-arg prefix-arg)
1649 (setq event (vip-read-event-convert-to-char)))
1650 (vip-set-unread-command-events event)))
1652 ;; Vi operator as prefix argument."
1653 (defun vip-prefix-arg-com (char value com)
1657 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1658 vip-buffer-search-char)))
1660 ;; this means that we already have a command character, so we
1661 ;; construct a com list and exit while. however, if char is "
1664 ;; new com is (CHAR . OLDCOM)
1665 (if (memq char '(?# ?\")) (error ""))
1666 (setq com (cons char com))
1668 ;; If com is nil we set com as char, and read more. Again, if char
1669 ;; is ", we read the name of register and store it in vip-use-register.
1670 ;; if char is !, =, or #, a complete com is formed so we exit the
1672 (cond ((memq char '(?! ?=))
1674 (setq char (read-char))
1677 ;; read a char and encode it as com
1678 (setq com (+ 128 (read-char)))
1679 (setq char (read-char)))
1681 (let ((reg (read-char)))
1682 (if (vip-valid-register reg)
1683 (setq vip-use-register reg)
1685 (setq char (read-char))))
1688 (setq char (vip-read-char-exclusive)))))))
1690 ;; com is a single char, so we construct prefix-arg
1691 ;; and if char is ?, describe prefix arg, otherwise exit by
1692 ;; pushing the char back
1694 (setq prefix-arg (cons value com))
1696 (vip-describe-arg prefix-arg)
1697 (setq char (read-char)))
1698 (vip-set-unread-command-events char)
1700 ;; as com is non-nil, this means that we have a command to execute
1701 (if (memq (car com) '(?r ?R))
1702 ;; execute apropriate region command.
1703 (let ((char (car com)) (com (cdr com)))
1704 (setq prefix-arg (cons value com))
1705 (if (= char ?r) (vip-region prefix-arg)
1706 (vip-Region prefix-arg))
1708 (setq prefix-arg nil))
1709 ;; otherwise, reset prefix arg and call appropriate command
1710 (setq value (if (null value) 1 value))
1711 (setq prefix-arg nil)
1712 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1713 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1714 ((equal com '(?d . ?y)) (vip-yank-defun))
1715 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1716 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1717 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1718 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1719 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1722 (defun vip-describe-arg (arg)
1724 (setq val (vip-P-val arg)
1725 com (vip-getcom arg))
1728 (message "Value is nil, and command is nil")
1729 (message "Value is nil, and command is `%c'" com))
1731 (message "Value is `%d', and command is nil" val)
1732 (message "Value is `%d', and command is `%c'" val com)))))
1734 (defun vip-digit-argument (arg)
1735 "Begin numeric argument for the next command."
1737 (vip-leave-region-active)
1738 (vip-prefix-arg-value last-command-char
1739 (if (consp arg) (cdr arg) nil)))
1741 (defun vip-command-argument (arg)
1742 "Accept a motion command as an argument."
1747 (cond ((null arg) nil)
1748 ((consp arg) (car arg))
1750 (t (error vip-InvalidCommandArgument)))
1751 (cond ((null arg) nil)
1752 ((consp arg) (cdr arg))
1754 (t (error vip-InvalidCommandArgument))))
1755 (quit (setq vip-use-register nil)
1756 (signal 'quit nil)))
1757 (vip-deactivate-mark))
1759 ;; Get value part of prefix-argument ARG.
1760 (defsubst vip-p-val (arg)
1761 (cond ((null arg) 1)
1763 (if (or (null (car arg)) (equal (car arg) '(nil)))
1767 ;; Get raw value part of prefix-argument ARG.
1768 (defsubst vip-P-val (arg)
1769 (cond ((consp arg) (car arg))
1772 ;; Get com part of prefix-argument ARG.
1773 (defsubst vip-getcom (arg)
1774 (cond ((null arg) nil)
1775 ((consp arg) (cdr arg))
1778 ;; Get com part of prefix-argument ARG and modify it.
1779 (defun vip-getCom (arg)
1780 (let ((com (vip-getcom arg)))
1781 (cond ((equal com ?c) ?C)
1787 ;; repeat last destructive command
1789 ;; Append region to text in register REG.
1790 ;; START and END are buffer positions indicating what to append.
1791 (defsubst vip-append-to-register (reg start end)
1792 (set-register reg (concat (if (stringp (get-register reg))
1793 (get-register reg) "")
1794 (buffer-substring start end))))
1796 ;; define functions to be executed
1798 ;; invoked by C command
1799 (defun vip-exec-change (m-com com)
1800 ;; handle C cmd at the eol and at eob.
1801 (if (or (and (eolp) (= vip-com-point (point)))
1802 (= vip-com-point (point-max)))
1804 (insert " ")(backward-char 1)))
1805 (if (= vip-com-point (point))
1806 (vip-forward-char-carefully))
1808 (vip-change vip-com-point (point))
1809 (vip-change-subr vip-com-point (point))))
1811 ;; this is invoked by vip-substitute-line
1812 (defun vip-exec-Change (m-com com)
1814 (set-mark vip-com-point)
1815 (vip-enlarge-region (mark t) (point))
1816 (if vip-use-register
1818 (cond ((vip-valid-register vip-use-register '(letter digit))
1819 ;;(vip-valid-register vip-use-register '(letter)
1821 vip-use-register (mark t) (point) nil))
1822 ((vip-valid-register vip-use-register '(Letter))
1823 (vip-append-to-register
1824 (downcase vip-use-register) (mark t) (point)))
1825 (t (setq vip-use-register nil)
1826 (error vip-InvalidRegister vip-use-register)))
1827 (setq vip-use-register nil)))
1828 (delete-region (mark t) (point)))
1830 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
1832 (defun vip-exec-delete (m-com com)
1833 (if vip-use-register
1835 (cond ((vip-valid-register vip-use-register '(letter digit))
1836 ;;(vip-valid-register vip-use-register '(letter))
1838 vip-use-register vip-com-point (point) nil))
1839 ((vip-valid-register vip-use-register '(Letter))
1840 (vip-append-to-register
1841 (downcase vip-use-register) vip-com-point (point)))
1842 (t (setq vip-use-register nil)
1843 (error vip-InvalidRegister vip-use-register)))
1844 (setq vip-use-register nil)))
1846 (if (eq last-command 'd-command) 'kill-region nil))
1847 (kill-region vip-com-point (point))
1848 (setq this-command 'd-command)
1849 (if vip-ex-style-motion
1850 (if (and (eolp) (not (bolp))) (backward-char 1))))
1852 (defun vip-exec-Delete (m-com com)
1854 (set-mark vip-com-point)
1855 (vip-enlarge-region (mark t) (point))
1856 (if vip-use-register
1858 (cond ((vip-valid-register vip-use-register '(letter digit))
1859 ;;(vip-valid-register vip-use-register '(letter))
1861 vip-use-register (mark t) (point) nil))
1862 ((vip-valid-register vip-use-register '(Letter))
1863 (vip-append-to-register
1864 (downcase vip-use-register) (mark t) (point)))
1865 (t (setq vip-use-register nil)
1866 (error vip-InvalidRegister vip-use-register)))
1867 (setq vip-use-register nil)))
1869 (if (eq last-command 'D-command) 'kill-region nil))
1870 (kill-region (mark t) (point))
1871 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
1872 (back-to-indentation))
1874 (defun vip-exec-yank (m-com com)
1875 (if vip-use-register
1877 (cond ((vip-valid-register vip-use-register '(letter digit))
1878 ;; (vip-valid-register vip-use-register '(letter))
1880 vip-use-register vip-com-point (point) nil))
1881 ((vip-valid-register vip-use-register '(Letter))
1882 (vip-append-to-register
1883 (downcase vip-use-register) vip-com-point (point)))
1884 (t (setq vip-use-register nil)
1885 (error vip-InvalidRegister vip-use-register)))
1886 (setq vip-use-register nil)))
1887 (setq last-command nil)
1888 (copy-region-as-kill vip-com-point (point))
1889 (goto-char vip-com-point))
1891 (defun vip-exec-Yank (m-com com)
1893 (set-mark vip-com-point)
1894 (vip-enlarge-region (mark t) (point))
1895 (if vip-use-register
1897 (cond ((vip-valid-register vip-use-register '(letter digit))
1899 vip-use-register (mark t) (point) nil))
1900 ((vip-valid-register vip-use-register '(Letter))
1901 (vip-append-to-register
1902 (downcase vip-use-register) (mark t) (point)))
1903 (t (setq vip-use-register nil)
1904 (error vip-InvalidRegister vip-use-register)))
1905 (setq vip-use-register nil)))
1906 (setq last-command nil)
1907 (copy-region-as-kill (mark t) (point)))
1908 (vip-deactivate-mark)
1909 (goto-char vip-com-point))
1911 (defun vip-exec-bang (m-com com)
1913 (set-mark vip-com-point)
1914 (vip-enlarge-region (mark t) (point))
1915 (shell-command-on-region
1918 (setq vip-last-shell-com
1919 (vip-read-string-with-history
1923 (car vip-shell-history)
1928 (defun vip-exec-equals (m-com com)
1930 (set-mark vip-com-point)
1931 (vip-enlarge-region (mark t) (point))
1932 (if (> (mark t) (point)) (exchange-point-and-mark))
1933 (indent-region (mark t) (point) nil)))
1935 (defun vip-exec-shift (m-com com)
1937 (set-mark vip-com-point)
1938 (vip-enlarge-region (mark t) (point))
1939 (if (> (mark t) (point)) (exchange-point-and-mark))
1940 (indent-rigidly (mark t) (point)
1943 (- vip-shift-width))))
1944 ;; return point to where it was before shift
1945 (goto-char vip-com-point))
1947 ;; this is needed because some commands fake com by setting it to ?r, which
1948 ;; denotes repeated insert command.
1949 (defsubst vip-exec-dummy (m-com com)
1952 (defun vip-exec-buffer-search (m-com com)
1953 (setq vip-s-string (buffer-substring (point) vip-com-point))
1954 (setq vip-s-forward t)
1955 (setq vip-search-history (cons vip-s-string vip-search-history))
1956 (vip-search vip-s-string vip-s-forward 1))
1958 (defvar vip-exec-array (make-vector 128 nil))
1960 ;; Using a dispatch array allows adding functions like buffer search
1961 ;; without affecting other functions. Buffer search can now be bound
1962 ;; to any character.
1964 (aset vip-exec-array ?c 'vip-exec-change)
1965 (aset vip-exec-array ?C 'vip-exec-Change)
1966 (aset vip-exec-array ?d 'vip-exec-delete)
1967 (aset vip-exec-array ?D 'vip-exec-Delete)
1968 (aset vip-exec-array ?y 'vip-exec-yank)
1969 (aset vip-exec-array ?Y 'vip-exec-Yank)
1970 (aset vip-exec-array ?r 'vip-exec-dummy)
1971 (aset vip-exec-array ?! 'vip-exec-bang)
1972 (aset vip-exec-array ?< 'vip-exec-shift)
1973 (aset vip-exec-array ?> 'vip-exec-shift)
1974 (aset vip-exec-array ?= 'vip-exec-equals)
1978 ;; This function is called by various movement commands to execute a
1979 ;; destructive command on the region specified by the movement command. For
1980 ;; instance, if the user types cw, then the command vip-forward-word will
1981 ;; call vip-execute-com to execute vip-exec-change, which eventually will
1982 ;; call vip-change to invoke the replace mode on the region.
1984 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
1985 ;; vip-d-com for later use by vip-repeat.
1986 (defun vip-execute-com (m-com val com)
1987 (let ((reg vip-use-register))
1988 ;; this is the special command `#'
1990 (vip-special-prefix-com (- com 128))
1991 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
1993 (error "%c: %s" com vip-InvalidViCommand)
1994 (funcall fn m-com com))))
1995 (if (vip-dotable-command-p com)
1996 (vip-set-destructive-command
1998 (if (memq com (list ?c ?C ?!)) (- com) com)
2003 (defun vip-repeat (arg)
2004 "Re-execute last destructive command.
2005 Use the info in vip-d-com, which has the form
2006 \(com val ch reg inserted-text command-keys\),
2007 where `com' is the command to be re-executed, `val' is the
2008 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
2009 if it exists, it is the name of the register for `com'.
2010 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
2012 (let ((save-point (point)) ; save point before repeating prev cmd
2013 ;; Pass along that we are repeating a destructive command
2014 ;; This tells vip-set-destructive-command not to update
2016 (vip-intermediate-command 'vip-repeat))
2017 (if (eq last-command 'vip-undo)
2018 ;; if the last command was vip-undo, then undo-more
2020 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
2021 ;; its prefix value is used as new prefix value for the command.
2022 (let ((m-com (car vip-d-com))
2023 (val (vip-P-val arg))
2024 (com (nth 2 vip-d-com))
2025 (reg (nth 3 vip-d-com)))
2026 (if (null val) (setq val (nth 1 vip-d-com)))
2027 (if (null m-com) (error "No previous command to repeat."))
2028 (setq vip-use-register reg)
2029 (if (nth 4 vip-d-com) ; text inserted by command
2030 (setq vip-last-insertion (nth 4 vip-d-com)
2031 vip-d-char (nth 4 vip-d-com)))
2032 (funcall m-com (cons val com))
2033 (if (and vip-keep-point-on-repeat (< save-point (point)))
2034 (goto-char save-point)) ; go back to before repeat.
2035 (if (and (eolp) (not (bolp)))
2038 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
2039 ;; If the prev cmd was rotating the command ring, this means that `.' has
2040 ;; just executed a command from that ring. So, push it on the ring again.
2041 ;; If we are just executing previous command , then don't push vip-d-com
2042 ;; because vip-d-com is not fully constructed in this case (its keys and
2043 ;; the inserted text may be nil). Besides, in this case, the command
2044 ;; executed by `.' is already on the ring.
2045 (if (eq last-command 'vip-display-current-destructive-command)
2046 (vip-push-onto-ring vip-d-com 'vip-command-ring))
2047 (vip-deactivate-mark)
2050 (defun vip-repeat-from-history ()
2051 "Repeat a destructive command from history.
2052 Doesn't change vip-command-ring in any way, so `.' will work as before
2053 executing this command.
2054 This command is supposed to be bound to a two-character Vi macro where
2055 the second character is a digit 0 to 9. The digit indicates which
2056 history command to execute. `<char>0' is equivalent to `.', `<char>1'
2057 invokes the command before that, etc."
2059 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
2060 (idx (cond (vip-this-kbd-macro
2062 (symbol-name (elt vip-this-kbd-macro 1))))
2065 (vip-d-com vip-d-com))
2067 (or (and (numberp num) (<= 0 num) (<= num 9))
2071 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'"))
2073 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
2074 (setq num (1- num)))
2077 (vip-special-ring-rotate1 vip-command-ring 1)
2078 (setq num (1+ num)))
2082 ;; This command is invoked interactively by the key sequence #<char>
2083 (defun vip-special-prefix-com (char)
2085 (downcase-region (min vip-com-point (point))
2086 (max vip-com-point (point))))
2088 (upcase-region (min vip-com-point (point))
2089 (max vip-com-point (point))))
2091 (push-mark vip-com-point t)
2092 (vip-global-execute))
2094 (push-mark vip-com-point t)
2096 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
2097 (t (error "#%c: %s" char vip-InvalidViCommand))))
2103 "Undo previous change."
2106 (let ((modified (buffer-modified-p))
2107 (before-undo-pt (point-marker))
2108 (after-change-functions after-change-functions)
2109 undo-beg-posn undo-end-posn)
2111 ;; no need to remove this hook, since this var has scope inside a let.
2112 (add-hook 'after-change-functions
2113 '(lambda (beg end len)
2114 (setq undo-beg-posn beg
2115 undo-end-posn (or end beg))))
2119 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
2120 undo-end-posn (or undo-end-posn undo-beg-posn))
2122 (goto-char undo-beg-posn)
2124 (if (and vip-keep-point-on-undo
2125 (pos-visible-in-window-p before-undo-pt))
2127 (push-mark (point-marker) t)
2128 (vip-sit-for-short 300)
2129 (goto-char undo-end-posn)
2130 (vip-sit-for-short 300)
2131 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
2132 (> (abs (- undo-end-posn before-undo-pt)) 1))
2133 (goto-char before-undo-pt)
2134 (goto-char undo-beg-posn)))
2135 (push-mark before-undo-pt t))
2136 (if (and (eolp) (not (bolp))) (backward-char 1))
2137 (if (not modified) (set-buffer-modified-p t)))
2138 (setq this-command 'vip-undo))
2140 ;; Continue undoing previous changes.
2141 (defun vip-undo-more ()
2142 (message "undo more!")
2146 (message "No further undo information in this buffer")))
2147 (if (and (eolp) (not (bolp))) (backward-char 1))
2148 (setq this-command 'vip-undo))
2150 ;; The following two functions are used to set up undo properly.
2151 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
2152 ;; they are undone all at once.
2153 (defun vip-adjust-undo ()
2154 (let ((inhibit-quit t)
2156 (setq vip-undo-needs-adjustment nil)
2157 (if (listp buffer-undo-list)
2158 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
2160 (setq tmp2 (cdr tmp)) ; the part after mark
2162 ;; cut tail from buffer-undo-list temporarily by direct
2163 ;; manipulation with pointers in buffer-undo-list
2166 (setq buffer-undo-list (delq nil buffer-undo-list))
2167 (setq buffer-undo-list
2168 (delq vip-buffer-undo-list-mark buffer-undo-list))
2169 ;; restore tail of buffer-undo-list
2170 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
2171 (setq buffer-undo-list (delq nil buffer-undo-list))))))
2174 (defun vip-set-complex-command-for-undo ()
2175 (if (listp buffer-undo-list)
2176 (if (not vip-undo-needs-adjustment)
2177 (let ((inhibit-quit t))
2178 (setq buffer-undo-list
2179 (cons vip-buffer-undo-list-mark buffer-undo-list))
2180 (setq vip-undo-needs-adjustment t)))))
2185 (defun vip-display-current-destructive-command ()
2186 (let ((text (nth 4 vip-d-com))
2187 (keys (nth 5 vip-d-com))
2190 (setq this-command 'vip-display-current-destructive-command)
2192 (message " `.' runs %s%s"
2193 (concat "`" (vip-array-to-string keys) "'")
2194 (vip-abbreviate-string text max-text-len
2195 " inserting `" "'" " ......."))
2199 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
2200 ;; or in some other way (non-interactively).
2201 (defun vip-set-destructive-command (list)
2202 (or (eq vip-intermediate-command 'vip-repeat)
2204 (setq vip-d-com list)
2205 (setcar (nthcdr 5 vip-d-com)
2206 (vip-array-to-string (this-command-keys)))
2207 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
2209 (defun vip-prev-destructive-command (next)
2210 "Find previous destructive command in the history of destructive commands.
2211 With prefix argument, find next destructive command."
2213 (let (cmd vip-intermediate-command)
2214 (if (eq last-command 'vip-display-current-destructive-command)
2215 ;; repeated search through command history
2216 (setq vip-intermediate-command 'repeating-display-destructive-command)
2217 ;; first search through command history--set temp ring
2218 (setq vip-temp-command-ring (copy-list vip-command-ring)))
2220 (vip-special-ring-rotate1 vip-temp-command-ring 1)
2221 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
2224 (setq vip-d-com cmd))
2225 (vip-display-current-destructive-command)))
2227 (defun vip-next-destructive-command ()
2228 "Find next destructive command in the history of destructive commands."
2230 (vip-prev-destructive-command 'next))
2232 (defun vip-insert-prev-from-insertion-ring (arg)
2233 "Cycle through insertion ring in the direction of older insertions.
2234 Undoes previous insertion and inserts new.
2235 With prefix argument, cycles in the direction of newer elements.
2236 In minibuffer, this command executes whatever the invocation key is bound
2237 to in the global map, instead of cycling through the insertion ring."
2239 (let (vip-intermediate-command)
2240 (if (eq last-command 'vip-insert-from-insertion-ring)
2241 (progn ; repeated search through insertion history
2242 (setq vip-intermediate-command 'repeating-insertion-from-ring)
2243 (if (eq vip-current-state 'replace-state)
2245 (if vip-last-inserted-string-from-insertion-ring
2246 (backward-delete-char
2247 (length vip-last-inserted-string-from-insertion-ring))))
2249 ;;first search through insertion history
2250 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
2251 (setq this-command 'vip-insert-from-insertion-ring)
2252 ;; so that things will be undone properly
2253 (setq buffer-undo-list (cons nil buffer-undo-list))
2254 (setq vip-last-inserted-string-from-insertion-ring
2255 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
2257 ;; this change of vip-intermediate-command must come after
2258 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
2260 (setq vip-intermediate-command nil)
2261 (if vip-last-inserted-string-from-insertion-ring
2262 (insert vip-last-inserted-string-from-insertion-ring))
2265 (defun vip-insert-next-from-insertion-ring ()
2266 "Cycle through insertion ring in the direction of older insertions.
2267 Undo previous insertion and inserts new."
2269 (vip-insert-prev-from-insertion-ring 'next))
2272 ;; some region utilities
2274 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
2275 (defun vip-add-newline-at-eob-if-necessary ()
2278 ;; make sure all lines end with newline, unless in the minibuffer or
2279 ;; when requested otherwise (require-final-newline is nil)
2283 require-final-newline
2284 (not (vip-is-in-minibuffer)))
2287 (defun vip-yank-defun ()
2289 (copy-region-as-kill (point) (mark t)))
2291 ;; Enlarge region between BEG and END.
2292 (defun vip-enlarge-region (beg end)
2293 (or beg (setq beg end)) ; if beg is nil, set to end
2294 (or end (setq end beg)) ; if end is nil, set to beg
2297 (progn (goto-char beg) (set-mark end))
2301 (exchange-point-and-mark)
2302 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
2303 (if (not (eobp)) (beginning-of-line))
2304 (if (> beg end) (exchange-point-and-mark)))
2307 ;; Quote region by each line with a user supplied string.
2308 (defun vip-quote-region ()
2309 (setq vip-quote-string
2310 (vip-read-string-with-history
2313 'vip-quote-region-history
2315 (vip-enlarge-region (point) (mark t))
2316 (if (> (point) (mark t)) (exchange-point-and-mark))
2317 (insert vip-quote-string)
2320 (while (and (< (point) (mark t)) (bolp))
2321 (insert vip-quote-string)
2325 ;; Tells whether BEG is on the same line as END.
2326 ;; If one of the args is nil, it'll return nil.
2327 (defun vip-same-line (beg end)
2328 (let ((selective-display nil))
2329 (cond ((and beg end)
2330 ;; This 'if' is needed because Emacs treats the next empty line
2331 ;; as part of the previous line.
2332 (if (or (> beg (point-max)) (> end (point-max))) ; out of range
2334 (if (and (> end beg) (= (vip-line-pos 'start) end))
2335 (setq end (min (1+ end) (point-max))))
2336 (if (and (> beg end) (= (vip-line-pos 'start) beg))
2337 (setq beg (min (1+ beg) (point-max))))
2338 (<= (count-lines beg end) 1) ))
2344 ;; Check if the string ends with a newline.
2345 (defun vip-end-with-a-newline-p (string)
2346 (or (string= string "")
2347 (= (vip-seq-last-elt string) ?\n)))
2349 (defun vip-tmp-insert-at-eob (msg)
2350 (let ((savemax (point-max)))
2354 (goto-char savemax) (delete-region (point) (point-max))
2359 ;;; Minibuffer business
2361 (defsubst vip-set-minibuffer-style ()
2362 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
2365 (defun vip-minibuffer-setup-sentinel ()
2366 (let ((hook (if vip-vi-style-in-minibuffer
2367 'vip-change-state-to-insert
2368 'vip-change-state-to-emacs)))
2371 ;; Make sure the minibufer overlay is kept up-to-date. In XEmacs also
2372 ;; guards against the possibility of detaching this overlay.
2373 (vip-add-hook 'vip-post-command-hooks 'vip-move-minibuffer-overlay)
2376 ;; Interpret last event in the local map
2377 (defun vip-exit-minibuffer ()
2380 (setq command (local-key-binding (char-to-string last-command-char)))
2382 (command-execute command)
2383 (exit-minibuffer))))
2386 (defun vip-set-search-face ()
2387 (if (not (vip-window-display-p))
2389 (defvar vip-search-face
2391 (make-face 'vip-search-face)
2392 (or (face-differs-from-default-p 'vip-search-face)
2393 ;; face wasn't set in .vip or .Xdefaults
2394 (if (vip-can-use-colors "Black" "khaki")
2396 (set-face-background 'vip-search-face "khaki")
2397 (set-face-foreground 'vip-search-face "Black"))
2398 (copy-face 'italic 'vip-search-face)
2399 (set-face-underline-p 'vip-search-face t)))
2401 "*Face used to flash out the search pattern.")
2405 (defun vip-set-minibuffer-faces ()
2406 (if (not (vip-window-display-p))
2408 (defvar vip-minibuffer-emacs-face
2410 (make-face 'vip-minibuffer-emacs-face)
2411 (or (face-differs-from-default-p 'vip-minibuffer-emacs-face)
2412 ;; face wasn't set in .vip or .Xdefaults
2413 (if vip-vi-style-in-minibuffer
2414 ;; emacs state is an exception in the minibuffer
2415 (if (vip-can-use-colors "darkseagreen2" "Black")
2417 (set-face-background
2418 'vip-minibuffer-emacs-face "darkseagreen2")
2419 (set-face-foreground
2420 'vip-minibuffer-emacs-face "Black"))
2421 (copy-face 'highlight 'vip-minibuffer-emacs-face))
2422 ;; emacs state is the main state in the minibuffer
2423 (if (vip-can-use-colors "Black" "pink")
2425 (set-face-background 'vip-minibuffer-emacs-face "pink")
2426 (set-face-foreground
2427 'vip-minibuffer-emacs-face "Black"))
2428 (copy-face 'italic 'vip-minibuffer-emacs-face))
2430 'vip-minibuffer-emacs-face)
2431 "Face used in the Minibuffer when it is in Emacs state.")
2433 (defvar vip-minibuffer-insert-face
2435 (make-face 'vip-minibuffer-insert-face)
2436 (or (face-differs-from-default-p 'vip-minibuffer-insert-face)
2437 (if vip-vi-style-in-minibuffer
2438 (if (vip-can-use-colors "Black" "pink")
2440 (set-face-background 'vip-minibuffer-insert-face "pink")
2441 (set-face-foreground
2442 'vip-minibuffer-insert-face "Black"))
2443 (copy-face 'italic 'vip-minibuffer-insert-face))
2444 ;; If Insert state is an exception
2445 (if (vip-can-use-colors "darkseagreen2" "Black")
2447 (set-face-background
2448 'vip-minibuffer-insert-face "darkseagreen2")
2449 (set-face-foreground
2450 'vip-minibuffer-insert-face "Black"))
2451 (copy-face 'highlight 'vip-minibuffer-insert-face))
2452 (vip-italicize-face 'vip-minibuffer-insert-face)))
2453 'vip-minibuffer-insert-face)
2454 "Face used in the Minibuffer when it is in Insert state.")
2456 (defvar vip-minibuffer-vi-face
2458 (make-face 'vip-minibuffer-vi-face)
2459 (or (face-differs-from-default-p 'vip-minibuffer-vi-face)
2460 (if vip-vi-style-in-minibuffer
2461 (if (vip-can-use-colors "Black" "grey")
2463 (set-face-background 'vip-minibuffer-vi-face "grey")
2464 (set-face-foreground 'vip-minibuffer-vi-face "Black"))
2465 (copy-face 'bold 'vip-minibuffer-vi-face))
2466 (copy-face 'bold 'vip-minibuffer-vi-face)
2467 (invert-face 'vip-minibuffer-vi-face)))
2468 'vip-minibuffer-vi-face)
2469 "Face used in the Minibuffer when it is in Vi state.")
2471 ;; the current face used in the minibuffer
2472 (vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "")
2477 ;;; Reading string with history
2479 (defun vip-read-string-with-history (prompt &optional initial
2480 history-var default keymap)
2481 ;; Reads string, prompting with PROMPT and inserting the INITIAL
2482 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2483 ;; input is an empty string. Uses KEYMAP, if given, or the
2484 ;; minibuffer-local-map.
2485 ;; Default value is displayed until the user types something in the
2487 (let ((minibuffer-setup-hook
2489 (if (stringp initial)
2491 ;; don't wait if we have unread events or in kbd macro
2492 (or unread-command-events
2497 (vip-minibuffer-setup-sentinel)))
2502 (setq keymap (or keymap minibuffer-local-map)
2503 initial (or initial "")
2504 temp-msg (if default
2505 (format "(default: %s) " default)
2508 (setq vip-incomplete-ex-cmd nil)
2509 (setq val (read-from-minibuffer prompt
2510 (concat temp-msg initial val padding)
2511 keymap nil history-var))
2512 (setq minibuffer-setup-hook nil
2513 padding (vip-array-to-string (this-command-keys))
2515 ;; the following tries to be smart about what to put in history
2516 (if (not (string= val (car (eval history-var))))
2517 (set history-var (cons val (eval history-var))))
2518 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2519 (string= (nth 0 (eval history-var)) ""))
2520 (set history-var (cdr (eval history-var))))
2521 ;; if the user enters nothing but the prev cmd wasn't vip-ex or
2522 ;; vip-command-argument, this means that the user typed something then
2523 ;; erased. Return "" in this case, not the default---default is too
2524 ;; confusing in this case
2525 (cond ((and (string= val "")
2526 (not (memq last-command
2527 (list 'vip-ex 'vip-command-argument t))))
2529 ((string= val "") (or default ""))
2535 ;; insertion commands
2537 ;; Called when state changes from Insert Vi command mode.
2538 ;; Repeats the insertion command if Insert state was entered with prefix
2540 (defun vip-repeat-insert-command ()
2541 (let ((i-com (car vip-d-com))
2542 (val (nth 1 vip-d-com))
2543 (char (nth 2 vip-d-com)))
2544 (if (and val (> val 1)) ; first check that val is non-nil
2546 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
2548 (setq vip-d-com (list i-com val char nil nil nil))
2551 (defun vip-insert (arg)
2552 "Insert before point."
2554 (vip-set-complex-command-for-undo)
2555 (let ((val (vip-p-val arg))
2556 (com (vip-getcom arg)))
2557 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
2559 (vip-loop val (vip-yank-last-insertion))
2560 (vip-change-state-to-insert))))
2562 (defun vip-append (arg)
2563 "Append after point."
2565 (vip-set-complex-command-for-undo)
2566 (let ((val (vip-p-val arg))
2567 (com (vip-getcom arg)))
2568 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
2569 (if (not (eolp)) (forward-char))
2571 (vip-loop val (vip-yank-last-insertion))
2572 (vip-change-state-to-insert))))
2574 (defun vip-Append (arg)
2575 "Append at end of line."
2577 (vip-set-complex-command-for-undo)
2578 (let ((val (vip-p-val arg))
2579 (com (vip-getcom arg)))
2580 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
2583 (vip-loop val (vip-yank-last-insertion))
2584 (vip-change-state-to-insert))))
2586 (defun vip-Insert (arg)
2587 "Insert before first non-white."
2589 (vip-set-complex-command-for-undo)
2590 (let ((val (vip-p-val arg))
2591 (com (vip-getcom arg)))
2592 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
2593 (back-to-indentation)
2595 (vip-loop val (vip-yank-last-insertion))
2596 (vip-change-state-to-insert))))
2598 (defun vip-open-line (arg)
2601 (vip-set-complex-command-for-undo)
2602 (let ((val (vip-p-val arg))
2603 (com (vip-getcom arg)))
2604 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
2605 (let ((col (current-indentation)))
2612 (progn (setq vip-cted t) (indent-to col)))
2613 (vip-yank-last-insertion)))
2616 (if vip-auto-indent (progn (setq vip-cted t) (indent-to col)))
2617 (vip-change-state-to-insert)
2620 (defun vip-Open-line (arg)
2623 (vip-set-complex-command-for-undo)
2624 (let ((val (vip-p-val arg))
2625 (com (vip-getcom arg)))
2626 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
2627 (let ((col (current-indentation)))
2634 (progn (setq vip-cted t) (indent-to col)))
2635 (vip-yank-last-insertion)))
2638 (if vip-auto-indent (progn (setq vip-cted t) (indent-to col)))
2639 (vip-change-state-to-insert)))))
2641 (defun vip-open-line-at-point (arg)
2642 "Open line at point."
2644 (vip-set-complex-command-for-undo)
2645 (let ((val (vip-p-val arg))
2646 (com (vip-getcom arg)))
2647 (vip-set-destructive-command
2648 (list 'vip-open-line-at-point val ?r nil nil nil))
2653 (vip-yank-last-insertion)))
2655 (vip-change-state-to-insert))))
2657 (defun vip-substitute (arg)
2658 "Substitute characters."
2660 (let ((val (vip-p-val arg))
2661 (com (vip-getcom arg)))
2665 (vip-change-subr (mark t) (point))
2666 (vip-change (mark t) (point)))
2667 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
2670 (defun vip-substitute-line (arg)
2673 (vip-set-complex-command-for-undo)
2674 (vip-line (cons arg ?C)))
2676 ;; Prepare for replace
2677 (defun vip-start-replace ()
2678 (setq vip-began-as-replace t
2679 vip-sitting-in-replace t
2680 vip-replace-chars-to-delete 0
2681 vip-replace-chars-deleted 0)
2682 (vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
2683 (vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
2684 ;; this will get added repeatedly, but no harm
2685 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
2686 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
2687 (vip-move-marker-locally 'vip-last-posn-in-replace-region
2688 (vip-replace-start))
2690 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
2692 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2695 ;; Runs vip-after-change-functions inside after-change-functions
2696 (defun vip-after-change-sentinel (beg end len)
2697 (let ((list vip-after-change-functions))
2699 (funcall (car list) beg end len)
2700 (setq list (cdr list)))))
2702 ;; Runs vip-before-change-functions inside before-change-functions
2703 (defun vip-before-change-sentinel (beg end)
2704 (let ((list vip-before-change-functions))
2706 (funcall (car list) beg end)
2707 (setq list (cdr list)))))
2709 (defsubst vip-post-command-sentinel ()
2710 (run-hooks 'vip-post-command-hooks))
2712 (defsubst vip-pre-command-sentinel ()
2713 (run-hooks 'vip-pre-command-hooks))
2715 ;; Needed so that Viper will be able to figure the last inserted
2716 ;; chunk of text with reasonable accuracy.
2717 (defsubst vip-insert-state-post-command-sentinel ()
2718 (if (and (memq vip-current-state '(insert-state replace-state))
2720 (>= (point) vip-insert-point))
2721 (setq vip-last-posn-while-in-insert-state (point-marker)))
2722 (if (and (eq this-command 'dabbrev-expand)
2723 (integerp vip-pre-command-point)
2724 (> vip-insert-point vip-pre-command-point))
2725 (move-marker vip-insert-point vip-pre-command-point))
2728 (defsubst vip-insert-state-pre-command-sentinel ()
2729 (if (and (eq this-command 'dabbrev-expand)
2730 (markerp vip-insert-point)
2731 (marker-position vip-insert-point))
2732 (setq vip-pre-command-point (marker-position vip-insert-point))))
2734 (defsubst vip-R-state-post-command-sentinel ()
2735 ;; Restoring cursor color is needed despite
2736 ;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
2737 ;; another frame, the pre-command hook won't change cursor color to default
2738 ;; in that other frame. So, if the second frame cursor was red and we set
2739 ;; the point outside the replacement region, then the cursor color will
2740 ;; remain red. Restoring the default, below, prevents this.
2741 (if (and (<= (vip-replace-start) (point))
2742 (<= (point) (vip-replace-end)))
2743 (vip-change-cursor-color vip-replace-overlay-cursor-color)
2744 (vip-restore-cursor-color)
2747 ;; to speed up, don't change cursor color before self-insert
2748 ;; and common move commands
2749 (defsubst vip-replace-state-pre-command-sentinel ()
2750 (or (memq this-command '(self-insert-command))
2751 (memq (vip-event-key last-command-event)
2752 '(up down left right (meta f) (meta b)
2753 (control n) (control p) (control f) (control b)))
2754 (vip-restore-cursor-color)))
2756 (defun vip-replace-state-post-command-sentinel ()
2757 ;; Restoring cursor color is needed despite
2758 ;; vip-replace-state-pre-command-sentinel: When one jumps to another buffer
2759 ;; in another frame, the pre-command hook won't change cursor color to
2760 ;; default in that other frame. So, if the second frame cursor was red and
2761 ;; we set the point outside the replacement region, then the cursor color
2762 ;; will remain red. Restoring the default, below, fixes this problem.
2764 ;; We optimize for self-insert-command's here, since they either don't change
2765 ;; cursor color or, if they terminate replace mode, the color will be changed
2766 ;; in vip-finish-change
2767 (or (memq this-command '(self-insert-command))
2768 (vip-restore-cursor-color))
2770 ((eq vip-current-state 'replace-state)
2771 ;; delete characters to compensate for inserted chars.
2772 (let ((replace-boundary (vip-replace-end)))
2774 (goto-char vip-last-posn-in-replace-region)
2775 (delete-char vip-replace-chars-to-delete)
2776 (setq vip-replace-chars-to-delete 0
2777 vip-replace-chars-deleted 0)
2778 ;; terminate replace mode if reached replace limit
2779 (if (= vip-last-posn-in-replace-region
2781 (vip-finish-change vip-last-posn-in-replace-region)))
2783 (if (and (<= (vip-replace-start) (point))
2784 (<= (point) replace-boundary))
2786 ;; the state may have changed in vip-finish-change above
2787 (if (eq vip-current-state 'replace-state)
2788 (vip-change-cursor-color vip-replace-overlay-cursor-color))
2789 (setq vip-last-posn-in-replace-region (point-marker))))
2792 (t ;; terminate replace mode if changed Viper states.
2793 (vip-finish-change vip-last-posn-in-replace-region))))
2796 ;; checks how many chars were deleted by the last change
2797 (defun vip-replace-mode-spy-before (beg end)
2798 (setq vip-replace-chars-deleted
2800 (max 0 (- end (vip-replace-end)))
2801 (max 0 (- (vip-replace-start) beg))
2804 ;; Invoked as an after-change-function to set up parameters of the last change
2805 (defun vip-replace-mode-spy-after (beg end length)
2806 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
2808 (setq vip-replace-chars-to-delete 0)
2809 (vip-move-marker-locally
2810 'vip-last-posn-in-replace-region (point)))
2812 (let (beg-col end-col real-end chars-to-delete)
2813 (setq real-end (min end (vip-replace-end)))
2816 (setq beg-col (current-column))
2817 (goto-char real-end)
2818 (setq end-col (current-column)))
2820 ;; If beg of change is outside the replacement region, then don't
2821 ;; delete anything in the repl region (set chars-to-delete to 0).
2823 ;; This works fine except that we have to take special care of
2824 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
2825 ;; sometimes simply shifts the repl region rightwards, without
2826 ;; deleting an equal amount of characters.
2828 ;; The reason why new-dabbrev.el causes this are this:
2829 ;; if one dinamically completes a partial word that starts before the
2830 ;; replacement region (but ends inside)then new-dabbrev.el first
2831 ;; moves cursor backwards, to the beginning of the word to be
2832 ;; completed (say, pt A). Then it inserts the
2833 ;; completed word and then deletes the old, incomplete part.
2834 ;; Since the complete word is inserted at position before the repl
2835 ;; region, the next If-statement would have set chars-to-delete to 0
2836 ;; unless we check for the current command, which must be
2839 ;; We should be able deal with these problems in a better way
2840 ;; when emacs will have overlays with sticky back ends.
2841 ;; In fact, it would be also useful to add overlays for insert
2842 ;; regions as well, since this will let us capture the situation when
2843 ;; dabbrev-expand goes back past the insertion point to find the
2844 ;; beginning of the word to be expanded.
2845 (if (or (and (<= (vip-replace-start) beg)
2846 (<= beg (vip-replace-end)))
2847 (and (= length 0) (eq this-command 'dabbrev-expand)))
2848 (setq chars-to-delete
2849 (max (- end-col beg-col) (- real-end beg) 0))
2850 (setq chars-to-delete 0))
2852 ;; if beg = last change position, it means that we are within the
2853 ;; same command that does multiple changes. Moreover, it means
2854 ;; that we have two subsequent changes (insert/delete) that
2855 ;; complement each other.
2856 (if (= beg (marker-position vip-last-posn-in-replace-region))
2857 (setq vip-replace-chars-to-delete
2858 (- (+ chars-to-delete vip-replace-chars-to-delete)
2859 vip-replace-chars-deleted))
2860 (setq vip-replace-chars-to-delete chars-to-delete))
2862 (vip-move-marker-locally
2863 'vip-last-posn-in-replace-region
2864 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
2865 (or (marker-position vip-last-posn-in-replace-region)
2866 (vip-replace-start))
2869 (setq vip-replace-chars-to-delete
2870 (max 0 (min vip-replace-chars-to-delete
2871 (- (vip-replace-end)
2872 vip-last-posn-in-replace-region))))
2876 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
2877 ;; posn is within the overlay.
2878 (defun vip-finish-change (posn)
2879 (vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
2880 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2881 (vip-remove-hook 'vip-post-command-hooks
2882 'vip-replace-state-post-command-sentinel)
2884 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2885 (vip-restore-cursor-color)
2886 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2890 (>= posn (vip-replace-start))
2891 (< posn (vip-replace-end)))
2892 (delete-region posn (vip-replace-end)))
2895 (if (eq vip-current-state 'replace-state)
2896 (vip-downgrade-to-insert))
2897 ;; replace mode ended => nullify vip-last-posn-in-replace-region
2898 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
2899 (vip-hide-replace-overlay)
2900 (vip-refresh-mode-line)
2901 (vip-put-string-on-kill-ring vip-last-replace-region)
2904 ;; Make STRING be the first element of the kill ring.
2905 (defun vip-put-string-on-kill-ring (string)
2906 (setq kill-ring (cons string kill-ring))
2907 (if (> (length kill-ring) kill-ring-max)
2908 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2909 (setq kill-ring-yank-pointer kill-ring))
2911 (defun vip-finish-R-mode ()
2912 (vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2914 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2915 (vip-downgrade-to-insert))
2917 (defun vip-start-R-mode ()
2918 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2921 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
2923 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2928 (defun vip-replace-state-exit-cmd ()
2929 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2930 These keys are ESC, RET, and LineFeed"
2932 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2934 (vip-finish-change vip-last-posn-in-replace-region))
2936 (if (eq this-command 'vip-intercept-ESC-key)
2937 (setq com 'vip-exit-insert-state)
2938 (vip-set-unread-command-events last-input-char)
2939 (setq com (key-binding (read-key-sequence nil))))
2941 (condition-case conds
2942 (command-execute com)
2944 (vip-message-conditions conds)))
2946 (vip-hide-replace-overlay))
2949 ;; This is the function bound to 'R'---unlimited replace.
2950 ;; Similar to Emacs's own overwrite-mode.
2951 (defun vip-overwrite (arg)
2952 "Begin overwrite mode."
2954 (let ((val (vip-p-val arg))
2955 (com (vip-getcom arg)) (len))
2956 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
2959 ;; Viper saves inserted text in vip-last-insertion
2960 (setq len (length vip-last-insertion))
2962 (vip-loop val (vip-yank-last-insertion)))
2963 (setq last-command 'vip-overwrite)
2964 (vip-set-complex-command-for-undo)
2965 (vip-set-replace-overlay (point) (vip-line-pos 'end))
2966 (vip-change-state-to-replace)
2972 (defun vip-line (arg)
2973 (let ((val (car arg))
2975 (vip-move-marker-locally 'vip-com-point (point))
2977 (vip-next-line-carefully (1- val)))
2978 ;; this ensures that dd, cc, D, yy will do the right thing on the last
2979 ;; line of buffer when this line has no \n.
2980 (vip-add-newline-at-eob-if-necessary)
2981 (vip-execute-com 'vip-line val com))
2982 (if (and (eobp) (not (bobp))) (forward-line -1))
2985 (defun vip-yank-line (arg)
2986 "Yank ARG lines (in Vi's sense)."
2988 (let ((val (vip-p-val arg)))
2989 (vip-line (cons val ?Y))))
2994 (defun vip-region (arg)
2995 "Execute command on a region."
2997 (let ((val (vip-P-val arg))
2998 (com (vip-getcom arg)))
2999 (vip-move-marker-locally 'vip-com-point (point))
3000 (exchange-point-and-mark)
3001 (vip-execute-com 'vip-region val com)))
3003 (defun vip-Region (arg)
3004 "Execute command on a Region."
3006 (let ((val (vip-P-val arg))
3007 (com (vip-getCom arg)))
3008 (vip-move-marker-locally 'vip-com-point (point))
3009 (exchange-point-and-mark)
3010 (vip-execute-com 'vip-Region val com)))
3012 (defun vip-replace-char (arg)
3013 "Replace the following ARG chars by the character read."
3015 (if (and (eolp) (bolp)) (error "No character to replace here"))
3016 (let ((val (vip-p-val arg))
3017 (com (vip-getcom arg)))
3018 (vip-replace-char-subr com val)
3019 (if (and (eolp) (not (bolp))) (forward-char 1))
3020 (vip-set-destructive-command
3021 (list 'vip-replace-char val ?r nil vip-d-char nil))
3024 (defun vip-replace-char-subr (com arg)
3025 (let ((take-care-of-iso-accents
3026 (and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
3028 (setq char (if (equal com ?r)
3031 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
3032 ;; get European characters
3034 (iso-accents-mode 1)
3035 (vip-set-unread-command-events char)
3036 (setq char (aref (read-key-sequence nil) 0))
3037 (iso-accents-mode -1)))
3039 (setq vip-d-char char)
3040 (vip-loop (if (> arg 0) arg (- arg))
3041 (if (eq char ?\C-m) (insert "\n") (insert char)))
3042 (backward-char arg)))
3045 ;; basic cursor movement. j, k, l, h commands.
3047 (defun vip-forward-char (arg)
3048 "Move point right ARG characters (left if ARG negative).
3049 On reaching end of line, stop and signal error."
3051 (vip-leave-region-active)
3052 (let ((val (vip-p-val arg))
3053 (com (vip-getcom arg)))
3054 (if com (vip-move-marker-locally 'vip-com-point (point)))
3055 (if vip-ex-style-motion
3057 ;; the boundary condition check gets weird here because
3058 ;; forward-char may be the parameter of a delete, and 'dl' works
3059 ;; just like 'x' for the last char on a line, so we have to allow
3060 ;; the forward motion before the 'vip-execute-com', but, of
3061 ;; course, 'dl' doesn't work on an empty line, so we have to
3062 ;; catch that condition before 'vip-execute-com'
3063 (if (and (eolp) (bolp)) (error "") (forward-char val))
3064 (if com (vip-execute-com 'vip-forward-char val com))
3065 (if (eolp) (progn (backward-char 1) (error ""))))
3067 (if com (vip-execute-com 'vip-forward-char val com)))))
3069 (defun vip-backward-char (arg)
3070 "Move point left ARG characters (right if ARG negative).
3071 On reaching beginning of line, stop and signal error."
3073 (vip-leave-region-active)
3074 (let ((val (vip-p-val arg))
3075 (com (vip-getcom arg)))
3076 (if com (vip-move-marker-locally 'vip-com-point (point)))
3077 (if vip-ex-style-motion
3079 (if (bolp) (error "") (backward-char val))
3080 (if com (vip-execute-com 'vip-backward-char val com)))
3082 (if com (vip-execute-com 'vip-backward-char val com)))))
3084 ;; Like forward-char, but doesn't move at end of buffer.
3085 (defun vip-forward-char-carefully (&optional arg)
3086 (setq arg (or arg 1))
3087 (if (>= (point-max) (+ (point) arg))
3089 (goto-char (point-max))))
3091 ;; Like backward-char, but doesn't move at end of buffer.
3092 (defun vip-backward-char-carefully (&optional arg)
3093 (setq arg (or arg 1))
3094 (if (<= (point-min) (- (point) arg))
3096 (goto-char (point-min))))
3098 (defun vip-next-line-carefully (arg)
3107 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
3108 ;; for word movement. When executed with a destructive command, \n is
3109 ;; usually left untouched for the last word.
3110 ;; Viper uses syntax table to determine what is a word and what is a
3111 ;; separator. However, \n is always a separator. Also, if vip-syntax-preference
3112 ;; is 'vi, then `_' is part of the word.
3115 (defun vip-skip-separators (forward)
3118 (vip-skip-all-separators-forward 'within-line)
3119 (if (looking-at "\n")
3122 (vip-skip-all-separators-forward 'within-line))))
3123 (vip-skip-all-separators-backward 'within-line)
3125 (if (looking-at "\n")
3126 (vip-skip-all-separators-backward 'within-line)
3129 (defun vip-forward-word-kernel (val)
3131 (cond ((vip-looking-at-alpha)
3132 (vip-skip-alpha-forward "_")
3133 (vip-skip-separators t))
3134 ((vip-looking-at-separator)
3135 (vip-skip-separators t))
3136 ((not (vip-looking-at-alphasep))
3137 (vip-skip-nonalphasep-forward)
3138 (vip-skip-separators t)))
3139 (setq val (1- val))))
3141 ;; first search backward for pat. Then skip chars backwards using aux-pat
3142 (defun vip-fwd-skip (pat aux-pat lim)
3143 (if (and (save-excursion
3144 (re-search-backward pat lim t))
3145 (= (point) (match-end 0)))
3146 (goto-char (match-beginning 0)))
3147 (skip-chars-backward aux-pat lim)
3149 (vip-forward-char-carefully))
3153 (defun vip-forward-word (arg)
3156 (vip-leave-region-active)
3157 (let ((val (vip-p-val arg))
3158 (com (vip-getcom arg)))
3159 (if com (vip-move-marker-locally 'vip-com-point (point)))
3160 (vip-forward-word-kernel val)
3162 (cond ((memq com (list ?c (- ?c)))
3163 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3164 ;; Yank words including the whitespace, but not newline
3165 ((memq com (list ?y (- ?y)))
3166 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3167 ((vip-dotable-command-p com)
3168 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3169 (vip-execute-com 'vip-forward-word val com)))))
3172 (defun vip-forward-Word (arg)
3173 "Forward word delimited by white characters."
3175 (vip-leave-region-active)
3176 (let ((val (vip-p-val arg))
3177 (com (vip-getcom arg)))
3178 (if com (vip-move-marker-locally 'vip-com-point (point)))
3181 (vip-skip-nonseparators 'forward)
3182 (vip-skip-separators t)))
3184 (cond ((memq com (list ?c (- ?c)))
3185 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3186 ;; Yank words including the whitespace, but not newline
3187 ((memq com (list ?y (- ?y)))
3188 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3189 ((vip-dotable-command-p com)
3190 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3191 (vip-execute-com 'vip-forward-Word val com)))))
3194 ;; this is a bit different from Vi, but Vi's end of word
3195 ;; makes no sense whatsoever
3196 (defun vip-end-of-word-kernel ()
3197 (if (vip-end-of-word-p) (forward-char))
3198 (if (vip-looking-at-separator)
3199 (vip-skip-all-separators-forward))
3201 (cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
3202 ((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
3203 (vip-backward-char-carefully))
3205 (defun vip-end-of-word-p ()
3208 (cond ((vip-looking-at-alpha)
3210 (not (vip-looking-at-alpha)))
3211 ((not (vip-looking-at-alphasep))
3213 (vip-looking-at-alphasep))))))
3216 (defun vip-end-of-word (arg &optional careful)
3217 "Move point to end of current word."
3219 (vip-leave-region-active)
3220 (let ((val (vip-p-val arg))
3221 (com (vip-getcom arg)))
3222 (if com (vip-move-marker-locally 'vip-com-point (point)))
3223 (vip-loop val (vip-end-of-word-kernel))
3227 (vip-execute-com 'vip-end-of-word val com)))))
3229 (defun vip-end-of-Word (arg)
3230 "Forward to end of word delimited by white character."
3232 (vip-leave-region-active)
3233 (let ((val (vip-p-val arg))
3234 (com (vip-getcom arg)))
3235 (if com (vip-move-marker-locally 'vip-com-point (point)))
3238 (vip-end-of-word-kernel)
3239 (vip-skip-nonseparators 'forward)
3244 (vip-execute-com 'vip-end-of-Word val com)))))
3246 (defun vip-backward-word-kernel (val)
3249 (cond ((vip-looking-at-alpha)
3250 (vip-skip-alpha-backward "_"))
3251 ((vip-looking-at-separator)
3253 (vip-skip-separators nil)
3255 (cond ((vip-looking-at-alpha)
3256 (vip-skip-alpha-backward "_"))
3257 ((not (vip-looking-at-alphasep))
3258 (vip-skip-nonalphasep-backward))
3259 (t (forward-char))))
3260 ((not (vip-looking-at-alphasep))
3261 (vip-skip-nonalphasep-backward)))
3262 (setq val (1- val))))
3264 (defun vip-backward-word (arg)
3267 (vip-leave-region-active)
3268 (let ((val (vip-p-val arg))
3269 (com (vip-getcom arg)))
3272 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3274 (vip-move-marker-locally 'vip-com-point (point))
3275 (if i (forward-char))))
3276 (vip-backward-word-kernel val)
3277 (if com (vip-execute-com 'vip-backward-word val com))))
3279 (defun vip-backward-Word (arg)
3280 "Backward word delimited by white character."
3282 (vip-leave-region-active)
3283 (let ((val (vip-p-val arg))
3284 (com (vip-getcom arg)))
3287 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3289 (vip-move-marker-locally 'vip-com-point (point))
3290 (if i (forward-char))))
3293 (vip-skip-separators nil)
3294 (vip-skip-nonseparators 'backward)))
3295 (if com (vip-execute-com 'vip-backward-Word val com))))
3301 (defun vip-beginning-of-line (arg)
3302 "Go to beginning of line."
3304 (vip-leave-region-active)
3305 (let ((val (vip-p-val arg))
3306 (com (vip-getcom arg)))
3307 (if com (vip-move-marker-locally 'vip-com-point (point)))
3308 (beginning-of-line val)
3309 (if com (vip-execute-com 'vip-beginning-of-line val com))))
3311 (defun vip-bol-and-skip-white (arg)
3312 "Beginning of line at first non-white character."
3314 (vip-leave-region-active)
3315 (let ((val (vip-p-val arg))
3316 (com (vip-getcom arg)))
3317 (if com (vip-move-marker-locally 'vip-com-point (point)))
3318 (forward-to-indentation (1- val))
3319 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
3321 (defun vip-goto-eol (arg)
3322 "Go to end of line."
3324 (vip-leave-region-active)
3325 (let ((val (vip-p-val arg))
3326 (com (vip-getcom arg)))
3327 (if com (vip-move-marker-locally 'vip-com-point (point)))
3329 (if com (vip-execute-com 'vip-goto-eol val com))
3330 (if vip-ex-style-motion
3331 (if (and (eolp) (not (bolp))
3332 ;; a fix for vip-change-to-eol
3333 (not (equal vip-current-state 'insert-state)))
3338 (defun vip-goto-col (arg)
3339 "Go to ARG's column."
3341 (vip-leave-region-active)
3342 (let ((val (vip-p-val arg))
3343 (com (vip-getcom arg)))
3346 (if (> val (1+ (current-column))) (error "")))
3347 (if com (vip-move-marker-locally 'vip-com-point (point)))
3349 (forward-char (1- val))
3350 (if com (vip-execute-com 'vip-goto-col val com))))
3353 (defun vip-next-line (arg)
3356 (vip-leave-region-active)
3357 (let ((val (vip-p-val arg))
3358 (com (vip-getCom arg)))
3359 (if com (vip-move-marker-locally 'vip-com-point (point)))
3361 (if vip-ex-style-motion
3362 (if (and (eolp) (not (bolp))) (backward-char 1)))
3363 (setq this-command 'next-line)
3364 (if com (vip-execute-com 'vip-next-line val com))))
3366 (defun vip-next-line-at-bol (arg)
3367 "Next line at beginning of line."
3369 (vip-leave-region-active)
3372 (if (eobp) (error "Last line in buffer")))
3373 (let ((val (vip-p-val arg))
3374 (com (vip-getCom arg)))
3375 (if com (vip-move-marker-locally 'vip-com-point (point)))
3377 (back-to-indentation)
3378 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
3380 (defun vip-previous-line (arg)
3381 "Go to previous line."
3383 (vip-leave-region-active)
3384 (let ((val (vip-p-val arg))
3385 (com (vip-getCom arg)))
3386 (if com (vip-move-marker-locally 'vip-com-point (point)))
3388 (if vip-ex-style-motion
3389 (if (and (eolp) (not (bolp))) (backward-char 1)))
3390 (setq this-command 'previous-line)
3391 (if com (vip-execute-com 'vip-previous-line val com))))
3394 (defun vip-previous-line-at-bol (arg)
3395 "Previous line at beginning of line."
3397 (vip-leave-region-active)
3400 (if (bobp) (error "First line in buffer")))
3401 (let ((val (vip-p-val arg))
3402 (com (vip-getCom arg)))
3403 (if com (vip-move-marker-locally 'vip-com-point (point)))
3404 (forward-line (- val))
3405 (back-to-indentation)
3406 (if com (vip-execute-com 'vip-previous-line val com))))
3408 (defun vip-change-to-eol (arg)
3409 "Change to end of line."
3411 (vip-goto-eol (cons arg ?c)))
3413 (defun vip-kill-line (arg)
3416 (vip-goto-eol (cons arg ?d)))
3418 (defun vip-erase-line (arg)
3421 (vip-beginning-of-line (cons arg ?d)))
3426 (defun vip-goto-line (arg)
3427 "Go to ARG's line. Without ARG go to end of buffer."
3429 (let ((val (vip-P-val arg))
3430 (com (vip-getCom arg)))
3431 (vip-move-marker-locally 'vip-com-point (point))
3432 (vip-deactivate-mark)
3435 (goto-char (point-max))
3436 (goto-char (point-min))
3437 (forward-line (1- val)))
3439 ;; positioning is done twice: before and after command execution
3440 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3441 (back-to-indentation)
3443 (if com (vip-execute-com 'vip-goto-line val com))
3445 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3446 (back-to-indentation)
3449 ;; Find ARG's occurrence of CHAR on the current line.
3450 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
3451 ;; adjust point after search.
3452 (defun vip-find-char (arg char forward offset)
3453 (or (char-or-string-p char) (error ""))
3454 (let ((arg (if forward arg (- arg)))
3455 (cmd (if (eq vip-intermediate-command 'vip-repeat)
3457 (vip-array-to-string (this-command-keys))))
3463 ;; forward search begins here
3464 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
3465 ;; forward search ends here
3466 (progn (end-of-line) (point)))
3468 ;; backward search begins from here
3470 (error "Command `%s': At beginning of line" cmd) (point))
3471 ;; backward search ends here
3472 (progn (beginning-of-line) (point))))
3473 ;; if arg > 0, point is forwarded before search.
3474 (if (> arg 0) (goto-char (1+ (point-min)))
3475 (goto-char (point-max)))
3476 (if (let ((case-fold-search nil))
3477 (search-forward (char-to-string char) nil 0 arg))
3478 (setq point (point))
3479 (error "Command `%s': `%c' not found" cmd char))))
3480 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
3482 (defun vip-find-char-forward (arg)
3483 "Find char on the line.
3484 If called interactively read the char to find from the terminal, and if
3485 called from vip-repeat, the char last used is used. This behaviour is
3486 controlled by the sign of prefix numeric value."
3488 (let ((val (vip-p-val arg))
3489 (com (vip-getcom arg)))
3491 ;; this means that the function was called interactively
3492 (setq vip-f-char (read-char)
3495 ;; vip-repeat --- set vip-F-char from command-keys
3496 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3497 (vip-seq-last-elt (nth 5 vip-d-com))
3499 vip-f-char vip-F-char)
3501 (if com (vip-move-marker-locally 'vip-com-point (point)))
3502 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
3506 (setq vip-F-char vip-f-char) ; set new vip-F-char
3508 (vip-execute-com 'vip-find-char-forward val com)))))
3510 (defun vip-goto-char-forward (arg)
3511 "Go up to char ARG forward on line."
3513 (let ((val (vip-p-val arg))
3514 (com (vip-getcom arg)))
3516 ;; this means that the function was called interactively
3517 (setq vip-f-char (read-char)
3520 ;; vip-repeat --- set vip-F-char from command-keys
3521 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3522 (vip-seq-last-elt (nth 5 vip-d-com))
3524 vip-f-char vip-F-char)
3526 (if com (vip-move-marker-locally 'vip-com-point (point)))
3527 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
3531 (setq vip-F-char vip-f-char) ; set new vip-F-char
3533 (vip-execute-com 'vip-goto-char-forward val com)))))
3535 (defun vip-find-char-backward (arg)
3536 "Find char ARG on line backward."
3538 (let ((val (vip-p-val arg))
3539 (com (vip-getcom arg)))
3541 ;; this means that the function was called interactively
3542 (setq vip-f-char (read-char)
3545 ;; vip-repeat --- set vip-F-char from command-keys
3546 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3547 (vip-seq-last-elt (nth 5 vip-d-com))
3549 vip-f-char vip-F-char)
3551 (if com (vip-move-marker-locally 'vip-com-point (point)))
3553 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
3557 (setq vip-F-char vip-f-char) ; set new vip-F-char
3558 (vip-execute-com 'vip-find-char-backward val com)))))
3560 (defun vip-goto-char-backward (arg)
3561 "Go up to char ARG backward on line."
3563 (let ((val (vip-p-val arg))
3564 (com (vip-getcom arg)))
3566 ;; this means that the function was called interactively
3567 (setq vip-f-char (read-char)
3570 ;; vip-repeat --- set vip-F-char from command-keys
3571 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3572 (vip-seq-last-elt (nth 5 vip-d-com))
3574 vip-f-char vip-F-char)
3576 (if com (vip-move-marker-locally 'vip-com-point (point)))
3577 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
3581 (setq vip-F-char vip-f-char) ; set new vip-F-char
3582 (vip-execute-com 'vip-goto-char-backward val com)))))
3584 (defun vip-repeat-find (arg)
3585 "Repeat previous find command."
3587 (let ((val (vip-p-val arg))
3588 (com (vip-getcom arg)))
3589 (vip-deactivate-mark)
3590 (if com (vip-move-marker-locally 'vip-com-point (point)))
3591 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
3594 (if vip-f-forward (forward-char))
3595 (vip-execute-com 'vip-repeat-find val com)))))
3597 (defun vip-repeat-find-opposite (arg)
3598 "Repeat previous find command in the opposite direction."
3600 (let ((val (vip-p-val arg))
3601 (com (vip-getcom arg)))
3602 (vip-deactivate-mark)
3603 (if com (vip-move-marker-locally 'vip-com-point (point)))
3604 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
3607 (if vip-f-forward (forward-char))
3608 (vip-execute-com 'vip-repeat-find-opposite val com)))))
3611 ;; window scrolling etc.
3613 (defun vip-other-window (arg)
3614 "Switch to other window."
3617 (or (not (eq vip-current-state 'emacs-state))
3618 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
3619 (vip-change-state-to-vi)))
3621 (defun vip-window-top (arg)
3622 "Go to home window line."
3624 (let ((val (vip-p-val arg))
3625 (com (vip-getCom arg)))
3626 (if com (vip-move-marker-locally 'vip-com-point (point)))
3628 (move-to-window-line (1- val))
3630 ;; positioning is done twice: before and after command execution
3631 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3632 (back-to-indentation)
3634 (if com (vip-execute-com 'vip-window-top val com))
3636 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3637 (back-to-indentation)
3640 (defun vip-window-middle (arg)
3641 "Go to middle window line."
3643 (let ((val (vip-p-val arg))
3644 (com (vip-getCom arg))
3646 (if com (vip-move-marker-locally 'vip-com-point (point)))
3648 (if (not (pos-visible-in-window-p (point-max)))
3649 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3650 (setq lines (count-lines (window-start) (point-max)))
3651 (move-to-window-line (+ (/ lines 2) (1- val))))
3653 ;; positioning is done twice: before and after command execution
3654 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3655 (back-to-indentation)
3657 (if com (vip-execute-com 'vip-window-middle val com))
3659 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3660 (back-to-indentation)
3663 (defun vip-window-bottom (arg)
3664 "Go to last window line."
3666 (let ((val (vip-p-val arg))
3667 (com (vip-getCom arg)))
3668 (if com (vip-move-marker-locally 'vip-com-point (point)))
3670 (move-to-window-line (- val))
3672 ;; positioning is done twice: before and after command execution
3673 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3674 (back-to-indentation)
3676 (if com (vip-execute-com 'vip-window-bottom val com))
3678 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3679 (back-to-indentation)
3682 (defun vip-line-to-top (arg)
3683 "Put current line on the home line."
3685 (recenter (1- arg)))
3687 (defun vip-line-to-middle (arg)
3688 "Put current line on the middle line."
3690 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3692 (defun vip-line-to-bottom (arg)
3693 "Put current line on the last line."
3695 (recenter (- (window-height) (1+ arg))))
3699 ;; must correct this to only match ( to ) etc. On the other hand
3700 ;; it is good that paren match gets confused, because that way you
3701 ;; catch _all_ imbalances.
3703 (defun vip-paren-match (arg)
3704 "Go to the matching parenthesis."
3706 (let ((com (vip-getcom arg)))
3708 (if (or (> arg 99) (< arg 1))
3709 (error "Prefix must be between 1 and 99")
3711 (if (> (point-max) 80000)
3712 (* (/ (point-max) 100) arg)
3713 (/ (* (point-max) arg) 100)))
3714 (back-to-indentation))
3716 (if (and (eolp) (not (bolp))) (forward-char -1))
3720 (if (re-search-forward "[][(){}]" lim t)
3722 (error "No matching character on line")))
3723 (cond ((looking-at "[\(\[{]")
3724 (if com (vip-move-marker-locally 'vip-com-point (point)))
3727 (vip-execute-com 'vip-paren-match nil com)
3729 ((looking-at "[])}]")
3731 (if com (vip-move-marker-locally 'vip-com-point (point)))
3733 (if com (vip-execute-com 'vip-paren-match nil com)))
3737 ;; sentence ,paragraph and heading
3739 (defun vip-forward-sentence (arg)
3743 (let ((val (vip-p-val arg))
3744 (com (vip-getcom arg)))
3745 (if com (vip-move-marker-locally 'vip-com-point (point)))
3746 (forward-sentence val)
3747 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3749 (defun vip-backward-sentence (arg)
3750 "Backward sentence."
3753 (let ((val (vip-p-val arg))
3754 (com (vip-getcom arg)))
3755 (if com (vip-move-marker-locally 'vip-com-point (point)))
3756 (backward-sentence val)
3757 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3759 (defun vip-forward-paragraph (arg)
3760 "Forward paragraph."
3763 (let ((val (vip-p-val arg))
3764 (com (vip-getCom arg)))
3765 (if com (vip-move-marker-locally 'vip-com-point (point)))
3766 (forward-paragraph val)
3767 (if com (vip-execute-com 'vip-forward-paragraph nil com))))
3769 (defun vip-backward-paragraph (arg)
3770 "Backward paragraph."
3773 (let ((val (vip-p-val arg))
3774 (com (vip-getCom arg)))
3775 (if com (vip-move-marker-locally 'vip-com-point (point)))
3776 (backward-paragraph val)
3777 (if com (vip-execute-com 'vip-backward-paragraph nil com))))
3779 ;; should be mode-specific etc.
3781 (defun vip-prev-heading (arg)
3783 (let ((val (vip-p-val arg))
3784 (com (vip-getCom arg)))
3785 (if com (vip-move-marker-locally 'vip-com-point (point)))
3786 (re-search-backward vip-heading-start nil t val)
3787 (goto-char (match-beginning 0))
3788 (if com (vip-execute-com 'vip-prev-heading nil com))))
3790 (defun vip-heading-end (arg)
3792 (let ((val (vip-p-val arg))
3793 (com (vip-getCom arg)))
3794 (if com (vip-move-marker-locally 'vip-com-point (point)))
3795 (re-search-forward vip-heading-end nil t val)
3796 (goto-char (match-beginning 0))
3797 (if com (vip-execute-com 'vip-heading-end nil com))))
3799 (defun vip-next-heading (arg)
3801 (let ((val (vip-p-val arg))
3802 (com (vip-getCom arg)))
3803 (if com (vip-move-marker-locally 'vip-com-point (point)))
3805 (re-search-forward vip-heading-start nil t val)
3806 (goto-char (match-beginning 0))
3807 (if com (vip-execute-com 'vip-next-heading nil com))))
3812 (setq scroll-step 1)
3814 (defun vip-scroll (arg)
3815 "Scroll to next screen."
3820 (setq arg (1- arg)))
3823 (setq arg (1+ arg)))))
3825 (defun vip-scroll-back (arg)
3826 "Scroll to previous screen."
3828 (vip-scroll (- arg)))
3830 (defun vip-scroll-down (arg)
3831 "Pull down half screen."
3835 (scroll-down (/ (window-height) 2))
3838 (message "Beginning of buffer")
3839 (goto-char (point-min)))))
3841 (defun vip-scroll-down-one (arg)
3842 "Scroll up one line."
3846 (defun vip-scroll-up (arg)
3847 "Pull up half screen."
3851 (scroll-up (/ (window-height) 2))
3854 (message "End of buffer")
3855 (goto-char (point-max)))))
3857 (defun vip-scroll-up-one (arg)
3858 "Scroll down one line."
3865 (defun vip-if-string (prompt)
3866 (let ((s (vip-read-string-with-history
3870 (car vip-search-history))))
3871 (if (not (string= s ""))
3872 (setq vip-s-string s))))
3875 (defun vip-toggle-search-style (arg)
3876 "Toggle the value of vip-case-fold-search/vip-re-search.
3877 Without prefix argument, will ask which search style to toggle. With prefix
3878 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
3880 Although this function is bound to \\[vip-toggle-search-style], the most
3881 convenient way to use it is to bind `//' to the macro
3882 `1 M-x vip-toggle-search-style' and `///' to
3883 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
3884 toggle case-fold-search and hitting `/' three times witth toggle regexp
3885 search. Macros are more convenient in this case because they don't affect
3886 the Emacs binding of `/'."
3889 (cond ((or (eq arg 1)
3891 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3892 (if vip-case-fold-search
3893 "case-insensitive" "case-sensitive")
3894 (if vip-case-fold-search
3896 "case-insensitive")))))
3897 (setq vip-case-fold-search (null vip-case-fold-search))
3898 (if vip-case-fold-search
3899 (setq msg "Search becomes case-insensitive")
3900 (setq msg "Search becomes case-sensitive")))
3903 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3905 "regexp-search" "vanilla-search")
3908 "regexp-search")))))
3909 (setq vip-re-search (null vip-re-search))
3911 (setq msg "Search becomes regexp-style")
3912 (setq msg "Search becomes vanilla-style")))
3914 (setq msg "Search style remains unchanged")))
3918 (defun vip-search-forward (arg)
3919 "Search a string forward.
3920 ARG is used to find the ARG's occurrence of the string.
3921 Null string will repeat previous search."
3923 (let ((val (vip-P-val arg))
3924 (com (vip-getcom arg))
3925 (old-str vip-s-string))
3926 (setq vip-s-forward t)
3928 ;; this is not used at present, but may be used later
3929 (if (or (not (equal old-str vip-s-string))
3930 (not (markerp vip-local-search-start-marker))
3931 (not (marker-buffer vip-local-search-start-marker)))
3932 (setq vip-local-search-start-marker (point-marker)))
3933 (vip-search vip-s-string t val)
3936 (vip-move-marker-locally 'vip-com-point (mark t))
3937 (vip-execute-com 'vip-search-next val com)))))
3939 (defun vip-search-backward (arg)
3940 "Search a string backward.
3941 ARG is used to find the ARG's occurrence of the string.
3942 Null string will repeat previous search."
3944 (let ((val (vip-P-val arg))
3945 (com (vip-getcom arg))
3946 (old-str vip-s-string))
3947 (setq vip-s-forward nil)
3949 ;; this is not used at present, but may be used later
3950 (if (or (not (equal old-str vip-s-string))
3951 (not (markerp vip-local-search-start-marker))
3952 (not (marker-buffer vip-local-search-start-marker)))
3953 (setq vip-local-search-start-marker (point-marker)))
3954 (vip-search vip-s-string nil val)
3957 (vip-move-marker-locally 'vip-com-point (mark t))
3958 (vip-execute-com 'vip-search-next val com)))))
3961 ;; Search for COUNT's occurrence of STRING.
3962 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3963 ;; INIT-POINT is the position where search is to start.
3965 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3966 (defun vip-search (string forward arg
3967 &optional no-offset init-point fail-if-not-found)
3968 (if (not (equal string ""))
3969 (let ((val (vip-p-val arg))
3970 (com (vip-getcom arg))
3971 (offset (not no-offset))
3972 (case-fold-search vip-case-fold-search)
3973 (start-point (or init-point (point))))
3974 (vip-deactivate-mark)
3978 (if offset (vip-forward-char-carefully))
3981 (re-search-forward string nil nil val)
3982 (re-search-backward string))
3983 (search-forward string nil nil val)
3984 (search-backward string))
3985 ;; don't wait and don't flash in macros
3986 (or executing-kbd-macro
3987 (vip-flash-search-pattern))
3988 (if (not (equal start-point (point)))
3989 (push-mark start-point t)))
3991 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
3993 (message "Search wrapped around end of buffer")
3994 (goto-char (point-min))
3995 (vip-search string forward (cons 1 com) t start-point 'fail)
3996 ;; don't wait in macros
3997 (or executing-kbd-macro (sit-for 2))
3998 ;; delete the wrap-around message
4001 (goto-char start-point)
4002 (error "`%s': %s not found"
4004 (if vip-re-search "Pattern" "String"))
4010 (re-search-backward string nil nil val)
4011 (search-backward string nil nil val))
4012 ;; don't wait and don't flash in macros
4013 (or executing-kbd-macro
4014 (vip-flash-search-pattern))
4015 (if (not (equal start-point (point)))
4016 (push-mark start-point t)))
4018 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4020 (message "Search wrapped around beginning of buffer")
4021 (goto-char (point-max))
4022 (vip-search string forward (cons 1 com) t start-point 'fail)
4023 ;; don't wait in macros
4024 (or executing-kbd-macro (sit-for 2))
4025 ;; delete the wrap-around message
4028 (goto-char start-point)
4029 (error "`%s': %s not found"
4031 (if vip-re-search "Pattern" "String"))
4034 (defun vip-search-next (arg)
4035 "Repeat previous search."
4037 (let ((val (vip-p-val arg))
4038 (com (vip-getcom arg)))
4039 (if (null vip-s-string) (error vip-NoPrevSearch))
4040 (vip-search vip-s-string vip-s-forward arg)
4043 (vip-move-marker-locally 'vip-com-point (mark t))
4044 (vip-execute-com 'vip-search-next val com)))))
4046 (defun vip-search-Next (arg)
4047 "Repeat previous search in the reverse direction."
4049 (let ((val (vip-p-val arg))
4050 (com (vip-getcom arg)))
4051 (if (null vip-s-string) (error vip-NoPrevSearch))
4052 (vip-search vip-s-string (not vip-s-forward) arg)
4055 (vip-move-marker-locally 'vip-com-point (mark t))
4056 (vip-execute-com 'vip-search-Next val com)))))
4059 ;; Search contents of buffer defined by one of Viper's motion commands.
4060 ;; Repeatable via `n' and `N'.
4061 (defun vip-buffer-search-enable (&optional c)
4062 (cond (c (setq vip-buffer-search-char c))
4063 ((null vip-buffer-search-char)
4064 (setq vip-buffer-search-char ?g)))
4065 (define-key vip-vi-basic-map
4066 (char-to-string vip-buffer-search-char) 'vip-command-argument)
4067 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
4068 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
4070 ;; This is a Viper wraper for isearch-forward.
4071 (defun vip-isearch-forward (arg)
4072 "Do incremental search forward."
4074 ;; emacs bug workaround
4075 (if (listp arg) (setq arg (car arg)))
4076 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
4078 ;; This is a Viper wraper for isearch-backward."
4079 (defun vip-isearch-backward (arg)
4080 "Do incremental search backward."
4082 ;; emacs bug workaround
4083 (if (listp arg) (setq arg (car arg)))
4084 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
4087 ;; visiting and killing files, buffers
4089 (defun vip-switch-to-buffer ()
4090 "Switch to buffer in the current window."
4095 (format "Switch to buffer in this window \(%s\): "
4096 (buffer-name (other-buffer (current-buffer))))))
4097 (switch-to-buffer buffer)
4100 (defun vip-switch-to-buffer-other-window ()
4101 "Switch to buffer in another window."
4106 (format "Switch to buffer in another window \(%s\): "
4107 (buffer-name (other-buffer (current-buffer))))))
4108 (switch-to-buffer-other-window buffer)
4111 (defun vip-kill-buffer ()
4114 (let (buffer buffer-name)
4117 (format "Kill buffer \(%s\): "
4118 (buffer-name (current-buffer)))))
4120 (if (null buffer-name)
4122 (get-buffer buffer-name)))
4123 (if (null buffer) (error "`%s': No such buffer" buffer-name))
4124 (if (or (not (buffer-modified-p buffer))
4127 "Buffer `%s' is modified, are you sure you want to kill it? "
4129 (kill-buffer buffer)
4130 (error "Buffer not killed"))))
4133 (defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p")
4134 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
4135 This is useful when you the current directory contains files with the same
4136 prefix and many different suffixes. Usually, only one of the suffixes
4137 represents an editable file. However, file completion will stop at the `.'
4138 The smart suffix feature lets you hit RET in such a case, and Viper will
4139 select the appropriate suffix.
4141 Suffixes are tried in the order given and the first suffix for which a
4142 corresponding file exists is selected. If no file exists for any of the
4143 suffixes, the user is asked to confirm.
4145 To turn this feature off, set this variable to nil.")
4147 ;; Try to add suffix to files ending with a `.'
4148 ;; Useful when the user hits RET on a non-completed file name.
4149 (defun vip-file-add-suffix ()
4151 (len (length vip-smart-suffix-list))
4152 (file (buffer-string))
4154 (goto-char (point-max))
4155 (if (and vip-smart-suffix-list (string-match "\\.$" file))
4157 (while (and (not found) (< count len))
4158 (setq suff (nth count vip-smart-suffix-list)
4160 (if (file-exists-p (format "%s%s" file suff))
4167 (vip-tmp-insert-at-eob " [Please complete file name]")
4169 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
4171 (key-binding (setq key (read-key-sequence nil))))
4172 (cond ((eq cmd 'self-insert-command)
4174 (insert (events-to-keys key))
4176 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
4178 (t (command-execute cmd)))
4184 ;; Advice for use in find-file and read-file-name commands.
4185 (defadvice exit-minibuffer (before vip-exit-minibuffer-advice activate)
4186 "Run `vip-minibuffer-exit-hook' just before exiting the minibuffer."
4187 (run-hooks 'vip-minibuffer-exit-hook))
4189 (defadvice find-file (before vip-add-suffix-advice activate)
4190 "Use `read-file-name' for reading arguments."
4191 (interactive (list (read-file-name "Find file: "
4192 nil default-directory))))
4194 (defadvice find-file-other-window (before vip-add-suffix-advice activate)
4195 "Use `read-file-name' for reading arguments."
4196 (interactive (list (read-file-name "Find file in other window: "
4197 nil default-directory))))
4199 (defadvice find-file-other-frame (before vip-add-suffix-advice activate)
4200 "Use `read-file-name' for reading arguments."
4201 (interactive (list (read-file-name "Find file in other frame: "
4202 nil default-directory))))
4204 (defadvice read-file-name (around vip-suffix-advice activate)
4205 "Tell `exit-minibuffer' to run `vip-file-add-suffix' as a hook."
4206 (let ((vip-minibuffer-exit-hook 'vip-file-add-suffix))
4213 (defsubst vip-yank (text)
4214 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
4216 (setq this-command 'yank))
4218 (defun vip-put-back (arg)
4219 "Put back after point/below line."
4221 (let ((val (vip-p-val arg))
4222 (text (if vip-use-register
4223 (cond ((vip-valid-register vip-use-register '(digit))
4224 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4225 ((vip-valid-register vip-use-register)
4226 (get-register (downcase vip-use-register)))
4227 (t (error vip-InvalidRegister vip-use-register)))
4230 (if vip-use-register
4231 (let ((reg vip-use-register))
4232 (setq vip-use-register nil)
4233 (error vip-EmptyRegister reg))
4235 (setq vip-use-register nil)
4236 (if (vip-end-with-a-newline-p text)
4241 (beginning-of-line))
4242 (if (not (eolp)) (vip-forward-char-carefully)))
4243 (set-marker (vip-mark-marker) (point) (current-buffer))
4244 (vip-set-destructive-command
4245 (list 'vip-put-back val nil vip-use-register nil nil))
4246 (vip-loop val (vip-yank text)))
4247 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4248 ;; newline; it leaves the cursor at the beginning when the text contains
4250 (if (vip-same-line (point) (mark))
4251 (or (= (point) (mark)) (vip-backward-char-carefully))
4252 (exchange-point-and-mark)
4254 (back-to-indentation)))
4255 (vip-deactivate-mark))
4257 (defun vip-Put-back (arg)
4258 "Put back at point/above line."
4260 (let ((val (vip-p-val arg))
4261 (text (if vip-use-register
4262 (cond ((vip-valid-register vip-use-register '(digit))
4263 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4264 ((vip-valid-register vip-use-register)
4265 (get-register (downcase vip-use-register)))
4266 (t (error vip-InvalidRegister vip-use-register)))
4269 (if vip-use-register
4270 (let ((reg vip-use-register))
4271 (setq vip-use-register nil)
4272 (error vip-EmptyRegister reg))
4274 (setq vip-use-register nil)
4275 (if (vip-end-with-a-newline-p text) (beginning-of-line))
4276 (vip-set-destructive-command
4277 (list 'vip-Put-back val nil vip-use-register nil nil))
4278 (set-marker (vip-mark-marker) (point) (current-buffer))
4279 (vip-loop val (vip-yank text)))
4280 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4281 ;; newline; it leaves the cursor at the beginning when the text contains
4283 (if (vip-same-line (point) (mark))
4284 (or (= (point) (mark)) (vip-backward-char-carefully))
4285 (exchange-point-and-mark)
4287 (back-to-indentation)))
4288 (vip-deactivate-mark))
4291 ;; Copy region to kill-ring.
4292 ;; If BEG and END do not belong to the same buffer, copy empty region.
4293 (defun vip-copy-region-as-kill (beg end)
4295 (copy-region-as-kill beg end)
4296 (error (copy-region-as-kill beg beg))))
4298 ;; Saves last inserted text for possible use by vip-repeat command.
4299 (defun vip-save-last-insertion (beg end)
4300 (setq vip-last-insertion (buffer-substring beg end))
4301 (or (< (length vip-d-com) 5)
4302 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
4303 (or (null vip-command-ring)
4304 (ring-empty-p vip-command-ring)
4306 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
4308 ;; del most recent elt, if identical to the second most-recent
4309 (vip-cleanup-ring vip-command-ring)))
4312 (defsubst vip-yank-last-insertion ()
4313 "Inserts the text saved by the previous vip-save-last-insertion command."
4315 (insert vip-last-insertion)
4319 (defun vip-delete-char (arg)
4322 (let ((val (vip-p-val arg)))
4323 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
4326 (let ((here (point)))
4328 (if (> val (- (point) here))
4329 (setq val (- (point) here))))))
4330 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
4331 (if (and vip-ex-style-motion (eolp))
4332 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
4333 (if vip-use-register
4335 (cond ((vip-valid-register vip-use-register '((Letter)))
4336 (vip-append-to-register
4337 (downcase vip-use-register) (point) (- (point) val)))
4338 ((vip-valid-register vip-use-register)
4340 vip-use-register (point) (- (point) val) nil))
4341 (t (error vip-InvalidRegister vip-use-register)))
4342 (setq vip-use-register nil)))
4343 (if vip-ex-style-motion
4346 (if (and (eolp) (not (bolp))) (backward-char 1)))
4348 (delete-backward-char val t)
4349 (delete-char val t)))))
4351 (defun vip-delete-backward-char (arg)
4352 "Delete previous character. On reaching beginning of line, stop and beep."
4354 (let ((val (vip-p-val arg)))
4355 (vip-set-destructive-command
4356 (list 'vip-delete-backward-char val nil nil nil nil))
4359 (let ((here (point)))
4361 (if (> val (- here (point)))
4362 (setq val (- here (point)))))))
4363 (if vip-use-register
4365 (cond ((vip-valid-register vip-use-register '(Letter))
4366 (vip-append-to-register
4367 (downcase vip-use-register) (point) (+ (point) val)))
4368 ((vip-valid-register vip-use-register)
4370 vip-use-register (point) (+ (point) val) nil))
4371 (t (error vip-InvalidRegister vip-use-register)))
4372 (setq vip-use-register nil)))
4374 (delete-backward-char val t))))
4376 (defun vip-del-backward-char-in-insert ()
4377 "Delete 1 char backwards while in insert mode."
4379 (if (and vip-ex-style-editing-in-insert (bolp))
4381 (delete-backward-char 1 t)))
4383 (defun vip-del-backward-char-in-replace ()
4384 "Delete one character in replace mode.
4385 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
4386 charecters. If it is nil, then the cursor just moves backwards, similarly
4387 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
4388 cursor move past the beginning of line."
4390 (cond (vip-delete-backwards-in-replace
4392 (delete-backward-char 1 t))
4393 (vip-ex-style-editing-in-insert
4398 (delete-backward-char 1 t))))
4399 (vip-ex-style-editing-in-insert
4404 (backward-char 1))))
4410 (defun vip-join-lines (arg)
4411 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4413 (let ((val (vip-P-val arg)))
4414 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
4415 (vip-loop (if (null val) 1 (1- val))
4421 (delete-region (point) (1- (point)))
4422 (fixup-whitespace)))))))
4427 (defun vip-change (beg end)
4428 (if (markerp beg) (setq beg (marker-position beg)))
4429 (if (markerp end) (setq end (marker-position end)))
4430 ;; beg is sometimes (mark t), which may be nil
4431 (or beg (setq beg end))
4433 (vip-set-complex-command-for-undo)
4434 (if vip-use-register
4436 (copy-to-register vip-use-register beg end nil)
4437 (setq vip-use-register nil)))
4438 (vip-set-replace-overlay beg end)
4439 (setq last-command nil) ; separate repl text from prev kills
4441 (if (= (vip-replace-start) (point-max))
4442 (error "End of buffer"))
4444 (setq vip-last-replace-region
4445 (buffer-substring (vip-replace-start)
4448 ;; protect against error while inserting "@" and other disasters
4449 ;; (e.g., read-only buff)
4450 (condition-case conds
4451 (if (vip-same-line (vip-replace-start)
4454 ;; tabs cause problems in replace, so untabify
4455 (goto-char (vip-replace-end))
4456 (insert-before-markers "@") ; put placeholder after the TAB
4457 (untabify (vip-replace-start) (point))
4458 ;; del @, don't put on kill ring
4459 (delete-backward-char 1)
4461 (vip-set-replace-overlay-glyphs
4462 vip-replace-region-start-delimiter
4463 vip-replace-region-end-delimiter)
4464 ;; this move takes care of the last posn in the overlay, which
4465 ;; has to be shifted because of insert. We can't simply insert
4466 ;; "$" before-markers because then overlay-start will shift the
4467 ;; beginning of the overlay in case we are replacing a single
4468 ;; character. This fixes the bug with `s' and `cl' commands.
4469 (vip-move-replace-overlay (vip-replace-start) (point))
4470 (goto-char (vip-replace-start))
4471 (vip-change-state-to-replace t))
4472 (kill-region (vip-replace-start)
4474 (vip-restore-cursor-color)
4475 (vip-change-state-to-insert))
4476 (error ;; make sure that the overlay doesn't stay.
4477 ;; go back to the original point
4478 (goto-char (vip-replace-start))
4479 (vip-hide-replace-overlay)
4480 (vip-message-conditions conds))))
4483 (defun vip-change-subr (beg end)
4484 ;; beg is sometimes (mark t), which may be nil
4485 (or beg (setq beg end))
4487 (if vip-use-register
4489 (copy-to-register vip-use-register beg end nil)
4490 (setq vip-use-register nil)))
4491 (kill-region beg end)
4492 (setq this-command 'vip-change)
4493 (vip-yank-last-insertion))
4495 (defun vip-toggle-case (arg)
4496 "Toggle character case."
4498 (let ((val (vip-p-val arg)) (c))
4499 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
4501 (setq c (following-char))
4503 (if (eq c (upcase c))
4504 (insert-char (downcase c) 1)
4505 (insert-char (upcase c) 1))
4506 (if (eolp) (backward-char 1))
4507 (setq val (1- val)))))
4512 (defun vip-query-replace ()
4514 If a null string is suplied as the string to be replaced,
4515 the query replace mode will toggle between string replace
4516 and regexp replace."
4519 (setq str (vip-read-string-with-history
4520 (if vip-re-query-replace "Query replace regexp: "
4523 'vip-replace1-history
4524 (car vip-replace1-history) ; default
4526 (if (string= str "")
4528 (setq vip-re-query-replace (not vip-re-query-replace))
4529 (message "Query replace mode changed to %s"
4530 (if vip-re-query-replace "regexp replace"
4532 (if vip-re-query-replace
4533 (query-replace-regexp
4535 (vip-read-string-with-history
4536 (format "Query replace regexp `%s' with: " str)
4538 'vip-replace1-history
4539 (car vip-replace1-history) ; default
4543 (vip-read-string-with-history
4544 (format "Query replace `%s' with: " str)
4546 'vip-replace1-history
4547 (car vip-replace1-history) ; default
4553 (defun vip-mark-beginning-of-buffer ()
4554 "Mark beginning of buffer."
4557 (goto-char (point-min))
4558 (exchange-point-and-mark)
4559 (message "Mark set at the beginning of buffer"))
4561 (defun vip-mark-end-of-buffer ()
4562 "Mark end of buffer."
4565 (goto-char (point-max))
4566 (exchange-point-and-mark)
4567 (message "Mark set at the end of buffer"))
4569 (defun vip-mark-point ()
4570 "Set mark at point of buffer."
4572 (let ((char (vip-read-char-exclusive)))
4573 (cond ((and (<= ?a char) (<= char ?z))
4574 (point-to-register (1+ (- char ?a))))
4575 ((= char ?<) (vip-mark-beginning-of-buffer))
4576 ((= char ?>) (vip-mark-end-of-buffer))
4577 ((= char ?.) (vip-set-mark-if-necessary))
4578 ((= char ?,) (vip-cycle-through-mark-ring))
4579 ((= char ?D) (mark-defun))
4583 ;; Algorithm: If first invocation of this command save mark on ring, goto
4584 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4585 ;; making it into the new mark, M1.
4586 ;; Push this mark back and set mark to the original point position, p1.
4587 ;; So, if you hit '' or `` then you can return to p1.
4589 ;; If repeated command, pop top elt from the ring into mark and
4590 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4591 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4592 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4594 (defun vip-cycle-through-mark-ring ()
4595 "Visit previous locations on the mark ring.
4596 One can use `` and '' to temporarily jump 1 step back."
4597 (let* ((sv-pt (point)))
4598 ;; if repeated `m,' command, pop the previously saved mark.
4599 ;; Prev saved mark is actually prev saved point. It is used if the
4600 ;; user types `` or '' and is discarded
4601 ;; from the mark ring by the next `m,' command.
4602 ;; In any case, go to the previous or previously saved mark.
4603 ;; Then push the current mark (popped off the ring) and set current
4604 ;; point to be the mark. Current pt as mark is discarded by the next
4606 (if (eq last-command 'vip-cycle-through-mark-ring)
4608 ;; save current mark if the first iteration
4609 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4611 (push-mark (mark t) t)) )
4613 (set-mark-command 1)
4614 ;; don't duplicate mark on the ring
4615 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4617 (vip-deactivate-mark)
4618 (setq this-command 'vip-cycle-through-mark-ring)
4622 (defun vip-goto-mark (arg)
4625 (let ((char (read-char))
4626 (com (vip-getcom arg)))
4627 (vip-goto-mark-subr char com nil)))
4629 (defun vip-goto-mark-and-skip-white (arg)
4630 "Go to mark and skip to first non-white character on line."
4632 (let ((char (read-char))
4633 (com (vip-getCom arg)))
4634 (vip-goto-mark-subr char com t)))
4636 (defun vip-goto-mark-subr (char com skip-white)
4639 (error "Empty buffer")
4641 (cond ((vip-valid-register char '(letter))
4642 (let* ((buff (current-buffer))
4643 (reg (1+ (- char ?a)))
4644 (text-marker (get-register reg)))
4645 (if com (vip-move-marker-locally 'vip-com-point (point)))
4646 (if (not (vip-valid-marker text-marker))
4647 (error (format vip-EmptyTextmarker char)))
4648 (if (and (vip-same-line (point) vip-last-jump)
4649 (= (point) vip-last-jump-ignore))
4650 (push-mark vip-last-jump t)
4651 (push-mark nil t)) ; no msg
4652 (vip-register-to-point reg)
4653 (setq vip-last-jump (point-marker))
4655 (back-to-indentation)
4656 (setq vip-last-jump-ignore (point))))
4658 (if (equal buff (current-buffer))
4659 (vip-execute-com (if skip-white
4660 'vip-goto-mark-and-skip-white
4663 (switch-to-buffer buff)
4664 (goto-char vip-com-point)
4665 (vip-change-state-to-vi)
4667 ((and (not skip-white) (= char ?`))
4668 (if com (vip-move-marker-locally 'vip-com-point (point)))
4669 (if (and (vip-same-line (point) vip-last-jump)
4670 (= (point) vip-last-jump-ignore))
4671 (goto-char vip-last-jump))
4672 (if (= (point) (mark t)) (pop-mark))
4673 (exchange-point-and-mark)
4674 (setq vip-last-jump (point-marker)
4675 vip-last-jump-ignore 0)
4676 (if com (vip-execute-com 'vip-goto-mark nil com)))
4677 ((and skip-white (= char ?'))
4678 (if com (vip-move-marker-locally 'vip-com-point (point)))
4679 (if (and (vip-same-line (point) vip-last-jump)
4680 (= (point) vip-last-jump-ignore))
4681 (goto-char vip-last-jump))
4682 (if (= (point) (mark t)) (pop-mark))
4683 (exchange-point-and-mark)
4684 (setq vip-last-jump (point))
4685 (back-to-indentation)
4686 (setq vip-last-jump-ignore (point))
4687 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
4688 (t (error vip-InvalidTextmarker char))))
4690 (defun vip-insert-tab ()
4694 (defun vip-exchange-point-and-mark ()
4696 (exchange-point-and-mark)
4697 (back-to-indentation))
4699 ;; Input Mode Indentation
4701 (defun vip-forward-indent ()
4702 "Indent forward -- `C-t' in Vi."
4705 (indent-to (+ (current-column) vip-shift-width)))
4707 (defun vip-backward-indent ()
4708 "Backtab, C-d in VI"
4711 (let ((p (point)) (c (current-column)) bol (indent t))
4712 (if (vip-looking-back "[0^]")
4714 (if (= ?^ (preceding-char)) (setq vip-preserve-indent t))
4715 (delete-backward-char 1)
4721 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4722 (delete-region (point) p)
4724 (indent-to (- c vip-shift-width)))
4725 (if (or (bolp) (vip-looking-back "[^ \t]"))
4726 (setq vip-cted nil)))))
4728 (defun vip-autoindent ()
4729 "Auto Indentation, Vi-style."
4731 (let ((col (current-indentation)))
4732 (if (not vip-preserve-indent)
4733 (setq vip-current-indent col)
4734 (setq vip-preserve-indent nil))
4735 ;; don't leave whitespace lines around
4736 (if (memq last-command
4738 vip-open-line vip-Open-line
4739 vip-replace-state-exit-cmd))
4740 (indent-to-left-margin))
4745 (indent-to vip-current-indent)))))
4748 ;; Viewing registers
4750 (defun vip-ket-function (arg)
4751 "Function called by \], the ket. View registers and call \]\]."
4753 (let ((reg (read-char)))
4754 (cond ((vip-valid-register reg '(letter Letter))
4755 (view-register (downcase reg)))
4756 ((vip-valid-register reg '(digit))
4757 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4759 (set-buffer (get-buffer-create "*Output*"))
4760 (delete-region (point-min) (point-max))
4761 (insert (format "Register %c contains the string:\n" reg))
4763 (goto-char (point-min)))
4764 (display-buffer "*Output*")))
4766 (vip-next-heading arg))
4768 vip-InvalidRegister reg)))))
4770 (defun vip-brac-function (arg)
4771 "Function called by \[, the brac. View textmarkers and call \[\["
4773 (let ((reg (read-char)))
4775 (vip-prev-heading arg))
4777 (vip-heading-end arg))
4778 ((vip-valid-register reg '(letter))
4779 (let* ((val (get-register (1+ (- reg ?a))))
4782 (format vip-EmptyTextmarker reg))
4783 (marker-buffer val)))
4784 (pos (marker-position val))
4785 line-no text (s pos) (e pos))
4787 (set-buffer (get-buffer-create "*Output*"))
4788 (delete-region (point-min) (point-max))
4793 (setq line-no (1+ (count-lines (point-min) val)))
4796 (if (re-search-backward "[^ \t]" nil t)
4802 (if (re-search-forward "[^ \t]" nil t)
4806 (setq text (buffer-substring s e))
4807 (setq text (format "%s<%c>%s"
4808 (substring text 0 (- pos s))
4809 reg (substring text (- pos s)))))
4812 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4813 reg (buffer-name buf) line-no))
4814 (insert (format "Here is some text around %c:\n\n %s"
4816 (insert (format vip-EmptyTextmarker reg)))
4817 (goto-char (point-min)))
4818 (display-buffer "*Output*")))
4819 (t (error vip-InvalidTextmarker reg)))))
4823 ;; commands in insertion mode
4825 (defun vip-delete-backward-word (arg)
4826 "Delete previous word."
4831 (delete-region (point) (mark t))
4835 (defun vip-set-expert-level (&optional dont-change-unless)
4836 "Sets the expert level for a Viper user.
4837 Can be called interactively to change (temporarily or permanently) the
4838 current expert level.
4840 The optional argument DONT-CHANGE-UNLESS if not nil, says that
4841 the level should not be changed, unless its current value is
4842 meaningless (i.e., not one of 1,2,3,4,5).
4844 User level determines the setting of Viper variables that are most
4845 sensitive for VI-style look-and-feel."
4849 (if (not (numberp vip-expert-level)) (setq vip-expert-level 0))
4851 (save-window-excursion
4852 (delete-other-windows)
4853 ;; if 0 < vip-expert-level < vip-max-expert-level
4854 ;; & dont-change-unless = t -- use it; else ask
4855 (vip-ask-level dont-change-unless))
4858 vip-ex-style-motion t
4859 vip-ex-style-editing-in-insert t
4860 vip-want-ctl-h-help nil)
4863 ;; a novice or a beginner
4864 ((eq vip-expert-level 1)
4865 (global-set-key vip-toggle-key ;; in emacs-state
4866 (if (vip-window-display-p)
4869 (setq vip-no-multiple-ESC t
4871 vip-vi-style-in-minibuffer t
4872 vip-search-wrap-around-t t
4873 vip-want-emacs-keys-in-vi nil
4874 vip-want-emacs-keys-in-insert nil))
4876 ;; an intermediate to guru
4877 ((and (> vip-expert-level 1) (< vip-expert-level 5))
4878 (setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice)
4879 vip-want-emacs-keys-in-vi t
4880 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4882 (if (eq vip-expert-level 4) ; respect user's ex-style motions
4883 ; and vip-no-multiple-ESC
4885 (setq-default vip-ex-style-editing-in-insert
4886 (cdr (assoc 'vip-ex-style-editing-in-insert
4887 vip-saved-user-settings))
4889 (cdr (assoc 'vip-ex-style-motion
4890 vip-saved-user-settings)))
4891 (setq vip-ex-style-motion
4892 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4893 vip-ex-style-editing-in-insert
4894 (cdr (assoc 'vip-ex-style-editing-in-insert
4895 vip-saved-user-settings))
4897 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4899 (cdr (assoc 'vip-no-multiple-ESC
4900 vip-saved-user-settings))))))
4903 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4904 ;; user toggle variable values.
4905 (t (setq-default vip-ex-style-editing-in-insert
4906 (cdr (assoc 'vip-ex-style-editing-in-insert
4907 vip-saved-user-settings))
4909 (cdr (assoc 'vip-ex-style-motion
4910 vip-saved-user-settings)))
4911 (setq vip-want-ctl-h-help
4912 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
4914 (cdr (assoc 'vip-always vip-saved-user-settings))
4916 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
4918 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4919 vip-ex-style-editing-in-insert
4920 (cdr (assoc 'vip-ex-style-editing-in-insert
4921 vip-saved-user-settings))
4923 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4924 vip-want-emacs-keys-in-vi
4925 (cdr (assoc 'vip-want-emacs-keys-in-vi
4926 vip-saved-user-settings))
4927 vip-want-emacs-keys-in-insert
4928 (cdr (assoc 'vip-want-emacs-keys-in-insert
4929 vip-saved-user-settings)))))
4930 (vip-set-mode-vars-for vip-current-state)
4932 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
4935 ;; Ask user expert level.
4936 (defun vip-ask-level (dont-change-unless)
4937 (let ((ask-buffer " *vip-ask-level*")
4938 level-changed repeated)
4939 (save-window-excursion
4940 (switch-to-buffer ask-buffer)
4942 (or (eq this-command 'vip-set-expert-level)
4944 (<= vip-expert-level vip-max-expert-level)
4945 (>= vip-expert-level 1))
4949 *** Important Notice for VIP users***
4954 Viper Is a Package for Emacs Rebels,
4955 a VI Plan for Emacs Rescue,
4956 and a venomous VI PERil.
4959 Technically speaking, Viper is a new Vi emulator that replaces
4960 the old VIP package.
4962 Viper emulates Vi much better than VIP. It also significantly
4963 extends and improves upon Vi in many useful ways.
4965 Although many VIP settings in your ~/.vip are compatible with Viper,
4966 you may have to change some of them. Please refer to the documentation,
4967 which can be obtained by executing
4971 when Viper is in Vi state.
4973 If you will be so lucky as to find a bug, report it via the command
4977 Type any key to continue... ")
4982 (while (or (> vip-expert-level vip-max-expert-level)
4983 (< vip-expert-level 1)
4984 (null dont-change-unless))
4988 (message "Invalid user level")
4991 (setq dont-change-unless t
4994 Please specify your level of familiarity with the venomous VI PERil
4995 (and the VI Plan for Emacs Rescue).
4996 You can change it at any time by typing `M-x vip-set-expert-level RET'
4998 1 -- BEGINNER: Almost all Emacs features are suppressed.
4999 Feels almost like straight Vi. File name completion and
5000 command history in the minibuffer are thrown in as a bonus.
5001 To use Emacs productively, you must reach level 3 or higher.
5002 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
5003 so most Emacs commands can be used when Viper is in Vi state.
5004 Good progress---you are well on the way to level 3!
5005 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
5006 in Viper's insert state.
5007 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
5008 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
5009 variables. Adjust these settings to your taste.
5010 5 -- WIZARD: Like 4, but user settings are also respected for vip-always,
5011 vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and
5012 vip-want-emacs-keys-in-insert. Adjust these to your taste.
5014 Please, specify your level now: ")
5016 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
5019 ;; tell the user if level was changed
5023 (format "\n\n\n\n\n\t\tYou have selected user level %d"
5025 (if (y-or-n-p "Do you wish to make this change permanent? ")
5026 ;; save the setting for vip-expert-level
5029 (format "Saving user level %d ..." vip-expert-level)
5030 vip-custom-file-name))
5032 (bury-buffer) ; remove ask-buffer from screen
5037 (defun viper-version ()
5039 (message "Viper version is %s" viper-version))
5041 (defalias 'vip-version 'viper-version)
5048 ;; Returns t, if the string before point matches the regexp STR.
5049 (defsubst vip-looking-back (str)
5050 (and (save-excursion (re-search-backward str nil t))
5051 (= (point) (match-end 0))))
5055 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
5056 (defun vip-register-to-point (char &optional enforce-buffer)
5057 "Like jump-to-register, but switches to another buffer in another window."
5058 (interactive "cViper register to point: ")
5059 (let ((val (get-register char)))
5061 ((and (fboundp 'frame-configuration-p)
5062 (frame-configuration-p val))
5063 (set-frame-configuration val))
5064 ((window-configuration-p val)
5065 (set-window-configuration val))
5066 ((vip-valid-marker val)
5067 (if (and enforce-buffer
5068 (not (equal (current-buffer) (marker-buffer val))))
5069 (error (concat vip-EmptyTextmarker " in this buffer")
5071 (pop-to-buffer (marker-buffer val))
5073 ((and (consp val) (eq (car val) 'file))
5074 (find-file (cdr val)))
5076 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
5079 (defun vip-save-kill-buffer ()
5080 "Save then kill current buffer. "
5082 (if (< vip-expert-level 2)
5083 (save-buffers-kill-emacs)
5085 (kill-buffer (current-buffer))))
5091 (defun vip-submit-report ()
5092 "Submit bug report on Viper."
5094 (let ((reporter-prompt-for-summary-p t)
5095 (vip-device-type (vip-device-type))
5096 color-display-p frame-parameters
5097 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
5098 varlist salutation window-config)
5100 ;; If mode info is needed, add variable to `let' and then set it below,
5101 ;; like we did with color-display-p.
5102 (setq color-display-p (if (vip-window-display-p)
5103 (vip-color-display-p)
5105 minibuffer-vi-face (if (vip-window-display-p)
5106 (vip-get-face vip-minibuffer-vi-face)
5108 minibuffer-insert-face (if (vip-window-display-p)
5109 (vip-get-face vip-minibuffer-insert-face)
5111 minibuffer-emacs-face (if (vip-window-display-p)
5112 (vip-get-face vip-minibuffer-emacs-face)
5114 frame-parameters (if (fboundp 'frame-parameters)
5115 (frame-parameters (selected-frame))))
5117 (setq varlist (list 'vip-vi-minibuffer-minor-mode
5118 'vip-insert-minibuffer-minor-mode
5119 'vip-vi-intercept-minor-mode
5120 'vip-vi-local-user-minor-mode
5121 'vip-vi-kbd-minor-mode
5122 'vip-vi-global-user-minor-mode
5123 'vip-vi-state-modifier-minor-mode
5124 'vip-vi-diehard-minor-mode
5125 'vip-vi-basic-minor-mode
5126 'vip-replace-minor-mode
5127 'vip-insert-intercept-minor-mode
5128 'vip-insert-local-user-minor-mode
5129 'vip-insert-kbd-minor-mode
5130 'vip-insert-global-user-minor-mode
5131 'vip-insert-state-modifier-minor-mode
5132 'vip-insert-diehard-minor-mode
5133 'vip-insert-basic-minor-mode
5134 'vip-emacs-intercept-minor-mode
5135 'vip-emacs-local-user-minor-mode
5136 'vip-emacs-kbd-minor-mode
5137 'vip-emacs-global-user-minor-mode
5138 'vip-emacs-state-modifier-minor-mode
5139 'vip-automatic-iso-accents
5140 'vip-want-emacs-keys-in-insert
5141 'vip-want-emacs-keys-in-vi
5142 'vip-keep-point-on-undo
5143 'vip-no-multiple-ESC
5145 'vip-want-ctl-h-help
5146 'vip-ex-style-editing-in-insert
5147 'vip-delete-backwards-in-replace
5148 'vip-vi-style-in-minibuffer
5150 'vip-insert-state-hook
5151 'vip-replace-state-hook
5152 'vip-emacs-state-hook
5153 'ex-cycle-other-window
5154 'ex-cycle-through-non-files
5161 'minibuffer-insert-face
5162 'minibuffer-emacs-face
5165 Congratulations! You may have unearthed a bug in Viper!
5166 Please mail a concise, accurate summary of the problem to the address above.
5168 -------------------------------------------------------------------")
5169 (setq window-config (current-window-configuration))
5170 (with-output-to-temp-buffer " *vip-info*"
5171 (switch-to-buffer " *vip-info*")
5172 (delete-other-windows)
5174 PLEASE FOLLOW THESE PROCEDURES
5175 ------------------------------
5177 Before reporting a bug, please verify that it is related to Viper, and is
5178 not cause by other packages you are using.
5180 Don't report compilation warnings, unless you are certain that there is a
5181 problem. These warnings are normal and unavoidable.
5183 Please note that users should not modify variables and keymaps other than
5184 those advertised in the manual. Such `customization' is likely to crash
5185 Viper, as it would any other improperly customized Emacs package.
5187 If you are reporting an error message received while executing one of the
5188 Viper commands, type:
5190 M-x set-variable <Return> debug-on-error <Return> t <Return>
5192 Then reproduce the error. The above command will cause Emacs to produce a
5193 back trace of the execution that leads to the error. Please include this
5194 trace in your bug report.
5196 If you believe that one of Viper's commands goes into an infinite loop
5197 \(e.g., Emacs freezes\), type:
5199 M-x set-variable <Return> debug-on-quit <Return> t <Return>
5201 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
5202 the current command. Include the resulting back trace in the bug report.
5204 Mail anyway (y or n)? ")
5205 (if (y-or-n-p "Mail anyway? ")
5207 (set-window-configuration window-config)
5208 (error "Bug report aborted")))
5211 (set-window-configuration window-config)
5213 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
5216 nil 'delete-other-windows
5223 ;; Smoothes out the difference between Emacs' unread-command-events
5224 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
5225 ;; events or a sequence of keys.
5227 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
5228 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
5229 ;; into an event. Below, we delete nil from event lists, since nil is the most
5230 ;; common symbol that might appear in this wrong context.
5231 (defun vip-set-unread-command-events (arg)
5234 unread-command-events
5236 (cond ((eventp arg) (list arg))
5239 (listify-key-sequence arg))
5241 "vip-set-unread-command-events: Invalid argument, %S"
5243 (if (not (eventp nil))
5244 (setq new-events (delq nil new-events)))
5245 (append new-events unread-command-events)))
5248 unread-command-events
5250 (cond ((numberp arg) (list (character-to-event arg)))
5251 ((eventp arg) (list arg))
5252 ((stringp arg) (mapcar 'character-to-event arg))
5253 ((vectorp arg) (append arg nil)) ; turn into list
5254 ((listp arg) (vip-eventify-list-xemacs arg))
5256 "vip-set-unread-command-events: Invalid argument, %S" arg)))
5257 unread-command-events))))
5259 ;; list is assumed to be a list of events of characters
5260 (defun vip-eventify-list-xemacs (lis)
5262 (function (lambda (elt)
5263 (cond ((numberp elt) (character-to-event elt))
5266 "vip-eventify-list-xemacs: can't convert to event, %S"
5272 ;;; Bring in the rest of the files
5273 (require 'viper-mous)
5274 (require 'viper-macs)
5279 ;; The following is provided for compatibility with older VIP's
5281 (defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
5282 (defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert)
5283 (defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs)
5287 ;;; Load .vip and set up hooks
5289 ;; This hook designed to enable Vi-style editing in comint-based modes."
5290 (defun vip-comint-mode-hook ()
5291 (setq require-final-newline nil)
5292 (setq vip-ex-style-editing-in-insert nil
5293 vip-ex-style-motion nil)
5294 (vip-add-local-keys 'vi-state
5295 '(("\C-m" . comint-send-input) ; return
5296 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5297 (vip-add-local-keys 'insert-state
5298 '(("\C-m" . comint-send-input) ; return
5299 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5303 ;; This sets major mode hooks to make them come up in vi-state.
5304 (defun vip-set-hooks ()
5306 ;; It is of course a misnomer to call viper-mode a `major mode'.
5307 ;; However, this has the effect that if the user didn't specify the
5308 ;; default mode, new buffers that fall back on the default will come up
5309 ;; in Fundamental Mode and Vi state.
5310 (setq default-major-mode 'viper-mode)
5312 ;; The following major modes should come up in vi-state
5313 (defadvice fundamental-mode (after vip-fundamental-mode-ad activate)
5314 "Run `vip-change-state-to-vi' on entry."
5315 (vip-change-state-to-vi))
5317 (defvar help-mode-hook nil)
5318 (add-hook 'help-mode-hook 'viper-mode)
5320 (defvar emacs-lisp-mode-hook nil)
5321 (add-hook 'emacs-lisp-mode-hook 'viper-mode)
5323 (defvar html-mode-hook nil)
5324 (add-hook 'html-mode-hook 'viper-mode)
5326 (defvar lisp-mode-hook nil)
5327 (add-hook 'lisp-mode-hook 'viper-mode)
5329 (defvar bibtex-mode-hook nil)
5330 (add-hook 'bibtex-mode-hook 'viper-mode)
5332 (defvar cc-mode-hook nil)
5333 (add-hook 'cc-mode-hook 'viper-mode)
5335 (defvar c-mode-hook nil)
5336 (add-hook 'c-mode-hook 'viper-mode)
5338 (defvar c++-mode-hook nil)
5339 (add-hook 'c++-mode-hook 'viper-mode)
5341 (defvar lisp-interaction-mode-hook nil)
5342 (add-hook 'lisp-interaction-mode-hook 'viper-mode)
5344 (defvar text-mode-hook nil)
5345 (add-hook 'text-mode-hook 'viper-mode)
5347 (add-hook 'completion-list-mode-hook 'viper-mode)
5348 (add-hook 'compilation-mode-hook 'viper-mode)
5350 (defvar emerge-startup-hook nil)
5351 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
5352 ;; Run vip-change-state-to-vi after quitting emerge.
5353 (vip-eval-after-load
5355 '(defadvice emerge-quit (after vip-emerge-advice activate)
5356 "Run `vip-change-state-to-vi' after quitting emerge."
5357 (vip-change-state-to-vi)))
5358 ;; In case Emerge was loaded before Viper.
5359 (defadvice emerge-quit (after vip-emerge-advice activate)
5360 "Run `vip-change-state-to-vi' after quitting emerge."
5361 (vip-change-state-to-vi))
5363 (vip-eval-after-load
5365 '(defadvice asm-mode (after vip-asm-mode-ad activate)
5366 "Run `vip-change-state-to-vi' on entry."
5367 (vip-change-state-to-vi)))
5369 ;; passwd.el sets up its own buffer, which turns up in Vi mode,
5370 ;; thus overriding the local map. We don't need Vi mode here.
5371 (vip-eval-after-load
5373 '(defadvice read-passwd-1 (before vip-passwd-ad activate)
5374 "Switch to emacs state while reading password."
5375 (vip-change-state-to-emacs)))
5377 ;; Emacs shell, ange-ftp, and comint-based modes
5378 (defvar comint-mode-hook nil)
5379 (add-hook 'comint-mode-hook 'vip-change-state-to-insert)
5380 (add-hook 'comint-mode-hook 'vip-comint-mode-hook)
5383 (defvar sh-mode-hook nil)
5384 (add-hook 'sh-mode-hook 'viper-mode)
5387 ;; This is only necessary when the user uses vip-modify-major-mode
5388 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
5392 (defvar view-mode-hook nil
5393 "View hook. Run after view mode.")
5394 (add-hook 'view-mode-hook 'vip-change-state-to-emacs))
5395 (defadvice view-minor-mode (after vip-view-ad activate)
5396 "Switch to Emacs state in View mode."
5397 (vip-change-state-to-emacs))
5398 (defvar view-hook nil
5399 "View hook. Run after view mode.")
5400 (add-hook 'view-hook 'vip-change-state-to-emacs))
5403 ;; Put summary and other VM buffers in Emacs state.
5404 (defvar vm-mode-hooks nil
5405 "This hook is run after vm is started.")
5406 (defvar vm-summary-mode-hooks nil
5407 "This hook is run after vm switches to summary mode.")
5408 (add-hook 'vm-mode-hooks 'vip-change-state-to-emacs)
5409 (add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs)
5412 ;; Put buf in Emacs state after edit.
5413 (vip-eval-after-load
5415 '(defadvice rmail-cease-edit (after vip-rmail-advice activate)
5416 "Switch to emacs state when done editing message."
5417 (vip-change-state-to-emacs)))
5418 ;; In case RMAIL was loaded before Viper.
5419 (defadvice rmail-cease-edit (after vip-rmail-advice activate)
5420 "Switch to emacs state when done editing message."
5421 (vip-change-state-to-emacs))
5424 ;; Set some useful macros
5425 ;; These must be before we load .vip, so the user can unrecord them.
5427 ;; repeat the 2nd previous command without rotating the command history
5428 (vip-record-kbd-macro
5429 (vector vip-repeat-from-history-key '\1) 'vi-state
5430 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5431 ;; repeat the 3d previous command without rotating the command history
5432 (vip-record-kbd-macro
5433 (vector vip-repeat-from-history-key '\2) 'vi-state
5434 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5436 ;; toggle case sensitivity in search
5437 (vip-record-kbd-macro
5439 [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)
5440 ;; toggle regexp/vanila search
5441 (vip-record-kbd-macro
5443 [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)
5446 ;; ~/.vip is loaded if it exists
5447 (if (and (file-exists-p vip-custom-file-name)
5448 (not noninteractive))
5449 (load vip-custom-file-name))
5451 ;; VIP compatibility: merge whatever the user has in vip-mode-map into
5452 ;; Viper's basic map.
5453 (vip-add-keymap vip-mode-map vip-vi-global-user-map)
5456 ;; Applying Viper customization -- runs after (load .vip)
5458 ;; Save user settings or Viper defaults for vars controled by vip-expert-level
5459 (setq vip-saved-user-settings
5460 (list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help)
5461 (cons 'vip-always vip-always)
5462 (cons 'vip-no-multiple-ESC vip-no-multiple-ESC)
5463 (cons 'vip-ex-style-motion vip-ex-style-motion)
5464 (cons 'vip-ex-style-editing-in-insert
5465 vip-ex-style-editing-in-insert)
5466 (cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi)
5467 (cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert)
5468 (cons 'vip-re-search vip-re-search)))
5471 (vip-set-minibuffer-style)
5472 (vip-set-minibuffer-faces)
5473 (vip-set-search-face)
5474 (if vip-buffer-search-char
5475 (vip-buffer-search-enable))
5476 (vip-update-alphanumeric-class)
5478 ;;; Familiarize Viper with some minor modes that have their own keymaps
5479 (vip-harness-minor-mode "compile")
5480 (vip-harness-minor-mode "outline")
5481 (vip-harness-minor-mode "allout")
5482 (vip-harness-minor-mode "xref")
5483 (vip-harness-minor-mode "lmenu")
5484 (vip-harness-minor-mode "vc")
5485 (vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX
5486 (vip-harness-minor-mode "latex") ; which is in one of these two files
5487 (vip-harness-minor-mode "cyrillic")
5488 (vip-harness-minor-mode "russian")
5489 (vip-harness-minor-mode "view-less")
5492 ;; Intercept maps could go in viper-keym.el
5493 ;; We keep them here in case someone redefines them in ~/.vip
5495 (define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5496 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5498 ;; This is taken care of by vip-insert-global-user-map.
5499 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
5501 (define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC)
5502 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
5504 (define-key vip-vi-intercept-map vip-toggle-key
5505 '(lambda () (interactive)
5506 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
5507 (if (vip-window-display-p) (vip-iconify) (suspend-emacs))
5508 (vip-change-state-to-emacs))))
5510 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
5514 (and (< vip-expert-level 5) (> vip-expert-level 0)))
5517 ;; Let all minor modes take effect after loading
5518 ;; this may not be enough, so we also set default minor-mode-alist.
5519 ;; Without setting the default, new buffers that come up in emacs mode have
5520 ;; minor-mode-map-alist = nil, unless we call vip-change-state-*
5521 (if (eq vip-current-state 'emacs-state)
5523 (vip-change-state-to-emacs)
5524 (setq-default minor-mode-map-alist minor-mode-map-alist)
5528 (run-hooks 'vip-load-hook) ; the last chance to change something
5534 ;;; viper.el ends here