2006-08-25 Michael Kifer <kifer@cs.stonybrook.edu>
[emacs.git] / lisp / emulation / viper-cmd.el
blobaf757a2a55c6b1e18d2c5922c5b518869aa50a51
1 ;;; viper-cmd.el --- Vi command support for Viper
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 Free Software Foundation, Inc.
6 ;; 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 2, or (at your option)
13 ;; 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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;;; Code:
29 (provide 'viper-cmd)
30 (require 'advice)
32 ;; Compiler pacifier
33 (defvar viper-minibuffer-current-face)
34 (defvar viper-minibuffer-insert-face)
35 (defvar viper-minibuffer-vi-face)
36 (defvar viper-minibuffer-emacs-face)
37 (defvar viper-always)
38 (defvar viper-mode-string)
39 (defvar viper-custom-file-name)
40 (defvar viper--key-maps)
41 (defvar viper--intercept-key-maps)
42 (defvar iso-accents-mode)
43 (defvar quail-mode)
44 (defvar quail-current-str)
45 (defvar zmacs-region-stays)
46 (defvar mark-even-if-inactive)
47 (defvar init-message)
48 (defvar initial)
49 (defvar undo-beg-posn)
50 (defvar undo-end-posn)
52 ;; loading happens only in non-interactive compilation
53 ;; in order to spare non-viperized emacs from being viperized
54 (if noninteractive
55 (eval-when-compile
56 (let ((load-path (cons (expand-file-name ".") load-path)))
57 (or (featurep 'viper-util)
58 (load "viper-util.el" nil nil 'nosuffix))
59 (or (featurep 'viper-keym)
60 (load "viper-keym.el" nil nil 'nosuffix))
61 (or (featurep 'viper-mous)
62 (load "viper-mous.el" nil nil 'nosuffix))
63 (or (featurep 'viper-macs)
64 (load "viper-macs.el" nil nil 'nosuffix))
65 (or (featurep 'viper-ex)
66 (load "viper-ex.el" nil nil 'nosuffix))
67 )))
68 ;; end pacifier
71 (require 'viper-util)
72 (require 'viper-keym)
73 (require 'viper-mous)
74 (require 'viper-macs)
75 (require 'viper-ex)
79 ;; Generic predicates
81 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
83 ;; generate test functions
84 ;; given symbol foo, foo-p is the test function, foos is the set of
85 ;; Viper command keys
86 ;; (macroexpand '(viper-test-com-defun foo))
87 ;; (defun foo-p (com) (consp (memq com foos)))
89 (defmacro viper-test-com-defun (name)
90 (let* ((snm (symbol-name name))
91 (nm-p (intern (concat snm "-p")))
92 (nms (intern (concat snm "s"))))
93 `(defun ,nm-p (com)
94 (consp (viper-memq-char com ,nms)
95 ))))
97 ;; Variables for defining VI commands
99 ;; Modifying commands that can be prefixes to movement commands
100 (defvar viper-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
101 ;; define viper-prefix-command-p
102 (viper-test-com-defun viper-prefix-command)
104 ;; Commands that are pairs eg. dd. r and R here are a hack
105 (defconst viper-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
106 ;; define viper-charpair-command-p
107 (viper-test-com-defun viper-charpair-command)
109 (defconst viper-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
110 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
111 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
112 ?\; ?, ?0 ?? ?/ ?\ ?\C-m
113 space return
114 delete backspace
116 "Movement commands")
117 ;; define viper-movement-command-p
118 (viper-test-com-defun viper-movement-command)
120 ;; Vi digit commands
121 (defconst viper-digit-commands '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
123 ;; define viper-digit-command-p
124 (viper-test-com-defun viper-digit-command)
126 ;; Commands that can be repeated by . (dotted)
127 (defconst viper-dotable-commands '(?c ?d ?C ?s ?S ?D ?> ?<))
128 ;; define viper-dotable-command-p
129 (viper-test-com-defun viper-dotable-command)
131 ;; Commands that can follow a #
132 (defconst viper-hash-commands '(?c ?C ?g ?q ?s))
133 ;; define viper-hash-command-p
134 (viper-test-com-defun viper-hash-command)
136 ;; Commands that may have registers as prefix
137 (defconst viper-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
138 ;; define viper-regsuffix-command-p
139 (viper-test-com-defun viper-regsuffix-command)
141 (defconst viper-vi-commands (append viper-movement-commands
142 viper-digit-commands
143 viper-dotable-commands
144 viper-charpair-commands
145 viper-hash-commands
146 viper-prefix-commands
147 viper-regsuffix-commands)
148 "The list of all commands in Vi-state.")
149 ;; define viper-vi-command-p
150 (viper-test-com-defun viper-vi-command)
152 ;; Where viper saves mark. This mark is resurrected by m^
153 (defvar viper-saved-mark nil)
155 ;; Contains user settings for vars affected by viper-set-expert-level function.
156 ;; Not a user option.
157 (defvar viper-saved-user-settings nil)
161 ;;; CODE
163 ;; sentinels
165 ;; Runs viper-after-change-functions inside after-change-functions
166 (defun viper-after-change-sentinel (beg end len)
167 (run-hook-with-args 'viper-after-change-functions beg end len))
169 ;; Runs viper-before-change-functions inside before-change-functions
170 (defun viper-before-change-sentinel (beg end)
171 (run-hook-with-args 'viper-before-change-functions beg end))
173 (defsubst viper-post-command-sentinel ()
174 (run-hooks 'viper-post-command-hooks)
175 (if (eq viper-current-state 'vi-state)
176 (viper-restore-cursor-color 'after-insert-mode)))
178 (defsubst viper-pre-command-sentinel ()
179 (run-hooks 'viper-pre-command-hooks))
181 ;; Needed so that Viper will be able to figure the last inserted
182 ;; chunk of text with reasonable accuracy.
183 (defsubst viper-insert-state-post-command-sentinel ()
184 (if (and (memq viper-current-state '(insert-state replace-state))
185 viper-insert-point
186 (>= (point) viper-insert-point))
187 (setq viper-last-posn-while-in-insert-state (point-marker)))
188 (or (viper-overlay-p viper-replace-overlay)
189 (progn
190 (viper-set-replace-overlay (point-min) (point-min))
191 (viper-hide-replace-overlay)))
192 (if (eq viper-current-state 'insert-state)
193 (let ((has-saved-cursor-color-in-insert-mode
194 (stringp (viper-get-saved-cursor-color-in-insert-mode))))
195 (or has-saved-cursor-color-in-insert-mode
196 (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
197 ;; save current color, if not already saved
198 (viper-save-cursor-color 'before-insert-mode))
199 ;; set insert mode cursor color
200 (viper-change-cursor-color viper-insert-state-cursor-color)))
201 (if (and viper-emacs-state-cursor-color (eq viper-current-state 'emacs-state))
202 (let ((has-saved-cursor-color-in-emacs-mode
203 (stringp (viper-get-saved-cursor-color-in-emacs-mode))))
204 (or has-saved-cursor-color-in-emacs-mode
205 (string= (viper-get-cursor-color) viper-emacs-state-cursor-color)
206 ;; save current color, if not already saved
207 (viper-save-cursor-color 'before-emacs-mode))
208 ;; set emacs mode cursor color
209 (viper-change-cursor-color viper-emacs-state-cursor-color)))
211 (if (and (memq this-command '(dabbrev-expand hippie-expand))
212 (integerp viper-pre-command-point)
213 (markerp viper-insert-point)
214 (marker-position viper-insert-point)
215 (> viper-insert-point viper-pre-command-point))
216 (viper-move-marker-locally viper-insert-point viper-pre-command-point))
219 (defsubst viper-preserve-cursor-color ()
220 (or (memq this-command '(self-insert-command
221 viper-del-backward-char-in-insert
222 viper-del-backward-char-in-replace
223 viper-delete-backward-char
224 viper-join-lines
225 viper-delete-char))
226 (memq (viper-event-key last-command-event)
227 '(up down left right (meta f) (meta b)
228 (control n) (control p) (control f) (control b)))))
230 (defsubst viper-insert-state-pre-command-sentinel ()
231 (or (viper-preserve-cursor-color)
232 (viper-restore-cursor-color 'after-insert-mode))
233 (if (and (memq this-command '(dabbrev-expand hippie-expand))
234 (markerp viper-insert-point)
235 (marker-position viper-insert-point))
236 (setq viper-pre-command-point (marker-position viper-insert-point))))
238 (defsubst viper-R-state-post-command-sentinel ()
239 ;; Restoring cursor color is needed despite
240 ;; viper-replace-state-pre-command-sentinel: When you jump to another buffer
241 ;; in another frame, the pre-command hook won't change cursor color to
242 ;; default in that other frame. So, if the second frame cursor was red and
243 ;; we set the point outside the replacement region, then the cursor color
244 ;; will remain red. Restoring the default, below, prevents this.
245 (if (and (<= (viper-replace-start) (point))
246 (<= (point) (viper-replace-end)))
247 (viper-change-cursor-color viper-replace-overlay-cursor-color)
248 (viper-restore-cursor-color 'after-replace-mode)
251 ;; to speed up, don't change cursor color before self-insert
252 ;; and common move commands
253 (defsubst viper-replace-state-pre-command-sentinel ()
254 (or (viper-preserve-cursor-color)
255 (viper-restore-cursor-color 'after-replace-mode)))
258 ;; Make sure we don't delete more than needed.
259 ;; This is executed at viper-last-posn-in-replace-region
260 (defsubst viper-trim-replace-chars-to-delete-if-necessary ()
261 (setq viper-replace-chars-to-delete
262 (max 0
263 (min viper-replace-chars-to-delete
264 ;; Don't delete more than to the end of repl overlay
265 (viper-chars-in-region
266 (viper-replace-end) viper-last-posn-in-replace-region)
267 ;; point is viper-last-posn-in-replace-region now
268 ;; So, this limits deletion to the end of line
269 (viper-chars-in-region (point) (viper-line-pos 'end))
270 ))))
273 (defun viper-replace-state-post-command-sentinel ()
274 ;; Restoring cursor color is needed despite
275 ;; viper-replace-state-pre-command-sentinel: When one jumps to another buffer
276 ;; in another frame, the pre-command hook won't change cursor color to
277 ;; default in that other frame. So, if the second frame cursor was red and
278 ;; we set the point outside the replacement region, then the cursor color
279 ;; will remain red. Restoring the default, below, fixes this problem.
281 ;; We optimize for some commands, like self-insert-command,
282 ;; viper-delete-backward-char, etc., since they either don't change
283 ;; cursor color or, if they terminate replace mode, the color will be changed
284 ;; in viper-finish-change
285 (or (viper-preserve-cursor-color)
286 (viper-restore-cursor-color 'after-replace-mode))
287 (cond
288 ((eq viper-current-state 'replace-state)
289 ;; delete characters to compensate for inserted chars.
290 (let ((replace-boundary (viper-replace-end)))
291 (save-excursion
292 (goto-char viper-last-posn-in-replace-region)
293 (viper-trim-replace-chars-to-delete-if-necessary)
294 (delete-char viper-replace-chars-to-delete)
295 (setq viper-replace-chars-to-delete 0)
296 ;; terminate replace mode if reached replace limit
297 (if (= viper-last-posn-in-replace-region (viper-replace-end))
298 (viper-finish-change)))
300 (if (viper-pos-within-region
301 (point) (viper-replace-start) replace-boundary)
302 (progn
303 ;; the state may have changed in viper-finish-change above
304 (if (eq viper-current-state 'replace-state)
305 (viper-change-cursor-color viper-replace-overlay-cursor-color))
306 (setq viper-last-posn-in-replace-region (point-marker))))
308 ;; terminate replace mode if changed Viper states.
309 (t (viper-finish-change))))
312 ;; changing mode
314 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
315 (defun viper-change-state (new-state)
316 ;; Keep viper-post/pre-command-hooks fresh.
317 ;; We remove then add viper-post/pre-command-sentinel since it is very
318 ;; desirable that viper-pre-command-sentinel is the last hook and
319 ;; viper-post-command-sentinel is the first hook.
321 (viper-cond-compile-for-xemacs-or-emacs
322 ;; xemacs
323 (progn
324 (make-local-hook 'viper-after-change-functions)
325 (make-local-hook 'viper-before-change-functions)
326 (make-local-hook 'viper-post-command-hooks)
327 (make-local-hook 'viper-pre-command-hooks))
328 nil ; emacs
331 (remove-hook 'post-command-hook 'viper-post-command-sentinel)
332 (add-hook 'post-command-hook 'viper-post-command-sentinel)
333 (remove-hook 'pre-command-hook 'viper-pre-command-sentinel)
334 (add-hook 'pre-command-hook 'viper-pre-command-sentinel t)
335 ;; These hooks will be added back if switching to insert/replace mode
336 (remove-hook 'viper-post-command-hooks
337 'viper-insert-state-post-command-sentinel 'local)
338 (remove-hook 'viper-pre-command-hooks
339 'viper-insert-state-pre-command-sentinel 'local)
340 (setq viper-intermediate-command nil)
341 (cond ((eq new-state 'vi-state)
342 (cond ((member viper-current-state '(insert-state replace-state))
344 ;; move viper-last-posn-while-in-insert-state
345 ;; This is a normal hook that is executed in insert/replace
346 ;; states after each command. In Vi/Emacs state, it does
347 ;; nothing. We need to execute it here to make sure that
348 ;; the last posn was recorded when we hit ESC.
349 ;; It may be left unrecorded if the last thing done in
350 ;; insert/repl state was dabbrev-expansion or abbrev
351 ;; expansion caused by hitting ESC
352 (viper-insert-state-post-command-sentinel)
354 (condition-case conds
355 (progn
356 (viper-save-last-insertion
357 viper-insert-point
358 viper-last-posn-while-in-insert-state)
359 (if viper-began-as-replace
360 (setq viper-began-as-replace nil)
361 ;; repeat insert commands if numerical arg > 1
362 (save-excursion
363 (viper-repeat-insert-command))))
364 (error
365 (viper-message-conditions conds)))
367 (if (> (length viper-last-insertion) 0)
368 (viper-push-onto-ring viper-last-insertion
369 'viper-insertion-ring))
371 (if viper-ESC-moves-cursor-back
372 (or (bolp) (viper-beginning-of-field) (backward-char 1))))
375 ;; insert or replace
376 ((memq new-state '(insert-state replace-state))
377 (if (memq viper-current-state '(emacs-state vi-state))
378 (viper-move-marker-locally 'viper-insert-point (point)))
379 (viper-move-marker-locally
380 'viper-last-posn-while-in-insert-state (point))
381 (add-hook 'viper-post-command-hooks
382 'viper-insert-state-post-command-sentinel t 'local)
383 (add-hook 'viper-pre-command-hooks
384 'viper-insert-state-pre-command-sentinel t 'local))
385 ) ; outermost cond
387 ;; Nothing needs to be done to switch to emacs mode! Just set some
388 ;; variables, which is already done in viper-change-state-to-emacs!
390 ;; ISO accents
391 ;; always turn off iso-accents-mode in vi-state, or else we won't be able to
392 ;; use the keys `,',^ , as they will do accents instead of Vi actions.
393 (cond ((eq new-state 'vi-state) (viper-set-iso-accents-mode nil));accents off
394 (viper-automatic-iso-accents (viper-set-iso-accents-mode t));accents on
395 (t (viper-set-iso-accents-mode nil)))
396 ;; Always turn off quail mode in vi state
397 (cond ((eq new-state 'vi-state) (viper-set-input-method nil)) ;intl input off
398 (viper-special-input-method (viper-set-input-method t)) ;intl input on
399 (t (viper-set-input-method nil)))
401 (setq viper-current-state new-state)
403 (viper-update-syntax-classes)
404 (viper-normalize-minor-mode-map-alist)
405 (viper-adjust-keys-for new-state)
406 (viper-set-mode-vars-for new-state)
407 (viper-refresh-mode-line)
412 (defun viper-adjust-keys-for (state)
413 "Make necessary adjustments to keymaps before entering STATE."
414 (cond ((memq state '(insert-state replace-state))
415 (if viper-auto-indent
416 (progn
417 (define-key viper-insert-basic-map "\C-m" 'viper-autoindent)
418 (if viper-want-emacs-keys-in-insert
419 ;; expert
420 (define-key viper-insert-basic-map "\C-j" nil)
421 ;; novice
422 (define-key viper-insert-basic-map "\C-j" 'viper-autoindent)))
423 (define-key viper-insert-basic-map "\C-m" nil)
424 (define-key viper-insert-basic-map "\C-j" nil))
426 (setq viper-insert-diehard-minor-mode
427 (not viper-want-emacs-keys-in-insert))
429 (if viper-want-ctl-h-help
430 (progn
431 (define-key viper-insert-basic-map "\C-h" 'help-command)
432 (define-key viper-replace-map "\C-h" 'help-command))
433 (define-key viper-insert-basic-map
434 "\C-h" 'viper-del-backward-char-in-insert)
435 (define-key viper-replace-map
436 "\C-h" 'viper-del-backward-char-in-replace))
437 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
438 (define-key viper-insert-basic-map
439 [backspace] 'viper-del-backward-char-in-insert)
440 (define-key viper-replace-map
441 [backspace] 'viper-del-backward-char-in-replace)
442 ) ; end insert/replace case
443 (t ; Vi state
444 (setq viper-vi-diehard-minor-mode (not viper-want-emacs-keys-in-vi))
445 (if viper-want-ctl-h-help
446 (define-key viper-vi-basic-map "\C-h" 'help-command)
447 (define-key viper-vi-basic-map "\C-h" 'viper-backward-char))
448 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
449 (define-key viper-vi-basic-map [backspace] 'viper-backward-char))
453 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
454 ;; This ensures that Viper bindings are in effect, regardless of which minor
455 ;; modes were turned on by the user or by other packages.
456 (defun viper-normalize-minor-mode-map-alist ()
457 (setq viper--intercept-key-maps
458 (list
459 (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map)
460 (cons 'viper-insert-intercept-minor-mode viper-insert-intercept-map)
461 (cons 'viper-emacs-intercept-minor-mode viper-emacs-intercept-map)
463 (setq viper--key-maps
464 (list (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map)
465 (cons 'viper-vi-local-user-minor-mode viper-vi-local-user-map)
466 (cons 'viper-vi-kbd-minor-mode viper-vi-kbd-map)
467 (cons 'viper-vi-global-user-minor-mode viper-vi-global-user-map)
468 (cons 'viper-vi-state-modifier-minor-mode
469 (if (keymapp
470 (cdr (assoc major-mode viper-vi-state-modifier-alist)))
471 (cdr (assoc major-mode viper-vi-state-modifier-alist))
472 viper-empty-keymap))
473 (cons 'viper-vi-diehard-minor-mode viper-vi-diehard-map)
474 (cons 'viper-vi-basic-minor-mode viper-vi-basic-map)
475 (cons 'viper-replace-minor-mode viper-replace-map)
476 ;; viper-insert-minibuffer-minor-mode must come after
477 ;; viper-replace-minor-mode
478 (cons 'viper-insert-minibuffer-minor-mode
479 viper-minibuffer-map)
480 (cons 'viper-insert-local-user-minor-mode
481 viper-insert-local-user-map)
482 (cons 'viper-insert-kbd-minor-mode viper-insert-kbd-map)
483 (cons 'viper-insert-global-user-minor-mode
484 viper-insert-global-user-map)
485 (cons 'viper-insert-state-modifier-minor-mode
486 (if (keymapp
487 (cdr (assoc major-mode
488 viper-insert-state-modifier-alist)))
489 (cdr (assoc major-mode
490 viper-insert-state-modifier-alist))
491 viper-empty-keymap))
492 (cons 'viper-insert-diehard-minor-mode viper-insert-diehard-map)
493 (cons 'viper-insert-basic-minor-mode viper-insert-basic-map)
494 (cons 'viper-emacs-local-user-minor-mode
495 viper-emacs-local-user-map)
496 (cons 'viper-emacs-kbd-minor-mode viper-emacs-kbd-map)
497 (cons 'viper-emacs-global-user-minor-mode
498 viper-emacs-global-user-map)
499 (cons 'viper-emacs-state-modifier-minor-mode
500 (if (keymapp
501 (cdr
502 (assoc major-mode viper-emacs-state-modifier-alist)))
503 (cdr
504 (assoc major-mode viper-emacs-state-modifier-alist))
505 viper-empty-keymap))
508 ;; This var is not local in Emacs, so we make it local. It must be local
509 ;; because although the stack of minor modes can be the same for all buffers,
510 ;; the associated *keymaps* can be different. In Viper,
511 ;; viper-vi-local-user-map, viper-insert-local-user-map, and others can have
512 ;; different keymaps for different buffers. Also, the keymaps associated
513 ;; with viper-vi/insert-state-modifier-minor-mode can be different.
514 ;; ***This is needed only in case emulation-mode-map-alists is not defined.
515 ;; In emacs with emulation-mode-map-alists, nothing needs to be done
516 (unless
517 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
518 (set (make-local-variable 'minor-mode-map-alist)
519 (viper-append-filter-alist
520 (append viper--intercept-key-maps viper--key-maps)
521 minor-mode-map-alist)))
526 ;; Viper mode-changing commands and utilities
528 ;; Modifies mode-line-buffer-identification.
529 (defun viper-refresh-mode-line ()
530 (set (make-local-variable 'viper-mode-string)
531 (cond ((eq viper-current-state 'emacs-state) viper-emacs-state-id)
532 ((eq viper-current-state 'vi-state) viper-vi-state-id)
533 ((eq viper-current-state 'replace-state) viper-replace-state-id)
534 ((eq viper-current-state 'insert-state) viper-insert-state-id)))
536 ;; Sets Viper mode string in global-mode-string
537 (force-mode-line-update))
540 ;; Switch from Insert state to Vi state.
541 (defun viper-exit-insert-state ()
542 (interactive)
543 (viper-change-state-to-vi))
545 (defun viper-set-mode-vars-for (state)
546 "Sets Viper minor mode variables to put Viper's state STATE in effect."
548 ;; Emacs state
549 (setq viper-vi-minibuffer-minor-mode nil
550 viper-insert-minibuffer-minor-mode nil
551 viper-vi-intercept-minor-mode nil
552 viper-insert-intercept-minor-mode nil
554 viper-vi-local-user-minor-mode nil
555 viper-vi-kbd-minor-mode nil
556 viper-vi-global-user-minor-mode nil
557 viper-vi-state-modifier-minor-mode nil
558 viper-vi-diehard-minor-mode nil
559 viper-vi-basic-minor-mode nil
561 viper-replace-minor-mode nil
563 viper-insert-local-user-minor-mode nil
564 viper-insert-kbd-minor-mode nil
565 viper-insert-global-user-minor-mode nil
566 viper-insert-state-modifier-minor-mode nil
567 viper-insert-diehard-minor-mode nil
568 viper-insert-basic-minor-mode nil
569 viper-emacs-intercept-minor-mode t
570 viper-emacs-local-user-minor-mode t
571 viper-emacs-kbd-minor-mode (not (viper-is-in-minibuffer))
572 viper-emacs-global-user-minor-mode t
573 viper-emacs-state-modifier-minor-mode t
576 ;; Vi state
577 (if (eq state 'vi-state) ; adjust for vi-state
578 (setq
579 viper-vi-intercept-minor-mode t
580 viper-vi-minibuffer-minor-mode (viper-is-in-minibuffer)
581 viper-vi-local-user-minor-mode t
582 viper-vi-kbd-minor-mode (not (viper-is-in-minibuffer))
583 viper-vi-global-user-minor-mode t
584 viper-vi-state-modifier-minor-mode t
585 ;; don't let the diehard keymap block command completion
586 ;; and other things in the minibuffer
587 viper-vi-diehard-minor-mode (not
588 (or viper-want-emacs-keys-in-vi
589 (viper-is-in-minibuffer)))
590 viper-vi-basic-minor-mode t
591 viper-emacs-intercept-minor-mode nil
592 viper-emacs-local-user-minor-mode nil
593 viper-emacs-kbd-minor-mode nil
594 viper-emacs-global-user-minor-mode nil
595 viper-emacs-state-modifier-minor-mode nil
598 ;; Insert and Replace states
599 (if (member state '(insert-state replace-state))
600 (setq
601 viper-insert-intercept-minor-mode t
602 viper-replace-minor-mode (eq state 'replace-state)
603 viper-insert-minibuffer-minor-mode (viper-is-in-minibuffer)
604 viper-insert-local-user-minor-mode t
605 viper-insert-kbd-minor-mode (not (viper-is-in-minibuffer))
606 viper-insert-global-user-minor-mode t
607 viper-insert-state-modifier-minor-mode t
608 ;; don't let the diehard keymap block command completion
609 ;; and other things in the minibuffer
610 viper-insert-diehard-minor-mode (not
612 viper-want-emacs-keys-in-insert
613 (viper-is-in-minibuffer)))
614 viper-insert-basic-minor-mode t
615 viper-emacs-intercept-minor-mode nil
616 viper-emacs-local-user-minor-mode nil
617 viper-emacs-kbd-minor-mode nil
618 viper-emacs-global-user-minor-mode nil
619 viper-emacs-state-modifier-minor-mode nil
622 ;; minibuffer faces
623 (if (viper-has-face-support-p)
624 (setq viper-minibuffer-current-face
625 (cond ((eq state 'emacs-state) viper-minibuffer-emacs-face)
626 ((eq state 'vi-state) viper-minibuffer-vi-face)
627 ((memq state '(insert-state replace-state))
628 viper-minibuffer-insert-face))))
630 (if (viper-is-in-minibuffer)
631 (viper-set-minibuffer-overlay))
634 ;; This also takes care of the annoying incomplete lines in files.
635 ;; Also, this fixes `undo' to work vi-style for complex commands.
636 (defun viper-change-state-to-vi ()
637 "Change Viper state to Vi."
638 (interactive)
639 (if (and viper-first-time (not (viper-is-in-minibuffer)))
640 (viper-mode)
641 (if overwrite-mode (overwrite-mode -1))
642 (or (viper-overlay-p viper-replace-overlay)
643 (viper-set-replace-overlay (point-min) (point-min)))
644 (viper-hide-replace-overlay)
645 (if abbrev-mode (expand-abbrev))
646 (if (and auto-fill-function (> (current-column) fill-column))
647 (funcall auto-fill-function))
648 ;; don't leave whitespace lines around
649 (if (and (memq last-command
650 '(viper-autoindent
651 viper-open-line viper-Open-line
652 viper-replace-state-exit-cmd))
653 (viper-over-whitespace-line))
654 (indent-to-left-margin))
655 (viper-add-newline-at-eob-if-necessary)
656 (viper-adjust-undo)
658 (if (eq viper-current-state 'emacs-state)
659 (viper-restore-cursor-color 'after-emacs-mode)
660 (viper-restore-cursor-color 'after-insert-mode))
662 (viper-change-state 'vi-state)
664 ;; Protect against user errors in hooks
665 (condition-case conds
666 (run-hooks 'viper-vi-state-hook)
667 (error
668 (viper-message-conditions conds)))))
670 (defun viper-change-state-to-insert ()
671 "Change Viper state to Insert."
672 (interactive)
673 (viper-change-state 'insert-state)
675 (or (viper-overlay-p viper-replace-overlay)
676 (viper-set-replace-overlay (point-min) (point-min)))
677 (viper-hide-replace-overlay)
679 (let ((has-saved-cursor-color-in-insert-mode
680 (stringp (viper-get-saved-cursor-color-in-insert-mode))))
681 (or has-saved-cursor-color-in-insert-mode
682 (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
683 (viper-save-cursor-color 'before-insert-mode))
684 (viper-change-cursor-color viper-insert-state-cursor-color))
686 ;; Protect against user errors in hooks
687 (condition-case conds
688 (run-hooks 'viper-insert-state-hook)
689 (error
690 (viper-message-conditions conds))))
692 (defsubst viper-downgrade-to-insert ()
693 ;; Protect against user errors in hooks
694 (condition-case conds
695 (run-hooks 'viper-insert-state-hook)
696 (error
697 (viper-message-conditions conds)))
698 (setq viper-current-state 'insert-state
699 viper-replace-minor-mode nil))
703 ;; Change to replace state. When the end of replacement region is reached,
704 ;; replace state changes to insert state.
705 (defun viper-change-state-to-replace (&optional non-R-cmd)
706 (viper-change-state 'replace-state)
707 ;; Run insert-state-hook
708 (condition-case conds
709 (run-hooks 'viper-insert-state-hook 'viper-replace-state-hook)
710 (error
711 (viper-message-conditions conds)))
713 (if non-R-cmd
714 (viper-start-replace)
715 ;; 'R' is implemented using Emacs's overwrite-mode
716 (viper-start-R-mode))
720 (defun viper-change-state-to-emacs ()
721 "Change Viper state to Emacs."
722 (interactive)
723 (or (viper-overlay-p viper-replace-overlay)
724 (viper-set-replace-overlay (point-min) (point-min)))
725 (viper-hide-replace-overlay)
727 (if viper-emacs-state-cursor-color
728 (let ((has-saved-cursor-color-in-emacs-mode
729 (stringp (viper-get-saved-cursor-color-in-emacs-mode))))
730 (or has-saved-cursor-color-in-emacs-mode
731 (string= (viper-get-cursor-color) viper-emacs-state-cursor-color)
732 (viper-save-cursor-color 'before-emacs-mode))
733 (viper-change-cursor-color viper-emacs-state-cursor-color)))
735 (viper-change-state 'emacs-state)
737 ;; Protect against user errors in hooks
738 (condition-case conds
739 (run-hooks 'viper-emacs-state-hook)
740 (error
741 (viper-message-conditions conds))))
743 ;; escape to emacs mode termporarily
744 (defun viper-escape-to-emacs (arg &optional events)
745 "Escape to Emacs state from Vi state for one Emacs command.
746 ARG is used as the prefix value for the executed command. If
747 EVENTS is a list of events, which become the beginning of the command."
748 (interactive "P")
749 (if (viper= last-command-char ?\\)
750 (message "Switched to EMACS state for the next command..."))
751 (viper-escape-to-state arg events 'emacs-state))
753 ;; escape to Vi mode termporarily
754 (defun viper-escape-to-vi (arg)
755 "Escape from Emacs state to Vi state for one Vi 1-character command.
756 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
757 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
758 `viper-escape-to-vi' is used."
759 (interactive "P")
760 (message "Switched to VI state for the next command...")
761 (viper-escape-to-state arg nil 'vi-state))
763 ;; Escape to STATE mode for one Emacs command.
764 (defun viper-escape-to-state (arg events state)
765 ;;(let (com key prefix-arg)
766 (let (com key)
767 ;; this temporarily turns off Viper's minor mode keymaps
768 (viper-set-mode-vars-for state)
769 (viper-normalize-minor-mode-map-alist)
770 (if events (viper-set-unread-command-events events))
772 ;; protect against keyboard quit and other errors
773 (condition-case nil
774 (let (viper-vi-kbd-minor-mode
775 viper-insert-kbd-minor-mode
776 viper-emacs-kbd-minor-mode)
777 (unwind-protect
778 (progn
779 (setq com
780 (key-binding (setq key (viper-read-key-sequence nil))))
781 ;; In case of binding indirection--chase definitions.
782 ;; Have to do it here because we execute this command under
783 ;; different keymaps, so command-execute may not do the
784 ;; right thing there
785 (while (vectorp com) (setq com (key-binding com))))
786 nil)
787 ;; Execute command com in the original Viper state, not in state
788 ;; `state'. Otherwise, if we switch buffers while executing the
789 ;; escaped to command, Viper's mode vars will remain those of
790 ;; `state'. When we return to the orig buffer, the bindings will be
791 ;; screwed up.
792 (viper-set-mode-vars-for viper-current-state)
794 ;; this-command, last-command-char, last-command-event
795 (setq this-command com)
796 (viper-cond-compile-for-xemacs-or-emacs
797 ;; XEmacs represents key sequences as vectors
798 (setq last-command-event
799 (viper-copy-event (viper-seq-last-elt key))
800 last-command-char (event-to-character last-command-event))
801 ;; Emacs represents them as sequences (str or vec)
802 (setq last-command-event
803 (viper-copy-event (viper-seq-last-elt key))
804 last-command-char last-command-event)
807 (if (commandp com)
808 ;; pretend that current state is the state we excaped to
809 (let ((viper-current-state state))
810 (setq prefix-arg (or prefix-arg arg))
811 (command-execute com)))
813 (quit (ding))
814 (error (beep 1))))
815 ;; set state in the new buffer
816 (viper-set-mode-vars-for viper-current-state))
818 ;; This is used in order to allow reading characters according to the input
819 ;; method. The character is read in emacs and inserted into the buffer.
820 ;; If an input method is in effect, this might
821 ;; cause several characters to be combined into one.
822 ;; Also takes care of the iso-accents mode
823 (defun viper-special-read-and-insert-char ()
824 (viper-set-mode-vars-for 'emacs-state)
825 (viper-normalize-minor-mode-map-alist)
826 (if viper-special-input-method
827 (viper-set-input-method t))
828 (if viper-automatic-iso-accents
829 (viper-set-iso-accents-mode t))
830 (condition-case nil
831 (let (viper-vi-kbd-minor-mode
832 viper-insert-kbd-minor-mode
833 viper-emacs-kbd-minor-mode
835 (cond ((and viper-special-input-method
836 viper-emacs-p
837 (fboundp 'quail-input-method))
838 ;; (let ...) is used to restore unread-command-events to the
839 ;; original state. We don't want anything left in there after
840 ;; key translation. (Such left-overs are possible if the user
841 ;; types a regular key.)
842 (let (unread-command-events)
843 ;; The next cmd and viper-set-unread-command-events
844 ;; are intended to prevent the input method
845 ;; from swallowing ^M, ^Q and other special characters
846 (setq ch (read-char-exclusive))
847 ;; replace ^M with the newline
848 (if (eq ch ?\C-m) (setq ch ?\n))
849 ;; Make sure ^V and ^Q work as quotation chars
850 (if (memq ch '(?\C-v ?\C-q))
851 (setq ch (read-char-exclusive)))
852 (viper-set-unread-command-events ch)
853 (quail-input-method nil)
855 (if (and ch (string= quail-current-str ""))
856 (insert ch)
857 (insert quail-current-str))
858 (setq ch (or ch
859 (aref quail-current-str
860 (1- (length quail-current-str)))))
862 ((and viper-special-input-method
863 viper-xemacs-p
864 (fboundp 'quail-start-translation))
865 ;; same as above but for XEmacs, which doesn't have
866 ;; quail-input-method
867 (let (unread-command-events)
868 (setq ch (read-char-exclusive))
869 ;; replace ^M with the newline
870 (if (eq ch ?\C-m) (setq ch ?\n))
871 ;; Make sure ^V and ^Q work as quotation chars
872 (if (memq ch '(?\C-v ?\C-q))
873 (setq ch (read-char-exclusive)))
874 (viper-set-unread-command-events ch)
875 (quail-start-translation nil)
877 (if (and ch (string= quail-current-str ""))
878 (insert ch)
879 (insert quail-current-str))
880 (setq ch (or ch
881 (aref quail-current-str
882 (1- (length quail-current-str)))))
884 ((and (boundp 'iso-accents-mode) iso-accents-mode)
885 (setq ch (aref (read-key-sequence nil) 0))
886 ;; replace ^M with the newline
887 (if (eq ch ?\C-m) (setq ch ?\n))
888 ;; Make sure ^V and ^Q work as quotation chars
889 (if (memq ch '(?\C-v ?\C-q))
890 (setq ch (aref (read-key-sequence nil) 0)))
891 (insert ch))
893 ;;(setq ch (read-char-exclusive))
894 (setq ch (aref (read-key-sequence nil) 0))
895 ;; replace ^M with the newline
896 (if (eq ch ?\C-m) (setq ch ?\n))
897 ;; Make sure ^V and ^Q work as quotation chars
898 (if (memq ch '(?\C-v ?\C-q))
899 ;;(setq ch (read-char-exclusive))
900 (setq ch (aref (read-key-sequence nil) 0))
902 (insert ch))
904 (setq last-command-event
905 (viper-copy-event (if viper-xemacs-p
906 (character-to-event ch) ch)))
907 ) ; let
908 (error nil)
909 ) ; condition-case
911 (viper-set-input-method nil)
912 (viper-set-iso-accents-mode nil)
913 (viper-set-mode-vars-for viper-current-state)
917 (defun viper-exec-form-in-vi (form)
918 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
919 (let ((buff (current-buffer))
920 result)
921 (viper-set-mode-vars-for 'vi-state)
923 (condition-case nil
924 (let (viper-vi-kbd-minor-mode) ; execute without kbd macros
925 (setq result (eval form))
927 (error
928 (signal 'quit nil)))
930 (if (not (equal buff (current-buffer))) ; cmd switched buffer
931 (save-excursion
932 (set-buffer buff)
933 (viper-set-mode-vars-for viper-current-state)))
934 (viper-set-mode-vars-for viper-current-state)
935 result))
937 (defun viper-exec-form-in-emacs (form)
938 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
939 Similar to viper-escape-to-emacs, but accepts forms rather than keystrokes."
940 (let ((buff (current-buffer))
941 result)
942 (viper-set-mode-vars-for 'emacs-state)
943 (setq result (eval form))
944 (if (not (equal buff (current-buffer))) ; cmd switched buffer
945 (save-excursion
946 (set-buffer buff)
947 (viper-set-mode-vars-for viper-current-state)))
948 (viper-set-mode-vars-for viper-current-state)
949 result))
951 ;; This executes the last kbd event in emacs mode. Is used when we want to
952 ;; interpret certain keys directly in emacs (as, for example, in comint mode).
953 (defun viper-exec-key-in-emacs (arg)
954 (interactive "P")
955 (viper-escape-to-emacs arg last-command-event))
958 ;; This is needed because minor modes sometimes override essential Viper
959 ;; bindings. By letting Viper know which files these modes are in, it will
960 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
961 (defun viper-harness-minor-mode (load-file)
962 "Familiarize Viper with a minor mode defined in LOAD_FILE.
963 Minor modes that have their own keymaps may overshadow Viper keymaps.
964 This function is designed to make Viper aware of the packages that define
965 such minor modes.
966 Usage:
967 (viper-harness-minor-mode load-file)
969 LOAD-FILE is a name of the file where the specific minor mode is defined.
970 Suffixes such as .el or .elc should be stripped."
972 (interactive "sEnter name of the load file: ")
974 (eval-after-load load-file '(viper-normalize-minor-mode-map-alist))
976 ;; Change the default for minor-mode-map-alist each time a harnessed minor
977 ;; mode adds its own keymap to the a-list.
978 (unless
979 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
980 (eval-after-load
981 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist)))
985 (defun viper-ESC (arg)
986 "Emulate ESC key in Emacs.
987 Prevents multiple escape keystrokes if viper-no-multiple-ESC is true.
988 If viper-no-multiple-ESC is 'twice double ESC would ding in vi-state.
989 Other ESC sequences are emulated via the current Emacs's major mode
990 keymap. This is more convenient on TTYs, since this won't block
991 function keys such as up,down, etc. ESC will also will also work as
992 a Meta key in this case. When viper-no-multiple-ESC is nil, ESC functions
993 as a Meta key and any number of multiple escapes is allowed."
994 (interactive "P")
995 (let (char)
996 (cond ((and (not viper-no-multiple-ESC) (eq viper-current-state 'vi-state))
997 (setq char (viper-read-char-exclusive))
998 (viper-escape-to-emacs arg (list ?\e char) ))
999 ((and (eq viper-no-multiple-ESC 'twice)
1000 (eq viper-current-state 'vi-state))
1001 (setq char (viper-read-char-exclusive))
1002 (if (= char (string-to-char viper-ESC-key))
1003 (ding)
1004 (viper-escape-to-emacs arg (list ?\e char) )))
1005 (t (ding)))
1008 (defun viper-alternate-Meta-key (arg)
1009 "Simulate Emacs Meta key."
1010 (interactive "P")
1011 (sit-for 1) (message "ESC-")
1012 (viper-escape-to-emacs arg '(?\e)))
1014 (defun viper-toggle-key-action ()
1015 "Action bound to `viper-toggle-key'."
1016 (interactive)
1017 (if (and (< viper-expert-level 2) (equal viper-toggle-key "\C-z"))
1018 (if (viper-window-display-p)
1019 (viper-iconify)
1020 (suspend-emacs))
1021 (viper-change-state-to-emacs)))
1024 ;; Intercept ESC sequences on dumb terminals.
1025 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
1027 ;; Check if last key was ESC and if so try to reread it as a function key.
1028 ;; But only if there are characters to read during a very short time.
1029 ;; Returns the last event, if any.
1030 (defun viper-envelop-ESC-key ()
1031 (let ((event last-input-event)
1032 (keyseq [nil])
1033 (inhibit-quit t))
1034 (if (viper-ESC-event-p event)
1035 (progn
1036 ;; Some versions of Emacs (eg., 22.50.8 have a bug, which makes even
1037 ;; a single ESC into ;; a fast keyseq. To guard against this, we
1038 ;; added a check if there are other events as well. Keep the next
1039 ;; line for the next time the bug reappears, so that will remember to
1040 ;; report it.
1041 ;;(if (and (viper-fast-keysequence-p) unread-command-events)
1042 (if (viper-fast-keysequence-p) ;; for Emacsen without the above bug
1043 (progn
1044 (let (minor-mode-map-alist emulation-mode-map-alists)
1045 (viper-set-unread-command-events event)
1046 (setq keyseq (read-key-sequence nil 'continue-echo))
1047 ) ; let
1048 ;; If keyseq translates into something that still has ESC
1049 ;; at the beginning, separate ESC from the rest of the seq.
1050 ;; In XEmacs we check for events that are keypress meta-key
1051 ;; and convert them into [escape key]
1053 ;; This is needed for the following reason:
1054 ;; If ESC is the first symbol, we interpret it as if the
1055 ;; user typed ESC and then quickly some other symbols.
1056 ;; If ESC is not the first one, then the key sequence
1057 ;; entered was apparently translated into a function key or
1058 ;; something (e.g., one may have
1059 ;; (define-key function-key-map "\e[192z" [f11])
1060 ;; which would translate the escape-sequence generated by
1061 ;; f11 in an xterm window into the symbolic key f11.
1063 ;; If `first-key' is not an ESC event, we make it into the
1064 ;; last-command-event in order to pretend that this key was
1065 ;; pressed. This is needed to allow arrow keys to be bound to
1066 ;; macros. Otherwise, viper-exec-mapped-kbd-macro will think
1067 ;; that the last event was ESC and so it'll execute whatever is
1068 ;; bound to ESC. (Viper macros can't be bound to
1069 ;; ESC-sequences).
1070 (let* ((first-key (elt keyseq 0))
1071 (key-mod (event-modifiers first-key)))
1072 (cond ((and (viper-ESC-event-p first-key)
1073 (not viper-translate-all-ESC-keysequences))
1074 ;; put keys following ESC on the unread list
1075 ;; and return ESC as the key-sequence
1076 (viper-set-unread-command-events (viper-subseq keyseq 1))
1077 (setq last-input-event event
1078 keyseq (if viper-emacs-p
1079 "\e"
1080 (vector (character-to-event ?\e)))))
1081 ((and viper-xemacs-p
1082 (key-press-event-p first-key)
1083 (equal '(meta) key-mod))
1084 (viper-set-unread-command-events
1085 (vconcat (vector
1086 (character-to-event (event-key first-key)))
1087 (viper-subseq keyseq 1)))
1088 (setq last-input-event event
1089 keyseq (vector (character-to-event ?\e))))
1090 ((eventp first-key)
1091 (setq last-command-event
1092 (viper-copy-event first-key)))
1094 ) ; end progn
1096 ;; this is escape event with nothing after it
1097 ;; put in unread-command-event and then re-read
1098 (viper-set-unread-command-events event)
1099 (setq keyseq (read-key-sequence nil))
1101 ;; not an escape event
1102 (setq keyseq (vector event)))
1103 keyseq))
1107 ;; Listen to ESC key.
1108 ;; If a sequence of keys starting with ESC is issued with very short delays,
1109 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
1110 (defun viper-intercept-ESC-key ()
1111 "Function that implements ESC key in Viper emulation of Vi."
1112 (interactive)
1113 (let ((cmd (or (key-binding (viper-envelop-ESC-key))
1114 '(lambda () (interactive) (error "")))))
1116 ;; call the actual function to execute ESC (if no other symbols followed)
1117 ;; or the key bound to the ESC sequence (if the sequence was issued
1118 ;; with very short delay between characters).
1119 (if (eq cmd 'viper-intercept-ESC-key)
1120 (setq cmd
1121 (cond ((eq viper-current-state 'vi-state)
1122 'viper-ESC)
1123 ((eq viper-current-state 'insert-state)
1124 'viper-exit-insert-state)
1125 ((eq viper-current-state 'replace-state)
1126 'viper-replace-state-exit-cmd)
1127 (t 'viper-change-state-to-vi)
1129 (call-interactively cmd)))
1134 ;; prefix argument for Vi mode
1136 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
1137 ;; represents the numeric value of the prefix argument and COM represents
1138 ;; command prefix such as "c", "d", "m" and "y".
1140 ;; Get value part of prefix-argument ARG.
1141 (defsubst viper-p-val (arg)
1142 (cond ((null arg) 1)
1143 ((consp arg)
1144 (if (or (null (car arg)) (equal (car arg) '(nil)))
1145 1 (car arg)))
1146 (t arg)))
1148 ;; Get raw value part of prefix-argument ARG.
1149 (defsubst viper-P-val (arg)
1150 (cond ((consp arg) (car arg))
1151 (t arg)))
1153 ;; Get com part of prefix-argument ARG.
1154 (defsubst viper-getcom (arg)
1155 (cond ((null arg) nil)
1156 ((consp arg) (cdr arg))
1157 (t nil)))
1159 ;; Get com part of prefix-argument ARG and modify it.
1160 (defun viper-getCom (arg)
1161 (let ((com (viper-getcom arg)))
1162 (cond ((viper= com ?c) ?c)
1163 ;; Previously, ?c was being converted to ?C, but this prevented
1164 ;; multiline replace regions.
1165 ;;((viper= com ?c) ?C)
1166 ((viper= com ?d) ?D)
1167 ((viper= com ?y) ?Y)
1168 (t com))))
1171 ;; Compute numeric prefix arg value.
1172 ;; Invoked by EVENT-CHAR. COM is the command part obtained so far.
1173 (defun viper-prefix-arg-value (event-char com)
1174 (let ((viper-intermediate-command 'viper-digit-argument)
1175 value func)
1176 ;; read while number
1177 (while (and (viper-characterp event-char)
1178 (>= event-char ?0) (<= event-char ?9))
1179 (setq value (+ (* (if (integerp value) value 0) 10) (- event-char ?0)))
1180 (setq event-char (viper-read-event-convert-to-char)))
1182 (setq prefix-arg value)
1183 (if com (setq prefix-arg (cons prefix-arg com)))
1184 (while (eq event-char ?U)
1185 (viper-describe-arg prefix-arg)
1186 (setq event-char (viper-read-event-convert-to-char)))
1188 (if (or com (and (not (eq viper-current-state 'vi-state))
1189 ;; make sure it is a Vi command
1190 (viper-characterp event-char)
1191 (viper-vi-command-p event-char)
1193 ;; If appears to be one of the vi commands,
1194 ;; then execute it with funcall and clear prefix-arg in order to not
1195 ;; confuse subsequent commands
1196 (progn
1197 ;; last-command-char is the char we want emacs to think was typed
1198 ;; last. If com is not nil, the viper-digit-argument command was
1199 ;; called from within viper-prefix-arg command, such as `d', `w',
1200 ;; etc., i.e., the user typed, say, d2. In this case, `com' would be
1201 ;; `d', `w', etc. If viper-digit-argument was invoked by
1202 ;; viper-escape-to-vi (which is indicated by the fact that the
1203 ;; current state is not vi-state), then `event-char' represents the
1204 ;; vi command to be executed (e.g., `d', `w', etc). Again,
1205 ;; last-command-char must make emacs believe that this is the command
1206 ;; we typed.
1207 (cond ((eq event-char 'return) (setq event-char ?\C-m))
1208 ((eq event-char 'delete) (setq event-char ?\C-?))
1209 ((eq event-char 'backspace) (setq event-char ?\C-h))
1210 ((eq event-char 'space) (setq event-char ?\ )))
1211 (setq last-command-char (or com event-char))
1212 (setq func (viper-exec-form-in-vi
1213 `(key-binding (char-to-string ,event-char))))
1214 (funcall func prefix-arg)
1215 (setq prefix-arg nil))
1216 ;; some other command -- let emacs do it in its own way
1217 (viper-set-unread-command-events event-char))
1221 ;; Vi operator as prefix argument."
1222 (defun viper-prefix-arg-com (char value com)
1223 (let ((cont t)
1224 cmd-info
1225 cmd-to-exec-at-end)
1226 (while (and cont
1227 (viper-memq-char char
1228 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1229 viper-buffer-search-char)))
1230 (if com
1231 ;; this means that we already have a command character, so we
1232 ;; construct a com list and exit while. however, if char is "
1233 ;; it is an error.
1234 (progn
1235 ;; new com is (CHAR . OLDCOM)
1236 (if (viper-memq-char char '(?# ?\")) (error ""))
1237 (setq com (cons char com))
1238 (setq cont nil))
1239 ;; If com is nil we set com as char, and read more. Again, if char is
1240 ;; ", we read the name of register and store it in viper-use-register.
1241 ;; if char is !, =, or #, a complete com is formed so we exit the while
1242 ;; loop.
1243 (cond ((viper-memq-char char '(?! ?=))
1244 (setq com char)
1245 (setq char (read-char))
1246 (setq cont nil))
1247 ((viper= char ?#)
1248 ;; read a char and encode it as com
1249 (setq com (+ 128 (read-char)))
1250 (setq char (read-char)))
1251 ((viper= char ?\")
1252 (let ((reg (read-char)))
1253 (if (viper-valid-register reg)
1254 (setq viper-use-register reg)
1255 (error ""))
1256 (setq char (read-char))))
1258 (setq com char)
1259 (setq char (read-char))))))
1261 (if (atom com)
1262 ;; `com' is a single char, so we construct the command argument
1263 ;; and if `char' is `?', we describe the arg; otherwise
1264 ;; we prepare the command that will be executed at the end.
1265 (progn
1266 (setq cmd-info (cons value com))
1267 (while (viper= char ?U)
1268 (viper-describe-arg cmd-info)
1269 (setq char (read-char)))
1270 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
1271 ;; execute it at the very end
1272 (or (viper-movement-command-p char)
1273 (viper-digit-command-p char)
1274 (viper-regsuffix-command-p char)
1275 (viper= char ?!) ; bang command
1276 (error ""))
1277 (setq cmd-to-exec-at-end
1278 (viper-exec-form-in-vi
1279 `(key-binding (char-to-string ,char)))))
1281 ;; as com is non-nil, this means that we have a command to execute
1282 (if (viper-memq-char (car com) '(?r ?R))
1283 ;; execute apropriate region command.
1284 (let ((char (car com)) (com (cdr com)))
1285 (setq prefix-arg (cons value com))
1286 (if (viper= char ?r)
1287 (viper-region prefix-arg)
1288 (viper-Region prefix-arg))
1289 ;; reset prefix-arg
1290 (setq prefix-arg nil))
1291 ;; otherwise, reset prefix arg and call appropriate command
1292 (setq value (if (null value) 1 value))
1293 (setq prefix-arg nil)
1294 (cond
1295 ;; If we change ?C to ?c here, then cc will enter replacement mode
1296 ;; rather than deleting lines. However, it will affect 1 less line than
1297 ;; normal. We decided to not use replacement mode here and follow Vi,
1298 ;; since replacement mode on n full lines can be achieved with nC.
1299 ((equal com '(?c . ?c)) (viper-line (cons value ?C)))
1300 ((equal com '(?d . ?d)) (viper-line (cons value ?D)))
1301 ((equal com '(?d . ?y)) (viper-yank-defun))
1302 ((equal com '(?y . ?y)) (viper-line (cons value ?Y)))
1303 ((equal com '(?< . ?<)) (viper-line (cons value ?<)))
1304 ((equal com '(?> . ?>)) (viper-line (cons value ?>)))
1305 ((equal com '(?! . ?!)) (viper-line (cons value ?!)))
1306 ((equal com '(?= . ?=)) (viper-line (cons value ?=)))
1307 (t (error "")))))
1309 (if cmd-to-exec-at-end
1310 (progn
1311 (setq last-command-char char)
1312 (setq last-command-event
1313 (viper-copy-event
1314 (if viper-xemacs-p (character-to-event char) char)))
1315 (condition-case nil
1316 (funcall cmd-to-exec-at-end cmd-info)
1317 (error
1318 (error "")))))
1321 (defun viper-describe-arg (arg)
1322 (let (val com)
1323 (setq val (viper-P-val arg)
1324 com (viper-getcom arg))
1325 (if (null val)
1326 (if (null com)
1327 (message "Value is nil, and command is nil")
1328 (message "Value is nil, and command is `%c'" com))
1329 (if (null com)
1330 (message "Value is `%d', and command is nil" val)
1331 (message "Value is `%d', and command is `%c'" val com)))))
1333 (defun viper-digit-argument (arg)
1334 "Begin numeric argument for the next command."
1335 (interactive "P")
1336 (viper-leave-region-active)
1337 (viper-prefix-arg-value
1338 last-command-char (if (consp arg) (cdr arg) nil)))
1340 (defun viper-command-argument (arg)
1341 "Accept a motion command as an argument."
1342 (interactive "P")
1343 (let ((viper-intermediate-command 'viper-command-argument))
1344 (condition-case nil
1345 (viper-prefix-arg-com
1346 last-command-char
1347 (cond ((null arg) nil)
1348 ((consp arg) (car arg))
1349 ((integerp arg) arg)
1350 (t (error viper-InvalidCommandArgument)))
1351 (cond ((null arg) nil)
1352 ((consp arg) (cdr arg))
1353 ((integerp arg) nil)
1354 (t (error viper-InvalidCommandArgument))))
1355 (quit (setq viper-use-register nil)
1356 (signal 'quit nil)))
1357 (viper-deactivate-mark)))
1360 ;; repeat last destructive command
1362 ;; Append region to text in register REG.
1363 ;; START and END are buffer positions indicating what to append.
1364 (defsubst viper-append-to-register (reg start end)
1365 (set-register reg (concat (if (stringp (get-register reg))
1366 (get-register reg) "")
1367 (buffer-substring start end))))
1369 ;; Saves last inserted text for possible use by viper-repeat command.
1370 (defun viper-save-last-insertion (beg end)
1371 (condition-case nil
1372 (setq viper-last-insertion (buffer-substring beg end))
1373 (error
1374 ;; beg or end marker are somehow screwed up
1375 (setq viper-last-insertion nil)))
1376 (setq viper-last-insertion (buffer-substring beg end))
1377 (or (< (length viper-d-com) 5)
1378 (setcar (nthcdr 4 viper-d-com) viper-last-insertion))
1379 (or (null viper-command-ring)
1380 (ring-empty-p viper-command-ring)
1381 (progn
1382 (setcar (nthcdr 4 (viper-current-ring-item viper-command-ring))
1383 viper-last-insertion)
1384 ;; del most recent elt, if identical to the second most-recent
1385 (viper-cleanup-ring viper-command-ring)))
1388 (defsubst viper-yank-last-insertion ()
1389 "Inserts the text saved by the previous viper-save-last-insertion command."
1390 (condition-case nil
1391 (insert viper-last-insertion)
1392 (error nil)))
1395 ;; define functions to be executed
1397 ;; invoked by the `C' command
1398 (defun viper-exec-change (m-com com)
1399 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1400 (set-marker viper-com-point (point) (current-buffer)))
1401 ;; handle C cmd at the eol and at eob.
1402 (if (or (and (eolp) (= viper-com-point (point)))
1403 (= viper-com-point (point-max)))
1404 (progn
1405 (insert " ")(backward-char 1)))
1406 (if (= viper-com-point (point))
1407 (viper-forward-char-carefully))
1408 (set-mark viper-com-point)
1409 (if (eq m-com 'viper-next-line-at-bol)
1410 (viper-enlarge-region (mark t) (point)))
1411 (if (< (point) (mark t))
1412 (exchange-point-and-mark))
1413 (if (eq (preceding-char) ?\n)
1414 (viper-backward-char-carefully)) ; give back the newline
1415 (if (eq viper-intermediate-command 'viper-repeat)
1416 (viper-change-subr (mark t) (point))
1417 (viper-change (mark t) (point))
1420 ;; this is invoked by viper-substitute-line
1421 (defun viper-exec-Change (m-com com)
1422 (save-excursion
1423 (set-mark viper-com-point)
1424 (viper-enlarge-region (mark t) (point))
1425 (if viper-use-register
1426 (progn
1427 (cond ((viper-valid-register viper-use-register '(letter digit))
1428 (copy-to-register
1429 viper-use-register (mark t) (point) nil))
1430 ((viper-valid-register viper-use-register '(Letter))
1431 (viper-append-to-register
1432 (downcase viper-use-register) (mark t) (point)))
1433 (t (setq viper-use-register nil)
1434 (error viper-InvalidRegister viper-use-register)))
1435 (setq viper-use-register nil)))
1436 (delete-region (mark t) (point)))
1437 (open-line 1)
1438 (if (eq viper-intermediate-command 'viper-repeat)
1439 (viper-yank-last-insertion)
1440 (viper-change-state-to-insert)
1443 (defun viper-exec-delete (m-com com)
1444 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1445 (set-marker viper-com-point (point) (current-buffer)))
1446 (let (chars-deleted)
1447 (if viper-use-register
1448 (progn
1449 (cond ((viper-valid-register viper-use-register '(letter digit))
1450 (copy-to-register
1451 viper-use-register viper-com-point (point) nil))
1452 ((viper-valid-register viper-use-register '(Letter))
1453 (viper-append-to-register
1454 (downcase viper-use-register) viper-com-point (point)))
1455 (t (setq viper-use-register nil)
1456 (error viper-InvalidRegister viper-use-register)))
1457 (setq viper-use-register nil)))
1458 (setq last-command
1459 (if (eq last-command 'd-command) 'kill-region nil))
1460 (setq chars-deleted (abs (- (point) viper-com-point)))
1461 (if (> chars-deleted viper-change-notification-threshold)
1462 (unless (viper-is-in-minibuffer)
1463 (message "Deleted %d characters" chars-deleted)))
1464 (kill-region viper-com-point (point))
1465 (setq this-command 'd-command)
1466 (if viper-ex-style-motion
1467 (if (and (eolp) (not (bolp))) (backward-char 1)))))
1469 (defun viper-exec-Delete (m-com com)
1470 (save-excursion
1471 (set-mark viper-com-point)
1472 (viper-enlarge-region (mark t) (point))
1473 (let (lines-deleted)
1474 (if viper-use-register
1475 (progn
1476 (cond ((viper-valid-register viper-use-register '(letter digit))
1477 (copy-to-register
1478 viper-use-register (mark t) (point) nil))
1479 ((viper-valid-register viper-use-register '(Letter))
1480 (viper-append-to-register
1481 (downcase viper-use-register) (mark t) (point)))
1482 (t (setq viper-use-register nil)
1483 (error viper-InvalidRegister viper-use-register)))
1484 (setq viper-use-register nil)))
1485 (setq last-command
1486 (if (eq last-command 'D-command) 'kill-region nil))
1487 (setq lines-deleted (count-lines (point) viper-com-point))
1488 (if (> lines-deleted viper-change-notification-threshold)
1489 (unless (viper-is-in-minibuffer)
1490 (message "Deleted %d lines" lines-deleted)))
1491 (kill-region (mark t) (point))
1492 (if (eq m-com 'viper-line) (setq this-command 'D-command)))
1493 (back-to-indentation)))
1495 ;; save region
1496 (defun viper-exec-yank (m-com com)
1497 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1498 (set-marker viper-com-point (point) (current-buffer)))
1499 (let (chars-saved)
1500 (if viper-use-register
1501 (progn
1502 (cond ((viper-valid-register viper-use-register '(letter digit))
1503 (copy-to-register
1504 viper-use-register viper-com-point (point) nil))
1505 ((viper-valid-register viper-use-register '(Letter))
1506 (viper-append-to-register
1507 (downcase viper-use-register) viper-com-point (point)))
1508 (t (setq viper-use-register nil)
1509 (error viper-InvalidRegister viper-use-register)))
1510 (setq viper-use-register nil)))
1511 (setq last-command nil)
1512 (copy-region-as-kill viper-com-point (point))
1513 (setq chars-saved (abs (- (point) viper-com-point)))
1514 (if (> chars-saved viper-change-notification-threshold)
1515 (unless (viper-is-in-minibuffer)
1516 (message "Saved %d characters" chars-saved)))
1517 (goto-char viper-com-point)))
1519 ;; save lines
1520 (defun viper-exec-Yank (m-com com)
1521 (save-excursion
1522 (set-mark viper-com-point)
1523 (viper-enlarge-region (mark t) (point))
1524 (let (lines-saved)
1525 (if viper-use-register
1526 (progn
1527 (cond ((viper-valid-register viper-use-register '(letter digit))
1528 (copy-to-register
1529 viper-use-register (mark t) (point) nil))
1530 ((viper-valid-register viper-use-register '(Letter))
1531 (viper-append-to-register
1532 (downcase viper-use-register) (mark t) (point)))
1533 (t (setq viper-use-register nil)
1534 (error viper-InvalidRegister viper-use-register)))
1535 (setq viper-use-register nil)))
1536 (setq last-command nil)
1537 (copy-region-as-kill (mark t) (point))
1538 (setq lines-saved (count-lines (mark t) (point)))
1539 (if (> lines-saved viper-change-notification-threshold)
1540 (unless (viper-is-in-minibuffer)
1541 (message "Saved %d lines" lines-saved)))))
1542 (viper-deactivate-mark)
1543 (goto-char viper-com-point))
1545 (defun viper-exec-bang (m-com com)
1546 (save-excursion
1547 (set-mark viper-com-point)
1548 (viper-enlarge-region (mark t) (point))
1549 (exchange-point-and-mark)
1550 (shell-command-on-region
1551 (mark t) (point)
1552 (if (viper= com ?!)
1553 (setq viper-last-shell-com
1554 (viper-read-string-with-history
1557 'viper-shell-history
1558 (car viper-shell-history)
1560 viper-last-shell-com)
1561 t)))
1563 (defun viper-exec-equals (m-com com)
1564 (save-excursion
1565 (set-mark viper-com-point)
1566 (viper-enlarge-region (mark t) (point))
1567 (if (> (mark t) (point)) (exchange-point-and-mark))
1568 (indent-region (mark t) (point) nil)))
1570 (defun viper-exec-shift (m-com com)
1571 (save-excursion
1572 (set-mark viper-com-point)
1573 (viper-enlarge-region (mark t) (point))
1574 (if (> (mark t) (point)) (exchange-point-and-mark))
1575 (indent-rigidly (mark t) (point)
1576 (if (viper= com ?>)
1577 viper-shift-width
1578 (- viper-shift-width))))
1579 ;; return point to where it was before shift
1580 (goto-char viper-com-point))
1582 ;; this is needed because some commands fake com by setting it to ?r, which
1583 ;; denotes repeated insert command.
1584 (defsubst viper-exec-dummy (m-com com)
1585 nil)
1587 (defun viper-exec-buffer-search (m-com com)
1588 (setq viper-s-string
1589 (regexp-quote (buffer-substring (point) viper-com-point)))
1590 (setq viper-s-forward t)
1591 (setq viper-search-history (cons viper-s-string viper-search-history))
1592 (setq viper-intermediate-command 'viper-exec-buffer-search)
1593 (viper-search viper-s-string viper-s-forward 1))
1595 (defvar viper-exec-array (make-vector 128 nil))
1597 ;; Using a dispatch array allows adding functions like buffer search
1598 ;; without affecting other functions. Buffer search can now be bound
1599 ;; to any character.
1601 (aset viper-exec-array ?c 'viper-exec-change)
1602 (aset viper-exec-array ?C 'viper-exec-Change)
1603 (aset viper-exec-array ?d 'viper-exec-delete)
1604 (aset viper-exec-array ?D 'viper-exec-Delete)
1605 (aset viper-exec-array ?y 'viper-exec-yank)
1606 (aset viper-exec-array ?Y 'viper-exec-Yank)
1607 (aset viper-exec-array ?r 'viper-exec-dummy)
1608 (aset viper-exec-array ?! 'viper-exec-bang)
1609 (aset viper-exec-array ?< 'viper-exec-shift)
1610 (aset viper-exec-array ?> 'viper-exec-shift)
1611 (aset viper-exec-array ?= 'viper-exec-equals)
1615 ;; This function is called by various movement commands to execute a
1616 ;; destructive command on the region specified by the movement command. For
1617 ;; instance, if the user types cw, then the command viper-forward-word will
1618 ;; call viper-execute-com to execute viper-exec-change, which eventually will
1619 ;; call viper-change to invoke the replace mode on the region.
1621 ;; The var viper-d-com is set to (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS)
1622 ;; via a call to viper-set-destructive-command, for later use by viper-repeat.
1623 (defun viper-execute-com (m-com val com)
1624 (let ((reg viper-use-register))
1625 ;; this is the special command `#'
1626 (if (> com 128)
1627 (viper-special-prefix-com (- com 128))
1628 (let ((fn (aref viper-exec-array com)))
1629 (if (null fn)
1630 (error "%c: %s" com viper-InvalidViCommand)
1631 (funcall fn m-com com))))
1632 (if (viper-dotable-command-p com)
1633 (viper-set-destructive-command
1634 (list m-com val com reg nil nil)))
1638 (defun viper-repeat (arg)
1639 "Re-execute last destructive command.
1640 Use the info in viper-d-com, which has the form
1641 \(com val ch reg inserted-text command-keys\),
1642 where `com' is the command to be re-executed, `val' is the
1643 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1644 if it exists, it is the name of the register for `com'.
1645 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
1646 (interactive "P")
1647 (let ((save-point (point)) ; save point before repeating prev cmd
1648 ;; Pass along that we are repeating a destructive command
1649 ;; This tells viper-set-destructive-command not to update
1650 ;; viper-command-ring
1651 (viper-intermediate-command 'viper-repeat))
1652 (if (eq last-command 'viper-undo)
1653 ;; if the last command was viper-undo, then undo-more
1654 (viper-undo-more)
1655 ;; otherwise execute the command stored in viper-d-com. if arg is
1656 ;; non-nil its prefix value is used as new prefix value for the command.
1657 (let ((m-com (car viper-d-com))
1658 (val (viper-P-val arg))
1659 (com (nth 2 viper-d-com))
1660 (reg (nth 3 viper-d-com)))
1661 (if (null val) (setq val (nth 1 viper-d-com)))
1662 (if (null m-com) (error "No previous command to repeat"))
1663 (setq viper-use-register reg)
1664 (if (nth 4 viper-d-com) ; text inserted by command
1665 (setq viper-last-insertion (nth 4 viper-d-com)
1666 viper-d-char (nth 4 viper-d-com)))
1667 (funcall m-com (cons val com))
1668 (cond ((and (< save-point (point)) viper-keep-point-on-repeat)
1669 (goto-char save-point)) ; go back to before repeat.
1670 ((and (< save-point (point)) viper-ex-style-editing)
1671 (or (bolp) (backward-char 1))))
1672 (if (and (eolp) (not (bolp)))
1673 (backward-char 1))
1675 (viper-adjust-undo) ; take care of undo
1676 ;; If the prev cmd was rotating the command ring, this means that `.' has
1677 ;; just executed a command from that ring. So, push it on the ring again.
1678 ;; If we are just executing previous command , then don't push viper-d-com
1679 ;; because viper-d-com is not fully constructed in this case (its keys and
1680 ;; the inserted text may be nil). Besides, in this case, the command
1681 ;; executed by `.' is already on the ring.
1682 (if (eq last-command 'viper-display-current-destructive-command)
1683 (viper-push-onto-ring viper-d-com 'viper-command-ring))
1684 (viper-deactivate-mark)
1687 (defun viper-repeat-from-history ()
1688 "Repeat a destructive command from history.
1689 Doesn't change viper-command-ring in any way, so `.' will work as before
1690 executing this command.
1691 This command is supposed to be bound to a two-character Vi macro where
1692 the second character is a digit 0 to 9. The digit indicates which
1693 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1694 invokes the command before that, etc."
1695 (interactive)
1696 (let* ((viper-intermediate-command 'repeating-display-destructive-command)
1697 (idx (cond (viper-this-kbd-macro
1698 (string-to-number
1699 (symbol-name (elt viper-this-kbd-macro 1))))
1700 (t 0)))
1701 (num idx)
1702 (viper-d-com viper-d-com))
1704 (or (and (numberp num) (<= 0 num) (<= num 9))
1705 (progn
1706 (setq idx 0
1707 num 0)
1708 (message
1709 "`viper-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1710 (while (< 0 num)
1711 (setq viper-d-com (viper-special-ring-rotate1 viper-command-ring -1))
1712 (setq num (1- num)))
1713 (viper-repeat nil)
1714 (while (> idx num)
1715 (viper-special-ring-rotate1 viper-command-ring 1)
1716 (setq num (1+ num)))
1720 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1721 ;; The chars that can follow `#' are determined by viper-hash-command-p
1722 (defun viper-special-prefix-com (char)
1723 (cond ((viper= char ?c)
1724 (downcase-region (min viper-com-point (point))
1725 (max viper-com-point (point))))
1726 ((viper= char ?C)
1727 (upcase-region (min viper-com-point (point))
1728 (max viper-com-point (point))))
1729 ((viper= char ?g)
1730 (push-mark viper-com-point t)
1731 (viper-global-execute))
1732 ((viper= char ?q)
1733 (push-mark viper-com-point t)
1734 (viper-quote-region))
1735 ((viper= char ?s)
1736 (funcall viper-spell-function viper-com-point (point)))
1737 (t (error "#%c: %s" char viper-InvalidViCommand))))
1740 ;; undoing
1742 ;; hook used inside undo
1743 (defvar viper-undo-functions nil)
1745 ;; Runs viper-before-change-functions inside before-change-functions
1746 (defun viper-undo-sentinel (beg end length)
1747 (run-hook-with-args 'viper-undo-functions beg end length))
1749 (add-hook 'after-change-functions 'viper-undo-sentinel)
1751 ;; Hook used in viper-undo
1752 (defun viper-after-change-undo-hook (beg end len)
1753 (if undo-in-progress
1754 (setq undo-beg-posn beg
1755 undo-end-posn (or end beg))
1756 ;; some other hooks may be changing various text properties in
1757 ;; the buffer in response to 'undo'; so remove this hook to avoid
1758 ;; its repeated invocation
1759 (remove-hook 'viper-undo-functions 'viper-after-change-undo-hook 'local)
1762 (defun viper-undo ()
1763 "Undo previous change."
1764 (interactive)
1765 (message "undo!")
1766 (let ((modified (buffer-modified-p))
1767 (before-undo-pt (point-marker))
1768 undo-beg-posn undo-end-posn)
1770 ;; the viper-after-change-undo-hook removes itself after the 1st invocation
1771 (add-hook 'viper-undo-functions 'viper-after-change-undo-hook nil 'local)
1773 (undo-start)
1774 (undo-more 2)
1775 ;;(setq undo-beg-posn (or undo-beg-posn (point))
1776 ;; undo-end-posn (or undo-end-posn (point)))
1777 ;;(setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1778 ;; undo-end-posn (or undo-end-posn undo-beg-posn))
1780 (if (and undo-beg-posn undo-end-posn)
1781 (progn
1782 (goto-char undo-beg-posn)
1783 (sit-for 0)
1784 (if (and viper-keep-point-on-undo
1785 (pos-visible-in-window-p before-undo-pt))
1786 (progn
1787 (push-mark (point-marker) t)
1788 (viper-sit-for-short 300)
1789 (goto-char undo-end-posn)
1790 (viper-sit-for-short 300)
1791 (if (pos-visible-in-window-p undo-beg-posn)
1792 (goto-char before-undo-pt)
1793 (goto-char undo-beg-posn)))
1794 (push-mark before-undo-pt t))
1797 (if (and (eolp) (not (bolp))) (backward-char 1))
1799 (setq this-command 'viper-undo))
1801 ;; Continue undoing previous changes.
1802 (defun viper-undo-more ()
1803 (message "undo more!")
1804 (condition-case nil
1805 (undo-more 1)
1806 (error (beep)
1807 (message "No further undo information in this buffer")))
1808 (if (and (eolp) (not (bolp))) (backward-char 1))
1809 (setq this-command 'viper-undo))
1811 ;; The following two functions are used to set up undo properly.
1812 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1813 ;; they are undone all at once.
1814 (defun viper-adjust-undo ()
1815 (if viper-undo-needs-adjustment
1816 (let ((inhibit-quit t)
1817 tmp tmp2)
1818 (setq viper-undo-needs-adjustment nil)
1819 (if (listp buffer-undo-list)
1820 (if (setq tmp (memq viper-buffer-undo-list-mark buffer-undo-list))
1821 (progn
1822 (setq tmp2 (cdr tmp)) ; the part after mark
1824 ;; cut tail from buffer-undo-list temporarily by direct
1825 ;; manipulation with pointers in buffer-undo-list
1826 (setcdr tmp nil)
1828 (setq buffer-undo-list (delq nil buffer-undo-list))
1829 (setq buffer-undo-list
1830 (delq viper-buffer-undo-list-mark buffer-undo-list))
1831 ;; restore tail of buffer-undo-list
1832 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1833 (setq buffer-undo-list (delq nil buffer-undo-list)))))
1837 (defun viper-set-complex-command-for-undo ()
1838 (if (listp buffer-undo-list)
1839 (if (not viper-undo-needs-adjustment)
1840 (let ((inhibit-quit t))
1841 (setq buffer-undo-list
1842 (cons viper-buffer-undo-list-mark buffer-undo-list))
1843 (setq viper-undo-needs-adjustment t)))))
1846 ;;; Viper's destructive Command ring utilities
1848 (defun viper-display-current-destructive-command ()
1849 (let ((text (nth 4 viper-d-com))
1850 (keys (nth 5 viper-d-com))
1851 (max-text-len 30))
1853 (setq this-command 'viper-display-current-destructive-command)
1855 (message " `.' runs %s%s"
1856 (concat "`" (viper-array-to-string keys) "'")
1857 (viper-abbreviate-string
1858 (viper-cond-compile-for-xemacs-or-emacs
1859 (replace-in-string ; xemacs
1860 (cond ((characterp text) (char-to-string text))
1861 ((stringp text) text)
1862 (t ""))
1863 "\n" "^J")
1864 text ; emacs
1866 max-text-len
1867 " inserting `" "'" " ......."))
1871 ;; don't change viper-d-com if it was viper-repeat command invoked with `.'
1872 ;; or in some other way (non-interactively).
1873 (defun viper-set-destructive-command (list)
1874 (or (eq viper-intermediate-command 'viper-repeat)
1875 (progn
1876 (setq viper-d-com list)
1877 (setcar (nthcdr 5 viper-d-com)
1878 (viper-array-to-string (if (arrayp viper-this-command-keys)
1879 viper-this-command-keys
1880 (this-command-keys))))
1881 (viper-push-onto-ring viper-d-com 'viper-command-ring)))
1882 (setq viper-this-command-keys nil))
1885 (defun viper-prev-destructive-command (next)
1886 "Find previous destructive command in the history of destructive commands.
1887 With prefix argument, find next destructive command."
1888 (interactive "P")
1889 (let (cmd viper-intermediate-command)
1890 (if (eq last-command 'viper-display-current-destructive-command)
1891 ;; repeated search through command history
1892 (setq viper-intermediate-command
1893 'repeating-display-destructive-command)
1894 ;; first search through command history--set temp ring
1895 (setq viper-temp-command-ring (copy-list viper-command-ring)))
1896 (setq cmd (if next
1897 (viper-special-ring-rotate1 viper-temp-command-ring 1)
1898 (viper-special-ring-rotate1 viper-temp-command-ring -1)))
1899 (if (null cmd)
1901 (setq viper-d-com cmd))
1902 (viper-display-current-destructive-command)))
1905 (defun viper-next-destructive-command ()
1906 "Find next destructive command in the history of destructive commands."
1907 (interactive)
1908 (viper-prev-destructive-command 'next))
1911 (defun viper-insert-prev-from-insertion-ring (arg)
1912 "Cycle through insertion ring in the direction of older insertions.
1913 Undoes previous insertion and inserts new.
1914 With prefix argument, cycles in the direction of newer elements.
1915 In minibuffer, this command executes whatever the invocation key is bound
1916 to in the global map, instead of cycling through the insertion ring."
1917 (interactive "P")
1918 (let (viper-intermediate-command)
1919 (if (eq last-command 'viper-insert-from-insertion-ring)
1920 (progn ; repeated search through insertion history
1921 (setq viper-intermediate-command 'repeating-insertion-from-ring)
1922 (if (eq viper-current-state 'replace-state)
1923 (undo 1)
1924 (if viper-last-inserted-string-from-insertion-ring
1925 (backward-delete-char
1926 (length viper-last-inserted-string-from-insertion-ring))))
1928 ;;first search through insertion history
1929 (setq viper-temp-insertion-ring (copy-list viper-insertion-ring)))
1930 (setq this-command 'viper-insert-from-insertion-ring)
1931 ;; so that things will be undone properly
1932 (setq buffer-undo-list (cons nil buffer-undo-list))
1933 (setq viper-last-inserted-string-from-insertion-ring
1934 (viper-special-ring-rotate1 viper-temp-insertion-ring (if arg 1 -1)))
1936 ;; this change of viper-intermediate-command must come after
1937 ;; viper-special-ring-rotate1, so that the ring will rotate, but before the
1938 ;; insertion.
1939 (setq viper-intermediate-command nil)
1940 (if viper-last-inserted-string-from-insertion-ring
1941 (insert viper-last-inserted-string-from-insertion-ring))
1944 (defun viper-insert-next-from-insertion-ring ()
1945 "Cycle through insertion ring in the direction of older insertions.
1946 Undo previous insertion and inserts new."
1947 (interactive)
1948 (viper-insert-prev-from-insertion-ring 'next))
1952 ;; some region utilities
1954 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1955 (defun viper-add-newline-at-eob-if-necessary ()
1956 (save-excursion
1957 (end-of-line)
1958 ;; make sure all lines end with newline, unless in the minibuffer or
1959 ;; when requested otherwise (require-final-newline is nil)
1960 (save-restriction
1961 (widen)
1962 (if (and (eobp)
1963 (not (bolp))
1964 require-final-newline
1965 (not (viper-is-in-minibuffer))
1966 (not buffer-read-only))
1967 (insert "\n")))
1970 (defun viper-yank-defun ()
1971 (mark-defun)
1972 (copy-region-as-kill (point) (mark t)))
1974 ;; Enlarge region between BEG and END.
1975 (defun viper-enlarge-region (beg end)
1976 (or beg (setq beg end)) ; if beg is nil, set to end
1977 (or end (setq end beg)) ; if end is nil, set to beg
1979 (if (< beg end)
1980 (progn (goto-char beg) (set-mark end))
1981 (goto-char end)
1982 (set-mark beg))
1983 (beginning-of-line)
1984 (exchange-point-and-mark)
1985 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1986 (if (not (eobp)) (beginning-of-line))
1987 (if (> beg end) (exchange-point-and-mark)))
1990 ;; Quote region by each line with a user supplied string.
1991 (defun viper-quote-region ()
1992 (let ((quote-str viper-quote-string)
1993 (donot-change-dafault t))
1994 (setq quote-str
1995 (viper-read-string-with-history
1996 "Quote string: "
1998 'viper-quote-region-history
1999 (cond ((string-match "tex.*-mode" (symbol-name major-mode)) "%%")
2000 ((string-match "java.*-mode" (symbol-name major-mode)) "//")
2001 ((string-match "perl.*-mode" (symbol-name major-mode)) "#")
2002 ((string-match "lisp.*-mode" (symbol-name major-mode)) ";;")
2003 ((memq major-mode '(c-mode cc-mode c++-mode)) "//")
2004 ((memq major-mode '(sh-mode shell-mode)) "#")
2005 (t (setq donot-change-dafault nil)
2006 quote-str))))
2007 (or donot-change-dafault
2008 (setq viper-quote-string quote-str))
2009 (viper-enlarge-region (point) (mark t))
2010 (if (> (point) (mark t)) (exchange-point-and-mark))
2011 (insert quote-str)
2012 (beginning-of-line)
2013 (forward-line 1)
2014 (while (and (< (point) (mark t)) (bolp))
2015 (insert quote-str)
2016 (beginning-of-line)
2017 (forward-line 1))))
2019 ;; Tells whether BEG is on the same line as END.
2020 ;; If one of the args is nil, it'll return nil.
2021 (defun viper-same-line (beg end)
2022 (let ((selective-display nil)
2023 (incr 0)
2024 temp)
2025 (if (and beg end (> beg end))
2026 (setq temp beg
2027 beg end
2028 end temp))
2029 (if (and beg end)
2030 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
2031 nil)
2033 ;; This 'if' is needed because Emacs treats the next empty line
2034 ;; as part of the previous line.
2035 (if (= (viper-line-pos 'start) end)
2036 (setq incr 1))
2037 (<= (+ incr (count-lines beg end)) 1))))
2041 ;; Check if the string ends with a newline.
2042 (defun viper-end-with-a-newline-p (string)
2043 (or (string= string "")
2044 (= (viper-seq-last-elt string) ?\n)))
2046 (defun viper-tmp-insert-at-eob (msg)
2047 (let ((savemax (point-max)))
2048 (goto-char savemax)
2049 (insert msg)
2050 (sit-for 2)
2051 (goto-char savemax) (delete-region (point) (point-max))
2056 ;;; Minibuffer business
2058 (defsubst viper-set-minibuffer-style ()
2059 (add-hook 'minibuffer-setup-hook 'viper-minibuffer-setup-sentinel)
2060 (add-hook 'post-command-hook 'viper-minibuffer-post-command-hook))
2063 (defun viper-minibuffer-setup-sentinel ()
2064 (let ((hook (if viper-vi-style-in-minibuffer
2065 'viper-change-state-to-insert
2066 'viper-change-state-to-emacs)))
2067 ;; making buffer-local variables so that normal buffers won't affect the
2068 ;; minibuffer and vice versa. Otherwise, command arguments will affect
2069 ;; minibuffer ops and insertions from the minibuffer will change those in
2070 ;; the normal buffers
2071 (make-local-variable 'viper-d-com)
2072 (make-local-variable 'viper-last-insertion)
2073 (make-local-variable 'viper-command-ring)
2074 (setq viper-d-com nil
2075 viper-last-insertion nil
2076 viper-command-ring nil)
2077 (funcall hook)
2080 ;; Thie is a temp hook that uses free variables init-message and initial.
2081 ;; A dirty feature, but it is the simplest way to have it do the right thing.
2082 ;; The INIT-MESSAGE and INITIAL vars come from the scope set by
2083 ;; viper-read-string-with-history
2084 (defun viper-minibuffer-standard-hook ()
2085 (if (stringp init-message)
2086 (viper-tmp-insert-at-eob init-message))
2087 (if (stringp initial)
2088 (progn
2089 ;; don't wait if we have unread events or in kbd macro
2090 (or unread-command-events
2091 executing-kbd-macro
2092 (sit-for 840))
2093 (if (fboundp 'minibuffer-prompt-end)
2094 (delete-region (minibuffer-prompt-end) (point-max))
2095 (erase-buffer))
2096 (insert initial))))
2098 (defsubst viper-minibuffer-real-start ()
2099 (if (fboundp 'minibuffer-prompt-end)
2100 (minibuffer-prompt-end)
2101 (point-min)))
2103 (defun viper-minibuffer-post-command-hook()
2104 (when (active-minibuffer-window)
2105 (when (< (point) (viper-minibuffer-real-start))
2106 (goto-char (viper-minibuffer-real-start)))))
2109 ;; Interpret last event in the local map first; if fails, use exit-minibuffer.
2110 ;; Run viper-minibuffer-exit-hook before exiting.
2111 (defun viper-exit-minibuffer ()
2112 "Exit minibuffer Viper way."
2113 (interactive)
2114 (let (command)
2115 (setq command (local-key-binding (char-to-string last-command-char)))
2116 (run-hooks 'viper-minibuffer-exit-hook)
2117 (if command
2118 (command-execute command)
2119 (exit-minibuffer))))
2122 (defcustom viper-smart-suffix-list
2123 '("" "tex" "c" "cc" "C" "java" "el" "html" "htm" "xml"
2124 "pl" "flr" "P" "p" "h" "H")
2125 "*List of suffixes that Viper tries to append to filenames ending with a `.'.
2126 This is useful when the current directory contains files with the same
2127 prefix and many different suffixes. Usually, only one of the suffixes
2128 represents an editable file. However, file completion will stop at the `.'
2129 The smart suffix feature lets you hit RET in such a case, and Viper will
2130 select the appropriate suffix.
2132 Suffixes are tried in the order given and the first suffix for which a
2133 corresponding file exists is selected. If no file exists for any of the
2134 suffixes, the user is asked to confirm.
2136 To turn this feature off, set this variable to nil."
2137 :type '(repeat string)
2138 :group 'viper-misc)
2141 ;; Try to add a suitable suffix to files whose name ends with a `.'
2142 ;; Useful when the user hits RET on a non-completed file name.
2143 ;; Used as a minibuffer exit hook in read-file-name
2144 (defun viper-file-add-suffix ()
2145 (let ((count 0)
2146 (len (length viper-smart-suffix-list))
2147 (file (buffer-substring-no-properties
2148 (viper-minibuffer-real-start) (point-max)))
2149 found key cmd suff)
2150 (goto-char (point-max))
2151 (if (and viper-smart-suffix-list (string-match "\\.$" file))
2152 (progn
2153 (while (and (not found) (< count len))
2154 (setq suff (nth count viper-smart-suffix-list)
2155 count (1+ count))
2156 (if (file-exists-p
2157 (format "%s%s" (substitute-in-file-name file) suff))
2158 (progn
2159 (setq found t)
2160 (insert suff))))
2162 (if found
2164 (viper-tmp-insert-at-eob " [Please complete file name]")
2165 (unwind-protect
2166 (while (not (memq cmd
2167 '(exit-minibuffer viper-exit-minibuffer)))
2168 (setq cmd
2169 (key-binding (setq key (read-key-sequence nil))))
2170 (cond ((eq cmd 'self-insert-command)
2171 (viper-cond-compile-for-xemacs-or-emacs
2172 (insert (events-to-keys key)) ; xemacs
2173 (insert key) ; emacs
2175 ((memq cmd '(exit-minibuffer viper-exit-minibuffer))
2176 nil)
2177 (t (command-execute cmd)))
2179 ))))
2182 (defun viper-minibuffer-trim-tail ()
2183 "Delete junk at the end of the first line of the minibuffer input.
2184 Remove this function from `viper-minibuffer-exit-hook', if this causes
2185 problems."
2186 (if (viper-is-in-minibuffer)
2187 (let ((inhibit-field-text-motion t))
2188 (goto-char (viper-minibuffer-real-start))
2189 (end-of-line)
2190 (delete-region (point) (point-max)))))
2193 ;;; Reading string with history
2195 (defun viper-read-string-with-history (prompt &optional initial
2196 history-var default keymap
2197 init-message)
2198 ;; Read string, prompting with PROMPT and inserting the INITIAL
2199 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2200 ;; input is an empty string.
2201 ;; Default value is displayed until the user types something in the
2202 ;; minibuffer.
2203 ;; KEYMAP is used, if given, instead of minibuffer-local-map.
2204 ;; INIT-MESSAGE is the message temporarily displayed after entering the
2205 ;; minibuffer.
2206 (let ((minibuffer-setup-hook
2207 ;; stolen from add-hook
2208 (let ((old
2209 (if (boundp 'minibuffer-setup-hook)
2210 minibuffer-setup-hook
2211 nil)))
2212 (cons
2213 'viper-minibuffer-standard-hook
2214 (if (or (not (listp old)) (eq (car old) 'lambda))
2215 (list old) old))))
2216 (val "")
2217 (padding "")
2218 temp-msg)
2220 (setq keymap (or keymap minibuffer-local-map)
2221 initial (or initial "")
2222 temp-msg (if default
2223 (format "(default %s) " default)
2224 ""))
2226 (setq viper-incomplete-ex-cmd nil)
2227 (setq val (read-from-minibuffer prompt
2228 (concat temp-msg initial val padding)
2229 keymap nil history-var))
2230 (setq minibuffer-setup-hook nil
2231 padding (viper-array-to-string (this-command-keys))
2232 temp-msg "")
2233 ;; the following tries to be smart about what to put in history
2234 (if (not (string= val (car (eval history-var))))
2235 (set history-var (cons val (eval history-var))))
2236 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2237 (string= (nth 0 (eval history-var)) ""))
2238 (set history-var (cdr (eval history-var))))
2239 ;; If the user enters nothing but the prev cmd wasn't viper-ex,
2240 ;; viper-command-argument, or `! shell-command', this probably means
2241 ;; that the user typed something then erased. Return "" in this case, not
2242 ;; the default---the default is too confusing in this case.
2243 (cond ((and (string= val "")
2244 (not (string= prompt "!")) ; was a `! shell-command'
2245 (not (memq last-command
2246 '(viper-ex
2247 viper-command-argument
2251 ((string= val "") (or default ""))
2252 (t val))
2257 ;; insertion commands
2259 ;; Called when state changes from Insert Vi command mode.
2260 ;; Repeats the insertion command if Insert state was entered with prefix
2261 ;; argument > 1.
2262 (defun viper-repeat-insert-command ()
2263 (let ((i-com (car viper-d-com))
2264 (val (nth 1 viper-d-com))
2265 (char (nth 2 viper-d-com)))
2266 (if (and val (> val 1)) ; first check that val is non-nil
2267 (progn
2268 (setq viper-d-com (list i-com (1- val) ?r nil nil nil))
2269 (viper-repeat nil)
2270 (setq viper-d-com (list i-com val char nil nil nil))
2271 ))))
2273 (defun viper-insert (arg)
2274 "Insert before point."
2275 (interactive "P")
2276 (viper-set-complex-command-for-undo)
2277 (let ((val (viper-p-val arg))
2278 ;;(com (viper-getcom arg))
2280 (viper-set-destructive-command (list 'viper-insert val ?r nil nil nil))
2281 (if (eq viper-intermediate-command 'viper-repeat)
2282 (viper-loop val (viper-yank-last-insertion))
2283 (viper-change-state-to-insert))))
2285 (defun viper-append (arg)
2286 "Append after point."
2287 (interactive "P")
2288 (viper-set-complex-command-for-undo)
2289 (let ((val (viper-p-val arg))
2290 ;;(com (viper-getcom arg))
2292 (viper-set-destructive-command (list 'viper-append val ?r nil nil nil))
2293 (if (not (eolp)) (forward-char))
2294 (if (eq viper-intermediate-command 'viper-repeat)
2295 (viper-loop val (viper-yank-last-insertion))
2296 (viper-change-state-to-insert))))
2298 (defun viper-Append (arg)
2299 "Append at end of line."
2300 (interactive "P")
2301 (viper-set-complex-command-for-undo)
2302 (let ((val (viper-p-val arg))
2303 ;;(com (viper-getcom arg))
2305 (viper-set-destructive-command (list 'viper-Append val ?r nil nil nil))
2306 (end-of-line)
2307 (if (eq viper-intermediate-command 'viper-repeat)
2308 (viper-loop val (viper-yank-last-insertion))
2309 (viper-change-state-to-insert))))
2311 (defun viper-Insert (arg)
2312 "Insert before first non-white."
2313 (interactive "P")
2314 (viper-set-complex-command-for-undo)
2315 (let ((val (viper-p-val arg))
2316 ;;(com (viper-getcom arg))
2318 (viper-set-destructive-command (list 'viper-Insert val ?r nil nil nil))
2319 (back-to-indentation)
2320 (if (eq viper-intermediate-command 'viper-repeat)
2321 (viper-loop val (viper-yank-last-insertion))
2322 (viper-change-state-to-insert))))
2324 (defun viper-open-line (arg)
2325 "Open line below."
2326 (interactive "P")
2327 (viper-set-complex-command-for-undo)
2328 (let ((val (viper-p-val arg))
2329 ;;(com (viper-getcom arg))
2331 (viper-set-destructive-command (list 'viper-open-line val ?r nil nil nil))
2332 (let ((col (current-indentation)))
2333 (if (eq viper-intermediate-command 'viper-repeat)
2334 (viper-loop val
2335 (end-of-line)
2336 (newline 1)
2337 (viper-indent-line col)
2338 (viper-yank-last-insertion))
2339 (end-of-line)
2340 (newline 1)
2341 (viper-indent-line col)
2342 (viper-change-state-to-insert)))))
2344 (defun viper-Open-line (arg)
2345 "Open line above."
2346 (interactive "P")
2347 (viper-set-complex-command-for-undo)
2348 (let ((val (viper-p-val arg))
2349 ;;(com (viper-getcom arg))
2351 (viper-set-destructive-command (list 'viper-Open-line val ?r nil nil nil))
2352 (let ((col (current-indentation)))
2353 (if (eq viper-intermediate-command 'viper-repeat)
2354 (viper-loop val
2355 (beginning-of-line)
2356 (open-line 1)
2357 (viper-indent-line col)
2358 (viper-yank-last-insertion))
2359 (beginning-of-line)
2360 (open-line 1)
2361 (viper-indent-line col)
2362 (viper-change-state-to-insert)))))
2364 (defun viper-open-line-at-point (arg)
2365 "Open line at point."
2366 (interactive "P")
2367 (viper-set-complex-command-for-undo)
2368 (let ((val (viper-p-val arg))
2369 ;;(com (viper-getcom arg))
2371 (viper-set-destructive-command
2372 (list 'viper-open-line-at-point val ?r nil nil nil))
2373 (if (eq viper-intermediate-command 'viper-repeat)
2374 (viper-loop val
2375 (open-line 1)
2376 (viper-yank-last-insertion))
2377 (open-line 1)
2378 (viper-change-state-to-insert))))
2380 ;; bound to s
2381 (defun viper-substitute (arg)
2382 "Substitute characters."
2383 (interactive "P")
2384 (let ((val (viper-p-val arg))
2385 ;;(com (viper-getcom arg))
2387 (push-mark nil t)
2388 (forward-char val)
2389 (if (eq viper-intermediate-command 'viper-repeat)
2390 (viper-change-subr (mark t) (point))
2391 (viper-change (mark t) (point)))
2392 ;; com is set to ?r when we repeat this comand with dot
2393 (viper-set-destructive-command (list 'viper-substitute val ?r nil nil nil))
2396 ;; Command bound to S
2397 (defun viper-substitute-line (arg)
2398 "Substitute lines."
2399 (interactive "p")
2400 (viper-set-complex-command-for-undo)
2401 (viper-line (cons arg ?C)))
2403 ;; Prepare for replace
2404 (defun viper-start-replace ()
2405 (setq viper-began-as-replace t
2406 viper-sitting-in-replace t
2407 viper-replace-chars-to-delete 0)
2408 (add-hook
2409 'viper-after-change-functions 'viper-replace-mode-spy-after t 'local)
2410 (add-hook
2411 'viper-before-change-functions 'viper-replace-mode-spy-before t 'local)
2412 ;; this will get added repeatedly, but no harm
2413 (add-hook 'after-change-functions 'viper-after-change-sentinel t)
2414 (add-hook 'before-change-functions 'viper-before-change-sentinel t)
2415 (viper-move-marker-locally
2416 'viper-last-posn-in-replace-region (viper-replace-start))
2417 (add-hook
2418 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel
2419 t 'local)
2420 (add-hook
2421 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2422 ;; guard against a smartie who switched from R-replace to normal replace
2423 (remove-hook
2424 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2425 (if overwrite-mode (overwrite-mode -1))
2429 (defun viper-replace-mode-spy-before (beg end)
2430 (setq viper-replace-region-chars-deleted (viper-chars-in-region beg end))
2433 ;; Invoked as an after-change-function to calculate how many chars have to be
2434 ;; deleted. This function may be called several times within a single command,
2435 ;; if this command performs several separate buffer changes. Therefore, if
2436 ;; adds up the number of chars inserted and subtracts the number of chars
2437 ;; deleted.
2438 (defun viper-replace-mode-spy-after (beg end length)
2439 (if (memq viper-intermediate-command
2440 '(dabbrev-expand hippie-expand repeating-insertion-from-ring))
2441 ;; Take special care of text insertion from insertion ring inside
2442 ;; replacement overlays.
2443 (progn
2444 (setq viper-replace-chars-to-delete 0)
2445 (viper-move-marker-locally
2446 'viper-last-posn-in-replace-region (point)))
2448 (let* ((real-end (min end (viper-replace-end)))
2449 (column-shift (- (save-excursion (goto-char real-end)
2450 (current-column))
2451 (save-excursion (goto-char beg)
2452 (current-column))))
2453 (chars-deleted 0))
2455 (if (> length 0)
2456 (setq chars-deleted viper-replace-region-chars-deleted))
2457 (setq viper-replace-region-chars-deleted 0)
2458 (setq viper-replace-chars-to-delete
2459 (+ viper-replace-chars-to-delete
2461 ;; if column shift is bigger, due to a TAB insertion, take
2462 ;; column-shift instead of the number of inserted chars
2463 (max (viper-chars-in-region beg real-end)
2464 ;; This test accounts for Chinese/Japanese/... chars,
2465 ;; which occupy 2 columns instead of one. If we use
2466 ;; column-shift here, we may delete two chars instead of
2467 ;; one when the user types one Chinese character.
2468 ;; Deleting two would be OK, if they were European chars,
2469 ;; but it is not OK if they are Chinese chars.
2470 ;; Since it is hard to
2471 ;; figure out which characters are being deleted in any
2472 ;; given region, we decided to treat Eastern and European
2473 ;; characters equally, even though Eastern chars may
2474 ;; occupy more columns.
2475 (if (memq this-command '(self-insert-command
2476 quoted-insert viper-insert-tab))
2477 column-shift
2479 ;; the number of deleted chars
2480 chars-deleted)))
2482 (viper-move-marker-locally
2483 'viper-last-posn-in-replace-region
2484 (max (if (> end (viper-replace-end)) (viper-replace-end) end)
2485 (or (marker-position viper-last-posn-in-replace-region)
2486 (viper-replace-start))
2492 ;; Delete stuff between viper-last-posn-in-replace-region and the end of
2493 ;; viper-replace-overlay-marker, if viper-last-posn-in-replace-region is within
2494 ;; the overlay and current point is before the end of the overlay.
2495 ;; Don't delete anything if current point is past the end of the overlay.
2496 (defun viper-finish-change ()
2497 (remove-hook
2498 'viper-after-change-functions 'viper-replace-mode-spy-after 'local)
2499 (remove-hook
2500 'viper-before-change-functions 'viper-replace-mode-spy-before 'local)
2501 (remove-hook
2502 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2503 (remove-hook
2504 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2505 (viper-restore-cursor-color 'after-replace-mode)
2506 (setq viper-sitting-in-replace nil) ; just in case we'll need to know it
2507 (save-excursion
2508 (if (and viper-replace-overlay
2509 (viper-pos-within-region viper-last-posn-in-replace-region
2510 (viper-replace-start)
2511 (viper-replace-end))
2512 (< (point) (viper-replace-end)))
2513 (delete-region
2514 viper-last-posn-in-replace-region (viper-replace-end))))
2516 (if (eq viper-current-state 'replace-state)
2517 (viper-downgrade-to-insert))
2518 ;; replace mode ended => nullify viper-last-posn-in-replace-region
2519 (viper-move-marker-locally 'viper-last-posn-in-replace-region nil)
2520 (viper-hide-replace-overlay)
2521 (viper-refresh-mode-line)
2522 (viper-put-string-on-kill-ring viper-last-replace-region)
2525 ;; Make STRING be the first element of the kill ring.
2526 (defun viper-put-string-on-kill-ring (string)
2527 (setq kill-ring (cons string kill-ring))
2528 (if (> (length kill-ring) kill-ring-max)
2529 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2530 (setq kill-ring-yank-pointer kill-ring))
2532 (defun viper-finish-R-mode ()
2533 (remove-hook
2534 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2535 (remove-hook
2536 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2537 (viper-downgrade-to-insert))
2539 (defun viper-start-R-mode ()
2540 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2541 (overwrite-mode 1)
2542 (add-hook
2543 'viper-post-command-hooks 'viper-R-state-post-command-sentinel t 'local)
2544 (add-hook
2545 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2546 ;; guard against a smartie who switched from R-replace to normal replace
2547 (remove-hook
2548 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2553 (defun viper-replace-state-exit-cmd ()
2554 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2555 These keys are ESC, RET, and LineFeed"
2556 (interactive)
2557 (if overwrite-mode ; if in replace mode invoked via 'R'
2558 (viper-finish-R-mode)
2559 (viper-finish-change))
2560 (let (com)
2561 (if (eq this-command 'viper-intercept-ESC-key)
2562 (setq com 'viper-exit-insert-state)
2563 (viper-set-unread-command-events last-input-char)
2564 (setq com (key-binding (viper-read-key-sequence nil))))
2566 (condition-case conds
2567 (command-execute com)
2568 (error
2569 (viper-message-conditions conds)))
2571 (viper-hide-replace-overlay))
2574 (defun viper-replace-state-carriage-return ()
2575 "Carriage return in Viper replace state."
2576 (interactive)
2577 ;; If Emacs start supporting overlay maps, as it currently supports
2578 ;; text-property maps, we could do away with viper-replace-minor-mode and
2579 ;; just have keymap attached to replace overlay. Then the "if part" of this
2580 ;; statement can be deleted.
2581 (if (or (< (point) (viper-replace-start))
2582 (> (point) (viper-replace-end)))
2583 (let (viper-replace-minor-mode com)
2584 (viper-set-unread-command-events last-input-char)
2585 (setq com (key-binding (read-key-sequence nil)))
2586 (condition-case conds
2587 (command-execute com)
2588 (error
2589 (viper-message-conditions conds))))
2590 (if (not viper-allow-multiline-replace-regions)
2591 (viper-replace-state-exit-cmd)
2592 (if (viper-same-line (point) (viper-replace-end))
2593 (viper-replace-state-exit-cmd)
2594 ;; delete the rest of line
2595 (delete-region (point) (viper-line-pos 'end))
2596 (save-excursion
2597 (end-of-line)
2598 (if (eobp) (error "Last line in buffer")))
2599 ;; skip to the next line
2600 (forward-line 1)
2601 (back-to-indentation)
2602 ))))
2605 ;; This is the function bound to 'R'---unlimited replace.
2606 ;; Similar to Emacs's own overwrite-mode.
2607 (defun viper-overwrite (arg)
2608 "Begin overwrite mode."
2609 (interactive "P")
2610 (let ((val (viper-p-val arg))
2611 ;;(com (viper-getcom arg))
2612 (len))
2613 (viper-set-destructive-command (list 'viper-overwrite val ?r nil nil nil))
2614 (if (eq viper-intermediate-command 'viper-repeat)
2615 (progn
2616 ;; Viper saves inserted text in viper-last-insertion
2617 (setq len (length viper-last-insertion))
2618 (delete-char (min len (- (point-max) (point) 1)))
2619 (viper-loop val (viper-yank-last-insertion)))
2620 (setq last-command 'viper-overwrite)
2621 (viper-set-complex-command-for-undo)
2622 (viper-set-replace-overlay (point) (viper-line-pos 'end))
2623 (viper-change-state-to-replace)
2627 ;; line commands
2629 (defun viper-line (arg)
2630 (let ((val (car arg))
2631 (com (cdr arg)))
2632 (viper-move-marker-locally 'viper-com-point (point))
2633 (if (not (eobp))
2634 (viper-next-line-carefully (1- val)))
2635 ;; the following ensures that dd, cc, D, yy will do the right thing on the
2636 ;; last line of buffer when this line has no \n.
2637 (viper-add-newline-at-eob-if-necessary)
2638 (viper-execute-com 'viper-line val com))
2639 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2642 (defun viper-yank-line (arg)
2643 "Yank ARG lines (in Vi's sense)."
2644 (interactive "P")
2645 (let ((val (viper-p-val arg)))
2646 (viper-line (cons val ?Y))))
2649 ;; region commands
2651 (defun viper-region (arg)
2652 "Execute command on a region."
2653 (interactive "P")
2654 (let ((val (viper-P-val arg))
2655 (com (viper-getcom arg)))
2656 (viper-move-marker-locally 'viper-com-point (point))
2657 (exchange-point-and-mark)
2658 (viper-execute-com 'viper-region val com)))
2660 (defun viper-Region (arg)
2661 "Execute command on a Region."
2662 (interactive "P")
2663 (let ((val (viper-P-val arg))
2664 (com (viper-getCom arg)))
2665 (viper-move-marker-locally 'viper-com-point (point))
2666 (exchange-point-and-mark)
2667 (viper-execute-com 'viper-Region val com)))
2669 (defun viper-replace-char (arg)
2670 "Replace the following ARG chars by the character read."
2671 (interactive "P")
2672 (if (and (eolp) (bolp)) (error "No character to replace here"))
2673 (let ((val (viper-p-val arg))
2674 (com (viper-getcom arg)))
2675 (viper-replace-char-subr com val)
2676 (if (and (eolp) (not (bolp))) (forward-char 1))
2677 (setq viper-this-command-keys
2678 (format "%sr" (if (integerp arg) arg "")))
2679 (viper-set-destructive-command
2680 (list 'viper-replace-char val ?r nil viper-d-char nil))
2683 (defun viper-replace-char-subr (com arg)
2684 (let ((inhibit-quit t)
2685 char)
2686 (viper-set-complex-command-for-undo)
2687 (or (eq viper-intermediate-command 'viper-repeat)
2688 (viper-special-read-and-insert-char))
2690 (delete-char 1 t)
2691 (setq char (if com viper-d-char (viper-char-at-pos 'backward)))
2693 (if com (insert char))
2695 (setq viper-d-char char)
2697 (viper-loop (1- (if (> arg 0) arg (- arg)))
2698 (delete-char 1 t)
2699 (insert char))
2701 (viper-adjust-undo)
2702 (backward-char arg)
2706 ;; basic cursor movement. j, k, l, h commands.
2708 (defun viper-forward-char (arg)
2709 "Move point right ARG characters (left if ARG negative).
2710 On reaching end of line, stop and signal error."
2711 (interactive "P")
2712 (viper-leave-region-active)
2713 (let ((val (viper-p-val arg))
2714 (com (viper-getcom arg)))
2715 (if com (viper-move-marker-locally 'viper-com-point (point)))
2716 (if viper-ex-style-motion
2717 (progn
2718 ;; the boundary condition check gets weird here because
2719 ;; forward-char may be the parameter of a delete, and 'dl' works
2720 ;; just like 'x' for the last char on a line, so we have to allow
2721 ;; the forward motion before the 'viper-execute-com', but, of
2722 ;; course, 'dl' doesn't work on an empty line, so we have to
2723 ;; catch that condition before 'viper-execute-com'
2724 (if (and (eolp) (bolp)) (error "") (forward-char val))
2725 (if com (viper-execute-com 'viper-forward-char val com))
2726 (if (eolp) (progn (backward-char 1) (error ""))))
2727 (forward-char val)
2728 (if com (viper-execute-com 'viper-forward-char val com)))))
2731 (defun viper-backward-char (arg)
2732 "Move point left ARG characters (right if ARG negative).
2733 On reaching beginning of line, stop and signal error."
2734 (interactive "P")
2735 (viper-leave-region-active)
2736 (let ((val (viper-p-val arg))
2737 (com (viper-getcom arg)))
2738 (if com (viper-move-marker-locally 'viper-com-point (point)))
2739 (if viper-ex-style-motion
2740 (progn
2741 (if (bolp) (error "") (backward-char val))
2742 (if com (viper-execute-com 'viper-backward-char val com)))
2743 (backward-char val)
2744 (if com (viper-execute-com 'viper-backward-char val com)))))
2747 ;; Like forward-char, but doesn't move at end of buffer.
2748 ;; Returns distance traveled
2749 ;; (positive or 0, if arg positive; negative if arg negative).
2750 (defun viper-forward-char-carefully (&optional arg)
2751 (setq arg (or arg 1))
2752 (let ((pt (point)))
2753 (condition-case nil
2754 (forward-char arg)
2755 (error nil))
2756 (if (< (point) pt) ; arg was negative
2757 (- (viper-chars-in-region pt (point)))
2758 (viper-chars-in-region pt (point)))))
2761 ;; Like backward-char, but doesn't move at beg of buffer.
2762 ;; Returns distance traveled
2763 ;; (negative or 0, if arg positive; positive if arg negative).
2764 (defun viper-backward-char-carefully (&optional arg)
2765 (setq arg (or arg 1))
2766 (let ((pt (point)))
2767 (condition-case nil
2768 (backward-char arg)
2769 (error nil))
2770 (if (> (point) pt) ; arg was negative
2771 (viper-chars-in-region pt (point))
2772 (- (viper-chars-in-region pt (point))))))
2774 (defun viper-next-line-carefully (arg)
2775 (condition-case nil
2776 (next-line arg)
2777 (error nil)))
2781 ;;; Word command
2783 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators for
2784 ;; word movement. When executed with a destructive command, \n is usually left
2785 ;; untouched for the last word. Viper uses syntax table to determine what is a
2786 ;; word and what is a separator. However, \n is always a separator. Also, if
2787 ;; viper-syntax-preference is 'vi, then `_' is part of the word.
2789 ;; skip only one \n
2790 (defun viper-skip-separators (forward)
2791 (if forward
2792 (progn
2793 (viper-skip-all-separators-forward 'within-line)
2794 (if (looking-at "\n")
2795 (progn
2796 (forward-char)
2797 (viper-skip-all-separators-forward 'within-line))))
2798 ;; check for eob and white space before it. move off of eob
2799 (if (and (eobp) (save-excursion
2800 (viper-backward-char-carefully)
2801 (viper-looking-at-separator)))
2802 (viper-backward-char-carefully))
2803 (viper-skip-all-separators-backward 'within-line)
2804 (viper-backward-char-carefully)
2805 (if (looking-at "\n")
2806 (viper-skip-all-separators-backward 'within-line)
2807 (or (viper-looking-at-separator) (forward-char)))))
2810 (defun viper-forward-word-kernel (val)
2811 (while (> val 0)
2812 (cond ((viper-looking-at-alpha)
2813 (viper-skip-alpha-forward "_")
2814 (viper-skip-separators t))
2815 ((viper-looking-at-separator)
2816 (viper-skip-separators t))
2817 ((not (viper-looking-at-alphasep))
2818 (viper-skip-nonalphasep-forward)
2819 (viper-skip-separators t)))
2820 (setq val (1- val))))
2822 ;; first skip non-newline separators backward, then skip \n. Then, if TWICE is
2823 ;; non-nil, skip non-\n back again, but don't overshoot the limit LIM.
2824 (defun viper-separator-skipback-special (twice lim)
2825 (let ((prev-char (viper-char-at-pos 'backward))
2826 (saved-point (point)))
2827 ;; skip non-newline separators backward
2828 (while (and (not (viper-memq-char prev-char '(nil \n)))
2829 (< lim (point))
2830 ;; must be non-newline separator
2831 (if (eq viper-syntax-preference 'strict-vi)
2832 (viper-memq-char prev-char '(?\ ?\t))
2833 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2834 (viper-backward-char-carefully)
2835 (setq prev-char (viper-char-at-pos 'backward)))
2837 (if (and (< lim (point)) (eq prev-char ?\n))
2838 (backward-char)
2839 ;; If we skipped to the next word and the prefix of this line doesn't
2840 ;; consist of separators preceded by a newline, then don't skip backwards
2841 ;; at all.
2842 (goto-char saved-point))
2843 (setq prev-char (viper-char-at-pos 'backward))
2845 ;; skip again, but make sure we don't overshoot the limit
2846 (if twice
2847 (while (and (not (viper-memq-char prev-char '(nil \n)))
2848 (< lim (point))
2849 ;; must be non-newline separator
2850 (if (eq viper-syntax-preference 'strict-vi)
2851 (viper-memq-char prev-char '(?\ ?\t))
2852 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2853 (viper-backward-char-carefully)
2854 (setq prev-char (viper-char-at-pos 'backward))))
2856 (if (= (point) lim)
2857 (viper-forward-char-carefully))
2861 (defun viper-forward-word (arg)
2862 "Forward word."
2863 (interactive "P")
2864 (viper-leave-region-active)
2865 (let ((val (viper-p-val arg))
2866 (com (viper-getcom arg)))
2867 (if com (viper-move-marker-locally 'viper-com-point (point)))
2868 (viper-forward-word-kernel val)
2869 (if com
2870 (progn
2871 (cond ((viper-char-equal com ?c)
2872 (viper-separator-skipback-special 'twice viper-com-point))
2873 ;; Yank words including the whitespace, but not newline
2874 ((viper-char-equal com ?y)
2875 (viper-separator-skipback-special nil viper-com-point))
2876 ((viper-dotable-command-p com)
2877 (viper-separator-skipback-special nil viper-com-point)))
2878 (viper-execute-com 'viper-forward-word val com)))
2882 (defun viper-forward-Word (arg)
2883 "Forward word delimited by white characters."
2884 (interactive "P")
2885 (viper-leave-region-active)
2886 (let ((val (viper-p-val arg))
2887 (com (viper-getcom arg)))
2888 (if com (viper-move-marker-locally 'viper-com-point (point)))
2889 (viper-loop val
2890 (viper-skip-nonseparators 'forward)
2891 (viper-skip-separators t))
2892 (if com (progn
2893 (cond ((viper-char-equal com ?c)
2894 (viper-separator-skipback-special 'twice viper-com-point))
2895 ;; Yank words including the whitespace, but not newline
2896 ((viper-char-equal com ?y)
2897 (viper-separator-skipback-special nil viper-com-point))
2898 ((viper-dotable-command-p com)
2899 (viper-separator-skipback-special nil viper-com-point)))
2900 (viper-execute-com 'viper-forward-Word val com)))))
2903 ;; this is a bit different from Vi, but Vi's end of word
2904 ;; makes no sense whatsoever
2905 (defun viper-end-of-word-kernel ()
2906 (if (viper-end-of-word-p) (forward-char))
2907 (if (viper-looking-at-separator)
2908 (viper-skip-all-separators-forward))
2910 (cond ((viper-looking-at-alpha) (viper-skip-alpha-forward "_"))
2911 ((not (viper-looking-at-alphasep)) (viper-skip-nonalphasep-forward)))
2912 (viper-backward-char-carefully))
2914 (defun viper-end-of-word-p ()
2915 (or (eobp)
2916 (save-excursion
2917 (cond ((viper-looking-at-alpha)
2918 (forward-char)
2919 (not (viper-looking-at-alpha)))
2920 ((not (viper-looking-at-alphasep))
2921 (forward-char)
2922 (viper-looking-at-alphasep))))))
2925 (defun viper-end-of-word (arg &optional careful)
2926 "Move point to end of current word."
2927 (interactive "P")
2928 (viper-leave-region-active)
2929 (let ((val (viper-p-val arg))
2930 (com (viper-getcom arg)))
2931 (if com (viper-move-marker-locally 'viper-com-point (point)))
2932 (viper-loop val (viper-end-of-word-kernel))
2933 (if com
2934 (progn
2935 (forward-char)
2936 (viper-execute-com 'viper-end-of-word val com)))))
2938 (defun viper-end-of-Word (arg)
2939 "Forward to end of word delimited by white character."
2940 (interactive "P")
2941 (viper-leave-region-active)
2942 (let ((val (viper-p-val arg))
2943 (com (viper-getcom arg)))
2944 (if com (viper-move-marker-locally 'viper-com-point (point)))
2945 (viper-loop val
2946 (viper-end-of-word-kernel)
2947 (viper-skip-nonseparators 'forward)
2948 (backward-char))
2949 (if com
2950 (progn
2951 (forward-char)
2952 (viper-execute-com 'viper-end-of-Word val com)))))
2954 (defun viper-backward-word-kernel (val)
2955 (while (> val 0)
2956 (viper-backward-char-carefully)
2957 (cond ((viper-looking-at-alpha)
2958 (viper-skip-alpha-backward "_"))
2959 ((viper-looking-at-separator)
2960 (forward-char)
2961 (viper-skip-separators nil)
2962 (viper-backward-char-carefully)
2963 (cond ((viper-looking-at-alpha)
2964 (viper-skip-alpha-backward "_"))
2965 ((not (viper-looking-at-alphasep))
2966 (viper-skip-nonalphasep-backward))
2967 ((bobp)) ; could still be at separator, but at beg of buffer
2968 (t (forward-char))))
2969 ((not (viper-looking-at-alphasep))
2970 (viper-skip-nonalphasep-backward)))
2971 (setq val (1- val))))
2973 (defun viper-backward-word (arg)
2974 "Backward word."
2975 (interactive "P")
2976 (viper-leave-region-active)
2977 (let ((val (viper-p-val arg))
2978 (com (viper-getcom arg)))
2979 (if com
2980 (let (i)
2981 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2982 (backward-char))
2983 (viper-move-marker-locally 'viper-com-point (point))
2984 (if i (forward-char))))
2985 (viper-backward-word-kernel val)
2986 (if com (viper-execute-com 'viper-backward-word val com))))
2988 (defun viper-backward-Word (arg)
2989 "Backward word delimited by white character."
2990 (interactive "P")
2991 (viper-leave-region-active)
2992 (let ((val (viper-p-val arg))
2993 (com (viper-getcom arg)))
2994 (if com
2995 (let (i)
2996 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2997 (backward-char))
2998 (viper-move-marker-locally 'viper-com-point (point))
2999 (if i (forward-char))))
3000 (viper-loop val
3001 (viper-skip-separators nil) ; nil means backward here
3002 (viper-skip-nonseparators 'backward))
3003 (if com (viper-execute-com 'viper-backward-Word val com))))
3007 ;; line commands
3009 (defun viper-beginning-of-line (arg)
3010 "Go to beginning of line."
3011 (interactive "P")
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 (beginning-of-line val)
3017 (if com (viper-execute-com 'viper-beginning-of-line val com))))
3019 (defun viper-bol-and-skip-white (arg)
3020 "Beginning of line at first non-white character."
3021 (interactive "P")
3022 (viper-leave-region-active)
3023 (let ((val (viper-p-val arg))
3024 (com (viper-getcom arg)))
3025 (if com (viper-move-marker-locally 'viper-com-point (point)))
3026 (forward-to-indentation (1- val))
3027 (if com (viper-execute-com 'viper-bol-and-skip-white val com))))
3029 (defun viper-goto-eol (arg)
3030 "Go to end of line."
3031 (interactive "P")
3032 (viper-leave-region-active)
3033 (let ((val (viper-p-val arg))
3034 (com (viper-getcom arg)))
3035 (if com (viper-move-marker-locally 'viper-com-point (point)))
3036 (end-of-line val)
3037 (if com (viper-execute-com 'viper-goto-eol val com))
3038 (if viper-ex-style-motion
3039 (if (and (eolp) (not (bolp))
3040 ;; a fix for viper-change-to-eol
3041 (not (equal viper-current-state 'insert-state)))
3042 (backward-char 1)
3043 ))))
3046 (defun viper-goto-col (arg)
3047 "Go to ARG's column."
3048 (interactive "P")
3049 (viper-leave-region-active)
3050 (let ((val (viper-p-val arg))
3051 (com (viper-getcom arg))
3052 line-len)
3053 (setq line-len
3054 (viper-chars-in-region
3055 (viper-line-pos 'start) (viper-line-pos 'end)))
3056 (if com (viper-move-marker-locally 'viper-com-point (point)))
3057 (beginning-of-line)
3058 (forward-char (1- (min line-len val)))
3059 (while (> (current-column) (1- val))
3060 (backward-char 1))
3061 (if com (viper-execute-com 'viper-goto-col val com))
3062 (save-excursion
3063 (end-of-line)
3064 (if (> val (current-column)) (error "")))
3068 (defun viper-next-line (arg)
3069 "Go to next line."
3070 (interactive "P")
3071 (viper-leave-region-active)
3072 (let ((val (viper-p-val arg))
3073 (com (viper-getCom arg)))
3074 (if com (viper-move-marker-locally 'viper-com-point (point)))
3075 (next-line val)
3076 (if viper-ex-style-motion
3077 (if (and (eolp) (not (bolp))) (backward-char 1)))
3078 (setq this-command 'next-line)
3079 (if com (viper-execute-com 'viper-next-line val com))))
3082 (defun viper-next-line-at-bol (arg)
3083 "Next line at beginning of line.
3084 If point is on a widget or a button, simulate clicking on that widget/button."
3085 (interactive "P")
3086 (let* ((field (get-char-property (point) 'field))
3087 (button (get-char-property (point) 'button))
3088 (doc (get-char-property (point) 'widget-doc))
3089 (widget (or field button doc)))
3090 (if (and widget
3091 (if (symbolp widget)
3092 (get widget 'widget-type)
3093 (and (consp widget)
3094 (get (widget-type widget) 'widget-type))))
3095 (widget-button-press (point))
3096 (if (button-at (point))
3097 (push-button)
3098 ;; not a widget or a button
3099 (viper-leave-region-active)
3100 (save-excursion
3101 (end-of-line)
3102 (if (eobp) (error "Last line in buffer")))
3103 (let ((val (viper-p-val arg))
3104 (com (viper-getCom arg)))
3105 (if com (viper-move-marker-locally 'viper-com-point (point)))
3106 (forward-line val)
3107 (back-to-indentation)
3108 (if com (viper-execute-com 'viper-next-line-at-bol val com)))))))
3111 (defun viper-previous-line (arg)
3112 "Go to previous line."
3113 (interactive "P")
3114 (viper-leave-region-active)
3115 (let ((val (viper-p-val arg))
3116 (com (viper-getCom arg)))
3117 (if com (viper-move-marker-locally 'viper-com-point (point)))
3118 (previous-line val)
3119 (if viper-ex-style-motion
3120 (if (and (eolp) (not (bolp))) (backward-char 1)))
3121 (setq this-command 'previous-line)
3122 (if com (viper-execute-com 'viper-previous-line val com))))
3125 (defun viper-previous-line-at-bol (arg)
3126 "Previous line at beginning of line."
3127 (interactive "P")
3128 (viper-leave-region-active)
3129 (save-excursion
3130 (beginning-of-line)
3131 (if (bobp) (error "First line in buffer")))
3132 (let ((val (viper-p-val arg))
3133 (com (viper-getCom arg)))
3134 (if com (viper-move-marker-locally 'viper-com-point (point)))
3135 (forward-line (- val))
3136 (back-to-indentation)
3137 (if com (viper-execute-com 'viper-previous-line val com))))
3139 (defun viper-change-to-eol (arg)
3140 "Change to end of line."
3141 (interactive "P")
3142 (viper-goto-eol (cons arg ?c)))
3144 (defun viper-kill-line (arg)
3145 "Delete line."
3146 (interactive "P")
3147 (viper-goto-eol (cons arg ?d)))
3149 (defun viper-erase-line (arg)
3150 "Erase line."
3151 (interactive "P")
3152 (viper-beginning-of-line (cons arg ?d)))
3155 ;;; Moving around
3157 (defun viper-goto-line (arg)
3158 "Go to ARG's line. Without ARG go to end of buffer."
3159 (interactive "P")
3160 (let ((val (viper-P-val arg))
3161 (com (viper-getCom arg)))
3162 (viper-move-marker-locally 'viper-com-point (point))
3163 (viper-deactivate-mark)
3164 (push-mark nil t)
3165 (if (null val)
3166 (goto-char (point-max))
3167 (goto-char (point-min))
3168 (forward-line (1- val)))
3170 ;; positioning is done twice: before and after command execution
3171 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3172 (back-to-indentation)
3174 (if com (viper-execute-com 'viper-goto-line val com))
3176 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3177 (back-to-indentation)
3180 ;; Find ARG's occurrence of CHAR on the current line.
3181 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
3182 ;; adjust point after search.
3183 (defun viper-find-char (arg char forward offset)
3184 (or (char-or-string-p char) (error ""))
3185 (let ((arg (if forward arg (- arg)))
3186 (cmd (if (eq viper-intermediate-command 'viper-repeat)
3187 (nth 5 viper-d-com)
3188 (viper-array-to-string (this-command-keys))))
3189 point region-beg region-end)
3190 (save-excursion
3191 (save-restriction
3192 (if (> arg 0) ; forward
3193 (progn
3194 (setq region-beg (point))
3195 (if viper-allow-multiline-replace-regions
3196 (viper-forward-paragraph 1)
3197 (end-of-line))
3198 (setq region-end (point)))
3199 (setq region-end (point))
3200 (if viper-allow-multiline-replace-regions
3201 (viper-backward-paragraph 1)
3202 (beginning-of-line))
3203 (setq region-beg (point)))
3204 (if (or (and (< arg 0)
3205 (< (- region-end region-beg)
3206 (if viper-allow-multiline-replace-regions
3207 2 1))
3208 (bolp))
3209 (and (> arg 0)
3210 (< (- region-end region-beg)
3211 (if viper-allow-multiline-replace-regions
3212 3 2))
3213 (eolp)))
3214 (error "Command `%s': At %s of %s"
3216 (if (> arg 0) "end" "beginning")
3217 (if viper-allow-multiline-replace-regions
3218 "paragraph" "line")))
3219 (narrow-to-region region-beg region-end)
3220 ;; if arg > 0, point is forwarded before search.
3221 (if (> arg 0) (goto-char (1+ (point-min)))
3222 (goto-char (point-max)))
3223 (if (let ((case-fold-search nil))
3224 (search-forward (char-to-string char) nil 0 arg))
3225 (setq point (point))
3226 (error "Command `%s': `%c' not found" cmd char))))
3227 (goto-char point)
3228 (if (> arg 0)
3229 (backward-char (if offset 2 1))
3230 (forward-char (if offset 1 0)))))
3232 (defun viper-find-char-forward (arg)
3233 "Find char on the line.
3234 If called interactively read the char to find from the terminal, and if
3235 called from viper-repeat, the char last used is used. This behavior is
3236 controlled by the sign of prefix numeric value."
3237 (interactive "P")
3238 (let ((val (viper-p-val arg))
3239 (com (viper-getcom arg))
3240 (cmd-representation (nth 5 viper-d-com)))
3241 (if (> val 0)
3242 ;; this means that the function was called interactively
3243 (setq viper-f-char (read-char)
3244 viper-f-forward t
3245 viper-f-offset nil)
3246 ;; viper-repeat --- set viper-F-char from command-keys
3247 (setq viper-F-char (if (stringp cmd-representation)
3248 (viper-seq-last-elt cmd-representation)
3249 viper-F-char)
3250 viper-f-char viper-F-char)
3251 (setq val (- val)))
3252 (if com (viper-move-marker-locally 'viper-com-point (point)))
3253 (viper-find-char
3254 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t nil)
3255 (setq val (- val))
3256 (if com
3257 (progn
3258 (setq viper-F-char viper-f-char) ; set new viper-F-char
3259 (forward-char)
3260 (viper-execute-com 'viper-find-char-forward val com)))))
3262 (defun viper-goto-char-forward (arg)
3263 "Go up to char ARG forward on line."
3264 (interactive "P")
3265 (let ((val (viper-p-val arg))
3266 (com (viper-getcom arg))
3267 (cmd-representation (nth 5 viper-d-com)))
3268 (if (> val 0)
3269 ;; this means that the function was called interactively
3270 (setq viper-f-char (read-char)
3271 viper-f-forward t
3272 viper-f-offset t)
3273 ;; viper-repeat --- set viper-F-char from command-keys
3274 (setq viper-F-char (if (stringp cmd-representation)
3275 (viper-seq-last-elt cmd-representation)
3276 viper-F-char)
3277 viper-f-char viper-F-char)
3278 (setq val (- val)))
3279 (if com (viper-move-marker-locally 'viper-com-point (point)))
3280 (viper-find-char
3281 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t t)
3282 (setq val (- val))
3283 (if com
3284 (progn
3285 (setq viper-F-char viper-f-char) ; set new viper-F-char
3286 (forward-char)
3287 (viper-execute-com 'viper-goto-char-forward val com)))))
3289 (defun viper-find-char-backward (arg)
3290 "Find char ARG on line backward."
3291 (interactive "P")
3292 (let ((val (viper-p-val arg))
3293 (com (viper-getcom arg))
3294 (cmd-representation (nth 5 viper-d-com)))
3295 (if (> val 0)
3296 ;; this means that the function was called interactively
3297 (setq viper-f-char (read-char)
3298 viper-f-forward nil
3299 viper-f-offset nil)
3300 ;; viper-repeat --- set viper-F-char from command-keys
3301 (setq viper-F-char (if (stringp cmd-representation)
3302 (viper-seq-last-elt cmd-representation)
3303 viper-F-char)
3304 viper-f-char viper-F-char)
3305 (setq val (- val)))
3306 (if com (viper-move-marker-locally 'viper-com-point (point)))
3307 (viper-find-char
3308 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil nil)
3309 (setq val (- val))
3310 (if com
3311 (progn
3312 (setq viper-F-char viper-f-char) ; set new viper-F-char
3313 (viper-execute-com 'viper-find-char-backward val com)))))
3315 (defun viper-goto-char-backward (arg)
3316 "Go up to char ARG backward on line."
3317 (interactive "P")
3318 (let ((val (viper-p-val arg))
3319 (com (viper-getcom arg))
3320 (cmd-representation (nth 5 viper-d-com)))
3321 (if (> val 0)
3322 ;; this means that the function was called interactively
3323 (setq viper-f-char (read-char)
3324 viper-f-forward nil
3325 viper-f-offset t)
3326 ;; viper-repeat --- set viper-F-char from command-keys
3327 (setq viper-F-char (if (stringp cmd-representation)
3328 (viper-seq-last-elt cmd-representation)
3329 viper-F-char)
3330 viper-f-char viper-F-char)
3331 (setq val (- val)))
3332 (if com (viper-move-marker-locally 'viper-com-point (point)))
3333 (viper-find-char
3334 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil t)
3335 (setq val (- val))
3336 (if com
3337 (progn
3338 (setq viper-F-char viper-f-char) ; set new viper-F-char
3339 (viper-execute-com 'viper-goto-char-backward val com)))))
3341 (defun viper-repeat-find (arg)
3342 "Repeat previous find command."
3343 (interactive "P")
3344 (let ((val (viper-p-val arg))
3345 (com (viper-getcom arg)))
3346 (viper-deactivate-mark)
3347 (if com (viper-move-marker-locally 'viper-com-point (point)))
3348 (viper-find-char val viper-f-char viper-f-forward viper-f-offset)
3349 (if com
3350 (progn
3351 (if viper-f-forward (forward-char))
3352 (viper-execute-com 'viper-repeat-find val com)))))
3354 (defun viper-repeat-find-opposite (arg)
3355 "Repeat previous find command in the opposite direction."
3356 (interactive "P")
3357 (let ((val (viper-p-val arg))
3358 (com (viper-getcom arg)))
3359 (viper-deactivate-mark)
3360 (if com (viper-move-marker-locally 'viper-com-point (point)))
3361 (viper-find-char val viper-f-char (not viper-f-forward) viper-f-offset)
3362 (if com
3363 (progn
3364 (if viper-f-forward (forward-char))
3365 (viper-execute-com 'viper-repeat-find-opposite val com)))))
3368 ;; window scrolling etc.
3370 (defun viper-window-top (arg)
3371 "Go to home window line."
3372 (interactive "P")
3373 (let ((val (viper-p-val arg))
3374 (com (viper-getCom arg)))
3375 (viper-leave-region-active)
3376 (if com (viper-move-marker-locally 'viper-com-point (point)))
3377 (push-mark nil t)
3378 (move-to-window-line (1- val))
3380 ;; positioning is done twice: before and after command execution
3381 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3382 (back-to-indentation)
3384 (if com (viper-execute-com 'viper-window-top val com))
3386 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3387 (back-to-indentation)
3390 (defun viper-window-middle (arg)
3391 "Go to middle window line."
3392 (interactive "P")
3393 (let ((val (viper-p-val arg))
3394 (com (viper-getCom arg)))
3395 (viper-leave-region-active)
3396 (if com (viper-move-marker-locally 'viper-com-point (point)))
3397 (push-mark nil t)
3398 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3400 ;; positioning is done twice: before and after command execution
3401 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3402 (back-to-indentation)
3404 (if com (viper-execute-com 'viper-window-middle val com))
3406 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3407 (back-to-indentation)
3410 (defun viper-window-bottom (arg)
3411 "Go to last window line."
3412 (interactive "P")
3413 (let ((val (viper-p-val arg))
3414 (com (viper-getCom arg)))
3415 (viper-leave-region-active)
3416 (if com (viper-move-marker-locally 'viper-com-point (point)))
3417 (push-mark nil t)
3418 (move-to-window-line (- val))
3420 ;; positioning is done twice: before and after command execution
3421 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3422 (back-to-indentation)
3424 (if com (viper-execute-com 'viper-window-bottom val com))
3426 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3427 (back-to-indentation)
3430 (defun viper-line-to-top (arg)
3431 "Put current line on the home line."
3432 (interactive "p")
3433 (recenter (1- arg)))
3435 (defun viper-line-to-middle (arg)
3436 "Put current line on the middle line."
3437 (interactive "p")
3438 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3440 (defun viper-line-to-bottom (arg)
3441 "Put current line on the last line."
3442 (interactive "p")
3443 (recenter (- (window-height) (1+ arg))))
3445 ;; If point is within viper-search-scroll-threshold of window top or bottom,
3446 ;; scroll up or down 1/7 of window height, depending on whether we are at the
3447 ;; bottom or at the top of the window. This function is called by viper-search
3448 ;; (which is called from viper-search-forward/backward/next). If the value of
3449 ;; viper-search-scroll-threshold is negative - don't scroll.
3450 (defun viper-adjust-window ()
3451 (let ((win-height (viper-cond-compile-for-xemacs-or-emacs
3452 (window-displayed-height) ; xemacs
3453 ;; emacs
3454 (1- (window-height)) ; adjust for modeline
3456 (pt (point))
3457 at-top-p at-bottom-p
3458 min-scroll direction)
3459 (save-excursion
3460 (move-to-window-line 0) ; top
3461 (setq at-top-p
3462 (<= (count-lines pt (point))
3463 viper-search-scroll-threshold))
3464 (move-to-window-line -1) ; bottom
3465 (setq at-bottom-p
3466 (<= (count-lines pt (point)) viper-search-scroll-threshold))
3468 (cond (at-top-p (setq min-scroll (1- viper-search-scroll-threshold)
3469 direction 1))
3470 (at-bottom-p (setq min-scroll (1+ viper-search-scroll-threshold)
3471 direction -1)))
3472 (if min-scroll
3473 (recenter
3474 (* (max min-scroll (/ win-height 7)) direction)))
3478 ;; paren match
3479 ;; must correct this to only match ( to ) etc. On the other hand
3480 ;; it is good that paren match gets confused, because that way you
3481 ;; catch _all_ imbalances.
3483 (defun viper-paren-match (arg)
3484 "Go to the matching parenthesis."
3485 (interactive "P")
3486 (viper-leave-region-active)
3487 (let ((com (viper-getcom arg))
3488 (parse-sexp-ignore-comments viper-parse-sexp-ignore-comments)
3489 anchor-point)
3490 (if (integerp arg)
3491 (if (or (> arg 99) (< arg 1))
3492 (error "Prefix must be between 1 and 99")
3493 (goto-char
3494 (if (> (point-max) 80000)
3495 (* (/ (point-max) 100) arg)
3496 (/ (* (point-max) arg) 100)))
3497 (back-to-indentation))
3498 (let (beg-lim end-lim)
3499 (if (and (eolp) (not (bolp))) (forward-char -1))
3500 (if (not (looking-at "[][(){}]"))
3501 (setq anchor-point (point)))
3502 (save-excursion
3503 (beginning-of-line)
3504 (setq beg-lim (point))
3505 (end-of-line)
3506 (setq end-lim (point)))
3507 (cond ((re-search-forward "[][(){}]" end-lim t)
3508 (backward-char) )
3509 ((re-search-backward "[][(){}]" beg-lim t))
3511 (error "No matching character on line"))))
3512 (cond ((looking-at "[\(\[{]")
3513 (if com (viper-move-marker-locally 'viper-com-point (point)))
3514 (forward-sexp 1)
3515 (if com
3516 (viper-execute-com 'viper-paren-match nil com)
3517 (backward-char)))
3518 (anchor-point
3519 (if com
3520 (progn
3521 (viper-move-marker-locally 'viper-com-point anchor-point)
3522 (forward-char 1)
3523 (viper-execute-com 'viper-paren-match nil com)
3525 ((looking-at "[])}]")
3526 (forward-char)
3527 (if com (viper-move-marker-locally 'viper-com-point (point)))
3528 (backward-sexp 1)
3529 (if com (viper-execute-com 'viper-paren-match nil com)))
3530 (t (error ""))))))
3532 (defun viper-toggle-parse-sexp-ignore-comments ()
3533 (interactive)
3534 (setq viper-parse-sexp-ignore-comments
3535 (not viper-parse-sexp-ignore-comments))
3536 (princ (format
3537 "From now on, `%%' will %signore parentheses inside comment fields"
3538 (if viper-parse-sexp-ignore-comments "" "NOT "))))
3541 ;; sentence, paragraph and heading
3543 (defun viper-forward-sentence (arg)
3544 "Forward sentence."
3545 (interactive "P")
3546 (or (eq last-command this-command)
3547 (push-mark nil t))
3548 (let ((val (viper-p-val arg))
3549 (com (viper-getcom arg)))
3550 (if com (viper-move-marker-locally 'viper-com-point (point)))
3551 (forward-sentence val)
3552 (if com (viper-execute-com 'viper-forward-sentence nil com))))
3554 (defun viper-backward-sentence (arg)
3555 "Backward sentence."
3556 (interactive "P")
3557 (or (eq last-command this-command)
3558 (push-mark nil t))
3559 (let ((val (viper-p-val arg))
3560 (com (viper-getcom arg)))
3561 (if com (viper-move-marker-locally 'viper-com-point (point)))
3562 (backward-sentence val)
3563 (if com (viper-execute-com 'viper-backward-sentence nil com))))
3565 (defun viper-forward-paragraph (arg)
3566 "Forward paragraph."
3567 (interactive "P")
3568 (or (eq last-command this-command)
3569 (push-mark nil t))
3570 (let ((val (viper-p-val arg))
3571 ;; if you want d} operate on whole lines, change viper-getcom to
3572 ;; viper-getCom below
3573 (com (viper-getcom arg)))
3574 (if com (viper-move-marker-locally 'viper-com-point (point)))
3575 (forward-paragraph val)
3576 (if com
3577 (progn
3578 (backward-char 1)
3579 (viper-execute-com 'viper-forward-paragraph nil com)))))
3581 (defun viper-backward-paragraph (arg)
3582 "Backward paragraph."
3583 (interactive "P")
3584 (or (eq last-command this-command)
3585 (push-mark nil t))
3586 (let ((val (viper-p-val arg))
3587 ;; if you want d{ operate on whole lines, change viper-getcom to
3588 ;; viper-getCom below
3589 (com (viper-getcom arg)))
3590 (if com (viper-move-marker-locally 'viper-com-point (point)))
3591 (backward-paragraph val)
3592 (if com
3593 (progn
3594 (forward-char 1)
3595 (viper-execute-com 'viper-backward-paragraph nil com)
3596 (backward-char 1)))))
3598 ;; should be mode-specific
3599 (defun viper-prev-heading (arg)
3600 (interactive "P")
3601 (let ((val (viper-p-val arg))
3602 (com (viper-getCom arg)))
3603 (if com (viper-move-marker-locally 'viper-com-point (point)))
3604 (re-search-backward viper-heading-start nil t val)
3605 (goto-char (match-beginning 0))
3606 (if com (viper-execute-com 'viper-prev-heading nil com))))
3608 (defun viper-heading-end (arg)
3609 (interactive "P")
3610 (let ((val (viper-p-val arg))
3611 (com (viper-getCom arg)))
3612 (if com (viper-move-marker-locally 'viper-com-point (point)))
3613 (re-search-forward viper-heading-end nil t val)
3614 (goto-char (match-beginning 0))
3615 (if com (viper-execute-com 'viper-heading-end nil com))))
3617 (defun viper-next-heading (arg)
3618 (interactive "P")
3619 (let ((val (viper-p-val arg))
3620 (com (viper-getCom arg)))
3621 (if com (viper-move-marker-locally 'viper-com-point (point)))
3622 (end-of-line)
3623 (re-search-forward viper-heading-start nil t val)
3624 (goto-char (match-beginning 0))
3625 (if com (viper-execute-com 'viper-next-heading nil com))))
3628 ;; scrolling
3630 (defun viper-scroll-screen (arg)
3631 "Scroll to next screen."
3632 (interactive "p")
3633 (condition-case nil
3634 (if (> arg 0)
3635 (while (> arg 0)
3636 (scroll-up)
3637 (setq arg (1- arg)))
3638 (while (> 0 arg)
3639 (scroll-down)
3640 (setq arg (1+ arg))))
3641 (error (beep 1)
3642 (if (> arg 0)
3643 (progn
3644 (message "End of buffer")
3645 (goto-char (point-max)))
3646 (message "Beginning of buffer")
3647 (goto-char (point-min))))
3650 (defun viper-scroll-screen-back (arg)
3651 "Scroll to previous screen."
3652 (interactive "p")
3653 (viper-scroll-screen (- arg)))
3655 (defun viper-scroll-down (arg)
3656 "Pull down half screen."
3657 (interactive "P")
3658 (condition-case nil
3659 (if (null arg)
3660 (scroll-down (/ (window-height) 2))
3661 (scroll-down arg))
3662 (error (beep 1)
3663 (message "Beginning of buffer")
3664 (goto-char (point-min)))))
3666 (defun viper-scroll-down-one (arg)
3667 "Scroll up one line."
3668 (interactive "p")
3669 (scroll-down arg))
3671 (defun viper-scroll-up (arg)
3672 "Pull up half screen."
3673 (interactive "P")
3674 (condition-case nil
3675 (if (null arg)
3676 (scroll-up (/ (window-height) 2))
3677 (scroll-up arg))
3678 (error (beep 1)
3679 (message "End of buffer")
3680 (goto-char (point-max)))))
3682 (defun viper-scroll-up-one (arg)
3683 "Scroll down one line."
3684 (interactive "p")
3685 (scroll-up arg))
3688 ;; searching
3690 (defun viper-if-string (prompt)
3691 (if (memq viper-intermediate-command
3692 '(viper-command-argument viper-digit-argument viper-repeat))
3693 (setq viper-this-command-keys (this-command-keys)))
3694 (let ((s (viper-read-string-with-history
3695 prompt
3696 nil ; no initial
3697 'viper-search-history
3698 (car viper-search-history))))
3699 (if (not (string= s ""))
3700 (setq viper-s-string s))))
3703 (defun viper-toggle-search-style (arg)
3704 "Toggle the value of viper-case-fold-search/viper-re-search.
3705 Without prefix argument, will ask which search style to toggle. With prefix
3706 arg 1,toggles viper-case-fold-search; with arg 2 toggles viper-re-search.
3708 Although this function is bound to \\[viper-toggle-search-style], the most
3709 convenient way to use it is to bind `//' to the macro
3710 `1 M-x viper-toggle-search-style' and `///' to
3711 `2 M-x viper-toggle-search-style'. In this way, hitting `//' quickly will
3712 toggle case-fold-search and hitting `/' three times witth toggle regexp
3713 search. Macros are more convenient in this case because they don't affect
3714 the Emacs binding of `/'."
3715 (interactive "P")
3716 (let (msg)
3717 (cond ((or (eq arg 1)
3718 (and (null arg)
3719 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3720 (if viper-case-fold-search
3721 "case-insensitive" "case-sensitive")
3722 (if viper-case-fold-search
3723 "case-sensitive"
3724 "case-insensitive")))))
3725 (setq viper-case-fold-search (null viper-case-fold-search))
3726 (if viper-case-fold-search
3727 (setq msg "Search becomes case-insensitive")
3728 (setq msg "Search becomes case-sensitive")))
3729 ((or (eq arg 2)
3730 (and (null arg)
3731 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3732 (if viper-re-search
3733 "regexp-search" "vanilla-search")
3734 (if viper-re-search
3735 "vanilla-search"
3736 "regexp-search")))))
3737 (setq viper-re-search (null viper-re-search))
3738 (if viper-re-search
3739 (setq msg "Search becomes regexp-style")
3740 (setq msg "Search becomes vanilla-style")))
3742 (setq msg "Search style remains unchanged")))
3743 (princ msg t)))
3745 (defun viper-set-searchstyle-toggling-macros (unset &optional major-mode)
3746 "Set the macros for toggling the search style in Viper's vi-state.
3747 The macro that toggles case sensitivity is bound to `//', and the one that
3748 toggles regexp search is bound to `///'.
3749 With a prefix argument, this function unsets the macros.
3750 If MAJOR-MODE is set, set the macros only in that major mode."
3751 (interactive "P")
3752 (let (scope)
3753 (if (and major-mode (symbolp major-mode))
3754 (setq scope major-mode)
3755 (setq scope 't))
3756 (or noninteractive
3757 (if (not unset)
3758 (progn
3759 ;; toggle case sensitivity in search
3760 (viper-record-kbd-macro
3761 "//" 'vi-state
3762 [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]
3763 scope)
3764 ;; toggle regexp/vanila search
3765 (viper-record-kbd-macro
3766 "///" 'vi-state
3767 [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]
3768 scope)
3769 (if (interactive-p)
3770 (message
3771 "// and /// now toggle case-sensitivity and regexp search")))
3772 (viper-unrecord-kbd-macro "//" 'vi-state)
3773 (sit-for 2)
3774 (viper-unrecord-kbd-macro "///" 'vi-state)))
3778 (defun viper-set-parsing-style-toggling-macro (unset)
3779 "Set `%%%' to be a macro that toggles whether comment fields should be parsed for matching parentheses.
3780 This is used in conjunction with the `%' command.
3782 With a prefix argument, unsets the macro."
3783 (interactive "P")
3784 (or noninteractive
3785 (if (not unset)
3786 (progn
3787 ;; Make %%% toggle parsing comments for matching parentheses
3788 (viper-record-kbd-macro
3789 "%%%" 'vi-state
3790 [(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]
3792 (if (interactive-p)
3793 (message
3794 "%%%%%% now toggles whether comments should be parsed for matching parentheses")))
3795 (viper-unrecord-kbd-macro "%%%" 'vi-state))))
3798 (defun viper-set-emacs-state-searchstyle-macros (unset &optional arg-majormode)
3799 "Set the macros for toggling the search style in Viper's emacs-state.
3800 The macro that toggles case sensitivity is bound to `//', and the one that
3801 toggles regexp search is bound to `///'.
3802 With a prefix argument, this function unsets the macros.
3803 If the optional prefix argument is non-nil and specifies a valid major mode,
3804 this sets the macros only in the macros in that major mode. Otherwise,
3805 the macros are set in the current major mode.
3806 \(When unsetting the macros, the second argument has no effect.\)"
3807 (interactive "P")
3808 (or noninteractive
3809 (if (not unset)
3810 (progn
3811 ;; toggle case sensitivity in search
3812 (viper-record-kbd-macro
3813 "//" 'emacs-state
3814 [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]
3815 (or arg-majormode major-mode))
3816 ;; toggle regexp/vanila search
3817 (viper-record-kbd-macro
3818 "///" 'emacs-state
3819 [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]
3820 (or arg-majormode major-mode))
3821 (if (interactive-p)
3822 (message
3823 "// and /// now toggle case-sensitivity and regexp search.")))
3824 (viper-unrecord-kbd-macro "//" 'emacs-state)
3825 (sit-for 2)
3826 (viper-unrecord-kbd-macro "///" 'emacs-state))))
3829 (defun viper-search-forward (arg)
3830 "Search a string forward.
3831 ARG is used to find the ARG's occurrence of the string.
3832 Null string will repeat previous search."
3833 (interactive "P")
3834 (let ((val (viper-P-val arg))
3835 (com (viper-getcom arg))
3836 (old-str viper-s-string)
3837 debug-on-error)
3838 (setq viper-s-forward t)
3839 (viper-if-string "/")
3840 ;; this is not used at present, but may be used later
3841 (if (or (not (equal old-str viper-s-string))
3842 (not (markerp viper-local-search-start-marker))
3843 (not (marker-buffer viper-local-search-start-marker)))
3844 (setq viper-local-search-start-marker (point-marker)))
3845 (viper-search viper-s-string t val)
3846 (if com
3847 (progn
3848 (viper-move-marker-locally 'viper-com-point (mark t))
3849 (viper-execute-com 'viper-search-next val com)))
3852 (defun viper-search-backward (arg)
3853 "Search a string backward.
3854 ARG is used to find the ARG's occurrence of the string.
3855 Null string will repeat previous search."
3856 (interactive "P")
3857 (let ((val (viper-P-val arg))
3858 (com (viper-getcom arg))
3859 (old-str viper-s-string)
3860 debug-on-error)
3861 (setq viper-s-forward nil)
3862 (viper-if-string "?")
3863 ;; this is not used at present, but may be used later
3864 (if (or (not (equal old-str viper-s-string))
3865 (not (markerp viper-local-search-start-marker))
3866 (not (marker-buffer viper-local-search-start-marker)))
3867 (setq viper-local-search-start-marker (point-marker)))
3868 (viper-search viper-s-string nil val)
3869 (if com
3870 (progn
3871 (viper-move-marker-locally 'viper-com-point (mark t))
3872 (viper-execute-com 'viper-search-next val com)))))
3875 ;; Search for COUNT's occurrence of STRING.
3876 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3877 ;; INIT-POINT is the position where search is to start.
3878 ;; Arguments:
3879 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3880 (defun viper-search (string forward arg
3881 &optional no-offset init-point fail-if-not-found)
3882 (if (not (equal string ""))
3883 (let ((val (viper-p-val arg))
3884 (com (viper-getcom arg))
3885 (offset (not no-offset))
3886 (case-fold-search viper-case-fold-search)
3887 (start-point (or init-point (point))))
3888 (viper-deactivate-mark)
3889 (if forward
3890 (condition-case nil
3891 (progn
3892 (if offset (viper-forward-char-carefully))
3893 (if viper-re-search
3894 (progn
3895 (re-search-forward string nil nil val)
3896 (re-search-backward string))
3897 (search-forward string nil nil val)
3898 (search-backward string))
3899 (if (not (equal start-point (point)))
3900 (push-mark start-point t)))
3901 (search-failed
3902 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3903 (progn
3904 (message "Search wrapped around BOTTOM of buffer")
3905 (goto-char (point-min))
3906 (viper-search string forward (cons 1 com) t start-point 'fail)
3907 ;; don't wait in macros
3908 (or executing-kbd-macro
3909 (memq viper-intermediate-command
3910 '(viper-repeat
3911 viper-digit-argument
3912 viper-command-argument))
3913 (sit-for 2))
3914 ;; delete the wrap-around message
3915 (message "")
3917 (goto-char start-point)
3918 (error "`%s': %s not found"
3919 string
3920 (if viper-re-search "Pattern" "String"))
3922 ;; backward
3923 (condition-case nil
3924 (progn
3925 (if viper-re-search
3926 (re-search-backward string nil nil val)
3927 (search-backward string nil nil val))
3928 (if (not (equal start-point (point)))
3929 (push-mark start-point t)))
3930 (search-failed
3931 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3932 (progn
3933 (message "Search wrapped around TOP of buffer")
3934 (goto-char (point-max))
3935 (viper-search string forward (cons 1 com) t start-point 'fail)
3936 ;; don't wait in macros
3937 (or executing-kbd-macro
3938 (memq viper-intermediate-command
3939 '(viper-repeat
3940 viper-digit-argument
3941 viper-command-argument))
3942 (sit-for 2))
3943 ;; delete the wrap-around message
3944 (message "")
3946 (goto-char start-point)
3947 (error "`%s': %s not found"
3948 string
3949 (if viper-re-search "Pattern" "String"))
3950 ))))
3951 ;; pull up or down if at top/bottom of window
3952 (viper-adjust-window)
3953 ;; highlight the result of search
3954 ;; don't wait and don't highlight in macros
3955 (or executing-kbd-macro
3956 (memq viper-intermediate-command
3957 '(viper-repeat viper-digit-argument viper-command-argument))
3958 (viper-flash-search-pattern))
3961 (defun viper-search-next (arg)
3962 "Repeat previous search."
3963 (interactive "P")
3964 (let ((val (viper-p-val arg))
3965 (com (viper-getcom arg))
3966 debug-on-error)
3967 (if (or (null viper-s-string) (string= viper-s-string ""))
3968 (error viper-NoPrevSearch))
3969 (viper-search viper-s-string viper-s-forward arg)
3970 (if com
3971 (progn
3972 (viper-move-marker-locally 'viper-com-point (mark t))
3973 (viper-execute-com 'viper-search-next val com)))))
3975 (defun viper-search-Next (arg)
3976 "Repeat previous search in the reverse direction."
3977 (interactive "P")
3978 (let ((val (viper-p-val arg))
3979 (com (viper-getcom arg))
3980 debug-on-error)
3981 (if (null viper-s-string) (error viper-NoPrevSearch))
3982 (viper-search viper-s-string (not viper-s-forward) arg)
3983 (if com
3984 (progn
3985 (viper-move-marker-locally 'viper-com-point (mark t))
3986 (viper-execute-com 'viper-search-Next val com)))))
3989 ;; Search contents of buffer defined by one of Viper's motion commands.
3990 ;; Repeatable via `n' and `N'.
3991 (defun viper-buffer-search-enable (&optional c)
3992 (cond (c (setq viper-buffer-search-char c))
3993 ((null viper-buffer-search-char)
3994 (setq viper-buffer-search-char ?g)))
3995 (define-key viper-vi-basic-map
3996 (cond ((viper-characterp viper-buffer-search-char)
3997 (char-to-string viper-buffer-search-char))
3998 (t (error "viper-buffer-search-char: wrong value type, %S"
3999 viper-buffer-search-char)))
4000 'viper-command-argument)
4001 (aset viper-exec-array viper-buffer-search-char 'viper-exec-buffer-search)
4002 (setq viper-prefix-commands
4003 (cons viper-buffer-search-char viper-prefix-commands)))
4005 ;; This is a Viper wraper for isearch-forward.
4006 (defun viper-isearch-forward (arg)
4007 "Do incremental search forward."
4008 (interactive "P")
4009 ;; emacs bug workaround
4010 (if (listp arg) (setq arg (car arg)))
4011 (viper-exec-form-in-emacs (list 'isearch-forward arg)))
4013 ;; This is a Viper wraper for isearch-backward."
4014 (defun viper-isearch-backward (arg)
4015 "Do incremental search backward."
4016 (interactive "P")
4017 ;; emacs bug workaround
4018 (if (listp arg) (setq arg (car arg)))
4019 (viper-exec-form-in-emacs (list 'isearch-backward arg)))
4022 ;; visiting and killing files, buffers
4024 (defun viper-switch-to-buffer ()
4025 "Switch to buffer in the current window."
4026 (interactive)
4027 (let ((other-buffer (other-buffer (current-buffer)))
4028 buffer)
4029 (setq buffer
4030 (funcall viper-read-buffer-function
4031 "Switch to buffer in this window: " other-buffer))
4032 (switch-to-buffer buffer)))
4034 (defun viper-switch-to-buffer-other-window ()
4035 "Switch to buffer in another window."
4036 (interactive)
4037 (let ((other-buffer (other-buffer (current-buffer)))
4038 buffer)
4039 (setq buffer
4040 (funcall viper-read-buffer-function
4041 "Switch to buffer in another window: " other-buffer))
4042 (switch-to-buffer-other-window buffer)))
4044 (defun viper-kill-buffer ()
4045 "Kill a buffer."
4046 (interactive)
4047 (let (buffer buffer-name)
4048 (setq buffer-name
4049 (funcall viper-read-buffer-function
4050 (format "Kill buffer \(%s\): "
4051 (buffer-name (current-buffer)))))
4052 (setq buffer
4053 (if (null buffer-name)
4054 (current-buffer)
4055 (get-buffer buffer-name)))
4056 (if (null buffer) (error "`%s': No such buffer" buffer-name))
4057 (if (or (not (buffer-modified-p buffer))
4058 (y-or-n-p
4059 (format
4060 "Buffer `%s' is modified, are you sure you want to kill it? "
4061 buffer-name)))
4062 (kill-buffer buffer)
4063 (error "Buffer not killed"))))
4067 ;; yank and pop
4069 (defsubst viper-yank (text)
4070 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
4071 (insert text)
4072 (setq this-command 'yank))
4074 (defun viper-put-back (arg)
4075 "Put back after point/below line."
4076 (interactive "P")
4077 (let ((val (viper-p-val arg))
4078 (text (if viper-use-register
4079 (cond ((viper-valid-register viper-use-register '(digit))
4080 (current-kill
4081 (- viper-use-register ?1) 'do-not-rotate))
4082 ((viper-valid-register viper-use-register)
4083 (get-register (downcase viper-use-register)))
4084 (t (error viper-InvalidRegister viper-use-register)))
4085 (current-kill 0)))
4086 sv-point chars-inserted lines-inserted)
4087 (if (null text)
4088 (if viper-use-register
4089 (let ((reg viper-use-register))
4090 (setq viper-use-register nil)
4091 (error viper-EmptyRegister reg))
4092 (error "")))
4093 (setq viper-use-register nil)
4094 (if (viper-end-with-a-newline-p text)
4095 (progn
4096 (end-of-line)
4097 (if (eobp)
4098 (insert "\n")
4099 (forward-line 1))
4100 (beginning-of-line))
4101 (if (not (eolp)) (viper-forward-char-carefully)))
4102 (set-marker (viper-mark-marker) (point) (current-buffer))
4103 (viper-set-destructive-command
4104 (list 'viper-put-back val nil viper-use-register nil nil))
4105 (setq sv-point (point))
4106 (viper-loop val (viper-yank text))
4107 (setq chars-inserted (abs (- (point) sv-point))
4108 lines-inserted (abs (count-lines (point) sv-point)))
4109 (if (or (> chars-inserted viper-change-notification-threshold)
4110 (> lines-inserted viper-change-notification-threshold))
4111 (unless (viper-is-in-minibuffer)
4112 (message "Inserted %d character(s), %d line(s)"
4113 chars-inserted lines-inserted))))
4114 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4115 ;; newline; it leaves the cursor at the beginning when the text contains
4116 ;; a newline
4117 (if (viper-same-line (point) (mark))
4118 (or (= (point) (mark)) (viper-backward-char-carefully))
4119 (exchange-point-and-mark)
4120 (if (bolp)
4121 (back-to-indentation)))
4122 (viper-deactivate-mark))
4124 (defun viper-Put-back (arg)
4125 "Put back at point/above line."
4126 (interactive "P")
4127 (let ((val (viper-p-val arg))
4128 (text (if viper-use-register
4129 (cond ((viper-valid-register viper-use-register '(digit))
4130 (current-kill
4131 (- viper-use-register ?1) 'do-not-rotate))
4132 ((viper-valid-register viper-use-register)
4133 (get-register (downcase viper-use-register)))
4134 (t (error viper-InvalidRegister viper-use-register)))
4135 (current-kill 0)))
4136 sv-point chars-inserted lines-inserted)
4137 (if (null text)
4138 (if viper-use-register
4139 (let ((reg viper-use-register))
4140 (setq viper-use-register nil)
4141 (error viper-EmptyRegister reg))
4142 (error "")))
4143 (setq viper-use-register nil)
4144 (if (viper-end-with-a-newline-p text) (beginning-of-line))
4145 (viper-set-destructive-command
4146 (list 'viper-Put-back val nil viper-use-register nil nil))
4147 (set-marker (viper-mark-marker) (point) (current-buffer))
4148 (setq sv-point (point))
4149 (viper-loop val (viper-yank text))
4150 (setq chars-inserted (abs (- (point) sv-point))
4151 lines-inserted (abs (count-lines (point) sv-point)))
4152 (if (or (> chars-inserted viper-change-notification-threshold)
4153 (> lines-inserted viper-change-notification-threshold))
4154 (unless (viper-is-in-minibuffer)
4155 (message "Inserted %d character(s), %d line(s)"
4156 chars-inserted lines-inserted))))
4157 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4158 ;; newline; it leaves the cursor at the beginning when the text contains
4159 ;; a newline
4160 (if (viper-same-line (point) (mark))
4161 (or (= (point) (mark)) (viper-backward-char-carefully))
4162 (exchange-point-and-mark)
4163 (if (bolp)
4164 (back-to-indentation)))
4165 (viper-deactivate-mark))
4168 ;; Copy region to kill-ring.
4169 ;; If BEG and END do not belong to the same buffer, copy empty region.
4170 (defun viper-copy-region-as-kill (beg end)
4171 (condition-case nil
4172 (copy-region-as-kill beg end)
4173 (error (copy-region-as-kill beg beg))))
4176 (defun viper-delete-char (arg)
4177 "Delete next character."
4178 (interactive "P")
4179 (let ((val (viper-p-val arg))
4180 end-del-pos)
4181 (viper-set-destructive-command
4182 (list 'viper-delete-char val nil nil nil nil))
4183 (if (and viper-ex-style-editing
4184 (> val (viper-chars-in-region (point) (viper-line-pos 'end))))
4185 (setq val (viper-chars-in-region (point) (viper-line-pos 'end))))
4186 (if (and viper-ex-style-motion (eolp))
4187 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
4188 (save-excursion
4189 (viper-forward-char-carefully val)
4190 (setq end-del-pos (point)))
4191 (if viper-use-register
4192 (progn
4193 (cond ((viper-valid-register viper-use-register '((Letter)))
4194 (viper-append-to-register
4195 (downcase viper-use-register) (point) end-del-pos))
4196 ((viper-valid-register viper-use-register)
4197 (copy-to-register
4198 viper-use-register (point) end-del-pos nil))
4199 (t (error viper-InvalidRegister viper-use-register)))
4200 (setq viper-use-register nil)))
4202 (delete-char val t)
4203 (if viper-ex-style-motion
4204 (if (and (eolp) (not (bolp))) (backward-char 1)))
4207 (defun viper-delete-backward-char (arg)
4208 "Delete previous character. On reaching beginning of line, stop and beep."
4209 (interactive "P")
4210 (let ((val (viper-p-val arg))
4211 end-del-pos)
4212 (viper-set-destructive-command
4213 (list 'viper-delete-backward-char val nil nil nil nil))
4214 (if (and
4215 viper-ex-style-editing
4216 (> val (viper-chars-in-region (viper-line-pos 'start) (point))))
4217 (setq val (viper-chars-in-region (viper-line-pos 'start) (point))))
4218 (save-excursion
4219 (viper-backward-char-carefully val)
4220 (setq end-del-pos (point)))
4221 (if viper-use-register
4222 (progn
4223 (cond ((viper-valid-register viper-use-register '(Letter))
4224 (viper-append-to-register
4225 (downcase viper-use-register) end-del-pos (point)))
4226 ((viper-valid-register viper-use-register)
4227 (copy-to-register
4228 viper-use-register end-del-pos (point) nil))
4229 (t (error viper-InvalidRegister viper-use-register)))
4230 (setq viper-use-register nil)))
4231 (if (and (bolp) viper-ex-style-editing)
4232 (ding))
4233 (delete-backward-char val t)))
4236 (defun viper-del-backward-char-in-insert ()
4237 "Delete 1 char backwards while in insert mode."
4238 (interactive)
4239 (if (and viper-ex-style-editing (bolp))
4240 (beep 1)
4241 ;; don't put on kill ring
4242 (delete-backward-char 1 nil)))
4245 (defun viper-del-backward-char-in-replace ()
4246 "Delete one character in replace mode.
4247 If `viper-delete-backwards-in-replace' is t, then DEL key actually deletes
4248 charecters. If it is nil, then the cursor just moves backwards, similarly
4249 to Vi. The variable `viper-ex-style-editing', if t, doesn't let the
4250 cursor move past the beginning of line."
4251 (interactive)
4252 (cond (viper-delete-backwards-in-replace
4253 (cond ((not (bolp))
4254 ;; don't put on kill ring
4255 (delete-backward-char 1 nil))
4256 (viper-ex-style-editing
4257 (beep 1))
4258 ((bobp)
4259 (beep 1))
4261 ;; don't put on kill ring
4262 (delete-backward-char 1 nil))))
4263 (viper-ex-style-editing
4264 (if (bolp)
4265 (beep 1)
4266 (backward-char 1)))
4268 (backward-char 1))))
4272 ;; join lines.
4274 (defun viper-join-lines (arg)
4275 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4276 (interactive "*P")
4277 (let ((val (viper-P-val arg)))
4278 (viper-set-destructive-command
4279 (list 'viper-join-lines val nil nil nil nil))
4280 (viper-loop (if (null val) 1 (1- val))
4281 (end-of-line)
4282 (if (not (eobp))
4283 (progn
4284 (forward-line 1)
4285 (delete-region (point) (1- (point)))
4286 (fixup-whitespace)
4287 ;; fixup-whitespace sometimes does not leave space
4288 ;; between objects, so we insert it as in Vi
4289 (or (looking-at " ")
4290 (insert " ")
4291 (backward-char 1))
4292 )))))
4295 ;; Replace state
4297 (defun viper-change (beg end)
4298 (if (markerp beg) (setq beg (marker-position beg)))
4299 (if (markerp end) (setq end (marker-position end)))
4300 ;; beg is sometimes (mark t), which may be nil
4301 (or beg (setq beg end))
4303 (viper-set-complex-command-for-undo)
4304 (if viper-use-register
4305 (progn
4306 (copy-to-register viper-use-register beg end nil)
4307 (setq viper-use-register nil)))
4308 (viper-set-replace-overlay beg end)
4309 (setq last-command nil) ; separate repl text from prev kills
4311 (if (= (viper-replace-start) (point-max))
4312 (error "End of buffer"))
4314 (setq viper-last-replace-region
4315 (buffer-substring (viper-replace-start)
4316 (viper-replace-end)))
4318 ;; protect against error while inserting "@" and other disasters
4319 ;; (e.g., read-only buff)
4320 (condition-case conds
4321 (if (or viper-allow-multiline-replace-regions
4322 (viper-same-line (viper-replace-start)
4323 (viper-replace-end)))
4324 (progn
4325 ;; tabs cause problems in replace, so untabify
4326 (goto-char (viper-replace-end))
4327 (insert-before-markers "@") ; put placeholder after the TAB
4328 (untabify (viper-replace-start) (point))
4329 ;; del @, don't put on kill ring
4330 (delete-backward-char 1)
4332 (viper-set-replace-overlay-glyphs
4333 viper-replace-region-start-delimiter
4334 viper-replace-region-end-delimiter)
4335 ;; this move takes care of the last posn in the overlay, which
4336 ;; has to be shifted because of insert. We can't simply insert
4337 ;; "$" before-markers because then overlay-start will shift the
4338 ;; beginning of the overlay in case we are replacing a single
4339 ;; character. This fixes the bug with `s' and `cl' commands.
4340 (viper-move-replace-overlay (viper-replace-start) (point))
4341 (goto-char (viper-replace-start))
4342 (viper-change-state-to-replace t))
4343 (kill-region (viper-replace-start)
4344 (viper-replace-end))
4345 (viper-hide-replace-overlay)
4346 (viper-change-state-to-insert))
4347 (error ;; make sure that the overlay doesn't stay.
4348 ;; go back to the original point
4349 (goto-char (viper-replace-start))
4350 (viper-hide-replace-overlay)
4351 (viper-message-conditions conds))))
4354 (defun viper-change-subr (beg end)
4355 ;; beg is sometimes (mark t), which may be nil
4356 (or beg (setq beg end))
4357 (if viper-use-register
4358 (progn
4359 (copy-to-register viper-use-register beg end nil)
4360 (setq viper-use-register nil)))
4361 (kill-region beg end)
4362 (setq this-command 'viper-change)
4363 (viper-yank-last-insertion))
4365 (defun viper-toggle-case (arg)
4366 "Toggle character case."
4367 (interactive "P")
4368 (let ((val (viper-p-val arg)) (c))
4369 (viper-set-destructive-command
4370 (list 'viper-toggle-case val nil nil nil nil))
4371 (while (> val 0)
4372 (setq c (following-char))
4373 (delete-char 1 nil)
4374 (if (eq c (upcase c))
4375 (insert-char (downcase c) 1)
4376 (insert-char (upcase c) 1))
4377 (if (eolp) (backward-char 1))
4378 (setq val (1- val)))))
4381 ;; query replace
4383 (defun viper-query-replace ()
4384 "Query replace.
4385 If a null string is suplied as the string to be replaced,
4386 the query replace mode will toggle between string replace
4387 and regexp replace."
4388 (interactive)
4389 (let (str)
4390 (setq str (viper-read-string-with-history
4391 (if viper-re-query-replace "Query replace regexp: "
4392 "Query replace: ")
4393 nil ; no initial
4394 'viper-replace1-history
4395 (car viper-replace1-history) ; default
4397 (if (string= str "")
4398 (progn
4399 (setq viper-re-query-replace (not viper-re-query-replace))
4400 (message "Query replace mode changed to %s"
4401 (if viper-re-query-replace "regexp replace"
4402 "string replace")))
4403 (if viper-re-query-replace
4404 (query-replace-regexp
4406 (viper-read-string-with-history
4407 (format "Query replace regexp `%s' with: " str)
4408 nil ; no initial
4409 'viper-replace1-history
4410 (car viper-replace1-history) ; default
4412 (query-replace
4414 (viper-read-string-with-history
4415 (format "Query replace `%s' with: " str)
4416 nil ; no initial
4417 'viper-replace1-history
4418 (car viper-replace1-history) ; default
4419 ))))))
4422 ;; marking
4424 (defun viper-mark-beginning-of-buffer ()
4425 "Mark beginning of buffer."
4426 (interactive)
4427 (push-mark (point))
4428 (goto-char (point-min))
4429 (exchange-point-and-mark)
4430 (message "Mark set at the beginning of buffer"))
4432 (defun viper-mark-end-of-buffer ()
4433 "Mark end of buffer."
4434 (interactive)
4435 (push-mark (point))
4436 (goto-char (point-max))
4437 (exchange-point-and-mark)
4438 (message "Mark set at the end of buffer"))
4440 (defun viper-mark-point ()
4441 "Set mark at point of buffer."
4442 (interactive)
4443 (let ((char (read-char)))
4444 (cond ((and (<= ?a char) (<= char ?z))
4445 (point-to-register (viper-int-to-char (1+ (- char ?a)))))
4446 ((viper= char ?<) (viper-mark-beginning-of-buffer))
4447 ((viper= char ?>) (viper-mark-end-of-buffer))
4448 ((viper= char ?.) (viper-set-mark-if-necessary))
4449 ((viper= char ?,) (viper-cycle-through-mark-ring))
4450 ((viper= char ?^) (push-mark viper-saved-mark t t))
4451 ((viper= char ?D) (mark-defun))
4452 (t (error ""))
4455 ;; Algorithm: If first invocation of this command save mark on ring, goto
4456 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4457 ;; making it into the new mark, M1.
4458 ;; Push this mark back and set mark to the original point position, p1.
4459 ;; So, if you hit '' or `` then you can return to p1.
4461 ;; If repeated command, pop top elt from the ring into mark and
4462 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4463 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4464 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4465 ;; etc.
4466 (defun viper-cycle-through-mark-ring ()
4467 "Visit previous locations on the mark ring.
4468 One can use `` and '' to temporarily jump 1 step back."
4469 (let* ((sv-pt (point)))
4470 ;; if repeated `m,' command, pop the previously saved mark.
4471 ;; Prev saved mark is actually prev saved point. It is used if the
4472 ;; user types `` or '' and is discarded
4473 ;; from the mark ring by the next `m,' command.
4474 ;; In any case, go to the previous or previously saved mark.
4475 ;; Then push the current mark (popped off the ring) and set current
4476 ;; point to be the mark. Current pt as mark is discarded by the next
4477 ;; m, command.
4478 (if (eq last-command 'viper-cycle-through-mark-ring)
4480 ;; save current mark if the first iteration
4481 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4482 (if (mark t)
4483 (push-mark (mark t) t)) )
4484 (pop-mark)
4485 (set-mark-command 1)
4486 ;; don't duplicate mark on the ring
4487 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4488 (push-mark sv-pt t)
4489 (viper-deactivate-mark)
4490 (setq this-command 'viper-cycle-through-mark-ring)
4494 (defun viper-goto-mark (arg)
4495 "Go to mark."
4496 (interactive "P")
4497 (let ((char (read-char))
4498 (com (viper-getcom arg)))
4499 (viper-goto-mark-subr char com nil)))
4501 (defun viper-goto-mark-and-skip-white (arg)
4502 "Go to mark and skip to first non-white character on line."
4503 (interactive "P")
4504 (let ((char (read-char))
4505 (com (viper-getCom arg)))
4506 (viper-goto-mark-subr char com t)))
4508 (defun viper-goto-mark-subr (char com skip-white)
4509 (if (eobp)
4510 (if (bobp)
4511 (error "Empty buffer")
4512 (backward-char 1)))
4513 (cond ((viper-valid-register char '(letter))
4514 (let* ((buff (current-buffer))
4515 (reg (viper-int-to-char (1+ (- char ?a))))
4516 (text-marker (get-register reg)))
4517 ;; If marker points to file that had markers set (and those markers
4518 ;; were saved (as e.g., in session.el), then restore those markers
4519 (if (and (consp text-marker)
4520 (eq (car text-marker) 'file-query)
4521 (or (find-buffer-visiting (nth 1 text-marker))
4522 (y-or-n-p (format "Visit file %s again? "
4523 (nth 1 text-marker)))))
4524 (save-excursion
4525 (find-file (nth 1 text-marker))
4526 (when (and (<= (nth 2 text-marker) (point-max))
4527 (<= (point-min) (nth 2 text-marker)))
4528 (setq text-marker (copy-marker (nth 2 text-marker)))
4529 (set-register reg text-marker))))
4530 (if com (viper-move-marker-locally 'viper-com-point (point)))
4531 (if (not (viper-valid-marker text-marker))
4532 (error viper-EmptyTextmarker char))
4533 (if (and (viper-same-line (point) viper-last-jump)
4534 (= (point) viper-last-jump-ignore))
4535 (push-mark viper-last-jump t)
4536 (push-mark nil t)) ; no msg
4537 (viper-register-to-point reg)
4538 (setq viper-last-jump (point-marker))
4539 (cond (skip-white
4540 (back-to-indentation)
4541 (setq viper-last-jump-ignore (point))))
4542 (if com
4543 (if (equal buff (current-buffer))
4544 (viper-execute-com (if skip-white
4545 'viper-goto-mark-and-skip-white
4546 'viper-goto-mark)
4547 nil com)
4548 (switch-to-buffer buff)
4549 (goto-char viper-com-point)
4550 (viper-change-state-to-vi)
4551 (error "")))))
4552 ((and (not skip-white) (viper= char ?`))
4553 (if com (viper-move-marker-locally 'viper-com-point (point)))
4554 (if (and (viper-same-line (point) viper-last-jump)
4555 (= (point) viper-last-jump-ignore))
4556 (goto-char viper-last-jump))
4557 (if (null (mark t)) (error "Mark is not set in this buffer"))
4558 (if (= (point) (mark t)) (pop-mark))
4559 (exchange-point-and-mark)
4560 (setq viper-last-jump (point-marker)
4561 viper-last-jump-ignore 0)
4562 (if com (viper-execute-com 'viper-goto-mark nil com)))
4563 ((and skip-white (viper= char ?'))
4564 (if com (viper-move-marker-locally 'viper-com-point (point)))
4565 (if (and (viper-same-line (point) viper-last-jump)
4566 (= (point) viper-last-jump-ignore))
4567 (goto-char viper-last-jump))
4568 (if (= (point) (mark t)) (pop-mark))
4569 (exchange-point-and-mark)
4570 (setq viper-last-jump (point))
4571 (back-to-indentation)
4572 (setq viper-last-jump-ignore (point))
4573 (if com (viper-execute-com 'viper-goto-mark-and-skip-white nil com)))
4574 (t (error viper-InvalidTextmarker char))))
4576 (defun viper-insert-tab ()
4577 (interactive)
4578 (insert-tab))
4580 (defun viper-exchange-point-and-mark ()
4581 (interactive)
4582 (exchange-point-and-mark)
4583 (back-to-indentation))
4585 ;; Input Mode Indentation
4587 ;; Returns t, if the string before point matches the regexp STR.
4588 (defsubst viper-looking-back (str)
4589 (and (save-excursion (re-search-backward str nil t))
4590 (= (point) (match-end 0))))
4593 (defun viper-forward-indent ()
4594 "Indent forward -- `C-t' in Vi."
4595 (interactive)
4596 (setq viper-cted t)
4597 (indent-to (+ (current-column) viper-shift-width)))
4599 (defun viper-backward-indent ()
4600 "Backtab, C-d in VI"
4601 (interactive)
4602 (if viper-cted
4603 (let ((p (point)) (c (current-column)) bol (indent t))
4604 (if (viper-looking-back "[0^]")
4605 (progn
4606 (if (eq ?^ (preceding-char))
4607 (setq viper-preserve-indent t))
4608 (delete-backward-char 1)
4609 (setq p (point))
4610 (setq indent nil)))
4611 (save-excursion
4612 (beginning-of-line)
4613 (setq bol (point)))
4614 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4615 (delete-region (point) p)
4616 (if indent
4617 (indent-to (- c viper-shift-width)))
4618 (if (or (bolp) (viper-looking-back "[^ \t]"))
4619 (setq viper-cted nil)))))
4621 ;; do smart indent
4622 (defun viper-indent-line (col)
4623 (if viper-auto-indent
4624 (progn
4625 (setq viper-cted t)
4626 (if (and viper-electric-mode
4627 (not (memq major-mode '(fundamental-mode
4628 text-mode
4629 paragraph-indent-text-mode))))
4630 (indent-according-to-mode)
4631 (indent-to col)))))
4634 (defun viper-autoindent ()
4635 "Auto Indentation, Vi-style."
4636 (interactive)
4637 (let ((col (current-indentation)))
4638 (if abbrev-mode (expand-abbrev))
4639 (if viper-preserve-indent
4640 (setq viper-preserve-indent nil)
4641 (setq viper-current-indent col))
4642 ;; don't leave whitespace lines around
4643 (if (memq last-command
4644 '(viper-autoindent
4645 viper-open-line viper-Open-line
4646 viper-replace-state-exit-cmd))
4647 (indent-to-left-margin))
4648 ;; use \n instead of newline, or else <Return> will move the insert point
4649 ;;(newline 1)
4650 (insert "\n")
4651 (viper-indent-line viper-current-indent)
4655 ;; Viewing registers
4657 (defun viper-ket-function (arg)
4658 "Function called by \], the ket. View registers and call \]\]."
4659 (interactive "P")
4660 (let ((reg (read-char)))
4661 (cond ((viper-valid-register reg '(letter Letter))
4662 (view-register (downcase reg)))
4663 ((viper-valid-register reg '(digit))
4664 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4665 (with-output-to-temp-buffer " *viper-info*"
4666 (princ (format "Register %c contains the string:\n" reg))
4667 (princ text))
4669 ((viper= ?\] reg)
4670 (viper-next-heading arg))
4671 (t (error
4672 viper-InvalidRegister reg)))))
4674 (defun viper-brac-function (arg)
4675 "Function called by \[, the brac. View textmarkers and call \[\["
4676 (interactive "P")
4677 (let ((reg (read-char)))
4678 (cond ((viper= ?\[ reg)
4679 (viper-prev-heading arg))
4680 ((viper= ?\] reg)
4681 (viper-heading-end arg))
4682 ((viper-valid-register reg '(letter))
4683 (let* ((val (get-register (viper-int-to-char (1+ (- reg ?a)))))
4684 (buf (if (not (markerp val))
4685 (error viper-EmptyTextmarker reg)
4686 (marker-buffer val)))
4687 (pos (marker-position val))
4688 line-no text (s pos) (e pos))
4689 (with-output-to-temp-buffer " *viper-info*"
4690 (if (and buf pos)
4691 (progn
4692 (save-excursion
4693 (set-buffer buf)
4694 (setq line-no (1+ (count-lines (point-min) val)))
4695 (goto-char pos)
4696 (beginning-of-line)
4697 (if (re-search-backward "[^ \t]" nil t)
4698 (progn
4699 (beginning-of-line)
4700 (setq s (point))))
4701 (goto-char pos)
4702 (forward-line 1)
4703 (if (re-search-forward "[^ \t]" nil t)
4704 (progn
4705 (end-of-line)
4706 (setq e (point))))
4707 (setq text (buffer-substring s e))
4708 (setq text (format "%s<%c>%s"
4709 (substring text 0 (- pos s))
4710 reg (substring text (- pos s)))))
4711 (princ
4712 (format
4713 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4714 reg (buffer-name buf) line-no))
4715 (princ (format "Here is some text around %c:\n\n %s"
4716 reg text)))
4717 (princ (format viper-EmptyTextmarker reg))))
4719 (t (error viper-InvalidTextmarker reg)))))
4723 (defun viper-delete-backward-word (arg)
4724 "Delete previous word."
4725 (interactive "p")
4726 (save-excursion
4727 (push-mark nil t)
4728 (backward-word arg)
4729 (delete-region (point) (mark t))
4730 (pop-mark)))
4734 ;; Get viper standard value of SYMBOL. If symbol is customized, get its
4735 ;; standard value. Otherwise, get the value saved in the alist STORAGE. If
4736 ;; STORAGE is nil, use viper-saved-user-settings.
4737 (defun viper-standard-value (symbol &optional storage)
4738 (or (eval (car (get symbol 'customized-value)))
4739 (eval (car (get symbol 'saved-value)))
4740 (nth 1 (assoc symbol (or storage viper-saved-user-settings)))))
4744 (defun viper-set-expert-level (&optional dont-change-unless)
4745 "Sets the expert level for a Viper user.
4746 Can be called interactively to change (temporarily or permanently) the
4747 current expert level.
4749 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4750 the level should not be changed, unless its current value is
4751 meaningless (i.e., not one of 1,2,3,4,5).
4753 User level determines the setting of Viper variables that are most
4754 sensitive for VI-style look-and-feel."
4756 (interactive)
4758 (if (not (natnump viper-expert-level)) (setq viper-expert-level 0))
4760 (save-window-excursion
4761 (delete-other-windows)
4762 ;; if 0 < viper-expert-level < viper-max-expert-level
4763 ;; & dont-change-unless = t -- use it; else ask
4764 (viper-ask-level dont-change-unless))
4766 (setq viper-always t
4767 viper-ex-style-motion t
4768 viper-ex-style-editing t
4769 viper-want-ctl-h-help nil)
4771 (cond ((eq viper-expert-level 1) ; novice or beginner
4772 (global-set-key ; in emacs-state
4773 viper-toggle-key
4774 (if (viper-window-display-p) 'viper-iconify 'suspend-emacs))
4775 (setq viper-no-multiple-ESC t
4776 viper-re-search t
4777 viper-vi-style-in-minibuffer t
4778 viper-search-wrap-around-t t
4779 viper-electric-mode nil
4780 viper-want-emacs-keys-in-vi nil
4781 viper-want-emacs-keys-in-insert nil))
4783 ((and (> viper-expert-level 1) (< viper-expert-level 5))
4784 ;; intermediate to guru
4785 (setq viper-no-multiple-ESC (if (viper-window-display-p)
4786 t 'twice)
4787 viper-electric-mode t
4788 viper-want-emacs-keys-in-vi t
4789 viper-want-emacs-keys-in-insert (> viper-expert-level 2))
4791 (if (eq viper-expert-level 4) ; respect user's ex-style motion
4792 ; and viper-no-multiple-ESC
4793 (progn
4794 (setq-default
4795 viper-ex-style-editing
4796 (viper-standard-value 'viper-ex-style-editing)
4797 viper-ex-style-motion
4798 (viper-standard-value 'viper-ex-style-motion))
4799 (setq viper-ex-style-motion
4800 (viper-standard-value 'viper-ex-style-motion)
4801 viper-ex-style-editing
4802 (viper-standard-value 'viper-ex-style-editing)
4803 viper-re-search
4804 (viper-standard-value 'viper-re-search)
4805 viper-no-multiple-ESC
4806 (viper-standard-value 'viper-no-multiple-ESC)))))
4808 ;; A wizard!!
4809 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4810 ;; user toggle the values of variables.
4811 (t (setq-default viper-ex-style-editing
4812 (viper-standard-value 'viper-ex-style-editing)
4813 viper-ex-style-motion
4814 (viper-standard-value 'viper-ex-style-motion))
4815 (setq viper-want-ctl-h-help
4816 (viper-standard-value 'viper-want-ctl-h-help)
4817 viper-always
4818 (viper-standard-value 'viper-always)
4819 viper-no-multiple-ESC
4820 (viper-standard-value 'viper-no-multiple-ESC)
4821 viper-ex-style-motion
4822 (viper-standard-value 'viper-ex-style-motion)
4823 viper-ex-style-editing
4824 (viper-standard-value 'viper-ex-style-editing)
4825 viper-re-search
4826 (viper-standard-value 'viper-re-search)
4827 viper-electric-mode
4828 (viper-standard-value 'viper-electric-mode)
4829 viper-want-emacs-keys-in-vi
4830 (viper-standard-value 'viper-want-emacs-keys-in-vi)
4831 viper-want-emacs-keys-in-insert
4832 (viper-standard-value 'viper-want-emacs-keys-in-insert))))
4834 (viper-set-mode-vars-for viper-current-state)
4835 (if (or viper-always
4836 (and (> viper-expert-level 0) (> 5 viper-expert-level)))
4837 (viper-set-hooks)))
4840 ;; Ask user expert level.
4841 (defun viper-ask-level (dont-change-unless)
4842 (let ((ask-buffer " *viper-ask-level*")
4843 level-changed repeated)
4844 (save-window-excursion
4845 (switch-to-buffer ask-buffer)
4847 (while (or (> viper-expert-level viper-max-expert-level)
4848 (< viper-expert-level 1)
4849 (null dont-change-unless))
4850 (erase-buffer)
4851 (if repeated
4852 (progn
4853 (message "Invalid user level")
4854 (beep 1))
4855 (setq repeated t))
4856 (setq dont-change-unless t
4857 level-changed t)
4858 (insert "
4859 Please specify your level of familiarity with the venomous VI PERil
4860 \(and the VI Plan for Emacs Rescue).
4861 You can change it at any time by typing `M-x viper-set-expert-level RET'
4863 1 -- BEGINNER: Almost all Emacs features are suppressed.
4864 Feels almost like straight Vi. File name completion and
4865 command history in the minibuffer are thrown in as a bonus.
4866 To use Emacs productively, you must reach level 3 or higher.
4867 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4868 so most Emacs commands can be used when Viper is in Vi state.
4869 Good progress---you are well on the way to level 3!
4870 3 -- GRAND MASTER: Like 2, but most Emacs commands are available also
4871 in Viper's insert state.
4872 4 -- GURU: Like 3, but user settings are respected for viper-no-multiple-ESC,
4873 viper-ex-style-motion, viper-ex-style-editing, and
4874 viper-re-search variables. Adjust these settings to your taste.
4875 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4876 viper-electric-mode, viper-want-ctl-h-help, viper-want-emacs-keys-in-vi,
4877 and viper-want-emacs-keys-in-insert. Adjust these to your taste.
4879 Please, specify your level now: ")
4881 (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
4882 ) ; end while
4884 ;; tell the user if level was changed
4885 (and level-changed
4886 (progn
4887 (insert
4888 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4889 viper-expert-level))
4890 (if (y-or-n-p "Do you wish to make this change permanent? ")
4891 ;; save the setting for viper-expert-level
4892 (viper-save-setting
4893 'viper-expert-level
4894 (format "Saving user level %d ..." viper-expert-level)
4895 viper-custom-file-name))
4897 (bury-buffer) ; remove ask-buffer from screen
4898 (message "")
4902 (defun viper-nil ()
4903 (interactive)
4904 (beep 1))
4907 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4908 (defun viper-register-to-point (char &optional enforce-buffer)
4909 "Like jump-to-register, but switches to another buffer in another window."
4910 (interactive "cViper register to point: ")
4911 (let ((val (get-register char)))
4912 (cond
4913 ((and (fboundp 'frame-configuration-p)
4914 (frame-configuration-p val))
4915 (set-frame-configuration val))
4916 ((window-configuration-p val)
4917 (set-window-configuration val))
4918 ((viper-valid-marker val)
4919 (if (and enforce-buffer
4920 (not (equal (current-buffer) (marker-buffer val))))
4921 (error (concat viper-EmptyTextmarker " in this buffer")
4922 (viper-int-to-char (1- (+ char ?a)))))
4923 (pop-to-buffer (marker-buffer val))
4924 (goto-char val))
4925 ((and (consp val) (eq (car val) 'file))
4926 (find-file (cdr val)))
4928 (error viper-EmptyTextmarker (viper-int-to-char (1- (+ char ?a))))))))
4931 (defun viper-save-kill-buffer ()
4932 "Save then kill current buffer."
4933 (interactive)
4934 (if (< viper-expert-level 2)
4935 (save-buffers-kill-emacs)
4936 (save-buffer)
4937 (kill-buffer (current-buffer))))
4941 ;;; Bug Report
4943 (defun viper-submit-report ()
4944 "Submit bug report on Viper."
4945 (interactive)
4946 (let ((reporter-prompt-for-summary-p t)
4947 (viper-device-type (viper-device-type))
4948 color-display-p frame-parameters
4949 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4950 varlist salutation window-config)
4952 ;; If mode info is needed, add variable to `let' and then set it below,
4953 ;; like we did with color-display-p.
4954 (setq color-display-p (if (viper-window-display-p)
4955 (viper-color-display-p)
4956 'non-x)
4957 minibuffer-vi-face (if (viper-has-face-support-p)
4958 (viper-get-face viper-minibuffer-vi-face)
4959 'non-x)
4960 minibuffer-insert-face (if (viper-has-face-support-p)
4961 (viper-get-face
4962 viper-minibuffer-insert-face)
4963 'non-x)
4964 minibuffer-emacs-face (if (viper-has-face-support-p)
4965 (viper-get-face
4966 viper-minibuffer-emacs-face)
4967 'non-x)
4968 frame-parameters (if (fboundp 'frame-parameters)
4969 (frame-parameters (selected-frame))))
4971 (setq varlist (list 'viper-vi-minibuffer-minor-mode
4972 'viper-insert-minibuffer-minor-mode
4973 'viper-vi-intercept-minor-mode
4974 'viper-vi-local-user-minor-mode
4975 'viper-vi-kbd-minor-mode
4976 'viper-vi-global-user-minor-mode
4977 'viper-vi-state-modifier-minor-mode
4978 'viper-vi-diehard-minor-mode
4979 'viper-vi-basic-minor-mode
4980 'viper-replace-minor-mode
4981 'viper-insert-intercept-minor-mode
4982 'viper-insert-local-user-minor-mode
4983 'viper-insert-kbd-minor-mode
4984 'viper-insert-global-user-minor-mode
4985 'viper-insert-state-modifier-minor-mode
4986 'viper-insert-diehard-minor-mode
4987 'viper-insert-basic-minor-mode
4988 'viper-emacs-intercept-minor-mode
4989 'viper-emacs-local-user-minor-mode
4990 'viper-emacs-kbd-minor-mode
4991 'viper-emacs-global-user-minor-mode
4992 'viper-emacs-state-modifier-minor-mode
4993 'viper-automatic-iso-accents
4994 'viper-special-input-method
4995 'viper-want-emacs-keys-in-insert
4996 'viper-want-emacs-keys-in-vi
4997 'viper-keep-point-on-undo
4998 'viper-no-multiple-ESC
4999 'viper-electric-mode
5000 'viper-ESC-key
5001 'viper-want-ctl-h-help
5002 'viper-ex-style-editing
5003 'viper-delete-backwards-in-replace
5004 'viper-vi-style-in-minibuffer
5005 'viper-vi-state-hook
5006 'viper-insert-state-hook
5007 'viper-replace-state-hook
5008 'viper-emacs-state-hook
5009 'ex-cycle-other-window
5010 'ex-cycle-through-non-files
5011 'viper-expert-level
5012 'major-mode
5013 'viper-device-type
5014 'color-display-p
5015 'frame-parameters
5016 'minibuffer-vi-face
5017 'minibuffer-insert-face
5018 'minibuffer-emacs-face
5020 (setq salutation "
5021 Congratulations! You may have unearthed a bug in Viper!
5022 Please mail a concise, accurate summary of the problem to the address above.
5024 -------------------------------------------------------------------")
5025 (setq window-config (current-window-configuration))
5026 (with-output-to-temp-buffer " *viper-info*"
5027 (switch-to-buffer " *viper-info*")
5028 (delete-other-windows)
5029 (princ "
5030 PLEASE FOLLOW THESE PROCEDURES
5031 ------------------------------
5033 Before reporting a bug, please verify that it is related to Viper, and is
5034 not cause by other packages you are using.
5036 Don't report compilation warnings, unless you are certain that there is a
5037 problem. These warnings are normal and unavoidable.
5039 Please note that users should not modify variables and keymaps other than
5040 those advertised in the manual. Such `customization' is likely to crash
5041 Viper, as it would any other improperly customized Emacs package.
5043 If you are reporting an error message received while executing one of the
5044 Viper commands, type:
5046 M-x set-variable <Return> debug-on-error <Return> t <Return>
5048 Then reproduce the error. The above command will cause Emacs to produce a
5049 back trace of the execution that leads to the error. Please include this
5050 trace in your bug report.
5052 If you believe that one of Viper's commands goes into an infinite loop
5053 \(e.g., Emacs freezes\), type:
5055 M-x set-variable <Return> debug-on-quit <Return> t <Return>
5057 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
5058 the current command. Include the resulting back trace in the bug report.
5060 Mail anyway (y or n)? ")
5061 (if (y-or-n-p "Mail anyway? ")
5063 (set-window-configuration window-config)
5064 (error "Bug report aborted")))
5066 (require 'reporter)
5067 (set-window-configuration window-config)
5069 (reporter-submit-bug-report "kifer@cs.stonybrook.edu"
5070 (viper-version)
5071 varlist
5072 nil 'delete-other-windows
5073 salutation)
5079 ;; arch-tag: 739a6450-5fda-44d0-88b0-325053d888c2
5080 ;;; viper-cmd.el ends here