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