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