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