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