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