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