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