1 ;;; viper.el --- A full-featured Vi emulator for GNU Emacs 19 and XEmacs 19,
2 ;; a VI Plan for Emacs Rescue,
3 ;; and a venomous VI PERil.
4 ;; Viper Is also a Package for Emacs Rebels.
6 ;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.
9 ;; Keywords: emulations
10 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29 ;; viper|Michael Kifer|kifer@cs.sunysb.edu|
30 ;; A full-featured Vi emulator for Emacs 19 and XEmacs 19|
31 ;; 19-February-95|2.72|~/modes/viper.tar.Z|
33 (defconst viper-version
"2.72 of February 19, 1995"
34 "The current version of Viper")
38 ;; Viper is a full-featured Vi emulator for Emacs 19. It emulates and
39 ;; improves upon the standard features of Vi and, at the same time, allows
40 ;; full access to all Emacs facilities. Viper supports multiple undo,
41 ;; file name completion, command, file, and search history and it extends
42 ;; Vi in many other ways. Viper is highly customizable through the various
43 ;; hooks, user variables, and keymaps. It is implemented as a collection
44 ;; of minor modes and it is designed to provide full access to all Emacs
45 ;; major and minor modes.
49 ;; Viper is a new name for a package formerly known as VIP-19,
50 ;; which was a successor of VIP version 3.5 by Masahiko Sato
51 ;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
52 ;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
53 ;; were also shamelessly plagiarized.
55 ;; Viper maintains some degree of compatibility with these older
56 ;; packages. See the documentation for customization.
58 ;; The main difference between Viper and these older packages are:
60 ;; 1. Viper emulates Vi at several levels, from almost complete conformity
61 ;; to a rather loose Vi-compliance.
63 ;; 2. Viper provides full access to all major and minor modes of Emacs
64 ;; without the need to type extra keys.
65 ;; The older versions of VIP (and other Vi emulators) do not work with
66 ;; some major and minor modes.
68 ;; 3. Viper supports vi-style undo.
70 ;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
72 ;; 5. Viper has a better interface to ex, including command, variable, and
73 ;; file name completion.
75 ;; 6. Viper uses native Emacs history and completion features; it doesn't
76 ;; rely on other packages (such as gmhist.el and completer.el) to provide
79 ;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
80 ;; user to switch from Insert state to Vi state to Replace state, etc.
82 ;; 8. Viper keeps history of recently inserted pieces of text and recently
83 ;; executed Vi-style destructive commands, such as `i', `d', etc.
84 ;; These pieces of text can be inserted in later insertion commands;
85 ;; the previous destructive commands can be re-executed.
87 ;; 9. Viper has Vi-style keyboard macros, which enhances the similar
88 ;; facility in the original Vi.
89 ;; First, one can execute any Emacs command while defining a
90 ;; macro, not just the Vi commands. Second, macros are defined in a
91 ;; WYSYWYG mode, using an interface to Emacs' WYSIWYG style of defining
92 ;; macros. Third, in Viper, one can define macros that are specific to
93 ;; a given buffer, a given major mode, or macros defined for all buffers.
94 ;; The same macro name can have several different definitions:
95 ;; one global, several definitions for various major modes, and
96 ;; definitions for specific buffers.
97 ;; Bffer-specific definitions override mode-specific
98 ;; definitions, which, in turn, override global definitions.
107 ;;; Acknowledgements:
109 ;; Bug reports and ideas contributed by the following users
110 ;; have helped improve Viper and the various versions of VIP:
112 ;; jjm@hplb.hpl.hp.com (Jean-Jacques Moreau), jl@cse.ogi.edu (John
113 ;; Launchbury), rxga@ulysses.att.com, jamesm@bga.com (D.J. Miller II),
114 ;; ascott@fws214.intel.com (Andy Scott), toma@convex.convex.com,
115 ;; gvr@cs.brown.edu, dave@hellgate.utah.edu, cook@biostat.wisc.edu
116 ;; (Tom Cook), lindstro@biostat.wisc.edu (Mary Lindstrom),
117 ;; edmonds@edmonds.home.cs.ubc.ca (Brian Edmonds), mveiga@dit.upm.es
118 ;; (Marcelino Veiga Tuimil), dwight@toolucky.llnl.gov (Dwight Shih),
119 ;; phil_brooks@MENTORG.COM (Phil Brooks), kin@isi.com (Kin Cho),
120 ;; ahg@panix.com (Al Gelders), dwallach@cs.princeton.edu (Dan Wallach),
121 ;; hpz@ibmhpz.aug.ipp-garching.mpg.de (Hans-Peter Zehrfeld),
122 ;; simonb@prl.philips.co.uk (Simon Blanchard), Mark.Bordas@East.Sun.COM
123 ;; (Mark Bordas), gviswana@cs.wisc.edu (Guhan Viswanathan)
125 ;; Special thanks to Marcelino Veiga Tuimil <mveiga@dit.upm.es> for
126 ;; suggesting a way of intercepting ESC sequences on dumb terminals. Due to
127 ;; this, Viper can now handle arrow keys, F-keys, etc., in Xterm windows
128 ;; and on dumb terminals. This also made it possible to implement Vi-style
135 ;; In most cases, Viper handles major modes correctly, i.e., they come up
136 ;; in the right state (either vi-state or emacs-state). For instance, text
137 ;; files come up in vi-state, while, say, Dired appears in emacs-state by
139 ;; However, some modes do not appear in the right mode in the beginning,
140 ;; usually because they neglect to follow Emacs conventions (e.g., they don't
141 ;; use (kill-all-local-variables) when they start. Some major modes
142 ;; may fail to come up in emacs-state if they call hooks, such as
143 ;; text-hook, for no good reason.
145 ;; As an immediate solution, you can hit C-z to bring about the right mode.
146 ;; An interim solution is to add an appropriate hook to the mode like this:
148 ;; (add-hook 'your-favorite-mode 'viper-mode)
150 ;; (add-hook 'your-favorite-mode 'vip-change-state-to-emacs)
152 ;; whichever applies. The right thing to do, however, is to complain to the
153 ;; author of the respective package. (Sometimes they also neglect to equip
154 ;; their modes with hooks, which is one more reason for complaining.)
156 ;; 2. Keymap handling
157 ;; Because Emacs 19 has an elegant mechanism for turning minor mode keymaps
158 ;; on and off, implementation of Viper has been greatly simplified. Viper
159 ;; has several minor modes.
161 ;; Viper's Vi state consists of seven minor modes:
163 ;; vip-vi-intercept-minor-mode
164 ;; vip-vi-local-user-minor-mode
165 ;; vip-vi-global-user-minor-mode
166 ;; vip-vi-kbd-minor-mode
167 ;; vip-vi-state-modifier-minor-mode
168 ;; vip-vi-diehard-minor-mode
169 ;; vip-vi-basic-minor-mode
171 ;; Bindings done to the keymap of the first mode overshadow those done to
172 ;; the second, which, in turn, overshadows those done to the third, etc.
174 ;; The last vip-vi-basic-minor-mode contains most of the usual Vi bindings
175 ;; in its edit mode. This mode provides access to all Emacs facilities.
176 ;; Novice users, however, may want to set their vip-expert-level to 1
177 ;; in their .vip file. This will enable vip-vi-diehard-minor-mode. This
178 ;; minor mode's bindings make Viper simulate the usual Vi very closely.
179 ;; For instance, C-c will not have its standard Emacs binding
180 ;; and so many of the goodies of Emacs are not available.
182 ;; An skilled user, should set vip-expert-level to at least 3. This will
183 ;; enable ;; C-c and many Emacs facilities will become available.
184 ;; In this case, vip-vi-diehard-minor-mode is inactive.
186 ;; Viper gurus should have at least
187 ;; (setq vip-expert-level 4)
188 ;; in their ~/.vip files. This will unsuppress all Emacs keys that are not
189 ;; essential for VI-style editing.
190 ;; Pick-and-choose users may want to put
191 ;; (setq vip-expert-level 5)
192 ;; in ~/.vip. Viper will then leave it up to the user to set the variables
193 ;; vip-want-* See vip-set-expert-level for details.
195 ;; The very first minor mode, vip-vi-intercept-minor-mode, is of no
196 ;; concern for the user. It is needed to bind Viper's vital keys, such as
199 ;; The second mode, vip-vi-local-user-minor-mode, usually has an
200 ;; empty keymap. However, the user can set bindings in this keymap, which
201 ;; will overshadow the corresponding bindings in the other two minor
202 ;; modes. This is useful, for example, for setting up ZZ in gnus,
203 ;; rmail, mh-e, etc., to send message instead of saving it in a file.
204 ;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
205 ;; that would visit the next or the previous file in the Dired buffer.
206 ;; Setting local keys is tricky, so don't do it directly. Instead, use
207 ;; vip-add-local-keys function (see its doc).
209 ;; The third minor mode, vip-vi-global-user-minor-mode, is also intended
210 ;; for the users but, unlike vip-vi-local-user-minor-mode, its key
211 ;; bindings are seen in all Viper buffers. This mode keys can be done
212 ;; with define-key command.
214 ;; The fourth minor mode, vip-vi-kbd-minor-mode, is used by keyboard
215 ;; macros. Users are NOT supposed to modify this keymap directly.
217 ;; The fifth mode, vip-vi-state-modifier-minor-mode, can be used to set
218 ;; key bindings that are visible in some major modes but not in others.
220 ;; Users are allowed to modify keymaps that belong to
221 ;; vip-vi-local-user-minor-mode, vip-vi-global-user-minor-mode,
222 ;; and vip-vi-state-modifier-minor-mode only.
224 ;; Viper's Insert state also has seven minor modes:
226 ;; vip-insert-intercept-minor-mode
227 ;; vip-insert-local-user-minor-mode
228 ;; vip-insert-global-user-minor-mode
229 ;; vip-insert-kbd-minor-mode
230 ;; vip-insert-state-modifier-minor-mode
231 ;; vip-insert-diehard-minor-mode
232 ;; vip-insert-basic-minor-mode
234 ;; As with VI's editing modes, the first mode, vip-insert-intercept-minor-mode
235 ;; is used to bind vital keys that are not to be changed by the user.
237 ;; The next mode, vip-insert-local-user-minor-mode, is used to customize
238 ;; bindings in the insert state of Viper. The third mode,
239 ;; vip-insert-global-user-minor-mode is like
240 ;; vip-insert-local-user-minor-mode, except that its bindings are seen in
241 ;; all Viper buffers. As with vip-vi-local-user-minor-mode, its bindings
242 ;; should be done via the function vip-add-local-keys. Bindings for
243 ;; vip-insert-global-user-minor-mode can be set with the define-key command.
245 ;; The next minor mode, vip-insert-kbd-minor-mode,
246 ;; is used for keyboard VI-style macros defined with :map!.
248 ;; The fifth minor mode, vip-insert-state-modifier-minor-mode, is like
249 ;; vip-vi-state-modifier-minor-mode, except that it is used in the Insert
250 ;; state; it can be used to modify keys in a mode-specific fashion.
252 ;; The minor mode vip-insert-diehard-minor-mode is in effect when
253 ;; the user wants a high degree of Vi compatibility (a bad idea, really!).
254 ;; The last minor mode, vip-insert-basic-minor-mode, is always in effect
255 ;; when Viper is in insert state. It binds a small number of keys needed for
256 ;; Viper's operation.
258 ;; Finally, Viper provides minor modes for overriding bindings set by Emacs
259 ;; modes when Viper is in Emacs state:
261 ;; vip-emacs-local-user-minor-mode
262 ;; vip-emacs-global-user-minor-mode
263 ;; vip-emacs-kbd-minor-mode
264 ;; vip-emacs-state-modifier-minor-mode
266 ;; These minor modes are in effect when Viper is in Emacs state. The keymap
267 ;; associated with vip-emacs-global-user-minor-mode,
268 ;; vip-emacs-global-user-map, overrides the global and local keymaps as
269 ;; well as the minor mode keymaps set by other modes. The keymap of
270 ;; vip-emacs-local-user-minor-mode, vip-emacs-local-user-map, overrides
271 ;; everything, but it is used on a per buffer basis.
272 ;; The keymap associated with vip-emacs-state-modifier-minor-mode
273 ;; overrides keys on a per-major-mode basis. The mode
274 ;; vip-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
277 ;; 3. There is also one minor mode that is used when Viper is in its
278 ;; replace-state (used for commands like cw, C, etc.). This mode is
281 ;; vip-replace-minor-mode
283 ;; and its keymap is vip-replace-map. Replace minor mode is always
284 ;; used in conjunction with the minor modes for insert-state, and its
285 ;; keymap overshadows the keymaps for insert minor modes.
287 ;; 4. Defining buffer-local bindings in Vi and Insert modes.
288 ;; As mentioned before, sometimes, it is convenient to have
289 ;; buffer-specific of mode-specific key bindings in Vi and insert modes.
290 ;; Viper provides a special function, vip-add-local-keys, to do precisely
291 ;; this. For instance, is you need to add couple of mode-specific bindings
292 ;; to Insert mode, you can put
294 ;; (vip-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
296 ;; somewhere in a hook of this major mode. If you put something like this
297 ;; in your own elisp function, this will define bindings specific to the
298 ;; buffer that was current at the time of the call to vip-add-local-keys.
299 ;; The only thing to make sure here is that the major mode of this buffer
300 ;; is written according to Emacs conventions, which includes a call to
301 ;; (kill-all-local-variables). See vip-add-local-keys for more details.
304 ;; TO DO (volunteers?):
306 ;; 1. Some of the code that is inherited from VIP-3.5 is rather
307 ;; convoluted. Instead of vip-command-argument, keymaps should bind the
308 ;; actual commands. E.g., "dw" should be bound to a generic command
309 ;; vip-delete that will delete things based on the value of
310 ;; last-command-char. This would greatly simplify the logic and the code.
312 ;; 2. Somebody should venture to write a customization package a la
313 ;; options.el that would allow the user to change values of variables
314 ;; that meet certain specs (e.g., match a regexp) and whose doc string
315 ;; starts with a '*'. Then, the user should be offered to save
316 ;; variables that were changed. This will make user's customization job
325 (require 'viper-util
)
330 ;; Is t until viper-mode executes for the very first time.
331 ;; Prevents recursive descend into startup messages.
332 (defvar vip-first-time t
)
334 (defvar vip-expert-level
0
335 "User's expert level.
336 The minor mode vip-vi-diehard-minor-mode is in effect when
337 vip-expert-level is 1 or 2 or when vip-want-emacs-keys-in-vi is t.
338 The minor mode vip-insert-diehard-minor-mode is in effect when
339 vip-expert-level is 1 or 2 or if vip-want-emacs-keys-in-insert is t.
340 Use `M-x vip-set-expert-level' to change this.")
342 ;; Max expert level supported by Viper. This is NOT a user option.
343 ;; It is here to make it hard for the user from resetting it.
344 (defconst vip-max-expert-level
5)
346 ;; Contains user settings for vars affected by vip-set-expert-level function.
347 ;; Not a user option.
348 (defvar vip-saved-user-settings nil
)
351 ;;; Viper minor modes
353 ;; for some reason, this is not local in Emacs, so I made it so.
354 (make-variable-buffer-local 'minor-mode-map-alist
)
356 ;; Ideally, minor-mode-map-alist should be permanent-local. But Emacs has a
357 ;; bug that precludes that. So, there is a workaround in
358 ;; vip-harness-minor-mode.
359 ;;(put 'minor-mode-map-alist 'permanent-local t)
361 ;; Mode for vital things like \e, C-z.
362 (vip-deflocalvar vip-vi-intercept-minor-mode nil
)
364 (vip-deflocalvar vip-vi-basic-minor-mode nil
365 "Viper's minor mode for Vi bindings.")
367 (vip-deflocalvar vip-vi-local-user-minor-mode nil
368 "Auxiliary minor mode for user-defined local bindings in Vi state.")
370 (vip-deflocalvar vip-vi-global-user-minor-mode nil
371 "Auxiliary minor mode for user-defined global bindings in Vi state.")
373 (vip-deflocalvar vip-vi-state-modifier-minor-mode nil
374 "Minor mode used to make major-mode-specific modification to Vi state.")
376 (vip-deflocalvar vip-vi-diehard-minor-mode nil
377 "This minor mode is in effect when the user wants Viper to be Vi.")
379 (vip-deflocalvar vip-vi-kbd-minor-mode nil
380 "Minor mode for Ex command macros Vi state.
381 The corresponding keymap stores key bindings of Vi macros defined with
382 the Ex command :map.")
384 ;; Mode for vital things like \e, C-z.
385 (vip-deflocalvar vip-insert-intercept-minor-mode nil
)
387 (vip-deflocalvar vip-insert-basic-minor-mode nil
388 "Viper's minor mode for bindings in Insert mode.")
390 (vip-deflocalvar vip-insert-local-user-minor-mode nil
391 "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
392 This is a way to overshadow normal Insert mode bindings locally to certain
393 designated buffers.")
395 (vip-deflocalvar vip-insert-global-user-minor-mode nil
396 "Auxiliary minor mode for global user-defined bindings in Insert state.")
398 (vip-deflocalvar vip-insert-state-modifier-minor-mode nil
399 "Minor mode used to make major-mode-specific modification to Insert state.")
401 (vip-deflocalvar vip-insert-diehard-minor-mode nil
402 "Minor mode that simulates Vi very closely.
403 Not recommened, except for the novice user.")
405 (vip-deflocalvar vip-insert-kbd-minor-mode nil
406 "Minor mode for Ex command macros Insert state.
407 The corresponding keymap stores key bindings of Vi macros defined with
408 the Ex command :map!.")
410 (vip-deflocalvar vip-replace-minor-mode nil
411 "Minor mode in effect in replace state (cw, C, and the like commands).")
413 ;; Mode for vital things like \C-z and \C-x)
414 ;; This is t, by default. So, any new buffer will have C-z defined as
415 ;; switch to Vi, unless we switched states in this buffer
416 (vip-deflocalvar vip-emacs-intercept-minor-mode t
)
418 (vip-deflocalvar vip-emacs-local-user-minor-mode t
419 "Minor mode for local user bindings effective in Emacs state.
420 Users can use it to override Emacs bindings when Viper is in its Emacs
423 (vip-deflocalvar vip-emacs-global-user-minor-mode t
424 "Minor mode for global user bindings in effect in Emacs state.
425 Users can use it to override Emacs bindings when Viper is in its Emacs
428 (vip-deflocalvar vip-emacs-kbd-minor-mode t
429 "Minor mode for Vi style macros in Emacs state.
430 The corresponding keymap stores key bindings of Vi macros defined with
431 `vip-record-kbd-macro' command. There is no Ex-level command to do this
434 (vip-deflocalvar vip-emacs-state-modifier-minor-mode t
435 "Minor mode used to make major-mode-specific modification to Emacs state.
436 For instance, a Vi purist may want to bind `dd' in Dired mode to a function
437 that deletes a file.")
443 (defvar vip-automatic-iso-accents nil
444 "*If non-nil, ISO accents will be turned on in insert/replace emacs states and turned off in vi-state.
445 For some users, this behavior may be too primitive. In this case, use
446 insert/emacs/vi state hooks.")
449 ;;; Emacs keys in other states.
451 (defvar vip-want-emacs-keys-in-insert t
452 "*Set to nil if you want complete Vi compatibility in insert mode.
453 Complete compatibility with Vi is not recommended for power use of Viper.")
455 (defvar vip-want-emacs-keys-in-vi t
456 "*Set to nil if you want complete Vi compatibility in Vi mode.
457 Full Vi compatibility is not recommended for power use of Viper.")
463 ;; Used to 'undo' complex commands, such as replace and insert commands.
464 (vip-deflocalvar vip-undo-needs-adjustment nil
)
465 (put 'vip-undo-needs-adjustment
'permanent-local t
)
467 ;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
468 ;; complex command that must be undone atomically. If inserted, it is
469 ;; erased by vip-change-state-to-vi and vip-repeat.
470 (defconst vip-buffer-undo-list-mark
'viper
)
472 (defvar vip-keep-point-on-undo nil
473 "*Non-nil means not to move point while undoing commands.
474 This style is different from Emacs and Vi. Try it to see if
475 it better fits your working style.")
477 ;; Replace mode and changing text
479 ;; Viper's own after/before change functions, which get add-hook'ed to Emacs'
480 (vip-deflocalvar vip-after-change-functions nil
"")
481 (vip-deflocalvar vip-before-change-functions nil
"")
482 (vip-deflocalvar vip-post-command-hooks nil
"")
483 (vip-deflocalvar vip-pre-command-hooks nil
"")
485 ;; Can be used to pass global states around for short period of time
486 (vip-deflocalvar vip-intermediate-command nil
"")
488 ;; Indicates that the current destructive command has started in replace mode.
489 (vip-deflocalvar vip-began-as-replace nil
"")
491 (defvar vip-replace-overlay-cursor-color
"Red"
492 "*Color to use in Replace state")
495 (vip-deflocalvar vip-replace-overlay nil
"")
496 (put 'vip-replace-overlay
'permanent-local t
)
500 (make-face 'vip-replace-overlay-face
)
501 (or (face-differs-from-default-p 'vip-replace-overlay-face
)
503 (if (vip-can-use-colors "darkseagreen2" "Black")
506 'vip-replace-overlay-face
"darkseagreen2")
507 (set-face-foreground 'vip-replace-overlay-face
"Black")))
508 (set-face-underline-p 'vip-replace-overlay-face t
))
511 (defvar vip-replace-overlay-face
'vip-replace-overlay-face
512 "*Face for highlighting replace regions on a window display.")
514 (defvar vip-replace-region-end-symbol
515 (if (and window-system
(vip-display-color-p)) "" "$")
516 "*Symbol to mark the end of a replacement region. A string.
517 At present, only the first character of a non-empty string is used to
518 actually mark the region.")
519 (defvar vip-replace-region-start-symbol
""
520 "*Symbol to mark the beginning of a replacement region. A string.
521 Not yet implemented.")
523 ;; These are local marker that must be initialized to nil and moved with
524 ;; `vip-move-marker-locally'
526 ;; Remember the last position inside the replace region.
527 (vip-deflocalvar vip-last-posn-in-replace-region nil
)
528 ;; Remember the last position while inserting
529 (vip-deflocalvar vip-last-posn-while-in-insert-state nil
)
530 (put 'vip-last-posn-in-replace-region
'permanent-local t
)
531 (put 'vip-last-posn-while-in-insert-state
'permanent-local t
)
533 (vip-deflocalvar vip-sitting-in-replace nil
"")
534 (put 'vip-sitting-in-replace
'permanent-local t
)
536 ;; Remember the number of characters that have to be deleted in replace
537 ;; mode to compensate for the inserted characters.
538 (vip-deflocalvar vip-replace-chars-to-delete
0 "")
539 (vip-deflocalvar vip-replace-chars-deleted
0 "")
541 ;; Insertion ring and command ring
542 (defvar vip-insertion-ring-size
14
543 "The size of the insertion ring.")
544 ;; The insertion ring.
545 (defvar vip-insertion-ring nil
)
546 ;; This is temp insertion ring. Used to do rotation for display purposes.
547 ;; When rotation just started, it is initialized to vip-insertion-ring.
548 (defvar vip-temp-insertion-ring nil
)
549 (defvar vip-last-inserted-string-from-insertion-ring
"")
551 (defvar vip-command-ring-size
14
552 "The size of the command ring.")
554 (defvar vip-command-ring nil
)
555 ;; This is temp command ring. Used to do rotation for display purposes.
556 ;; When rotation just started, it is initialized to vip-command-ring.
557 (defvar vip-temp-command-ring nil
)
559 ;; Modes and related variables
561 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
562 (vip-deflocalvar vip-current-state
'emacs-state
)
565 (defvar vip-toggle-key
"\C-z"
566 "The key used to change states from emacs to Vi and back.
567 In insert mode, this key also functions as Meta.
568 Must be set in .vip file or prior to loading Viper.
569 This setting cannot be changed interactively.")
571 (defvar vip-ESC-key
"\e"
573 Must be set in .vip file or prior to loading Viper.
574 This setting cannot be changed interactively.")
576 (defvar vip-no-multiple-ESC t
577 "*If true, multiple ESC in Vi mode will cause bell to ring.
578 \_ is then mapped to Meta.
579 This is set to t on a windowing terminal and to 'twice on a dumb
580 terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
581 enables cursor keys and is generally more convenient, as terminals usually
582 don't have a convenient Meta key.
583 Setting vip-no-multiple-ESC to nil will allow as many multiple ESC,
584 as is allowed by the major mode in effect.")
587 (defvar vip-want-ctl-h-help nil
588 "*If t then C-h is bound to help-command in insert mode, if nil then it is
589 bound to delete-backward-char.")
591 ;; Autoindent in insert
593 ;; Variable that keeps track of whether C-t has been pressed.
594 (vip-deflocalvar vip-cted nil
"")
596 ;; Preserve the indent value, used by C-d in insert mode.
597 (vip-deflocalvar vip-current-indent
0)
599 ;; Whether to preserve the indent, used by C-d in insert mode.
600 (vip-deflocalvar vip-preserve-indent nil
)
602 (defconst vip-auto-indent nil
605 (defconst vip-shift-width
8
606 "*The shiftwidth variable.")
608 ;; Variables for repeating destructive commands
610 (defconst vip-keep-point-on-repeat t
611 "*If t, don't move point when repeating previous command.
612 This is useful for doing repeated changes with the '.' key.
613 The user can change this to nil, if she likes when the cursor moves
614 to a new place after repeating previous Vi command.")
616 ;; Remember insert point as a marker. This is a local marker that must be
617 ;; initialized to nil and moved with `vip-move-marker-locally'.
618 (vip-deflocalvar vip-insert-point nil
)
619 (put 'vip-insert-point
'permanent-local t
)
621 ;; This remembers the point before dabbrev-expand was called.
622 ;; If vip-insert-point turns out to be bigger than that, it is reset
623 ;; back to vip-pre-command-point.
624 ;; The reason this is needed is because dabbrev-expand (and possibly
625 ;; others) may jump to before the insertion point, delete something and
626 ;; then reinsert a bigger piece. For instance: bla^blo
627 ;; If dabbrev-expand is called after `blo' and ^ undicates vip-insert-point,
628 ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
629 ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
630 ;; will insert the expansion, and we get: blablo^
631 ;; Whatever we insert next goes before the ^, i.e., before the
632 ;; vip-insert-point marker. So, Viper will think that nothing was
633 ;; inserted. Remembering the orig position of the marker circumvents the
635 ;; We don't know of any command, except dabbrev-expand, that has the same
636 ;; problem. However, the same trick can be used if such a command is
639 (vip-deflocalvar vip-pre-command-point nil
)
640 (put 'vip-pre-command-point
'permanent-local t
) ; this is probably an overkill
642 ;; This is used for saving inserted text.
643 (defvar vip-last-insertion nil
)
645 ;; Remembers the last replaced region.
646 (defvar vip-last-replace-region
"")
648 ;; Remember com point as a marker.
649 ;; This is a local marker. Should be moved with `vip-move-marker-locally'
650 (vip-deflocalvar vip-com-point nil
)
652 ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
653 ;; It is used to re-execute last destructive command.
654 ;; M-COM is a Lisp symbol representing the function to be executed.
655 ;; VAL is the prefix argument that was used with that command.
656 ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
657 ;; additional information on how the function in M-COM is to be handled.
658 ;; REG is the register used by command
659 ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
661 ;; COMMAND-KEYS are the keys that were typed to invoke the command.
662 (defvar vip-d-com nil
)
664 ;; The character remembered by the Vi `r' command.
665 (defvar vip-d-char nil
)
667 ;; Name of register to store deleted or yanked strings
668 (defvar vip-use-register nil
)
672 ;; Variables for Moves and Searches
674 ;; For use by `;' command.
675 (defvar vip-f-char nil
)
677 ;; For use by `.' command.
678 (defvar vip-F-char nil
)
680 ;; For use by `;' command.
681 (defvar vip-f-forward nil
)
683 ;; For use by `;' command.
684 (defvar vip-f-offset nil
)
686 ;; Last search string
687 (defvar vip-s-string
"")
689 (defvar vip-quote-string
"> "
690 "String inserted at the beginning of quoted region.")
692 ;; If t, search is forward.
693 (defvar vip-s-forward nil
)
695 (defconst vip-case-fold-search nil
696 "*If t, search ignores cases.")
698 (defconst vip-re-search t
699 "*If t, search is reg-exp search, otherwise vanilla search.")
701 (defconst vip-re-query-replace t
702 "*If t then do regexp replace, if nil then do string replace.")
704 (defconst vip-re-replace t
705 "*If t, do regexp replace. nil means do string replace.")
707 (vip-deflocalvar vip-ex-style-motion t
708 "*Ex-style: the commands l,h do not cross lines, etc.")
710 (vip-deflocalvar vip-ex-style-editing-in-insert t
711 "*The keys ^H, ^? don't jump lines in insert, ESC moves cursor back, etc.
712 Note: this doesn't preclude ^H and ^? from deleting characters by moving
713 past the insertion point. This is a feature, not a bug. ")
715 (vip-deflocalvar vip-delete-backwards-in-replace nil
716 "*If t, DEL key will delete characters while moving the cursor backwards.
717 If nil, the cursor will move backwards without deleting anything.")
719 (defconst vip-buffer-search-char nil
720 "*Key bound for buffer-searching.")
722 (defconst vip-search-wrap-around-t t
723 "*If t, search wraps around.")
725 (vip-deflocalvar vip-related-files-and-buffers-ring nil
726 "*Ring of file and buffer names that are considered to be related to the
728 These buffers can be cycled through via :R and :P commands.")
729 (put 'vip-related-files-and-buffers-ring
'permanent-local t
)
731 ;; Used to find out if we are done with searching the current buffer.
732 (vip-deflocalvar vip-local-search-start-marker nil
)
733 ;; As above, but global
734 (defvar vip-search-start-marker
(make-marker))
736 ;; the search overlay
737 (vip-deflocalvar vip-search-overlay nil
)
740 (defvar vip-heading-start
741 (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
742 "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
743 "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
744 "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
745 "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
747 "*Regexps for Headings. Used by \[\[ and \]\].")
749 (defvar vip-heading-end
750 (concat "^}\\|" ; C/C++
751 "^\\\\end{\\|" ; latex
752 "^@end \\|" ; texinfo
753 ")\n\n[ \t\n]*\\|" ; lisp
754 "\\.\\s-*$") ; prolog
755 "*Regexps to end Headings/Sections. Used by \[\].")
758 ;; These two vars control the interaction of jumps performed by ' and `.
759 ;; In this new version, '' doesn't erase the marks set by ``, so one can
760 ;; use both kinds of jumps interchangeably and without loosing positions
763 ;; Remembers position of the last jump done using ``'.
764 (vip-deflocalvar vip-last-jump nil
)
765 ;; Remembers position of the last jump done using `''.
766 (vip-deflocalvar vip-last-jump-ignore
0)
768 ;; Some common error messages
770 (defconst vip-SpuriousText
"Spurious text after command" "")
771 (defconst vip-BadExCommand
"Not an editor command" "")
772 (defconst vip-InvalidCommandArgument
"Invalid command argument" "")
773 (defconst vip-NoPrevSearch
"No previous search string" "")
774 (defconst vip-EmptyRegister
"`%c': Nothing in this register" "")
775 (defconst vip-InvalidRegister
"`%c': Invalid register" "")
776 (defconst vip-EmptyTextmarker
"`%c': Text marker doesn't point anywhere" "")
777 (defconst vip-InvalidTextmarker
"`%c': Invalid text marker" "")
778 (defconst vip-InvalidViCommand
"Invalid command" "")
779 (defconst vip-BadAddress
"Ill-formed address" "")
780 (defconst vip-FirstAddrExceedsSecond
"First address exceeds second" "")
781 (defconst vip-NoFileSpecified
"No file specified" "")
786 (defvar vip-history nil
)
787 ;; History of search strings.
788 (defvar vip-search-history
(list ""))
789 ;; History of query-replace strings used as a source.
790 (defvar vip-replace1-history nil
)
791 ;; History of query-replace strings used as replacement.
792 (defvar vip-replace2-history nil
)
793 ;; History of region quoting strings.
794 (defvar vip-quote-region-history
(list vip-quote-string
))
795 ;; History of Ex-style commands.
796 (defvar vip-ex-history nil
)
797 ;; History of shell commands.
798 (defvar vip-shell-history nil
)
801 ;; Last shell command. There are two of these, one for Ex (in viper-ex)
804 ;; Last shell command executed with ! command.
805 (defvar vip-last-shell-com nil
)
811 ;; setup emacs-supported vi-style feel
812 (setq mark-even-if-inactive t
813 next-line-add-newlines nil
814 require-final-newline t
)
816 (defvar vip-inhibit-startup-message nil
817 "Whether Viper startup message should be inhibited.")
820 "t means, arrange that vi-state will be a default.")
822 (defvar vip-custom-file-name
"~/.vip"
823 "Viper customisation file.
824 This variable must be set _before_ loading Viper.")
826 (defvar vip-info-file-name
"viper"
827 "The name prefix for Viper Info files.")
829 (defvar vip-spell-function
'ispell-region
830 "Spell function used by #s<move> command to spell.")
832 (defvar vip-tags-file-name
"TAGS")
836 (defvar vip-vi-style-in-minibuffer t
837 "If t, use vi-style editing in minibuffer.
838 Should be set in `~/.vip' file.")
840 ;; overlay used in the minibuffer to indicate which state it is in
841 (vip-deflocalvar vip-minibuffer-overlay nil
)
843 ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
844 ;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
845 ;; *after* exiting the minibuffer
846 (defvar vip-minibuffer-exit-hook nil
)
848 (vip-deflocalvar vip-vi-minibuffer-minor-mode nil
849 "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
850 (vip-deflocalvar vip-insert-minibuffer-minor-mode nil
851 "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
853 (vip-deflocalvar vip-add-newline-at-eob t
854 "If t, always add a newline at the end of buffer.
855 Usually, Viper adds a newline character at the end of the last
856 line in a buffer, if it's missing. In some major modes, however, like
857 shell-mode, this is undesirable and must be set to nil. See vip-set-hooks.")
861 (defconst vip-vi-state-id
"<V> "
862 "Mode line tag identifying the Vi mode of Viper.")
863 (defconst vip-emacs-state-id
"<E> "
864 "Mode line tag identifying the Emacs mode of Viper.")
865 (defconst vip-insert-state-id
"<I> "
866 "Mode line tag identifying the Insert mode of Viper.")
867 (defconst vip-replace-state-id
"<R> "
868 "Mode line tag identifying the Replace mode of Viper.")
870 ;; Viper changes the default mode-line-buffer-identification
871 (setq-default mode-line-buffer-identification
'(" %b"))
873 ;; Variable displaying the current Viper state in the mode line.
874 (vip-deflocalvar vip-mode-string vip-emacs-state-id
)
875 (or (memq 'vip-mode-string global-mode-string
)
876 (setq global-mode-string
877 (append '("" vip-mode-string
) (cdr global-mode-string
))))
880 (defvar vip-vi-state-hooks nil
881 "*Hooks run just before the switch to Vi mode is completed.")
882 (defvar vip-insert-state-hooks nil
883 "*Hooks run just before the switch to Insert mode is completed.")
884 (defvar vip-replace-state-hooks nil
885 "*Hooks run just before the switch to Replace mode is completed.")
886 (defvar vip-emacs-state-hooks nil
887 "*Hooks run just before the switch to Emacs mode is completed.")
889 (defvar vip-load-hooks nil
890 "Hooks run just after loading Viper.")
893 ;; Generic predicates
895 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
897 ;; generate test functions
898 ;; given symbol foo, foo-p is the test function, foos is the set of
899 ;; Viper command keys
900 ;; (macroexpand '(vip-test-com-defun foo))
901 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
903 (defmacro vip-test-com-defun
(name)
904 (let* (;;(snm (make-symbol "s1"))
905 (snm (symbol-name name
))
906 ;;(nm-p (make-symbol "s2"))
907 (nm-p (intern (concat snm
"-p")))
908 ;;(nms (make-symbol "s3"))
909 (nms (intern (concat snm
"s"))))
910 (` (defun (, nm-p
) (com)
911 (consp (memq (if (< com
0) (- com
) com
) (, nms
)))))))
913 ;; Variables for defining VI commands
915 (defconst vip-prefix-commands
'(?c ?d ?y ?
! ?
= ?
# ?
< ?
> ?
\")
916 "Modifying commands that can be prefixes to movement commands")
917 (vip-test-com-defun vip-prefix-command)
919 (defconst vip-charpair-commands
'(?c ?d ?y ?
! ?
= ?
< ?
> ?r ?R
)
920 "Commands that are pairs eg. dd. r and R here are a hack")
921 (vip-test-com-defun vip-charpair-command)
923 (defconst vip-movement-commands
'(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
924 ?H ?M ?n ?t ?T ?w ?W ?$ ?%
925 ?^ ?
( ?
) ?- ?
+ ?| ?
{ ?
} ?
[ ?
] ?
' ?
`
929 (vip-test-com-defun vip-movement-command)
931 (defconst vip-dotable-commands
'(?c ?d ?C ?D ?
> ?
<)
932 "Commands that can be repeated by .(dotted)")
933 (vip-test-com-defun vip-dotable-command)
935 (defconst vip-hash-cmds
'(?c ?C ?g ?q ?S
)
936 "Commands that can follow a #")
937 (vip-test-com-defun vip-hash-cmd)
939 (defconst vip-regsuffix-commands
'(?d ?y ?Y ?D ?p ?P ?x ?X
)
940 "Commands that may have registers as prefix")
941 (vip-test-com-defun vip-regsuffix-command)
945 ;;; Arrange the keymaps
946 (require 'viper-keym
)
953 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
954 (defun vip-change-state (new-state)
955 ;; keep them always fresh
956 (add-hook 'post-command-hook
'vip-post-command-sentinel t
)
957 (add-hook 'pre-command-hook
'vip-pre-command-sentinel t
)
958 ;; These hooks will be added back if switching to insert/replace mode
959 (remove-hook 'vip-post-command-hooks
960 'vip-insert-state-post-command-sentinel
)
961 (remove-hook 'vip-pre-command-hooks
962 'vip-insert-state-pre-command-sentinel
)
963 (cond ((eq new-state
'vi-state
)
964 (cond ((member vip-current-state
'(insert-state replace-state
))
966 ;; move vip-last-posn-while-in-insert-state
967 ;; This is a normal hook that is executed in insert/replace
968 ;; states after each command. In Vi/Emacs state, it does
969 ;; nothing. We need to execute it here to make sure that
970 ;; the last posn was recorded when we hit ESC.
971 ;; It may be left unrecorded if the last thing done in
972 ;; insert/repl state was dabbrev-expansion or abbrev
973 ;; expansion caused by hitting ESC
974 (vip-insert-state-post-command-sentinel)
976 (condition-case conds
978 (vip-save-last-insertion
980 vip-last-posn-while-in-insert-state
)
981 (if vip-began-as-replace
982 (setq vip-began-as-replace nil
)
983 ;; repeat insert commands if numerical arg > 1
985 (vip-repeat-insert-command))))
987 (vip-message-conditions conds
)))
989 (if (> (length vip-last-insertion
) 0)
990 (vip-push-onto-ring vip-last-insertion
991 'vip-insertion-ring
))
993 (if vip-ex-style-editing-in-insert
994 (or (bolp) (backward-char 1))))
998 ((memq new-state
'(insert-state replace-state
))
999 (if (memq vip-current-state
'(emacs-state vi-state
))
1000 (vip-move-marker-locally 'vip-insert-point
(point)))
1001 (vip-move-marker-locally 'vip-last-posn-while-in-insert-state
(point))
1002 (add-hook 'vip-post-command-hooks
1003 'vip-insert-state-post-command-sentinel t
)
1004 (add-hook 'vip-pre-command-hooks
1005 'vip-insert-state-pre-command-sentinel t
)
1009 ;; Nothing needs to be done to switch to emacs mode! Just set some
1010 ;; variables, which is done in vip-change-state-to-emacs!
1012 (setq vip-current-state new-state
)
1013 (vip-normalize-minor-mode-map-alist)
1014 (vip-adjust-keys-for new-state
)
1015 (vip-set-mode-vars-for new-state
)
1016 (vip-refresh-mode-line)
1021 (defun vip-adjust-keys-for (state)
1022 "Make necessary adjustments to keymaps before entering STATE."
1023 (cond ((memq state
'(insert-state replace-state
))
1026 (define-key vip-insert-basic-map
"\C-m" 'vip-autoindent
)
1027 (if vip-want-emacs-keys-in-insert
1029 (define-key vip-insert-basic-map
"\C-j" nil
)
1031 (define-key vip-insert-basic-map
"\C-j" 'vip-autoindent
))))
1033 (setq vip-insert-diehard-minor-mode
1034 (not vip-want-emacs-keys-in-insert
))
1036 (if vip-want-ctl-h-help
1038 (define-key vip-insert-basic-map
"\C-h" 'help-command
)
1039 (define-key vip-replace-map
"\C-h" 'help-command
))
1040 (define-key vip-insert-basic-map
1041 "\C-h" 'vip-del-backward-char-in-insert
)
1042 (define-key vip-replace-map
1043 "\C-h" 'vip-del-backward-char-in-replace
)))
1046 (setq vip-vi-diehard-minor-mode
(not vip-want-emacs-keys-in-vi
))
1047 (if vip-want-ctl-h-help
1048 (define-key vip-vi-basic-map
"\C-h" 'help-command
)
1049 (define-key vip-vi-basic-map
"\C-h" 'vip-backward-char
)))
1053 (defun vip-normalize-minor-mode-map-alist ()
1054 "Normalizes minor-mode-map-alist by putting Viper keymaps first.
1055 This ensures that Viper bindings are in effect, regardless of which minor
1056 modes were turned on by the user or by other packages."
1057 (setq minor-mode-map-alist
1058 (vip-append-filter-alist
1060 (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map
)
1061 (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map
)
1062 (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map
)
1063 (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map
)
1064 (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map
)
1065 (cons 'vip-vi-state-modifier-minor-mode
1067 (cdr (assoc major-mode vip-vi-state-modifier-alist
)))
1068 (cdr (assoc major-mode vip-vi-state-modifier-alist
))
1070 (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map
)
1071 (cons 'vip-vi-basic-minor-mode vip-vi-basic-map
)
1072 (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map
)
1073 (cons 'vip-replace-minor-mode vip-replace-map
)
1074 ;; vip-insert-minibuffer-minor-mode must come after
1075 ;; vip-replace-minor-mode
1076 (cons 'vip-insert-minibuffer-minor-mode
1078 (cons 'vip-insert-local-user-minor-mode
1079 vip-insert-local-user-map
)
1080 (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map
)
1081 (cons 'vip-insert-global-user-minor-mode
1082 vip-insert-global-user-map
)
1083 (cons 'vip-insert-state-modifier-minor-mode
1086 (assoc major-mode vip-insert-state-modifier-alist
)))
1088 (assoc major-mode vip-insert-state-modifier-alist
))
1090 (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map
)
1091 (cons 'vip-insert-basic-minor-mode vip-insert-basic-map
)
1092 (cons 'vip-emacs-intercept-minor-mode
1093 vip-emacs-intercept-map
)
1094 (cons 'vip-emacs-local-user-minor-mode
1095 vip-emacs-local-user-map
)
1096 (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map
)
1097 (cons 'vip-emacs-global-user-minor-mode
1098 vip-emacs-global-user-map
)
1099 (cons 'vip-emacs-state-modifier-minor-mode
1102 (assoc major-mode vip-emacs-state-modifier-alist
)))
1104 (assoc major-mode vip-emacs-state-modifier-alist
))
1107 minor-mode-map-alist
)))
1113 ;; Viper mode-changing commands and utilities
1115 (defun vip-refresh-mode-line ()
1116 "Modifies mode-line-buffer-identification."
1117 (setq vip-mode-string
1118 (cond ((eq vip-current-state
'emacs-state
) vip-emacs-state-id
)
1119 ((eq vip-current-state
'vi-state
) vip-vi-state-id
)
1120 ((eq vip-current-state
'replace-state
) vip-replace-state-id
)
1121 ((eq vip-current-state
'insert-state
) vip-insert-state-id
)))
1123 ;; Sets Viper mode string in global-mode-string
1124 (force-mode-line-update))
1127 (defun viper-mode ()
1128 "Turn on Viper emulation of Vi."
1130 (if (not noninteractive
)
1132 (if vip-first-time
; This check is important. Without it, startup and
1133 (progn ; expert-level msgs mix up when viper-mode recurses
1134 (setq vip-first-time nil
)
1135 (if (not vip-inhibit-startup-message
)
1136 (save-window-excursion
1137 (setq vip-inhibit-startup-message t
)
1138 (delete-other-windows)
1139 (switch-to-buffer "Viper Startup Message")
1142 (substitute-command-keys
1143 "Viper Is a Package for Emacs Rebels.
1144 It is also a VI Plan for Emacs Rescue and a venomous VI PERil.
1146 Technically speaking, Viper is a Vi emulation package for Emacs 19 and
1147 XEmacs 19. It supports virtually all of Vi and Ex functionality, extending
1148 and improving upon much of it.
1150 1. Viper supports Vi at several levels. Level 1 is the closest to
1151 Vi, level 5 provides the most flexibility to depart from many Vi
1154 You will be asked to specify your user level in a following screen.
1156 If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will
1157 behave as in VI, to smooth transition to Viper for the beginners.
1158 However, to use Emacs productively, you are advised to reach user
1161 If your user level is 2 or higher, ^X and ^C will invoke Emacs
1162 functions,as usual in Emacs; ^Z will toggle vi/emacs modes, and
1163 ^G will be the usual Emacs's keyboard-quit (something like ^C in VI).
1165 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
1166 do not cause Emacs to quit, except at user level 1 (a novice).
1167 3. ^X^C EXITS EMACS.
1168 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
1169 undo. Another `u' changes direction.
1171 6. Emacs Meta functions are invoked by typing `_' or `\\ ESC'.
1172 On a window system, the best way is to use the Meta-key.
1173 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,
1174 if something funny happens. This would abort the current editing
1177 You can get more information on Viper by:
1179 a. Typing `:help' in Vi state
1180 b. Printing Viper manual, found in ./etc/viper.dvi
1181 c. Printing ViperCard, the Quick Reference, found in ./etc/viperCard.dvi
1183 This startup message appears whenever you load Viper, unless you type `y' now."
1185 (goto-char (point-min))
1186 (if (y-or-n-p "Inhibit Viper startup message? ")
1188 'vip-inhibit-startup-message
1189 "Viper startup message inhibited"
1190 vip-custom-file-name t
))
1191 (kill-buffer (current-buffer))))
1193 (vip-set-expert-level 'dont-change-unless
)))
1194 (vip-change-state-to-vi))))
1197 (defalias 'vip-mode
'viper-mode
)
1200 (defun vip-exit-insert-state ()
1201 "Switch from Insert state to Vi state."
1203 (vip-change-state-to-vi))
1205 (defun vip-set-mode-vars-for (state)
1206 "Sets Viper minor mode variables to put Viper's state STATE in effect."
1209 (setq vip-vi-minibuffer-minor-mode nil
1210 vip-insert-minibuffer-minor-mode nil
1211 vip-vi-intercept-minor-mode nil
1212 vip-insert-intercept-minor-mode nil
1214 vip-vi-local-user-minor-mode nil
1215 vip-vi-kbd-minor-mode nil
1216 vip-vi-global-user-minor-mode nil
1217 vip-vi-state-modifier-minor-mode nil
1218 vip-vi-diehard-minor-mode nil
1219 vip-vi-basic-minor-mode nil
1221 vip-replace-minor-mode nil
1223 vip-insert-local-user-minor-mode nil
1224 vip-insert-kbd-minor-mode nil
1225 vip-insert-global-user-minor-mode nil
1226 vip-insert-state-modifier-minor-mode nil
1227 vip-insert-diehard-minor-mode nil
1228 vip-insert-basic-minor-mode nil
1229 vip-emacs-intercept-minor-mode t
1230 vip-emacs-local-user-minor-mode t
1231 vip-emacs-kbd-minor-mode
(not (vip-is-in-minibuffer))
1232 vip-emacs-global-user-minor-mode t
1233 vip-emacs-state-modifier-minor-mode t
1237 (if (eq state
'vi-state
) ; adjust for vi-state
1239 vip-vi-intercept-minor-mode t
1240 vip-vi-minibuffer-minor-mode
(vip-is-in-minibuffer)
1241 vip-vi-local-user-minor-mode t
1242 vip-vi-kbd-minor-mode
(not (vip-is-in-minibuffer))
1243 vip-vi-global-user-minor-mode t
1244 vip-vi-state-modifier-minor-mode t
1245 ;; don't let the diehard keymap block command completion
1246 ;; and other things in the minibuffer
1247 vip-vi-diehard-minor-mode
(not
1248 (or vip-want-emacs-keys-in-vi
1249 (vip-is-in-minibuffer)))
1250 vip-vi-basic-minor-mode t
1251 vip-emacs-intercept-minor-mode nil
1252 vip-emacs-local-user-minor-mode nil
1253 vip-emacs-kbd-minor-mode nil
1254 vip-emacs-global-user-minor-mode nil
1255 vip-emacs-state-modifier-minor-mode nil
1258 ;; Insert and Replace states
1259 (if (member state
'(insert-state replace-state
))
1261 vip-insert-intercept-minor-mode t
1262 vip-replace-minor-mode
(eq state
'replace-state
)
1263 vip-insert-minibuffer-minor-mode
(vip-is-in-minibuffer)
1264 vip-insert-local-user-minor-mode t
1265 vip-insert-kbd-minor-mode
(not (vip-is-in-minibuffer))
1266 vip-insert-global-user-minor-mode t
1267 vip-insert-state-modifier-minor-mode t
1268 ;; don't let the diehard keymap block command completion
1269 ;; and other things in the minibuffer
1270 vip-insert-diehard-minor-mode
(not
1271 (or vip-want-emacs-keys-in-insert
1272 (vip-is-in-minibuffer)))
1273 vip-insert-basic-minor-mode t
1274 vip-emacs-intercept-minor-mode nil
1275 vip-emacs-local-user-minor-mode nil
1276 vip-emacs-kbd-minor-mode nil
1277 vip-emacs-global-user-minor-mode nil
1278 vip-emacs-state-modifier-minor-mode nil
1283 (setq vip-minibuffer-current-face
1284 (cond ((eq state
'emacs-state
) vip-minibuffer-emacs-face
)
1285 ((eq state
'vi-state
) vip-minibuffer-vi-face
)
1286 ((memq state
'(insert-state replace-state
))
1287 vip-minibuffer-insert-face
))))
1289 (if (vip-is-in-minibuffer)
1290 (vip-set-minibuffer-overlay))
1293 ;; This also takes care of the annoying incomplete lines in files.
1294 ;; Also, this fixed 'undo' to work vi-style for complex commands.
1295 (defun vip-change-state-to-vi ()
1296 "Change Viper state to Vi."
1298 (if (and vip-first-time
(not (vip-is-in-minibuffer)))
1300 (if overwrite-mode
(overwrite-mode nil
))
1301 (if abbrev-mode
(expand-abbrev))
1302 (if (and auto-fill-function
(> (current-column) fill-column
))
1303 (funcall auto-fill-function
))
1304 (vip-add-newline-at-eob-if-necessary)
1305 (if vip-undo-needs-adjustment
(vip-adjust-undo))
1306 (vip-change-state 'vi-state
)
1307 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
1308 (iso-accents-mode -
1)) ; turn off iso accents
1310 ;; Protection against user errors in hooks
1311 (condition-case conds
1312 (run-hooks 'vip-vi-state-hooks
)
1314 (vip-message-conditions conds
)))))
1316 (defun vip-change-state-to-insert ()
1317 "Change Viper state to Insert."
1319 (vip-change-state 'insert-state
)
1320 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
1321 (iso-accents-mode 1)) ; turn iso accents on
1323 ;; Protection against user errors in hooks
1324 (condition-case conds
1325 (run-hooks 'vip-insert-state-hooks
)
1327 (vip-message-conditions conds
))))
1329 (defsubst vip-downgrade-to-insert
()
1330 (setq vip-current-state
'insert-state
1331 vip-replace-minor-mode nil
)
1336 ;; Change to replace state. When the end of replacement region is reached,
1337 ;; replace state changes to insert state.
1338 (defun vip-change-state-to-replace (&optional non-R-cmd
)
1339 (vip-change-state 'replace-state
)
1340 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
1341 (iso-accents-mode 1)) ; turn iso accents on
1342 ;; Run insert-state-hook
1343 (condition-case conds
1344 (run-hooks 'vip-insert-state-hooks
'vip-replace-state-hooks
)
1346 (vip-message-conditions conds
)))
1350 ;; 'R' is implemented using Emacs's overwrite-mode
1355 (defun vip-change-state-to-emacs ()
1356 "Change Viper state to Emacs."
1358 (vip-change-state 'emacs-state
)
1359 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
1360 (iso-accents-mode 1)) ; turn iso accents on
1362 ;; Protection agains user errors in hooks
1363 (condition-case conds
1364 (run-hooks 'vip-emacs-state-hooks
)
1366 (vip-message-conditions conds
))))
1368 ;; escape to emacs mode termporarily
1369 (defun vip-escape-to-emacs (arg &optional events
)
1370 "Escape to Emacs state from Vi state for one Emacs command.
1371 ARG is used as the prefix value for the executed command. If
1372 EVENTS is a list of events, which become the beginning of the command."
1374 (vip-escape-to-state arg events
'emacs-state
))
1376 ;; escape to Vi mode termporarily
1377 (defun vip-escape-to-vi ()
1378 "Escape from Emacs state to Vi state for one Vi 1-character command.
1379 This doesn't work with prefix arguments or most complex commands like
1380 cw, dw, etc. But it does work with some 2-character commands,
1383 (vip-escape-to-state nil nil
'vi-state
))
1385 ;; Escape to STATE mode for one Emacs command.
1386 (defun vip-escape-to-state (arg events state
)
1387 (let (com key prefix-arg
)
1388 ;; this temporarily turns off Viper's minor mode keymaps
1389 (vip-set-mode-vars-for state
)
1390 (vip-normalize-minor-mode-map-alist)
1391 (if events
(vip-set-unread-command-events events
))
1393 ;; protect against keyboard quit and other errors
1398 (setq com
(key-binding (setq key
1400 (read-key-sequence nil
)
1401 (read-key-sequence nil t
)))))
1402 ;; In case of indirection--chase definitions.
1403 ;; Have to do it here because we execute this command under
1404 ;; different keymaps, so command-execute may not do the
1405 ;; right thing there
1406 (while (vectorp com
) (setq com
(key-binding com
))))
1408 ;; exec command in the right Viper state
1409 ;; otherwise, if we switch buffers in the escaped command,
1410 ;; Viper's mode vars will remain those of `state'. When we return
1411 ;; to the orig buffer, the bindings will be screwed up.
1412 (vip-set-mode-vars-for vip-current-state
)
1414 ;; this-command, last-command-char, last-command-event
1415 (setq this-command com
)
1416 (if vip-xemacs-p
; XEmacs represents key sequences as vectors
1417 (setq last-command-event
(vip-seq-last-elt key
)
1418 last-command-char
(event-to-character last-command-event
))
1419 ;; Emacs represents them as sequences (str or vec)
1420 (setq last-command-event
(vip-seq-last-elt key
)
1421 last-command-char last-command-event
))
1425 (setq prefix-arg arg
)
1426 (command-execute com
)))
1430 (vip-set-mode-vars-for vip-current-state
)) ; set state in new buffer
1432 (defun vip-exec-form-in-emacs (form)
1433 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
1434 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
1435 (let ((buff (current-buffer))
1437 (vip-set-mode-vars-for 'emacs-state
)
1438 (setq result
(eval form
))
1439 (if (not (equal buff
(current-buffer))) ; cmd switched buffer
1442 (vip-set-mode-vars-for vip-current-state
)))
1443 (vip-set-mode-vars-for vip-current-state
)
1448 ;; This is needed because minor modes sometimes override essential Viper
1449 ;; bindings. By letting Viper know which files these modes are in, it will
1450 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
1451 (defun vip-harness-minor-mode (load-file)
1452 "Familiarize Viper with a minor mode defined in LOAD_FILE.
1453 Minor modes that have their own keymaps may overshadow Viper keymaps.
1454 This function is designed to make Viper aware of the packages that define
1457 (vip-harness-minor-mode load-file)
1459 LOAD-FILE is a name of the file where the specific minor mode is defined.
1460 Suffixes such as .el or .elc should be stripped."
1462 (interactive "sEnter name of the load file: ")
1464 (vip-eval-after-load load-file
'(vip-normalize-minor-mode-map-alist))
1466 ;; This is a work-around the emacs bug that doesn't let us make
1467 ;; minor-mode-map-alist permanent-local.
1468 ;; This workaround changes the default for minor-mode-map-alist
1469 ;; each time a harnessed minor mode adds its own keymap to the a-list.
1470 (vip-eval-after-load load-file
'(setq-default minor-mode-map-alist
1471 minor-mode-map-alist
))
1474 ;; This doesn't work, i.e., doesn't replace vip-harness-minor-mode
1475 ;; function, since autoloaded files don't seem to be loaded with lisp's
1477 ;;(defadvice load (after vip-load-advice activate)
1478 ;; "Rearrange `minor-mode-map-alist' after loading a file or a library."
1479 ;; (vip-normalize-minor-mode-map-alist)
1480 ;; (setq-default minor-mode-map-alist minor-mode-map-alist))
1484 (defun vip-ESC (arg)
1485 "Emulate ESC key in Emacs.
1486 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true. In that
1487 case \@ will be bound to ESC. If vip-no-multiple-ESC is 'twice double ESC
1488 would dings in vi-state. Other ESC sequences are emulated via the current
1489 Emacs's major mode keymap. This is more convenient on dumb terminals and in
1490 Emacs -nw, since this won't block functional keys such as up,down,
1491 etc. Meta key also will work. When vip-no-multiple-ESC is nil, ESC key
1492 behaves as in Emacs, any number of multiple escapes is allowed."
1495 (cond ((and (not vip-no-multiple-ESC
) (eq vip-current-state
'vi-state
))
1496 (setq char
(vip-read-char-exclusive))
1497 (vip-escape-to-emacs arg
(list ?\e char
) ))
1498 ((and (eq vip-no-multiple-ESC
'twice
)
1499 (eq vip-current-state
'vi-state
))
1500 (setq char
(vip-read-char-exclusive))
1501 (if (= char
(string-to-char vip-ESC-key
))
1503 (vip-escape-to-emacs arg
(list ?\e char
) )))
1507 (defun vip-alternate-ESC (arg)
1508 "ESC key without checking for multiple keystrokes."
1510 (vip-escape-to-emacs arg
'(?\e
)))
1513 ;; Intercept ESC sequences on dumb terminals.
1514 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
1516 ;; Check if last key was ESC and if so try to reread it as a function key.
1517 ;; But only if there are characters to read during a very short time.
1518 ;; Returns the last event, if any.
1519 (defun vip-envelop-ESC-key ()
1520 (let ((event last-input-event
)
1523 (if (vip-ESC-event-p event)
1525 (if (vip-fast-keysequence-p)
1527 (let ((vip-vi-intercept-minor-mode nil)
1528 (vip-insert-intercept-minor-mode nil)
1529 (vip-emacs-intercept-minor-mode nil)
1530 (vip-vi-state-modifier-minor-mode nil)
1531 (vip-vi-global-user-minor-mode nil)
1532 (vip-vi-local-user-minor-mode nil)
1533 (vip-replace-minor-mode nil) ; actually unnecessary
1534 (vip-insert-state-modifier-minor-mode nil)
1535 (vip-insert-global-user-minor-mode nil)
1536 (vip-insert-local-user-minor-mode nil)
1537 (vip-emacs-state-modifier-minor-mode nil)
1538 (vip-emacs-global-user-minor-mode nil)
1539 (vip-emacs-local-user-minor-mode nil)
1541 ;; The treatment of XEmacs, below, is temporary, since we
1542 ;; don't know how XEmacs will implement dumb terminals.
1543 ;; Note: the treatment of fast keysequences here is
1544 ;; needed only on dumb terminals in order to be able to
1545 ;; handle function keys correctly.
1547 (setq keyseq (vector event))
1548 (vip-set-unread-command-events event)
1551 (ad-get-orig-definition 'read-key-sequence) nil))
1553 ;; If keyseq translates into something that still has ESC
1554 ;; in the beginning, separate ESC from the rest of the seq.
1556 ;; This is needed for the following reason:
1557 ;; If ESC is the first symbol, we interpret it as if the
1558 ;; user typed ESC and then quickly some other symbols.
1559 ;; If ESC is not the first one, then the key sequence
1560 ;; entered was apparently translated into a function key or
1561 ;; something (e.g., one may have
1562 ;; (define-key function-key-map "\e[192z" [f11])
1563 ;; which would translate the escape-sequence generated by
1564 ;; f11 in an xterm window into the symbolic key f11.
1565 (if (vip-ESC-event-p (elt keyseq 0))
1567 ;; put keys following ESC on the unread list
1568 ;; and return ESC as the key-sequence
1569 (vip-set-unread-command-events (subseq keyseq 1))
1570 (setq last-input-event event
1574 ;; this is escape event with nothing after it
1575 ;; put in unread-command-event and then re-read
1576 (vip-set-unread-command-events event)
1578 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
1580 ;; not an escape event
1581 (setq keyseq (vector event)))
1586 (defadvice read-key-sequence (around vip-read-key-sequence-ad activate)
1587 (let (inhibit-quit event keyseq)
1588 (setq keyseq ad-do-it)
1589 (setq event (if vip-xemacs-p
1590 (elt keyseq 0) ; XEmacs returns vector of events
1591 (elt (listify-key-sequence keyseq) 0)))
1592 (if (vip-ESC-event-p event)
1593 (let (unread-command-events unread-command-event)
1594 (vip-set-unread-command-events keyseq)
1595 (if (vip-fast-keysequence-p)
1596 (let ((vip-vi-global-user-minor-mode nil)
1597 (vip-vi-local-user-minor-mode nil)
1598 (vip-replace-minor-mode nil) ; actually unnecessary
1599 (vip-insert-global-user-minor-mode nil)
1600 (vip-insert-local-user-minor-mode nil))
1601 (setq keyseq ad-do-it))
1602 (setq keyseq ad-do-it))))
1605 (defadvice describe-key (before vip-read-key-sequence-ad protect activate)
1606 "Force `describe-key' to read key via `read-key-sequence'."
1607 (interactive (list (vip-events-to-keys
1608 (read-key-sequence "Describe key: ")))))
1610 (defadvice describe-key-briefly
1611 (before vip-read-key-sequence-ad protect activate)
1612 "Force `describe-key-briefly' to read key via `read-key-sequence'."
1613 (interactive (list (vip-events-to-keys
1614 (read-key-sequence "Describe key briefly: ")))))
1616 (defun vip-intercept-ESC-key ()
1618 If a sequence of keys starting with ESC is issued with very short delays,
1619 interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key."
1621 (let ((cmd (or (key-binding (vip-envelop-ESC-key))
1622 '(lambda () (interactive) (error "")))))
1624 ;; call the actual function to execute ESC (if no other symbols followed)
1625 ;; or the key bound to the ESC sequence (if the sequence was issued
1626 ;; with very short delay between characters.
1627 (if (eq cmd 'vip-intercept-ESC-key)
1629 (cond ((eq vip-current-state 'vi-state)
1631 ((eq vip-current-state 'insert-state)
1632 'vip-exit-insert-state)
1633 ((eq vip-current-state 'replace-state)
1634 'vip-replace-state-exit-cmd)
1635 (t 'vip-change-state-to-vi)
1637 (call-interactively cmd)))
1641 ;; prefix argument for Vi mode
1643 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
1644 ;; represents the numeric value of the prefix argument and COM represents
1645 ;; command prefix such as "c", "d", "m" and "y".
1647 (defun vip-prefix-arg-value (event com)
1648 "Compute numeric prefix arg value.
1649 Invoked by CHAR. COM is the command part obtained so far."
1651 ;; read while number
1652 (while (and (numberp event) (>= event ?0) (<= event ?9))
1653 (setq value (+ (* (if (numberp value) value 0) 10) (- event ?0)))
1654 (setq event (vip-read-event-convert-to-char)))
1656 (setq prefix-arg value)
1657 (if com (setq prefix-arg (cons prefix-arg com)))
1658 (while (eq event ?U)
1659 (vip-describe-arg prefix-arg)
1660 (setq event (vip-read-event-convert-to-char)))
1661 (vip-set-unread-command-events event)))
1663 (defun vip-prefix-arg-com (char value com)
1664 "Vi operator as prefix argument."
1668 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1669 vip-buffer-search-char)))
1671 ;; this means that we already have a command character, so we
1672 ;; construct a com list and exit while. however, if char is "
1675 ;; new com is (CHAR . OLDCOM)
1676 (if (memq char '(?# ?\")) (error ""))
1677 (setq com (cons char com))
1679 ;; If com is nil we set com as char, and read more. Again, if char
1680 ;; is ", we read the name of register and store it in vip-use-register.
1681 ;; if char is !, =, or #, a complete com is formed so we exit the
1683 (cond ((memq char '(?! ?=))
1685 (setq char (read-char))
1688 ;; read a char and encode it as com
1689 (setq com (+ 128 (read-char)))
1690 (setq char (read-char)))
1692 (let ((reg (read-char)))
1693 (if (vip-valid-register reg)
1694 (setq vip-use-register reg)
1696 (setq char (read-char))))
1699 (setq char (vip-read-char-exclusive)))))))
1701 ;; com is a single char, so we construct prefix-arg
1702 ;; and if char is ?, describe prefix arg, otherwise exit by
1703 ;; pushing the char back
1705 (setq prefix-arg (cons value com))
1707 (vip-describe-arg prefix-arg)
1708 (setq char (read-char)))
1709 (vip-set-unread-command-events char)
1711 ;; as com is non-nil, this means that we have a command to execute
1712 (if (memq (car com) '(?r ?R))
1713 ;; execute apropriate region command.
1714 (let ((char (car com)) (com (cdr com)))
1715 (setq prefix-arg (cons value com))
1716 (if (= char ?r) (vip-region prefix-arg)
1717 (vip-Region prefix-arg))
1719 (setq prefix-arg nil))
1720 ;; otherwise, reset prefix arg and call appropriate command
1721 (setq value (if (null value) 1 value))
1722 (setq prefix-arg nil)
1723 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1724 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1725 ((equal com '(?d . ?y)) (vip-yank-defun))
1726 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1727 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1728 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1729 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1730 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1733 (defun vip-describe-arg (arg)
1735 (setq val (vip-P-val arg)
1736 com (vip-getcom arg))
1739 (message "Value is nil, and command is nil")
1740 (message "Value is nil, and command is `%c'" com))
1742 (message "Value is `%d', and command is nil" val)
1743 (message "Value is `%d', and command is `%c'" val com)))))
1745 (defun vip-digit-argument (arg)
1746 "Begin numeric argument for the next command."
1748 (vip-prefix-arg-value last-command-char
1749 (if (consp arg) (cdr arg) nil)))
1751 (defun vip-command-argument (arg)
1752 "Accept a motion command as an argument."
1757 (cond ((null arg) nil)
1758 ((consp arg) (car arg))
1760 (t (error vip-InvalidCommandArgument)))
1761 (cond ((null arg) nil)
1762 ((consp arg) (cdr arg))
1764 (t (error vip-InvalidCommandArgument))))
1765 (quit (setq vip-use-register nil)
1766 (signal 'quit nil)))
1767 (vip-deactivate-mark))
1769 ;; Get value part of prefix-argument ARG.
1770 (defsubst vip-p-val (arg)
1771 (cond ((null arg) 1)
1772 ((consp arg) (if (null (car arg)) 1 (car arg)))
1775 ;; Get raw value part of prefix-argument ARG.
1776 (defsubst vip-P-val (arg)
1777 (cond ((consp arg) (car arg))
1780 ;; Get com part of prefix-argument ARG.
1781 (defsubst vip-getcom (arg)
1782 (cond ((null arg) nil)
1783 ((consp arg) (cdr arg))
1786 ;; Get com part of prefix-argument ARG and modify it.
1787 (defun vip-getCom (arg)
1788 (let ((com (vip-getcom arg)))
1789 (cond ((equal com ?c) ?C)
1795 ;; repeat last destructive command
1797 ;; Append region to text in register REG.
1798 ;; START and END are buffer positions indicating what to append.
1799 (defsubst vip-append-to-register (reg start end)
1800 (set-register reg (concat (or (get-register reg) "")
1801 (buffer-substring start end))))
1803 ;; define functions to be executed
1805 ;; invoked by C command
1806 (defun vip-exec-change (m-com com)
1807 ;; handle C cmd at the eol and at eob.
1808 (if (or (and (eolp) (= vip-com-point (point)))
1809 (= vip-com-point (point-max)))
1811 (insert " ")(backward-char 1)))
1812 (if (= vip-com-point (point))
1813 (vip-forward-char-carefully))
1815 (vip-change vip-com-point (point))
1816 (vip-change-subr vip-com-point (point))))
1818 ;; this is invoked by vip-substitute-line
1819 (defun vip-exec-Change (m-com com)
1821 (set-mark vip-com-point)
1822 (vip-enlarge-region (mark t) (point))
1823 (if vip-use-register
1825 (cond ((vip-valid-register vip-use-register '(letter digit))
1826 ;;(vip-valid-register vip-use-register '(letter)
1828 vip-use-register (mark t) (point) nil))
1829 ((vip-valid-register vip-use-register '(Letter))
1830 (vip-append-to-register
1831 (downcase vip-use-register) (mark t) (point)))
1832 (t (setq vip-use-register nil)
1833 (error vip-InvalidRegister vip-use-register)))
1834 (setq vip-use-register nil)))
1835 (delete-region (mark t) (point)))
1837 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
1839 (defun vip-exec-delete (m-com com)
1840 (if vip-use-register
1842 (cond ((vip-valid-register vip-use-register '(letter digit))
1843 ;;(vip-valid-register vip-use-register '(letter))
1845 vip-use-register vip-com-point (point) nil))
1846 ((vip-valid-register vip-use-register '(Letter))
1847 (vip-append-to-register
1848 (downcase vip-use-register) vip-com-point (point)))
1849 (t (setq vip-use-register nil)
1850 (error vip-InvalidRegister vip-use-register)))
1851 (setq vip-use-register nil)))
1853 (if (eq last-command 'd-command) 'kill-region nil))
1854 (kill-region vip-com-point (point))
1855 (setq this-command 'd-command)
1856 (if vip-ex-style-motion
1857 (if (and (eolp) (not (bolp))) (backward-char 1))))
1859 (defun vip-exec-Delete (m-com com)
1861 (set-mark vip-com-point)
1862 (vip-enlarge-region (mark t) (point))
1863 (if vip-use-register
1865 (cond ((vip-valid-register vip-use-register '(letter digit))
1866 ;;(vip-valid-register vip-use-register '(letter))
1868 vip-use-register (mark t) (point) nil))
1869 ((vip-valid-register vip-use-register '(Letter))
1870 (vip-append-to-register
1871 (downcase vip-use-register) (mark t) (point)))
1872 (t (setq vip-use-register nil)
1873 (error vip-InvalidRegister vip-use-register)))
1874 (setq vip-use-register nil)))
1876 (if (eq last-command 'D-command) 'kill-region nil))
1877 (kill-region (mark t) (point))
1878 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
1879 (back-to-indentation))
1881 (defun vip-exec-yank (m-com com)
1882 (if vip-use-register
1884 (cond ((vip-valid-register vip-use-register '(letter digit))
1885 ;; (vip-valid-register vip-use-register '(letter))
1887 vip-use-register vip-com-point (point) nil))
1888 ((vip-valid-register vip-use-register '(Letter))
1889 (vip-append-to-register
1890 (downcase vip-use-register) vip-com-point (point)))
1891 (t (setq vip-use-register nil)
1892 (error vip-InvalidRegister vip-use-register)))
1893 (setq vip-use-register nil)))
1894 (setq last-command nil)
1895 (copy-region-as-kill vip-com-point (point))
1896 (goto-char vip-com-point))
1898 (defun vip-exec-Yank (m-com com)
1900 (set-mark vip-com-point)
1901 (vip-enlarge-region (mark t) (point))
1902 (if vip-use-register
1904 (cond ((vip-valid-register vip-use-register '(letter digit))
1905 ;;(vip-valid-register vip-use-register '(letter))
1907 vip-use-register (mark t) (point) nil))
1908 ((vip-valid-register vip-use-register '(Letter))
1909 (vip-append-to-register
1910 (downcase vip-use-register) (mark t) (point)))
1911 (t (setq vip-use-register nil)
1912 (error vip-InvalidRegister vip-use-register)))
1913 (setq vip-use-register nil)))
1914 (setq last-command nil)
1915 (copy-region-as-kill (mark t) (point)))
1916 (goto-char vip-com-point))
1918 (defun vip-exec-bang (m-com com)
1920 (set-mark vip-com-point)
1921 (vip-enlarge-region (mark t) (point))
1922 (shell-command-on-region
1925 (setq vip-last-shell-com
1926 (vip-read-string-with-history
1930 (car vip-shell-history)
1935 (defun vip-exec-equals (m-com com)
1937 (set-mark vip-com-point)
1938 (vip-enlarge-region (mark t) (point))
1939 (if (> (mark t) (point)) (exchange-point-and-mark))
1940 (indent-region (mark t) (point) nil)))
1942 (defun vip-exec-shift (m-com com)
1944 (set-mark vip-com-point)
1945 (vip-enlarge-region (mark t) (point))
1946 (if (> (mark t) (point)) (exchange-point-and-mark))
1947 (indent-rigidly (mark t) (point)
1950 (- vip-shift-width)))))
1952 ;; this is needed because some commands fake com by setting it to ?r, which
1953 ;; denotes repeated insert command.
1954 (defsubst vip-exec-dummy (m-com com)
1957 (defun vip-exec-buffer-search (m-com com)
1958 (setq vip-s-string (buffer-substring (point) vip-com-point))
1959 (setq vip-s-forward t)
1960 (setq vip-search-history (cons vip-s-string vip-search-history))
1961 (vip-search vip-s-string vip-s-forward 1))
1963 (defvar vip-exec-array (make-vector 128 nil))
1965 ;; Using a dispatch array allows adding functions like buffer search
1966 ;; without affecting other functions. Buffer search can now be bound
1967 ;; to any character.
1969 (aset vip-exec-array ?c 'vip-exec-change)
1970 (aset vip-exec-array ?C 'vip-exec-Change)
1971 (aset vip-exec-array ?d 'vip-exec-delete)
1972 (aset vip-exec-array ?D 'vip-exec-Delete)
1973 (aset vip-exec-array ?y 'vip-exec-yank)
1974 (aset vip-exec-array ?Y 'vip-exec-Yank)
1975 (aset vip-exec-array ?r 'vip-exec-dummy)
1976 (aset vip-exec-array ?! 'vip-exec-bang)
1977 (aset vip-exec-array ?< 'vip-exec-shift)
1978 (aset vip-exec-array ?> 'vip-exec-shift)
1979 (aset vip-exec-array ?= 'vip-exec-equals)
1983 ;; This function is called by various movement commands to execute a
1984 ;; destructive command on the region specified by the movement command. For
1985 ;; instance, if the user types cw, then the command vip-forward-word will
1986 ;; call vip-execute-com to execute vip-exec-change, which eventually will
1987 ;; call vip-change to invoke the replace mode on the region.
1989 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
1990 ;; vip-d-com for later use by vip-repeat.
1991 (defun vip-execute-com (m-com val com)
1992 (let ((reg vip-use-register))
1993 ;; this is the special command `#'
1995 (vip-special-prefix-com (- com 128))
1996 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
1998 (error "%c: %s" com vip-InvalidViCommand)
1999 (funcall fn m-com com))))
2000 (if (vip-dotable-command-p com)
2001 (vip-set-destructive-command
2003 (if (memq com (list ?c ?C ?!)) (- com) com)
2008 (defun vip-repeat (arg)
2009 "Re-execute last destructive command.
2010 Use the info in vip-d-com, which has the form
2011 \(com val ch reg inserted-text command-keys\),
2012 where `com' is the command to be re-executed, `val' is the
2013 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
2014 if it exists, it is the name of the register for `com'.
2015 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
2017 (let ((save-point (point)) ; save point before repeating prev cmd
2018 ;; Pass along that we are repeating a destructive command
2019 ;; This tells vip-set-destructive-command not to update
2021 (vip-intermediate-command 'vip-repeat))
2022 (if (eq last-command 'vip-undo)
2023 ;; if the last command was vip-undo, then undo-more
2025 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
2026 ;; its prefix value is used as new prefix value for the command.
2027 (let ((m-com (car vip-d-com))
2028 (val (vip-P-val arg))
2029 (com (nth 2 vip-d-com))
2030 (reg (nth 3 vip-d-com)))
2031 (if (null val) (setq val (nth 1 vip-d-com)))
2032 (if (null m-com) (error "No previous command to repeat."))
2033 (setq vip-use-register reg)
2034 (if (nth 4 vip-d-com) ; text inserted by command
2035 (setq vip-last-insertion (nth 4 vip-d-com)
2036 vip-d-char (nth 4 vip-d-com)))
2037 (funcall m-com (cons val com))
2038 (if (and vip-keep-point-on-repeat (< save-point (point)))
2039 (goto-char save-point)) ; go back to before repeat.
2040 (if (and (eolp) (not (bolp)))
2043 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
2044 ;; If the prev cmd was rotating the command ring, this means that `.' has
2045 ;; just executed a command from that ring. So, push it on the ring again.
2046 ;; If we are just executing previous command , then don't push vip-d-com
2047 ;; because vip-d-com is not fully constructed in this case (its keys and
2048 ;; the inserted text may be nil). Besides, in this case, the command
2049 ;; executed by `.' is already on the ring.
2050 (if (eq last-command 'vip-display-current-destructive-command)
2051 (vip-push-onto-ring vip-d-com 'vip-command-ring))
2052 (vip-deactivate-mark)
2055 (defun vip-repeat-from-history ()
2056 "Repeat a destructive command from history.
2057 Doesn't change vip-command-ring in any way, so `.' will work as before
2058 executing this command.
2059 This command is supposed to be bound to a two-character Vi macro where
2060 the second character is a digit 0 to 9. The digit indicates which
2061 history command to execute. `<char>0' is equivalent to `.', `<char>1'
2062 invokes the command before that, etc."
2064 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
2065 (idx (cond (vip-this-kbd-macro
2067 (symbol-name (elt vip-this-kbd-macro 1))))
2070 (vip-d-com vip-d-com))
2072 (or (and (numberp num) (<= 0 num) (<= num 9))
2076 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'"))
2078 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
2079 (setq num (1- num)))
2082 (vip-special-ring-rotate1 vip-command-ring 1)
2083 (setq num (1+ num)))
2087 (defun vip-special-prefix-com (char)
2088 "This command is invoked interactively by the key sequence #<char>."
2090 (downcase-region (min vip-com-point (point))
2091 (max vip-com-point (point))))
2093 (upcase-region (min vip-com-point (point))
2094 (max vip-com-point (point))))
2096 (push-mark vip-com-point t)
2097 (vip-global-execute))
2099 (push-mark vip-com-point t)
2101 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
2102 (t (error "#%c: %s" char vip-InvalidViCommand))))
2108 "Undo previous change."
2111 (let ((modified (buffer-modified-p))
2112 (before-undo-pt (point-marker))
2113 (after-change-functions after-change-functions)
2114 undo-beg-posn undo-end-posn)
2116 ;; no need to remove this hook, since this var has scope inside a let.
2117 (add-hook 'after-change-functions
2118 '(lambda (beg end len)
2119 (setq undo-beg-posn beg
2120 undo-end-posn (or end beg))))
2124 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
2125 undo-end-posn (or undo-end-posn undo-beg-posn))
2127 (goto-char undo-beg-posn)
2129 (if (and vip-keep-point-on-undo
2130 (pos-visible-in-window-p before-undo-pt))
2132 (push-mark (point-marker) t)
2133 (vip-sit-for-short 300)
2134 (goto-char undo-end-posn)
2135 (vip-sit-for-short 300)
2136 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
2137 (> (abs (- undo-end-posn before-undo-pt)) 1))
2138 (goto-char before-undo-pt)
2139 (goto-char undo-beg-posn)))
2140 (push-mark before-undo-pt t))
2141 (if (and (eolp) (not (bolp))) (backward-char 1))
2142 (if (not modified) (set-buffer-modified-p t)))
2143 (setq this-command 'vip-undo))
2145 (defun vip-undo-more ()
2146 "Continue undoing previous changes."
2147 (message "undo more!")
2151 (message "No further undo information in this buffer")))
2152 (if (and (eolp) (not (bolp))) (backward-char 1))
2153 (setq this-command 'vip-undo))
2155 ;; The following two functions are used to set up undo properly.
2156 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
2157 ;; they are undone all at once.
2158 (defun vip-adjust-undo ()
2159 (let ((inhibit-quit t)
2161 (setq vip-undo-needs-adjustment nil)
2162 (if (listp buffer-undo-list)
2163 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
2165 (setq tmp2 (cdr tmp)) ; the part after mark
2167 ;; cut tail from buffer-undo-list temporarily by direct
2168 ;; manipulation with pointers in buffer-undo-list
2171 (setq buffer-undo-list (delq nil buffer-undo-list))
2172 (setq buffer-undo-list
2173 (delq vip-buffer-undo-list-mark buffer-undo-list))
2174 ;; restore tail of buffer-undo-list
2175 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
2176 (setq buffer-undo-list (delq nil buffer-undo-list))))))
2179 (defun vip-set-complex-command-for-undo ()
2180 (if (listp buffer-undo-list)
2181 (if (not vip-undo-needs-adjustment)
2182 (let ((inhibit-quit t))
2183 (setq buffer-undo-list
2184 (cons vip-buffer-undo-list-mark buffer-undo-list))
2185 (setq vip-undo-needs-adjustment t)))))
2190 (defun vip-display-current-destructive-command ()
2191 (let ((text (nth 4 vip-d-com))
2192 (keys (nth 5 vip-d-com))
2195 (setq this-command 'vip-display-current-destructive-command)
2197 (message " `.' runs %s%s"
2198 (concat "`" (vip-array-to-string keys) "'")
2199 (vip-abbreviate-string text max-text-len
2200 " inserting `" "'" " ......."))
2204 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
2205 ;; or in some other way (non-interactively).
2206 (defun vip-set-destructive-command (list)
2207 (or (eq vip-intermediate-command 'vip-repeat)
2209 (setq vip-d-com list)
2210 (setcar (nthcdr 5 vip-d-com)
2211 (vip-array-to-string (this-command-keys)))
2212 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
2214 (defun vip-prev-destructive-command (next)
2215 "Find previous destructive command in the history of destructive commands.
2216 With prefix argument, find next destructive command."
2218 (let (cmd vip-intermediate-command)
2219 (if (eq last-command 'vip-display-current-destructive-command)
2220 ;; repeated search through command history
2221 (setq vip-intermediate-command 'repeating-display-destructive-command)
2222 ;; first search through command history--set temp ring
2223 (setq vip-temp-command-ring (copy-list vip-command-ring)))
2225 (vip-special-ring-rotate1 vip-temp-command-ring 1)
2226 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
2229 (setq vip-d-com cmd))
2230 (vip-display-current-destructive-command)))
2232 (defun vip-next-destructive-command ()
2233 "Find next destructive command in the history of destructive commands."
2235 (vip-prev-destructive-command 'next))
2237 (defun vip-insert-prev-from-insertion-ring (arg)
2238 "Cycles through insertion ring in the direction of older insertions.
2239 Undoes previous insertion and inserts new.
2240 With prefix argument, cycles in the direction of newer elements.
2241 In minibuffer, this command executes whatever the invocation key is bound
2242 to in the global map, instead of cycling through the insertion ring."
2244 (let (vip-intermediate-command)
2245 (if (eq last-command 'vip-insert-from-insertion-ring)
2246 (progn ; repeated search through insertion history
2247 (setq vip-intermediate-command 'repeating-insertion-from-ring)
2248 (if (eq vip-current-state 'replace-state)
2250 (if vip-last-inserted-string-from-insertion-ring
2251 (backward-delete-char
2252 (length vip-last-inserted-string-from-insertion-ring))))
2254 ;;first search through insertion history
2255 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
2256 (setq this-command 'vip-insert-from-insertion-ring)
2257 ;; so that things will be undone properly
2258 (setq buffer-undo-list (cons nil buffer-undo-list))
2259 (setq vip-last-inserted-string-from-insertion-ring
2260 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
2262 ;; this change of vip-intermediate-command must come after
2263 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
2265 (setq vip-intermediate-command nil)
2266 (if vip-last-inserted-string-from-insertion-ring
2267 (insert vip-last-inserted-string-from-insertion-ring))
2270 (defun vip-insert-next-from-insertion-ring ()
2271 "Cycles through insertion ring in the direction of older insertions. Undoes previous insertion and inserts new."
2273 (vip-insert-prev-from-insertion-ring 'next))
2276 ;; some region utilities
2278 (defun vip-add-newline-at-eob-if-necessary ()
2279 "If at the last line of buffer, add \\n before eob, if newline is missing."
2282 ;; make sure all lines end with newline, unless in the minibuffer or
2283 ;; when requested otherwise (vip-add-newline-at-eob is nil)
2287 vip-add-newline-at-eob
2288 (not (vip-is-in-minibuffer)))
2291 (defun vip-yank-defun ()
2293 (copy-region-as-kill (point) (mark t)))
2295 (defun vip-enlarge-region (beg end)
2296 "Enlarge region between BEG and END."
2297 (or beg (setq beg end)) ; if beg is nil, set to end
2298 (or end (setq end beg)) ; if end is nil, set to beg
2301 (progn (goto-char beg) (set-mark end))
2305 (exchange-point-and-mark)
2306 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
2307 (if (not (eobp)) (beginning-of-line))
2308 (if (> beg end) (exchange-point-and-mark)))
2311 (defun vip-quote-region ()
2312 "Quote region by each line with a user supplied string."
2313 (setq vip-quote-string
2314 (vip-read-string-with-history
2317 'vip-quote-region-history
2319 (vip-enlarge-region (point) (mark t))
2320 (if (> (point) (mark t)) (exchange-point-and-mark))
2321 (insert vip-quote-string)
2324 (while (and (< (point) (mark t)) (bolp))
2325 (insert vip-quote-string)
2330 ;; Tells whether BEG is on the same line as END.
2331 ;; If one of the args is nil, it'll return nil.
2332 (defun vip-same-line (beg end)
2333 (let ((selective-display nil))
2334 (cond ((and beg end)
2335 ;; This 'if' is needed because Emacs treats the next empty line
2336 ;; as part of the previous line.
2337 (if (or (> beg (point-max)) (> end (point-max))) ; out of range
2339 (if (and (> end beg) (= (vip-line-pos 'start) end))
2340 (setq end (min (1+ end) (point-max))))
2341 (if (and (> beg end) (= (vip-line-pos 'start) beg))
2342 (setq beg (min (1+ beg) (point-max))))
2343 (<= (count-lines beg end) 1) ))
2349 ;; Check if the string ends with a newline.
2350 (defun vip-end-with-a-newline-p (string)
2351 (or (string= string "")
2352 (= (vip-seq-last-elt string) ?\n)))
2354 (defun vip-tmp-insert-at-eob (msg)
2355 (let ((savemax (point-max)))
2359 (goto-char savemax) (delete-region (point) (point-max))
2364 ;;; Minibuffer business
2366 (defsubst vip-set-minibuffer-style ()
2367 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
2370 (defun vip-minibuffer-setup-sentinel ()
2371 (let ((hook (if vip-vi-style-in-minibuffer
2372 'vip-change-state-to-insert
2373 'vip-change-state-to-emacs)))
2376 ;; Make sure the minibufer overlay is kept up-to-date. In XEmacs also
2377 ;; guards against the possibility of detaching this overlay.
2378 (add-hook 'vip-post-command-hooks 'vip-move-minibuffer-overlay)
2381 ;; Interpret last event in the local map
2382 (defun vip-exit-minibuffer ()
2385 (setq command (local-key-binding (char-to-string last-command-char)))
2387 (command-execute command)
2388 (exit-minibuffer))))
2391 (defun vip-set-search-face ()
2392 (if (not window-system)
2394 (defvar vip-search-face
2396 (make-face 'vip-search-face)
2397 (or (face-differs-from-default-p 'vip-search-face)
2398 ;; face wasn't set in .vip or .Xdefaults
2399 (if (vip-can-use-colors "Black" "khaki")
2401 (set-face-background 'vip-search-face "khaki")
2402 (set-face-foreground 'vip-search-face "Black"))
2403 (copy-face 'italic 'vip-search-face)
2404 (set-face-underline-p 'vip-search-face t)))
2406 "*Face used to flash out the search pattern.")
2410 (defun vip-set-minibuffer-faces ()
2411 (if (not window-system)
2413 (defvar vip-minibuffer-emacs-face
2415 (make-face 'vip-minibuffer-emacs-face)
2416 (or (face-differs-from-default-p 'vip-minibuffer-emacs-face)
2417 ;; face wasn't set in .vip or .Xdefaults
2418 (if vip-vi-style-in-minibuffer
2419 ;; emacs state is an exception in the minibuffer
2420 (if (vip-can-use-colors "darkseagreen2" "Black")
2422 (set-face-background
2423 'vip-minibuffer-emacs-face "darkseagreen2")
2424 (set-face-foreground
2425 'vip-minibuffer-emacs-face "Black"))
2426 (copy-face 'highlight 'vip-minibuffer-emacs-face))
2427 ;; emacs state is the main state in the minibuffer
2428 (if (vip-can-use-colors "Black" "pink")
2430 (set-face-background 'vip-minibuffer-emacs-face "pink")
2431 (set-face-foreground
2432 'vip-minibuffer-emacs-face "Black"))
2433 (copy-face 'italic 'vip-minibuffer-emacs-face))
2435 'vip-minibuffer-emacs-face)
2436 "Face used in the Minibuffer when it is in Emacs state.")
2438 (defvar vip-minibuffer-insert-face
2440 (make-face 'vip-minibuffer-insert-face)
2441 (or (face-differs-from-default-p 'vip-minibuffer-insert-face)
2442 (if vip-vi-style-in-minibuffer
2443 (if (vip-can-use-colors "Black" "pink")
2445 (set-face-background 'vip-minibuffer-insert-face "pink")
2446 (set-face-foreground
2447 'vip-minibuffer-insert-face "Black"))
2448 (copy-face 'italic 'vip-minibuffer-insert-face))
2449 ;; If Insert state is an exception
2450 (if (vip-can-use-colors "darkseagreen2" "Black")
2452 (set-face-background
2453 'vip-minibuffer-insert-face "darkseagreen2")
2454 (set-face-foreground
2455 'vip-minibuffer-insert-face "Black"))
2456 (copy-face 'highlight 'vip-minibuffer-insert-face))
2457 (vip-italicize-face 'vip-minibuffer-insert-face)))
2458 'vip-minibuffer-insert-face)
2459 "Face used in the Minibuffer when it is in Insert state.")
2461 (defvar vip-minibuffer-vi-face
2463 (make-face 'vip-minibuffer-vi-face)
2464 (or (face-differs-from-default-p 'vip-minibuffer-vi-face)
2465 (if vip-vi-style-in-minibuffer
2466 (if (vip-can-use-colors "Black" "grey")
2468 (set-face-background 'vip-minibuffer-vi-face "grey")
2469 (set-face-foreground 'vip-minibuffer-vi-face "Black"))
2470 (copy-face 'bold 'vip-minibuffer-vi-face))
2471 (copy-face 'bold 'vip-minibuffer-vi-face)
2472 (invert-face 'vip-minibuffer-vi-face)))
2473 'vip-minibuffer-vi-face)
2474 "Face used in the Minibuffer when it is in Vi state.")
2476 ;; the current face used in the minibuffer
2477 (vip-deflocalvar vip-minibuffer-current-face vip-minibuffer-emacs-face "")
2482 ;;; Reading string with history
2484 (defun vip-read-string-with-history (prompt &optional initial
2485 history-var default keymap)
2486 ;; Reads string, prompting with PROMPT and inserting the INITIAL
2487 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2488 ;; input is an empty string. Uses KEYMAP, if given, or the
2489 ;; minibuffer-local-map.
2490 ;; Default value is displayed until the user types something in the
2492 (let ((minibuffer-setup-hook
2494 (if (stringp initial)
2499 (vip-minibuffer-setup-sentinel)))
2504 (setq keymap (or keymap minibuffer-local-map)
2505 initial (or initial "")
2506 temp-msg (if default
2507 (format "(default: %s) " default)
2510 (setq vip-incomplete-ex-cmd nil)
2511 (setq val (read-from-minibuffer prompt
2512 (concat temp-msg initial val padding)
2513 keymap nil history-var))
2514 (setq minibuffer-setup-hook nil
2515 padding (vip-array-to-string (this-command-keys))
2517 ;; the following overcomes a glaring bug in history handling
2519 (if (not (string= val (car (eval history-var))))
2520 (set history-var (cons val (eval history-var))))
2521 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2522 (string= (nth 0 (eval history-var)) ""))
2523 (set history-var (cdr (eval history-var))))
2524 (if (string= val "")
2530 ;; insertion commands
2532 ;; Called when state changes from Insert Vi command mode.
2533 ;; Repeats the insertion command if Insert state was entered with prefix
2535 (defun vip-repeat-insert-command ()
2536 (let ((i-com (car vip-d-com))
2537 (val (nth 1 vip-d-com))
2538 (char (nth 2 vip-d-com)))
2539 (if (and val (> val 1)) ; first check that val is non-nil
2541 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
2543 (setq vip-d-com (list i-com val char nil nil nil))
2546 (defun vip-insert (arg)
2547 "Insert before point."
2549 (vip-set-complex-command-for-undo)
2550 (let ((val (vip-p-val arg))
2551 (com (vip-getcom arg)))
2552 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
2554 (vip-loop val (vip-yank-last-insertion))
2555 (vip-change-state-to-insert))))
2557 (defun vip-append (arg)
2558 "Append after point."
2560 (vip-set-complex-command-for-undo)
2561 (let ((val (vip-p-val arg))
2562 (com (vip-getcom arg)))
2563 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
2564 (if (not (eolp)) (forward-char))
2566 (vip-loop val (vip-yank-last-insertion))
2567 (vip-change-state-to-insert))))
2569 (defun vip-Append (arg)
2570 "Append at end of line."
2572 (vip-set-complex-command-for-undo)
2573 (let ((val (vip-p-val arg))
2574 (com (vip-getcom arg)))
2575 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
2578 (vip-loop val (vip-yank-last-insertion))
2579 (vip-change-state-to-insert))))
2581 (defun vip-Insert (arg)
2582 "Insert before first non-white."
2584 (vip-set-complex-command-for-undo)
2585 (let ((val (vip-p-val arg))
2586 (com (vip-getcom arg)))
2587 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
2588 (back-to-indentation)
2590 (vip-loop val (vip-yank-last-insertion))
2591 (vip-change-state-to-insert))))
2593 (defun vip-open-line (arg)
2596 (vip-set-complex-command-for-undo)
2597 (let ((val (vip-p-val arg))
2598 (com (vip-getcom arg)))
2599 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
2600 (let ((col (current-indentation)))
2607 (progn (setq vip-cted t) (indent-to col)))
2608 (vip-yank-last-insertion)))
2611 (if vip-auto-indent (progn (setq vip-cted t) (indent-to col)))
2612 (vip-change-state-to-insert)
2615 (defun vip-Open-line (arg)
2618 (vip-set-complex-command-for-undo)
2619 (let ((val (vip-p-val arg))
2620 (com (vip-getcom arg)))
2621 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
2622 (let ((col (current-indentation)))
2629 (progn (setq vip-cted t) (indent-to col)))
2630 (vip-yank-last-insertion)))
2633 (if vip-auto-indent (progn (setq vip-cted t) (indent-to col)))
2634 (vip-change-state-to-insert)))))
2636 (defun vip-open-line-at-point (arg)
2637 "Open line at point."
2639 (vip-set-complex-command-for-undo)
2640 (let ((val (vip-p-val arg))
2641 (com (vip-getcom arg)))
2642 (vip-set-destructive-command
2643 (list 'vip-open-line-at-point val ?r nil nil nil))
2648 (vip-yank-last-insertion)))
2650 (vip-change-state-to-insert))))
2652 (defun vip-substitute (arg)
2653 "Substitute characters."
2655 (let ((val (vip-p-val arg))
2656 (com (vip-getcom arg)))
2660 (vip-change-subr (mark t) (point))
2661 (vip-change (mark t) (point)))
2662 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
2665 (defun vip-substitute-line (arg)
2668 (vip-set-complex-command-for-undo)
2669 (vip-line (cons arg ?C)))
2671 ;; Prepare for replace
2672 (defun vip-start-replace ()
2673 (setq vip-began-as-replace t
2674 vip-sitting-in-replace t
2675 vip-replace-chars-to-delete 0
2676 vip-replace-chars-deleted 0)
2677 (add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
2678 (add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
2679 ;; this will get added repeatedly, but no harm
2680 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
2681 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
2682 (vip-move-marker-locally 'vip-last-posn-in-replace-region
2683 (vip-replace-start))
2684 (add-hook 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
2685 (add-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2688 ;; Runs vip-after-change-functions inside after-change-functions
2689 (defun vip-after-change-sentinel (beg end len)
2690 (let ((list vip-after-change-functions))
2692 (funcall (car list) beg end len)
2693 (setq list (cdr list)))))
2695 ;; Runs vip-before-change-functions inside before-change-functions
2696 (defun vip-before-change-sentinel (beg end)
2697 (let ((list vip-before-change-functions))
2699 (funcall (car list) beg end)
2700 (setq list (cdr list)))))
2702 (defun vip-post-command-sentinel ()
2703 (run-hooks 'vip-post-command-hooks))
2705 (defun vip-pre-command-sentinel ()
2706 (run-hooks 'vip-pre-command-hooks))
2708 ;; Needed so that Viper will be able to figure the last inserted
2709 ;; chunk of text with reasonable accuracy.
2710 (defun vip-insert-state-post-command-sentinel ()
2711 (if (and (memq vip-current-state '(insert-state replace-state))
2713 (>= (point) vip-insert-point))
2714 (setq vip-last-posn-while-in-insert-state (point-marker)))
2715 (if (and (eq this-command 'dabbrev-expand)
2716 (integerp vip-pre-command-point)
2717 (> vip-insert-point vip-pre-command-point))
2718 (move-marker vip-insert-point vip-pre-command-point))
2721 (defun vip-insert-state-pre-command-sentinel ()
2722 (if (and (eq this-command 'dabbrev-expand)
2723 (markerp vip-insert-point)
2724 (marker-position vip-insert-point))
2725 (setq vip-pre-command-point (marker-position vip-insert-point))))
2727 (defun vip-R-state-post-command-sentinel ()
2728 ;; This is needed despite vip-replace-state-pre-command-sentinel
2729 ;; When you jump to another buffer in another frame, the pre-command
2730 ;; hook won't change cursor color to default in that other frame.
2731 ;; So, if the second frame cursor was red and we set the point
2732 ;; outside the replacement region, then the cursor color woll remain
2733 ;; red. Restoring the default, below, prevents this.
2734 (vip-restore-cursor-color)
2735 (if (and (<= (vip-replace-start) (point))
2736 (<= (point) (vip-replace-end)))
2737 (vip-change-cursor-color vip-replace-overlay-cursor-color)))
2739 (defun vip-replace-state-pre-command-sentinel ()
2740 (vip-restore-cursor-color))
2742 (defun vip-replace-state-post-command-sentinel ()
2743 ;; This is needed despite vip-replace-state-pre-command-sentinel
2744 ;; When you jump to another buffer in another frame, the pre-command
2745 ;; hook won't change cursor color to default in that other frame.
2746 ;; So, if the second frame cursor was red and we set the point
2747 ;; outside the replacement region, then the cursor color woll remain
2748 ;; red. Restoring the default, below, prevents this.
2749 (vip-restore-cursor-color)
2751 ((eq vip-current-state 'replace-state)
2752 ;; delete characters to compensate for inserted chars.
2753 (let ((replace-boundary
2754 ;; distinguish empty repl-reg-end-symbol from non-empty
2755 (- (vip-replace-end)
2756 (if (eq (length vip-replace-region-end-symbol) 0)
2761 (goto-char vip-last-posn-in-replace-region)
2762 (delete-char vip-replace-chars-to-delete)
2763 (setq vip-replace-chars-to-delete 0
2764 vip-replace-chars-deleted 0)
2765 ;; terminate replace mode if reached replace limit
2766 (if (= vip-last-posn-in-replace-region
2768 (vip-finish-change vip-last-posn-in-replace-region)))
2770 (if (and (<= (vip-replace-start) (point))
2771 (<= (point) replace-boundary))
2773 ;; the state may have changed in vip-finish-change above
2774 (if (eq vip-current-state 'replace-state)
2775 (vip-change-cursor-color vip-replace-overlay-cursor-color))
2776 (setq vip-last-posn-in-replace-region (point-marker))))
2779 (t ;; terminate replace mode if changed Viper states.
2780 (vip-finish-change vip-last-posn-in-replace-region)))
2784 ;; checks how many chars were deleted by the last change
2785 (defun vip-replace-mode-spy-before (beg end)
2786 (setq vip-replace-chars-deleted (- end beg
2787 (max 0 (- end (vip-replace-end)))
2788 (max 0 (- (vip-replace-start) beg))
2792 ;; Invoked as an after-change-function to set up parameters of the last change
2793 (defun vip-replace-mode-spy-after (beg end length)
2794 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
2796 (setq vip-replace-chars-to-delete 0)
2797 (vip-move-marker-locally
2798 'vip-last-posn-in-replace-region (point)))
2800 (let (beg-col end-col real-end chars-to-delete)
2801 (setq real-end (min end (vip-replace-end)))
2804 (setq beg-col (current-column))
2805 (goto-char real-end)
2806 (setq end-col (current-column)))
2808 ;; If beg of change is outside the replacement region, then don't
2809 ;; delete anything in the repl region (set chars-to-delete to 0).
2811 ;; This works fine except that we have to take special care of
2812 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
2813 ;; sometimes simply shifts the repl region rightwards, without
2814 ;; deleting an equal amount of characters.
2816 ;; The reason why new-dabbrev.el causes this are this:
2817 ;; if one dinamically completes a partial word that starts before the
2818 ;; replacement region (but ends inside)then new-dabbrev.el first
2819 ;; moves cursor backwards, to the beginning of the word to be
2820 ;; completed (say, pt A). Then it inserts the
2821 ;; completed word and then deletes the old, incomplete part.
2822 ;; Since the complete word is inserted at position before the repl
2823 ;; region, the next If-statement would have set chars-to-delete to 0
2824 ;; unless we check for the current command, which must be
2827 ;; We should be able deal with these problems in a better way
2828 ;; when emacs will have overlays with sticky back ends.
2829 ;; In fact, it would be also useful to add overlays for insert
2830 ;; regions as well, since this will let us capture the situation when
2831 ;; dabbrev-expand goes back past the insertion point to find the
2832 ;; beginning of the word to be expanded.
2833 (if (or (and (<= (vip-replace-start) beg)
2834 (<= beg (vip-replace-end)))
2835 (and (= length 0) (eq this-command 'dabbrev-expand)))
2836 (setq chars-to-delete
2837 (max (- end-col beg-col) (- real-end beg) 0))
2838 (setq chars-to-delete 0))
2840 ;; if beg = last change position, it means that we are within the
2841 ;; same command that does multiple changes. Moreover, it means
2842 ;; that we have two subsequent changes (insert/delete) that
2843 ;; complement each other.
2844 (if (= beg (marker-position vip-last-posn-in-replace-region))
2845 (setq vip-replace-chars-to-delete
2846 (- (+ chars-to-delete vip-replace-chars-to-delete)
2847 vip-replace-chars-deleted))
2848 (setq vip-replace-chars-to-delete chars-to-delete))
2850 (vip-move-marker-locally
2851 'vip-last-posn-in-replace-region
2852 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
2853 (or (marker-position vip-last-posn-in-replace-region)
2854 (vip-replace-start))
2857 (setq vip-replace-chars-to-delete
2858 (max 0 (min vip-replace-chars-to-delete
2859 (- (vip-replace-end)
2860 vip-last-posn-in-replace-region))))
2864 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
2865 ;; posn is within the overlay.
2866 (defun vip-finish-change (posn)
2867 (remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
2868 (remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2869 (remove-hook 'vip-post-command-hooks
2870 'vip-replace-state-post-command-sentinel)
2871 (remove-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2872 (vip-restore-cursor-color)
2873 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2877 (>= posn (vip-replace-start))
2878 (< posn (vip-replace-end)))
2879 (delete-region posn (vip-replace-end)))
2882 (if (eq vip-current-state 'replace-state)
2883 (vip-downgrade-to-insert))
2884 ;; replace mode ended => nullify vip-last-posn-in-replace-region
2885 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
2886 (vip-hide-replace-overlay)
2887 (vip-refresh-mode-line)
2888 (vip-put-string-on-kill-ring vip-last-replace-region)
2891 (defun vip-put-string-on-kill-ring (string)
2892 "Make STRING be the first element of the kill ring."
2893 (setq kill-ring (cons string kill-ring))
2894 (if (> (length kill-ring) kill-ring-max)
2895 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2896 (setq kill-ring-yank-pointer kill-ring))
2898 (defun vip-finish-R-mode ()
2899 (remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2900 (remove-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2901 (vip-downgrade-to-insert))
2903 (defun vip-start-R-mode ()
2904 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2906 (add-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
2907 (add-hook 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2912 (defun vip-replace-state-exit-cmd ()
2913 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2914 These keys are ESC, RET, and LineFeed"
2916 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2918 (vip-finish-change vip-last-posn-in-replace-region))
2920 (if (eq this-command 'vip-intercept-ESC-key)
2921 (setq com 'vip-exit-insert-state)
2922 (vip-set-unread-command-events last-input-char)
2923 (setq com (key-binding (read-key-sequence nil))))
2925 (condition-case conds
2926 (command-execute com)
2928 (vip-message-conditions conds)))
2930 (vip-hide-replace-overlay))
2933 (defun vip-overwrite (arg)
2934 "This is the function bound to 'R'---unlimited replace.
2935 Similar to Emacs's own overwrite-mode."
2937 (let ((val (vip-p-val arg))
2938 (com (vip-getcom arg)) (len))
2939 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
2942 ;; Viper saves inserted text in vip-last-insertion
2943 (setq len (length vip-last-insertion))
2945 (vip-loop val (vip-yank-last-insertion)))
2946 (setq last-command 'vip-overwrite)
2947 (vip-set-complex-command-for-undo)
2948 (vip-set-replace-overlay (point) (vip-line-pos 'end))
2949 (vip-change-state-to-replace)
2955 (defun vip-line (arg)
2956 (let ((val (car arg))
2958 (vip-move-marker-locally 'vip-com-point (point))
2960 (next-line (1- val)))
2961 ;; this ensures that dd, cc, D, yy will do the right thing on the last
2962 ;; line of buffer when this line has no \n.
2963 (vip-add-newline-at-eob-if-necessary)
2964 (vip-execute-com 'vip-line val com))
2965 (if (and (eobp) (not (bobp))) (forward-line -1))
2968 (defun vip-yank-line (arg)
2969 "Yank ARG lines (in Vi's sense)."
2971 (let ((val (vip-p-val arg)))
2972 (vip-line (cons val ?Y))))
2977 (defun vip-region (arg)
2979 (let ((val (vip-P-val arg))
2980 (com (vip-getcom arg)))
2981 (vip-move-marker-locally 'vip-com-point (point))
2982 (exchange-point-and-mark)
2983 (vip-execute-com 'vip-region val com)))
2985 (defun vip-Region (arg)
2987 (let ((val (vip-P-val arg))
2988 (com (vip-getCom arg)))
2989 (vip-move-marker-locally 'vip-com-point (point))
2990 (exchange-point-and-mark)
2991 (vip-execute-com 'vip-Region val com)))
2993 (defun vip-replace-char (arg)
2994 "Replace the following ARG chars by the character read."
2996 (if (and (eolp) (bolp)) (error "I see no character to replace here"))
2997 (let ((val (vip-p-val arg))
2998 (com (vip-getcom arg)))
2999 (vip-replace-char-subr (if (equal com ?r) vip-d-char (read-char)) val)
3000 (if (and (eolp) (not (bolp))) (forward-char 1))
3001 (vip-set-destructive-command
3002 (list 'vip-replace-char val ?r nil vip-d-char nil))
3005 (defun vip-replace-char-subr (char arg)
3007 (setq vip-d-char char)
3008 (vip-loop (if (> arg 0) arg (- arg))
3009 (if (eq char ?\C-m) (insert "\n") (insert char)))
3010 (backward-char arg))
3012 (defun vip-replace-string ()
3013 "The old replace string function.
3014 If you supply null string as the string to be replaced,
3015 the query replace mode will toggle between string replace and regexp replace.
3016 This function comes from VIP 3.5 and is not used in Viper. A nostalgic user
3017 can bind it to a key, if necessary."
3020 (setq str (vip-read-string-with-history
3021 (if vip-re-replace "Replace regexp: " "Replace string: ")
3023 'vip-replace1-history
3024 (car vip-replace1-history) ; default
3026 (if (string= str "")
3028 (setq vip-re-replace (not vip-re-replace))
3029 (message (format "Replace mode changed to %s"
3030 (if vip-re-replace "regexp replace"
3031 "string replace"))))
3035 (vip-read-string-with-history
3036 (format "Replace regexp `%s' with: " str)
3038 'vip-replace2-history
3039 (car vip-replace2-history) ; default
3043 (vip-read-string-with-history
3044 (format "Replace `%s' with: " str)
3046 'vip-replace2-history
3047 (car vip-replace2-history) ; default
3052 ;; basic cursor movement. j, k, l, h commands.
3054 (defun vip-forward-char (arg)
3055 "Move point right ARG characters (left if ARG negative).
3056 On reaching end of line, stop and signal error."
3058 (let ((val (vip-p-val arg))
3059 (com (vip-getcom arg)))
3060 (if com (vip-move-marker-locally 'vip-com-point (point)))
3061 (if vip-ex-style-motion
3063 ;; the boundary condition check gets weird here because
3064 ;; forward-char may be the parameter of a delete, and 'dl' works
3065 ;; just like 'x' for the last char on a line, so we have to allow
3066 ;; the forward motion before the 'vip-execute-com', but, of
3067 ;; course, 'dl' doesn't work on an empty line, so we have to
3068 ;; catch that condition before 'vip-execute-com'
3069 (if (and (eolp) (bolp)) (error "") (forward-char val))
3070 (if com (vip-execute-com 'vip-forward-char val com))
3071 (if (eolp) (progn (backward-char 1) (error ""))))
3073 (if com (vip-execute-com 'vip-forward-char val com)))))
3075 (defun vip-backward-char (arg)
3076 "Move point left ARG characters (right if ARG negative).
3077 On reaching beginning of line, stop and signal error."
3079 (let ((val (vip-p-val arg))
3080 (com (vip-getcom arg)))
3081 (if com (vip-move-marker-locally 'vip-com-point (point)))
3082 (if vip-ex-style-motion
3084 (if (bolp) (error "") (backward-char val))
3085 (if com (vip-execute-com 'vip-backward-char val com)))
3087 (if com (vip-execute-com 'vip-backward-char val com)))))
3089 (defun vip-forward-char-carefully (&optional arg)
3090 "Like forward-char, but doesn't move at end of buffer."
3091 (setq arg (or arg 1))
3092 (if (>= (point-max) (+ (point) arg))
3094 (goto-char (point-max))))
3096 (defun vip-backward-char-carefully (&optional arg)
3097 "Like backward-char, but doesn't move at end of buffer."
3098 (setq arg (or arg 1))
3099 (if (<= (point-min) (- (point) arg))
3101 (goto-char (point-min))))
3107 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
3108 ;; for word movement. When executed with a destructive command, \n is
3109 ;; usually left untouched for the last word.
3112 (defun vip-skip-separators (forward)
3115 (skip-chars-forward " \t")
3116 (if (looking-at "\n")
3119 (skip-chars-forward " \t"))))
3120 (skip-chars-backward " \t")
3122 (if (looking-at "\n")
3123 (skip-chars-backward " \t")
3126 (defconst vip-ALPHA "a-zA-Z0-9_")
3127 (defconst vip-ALPHA-B (concat "[" vip-ALPHA "]"))
3128 (defconst vip-NONALPHA (concat "^" vip-ALPHA))
3129 (defconst vip-NONALPHA-B (concat "[" vip-NONALPHA "]"))
3130 (defconst vip-SEP " \t\n")
3131 (defconst vip-SEP-B (concat "[" vip-SEP "]"))
3132 (defconst vip-NONSEP (concat "^" vip-SEP))
3133 (defconst vip-NONSEP-B (concat "[" vip-NONSEP "]"))
3134 (defconst vip-ALPHASEP (concat vip-ALPHA vip-SEP))
3135 (defconst vip-ALPHASEP-B (concat "[" vip-ALPHASEP "]"))
3136 (defconst vip-NONALPHASEP (concat "^" vip-ALPHASEP ))
3137 (defconst vip-NONALPHASEP-B (concat "[" vip-NONALPHASEP "]"))
3140 (defun vip-forward-word-kernel (val)
3142 (cond ((looking-at vip-ALPHA-B)
3143 (skip-chars-forward vip-ALPHA)
3144 (vip-skip-separators t))
3145 ((looking-at vip-SEP-B)
3146 (vip-skip-separators t))
3147 ((looking-at vip-NONALPHASEP-B)
3148 (skip-chars-forward vip-NONALPHASEP)
3149 (vip-skip-separators t)))
3150 (setq val (1- val))))
3152 (defun vip-fwd-skip (pat aux-pat lim)
3153 (if (and (save-excursion
3154 (re-search-backward pat lim t))
3155 (= (point) (match-end 0)))
3156 (goto-char (match-beginning 0)))
3157 (skip-chars-backward aux-pat lim)
3159 (vip-forward-char-carefully))
3163 (defun vip-forward-word (arg)
3166 (let ((val (vip-p-val arg))
3167 (com (vip-getcom arg)))
3168 (if com (vip-move-marker-locally 'vip-com-point (point)))
3169 (vip-forward-word-kernel val)
3171 (cond ((memq com (list ?c (- ?c) ?y (- ?y)))
3172 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3173 ((vip-dotable-command-p com)
3174 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3175 (vip-execute-com 'vip-forward-word val com)))))
3178 (defun vip-forward-Word (arg)
3179 "Forward word delimited by white character."
3181 (let ((val (vip-p-val arg))
3182 (com (vip-getcom arg)))
3183 (if com (vip-move-marker-locally 'vip-com-point (point)))
3186 (skip-chars-forward vip-NONSEP)
3187 (vip-skip-separators t)))
3189 (cond ((memq com (list ?c (- ?c) ?y (- ?y)))
3190 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
3191 ((vip-dotable-command-p com)
3192 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
3193 (vip-execute-com 'vip-forward-Word val com)))))
3196 ;; this is a bit different from Vi, but Vi's end of word
3197 ;; makes no sense whatsoever
3198 (defun vip-end-of-word-kernel ()
3199 (if (vip-end-of-word-p) (forward-char))
3200 (if (looking-at "[ \t\n]")
3201 (skip-chars-forward vip-SEP))
3203 (cond ((looking-at vip-ALPHA-B) (skip-chars-forward vip-ALPHA))
3204 ((looking-at vip-NONALPHASEP-B)
3205 (skip-chars-forward vip-NONALPHASEP)))
3206 (vip-backward-char-carefully))
3208 (defun vip-end-of-word-p ()
3211 (cond ((looking-at vip-ALPHA-B)
3213 (looking-at vip-NONALPHA-B))
3214 ((looking-at vip-NONALPHASEP-B)
3216 (looking-at vip-ALPHASEP-B))))))
3218 (defun vip-one-char-word-p ()
3221 (cond ((looking-at vip-ALPHA-B)
3222 (if (bobp) (setq step 1) (backward-char))
3223 (if (or (bobp) (looking-at vip-NONALPHA-B))
3226 (looking-at vip-NONALPHA-B))
3228 ((looking-at vip-NONALPHASEP-B)
3229 (if (bobp) (setq step 1) (backward-char))
3230 (if (or (bobp) (looking-at vip-ALPHASEP-B))
3233 (looking-at vip-ALPHASEP-B))
3236 (defun vip-one-char-Word-p ()
3237 (and (looking-at vip-NONSEP-B)
3242 (looking-at vip-SEP-B)))
3246 (looking-at vip-SEP-B)))))
3248 (defun vip-end-of-word (arg &optional careful)
3249 "Move point to end of current word."
3251 (let ((val (vip-p-val arg))
3252 (com (vip-getcom arg)))
3253 (if com (vip-move-marker-locally 'vip-com-point (point)))
3254 (vip-loop val (vip-end-of-word-kernel))
3258 (vip-execute-com 'vip-end-of-word val com)))))
3260 (defun vip-end-of-Word (arg)
3261 "Forward to end of word delimited by white character."
3263 (let ((val (vip-p-val arg))
3264 (com (vip-getcom arg)))
3265 (if com (vip-move-marker-locally 'vip-com-point (point)))
3268 (vip-end-of-word-kernel)
3269 (if (not (re-search-forward
3271 (goto-char (point-max)))
3272 (skip-chars-backward vip-SEP)
3277 (vip-execute-com 'vip-end-of-Word val com)))))
3279 (defun vip-backward-word-kernel (val)
3282 (cond ((looking-at vip-ALPHA-B)
3283 (skip-chars-backward vip-ALPHA))
3284 ((looking-at vip-SEP-B)
3286 (vip-skip-separators nil)
3288 (cond ((looking-at vip-ALPHA-B)
3289 (skip-chars-backward vip-ALPHA))
3290 ((looking-at vip-NONALPHASEP-B)
3291 (skip-chars-backward vip-NONALPHASEP))
3292 (t (forward-char))))
3293 ((looking-at vip-NONALPHASEP-B)
3294 (skip-chars-backward vip-NONALPHASEP)))
3295 (setq val (1- val))))
3297 (defun vip-backward-word (arg)
3300 (let ((val (vip-p-val arg))
3301 (com (vip-getcom arg)))
3304 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3306 (vip-move-marker-locally 'vip-com-point (point))
3307 (if i (forward-char))))
3308 (vip-backward-word-kernel val)
3309 (if com (vip-execute-com 'vip-backward-word val com))))
3311 (defun vip-backward-Word (arg)
3312 "Backward word delimited by white character."
3314 (let ((val (vip-p-val arg))
3315 (com (vip-getcom arg)))
3318 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
3320 (vip-move-marker-locally 'vip-com-point (point))
3321 (if i (forward-char))))
3324 (vip-skip-separators nil)
3325 (skip-chars-backward vip-NONSEP)))
3326 (if com (vip-execute-com 'vip-backward-Word val com))))
3332 (defun vip-beginning-of-line (arg)
3333 "Go to beginning of line."
3335 (let ((val (vip-p-val arg))
3336 (com (vip-getcom arg)))
3337 (if com (vip-move-marker-locally 'vip-com-point (point)))
3338 (beginning-of-line val)
3339 (if com (vip-execute-com 'vip-beginning-of-line val com))))
3341 (defun vip-bol-and-skip-white (arg)
3342 "Beginning of line at first non-white character."
3344 (let ((val (vip-p-val arg))
3345 (com (vip-getcom arg)))
3346 (if com (vip-move-marker-locally 'vip-com-point (point)))
3347 (forward-to-indentation (1- val))
3348 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
3350 (defun vip-goto-eol (arg)
3351 "Go to end of line."
3353 (let ((val (vip-p-val arg))
3354 (com (vip-getcom arg)))
3355 (if com (vip-move-marker-locally 'vip-com-point (point)))
3357 (if com (vip-execute-com 'vip-goto-eol val com))
3358 (if vip-ex-style-motion
3359 (if (and (eolp) (not (bolp))
3360 ;; a fix for vip-change-to-eol
3361 (not (equal vip-current-state 'insert-state)))
3366 (defun vip-goto-col (arg)
3367 "Go to ARG's column."
3369 (let ((val (vip-p-val arg))
3370 (com (vip-getcom arg)))
3373 (if (> val (1+ (current-column))) (error "")))
3374 (if com (vip-move-marker-locally 'vip-com-point (point)))
3376 (forward-char (1- val))
3377 (if com (vip-execute-com 'vip-goto-col val com))))
3380 (defun vip-next-line (arg)
3383 (let ((val (vip-p-val arg))
3384 (com (vip-getCom arg)))
3385 (if com (vip-move-marker-locally 'vip-com-point (point)))
3387 (if vip-ex-style-motion
3388 (if (and (eolp) (not (bolp))) (backward-char 1)))
3389 (setq this-command 'next-line)
3390 (if com (vip-execute-com 'vip-next-line val com))))
3392 (defun vip-next-line-at-bol (arg)
3393 "Next line at beginning of line."
3397 (if (eobp) (error "Last line in buffer")))
3398 (let ((val (vip-p-val arg))
3399 (com (vip-getCom arg)))
3400 (if com (vip-move-marker-locally 'vip-com-point (point)))
3402 (back-to-indentation)
3403 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
3405 (defun vip-previous-line (arg)
3406 "Go to previous line."
3408 (let ((val (vip-p-val arg))
3409 (com (vip-getCom arg)))
3410 (if com (vip-move-marker-locally 'vip-com-point (point)))
3412 (if vip-ex-style-motion
3413 (if (and (eolp) (not (bolp))) (backward-char 1)))
3414 (setq this-command 'previous-line)
3415 (if com (vip-execute-com 'vip-previous-line val com))))
3418 (defun vip-previous-line-at-bol (arg)
3419 "Previous line at beginning of line."
3423 (if (bobp) (error "First line in buffer")))
3424 (let ((val (vip-p-val arg))
3425 (com (vip-getCom arg)))
3426 (if com (vip-move-marker-locally 'vip-com-point (point)))
3427 (forward-line (- val))
3428 (back-to-indentation)
3429 (if com (vip-execute-com 'vip-previous-line val com))))
3431 (defun vip-change-to-eol (arg)
3432 "Change to end of line."
3434 (vip-goto-eol (cons arg ?c)))
3436 (defun vip-kill-line (arg)
3439 (vip-goto-eol (cons arg ?d)))
3441 (defun vip-erase-line (arg)
3444 (vip-beginning-of-line (cons arg ?d)))
3449 (defun vip-goto-line (arg)
3450 "Go to ARG's line. Without ARG go to end of buffer."
3452 (let ((val (vip-P-val arg))
3453 (com (vip-getCom arg)))
3454 (vip-move-marker-locally 'vip-com-point (point))
3455 (vip-deactivate-mark)
3458 (goto-char (point-max))
3459 (goto-char (point-min))
3460 (forward-line (1- val)))
3461 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3462 (back-to-indentation)
3463 (if com (vip-execute-com 'vip-goto-line val com))))
3465 (defun vip-find-char (arg char forward offset)
3466 "Find ARG's occurrence of CHAR on the current line.
3467 If FORWARD then search is forward, otherwise backward. OFFSET is used to
3468 adjust point after search."
3469 (or (char-or-string-p char) (error ""))
3470 (let ((arg (if forward arg (- arg)))
3471 (cmd (if (eq vip-intermediate-command 'vip-repeat)
3473 (vip-array-to-string (this-command-keys))))
3479 ;; forward search begins here
3480 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
3481 ;; forward search ends here
3482 (progn (end-of-line) (point)))
3484 ;; backward search begins from here
3486 (error "Command `%s': At beginning of line" cmd) (point))
3487 ;; backward search ends here
3488 (progn (beginning-of-line) (point))))
3489 ;; if arg > 0, point is forwarded before search.
3490 (if (> arg 0) (goto-char (1+ (point-min)))
3491 (goto-char (point-max)))
3492 (if (let ((case-fold-search nil))
3493 (search-forward (char-to-string char) nil 0 arg))
3494 (setq point (point))
3495 (error "Command `%s': `%c' not found" cmd char))))
3496 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
3498 (defun vip-find-char-forward (arg)
3499 "Find char on the line.
3500 If called interactively read the char to find from the terminal, and if
3501 called from vip-repeat, the char last used is used. This behaviour is
3502 controlled by the sign of prefix numeric value."
3504 (let ((val (vip-p-val arg))
3505 (com (vip-getcom arg)))
3507 ;; this means that the function was called interactively
3508 (setq vip-f-char (read-char)
3511 ;; vip-repeat --- set vip-F-char from command-keys
3512 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3513 (vip-seq-last-elt (nth 5 vip-d-com))
3515 vip-f-char vip-F-char)
3517 (if com (vip-move-marker-locally 'vip-com-point (point)))
3518 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
3522 (setq vip-F-char vip-f-char) ; set new vip-F-char
3524 (vip-execute-com 'vip-find-char-forward val com)))))
3526 (defun vip-goto-char-forward (arg)
3527 "Go up to char ARG forward on line."
3529 (let ((val (vip-p-val arg))
3530 (com (vip-getcom arg)))
3532 ;; this means that the function was called interactively
3533 (setq vip-f-char (read-char)
3536 ;; vip-repeat --- set vip-F-char from command-keys
3537 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3538 (vip-seq-last-elt (nth 5 vip-d-com))
3540 vip-f-char vip-F-char)
3542 (if com (vip-move-marker-locally 'vip-com-point (point)))
3543 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
3547 (setq vip-F-char vip-f-char) ; set new vip-F-char
3549 (vip-execute-com 'vip-goto-char-forward val com)))))
3551 (defun vip-find-char-backward (arg)
3552 "Find char ARG on line backward."
3554 (let ((val (vip-p-val arg))
3555 (com (vip-getcom arg)))
3557 ;; this means that the function was called interactively
3558 (setq vip-f-char (read-char)
3561 ;; vip-repeat --- set vip-F-char from command-keys
3562 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3563 (vip-seq-last-elt (nth 5 vip-d-com))
3565 vip-f-char vip-F-char)
3567 (if com (vip-move-marker-locally 'vip-com-point (point)))
3569 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
3573 (setq vip-F-char vip-f-char) ; set new vip-F-char
3574 (vip-execute-com 'vip-find-char-backward val com)))))
3576 (defun vip-goto-char-backward (arg)
3577 "Go up to char ARG backward on line."
3579 (let ((val (vip-p-val arg))
3580 (com (vip-getcom arg)))
3582 ;; this means that the function was called interactively
3583 (setq vip-f-char (read-char)
3586 ;; vip-repeat --- set vip-F-char from command-keys
3587 (setq vip-F-char (if (stringp (nth 5 vip-d-com))
3588 (vip-seq-last-elt (nth 5 vip-d-com))
3590 vip-f-char vip-F-char)
3592 (if com (vip-move-marker-locally 'vip-com-point (point)))
3593 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
3597 (setq vip-F-char vip-f-char) ; set new vip-F-char
3598 (vip-execute-com 'vip-goto-char-backward val com)))))
3600 (defun vip-repeat-find (arg)
3601 "Repeat previous find command."
3603 (let ((val (vip-p-val arg))
3604 (com (vip-getcom arg)))
3605 (vip-deactivate-mark)
3606 (if com (vip-move-marker-locally 'vip-com-point (point)))
3607 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
3610 (if vip-f-forward (forward-char))
3611 (vip-execute-com 'vip-repeat-find val com)))))
3613 (defun vip-repeat-find-opposite (arg)
3614 "Repeat previous find command in the opposite direction."
3616 (let ((val (vip-p-val arg))
3617 (com (vip-getcom arg)))
3618 (vip-deactivate-mark)
3619 (if com (vip-move-marker-locally 'vip-com-point (point)))
3620 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
3623 (if vip-f-forward (forward-char))
3624 (vip-execute-com 'vip-repeat-find-opposite val com)))))
3627 ;; window scrolling etc.
3629 (defun vip-other-window (arg)
3630 "Switch to other window."
3633 (or (not (eq vip-current-state 'emacs-state))
3634 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
3635 (vip-change-state-to-vi)))
3637 (defun vip-window-top (arg)
3638 "Go to home window line."
3640 (let ((val (vip-p-val arg))
3641 (com (vip-getCom arg)))
3642 (if com (vip-move-marker-locally 'vip-com-point (point)))
3644 (move-to-window-line (1- val))
3645 (if (not com) (back-to-indentation))
3646 (if com (vip-execute-com 'vip-window-top val com))))
3648 (defun vip-window-middle (arg)
3649 "Go to middle window line."
3651 (let ((val (vip-p-val arg))
3652 (com (vip-getCom arg)))
3653 (if com (vip-move-marker-locally 'vip-com-point (point)))
3655 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3656 (if (not com) (back-to-indentation))
3657 (if com (vip-execute-com 'vip-window-middle val com))))
3659 (defun vip-window-bottom (arg)
3660 "Go to last window line."
3662 (let ((val (vip-p-val arg))
3663 (com (vip-getCom arg)))
3664 (if com (vip-move-marker-locally 'vip-com-point (point)))
3666 (move-to-window-line (- val))
3667 (if (not com) (back-to-indentation))
3668 (if com (vip-execute-com 'vip-window-bottom val com))))
3670 (defun vip-line-to-top (arg)
3671 "Put current line on the home line."
3673 (recenter (1- arg)))
3675 (defun vip-line-to-middle (arg)
3676 "Put current line on the middle line."
3678 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3680 (defun vip-line-to-bottom (arg)
3681 "Put current line on the last line."
3683 (recenter (- (window-height) (1+ arg))))
3687 ;; must correct this to only match ( to ) etc. On the other hand
3688 ;; it is good that paren match gets confused, because that way you
3689 ;; catch _all_ imbalances.
3691 (defun vip-paren-match (arg)
3692 "Go to the matching parenthesis."
3694 (let ((com (vip-getcom arg)))
3696 (if (or (> arg 99) (< arg 1))
3697 (error "Prefix must be between 1 and 99")
3699 (if (> (point-max) 80000)
3700 (* (/ (point-max) 100) arg)
3701 (/ (* (point-max) arg) 100)))
3702 (back-to-indentation))
3704 (if (and (eolp) (not (bolp))) (forward-char -1))
3708 (if (re-search-forward "[][(){}]" lim t)
3710 (error "No matching character on line")))
3711 (cond ((looking-at "[\(\[{]")
3712 (if com (vip-move-marker-locally 'vip-com-point (point)))
3715 (vip-execute-com 'vip-paren-match nil com)
3717 ((looking-at "[])}]")
3719 (if com (vip-move-marker-locally 'vip-com-point (point)))
3721 (if com (vip-execute-com 'vip-paren-match nil com)))
3725 ;; sentence ,paragraph and heading
3727 (defun vip-forward-sentence (arg)
3731 (let ((val (vip-p-val arg))
3732 (com (vip-getcom arg)))
3733 (if com (vip-move-marker-locally 'vip-com-point (point)))
3734 (forward-sentence val)
3735 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3737 (defun vip-backward-sentence (arg)
3738 "Backward sentence."
3741 (let ((val (vip-p-val arg))
3742 (com (vip-getcom arg)))
3743 (if com (vip-move-marker-locally 'vip-com-point (point)))
3744 (backward-sentence val)
3745 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3747 (defun vip-forward-paragraph (arg)
3748 "Forward paragraph."
3751 (let ((val (vip-p-val arg))
3752 (com (vip-getCom arg)))
3753 (if com (vip-move-marker-locally 'vip-com-point (point)))
3754 (forward-paragraph val)
3755 (if com (vip-execute-com 'vip-forward-paragraph nil com))))
3757 (defun vip-backward-paragraph (arg)
3758 "Backward paragraph."
3761 (let ((val (vip-p-val arg))
3762 (com (vip-getCom arg)))
3763 (if com (vip-move-marker-locally 'vip-com-point (point)))
3764 (backward-paragraph val)
3765 (if com (vip-execute-com 'vip-backward-paragraph nil com))))
3767 ;; should be mode-specific etc.
3769 (defun vip-prev-heading (arg)
3771 (let ((val (vip-p-val arg))
3772 (com (vip-getCom arg)))
3773 (if com (vip-move-marker-locally 'vip-com-point (point)))
3774 (re-search-backward vip-heading-start nil t val)
3775 (goto-char (match-beginning 0))
3776 (if com (vip-execute-com 'vip-prev-heading nil com))))
3778 (defun vip-heading-end (arg)
3780 (let ((val (vip-p-val arg))
3781 (com (vip-getCom arg)))
3782 (if com (vip-move-marker-locally 'vip-com-point (point)))
3783 (re-search-forward vip-heading-end nil t val)
3784 (goto-char (match-beginning 0))
3785 (if com (vip-execute-com 'vip-heading-end nil com))))
3787 (defun vip-next-heading (arg)
3789 (let ((val (vip-p-val arg))
3790 (com (vip-getCom arg)))
3791 (if com (vip-move-marker-locally 'vip-com-point (point)))
3793 (re-search-forward vip-heading-start nil t val)
3794 (goto-char (match-beginning 0))
3795 (if com (vip-execute-com 'vip-next-heading nil com))))
3800 (setq scroll-step 1)
3802 (defun vip-scroll (arg)
3803 "Scroll to next screen."
3808 (setq arg (1- arg)))
3811 (setq arg (1+ arg)))))
3813 (defun vip-scroll-back (arg)
3814 "Scroll to previous screen."
3816 (vip-scroll (- arg)))
3818 (defun vip-scroll-down (arg)
3819 "Pull down half screen."
3823 (scroll-down (/ (window-height) 2))
3826 (message "Beginning of buffer")
3827 (goto-char (point-min)))))
3829 (defun vip-scroll-down-one (arg)
3830 "Scroll up one line."
3834 (defun vip-scroll-up (arg)
3835 "Pull up half screen."
3839 (scroll-up (/ (window-height) 2))
3842 (message "End of buffer")
3843 (goto-char (point-max)))))
3845 (defun vip-scroll-up-one (arg)
3846 "Scroll down one line."
3853 (defun vip-if-string (prompt)
3854 (let ((s (vip-read-string-with-history
3858 (car vip-search-history))))
3859 (if (not (string= s ""))
3860 (setq vip-s-string s))))
3863 (defun vip-toggle-search-style (arg)
3864 "Toggle the value of vip-case-fold-search/vip-re-search.
3865 Without prefix argument, will ask which search style to toggle. With prefix
3866 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
3868 Although this function is bound to \\[vip-toggle-search-style], the most
3869 convenient way to use it is to bind `//' to the macro
3870 `1 M-x vip-toggle-search-style' and `///' to
3871 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
3872 toggle case-fold-search and hitting `/' three times witth toggle regexp
3873 search. Macros are more convenient in this case because they don't affect
3874 the Emacs binding of `/'."
3877 (cond ((or (eq arg 1)
3879 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3880 (if vip-case-fold-search
3881 "case-insensitive" "case-sensitive")
3882 (if vip-case-fold-search
3884 "case-insensitive")))))
3885 (setq vip-case-fold-search (null vip-case-fold-search))
3886 (if vip-case-fold-search
3887 (setq msg "Search becomes case-insensitive")
3888 (setq msg "Search becomes case-sensitive")))
3891 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3893 "regexp-search" "vanilla-search")
3896 "regexp-search")))))
3897 (setq vip-re-search (null vip-re-search))
3899 (setq msg "Search becomes regexp-style")
3900 (setq msg "Search becomes vanilla-style")))
3902 (setq msg "Search style remains unchanged")))
3906 (defun vip-search-forward (arg)
3907 "Search a string forward.
3908 ARG is used to find the ARG's occurrence of the string.
3909 Null string will repeat previous search."
3911 (let ((val (vip-P-val arg))
3912 (com (vip-getcom arg))
3913 (old-str vip-s-string))
3914 (setq vip-s-forward t)
3916 ;; this is not used at present, but may be used later
3917 (if (or (not (equal old-str vip-s-string))
3918 (not (markerp vip-local-search-start-marker))
3919 (not (marker-buffer vip-local-search-start-marker)))
3920 (setq vip-local-search-start-marker (point-marker)))
3921 (vip-search vip-s-string t val)
3924 (vip-move-marker-locally 'vip-com-point (mark t))
3925 (vip-execute-com 'vip-search-next val com)))))
3927 (defun vip-search-backward (arg)
3928 "Search a string backward.
3929 ARG is used to find the ARG's occurrence of the string.
3930 Null string will repeat previous search."
3932 (let ((val (vip-P-val arg))
3933 (com (vip-getcom arg))
3934 (old-str vip-s-string))
3935 (setq vip-s-forward nil)
3937 ;; this is not used at present, but may be used later
3938 (if (or (not (equal old-str vip-s-string))
3939 (not (markerp vip-local-search-start-marker))
3940 (not (marker-buffer vip-local-search-start-marker)))
3941 (setq vip-local-search-start-marker (point-marker)))
3942 (vip-search vip-s-string nil val)
3945 (vip-move-marker-locally 'vip-com-point (mark t))
3946 (vip-execute-com 'vip-search-next val com)))))
3949 (defun vip-search (string forward arg &optional no-offset init-point)
3950 "Search for COUNT's occurrence of STRING.
3951 Search is forward if FORWARD is non-nil, otherwise backward.
3952 INIT-POINT is the position where search is to start.
3953 Arguments: (STRING FORWARD COUNT &optional NO-OFFSET INIT-POINT LIMIT)."
3954 (if (not (equal string ""))
3955 (let ((val (vip-p-val arg))
3956 (com (vip-getcom arg))
3957 (null-arg (null (vip-P-val arg))) (offset (not no-offset))
3958 (case-fold-search vip-case-fold-search)
3959 (start-point (or init-point (point))))
3960 (vip-deactivate-mark)
3964 (if offset (vip-forward-char-carefully))
3967 (re-search-forward string nil nil val)
3968 (re-search-backward string))
3969 (search-forward string nil nil val)
3970 (search-backward string))
3971 (vip-flash-search-pattern)
3972 (if (not (equal start-point (point)))
3973 (push-mark start-point t)))
3975 (if (and null-arg vip-search-wrap-around-t)
3977 (message "Search wrapped around end of buffer")
3978 (goto-char (point-min))
3979 (vip-search string forward (cons 1 com) t start-point)
3980 ;; delete the wrapped around message
3981 (sit-for 2)(message "")
3983 (goto-char start-point)
3984 (error "`%s': %s not found"
3986 (if vip-re-search "Pattern" "String"))
3992 (re-search-backward string nil nil val)
3993 (search-backward string nil nil val))
3994 (vip-flash-search-pattern)
3995 (if (not (equal start-point (point)))
3996 (push-mark start-point t)))
3998 (if (and null-arg vip-search-wrap-around-t)
4000 (message "Search wrapped around beginning of buffer")
4001 (goto-char (point-max))
4002 (vip-search string forward (cons 1 com) t start-point)
4003 ;; delete the wrapped around message
4004 (sit-for 2)(message "")
4006 (goto-char start-point)
4007 (error "`%s': %s not found"
4009 (if vip-re-search "Pattern" "String"))
4012 (defun vip-search-next (arg)
4013 "Repeat previous search."
4015 (let ((val (vip-p-val arg))
4016 (com (vip-getcom arg)))
4017 (if (null vip-s-string) (error vip-NoPrevSearch))
4018 (vip-search vip-s-string vip-s-forward arg)
4021 (vip-move-marker-locally 'vip-com-point (mark t))
4022 (vip-execute-com 'vip-search-next val com)))))
4024 (defun vip-search-Next (arg)
4025 "Repeat previous search in the reverse direction."
4027 (let ((val (vip-p-val arg))
4028 (com (vip-getcom arg)))
4029 (if (null vip-s-string) (error vip-NoPrevSearch))
4030 (vip-search vip-s-string (not vip-s-forward) arg)
4033 (vip-move-marker-locally 'vip-com-point (mark t))
4034 (vip-execute-com 'vip-search-Next val com)))))
4037 ;; Search contents of buffer defined by one of Viper's motion commands.
4038 ;; Repeatable via `n' and `N'.
4039 (defun vip-buffer-search-enable (&optional c)
4040 (cond (c (setq vip-buffer-search-char c))
4041 ((null vip-buffer-search-char)
4042 (setq vip-buffer-search-char ?g)))
4043 (define-key vip-vi-basic-map
4044 (char-to-string vip-buffer-search-char) 'vip-command-argument)
4045 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
4046 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
4048 (defun vip-isearch-forward (arg)
4049 "This is a Viper wrap-around for isearch-forward."
4051 ;; emacs bug workaround
4052 (if (listp arg) (setq arg (car arg)))
4053 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
4055 (defun vip-isearch-backward (arg)
4056 "This is a Viper wrap-around for isearch-backward."
4058 ;; emacs bug workaround
4059 (if (listp arg) (setq arg (car arg)))
4060 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
4063 ;; visiting and killing files, buffers
4065 (defun vip-switch-to-buffer ()
4066 "Switch to buffer in the current window."
4071 (format "Switch to buffer in this window \(%s\): "
4072 (buffer-name (other-buffer (current-buffer))))))
4073 (switch-to-buffer buffer)
4076 (defun vip-switch-to-buffer-other-window ()
4077 "Switch to buffer in another window."
4082 (format "Switch to buffer in another window \(%s\): "
4083 (buffer-name (other-buffer (current-buffer))))))
4084 (switch-to-buffer-other-window buffer)
4087 (defun vip-kill-buffer ()
4090 (let (buffer buffer-name)
4093 (format "Kill buffer \(%s\): "
4094 (buffer-name (current-buffer)))))
4096 (if (null buffer-name)
4098 (get-buffer buffer-name)))
4099 (if (null buffer) (error "`%s': No such buffer" buffer-name))
4100 (if (or (not (buffer-modified-p buffer))
4103 "Buffer `%s' is modified, are you sure you want to kill it? "
4105 (kill-buffer buffer)
4106 (error "Buffer not killed"))))
4109 (defvar vip-smart-suffix-list '("" "tex" "c" "cc" "el" "p")
4110 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
4111 This is useful when you the current directory contains files with the same
4112 prefix and many different suffixes. Usually, only one of the suffixes
4113 represents an editable file. However, file completion will stop at the `.'
4114 The smart suffix feature lets you hit RET in such a case, and Viper will
4115 select the appropriate suffix.
4117 Suffixes are tried in the order given and the first suffix for which a
4118 corresponding file exists is selected. If no file exists for any of the
4119 suffixes, the user is asked to confirm.
4121 To turn this feature off, set this variable to nil.")
4123 ;; Try to add suffix to files ending with a `.'
4124 ;; Useful when the user hits RET on a non-completed file name.
4125 (defun vip-file-add-suffix ()
4127 (len (length vip-smart-suffix-list))
4128 (file (buffer-string))
4130 (goto-char (point-max))
4131 (if (and vip-smart-suffix-list (string-match "\\.$" file))
4133 (while (and (not found) (< count len))
4134 (setq suff (nth count vip-smart-suffix-list)
4136 (if (file-exists-p (format "%s%s" file suff))
4143 (vip-tmp-insert-at-eob " [Please complete file name]")
4145 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
4147 (key-binding (setq key (read-key-sequence nil))))
4148 (cond ((eq cmd 'self-insert-command)
4150 (insert (events-to-keys key))
4152 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
4154 (t (command-execute cmd)))
4160 ;; Advice for use in find-file and read-file-name commands.
4161 (defadvice exit-minibuffer (before vip-exit-minibuffer-advice activate)
4162 "Runs vip-minibuffer-exit-hook just before exiting the minibuffer.
4163 Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
4164 *after* exiting the minibuffer."
4165 (run-hooks 'vip-minibuffer-exit-hook))
4167 (defadvice find-file (before vip-add-suffix-advice activate)
4168 "Uses read-file-name to read arguments."
4169 (interactive (list (read-file-name "Find file: "
4170 nil default-directory))))
4172 (defadvice find-file-other-window (before vip-add-suffix-advice activate)
4173 "Uses read-file-name to read arguments."
4174 (interactive (list (read-file-name "Find file in other window: "
4175 nil default-directory))))
4177 ;; find-file-other-screen doesn't need advice because it apparently uses
4178 ;; read-file-name to read its argument.
4179 (defadvice find-file-other-frame (before vip-add-suffix-advice activate)
4180 "Uses read-file-name to read arguments."
4181 (interactive (list (read-file-name "Find file in other frame: "
4182 nil default-directory))))
4184 (defadvice read-file-name (around vip-suffix-advice activate)
4185 "Makes exit-minibuffer run `vip-file-add-suffix' as a hook."
4186 (let ((vip-minibuffer-exit-hook 'vip-file-add-suffix))
4189 ;; must be after we did advice or else the advice won't take hold
4191 (fset 'vip-find-file-other-frame
4192 (symbol-function 'find-file-other-screen))
4193 (fset 'vip-find-file-other-frame
4194 (symbol-function 'find-file-other-frame)))
4200 (defsubst vip-yank (text)
4201 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
4203 (setq this-command 'yank))
4205 (defun vip-put-back (arg)
4206 "Put back after point/below line."
4208 (let ((val (vip-p-val arg))
4209 (text (if vip-use-register
4210 (cond ((vip-valid-register vip-use-register '(digit))
4211 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4212 ((vip-valid-register vip-use-register)
4213 (get-register (downcase vip-use-register)))
4214 (t (error vip-InvalidRegister vip-use-register)))
4217 (if vip-use-register
4218 (let ((reg vip-use-register))
4219 (setq vip-use-register nil)
4220 (error vip-EmptyRegister reg))
4222 (setq vip-use-register nil)
4223 (if (vip-end-with-a-newline-p text)
4228 (beginning-of-line))
4229 (if (not (eolp)) (vip-forward-char-carefully)))
4230 (set-marker (vip-mark-marker) (point) (current-buffer))
4231 (vip-set-destructive-command
4232 (list 'vip-put-back val nil vip-use-register nil nil))
4233 (vip-loop val (vip-yank text)))
4234 (exchange-point-and-mark)
4235 (vip-deactivate-mark))
4237 (defun vip-Put-back (arg)
4238 "Put back at point/above line."
4240 (let ((val (vip-p-val arg))
4241 (text (if vip-use-register
4242 (cond ((vip-valid-register vip-use-register '(digit))
4243 (current-kill (- vip-use-register ?1) 'do-not-rotate))
4244 ((vip-valid-register vip-use-register)
4245 (get-register (downcase vip-use-register)))
4246 (t (error vip-InvalidRegister vip-use-register)))
4249 (if vip-use-register
4250 (let ((reg vip-use-register))
4251 (setq vip-use-register nil)
4252 (error vip-EmptyRegister reg))
4254 (setq vip-use-register nil)
4255 (if (vip-end-with-a-newline-p text) (beginning-of-line))
4256 (vip-set-destructive-command
4257 (list 'vip-Put-back val nil vip-use-register nil nil))
4258 (set-marker (vip-mark-marker) (point) (current-buffer))
4259 (vip-loop val (vip-yank text)))
4260 (exchange-point-and-mark)
4261 (vip-deactivate-mark))
4264 (defun vip-copy-region-as-kill (beg end)
4265 "Copy region to kill-ring.
4266 If BEG and END do not belong to the same buffer, copy empty region."
4268 (copy-region-as-kill beg end)
4269 (error (copy-region-as-kill beg beg))))
4271 (defun vip-save-last-insertion (beg end)
4272 "Saves last inserted text for possible use by vip-repeat command."
4273 (setq vip-last-insertion (buffer-substring beg end))
4274 (or (< (length vip-d-com) 5)
4275 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
4276 (or (null vip-command-ring)
4277 (ring-empty-p vip-command-ring)
4279 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
4281 ;; del most recent elt, if identical to the second most-recent
4282 (vip-cleanup-ring vip-command-ring)))
4285 (defsubst vip-yank-last-insertion ()
4286 "Inserts the text saved by the previous vip-save-last-insertion command."
4288 (insert vip-last-insertion)
4292 (defun vip-delete-char (arg)
4295 (let ((val (vip-p-val arg)))
4296 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
4299 (let ((here (point)))
4301 (if (> val (- (point) here))
4302 (setq val (- (point) here))))))
4303 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
4304 (if (and vip-ex-style-motion (eolp))
4305 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
4306 (if vip-use-register
4308 (cond ((vip-valid-register vip-use-register '((Letter)))
4309 (vip-append-to-register
4310 (downcase vip-use-register) (point) (- (point) val)))
4311 ((vip-valid-register vip-use-register)
4313 vip-use-register (point) (- (point) val) nil))
4314 (t (error vip-InvalidRegister vip-use-register)))
4315 (setq vip-use-register nil)))
4316 (if vip-ex-style-motion
4319 (if (and (eolp) (not (bolp))) (backward-char 1)))
4321 (delete-backward-char val t)
4322 (delete-char val t)))))
4324 (defun vip-delete-backward-char (arg)
4325 "Delete previous character. On reaching beginning of line, stop and beep."
4327 (let ((val (vip-p-val arg)))
4328 (vip-set-destructive-command
4329 (list 'vip-delete-backward-char val nil nil nil nil))
4332 (let ((here (point)))
4334 (if (> val (- here (point)))
4335 (setq val (- here (point)))))))
4336 (if vip-use-register
4338 (cond ((vip-valid-register vip-use-register '(Letter))
4339 (vip-append-to-register
4340 (downcase vip-use-register) (point) (+ (point) val)))
4341 ((vip-valid-register vip-use-register)
4343 vip-use-register (point) (+ (point) val) nil))
4344 (t (error vip-InvalidRegister vip-use-register)))
4345 (setq vip-use-register nil)))
4347 (delete-backward-char val t))))
4349 (defun vip-del-backward-char-in-insert ()
4350 "Delete 1 char backwards while in insert mode."
4352 (if (and vip-ex-style-editing-in-insert (bolp))
4354 (delete-backward-char 1 t)))
4356 (defun vip-del-backward-char-in-replace ()
4357 "Delete one character in replace mode.
4358 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
4359 charecters. If it is nil, then the cursor just moves backwards, similarly
4360 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
4361 cursor move past the beginning of the replacement region."
4363 (cond (vip-delete-backwards-in-replace
4365 (delete-backward-char 1 t))
4366 (vip-ex-style-editing-in-insert
4371 (delete-backward-char 1 t))))
4372 (vip-ex-style-editing-in-insert
4377 (backward-char 1))))
4383 (defun vip-join-lines (arg)
4384 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4386 (let ((val (vip-P-val arg)))
4387 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
4388 (vip-loop (if (null val) 1 (1- val))
4394 (delete-region (point) (1- (point)))
4395 (fixup-whitespace)))))))
4400 (defun vip-change (beg end)
4401 (if (markerp beg) (setq beg (marker-position beg)))
4402 (if (markerp end) (setq end (marker-position end)))
4403 ;; beg is sometimes (mark t), which may be nil
4404 (or beg (setq beg end))
4406 (vip-set-complex-command-for-undo)
4407 (if vip-use-register
4409 (copy-to-register vip-use-register beg end nil)
4410 (setq vip-use-register nil)))
4411 (vip-set-replace-overlay beg end)
4412 (setq last-command nil) ; separate repl text from prev kills
4414 (if (= (vip-replace-start) (point-max))
4415 (error "End of buffer"))
4417 (setq vip-last-replace-region
4418 (buffer-substring (vip-replace-start)
4421 ;; protect against error while inserting "@" and other disasters
4422 ;; (e.g., read-only buff)
4423 (condition-case conds
4424 (if (vip-same-line (vip-replace-start)
4426 (let ((delim-end (if (= (length vip-replace-region-end-symbol) 0)
4428 (substring vip-replace-region-end-symbol 0 1))))
4430 ;; tabs cause problems in replace, so untabify
4431 (goto-char (vip-replace-end))
4432 (insert-before-markers "@") ; put placeholder after the TAB
4434 (untabify (vip-replace-start) (point))
4435 ;; del @ and the char under the '$'; don't put on kill ring
4436 (delete-backward-char (1+ (length delim-end)))
4438 ;; this move takes care of the last posn in the overlay, which
4439 ;; has to be shifted because of insert. We can't simply insert
4440 ;; "$" before-markers because then overlay-start will shift the
4441 ;; beginning of the overlay in case we are replacing a single
4442 ;; character. This fixes the bug with `s' and `cl' commands.
4443 (vip-move-replace-overlay (vip-replace-start) (point))
4444 (goto-char (vip-replace-start))
4445 (vip-change-state-to-replace t))
4446 (kill-region (vip-replace-start)
4448 (vip-change-state-to-insert))
4449 (error ;; make sure that the overlay doesn't stay.
4450 ;; go back to the original point
4451 (goto-char (vip-replace-start))
4452 (vip-hide-replace-overlay)
4453 (vip-message-conditions conds))))
4456 (defun vip-change-subr (beg end)
4457 ;; beg is sometimes (mark t), which may be nil
4458 (or beg (setq beg end))
4460 (if vip-use-register
4462 (copy-to-register vip-use-register beg end nil)
4463 (setq vip-use-register nil)))
4464 (kill-region beg end)
4465 (setq this-command 'vip-change)
4466 (vip-yank-last-insertion))
4468 (defun vip-toggle-case (arg)
4469 "Toggle character case."
4471 (let ((val (vip-p-val arg)) (c))
4472 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
4474 (setq c (following-char))
4476 (if (eq c (upcase c))
4477 (insert-char (downcase c) 1)
4478 (insert-char (upcase c) 1))
4479 (setq val (1- val)))))
4484 (defun vip-query-replace ()
4486 If a null string is suplied as the string to be replaced,
4487 the query replace mode will toggle between string replace
4488 and regexp replace."
4491 (setq str (vip-read-string-with-history
4492 (if vip-re-query-replace "Query replace regexp: "
4495 'vip-replace1-history
4496 (car vip-replace1-history) ; default
4498 (if (string= str "")
4500 (setq vip-re-query-replace (not vip-re-query-replace))
4501 (message "Query replace mode changed to %s"
4502 (if vip-re-query-replace "regexp replace"
4504 (if vip-re-query-replace
4505 (query-replace-regexp
4507 (vip-read-string-with-history
4508 (format "Query replace regexp `%s' with: " str)
4510 'vip-replace1-history
4511 (car vip-replace1-history) ; default
4515 (vip-read-string-with-history
4516 (format "Query replace `%s' with: " str)
4518 'vip-replace1-history
4519 (car vip-replace1-history) ; default
4525 (defun vip-mark-beginning-of-buffer ()
4528 (goto-char (point-min))
4529 (exchange-point-and-mark)
4530 (message "Mark set at the beginning of buffer"))
4532 (defun vip-mark-end-of-buffer ()
4535 (goto-char (point-max))
4536 (exchange-point-and-mark)
4537 (message "Mark set at the end of buffer"))
4539 (defun vip-mark-point ()
4541 (let ((char (vip-read-char-exclusive)))
4542 (cond ((and (<= ?a char) (<= char ?z))
4543 (point-to-register (1+ (- char ?a))))
4544 ((= char ?<) (vip-mark-beginning-of-buffer))
4545 ((= char ?>) (vip-mark-end-of-buffer))
4546 ((= char ?.) (vip-set-mark-if-necessary))
4547 ((= char ?,) (vip-cycle-through-mark-ring))
4548 ((= char ?D) (mark-defun))
4552 ;; Algorithm: If first invocation of this command save mark on ring, goto
4553 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4554 ;; making it into the new mark, M1.
4555 ;; Push this mark back and set mark to the original point position, p1.
4556 ;; So, if you hit '' or `` then you can return to p1.
4558 ;; If repeated command, pop top elt from the ring into mark and
4559 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4560 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4561 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4563 (defun vip-cycle-through-mark-ring ()
4564 "Visit previous locations on the mark ring.
4565 One can use `` and '' to temporarily jump 1 step back."
4566 (let* ((sv-pt (point)))
4567 ;; if repeated `m,' command, pop the previously saved mark.
4568 ;; Prev saved mark is actually prev saved point. It is used if the
4569 ;; user types `` or '' and is discarded
4570 ;; from the mark ring by the next `m,' command.
4571 ;; In any case, go to the previous or previously saved mark.
4572 ;; Then push the current mark (popped off the ring) and set current
4573 ;; point to be the mark. Current pt as mark is discarded by the next
4575 (if (eq last-command 'vip-cycle-through-mark-ring)
4577 ;; save current mark if the first iteration
4578 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4580 (push-mark (mark t) t)) )
4582 (set-mark-command 1)
4583 ;; don't duplicate mark on the ring
4584 (setq mark-ring (delete (vip-mark-marker) mark-ring))
4586 (vip-deactivate-mark)
4587 (setq this-command 'vip-cycle-through-mark-ring)
4591 (defun vip-goto-mark (arg)
4594 (let ((char (read-char))
4595 (com (vip-getcom arg)))
4596 (vip-goto-mark-subr char com nil)))
4598 (defun vip-goto-mark-and-skip-white (arg)
4599 "Go to mark and skip to first non-white character on line."
4601 (let ((char (read-char))
4602 (com (vip-getCom arg)))
4603 (vip-goto-mark-subr char com t)))
4605 (defun vip-goto-mark-subr (char com skip-white)
4608 (error "Empty buffer")
4610 (cond ((vip-valid-register char '(letter))
4611 (let* ((buff (current-buffer))
4612 (reg (1+ (- char ?a)))
4613 (text-marker (get-register reg)))
4614 (if com (vip-move-marker-locally 'vip-com-point (point)))
4615 (if (not (vip-valid-marker text-marker))
4616 (error (format vip-EmptyTextmarker char)))
4617 (if (and (vip-same-line (point) vip-last-jump)
4618 (= (point) vip-last-jump-ignore))
4619 (push-mark vip-last-jump t)
4620 (push-mark nil t)) ; no msg
4621 (vip-register-to-point reg)
4622 (setq vip-last-jump (point-marker))
4624 (back-to-indentation)
4625 (setq vip-last-jump-ignore (point))))
4627 (if (equal buff (current-buffer))
4628 (vip-execute-com (if skip-white
4629 'vip-goto-mark-and-skip-white
4632 (switch-to-buffer buff)
4633 (goto-char vip-com-point)
4634 (vip-change-state-to-vi)
4636 ((and (not skip-white) (= char ?`))
4637 (if com (vip-move-marker-locally 'vip-com-point (point)))
4638 (if (and (vip-same-line (point) vip-last-jump)
4639 (= (point) vip-last-jump-ignore))
4640 (goto-char vip-last-jump))
4641 (if (= (point) (mark t)) (pop-mark))
4642 (exchange-point-and-mark)
4643 (setq vip-last-jump (point-marker)
4644 vip-last-jump-ignore 0)
4645 (if com (vip-execute-com 'vip-goto-mark nil com)))
4646 ((and skip-white (= char ?'))
4647 (if com (vip-move-marker-locally 'vip-com-point (point)))
4648 (if (and (vip-same-line (point) vip-last-jump)
4649 (= (point) vip-last-jump-ignore))
4650 (goto-char vip-last-jump))
4651 (if (= (point) (mark t)) (pop-mark))
4652 (exchange-point-and-mark)
4653 (setq vip-last-jump (point))
4654 (back-to-indentation)
4655 (setq vip-last-jump-ignore (point))
4656 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
4657 (t (error vip-InvalidTextmarker char))))
4659 (defun vip-insert-tab ()
4663 (defun vip-exchange-point-and-mark ()
4665 (exchange-point-and-mark)
4666 (back-to-indentation))
4668 ;; Input Mode Indentation
4670 (defun vip-forward-indent ()
4671 "Indent forward -- `C-t' in Vi."
4674 (indent-to (+ (current-column) vip-shift-width)))
4676 (defun vip-backward-indent ()
4677 "Backtab, C-d in VI"
4680 (let ((p (point)) (c (current-column)) bol (indent t))
4681 (if (vip-looking-back "[0^]")
4683 (if (= ?^ (preceding-char)) (setq vip-preserve-indent t))
4684 (delete-backward-char 1)
4690 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4691 (delete-region (point) p)
4693 (indent-to (- c vip-shift-width)))
4694 (if (or (bolp) (vip-looking-back "[^ \t]"))
4695 (setq vip-cted nil)))))
4697 (defun vip-autoindent ()
4698 "Auto Indentation, Vi-style."
4700 (let ((col (current-indentation)))
4701 (if (not vip-preserve-indent)
4702 (setq vip-current-indent col)
4703 (setq vip-preserve-indent nil))
4708 (indent-to vip-current-indent)))))
4711 ;; Viewing registers
4713 (defun vip-ket-function (arg)
4714 "Function called by \], the ket. View registers and call \]\]."
4716 (let ((reg (read-char)))
4717 (cond ((vip-valid-register reg '(letter Letter))
4718 (view-register (downcase reg)))
4719 ((vip-valid-register reg '(digit))
4720 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4722 (set-buffer (get-buffer-create "*Output*"))
4723 (delete-region (point-min) (point-max))
4724 (insert (format "Register %c contains the string:\n" reg))
4726 (goto-char (point-min)))
4727 (display-buffer "*Output*")))
4729 (vip-next-heading arg))
4731 vip-InvalidRegister reg)))))
4733 (defun vip-brac-function (arg)
4734 "Function called by \[, the brac. View textmarkers and call \[\["
4736 (let ((reg (read-char)))
4738 (vip-prev-heading arg))
4740 (vip-heading-end arg))
4741 ((vip-valid-register reg '(letter))
4742 (let* ((val (get-register (1+ (- reg ?a))))
4745 (format vip-EmptyTextmarker reg))
4746 (marker-buffer val)))
4747 (pos (marker-position val))
4748 line-no text (s pos) (e pos))
4750 (set-buffer (get-buffer-create "*Output*"))
4751 (delete-region (point-min) (point-max))
4756 (setq line-no (1+ (count-lines (point-min) val)))
4759 (if (re-search-backward "[^ \t]" nil t)
4765 (if (re-search-forward "[^ \t]" nil t)
4769 (setq text (buffer-substring s e))
4770 (setq text (format "%s<%c>%s"
4771 (substring text 0 (- pos s))
4772 reg (substring text (- pos s)))))
4775 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4776 reg (buffer-name buf) line-no))
4777 (insert (format "Here is some text around %c:\n\n %s"
4779 (insert (format vip-EmptyTextmarker reg)))
4780 (goto-char (point-min)))
4781 (display-buffer "*Output*")))
4782 (t (error vip-InvalidTextmarker reg)))))
4786 ;; commands in insertion mode
4788 (defun vip-delete-backward-word (arg)
4789 "Delete previous word."
4794 (delete-region (point) (mark t))
4798 (defun vip-set-expert-level (&optional dont-change-unless)
4799 "Sets the expert level for a Viper user.
4800 Can be called interactively to change (temporarily or permanently) the
4801 current expert level.
4803 The optional argument DONT-CHANGE-UNLESS if not nil, says that
4804 the level should not be changed, unless its current value is
4805 meaningless (i.e., not one of 1,2,3,4,5).
4807 User level determines the setting of Viper variables that are most
4808 sensitive for VI-style look-and-feel."
4812 (if (not (numberp vip-expert-level)) (setq vip-expert-level 0))
4814 (save-window-excursion
4815 (delete-other-windows)
4816 ;; if 0 < vip-expert-level < vip-max-expert-level
4817 ;; & dont-change-unless = t -- use it; else ask
4818 (vip-ask-level dont-change-unless))
4821 vip-ex-style-motion t
4822 vip-ex-style-editing-in-insert t
4823 vip-want-ctl-h-help nil)
4826 ;; a novice or a beginner
4827 ((eq vip-expert-level 1)
4828 (global-set-key vip-toggle-key ;; in emacs-state
4832 (setq vip-no-multiple-ESC t
4834 vip-vi-style-in-minibuffer t
4835 vip-search-wrap-around-t t
4836 vip-want-emacs-keys-in-vi nil
4837 vip-want-emacs-keys-in-insert nil))
4839 ;; an intermediate to guru
4840 ((and (> vip-expert-level 1) (< vip-expert-level 5))
4841 (setq vip-no-multiple-ESC (if window-system t 'twice)
4842 vip-want-emacs-keys-in-vi t
4843 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4845 (if (eq vip-expert-level 4) ; respect user's ex-style motions
4846 ; and vip-no-multiple-ESC
4848 (setq-default vip-ex-style-editing-in-insert
4849 (cdr (assoc 'vip-ex-style-editing-in-insert
4850 vip-saved-user-settings))
4852 (cdr (assoc 'vip-ex-style-motion
4853 vip-saved-user-settings)))
4854 (setq vip-ex-style-motion
4855 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4856 vip-ex-style-editing-in-insert
4857 (cdr (assoc 'vip-ex-style-editing-in-insert
4858 vip-saved-user-settings))
4860 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4862 (cdr (assoc 'vip-no-multiple-ESC
4863 vip-saved-user-settings))))))
4866 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4867 ;; user toggle variable values.
4868 (t (setq-default vip-ex-style-editing-in-insert
4869 (cdr (assoc 'vip-ex-style-editing-in-insert
4870 vip-saved-user-settings))
4872 (cdr (assoc 'vip-ex-style-motion
4873 vip-saved-user-settings)))
4874 (setq vip-want-ctl-h-help
4875 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
4877 (cdr (assoc 'vip-always vip-saved-user-settings))
4879 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
4881 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4882 vip-ex-style-editing-in-insert
4883 (cdr (assoc 'vip-ex-style-editing-in-insert
4884 vip-saved-user-settings))
4886 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4887 vip-want-emacs-keys-in-vi
4888 (cdr (assoc 'vip-want-emacs-keys-in-vi
4889 vip-saved-user-settings))
4890 vip-want-emacs-keys-in-insert
4891 (cdr (assoc 'vip-want-emacs-keys-in-insert
4892 vip-saved-user-settings)))))
4893 (vip-set-mode-vars-for vip-current-state)
4895 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
4898 (defun vip-ask-level (dont-change-unless)
4899 "Ask user expert level."
4900 (let ((ask-buffer " *vip-ask-level*")
4901 level-changed repeated)
4902 (save-window-excursion
4903 (switch-to-buffer ask-buffer)
4905 (or (eq this-command 'vip-set-expert-level)
4907 (<= vip-expert-level vip-max-expert-level)
4908 (>= vip-expert-level 1))
4912 *** Important Notice for VIP users***
4917 Viper Is a Package for Emacs Rebels,
4918 a VI Plan for Emacs Rescue,
4919 and a venomous VI PERil.
4922 Technically speaking, Viper is a new Vi emulator that replaces
4923 the old VIP package.
4925 Viper emulates Vi much better than VIP. It also significantly
4926 extends and improves upon Vi in many useful ways.
4928 Although many VIP settings in your ~/.vip are compatible with Viper,
4929 you may have to change some of them. Please refer to the documentation,
4930 which can be obtained by executing
4934 when Viper is in Vi state.
4936 If you will be so lucky as to find a bug, report it via the command
4940 Type any key to continue... ")
4945 (while (or (> vip-expert-level vip-max-expert-level)
4946 (< vip-expert-level 1)
4947 (null dont-change-unless))
4951 (message "Invalid user level")
4954 (setq dont-change-unless t
4957 Please specify your level of familiarity with the venomous VI PERil
4958 (and the VI Plan for Emacs Rescue).
4959 You can change it at any time by typing `M-x vip-set-expert-level RET'
4961 1 -- BEGINNER: Almost all Emacs features are suppressed.
4962 Feels almost like straight Vi. File name completion and
4963 command history in the minibuffer are thrown in as a bonus.
4964 To use Emacs productively, you must reach level 3 or higher.
4965 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4966 so most Emacs commands can be used when Viper is in Vi state.
4967 Good progress---you are well on the way to level 3!
4968 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
4969 in Viper's insert state.
4970 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
4971 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
4972 variables. Adjust these settings to your taste.
4973 5 -- WIZARD: Like 4, but user settings are also respected for vip-always,
4974 vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and
4975 vip-want-emacs-keys-in-insert. Adjust these to your taste.
4977 Please, specify your level now: ")
4979 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
4982 ;; tell the user if level was changed
4986 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4988 (if (y-or-n-p "Do you wish to make this change permanent? ")
4989 ;; save the setting for vip-expert-level
4992 (format "Saving user level %d ..." vip-expert-level)
4993 vip-custom-file-name))
4995 (bury-buffer) ; remove ask-buffer from screen
5000 (defun viper-version ()
5002 (message "Viper version is %s" viper-version))
5004 (defalias 'vip-version 'viper-version)
5011 ;; Returns t, if the string before point matches the regexp STR.
5012 (defsubst vip-looking-back (str)
5013 (and (save-excursion (re-search-backward str nil t))
5014 (= (point) (match-end 0))))
5018 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
5019 (defun vip-register-to-point (char &optional enforce-buffer)
5020 "Like jump-to-register, but switches to another buffer in another window."
5021 (interactive "cViper register to point: ")
5022 (let ((val (get-register char)))
5024 ((and (fboundp 'frame-configuration-p)
5025 (frame-configuration-p val))
5026 (set-frame-configuration val))
5027 ((window-configuration-p val)
5028 (set-window-configuration val))
5029 ((vip-valid-marker val)
5030 (if (and enforce-buffer
5031 (not (equal (current-buffer) (marker-buffer val))))
5032 (error (concat vip-EmptyTextmarker " in this buffer")
5034 (pop-to-buffer (marker-buffer val))
5036 ((and (consp val) (eq (car val) 'file))
5037 (find-file (cdr val)))
5039 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
5042 (defun vip-save-kill-buffer ()
5043 "Save then kill current buffer. "
5045 (if (< vip-expert-level 2)
5046 (save-buffers-kill-emacs)
5048 (kill-buffer (current-buffer))))
5054 (defun vip-submit-report ()
5055 "Submit bug report on Viper."
5057 (let ((reporter-prompt-for-summary-p t)
5058 color-display-p frame-parameters
5059 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
5060 varlist salutation window-config)
5062 ;; If mode info is needed, add variable to `let' and then set it below,
5063 ;; like we did with color-display-p.
5064 (setq color-display-p (if window-system
5065 (vip-display-color-p)
5067 minibuffer-vi-face (if window-system
5068 (vip-get-face vip-minibuffer-vi-face)
5070 minibuffer-insert-face (if window-system
5071 (vip-get-face vip-minibuffer-insert-face)
5073 minibuffer-emacs-face (if window-system
5074 (vip-get-face vip-minibuffer-emacs-face)
5076 frame-parameters (if (fboundp 'vip-frame-parameters)
5077 (vip-frame-parameters (vip-selected-frame))))
5079 (setq varlist (list 'vip-vi-minibuffer-minor-mode
5080 'vip-insert-minibuffer-minor-mode
5081 'vip-vi-intercept-minor-mode
5082 'vip-vi-local-user-minor-mode
5083 'vip-vi-kbd-minor-mode
5084 'vip-vi-global-user-minor-mode
5085 'vip-vi-state-modifier-minor-mode
5086 'vip-vi-diehard-minor-mode
5087 'vip-vi-basic-minor-mode
5088 'vip-replace-minor-mode
5089 'vip-insert-intercept-minor-mode
5090 'vip-insert-local-user-minor-mode
5091 'vip-insert-kbd-minor-mode
5092 'vip-insert-global-user-minor-mode
5093 'vip-insert-state-modifier-minor-mode
5094 'vip-insert-diehard-minor-mode
5095 'vip-insert-basic-minor-mode
5096 'vip-emacs-intercept-minor-mode
5097 'vip-emacs-local-user-minor-mode
5098 'vip-emacs-kbd-minor-mode
5099 'vip-emacs-global-user-minor-mode
5100 'vip-emacs-state-modifier-minor-mode
5101 'vip-automatic-iso-accents
5102 'vip-want-emacs-keys-in-insert
5103 'vip-want-emacs-keys-in-vi
5104 'vip-keep-point-on-undo
5105 'vip-no-multiple-ESC
5107 'vip-want-ctl-h-help
5108 'vip-ex-style-editing-in-insert
5109 'vip-delete-backwards-in-replace
5110 'vip-vi-style-in-minibuffer
5112 'vip-insert-state-hooks
5113 'vip-replace-state-hooks
5114 'vip-emacs-state-hooks
5115 'ex-cycle-other-window
5116 'ex-cycle-through-non-files
5123 'minibuffer-insert-face
5124 'minibuffer-emacs-face
5127 Congratulations! You may have unearthed a bug in Viper!
5128 Please mail a concise, accurate summary of the problem to the address above.
5130 -------------------------------------------------------------------")
5131 (setq window-config (current-window-configuration))
5132 (with-output-to-temp-buffer " *vip-info*"
5133 (switch-to-buffer " *vip-info*")
5134 (delete-other-windows)
5136 PLEASE FOLLOW THESE PROCEDURES
5137 ------------------------------
5139 Before reporting a bug, please verify that it is related to Viper, and is
5140 not cause by other packages you are using.
5142 Don't report compilation warnings, unless you are certain that there is a
5143 problem. These warnings are normal and unavoidable.
5145 Please note that users should not modify variables and keymaps other than
5146 those advertised in the manual. Such `customization' is likely to crash
5147 Viper, as it would any other improperly customized Emacs package.
5149 If you are reporting an error message received while executing one of the
5150 Viper commands, type:
5152 M-x set-variable <Return> debug-on-error <Return> t <Return>
5154 Then reproduce the error. The above command will cause Emacs to produce a
5155 back trace of the execution that leads to the error. Please include this
5156 trace in your bug report.
5158 If you believe that one of Viper's commands goes into an infinite loop
5159 \(e.g., Emacs freezes\), type:
5161 M-x set-variable <Return> debug-on-quit <Return> t <Return>
5163 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
5164 the current command. Include the resulting back trace in the bug report.
5166 Mail anyway (y or n)? ")
5167 (if (y-or-n-p "Mail anyway? ")
5169 (set-window-configuration window-config)
5170 (error "Bug report aborted")))
5173 (set-window-configuration window-config)
5175 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
5178 nil 'delete-other-windows
5185 ;; Needed to smooth out the difference between Emacs' unread-command-events
5186 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
5187 ;; events or a sequence of keys.
5188 ;; The semantics of placing an event on unread-command-event in XEmacs is
5189 ;; not the same as placing (setq unread-command-event event)
5190 ;; on the event queue using enqueue-eval-event. For instance, an event
5191 ;; sitting in unread-command-event will be available to (next-event).
5192 ;; In contrast, evals placed on event queue are not evaluated until all
5193 ;; previous commands have been executed. This makes a difference when one
5194 ;; of the events placed on the event queue is bound to a function that
5195 ;; pauses for input, because these evals won't make input immediately
5198 ;; Due to a bug in unread-command-events, a non-event symbol in
5199 ;; unread-command-evets list may cause Emacs to label this symbol to be an
5200 ;; event. Below, we delete nil from event lists, since nil is the most
5201 ;; common problem here. Hopefully, unread-command-evets will be fixed in
5202 ;; the next release.
5203 (defun vip-set-unread-command-events (arg)
5205 (setq unread-command-events
5207 (cond ((eventp arg) (list arg))
5210 (listify-key-sequence arg))
5212 "vip-set-unread-command-events: Invalid arg, %S"
5214 (if (not (eventp nil))
5215 (setq new-events (delq nil new-events)))
5216 (append new-events unread-command-events)))
5218 (cond ((numberp arg)
5219 (setq unread-command-event (character-to-event arg)))
5221 (setq unread-command-event arg))
5223 (let ((length (length arg))
5225 (while (< count length)
5227 'vip-fudge-event-list-in-xemacs
5229 (character-to-event (elt arg count))
5231 (setq count (1+ count))
5234 (or arg unread-command-event))))
5235 (t (error "vip-set-unread-command-events: Invalid argument")))))
5237 (defun vip-fudge-event-list-in-xemacs (arg)
5238 (setq unread-command-event arg))
5241 ;;; Bring in the rest of the files
5242 (require 'viper-mous)
5243 (require 'viper-macs)
5248 ;; The following is provided for compatibility with older VIP's
5250 (defalias 'vip-change-mode-to-vi 'vip-change-state-to-vi)
5251 (defalias 'vip-change-mode-to-insert 'vip-change-state-to-insert)
5252 (defalias 'vip-change-mode-to-emacs 'vip-change-state-to-emacs)
5254 ;; This was the main Vi mode in old versions of VIP which may have been
5255 ;; extensively used by VIP users. We declare it as a global var
5256 ;; and, after .vip is loaded, we add this keymap to vip-vi-basic-map.
5257 (defvar vip-mode-map (make-sparse-keymap)
5258 "This was the main Vi-mode keymap in the old versions of VIP.
5259 Viper provides this variable for compatibility. Whatever the user defines
5260 for this map, is merged into Viper's vip-vi-basic-map after loading .vip")
5264 ;; Load .vip and setup hooks
5265 (defun vip-shell-mode-hook ()
5266 "Hook specifically designed to enable Vi-style editing in shell mode."
5267 (setq vip-add-newline-at-eob nil)
5268 ;; this is nicer in shell mode
5269 (setq vip-ex-style-editing-in-insert nil
5270 vip-ex-style-motion nil)
5271 (vip-add-local-keys 'vi-state
5272 '(("\C-m" . comint-send-input) ; return
5273 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5274 (vip-add-local-keys 'insert-state
5275 '(("\C-m" . comint-send-input) ; return
5276 ("\C-d" . comint-delchar-or-maybe-eof))) ; \C-d
5280 ;; This sets major mode hooks to make them come up in vip-state.
5281 (defun vip-set-hooks ()
5283 ;; It is of course a misnomer to call viper-mode a `major mode'.
5284 ;; However, this has the effect that if the user didn't specify the
5285 ;; default mode, new buffers that fall back on the default will come up
5286 ;; in Fundamental Mode and Vi state.
5287 (setq default-major-mode 'viper-mode)
5289 (defadvice fundamental-mode (after vip-fundamental-mode-ad activate)
5290 (vip-change-state-to-vi))
5292 ;; The following major modes should come up in vi-state
5293 (defvar emacs-lisp-mode-hook nil)
5294 (add-hook 'emacs-lisp-mode-hook 'viper-mode)
5296 (defvar lisp-mode-hook nil)
5297 (add-hook 'lisp-mode-hook 'viper-mode)
5299 (defvar bibtex-mode-hook nil)
5300 (add-hook 'bibtex-mode-hook 'viper-mode)
5302 (defvar cc-mode-hook nil)
5303 (add-hook 'cc-mode-hook 'viper-mode)
5305 (defvar c-mode-hook nil)
5306 (add-hook 'c-mode-hook 'viper-mode)
5308 (defvar c++-mode-hook nil)
5309 (add-hook 'c++-mode-hook 'viper-mode)
5311 (defvar lisp-interaction-mode-hook nil)
5312 (add-hook 'lisp-interaction-mode-hook 'viper-mode)
5314 (defvar text-mode-hook nil)
5315 (add-hook 'text-mode-hook 'viper-mode)
5317 (add-hook 'completion-list-mode-hook 'viper-mode)
5318 (add-hook 'compilation-mode-hook 'viper-mode)
5320 (defvar emerge-startup-hook nil)
5321 (add-hook 'emerge-startup-hook 'vip-change-state-to-emacs)
5322 ;; Run vip-change-state-to-vi after quitting emerge.
5323 (vip-eval-after-load "emerge"
5324 '(defadvice emerge-quit (after vip-emerge-advice activate)
5325 "Run vip-change-state-to-vi after quitting emerge."
5326 (vip-change-state-to-vi)))
5327 ;; In case Emerge was loaded before Viper.
5328 (defadvice emerge-quit (after vip-emerge-advice activate)
5329 "Run vip-change-state-to-vi after quitting emerge."
5330 (vip-change-state-to-vi))
5332 (vip-eval-after-load "asm-mode"
5333 '(defadvice asm-mode (after vip-asm-mode-ad activate)
5334 "Run vip-change-state-to-vi on entry."
5335 (vip-change-state-to-vi)))
5337 ;; passwd.el sets up its own buffer, which turns up in Vi mode,
5338 ;; overriding the local map. Noone needs Vi mode here.
5339 (vip-eval-after-load
5341 '(defadvice read-passwd-1 (before vip-passwd-ad activate)
5342 "Vi-ism is prohibited when reading passwords, so switch to Emacs."
5343 (vip-change-state-to-emacs)))
5346 (defvar shell-mode-hook nil)
5347 (add-hook 'shell-mode-hook 'vip-change-state-to-insert)
5348 (add-hook 'shell-mode-hook 'vip-shell-mode-hook)
5351 (defvar sh-mode-hook nil)
5352 (add-hook 'sh-mode-hook 'viper-mode)
5355 ;; This is only necessary when the user uses vip-modify-major-mode
5356 (add-hook 'dired-mode-hook 'vip-change-state-to-emacs)
5358 (defvar view-hook nil
5359 "View hook. Run after view mode.")
5360 (add-hook 'view-hook 'vip-change-state-to-emacs)
5363 ;; Put summary and other VM buffers in Emacs state.
5364 (defvar vm-mode-hooks nil
5365 "This hook is run after vm is started.")
5366 (defvar vm-summary-mode-hooks nil
5367 "This hook is run after vm switches to summary mode.")
5368 (add-hook 'vm-mode-hooks 'vip-change-state-to-emacs)
5369 (add-hook 'vm-summary-mode-hooks 'vip-change-state-to-emacs)
5372 ;; Put buf in Emacs state after edit.
5373 (vip-eval-after-load
5375 '(defadvice rmail-cease-edit (after vip-rmail-advice activate)
5376 "Switch buffer to emacs-state after finishing with editing a message."
5377 (vip-change-state-to-emacs)))
5378 ;; In case RMAIL was loaded before Viper.
5379 (defadvice rmail-cease-edit (after vip-rmail-advice activate)
5380 "Switch buffer to emacs-state after finishing with editing a message."
5381 (vip-change-state-to-emacs))
5385 ;; ~/.vip is loaded if it exists
5386 (if (and (file-exists-p vip-custom-file-name)
5387 (not noninteractive))
5388 (load vip-custom-file-name))
5390 ;; VIP compatibility: merge whatever the user has in vip-mode-map into
5391 ;; Viper's basic map.
5392 (vip-add-keymap vip-mode-map vip-vi-global-user-map)
5395 ;; Applying Viper customization -- runs after (load .vip)
5397 ;; Save user settings or Viper defaults for vars controled by vip-expert-level
5398 (setq vip-saved-user-settings
5399 (list (cons 'vip-want-ctl-h-help vip-want-ctl-h-help)
5400 (cons 'vip-always vip-always)
5401 (cons 'vip-no-multiple-ESC vip-no-multiple-ESC)
5402 (cons 'vip-ex-style-motion vip-ex-style-motion)
5403 (cons 'vip-ex-style-editing-in-insert
5404 vip-ex-style-editing-in-insert)
5405 (cons 'vip-want-emacs-keys-in-vi vip-want-emacs-keys-in-vi)
5406 (cons 'vip-want-emacs-keys-in-insert vip-want-emacs-keys-in-insert)
5407 (cons 'vip-re-search vip-re-search)))
5410 (vip-set-minibuffer-style)
5411 (vip-set-minibuffer-faces)
5412 (vip-set-search-face)
5414 ;;; Familiarize Viper with some minor modes that have their own keymaps
5415 (vip-harness-minor-mode "compile")
5416 (vip-harness-minor-mode "outline")
5417 (vip-harness-minor-mode "allout")
5418 (vip-harness-minor-mode "xref")
5419 (vip-harness-minor-mode "lmenu")
5420 (vip-harness-minor-mode "vc")
5421 (vip-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX
5422 (vip-harness-minor-mode "latex") ; latex they moved math mode here
5425 ;; Intercept maps could go in viper-keym.el
5426 ;; We keep them here in case someone redefines them in ~/.vip
5428 (define-key vip-vi-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5429 (define-key vip-insert-intercept-map vip-ESC-key 'vip-intercept-ESC-key)
5431 ;; This is taken care of by vip-insert-global-user-map.
5432 ;;(define-key vip-replace-map vip-ESC-key 'vip-intercept-ESC-key)
5434 (define-key vip-insert-intercept-map vip-toggle-key 'vip-alternate-ESC)
5435 ;; The default vip-toggle-key is \C-z; for the novice, it suspends or
5437 (define-key vip-vi-intercept-map vip-toggle-key
5438 '(lambda () (interactive)
5439 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
5440 (if window-system (vip-iconify) (suspend-emacs))
5441 (vip-change-state-to-emacs))))
5443 (define-key vip-emacs-intercept-map vip-toggle-key 'vip-change-state-to-vi)
5447 (and (< vip-expert-level 5) (> vip-expert-level 0)))
5450 ;; Let all minor modes take effect after loading
5451 ;; this may not be enough, so we also set default minor-mode-alist.
5452 ;; Without setting the default, new buffers that come up in emacs mode have
5453 ;; minor-mode-map-alist = nil, unless we call vip-change-state-*
5454 (if (eq vip-current-state 'emacs-state)
5456 (vip-change-state-to-emacs)
5457 (setq-default minor-mode-map-alist minor-mode-map-alist)
5460 ;; set some useful macros
5462 ;; repeat the 2nd previous command without rotating the command history
5463 (vip-record-kbd-macro
5464 (vector vip-repeat-from-history-key '\1) 'vi-state
5465 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5466 ;; repeat the 3d previous command without rotating the command history
5467 (vip-record-kbd-macro
5468 (vector vip-repeat-from-history-key '\2) 'vi-state
5469 [(meta x) v i p - r e p e a t - f r o m - h i s t o r y return] 't)
5471 ;; toggle case sensitivity in search
5472 (vip-record-kbd-macro
5474 [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)
5475 ;; toggle regexp/vanila search
5476 (vip-record-kbd-macro
5478 [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)
5481 (run-hooks 'vip-load-hooks) ; the last chance to change anything
5487 ;;; viper.el ends here