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