1 ;;; viper-cmd.el --- Vi command support for Viper
3 ;; Copyright (C) 1997-2016 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (defvar viper-minibuffer-current-face)
31 (defvar viper-minibuffer-insert-face)
32 (defvar viper-minibuffer-vi-face)
33 (defvar viper-minibuffer-emacs-face)
35 (defvar viper-mode-string)
36 (defvar viper-custom-file-name)
37 (defvar viper--key-maps)
38 (defvar viper--intercept-key-maps)
39 (defvar iso-accents-mode)
41 (defvar quail-current-str)
42 (defvar mark-even-if-inactive)
44 (defvar viper-initial)
45 (defvar undo-beg-posn)
46 (defvar undo-end-posn)
49 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
63 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
65 ;; generate test functions
66 ;; given symbol foo, foo-p is the test function, foos is the set of
68 ;; (macroexpand '(viper-test-com-defun foo))
69 ;; (defun foo-p (com) (consp (memq com foos)))
71 (defmacro viper-test-com-defun (name)
72 (let* ((snm (symbol-name name))
73 (nm-p (intern (concat snm "-p")))
74 (nms (intern (concat snm "s"))))
76 (consp (viper-memq-char com ,nms)
79 ;; Variables for defining VI commands
81 ;; Modifying commands that can be prefixes to movement commands
82 (defvar viper-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
83 ;; define viper-prefix-command-p
84 (viper-test-com-defun viper-prefix-command)
86 ;; Commands that are pairs eg. dd. r and R here are a hack
87 (defconst viper-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
88 ;; define viper-charpair-command-p
89 (viper-test-com-defun viper-charpair-command)
91 (defconst viper-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?j ?k ?l
92 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
93 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
94 ?\; ?, ?0 ?? ?/ ?\ ?\C-m
99 ;; define viper-movement-command-p
100 (viper-test-com-defun viper-movement-command)
103 (defconst viper-digit-commands '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
105 ;; define viper-digit-command-p
106 (viper-test-com-defun viper-digit-command)
108 ;; Commands that can be repeated by . (dotted)
109 (defconst viper-dotable-commands '(?c ?d ?C ?s ?S ?D ?> ?<))
110 ;; define viper-dotable-command-p
111 (viper-test-com-defun viper-dotable-command)
113 ;; Commands that can follow a #
114 (defconst viper-hash-commands '(?c ?C ?g ?q ?s))
115 ;; define viper-hash-command-p
116 (viper-test-com-defun viper-hash-command)
118 ;; Commands that may have registers as prefix
119 (defconst viper-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
120 ;; define viper-regsuffix-command-p
121 (viper-test-com-defun viper-regsuffix-command)
123 (defconst viper-vi-commands (append viper-movement-commands
125 viper-dotable-commands
126 viper-charpair-commands
128 viper-prefix-commands
129 viper-regsuffix-commands)
130 "The list of all commands in Vi-state.")
131 ;; define viper-vi-command-p
132 (viper-test-com-defun viper-vi-command)
134 ;; Where viper saves mark. This mark is resurrected by m^
135 (defvar viper-saved-mark nil)
137 ;; Contains user settings for vars affected by viper-set-expert-level function.
138 ;; Not a user option.
139 (defvar viper-saved-user-settings nil)
147 ;; Runs viper-after-change-functions inside after-change-functions
148 (defun viper-after-change-sentinel (beg end len)
149 (run-hook-with-args 'viper-after-change-functions beg end len))
151 ;; Runs viper-before-change-functions inside before-change-functions
152 (defun viper-before-change-sentinel (beg end)
153 (run-hook-with-args 'viper-before-change-functions beg end))
155 (defsubst viper-post-command-sentinel ()
156 (condition-case conds
157 (run-hooks 'viper-post-command-hooks)
158 (error (viper-message-conditions conds)))
159 (if (eq viper-current-state 'vi-state)
160 (viper-restore-cursor-color 'after-insert-mode)))
162 (defsubst viper-pre-command-sentinel ()
163 (run-hooks 'viper-pre-command-hooks))
165 ;; Needed so that Viper will be able to figure the last inserted
166 ;; chunk of text with reasonable accuracy.
167 (defsubst viper-insert-state-post-command-sentinel ()
168 (if (and (memq viper-current-state '(insert-state replace-state))
170 (>= (point) viper-insert-point))
171 (setq viper-last-posn-while-in-insert-state (point-marker)))
172 (or (viper-overlay-p viper-replace-overlay)
174 (viper-set-replace-overlay (point-min) (point-min))
175 (viper-hide-replace-overlay)))
176 (if (eq viper-current-state 'insert-state)
177 (let ((icolor (viper-frame-value viper-insert-state-cursor-color)))
178 (or (stringp (viper-get-saved-cursor-color-in-insert-mode))
179 (string= (viper-get-cursor-color) icolor)
180 ;; save current color, if not already saved
181 (viper-save-cursor-color 'before-insert-mode))
182 ;; set insert mode cursor color
183 (viper-change-cursor-color icolor)))
184 (let ((ecolor (viper-frame-value viper-emacs-state-cursor-color)))
185 (when (and ecolor (eq viper-current-state 'emacs-state))
186 (or (stringp (viper-get-saved-cursor-color-in-emacs-mode))
187 (string= (viper-get-cursor-color) ecolor)
188 ;; save current color, if not already saved
189 (viper-save-cursor-color 'before-emacs-mode))
190 ;; set emacs mode cursor color
191 (viper-change-cursor-color ecolor)))
193 (if (and (memq this-command '(dabbrev-expand hippie-expand))
194 (integerp viper-pre-command-point)
195 (markerp viper-insert-point)
196 (marker-position viper-insert-point)
197 (> viper-insert-point viper-pre-command-point))
198 (viper-move-marker-locally viper-insert-point viper-pre-command-point)))
200 (defsubst viper-preserve-cursor-color ()
201 (or (memq this-command '(self-insert-command
202 viper-del-backward-char-in-insert
203 viper-del-backward-char-in-replace
204 viper-delete-backward-char
207 (memq (viper-event-key last-command-event)
208 '(up down left right (meta f) (meta b)
209 (control n) (control p) (control f) (control b)))))
211 (defsubst viper-insert-state-pre-command-sentinel ()
212 (or (viper-preserve-cursor-color)
213 (viper-restore-cursor-color 'after-insert-mode))
214 (if (and (memq this-command '(dabbrev-expand hippie-expand))
215 (markerp viper-insert-point)
216 (marker-position viper-insert-point))
217 (setq viper-pre-command-point (marker-position viper-insert-point))))
219 (defun viper-R-state-post-command-sentinel ()
220 ;; Restoring cursor color is needed despite
221 ;; viper-replace-state-pre-command-sentinel: When you jump to another buffer
222 ;; in another frame, the pre-command hook won't change cursor color to
223 ;; default in that other frame. So, if the second frame cursor was red and
224 ;; we set the point outside the replacement region, then the cursor color
225 ;; will remain red. Restoring the default, below, prevents this.
226 (if (and (<= (viper-replace-start) (point))
227 (<= (point) (viper-replace-end)))
228 (viper-change-cursor-color
229 (viper-frame-value viper-replace-overlay-cursor-color))
230 (viper-restore-cursor-color 'after-replace-mode)))
232 ;; to speed up, don't change cursor color before self-insert
233 ;; and common move commands
234 (defsubst viper-replace-state-pre-command-sentinel ()
235 (or (viper-preserve-cursor-color)
236 (viper-restore-cursor-color 'after-replace-mode)))
239 ;; Make sure we don't delete more than needed.
240 ;; This is executed at viper-last-posn-in-replace-region
241 (defsubst viper-trim-replace-chars-to-delete-if-necessary ()
242 (setq viper-replace-chars-to-delete
244 (min viper-replace-chars-to-delete
245 ;; Don't delete more than to the end of repl overlay
246 (viper-chars-in-region
247 (viper-replace-end) viper-last-posn-in-replace-region)
248 ;; point is viper-last-posn-in-replace-region now
249 ;; So, this limits deletion to the end of line
250 (viper-chars-in-region (point) (viper-line-pos 'end))
254 (defun viper-replace-state-post-command-sentinel ()
255 ;; Restoring cursor color is needed despite
256 ;; viper-replace-state-pre-command-sentinel: When one jumps to another buffer
257 ;; in another frame, the pre-command hook won't change cursor color to
258 ;; default in that other frame. So, if the second frame cursor was red and
259 ;; we set the point outside the replacement region, then the cursor color
260 ;; will remain red. Restoring the default, below, fixes this problem.
262 ;; We optimize for some commands, like self-insert-command,
263 ;; viper-delete-backward-char, etc., since they either don't change
264 ;; cursor color or, if they terminate replace mode, the color will be changed
265 ;; in viper-finish-change
266 (or (viper-preserve-cursor-color)
267 (viper-restore-cursor-color 'after-replace-mode))
269 ((eq viper-current-state 'replace-state)
270 ;; delete characters to compensate for inserted chars.
271 (let ((replace-boundary (viper-replace-end)))
273 (goto-char viper-last-posn-in-replace-region)
274 (viper-trim-replace-chars-to-delete-if-necessary)
275 (delete-char viper-replace-chars-to-delete)
276 (setq viper-replace-chars-to-delete 0)
277 ;; terminate replace mode if reached replace limit
278 (if (= viper-last-posn-in-replace-region (viper-replace-end))
279 (viper-finish-change)))
281 (when (viper-pos-within-region
282 (point) (viper-replace-start) replace-boundary)
283 ;; the state may have changed in viper-finish-change above
284 (if (eq viper-current-state 'replace-state)
285 (viper-change-cursor-color
286 (viper-frame-value viper-replace-overlay-cursor-color)))
287 (setq viper-last-posn-in-replace-region (point-marker)))))
288 ;; terminate replace mode if changed Viper states.
289 (t (viper-finish-change))))
294 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
295 (defun viper-change-state (new-state)
296 ;; Keep viper-post/pre-command-hooks fresh.
297 ;; We remove then add viper-post/pre-command-sentinel since it is very
298 ;; desirable that viper-pre-command-sentinel is the last hook and
299 ;; viper-post-command-sentinel is the first hook.
301 (when (featurep 'xemacs)
302 (make-local-hook 'viper-after-change-functions)
303 (make-local-hook 'viper-before-change-functions)
304 (make-local-hook 'viper-post-command-hooks)
305 (make-local-hook 'viper-pre-command-hooks))
307 (remove-hook 'post-command-hook 'viper-post-command-sentinel)
308 (add-hook 'post-command-hook 'viper-post-command-sentinel)
309 (remove-hook 'pre-command-hook 'viper-pre-command-sentinel)
310 (add-hook 'pre-command-hook 'viper-pre-command-sentinel t)
311 ;; These hooks will be added back if switching to insert/replace mode
312 (remove-hook 'viper-post-command-hooks
313 'viper-insert-state-post-command-sentinel 'local)
314 (remove-hook 'viper-pre-command-hooks
315 'viper-insert-state-pre-command-sentinel 'local)
316 (setq viper-intermediate-command nil)
317 (cond ((eq new-state 'vi-state)
318 (cond ((member viper-current-state '(insert-state replace-state))
320 ;; move viper-last-posn-while-in-insert-state
321 ;; This is a normal hook that is executed in insert/replace
322 ;; states after each command. In Vi/Emacs state, it does
323 ;; nothing. We need to execute it here to make sure that
324 ;; the last posn was recorded when we hit ESC.
325 ;; It may be left unrecorded if the last thing done in
326 ;; insert/repl state was dabbrev-expansion or abbrev
327 ;; expansion caused by hitting ESC
328 (viper-insert-state-post-command-sentinel)
330 (condition-case conds
332 (viper-save-last-insertion
334 viper-last-posn-while-in-insert-state)
335 (if viper-began-as-replace
336 (setq viper-began-as-replace nil)
337 ;; repeat insert commands if numerical arg > 1
339 (viper-repeat-insert-command))))
341 (viper-message-conditions conds)))
343 (if (> (length viper-last-insertion) 0)
344 (viper-push-onto-ring viper-last-insertion
345 'viper-insertion-ring))
347 (if viper-ESC-moves-cursor-back
348 (or (bolp) (viper-beginning-of-field) (backward-char 1))))
352 ((memq new-state '(insert-state replace-state))
353 (if (memq viper-current-state '(emacs-state vi-state))
354 (viper-move-marker-locally 'viper-insert-point (point)))
355 (viper-move-marker-locally
356 'viper-last-posn-while-in-insert-state (point))
357 (add-hook 'viper-post-command-hooks
358 'viper-insert-state-post-command-sentinel t 'local)
359 (add-hook 'viper-pre-command-hooks
360 'viper-insert-state-pre-command-sentinel t 'local))
363 ;; Nothing needs to be done to switch to emacs mode! Just set some
364 ;; variables, which is already done in viper-change-state-to-emacs!
367 ;; always turn off iso-accents-mode in vi-state, or else we won't be able to
368 ;; use the keys `,',^ , as they will do accents instead of Vi actions.
369 (cond ((eq new-state 'vi-state) (viper-set-iso-accents-mode nil));accents off
370 (viper-automatic-iso-accents (viper-set-iso-accents-mode t));accents on
371 (t (viper-set-iso-accents-mode nil)))
372 ;; Always turn off quail mode in vi state
373 (cond ((eq new-state 'vi-state) (viper-set-input-method nil)) ;intl input off
374 (viper-special-input-method (viper-set-input-method t)) ;intl input on
375 (t (viper-set-input-method nil)))
377 (setq viper-current-state new-state)
379 (viper-update-syntax-classes)
380 (viper-normalize-minor-mode-map-alist)
381 (viper-adjust-keys-for new-state)
382 (viper-set-mode-vars-for new-state)
383 (viper-refresh-mode-line)
387 (defun viper-adjust-keys-for (state)
388 "Make necessary adjustments to keymaps before entering STATE."
389 (cond ((memq state '(insert-state replace-state))
390 (if viper-auto-indent
392 (define-key viper-insert-basic-map "\C-m" 'viper-autoindent)
393 (if viper-want-emacs-keys-in-insert
395 (define-key viper-insert-basic-map "\C-j" nil)
397 (define-key viper-insert-basic-map "\C-j" 'viper-autoindent)))
398 (define-key viper-insert-basic-map "\C-m" nil)
399 (define-key viper-insert-basic-map "\C-j" nil))
401 (setq viper-insert-diehard-minor-mode
402 (not viper-want-emacs-keys-in-insert))
404 (if viper-want-ctl-h-help
406 (define-key viper-insert-basic-map "\C-h" 'help-command)
407 (define-key viper-replace-map "\C-h" 'help-command))
408 (define-key viper-insert-basic-map
409 "\C-h" 'viper-del-backward-char-in-insert)
410 (define-key viper-replace-map
411 "\C-h" 'viper-del-backward-char-in-replace))
412 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
413 (define-key viper-insert-basic-map
414 [backspace] 'viper-del-backward-char-in-insert)
415 (define-key viper-replace-map
416 [backspace] 'viper-del-backward-char-in-replace)
417 ) ; end insert/replace case
419 (setq viper-vi-diehard-minor-mode (not viper-want-emacs-keys-in-vi))
420 (if viper-want-ctl-h-help
421 (define-key viper-vi-basic-map "\C-h" 'help-command)
422 (define-key viper-vi-basic-map "\C-h" 'viper-backward-char))
423 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
424 (define-key viper-vi-basic-map [backspace] 'viper-backward-char))
428 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
429 ;; This ensures that Viper bindings are in effect, regardless of which minor
430 ;; modes were turned on by the user or by other packages.
431 (defun viper-normalize-minor-mode-map-alist ()
432 (setq viper--intercept-key-maps
434 (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map)
435 (cons 'viper-insert-intercept-minor-mode viper-insert-intercept-map)
436 (cons 'viper-emacs-intercept-minor-mode viper-emacs-intercept-map)
438 (setq viper--key-maps
439 (list (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map)
440 (cons 'viper-vi-local-user-minor-mode viper-vi-local-user-map)
441 (cons 'viper-vi-kbd-minor-mode viper-vi-kbd-map)
442 (cons 'viper-vi-global-user-minor-mode viper-vi-global-user-map)
443 (cons 'viper-vi-state-modifier-minor-mode
445 (cdr (assoc major-mode viper-vi-state-modifier-alist)))
446 (cdr (assoc major-mode viper-vi-state-modifier-alist))
448 (cons 'viper-vi-diehard-minor-mode viper-vi-diehard-map)
449 (cons 'viper-vi-basic-minor-mode viper-vi-basic-map)
450 (cons 'viper-replace-minor-mode viper-replace-map)
451 ;; viper-insert-minibuffer-minor-mode must come after
452 ;; viper-replace-minor-mode
453 (cons 'viper-insert-minibuffer-minor-mode
454 viper-minibuffer-map)
455 (cons 'viper-insert-local-user-minor-mode
456 viper-insert-local-user-map)
457 (cons 'viper-insert-kbd-minor-mode viper-insert-kbd-map)
458 (cons 'viper-insert-global-user-minor-mode
459 viper-insert-global-user-map)
460 (cons 'viper-insert-state-modifier-minor-mode
462 (cdr (assoc major-mode
463 viper-insert-state-modifier-alist)))
464 (cdr (assoc major-mode
465 viper-insert-state-modifier-alist))
467 (cons 'viper-insert-diehard-minor-mode viper-insert-diehard-map)
468 (cons 'viper-insert-basic-minor-mode viper-insert-basic-map)
469 (cons 'viper-emacs-local-user-minor-mode
470 viper-emacs-local-user-map)
471 (cons 'viper-emacs-kbd-minor-mode viper-emacs-kbd-map)
472 (cons 'viper-emacs-global-user-minor-mode
473 viper-emacs-global-user-map)
474 (cons 'viper-emacs-state-modifier-minor-mode
477 (assoc major-mode viper-emacs-state-modifier-alist)))
479 (assoc major-mode viper-emacs-state-modifier-alist))
483 ;; This var is not local in Emacs, so we make it local. It must be local
484 ;; because although the stack of minor modes can be the same for all buffers,
485 ;; the associated *keymaps* can be different. In Viper,
486 ;; viper-vi-local-user-map, viper-insert-local-user-map, and others can have
487 ;; different keymaps for different buffers. Also, the keymaps associated
488 ;; with viper-vi/insert-state-modifier-minor-mode can be different.
489 ;; ***This is needed only in case emulation-mode-map-alists is not defined.
490 ;; In emacs with emulation-mode-map-alists, nothing needs to be done
492 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
493 (set (make-local-variable 'minor-mode-map-alist)
494 (viper-append-filter-alist
495 (append viper--intercept-key-maps viper--key-maps)
496 minor-mode-map-alist)))
501 ;; Viper mode-changing commands and utilities
503 ;; Modifies mode-line-buffer-identification.
504 (defun viper-refresh-mode-line ()
505 (set (make-local-variable 'viper-mode-string)
506 (cond ((eq viper-current-state 'emacs-state) viper-emacs-state-id)
507 ((eq viper-current-state 'vi-state) viper-vi-state-id)
508 ((eq viper-current-state 'replace-state) viper-replace-state-id)
509 ((eq viper-current-state 'insert-state) viper-insert-state-id)))
511 ;; Sets Viper mode string in global-mode-string
512 (force-mode-line-update))
515 ;; Switch from Insert state to Vi state.
516 (defun viper-exit-insert-state ()
518 (viper-change-state-to-vi))
520 (defun viper-set-mode-vars-for (state)
521 "Sets Viper minor mode variables to put Viper's state STATE in effect."
524 (setq viper-vi-minibuffer-minor-mode nil
525 viper-insert-minibuffer-minor-mode nil
526 viper-vi-intercept-minor-mode nil
527 viper-insert-intercept-minor-mode nil
529 viper-vi-local-user-minor-mode nil
530 viper-vi-kbd-minor-mode nil
531 viper-vi-global-user-minor-mode nil
532 viper-vi-state-modifier-minor-mode nil
533 viper-vi-diehard-minor-mode nil
534 viper-vi-basic-minor-mode nil
536 viper-replace-minor-mode nil
538 viper-insert-local-user-minor-mode nil
539 viper-insert-kbd-minor-mode nil
540 viper-insert-global-user-minor-mode nil
541 viper-insert-state-modifier-minor-mode nil
542 viper-insert-diehard-minor-mode nil
543 viper-insert-basic-minor-mode nil
544 viper-emacs-intercept-minor-mode t
545 viper-emacs-local-user-minor-mode t
546 viper-emacs-kbd-minor-mode (not (viper-is-in-minibuffer))
547 viper-emacs-global-user-minor-mode t
548 viper-emacs-state-modifier-minor-mode t
552 (if (eq state 'vi-state) ; adjust for vi-state
554 viper-vi-intercept-minor-mode t
555 viper-vi-minibuffer-minor-mode (viper-is-in-minibuffer)
556 viper-vi-local-user-minor-mode t
557 viper-vi-kbd-minor-mode (not (viper-is-in-minibuffer))
558 viper-vi-global-user-minor-mode t
559 viper-vi-state-modifier-minor-mode t
560 ;; don't let the diehard keymap block command completion
561 ;; and other things in the minibuffer
562 viper-vi-diehard-minor-mode (not
563 (or viper-want-emacs-keys-in-vi
564 (viper-is-in-minibuffer)))
565 viper-vi-basic-minor-mode t
566 viper-emacs-intercept-minor-mode nil
567 viper-emacs-local-user-minor-mode nil
568 viper-emacs-kbd-minor-mode nil
569 viper-emacs-global-user-minor-mode nil
570 viper-emacs-state-modifier-minor-mode nil
573 ;; Insert and Replace states
574 (if (member state '(insert-state replace-state))
576 viper-insert-intercept-minor-mode t
577 viper-replace-minor-mode (eq state 'replace-state)
578 viper-insert-minibuffer-minor-mode (viper-is-in-minibuffer)
579 viper-insert-local-user-minor-mode t
580 viper-insert-kbd-minor-mode (not (viper-is-in-minibuffer))
581 viper-insert-global-user-minor-mode t
582 viper-insert-state-modifier-minor-mode t
583 ;; don't let the diehard keymap block command completion
584 ;; and other things in the minibuffer
585 viper-insert-diehard-minor-mode (not
587 viper-want-emacs-keys-in-insert
588 (viper-is-in-minibuffer)))
589 viper-insert-basic-minor-mode t
590 viper-emacs-intercept-minor-mode nil
591 viper-emacs-local-user-minor-mode nil
592 viper-emacs-kbd-minor-mode nil
593 viper-emacs-global-user-minor-mode nil
594 viper-emacs-state-modifier-minor-mode nil
598 (if (viper-has-face-support-p)
599 (setq viper-minibuffer-current-face
600 (cond ((eq state 'emacs-state) viper-minibuffer-emacs-face)
601 ((eq state 'vi-state) viper-minibuffer-vi-face)
602 ((memq state '(insert-state replace-state))
603 viper-minibuffer-insert-face))))
605 (if (viper-is-in-minibuffer)
606 (viper-set-minibuffer-overlay))
609 ;; This also takes care of the annoying incomplete lines in files.
610 ;; Also, this fixes `undo' to work vi-style for complex commands.
611 (defun viper-change-state-to-vi ()
612 "Change Viper state to Vi."
614 (if (and viper-first-time (not (viper-is-in-minibuffer)))
616 (if overwrite-mode (overwrite-mode -1))
617 (or (viper-overlay-p viper-replace-overlay)
618 (viper-set-replace-overlay (point-min) (point-min)))
619 (viper-hide-replace-overlay)
620 ;; Expand abbrevs iff the previous character has word syntax.
622 (eq (char-syntax (preceding-char)) ?w)
624 (if (and auto-fill-function (> (current-column) fill-column))
625 (funcall auto-fill-function))
626 ;; don't leave whitespace lines around
627 (if (and (memq last-command
629 viper-open-line viper-Open-line
630 viper-replace-state-exit-cmd))
631 (viper-over-whitespace-line))
632 (indent-to-left-margin))
633 (viper-add-newline-at-eob-if-necessary)
634 (viper-complete-complex-command-for-undo)
636 (if (eq viper-current-state 'emacs-state)
637 (viper-restore-cursor-color 'after-emacs-mode)
638 (viper-restore-cursor-color 'after-insert-mode))
640 (viper-change-state 'vi-state)
642 ;; Protect against user errors in hooks
643 (condition-case conds
644 (run-hooks 'viper-vi-state-hook)
646 (viper-message-conditions conds)))))
648 (defun viper-change-state-to-insert ()
649 "Change Viper state to Insert."
651 (viper-change-state 'insert-state)
653 (or (viper-overlay-p viper-replace-overlay)
654 (viper-set-replace-overlay (point-min) (point-min)))
655 (viper-hide-replace-overlay)
657 (let ((icolor (viper-frame-value viper-insert-state-cursor-color)))
658 (or (stringp (viper-get-saved-cursor-color-in-insert-mode))
659 (string= (viper-get-cursor-color) icolor)
660 (viper-save-cursor-color 'before-insert-mode))
661 (viper-change-cursor-color icolor))
663 ;; Protect against user errors in hooks
664 (condition-case conds
665 (run-hooks 'viper-insert-state-hook)
667 (viper-message-conditions conds))))
669 (defsubst viper-downgrade-to-insert ()
670 ;; Protect against user errors in hooks
671 (condition-case conds
672 (run-hooks 'viper-insert-state-hook)
674 (viper-message-conditions conds)))
675 (setq viper-current-state 'insert-state
676 viper-replace-minor-mode nil))
680 ;; Change to replace state. When the end of replacement region is reached,
681 ;; replace state changes to insert state.
682 (defun viper-change-state-to-replace (&optional non-R-cmd)
683 (viper-change-state 'replace-state)
684 ;; Run insert-state-hook
685 (condition-case conds
686 (run-hooks 'viper-insert-state-hook 'viper-replace-state-hook)
688 (viper-message-conditions conds)))
691 (viper-start-replace)
692 ;; 'R' is implemented using Emacs's overwrite-mode
693 (viper-start-R-mode))
697 (defun viper-change-state-to-emacs ()
698 "Change Viper state to Emacs."
700 (or (viper-overlay-p viper-replace-overlay)
701 (viper-set-replace-overlay (point-min) (point-min)))
702 (viper-hide-replace-overlay)
704 (let ((ecolor (viper-frame-value viper-emacs-state-cursor-color)))
706 (or (stringp (viper-get-saved-cursor-color-in-emacs-mode))
707 (string= (viper-get-cursor-color) ecolor)
708 (viper-save-cursor-color 'before-emacs-mode))
709 (viper-change-cursor-color ecolor)))
711 (viper-change-state 'emacs-state)
713 ;; Protect against user errors in hooks
714 (condition-case conds
715 (run-hooks 'viper-emacs-state-hook)
717 (viper-message-conditions conds))))
719 ;; escape to emacs mode temporarily
720 (defun viper-escape-to-emacs (arg &optional events)
721 "Escape to Emacs state from Vi state for one Emacs command.
722 ARG is used as the prefix value for the executed command. If
723 EVENTS is a list of events, which become the beginning of the command."
725 (if (viper= (viper-last-command-char) ?\\)
726 (message "Switched to EMACS state for the next command..."))
727 (viper-escape-to-state arg events 'emacs-state))
729 ;; escape to Vi mode temporarily
730 (defun viper-escape-to-vi (arg)
731 "Escape from Emacs state to Vi state for one Vi 1-character command.
732 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
733 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
734 `viper-escape-to-vi' is used."
736 (message "Switched to VI state for the next command...")
737 (viper-escape-to-state arg nil 'vi-state))
739 ;; Escape to STATE mode for one Emacs command.
740 (defun viper-escape-to-state (arg events state)
741 ;;(let (com key prefix-arg)
743 ;; this temporarily turns off Viper's minor mode keymaps
744 (viper-set-mode-vars-for state)
745 (viper-normalize-minor-mode-map-alist)
746 (if events (viper-set-unread-command-events events))
748 ;; protect against keyboard quit and other errors
750 (let (viper-vi-kbd-minor-mode
751 viper-insert-kbd-minor-mode
752 viper-emacs-kbd-minor-mode)
756 (key-binding (setq key (viper-read-key-sequence nil))))
757 ;; In case of binding indirection--chase definitions.
758 ;; Have to do it here because we execute this command under
759 ;; different keymaps, so command-execute may not do the
761 (while (vectorp com) (setq com (key-binding com))))
763 ;; Execute command com in the original Viper state, not in state
764 ;; `state'. Otherwise, if we switch buffers while executing the
765 ;; escaped to command, Viper's mode vars will remain those of
766 ;; `state'. When we return to the orig buffer, the bindings will be
768 (viper-set-mode-vars-for viper-current-state)
770 ;; this-command, last-command-char, last-command-event
771 (setq this-command com)
772 (if (featurep 'xemacs)
773 ;; XEmacs represents key sequences as vectors
774 (setq last-command-event
775 (viper-copy-event (viper-seq-last-elt key))
776 last-command-char (event-to-character last-command-event))
777 ;; Emacs represents them as sequences (str or vec)
778 (setq last-command-event
779 (viper-copy-event (viper-seq-last-elt key))))
782 ;; pretend that current state is the state we escaped to
783 (let ((viper-current-state state))
784 (setq prefix-arg (or prefix-arg arg))
785 (command-execute com)))
789 ;; set state in the new buffer
790 (viper-set-mode-vars-for viper-current-state))
792 ;; This is used in order to allow reading characters according to the input
793 ;; method. The character is read in emacs and inserted into the buffer.
794 ;; If an input method is in effect, this might
795 ;; cause several characters to be combined into one.
796 ;; Also takes care of the iso-accents mode
797 (defun viper-special-read-and-insert-char ()
798 (viper-set-mode-vars-for 'emacs-state)
799 (viper-normalize-minor-mode-map-alist)
800 (if viper-special-input-method
801 (viper-set-input-method t))
802 (if viper-automatic-iso-accents
803 (viper-set-iso-accents-mode t))
805 (let (viper-vi-kbd-minor-mode
806 viper-insert-kbd-minor-mode
807 viper-emacs-kbd-minor-mode
809 (cond ((and viper-special-input-method
811 (fboundp 'quail-input-method))
812 ;; (let ...) is used to restore unread-command-events to the
813 ;; original state. We don't want anything left in there after
814 ;; key translation. (Such left-overs are possible if the user
815 ;; types a regular key.)
816 (let (unread-command-events)
817 ;; The next cmd and viper-set-unread-command-events
818 ;; are intended to prevent the input method
819 ;; from swallowing ^M, ^Q and other special characters
820 (setq ch (read-char-exclusive))
821 ;; replace ^M with the newline
822 (if (eq ch ?\C-m) (setq ch ?\n))
823 ;; Make sure ^V and ^Q work as quotation chars
824 (if (memq ch '(?\C-v ?\C-q))
825 (setq ch (read-char-exclusive)))
826 (viper-set-unread-command-events ch)
827 (quail-input-method nil)
829 (if (and ch (string= quail-current-str ""))
831 (insert quail-current-str))
833 (aref quail-current-str
834 (1- (length quail-current-str)))))
836 ((and viper-special-input-method
838 (fboundp 'quail-start-translation))
839 ;; same as above but for XEmacs, which doesn't have
840 ;; quail-input-method
841 (let (unread-command-events)
842 (setq ch (read-char-exclusive))
843 ;; replace ^M with the newline
844 (if (eq ch ?\C-m) (setq ch ?\n))
845 ;; Make sure ^V and ^Q work as quotation chars
846 (if (memq ch '(?\C-v ?\C-q))
847 (setq ch (read-char-exclusive)))
848 (viper-set-unread-command-events ch)
849 (quail-start-translation nil)
851 (if (and ch (string= quail-current-str ""))
853 (insert quail-current-str))
855 (aref quail-current-str
856 (1- (length quail-current-str)))))
858 ((and (boundp 'iso-accents-mode) iso-accents-mode)
859 (setq ch (aref (read-key-sequence nil) 0))
860 ;; replace ^M with the newline
861 (if (eq ch ?\C-m) (setq ch ?\n))
862 ;; Make sure ^V and ^Q work as quotation chars
863 (if (memq ch '(?\C-v ?\C-q))
864 (setq ch (aref (read-key-sequence nil) 0)))
867 ;;(setq ch (read-char-exclusive))
868 (setq ch (aref (read-key-sequence nil) 0))
869 (if (featurep 'xemacs)
870 (setq ch (event-to-character ch)))
871 ;; replace ^M with the newline
872 (if (eq ch ?\C-m) (setq ch ?\n))
873 ;; Make sure ^V and ^Q work as quotation chars
874 (if (memq ch '(?\C-v ?\C-q))
876 ;;(setq ch (read-char-exclusive))
877 (setq ch (aref (read-key-sequence nil) 0))
878 (if (featurep 'xemacs)
879 (setq ch (event-to-character ch))))
883 (setq last-command-event
884 (viper-copy-event (if (featurep 'xemacs)
885 (character-to-event ch) ch)))
890 (viper-set-input-method nil)
891 (viper-set-iso-accents-mode nil)
892 (viper-set-mode-vars-for viper-current-state)
896 (defun viper-exec-form-in-vi (form)
897 "Execute FORM in Vi state, regardless of the current Vi state."
898 (let ((buff (current-buffer))
900 (viper-set-mode-vars-for 'vi-state)
903 (let (viper-vi-kbd-minor-mode) ; execute without kbd macros
904 (setq result (eval form)))
908 (if (not (equal buff (current-buffer))) ; cmd switched buffer
909 (with-current-buffer buff
910 (viper-set-mode-vars-for viper-current-state)))
911 (viper-set-mode-vars-for viper-current-state)
914 (defun viper-exec-form-in-emacs (form)
915 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
916 Similar to `viper-escape-to-emacs', but accepts forms rather than keystrokes."
917 (let ((buff (current-buffer))
919 (viper-set-mode-vars-for 'emacs-state)
920 (setq result (eval form))
921 (if (not (equal buff (current-buffer))) ; cmd switched buffer
922 (with-current-buffer buff
923 (viper-set-mode-vars-for viper-current-state)))
924 (viper-set-mode-vars-for viper-current-state)
927 ;; This executes the last kbd event in emacs mode. Is used when we want to
928 ;; interpret certain keys directly in emacs (as, for example, in comint mode).
929 (defun viper-exec-key-in-emacs (arg)
931 (viper-escape-to-emacs arg last-command-event))
934 ;; This is needed because minor modes sometimes override essential Viper
935 ;; bindings. By letting Viper know which files these modes are in, it will
936 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
937 (defun viper-harness-minor-mode (load-file)
938 "Familiarize Viper with a minor mode defined in LOAD-FILE.
939 Minor modes that have their own keymaps may overshadow Viper keymaps.
940 This function is designed to make Viper aware of the packages that define
943 (viper-harness-minor-mode load-file)
945 LOAD-FILE is the name of the file where the specific minor mode is defined.
946 Suffixes such as .el or .elc should be stripped."
948 (interactive "sEnter name of the load file: ")
950 (eval-after-load load-file '(viper-normalize-minor-mode-map-alist))
952 ;; Change the default for minor-mode-map-alist each time a harnessed minor
953 ;; mode adds its own keymap to the a-list.
955 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
957 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist)))
961 (defun viper-ESC (arg)
962 "Emulate ESC key in Emacs.
963 Prevents multiple escape keystrokes if viper-no-multiple-ESC is true.
964 If `viper-no-multiple-ESC' is `twice' double ESC would ding in vi-state.
965 Other ESC sequences are emulated via the current Emacs's major mode
966 keymap. This is more convenient on TTYs, since this won't block
967 function keys such as up, down, etc. ESC will also will also work as
968 a Meta key in this case. When `viper-no-multiple-ESC' is nil, ESC works
969 as a Meta key and any number of multiple escapes are allowed."
972 (cond ((and (not viper-no-multiple-ESC) (eq viper-current-state 'vi-state))
973 (setq char (viper-read-char-exclusive))
974 (viper-escape-to-emacs arg (list ?\e char) ))
975 ((and (eq viper-no-multiple-ESC 'twice)
976 (eq viper-current-state 'vi-state))
977 (setq char (viper-read-char-exclusive))
978 (if (= char (string-to-char viper-ESC-key))
980 (viper-escape-to-emacs arg (list ?\e char) )))
984 (defun viper-alternate-Meta-key (arg)
985 "Simulate Emacs Meta key."
987 (sit-for 1) (message "ESC-")
988 (viper-escape-to-emacs arg '(?\e)))
990 (defun viper-toggle-key-action ()
991 "Action bound to `viper-toggle-key'."
993 (if (and (< viper-expert-level 2) (equal viper-toggle-key "\C-z"))
994 (if (viper-window-display-p)
997 (viper-change-state-to-emacs)))
999 ;; Listen to ESC key.
1000 (defun viper-intercept-ESC-key ()
1001 "Function that implements ESC key in Viper emulation of Vi."
1003 ;; `key-binding' needs to be called in a context where Viper's
1004 ;; minor-mode map(s) have been temporarily disabled so the ESC
1005 ;; binding to viper-intercept-ESC-key doesn't hide the binding we're
1006 ;; looking for (Bug#9146):
1007 (let* ((cmd 'viper-intercept-ESC-key))
1009 ;; call the actual function to execute ESC (if no other symbols followed)
1010 ;; or the key bound to the ESC sequence (if the sequence was issued
1011 ;; with very short delay between characters).
1012 (if (eq cmd 'viper-intercept-ESC-key)
1014 (cond ((eq viper-current-state 'vi-state)
1016 ((eq viper-current-state 'insert-state)
1017 'viper-exit-insert-state)
1018 ((eq viper-current-state 'replace-state)
1019 'viper-replace-state-exit-cmd)
1020 (t 'viper-change-state-to-vi)
1022 (call-interactively cmd)))
1027 ;; prefix argument for Vi mode
1029 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
1030 ;; represents the numeric value of the prefix argument and COM represents
1031 ;; command prefix such as "c", "d", "m" and "y".
1033 ;; Get value part of prefix-argument ARG.
1034 (defsubst viper-p-val (arg)
1035 (cond ((null arg) 1)
1037 (if (or (null (car arg)) (equal (car arg) '(nil)))
1041 ;; Get raw value part of prefix-argument ARG.
1042 (defsubst viper-P-val (arg)
1043 (cond ((consp arg) (car arg))
1046 ;; Get com part of prefix-argument ARG.
1047 (defsubst viper-getcom (arg)
1048 (cond ((null arg) nil)
1049 ((consp arg) (cdr arg))
1052 ;; Get com part of prefix-argument ARG and modify it.
1053 (defun viper-getCom (arg)
1054 (let ((com (viper-getcom arg)))
1055 (cond ((viper= com ?c) ?c)
1056 ;; Previously, ?c was being converted to ?C, but this prevented
1057 ;; multiline replace regions.
1058 ;;((viper= com ?c) ?C)
1059 ((viper= com ?d) ?D)
1060 ((viper= com ?y) ?Y)
1064 ;; Compute numeric prefix arg value.
1065 ;; Invoked by EVENT-CHAR. COM is the command part obtained so far.
1066 (defun viper-prefix-arg-value (event-char com)
1067 (let ((viper-intermediate-command 'viper-digit-argument)
1069 ;; read while number
1070 (while (and (viper-characterp event-char)
1071 (>= event-char ?0) (<= event-char ?9))
1072 (setq value (+ (* (if (integerp value) value 0) 10) (- event-char ?0)))
1073 (setq event-char (viper-read-event-convert-to-char)))
1075 (setq prefix-arg value)
1076 (if com (setq prefix-arg (cons prefix-arg com)))
1077 (while (eq event-char ?U)
1078 (viper-describe-arg prefix-arg)
1079 (setq event-char (viper-read-event-convert-to-char)))
1081 (if (or com (and (not (eq viper-current-state 'vi-state))
1082 ;; make sure it is a Vi command
1083 (viper-characterp event-char)
1084 (viper-vi-command-p event-char)
1086 ;; If appears to be one of the vi commands,
1087 ;; then execute it with funcall and clear prefix-arg in order to not
1088 ;; confuse subsequent commands
1090 ;; last-command-event is the char we want emacs to think was typed
1091 ;; last. If com is not nil, the viper-digit-argument command was
1092 ;; called from within viper-prefix-arg command, such as `d', `w',
1093 ;; etc., i.e., the user typed, say, d2. In this case, `com' would be
1094 ;; `d', `w', etc. If viper-digit-argument was invoked by
1095 ;; viper-escape-to-vi (which is indicated by the fact that the
1096 ;; current state is not vi-state), then `event-char' represents the
1097 ;; vi command to be executed (e.g., `d', `w', etc). Again,
1098 ;; last-command-event must make emacs believe that this is the command
1100 (cond ((eq event-char 'return) (setq event-char ?\C-m))
1101 ((eq event-char 'delete) (setq event-char ?\C-?))
1102 ((eq event-char 'backspace) (setq event-char ?\C-h))
1103 ((eq event-char 'space) (setq event-char ?\ )))
1104 (setq last-command-event
1105 (if (featurep 'xemacs)
1106 (character-to-event (or com event-char))
1107 (or com event-char)))
1108 (setq func (viper-exec-form-in-vi
1109 `(key-binding (char-to-string ,event-char))))
1110 (funcall func prefix-arg)
1111 (setq prefix-arg nil))
1112 ;; some other command -- let emacs do it in its own way
1113 (viper-set-unread-command-events event-char))
1117 ;; Vi operator as prefix argument."
1118 (defun viper-prefix-arg-com (char value com)
1123 (viper-memq-char char
1124 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1125 viper-buffer-search-char)))
1127 ;; this means that we already have a command character, so we
1128 ;; construct a com list and exit while. however, if char is "
1131 ;; new com is (CHAR . OLDCOM)
1132 (if (viper-memq-char char '(?# ?\")) (error "Viper bell"))
1133 (setq com (cons char com))
1135 ;; If com is nil we set com as char, and read more. Again, if char is
1136 ;; ", we read the name of register and store it in viper-use-register.
1137 ;; if char is !, =, or #, a complete com is formed so we exit the while
1139 (cond ((viper-memq-char char '(?! ?=))
1141 (setq char (read-char))
1144 ;; read a char and encode it as com
1145 (setq com (+ 128 (read-char)))
1146 (setq char (read-char)))
1148 (let ((reg (read-char)))
1149 (if (viper-valid-register reg)
1150 (setq viper-use-register reg)
1151 (error "Viper bell"))
1152 (setq char (read-char))))
1155 (setq char (read-char))))))
1158 ;; `com' is a single char, so we construct the command argument
1159 ;; and if `char' is `?', we describe the arg; otherwise
1160 ;; we prepare the command that will be executed at the end.
1162 (setq cmd-info (cons value com))
1163 (while (viper= char ?U)
1164 (viper-describe-arg cmd-info)
1165 (setq char (read-char)))
1166 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so
1167 ;; we execute it at the very end
1168 (or (viper-movement-command-p char)
1169 (viper-digit-command-p char)
1170 (viper-regsuffix-command-p char)
1171 (viper= char ?!) ; bang command
1172 (viper= char ?g) ; the gg command (like G0)
1173 (error "Viper bell"))
1174 (setq cmd-to-exec-at-end
1175 (viper-exec-form-in-vi
1176 `(key-binding (char-to-string ,char)))))
1178 ;; as com is non-nil, this means that we have a command to execute
1179 (if (viper-memq-char (car com) '(?r ?R))
1180 ;; execute appropriate region command.
1181 (let ((char (car com)) (com (cdr com)))
1182 (setq prefix-arg (cons value com))
1183 (if (viper= char ?r)
1184 (viper-region prefix-arg)
1185 (viper-Region prefix-arg))
1187 (setq prefix-arg nil))
1188 ;; otherwise, reset prefix arg and call appropriate command
1189 (setq value (if (null value) 1 value))
1190 (setq prefix-arg nil)
1192 ;; If we change ?C to ?c here, then cc will enter replacement mode
1193 ;; rather than deleting lines. However, it will affect 1 less line
1194 ;; than normal. We decided to not use replacement mode here and
1195 ;; follow Vi, since replacement mode on n full lines can be achieved
1197 ((equal com '(?c . ?c)) (viper-line (cons value ?C)))
1198 ((equal com '(?d . ?d)) (viper-line (cons value ?D)))
1199 ((equal com '(?d . ?y)) (viper-yank-defun))
1200 ((equal com '(?y . ?y)) (viper-line (cons value ?Y)))
1201 ((equal com '(?< . ?<)) (viper-line (cons value ?<)))
1202 ((equal com '(?> . ?>)) (viper-line (cons value ?>)))
1203 ((equal com '(?! . ?!)) (viper-line (cons value ?!)))
1204 ((equal com '(?= . ?=)) (viper-line (cons value ?=)))
1206 ((equal (car com) ?g) (viper-goto-line 0))
1207 (t (error "Viper bell")))))
1209 (if cmd-to-exec-at-end
1211 (setq last-command-event
1213 (if (featurep 'xemacs) (character-to-event char) char)))
1215 (funcall cmd-to-exec-at-end cmd-info)
1217 (error "%s" (error-message-string err))))))
1220 (defun viper-describe-arg (arg)
1222 (setq val (viper-P-val arg)
1223 com (viper-getcom arg))
1226 (message "Value is nil, and command is nil")
1227 (message "Value is nil, and command is `%c'" com))
1229 (message "Value is `%d', and command is nil" val)
1230 (message "Value is `%d', and command is `%c'" val com)))))
1232 (defun viper-digit-argument (arg)
1233 "Begin numeric argument for the next command."
1235 (viper-leave-region-active)
1236 (viper-prefix-arg-value
1237 (viper-last-command-char) (if (consp arg) (cdr arg) nil)))
1239 (defun viper-command-argument (arg)
1240 "Accept a motion command as an argument."
1242 (let ((viper-intermediate-command 'viper-command-argument))
1244 (viper-prefix-arg-com
1245 (viper-last-command-char)
1246 (cond ((null arg) nil)
1247 ((consp arg) (car arg))
1248 ((integerp arg) arg)
1249 (t (error viper-InvalidCommandArgument)))
1250 (cond ((null arg) nil)
1251 ((consp arg) (cdr arg))
1252 ((integerp arg) nil)
1253 (t (error viper-InvalidCommandArgument))))
1254 (quit (setq viper-use-register nil)
1255 (signal 'quit nil)))
1256 (viper-deactivate-mark)))
1259 ;; repeat last destructive command
1261 ;; Append region to text in register REG.
1262 ;; START and END are buffer positions indicating what to append.
1263 (defsubst viper-append-to-register (reg start end)
1264 (set-register reg (concat (if (stringp (get-register reg))
1265 (get-register reg) "")
1266 (buffer-substring start end))))
1268 ;; Saves last inserted text for possible use by viper-repeat command.
1269 (defun viper-save-last-insertion (beg end)
1271 (setq viper-last-insertion (buffer-substring beg end))
1273 ;; beg or end marker are somehow screwed up
1274 (setq viper-last-insertion nil)))
1275 (setq viper-last-insertion (buffer-substring beg end))
1276 (or (< (length viper-d-com) 5)
1277 (setcar (nthcdr 4 viper-d-com) viper-last-insertion))
1278 (or (null viper-command-ring)
1279 (ring-empty-p viper-command-ring)
1281 (setcar (nthcdr 4 (viper-current-ring-item viper-command-ring))
1282 viper-last-insertion)
1283 ;; del most recent elt, if identical to the second most-recent
1284 (viper-cleanup-ring viper-command-ring)))
1287 (defsubst viper-yank-last-insertion ()
1288 "Inserts the text saved by the previous viper-save-last-insertion command."
1290 (insert viper-last-insertion)
1294 ;; define functions to be executed
1296 ;; invoked by the `C' command
1297 (defun viper-exec-change (m-com com)
1298 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1299 (set-marker viper-com-point (point) (current-buffer)))
1300 ;; handle C cmd at the eol and at eob.
1301 (if (or (and (eolp) (= viper-com-point (point)))
1302 (= viper-com-point (point-max)))
1304 (insert " ")(backward-char 1)))
1305 (if (= viper-com-point (point))
1306 (viper-forward-char-carefully))
1307 (push-mark viper-com-point)
1308 (if (eq m-com 'viper-next-line-at-bol)
1309 (viper-enlarge-region (mark t) (point)))
1310 (if (< (point) (mark t))
1311 (exchange-point-and-mark))
1312 (if (eq (preceding-char) ?\n)
1313 (viper-backward-char-carefully)) ; give back the newline
1314 (if (eq viper-intermediate-command 'viper-repeat)
1315 (viper-change-subr (mark t) (point))
1316 (viper-change (mark t) (point))))
1318 ;; this is invoked by viper-substitute-line
1319 (defun viper-exec-Change (m-com com)
1321 (set-mark viper-com-point)
1322 (viper-enlarge-region (mark t) (point))
1323 (if viper-use-register
1325 (cond ((viper-valid-register viper-use-register '(letter digit))
1327 viper-use-register (mark t) (point) nil))
1328 ((viper-valid-register viper-use-register '(Letter))
1329 (viper-append-to-register
1330 (downcase viper-use-register) (mark t) (point)))
1331 (t (setq viper-use-register nil)
1332 (error viper-InvalidRegister viper-use-register)))
1333 (setq viper-use-register nil)))
1334 (delete-region (mark t) (point)))
1336 (if (eq viper-intermediate-command 'viper-repeat)
1337 (viper-yank-last-insertion)
1338 (viper-change-state-to-insert)
1341 (defun viper-exec-delete (m-com com)
1342 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1343 (set-marker viper-com-point (point) (current-buffer)))
1344 (let (chars-deleted)
1345 (if viper-use-register
1347 (cond ((viper-valid-register viper-use-register '(letter digit))
1349 viper-use-register viper-com-point (point) nil))
1350 ((viper-valid-register viper-use-register '(Letter))
1351 (viper-append-to-register
1352 (downcase viper-use-register) viper-com-point (point)))
1353 (t (setq viper-use-register nil)
1354 (error viper-InvalidRegister viper-use-register)))
1355 (setq viper-use-register nil)))
1357 (if (eq last-command 'd-command) 'kill-region nil))
1358 (setq chars-deleted (abs (- (point) viper-com-point)))
1359 (if (> chars-deleted viper-change-notification-threshold)
1360 (unless (viper-is-in-minibuffer)
1361 (message "Deleted %d characters" chars-deleted)))
1362 (kill-region viper-com-point (point))
1363 (setq this-command 'd-command)
1364 (if viper-ex-style-motion
1365 (if (and (eolp) (not (bolp))) (backward-char 1)))))
1367 (defun viper-exec-Delete (m-com com)
1369 (set-mark viper-com-point)
1370 (viper-enlarge-region (mark t) (point))
1371 (let (lines-deleted)
1372 (if viper-use-register
1374 (cond ((viper-valid-register viper-use-register '(letter digit))
1376 viper-use-register (mark t) (point) nil))
1377 ((viper-valid-register viper-use-register '(Letter))
1378 (viper-append-to-register
1379 (downcase viper-use-register) (mark t) (point)))
1380 (t (setq viper-use-register nil)
1381 (error viper-InvalidRegister viper-use-register)))
1382 (setq viper-use-register nil)))
1384 (if (eq last-command 'D-command) 'kill-region nil))
1385 (setq lines-deleted (count-lines (point) viper-com-point))
1386 (if (> lines-deleted viper-change-notification-threshold)
1387 (unless (viper-is-in-minibuffer)
1388 (message "Deleted %d lines" lines-deleted)))
1389 (kill-region (mark t) (point))
1390 (if (eq m-com 'viper-line) (setq this-command 'D-command)))
1391 (back-to-indentation)))
1394 (defun viper-exec-yank (m-com com)
1395 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1396 (set-marker viper-com-point (point) (current-buffer)))
1398 (if viper-use-register
1400 (cond ((viper-valid-register viper-use-register '(letter digit))
1402 viper-use-register viper-com-point (point) nil))
1403 ((viper-valid-register viper-use-register '(Letter))
1404 (viper-append-to-register
1405 (downcase viper-use-register) viper-com-point (point)))
1406 (t (setq viper-use-register nil)
1407 (error viper-InvalidRegister viper-use-register)))
1408 (setq viper-use-register nil)))
1409 (setq last-command nil)
1410 (copy-region-as-kill viper-com-point (point))
1411 (setq chars-saved (abs (- (point) viper-com-point)))
1412 (if (> chars-saved viper-change-notification-threshold)
1413 (unless (viper-is-in-minibuffer)
1414 (message "Saved %d characters" chars-saved)))
1415 (goto-char viper-com-point)))
1418 (defun viper-exec-Yank (m-com com)
1420 (set-mark viper-com-point)
1421 (viper-enlarge-region (mark t) (point))
1423 (if viper-use-register
1425 (cond ((viper-valid-register viper-use-register '(letter digit))
1427 viper-use-register (mark t) (point) nil))
1428 ((viper-valid-register viper-use-register '(Letter))
1429 (viper-append-to-register
1430 (downcase viper-use-register) (mark t) (point)))
1431 (t (setq viper-use-register nil)
1432 (error viper-InvalidRegister viper-use-register)))
1433 (setq viper-use-register nil)))
1434 (setq last-command nil)
1435 (copy-region-as-kill (mark t) (point))
1436 (setq lines-saved (count-lines (mark t) (point)))
1437 (if (> lines-saved viper-change-notification-threshold)
1438 (unless (viper-is-in-minibuffer)
1439 (message "Saved %d lines" lines-saved)))))
1440 (viper-deactivate-mark)
1441 (goto-char viper-com-point))
1443 (defun viper-exec-bang (m-com com)
1445 (set-mark viper-com-point)
1446 (viper-enlarge-region (mark t) (point))
1447 (exchange-point-and-mark)
1448 (shell-command-on-region
1451 (setq viper-last-shell-com
1452 (viper-read-string-with-history
1455 'viper-shell-history
1456 (car viper-shell-history)
1458 viper-last-shell-com)
1461 (defun viper-exec-equals (m-com com)
1463 (set-mark viper-com-point)
1464 (viper-enlarge-region (mark t) (point))
1465 (if (> (mark t) (point)) (exchange-point-and-mark))
1466 (indent-region (mark t) (point) nil)))
1468 (defun viper-exec-shift (m-com com)
1470 (set-mark viper-com-point)
1471 (viper-enlarge-region (mark t) (point))
1472 (if (> (mark t) (point)) (exchange-point-and-mark))
1473 (indent-rigidly (mark t) (point)
1476 (- viper-shift-width))))
1477 ;; return point to where it was before shift
1478 (goto-char viper-com-point))
1480 ;; this is needed because some commands fake com by setting it to ?r, which
1481 ;; denotes repeated insert command.
1482 (defsubst viper-exec-dummy (m-com com)
1485 (defun viper-exec-buffer-search (m-com com)
1486 (setq viper-s-string
1487 (regexp-quote (buffer-substring (point) viper-com-point)))
1488 (setq viper-s-forward t)
1489 (setq viper-search-history (cons viper-s-string viper-search-history))
1490 (setq viper-intermediate-command 'viper-exec-buffer-search)
1491 (viper-search viper-s-string viper-s-forward 1))
1493 (defvar viper-exec-array (make-vector 128 nil))
1495 ;; Using a dispatch array allows adding functions like buffer search
1496 ;; without affecting other functions. Buffer search can now be bound
1497 ;; to any character.
1499 (aset viper-exec-array ?c 'viper-exec-change)
1500 (aset viper-exec-array ?C 'viper-exec-Change)
1501 (aset viper-exec-array ?d 'viper-exec-delete)
1502 (aset viper-exec-array ?D 'viper-exec-Delete)
1503 (aset viper-exec-array ?y 'viper-exec-yank)
1504 (aset viper-exec-array ?Y 'viper-exec-Yank)
1505 (aset viper-exec-array ?r 'viper-exec-dummy)
1506 (aset viper-exec-array ?! 'viper-exec-bang)
1507 (aset viper-exec-array ?< 'viper-exec-shift)
1508 (aset viper-exec-array ?> 'viper-exec-shift)
1509 (aset viper-exec-array ?= 'viper-exec-equals)
1513 ;; This function is called by various movement commands to execute a
1514 ;; destructive command on the region specified by the movement command. For
1515 ;; instance, if the user types cw, then the command viper-forward-word will
1516 ;; call viper-execute-com to execute viper-exec-change, which eventually will
1517 ;; call viper-change to invoke the replace mode on the region.
1519 ;; The var viper-d-com is set to (M-COM VAL COM REG INSERTED-TEXT COMMAND-KEYS)
1520 ;; via a call to viper-set-destructive-command, for later use by viper-repeat.
1521 (defun viper-execute-com (m-com val com)
1522 (let ((reg viper-use-register))
1523 ;; this is the special command `#'
1525 (viper-special-prefix-com (- com 128))
1526 (let ((fn (aref viper-exec-array com)))
1528 (error "%c: %s" com viper-InvalidViCommand)
1529 (funcall fn m-com com))))
1530 (if (viper-dotable-command-p com)
1531 (viper-set-destructive-command
1532 (list m-com val com reg nil nil)))
1536 (defun viper-repeat (arg)
1537 "Re-execute last destructive command.
1538 Use the info in viper-d-com, which has the form
1539 \(com val ch reg inserted-text command-keys),
1540 where `com' is the command to be re-executed, `val' is the
1541 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1542 if it exists, it is the name of the register for `com'.
1543 If the prefix argument ARG is non-nil, it is used instead of `val'."
1545 (let ((save-point (point)) ; save point before repeating prev cmd
1546 ;; Pass along that we are repeating a destructive command
1547 ;; This tells viper-set-destructive-command not to update
1548 ;; viper-command-ring
1549 (viper-intermediate-command 'viper-repeat))
1550 (if (eq last-command 'viper-undo)
1551 ;; if the last command was viper-undo, then undo-more
1553 ;; otherwise execute the command stored in viper-d-com. if arg is
1554 ;; non-nil its prefix value is used as new prefix value for the command.
1555 (let ((m-com (car viper-d-com))
1556 (val (viper-P-val arg))
1557 (com (nth 2 viper-d-com))
1558 (reg (nth 3 viper-d-com)))
1559 (if (null val) (setq val (nth 1 viper-d-com)))
1560 (if (null m-com) (error "No previous command to repeat"))
1561 (setq viper-use-register reg)
1562 (if (nth 4 viper-d-com) ; text inserted by command
1563 (setq viper-last-insertion (nth 4 viper-d-com)
1564 viper-d-char (nth 4 viper-d-com)))
1565 (funcall m-com (cons val com))
1566 (cond ((and (< save-point (point)) viper-keep-point-on-repeat)
1567 (goto-char save-point)) ; go back to before repeat.
1568 ((and (< save-point (point)) viper-ex-style-editing)
1569 (or (bolp) (backward-char 1))))
1570 (if (and (eolp) (not (bolp)))
1573 (viper-complete-complex-command-for-undo) ; take care of undo
1574 ;; If the prev cmd was rotating the command ring, this means that `.' has
1575 ;; just executed a command from that ring. So, push it on the ring again.
1576 ;; If we are just executing previous command , then don't push viper-d-com
1577 ;; because viper-d-com is not fully constructed in this case (its keys and
1578 ;; the inserted text may be nil). Besides, in this case, the command
1579 ;; executed by `.' is already on the ring.
1580 (if (eq last-command 'viper-display-current-destructive-command)
1581 (viper-push-onto-ring viper-d-com 'viper-command-ring))
1582 (viper-deactivate-mark)
1585 (defun viper-repeat-from-history ()
1586 "Repeat a destructive command from history.
1587 Doesn't change viper-command-ring in any way, so `.' will work as before
1588 executing this command.
1589 This command is supposed to be bound to a two-character Vi macro where
1590 the second character is a digit 0 to 9. The digit indicates which
1591 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1592 invokes the command before that, etc."
1594 (let* ((viper-intermediate-command 'repeating-display-destructive-command)
1595 (idx (cond (viper-this-kbd-macro
1597 (symbol-name (elt viper-this-kbd-macro 1))))
1600 (viper-d-com viper-d-com))
1602 (or (and (numberp num) (<= 0 num) (<= num 9))
1607 "`viper-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1609 (setq viper-d-com (viper-special-ring-rotate1 viper-command-ring -1))
1610 (setq num (1- num)))
1613 (viper-special-ring-rotate1 viper-command-ring 1)
1614 (setq num (1+ num)))
1618 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1619 ;; The chars that can follow `#' are determined by viper-hash-command-p
1620 (defun viper-special-prefix-com (char)
1621 (cond ((viper= char ?c)
1622 (downcase-region (min viper-com-point (point))
1623 (max viper-com-point (point))))
1625 (upcase-region (min viper-com-point (point))
1626 (max viper-com-point (point))))
1628 (push-mark viper-com-point t)
1629 ;; execute the last emacs kbd macro on each line of the region
1630 (viper-global-execute))
1632 (push-mark viper-com-point t)
1633 (viper-quote-region))
1635 (funcall viper-spell-function viper-com-point (point)))
1636 (t (error "#%c: %s" char viper-InvalidViCommand))))
1641 ;; hook used inside undo
1642 (defvar viper-undo-functions nil)
1644 ;; Runs viper-before-change-functions inside before-change-functions
1645 (defun viper-undo-sentinel (beg end length)
1646 (run-hook-with-args 'viper-undo-functions beg end length))
1648 (add-hook 'after-change-functions 'viper-undo-sentinel)
1650 ;; Hook used in viper-undo
1651 (defun viper-after-change-undo-hook (beg end len)
1652 (if (and (boundp 'undo-in-progress) undo-in-progress)
1653 (setq undo-beg-posn beg
1654 undo-end-posn (or end beg))
1655 ;; some other hooks may be changing various text properties in
1656 ;; the buffer in response to 'undo'; so remove this hook to avoid
1657 ;; its repeated invocation
1658 (remove-hook 'viper-undo-functions 'viper-after-change-undo-hook 'local)
1661 (defun viper-undo ()
1662 "Undo previous change."
1665 (let ((modified (buffer-modified-p))
1666 (before-undo-pt (point-marker))
1667 undo-beg-posn undo-end-posn)
1669 ;; the viper-after-change-undo-hook removes itself after the 1st invocation
1670 (add-hook 'viper-undo-functions 'viper-after-change-undo-hook nil 'local)
1674 (viper-complete-complex-command-for-undo)
1675 ;;(setq undo-beg-posn (or undo-beg-posn (point))
1676 ;; undo-end-posn (or undo-end-posn (point)))
1677 ;;(setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1678 ;; undo-end-posn (or undo-end-posn undo-beg-posn))
1680 (if (and undo-beg-posn undo-end-posn)
1682 (goto-char undo-beg-posn)
1684 (if (and viper-keep-point-on-undo
1685 (pos-visible-in-window-p before-undo-pt))
1687 (push-mark (point-marker) t)
1688 (viper-sit-for-short 300)
1689 (goto-char undo-end-posn)
1690 (viper-sit-for-short 300)
1691 (if (pos-visible-in-window-p undo-beg-posn)
1692 (goto-char before-undo-pt)
1693 (goto-char undo-beg-posn)))
1694 (push-mark before-undo-pt t))
1697 (if (and (eolp) (not (bolp))) (backward-char 1))
1699 (setq this-command 'viper-undo))
1701 ;; Continue undoing previous changes.
1702 (defun viper-undo-more ()
1703 (message "undo more!")
1707 (message "No further undo information in this buffer")))
1708 (if (and (eolp) (not (bolp))) (backward-char 1))
1709 (setq this-command 'viper-undo))
1711 ;; The following two functions are used to set up undo properly.
1712 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1713 ;; they are undone all at once.
1714 (defun viper-complete-complex-command-for-undo ()
1715 (setq undo-auto-disable-boundaries nil)
1716 (setq viper-undo-in-complex-command nil)
1720 (defun viper-set-complex-command-for-undo ()
1721 (when (not viper-undo-in-complex-command)
1722 (setq undo-auto-disable-boundaries t)
1723 (setq viper-undo-in-complex-command t)
1726 ;;; Viper's destructive Command ring utilities
1728 (defun viper-display-current-destructive-command ()
1729 (let ((text (nth 4 viper-d-com))
1730 (keys (nth 5 viper-d-com))
1733 (setq this-command 'viper-display-current-destructive-command)
1735 (message " `.' runs `%s'%s"
1736 (viper-array-to-string keys)
1737 (viper-abbreviate-string
1738 (if (featurep 'xemacs)
1739 (replace-in-string ; xemacs
1740 (cond ((characterp text) (char-to-string text))
1741 ((stringp text) text)
1747 (format-message " inserting `") (format-message "'")
1752 ;; don't change viper-d-com if it was viper-repeat command invoked with `.'
1753 ;; or in some other way (non-interactively).
1754 (defun viper-set-destructive-command (list)
1755 (or (eq viper-intermediate-command 'viper-repeat)
1757 (setq viper-d-com list)
1758 (setcar (nthcdr 5 viper-d-com)
1759 (viper-array-to-string (if (arrayp viper-this-command-keys)
1760 viper-this-command-keys
1761 (this-command-keys))))
1762 (viper-push-onto-ring viper-d-com 'viper-command-ring)))
1763 (setq viper-this-command-keys nil))
1766 (defun viper-prev-destructive-command (next)
1767 "Find previous destructive command in the history of destructive commands.
1768 With prefix argument, find next destructive command."
1770 (let (cmd viper-intermediate-command)
1771 (if (eq last-command 'viper-display-current-destructive-command)
1772 ;; repeated search through command history
1773 (setq viper-intermediate-command
1774 'repeating-display-destructive-command)
1775 ;; first search through command history--set temp ring
1776 (setq viper-temp-command-ring (ring-copy viper-command-ring)))
1778 (viper-special-ring-rotate1 viper-temp-command-ring 1)
1779 (viper-special-ring-rotate1 viper-temp-command-ring -1)))
1782 (setq viper-d-com cmd))
1783 (viper-display-current-destructive-command)))
1786 (defun viper-next-destructive-command ()
1787 "Find next destructive command in the history of destructive commands."
1789 (viper-prev-destructive-command 'next))
1792 (defun viper-insert-prev-from-insertion-ring (arg)
1793 "Cycle through insertion ring in the direction of older insertions.
1794 Undoes previous insertion and inserts new.
1795 With prefix argument, cycles in the direction of newer elements.
1796 In minibuffer, this command executes whatever the invocation key is bound
1797 to in the global map, instead of cycling through the insertion ring."
1799 (let (viper-intermediate-command)
1800 (if (eq last-command 'viper-insert-from-insertion-ring)
1801 (progn ; repeated search through insertion history
1802 (setq viper-intermediate-command 'repeating-insertion-from-ring)
1803 (if (eq viper-current-state 'replace-state)
1805 (if viper-last-inserted-string-from-insertion-ring
1806 (backward-delete-char
1807 (length viper-last-inserted-string-from-insertion-ring))))
1809 ;;first search through insertion history
1810 (setq viper-temp-insertion-ring (ring-copy viper-insertion-ring)))
1811 (setq this-command 'viper-insert-from-insertion-ring)
1812 ;; so that things will be undone properly
1813 (setq buffer-undo-list (cons nil buffer-undo-list))
1814 (setq viper-last-inserted-string-from-insertion-ring
1815 (viper-special-ring-rotate1 viper-temp-insertion-ring (if arg 1 -1)))
1817 ;; this change of viper-intermediate-command must come after
1818 ;; viper-special-ring-rotate1, so that the ring will rotate, but before the
1820 (setq viper-intermediate-command nil)
1821 (if viper-last-inserted-string-from-insertion-ring
1822 (insert viper-last-inserted-string-from-insertion-ring))
1825 (defun viper-insert-next-from-insertion-ring ()
1826 "Cycle through insertion ring in the direction of older insertions.
1827 Undo previous insertion and inserts new."
1829 (viper-insert-prev-from-insertion-ring 'next))
1833 ;; some region utilities
1835 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1836 (defun viper-add-newline-at-eob-if-necessary ()
1839 ;; make sure all lines end with newline, unless in the minibuffer or
1840 ;; when requested otherwise (require-final-newline is nil)
1845 require-final-newline
1846 ;; add newline only if we actually edited buffer. otherwise it
1847 ;; might unintentionally modify binary buffers
1849 (not (viper-is-in-minibuffer))
1850 (not buffer-read-only))
1851 ;; text property may be read-only
1858 (defun viper-yank-defun ()
1860 (copy-region-as-kill (point) (mark t)))
1862 ;; Enlarge region between BEG and END.
1863 (defun viper-enlarge-region (beg end)
1864 (or beg (setq beg end)) ; if beg is nil, set to end
1865 (or end (setq end beg)) ; if end is nil, set to beg
1868 (progn (goto-char beg) (set-mark end))
1872 (exchange-point-and-mark)
1873 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1874 (if (not (eobp)) (beginning-of-line))
1875 (if (> beg end) (exchange-point-and-mark)))
1878 ;; Quote region by each line with a user supplied string.
1879 (defun viper-quote-region ()
1880 (let ((quote-str viper-quote-string)
1881 (do-not-change-default t))
1883 (viper-read-string-with-history
1886 'viper-quote-region-history
1887 (cond ((string-match "tex.*-mode" (symbol-name major-mode)) "%%")
1888 ((string-match "java.*-mode" (symbol-name major-mode)) "//")
1889 ((string-match "perl.*-mode" (symbol-name major-mode)) "#")
1890 ((string-match "lisp.*-mode" (symbol-name major-mode)) ";;")
1891 ((memq major-mode '(c-mode cc-mode c++-mode)) "//")
1892 ((memq major-mode '(sh-mode shell-mode)) "#")
1893 (t (setq do-not-change-default nil)
1895 (or do-not-change-default
1896 (setq viper-quote-string quote-str))
1897 (viper-enlarge-region (point) (mark t))
1898 (if (> (point) (mark t)) (exchange-point-and-mark))
1902 (while (and (< (point) (mark t)) (bolp))
1907 ;; Tells whether BEG is on the same line as END.
1908 ;; If one of the args is nil, it'll return nil.
1909 (defun viper-same-line (beg end)
1910 (let ((selective-display nil)
1913 (if (and beg end (> beg end))
1918 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
1921 ;; This 'if' is needed because Emacs treats the next empty line
1922 ;; as part of the previous line.
1923 (if (= (viper-line-pos 'start) end)
1925 (<= (+ incr (count-lines beg end)) 1))))
1929 ;; Check if the string ends with a newline.
1930 (defun viper-end-with-a-newline-p (string)
1931 (or (string= string "")
1932 (= (viper-seq-last-elt string) ?\n)))
1934 (defun viper-tmp-insert-at-eob (msg)
1935 (let ((savemax (point-max)))
1939 (goto-char savemax) (delete-region (point) (point-max))
1944 ;;; Minibuffer business
1946 (defsubst viper-set-minibuffer-style ()
1947 (add-hook 'minibuffer-setup-hook 'viper-minibuffer-setup-sentinel)
1948 (add-hook 'post-command-hook 'viper-minibuffer-post-command-hook))
1951 (defun viper-minibuffer-setup-sentinel ()
1952 (let ((hook (if viper-vi-style-in-minibuffer
1953 'viper-change-state-to-insert
1954 'viper-change-state-to-emacs)))
1955 ;; making buffer-local variables so that normal buffers won't affect the
1956 ;; minibuffer and vice versa. Otherwise, command arguments will affect
1957 ;; minibuffer ops and insertions from the minibuffer will change those in
1958 ;; the normal buffers
1959 (make-local-variable 'viper-d-com)
1960 (make-local-variable 'viper-last-insertion)
1961 (make-local-variable 'viper-command-ring)
1962 (setq viper-d-com nil
1963 viper-last-insertion nil
1964 viper-command-ring nil)
1968 ;; This is a temp hook that uses free variables init-message and viper-initial.
1969 ;; A dirty feature, but it is the simplest way to have it do the right thing.
1970 ;; The INIT-MESSAGE and VIPER-INITIAL vars come from the scope set by
1971 ;; viper-read-string-with-history
1972 (defun viper-minibuffer-standard-hook ()
1973 (if (stringp init-message)
1974 (viper-tmp-insert-at-eob init-message))
1975 (when (stringp viper-initial)
1976 ;; don't wait if we have unread events or in kbd macro
1977 (or unread-command-events
1980 (if (fboundp 'minibuffer-prompt-end)
1981 (delete-region (minibuffer-prompt-end) (point-max))
1983 (insert viper-initial)))
1985 (defsubst viper-minibuffer-real-start ()
1986 (if (fboundp 'minibuffer-prompt-end)
1987 (minibuffer-prompt-end)
1990 (defun viper-minibuffer-post-command-hook()
1991 (when (active-minibuffer-window)
1992 (when (< (point) (viper-minibuffer-real-start))
1993 (goto-char (viper-minibuffer-real-start)))))
1996 ;; Interpret last event in the local map first; if fails, use exit-minibuffer.
1997 ;; Run viper-minibuffer-exit-hook before exiting.
1998 (defun viper-exit-minibuffer ()
1999 "Exit minibuffer Viper way."
2002 (setq command (local-key-binding (char-to-string (viper-last-command-char))))
2003 (run-hooks 'viper-minibuffer-exit-hook)
2005 (command-execute command)
2006 (exit-minibuffer))))
2009 (defcustom viper-smart-suffix-list
2010 '("" "tex" "c" "cc" "C" "java" "el" "html" "htm" "xml"
2011 "pl" "flr" "P" "p" "h" "H")
2012 "List of suffixes that Viper tries to append to filenames ending with a `.'.
2013 This is useful when the current directory contains files with the same
2014 prefix and many different suffixes. Usually, only one of the suffixes
2015 represents an editable file. However, file completion will stop at the `.'
2016 The smart suffix feature lets you hit RET in such a case, and Viper will
2017 select the appropriate suffix.
2019 Suffixes are tried in the order given and the first suffix for which a
2020 corresponding file exists is selected. If no file exists for any of the
2021 suffixes, the user is asked to confirm.
2023 To turn this feature off, set this variable to nil."
2024 :type '(repeat string)
2028 ;; Try to add a suitable suffix to files whose name ends with a `.'
2029 ;; Useful when the user hits RET on a non-completed file name.
2030 ;; Used as a minibuffer exit hook in read-file-name
2031 (defun viper-file-add-suffix ()
2033 (len (length viper-smart-suffix-list))
2034 (file (buffer-substring-no-properties
2035 (viper-minibuffer-real-start) (point-max)))
2037 (goto-char (point-max))
2038 (if (and viper-smart-suffix-list (string-match "\\.$" file))
2040 (while (and (not found) (< count len))
2041 (setq suff (nth count viper-smart-suffix-list)
2044 (format "%s%s" (substitute-in-file-name file) suff))
2051 (viper-tmp-insert-at-eob " [Please complete file name]")
2053 (while (not (memq cmd
2054 '(exit-minibuffer viper-exit-minibuffer)))
2056 (key-binding (setq key (read-key-sequence nil))))
2057 (cond ((eq cmd 'self-insert-command)
2058 (if (featurep 'xemacs)
2059 (insert (events-to-keys key)) ; xemacs
2060 (insert key) ; emacs
2062 ((memq cmd '(exit-minibuffer viper-exit-minibuffer))
2064 (t (command-execute cmd)))
2069 (defun viper-minibuffer-trim-tail ()
2070 "Delete junk at the end of the first line of the minibuffer input.
2071 Remove this function from `viper-minibuffer-exit-hook', if this causes
2073 (if (viper-is-in-minibuffer)
2074 (let ((inhibit-field-text-motion t))
2075 (goto-char (viper-minibuffer-real-start))
2077 (delete-region (point) (point-max)))))
2080 ;;; Reading string with history
2082 (defun viper-read-string-with-history (prompt &optional viper-initial
2083 history-var default keymap
2085 ;; Read string, prompting with PROMPT and inserting the VIPER-INITIAL
2086 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2087 ;; input is an empty string.
2088 ;; Default value is displayed until the user types something in the
2090 ;; KEYMAP is used, if given, instead of minibuffer-local-map.
2091 ;; INIT-MESSAGE is the message temporarily displayed after entering the
2093 (let ((minibuffer-setup-hook
2094 ;; stolen from add-hook
2096 (if (boundp 'minibuffer-setup-hook)
2097 minibuffer-setup-hook
2100 'viper-minibuffer-standard-hook
2101 (if (or (not (listp old)) (eq (car old) 'lambda))
2107 (setq keymap (or keymap minibuffer-local-map)
2108 viper-initial (or viper-initial "")
2109 temp-msg (if default
2110 (format "(default %s) " default)
2113 (setq viper-incomplete-ex-cmd nil)
2114 (setq val (read-from-minibuffer prompt
2115 (concat temp-msg viper-initial val padding)
2116 keymap nil history-var))
2117 (setq minibuffer-setup-hook nil
2118 padding (viper-array-to-string (this-command-keys))
2120 ;; the following tries to be smart about what to put in history
2121 (if (not (string= val (car (eval history-var))))
2122 (set history-var (cons val (eval history-var))))
2123 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2124 (string= (nth 0 (eval history-var)) ""))
2125 (set history-var (cdr (eval history-var))))
2126 ;; If the user enters nothing but the prev cmd wasn't viper-ex,
2127 ;; viper-command-argument, or `! shell-command', this probably means
2128 ;; that the user typed something then erased. Return "" in this case, not
2129 ;; the default---the default is too confusing in this case.
2130 (cond ((and (string= val "")
2131 (not (string= prompt "!")) ; was a `! shell-command'
2132 (not (memq last-command
2134 viper-command-argument
2138 ((string= val "") (or default ""))
2144 ;; insertion commands
2146 ;; Called when state changes from Insert Vi command mode.
2147 ;; Repeats the insertion command if Insert state was entered with prefix
2149 (defun viper-repeat-insert-command ()
2150 (let ((i-com (car viper-d-com))
2151 (val (nth 1 viper-d-com))
2152 (char (nth 2 viper-d-com)))
2153 (if (and val (> val 1)) ; first check that val is non-nil
2155 (setq viper-d-com (list i-com (1- val) ?r nil nil nil))
2157 (setq viper-d-com (list i-com val char nil nil nil))
2160 (defun viper-insert (arg)
2161 "Insert before point."
2163 (viper-set-complex-command-for-undo)
2164 (let ((val (viper-p-val arg))
2165 ;;(com (viper-getcom arg))
2167 (viper-set-destructive-command (list 'viper-insert val ?r nil nil nil))
2168 (if (eq viper-intermediate-command 'viper-repeat)
2169 (viper-loop val (viper-yank-last-insertion))
2170 (viper-change-state-to-insert))))
2172 (defun viper-append (arg)
2173 "Append after point."
2175 (viper-set-complex-command-for-undo)
2176 (let ((val (viper-p-val arg))
2177 ;;(com (viper-getcom arg))
2179 (viper-set-destructive-command (list 'viper-append val ?r nil nil nil))
2180 (if (not (eolp)) (forward-char))
2181 (if (eq viper-intermediate-command 'viper-repeat)
2182 (viper-loop val (viper-yank-last-insertion))
2183 (viper-change-state-to-insert))))
2185 (defun viper-Append (arg)
2186 "Append at end of line."
2188 (viper-set-complex-command-for-undo)
2189 (let ((val (viper-p-val arg))
2190 ;;(com (viper-getcom arg))
2192 (viper-set-destructive-command (list 'viper-Append val ?r nil nil nil))
2194 (if (eq viper-intermediate-command 'viper-repeat)
2195 (viper-loop val (viper-yank-last-insertion))
2196 (viper-change-state-to-insert))))
2198 (defun viper-Insert (arg)
2199 "Insert before first non-white."
2201 (viper-set-complex-command-for-undo)
2202 (let ((val (viper-p-val arg))
2203 ;;(com (viper-getcom arg))
2205 (viper-set-destructive-command (list 'viper-Insert val ?r nil nil nil))
2206 (back-to-indentation)
2207 (if (eq viper-intermediate-command 'viper-repeat)
2208 (viper-loop val (viper-yank-last-insertion))
2209 (viper-change-state-to-insert))))
2211 (defun viper-open-line (arg)
2214 (viper-set-complex-command-for-undo)
2215 (let ((val (viper-p-val arg))
2216 ;;(com (viper-getcom arg))
2218 (viper-set-destructive-command (list 'viper-open-line val ?r nil nil nil))
2219 (let ((col (current-indentation)))
2220 (if (eq viper-intermediate-command 'viper-repeat)
2224 (viper-indent-line col)
2225 (viper-yank-last-insertion))
2228 (viper-indent-line col)
2229 (viper-change-state-to-insert)))))
2231 (defun viper-Open-line (arg)
2234 (viper-set-complex-command-for-undo)
2235 (let ((val (viper-p-val arg))
2236 ;;(com (viper-getcom arg))
2238 (viper-set-destructive-command (list 'viper-Open-line val ?r nil nil nil))
2239 (let ((col (current-indentation)))
2240 (if (eq viper-intermediate-command 'viper-repeat)
2244 (viper-indent-line col)
2245 (viper-yank-last-insertion))
2248 (viper-indent-line col)
2249 (viper-change-state-to-insert)))))
2251 (defun viper-open-line-at-point (arg)
2252 "Open line at point."
2254 (viper-set-complex-command-for-undo)
2255 (let ((val (viper-p-val arg))
2256 ;;(com (viper-getcom arg))
2258 (viper-set-destructive-command
2259 (list 'viper-open-line-at-point val ?r nil nil nil))
2260 (if (eq viper-intermediate-command 'viper-repeat)
2263 (viper-yank-last-insertion))
2265 (viper-change-state-to-insert))))
2268 (defun viper-substitute (arg)
2269 "Substitute characters."
2271 (let ((val (viper-p-val arg))
2272 ;;(com (viper-getcom arg))
2276 (if (eq viper-intermediate-command 'viper-repeat)
2277 (viper-change-subr (mark t) (point))
2278 (viper-change (mark t) (point)))
2279 ;; com is set to ?r when we repeat this command with dot
2280 (viper-set-destructive-command (list 'viper-substitute val ?r nil nil nil))
2283 ;; Command bound to S
2284 (defun viper-substitute-line (arg)
2287 (viper-set-complex-command-for-undo)
2288 (viper-line (cons arg ?C)))
2290 ;; Prepare for replace
2291 (defun viper-start-replace ()
2292 (setq viper-began-as-replace t
2293 viper-sitting-in-replace t
2294 viper-replace-chars-to-delete 0)
2296 'viper-after-change-functions 'viper-replace-mode-spy-after t 'local)
2298 'viper-before-change-functions 'viper-replace-mode-spy-before t 'local)
2299 ;; this will get added repeatedly, but no harm
2300 (add-hook 'after-change-functions 'viper-after-change-sentinel t)
2301 (add-hook 'before-change-functions 'viper-before-change-sentinel t)
2302 (viper-move-marker-locally
2303 'viper-last-posn-in-replace-region (viper-replace-start))
2305 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel
2308 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2309 ;; guard against a smarty who switched from R-replace to normal replace
2311 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2312 (if overwrite-mode (overwrite-mode -1))
2316 (defun viper-replace-mode-spy-before (beg end)
2317 (setq viper-replace-region-chars-deleted (viper-chars-in-region beg end))
2320 ;; Invoked as an after-change-function to calculate how many chars have to be
2321 ;; deleted. This function may be called several times within a single command,
2322 ;; if this command performs several separate buffer changes. Therefore, if
2323 ;; adds up the number of chars inserted and subtracts the number of chars
2325 (defun viper-replace-mode-spy-after (beg end length)
2326 (if (memq viper-intermediate-command
2327 '(dabbrev-expand hippie-expand repeating-insertion-from-ring))
2328 ;; Take special care of text insertion from insertion ring inside
2329 ;; replacement overlays.
2331 (setq viper-replace-chars-to-delete 0)
2332 (viper-move-marker-locally
2333 'viper-last-posn-in-replace-region (point)))
2335 (let* ((real-end (min end (viper-replace-end)))
2336 (column-shift (- (save-excursion (goto-char real-end)
2338 (save-excursion (goto-char beg)
2343 (setq chars-deleted viper-replace-region-chars-deleted))
2344 (setq viper-replace-region-chars-deleted 0)
2345 (setq viper-replace-chars-to-delete
2346 (+ viper-replace-chars-to-delete
2348 ;; if column shift is bigger, due to a TAB insertion, take
2349 ;; column-shift instead of the number of inserted chars
2350 (max (viper-chars-in-region beg real-end)
2351 ;; This test accounts for Chinese/Japanese/... chars,
2352 ;; which occupy 2 columns instead of one. If we use
2353 ;; column-shift here, we may delete two chars instead of
2354 ;; one when the user types one Chinese character.
2355 ;; Deleting two would be OK, if they were European chars,
2356 ;; but it is not OK if they are Chinese chars.
2357 ;; Since it is hard to
2358 ;; figure out which characters are being deleted in any
2359 ;; given region, we decided to treat Eastern and European
2360 ;; characters equally, even though Eastern chars may
2361 ;; occupy more columns.
2362 (if (memq this-command '(self-insert-command
2363 quoted-insert viper-insert-tab))
2366 ;; the number of deleted chars
2369 (viper-move-marker-locally
2370 'viper-last-posn-in-replace-region
2371 (max (if (> end (viper-replace-end)) (viper-replace-end) end)
2372 (or (marker-position viper-last-posn-in-replace-region)
2373 (viper-replace-start))
2379 ;; Delete stuff between viper-last-posn-in-replace-region and the end of
2380 ;; viper-replace-overlay-marker, if viper-last-posn-in-replace-region is within
2381 ;; the overlay and current point is before the end of the overlay.
2382 ;; Don't delete anything if current point is past the end of the overlay.
2383 (defun viper-finish-change ()
2385 'viper-after-change-functions 'viper-replace-mode-spy-after 'local)
2387 'viper-before-change-functions 'viper-replace-mode-spy-before 'local)
2389 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2391 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2392 (viper-restore-cursor-color 'after-replace-mode)
2393 (setq viper-sitting-in-replace nil) ; just in case we'll need to know it
2395 (if (and viper-replace-overlay
2396 (viper-pos-within-region viper-last-posn-in-replace-region
2397 (viper-replace-start)
2398 (viper-replace-end))
2399 (< (point) (viper-replace-end)))
2401 viper-last-posn-in-replace-region (viper-replace-end))))
2403 (if (eq viper-current-state 'replace-state)
2404 (viper-downgrade-to-insert))
2405 ;; replace mode ended => nullify viper-last-posn-in-replace-region
2406 (viper-move-marker-locally 'viper-last-posn-in-replace-region nil)
2407 (viper-hide-replace-overlay)
2408 (viper-refresh-mode-line)
2409 (viper-put-string-on-kill-ring viper-last-replace-region)
2412 ;; Make STRING be the first element of the kill ring.
2413 (defun viper-put-string-on-kill-ring (string)
2414 (setq kill-ring (cons string kill-ring))
2415 (if (> (length kill-ring) kill-ring-max)
2416 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2417 (setq kill-ring-yank-pointer kill-ring))
2419 (defun viper-finish-R-mode ()
2421 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2423 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2424 (viper-downgrade-to-insert))
2426 (defun viper-start-R-mode ()
2427 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2430 'viper-post-command-hooks 'viper-R-state-post-command-sentinel t 'local)
2432 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2433 ;; guard against a smarty who switched from R-replace to normal replace
2435 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2440 (defun viper-replace-state-exit-cmd ()
2441 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2442 These keys are ESC, RET, and LineFeed."
2444 (if overwrite-mode ; if in replace mode invoked via 'R'
2445 (viper-finish-R-mode)
2446 (viper-finish-change))
2448 (if (eq this-command 'viper-intercept-ESC-key)
2449 (setq com 'viper-exit-insert-state)
2450 (viper-set-unread-command-events last-input-event)
2451 (setq com (key-binding (viper-read-key-sequence nil))))
2453 (condition-case conds
2454 (command-execute com)
2456 (viper-message-conditions conds)))
2458 (viper-hide-replace-overlay))
2461 (defun viper-replace-state-carriage-return ()
2462 "Carriage return in Viper replace state."
2464 ;; If Emacs start supporting overlay maps, as it currently supports
2465 ;; text-property maps, we could do away with viper-replace-minor-mode and
2466 ;; just have keymap attached to replace overlay. Then the "if part" of this
2467 ;; statement can be deleted.
2468 (if (or (< (point) (viper-replace-start))
2469 (> (point) (viper-replace-end)))
2470 (let (viper-replace-minor-mode com)
2471 (viper-set-unread-command-events last-input-event)
2472 (setq com (key-binding (read-key-sequence nil)))
2473 (condition-case conds
2474 (command-execute com)
2476 (viper-message-conditions conds))))
2477 (if (not viper-allow-multiline-replace-regions)
2478 (viper-replace-state-exit-cmd)
2479 (if (viper-same-line (point) (viper-replace-end))
2480 (viper-replace-state-exit-cmd)
2481 ;; delete the rest of line
2482 (delete-region (point) (viper-line-pos 'end))
2485 (if (eobp) (error "Last line in buffer")))
2486 ;; skip to the next line
2488 (back-to-indentation)
2492 ;; This is the function bound to 'R'---unlimited replace.
2493 ;; Similar to Emacs's own overwrite-mode.
2494 (defun viper-overwrite (arg)
2495 "Begin overwrite mode."
2497 (let ((val (viper-p-val arg))
2498 ;;(com (viper-getcom arg))
2500 (viper-set-destructive-command (list 'viper-overwrite val ?r nil nil nil))
2501 (if (eq viper-intermediate-command 'viper-repeat)
2503 ;; Viper saves inserted text in viper-last-insertion
2504 (setq len (length viper-last-insertion))
2505 (delete-char (min len (- (point-max) (point) 1)))
2506 (viper-loop val (viper-yank-last-insertion)))
2507 (setq last-command 'viper-overwrite)
2508 (viper-set-complex-command-for-undo)
2509 (viper-set-replace-overlay (point) (viper-line-pos 'end))
2510 (viper-change-state-to-replace)
2516 (defun viper-line (arg)
2517 (let ((val (car arg))
2519 (viper-move-marker-locally 'viper-com-point (point))
2521 (viper-next-line-carefully (1- val)))
2522 ;; the following ensures that dd, cc, D, yy will do the right thing on the
2523 ;; last line of buffer when this line has no \n.
2524 (viper-add-newline-at-eob-if-necessary)
2525 (viper-execute-com 'viper-line val com))
2526 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2529 (defun viper-yank-line (arg)
2530 "Yank ARG lines (in Vi's sense)."
2532 (let ((val (viper-p-val arg)))
2533 (viper-line (cons val ?Y))))
2538 (defun viper-region (arg)
2539 "Execute command on a region."
2541 (let ((val (viper-P-val arg))
2542 (com (viper-getcom arg)))
2543 (viper-move-marker-locally 'viper-com-point (point))
2544 (exchange-point-and-mark)
2545 (viper-execute-com 'viper-region val com)))
2547 (defun viper-Region (arg)
2548 "Execute command on a Region."
2550 (let ((val (viper-P-val arg))
2551 (com (viper-getCom arg)))
2552 (viper-move-marker-locally 'viper-com-point (point))
2553 (exchange-point-and-mark)
2554 (viper-execute-com 'viper-Region val com)))
2556 (defun viper-replace-char (arg)
2557 "Replace the following ARG chars by the character read."
2559 (if (and (eolp) (bolp)) (error "No character to replace here"))
2560 (let ((val (viper-p-val arg))
2561 (com (viper-getcom arg)))
2562 (viper-replace-char-subr com val)
2563 (if (and (eolp) (not (bolp))) (forward-char 1))
2564 (setq viper-this-command-keys
2565 (format "%sr" (if (integerp arg) arg "")))
2566 (viper-set-destructive-command
2567 (list 'viper-replace-char val ?r nil viper-d-char nil))
2570 (defun viper-replace-char-subr (com arg)
2571 (let ((inhibit-quit t)
2573 (viper-set-complex-command-for-undo)
2574 (or (eq viper-intermediate-command 'viper-repeat)
2575 (viper-special-read-and-insert-char))
2578 (setq char (if com viper-d-char (viper-char-at-pos 'backward)))
2580 (if com (insert char))
2582 (setq viper-d-char char)
2584 (viper-loop (1- (if (> arg 0) arg (- arg)))
2588 (viper-complete-complex-command-for-undo)
2593 ;; basic cursor movement. j, k, l, h commands.
2595 (defun viper-forward-char (arg)
2596 "Move point right ARG characters (left if ARG negative).
2597 On reaching end of line, stop and signal error."
2599 (viper-leave-region-active)
2600 (let ((val (viper-p-val arg))
2601 (com (viper-getcom arg)))
2602 (if com (viper-move-marker-locally 'viper-com-point (point)))
2603 (if viper-ex-style-motion
2605 ;; the boundary condition check gets weird here because
2606 ;; forward-char may be the parameter of a delete, and 'dl' works
2607 ;; just like 'x' for the last char on a line, so we have to allow
2608 ;; the forward motion before the 'viper-execute-com', but, of
2609 ;; course, 'dl' doesn't work on an empty line, so we have to
2610 ;; catch that condition before 'viper-execute-com'
2611 (if (and (eolp) (bolp)) (error "Viper bell") (forward-char val))
2612 (if com (viper-execute-com 'viper-forward-char val com))
2613 (if (eolp) (progn (backward-char 1) (error "Viper bell"))))
2615 (if com (viper-execute-com 'viper-forward-char val com)))))
2618 (defun viper-backward-char (arg)
2619 "Move point left ARG characters (right if ARG negative).
2620 On reaching beginning of line, stop and signal error."
2622 (viper-leave-region-active)
2623 (let ((val (viper-p-val arg))
2624 (com (viper-getcom arg)))
2625 (if com (viper-move-marker-locally 'viper-com-point (point)))
2626 (if viper-ex-style-motion
2628 (if (bolp) (error "Viper bell") (backward-char val))
2629 (if com (viper-execute-com 'viper-backward-char val com)))
2631 (if com (viper-execute-com 'viper-backward-char val com)))))
2634 ;; Like forward-char, but doesn't move at end of buffer.
2635 ;; Returns distance traveled
2636 ;; (positive or 0, if arg positive; negative if arg negative).
2637 (defun viper-forward-char-carefully (&optional arg)
2638 (setq arg (or arg 1))
2643 (if (< (point) pt) ; arg was negative
2644 (- (viper-chars-in-region pt (point)))
2645 (viper-chars-in-region pt (point)))))
2648 ;; Like backward-char, but doesn't move at beg of buffer.
2649 ;; Returns distance traveled
2650 ;; (negative or 0, if arg positive; positive if arg negative).
2651 (defun viper-backward-char-carefully (&optional arg)
2652 (setq arg (or arg 1))
2657 (if (> (point) pt) ; arg was negative
2658 (viper-chars-in-region pt (point))
2659 (- (viper-chars-in-region pt (point))))))
2661 (defun viper-next-line-carefully (arg)
2663 ;; do not use forward-line! need to keep column
2664 (let ((line-move-visual nil))
2665 (if (featurep 'emacs)
2666 (with-no-warnings (next-line arg))
2674 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators for
2675 ;; word movement. When executed with a destructive command, \n is usually left
2676 ;; untouched for the last word. Viper uses syntax table to determine what is a
2677 ;; word and what is a separator. However, \n is always a separator. Also, if
2678 ;; viper-syntax-preference is 'vi, then `_' is part of the word.
2681 (defun viper-skip-separators (forward)
2684 (viper-skip-all-separators-forward 'within-line)
2685 (if (looking-at "\n")
2688 (viper-skip-all-separators-forward 'within-line))))
2689 ;; check for eob and white space before it. move off of eob
2690 (if (and (eobp) (save-excursion
2691 (viper-backward-char-carefully)
2692 (viper-looking-at-separator)))
2693 (viper-backward-char-carefully))
2694 (viper-skip-all-separators-backward 'within-line)
2695 (viper-backward-char-carefully)
2696 (if (looking-at "\n")
2697 (viper-skip-all-separators-backward 'within-line)
2698 (or (viper-looking-at-separator) (forward-char)))))
2701 (defun viper-forward-word-kernel (val)
2703 (cond ((viper-looking-at-alpha)
2704 (viper-skip-alpha-forward "_")
2705 (viper-skip-separators t))
2706 ((viper-looking-at-separator)
2707 (viper-skip-separators t))
2708 ((not (viper-looking-at-alphasep))
2709 (viper-skip-nonalphasep-forward)
2710 (viper-skip-separators t)))
2711 (setq val (1- val))))
2713 ;; first skip non-newline separators backward, then skip \n. Then, if TWICE is
2714 ;; non-nil, skip non-\n back again, but don't overshoot the limit LIM.
2715 (defun viper-separator-skipback-special (twice lim)
2716 (let ((prev-char (viper-char-at-pos 'backward))
2717 (saved-point (point)))
2718 ;; skip non-newline separators backward
2719 (while (and (not (viper-memq-char prev-char '(nil \n)))
2721 ;; must be non-newline separator
2722 (if (eq viper-syntax-preference 'strict-vi)
2723 (viper-memq-char prev-char '(?\ ?\t))
2724 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2725 (viper-backward-char-carefully)
2726 (setq prev-char (viper-char-at-pos 'backward)))
2728 (if (and (< lim (point)) (eq prev-char ?\n))
2730 ;; If we skipped to the next word and the prefix of this line doesn't
2731 ;; consist of separators preceded by a newline, then don't skip backwards
2733 (goto-char saved-point))
2734 (setq prev-char (viper-char-at-pos 'backward))
2736 ;; skip again, but make sure we don't overshoot the limit
2738 (while (and (not (viper-memq-char prev-char '(nil \n)))
2740 ;; must be non-newline separator
2741 (if (eq viper-syntax-preference 'strict-vi)
2742 (viper-memq-char prev-char '(?\ ?\t))
2743 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2744 (viper-backward-char-carefully)
2745 (setq prev-char (viper-char-at-pos 'backward))))
2748 (viper-forward-char-carefully))
2752 (defun viper-forward-word (arg)
2755 (viper-leave-region-active)
2756 (let ((val (viper-p-val arg))
2757 (com (viper-getcom arg)))
2758 (if com (viper-move-marker-locally 'viper-com-point (point)))
2759 (viper-forward-word-kernel val)
2762 (cond ((viper-char-equal com ?c)
2763 (viper-separator-skipback-special 'twice viper-com-point))
2764 ;; Yank words including the whitespace, but not newline
2765 ((viper-char-equal com ?y)
2766 (viper-separator-skipback-special nil viper-com-point))
2767 ((viper-dotable-command-p com)
2768 (viper-separator-skipback-special nil viper-com-point)))
2769 (viper-execute-com 'viper-forward-word val com)))
2773 (defun viper-forward-Word (arg)
2774 "Forward word delimited by white characters."
2776 (viper-leave-region-active)
2777 (let ((val (viper-p-val arg))
2778 (com (viper-getcom arg)))
2779 (if com (viper-move-marker-locally 'viper-com-point (point)))
2781 (viper-skip-nonseparators 'forward)
2782 (viper-skip-separators t))
2784 (cond ((viper-char-equal com ?c)
2785 (viper-separator-skipback-special 'twice viper-com-point))
2786 ;; Yank words including the whitespace, but not newline
2787 ((viper-char-equal com ?y)
2788 (viper-separator-skipback-special nil viper-com-point))
2789 ((viper-dotable-command-p com)
2790 (viper-separator-skipback-special nil viper-com-point)))
2791 (viper-execute-com 'viper-forward-Word val com)))))
2794 ;; this is a bit different from Vi, but Vi's end of word
2795 ;; makes no sense whatsoever
2796 (defun viper-end-of-word-kernel ()
2797 (if (viper-end-of-word-p) (forward-char))
2798 (if (viper-looking-at-separator)
2799 (viper-skip-all-separators-forward))
2801 (cond ((viper-looking-at-alpha) (viper-skip-alpha-forward "_"))
2802 ((not (viper-looking-at-alphasep)) (viper-skip-nonalphasep-forward)))
2803 (viper-backward-char-carefully))
2805 (defun viper-end-of-word-p ()
2808 (cond ((viper-looking-at-alpha)
2810 (not (viper-looking-at-alpha)))
2811 ((not (viper-looking-at-alphasep))
2813 (viper-looking-at-alphasep))))))
2816 (defun viper-end-of-word (arg &optional careful)
2817 "Move point to end of current word."
2819 (viper-leave-region-active)
2820 (let ((val (viper-p-val arg))
2821 (com (viper-getcom arg)))
2822 (if com (viper-move-marker-locally 'viper-com-point (point)))
2823 (viper-loop val (viper-end-of-word-kernel))
2827 (viper-execute-com 'viper-end-of-word val com)))))
2829 (defun viper-end-of-Word (arg)
2830 "Forward to end of word delimited by white character."
2832 (viper-leave-region-active)
2833 (let ((val (viper-p-val arg))
2834 (com (viper-getcom arg)))
2835 (if com (viper-move-marker-locally 'viper-com-point (point)))
2837 (viper-end-of-word-kernel)
2838 (viper-skip-nonseparators 'forward)
2843 (viper-execute-com 'viper-end-of-Word val com)))))
2845 (defun viper-backward-word-kernel (val)
2847 (viper-backward-char-carefully)
2848 (cond ((viper-looking-at-alpha)
2849 (viper-skip-alpha-backward "_"))
2850 ((viper-looking-at-separator)
2852 (viper-skip-separators nil)
2853 (viper-backward-char-carefully)
2854 (cond ((viper-looking-at-alpha)
2855 (viper-skip-alpha-backward "_"))
2856 ((not (viper-looking-at-alphasep))
2857 (viper-skip-nonalphasep-backward))
2858 ((bobp)) ; could still be at separator, but at beg of buffer
2859 (t (forward-char))))
2860 ((not (viper-looking-at-alphasep))
2861 (viper-skip-nonalphasep-backward)))
2862 (setq val (1- val))))
2864 (defun viper-backward-word (arg)
2867 (viper-leave-region-active)
2868 (let ((val (viper-p-val arg))
2869 (com (viper-getcom arg)))
2872 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2874 (viper-move-marker-locally 'viper-com-point (point))
2875 (if i (forward-char))))
2876 (viper-backward-word-kernel val)
2877 (if com (viper-execute-com 'viper-backward-word val com))))
2879 (defun viper-backward-Word (arg)
2880 "Backward word delimited by white character."
2882 (viper-leave-region-active)
2883 (let ((val (viper-p-val arg))
2884 (com (viper-getcom arg)))
2887 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2889 (viper-move-marker-locally 'viper-com-point (point))
2890 (if i (forward-char))))
2892 (viper-skip-separators nil) ; nil means backward here
2893 (viper-skip-nonseparators 'backward))
2894 (if com (viper-execute-com 'viper-backward-Word val com))))
2900 (defun viper-beginning-of-line (arg)
2901 "Go to beginning of line."
2903 (viper-leave-region-active)
2904 (let ((val (viper-p-val arg))
2905 (com (viper-getcom arg)))
2906 (if com (viper-move-marker-locally 'viper-com-point (point)))
2907 (beginning-of-line val)
2908 (if com (viper-execute-com 'viper-beginning-of-line val com))))
2910 (defun viper-bol-and-skip-white (arg)
2911 "Beginning of line at first non-white character."
2913 (viper-leave-region-active)
2914 (let ((val (viper-p-val arg))
2915 (com (viper-getcom arg)))
2916 (if com (viper-move-marker-locally 'viper-com-point (point)))
2917 (forward-to-indentation (1- val))
2918 (if com (viper-execute-com 'viper-bol-and-skip-white val com))))
2920 (defun viper-goto-eol (arg)
2921 "Go to end of line."
2923 (viper-leave-region-active)
2924 (let ((val (viper-p-val arg))
2925 (com (viper-getcom arg)))
2926 (if com (viper-move-marker-locally 'viper-com-point (point)))
2928 (if com (viper-execute-com 'viper-goto-eol val com))
2929 (if viper-ex-style-motion
2930 (if (and (eolp) (not (bolp))
2931 ;; a fix for viper-change-to-eol
2932 (not (equal viper-current-state 'insert-state)))
2937 (defun viper-goto-col (arg)
2938 "Go to ARG's column."
2940 (viper-leave-region-active)
2941 (let ((val (viper-p-val arg))
2942 (com (viper-getcom arg))
2945 (viper-chars-in-region
2946 (viper-line-pos 'start) (viper-line-pos 'end)))
2947 (if com (viper-move-marker-locally 'viper-com-point (point)))
2949 (forward-char (1- (min line-len val)))
2950 (while (> (current-column) (1- val))
2952 (if com (viper-execute-com 'viper-goto-col val com))
2955 (if (> val (current-column)) (error "Viper bell")))
2959 (defun viper-next-line (arg)
2962 (viper-leave-region-active)
2963 (let ((val (viper-p-val arg))
2964 (com (viper-getCom arg)))
2965 (if com (viper-move-marker-locally 'viper-com-point (point)))
2966 ;; do not use forward-line! need to keep column
2967 (let ((line-move-visual nil))
2968 (if (featurep 'emacs)
2969 (with-no-warnings (next-line val))
2971 (if viper-ex-style-motion
2972 (if (and (eolp) (not (bolp))) (backward-char 1)))
2973 (setq this-command 'next-line)
2974 (if com (viper-execute-com 'viper-next-line val com))))
2976 (declare-function widget-type "wid-edit" (widget))
2977 (declare-function widget-button-press "wid-edit" (pos &optional event))
2978 (declare-function viper-set-hooks "viper" ())
2980 (defun viper-next-line-at-bol (arg)
2981 "Next line at beginning of line.
2982 If point is on a widget or a button, simulate clicking on that widget/button."
2984 (let* ((field (get-char-property (point) 'field))
2985 (button (get-char-property (point) 'button))
2986 (doc (get-char-property (point) 'widget-doc))
2987 (widget (or field button doc)))
2989 (if (symbolp widget)
2990 (get widget 'widget-type)
2992 (get (widget-type widget) 'widget-type))))
2993 (widget-button-press (point))
2994 (if (and (fboundp 'button-at) (fboundp 'push-button) (button-at (point)))
2996 ;; not a widget or a button
2997 (viper-leave-region-active)
3000 (if (eobp) (error "Last line in buffer")))
3001 (let ((val (viper-p-val arg))
3002 (com (viper-getCom arg)))
3003 (if com (viper-move-marker-locally 'viper-com-point (point)))
3005 (back-to-indentation)
3006 (if com (viper-execute-com 'viper-next-line-at-bol val com)))))))
3009 (defun viper-previous-line (arg)
3010 "Go to previous line."
3012 (viper-leave-region-active)
3013 (let ((val (viper-p-val arg))
3014 (com (viper-getCom arg)))
3015 (if com (viper-move-marker-locally 'viper-com-point (point)))
3016 ;; do not use forward-line! need to keep column
3017 (let ((line-move-visual nil))
3018 (if (featurep 'emacs)
3019 (with-no-warnings (previous-line val))
3020 (previous-line val)))
3021 (if viper-ex-style-motion
3022 (if (and (eolp) (not (bolp))) (backward-char 1)))
3023 (setq this-command 'previous-line)
3024 (if com (viper-execute-com 'viper-previous-line val com))))
3027 (defun viper-previous-line-at-bol (arg)
3028 "Previous line at beginning of line."
3030 (viper-leave-region-active)
3033 (if (bobp) (error "First line in buffer")))
3034 (let ((val (viper-p-val arg))
3035 (com (viper-getCom arg)))
3036 (if com (viper-move-marker-locally 'viper-com-point (point)))
3037 (forward-line (- val))
3038 (back-to-indentation)
3039 (if com (viper-execute-com 'viper-previous-line val com))))
3041 (defun viper-change-to-eol (arg)
3042 "Change to end of line."
3044 (viper-goto-eol (cons arg ?c)))
3046 (defun viper-kill-line (arg)
3049 (viper-goto-eol (cons arg ?d)))
3051 (defun viper-erase-line (arg)
3054 (viper-beginning-of-line (cons arg ?d)))
3059 (defun viper-goto-line (arg)
3060 "Go to ARG's line. Without ARG go to end of buffer."
3062 (let ((val (viper-P-val arg))
3063 (com (viper-getCom arg)))
3064 (viper-move-marker-locally 'viper-com-point (point))
3065 (viper-deactivate-mark)
3068 (goto-char (point-max))
3069 (goto-char (point-min))
3070 (forward-line (1- val)))
3072 ;; positioning is done twice: before and after command execution
3073 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3074 (back-to-indentation)
3076 (if com (viper-execute-com 'viper-goto-line val com))
3078 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3079 (back-to-indentation)
3082 ;; Find ARG's occurrence of CHAR on the current line.
3083 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
3084 ;; adjust point after search.
3085 (defun viper-find-char (arg char forward offset)
3086 (or (char-or-string-p char) (error "Viper bell"))
3087 (let ((arg (if forward arg (- arg)))
3088 (cmd (if (eq viper-intermediate-command 'viper-repeat)
3090 (viper-array-to-string (this-command-keys))))
3091 point region-beg region-end)
3094 (if (> arg 0) ; forward
3096 (setq region-beg (point))
3097 (if viper-allow-multiline-replace-regions
3098 (viper-forward-paragraph 1)
3100 (setq region-end (point)))
3101 (setq region-end (point))
3102 (if viper-allow-multiline-replace-regions
3103 (viper-backward-paragraph 1)
3104 (beginning-of-line))
3105 (setq region-beg (point)))
3106 (if (or (and (< arg 0)
3107 (< (- region-end region-beg)
3108 (if viper-allow-multiline-replace-regions
3112 (< (- region-end region-beg)
3113 (if viper-allow-multiline-replace-regions
3116 (error "Command `%s': At %s of %s"
3118 (if (> arg 0) "end" "beginning")
3119 (if viper-allow-multiline-replace-regions
3120 "paragraph" "line")))
3121 (narrow-to-region region-beg region-end)
3122 ;; if arg > 0, point is forwarded before search.
3123 (if (> arg 0) (goto-char (1+ (point-min)))
3124 (goto-char (point-max)))
3125 (if (let ((case-fold-search nil))
3126 (search-forward (char-to-string char) nil 0 arg))
3127 (setq point (point))
3128 (error "Command `%s': `%c' not found" cmd char))))
3131 (backward-char (if offset 2 1))
3132 (forward-char (if offset 1 0)))))
3134 (defun viper-find-char-forward (arg)
3135 "Find char on the line.
3136 If called interactively read the char to find from the terminal, and if
3137 called from viper-repeat, the char last used is used. This behavior is
3138 controlled by the sign of prefix numeric value."
3140 (let ((val (viper-p-val arg))
3141 (com (viper-getcom arg))
3142 (cmd-representation (nth 5 viper-d-com)))
3144 ;; this means that the function was called interactively
3145 (setq viper-f-char (read-char)
3148 ;; viper-repeat --- set viper-F-char from command-keys
3149 (setq viper-F-char (if (stringp cmd-representation)
3150 (viper-seq-last-elt cmd-representation)
3152 viper-f-char viper-F-char)
3154 (if com (viper-move-marker-locally 'viper-com-point (point)))
3156 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t nil)
3160 (setq viper-F-char viper-f-char) ; set new viper-F-char
3162 (viper-execute-com 'viper-find-char-forward val com)))))
3164 (defun viper-goto-char-forward (arg)
3165 "Go up to char ARG forward on line."
3167 (let ((val (viper-p-val arg))
3168 (com (viper-getcom arg))
3169 (cmd-representation (nth 5 viper-d-com)))
3171 ;; this means that the function was called interactively
3172 (setq viper-f-char (read-char)
3175 ;; viper-repeat --- set viper-F-char from command-keys
3176 (setq viper-F-char (if (stringp cmd-representation)
3177 (viper-seq-last-elt cmd-representation)
3179 viper-f-char viper-F-char)
3181 (if com (viper-move-marker-locally 'viper-com-point (point)))
3183 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t t)
3187 (setq viper-F-char viper-f-char) ; set new viper-F-char
3189 (viper-execute-com 'viper-goto-char-forward val com)))))
3191 (defun viper-find-char-backward (arg)
3192 "Find char ARG on line backward."
3194 (let ((val (viper-p-val arg))
3195 (com (viper-getcom arg))
3196 (cmd-representation (nth 5 viper-d-com)))
3198 ;; this means that the function was called interactively
3199 (setq viper-f-char (read-char)
3202 ;; viper-repeat --- set viper-F-char from command-keys
3203 (setq viper-F-char (if (stringp cmd-representation)
3204 (viper-seq-last-elt cmd-representation)
3206 viper-f-char viper-F-char)
3208 (if com (viper-move-marker-locally 'viper-com-point (point)))
3210 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil nil)
3214 (setq viper-F-char viper-f-char) ; set new viper-F-char
3215 (viper-execute-com 'viper-find-char-backward val com)))))
3217 (defun viper-goto-char-backward (arg)
3218 "Go up to char ARG backward on line."
3220 (let ((val (viper-p-val arg))
3221 (com (viper-getcom arg))
3222 (cmd-representation (nth 5 viper-d-com)))
3224 ;; this means that the function was called interactively
3225 (setq viper-f-char (read-char)
3228 ;; viper-repeat --- set viper-F-char from command-keys
3229 (setq viper-F-char (if (stringp cmd-representation)
3230 (viper-seq-last-elt cmd-representation)
3232 viper-f-char viper-F-char)
3234 (if com (viper-move-marker-locally 'viper-com-point (point)))
3236 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil t)
3240 (setq viper-F-char viper-f-char) ; set new viper-F-char
3241 (viper-execute-com 'viper-goto-char-backward val com)))))
3243 (defun viper-repeat-find (arg)
3244 "Repeat previous find command."
3246 (let ((val (viper-p-val arg))
3247 (com (viper-getcom arg)))
3248 (viper-deactivate-mark)
3249 (if com (viper-move-marker-locally 'viper-com-point (point)))
3250 (viper-find-char val viper-f-char viper-f-forward viper-f-offset)
3253 (if viper-f-forward (forward-char))
3254 (viper-execute-com 'viper-repeat-find val com)))))
3256 (defun viper-repeat-find-opposite (arg)
3257 "Repeat previous find command in the opposite direction."
3259 (let ((val (viper-p-val arg))
3260 (com (viper-getcom arg)))
3261 (viper-deactivate-mark)
3262 (if com (viper-move-marker-locally 'viper-com-point (point)))
3263 (viper-find-char val viper-f-char (not viper-f-forward) viper-f-offset)
3266 (if viper-f-forward (forward-char))
3267 (viper-execute-com 'viper-repeat-find-opposite val com)))))
3270 ;; window scrolling etc.
3272 (defun viper-window-top (arg)
3273 "Go to home window line."
3275 (let ((val (viper-p-val arg))
3276 (com (viper-getCom arg)))
3277 (viper-leave-region-active)
3278 (if com (viper-move-marker-locally 'viper-com-point (point)))
3280 (move-to-window-line (1- val))
3282 ;; positioning is done twice: before and after command execution
3283 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3284 (back-to-indentation)
3286 (if com (viper-execute-com 'viper-window-top val com))
3288 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3289 (back-to-indentation)
3292 (defun viper-window-middle (arg)
3293 "Go to middle window line."
3295 (let ((val (viper-p-val arg))
3296 (com (viper-getCom arg)))
3297 (viper-leave-region-active)
3298 (if com (viper-move-marker-locally 'viper-com-point (point)))
3300 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3302 ;; positioning is done twice: before and after command execution
3303 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3304 (back-to-indentation)
3306 (if com (viper-execute-com 'viper-window-middle val com))
3308 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3309 (back-to-indentation)
3312 (defun viper-window-bottom (arg)
3313 "Go to last window line."
3315 (let ((val (viper-p-val arg))
3316 (com (viper-getCom arg)))
3317 (viper-leave-region-active)
3318 (if com (viper-move-marker-locally 'viper-com-point (point)))
3320 (move-to-window-line (- val))
3322 ;; positioning is done twice: before and after command execution
3323 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3324 (back-to-indentation)
3326 (if com (viper-execute-com 'viper-window-bottom val com))
3328 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3329 (back-to-indentation)
3332 (defun viper-line-to-top (arg)
3333 "Put current line on the home line."
3335 (recenter (1- arg)))
3337 (defun viper-line-to-middle (arg)
3338 "Put current line on the middle line."
3340 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3342 (defun viper-line-to-bottom (arg)
3343 "Put current line on the last line."
3345 (recenter (- (window-height) (1+ arg))))
3347 ;; If point is within viper-search-scroll-threshold of window top or bottom,
3348 ;; scroll up or down 1/7 of window height, depending on whether we are at the
3349 ;; bottom or at the top of the window. This function is called by viper-search
3350 ;; (which is called from viper-search-forward/backward/next). If the value of
3351 ;; viper-search-scroll-threshold is negative - don't scroll.
3352 (defun viper-adjust-window ()
3353 (let ((win-height (if (featurep 'xemacs)
3354 (window-displayed-height)
3355 (1- (window-height)))) ; adjust for mode line
3357 at-top-p at-bottom-p
3358 min-scroll direction)
3360 (move-to-window-line 0) ; top
3362 (<= (count-lines pt (point))
3363 viper-search-scroll-threshold))
3364 (move-to-window-line -1) ; bottom
3366 (<= (count-lines pt (point)) viper-search-scroll-threshold)))
3367 (cond (at-top-p (setq min-scroll (1- viper-search-scroll-threshold)
3369 (at-bottom-p (setq min-scroll (1+ viper-search-scroll-threshold)
3373 (* (max min-scroll (/ win-height 7)) direction)))
3378 ;; must correct this to only match ( to ) etc. On the other hand
3379 ;; it is good that paren match gets confused, because that way you
3380 ;; catch _all_ imbalances.
3382 (defun viper-paren-match (arg)
3383 "Go to the matching parenthesis."
3385 (viper-leave-region-active)
3386 (let ((com (viper-getcom arg))
3387 (parse-sexp-ignore-comments viper-parse-sexp-ignore-comments)
3390 (if (or (> arg 99) (< arg 1))
3391 (error "Prefix must be between 1 and 99")
3393 (if (> (point-max) 80000)
3394 (* (/ (point-max) 100) arg)
3395 (/ (* (point-max) arg) 100)))
3396 (back-to-indentation))
3397 (let (beg-lim end-lim)
3398 (if (and (eolp) (not (bolp))) (forward-char -1))
3399 (if (not (looking-at "[][(){}]"))
3400 (setq anchor-point (point)))
3401 (setq beg-lim (point-at-bol)
3402 end-lim (point-at-eol))
3403 (cond ((re-search-forward "[][(){}]" end-lim t)
3405 ((re-search-backward "[][(){}]" beg-lim t))
3407 (error "No matching character on line"))))
3408 (cond ((looking-at "[([{]")
3409 (if com (viper-move-marker-locally 'viper-com-point (point)))
3412 (viper-execute-com 'viper-paren-match nil com)
3417 (viper-move-marker-locally 'viper-com-point anchor-point)
3419 (viper-execute-com 'viper-paren-match nil com)
3421 ((looking-at "[])}]")
3423 (if com (viper-move-marker-locally 'viper-com-point (point)))
3425 (if com (viper-execute-com 'viper-paren-match nil com)))
3426 (t (error "Viper bell"))))))
3428 (defun viper-toggle-parse-sexp-ignore-comments ()
3430 (setq viper-parse-sexp-ignore-comments
3431 (not viper-parse-sexp-ignore-comments))
3432 (princ (format-message
3433 "From now on, `%%' will %signore parentheses inside comment fields"
3434 (if viper-parse-sexp-ignore-comments "" "NOT "))))
3437 ;; sentence, paragraph and heading
3439 (defun viper-forward-sentence (arg)
3442 (or (eq last-command this-command)
3444 (let ((val (viper-p-val arg))
3445 (com (viper-getcom arg)))
3446 (if com (viper-move-marker-locally 'viper-com-point (point)))
3447 (forward-sentence val)
3448 (if com (viper-execute-com 'viper-forward-sentence nil com))))
3450 (defun viper-backward-sentence (arg)
3451 "Backward sentence."
3453 (or (eq last-command this-command)
3455 (let ((val (viper-p-val arg))
3456 (com (viper-getcom arg)))
3457 (if com (viper-move-marker-locally 'viper-com-point (point)))
3458 (backward-sentence val)
3459 (if com (viper-execute-com 'viper-backward-sentence nil com))))
3461 (defun viper-forward-paragraph (arg)
3462 "Forward paragraph."
3464 (or (eq last-command this-command)
3466 (let ((val (viper-p-val arg))
3467 ;; if you want d} operate on whole lines, change viper-getcom to
3468 ;; viper-getCom below
3469 (com (viper-getcom arg)))
3470 (if com (viper-move-marker-locally 'viper-com-point (point)))
3471 (forward-paragraph val)
3475 (viper-execute-com 'viper-forward-paragraph nil com)))))
3477 (defun viper-backward-paragraph (arg)
3478 "Backward paragraph."
3480 (or (eq last-command this-command)
3482 (let ((val (viper-p-val arg))
3483 ;; if you want d{ operate on whole lines, change viper-getcom to
3484 ;; viper-getCom below
3485 (com (viper-getcom arg)))
3486 (if com (viper-move-marker-locally 'viper-com-point (point)))
3487 (backward-paragraph val)
3491 (viper-execute-com 'viper-backward-paragraph nil com)
3492 (backward-char 1)))))
3494 ;; should be mode-specific
3495 (defun viper-prev-heading (arg)
3497 (let ((val (viper-p-val arg))
3498 (com (viper-getCom arg)))
3499 (if com (viper-move-marker-locally 'viper-com-point (point)))
3500 (re-search-backward viper-heading-start nil t val)
3501 (goto-char (match-beginning 0))
3502 (if com (viper-execute-com 'viper-prev-heading nil com))))
3504 (defun viper-heading-end (arg)
3506 (let ((val (viper-p-val arg))
3507 (com (viper-getCom arg)))
3508 (if com (viper-move-marker-locally 'viper-com-point (point)))
3509 (re-search-forward viper-heading-end nil t val)
3510 (goto-char (match-beginning 0))
3511 (if com (viper-execute-com 'viper-heading-end nil com))))
3513 (defun viper-next-heading (arg)
3515 (let ((val (viper-p-val arg))
3516 (com (viper-getCom arg)))
3517 (if com (viper-move-marker-locally 'viper-com-point (point)))
3519 (re-search-forward viper-heading-start nil t val)
3520 (goto-char (match-beginning 0))
3521 (if com (viper-execute-com 'viper-next-heading nil com))))
3526 (defun viper-scroll-screen (arg)
3527 "Scroll to next screen."
3533 (setq arg (1- arg)))
3536 (setq arg (1+ arg))))
3540 (message "End of buffer")
3541 (goto-char (point-max)))
3542 (message "Beginning of buffer")
3543 (goto-char (point-min))))
3546 (defun viper-scroll-screen-back (arg)
3547 "Scroll to previous screen."
3549 (viper-scroll-screen (- arg)))
3551 (defun viper-scroll-down (arg)
3552 "Pull down half screen."
3556 (scroll-down (/ (window-height) 2))
3559 (message "Beginning of buffer")
3560 (goto-char (point-min)))))
3562 (defun viper-scroll-down-one (arg)
3563 "Scroll up one line."
3567 (defun viper-scroll-up (arg)
3568 "Pull up half screen."
3572 (scroll-up (/ (window-height) 2))
3575 (message "End of buffer")
3576 (goto-char (point-max)))))
3578 (defun viper-scroll-up-one (arg)
3579 "Scroll down one line."
3586 (defun viper-insert-isearch-string ()
3587 "Insert `isearch' last search string."
3589 (when isearch-string (insert isearch-string)))
3591 (defun viper-if-string (prompt)
3592 (if (memq viper-intermediate-command
3593 '(viper-command-argument viper-digit-argument viper-repeat))
3594 (setq viper-this-command-keys (this-command-keys)))
3595 (let* ((keymap (let ((keymap (copy-keymap minibuffer-local-map)))
3596 (define-key keymap [(control ?s)] 'viper-insert-isearch-string)
3598 (s (viper-read-string-with-history
3601 'viper-search-history
3602 (car viper-search-history)
3604 (if (not (string= s ""))
3605 (setq viper-s-string s))))
3608 (defun viper-toggle-search-style (arg)
3609 "Toggle the value of viper-case-fold-search/viper-re-search.
3610 Without prefix argument, will ask which search style to toggle. With prefix
3611 arg 1, toggles viper-case-fold-search; with arg 2 toggles viper-re-search.
3613 Although this function is bound to \\[viper-toggle-search-style], the most
3614 convenient way to use it is to bind `//' to the macro
3615 `1 M-x viper-toggle-search-style' and `///' to
3616 `2 M-x viper-toggle-search-style'. In this way, hitting `//' quickly will
3617 toggle case-fold-search and hitting `/' three times with toggle regexp
3618 search. Macros are more convenient in this case because they don't affect
3619 the Emacs binding of `/'."
3622 (cond ((or (eq arg 1)
3624 (y-or-n-p (format-message
3625 "Search style: `%s'. Want `%s'? "
3626 (if viper-case-fold-search
3627 "case-insensitive" "case-sensitive")
3628 (if viper-case-fold-search
3630 "case-insensitive")))))
3631 (setq viper-case-fold-search (null viper-case-fold-search))
3632 (if viper-case-fold-search
3633 (setq msg "Search becomes case-insensitive")
3634 (setq msg "Search becomes case-sensitive")))
3637 (y-or-n-p (format-message
3638 "Search style: `%s'. Want `%s'? "
3640 "regexp-search" "vanilla-search")
3643 "regexp-search")))))
3644 (setq viper-re-search (null viper-re-search))
3646 (setq msg "Search becomes regexp-style")
3647 (setq msg "Search becomes vanilla-style")))
3649 (setq msg "Search style remains unchanged")))
3652 (defun viper-set-searchstyle-toggling-macros (unset &optional major-mode)
3653 "Set the macros for toggling the search style in Viper's vi-state.
3654 The macro that toggles case sensitivity is bound to `//', and the one that
3655 toggles regexp search is bound to `///'.
3656 With a prefix argument, this function unsets the macros.
3657 If MAJOR-MODE is set, set the macros only in that major mode."
3660 (if (and major-mode (symbolp major-mode))
3661 (setq scope major-mode)
3666 ;; toggle case sensitivity in search
3667 (viper-record-kbd-macro
3669 [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]
3671 ;; toggle regexp/vanilla search
3672 (viper-record-kbd-macro
3674 [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]
3676 (if (if (featurep 'xemacs)
3678 (called-interactively-p 'interactive))
3680 "// and /// now toggle case-sensitivity and regexp search")))
3681 (viper-unrecord-kbd-macro "//" 'vi-state)
3683 (viper-unrecord-kbd-macro "///" 'vi-state)))
3687 (defun viper-set-parsing-style-toggling-macro (unset)
3688 "Set `%%%' to be a macro that toggles whether comment fields should be parsed for matching parentheses.
3689 This is used in conjunction with the `%' command.
3691 With a prefix argument, unsets the macro."
3696 ;; Make %%% toggle parsing comments for matching parentheses
3697 (viper-record-kbd-macro
3699 [(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]
3701 (if (if (featurep 'xemacs)
3703 (called-interactively-p 'interactive))
3705 "%%%%%% now toggles whether comments should be parsed for matching parentheses")))
3706 (viper-unrecord-kbd-macro "%%%" 'vi-state))))
3709 (defun viper-set-emacs-state-searchstyle-macros (unset &optional arg-majormode)
3710 "Set the macros for toggling the search style in Viper's emacs-state.
3711 The macro that toggles case sensitivity is bound to `//', and the one that
3712 toggles regexp search is bound to `///'.
3713 With a prefix argument, this function unsets the macros.
3714 If the optional prefix argument is non-nil and specifies a valid major mode,
3715 this sets the macros only in the macros in that major mode. Otherwise,
3716 the macros are set in the current major mode.
3717 \(When unsetting the macros, the second argument has no effect.)"
3722 ;; toggle case sensitivity in search
3723 (viper-record-kbd-macro
3725 [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]
3726 (or arg-majormode major-mode))
3727 ;; toggle regexp/vanilla search
3728 (viper-record-kbd-macro
3730 [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]
3731 (or arg-majormode major-mode))
3732 (if (if (featurep 'xemacs)
3734 (called-interactively-p 'interactive))
3736 "// and /// now toggle case-sensitivity and regexp search.")))
3737 (viper-unrecord-kbd-macro "//" 'emacs-state)
3739 (viper-unrecord-kbd-macro "///" 'emacs-state))))
3742 (defun viper-search-forward (arg)
3743 "Search a string forward.
3744 ARG is used to find the ARG's occurrence of the string.
3745 Null string will repeat previous search."
3747 (let ((val (viper-P-val arg))
3748 (com (viper-getcom arg))
3749 (old-str viper-s-string)
3751 (setq viper-s-forward t)
3752 (viper-if-string "/")
3753 ;; this is not used at present, but may be used later
3754 (if (or (not (equal old-str viper-s-string))
3755 (not (markerp viper-local-search-start-marker))
3756 (not (marker-buffer viper-local-search-start-marker)))
3757 (setq viper-local-search-start-marker (point-marker)))
3758 (viper-search viper-s-string t val)
3761 (viper-move-marker-locally 'viper-com-point (mark t))
3762 (viper-execute-com 'viper-search-next val com)))
3765 (defun viper-search-backward (arg)
3766 "Search a string backward.
3767 ARG is used to find the ARG's occurrence of the string.
3768 Null string will repeat previous search."
3770 (let ((val (viper-P-val arg))
3771 (com (viper-getcom arg))
3772 (old-str viper-s-string)
3774 (setq viper-s-forward nil)
3775 (viper-if-string "?")
3776 ;; this is not used at present, but may be used later
3777 (if (or (not (equal old-str viper-s-string))
3778 (not (markerp viper-local-search-start-marker))
3779 (not (marker-buffer viper-local-search-start-marker)))
3780 (setq viper-local-search-start-marker (point-marker)))
3781 (viper-search viper-s-string nil val)
3784 (viper-move-marker-locally 'viper-com-point (mark t))
3785 (viper-execute-com 'viper-search-next val com)))))
3788 ;; Search for COUNT's occurrence of STRING.
3789 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3790 ;; INIT-POINT is the position where search is to start.
3792 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3793 (defun viper-search (string forward arg
3794 &optional no-offset init-point fail-if-not-found)
3795 (if (not (equal string ""))
3796 (let ((val (viper-p-val arg))
3797 (com (viper-getcom arg))
3798 (offset (not no-offset))
3799 (case-fold-search viper-case-fold-search)
3800 (start-point (or init-point (point))))
3801 (viper-deactivate-mark)
3805 (if offset (viper-forward-char-carefully))
3808 (re-search-forward string nil nil val)
3809 (re-search-backward string))
3810 (search-forward string nil nil val)
3811 (search-backward string))
3812 (if (not (equal start-point (point)))
3813 (push-mark start-point t)))
3815 (if (and (not fail-if-not-found) viper-search-wrap-around)
3817 (message "Search wrapped around BOTTOM of buffer")
3818 (goto-char (point-min))
3819 (viper-search string forward (cons 1 com) t start-point 'fail)
3820 ;; don't wait in macros
3821 (or executing-kbd-macro
3822 (memq viper-intermediate-command
3824 viper-digit-argument
3825 viper-command-argument))
3827 ;; delete the wrap-around message
3830 (goto-char start-point)
3831 (error "`%s': %s not found"
3833 (if viper-re-search "Pattern" "String"))
3839 (re-search-backward string nil nil val)
3840 (search-backward string nil nil val))
3841 (if (not (equal start-point (point)))
3842 (push-mark start-point t)))
3844 (if (and (not fail-if-not-found) viper-search-wrap-around)
3846 (message "Search wrapped around TOP of buffer")
3847 (goto-char (point-max))
3848 (viper-search string forward (cons 1 com) t start-point 'fail)
3849 ;; don't wait in macros
3850 (or executing-kbd-macro
3851 (memq viper-intermediate-command
3853 viper-digit-argument
3854 viper-command-argument))
3856 ;; delete the wrap-around message
3859 (goto-char start-point)
3860 (error "`%s': %s not found"
3862 (if viper-re-search "Pattern" "String"))
3864 ;; pull up or down if at top/bottom of window
3865 (viper-adjust-window)
3866 ;; highlight the result of search
3867 ;; don't wait and don't highlight in macros
3868 (or executing-kbd-macro
3869 (memq viper-intermediate-command
3870 '(viper-repeat viper-digit-argument viper-command-argument))
3871 (viper-flash-search-pattern))
3874 (defun viper-search-next (arg)
3875 "Repeat previous search."
3877 (let ((val (viper-p-val arg))
3878 (com (viper-getcom arg))
3880 (if (or (null viper-s-string) (string= viper-s-string ""))
3881 (error viper-NoPrevSearch))
3882 (viper-search viper-s-string viper-s-forward arg)
3885 (viper-move-marker-locally 'viper-com-point (mark t))
3886 (viper-execute-com 'viper-search-next val com)))))
3888 (defun viper-search-Next (arg)
3889 "Repeat previous search in the reverse direction."
3891 (let ((val (viper-p-val arg))
3892 (com (viper-getcom arg))
3894 (if (null viper-s-string) (error viper-NoPrevSearch))
3895 (viper-search viper-s-string (not viper-s-forward) arg)
3898 (viper-move-marker-locally 'viper-com-point (mark t))
3899 (viper-execute-com 'viper-search-Next val com)))))
3902 ;; Search contents of buffer defined by one of Viper's motion commands.
3903 ;; Repeatable via `n' and `N'.
3904 (defun viper-buffer-search-enable (&optional c)
3905 (cond (c (setq viper-buffer-search-char c))
3906 ((null viper-buffer-search-char)
3907 ;; ?g acts as a default value for viper-buffer-search-char
3908 (setq viper-buffer-search-char ?g)))
3909 (define-key viper-vi-basic-map
3910 (cond ((viper-characterp viper-buffer-search-char)
3911 (char-to-string viper-buffer-search-char))
3912 (t (error "viper-buffer-search-char: wrong value type, %S"
3913 viper-buffer-search-char)))
3914 'viper-command-argument)
3915 (aset viper-exec-array viper-buffer-search-char 'viper-exec-buffer-search)
3916 (setq viper-prefix-commands
3917 (cons viper-buffer-search-char viper-prefix-commands)))
3919 ;; This is a Viper wrapper for isearch-forward.
3920 (defun viper-isearch-forward (arg)
3921 "Do incremental search forward."
3923 ;; emacs bug workaround
3924 (if (listp arg) (setq arg (car arg)))
3925 (viper-exec-form-in-emacs (list 'isearch-forward arg)))
3927 ;; This is a Viper wrapper for isearch-backward."
3928 (defun viper-isearch-backward (arg)
3929 "Do incremental search backward."
3931 ;; emacs bug workaround
3932 (if (listp arg) (setq arg (car arg)))
3933 (viper-exec-form-in-emacs (list 'isearch-backward arg)))
3936 ;; visiting and killing files, buffers
3938 (defun viper-switch-to-buffer ()
3939 "Switch to buffer in the current window."
3941 (let ((other-buffer (other-buffer (current-buffer)))
3944 (funcall viper-read-buffer-function
3945 "Switch to buffer in this window: " other-buffer))
3946 (switch-to-buffer buffer)))
3948 (defun viper-switch-to-buffer-other-window ()
3949 "Switch to buffer in another window."
3951 (let ((other-buffer (other-buffer (current-buffer)))
3954 (funcall viper-read-buffer-function
3955 "Switch to buffer in another window: " other-buffer))
3956 (switch-to-buffer-other-window buffer)))
3958 (defun viper-kill-buffer ()
3961 (let (buffer buffer-name)
3963 (funcall viper-read-buffer-function
3964 (format "Kill buffer (%s): "
3965 (buffer-name (current-buffer)))))
3967 (if (null buffer-name)
3969 (get-buffer buffer-name)))
3970 (if (null buffer) (error "`%s': No such buffer" buffer-name))
3971 (if (or (not (buffer-modified-p buffer))
3974 "Buffer `%s' is modified, are you sure you want to kill it? "
3976 (kill-buffer buffer)
3977 (error "Buffer not killed"))))
3983 (defsubst viper-yank (text)
3984 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
3986 (setq this-command 'yank))
3988 (defun viper-put-back (arg)
3989 "Put back after point/below line."
3991 (let ((val (viper-p-val arg))
3992 (text (if viper-use-register
3993 (cond ((viper-valid-register viper-use-register '(digit))
3995 (- viper-use-register ?1) 'do-not-rotate))
3996 ((viper-valid-register viper-use-register)
3997 (get-register (downcase viper-use-register)))
3998 (t (error viper-InvalidRegister viper-use-register)))
4000 sv-point chars-inserted lines-inserted)
4002 (if viper-use-register
4003 (let ((reg viper-use-register))
4004 (setq viper-use-register nil)
4005 (error viper-EmptyRegister reg))
4006 (error "Viper bell")))
4007 (setq viper-use-register nil)
4008 (if (viper-end-with-a-newline-p text)
4014 (beginning-of-line))
4015 (if (not (eolp)) (viper-forward-char-carefully)))
4016 (set-marker (viper-mark-marker) (point) (current-buffer))
4017 (viper-set-destructive-command
4018 (list 'viper-put-back val nil viper-use-register nil nil))
4019 (setq sv-point (point))
4020 (viper-loop val (viper-yank text))
4021 (setq chars-inserted (abs (- (point) sv-point))
4022 lines-inserted (abs (count-lines (point) sv-point)))
4023 (if (or (> chars-inserted viper-change-notification-threshold)
4024 (> lines-inserted viper-change-notification-threshold))
4025 (unless (viper-is-in-minibuffer)
4026 (message "Inserted %d character(s), %d line(s)"
4027 chars-inserted lines-inserted))))
4028 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4029 ;; newline; it leaves the cursor at the beginning when the text contains
4031 (if (viper-same-line (point) (mark))
4032 (or (= (point) (mark)) (viper-backward-char-carefully))
4033 (exchange-point-and-mark)
4035 (back-to-indentation)))
4036 (viper-deactivate-mark))
4038 (defun viper-Put-back (arg)
4039 "Put back at point/above line."
4041 (let ((val (viper-p-val arg))
4042 (text (if viper-use-register
4043 (cond ((viper-valid-register viper-use-register '(digit))
4045 (- viper-use-register ?1) 'do-not-rotate))
4046 ((viper-valid-register viper-use-register)
4047 (get-register (downcase viper-use-register)))
4048 (t (error viper-InvalidRegister viper-use-register)))
4050 sv-point chars-inserted lines-inserted)
4052 (if viper-use-register
4053 (let ((reg viper-use-register))
4054 (setq viper-use-register nil)
4055 (error viper-EmptyRegister reg))
4056 (error "Viper bell")))
4057 (setq viper-use-register nil)
4058 (if (viper-end-with-a-newline-p text) (beginning-of-line))
4059 (viper-set-destructive-command
4060 (list 'viper-Put-back val nil viper-use-register nil nil))
4061 (set-marker (viper-mark-marker) (point) (current-buffer))
4062 (setq sv-point (point))
4063 (viper-loop val (viper-yank text))
4064 (setq chars-inserted (abs (- (point) sv-point))
4065 lines-inserted (abs (count-lines (point) sv-point)))
4066 (if (or (> chars-inserted viper-change-notification-threshold)
4067 (> lines-inserted viper-change-notification-threshold))
4068 (unless (viper-is-in-minibuffer)
4069 (message "Inserted %d character(s), %d line(s)"
4070 chars-inserted lines-inserted))))
4071 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4072 ;; newline; it leaves the cursor at the beginning when the text contains
4074 (if (viper-same-line (point) (mark))
4075 (or (= (point) (mark)) (viper-backward-char-carefully))
4076 (exchange-point-and-mark)
4078 (back-to-indentation)))
4079 (viper-deactivate-mark))
4082 ;; Copy region to kill-ring.
4083 ;; If BEG and END do not belong to the same buffer, copy empty region.
4084 (defun viper-copy-region-as-kill (beg end)
4086 (copy-region-as-kill beg end)
4087 (error (copy-region-as-kill beg beg))))
4090 (defun viper-delete-char (arg)
4091 "Delete next character."
4093 (let ((val (viper-p-val arg))
4095 (viper-set-destructive-command
4096 (list 'viper-delete-char val nil nil nil nil))
4097 (if (and viper-ex-style-editing
4098 (> val (viper-chars-in-region (point) (viper-line-pos 'end))))
4099 (setq val (viper-chars-in-region (point) (viper-line-pos 'end))))
4100 (if (and viper-ex-style-motion (eolp))
4101 (if (bolp) (error "Viper bell") (setq val 0))) ; not bol---simply back 1 ch
4103 (viper-forward-char-carefully val)
4104 (setq end-del-pos (point)))
4105 (if viper-use-register
4107 (cond ((viper-valid-register viper-use-register '((Letter)))
4108 (viper-append-to-register
4109 (downcase viper-use-register) (point) end-del-pos))
4110 ((viper-valid-register viper-use-register)
4112 viper-use-register (point) end-del-pos nil))
4113 (t (error viper-InvalidRegister viper-use-register)))
4114 (setq viper-use-register nil)))
4117 (if viper-ex-style-motion
4118 (if (and (eolp) (not (bolp))) (backward-char 1)))
4121 (defun viper-delete-backward-char (arg)
4122 "Delete previous character. On reaching beginning of line, stop and beep."
4124 (let ((val (viper-p-val arg))
4126 (viper-set-destructive-command
4127 (list 'viper-delete-backward-char val nil nil nil nil))
4129 viper-ex-style-editing
4130 (> val (viper-chars-in-region (viper-line-pos 'start) (point))))
4131 (setq val (viper-chars-in-region (viper-line-pos 'start) (point))))
4133 (viper-backward-char-carefully val)
4134 (setq end-del-pos (point)))
4135 (if viper-use-register
4137 (cond ((viper-valid-register viper-use-register '(Letter))
4138 (viper-append-to-register
4139 (downcase viper-use-register) end-del-pos (point)))
4140 ((viper-valid-register viper-use-register)
4142 viper-use-register end-del-pos (point) nil))
4143 (t (error viper-InvalidRegister viper-use-register)))
4144 (setq viper-use-register nil)))
4145 (if (and (bolp) viper-ex-style-editing)
4147 (delete-char (- val) t)))
4150 (defun viper-del-backward-char-in-insert ()
4151 "Delete 1 char backwards while in insert mode."
4153 (if (and viper-ex-style-editing (bolp))
4155 ;; don't put on kill ring
4156 (delete-char -1 nil)))
4159 (defun viper-del-backward-char-in-replace ()
4160 "Delete one character in replace mode.
4161 If `viper-delete-backwards-in-replace' is t, then DEL key actually deletes
4162 characters. If it is nil, then the cursor just moves backwards, similarly
4163 to Vi. The variable `viper-ex-style-editing', if t, doesn't let the
4164 cursor move past the beginning of line."
4166 (cond (viper-delete-backwards-in-replace
4168 ;; don't put on kill ring
4169 (delete-char -1 nil))
4170 (viper-ex-style-editing
4175 ;; don't put on kill ring
4176 (delete-char -1 nil))))
4177 (viper-ex-style-editing
4182 (backward-char 1))))
4184 (defun viper-del-forward-char-in-insert ()
4185 "Delete 1 char forward if in insert or replace state."
4187 ;; don't put on kill ring
4188 (delete-char 1 nil))
4193 (defun viper-join-lines (arg)
4194 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4196 (let ((val (viper-P-val arg)))
4197 (viper-set-destructive-command
4198 (list 'viper-join-lines val nil nil nil nil))
4199 (viper-loop (if (null val) 1 (1- val))
4204 (delete-region (point) (1- (point)))
4206 ;; fixup-whitespace sometimes does not leave space
4207 ;; between objects, so we insert it as in Vi
4208 (or (looking-at " ")
4216 (defun viper-change (beg end)
4217 (if (markerp beg) (setq beg (marker-position beg)))
4218 (if (markerp end) (setq end (marker-position end)))
4219 ;; beg is sometimes (mark t), which may be nil
4220 (or beg (setq beg end))
4222 (viper-set-complex-command-for-undo)
4223 (if viper-use-register
4225 (copy-to-register viper-use-register beg end nil)
4226 (setq viper-use-register nil)))
4227 (viper-set-replace-overlay beg end)
4228 (setq last-command nil) ; separate repl text from prev kills
4230 (if (= (viper-replace-start) (point-max))
4231 (error "End of buffer"))
4233 (setq viper-last-replace-region
4234 (buffer-substring (viper-replace-start)
4235 (viper-replace-end)))
4237 ;; protect against error while inserting "@" and other disasters
4238 ;; (e.g., read-only buff)
4239 (condition-case conds
4240 (if (or viper-allow-multiline-replace-regions
4241 (viper-same-line (viper-replace-start)
4242 (viper-replace-end)))
4244 ;; tabs cause problems in replace, so untabify
4245 (goto-char (viper-replace-end))
4246 (insert-before-markers "@") ; put placeholder after the TAB
4247 (untabify (viper-replace-start) (point))
4248 ;; del @, don't put on kill ring
4251 (viper-set-replace-overlay-glyphs
4252 viper-replace-region-start-delimiter
4253 viper-replace-region-end-delimiter)
4254 ;; this move takes care of the last posn in the overlay, which
4255 ;; has to be shifted because of insert. We can't simply insert
4256 ;; "$" before-markers because then overlay-start will shift the
4257 ;; beginning of the overlay in case we are replacing a single
4258 ;; character. This fixes the bug with `s' and `cl' commands.
4259 (viper-move-replace-overlay (viper-replace-start) (point))
4260 (goto-char (viper-replace-start))
4261 (viper-change-state-to-replace t))
4262 (kill-region (viper-replace-start)
4263 (viper-replace-end))
4264 (viper-hide-replace-overlay)
4265 (viper-change-state-to-insert))
4266 (error ;; make sure that the overlay doesn't stay.
4267 ;; go back to the original point
4268 (goto-char (viper-replace-start))
4269 (viper-hide-replace-overlay)
4270 (viper-message-conditions conds))))
4273 (defun viper-change-subr (beg end)
4274 ;; beg is sometimes (mark t), which may be nil
4275 (or beg (setq beg end))
4276 (if viper-use-register
4278 (copy-to-register viper-use-register beg end nil)
4279 (setq viper-use-register nil)))
4280 (kill-region beg end)
4281 (setq this-command 'viper-change)
4282 (viper-yank-last-insertion))
4284 (defun viper-toggle-case (arg)
4285 "Toggle character case."
4287 (let ((val (viper-p-val arg)) (c))
4288 (viper-set-destructive-command
4289 (list 'viper-toggle-case val nil nil nil nil))
4291 (setq c (following-char))
4293 (if (eq c (upcase c))
4294 (insert-char (downcase c) 1)
4295 (insert-char (upcase c) 1))
4296 (if (eolp) (backward-char 1))
4297 (setq val (1- val)))))
4302 (defun viper-query-replace ()
4304 If a null string is supplied as the string to be replaced,
4305 the query replace mode will toggle between string replace
4306 and regexp replace."
4309 (setq str (viper-read-string-with-history
4310 (if viper-re-query-replace "Query replace regexp: "
4313 'viper-replace1-history
4314 (car viper-replace1-history) ; default
4316 (if (string= str "")
4318 (setq viper-re-query-replace (not viper-re-query-replace))
4319 (message "Query replace mode changed to %s"
4320 (if viper-re-query-replace "regexp replace"
4322 (if viper-re-query-replace
4323 (query-replace-regexp
4325 (viper-read-string-with-history
4326 (format-message "Query replace regexp `%s' with: " str)
4328 'viper-replace1-history
4329 (car viper-replace1-history) ; default
4333 (viper-read-string-with-history
4334 (format-message "Query replace `%s' with: " str)
4336 'viper-replace1-history
4337 (car viper-replace1-history) ; default
4343 (defun viper-mark-beginning-of-buffer ()
4344 "Mark beginning of buffer."
4347 (goto-char (point-min))
4348 (exchange-point-and-mark)
4349 (message "Mark set at the beginning of buffer"))
4351 (defun viper-mark-end-of-buffer ()
4352 "Mark end of buffer."
4355 (goto-char (point-max))
4356 (exchange-point-and-mark)
4357 (message "Mark set at the end of buffer"))
4359 (defun viper-mark-point ()
4360 "Set mark at point of buffer."
4362 (let ((char (read-char)))
4363 (cond ((and (<= ?a char) (<= char ?z))
4364 (point-to-register (viper-int-to-char (1+ (- char ?a)))))
4365 ((viper= char ?<) (viper-mark-beginning-of-buffer))
4366 ((viper= char ?>) (viper-mark-end-of-buffer))
4367 ((viper= char ?.) (viper-set-mark-if-necessary))
4368 ((viper= char ?,) (viper-cycle-through-mark-ring))
4369 ((viper= char ?^) (push-mark viper-saved-mark t t))
4370 ((viper= char ?D) (mark-defun))
4371 (t (error "Viper bell"))
4374 ;; Algorithm: If first invocation of this command save mark on ring, goto
4375 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4376 ;; making it into the new mark, M1.
4377 ;; Push this mark back and set mark to the original point position, p1.
4378 ;; So, if you hit '' or `` then you can return to p1.
4380 ;; If repeated command, pop top elt from the ring into mark and
4381 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4382 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4383 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4385 (defun viper-cycle-through-mark-ring ()
4386 "Visit previous locations on the mark ring.
4387 One can use \\=`\\=` and \\='\\=' to temporarily jump 1 step back."
4388 (let* ((sv-pt (point)))
4389 ;; if repeated `m,' command, pop the previously saved mark.
4390 ;; Prev saved mark is actually prev saved point. It is used if the
4391 ;; user types `` or '' and is discarded
4392 ;; from the mark ring by the next `m,' command.
4393 ;; In any case, go to the previous or previously saved mark.
4394 ;; Then push the current mark (popped off the ring) and set current
4395 ;; point to be the mark. Current pt as mark is discarded by the next
4397 (if (eq last-command 'viper-cycle-through-mark-ring)
4399 ;; save current mark if the first iteration
4400 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4402 (push-mark (mark t) t)) )
4404 (set-mark-command 1)
4405 ;; don't duplicate mark on the ring
4406 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4408 (viper-deactivate-mark)
4409 (setq this-command 'viper-cycle-through-mark-ring)
4413 (defun viper-goto-mark (arg)
4416 (let ((char (read-char))
4417 (com (viper-getcom arg)))
4418 (viper-goto-mark-subr char com nil)))
4420 (defun viper-goto-mark-and-skip-white (arg)
4421 "Go to mark and skip to first non-white character on line."
4423 (let ((char (read-char))
4424 (com (viper-getCom arg)))
4425 (viper-goto-mark-subr char com t)))
4427 (defun viper-goto-mark-subr (char com skip-white)
4430 (error "Empty buffer")
4432 (cond ((viper-valid-register char '(letter))
4433 (let* ((buff (current-buffer))
4434 (reg (viper-int-to-char (1+ (- char ?a))))
4435 (text-marker (get-register reg)))
4436 ;; If marker points to file that had markers set (and those markers
4437 ;; were saved (as e.g., in session.el), then restore those markers
4438 (if (and (consp text-marker)
4439 (eq (car text-marker) 'file-query)
4440 (or (find-buffer-visiting (nth 1 text-marker))
4441 (y-or-n-p (format "Visit file %s again? "
4442 (nth 1 text-marker)))))
4444 (find-file (nth 1 text-marker))
4445 (when (and (<= (nth 2 text-marker) (point-max))
4446 (<= (point-min) (nth 2 text-marker)))
4447 (setq text-marker (copy-marker (nth 2 text-marker)))
4448 (set-register reg text-marker))))
4449 (if com (viper-move-marker-locally 'viper-com-point (point)))
4450 (if (not (viper-valid-marker text-marker))
4451 (error viper-EmptyTextmarker char))
4452 (if (and (viper-same-line (point) viper-last-jump)
4453 (= (point) viper-last-jump-ignore))
4454 (push-mark viper-last-jump t)
4455 (push-mark nil t)) ; no msg
4456 (viper-register-to-point reg)
4457 (setq viper-last-jump (point-marker))
4459 (back-to-indentation)
4460 (setq viper-last-jump-ignore (point))))
4462 (if (equal buff (current-buffer))
4463 (viper-execute-com (if skip-white
4464 'viper-goto-mark-and-skip-white
4467 (switch-to-buffer buff)
4468 (goto-char viper-com-point)
4469 (viper-change-state-to-vi)
4470 (error "Viper bell")))))
4471 ((and (not skip-white) (viper= char ?`))
4472 (if com (viper-move-marker-locally 'viper-com-point (point)))
4473 (if (and (viper-same-line (point) viper-last-jump)
4474 (= (point) viper-last-jump-ignore))
4475 (goto-char viper-last-jump))
4476 (if (null (mark t)) (error "Mark is not set in this buffer"))
4477 (if (= (point) (mark t)) (pop-mark))
4478 (exchange-point-and-mark)
4479 (setq viper-last-jump (point-marker)
4480 viper-last-jump-ignore 0)
4481 (if com (viper-execute-com 'viper-goto-mark nil com)))
4482 ((and skip-white (viper= char ?'))
4483 (if com (viper-move-marker-locally 'viper-com-point (point)))
4484 (if (and (viper-same-line (point) viper-last-jump)
4485 (= (point) viper-last-jump-ignore))
4486 (goto-char viper-last-jump))
4487 (if (= (point) (mark t)) (pop-mark))
4488 (exchange-point-and-mark)
4489 (setq viper-last-jump (point))
4490 (back-to-indentation)
4491 (setq viper-last-jump-ignore (point))
4492 (if com (viper-execute-com 'viper-goto-mark-and-skip-white nil com)))
4493 (t (error viper-InvalidTextmarker char))))
4495 (defun viper-insert-tab ()
4499 (defun viper-exchange-point-and-mark ()
4501 (exchange-point-and-mark)
4502 (back-to-indentation))
4504 ;; Input Mode Indentation
4506 (define-obsolete-function-alias 'viper-looking-back 'looking-back "24.4")
4509 (defun viper-forward-indent ()
4510 "Indent forward -- `C-t' in Vi."
4513 (indent-to (+ (current-column) viper-shift-width)))
4515 (defun viper-backward-indent ()
4516 "Backtab, `C-d' in Vi."
4519 (let ((p (point)) (c (current-column)) bol (indent t))
4520 (if (looking-back "[0^]" (1- (point)))
4522 (if (eq ?^ (preceding-char))
4523 (setq viper-preserve-indent t))
4527 (setq bol (point-at-bol))
4528 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4529 (delete-region (point) p)
4531 (indent-to (- c viper-shift-width)))
4532 (if (or (bolp) (looking-back "[^ \t]" (1- (point))))
4533 (setq viper-cted nil)))))
4536 (defun viper-indent-line (col)
4537 (if viper-auto-indent
4540 (if (and viper-electric-mode
4541 (not (memq major-mode '(fundamental-mode
4543 paragraph-indent-text-mode))))
4544 (indent-according-to-mode)
4548 (defun viper-autoindent ()
4549 "Auto Indentation, Vi-style."
4551 (let ((col (current-indentation)))
4552 (if abbrev-mode (expand-abbrev))
4553 (if viper-preserve-indent
4554 (setq viper-preserve-indent nil)
4555 (setq viper-current-indent col))
4556 ;; don't leave whitespace lines around
4557 (if (memq last-command
4559 viper-open-line viper-Open-line
4560 viper-replace-state-exit-cmd))
4561 (indent-to-left-margin))
4562 ;; use \n instead of newline, or else <Return> will move the insert point
4565 (viper-indent-line viper-current-indent)
4569 ;; Viewing registers
4571 (defun viper-ket-function (arg)
4572 "Function called by ], the ket. View registers and call ]]."
4574 (let ((reg (read-char)))
4575 (cond ((viper-valid-register reg '(letter Letter))
4576 (view-register (downcase reg)))
4577 ((viper-valid-register reg '(digit))
4578 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4579 (with-output-to-temp-buffer " *viper-info*"
4580 (princ (format "Register %c contains the string:\n" reg))
4584 (viper-next-heading arg))
4586 viper-InvalidRegister reg)))))
4588 (defun viper-brac-function (arg)
4589 "Function called by [, the brac. View textmarkers and call [[."
4591 (let ((reg (read-char)))
4592 (cond ((viper= ?\[ reg)
4593 (viper-prev-heading arg))
4595 (viper-heading-end arg))
4596 ((viper-valid-register reg '(letter))
4597 (let* ((val (get-register (viper-int-to-char (1+ (- reg ?a)))))
4598 (buf (if (not (markerp val))
4599 (error viper-EmptyTextmarker reg)
4600 (marker-buffer val)))
4601 (pos (marker-position val))
4602 line-no text (s pos) (e pos))
4603 (with-output-to-temp-buffer " *viper-info*"
4606 (with-current-buffer buf
4607 (setq line-no (1+ (count-lines (point-min) val)))
4610 (if (re-search-backward "[^ \t]" nil t)
4611 (setq s (point-at-bol)))
4614 (if (re-search-forward "[^ \t]" nil t)
4618 (setq text (buffer-substring s e))
4619 (setq text (format "%s<%c>%s"
4620 (substring text 0 (- pos s))
4621 reg (substring text (- pos s)))))
4624 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4625 reg (buffer-name buf) line-no))
4626 (princ (format "Here is some text around %c:\n\n %s"
4628 (princ (format-message viper-EmptyTextmarker reg))))
4630 (t (error viper-InvalidTextmarker reg)))))
4634 (defun viper-delete-backward-word (arg)
4635 "Delete previous word."
4640 (delete-region (point) (mark t))
4645 ;; Get viper standard value of SYMBOL. If symbol is customized, get its
4646 ;; standard value. Otherwise, get the value saved in the alist STORAGE. If
4647 ;; STORAGE is nil, use viper-saved-user-settings.
4648 (defun viper-standard-value (symbol &optional storage)
4649 (or (eval (car (get symbol 'customized-value)))
4650 (eval (car (get symbol 'saved-value)))
4651 (nth 1 (assoc symbol (or storage viper-saved-user-settings)))))
4655 (defun viper-set-expert-level (&optional dont-change-unless)
4656 "Sets the expert level for a Viper user.
4657 Can be called interactively to change (temporarily or permanently) the
4658 current expert level.
4660 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4661 the level should not be changed, unless its current value is
4662 meaningless (i.e., not one of 1,2,3,4,5).
4664 User level determines the setting of Viper variables that are most
4665 sensitive for VI-style look-and-feel."
4669 (if (not (natnump viper-expert-level)) (setq viper-expert-level 0))
4671 (save-window-excursion
4672 (delete-other-windows)
4673 ;; if 0 < viper-expert-level < viper-max-expert-level
4674 ;; & dont-change-unless = t -- use it; else ask
4675 (viper-ask-level dont-change-unless))
4677 (setq viper-always t
4678 viper-ex-style-motion t
4679 viper-ex-style-editing t
4680 viper-want-ctl-h-help nil)
4682 (cond ((eq viper-expert-level 1) ; novice or beginner
4683 (global-set-key ; in emacs-state
4685 (if (viper-window-display-p) 'viper-iconify 'suspend-emacs))
4686 (setq viper-no-multiple-ESC t
4688 viper-vi-style-in-minibuffer t
4689 viper-search-wrap-around t
4690 viper-electric-mode nil
4691 viper-want-emacs-keys-in-vi nil
4692 viper-want-emacs-keys-in-insert nil))
4694 ((and (> viper-expert-level 1) (< viper-expert-level 5))
4695 ;; intermediate to guru
4696 (setq viper-no-multiple-ESC (if (viper-window-display-p)
4698 viper-electric-mode t
4699 viper-want-emacs-keys-in-vi t
4700 viper-want-emacs-keys-in-insert (> viper-expert-level 2))
4702 (if (eq viper-expert-level 4) ; respect user's ex-style motion
4703 ; and viper-no-multiple-ESC
4706 viper-ex-style-editing
4707 (viper-standard-value 'viper-ex-style-editing)
4708 viper-ex-style-motion
4709 (viper-standard-value 'viper-ex-style-motion))
4710 (setq viper-ex-style-motion
4711 (viper-standard-value 'viper-ex-style-motion)
4712 viper-ex-style-editing
4713 (viper-standard-value 'viper-ex-style-editing)
4715 (viper-standard-value 'viper-re-search)
4716 viper-no-multiple-ESC
4717 (viper-standard-value 'viper-no-multiple-ESC)))))
4720 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4721 ;; user toggle the values of variables.
4722 (t (setq-default viper-ex-style-editing
4723 (viper-standard-value 'viper-ex-style-editing)
4724 viper-ex-style-motion
4725 (viper-standard-value 'viper-ex-style-motion))
4726 (setq viper-want-ctl-h-help
4727 (viper-standard-value 'viper-want-ctl-h-help)
4729 (viper-standard-value 'viper-always)
4730 viper-no-multiple-ESC
4731 (viper-standard-value 'viper-no-multiple-ESC)
4732 viper-ex-style-motion
4733 (viper-standard-value 'viper-ex-style-motion)
4734 viper-ex-style-editing
4735 (viper-standard-value 'viper-ex-style-editing)
4737 (viper-standard-value 'viper-re-search)
4739 (viper-standard-value 'viper-electric-mode)
4740 viper-want-emacs-keys-in-vi
4741 (viper-standard-value 'viper-want-emacs-keys-in-vi)
4742 viper-want-emacs-keys-in-insert
4743 (viper-standard-value 'viper-want-emacs-keys-in-insert))))
4745 (viper-set-mode-vars-for viper-current-state)
4746 (if (or viper-always
4747 (and (> viper-expert-level 0) (> 5 viper-expert-level)))
4751 ;; Ask user expert level.
4752 (defun viper-ask-level (dont-change-unless)
4753 (let ((ask-buffer " *viper-ask-level*")
4754 level-changed repeated)
4755 (save-window-excursion
4756 (switch-to-buffer ask-buffer)
4758 (while (or (> viper-expert-level viper-max-expert-level)
4759 (< viper-expert-level 1)
4760 (null dont-change-unless))
4764 (message "Invalid user level")
4767 (setq dont-change-unless t
4769 (insert (substitute-command-keys "
4770 Please specify your level of familiarity with the venomous VI PERil
4771 \(and the VI Plan for Emacs Rescue).
4772 You can change it at any time by typing `\\[viper-set-expert-level]'
4774 1 -- BEGINNER: Almost all Emacs features are suppressed.
4775 Feels almost like straight Vi. File name completion and
4776 command history in the minibuffer are thrown in as a bonus.
4777 To use Emacs productively, you must reach level 3 or higher.
4778 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4779 so most Emacs commands can be used when Viper is in Vi state.
4780 Good progress---you are well on the way to level 3!
4781 3 -- GRAND MASTER: Like 2, but most Emacs commands are available also
4782 in Viper's insert state.
4783 4 -- GURU: Like 3, but user settings are respected for viper-no-multiple-ESC,
4784 viper-ex-style-motion, viper-ex-style-editing, and
4785 viper-re-search variables. Adjust these settings to your taste.
4786 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4787 viper-electric-mode, viper-want-ctl-h-help, viper-want-emacs-keys-in-vi,
4788 and viper-want-emacs-keys-in-insert. Adjust these to your taste.
4790 Please, specify your level now: "))
4792 (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
4795 ;; tell the user if level was changed
4799 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4800 viper-expert-level))
4801 (if (y-or-n-p "Do you wish to make this change permanent? ")
4802 ;; save the setting for viper-expert-level
4805 (format "Saving user level %d ..." viper-expert-level)
4806 viper-custom-file-name))
4808 (bury-buffer) ; remove ask-buffer from screen
4818 ;; FIXME Use register-read-with-preview?
4819 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4820 (defun viper-register-to-point (char &optional enforce-buffer)
4821 "Like `jump-to-register', but switches to another buffer in another window."
4822 (interactive "cViper register to point: ")
4823 (let ((val (get-register char)))
4825 ((and (fboundp 'frame-configuration-p)
4826 (frame-configuration-p val))
4827 (set-frame-configuration val))
4828 ((window-configuration-p val)
4829 (set-window-configuration val))
4830 ((viper-valid-marker val)
4831 (if (and enforce-buffer
4832 (not (equal (current-buffer) (marker-buffer val))))
4833 (error (concat viper-EmptyTextmarker " in this buffer")
4834 (viper-int-to-char (1- (+ char ?a)))))
4835 (pop-to-buffer (marker-buffer val))
4837 ((and (consp val) (eq (car val) 'file))
4838 (find-file (cdr val)))
4840 (error viper-EmptyTextmarker (viper-int-to-char (1- (+ char ?a))))))))
4843 (defun viper-save-kill-buffer ()
4844 "Save then kill current buffer."
4846 (if (< viper-expert-level 2)
4847 (save-buffers-kill-emacs)
4848 (if (buffer-modified-p) (save-buffer))
4849 (kill-buffer (current-buffer))))
4855 (defun viper-submit-report ()
4856 "Submit bug report on Viper."
4858 (let ((reporter-prompt-for-summary-p t)
4859 (viper-device-type (viper-device-type))
4860 color-display-p frame-parameters
4861 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4862 varlist salutation window-config)
4864 ;; If mode info is needed, add variable to `let' and then set it below,
4865 ;; like we did with color-display-p.
4866 (setq color-display-p (if (viper-window-display-p)
4867 (viper-color-display-p)
4869 minibuffer-vi-face (if (viper-has-face-support-p)
4870 (viper-get-face viper-minibuffer-vi-face)
4872 minibuffer-insert-face (if (viper-has-face-support-p)
4874 viper-minibuffer-insert-face)
4876 minibuffer-emacs-face (if (viper-has-face-support-p)
4878 viper-minibuffer-emacs-face)
4880 frame-parameters (if (fboundp 'frame-parameters)
4881 (frame-parameters (selected-frame))))
4883 (setq varlist (list 'viper-vi-minibuffer-minor-mode
4884 'viper-insert-minibuffer-minor-mode
4885 'viper-vi-intercept-minor-mode
4886 'viper-vi-local-user-minor-mode
4887 'viper-vi-kbd-minor-mode
4888 'viper-vi-global-user-minor-mode
4889 'viper-vi-state-modifier-minor-mode
4890 'viper-vi-diehard-minor-mode
4891 'viper-vi-basic-minor-mode
4892 'viper-replace-minor-mode
4893 'viper-insert-intercept-minor-mode
4894 'viper-insert-local-user-minor-mode
4895 'viper-insert-kbd-minor-mode
4896 'viper-insert-global-user-minor-mode
4897 'viper-insert-state-modifier-minor-mode
4898 'viper-insert-diehard-minor-mode
4899 'viper-insert-basic-minor-mode
4900 'viper-emacs-intercept-minor-mode
4901 'viper-emacs-local-user-minor-mode
4902 'viper-emacs-kbd-minor-mode
4903 'viper-emacs-global-user-minor-mode
4904 'viper-emacs-state-modifier-minor-mode
4905 'viper-automatic-iso-accents
4906 'viper-special-input-method
4907 'viper-want-emacs-keys-in-insert
4908 'viper-want-emacs-keys-in-vi
4909 'viper-keep-point-on-undo
4910 'viper-no-multiple-ESC
4911 'viper-electric-mode
4913 'viper-want-ctl-h-help
4914 'viper-ex-style-editing
4915 'viper-delete-backwards-in-replace
4916 'viper-vi-style-in-minibuffer
4917 'viper-vi-state-hook
4918 'viper-insert-state-hook
4919 'viper-replace-state-hook
4920 'viper-emacs-state-hook
4921 'ex-cycle-other-window
4922 'ex-cycle-through-non-files
4929 'minibuffer-insert-face
4930 'minibuffer-emacs-face
4933 Congratulations! You may have unearthed a bug in Viper!
4934 Please mail a concise, accurate summary of the problem to the address above.
4936 -------------------------------------------------------------------")
4937 (setq window-config (current-window-configuration))
4938 (with-output-to-temp-buffer " *viper-info*"
4939 (switch-to-buffer " *viper-info*")
4940 (delete-other-windows)
4942 PLEASE FOLLOW THESE PROCEDURES
4943 ------------------------------
4945 Before reporting a bug, please verify that it is related to Viper, and is
4946 not caused by other packages you are using.
4948 Don't report compilation warnings, unless you are certain that there is a
4949 problem. These warnings are normal and unavoidable.
4951 Please note that users should not modify variables and keymaps other than
4952 those advertised in the manual. Such `customization' is likely to crash
4953 Viper, as it would any other improperly customized Emacs package.
4955 If you are reporting an error message received while executing one of the
4956 Viper commands, type:
4958 M-x set-variable <Return> debug-on-error <Return> t <Return>
4960 Then reproduce the error. The above command will cause Emacs to produce a
4961 back trace of the execution that leads to the error. Please include this
4962 trace in your bug report.
4964 If you believe that one of Viper's commands goes into an infinite loop
4965 \(e.g., Emacs freezes), type:
4967 M-x set-variable <Return> debug-on-quit <Return> t <Return>
4969 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
4970 the current command. Include the resulting back trace in the bug report.
4972 Mail anyway (y or n)? ")
4973 (if (y-or-n-p "Mail anyway? ")
4975 (set-window-configuration window-config)
4976 (error "Bug report aborted")))
4979 (set-window-configuration window-config)
4981 (reporter-submit-bug-report
4982 "kifer@cs.stonybrook.edu, bug-gnu-emacs@gnu.org"
4985 nil 'delete-other-windows
4991 ;;; viper-cmd.el ends here