*** empty log message ***
[emacs.git] / lisp / emulation / viper.el
blob34e23efc644f4c5dd5e0789881678dfd90a937a0
1 ;;; viper.el --- A full-featured Vi emulator for GNU Emacs 19 and XEmacs 19,
2 ;; a VI Plan for Emacs Rescue,
3 ;; and a venomous VI PERil.
4 ;; Viper Is also a Package for Emacs Rebels.
5 ;;
6 ;; Keywords: emulations
7 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
9 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
11 (defconst viper-version "2.91 of August 5, 1996"
12 "The current version of Viper")
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 ;; Boston, MA 02111-1307, USA.
31 ;;; Commentary:
33 ;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and
34 ;; improves upon the standard features of Vi and, at the same time, allows
35 ;; full access to all Emacs facilities. Viper supports multiple undo,
36 ;; file name completion, command, file, and search history and it extends
37 ;; Vi in many other ways. Viper is highly customizable through the various
38 ;; hooks, user variables, and keymaps. It is implemented as a collection
39 ;; of minor modes and it is designed to provide full access to all Emacs
40 ;; major and minor modes.
42 ;;; History
44 ;; Viper is a new name for a package formerly known as VIP-19,
45 ;; which was a successor of VIP version 3.5 by Masahiko Sato
46 ;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
47 ;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
48 ;; were also shamelessly plagiarized.
50 ;; Viper maintains some degree of compatibility with these older
51 ;; packages. See the documentation for customization.
53 ;; The main difference between Viper and these older packages are:
55 ;; 1. Viper emulates Vi at several levels, from almost complete conformity
56 ;; to a rather loose Vi-compliance.
58 ;; 2. Viper provides full access to all major and minor modes of Emacs
59 ;; without the need to type extra keys.
60 ;; The older versions of VIP (and other Vi emulators) do not work with
61 ;; some major and minor modes.
63 ;; 3. Viper supports vi-style undo.
65 ;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
67 ;; 5. Viper has a better interface to ex, including command, variable, and
68 ;; file name completion.
70 ;; 6. Viper uses native Emacs history and completion features; it doesn't
71 ;; rely on other packages (such as gmhist.el and completer.el) to provide
72 ;; these features.
74 ;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
75 ;; user to switch from Insert state to Vi state to Replace state, etc.
77 ;; 8. Viper keeps history of recently inserted pieces of text and recently
78 ;; executed Vi-style destructive commands, such as `i', `d', etc.
79 ;; These pieces of text can be inserted in later insertion commands;
80 ;; the previous destructive commands can be re-executed.
82 ;; 9. Viper has Vi-style keyboard macros, which enhances the similar
83 ;; facility in the original Vi.
84 ;; First, one can execute any Emacs command while defining a
85 ;; macro, not just the Vi commands. Second, macros are defined in a
86 ;; WYSYWYG mode, using an interface to Emacs' WYSIWYG style of defining
87 ;; macros. Third, in Viper, one can define macros that are specific to
88 ;; a given buffer, a given major mode, or macros defined for all buffers.
89 ;; The same macro name can have several different definitions:
90 ;; one global, several definitions for various major modes, and
91 ;; definitions for specific buffers.
92 ;; Buffer-specific definitions override mode-specific
93 ;; definitions, which, in turn, override global definitions.
96 ;;; Installation:
97 ;; -------------
99 ;; (require 'viper)
102 ;;; Acknowledgements:
103 ;; -----------------
104 ;; Bug reports and ideas contributed by many users have helped
105 ;; improve Viper and the various versions of VIP.
106 ;; See the on-line manual for a complete list of contributors.
109 ;;; Notes:
111 ;; 1. Major modes.
112 ;; In most cases, Viper handles major modes correctly, i.e., they come up
113 ;; in the right state (either vi-state or emacs-state). For instance, text
114 ;; files come up in vi-state, while, say, Dired appears in emacs-state by
115 ;; default.
116 ;; However, some modes do not appear in the right mode in the beginning,
117 ;; usually because they neglect to follow Emacs conventions (e.g., they don't
118 ;; use kill-all-local-variables when they start). Some major modes
119 ;; may fail to come up in emacs-state if they call hooks, such as
120 ;; text-hook, for no good reason.
122 ;; As an immediate solution, you can hit C-z to bring about the right mode.
123 ;; An interim solution is to add an appropriate hook to the mode like this:
125 ;; (add-hook 'your-favorite-mode 'viper-mode)
126 ;; or
127 ;; (add-hook 'your-favorite-mode 'vip-change-state-to-emacs)
129 ;; whichever applies. The right thing to do, however, is to complain to the
130 ;; author of the respective package. (Sometimes they also neglect to equip
131 ;; their modes with hooks, which is one more reason for complaining.)
133 ;; 2. Keymap handling
134 ;; Because Emacs 19 has an elegant mechanism for turning minor mode keymaps
135 ;; on and off, implementation of Viper has been greatly simplified. Viper
136 ;; has several minor modes.
138 ;; Viper's Vi state consists of seven minor modes:
140 ;; vip-vi-intercept-minor-mode
141 ;; vip-vi-local-user-minor-mode
142 ;; vip-vi-global-user-minor-mode
143 ;; vip-vi-kbd-minor-mode
144 ;; vip-vi-state-modifier-minor-mode
145 ;; vip-vi-diehard-minor-mode
146 ;; vip-vi-basic-minor-mode
148 ;; Bindings done to the keymap of the first mode overshadow those done to
149 ;; the second, which, in turn, overshadows those done to the third, etc.
151 ;; The last vip-vi-basic-minor-mode contains most of the usual Vi bindings
152 ;; in its edit mode. This mode provides access to all Emacs facilities.
153 ;; Novice users, however, may want to set their vip-expert-level to 1
154 ;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This
155 ;; minor mode's bindings make Viper simulate the usual Vi very closely.
156 ;; For instance, C-c will not have its standard Emacs binding
157 ;; and so many of the goodies of Emacs are not available.
159 ;; A skilled user should set vip-expert-level to at least 3. This will
160 ;; enable `C-c' and many Emacs facilities will become available.
161 ;; In this case, vip-vi-diehard-minor-mode is inactive.
163 ;; Viper gurus should have at least
164 ;; (setq vip-expert-level 4)
165 ;; in their ~/.vip files. This will unsuppress all Emacs keys that are not
166 ;; essential for VI-style editing.
167 ;; Pick-and-choose users may want to put
168 ;; (setq vip-expert-level 5)
169 ;; in ~/.vip. Viper will then leave it up to the user to set the variables
170 ;; vip-want-* See vip-set-expert-level for details.
172 ;; The very first minor mode, vip-vi-intercept-minor-mode, is of no
173 ;; concern for the user. It is needed to bind Viper's vital keys, such as
174 ;; ESC and C-z.
176 ;; The second mode, vip-vi-local-user-minor-mode, usually has an
177 ;; empty keymap. However, the user can set bindings in this keymap, which
178 ;; will overshadow the corresponding bindings in the other two minor
179 ;; modes. This is useful, for example, for setting up ZZ in gnus,
180 ;; rmail, mh-e, etc., to send message instead of saving it in a file.
181 ;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
182 ;; that would visit the next or the previous file in the Dired buffer.
183 ;; Setting local keys is tricky, so don't do it directly. Instead, use
184 ;; vip-add-local-keys function (see its doc).
186 ;; The third minor mode, vip-vi-global-user-minor-mode, is also intended
187 ;; for the users but, unlike vip-vi-local-user-minor-mode, its key
188 ;; bindings are seen in all Viper buffers. This mode keys can be done
189 ;; with define-key command.
191 ;; The fourth minor mode, vip-vi-kbd-minor-mode, is used by keyboard
192 ;; macros. Users are NOT supposed to modify this keymap directly.
194 ;; The fifth mode, vip-vi-state-modifier-minor-mode, can be used to set
195 ;; key bindings that are visible in some major modes but not in others.
197 ;; Users are allowed to modify keymaps that belong to
198 ;; vip-vi-local-user-minor-mode, vip-vi-global-user-minor-mode,
199 ;; and vip-vi-state-modifier-minor-mode only.
201 ;; Viper's Insert state also has seven minor modes:
203 ;; vip-insert-intercept-minor-mode
204 ;; vip-insert-local-user-minor-mode
205 ;; vip-insert-global-user-minor-mode
206 ;; vip-insert-kbd-minor-mode
207 ;; vip-insert-state-modifier-minor-mode
208 ;; vip-insert-diehard-minor-mode
209 ;; vip-insert-basic-minor-mode
211 ;; As with VI's editing modes, the first mode, vip-insert-intercept-minor-mode
212 ;; is used to bind vital keys that are not to be changed by the user.
214 ;; The next mode, vip-insert-local-user-minor-mode, is used to customize
215 ;; bindings in the insert state of Viper. The third mode,
216 ;; vip-insert-global-user-minor-mode is like
217 ;; vip-insert-local-user-minor-mode, except that its bindings are seen in
218 ;; all Viper buffers. As with vip-vi-local-user-minor-mode, its bindings
219 ;; should be done via the function vip-add-local-keys. Bindings for
220 ;; vip-insert-global-user-minor-mode can be set with the define-key command.
222 ;; The next minor mode, vip-insert-kbd-minor-mode,
223 ;; is used for keyboard VI-style macros defined with :map!.
225 ;; The fifth minor mode, vip-insert-state-modifier-minor-mode, is like
226 ;; vip-vi-state-modifier-minor-mode, except that it is used in the Insert
227 ;; state; it can be used to modify keys in a mode-specific fashion.
229 ;; The minor mode vip-insert-diehard-minor-mode is in effect when
230 ;; the user wants a high degree of Vi compatibility (a bad idea, really!).
231 ;; The last minor mode, vip-insert-basic-minor-mode, is always in effect
232 ;; when Viper is in insert state. It binds a small number of keys needed for
233 ;; Viper's operation.
235 ;; Finally, Viper provides minor modes for overriding bindings set by Emacs
236 ;; modes when Viper is in Emacs state:
238 ;; vip-emacs-local-user-minor-mode
239 ;; vip-emacs-global-user-minor-mode
240 ;; vip-emacs-kbd-minor-mode
241 ;; vip-emacs-state-modifier-minor-mode
243 ;; These minor modes are in effect when Viper is in Emacs state. The keymap
244 ;; associated with vip-emacs-global-user-minor-mode,
245 ;; vip-emacs-global-user-map, overrides the global and local keymaps as
246 ;; well as the minor mode keymaps set by other modes. The keymap of
247 ;; vip-emacs-local-user-minor-mode, vip-emacs-local-user-map, overrides
248 ;; everything, but it is used on a per buffer basis.
249 ;; The keymap associated with vip-emacs-state-modifier-minor-mode
250 ;; overrides keys on a per-major-mode basis. The mode
251 ;; vip-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
252 ;; state.
254 ;; 3. There is also one minor mode that is used when Viper is in its
255 ;; replace-state (used for commands like cw, C, etc.). This mode is
256 ;; called
258 ;; vip-replace-minor-mode
260 ;; and its keymap is vip-replace-map. Replace minor mode is always
261 ;; used in conjunction with the minor modes for insert-state, and its
262 ;; keymap overshadows the keymaps for insert minor modes.
264 ;; 4. Defining buffer-local bindings in Vi and Insert modes.
265 ;; As mentioned before, sometimes, it is convenient to have
266 ;; buffer-specific of mode-specific key bindings in Vi and insert modes.
267 ;; Viper provides a special function, vip-add-local-keys, to do precisely
268 ;; this. For instance, is you need to add couple of mode-specific bindings
269 ;; to Insert mode, you can put
271 ;; (vip-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
273 ;; somewhere in a hook of this major mode. If you put something like this
274 ;; in your own elisp function, this will define bindings specific to the
275 ;; buffer that was current at the time of the call to vip-add-local-keys.
276 ;; The only thing to make sure here is that the major mode of this buffer
277 ;; is written according to Emacs conventions, which includes a call to
278 ;; (kill-all-local-variables). See vip-add-local-keys for more details.
281 ;; TO DO (volunteers?):
283 ;; 1. Some of the code that is inherited from VIP-3.5 is rather
284 ;; convoluted. Instead of vip-command-argument, keymaps should bind the
285 ;; actual commands. E.g., "dw" should be bound to a generic command
286 ;; vip-delete that will delete things based on the value of
287 ;; last-command-char. This would greatly simplify the logic and the code.
289 ;; 2. Somebody should venture to write a customization package a la
290 ;; options.el that would allow the user to change values of variables
291 ;; that meet certain specs (e.g., match a regexp) and whose doc string
292 ;; starts with a '*'. Then, the user should be offered to save
293 ;; variables that were changed. This will make user's customization job
294 ;; much easier.
297 ;; Code
299 (require 'advice)
300 (require 'cl)
301 (require 'ring)
303 (require 'viper-util)
305 ;; Compiler pacifier
306 (defvar vip-minibuffer-current-face)
307 (defvar vip-minibuffer-insert-face)
308 (defvar vip-minibuffer-vi-face)
309 (defvar vip-minibuffer-emacs-face)
310 (defvar iso-accents-mode)
311 (defvar zmacs-region-stays)
312 ;; end pacifier
315 ;;; Variables
317 ;; Is t until viper-mode executes for the very first time.
318 ;; Prevents recursive descend into startup messages.
319 (defvar vip-first-time t)
321 (defvar vip-expert-level 0
322 "User's expert level.
323 The minor mode vip-vi-diehard-minor-mode is in effect when
324 vip-expert-level is 1 or 2 or when vip-want-emacs-keys-in-vi is t.
325 The minor mode vip-insert-diehard-minor-mode is in effect when
326 vip-expert-level is 1 or 2 or if vip-want-emacs-keys-in-insert is t.
327 Use `M-x vip-set-expert-level' to change this.")
329 ;; Max expert level supported by Viper. This is NOT a user option.
330 ;; It is here to make it hard for the user from resetting it.
331 (defconst vip-max-expert-level 5)
333 ;; Contains user settings for vars affected by vip-set-expert-level function.
334 ;; Not a user option.
335 (defvar vip-saved-user-settings nil)
338 ;;; Viper minor modes
340 ;; This is not local in Emacs, so we make it local.
341 ;; This must be local because although the stack of minor modes can be the same
342 ;; for all buffers, the associated *keymaps* can be different. In Viper,
343 ;; vip-vi-local-user-map, vip-insert-local-user-map, and others can have
344 ;; different keymaps for different buffers.
345 ;; Also, the keymaps associated with vip-vi/insert-state-modifier-minor-mode
346 ;; can be different.
347 (make-variable-buffer-local 'minor-mode-map-alist)
349 ;; Mode for vital things like \e, C-z.
350 (vip-deflocalvar vip-vi-intercept-minor-mode nil)
352 (vip-deflocalvar vip-vi-basic-minor-mode nil
353 "Viper's minor mode for Vi bindings.")
355 (vip-deflocalvar vip-vi-local-user-minor-mode nil
356 "Auxiliary minor mode for user-defined local bindings in Vi state.")
358 (vip-deflocalvar vip-vi-global-user-minor-mode nil
359 "Auxiliary minor mode for user-defined global bindings in Vi state.")
361 (vip-deflocalvar vip-vi-state-modifier-minor-mode nil
362 "Minor mode used to make major-mode-specific modification to Vi state.")
364 (vip-deflocalvar vip-vi-diehard-minor-mode nil
365 "This minor mode is in effect when the user wants Viper to be Vi.")
367 (vip-deflocalvar vip-vi-kbd-minor-mode nil
368 "Minor mode for Ex command macros in Vi state.
369 The corresponding keymap stores key bindings of Vi macros defined with
370 the Ex command :map.")
372 ;; Mode for vital things like \e, C-z.
373 (vip-deflocalvar vip-insert-intercept-minor-mode nil)
375 (vip-deflocalvar vip-insert-basic-minor-mode nil
376 "Viper's minor mode for bindings in Insert mode.")
378 (vip-deflocalvar vip-insert-local-user-minor-mode nil
379 "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
380 This is a way to overshadow normal Insert mode bindings locally to certain
381 designated buffers.")
383 (vip-deflocalvar vip-insert-global-user-minor-mode nil
384 "Auxiliary minor mode for global user-defined bindings in Insert state.")
386 (vip-deflocalvar vip-insert-state-modifier-minor-mode nil
387 "Minor mode used to make major-mode-specific modification to Insert state.")
389 (vip-deflocalvar vip-insert-diehard-minor-mode nil
390 "Minor mode that simulates Vi very closely.
391 Not recommened, except for the novice user.")
393 (vip-deflocalvar vip-insert-kbd-minor-mode nil
394 "Minor mode for Ex command macros Insert state.
395 The corresponding keymap stores key bindings of Vi macros defined with
396 the Ex command :map!.")
398 (vip-deflocalvar vip-replace-minor-mode nil
399 "Minor mode in effect in replace state (cw, C, and the like commands).")
401 ;; Mode for vital things like \C-z and \C-x)
402 ;; This is t, by default. So, any new buffer will have C-z defined as
403 ;; switch to Vi, unless we switched states in this buffer
404 (vip-deflocalvar vip-emacs-intercept-minor-mode t)
406 (vip-deflocalvar vip-emacs-local-user-minor-mode t
407 "Minor mode for local user bindings effective in Emacs state.
408 Users can use it to override Emacs bindings when Viper is in its Emacs
409 state.")
411 (vip-deflocalvar vip-emacs-global-user-minor-mode t
412 "Minor mode for global user bindings in effect in Emacs state.
413 Users can use it to override Emacs bindings when Viper is in its Emacs
414 state.")
416 (vip-deflocalvar vip-emacs-kbd-minor-mode t
417 "Minor mode for Vi style macros in Emacs state.
418 The corresponding keymap stores key bindings of Vi macros defined with
419 `vip-record-kbd-macro' command. There is no Ex-level command to do this
420 interactively.")
422 (vip-deflocalvar vip-emacs-state-modifier-minor-mode t
423 "Minor mode used to make major-mode-specific modification to Emacs state.
424 For instance, a Vi purist may want to bind `dd' in Dired mode to a function
425 that deletes a file.")
429 ;;; ISO characters
431 (vip-deflocalvar vip-automatic-iso-accents nil
432 "*If non-nil, ISO accents will be turned on in insert/replace emacs states and turned off in vi-state.
433 For some users, this behavior may be too primitive. In this case, use
434 insert/emacs/vi state hooks.")
437 ;;; Emacs keys in other states.
439 (defvar vip-want-emacs-keys-in-insert t
440 "*Set to nil if you want complete Vi compatibility in insert mode.
441 Complete compatibility with Vi is not recommended for power use of Viper.")
443 (defvar vip-want-emacs-keys-in-vi t
444 "*Set to nil if you want complete Vi compatibility in Vi mode.
445 Full Vi compatibility is not recommended for power use of Viper.")
449 ;; VI-style Undo
451 ;; Used to 'undo' complex commands, such as replace and insert commands.
452 (vip-deflocalvar vip-undo-needs-adjustment nil)
453 (put 'vip-undo-needs-adjustment 'permanent-local t)
455 ;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
456 ;; complex command that must be undone atomically. If inserted, it is
457 ;; erased by vip-change-state-to-vi and vip-repeat.
458 (defconst vip-buffer-undo-list-mark 'viper)
460 (defvar vip-keep-point-on-undo nil
461 "*Non-nil means not to move point while undoing commands.
462 This style is different from Emacs and Vi. Try it to see if
463 it better fits your working style.")
465 ;; Replace mode and changing text
467 ;; Viper's own after/before change functions, which get vip-add-hook'ed to
468 ;; Emacs's
469 (vip-deflocalvar vip-after-change-functions nil "")
470 (vip-deflocalvar vip-before-change-functions nil "")
471 (vip-deflocalvar vip-post-command-hooks nil "")
472 (vip-deflocalvar vip-pre-command-hooks nil "")
474 ;; Can be used to pass global states around for short period of time
475 (vip-deflocalvar vip-intermediate-command nil "")
477 ;; Indicates that the current destructive command has started in replace mode.
478 (vip-deflocalvar vip-began-as-replace nil "")
480 (defvar vip-replace-overlay-cursor-color "Red"
481 "*Cursor color to use in Replace state")
484 (vip-deflocalvar vip-replace-overlay nil "")
485 (put 'vip-replace-overlay 'permanent-local t)
487 (defvar vip-replace-overlay-pixmap "gray3"
488 "Pixmap to use for search face on non-color displays.")
489 (defvar vip-search-face-pixmap "gray3"
490 "Pixmap to use for search face on non-color displays.")
493 (defun vip-set-replace-overlay-face ()
494 (if (vip-has-face-support-p)
495 (defvar vip-replace-overlay-face
496 (progn
497 (make-face 'vip-replace-overlay-face)
498 (vip-hide-face 'vip-replace-overlay-face)
499 (or (face-differs-from-default-p 'vip-replace-overlay-face)
500 (progn
501 (if (vip-can-use-colors "darkseagreen2" "Black")
502 (progn
503 (set-face-background
504 'vip-replace-overlay-face "darkseagreen2")
505 (set-face-foreground 'vip-replace-overlay-face "Black")))
506 (set-face-underline-p 'vip-replace-overlay-face t)
507 (vip-set-face-pixmap
508 'vip-replace-overlay-face vip-replace-overlay-pixmap)))
509 'vip-replace-overlay-face)
510 "*Face for highlighting replace regions on a window display.")
513 (defvar vip-replace-region-end-delimiter "$"
514 "A string marking the end of replacement regions.
515 It is used only with TTYs or if `vip-use-replace-region-delimiters'
516 is non-nil.")
517 (defvar vip-replace-region-start-delimiter ""
518 "A string marking the beginning of replacement regions.
519 It is used only with TTYs or if `vip-use-replace-region-delimiters'
520 is non-nil.")
521 (defvar vip-use-replace-region-delimiters (not (vip-has-face-support-p))
522 "*If non-nil, Viper will always use `vip-replace-region-end-delimiter' and
523 `vip-replace-region-start-delimiter' to delimit replacement regions, even on
524 color displays. By default, the delimiters are used only on TTYs.")
526 ;; XEmacs requires glyphs
527 (if vip-xemacs-p
528 (progn
529 (or (glyphp vip-replace-region-end-delimiter)
530 (setq vip-replace-region-end-delimiter
531 (make-glyph vip-replace-region-end-delimiter)))
532 (or (glyphp vip-replace-region-start-delimiter)
533 (setq vip-replace-region-start-delimiter
534 (make-glyph vip-replace-region-start-delimiter)))
538 ;; These are local marker that must be initialized to nil and moved with
539 ;; `vip-move-marker-locally'
541 ;; Remember the last position inside the replace region.
542 (vip-deflocalvar vip-last-posn-in-replace-region nil)
543 ;; Remember the last position while inserting
544 (vip-deflocalvar vip-last-posn-while-in-insert-state nil)
545 (put 'vip-last-posn-in-replace-region 'permanent-local t)
546 (put 'vip-last-posn-while-in-insert-state 'permanent-local t)
548 (vip-deflocalvar vip-sitting-in-replace nil "")
549 (put 'vip-sitting-in-replace 'permanent-local t)
551 ;; Remember the number of characters that have to be deleted in replace
552 ;; mode to compensate for the inserted characters.
553 (vip-deflocalvar vip-replace-chars-to-delete 0 "")
554 (vip-deflocalvar vip-replace-chars-deleted 0 "")
556 ;; Insertion ring and command ring
557 (defvar vip-insertion-ring-size 14
558 "The size of the insertion ring.")
559 ;; The insertion ring.
560 (defvar vip-insertion-ring nil)
561 ;; This is temp insertion ring. Used to do rotation for display purposes.
562 ;; When rotation just started, it is initialized to vip-insertion-ring.
563 (defvar vip-temp-insertion-ring nil)
564 (defvar vip-last-inserted-string-from-insertion-ring "")
566 (defvar vip-command-ring-size 14
567 "The size of the command ring.")
568 ;; The command ring.
569 (defvar vip-command-ring nil)
570 ;; This is temp command ring. Used to do rotation for display purposes.
571 ;; When rotation just started, it is initialized to vip-command-ring.
572 (defvar vip-temp-command-ring nil)
574 ;; Modes and related variables
576 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
577 (vip-deflocalvar vip-current-state 'emacs-state)
579 (defvar vip-no-multiple-ESC t
580 "*If true, multiple ESC in Vi mode will cause bell to ring.
581 This is set to t on a windowing terminal and to 'twice on a dumb
582 terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
583 enables cursor keys and is generally more convenient, as terminals usually
584 don't have a convenient Meta key.
585 Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
586 as is allowed by the major mode in effect.")
589 (defvar vip-want-ctl-h-help nil
590 "*If t then C-h is bound to help-command in insert mode, if nil then it is
591 bound to delete-backward-char.")
593 ;; Autoindent in insert
595 ;; Variable that keeps track of whether C-t has been pressed.
596 (vip-deflocalvar vip-cted nil "")
598 ;; Preserve the indent value, used by C-d in insert mode.
599 (vip-deflocalvar vip-current-indent 0)
601 ;; Whether to preserve the indent, used by C-d in insert mode.
602 (vip-deflocalvar vip-preserve-indent nil)
604 (vip-deflocalvar vip-auto-indent nil
605 "*Autoindent if t.")
606 (vip-deflocalvar vip-electric-mode t
607 "*If t, enable electric behavior.
608 Currently only enables auto-indentation `according to mode'.")
610 (defconst vip-shift-width 8
611 "*The shiftwidth variable.")
613 ;; Variables for repeating destructive commands
615 (defconst vip-keep-point-on-repeat t
616 "*If t, don't move point when repeating previous command.
617 This is useful for doing repeated changes with the '.' key.
618 The user can change this to nil, if she likes when the cursor moves
619 to a new place after repeating previous Vi command.")
621 ;; Remember insert point as a marker. This is a local marker that must be
622 ;; initialized to nil and moved with `vip-move-marker-locally'.
623 (vip-deflocalvar vip-insert-point nil)
624 (put 'vip-insert-point 'permanent-local t)
626 ;; This remembers the point before dabbrev-expand was called.
627 ;; If vip-insert-point turns out to be bigger than that, it is reset
628 ;; back to vip-pre-command-point.
629 ;; The reason this is needed is because dabbrev-expand (and possibly
630 ;; others) may jump to before the insertion point, delete something and
631 ;; then reinsert a bigger piece. For instance: bla^blo
632 ;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point,
633 ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
634 ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
635 ;; will insert the expansion, and we get: blablo^
636 ;; Whatever we insert next goes before the ^, i.e., before the
637 ;; vip-insert-point marker. So, Viper will think that nothing was
638 ;; inserted. Remembering the orig position of the marker circumvents the
639 ;; problem.
640 ;; We don't know of any command, except dabbrev-expand, that has the same
641 ;; problem. However, the same trick can be used if such a command is
642 ;; discovered later.
644 (vip-deflocalvar vip-pre-command-point nil)
645 (put 'vip-pre-command-point 'permanent-local t) ; this is probably an overkill
647 ;; This is used for saving inserted text.
648 (defvar vip-last-insertion nil)
650 ;; Remembers the last replaced region.
651 (defvar vip-last-replace-region "")
653 ;; Remember com point as a marker.
654 ;; This is a local marker. Should be moved with `vip-move-marker-locally'
655 (vip-deflocalvar vip-com-point nil)
657 ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
658 ;; It is used to re-execute last destructive command.
659 ;; M-COM is a Lisp symbol representing the function to be executed.
660 ;; VAL is the prefix argument that was used with that command.
661 ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
662 ;; additional information on how the function in M-COM is to be handled.
663 ;; REG is the register used by command
664 ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
665 ;; commands).
666 ;; COMMAND-KEYS are the keys that were typed to invoke the command.
667 (defvar vip-d-com nil)
669 ;; The character remembered by the Vi `r' command.
670 (defvar vip-d-char nil)
672 ;; Name of register to store deleted or yanked strings
673 (defvar vip-use-register nil)
677 ;; Variables for Moves and Searches
679 ;; For use by `;' command.
680 (defvar vip-f-char nil)
682 ;; For use by `.' command.
683 (defvar vip-F-char nil)
685 ;; For use by `;' command.
686 (defvar vip-f-forward nil)
688 ;; For use by `;' command.
689 (defvar vip-f-offset nil)
691 ;; Last search string
692 (defvar vip-s-string "")
694 (defvar vip-quote-string "> "
695 "String inserted at the beginning of quoted region.")
697 ;; If t, search is forward.
698 (defvar vip-s-forward nil)
700 (defconst vip-case-fold-search nil
701 "*If t, search ignores cases.")
703 (defconst vip-re-search t
704 "*If t, search is reg-exp search, otherwise vanilla search.")
706 (defconst vip-re-query-replace t
707 "*If t then do regexp replace, if nil then do string replace.")
709 (defconst vip-re-replace t
710 "*If t, do regexp replace. nil means do string replace.")
712 (vip-deflocalvar vip-ex-style-motion t
713 "*Ex-style: the commands l,h do not cross lines, etc.")
715 (vip-deflocalvar vip-ex-style-editing-in-insert t
716 "*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc.
717 Note: this doesn't preclude ^H and ^? from deleting characters by moving
718 past the insertion point. This is a feature, not a bug. ")
720 (vip-deflocalvar vip-delete-backwards-in-replace nil
721 "*If t, DEL key will delete characters while moving the cursor backwards.
722 If nil, the cursor will move backwards without deleting anything.")
724 (defconst vip-buffer-search-char nil
725 "*Key bound for buffer-searching.")
727 (defconst vip-search-wrap-around-t t
728 "*If t, search wraps around.")
730 (vip-deflocalvar vip-related-files-and-buffers-ring nil
731 "*Ring of file and buffer names that are considered to be related to the
732 current buffer.
733 These buffers can be cycled through via :R and :P commands.")
734 (put 'vip-related-files-and-buffers-ring 'permanent-local t)
736 ;; Used to find out if we are done with searching the current buffer.
737 (vip-deflocalvar vip-local-search-start-marker nil)
738 ;; As above, but global
739 (defvar vip-search-start-marker (make-marker))
741 ;; the search overlay
742 (vip-deflocalvar vip-search-overlay nil)
745 (defvar vip-heading-start
746 (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
747 "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
748 "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
749 "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
750 "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
751 "^.+:-") ; prolog
752 "*Regexps for Headings. Used by \[\[ and \]\].")
754 (defvar vip-heading-end
755 (concat "^}\\|" ; C/C++
756 "^\\\\end{\\|" ; latex
757 "^@end \\|" ; texinfo
758 ")\n\n[ \t\n]*\\|" ; lisp
759 "\\.\\s-*$") ; prolog
760 "*Regexps to end Headings/Sections. Used by \[\].")
763 ;; These two vars control the interaction of jumps performed by ' and `.
764 ;; In this new version, '' doesn't erase the marks set by ``, so one can
765 ;; use both kinds of jumps interchangeably and without loosing positions
766 ;; inside the lines.
768 ;; Remembers position of the last jump done using ``'.
769 (vip-deflocalvar vip-last-jump nil)
770 ;; Remembers position of the last jump done using `''.
771 (vip-deflocalvar vip-last-jump-ignore 0)
773 ;; Some common error messages
775 (defconst vip-SpuriousText "Spurious text after command" "")
776 (defconst vip-BadExCommand "Not an editor command" "")
777 (defconst vip-InvalidCommandArgument "Invalid command argument" "")
778 (defconst vip-NoPrevSearch "No previous search string" "")
779 (defconst vip-EmptyRegister "`%c': Nothing in this register" "")
780 (defconst vip-InvalidRegister "`%c': Invalid register" "")
781 (defconst vip-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
782 (defconst vip-InvalidTextmarker "`%c': Invalid text marker" "")
783 (defconst vip-InvalidViCommand "Invalid command" "")
784 (defconst vip-BadAddress "Ill-formed address" "")
785 (defconst vip-FirstAddrExceedsSecond "First address exceeds second" "")
786 (defconst vip-NoFileSpecified "No file specified" "")
789 ;; History variables
791 ;; History of search strings.
792 (defvar vip-search-history (list ""))
793 ;; History of query-replace strings used as a source.
794 (defvar vip-replace1-history nil)
795 ;; History of query-replace strings used as replacement.
796 (defvar vip-replace2-history nil)
797 ;; History of region quoting strings.
798 (defvar vip-quote-region-history (list vip-quote-string))
799 ;; History of Ex-style commands.
800 (defvar vip-ex-history nil)
801 ;; History of shell commands.
802 (defvar vip-shell-history nil)
805 ;; Last shell command. There are two of these, one for Ex (in viper-ex)
806 ;; and one for Vi.
808 ;; Last shell command executed with ! command.
809 (defvar vip-last-shell-com nil)
813 ;;; Miscellaneous
815 ;; don't bark when mark is inactive
816 (setq mark-even-if-inactive t)
818 (defvar vip-inhibit-startup-message nil
819 "Whether Viper startup message should be inhibited.")
821 (defvar vip-always t
822 "t means, arrange that vi-state will be a default.")
824 (defvar vip-custom-file-name (cond (vip-vms-os-p "sys$login:.vip")
825 ((memq system-type '(emx ms-dos))
826 "/_vip")
827 ((memq system-type '(windows-nt windows-95))
828 "~/_vip")
829 (t ; Unix
830 "~/.vip"))
831 "Viper customisation file.
832 This variable must be set _before_ loading Viper.")
835 (defvar vip-spell-function 'ispell-region
836 "Spell function used by #s<move> command to spell.")
838 (defvar vip-tags-file-name "TAGS"
839 "The tags file used by Viper.")
841 ;; Minibuffer
843 (defvar vip-vi-style-in-minibuffer t
844 "If t, use vi-style editing in minibuffer.
845 Should be set in `~/.vip' file.")
847 ;; overlay used in the minibuffer to indicate which state it is in
848 (vip-deflocalvar vip-minibuffer-overlay nil)
850 ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
851 ;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
852 ;; *after* exiting the minibuffer
853 (defvar vip-minibuffer-exit-hook nil)
855 (vip-deflocalvar vip-vi-minibuffer-minor-mode nil
856 "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
857 (vip-deflocalvar vip-insert-minibuffer-minor-mode nil
858 "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
860 ;; setup emacs-supported vi-style feel
861 (setq next-line-add-newlines nil
862 require-final-newline t)
864 (make-variable-buffer-local 'require-final-newline)
867 ;; Mode line
868 (defconst vip-vi-state-id "<V> "
869 "Mode line tag identifying the Vi mode of Viper.")
870 (defconst vip-emacs-state-id "<E> "
871 "Mode line tag identifying the Emacs mode of Viper.")
872 (defconst vip-insert-state-id "<I> "
873 "Mode line tag identifying the Insert mode of Viper.")
874 (defconst vip-replace-state-id "<R> "
875 "Mode line tag identifying the Replace mode of Viper.")
877 ;; Viper changes the default mode-line-buffer-identification
878 (setq-default mode-line-buffer-identification '(" %b"))
880 ;; Variable displaying the current Viper state in the mode line.
881 (vip-deflocalvar vip-mode-string vip-emacs-state-id)
882 (or (memq 'vip-mode-string global-mode-string)
883 (setq global-mode-string
884 (append '("" vip-mode-string) (cdr global-mode-string))))
887 (defvar vip-vi-state-hook nil
888 "*Hooks run just before the switch to Vi mode is completed.")
889 (defvar vip-insert-state-hook nil
890 "*Hooks run just before the switch to Insert mode is completed.")
891 (defvar vip-replace-state-hook nil
892 "*Hooks run just before the switch to Replace mode is completed.")
893 (defvar vip-emacs-state-hook nil
894 "*Hooks run just before the switch to Emacs mode is completed.")
896 (defvar vip-load-hook nil
897 "Hooks run just after loading Viper.")
900 ;; Generic predicates
902 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
904 ;; generate test functions
905 ;; given symbol foo, foo-p is the test function, foos is the set of
906 ;; Viper command keys
907 ;; (macroexpand '(vip-test-com-defun foo))
908 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
910 (defmacro vip-test-com-defun (name)
911 (let* ((snm (symbol-name name))
912 (nm-p (intern (concat snm "-p")))
913 (nms (intern (concat snm "s"))))
914 (` (defun (, nm-p) (com)
915 (consp (memq (if (< com 0) (- com) com) (, nms)))))))
917 ;; Variables for defining VI commands
919 ;; Modifying commands that can be prefixes to movement commands
920 (defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
921 (vip-test-com-defun vip-prefix-command)
923 ;; Commands that are pairs eg. dd. r and R here are a hack
924 (defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
925 (vip-test-com-defun vip-charpair-command)
927 (defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
928 ?H ?M ?n ?t ?T ?w ?W ?$ ?%
929 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
930 ?; ?, ?0 ?? ?/
932 "Movement commands")
933 (vip-test-com-defun vip-movement-command)
935 ;; Commands that can be repeated by . (dotted)
936 (defconst vip-dotable-commands '(?c ?d ?C ?D ?> ?<))
937 (vip-test-com-defun vip-dotable-command)
939 ;; Commands that can follow a #
940 (defconst vip-hash-cmds '(?c ?C ?g ?q ?S))
941 (vip-test-com-defun vip-hash-cmd)
943 ;; Commands that may have registers as prefix
944 (defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
945 (vip-test-com-defun vip-regsuffix-command)
947 (defconst vip-vi-commands (append vip-movement-commands
948 vip-dotable-commands
949 vip-charpair-commands
950 vip-hash-cmds
951 vip-prefix-commands
952 vip-regsuffix-commands)
953 "The list of all commands in Vi-state.")
954 (vip-test-com-defun vip-vi-command)
957 ;;; Arrange the keymaps
958 (require 'viper-keym)
961 ;;; CODE
963 ;; sentinels
965 ;; Runs vip-after-change-functions inside after-change-functions
966 (defun vip-after-change-sentinel (beg end len)
967 (let ((list vip-after-change-functions))
968 (while list
969 (funcall (car list) beg end len)
970 (setq list (cdr list)))))
972 ;; Runs vip-before-change-functions inside before-change-functions
973 (defun vip-before-change-sentinel (beg end)
974 (let ((list vip-before-change-functions))
975 (while list
976 (funcall (car list) beg end)
977 (setq list (cdr list)))))
979 (defsubst vip-post-command-sentinel ()
980 (run-hooks 'vip-post-command-hooks))
982 (defsubst vip-pre-command-sentinel ()
983 (run-hooks 'vip-pre-command-hooks))
985 ;; Needed so that Viper will be able to figure the last inserted
986 ;; chunk of text with reasonable accuracy.
987 (defsubst vip-insert-state-post-command-sentinel ()
988 (if (and (memq vip-current-state '(insert-state replace-state))
989 vip-insert-point
990 (>= (point) vip-insert-point))
991 (setq vip-last-posn-while-in-insert-state (point-marker)))
992 (if (and (eq this-command 'dabbrev-expand)
993 (integerp vip-pre-command-point)
994 (> vip-insert-point vip-pre-command-point))
995 (move-marker vip-insert-point vip-pre-command-point))
998 (defsubst vip-insert-state-pre-command-sentinel ()
999 (if (and (eq this-command 'dabbrev-expand)
1000 (markerp vip-insert-point)
1001 (marker-position vip-insert-point))
1002 (setq vip-pre-command-point (marker-position vip-insert-point))))
1004 (defsubst vip-R-state-post-command-sentinel ()
1005 ;; Restoring cursor color is needed despite
1006 ;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
1007 ;; another frame, the pre-command hook won't change cursor color to default
1008 ;; in that other frame. So, if the second frame cursor was red and we set
1009 ;; the point outside the replacement region, then the cursor color will
1010 ;; remain red. Restoring the default, below, prevents this.
1011 (if (and (<= (vip-replace-start) (point))
1012 (<= (point) (vip-replace-end)))
1013 (vip-change-cursor-color vip-replace-overlay-cursor-color)
1014 (vip-restore-cursor-color)
1017 ;; to speed up, don't change cursor color before self-insert
1018 ;; and common move commands
1019 (defsubst vip-replace-state-pre-command-sentinel ()
1020 (or (memq this-command '(self-insert-command))
1021 (memq (vip-event-key last-command-event)
1022 '(up down left right (meta f) (meta b)
1023 (control n) (control p) (control f) (control b)))
1024 (vip-restore-cursor-color)))
1026 (defun vip-replace-state-post-command-sentinel ()
1027 ;; Restoring cursor color is needed despite
1028 ;; vip-replace-state-pre-command-sentinel: When one jumps to another buffer
1029 ;; in another frame, the pre-command hook won't change cursor color to
1030 ;; default in that other frame. So, if the second frame cursor was red and
1031 ;; we set the point outside the replacement region, then the cursor color
1032 ;; will remain red. Restoring the default, below, fixes this problem.
1034 ;; We optimize for self-insert-command's here, since they either don't change
1035 ;; cursor color or, if they terminate replace mode, the color will be changed
1036 ;; in vip-finish-change
1037 (or (memq this-command '(self-insert-command))
1038 (vip-restore-cursor-color))
1039 (cond
1040 ((eq vip-current-state 'replace-state)
1041 ;; delete characters to compensate for inserted chars.
1042 (let ((replace-boundary (vip-replace-end)))
1043 (save-excursion
1044 (goto-char vip-last-posn-in-replace-region)
1045 (delete-char vip-replace-chars-to-delete)
1046 (setq vip-replace-chars-to-delete 0
1047 vip-replace-chars-deleted 0)
1048 ;; terminate replace mode if reached replace limit
1049 (if (= vip-last-posn-in-replace-region
1050 (vip-replace-end))
1051 (vip-finish-change vip-last-posn-in-replace-region)))
1053 (if (and (<= (vip-replace-start) (point))
1054 (<= (point) replace-boundary))
1055 (progn
1056 ;; the state may have changed in vip-finish-change above
1057 (if (eq vip-current-state 'replace-state)
1058 (vip-change-cursor-color vip-replace-overlay-cursor-color))
1059 (setq vip-last-posn-in-replace-region (point-marker))))
1062 (t ;; terminate replace mode if changed Viper states.
1063 (vip-finish-change vip-last-posn-in-replace-region))))
1066 ;; changing mode
1068 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
1069 (defun vip-change-state (new-state)
1070 ;; Keep vip-post/pre-command-hooks fresh.
1071 ;; We remove then add vip-post/pre-command-sentinel since it is very
1072 ;; desirable that vip-pre-command-sentinel is the last hook and
1073 ;; vip-post-command-sentinel is the first hook.
1074 (remove-hook 'post-command-hook 'vip-post-command-sentinel)
1075 (add-hook 'post-command-hook 'vip-post-command-sentinel)
1076 (remove-hook 'pre-command-hook 'vip-pre-command-sentinel)
1077 (add-hook 'pre-command-hook 'vip-pre-command-sentinel t)
1078 ;; These hooks will be added back if switching to insert/replace mode
1079 (vip-remove-hook 'vip-post-command-hooks
1080 'vip-insert-state-post-command-sentinel)
1081 (vip-remove-hook 'vip-pre-command-hooks
1082 'vip-insert-state-pre-command-sentinel)
1083 (cond ((eq new-state 'vi-state)
1084 (cond ((member vip-current-state '(insert-state replace-state))
1086 ;; move vip-last-posn-while-in-insert-state
1087 ;; This is a normal hook that is executed in insert/replace
1088 ;; states after each command. In Vi/Emacs state, it does
1089 ;; nothing. We need to execute it here to make sure that
1090 ;; the last posn was recorded when we hit ESC.
1091 ;; It may be left unrecorded if the last thing done in
1092 ;; insert/repl state was dabbrev-expansion or abbrev
1093 ;; expansion caused by hitting ESC
1094 (vip-insert-state-post-command-sentinel)
1096 (condition-case conds
1097 (progn
1098 (vip-save-last-insertion
1099 vip-insert-point
1100 vip-last-posn-while-in-insert-state)
1101 (if vip-began-as-replace
1102 (setq vip-began-as-replace nil)
1103 ;; repeat insert commands if numerical arg > 1
1104 (save-excursion
1105 (vip-repeat-insert-command))))
1106 (error
1107 (vip-message-conditions conds)))
1109 (if (> (length vip-last-insertion) 0)
1110 (vip-push-onto-ring vip-last-insertion
1111 'vip-insertion-ring))
1113 (if vip-ex-style-editing-in-insert
1114 (or (bolp) (backward-char 1))))
1117 ;; insert or replace
1118 ((memq new-state '(insert-state replace-state))
1119 (if (memq vip-current-state '(emacs-state vi-state))
1120 (vip-move-marker-locally 'vip-insert-point (point)))
1121 (vip-move-marker-locally 'vip-last-posn-while-in-insert-state (point))
1122 (vip-add-hook 'vip-post-command-hooks
1123 'vip-insert-state-post-command-sentinel t)
1124 (vip-add-hook 'vip-pre-command-hooks
1125 'vip-insert-state-pre-command-sentinel t))
1126 ) ; outermost cond
1128 ;; Nothing needs to be done to switch to emacs mode! Just set some
1129 ;; variables, which is already done in vip-change-state-to-emacs!
1131 (setq vip-current-state new-state)
1132 (vip-normalize-minor-mode-map-alist)
1133 (vip-adjust-keys-for new-state)
1134 (vip-set-mode-vars-for new-state)
1135 (vip-refresh-mode-line)
1140 (defun vip-adjust-keys-for (state)
1141 "Make necessary adjustments to keymaps before entering STATE."
1142 (cond ((memq state '(insert-state replace-state))
1143 (if vip-auto-indent
1144 (progn
1145 (define-key vip-insert-basic-map "\C-m" 'vip-autoindent)
1146 (if vip-want-emacs-keys-in-insert
1147 ;; expert
1148 (define-key vip-insert-basic-map "\C-j" nil)
1149 ;; novice
1150 (define-key vip-insert-basic-map "\C-j" 'vip-autoindent))))
1152 (setq vip-insert-diehard-minor-mode
1153 (not vip-want-emacs-keys-in-insert))
1155 (if vip-want-ctl-h-help
1156 (progn
1157 (define-key vip-insert-basic-map "\C-h" 'help-command)
1158 (define-key vip-replace-map "\C-h" 'help-command))
1159 (define-key vip-insert-basic-map
1160 "\C-h" 'vip-del-backward-char-in-insert)
1161 (define-key vip-replace-map
1162 "\C-h" 'vip-del-backward-char-in-replace)))
1165 (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi))
1166 (if vip-want-ctl-h-help
1167 (define-key vip-vi-basic-map "\C-h" 'help-command)
1168 (define-key vip-vi-basic-map "\C-h" 'vip-backward-char)))
1172 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
1173 ;; This ensures that Viper bindings are in effect, regardless of which minor
1174 ;; modes were turned on by the user or by other packages.
1175 (defun vip-normalize-minor-mode-map-alist ()
1176 (setq minor-mode-map-alist
1177 (vip-append-filter-alist
1178 (list
1179 (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map)
1180 (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map)
1181 (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map)
1182 (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map)
1183 (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map)
1184 (cons 'vip-vi-state-modifier-minor-mode
1185 (if (keymapp
1186 (cdr (assoc major-mode vip-vi-state-modifier-alist)))
1187 (cdr (assoc major-mode vip-vi-state-modifier-alist))
1188 vip-empty-keymap))
1189 (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map)
1190 (cons 'vip-vi-basic-minor-mode vip-vi-basic-map)
1191 (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map)
1192 (cons 'vip-replace-minor-mode vip-replace-map)
1193 ;; vip-insert-minibuffer-minor-mode must come after
1194 ;; vip-replace-minor-mode
1195 (cons 'vip-insert-minibuffer-minor-mode
1196 vip-minibuffer-map)
1197 (cons 'vip-insert-local-user-minor-mode
1198 vip-insert-local-user-map)
1199 (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map)
1200 (cons 'vip-insert-global-user-minor-mode
1201 vip-insert-global-user-map)
1202 (cons 'vip-insert-state-modifier-minor-mode
1203 (if (keymapp
1204 (cdr
1205 (assoc major-mode vip-insert-state-modifier-alist)))
1206 (cdr
1207 (assoc major-mode vip-insert-state-modifier-alist))
1208 vip-empty-keymap))
1209 (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map)
1210 (cons 'vip-insert-basic-minor-mode vip-insert-basic-map)
1211 (cons 'vip-emacs-intercept-minor-mode
1212 vip-emacs-intercept-map)
1213 (cons 'vip-emacs-local-user-minor-mode
1214 vip-emacs-local-user-map)
1215 (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map)
1216 (cons 'vip-emacs-global-user-minor-mode
1217 vip-emacs-global-user-map)
1218 (cons 'vip-emacs-state-modifier-minor-mode
1219 (if (keymapp
1220 (cdr
1221 (assoc major-mode vip-emacs-state-modifier-alist)))
1222 (cdr
1223 (assoc major-mode vip-emacs-state-modifier-alist))
1224 vip-empty-keymap))
1226 minor-mode-map-alist)))
1232 ;; Viper mode-changing commands and utilities
1234 ;; Modifies mode-line-buffer-identification.
1235 (defun vip-refresh-mode-line ()
1236 (setq vip-mode-string
1237 (cond ((eq vip-current-state 'emacs-state) vip-emacs-state-id)
1238 ((eq vip-current-state 'vi-state) vip-vi-state-id)
1239 ((eq vip-current-state 'replace-state) vip-replace-state-id)
1240 ((eq vip-current-state 'insert-state) vip-insert-state-id)))
1242 ;; Sets Viper mode string in global-mode-string
1243 (force-mode-line-update))
1245 ;;;###autoload
1246 (defun viper-mode ()
1247 "Turn on Viper emulation of Vi."
1248 (interactive)
1249 (if (not noninteractive)
1250 (progn
1251 (if vip-first-time ; This check is important. Without it, startup and
1252 (progn ; expert-level msgs mix up when viper-mode recurses
1253 (setq vip-first-time nil)
1254 (if (not vip-inhibit-startup-message)
1255 (save-window-excursion
1256 (setq vip-inhibit-startup-message t)
1257 (delete-other-windows)
1258 (switch-to-buffer "Viper Startup Message")
1259 (erase-buffer)
1260 (insert
1261 (substitute-command-keys
1262 "Viper Is a Package for Emacs Rebels.
1263 It is also a VI Plan for Emacs Rescue and a venomous VI PERil.
1265 Technically speaking, Viper is a Vi emulation package for GNU Emacs 19 and
1266 XEmacs 19. It supports virtually all of Vi and Ex functionality, extending
1267 and improving upon much of it.
1269 1. Viper supports Vi at several levels. Level 1 is the closest to Vi,
1270 level 5 provides the most flexibility to depart from many Vi conventions.
1272 You will be asked to specify your user level in a following screen.
1274 If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will behave
1275 as in VI, to smooth transition to Viper for the beginners. However, to
1276 use Emacs productively, you are advised to reach user level 3 or higher.
1278 If your user level is 2 or higher, ^X and ^C will invoke Emacs
1279 functions,as usual in Emacs; ^Z will toggle vi/emacs modes, and
1280 ^G will be the usual Emacs's keyboard-quit (something like ^C in VI).
1282 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
1283 do not cause Emacs to quit, except at user level 1 (a novice).
1284 3. ^X^C EXITS EMACS.
1285 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
1286 undo. Another `u' changes direction.
1288 6. Emacs Meta functions are invoked by typing `C-\\' or `\\ ESC'.
1289 On a window system, the best way is to use the Meta-key.
1290 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if
1291 something funny happens. This would abort the current editing command.
1293 You can get more information on Viper by:
1295 a. Typing `:help' in Vi state
1296 b. Printing Viper manual, found in ./etc/viper.dvi
1297 c. Printing ViperCard, the Quick Reference, found in ./etc/viperCard.dvi
1299 This startup message appears whenever you load Viper, unless you type `y' now."
1301 (goto-char (point-min))
1302 (if (y-or-n-p "Inhibit Viper startup message? ")
1303 (vip-save-setting
1304 'vip-inhibit-startup-message
1305 "Viper startup message inhibited"
1306 vip-custom-file-name t))
1307 ;;(kill-buffer (current-buffer))
1308 (message
1309 "The last message is in buffer `Viper Startup Message'")
1310 (sit-for 4)
1312 (vip-set-expert-level 'dont-change-unless)))
1313 (vip-change-state-to-vi))))
1315 ;;;###autoload
1316 (defalias 'vip-mode 'viper-mode)
1319 ;; Switch from Insert state to Vi state.
1320 (defun vip-exit-insert-state ()
1321 (interactive)
1322 (vip-change-state-to-vi))
1324 (defun vip-set-mode-vars-for (state)
1325 "Sets Viper minor mode variables to put Viper's state STATE in effect."
1327 ;; Emacs state
1328 (setq vip-vi-minibuffer-minor-mode nil
1329 vip-insert-minibuffer-minor-mode nil
1330 vip-vi-intercept-minor-mode nil
1331 vip-insert-intercept-minor-mode nil
1333 vip-vi-local-user-minor-mode nil
1334 vip-vi-kbd-minor-mode nil
1335 vip-vi-global-user-minor-mode nil
1336 vip-vi-state-modifier-minor-mode nil
1337 vip-vi-diehard-minor-mode nil
1338 vip-vi-basic-minor-mode nil
1340 vip-replace-minor-mode nil
1342 vip-insert-local-user-minor-mode nil
1343 vip-insert-kbd-minor-mode nil
1344 vip-insert-global-user-minor-mode nil
1345 vip-insert-state-modifier-minor-mode nil
1346 vip-insert-diehard-minor-mode nil
1347 vip-insert-basic-minor-mode nil
1348 vip-emacs-intercept-minor-mode t
1349 vip-emacs-local-user-minor-mode t
1350 vip-emacs-kbd-minor-mode (not (vip-is-in-minibuffer))
1351 vip-emacs-global-user-minor-mode t
1352 vip-emacs-state-modifier-minor-mode t
1355 ;; Vi state
1356 (if (eq state 'vi-state) ; adjust for vi-state
1357 (setq
1358 vip-vi-intercept-minor-mode t
1359 vip-vi-minibuffer-minor-mode (vip-is-in-minibuffer)
1360 vip-vi-local-user-minor-mode t
1361 vip-vi-kbd-minor-mode (not (vip-is-in-minibuffer))
1362 vip-vi-global-user-minor-mode t
1363 vip-vi-state-modifier-minor-mode t
1364 ;; don't let the diehard keymap block command completion
1365 ;; and other things in the minibuffer
1366 vip-vi-diehard-minor-mode (not
1367 (or vip-want-emacs-keys-in-vi
1368 (vip-is-in-minibuffer)))
1369 vip-vi-basic-minor-mode t
1370 vip-emacs-intercept-minor-mode nil
1371 vip-emacs-local-user-minor-mode nil
1372 vip-emacs-kbd-minor-mode nil
1373 vip-emacs-global-user-minor-mode nil
1374 vip-emacs-state-modifier-minor-mode nil
1377 ;; Insert and Replace states
1378 (if (member state '(insert-state replace-state))
1379 (setq
1380 vip-insert-intercept-minor-mode t
1381 vip-replace-minor-mode (eq state 'replace-state)
1382 vip-insert-minibuffer-minor-mode (vip-is-in-minibuffer)
1383 vip-insert-local-user-minor-mode t
1384 vip-insert-kbd-minor-mode (not (vip-is-in-minibuffer))
1385 vip-insert-global-user-minor-mode t
1386 vip-insert-state-modifier-minor-mode t
1387 ;; don't let the diehard keymap block command completion
1388 ;; and other things in the minibuffer
1389 vip-insert-diehard-minor-mode (not
1390 (or vip-want-emacs-keys-in-insert
1391 (vip-is-in-minibuffer)))
1392 vip-insert-basic-minor-mode t
1393 vip-emacs-intercept-minor-mode nil
1394 vip-emacs-local-user-minor-mode nil
1395 vip-emacs-kbd-minor-mode nil
1396 vip-emacs-global-user-minor-mode nil
1397 vip-emacs-state-modifier-minor-mode nil
1400 ;; minibuffer faces
1401 (if (vip-has-face-support-p)
1402 (setq vip-minibuffer-current-face
1403 (cond ((eq state 'emacs-state) vip-minibuffer-emacs-face)
1404 ((eq state 'vi-state) vip-minibuffer-vi-face)
1405 ((memq state '(insert-state replace-state))
1406 vip-minibuffer-insert-face))))
1408 (if (vip-is-in-minibuffer)
1409 (vip-set-minibuffer-overlay))
1412 ;; This also takes care of the annoying incomplete lines in files.
1413 ;; Also, this fixes `undo' to work vi-style for complex commands.
1414 (defun vip-change-state-to-vi ()
1415 "Change Viper state to Vi."
1416 (interactive)
1417 (if (and vip-first-time (not (vip-is-in-minibuffer)))
1418 (viper-mode)
1419 (if overwrite-mode (overwrite-mode nil))
1420 (if abbrev-mode (expand-abbrev))
1421 (if (and auto-fill-function (> (current-column) fill-column))
1422 (funcall auto-fill-function))
1423 ;; don't leave whitespace lines around
1424 (if (and (memq last-command
1425 '(vip-autoindent
1426 vip-open-line vip-Open-line
1427 vip-replace-state-exit-cmd))
1428 (vip-over-whitespace-line))
1429 (indent-to-left-margin))
1430 (vip-add-newline-at-eob-if-necessary)
1431 (if vip-undo-needs-adjustment (vip-adjust-undo))
1432 (vip-change-state 'vi-state)
1434 ;; always turn off iso-accents-mode, or else we won't be able to use the
1435 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
1436 (if (and (boundp 'iso-accents-mode) iso-accents-mode)
1437 (iso-accents-mode -1))
1439 ;; Protection against user errors in hooks
1440 (condition-case conds
1441 (run-hooks 'vip-vi-state-hook)
1442 (error
1443 (vip-message-conditions conds)))))
1445 (defun vip-change-state-to-insert ()
1446 "Change Viper state to Insert."
1447 (interactive)
1448 (vip-change-state 'insert-state)
1449 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
1450 (iso-accents-mode 1)) ; turn iso accents on
1452 ;; Protection against user errors in hooks
1453 (condition-case conds
1454 (run-hooks 'vip-insert-state-hook)
1455 (error
1456 (vip-message-conditions conds))))
1458 (defsubst vip-downgrade-to-insert ()
1459 (setq vip-current-state 'insert-state
1460 vip-replace-minor-mode nil)
1465 ;; Change to replace state. When the end of replacement region is reached,
1466 ;; replace state changes to insert state.
1467 (defun vip-change-state-to-replace (&optional non-R-cmd)
1468 (vip-change-state 'replace-state)
1469 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
1470 (iso-accents-mode 1)) ; turn iso accents on
1471 ;; Run insert-state-hook
1472 (condition-case conds
1473 (run-hooks 'vip-insert-state-hook 'vip-replace-state-hook)
1474 (error
1475 (vip-message-conditions conds)))
1477 (if non-R-cmd
1478 (vip-start-replace)
1479 ;; 'R' is implemented using Emacs's overwrite-mode
1480 (vip-start-R-mode))
1484 (defun vip-change-state-to-emacs ()
1485 "Change Viper state to Emacs."
1486 (interactive)
1487 (vip-change-state 'emacs-state)
1488 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
1489 (iso-accents-mode 1)) ; turn iso accents on
1491 ;; Protection agains user errors in hooks
1492 (condition-case conds
1493 (run-hooks 'vip-emacs-state-hook)
1494 (error
1495 (vip-message-conditions conds))))
1497 ;; escape to emacs mode termporarily
1498 (defun vip-escape-to-emacs (arg &optional events)
1499 "Escape to Emacs state from Vi state for one Emacs command.
1500 ARG is used as the prefix value for the executed command. If
1501 EVENTS is a list of events, which become the beginning of the command."
1502 (interactive "P")
1503 (if (= last-command-char ?\\)
1504 (message "Switched to EMACS state for the next command..."))
1505 (vip-escape-to-state arg events 'emacs-state))
1507 ;; escape to Vi mode termporarily
1508 (defun vip-escape-to-vi (arg)
1509 "Escape from Emacs state to Vi state for one Vi 1-character command.
1510 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
1511 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
1512 `vip-escape-to-vi' is used."
1513 (interactive "P")
1514 (message "Switched to VI state for the next command...")
1515 (vip-escape-to-state arg nil 'vi-state))
1517 ;; Escape to STATE mode for one Emacs command.
1518 (defun vip-escape-to-state (arg events state)
1519 ;;(let (com key prefix-arg)
1520 (let (com key)
1521 ;; this temporarily turns off Viper's minor mode keymaps
1522 (vip-set-mode-vars-for state)
1523 (vip-normalize-minor-mode-map-alist)
1524 (if events (vip-set-unread-command-events events))
1526 ;; protect against keyboard quit and other errors
1527 (condition-case nil
1528 (let (vip-vi-kbd-minor-mode
1529 vip-insert-kbd-minor-mode
1530 vip-emacs-kbd-minor-mode)
1531 (unwind-protect
1532 (progn
1533 (setq com (key-binding (setq key
1534 (if vip-xemacs-p
1535 (read-key-sequence nil)
1536 (read-key-sequence nil t)))))
1537 ;; In case of binding indirection--chase definitions.
1538 ;; Have to do it here because we execute this command under
1539 ;; different keymaps, so command-execute may not do the
1540 ;; right thing there
1541 (while (vectorp com) (setq com (key-binding com))))
1542 nil)
1543 ;; Execute command com in the original Viper state, not in state
1544 ;; `state'. Otherwise, if we switch buffers while executing the
1545 ;; escaped to command, Viper's mode vars will remain those of
1546 ;; `state'. When we return to the orig buffer, the bindings will be
1547 ;; screwed up.
1548 (vip-set-mode-vars-for vip-current-state)
1550 ;; this-command, last-command-char, last-command-event
1551 (setq this-command com)
1552 (if vip-xemacs-p ; XEmacs represents key sequences as vectors
1553 (setq last-command-event (vip-seq-last-elt key)
1554 last-command-char (event-to-character last-command-event))
1555 ;; Emacs represents them as sequences (str or vec)
1556 (setq last-command-event (vip-seq-last-elt key)
1557 last-command-char last-command-event))
1559 (if (commandp com)
1560 (progn
1561 (setq prefix-arg (or prefix-arg arg))
1562 (command-execute com)))
1564 (quit (ding))
1565 (error (beep 1))))
1566 ;; set state in the new buffer
1567 (vip-set-mode-vars-for vip-current-state))
1569 (defun vip-exec-form-in-vi (form)
1570 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
1571 (let ((buff (current-buffer))
1572 result)
1573 (vip-set-mode-vars-for 'vi-state)
1574 (setq result (eval form))
1575 (if (not (equal buff (current-buffer))) ; cmd switched buffer
1576 (save-excursion
1577 (set-buffer buff)
1578 (vip-set-mode-vars-for vip-current-state)))
1579 (vip-set-mode-vars-for vip-current-state)
1580 result))
1582 (defun vip-exec-form-in-emacs (form)
1583 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
1584 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
1585 (let ((buff (current-buffer))
1586 result)
1587 (vip-set-mode-vars-for 'emacs-state)
1588 (setq result (eval form))
1589 (if (not (equal buff (current-buffer))) ; cmd switched buffer
1590 (save-excursion
1591 (set-buffer buff)
1592 (vip-set-mode-vars-for vip-current-state)))
1593 (vip-set-mode-vars-for vip-current-state)
1594 result))
1597 ;; This is needed because minor modes sometimes override essential Viper
1598 ;; bindings. By letting Viper know which files these modes are in, it will
1599 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
1600 (defun vip-harness-minor-mode (load-file)
1601 "Familiarize Viper with a minor mode defined in LOAD_FILE.
1602 Minor modes that have their own keymaps may overshadow Viper keymaps.
1603 This function is designed to make Viper aware of the packages that define
1604 such minor modes.
1605 Usage:
1606 (vip-harness-minor-mode load-file)
1608 LOAD-FILE is a name of the file where the specific minor mode is defined.
1609 Suffixes such as .el or .elc should be stripped."
1611 (interactive "sEnter name of the load file: ")
1613 (vip-eval-after-load load-file '(vip-normalize-minor-mode-map-alist))
1615 ;; Change the default for minor-mode-map-alist each time a harnessed minor
1616 ;; mode adds its own keymap to the a-list.
1617 (vip-eval-after-load
1618 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist))
1622 (defun vip-ESC (arg)
1623 "Emulate ESC key in Emacs.
1624 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true.
1625 If vip-no-multiple-ESC is 'twice double ESC would ding in vi-state.
1626 Other ESC sequences are emulated via the current Emacs's major mode
1627 keymap. This is more convenient on TTYs, since this won't block
1628 function keys such as up,down, etc. ESC will also will also work as
1629 a Meta key in this case. When vip-no-multiple-ESC is nil, ESC functions
1630 as a Meta key and any number of multiple escapes is allowed."
1631 (interactive "P")
1632 (let (char)
1633 (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state))
1634 (setq char (vip-read-char-exclusive))
1635 (vip-escape-to-emacs arg (list ?\e char) ))
1636 ((and (eq vip-no-multiple-ESC 'twice)
1637 (eq vip-current-state 'vi-state))
1638 (setq char (vip-read-char-exclusive))
1639 (if (= char (string-to-char vip-ESC-key))
1640 (ding)
1641 (vip-escape-to-emacs arg (list ?\e char) )))
1642 (t (ding)))
1645 (defun vip-alternate-Meta-key (arg)
1646 "Simulate Emacs Meta key."
1647 (interactive "P")
1648 (sit-for 1) (message "ESC-")
1649 (vip-escape-to-emacs arg '(?\e)))
1651 (defun vip-toggle-key-action ()
1652 "Action bound to `vip-toggle-key'."
1653 (interactive)
1654 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
1655 (if (vip-window-display-p)
1656 (vip-iconify)
1657 (suspend-emacs))
1658 (vip-change-state-to-emacs)))
1661 ;; Intercept ESC sequences on dumb terminals.
1662 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
1664 ;; Check if last key was ESC and if so try to reread it as a function key.
1665 ;; But only if there are characters to read during a very short time.
1666 ;; Returns the last event, if any.
1667 (defun vip-envelop-ESC-key ()
1668 (let ((event last-input-event)
1669 (keyseq [nil])
1670 inhibit-quit)
1671 (if (vip-ESC-event-p event)
1672 (progn
1673 (if (vip-fast-keysequence-p)
1674 (progn
1675 (let (minor-mode-map-alist)
1676 (vip-set-unread-command-events event)
1677 (setq keyseq
1678 (funcall
1679 (ad-get-orig-definition 'read-key-sequence) nil))
1680 ) ; let
1681 ;; If keyseq translates into something that still has ESC
1682 ;; at the beginning, separate ESC from the rest of the seq.
1683 ;; In XEmacs we check for events that are keypress meta-key
1684 ;; and convert them into [escape key]
1686 ;; This is needed for the following reason:
1687 ;; If ESC is the first symbol, we interpret it as if the
1688 ;; user typed ESC and then quickly some other symbols.
1689 ;; If ESC is not the first one, then the key sequence
1690 ;; entered was apparently translated into a function key or
1691 ;; something (e.g., one may have
1692 ;; (define-key function-key-map "\e[192z" [f11])
1693 ;; which would translate the escape-sequence generated by
1694 ;; f11 in an xterm window into the symbolic key f11.
1696 ;; If `first-key' is not an ESC event, we make it into the
1697 ;; last-command-event in order to pretend that this key was
1698 ;; pressed. This is needed to allow arrow keys to be bound to
1699 ;; macros. Otherwise, vip-exec-mapped-kbd-macro will think that
1700 ;; the last event was ESC and so it'll execute whatever is
1701 ;; bound to ESC. (Viper macros can't be bound to
1702 ;; ESC-sequences).
1703 (let* ((first-key (elt keyseq 0))
1704 (key-mod (event-modifiers first-key)))
1705 (cond ((vip-ESC-event-p first-key)
1706 ;; put keys following ESC on the unread list
1707 ;; and return ESC as the key-sequence
1708 (vip-set-unread-command-events (subseq keyseq 1))
1709 (setq last-input-event event
1710 keyseq (if vip-emacs-p
1711 "\e"
1712 (vector (character-to-event ?\e)))))
1713 ((and vip-xemacs-p
1714 (key-press-event-p first-key)
1715 (equal '(meta) key-mod))
1716 (vip-set-unread-command-events
1717 (vconcat (vector
1718 (character-to-event (event-key first-key)))
1719 (subseq keyseq 1)))
1720 (setq last-input-event event
1721 keyseq (vector (character-to-event ?\e))))
1722 ((eventp first-key)
1723 (setq last-command-event first-key))
1725 ) ; end progn
1727 ;; this is escape event with nothing after it
1728 ;; put in unread-command-event and then re-read
1729 (vip-set-unread-command-events event)
1730 (setq keyseq
1731 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
1733 ;; not an escape event
1734 (setq keyseq (vector event)))
1735 keyseq))
1739 (defadvice read-key-sequence (around vip-read-keyseq-ad activate)
1740 "Harness to work for Viper. This advice is harmless---don't worry!"
1741 (let (inhibit-quit event keyseq)
1742 (setq keyseq ad-do-it)
1743 (setq event (if vip-xemacs-p
1744 (elt keyseq 0) ; XEmacs returns vector of events
1745 (elt (listify-key-sequence keyseq) 0)))
1746 (if (vip-ESC-event-p event)
1747 (let (unread-command-events)
1748 (vip-set-unread-command-events keyseq)
1749 (if (vip-fast-keysequence-p)
1750 (let ((vip-vi-global-user-minor-mode nil)
1751 (vip-vi-local-user-minor-mode nil)
1752 (vip-replace-minor-mode nil) ; actually unnecessary
1753 (vip-insert-global-user-minor-mode nil)
1754 (vip-insert-local-user-minor-mode nil))
1755 (setq keyseq ad-do-it))
1756 (setq keyseq ad-do-it))))
1757 keyseq))
1759 (defadvice describe-key (before vip-read-keyseq-ad protect activate)
1760 "Force to read key via `read-key-sequence'."
1761 (interactive (list (vip-events-to-keys
1762 (read-key-sequence "Describe key: ")))))
1764 (defadvice describe-key-briefly (before vip-read-keyseq-ad protect activate)
1765 "Force to read key via `read-key-sequence'."
1766 (interactive (list (vip-events-to-keys
1767 (read-key-sequence "Describe key briefly: ")))))
1769 ;; Listen to ESC key.
1770 ;; If a sequence of keys starting with ESC is issued with very short delays,
1771 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
1772 (defun vip-intercept-ESC-key ()
1773 "Function that implements ESC key in Viper emulation of Vi."
1774 (interactive)
1775 (let ((cmd (or (key-binding (vip-envelop-ESC-key))
1776 '(lambda () (interactive) (error "")))))
1778 ;; call the actual function to execute ESC (if no other symbols followed)
1779 ;; or the key bound to the ESC sequence (if the sequence was issued
1780 ;; with very short delay between characters.
1781 (if (eq cmd 'vip-intercept-ESC-key)
1782 (setq cmd
1783 (cond ((eq vip-current-state 'vi-state)
1784 'vip-ESC)
1785 ((eq vip-current-state 'insert-state)
1786 'vip-exit-insert-state)
1787 ((eq vip-current-state 'replace-state)
1788 'vip-replace-state-exit-cmd)
1789 (t 'vip-change-state-to-vi)
1791 (call-interactively cmd)))
1795 ;; prefix argument for Vi mode
1797 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
1798 ;; represents the numeric value of the prefix argument and COM represents
1799 ;; command prefix such as "c", "d", "m" and "y".
1801 ;; Get value part of prefix-argument ARG.
1802 (defsubst vip-p-val (arg)
1803 (cond ((null arg) 1)
1804 ((consp arg)
1805 (if (or (null (car arg)) (equal (car arg) '(nil)))
1806 1 (car arg)))
1807 (t arg)))
1809 ;; Get raw value part of prefix-argument ARG.
1810 (defsubst vip-P-val (arg)
1811 (cond ((consp arg) (car arg))
1812 (t arg)))
1814 ;; Get com part of prefix-argument ARG.
1815 (defsubst vip-getcom (arg)
1816 (cond ((null arg) nil)
1817 ((consp arg) (cdr arg))
1818 (t nil)))
1820 ;; Get com part of prefix-argument ARG and modify it.
1821 (defun vip-getCom (arg)
1822 (let ((com (vip-getcom arg)))
1823 (cond ((equal com ?c) ?C)
1824 ((equal com ?d) ?D)
1825 ((equal com ?y) ?Y)
1826 (t com))))
1829 ;; Compute numeric prefix arg value.
1830 ;; Invoked by EVENT. COM is the command part obtained so far.
1831 (defun vip-prefix-arg-value (event com)
1832 (let (value func)
1833 ;; read while number
1834 (while (and (vip-characterp event) (>= event ?0) (<= event ?9))
1835 (setq value (+ (* (if (vip-characterp value) value 0) 10) (- event ?0)))
1836 (setq event (vip-read-event-convert-to-char)))
1838 (setq prefix-arg value)
1839 (if com (setq prefix-arg (cons prefix-arg com)))
1840 (while (eq event ?U)
1841 (vip-describe-arg prefix-arg)
1842 (setq event (vip-read-event-convert-to-char)))
1844 (if (or com (and (not (eq vip-current-state 'vi-state))
1845 ;; make sure it is a Vi command
1846 (vip-characterp event) (vip-vi-command-p event)
1848 ;; If appears to be one of the vi commands,
1849 ;; then execute it with funcall and clear prefix-arg in order to not
1850 ;; confuse subsequent commands
1851 (progn
1852 ;; last-command-char is the char we want emacs to think was typed
1853 ;; last. If com is not nil, the vip-digit-argument command was called
1854 ;; from within vip-prefix-arg command, such as `d', `w', etc., i.e.,
1855 ;; the user typed, say, d2. In this case, `com' would be `d', `w',
1856 ;; etc.
1857 ;; If vip-digit-argument was invoked by vip-escape-to-vi (which is
1858 ;; indicated by the fact that the current state is not vi-state,
1859 ;; then `event' represents the vi command to be executed (e.g., `d',
1860 ;; `w', etc. Again, last-command-char must make emacs believe that
1861 ;; this is the command we typed.
1862 (setq last-command-char (or com event))
1863 (setq func (vip-exec-form-in-vi
1864 (` (key-binding (char-to-string (, event))))))
1865 (funcall func prefix-arg)
1866 (setq prefix-arg nil))
1867 ;; some other command -- let emacs do it in its own way
1868 (vip-set-unread-command-events event))
1872 ;; Vi operator as prefix argument."
1873 (defun vip-prefix-arg-com (char value com)
1874 (let ((cont t)
1875 cmd-info mv-or-digit-cmd)
1876 (while (and cont
1877 (memq char
1878 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1879 vip-buffer-search-char)))
1880 (if com
1881 ;; this means that we already have a command character, so we
1882 ;; construct a com list and exit while. however, if char is "
1883 ;; it is an error.
1884 (progn
1885 ;; new com is (CHAR . OLDCOM)
1886 (if (memq char '(?# ?\")) (error ""))
1887 (setq com (cons char com))
1888 (setq cont nil))
1889 ;; If com is nil we set com as char, and read more. Again, if char
1890 ;; is ", we read the name of register and store it in vip-use-register.
1891 ;; if char is !, =, or #, a complete com is formed so we exit the
1892 ;; while loop.
1893 (cond ((memq char '(?! ?=))
1894 (setq com char)
1895 (setq char (read-char))
1896 (setq cont nil))
1897 ((= char ?#)
1898 ;; read a char and encode it as com
1899 (setq com (+ 128 (read-char)))
1900 (setq char (read-char)))
1901 ((= char ?\")
1902 (let ((reg (read-char)))
1903 (if (vip-valid-register reg)
1904 (setq vip-use-register reg)
1905 (error ""))
1906 (setq char (read-char))))
1908 (setq com char)
1909 (setq char (vip-read-char-exclusive))))))
1911 (if (atom com)
1912 ;; `com' is a single char, so we construct the command argument
1913 ;; and if `char' is `?', we describe the arg; otherwise
1914 ;; we prepare the command that will be executed at the end.
1915 (progn
1916 (setq cmd-info (cons value com))
1917 (while (= char ?U)
1918 (vip-describe-arg cmd-info)
1919 (setq char (read-char)))
1920 ;; `char' is a movement command or a digit arg command---so we execute
1921 ;; it at the very end
1922 (setq mv-or-digit-cmd
1923 (vip-exec-form-in-vi
1924 (` (key-binding (char-to-string (, char)))))))
1926 ;; as com is non-nil, this means that we have a command to execute
1927 (if (memq (car com) '(?r ?R))
1928 ;; execute apropriate region command.
1929 (let ((char (car com)) (com (cdr com)))
1930 (setq prefix-arg (cons value com))
1931 (if (= char ?r) (vip-region prefix-arg)
1932 (vip-Region prefix-arg))
1933 ;; reset prefix-arg
1934 (setq prefix-arg nil))
1935 ;; otherwise, reset prefix arg and call appropriate command
1936 (setq value (if (null value) 1 value))
1937 (setq prefix-arg nil)
1938 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1939 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1940 ((equal com '(?d . ?y)) (vip-yank-defun))
1941 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1942 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1943 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1944 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1945 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1946 (t (error "")))))
1948 (if mv-or-digit-cmd
1949 (progn
1950 (setq last-command-char char)
1951 (funcall mv-or-digit-cmd cmd-info)))
1954 (defun vip-describe-arg (arg)
1955 (let (val com)
1956 (setq val (vip-P-val arg)
1957 com (vip-getcom arg))
1958 (if (null val)
1959 (if (null com)
1960 (message "Value is nil, and command is nil")
1961 (message "Value is nil, and command is `%c'" com))
1962 (if (null com)
1963 (message "Value is `%d', and command is nil" val)
1964 (message "Value is `%d', and command is `%c'" val com)))))
1966 (defun vip-digit-argument (arg)
1967 "Begin numeric argument for the next command."
1968 (interactive "P")
1969 (vip-leave-region-active)
1970 (vip-prefix-arg-value
1971 last-command-char (if (consp arg) (cdr arg) nil)))
1973 (defun vip-command-argument (arg)
1974 "Accept a motion command as an argument."
1975 (interactive "P")
1976 (condition-case nil
1977 (vip-prefix-arg-com
1978 last-command-char
1979 (cond ((null arg) nil)
1980 ((consp arg) (car arg))
1981 ((integerp arg) arg)
1982 (t (error vip-InvalidCommandArgument)))
1983 (cond ((null arg) nil)
1984 ((consp arg) (cdr arg))
1985 ((integerp arg) nil)
1986 (t (error vip-InvalidCommandArgument))))
1987 (quit (setq vip-use-register nil)
1988 (signal 'quit nil)))
1989 (vip-deactivate-mark))
1992 ;; repeat last destructive command
1994 ;; Append region to text in register REG.
1995 ;; START and END are buffer positions indicating what to append.
1996 (defsubst vip-append-to-register (reg start end)
1997 (set-register reg (concat (if (stringp (get-register reg))
1998 (get-register reg) "")
1999 (buffer-substring start end))))
2001 ;; Saves last inserted text for possible use by vip-repeat command.
2002 (defun vip-save-last-insertion (beg end)
2003 (setq vip-last-insertion (buffer-substring beg end))
2004 (or (< (length vip-d-com) 5)
2005 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
2006 (or (null vip-command-ring)
2007 (ring-empty-p vip-command-ring)
2008 (progn
2009 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
2010 vip-last-insertion)
2011 ;; del most recent elt, if identical to the second most-recent
2012 (vip-cleanup-ring vip-command-ring)))
2015 (defsubst vip-yank-last-insertion ()
2016 "Inserts the text saved by the previous vip-save-last-insertion command."
2017 (condition-case nil
2018 (insert vip-last-insertion)
2019 (error nil)))
2022 ;; define functions to be executed
2024 ;; invoked by the `C' command
2025 (defun vip-exec-change (m-com com)
2026 ;; handle C cmd at the eol and at eob.
2027 (if (or (and (eolp) (= vip-com-point (point)))
2028 (= vip-com-point (point-max)))
2029 (progn
2030 (insert " ")(backward-char 1)))
2031 (if (= vip-com-point (point))
2032 (vip-forward-char-carefully))
2033 (if (= com ?c)
2034 (vip-change vip-com-point (point))
2035 (vip-change-subr vip-com-point (point))))
2037 ;; this is invoked by vip-substitute-line
2038 (defun vip-exec-Change (m-com com)
2039 (save-excursion
2040 (set-mark vip-com-point)
2041 (vip-enlarge-region (mark t) (point))
2042 (if vip-use-register
2043 (progn
2044 (cond ((vip-valid-register vip-use-register '(letter digit))
2045 ;;(vip-valid-register vip-use-register '(letter)
2046 (copy-to-register
2047 vip-use-register (mark t) (point) nil))
2048 ((vip-valid-register vip-use-register '(Letter))
2049 (vip-append-to-register
2050 (downcase vip-use-register) (mark t) (point)))
2051 (t (setq vip-use-register nil)
2052 (error vip-InvalidRegister vip-use-register)))
2053 (setq vip-use-register nil)))
2054 (delete-region (mark t) (point)))
2055 (open-line 1)
2056 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
2058 (defun vip-exec-delete (m-com com)
2059 (if vip-use-register
2060 (progn
2061 (cond ((vip-valid-register vip-use-register '(letter digit))
2062 ;;(vip-valid-register vip-use-register '(letter))
2063 (copy-to-register
2064 vip-use-register vip-com-point (point) nil))
2065 ((vip-valid-register vip-use-register '(Letter))
2066 (vip-append-to-register
2067 (downcase vip-use-register) vip-com-point (point)))
2068 (t (setq vip-use-register nil)
2069 (error vip-InvalidRegister vip-use-register)))
2070 (setq vip-use-register nil)))
2071 (setq last-command
2072 (if (eq last-command 'd-command) 'kill-region nil))
2073 (kill-region vip-com-point (point))
2074 (setq this-command 'd-command)
2075 (if vip-ex-style-motion
2076 (if (and (eolp) (not (bolp))) (backward-char 1))))
2078 (defun vip-exec-Delete (m-com com)
2079 (save-excursion
2080 (set-mark vip-com-point)
2081 (vip-enlarge-region (mark t) (point))
2082 (if vip-use-register
2083 (progn
2084 (cond ((vip-valid-register vip-use-register '(letter digit))
2085 ;;(vip-valid-register vip-use-register '(letter))
2086 (copy-to-register
2087 vip-use-register (mark t) (point) nil))
2088 ((vip-valid-register vip-use-register '(Letter))
2089 (vip-append-to-register
2090 (downcase vip-use-register) (mark t) (point)))
2091 (t (setq vip-use-register nil)
2092 (error vip-InvalidRegister vip-use-register)))
2093 (setq vip-use-register nil)))
2094 (setq last-command
2095 (if (eq last-command 'D-command) 'kill-region nil))
2096 (kill-region (mark t) (point))
2097 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
2098 (back-to-indentation))
2100 (defun vip-exec-yank (m-com com)
2101 (if vip-use-register
2102 (progn
2103 (cond ((vip-valid-register vip-use-register '(letter digit))
2104 ;; (vip-valid-register vip-use-register '(letter))
2105 (copy-to-register
2106 vip-use-register vip-com-point (point) nil))
2107 ((vip-valid-register vip-use-register '(Letter))
2108 (vip-append-to-register
2109 (downcase vip-use-register) vip-com-point (point)))
2110 (t (setq vip-use-register nil)
2111 (error vip-InvalidRegister vip-use-register)))
2112 (setq vip-use-register nil)))
2113 (setq last-command nil)
2114 (copy-region-as-kill vip-com-point (point))
2115 (goto-char vip-com-point))
2117 (defun vip-exec-Yank (m-com com)
2118 (save-excursion
2119 (set-mark vip-com-point)
2120 (vip-enlarge-region (mark t) (point))
2121 (if vip-use-register
2122 (progn
2123 (cond ((vip-valid-register vip-use-register '(letter digit))
2124 (copy-to-register
2125 vip-use-register (mark t) (point) nil))
2126 ((vip-valid-register vip-use-register '(Letter))
2127 (vip-append-to-register
2128 (downcase vip-use-register) (mark t) (point)))
2129 (t (setq vip-use-register nil)
2130 (error vip-InvalidRegister vip-use-register)))
2131 (setq vip-use-register nil)))
2132 (setq last-command nil)
2133 (copy-region-as-kill (mark t) (point)))
2134 (vip-deactivate-mark)
2135 (goto-char vip-com-point))
2137 (defun vip-exec-bang (m-com com)
2138 (save-excursion
2139 (set-mark vip-com-point)
2140 (vip-enlarge-region (mark t) (point))
2141 (shell-command-on-region
2142 (mark t) (point)
2143 (if (= com ?!)
2144 (setq vip-last-shell-com
2145 (vip-read-string-with-history
2148 'vip-shell-history
2149 (car vip-shell-history)
2151 vip-last-shell-com)
2152 t)))
2154 (defun vip-exec-equals (m-com com)
2155 (save-excursion
2156 (set-mark vip-com-point)
2157 (vip-enlarge-region (mark t) (point))
2158 (if (> (mark t) (point)) (exchange-point-and-mark))
2159 (indent-region (mark t) (point) nil)))
2161 (defun vip-exec-shift (m-com com)
2162 (save-excursion
2163 (set-mark vip-com-point)
2164 (vip-enlarge-region (mark t) (point))
2165 (if (> (mark t) (point)) (exchange-point-and-mark))
2166 (indent-rigidly (mark t) (point)
2167 (if (= com ?>)
2168 vip-shift-width
2169 (- vip-shift-width))))
2170 ;; return point to where it was before shift
2171 (goto-char vip-com-point))
2173 ;; this is needed because some commands fake com by setting it to ?r, which
2174 ;; denotes repeated insert command.
2175 (defsubst vip-exec-dummy (m-com com)
2176 nil)
2178 (defun vip-exec-buffer-search (m-com com)
2179 (setq vip-s-string (buffer-substring (point) vip-com-point))
2180 (setq vip-s-forward t)
2181 (setq vip-search-history (cons vip-s-string vip-search-history))
2182 (vip-search vip-s-string vip-s-forward 1))
2184 (defvar vip-exec-array (make-vector 128 nil))
2186 ;; Using a dispatch array allows adding functions like buffer search
2187 ;; without affecting other functions. Buffer search can now be bound
2188 ;; to any character.
2190 (aset vip-exec-array ?c 'vip-exec-change)
2191 (aset vip-exec-array ?C 'vip-exec-Change)
2192 (aset vip-exec-array ?d 'vip-exec-delete)
2193 (aset vip-exec-array ?D 'vip-exec-Delete)
2194 (aset vip-exec-array ?y 'vip-exec-yank)
2195 (aset vip-exec-array ?Y 'vip-exec-Yank)
2196 (aset vip-exec-array ?r 'vip-exec-dummy)
2197 (aset vip-exec-array ?! 'vip-exec-bang)
2198 (aset vip-exec-array ?< 'vip-exec-shift)
2199 (aset vip-exec-array ?> 'vip-exec-shift)
2200 (aset vip-exec-array ?= 'vip-exec-equals)
2204 ;; This function is called by various movement commands to execute a
2205 ;; destructive command on the region specified by the movement command. For
2206 ;; instance, if the user types cw, then the command vip-forward-word will
2207 ;; call vip-execute-com to execute vip-exec-change, which eventually will
2208 ;; call vip-change to invoke the replace mode on the region.
2210 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
2211 ;; vip-d-com for later use by vip-repeat.
2212 (defun vip-execute-com (m-com val com)
2213 (let ((reg vip-use-register))
2214 ;; this is the special command `#'
2215 (if (> com 128)
2216 (vip-special-prefix-com (- com 128))
2217 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
2218 (if (null fn)
2219 (error "%c: %s" com vip-InvalidViCommand)
2220 (funcall fn m-com com))))
2221 (if (vip-dotable-command-p com)
2222 (vip-set-destructive-command
2223 (list m-com val
2224 (if (memq com (list ?c ?C ?!)) (- com) com)
2225 reg nil nil)))
2229 (defun vip-repeat (arg)
2230 "Re-execute last destructive command.
2231 Use the info in vip-d-com, which has the form
2232 \(com val ch reg inserted-text command-keys\),
2233 where `com' is the command to be re-executed, `val' is the
2234 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
2235 if it exists, it is the name of the register for `com'.
2236 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
2237 (interactive "P")
2238 (let ((save-point (point)) ; save point before repeating prev cmd
2239 ;; Pass along that we are repeating a destructive command
2240 ;; This tells vip-set-destructive-command not to update
2241 ;; vip-command-ring
2242 (vip-intermediate-command 'vip-repeat))
2243 (if (eq last-command 'vip-undo)
2244 ;; if the last command was vip-undo, then undo-more
2245 (vip-undo-more)
2246 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
2247 ;; its prefix value is used as new prefix value for the command.
2248 (let ((m-com (car vip-d-com))
2249 (val (vip-P-val arg))
2250 (com (nth 2 vip-d-com))
2251 (reg (nth 3 vip-d-com)))
2252 (if (null val) (setq val (nth 1 vip-d-com)))
2253 (if (null m-com) (error "No previous command to repeat."))
2254 (setq vip-use-register reg)
2255 (if (nth 4 vip-d-com) ; text inserted by command
2256 (setq vip-last-insertion (nth 4 vip-d-com)
2257 vip-d-char (nth 4 vip-d-com)))
2258 (funcall m-com (cons val com))
2259 (if (and vip-keep-point-on-repeat (< save-point (point)))
2260 (goto-char save-point)) ; go back to before repeat.
2261 (if (and (eolp) (not (bolp)))
2262 (backward-char 1))
2264 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
2265 ;; If the prev cmd was rotating the command ring, this means that `.' has
2266 ;; just executed a command from that ring. So, push it on the ring again.
2267 ;; If we are just executing previous command , then don't push vip-d-com
2268 ;; because vip-d-com is not fully constructed in this case (its keys and
2269 ;; the inserted text may be nil). Besides, in this case, the command
2270 ;; executed by `.' is already on the ring.
2271 (if (eq last-command 'vip-display-current-destructive-command)
2272 (vip-push-onto-ring vip-d-com 'vip-command-ring))
2273 (vip-deactivate-mark)
2276 (defun vip-repeat-from-history ()
2277 "Repeat a destructive command from history.
2278 Doesn't change vip-command-ring in any way, so `.' will work as before
2279 executing this command.
2280 This command is supposed to be bound to a two-character Vi macro where
2281 the second character is a digit 0 to 9. The digit indicates which
2282 history command to execute. `<char>0' is equivalent to `.', `<char>1'
2283 invokes the command before that, etc."
2284 (interactive)
2285 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
2286 (idx (cond (vip-this-kbd-macro
2287 (string-to-number
2288 (symbol-name (elt vip-this-kbd-macro 1))))
2289 (t 0)))
2290 (num idx)
2291 (vip-d-com vip-d-com))
2293 (or (and (numberp num) (<= 0 num) (<= num 9))
2294 (progn
2295 (setq idx 0
2296 num 0)
2297 (message
2298 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
2299 (while (< 0 num)
2300 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
2301 (setq num (1- num)))
2302 (vip-repeat nil)
2303 (while (> idx num)
2304 (vip-special-ring-rotate1 vip-command-ring 1)
2305 (setq num (1+ num)))
2309 ;; This command is invoked interactively by the key sequence #<char>
2310 (defun vip-special-prefix-com (char)
2311 (cond ((= char ?c)
2312 (downcase-region (min vip-com-point (point))
2313 (max vip-com-point (point))))
2314 ((= char ?C)
2315 (upcase-region (min vip-com-point (point))
2316 (max vip-com-point (point))))
2317 ((= char ?g)
2318 (push-mark vip-com-point t)
2319 (vip-global-execute))
2320 ((= char ?q)
2321 (push-mark vip-com-point t)
2322 (vip-quote-region))
2323 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
2324 (t (error "#%c: %s" char vip-InvalidViCommand))))
2327 ;; undoing
2329 (defun vip-undo ()
2330 "Undo previous change."
2331 (interactive)
2332 (message "undo!")
2333 (let ((modified (buffer-modified-p))
2334 (before-undo-pt (point-marker))
2335 (after-change-functions after-change-functions)
2336 undo-beg-posn undo-end-posn)
2338 ;; no need to remove this hook, since this var has scope inside a let.
2339 (add-hook 'after-change-functions
2340 '(lambda (beg end len)
2341 (setq undo-beg-posn beg
2342 undo-end-posn (or end beg))))
2344 (undo-start)
2345 (undo-more 2)
2346 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
2347 undo-end-posn (or undo-end-posn undo-beg-posn))
2349 (goto-char undo-beg-posn)
2350 (sit-for 0)
2351 (if (and vip-keep-point-on-undo
2352 (pos-visible-in-window-p before-undo-pt))
2353 (progn
2354 (push-mark (point-marker) t)
2355 (vip-sit-for-short 300)
2356 (goto-char undo-end-posn)
2357 (vip-sit-for-short 300)
2358 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
2359 (> (abs (- undo-end-posn before-undo-pt)) 1))
2360 (goto-char before-undo-pt)
2361 (goto-char undo-beg-posn)))
2362 (push-mark before-undo-pt t))
2363 (if (and (eolp) (not (bolp))) (backward-char 1))
2364 (if (not modified) (set-buffer-modified-p t)))
2365 (setq this-command 'vip-undo))
2367 ;; Continue undoing previous changes.
2368 (defun vip-undo-more ()
2369 (message "undo more!")
2370 (condition-case nil
2371 (undo-more 1)
2372 (error (beep)
2373 (message "No further undo information in this buffer")))
2374 (if (and (eolp) (not (bolp))) (backward-char 1))
2375 (setq this-command 'vip-undo))
2377 ;; The following two functions are used to set up undo properly.
2378 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
2379 ;; they are undone all at once.
2380 (defun vip-adjust-undo ()
2381 (let ((inhibit-quit t)
2382 tmp tmp2)
2383 (setq vip-undo-needs-adjustment nil)
2384 (if (listp buffer-undo-list)
2385 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
2386 (progn
2387 (setq tmp2 (cdr tmp)) ; the part after mark
2389 ;; cut tail from buffer-undo-list temporarily by direct
2390 ;; manipulation with pointers in buffer-undo-list
2391 (setcdr tmp nil)
2393 (setq buffer-undo-list (delq nil buffer-undo-list))
2394 (setq buffer-undo-list
2395 (delq vip-buffer-undo-list-mark buffer-undo-list))
2396 ;; restore tail of buffer-undo-list
2397 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
2398 (setq buffer-undo-list (delq nil buffer-undo-list))))))
2401 (defun vip-set-complex-command-for-undo ()
2402 (if (listp buffer-undo-list)
2403 (if (not vip-undo-needs-adjustment)
2404 (let ((inhibit-quit t))
2405 (setq buffer-undo-list
2406 (cons vip-buffer-undo-list-mark buffer-undo-list))
2407 (setq vip-undo-needs-adjustment t)))))
2412 (defun vip-display-current-destructive-command ()
2413 (let ((text (nth 4 vip-d-com))
2414 (keys (nth 5 vip-d-com))
2415 (max-text-len 30))
2417 (setq this-command 'vip-display-current-destructive-command)
2419 (message " `.' runs %s%s"
2420 (concat "`" (vip-array-to-string keys) "'")
2421 (vip-abbreviate-string text max-text-len
2422 " inserting `" "'" " ......."))
2426 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
2427 ;; or in some other way (non-interactively).
2428 (defun vip-set-destructive-command (list)
2429 (or (eq vip-intermediate-command 'vip-repeat)
2430 (progn
2431 (setq vip-d-com list)
2432 (setcar (nthcdr 5 vip-d-com)
2433 (vip-array-to-string (this-command-keys)))
2434 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
2436 (defun vip-prev-destructive-command (next)
2437 "Find previous destructive command in the history of destructive commands.
2438 With prefix argument, find next destructive command."
2439 (interactive "P")
2440 (let (cmd vip-intermediate-command)
2441 (if (eq last-command 'vip-display-current-destructive-command)
2442 ;; repeated search through command history
2443 (setq vip-intermediate-command 'repeating-display-destructive-command)
2444 ;; first search through command history--set temp ring
2445 (setq vip-temp-command-ring (copy-list vip-command-ring)))
2446 (setq cmd (if next
2447 (vip-special-ring-rotate1 vip-temp-command-ring 1)
2448 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
2449 (if (null cmd)
2451 (setq vip-d-com cmd))
2452 (vip-display-current-destructive-command)))
2454 (defun vip-next-destructive-command ()
2455 "Find next destructive command in the history of destructive commands."
2456 (interactive)
2457 (vip-prev-destructive-command 'next))
2459 (defun vip-insert-prev-from-insertion-ring (arg)
2460 "Cycle through insertion ring in the direction of older insertions.
2461 Undoes previous insertion and inserts new.
2462 With prefix argument, cycles in the direction of newer elements.
2463 In minibuffer, this command executes whatever the invocation key is bound
2464 to in the global map, instead of cycling through the insertion ring."
2465 (interactive "P")
2466 (let (vip-intermediate-command)
2467 (if (eq last-command 'vip-insert-from-insertion-ring)
2468 (progn ; repeated search through insertion history
2469 (setq vip-intermediate-command 'repeating-insertion-from-ring)
2470 (if (eq vip-current-state 'replace-state)
2471 (undo 1)
2472 (if vip-last-inserted-string-from-insertion-ring
2473 (backward-delete-char
2474 (length vip-last-inserted-string-from-insertion-ring))))
2476 ;;first search through insertion history
2477 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
2478 (setq this-command 'vip-insert-from-insertion-ring)
2479 ;; so that things will be undone properly
2480 (setq buffer-undo-list (cons nil buffer-undo-list))
2481 (setq vip-last-inserted-string-from-insertion-ring
2482 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
2484 ;; this change of vip-intermediate-command must come after
2485 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
2486 ;; insertion.
2487 (setq vip-intermediate-command nil)
2488 (if vip-last-inserted-string-from-insertion-ring
2489 (insert vip-last-inserted-string-from-insertion-ring))
2492 (defun vip-insert-next-from-insertion-ring ()
2493 "Cycle through insertion ring in the direction of older insertions.
2494 Undo previous insertion and inserts new."
2495 (interactive)
2496 (vip-insert-prev-from-insertion-ring 'next))
2499 ;; some region utilities
2501 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
2502 (defun vip-add-newline-at-eob-if-necessary ()
2503 (save-excursion
2504 (end-of-line)
2505 ;; make sure all lines end with newline, unless in the minibuffer or
2506 ;; when requested otherwise (require-final-newline is nil)
2507 (if (and (eobp)
2508 (not (bolp))
2509 require-final-newline
2510 (not (vip-is-in-minibuffer))
2511 (not buffer-read-only))
2512 (insert "\n"))))
2514 (defun vip-yank-defun ()
2515 (mark-defun)
2516 (copy-region-as-kill (point) (mark t)))
2518 ;; Enlarge region between BEG and END.
2519 (defun vip-enlarge-region (beg end)
2520 (or beg (setq beg end)) ; if beg is nil, set to end
2521 (or end (setq end beg)) ; if end is nil, set to beg
2523 (if (< beg end)
2524 (progn (goto-char beg) (set-mark end))
2525 (goto-char end)
2526 (set-mark beg))
2527 (beginning-of-line)
2528 (exchange-point-and-mark)
2529 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
2530 (if (not (eobp)) (beginning-of-line))
2531 (if (> beg end) (exchange-point-and-mark)))
2534 ;; Quote region by each line with a user supplied string.
2535 (defun vip-quote-region ()
2536 (setq vip-quote-string
2537 (vip-read-string-with-history
2538 "Quote string: "
2540 'vip-quote-region-history
2541 vip-quote-string))
2542 (vip-enlarge-region (point) (mark t))
2543 (if (> (point) (mark t)) (exchange-point-and-mark))
2544 (insert vip-quote-string)
2545 (beginning-of-line)
2546 (forward-line 1)
2547 (while (and (< (point) (mark t)) (bolp))
2548 (insert vip-quote-string)
2549 (beginning-of-line)
2550 (forward-line 1)))
2552 ;; Tells whether BEG is on the same line as END.
2553 ;; If one of the args is nil, it'll return nil.
2554 (defun vip-same-line (beg end)
2555 (let ((selective-display nil))
2556 (cond ((and beg end)
2557 ;; This 'if' is needed because Emacs treats the next empty line
2558 ;; as part of the previous line.
2559 (if (or (> beg (point-max)) (> end (point-max))) ; out of range
2561 (if (and (> end beg) (= (vip-line-pos 'start) end))
2562 (setq end (min (1+ end) (point-max))))
2563 (if (and (> beg end) (= (vip-line-pos 'start) beg))
2564 (setq beg (min (1+ beg) (point-max))))
2565 (<= (count-lines beg end) 1) ))
2567 (t nil))
2571 ;; Check if the string ends with a newline.
2572 (defun vip-end-with-a-newline-p (string)
2573 (or (string= string "")
2574 (= (vip-seq-last-elt string) ?\n)))
2576 (defun vip-tmp-insert-at-eob (msg)
2577 (let ((savemax (point-max)))
2578 (goto-char savemax)
2579 (insert msg)
2580 (sit-for 2)
2581 (goto-char savemax) (delete-region (point) (point-max))
2586 ;;; Minibuffer business
2588 (defsubst vip-set-minibuffer-style ()
2589 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
2592 (defun vip-minibuffer-setup-sentinel ()
2593 (let ((hook (if vip-vi-style-in-minibuffer
2594 'vip-change-state-to-insert
2595 'vip-change-state-to-emacs)))
2596 (funcall hook)
2599 ;; Interpret last event in the local map
2600 (defun vip-exit-minibuffer ()
2601 (interactive)
2602 (let (command)
2603 (setq command (local-key-binding (char-to-string last-command-char)))
2604 (if command
2605 (command-execute command)
2606 (exit-minibuffer))))
2609 (defun vip-set-search-face ()
2610 (if (vip-has-face-support-p)
2611 (defvar vip-search-face
2612 (progn
2613 (make-face 'vip-search-face)
2614 (vip-hide-face 'vip-search-face)
2615 (or (face-differs-from-default-p 'vip-search-face)
2616 ;; face wasn't set in .vip or .Xdefaults
2617 (if (vip-can-use-colors "Black" "khaki")
2618 (progn
2619 (set-face-background 'vip-search-face "khaki")
2620 (set-face-foreground 'vip-search-face "Black"))
2621 (set-face-underline-p 'vip-search-face t)
2622 (vip-set-face-pixmap 'vip-search-face vip-search-face-pixmap)))
2623 'vip-search-face)
2624 "*Face used to flash out the search pattern.")
2628 (defun vip-set-minibuffer-faces ()
2629 (if (not (vip-has-face-support-p))
2631 (defvar vip-minibuffer-emacs-face
2632 (progn
2633 (make-face 'vip-minibuffer-emacs-face)
2634 (vip-hide-face 'vip-minibuffer-emacs-face)
2635 (or (face-differs-from-default-p 'vip-minibuffer-emacs-face)
2636 ;; face wasn't set in .vip or .Xdefaults
2637 (if vip-vi-style-in-minibuffer
2638 ;; emacs state is an exception in the minibuffer
2639 (if (vip-can-use-colors "darkseagreen2" "Black")
2640 (progn
2641 (set-face-background
2642 'vip-minibuffer-emacs-face "darkseagreen2")
2643 (set-face-foreground
2644 'vip-minibuffer-emacs-face "Black"))
2645 (copy-face 'modeline 'vip-minibuffer-emacs-face))
2646 ;; emacs state is the main state in the minibuffer
2647 (if (vip-can-use-colors "Black" "pink")
2648 (progn
2649 (set-face-background 'vip-minibuffer-emacs-face "pink")
2650 (set-face-foreground
2651 'vip-minibuffer-emacs-face "Black"))
2652 (copy-face 'italic 'vip-minibuffer-emacs-face))
2654 'vip-minibuffer-emacs-face)
2655 "Face used in the Minibuffer when it is in Emacs state.")
2657 (defvar vip-minibuffer-insert-face
2658 (progn
2659 (make-face 'vip-minibuffer-insert-face)
2660 (vip-hide-face 'vip-minibuffer-insert-face)
2661 (or (face-differs-from-default-p 'vip-minibuffer-insert-face)
2662 (if vip-vi-style-in-minibuffer
2663 (if (vip-can-use-colors "Black" "pink")
2664 (progn
2665 (set-face-background 'vip-minibuffer-insert-face "pink")
2666 (set-face-foreground
2667 'vip-minibuffer-insert-face "Black"))
2668 (copy-face 'italic 'vip-minibuffer-insert-face))
2669 ;; If Insert state is an exception
2670 (if (vip-can-use-colors "darkseagreen2" "Black")
2671 (progn
2672 (set-face-background
2673 'vip-minibuffer-insert-face "darkseagreen2")
2674 (set-face-foreground
2675 'vip-minibuffer-insert-face "Black"))
2676 (copy-face 'modeline 'vip-minibuffer-insert-face))
2677 (vip-italicize-face 'vip-minibuffer-insert-face)))
2678 'vip-minibuffer-insert-face)
2679 "Face used in the Minibuffer when it is in Insert state.")
2681 (defvar vip-minibuffer-vi-face
2682 (progn
2683 (make-face 'vip-minibuffer-vi-face)
2684 (vip-hide-face 'vip-minibuffer-vi-face)
2685 (or (face-differs-from-default-p 'vip-minibuffer-vi-face)
2686 (if vip-vi-style-in-minibuffer
2687 (if (vip-can-use-colors "Black" "grey")
2688 (progn
2689 (set-face-background 'vip-minibuffer-vi-face "grey")
2690 (set-face-foreground 'vip-minibuffer-vi-face "Black"))
2691 (copy-face 'bold 'vip-minibuffer-vi-face))
2692 (copy-face 'bold 'vip-minibuffer-vi-face)
2693 (invert-face 'vip-minibuffer-vi-face)))
2694 'vip-minibuffer-vi-face)
2695 "Face used in the Minibuffer when it is in Vi state.")
2697 ;; the current face used in the minibuffer
2698 (vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "")
2703 ;;; Reading string with history
2705 (defun vip-read-string-with-history (prompt &optional initial
2706 history-var default keymap)
2707 ;; Read string, prompting with PROMPT and inserting the INITIAL
2708 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2709 ;; input is an empty string. Use KEYMAP, if given, or the
2710 ;; minibuffer-local-map.
2711 ;; Default value is displayed until the user types something in the
2712 ;; minibuffer.
2713 (let ((minibuffer-setup-hook
2714 '(lambda ()
2715 (if (stringp initial)
2716 (progn
2717 ;; don't wait if we have unread events or in kbd macro
2718 (or unread-command-events
2719 executing-kbd-macro
2720 (sit-for 840))
2721 (erase-buffer)
2722 (insert initial)))
2723 (vip-minibuffer-setup-sentinel)))
2724 (val "")
2725 (padding "")
2726 temp-msg)
2728 (setq keymap (or keymap minibuffer-local-map)
2729 initial (or initial "")
2730 temp-msg (if default
2731 (format "(default: %s) " default)
2732 ""))
2734 (setq vip-incomplete-ex-cmd nil)
2735 (setq val (read-from-minibuffer prompt
2736 (concat temp-msg initial val padding)
2737 keymap nil history-var))
2738 (setq minibuffer-setup-hook nil
2739 padding (vip-array-to-string (this-command-keys))
2740 temp-msg "")
2741 ;; the following tries to be smart about what to put in history
2742 (if (not (string= val (car (eval history-var))))
2743 (set history-var (cons val (eval history-var))))
2744 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2745 (string= (nth 0 (eval history-var)) ""))
2746 (set history-var (cdr (eval history-var))))
2747 ;; If the user enters nothing but the prev cmd wasn't vip-ex,
2748 ;; vip-command-argument, or `! shell-command', this probably means
2749 ;; that the user typed something then erased. Return "" in this case, not
2750 ;; the default---the default is too confusing in this case.
2751 (cond ((and (string= val "")
2752 (not (string= prompt "!")) ; was a `! shell-command'
2753 (not (memq last-command
2754 '(vip-ex
2755 vip-command-argument
2759 ((string= val "") (or default ""))
2760 (t val))
2765 ;; insertion commands
2767 ;; Called when state changes from Insert Vi command mode.
2768 ;; Repeats the insertion command if Insert state was entered with prefix
2769 ;; argument > 1.
2770 (defun vip-repeat-insert-command ()
2771 (let ((i-com (car vip-d-com))
2772 (val (nth 1 vip-d-com))
2773 (char (nth 2 vip-d-com)))
2774 (if (and val (> val 1)) ; first check that val is non-nil
2775 (progn
2776 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
2777 (vip-repeat nil)
2778 (setq vip-d-com (list i-com val char nil nil nil))
2779 ))))
2781 (defun vip-insert (arg)
2782 "Insert before point."
2783 (interactive "P")
2784 (vip-set-complex-command-for-undo)
2785 (let ((val (vip-p-val arg))
2786 (com (vip-getcom arg)))
2787 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
2788 (if com
2789 (vip-loop val (vip-yank-last-insertion))
2790 (vip-change-state-to-insert))))
2792 (defun vip-append (arg)
2793 "Append after point."
2794 (interactive "P")
2795 (vip-set-complex-command-for-undo)
2796 (let ((val (vip-p-val arg))
2797 (com (vip-getcom arg)))
2798 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
2799 (if (not (eolp)) (forward-char))
2800 (if (equal com ?r)
2801 (vip-loop val (vip-yank-last-insertion))
2802 (vip-change-state-to-insert))))
2804 (defun vip-Append (arg)
2805 "Append at end of line."
2806 (interactive "P")
2807 (vip-set-complex-command-for-undo)
2808 (let ((val (vip-p-val arg))
2809 (com (vip-getcom arg)))
2810 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
2811 (end-of-line)
2812 (if (equal com ?r)
2813 (vip-loop val (vip-yank-last-insertion))
2814 (vip-change-state-to-insert))))
2816 (defun vip-Insert (arg)
2817 "Insert before first non-white."
2818 (interactive "P")
2819 (vip-set-complex-command-for-undo)
2820 (let ((val (vip-p-val arg))
2821 (com (vip-getcom arg)))
2822 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
2823 (back-to-indentation)
2824 (if (equal com ?r)
2825 (vip-loop val (vip-yank-last-insertion))
2826 (vip-change-state-to-insert))))
2828 (defun vip-open-line (arg)
2829 "Open line below."
2830 (interactive "P")
2831 (vip-set-complex-command-for-undo)
2832 (let ((val (vip-p-val arg))
2833 (com (vip-getcom arg)))
2834 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
2835 (let ((col (current-indentation)))
2836 (if (equal com ?r)
2837 (vip-loop val
2838 (progn
2839 (end-of-line)
2840 (newline 1)
2841 (if vip-auto-indent
2842 (progn
2843 (setq vip-cted t)
2844 (if vip-electric-mode
2845 (indent-according-to-mode)
2846 (indent-to col))
2848 (vip-yank-last-insertion)))
2849 (end-of-line)
2850 (newline 1)
2851 (if vip-auto-indent
2852 (progn
2853 (setq vip-cted t)
2854 (if vip-electric-mode
2855 (indent-according-to-mode)
2856 (indent-to col))
2858 (vip-change-state-to-insert)
2859 ))))
2861 (defun vip-Open-line (arg)
2862 "Open line above."
2863 (interactive "P")
2864 (vip-set-complex-command-for-undo)
2865 (let ((val (vip-p-val arg))
2866 (com (vip-getcom arg)))
2867 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
2868 (let ((col (current-indentation)))
2869 (if (equal com ?r)
2870 (vip-loop val
2871 (progn
2872 (beginning-of-line)
2873 (open-line 1)
2874 (if vip-auto-indent
2875 (progn
2876 (setq vip-cted t)
2877 (if vip-electric-mode
2878 (indent-according-to-mode)
2879 (indent-to col))
2881 (vip-yank-last-insertion)))
2882 (beginning-of-line)
2883 (open-line 1)
2884 (if vip-auto-indent
2885 (progn
2886 (setq vip-cted t)
2887 (if vip-electric-mode
2888 (indent-according-to-mode)
2889 (indent-to col))
2891 (vip-change-state-to-insert)))))
2893 (defun vip-open-line-at-point (arg)
2894 "Open line at point."
2895 (interactive "P")
2896 (vip-set-complex-command-for-undo)
2897 (let ((val (vip-p-val arg))
2898 (com (vip-getcom arg)))
2899 (vip-set-destructive-command
2900 (list 'vip-open-line-at-point val ?r nil nil nil))
2901 (if (equal com ?r)
2902 (vip-loop val
2903 (progn
2904 (open-line 1)
2905 (vip-yank-last-insertion)))
2906 (open-line 1)
2907 (vip-change-state-to-insert))))
2909 (defun vip-substitute (arg)
2910 "Substitute characters."
2911 (interactive "P")
2912 (let ((val (vip-p-val arg))
2913 (com (vip-getcom arg)))
2914 (push-mark nil t)
2915 (forward-char val)
2916 (if (equal com ?r)
2917 (vip-change-subr (mark t) (point))
2918 (vip-change (mark t) (point)))
2919 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
2922 (defun vip-substitute-line (arg)
2923 "Substitute lines."
2924 (interactive "p")
2925 (vip-set-complex-command-for-undo)
2926 (vip-line (cons arg ?C)))
2928 ;; Prepare for replace
2929 (defun vip-start-replace ()
2930 (setq vip-began-as-replace t
2931 vip-sitting-in-replace t
2932 vip-replace-chars-to-delete 0
2933 vip-replace-chars-deleted 0)
2934 (vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
2935 (vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
2936 ;; this will get added repeatedly, but no harm
2937 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
2938 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
2939 (vip-move-marker-locally 'vip-last-posn-in-replace-region
2940 (vip-replace-start))
2941 (vip-add-hook
2942 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
2943 (vip-add-hook
2944 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2948 ;; checks how many chars were deleted by the last change
2949 (defun vip-replace-mode-spy-before (beg end)
2950 (setq vip-replace-chars-deleted
2951 (- end beg
2952 (max 0 (- end (vip-replace-end)))
2953 (max 0 (- (vip-replace-start) beg))
2956 ;; Invoked as an after-change-function to set up parameters of the last change
2957 (defun vip-replace-mode-spy-after (beg end length)
2958 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
2959 (progn
2960 (setq vip-replace-chars-to-delete 0)
2961 (vip-move-marker-locally
2962 'vip-last-posn-in-replace-region (point)))
2964 (let (beg-col end-col real-end chars-to-delete)
2965 (setq real-end (min end (vip-replace-end)))
2966 (save-excursion
2967 (goto-char beg)
2968 (setq beg-col (current-column))
2969 (goto-char real-end)
2970 (setq end-col (current-column)))
2972 ;; If beg of change is outside the replacement region, then don't
2973 ;; delete anything in the repl region (set chars-to-delete to 0).
2975 ;; This works fine except that we have to take special care of
2976 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
2977 ;; sometimes simply shifts the repl region rightwards, without
2978 ;; deleting an equal amount of characters.
2980 ;; The reason why new-dabbrev.el causes this are this:
2981 ;; if one dinamically completes a partial word that starts before the
2982 ;; replacement region (but ends inside) then new-dabbrev.el first
2983 ;; moves cursor backwards, to the beginning of the word to be
2984 ;; completed (say, pt A). Then it inserts the
2985 ;; completed word and then deletes the old, incomplete part.
2986 ;; Since the complete word is inserted at position before the repl
2987 ;; region, the next If-statement would have set chars-to-delete to 0
2988 ;; unless we check for the current command, which must be
2989 ;; dabbrev-expand.
2991 ;; In fact, it might be also useful to have overlays for insert
2992 ;; regions as well, since this will let us capture the situation when
2993 ;; dabbrev-expand goes back past the insertion point to find the
2994 ;; beginning of the word to be expanded.
2995 (if (or (and (<= (vip-replace-start) beg)
2996 (<= beg (vip-replace-end)))
2997 (and (= length 0) (eq this-command 'dabbrev-expand)))
2998 (setq chars-to-delete
2999 (max (- end-col beg-col) (- real-end beg) 0))
3000 (setq chars-to-delete 0))
3002 ;; if beg = last change position, it means that we are within the
3003 ;; same command that does multiple changes. Moreover, it means
3004 ;; that we have two subsequent changes (insert/delete) that
3005 ;; complement each other.
3006 (if (= beg (marker-position vip-last-posn-in-replace-region))
3007 (setq vip-replace-chars-to-delete
3008 (- (+ chars-to-delete vip-replace-chars-to-delete)
3009 vip-replace-chars-deleted))
3010 (setq vip-replace-chars-to-delete chars-to-delete))
3012 (vip-move-marker-locally
3013 'vip-last-posn-in-replace-region
3014 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
3015 (or (marker-position vip-last-posn-in-replace-region)
3016 (vip-replace-start))
3019 (setq vip-replace-chars-to-delete
3020 (max 0 (min vip-replace-chars-to-delete
3021 (- (vip-replace-end)
3022 vip-last-posn-in-replace-region))))
3026 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
3027 ;; posn is within the overlay.
3028 (defun vip-finish-change (posn)
3029 (vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
3030 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
3031 (vip-remove-hook 'vip-post-command-hooks
3032 'vip-replace-state-post-command-sentinel)
3033 (vip-remove-hook
3034 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
3035 (vip-restore-cursor-color)
3036 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
3037 (save-excursion
3038 (if (and
3039 vip-replace-overlay
3040 (>= posn (vip-replace-start))
3041 (< posn (vip-replace-end)))
3042 (delete-region posn (vip-replace-end)))
3045 (if (eq vip-current-state 'replace-state)
3046 (vip-downgrade-to-insert))
3047 ;; replace mode ended => nullify vip-last-posn-in-replace-region
3048 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
3049 (vip-hide-replace-overlay)
3050 (vip-refresh-mode-line)
3051 (vip-put-string-on-kill-ring vip-last-replace-region)
3054 ;; Make STRING be the first element of the kill ring.
3055 (defun vip-put-string-on-kill-ring (string)
3056 (setq kill-ring (cons string kill-ring))
3057 (if (> (length kill-ring) kill-ring-max)
3058 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
3059 (setq kill-ring-yank-pointer kill-ring))
3061 (defun vip-finish-R-mode ()
3062 (vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
3063 (vip-remove-hook
3064 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
3065 (vip-downgrade-to-insert))
3067 (defun vip-start-R-mode ()
3068 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
3069 (overwrite-mode 1)
3070 (vip-add-hook
3071 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
3072 (vip-add-hook
3073 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
3078 (defun vip-replace-state-exit-cmd ()
3079 "Binding for keys that cause Replace state to switch to Vi or to Insert.
3080 These keys are ESC, RET, and LineFeed"
3081 (interactive)
3082 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
3083 (vip-finish-R-mode)
3084 (vip-finish-change vip-last-posn-in-replace-region))
3085 (let (com)
3086 (if (eq this-command 'vip-intercept-ESC-key)
3087 (setq com 'vip-exit-insert-state)
3088 (vip-set-unread-command-events last-input-char)
3089 (setq com (key-binding (read-key-sequence nil))))
3091 (condition-case conds
3092 (command-execute com)
3093 (error
3094 (vip-message-conditions conds)))
3096 (vip-hide-replace-overlay))
3099 ;; This is the function bound to 'R'---unlimited replace.
3100 ;; Similar to Emacs's own overwrite-mode.
3101 (defun vip-overwrite (arg)
3102 "Begin overwrite mode."
3103 (interactive "P")
3104 (let ((val (vip-p-val arg))
3105 (com (vip-getcom arg)) (len))
3106 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
3107 (if com
3108 (progn
3109 ;; Viper saves inserted text in vip-last-insertion
3110 (setq len (length vip-last-insertion))
3111 (delete-char len)
3112 (vip-loop val (vip-yank-last-insertion)))
3113 (setq last-command 'vip-overwrite)
3114 (vip-set-complex-command-for-undo)
3115 (vip-set-replace-overlay (point) (vip-line-pos 'end))
3116 (vip-change-state-to-replace)
3120 ;; line commands
3122 (defun vip-line (arg)
3123 (let ((val (car arg))
3124 (com (cdr arg)))
3125 (vip-move-marker-locally 'vip-com-point (point))
3126 (if (not (eobp))
3127 (vip-next-line-carefully (1- val)))
3128 ;; this ensures that dd, cc, D, yy will do the right thing on the last
3129 ;; line of buffer when this line has no \n.
3130 (vip-add-newline-at-eob-if-necessary)
3131 (vip-execute-com 'vip-line val com))
3132 (if (and (eobp) (not (bobp))) (forward-line -1))
3135 (defun vip-yank-line (arg)
3136 "Yank ARG lines (in Vi's sense)."
3137 (interactive "P")
3138 (let ((val (vip-p-val arg)))
3139 (vip-line (cons val ?Y))))
3142 ;; region commands
3144 (defun vip-region (arg)
3145 "Execute command on a region."
3146 (interactive "P")
3147 (let ((val (vip-P-val arg))
3148 (com (vip-getcom arg)))
3149 (vip-move-marker-locally 'vip-com-point (point))
3150 (exchange-point-and-mark)
3151 (vip-execute-com 'vip-region val com)))
3153 (defun vip-Region (arg)
3154 "Execute command on a Region."
3155 (interactive "P")
3156 (let ((val (vip-P-val arg))
3157 (com (vip-getCom arg)))
3158 (vip-move-marker-locally 'vip-com-point (point))
3159 (exchange-point-and-mark)
3160 (vip-execute-com 'vip-Region val com)))
3162 (defun vip-replace-char (arg)
3163 "Replace the following ARG chars by the character read."
3164 (interactive "P")
3165 (if (and (eolp) (bolp)) (error "No character to replace here"))
3166 (let ((val (vip-p-val arg))
3167 (com (vip-getcom arg)))
3168 (vip-replace-char-subr com val)
3169 (if (and (eolp) (not (bolp))) (forward-char 1))
3170 (vip-set-destructive-command
3171 (list 'vip-replace-char val ?r nil vip-d-char nil))
3174 (defun vip-replace-char-subr (com arg)
3175 (let ((take-care-of-iso-accents
3176 (and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
3177 char)
3178 (setq char (if (equal com ?r)
3179 vip-d-char
3180 (read-char)))
3181 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
3182 ;; get European characters
3183 (progn
3184 (iso-accents-mode 1)
3185 (vip-set-unread-command-events char)
3186 (setq char (aref (read-key-sequence nil) 0))
3187 (iso-accents-mode -1)))
3188 (delete-char arg t)
3189 (setq vip-d-char char)
3190 (vip-loop (if (> arg 0) arg (- arg))
3191 (if (eq char ?\C-m) (insert "\n") (insert char)))
3192 (backward-char arg)))
3195 ;; basic cursor movement. j, k, l, h commands.
3197 (defun vip-forward-char (arg)
3198 "Move point right ARG characters (left if ARG negative).
3199 On reaching end of line, stop and signal error."
3200 (interactive "P")
3201 (vip-leave-region-active)
3202 (let ((val (vip-p-val arg))
3203 (com (vip-getcom arg)))
3204 (if com (vip-move-marker-locally 'vip-com-point (point)))
3205 (if vip-ex-style-motion
3206 (progn
3207 ;; the boundary condition check gets weird here because
3208 ;; forward-char may be the parameter of a delete, and 'dl' works
3209 ;; just like 'x' for the last char on a line, so we have to allow
3210 ;; the forward motion before the 'vip-execute-com', but, of
3211 ;; course, 'dl' doesn't work on an empty line, so we have to
3212 ;; catch that condition before 'vip-execute-com'
3213 (if (and (eolp) (bolp)) (error "") (forward-char val))
3214 (if com (vip-execute-com 'vip-forward-char val com))
3215 (if (eolp) (progn (backward-char 1) (error ""))))
3216 (forward-char val)
3217 (if com (vip-execute-com 'vip-forward-char val com)))))
3219 (defun vip-backward-char (arg)
3220 "Move point left ARG characters (right if ARG negative).
3221 On reaching beginning of line, stop and signal error."
3222 (interactive "P")
3223 (vip-leave-region-active)
3224 (let ((val (vip-p-val arg))
3225 (com (vip-getcom arg)))
3226 (if com (vip-move-marker-locally 'vip-com-point (point)))
3227 (if vip-ex-style-motion
3228 (progn
3229 (if (bolp) (error "") (backward-char val))
3230 (if com (vip-execute-com 'vip-backward-char val com)))
3231 (backward-char val)
3232 (if com (vip-execute-com 'vip-backward-char val com)))))
3234 ;; Like forward-char, but doesn't move at end of buffer.
3235 (defun vip-forward-char-carefully (&optional arg)
3236 (setq arg (or arg 1))
3237 (if (>= (point-max) (+ (point) arg))
3238 (forward-char arg)
3239 (goto-char (point-max))))
3241 ;; Like backward-char, but doesn't move at end of buffer.
3242 (defun vip-backward-char-carefully (&optional arg)
3243 (setq arg (or arg 1))
3244 (if (<= (point-min) (- (point) arg))
3245 (backward-char arg)
3246 (goto-char (point-min))))
3248 (defun vip-next-line-carefully (arg)
3249 (condition-case nil
3250 (next-line arg)
3251 (error nil)))
3255 ;;; Word command
3257 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
3258 ;; for word movement. When executed with a destructive command, \n is
3259 ;; usually left untouched for the last word.
3260 ;; Viper uses syntax table to determine what is a word and what is a
3261 ;; separator. However, \n is always a separator. Also, if vip-syntax-preference
3262 ;; is 'vi, then `_' is part of the word.
3264 ;; skip only one \n
3265 (defun vip-skip-separators (forward)
3266 (if forward
3267 (progn
3268 (vip-skip-all-separators-forward 'within-line)
3269 (if (looking-at "\n")
3270 (progn
3271 (forward-char)
3272 (vip-skip-all-separators-forward 'within-line))))
3273 (vip-skip-all-separators-backward 'within-line)
3274 (backward-char)
3275 (if (looking-at "\n")
3276 (vip-skip-all-separators-backward 'within-line)
3277 (forward-char))))
3279 (defun vip-forward-word-kernel (val)
3280 (while (> val 0)
3281 (cond ((vip-looking-at-alpha)
3282 (vip-skip-alpha-forward "_")
3283 (vip-skip-separators t))
3284 ((vip-looking-at-separator)
3285 (vip-skip-separators t))
3286 ((not (vip-looking-at-alphasep))
3287 (vip-skip-nonalphasep-forward)
3288 (vip-skip-separators t)))
3289 (setq val (1- val))))
3291 ;; first search backward for pat. Then skip chars backwards using aux-pat
3292 (defun vip-fwd-skip (pat aux-pat lim)
3293 (if (and (save-excursion
3294 (re-search-backward pat lim t))
3295 (= (point) (match-end 0)))
3296 (goto-char (match-beginning 0)))
3297 (skip-chars-backward aux-pat lim)
3298 (if (= (point) lim)
3299 (vip-forward-char-carefully))
3303 (defun vip-forward-word (arg)
3304 "Forward word."
3305 (interactive "P")
3306 (vip-leave-region-active)
3307 (let ((val (vip-p-val arg))
3308 (com (vip-getcom arg)))
3309 (if com (vip-move-marker-locally 'vip-com-point (point)))
3310 (vip-forward-word-kernel val)
3311 (if com (progn
3312 (cond ((memq com (list ?c (- ?c)))
3313 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3314 ;; Yank words including the whitespace, but not newline
3315 ((memq com (list ?y (- ?y)))
3316 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3317 ((vip-dotable-command-p com)
3318 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3319 (vip-execute-com 'vip-forward-word val com)))))
3322 (defun vip-forward-Word (arg)
3323 "Forward word delimited by white characters."
3324 (interactive "P")
3325 (vip-leave-region-active)
3326 (let ((val (vip-p-val arg))
3327 (com (vip-getcom arg)))
3328 (if com (vip-move-marker-locally 'vip-com-point (point)))
3329 (vip-loop val
3330 (progn
3331 (vip-skip-nonseparators 'forward)
3332 (vip-skip-separators t)))
3333 (if com (progn
3334 (cond ((memq com (list ?c (- ?c)))
3335 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3336 ;; Yank words including the whitespace, but not newline
3337 ((memq com (list ?y (- ?y)))
3338 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
3339 ((vip-dotable-command-p com)
3340 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3341 (vip-execute-com 'vip-forward-Word val com)))))
3344 ;; this is a bit different from Vi, but Vi's end of word
3345 ;; makes no sense whatsoever
3346 (defun vip-end-of-word-kernel ()
3347 (if (vip-end-of-word-p) (forward-char))
3348 (if (vip-looking-at-separator)
3349 (vip-skip-all-separators-forward))
3351 (cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
3352 ((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
3353 (vip-backward-char-carefully))
3355 (defun vip-end-of-word-p ()
3356 (or (eobp)
3357 (save-excursion
3358 (cond ((vip-looking-at-alpha)
3359 (forward-char)
3360 (not (vip-looking-at-alpha)))
3361 ((not (vip-looking-at-alphasep))
3362 (forward-char)
3363 (vip-looking-at-alphasep))))))
3366 (defun vip-end-of-word (arg &optional careful)
3367 "Move point to end of current word."
3368 (interactive "P")
3369 (vip-leave-region-active)
3370 (let ((val (vip-p-val arg))
3371 (com (vip-getcom arg)))
3372 (if com (vip-move-marker-locally 'vip-com-point (point)))
3373 (vip-loop val (vip-end-of-word-kernel))
3374 (if com
3375 (progn
3376 (forward-char)
3377 (vip-execute-com 'vip-end-of-word val com)))))
3379 (defun vip-end-of-Word (arg)
3380 "Forward to end of word delimited by white character."
3381 (interactive "P")
3382 (vip-leave-region-active)
3383 (let ((val (vip-p-val arg))
3384 (com (vip-getcom arg)))
3385 (if com (vip-move-marker-locally 'vip-com-point (point)))
3386 (vip-loop val
3387 (progn
3388 (vip-end-of-word-kernel)
3389 (vip-skip-nonseparators 'forward)
3390 (backward-char)))
3391 (if com
3392 (progn
3393 (forward-char)
3394 (vip-execute-com 'vip-end-of-Word val com)))))
3396 (defun vip-backward-word-kernel (val)
3397 (while (> val 0)
3398 (backward-char)
3399 (cond ((vip-looking-at-alpha)
3400 (vip-skip-alpha-backward "_"))
3401 ((vip-looking-at-separator)
3402 (forward-char)
3403 (vip-skip-separators nil)
3404 (backward-char)
3405 (cond ((vip-looking-at-alpha)
3406 (vip-skip-alpha-backward "_"))
3407 ((not (vip-looking-at-alphasep))
3408 (vip-skip-nonalphasep-backward))
3409 (t (forward-char))))
3410 ((not (vip-looking-at-alphasep))
3411 (vip-skip-nonalphasep-backward)))
3412 (setq val (1- val))))
3414 (defun vip-backward-word (arg)
3415 "Backward word."
3416 (interactive "P")
3417 (vip-leave-region-active)
3418 (let ((val (vip-p-val arg))
3419 (com (vip-getcom arg)))
3420 (if com
3421 (let (i)
3422 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3423 (backward-char))
3424 (vip-move-marker-locally 'vip-com-point (point))
3425 (if i (forward-char))))
3426 (vip-backward-word-kernel val)
3427 (if com (vip-execute-com 'vip-backward-word val com))))
3429 (defun vip-backward-Word (arg)
3430 "Backward word delimited by white character."
3431 (interactive "P")
3432 (vip-leave-region-active)
3433 (let ((val (vip-p-val arg))
3434 (com (vip-getcom arg)))
3435 (if com
3436 (let (i)
3437 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3438 (backward-char))
3439 (vip-move-marker-locally 'vip-com-point (point))
3440 (if i (forward-char))))
3441 (vip-loop val
3442 (progn
3443 (vip-skip-separators nil)
3444 (vip-skip-nonseparators 'backward)))
3445 (if com (vip-execute-com 'vip-backward-Word val com))))
3449 ;; line commands
3451 (defun vip-beginning-of-line (arg)
3452 "Go to beginning of line."
3453 (interactive "P")
3454 (vip-leave-region-active)
3455 (let ((val (vip-p-val arg))
3456 (com (vip-getcom arg)))
3457 (if com (vip-move-marker-locally 'vip-com-point (point)))
3458 (beginning-of-line val)
3459 (if com (vip-execute-com 'vip-beginning-of-line val com))))
3461 (defun vip-bol-and-skip-white (arg)
3462 "Beginning of line at first non-white character."
3463 (interactive "P")
3464 (vip-leave-region-active)
3465 (let ((val (vip-p-val arg))
3466 (com (vip-getcom arg)))
3467 (if com (vip-move-marker-locally 'vip-com-point (point)))
3468 (forward-to-indentation (1- val))
3469 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
3471 (defun vip-goto-eol (arg)
3472 "Go to end of line."
3473 (interactive "P")
3474 (vip-leave-region-active)
3475 (let ((val (vip-p-val arg))
3476 (com (vip-getcom arg)))
3477 (if com (vip-move-marker-locally 'vip-com-point (point)))
3478 (end-of-line val)
3479 (if com (vip-execute-com 'vip-goto-eol val com))
3480 (if vip-ex-style-motion
3481 (if (and (eolp) (not (bolp))
3482 ;; a fix for vip-change-to-eol
3483 (not (equal vip-current-state 'insert-state)))
3484 (backward-char 1)
3485 ))))
3488 (defun vip-goto-col (arg)
3489 "Go to ARG's column."
3490 (interactive "P")
3491 (vip-leave-region-active)
3492 (let ((val (vip-p-val arg))
3493 (com (vip-getcom arg)))
3494 (save-excursion
3495 (end-of-line)
3496 (if (> val (1+ (current-column))) (error "")))
3497 (if com (vip-move-marker-locally 'vip-com-point (point)))
3498 (beginning-of-line)
3499 (forward-char (1- val))
3500 (if com (vip-execute-com 'vip-goto-col val com))))
3503 (defun vip-next-line (arg)
3504 "Go to next line."
3505 (interactive "P")
3506 (vip-leave-region-active)
3507 (let ((val (vip-p-val arg))
3508 (com (vip-getCom arg)))
3509 (if com (vip-move-marker-locally 'vip-com-point (point)))
3510 (next-line val)
3511 (if vip-ex-style-motion
3512 (if (and (eolp) (not (bolp))) (backward-char 1)))
3513 (setq this-command 'next-line)
3514 (if com (vip-execute-com 'vip-next-line val com))))
3516 (defun vip-next-line-at-bol (arg)
3517 "Next line at beginning of line."
3518 (interactive "P")
3519 (vip-leave-region-active)
3520 (save-excursion
3521 (end-of-line)
3522 (if (eobp) (error "Last line in buffer")))
3523 (let ((val (vip-p-val arg))
3524 (com (vip-getCom arg)))
3525 (if com (vip-move-marker-locally 'vip-com-point (point)))
3526 (forward-line val)
3527 (back-to-indentation)
3528 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
3530 (defun vip-previous-line (arg)
3531 "Go to previous line."
3532 (interactive "P")
3533 (vip-leave-region-active)
3534 (let ((val (vip-p-val arg))
3535 (com (vip-getCom arg)))
3536 (if com (vip-move-marker-locally 'vip-com-point (point)))
3537 (previous-line val)
3538 (if vip-ex-style-motion
3539 (if (and (eolp) (not (bolp))) (backward-char 1)))
3540 (setq this-command 'previous-line)
3541 (if com (vip-execute-com 'vip-previous-line val com))))
3544 (defun vip-previous-line-at-bol (arg)
3545 "Previous line at beginning of line."
3546 (interactive "P")
3547 (vip-leave-region-active)
3548 (save-excursion
3549 (beginning-of-line)
3550 (if (bobp) (error "First line in buffer")))
3551 (let ((val (vip-p-val arg))
3552 (com (vip-getCom arg)))
3553 (if com (vip-move-marker-locally 'vip-com-point (point)))
3554 (forward-line (- val))
3555 (back-to-indentation)
3556 (if com (vip-execute-com 'vip-previous-line val com))))
3558 (defun vip-change-to-eol (arg)
3559 "Change to end of line."
3560 (interactive "P")
3561 (vip-goto-eol (cons arg ?c)))
3563 (defun vip-kill-line (arg)
3564 "Delete line."
3565 (interactive "P")
3566 (vip-goto-eol (cons arg ?d)))
3568 (defun vip-erase-line (arg)
3569 "Erase line."
3570 (interactive "P")
3571 (vip-beginning-of-line (cons arg ?d)))
3574 ;;; Moving around
3576 (defun vip-goto-line (arg)
3577 "Go to ARG's line. Without ARG go to end of buffer."
3578 (interactive "P")
3579 (let ((val (vip-P-val arg))
3580 (com (vip-getCom arg)))
3581 (vip-move-marker-locally 'vip-com-point (point))
3582 (vip-deactivate-mark)
3583 (push-mark nil t)
3584 (if (null val)
3585 (goto-char (point-max))
3586 (goto-char (point-min))
3587 (forward-line (1- val)))
3589 ;; positioning is done twice: before and after command execution
3590 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3591 (back-to-indentation)
3593 (if com (vip-execute-com 'vip-goto-line val com))
3595 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3596 (back-to-indentation)
3599 ;; Find ARG's occurrence of CHAR on the current line.
3600 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
3601 ;; adjust point after search.
3602 (defun vip-find-char (arg char forward offset)
3603 (or (char-or-string-p char) (error ""))
3604 (let ((arg (if forward arg (- arg)))
3605 (cmd (if (eq vip-intermediate-command 'vip-repeat)
3606 (nth 5 vip-d-com)
3607 (vip-array-to-string (this-command-keys))))
3608 point)
3609 (save-excursion
3610 (save-restriction
3611 (if (> arg 0)
3612 (narrow-to-region
3613 ;; forward search begins here
3614 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
3615 ;; forward search ends here
3616 (progn (end-of-line) (point)))
3617 (narrow-to-region
3618 ;; backward search begins from here
3619 (if (bolp)
3620 (error "Command `%s': At beginning of line" cmd) (point))
3621 ;; backward search ends here
3622 (progn (beginning-of-line) (point))))
3623 ;; if arg > 0, point is forwarded before search.
3624 (if (> arg 0) (goto-char (1+ (point-min)))
3625 (goto-char (point-max)))
3626 (if (let ((case-fold-search nil))
3627 (search-forward (char-to-string char) nil 0 arg))
3628 (setq point (point))
3629 (error "Command `%s': `%c' not found" cmd char))))
3630 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
3632 (defun vip-find-char-forward (arg)
3633 "Find char on the line.
3634 If called interactively read the char to find from the terminal, and if
3635 called from vip-repeat, the char last used is used. This behaviour is
3636 controlled by the sign of prefix numeric value."
3637 (interactive "P")
3638 (let ((val (vip-p-val arg))
3639 (com (vip-getcom arg))
3640 (cmd-representation (nth 5 vip-d-com)))
3641 (if (> val 0)
3642 ;; this means that the function was called interactively
3643 (setq vip-f-char (read-char)
3644 vip-f-forward t
3645 vip-f-offset nil)
3646 ;; vip-repeat --- set vip-F-char from command-keys
3647 (setq vip-F-char (if (stringp cmd-representation)
3648 (vip-seq-last-elt cmd-representation)
3649 vip-F-char)
3650 vip-f-char vip-F-char)
3651 (setq val (- val)))
3652 (if com (vip-move-marker-locally 'vip-com-point (point)))
3653 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
3654 (setq val (- val))
3655 (if com
3656 (progn
3657 (setq vip-F-char vip-f-char) ; set new vip-F-char
3658 (forward-char)
3659 (vip-execute-com 'vip-find-char-forward val com)))))
3661 (defun vip-goto-char-forward (arg)
3662 "Go up to char ARG forward on line."
3663 (interactive "P")
3664 (let ((val (vip-p-val arg))
3665 (com (vip-getcom arg))
3666 (cmd-representation (nth 5 vip-d-com)))
3667 (if (> val 0)
3668 ;; this means that the function was called interactively
3669 (setq vip-f-char (read-char)
3670 vip-f-forward t
3671 vip-f-offset t)
3672 ;; vip-repeat --- set vip-F-char from command-keys
3673 (setq vip-F-char (if (stringp cmd-representation)
3674 (vip-seq-last-elt cmd-representation)
3675 vip-F-char)
3676 vip-f-char vip-F-char)
3677 (setq val (- val)))
3678 (if com (vip-move-marker-locally 'vip-com-point (point)))
3679 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
3680 (setq val (- val))
3681 (if com
3682 (progn
3683 (setq vip-F-char vip-f-char) ; set new vip-F-char
3684 (forward-char)
3685 (vip-execute-com 'vip-goto-char-forward val com)))))
3687 (defun vip-find-char-backward (arg)
3688 "Find char ARG on line backward."
3689 (interactive "P")
3690 (let ((val (vip-p-val arg))
3691 (com (vip-getcom arg))
3692 (cmd-representation (nth 5 vip-d-com)))
3693 (if (> val 0)
3694 ;; this means that the function was called interactively
3695 (setq vip-f-char (read-char)
3696 vip-f-forward nil
3697 vip-f-offset nil)
3698 ;; vip-repeat --- set vip-F-char from command-keys
3699 (setq vip-F-char (if (stringp cmd-representation)
3700 (vip-seq-last-elt cmd-representation)
3701 vip-F-char)
3702 vip-f-char vip-F-char)
3703 (setq val (- val)))
3704 (if com (vip-move-marker-locally 'vip-com-point (point)))
3705 (vip-find-char
3706 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
3707 (setq val (- val))
3708 (if com
3709 (progn
3710 (setq vip-F-char vip-f-char) ; set new vip-F-char
3711 (vip-execute-com 'vip-find-char-backward val com)))))
3713 (defun vip-goto-char-backward (arg)
3714 "Go up to char ARG backward on line."
3715 (interactive "P")
3716 (let ((val (vip-p-val arg))
3717 (com (vip-getcom arg))
3718 (cmd-representation (nth 5 vip-d-com)))
3719 (if (> val 0)
3720 ;; this means that the function was called interactively
3721 (setq vip-f-char (read-char)
3722 vip-f-forward nil
3723 vip-f-offset t)
3724 ;; vip-repeat --- set vip-F-char from command-keys
3725 (setq vip-F-char (if (stringp cmd-representation)
3726 (vip-seq-last-elt cmd-representation)
3727 vip-F-char)
3728 vip-f-char vip-F-char)
3729 (setq val (- val)))
3730 (if com (vip-move-marker-locally 'vip-com-point (point)))
3731 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
3732 (setq val (- val))
3733 (if com
3734 (progn
3735 (setq vip-F-char vip-f-char) ; set new vip-F-char
3736 (vip-execute-com 'vip-goto-char-backward val com)))))
3738 (defun vip-repeat-find (arg)
3739 "Repeat previous find command."
3740 (interactive "P")
3741 (let ((val (vip-p-val arg))
3742 (com (vip-getcom arg)))
3743 (vip-deactivate-mark)
3744 (if com (vip-move-marker-locally 'vip-com-point (point)))
3745 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
3746 (if com
3747 (progn
3748 (if vip-f-forward (forward-char))
3749 (vip-execute-com 'vip-repeat-find val com)))))
3751 (defun vip-repeat-find-opposite (arg)
3752 "Repeat previous find command in the opposite direction."
3753 (interactive "P")
3754 (let ((val (vip-p-val arg))
3755 (com (vip-getcom arg)))
3756 (vip-deactivate-mark)
3757 (if com (vip-move-marker-locally 'vip-com-point (point)))
3758 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
3759 (if com
3760 (progn
3761 (if vip-f-forward (forward-char))
3762 (vip-execute-com 'vip-repeat-find-opposite val com)))))
3765 ;; window scrolling etc.
3767 (defun vip-other-window (arg)
3768 "Switch to other window."
3769 (interactive "p")
3770 (other-window arg)
3771 (or (not (eq vip-current-state 'emacs-state))
3772 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
3773 (vip-change-state-to-vi)))
3775 (defun vip-window-top (arg)
3776 "Go to home window line."
3777 (interactive "P")
3778 (let ((val (vip-p-val arg))
3779 (com (vip-getCom arg)))
3780 (if com (vip-move-marker-locally 'vip-com-point (point)))
3781 (push-mark nil t)
3782 (move-to-window-line (1- val))
3784 ;; positioning is done twice: before and after command execution
3785 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3786 (back-to-indentation)
3788 (if com (vip-execute-com 'vip-window-top val com))
3790 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3791 (back-to-indentation)
3794 (defun vip-window-middle (arg)
3795 "Go to middle window line."
3796 (interactive "P")
3797 (let ((val (vip-p-val arg))
3798 (com (vip-getCom arg))
3799 lines)
3800 (if com (vip-move-marker-locally 'vip-com-point (point)))
3801 (push-mark nil t)
3802 (if (not (pos-visible-in-window-p (point-max)))
3803 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3804 (setq lines (count-lines (window-start) (point-max)))
3805 (move-to-window-line (+ (/ lines 2) (1- val))))
3807 ;; positioning is done twice: before and after command execution
3808 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3809 (back-to-indentation)
3811 (if com (vip-execute-com 'vip-window-middle val com))
3813 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3814 (back-to-indentation)
3817 (defun vip-window-bottom (arg)
3818 "Go to last window line."
3819 (interactive "P")
3820 (let ((val (vip-p-val arg))
3821 (com (vip-getCom arg)))
3822 (if com (vip-move-marker-locally 'vip-com-point (point)))
3823 (push-mark nil t)
3824 (move-to-window-line (- val))
3826 ;; positioning is done twice: before and after command execution
3827 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3828 (back-to-indentation)
3830 (if com (vip-execute-com 'vip-window-bottom val com))
3832 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3833 (back-to-indentation)
3836 (defun vip-line-to-top (arg)
3837 "Put current line on the home line."
3838 (interactive "p")
3839 (recenter (1- arg)))
3841 (defun vip-line-to-middle (arg)
3842 "Put current line on the middle line."
3843 (interactive "p")
3844 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3846 (defun vip-line-to-bottom (arg)
3847 "Put current line on the last line."
3848 (interactive "p")
3849 (recenter (- (window-height) (1+ arg))))
3852 ;; paren match
3853 ;; must correct this to only match ( to ) etc. On the other hand
3854 ;; it is good that paren match gets confused, because that way you
3855 ;; catch _all_ imbalances.
3857 (defun vip-paren-match (arg)
3858 "Go to the matching parenthesis."
3859 (interactive "P")
3860 (let ((com (vip-getcom arg))
3861 anchor-point)
3862 (if (integerp arg)
3863 (if (or (> arg 99) (< arg 1))
3864 (error "Prefix must be between 1 and 99")
3865 (goto-char
3866 (if (> (point-max) 80000)
3867 (* (/ (point-max) 100) arg)
3868 (/ (* (point-max) arg) 100)))
3869 (back-to-indentation))
3870 (let (beg-lim end-lim)
3871 (if (and (eolp) (not (bolp))) (forward-char -1))
3872 (if (not (looking-at "[][(){}]"))
3873 (setq anchor-point (point)))
3874 (save-excursion
3875 (beginning-of-line)
3876 (setq beg-lim (point))
3877 (end-of-line)
3878 (setq end-lim (point)))
3879 (cond ((re-search-forward "[][(){}]" end-lim t)
3880 (backward-char) )
3881 ((re-search-backward "[][(){}]" beg-lim t))
3883 (error "No matching character on line"))))
3884 (cond ((looking-at "[\(\[{]")
3885 (if com (vip-move-marker-locally 'vip-com-point (point)))
3886 (forward-sexp 1)
3887 (if com
3888 (vip-execute-com 'vip-paren-match nil com)
3889 (backward-char)))
3890 (anchor-point
3891 (if com
3892 (progn
3893 (vip-move-marker-locally 'vip-com-point anchor-point)
3894 (forward-char 1)
3895 (vip-execute-com 'vip-paren-match nil com)
3897 ((looking-at "[])}]")
3898 (forward-char)
3899 (if com (vip-move-marker-locally 'vip-com-point (point)))
3900 (backward-sexp 1)
3901 (if com (vip-execute-com 'vip-paren-match nil com)))
3902 (t (error ""))))))
3905 ;; sentence ,paragraph and heading
3907 (defun vip-forward-sentence (arg)
3908 "Forward sentence."
3909 (interactive "P")
3910 (push-mark nil t)
3911 (let ((val (vip-p-val arg))
3912 (com (vip-getcom arg)))
3913 (if com (vip-move-marker-locally 'vip-com-point (point)))
3914 (forward-sentence val)
3915 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3917 (defun vip-backward-sentence (arg)
3918 "Backward sentence."
3919 (interactive "P")
3920 (push-mark nil t)
3921 (let ((val (vip-p-val arg))
3922 (com (vip-getcom arg)))
3923 (if com (vip-move-marker-locally 'vip-com-point (point)))
3924 (backward-sentence val)
3925 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3927 (defun vip-forward-paragraph (arg)
3928 "Forward paragraph."
3929 (interactive "P")
3930 (push-mark nil t)
3931 (let ((val (vip-p-val arg))
3932 (com (vip-getCom arg)))
3933 (if com (vip-move-marker-locally 'vip-com-point (point)))
3934 (forward-paragraph val)
3935 (if com
3936 (progn
3937 (backward-char 1)
3938 (vip-execute-com 'vip-forward-paragraph nil com)))))
3940 (defun vip-backward-paragraph (arg)
3941 "Backward paragraph."
3942 (interactive "P")
3943 (push-mark nil t)
3944 (let ((val (vip-p-val arg))
3945 (com (vip-getCom arg)))
3946 (if com (vip-move-marker-locally 'vip-com-point (point)))
3947 (backward-paragraph val)
3948 (if com
3949 (progn
3950 (forward-char 1)
3951 (vip-execute-com 'vip-backward-paragraph nil com)
3952 (backward-char 1)))))
3954 ;; should be mode-specific etc.
3956 (defun vip-prev-heading (arg)
3957 (interactive "P")
3958 (let ((val (vip-p-val arg))
3959 (com (vip-getCom arg)))
3960 (if com (vip-move-marker-locally 'vip-com-point (point)))
3961 (re-search-backward vip-heading-start nil t val)
3962 (goto-char (match-beginning 0))
3963 (if com (vip-execute-com 'vip-prev-heading nil com))))
3965 (defun vip-heading-end (arg)
3966 (interactive "P")
3967 (let ((val (vip-p-val arg))
3968 (com (vip-getCom arg)))
3969 (if com (vip-move-marker-locally 'vip-com-point (point)))
3970 (re-search-forward vip-heading-end nil t val)
3971 (goto-char (match-beginning 0))
3972 (if com (vip-execute-com 'vip-heading-end nil com))))
3974 (defun vip-next-heading (arg)
3975 (interactive "P")
3976 (let ((val (vip-p-val arg))
3977 (com (vip-getCom arg)))
3978 (if com (vip-move-marker-locally 'vip-com-point (point)))
3979 (end-of-line)
3980 (re-search-forward vip-heading-start nil t val)
3981 (goto-char (match-beginning 0))
3982 (if com (vip-execute-com 'vip-next-heading nil com))))
3985 ;; scrolling
3987 (setq scroll-step 1)
3989 (defun vip-scroll-screen (arg)
3990 "Scroll to next screen."
3991 (interactive "p")
3992 (condition-case nil
3993 (if (> arg 0)
3994 (while (> arg 0)
3995 (scroll-up)
3996 (setq arg (1- arg)))
3997 (while (> 0 arg)
3998 (scroll-down)
3999 (setq arg (1+ arg))))
4000 (error (beep 1)
4001 (if (> arg 0)
4002 (progn
4003 (message "End of buffer")
4004 (goto-char (point-max)))
4005 (message "Beginning of buffer")
4006 (goto-char (point-min))))
4009 (defun vip-scroll-screen-back (arg)
4010 "Scroll to previous screen."
4011 (interactive "p")
4012 (vip-scroll-screen (- arg)))
4014 (defun vip-scroll-down (arg)
4015 "Pull down half screen."
4016 (interactive "P")
4017 (condition-case nil
4018 (if (null arg)
4019 (scroll-down (/ (window-height) 2))
4020 (scroll-down arg))
4021 (error (beep 1)
4022 (message "Beginning of buffer")
4023 (goto-char (point-min)))))
4025 (defun vip-scroll-down-one (arg)
4026 "Scroll up one line."
4027 (interactive "p")
4028 (scroll-down arg))
4030 (defun vip-scroll-up (arg)
4031 "Pull up half screen."
4032 (interactive "P")
4033 (condition-case nil
4034 (if (null arg)
4035 (scroll-up (/ (window-height) 2))
4036 (scroll-up arg))
4037 (error (beep 1)
4038 (message "End of buffer")
4039 (goto-char (point-max)))))
4041 (defun vip-scroll-up-one (arg)
4042 "Scroll down one line."
4043 (interactive "p")
4044 (scroll-up arg))
4047 ;; searching
4049 (defun vip-if-string (prompt)
4050 (let ((s (vip-read-string-with-history
4051 prompt
4052 nil ; no initial
4053 'vip-search-history
4054 (car vip-search-history))))
4055 (if (not (string= s ""))
4056 (setq vip-s-string s))))
4059 (defun vip-toggle-search-style (arg)
4060 "Toggle the value of vip-case-fold-search/vip-re-search.
4061 Without prefix argument, will ask which search style to toggle. With prefix
4062 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
4064 Although this function is bound to \\[vip-toggle-search-style], the most
4065 convenient way to use it is to bind `//' to the macro
4066 `1 M-x vip-toggle-search-style' and `///' to
4067 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
4068 toggle case-fold-search and hitting `/' three times witth toggle regexp
4069 search. Macros are more convenient in this case because they don't affect
4070 the Emacs binding of `/'."
4071 (interactive "P")
4072 (let (msg)
4073 (cond ((or (eq arg 1)
4074 (and (null arg)
4075 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
4076 (if vip-case-fold-search
4077 "case-insensitive" "case-sensitive")
4078 (if vip-case-fold-search
4079 "case-sensitive"
4080 "case-insensitive")))))
4081 (setq vip-case-fold-search (null vip-case-fold-search))
4082 (if vip-case-fold-search
4083 (setq msg "Search becomes case-insensitive")
4084 (setq msg "Search becomes case-sensitive")))
4085 ((or (eq arg 2)
4086 (and (null arg)
4087 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
4088 (if vip-re-search
4089 "regexp-search" "vanilla-search")
4090 (if vip-re-search
4091 "vanilla-search"
4092 "regexp-search")))))
4093 (setq vip-re-search (null vip-re-search))
4094 (if vip-re-search
4095 (setq msg "Search becomes regexp-style")
4096 (setq msg "Search becomes vanilla-style")))
4098 (setq msg "Search style remains unchanged")))
4099 (prin1 msg t)))
4102 (defun vip-search-forward (arg)
4103 "Search a string forward.
4104 ARG is used to find the ARG's occurrence of the string.
4105 Null string will repeat previous search."
4106 (interactive "P")
4107 (let ((val (vip-P-val arg))
4108 (com (vip-getcom arg))
4109 (old-str vip-s-string))
4110 (setq vip-s-forward t)
4111 (vip-if-string "/")
4112 ;; this is not used at present, but may be used later
4113 (if (or (not (equal old-str vip-s-string))
4114 (not (markerp vip-local-search-start-marker))
4115 (not (marker-buffer vip-local-search-start-marker)))
4116 (setq vip-local-search-start-marker (point-marker)))
4117 (vip-search vip-s-string t val)
4118 (if com
4119 (progn
4120 (vip-move-marker-locally 'vip-com-point (mark t))
4121 (vip-execute-com 'vip-search-next val com)))))
4123 (defun vip-search-backward (arg)
4124 "Search a string backward.
4125 ARG is used to find the ARG's occurrence of the string.
4126 Null string will repeat previous search."
4127 (interactive "P")
4128 (let ((val (vip-P-val arg))
4129 (com (vip-getcom arg))
4130 (old-str vip-s-string))
4131 (setq vip-s-forward nil)
4132 (vip-if-string "?")
4133 ;; this is not used at present, but may be used later
4134 (if (or (not (equal old-str vip-s-string))
4135 (not (markerp vip-local-search-start-marker))
4136 (not (marker-buffer vip-local-search-start-marker)))
4137 (setq vip-local-search-start-marker (point-marker)))
4138 (vip-search vip-s-string nil val)
4139 (if com
4140 (progn
4141 (vip-move-marker-locally 'vip-com-point (mark t))
4142 (vip-execute-com 'vip-search-next val com)))))
4145 ;; Search for COUNT's occurrence of STRING.
4146 ;; Search is forward if FORWARD is non-nil, otherwise backward.
4147 ;; INIT-POINT is the position where search is to start.
4148 ;; Arguments:
4149 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
4150 (defun vip-search (string forward arg
4151 &optional no-offset init-point fail-if-not-found)
4152 (if (not (equal string ""))
4153 (let ((val (vip-p-val arg))
4154 (com (vip-getcom arg))
4155 (offset (not no-offset))
4156 (case-fold-search vip-case-fold-search)
4157 (start-point (or init-point (point))))
4158 (vip-deactivate-mark)
4159 (if forward
4160 (condition-case nil
4161 (progn
4162 (if offset (vip-forward-char-carefully))
4163 (if vip-re-search
4164 (progn
4165 (re-search-forward string nil nil val)
4166 (re-search-backward string))
4167 (search-forward string nil nil val)
4168 (search-backward string))
4169 ;; don't wait and don't flash in macros
4170 (or executing-kbd-macro
4171 (vip-flash-search-pattern))
4172 (if (not (equal start-point (point)))
4173 (push-mark start-point t)))
4174 (search-failed
4175 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4176 (progn
4177 (message "Search wrapped around BOTTOM of buffer")
4178 (goto-char (point-min))
4179 (vip-search string forward (cons 1 com) t start-point 'fail)
4180 ;; don't wait in macros
4181 (or executing-kbd-macro (sit-for 2))
4182 ;; delete the wrap-around message
4183 (message "")
4185 (goto-char start-point)
4186 (error "`%s': %s not found"
4187 string
4188 (if vip-re-search "Pattern" "String"))
4190 ;; backward
4191 (condition-case nil
4192 (progn
4193 (if vip-re-search
4194 (re-search-backward string nil nil val)
4195 (search-backward string nil nil val))
4196 ;; don't wait and don't flash in macros
4197 (or executing-kbd-macro
4198 (vip-flash-search-pattern))
4199 (if (not (equal start-point (point)))
4200 (push-mark start-point t)))
4201 (search-failed
4202 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
4203 (progn
4204 (message "Search wrapped around TOP of buffer")
4205 (goto-char (point-max))
4206 (vip-search string forward (cons 1 com) t start-point 'fail)
4207 ;; don't wait in macros
4208 (or executing-kbd-macro (sit-for 2))
4209 ;; delete the wrap-around message
4210 (message "")
4212 (goto-char start-point)
4213 (error "`%s': %s not found"
4214 string
4215 (if vip-re-search "Pattern" "String"))
4216 )))))))
4218 (defun vip-search-next (arg)
4219 "Repeat previous search."
4220 (interactive "P")
4221 (let ((val (vip-p-val arg))
4222 (com (vip-getcom arg)))
4223 (if (null vip-s-string) (error vip-NoPrevSearch))
4224 (vip-search vip-s-string vip-s-forward arg)
4225 (if com
4226 (progn
4227 (vip-move-marker-locally 'vip-com-point (mark t))
4228 (vip-execute-com 'vip-search-next val com)))))
4230 (defun vip-search-Next (arg)
4231 "Repeat previous search in the reverse direction."
4232 (interactive "P")
4233 (let ((val (vip-p-val arg))
4234 (com (vip-getcom arg)))
4235 (if (null vip-s-string) (error vip-NoPrevSearch))
4236 (vip-search vip-s-string (not vip-s-forward) arg)
4237 (if com
4238 (progn
4239 (vip-move-marker-locally 'vip-com-point (mark t))
4240 (vip-execute-com 'vip-search-Next val com)))))
4243 ;; Search contents of buffer defined by one of Viper's motion commands.
4244 ;; Repeatable via `n' and `N'.
4245 (defun vip-buffer-search-enable (&optional c)
4246 (cond (c (setq vip-buffer-search-char c))
4247 ((null vip-buffer-search-char)
4248 (setq vip-buffer-search-char ?g)))
4249 (define-key vip-vi-basic-map
4250 (char-to-string vip-buffer-search-char) 'vip-command-argument)
4251 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
4252 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
4254 ;; This is a Viper wraper for isearch-forward.
4255 (defun vip-isearch-forward (arg)
4256 "Do incremental search forward."
4257 (interactive "P")
4258 ;; emacs bug workaround
4259 (if (listp arg) (setq arg (car arg)))
4260 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
4262 ;; This is a Viper wraper for isearch-backward."
4263 (defun vip-isearch-backward (arg)
4264 "Do incremental search backward."
4265 (interactive "P")
4266 ;; emacs bug workaround
4267 (if (listp arg) (setq arg (car arg)))
4268 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
4271 ;; visiting and killing files, buffers
4273 (defun vip-switch-to-buffer ()
4274 "Switch to buffer in the current window."
4275 (interactive)
4276 (let (buffer)
4277 (setq buffer
4278 (read-buffer
4279 (format "Switch to buffer in this window \(%s\): "
4280 (buffer-name (other-buffer (current-buffer))))))
4281 (switch-to-buffer buffer)
4284 (defun vip-switch-to-buffer-other-window ()
4285 "Switch to buffer in another window."
4286 (interactive)
4287 (let (buffer)
4288 (setq buffer
4289 (read-buffer
4290 (format "Switch to buffer in another window \(%s\): "
4291 (buffer-name (other-buffer (current-buffer))))))
4292 (switch-to-buffer-other-window buffer)
4295 (defun vip-kill-buffer ()
4296 "Kill a buffer."
4297 (interactive)
4298 (let (buffer buffer-name)
4299 (setq buffer-name
4300 (read-buffer
4301 (format "Kill buffer \(%s\): "
4302 (buffer-name (current-buffer)))))
4303 (setq buffer
4304 (if (null buffer-name)
4305 (current-buffer)
4306 (get-buffer buffer-name)))
4307 (if (null buffer) (error "`%s': No such buffer" buffer-name))
4308 (if (or (not (buffer-modified-p buffer))
4309 (y-or-n-p
4310 (format
4311 "Buffer `%s' is modified, are you sure you want to kill it? "
4312 buffer-name)))
4313 (kill-buffer buffer)
4314 (error "Buffer not killed"))))
4317 (defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p")
4318 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
4319 This is useful when you the current directory contains files with the same
4320 prefix and many different suffixes. Usually, only one of the suffixes
4321 represents an editable file. However, file completion will stop at the `.'
4322 The smart suffix feature lets you hit RET in such a case, and Viper will
4323 select the appropriate suffix.
4325 Suffixes are tried in the order given and the first suffix for which a
4326 corresponding file exists is selected. If no file exists for any of the
4327 suffixes, the user is asked to confirm.
4329 To turn this feature off, set this variable to nil.")
4331 ;; Try to add suffix to files ending with a `.'
4332 ;; Useful when the user hits RET on a non-completed file name.
4333 (defun vip-file-add-suffix ()
4334 (let ((count 0)
4335 (len (length vip-smart-suffix-list))
4336 (file (buffer-string))
4337 found key cmd suff)
4338 (goto-char (point-max))
4339 (if (and vip-smart-suffix-list (string-match "\\.$" file))
4340 (progn
4341 (while (and (not found) (< count len))
4342 (setq suff (nth count vip-smart-suffix-list)
4343 count (1+ count))
4344 (if (file-exists-p (format "%s%s" file suff))
4345 (progn
4346 (setq found t)
4347 (insert suff))))
4349 (if found
4351 (vip-tmp-insert-at-eob " [Please complete file name]")
4352 (unwind-protect
4353 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
4354 (setq cmd
4355 (key-binding (setq key (read-key-sequence nil))))
4356 (cond ((eq cmd 'self-insert-command)
4357 (if vip-xemacs-p
4358 (insert (events-to-keys key))
4359 (insert key)))
4360 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
4361 nil)
4362 (t (command-execute cmd)))
4368 ;; Advice for use in find-file and read-file-name commands.
4369 (defadvice exit-minibuffer (before vip-exit-minibuffer-advice activate)
4370 "Run `vip-minibuffer-exit-hook' just before exiting the minibuffer."
4371 (run-hooks 'vip-minibuffer-exit-hook))
4373 (defadvice find-file (before vip-add-suffix-advice activate)
4374 "Use `read-file-name' for reading arguments."
4375 (interactive (list (read-file-name "Find file: "
4376 nil default-directory))))
4378 (defadvice find-file-other-window (before vip-add-suffix-advice activate)
4379 "Use `read-file-name' for reading arguments."
4380 (interactive (list (read-file-name "Find file in other window: "
4381 nil default-directory))))
4383 (defadvice find-file-other-frame (before vip-add-suffix-advice activate)
4384 "Use `read-file-name' for reading arguments."
4385 (interactive (list (read-file-name "Find file in other frame: "
4386 nil default-directory))))
4388 (defadvice read-file-name (around vip-suffix-advice activate)
4389 "Tell `exit-minibuffer' to run `vip-file-add-suffix' as a hook."
4390 (let ((vip-minibuffer-exit-hook 'vip-file-add-suffix))
4391 ad-do-it))
4395 ;; yank and pop
4397 (defsubst vip-yank (text)
4398 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
4399 (insert text)
4400 (setq this-command 'yank))
4402 (defun vip-put-back (arg)
4403 "Put back after point/below line."
4404 (interactive "P")
4405 (let ((val (vip-p-val arg))
4406 (text (if vip-use-register
4407 (cond ((vip-valid-register vip-use-register '(digit))
4408 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4409 ((vip-valid-register vip-use-register)
4410 (get-register (downcase vip-use-register)))
4411 (t (error vip-InvalidRegister vip-use-register)))
4412 (current-kill 0))))
4413 (if (null text)
4414 (if vip-use-register
4415 (let ((reg vip-use-register))
4416 (setq vip-use-register nil)
4417 (error vip-EmptyRegister reg))
4418 (error "")))
4419 (setq vip-use-register nil)
4420 (if (vip-end-with-a-newline-p text)
4421 (progn
4422 (if (eobp)
4423 (insert "\n")
4424 (forward-line 1))
4425 (beginning-of-line))
4426 (if (not (eolp)) (vip-forward-char-carefully)))
4427 (set-marker (vip-mark-marker) (point) (current-buffer))
4428 (vip-set-destructive-command
4429 (list 'vip-put-back val nil vip-use-register nil nil))
4430 (vip-loop val (vip-yank text)))
4431 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4432 ;; newline; it leaves the cursor at the beginning when the text contains
4433 ;; a newline
4434 (if (vip-same-line (point) (mark))
4435 (or (= (point) (mark)) (vip-backward-char-carefully))
4436 (exchange-point-and-mark)
4437 (if (bolp)
4438 (back-to-indentation)))
4439 (vip-deactivate-mark))
4441 (defun vip-Put-back (arg)
4442 "Put back at point/above line."
4443 (interactive "P")
4444 (let ((val (vip-p-val arg))
4445 (text (if vip-use-register
4446 (cond ((vip-valid-register vip-use-register '(digit))
4447 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4448 ((vip-valid-register vip-use-register)
4449 (get-register (downcase vip-use-register)))
4450 (t (error vip-InvalidRegister vip-use-register)))
4451 (current-kill 0))))
4452 (if (null text)
4453 (if vip-use-register
4454 (let ((reg vip-use-register))
4455 (setq vip-use-register nil)
4456 (error vip-EmptyRegister reg))
4457 (error "")))
4458 (setq vip-use-register nil)
4459 (if (vip-end-with-a-newline-p text) (beginning-of-line))
4460 (vip-set-destructive-command
4461 (list 'vip-Put-back val nil vip-use-register nil nil))
4462 (set-marker (vip-mark-marker) (point) (current-buffer))
4463 (vip-loop val (vip-yank text)))
4464 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4465 ;; newline; it leaves the cursor at the beginning when the text contains
4466 ;; a newline
4467 (if (vip-same-line (point) (mark))
4468 (or (= (point) (mark)) (vip-backward-char-carefully))
4469 (exchange-point-and-mark)
4470 (if (bolp)
4471 (back-to-indentation)))
4472 (vip-deactivate-mark))
4475 ;; Copy region to kill-ring.
4476 ;; If BEG and END do not belong to the same buffer, copy empty region.
4477 (defun vip-copy-region-as-kill (beg end)
4478 (condition-case nil
4479 (copy-region-as-kill beg end)
4480 (error (copy-region-as-kill beg beg))))
4483 (defun vip-delete-char (arg)
4484 "Delete character."
4485 (interactive "P")
4486 (let ((val (vip-p-val arg)))
4487 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
4488 (if (> val 1)
4489 (save-excursion
4490 (let ((here (point)))
4491 (end-of-line)
4492 (if (> val (- (point) here))
4493 (setq val (- (point) here))))))
4494 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
4495 (if (and vip-ex-style-motion (eolp))
4496 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
4497 (if vip-use-register
4498 (progn
4499 (cond ((vip-valid-register vip-use-register '((Letter)))
4500 (vip-append-to-register
4501 (downcase vip-use-register) (point) (- (point) val)))
4502 ((vip-valid-register vip-use-register)
4503 (copy-to-register
4504 vip-use-register (point) (- (point) val) nil))
4505 (t (error vip-InvalidRegister vip-use-register)))
4506 (setq vip-use-register nil)))
4507 (if vip-ex-style-motion
4508 (progn
4509 (delete-char val t)
4510 (if (and (eolp) (not (bolp))) (backward-char 1)))
4511 (if (eolp)
4512 (delete-backward-char val t)
4513 (delete-char val t)))))
4515 (defun vip-delete-backward-char (arg)
4516 "Delete previous character. On reaching beginning of line, stop and beep."
4517 (interactive "P")
4518 (let ((val (vip-p-val arg)))
4519 (vip-set-destructive-command
4520 (list 'vip-delete-backward-char val nil nil nil nil))
4521 (if (> val 1)
4522 (save-excursion
4523 (let ((here (point)))
4524 (beginning-of-line)
4525 (if (> val (- here (point)))
4526 (setq val (- here (point)))))))
4527 (if vip-use-register
4528 (progn
4529 (cond ((vip-valid-register vip-use-register '(Letter))
4530 (vip-append-to-register
4531 (downcase vip-use-register) (point) (+ (point) val)))
4532 ((vip-valid-register vip-use-register)
4533 (copy-to-register
4534 vip-use-register (point) (+ (point) val) nil))
4535 (t (error vip-InvalidRegister vip-use-register)))
4536 (setq vip-use-register nil)))
4537 (if (bolp) (ding)
4538 (delete-backward-char val t))))
4540 (defun vip-del-backward-char-in-insert ()
4541 "Delete 1 char backwards while in insert mode."
4542 (interactive)
4543 (if (and vip-ex-style-editing-in-insert (bolp))
4544 (beep 1)
4545 (delete-backward-char 1 t)))
4547 (defun vip-del-backward-char-in-replace ()
4548 "Delete one character in replace mode.
4549 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
4550 charecters. If it is nil, then the cursor just moves backwards, similarly
4551 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
4552 cursor move past the beginning of line."
4553 (interactive)
4554 (cond (vip-delete-backwards-in-replace
4555 (cond ((not (bolp))
4556 (delete-backward-char 1 t))
4557 (vip-ex-style-editing-in-insert
4558 (beep 1))
4559 ((bobp)
4560 (beep 1))
4562 (delete-backward-char 1 t))))
4563 (vip-ex-style-editing-in-insert
4564 (if (bolp)
4565 (beep 1)
4566 (backward-char 1)))
4568 (backward-char 1))))
4572 ;; join lines.
4574 (defun vip-join-lines (arg)
4575 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4576 (interactive "*P")
4577 (let ((val (vip-P-val arg)))
4578 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
4579 (vip-loop (if (null val) 1 (1- val))
4580 (progn
4581 (end-of-line)
4582 (if (not (eobp))
4583 (progn
4584 (forward-line 1)
4585 (delete-region (point) (1- (point)))
4586 (fixup-whitespace)))))))
4589 ;; Replace state
4591 (defun vip-change (beg end)
4592 (if (markerp beg) (setq beg (marker-position beg)))
4593 (if (markerp end) (setq end (marker-position end)))
4594 ;; beg is sometimes (mark t), which may be nil
4595 (or beg (setq beg end))
4597 (vip-set-complex-command-for-undo)
4598 (if vip-use-register
4599 (progn
4600 (copy-to-register vip-use-register beg end nil)
4601 (setq vip-use-register nil)))
4602 (vip-set-replace-overlay beg end)
4603 (setq last-command nil) ; separate repl text from prev kills
4605 (if (= (vip-replace-start) (point-max))
4606 (error "End of buffer"))
4608 (setq vip-last-replace-region
4609 (buffer-substring (vip-replace-start)
4610 (vip-replace-end)))
4612 ;; protect against error while inserting "@" and other disasters
4613 ;; (e.g., read-only buff)
4614 (condition-case conds
4615 (if (vip-same-line (vip-replace-start)
4616 (vip-replace-end))
4617 (progn
4618 ;; tabs cause problems in replace, so untabify
4619 (goto-char (vip-replace-end))
4620 (insert-before-markers "@") ; put placeholder after the TAB
4621 (untabify (vip-replace-start) (point))
4622 ;; del @, don't put on kill ring
4623 (delete-backward-char 1)
4625 (vip-set-replace-overlay-glyphs
4626 vip-replace-region-start-delimiter
4627 vip-replace-region-end-delimiter)
4628 ;; this move takes care of the last posn in the overlay, which
4629 ;; has to be shifted because of insert. We can't simply insert
4630 ;; "$" before-markers because then overlay-start will shift the
4631 ;; beginning of the overlay in case we are replacing a single
4632 ;; character. This fixes the bug with `s' and `cl' commands.
4633 (vip-move-replace-overlay (vip-replace-start) (point))
4634 (goto-char (vip-replace-start))
4635 (vip-change-state-to-replace t))
4636 (kill-region (vip-replace-start)
4637 (vip-replace-end))
4638 (vip-hide-replace-overlay)
4639 (vip-change-state-to-insert))
4640 (error ;; make sure that the overlay doesn't stay.
4641 ;; go back to the original point
4642 (goto-char (vip-replace-start))
4643 (vip-hide-replace-overlay)
4644 (vip-message-conditions conds))))
4647 (defun vip-change-subr (beg end)
4648 ;; beg is sometimes (mark t), which may be nil
4649 (or beg (setq beg end))
4651 (if vip-use-register
4652 (progn
4653 (copy-to-register vip-use-register beg end nil)
4654 (setq vip-use-register nil)))
4655 (kill-region beg end)
4656 (setq this-command 'vip-change)
4657 (vip-yank-last-insertion))
4659 (defun vip-toggle-case (arg)
4660 "Toggle character case."
4661 (interactive "P")
4662 (let ((val (vip-p-val arg)) (c))
4663 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
4664 (while (> val 0)
4665 (setq c (following-char))
4666 (delete-char 1 nil)
4667 (if (eq c (upcase c))
4668 (insert-char (downcase c) 1)
4669 (insert-char (upcase c) 1))
4670 (if (eolp) (backward-char 1))
4671 (setq val (1- val)))))
4674 ;; query replace
4676 (defun vip-query-replace ()
4677 "Query replace.
4678 If a null string is suplied as the string to be replaced,
4679 the query replace mode will toggle between string replace
4680 and regexp replace."
4681 (interactive)
4682 (let (str)
4683 (setq str (vip-read-string-with-history
4684 (if vip-re-query-replace "Query replace regexp: "
4685 "Query replace: ")
4686 nil ; no initial
4687 'vip-replace1-history
4688 (car vip-replace1-history) ; default
4690 (if (string= str "")
4691 (progn
4692 (setq vip-re-query-replace (not vip-re-query-replace))
4693 (message "Query replace mode changed to %s"
4694 (if vip-re-query-replace "regexp replace"
4695 "string replace")))
4696 (if vip-re-query-replace
4697 (query-replace-regexp
4699 (vip-read-string-with-history
4700 (format "Query replace regexp `%s' with: " str)
4701 nil ; no initial
4702 'vip-replace1-history
4703 (car vip-replace1-history) ; default
4705 (query-replace
4707 (vip-read-string-with-history
4708 (format "Query replace `%s' with: " str)
4709 nil ; no initial
4710 'vip-replace1-history
4711 (car vip-replace1-history) ; default
4712 ))))))
4715 ;; marking
4717 (defun vip-mark-beginning-of-buffer ()
4718 "Mark beginning of buffer."
4719 (interactive)
4720 (push-mark (point))
4721 (goto-char (point-min))
4722 (exchange-point-and-mark)
4723 (message "Mark set at the beginning of buffer"))
4725 (defun vip-mark-end-of-buffer ()
4726 "Mark end of buffer."
4727 (interactive)
4728 (push-mark (point))
4729 (goto-char (point-max))
4730 (exchange-point-and-mark)
4731 (message "Mark set at the end of buffer"))
4733 (defun vip-mark-point ()
4734 "Set mark at point of buffer."
4735 (interactive)
4736 (let ((char (vip-read-char-exclusive)))
4737 (cond ((and (<= ?a char) (<= char ?z))
4738 (point-to-register (1+ (- char ?a))))
4739 ((= char ?<) (vip-mark-beginning-of-buffer))
4740 ((= char ?>) (vip-mark-end-of-buffer))
4741 ((= char ?.) (vip-set-mark-if-necessary))
4742 ((= char ?,) (vip-cycle-through-mark-ring))
4743 ((= char ?D) (mark-defun))
4744 (t (error ""))
4747 ;; Algorithm: If first invocation of this command save mark on ring, goto
4748 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4749 ;; making it into the new mark, M1.
4750 ;; Push this mark back and set mark to the original point position, p1.
4751 ;; So, if you hit '' or `` then you can return to p1.
4753 ;; If repeated command, pop top elt from the ring into mark and
4754 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4755 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4756 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4757 ;; etc.
4758 (defun vip-cycle-through-mark-ring ()
4759 "Visit previous locations on the mark ring.
4760 One can use `` and '' to temporarily jump 1 step back."
4761 (let* ((sv-pt (point)))
4762 ;; if repeated `m,' command, pop the previously saved mark.
4763 ;; Prev saved mark is actually prev saved point. It is used if the
4764 ;; user types `` or '' and is discarded
4765 ;; from the mark ring by the next `m,' command.
4766 ;; In any case, go to the previous or previously saved mark.
4767 ;; Then push the current mark (popped off the ring) and set current
4768 ;; point to be the mark. Current pt as mark is discarded by the next
4769 ;; m, command.
4770 (if (eq last-command 'vip-cycle-through-mark-ring)
4772 ;; save current mark if the first iteration
4773 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4774 (if (mark t)
4775 (push-mark (mark t) t)) )
4776 (pop-mark)
4777 (set-mark-command 1)
4778 ;; don't duplicate mark on the ring
4779 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4780 (push-mark sv-pt t)
4781 (vip-deactivate-mark)
4782 (setq this-command 'vip-cycle-through-mark-ring)
4786 (defun vip-goto-mark (arg)
4787 "Go to mark."
4788 (interactive "P")
4789 (let ((char (read-char))
4790 (com (vip-getcom arg)))
4791 (vip-goto-mark-subr char com nil)))
4793 (defun vip-goto-mark-and-skip-white (arg)
4794 "Go to mark and skip to first non-white character on line."
4795 (interactive "P")
4796 (let ((char (read-char))
4797 (com (vip-getCom arg)))
4798 (vip-goto-mark-subr char com t)))
4800 (defun vip-goto-mark-subr (char com skip-white)
4801 (if (eobp)
4802 (if (bobp)
4803 (error "Empty buffer")
4804 (backward-char 1)))
4805 (cond ((vip-valid-register char '(letter))
4806 (let* ((buff (current-buffer))
4807 (reg (1+ (- char ?a)))
4808 (text-marker (get-register reg)))
4809 (if com (vip-move-marker-locally 'vip-com-point (point)))
4810 (if (not (vip-valid-marker text-marker))
4811 (error vip-EmptyTextmarker char))
4812 (if (and (vip-same-line (point) vip-last-jump)
4813 (= (point) vip-last-jump-ignore))
4814 (push-mark vip-last-jump t)
4815 (push-mark nil t)) ; no msg
4816 (vip-register-to-point reg)
4817 (setq vip-last-jump (point-marker))
4818 (cond (skip-white
4819 (back-to-indentation)
4820 (setq vip-last-jump-ignore (point))))
4821 (if com
4822 (if (equal buff (current-buffer))
4823 (vip-execute-com (if skip-white
4824 'vip-goto-mark-and-skip-white
4825 'vip-goto-mark)
4826 nil com)
4827 (switch-to-buffer buff)
4828 (goto-char vip-com-point)
4829 (vip-change-state-to-vi)
4830 (error "")))))
4831 ((and (not skip-white) (= char ?`))
4832 (if com (vip-move-marker-locally 'vip-com-point (point)))
4833 (if (and (vip-same-line (point) vip-last-jump)
4834 (= (point) vip-last-jump-ignore))
4835 (goto-char vip-last-jump))
4836 (if (null (mark t)) (error "Mark is not set in this buffer"))
4837 (if (= (point) (mark t)) (pop-mark))
4838 (exchange-point-and-mark)
4839 (setq vip-last-jump (point-marker)
4840 vip-last-jump-ignore 0)
4841 (if com (vip-execute-com 'vip-goto-mark nil com)))
4842 ((and skip-white (= char ?'))
4843 (if com (vip-move-marker-locally 'vip-com-point (point)))
4844 (if (and (vip-same-line (point) vip-last-jump)
4845 (= (point) vip-last-jump-ignore))
4846 (goto-char vip-last-jump))
4847 (if (= (point) (mark t)) (pop-mark))
4848 (exchange-point-and-mark)
4849 (setq vip-last-jump (point))
4850 (back-to-indentation)
4851 (setq vip-last-jump-ignore (point))
4852 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
4853 (t (error vip-InvalidTextmarker char))))
4855 (defun vip-insert-tab ()
4856 (interactive)
4857 (insert-tab))
4859 (defun vip-exchange-point-and-mark ()
4860 (interactive)
4861 (exchange-point-and-mark)
4862 (back-to-indentation))
4864 ;; Input Mode Indentation
4866 ;; Returns t, if the string before point matches the regexp STR.
4867 (defsubst vip-looking-back (str)
4868 (and (save-excursion (re-search-backward str nil t))
4869 (= (point) (match-end 0))))
4872 (defun vip-forward-indent ()
4873 "Indent forward -- `C-t' in Vi."
4874 (interactive)
4875 (setq vip-cted t)
4876 (indent-to (+ (current-column) vip-shift-width)))
4878 (defun vip-backward-indent ()
4879 "Backtab, C-d in VI"
4880 (interactive)
4881 (if vip-cted
4882 (let ((p (point)) (c (current-column)) bol (indent t))
4883 (if (vip-looking-back "[0^]")
4884 (progn
4885 (if (eq ?^ (preceding-char))
4886 (setq vip-preserve-indent t))
4887 (delete-backward-char 1)
4888 (setq p (point))
4889 (setq indent nil)))
4890 (save-excursion
4891 (beginning-of-line)
4892 (setq bol (point)))
4893 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4894 (delete-region (point) p)
4895 (if indent
4896 (indent-to (- c vip-shift-width)))
4897 (if (or (bolp) (vip-looking-back "[^ \t]"))
4898 (setq vip-cted nil)))))
4900 (defun vip-autoindent ()
4901 "Auto Indentation, Vi-style."
4902 (interactive)
4903 (let ((col (current-indentation)))
4904 (if vip-preserve-indent
4905 (setq vip-preserve-indent nil)
4906 (setq vip-current-indent col))
4907 ;; don't leave whitespace lines around
4908 (if (memq last-command
4909 '(vip-autoindent
4910 vip-open-line vip-Open-line
4911 vip-replace-state-exit-cmd))
4912 (indent-to-left-margin))
4913 (newline 1)
4914 (if vip-auto-indent
4915 (progn
4916 (setq vip-cted t)
4917 (if vip-electric-mode
4918 (indent-according-to-mode)
4919 (indent-to vip-current-indent))
4924 ;; Viewing registers
4926 (defun vip-ket-function (arg)
4927 "Function called by \], the ket. View registers and call \]\]."
4928 (interactive "P")
4929 (let ((reg (read-char)))
4930 (cond ((vip-valid-register reg '(letter Letter))
4931 (view-register (downcase reg)))
4932 ((vip-valid-register reg '(digit))
4933 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4934 (save-excursion
4935 (set-buffer (get-buffer-create "*Output*"))
4936 (delete-region (point-min) (point-max))
4937 (insert (format "Register %c contains the string:\n" reg))
4938 (insert text)
4939 (goto-char (point-min)))
4940 (display-buffer "*Output*")))
4941 ((= ?\] reg)
4942 (vip-next-heading arg))
4943 (t (error
4944 vip-InvalidRegister reg)))))
4946 (defun vip-brac-function (arg)
4947 "Function called by \[, the brac. View textmarkers and call \[\["
4948 (interactive "P")
4949 (let ((reg (read-char)))
4950 (cond ((= ?\[ reg)
4951 (vip-prev-heading arg))
4952 ((= ?\] reg)
4953 (vip-heading-end arg))
4954 ((vip-valid-register reg '(letter))
4955 (let* ((val (get-register (1+ (- reg ?a))))
4956 (buf (if (not val)
4957 (error vip-EmptyTextmarker reg)
4958 (marker-buffer val)))
4959 (pos (marker-position val))
4960 line-no text (s pos) (e pos))
4961 (save-excursion
4962 (set-buffer (get-buffer-create "*Output*"))
4963 (delete-region (point-min) (point-max))
4964 (if (and buf pos)
4965 (progn
4966 (save-excursion
4967 (set-buffer buf)
4968 (setq line-no (1+ (count-lines (point-min) val)))
4969 (goto-char pos)
4970 (beginning-of-line)
4971 (if (re-search-backward "[^ \t]" nil t)
4972 (progn
4973 (beginning-of-line)
4974 (setq s (point))))
4975 (goto-char pos)
4976 (forward-line 1)
4977 (if (re-search-forward "[^ \t]" nil t)
4978 (progn
4979 (end-of-line)
4980 (setq e (point))))
4981 (setq text (buffer-substring s e))
4982 (setq text (format "%s<%c>%s"
4983 (substring text 0 (- pos s))
4984 reg (substring text (- pos s)))))
4985 (insert
4986 (format
4987 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4988 reg (buffer-name buf) line-no))
4989 (insert (format "Here is some text around %c:\n\n %s"
4990 reg text)))
4991 (insert (format vip-EmptyTextmarker reg)))
4992 (goto-char (point-min)))
4993 (display-buffer "*Output*")))
4994 (t (error vip-InvalidTextmarker reg)))))
4998 ;; commands in insertion mode
5000 (defun vip-delete-backward-word (arg)
5001 "Delete previous word."
5002 (interactive "p")
5003 (save-excursion
5004 (push-mark nil t)
5005 (backward-word arg)
5006 (delete-region (point) (mark t))
5007 (pop-mark)))
5010 (defun vip-set-expert-level (&optional dont-change-unless)
5011 "Sets the expert level for a Viper user.
5012 Can be called interactively to change (temporarily or permanently) the
5013 current expert level.
5015 The optional argument DONT-CHANGE-UNLESS if not nil, says that
5016 the level should not be changed, unless its current value is
5017 meaningless (i.e., not one of 1,2,3,4,5).
5019 User level determines the setting of Viper variables that are most
5020 sensitive for VI-style look-and-feel."
5022 (interactive)
5024 (if (not (natnump vip-expert-level)) (setq vip-expert-level 0))
5026 (save-window-excursion
5027 (delete-other-windows)
5028 ;; if 0 < vip-expert-level < vip-max-expert-level
5029 ;; & dont-change-unless = t -- use it; else ask
5030 (vip-ask-level dont-change-unless))
5032 (setq vip-always t
5033 vip-ex-style-motion t
5034 vip-ex-style-editing-in-insert t
5035 vip-want-ctl-h-help nil)
5037 (cond ((eq vip-expert-level 1) ; novice or beginner
5038 (global-set-key ; in emacs-state
5039 vip-toggle-key
5040 (if (vip-window-display-p) 'vip-iconify 'suspend-emacs))
5041 (setq vip-no-multiple-ESC t
5042 vip-re-search t
5043 vip-vi-style-in-minibuffer t
5044 vip-search-wrap-around-t t
5045 vip-want-emacs-keys-in-vi nil
5046 vip-want-emacs-keys-in-insert nil))
5048 ((and (> vip-expert-level 1) (< vip-expert-level 5))
5049 ;; intermediate to guru
5050 (setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice)
5051 vip-want-emacs-keys-in-vi t
5052 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
5054 (if (eq vip-expert-level 4) ; respect user's ex-style motions
5055 ; and vip-no-multiple-ESC
5056 (progn
5057 (setq-default vip-ex-style-editing-in-insert
5058 (cdr (assoc 'vip-ex-style-editing-in-insert
5059 vip-saved-user-settings))
5060 vip-ex-style-motion
5061 (cdr (assoc 'vip-ex-style-motion
5062 vip-saved-user-settings)))
5063 (setq vip-ex-style-motion
5064 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
5065 vip-ex-style-editing-in-insert
5066 (cdr (assoc 'vip-ex-style-editing-in-insert
5067 vip-saved-user-settings))
5068 vip-re-search
5069 (cdr (assoc 'vip-re-search vip-saved-user-settings))
5070 vip-no-multiple-ESC
5071 (cdr (assoc 'vip-no-multiple-ESC
5072 vip-saved-user-settings))))))
5074 ;; A wizard!!
5075 ;; Ideally, if 5 is selected, a buffer should pop up to let the
5076 ;; user toggle the values of variables.
5077 (t (setq-default vip-ex-style-editing-in-insert
5078 (cdr (assoc 'vip-ex-style-editing-in-insert
5079 vip-saved-user-settings))
5080 vip-ex-style-motion
5081 (cdr (assoc 'vip-ex-style-motion
5082 vip-saved-user-settings)))
5083 (setq vip-want-ctl-h-help
5084 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
5085 vip-always
5086 (cdr (assoc 'vip-always vip-saved-user-settings))
5087 vip-no-multiple-ESC
5088 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
5089 vip-ex-style-motion
5090 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
5091 vip-ex-style-editing-in-insert
5092 (cdr (assoc 'vip-ex-style-editing-in-insert
5093 vip-saved-user-settings))
5094 vip-re-search
5095 (cdr (assoc 'vip-re-search vip-saved-user-settings))
5096 vip-want-emacs-keys-in-vi
5097 (cdr (assoc 'vip-want-emacs-keys-in-vi
5098 vip-saved-user-settings))
5099 vip-want-emacs-keys-in-insert
5100 (cdr (assoc 'vip-want-emacs-keys-in-insert
5101 vip-saved-user-settings)))))
5102 (vip-set-mode-vars-for vip-current-state)
5103 (if (or vip-always
5104 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
5105 (vip-set-hooks)))
5107 ;; Ask user expert level.
5108 (defun vip-ask-level (dont-change-unless)
5109 (let ((ask-buffer " *vip-ask-level*")
5110 level-changed repeated)
5111 (save-window-excursion
5112 (switch-to-buffer ask-buffer)
5114 (or (eq this-command 'vip-set-expert-level)
5115 (and
5116 (<= vip-expert-level vip-max-expert-level)
5117 (>= vip-expert-level 1))
5118 (progn
5119 (insert "
5121 *** Important Notice for VIP users***
5123 This is VIPER
5125 @joke
5126 Viper Is a Package for Emacs Rebels,
5127 a VI Plan for Emacs Rescue,
5128 and a venomous VI PERil.
5129 @end joke
5131 Technically speaking, Viper is a new Vi emulator that replaces
5132 the old VIP package.
5134 Viper emulates Vi much better than VIP. It also significantly
5135 extends and improves upon Vi in many useful ways.
5137 Although many VIP settings in your ~/.vip are compatible with Viper,
5138 you may have to change some of them. Please refer to the documentation,
5139 which can be obtained by executing
5141 :help
5143 when Viper is in Vi state.
5145 If you will be so lucky as to find a bug, report it via the command
5147 :submitReport
5149 Type any key to continue... ")
5151 (read-char)
5152 (erase-buffer)))
5154 (while (or (> vip-expert-level vip-max-expert-level)
5155 (< vip-expert-level 1)
5156 (null dont-change-unless))
5157 (erase-buffer)
5158 (if repeated
5159 (progn
5160 (message "Invalid user level")
5161 (beep 1))
5162 (setq repeated t))
5163 (setq dont-change-unless t
5164 level-changed t)
5165 (insert "
5166 Please specify your level of familiarity with the venomous VI PERil
5167 (and the VI Plan for Emacs Rescue).
5168 You can change it at any time by typing `M-x vip-set-expert-level RET'
5170 1 -- BEGINNER: Almost all Emacs features are suppressed.
5171 Feels almost like straight Vi. File name completion and
5172 command history in the minibuffer are thrown in as a bonus.
5173 To use Emacs productively, you must reach level 3 or higher.
5174 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
5175 so most Emacs commands can be used when Viper is in Vi state.
5176 Good progress---you are well on the way to level 3!
5177 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
5178 in Viper's insert state.
5179 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
5180 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
5181 variables. Adjust these settings to your taste.
5182 5 -- WIZARD: Like 4, but user settings are also respected for vip-always,
5183 vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and
5184 vip-want-emacs-keys-in-insert. Adjust these to your taste.
5186 Please, specify your level now: ")
5188 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
5189 ) ; end while
5191 ;; tell the user if level was changed
5192 (and level-changed
5193 (progn
5194 (insert
5195 (format "\n\n\n\n\n\t\tYou have selected user level %d"
5196 vip-expert-level))
5197 (if (y-or-n-p "Do you wish to make this change permanent? ")
5198 ;; save the setting for vip-expert-level
5199 (vip-save-setting
5200 'vip-expert-level
5201 (format "Saving user level %d ..." vip-expert-level)
5202 vip-custom-file-name))
5204 (bury-buffer) ; remove ask-buffer from screen
5205 (message "")
5209 (defun viper-version ()
5210 (interactive)
5211 (message "Viper version is %s" viper-version))
5213 (defalias 'vip-version 'viper-version)
5215 (defun vip-nil ()
5216 (interactive)
5217 (beep 1))
5220 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
5221 (defun vip-register-to-point (char &optional enforce-buffer)
5222 "Like jump-to-register, but switches to another buffer in another window."
5223 (interactive "cViper register to point: ")
5224 (let ((val (get-register char)))
5225 (cond
5226 ((and (fboundp 'frame-configuration-p)
5227 (frame-configuration-p val))
5228 (set-frame-configuration val))
5229 ((window-configuration-p val)
5230 (set-window-configuration val))
5231 ((vip-valid-marker val)
5232 (if (and enforce-buffer
5233 (not (equal (current-buffer) (marker-buffer val))))
5234 (error (concat vip-EmptyTextmarker " in this buffer")
5235 (1- (+ char ?a))))
5236 (pop-to-buffer (marker-buffer val))
5237 (goto-char val))
5238 ((and (consp val) (eq (car val) 'file))
5239 (find-file (cdr val)))
5241 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
5244 (defun vip-save-kill-buffer ()
5245 "Save then kill current buffer. "
5246 (interactive)
5247 (if (< vip-expert-level 2)
5248 (save-buffers-kill-emacs)
5249 (save-buffer)
5250 (kill-buffer (current-buffer))))
5254 ;;; Bug Report
5256 (defun vip-submit-report ()
5257 "Submit bug report on Viper."
5258 (interactive)
5259 (let ((reporter-prompt-for-summary-p t)
5260 (vip-device-type (vip-device-type))
5261 color-display-p frame-parameters
5262 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
5263 varlist salutation window-config)
5265 ;; If mode info is needed, add variable to `let' and then set it below,
5266 ;; like we did with color-display-p.
5267 (setq color-display-p (if (vip-window-display-p)
5268 (vip-color-display-p)
5269 'non-x)
5270 minibuffer-vi-face (if (vip-has-face-support-p)
5271 (vip-get-face vip-minibuffer-vi-face)
5272 'non-x)
5273 minibuffer-insert-face (if (vip-has-face-support-p)
5274 (vip-get-face vip-minibuffer-insert-face)
5275 'non-x)
5276 minibuffer-emacs-face (if (vip-has-face-support-p)
5277 (vip-get-face vip-minibuffer-emacs-face)
5278 'non-x)
5279 frame-parameters (if (fboundp 'frame-parameters)
5280 (frame-parameters (selected-frame))))
5282 (setq varlist (list 'vip-vi-minibuffer-minor-mode
5283 'vip-insert-minibuffer-minor-mode
5284 'vip-vi-intercept-minor-mode
5285 'vip-vi-local-user-minor-mode
5286 'vip-vi-kbd-minor-mode
5287 'vip-vi-global-user-minor-mode
5288 'vip-vi-state-modifier-minor-mode
5289 'vip-vi-diehard-minor-mode
5290 'vip-vi-basic-minor-mode
5291 'vip-replace-minor-mode
5292 'vip-insert-intercept-minor-mode
5293 'vip-insert-local-user-minor-mode
5294 'vip-insert-kbd-minor-mode
5295 'vip-insert-global-user-minor-mode
5296 'vip-insert-state-modifier-minor-mode
5297 'vip-insert-diehard-minor-mode
5298 'vip-insert-basic-minor-mode
5299 'vip-emacs-intercept-minor-mode
5300 'vip-emacs-local-user-minor-mode
5301 'vip-emacs-kbd-minor-mode
5302 'vip-emacs-global-user-minor-mode
5303 'vip-emacs-state-modifier-minor-mode
5304 'vip-automatic-iso-accents
5305 'vip-want-emacs-keys-in-insert
5306 'vip-want-emacs-keys-in-vi
5307 'vip-keep-point-on-undo
5308 'vip-no-multiple-ESC
5309 'vip-ESC-key
5310 'vip-want-ctl-h-help
5311 'vip-ex-style-editing-in-insert
5312 'vip-delete-backwards-in-replace
5313 'vip-vi-style-in-minibuffer
5314 'vip-vi-state-hook
5315 'vip-insert-state-hook
5316 'vip-replace-state-hook
5317 'vip-emacs-state-hook
5318 'ex-cycle-other-window
5319 'ex-cycle-through-non-files
5320 'vip-expert-level
5321 'major-mode
5322 'vip-device-type
5323 'color-display-p
5324 'frame-parameters
5325 'minibuffer-vi-face
5326 'minibuffer-insert-face
5327 'minibuffer-emacs-face
5329 (setq salutation "
5330 Congratulations! You may have unearthed a bug in Viper!
5331 Please mail a concise, accurate summary of the problem to the address above.
5333 -------------------------------------------------------------------")
5334 (setq window-config (current-window-configuration))
5335 (with-output-to-temp-buffer " *vip-info*"
5336 (switch-to-buffer " *vip-info*")
5337 (delete-other-windows)
5338 (princ "
5339 PLEASE FOLLOW THESE PROCEDURES
5340 ------------------------------
5342 Before reporting a bug, please verify that it is related to Viper, and is
5343 not cause by other packages you are using.
5345 Don't report compilation warnings, unless you are certain that there is a
5346 problem. These warnings are normal and unavoidable.
5348 Please note that users should not modify variables and keymaps other than
5349 those advertised in the manual. Such `customization' is likely to crash
5350 Viper, as it would any other improperly customized Emacs package.
5352 If you are reporting an error message received while executing one of the
5353 Viper commands, type:
5355 M-x set-variable <Return> debug-on-error <Return> t <Return>
5357 Then reproduce the error. The above command will cause Emacs to produce a
5358 back trace of the execution that leads to the error. Please include this
5359 trace in your bug report.
5361 If you believe that one of Viper's commands goes into an infinite loop
5362 \(e.g., Emacs freezes\), type:
5364 M-x set-variable <Return> debug-on-quit <Return> t <Return>
5366 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
5367 the current command. Include the resulting back trace in the bug report.
5369 Mail anyway (y or n)? ")
5370 (if (y-or-n-p "Mail anyway? ")
5372 (set-window-configuration window-config)
5373 (error "Bug report aborted")))
5375 (require 'reporter)
5376 (set-window-configuration window-config)
5378 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
5379 (vip-version)
5380 varlist
5381 nil 'delete-other-windows
5382 salutation)
5388 ;; Smoothes out the difference between Emacs' unread-command-events
5389 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
5390 ;; events or a sequence of keys.
5392 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
5393 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
5394 ;; into an event. Below, we delete nil from event lists, since nil is the most
5395 ;; common symbol that might appear in this wrong context.
5396 (defun vip-set-unread-command-events (arg)
5397 (if vip-emacs-p
5398 (setq
5399 unread-command-events
5400 (let ((new-events
5401 (cond ((eventp arg) (list arg))
5402 ((listp arg) arg)
5403 ((sequencep arg)
5404 (listify-key-sequence arg))
5405 (t (error
5406 "vip-set-unread-command-events: Invalid argument, %S"
5407 arg)))))
5408 (if (not (eventp nil))
5409 (setq new-events (delq nil new-events)))
5410 (append new-events unread-command-events)))
5411 ;; XEmacs
5412 (setq
5413 unread-command-events
5414 (append
5415 (cond ((vip-characterp arg) (list (character-to-event arg)))
5416 ((eventp arg) (list arg))
5417 ((stringp arg) (mapcar 'character-to-event arg))
5418 ((vectorp arg) (append arg nil)) ; turn into list
5419 ((listp arg) (vip-eventify-list-xemacs arg))
5420 (t (error
5421 "vip-set-unread-command-events: Invalid argument, %S" arg)))
5422 unread-command-events))))
5424 ;; list is assumed to be a list of events of characters
5425 (defun vip-eventify-list-xemacs (lis)
5426 (mapcar
5427 (function (lambda (elt)
5428 (cond ((vip-characterp elt) (character-to-event elt))
5429 ((eventp elt) elt)
5430 (t (error
5431 "vip-eventify-list-xemacs: can't convert to event, %S"
5432 elt)))))
5433 lis))
5437 ;;; Bring in the rest of the files
5438 (require 'viper-mous)
5439 (require 'viper-macs)
5440 (require 'viper-ex)
5444 ;; The following is provided for compatibility with older VIP's
5446 (defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
5447 (defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert)
5448 (defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs)
5452 ;;; Load .vip and set up hooks
5454 ;; This hook designed to enable Vi-style editing in comint-based modes."
5455 (defun vip-comint-mode-hook ()
5456 (setq require-final-newline nil
5457 vip-ex-style-editing-in-insert nil
5458 vip-ex-style-motion nil)
5459 (vip-change-state-to-insert))
5462 ;; This sets major mode hooks to make them come up in vi-state.
5463 (defun vip-set-hooks ()
5465 ;; It is of course a misnomer to call viper-mode a `major mode'.
5466 ;; However, this has the effect that if the user didn't specify the
5467 ;; default mode, new buffers that fall back on the default will come up
5468 ;; in Fundamental Mode and Vi state.
5469 (setq default-major-mode 'viper-mode)
5471 ;; The following major modes should come up in vi-state
5472 (defadvice fundamental-mode (after vip-fundamental-mode-ad activate)
5473 "Run `vip-change-state-to-vi' on entry."
5474 (vip-change-state-to-vi))
5476 (defvar makefile-mode-hook)
5477 (add-hook 'makefile-mode-hook 'viper-mode)
5479 (defvar help-mode-hook)
5480 (add-hook 'help-mode-hook 'viper-mode)
5482 (defvar awk-mode-hook)
5483 (add-hook 'awk-mode-hook 'viper-mode)
5485 (defvar html-mode-hook)
5486 (add-hook 'html-mode-hook 'viper-mode)
5487 (defvar html-helper-mode-hook)
5488 (add-hook 'html-helper-mode-hook 'viper-mode)
5489 (defvar java-mode-hook)
5490 (add-hook 'java-mode-hook 'viper-mode)
5492 (defvar emacs-lisp-mode-hook)
5493 (add-hook 'emacs-lisp-mode-hook 'viper-mode)
5495 (defvar lisp-mode-hook)
5496 (add-hook 'lisp-mode-hook 'viper-mode)
5498 (defvar bibtex-mode-hook)
5499 (add-hook 'bibtex-mode-hook 'viper-mode)
5501 (defvar cc-mode-hook)
5502 (add-hook 'cc-mode-hook 'viper-mode)
5504 (defvar c-mode-hook)
5505 (add-hook 'c-mode-hook 'viper-mode)
5507 (defvar c++-mode-hook)
5508 (add-hook 'c++-mode-hook 'viper-mode)
5510 (defvar lisp-interaction-mode-hook)
5511 (add-hook 'lisp-interaction-mode-hook 'viper-mode)
5513 (defvar fortran-mode-hook)
5514 (add-hook 'fortran-mode-hook 'vip-mode)
5516 (defvar text-mode-hook)
5517 (add-hook 'text-mode-hook 'viper-mode)
5519 (add-hook 'completion-list-mode-hook 'viper-mode)
5520 (add-hook 'compilation-mode-hook 'viper-mode)
5522 (add-hook 'perl-mode-hook 'viper-mode)
5523 (add-hook 'tcl-mode-hook 'viper-mode)
5525 (defvar emerge-startup-hook)
5526 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
5528 ;; Tell vc-diff to put *vc* in Vi mode
5529 (if (featurep 'vc)
5530 (defadvice vc-diff (after vip-vc-ad activate)
5531 "Force Vi state in VC diff buffer."
5532 (vip-change-state-to-vi))
5533 (vip-eval-after-load
5534 "vc"
5535 '(defadvice vc-diff (after vip-vc-ad activate)
5536 "Force Vi state in VC diff buffer."
5537 (vip-change-state-to-vi))))
5539 (vip-eval-after-load
5540 "emerge"
5541 '(defadvice emerge-quit (after vip-emerge-advice activate)
5542 "Run `vip-change-state-to-vi' after quitting emerge."
5543 (vip-change-state-to-vi)))
5544 ;; In case Emerge was loaded before Viper.
5545 (defadvice emerge-quit (after vip-emerge-advice activate)
5546 "Run `vip-change-state-to-vi' after quitting emerge."
5547 (vip-change-state-to-vi))
5549 (vip-eval-after-load
5550 "asm-mode"
5551 '(defadvice asm-mode (after vip-asm-mode-ad activate)
5552 "Run `vip-change-state-to-vi' on entry."
5553 (vip-change-state-to-vi)))
5555 ;; passwd.el sets up its own buffer, which turns up in Vi mode,
5556 ;; thus overriding the local map. We don't need Vi mode here.
5557 (vip-eval-after-load
5558 "passwd"
5559 '(defadvice read-passwd-1 (before vip-passwd-ad activate)
5560 "Switch to emacs state while reading password."
5561 (vip-change-state-to-emacs)))
5563 (vip-eval-after-load
5564 "prolog"
5565 '(defadvice prolog-mode (after vip-prolog-ad activate)
5566 "Switch to Vi state in Prolog mode."
5567 (vip-change-state-to-vi)))
5569 ;; Emacs shell, ange-ftp, and comint-based modes
5570 (defvar comint-mode-hook)
5571 (vip-modify-major-mode
5572 'comint-mode 'insert-state vip-comint-mode-modifier-map)
5573 (vip-modify-major-mode
5574 'comint-mode 'vi-state vip-comint-mode-modifier-map)
5575 (add-hook 'comint-mode-hook 'vip-comint-mode-hook)
5577 ;; Shell scripts
5578 (defvar sh-mode-hook)
5579 (add-hook 'sh-mode-hook 'viper-mode)
5580 (defvar ksh-mode-hook)
5581 (add-hook 'ksh-mode-hook 'viper-mode)
5583 ;; Dired
5584 (vip-modify-major-mode 'dired-mode 'emacs-state vip-dired-modifier-map)
5585 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
5587 ;; MH-E
5588 (vip-modify-major-mode 'mh-folder-mode 'emacs-state vip-slash-and-colon-map)
5589 ;; changing state to emacs is needed so the preceding will take hold
5590 (add-hook 'mh-folder-mode-hook 'vip-change-state-to-emacs)
5591 (add-hook 'mh-show-mode-hook 'viper-mode)
5593 ;; Gnus
5594 (vip-modify-major-mode 'gnus-group-mode 'emacs-state vip-slash-and-colon-map)
5595 (vip-modify-major-mode
5596 'gnus-summary-mode 'emacs-state vip-slash-and-colon-map)
5597 ;; changing state to emacs is needed so the preceding will take hold
5598 (add-hook 'gnus-group-mode-hook 'vip-change-state-to-emacs)
5599 (add-hook 'gnus-summary-mode-hook 'vip-change-state-to-emacs)
5600 (add-hook 'gnus-article-mode-hook 'viper-mode)
5602 ;; Info
5603 (vip-modify-major-mode 'Info-mode 'emacs-state vip-slash-and-colon-map)
5604 ;; Switching to emacs is needed so the above will take hold
5605 (defadvice Info-mode (after vip-Info-ad activate)
5606 "Switch to emacs mode."
5607 (vip-change-state-to-emacs))
5609 ;; Buffer menu
5610 (vip-modify-major-mode
5611 'Buffer-menu-mode 'emacs-state vip-slash-and-colon-map)
5612 ;; Switching to emacs is needed so the above will take hold
5613 (defadvice Buffer-menu-mode (after vip-Buffer-menu-ad activate)
5614 "Switch to emacs mode."
5615 (vip-change-state-to-emacs))
5617 ;; View mode
5618 (if vip-emacs-p
5619 (progn
5620 (defvar view-mode-hook)
5621 (add-hook 'view-mode-hook 'vip-change-state-to-emacs))
5622 (defadvice view-minor-mode (after vip-view-ad activate)
5623 "Switch to Emacs state in View mode."
5624 (vip-change-state-to-emacs))
5625 (defvar view-hook)
5626 (add-hook 'view-hook 'vip-change-state-to-emacs))
5628 ;; For VM users.
5629 ;; Put summary and other VM buffers in Emacs state.
5630 (defvar vm-mode-hooks)
5631 (defvar vm-summary-mode-hooks)
5632 (add-hook 'vm-mode-hooks 'vip-change-state-to-emacs)
5633 (add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs)
5635 ;; For RMAIL users.
5636 ;; Put buf in Emacs state after edit.
5637 (vip-eval-after-load
5638 "rmailedit"
5639 '(defadvice rmail-cease-edit (after vip-rmail-advice activate)
5640 "Switch to emacs state when done editing message."
5641 (vip-change-state-to-emacs)))
5642 ;; In case RMAIL was loaded before Viper.
5643 (defadvice rmail-cease-edit (after vip-rmail-advice activate)
5644 "Switch to emacs state when done editing message."
5645 (vip-change-state-to-emacs))
5646 ) ; vip-set-hooks
5648 ;; Set some useful macros
5649 ;; These must be before we load .vip, so the user can unrecord them.
5651 ;; repeat the 2nd previous command without rotating the command history
5652 (vip-record-kbd-macro
5653 (vector vip-repeat-from-history-key '\1) 'vi-state
5654 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5655 ;; repeat the 3d previous command without rotating the command history
5656 (vip-record-kbd-macro
5657 (vector vip-repeat-from-history-key '\2) 'vi-state
5658 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5660 ;; toggle case sensitivity in search
5661 (vip-record-kbd-macro
5662 "//" 'vi-state
5663 [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)
5664 ;; toggle regexp/vanila search
5665 (vip-record-kbd-macro
5666 "///" 'vi-state
5667 [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)
5670 ;; ~/.vip is loaded if it exists
5671 (if (and (file-exists-p vip-custom-file-name)
5672 (not noninteractive))
5673 (load vip-custom-file-name))
5675 ;; VIP compatibility: merge whatever the user has in vip-mode-map into
5676 ;; Viper's basic map.
5677 (vip-add-keymap vip-mode-map vip-vi-global-user-map)
5680 ;; Applying Viper customization -- runs after (load .vip)
5682 ;; Save user settings or Viper defaults for vars controled by vip-expert-level
5683 (setq vip-saved-user-settings
5684 (list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help)
5685 (cons 'vip-always vip-always)
5686 (cons 'vip-no-multiple-ESC vip-no-multiple-ESC)
5687 (cons 'vip-ex-style-motion vip-ex-style-motion)
5688 (cons 'vip-ex-style-editing-in-insert
5689 vip-ex-style-editing-in-insert)
5690 (cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi)
5691 (cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert)
5692 (cons 'vip-re-search vip-re-search)))
5695 (vip-set-minibuffer-style)
5696 (vip-set-minibuffer-faces)
5697 (vip-set-search-face)
5698 (vip-set-replace-overlay-face)
5699 (if vip-buffer-search-char
5700 (vip-buffer-search-enable))
5701 (vip-update-alphanumeric-class)
5703 ;;; Familiarize Viper with some minor modes that have their own keymaps
5704 (vip-harness-minor-mode "compile")
5705 (vip-harness-minor-mode "outline")
5706 (vip-harness-minor-mode "allout")
5707 (vip-harness-minor-mode "xref")
5708 (vip-harness-minor-mode "lmenu")
5709 (vip-harness-minor-mode "vc")
5710 (vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX
5711 (vip-harness-minor-mode "latex") ; which is in one of these two files
5712 (vip-harness-minor-mode "cyrillic")
5713 (vip-harness-minor-mode "russian")
5714 (vip-harness-minor-mode "view-less")
5715 (vip-harness-minor-mode "view")
5718 ;; Intercept maps could go in viper-keym.el
5719 ;; We keep them here in case someone redefines them in ~/.vip
5721 (define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5722 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5724 ;; This is taken care of by vip-insert-global-user-map.
5725 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
5728 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
5729 ;; iconifies Emacs
5730 (define-key vip-vi-intercept-map vip-toggle-key 'vip-toggle-key-action)
5731 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
5734 (if (or vip-always
5735 (and (< vip-expert-level 5) (> vip-expert-level 0)))
5736 (vip-set-hooks))
5738 ;; Let all minor modes take effect after loading
5739 ;; this may not be enough, so we also set default minor-mode-alist.
5740 ;; Without setting the default, new buffers that come up in emacs mode have
5741 ;; minor-mode-map-alist = nil, unless we call vip-change-state-*
5742 (if (eq vip-current-state 'emacs-state)
5743 (progn
5744 (vip-change-state-to-emacs)
5745 (setq-default minor-mode-map-alist minor-mode-map-alist)
5749 (run-hooks 'vip-load-hook) ; the last chance to change something
5751 (provide 'viper)
5752 (provide 'vip19)
5753 (provide 'vip)
5755 ;;; viper.el ends here