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