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