1 ;;; viper-cmd.el --- Vi command support for Viper
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
11 (defvar vip-minibuffer-current-face
)
12 (defvar vip-minibuffer-insert-face
)
13 (defvar vip-minibuffer-vi-face
)
14 (defvar vip-minibuffer-emacs-face
)
16 (defvar vip-mode-string
)
17 (defvar iso-accents-mode
)
18 (defvar zmacs-region-stays
)
19 (defvar mark-even-if-inactive
)
21 ;; loading happens only in non-interactive compilation
22 ;; in order to spare non-viperized emacs from being viperized
25 (let ((load-path (cons (expand-file-name ".") load-path
)))
26 (or (featurep 'viper-util
)
27 (load "viper-util.el" nil nil
'nosuffix
))
28 (or (featurep 'viper-keym
)
29 (load "viper-keym.el" nil nil
'nosuffix
))
30 (or (featurep 'viper-mous
)
31 (load "viper-mous.el" nil nil
'nosuffix
))
32 (or (featurep 'viper-macs
)
33 (load "viper-macs.el" nil nil
'nosuffix
))
34 (or (featurep 'viper-ex
)
35 (load "viper-ex.el" nil nil
'nosuffix
))
50 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
52 ;; generate test functions
53 ;; given symbol foo, foo-p is the test function, foos is the set of
55 ;; (macroexpand '(vip-test-com-defun foo))
56 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
58 (defmacro vip-test-com-defun
(name)
59 (let* ((snm (symbol-name name
))
60 (nm-p (intern (concat snm
"-p")))
61 (nms (intern (concat snm
"s"))))
62 (` (defun (, nm-p
) (com)
63 (consp (memq (if (< com
0) (- com
) com
) (, nms
)))))))
65 ;; Variables for defining VI commands
67 ;; Modifying commands that can be prefixes to movement commands
68 (defconst vip-prefix-commands
'(?c ?d ?y ?
! ?
= ?
# ?
< ?
> ?
\"))
69 ;; define vip-prefix-command-p
70 (vip-test-com-defun vip-prefix-command)
72 ;; Commands that are pairs eg. dd. r and R here are a hack
73 (defconst vip-charpair-commands
'(?c ?d ?y ?
! ?
= ?
< ?
> ?r ?R
))
74 ;; define vip-charpair-command-p
75 (vip-test-com-defun vip-charpair-command)
77 (defconst vip-movement-commands
'(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
78 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
79 ?^ ?
( ?
) ?- ?
+ ?| ?
{ ?
} ?
[ ?
] ?
' ?
`
83 ;; define vip-movement-command-p
84 (vip-test-com-defun vip-movement-command)
86 (defconst vip-digit-commands
'(?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9)
88 ;; define vip-digit-command-p
89 (vip-test-com-defun vip-digit-command)
91 ;; Commands that can be repeated by . (dotted)
92 (defconst vip-dotable-commands
'(?c ?d ?C ?s ?S ?D ?
> ?
<))
93 ;; define vip-dotable-command-p
94 (vip-test-com-defun vip-dotable-command)
96 ;; Commands that can follow a #
97 (defconst vip-hash-commands
'(?c ?C ?g ?q ?s
))
98 ;; define vip-hash-command-p
99 (vip-test-com-defun vip-hash-command)
101 ;; Commands that may have registers as prefix
102 (defconst vip-regsuffix-commands
'(?d ?y ?Y ?D ?p ?P ?x ?X
))
103 ;; define vip-regsuffix-command-p
104 (vip-test-com-defun vip-regsuffix-command)
106 (defconst vip-vi-commands
(append vip-movement-commands
109 vip-charpair-commands
112 vip-regsuffix-commands
)
113 "The list of all commands in Vi-state.")
114 ;; define vip-vi-command-p
115 (vip-test-com-defun vip-vi-command)
122 ;; Runs vip-after-change-functions inside after-change-functions
123 (defun vip-after-change-sentinel (beg end len
)
124 (let ((list vip-after-change-functions
))
126 (funcall (car list
) beg end len
)
127 (setq list
(cdr list
)))))
129 ;; Runs vip-before-change-functions inside before-change-functions
130 (defun vip-before-change-sentinel (beg end
)
131 (let ((list vip-before-change-functions
))
133 (funcall (car list
) beg end
)
134 (setq list
(cdr list
)))))
136 (defsubst vip-post-command-sentinel
()
137 (run-hooks 'vip-post-command-hooks
))
139 (defsubst vip-pre-command-sentinel
()
140 (run-hooks 'vip-pre-command-hooks
))
142 ;; Needed so that Viper will be able to figure the last inserted
143 ;; chunk of text with reasonable accuracy.
144 (defsubst vip-insert-state-post-command-sentinel
()
145 (if (and (memq vip-current-state
'(insert-state replace-state
))
147 (>= (point) vip-insert-point
))
148 (setq vip-last-posn-while-in-insert-state
(point-marker)))
149 (if (eq vip-current-state
'insert-state
)
151 (or (stringp vip-saved-cursor-color
)
152 (string= (vip-get-cursor-color) vip-insert-state-cursor-color
)
153 (setq vip-saved-cursor-color
(vip-get-cursor-color)))
154 (if (stringp vip-saved-cursor-color
)
155 (vip-change-cursor-color vip-insert-state-cursor-color
))
157 (if (and (eq this-command
'dabbrev-expand
)
158 (integerp vip-pre-command-point
)
159 (> vip-insert-point vip-pre-command-point
))
160 (move-marker vip-insert-point vip-pre-command-point
))
163 (defsubst vip-insert-state-pre-command-sentinel
()
164 (or (memq this-command
'(self-insert-command))
165 (memq (vip-event-key last-command-event
)
166 '(up down left right
(meta f
) (meta b
)
167 (control n
) (control p
) (control f
) (control b
)))
168 (vip-restore-cursor-color-after-insert))
169 (if (and (eq this-command
'dabbrev-expand
)
170 (markerp vip-insert-point
)
171 (marker-position vip-insert-point
))
172 (setq vip-pre-command-point
(marker-position vip-insert-point
))))
174 (defsubst vip-R-state-post-command-sentinel
()
175 ;; Restoring cursor color is needed despite
176 ;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
177 ;; another frame, the pre-command hook won't change cursor color to default
178 ;; in that other frame. So, if the second frame cursor was red and we set
179 ;; the point outside the replacement region, then the cursor color will
180 ;; remain red. Restoring the default, below, prevents this.
181 (if (and (<= (vip-replace-start) (point))
182 (<= (point) (vip-replace-end)))
183 (vip-change-cursor-color vip-replace-overlay-cursor-color
)
184 (vip-restore-cursor-color-after-replace)
187 ;; to speed up, don't change cursor color before self-insert
188 ;; and common move commands
189 (defsubst vip-replace-state-pre-command-sentinel
()
190 (or (memq this-command
'(self-insert-command))
191 (memq (vip-event-key last-command-event
)
192 '(up down left right
(meta f
) (meta b
)
193 (control n
) (control p
) (control f
) (control b
)))
194 (vip-restore-cursor-color-after-replace)))
196 (defun vip-replace-state-post-command-sentinel ()
197 ;; Restoring cursor color is needed despite
198 ;; vip-replace-state-pre-command-sentinel: When one jumps to another buffer
199 ;; in another frame, the pre-command hook won't change cursor color to
200 ;; default in that other frame. So, if the second frame cursor was red and
201 ;; we set the point outside the replacement region, then the cursor color
202 ;; will remain red. Restoring the default, below, fixes this problem.
204 ;; We optimize for self-insert-command's here, since they either don't change
205 ;; cursor color or, if they terminate replace mode, the color will be changed
206 ;; in vip-finish-change
207 (or (memq this-command
'(self-insert-command))
208 (vip-restore-cursor-color-after-replace))
210 ((eq vip-current-state
'replace-state
)
211 ;; delete characters to compensate for inserted chars.
212 (let ((replace-boundary (vip-replace-end)))
214 (goto-char vip-last-posn-in-replace-region
)
215 (delete-char vip-replace-chars-to-delete
)
216 (setq vip-replace-chars-to-delete
0
217 vip-replace-chars-deleted
0)
218 ;; terminate replace mode if reached replace limit
219 (if (= vip-last-posn-in-replace-region
221 (vip-finish-change vip-last-posn-in-replace-region
)))
223 (if (and (<= (vip-replace-start) (point))
224 (<= (point) replace-boundary
))
226 ;; the state may have changed in vip-finish-change above
227 (if (eq vip-current-state
'replace-state
)
228 (vip-change-cursor-color vip-replace-overlay-cursor-color
))
229 (setq vip-last-posn-in-replace-region
(point-marker))))
232 (t ;; terminate replace mode if changed Viper states.
233 (vip-finish-change vip-last-posn-in-replace-region
))))
238 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
239 (defun vip-change-state (new-state)
240 ;; Keep vip-post/pre-command-hooks fresh.
241 ;; We remove then add vip-post/pre-command-sentinel since it is very
242 ;; desirable that vip-pre-command-sentinel is the last hook and
243 ;; vip-post-command-sentinel is the first hook.
244 (remove-hook 'post-command-hook
'vip-post-command-sentinel
)
245 (add-hook 'post-command-hook
'vip-post-command-sentinel
)
246 (remove-hook 'pre-command-hook
'vip-pre-command-sentinel
)
247 (add-hook 'pre-command-hook
'vip-pre-command-sentinel t
)
248 ;; These hooks will be added back if switching to insert/replace mode
249 (vip-remove-hook 'vip-post-command-hooks
250 'vip-insert-state-post-command-sentinel
)
251 (vip-remove-hook 'vip-pre-command-hooks
252 'vip-insert-state-pre-command-sentinel
)
253 (cond ((eq new-state
'vi-state
)
254 (cond ((member vip-current-state
'(insert-state replace-state
))
256 ;; move vip-last-posn-while-in-insert-state
257 ;; This is a normal hook that is executed in insert/replace
258 ;; states after each command. In Vi/Emacs state, it does
259 ;; nothing. We need to execute it here to make sure that
260 ;; the last posn was recorded when we hit ESC.
261 ;; It may be left unrecorded if the last thing done in
262 ;; insert/repl state was dabbrev-expansion or abbrev
263 ;; expansion caused by hitting ESC
264 (vip-insert-state-post-command-sentinel)
266 (condition-case conds
268 (vip-save-last-insertion
270 vip-last-posn-while-in-insert-state
)
271 (if vip-began-as-replace
272 (setq vip-began-as-replace nil
)
273 ;; repeat insert commands if numerical arg > 1
275 (vip-repeat-insert-command))))
277 (vip-message-conditions conds
)))
279 (if (> (length vip-last-insertion
) 0)
280 (vip-push-onto-ring vip-last-insertion
281 'vip-insertion-ring
))
283 (if vip-ex-style-editing-in-insert
284 (or (bolp) (backward-char 1))))
288 ((memq new-state
'(insert-state replace-state
))
289 (if (memq vip-current-state
'(emacs-state vi-state
))
290 (vip-move-marker-locally 'vip-insert-point
(point)))
291 (vip-move-marker-locally 'vip-last-posn-while-in-insert-state
(point))
292 (vip-add-hook 'vip-post-command-hooks
293 'vip-insert-state-post-command-sentinel t
)
294 (vip-add-hook 'vip-pre-command-hooks
295 'vip-insert-state-pre-command-sentinel t
))
298 ;; Nothing needs to be done to switch to emacs mode! Just set some
299 ;; variables, which is already done in vip-change-state-to-emacs!
301 (setq vip-current-state new-state
)
302 (vip-normalize-minor-mode-map-alist)
303 (vip-adjust-keys-for new-state
)
304 (vip-set-mode-vars-for new-state
)
305 (vip-refresh-mode-line)
310 (defun vip-adjust-keys-for (state)
311 "Make necessary adjustments to keymaps before entering STATE."
312 (cond ((memq state
'(insert-state replace-state
))
315 (define-key vip-insert-basic-map
"\C-m" 'vip-autoindent
)
316 (if vip-want-emacs-keys-in-insert
318 (define-key vip-insert-basic-map
"\C-j" nil
)
320 (define-key vip-insert-basic-map
"\C-j" 'vip-autoindent
)))
321 (define-key vip-insert-basic-map
"\C-m" nil
)
322 (define-key vip-insert-basic-map
"\C-j" nil
))
324 (setq vip-insert-diehard-minor-mode
325 (not vip-want-emacs-keys-in-insert
))
327 (if vip-want-ctl-h-help
329 (define-key vip-insert-basic-map
[(control h
)] 'help-command
)
330 (define-key vip-replace-map
[(control h
)] 'help-command
))
331 (define-key vip-insert-basic-map
332 [(control h
)] 'vip-del-backward-char-in-insert
)
333 (define-key vip-replace-map
334 [(control h
)] 'vip-del-backward-char-in-replace
)))
337 (setq vip-vi-diehard-minor-mode
(not vip-want-emacs-keys-in-vi
))
338 (if vip-want-ctl-h-help
339 (define-key vip-vi-basic-map
[(control h
)] 'help-command
)
340 (define-key vip-vi-basic-map
[(control h
)] 'vip-backward-char
)))
344 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
345 ;; This ensures that Viper bindings are in effect, regardless of which minor
346 ;; modes were turned on by the user or by other packages.
347 (defun vip-normalize-minor-mode-map-alist ()
348 (setq minor-mode-map-alist
349 (vip-append-filter-alist
351 (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map
)
352 (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map
)
353 (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map
)
354 (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map
)
355 (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map
)
356 (cons 'vip-vi-state-modifier-minor-mode
358 (cdr (assoc major-mode vip-vi-state-modifier-alist
)))
359 (cdr (assoc major-mode vip-vi-state-modifier-alist
))
361 (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map
)
362 (cons 'vip-vi-basic-minor-mode vip-vi-basic-map
)
363 (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map
)
364 (cons 'vip-replace-minor-mode vip-replace-map
)
365 ;; vip-insert-minibuffer-minor-mode must come after
366 ;; vip-replace-minor-mode
367 (cons 'vip-insert-minibuffer-minor-mode
369 (cons 'vip-insert-local-user-minor-mode
370 vip-insert-local-user-map
)
371 (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map
)
372 (cons 'vip-insert-global-user-minor-mode
373 vip-insert-global-user-map
)
374 (cons 'vip-insert-state-modifier-minor-mode
377 (assoc major-mode vip-insert-state-modifier-alist
)))
379 (assoc major-mode vip-insert-state-modifier-alist
))
381 (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map
)
382 (cons 'vip-insert-basic-minor-mode vip-insert-basic-map
)
383 (cons 'vip-emacs-intercept-minor-mode
384 vip-emacs-intercept-map
)
385 (cons 'vip-emacs-local-user-minor-mode
386 vip-emacs-local-user-map
)
387 (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map
)
388 (cons 'vip-emacs-global-user-minor-mode
389 vip-emacs-global-user-map
)
390 (cons 'vip-emacs-state-modifier-minor-mode
393 (assoc major-mode vip-emacs-state-modifier-alist
)))
395 (assoc major-mode vip-emacs-state-modifier-alist
))
398 minor-mode-map-alist
)))
404 ;; Viper mode-changing commands and utilities
406 ;; Modifies mode-line-buffer-identification.
407 (defun vip-refresh-mode-line ()
408 (setq vip-mode-string
409 (cond ((eq vip-current-state
'emacs-state
) vip-emacs-state-id
)
410 ((eq vip-current-state
'vi-state
) vip-vi-state-id
)
411 ((eq vip-current-state
'replace-state
) vip-replace-state-id
)
412 ((eq vip-current-state
'insert-state
) vip-insert-state-id
)))
414 ;; Sets Viper mode string in global-mode-string
415 (force-mode-line-update))
418 ;; Switch from Insert state to Vi state.
419 (defun vip-exit-insert-state ()
421 (vip-change-state-to-vi))
423 (defun vip-set-mode-vars-for (state)
424 "Sets Viper minor mode variables to put Viper's state STATE in effect."
427 (setq vip-vi-minibuffer-minor-mode nil
428 vip-insert-minibuffer-minor-mode nil
429 vip-vi-intercept-minor-mode nil
430 vip-insert-intercept-minor-mode nil
432 vip-vi-local-user-minor-mode nil
433 vip-vi-kbd-minor-mode nil
434 vip-vi-global-user-minor-mode nil
435 vip-vi-state-modifier-minor-mode nil
436 vip-vi-diehard-minor-mode nil
437 vip-vi-basic-minor-mode nil
439 vip-replace-minor-mode nil
441 vip-insert-local-user-minor-mode nil
442 vip-insert-kbd-minor-mode nil
443 vip-insert-global-user-minor-mode nil
444 vip-insert-state-modifier-minor-mode nil
445 vip-insert-diehard-minor-mode nil
446 vip-insert-basic-minor-mode nil
447 vip-emacs-intercept-minor-mode t
448 vip-emacs-local-user-minor-mode t
449 vip-emacs-kbd-minor-mode
(not (vip-is-in-minibuffer))
450 vip-emacs-global-user-minor-mode t
451 vip-emacs-state-modifier-minor-mode t
455 (if (eq state
'vi-state
) ; adjust for vi-state
457 vip-vi-intercept-minor-mode t
458 vip-vi-minibuffer-minor-mode
(vip-is-in-minibuffer)
459 vip-vi-local-user-minor-mode t
460 vip-vi-kbd-minor-mode
(not (vip-is-in-minibuffer))
461 vip-vi-global-user-minor-mode t
462 vip-vi-state-modifier-minor-mode t
463 ;; don't let the diehard keymap block command completion
464 ;; and other things in the minibuffer
465 vip-vi-diehard-minor-mode
(not
466 (or vip-want-emacs-keys-in-vi
467 (vip-is-in-minibuffer)))
468 vip-vi-basic-minor-mode t
469 vip-emacs-intercept-minor-mode nil
470 vip-emacs-local-user-minor-mode nil
471 vip-emacs-kbd-minor-mode nil
472 vip-emacs-global-user-minor-mode nil
473 vip-emacs-state-modifier-minor-mode nil
476 ;; Insert and Replace states
477 (if (member state
'(insert-state replace-state
))
479 vip-insert-intercept-minor-mode t
480 vip-replace-minor-mode
(eq state
'replace-state
)
481 vip-insert-minibuffer-minor-mode
(vip-is-in-minibuffer)
482 vip-insert-local-user-minor-mode t
483 vip-insert-kbd-minor-mode
(not (vip-is-in-minibuffer))
484 vip-insert-global-user-minor-mode t
485 vip-insert-state-modifier-minor-mode t
486 ;; don't let the diehard keymap block command completion
487 ;; and other things in the minibuffer
488 vip-insert-diehard-minor-mode
(not
489 (or vip-want-emacs-keys-in-insert
490 (vip-is-in-minibuffer)))
491 vip-insert-basic-minor-mode t
492 vip-emacs-intercept-minor-mode nil
493 vip-emacs-local-user-minor-mode nil
494 vip-emacs-kbd-minor-mode nil
495 vip-emacs-global-user-minor-mode nil
496 vip-emacs-state-modifier-minor-mode nil
500 (if (vip-has-face-support-p)
501 (setq vip-minibuffer-current-face
502 (cond ((eq state
'emacs-state
) vip-minibuffer-emacs-face
)
503 ((eq state
'vi-state
) vip-minibuffer-vi-face
)
504 ((memq state
'(insert-state replace-state
))
505 vip-minibuffer-insert-face
))))
507 (if (vip-is-in-minibuffer)
508 (vip-set-minibuffer-overlay))
511 ;; This also takes care of the annoying incomplete lines in files.
512 ;; Also, this fixes `undo' to work vi-style for complex commands.
513 (defun vip-change-state-to-vi ()
514 "Change Viper state to Vi."
516 (if (and vip-first-time
(not (vip-is-in-minibuffer)))
518 (if overwrite-mode
(overwrite-mode nil
))
519 (if abbrev-mode
(expand-abbrev))
520 (if (and auto-fill-function
(> (current-column) fill-column
))
521 (funcall auto-fill-function
))
522 ;; don't leave whitespace lines around
523 (if (and (memq last-command
525 vip-open-line vip-Open-line
526 vip-replace-state-exit-cmd
))
527 (vip-over-whitespace-line))
528 (indent-to-left-margin))
529 (vip-add-newline-at-eob-if-necessary)
530 (if vip-undo-needs-adjustment
(vip-adjust-undo))
531 (vip-change-state 'vi-state
)
533 ;; always turn off iso-accents-mode, or else we won't be able to use the
534 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
535 (if (and (boundp 'iso-accents-mode
) iso-accents-mode
)
536 (iso-accents-mode -
1))
538 (vip-restore-cursor-color-after-insert)
540 ;; Protection against user errors in hooks
541 (condition-case conds
542 (run-hooks 'vip-vi-state-hook
)
544 (vip-message-conditions conds
)))))
546 (defun vip-change-state-to-insert ()
547 "Change Viper state to Insert."
549 (vip-change-state 'insert-state
)
550 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
551 (iso-accents-mode 1)) ; turn iso accents on
553 (or (stringp vip-saved-cursor-color
)
554 (string= (vip-get-cursor-color) vip-insert-state-cursor-color
)
555 (setq vip-saved-cursor-color
(vip-get-cursor-color)))
556 ;; Commented out, because if vip-change-state-to-insert is executed
557 ;; non-interactively then the old cursor color may get lost. Same old Emacs
558 ;; bug related to local variables?
559 ;;;(if (stringp vip-saved-cursor-color)
560 ;;; (vip-change-cursor-color vip-insert-state-cursor-color))
561 ;; Protection against user errors in hooks
562 (condition-case conds
563 (run-hooks 'vip-insert-state-hook
)
565 (vip-message-conditions conds
))))
567 (defsubst vip-downgrade-to-insert
()
568 (setq vip-current-state
'insert-state
569 vip-replace-minor-mode nil
)
574 ;; Change to replace state. When the end of replacement region is reached,
575 ;; replace state changes to insert state.
576 (defun vip-change-state-to-replace (&optional non-R-cmd
)
577 (vip-change-state 'replace-state
)
578 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
579 (iso-accents-mode 1)) ; turn iso accents on
580 ;; Run insert-state-hook
581 (condition-case conds
582 (run-hooks 'vip-insert-state-hook
'vip-replace-state-hook
)
584 (vip-message-conditions conds
)))
588 ;; 'R' is implemented using Emacs's overwrite-mode
593 (defun vip-change-state-to-emacs ()
594 "Change Viper state to Emacs."
596 (vip-change-state 'emacs-state
)
597 (if (and vip-automatic-iso-accents
(fboundp 'iso-accents-mode
))
598 (iso-accents-mode 1)) ; turn iso accents on
600 ;; Protection agains user errors in hooks
601 (condition-case conds
602 (run-hooks 'vip-emacs-state-hook
)
604 (vip-message-conditions conds
))))
606 ;; escape to emacs mode termporarily
607 (defun vip-escape-to-emacs (arg &optional events
)
608 "Escape to Emacs state from Vi state for one Emacs command.
609 ARG is used as the prefix value for the executed command. If
610 EVENTS is a list of events, which become the beginning of the command."
612 (if (= last-command-char ?
\\)
613 (message "Switched to EMACS state for the next command..."))
614 (vip-escape-to-state arg events
'emacs-state
))
616 ;; escape to Vi mode termporarily
617 (defun vip-escape-to-vi (arg)
618 "Escape from Emacs state to Vi state for one Vi 1-character command.
619 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
620 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
621 `vip-escape-to-vi' is used."
623 (message "Switched to VI state for the next command...")
624 (vip-escape-to-state arg nil
'vi-state
))
626 ;; Escape to STATE mode for one Emacs command.
627 (defun vip-escape-to-state (arg events state
)
628 ;;(let (com key prefix-arg)
630 ;; this temporarily turns off Viper's minor mode keymaps
631 (vip-set-mode-vars-for state
)
632 (vip-normalize-minor-mode-map-alist)
633 (if events
(vip-set-unread-command-events events
))
635 ;; protect against keyboard quit and other errors
637 (let (vip-vi-kbd-minor-mode
638 vip-insert-kbd-minor-mode
639 vip-emacs-kbd-minor-mode
)
642 (setq com
(key-binding (setq key
644 (read-key-sequence nil
)
645 (read-key-sequence nil t
)))))
646 ;; In case of binding indirection--chase definitions.
647 ;; Have to do it here because we execute this command under
648 ;; different keymaps, so command-execute may not do the
650 (while (vectorp com
) (setq com
(key-binding com
))))
652 ;; Execute command com in the original Viper state, not in state
653 ;; `state'. Otherwise, if we switch buffers while executing the
654 ;; escaped to command, Viper's mode vars will remain those of
655 ;; `state'. When we return to the orig buffer, the bindings will be
657 (vip-set-mode-vars-for vip-current-state
)
659 ;; this-command, last-command-char, last-command-event
660 (setq this-command com
)
661 (if vip-xemacs-p
; XEmacs represents key sequences as vectors
662 (setq last-command-event
(vip-copy-event (vip-seq-last-elt key
))
663 last-command-char
(event-to-character last-command-event
))
664 ;; Emacs represents them as sequences (str or vec)
665 (setq last-command-event
(vip-copy-event (vip-seq-last-elt key
))
666 last-command-char last-command-event
))
670 (setq prefix-arg
(or prefix-arg arg
))
671 (command-execute com
)))
675 ;; set state in the new buffer
676 (vip-set-mode-vars-for vip-current-state
))
678 (defun vip-exec-form-in-vi (form)
679 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
680 (let ((buff (current-buffer))
682 (vip-set-mode-vars-for 'vi-state
)
685 (setq result
(eval form
))
689 (if (not (equal buff
(current-buffer))) ; cmd switched buffer
692 (vip-set-mode-vars-for vip-current-state
)))
693 (vip-set-mode-vars-for vip-current-state
)
696 (defun vip-exec-form-in-emacs (form)
697 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
698 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
699 (let ((buff (current-buffer))
701 (vip-set-mode-vars-for 'emacs-state
)
702 (setq result
(eval form
))
703 (if (not (equal buff
(current-buffer))) ; cmd switched buffer
706 (vip-set-mode-vars-for vip-current-state
)))
707 (vip-set-mode-vars-for vip-current-state
)
711 ;; This is needed because minor modes sometimes override essential Viper
712 ;; bindings. By letting Viper know which files these modes are in, it will
713 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
714 (defun vip-harness-minor-mode (load-file)
715 "Familiarize Viper with a minor mode defined in LOAD_FILE.
716 Minor modes that have their own keymaps may overshadow Viper keymaps.
717 This function is designed to make Viper aware of the packages that define
720 (vip-harness-minor-mode load-file)
722 LOAD-FILE is a name of the file where the specific minor mode is defined.
723 Suffixes such as .el or .elc should be stripped."
725 (interactive "sEnter name of the load file: ")
727 (vip-eval-after-load load-file
'(vip-normalize-minor-mode-map-alist))
729 ;; Change the default for minor-mode-map-alist each time a harnessed minor
730 ;; mode adds its own keymap to the a-list.
732 load-file
'(setq-default minor-mode-map-alist minor-mode-map-alist
))
737 "Emulate ESC key in Emacs.
738 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true.
739 If vip-no-multiple-ESC is 'twice double ESC would ding in vi-state.
740 Other ESC sequences are emulated via the current Emacs's major mode
741 keymap. This is more convenient on TTYs, since this won't block
742 function keys such as up,down, etc. ESC will also will also work as
743 a Meta key in this case. When vip-no-multiple-ESC is nil, ESC functions
744 as a Meta key and any number of multiple escapes is allowed."
747 (cond ((and (not vip-no-multiple-ESC
) (eq vip-current-state
'vi-state
))
748 (setq char
(vip-read-char-exclusive))
749 (vip-escape-to-emacs arg
(list ?\e char
) ))
750 ((and (eq vip-no-multiple-ESC
'twice
)
751 (eq vip-current-state
'vi-state
))
752 (setq char
(vip-read-char-exclusive))
753 (if (= char
(string-to-char vip-ESC-key
))
755 (vip-escape-to-emacs arg
(list ?\e char
) )))
759 (defun vip-alternate-Meta-key (arg)
760 "Simulate Emacs Meta key."
762 (sit-for 1) (message "ESC-")
763 (vip-escape-to-emacs arg
'(?\e
)))
765 (defun vip-toggle-key-action ()
766 "Action bound to `vip-toggle-key'."
768 (if (and (< vip-expert-level
2) (equal vip-toggle-key
"\C-z"))
769 (if (vip-window-display-p)
772 (vip-change-state-to-emacs)))
775 ;; Intercept ESC sequences on dumb terminals.
776 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
778 ;; Check if last key was ESC and if so try to reread it as a function key.
779 ;; But only if there are characters to read during a very short time.
780 ;; Returns the last event, if any.
781 (defun vip-envelop-ESC-key ()
782 (let ((event last-input-event
)
785 (if (vip-ESC-event-p event)
787 (if (vip-fast-keysequence-p)
789 (let (minor-mode-map-alist)
790 (vip-set-unread-command-events event)
793 (ad-get-orig-definition 'read-key-sequence) nil))
795 ;; If keyseq translates into something that still has ESC
796 ;; at the beginning, separate ESC from the rest of the seq.
797 ;; In XEmacs we check for events that are keypress meta-key
798 ;; and convert them into [escape key]
800 ;; This is needed for the following reason:
801 ;; If ESC is the first symbol, we interpret it as if the
802 ;; user typed ESC and then quickly some other symbols.
803 ;; If ESC is not the first one, then the key sequence
804 ;; entered was apparently translated into a function key or
805 ;; something (e.g., one may have
806 ;; (define-key function-key-map "\e[192z" [f11])
807 ;; which would translate the escape-sequence generated by
808 ;; f11 in an xterm window into the symbolic key f11.
810 ;; If `first-key' is not an ESC event, we make it into the
811 ;; last-command-event in order to pretend that this key was
812 ;; pressed. This is needed to allow arrow keys to be bound to
813 ;; macros. Otherwise, vip-exec-mapped-kbd-macro will think that
814 ;; the last event was ESC and so it'll execute whatever is
815 ;; bound to ESC. (Viper macros can't be bound to
817 (let* ((first-key (elt keyseq 0))
818 (key-mod (event-modifiers first-key)))
819 (cond ((vip-ESC-event-p first-key)
820 ;; put keys following ESC on the unread list
821 ;; and return ESC as the key-sequence
822 (vip-set-unread-command-events (subseq keyseq 1))
823 (setq last-input-event event
824 keyseq (if vip-emacs-p
826 (vector (character-to-event ?\e)))))
828 (key-press-event-p first-key)
829 (equal '(meta) key-mod))
830 (vip-set-unread-command-events
832 (character-to-event (event-key first-key)))
834 (setq last-input-event event
835 keyseq (vector (character-to-event ?\e))))
837 (setq last-command-event (vip-copy-event first-key)))
841 ;; this is escape event with nothing after it
842 ;; put in unread-command-event and then re-read
843 (vip-set-unread-command-events event)
845 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
847 ;; not an escape event
848 (setq keyseq (vector event)))
853 ;; Listen to ESC key.
854 ;; If a sequence of keys starting with ESC is issued with very short delays,
855 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
856 (defun vip-intercept-ESC-key ()
857 "Function that implements ESC key in Viper emulation of Vi."
859 (let ((cmd (or (key-binding (vip-envelop-ESC-key))
860 '(lambda () (interactive) (error "")))))
862 ;; call the actual function to execute ESC (if no other symbols followed)
863 ;; or the key bound to the ESC sequence (if the sequence was issued
864 ;; with very short delay between characters.
865 (if (eq cmd 'vip-intercept-ESC-key)
867 (cond ((eq vip-current-state 'vi-state)
869 ((eq vip-current-state 'insert-state)
870 'vip-exit-insert-state)
871 ((eq vip-current-state 'replace-state)
872 'vip-replace-state-exit-cmd)
873 (t 'vip-change-state-to-vi)
875 (call-interactively cmd)))
880 ;; prefix argument for Vi mode
882 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
883 ;; represents the numeric value of the prefix argument and COM represents
884 ;; command prefix such as "c", "d", "m" and "y".
886 ;; Get value part of prefix-argument ARG.
887 (defsubst vip-p-val (arg)
890 (if (or (null (car arg)) (equal (car arg) '(nil)))
894 ;; Get raw value part of prefix-argument ARG.
895 (defsubst vip-P-val (arg)
896 (cond ((consp arg) (car arg))
899 ;; Get com part of prefix-argument ARG.
900 (defsubst vip-getcom (arg)
901 (cond ((null arg) nil)
902 ((consp arg) (cdr arg))
905 ;; Get com part of prefix-argument ARG and modify it.
906 (defun vip-getCom (arg)
907 (let ((com (vip-getcom arg)))
908 (cond ((equal com ?c) ?C)
914 ;; Compute numeric prefix arg value.
915 ;; Invoked by EVENT. COM is the command part obtained so far.
916 (defun vip-prefix-arg-value (event com)
919 (while (and (vip-characterp event) (>= event ?0) (<= event ?9))
920 (setq value (+ (* (if (integerp value) value 0) 10) (- event ?0)))
921 (setq event (vip-read-event-convert-to-char)))
923 (setq prefix-arg value)
924 (if com (setq prefix-arg (cons prefix-arg com)))
926 (vip-describe-arg prefix-arg)
927 (setq event (vip-read-event-convert-to-char)))
929 (if (or com (and (not (eq vip-current-state 'vi-state))
930 ;; make sure it is a Vi command
931 (vip-characterp event) (vip-vi-command-p event)
933 ;; If appears to be one of the vi commands,
934 ;; then execute it with funcall and clear prefix-arg in order to not
935 ;; confuse subsequent commands
937 ;; last-command-char is the char we want emacs to think was typed
938 ;; last. If com is not nil, the vip-digit-argument command was called
939 ;; from within vip-prefix-arg command, such as `d', `w', etc., i.e.,
940 ;; the user typed, say, d2. In this case, `com' would be `d', `w',
942 ;; If vip-digit-argument was invoked by vip-escape-to-vi (which is
943 ;; indicated by the fact that the current state is not vi-state),
944 ;; then `event' represents the vi command to be executed (e.g., `d',
945 ;; `w', etc). Again, last-command-char must make emacs believe that
946 ;; this is the command we typed.
947 (setq last-command-char (or com event))
948 (setq func (vip-exec-form-in-vi
949 (` (key-binding (char-to-string (, event))))))
950 (funcall func prefix-arg)
951 (setq prefix-arg nil))
952 ;; some other command -- let emacs do it in its own way
953 (vip-set-unread-command-events event))
957 ;; Vi operator as prefix argument."
958 (defun vip-prefix-arg-com (char value com)
960 cmd-info mv-or-digit-cmd)
963 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
964 vip-buffer-search-char)))
966 ;; this means that we already have a command character, so we
967 ;; construct a com list and exit while. however, if char is "
970 ;; new com is (CHAR . OLDCOM)
971 (if (memq char '(?# ?\")) (error ""))
972 (setq com (cons char com))
974 ;; If com is nil we set com as char, and read more. Again, if char
975 ;; is ", we read the name of register and store it in vip-use-register.
976 ;; if char is !, =, or #, a complete com is formed so we exit the
978 (cond ((memq char '(?! ?=))
980 (setq char (read-char))
983 ;; read a char and encode it as com
984 (setq com (+ 128 (read-char)))
985 (setq char (read-char)))
987 (let ((reg (read-char)))
988 (if (vip-valid-register reg)
989 (setq vip-use-register reg)
991 (setq char (read-char))))
994 (setq char (read-char))))))
997 ;; `com' is a single char, so we construct the command argument
998 ;; and if `char' is `?', we describe the arg; otherwise
999 ;; we prepare the command that will be executed at the end.
1001 (setq cmd-info (cons value com))
1003 (vip-describe-arg cmd-info)
1004 (setq char (read-char)))
1005 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
1006 ;; execute it at the very end
1007 (or (vip-movement-command-p char)
1008 (vip-digit-command-p char)
1009 (vip-regsuffix-command-p char)
1011 (setq mv-or-digit-cmd
1012 (vip-exec-form-in-vi
1013 (` (key-binding (char-to-string (, char)))))))
1015 ;; as com is non-nil, this means that we have a command to execute
1016 (if (memq (car com) '(?r ?R))
1017 ;; execute apropriate region command.
1018 (let ((char (car com)) (com (cdr com)))
1019 (setq prefix-arg (cons value com))
1020 (if (= char ?r) (vip-region prefix-arg)
1021 (vip-Region prefix-arg))
1023 (setq prefix-arg nil))
1024 ;; otherwise, reset prefix arg and call appropriate command
1025 (setq value (if (null value) 1 value))
1026 (setq prefix-arg nil)
1027 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1028 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1029 ((equal com '(?d . ?y)) (vip-yank-defun))
1030 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1031 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1032 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1033 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1034 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1039 (setq last-command-char char)
1040 (setq last-command-event
1042 (if vip-xemacs-p (character-to-event char) char)))
1044 (funcall mv-or-digit-cmd cmd-info)
1049 (defun vip-describe-arg (arg)
1051 (setq val (vip-P-val arg)
1052 com (vip-getcom arg))
1055 (message "Value is nil, and command is nil")
1056 (message "Value is nil, and command is `%c'" com))
1058 (message "Value is `%d', and command is nil" val)
1059 (message "Value is `%d', and command is `%c'" val com)))))
1061 (defun vip-digit-argument (arg)
1062 "Begin numeric argument for the next command."
1064 (vip-leave-region-active)
1065 (vip-prefix-arg-value
1066 last-command-char (if (consp arg) (cdr arg) nil)))
1068 (defun vip-command-argument (arg)
1069 "Accept a motion command as an argument."
1071 (let ((vip-inside-command-argument-action t))
1075 (cond ((null arg) nil)
1076 ((consp arg) (car arg))
1077 ((integerp arg) arg)
1078 (t (error vip-InvalidCommandArgument)))
1079 (cond ((null arg) nil)
1080 ((consp arg) (cdr arg))
1081 ((integerp arg) nil)
1082 (t (error vip-InvalidCommandArgument))))
1083 (quit (setq vip-use-register nil)
1084 (signal 'quit nil)))
1085 (vip-deactivate-mark)))
1088 ;; repeat last destructive command
1090 ;; Append region to text in register REG.
1091 ;; START and END are buffer positions indicating what to append.
1092 (defsubst vip-append-to-register (reg start end)
1093 (set-register reg (concat (if (stringp (get-register reg))
1094 (get-register reg) "")
1095 (buffer-substring start end))))
1097 ;; Saves last inserted text for possible use by vip-repeat command.
1098 (defun vip-save-last-insertion (beg end)
1099 (setq vip-last-insertion (buffer-substring beg end))
1100 (or (< (length vip-d-com) 5)
1101 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
1102 (or (null vip-command-ring)
1103 (ring-empty-p vip-command-ring)
1105 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
1107 ;; del most recent elt, if identical to the second most-recent
1108 (vip-cleanup-ring vip-command-ring)))
1111 (defsubst vip-yank-last-insertion ()
1112 "Inserts the text saved by the previous vip-save-last-insertion command."
1114 (insert vip-last-insertion)
1118 ;; define functions to be executed
1120 ;; invoked by the `C' command
1121 (defun vip-exec-change (m-com com)
1122 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1123 (set-marker vip-com-point (point) (current-buffer)))
1124 ;; handle C cmd at the eol and at eob.
1125 (if (or (and (eolp) (= vip-com-point (point)))
1126 (= vip-com-point (point-max)))
1128 (insert " ")(backward-char 1)))
1129 (if (= vip-com-point (point))
1130 (vip-forward-char-carefully))
1132 (vip-change vip-com-point (point))
1133 (vip-change-subr vip-com-point (point))))
1135 ;; this is invoked by vip-substitute-line
1136 (defun vip-exec-Change (m-com com)
1138 (set-mark vip-com-point)
1139 (vip-enlarge-region (mark t) (point))
1140 (if vip-use-register
1142 (cond ((vip-valid-register vip-use-register '(letter digit))
1143 ;;(vip-valid-register vip-use-register '(letter)
1145 vip-use-register (mark t) (point) nil))
1146 ((vip-valid-register vip-use-register '(Letter))
1147 (vip-append-to-register
1148 (downcase vip-use-register) (mark t) (point)))
1149 (t (setq vip-use-register nil)
1150 (error vip-InvalidRegister vip-use-register)))
1151 (setq vip-use-register nil)))
1152 (delete-region (mark t) (point)))
1154 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
1156 (defun vip-exec-delete (m-com com)
1157 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1158 (set-marker vip-com-point (point) (current-buffer)))
1159 (if vip-use-register
1161 (cond ((vip-valid-register vip-use-register '(letter digit))
1162 ;;(vip-valid-register vip-use-register '(letter))
1164 vip-use-register vip-com-point (point) nil))
1165 ((vip-valid-register vip-use-register '(Letter))
1166 (vip-append-to-register
1167 (downcase vip-use-register) vip-com-point (point)))
1168 (t (setq vip-use-register nil)
1169 (error vip-InvalidRegister vip-use-register)))
1170 (setq vip-use-register nil)))
1172 (if (eq last-command 'd-command) 'kill-region nil))
1173 (kill-region vip-com-point (point))
1174 (setq this-command 'd-command)
1175 (if vip-ex-style-motion
1176 (if (and (eolp) (not (bolp))) (backward-char 1))))
1178 (defun vip-exec-Delete (m-com com)
1180 (set-mark vip-com-point)
1181 (vip-enlarge-region (mark t) (point))
1182 (if vip-use-register
1184 (cond ((vip-valid-register vip-use-register '(letter digit))
1185 ;;(vip-valid-register vip-use-register '(letter))
1187 vip-use-register (mark t) (point) nil))
1188 ((vip-valid-register vip-use-register '(Letter))
1189 (vip-append-to-register
1190 (downcase vip-use-register) (mark t) (point)))
1191 (t (setq vip-use-register nil)
1192 (error vip-InvalidRegister vip-use-register)))
1193 (setq vip-use-register nil)))
1195 (if (eq last-command 'D-command) 'kill-region nil))
1196 (kill-region (mark t) (point))
1197 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
1198 (back-to-indentation))
1200 (defun vip-exec-yank (m-com com)
1201 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1202 (set-marker vip-com-point (point) (current-buffer)))
1203 (if vip-use-register
1205 (cond ((vip-valid-register vip-use-register '(letter digit))
1206 ;; (vip-valid-register vip-use-register '(letter))
1208 vip-use-register vip-com-point (point) nil))
1209 ((vip-valid-register vip-use-register '(Letter))
1210 (vip-append-to-register
1211 (downcase vip-use-register) vip-com-point (point)))
1212 (t (setq vip-use-register nil)
1213 (error vip-InvalidRegister vip-use-register)))
1214 (setq vip-use-register nil)))
1215 (setq last-command nil)
1216 (copy-region-as-kill vip-com-point (point))
1217 (goto-char vip-com-point))
1219 (defun vip-exec-Yank (m-com com)
1221 (set-mark vip-com-point)
1222 (vip-enlarge-region (mark t) (point))
1223 (if vip-use-register
1225 (cond ((vip-valid-register vip-use-register '(letter digit))
1227 vip-use-register (mark t) (point) nil))
1228 ((vip-valid-register vip-use-register '(Letter))
1229 (vip-append-to-register
1230 (downcase vip-use-register) (mark t) (point)))
1231 (t (setq vip-use-register nil)
1232 (error vip-InvalidRegister vip-use-register)))
1233 (setq vip-use-register nil)))
1234 (setq last-command nil)
1235 (copy-region-as-kill (mark t) (point)))
1236 (vip-deactivate-mark)
1237 (goto-char vip-com-point))
1239 (defun vip-exec-bang (m-com com)
1241 (set-mark vip-com-point)
1242 (vip-enlarge-region (mark t) (point))
1243 (shell-command-on-region
1246 (setq vip-last-shell-com
1247 (vip-read-string-with-history
1251 (car vip-shell-history)
1256 (defun vip-exec-equals (m-com com)
1258 (set-mark vip-com-point)
1259 (vip-enlarge-region (mark t) (point))
1260 (if (> (mark t) (point)) (exchange-point-and-mark))
1261 (indent-region (mark t) (point) nil)))
1263 (defun vip-exec-shift (m-com com)
1265 (set-mark vip-com-point)
1266 (vip-enlarge-region (mark t) (point))
1267 (if (> (mark t) (point)) (exchange-point-and-mark))
1268 (indent-rigidly (mark t) (point)
1271 (- vip-shift-width))))
1272 ;; return point to where it was before shift
1273 (goto-char vip-com-point))
1275 ;; this is needed because some commands fake com by setting it to ?r, which
1276 ;; denotes repeated insert command.
1277 (defsubst vip-exec-dummy (m-com com)
1280 (defun vip-exec-buffer-search (m-com com)
1281 (setq vip-s-string (buffer-substring (point) vip-com-point))
1282 (setq vip-s-forward t)
1283 (setq vip-search-history (cons vip-s-string vip-search-history))
1284 (vip-search vip-s-string vip-s-forward 1))
1286 (defvar vip-exec-array (make-vector 128 nil))
1288 ;; Using a dispatch array allows adding functions like buffer search
1289 ;; without affecting other functions. Buffer search can now be bound
1290 ;; to any character.
1292 (aset vip-exec-array ?c 'vip-exec-change)
1293 (aset vip-exec-array ?C 'vip-exec-Change)
1294 (aset vip-exec-array ?d 'vip-exec-delete)
1295 (aset vip-exec-array ?D 'vip-exec-Delete)
1296 (aset vip-exec-array ?y 'vip-exec-yank)
1297 (aset vip-exec-array ?Y 'vip-exec-Yank)
1298 (aset vip-exec-array ?r 'vip-exec-dummy)
1299 (aset vip-exec-array ?! 'vip-exec-bang)
1300 (aset vip-exec-array ?< 'vip-exec-shift)
1301 (aset vip-exec-array ?> 'vip-exec-shift)
1302 (aset vip-exec-array ?= 'vip-exec-equals)
1306 ;; This function is called by various movement commands to execute a
1307 ;; destructive command on the region specified by the movement command. For
1308 ;; instance, if the user types cw, then the command vip-forward-word will
1309 ;; call vip-execute-com to execute vip-exec-change, which eventually will
1310 ;; call vip-change to invoke the replace mode on the region.
1312 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
1313 ;; vip-d-com for later use by vip-repeat.
1314 (defun vip-execute-com (m-com val com)
1315 (let ((reg vip-use-register))
1316 ;; this is the special command `#'
1318 (vip-special-prefix-com (- com 128))
1319 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
1321 (error "%c: %s" com vip-InvalidViCommand)
1322 (funcall fn m-com com))))
1323 (if (vip-dotable-command-p com)
1324 (vip-set-destructive-command
1326 (if (memq com (list ?c ?C ?!)) (- com) com)
1331 (defun vip-repeat (arg)
1332 "Re-execute last destructive command.
1333 Use the info in vip-d-com, which has the form
1334 \(com val ch reg inserted-text command-keys\),
1335 where `com' is the command to be re-executed, `val' is the
1336 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1337 if it exists, it is the name of the register for `com'.
1338 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
1340 (let ((save-point (point)) ; save point before repeating prev cmd
1341 ;; Pass along that we are repeating a destructive command
1342 ;; This tells vip-set-destructive-command not to update
1344 (vip-intermediate-command 'vip-repeat))
1345 (if (eq last-command 'vip-undo)
1346 ;; if the last command was vip-undo, then undo-more
1348 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
1349 ;; its prefix value is used as new prefix value for the command.
1350 (let ((m-com (car vip-d-com))
1351 (val (vip-P-val arg))
1352 (com (nth 2 vip-d-com))
1353 (reg (nth 3 vip-d-com)))
1354 (if (null val) (setq val (nth 1 vip-d-com)))
1355 (if (null m-com) (error "No previous command to repeat."))
1356 (setq vip-use-register reg)
1357 (if (nth 4 vip-d-com) ; text inserted by command
1358 (setq vip-last-insertion (nth 4 vip-d-com)
1359 vip-d-char (nth 4 vip-d-com)))
1360 (funcall m-com (cons val com))
1361 (if (and vip-keep-point-on-repeat (< save-point (point)))
1362 (goto-char save-point)) ; go back to before repeat.
1363 (if (and (eolp) (not (bolp)))
1366 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
1367 ;; If the prev cmd was rotating the command ring, this means that `.' has
1368 ;; just executed a command from that ring. So, push it on the ring again.
1369 ;; If we are just executing previous command , then don't push vip-d-com
1370 ;; because vip-d-com is not fully constructed in this case (its keys and
1371 ;; the inserted text may be nil). Besides, in this case, the command
1372 ;; executed by `.' is already on the ring.
1373 (if (eq last-command 'vip-display-current-destructive-command)
1374 (vip-push-onto-ring vip-d-com 'vip-command-ring))
1375 (vip-deactivate-mark)
1378 (defun vip-repeat-from-history ()
1379 "Repeat a destructive command from history.
1380 Doesn't change vip-command-ring in any way, so `.' will work as before
1381 executing this command.
1382 This command is supposed to be bound to a two-character Vi macro where
1383 the second character is a digit 0 to 9. The digit indicates which
1384 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1385 invokes the command before that, etc."
1387 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
1388 (idx (cond (vip-this-kbd-macro
1390 (symbol-name (elt vip-this-kbd-macro 1))))
1393 (vip-d-com vip-d-com))
1395 (or (and (numberp num) (<= 0 num) (<= num 9))
1400 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1402 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
1403 (setq num (1- num)))
1406 (vip-special-ring-rotate1 vip-command-ring 1)
1407 (setq num (1+ num)))
1411 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1412 ;; The chars that can follow `#' are determined by vip-hash-command-p
1413 (defun vip-special-prefix-com (char)
1415 (downcase-region (min vip-com-point (point))
1416 (max vip-com-point (point))))
1418 (upcase-region (min vip-com-point (point))
1419 (max vip-com-point (point))))
1421 (push-mark vip-com-point t)
1422 (vip-global-execute))
1424 (push-mark vip-com-point t)
1426 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
1427 (t (error "#%c: %s" char vip-InvalidViCommand))))
1433 "Undo previous change."
1436 (let ((modified (buffer-modified-p))
1437 (before-undo-pt (point-marker))
1438 (after-change-functions after-change-functions)
1439 undo-beg-posn undo-end-posn)
1441 ;; no need to remove this hook, since this var has scope inside a let.
1442 (add-hook 'after-change-functions
1443 '(lambda (beg end len)
1444 (setq undo-beg-posn beg
1445 undo-end-posn (or end beg))))
1449 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1450 undo-end-posn (or undo-end-posn undo-beg-posn))
1452 (goto-char undo-beg-posn)
1454 (if (and vip-keep-point-on-undo
1455 (pos-visible-in-window-p before-undo-pt))
1457 (push-mark (point-marker) t)
1458 (vip-sit-for-short 300)
1459 (goto-char undo-end-posn)
1460 (vip-sit-for-short 300)
1461 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
1462 (> (abs (- undo-end-posn before-undo-pt)) 1))
1463 (goto-char before-undo-pt)
1464 (goto-char undo-beg-posn)))
1465 (push-mark before-undo-pt t))
1466 (if (and (eolp) (not (bolp))) (backward-char 1))
1467 (if (not modified) (set-buffer-modified-p t)))
1468 (setq this-command 'vip-undo))
1470 ;; Continue undoing previous changes.
1471 (defun vip-undo-more ()
1472 (message "undo more!")
1476 (message "No further undo information in this buffer")))
1477 (if (and (eolp) (not (bolp))) (backward-char 1))
1478 (setq this-command 'vip-undo))
1480 ;; The following two functions are used to set up undo properly.
1481 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1482 ;; they are undone all at once.
1483 (defun vip-adjust-undo ()
1484 (let ((inhibit-quit t)
1486 (setq vip-undo-needs-adjustment nil)
1487 (if (listp buffer-undo-list)
1488 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
1490 (setq tmp2 (cdr tmp)) ; the part after mark
1492 ;; cut tail from buffer-undo-list temporarily by direct
1493 ;; manipulation with pointers in buffer-undo-list
1496 (setq buffer-undo-list (delq nil buffer-undo-list))
1497 (setq buffer-undo-list
1498 (delq vip-buffer-undo-list-mark buffer-undo-list))
1499 ;; restore tail of buffer-undo-list
1500 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1501 (setq buffer-undo-list (delq nil buffer-undo-list))))))
1504 (defun vip-set-complex-command-for-undo ()
1505 (if (listp buffer-undo-list)
1506 (if (not vip-undo-needs-adjustment)
1507 (let ((inhibit-quit t))
1508 (setq buffer-undo-list
1509 (cons vip-buffer-undo-list-mark buffer-undo-list))
1510 (setq vip-undo-needs-adjustment t)))))
1515 (defun vip-display-current-destructive-command ()
1516 (let ((text (nth 4 vip-d-com))
1517 (keys (nth 5 vip-d-com))
1520 (setq this-command 'vip-display-current-destructive-command)
1522 (message " `.' runs %s%s"
1523 (concat "`" (vip-array-to-string keys) "'")
1524 (vip-abbreviate-string text max-text-len
1525 " inserting `" "'" " ......."))
1529 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
1530 ;; or in some other way (non-interactively).
1531 (defun vip-set-destructive-command (list)
1532 (or (eq vip-intermediate-command 'vip-repeat)
1534 (setq vip-d-com list)
1535 (setcar (nthcdr 5 vip-d-com)
1536 (vip-array-to-string (this-command-keys)))
1537 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
1539 (defun vip-prev-destructive-command (next)
1540 "Find previous destructive command in the history of destructive commands.
1541 With prefix argument, find next destructive command."
1543 (let (cmd vip-intermediate-command)
1544 (if (eq last-command 'vip-display-current-destructive-command)
1545 ;; repeated search through command history
1546 (setq vip-intermediate-command 'repeating-display-destructive-command)
1547 ;; first search through command history--set temp ring
1548 (setq vip-temp-command-ring (copy-list vip-command-ring)))
1550 (vip-special-ring-rotate1 vip-temp-command-ring 1)
1551 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
1554 (setq vip-d-com cmd))
1555 (vip-display-current-destructive-command)))
1557 (defun vip-next-destructive-command ()
1558 "Find next destructive command in the history of destructive commands."
1560 (vip-prev-destructive-command 'next))
1562 (defun vip-insert-prev-from-insertion-ring (arg)
1563 "Cycle through insertion ring in the direction of older insertions.
1564 Undoes previous insertion and inserts new.
1565 With prefix argument, cycles in the direction of newer elements.
1566 In minibuffer, this command executes whatever the invocation key is bound
1567 to in the global map, instead of cycling through the insertion ring."
1569 (let (vip-intermediate-command)
1570 (if (eq last-command 'vip-insert-from-insertion-ring)
1571 (progn ; repeated search through insertion history
1572 (setq vip-intermediate-command 'repeating-insertion-from-ring)
1573 (if (eq vip-current-state 'replace-state)
1575 (if vip-last-inserted-string-from-insertion-ring
1576 (backward-delete-char
1577 (length vip-last-inserted-string-from-insertion-ring))))
1579 ;;first search through insertion history
1580 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
1581 (setq this-command 'vip-insert-from-insertion-ring)
1582 ;; so that things will be undone properly
1583 (setq buffer-undo-list (cons nil buffer-undo-list))
1584 (setq vip-last-inserted-string-from-insertion-ring
1585 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
1587 ;; this change of vip-intermediate-command must come after
1588 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
1590 (setq vip-intermediate-command nil)
1591 (if vip-last-inserted-string-from-insertion-ring
1592 (insert vip-last-inserted-string-from-insertion-ring))
1595 (defun vip-insert-next-from-insertion-ring ()
1596 "Cycle through insertion ring in the direction of older insertions.
1597 Undo previous insertion and inserts new."
1599 (vip-insert-prev-from-insertion-ring 'next))
1602 ;; some region utilities
1604 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1605 (defun vip-add-newline-at-eob-if-necessary ()
1608 ;; make sure all lines end with newline, unless in the minibuffer or
1609 ;; when requested otherwise (require-final-newline is nil)
1612 require-final-newline
1613 (not (vip-is-in-minibuffer))
1614 (not buffer-read-only))
1617 (defun vip-yank-defun ()
1619 (copy-region-as-kill (point) (mark t)))
1621 ;; Enlarge region between BEG and END.
1622 (defun vip-enlarge-region (beg end)
1623 (or beg (setq beg end)) ; if beg is nil, set to end
1624 (or end (setq end beg)) ; if end is nil, set to beg
1627 (progn (goto-char beg) (set-mark end))
1631 (exchange-point-and-mark)
1632 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1633 (if (not (eobp)) (beginning-of-line))
1634 (if (> beg end) (exchange-point-and-mark)))
1637 ;; Quote region by each line with a user supplied string.
1638 (defun vip-quote-region ()
1639 (setq vip-quote-string
1640 (vip-read-string-with-history
1643 'vip-quote-region-history
1645 (vip-enlarge-region (point) (mark t))
1646 (if (> (point) (mark t)) (exchange-point-and-mark))
1647 (insert vip-quote-string)
1650 (while (and (< (point) (mark t)) (bolp))
1651 (insert vip-quote-string)
1655 ;; Tells whether BEG is on the same line as END.
1656 ;; If one of the args is nil, it'll return nil.
1657 (defun vip-same-line (beg end)
1658 (let ((selective-display nil)
1661 (if (and beg end (> beg end))
1666 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
1669 ;; This 'if' is needed because Emacs treats the next empty line
1670 ;; as part of the previous line.
1671 (if (= (vip-line-pos 'start) end)
1673 (<= (+ incr (count-lines beg end)) 1))))
1677 ;; Check if the string ends with a newline.
1678 (defun vip-end-with-a-newline-p (string)
1679 (or (string= string "")
1680 (= (vip-seq-last-elt string) ?\n)))
1682 (defun vip-tmp-insert-at-eob (msg)
1683 (let ((savemax (point-max)))
1687 (goto-char savemax) (delete-region (point) (point-max))
1692 ;;; Minibuffer business
1694 (defsubst vip-set-minibuffer-style ()
1695 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
1698 (defun vip-minibuffer-setup-sentinel ()
1699 (let ((hook (if vip-vi-style-in-minibuffer
1700 'vip-change-state-to-insert
1701 'vip-change-state-to-emacs)))
1705 ;; Interpret last event in the local map
1706 (defun vip-exit-minibuffer ()
1709 (setq command (local-key-binding (char-to-string last-command-char)))
1711 (command-execute command)
1712 (exit-minibuffer))))
1715 ;;; Reading string with history
1717 (defun vip-read-string-with-history (prompt &optional initial
1718 history-var default keymap)
1719 ;; Read string, prompting with PROMPT and inserting the INITIAL
1720 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
1721 ;; input is an empty string. Use KEYMAP, if given, or the
1722 ;; minibuffer-local-map.
1723 ;; Default value is displayed until the user types something in the
1725 (let ((minibuffer-setup-hook
1727 (if (stringp initial)
1729 ;; don't wait if we have unread events or in kbd macro
1730 (or unread-command-events
1735 (vip-minibuffer-setup-sentinel)))
1740 (setq keymap (or keymap minibuffer-local-map)
1741 initial (or initial "")
1742 temp-msg (if default
1743 (format "(default: %s) " default)
1746 (setq vip-incomplete-ex-cmd nil)
1747 (setq val (read-from-minibuffer prompt
1748 (concat temp-msg initial val padding)
1749 keymap nil history-var))
1750 (setq minibuffer-setup-hook nil
1751 padding (vip-array-to-string (this-command-keys))
1753 ;; the following tries to be smart about what to put in history
1754 (if (not (string= val (car (eval history-var))))
1755 (set history-var (cons val (eval history-var))))
1756 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
1757 (string= (nth 0 (eval history-var)) ""))
1758 (set history-var (cdr (eval history-var))))
1759 ;; If the user enters nothing but the prev cmd wasn't vip-ex,
1760 ;; vip-command-argument, or `! shell-command', this probably means
1761 ;; that the user typed something then erased. Return "" in this case, not
1762 ;; the default---the default is too confusing in this case.
1763 (cond ((and (string= val "")
1764 (not (string= prompt "!")) ; was a `! shell-command'
1765 (not (memq last-command
1767 vip-command-argument
1771 ((string= val "") (or default ""))
1777 ;; insertion commands
1779 ;; Called when state changes from Insert Vi command mode.
1780 ;; Repeats the insertion command if Insert state was entered with prefix
1782 (defun vip-repeat-insert-command ()
1783 (let ((i-com (car vip-d-com))
1784 (val (nth 1 vip-d-com))
1785 (char (nth 2 vip-d-com)))
1786 (if (and val (> val 1)) ; first check that val is non-nil
1788 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
1790 (setq vip-d-com (list i-com val char nil nil nil))
1793 (defun vip-insert (arg)
1794 "Insert before point."
1796 (vip-set-complex-command-for-undo)
1797 (let ((val (vip-p-val arg))
1798 (com (vip-getcom arg)))
1799 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
1801 (vip-loop val (vip-yank-last-insertion))
1802 (vip-change-state-to-insert))))
1804 (defun vip-append (arg)
1805 "Append after point."
1807 (vip-set-complex-command-for-undo)
1808 (let ((val (vip-p-val arg))
1809 (com (vip-getcom arg)))
1810 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
1811 (if (not (eolp)) (forward-char))
1813 (vip-loop val (vip-yank-last-insertion))
1814 (vip-change-state-to-insert))))
1816 (defun vip-Append (arg)
1817 "Append at end of line."
1819 (vip-set-complex-command-for-undo)
1820 (let ((val (vip-p-val arg))
1821 (com (vip-getcom arg)))
1822 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
1825 (vip-loop val (vip-yank-last-insertion))
1826 (vip-change-state-to-insert))))
1828 (defun vip-Insert (arg)
1829 "Insert before first non-white."
1831 (vip-set-complex-command-for-undo)
1832 (let ((val (vip-p-val arg))
1833 (com (vip-getcom arg)))
1834 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
1835 (back-to-indentation)
1837 (vip-loop val (vip-yank-last-insertion))
1838 (vip-change-state-to-insert))))
1840 (defun vip-open-line (arg)
1843 (vip-set-complex-command-for-undo)
1844 (let ((val (vip-p-val arg))
1845 (com (vip-getcom arg)))
1846 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
1847 (let ((col (current-indentation)))
1856 (if vip-electric-mode
1857 (indent-according-to-mode)
1860 (vip-yank-last-insertion)))
1866 (if vip-electric-mode
1867 (indent-according-to-mode)
1869 (vip-change-state-to-insert)))))
1871 (defun vip-Open-line (arg)
1874 (vip-set-complex-command-for-undo)
1875 (let ((val (vip-p-val arg))
1876 (com (vip-getcom arg)))
1877 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
1878 (let ((col (current-indentation)))
1887 (if vip-electric-mode
1888 (indent-according-to-mode)
1891 (vip-yank-last-insertion)))
1897 (if vip-electric-mode
1898 (indent-according-to-mode)
1901 (vip-change-state-to-insert)))))
1903 (defun vip-open-line-at-point (arg)
1904 "Open line at point."
1906 (vip-set-complex-command-for-undo)
1907 (let ((val (vip-p-val arg))
1908 (com (vip-getcom arg)))
1909 (vip-set-destructive-command
1910 (list 'vip-open-line-at-point val ?r nil nil nil))
1915 (vip-yank-last-insertion)))
1917 (vip-change-state-to-insert))))
1919 (defun vip-substitute (arg)
1920 "Substitute characters."
1922 (let ((val (vip-p-val arg))
1923 (com (vip-getcom arg)))
1927 (vip-change-subr (mark t) (point))
1928 (vip-change (mark t) (point)))
1929 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
1932 (defun vip-substitute-line (arg)
1935 (vip-set-complex-command-for-undo)
1936 (vip-line (cons arg ?C)))
1938 ;; Prepare for replace
1939 (defun vip-start-replace ()
1940 (setq vip-began-as-replace t
1941 vip-sitting-in-replace t
1942 vip-replace-chars-to-delete 0
1943 vip-replace-chars-deleted 0)
1944 (vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
1945 (vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
1946 ;; this will get added repeatedly, but no harm
1947 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
1948 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
1949 (vip-move-marker-locally 'vip-last-posn-in-replace-region
1950 (vip-replace-start))
1952 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
1954 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
1955 ;; guard against a smartie who switched from R-replace to normal replace
1957 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
1958 (if overwrite-mode (overwrite-mode nil))
1962 ;; checks how many chars were deleted by the last change
1963 (defun vip-replace-mode-spy-before (beg end)
1964 (setq vip-replace-chars-deleted
1966 (max 0 (- end (vip-replace-end)))
1967 (max 0 (- (vip-replace-start) beg))
1970 ;; Invoked as an after-change-function to set up parameters of the last change
1971 (defun vip-replace-mode-spy-after (beg end length)
1972 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
1974 (setq vip-replace-chars-to-delete 0)
1975 (vip-move-marker-locally
1976 'vip-last-posn-in-replace-region (point)))
1978 (let (beg-col end-col real-end chars-to-delete)
1979 (setq real-end (min end (vip-replace-end)))
1982 (setq beg-col (current-column))
1983 (goto-char real-end)
1984 (setq end-col (current-column)))
1986 ;; If beg of change is outside the replacement region, then don't
1987 ;; delete anything in the repl region (set chars-to-delete to 0).
1989 ;; This works fine except that we have to take special care of
1990 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
1991 ;; sometimes simply shifts the repl region rightwards, without
1992 ;; deleting an equal amount of characters.
1994 ;; The reason why new-dabbrev.el causes this are this:
1995 ;; if one dinamically completes a partial word that starts before the
1996 ;; replacement region (but ends inside) then new-dabbrev.el first
1997 ;; moves cursor backwards, to the beginning of the word to be
1998 ;; completed (say, pt A). Then it inserts the
1999 ;; completed word and then deletes the old, incomplete part.
2000 ;; Since the complete word is inserted at position before the repl
2001 ;; region, the next If-statement would have set chars-to-delete to 0
2002 ;; unless we check for the current command, which must be
2005 ;; In fact, it might be also useful to have overlays for insert
2006 ;; regions as well, since this will let us capture the situation when
2007 ;; dabbrev-expand goes back past the insertion point to find the
2008 ;; beginning of the word to be expanded.
2009 (if (or (and (<= (vip-replace-start) beg)
2010 (<= beg (vip-replace-end)))
2011 (and (= length 0) (eq this-command 'dabbrev-expand)))
2012 (setq chars-to-delete
2013 (max (- end-col beg-col) (- real-end beg) 0))
2014 (setq chars-to-delete 0))
2016 ;; if beg = last change position, it means that we are within the
2017 ;; same command that does multiple changes. Moreover, it means
2018 ;; that we have two subsequent changes (insert/delete) that
2019 ;; complement each other.
2020 (if (= beg (marker-position vip-last-posn-in-replace-region))
2021 (setq vip-replace-chars-to-delete
2022 (- (+ chars-to-delete vip-replace-chars-to-delete)
2023 vip-replace-chars-deleted))
2024 (setq vip-replace-chars-to-delete chars-to-delete))
2026 (vip-move-marker-locally
2027 'vip-last-posn-in-replace-region
2028 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
2029 (or (marker-position vip-last-posn-in-replace-region)
2030 (vip-replace-start))
2033 (setq vip-replace-chars-to-delete
2035 (min vip-replace-chars-to-delete
2036 (- (vip-replace-end) vip-last-posn-in-replace-region)
2037 (- (vip-line-pos 'end) vip-last-posn-in-replace-region)
2042 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
2043 ;; posn is within the overlay.
2044 (defun vip-finish-change (posn)
2045 (vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
2046 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2047 (vip-remove-hook 'vip-post-command-hooks
2048 'vip-replace-state-post-command-sentinel)
2050 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2051 (vip-restore-cursor-color-after-replace)
2052 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2056 (>= posn (vip-replace-start))
2057 (< posn (vip-replace-end)))
2058 (delete-region posn (vip-replace-end)))
2061 (if (eq vip-current-state 'replace-state)
2062 (vip-downgrade-to-insert))
2063 ;; replace mode ended => nullify vip-last-posn-in-replace-region
2064 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
2065 (vip-hide-replace-overlay)
2066 (vip-refresh-mode-line)
2067 (vip-put-string-on-kill-ring vip-last-replace-region)
2070 ;; Make STRING be the first element of the kill ring.
2071 (defun vip-put-string-on-kill-ring (string)
2072 (setq kill-ring (cons string kill-ring))
2073 (if (> (length kill-ring) kill-ring-max)
2074 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2075 (setq kill-ring-yank-pointer kill-ring))
2077 (defun vip-finish-R-mode ()
2078 (vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2080 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2081 (vip-downgrade-to-insert))
2083 (defun vip-start-R-mode ()
2084 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2087 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
2089 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2090 ;; guard against a smartie who switched from R-replace to normal replace
2092 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel)
2097 (defun vip-replace-state-exit-cmd ()
2098 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2099 These keys are ESC, RET, and LineFeed"
2101 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2103 (vip-finish-change vip-last-posn-in-replace-region))
2105 (if (eq this-command 'vip-intercept-ESC-key)
2106 (setq com 'vip-exit-insert-state)
2107 (vip-set-unread-command-events last-input-char)
2108 (setq com (key-binding (read-key-sequence nil))))
2110 (condition-case conds
2111 (command-execute com)
2113 (vip-message-conditions conds)))
2115 (vip-hide-replace-overlay))
2117 (defun vip-replace-state-carriage-return ()
2118 "Implements carriage return in Viper replace state."
2120 ;; If Emacs start supporting overlay maps, as it currently supports
2121 ;; text-property maps, we could do away with vip-replace-minor-mode and
2122 ;; just have keymap attached to replace overlay. Then the "if part" of this
2123 ;; statement can be deleted.
2124 (if (or (< (point) (vip-replace-start))
2125 (> (point) (vip-replace-end)))
2126 (let (vip-replace-minor-mode com)
2127 (vip-set-unread-command-events last-input-char)
2128 (setq com (key-binding (read-key-sequence nil)))
2129 (condition-case conds
2130 (command-execute com)
2132 (vip-message-conditions conds))))
2133 (if (not vip-allow-multiline-replace-regions)
2134 (vip-replace-state-exit-cmd)
2135 (if (vip-same-line (point) (vip-replace-end))
2136 (vip-replace-state-exit-cmd)
2138 (vip-next-line-at-bol nil)))))
2141 ;; This is the function bound to 'R'---unlimited replace.
2142 ;; Similar to Emacs's own overwrite-mode.
2143 (defun vip-overwrite (arg)
2144 "Begin overwrite mode."
2146 (let ((val (vip-p-val arg))
2147 (com (vip-getcom arg)) (len))
2148 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
2151 ;; Viper saves inserted text in vip-last-insertion
2152 (setq len (length vip-last-insertion))
2154 (vip-loop val (vip-yank-last-insertion)))
2155 (setq last-command 'vip-overwrite)
2156 (vip-set-complex-command-for-undo)
2157 (vip-set-replace-overlay (point) (vip-line-pos 'end))
2158 (vip-change-state-to-replace)
2164 (defun vip-line (arg)
2165 (let ((val (car arg))
2167 (vip-move-marker-locally 'vip-com-point (point))
2169 (vip-next-line-carefully (1- val)))
2170 ;; this ensures that dd, cc, D, yy will do the right thing on the last
2171 ;; line of buffer when this line has no \n.
2172 (vip-add-newline-at-eob-if-necessary)
2173 (vip-execute-com 'vip-line val com))
2174 (if (and (eobp) (not (bobp))) (forward-line -1))
2177 (defun vip-yank-line (arg)
2178 "Yank ARG lines (in Vi's sense)."
2180 (let ((val (vip-p-val arg)))
2181 (vip-line (cons val ?Y))))
2186 (defun vip-region (arg)
2187 "Execute command on a region."
2189 (let ((val (vip-P-val arg))
2190 (com (vip-getcom arg)))
2191 (vip-move-marker-locally 'vip-com-point (point))
2192 (exchange-point-and-mark)
2193 (vip-execute-com 'vip-region val com)))
2195 (defun vip-Region (arg)
2196 "Execute command on a Region."
2198 (let ((val (vip-P-val arg))
2199 (com (vip-getCom arg)))
2200 (vip-move-marker-locally 'vip-com-point (point))
2201 (exchange-point-and-mark)
2202 (vip-execute-com 'vip-Region val com)))
2204 (defun vip-replace-char (arg)
2205 "Replace the following ARG chars by the character read."
2207 (if (and (eolp) (bolp)) (error "No character to replace here"))
2208 (let ((val (vip-p-val arg))
2209 (com (vip-getcom arg)))
2210 (vip-replace-char-subr com val)
2211 (if (and (eolp) (not (bolp))) (forward-char 1))
2212 (vip-set-destructive-command
2213 (list 'vip-replace-char val ?r nil vip-d-char nil))
2216 (defun vip-replace-char-subr (com arg)
2217 (let ((take-care-of-iso-accents
2218 (and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
2220 (setq char (if (equal com ?r)
2223 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
2224 ;; get European characters
2226 (iso-accents-mode 1)
2227 (vip-set-unread-command-events char)
2228 (setq char (aref (read-key-sequence nil) 0))
2229 (iso-accents-mode -1)))
2231 (setq vip-d-char char)
2232 (vip-loop (if (> arg 0) arg (- arg))
2233 (if (eq char ?\C-m) (insert "\n") (insert char)))
2234 (backward-char arg)))
2237 ;; basic cursor movement. j, k, l, h commands.
2239 (defun vip-forward-char (arg)
2240 "Move point right ARG characters (left if ARG negative).
2241 On reaching end of line, stop and signal error."
2243 (vip-leave-region-active)
2244 (let ((val (vip-p-val arg))
2245 (com (vip-getcom arg)))
2246 (if com (vip-move-marker-locally 'vip-com-point (point)))
2247 (if vip-ex-style-motion
2249 ;; the boundary condition check gets weird here because
2250 ;; forward-char may be the parameter of a delete, and 'dl' works
2251 ;; just like 'x' for the last char on a line, so we have to allow
2252 ;; the forward motion before the 'vip-execute-com', but, of
2253 ;; course, 'dl' doesn't work on an empty line, so we have to
2254 ;; catch that condition before 'vip-execute-com'
2255 (if (and (eolp) (bolp)) (error "") (forward-char val))
2256 (if com (vip-execute-com 'vip-forward-char val com))
2257 (if (eolp) (progn (backward-char 1) (error ""))))
2259 (if com (vip-execute-com 'vip-forward-char val com)))))
2261 (defun vip-backward-char (arg)
2262 "Move point left ARG characters (right if ARG negative).
2263 On reaching beginning of line, stop and signal error."
2265 (vip-leave-region-active)
2266 (let ((val (vip-p-val arg))
2267 (com (vip-getcom arg)))
2268 (if com (vip-move-marker-locally 'vip-com-point (point)))
2269 (if vip-ex-style-motion
2271 (if (bolp) (error "") (backward-char val))
2272 (if com (vip-execute-com 'vip-backward-char val com)))
2274 (if com (vip-execute-com 'vip-backward-char val com)))))
2276 ;; Like forward-char, but doesn't move at end of buffer.
2277 (defun vip-forward-char-carefully (&optional arg)
2278 (setq arg (or arg 1))
2279 (if (>= (point-max) (+ (point) arg))
2281 (goto-char (point-max))))
2283 ;; Like backward-char, but doesn't move at end of buffer.
2284 (defun vip-backward-char-carefully (&optional arg)
2285 (setq arg (or arg 1))
2286 (if (<= (point-min) (- (point) arg))
2288 (goto-char (point-min))))
2290 (defun vip-next-line-carefully (arg)
2299 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
2300 ;; for word movement. When executed with a destructive command, \n is
2301 ;; usually left untouched for the last word.
2302 ;; Viper uses syntax table to determine what is a word and what is a
2303 ;; separator. However, \n is always a separator. Also, if vip-syntax-preference
2304 ;; is 'vi, then `_' is part of the word.
2307 (defun vip-skip-separators (forward)
2310 (vip-skip-all-separators-forward 'within-line)
2311 (if (looking-at "\n")
2314 (vip-skip-all-separators-forward 'within-line))))
2315 (vip-skip-all-separators-backward 'within-line)
2317 (if (looking-at "\n")
2318 (vip-skip-all-separators-backward 'within-line)
2321 (defun vip-forward-word-kernel (val)
2323 (cond ((vip-looking-at-alpha)
2324 (vip-skip-alpha-forward "_")
2325 (vip-skip-separators t))
2326 ((vip-looking-at-separator)
2327 (vip-skip-separators t))
2328 ((not (vip-looking-at-alphasep))
2329 (vip-skip-nonalphasep-forward)
2330 (vip-skip-separators t)))
2331 (setq val (1- val))))
2333 ;; first search backward for pat. Then skip chars backwards using aux-pat
2334 (defun vip-fwd-skip (pat aux-pat lim)
2335 (if (and (save-excursion
2336 (re-search-backward pat lim t))
2337 (= (point) (match-end 0)))
2338 (goto-char (match-beginning 0)))
2339 (skip-chars-backward aux-pat lim)
2341 (vip-forward-char-carefully))
2345 (defun vip-forward-word (arg)
2348 (vip-leave-region-active)
2349 (let ((val (vip-p-val arg))
2350 (com (vip-getcom arg)))
2351 (if com (vip-move-marker-locally 'vip-com-point (point)))
2352 (vip-forward-word-kernel val)
2354 (cond ((memq com (list ?c (- ?c)))
2355 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
2356 ;; Yank words including the whitespace, but not newline
2357 ((memq com (list ?y (- ?y)))
2358 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
2359 ((vip-dotable-command-p com)
2360 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
2361 (vip-execute-com 'vip-forward-word val com)))))
2364 (defun vip-forward-Word (arg)
2365 "Forward word delimited by white characters."
2367 (vip-leave-region-active)
2368 (let ((val (vip-p-val arg))
2369 (com (vip-getcom arg)))
2370 (if com (vip-move-marker-locally 'vip-com-point (point)))
2373 (vip-skip-nonseparators 'forward)
2374 (vip-skip-separators t)))
2376 (cond ((memq com (list ?c (- ?c)))
2377 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
2378 ;; Yank words including the whitespace, but not newline
2379 ((memq com (list ?y (- ?y)))
2380 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
2381 ((vip-dotable-command-p com)
2382 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
2383 (vip-execute-com 'vip-forward-Word val com)))))
2386 ;; this is a bit different from Vi, but Vi's end of word
2387 ;; makes no sense whatsoever
2388 (defun vip-end-of-word-kernel ()
2389 (if (vip-end-of-word-p) (forward-char))
2390 (if (vip-looking-at-separator)
2391 (vip-skip-all-separators-forward))
2393 (cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
2394 ((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
2395 (vip-backward-char-carefully))
2397 (defun vip-end-of-word-p ()
2400 (cond ((vip-looking-at-alpha)
2402 (not (vip-looking-at-alpha)))
2403 ((not (vip-looking-at-alphasep))
2405 (vip-looking-at-alphasep))))))
2408 (defun vip-end-of-word (arg &optional careful)
2409 "Move point to end of current word."
2411 (vip-leave-region-active)
2412 (let ((val (vip-p-val arg))
2413 (com (vip-getcom arg)))
2414 (if com (vip-move-marker-locally 'vip-com-point (point)))
2415 (vip-loop val (vip-end-of-word-kernel))
2419 (vip-execute-com 'vip-end-of-word val com)))))
2421 (defun vip-end-of-Word (arg)
2422 "Forward to end of word delimited by white character."
2424 (vip-leave-region-active)
2425 (let ((val (vip-p-val arg))
2426 (com (vip-getcom arg)))
2427 (if com (vip-move-marker-locally 'vip-com-point (point)))
2430 (vip-end-of-word-kernel)
2431 (vip-skip-nonseparators 'forward)
2436 (vip-execute-com 'vip-end-of-Word val com)))))
2438 (defun vip-backward-word-kernel (val)
2441 (cond ((vip-looking-at-alpha)
2442 (vip-skip-alpha-backward "_"))
2443 ((vip-looking-at-separator)
2445 (vip-skip-separators nil)
2447 (cond ((vip-looking-at-alpha)
2448 (vip-skip-alpha-backward "_"))
2449 ((not (vip-looking-at-alphasep))
2450 (vip-skip-nonalphasep-backward))
2451 (t (forward-char))))
2452 ((not (vip-looking-at-alphasep))
2453 (vip-skip-nonalphasep-backward)))
2454 (setq val (1- val))))
2456 (defun vip-backward-word (arg)
2459 (vip-leave-region-active)
2460 (let ((val (vip-p-val arg))
2461 (com (vip-getcom arg)))
2464 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2466 (vip-move-marker-locally 'vip-com-point (point))
2467 (if i (forward-char))))
2468 (vip-backward-word-kernel val)
2469 (if com (vip-execute-com 'vip-backward-word val com))))
2471 (defun vip-backward-Word (arg)
2472 "Backward word delimited by white character."
2474 (vip-leave-region-active)
2475 (let ((val (vip-p-val arg))
2476 (com (vip-getcom arg)))
2479 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2481 (vip-move-marker-locally 'vip-com-point (point))
2482 (if i (forward-char))))
2485 (vip-skip-separators nil)
2486 (vip-skip-nonseparators 'backward)))
2487 (if com (vip-execute-com 'vip-backward-Word val com))))
2493 (defun vip-beginning-of-line (arg)
2494 "Go to beginning of line."
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 (beginning-of-line val)
2501 (if com (vip-execute-com 'vip-beginning-of-line val com))))
2503 (defun vip-bol-and-skip-white (arg)
2504 "Beginning of line at first non-white character."
2506 (vip-leave-region-active)
2507 (let ((val (vip-p-val arg))
2508 (com (vip-getcom arg)))
2509 (if com (vip-move-marker-locally 'vip-com-point (point)))
2510 (forward-to-indentation (1- val))
2511 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
2513 (defun vip-goto-eol (arg)
2514 "Go to end of line."
2516 (vip-leave-region-active)
2517 (let ((val (vip-p-val arg))
2518 (com (vip-getcom arg)))
2519 (if com (vip-move-marker-locally 'vip-com-point (point)))
2521 (if com (vip-execute-com 'vip-goto-eol val com))
2522 (if vip-ex-style-motion
2523 (if (and (eolp) (not (bolp))
2524 ;; a fix for vip-change-to-eol
2525 (not (equal vip-current-state 'insert-state)))
2530 (defun vip-goto-col (arg)
2531 "Go to ARG's column."
2533 (vip-leave-region-active)
2534 (let ((val (vip-p-val arg))
2535 (com (vip-getcom arg))
2537 (setq line-len (- (vip-line-pos 'end) (vip-line-pos 'start)))
2538 (if com (vip-move-marker-locally 'vip-com-point (point)))
2540 (forward-char (1- (min line-len val)))
2541 (while (> (current-column) (1- val))
2543 (if com (vip-execute-com 'vip-goto-col val com))
2546 (if (> val (current-column)) (error "")))
2550 (defun vip-next-line (arg)
2553 (vip-leave-region-active)
2554 (let ((val (vip-p-val arg))
2555 (com (vip-getCom arg)))
2556 (if com (vip-move-marker-locally 'vip-com-point (point)))
2558 (if vip-ex-style-motion
2559 (if (and (eolp) (not (bolp))) (backward-char 1)))
2560 (setq this-command 'next-line)
2561 (if com (vip-execute-com 'vip-next-line val com))))
2563 (defun vip-next-line-at-bol (arg)
2564 "Next line at beginning of line."
2566 (vip-leave-region-active)
2569 (if (eobp) (error "Last line in buffer")))
2570 (let ((val (vip-p-val arg))
2571 (com (vip-getCom arg)))
2572 (if com (vip-move-marker-locally 'vip-com-point (point)))
2574 (back-to-indentation)
2575 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
2577 (defun vip-previous-line (arg)
2578 "Go to previous line."
2580 (vip-leave-region-active)
2581 (let ((val (vip-p-val arg))
2582 (com (vip-getCom arg)))
2583 (if com (vip-move-marker-locally 'vip-com-point (point)))
2585 (if vip-ex-style-motion
2586 (if (and (eolp) (not (bolp))) (backward-char 1)))
2587 (setq this-command 'previous-line)
2588 (if com (vip-execute-com 'vip-previous-line val com))))
2591 (defun vip-previous-line-at-bol (arg)
2592 "Previous line at beginning of line."
2594 (vip-leave-region-active)
2597 (if (bobp) (error "First line in buffer")))
2598 (let ((val (vip-p-val arg))
2599 (com (vip-getCom arg)))
2600 (if com (vip-move-marker-locally 'vip-com-point (point)))
2601 (forward-line (- val))
2602 (back-to-indentation)
2603 (if com (vip-execute-com 'vip-previous-line val com))))
2605 (defun vip-change-to-eol (arg)
2606 "Change to end of line."
2608 (vip-goto-eol (cons arg ?c)))
2610 (defun vip-kill-line (arg)
2613 (vip-goto-eol (cons arg ?d)))
2615 (defun vip-erase-line (arg)
2618 (vip-beginning-of-line (cons arg ?d)))
2623 (defun vip-goto-line (arg)
2624 "Go to ARG's line. Without ARG go to end of buffer."
2626 (let ((val (vip-P-val arg))
2627 (com (vip-getCom arg)))
2628 (vip-move-marker-locally 'vip-com-point (point))
2629 (vip-deactivate-mark)
2632 (goto-char (point-max))
2633 (goto-char (point-min))
2634 (forward-line (1- val)))
2636 ;; positioning is done twice: before and after command execution
2637 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2638 (back-to-indentation)
2640 (if com (vip-execute-com 'vip-goto-line val com))
2642 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2643 (back-to-indentation)
2646 ;; Find ARG's occurrence of CHAR on the current line.
2647 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
2648 ;; adjust point after search.
2649 (defun vip-find-char (arg char forward offset)
2650 (or (char-or-string-p char) (error ""))
2651 (let ((arg (if forward arg (- arg)))
2652 (cmd (if (eq vip-intermediate-command 'vip-repeat)
2654 (vip-array-to-string (this-command-keys))))
2660 ;; forward search begins here
2661 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
2662 ;; forward search ends here
2663 (progn (end-of-line) (point)))
2665 ;; backward search begins from here
2667 (error "Command `%s': At beginning of line" cmd) (point))
2668 ;; backward search ends here
2669 (progn (beginning-of-line) (point))))
2670 ;; if arg > 0, point is forwarded before search.
2671 (if (> arg 0) (goto-char (1+ (point-min)))
2672 (goto-char (point-max)))
2673 (if (let ((case-fold-search nil))
2674 (search-forward (char-to-string char) nil 0 arg))
2675 (setq point (point))
2676 (error "Command `%s': `%c' not found" cmd char))))
2677 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
2679 (defun vip-find-char-forward (arg)
2680 "Find char on the line.
2681 If called interactively read the char to find from the terminal, and if
2682 called from vip-repeat, the char last used is used. This behaviour is
2683 controlled by the sign of prefix numeric value."
2685 (let ((val (vip-p-val arg))
2686 (com (vip-getcom arg))
2687 (cmd-representation (nth 5 vip-d-com)))
2689 ;; this means that the function was called interactively
2690 (setq vip-f-char (read-char)
2693 ;; vip-repeat --- set vip-F-char from command-keys
2694 (setq vip-F-char (if (stringp cmd-representation)
2695 (vip-seq-last-elt cmd-representation)
2697 vip-f-char vip-F-char)
2699 (if com (vip-move-marker-locally 'vip-com-point (point)))
2700 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
2704 (setq vip-F-char vip-f-char) ; set new vip-F-char
2706 (vip-execute-com 'vip-find-char-forward val com)))))
2708 (defun vip-goto-char-forward (arg)
2709 "Go up to char ARG forward on line."
2711 (let ((val (vip-p-val arg))
2712 (com (vip-getcom arg))
2713 (cmd-representation (nth 5 vip-d-com)))
2715 ;; this means that the function was called interactively
2716 (setq vip-f-char (read-char)
2719 ;; vip-repeat --- set vip-F-char from command-keys
2720 (setq vip-F-char (if (stringp cmd-representation)
2721 (vip-seq-last-elt cmd-representation)
2723 vip-f-char vip-F-char)
2725 (if com (vip-move-marker-locally 'vip-com-point (point)))
2726 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
2730 (setq vip-F-char vip-f-char) ; set new vip-F-char
2732 (vip-execute-com 'vip-goto-char-forward val com)))))
2734 (defun vip-find-char-backward (arg)
2735 "Find char ARG on line backward."
2737 (let ((val (vip-p-val arg))
2738 (com (vip-getcom arg))
2739 (cmd-representation (nth 5 vip-d-com)))
2741 ;; this means that the function was called interactively
2742 (setq vip-f-char (read-char)
2745 ;; vip-repeat --- set vip-F-char from command-keys
2746 (setq vip-F-char (if (stringp cmd-representation)
2747 (vip-seq-last-elt cmd-representation)
2749 vip-f-char vip-F-char)
2751 (if com (vip-move-marker-locally 'vip-com-point (point)))
2753 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
2757 (setq vip-F-char vip-f-char) ; set new vip-F-char
2758 (vip-execute-com 'vip-find-char-backward val com)))))
2760 (defun vip-goto-char-backward (arg)
2761 "Go up to char ARG backward on line."
2763 (let ((val (vip-p-val arg))
2764 (com (vip-getcom arg))
2765 (cmd-representation (nth 5 vip-d-com)))
2767 ;; this means that the function was called interactively
2768 (setq vip-f-char (read-char)
2771 ;; vip-repeat --- set vip-F-char from command-keys
2772 (setq vip-F-char (if (stringp cmd-representation)
2773 (vip-seq-last-elt cmd-representation)
2775 vip-f-char vip-F-char)
2777 (if com (vip-move-marker-locally 'vip-com-point (point)))
2778 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
2782 (setq vip-F-char vip-f-char) ; set new vip-F-char
2783 (vip-execute-com 'vip-goto-char-backward val com)))))
2785 (defun vip-repeat-find (arg)
2786 "Repeat previous find command."
2788 (let ((val (vip-p-val arg))
2789 (com (vip-getcom arg)))
2790 (vip-deactivate-mark)
2791 (if com (vip-move-marker-locally 'vip-com-point (point)))
2792 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
2795 (if vip-f-forward (forward-char))
2796 (vip-execute-com 'vip-repeat-find val com)))))
2798 (defun vip-repeat-find-opposite (arg)
2799 "Repeat previous find command in the opposite direction."
2801 (let ((val (vip-p-val arg))
2802 (com (vip-getcom arg)))
2803 (vip-deactivate-mark)
2804 (if com (vip-move-marker-locally 'vip-com-point (point)))
2805 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
2808 (if vip-f-forward (forward-char))
2809 (vip-execute-com 'vip-repeat-find-opposite val com)))))
2812 ;; window scrolling etc.
2814 (defun vip-other-window (arg)
2815 "Switch to other window."
2818 (or (not (eq vip-current-state 'emacs-state))
2819 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
2820 (vip-change-state-to-vi)))
2822 (defun vip-window-top (arg)
2823 "Go to home window line."
2825 (let ((val (vip-p-val arg))
2826 (com (vip-getCom arg)))
2827 (if com (vip-move-marker-locally 'vip-com-point (point)))
2829 (move-to-window-line (1- val))
2831 ;; positioning is done twice: before and after command execution
2832 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2833 (back-to-indentation)
2835 (if com (vip-execute-com 'vip-window-top val com))
2837 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2838 (back-to-indentation)
2841 (defun vip-window-middle (arg)
2842 "Go to middle window line."
2844 (let ((val (vip-p-val arg))
2845 (com (vip-getCom arg))
2847 (if com (vip-move-marker-locally 'vip-com-point (point)))
2849 (if (not (pos-visible-in-window-p (point-max)))
2850 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
2851 (setq lines (count-lines (window-start) (point-max)))
2852 (move-to-window-line (+ (/ lines 2) (1- val))))
2854 ;; positioning is done twice: before and after command execution
2855 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2856 (back-to-indentation)
2858 (if com (vip-execute-com 'vip-window-middle val com))
2860 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2861 (back-to-indentation)
2864 (defun vip-window-bottom (arg)
2865 "Go to last window line."
2867 (let ((val (vip-p-val arg))
2868 (com (vip-getCom arg)))
2869 (if com (vip-move-marker-locally 'vip-com-point (point)))
2871 (move-to-window-line (- val))
2873 ;; positioning is done twice: before and after command execution
2874 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2875 (back-to-indentation)
2877 (if com (vip-execute-com 'vip-window-bottom val com))
2879 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2880 (back-to-indentation)
2883 (defun vip-line-to-top (arg)
2884 "Put current line on the home line."
2886 (recenter (1- arg)))
2888 (defun vip-line-to-middle (arg)
2889 "Put current line on the middle line."
2891 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
2893 (defun vip-line-to-bottom (arg)
2894 "Put current line on the last line."
2896 (recenter (- (window-height) (1+ arg))))
2898 ;; If point is within vip-search-scroll-threshold of window top or bottom,
2899 ;; scroll up or down 1/7 of window height, depending on whether we are at the
2900 ;; bottom or at the top of the window. This function is called by vip-search
2901 ;; (which is called from vip-search-forward/backward/next). If the value of
2902 ;; vip-search-scroll-threshold is negative - don't scroll.
2903 (defun vip-adjust-window ()
2904 (let ((win-height (if vip-emacs-p
2905 (1- (window-height)) ; adjust for modeline
2906 (window-displayed-height)))
2908 at-top-p at-bottom-p
2909 min-scroll direction)
2911 (move-to-window-line 0) ; top
2913 (<= (count-lines pt (point))
2914 vip-search-scroll-threshold))
2915 (move-to-window-line -1) ; bottom
2917 (<= (count-lines pt (point)) vip-search-scroll-threshold))
2919 (cond (at-top-p (setq min-scroll (1- vip-search-scroll-threshold)
2921 (at-bottom-p (setq min-scroll (1+ vip-search-scroll-threshold)
2925 (* (max min-scroll (/ win-height 7)) direction)))
2930 ;; must correct this to only match ( to ) etc. On the other hand
2931 ;; it is good that paren match gets confused, because that way you
2932 ;; catch _all_ imbalances.
2934 (defun vip-paren-match (arg)
2935 "Go to the matching parenthesis."
2937 (vip-leave-region-active)
2938 (let ((com (vip-getcom arg))
2939 (parse-sexp-ignore-comments vip-parse-sexp-ignore-comments)
2942 (if (or (> arg 99) (< arg 1))
2943 (error "Prefix must be between 1 and 99")
2945 (if (> (point-max) 80000)
2946 (* (/ (point-max) 100) arg)
2947 (/ (* (point-max) arg) 100)))
2948 (back-to-indentation))
2949 (let (beg-lim end-lim)
2950 (if (and (eolp) (not (bolp))) (forward-char -1))
2951 (if (not (looking-at "[][(){}]"))
2952 (setq anchor-point (point)))
2955 (setq beg-lim (point))
2957 (setq end-lim (point)))
2958 (cond ((re-search-forward "[][(){}]" end-lim t)
2960 ((re-search-backward "[][(){}]" beg-lim t))
2962 (error "No matching character on line"))))
2963 (cond ((looking-at "[\(\[{]")
2964 (if com (vip-move-marker-locally 'vip-com-point (point)))
2967 (vip-execute-com 'vip-paren-match nil com)
2972 (vip-move-marker-locally 'vip-com-point anchor-point)
2974 (vip-execute-com 'vip-paren-match nil com)
2976 ((looking-at "[])}]")
2978 (if com (vip-move-marker-locally 'vip-com-point (point)))
2980 (if com (vip-execute-com 'vip-paren-match nil com)))
2983 (defun vip-toggle-parse-sexp-ignore-comments ()
2985 (setq vip-parse-sexp-ignore-comments (not vip-parse-sexp-ignore-comments))
2986 (prin1 (format "`%%' will %signore parentheses inside the comments"
2987 (if vip-parse-sexp-ignore-comments "" "NOT ")))
2991 ;; sentence ,paragraph and heading
2993 (defun vip-forward-sentence (arg)
2997 (let ((val (vip-p-val arg))
2998 (com (vip-getcom arg)))
2999 (if com (vip-move-marker-locally 'vip-com-point (point)))
3000 (forward-sentence val)
3001 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3003 (defun vip-backward-sentence (arg)
3004 "Backward sentence."
3007 (let ((val (vip-p-val arg))
3008 (com (vip-getcom arg)))
3009 (if com (vip-move-marker-locally 'vip-com-point (point)))
3010 (backward-sentence val)
3011 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3013 (defun vip-forward-paragraph (arg)
3014 "Forward paragraph."
3017 (let ((val (vip-p-val arg))
3018 (com (vip-getCom arg)))
3019 (if com (vip-move-marker-locally 'vip-com-point (point)))
3020 (forward-paragraph val)
3024 (vip-execute-com 'vip-forward-paragraph nil com)))))
3026 (defun vip-backward-paragraph (arg)
3027 "Backward paragraph."
3030 (let ((val (vip-p-val arg))
3031 (com (vip-getCom arg)))
3032 (if com (vip-move-marker-locally 'vip-com-point (point)))
3033 (backward-paragraph val)
3037 (vip-execute-com 'vip-backward-paragraph nil com)
3038 (backward-char 1)))))
3040 ;; should be mode-specific etc.
3042 (defun vip-prev-heading (arg)
3044 (let ((val (vip-p-val arg))
3045 (com (vip-getCom arg)))
3046 (if com (vip-move-marker-locally 'vip-com-point (point)))
3047 (re-search-backward vip-heading-start nil t val)
3048 (goto-char (match-beginning 0))
3049 (if com (vip-execute-com 'vip-prev-heading nil com))))
3051 (defun vip-heading-end (arg)
3053 (let ((val (vip-p-val arg))
3054 (com (vip-getCom arg)))
3055 (if com (vip-move-marker-locally 'vip-com-point (point)))
3056 (re-search-forward vip-heading-end nil t val)
3057 (goto-char (match-beginning 0))
3058 (if com (vip-execute-com 'vip-heading-end nil com))))
3060 (defun vip-next-heading (arg)
3062 (let ((val (vip-p-val arg))
3063 (com (vip-getCom arg)))
3064 (if com (vip-move-marker-locally 'vip-com-point (point)))
3066 (re-search-forward vip-heading-start nil t val)
3067 (goto-char (match-beginning 0))
3068 (if com (vip-execute-com 'vip-next-heading nil com))))
3073 (defun vip-scroll-screen (arg)
3074 "Scroll to next screen."
3080 (setq arg (1- arg)))
3083 (setq arg (1+ arg))))
3087 (message "End of buffer")
3088 (goto-char (point-max)))
3089 (message "Beginning of buffer")
3090 (goto-char (point-min))))
3093 (defun vip-scroll-screen-back (arg)
3094 "Scroll to previous screen."
3096 (vip-scroll-screen (- arg)))
3098 (defun vip-scroll-down (arg)
3099 "Pull down half screen."
3103 (scroll-down (/ (window-height) 2))
3106 (message "Beginning of buffer")
3107 (goto-char (point-min)))))
3109 (defun vip-scroll-down-one (arg)
3110 "Scroll up one line."
3114 (defun vip-scroll-up (arg)
3115 "Pull up half screen."
3119 (scroll-up (/ (window-height) 2))
3122 (message "End of buffer")
3123 (goto-char (point-max)))))
3125 (defun vip-scroll-up-one (arg)
3126 "Scroll down one line."
3133 (defun vip-if-string (prompt)
3134 (let ((s (vip-read-string-with-history
3138 (car vip-search-history))))
3139 (if (not (string= s ""))
3140 (setq vip-s-string s))))
3143 (defun vip-toggle-search-style (arg)
3144 "Toggle the value of vip-case-fold-search/vip-re-search.
3145 Without prefix argument, will ask which search style to toggle. With prefix
3146 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
3148 Although this function is bound to \\[vip-toggle-search-style], the most
3149 convenient way to use it is to bind `//' to the macro
3150 `1 M-x vip-toggle-search-style' and `///' to
3151 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
3152 toggle case-fold-search and hitting `/' three times witth toggle regexp
3153 search. Macros are more convenient in this case because they don't affect
3154 the Emacs binding of `/'."
3157 (cond ((or (eq arg 1)
3159 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3160 (if vip-case-fold-search
3161 "case-insensitive" "case-sensitive")
3162 (if vip-case-fold-search
3164 "case-insensitive")))))
3165 (setq vip-case-fold-search (null vip-case-fold-search))
3166 (if vip-case-fold-search
3167 (setq msg "Search becomes case-insensitive")
3168 (setq msg "Search becomes case-sensitive")))
3171 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3173 "regexp-search" "vanilla-search")
3176 "regexp-search")))))
3177 (setq vip-re-search (null vip-re-search))
3179 (setq msg "Search becomes regexp-style")
3180 (setq msg "Search becomes vanilla-style")))
3182 (setq msg "Search style remains unchanged")))
3185 (defun vip-set-vi-search-style-macros (unset)
3186 "Set the macros for toggling the search style in Viper's vi-state.
3187 The macro that toggles case sensitivity is bound to `//', and the one that
3188 toggles regexp search is bound to `///'.
3189 With a prefix argument, this function unsets the macros. "
3194 ;; toggle case sensitivity in search
3195 (vip-record-kbd-macro
3197 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3199 ;; toggle regexp/vanila search
3200 (vip-record-kbd-macro
3202 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3206 "// and /// now toggle case-sensitivity and regexp search.")))
3207 (vip-unrecord-kbd-macro "//" 'vi-state)
3209 (vip-unrecord-kbd-macro "///" 'vi-state))))
3211 (defun vip-set-emacs-search-style-macros (unset &optional arg-majormode)
3212 "Set the macros for toggling the search style in Viper's emacs-state.
3213 The macro that toggles case sensitivity is bound to `//', and the one that
3214 toggles regexp search is bound to `///'.
3215 With a prefix argument, this function unsets the macros.
3216 If the optional prefix argument is non-nil and specifies a valid major mode,
3217 this sets the macros only in the macros in that major mode. Otherwise,
3218 the macros are set in the current major mode.
3219 \(When unsetting the macros, the second argument has no effect.\)"
3224 ;; toggle case sensitivity in search
3225 (vip-record-kbd-macro
3227 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3228 (or arg-majormode major-mode))
3229 ;; toggle regexp/vanila search
3230 (vip-record-kbd-macro
3232 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3233 (or arg-majormode major-mode))
3236 "// and /// now toggle case-sensitivity and regexp search.")))
3237 (vip-unrecord-kbd-macro "//" 'emacs-state)
3239 (vip-unrecord-kbd-macro "///" 'emacs-state))))
3242 (defun vip-search-forward (arg)
3243 "Search a string forward.
3244 ARG is used to find the ARG's occurrence of the string.
3245 Null string will repeat previous search."
3247 (let ((val (vip-P-val arg))
3248 (com (vip-getcom arg))
3249 (old-str vip-s-string))
3250 (setq vip-s-forward t)
3252 ;; this is not used at present, but may be used later
3253 (if (or (not (equal old-str vip-s-string))
3254 (not (markerp vip-local-search-start-marker))
3255 (not (marker-buffer vip-local-search-start-marker)))
3256 (setq vip-local-search-start-marker (point-marker)))
3257 (vip-search vip-s-string t val)
3260 (vip-move-marker-locally 'vip-com-point (mark t))
3261 (vip-execute-com 'vip-search-next val com)))))
3263 (defun vip-search-backward (arg)
3264 "Search a string backward.
3265 ARG is used to find the ARG's occurrence of the string.
3266 Null string will repeat previous search."
3268 (let ((val (vip-P-val arg))
3269 (com (vip-getcom arg))
3270 (old-str vip-s-string))
3271 (setq vip-s-forward nil)
3273 ;; this is not used at present, but may be used later
3274 (if (or (not (equal old-str vip-s-string))
3275 (not (markerp vip-local-search-start-marker))
3276 (not (marker-buffer vip-local-search-start-marker)))
3277 (setq vip-local-search-start-marker (point-marker)))
3278 (vip-search vip-s-string nil val)
3281 (vip-move-marker-locally 'vip-com-point (mark t))
3282 (vip-execute-com 'vip-search-next val com)))))
3285 ;; Search for COUNT's occurrence of STRING.
3286 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3287 ;; INIT-POINT is the position where search is to start.
3289 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3290 (defun vip-search (string forward arg
3291 &optional no-offset init-point fail-if-not-found)
3292 (if (not (equal string ""))
3293 (let ((val (vip-p-val arg))
3294 (com (vip-getcom arg))
3295 (offset (not no-offset))
3296 (case-fold-search vip-case-fold-search)
3297 (start-point (or init-point (point))))
3298 (vip-deactivate-mark)
3302 (if offset (vip-forward-char-carefully))
3305 (re-search-forward string nil nil val)
3306 (re-search-backward string))
3307 (search-forward string nil nil val)
3308 (search-backward string))
3309 (if (not (equal start-point (point)))
3310 (push-mark start-point t)))
3312 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
3314 (message "Search wrapped around BOTTOM of buffer")
3315 (goto-char (point-min))
3316 (vip-search string forward (cons 1 com) t start-point 'fail)
3317 ;; don't wait in macros
3318 (or executing-kbd-macro (sit-for 2))
3319 ;; delete the wrap-around message
3322 (goto-char start-point)
3323 (error "`%s': %s not found"
3325 (if vip-re-search "Pattern" "String"))
3331 (re-search-backward string nil nil val)
3332 (search-backward string nil nil val))
3333 (if (not (equal start-point (point)))
3334 (push-mark start-point t)))
3336 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
3338 (message "Search wrapped around TOP of buffer")
3339 (goto-char (point-max))
3340 (vip-search string forward (cons 1 com) t start-point 'fail)
3341 ;; don't wait in macros
3342 (or executing-kbd-macro (sit-for 2))
3343 ;; delete the wrap-around message
3346 (goto-char start-point)
3347 (error "`%s': %s not found"
3349 (if vip-re-search "Pattern" "String"))
3351 ;; pull up or down if at top/bottom of window
3353 ;; highlight the result of search
3354 ;; don't wait and don't highlight in macros
3355 (or executing-kbd-macro
3356 vip-inside-command-argument-action
3357 (vip-flash-search-pattern))
3360 (defun vip-search-next (arg)
3361 "Repeat previous search."
3363 (let ((val (vip-p-val arg))
3364 (com (vip-getcom arg)))
3365 (if (null vip-s-string) (error vip-NoPrevSearch))
3366 (vip-search vip-s-string vip-s-forward arg)
3369 (vip-move-marker-locally 'vip-com-point (mark t))
3370 (vip-execute-com 'vip-search-next val com)))))
3372 (defun vip-search-Next (arg)
3373 "Repeat previous search in the reverse direction."
3375 (let ((val (vip-p-val arg))
3376 (com (vip-getcom arg)))
3377 (if (null vip-s-string) (error vip-NoPrevSearch))
3378 (vip-search vip-s-string (not vip-s-forward) arg)
3381 (vip-move-marker-locally 'vip-com-point (mark t))
3382 (vip-execute-com 'vip-search-Next val com)))))
3385 ;; Search contents of buffer defined by one of Viper's motion commands.
3386 ;; Repeatable via `n' and `N'.
3387 (defun vip-buffer-search-enable (&optional c)
3388 (cond (c (setq vip-buffer-search-char c))
3389 ((null vip-buffer-search-char)
3390 (setq vip-buffer-search-char ?g)))
3391 (define-key vip-vi-basic-map
3392 (char-to-string vip-buffer-search-char) 'vip-command-argument)
3393 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
3394 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
3396 ;; This is a Viper wraper for isearch-forward.
3397 (defun vip-isearch-forward (arg)
3398 "Do incremental search forward."
3400 ;; emacs bug workaround
3401 (if (listp arg) (setq arg (car arg)))
3402 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
3404 ;; This is a Viper wraper for isearch-backward."
3405 (defun vip-isearch-backward (arg)
3406 "Do incremental search backward."
3408 ;; emacs bug workaround
3409 (if (listp arg) (setq arg (car arg)))
3410 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
3413 ;; visiting and killing files, buffers
3415 (defun vip-switch-to-buffer ()
3416 "Switch to buffer in the current window."
3421 (format "Switch to buffer in this window \(%s\): "
3422 (buffer-name (other-buffer (current-buffer))))))
3423 (switch-to-buffer buffer)
3426 (defun vip-switch-to-buffer-other-window ()
3427 "Switch to buffer in another window."
3432 (format "Switch to buffer in another window \(%s\): "
3433 (buffer-name (other-buffer (current-buffer))))))
3434 (switch-to-buffer-other-window buffer)
3437 (defun vip-kill-buffer ()
3440 (let (buffer buffer-name)
3443 (format "Kill buffer \(%s\): "
3444 (buffer-name (current-buffer)))))
3446 (if (null buffer-name)
3448 (get-buffer buffer-name)))
3449 (if (null buffer) (error "`%s': No such buffer" buffer-name))
3450 (if (or (not (buffer-modified-p buffer))
3453 "Buffer `%s' is modified, are you sure you want to kill it? "
3455 (kill-buffer buffer)
3456 (error "Buffer not killed"))))
3459 (defvar vip-smart-suffix-list
3460 '("" "tex" "c" "cc" "C" "el" "java" "html" "htm" "pl" "P" "p")
3461 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
3462 This is useful when you the current directory contains files with the same
3463 prefix and many different suffixes. Usually, only one of the suffixes
3464 represents an editable file. However, file completion will stop at the `.'
3465 The smart suffix feature lets you hit RET in such a case, and Viper will
3466 select the appropriate suffix.
3468 Suffixes are tried in the order given and the first suffix for which a
3469 corresponding file exists is selected. If no file exists for any of the
3470 suffixes, the user is asked to confirm.
3472 To turn this feature off, set this variable to nil.")
3474 ;; Try to add suffix to files ending with a `.'
3475 ;; Useful when the user hits RET on a non-completed file name.
3476 (defun vip-file-add-suffix ()
3478 (len (length vip-smart-suffix-list))
3479 (file (buffer-string))
3481 (goto-char (point-max))
3482 (if (and vip-smart-suffix-list (string-match "\\.$" file))
3484 (while (and (not found) (< count len))
3485 (setq suff (nth count vip-smart-suffix-list)
3487 (if (file-exists-p (format "%s%s" file suff))
3494 (vip-tmp-insert-at-eob " [Please complete file name]")
3496 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
3498 (key-binding (setq key (read-key-sequence nil))))
3499 (cond ((eq cmd 'self-insert-command)
3501 (insert (events-to-keys key))
3503 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
3505 (t (command-execute cmd)))
3515 (defsubst vip-yank (text)
3516 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
3518 (setq this-command 'yank))
3520 (defun vip-put-back (arg)
3521 "Put back after point/below line."
3523 (let ((val (vip-p-val arg))
3524 (text (if vip-use-register
3525 (cond ((vip-valid-register vip-use-register '(digit))
3526 (current-kill (- vip-use-register ?1) 'do-not-rotate))
3527 ((vip-valid-register vip-use-register)
3528 (get-register (downcase vip-use-register)))
3529 (t (error vip-InvalidRegister vip-use-register)))
3532 (if vip-use-register
3533 (let ((reg vip-use-register))
3534 (setq vip-use-register nil)
3535 (error vip-EmptyRegister reg))
3537 (setq vip-use-register nil)
3538 (if (vip-end-with-a-newline-p text)
3544 (beginning-of-line))
3545 (if (not (eolp)) (vip-forward-char-carefully)))
3546 (set-marker (vip-mark-marker) (point) (current-buffer))
3547 (vip-set-destructive-command
3548 (list 'vip-put-back val nil vip-use-register nil nil))
3549 (vip-loop val (vip-yank text)))
3550 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3551 ;; newline; it leaves the cursor at the beginning when the text contains
3553 (if (vip-same-line (point) (mark))
3554 (or (= (point) (mark)) (vip-backward-char-carefully))
3555 (exchange-point-and-mark)
3557 (back-to-indentation)))
3558 (vip-deactivate-mark))
3560 (defun vip-Put-back (arg)
3561 "Put back at point/above line."
3563 (let ((val (vip-p-val arg))
3564 (text (if vip-use-register
3565 (cond ((vip-valid-register vip-use-register '(digit))
3566 (current-kill (- vip-use-register ?1) 'do-not-rotate))
3567 ((vip-valid-register vip-use-register)
3568 (get-register (downcase vip-use-register)))
3569 (t (error vip-InvalidRegister vip-use-register)))
3572 (if vip-use-register
3573 (let ((reg vip-use-register))
3574 (setq vip-use-register nil)
3575 (error vip-EmptyRegister reg))
3577 (setq vip-use-register nil)
3578 (if (vip-end-with-a-newline-p text) (beginning-of-line))
3579 (vip-set-destructive-command
3580 (list 'vip-Put-back val nil vip-use-register nil nil))
3581 (set-marker (vip-mark-marker) (point) (current-buffer))
3582 (vip-loop val (vip-yank text)))
3583 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3584 ;; newline; it leaves the cursor at the beginning when the text contains
3586 (if (vip-same-line (point) (mark))
3587 (or (= (point) (mark)) (vip-backward-char-carefully))
3588 (exchange-point-and-mark)
3590 (back-to-indentation)))
3591 (vip-deactivate-mark))
3594 ;; Copy region to kill-ring.
3595 ;; If BEG and END do not belong to the same buffer, copy empty region.
3596 (defun vip-copy-region-as-kill (beg end)
3598 (copy-region-as-kill beg end)
3599 (error (copy-region-as-kill beg beg))))
3602 (defun vip-delete-char (arg)
3605 (let ((val (vip-p-val arg)))
3606 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
3609 (let ((here (point)))
3611 (if (> val (- (point) here))
3612 (setq val (- (point) here))))))
3613 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
3614 (if (and vip-ex-style-motion (eolp))
3615 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
3616 (if vip-use-register
3618 (cond ((vip-valid-register vip-use-register '((Letter)))
3619 (vip-append-to-register
3620 (downcase vip-use-register) (point) (- (point) val)))
3621 ((vip-valid-register vip-use-register)
3623 vip-use-register (point) (- (point) val) nil))
3624 (t (error vip-InvalidRegister vip-use-register)))
3625 (setq vip-use-register nil)))
3626 (if vip-ex-style-motion
3629 (if (and (eolp) (not (bolp))) (backward-char 1)))
3631 (delete-backward-char val t)
3632 (delete-char val t)))))
3634 (defun vip-delete-backward-char (arg)
3635 "Delete previous character. On reaching beginning of line, stop and beep."
3637 (let ((val (vip-p-val arg)))
3638 (vip-set-destructive-command
3639 (list 'vip-delete-backward-char val nil nil nil nil))
3642 (let ((here (point)))
3644 (if (> val (- here (point)))
3645 (setq val (- here (point)))))))
3646 (if vip-use-register
3648 (cond ((vip-valid-register vip-use-register '(Letter))
3649 (vip-append-to-register
3650 (downcase vip-use-register) (point) (+ (point) val)))
3651 ((vip-valid-register vip-use-register)
3653 vip-use-register (point) (+ (point) val) nil))
3654 (t (error vip-InvalidRegister vip-use-register)))
3655 (setq vip-use-register nil)))
3657 (delete-backward-char val t))))
3659 (defun vip-del-backward-char-in-insert ()
3660 "Delete 1 char backwards while in insert mode."
3662 (if (and vip-ex-style-editing-in-insert (bolp))
3664 (delete-backward-char 1 t)))
3666 (defun vip-del-backward-char-in-replace ()
3667 "Delete one character in replace mode.
3668 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
3669 charecters. If it is nil, then the cursor just moves backwards, similarly
3670 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
3671 cursor move past the beginning of line."
3673 (cond (vip-delete-backwards-in-replace
3675 (delete-backward-char 1 t))
3676 (vip-ex-style-editing-in-insert
3681 (delete-backward-char 1 t))))
3682 (vip-ex-style-editing-in-insert
3687 (backward-char 1))))
3693 (defun vip-join-lines (arg)
3694 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
3696 (let ((val (vip-P-val arg)))
3697 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
3698 (vip-loop (if (null val) 1 (1- val))
3704 (delete-region (point) (1- (point)))
3705 (fixup-whitespace)))))))
3710 (defun vip-change (beg end)
3711 (if (markerp beg) (setq beg (marker-position beg)))
3712 (if (markerp end) (setq end (marker-position end)))
3713 ;; beg is sometimes (mark t), which may be nil
3714 (or beg (setq beg end))
3716 (vip-set-complex-command-for-undo)
3717 (if vip-use-register
3719 (copy-to-register vip-use-register beg end nil)
3720 (setq vip-use-register nil)))
3721 (vip-set-replace-overlay beg end)
3722 (setq last-command nil) ; separate repl text from prev kills
3724 (if (= (vip-replace-start) (point-max))
3725 (error "End of buffer"))
3727 (setq vip-last-replace-region
3728 (buffer-substring (vip-replace-start)
3731 ;; protect against error while inserting "@" and other disasters
3732 ;; (e.g., read-only buff)
3733 (condition-case conds
3734 (if (or vip-allow-multiline-replace-regions
3735 (vip-same-line (vip-replace-start)
3738 ;; tabs cause problems in replace, so untabify
3739 (goto-char (vip-replace-end))
3740 (insert-before-markers "@") ; put placeholder after the TAB
3741 (untabify (vip-replace-start) (point))
3742 ;; del @, don't put on kill ring
3743 (delete-backward-char 1)
3745 (vip-set-replace-overlay-glyphs
3746 vip-replace-region-start-delimiter
3747 vip-replace-region-end-delimiter)
3748 ;; this move takes care of the last posn in the overlay, which
3749 ;; has to be shifted because of insert. We can't simply insert
3750 ;; "$" before-markers because then overlay-start will shift the
3751 ;; beginning of the overlay in case we are replacing a single
3752 ;; character. This fixes the bug with `s' and `cl' commands.
3753 (vip-move-replace-overlay (vip-replace-start) (point))
3754 (goto-char (vip-replace-start))
3755 (vip-change-state-to-replace t))
3756 (kill-region (vip-replace-start)
3758 (vip-hide-replace-overlay)
3759 (vip-change-state-to-insert))
3760 (error ;; make sure that the overlay doesn't stay.
3761 ;; go back to the original point
3762 (goto-char (vip-replace-start))
3763 (vip-hide-replace-overlay)
3764 (vip-message-conditions conds))))
3767 (defun vip-change-subr (beg end)
3768 ;; beg is sometimes (mark t), which may be nil
3769 (or beg (setq beg end))
3771 (if vip-use-register
3773 (copy-to-register vip-use-register beg end nil)
3774 (setq vip-use-register nil)))
3775 (kill-region beg end)
3776 (setq this-command 'vip-change)
3777 (vip-yank-last-insertion))
3779 (defun vip-toggle-case (arg)
3780 "Toggle character case."
3782 (let ((val (vip-p-val arg)) (c))
3783 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
3785 (setq c (following-char))
3787 (if (eq c (upcase c))
3788 (insert-char (downcase c) 1)
3789 (insert-char (upcase c) 1))
3790 (if (eolp) (backward-char 1))
3791 (setq val (1- val)))))
3796 (defun vip-query-replace ()
3798 If a null string is suplied as the string to be replaced,
3799 the query replace mode will toggle between string replace
3800 and regexp replace."
3803 (setq str (vip-read-string-with-history
3804 (if vip-re-query-replace "Query replace regexp: "
3807 'vip-replace1-history
3808 (car vip-replace1-history) ; default
3810 (if (string= str "")
3812 (setq vip-re-query-replace (not vip-re-query-replace))
3813 (message "Query replace mode changed to %s"
3814 (if vip-re-query-replace "regexp replace"
3816 (if vip-re-query-replace
3817 (query-replace-regexp
3819 (vip-read-string-with-history
3820 (format "Query replace regexp `%s' with: " str)
3822 'vip-replace1-history
3823 (car vip-replace1-history) ; default
3827 (vip-read-string-with-history
3828 (format "Query replace `%s' with: " str)
3830 'vip-replace1-history
3831 (car vip-replace1-history) ; default
3837 (defun vip-mark-beginning-of-buffer ()
3838 "Mark beginning of buffer."
3841 (goto-char (point-min))
3842 (exchange-point-and-mark)
3843 (message "Mark set at the beginning of buffer"))
3845 (defun vip-mark-end-of-buffer ()
3846 "Mark end of buffer."
3849 (goto-char (point-max))
3850 (exchange-point-and-mark)
3851 (message "Mark set at the end of buffer"))
3853 (defun vip-mark-point ()
3854 "Set mark at point of buffer."
3856 (let ((char (read-char)))
3857 (cond ((and (<= ?a char) (<= char ?z))
3858 (point-to-register (1+ (- char ?a))))
3859 ((= char ?<) (vip-mark-beginning-of-buffer))
3860 ((= char ?>) (vip-mark-end-of-buffer))
3861 ((= char ?.) (vip-set-mark-if-necessary))
3862 ((= char ?,) (vip-cycle-through-mark-ring))
3863 ((= char ?D) (mark-defun))
3867 ;; Algorithm: If first invocation of this command save mark on ring, goto
3868 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
3869 ;; making it into the new mark, M1.
3870 ;; Push this mark back and set mark to the original point position, p1.
3871 ;; So, if you hit '' or `` then you can return to p1.
3873 ;; If repeated command, pop top elt from the ring into mark and
3874 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
3875 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
3876 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
3878 (defun vip-cycle-through-mark-ring ()
3879 "Visit previous locations on the mark ring.
3880 One can use `` and '' to temporarily jump 1 step back."
3881 (let* ((sv-pt (point)))
3882 ;; if repeated `m,' command, pop the previously saved mark.
3883 ;; Prev saved mark is actually prev saved point. It is used if the
3884 ;; user types `` or '' and is discarded
3885 ;; from the mark ring by the next `m,' command.
3886 ;; In any case, go to the previous or previously saved mark.
3887 ;; Then push the current mark (popped off the ring) and set current
3888 ;; point to be the mark. Current pt as mark is discarded by the next
3890 (if (eq last-command 'vip-cycle-through-mark-ring)
3892 ;; save current mark if the first iteration
3893 (setq mark-ring (delete (vip-mark-marker) mark-ring))
3895 (push-mark (mark t) t)) )
3897 (set-mark-command 1)
3898 ;; don't duplicate mark on the ring
3899 (setq mark-ring (delete (vip-mark-marker) mark-ring))
3901 (vip-deactivate-mark)
3902 (setq this-command 'vip-cycle-through-mark-ring)
3906 (defun vip-goto-mark (arg)
3909 (let ((char (read-char))
3910 (com (vip-getcom arg)))
3911 (vip-goto-mark-subr char com nil)))
3913 (defun vip-goto-mark-and-skip-white (arg)
3914 "Go to mark and skip to first non-white character on line."
3916 (let ((char (read-char))
3917 (com (vip-getCom arg)))
3918 (vip-goto-mark-subr char com t)))
3920 (defun vip-goto-mark-subr (char com skip-white)
3923 (error "Empty buffer")
3925 (cond ((vip-valid-register char '(letter))
3926 (let* ((buff (current-buffer))
3927 (reg (1+ (- char ?a)))
3928 (text-marker (get-register reg)))
3929 (if com (vip-move-marker-locally 'vip-com-point (point)))
3930 (if (not (vip-valid-marker text-marker))
3931 (error vip-EmptyTextmarker char))
3932 (if (and (vip-same-line (point) vip-last-jump)
3933 (= (point) vip-last-jump-ignore))
3934 (push-mark vip-last-jump t)
3935 (push-mark nil t)) ; no msg
3936 (vip-register-to-point reg)
3937 (setq vip-last-jump (point-marker))
3939 (back-to-indentation)
3940 (setq vip-last-jump-ignore (point))))
3942 (if (equal buff (current-buffer))
3943 (vip-execute-com (if skip-white
3944 'vip-goto-mark-and-skip-white
3947 (switch-to-buffer buff)
3948 (goto-char vip-com-point)
3949 (vip-change-state-to-vi)
3951 ((and (not skip-white) (= char ?`))
3952 (if com (vip-move-marker-locally 'vip-com-point (point)))
3953 (if (and (vip-same-line (point) vip-last-jump)
3954 (= (point) vip-last-jump-ignore))
3955 (goto-char vip-last-jump))
3956 (if (null (mark t)) (error "Mark is not set in this buffer"))
3957 (if (= (point) (mark t)) (pop-mark))
3958 (exchange-point-and-mark)
3959 (setq vip-last-jump (point-marker)
3960 vip-last-jump-ignore 0)
3961 (if com (vip-execute-com 'vip-goto-mark nil com)))
3962 ((and skip-white (= char ?'))
3963 (if com (vip-move-marker-locally 'vip-com-point (point)))
3964 (if (and (vip-same-line (point) vip-last-jump)
3965 (= (point) vip-last-jump-ignore))
3966 (goto-char vip-last-jump))
3967 (if (= (point) (mark t)) (pop-mark))
3968 (exchange-point-and-mark)
3969 (setq vip-last-jump (point))
3970 (back-to-indentation)
3971 (setq vip-last-jump-ignore (point))
3972 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
3973 (t (error vip-InvalidTextmarker char))))
3975 (defun vip-insert-tab ()
3979 (defun vip-exchange-point-and-mark ()
3981 (exchange-point-and-mark)
3982 (back-to-indentation))
3984 ;; Input Mode Indentation
3986 ;; Returns t, if the string before point matches the regexp STR.
3987 (defsubst vip-looking-back (str)
3988 (and (save-excursion (re-search-backward str nil t))
3989 (= (point) (match-end 0))))
3992 (defun vip-forward-indent ()
3993 "Indent forward -- `C-t' in Vi."
3996 (indent-to (+ (current-column) vip-shift-width)))
3998 (defun vip-backward-indent ()
3999 "Backtab, C-d in VI"
4002 (let ((p (point)) (c (current-column)) bol (indent t))
4003 (if (vip-looking-back "[0^]")
4005 (if (eq ?^ (preceding-char))
4006 (setq vip-preserve-indent t))
4007 (delete-backward-char 1)
4013 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4014 (delete-region (point) p)
4016 (indent-to (- c vip-shift-width)))
4017 (if (or (bolp) (vip-looking-back "[^ \t]"))
4018 (setq vip-cted nil)))))
4020 (defun vip-autoindent ()
4021 "Auto Indentation, Vi-style."
4023 (let ((col (current-indentation)))
4024 (if abbrev-mode (expand-abbrev))
4025 (if vip-preserve-indent
4026 (setq vip-preserve-indent nil)
4027 (setq vip-current-indent col))
4028 ;; don't leave whitespace lines around
4029 (if (memq last-command
4031 vip-open-line vip-Open-line
4032 vip-replace-state-exit-cmd))
4033 (indent-to-left-margin))
4034 ;; use \n instead of newline, or else <Return> will move the insert point
4040 (if (and vip-electric-mode
4041 (not (eq major-mode 'fundamental-mode)))
4042 (indent-according-to-mode)
4043 (indent-to vip-current-indent))
4048 ;; Viewing registers
4050 (defun vip-ket-function (arg)
4051 "Function called by \], the ket. View registers and call \]\]."
4053 (let ((reg (read-char)))
4054 (cond ((vip-valid-register reg '(letter Letter))
4055 (view-register (downcase reg)))
4056 ((vip-valid-register reg '(digit))
4057 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4059 (set-buffer (get-buffer-create "*Output*"))
4060 (delete-region (point-min) (point-max))
4061 (insert (format "Register %c contains the string:\n" reg))
4063 (goto-char (point-min)))
4064 (display-buffer "*Output*")))
4066 (vip-next-heading arg))
4068 vip-InvalidRegister reg)))))
4070 (defun vip-brac-function (arg)
4071 "Function called by \[, the brac. View textmarkers and call \[\["
4073 (let ((reg (read-char)))
4075 (vip-prev-heading arg))
4077 (vip-heading-end arg))
4078 ((vip-valid-register reg '(letter))
4079 (let* ((val (get-register (1+ (- reg ?a))))
4081 (error vip-EmptyTextmarker reg)
4082 (marker-buffer val)))
4083 (pos (marker-position val))
4084 line-no text (s pos) (e pos))
4086 (set-buffer (get-buffer-create "*Output*"))
4087 (delete-region (point-min) (point-max))
4092 (setq line-no (1+ (count-lines (point-min) val)))
4095 (if (re-search-backward "[^ \t]" nil t)
4101 (if (re-search-forward "[^ \t]" nil t)
4105 (setq text (buffer-substring s e))
4106 (setq text (format "%s<%c>%s"
4107 (substring text 0 (- pos s))
4108 reg (substring text (- pos s)))))
4111 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4112 reg (buffer-name buf) line-no))
4113 (insert (format "Here is some text around %c:\n\n %s"
4115 (insert (format vip-EmptyTextmarker reg)))
4116 (goto-char (point-min)))
4117 (display-buffer "*Output*")))
4118 (t (error vip-InvalidTextmarker reg)))))
4122 ;; commands in insertion mode
4124 (defun vip-delete-backward-word (arg)
4125 "Delete previous word."
4130 (delete-region (point) (mark t))
4134 (defun vip-set-expert-level (&optional dont-change-unless)
4135 "Sets the expert level for a Viper user.
4136 Can be called interactively to change (temporarily or permanently) the
4137 current expert level.
4139 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4140 the level should not be changed, unless its current value is
4141 meaningless (i.e., not one of 1,2,3,4,5).
4143 User level determines the setting of Viper variables that are most
4144 sensitive for VI-style look-and-feel."
4148 (if (not (natnump vip-expert-level)) (setq vip-expert-level 0))
4150 (save-window-excursion
4151 (delete-other-windows)
4152 ;; if 0 < vip-expert-level < vip-max-expert-level
4153 ;; & dont-change-unless = t -- use it; else ask
4154 (vip-ask-level dont-change-unless))
4156 (setq viper-always t
4157 vip-ex-style-motion t
4158 vip-ex-style-editing-in-insert t
4159 vip-want-ctl-h-help nil)
4161 (cond ((eq vip-expert-level 1) ; novice or beginner
4162 (global-set-key ; in emacs-state
4164 (if (vip-window-display-p) 'vip-iconify 'suspend-emacs))
4165 (setq vip-no-multiple-ESC t
4167 vip-vi-style-in-minibuffer t
4168 vip-search-wrap-around-t t
4169 vip-electric-mode nil
4170 vip-want-emacs-keys-in-vi nil
4171 vip-want-emacs-keys-in-insert nil))
4173 ((and (> vip-expert-level 1) (< vip-expert-level 5))
4174 ;; intermediate to guru
4175 (setq vip-no-multiple-ESC (if (vip-window-display-p)
4178 vip-want-emacs-keys-in-vi t
4179 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4181 (if (eq vip-expert-level 4) ; respect user's ex-style motion
4182 ; and vip-no-multiple-ESC
4184 (setq-default vip-ex-style-editing-in-insert
4185 (cdr (assoc 'vip-ex-style-editing-in-insert
4186 vip-saved-user-settings))
4188 (cdr (assoc 'vip-ex-style-motion
4189 vip-saved-user-settings)))
4190 (setq vip-ex-style-motion
4191 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4192 vip-ex-style-editing-in-insert
4193 (cdr (assoc 'vip-ex-style-editing-in-insert
4194 vip-saved-user-settings))
4196 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4198 (cdr (assoc 'vip-no-multiple-ESC
4199 vip-saved-user-settings))))))
4202 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4203 ;; user toggle the values of variables.
4204 (t (setq-default vip-ex-style-editing-in-insert
4205 (cdr (assoc 'vip-ex-style-editing-in-insert
4206 vip-saved-user-settings))
4208 (cdr (assoc 'vip-ex-style-motion
4209 vip-saved-user-settings)))
4210 (setq vip-want-ctl-h-help
4211 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
4213 (cdr (assoc 'viper-always vip-saved-user-settings))
4215 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
4217 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4218 vip-ex-style-editing-in-insert
4219 (cdr (assoc 'vip-ex-style-editing-in-insert
4220 vip-saved-user-settings))
4222 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4224 (cdr (assoc 'vip-electric-mode
4225 vip-saved-user-settings))
4226 vip-want-emacs-keys-in-vi
4227 (cdr (assoc 'vip-want-emacs-keys-in-vi
4228 vip-saved-user-settings))
4229 vip-want-emacs-keys-in-insert
4230 (cdr (assoc 'vip-want-emacs-keys-in-insert
4231 vip-saved-user-settings)))))
4232 (vip-set-mode-vars-for vip-current-state)
4233 (if (or viper-always
4234 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
4237 ;; Ask user expert level.
4238 (defun vip-ask-level (dont-change-unless)
4239 (let ((ask-buffer " *vip-ask-level*")
4240 level-changed repeated)
4241 (save-window-excursion
4242 (switch-to-buffer ask-buffer)
4244 (or (eq this-command 'vip-set-expert-level)
4246 (<= vip-expert-level vip-max-expert-level)
4247 (>= vip-expert-level 1))
4251 *** Important Notice for VIP users***
4256 Viper Is a Package for Emacs Rebels,
4257 a VI Plan for Emacs Rescue,
4258 and a venomous VI PERil.
4261 Technically speaking, Viper is a new Vi emulator that replaces
4262 the old VIP package.
4264 Viper emulates Vi much better than VIP. It also significantly
4265 extends and improves upon Vi in many useful ways.
4267 Although many VIP settings in your ~/.vip are compatible with Viper,
4268 you may have to change some of them. Please refer to the documentation,
4269 which can be obtained by executing
4273 when Viper is in Vi state.
4275 If you will be so lucky as to find a bug, report it via the command
4279 Type any key to continue... ")
4284 (while (or (> vip-expert-level vip-max-expert-level)
4285 (< vip-expert-level 1)
4286 (null dont-change-unless))
4290 (message "Invalid user level")
4293 (setq dont-change-unless t
4296 Please specify your level of familiarity with the venomous VI PERil
4297 (and the VI Plan for Emacs Rescue).
4298 You can change it at any time by typing `M-x vip-set-expert-level RET'
4300 1 -- BEGINNER: Almost all Emacs features are suppressed.
4301 Feels almost like straight Vi. File name completion and
4302 command history in the minibuffer are thrown in as a bonus.
4303 To use Emacs productively, you must reach level 3 or higher.
4304 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4305 so most Emacs commands can be used when Viper is in Vi state.
4306 Good progress---you are well on the way to level 3!
4307 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
4308 in Viper's insert state.
4309 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
4310 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
4311 variables. Adjust these settings to your taste.
4312 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4313 vip-electric-mode, vip-want-ctl-h-help, vip-want-emacs-keys-in-vi,
4314 and vip-want-emacs-keys-in-insert. Adjust these to your taste.
4316 Please, specify your level now: ")
4318 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
4321 ;; tell the user if level was changed
4325 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4327 (if (y-or-n-p "Do you wish to make this change permanent? ")
4328 ;; save the setting for vip-expert-level
4331 (format "Saving user level %d ..." vip-expert-level)
4332 vip-custom-file-name))
4334 (bury-buffer) ; remove ask-buffer from screen
4344 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4345 (defun vip-register-to-point (char &optional enforce-buffer)
4346 "Like jump-to-register, but switches to another buffer in another window."
4347 (interactive "cViper register to point: ")
4348 (let ((val (get-register char)))
4350 ((and (fboundp 'frame-configuration-p)
4351 (frame-configuration-p val))
4352 (set-frame-configuration val))
4353 ((window-configuration-p val)
4354 (set-window-configuration val))
4355 ((vip-valid-marker val)
4356 (if (and enforce-buffer
4357 (not (equal (current-buffer) (marker-buffer val))))
4358 (error (concat vip-EmptyTextmarker " in this buffer")
4360 (pop-to-buffer (marker-buffer val))
4362 ((and (consp val) (eq (car val) 'file))
4363 (find-file (cdr val)))
4365 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
4368 (defun vip-save-kill-buffer ()
4369 "Save then kill current buffer. "
4371 (if (< vip-expert-level 2)
4372 (save-buffers-kill-emacs)
4374 (kill-buffer (current-buffer))))
4380 (defun vip-submit-report ()
4381 "Submit bug report on Viper."
4383 (let ((reporter-prompt-for-summary-p t)
4384 (vip-device-type (vip-device-type))
4385 color-display-p frame-parameters
4386 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4387 varlist salutation window-config)
4389 ;; If mode info is needed, add variable to `let' and then set it below,
4390 ;; like we did with color-display-p.
4391 (setq color-display-p (if (vip-window-display-p)
4392 (vip-color-display-p)
4394 minibuffer-vi-face (if (vip-has-face-support-p)
4395 (vip-get-face vip-minibuffer-vi-face)
4397 minibuffer-insert-face (if (vip-has-face-support-p)
4398 (vip-get-face vip-minibuffer-insert-face)
4400 minibuffer-emacs-face (if (vip-has-face-support-p)
4401 (vip-get-face vip-minibuffer-emacs-face)
4403 frame-parameters (if (fboundp 'frame-parameters)
4404 (frame-parameters (selected-frame))))
4406 (setq varlist (list 'vip-vi-minibuffer-minor-mode
4407 'vip-insert-minibuffer-minor-mode
4408 'vip-vi-intercept-minor-mode
4409 'vip-vi-local-user-minor-mode
4410 'vip-vi-kbd-minor-mode
4411 'vip-vi-global-user-minor-mode
4412 'vip-vi-state-modifier-minor-mode
4413 'vip-vi-diehard-minor-mode
4414 'vip-vi-basic-minor-mode
4415 'vip-replace-minor-mode
4416 'vip-insert-intercept-minor-mode
4417 'vip-insert-local-user-minor-mode
4418 'vip-insert-kbd-minor-mode
4419 'vip-insert-global-user-minor-mode
4420 'vip-insert-state-modifier-minor-mode
4421 'vip-insert-diehard-minor-mode
4422 'vip-insert-basic-minor-mode
4423 'vip-emacs-intercept-minor-mode
4424 'vip-emacs-local-user-minor-mode
4425 'vip-emacs-kbd-minor-mode
4426 'vip-emacs-global-user-minor-mode
4427 'vip-emacs-state-modifier-minor-mode
4428 'vip-automatic-iso-accents
4429 'vip-want-emacs-keys-in-insert
4430 'vip-want-emacs-keys-in-vi
4431 'vip-keep-point-on-undo
4432 'vip-no-multiple-ESC
4435 'vip-want-ctl-h-help
4436 'vip-ex-style-editing-in-insert
4437 'vip-delete-backwards-in-replace
4438 'vip-vi-style-in-minibuffer
4440 'vip-insert-state-hook
4441 'vip-replace-state-hook
4442 'vip-emacs-state-hook
4443 'ex-cycle-other-window
4444 'ex-cycle-through-non-files
4451 'minibuffer-insert-face
4452 'minibuffer-emacs-face
4455 Congratulations! You may have unearthed a bug in Viper!
4456 Please mail a concise, accurate summary of the problem to the address above.
4458 -------------------------------------------------------------------")
4459 (setq window-config (current-window-configuration))
4460 (with-output-to-temp-buffer " *vip-info*"
4461 (switch-to-buffer " *vip-info*")
4462 (delete-other-windows)
4464 PLEASE FOLLOW THESE PROCEDURES
4465 ------------------------------
4467 Before reporting a bug, please verify that it is related to Viper, and is
4468 not cause by other packages you are using.
4470 Don't report compilation warnings, unless you are certain that there is a
4471 problem. These warnings are normal and unavoidable.
4473 Please note that users should not modify variables and keymaps other than
4474 those advertised in the manual. Such `customization' is likely to crash
4475 Viper, as it would any other improperly customized Emacs package.
4477 If you are reporting an error message received while executing one of the
4478 Viper commands, type:
4480 M-x set-variable <Return> debug-on-error <Return> t <Return>
4482 Then reproduce the error. The above command will cause Emacs to produce a
4483 back trace of the execution that leads to the error. Please include this
4484 trace in your bug report.
4486 If you believe that one of Viper's commands goes into an infinite loop
4487 \(e.g., Emacs freezes\), type:
4489 M-x set-variable <Return> debug-on-quit <Return> t <Return>
4491 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
4492 the current command. Include the resulting back trace in the bug report.
4494 Mail anyway (y or n)? ")
4495 (if (y-or-n-p "Mail anyway? ")
4497 (set-window-configuration window-config)
4498 (error "Bug report aborted")))
4501 (set-window-configuration window-config)
4503 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
4506 nil 'delete-other-windows
4513 ;; Smoothes out the difference between Emacs' unread-command-events
4514 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
4515 ;; events or a sequence of keys.
4517 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
4518 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
4519 ;; into an event. Below, we delete nil from event lists, since nil is the most
4520 ;; common symbol that might appear in this wrong context.
4521 (defun vip-set-unread-command-events (arg)
4524 unread-command-events
4526 (cond ((eventp arg) (list arg))
4529 (listify-key-sequence arg))
4531 "vip-set-unread-command-events: Invalid argument, %S"
4533 (if (not (eventp nil))
4534 (setq new-events (delq nil new-events)))
4535 (append new-events unread-command-events)))
4538 unread-command-events
4540 (cond ((vip-characterp arg) (list (character-to-event arg)))
4541 ((eventp arg) (list arg))
4542 ((stringp arg) (mapcar 'character-to-event arg))
4543 ((vectorp arg) (append arg nil)) ; turn into list
4544 ((listp arg) (vip-eventify-list-xemacs arg))
4546 "vip-set-unread-command-events: Invalid argument, %S" arg)))
4547 unread-command-events))))
4549 ;; list is assumed to be a list of events of characters
4550 (defun vip-eventify-list-xemacs (lis)
4552 (function (lambda (elt)
4553 (cond ((vip-characterp elt) (character-to-event elt))
4556 "vip-eventify-list-xemacs: can't convert to event, %S"
4562 ;;; viper-cmd.el ends here