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