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