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