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