new version
[emacs.git] / lisp / emulation / viper-cmd.el
blob2e25a44a7e5c943d3637d5752e6f9f34b762985c
1 ;;; viper-cmd.el --- Vi command support for Viper
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
5 ;; Code
7 (provide 'viper-cmd)
8 (require 'advice)
10 ;; Compiler pacifier
11 (defvar vip-minibuffer-current-face)
12 (defvar vip-minibuffer-insert-face)
13 (defvar vip-minibuffer-vi-face)
14 (defvar vip-minibuffer-emacs-face)
15 (defvar vip-mode-string )
16 (defvar iso-accents-mode)
17 (defvar zmacs-region-stays)
18 (defvar mark-even-if-inactive)
20 ;; loading happens only in non-interactive compilation
21 ;; in order to spare non-viperized emacs from being viperized
22 (if noninteractive
23 (eval-when-compile
24 (let ((load-path (cons (expand-file-name ".") load-path)))
25 (or (featurep 'viper-util)
26 (load "viper-util.el" nil nil 'nosuffix))
27 (or (featurep 'viper-keym)
28 (load "viper-keym.el" nil nil 'nosuffix))
29 (or (featurep 'viper-mous)
30 (load "viper-mous.el" nil nil 'nosuffix))
31 (or (featurep 'viper-macs)
32 (load "viper-macs.el" nil nil 'nosuffix))
33 (or (featurep 'viper-ex)
34 (load "viper-ex.el" nil nil 'nosuffix))
35 )))
36 ;; end pacifier
39 (require 'viper-util)
40 (require 'viper-keym)
41 (require 'viper-mous)
42 (require 'viper-macs)
43 (require 'viper-ex)
47 ;; Generic predicates
49 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
51 ;; generate test functions
52 ;; given symbol foo, foo-p is the test function, foos is the set of
53 ;; Viper command keys
54 ;; (macroexpand '(vip-test-com-defun foo))
55 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
57 (defmacro vip-test-com-defun (name)
58 (let* ((snm (symbol-name name))
59 (nm-p (intern (concat snm "-p")))
60 (nms (intern (concat snm "s"))))
61 (` (defun (, nm-p) (com)
62 (consp (memq (if (< com 0) (- com) com) (, nms)))))))
64 ;; Variables for defining VI commands
66 ;; Modifying commands that can be prefixes to movement commands
67 (defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
68 ;; define vip-prefix-command-p
69 (vip-test-com-defun vip-prefix-command)
71 ;; Commands that are pairs eg. dd. r and R here are a hack
72 (defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
73 ;; define vip-charpair-command-p
74 (vip-test-com-defun vip-charpair-command)
76 (defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
77 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
78 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
79 ?; ?, ?0 ?? ?/
81 "Movement commands")
82 ;; define vip-movement-command-p
83 (vip-test-com-defun vip-movement-command)
85 (defconst vip-digit-commands '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)
86 "Digit commands")
87 ;; define vip-digit-command-p
88 (vip-test-com-defun vip-digit-command)
90 ;; Commands that can be repeated by . (dotted)
91 (defconst vip-dotable-commands '(?c ?d ?C ?s ?S ?D ?> ?<))
92 ;; define vip-dotable-command-p
93 (vip-test-com-defun vip-dotable-command)
95 ;; Commands that can follow a #
96 (defconst vip-hash-commands '(?c ?C ?g ?q ?s))
97 ;; define vip-hash-command-p
98 (vip-test-com-defun vip-hash-command)
100 ;; Commands that may have registers as prefix
101 (defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
102 ;; define vip-regsuffix-command-p
103 (vip-test-com-defun vip-regsuffix-command)
105 (defconst vip-vi-commands (append vip-movement-commands
106 vip-digit-commands
107 vip-dotable-commands
108 vip-charpair-commands
109 vip-hash-commands
110 vip-prefix-commands
111 vip-regsuffix-commands)
112 "The list of all commands in Vi-state.")
113 ;; define vip-vi-command-p
114 (vip-test-com-defun vip-vi-command)
117 ;;; CODE
119 ;; sentinels
121 ;; Runs vip-after-change-functions inside after-change-functions
122 (defun vip-after-change-sentinel (beg end len)
123 (let ((list vip-after-change-functions))
124 (while list
125 (funcall (car list) beg end len)
126 (setq list (cdr list)))))
128 ;; Runs vip-before-change-functions inside before-change-functions
129 (defun vip-before-change-sentinel (beg end)
130 (let ((list vip-before-change-functions))
131 (while list
132 (funcall (car list) beg end)
133 (setq list (cdr list)))))
135 (defsubst vip-post-command-sentinel ()
136 (run-hooks 'vip-post-command-hooks))
138 (defsubst vip-pre-command-sentinel ()
139 (run-hooks 'vip-pre-command-hooks))
141 ;; Needed so that Viper will be able to figure the last inserted
142 ;; chunk of text with reasonable accuracy.
143 (defsubst vip-insert-state-post-command-sentinel ()
144 (if (and (memq vip-current-state '(insert-state replace-state))
145 vip-insert-point
146 (>= (point) vip-insert-point))
147 (setq vip-last-posn-while-in-insert-state (point-marker)))
148 (if (eq vip-current-state 'insert-state)
149 (progn
150 (or (stringp vip-saved-cursor-color)
151 (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
152 (setq vip-saved-cursor-color (vip-get-cursor-color)))
153 (if (stringp vip-saved-cursor-color)
154 (vip-change-cursor-color vip-insert-state-cursor-color))
156 (if (and (eq this-command 'dabbrev-expand)
157 (integerp vip-pre-command-point)
158 (> vip-insert-point vip-pre-command-point))
159 (move-marker vip-insert-point vip-pre-command-point))
162 (defsubst vip-insert-state-pre-command-sentinel ()
163 (or (memq this-command '(self-insert-command))
164 (memq (vip-event-key last-command-event)
165 '(up down left right (meta f) (meta b)
166 (control n) (control p) (control f) (control b)))
167 (vip-restore-cursor-color-after-insert))
168 (if (and (eq this-command 'dabbrev-expand)
169 (markerp vip-insert-point)
170 (marker-position vip-insert-point))
171 (setq vip-pre-command-point (marker-position vip-insert-point))))
173 (defsubst vip-R-state-post-command-sentinel ()
174 ;; Restoring cursor color is needed despite
175 ;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
176 ;; another frame, the pre-command hook won't change cursor color to default
177 ;; in that other frame. So, if the second frame cursor was red and we set
178 ;; the point outside the replacement region, then the cursor color will
179 ;; remain red. Restoring the default, below, prevents this.
180 (if (and (<= (vip-replace-start) (point))
181 (<= (point) (vip-replace-end)))
182 (vip-change-cursor-color vip-replace-overlay-cursor-color)
183 (vip-restore-cursor-color-after-replace)
186 ;; to speed up, don't change cursor color before self-insert
187 ;; and common move commands
188 (defsubst vip-replace-state-pre-command-sentinel ()
189 (or (memq this-command '(self-insert-command))
190 (memq (vip-event-key last-command-event)
191 '(up down left right (meta f) (meta b)
192 (control n) (control p) (control f) (control b)))
193 (vip-restore-cursor-color-after-replace)))
195 (defun vip-replace-state-post-command-sentinel ()
196 ;; Restoring cursor color is needed despite
197 ;; vip-replace-state-pre-command-sentinel: When one jumps to another buffer
198 ;; in another frame, the pre-command hook won't change cursor color to
199 ;; default in that other frame. So, if the second frame cursor was red and
200 ;; we set the point outside the replacement region, then the cursor color
201 ;; will remain red. Restoring the default, below, fixes this problem.
203 ;; We optimize for self-insert-command's here, since they either don't change
204 ;; cursor color or, if they terminate replace mode, the color will be changed
205 ;; in vip-finish-change
206 (or (memq this-command '(self-insert-command))
207 (vip-restore-cursor-color-after-replace))
208 (cond
209 ((eq vip-current-state 'replace-state)
210 ;; delete characters to compensate for inserted chars.
211 (let ((replace-boundary (vip-replace-end)))
212 (save-excursion
213 (goto-char vip-last-posn-in-replace-region)
214 (delete-char vip-replace-chars-to-delete)
215 (setq vip-replace-chars-to-delete 0
216 vip-replace-chars-deleted 0)
217 ;; terminate replace mode if reached replace limit
218 (if (= vip-last-posn-in-replace-region
219 (vip-replace-end))
220 (vip-finish-change vip-last-posn-in-replace-region)))
222 (if (and (<= (vip-replace-start) (point))
223 (<= (point) replace-boundary))
224 (progn
225 ;; the state may have changed in vip-finish-change above
226 (if (eq vip-current-state 'replace-state)
227 (vip-change-cursor-color vip-replace-overlay-cursor-color))
228 (setq vip-last-posn-in-replace-region (point-marker))))
231 (t ;; terminate replace mode if changed Viper states.
232 (vip-finish-change vip-last-posn-in-replace-region))))
235 ;; changing mode
237 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
238 (defun vip-change-state (new-state)
239 ;; Keep vip-post/pre-command-hooks fresh.
240 ;; We remove then add vip-post/pre-command-sentinel since it is very
241 ;; desirable that vip-pre-command-sentinel is the last hook and
242 ;; vip-post-command-sentinel is the first hook.
243 (remove-hook 'post-command-hook 'vip-post-command-sentinel)
244 (add-hook 'post-command-hook 'vip-post-command-sentinel)
245 (remove-hook 'pre-command-hook 'vip-pre-command-sentinel)
246 (add-hook 'pre-command-hook 'vip-pre-command-sentinel t)
247 ;; These hooks will be added back if switching to insert/replace mode
248 (vip-remove-hook 'vip-post-command-hooks
249 'vip-insert-state-post-command-sentinel)
250 (vip-remove-hook 'vip-pre-command-hooks
251 'vip-insert-state-pre-command-sentinel)
252 (cond ((eq new-state 'vi-state)
253 (cond ((member vip-current-state '(insert-state replace-state))
255 ;; move vip-last-posn-while-in-insert-state
256 ;; This is a normal hook that is executed in insert/replace
257 ;; states after each command. In Vi/Emacs state, it does
258 ;; nothing. We need to execute it here to make sure that
259 ;; the last posn was recorded when we hit ESC.
260 ;; It may be left unrecorded if the last thing done in
261 ;; insert/repl state was dabbrev-expansion or abbrev
262 ;; expansion caused by hitting ESC
263 (vip-insert-state-post-command-sentinel)
265 (condition-case conds
266 (progn
267 (vip-save-last-insertion
268 vip-insert-point
269 vip-last-posn-while-in-insert-state)
270 (if vip-began-as-replace
271 (setq vip-began-as-replace nil)
272 ;; repeat insert commands if numerical arg > 1
273 (save-excursion
274 (vip-repeat-insert-command))))
275 (error
276 (vip-message-conditions conds)))
278 (if (> (length vip-last-insertion) 0)
279 (vip-push-onto-ring vip-last-insertion
280 'vip-insertion-ring))
282 (if vip-ex-style-editing-in-insert
283 (or (bolp) (backward-char 1))))
286 ;; insert or replace
287 ((memq new-state '(insert-state replace-state))
288 (if (memq vip-current-state '(emacs-state vi-state))
289 (vip-move-marker-locally 'vip-insert-point (point)))
290 (vip-move-marker-locally 'vip-last-posn-while-in-insert-state (point))
291 (vip-add-hook 'vip-post-command-hooks
292 'vip-insert-state-post-command-sentinel t)
293 (vip-add-hook 'vip-pre-command-hooks
294 'vip-insert-state-pre-command-sentinel t))
295 ) ; outermost cond
297 ;; Nothing needs to be done to switch to emacs mode! Just set some
298 ;; variables, which is already done in vip-change-state-to-emacs!
300 (setq vip-current-state new-state)
301 (vip-normalize-minor-mode-map-alist)
302 (vip-adjust-keys-for new-state)
303 (vip-set-mode-vars-for new-state)
304 (vip-refresh-mode-line)
309 (defun vip-adjust-keys-for (state)
310 "Make necessary adjustments to keymaps before entering STATE."
311 (cond ((memq state '(insert-state replace-state))
312 (if vip-auto-indent
313 (progn
314 (define-key vip-insert-basic-map "\C-m" 'vip-autoindent)
315 (if vip-want-emacs-keys-in-insert
316 ;; expert
317 (define-key vip-insert-basic-map "\C-j" nil)
318 ;; novice
319 (define-key vip-insert-basic-map "\C-j" 'vip-autoindent)))
320 (define-key vip-insert-basic-map "\C-m" nil)
321 (define-key vip-insert-basic-map "\C-j" nil))
323 (setq vip-insert-diehard-minor-mode
324 (not vip-want-emacs-keys-in-insert))
326 (if vip-want-ctl-h-help
327 (progn
328 (define-key vip-insert-basic-map [(control h)] 'help-command)
329 (define-key vip-replace-map [(control h)] 'help-command))
330 (define-key vip-insert-basic-map
331 [(control h)] 'vip-del-backward-char-in-insert)
332 (define-key vip-replace-map
333 [(control h)] 'vip-del-backward-char-in-replace)))
335 (t ; Vi state
336 (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi))
337 (if vip-want-ctl-h-help
338 (define-key vip-vi-basic-map [(control h)] 'help-command)
339 (define-key vip-vi-basic-map [(control h)] 'vip-backward-char)))
343 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
344 ;; This ensures that Viper bindings are in effect, regardless of which minor
345 ;; modes were turned on by the user or by other packages.
346 (defun vip-normalize-minor-mode-map-alist ()
347 (setq minor-mode-map-alist
348 (vip-append-filter-alist
349 (list
350 (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map)
351 (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map)
352 (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map)
353 (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map)
354 (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map)
355 (cons 'vip-vi-state-modifier-minor-mode
356 (if (keymapp
357 (cdr (assoc major-mode vip-vi-state-modifier-alist)))
358 (cdr (assoc major-mode vip-vi-state-modifier-alist))
359 vip-empty-keymap))
360 (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map)
361 (cons 'vip-vi-basic-minor-mode vip-vi-basic-map)
362 (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map)
363 (cons 'vip-replace-minor-mode vip-replace-map)
364 ;; vip-insert-minibuffer-minor-mode must come after
365 ;; vip-replace-minor-mode
366 (cons 'vip-insert-minibuffer-minor-mode
367 vip-minibuffer-map)
368 (cons 'vip-insert-local-user-minor-mode
369 vip-insert-local-user-map)
370 (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map)
371 (cons 'vip-insert-global-user-minor-mode
372 vip-insert-global-user-map)
373 (cons 'vip-insert-state-modifier-minor-mode
374 (if (keymapp
375 (cdr
376 (assoc major-mode vip-insert-state-modifier-alist)))
377 (cdr
378 (assoc major-mode vip-insert-state-modifier-alist))
379 vip-empty-keymap))
380 (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map)
381 (cons 'vip-insert-basic-minor-mode vip-insert-basic-map)
382 (cons 'vip-emacs-intercept-minor-mode
383 vip-emacs-intercept-map)
384 (cons 'vip-emacs-local-user-minor-mode
385 vip-emacs-local-user-map)
386 (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map)
387 (cons 'vip-emacs-global-user-minor-mode
388 vip-emacs-global-user-map)
389 (cons 'vip-emacs-state-modifier-minor-mode
390 (if (keymapp
391 (cdr
392 (assoc major-mode vip-emacs-state-modifier-alist)))
393 (cdr
394 (assoc major-mode vip-emacs-state-modifier-alist))
395 vip-empty-keymap))
397 minor-mode-map-alist)))
403 ;; Viper mode-changing commands and utilities
405 ;; Modifies mode-line-buffer-identification.
406 (defun vip-refresh-mode-line ()
407 (setq vip-mode-string
408 (cond ((eq vip-current-state 'emacs-state) vip-emacs-state-id)
409 ((eq vip-current-state 'vi-state) vip-vi-state-id)
410 ((eq vip-current-state 'replace-state) vip-replace-state-id)
411 ((eq vip-current-state 'insert-state) vip-insert-state-id)))
413 ;; Sets Viper mode string in global-mode-string
414 (force-mode-line-update))
416 ;;;###autoload
417 (defun viper-mode ()
418 "Turn on Viper emulation of Vi."
419 (interactive)
420 (if (not noninteractive)
421 (progn
422 (if vip-first-time ; This check is important. Without it, startup and
423 (progn ; expert-level msgs mix up when viper-mode recurses
424 (setq vip-first-time nil)
425 (if (not vip-inhibit-startup-message)
426 (save-window-excursion
427 (setq vip-inhibit-startup-message t)
428 (delete-other-windows)
429 (switch-to-buffer "Viper Startup Message")
430 (erase-buffer)
431 (insert
432 (substitute-command-keys
433 "Viper Is a Package for Emacs Rebels.
434 It is also a VI Plan for Emacs Rescue and a venomous VI PERil.
436 Technically speaking, Viper is a Vi emulation package for GNU Emacs 19 and
437 XEmacs 19. It supports virtually all of Vi and Ex functionality, extending
438 and improving upon much of it.
440 1. Viper supports Vi at several levels. Level 1 is the closest to Vi,
441 level 5 provides the most flexibility to depart from many Vi conventions.
443 You will be asked to specify your user level in a following screen.
445 If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will behave
446 as in VI, to smooth transition to Viper for the beginners. However, to
447 use Emacs productively, you are advised to reach user level 3 or higher.
449 If your user level is 2 or higher, ^X and ^C will invoke Emacs
450 functions,as usual in Emacs; ^Z will toggle vi/emacs modes, and
451 ^G will be the usual Emacs's keyboard-quit (something like ^C in VI).
453 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
454 do not cause Emacs to quit, except at user level 1 (a novice).
455 3. ^X^C EXITS EMACS.
456 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
457 undo. Another `u' changes direction.
459 6. Emacs Meta functions are invoked by typing `C-\\' or `\\ ESC'.
460 On a window system, the best way is to use the Meta-key.
461 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if
462 something funny happens. This would abort the current editing command.
464 You can get more information on Viper by:
466 a. Typing `:help' in Vi state
467 b. Printing Viper manual, found in ./etc/viper.dvi
468 c. Printing ViperCard, the Quick Reference, found in ./etc/viperCard.dvi
470 This startup message appears whenever you load Viper, unless you type `y' now."
472 (goto-char (point-min))
473 (if (y-or-n-p "Inhibit Viper startup message? ")
474 (vip-save-setting
475 'vip-inhibit-startup-message
476 "Viper startup message inhibited"
477 vip-custom-file-name t))
478 ;;(kill-buffer (current-buffer))
479 (message
480 "The last message is in buffer `Viper Startup Message'")
481 (sit-for 4)
483 (vip-set-expert-level 'dont-change-unless)))
484 (vip-change-state-to-vi))))
486 ;;;###autoload
487 (defalias 'vip-mode 'viper-mode)
490 ;; Switch from Insert state to Vi state.
491 (defun vip-exit-insert-state ()
492 (interactive)
493 (vip-change-state-to-vi))
495 (defun vip-set-mode-vars-for (state)
496 "Sets Viper minor mode variables to put Viper's state STATE in effect."
498 ;; Emacs state
499 (setq vip-vi-minibuffer-minor-mode nil
500 vip-insert-minibuffer-minor-mode nil
501 vip-vi-intercept-minor-mode nil
502 vip-insert-intercept-minor-mode nil
504 vip-vi-local-user-minor-mode nil
505 vip-vi-kbd-minor-mode nil
506 vip-vi-global-user-minor-mode nil
507 vip-vi-state-modifier-minor-mode nil
508 vip-vi-diehard-minor-mode nil
509 vip-vi-basic-minor-mode nil
511 vip-replace-minor-mode nil
513 vip-insert-local-user-minor-mode nil
514 vip-insert-kbd-minor-mode nil
515 vip-insert-global-user-minor-mode nil
516 vip-insert-state-modifier-minor-mode nil
517 vip-insert-diehard-minor-mode nil
518 vip-insert-basic-minor-mode nil
519 vip-emacs-intercept-minor-mode t
520 vip-emacs-local-user-minor-mode t
521 vip-emacs-kbd-minor-mode (not (vip-is-in-minibuffer))
522 vip-emacs-global-user-minor-mode t
523 vip-emacs-state-modifier-minor-mode t
526 ;; Vi state
527 (if (eq state 'vi-state) ; adjust for vi-state
528 (setq
529 vip-vi-intercept-minor-mode t
530 vip-vi-minibuffer-minor-mode (vip-is-in-minibuffer)
531 vip-vi-local-user-minor-mode t
532 vip-vi-kbd-minor-mode (not (vip-is-in-minibuffer))
533 vip-vi-global-user-minor-mode t
534 vip-vi-state-modifier-minor-mode t
535 ;; don't let the diehard keymap block command completion
536 ;; and other things in the minibuffer
537 vip-vi-diehard-minor-mode (not
538 (or vip-want-emacs-keys-in-vi
539 (vip-is-in-minibuffer)))
540 vip-vi-basic-minor-mode t
541 vip-emacs-intercept-minor-mode nil
542 vip-emacs-local-user-minor-mode nil
543 vip-emacs-kbd-minor-mode nil
544 vip-emacs-global-user-minor-mode nil
545 vip-emacs-state-modifier-minor-mode nil
548 ;; Insert and Replace states
549 (if (member state '(insert-state replace-state))
550 (setq
551 vip-insert-intercept-minor-mode t
552 vip-replace-minor-mode (eq state 'replace-state)
553 vip-insert-minibuffer-minor-mode (vip-is-in-minibuffer)
554 vip-insert-local-user-minor-mode t
555 vip-insert-kbd-minor-mode (not (vip-is-in-minibuffer))
556 vip-insert-global-user-minor-mode t
557 vip-insert-state-modifier-minor-mode t
558 ;; don't let the diehard keymap block command completion
559 ;; and other things in the minibuffer
560 vip-insert-diehard-minor-mode (not
561 (or vip-want-emacs-keys-in-insert
562 (vip-is-in-minibuffer)))
563 vip-insert-basic-minor-mode t
564 vip-emacs-intercept-minor-mode nil
565 vip-emacs-local-user-minor-mode nil
566 vip-emacs-kbd-minor-mode nil
567 vip-emacs-global-user-minor-mode nil
568 vip-emacs-state-modifier-minor-mode nil
571 ;; minibuffer faces
572 (if (vip-has-face-support-p)
573 (setq vip-minibuffer-current-face
574 (cond ((eq state 'emacs-state) vip-minibuffer-emacs-face)
575 ((eq state 'vi-state) vip-minibuffer-vi-face)
576 ((memq state '(insert-state replace-state))
577 vip-minibuffer-insert-face))))
579 (if (vip-is-in-minibuffer)
580 (vip-set-minibuffer-overlay))
583 ;; This also takes care of the annoying incomplete lines in files.
584 ;; Also, this fixes `undo' to work vi-style for complex commands.
585 (defun vip-change-state-to-vi ()
586 "Change Viper state to Vi."
587 (interactive)
588 (if (and vip-first-time (not (vip-is-in-minibuffer)))
589 (viper-mode)
590 (if overwrite-mode (overwrite-mode nil))
591 (if abbrev-mode (expand-abbrev))
592 (if (and auto-fill-function (> (current-column) fill-column))
593 (funcall auto-fill-function))
594 ;; don't leave whitespace lines around
595 (if (and (memq last-command
596 '(vip-autoindent
597 vip-open-line vip-Open-line
598 vip-replace-state-exit-cmd))
599 (vip-over-whitespace-line))
600 (indent-to-left-margin))
601 (vip-add-newline-at-eob-if-necessary)
602 (if vip-undo-needs-adjustment (vip-adjust-undo))
603 (vip-change-state 'vi-state)
605 ;; always turn off iso-accents-mode, or else we won't be able to use the
606 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
607 (if (and (boundp 'iso-accents-mode) iso-accents-mode)
608 (iso-accents-mode -1))
610 (vip-restore-cursor-color-after-insert)
612 ;; Protection against user errors in hooks
613 (condition-case conds
614 (run-hooks 'vip-vi-state-hook)
615 (error
616 (vip-message-conditions conds)))))
618 (defun vip-change-state-to-insert ()
619 "Change Viper state to Insert."
620 (interactive)
621 (vip-change-state 'insert-state)
622 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
623 (iso-accents-mode 1)) ; turn iso accents on
625 (or (stringp vip-saved-cursor-color)
626 (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
627 (setq vip-saved-cursor-color (vip-get-cursor-color)))
628 ;; Commented out, because if vip-change-state-to-insert is executed
629 ;; non-interactively then the old cursor color may get lost. Same old Emacs
630 ;; bug related to local variables?
631 ;;;(if (stringp vip-saved-cursor-color)
632 ;;; (vip-change-cursor-color vip-insert-state-cursor-color))
633 ;; Protection against user errors in hooks
634 (condition-case conds
635 (run-hooks 'vip-insert-state-hook)
636 (error
637 (vip-message-conditions conds))))
639 (defsubst vip-downgrade-to-insert ()
640 (setq vip-current-state 'insert-state
641 vip-replace-minor-mode nil)
646 ;; Change to replace state. When the end of replacement region is reached,
647 ;; replace state changes to insert state.
648 (defun vip-change-state-to-replace (&optional non-R-cmd)
649 (vip-change-state 'replace-state)
650 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
651 (iso-accents-mode 1)) ; turn iso accents on
652 ;; Run insert-state-hook
653 (condition-case conds
654 (run-hooks 'vip-insert-state-hook 'vip-replace-state-hook)
655 (error
656 (vip-message-conditions conds)))
658 (if non-R-cmd
659 (vip-start-replace)
660 ;; 'R' is implemented using Emacs's overwrite-mode
661 (vip-start-R-mode))
665 (defun vip-change-state-to-emacs ()
666 "Change Viper state to Emacs."
667 (interactive)
668 (vip-change-state 'emacs-state)
669 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
670 (iso-accents-mode 1)) ; turn iso accents on
672 ;; Protection agains user errors in hooks
673 (condition-case conds
674 (run-hooks 'vip-emacs-state-hook)
675 (error
676 (vip-message-conditions conds))))
678 ;; escape to emacs mode termporarily
679 (defun vip-escape-to-emacs (arg &optional events)
680 "Escape to Emacs state from Vi state for one Emacs command.
681 ARG is used as the prefix value for the executed command. If
682 EVENTS is a list of events, which become the beginning of the command."
683 (interactive "P")
684 (if (= last-command-char ?\\)
685 (message "Switched to EMACS state for the next command..."))
686 (vip-escape-to-state arg events 'emacs-state))
688 ;; escape to Vi mode termporarily
689 (defun vip-escape-to-vi (arg)
690 "Escape from Emacs state to Vi state for one Vi 1-character command.
691 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
692 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
693 `vip-escape-to-vi' is used."
694 (interactive "P")
695 (message "Switched to VI state for the next command...")
696 (vip-escape-to-state arg nil 'vi-state))
698 ;; Escape to STATE mode for one Emacs command.
699 (defun vip-escape-to-state (arg events state)
700 ;;(let (com key prefix-arg)
701 (let (com key)
702 ;; this temporarily turns off Viper's minor mode keymaps
703 (vip-set-mode-vars-for state)
704 (vip-normalize-minor-mode-map-alist)
705 (if events (vip-set-unread-command-events events))
707 ;; protect against keyboard quit and other errors
708 (condition-case nil
709 (let (vip-vi-kbd-minor-mode
710 vip-insert-kbd-minor-mode
711 vip-emacs-kbd-minor-mode)
712 (unwind-protect
713 (progn
714 (setq com (key-binding (setq key
715 (if vip-xemacs-p
716 (read-key-sequence nil)
717 (read-key-sequence nil t)))))
718 ;; In case of binding indirection--chase definitions.
719 ;; Have to do it here because we execute this command under
720 ;; different keymaps, so command-execute may not do the
721 ;; right thing there
722 (while (vectorp com) (setq com (key-binding com))))
723 nil)
724 ;; Execute command com in the original Viper state, not in state
725 ;; `state'. Otherwise, if we switch buffers while executing the
726 ;; escaped to command, Viper's mode vars will remain those of
727 ;; `state'. When we return to the orig buffer, the bindings will be
728 ;; screwed up.
729 (vip-set-mode-vars-for vip-current-state)
731 ;; this-command, last-command-char, last-command-event
732 (setq this-command com)
733 (if vip-xemacs-p ; XEmacs represents key sequences as vectors
734 (setq last-command-event (vip-copy-event (vip-seq-last-elt key))
735 last-command-char (event-to-character last-command-event))
736 ;; Emacs represents them as sequences (str or vec)
737 (setq last-command-event (vip-copy-event (vip-seq-last-elt key))
738 last-command-char last-command-event))
740 (if (commandp com)
741 (progn
742 (setq prefix-arg (or prefix-arg arg))
743 (command-execute com)))
745 (quit (ding))
746 (error (beep 1))))
747 ;; set state in the new buffer
748 (vip-set-mode-vars-for vip-current-state))
750 (defun vip-exec-form-in-vi (form)
751 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
752 (let ((buff (current-buffer))
753 result)
754 (vip-set-mode-vars-for 'vi-state)
756 (condition-case nil
757 (setq result (eval form))
758 (error
759 (signal 'quit nil)))
761 (if (not (equal buff (current-buffer))) ; cmd switched buffer
762 (save-excursion
763 (set-buffer buff)
764 (vip-set-mode-vars-for vip-current-state)))
765 (vip-set-mode-vars-for vip-current-state)
766 result))
768 (defun vip-exec-form-in-emacs (form)
769 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
770 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
771 (let ((buff (current-buffer))
772 result)
773 (vip-set-mode-vars-for 'emacs-state)
774 (setq result (eval form))
775 (if (not (equal buff (current-buffer))) ; cmd switched buffer
776 (save-excursion
777 (set-buffer buff)
778 (vip-set-mode-vars-for vip-current-state)))
779 (vip-set-mode-vars-for vip-current-state)
780 result))
783 ;; This is needed because minor modes sometimes override essential Viper
784 ;; bindings. By letting Viper know which files these modes are in, it will
785 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
786 (defun vip-harness-minor-mode (load-file)
787 "Familiarize Viper with a minor mode defined in LOAD_FILE.
788 Minor modes that have their own keymaps may overshadow Viper keymaps.
789 This function is designed to make Viper aware of the packages that define
790 such minor modes.
791 Usage:
792 (vip-harness-minor-mode load-file)
794 LOAD-FILE is a name of the file where the specific minor mode is defined.
795 Suffixes such as .el or .elc should be stripped."
797 (interactive "sEnter name of the load file: ")
799 (vip-eval-after-load load-file '(vip-normalize-minor-mode-map-alist))
801 ;; Change the default for minor-mode-map-alist each time a harnessed minor
802 ;; mode adds its own keymap to the a-list.
803 (vip-eval-after-load
804 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist))
808 (defun vip-ESC (arg)
809 "Emulate ESC key in Emacs.
810 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true.
811 If vip-no-multiple-ESC is 'twice double ESC would ding in vi-state.
812 Other ESC sequences are emulated via the current Emacs's major mode
813 keymap. This is more convenient on TTYs, since this won't block
814 function keys such as up,down, etc. ESC will also will also work as
815 a Meta key in this case. When vip-no-multiple-ESC is nil, ESC functions
816 as a Meta key and any number of multiple escapes is allowed."
817 (interactive "P")
818 (let (char)
819 (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state))
820 (setq char (vip-read-char-exclusive))
821 (vip-escape-to-emacs arg (list ?\e char) ))
822 ((and (eq vip-no-multiple-ESC 'twice)
823 (eq vip-current-state 'vi-state))
824 (setq char (vip-read-char-exclusive))
825 (if (= char (string-to-char vip-ESC-key))
826 (ding)
827 (vip-escape-to-emacs arg (list ?\e char) )))
828 (t (ding)))
831 (defun vip-alternate-Meta-key (arg)
832 "Simulate Emacs Meta key."
833 (interactive "P")
834 (sit-for 1) (message "ESC-")
835 (vip-escape-to-emacs arg '(?\e)))
837 (defun vip-toggle-key-action ()
838 "Action bound to `vip-toggle-key'."
839 (interactive)
840 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
841 (if (vip-window-display-p)
842 (vip-iconify)
843 (suspend-emacs))
844 (vip-change-state-to-emacs)))
847 ;; Intercept ESC sequences on dumb terminals.
848 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
850 ;; Check if last key was ESC and if so try to reread it as a function key.
851 ;; But only if there are characters to read during a very short time.
852 ;; Returns the last event, if any.
853 (defun vip-envelop-ESC-key ()
854 (let ((event last-input-event)
855 (keyseq [nil])
856 inhibit-quit)
857 (if (vip-ESC-event-p event)
858 (progn
859 (if (vip-fast-keysequence-p)
860 (progn
861 (let (minor-mode-map-alist)
862 (vip-set-unread-command-events event)
863 (setq keyseq
864 (funcall
865 (ad-get-orig-definition 'read-key-sequence) nil))
866 ) ; let
867 ;; If keyseq translates into something that still has ESC
868 ;; at the beginning, separate ESC from the rest of the seq.
869 ;; In XEmacs we check for events that are keypress meta-key
870 ;; and convert them into [escape key]
872 ;; This is needed for the following reason:
873 ;; If ESC is the first symbol, we interpret it as if the
874 ;; user typed ESC and then quickly some other symbols.
875 ;; If ESC is not the first one, then the key sequence
876 ;; entered was apparently translated into a function key or
877 ;; something (e.g., one may have
878 ;; (define-key function-key-map "\e[192z" [f11])
879 ;; which would translate the escape-sequence generated by
880 ;; f11 in an xterm window into the symbolic key f11.
882 ;; If `first-key' is not an ESC event, we make it into the
883 ;; last-command-event in order to pretend that this key was
884 ;; pressed. This is needed to allow arrow keys to be bound to
885 ;; macros. Otherwise, vip-exec-mapped-kbd-macro will think that
886 ;; the last event was ESC and so it'll execute whatever is
887 ;; bound to ESC. (Viper macros can't be bound to
888 ;; ESC-sequences).
889 (let* ((first-key (elt keyseq 0))
890 (key-mod (event-modifiers first-key)))
891 (cond ((vip-ESC-event-p first-key)
892 ;; put keys following ESC on the unread list
893 ;; and return ESC as the key-sequence
894 (vip-set-unread-command-events (subseq keyseq 1))
895 (setq last-input-event event
896 keyseq (if vip-emacs-p
897 "\e"
898 (vector (character-to-event ?\e)))))
899 ((and vip-xemacs-p
900 (key-press-event-p first-key)
901 (equal '(meta) key-mod))
902 (vip-set-unread-command-events
903 (vconcat (vector
904 (character-to-event (event-key first-key)))
905 (subseq keyseq 1)))
906 (setq last-input-event event
907 keyseq (vector (character-to-event ?\e))))
908 ((eventp first-key)
909 (setq last-command-event (vip-copy-event first-key)))
911 ) ; end progn
913 ;; this is escape event with nothing after it
914 ;; put in unread-command-event and then re-read
915 (vip-set-unread-command-events event)
916 (setq keyseq
917 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
919 ;; not an escape event
920 (setq keyseq (vector event)))
921 keyseq))
925 ;; Listen to ESC key.
926 ;; If a sequence of keys starting with ESC is issued with very short delays,
927 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
928 (defun vip-intercept-ESC-key ()
929 "Function that implements ESC key in Viper emulation of Vi."
930 (interactive)
931 (let ((cmd (or (key-binding (vip-envelop-ESC-key))
932 '(lambda () (interactive) (error "")))))
934 ;; call the actual function to execute ESC (if no other symbols followed)
935 ;; or the key bound to the ESC sequence (if the sequence was issued
936 ;; with very short delay between characters.
937 (if (eq cmd 'vip-intercept-ESC-key)
938 (setq cmd
939 (cond ((eq vip-current-state 'vi-state)
940 'vip-ESC)
941 ((eq vip-current-state 'insert-state)
942 'vip-exit-insert-state)
943 ((eq vip-current-state 'replace-state)
944 'vip-replace-state-exit-cmd)
945 (t 'vip-change-state-to-vi)
947 (call-interactively cmd)))
952 ;; prefix argument for Vi mode
954 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
955 ;; represents the numeric value of the prefix argument and COM represents
956 ;; command prefix such as "c", "d", "m" and "y".
958 ;; Get value part of prefix-argument ARG.
959 (defsubst vip-p-val (arg)
960 (cond ((null arg) 1)
961 ((consp arg)
962 (if (or (null (car arg)) (equal (car arg) '(nil)))
963 1 (car arg)))
964 (t arg)))
966 ;; Get raw value part of prefix-argument ARG.
967 (defsubst vip-P-val (arg)
968 (cond ((consp arg) (car arg))
969 (t arg)))
971 ;; Get com part of prefix-argument ARG.
972 (defsubst vip-getcom (arg)
973 (cond ((null arg) nil)
974 ((consp arg) (cdr arg))
975 (t nil)))
977 ;; Get com part of prefix-argument ARG and modify it.
978 (defun vip-getCom (arg)
979 (let ((com (vip-getcom arg)))
980 (cond ((equal com ?c) ?C)
981 ((equal com ?d) ?D)
982 ((equal com ?y) ?Y)
983 (t com))))
986 ;; Compute numeric prefix arg value.
987 ;; Invoked by EVENT. COM is the command part obtained so far.
988 (defun vip-prefix-arg-value (event com)
989 (let (value func)
990 ;; read while number
991 (while (and (vip-characterp event) (>= event ?0) (<= event ?9))
992 (setq value (+ (* (if (integerp value) value 0) 10) (- event ?0)))
993 (setq event (vip-read-event-convert-to-char)))
995 (setq prefix-arg value)
996 (if com (setq prefix-arg (cons prefix-arg com)))
997 (while (eq event ?U)
998 (vip-describe-arg prefix-arg)
999 (setq event (vip-read-event-convert-to-char)))
1001 (if (or com (and (not (eq vip-current-state 'vi-state))
1002 ;; make sure it is a Vi command
1003 (vip-characterp event) (vip-vi-command-p event)
1005 ;; If appears to be one of the vi commands,
1006 ;; then execute it with funcall and clear prefix-arg in order to not
1007 ;; confuse subsequent commands
1008 (progn
1009 ;; last-command-char is the char we want emacs to think was typed
1010 ;; last. If com is not nil, the vip-digit-argument command was called
1011 ;; from within vip-prefix-arg command, such as `d', `w', etc., i.e.,
1012 ;; the user typed, say, d2. In this case, `com' would be `d', `w',
1013 ;; etc.
1014 ;; If vip-digit-argument was invoked by vip-escape-to-vi (which is
1015 ;; indicated by the fact that the current state is not vi-state),
1016 ;; then `event' represents the vi command to be executed (e.g., `d',
1017 ;; `w', etc). Again, last-command-char must make emacs believe that
1018 ;; this is the command we typed.
1019 (setq last-command-char (or com event))
1020 (setq func (vip-exec-form-in-vi
1021 (` (key-binding (char-to-string (, event))))))
1022 (funcall func prefix-arg)
1023 (setq prefix-arg nil))
1024 ;; some other command -- let emacs do it in its own way
1025 (vip-set-unread-command-events event))
1029 ;; Vi operator as prefix argument."
1030 (defun vip-prefix-arg-com (char value com)
1031 (let ((cont t)
1032 cmd-info mv-or-digit-cmd)
1033 (while (and cont
1034 (memq char
1035 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1036 vip-buffer-search-char)))
1037 (if com
1038 ;; this means that we already have a command character, so we
1039 ;; construct a com list and exit while. however, if char is "
1040 ;; it is an error.
1041 (progn
1042 ;; new com is (CHAR . OLDCOM)
1043 (if (memq char '(?# ?\")) (error ""))
1044 (setq com (cons char com))
1045 (setq cont nil))
1046 ;; If com is nil we set com as char, and read more. Again, if char
1047 ;; is ", we read the name of register and store it in vip-use-register.
1048 ;; if char is !, =, or #, a complete com is formed so we exit the
1049 ;; while loop.
1050 (cond ((memq char '(?! ?=))
1051 (setq com char)
1052 (setq char (read-char))
1053 (setq cont nil))
1054 ((= char ?#)
1055 ;; read a char and encode it as com
1056 (setq com (+ 128 (read-char)))
1057 (setq char (read-char)))
1058 ((= char ?\")
1059 (let ((reg (read-char)))
1060 (if (vip-valid-register reg)
1061 (setq vip-use-register reg)
1062 (error ""))
1063 (setq char (read-char))))
1065 (setq com char)
1066 (setq char (read-char))))))
1068 (if (atom com)
1069 ;; `com' is a single char, so we construct the command argument
1070 ;; and if `char' is `?', we describe the arg; otherwise
1071 ;; we prepare the command that will be executed at the end.
1072 (progn
1073 (setq cmd-info (cons value com))
1074 (while (= char ?U)
1075 (vip-describe-arg cmd-info)
1076 (setq char (read-char)))
1077 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
1078 ;; execute it at the very end
1079 (or (vip-movement-command-p char)
1080 (vip-digit-command-p char)
1081 (vip-regsuffix-command-p char)
1082 (error ""))
1083 (setq mv-or-digit-cmd
1084 (vip-exec-form-in-vi
1085 (` (key-binding (char-to-string (, char)))))))
1087 ;; as com is non-nil, this means that we have a command to execute
1088 (if (memq (car com) '(?r ?R))
1089 ;; execute apropriate region command.
1090 (let ((char (car com)) (com (cdr com)))
1091 (setq prefix-arg (cons value com))
1092 (if (= char ?r) (vip-region prefix-arg)
1093 (vip-Region prefix-arg))
1094 ;; reset prefix-arg
1095 (setq prefix-arg nil))
1096 ;; otherwise, reset prefix arg and call appropriate command
1097 (setq value (if (null value) 1 value))
1098 (setq prefix-arg nil)
1099 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1100 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1101 ((equal com '(?d . ?y)) (vip-yank-defun))
1102 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1103 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1104 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1105 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1106 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1107 (t (error "")))))
1109 (if mv-or-digit-cmd
1110 (progn
1111 (setq last-command-char char)
1112 (setq last-command-event
1113 (vip-copy-event
1114 (if vip-xemacs-p (character-to-event char) char)))
1115 (condition-case nil
1116 (funcall mv-or-digit-cmd cmd-info)
1117 (error
1118 (error "")))))
1121 (defun vip-describe-arg (arg)
1122 (let (val com)
1123 (setq val (vip-P-val arg)
1124 com (vip-getcom arg))
1125 (if (null val)
1126 (if (null com)
1127 (message "Value is nil, and command is nil")
1128 (message "Value is nil, and command is `%c'" com))
1129 (if (null com)
1130 (message "Value is `%d', and command is nil" val)
1131 (message "Value is `%d', and command is `%c'" val com)))))
1133 (defun vip-digit-argument (arg)
1134 "Begin numeric argument for the next command."
1135 (interactive "P")
1136 (vip-leave-region-active)
1137 (vip-prefix-arg-value
1138 last-command-char (if (consp arg) (cdr arg) nil)))
1140 (defun vip-command-argument (arg)
1141 "Accept a motion command as an argument."
1142 (interactive "P")
1143 (let ((vip-inside-command-argument-action t))
1144 (condition-case nil
1145 (vip-prefix-arg-com
1146 last-command-char
1147 (cond ((null arg) nil)
1148 ((consp arg) (car arg))
1149 ((integerp arg) arg)
1150 (t (error vip-InvalidCommandArgument)))
1151 (cond ((null arg) nil)
1152 ((consp arg) (cdr arg))
1153 ((integerp arg) nil)
1154 (t (error vip-InvalidCommandArgument))))
1155 (quit (setq vip-use-register nil)
1156 (signal 'quit nil)))
1157 (vip-deactivate-mark)))
1160 ;; repeat last destructive command
1162 ;; Append region to text in register REG.
1163 ;; START and END are buffer positions indicating what to append.
1164 (defsubst vip-append-to-register (reg start end)
1165 (set-register reg (concat (if (stringp (get-register reg))
1166 (get-register reg) "")
1167 (buffer-substring start end))))
1169 ;; Saves last inserted text for possible use by vip-repeat command.
1170 (defun vip-save-last-insertion (beg end)
1171 (setq vip-last-insertion (buffer-substring beg end))
1172 (or (< (length vip-d-com) 5)
1173 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
1174 (or (null vip-command-ring)
1175 (ring-empty-p vip-command-ring)
1176 (progn
1177 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
1178 vip-last-insertion)
1179 ;; del most recent elt, if identical to the second most-recent
1180 (vip-cleanup-ring vip-command-ring)))
1183 (defsubst vip-yank-last-insertion ()
1184 "Inserts the text saved by the previous vip-save-last-insertion command."
1185 (condition-case nil
1186 (insert vip-last-insertion)
1187 (error nil)))
1190 ;; define functions to be executed
1192 ;; invoked by the `C' command
1193 (defun vip-exec-change (m-com com)
1194 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1195 (set-marker vip-com-point (point) (current-buffer)))
1196 ;; handle C cmd at the eol and at eob.
1197 (if (or (and (eolp) (= vip-com-point (point)))
1198 (= vip-com-point (point-max)))
1199 (progn
1200 (insert " ")(backward-char 1)))
1201 (if (= vip-com-point (point))
1202 (vip-forward-char-carefully))
1203 (if (= com ?c)
1204 (vip-change vip-com-point (point))
1205 (vip-change-subr vip-com-point (point))))
1207 ;; this is invoked by vip-substitute-line
1208 (defun vip-exec-Change (m-com com)
1209 (save-excursion
1210 (set-mark vip-com-point)
1211 (vip-enlarge-region (mark t) (point))
1212 (if vip-use-register
1213 (progn
1214 (cond ((vip-valid-register vip-use-register '(letter digit))
1215 ;;(vip-valid-register vip-use-register '(letter)
1216 (copy-to-register
1217 vip-use-register (mark t) (point) nil))
1218 ((vip-valid-register vip-use-register '(Letter))
1219 (vip-append-to-register
1220 (downcase vip-use-register) (mark t) (point)))
1221 (t (setq vip-use-register nil)
1222 (error vip-InvalidRegister vip-use-register)))
1223 (setq vip-use-register nil)))
1224 (delete-region (mark t) (point)))
1225 (open-line 1)
1226 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
1228 (defun vip-exec-delete (m-com com)
1229 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1230 (set-marker vip-com-point (point) (current-buffer)))
1231 (if vip-use-register
1232 (progn
1233 (cond ((vip-valid-register vip-use-register '(letter digit))
1234 ;;(vip-valid-register vip-use-register '(letter))
1235 (copy-to-register
1236 vip-use-register vip-com-point (point) nil))
1237 ((vip-valid-register vip-use-register '(Letter))
1238 (vip-append-to-register
1239 (downcase vip-use-register) vip-com-point (point)))
1240 (t (setq vip-use-register nil)
1241 (error vip-InvalidRegister vip-use-register)))
1242 (setq vip-use-register nil)))
1243 (setq last-command
1244 (if (eq last-command 'd-command) 'kill-region nil))
1245 (kill-region vip-com-point (point))
1246 (setq this-command 'd-command)
1247 (if vip-ex-style-motion
1248 (if (and (eolp) (not (bolp))) (backward-char 1))))
1250 (defun vip-exec-Delete (m-com com)
1251 (save-excursion
1252 (set-mark vip-com-point)
1253 (vip-enlarge-region (mark t) (point))
1254 (if vip-use-register
1255 (progn
1256 (cond ((vip-valid-register vip-use-register '(letter digit))
1257 ;;(vip-valid-register vip-use-register '(letter))
1258 (copy-to-register
1259 vip-use-register (mark t) (point) nil))
1260 ((vip-valid-register vip-use-register '(Letter))
1261 (vip-append-to-register
1262 (downcase vip-use-register) (mark t) (point)))
1263 (t (setq vip-use-register nil)
1264 (error vip-InvalidRegister vip-use-register)))
1265 (setq vip-use-register nil)))
1266 (setq last-command
1267 (if (eq last-command 'D-command) 'kill-region nil))
1268 (kill-region (mark t) (point))
1269 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
1270 (back-to-indentation))
1272 (defun vip-exec-yank (m-com com)
1273 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1274 (set-marker vip-com-point (point) (current-buffer)))
1275 (if vip-use-register
1276 (progn
1277 (cond ((vip-valid-register vip-use-register '(letter digit))
1278 ;; (vip-valid-register vip-use-register '(letter))
1279 (copy-to-register
1280 vip-use-register vip-com-point (point) nil))
1281 ((vip-valid-register vip-use-register '(Letter))
1282 (vip-append-to-register
1283 (downcase vip-use-register) vip-com-point (point)))
1284 (t (setq vip-use-register nil)
1285 (error vip-InvalidRegister vip-use-register)))
1286 (setq vip-use-register nil)))
1287 (setq last-command nil)
1288 (copy-region-as-kill vip-com-point (point))
1289 (goto-char vip-com-point))
1291 (defun vip-exec-Yank (m-com com)
1292 (save-excursion
1293 (set-mark vip-com-point)
1294 (vip-enlarge-region (mark t) (point))
1295 (if vip-use-register
1296 (progn
1297 (cond ((vip-valid-register vip-use-register '(letter digit))
1298 (copy-to-register
1299 vip-use-register (mark t) (point) nil))
1300 ((vip-valid-register vip-use-register '(Letter))
1301 (vip-append-to-register
1302 (downcase vip-use-register) (mark t) (point)))
1303 (t (setq vip-use-register nil)
1304 (error vip-InvalidRegister vip-use-register)))
1305 (setq vip-use-register nil)))
1306 (setq last-command nil)
1307 (copy-region-as-kill (mark t) (point)))
1308 (vip-deactivate-mark)
1309 (goto-char vip-com-point))
1311 (defun vip-exec-bang (m-com com)
1312 (save-excursion
1313 (set-mark vip-com-point)
1314 (vip-enlarge-region (mark t) (point))
1315 (shell-command-on-region
1316 (mark t) (point)
1317 (if (= com ?!)
1318 (setq vip-last-shell-com
1319 (vip-read-string-with-history
1322 'vip-shell-history
1323 (car vip-shell-history)
1325 vip-last-shell-com)
1326 t)))
1328 (defun vip-exec-equals (m-com com)
1329 (save-excursion
1330 (set-mark vip-com-point)
1331 (vip-enlarge-region (mark t) (point))
1332 (if (> (mark t) (point)) (exchange-point-and-mark))
1333 (indent-region (mark t) (point) nil)))
1335 (defun vip-exec-shift (m-com com)
1336 (save-excursion
1337 (set-mark vip-com-point)
1338 (vip-enlarge-region (mark t) (point))
1339 (if (> (mark t) (point)) (exchange-point-and-mark))
1340 (indent-rigidly (mark t) (point)
1341 (if (= com ?>)
1342 vip-shift-width
1343 (- vip-shift-width))))
1344 ;; return point to where it was before shift
1345 (goto-char vip-com-point))
1347 ;; this is needed because some commands fake com by setting it to ?r, which
1348 ;; denotes repeated insert command.
1349 (defsubst vip-exec-dummy (m-com com)
1350 nil)
1352 (defun vip-exec-buffer-search (m-com com)
1353 (setq vip-s-string (buffer-substring (point) vip-com-point))
1354 (setq vip-s-forward t)
1355 (setq vip-search-history (cons vip-s-string vip-search-history))
1356 (vip-search vip-s-string vip-s-forward 1))
1358 (defvar vip-exec-array (make-vector 128 nil))
1360 ;; Using a dispatch array allows adding functions like buffer search
1361 ;; without affecting other functions. Buffer search can now be bound
1362 ;; to any character.
1364 (aset vip-exec-array ?c 'vip-exec-change)
1365 (aset vip-exec-array ?C 'vip-exec-Change)
1366 (aset vip-exec-array ?d 'vip-exec-delete)
1367 (aset vip-exec-array ?D 'vip-exec-Delete)
1368 (aset vip-exec-array ?y 'vip-exec-yank)
1369 (aset vip-exec-array ?Y 'vip-exec-Yank)
1370 (aset vip-exec-array ?r 'vip-exec-dummy)
1371 (aset vip-exec-array ?! 'vip-exec-bang)
1372 (aset vip-exec-array ?< 'vip-exec-shift)
1373 (aset vip-exec-array ?> 'vip-exec-shift)
1374 (aset vip-exec-array ?= 'vip-exec-equals)
1378 ;; This function is called by various movement commands to execute a
1379 ;; destructive command on the region specified by the movement command. For
1380 ;; instance, if the user types cw, then the command vip-forward-word will
1381 ;; call vip-execute-com to execute vip-exec-change, which eventually will
1382 ;; call vip-change to invoke the replace mode on the region.
1384 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
1385 ;; vip-d-com for later use by vip-repeat.
1386 (defun vip-execute-com (m-com val com)
1387 (let ((reg vip-use-register))
1388 ;; this is the special command `#'
1389 (if (> com 128)
1390 (vip-special-prefix-com (- com 128))
1391 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
1392 (if (null fn)
1393 (error "%c: %s" com vip-InvalidViCommand)
1394 (funcall fn m-com com))))
1395 (if (vip-dotable-command-p com)
1396 (vip-set-destructive-command
1397 (list m-com val
1398 (if (memq com (list ?c ?C ?!)) (- com) com)
1399 reg nil nil)))
1403 (defun vip-repeat (arg)
1404 "Re-execute last destructive command.
1405 Use the info in vip-d-com, which has the form
1406 \(com val ch reg inserted-text command-keys\),
1407 where `com' is the command to be re-executed, `val' is the
1408 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1409 if it exists, it is the name of the register for `com'.
1410 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
1411 (interactive "P")
1412 (let ((save-point (point)) ; save point before repeating prev cmd
1413 ;; Pass along that we are repeating a destructive command
1414 ;; This tells vip-set-destructive-command not to update
1415 ;; vip-command-ring
1416 (vip-intermediate-command 'vip-repeat))
1417 (if (eq last-command 'vip-undo)
1418 ;; if the last command was vip-undo, then undo-more
1419 (vip-undo-more)
1420 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
1421 ;; its prefix value is used as new prefix value for the command.
1422 (let ((m-com (car vip-d-com))
1423 (val (vip-P-val arg))
1424 (com (nth 2 vip-d-com))
1425 (reg (nth 3 vip-d-com)))
1426 (if (null val) (setq val (nth 1 vip-d-com)))
1427 (if (null m-com) (error "No previous command to repeat."))
1428 (setq vip-use-register reg)
1429 (if (nth 4 vip-d-com) ; text inserted by command
1430 (setq vip-last-insertion (nth 4 vip-d-com)
1431 vip-d-char (nth 4 vip-d-com)))
1432 (funcall m-com (cons val com))
1433 (if (and vip-keep-point-on-repeat (< save-point (point)))
1434 (goto-char save-point)) ; go back to before repeat.
1435 (if (and (eolp) (not (bolp)))
1436 (backward-char 1))
1438 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
1439 ;; If the prev cmd was rotating the command ring, this means that `.' has
1440 ;; just executed a command from that ring. So, push it on the ring again.
1441 ;; If we are just executing previous command , then don't push vip-d-com
1442 ;; because vip-d-com is not fully constructed in this case (its keys and
1443 ;; the inserted text may be nil). Besides, in this case, the command
1444 ;; executed by `.' is already on the ring.
1445 (if (eq last-command 'vip-display-current-destructive-command)
1446 (vip-push-onto-ring vip-d-com 'vip-command-ring))
1447 (vip-deactivate-mark)
1450 (defun vip-repeat-from-history ()
1451 "Repeat a destructive command from history.
1452 Doesn't change vip-command-ring in any way, so `.' will work as before
1453 executing this command.
1454 This command is supposed to be bound to a two-character Vi macro where
1455 the second character is a digit 0 to 9. The digit indicates which
1456 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1457 invokes the command before that, etc."
1458 (interactive)
1459 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
1460 (idx (cond (vip-this-kbd-macro
1461 (string-to-number
1462 (symbol-name (elt vip-this-kbd-macro 1))))
1463 (t 0)))
1464 (num idx)
1465 (vip-d-com vip-d-com))
1467 (or (and (numberp num) (<= 0 num) (<= num 9))
1468 (progn
1469 (setq idx 0
1470 num 0)
1471 (message
1472 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1473 (while (< 0 num)
1474 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
1475 (setq num (1- num)))
1476 (vip-repeat nil)
1477 (while (> idx num)
1478 (vip-special-ring-rotate1 vip-command-ring 1)
1479 (setq num (1+ num)))
1483 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1484 ;; The chars that can follow `#' are determined by vip-hash-command-p
1485 (defun vip-special-prefix-com (char)
1486 (cond ((= char ?c)
1487 (downcase-region (min vip-com-point (point))
1488 (max vip-com-point (point))))
1489 ((= char ?C)
1490 (upcase-region (min vip-com-point (point))
1491 (max vip-com-point (point))))
1492 ((= char ?g)
1493 (push-mark vip-com-point t)
1494 (vip-global-execute))
1495 ((= char ?q)
1496 (push-mark vip-com-point t)
1497 (vip-quote-region))
1498 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
1499 (t (error "#%c: %s" char vip-InvalidViCommand))))
1502 ;; undoing
1504 (defun vip-undo ()
1505 "Undo previous change."
1506 (interactive)
1507 (message "undo!")
1508 (let ((modified (buffer-modified-p))
1509 (before-undo-pt (point-marker))
1510 (after-change-functions after-change-functions)
1511 undo-beg-posn undo-end-posn)
1513 ;; no need to remove this hook, since this var has scope inside a let.
1514 (add-hook 'after-change-functions
1515 '(lambda (beg end len)
1516 (setq undo-beg-posn beg
1517 undo-end-posn (or end beg))))
1519 (undo-start)
1520 (undo-more 2)
1521 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1522 undo-end-posn (or undo-end-posn undo-beg-posn))
1524 (goto-char undo-beg-posn)
1525 (sit-for 0)
1526 (if (and vip-keep-point-on-undo
1527 (pos-visible-in-window-p before-undo-pt))
1528 (progn
1529 (push-mark (point-marker) t)
1530 (vip-sit-for-short 300)
1531 (goto-char undo-end-posn)
1532 (vip-sit-for-short 300)
1533 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
1534 (> (abs (- undo-end-posn before-undo-pt)) 1))
1535 (goto-char before-undo-pt)
1536 (goto-char undo-beg-posn)))
1537 (push-mark before-undo-pt t))
1538 (if (and (eolp) (not (bolp))) (backward-char 1))
1539 (if (not modified) (set-buffer-modified-p t)))
1540 (setq this-command 'vip-undo))
1542 ;; Continue undoing previous changes.
1543 (defun vip-undo-more ()
1544 (message "undo more!")
1545 (condition-case nil
1546 (undo-more 1)
1547 (error (beep)
1548 (message "No further undo information in this buffer")))
1549 (if (and (eolp) (not (bolp))) (backward-char 1))
1550 (setq this-command 'vip-undo))
1552 ;; The following two functions are used to set up undo properly.
1553 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1554 ;; they are undone all at once.
1555 (defun vip-adjust-undo ()
1556 (let ((inhibit-quit t)
1557 tmp tmp2)
1558 (setq vip-undo-needs-adjustment nil)
1559 (if (listp buffer-undo-list)
1560 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
1561 (progn
1562 (setq tmp2 (cdr tmp)) ; the part after mark
1564 ;; cut tail from buffer-undo-list temporarily by direct
1565 ;; manipulation with pointers in buffer-undo-list
1566 (setcdr tmp nil)
1568 (setq buffer-undo-list (delq nil buffer-undo-list))
1569 (setq buffer-undo-list
1570 (delq vip-buffer-undo-list-mark buffer-undo-list))
1571 ;; restore tail of buffer-undo-list
1572 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1573 (setq buffer-undo-list (delq nil buffer-undo-list))))))
1576 (defun vip-set-complex-command-for-undo ()
1577 (if (listp buffer-undo-list)
1578 (if (not vip-undo-needs-adjustment)
1579 (let ((inhibit-quit t))
1580 (setq buffer-undo-list
1581 (cons vip-buffer-undo-list-mark buffer-undo-list))
1582 (setq vip-undo-needs-adjustment t)))))
1587 (defun vip-display-current-destructive-command ()
1588 (let ((text (nth 4 vip-d-com))
1589 (keys (nth 5 vip-d-com))
1590 (max-text-len 30))
1592 (setq this-command 'vip-display-current-destructive-command)
1594 (message " `.' runs %s%s"
1595 (concat "`" (vip-array-to-string keys) "'")
1596 (vip-abbreviate-string text max-text-len
1597 " inserting `" "'" " ......."))
1601 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
1602 ;; or in some other way (non-interactively).
1603 (defun vip-set-destructive-command (list)
1604 (or (eq vip-intermediate-command 'vip-repeat)
1605 (progn
1606 (setq vip-d-com list)
1607 (setcar (nthcdr 5 vip-d-com)
1608 (vip-array-to-string (this-command-keys)))
1609 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
1611 (defun vip-prev-destructive-command (next)
1612 "Find previous destructive command in the history of destructive commands.
1613 With prefix argument, find next destructive command."
1614 (interactive "P")
1615 (let (cmd vip-intermediate-command)
1616 (if (eq last-command 'vip-display-current-destructive-command)
1617 ;; repeated search through command history
1618 (setq vip-intermediate-command 'repeating-display-destructive-command)
1619 ;; first search through command history--set temp ring
1620 (setq vip-temp-command-ring (copy-list vip-command-ring)))
1621 (setq cmd (if next
1622 (vip-special-ring-rotate1 vip-temp-command-ring 1)
1623 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
1624 (if (null cmd)
1626 (setq vip-d-com cmd))
1627 (vip-display-current-destructive-command)))
1629 (defun vip-next-destructive-command ()
1630 "Find next destructive command in the history of destructive commands."
1631 (interactive)
1632 (vip-prev-destructive-command 'next))
1634 (defun vip-insert-prev-from-insertion-ring (arg)
1635 "Cycle through insertion ring in the direction of older insertions.
1636 Undoes previous insertion and inserts new.
1637 With prefix argument, cycles in the direction of newer elements.
1638 In minibuffer, this command executes whatever the invocation key is bound
1639 to in the global map, instead of cycling through the insertion ring."
1640 (interactive "P")
1641 (let (vip-intermediate-command)
1642 (if (eq last-command 'vip-insert-from-insertion-ring)
1643 (progn ; repeated search through insertion history
1644 (setq vip-intermediate-command 'repeating-insertion-from-ring)
1645 (if (eq vip-current-state 'replace-state)
1646 (undo 1)
1647 (if vip-last-inserted-string-from-insertion-ring
1648 (backward-delete-char
1649 (length vip-last-inserted-string-from-insertion-ring))))
1651 ;;first search through insertion history
1652 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
1653 (setq this-command 'vip-insert-from-insertion-ring)
1654 ;; so that things will be undone properly
1655 (setq buffer-undo-list (cons nil buffer-undo-list))
1656 (setq vip-last-inserted-string-from-insertion-ring
1657 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
1659 ;; this change of vip-intermediate-command must come after
1660 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
1661 ;; insertion.
1662 (setq vip-intermediate-command nil)
1663 (if vip-last-inserted-string-from-insertion-ring
1664 (insert vip-last-inserted-string-from-insertion-ring))
1667 (defun vip-insert-next-from-insertion-ring ()
1668 "Cycle through insertion ring in the direction of older insertions.
1669 Undo previous insertion and inserts new."
1670 (interactive)
1671 (vip-insert-prev-from-insertion-ring 'next))
1674 ;; some region utilities
1676 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1677 (defun vip-add-newline-at-eob-if-necessary ()
1678 (save-excursion
1679 (end-of-line)
1680 ;; make sure all lines end with newline, unless in the minibuffer or
1681 ;; when requested otherwise (require-final-newline is nil)
1682 (if (and (eobp)
1683 (not (bolp))
1684 require-final-newline
1685 (not (vip-is-in-minibuffer))
1686 (not buffer-read-only))
1687 (insert "\n"))))
1689 (defun vip-yank-defun ()
1690 (mark-defun)
1691 (copy-region-as-kill (point) (mark t)))
1693 ;; Enlarge region between BEG and END.
1694 (defun vip-enlarge-region (beg end)
1695 (or beg (setq beg end)) ; if beg is nil, set to end
1696 (or end (setq end beg)) ; if end is nil, set to beg
1698 (if (< beg end)
1699 (progn (goto-char beg) (set-mark end))
1700 (goto-char end)
1701 (set-mark beg))
1702 (beginning-of-line)
1703 (exchange-point-and-mark)
1704 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1705 (if (not (eobp)) (beginning-of-line))
1706 (if (> beg end) (exchange-point-and-mark)))
1709 ;; Quote region by each line with a user supplied string.
1710 (defun vip-quote-region ()
1711 (setq vip-quote-string
1712 (vip-read-string-with-history
1713 "Quote string: "
1715 'vip-quote-region-history
1716 vip-quote-string))
1717 (vip-enlarge-region (point) (mark t))
1718 (if (> (point) (mark t)) (exchange-point-and-mark))
1719 (insert vip-quote-string)
1720 (beginning-of-line)
1721 (forward-line 1)
1722 (while (and (< (point) (mark t)) (bolp))
1723 (insert vip-quote-string)
1724 (beginning-of-line)
1725 (forward-line 1)))
1727 ;; Tells whether BEG is on the same line as END.
1728 ;; If one of the args is nil, it'll return nil.
1729 (defun vip-same-line (beg end)
1730 (let ((selective-display nil)
1731 (incr 0)
1732 temp)
1733 (if (and beg end (> beg end))
1734 (setq temp beg
1735 beg end
1736 end temp))
1737 (if (and beg end)
1738 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
1739 nil)
1741 ;; This 'if' is needed because Emacs treats the next empty line
1742 ;; as part of the previous line.
1743 (if (= (vip-line-pos 'start) end)
1744 (setq incr 1))
1745 (<= (+ incr (count-lines beg end)) 1))))
1749 ;; Check if the string ends with a newline.
1750 (defun vip-end-with-a-newline-p (string)
1751 (or (string= string "")
1752 (= (vip-seq-last-elt string) ?\n)))
1754 (defun vip-tmp-insert-at-eob (msg)
1755 (let ((savemax (point-max)))
1756 (goto-char savemax)
1757 (insert msg)
1758 (sit-for 2)
1759 (goto-char savemax) (delete-region (point) (point-max))
1764 ;;; Minibuffer business
1766 (defsubst vip-set-minibuffer-style ()
1767 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
1770 (defun vip-minibuffer-setup-sentinel ()
1771 (let ((hook (if vip-vi-style-in-minibuffer
1772 'vip-change-state-to-insert
1773 'vip-change-state-to-emacs)))
1774 (funcall hook)
1777 ;; Interpret last event in the local map
1778 (defun vip-exit-minibuffer ()
1779 (interactive)
1780 (let (command)
1781 (setq command (local-key-binding (char-to-string last-command-char)))
1782 (if command
1783 (command-execute command)
1784 (exit-minibuffer))))
1787 ;;; Reading string with history
1789 (defun vip-read-string-with-history (prompt &optional initial
1790 history-var default keymap)
1791 ;; Read string, prompting with PROMPT and inserting the INITIAL
1792 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
1793 ;; input is an empty string. Use KEYMAP, if given, or the
1794 ;; minibuffer-local-map.
1795 ;; Default value is displayed until the user types something in the
1796 ;; minibuffer.
1797 (let ((minibuffer-setup-hook
1798 '(lambda ()
1799 (if (stringp initial)
1800 (progn
1801 ;; don't wait if we have unread events or in kbd macro
1802 (or unread-command-events
1803 executing-kbd-macro
1804 (sit-for 840))
1805 (erase-buffer)
1806 (insert initial)))
1807 (vip-minibuffer-setup-sentinel)))
1808 (val "")
1809 (padding "")
1810 temp-msg)
1812 (setq keymap (or keymap minibuffer-local-map)
1813 initial (or initial "")
1814 temp-msg (if default
1815 (format "(default: %s) " default)
1816 ""))
1818 (setq vip-incomplete-ex-cmd nil)
1819 (setq val (read-from-minibuffer prompt
1820 (concat temp-msg initial val padding)
1821 keymap nil history-var))
1822 (setq minibuffer-setup-hook nil
1823 padding (vip-array-to-string (this-command-keys))
1824 temp-msg "")
1825 ;; the following tries to be smart about what to put in history
1826 (if (not (string= val (car (eval history-var))))
1827 (set history-var (cons val (eval history-var))))
1828 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
1829 (string= (nth 0 (eval history-var)) ""))
1830 (set history-var (cdr (eval history-var))))
1831 ;; If the user enters nothing but the prev cmd wasn't vip-ex,
1832 ;; vip-command-argument, or `! shell-command', this probably means
1833 ;; that the user typed something then erased. Return "" in this case, not
1834 ;; the default---the default is too confusing in this case.
1835 (cond ((and (string= val "")
1836 (not (string= prompt "!")) ; was a `! shell-command'
1837 (not (memq last-command
1838 '(vip-ex
1839 vip-command-argument
1843 ((string= val "") (or default ""))
1844 (t val))
1849 ;; insertion commands
1851 ;; Called when state changes from Insert Vi command mode.
1852 ;; Repeats the insertion command if Insert state was entered with prefix
1853 ;; argument > 1.
1854 (defun vip-repeat-insert-command ()
1855 (let ((i-com (car vip-d-com))
1856 (val (nth 1 vip-d-com))
1857 (char (nth 2 vip-d-com)))
1858 (if (and val (> val 1)) ; first check that val is non-nil
1859 (progn
1860 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
1861 (vip-repeat nil)
1862 (setq vip-d-com (list i-com val char nil nil nil))
1863 ))))
1865 (defun vip-insert (arg)
1866 "Insert before point."
1867 (interactive "P")
1868 (vip-set-complex-command-for-undo)
1869 (let ((val (vip-p-val arg))
1870 (com (vip-getcom arg)))
1871 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
1872 (if com
1873 (vip-loop val (vip-yank-last-insertion))
1874 (vip-change-state-to-insert))))
1876 (defun vip-append (arg)
1877 "Append after point."
1878 (interactive "P")
1879 (vip-set-complex-command-for-undo)
1880 (let ((val (vip-p-val arg))
1881 (com (vip-getcom arg)))
1882 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
1883 (if (not (eolp)) (forward-char))
1884 (if (equal com ?r)
1885 (vip-loop val (vip-yank-last-insertion))
1886 (vip-change-state-to-insert))))
1888 (defun vip-Append (arg)
1889 "Append at end of line."
1890 (interactive "P")
1891 (vip-set-complex-command-for-undo)
1892 (let ((val (vip-p-val arg))
1893 (com (vip-getcom arg)))
1894 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
1895 (end-of-line)
1896 (if (equal com ?r)
1897 (vip-loop val (vip-yank-last-insertion))
1898 (vip-change-state-to-insert))))
1900 (defun vip-Insert (arg)
1901 "Insert before first non-white."
1902 (interactive "P")
1903 (vip-set-complex-command-for-undo)
1904 (let ((val (vip-p-val arg))
1905 (com (vip-getcom arg)))
1906 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
1907 (back-to-indentation)
1908 (if (equal com ?r)
1909 (vip-loop val (vip-yank-last-insertion))
1910 (vip-change-state-to-insert))))
1912 (defun vip-open-line (arg)
1913 "Open line below."
1914 (interactive "P")
1915 (vip-set-complex-command-for-undo)
1916 (let ((val (vip-p-val arg))
1917 (com (vip-getcom arg)))
1918 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
1919 (let ((col (current-indentation)))
1920 (if (equal com ?r)
1921 (vip-loop val
1922 (progn
1923 (end-of-line)
1924 (newline 1)
1925 (if vip-auto-indent
1926 (progn
1927 (setq vip-cted t)
1928 (if vip-electric-mode
1929 (indent-according-to-mode)
1930 (indent-to col))
1932 (vip-yank-last-insertion)))
1933 (end-of-line)
1934 (newline 1)
1935 (if vip-auto-indent
1936 (progn
1937 (setq vip-cted t)
1938 (if vip-electric-mode
1939 (indent-according-to-mode)
1940 (indent-to col))))
1941 (vip-change-state-to-insert)))))
1943 (defun vip-Open-line (arg)
1944 "Open line above."
1945 (interactive "P")
1946 (vip-set-complex-command-for-undo)
1947 (let ((val (vip-p-val arg))
1948 (com (vip-getcom arg)))
1949 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
1950 (let ((col (current-indentation)))
1951 (if (equal com ?r)
1952 (vip-loop val
1953 (progn
1954 (beginning-of-line)
1955 (open-line 1)
1956 (if vip-auto-indent
1957 (progn
1958 (setq vip-cted t)
1959 (if vip-electric-mode
1960 (indent-according-to-mode)
1961 (indent-to col))
1963 (vip-yank-last-insertion)))
1964 (beginning-of-line)
1965 (open-line 1)
1966 (if vip-auto-indent
1967 (progn
1968 (setq vip-cted t)
1969 (if vip-electric-mode
1970 (indent-according-to-mode)
1971 (indent-to col))
1973 (vip-change-state-to-insert)))))
1975 (defun vip-open-line-at-point (arg)
1976 "Open line at point."
1977 (interactive "P")
1978 (vip-set-complex-command-for-undo)
1979 (let ((val (vip-p-val arg))
1980 (com (vip-getcom arg)))
1981 (vip-set-destructive-command
1982 (list 'vip-open-line-at-point val ?r nil nil nil))
1983 (if (equal com ?r)
1984 (vip-loop val
1985 (progn
1986 (open-line 1)
1987 (vip-yank-last-insertion)))
1988 (open-line 1)
1989 (vip-change-state-to-insert))))
1991 (defun vip-substitute (arg)
1992 "Substitute characters."
1993 (interactive "P")
1994 (let ((val (vip-p-val arg))
1995 (com (vip-getcom arg)))
1996 (push-mark nil t)
1997 (forward-char val)
1998 (if (equal com ?r)
1999 (vip-change-subr (mark t) (point))
2000 (vip-change (mark t) (point)))
2001 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
2004 (defun vip-substitute-line (arg)
2005 "Substitute lines."
2006 (interactive "p")
2007 (vip-set-complex-command-for-undo)
2008 (vip-line (cons arg ?C)))
2010 ;; Prepare for replace
2011 (defun vip-start-replace ()
2012 (setq vip-began-as-replace t
2013 vip-sitting-in-replace t
2014 vip-replace-chars-to-delete 0
2015 vip-replace-chars-deleted 0)
2016 (vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
2017 (vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
2018 ;; this will get added repeatedly, but no harm
2019 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
2020 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
2021 (vip-move-marker-locally 'vip-last-posn-in-replace-region
2022 (vip-replace-start))
2023 (vip-add-hook
2024 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
2025 (vip-add-hook
2026 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2027 ;; guard against a smartie who switched from R-replace to normal replace
2028 (vip-remove-hook
2029 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2030 (if overwrite-mode (overwrite-mode nil))
2034 ;; checks how many chars were deleted by the last change
2035 (defun vip-replace-mode-spy-before (beg end)
2036 (setq vip-replace-chars-deleted
2037 (- end beg
2038 (max 0 (- end (vip-replace-end)))
2039 (max 0 (- (vip-replace-start) beg))
2042 ;; Invoked as an after-change-function to set up parameters of the last change
2043 (defun vip-replace-mode-spy-after (beg end length)
2044 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
2045 (progn
2046 (setq vip-replace-chars-to-delete 0)
2047 (vip-move-marker-locally
2048 'vip-last-posn-in-replace-region (point)))
2050 (let (beg-col end-col real-end chars-to-delete)
2051 (setq real-end (min end (vip-replace-end)))
2052 (save-excursion
2053 (goto-char beg)
2054 (setq beg-col (current-column))
2055 (goto-char real-end)
2056 (setq end-col (current-column)))
2058 ;; If beg of change is outside the replacement region, then don't
2059 ;; delete anything in the repl region (set chars-to-delete to 0).
2061 ;; This works fine except that we have to take special care of
2062 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
2063 ;; sometimes simply shifts the repl region rightwards, without
2064 ;; deleting an equal amount of characters.
2066 ;; The reason why new-dabbrev.el causes this are this:
2067 ;; if one dinamically completes a partial word that starts before the
2068 ;; replacement region (but ends inside) then new-dabbrev.el first
2069 ;; moves cursor backwards, to the beginning of the word to be
2070 ;; completed (say, pt A). Then it inserts the
2071 ;; completed word and then deletes the old, incomplete part.
2072 ;; Since the complete word is inserted at position before the repl
2073 ;; region, the next If-statement would have set chars-to-delete to 0
2074 ;; unless we check for the current command, which must be
2075 ;; dabbrev-expand.
2077 ;; In fact, it might be also useful to have overlays for insert
2078 ;; regions as well, since this will let us capture the situation when
2079 ;; dabbrev-expand goes back past the insertion point to find the
2080 ;; beginning of the word to be expanded.
2081 (if (or (and (<= (vip-replace-start) beg)
2082 (<= beg (vip-replace-end)))
2083 (and (= length 0) (eq this-command 'dabbrev-expand)))
2084 (setq chars-to-delete
2085 (max (- end-col beg-col) (- real-end beg) 0))
2086 (setq chars-to-delete 0))
2088 ;; if beg = last change position, it means that we are within the
2089 ;; same command that does multiple changes. Moreover, it means
2090 ;; that we have two subsequent changes (insert/delete) that
2091 ;; complement each other.
2092 (if (= beg (marker-position vip-last-posn-in-replace-region))
2093 (setq vip-replace-chars-to-delete
2094 (- (+ chars-to-delete vip-replace-chars-to-delete)
2095 vip-replace-chars-deleted))
2096 (setq vip-replace-chars-to-delete chars-to-delete))
2098 (vip-move-marker-locally
2099 'vip-last-posn-in-replace-region
2100 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
2101 (or (marker-position vip-last-posn-in-replace-region)
2102 (vip-replace-start))
2105 (setq vip-replace-chars-to-delete
2106 (max 0
2107 (min vip-replace-chars-to-delete
2108 (- (vip-replace-end) vip-last-posn-in-replace-region)
2109 (- (vip-line-pos 'end) vip-last-posn-in-replace-region)
2114 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
2115 ;; posn is within the overlay.
2116 (defun vip-finish-change (posn)
2117 (vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
2118 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2119 (vip-remove-hook 'vip-post-command-hooks
2120 'vip-replace-state-post-command-sentinel)
2121 (vip-remove-hook
2122 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2123 (vip-restore-cursor-color-after-replace)
2124 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2125 (save-excursion
2126 (if (and
2127 vip-replace-overlay
2128 (>= posn (vip-replace-start))
2129 (< posn (vip-replace-end)))
2130 (delete-region posn (vip-replace-end)))
2133 (if (eq vip-current-state 'replace-state)
2134 (vip-downgrade-to-insert))
2135 ;; replace mode ended => nullify vip-last-posn-in-replace-region
2136 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
2137 (vip-hide-replace-overlay)
2138 (vip-refresh-mode-line)
2139 (vip-put-string-on-kill-ring vip-last-replace-region)
2142 ;; Make STRING be the first element of the kill ring.
2143 (defun vip-put-string-on-kill-ring (string)
2144 (setq kill-ring (cons string kill-ring))
2145 (if (> (length kill-ring) kill-ring-max)
2146 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2147 (setq kill-ring-yank-pointer kill-ring))
2149 (defun vip-finish-R-mode ()
2150 (vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2151 (vip-remove-hook
2152 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2153 (vip-downgrade-to-insert))
2155 (defun vip-start-R-mode ()
2156 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2157 (overwrite-mode 1)
2158 (vip-add-hook
2159 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
2160 (vip-add-hook
2161 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2162 ;; guard against a smartie who switched from R-replace to normal replace
2163 (vip-remove-hook
2164 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel)
2169 (defun vip-replace-state-exit-cmd ()
2170 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2171 These keys are ESC, RET, and LineFeed"
2172 (interactive)
2173 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2174 (vip-finish-R-mode)
2175 (vip-finish-change vip-last-posn-in-replace-region))
2176 (let (com)
2177 (if (eq this-command 'vip-intercept-ESC-key)
2178 (setq com 'vip-exit-insert-state)
2179 (vip-set-unread-command-events last-input-char)
2180 (setq com (key-binding (read-key-sequence nil))))
2182 (condition-case conds
2183 (command-execute com)
2184 (error
2185 (vip-message-conditions conds)))
2187 (vip-hide-replace-overlay))
2189 (defun vip-replace-state-carriage-return ()
2190 "Implements carriage return in Viper replace state."
2191 (interactive)
2192 ;; If Emacs start supporting overlay maps, as it currently supports
2193 ;; text-property maps, we could do away with vip-replace-minor-mode and
2194 ;; just have keymap attached to replace overlay. Then the "if part" of this
2195 ;; statement can be deleted.
2196 (if (or (< (point) (vip-replace-start))
2197 (> (point) (vip-replace-end)))
2198 (let (vip-replace-minor-mode com)
2199 (vip-set-unread-command-events last-input-char)
2200 (setq com (key-binding (read-key-sequence nil)))
2201 (condition-case conds
2202 (command-execute com)
2203 (error
2204 (vip-message-conditions conds))))
2205 (if (not vip-allow-multiline-replace-regions)
2206 (vip-replace-state-exit-cmd)
2207 (if (vip-same-line (point) (vip-replace-end))
2208 (vip-replace-state-exit-cmd)
2209 (vip-kill-line nil)
2210 (vip-next-line-at-bol nil)))))
2213 ;; This is the function bound to 'R'---unlimited replace.
2214 ;; Similar to Emacs's own overwrite-mode.
2215 (defun vip-overwrite (arg)
2216 "Begin overwrite mode."
2217 (interactive "P")
2218 (let ((val (vip-p-val arg))
2219 (com (vip-getcom arg)) (len))
2220 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
2221 (if com
2222 (progn
2223 ;; Viper saves inserted text in vip-last-insertion
2224 (setq len (length vip-last-insertion))
2225 (delete-char len)
2226 (vip-loop val (vip-yank-last-insertion)))
2227 (setq last-command 'vip-overwrite)
2228 (vip-set-complex-command-for-undo)
2229 (vip-set-replace-overlay (point) (vip-line-pos 'end))
2230 (vip-change-state-to-replace)
2234 ;; line commands
2236 (defun vip-line (arg)
2237 (let ((val (car arg))
2238 (com (cdr arg)))
2239 (vip-move-marker-locally 'vip-com-point (point))
2240 (if (not (eobp))
2241 (vip-next-line-carefully (1- val)))
2242 ;; this ensures that dd, cc, D, yy will do the right thing on the last
2243 ;; line of buffer when this line has no \n.
2244 (vip-add-newline-at-eob-if-necessary)
2245 (vip-execute-com 'vip-line val com))
2246 (if (and (eobp) (not (bobp))) (forward-line -1))
2249 (defun vip-yank-line (arg)
2250 "Yank ARG lines (in Vi's sense)."
2251 (interactive "P")
2252 (let ((val (vip-p-val arg)))
2253 (vip-line (cons val ?Y))))
2256 ;; region commands
2258 (defun vip-region (arg)
2259 "Execute command on a region."
2260 (interactive "P")
2261 (let ((val (vip-P-val arg))
2262 (com (vip-getcom arg)))
2263 (vip-move-marker-locally 'vip-com-point (point))
2264 (exchange-point-and-mark)
2265 (vip-execute-com 'vip-region val com)))
2267 (defun vip-Region (arg)
2268 "Execute command on a Region."
2269 (interactive "P")
2270 (let ((val (vip-P-val arg))
2271 (com (vip-getCom arg)))
2272 (vip-move-marker-locally 'vip-com-point (point))
2273 (exchange-point-and-mark)
2274 (vip-execute-com 'vip-Region val com)))
2276 (defun vip-replace-char (arg)
2277 "Replace the following ARG chars by the character read."
2278 (interactive "P")
2279 (if (and (eolp) (bolp)) (error "No character to replace here"))
2280 (let ((val (vip-p-val arg))
2281 (com (vip-getcom arg)))
2282 (vip-replace-char-subr com val)
2283 (if (and (eolp) (not (bolp))) (forward-char 1))
2284 (vip-set-destructive-command
2285 (list 'vip-replace-char val ?r nil vip-d-char nil))
2288 (defun vip-replace-char-subr (com arg)
2289 (let ((take-care-of-iso-accents
2290 (and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
2291 char)
2292 (setq char (if (equal com ?r)
2293 vip-d-char
2294 (read-char)))
2295 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
2296 ;; get European characters
2297 (progn
2298 (iso-accents-mode 1)
2299 (vip-set-unread-command-events char)
2300 (setq char (aref (read-key-sequence nil) 0))
2301 (iso-accents-mode -1)))
2302 (delete-char arg t)
2303 (setq vip-d-char char)
2304 (vip-loop (if (> arg 0) arg (- arg))
2305 (if (eq char ?\C-m) (insert "\n") (insert char)))
2306 (backward-char arg)))
2309 ;; basic cursor movement. j, k, l, h commands.
2311 (defun vip-forward-char (arg)
2312 "Move point right ARG characters (left if ARG negative).
2313 On reaching end of line, stop and signal error."
2314 (interactive "P")
2315 (vip-leave-region-active)
2316 (let ((val (vip-p-val arg))
2317 (com (vip-getcom arg)))
2318 (if com (vip-move-marker-locally 'vip-com-point (point)))
2319 (if vip-ex-style-motion
2320 (progn
2321 ;; the boundary condition check gets weird here because
2322 ;; forward-char may be the parameter of a delete, and 'dl' works
2323 ;; just like 'x' for the last char on a line, so we have to allow
2324 ;; the forward motion before the 'vip-execute-com', but, of
2325 ;; course, 'dl' doesn't work on an empty line, so we have to
2326 ;; catch that condition before 'vip-execute-com'
2327 (if (and (eolp) (bolp)) (error "") (forward-char val))
2328 (if com (vip-execute-com 'vip-forward-char val com))
2329 (if (eolp) (progn (backward-char 1) (error ""))))
2330 (forward-char val)
2331 (if com (vip-execute-com 'vip-forward-char val com)))))
2333 (defun vip-backward-char (arg)
2334 "Move point left ARG characters (right if ARG negative).
2335 On reaching beginning of line, stop and signal error."
2336 (interactive "P")
2337 (vip-leave-region-active)
2338 (let ((val (vip-p-val arg))
2339 (com (vip-getcom arg)))
2340 (if com (vip-move-marker-locally 'vip-com-point (point)))
2341 (if vip-ex-style-motion
2342 (progn
2343 (if (bolp) (error "") (backward-char val))
2344 (if com (vip-execute-com 'vip-backward-char val com)))
2345 (backward-char val)
2346 (if com (vip-execute-com 'vip-backward-char val com)))))
2348 ;; Like forward-char, but doesn't move at end of buffer.
2349 (defun vip-forward-char-carefully (&optional arg)
2350 (setq arg (or arg 1))
2351 (if (>= (point-max) (+ (point) arg))
2352 (forward-char arg)
2353 (goto-char (point-max))))
2355 ;; Like backward-char, but doesn't move at end of buffer.
2356 (defun vip-backward-char-carefully (&optional arg)
2357 (setq arg (or arg 1))
2358 (if (<= (point-min) (- (point) arg))
2359 (backward-char arg)
2360 (goto-char (point-min))))
2362 (defun vip-next-line-carefully (arg)
2363 (condition-case nil
2364 (next-line arg)
2365 (error nil)))
2369 ;;; Word command
2371 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
2372 ;; for word movement. When executed with a destructive command, \n is
2373 ;; usually left untouched for the last word.
2374 ;; Viper uses syntax table to determine what is a word and what is a
2375 ;; separator. However, \n is always a separator. Also, if vip-syntax-preference
2376 ;; is 'vi, then `_' is part of the word.
2378 ;; skip only one \n
2379 (defun vip-skip-separators (forward)
2380 (if forward
2381 (progn
2382 (vip-skip-all-separators-forward 'within-line)
2383 (if (looking-at "\n")
2384 (progn
2385 (forward-char)
2386 (vip-skip-all-separators-forward 'within-line))))
2387 (vip-skip-all-separators-backward 'within-line)
2388 (backward-char)
2389 (if (looking-at "\n")
2390 (vip-skip-all-separators-backward 'within-line)
2391 (forward-char))))
2393 (defun vip-forward-word-kernel (val)
2394 (while (> val 0)
2395 (cond ((vip-looking-at-alpha)
2396 (vip-skip-alpha-forward "_")
2397 (vip-skip-separators t))
2398 ((vip-looking-at-separator)
2399 (vip-skip-separators t))
2400 ((not (vip-looking-at-alphasep))
2401 (vip-skip-nonalphasep-forward)
2402 (vip-skip-separators t)))
2403 (setq val (1- val))))
2405 ;; first search backward for pat. Then skip chars backwards using aux-pat
2406 (defun vip-fwd-skip (pat aux-pat lim)
2407 (if (and (save-excursion
2408 (re-search-backward pat lim t))
2409 (= (point) (match-end 0)))
2410 (goto-char (match-beginning 0)))
2411 (skip-chars-backward aux-pat lim)
2412 (if (= (point) lim)
2413 (vip-forward-char-carefully))
2417 (defun vip-forward-word (arg)
2418 "Forward word."
2419 (interactive "P")
2420 (vip-leave-region-active)
2421 (let ((val (vip-p-val arg))
2422 (com (vip-getcom arg)))
2423 (if com (vip-move-marker-locally 'vip-com-point (point)))
2424 (vip-forward-word-kernel val)
2425 (if com (progn
2426 (cond ((memq com (list ?c (- ?c)))
2427 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
2428 ;; Yank words including the whitespace, but not newline
2429 ((memq com (list ?y (- ?y)))
2430 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
2431 ((vip-dotable-command-p com)
2432 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
2433 (vip-execute-com 'vip-forward-word val com)))))
2436 (defun vip-forward-Word (arg)
2437 "Forward word delimited by white characters."
2438 (interactive "P")
2439 (vip-leave-region-active)
2440 (let ((val (vip-p-val arg))
2441 (com (vip-getcom arg)))
2442 (if com (vip-move-marker-locally 'vip-com-point (point)))
2443 (vip-loop val
2444 (progn
2445 (vip-skip-nonseparators 'forward)
2446 (vip-skip-separators t)))
2447 (if com (progn
2448 (cond ((memq com (list ?c (- ?c)))
2449 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
2450 ;; Yank words including the whitespace, but not newline
2451 ((memq com (list ?y (- ?y)))
2452 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
2453 ((vip-dotable-command-p com)
2454 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
2455 (vip-execute-com 'vip-forward-Word val com)))))
2458 ;; this is a bit different from Vi, but Vi's end of word
2459 ;; makes no sense whatsoever
2460 (defun vip-end-of-word-kernel ()
2461 (if (vip-end-of-word-p) (forward-char))
2462 (if (vip-looking-at-separator)
2463 (vip-skip-all-separators-forward))
2465 (cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
2466 ((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
2467 (vip-backward-char-carefully))
2469 (defun vip-end-of-word-p ()
2470 (or (eobp)
2471 (save-excursion
2472 (cond ((vip-looking-at-alpha)
2473 (forward-char)
2474 (not (vip-looking-at-alpha)))
2475 ((not (vip-looking-at-alphasep))
2476 (forward-char)
2477 (vip-looking-at-alphasep))))))
2480 (defun vip-end-of-word (arg &optional careful)
2481 "Move point to end of current word."
2482 (interactive "P")
2483 (vip-leave-region-active)
2484 (let ((val (vip-p-val arg))
2485 (com (vip-getcom arg)))
2486 (if com (vip-move-marker-locally 'vip-com-point (point)))
2487 (vip-loop val (vip-end-of-word-kernel))
2488 (if com
2489 (progn
2490 (forward-char)
2491 (vip-execute-com 'vip-end-of-word val com)))))
2493 (defun vip-end-of-Word (arg)
2494 "Forward to end of word delimited by white character."
2495 (interactive "P")
2496 (vip-leave-region-active)
2497 (let ((val (vip-p-val arg))
2498 (com (vip-getcom arg)))
2499 (if com (vip-move-marker-locally 'vip-com-point (point)))
2500 (vip-loop val
2501 (progn
2502 (vip-end-of-word-kernel)
2503 (vip-skip-nonseparators 'forward)
2504 (backward-char)))
2505 (if com
2506 (progn
2507 (forward-char)
2508 (vip-execute-com 'vip-end-of-Word val com)))))
2510 (defun vip-backward-word-kernel (val)
2511 (while (> val 0)
2512 (backward-char)
2513 (cond ((vip-looking-at-alpha)
2514 (vip-skip-alpha-backward "_"))
2515 ((vip-looking-at-separator)
2516 (forward-char)
2517 (vip-skip-separators nil)
2518 (backward-char)
2519 (cond ((vip-looking-at-alpha)
2520 (vip-skip-alpha-backward "_"))
2521 ((not (vip-looking-at-alphasep))
2522 (vip-skip-nonalphasep-backward))
2523 (t (forward-char))))
2524 ((not (vip-looking-at-alphasep))
2525 (vip-skip-nonalphasep-backward)))
2526 (setq val (1- val))))
2528 (defun vip-backward-word (arg)
2529 "Backward word."
2530 (interactive "P")
2531 (vip-leave-region-active)
2532 (let ((val (vip-p-val arg))
2533 (com (vip-getcom arg)))
2534 (if com
2535 (let (i)
2536 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2537 (backward-char))
2538 (vip-move-marker-locally 'vip-com-point (point))
2539 (if i (forward-char))))
2540 (vip-backward-word-kernel val)
2541 (if com (vip-execute-com 'vip-backward-word val com))))
2543 (defun vip-backward-Word (arg)
2544 "Backward word delimited by white character."
2545 (interactive "P")
2546 (vip-leave-region-active)
2547 (let ((val (vip-p-val arg))
2548 (com (vip-getcom arg)))
2549 (if com
2550 (let (i)
2551 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2552 (backward-char))
2553 (vip-move-marker-locally 'vip-com-point (point))
2554 (if i (forward-char))))
2555 (vip-loop val
2556 (progn
2557 (vip-skip-separators nil)
2558 (vip-skip-nonseparators 'backward)))
2559 (if com (vip-execute-com 'vip-backward-Word val com))))
2563 ;; line commands
2565 (defun vip-beginning-of-line (arg)
2566 "Go to beginning of line."
2567 (interactive "P")
2568 (vip-leave-region-active)
2569 (let ((val (vip-p-val arg))
2570 (com (vip-getcom arg)))
2571 (if com (vip-move-marker-locally 'vip-com-point (point)))
2572 (beginning-of-line val)
2573 (if com (vip-execute-com 'vip-beginning-of-line val com))))
2575 (defun vip-bol-and-skip-white (arg)
2576 "Beginning of line at first non-white character."
2577 (interactive "P")
2578 (vip-leave-region-active)
2579 (let ((val (vip-p-val arg))
2580 (com (vip-getcom arg)))
2581 (if com (vip-move-marker-locally 'vip-com-point (point)))
2582 (forward-to-indentation (1- val))
2583 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
2585 (defun vip-goto-eol (arg)
2586 "Go to end of line."
2587 (interactive "P")
2588 (vip-leave-region-active)
2589 (let ((val (vip-p-val arg))
2590 (com (vip-getcom arg)))
2591 (if com (vip-move-marker-locally 'vip-com-point (point)))
2592 (end-of-line val)
2593 (if com (vip-execute-com 'vip-goto-eol val com))
2594 (if vip-ex-style-motion
2595 (if (and (eolp) (not (bolp))
2596 ;; a fix for vip-change-to-eol
2597 (not (equal vip-current-state 'insert-state)))
2598 (backward-char 1)
2599 ))))
2602 (defun vip-goto-col (arg)
2603 "Go to ARG's column."
2604 (interactive "P")
2605 (vip-leave-region-active)
2606 (let ((val (vip-p-val arg))
2607 (com (vip-getcom arg))
2608 line-len)
2609 (setq line-len (- (vip-line-pos 'end) (vip-line-pos 'start)))
2610 (if com (vip-move-marker-locally 'vip-com-point (point)))
2611 (beginning-of-line)
2612 (forward-char (1- (min line-len val)))
2613 (while (> (current-column) (1- val))
2614 (backward-char 1))
2615 (if com (vip-execute-com 'vip-goto-col val com))
2616 (save-excursion
2617 (end-of-line)
2618 (if (> val (current-column)) (error "")))
2622 (defun vip-next-line (arg)
2623 "Go to next line."
2624 (interactive "P")
2625 (vip-leave-region-active)
2626 (let ((val (vip-p-val arg))
2627 (com (vip-getCom arg)))
2628 (if com (vip-move-marker-locally 'vip-com-point (point)))
2629 (next-line val)
2630 (if vip-ex-style-motion
2631 (if (and (eolp) (not (bolp))) (backward-char 1)))
2632 (setq this-command 'next-line)
2633 (if com (vip-execute-com 'vip-next-line val com))))
2635 (defun vip-next-line-at-bol (arg)
2636 "Next line at beginning of line."
2637 (interactive "P")
2638 (vip-leave-region-active)
2639 (save-excursion
2640 (end-of-line)
2641 (if (eobp) (error "Last line in buffer")))
2642 (let ((val (vip-p-val arg))
2643 (com (vip-getCom arg)))
2644 (if com (vip-move-marker-locally 'vip-com-point (point)))
2645 (forward-line val)
2646 (back-to-indentation)
2647 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
2649 (defun vip-previous-line (arg)
2650 "Go to previous line."
2651 (interactive "P")
2652 (vip-leave-region-active)
2653 (let ((val (vip-p-val arg))
2654 (com (vip-getCom arg)))
2655 (if com (vip-move-marker-locally 'vip-com-point (point)))
2656 (previous-line val)
2657 (if vip-ex-style-motion
2658 (if (and (eolp) (not (bolp))) (backward-char 1)))
2659 (setq this-command 'previous-line)
2660 (if com (vip-execute-com 'vip-previous-line val com))))
2663 (defun vip-previous-line-at-bol (arg)
2664 "Previous line at beginning of line."
2665 (interactive "P")
2666 (vip-leave-region-active)
2667 (save-excursion
2668 (beginning-of-line)
2669 (if (bobp) (error "First line in buffer")))
2670 (let ((val (vip-p-val arg))
2671 (com (vip-getCom arg)))
2672 (if com (vip-move-marker-locally 'vip-com-point (point)))
2673 (forward-line (- val))
2674 (back-to-indentation)
2675 (if com (vip-execute-com 'vip-previous-line val com))))
2677 (defun vip-change-to-eol (arg)
2678 "Change to end of line."
2679 (interactive "P")
2680 (vip-goto-eol (cons arg ?c)))
2682 (defun vip-kill-line (arg)
2683 "Delete line."
2684 (interactive "P")
2685 (vip-goto-eol (cons arg ?d)))
2687 (defun vip-erase-line (arg)
2688 "Erase line."
2689 (interactive "P")
2690 (vip-beginning-of-line (cons arg ?d)))
2693 ;;; Moving around
2695 (defun vip-goto-line (arg)
2696 "Go to ARG's line. Without ARG go to end of buffer."
2697 (interactive "P")
2698 (let ((val (vip-P-val arg))
2699 (com (vip-getCom arg)))
2700 (vip-move-marker-locally 'vip-com-point (point))
2701 (vip-deactivate-mark)
2702 (push-mark nil t)
2703 (if (null val)
2704 (goto-char (point-max))
2705 (goto-char (point-min))
2706 (forward-line (1- val)))
2708 ;; positioning is done twice: before and after command execution
2709 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2710 (back-to-indentation)
2712 (if com (vip-execute-com 'vip-goto-line val com))
2714 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2715 (back-to-indentation)
2718 ;; Find ARG's occurrence of CHAR on the current line.
2719 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
2720 ;; adjust point after search.
2721 (defun vip-find-char (arg char forward offset)
2722 (or (char-or-string-p char) (error ""))
2723 (let ((arg (if forward arg (- arg)))
2724 (cmd (if (eq vip-intermediate-command 'vip-repeat)
2725 (nth 5 vip-d-com)
2726 (vip-array-to-string (this-command-keys))))
2727 point)
2728 (save-excursion
2729 (save-restriction
2730 (if (> arg 0)
2731 (narrow-to-region
2732 ;; forward search begins here
2733 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
2734 ;; forward search ends here
2735 (progn (end-of-line) (point)))
2736 (narrow-to-region
2737 ;; backward search begins from here
2738 (if (bolp)
2739 (error "Command `%s': At beginning of line" cmd) (point))
2740 ;; backward search ends here
2741 (progn (beginning-of-line) (point))))
2742 ;; if arg > 0, point is forwarded before search.
2743 (if (> arg 0) (goto-char (1+ (point-min)))
2744 (goto-char (point-max)))
2745 (if (let ((case-fold-search nil))
2746 (search-forward (char-to-string char) nil 0 arg))
2747 (setq point (point))
2748 (error "Command `%s': `%c' not found" cmd char))))
2749 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
2751 (defun vip-find-char-forward (arg)
2752 "Find char on the line.
2753 If called interactively read the char to find from the terminal, and if
2754 called from vip-repeat, the char last used is used. This behaviour is
2755 controlled by the sign of prefix numeric value."
2756 (interactive "P")
2757 (let ((val (vip-p-val arg))
2758 (com (vip-getcom arg))
2759 (cmd-representation (nth 5 vip-d-com)))
2760 (if (> val 0)
2761 ;; this means that the function was called interactively
2762 (setq vip-f-char (read-char)
2763 vip-f-forward t
2764 vip-f-offset nil)
2765 ;; vip-repeat --- set vip-F-char from command-keys
2766 (setq vip-F-char (if (stringp cmd-representation)
2767 (vip-seq-last-elt cmd-representation)
2768 vip-F-char)
2769 vip-f-char vip-F-char)
2770 (setq val (- val)))
2771 (if com (vip-move-marker-locally 'vip-com-point (point)))
2772 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
2773 (setq val (- val))
2774 (if com
2775 (progn
2776 (setq vip-F-char vip-f-char) ; set new vip-F-char
2777 (forward-char)
2778 (vip-execute-com 'vip-find-char-forward val com)))))
2780 (defun vip-goto-char-forward (arg)
2781 "Go up to char ARG forward on line."
2782 (interactive "P")
2783 (let ((val (vip-p-val arg))
2784 (com (vip-getcom arg))
2785 (cmd-representation (nth 5 vip-d-com)))
2786 (if (> val 0)
2787 ;; this means that the function was called interactively
2788 (setq vip-f-char (read-char)
2789 vip-f-forward t
2790 vip-f-offset t)
2791 ;; vip-repeat --- set vip-F-char from command-keys
2792 (setq vip-F-char (if (stringp cmd-representation)
2793 (vip-seq-last-elt cmd-representation)
2794 vip-F-char)
2795 vip-f-char vip-F-char)
2796 (setq val (- val)))
2797 (if com (vip-move-marker-locally 'vip-com-point (point)))
2798 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
2799 (setq val (- val))
2800 (if com
2801 (progn
2802 (setq vip-F-char vip-f-char) ; set new vip-F-char
2803 (forward-char)
2804 (vip-execute-com 'vip-goto-char-forward val com)))))
2806 (defun vip-find-char-backward (arg)
2807 "Find char ARG on line backward."
2808 (interactive "P")
2809 (let ((val (vip-p-val arg))
2810 (com (vip-getcom arg))
2811 (cmd-representation (nth 5 vip-d-com)))
2812 (if (> val 0)
2813 ;; this means that the function was called interactively
2814 (setq vip-f-char (read-char)
2815 vip-f-forward nil
2816 vip-f-offset nil)
2817 ;; vip-repeat --- set vip-F-char from command-keys
2818 (setq vip-F-char (if (stringp cmd-representation)
2819 (vip-seq-last-elt cmd-representation)
2820 vip-F-char)
2821 vip-f-char vip-F-char)
2822 (setq val (- val)))
2823 (if com (vip-move-marker-locally 'vip-com-point (point)))
2824 (vip-find-char
2825 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
2826 (setq val (- val))
2827 (if com
2828 (progn
2829 (setq vip-F-char vip-f-char) ; set new vip-F-char
2830 (vip-execute-com 'vip-find-char-backward val com)))))
2832 (defun vip-goto-char-backward (arg)
2833 "Go up to char ARG backward on line."
2834 (interactive "P")
2835 (let ((val (vip-p-val arg))
2836 (com (vip-getcom arg))
2837 (cmd-representation (nth 5 vip-d-com)))
2838 (if (> val 0)
2839 ;; this means that the function was called interactively
2840 (setq vip-f-char (read-char)
2841 vip-f-forward nil
2842 vip-f-offset t)
2843 ;; vip-repeat --- set vip-F-char from command-keys
2844 (setq vip-F-char (if (stringp cmd-representation)
2845 (vip-seq-last-elt cmd-representation)
2846 vip-F-char)
2847 vip-f-char vip-F-char)
2848 (setq val (- val)))
2849 (if com (vip-move-marker-locally 'vip-com-point (point)))
2850 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
2851 (setq val (- val))
2852 (if com
2853 (progn
2854 (setq vip-F-char vip-f-char) ; set new vip-F-char
2855 (vip-execute-com 'vip-goto-char-backward val com)))))
2857 (defun vip-repeat-find (arg)
2858 "Repeat previous find command."
2859 (interactive "P")
2860 (let ((val (vip-p-val arg))
2861 (com (vip-getcom arg)))
2862 (vip-deactivate-mark)
2863 (if com (vip-move-marker-locally 'vip-com-point (point)))
2864 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
2865 (if com
2866 (progn
2867 (if vip-f-forward (forward-char))
2868 (vip-execute-com 'vip-repeat-find val com)))))
2870 (defun vip-repeat-find-opposite (arg)
2871 "Repeat previous find command in the opposite direction."
2872 (interactive "P")
2873 (let ((val (vip-p-val arg))
2874 (com (vip-getcom arg)))
2875 (vip-deactivate-mark)
2876 (if com (vip-move-marker-locally 'vip-com-point (point)))
2877 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
2878 (if com
2879 (progn
2880 (if vip-f-forward (forward-char))
2881 (vip-execute-com 'vip-repeat-find-opposite val com)))))
2884 ;; window scrolling etc.
2886 (defun vip-other-window (arg)
2887 "Switch to other window."
2888 (interactive "p")
2889 (other-window arg)
2890 (or (not (eq vip-current-state 'emacs-state))
2891 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
2892 (vip-change-state-to-vi)))
2894 (defun vip-window-top (arg)
2895 "Go to home window line."
2896 (interactive "P")
2897 (let ((val (vip-p-val arg))
2898 (com (vip-getCom arg)))
2899 (if com (vip-move-marker-locally 'vip-com-point (point)))
2900 (push-mark nil t)
2901 (move-to-window-line (1- val))
2903 ;; positioning is done twice: before and after command execution
2904 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2905 (back-to-indentation)
2907 (if com (vip-execute-com 'vip-window-top val com))
2909 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2910 (back-to-indentation)
2913 (defun vip-window-middle (arg)
2914 "Go to middle window line."
2915 (interactive "P")
2916 (let ((val (vip-p-val arg))
2917 (com (vip-getCom arg))
2918 lines)
2919 (if com (vip-move-marker-locally 'vip-com-point (point)))
2920 (push-mark nil t)
2921 (if (not (pos-visible-in-window-p (point-max)))
2922 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
2923 (setq lines (count-lines (window-start) (point-max)))
2924 (move-to-window-line (+ (/ lines 2) (1- val))))
2926 ;; positioning is done twice: before and after command execution
2927 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2928 (back-to-indentation)
2930 (if com (vip-execute-com 'vip-window-middle val com))
2932 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2933 (back-to-indentation)
2936 (defun vip-window-bottom (arg)
2937 "Go to last window line."
2938 (interactive "P")
2939 (let ((val (vip-p-val arg))
2940 (com (vip-getCom arg)))
2941 (if com (vip-move-marker-locally 'vip-com-point (point)))
2942 (push-mark nil t)
2943 (move-to-window-line (- val))
2945 ;; positioning is done twice: before and after command execution
2946 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2947 (back-to-indentation)
2949 (if com (vip-execute-com 'vip-window-bottom val com))
2951 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2952 (back-to-indentation)
2955 (defun vip-line-to-top (arg)
2956 "Put current line on the home line."
2957 (interactive "p")
2958 (recenter (1- arg)))
2960 (defun vip-line-to-middle (arg)
2961 "Put current line on the middle line."
2962 (interactive "p")
2963 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
2965 (defun vip-line-to-bottom (arg)
2966 "Put current line on the last line."
2967 (interactive "p")
2968 (recenter (- (window-height) (1+ arg))))
2970 ;; If point is within vip-search-scroll-threshold of window top or bottom,
2971 ;; scroll up or down 1/7 of window height, depending on whether we are at the
2972 ;; bottom or at the top of the window. This function is called by vip-search
2973 ;; (which is called from vip-search-forward/backward/next). If the value of
2974 ;; vip-search-scroll-threshold is negative - don't scroll.
2975 (defun vip-adjust-window ()
2976 (let ((win-height (if vip-emacs-p
2977 (1- (window-height)) ; adjust for modeline
2978 (window-displayed-height)))
2979 (pt (point))
2980 at-top-p at-bottom-p
2981 min-scroll direction)
2982 (save-excursion
2983 (move-to-window-line 0) ; top
2984 (setq at-top-p
2985 (<= (count-lines pt (point))
2986 vip-search-scroll-threshold))
2987 (move-to-window-line -1) ; bottom
2988 (setq at-bottom-p
2989 (<= (count-lines pt (point)) vip-search-scroll-threshold))
2991 (cond (at-top-p (setq min-scroll (1- vip-search-scroll-threshold)
2992 direction 1))
2993 (at-bottom-p (setq min-scroll (1+ vip-search-scroll-threshold)
2994 direction -1)))
2995 (if min-scroll
2996 (recenter
2997 (* (max min-scroll (/ win-height 7)) direction)))
3001 ;; paren match
3002 ;; must correct this to only match ( to ) etc. On the other hand
3003 ;; it is good that paren match gets confused, because that way you
3004 ;; catch _all_ imbalances.
3006 (defun vip-paren-match (arg)
3007 "Go to the matching parenthesis."
3008 (interactive "P")
3009 (vip-leave-region-active)
3010 (let ((com (vip-getcom arg))
3011 (parse-sexp-ignore-comments vip-parse-sexp-ignore-comments)
3012 anchor-point)
3013 (if (integerp arg)
3014 (if (or (> arg 99) (< arg 1))
3015 (error "Prefix must be between 1 and 99")
3016 (goto-char
3017 (if (> (point-max) 80000)
3018 (* (/ (point-max) 100) arg)
3019 (/ (* (point-max) arg) 100)))
3020 (back-to-indentation))
3021 (let (beg-lim end-lim)
3022 (if (and (eolp) (not (bolp))) (forward-char -1))
3023 (if (not (looking-at "[][(){}]"))
3024 (setq anchor-point (point)))
3025 (save-excursion
3026 (beginning-of-line)
3027 (setq beg-lim (point))
3028 (end-of-line)
3029 (setq end-lim (point)))
3030 (cond ((re-search-forward "[][(){}]" end-lim t)
3031 (backward-char) )
3032 ((re-search-backward "[][(){}]" beg-lim t))
3034 (error "No matching character on line"))))
3035 (cond ((looking-at "[\(\[{]")
3036 (if com (vip-move-marker-locally 'vip-com-point (point)))
3037 (forward-sexp 1)
3038 (if com
3039 (vip-execute-com 'vip-paren-match nil com)
3040 (backward-char)))
3041 (anchor-point
3042 (if com
3043 (progn
3044 (vip-move-marker-locally 'vip-com-point anchor-point)
3045 (forward-char 1)
3046 (vip-execute-com 'vip-paren-match nil com)
3048 ((looking-at "[])}]")
3049 (forward-char)
3050 (if com (vip-move-marker-locally 'vip-com-point (point)))
3051 (backward-sexp 1)
3052 (if com (vip-execute-com 'vip-paren-match nil com)))
3053 (t (error ""))))))
3055 (defun vip-toggle-parse-sexp-ignore-comments ()
3056 (interactive)
3057 (setq vip-parse-sexp-ignore-comments (not vip-parse-sexp-ignore-comments))
3058 (prin1 (format "`%%' will %signore parentheses inside the comments"
3059 (if vip-parse-sexp-ignore-comments "" "NOT ")))
3063 ;; sentence ,paragraph and heading
3065 (defun vip-forward-sentence (arg)
3066 "Forward sentence."
3067 (interactive "P")
3068 (push-mark nil t)
3069 (let ((val (vip-p-val arg))
3070 (com (vip-getcom arg)))
3071 (if com (vip-move-marker-locally 'vip-com-point (point)))
3072 (forward-sentence val)
3073 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3075 (defun vip-backward-sentence (arg)
3076 "Backward sentence."
3077 (interactive "P")
3078 (push-mark nil t)
3079 (let ((val (vip-p-val arg))
3080 (com (vip-getcom arg)))
3081 (if com (vip-move-marker-locally 'vip-com-point (point)))
3082 (backward-sentence val)
3083 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3085 (defun vip-forward-paragraph (arg)
3086 "Forward paragraph."
3087 (interactive "P")
3088 (push-mark nil t)
3089 (let ((val (vip-p-val arg))
3090 (com (vip-getCom arg)))
3091 (if com (vip-move-marker-locally 'vip-com-point (point)))
3092 (forward-paragraph val)
3093 (if com
3094 (progn
3095 (backward-char 1)
3096 (vip-execute-com 'vip-forward-paragraph nil com)))))
3098 (defun vip-backward-paragraph (arg)
3099 "Backward paragraph."
3100 (interactive "P")
3101 (push-mark nil t)
3102 (let ((val (vip-p-val arg))
3103 (com (vip-getCom arg)))
3104 (if com (vip-move-marker-locally 'vip-com-point (point)))
3105 (backward-paragraph val)
3106 (if com
3107 (progn
3108 (forward-char 1)
3109 (vip-execute-com 'vip-backward-paragraph nil com)
3110 (backward-char 1)))))
3112 ;; should be mode-specific etc.
3114 (defun vip-prev-heading (arg)
3115 (interactive "P")
3116 (let ((val (vip-p-val arg))
3117 (com (vip-getCom arg)))
3118 (if com (vip-move-marker-locally 'vip-com-point (point)))
3119 (re-search-backward vip-heading-start nil t val)
3120 (goto-char (match-beginning 0))
3121 (if com (vip-execute-com 'vip-prev-heading nil com))))
3123 (defun vip-heading-end (arg)
3124 (interactive "P")
3125 (let ((val (vip-p-val arg))
3126 (com (vip-getCom arg)))
3127 (if com (vip-move-marker-locally 'vip-com-point (point)))
3128 (re-search-forward vip-heading-end nil t val)
3129 (goto-char (match-beginning 0))
3130 (if com (vip-execute-com 'vip-heading-end nil com))))
3132 (defun vip-next-heading (arg)
3133 (interactive "P")
3134 (let ((val (vip-p-val arg))
3135 (com (vip-getCom arg)))
3136 (if com (vip-move-marker-locally 'vip-com-point (point)))
3137 (end-of-line)
3138 (re-search-forward vip-heading-start nil t val)
3139 (goto-char (match-beginning 0))
3140 (if com (vip-execute-com 'vip-next-heading nil com))))
3143 ;; scrolling
3145 (defun vip-scroll-screen (arg)
3146 "Scroll to next screen."
3147 (interactive "p")
3148 (condition-case nil
3149 (if (> arg 0)
3150 (while (> arg 0)
3151 (scroll-up)
3152 (setq arg (1- arg)))
3153 (while (> 0 arg)
3154 (scroll-down)
3155 (setq arg (1+ arg))))
3156 (error (beep 1)
3157 (if (> arg 0)
3158 (progn
3159 (message "End of buffer")
3160 (goto-char (point-max)))
3161 (message "Beginning of buffer")
3162 (goto-char (point-min))))
3165 (defun vip-scroll-screen-back (arg)
3166 "Scroll to previous screen."
3167 (interactive "p")
3168 (vip-scroll-screen (- arg)))
3170 (defun vip-scroll-down (arg)
3171 "Pull down half screen."
3172 (interactive "P")
3173 (condition-case nil
3174 (if (null arg)
3175 (scroll-down (/ (window-height) 2))
3176 (scroll-down arg))
3177 (error (beep 1)
3178 (message "Beginning of buffer")
3179 (goto-char (point-min)))))
3181 (defun vip-scroll-down-one (arg)
3182 "Scroll up one line."
3183 (interactive "p")
3184 (scroll-down arg))
3186 (defun vip-scroll-up (arg)
3187 "Pull up half screen."
3188 (interactive "P")
3189 (condition-case nil
3190 (if (null arg)
3191 (scroll-up (/ (window-height) 2))
3192 (scroll-up arg))
3193 (error (beep 1)
3194 (message "End of buffer")
3195 (goto-char (point-max)))))
3197 (defun vip-scroll-up-one (arg)
3198 "Scroll down one line."
3199 (interactive "p")
3200 (scroll-up arg))
3203 ;; searching
3205 (defun vip-if-string (prompt)
3206 (let ((s (vip-read-string-with-history
3207 prompt
3208 nil ; no initial
3209 'vip-search-history
3210 (car vip-search-history))))
3211 (if (not (string= s ""))
3212 (setq vip-s-string s))))
3215 (defun vip-toggle-search-style (arg)
3216 "Toggle the value of vip-case-fold-search/vip-re-search.
3217 Without prefix argument, will ask which search style to toggle. With prefix
3218 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
3220 Although this function is bound to \\[vip-toggle-search-style], the most
3221 convenient way to use it is to bind `//' to the macro
3222 `1 M-x vip-toggle-search-style' and `///' to
3223 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
3224 toggle case-fold-search and hitting `/' three times witth toggle regexp
3225 search. Macros are more convenient in this case because they don't affect
3226 the Emacs binding of `/'."
3227 (interactive "P")
3228 (let (msg)
3229 (cond ((or (eq arg 1)
3230 (and (null arg)
3231 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3232 (if vip-case-fold-search
3233 "case-insensitive" "case-sensitive")
3234 (if vip-case-fold-search
3235 "case-sensitive"
3236 "case-insensitive")))))
3237 (setq vip-case-fold-search (null vip-case-fold-search))
3238 (if vip-case-fold-search
3239 (setq msg "Search becomes case-insensitive")
3240 (setq msg "Search becomes case-sensitive")))
3241 ((or (eq arg 2)
3242 (and (null arg)
3243 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3244 (if vip-re-search
3245 "regexp-search" "vanilla-search")
3246 (if vip-re-search
3247 "vanilla-search"
3248 "regexp-search")))))
3249 (setq vip-re-search (null vip-re-search))
3250 (if vip-re-search
3251 (setq msg "Search becomes regexp-style")
3252 (setq msg "Search becomes vanilla-style")))
3254 (setq msg "Search style remains unchanged")))
3255 (prin1 msg t)))
3257 (defun vip-set-vi-search-style-macros (unset)
3258 "Set the macros for toggling the search style in Viper's vi-state.
3259 The macro that toggles case sensitivity is bound to `//', and the one that
3260 toggles regexp search is bound to `///'.
3261 With a prefix argument, this function unsets the macros. "
3262 (interactive "P")
3263 (or noninteractive
3264 (if (not unset)
3265 (progn
3266 ;; toggle case sensitivity in search
3267 (vip-record-kbd-macro
3268 "//" 'vi-state
3269 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3271 ;; toggle regexp/vanila search
3272 (vip-record-kbd-macro
3273 "///" 'vi-state
3274 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3276 (if (interactive-p)
3277 (message
3278 "// and /// now toggle case-sensitivity and regexp search.")))
3279 (vip-unrecord-kbd-macro "//" 'vi-state)
3280 (sit-for 2)
3281 (vip-unrecord-kbd-macro "///" 'vi-state))))
3283 (defun vip-set-emacs-search-style-macros (unset &optional arg-majormode)
3284 "Set the macros for toggling the search style in Viper's emacs-state.
3285 The macro that toggles case sensitivity is bound to `//', and the one that
3286 toggles regexp search is bound to `///'.
3287 With a prefix argument, this function unsets the macros.
3288 If the optional prefix argument is non-nil and specifies a valid major mode,
3289 this sets the macros only in the macros in that major mode. Otherwise,
3290 the macros are set in the current major mode.
3291 \(When unsetting the macros, the second argument has no effect.\)"
3292 (interactive "P")
3293 (or noninteractive
3294 (if (not unset)
3295 (progn
3296 ;; toggle case sensitivity in search
3297 (vip-record-kbd-macro
3298 "//" 'emacs-state
3299 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3300 (or arg-majormode major-mode))
3301 ;; toggle regexp/vanila search
3302 (vip-record-kbd-macro
3303 "///" 'emacs-state
3304 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3305 (or arg-majormode major-mode))
3306 (if (interactive-p)
3307 (message
3308 "// and /// now toggle case-sensitivity and regexp search.")))
3309 (vip-unrecord-kbd-macro "//" 'emacs-state)
3310 (sit-for 2)
3311 (vip-unrecord-kbd-macro "///" 'emacs-state))))
3314 (defun vip-search-forward (arg)
3315 "Search a string forward.
3316 ARG is used to find the ARG's occurrence of the string.
3317 Null string will repeat previous search."
3318 (interactive "P")
3319 (let ((val (vip-P-val arg))
3320 (com (vip-getcom arg))
3321 (old-str vip-s-string))
3322 (setq vip-s-forward t)
3323 (vip-if-string "/")
3324 ;; this is not used at present, but may be used later
3325 (if (or (not (equal old-str vip-s-string))
3326 (not (markerp vip-local-search-start-marker))
3327 (not (marker-buffer vip-local-search-start-marker)))
3328 (setq vip-local-search-start-marker (point-marker)))
3329 (vip-search vip-s-string t val)
3330 (if com
3331 (progn
3332 (vip-move-marker-locally 'vip-com-point (mark t))
3333 (vip-execute-com 'vip-search-next val com)))))
3335 (defun vip-search-backward (arg)
3336 "Search a string backward.
3337 ARG is used to find the ARG's occurrence of the string.
3338 Null string will repeat previous search."
3339 (interactive "P")
3340 (let ((val (vip-P-val arg))
3341 (com (vip-getcom arg))
3342 (old-str vip-s-string))
3343 (setq vip-s-forward nil)
3344 (vip-if-string "?")
3345 ;; this is not used at present, but may be used later
3346 (if (or (not (equal old-str vip-s-string))
3347 (not (markerp vip-local-search-start-marker))
3348 (not (marker-buffer vip-local-search-start-marker)))
3349 (setq vip-local-search-start-marker (point-marker)))
3350 (vip-search vip-s-string nil val)
3351 (if com
3352 (progn
3353 (vip-move-marker-locally 'vip-com-point (mark t))
3354 (vip-execute-com 'vip-search-next val com)))))
3357 ;; Search for COUNT's occurrence of STRING.
3358 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3359 ;; INIT-POINT is the position where search is to start.
3360 ;; Arguments:
3361 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3362 (defun vip-search (string forward arg
3363 &optional no-offset init-point fail-if-not-found)
3364 (if (not (equal string ""))
3365 (let ((val (vip-p-val arg))
3366 (com (vip-getcom arg))
3367 (offset (not no-offset))
3368 (case-fold-search vip-case-fold-search)
3369 (start-point (or init-point (point))))
3370 (vip-deactivate-mark)
3371 (if forward
3372 (condition-case nil
3373 (progn
3374 (if offset (vip-forward-char-carefully))
3375 (if vip-re-search
3376 (progn
3377 (re-search-forward string nil nil val)
3378 (re-search-backward string))
3379 (search-forward string nil nil val)
3380 (search-backward string))
3381 (if (not (equal start-point (point)))
3382 (push-mark start-point t)))
3383 (search-failed
3384 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
3385 (progn
3386 (message "Search wrapped around BOTTOM of buffer")
3387 (goto-char (point-min))
3388 (vip-search string forward (cons 1 com) t start-point 'fail)
3389 ;; don't wait in macros
3390 (or executing-kbd-macro (sit-for 2))
3391 ;; delete the wrap-around message
3392 (message "")
3394 (goto-char start-point)
3395 (error "`%s': %s not found"
3396 string
3397 (if vip-re-search "Pattern" "String"))
3399 ;; backward
3400 (condition-case nil
3401 (progn
3402 (if vip-re-search
3403 (re-search-backward string nil nil val)
3404 (search-backward string nil nil val))
3405 (if (not (equal start-point (point)))
3406 (push-mark start-point t)))
3407 (search-failed
3408 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
3409 (progn
3410 (message "Search wrapped around TOP of buffer")
3411 (goto-char (point-max))
3412 (vip-search string forward (cons 1 com) t start-point 'fail)
3413 ;; don't wait in macros
3414 (or executing-kbd-macro (sit-for 2))
3415 ;; delete the wrap-around message
3416 (message "")
3418 (goto-char start-point)
3419 (error "`%s': %s not found"
3420 string
3421 (if vip-re-search "Pattern" "String"))
3422 ))))
3423 ;; pull up or down if at top/bottom of window
3424 (vip-adjust-window)
3425 ;; highlight the result of search
3426 ;; don't wait and don't highlight in macros
3427 (or executing-kbd-macro
3428 vip-inside-command-argument-action
3429 (vip-flash-search-pattern))
3432 (defun vip-search-next (arg)
3433 "Repeat previous search."
3434 (interactive "P")
3435 (let ((val (vip-p-val arg))
3436 (com (vip-getcom arg)))
3437 (if (null vip-s-string) (error vip-NoPrevSearch))
3438 (vip-search vip-s-string vip-s-forward arg)
3439 (if com
3440 (progn
3441 (vip-move-marker-locally 'vip-com-point (mark t))
3442 (vip-execute-com 'vip-search-next val com)))))
3444 (defun vip-search-Next (arg)
3445 "Repeat previous search in the reverse direction."
3446 (interactive "P")
3447 (let ((val (vip-p-val arg))
3448 (com (vip-getcom arg)))
3449 (if (null vip-s-string) (error vip-NoPrevSearch))
3450 (vip-search vip-s-string (not vip-s-forward) arg)
3451 (if com
3452 (progn
3453 (vip-move-marker-locally 'vip-com-point (mark t))
3454 (vip-execute-com 'vip-search-Next val com)))))
3457 ;; Search contents of buffer defined by one of Viper's motion commands.
3458 ;; Repeatable via `n' and `N'.
3459 (defun vip-buffer-search-enable (&optional c)
3460 (cond (c (setq vip-buffer-search-char c))
3461 ((null vip-buffer-search-char)
3462 (setq vip-buffer-search-char ?g)))
3463 (define-key vip-vi-basic-map
3464 (char-to-string vip-buffer-search-char) 'vip-command-argument)
3465 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
3466 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
3468 ;; This is a Viper wraper for isearch-forward.
3469 (defun vip-isearch-forward (arg)
3470 "Do incremental search forward."
3471 (interactive "P")
3472 ;; emacs bug workaround
3473 (if (listp arg) (setq arg (car arg)))
3474 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
3476 ;; This is a Viper wraper for isearch-backward."
3477 (defun vip-isearch-backward (arg)
3478 "Do incremental search backward."
3479 (interactive "P")
3480 ;; emacs bug workaround
3481 (if (listp arg) (setq arg (car arg)))
3482 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
3485 ;; visiting and killing files, buffers
3487 (defun vip-switch-to-buffer ()
3488 "Switch to buffer in the current window."
3489 (interactive)
3490 (let (buffer)
3491 (setq buffer
3492 (read-buffer
3493 (format "Switch to buffer in this window \(%s\): "
3494 (buffer-name (other-buffer (current-buffer))))))
3495 (switch-to-buffer buffer)
3498 (defun vip-switch-to-buffer-other-window ()
3499 "Switch to buffer in another window."
3500 (interactive)
3501 (let (buffer)
3502 (setq buffer
3503 (read-buffer
3504 (format "Switch to buffer in another window \(%s\): "
3505 (buffer-name (other-buffer (current-buffer))))))
3506 (switch-to-buffer-other-window buffer)
3509 (defun vip-kill-buffer ()
3510 "Kill a buffer."
3511 (interactive)
3512 (let (buffer buffer-name)
3513 (setq buffer-name
3514 (read-buffer
3515 (format "Kill buffer \(%s\): "
3516 (buffer-name (current-buffer)))))
3517 (setq buffer
3518 (if (null buffer-name)
3519 (current-buffer)
3520 (get-buffer buffer-name)))
3521 (if (null buffer) (error "`%s': No such buffer" buffer-name))
3522 (if (or (not (buffer-modified-p buffer))
3523 (y-or-n-p
3524 (format
3525 "Buffer `%s' is modified, are you sure you want to kill it? "
3526 buffer-name)))
3527 (kill-buffer buffer)
3528 (error "Buffer not killed"))))
3531 (defvar vip-smart-suffix-list
3532 '("" "tex" "c" "cc" "C" "el" "java" "html" "htm" "pl" "P" "p")
3533 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
3534 This is useful when you the current directory contains files with the same
3535 prefix and many different suffixes. Usually, only one of the suffixes
3536 represents an editable file. However, file completion will stop at the `.'
3537 The smart suffix feature lets you hit RET in such a case, and Viper will
3538 select the appropriate suffix.
3540 Suffixes are tried in the order given and the first suffix for which a
3541 corresponding file exists is selected. If no file exists for any of the
3542 suffixes, the user is asked to confirm.
3544 To turn this feature off, set this variable to nil.")
3546 ;; Try to add suffix to files ending with a `.'
3547 ;; Useful when the user hits RET on a non-completed file name.
3548 (defun vip-file-add-suffix ()
3549 (let ((count 0)
3550 (len (length vip-smart-suffix-list))
3551 (file (buffer-string))
3552 found key cmd suff)
3553 (goto-char (point-max))
3554 (if (and vip-smart-suffix-list (string-match "\\.$" file))
3555 (progn
3556 (while (and (not found) (< count len))
3557 (setq suff (nth count vip-smart-suffix-list)
3558 count (1+ count))
3559 (if (file-exists-p (format "%s%s" file suff))
3560 (progn
3561 (setq found t)
3562 (insert suff))))
3564 (if found
3566 (vip-tmp-insert-at-eob " [Please complete file name]")
3567 (unwind-protect
3568 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
3569 (setq cmd
3570 (key-binding (setq key (read-key-sequence nil))))
3571 (cond ((eq cmd 'self-insert-command)
3572 (if vip-xemacs-p
3573 (insert (events-to-keys key))
3574 (insert key)))
3575 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
3576 nil)
3577 (t (command-execute cmd)))
3585 ;; yank and pop
3587 (defsubst vip-yank (text)
3588 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
3589 (insert text)
3590 (setq this-command 'yank))
3592 (defun vip-put-back (arg)
3593 "Put back after point/below line."
3594 (interactive "P")
3595 (let ((val (vip-p-val arg))
3596 (text (if vip-use-register
3597 (cond ((vip-valid-register vip-use-register '(digit))
3598 (current-kill (- vip-use-register ?1) 'do-not-rotate))
3599 ((vip-valid-register vip-use-register)
3600 (get-register (downcase vip-use-register)))
3601 (t (error vip-InvalidRegister vip-use-register)))
3602 (current-kill 0))))
3603 (if (null text)
3604 (if vip-use-register
3605 (let ((reg vip-use-register))
3606 (setq vip-use-register nil)
3607 (error vip-EmptyRegister reg))
3608 (error "")))
3609 (setq vip-use-register nil)
3610 (if (vip-end-with-a-newline-p text)
3611 (progn
3612 (end-of-line)
3613 (if (eobp)
3614 (insert "\n")
3615 (forward-line 1))
3616 (beginning-of-line))
3617 (if (not (eolp)) (vip-forward-char-carefully)))
3618 (set-marker (vip-mark-marker) (point) (current-buffer))
3619 (vip-set-destructive-command
3620 (list 'vip-put-back val nil vip-use-register nil nil))
3621 (vip-loop val (vip-yank text)))
3622 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3623 ;; newline; it leaves the cursor at the beginning when the text contains
3624 ;; a newline
3625 (if (vip-same-line (point) (mark))
3626 (or (= (point) (mark)) (vip-backward-char-carefully))
3627 (exchange-point-and-mark)
3628 (if (bolp)
3629 (back-to-indentation)))
3630 (vip-deactivate-mark))
3632 (defun vip-Put-back (arg)
3633 "Put back at point/above line."
3634 (interactive "P")
3635 (let ((val (vip-p-val arg))
3636 (text (if vip-use-register
3637 (cond ((vip-valid-register vip-use-register '(digit))
3638 (current-kill (- vip-use-register ?1) 'do-not-rotate))
3639 ((vip-valid-register vip-use-register)
3640 (get-register (downcase vip-use-register)))
3641 (t (error vip-InvalidRegister vip-use-register)))
3642 (current-kill 0))))
3643 (if (null text)
3644 (if vip-use-register
3645 (let ((reg vip-use-register))
3646 (setq vip-use-register nil)
3647 (error vip-EmptyRegister reg))
3648 (error "")))
3649 (setq vip-use-register nil)
3650 (if (vip-end-with-a-newline-p text) (beginning-of-line))
3651 (vip-set-destructive-command
3652 (list 'vip-Put-back val nil vip-use-register nil nil))
3653 (set-marker (vip-mark-marker) (point) (current-buffer))
3654 (vip-loop val (vip-yank text)))
3655 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3656 ;; newline; it leaves the cursor at the beginning when the text contains
3657 ;; a newline
3658 (if (vip-same-line (point) (mark))
3659 (or (= (point) (mark)) (vip-backward-char-carefully))
3660 (exchange-point-and-mark)
3661 (if (bolp)
3662 (back-to-indentation)))
3663 (vip-deactivate-mark))
3666 ;; Copy region to kill-ring.
3667 ;; If BEG and END do not belong to the same buffer, copy empty region.
3668 (defun vip-copy-region-as-kill (beg end)
3669 (condition-case nil
3670 (copy-region-as-kill beg end)
3671 (error (copy-region-as-kill beg beg))))
3674 (defun vip-delete-char (arg)
3675 "Delete character."
3676 (interactive "P")
3677 (let ((val (vip-p-val arg)))
3678 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
3679 (if (> val 1)
3680 (save-excursion
3681 (let ((here (point)))
3682 (end-of-line)
3683 (if (> val (- (point) here))
3684 (setq val (- (point) here))))))
3685 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
3686 (if (and vip-ex-style-motion (eolp))
3687 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
3688 (if vip-use-register
3689 (progn
3690 (cond ((vip-valid-register vip-use-register '((Letter)))
3691 (vip-append-to-register
3692 (downcase vip-use-register) (point) (- (point) val)))
3693 ((vip-valid-register vip-use-register)
3694 (copy-to-register
3695 vip-use-register (point) (- (point) val) nil))
3696 (t (error vip-InvalidRegister vip-use-register)))
3697 (setq vip-use-register nil)))
3698 (if vip-ex-style-motion
3699 (progn
3700 (delete-char val t)
3701 (if (and (eolp) (not (bolp))) (backward-char 1)))
3702 (if (eolp)
3703 (delete-backward-char val t)
3704 (delete-char val t)))))
3706 (defun vip-delete-backward-char (arg)
3707 "Delete previous character. On reaching beginning of line, stop and beep."
3708 (interactive "P")
3709 (let ((val (vip-p-val arg)))
3710 (vip-set-destructive-command
3711 (list 'vip-delete-backward-char val nil nil nil nil))
3712 (if (> val 1)
3713 (save-excursion
3714 (let ((here (point)))
3715 (beginning-of-line)
3716 (if (> val (- here (point)))
3717 (setq val (- here (point)))))))
3718 (if vip-use-register
3719 (progn
3720 (cond ((vip-valid-register vip-use-register '(Letter))
3721 (vip-append-to-register
3722 (downcase vip-use-register) (point) (+ (point) val)))
3723 ((vip-valid-register vip-use-register)
3724 (copy-to-register
3725 vip-use-register (point) (+ (point) val) nil))
3726 (t (error vip-InvalidRegister vip-use-register)))
3727 (setq vip-use-register nil)))
3728 (if (bolp) (ding)
3729 (delete-backward-char val t))))
3731 (defun vip-del-backward-char-in-insert ()
3732 "Delete 1 char backwards while in insert mode."
3733 (interactive)
3734 (if (and vip-ex-style-editing-in-insert (bolp))
3735 (beep 1)
3736 (delete-backward-char 1 t)))
3738 (defun vip-del-backward-char-in-replace ()
3739 "Delete one character in replace mode.
3740 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
3741 charecters. If it is nil, then the cursor just moves backwards, similarly
3742 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
3743 cursor move past the beginning of line."
3744 (interactive)
3745 (cond (vip-delete-backwards-in-replace
3746 (cond ((not (bolp))
3747 (delete-backward-char 1 t))
3748 (vip-ex-style-editing-in-insert
3749 (beep 1))
3750 ((bobp)
3751 (beep 1))
3753 (delete-backward-char 1 t))))
3754 (vip-ex-style-editing-in-insert
3755 (if (bolp)
3756 (beep 1)
3757 (backward-char 1)))
3759 (backward-char 1))))
3763 ;; join lines.
3765 (defun vip-join-lines (arg)
3766 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
3767 (interactive "*P")
3768 (let ((val (vip-P-val arg)))
3769 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
3770 (vip-loop (if (null val) 1 (1- val))
3771 (progn
3772 (end-of-line)
3773 (if (not (eobp))
3774 (progn
3775 (forward-line 1)
3776 (delete-region (point) (1- (point)))
3777 (fixup-whitespace)))))))
3780 ;; Replace state
3782 (defun vip-change (beg end)
3783 (if (markerp beg) (setq beg (marker-position beg)))
3784 (if (markerp end) (setq end (marker-position end)))
3785 ;; beg is sometimes (mark t), which may be nil
3786 (or beg (setq beg end))
3788 (vip-set-complex-command-for-undo)
3789 (if vip-use-register
3790 (progn
3791 (copy-to-register vip-use-register beg end nil)
3792 (setq vip-use-register nil)))
3793 (vip-set-replace-overlay beg end)
3794 (setq last-command nil) ; separate repl text from prev kills
3796 (if (= (vip-replace-start) (point-max))
3797 (error "End of buffer"))
3799 (setq vip-last-replace-region
3800 (buffer-substring (vip-replace-start)
3801 (vip-replace-end)))
3803 ;; protect against error while inserting "@" and other disasters
3804 ;; (e.g., read-only buff)
3805 (condition-case conds
3806 (if (or vip-allow-multiline-replace-regions
3807 (vip-same-line (vip-replace-start)
3808 (vip-replace-end)))
3809 (progn
3810 ;; tabs cause problems in replace, so untabify
3811 (goto-char (vip-replace-end))
3812 (insert-before-markers "@") ; put placeholder after the TAB
3813 (untabify (vip-replace-start) (point))
3814 ;; del @, don't put on kill ring
3815 (delete-backward-char 1)
3817 (vip-set-replace-overlay-glyphs
3818 vip-replace-region-start-delimiter
3819 vip-replace-region-end-delimiter)
3820 ;; this move takes care of the last posn in the overlay, which
3821 ;; has to be shifted because of insert. We can't simply insert
3822 ;; "$" before-markers because then overlay-start will shift the
3823 ;; beginning of the overlay in case we are replacing a single
3824 ;; character. This fixes the bug with `s' and `cl' commands.
3825 (vip-move-replace-overlay (vip-replace-start) (point))
3826 (goto-char (vip-replace-start))
3827 (vip-change-state-to-replace t))
3828 (kill-region (vip-replace-start)
3829 (vip-replace-end))
3830 (vip-hide-replace-overlay)
3831 (vip-change-state-to-insert))
3832 (error ;; make sure that the overlay doesn't stay.
3833 ;; go back to the original point
3834 (goto-char (vip-replace-start))
3835 (vip-hide-replace-overlay)
3836 (vip-message-conditions conds))))
3839 (defun vip-change-subr (beg end)
3840 ;; beg is sometimes (mark t), which may be nil
3841 (or beg (setq beg end))
3843 (if vip-use-register
3844 (progn
3845 (copy-to-register vip-use-register beg end nil)
3846 (setq vip-use-register nil)))
3847 (kill-region beg end)
3848 (setq this-command 'vip-change)
3849 (vip-yank-last-insertion))
3851 (defun vip-toggle-case (arg)
3852 "Toggle character case."
3853 (interactive "P")
3854 (let ((val (vip-p-val arg)) (c))
3855 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
3856 (while (> val 0)
3857 (setq c (following-char))
3858 (delete-char 1 nil)
3859 (if (eq c (upcase c))
3860 (insert-char (downcase c) 1)
3861 (insert-char (upcase c) 1))
3862 (if (eolp) (backward-char 1))
3863 (setq val (1- val)))))
3866 ;; query replace
3868 (defun vip-query-replace ()
3869 "Query replace.
3870 If a null string is suplied as the string to be replaced,
3871 the query replace mode will toggle between string replace
3872 and regexp replace."
3873 (interactive)
3874 (let (str)
3875 (setq str (vip-read-string-with-history
3876 (if vip-re-query-replace "Query replace regexp: "
3877 "Query replace: ")
3878 nil ; no initial
3879 'vip-replace1-history
3880 (car vip-replace1-history) ; default
3882 (if (string= str "")
3883 (progn
3884 (setq vip-re-query-replace (not vip-re-query-replace))
3885 (message "Query replace mode changed to %s"
3886 (if vip-re-query-replace "regexp replace"
3887 "string replace")))
3888 (if vip-re-query-replace
3889 (query-replace-regexp
3891 (vip-read-string-with-history
3892 (format "Query replace regexp `%s' with: " str)
3893 nil ; no initial
3894 'vip-replace1-history
3895 (car vip-replace1-history) ; default
3897 (query-replace
3899 (vip-read-string-with-history
3900 (format "Query replace `%s' with: " str)
3901 nil ; no initial
3902 'vip-replace1-history
3903 (car vip-replace1-history) ; default
3904 ))))))
3907 ;; marking
3909 (defun vip-mark-beginning-of-buffer ()
3910 "Mark beginning of buffer."
3911 (interactive)
3912 (push-mark (point))
3913 (goto-char (point-min))
3914 (exchange-point-and-mark)
3915 (message "Mark set at the beginning of buffer"))
3917 (defun vip-mark-end-of-buffer ()
3918 "Mark end of buffer."
3919 (interactive)
3920 (push-mark (point))
3921 (goto-char (point-max))
3922 (exchange-point-and-mark)
3923 (message "Mark set at the end of buffer"))
3925 (defun vip-mark-point ()
3926 "Set mark at point of buffer."
3927 (interactive)
3928 (let ((char (read-char)))
3929 (cond ((and (<= ?a char) (<= char ?z))
3930 (point-to-register (1+ (- char ?a))))
3931 ((= char ?<) (vip-mark-beginning-of-buffer))
3932 ((= char ?>) (vip-mark-end-of-buffer))
3933 ((= char ?.) (vip-set-mark-if-necessary))
3934 ((= char ?,) (vip-cycle-through-mark-ring))
3935 ((= char ?D) (mark-defun))
3936 (t (error ""))
3939 ;; Algorithm: If first invocation of this command save mark on ring, goto
3940 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
3941 ;; making it into the new mark, M1.
3942 ;; Push this mark back and set mark to the original point position, p1.
3943 ;; So, if you hit '' or `` then you can return to p1.
3945 ;; If repeated command, pop top elt from the ring into mark and
3946 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
3947 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
3948 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
3949 ;; etc.
3950 (defun vip-cycle-through-mark-ring ()
3951 "Visit previous locations on the mark ring.
3952 One can use `` and '' to temporarily jump 1 step back."
3953 (let* ((sv-pt (point)))
3954 ;; if repeated `m,' command, pop the previously saved mark.
3955 ;; Prev saved mark is actually prev saved point. It is used if the
3956 ;; user types `` or '' and is discarded
3957 ;; from the mark ring by the next `m,' command.
3958 ;; In any case, go to the previous or previously saved mark.
3959 ;; Then push the current mark (popped off the ring) and set current
3960 ;; point to be the mark. Current pt as mark is discarded by the next
3961 ;; m, command.
3962 (if (eq last-command 'vip-cycle-through-mark-ring)
3964 ;; save current mark if the first iteration
3965 (setq mark-ring (delete (vip-mark-marker) mark-ring))
3966 (if (mark t)
3967 (push-mark (mark t) t)) )
3968 (pop-mark)
3969 (set-mark-command 1)
3970 ;; don't duplicate mark on the ring
3971 (setq mark-ring (delete (vip-mark-marker) mark-ring))
3972 (push-mark sv-pt t)
3973 (vip-deactivate-mark)
3974 (setq this-command 'vip-cycle-through-mark-ring)
3978 (defun vip-goto-mark (arg)
3979 "Go to mark."
3980 (interactive "P")
3981 (let ((char (read-char))
3982 (com (vip-getcom arg)))
3983 (vip-goto-mark-subr char com nil)))
3985 (defun vip-goto-mark-and-skip-white (arg)
3986 "Go to mark and skip to first non-white character on line."
3987 (interactive "P")
3988 (let ((char (read-char))
3989 (com (vip-getCom arg)))
3990 (vip-goto-mark-subr char com t)))
3992 (defun vip-goto-mark-subr (char com skip-white)
3993 (if (eobp)
3994 (if (bobp)
3995 (error "Empty buffer")
3996 (backward-char 1)))
3997 (cond ((vip-valid-register char '(letter))
3998 (let* ((buff (current-buffer))
3999 (reg (1+ (- char ?a)))
4000 (text-marker (get-register reg)))
4001 (if com (vip-move-marker-locally 'vip-com-point (point)))
4002 (if (not (vip-valid-marker text-marker))
4003 (error vip-EmptyTextmarker char))
4004 (if (and (vip-same-line (point) vip-last-jump)
4005 (= (point) vip-last-jump-ignore))
4006 (push-mark vip-last-jump t)
4007 (push-mark nil t)) ; no msg
4008 (vip-register-to-point reg)
4009 (setq vip-last-jump (point-marker))
4010 (cond (skip-white
4011 (back-to-indentation)
4012 (setq vip-last-jump-ignore (point))))
4013 (if com
4014 (if (equal buff (current-buffer))
4015 (vip-execute-com (if skip-white
4016 'vip-goto-mark-and-skip-white
4017 'vip-goto-mark)
4018 nil com)
4019 (switch-to-buffer buff)
4020 (goto-char vip-com-point)
4021 (vip-change-state-to-vi)
4022 (error "")))))
4023 ((and (not skip-white) (= char ?`))
4024 (if com (vip-move-marker-locally 'vip-com-point (point)))
4025 (if (and (vip-same-line (point) vip-last-jump)
4026 (= (point) vip-last-jump-ignore))
4027 (goto-char vip-last-jump))
4028 (if (null (mark t)) (error "Mark is not set in this buffer"))
4029 (if (= (point) (mark t)) (pop-mark))
4030 (exchange-point-and-mark)
4031 (setq vip-last-jump (point-marker)
4032 vip-last-jump-ignore 0)
4033 (if com (vip-execute-com 'vip-goto-mark nil com)))
4034 ((and skip-white (= char ?'))
4035 (if com (vip-move-marker-locally 'vip-com-point (point)))
4036 (if (and (vip-same-line (point) vip-last-jump)
4037 (= (point) vip-last-jump-ignore))
4038 (goto-char vip-last-jump))
4039 (if (= (point) (mark t)) (pop-mark))
4040 (exchange-point-and-mark)
4041 (setq vip-last-jump (point))
4042 (back-to-indentation)
4043 (setq vip-last-jump-ignore (point))
4044 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
4045 (t (error vip-InvalidTextmarker char))))
4047 (defun vip-insert-tab ()
4048 (interactive)
4049 (insert-tab))
4051 (defun vip-exchange-point-and-mark ()
4052 (interactive)
4053 (exchange-point-and-mark)
4054 (back-to-indentation))
4056 ;; Input Mode Indentation
4058 ;; Returns t, if the string before point matches the regexp STR.
4059 (defsubst vip-looking-back (str)
4060 (and (save-excursion (re-search-backward str nil t))
4061 (= (point) (match-end 0))))
4064 (defun vip-forward-indent ()
4065 "Indent forward -- `C-t' in Vi."
4066 (interactive)
4067 (setq vip-cted t)
4068 (indent-to (+ (current-column) vip-shift-width)))
4070 (defun vip-backward-indent ()
4071 "Backtab, C-d in VI"
4072 (interactive)
4073 (if vip-cted
4074 (let ((p (point)) (c (current-column)) bol (indent t))
4075 (if (vip-looking-back "[0^]")
4076 (progn
4077 (if (eq ?^ (preceding-char))
4078 (setq vip-preserve-indent t))
4079 (delete-backward-char 1)
4080 (setq p (point))
4081 (setq indent nil)))
4082 (save-excursion
4083 (beginning-of-line)
4084 (setq bol (point)))
4085 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4086 (delete-region (point) p)
4087 (if indent
4088 (indent-to (- c vip-shift-width)))
4089 (if (or (bolp) (vip-looking-back "[^ \t]"))
4090 (setq vip-cted nil)))))
4092 (defun vip-autoindent ()
4093 "Auto Indentation, Vi-style."
4094 (interactive)
4095 (let ((col (current-indentation)))
4096 (if abbrev-mode (expand-abbrev))
4097 (if vip-preserve-indent
4098 (setq vip-preserve-indent nil)
4099 (setq vip-current-indent col))
4100 ;; don't leave whitespace lines around
4101 (if (memq last-command
4102 '(vip-autoindent
4103 vip-open-line vip-Open-line
4104 vip-replace-state-exit-cmd))
4105 (indent-to-left-margin))
4106 ;; use \n instead of newline, or else <Return> will move the insert point
4107 ;;(newline 1)
4108 (insert "\n")
4109 (if vip-auto-indent
4110 (progn
4111 (setq vip-cted t)
4112 (if vip-electric-mode
4113 (indent-according-to-mode)
4114 (indent-to vip-current-indent))
4119 ;; Viewing registers
4121 (defun vip-ket-function (arg)
4122 "Function called by \], the ket. View registers and call \]\]."
4123 (interactive "P")
4124 (let ((reg (read-char)))
4125 (cond ((vip-valid-register reg '(letter Letter))
4126 (view-register (downcase reg)))
4127 ((vip-valid-register reg '(digit))
4128 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4129 (save-excursion
4130 (set-buffer (get-buffer-create "*Output*"))
4131 (delete-region (point-min) (point-max))
4132 (insert (format "Register %c contains the string:\n" reg))
4133 (insert text)
4134 (goto-char (point-min)))
4135 (display-buffer "*Output*")))
4136 ((= ?\] reg)
4137 (vip-next-heading arg))
4138 (t (error
4139 vip-InvalidRegister reg)))))
4141 (defun vip-brac-function (arg)
4142 "Function called by \[, the brac. View textmarkers and call \[\["
4143 (interactive "P")
4144 (let ((reg (read-char)))
4145 (cond ((= ?\[ reg)
4146 (vip-prev-heading arg))
4147 ((= ?\] reg)
4148 (vip-heading-end arg))
4149 ((vip-valid-register reg '(letter))
4150 (let* ((val (get-register (1+ (- reg ?a))))
4151 (buf (if (not val)
4152 (error vip-EmptyTextmarker reg)
4153 (marker-buffer val)))
4154 (pos (marker-position val))
4155 line-no text (s pos) (e pos))
4156 (save-excursion
4157 (set-buffer (get-buffer-create "*Output*"))
4158 (delete-region (point-min) (point-max))
4159 (if (and buf pos)
4160 (progn
4161 (save-excursion
4162 (set-buffer buf)
4163 (setq line-no (1+ (count-lines (point-min) val)))
4164 (goto-char pos)
4165 (beginning-of-line)
4166 (if (re-search-backward "[^ \t]" nil t)
4167 (progn
4168 (beginning-of-line)
4169 (setq s (point))))
4170 (goto-char pos)
4171 (forward-line 1)
4172 (if (re-search-forward "[^ \t]" nil t)
4173 (progn
4174 (end-of-line)
4175 (setq e (point))))
4176 (setq text (buffer-substring s e))
4177 (setq text (format "%s<%c>%s"
4178 (substring text 0 (- pos s))
4179 reg (substring text (- pos s)))))
4180 (insert
4181 (format
4182 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4183 reg (buffer-name buf) line-no))
4184 (insert (format "Here is some text around %c:\n\n %s"
4185 reg text)))
4186 (insert (format vip-EmptyTextmarker reg)))
4187 (goto-char (point-min)))
4188 (display-buffer "*Output*")))
4189 (t (error vip-InvalidTextmarker reg)))))
4193 ;; commands in insertion mode
4195 (defun vip-delete-backward-word (arg)
4196 "Delete previous word."
4197 (interactive "p")
4198 (save-excursion
4199 (push-mark nil t)
4200 (backward-word arg)
4201 (delete-region (point) (mark t))
4202 (pop-mark)))
4205 (defun vip-set-expert-level (&optional dont-change-unless)
4206 "Sets the expert level for a Viper user.
4207 Can be called interactively to change (temporarily or permanently) the
4208 current expert level.
4210 The optional argument DONT-CHANGE-UNLESS if not nil, says that
4211 the level should not be changed, unless its current value is
4212 meaningless (i.e., not one of 1,2,3,4,5).
4214 User level determines the setting of Viper variables that are most
4215 sensitive for VI-style look-and-feel."
4217 (interactive)
4219 (if (not (natnump vip-expert-level)) (setq vip-expert-level 0))
4221 (save-window-excursion
4222 (delete-other-windows)
4223 ;; if 0 < vip-expert-level < vip-max-expert-level
4224 ;; & dont-change-unless = t -- use it; else ask
4225 (vip-ask-level dont-change-unless))
4227 (setq vip-always t
4228 vip-ex-style-motion t
4229 vip-ex-style-editing-in-insert t
4230 vip-want-ctl-h-help nil)
4232 (cond ((eq vip-expert-level 1) ; novice or beginner
4233 (global-set-key ; in emacs-state
4234 vip-toggle-key
4235 (if (vip-window-display-p) 'vip-iconify 'suspend-emacs))
4236 (setq vip-no-multiple-ESC t
4237 vip-re-search t
4238 vip-vi-style-in-minibuffer t
4239 vip-search-wrap-around-t t
4240 vip-want-emacs-keys-in-vi nil
4241 vip-want-emacs-keys-in-insert nil))
4243 ((and (> vip-expert-level 1) (< vip-expert-level 5))
4244 ;; intermediate to guru
4245 (setq vip-no-multiple-ESC (if (vip-window-display-p) t 'twice)
4246 vip-want-emacs-keys-in-vi t
4247 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4249 (if (eq vip-expert-level 4) ; respect user's ex-style motions
4250 ; and vip-no-multiple-ESC
4251 (progn
4252 (setq-default vip-ex-style-editing-in-insert
4253 (cdr (assoc 'vip-ex-style-editing-in-insert
4254 vip-saved-user-settings))
4255 vip-ex-style-motion
4256 (cdr (assoc 'vip-ex-style-motion
4257 vip-saved-user-settings)))
4258 (setq vip-ex-style-motion
4259 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4260 vip-ex-style-editing-in-insert
4261 (cdr (assoc 'vip-ex-style-editing-in-insert
4262 vip-saved-user-settings))
4263 vip-re-search
4264 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4265 vip-no-multiple-ESC
4266 (cdr (assoc 'vip-no-multiple-ESC
4267 vip-saved-user-settings))))))
4269 ;; A wizard!!
4270 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4271 ;; user toggle the values of variables.
4272 (t (setq-default vip-ex-style-editing-in-insert
4273 (cdr (assoc 'vip-ex-style-editing-in-insert
4274 vip-saved-user-settings))
4275 vip-ex-style-motion
4276 (cdr (assoc 'vip-ex-style-motion
4277 vip-saved-user-settings)))
4278 (setq vip-want-ctl-h-help
4279 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
4280 vip-always
4281 (cdr (assoc 'vip-always vip-saved-user-settings))
4282 vip-no-multiple-ESC
4283 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
4284 vip-ex-style-motion
4285 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4286 vip-ex-style-editing-in-insert
4287 (cdr (assoc 'vip-ex-style-editing-in-insert
4288 vip-saved-user-settings))
4289 vip-re-search
4290 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4291 vip-want-emacs-keys-in-vi
4292 (cdr (assoc 'vip-want-emacs-keys-in-vi
4293 vip-saved-user-settings))
4294 vip-want-emacs-keys-in-insert
4295 (cdr (assoc 'vip-want-emacs-keys-in-insert
4296 vip-saved-user-settings)))))
4297 (vip-set-mode-vars-for vip-current-state)
4298 (if (or vip-always
4299 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
4300 (vip-set-hooks)))
4302 ;; Ask user expert level.
4303 (defun vip-ask-level (dont-change-unless)
4304 (let ((ask-buffer " *vip-ask-level*")
4305 level-changed repeated)
4306 (save-window-excursion
4307 (switch-to-buffer ask-buffer)
4309 (or (eq this-command 'vip-set-expert-level)
4310 (and
4311 (<= vip-expert-level vip-max-expert-level)
4312 (>= vip-expert-level 1))
4313 (progn
4314 (insert "
4316 *** Important Notice for VIP users***
4318 This is VIPER
4320 @joke
4321 Viper Is a Package for Emacs Rebels,
4322 a VI Plan for Emacs Rescue,
4323 and a venomous VI PERil.
4324 @end joke
4326 Technically speaking, Viper is a new Vi emulator that replaces
4327 the old VIP package.
4329 Viper emulates Vi much better than VIP. It also significantly
4330 extends and improves upon Vi in many useful ways.
4332 Although many VIP settings in your ~/.vip are compatible with Viper,
4333 you may have to change some of them. Please refer to the documentation,
4334 which can be obtained by executing
4336 :help
4338 when Viper is in Vi state.
4340 If you will be so lucky as to find a bug, report it via the command
4342 :submitReport
4344 Type any key to continue... ")
4346 (read-char)
4347 (erase-buffer)))
4349 (while (or (> vip-expert-level vip-max-expert-level)
4350 (< vip-expert-level 1)
4351 (null dont-change-unless))
4352 (erase-buffer)
4353 (if repeated
4354 (progn
4355 (message "Invalid user level")
4356 (beep 1))
4357 (setq repeated t))
4358 (setq dont-change-unless t
4359 level-changed t)
4360 (insert "
4361 Please specify your level of familiarity with the venomous VI PERil
4362 (and the VI Plan for Emacs Rescue).
4363 You can change it at any time by typing `M-x vip-set-expert-level RET'
4365 1 -- BEGINNER: Almost all Emacs features are suppressed.
4366 Feels almost like straight Vi. File name completion and
4367 command history in the minibuffer are thrown in as a bonus.
4368 To use Emacs productively, you must reach level 3 or higher.
4369 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4370 so most Emacs commands can be used when Viper is in Vi state.
4371 Good progress---you are well on the way to level 3!
4372 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
4373 in Viper's insert state.
4374 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
4375 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
4376 variables. Adjust these settings to your taste.
4377 5 -- WIZARD: Like 4, but user settings are also respected for vip-always,
4378 vip-want-ctl-h-help, vip-want-emacs-keys-in-vi, and
4379 vip-want-emacs-keys-in-insert. Adjust these to your taste.
4381 Please, specify your level now: ")
4383 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
4384 ) ; end while
4386 ;; tell the user if level was changed
4387 (and level-changed
4388 (progn
4389 (insert
4390 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4391 vip-expert-level))
4392 (if (y-or-n-p "Do you wish to make this change permanent? ")
4393 ;; save the setting for vip-expert-level
4394 (vip-save-setting
4395 'vip-expert-level
4396 (format "Saving user level %d ..." vip-expert-level)
4397 vip-custom-file-name))
4399 (bury-buffer) ; remove ask-buffer from screen
4400 (message "")
4404 (defun vip-nil ()
4405 (interactive)
4406 (beep 1))
4409 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4410 (defun vip-register-to-point (char &optional enforce-buffer)
4411 "Like jump-to-register, but switches to another buffer in another window."
4412 (interactive "cViper register to point: ")
4413 (let ((val (get-register char)))
4414 (cond
4415 ((and (fboundp 'frame-configuration-p)
4416 (frame-configuration-p val))
4417 (set-frame-configuration val))
4418 ((window-configuration-p val)
4419 (set-window-configuration val))
4420 ((vip-valid-marker val)
4421 (if (and enforce-buffer
4422 (not (equal (current-buffer) (marker-buffer val))))
4423 (error (concat vip-EmptyTextmarker " in this buffer")
4424 (1- (+ char ?a))))
4425 (pop-to-buffer (marker-buffer val))
4426 (goto-char val))
4427 ((and (consp val) (eq (car val) 'file))
4428 (find-file (cdr val)))
4430 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
4433 (defun vip-save-kill-buffer ()
4434 "Save then kill current buffer. "
4435 (interactive)
4436 (if (< vip-expert-level 2)
4437 (save-buffers-kill-emacs)
4438 (save-buffer)
4439 (kill-buffer (current-buffer))))
4443 ;;; Bug Report
4445 (defun vip-submit-report ()
4446 "Submit bug report on Viper."
4447 (interactive)
4448 (let ((reporter-prompt-for-summary-p t)
4449 (vip-device-type (vip-device-type))
4450 color-display-p frame-parameters
4451 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4452 varlist salutation window-config)
4454 ;; If mode info is needed, add variable to `let' and then set it below,
4455 ;; like we did with color-display-p.
4456 (setq color-display-p (if (vip-window-display-p)
4457 (vip-color-display-p)
4458 'non-x)
4459 minibuffer-vi-face (if (vip-has-face-support-p)
4460 (vip-get-face vip-minibuffer-vi-face)
4461 'non-x)
4462 minibuffer-insert-face (if (vip-has-face-support-p)
4463 (vip-get-face vip-minibuffer-insert-face)
4464 'non-x)
4465 minibuffer-emacs-face (if (vip-has-face-support-p)
4466 (vip-get-face vip-minibuffer-emacs-face)
4467 'non-x)
4468 frame-parameters (if (fboundp 'frame-parameters)
4469 (frame-parameters (selected-frame))))
4471 (setq varlist (list 'vip-vi-minibuffer-minor-mode
4472 'vip-insert-minibuffer-minor-mode
4473 'vip-vi-intercept-minor-mode
4474 'vip-vi-local-user-minor-mode
4475 'vip-vi-kbd-minor-mode
4476 'vip-vi-global-user-minor-mode
4477 'vip-vi-state-modifier-minor-mode
4478 'vip-vi-diehard-minor-mode
4479 'vip-vi-basic-minor-mode
4480 'vip-replace-minor-mode
4481 'vip-insert-intercept-minor-mode
4482 'vip-insert-local-user-minor-mode
4483 'vip-insert-kbd-minor-mode
4484 'vip-insert-global-user-minor-mode
4485 'vip-insert-state-modifier-minor-mode
4486 'vip-insert-diehard-minor-mode
4487 'vip-insert-basic-minor-mode
4488 'vip-emacs-intercept-minor-mode
4489 'vip-emacs-local-user-minor-mode
4490 'vip-emacs-kbd-minor-mode
4491 'vip-emacs-global-user-minor-mode
4492 'vip-emacs-state-modifier-minor-mode
4493 'vip-automatic-iso-accents
4494 'vip-want-emacs-keys-in-insert
4495 'vip-want-emacs-keys-in-vi
4496 'vip-keep-point-on-undo
4497 'vip-no-multiple-ESC
4498 'vip-ESC-key
4499 'vip-want-ctl-h-help
4500 'vip-ex-style-editing-in-insert
4501 'vip-delete-backwards-in-replace
4502 'vip-vi-style-in-minibuffer
4503 'vip-vi-state-hook
4504 'vip-insert-state-hook
4505 'vip-replace-state-hook
4506 'vip-emacs-state-hook
4507 'ex-cycle-other-window
4508 'ex-cycle-through-non-files
4509 'vip-expert-level
4510 'major-mode
4511 'vip-device-type
4512 'color-display-p
4513 'frame-parameters
4514 'minibuffer-vi-face
4515 'minibuffer-insert-face
4516 'minibuffer-emacs-face
4518 (setq salutation "
4519 Congratulations! You may have unearthed a bug in Viper!
4520 Please mail a concise, accurate summary of the problem to the address above.
4522 -------------------------------------------------------------------")
4523 (setq window-config (current-window-configuration))
4524 (with-output-to-temp-buffer " *vip-info*"
4525 (switch-to-buffer " *vip-info*")
4526 (delete-other-windows)
4527 (princ "
4528 PLEASE FOLLOW THESE PROCEDURES
4529 ------------------------------
4531 Before reporting a bug, please verify that it is related to Viper, and is
4532 not cause by other packages you are using.
4534 Don't report compilation warnings, unless you are certain that there is a
4535 problem. These warnings are normal and unavoidable.
4537 Please note that users should not modify variables and keymaps other than
4538 those advertised in the manual. Such `customization' is likely to crash
4539 Viper, as it would any other improperly customized Emacs package.
4541 If you are reporting an error message received while executing one of the
4542 Viper commands, type:
4544 M-x set-variable <Return> debug-on-error <Return> t <Return>
4546 Then reproduce the error. The above command will cause Emacs to produce a
4547 back trace of the execution that leads to the error. Please include this
4548 trace in your bug report.
4550 If you believe that one of Viper's commands goes into an infinite loop
4551 \(e.g., Emacs freezes\), type:
4553 M-x set-variable <Return> debug-on-quit <Return> t <Return>
4555 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
4556 the current command. Include the resulting back trace in the bug report.
4558 Mail anyway (y or n)? ")
4559 (if (y-or-n-p "Mail anyway? ")
4561 (set-window-configuration window-config)
4562 (error "Bug report aborted")))
4564 (require 'reporter)
4565 (set-window-configuration window-config)
4567 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
4568 (vip-version)
4569 varlist
4570 nil 'delete-other-windows
4571 salutation)
4577 ;; Smoothes out the difference between Emacs' unread-command-events
4578 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
4579 ;; events or a sequence of keys.
4581 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
4582 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
4583 ;; into an event. Below, we delete nil from event lists, since nil is the most
4584 ;; common symbol that might appear in this wrong context.
4585 (defun vip-set-unread-command-events (arg)
4586 (if vip-emacs-p
4587 (setq
4588 unread-command-events
4589 (let ((new-events
4590 (cond ((eventp arg) (list arg))
4591 ((listp arg) arg)
4592 ((sequencep arg)
4593 (listify-key-sequence arg))
4594 (t (error
4595 "vip-set-unread-command-events: Invalid argument, %S"
4596 arg)))))
4597 (if (not (eventp nil))
4598 (setq new-events (delq nil new-events)))
4599 (append new-events unread-command-events)))
4600 ;; XEmacs
4601 (setq
4602 unread-command-events
4603 (append
4604 (cond ((vip-characterp arg) (list (character-to-event arg)))
4605 ((eventp arg) (list arg))
4606 ((stringp arg) (mapcar 'character-to-event arg))
4607 ((vectorp arg) (append arg nil)) ; turn into list
4608 ((listp arg) (vip-eventify-list-xemacs arg))
4609 (t (error
4610 "vip-set-unread-command-events: Invalid argument, %S" arg)))
4611 unread-command-events))))
4613 ;; list is assumed to be a list of events of characters
4614 (defun vip-eventify-list-xemacs (lis)
4615 (mapcar
4616 (function (lambda (elt)
4617 (cond ((vip-characterp elt) (character-to-event elt))
4618 ((eventp elt) elt)
4619 (t (error
4620 "vip-eventify-list-xemacs: can't convert to event, %S"
4621 elt)))))
4622 lis))
4626 ;;; viper-cmd.el ends here