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