1 ;;; viper-cmd.el --- Vi command support for Viper
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
11 (defvar viper-minibuffer-current-face
)
12 (defvar viper-minibuffer-insert-face
)
13 (defvar viper-minibuffer-vi-face
)
14 (defvar viper-minibuffer-emacs-face
)
16 (defvar viper-mode-string
)
17 (defvar viper-custom-file-name
)
18 (defvar iso-accents-mode
)
19 (defvar zmacs-region-stays
)
20 (defvar mark-even-if-inactive
)
22 ;; loading happens only in non-interactive compilation
23 ;; in order to spare non-viperized emacs from being viperized
26 (let ((load-path (cons (expand-file-name ".") load-path
)))
27 (or (featurep 'viper-util
)
28 (load "viper-util.el" nil nil
'nosuffix
))
29 (or (featurep 'viper-keym
)
30 (load "viper-keym.el" nil nil
'nosuffix
))
31 (or (featurep 'viper-mous
)
32 (load "viper-mous.el" nil nil
'nosuffix
))
33 (or (featurep 'viper-macs
)
34 (load "viper-macs.el" nil nil
'nosuffix
))
35 (or (featurep 'viper-ex
)
36 (load "viper-ex.el" nil nil
'nosuffix
))
51 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
53 ;; generate test functions
54 ;; given symbol foo, foo-p is the test function, foos is the set of
56 ;; (macroexpand '(viper-test-com-defun foo))
57 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
59 (defmacro viper-test-com-defun
(name)
60 (let* ((snm (symbol-name name
))
61 (nm-p (intern (concat snm
"-p")))
62 (nms (intern (concat snm
"s"))))
63 (` (defun (, nm-p
) (com)
64 (consp (memq (if (and (viper-characterp com
) (< com
0))
65 (- com
) com
) (, nms
)))))))
67 ;; Variables for defining VI commands
69 ;; Modifying commands that can be prefixes to movement commands
70 (defconst viper-prefix-commands
'(?c ?d ?y ?
! ?
= ?
# ?
< ?
> ?
\"))
71 ;; define viper-prefix-command-p
72 (viper-test-com-defun viper-prefix-command)
74 ;; Commands that are pairs eg. dd. r and R here are a hack
75 (defconst viper-charpair-commands
'(?c ?d ?y ?
! ?
= ?
< ?
> ?r ?R
))
76 ;; define viper-charpair-command-p
77 (viper-test-com-defun viper-charpair-command)
79 (defconst viper-movement-commands
'(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
80 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
81 ?^ ?
( ?
) ?- ?
+ ?| ?
{ ?
} ?
[ ?
] ?
' ?
`
82 ?
; ?, ?0 ?? ?/ ?\C-m ?\
87 ;; define viper-movement-command-p
88 (viper-test-com-defun viper-movement-command)
91 (defconst viper-digit-commands
'(?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9))
93 ;; define viper-digit-command-p
94 (viper-test-com-defun viper-digit-command)
96 ;; Commands that can be repeated by . (dotted)
97 (defconst viper-dotable-commands
'(?c ?d ?C ?s ?S ?D ?
> ?
<))
98 ;; define viper-dotable-command-p
99 (viper-test-com-defun viper-dotable-command)
101 ;; Commands that can follow a #
102 (defconst viper-hash-commands
'(?c ?C ?g ?q ?s
))
103 ;; define viper-hash-command-p
104 (viper-test-com-defun viper-hash-command)
106 ;; Commands that may have registers as prefix
107 (defconst viper-regsuffix-commands
'(?d ?y ?Y ?D ?p ?P ?x ?X
))
108 ;; define viper-regsuffix-command-p
109 (viper-test-com-defun viper-regsuffix-command)
111 (defconst viper-vi-commands
(append viper-movement-commands
113 viper-dotable-commands
114 viper-charpair-commands
116 viper-prefix-commands
117 viper-regsuffix-commands
)
118 "The list of all commands in Vi-state.")
119 ;; define viper-vi-command-p
120 (viper-test-com-defun viper-vi-command)
127 ;; Runs viper-after-change-functions inside after-change-functions
128 (defun viper-after-change-sentinel (beg end len
)
129 (let ((list viper-after-change-functions
))
131 (funcall (car list
) beg end len
)
132 (setq list
(cdr list
)))))
134 ;; Runs viper-before-change-functions inside before-change-functions
135 (defun viper-before-change-sentinel (beg end
)
136 (let ((list viper-before-change-functions
))
138 (funcall (car list
) beg end
)
139 (setq list
(cdr list
)))))
141 (defsubst viper-post-command-sentinel
()
142 (run-hooks 'viper-post-command-hooks
))
144 (defsubst viper-pre-command-sentinel
()
145 (run-hooks 'viper-pre-command-hooks
))
147 ;; Needed so that Viper will be able to figure the last inserted
148 ;; chunk of text with reasonable accuracy.
149 (defsubst viper-insert-state-post-command-sentinel
()
150 (if (and (memq viper-current-state
'(insert-state replace-state
))
152 (>= (point) viper-insert-point
))
153 (setq viper-last-posn-while-in-insert-state
(point-marker)))
154 (if (eq viper-current-state
'insert-state
)
156 (or (stringp viper-saved-cursor-color
)
157 (string= (viper-get-cursor-color) viper-insert-state-cursor-color
)
158 (setq viper-saved-cursor-color
(viper-get-cursor-color)))
159 (if (stringp viper-saved-cursor-color
)
160 (viper-change-cursor-color viper-insert-state-cursor-color
))
162 (if (and (eq this-command
'dabbrev-expand
)
163 (integerp viper-pre-command-point
)
164 (> viper-insert-point viper-pre-command-point
))
165 (move-marker viper-insert-point viper-pre-command-point
))
168 (defsubst viper-insert-state-pre-command-sentinel
()
169 (or (memq this-command
'(self-insert-command))
170 (memq (viper-event-key last-command-event
)
171 '(up down left right
(meta f
) (meta b
)
172 (control n
) (control p
) (control f
) (control b
)))
173 (viper-restore-cursor-color-after-insert))
174 (if (and (eq this-command
'dabbrev-expand
)
175 (markerp viper-insert-point
)
176 (marker-position viper-insert-point
))
177 (setq viper-pre-command-point
(marker-position viper-insert-point
))))
179 (defsubst viper-R-state-post-command-sentinel
()
180 ;; Restoring cursor color is needed despite
181 ;; viper-replace-state-pre-command-sentinel: When you jump to another buffer
182 ;; in another frame, the pre-command hook won't change cursor color to
183 ;; default in that other frame. So, if the second frame cursor was red and
184 ;; we set the point outside the replacement region, then the cursor color
185 ;; will remain red. Restoring the default, below, prevents this.
186 (if (and (<= (viper-replace-start) (point))
187 (<= (point) (viper-replace-end)))
188 (viper-change-cursor-color viper-replace-overlay-cursor-color
)
189 (viper-restore-cursor-color-after-replace)
192 ;; to speed up, don't change cursor color before self-insert
193 ;; and common move commands
194 (defsubst viper-replace-state-pre-command-sentinel
()
195 (or (memq this-command
'(self-insert-command))
196 (memq (viper-event-key last-command-event
)
197 '(up down left right
(meta f
) (meta b
)
198 (control n
) (control p
) (control f
) (control b
)))
199 (viper-restore-cursor-color-after-replace)))
201 (defun viper-replace-state-post-command-sentinel ()
202 ;; Restoring cursor color is needed despite
203 ;; viper-replace-state-pre-command-sentinel: When one jumps to another buffer
204 ;; in another frame, the pre-command hook won't change cursor color to
205 ;; default in that other frame. So, if the second frame cursor was red and
206 ;; we set the point outside the replacement region, then the cursor color
207 ;; will remain red. Restoring the default, below, fixes this problem.
209 ;; We optimize for self-insert-command's here, since they either don't change
210 ;; cursor color or, if they terminate replace mode, the color will be changed
211 ;; in viper-finish-change
212 (or (memq this-command
'(self-insert-command))
213 (viper-restore-cursor-color-after-replace))
215 ((eq viper-current-state
'replace-state
)
216 ;; delete characters to compensate for inserted chars.
217 (let ((replace-boundary (viper-replace-end)))
219 (goto-char viper-last-posn-in-replace-region
)
220 (delete-char viper-replace-chars-to-delete
)
221 (setq viper-replace-chars-to-delete
0
222 viper-replace-chars-deleted
0)
223 ;; terminate replace mode if reached replace limit
224 (if (= viper-last-posn-in-replace-region
226 (viper-finish-change viper-last-posn-in-replace-region
)))
228 (if (and (<= (viper-replace-start) (point))
229 (<= (point) replace-boundary
))
231 ;; the state may have changed in viper-finish-change above
232 (if (eq viper-current-state
'replace-state
)
233 (viper-change-cursor-color viper-replace-overlay-cursor-color
))
234 (setq viper-last-posn-in-replace-region
(point-marker))))
237 (t ;; terminate replace mode if changed Viper states.
238 (viper-finish-change viper-last-posn-in-replace-region
))))
243 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
244 (defun viper-change-state (new-state)
245 ;; Keep viper-post/pre-command-hooks fresh.
246 ;; We remove then add viper-post/pre-command-sentinel since it is very
247 ;; desirable that viper-pre-command-sentinel is the last hook and
248 ;; viper-post-command-sentinel is the first hook.
249 (remove-hook 'post-command-hook
'viper-post-command-sentinel
)
250 (add-hook 'post-command-hook
'viper-post-command-sentinel
)
251 (remove-hook 'pre-command-hook
'viper-pre-command-sentinel
)
252 (add-hook 'pre-command-hook
'viper-pre-command-sentinel t
)
253 ;; These hooks will be added back if switching to insert/replace mode
254 (viper-remove-hook 'viper-post-command-hooks
255 'viper-insert-state-post-command-sentinel
)
256 (viper-remove-hook 'viper-pre-command-hooks
257 'viper-insert-state-pre-command-sentinel
)
258 (setq viper-intermediate-command nil
)
259 (cond ((eq new-state
'vi-state
)
260 (cond ((member viper-current-state
'(insert-state replace-state
))
262 ;; move viper-last-posn-while-in-insert-state
263 ;; This is a normal hook that is executed in insert/replace
264 ;; states after each command. In Vi/Emacs state, it does
265 ;; nothing. We need to execute it here to make sure that
266 ;; the last posn was recorded when we hit ESC.
267 ;; It may be left unrecorded if the last thing done in
268 ;; insert/repl state was dabbrev-expansion or abbrev
269 ;; expansion caused by hitting ESC
270 (viper-insert-state-post-command-sentinel)
272 (condition-case conds
274 (viper-save-last-insertion
276 viper-last-posn-while-in-insert-state
)
277 (if viper-began-as-replace
278 (setq viper-began-as-replace nil
)
279 ;; repeat insert commands if numerical arg > 1
281 (viper-repeat-insert-command))))
283 (viper-message-conditions conds
)))
285 (if (> (length viper-last-insertion
) 0)
286 (viper-push-onto-ring viper-last-insertion
287 'viper-insertion-ring
))
289 (if viper-ex-style-editing-in-insert
290 (or (bolp) (backward-char 1))))
294 ((memq new-state
'(insert-state replace-state
))
295 (if (memq viper-current-state
'(emacs-state vi-state
))
296 (viper-move-marker-locally 'viper-insert-point
(point)))
297 (viper-move-marker-locally
298 'viper-last-posn-while-in-insert-state
(point))
299 (viper-add-hook 'viper-post-command-hooks
300 'viper-insert-state-post-command-sentinel t
)
301 (viper-add-hook 'viper-pre-command-hooks
302 'viper-insert-state-pre-command-sentinel t
))
305 ;; Nothing needs to be done to switch to emacs mode! Just set some
306 ;; variables, which is already done in viper-change-state-to-emacs!
308 (setq viper-current-state new-state
)
309 (viper-normalize-minor-mode-map-alist)
310 (viper-adjust-keys-for new-state
)
311 (viper-set-mode-vars-for new-state
)
312 (viper-refresh-mode-line)
317 (defun viper-adjust-keys-for (state)
318 "Make necessary adjustments to keymaps before entering STATE."
319 (cond ((memq state
'(insert-state replace-state
))
320 (if viper-auto-indent
322 (define-key viper-insert-basic-map
"\C-m" 'viper-autoindent
)
323 (if viper-want-emacs-keys-in-insert
325 (define-key viper-insert-basic-map
"\C-j" nil
)
327 (define-key viper-insert-basic-map
"\C-j" 'viper-autoindent
)))
328 (define-key viper-insert-basic-map
"\C-m" nil
)
329 (define-key viper-insert-basic-map
"\C-j" nil
))
331 (setq viper-insert-diehard-minor-mode
332 (not viper-want-emacs-keys-in-insert
))
334 (if viper-want-ctl-h-help
336 (define-key viper-insert-basic-map
[(control h
)] 'help-command
)
337 (define-key viper-replace-map
[(control h
)] 'help-command
))
338 (define-key viper-insert-basic-map
339 [(control h
)] 'viper-del-backward-char-in-insert
)
340 (define-key viper-replace-map
341 [(control h
)] 'viper-del-backward-char-in-replace
)))
344 (setq viper-vi-diehard-minor-mode
(not viper-want-emacs-keys-in-vi
))
345 (if viper-want-ctl-h-help
346 (define-key viper-vi-basic-map
[(control h
)] 'help-command
)
347 (define-key viper-vi-basic-map
[(control h
)] 'viper-backward-char
)))
351 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
352 ;; This ensures that Viper bindings are in effect, regardless of which minor
353 ;; modes were turned on by the user or by other packages.
354 (defun viper-normalize-minor-mode-map-alist ()
355 (setq minor-mode-map-alist
356 (viper-append-filter-alist
358 (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map
)
359 (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map
)
360 (cons 'viper-vi-local-user-minor-mode viper-vi-local-user-map
)
361 (cons 'viper-vi-kbd-minor-mode viper-vi-kbd-map
)
362 (cons 'viper-vi-global-user-minor-mode viper-vi-global-user-map
)
363 (cons 'viper-vi-state-modifier-minor-mode
365 (cdr (assoc major-mode
366 viper-vi-state-modifier-alist
)))
367 (cdr (assoc major-mode viper-vi-state-modifier-alist
))
369 (cons 'viper-vi-diehard-minor-mode viper-vi-diehard-map
)
370 (cons 'viper-vi-basic-minor-mode viper-vi-basic-map
)
371 (cons 'viper-insert-intercept-minor-mode
372 viper-insert-intercept-map
)
373 (cons 'viper-replace-minor-mode viper-replace-map
)
374 ;; viper-insert-minibuffer-minor-mode must come after
375 ;; viper-replace-minor-mode
376 (cons 'viper-insert-minibuffer-minor-mode
377 viper-minibuffer-map
)
378 (cons 'viper-insert-local-user-minor-mode
379 viper-insert-local-user-map
)
380 (cons 'viper-insert-kbd-minor-mode viper-insert-kbd-map
)
381 (cons 'viper-insert-global-user-minor-mode
382 viper-insert-global-user-map
)
383 (cons 'viper-insert-state-modifier-minor-mode
385 (cdr (assoc major-mode
386 viper-insert-state-modifier-alist
)))
387 (cdr (assoc major-mode
388 viper-insert-state-modifier-alist
))
390 (cons 'viper-insert-diehard-minor-mode viper-insert-diehard-map
)
391 (cons 'viper-insert-basic-minor-mode viper-insert-basic-map
)
392 (cons 'viper-emacs-intercept-minor-mode
393 viper-emacs-intercept-map
)
394 (cons 'viper-emacs-local-user-minor-mode
395 viper-emacs-local-user-map
)
396 (cons 'viper-emacs-kbd-minor-mode viper-emacs-kbd-map
)
397 (cons 'viper-emacs-global-user-minor-mode
398 viper-emacs-global-user-map
)
399 (cons 'viper-emacs-state-modifier-minor-mode
402 (assoc major-mode viper-emacs-state-modifier-alist
)))
404 (assoc major-mode viper-emacs-state-modifier-alist
))
407 minor-mode-map-alist
)))
413 ;; Viper mode-changing commands and utilities
415 ;; Modifies mode-line-buffer-identification.
416 (defun viper-refresh-mode-line ()
417 (setq viper-mode-string
418 (cond ((eq viper-current-state
'emacs-state
) viper-emacs-state-id
)
419 ((eq viper-current-state
'vi-state
) viper-vi-state-id
)
420 ((eq viper-current-state
'replace-state
) viper-replace-state-id
)
421 ((eq viper-current-state
'insert-state
) viper-insert-state-id
)))
423 ;; Sets Viper mode string in global-mode-string
424 (force-mode-line-update))
427 ;; Switch from Insert state to Vi state.
428 (defun viper-exit-insert-state ()
430 (viper-change-state-to-vi))
432 (defun viper-set-mode-vars-for (state)
433 "Sets Viper minor mode variables to put Viper's state STATE in effect."
436 (setq viper-vi-minibuffer-minor-mode nil
437 viper-insert-minibuffer-minor-mode nil
438 viper-vi-intercept-minor-mode nil
439 viper-insert-intercept-minor-mode nil
441 viper-vi-local-user-minor-mode nil
442 viper-vi-kbd-minor-mode nil
443 viper-vi-global-user-minor-mode nil
444 viper-vi-state-modifier-minor-mode nil
445 viper-vi-diehard-minor-mode nil
446 viper-vi-basic-minor-mode nil
448 viper-replace-minor-mode nil
450 viper-insert-local-user-minor-mode nil
451 viper-insert-kbd-minor-mode nil
452 viper-insert-global-user-minor-mode nil
453 viper-insert-state-modifier-minor-mode nil
454 viper-insert-diehard-minor-mode nil
455 viper-insert-basic-minor-mode nil
456 viper-emacs-intercept-minor-mode t
457 viper-emacs-local-user-minor-mode t
458 viper-emacs-kbd-minor-mode
(not (viper-is-in-minibuffer))
459 viper-emacs-global-user-minor-mode t
460 viper-emacs-state-modifier-minor-mode t
464 (if (eq state
'vi-state
) ; adjust for vi-state
466 viper-vi-intercept-minor-mode t
467 viper-vi-minibuffer-minor-mode
(viper-is-in-minibuffer)
468 viper-vi-local-user-minor-mode t
469 viper-vi-kbd-minor-mode
(not (viper-is-in-minibuffer))
470 viper-vi-global-user-minor-mode t
471 viper-vi-state-modifier-minor-mode t
472 ;; don't let the diehard keymap block command completion
473 ;; and other things in the minibuffer
474 viper-vi-diehard-minor-mode
(not
475 (or viper-want-emacs-keys-in-vi
476 (viper-is-in-minibuffer)))
477 viper-vi-basic-minor-mode t
478 viper-emacs-intercept-minor-mode nil
479 viper-emacs-local-user-minor-mode nil
480 viper-emacs-kbd-minor-mode nil
481 viper-emacs-global-user-minor-mode nil
482 viper-emacs-state-modifier-minor-mode nil
485 ;; Insert and Replace states
486 (if (member state
'(insert-state replace-state
))
488 viper-insert-intercept-minor-mode t
489 viper-replace-minor-mode
(eq state
'replace-state
)
490 viper-insert-minibuffer-minor-mode
(viper-is-in-minibuffer)
491 viper-insert-local-user-minor-mode t
492 viper-insert-kbd-minor-mode
(not (viper-is-in-minibuffer))
493 viper-insert-global-user-minor-mode t
494 viper-insert-state-modifier-minor-mode t
495 ;; don't let the diehard keymap block command completion
496 ;; and other things in the minibuffer
497 viper-insert-diehard-minor-mode
(not
499 viper-want-emacs-keys-in-insert
500 (viper-is-in-minibuffer)))
501 viper-insert-basic-minor-mode t
502 viper-emacs-intercept-minor-mode nil
503 viper-emacs-local-user-minor-mode nil
504 viper-emacs-kbd-minor-mode nil
505 viper-emacs-global-user-minor-mode nil
506 viper-emacs-state-modifier-minor-mode nil
510 (if (viper-has-face-support-p)
511 (setq viper-minibuffer-current-face
512 (cond ((eq state
'emacs-state
) viper-minibuffer-emacs-face
)
513 ((eq state
'vi-state
) viper-minibuffer-vi-face
)
514 ((memq state
'(insert-state replace-state
))
515 viper-minibuffer-insert-face
))))
517 (if (viper-is-in-minibuffer)
518 (viper-set-minibuffer-overlay))
521 ;; This also takes care of the annoying incomplete lines in files.
522 ;; Also, this fixes `undo' to work vi-style for complex commands.
523 (defun viper-change-state-to-vi ()
524 "Change Viper state to Vi."
526 (if (and viper-first-time
(not (viper-is-in-minibuffer)))
528 (if overwrite-mode
(overwrite-mode nil
))
529 (if abbrev-mode
(expand-abbrev))
530 (if (and auto-fill-function
(> (current-column) fill-column
))
531 (funcall auto-fill-function
))
532 ;; don't leave whitespace lines around
533 (if (and (memq last-command
535 viper-open-line viper-Open-line
536 viper-replace-state-exit-cmd
))
537 (viper-over-whitespace-line))
538 (indent-to-left-margin))
539 (viper-add-newline-at-eob-if-necessary)
540 (if viper-undo-needs-adjustment
(viper-adjust-undo))
541 (viper-change-state 'vi-state
)
543 ;; always turn off iso-accents-mode, or else we won't be able to use the
544 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
545 (if (and (boundp 'iso-accents-mode
) iso-accents-mode
)
546 (iso-accents-mode -
1))
548 (viper-restore-cursor-color-after-insert)
550 ;; Protection against user errors in hooks
551 (condition-case conds
552 (run-hooks 'viper-vi-state-hook
)
554 (viper-message-conditions conds
)))))
556 (defun viper-change-state-to-insert ()
557 "Change Viper state to Insert."
559 (viper-change-state 'insert-state
)
560 (if (and viper-automatic-iso-accents
(fboundp 'iso-accents-mode
))
561 (iso-accents-mode 1)) ; turn iso accents on
563 (or (stringp viper-saved-cursor-color
)
564 (string= (viper-get-cursor-color) viper-insert-state-cursor-color
)
565 (setq viper-saved-cursor-color
(viper-get-cursor-color)))
566 ;; Commented out, because if viper-change-state-to-insert is executed
567 ;; non-interactively then the old cursor color may get lost. Same old Emacs
568 ;; bug related to local variables?
569 ;;;(if (stringp viper-saved-cursor-color)
570 ;;; (viper-change-cursor-color viper-insert-state-cursor-color))
571 ;; Protection against user errors in hooks
572 (condition-case conds
573 (run-hooks 'viper-insert-state-hook
)
575 (viper-message-conditions conds
))))
577 (defsubst viper-downgrade-to-insert
()
578 (setq viper-current-state
'insert-state
579 viper-replace-minor-mode nil
))
583 ;; Change to replace state. When the end of replacement region is reached,
584 ;; replace state changes to insert state.
585 (defun viper-change-state-to-replace (&optional non-R-cmd
)
586 (viper-change-state 'replace-state
)
587 (if (and viper-automatic-iso-accents
(fboundp 'iso-accents-mode
))
588 (iso-accents-mode 1)) ; turn iso accents on
589 ;; Run insert-state-hook
590 (condition-case conds
591 (run-hooks 'viper-insert-state-hook
'viper-replace-state-hook
)
593 (viper-message-conditions conds
)))
596 (viper-start-replace)
597 ;; 'R' is implemented using Emacs's overwrite-mode
598 (viper-start-R-mode))
602 (defun viper-change-state-to-emacs ()
603 "Change Viper state to Emacs."
605 (viper-change-state 'emacs-state
)
606 (if (and viper-automatic-iso-accents
(fboundp 'iso-accents-mode
))
607 (iso-accents-mode 1)) ; turn iso accents on
609 ;; Protection agains user errors in hooks
610 (condition-case conds
611 (run-hooks 'viper-emacs-state-hook
)
613 (viper-message-conditions conds
))))
615 ;; escape to emacs mode termporarily
616 (defun viper-escape-to-emacs (arg &optional events
)
617 "Escape to Emacs state from Vi state for one Emacs command.
618 ARG is used as the prefix value for the executed command. If
619 EVENTS is a list of events, which become the beginning of the command."
621 (if (= last-command-char ?
\\)
622 (message "Switched to EMACS state for the next command..."))
623 (viper-escape-to-state arg events
'emacs-state
))
625 ;; escape to Vi mode termporarily
626 (defun viper-escape-to-vi (arg)
627 "Escape from Emacs state to Vi state for one Vi 1-character command.
628 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
629 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
630 `viper-escape-to-vi' is used."
632 (message "Switched to VI state for the next command...")
633 (viper-escape-to-state arg nil
'vi-state
))
635 ;; Escape to STATE mode for one Emacs command.
636 (defun viper-escape-to-state (arg events state
)
637 ;;(let (com key prefix-arg)
639 ;; this temporarily turns off Viper's minor mode keymaps
640 (viper-set-mode-vars-for state
)
641 (viper-normalize-minor-mode-map-alist)
642 (if events
(viper-set-unread-command-events events
))
644 ;; protect against keyboard quit and other errors
646 (let (viper-vi-kbd-minor-mode
647 viper-insert-kbd-minor-mode
648 viper-emacs-kbd-minor-mode
)
651 (setq com
(key-binding (setq key
653 (read-key-sequence nil
)
654 (read-key-sequence nil t
)))))
655 ;; In case of binding indirection--chase definitions.
656 ;; Have to do it here because we execute this command under
657 ;; different keymaps, so command-execute may not do the
659 (while (vectorp com
) (setq com
(key-binding com
))))
661 ;; Execute command com in the original Viper state, not in state
662 ;; `state'. Otherwise, if we switch buffers while executing the
663 ;; escaped to command, Viper's mode vars will remain those of
664 ;; `state'. When we return to the orig buffer, the bindings will be
666 (viper-set-mode-vars-for viper-current-state
)
668 ;; this-command, last-command-char, last-command-event
669 (setq this-command com
)
670 (if viper-xemacs-p
; XEmacs represents key sequences as vectors
671 (setq last-command-event
672 (viper-copy-event (viper-seq-last-elt key
))
673 last-command-char
(event-to-character last-command-event
))
674 ;; Emacs represents them as sequences (str or vec)
675 (setq last-command-event
676 (viper-copy-event (viper-seq-last-elt key
))
677 last-command-char last-command-event
))
681 (setq prefix-arg
(or prefix-arg arg
))
682 (command-execute com
)))
686 ;; set state in the new buffer
687 (viper-set-mode-vars-for viper-current-state
))
689 (defun viper-exec-form-in-vi (form)
690 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
691 (let ((buff (current-buffer))
693 (viper-set-mode-vars-for 'vi-state
)
696 (let (viper-vi-kbd-minor-mode) ; execute without kbd macros
697 (setq result
(eval form
))
702 (if (not (equal buff
(current-buffer))) ; cmd switched buffer
705 (viper-set-mode-vars-for viper-current-state
)))
706 (viper-set-mode-vars-for viper-current-state
)
709 (defun viper-exec-form-in-emacs (form)
710 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
711 Similar to viper-escape-to-emacs, but accepts forms rather than keystrokes."
712 (let ((buff (current-buffer))
714 (viper-set-mode-vars-for 'emacs-state
)
715 (setq result
(eval form
))
716 (if (not (equal buff
(current-buffer))) ; cmd switched buffer
719 (viper-set-mode-vars-for viper-current-state
)))
720 (viper-set-mode-vars-for viper-current-state
)
724 ;; This is needed because minor modes sometimes override essential Viper
725 ;; bindings. By letting Viper know which files these modes are in, it will
726 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
727 (defun viper-harness-minor-mode (load-file)
728 "Familiarize Viper with a minor mode defined in LOAD_FILE.
729 Minor modes that have their own keymaps may overshadow Viper keymaps.
730 This function is designed to make Viper aware of the packages that define
733 (viper-harness-minor-mode load-file)
735 LOAD-FILE is a name of the file where the specific minor mode is defined.
736 Suffixes such as .el or .elc should be stripped."
738 (interactive "sEnter name of the load file: ")
740 (eval-after-load load-file
'(viper-normalize-minor-mode-map-alist))
742 ;; Change the default for minor-mode-map-alist each time a harnessed minor
743 ;; mode adds its own keymap to the a-list.
745 load-file
'(setq-default minor-mode-map-alist minor-mode-map-alist
))
749 (defun viper-ESC (arg)
750 "Emulate ESC key in Emacs.
751 Prevents multiple escape keystrokes if viper-no-multiple-ESC is true.
752 If viper-no-multiple-ESC is 'twice double ESC would ding in vi-state.
753 Other ESC sequences are emulated via the current Emacs's major mode
754 keymap. This is more convenient on TTYs, since this won't block
755 function keys such as up,down, etc. ESC will also will also work as
756 a Meta key in this case. When viper-no-multiple-ESC is nil, ESC functions
757 as a Meta key and any number of multiple escapes is allowed."
760 (cond ((and (not viper-no-multiple-ESC
) (eq viper-current-state
'vi-state
))
761 (setq char
(viper-read-char-exclusive))
762 (viper-escape-to-emacs arg
(list ?\e char
) ))
763 ((and (eq viper-no-multiple-ESC
'twice
)
764 (eq viper-current-state
'vi-state
))
765 (setq char
(viper-read-char-exclusive))
766 (if (= char
(string-to-char viper-ESC-key
))
768 (viper-escape-to-emacs arg
(list ?\e char
) )))
772 (defun viper-alternate-Meta-key (arg)
773 "Simulate Emacs Meta key."
775 (sit-for 1) (message "ESC-")
776 (viper-escape-to-emacs arg
'(?\e
)))
778 (defun viper-toggle-key-action ()
779 "Action bound to `viper-toggle-key'."
781 (if (and (< viper-expert-level
2) (equal viper-toggle-key
"\C-z"))
782 (if (viper-window-display-p)
785 (viper-change-state-to-emacs)))
788 ;; Intercept ESC sequences on dumb terminals.
789 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
791 ;; Check if last key was ESC and if so try to reread it as a function key.
792 ;; But only if there are characters to read during a very short time.
793 ;; Returns the last event, if any.
794 (defun viper-envelop-ESC-key ()
795 (let ((event last-input-event
)
798 (if (viper-ESC-event-p event)
800 (if (viper-fast-keysequence-p)
802 (let (minor-mode-map-alist)
803 (viper-set-unread-command-events event)
806 (ad-get-orig-definition 'read-key-sequence) nil))
808 ;; If keyseq translates into something that still has ESC
809 ;; at the beginning, separate ESC from the rest of the seq.
810 ;; In XEmacs we check for events that are keypress meta-key
811 ;; and convert them into [escape key]
813 ;; This is needed for the following reason:
814 ;; If ESC is the first symbol, we interpret it as if the
815 ;; user typed ESC and then quickly some other symbols.
816 ;; If ESC is not the first one, then the key sequence
817 ;; entered was apparently translated into a function key or
818 ;; something (e.g., one may have
819 ;; (define-key function-key-map "\e[192z" [f11])
820 ;; which would translate the escape-sequence generated by
821 ;; f11 in an xterm window into the symbolic key f11.
823 ;; If `first-key' is not an ESC event, we make it into the
824 ;; last-command-event in order to pretend that this key was
825 ;; pressed. This is needed to allow arrow keys to be bound to
826 ;; macros. Otherwise, viper-exec-mapped-kbd-macro will think
827 ;; that the last event was ESC and so it'll execute whatever is
828 ;; bound to ESC. (Viper macros can't be bound to
830 (let* ((first-key (elt keyseq 0))
831 (key-mod (event-modifiers first-key)))
832 (cond ((viper-ESC-event-p first-key)
833 ;; put keys following ESC on the unread list
834 ;; and return ESC as the key-sequence
835 (viper-set-unread-command-events (subseq keyseq 1))
836 (setq last-input-event event
837 keyseq (if viper-emacs-p
839 (vector (character-to-event ?\e)))))
841 (key-press-event-p first-key)
842 (equal '(meta) key-mod))
843 (viper-set-unread-command-events
845 (character-to-event (event-key first-key)))
847 (setq last-input-event event
848 keyseq (vector (character-to-event ?\e))))
850 (setq last-command-event
851 (viper-copy-event first-key)))
855 ;; this is escape event with nothing after it
856 ;; put in unread-command-event and then re-read
857 (viper-set-unread-command-events event)
859 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
861 ;; not an escape event
862 (setq keyseq (vector event)))
867 ;; Listen to ESC key.
868 ;; If a sequence of keys starting with ESC is issued with very short delays,
869 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
870 (defun viper-intercept-ESC-key ()
871 "Function that implements ESC key in Viper emulation of Vi."
873 (let ((cmd (or (key-binding (viper-envelop-ESC-key))
874 '(lambda () (interactive) (error "")))))
876 ;; call the actual function to execute ESC (if no other symbols followed)
877 ;; or the key bound to the ESC sequence (if the sequence was issued
878 ;; with very short delay between characters.
879 (if (eq cmd 'viper-intercept-ESC-key)
881 (cond ((eq viper-current-state 'vi-state)
883 ((eq viper-current-state 'insert-state)
884 'viper-exit-insert-state)
885 ((eq viper-current-state 'replace-state)
886 'viper-replace-state-exit-cmd)
887 (t 'viper-change-state-to-vi)
889 (call-interactively cmd)))
894 ;; prefix argument for Vi mode
896 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
897 ;; represents the numeric value of the prefix argument and COM represents
898 ;; command prefix such as "c", "d", "m" and "y".
900 ;; Get value part of prefix-argument ARG.
901 (defsubst viper-p-val (arg)
904 (if (or (null (car arg)) (equal (car arg) '(nil)))
908 ;; Get raw value part of prefix-argument ARG.
909 (defsubst viper-P-val (arg)
910 (cond ((consp arg) (car arg))
913 ;; Get com part of prefix-argument ARG.
914 (defsubst viper-getcom (arg)
915 (cond ((null arg) nil)
916 ((consp arg) (cdr arg))
919 ;; Get com part of prefix-argument ARG and modify it.
920 (defun viper-getCom (arg)
921 (let ((com (viper-getcom arg)))
922 (cond ((equal com ?c) ?c)
923 ;; Previously, ?c was being converted to ?C, but this prevented
924 ;; multiline replace regions.
925 ;;((equal com ?c) ?C)
931 ;; Compute numeric prefix arg value.
932 ;; Invoked by EVENT. COM is the command part obtained so far.
933 (defun viper-prefix-arg-value (event com)
934 (let ((viper-intermediate-command 'viper-digit-argument)
937 (while (and (viper-characterp event) (>= event ?0) (<= event ?9))
938 (setq value (+ (* (if (integerp value) value 0) 10) (- event ?0)))
939 (setq event (viper-read-event-convert-to-char)))
941 (setq prefix-arg value)
942 (if com (setq prefix-arg (cons prefix-arg com)))
944 (viper-describe-arg prefix-arg)
945 (setq event (viper-read-event-convert-to-char)))
947 (if (or com (and (not (eq viper-current-state 'vi-state))
948 ;; make sure it is a Vi command
949 (viper-characterp event) (viper-vi-command-p event)
951 ;; If appears to be one of the vi commands,
952 ;; then execute it with funcall and clear prefix-arg in order to not
953 ;; confuse subsequent commands
955 ;; last-command-char is the char we want emacs to think was typed
956 ;; last. If com is not nil, the viper-digit-argument command was
957 ;; called from within viper-prefix-arg command, such as `d', `w',
958 ;; etc., i.e., the user typed, say, d2. In this case, `com' would be
959 ;; `d', `w', etc. If viper-digit-argument was invoked by
960 ;; viper-escape-to-vi (which is indicated by the fact that the
961 ;; current state is not vi-state), then `event' represents the vi
962 ;; command to be executed (e.g., `d', `w', etc). Again,
963 ;; last-command-char must make emacs believe that this is the command
965 (cond ((eq event 'return) (setq event ?\C-m))
966 ((eq event 'delete) (setq event ?\C-?))
967 ((eq event 'backspace) (setq event ?\C-h))
968 ((eq event 'space) (setq event ?\ )))
969 (setq last-command-char (or com event))
970 (setq func (viper-exec-form-in-vi
971 (` (key-binding (char-to-string (, event))))))
972 (funcall func prefix-arg)
973 (setq prefix-arg nil))
974 ;; some other command -- let emacs do it in its own way
975 (viper-set-unread-command-events event))
979 ;; Vi operator as prefix argument."
980 (defun viper-prefix-arg-com (char value com)
986 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
987 viper-buffer-search-char)))
989 ;; this means that we already have a command character, so we
990 ;; construct a com list and exit while. however, if char is "
993 ;; new com is (CHAR . OLDCOM)
994 (if (memq char '(?# ?\")) (error ""))
995 (setq com (cons char com))
997 ;; If com is nil we set com as char, and read more. Again, if char is
998 ;; ", we read the name of register and store it in viper-use-register.
999 ;; if char is !, =, or #, a complete com is formed so we exit the while
1001 (cond ((memq char '(?! ?=))
1003 (setq char (read-char))
1006 ;; read a char and encode it as com
1007 (setq com (+ 128 (read-char)))
1008 (setq char (read-char)))
1010 (let ((reg (read-char)))
1011 (if (viper-valid-register reg)
1012 (setq viper-use-register reg)
1014 (setq char (read-char))))
1017 (setq char (read-char))))))
1020 ;; `com' is a single char, so we construct the command argument
1021 ;; and if `char' is `?', we describe the arg; otherwise
1022 ;; we prepare the command that will be executed at the end.
1024 (setq cmd-info (cons value com))
1026 (viper-describe-arg cmd-info)
1027 (setq char (read-char)))
1028 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
1029 ;; execute it at the very end
1030 (or (viper-movement-command-p char)
1031 (viper-digit-command-p char)
1032 (viper-regsuffix-command-p char)
1033 (= char ?!) ; bang command
1035 (setq cmd-to-exec-at-end
1036 (viper-exec-form-in-vi
1037 (` (key-binding (char-to-string (, char)))))))
1039 ;; as com is non-nil, this means that we have a command to execute
1040 (if (memq (car com) '(?r ?R))
1041 ;; execute apropriate region command.
1042 (let ((char (car com)) (com (cdr com)))
1043 (setq prefix-arg (cons value com))
1044 (if (= char ?r) (viper-region prefix-arg)
1045 (viper-Region prefix-arg))
1047 (setq prefix-arg nil))
1048 ;; otherwise, reset prefix arg and call appropriate command
1049 (setq value (if (null value) 1 value))
1050 (setq prefix-arg nil)
1052 ;; If we change ?C to ?c here, then cc will enter replacement mode
1053 ;; rather than deleting lines. However, it will affect 1 less line than
1054 ;; normal. We decided to not use replacement mode here and follow Vi,
1055 ;; since replacement mode on n full lines can be achieved with nC.
1056 ((equal com '(?c . ?c)) (viper-line (cons value ?C)))
1057 ((equal com '(?d . ?d)) (viper-line (cons value ?D)))
1058 ((equal com '(?d . ?y)) (viper-yank-defun))
1059 ((equal com '(?y . ?y)) (viper-line (cons value ?Y)))
1060 ((equal com '(?< . ?<)) (viper-line (cons value ?<)))
1061 ((equal com '(?> . ?>)) (viper-line (cons value ?>)))
1062 ((equal com '(?! . ?!)) (viper-line (cons value ?!)))
1063 ((equal com '(?= . ?=)) (viper-line (cons value ?=)))
1066 (if cmd-to-exec-at-end
1068 (setq last-command-char char)
1069 (setq last-command-event
1071 (if viper-xemacs-p (character-to-event char) char)))
1073 (funcall cmd-to-exec-at-end cmd-info)
1078 (defun viper-describe-arg (arg)
1080 (setq val (viper-P-val arg)
1081 com (viper-getcom arg))
1084 (message "Value is nil, and command is nil")
1085 (message "Value is nil, and command is `%c'" com))
1087 (message "Value is `%d', and command is nil" val)
1088 (message "Value is `%d', and command is `%c'" val com)))))
1090 (defun viper-digit-argument (arg)
1091 "Begin numeric argument for the next command."
1093 (viper-leave-region-active)
1094 (viper-prefix-arg-value
1095 last-command-char (if (consp arg) (cdr arg) nil)))
1097 (defun viper-command-argument (arg)
1098 "Accept a motion command as an argument."
1100 (let ((viper-intermediate-command 'viper-command-argument))
1102 (viper-prefix-arg-com
1104 (cond ((null arg) nil)
1105 ((consp arg) (car arg))
1106 ((integerp arg) arg)
1107 (t (error viper-InvalidCommandArgument)))
1108 (cond ((null arg) nil)
1109 ((consp arg) (cdr arg))
1110 ((integerp arg) nil)
1111 (t (error viper-InvalidCommandArgument))))
1112 (quit (setq viper-use-register nil)
1113 (signal 'quit nil)))
1114 (viper-deactivate-mark)))
1117 ;; repeat last destructive command
1119 ;; Append region to text in register REG.
1120 ;; START and END are buffer positions indicating what to append.
1121 (defsubst viper-append-to-register (reg start end)
1122 (set-register reg (concat (if (stringp (get-register reg))
1123 (get-register reg) "")
1124 (buffer-substring start end))))
1126 ;; Saves last inserted text for possible use by viper-repeat command.
1127 (defun viper-save-last-insertion (beg end)
1128 (setq viper-last-insertion (buffer-substring beg end))
1129 (or (< (length viper-d-com) 5)
1130 (setcar (nthcdr 4 viper-d-com) viper-last-insertion))
1131 (or (null viper-command-ring)
1132 (ring-empty-p viper-command-ring)
1134 (setcar (nthcdr 4 (viper-current-ring-item viper-command-ring))
1135 viper-last-insertion)
1136 ;; del most recent elt, if identical to the second most-recent
1137 (viper-cleanup-ring viper-command-ring)))
1140 (defsubst viper-yank-last-insertion ()
1141 "Inserts the text saved by the previous viper-save-last-insertion command."
1143 (insert viper-last-insertion)
1147 ;; define functions to be executed
1149 ;; invoked by the `C' command
1150 (defun viper-exec-change (m-com com)
1151 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1152 (set-marker viper-com-point (point) (current-buffer)))
1153 ;; handle C cmd at the eol and at eob.
1154 (if (or (and (eolp) (= viper-com-point (point)))
1155 (= viper-com-point (point-max)))
1157 (insert " ")(backward-char 1)))
1158 (if (= viper-com-point (point))
1159 (viper-forward-char-carefully))
1160 (set-mark viper-com-point)
1161 (if (eq m-com 'viper-next-line-at-bol)
1162 (viper-enlarge-region (mark t) (point)))
1163 (if (< (point) (mark t))
1164 (exchange-point-and-mark))
1165 (if (eq (preceding-char) ?\n)
1166 (viper-backward-char-carefully)) ; give back the newline
1168 (viper-change (mark t) (point))
1169 (viper-change-subr (mark t) (point))))
1171 ;; this is invoked by viper-substitute-line
1172 (defun viper-exec-Change (m-com com)
1174 (set-mark viper-com-point)
1175 (viper-enlarge-region (mark t) (point))
1176 (if viper-use-register
1178 (cond ((viper-valid-register viper-use-register '(letter digit))
1180 viper-use-register (mark t) (point) nil))
1181 ((viper-valid-register viper-use-register '(Letter))
1182 (viper-append-to-register
1183 (downcase viper-use-register) (mark t) (point)))
1184 (t (setq viper-use-register nil)
1185 (error viper-InvalidRegister viper-use-register)))
1186 (setq viper-use-register nil)))
1187 (delete-region (mark t) (point)))
1190 (viper-change-state-to-insert)
1191 (viper-yank-last-insertion)))
1193 (defun viper-exec-delete (m-com com)
1194 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1195 (set-marker viper-com-point (point) (current-buffer)))
1196 (if viper-use-register
1198 (cond ((viper-valid-register viper-use-register '(letter digit))
1200 viper-use-register viper-com-point (point) nil))
1201 ((viper-valid-register viper-use-register '(Letter))
1202 (viper-append-to-register
1203 (downcase viper-use-register) viper-com-point (point)))
1204 (t (setq viper-use-register nil)
1205 (error viper-InvalidRegister viper-use-register)))
1206 (setq viper-use-register nil)))
1208 (if (eq last-command 'd-command) 'kill-region nil))
1209 (kill-region viper-com-point (point))
1210 (setq this-command 'd-command)
1211 (if viper-ex-style-motion
1212 (if (and (eolp) (not (bolp))) (backward-char 1))))
1214 (defun viper-exec-Delete (m-com com)
1216 (set-mark viper-com-point)
1217 (viper-enlarge-region (mark t) (point))
1218 (if viper-use-register
1220 (cond ((viper-valid-register viper-use-register '(letter digit))
1222 viper-use-register (mark t) (point) nil))
1223 ((viper-valid-register viper-use-register '(Letter))
1224 (viper-append-to-register
1225 (downcase viper-use-register) (mark t) (point)))
1226 (t (setq viper-use-register nil)
1227 (error viper-InvalidRegister viper-use-register)))
1228 (setq viper-use-register nil)))
1230 (if (eq last-command 'D-command) 'kill-region nil))
1231 (kill-region (mark t) (point))
1232 (if (eq m-com 'viper-line) (setq this-command 'D-command)))
1233 (back-to-indentation))
1235 (defun viper-exec-yank (m-com com)
1236 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1237 (set-marker viper-com-point (point) (current-buffer)))
1238 (if viper-use-register
1240 (cond ((viper-valid-register viper-use-register '(letter digit))
1242 viper-use-register viper-com-point (point) nil))
1243 ((viper-valid-register viper-use-register '(Letter))
1244 (viper-append-to-register
1245 (downcase viper-use-register) viper-com-point (point)))
1246 (t (setq viper-use-register nil)
1247 (error viper-InvalidRegister viper-use-register)))
1248 (setq viper-use-register nil)))
1249 (setq last-command nil)
1250 (copy-region-as-kill viper-com-point (point))
1251 (goto-char viper-com-point))
1253 (defun viper-exec-Yank (m-com com)
1255 (set-mark viper-com-point)
1256 (viper-enlarge-region (mark t) (point))
1257 (if viper-use-register
1259 (cond ((viper-valid-register viper-use-register '(letter digit))
1261 viper-use-register (mark t) (point) nil))
1262 ((viper-valid-register viper-use-register '(Letter))
1263 (viper-append-to-register
1264 (downcase viper-use-register) (mark t) (point)))
1265 (t (setq viper-use-register nil)
1266 (error viper-InvalidRegister viper-use-register)))
1267 (setq viper-use-register nil)))
1268 (setq last-command nil)
1269 (copy-region-as-kill (mark t) (point)))
1270 (viper-deactivate-mark)
1271 (goto-char viper-com-point))
1273 (defun viper-exec-bang (m-com com)
1275 (set-mark viper-com-point)
1276 (viper-enlarge-region (mark t) (point))
1277 (exchange-point-and-mark)
1278 (shell-command-on-region
1281 (setq viper-last-shell-com
1282 (viper-read-string-with-history
1285 'viper-shell-history
1286 (car viper-shell-history)
1288 viper-last-shell-com)
1291 (defun viper-exec-equals (m-com com)
1293 (set-mark viper-com-point)
1294 (viper-enlarge-region (mark t) (point))
1295 (if (> (mark t) (point)) (exchange-point-and-mark))
1296 (indent-region (mark t) (point) nil)))
1298 (defun viper-exec-shift (m-com com)
1300 (set-mark viper-com-point)
1301 (viper-enlarge-region (mark t) (point))
1302 (if (> (mark t) (point)) (exchange-point-and-mark))
1303 (indent-rigidly (mark t) (point)
1306 (- viper-shift-width))))
1307 ;; return point to where it was before shift
1308 (goto-char viper-com-point))
1310 ;; this is needed because some commands fake com by setting it to ?r, which
1311 ;; denotes repeated insert command.
1312 (defsubst viper-exec-dummy (m-com com)
1315 (defun viper-exec-buffer-search (m-com com)
1316 (setq viper-s-string (buffer-substring (point) viper-com-point))
1317 (setq viper-s-forward t)
1318 (setq viper-search-history (cons viper-s-string viper-search-history))
1319 (viper-search viper-s-string viper-s-forward 1))
1321 (defvar viper-exec-array (make-vector 128 nil))
1323 ;; Using a dispatch array allows adding functions like buffer search
1324 ;; without affecting other functions. Buffer search can now be bound
1325 ;; to any character.
1327 (aset viper-exec-array ?c 'viper-exec-change)
1328 (aset viper-exec-array ?C 'viper-exec-Change)
1329 (aset viper-exec-array ?d 'viper-exec-delete)
1330 (aset viper-exec-array ?D 'viper-exec-Delete)
1331 (aset viper-exec-array ?y 'viper-exec-yank)
1332 (aset viper-exec-array ?Y 'viper-exec-Yank)
1333 (aset viper-exec-array ?r 'viper-exec-dummy)
1334 (aset viper-exec-array ?! 'viper-exec-bang)
1335 (aset viper-exec-array ?< 'viper-exec-shift)
1336 (aset viper-exec-array ?> 'viper-exec-shift)
1337 (aset viper-exec-array ?= 'viper-exec-equals)
1341 ;; This function is called by various movement commands to execute a
1342 ;; destructive command on the region specified by the movement command. For
1343 ;; instance, if the user types cw, then the command viper-forward-word will
1344 ;; call viper-execute-com to execute viper-exec-change, which eventually will
1345 ;; call viper-change to invoke the replace mode on the region.
1347 ;; The var viper-d-com is set to (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS)
1348 ;; via a call to viper-set-destructive-command, for later use by viper-repeat.
1349 (defun viper-execute-com (m-com val com)
1350 (let ((reg viper-use-register))
1351 ;; this is the special command `#'
1353 (viper-special-prefix-com (- com 128))
1354 (let ((fn (aref viper-exec-array (if (< com 0) (- com) com))))
1356 (error "%c: %s" com viper-InvalidViCommand)
1357 (funcall fn m-com com))))
1358 (if (viper-dotable-command-p com)
1359 (viper-set-destructive-command
1361 (if (memq com (list ?c ?C ?!)) (- com) com)
1366 (defun viper-repeat (arg)
1367 "Re-execute last destructive command.
1368 Use the info in viper-d-com, which has the form
1369 \(com val ch reg inserted-text command-keys\),
1370 where `com' is the command to be re-executed, `val' is the
1371 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1372 if it exists, it is the name of the register for `com'.
1373 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
1375 (let ((save-point (point)) ; save point before repeating prev cmd
1376 ;; Pass along that we are repeating a destructive command
1377 ;; This tells viper-set-destructive-command not to update
1378 ;; viper-command-ring
1379 (viper-intermediate-command 'viper-repeat))
1380 (if (eq last-command 'viper-undo)
1381 ;; if the last command was viper-undo, then undo-more
1383 ;; otherwise execute the command stored in viper-d-com. if arg is
1384 ;; non-nil its prefix value is used as new prefix value for the command.
1385 (let ((m-com (car viper-d-com))
1386 (val (viper-P-val arg))
1387 (com (nth 2 viper-d-com))
1388 (reg (nth 3 viper-d-com)))
1389 (if (null val) (setq val (nth 1 viper-d-com)))
1390 (if (null m-com) (error "No previous command to repeat."))
1391 (setq viper-use-register reg)
1392 (if (nth 4 viper-d-com) ; text inserted by command
1393 (setq viper-last-insertion (nth 4 viper-d-com)
1394 viper-d-char (nth 4 viper-d-com)))
1395 (funcall m-com (cons val com))
1396 (cond ((and (< save-point (point)) viper-keep-point-on-repeat)
1397 (goto-char save-point)) ; go back to before repeat.
1398 ((and (< save-point (point)) viper-ex-style-editing-in-insert)
1399 (or (bolp) (backward-char 1))))
1400 (if (and (eolp) (not (bolp)))
1403 (if viper-undo-needs-adjustment (viper-adjust-undo)) ; take care of undo
1404 ;; If the prev cmd was rotating the command ring, this means that `.' has
1405 ;; just executed a command from that ring. So, push it on the ring again.
1406 ;; If we are just executing previous command , then don't push viper-d-com
1407 ;; because viper-d-com is not fully constructed in this case (its keys and
1408 ;; the inserted text may be nil). Besides, in this case, the command
1409 ;; executed by `.' is already on the ring.
1410 (if (eq last-command 'viper-display-current-destructive-command)
1411 (viper-push-onto-ring viper-d-com 'viper-command-ring))
1412 (viper-deactivate-mark)
1415 (defun viper-repeat-from-history ()
1416 "Repeat a destructive command from history.
1417 Doesn't change viper-command-ring in any way, so `.' will work as before
1418 executing this command.
1419 This command is supposed to be bound to a two-character Vi macro where
1420 the second character is a digit 0 to 9. The digit indicates which
1421 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1422 invokes the command before that, etc."
1424 (let* ((viper-intermediate-command 'repeating-display-destructive-command)
1425 (idx (cond (viper-this-kbd-macro
1427 (symbol-name (elt viper-this-kbd-macro 1))))
1430 (viper-d-com viper-d-com))
1432 (or (and (numberp num) (<= 0 num) (<= num 9))
1437 "`viper-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1439 (setq viper-d-com (viper-special-ring-rotate1 viper-command-ring -1))
1440 (setq num (1- num)))
1443 (viper-special-ring-rotate1 viper-command-ring 1)
1444 (setq num (1+ num)))
1448 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1449 ;; The chars that can follow `#' are determined by viper-hash-command-p
1450 (defun viper-special-prefix-com (char)
1452 (downcase-region (min viper-com-point (point))
1453 (max viper-com-point (point))))
1455 (upcase-region (min viper-com-point (point))
1456 (max viper-com-point (point))))
1458 (push-mark viper-com-point t)
1459 (viper-global-execute))
1461 (push-mark viper-com-point t)
1462 (viper-quote-region))
1463 ((= char ?s) (funcall viper-spell-function viper-com-point (point)))
1464 (t (error "#%c: %s" char viper-InvalidViCommand))))
1469 (defun viper-undo ()
1470 "Undo previous change."
1473 (let ((modified (buffer-modified-p))
1474 (before-undo-pt (point-marker))
1475 (after-change-functions after-change-functions)
1476 undo-beg-posn undo-end-posn)
1478 ;; no need to remove this hook, since this var has scope inside a let.
1479 (add-hook 'after-change-functions
1480 '(lambda (beg end len)
1481 (setq undo-beg-posn beg
1482 undo-end-posn (or end beg))))
1486 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1487 undo-end-posn (or undo-end-posn undo-beg-posn))
1489 (goto-char undo-beg-posn)
1491 (if (and viper-keep-point-on-undo
1492 (pos-visible-in-window-p before-undo-pt))
1494 (push-mark (point-marker) t)
1495 (viper-sit-for-short 300)
1496 (goto-char undo-end-posn)
1497 (viper-sit-for-short 300)
1498 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
1499 (> (abs (- undo-end-posn before-undo-pt)) 1))
1500 (goto-char before-undo-pt)
1501 (goto-char undo-beg-posn)))
1502 (push-mark before-undo-pt t))
1503 (if (and (eolp) (not (bolp))) (backward-char 1))
1504 (if (not modified) (set-buffer-modified-p t)))
1505 (setq this-command 'viper-undo))
1507 ;; Continue undoing previous changes.
1508 (defun viper-undo-more ()
1509 (message "undo more!")
1513 (message "No further undo information in this buffer")))
1514 (if (and (eolp) (not (bolp))) (backward-char 1))
1515 (setq this-command 'viper-undo))
1517 ;; The following two functions are used to set up undo properly.
1518 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1519 ;; they are undone all at once.
1520 (defun viper-adjust-undo ()
1521 (let ((inhibit-quit t)
1523 (setq viper-undo-needs-adjustment nil)
1524 (if (listp buffer-undo-list)
1525 (if (setq tmp (memq viper-buffer-undo-list-mark buffer-undo-list))
1527 (setq tmp2 (cdr tmp)) ; the part after mark
1529 ;; cut tail from buffer-undo-list temporarily by direct
1530 ;; manipulation with pointers in buffer-undo-list
1533 (setq buffer-undo-list (delq nil buffer-undo-list))
1534 (setq buffer-undo-list
1535 (delq viper-buffer-undo-list-mark buffer-undo-list))
1536 ;; restore tail of buffer-undo-list
1537 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1538 (setq buffer-undo-list (delq nil buffer-undo-list))))))
1541 (defun viper-set-complex-command-for-undo ()
1542 (if (listp buffer-undo-list)
1543 (if (not viper-undo-needs-adjustment)
1544 (let ((inhibit-quit t))
1545 (setq buffer-undo-list
1546 (cons viper-buffer-undo-list-mark buffer-undo-list))
1547 (setq viper-undo-needs-adjustment t)))))
1552 (defun viper-display-current-destructive-command ()
1553 (let ((text (nth 4 viper-d-com))
1554 (keys (nth 5 viper-d-com))
1557 (setq this-command 'viper-display-current-destructive-command)
1559 (message " `.' runs %s%s"
1560 (concat "`" (viper-array-to-string keys) "'")
1561 (viper-abbreviate-string
1563 (replace-in-string text "\n" "^J")
1566 " inserting `" "'" " ......."))
1570 ;; don't change viper-d-com if it was viper-repeat command invoked with `.'
1571 ;; or in some other way (non-interactively).
1572 (defun viper-set-destructive-command (list)
1573 (or (eq viper-intermediate-command 'viper-repeat)
1575 (setq viper-d-com list)
1576 (setcar (nthcdr 5 viper-d-com)
1577 (viper-array-to-string (if (arrayp viper-this-command-keys)
1578 viper-this-command-keys
1579 (this-command-keys))))
1580 (viper-push-onto-ring viper-d-com 'viper-command-ring)))
1581 (setq viper-this-command-keys nil))
1583 (defun viper-prev-destructive-command (next)
1584 "Find previous destructive command in the history of destructive commands.
1585 With prefix argument, find next destructive command."
1587 (let (cmd viper-intermediate-command)
1588 (if (eq last-command 'viper-display-current-destructive-command)
1589 ;; repeated search through command history
1590 (setq viper-intermediate-command
1591 'repeating-display-destructive-command)
1592 ;; first search through command history--set temp ring
1593 (setq viper-temp-command-ring (copy-list viper-command-ring)))
1595 (viper-special-ring-rotate1 viper-temp-command-ring 1)
1596 (viper-special-ring-rotate1 viper-temp-command-ring -1)))
1599 (setq viper-d-com cmd))
1600 (viper-display-current-destructive-command)))
1602 (defun viper-next-destructive-command ()
1603 "Find next destructive command in the history of destructive commands."
1605 (viper-prev-destructive-command 'next))
1607 (defun viper-insert-prev-from-insertion-ring (arg)
1608 "Cycle through insertion ring in the direction of older insertions.
1609 Undoes previous insertion and inserts new.
1610 With prefix argument, cycles in the direction of newer elements.
1611 In minibuffer, this command executes whatever the invocation key is bound
1612 to in the global map, instead of cycling through the insertion ring."
1614 (let (viper-intermediate-command)
1615 (if (eq last-command 'viper-insert-from-insertion-ring)
1616 (progn ; repeated search through insertion history
1617 (setq viper-intermediate-command 'repeating-insertion-from-ring)
1618 (if (eq viper-current-state 'replace-state)
1620 (if viper-last-inserted-string-from-insertion-ring
1621 (backward-delete-char
1622 (length viper-last-inserted-string-from-insertion-ring))))
1624 ;;first search through insertion history
1625 (setq viper-temp-insertion-ring (copy-list viper-insertion-ring)))
1626 (setq this-command 'viper-insert-from-insertion-ring)
1627 ;; so that things will be undone properly
1628 (setq buffer-undo-list (cons nil buffer-undo-list))
1629 (setq viper-last-inserted-string-from-insertion-ring
1630 (viper-special-ring-rotate1 viper-temp-insertion-ring (if arg 1 -1)))
1632 ;; this change of viper-intermediate-command must come after
1633 ;; viper-special-ring-rotate1, so that the ring will rotate, but before the
1635 (setq viper-intermediate-command nil)
1636 (if viper-last-inserted-string-from-insertion-ring
1637 (insert viper-last-inserted-string-from-insertion-ring))
1640 (defun viper-insert-next-from-insertion-ring ()
1641 "Cycle through insertion ring in the direction of older insertions.
1642 Undo previous insertion and inserts new."
1644 (viper-insert-prev-from-insertion-ring 'next))
1647 ;; some region utilities
1649 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1650 (defun viper-add-newline-at-eob-if-necessary ()
1653 ;; make sure all lines end with newline, unless in the minibuffer or
1654 ;; when requested otherwise (require-final-newline is nil)
1657 require-final-newline
1658 (not (viper-is-in-minibuffer))
1659 (not buffer-read-only))
1662 (defun viper-yank-defun ()
1664 (copy-region-as-kill (point) (mark t)))
1666 ;; Enlarge region between BEG and END.
1667 (defun viper-enlarge-region (beg end)
1668 (or beg (setq beg end)) ; if beg is nil, set to end
1669 (or end (setq end beg)) ; if end is nil, set to beg
1672 (progn (goto-char beg) (set-mark end))
1676 (exchange-point-and-mark)
1677 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1678 (if (not (eobp)) (beginning-of-line))
1679 (if (> beg end) (exchange-point-and-mark)))
1682 ;; Quote region by each line with a user supplied string.
1683 (defun viper-quote-region ()
1684 (setq viper-quote-string
1685 (viper-read-string-with-history
1688 'viper-quote-region-history
1689 viper-quote-string))
1690 (viper-enlarge-region (point) (mark t))
1691 (if (> (point) (mark t)) (exchange-point-and-mark))
1692 (insert viper-quote-string)
1695 (while (and (< (point) (mark t)) (bolp))
1696 (insert viper-quote-string)
1700 ;; Tells whether BEG is on the same line as END.
1701 ;; If one of the args is nil, it'll return nil.
1702 (defun viper-same-line (beg end)
1703 (let ((selective-display nil)
1706 (if (and beg end (> beg end))
1711 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
1714 ;; This 'if' is needed because Emacs treats the next empty line
1715 ;; as part of the previous line.
1716 (if (= (viper-line-pos 'start) end)
1718 (<= (+ incr (count-lines beg end)) 1))))
1722 ;; Check if the string ends with a newline.
1723 (defun viper-end-with-a-newline-p (string)
1724 (or (string= string "")
1725 (= (viper-seq-last-elt string) ?\n)))
1727 (defun viper-tmp-insert-at-eob (msg)
1728 (let ((savemax (point-max)))
1732 (goto-char savemax) (delete-region (point) (point-max))
1737 ;;; Minibuffer business
1739 (defsubst viper-set-minibuffer-style ()
1740 (add-hook 'minibuffer-setup-hook 'viper-minibuffer-setup-sentinel))
1743 (defun viper-minibuffer-setup-sentinel ()
1744 (let ((hook (if viper-vi-style-in-minibuffer
1745 'viper-change-state-to-insert
1746 'viper-change-state-to-emacs)))
1750 ;; Interpret last event in the local map
1751 (defun viper-exit-minibuffer ()
1754 (setq command (local-key-binding (char-to-string last-command-char)))
1756 (command-execute command)
1757 (exit-minibuffer))))
1760 ;;; Reading string with history
1762 (defun viper-read-string-with-history (prompt &optional initial
1763 history-var default keymap)
1764 ;; Read string, prompting with PROMPT and inserting the INITIAL
1765 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
1766 ;; input is an empty string. Use KEYMAP, if given, or the
1767 ;; minibuffer-local-map.
1768 ;; Default value is displayed until the user types something in the
1770 (let ((minibuffer-setup-hook
1772 (if (stringp initial)
1774 ;; don't wait if we have unread events or in kbd macro
1775 (or unread-command-events
1780 (viper-minibuffer-setup-sentinel)))
1785 (setq keymap (or keymap minibuffer-local-map)
1786 initial (or initial "")
1787 temp-msg (if default
1788 (format "(default: %s) " default)
1791 (setq viper-incomplete-ex-cmd nil)
1792 (setq val (read-from-minibuffer prompt
1793 (concat temp-msg initial val padding)
1794 keymap nil history-var))
1795 (setq minibuffer-setup-hook nil
1796 padding (viper-array-to-string (this-command-keys))
1798 ;; the following tries to be smart about what to put in history
1799 (if (not (string= val (car (eval history-var))))
1800 (set history-var (cons val (eval history-var))))
1801 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
1802 (string= (nth 0 (eval history-var)) ""))
1803 (set history-var (cdr (eval history-var))))
1804 ;; If the user enters nothing but the prev cmd wasn't viper-ex,
1805 ;; viper-command-argument, or `! shell-command', this probably means
1806 ;; that the user typed something then erased. Return "" in this case, not
1807 ;; the default---the default is too confusing in this case.
1808 (cond ((and (string= val "")
1809 (not (string= prompt "!")) ; was a `! shell-command'
1810 (not (memq last-command
1812 viper-command-argument
1816 ((string= val "") (or default ""))
1822 ;; insertion commands
1824 ;; Called when state changes from Insert Vi command mode.
1825 ;; Repeats the insertion command if Insert state was entered with prefix
1827 (defun viper-repeat-insert-command ()
1828 (let ((i-com (car viper-d-com))
1829 (val (nth 1 viper-d-com))
1830 (char (nth 2 viper-d-com)))
1831 (if (and val (> val 1)) ; first check that val is non-nil
1833 (setq viper-d-com (list i-com (1- val) ?r nil nil nil))
1835 (setq viper-d-com (list i-com val char nil nil nil))
1838 (defun viper-insert (arg)
1839 "Insert before point."
1841 (viper-set-complex-command-for-undo)
1842 (let ((val (viper-p-val arg))
1843 (com (viper-getcom arg)))
1844 (viper-set-destructive-command (list 'viper-insert val ?r nil nil nil))
1846 (viper-loop val (viper-yank-last-insertion))
1847 (viper-change-state-to-insert))))
1849 (defun viper-append (arg)
1850 "Append after point."
1852 (viper-set-complex-command-for-undo)
1853 (let ((val (viper-p-val arg))
1854 (com (viper-getcom arg)))
1855 (viper-set-destructive-command (list 'viper-append val ?r nil nil nil))
1856 (if (not (eolp)) (forward-char))
1858 (viper-loop val (viper-yank-last-insertion))
1859 (viper-change-state-to-insert))))
1861 (defun viper-Append (arg)
1862 "Append at end of line."
1864 (viper-set-complex-command-for-undo)
1865 (let ((val (viper-p-val arg))
1866 (com (viper-getcom arg)))
1867 (viper-set-destructive-command (list 'viper-Append val ?r nil nil nil))
1870 (viper-loop val (viper-yank-last-insertion))
1871 (viper-change-state-to-insert))))
1873 (defun viper-Insert (arg)
1874 "Insert before first non-white."
1876 (viper-set-complex-command-for-undo)
1877 (let ((val (viper-p-val arg))
1878 (com (viper-getcom arg)))
1879 (viper-set-destructive-command (list 'viper-Insert val ?r nil nil nil))
1880 (back-to-indentation)
1882 (viper-loop val (viper-yank-last-insertion))
1883 (viper-change-state-to-insert))))
1885 (defun viper-open-line (arg)
1888 (viper-set-complex-command-for-undo)
1889 (let ((val (viper-p-val arg))
1890 (com (viper-getcom arg)))
1891 (viper-set-destructive-command (list 'viper-open-line val ?r nil nil nil))
1892 (let ((col (current-indentation)))
1898 (if viper-auto-indent
1901 (if viper-electric-mode
1902 (indent-according-to-mode)
1905 (viper-yank-last-insertion)))
1908 (if viper-auto-indent
1911 (if viper-electric-mode
1912 (indent-according-to-mode)
1914 (viper-change-state-to-insert)))))
1916 (defun viper-Open-line (arg)
1919 (viper-set-complex-command-for-undo)
1920 (let ((val (viper-p-val arg))
1921 (com (viper-getcom arg)))
1922 (viper-set-destructive-command (list 'viper-Open-line val ?r nil nil nil))
1923 (let ((col (current-indentation)))
1929 (if viper-auto-indent
1932 (if viper-electric-mode
1933 (indent-according-to-mode)
1936 (viper-yank-last-insertion)))
1939 (if viper-auto-indent
1942 (if viper-electric-mode
1943 (indent-according-to-mode)
1946 (viper-change-state-to-insert)))))
1948 (defun viper-open-line-at-point (arg)
1949 "Open line at point."
1951 (viper-set-complex-command-for-undo)
1952 (let ((val (viper-p-val arg))
1953 (com (viper-getcom arg)))
1954 (viper-set-destructive-command
1955 (list 'viper-open-line-at-point val ?r nil nil nil))
1960 (viper-yank-last-insertion)))
1962 (viper-change-state-to-insert))))
1964 (defun viper-substitute (arg)
1965 "Substitute characters."
1967 (let ((val (viper-p-val arg))
1968 (com (viper-getcom arg)))
1972 (viper-change-subr (mark t) (point))
1973 (viper-change (mark t) (point)))
1974 (viper-set-destructive-command (list 'viper-substitute val ?r nil nil nil))
1977 ;; Command bound to S
1978 (defun viper-substitute-line (arg)
1981 (viper-set-complex-command-for-undo)
1982 (viper-line (cons arg ?C)))
1984 ;; Prepare for replace
1985 (defun viper-start-replace ()
1986 (setq viper-began-as-replace t
1987 viper-sitting-in-replace t
1988 viper-replace-chars-to-delete 0
1989 viper-replace-chars-deleted 0)
1991 'viper-after-change-functions 'viper-replace-mode-spy-after t)
1993 'viper-before-change-functions 'viper-replace-mode-spy-before t)
1994 ;; this will get added repeatedly, but no harm
1995 (add-hook 'after-change-functions 'viper-after-change-sentinel t)
1996 (add-hook 'before-change-functions 'viper-before-change-sentinel t)
1997 (viper-move-marker-locally 'viper-last-posn-in-replace-region
1998 (viper-replace-start))
2000 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel t)
2002 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t)
2003 ;; guard against a smartie who switched from R-replace to normal replace
2005 'viper-post-command-hooks 'viper-R-state-post-command-sentinel)
2006 (if overwrite-mode (overwrite-mode nil))
2010 ;; checks how many chars were deleted by the last change
2011 (defun viper-replace-mode-spy-before (beg end)
2012 (setq viper-replace-chars-deleted
2014 (max 0 (- end (viper-replace-end)))
2015 (max 0 (- (viper-replace-start) beg))
2018 ;; Invoked as an after-change-function to set up parameters of the last change
2019 (defun viper-replace-mode-spy-after (beg end length)
2020 (if (memq viper-intermediate-command '(repeating-insertion-from-ring))
2022 (setq viper-replace-chars-to-delete 0)
2023 (viper-move-marker-locally
2024 'viper-last-posn-in-replace-region (point)))
2026 (let (beg-col end-col real-end chars-to-delete)
2027 (setq real-end (min end (viper-replace-end)))
2030 (setq beg-col (current-column))
2031 (goto-char real-end)
2032 (setq end-col (current-column)))
2034 ;; If beg of change is outside the replacement region, then don't
2035 ;; delete anything in the repl region (set chars-to-delete to 0).
2037 ;; This works fine except that we have to take special care of
2038 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
2039 ;; sometimes simply shifts the repl region rightwards, without
2040 ;; deleting an equal amount of characters.
2042 ;; The reason why new-dabbrev.el causes this are this:
2043 ;; if one dinamically completes a partial word that starts before the
2044 ;; replacement region (but ends inside) then new-dabbrev.el first
2045 ;; moves cursor backwards, to the beginning of the word to be
2046 ;; completed (say, pt A). Then it inserts the
2047 ;; completed word and then deletes the old, incomplete part.
2048 ;; Since the complete word is inserted at position before the repl
2049 ;; region, the next If-statement would have set chars-to-delete to 0
2050 ;; unless we check for the current command, which must be
2053 ;; In fact, it might be also useful to have overlays for insert
2054 ;; regions as well, since this will let us capture the situation when
2055 ;; dabbrev-expand goes back past the insertion point to find the
2056 ;; beginning of the word to be expanded.
2057 (if (or (and (<= (viper-replace-start) beg)
2058 (<= beg (viper-replace-end)))
2059 (and (= length 0) (eq this-command 'dabbrev-expand)))
2060 (setq chars-to-delete
2061 (max (- end-col beg-col) (- real-end beg) 0))
2062 (setq chars-to-delete 0))
2064 ;; if beg = last change position, it means that we are within the
2065 ;; same command that does multiple changes. Moreover, it means
2066 ;; that we have two subsequent changes (insert/delete) that
2067 ;; complement each other.
2068 (if (= beg (marker-position viper-last-posn-in-replace-region))
2069 (setq viper-replace-chars-to-delete
2070 (- (+ chars-to-delete viper-replace-chars-to-delete)
2071 viper-replace-chars-deleted))
2072 (setq viper-replace-chars-to-delete chars-to-delete))
2074 (viper-move-marker-locally
2075 'viper-last-posn-in-replace-region
2076 (max (if (> end (viper-replace-end)) (viper-replace-start) end)
2077 (or (marker-position viper-last-posn-in-replace-region)
2078 (viper-replace-start))
2081 (setq viper-replace-chars-to-delete
2083 (min viper-replace-chars-to-delete
2084 (- (viper-replace-end) viper-last-posn-in-replace-region)
2085 (- (viper-line-pos 'end)
2086 viper-last-posn-in-replace-region)
2091 ;; Delete stuff between posn and the end of viper-replace-overlay-marker, if
2092 ;; posn is within the overlay.
2093 (defun viper-finish-change (posn)
2095 'viper-after-change-functions 'viper-replace-mode-spy-after)
2097 'viper-before-change-functions 'viper-replace-mode-spy-before)
2099 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel)
2101 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel)
2102 (viper-restore-cursor-color-after-replace)
2103 (setq viper-sitting-in-replace nil) ; just in case we'll need to know it
2106 viper-replace-overlay
2107 (>= posn (viper-replace-start))
2108 (< posn (viper-replace-end)))
2109 (delete-region posn (viper-replace-end)))
2112 (if (eq viper-current-state 'replace-state)
2113 (viper-downgrade-to-insert))
2114 ;; replace mode ended => nullify viper-last-posn-in-replace-region
2115 (viper-move-marker-locally 'viper-last-posn-in-replace-region nil)
2116 (viper-hide-replace-overlay)
2117 (viper-refresh-mode-line)
2118 (viper-put-string-on-kill-ring viper-last-replace-region)
2121 ;; Make STRING be the first element of the kill ring.
2122 (defun viper-put-string-on-kill-ring (string)
2123 (setq kill-ring (cons string kill-ring))
2124 (if (> (length kill-ring) kill-ring-max)
2125 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2126 (setq kill-ring-yank-pointer kill-ring))
2128 (defun viper-finish-R-mode ()
2130 'viper-post-command-hooks 'viper-R-state-post-command-sentinel)
2132 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel)
2133 (viper-downgrade-to-insert))
2135 (defun viper-start-R-mode ()
2136 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2139 'viper-post-command-hooks 'viper-R-state-post-command-sentinel t)
2141 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t)
2142 ;; guard against a smartie who switched from R-replace to normal replace
2144 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel)
2149 (defun viper-replace-state-exit-cmd ()
2150 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2151 These keys are ESC, RET, and LineFeed"
2153 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2154 (viper-finish-R-mode)
2155 (viper-finish-change viper-last-posn-in-replace-region))
2157 (if (eq this-command 'viper-intercept-ESC-key)
2158 (setq com 'viper-exit-insert-state)
2159 (viper-set-unread-command-events last-input-char)
2160 (setq com (key-binding (read-key-sequence nil))))
2162 (condition-case conds
2163 (command-execute com)
2165 (viper-message-conditions conds)))
2167 (viper-hide-replace-overlay))
2170 (defun viper-replace-state-carriage-return ()
2171 "Carriage return in Viper replace state."
2173 ;; If Emacs start supporting overlay maps, as it currently supports
2174 ;; text-property maps, we could do away with viper-replace-minor-mode and
2175 ;; just have keymap attached to replace overlay. Then the "if part" of this
2176 ;; statement can be deleted.
2177 (if (or (< (point) (viper-replace-start))
2178 (> (point) (viper-replace-end)))
2179 (let (viper-replace-minor-mode com)
2180 (viper-set-unread-command-events last-input-char)
2181 (setq com (key-binding (read-key-sequence nil)))
2182 (condition-case conds
2183 (command-execute com)
2185 (viper-message-conditions conds))))
2186 (if (not viper-allow-multiline-replace-regions)
2187 (viper-replace-state-exit-cmd)
2188 (if (viper-same-line (point) (viper-replace-end))
2189 (viper-replace-state-exit-cmd)
2190 ;; delete the rest of line
2191 (delete-region (point) (viper-line-pos 'end))
2194 (if (eobp) (error "Last line in buffer")))
2195 ;; skip to the next line
2197 (back-to-indentation)
2201 ;; This is the function bound to 'R'---unlimited replace.
2202 ;; Similar to Emacs's own overwrite-mode.
2203 (defun viper-overwrite (arg)
2204 "Begin overwrite mode."
2206 (let ((val (viper-p-val arg))
2207 (com (viper-getcom arg)) (len))
2208 (viper-set-destructive-command (list 'viper-overwrite val ?r nil nil nil))
2211 ;; Viper saves inserted text in viper-last-insertion
2212 (setq len (length viper-last-insertion))
2214 (viper-loop val (viper-yank-last-insertion)))
2215 (setq last-command 'viper-overwrite)
2216 (viper-set-complex-command-for-undo)
2217 (viper-set-replace-overlay (point) (viper-line-pos 'end))
2218 (viper-change-state-to-replace)
2224 (defun viper-line (arg)
2225 (let ((val (car arg))
2227 (viper-move-marker-locally 'viper-com-point (point))
2229 (viper-next-line-carefully (1- val)))
2230 ;; this ensures that dd, cc, D, yy will do the right thing on the last
2231 ;; line of buffer when this line has no \n.
2232 (viper-add-newline-at-eob-if-necessary)
2233 (viper-execute-com 'viper-line val com))
2234 (if (and (eobp) (not (bobp))) (forward-line -1))
2237 (defun viper-yank-line (arg)
2238 "Yank ARG lines (in Vi's sense)."
2240 (let ((val (viper-p-val arg)))
2241 (viper-line (cons val ?Y))))
2246 (defun viper-region (arg)
2247 "Execute command on a region."
2249 (let ((val (viper-P-val arg))
2250 (com (viper-getcom arg)))
2251 (viper-move-marker-locally 'viper-com-point (point))
2252 (exchange-point-and-mark)
2253 (viper-execute-com 'viper-region val com)))
2255 (defun viper-Region (arg)
2256 "Execute command on a Region."
2258 (let ((val (viper-P-val arg))
2259 (com (viper-getCom arg)))
2260 (viper-move-marker-locally 'viper-com-point (point))
2261 (exchange-point-and-mark)
2262 (viper-execute-com 'viper-Region val com)))
2264 (defun viper-replace-char (arg)
2265 "Replace the following ARG chars by the character read."
2267 (if (and (eolp) (bolp)) (error "No character to replace here"))
2268 (let ((val (viper-p-val arg))
2269 (com (viper-getcom arg)))
2270 (viper-replace-char-subr com val)
2271 (if (and (eolp) (not (bolp))) (forward-char 1))
2272 (viper-set-destructive-command
2273 (list 'viper-replace-char val ?r nil viper-d-char nil))
2276 (defun viper-replace-char-subr (com arg)
2277 (let ((take-care-of-iso-accents
2278 (and (boundp 'iso-accents-mode) viper-automatic-iso-accents))
2280 (setq char (if (equal com ?r)
2283 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
2284 ;; get European characters
2286 (iso-accents-mode 1)
2287 (viper-set-unread-command-events char)
2288 (setq char (aref (read-key-sequence nil) 0))
2289 (iso-accents-mode -1)))
2291 (setq viper-d-char char)
2292 (viper-loop (if (> arg 0) arg (- arg))
2293 (if (eq char ?\C-m) (insert "\n") (insert char)))
2294 (backward-char arg)))
2297 ;; basic cursor movement. j, k, l, h commands.
2299 (defun viper-forward-char (arg)
2300 "Move point right ARG characters (left if ARG negative).
2301 On reaching end of line, stop and signal error."
2303 (viper-leave-region-active)
2304 (let ((val (viper-p-val arg))
2305 (com (viper-getcom arg)))
2306 (if com (viper-move-marker-locally 'viper-com-point (point)))
2307 (if viper-ex-style-motion
2309 ;; the boundary condition check gets weird here because
2310 ;; forward-char may be the parameter of a delete, and 'dl' works
2311 ;; just like 'x' for the last char on a line, so we have to allow
2312 ;; the forward motion before the 'viper-execute-com', but, of
2313 ;; course, 'dl' doesn't work on an empty line, so we have to
2314 ;; catch that condition before 'viper-execute-com'
2315 (if (and (eolp) (bolp)) (error "") (forward-char val))
2316 (if com (viper-execute-com 'viper-forward-char val com))
2317 (if (eolp) (progn (backward-char 1) (error ""))))
2319 (if com (viper-execute-com 'viper-forward-char val com)))))
2321 (defun viper-backward-char (arg)
2322 "Move point left ARG characters (right if ARG negative).
2323 On reaching beginning of line, stop and signal error."
2325 (viper-leave-region-active)
2326 (let ((val (viper-p-val arg))
2327 (com (viper-getcom arg)))
2328 (if com (viper-move-marker-locally 'viper-com-point (point)))
2329 (if viper-ex-style-motion
2331 (if (bolp) (error "") (backward-char val))
2332 (if com (viper-execute-com 'viper-backward-char val com)))
2334 (if com (viper-execute-com 'viper-backward-char val com)))))
2336 ;; Like forward-char, but doesn't move at end of buffer.
2337 (defun viper-forward-char-carefully (&optional arg)
2338 (setq arg (or arg 1))
2339 (if (>= (point-max) (+ (point) arg))
2341 (goto-char (point-max))))
2343 ;; Like backward-char, but doesn't move at end of buffer.
2344 (defun viper-backward-char-carefully (&optional arg)
2345 (setq arg (or arg 1))
2346 (if (<= (point-min) (- (point) arg))
2348 (goto-char (point-min))))
2350 (defun viper-next-line-carefully (arg)
2359 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators for
2360 ;; word movement. When executed with a destructive command, \n is usually left
2361 ;; untouched for the last word. Viper uses syntax table to determine what is a
2362 ;; word and what is a separator. However, \n is always a separator. Also, if
2363 ;; viper-syntax-preference is 'vi, then `_' is part of the word.
2366 (defun viper-skip-separators (forward)
2369 (viper-skip-all-separators-forward 'within-line)
2370 (if (looking-at "\n")
2373 (viper-skip-all-separators-forward 'within-line))))
2374 (viper-skip-all-separators-backward 'within-line)
2376 (if (looking-at "\n")
2377 (viper-skip-all-separators-backward 'within-line)
2380 (defun viper-forward-word-kernel (val)
2382 (cond ((viper-looking-at-alpha)
2383 (viper-skip-alpha-forward "_")
2384 (viper-skip-separators t))
2385 ((viper-looking-at-separator)
2386 (viper-skip-separators t))
2387 ((not (viper-looking-at-alphasep))
2388 (viper-skip-nonalphasep-forward)
2389 (viper-skip-separators t)))
2390 (setq val (1- val))))
2392 ;; first search backward for pat. Then skip chars backwards using aux-pat
2393 (defun viper-fwd-skip (pat aux-pat lim)
2394 (if (and (save-excursion
2395 (re-search-backward pat lim t))
2396 (= (point) (match-end 0)))
2397 (goto-char (match-beginning 0)))
2398 (skip-chars-backward aux-pat lim)
2400 (viper-forward-char-carefully))
2404 (defun viper-forward-word (arg)
2407 (viper-leave-region-active)
2408 (let ((val (viper-p-val arg))
2409 (com (viper-getcom arg)))
2410 (if com (viper-move-marker-locally 'viper-com-point (point)))
2411 (viper-forward-word-kernel val)
2413 (cond ((memq com (list ?c (- ?c)))
2414 (viper-fwd-skip "\n[ \t]*" " \t" viper-com-point))
2415 ;; Yank words including the whitespace, but not newline
2416 ((memq com (list ?y (- ?y)))
2417 (viper-fwd-skip "\n[ \t]*" "" viper-com-point))
2418 ((viper-dotable-command-p com)
2419 (viper-fwd-skip "\n[ \t]*" "" viper-com-point)))
2420 (viper-execute-com 'viper-forward-word val com)))))
2423 (defun viper-forward-Word (arg)
2424 "Forward word delimited by white characters."
2426 (viper-leave-region-active)
2427 (let ((val (viper-p-val arg))
2428 (com (viper-getcom arg)))
2429 (if com (viper-move-marker-locally 'viper-com-point (point)))
2432 (viper-skip-nonseparators 'forward)
2433 (viper-skip-separators t)))
2435 (cond ((memq com (list ?c (- ?c)))
2436 (viper-fwd-skip "\n[ \t]*" " \t" viper-com-point))
2437 ;; Yank words including the whitespace, but not newline
2438 ((memq com (list ?y (- ?y)))
2439 (viper-fwd-skip "\n[ \t]*" "" viper-com-point))
2440 ((viper-dotable-command-p com)
2441 (viper-fwd-skip "\n[ \t]*" "" viper-com-point)))
2442 (viper-execute-com 'viper-forward-Word val com)))))
2445 ;; this is a bit different from Vi, but Vi's end of word
2446 ;; makes no sense whatsoever
2447 (defun viper-end-of-word-kernel ()
2448 (if (viper-end-of-word-p) (forward-char))
2449 (if (viper-looking-at-separator)
2450 (viper-skip-all-separators-forward))
2452 (cond ((viper-looking-at-alpha) (viper-skip-alpha-forward "_"))
2453 ((not (viper-looking-at-alphasep)) (viper-skip-nonalphasep-forward)))
2454 (viper-backward-char-carefully))
2456 (defun viper-end-of-word-p ()
2459 (cond ((viper-looking-at-alpha)
2461 (not (viper-looking-at-alpha)))
2462 ((not (viper-looking-at-alphasep))
2464 (viper-looking-at-alphasep))))))
2467 (defun viper-end-of-word (arg &optional careful)
2468 "Move point to end of current word."
2470 (viper-leave-region-active)
2471 (let ((val (viper-p-val arg))
2472 (com (viper-getcom arg)))
2473 (if com (viper-move-marker-locally 'viper-com-point (point)))
2474 (viper-loop val (viper-end-of-word-kernel))
2478 (viper-execute-com 'viper-end-of-word val com)))))
2480 (defun viper-end-of-Word (arg)
2481 "Forward to end of word delimited by white character."
2483 (viper-leave-region-active)
2484 (let ((val (viper-p-val arg))
2485 (com (viper-getcom arg)))
2486 (if com (viper-move-marker-locally 'viper-com-point (point)))
2489 (viper-end-of-word-kernel)
2490 (viper-skip-nonseparators 'forward)
2495 (viper-execute-com 'viper-end-of-Word val com)))))
2497 (defun viper-backward-word-kernel (val)
2500 (cond ((viper-looking-at-alpha)
2501 (viper-skip-alpha-backward "_"))
2502 ((viper-looking-at-separator)
2504 (viper-skip-separators nil)
2506 (cond ((viper-looking-at-alpha)
2507 (viper-skip-alpha-backward "_"))
2508 ((not (viper-looking-at-alphasep))
2509 (viper-skip-nonalphasep-backward))
2510 (t (forward-char))))
2511 ((not (viper-looking-at-alphasep))
2512 (viper-skip-nonalphasep-backward)))
2513 (setq val (1- val))))
2515 (defun viper-backward-word (arg)
2518 (viper-leave-region-active)
2519 (let ((val (viper-p-val arg))
2520 (com (viper-getcom arg)))
2523 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2525 (viper-move-marker-locally 'viper-com-point (point))
2526 (if i (forward-char))))
2527 (viper-backward-word-kernel val)
2528 (if com (viper-execute-com 'viper-backward-word val com))))
2530 (defun viper-backward-Word (arg)
2531 "Backward word delimited by white character."
2533 (viper-leave-region-active)
2534 (let ((val (viper-p-val arg))
2535 (com (viper-getcom arg)))
2538 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2540 (viper-move-marker-locally 'viper-com-point (point))
2541 (if i (forward-char))))
2544 (viper-skip-separators nil)
2545 (viper-skip-nonseparators 'backward)))
2546 (if com (viper-execute-com 'viper-backward-Word val com))))
2552 (defun viper-beginning-of-line (arg)
2553 "Go to beginning of line."
2555 (viper-leave-region-active)
2556 (let ((val (viper-p-val arg))
2557 (com (viper-getcom arg)))
2558 (if com (viper-move-marker-locally 'viper-com-point (point)))
2559 (beginning-of-line val)
2560 (if com (viper-execute-com 'viper-beginning-of-line val com))))
2562 (defun viper-bol-and-skip-white (arg)
2563 "Beginning of line at first non-white character."
2565 (viper-leave-region-active)
2566 (let ((val (viper-p-val arg))
2567 (com (viper-getcom arg)))
2568 (if com (viper-move-marker-locally 'viper-com-point (point)))
2569 (forward-to-indentation (1- val))
2570 (if com (viper-execute-com 'viper-bol-and-skip-white val com))))
2572 (defun viper-goto-eol (arg)
2573 "Go to end of line."
2575 (viper-leave-region-active)
2576 (let ((val (viper-p-val arg))
2577 (com (viper-getcom arg)))
2578 (if com (viper-move-marker-locally 'viper-com-point (point)))
2580 (if com (viper-execute-com 'viper-goto-eol val com))
2581 (if viper-ex-style-motion
2582 (if (and (eolp) (not (bolp))
2583 ;; a fix for viper-change-to-eol
2584 (not (equal viper-current-state 'insert-state)))
2589 (defun viper-goto-col (arg)
2590 "Go to ARG's column."
2592 (viper-leave-region-active)
2593 (let ((val (viper-p-val arg))
2594 (com (viper-getcom arg))
2596 (setq line-len (- (viper-line-pos 'end) (viper-line-pos 'start)))
2597 (if com (viper-move-marker-locally 'viper-com-point (point)))
2599 (forward-char (1- (min line-len val)))
2600 (while (> (current-column) (1- val))
2602 (if com (viper-execute-com 'viper-goto-col val com))
2605 (if (> val (current-column)) (error "")))
2609 (defun viper-next-line (arg)
2612 (viper-leave-region-active)
2613 (let ((val (viper-p-val arg))
2614 (com (viper-getCom arg)))
2615 (if com (viper-move-marker-locally 'viper-com-point (point)))
2617 (if viper-ex-style-motion
2618 (if (and (eolp) (not (bolp))) (backward-char 1)))
2619 (setq this-command 'next-line)
2620 (if com (viper-execute-com 'viper-next-line val com))))
2622 (defun viper-next-line-at-bol (arg)
2623 "Next line at beginning of line."
2625 (viper-leave-region-active)
2628 (if (eobp) (error "Last line in buffer")))
2629 (let ((val (viper-p-val arg))
2630 (com (viper-getCom arg)))
2631 (if com (viper-move-marker-locally 'viper-com-point (point)))
2633 (back-to-indentation)
2634 (if com (viper-execute-com 'viper-next-line-at-bol val com))))
2636 (defun viper-previous-line (arg)
2637 "Go to previous line."
2639 (viper-leave-region-active)
2640 (let ((val (viper-p-val arg))
2641 (com (viper-getCom arg)))
2642 (if com (viper-move-marker-locally 'viper-com-point (point)))
2644 (if viper-ex-style-motion
2645 (if (and (eolp) (not (bolp))) (backward-char 1)))
2646 (setq this-command 'previous-line)
2647 (if com (viper-execute-com 'viper-previous-line val com))))
2650 (defun viper-previous-line-at-bol (arg)
2651 "Previous line at beginning of line."
2653 (viper-leave-region-active)
2656 (if (bobp) (error "First line in buffer")))
2657 (let ((val (viper-p-val arg))
2658 (com (viper-getCom arg)))
2659 (if com (viper-move-marker-locally 'viper-com-point (point)))
2660 (forward-line (- val))
2661 (back-to-indentation)
2662 (if com (viper-execute-com 'viper-previous-line val com))))
2664 (defun viper-change-to-eol (arg)
2665 "Change to end of line."
2667 (viper-goto-eol (cons arg ?c)))
2669 (defun viper-kill-line (arg)
2672 (viper-goto-eol (cons arg ?d)))
2674 (defun viper-erase-line (arg)
2677 (viper-beginning-of-line (cons arg ?d)))
2682 (defun viper-goto-line (arg)
2683 "Go to ARG's line. Without ARG go to end of buffer."
2685 (let ((val (viper-P-val arg))
2686 (com (viper-getCom arg)))
2687 (viper-move-marker-locally 'viper-com-point (point))
2688 (viper-deactivate-mark)
2691 (goto-char (point-max))
2692 (goto-char (point-min))
2693 (forward-line (1- val)))
2695 ;; positioning is done twice: before and after command execution
2696 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2697 (back-to-indentation)
2699 (if com (viper-execute-com 'viper-goto-line val com))
2701 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2702 (back-to-indentation)
2705 ;; Find ARG's occurrence of CHAR on the current line.
2706 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
2707 ;; adjust point after search.
2708 (defun viper-find-char (arg char forward offset)
2709 (or (char-or-string-p char) (error ""))
2710 (let ((arg (if forward arg (- arg)))
2711 (cmd (if (eq viper-intermediate-command 'viper-repeat)
2713 (viper-array-to-string (this-command-keys))))
2719 ;; forward search begins here
2720 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
2721 ;; forward search ends here
2722 (progn (end-of-line) (point)))
2724 ;; backward search begins from here
2726 (error "Command `%s': At beginning of line" cmd) (point))
2727 ;; backward search ends here
2728 (progn (beginning-of-line) (point))))
2729 ;; if arg > 0, point is forwarded before search.
2730 (if (> arg 0) (goto-char (1+ (point-min)))
2731 (goto-char (point-max)))
2732 (if (let ((case-fold-search nil))
2733 (search-forward (char-to-string char) nil 0 arg))
2734 (setq point (point))
2735 (error "Command `%s': `%c' not found" cmd char))))
2736 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
2738 (defun viper-find-char-forward (arg)
2739 "Find char on the line.
2740 If called interactively read the char to find from the terminal, and if
2741 called from viper-repeat, the char last used is used. This behaviour is
2742 controlled by the sign of prefix numeric value."
2744 (let ((val (viper-p-val arg))
2745 (com (viper-getcom arg))
2746 (cmd-representation (nth 5 viper-d-com)))
2748 ;; this means that the function was called interactively
2749 (setq viper-f-char (read-char)
2752 ;; viper-repeat --- set viper-F-char from command-keys
2753 (setq viper-F-char (if (stringp cmd-representation)
2754 (viper-seq-last-elt cmd-representation)
2756 viper-f-char viper-F-char)
2758 (if com (viper-move-marker-locally 'viper-com-point (point)))
2760 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t nil)
2764 (setq viper-F-char viper-f-char) ; set new viper-F-char
2766 (viper-execute-com 'viper-find-char-forward val com)))))
2768 (defun viper-goto-char-forward (arg)
2769 "Go up to char ARG forward on line."
2771 (let ((val (viper-p-val arg))
2772 (com (viper-getcom arg))
2773 (cmd-representation (nth 5 viper-d-com)))
2775 ;; this means that the function was called interactively
2776 (setq viper-f-char (read-char)
2779 ;; viper-repeat --- set viper-F-char from command-keys
2780 (setq viper-F-char (if (stringp cmd-representation)
2781 (viper-seq-last-elt cmd-representation)
2783 viper-f-char viper-F-char)
2785 (if com (viper-move-marker-locally 'viper-com-point (point)))
2787 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t t)
2791 (setq viper-F-char viper-f-char) ; set new viper-F-char
2793 (viper-execute-com 'viper-goto-char-forward val com)))))
2795 (defun viper-find-char-backward (arg)
2796 "Find char ARG on line backward."
2798 (let ((val (viper-p-val arg))
2799 (com (viper-getcom arg))
2800 (cmd-representation (nth 5 viper-d-com)))
2802 ;; this means that the function was called interactively
2803 (setq viper-f-char (read-char)
2806 ;; viper-repeat --- set viper-F-char from command-keys
2807 (setq viper-F-char (if (stringp cmd-representation)
2808 (viper-seq-last-elt cmd-representation)
2810 viper-f-char viper-F-char)
2812 (if com (viper-move-marker-locally 'viper-com-point (point)))
2814 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil nil)
2818 (setq viper-F-char viper-f-char) ; set new viper-F-char
2819 (viper-execute-com 'viper-find-char-backward val com)))))
2821 (defun viper-goto-char-backward (arg)
2822 "Go up to char ARG backward on line."
2824 (let ((val (viper-p-val arg))
2825 (com (viper-getcom arg))
2826 (cmd-representation (nth 5 viper-d-com)))
2828 ;; this means that the function was called interactively
2829 (setq viper-f-char (read-char)
2832 ;; viper-repeat --- set viper-F-char from command-keys
2833 (setq viper-F-char (if (stringp cmd-representation)
2834 (viper-seq-last-elt cmd-representation)
2836 viper-f-char viper-F-char)
2838 (if com (viper-move-marker-locally 'viper-com-point (point)))
2840 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil t)
2844 (setq viper-F-char viper-f-char) ; set new viper-F-char
2845 (viper-execute-com 'viper-goto-char-backward val com)))))
2847 (defun viper-repeat-find (arg)
2848 "Repeat previous find command."
2850 (let ((val (viper-p-val arg))
2851 (com (viper-getcom arg)))
2852 (viper-deactivate-mark)
2853 (if com (viper-move-marker-locally 'viper-com-point (point)))
2854 (viper-find-char val viper-f-char viper-f-forward viper-f-offset)
2857 (if viper-f-forward (forward-char))
2858 (viper-execute-com 'viper-repeat-find val com)))))
2860 (defun viper-repeat-find-opposite (arg)
2861 "Repeat previous find command in the opposite direction."
2863 (let ((val (viper-p-val arg))
2864 (com (viper-getcom arg)))
2865 (viper-deactivate-mark)
2866 (if com (viper-move-marker-locally 'viper-com-point (point)))
2867 (viper-find-char val viper-f-char (not viper-f-forward) viper-f-offset)
2870 (if viper-f-forward (forward-char))
2871 (viper-execute-com 'viper-repeat-find-opposite val com)))))
2874 ;; window scrolling etc.
2876 (defun viper-window-top (arg)
2877 "Go to home window line."
2879 (let ((val (viper-p-val arg))
2880 (com (viper-getCom arg)))
2881 (if com (viper-move-marker-locally 'viper-com-point (point)))
2883 (move-to-window-line (1- val))
2885 ;; positioning is done twice: before and after command execution
2886 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2887 (back-to-indentation)
2889 (if com (viper-execute-com 'viper-window-top val com))
2891 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2892 (back-to-indentation)
2895 (defun viper-window-middle (arg)
2896 "Go to middle window line."
2898 (let ((val (viper-p-val arg))
2899 (com (viper-getCom arg))
2901 (if com (viper-move-marker-locally 'viper-com-point (point)))
2903 (if (not (pos-visible-in-window-p (point-max)))
2904 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
2905 (setq lines (count-lines (window-start) (point-max)))
2906 (move-to-window-line (+ (/ lines 2) (1- val))))
2908 ;; positioning is done twice: before and after command execution
2909 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2910 (back-to-indentation)
2912 (if com (viper-execute-com 'viper-window-middle val com))
2914 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2915 (back-to-indentation)
2918 (defun viper-window-bottom (arg)
2919 "Go to last window line."
2921 (let ((val (viper-p-val arg))
2922 (com (viper-getCom arg)))
2923 (if com (viper-move-marker-locally 'viper-com-point (point)))
2925 (move-to-window-line (- val))
2927 ;; positioning is done twice: before and after command execution
2928 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2929 (back-to-indentation)
2931 (if com (viper-execute-com 'viper-window-bottom val com))
2933 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2934 (back-to-indentation)
2937 (defun viper-line-to-top (arg)
2938 "Put current line on the home line."
2940 (recenter (1- arg)))
2942 (defun viper-line-to-middle (arg)
2943 "Put current line on the middle line."
2945 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
2947 (defun viper-line-to-bottom (arg)
2948 "Put current line on the last line."
2950 (recenter (- (window-height) (1+ arg))))
2952 ;; If point is within viper-search-scroll-threshold of window top or bottom,
2953 ;; scroll up or down 1/7 of window height, depending on whether we are at the
2954 ;; bottom or at the top of the window. This function is called by viper-search
2955 ;; (which is called from viper-search-forward/backward/next). If the value of
2956 ;; viper-search-scroll-threshold is negative - don't scroll.
2957 (defun viper-adjust-window ()
2958 (let ((win-height (if viper-emacs-p
2959 (1- (window-height)) ; adjust for modeline
2960 (window-displayed-height)))
2962 at-top-p at-bottom-p
2963 min-scroll direction)
2965 (move-to-window-line 0) ; top
2967 (<= (count-lines pt (point))
2968 viper-search-scroll-threshold))
2969 (move-to-window-line -1) ; bottom
2971 (<= (count-lines pt (point)) viper-search-scroll-threshold))
2973 (cond (at-top-p (setq min-scroll (1- viper-search-scroll-threshold)
2975 (at-bottom-p (setq min-scroll (1+ viper-search-scroll-threshold)
2979 (* (max min-scroll (/ win-height 7)) direction)))
2984 ;; must correct this to only match ( to ) etc. On the other hand
2985 ;; it is good that paren match gets confused, because that way you
2986 ;; catch _all_ imbalances.
2988 (defun viper-paren-match (arg)
2989 "Go to the matching parenthesis."
2991 (viper-leave-region-active)
2992 (let ((com (viper-getcom arg))
2993 (parse-sexp-ignore-comments viper-parse-sexp-ignore-comments)
2996 (if (or (> arg 99) (< arg 1))
2997 (error "Prefix must be between 1 and 99")
2999 (if (> (point-max) 80000)
3000 (* (/ (point-max) 100) arg)
3001 (/ (* (point-max) arg) 100)))
3002 (back-to-indentation))
3003 (let (beg-lim end-lim)
3004 (if (and (eolp) (not (bolp))) (forward-char -1))
3005 (if (not (looking-at "[][(){}]"))
3006 (setq anchor-point (point)))
3009 (setq beg-lim (point))
3011 (setq end-lim (point)))
3012 (cond ((re-search-forward "[][(){}]" end-lim t)
3014 ((re-search-backward "[][(){}]" beg-lim t))
3016 (error "No matching character on line"))))
3017 (cond ((looking-at "[\(\[{]")
3018 (if com (viper-move-marker-locally 'viper-com-point (point)))
3021 (viper-execute-com 'viper-paren-match nil com)
3026 (viper-move-marker-locally 'viper-com-point anchor-point)
3028 (viper-execute-com 'viper-paren-match nil com)
3030 ((looking-at "[])}]")
3032 (if com (viper-move-marker-locally 'viper-com-point (point)))
3034 (if com (viper-execute-com 'viper-paren-match nil com)))
3037 (defun viper-toggle-parse-sexp-ignore-comments ()
3039 (setq viper-parse-sexp-ignore-comments
3040 (not viper-parse-sexp-ignore-comments))
3042 "From now on, `%%' will %signore parentheses inside comment fields"
3043 (if viper-parse-sexp-ignore-comments "" "NOT "))))
3046 ;; sentence ,paragraph and heading
3048 (defun viper-forward-sentence (arg)
3052 (let ((val (viper-p-val arg))
3053 (com (viper-getcom arg)))
3054 (if com (viper-move-marker-locally 'viper-com-point (point)))
3055 (forward-sentence val)
3056 (if com (viper-execute-com 'viper-forward-sentence nil com))))
3058 (defun viper-backward-sentence (arg)
3059 "Backward sentence."
3062 (let ((val (viper-p-val arg))
3063 (com (viper-getcom arg)))
3064 (if com (viper-move-marker-locally 'viper-com-point (point)))
3065 (backward-sentence val)
3066 (if com (viper-execute-com 'viper-backward-sentence nil com))))
3068 (defun viper-forward-paragraph (arg)
3069 "Forward paragraph."
3072 (let ((val (viper-p-val arg))
3073 (com (viper-getCom arg)))
3074 (if com (viper-move-marker-locally 'viper-com-point (point)))
3075 (forward-paragraph val)
3079 (viper-execute-com 'viper-forward-paragraph nil com)))))
3081 (defun viper-backward-paragraph (arg)
3082 "Backward paragraph."
3085 (let ((val (viper-p-val arg))
3086 (com (viper-getCom arg)))
3087 (if com (viper-move-marker-locally 'viper-com-point (point)))
3088 (backward-paragraph val)
3092 (viper-execute-com 'viper-backward-paragraph nil com)
3093 (backward-char 1)))))
3095 ;; should be mode-specific etc.
3097 (defun viper-prev-heading (arg)
3099 (let ((val (viper-p-val arg))
3100 (com (viper-getCom arg)))
3101 (if com (viper-move-marker-locally 'viper-com-point (point)))
3102 (re-search-backward viper-heading-start nil t val)
3103 (goto-char (match-beginning 0))
3104 (if com (viper-execute-com 'viper-prev-heading nil com))))
3106 (defun viper-heading-end (arg)
3108 (let ((val (viper-p-val arg))
3109 (com (viper-getCom arg)))
3110 (if com (viper-move-marker-locally 'viper-com-point (point)))
3111 (re-search-forward viper-heading-end nil t val)
3112 (goto-char (match-beginning 0))
3113 (if com (viper-execute-com 'viper-heading-end nil com))))
3115 (defun viper-next-heading (arg)
3117 (let ((val (viper-p-val arg))
3118 (com (viper-getCom arg)))
3119 (if com (viper-move-marker-locally 'viper-com-point (point)))
3121 (re-search-forward viper-heading-start nil t val)
3122 (goto-char (match-beginning 0))
3123 (if com (viper-execute-com 'viper-next-heading nil com))))
3128 (defun viper-scroll-screen (arg)
3129 "Scroll to next screen."
3135 (setq arg (1- arg)))
3138 (setq arg (1+ arg))))
3142 (message "End of buffer")
3143 (goto-char (point-max)))
3144 (message "Beginning of buffer")
3145 (goto-char (point-min))))
3148 (defun viper-scroll-screen-back (arg)
3149 "Scroll to previous screen."
3151 (viper-scroll-screen (- arg)))
3153 (defun viper-scroll-down (arg)
3154 "Pull down half screen."
3158 (scroll-down (/ (window-height) 2))
3161 (message "Beginning of buffer")
3162 (goto-char (point-min)))))
3164 (defun viper-scroll-down-one (arg)
3165 "Scroll up one line."
3169 (defun viper-scroll-up (arg)
3170 "Pull up half screen."
3174 (scroll-up (/ (window-height) 2))
3177 (message "End of buffer")
3178 (goto-char (point-max)))))
3180 (defun viper-scroll-up-one (arg)
3181 "Scroll down one line."
3188 (defun viper-if-string (prompt)
3189 (if (memq viper-intermediate-command
3190 '(viper-command-argument viper-digit-argument viper-repeat))
3191 (setq viper-this-command-keys (this-command-keys)))
3192 (let ((s (viper-read-string-with-history
3195 'viper-search-history
3196 (car viper-search-history))))
3197 (if (not (string= s ""))
3198 (setq viper-s-string s))))
3201 (defun viper-toggle-search-style (arg)
3202 "Toggle the value of viper-case-fold-search/viper-re-search.
3203 Without prefix argument, will ask which search style to toggle. With prefix
3204 arg 1,toggles viper-case-fold-search; with arg 2 toggles viper-re-search.
3206 Although this function is bound to \\[viper-toggle-search-style], the most
3207 convenient way to use it is to bind `//' to the macro
3208 `1 M-x viper-toggle-search-style' and `///' to
3209 `2 M-x viper-toggle-search-style'. In this way, hitting `//' quickly will
3210 toggle case-fold-search and hitting `/' three times witth toggle regexp
3211 search. Macros are more convenient in this case because they don't affect
3212 the Emacs binding of `/'."
3215 (cond ((or (eq arg 1)
3217 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3218 (if viper-case-fold-search
3219 "case-insensitive" "case-sensitive")
3220 (if viper-case-fold-search
3222 "case-insensitive")))))
3223 (setq viper-case-fold-search (null viper-case-fold-search))
3224 (if viper-case-fold-search
3225 (setq msg "Search becomes case-insensitive")
3226 (setq msg "Search becomes case-sensitive")))
3229 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3231 "regexp-search" "vanilla-search")
3234 "regexp-search")))))
3235 (setq viper-re-search (null viper-re-search))
3237 (setq msg "Search becomes regexp-style")
3238 (setq msg "Search becomes vanilla-style")))
3240 (setq msg "Search style remains unchanged")))
3243 (defun viper-set-searchstyle-toggling-macros (unset)
3244 "Set the macros for toggling the search style in Viper's vi-state.
3245 The macro that toggles case sensitivity is bound to `//', and the one that
3246 toggles regexp search is bound to `///'.
3247 With a prefix argument, this function unsets the macros. "
3252 ;; toggle case sensitivity in search
3253 (viper-record-kbd-macro
3255 [1 (meta x) v i p e r - t o g g l e - s e a r c h - s t y l e return]
3257 ;; toggle regexp/vanila search
3258 (viper-record-kbd-macro
3260 [2 (meta x) v i p e r - t o g g l e - s e a r c h - s t y l e return]
3264 "// and /// now toggle case-sensitivity and regexp search")))
3265 (viper-unrecord-kbd-macro "//" 'vi-state)
3267 (viper-unrecord-kbd-macro "///" 'vi-state))))
3270 (defun viper-set-parsing-style-toggling-macro (unset)
3271 "Set `%%%' to be a macro that toggles whether comment fields should be parsed for matching parentheses.
3272 This is used in conjunction with the `%' command.
3274 With a prefix argument, unsets the macro."
3279 ;; Make %%% toggle parsing comments for matching parentheses
3280 (viper-record-kbd-macro
3282 [(meta x) v i p e r - t o g g l e - p a r s e - s e x p - i g n o r e - c o m m e n t s return]
3286 "%%%%%% now toggles whether comments should be parsed for matching parentheses")))
3287 (viper-unrecord-kbd-macro "%%%" 'vi-state))))
3290 (defun viper-set-emacs-state-searchstyle-macros (unset &optional arg-majormode)
3291 "Set the macros for toggling the search style in Viper's emacs-state.
3292 The macro that toggles case sensitivity is bound to `//', and the one that
3293 toggles regexp search is bound to `///'.
3294 With a prefix argument, this function unsets the macros.
3295 If the optional prefix argument is non-nil and specifies a valid major mode,
3296 this sets the macros only in the macros in that major mode. Otherwise,
3297 the macros are set in the current major mode.
3298 \(When unsetting the macros, the second argument has no effect.\)"
3303 ;; toggle case sensitivity in search
3304 (viper-record-kbd-macro
3306 [1 (meta x) v i p e r - t o g g l e - s e a r c h - s t y l e return]
3307 (or arg-majormode major-mode))
3308 ;; toggle regexp/vanila search
3309 (viper-record-kbd-macro
3311 [2 (meta x) v i p e r - t o g g l e - s e a r c h - s t y l e return]
3312 (or arg-majormode major-mode))
3315 "// and /// now toggle case-sensitivity and regexp search.")))
3316 (viper-unrecord-kbd-macro "//" 'emacs-state)
3318 (viper-unrecord-kbd-macro "///" 'emacs-state))))
3321 (defun viper-search-forward (arg)
3322 "Search a string forward.
3323 ARG is used to find the ARG's occurrence of the string.
3324 Null string will repeat previous search."
3326 (let ((val (viper-P-val arg))
3327 (com (viper-getcom arg))
3328 (old-str viper-s-string))
3329 (setq viper-s-forward t)
3330 (viper-if-string "/")
3331 ;; this is not used at present, but may be used later
3332 (if (or (not (equal old-str viper-s-string))
3333 (not (markerp viper-local-search-start-marker))
3334 (not (marker-buffer viper-local-search-start-marker)))
3335 (setq viper-local-search-start-marker (point-marker)))
3336 (viper-search viper-s-string t val)
3339 (viper-move-marker-locally 'viper-com-point (mark t))
3340 (viper-execute-com 'viper-search-next val com)))))
3342 (defun viper-search-backward (arg)
3343 "Search a string backward.
3344 ARG is used to find the ARG's occurrence of the string.
3345 Null string will repeat previous search."
3347 (let ((val (viper-P-val arg))
3348 (com (viper-getcom arg))
3349 (old-str viper-s-string))
3350 (setq viper-s-forward nil)
3351 (viper-if-string "?")
3352 ;; this is not used at present, but may be used later
3353 (if (or (not (equal old-str viper-s-string))
3354 (not (markerp viper-local-search-start-marker))
3355 (not (marker-buffer viper-local-search-start-marker)))
3356 (setq viper-local-search-start-marker (point-marker)))
3357 (viper-search viper-s-string nil val)
3360 (viper-move-marker-locally 'viper-com-point (mark t))
3361 (viper-execute-com 'viper-search-next val com)))))
3364 ;; Search for COUNT's occurrence of STRING.
3365 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3366 ;; INIT-POINT is the position where search is to start.
3368 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3369 (defun viper-search (string forward arg
3370 &optional no-offset init-point fail-if-not-found)
3371 (if (not (equal string ""))
3372 (let ((val (viper-p-val arg))
3373 (com (viper-getcom arg))
3374 (offset (not no-offset))
3375 (case-fold-search viper-case-fold-search)
3376 (start-point (or init-point (point))))
3377 (viper-deactivate-mark)
3381 (if offset (viper-forward-char-carefully))
3384 (re-search-forward string nil nil val)
3385 (re-search-backward string))
3386 (search-forward string nil nil val)
3387 (search-backward string))
3388 (if (not (equal start-point (point)))
3389 (push-mark start-point t)))
3391 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3393 (message "Search wrapped around BOTTOM of buffer")
3394 (goto-char (point-min))
3395 (viper-search string forward (cons 1 com) t start-point 'fail)
3396 ;; don't wait in macros
3397 (or executing-kbd-macro
3398 (memq viper-intermediate-command
3400 viper-digit-argument
3401 viper-command-argument))
3403 ;; delete the wrap-around message
3406 (goto-char start-point)
3407 (error "`%s': %s not found"
3409 (if viper-re-search "Pattern" "String"))
3415 (re-search-backward string nil nil val)
3416 (search-backward string nil nil val))
3417 (if (not (equal start-point (point)))
3418 (push-mark start-point t)))
3420 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3422 (message "Search wrapped around TOP of buffer")
3423 (goto-char (point-max))
3424 (viper-search string forward (cons 1 com) t start-point 'fail)
3425 ;; don't wait in macros
3426 (or executing-kbd-macro
3427 (memq viper-intermediate-command
3429 viper-digit-argument
3430 viper-command-argument))
3432 ;; delete the wrap-around message
3435 (goto-char start-point)
3436 (error "`%s': %s not found"
3438 (if viper-re-search "Pattern" "String"))
3440 ;; pull up or down if at top/bottom of window
3441 (viper-adjust-window)
3442 ;; highlight the result of search
3443 ;; don't wait and don't highlight in macros
3444 (or executing-kbd-macro
3445 (memq viper-intermediate-command
3446 '(viper-repeat viper-digit-argument viper-command-argument))
3447 (viper-flash-search-pattern))
3450 (defun viper-search-next (arg)
3451 "Repeat previous search."
3453 (let ((val (viper-p-val arg))
3454 (com (viper-getcom arg)))
3455 (if (null viper-s-string) (error viper-NoPrevSearch))
3456 (viper-search viper-s-string viper-s-forward arg)
3459 (viper-move-marker-locally 'viper-com-point (mark t))
3460 (viper-execute-com 'viper-search-next val com)))))
3462 (defun viper-search-Next (arg)
3463 "Repeat previous search in the reverse direction."
3465 (let ((val (viper-p-val arg))
3466 (com (viper-getcom arg)))
3467 (if (null viper-s-string) (error viper-NoPrevSearch))
3468 (viper-search viper-s-string (not viper-s-forward) arg)
3471 (viper-move-marker-locally 'viper-com-point (mark t))
3472 (viper-execute-com 'viper-search-Next val com)))))
3475 ;; Search contents of buffer defined by one of Viper's motion commands.
3476 ;; Repeatable via `n' and `N'.
3477 (defun viper-buffer-search-enable (&optional c)
3478 (cond (c (setq viper-buffer-search-char c))
3479 ((null viper-buffer-search-char)
3480 (setq viper-buffer-search-char ?g)))
3481 (define-key viper-vi-basic-map
3482 (char-to-string viper-buffer-search-char) 'viper-command-argument)
3483 (aset viper-exec-array viper-buffer-search-char 'viper-exec-buffer-search)
3484 (setq viper-prefix-commands
3485 (cons viper-buffer-search-char viper-prefix-commands)))
3487 ;; This is a Viper wraper for isearch-forward.
3488 (defun viper-isearch-forward (arg)
3489 "Do incremental search forward."
3491 ;; emacs bug workaround
3492 (if (listp arg) (setq arg (car arg)))
3493 (viper-exec-form-in-emacs (list 'isearch-forward arg)))
3495 ;; This is a Viper wraper for isearch-backward."
3496 (defun viper-isearch-backward (arg)
3497 "Do incremental search backward."
3499 ;; emacs bug workaround
3500 (if (listp arg) (setq arg (car arg)))
3501 (viper-exec-form-in-emacs (list 'isearch-backward arg)))
3504 ;; visiting and killing files, buffers
3506 (defun viper-switch-to-buffer ()
3507 "Switch to buffer in the current window."
3512 (format "Switch to buffer in this window \(%s\): "
3513 (buffer-name (other-buffer (current-buffer))))))
3514 (switch-to-buffer buffer)
3517 (defun viper-switch-to-buffer-other-window ()
3518 "Switch to buffer in another window."
3523 (format "Switch to buffer in another window \(%s\): "
3524 (buffer-name (other-buffer (current-buffer))))))
3525 (switch-to-buffer-other-window buffer)
3528 (defun viper-kill-buffer ()
3531 (let (buffer buffer-name)
3534 (format "Kill buffer \(%s\): "
3535 (buffer-name (current-buffer)))))
3537 (if (null buffer-name)
3539 (get-buffer buffer-name)))
3540 (if (null buffer) (error "`%s': No such buffer" buffer-name))
3541 (if (or (not (buffer-modified-p buffer))
3544 "Buffer `%s' is modified, are you sure you want to kill it? "
3546 (kill-buffer buffer)
3547 (error "Buffer not killed"))))
3550 (defcustom viper-smart-suffix-list
3551 '("" "tex" "c" "cc" "C" "el" "java" "html" "htm" "pl" "P" "p")
3552 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
3553 This is useful when you the current directory contains files with the same
3554 prefix and many different suffixes. Usually, only one of the suffixes
3555 represents an editable file. However, file completion will stop at the `.'
3556 The smart suffix feature lets you hit RET in such a case, and Viper will
3557 select the appropriate suffix.
3559 Suffixes are tried in the order given and the first suffix for which a
3560 corresponding file exists is selected. If no file exists for any of the
3561 suffixes, the user is asked to confirm.
3563 To turn this feature off, set this variable to nil."
3567 ;; Try to add suffix to files ending with a `.'
3568 ;; Useful when the user hits RET on a non-completed file name.
3569 (defun viper-file-add-suffix ()
3571 (len (length viper-smart-suffix-list))
3572 (file (buffer-string))
3574 (goto-char (point-max))
3575 (if (and viper-smart-suffix-list (string-match "\\.$" file))
3577 (while (and (not found) (< count len))
3578 (setq suff (nth count viper-smart-suffix-list)
3581 (format "%s%s" (substitute-in-file-name file) suff))
3588 (viper-tmp-insert-at-eob " [Please complete file name]")
3590 (while (not (memq cmd
3591 '(exit-minibuffer viper-exit-minibuffer)))
3593 (key-binding (setq key (read-key-sequence nil))))
3594 (cond ((eq cmd 'self-insert-command)
3596 (insert (events-to-keys key))
3598 ((memq cmd '(exit-minibuffer viper-exit-minibuffer))
3600 (t (command-execute cmd)))
3609 (defsubst viper-yank (text)
3610 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
3612 (setq this-command 'yank))
3614 (defun viper-put-back (arg)
3615 "Put back after point/below line."
3617 (let ((val (viper-p-val arg))
3618 (text (if viper-use-register
3619 (cond ((viper-valid-register viper-use-register '(digit))
3621 (- viper-use-register ?1) 'do-not-rotate))
3622 ((viper-valid-register viper-use-register)
3623 (get-register (downcase viper-use-register)))
3624 (t (error viper-InvalidRegister viper-use-register)))
3627 (if viper-use-register
3628 (let ((reg viper-use-register))
3629 (setq viper-use-register nil)
3630 (error viper-EmptyRegister reg))
3632 (setq viper-use-register nil)
3633 (if (viper-end-with-a-newline-p text)
3639 (beginning-of-line))
3640 (if (not (eolp)) (viper-forward-char-carefully)))
3641 (set-marker (viper-mark-marker) (point) (current-buffer))
3642 (viper-set-destructive-command
3643 (list 'viper-put-back val nil viper-use-register nil nil))
3644 (viper-loop val (viper-yank text)))
3645 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3646 ;; newline; it leaves the cursor at the beginning when the text contains
3648 (if (viper-same-line (point) (mark))
3649 (or (= (point) (mark)) (viper-backward-char-carefully))
3650 (exchange-point-and-mark)
3652 (back-to-indentation)))
3653 (viper-deactivate-mark))
3655 (defun viper-Put-back (arg)
3656 "Put back at point/above line."
3658 (let ((val (viper-p-val arg))
3659 (text (if viper-use-register
3660 (cond ((viper-valid-register viper-use-register '(digit))
3662 (- viper-use-register ?1) 'do-not-rotate))
3663 ((viper-valid-register viper-use-register)
3664 (get-register (downcase viper-use-register)))
3665 (t (error viper-InvalidRegister viper-use-register)))
3668 (if viper-use-register
3669 (let ((reg viper-use-register))
3670 (setq viper-use-register nil)
3671 (error viper-EmptyRegister reg))
3673 (setq viper-use-register nil)
3674 (if (viper-end-with-a-newline-p text) (beginning-of-line))
3675 (viper-set-destructive-command
3676 (list 'viper-Put-back val nil viper-use-register nil nil))
3677 (set-marker (viper-mark-marker) (point) (current-buffer))
3678 (viper-loop val (viper-yank text)))
3679 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3680 ;; newline; it leaves the cursor at the beginning when the text contains
3682 (if (viper-same-line (point) (mark))
3683 (or (= (point) (mark)) (viper-backward-char-carefully))
3684 (exchange-point-and-mark)
3686 (back-to-indentation)))
3687 (viper-deactivate-mark))
3690 ;; Copy region to kill-ring.
3691 ;; If BEG and END do not belong to the same buffer, copy empty region.
3692 (defun viper-copy-region-as-kill (beg end)
3694 (copy-region-as-kill beg end)
3695 (error (copy-region-as-kill beg beg))))
3698 (defun viper-delete-char (arg)
3701 (let ((val (viper-p-val arg)))
3702 (viper-set-destructive-command
3703 (list 'viper-delete-char val nil nil nil nil))
3706 (let ((here (point)))
3708 (if (> val (- (point) here))
3709 (setq val (- (point) here))))))
3710 (if (and (eq val 0) (not viper-ex-style-motion)) (setq val 1))
3711 (if (and viper-ex-style-motion (eolp))
3712 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
3713 (if viper-use-register
3715 (cond ((viper-valid-register viper-use-register '((Letter)))
3716 (viper-append-to-register
3717 (downcase viper-use-register) (point) (- (point) val)))
3718 ((viper-valid-register viper-use-register)
3720 viper-use-register (point) (- (point) val) nil))
3721 (t (error viper-InvalidRegister viper-use-register)))
3722 (setq viper-use-register nil)))
3723 (if viper-ex-style-motion
3726 (if (and (eolp) (not (bolp))) (backward-char 1)))
3728 (delete-backward-char val t)
3729 (delete-char val t)))))
3731 (defun viper-delete-backward-char (arg)
3732 "Delete previous character. On reaching beginning of line, stop and beep."
3734 (let ((val (viper-p-val arg)))
3735 (viper-set-destructive-command
3736 (list 'viper-delete-backward-char val nil nil nil nil))
3739 (let ((here (point)))
3741 (if (> val (- here (point)))
3742 (setq val (- here (point)))))))
3743 (if viper-use-register
3745 (cond ((viper-valid-register viper-use-register '(Letter))
3746 (viper-append-to-register
3747 (downcase viper-use-register) (point) (+ (point) val)))
3748 ((viper-valid-register viper-use-register)
3750 viper-use-register (point) (+ (point) val) nil))
3751 (t (error viper-InvalidRegister viper-use-register)))
3752 (setq viper-use-register nil)))
3754 (delete-backward-char val t))))
3756 (defun viper-del-backward-char-in-insert ()
3757 "Delete 1 char backwards while in insert mode."
3759 (if (and viper-ex-style-editing-in-insert (bolp))
3761 (delete-backward-char 1 t)))
3763 (defun viper-del-backward-char-in-replace ()
3764 "Delete one character in replace mode.
3765 If `viper-delete-backwards-in-replace' is t, then DEL key actually deletes
3766 charecters. If it is nil, then the cursor just moves backwards, similarly
3767 to Vi. The variable `viper-ex-style-editing-in-insert', if t, doesn't let the
3768 cursor move past the beginning of line."
3770 (cond (viper-delete-backwards-in-replace
3772 (delete-backward-char 1 t))
3773 (viper-ex-style-editing-in-insert
3778 (delete-backward-char 1 t))))
3779 (viper-ex-style-editing-in-insert
3784 (backward-char 1))))
3790 (defun viper-join-lines (arg)
3791 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
3793 (let ((val (viper-P-val arg)))
3794 (viper-set-destructive-command
3795 (list 'viper-join-lines val nil nil nil nil))
3796 (viper-loop (if (null val) 1 (1- val))
3802 (delete-region (point) (1- (point)))
3804 ;; fixup-whitespace sometimes does not leave space
3805 ;; between objects, so we insert it as in Vi
3806 (or (looking-at " ")
3814 (defun viper-change (beg end)
3815 (if (markerp beg) (setq beg (marker-position beg)))
3816 (if (markerp end) (setq end (marker-position end)))
3817 ;; beg is sometimes (mark t), which may be nil
3818 (or beg (setq beg end))
3820 (viper-set-complex-command-for-undo)
3821 (if viper-use-register
3823 (copy-to-register viper-use-register beg end nil)
3824 (setq viper-use-register nil)))
3825 (viper-set-replace-overlay beg end)
3826 (setq last-command nil) ; separate repl text from prev kills
3828 (if (= (viper-replace-start) (point-max))
3829 (error "End of buffer"))
3831 (setq viper-last-replace-region
3832 (buffer-substring (viper-replace-start)
3833 (viper-replace-end)))
3835 ;; protect against error while inserting "@" and other disasters
3836 ;; (e.g., read-only buff)
3837 (condition-case conds
3838 (if (or viper-allow-multiline-replace-regions
3839 (viper-same-line (viper-replace-start)
3840 (viper-replace-end)))
3842 ;; tabs cause problems in replace, so untabify
3843 (goto-char (viper-replace-end))
3844 (insert-before-markers "@") ; put placeholder after the TAB
3845 (untabify (viper-replace-start) (point))
3846 ;; del @, don't put on kill ring
3847 (delete-backward-char 1)
3849 (viper-set-replace-overlay-glyphs
3850 viper-replace-region-start-delimiter
3851 viper-replace-region-end-delimiter)
3852 ;; this move takes care of the last posn in the overlay, which
3853 ;; has to be shifted because of insert. We can't simply insert
3854 ;; "$" before-markers because then overlay-start will shift the
3855 ;; beginning of the overlay in case we are replacing a single
3856 ;; character. This fixes the bug with `s' and `cl' commands.
3857 (viper-move-replace-overlay (viper-replace-start) (point))
3858 (goto-char (viper-replace-start))
3859 (viper-change-state-to-replace t))
3860 (kill-region (viper-replace-start)
3861 (viper-replace-end))
3862 (viper-hide-replace-overlay)
3863 (viper-change-state-to-insert))
3864 (error ;; make sure that the overlay doesn't stay.
3865 ;; go back to the original point
3866 (goto-char (viper-replace-start))
3867 (viper-hide-replace-overlay)
3868 (viper-message-conditions conds))))
3871 (defun viper-change-subr (beg end)
3872 ;; beg is sometimes (mark t), which may be nil
3873 (or beg (setq beg end))
3874 (if viper-use-register
3876 (copy-to-register viper-use-register beg end nil)
3877 (setq viper-use-register nil)))
3878 (kill-region beg end)
3879 (setq this-command 'viper-change)
3880 (viper-yank-last-insertion))
3882 (defun viper-toggle-case (arg)
3883 "Toggle character case."
3885 (let ((val (viper-p-val arg)) (c))
3886 (viper-set-destructive-command
3887 (list 'viper-toggle-case val nil nil nil nil))
3889 (setq c (following-char))
3891 (if (eq c (upcase c))
3892 (insert-char (downcase c) 1)
3893 (insert-char (upcase c) 1))
3894 (if (eolp) (backward-char 1))
3895 (setq val (1- val)))))
3900 (defun viper-query-replace ()
3902 If a null string is suplied as the string to be replaced,
3903 the query replace mode will toggle between string replace
3904 and regexp replace."
3907 (setq str (viper-read-string-with-history
3908 (if viper-re-query-replace "Query replace regexp: "
3911 'viper-replace1-history
3912 (car viper-replace1-history) ; default
3914 (if (string= str "")
3916 (setq viper-re-query-replace (not viper-re-query-replace))
3917 (message "Query replace mode changed to %s"
3918 (if viper-re-query-replace "regexp replace"
3920 (if viper-re-query-replace
3921 (query-replace-regexp
3923 (viper-read-string-with-history
3924 (format "Query replace regexp `%s' with: " str)
3926 'viper-replace1-history
3927 (car viper-replace1-history) ; default
3931 (viper-read-string-with-history
3932 (format "Query replace `%s' with: " str)
3934 'viper-replace1-history
3935 (car viper-replace1-history) ; default
3941 (defun viper-mark-beginning-of-buffer ()
3942 "Mark beginning of buffer."
3945 (goto-char (point-min))
3946 (exchange-point-and-mark)
3947 (message "Mark set at the beginning of buffer"))
3949 (defun viper-mark-end-of-buffer ()
3950 "Mark end of buffer."
3953 (goto-char (point-max))
3954 (exchange-point-and-mark)
3955 (message "Mark set at the end of buffer"))
3957 (defun viper-mark-point ()
3958 "Set mark at point of buffer."
3960 (let ((char (read-char)))
3961 (cond ((and (<= ?a char) (<= char ?z))
3962 (point-to-register (1+ (- char ?a))))
3963 ((= char ?<) (viper-mark-beginning-of-buffer))
3964 ((= char ?>) (viper-mark-end-of-buffer))
3965 ((= char ?.) (viper-set-mark-if-necessary))
3966 ((= char ?,) (viper-cycle-through-mark-ring))
3967 ((= char ?D) (mark-defun))
3971 ;; Algorithm: If first invocation of this command save mark on ring, goto
3972 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
3973 ;; making it into the new mark, M1.
3974 ;; Push this mark back and set mark to the original point position, p1.
3975 ;; So, if you hit '' or `` then you can return to p1.
3977 ;; If repeated command, pop top elt from the ring into mark and
3978 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
3979 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
3980 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
3982 (defun viper-cycle-through-mark-ring ()
3983 "Visit previous locations on the mark ring.
3984 One can use `` and '' to temporarily jump 1 step back."
3985 (let* ((sv-pt (point)))
3986 ;; if repeated `m,' command, pop the previously saved mark.
3987 ;; Prev saved mark is actually prev saved point. It is used if the
3988 ;; user types `` or '' and is discarded
3989 ;; from the mark ring by the next `m,' command.
3990 ;; In any case, go to the previous or previously saved mark.
3991 ;; Then push the current mark (popped off the ring) and set current
3992 ;; point to be the mark. Current pt as mark is discarded by the next
3994 (if (eq last-command 'viper-cycle-through-mark-ring)
3996 ;; save current mark if the first iteration
3997 (setq mark-ring (delete (viper-mark-marker) mark-ring))
3999 (push-mark (mark t) t)) )
4001 (set-mark-command 1)
4002 ;; don't duplicate mark on the ring
4003 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4005 (viper-deactivate-mark)
4006 (setq this-command 'viper-cycle-through-mark-ring)
4010 (defun viper-goto-mark (arg)
4013 (let ((char (read-char))
4014 (com (viper-getcom arg)))
4015 (viper-goto-mark-subr char com nil)))
4017 (defun viper-goto-mark-and-skip-white (arg)
4018 "Go to mark and skip to first non-white character on line."
4020 (let ((char (read-char))
4021 (com (viper-getCom arg)))
4022 (viper-goto-mark-subr char com t)))
4024 (defun viper-goto-mark-subr (char com skip-white)
4027 (error "Empty buffer")
4029 (cond ((viper-valid-register char '(letter))
4030 (let* ((buff (current-buffer))
4031 (reg (1+ (- char ?a)))
4032 (text-marker (get-register reg)))
4033 (if com (viper-move-marker-locally 'viper-com-point (point)))
4034 (if (not (viper-valid-marker text-marker))
4035 (error viper-EmptyTextmarker char))
4036 (if (and (viper-same-line (point) viper-last-jump)
4037 (= (point) viper-last-jump-ignore))
4038 (push-mark viper-last-jump t)
4039 (push-mark nil t)) ; no msg
4040 (viper-register-to-point reg)
4041 (setq viper-last-jump (point-marker))
4043 (back-to-indentation)
4044 (setq viper-last-jump-ignore (point))))
4046 (if (equal buff (current-buffer))
4047 (viper-execute-com (if skip-white
4048 'viper-goto-mark-and-skip-white
4051 (switch-to-buffer buff)
4052 (goto-char viper-com-point)
4053 (viper-change-state-to-vi)
4055 ((and (not skip-white) (= char ?`))
4056 (if com (viper-move-marker-locally 'viper-com-point (point)))
4057 (if (and (viper-same-line (point) viper-last-jump)
4058 (= (point) viper-last-jump-ignore))
4059 (goto-char viper-last-jump))
4060 (if (null (mark t)) (error "Mark is not set in this buffer"))
4061 (if (= (point) (mark t)) (pop-mark))
4062 (exchange-point-and-mark)
4063 (setq viper-last-jump (point-marker)
4064 viper-last-jump-ignore 0)
4065 (if com (viper-execute-com 'viper-goto-mark nil com)))
4066 ((and skip-white (= char ?'))
4067 (if com (viper-move-marker-locally 'viper-com-point (point)))
4068 (if (and (viper-same-line (point) viper-last-jump)
4069 (= (point) viper-last-jump-ignore))
4070 (goto-char viper-last-jump))
4071 (if (= (point) (mark t)) (pop-mark))
4072 (exchange-point-and-mark)
4073 (setq viper-last-jump (point))
4074 (back-to-indentation)
4075 (setq viper-last-jump-ignore (point))
4076 (if com (viper-execute-com 'viper-goto-mark-and-skip-white nil com)))
4077 (t (error viper-InvalidTextmarker char))))
4079 (defun viper-insert-tab ()
4083 (defun viper-exchange-point-and-mark ()
4085 (exchange-point-and-mark)
4086 (back-to-indentation))
4088 ;; Input Mode Indentation
4090 ;; Returns t, if the string before point matches the regexp STR.
4091 (defsubst viper-looking-back (str)
4092 (and (save-excursion (re-search-backward str nil t))
4093 (= (point) (match-end 0))))
4096 (defun viper-forward-indent ()
4097 "Indent forward -- `C-t' in Vi."
4100 (indent-to (+ (current-column) viper-shift-width)))
4102 (defun viper-backward-indent ()
4103 "Backtab, C-d in VI"
4106 (let ((p (point)) (c (current-column)) bol (indent t))
4107 (if (viper-looking-back "[0^]")
4109 (if (eq ?^ (preceding-char))
4110 (setq viper-preserve-indent t))
4111 (delete-backward-char 1)
4117 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4118 (delete-region (point) p)
4120 (indent-to (- c viper-shift-width)))
4121 (if (or (bolp) (viper-looking-back "[^ \t]"))
4122 (setq viper-cted nil)))))
4124 (defun viper-autoindent ()
4125 "Auto Indentation, Vi-style."
4127 (let ((col (current-indentation)))
4128 (if abbrev-mode (expand-abbrev))
4129 (if viper-preserve-indent
4130 (setq viper-preserve-indent nil)
4131 (setq viper-current-indent col))
4132 ;; don't leave whitespace lines around
4133 (if (memq last-command
4135 viper-open-line viper-Open-line
4136 viper-replace-state-exit-cmd))
4137 (indent-to-left-margin))
4138 ;; use \n instead of newline, or else <Return> will move the insert point
4141 (if viper-auto-indent
4144 (if (and viper-electric-mode
4146 (memq major-mode '(fundamental-mode
4148 paragraph-indent-text-mode ))))
4149 (indent-according-to-mode)
4150 (indent-to viper-current-indent))
4155 ;; Viewing registers
4157 (defun viper-ket-function (arg)
4158 "Function called by \], the ket. View registers and call \]\]."
4160 (let ((reg (read-char)))
4161 (cond ((viper-valid-register reg '(letter Letter))
4162 (view-register (downcase reg)))
4163 ((viper-valid-register reg '(digit))
4164 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4166 (set-buffer (get-buffer-create "*Output*"))
4167 (delete-region (point-min) (point-max))
4168 (insert (format "Register %c contains the string:\n" reg))
4170 (goto-char (point-min)))
4171 (display-buffer "*Output*")))
4173 (viper-next-heading arg))
4175 viper-InvalidRegister reg)))))
4177 (defun viper-brac-function (arg)
4178 "Function called by \[, the brac. View textmarkers and call \[\["
4180 (let ((reg (read-char)))
4182 (viper-prev-heading arg))
4184 (viper-heading-end arg))
4185 ((viper-valid-register reg '(letter))
4186 (let* ((val (get-register (1+ (- reg ?a))))
4188 (error viper-EmptyTextmarker reg)
4189 (marker-buffer val)))
4190 (pos (marker-position val))
4191 line-no text (s pos) (e pos))
4193 (set-buffer (get-buffer-create "*Output*"))
4194 (delete-region (point-min) (point-max))
4199 (setq line-no (1+ (count-lines (point-min) val)))
4202 (if (re-search-backward "[^ \t]" nil t)
4208 (if (re-search-forward "[^ \t]" nil t)
4212 (setq text (buffer-substring s e))
4213 (setq text (format "%s<%c>%s"
4214 (substring text 0 (- pos s))
4215 reg (substring text (- pos s)))))
4218 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4219 reg (buffer-name buf) line-no))
4220 (insert (format "Here is some text around %c:\n\n %s"
4222 (insert (format viper-EmptyTextmarker reg)))
4223 (goto-char (point-min)))
4224 (display-buffer "*Output*")))
4225 (t (error viper-InvalidTextmarker reg)))))
4229 ;; commands in insertion mode
4231 (defun viper-delete-backward-word (arg)
4232 "Delete previous word."
4237 (delete-region (point) (mark t))
4241 (defun viper-set-expert-level (&optional dont-change-unless)
4242 "Sets the expert level for a Viper user.
4243 Can be called interactively to change (temporarily or permanently) the
4244 current expert level.
4246 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4247 the level should not be changed, unless its current value is
4248 meaningless (i.e., not one of 1,2,3,4,5).
4250 User level determines the setting of Viper variables that are most
4251 sensitive for VI-style look-and-feel."
4255 (if (not (natnump viper-expert-level)) (setq viper-expert-level 0))
4257 (save-window-excursion
4258 (delete-other-windows)
4259 ;; if 0 < viper-expert-level < viper-max-expert-level
4260 ;; & dont-change-unless = t -- use it; else ask
4261 (viper-ask-level dont-change-unless))
4263 (setq viper-always t
4264 viper-ex-style-motion t
4265 viper-ex-style-editing-in-insert t
4266 viper-want-ctl-h-help nil)
4268 (cond ((eq viper-expert-level 1) ; novice or beginner
4269 (global-set-key ; in emacs-state
4271 (if (viper-window-display-p) 'viper-iconify 'suspend-emacs))
4272 (setq viper-no-multiple-ESC t
4274 viper-vi-style-in-minibuffer t
4275 viper-search-wrap-around-t t
4276 viper-electric-mode nil
4277 viper-want-emacs-keys-in-vi nil
4278 viper-want-emacs-keys-in-insert nil))
4280 ((and (> viper-expert-level 1) (< viper-expert-level 5))
4281 ;; intermediate to guru
4282 (setq viper-no-multiple-ESC (if (viper-window-display-p)
4284 viper-electric-mode t
4285 viper-want-emacs-keys-in-vi t
4286 viper-want-emacs-keys-in-insert (> viper-expert-level 2))
4288 (if (eq viper-expert-level 4) ; respect user's ex-style motion
4289 ; and viper-no-multiple-ESC
4292 viper-ex-style-editing-in-insert
4293 (viper-standard-value 'viper-ex-style-editing-in-insert)
4294 viper-ex-style-motion
4295 (viper-standard-value 'viper-ex-style-motion))
4296 (setq viper-ex-style-motion
4297 (viper-standard-value 'viper-ex-style-motion)
4298 viper-ex-style-editing-in-insert
4299 (viper-standard-value 'viper-ex-style-editing-in-insert)
4301 (viper-standard-value 'viper-re-search)
4302 viper-no-multiple-ESC
4303 (viper-standard-value 'viper-no-multiple-ESC)))))
4306 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4307 ;; user toggle the values of variables.
4308 (t (setq-default viper-ex-style-editing-in-insert
4309 (viper-standard-value 'viper-ex-style-editing-in-insert)
4310 viper-ex-style-motion
4311 (viper-standard-value 'viper-ex-style-motion))
4312 (setq viper-want-ctl-h-help
4313 (viper-standard-value 'viper-want-ctl-h-help)
4315 (viper-standard-value 'viper-always)
4316 viper-no-multiple-ESC
4317 (viper-standard-value 'viper-no-multiple-ESC)
4318 viper-ex-style-motion
4319 (viper-standard-value 'viper-ex-style-motion)
4320 viper-ex-style-editing-in-insert
4321 (viper-standard-value 'viper-ex-style-editing-in-insert)
4323 (viper-standard-value 'viper-re-search)
4325 (viper-standard-value 'viper-electric-mode)
4326 viper-want-emacs-keys-in-vi
4327 (viper-standard-value 'viper-want-emacs-keys-in-vi)
4328 viper-want-emacs-keys-in-insert
4329 (viper-standard-value 'viper-want-emacs-keys-in-insert))))
4331 (viper-set-mode-vars-for viper-current-state)
4332 (if (or viper-always
4333 (and (> viper-expert-level 0) (> 5 viper-expert-level)))
4336 ;; Ask user expert level.
4337 (defun viper-ask-level (dont-change-unless)
4338 (let ((ask-buffer " *viper-ask-level*")
4339 level-changed repeated)
4340 (save-window-excursion
4341 (switch-to-buffer ask-buffer)
4343 (while (or (> viper-expert-level viper-max-expert-level)
4344 (< viper-expert-level 1)
4345 (null dont-change-unless))
4349 (message "Invalid user level")
4352 (setq dont-change-unless t
4355 Please specify your level of familiarity with the venomous VI PERil
4356 (and the VI Plan for Emacs Rescue).
4357 You can change it at any time by typing `M-x viper-set-expert-level RET'
4359 1 -- BEGINNER: Almost all Emacs features are suppressed.
4360 Feels almost like straight Vi. File name completion and
4361 command history in the minibuffer are thrown in as a bonus.
4362 To use Emacs productively, you must reach level 3 or higher.
4363 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4364 so most Emacs commands can be used when Viper is in Vi state.
4365 Good progress---you are well on the way to level 3!
4366 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
4367 in Viper's insert state.
4368 4 -- GURU: Like 3, but user settings are respected for viper-no-multiple-ESC,
4369 viper-ex-style-motion, viper-ex-style-editing-in-insert, and
4370 viper-re-search variables. Adjust these settings to your taste.
4371 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4372 viper-electric-mode, viper-want-ctl-h-help, viper-want-emacs-keys-in-vi,
4373 and viper-want-emacs-keys-in-insert. Adjust these to your taste.
4375 Please, specify your level now: ")
4377 (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
4380 ;; tell the user if level was changed
4384 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4385 viper-expert-level))
4386 (if (y-or-n-p "Do you wish to make this change permanent? ")
4387 ;; save the setting for viper-expert-level
4390 (format "Saving user level %d ..." viper-expert-level)
4391 viper-custom-file-name))
4393 (bury-buffer) ; remove ask-buffer from screen
4403 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4404 (defun viper-register-to-point (char &optional enforce-buffer)
4405 "Like jump-to-register, but switches to another buffer in another window."
4406 (interactive "cViper register to point: ")
4407 (let ((val (get-register char)))
4409 ((and (fboundp 'frame-configuration-p)
4410 (frame-configuration-p val))
4411 (set-frame-configuration val))
4412 ((window-configuration-p val)
4413 (set-window-configuration val))
4414 ((viper-valid-marker val)
4415 (if (and enforce-buffer
4416 (not (equal (current-buffer) (marker-buffer val))))
4417 (error (concat viper-EmptyTextmarker " in this buffer")
4419 (pop-to-buffer (marker-buffer val))
4421 ((and (consp val) (eq (car val) 'file))
4422 (find-file (cdr val)))
4424 (error viper-EmptyTextmarker (1- (+ char ?a)))))))
4427 (defun viper-save-kill-buffer ()
4428 "Save then kill current buffer. "
4430 (if (< viper-expert-level 2)
4431 (save-buffers-kill-emacs)
4433 (kill-buffer (current-buffer))))
4439 (defun viper-submit-report ()
4440 "Submit bug report on Viper."
4442 (let ((reporter-prompt-for-summary-p t)
4443 (viper-device-type (viper-device-type))
4444 color-display-p frame-parameters
4445 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4446 varlist salutation window-config)
4448 ;; If mode info is needed, add variable to `let' and then set it below,
4449 ;; like we did with color-display-p.
4450 (setq color-display-p (if (viper-window-display-p)
4451 (viper-color-display-p)
4453 minibuffer-vi-face (if (viper-has-face-support-p)
4454 (viper-get-face viper-minibuffer-vi-face)
4456 minibuffer-insert-face (if (viper-has-face-support-p)
4458 viper-minibuffer-insert-face)
4460 minibuffer-emacs-face (if (viper-has-face-support-p)
4462 viper-minibuffer-emacs-face)
4464 frame-parameters (if (fboundp 'frame-parameters)
4465 (frame-parameters (selected-frame))))
4467 (setq varlist (list 'viper-vi-minibuffer-minor-mode
4468 'viper-insert-minibuffer-minor-mode
4469 'viper-vi-intercept-minor-mode
4470 'viper-vi-local-user-minor-mode
4471 'viper-vi-kbd-minor-mode
4472 'viper-vi-global-user-minor-mode
4473 'viper-vi-state-modifier-minor-mode
4474 'viper-vi-diehard-minor-mode
4475 'viper-vi-basic-minor-mode
4476 'viper-replace-minor-mode
4477 'viper-insert-intercept-minor-mode
4478 'viper-insert-local-user-minor-mode
4479 'viper-insert-kbd-minor-mode
4480 'viper-insert-global-user-minor-mode
4481 'viper-insert-state-modifier-minor-mode
4482 'viper-insert-diehard-minor-mode
4483 'viper-insert-basic-minor-mode
4484 'viper-emacs-intercept-minor-mode
4485 'viper-emacs-local-user-minor-mode
4486 'viper-emacs-kbd-minor-mode
4487 'viper-emacs-global-user-minor-mode
4488 'viper-emacs-state-modifier-minor-mode
4489 'viper-automatic-iso-accents
4490 'viper-want-emacs-keys-in-insert
4491 'viper-want-emacs-keys-in-vi
4492 'viper-keep-point-on-undo
4493 'viper-no-multiple-ESC
4494 'viper-electric-mode
4496 'viper-want-ctl-h-help
4497 'viper-ex-style-editing-in-insert
4498 'viper-delete-backwards-in-replace
4499 'viper-vi-style-in-minibuffer
4500 'viper-vi-state-hook
4501 'viper-insert-state-hook
4502 'viper-replace-state-hook
4503 'viper-emacs-state-hook
4504 'ex-cycle-other-window
4505 'ex-cycle-through-non-files
4512 'minibuffer-insert-face
4513 'minibuffer-emacs-face
4516 Congratulations! You may have unearthed a bug in Viper!
4517 Please mail a concise, accurate summary of the problem to the address above.
4519 -------------------------------------------------------------------")
4520 (setq window-config (current-window-configuration))
4521 (with-output-to-temp-buffer " *viper-info*"
4522 (switch-to-buffer " *viper-info*")
4523 (delete-other-windows)
4525 PLEASE FOLLOW THESE PROCEDURES
4526 ------------------------------
4528 Before reporting a bug, please verify that it is related to Viper, and is
4529 not cause by other packages you are using.
4531 Don't report compilation warnings, unless you are certain that there is a
4532 problem. These warnings are normal and unavoidable.
4534 Please note that users should not modify variables and keymaps other than
4535 those advertised in the manual. Such `customization' is likely to crash
4536 Viper, as it would any other improperly customized Emacs package.
4538 If you are reporting an error message received while executing one of the
4539 Viper commands, type:
4541 M-x set-variable <Return> debug-on-error <Return> t <Return>
4543 Then reproduce the error. The above command will cause Emacs to produce a
4544 back trace of the execution that leads to the error. Please include this
4545 trace in your bug report.
4547 If you believe that one of Viper's commands goes into an infinite loop
4548 \(e.g., Emacs freezes\), type:
4550 M-x set-variable <Return> debug-on-quit <Return> t <Return>
4552 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
4553 the current command. Include the resulting back trace in the bug report.
4555 Mail anyway (y or n)? ")
4556 (if (y-or-n-p "Mail anyway? ")
4558 (set-window-configuration window-config)
4559 (error "Bug report aborted")))
4562 (set-window-configuration window-config)
4564 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
4567 nil 'delete-other-windows
4574 ;; Smoothes out the difference between Emacs' unread-command-events
4575 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
4576 ;; events or a sequence of keys.
4578 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
4579 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
4580 ;; into an event. Below, we delete nil from event lists, since nil is the most
4581 ;; common symbol that might appear in this wrong context.
4582 (defun viper-set-unread-command-events (arg)
4585 unread-command-events
4587 (cond ((eventp arg) (list arg))
4590 (listify-key-sequence arg))
4592 "viper-set-unread-command-events: Invalid argument, %S"
4594 (if (not (eventp nil))
4595 (setq new-events (delq nil new-events)))
4596 (append new-events unread-command-events)))
4599 unread-command-events
4601 (cond ((viper-characterp arg) (list (character-to-event arg)))
4602 ((eventp arg) (list arg))
4603 ((stringp arg) (mapcar 'character-to-event arg))
4604 ((vectorp arg) (append arg nil)) ; turn into list
4605 ((listp arg) (viper-eventify-list-xemacs arg))
4607 "viper-set-unread-command-events: Invalid argument, %S" arg)))
4608 unread-command-events))))
4610 ;; list is assumed to be a list of events of characters
4611 (defun viper-eventify-list-xemacs (lis)
4613 (function (lambda (elt)
4614 (cond ((viper-characterp elt) (character-to-event elt))
4617 "viper-eventify-list-xemacs: can't convert to event, %S"
4623 ;;; viper-cmd.el ends here