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