Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / emulation / viper-cmd.el
blob07719ba45be503b802c0dfe28065c47f0cdc2b8b
1 ;;; viper-cmd.el --- Vi command support for Viper
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7 ;; Package: viper
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (provide 'viper-cmd)
30 ;; Compiler pacifier
31 (defvar viper-minibuffer-current-face)
32 (defvar viper-minibuffer-insert-face)
33 (defvar viper-minibuffer-vi-face)
34 (defvar viper-minibuffer-emacs-face)
35 (defvar viper-always)
36 (defvar viper-mode-string)
37 (defvar viper-custom-file-name)
38 (defvar viper--key-maps)
39 (defvar viper--intercept-key-maps)
40 (defvar iso-accents-mode)
41 (defvar quail-mode)
42 (defvar quail-current-str)
43 (defvar mark-even-if-inactive)
44 (defvar init-message)
45 (defvar initial)
46 (defvar undo-beg-posn)
47 (defvar undo-end-posn)
49 (eval-and-compile
50 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
51 ;; end pacifier
54 (require 'viper-util)
55 (require 'viper-keym)
56 (require 'viper-mous)
57 (require 'viper-macs)
58 (require 'viper-ex)
62 ;; Generic predicates
64 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
66 ;; generate test functions
67 ;; given symbol foo, foo-p is the test function, foos is the set of
68 ;; Viper command keys
69 ;; (macroexpand '(viper-test-com-defun foo))
70 ;; (defun foo-p (com) (consp (memq com foos)))
72 (defmacro viper-test-com-defun (name)
73 (let* ((snm (symbol-name name))
74 (nm-p (intern (concat snm "-p")))
75 (nms (intern (concat snm "s"))))
76 `(defun ,nm-p (com)
77 (consp (viper-memq-char com ,nms)
78 ))))
80 ;; Variables for defining VI commands
82 ;; Modifying commands that can be prefixes to movement commands
83 (defvar viper-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
84 ;; define viper-prefix-command-p
85 (viper-test-com-defun viper-prefix-command)
87 ;; Commands that are pairs eg. dd. r and R here are a hack
88 (defconst viper-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
89 ;; define viper-charpair-command-p
90 (viper-test-com-defun viper-charpair-command)
92 (defconst viper-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?j ?k ?l
93 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
94 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
95 ?\; ?, ?0 ?? ?/ ?\ ?\C-m
96 space return
97 delete backspace
99 "Movement commands")
100 ;; define viper-movement-command-p
101 (viper-test-com-defun viper-movement-command)
103 ;; Vi digit commands
104 (defconst viper-digit-commands '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
106 ;; define viper-digit-command-p
107 (viper-test-com-defun viper-digit-command)
109 ;; Commands that can be repeated by . (dotted)
110 (defconst viper-dotable-commands '(?c ?d ?C ?s ?S ?D ?> ?<))
111 ;; define viper-dotable-command-p
112 (viper-test-com-defun viper-dotable-command)
114 ;; Commands that can follow a #
115 (defconst viper-hash-commands '(?c ?C ?g ?q ?s))
116 ;; define viper-hash-command-p
117 (viper-test-com-defun viper-hash-command)
119 ;; Commands that may have registers as prefix
120 (defconst viper-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
121 ;; define viper-regsuffix-command-p
122 (viper-test-com-defun viper-regsuffix-command)
124 (defconst viper-vi-commands (append viper-movement-commands
125 viper-digit-commands
126 viper-dotable-commands
127 viper-charpair-commands
128 viper-hash-commands
129 viper-prefix-commands
130 viper-regsuffix-commands)
131 "The list of all commands in Vi-state.")
132 ;; define viper-vi-command-p
133 (viper-test-com-defun viper-vi-command)
135 ;; Where viper saves mark. This mark is resurrected by m^
136 (defvar viper-saved-mark nil)
138 ;; Contains user settings for vars affected by viper-set-expert-level function.
139 ;; Not a user option.
140 (defvar viper-saved-user-settings nil)
144 ;;; CODE
146 ;; sentinels
148 ;; Runs viper-after-change-functions inside after-change-functions
149 (defun viper-after-change-sentinel (beg end len)
150 (run-hook-with-args 'viper-after-change-functions beg end len))
152 ;; Runs viper-before-change-functions inside before-change-functions
153 (defun viper-before-change-sentinel (beg end)
154 (run-hook-with-args 'viper-before-change-functions beg end))
156 (defsubst viper-post-command-sentinel ()
157 (condition-case conds
158 (run-hooks 'viper-post-command-hooks)
159 (error (viper-message-conditions conds)))
160 (if (eq viper-current-state 'vi-state)
161 (viper-restore-cursor-color 'after-insert-mode)))
163 (defsubst viper-pre-command-sentinel ()
164 (run-hooks 'viper-pre-command-hooks))
166 ;; Needed so that Viper will be able to figure the last inserted
167 ;; chunk of text with reasonable accuracy.
168 (defsubst viper-insert-state-post-command-sentinel ()
169 (if (and (memq viper-current-state '(insert-state replace-state))
170 viper-insert-point
171 (>= (point) viper-insert-point))
172 (setq viper-last-posn-while-in-insert-state (point-marker)))
173 (or (viper-overlay-p viper-replace-overlay)
174 (progn
175 (viper-set-replace-overlay (point-min) (point-min))
176 (viper-hide-replace-overlay)))
177 (if (eq viper-current-state 'insert-state)
178 (let ((icolor (viper-frame-value viper-insert-state-cursor-color)))
179 (or (stringp (viper-get-saved-cursor-color-in-insert-mode))
180 (string= (viper-get-cursor-color) icolor)
181 ;; save current color, if not already saved
182 (viper-save-cursor-color 'before-insert-mode))
183 ;; set insert mode cursor color
184 (viper-change-cursor-color icolor)))
185 (let ((ecolor (viper-frame-value viper-emacs-state-cursor-color)))
186 (when (and ecolor (eq viper-current-state 'emacs-state))
187 (or (stringp (viper-get-saved-cursor-color-in-emacs-mode))
188 (string= (viper-get-cursor-color) ecolor)
189 ;; save current color, if not already saved
190 (viper-save-cursor-color 'before-emacs-mode))
191 ;; set emacs mode cursor color
192 (viper-change-cursor-color ecolor)))
194 (if (and (memq this-command '(dabbrev-expand hippie-expand))
195 (integerp viper-pre-command-point)
196 (markerp viper-insert-point)
197 (marker-position viper-insert-point)
198 (> viper-insert-point viper-pre-command-point))
199 (viper-move-marker-locally viper-insert-point viper-pre-command-point)))
201 (defsubst viper-preserve-cursor-color ()
202 (or (memq this-command '(self-insert-command
203 viper-del-backward-char-in-insert
204 viper-del-backward-char-in-replace
205 viper-delete-backward-char
206 viper-join-lines
207 viper-delete-char))
208 (memq (viper-event-key last-command-event)
209 '(up down left right (meta f) (meta b)
210 (control n) (control p) (control f) (control b)))))
212 (defsubst viper-insert-state-pre-command-sentinel ()
213 (or (viper-preserve-cursor-color)
214 (viper-restore-cursor-color 'after-insert-mode))
215 (if (and (memq this-command '(dabbrev-expand hippie-expand))
216 (markerp viper-insert-point)
217 (marker-position viper-insert-point))
218 (setq viper-pre-command-point (marker-position viper-insert-point))))
220 (defun viper-R-state-post-command-sentinel ()
221 ;; Restoring cursor color is needed despite
222 ;; viper-replace-state-pre-command-sentinel: When you jump to another buffer
223 ;; in another frame, the pre-command hook won't change cursor color to
224 ;; default in that other frame. So, if the second frame cursor was red and
225 ;; we set the point outside the replacement region, then the cursor color
226 ;; will remain red. Restoring the default, below, prevents this.
227 (if (and (<= (viper-replace-start) (point))
228 (<= (point) (viper-replace-end)))
229 (viper-change-cursor-color
230 (viper-frame-value viper-replace-overlay-cursor-color))
231 (viper-restore-cursor-color 'after-replace-mode)))
233 ;; to speed up, don't change cursor color before self-insert
234 ;; and common move commands
235 (defsubst viper-replace-state-pre-command-sentinel ()
236 (or (viper-preserve-cursor-color)
237 (viper-restore-cursor-color 'after-replace-mode)))
240 ;; Make sure we don't delete more than needed.
241 ;; This is executed at viper-last-posn-in-replace-region
242 (defsubst viper-trim-replace-chars-to-delete-if-necessary ()
243 (setq viper-replace-chars-to-delete
244 (max 0
245 (min viper-replace-chars-to-delete
246 ;; Don't delete more than to the end of repl overlay
247 (viper-chars-in-region
248 (viper-replace-end) viper-last-posn-in-replace-region)
249 ;; point is viper-last-posn-in-replace-region now
250 ;; So, this limits deletion to the end of line
251 (viper-chars-in-region (point) (viper-line-pos 'end))
252 ))))
255 (defun viper-replace-state-post-command-sentinel ()
256 ;; Restoring cursor color is needed despite
257 ;; viper-replace-state-pre-command-sentinel: When one jumps to another buffer
258 ;; in another frame, the pre-command hook won't change cursor color to
259 ;; default in that other frame. So, if the second frame cursor was red and
260 ;; we set the point outside the replacement region, then the cursor color
261 ;; will remain red. Restoring the default, below, fixes this problem.
263 ;; We optimize for some commands, like self-insert-command,
264 ;; viper-delete-backward-char, etc., since they either don't change
265 ;; cursor color or, if they terminate replace mode, the color will be changed
266 ;; in viper-finish-change
267 (or (viper-preserve-cursor-color)
268 (viper-restore-cursor-color 'after-replace-mode))
269 (cond
270 ((eq viper-current-state 'replace-state)
271 ;; delete characters to compensate for inserted chars.
272 (let ((replace-boundary (viper-replace-end)))
273 (save-excursion
274 (goto-char viper-last-posn-in-replace-region)
275 (viper-trim-replace-chars-to-delete-if-necessary)
276 (delete-char viper-replace-chars-to-delete)
277 (setq viper-replace-chars-to-delete 0)
278 ;; terminate replace mode if reached replace limit
279 (if (= viper-last-posn-in-replace-region (viper-replace-end))
280 (viper-finish-change)))
282 (when (viper-pos-within-region
283 (point) (viper-replace-start) replace-boundary)
284 ;; the state may have changed in viper-finish-change above
285 (if (eq viper-current-state 'replace-state)
286 (viper-change-cursor-color
287 (viper-frame-value viper-replace-overlay-cursor-color)))
288 (setq viper-last-posn-in-replace-region (point-marker)))))
289 ;; terminate replace mode if changed Viper states.
290 (t (viper-finish-change))))
293 ;; changing mode
295 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
296 (defun viper-change-state (new-state)
297 ;; Keep viper-post/pre-command-hooks fresh.
298 ;; We remove then add viper-post/pre-command-sentinel since it is very
299 ;; desirable that viper-pre-command-sentinel is the last hook and
300 ;; viper-post-command-sentinel is the first hook.
302 (when (featurep 'xemacs)
303 (make-local-hook 'viper-after-change-functions)
304 (make-local-hook 'viper-before-change-functions)
305 (make-local-hook 'viper-post-command-hooks)
306 (make-local-hook 'viper-pre-command-hooks))
308 (remove-hook 'post-command-hook 'viper-post-command-sentinel)
309 (add-hook 'post-command-hook 'viper-post-command-sentinel)
310 (remove-hook 'pre-command-hook 'viper-pre-command-sentinel)
311 (add-hook 'pre-command-hook 'viper-pre-command-sentinel t)
312 ;; These hooks will be added back if switching to insert/replace mode
313 (remove-hook 'viper-post-command-hooks
314 'viper-insert-state-post-command-sentinel 'local)
315 (remove-hook 'viper-pre-command-hooks
316 'viper-insert-state-pre-command-sentinel 'local)
317 (setq viper-intermediate-command nil)
318 (cond ((eq new-state 'vi-state)
319 (cond ((member viper-current-state '(insert-state replace-state))
321 ;; move viper-last-posn-while-in-insert-state
322 ;; This is a normal hook that is executed in insert/replace
323 ;; states after each command. In Vi/Emacs state, it does
324 ;; nothing. We need to execute it here to make sure that
325 ;; the last posn was recorded when we hit ESC.
326 ;; It may be left unrecorded if the last thing done in
327 ;; insert/repl state was dabbrev-expansion or abbrev
328 ;; expansion caused by hitting ESC
329 (viper-insert-state-post-command-sentinel)
331 (condition-case conds
332 (progn
333 (viper-save-last-insertion
334 viper-insert-point
335 viper-last-posn-while-in-insert-state)
336 (if viper-began-as-replace
337 (setq viper-began-as-replace nil)
338 ;; repeat insert commands if numerical arg > 1
339 (save-excursion
340 (viper-repeat-insert-command))))
341 (error
342 (viper-message-conditions conds)))
344 (if (> (length viper-last-insertion) 0)
345 (viper-push-onto-ring viper-last-insertion
346 'viper-insertion-ring))
348 (if viper-ESC-moves-cursor-back
349 (or (bolp) (viper-beginning-of-field) (backward-char 1))))
352 ;; insert or replace
353 ((memq new-state '(insert-state replace-state))
354 (if (memq viper-current-state '(emacs-state vi-state))
355 (viper-move-marker-locally 'viper-insert-point (point)))
356 (viper-move-marker-locally
357 'viper-last-posn-while-in-insert-state (point))
358 (add-hook 'viper-post-command-hooks
359 'viper-insert-state-post-command-sentinel t 'local)
360 (add-hook 'viper-pre-command-hooks
361 'viper-insert-state-pre-command-sentinel t 'local))
362 ) ; outermost cond
364 ;; Nothing needs to be done to switch to emacs mode! Just set some
365 ;; variables, which is already done in viper-change-state-to-emacs!
367 ;; ISO accents
368 ;; always turn off iso-accents-mode in vi-state, or else we won't be able to
369 ;; use the keys `,',^ , as they will do accents instead of Vi actions.
370 (cond ((eq new-state 'vi-state) (viper-set-iso-accents-mode nil));accents off
371 (viper-automatic-iso-accents (viper-set-iso-accents-mode t));accents on
372 (t (viper-set-iso-accents-mode nil)))
373 ;; Always turn off quail mode in vi state
374 (cond ((eq new-state 'vi-state) (viper-set-input-method nil)) ;intl input off
375 (viper-special-input-method (viper-set-input-method t)) ;intl input on
376 (t (viper-set-input-method nil)))
378 (setq viper-current-state new-state)
380 (viper-update-syntax-classes)
381 (viper-normalize-minor-mode-map-alist)
382 (viper-adjust-keys-for new-state)
383 (viper-set-mode-vars-for new-state)
384 (viper-refresh-mode-line)
388 (defun viper-adjust-keys-for (state)
389 "Make necessary adjustments to keymaps before entering STATE."
390 (cond ((memq state '(insert-state replace-state))
391 (if viper-auto-indent
392 (progn
393 (define-key viper-insert-basic-map "\C-m" 'viper-autoindent)
394 (if viper-want-emacs-keys-in-insert
395 ;; expert
396 (define-key viper-insert-basic-map "\C-j" nil)
397 ;; novice
398 (define-key viper-insert-basic-map "\C-j" 'viper-autoindent)))
399 (define-key viper-insert-basic-map "\C-m" nil)
400 (define-key viper-insert-basic-map "\C-j" nil))
402 (setq viper-insert-diehard-minor-mode
403 (not viper-want-emacs-keys-in-insert))
405 (if viper-want-ctl-h-help
406 (progn
407 (define-key viper-insert-basic-map "\C-h" 'help-command)
408 (define-key viper-replace-map "\C-h" 'help-command))
409 (define-key viper-insert-basic-map
410 "\C-h" 'viper-del-backward-char-in-insert)
411 (define-key viper-replace-map
412 "\C-h" 'viper-del-backward-char-in-replace))
413 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
414 (define-key viper-insert-basic-map
415 [backspace] 'viper-del-backward-char-in-insert)
416 (define-key viper-replace-map
417 [backspace] 'viper-del-backward-char-in-replace)
418 ) ; end insert/replace case
419 (t ; Vi state
420 (setq viper-vi-diehard-minor-mode (not viper-want-emacs-keys-in-vi))
421 (if viper-want-ctl-h-help
422 (define-key viper-vi-basic-map "\C-h" 'help-command)
423 (define-key viper-vi-basic-map "\C-h" 'viper-backward-char))
424 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
425 (define-key viper-vi-basic-map [backspace] 'viper-backward-char))
429 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
430 ;; This ensures that Viper bindings are in effect, regardless of which minor
431 ;; modes were turned on by the user or by other packages.
432 (defun viper-normalize-minor-mode-map-alist ()
433 (setq viper--intercept-key-maps
434 (list
435 (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map)
436 (cons 'viper-insert-intercept-minor-mode viper-insert-intercept-map)
437 (cons 'viper-emacs-intercept-minor-mode viper-emacs-intercept-map)
439 (setq viper--key-maps
440 (list (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map)
441 (cons 'viper-vi-local-user-minor-mode viper-vi-local-user-map)
442 (cons 'viper-vi-kbd-minor-mode viper-vi-kbd-map)
443 (cons 'viper-vi-global-user-minor-mode viper-vi-global-user-map)
444 (cons 'viper-vi-state-modifier-minor-mode
445 (if (keymapp
446 (cdr (assoc major-mode viper-vi-state-modifier-alist)))
447 (cdr (assoc major-mode viper-vi-state-modifier-alist))
448 viper-empty-keymap))
449 (cons 'viper-vi-diehard-minor-mode viper-vi-diehard-map)
450 (cons 'viper-vi-basic-minor-mode viper-vi-basic-map)
451 (cons 'viper-replace-minor-mode viper-replace-map)
452 ;; viper-insert-minibuffer-minor-mode must come after
453 ;; viper-replace-minor-mode
454 (cons 'viper-insert-minibuffer-minor-mode
455 viper-minibuffer-map)
456 (cons 'viper-insert-local-user-minor-mode
457 viper-insert-local-user-map)
458 (cons 'viper-insert-kbd-minor-mode viper-insert-kbd-map)
459 (cons 'viper-insert-global-user-minor-mode
460 viper-insert-global-user-map)
461 (cons 'viper-insert-state-modifier-minor-mode
462 (if (keymapp
463 (cdr (assoc major-mode
464 viper-insert-state-modifier-alist)))
465 (cdr (assoc major-mode
466 viper-insert-state-modifier-alist))
467 viper-empty-keymap))
468 (cons 'viper-insert-diehard-minor-mode viper-insert-diehard-map)
469 (cons 'viper-insert-basic-minor-mode viper-insert-basic-map)
470 (cons 'viper-emacs-local-user-minor-mode
471 viper-emacs-local-user-map)
472 (cons 'viper-emacs-kbd-minor-mode viper-emacs-kbd-map)
473 (cons 'viper-emacs-global-user-minor-mode
474 viper-emacs-global-user-map)
475 (cons 'viper-emacs-state-modifier-minor-mode
476 (if (keymapp
477 (cdr
478 (assoc major-mode viper-emacs-state-modifier-alist)))
479 (cdr
480 (assoc major-mode viper-emacs-state-modifier-alist))
481 viper-empty-keymap))
484 ;; This var is not local in Emacs, so we make it local. It must be local
485 ;; because although the stack of minor modes can be the same for all buffers,
486 ;; the associated *keymaps* can be different. In Viper,
487 ;; viper-vi-local-user-map, viper-insert-local-user-map, and others can have
488 ;; different keymaps for different buffers. Also, the keymaps associated
489 ;; with viper-vi/insert-state-modifier-minor-mode can be different.
490 ;; ***This is needed only in case emulation-mode-map-alists is not defined.
491 ;; In emacs with emulation-mode-map-alists, nothing needs to be done
492 (unless
493 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
494 (set (make-local-variable 'minor-mode-map-alist)
495 (viper-append-filter-alist
496 (append viper--intercept-key-maps viper--key-maps)
497 minor-mode-map-alist)))
502 ;; Viper mode-changing commands and utilities
504 ;; Modifies mode-line-buffer-identification.
505 (defun viper-refresh-mode-line ()
506 (set (make-local-variable 'viper-mode-string)
507 (cond ((eq viper-current-state 'emacs-state) viper-emacs-state-id)
508 ((eq viper-current-state 'vi-state) viper-vi-state-id)
509 ((eq viper-current-state 'replace-state) viper-replace-state-id)
510 ((eq viper-current-state 'insert-state) viper-insert-state-id)))
512 ;; Sets Viper mode string in global-mode-string
513 (force-mode-line-update))
516 ;; Switch from Insert state to Vi state.
517 (defun viper-exit-insert-state ()
518 (interactive)
519 (viper-change-state-to-vi))
521 (defun viper-set-mode-vars-for (state)
522 "Sets Viper minor mode variables to put Viper's state STATE in effect."
524 ;; Emacs state
525 (setq viper-vi-minibuffer-minor-mode nil
526 viper-insert-minibuffer-minor-mode nil
527 viper-vi-intercept-minor-mode nil
528 viper-insert-intercept-minor-mode nil
530 viper-vi-local-user-minor-mode nil
531 viper-vi-kbd-minor-mode nil
532 viper-vi-global-user-minor-mode nil
533 viper-vi-state-modifier-minor-mode nil
534 viper-vi-diehard-minor-mode nil
535 viper-vi-basic-minor-mode nil
537 viper-replace-minor-mode nil
539 viper-insert-local-user-minor-mode nil
540 viper-insert-kbd-minor-mode nil
541 viper-insert-global-user-minor-mode nil
542 viper-insert-state-modifier-minor-mode nil
543 viper-insert-diehard-minor-mode nil
544 viper-insert-basic-minor-mode nil
545 viper-emacs-intercept-minor-mode t
546 viper-emacs-local-user-minor-mode t
547 viper-emacs-kbd-minor-mode (not (viper-is-in-minibuffer))
548 viper-emacs-global-user-minor-mode t
549 viper-emacs-state-modifier-minor-mode t
552 ;; Vi state
553 (if (eq state 'vi-state) ; adjust for vi-state
554 (setq
555 viper-vi-intercept-minor-mode t
556 viper-vi-minibuffer-minor-mode (viper-is-in-minibuffer)
557 viper-vi-local-user-minor-mode t
558 viper-vi-kbd-minor-mode (not (viper-is-in-minibuffer))
559 viper-vi-global-user-minor-mode t
560 viper-vi-state-modifier-minor-mode t
561 ;; don't let the diehard keymap block command completion
562 ;; and other things in the minibuffer
563 viper-vi-diehard-minor-mode (not
564 (or viper-want-emacs-keys-in-vi
565 (viper-is-in-minibuffer)))
566 viper-vi-basic-minor-mode t
567 viper-emacs-intercept-minor-mode nil
568 viper-emacs-local-user-minor-mode nil
569 viper-emacs-kbd-minor-mode nil
570 viper-emacs-global-user-minor-mode nil
571 viper-emacs-state-modifier-minor-mode nil
574 ;; Insert and Replace states
575 (if (member state '(insert-state replace-state))
576 (setq
577 viper-insert-intercept-minor-mode t
578 viper-replace-minor-mode (eq state 'replace-state)
579 viper-insert-minibuffer-minor-mode (viper-is-in-minibuffer)
580 viper-insert-local-user-minor-mode t
581 viper-insert-kbd-minor-mode (not (viper-is-in-minibuffer))
582 viper-insert-global-user-minor-mode t
583 viper-insert-state-modifier-minor-mode t
584 ;; don't let the diehard keymap block command completion
585 ;; and other things in the minibuffer
586 viper-insert-diehard-minor-mode (not
588 viper-want-emacs-keys-in-insert
589 (viper-is-in-minibuffer)))
590 viper-insert-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 ;; minibuffer faces
599 (if (viper-has-face-support-p)
600 (setq viper-minibuffer-current-face
601 (cond ((eq state 'emacs-state) viper-minibuffer-emacs-face)
602 ((eq state 'vi-state) viper-minibuffer-vi-face)
603 ((memq state '(insert-state replace-state))
604 viper-minibuffer-insert-face))))
606 (if (viper-is-in-minibuffer)
607 (viper-set-minibuffer-overlay))
610 ;; This also takes care of the annoying incomplete lines in files.
611 ;; Also, this fixes `undo' to work vi-style for complex commands.
612 (defun viper-change-state-to-vi ()
613 "Change Viper state to Vi."
614 (interactive)
615 (if (and viper-first-time (not (viper-is-in-minibuffer)))
616 (viper-mode)
617 (if overwrite-mode (overwrite-mode -1))
618 (or (viper-overlay-p viper-replace-overlay)
619 (viper-set-replace-overlay (point-min) (point-min)))
620 (viper-hide-replace-overlay)
621 (if abbrev-mode (expand-abbrev))
622 (if (and auto-fill-function (> (current-column) fill-column))
623 (funcall auto-fill-function))
624 ;; don't leave whitespace lines around
625 (if (and (memq last-command
626 '(viper-autoindent
627 viper-open-line viper-Open-line
628 viper-replace-state-exit-cmd))
629 (viper-over-whitespace-line))
630 (indent-to-left-margin))
631 (viper-add-newline-at-eob-if-necessary)
632 (viper-adjust-undo)
634 (if (eq viper-current-state 'emacs-state)
635 (viper-restore-cursor-color 'after-emacs-mode)
636 (viper-restore-cursor-color 'after-insert-mode))
638 (viper-change-state 'vi-state)
640 ;; Protect against user errors in hooks
641 (condition-case conds
642 (run-hooks 'viper-vi-state-hook)
643 (error
644 (viper-message-conditions conds)))))
646 (defun viper-change-state-to-insert ()
647 "Change Viper state to Insert."
648 (interactive)
649 (viper-change-state 'insert-state)
651 (or (viper-overlay-p viper-replace-overlay)
652 (viper-set-replace-overlay (point-min) (point-min)))
653 (viper-hide-replace-overlay)
655 (let ((icolor (viper-frame-value viper-insert-state-cursor-color)))
656 (or (stringp (viper-get-saved-cursor-color-in-insert-mode))
657 (string= (viper-get-cursor-color) icolor)
658 (viper-save-cursor-color 'before-insert-mode))
659 (viper-change-cursor-color icolor))
661 ;; Protect against user errors in hooks
662 (condition-case conds
663 (run-hooks 'viper-insert-state-hook)
664 (error
665 (viper-message-conditions conds))))
667 (defsubst viper-downgrade-to-insert ()
668 ;; Protect against user errors in hooks
669 (condition-case conds
670 (run-hooks 'viper-insert-state-hook)
671 (error
672 (viper-message-conditions conds)))
673 (setq viper-current-state 'insert-state
674 viper-replace-minor-mode nil))
678 ;; Change to replace state. When the end of replacement region is reached,
679 ;; replace state changes to insert state.
680 (defun viper-change-state-to-replace (&optional non-R-cmd)
681 (viper-change-state 'replace-state)
682 ;; Run insert-state-hook
683 (condition-case conds
684 (run-hooks 'viper-insert-state-hook 'viper-replace-state-hook)
685 (error
686 (viper-message-conditions conds)))
688 (if non-R-cmd
689 (viper-start-replace)
690 ;; 'R' is implemented using Emacs's overwrite-mode
691 (viper-start-R-mode))
695 (defun viper-change-state-to-emacs ()
696 "Change Viper state to Emacs."
697 (interactive)
698 (or (viper-overlay-p viper-replace-overlay)
699 (viper-set-replace-overlay (point-min) (point-min)))
700 (viper-hide-replace-overlay)
702 (let ((ecolor (viper-frame-value viper-emacs-state-cursor-color)))
703 (when ecolor
704 (or (stringp (viper-get-saved-cursor-color-in-emacs-mode))
705 (string= (viper-get-cursor-color) ecolor)
706 (viper-save-cursor-color 'before-emacs-mode))
707 (viper-change-cursor-color ecolor)))
709 (viper-change-state 'emacs-state)
711 ;; Protect against user errors in hooks
712 (condition-case conds
713 (run-hooks 'viper-emacs-state-hook)
714 (error
715 (viper-message-conditions conds))))
717 ;; escape to emacs mode termporarily
718 (defun viper-escape-to-emacs (arg &optional events)
719 "Escape to Emacs state from Vi state for one Emacs command.
720 ARG is used as the prefix value for the executed command. If
721 EVENTS is a list of events, which become the beginning of the command."
722 (interactive "P")
723 (if (viper= (viper-last-command-char) ?\\)
724 (message "Switched to EMACS state for the next command..."))
725 (viper-escape-to-state arg events 'emacs-state))
727 ;; escape to Vi mode termporarily
728 (defun viper-escape-to-vi (arg)
729 "Escape from Emacs state to Vi state for one Vi 1-character command.
730 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
731 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
732 `viper-escape-to-vi' is used."
733 (interactive "P")
734 (message "Switched to VI state for the next command...")
735 (viper-escape-to-state arg nil 'vi-state))
737 ;; Escape to STATE mode for one Emacs command.
738 (defun viper-escape-to-state (arg events state)
739 ;;(let (com key prefix-arg)
740 (let (com key)
741 ;; this temporarily turns off Viper's minor mode keymaps
742 (viper-set-mode-vars-for state)
743 (viper-normalize-minor-mode-map-alist)
744 (if events (viper-set-unread-command-events events))
746 ;; protect against keyboard quit and other errors
747 (condition-case nil
748 (let (viper-vi-kbd-minor-mode
749 viper-insert-kbd-minor-mode
750 viper-emacs-kbd-minor-mode)
751 (unwind-protect
752 (progn
753 (setq com
754 (key-binding (setq key (viper-read-key-sequence nil))))
755 ;; In case of binding indirection--chase definitions.
756 ;; Have to do it here because we execute this command under
757 ;; different keymaps, so command-execute may not do the
758 ;; right thing there
759 (while (vectorp com) (setq com (key-binding com))))
760 nil)
761 ;; Execute command com in the original Viper state, not in state
762 ;; `state'. Otherwise, if we switch buffers while executing the
763 ;; escaped to command, Viper's mode vars will remain those of
764 ;; `state'. When we return to the orig buffer, the bindings will be
765 ;; screwed up.
766 (viper-set-mode-vars-for viper-current-state)
768 ;; this-command, last-command-char, last-command-event
769 (setq this-command com)
770 (if (featurep 'xemacs)
771 ;; XEmacs represents key sequences as vectors
772 (setq last-command-event
773 (viper-copy-event (viper-seq-last-elt key))
774 last-command-char (event-to-character last-command-event))
775 ;; Emacs represents them as sequences (str or vec)
776 (setq last-command-event
777 (viper-copy-event (viper-seq-last-elt key))))
779 (if (commandp com)
780 ;; pretend that current state is the state we excaped to
781 (let ((viper-current-state state))
782 (setq prefix-arg (or prefix-arg arg))
783 (command-execute com)))
785 (quit (ding))
786 (error (beep 1))))
787 ;; set state in the new buffer
788 (viper-set-mode-vars-for viper-current-state))
790 ;; This is used in order to allow reading characters according to the input
791 ;; method. The character is read in emacs and inserted into the buffer.
792 ;; If an input method is in effect, this might
793 ;; cause several characters to be combined into one.
794 ;; Also takes care of the iso-accents mode
795 (defun viper-special-read-and-insert-char ()
796 (viper-set-mode-vars-for 'emacs-state)
797 (viper-normalize-minor-mode-map-alist)
798 (if viper-special-input-method
799 (viper-set-input-method t))
800 (if viper-automatic-iso-accents
801 (viper-set-iso-accents-mode t))
802 (condition-case nil
803 (let (viper-vi-kbd-minor-mode
804 viper-insert-kbd-minor-mode
805 viper-emacs-kbd-minor-mode
807 (cond ((and viper-special-input-method
808 (featurep 'emacs)
809 (fboundp 'quail-input-method))
810 ;; (let ...) is used to restore unread-command-events to the
811 ;; original state. We don't want anything left in there after
812 ;; key translation. (Such left-overs are possible if the user
813 ;; types a regular key.)
814 (let (unread-command-events)
815 ;; The next cmd and viper-set-unread-command-events
816 ;; are intended to prevent the input method
817 ;; from swallowing ^M, ^Q and other special characters
818 (setq ch (read-char-exclusive))
819 ;; replace ^M with the newline
820 (if (eq ch ?\C-m) (setq ch ?\n))
821 ;; Make sure ^V and ^Q work as quotation chars
822 (if (memq ch '(?\C-v ?\C-q))
823 (setq ch (read-char-exclusive)))
824 (viper-set-unread-command-events ch)
825 (quail-input-method nil)
827 (if (and ch (string= quail-current-str ""))
828 (insert ch)
829 (insert quail-current-str))
830 (setq ch (or ch
831 (aref quail-current-str
832 (1- (length quail-current-str)))))
834 ((and viper-special-input-method
835 (featurep 'xemacs)
836 (fboundp 'quail-start-translation))
837 ;; same as above but for XEmacs, which doesn't have
838 ;; quail-input-method
839 (let (unread-command-events)
840 (setq ch (read-char-exclusive))
841 ;; replace ^M with the newline
842 (if (eq ch ?\C-m) (setq ch ?\n))
843 ;; Make sure ^V and ^Q work as quotation chars
844 (if (memq ch '(?\C-v ?\C-q))
845 (setq ch (read-char-exclusive)))
846 (viper-set-unread-command-events ch)
847 (quail-start-translation nil)
849 (if (and ch (string= quail-current-str ""))
850 (insert ch)
851 (insert quail-current-str))
852 (setq ch (or ch
853 (aref quail-current-str
854 (1- (length quail-current-str)))))
856 ((and (boundp 'iso-accents-mode) iso-accents-mode)
857 (setq ch (aref (read-key-sequence nil) 0))
858 ;; replace ^M with the newline
859 (if (eq ch ?\C-m) (setq ch ?\n))
860 ;; Make sure ^V and ^Q work as quotation chars
861 (if (memq ch '(?\C-v ?\C-q))
862 (setq ch (aref (read-key-sequence nil) 0)))
863 (insert ch))
865 ;;(setq ch (read-char-exclusive))
866 (setq ch (aref (read-key-sequence nil) 0))
867 (if (featurep 'xemacs)
868 (setq ch (event-to-character ch)))
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 (progn
874 ;;(setq ch (read-char-exclusive))
875 (setq ch (aref (read-key-sequence nil) 0))
876 (if (featurep 'xemacs)
877 (setq ch (event-to-character ch))))
879 (insert ch))
881 (setq last-command-event
882 (viper-copy-event (if (featurep 'xemacs)
883 (character-to-event ch) ch)))
884 ) ; let
885 (error nil)
886 ) ; condition-case
888 (viper-set-input-method nil)
889 (viper-set-iso-accents-mode nil)
890 (viper-set-mode-vars-for viper-current-state)
894 (defun viper-exec-form-in-vi (form)
895 "Execute FORM in Vi state, regardless of the current Vi state."
896 (let ((buff (current-buffer))
897 result)
898 (viper-set-mode-vars-for 'vi-state)
900 (condition-case nil
901 (let (viper-vi-kbd-minor-mode) ; execute without kbd macros
902 (setq result (eval form)))
903 (error
904 (signal 'quit nil)))
906 (if (not (equal buff (current-buffer))) ; cmd switched buffer
907 (with-current-buffer buff
908 (viper-set-mode-vars-for viper-current-state)))
909 (viper-set-mode-vars-for viper-current-state)
910 result))
912 (defun viper-exec-form-in-emacs (form)
913 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
914 Similar to `viper-escape-to-emacs', but accepts forms rather than keystrokes."
915 (let ((buff (current-buffer))
916 result)
917 (viper-set-mode-vars-for 'emacs-state)
918 (setq result (eval form))
919 (if (not (equal buff (current-buffer))) ; cmd switched buffer
920 (with-current-buffer buff
921 (viper-set-mode-vars-for viper-current-state)))
922 (viper-set-mode-vars-for viper-current-state)
923 result))
925 ;; This executes the last kbd event in emacs mode. Is used when we want to
926 ;; interpret certain keys directly in emacs (as, for example, in comint mode).
927 (defun viper-exec-key-in-emacs (arg)
928 (interactive "P")
929 (viper-escape-to-emacs arg last-command-event))
932 ;; This is needed because minor modes sometimes override essential Viper
933 ;; bindings. By letting Viper know which files these modes are in, it will
934 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
935 (defun viper-harness-minor-mode (load-file)
936 "Familiarize Viper with a minor mode defined in LOAD-FILE.
937 Minor modes that have their own keymaps may overshadow Viper keymaps.
938 This function is designed to make Viper aware of the packages that define
939 such minor modes.
940 Usage:
941 (viper-harness-minor-mode load-file)
943 LOAD-FILE is the name of the file where the specific minor mode is defined.
944 Suffixes such as .el or .elc should be stripped."
946 (interactive "sEnter name of the load file: ")
948 (eval-after-load load-file '(viper-normalize-minor-mode-map-alist))
950 ;; Change the default for minor-mode-map-alist each time a harnessed minor
951 ;; mode adds its own keymap to the a-list.
952 (unless
953 (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
954 (eval-after-load
955 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist)))
959 (defun viper-ESC (arg)
960 "Emulate ESC key in Emacs.
961 Prevents multiple escape keystrokes if viper-no-multiple-ESC is true.
962 If viper-no-multiple-ESC is 'twice double ESC would ding in vi-state.
963 Other ESC sequences are emulated via the current Emacs's major mode
964 keymap. This is more convenient on TTYs, since this won't block
965 function keys such as up, down, etc. ESC will also will also work as
966 a Meta key in this case. When viper-no-multiple-ESC is nil, ESC works
967 as a Meta key and any number of multiple escapes are allowed."
968 (interactive "P")
969 (let (char)
970 (cond ((and (not viper-no-multiple-ESC) (eq viper-current-state 'vi-state))
971 (setq char (viper-read-char-exclusive))
972 (viper-escape-to-emacs arg (list ?\e char) ))
973 ((and (eq viper-no-multiple-ESC 'twice)
974 (eq viper-current-state 'vi-state))
975 (setq char (viper-read-char-exclusive))
976 (if (= char (string-to-char viper-ESC-key))
977 (ding)
978 (viper-escape-to-emacs arg (list ?\e char) )))
979 (t (ding)))
982 (defun viper-alternate-Meta-key (arg)
983 "Simulate Emacs Meta key."
984 (interactive "P")
985 (sit-for 1) (message "ESC-")
986 (viper-escape-to-emacs arg '(?\e)))
988 (defun viper-toggle-key-action ()
989 "Action bound to `viper-toggle-key'."
990 (interactive)
991 (if (and (< viper-expert-level 2) (equal viper-toggle-key "\C-z"))
992 (if (viper-window-display-p)
993 (viper-iconify)
994 (suspend-emacs))
995 (viper-change-state-to-emacs)))
998 ;; Intercept ESC sequences on dumb terminals.
999 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
1001 ;; Check if last key was ESC and if so try to reread it as a function key.
1002 ;; But only if there are characters to read during a very short time.
1003 ;; Returns the last event, if any.
1004 (defun viper-envelop-ESC-key ()
1005 (let ((event last-input-event)
1006 (keyseq [nil])
1007 (inhibit-quit t))
1008 (if (viper-ESC-event-p event)
1009 (progn
1010 ;; Some versions of Emacs (eg., 22.50.8 have a bug, which makes even
1011 ;; a single ESC into ;; a fast keyseq. To guard against this, we
1012 ;; added a check if there are other events as well. Keep the next
1013 ;; line for the next time the bug reappears, so that will remember to
1014 ;; report it.
1015 ;;(if (and (viper-fast-keysequence-p) unread-command-events)
1016 (if (viper-fast-keysequence-p) ;; for Emacsen without the above bug
1017 (progn
1018 (let (minor-mode-map-alist emulation-mode-map-alists)
1019 (viper-set-unread-command-events event)
1020 (setq keyseq (read-key-sequence nil 'continue-echo))
1021 ) ; let
1022 ;; If keyseq translates into something that still has ESC
1023 ;; at the beginning, separate ESC from the rest of the seq.
1024 ;; In XEmacs we check for events that are keypress meta-key
1025 ;; and convert them into [escape key]
1027 ;; This is needed for the following reason:
1028 ;; If ESC is the first symbol, we interpret it as if the
1029 ;; user typed ESC and then quickly some other symbols.
1030 ;; If ESC is not the first one, then the key sequence
1031 ;; entered was apparently translated into a function key or
1032 ;; something (e.g., one may have
1033 ;; (define-key function-key-map "\e[192z" [f11])
1034 ;; which would translate the escape-sequence generated by
1035 ;; f11 in an xterm window into the symbolic key f11.
1037 ;; If `first-key' is not an ESC event, we make it into the
1038 ;; last-command-event in order to pretend that this key was
1039 ;; pressed. This is needed to allow arrow keys to be bound to
1040 ;; macros. Otherwise, viper-exec-mapped-kbd-macro will think
1041 ;; that the last event was ESC and so it'll execute whatever is
1042 ;; bound to ESC. (Viper macros can't be bound to
1043 ;; ESC-sequences).
1044 (let* ((first-key (elt keyseq 0))
1045 (key-mod (event-modifiers first-key)))
1046 (cond ((and (viper-ESC-event-p first-key)
1047 (not (viper-translate-all-ESC-keysequences)))
1048 ;; put keys following ESC on the unread list
1049 ;; and return ESC as the key-sequence
1050 (viper-set-unread-command-events (viper-subseq keyseq 1))
1051 (setq last-input-event event
1052 keyseq (if (featurep 'emacs)
1053 "\e"
1054 (vector (character-to-event ?\e)))))
1055 ((and (featurep 'xemacs)
1056 (key-press-event-p first-key)
1057 (equal '(meta) key-mod))
1058 (viper-set-unread-command-events
1059 (vconcat (vector
1060 (character-to-event (event-key first-key)))
1061 (viper-subseq keyseq 1)))
1062 (setq last-input-event event
1063 keyseq (vector (character-to-event ?\e))))
1064 ((eventp first-key)
1065 (setq last-command-event
1066 (viper-copy-event first-key)))
1068 ) ; end progn
1070 ;; this is escape event with nothing after it
1071 ;; put in unread-command-event and then re-read
1072 (viper-set-unread-command-events event)
1073 (setq keyseq (read-key-sequence nil))
1075 ;; not an escape event
1076 (setq keyseq (vector event)))
1077 keyseq))
1081 ;; Listen to ESC key.
1082 ;; If a sequence of keys starting with ESC is issued with very short delays,
1083 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
1084 (defun viper-intercept-ESC-key ()
1085 "Function that implements ESC key in Viper emulation of Vi."
1086 (interactive)
1087 (let ((cmd (or (key-binding (viper-envelop-ESC-key))
1088 '(lambda () (interactive) (error "Viper bell")))))
1090 ;; call the actual function to execute ESC (if no other symbols followed)
1091 ;; or the key bound to the ESC sequence (if the sequence was issued
1092 ;; with very short delay between characters).
1093 (if (eq cmd 'viper-intercept-ESC-key)
1094 (setq cmd
1095 (cond ((eq viper-current-state 'vi-state)
1096 'viper-ESC)
1097 ((eq viper-current-state 'insert-state)
1098 'viper-exit-insert-state)
1099 ((eq viper-current-state 'replace-state)
1100 'viper-replace-state-exit-cmd)
1101 (t 'viper-change-state-to-vi)
1103 (call-interactively cmd)))
1108 ;; prefix argument for Vi mode
1110 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
1111 ;; represents the numeric value of the prefix argument and COM represents
1112 ;; command prefix such as "c", "d", "m" and "y".
1114 ;; Get value part of prefix-argument ARG.
1115 (defsubst viper-p-val (arg)
1116 (cond ((null arg) 1)
1117 ((consp arg)
1118 (if (or (null (car arg)) (equal (car arg) '(nil)))
1119 1 (car arg)))
1120 (t arg)))
1122 ;; Get raw value part of prefix-argument ARG.
1123 (defsubst viper-P-val (arg)
1124 (cond ((consp arg) (car arg))
1125 (t arg)))
1127 ;; Get com part of prefix-argument ARG.
1128 (defsubst viper-getcom (arg)
1129 (cond ((null arg) nil)
1130 ((consp arg) (cdr arg))
1131 (t nil)))
1133 ;; Get com part of prefix-argument ARG and modify it.
1134 (defun viper-getCom (arg)
1135 (let ((com (viper-getcom arg)))
1136 (cond ((viper= com ?c) ?c)
1137 ;; Previously, ?c was being converted to ?C, but this prevented
1138 ;; multiline replace regions.
1139 ;;((viper= com ?c) ?C)
1140 ((viper= com ?d) ?D)
1141 ((viper= com ?y) ?Y)
1142 (t com))))
1145 ;; Compute numeric prefix arg value.
1146 ;; Invoked by EVENT-CHAR. COM is the command part obtained so far.
1147 (defun viper-prefix-arg-value (event-char com)
1148 (let ((viper-intermediate-command 'viper-digit-argument)
1149 value func)
1150 ;; read while number
1151 (while (and (viper-characterp event-char)
1152 (>= event-char ?0) (<= event-char ?9))
1153 (setq value (+ (* (if (integerp value) value 0) 10) (- event-char ?0)))
1154 (setq event-char (viper-read-event-convert-to-char)))
1156 (setq prefix-arg value)
1157 (if com (setq prefix-arg (cons prefix-arg com)))
1158 (while (eq event-char ?U)
1159 (viper-describe-arg prefix-arg)
1160 (setq event-char (viper-read-event-convert-to-char)))
1162 (if (or com (and (not (eq viper-current-state 'vi-state))
1163 ;; make sure it is a Vi command
1164 (viper-characterp event-char)
1165 (viper-vi-command-p event-char)
1167 ;; If appears to be one of the vi commands,
1168 ;; then execute it with funcall and clear prefix-arg in order to not
1169 ;; confuse subsequent commands
1170 (progn
1171 ;; last-command-event is the char we want emacs to think was typed
1172 ;; last. If com is not nil, the viper-digit-argument command was
1173 ;; called from within viper-prefix-arg command, such as `d', `w',
1174 ;; etc., i.e., the user typed, say, d2. In this case, `com' would be
1175 ;; `d', `w', etc. If viper-digit-argument was invoked by
1176 ;; viper-escape-to-vi (which is indicated by the fact that the
1177 ;; current state is not vi-state), then `event-char' represents the
1178 ;; vi command to be executed (e.g., `d', `w', etc). Again,
1179 ;; last-command-event must make emacs believe that this is the command
1180 ;; we typed.
1181 (cond ((eq event-char 'return) (setq event-char ?\C-m))
1182 ((eq event-char 'delete) (setq event-char ?\C-?))
1183 ((eq event-char 'backspace) (setq event-char ?\C-h))
1184 ((eq event-char 'space) (setq event-char ?\ )))
1185 (setq last-command-event
1186 (if (featurep 'xemacs)
1187 (character-to-event (or com event-char))
1188 (or com event-char)))
1189 (setq func (viper-exec-form-in-vi
1190 `(key-binding (char-to-string ,event-char))))
1191 (funcall func prefix-arg)
1192 (setq prefix-arg nil))
1193 ;; some other command -- let emacs do it in its own way
1194 (viper-set-unread-command-events event-char))
1198 ;; Vi operator as prefix argument."
1199 (defun viper-prefix-arg-com (char value com)
1200 (let ((cont t)
1201 cmd-info
1202 cmd-to-exec-at-end)
1203 (while (and cont
1204 (viper-memq-char char
1205 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1206 viper-buffer-search-char)))
1207 (if com
1208 ;; this means that we already have a command character, so we
1209 ;; construct a com list and exit while. however, if char is "
1210 ;; it is an error.
1211 (progn
1212 ;; new com is (CHAR . OLDCOM)
1213 (if (viper-memq-char char '(?# ?\")) (error "Viper bell"))
1214 (setq com (cons char com))
1215 (setq cont nil))
1216 ;; If com is nil we set com as char, and read more. Again, if char is
1217 ;; ", we read the name of register and store it in viper-use-register.
1218 ;; if char is !, =, or #, a complete com is formed so we exit the while
1219 ;; loop.
1220 (cond ((viper-memq-char char '(?! ?=))
1221 (setq com char)
1222 (setq char (read-char))
1223 (setq cont nil))
1224 ((viper= char ?#)
1225 ;; read a char and encode it as com
1226 (setq com (+ 128 (read-char)))
1227 (setq char (read-char)))
1228 ((viper= char ?\")
1229 (let ((reg (read-char)))
1230 (if (viper-valid-register reg)
1231 (setq viper-use-register reg)
1232 (error "Viper bell"))
1233 (setq char (read-char))))
1235 (setq com char)
1236 (setq char (read-char))))))
1238 (if (atom com)
1239 ;; `com' is a single char, so we construct the command argument
1240 ;; and if `char' is `?', we describe the arg; otherwise
1241 ;; we prepare the command that will be executed at the end.
1242 (progn
1243 (setq cmd-info (cons value com))
1244 (while (viper= char ?U)
1245 (viper-describe-arg cmd-info)
1246 (setq char (read-char)))
1247 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so
1248 ;; we execute it at the very end
1249 (or (viper-movement-command-p char)
1250 (viper-digit-command-p char)
1251 (viper-regsuffix-command-p char)
1252 (viper= char ?!) ; bang command
1253 (viper= char ?g) ; the gg command (like G0)
1254 (error "Viper bell"))
1255 (setq cmd-to-exec-at-end
1256 (viper-exec-form-in-vi
1257 `(key-binding (char-to-string ,char)))))
1259 ;; as com is non-nil, this means that we have a command to execute
1260 (if (viper-memq-char (car com) '(?r ?R))
1261 ;; execute appropriate region command.
1262 (let ((char (car com)) (com (cdr com)))
1263 (setq prefix-arg (cons value com))
1264 (if (viper= char ?r)
1265 (viper-region prefix-arg)
1266 (viper-Region prefix-arg))
1267 ;; reset prefix-arg
1268 (setq prefix-arg nil))
1269 ;; otherwise, reset prefix arg and call appropriate command
1270 (setq value (if (null value) 1 value))
1271 (setq prefix-arg nil)
1272 (cond
1273 ;; If we change ?C to ?c here, then cc will enter replacement mode
1274 ;; rather than deleting lines. However, it will affect 1 less line
1275 ;; than normal. We decided to not use replacement mode here and
1276 ;; follow Vi, since replacement mode on n full lines can be achieved
1277 ;; with nC.
1278 ((equal com '(?c . ?c)) (viper-line (cons value ?C)))
1279 ((equal com '(?d . ?d)) (viper-line (cons value ?D)))
1280 ((equal com '(?d . ?y)) (viper-yank-defun))
1281 ((equal com '(?y . ?y)) (viper-line (cons value ?Y)))
1282 ((equal com '(?< . ?<)) (viper-line (cons value ?<)))
1283 ((equal com '(?> . ?>)) (viper-line (cons value ?>)))
1284 ((equal com '(?! . ?!)) (viper-line (cons value ?!)))
1285 ((equal com '(?= . ?=)) (viper-line (cons value ?=)))
1286 ;; gg acts as G0
1287 ((equal (car com) ?g) (viper-goto-line 0))
1288 (t (error "Viper bell")))))
1290 (if cmd-to-exec-at-end
1291 (progn
1292 (setq last-command-event
1293 (viper-copy-event
1294 (if (featurep 'xemacs) (character-to-event char) char)))
1295 (condition-case err
1296 (funcall cmd-to-exec-at-end cmd-info)
1297 (error
1298 (error "%s" (error-message-string err))))))
1301 (defun viper-describe-arg (arg)
1302 (let (val com)
1303 (setq val (viper-P-val arg)
1304 com (viper-getcom arg))
1305 (if (null val)
1306 (if (null com)
1307 (message "Value is nil, and command is nil")
1308 (message "Value is nil, and command is `%c'" com))
1309 (if (null com)
1310 (message "Value is `%d', and command is nil" val)
1311 (message "Value is `%d', and command is `%c'" val com)))))
1313 (defun viper-digit-argument (arg)
1314 "Begin numeric argument for the next command."
1315 (interactive "P")
1316 (viper-leave-region-active)
1317 (viper-prefix-arg-value
1318 (viper-last-command-char) (if (consp arg) (cdr arg) nil)))
1320 (defun viper-command-argument (arg)
1321 "Accept a motion command as an argument."
1322 (interactive "P")
1323 (let ((viper-intermediate-command 'viper-command-argument))
1324 (condition-case nil
1325 (viper-prefix-arg-com
1326 (viper-last-command-char)
1327 (cond ((null arg) nil)
1328 ((consp arg) (car arg))
1329 ((integerp arg) arg)
1330 (t (error viper-InvalidCommandArgument)))
1331 (cond ((null arg) nil)
1332 ((consp arg) (cdr arg))
1333 ((integerp arg) nil)
1334 (t (error viper-InvalidCommandArgument))))
1335 (quit (setq viper-use-register nil)
1336 (signal 'quit nil)))
1337 (viper-deactivate-mark)))
1340 ;; repeat last destructive command
1342 ;; Append region to text in register REG.
1343 ;; START and END are buffer positions indicating what to append.
1344 (defsubst viper-append-to-register (reg start end)
1345 (set-register reg (concat (if (stringp (get-register reg))
1346 (get-register reg) "")
1347 (buffer-substring start end))))
1349 ;; Saves last inserted text for possible use by viper-repeat command.
1350 (defun viper-save-last-insertion (beg end)
1351 (condition-case nil
1352 (setq viper-last-insertion (buffer-substring beg end))
1353 (error
1354 ;; beg or end marker are somehow screwed up
1355 (setq viper-last-insertion nil)))
1356 (setq viper-last-insertion (buffer-substring beg end))
1357 (or (< (length viper-d-com) 5)
1358 (setcar (nthcdr 4 viper-d-com) viper-last-insertion))
1359 (or (null viper-command-ring)
1360 (ring-empty-p viper-command-ring)
1361 (progn
1362 (setcar (nthcdr 4 (viper-current-ring-item viper-command-ring))
1363 viper-last-insertion)
1364 ;; del most recent elt, if identical to the second most-recent
1365 (viper-cleanup-ring viper-command-ring)))
1368 (defsubst viper-yank-last-insertion ()
1369 "Inserts the text saved by the previous viper-save-last-insertion command."
1370 (condition-case nil
1371 (insert viper-last-insertion)
1372 (error nil)))
1375 ;; define functions to be executed
1377 ;; invoked by the `C' command
1378 (defun viper-exec-change (m-com com)
1379 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1380 (set-marker viper-com-point (point) (current-buffer)))
1381 ;; handle C cmd at the eol and at eob.
1382 (if (or (and (eolp) (= viper-com-point (point)))
1383 (= viper-com-point (point-max)))
1384 (progn
1385 (insert " ")(backward-char 1)))
1386 (if (= viper-com-point (point))
1387 (viper-forward-char-carefully))
1388 (set-mark viper-com-point)
1389 (if (eq m-com 'viper-next-line-at-bol)
1390 (viper-enlarge-region (mark t) (point)))
1391 (if (< (point) (mark t))
1392 (exchange-point-and-mark))
1393 (if (eq (preceding-char) ?\n)
1394 (viper-backward-char-carefully)) ; give back the newline
1395 (if (eq viper-intermediate-command 'viper-repeat)
1396 (viper-change-subr (mark t) (point))
1397 (viper-change (mark t) (point))
1400 ;; this is invoked by viper-substitute-line
1401 (defun viper-exec-Change (m-com com)
1402 (save-excursion
1403 (set-mark viper-com-point)
1404 (viper-enlarge-region (mark t) (point))
1405 (if viper-use-register
1406 (progn
1407 (cond ((viper-valid-register viper-use-register '(letter digit))
1408 (copy-to-register
1409 viper-use-register (mark t) (point) nil))
1410 ((viper-valid-register viper-use-register '(Letter))
1411 (viper-append-to-register
1412 (downcase viper-use-register) (mark t) (point)))
1413 (t (setq viper-use-register nil)
1414 (error viper-InvalidRegister viper-use-register)))
1415 (setq viper-use-register nil)))
1416 (delete-region (mark t) (point)))
1417 (open-line 1)
1418 (if (eq viper-intermediate-command 'viper-repeat)
1419 (viper-yank-last-insertion)
1420 (viper-change-state-to-insert)
1423 (defun viper-exec-delete (m-com com)
1424 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1425 (set-marker viper-com-point (point) (current-buffer)))
1426 (let (chars-deleted)
1427 (if viper-use-register
1428 (progn
1429 (cond ((viper-valid-register viper-use-register '(letter digit))
1430 (copy-to-register
1431 viper-use-register viper-com-point (point) nil))
1432 ((viper-valid-register viper-use-register '(Letter))
1433 (viper-append-to-register
1434 (downcase viper-use-register) viper-com-point (point)))
1435 (t (setq viper-use-register nil)
1436 (error viper-InvalidRegister viper-use-register)))
1437 (setq viper-use-register nil)))
1438 (setq last-command
1439 (if (eq last-command 'd-command) 'kill-region nil))
1440 (setq chars-deleted (abs (- (point) viper-com-point)))
1441 (if (> chars-deleted viper-change-notification-threshold)
1442 (unless (viper-is-in-minibuffer)
1443 (message "Deleted %d characters" chars-deleted)))
1444 (kill-region viper-com-point (point))
1445 (setq this-command 'd-command)
1446 (if viper-ex-style-motion
1447 (if (and (eolp) (not (bolp))) (backward-char 1)))))
1449 (defun viper-exec-Delete (m-com com)
1450 (save-excursion
1451 (set-mark viper-com-point)
1452 (viper-enlarge-region (mark t) (point))
1453 (let (lines-deleted)
1454 (if viper-use-register
1455 (progn
1456 (cond ((viper-valid-register viper-use-register '(letter digit))
1457 (copy-to-register
1458 viper-use-register (mark t) (point) nil))
1459 ((viper-valid-register viper-use-register '(Letter))
1460 (viper-append-to-register
1461 (downcase viper-use-register) (mark t) (point)))
1462 (t (setq viper-use-register nil)
1463 (error viper-InvalidRegister viper-use-register)))
1464 (setq viper-use-register nil)))
1465 (setq last-command
1466 (if (eq last-command 'D-command) 'kill-region nil))
1467 (setq lines-deleted (count-lines (point) viper-com-point))
1468 (if (> lines-deleted viper-change-notification-threshold)
1469 (unless (viper-is-in-minibuffer)
1470 (message "Deleted %d lines" lines-deleted)))
1471 (kill-region (mark t) (point))
1472 (if (eq m-com 'viper-line) (setq this-command 'D-command)))
1473 (back-to-indentation)))
1475 ;; save region
1476 (defun viper-exec-yank (m-com com)
1477 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1478 (set-marker viper-com-point (point) (current-buffer)))
1479 (let (chars-saved)
1480 (if viper-use-register
1481 (progn
1482 (cond ((viper-valid-register viper-use-register '(letter digit))
1483 (copy-to-register
1484 viper-use-register viper-com-point (point) nil))
1485 ((viper-valid-register viper-use-register '(Letter))
1486 (viper-append-to-register
1487 (downcase viper-use-register) viper-com-point (point)))
1488 (t (setq viper-use-register nil)
1489 (error viper-InvalidRegister viper-use-register)))
1490 (setq viper-use-register nil)))
1491 (setq last-command nil)
1492 (copy-region-as-kill viper-com-point (point))
1493 (setq chars-saved (abs (- (point) viper-com-point)))
1494 (if (> chars-saved viper-change-notification-threshold)
1495 (unless (viper-is-in-minibuffer)
1496 (message "Saved %d characters" chars-saved)))
1497 (goto-char viper-com-point)))
1499 ;; save lines
1500 (defun viper-exec-Yank (m-com com)
1501 (save-excursion
1502 (set-mark viper-com-point)
1503 (viper-enlarge-region (mark t) (point))
1504 (let (lines-saved)
1505 (if viper-use-register
1506 (progn
1507 (cond ((viper-valid-register viper-use-register '(letter digit))
1508 (copy-to-register
1509 viper-use-register (mark t) (point) nil))
1510 ((viper-valid-register viper-use-register '(Letter))
1511 (viper-append-to-register
1512 (downcase viper-use-register) (mark t) (point)))
1513 (t (setq viper-use-register nil)
1514 (error viper-InvalidRegister viper-use-register)))
1515 (setq viper-use-register nil)))
1516 (setq last-command nil)
1517 (copy-region-as-kill (mark t) (point))
1518 (setq lines-saved (count-lines (mark t) (point)))
1519 (if (> lines-saved viper-change-notification-threshold)
1520 (unless (viper-is-in-minibuffer)
1521 (message "Saved %d lines" lines-saved)))))
1522 (viper-deactivate-mark)
1523 (goto-char viper-com-point))
1525 (defun viper-exec-bang (m-com com)
1526 (save-excursion
1527 (set-mark viper-com-point)
1528 (viper-enlarge-region (mark t) (point))
1529 (exchange-point-and-mark)
1530 (shell-command-on-region
1531 (mark t) (point)
1532 (if (viper= com ?!)
1533 (setq viper-last-shell-com
1534 (viper-read-string-with-history
1537 'viper-shell-history
1538 (car viper-shell-history)
1540 viper-last-shell-com)
1541 t)))
1543 (defun viper-exec-equals (m-com com)
1544 (save-excursion
1545 (set-mark viper-com-point)
1546 (viper-enlarge-region (mark t) (point))
1547 (if (> (mark t) (point)) (exchange-point-and-mark))
1548 (indent-region (mark t) (point) nil)))
1550 (defun viper-exec-shift (m-com com)
1551 (save-excursion
1552 (set-mark viper-com-point)
1553 (viper-enlarge-region (mark t) (point))
1554 (if (> (mark t) (point)) (exchange-point-and-mark))
1555 (indent-rigidly (mark t) (point)
1556 (if (viper= com ?>)
1557 viper-shift-width
1558 (- viper-shift-width))))
1559 ;; return point to where it was before shift
1560 (goto-char viper-com-point))
1562 ;; this is needed because some commands fake com by setting it to ?r, which
1563 ;; denotes repeated insert command.
1564 (defsubst viper-exec-dummy (m-com com)
1565 nil)
1567 (defun viper-exec-buffer-search (m-com com)
1568 (setq viper-s-string
1569 (regexp-quote (buffer-substring (point) viper-com-point)))
1570 (setq viper-s-forward t)
1571 (setq viper-search-history (cons viper-s-string viper-search-history))
1572 (setq viper-intermediate-command 'viper-exec-buffer-search)
1573 (viper-search viper-s-string viper-s-forward 1))
1575 (defvar viper-exec-array (make-vector 128 nil))
1577 ;; Using a dispatch array allows adding functions like buffer search
1578 ;; without affecting other functions. Buffer search can now be bound
1579 ;; to any character.
1581 (aset viper-exec-array ?c 'viper-exec-change)
1582 (aset viper-exec-array ?C 'viper-exec-Change)
1583 (aset viper-exec-array ?d 'viper-exec-delete)
1584 (aset viper-exec-array ?D 'viper-exec-Delete)
1585 (aset viper-exec-array ?y 'viper-exec-yank)
1586 (aset viper-exec-array ?Y 'viper-exec-Yank)
1587 (aset viper-exec-array ?r 'viper-exec-dummy)
1588 (aset viper-exec-array ?! 'viper-exec-bang)
1589 (aset viper-exec-array ?< 'viper-exec-shift)
1590 (aset viper-exec-array ?> 'viper-exec-shift)
1591 (aset viper-exec-array ?= 'viper-exec-equals)
1595 ;; This function is called by various movement commands to execute a
1596 ;; destructive command on the region specified by the movement command. For
1597 ;; instance, if the user types cw, then the command viper-forward-word will
1598 ;; call viper-execute-com to execute viper-exec-change, which eventually will
1599 ;; call viper-change to invoke the replace mode on the region.
1601 ;; The var viper-d-com is set to (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS)
1602 ;; via a call to viper-set-destructive-command, for later use by viper-repeat.
1603 (defun viper-execute-com (m-com val com)
1604 (let ((reg viper-use-register))
1605 ;; this is the special command `#'
1606 (if (> com 128)
1607 (viper-special-prefix-com (- com 128))
1608 (let ((fn (aref viper-exec-array com)))
1609 (if (null fn)
1610 (error "%c: %s" com viper-InvalidViCommand)
1611 (funcall fn m-com com))))
1612 (if (viper-dotable-command-p com)
1613 (viper-set-destructive-command
1614 (list m-com val com reg nil nil)))
1618 (defun viper-repeat (arg)
1619 "Re-execute last destructive command.
1620 Use the info in viper-d-com, which has the form
1621 \(com val ch reg inserted-text command-keys\),
1622 where `com' is the command to be re-executed, `val' is the
1623 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1624 if it exists, it is the name of the register for `com'.
1625 If the prefix argument ARG is non-nil, it is used instead of `val'."
1626 (interactive "P")
1627 (let ((save-point (point)) ; save point before repeating prev cmd
1628 ;; Pass along that we are repeating a destructive command
1629 ;; This tells viper-set-destructive-command not to update
1630 ;; viper-command-ring
1631 (viper-intermediate-command 'viper-repeat))
1632 (if (eq last-command 'viper-undo)
1633 ;; if the last command was viper-undo, then undo-more
1634 (viper-undo-more)
1635 ;; otherwise execute the command stored in viper-d-com. if arg is
1636 ;; non-nil its prefix value is used as new prefix value for the command.
1637 (let ((m-com (car viper-d-com))
1638 (val (viper-P-val arg))
1639 (com (nth 2 viper-d-com))
1640 (reg (nth 3 viper-d-com)))
1641 (if (null val) (setq val (nth 1 viper-d-com)))
1642 (if (null m-com) (error "No previous command to repeat"))
1643 (setq viper-use-register reg)
1644 (if (nth 4 viper-d-com) ; text inserted by command
1645 (setq viper-last-insertion (nth 4 viper-d-com)
1646 viper-d-char (nth 4 viper-d-com)))
1647 (funcall m-com (cons val com))
1648 (cond ((and (< save-point (point)) viper-keep-point-on-repeat)
1649 (goto-char save-point)) ; go back to before repeat.
1650 ((and (< save-point (point)) viper-ex-style-editing)
1651 (or (bolp) (backward-char 1))))
1652 (if (and (eolp) (not (bolp)))
1653 (backward-char 1))
1655 (viper-adjust-undo) ; take care of undo
1656 ;; If the prev cmd was rotating the command ring, this means that `.' has
1657 ;; just executed a command from that ring. So, push it on the ring again.
1658 ;; If we are just executing previous command , then don't push viper-d-com
1659 ;; because viper-d-com is not fully constructed in this case (its keys and
1660 ;; the inserted text may be nil). Besides, in this case, the command
1661 ;; executed by `.' is already on the ring.
1662 (if (eq last-command 'viper-display-current-destructive-command)
1663 (viper-push-onto-ring viper-d-com 'viper-command-ring))
1664 (viper-deactivate-mark)
1667 (defun viper-repeat-from-history ()
1668 "Repeat a destructive command from history.
1669 Doesn't change viper-command-ring in any way, so `.' will work as before
1670 executing this command.
1671 This command is supposed to be bound to a two-character Vi macro where
1672 the second character is a digit 0 to 9. The digit indicates which
1673 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1674 invokes the command before that, etc."
1675 (interactive)
1676 (let* ((viper-intermediate-command 'repeating-display-destructive-command)
1677 (idx (cond (viper-this-kbd-macro
1678 (string-to-number
1679 (symbol-name (elt viper-this-kbd-macro 1))))
1680 (t 0)))
1681 (num idx)
1682 (viper-d-com viper-d-com))
1684 (or (and (numberp num) (<= 0 num) (<= num 9))
1685 (progn
1686 (setq idx 0
1687 num 0)
1688 (message
1689 "`viper-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1690 (while (< 0 num)
1691 (setq viper-d-com (viper-special-ring-rotate1 viper-command-ring -1))
1692 (setq num (1- num)))
1693 (viper-repeat nil)
1694 (while (> idx num)
1695 (viper-special-ring-rotate1 viper-command-ring 1)
1696 (setq num (1+ num)))
1700 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1701 ;; The chars that can follow `#' are determined by viper-hash-command-p
1702 (defun viper-special-prefix-com (char)
1703 (cond ((viper= char ?c)
1704 (downcase-region (min viper-com-point (point))
1705 (max viper-com-point (point))))
1706 ((viper= char ?C)
1707 (upcase-region (min viper-com-point (point))
1708 (max viper-com-point (point))))
1709 ((viper= char ?g)
1710 (push-mark viper-com-point t)
1711 ;; execute the last emacs kbd macro on each line of the region
1712 (viper-global-execute))
1713 ((viper= char ?q)
1714 (push-mark viper-com-point t)
1715 (viper-quote-region))
1716 ((viper= char ?s)
1717 (funcall viper-spell-function viper-com-point (point)))
1718 (t (error "#%c: %s" char viper-InvalidViCommand))))
1721 ;; undoing
1723 ;; hook used inside undo
1724 (defvar viper-undo-functions nil)
1726 ;; Runs viper-before-change-functions inside before-change-functions
1727 (defun viper-undo-sentinel (beg end length)
1728 (run-hook-with-args 'viper-undo-functions beg end length))
1730 (add-hook 'after-change-functions 'viper-undo-sentinel)
1732 ;; Hook used in viper-undo
1733 (defun viper-after-change-undo-hook (beg end len)
1734 (if (and (boundp 'undo-in-progress) undo-in-progress)
1735 (setq undo-beg-posn beg
1736 undo-end-posn (or end beg))
1737 ;; some other hooks may be changing various text properties in
1738 ;; the buffer in response to 'undo'; so remove this hook to avoid
1739 ;; its repeated invocation
1740 (remove-hook 'viper-undo-functions 'viper-after-change-undo-hook 'local)
1743 (defun viper-undo ()
1744 "Undo previous change."
1745 (interactive)
1746 (message "undo!")
1747 (let ((modified (buffer-modified-p))
1748 (before-undo-pt (point-marker))
1749 undo-beg-posn undo-end-posn)
1751 ;; the viper-after-change-undo-hook removes itself after the 1st invocation
1752 (add-hook 'viper-undo-functions 'viper-after-change-undo-hook nil 'local)
1754 (undo-start)
1755 (undo-more 2)
1756 ;;(setq undo-beg-posn (or undo-beg-posn (point))
1757 ;; undo-end-posn (or undo-end-posn (point)))
1758 ;;(setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1759 ;; undo-end-posn (or undo-end-posn undo-beg-posn))
1761 (if (and undo-beg-posn undo-end-posn)
1762 (progn
1763 (goto-char undo-beg-posn)
1764 (sit-for 0)
1765 (if (and viper-keep-point-on-undo
1766 (pos-visible-in-window-p before-undo-pt))
1767 (progn
1768 (push-mark (point-marker) t)
1769 (viper-sit-for-short 300)
1770 (goto-char undo-end-posn)
1771 (viper-sit-for-short 300)
1772 (if (pos-visible-in-window-p undo-beg-posn)
1773 (goto-char before-undo-pt)
1774 (goto-char undo-beg-posn)))
1775 (push-mark before-undo-pt t))
1778 (if (and (eolp) (not (bolp))) (backward-char 1))
1780 (setq this-command 'viper-undo))
1782 ;; Continue undoing previous changes.
1783 (defun viper-undo-more ()
1784 (message "undo more!")
1785 (condition-case nil
1786 (undo-more 1)
1787 (error (beep)
1788 (message "No further undo information in this buffer")))
1789 (if (and (eolp) (not (bolp))) (backward-char 1))
1790 (setq this-command 'viper-undo))
1792 ;; The following two functions are used to set up undo properly.
1793 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1794 ;; they are undone all at once.
1795 (defun viper-adjust-undo ()
1796 (if viper-undo-needs-adjustment
1797 (let ((inhibit-quit t)
1798 tmp tmp2)
1799 (setq viper-undo-needs-adjustment nil)
1800 (if (listp buffer-undo-list)
1801 (if (setq tmp (memq viper-buffer-undo-list-mark buffer-undo-list))
1802 (progn
1803 (setq tmp2 (cdr tmp)) ; the part after mark
1805 ;; cut tail from buffer-undo-list temporarily by direct
1806 ;; manipulation with pointers in buffer-undo-list
1807 (setcdr tmp nil)
1809 (setq buffer-undo-list (delq nil buffer-undo-list))
1810 (setq buffer-undo-list
1811 (delq viper-buffer-undo-list-mark buffer-undo-list))
1812 ;; restore tail of buffer-undo-list
1813 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1814 (setq buffer-undo-list (delq nil buffer-undo-list)))))
1818 (defun viper-set-complex-command-for-undo ()
1819 (if (listp buffer-undo-list)
1820 (if (not viper-undo-needs-adjustment)
1821 (let ((inhibit-quit t))
1822 (setq buffer-undo-list
1823 (cons viper-buffer-undo-list-mark buffer-undo-list))
1824 (setq viper-undo-needs-adjustment t)))))
1827 ;;; Viper's destructive Command ring utilities
1829 (defun viper-display-current-destructive-command ()
1830 (let ((text (nth 4 viper-d-com))
1831 (keys (nth 5 viper-d-com))
1832 (max-text-len 30))
1834 (setq this-command 'viper-display-current-destructive-command)
1836 (message " `.' runs %s%s"
1837 (concat "`" (viper-array-to-string keys) "'")
1838 (viper-abbreviate-string
1839 (if (featurep 'xemacs)
1840 (replace-in-string ; xemacs
1841 (cond ((characterp text) (char-to-string text))
1842 ((stringp text) text)
1843 (t ""))
1844 "\n" "^J")
1845 text ; emacs
1847 max-text-len
1848 " inserting `" "'" " ......."))
1852 ;; don't change viper-d-com if it was viper-repeat command invoked with `.'
1853 ;; or in some other way (non-interactively).
1854 (defun viper-set-destructive-command (list)
1855 (or (eq viper-intermediate-command 'viper-repeat)
1856 (progn
1857 (setq viper-d-com list)
1858 (setcar (nthcdr 5 viper-d-com)
1859 (viper-array-to-string (if (arrayp viper-this-command-keys)
1860 viper-this-command-keys
1861 (this-command-keys))))
1862 (viper-push-onto-ring viper-d-com 'viper-command-ring)))
1863 (setq viper-this-command-keys nil))
1866 (defun viper-prev-destructive-command (next)
1867 "Find previous destructive command in the history of destructive commands.
1868 With prefix argument, find next destructive command."
1869 (interactive "P")
1870 (let (cmd viper-intermediate-command)
1871 (if (eq last-command 'viper-display-current-destructive-command)
1872 ;; repeated search through command history
1873 (setq viper-intermediate-command
1874 'repeating-display-destructive-command)
1875 ;; first search through command history--set temp ring
1876 (setq viper-temp-command-ring (ring-copy viper-command-ring)))
1877 (setq cmd (if next
1878 (viper-special-ring-rotate1 viper-temp-command-ring 1)
1879 (viper-special-ring-rotate1 viper-temp-command-ring -1)))
1880 (if (null cmd)
1882 (setq viper-d-com cmd))
1883 (viper-display-current-destructive-command)))
1886 (defun viper-next-destructive-command ()
1887 "Find next destructive command in the history of destructive commands."
1888 (interactive)
1889 (viper-prev-destructive-command 'next))
1892 (defun viper-insert-prev-from-insertion-ring (arg)
1893 "Cycle through insertion ring in the direction of older insertions.
1894 Undoes previous insertion and inserts new.
1895 With prefix argument, cycles in the direction of newer elements.
1896 In minibuffer, this command executes whatever the invocation key is bound
1897 to in the global map, instead of cycling through the insertion ring."
1898 (interactive "P")
1899 (let (viper-intermediate-command)
1900 (if (eq last-command 'viper-insert-from-insertion-ring)
1901 (progn ; repeated search through insertion history
1902 (setq viper-intermediate-command 'repeating-insertion-from-ring)
1903 (if (eq viper-current-state 'replace-state)
1904 (undo 1)
1905 (if viper-last-inserted-string-from-insertion-ring
1906 (backward-delete-char
1907 (length viper-last-inserted-string-from-insertion-ring))))
1909 ;;first search through insertion history
1910 (setq viper-temp-insertion-ring (ring-copy viper-insertion-ring)))
1911 (setq this-command 'viper-insert-from-insertion-ring)
1912 ;; so that things will be undone properly
1913 (setq buffer-undo-list (cons nil buffer-undo-list))
1914 (setq viper-last-inserted-string-from-insertion-ring
1915 (viper-special-ring-rotate1 viper-temp-insertion-ring (if arg 1 -1)))
1917 ;; this change of viper-intermediate-command must come after
1918 ;; viper-special-ring-rotate1, so that the ring will rotate, but before the
1919 ;; insertion.
1920 (setq viper-intermediate-command nil)
1921 (if viper-last-inserted-string-from-insertion-ring
1922 (insert viper-last-inserted-string-from-insertion-ring))
1925 (defun viper-insert-next-from-insertion-ring ()
1926 "Cycle through insertion ring in the direction of older insertions.
1927 Undo previous insertion and inserts new."
1928 (interactive)
1929 (viper-insert-prev-from-insertion-ring 'next))
1933 ;; some region utilities
1935 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1936 (defun viper-add-newline-at-eob-if-necessary ()
1937 (save-excursion
1938 (end-of-line)
1939 ;; make sure all lines end with newline, unless in the minibuffer or
1940 ;; when requested otherwise (require-final-newline is nil)
1941 (save-restriction
1942 (widen)
1943 (if (and (eobp)
1944 (not (bolp))
1945 require-final-newline
1946 ;; add newline only if we actually edited buffer. otherwise it
1947 ;; might unintentionally modify binary buffers
1948 (buffer-modified-p)
1949 (not (viper-is-in-minibuffer))
1950 (not buffer-read-only))
1951 ;; text property may be read-only
1952 (condition-case nil
1953 (insert "\n")
1954 (error nil))
1958 (defun viper-yank-defun ()
1959 (mark-defun)
1960 (copy-region-as-kill (point) (mark t)))
1962 ;; Enlarge region between BEG and END.
1963 (defun viper-enlarge-region (beg end)
1964 (or beg (setq beg end)) ; if beg is nil, set to end
1965 (or end (setq end beg)) ; if end is nil, set to beg
1967 (if (< beg end)
1968 (progn (goto-char beg) (set-mark end))
1969 (goto-char end)
1970 (set-mark beg))
1971 (beginning-of-line)
1972 (exchange-point-and-mark)
1973 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1974 (if (not (eobp)) (beginning-of-line))
1975 (if (> beg end) (exchange-point-and-mark)))
1978 ;; Quote region by each line with a user supplied string.
1979 (defun viper-quote-region ()
1980 (let ((quote-str viper-quote-string)
1981 (donot-change-dafault t))
1982 (setq quote-str
1983 (viper-read-string-with-history
1984 "Quote string: "
1986 'viper-quote-region-history
1987 (cond ((string-match "tex.*-mode" (symbol-name major-mode)) "%%")
1988 ((string-match "java.*-mode" (symbol-name major-mode)) "//")
1989 ((string-match "perl.*-mode" (symbol-name major-mode)) "#")
1990 ((string-match "lisp.*-mode" (symbol-name major-mode)) ";;")
1991 ((memq major-mode '(c-mode cc-mode c++-mode)) "//")
1992 ((memq major-mode '(sh-mode shell-mode)) "#")
1993 (t (setq donot-change-dafault nil)
1994 quote-str))))
1995 (or donot-change-dafault
1996 (setq viper-quote-string quote-str))
1997 (viper-enlarge-region (point) (mark t))
1998 (if (> (point) (mark t)) (exchange-point-and-mark))
1999 (insert quote-str)
2000 (beginning-of-line)
2001 (forward-line 1)
2002 (while (and (< (point) (mark t)) (bolp))
2003 (insert quote-str)
2004 (beginning-of-line)
2005 (forward-line 1))))
2007 ;; Tells whether BEG is on the same line as END.
2008 ;; If one of the args is nil, it'll return nil.
2009 (defun viper-same-line (beg end)
2010 (let ((selective-display nil)
2011 (incr 0)
2012 temp)
2013 (if (and beg end (> beg end))
2014 (setq temp beg
2015 beg end
2016 end temp))
2017 (if (and beg end)
2018 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
2019 nil)
2021 ;; This 'if' is needed because Emacs treats the next empty line
2022 ;; as part of the previous line.
2023 (if (= (viper-line-pos 'start) end)
2024 (setq incr 1))
2025 (<= (+ incr (count-lines beg end)) 1))))
2029 ;; Check if the string ends with a newline.
2030 (defun viper-end-with-a-newline-p (string)
2031 (or (string= string "")
2032 (= (viper-seq-last-elt string) ?\n)))
2034 (defun viper-tmp-insert-at-eob (msg)
2035 (let ((savemax (point-max)))
2036 (goto-char savemax)
2037 (insert msg)
2038 (sit-for 2)
2039 (goto-char savemax) (delete-region (point) (point-max))
2044 ;;; Minibuffer business
2046 (defsubst viper-set-minibuffer-style ()
2047 (add-hook 'minibuffer-setup-hook 'viper-minibuffer-setup-sentinel)
2048 (add-hook 'post-command-hook 'viper-minibuffer-post-command-hook))
2051 (defun viper-minibuffer-setup-sentinel ()
2052 (let ((hook (if viper-vi-style-in-minibuffer
2053 'viper-change-state-to-insert
2054 'viper-change-state-to-emacs)))
2055 ;; making buffer-local variables so that normal buffers won't affect the
2056 ;; minibuffer and vice versa. Otherwise, command arguments will affect
2057 ;; minibuffer ops and insertions from the minibuffer will change those in
2058 ;; the normal buffers
2059 (make-local-variable 'viper-d-com)
2060 (make-local-variable 'viper-last-insertion)
2061 (make-local-variable 'viper-command-ring)
2062 (setq viper-d-com nil
2063 viper-last-insertion nil
2064 viper-command-ring nil)
2065 (funcall hook)
2068 ;; Thie is a temp hook that uses free variables init-message and initial.
2069 ;; A dirty feature, but it is the simplest way to have it do the right thing.
2070 ;; The INIT-MESSAGE and INITIAL vars come from the scope set by
2071 ;; viper-read-string-with-history
2072 (defun viper-minibuffer-standard-hook ()
2073 (if (stringp init-message)
2074 (viper-tmp-insert-at-eob init-message))
2075 (if (stringp initial)
2076 (progn
2077 ;; don't wait if we have unread events or in kbd macro
2078 (or unread-command-events
2079 executing-kbd-macro
2080 (sit-for 840))
2081 (if (fboundp 'minibuffer-prompt-end)
2082 (delete-region (minibuffer-prompt-end) (point-max))
2083 (erase-buffer))
2084 (insert initial))))
2086 (defsubst viper-minibuffer-real-start ()
2087 (if (fboundp 'minibuffer-prompt-end)
2088 (minibuffer-prompt-end)
2089 (point-min)))
2091 (defun viper-minibuffer-post-command-hook()
2092 (when (active-minibuffer-window)
2093 (when (< (point) (viper-minibuffer-real-start))
2094 (goto-char (viper-minibuffer-real-start)))))
2097 ;; Interpret last event in the local map first; if fails, use exit-minibuffer.
2098 ;; Run viper-minibuffer-exit-hook before exiting.
2099 (defun viper-exit-minibuffer ()
2100 "Exit minibuffer Viper way."
2101 (interactive)
2102 (let (command)
2103 (setq command (local-key-binding (char-to-string (viper-last-command-char))))
2104 (run-hooks 'viper-minibuffer-exit-hook)
2105 (if command
2106 (command-execute command)
2107 (exit-minibuffer))))
2110 (defcustom viper-smart-suffix-list
2111 '("" "tex" "c" "cc" "C" "java" "el" "html" "htm" "xml"
2112 "pl" "flr" "P" "p" "h" "H")
2113 "*List of suffixes that Viper tries to append to filenames ending with a `.'.
2114 This is useful when the current directory contains files with the same
2115 prefix and many different suffixes. Usually, only one of the suffixes
2116 represents an editable file. However, file completion will stop at the `.'
2117 The smart suffix feature lets you hit RET in such a case, and Viper will
2118 select the appropriate suffix.
2120 Suffixes are tried in the order given and the first suffix for which a
2121 corresponding file exists is selected. If no file exists for any of the
2122 suffixes, the user is asked to confirm.
2124 To turn this feature off, set this variable to nil."
2125 :type '(repeat string)
2126 :group 'viper-misc)
2129 ;; Try to add a suitable suffix to files whose name ends with a `.'
2130 ;; Useful when the user hits RET on a non-completed file name.
2131 ;; Used as a minibuffer exit hook in read-file-name
2132 (defun viper-file-add-suffix ()
2133 (let ((count 0)
2134 (len (length viper-smart-suffix-list))
2135 (file (buffer-substring-no-properties
2136 (viper-minibuffer-real-start) (point-max)))
2137 found key cmd suff)
2138 (goto-char (point-max))
2139 (if (and viper-smart-suffix-list (string-match "\\.$" file))
2140 (progn
2141 (while (and (not found) (< count len))
2142 (setq suff (nth count viper-smart-suffix-list)
2143 count (1+ count))
2144 (if (file-exists-p
2145 (format "%s%s" (substitute-in-file-name file) suff))
2146 (progn
2147 (setq found t)
2148 (insert suff))))
2150 (if found
2152 (viper-tmp-insert-at-eob " [Please complete file name]")
2153 (unwind-protect
2154 (while (not (memq cmd
2155 '(exit-minibuffer viper-exit-minibuffer)))
2156 (setq cmd
2157 (key-binding (setq key (read-key-sequence nil))))
2158 (cond ((eq cmd 'self-insert-command)
2159 (if (featurep 'xemacs)
2160 (insert (events-to-keys key)) ; xemacs
2161 (insert key) ; emacs
2163 ((memq cmd '(exit-minibuffer viper-exit-minibuffer))
2164 nil)
2165 (t (command-execute cmd)))
2167 ))))
2170 (defun viper-minibuffer-trim-tail ()
2171 "Delete junk at the end of the first line of the minibuffer input.
2172 Remove this function from `viper-minibuffer-exit-hook', if this causes
2173 problems."
2174 (if (viper-is-in-minibuffer)
2175 (let ((inhibit-field-text-motion t))
2176 (goto-char (viper-minibuffer-real-start))
2177 (end-of-line)
2178 (delete-region (point) (point-max)))))
2181 ;;; Reading string with history
2183 (defun viper-read-string-with-history (prompt &optional initial
2184 history-var default keymap
2185 init-message)
2186 ;; Read string, prompting with PROMPT and inserting the INITIAL
2187 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
2188 ;; input is an empty string.
2189 ;; Default value is displayed until the user types something in the
2190 ;; minibuffer.
2191 ;; KEYMAP is used, if given, instead of minibuffer-local-map.
2192 ;; INIT-MESSAGE is the message temporarily displayed after entering the
2193 ;; minibuffer.
2194 (let ((minibuffer-setup-hook
2195 ;; stolen from add-hook
2196 (let ((old
2197 (if (boundp 'minibuffer-setup-hook)
2198 minibuffer-setup-hook
2199 nil)))
2200 (cons
2201 'viper-minibuffer-standard-hook
2202 (if (or (not (listp old)) (eq (car old) 'lambda))
2203 (list old) old))))
2204 (val "")
2205 (padding "")
2206 temp-msg)
2208 (setq keymap (or keymap minibuffer-local-map)
2209 initial (or initial "")
2210 temp-msg (if default
2211 (format "(default %s) " default)
2212 ""))
2214 (setq viper-incomplete-ex-cmd nil)
2215 (setq val (read-from-minibuffer prompt
2216 (concat temp-msg initial val padding)
2217 keymap nil history-var))
2218 (setq minibuffer-setup-hook nil
2219 padding (viper-array-to-string (this-command-keys))
2220 temp-msg "")
2221 ;; the following tries to be smart about what to put in history
2222 (if (not (string= val (car (eval history-var))))
2223 (set history-var (cons val (eval history-var))))
2224 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2225 (string= (nth 0 (eval history-var)) ""))
2226 (set history-var (cdr (eval history-var))))
2227 ;; If the user enters nothing but the prev cmd wasn't viper-ex,
2228 ;; viper-command-argument, or `! shell-command', this probably means
2229 ;; that the user typed something then erased. Return "" in this case, not
2230 ;; the default---the default is too confusing in this case.
2231 (cond ((and (string= val "")
2232 (not (string= prompt "!")) ; was a `! shell-command'
2233 (not (memq last-command
2234 '(viper-ex
2235 viper-command-argument
2239 ((string= val "") (or default ""))
2240 (t val))
2245 ;; insertion commands
2247 ;; Called when state changes from Insert Vi command mode.
2248 ;; Repeats the insertion command if Insert state was entered with prefix
2249 ;; argument > 1.
2250 (defun viper-repeat-insert-command ()
2251 (let ((i-com (car viper-d-com))
2252 (val (nth 1 viper-d-com))
2253 (char (nth 2 viper-d-com)))
2254 (if (and val (> val 1)) ; first check that val is non-nil
2255 (progn
2256 (setq viper-d-com (list i-com (1- val) ?r nil nil nil))
2257 (viper-repeat nil)
2258 (setq viper-d-com (list i-com val char nil nil nil))
2259 ))))
2261 (defun viper-insert (arg)
2262 "Insert before point."
2263 (interactive "P")
2264 (viper-set-complex-command-for-undo)
2265 (let ((val (viper-p-val arg))
2266 ;;(com (viper-getcom arg))
2268 (viper-set-destructive-command (list 'viper-insert val ?r nil nil nil))
2269 (if (eq viper-intermediate-command 'viper-repeat)
2270 (viper-loop val (viper-yank-last-insertion))
2271 (viper-change-state-to-insert))))
2273 (defun viper-append (arg)
2274 "Append after 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-append val ?r nil nil nil))
2281 (if (not (eolp)) (forward-char))
2282 (if (eq viper-intermediate-command 'viper-repeat)
2283 (viper-loop val (viper-yank-last-insertion))
2284 (viper-change-state-to-insert))))
2286 (defun viper-Append (arg)
2287 "Append at end of line."
2288 (interactive "P")
2289 (viper-set-complex-command-for-undo)
2290 (let ((val (viper-p-val arg))
2291 ;;(com (viper-getcom arg))
2293 (viper-set-destructive-command (list 'viper-Append val ?r nil nil nil))
2294 (end-of-line)
2295 (if (eq viper-intermediate-command 'viper-repeat)
2296 (viper-loop val (viper-yank-last-insertion))
2297 (viper-change-state-to-insert))))
2299 (defun viper-Insert (arg)
2300 "Insert before first non-white."
2301 (interactive "P")
2302 (viper-set-complex-command-for-undo)
2303 (let ((val (viper-p-val arg))
2304 ;;(com (viper-getcom arg))
2306 (viper-set-destructive-command (list 'viper-Insert val ?r nil nil nil))
2307 (back-to-indentation)
2308 (if (eq viper-intermediate-command 'viper-repeat)
2309 (viper-loop val (viper-yank-last-insertion))
2310 (viper-change-state-to-insert))))
2312 (defun viper-open-line (arg)
2313 "Open line below."
2314 (interactive "P")
2315 (viper-set-complex-command-for-undo)
2316 (let ((val (viper-p-val arg))
2317 ;;(com (viper-getcom arg))
2319 (viper-set-destructive-command (list 'viper-open-line val ?r nil nil nil))
2320 (let ((col (current-indentation)))
2321 (if (eq viper-intermediate-command 'viper-repeat)
2322 (viper-loop val
2323 (end-of-line)
2324 (newline 1)
2325 (viper-indent-line col)
2326 (viper-yank-last-insertion))
2327 (end-of-line)
2328 (newline 1)
2329 (viper-indent-line col)
2330 (viper-change-state-to-insert)))))
2332 (defun viper-Open-line (arg)
2333 "Open line above."
2334 (interactive "P")
2335 (viper-set-complex-command-for-undo)
2336 (let ((val (viper-p-val arg))
2337 ;;(com (viper-getcom arg))
2339 (viper-set-destructive-command (list 'viper-Open-line val ?r nil nil nil))
2340 (let ((col (current-indentation)))
2341 (if (eq viper-intermediate-command 'viper-repeat)
2342 (viper-loop val
2343 (beginning-of-line)
2344 (open-line 1)
2345 (viper-indent-line col)
2346 (viper-yank-last-insertion))
2347 (beginning-of-line)
2348 (open-line 1)
2349 (viper-indent-line col)
2350 (viper-change-state-to-insert)))))
2352 (defun viper-open-line-at-point (arg)
2353 "Open line at point."
2354 (interactive "P")
2355 (viper-set-complex-command-for-undo)
2356 (let ((val (viper-p-val arg))
2357 ;;(com (viper-getcom arg))
2359 (viper-set-destructive-command
2360 (list 'viper-open-line-at-point val ?r nil nil nil))
2361 (if (eq viper-intermediate-command 'viper-repeat)
2362 (viper-loop val
2363 (open-line 1)
2364 (viper-yank-last-insertion))
2365 (open-line 1)
2366 (viper-change-state-to-insert))))
2368 ;; bound to s
2369 (defun viper-substitute (arg)
2370 "Substitute characters."
2371 (interactive "P")
2372 (let ((val (viper-p-val arg))
2373 ;;(com (viper-getcom arg))
2375 (push-mark nil t)
2376 (forward-char val)
2377 (if (eq viper-intermediate-command 'viper-repeat)
2378 (viper-change-subr (mark t) (point))
2379 (viper-change (mark t) (point)))
2380 ;; com is set to ?r when we repeat this comand with dot
2381 (viper-set-destructive-command (list 'viper-substitute val ?r nil nil nil))
2384 ;; Command bound to S
2385 (defun viper-substitute-line (arg)
2386 "Substitute lines."
2387 (interactive "p")
2388 (viper-set-complex-command-for-undo)
2389 (viper-line (cons arg ?C)))
2391 ;; Prepare for replace
2392 (defun viper-start-replace ()
2393 (setq viper-began-as-replace t
2394 viper-sitting-in-replace t
2395 viper-replace-chars-to-delete 0)
2396 (add-hook
2397 'viper-after-change-functions 'viper-replace-mode-spy-after t 'local)
2398 (add-hook
2399 'viper-before-change-functions 'viper-replace-mode-spy-before t 'local)
2400 ;; this will get added repeatedly, but no harm
2401 (add-hook 'after-change-functions 'viper-after-change-sentinel t)
2402 (add-hook 'before-change-functions 'viper-before-change-sentinel t)
2403 (viper-move-marker-locally
2404 'viper-last-posn-in-replace-region (viper-replace-start))
2405 (add-hook
2406 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel
2407 t 'local)
2408 (add-hook
2409 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2410 ;; guard against a smartie who switched from R-replace to normal replace
2411 (remove-hook
2412 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2413 (if overwrite-mode (overwrite-mode -1))
2417 (defun viper-replace-mode-spy-before (beg end)
2418 (setq viper-replace-region-chars-deleted (viper-chars-in-region beg end))
2421 ;; Invoked as an after-change-function to calculate how many chars have to be
2422 ;; deleted. This function may be called several times within a single command,
2423 ;; if this command performs several separate buffer changes. Therefore, if
2424 ;; adds up the number of chars inserted and subtracts the number of chars
2425 ;; deleted.
2426 (defun viper-replace-mode-spy-after (beg end length)
2427 (if (memq viper-intermediate-command
2428 '(dabbrev-expand hippie-expand repeating-insertion-from-ring))
2429 ;; Take special care of text insertion from insertion ring inside
2430 ;; replacement overlays.
2431 (progn
2432 (setq viper-replace-chars-to-delete 0)
2433 (viper-move-marker-locally
2434 'viper-last-posn-in-replace-region (point)))
2436 (let* ((real-end (min end (viper-replace-end)))
2437 (column-shift (- (save-excursion (goto-char real-end)
2438 (current-column))
2439 (save-excursion (goto-char beg)
2440 (current-column))))
2441 (chars-deleted 0))
2443 (if (> length 0)
2444 (setq chars-deleted viper-replace-region-chars-deleted))
2445 (setq viper-replace-region-chars-deleted 0)
2446 (setq viper-replace-chars-to-delete
2447 (+ viper-replace-chars-to-delete
2449 ;; if column shift is bigger, due to a TAB insertion, take
2450 ;; column-shift instead of the number of inserted chars
2451 (max (viper-chars-in-region beg real-end)
2452 ;; This test accounts for Chinese/Japanese/... chars,
2453 ;; which occupy 2 columns instead of one. If we use
2454 ;; column-shift here, we may delete two chars instead of
2455 ;; one when the user types one Chinese character.
2456 ;; Deleting two would be OK, if they were European chars,
2457 ;; but it is not OK if they are Chinese chars.
2458 ;; Since it is hard to
2459 ;; figure out which characters are being deleted in any
2460 ;; given region, we decided to treat Eastern and European
2461 ;; characters equally, even though Eastern chars may
2462 ;; occupy more columns.
2463 (if (memq this-command '(self-insert-command
2464 quoted-insert viper-insert-tab))
2465 column-shift
2467 ;; the number of deleted chars
2468 chars-deleted)))
2470 (viper-move-marker-locally
2471 'viper-last-posn-in-replace-region
2472 (max (if (> end (viper-replace-end)) (viper-replace-end) end)
2473 (or (marker-position viper-last-posn-in-replace-region)
2474 (viper-replace-start))
2480 ;; Delete stuff between viper-last-posn-in-replace-region and the end of
2481 ;; viper-replace-overlay-marker, if viper-last-posn-in-replace-region is within
2482 ;; the overlay and current point is before the end of the overlay.
2483 ;; Don't delete anything if current point is past the end of the overlay.
2484 (defun viper-finish-change ()
2485 (remove-hook
2486 'viper-after-change-functions 'viper-replace-mode-spy-after 'local)
2487 (remove-hook
2488 'viper-before-change-functions 'viper-replace-mode-spy-before 'local)
2489 (remove-hook
2490 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2491 (remove-hook
2492 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2493 (viper-restore-cursor-color 'after-replace-mode)
2494 (setq viper-sitting-in-replace nil) ; just in case we'll need to know it
2495 (save-excursion
2496 (if (and viper-replace-overlay
2497 (viper-pos-within-region viper-last-posn-in-replace-region
2498 (viper-replace-start)
2499 (viper-replace-end))
2500 (< (point) (viper-replace-end)))
2501 (delete-region
2502 viper-last-posn-in-replace-region (viper-replace-end))))
2504 (if (eq viper-current-state 'replace-state)
2505 (viper-downgrade-to-insert))
2506 ;; replace mode ended => nullify viper-last-posn-in-replace-region
2507 (viper-move-marker-locally 'viper-last-posn-in-replace-region nil)
2508 (viper-hide-replace-overlay)
2509 (viper-refresh-mode-line)
2510 (viper-put-string-on-kill-ring viper-last-replace-region)
2513 ;; Make STRING be the first element of the kill ring.
2514 (defun viper-put-string-on-kill-ring (string)
2515 (setq kill-ring (cons string kill-ring))
2516 (if (> (length kill-ring) kill-ring-max)
2517 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2518 (setq kill-ring-yank-pointer kill-ring))
2520 (defun viper-finish-R-mode ()
2521 (remove-hook
2522 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2523 (remove-hook
2524 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2525 (viper-downgrade-to-insert))
2527 (defun viper-start-R-mode ()
2528 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2529 (overwrite-mode 1)
2530 (add-hook
2531 'viper-post-command-hooks 'viper-R-state-post-command-sentinel t 'local)
2532 (add-hook
2533 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2534 ;; guard against a smartie who switched from R-replace to normal replace
2535 (remove-hook
2536 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2541 (defun viper-replace-state-exit-cmd ()
2542 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2543 These keys are ESC, RET, and LineFeed."
2544 (interactive)
2545 (if overwrite-mode ; if in replace mode invoked via 'R'
2546 (viper-finish-R-mode)
2547 (viper-finish-change))
2548 (let (com)
2549 (if (eq this-command 'viper-intercept-ESC-key)
2550 (setq com 'viper-exit-insert-state)
2551 (viper-set-unread-command-events last-input-event)
2552 (setq com (key-binding (viper-read-key-sequence nil))))
2554 (condition-case conds
2555 (command-execute com)
2556 (error
2557 (viper-message-conditions conds)))
2559 (viper-hide-replace-overlay))
2562 (defun viper-replace-state-carriage-return ()
2563 "Carriage return in Viper replace state."
2564 (interactive)
2565 ;; If Emacs start supporting overlay maps, as it currently supports
2566 ;; text-property maps, we could do away with viper-replace-minor-mode and
2567 ;; just have keymap attached to replace overlay. Then the "if part" of this
2568 ;; statement can be deleted.
2569 (if (or (< (point) (viper-replace-start))
2570 (> (point) (viper-replace-end)))
2571 (let (viper-replace-minor-mode com)
2572 (viper-set-unread-command-events last-input-event)
2573 (setq com (key-binding (read-key-sequence nil)))
2574 (condition-case conds
2575 (command-execute com)
2576 (error
2577 (viper-message-conditions conds))))
2578 (if (not viper-allow-multiline-replace-regions)
2579 (viper-replace-state-exit-cmd)
2580 (if (viper-same-line (point) (viper-replace-end))
2581 (viper-replace-state-exit-cmd)
2582 ;; delete the rest of line
2583 (delete-region (point) (viper-line-pos 'end))
2584 (save-excursion
2585 (end-of-line)
2586 (if (eobp) (error "Last line in buffer")))
2587 ;; skip to the next line
2588 (forward-line 1)
2589 (back-to-indentation)
2590 ))))
2593 ;; This is the function bound to 'R'---unlimited replace.
2594 ;; Similar to Emacs's own overwrite-mode.
2595 (defun viper-overwrite (arg)
2596 "Begin overwrite mode."
2597 (interactive "P")
2598 (let ((val (viper-p-val arg))
2599 ;;(com (viper-getcom arg))
2600 (len))
2601 (viper-set-destructive-command (list 'viper-overwrite val ?r nil nil nil))
2602 (if (eq viper-intermediate-command 'viper-repeat)
2603 (progn
2604 ;; Viper saves inserted text in viper-last-insertion
2605 (setq len (length viper-last-insertion))
2606 (delete-char (min len (- (point-max) (point) 1)))
2607 (viper-loop val (viper-yank-last-insertion)))
2608 (setq last-command 'viper-overwrite)
2609 (viper-set-complex-command-for-undo)
2610 (viper-set-replace-overlay (point) (viper-line-pos 'end))
2611 (viper-change-state-to-replace)
2615 ;; line commands
2617 (defun viper-line (arg)
2618 (let ((val (car arg))
2619 (com (cdr arg)))
2620 (viper-move-marker-locally 'viper-com-point (point))
2621 (if (not (eobp))
2622 (viper-next-line-carefully (1- val)))
2623 ;; the following ensures that dd, cc, D, yy will do the right thing on the
2624 ;; last line of buffer when this line has no \n.
2625 (viper-add-newline-at-eob-if-necessary)
2626 (viper-execute-com 'viper-line val com))
2627 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2630 (defun viper-yank-line (arg)
2631 "Yank ARG lines (in Vi's sense)."
2632 (interactive "P")
2633 (let ((val (viper-p-val arg)))
2634 (viper-line (cons val ?Y))))
2637 ;; region commands
2639 (defun viper-region (arg)
2640 "Execute command on a region."
2641 (interactive "P")
2642 (let ((val (viper-P-val arg))
2643 (com (viper-getcom arg)))
2644 (viper-move-marker-locally 'viper-com-point (point))
2645 (exchange-point-and-mark)
2646 (viper-execute-com 'viper-region val com)))
2648 (defun viper-Region (arg)
2649 "Execute command on a Region."
2650 (interactive "P")
2651 (let ((val (viper-P-val arg))
2652 (com (viper-getCom arg)))
2653 (viper-move-marker-locally 'viper-com-point (point))
2654 (exchange-point-and-mark)
2655 (viper-execute-com 'viper-Region val com)))
2657 (defun viper-replace-char (arg)
2658 "Replace the following ARG chars by the character read."
2659 (interactive "P")
2660 (if (and (eolp) (bolp)) (error "No character to replace here"))
2661 (let ((val (viper-p-val arg))
2662 (com (viper-getcom arg)))
2663 (viper-replace-char-subr com val)
2664 (if (and (eolp) (not (bolp))) (forward-char 1))
2665 (setq viper-this-command-keys
2666 (format "%sr" (if (integerp arg) arg "")))
2667 (viper-set-destructive-command
2668 (list 'viper-replace-char val ?r nil viper-d-char nil))
2671 (defun viper-replace-char-subr (com arg)
2672 (let ((inhibit-quit t)
2673 char)
2674 (viper-set-complex-command-for-undo)
2675 (or (eq viper-intermediate-command 'viper-repeat)
2676 (viper-special-read-and-insert-char))
2678 (delete-char 1 t)
2679 (setq char (if com viper-d-char (viper-char-at-pos 'backward)))
2681 (if com (insert char))
2683 (setq viper-d-char char)
2685 (viper-loop (1- (if (> arg 0) arg (- arg)))
2686 (delete-char 1 t)
2687 (insert char))
2689 (viper-adjust-undo)
2690 (backward-char arg)
2694 ;; basic cursor movement. j, k, l, h commands.
2696 (defun viper-forward-char (arg)
2697 "Move point right ARG characters (left if ARG negative).
2698 On reaching end of line, stop and signal error."
2699 (interactive "P")
2700 (viper-leave-region-active)
2701 (let ((val (viper-p-val arg))
2702 (com (viper-getcom arg)))
2703 (if com (viper-move-marker-locally 'viper-com-point (point)))
2704 (if viper-ex-style-motion
2705 (progn
2706 ;; the boundary condition check gets weird here because
2707 ;; forward-char may be the parameter of a delete, and 'dl' works
2708 ;; just like 'x' for the last char on a line, so we have to allow
2709 ;; the forward motion before the 'viper-execute-com', but, of
2710 ;; course, 'dl' doesn't work on an empty line, so we have to
2711 ;; catch that condition before 'viper-execute-com'
2712 (if (and (eolp) (bolp)) (error "Viper bell") (forward-char val))
2713 (if com (viper-execute-com 'viper-forward-char val com))
2714 (if (eolp) (progn (backward-char 1) (error "Viper bell"))))
2715 (forward-char val)
2716 (if com (viper-execute-com 'viper-forward-char val com)))))
2719 (defun viper-backward-char (arg)
2720 "Move point left ARG characters (right if ARG negative).
2721 On reaching beginning of line, stop and signal error."
2722 (interactive "P")
2723 (viper-leave-region-active)
2724 (let ((val (viper-p-val arg))
2725 (com (viper-getcom arg)))
2726 (if com (viper-move-marker-locally 'viper-com-point (point)))
2727 (if viper-ex-style-motion
2728 (progn
2729 (if (bolp) (error "Viper bell") (backward-char val))
2730 (if com (viper-execute-com 'viper-backward-char val com)))
2731 (backward-char val)
2732 (if com (viper-execute-com 'viper-backward-char val com)))))
2735 ;; Like forward-char, but doesn't move at end of buffer.
2736 ;; Returns distance traveled
2737 ;; (positive or 0, if arg positive; negative if arg negative).
2738 (defun viper-forward-char-carefully (&optional arg)
2739 (setq arg (or arg 1))
2740 (let ((pt (point)))
2741 (condition-case nil
2742 (forward-char arg)
2743 (error nil))
2744 (if (< (point) pt) ; arg was negative
2745 (- (viper-chars-in-region pt (point)))
2746 (viper-chars-in-region pt (point)))))
2749 ;; Like backward-char, but doesn't move at beg of buffer.
2750 ;; Returns distance traveled
2751 ;; (negative or 0, if arg positive; positive if arg negative).
2752 (defun viper-backward-char-carefully (&optional arg)
2753 (setq arg (or arg 1))
2754 (let ((pt (point)))
2755 (condition-case nil
2756 (backward-char arg)
2757 (error nil))
2758 (if (> (point) pt) ; arg was negative
2759 (viper-chars-in-region pt (point))
2760 (- (viper-chars-in-region pt (point))))))
2762 (defun viper-next-line-carefully (arg)
2763 (condition-case nil
2764 ;; do not use forward-line! need to keep column
2765 (let ((line-move-visual nil))
2766 (if (featurep 'emacs)
2767 (with-no-warnings (next-line arg))
2768 (next-line arg)))
2769 (error nil)))
2773 ;;; Word command
2775 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators for
2776 ;; word movement. When executed with a destructive command, \n is usually left
2777 ;; untouched for the last word. Viper uses syntax table to determine what is a
2778 ;; word and what is a separator. However, \n is always a separator. Also, if
2779 ;; viper-syntax-preference is 'vi, then `_' is part of the word.
2781 ;; skip only one \n
2782 (defun viper-skip-separators (forward)
2783 (if forward
2784 (progn
2785 (viper-skip-all-separators-forward 'within-line)
2786 (if (looking-at "\n")
2787 (progn
2788 (forward-char)
2789 (viper-skip-all-separators-forward 'within-line))))
2790 ;; check for eob and white space before it. move off of eob
2791 (if (and (eobp) (save-excursion
2792 (viper-backward-char-carefully)
2793 (viper-looking-at-separator)))
2794 (viper-backward-char-carefully))
2795 (viper-skip-all-separators-backward 'within-line)
2796 (viper-backward-char-carefully)
2797 (if (looking-at "\n")
2798 (viper-skip-all-separators-backward 'within-line)
2799 (or (viper-looking-at-separator) (forward-char)))))
2802 (defun viper-forward-word-kernel (val)
2803 (while (> val 0)
2804 (cond ((viper-looking-at-alpha)
2805 (viper-skip-alpha-forward "_")
2806 (viper-skip-separators t))
2807 ((viper-looking-at-separator)
2808 (viper-skip-separators t))
2809 ((not (viper-looking-at-alphasep))
2810 (viper-skip-nonalphasep-forward)
2811 (viper-skip-separators t)))
2812 (setq val (1- val))))
2814 ;; first skip non-newline separators backward, then skip \n. Then, if TWICE is
2815 ;; non-nil, skip non-\n back again, but don't overshoot the limit LIM.
2816 (defun viper-separator-skipback-special (twice lim)
2817 (let ((prev-char (viper-char-at-pos 'backward))
2818 (saved-point (point)))
2819 ;; skip non-newline separators backward
2820 (while (and (not (viper-memq-char prev-char '(nil \n)))
2821 (< lim (point))
2822 ;; must be non-newline separator
2823 (if (eq viper-syntax-preference 'strict-vi)
2824 (viper-memq-char prev-char '(?\ ?\t))
2825 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2826 (viper-backward-char-carefully)
2827 (setq prev-char (viper-char-at-pos 'backward)))
2829 (if (and (< lim (point)) (eq prev-char ?\n))
2830 (backward-char)
2831 ;; If we skipped to the next word and the prefix of this line doesn't
2832 ;; consist of separators preceded by a newline, then don't skip backwards
2833 ;; at all.
2834 (goto-char saved-point))
2835 (setq prev-char (viper-char-at-pos 'backward))
2837 ;; skip again, but make sure we don't overshoot the limit
2838 (if twice
2839 (while (and (not (viper-memq-char prev-char '(nil \n)))
2840 (< lim (point))
2841 ;; must be non-newline separator
2842 (if (eq viper-syntax-preference 'strict-vi)
2843 (viper-memq-char prev-char '(?\ ?\t))
2844 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2845 (viper-backward-char-carefully)
2846 (setq prev-char (viper-char-at-pos 'backward))))
2848 (if (= (point) lim)
2849 (viper-forward-char-carefully))
2853 (defun viper-forward-word (arg)
2854 "Forward word."
2855 (interactive "P")
2856 (viper-leave-region-active)
2857 (let ((val (viper-p-val arg))
2858 (com (viper-getcom arg)))
2859 (if com (viper-move-marker-locally 'viper-com-point (point)))
2860 (viper-forward-word-kernel val)
2861 (if com
2862 (progn
2863 (cond ((viper-char-equal com ?c)
2864 (viper-separator-skipback-special 'twice viper-com-point))
2865 ;; Yank words including the whitespace, but not newline
2866 ((viper-char-equal com ?y)
2867 (viper-separator-skipback-special nil viper-com-point))
2868 ((viper-dotable-command-p com)
2869 (viper-separator-skipback-special nil viper-com-point)))
2870 (viper-execute-com 'viper-forward-word val com)))
2874 (defun viper-forward-Word (arg)
2875 "Forward word delimited by white characters."
2876 (interactive "P")
2877 (viper-leave-region-active)
2878 (let ((val (viper-p-val arg))
2879 (com (viper-getcom arg)))
2880 (if com (viper-move-marker-locally 'viper-com-point (point)))
2881 (viper-loop val
2882 (viper-skip-nonseparators 'forward)
2883 (viper-skip-separators t))
2884 (if com (progn
2885 (cond ((viper-char-equal com ?c)
2886 (viper-separator-skipback-special 'twice viper-com-point))
2887 ;; Yank words including the whitespace, but not newline
2888 ((viper-char-equal com ?y)
2889 (viper-separator-skipback-special nil viper-com-point))
2890 ((viper-dotable-command-p com)
2891 (viper-separator-skipback-special nil viper-com-point)))
2892 (viper-execute-com 'viper-forward-Word val com)))))
2895 ;; this is a bit different from Vi, but Vi's end of word
2896 ;; makes no sense whatsoever
2897 (defun viper-end-of-word-kernel ()
2898 (if (viper-end-of-word-p) (forward-char))
2899 (if (viper-looking-at-separator)
2900 (viper-skip-all-separators-forward))
2902 (cond ((viper-looking-at-alpha) (viper-skip-alpha-forward "_"))
2903 ((not (viper-looking-at-alphasep)) (viper-skip-nonalphasep-forward)))
2904 (viper-backward-char-carefully))
2906 (defun viper-end-of-word-p ()
2907 (or (eobp)
2908 (save-excursion
2909 (cond ((viper-looking-at-alpha)
2910 (forward-char)
2911 (not (viper-looking-at-alpha)))
2912 ((not (viper-looking-at-alphasep))
2913 (forward-char)
2914 (viper-looking-at-alphasep))))))
2917 (defun viper-end-of-word (arg &optional careful)
2918 "Move point to end of current word."
2919 (interactive "P")
2920 (viper-leave-region-active)
2921 (let ((val (viper-p-val arg))
2922 (com (viper-getcom arg)))
2923 (if com (viper-move-marker-locally 'viper-com-point (point)))
2924 (viper-loop val (viper-end-of-word-kernel))
2925 (if com
2926 (progn
2927 (forward-char)
2928 (viper-execute-com 'viper-end-of-word val com)))))
2930 (defun viper-end-of-Word (arg)
2931 "Forward to end of word delimited by white character."
2932 (interactive "P")
2933 (viper-leave-region-active)
2934 (let ((val (viper-p-val arg))
2935 (com (viper-getcom arg)))
2936 (if com (viper-move-marker-locally 'viper-com-point (point)))
2937 (viper-loop val
2938 (viper-end-of-word-kernel)
2939 (viper-skip-nonseparators 'forward)
2940 (backward-char))
2941 (if com
2942 (progn
2943 (forward-char)
2944 (viper-execute-com 'viper-end-of-Word val com)))))
2946 (defun viper-backward-word-kernel (val)
2947 (while (> val 0)
2948 (viper-backward-char-carefully)
2949 (cond ((viper-looking-at-alpha)
2950 (viper-skip-alpha-backward "_"))
2951 ((viper-looking-at-separator)
2952 (forward-char)
2953 (viper-skip-separators nil)
2954 (viper-backward-char-carefully)
2955 (cond ((viper-looking-at-alpha)
2956 (viper-skip-alpha-backward "_"))
2957 ((not (viper-looking-at-alphasep))
2958 (viper-skip-nonalphasep-backward))
2959 ((bobp)) ; could still be at separator, but at beg of buffer
2960 (t (forward-char))))
2961 ((not (viper-looking-at-alphasep))
2962 (viper-skip-nonalphasep-backward)))
2963 (setq val (1- val))))
2965 (defun viper-backward-word (arg)
2966 "Backward word."
2967 (interactive "P")
2968 (viper-leave-region-active)
2969 (let ((val (viper-p-val arg))
2970 (com (viper-getcom arg)))
2971 (if com
2972 (let (i)
2973 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2974 (backward-char))
2975 (viper-move-marker-locally 'viper-com-point (point))
2976 (if i (forward-char))))
2977 (viper-backward-word-kernel val)
2978 (if com (viper-execute-com 'viper-backward-word val com))))
2980 (defun viper-backward-Word (arg)
2981 "Backward word delimited by white character."
2982 (interactive "P")
2983 (viper-leave-region-active)
2984 (let ((val (viper-p-val arg))
2985 (com (viper-getcom arg)))
2986 (if com
2987 (let (i)
2988 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2989 (backward-char))
2990 (viper-move-marker-locally 'viper-com-point (point))
2991 (if i (forward-char))))
2992 (viper-loop val
2993 (viper-skip-separators nil) ; nil means backward here
2994 (viper-skip-nonseparators 'backward))
2995 (if com (viper-execute-com 'viper-backward-Word val com))))
2999 ;; line commands
3001 (defun viper-beginning-of-line (arg)
3002 "Go to beginning of line."
3003 (interactive "P")
3004 (viper-leave-region-active)
3005 (let ((val (viper-p-val arg))
3006 (com (viper-getcom arg)))
3007 (if com (viper-move-marker-locally 'viper-com-point (point)))
3008 (beginning-of-line val)
3009 (if com (viper-execute-com 'viper-beginning-of-line val com))))
3011 (defun viper-bol-and-skip-white (arg)
3012 "Beginning of line at first non-white character."
3013 (interactive "P")
3014 (viper-leave-region-active)
3015 (let ((val (viper-p-val arg))
3016 (com (viper-getcom arg)))
3017 (if com (viper-move-marker-locally 'viper-com-point (point)))
3018 (forward-to-indentation (1- val))
3019 (if com (viper-execute-com 'viper-bol-and-skip-white val com))))
3021 (defun viper-goto-eol (arg)
3022 "Go to end of line."
3023 (interactive "P")
3024 (viper-leave-region-active)
3025 (let ((val (viper-p-val arg))
3026 (com (viper-getcom arg)))
3027 (if com (viper-move-marker-locally 'viper-com-point (point)))
3028 (end-of-line val)
3029 (if com (viper-execute-com 'viper-goto-eol val com))
3030 (if viper-ex-style-motion
3031 (if (and (eolp) (not (bolp))
3032 ;; a fix for viper-change-to-eol
3033 (not (equal viper-current-state 'insert-state)))
3034 (backward-char 1)
3035 ))))
3038 (defun viper-goto-col (arg)
3039 "Go to ARG's column."
3040 (interactive "P")
3041 (viper-leave-region-active)
3042 (let ((val (viper-p-val arg))
3043 (com (viper-getcom arg))
3044 line-len)
3045 (setq line-len
3046 (viper-chars-in-region
3047 (viper-line-pos 'start) (viper-line-pos 'end)))
3048 (if com (viper-move-marker-locally 'viper-com-point (point)))
3049 (beginning-of-line)
3050 (forward-char (1- (min line-len val)))
3051 (while (> (current-column) (1- val))
3052 (backward-char 1))
3053 (if com (viper-execute-com 'viper-goto-col val com))
3054 (save-excursion
3055 (end-of-line)
3056 (if (> val (current-column)) (error "Viper bell")))
3060 (defun viper-next-line (arg)
3061 "Go to next line."
3062 (interactive "P")
3063 (viper-leave-region-active)
3064 (let ((val (viper-p-val arg))
3065 (com (viper-getCom arg)))
3066 (if com (viper-move-marker-locally 'viper-com-point (point)))
3067 ;; do not use forward-line! need to keep column
3068 (let ((line-move-visual nil))
3069 (if (featurep 'emacs)
3070 (with-no-warnings (next-line val))
3071 (next-line val)))
3072 (if viper-ex-style-motion
3073 (if (and (eolp) (not (bolp))) (backward-char 1)))
3074 (setq this-command 'next-line)
3075 (if com (viper-execute-com 'viper-next-line val com))))
3077 (declare-function widget-type "wid-edit" (widget))
3078 (declare-function widget-button-press "wid-edit" (pos &optional event))
3079 (declare-function viper-set-hooks "viper" ())
3081 (defun viper-next-line-at-bol (arg)
3082 "Next line at beginning of line.
3083 If point is on a widget or a button, simulate clicking on that widget/button."
3084 (interactive "P")
3085 (let* ((field (get-char-property (point) 'field))
3086 (button (get-char-property (point) 'button))
3087 (doc (get-char-property (point) 'widget-doc))
3088 (widget (or field button doc)))
3089 (if (and widget
3090 (if (symbolp widget)
3091 (get widget 'widget-type)
3092 (and (consp widget)
3093 (get (widget-type widget) 'widget-type))))
3094 (widget-button-press (point))
3095 (if (and (fboundp 'button-at) (fboundp 'push-button) (button-at (point)))
3096 (push-button)
3097 ;; not a widget or a button
3098 (viper-leave-region-active)
3099 (save-excursion
3100 (end-of-line)
3101 (if (eobp) (error "Last line in buffer")))
3102 (let ((val (viper-p-val arg))
3103 (com (viper-getCom arg)))
3104 (if com (viper-move-marker-locally 'viper-com-point (point)))
3105 (forward-line val)
3106 (back-to-indentation)
3107 (if com (viper-execute-com 'viper-next-line-at-bol val com)))))))
3110 (defun viper-previous-line (arg)
3111 "Go to previous line."
3112 (interactive "P")
3113 (viper-leave-region-active)
3114 (let ((val (viper-p-val arg))
3115 (com (viper-getCom arg)))
3116 (if com (viper-move-marker-locally 'viper-com-point (point)))
3117 ;; do not use forward-line! need to keep column
3118 (let ((line-move-visual nil))
3119 (if (featurep 'emacs)
3120 (with-no-warnings (previous-line val))
3121 (previous-line val)))
3122 (if viper-ex-style-motion
3123 (if (and (eolp) (not (bolp))) (backward-char 1)))
3124 (setq this-command 'previous-line)
3125 (if com (viper-execute-com 'viper-previous-line val com))))
3128 (defun viper-previous-line-at-bol (arg)
3129 "Previous line at beginning of line."
3130 (interactive "P")
3131 (viper-leave-region-active)
3132 (save-excursion
3133 (beginning-of-line)
3134 (if (bobp) (error "First line in buffer")))
3135 (let ((val (viper-p-val arg))
3136 (com (viper-getCom arg)))
3137 (if com (viper-move-marker-locally 'viper-com-point (point)))
3138 (forward-line (- val))
3139 (back-to-indentation)
3140 (if com (viper-execute-com 'viper-previous-line val com))))
3142 (defun viper-change-to-eol (arg)
3143 "Change to end of line."
3144 (interactive "P")
3145 (viper-goto-eol (cons arg ?c)))
3147 (defun viper-kill-line (arg)
3148 "Delete line."
3149 (interactive "P")
3150 (viper-goto-eol (cons arg ?d)))
3152 (defun viper-erase-line (arg)
3153 "Erase line."
3154 (interactive "P")
3155 (viper-beginning-of-line (cons arg ?d)))
3158 ;;; Moving around
3160 (defun viper-goto-line (arg)
3161 "Go to ARG's line. Without ARG go to end of buffer."
3162 (interactive "P")
3163 (let ((val (viper-P-val arg))
3164 (com (viper-getCom arg)))
3165 (viper-move-marker-locally 'viper-com-point (point))
3166 (viper-deactivate-mark)
3167 (push-mark nil t)
3168 (if (null val)
3169 (goto-char (point-max))
3170 (goto-char (point-min))
3171 (forward-line (1- val)))
3173 ;; positioning is done twice: before and after command execution
3174 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3175 (back-to-indentation)
3177 (if com (viper-execute-com 'viper-goto-line val com))
3179 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3180 (back-to-indentation)
3183 ;; Find ARG's occurrence of CHAR on the current line.
3184 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
3185 ;; adjust point after search.
3186 (defun viper-find-char (arg char forward offset)
3187 (or (char-or-string-p char) (error "Viper bell"))
3188 (let ((arg (if forward arg (- arg)))
3189 (cmd (if (eq viper-intermediate-command 'viper-repeat)
3190 (nth 5 viper-d-com)
3191 (viper-array-to-string (this-command-keys))))
3192 point region-beg region-end)
3193 (save-excursion
3194 (save-restriction
3195 (if (> arg 0) ; forward
3196 (progn
3197 (setq region-beg (point))
3198 (if viper-allow-multiline-replace-regions
3199 (viper-forward-paragraph 1)
3200 (end-of-line))
3201 (setq region-end (point)))
3202 (setq region-end (point))
3203 (if viper-allow-multiline-replace-regions
3204 (viper-backward-paragraph 1)
3205 (beginning-of-line))
3206 (setq region-beg (point)))
3207 (if (or (and (< arg 0)
3208 (< (- region-end region-beg)
3209 (if viper-allow-multiline-replace-regions
3210 2 1))
3211 (bolp))
3212 (and (> arg 0)
3213 (< (- region-end region-beg)
3214 (if viper-allow-multiline-replace-regions
3215 3 2))
3216 (eolp)))
3217 (error "Command `%s': At %s of %s"
3219 (if (> arg 0) "end" "beginning")
3220 (if viper-allow-multiline-replace-regions
3221 "paragraph" "line")))
3222 (narrow-to-region region-beg region-end)
3223 ;; if arg > 0, point is forwarded before search.
3224 (if (> arg 0) (goto-char (1+ (point-min)))
3225 (goto-char (point-max)))
3226 (if (let ((case-fold-search nil))
3227 (search-forward (char-to-string char) nil 0 arg))
3228 (setq point (point))
3229 (error "Command `%s': `%c' not found" cmd char))))
3230 (goto-char point)
3231 (if (> arg 0)
3232 (backward-char (if offset 2 1))
3233 (forward-char (if offset 1 0)))))
3235 (defun viper-find-char-forward (arg)
3236 "Find char on the line.
3237 If called interactively read the char to find from the terminal, and if
3238 called from viper-repeat, the char last used is used. This behavior is
3239 controlled by the sign of prefix numeric value."
3240 (interactive "P")
3241 (let ((val (viper-p-val arg))
3242 (com (viper-getcom arg))
3243 (cmd-representation (nth 5 viper-d-com)))
3244 (if (> val 0)
3245 ;; this means that the function was called interactively
3246 (setq viper-f-char (read-char)
3247 viper-f-forward t
3248 viper-f-offset nil)
3249 ;; viper-repeat --- set viper-F-char from command-keys
3250 (setq viper-F-char (if (stringp cmd-representation)
3251 (viper-seq-last-elt cmd-representation)
3252 viper-F-char)
3253 viper-f-char viper-F-char)
3254 (setq val (- val)))
3255 (if com (viper-move-marker-locally 'viper-com-point (point)))
3256 (viper-find-char
3257 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t nil)
3258 (setq val (- val))
3259 (if com
3260 (progn
3261 (setq viper-F-char viper-f-char) ; set new viper-F-char
3262 (forward-char)
3263 (viper-execute-com 'viper-find-char-forward val com)))))
3265 (defun viper-goto-char-forward (arg)
3266 "Go up to char ARG forward on line."
3267 (interactive "P")
3268 (let ((val (viper-p-val arg))
3269 (com (viper-getcom arg))
3270 (cmd-representation (nth 5 viper-d-com)))
3271 (if (> val 0)
3272 ;; this means that the function was called interactively
3273 (setq viper-f-char (read-char)
3274 viper-f-forward t
3275 viper-f-offset t)
3276 ;; viper-repeat --- set viper-F-char from command-keys
3277 (setq viper-F-char (if (stringp cmd-representation)
3278 (viper-seq-last-elt cmd-representation)
3279 viper-F-char)
3280 viper-f-char viper-F-char)
3281 (setq val (- val)))
3282 (if com (viper-move-marker-locally 'viper-com-point (point)))
3283 (viper-find-char
3284 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t t)
3285 (setq val (- val))
3286 (if com
3287 (progn
3288 (setq viper-F-char viper-f-char) ; set new viper-F-char
3289 (forward-char)
3290 (viper-execute-com 'viper-goto-char-forward val com)))))
3292 (defun viper-find-char-backward (arg)
3293 "Find char ARG on line backward."
3294 (interactive "P")
3295 (let ((val (viper-p-val arg))
3296 (com (viper-getcom arg))
3297 (cmd-representation (nth 5 viper-d-com)))
3298 (if (> val 0)
3299 ;; this means that the function was called interactively
3300 (setq viper-f-char (read-char)
3301 viper-f-forward nil
3302 viper-f-offset nil)
3303 ;; viper-repeat --- set viper-F-char from command-keys
3304 (setq viper-F-char (if (stringp cmd-representation)
3305 (viper-seq-last-elt cmd-representation)
3306 viper-F-char)
3307 viper-f-char viper-F-char)
3308 (setq val (- val)))
3309 (if com (viper-move-marker-locally 'viper-com-point (point)))
3310 (viper-find-char
3311 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil nil)
3312 (setq val (- val))
3313 (if com
3314 (progn
3315 (setq viper-F-char viper-f-char) ; set new viper-F-char
3316 (viper-execute-com 'viper-find-char-backward val com)))))
3318 (defun viper-goto-char-backward (arg)
3319 "Go up to char ARG backward on line."
3320 (interactive "P")
3321 (let ((val (viper-p-val arg))
3322 (com (viper-getcom arg))
3323 (cmd-representation (nth 5 viper-d-com)))
3324 (if (> val 0)
3325 ;; this means that the function was called interactively
3326 (setq viper-f-char (read-char)
3327 viper-f-forward nil
3328 viper-f-offset t)
3329 ;; viper-repeat --- set viper-F-char from command-keys
3330 (setq viper-F-char (if (stringp cmd-representation)
3331 (viper-seq-last-elt cmd-representation)
3332 viper-F-char)
3333 viper-f-char viper-F-char)
3334 (setq val (- val)))
3335 (if com (viper-move-marker-locally 'viper-com-point (point)))
3336 (viper-find-char
3337 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil t)
3338 (setq val (- val))
3339 (if com
3340 (progn
3341 (setq viper-F-char viper-f-char) ; set new viper-F-char
3342 (viper-execute-com 'viper-goto-char-backward val com)))))
3344 (defun viper-repeat-find (arg)
3345 "Repeat previous find command."
3346 (interactive "P")
3347 (let ((val (viper-p-val arg))
3348 (com (viper-getcom arg)))
3349 (viper-deactivate-mark)
3350 (if com (viper-move-marker-locally 'viper-com-point (point)))
3351 (viper-find-char val viper-f-char viper-f-forward viper-f-offset)
3352 (if com
3353 (progn
3354 (if viper-f-forward (forward-char))
3355 (viper-execute-com 'viper-repeat-find val com)))))
3357 (defun viper-repeat-find-opposite (arg)
3358 "Repeat previous find command in the opposite direction."
3359 (interactive "P")
3360 (let ((val (viper-p-val arg))
3361 (com (viper-getcom arg)))
3362 (viper-deactivate-mark)
3363 (if com (viper-move-marker-locally 'viper-com-point (point)))
3364 (viper-find-char val viper-f-char (not viper-f-forward) viper-f-offset)
3365 (if com
3366 (progn
3367 (if viper-f-forward (forward-char))
3368 (viper-execute-com 'viper-repeat-find-opposite val com)))))
3371 ;; window scrolling etc.
3373 (defun viper-window-top (arg)
3374 "Go to home window line."
3375 (interactive "P")
3376 (let ((val (viper-p-val arg))
3377 (com (viper-getCom arg)))
3378 (viper-leave-region-active)
3379 (if com (viper-move-marker-locally 'viper-com-point (point)))
3380 (push-mark nil t)
3381 (move-to-window-line (1- val))
3383 ;; positioning is done twice: before and after command execution
3384 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3385 (back-to-indentation)
3387 (if com (viper-execute-com 'viper-window-top val com))
3389 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3390 (back-to-indentation)
3393 (defun viper-window-middle (arg)
3394 "Go to middle window line."
3395 (interactive "P")
3396 (let ((val (viper-p-val arg))
3397 (com (viper-getCom arg)))
3398 (viper-leave-region-active)
3399 (if com (viper-move-marker-locally 'viper-com-point (point)))
3400 (push-mark nil t)
3401 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3403 ;; positioning is done twice: before and after command execution
3404 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3405 (back-to-indentation)
3407 (if com (viper-execute-com 'viper-window-middle val com))
3409 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3410 (back-to-indentation)
3413 (defun viper-window-bottom (arg)
3414 "Go to last window line."
3415 (interactive "P")
3416 (let ((val (viper-p-val arg))
3417 (com (viper-getCom arg)))
3418 (viper-leave-region-active)
3419 (if com (viper-move-marker-locally 'viper-com-point (point)))
3420 (push-mark nil t)
3421 (move-to-window-line (- val))
3423 ;; positioning is done twice: before and after command execution
3424 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3425 (back-to-indentation)
3427 (if com (viper-execute-com 'viper-window-bottom val com))
3429 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3430 (back-to-indentation)
3433 (defun viper-line-to-top (arg)
3434 "Put current line on the home line."
3435 (interactive "p")
3436 (recenter (1- arg)))
3438 (defun viper-line-to-middle (arg)
3439 "Put current line on the middle line."
3440 (interactive "p")
3441 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3443 (defun viper-line-to-bottom (arg)
3444 "Put current line on the last line."
3445 (interactive "p")
3446 (recenter (- (window-height) (1+ arg))))
3448 ;; If point is within viper-search-scroll-threshold of window top or bottom,
3449 ;; scroll up or down 1/7 of window height, depending on whether we are at the
3450 ;; bottom or at the top of the window. This function is called by viper-search
3451 ;; (which is called from viper-search-forward/backward/next). If the value of
3452 ;; viper-search-scroll-threshold is negative - don't scroll.
3453 (defun viper-adjust-window ()
3454 (let ((win-height (if (featurep 'xemacs)
3455 (window-displayed-height)
3456 (1- (window-height)))) ; adjust for modeline
3457 (pt (point))
3458 at-top-p at-bottom-p
3459 min-scroll direction)
3460 (save-excursion
3461 (move-to-window-line 0) ; top
3462 (setq at-top-p
3463 (<= (count-lines pt (point))
3464 viper-search-scroll-threshold))
3465 (move-to-window-line -1) ; bottom
3466 (setq at-bottom-p
3467 (<= (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 "Viper bell"))))))
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-insert-isearch-string ()
3691 "Insert `isearch' last search string."
3692 (interactive)
3693 (when isearch-string (insert isearch-string)))
3695 (defun viper-if-string (prompt)
3696 (if (memq viper-intermediate-command
3697 '(viper-command-argument viper-digit-argument viper-repeat))
3698 (setq viper-this-command-keys (this-command-keys)))
3699 (let* ((keymap (let ((keymap (copy-keymap minibuffer-local-map)))
3700 (define-key keymap [(control ?s)] 'viper-insert-isearch-string)
3701 keymap))
3702 (s (viper-read-string-with-history
3703 prompt
3704 nil ; no initial
3705 'viper-search-history
3706 (car viper-search-history)
3707 keymap)))
3708 (if (not (string= s ""))
3709 (setq viper-s-string s))))
3712 (defun viper-toggle-search-style (arg)
3713 "Toggle the value of viper-case-fold-search/viper-re-search.
3714 Without prefix argument, will ask which search style to toggle. With prefix
3715 arg 1, toggles viper-case-fold-search; with arg 2 toggles viper-re-search.
3717 Although this function is bound to \\[viper-toggle-search-style], the most
3718 convenient way to use it is to bind `//' to the macro
3719 `1 M-x viper-toggle-search-style' and `///' to
3720 `2 M-x viper-toggle-search-style'. In this way, hitting `//' quickly will
3721 toggle case-fold-search and hitting `/' three times witth toggle regexp
3722 search. Macros are more convenient in this case because they don't affect
3723 the Emacs binding of `/'."
3724 (interactive "P")
3725 (let (msg)
3726 (cond ((or (eq arg 1)
3727 (and (null arg)
3728 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3729 (if viper-case-fold-search
3730 "case-insensitive" "case-sensitive")
3731 (if viper-case-fold-search
3732 "case-sensitive"
3733 "case-insensitive")))))
3734 (setq viper-case-fold-search (null viper-case-fold-search))
3735 (if viper-case-fold-search
3736 (setq msg "Search becomes case-insensitive")
3737 (setq msg "Search becomes case-sensitive")))
3738 ((or (eq arg 2)
3739 (and (null arg)
3740 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3741 (if viper-re-search
3742 "regexp-search" "vanilla-search")
3743 (if viper-re-search
3744 "vanilla-search"
3745 "regexp-search")))))
3746 (setq viper-re-search (null viper-re-search))
3747 (if viper-re-search
3748 (setq msg "Search becomes regexp-style")
3749 (setq msg "Search becomes vanilla-style")))
3751 (setq msg "Search style remains unchanged")))
3752 (princ msg t)))
3754 (defun viper-set-searchstyle-toggling-macros (unset &optional major-mode)
3755 "Set the macros for toggling the search style in Viper's vi-state.
3756 The macro that toggles case sensitivity is bound to `//', and the one that
3757 toggles regexp search is bound to `///'.
3758 With a prefix argument, this function unsets the macros.
3759 If MAJOR-MODE is set, set the macros only in that major mode."
3760 (interactive "P")
3761 (let (scope)
3762 (if (and major-mode (symbolp major-mode))
3763 (setq scope major-mode)
3764 (setq scope 't))
3765 (or noninteractive
3766 (if (not unset)
3767 (progn
3768 ;; toggle case sensitivity in search
3769 (viper-record-kbd-macro
3770 "//" 'vi-state
3771 [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]
3772 scope)
3773 ;; toggle regexp/vanila search
3774 (viper-record-kbd-macro
3775 "///" 'vi-state
3776 [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]
3777 scope)
3778 ;; XEmacs has no called-interactively-p
3779 ;; (if (called-interactively-p 'interactive)
3780 (if (interactive-p)
3781 (message
3782 "// and /// now toggle case-sensitivity and regexp search")))
3783 (viper-unrecord-kbd-macro "//" 'vi-state)
3784 (sit-for 2)
3785 (viper-unrecord-kbd-macro "///" 'vi-state)))
3789 (defun viper-set-parsing-style-toggling-macro (unset)
3790 "Set `%%%' to be a macro that toggles whether comment fields should be parsed for matching parentheses.
3791 This is used in conjunction with the `%' command.
3793 With a prefix argument, unsets the macro."
3794 (interactive "P")
3795 (or noninteractive
3796 (if (not unset)
3797 (progn
3798 ;; Make %%% toggle parsing comments for matching parentheses
3799 (viper-record-kbd-macro
3800 "%%%" 'vi-state
3801 [(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]
3803 ;; XEmacs has no called-interactively-p. And interactive-p
3804 ;; works fine here.
3805 ;; (if (called-interactively-p 'interactive)
3806 (if (interactive-p)
3807 (message
3808 "%%%%%% now toggles whether comments should be parsed for matching parentheses")))
3809 (viper-unrecord-kbd-macro "%%%" 'vi-state))))
3812 (defun viper-set-emacs-state-searchstyle-macros (unset &optional arg-majormode)
3813 "Set the macros for toggling the search style in Viper's emacs-state.
3814 The macro that toggles case sensitivity is bound to `//', and the one that
3815 toggles regexp search is bound to `///'.
3816 With a prefix argument, this function unsets the macros.
3817 If the optional prefix argument is non-nil and specifies a valid major mode,
3818 this sets the macros only in the macros in that major mode. Otherwise,
3819 the macros are set in the current major mode.
3820 \(When unsetting the macros, the second argument has no effect.\)"
3821 (interactive "P")
3822 (or noninteractive
3823 (if (not unset)
3824 (progn
3825 ;; toggle case sensitivity in search
3826 (viper-record-kbd-macro
3827 "//" 'emacs-state
3828 [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]
3829 (or arg-majormode major-mode))
3830 ;; toggle regexp/vanila search
3831 (viper-record-kbd-macro
3832 "///" 'emacs-state
3833 [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]
3834 (or arg-majormode major-mode))
3835 ;; called-interactively-p does not work for
3836 ;; XEmacs. interactive-p is ok here.
3837 ;; (if (called-interactively-p 'interactive)
3838 (if (interactive-p)
3839 (message
3840 "// and /// now toggle case-sensitivity and regexp search.")))
3841 (viper-unrecord-kbd-macro "//" 'emacs-state)
3842 (sit-for 2)
3843 (viper-unrecord-kbd-macro "///" 'emacs-state))))
3846 (defun viper-search-forward (arg)
3847 "Search a string forward.
3848 ARG is used to find the ARG's occurrence of the string.
3849 Null string will repeat previous search."
3850 (interactive "P")
3851 (let ((val (viper-P-val arg))
3852 (com (viper-getcom arg))
3853 (old-str viper-s-string)
3854 debug-on-error)
3855 (setq viper-s-forward t)
3856 (viper-if-string "/")
3857 ;; this is not used at present, but may be used later
3858 (if (or (not (equal old-str viper-s-string))
3859 (not (markerp viper-local-search-start-marker))
3860 (not (marker-buffer viper-local-search-start-marker)))
3861 (setq viper-local-search-start-marker (point-marker)))
3862 (viper-search viper-s-string t val)
3863 (if com
3864 (progn
3865 (viper-move-marker-locally 'viper-com-point (mark t))
3866 (viper-execute-com 'viper-search-next val com)))
3869 (defun viper-search-backward (arg)
3870 "Search a string backward.
3871 ARG is used to find the ARG's occurrence of the string.
3872 Null string will repeat previous search."
3873 (interactive "P")
3874 (let ((val (viper-P-val arg))
3875 (com (viper-getcom arg))
3876 (old-str viper-s-string)
3877 debug-on-error)
3878 (setq viper-s-forward nil)
3879 (viper-if-string "?")
3880 ;; this is not used at present, but may be used later
3881 (if (or (not (equal old-str viper-s-string))
3882 (not (markerp viper-local-search-start-marker))
3883 (not (marker-buffer viper-local-search-start-marker)))
3884 (setq viper-local-search-start-marker (point-marker)))
3885 (viper-search viper-s-string nil val)
3886 (if com
3887 (progn
3888 (viper-move-marker-locally 'viper-com-point (mark t))
3889 (viper-execute-com 'viper-search-next val com)))))
3892 ;; Search for COUNT's occurrence of STRING.
3893 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3894 ;; INIT-POINT is the position where search is to start.
3895 ;; Arguments:
3896 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3897 (defun viper-search (string forward arg
3898 &optional no-offset init-point fail-if-not-found)
3899 (if (not (equal string ""))
3900 (let ((val (viper-p-val arg))
3901 (com (viper-getcom arg))
3902 (offset (not no-offset))
3903 (case-fold-search viper-case-fold-search)
3904 (start-point (or init-point (point))))
3905 (viper-deactivate-mark)
3906 (if forward
3907 (condition-case nil
3908 (progn
3909 (if offset (viper-forward-char-carefully))
3910 (if viper-re-search
3911 (progn
3912 (re-search-forward string nil nil val)
3913 (re-search-backward string))
3914 (search-forward string nil nil val)
3915 (search-backward string))
3916 (if (not (equal start-point (point)))
3917 (push-mark start-point t)))
3918 (search-failed
3919 (if (and (not fail-if-not-found) viper-search-wrap-around)
3920 (progn
3921 (message "Search wrapped around BOTTOM of buffer")
3922 (goto-char (point-min))
3923 (viper-search string forward (cons 1 com) t start-point 'fail)
3924 ;; don't wait in macros
3925 (or executing-kbd-macro
3926 (memq viper-intermediate-command
3927 '(viper-repeat
3928 viper-digit-argument
3929 viper-command-argument))
3930 (sit-for 2))
3931 ;; delete the wrap-around message
3932 (message "")
3934 (goto-char start-point)
3935 (error "`%s': %s not found"
3936 string
3937 (if viper-re-search "Pattern" "String"))
3939 ;; backward
3940 (condition-case nil
3941 (progn
3942 (if viper-re-search
3943 (re-search-backward string nil nil val)
3944 (search-backward string nil nil val))
3945 (if (not (equal start-point (point)))
3946 (push-mark start-point t)))
3947 (search-failed
3948 (if (and (not fail-if-not-found) viper-search-wrap-around)
3949 (progn
3950 (message "Search wrapped around TOP of buffer")
3951 (goto-char (point-max))
3952 (viper-search string forward (cons 1 com) t start-point 'fail)
3953 ;; don't wait in macros
3954 (or executing-kbd-macro
3955 (memq viper-intermediate-command
3956 '(viper-repeat
3957 viper-digit-argument
3958 viper-command-argument))
3959 (sit-for 2))
3960 ;; delete the wrap-around message
3961 (message "")
3963 (goto-char start-point)
3964 (error "`%s': %s not found"
3965 string
3966 (if viper-re-search "Pattern" "String"))
3967 ))))
3968 ;; pull up or down if at top/bottom of window
3969 (viper-adjust-window)
3970 ;; highlight the result of search
3971 ;; don't wait and don't highlight in macros
3972 (or executing-kbd-macro
3973 (memq viper-intermediate-command
3974 '(viper-repeat viper-digit-argument viper-command-argument))
3975 (viper-flash-search-pattern))
3978 (defun viper-search-next (arg)
3979 "Repeat previous search."
3980 (interactive "P")
3981 (let ((val (viper-p-val arg))
3982 (com (viper-getcom arg))
3983 debug-on-error)
3984 (if (or (null viper-s-string) (string= viper-s-string ""))
3985 (error viper-NoPrevSearch))
3986 (viper-search viper-s-string viper-s-forward arg)
3987 (if com
3988 (progn
3989 (viper-move-marker-locally 'viper-com-point (mark t))
3990 (viper-execute-com 'viper-search-next val com)))))
3992 (defun viper-search-Next (arg)
3993 "Repeat previous search in the reverse direction."
3994 (interactive "P")
3995 (let ((val (viper-p-val arg))
3996 (com (viper-getcom arg))
3997 debug-on-error)
3998 (if (null viper-s-string) (error viper-NoPrevSearch))
3999 (viper-search viper-s-string (not viper-s-forward) arg)
4000 (if com
4001 (progn
4002 (viper-move-marker-locally 'viper-com-point (mark t))
4003 (viper-execute-com 'viper-search-Next val com)))))
4006 ;; Search contents of buffer defined by one of Viper's motion commands.
4007 ;; Repeatable via `n' and `N'.
4008 (defun viper-buffer-search-enable (&optional c)
4009 (cond (c (setq viper-buffer-search-char c))
4010 ((null viper-buffer-search-char)
4011 ;; ?g acts as a default value for viper-buffer-search-char
4012 (setq viper-buffer-search-char ?g)))
4013 (define-key viper-vi-basic-map
4014 (cond ((viper-characterp viper-buffer-search-char)
4015 (char-to-string viper-buffer-search-char))
4016 (t (error "viper-buffer-search-char: wrong value type, %S"
4017 viper-buffer-search-char)))
4018 'viper-command-argument)
4019 (aset viper-exec-array viper-buffer-search-char 'viper-exec-buffer-search)
4020 (setq viper-prefix-commands
4021 (cons viper-buffer-search-char viper-prefix-commands)))
4023 ;; This is a Viper wraper for isearch-forward.
4024 (defun viper-isearch-forward (arg)
4025 "Do incremental search forward."
4026 (interactive "P")
4027 ;; emacs bug workaround
4028 (if (listp arg) (setq arg (car arg)))
4029 (viper-exec-form-in-emacs (list 'isearch-forward arg)))
4031 ;; This is a Viper wraper for isearch-backward."
4032 (defun viper-isearch-backward (arg)
4033 "Do incremental search backward."
4034 (interactive "P")
4035 ;; emacs bug workaround
4036 (if (listp arg) (setq arg (car arg)))
4037 (viper-exec-form-in-emacs (list 'isearch-backward arg)))
4040 ;; visiting and killing files, buffers
4042 (defun viper-switch-to-buffer ()
4043 "Switch to buffer in the current window."
4044 (interactive)
4045 (let ((other-buffer (other-buffer (current-buffer)))
4046 buffer)
4047 (setq buffer
4048 (funcall viper-read-buffer-function
4049 "Switch to buffer in this window: " other-buffer))
4050 (switch-to-buffer buffer)))
4052 (defun viper-switch-to-buffer-other-window ()
4053 "Switch to buffer in another window."
4054 (interactive)
4055 (let ((other-buffer (other-buffer (current-buffer)))
4056 buffer)
4057 (setq buffer
4058 (funcall viper-read-buffer-function
4059 "Switch to buffer in another window: " other-buffer))
4060 (switch-to-buffer-other-window buffer)))
4062 (defun viper-kill-buffer ()
4063 "Kill a buffer."
4064 (interactive)
4065 (let (buffer buffer-name)
4066 (setq buffer-name
4067 (funcall viper-read-buffer-function
4068 (format "Kill buffer \(%s\): "
4069 (buffer-name (current-buffer)))))
4070 (setq buffer
4071 (if (null buffer-name)
4072 (current-buffer)
4073 (get-buffer buffer-name)))
4074 (if (null buffer) (error "`%s': No such buffer" buffer-name))
4075 (if (or (not (buffer-modified-p buffer))
4076 (y-or-n-p
4077 (format
4078 "Buffer `%s' is modified, are you sure you want to kill it? "
4079 buffer-name)))
4080 (kill-buffer buffer)
4081 (error "Buffer not killed"))))
4085 ;; yank and pop
4087 (defsubst viper-yank (text)
4088 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
4089 (insert text)
4090 (setq this-command 'yank))
4092 (defun viper-put-back (arg)
4093 "Put back after point/below line."
4094 (interactive "P")
4095 (let ((val (viper-p-val arg))
4096 (text (if viper-use-register
4097 (cond ((viper-valid-register viper-use-register '(digit))
4098 (current-kill
4099 (- viper-use-register ?1) 'do-not-rotate))
4100 ((viper-valid-register viper-use-register)
4101 (get-register (downcase viper-use-register)))
4102 (t (error viper-InvalidRegister viper-use-register)))
4103 (current-kill 0)))
4104 sv-point chars-inserted lines-inserted)
4105 (if (null text)
4106 (if viper-use-register
4107 (let ((reg viper-use-register))
4108 (setq viper-use-register nil)
4109 (error viper-EmptyRegister reg))
4110 (error "Viper bell")))
4111 (setq viper-use-register nil)
4112 (if (viper-end-with-a-newline-p text)
4113 (progn
4114 (end-of-line)
4115 (if (eobp)
4116 (insert "\n")
4117 (forward-line 1))
4118 (beginning-of-line))
4119 (if (not (eolp)) (viper-forward-char-carefully)))
4120 (set-marker (viper-mark-marker) (point) (current-buffer))
4121 (viper-set-destructive-command
4122 (list 'viper-put-back val nil viper-use-register nil nil))
4123 (setq sv-point (point))
4124 (viper-loop val (viper-yank text))
4125 (setq chars-inserted (abs (- (point) sv-point))
4126 lines-inserted (abs (count-lines (point) sv-point)))
4127 (if (or (> chars-inserted viper-change-notification-threshold)
4128 (> lines-inserted viper-change-notification-threshold))
4129 (unless (viper-is-in-minibuffer)
4130 (message "Inserted %d character(s), %d line(s)"
4131 chars-inserted lines-inserted))))
4132 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4133 ;; newline; it leaves the cursor at the beginning when the text contains
4134 ;; a newline
4135 (if (viper-same-line (point) (mark))
4136 (or (= (point) (mark)) (viper-backward-char-carefully))
4137 (exchange-point-and-mark)
4138 (if (bolp)
4139 (back-to-indentation)))
4140 (viper-deactivate-mark))
4142 (defun viper-Put-back (arg)
4143 "Put back at point/above line."
4144 (interactive "P")
4145 (let ((val (viper-p-val arg))
4146 (text (if viper-use-register
4147 (cond ((viper-valid-register viper-use-register '(digit))
4148 (current-kill
4149 (- viper-use-register ?1) 'do-not-rotate))
4150 ((viper-valid-register viper-use-register)
4151 (get-register (downcase viper-use-register)))
4152 (t (error viper-InvalidRegister viper-use-register)))
4153 (current-kill 0)))
4154 sv-point chars-inserted lines-inserted)
4155 (if (null text)
4156 (if viper-use-register
4157 (let ((reg viper-use-register))
4158 (setq viper-use-register nil)
4159 (error viper-EmptyRegister reg))
4160 (error "Viper bell")))
4161 (setq viper-use-register nil)
4162 (if (viper-end-with-a-newline-p text) (beginning-of-line))
4163 (viper-set-destructive-command
4164 (list 'viper-Put-back val nil viper-use-register nil nil))
4165 (set-marker (viper-mark-marker) (point) (current-buffer))
4166 (setq sv-point (point))
4167 (viper-loop val (viper-yank text))
4168 (setq chars-inserted (abs (- (point) sv-point))
4169 lines-inserted (abs (count-lines (point) sv-point)))
4170 (if (or (> chars-inserted viper-change-notification-threshold)
4171 (> lines-inserted viper-change-notification-threshold))
4172 (unless (viper-is-in-minibuffer)
4173 (message "Inserted %d character(s), %d line(s)"
4174 chars-inserted lines-inserted))))
4175 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
4176 ;; newline; it leaves the cursor at the beginning when the text contains
4177 ;; a newline
4178 (if (viper-same-line (point) (mark))
4179 (or (= (point) (mark)) (viper-backward-char-carefully))
4180 (exchange-point-and-mark)
4181 (if (bolp)
4182 (back-to-indentation)))
4183 (viper-deactivate-mark))
4186 ;; Copy region to kill-ring.
4187 ;; If BEG and END do not belong to the same buffer, copy empty region.
4188 (defun viper-copy-region-as-kill (beg end)
4189 (condition-case nil
4190 (copy-region-as-kill beg end)
4191 (error (copy-region-as-kill beg beg))))
4194 (defun viper-delete-char (arg)
4195 "Delete next character."
4196 (interactive "P")
4197 (let ((val (viper-p-val arg))
4198 end-del-pos)
4199 (viper-set-destructive-command
4200 (list 'viper-delete-char val nil nil nil nil))
4201 (if (and viper-ex-style-editing
4202 (> val (viper-chars-in-region (point) (viper-line-pos 'end))))
4203 (setq val (viper-chars-in-region (point) (viper-line-pos 'end))))
4204 (if (and viper-ex-style-motion (eolp))
4205 (if (bolp) (error "Viper bell") (setq val 0))) ; not bol---simply back 1 ch
4206 (save-excursion
4207 (viper-forward-char-carefully val)
4208 (setq end-del-pos (point)))
4209 (if viper-use-register
4210 (progn
4211 (cond ((viper-valid-register viper-use-register '((Letter)))
4212 (viper-append-to-register
4213 (downcase viper-use-register) (point) end-del-pos))
4214 ((viper-valid-register viper-use-register)
4215 (copy-to-register
4216 viper-use-register (point) end-del-pos nil))
4217 (t (error viper-InvalidRegister viper-use-register)))
4218 (setq viper-use-register nil)))
4220 (delete-char val t)
4221 (if viper-ex-style-motion
4222 (if (and (eolp) (not (bolp))) (backward-char 1)))
4225 (defun viper-delete-backward-char (arg)
4226 "Delete previous character. On reaching beginning of line, stop and beep."
4227 (interactive "P")
4228 (let ((val (viper-p-val arg))
4229 end-del-pos)
4230 (viper-set-destructive-command
4231 (list 'viper-delete-backward-char val nil nil nil nil))
4232 (if (and
4233 viper-ex-style-editing
4234 (> val (viper-chars-in-region (viper-line-pos 'start) (point))))
4235 (setq val (viper-chars-in-region (viper-line-pos 'start) (point))))
4236 (save-excursion
4237 (viper-backward-char-carefully val)
4238 (setq end-del-pos (point)))
4239 (if viper-use-register
4240 (progn
4241 (cond ((viper-valid-register viper-use-register '(Letter))
4242 (viper-append-to-register
4243 (downcase viper-use-register) end-del-pos (point)))
4244 ((viper-valid-register viper-use-register)
4245 (copy-to-register
4246 viper-use-register end-del-pos (point) nil))
4247 (t (error viper-InvalidRegister viper-use-register)))
4248 (setq viper-use-register nil)))
4249 (if (and (bolp) viper-ex-style-editing)
4250 (ding))
4251 (delete-char (- val) t)))
4254 (defun viper-del-backward-char-in-insert ()
4255 "Delete 1 char backwards while in insert mode."
4256 (interactive)
4257 (if (and viper-ex-style-editing (bolp))
4258 (beep 1)
4259 ;; don't put on kill ring
4260 (delete-char -1 nil)))
4263 (defun viper-del-backward-char-in-replace ()
4264 "Delete one character in replace mode.
4265 If `viper-delete-backwards-in-replace' is t, then DEL key actually deletes
4266 characters. If it is nil, then the cursor just moves backwards, similarly
4267 to Vi. The variable `viper-ex-style-editing', if t, doesn't let the
4268 cursor move past the beginning of line."
4269 (interactive)
4270 (cond (viper-delete-backwards-in-replace
4271 (cond ((not (bolp))
4272 ;; don't put on kill ring
4273 (delete-char -1 nil))
4274 (viper-ex-style-editing
4275 (beep 1))
4276 ((bobp)
4277 (beep 1))
4279 ;; don't put on kill ring
4280 (delete-char -1 nil))))
4281 (viper-ex-style-editing
4282 (if (bolp)
4283 (beep 1)
4284 (backward-char 1)))
4286 (backward-char 1))))
4290 ;; join lines.
4292 (defun viper-join-lines (arg)
4293 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4294 (interactive "*P")
4295 (let ((val (viper-P-val arg)))
4296 (viper-set-destructive-command
4297 (list 'viper-join-lines val nil nil nil nil))
4298 (viper-loop (if (null val) 1 (1- val))
4299 (end-of-line)
4300 (if (not (eobp))
4301 (progn
4302 (forward-line 1)
4303 (delete-region (point) (1- (point)))
4304 (fixup-whitespace)
4305 ;; fixup-whitespace sometimes does not leave space
4306 ;; between objects, so we insert it as in Vi
4307 (or (looking-at " ")
4308 (insert " ")
4309 (backward-char 1))
4310 )))))
4313 ;; Replace state
4315 (defun viper-change (beg end)
4316 (if (markerp beg) (setq beg (marker-position beg)))
4317 (if (markerp end) (setq end (marker-position end)))
4318 ;; beg is sometimes (mark t), which may be nil
4319 (or beg (setq beg end))
4321 (viper-set-complex-command-for-undo)
4322 (if viper-use-register
4323 (progn
4324 (copy-to-register viper-use-register beg end nil)
4325 (setq viper-use-register nil)))
4326 (viper-set-replace-overlay beg end)
4327 (setq last-command nil) ; separate repl text from prev kills
4329 (if (= (viper-replace-start) (point-max))
4330 (error "End of buffer"))
4332 (setq viper-last-replace-region
4333 (buffer-substring (viper-replace-start)
4334 (viper-replace-end)))
4336 ;; protect against error while inserting "@" and other disasters
4337 ;; (e.g., read-only buff)
4338 (condition-case conds
4339 (if (or viper-allow-multiline-replace-regions
4340 (viper-same-line (viper-replace-start)
4341 (viper-replace-end)))
4342 (progn
4343 ;; tabs cause problems in replace, so untabify
4344 (goto-char (viper-replace-end))
4345 (insert-before-markers "@") ; put placeholder after the TAB
4346 (untabify (viper-replace-start) (point))
4347 ;; del @, don't put on kill ring
4348 (delete-char -1)
4350 (viper-set-replace-overlay-glyphs
4351 viper-replace-region-start-delimiter
4352 viper-replace-region-end-delimiter)
4353 ;; this move takes care of the last posn in the overlay, which
4354 ;; has to be shifted because of insert. We can't simply insert
4355 ;; "$" before-markers because then overlay-start will shift the
4356 ;; beginning of the overlay in case we are replacing a single
4357 ;; character. This fixes the bug with `s' and `cl' commands.
4358 (viper-move-replace-overlay (viper-replace-start) (point))
4359 (goto-char (viper-replace-start))
4360 (viper-change-state-to-replace t))
4361 (kill-region (viper-replace-start)
4362 (viper-replace-end))
4363 (viper-hide-replace-overlay)
4364 (viper-change-state-to-insert))
4365 (error ;; make sure that the overlay doesn't stay.
4366 ;; go back to the original point
4367 (goto-char (viper-replace-start))
4368 (viper-hide-replace-overlay)
4369 (viper-message-conditions conds))))
4372 (defun viper-change-subr (beg end)
4373 ;; beg is sometimes (mark t), which may be nil
4374 (or beg (setq beg end))
4375 (if viper-use-register
4376 (progn
4377 (copy-to-register viper-use-register beg end nil)
4378 (setq viper-use-register nil)))
4379 (kill-region beg end)
4380 (setq this-command 'viper-change)
4381 (viper-yank-last-insertion))
4383 (defun viper-toggle-case (arg)
4384 "Toggle character case."
4385 (interactive "P")
4386 (let ((val (viper-p-val arg)) (c))
4387 (viper-set-destructive-command
4388 (list 'viper-toggle-case val nil nil nil nil))
4389 (while (> val 0)
4390 (setq c (following-char))
4391 (delete-char 1 nil)
4392 (if (eq c (upcase c))
4393 (insert-char (downcase c) 1)
4394 (insert-char (upcase c) 1))
4395 (if (eolp) (backward-char 1))
4396 (setq val (1- val)))))
4399 ;; query replace
4401 (defun viper-query-replace ()
4402 "Query replace.
4403 If a null string is suplied as the string to be replaced,
4404 the query replace mode will toggle between string replace
4405 and regexp replace."
4406 (interactive)
4407 (let (str)
4408 (setq str (viper-read-string-with-history
4409 (if viper-re-query-replace "Query replace regexp: "
4410 "Query replace: ")
4411 nil ; no initial
4412 'viper-replace1-history
4413 (car viper-replace1-history) ; default
4415 (if (string= str "")
4416 (progn
4417 (setq viper-re-query-replace (not viper-re-query-replace))
4418 (message "Query replace mode changed to %s"
4419 (if viper-re-query-replace "regexp replace"
4420 "string replace")))
4421 (if viper-re-query-replace
4422 (query-replace-regexp
4424 (viper-read-string-with-history
4425 (format "Query replace regexp `%s' with: " str)
4426 nil ; no initial
4427 'viper-replace1-history
4428 (car viper-replace1-history) ; default
4430 (query-replace
4432 (viper-read-string-with-history
4433 (format "Query replace `%s' with: " str)
4434 nil ; no initial
4435 'viper-replace1-history
4436 (car viper-replace1-history) ; default
4437 ))))))
4440 ;; marking
4442 (defun viper-mark-beginning-of-buffer ()
4443 "Mark beginning of buffer."
4444 (interactive)
4445 (push-mark (point))
4446 (goto-char (point-min))
4447 (exchange-point-and-mark)
4448 (message "Mark set at the beginning of buffer"))
4450 (defun viper-mark-end-of-buffer ()
4451 "Mark end of buffer."
4452 (interactive)
4453 (push-mark (point))
4454 (goto-char (point-max))
4455 (exchange-point-and-mark)
4456 (message "Mark set at the end of buffer"))
4458 (defun viper-mark-point ()
4459 "Set mark at point of buffer."
4460 (interactive)
4461 (let ((char (read-char)))
4462 (cond ((and (<= ?a char) (<= char ?z))
4463 (point-to-register (viper-int-to-char (1+ (- char ?a)))))
4464 ((viper= char ?<) (viper-mark-beginning-of-buffer))
4465 ((viper= char ?>) (viper-mark-end-of-buffer))
4466 ((viper= char ?.) (viper-set-mark-if-necessary))
4467 ((viper= char ?,) (viper-cycle-through-mark-ring))
4468 ((viper= char ?^) (push-mark viper-saved-mark t t))
4469 ((viper= char ?D) (mark-defun))
4470 (t (error "Viper bell"))
4473 ;; Algorithm: If first invocation of this command save mark on ring, goto
4474 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4475 ;; making it into the new mark, M1.
4476 ;; Push this mark back and set mark to the original point position, p1.
4477 ;; So, if you hit '' or `` then you can return to p1.
4479 ;; If repeated command, pop top elt from the ring into mark and
4480 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4481 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4482 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4483 ;; etc.
4484 (defun viper-cycle-through-mark-ring ()
4485 "Visit previous locations on the mark ring.
4486 One can use `` and '' to temporarily jump 1 step back."
4487 (let* ((sv-pt (point)))
4488 ;; if repeated `m,' command, pop the previously saved mark.
4489 ;; Prev saved mark is actually prev saved point. It is used if the
4490 ;; user types `` or '' and is discarded
4491 ;; from the mark ring by the next `m,' command.
4492 ;; In any case, go to the previous or previously saved mark.
4493 ;; Then push the current mark (popped off the ring) and set current
4494 ;; point to be the mark. Current pt as mark is discarded by the next
4495 ;; m, command.
4496 (if (eq last-command 'viper-cycle-through-mark-ring)
4498 ;; save current mark if the first iteration
4499 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4500 (if (mark t)
4501 (push-mark (mark t) t)) )
4502 (pop-mark)
4503 (set-mark-command 1)
4504 ;; don't duplicate mark on the ring
4505 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4506 (push-mark sv-pt t)
4507 (viper-deactivate-mark)
4508 (setq this-command 'viper-cycle-through-mark-ring)
4512 (defun viper-goto-mark (arg)
4513 "Go to mark."
4514 (interactive "P")
4515 (let ((char (read-char))
4516 (com (viper-getcom arg)))
4517 (viper-goto-mark-subr char com nil)))
4519 (defun viper-goto-mark-and-skip-white (arg)
4520 "Go to mark and skip to first non-white character on line."
4521 (interactive "P")
4522 (let ((char (read-char))
4523 (com (viper-getCom arg)))
4524 (viper-goto-mark-subr char com t)))
4526 (defun viper-goto-mark-subr (char com skip-white)
4527 (if (eobp)
4528 (if (bobp)
4529 (error "Empty buffer")
4530 (backward-char 1)))
4531 (cond ((viper-valid-register char '(letter))
4532 (let* ((buff (current-buffer))
4533 (reg (viper-int-to-char (1+ (- char ?a))))
4534 (text-marker (get-register reg)))
4535 ;; If marker points to file that had markers set (and those markers
4536 ;; were saved (as e.g., in session.el), then restore those markers
4537 (if (and (consp text-marker)
4538 (eq (car text-marker) 'file-query)
4539 (or (find-buffer-visiting (nth 1 text-marker))
4540 (y-or-n-p (format "Visit file %s again? "
4541 (nth 1 text-marker)))))
4542 (save-excursion
4543 (find-file (nth 1 text-marker))
4544 (when (and (<= (nth 2 text-marker) (point-max))
4545 (<= (point-min) (nth 2 text-marker)))
4546 (setq text-marker (copy-marker (nth 2 text-marker)))
4547 (set-register reg text-marker))))
4548 (if com (viper-move-marker-locally 'viper-com-point (point)))
4549 (if (not (viper-valid-marker text-marker))
4550 (error viper-EmptyTextmarker char))
4551 (if (and (viper-same-line (point) viper-last-jump)
4552 (= (point) viper-last-jump-ignore))
4553 (push-mark viper-last-jump t)
4554 (push-mark nil t)) ; no msg
4555 (viper-register-to-point reg)
4556 (setq viper-last-jump (point-marker))
4557 (cond (skip-white
4558 (back-to-indentation)
4559 (setq viper-last-jump-ignore (point))))
4560 (if com
4561 (if (equal buff (current-buffer))
4562 (viper-execute-com (if skip-white
4563 'viper-goto-mark-and-skip-white
4564 'viper-goto-mark)
4565 nil com)
4566 (switch-to-buffer buff)
4567 (goto-char viper-com-point)
4568 (viper-change-state-to-vi)
4569 (error "Viper bell")))))
4570 ((and (not skip-white) (viper= char ?`))
4571 (if com (viper-move-marker-locally 'viper-com-point (point)))
4572 (if (and (viper-same-line (point) viper-last-jump)
4573 (= (point) viper-last-jump-ignore))
4574 (goto-char viper-last-jump))
4575 (if (null (mark t)) (error "Mark is not set in this buffer"))
4576 (if (= (point) (mark t)) (pop-mark))
4577 (exchange-point-and-mark)
4578 (setq viper-last-jump (point-marker)
4579 viper-last-jump-ignore 0)
4580 (if com (viper-execute-com 'viper-goto-mark nil com)))
4581 ((and skip-white (viper= char ?'))
4582 (if com (viper-move-marker-locally 'viper-com-point (point)))
4583 (if (and (viper-same-line (point) viper-last-jump)
4584 (= (point) viper-last-jump-ignore))
4585 (goto-char viper-last-jump))
4586 (if (= (point) (mark t)) (pop-mark))
4587 (exchange-point-and-mark)
4588 (setq viper-last-jump (point))
4589 (back-to-indentation)
4590 (setq viper-last-jump-ignore (point))
4591 (if com (viper-execute-com 'viper-goto-mark-and-skip-white nil com)))
4592 (t (error viper-InvalidTextmarker char))))
4594 (defun viper-insert-tab ()
4595 (interactive)
4596 (insert-tab))
4598 (defun viper-exchange-point-and-mark ()
4599 (interactive)
4600 (exchange-point-and-mark)
4601 (back-to-indentation))
4603 ;; Input Mode Indentation
4605 ;; Returns t, if the string before point matches the regexp STR.
4606 (defsubst viper-looking-back (str)
4607 (and (save-excursion (re-search-backward str nil t))
4608 (= (point) (match-end 0))))
4611 (defun viper-forward-indent ()
4612 "Indent forward -- `C-t' in Vi."
4613 (interactive)
4614 (setq viper-cted t)
4615 (indent-to (+ (current-column) viper-shift-width)))
4617 (defun viper-backward-indent ()
4618 "Backtab, `C-d' in Vi."
4619 (interactive)
4620 (if viper-cted
4621 (let ((p (point)) (c (current-column)) bol (indent t))
4622 (if (viper-looking-back "[0^]")
4623 (progn
4624 (if (eq ?^ (preceding-char))
4625 (setq viper-preserve-indent t))
4626 (delete-char -1)
4627 (setq p (point))
4628 (setq indent nil)))
4629 (save-excursion
4630 (beginning-of-line)
4631 (setq bol (point)))
4632 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4633 (delete-region (point) p)
4634 (if indent
4635 (indent-to (- c viper-shift-width)))
4636 (if (or (bolp) (viper-looking-back "[^ \t]"))
4637 (setq viper-cted nil)))))
4639 ;; do smart indent
4640 (defun viper-indent-line (col)
4641 (if viper-auto-indent
4642 (progn
4643 (setq viper-cted t)
4644 (if (and viper-electric-mode
4645 (not (memq major-mode '(fundamental-mode
4646 text-mode
4647 paragraph-indent-text-mode))))
4648 (indent-according-to-mode)
4649 (indent-to col)))))
4652 (defun viper-autoindent ()
4653 "Auto Indentation, Vi-style."
4654 (interactive)
4655 (let ((col (current-indentation)))
4656 (if abbrev-mode (expand-abbrev))
4657 (if viper-preserve-indent
4658 (setq viper-preserve-indent nil)
4659 (setq viper-current-indent col))
4660 ;; don't leave whitespace lines around
4661 (if (memq last-command
4662 '(viper-autoindent
4663 viper-open-line viper-Open-line
4664 viper-replace-state-exit-cmd))
4665 (indent-to-left-margin))
4666 ;; use \n instead of newline, or else <Return> will move the insert point
4667 ;;(newline 1)
4668 (insert "\n")
4669 (viper-indent-line viper-current-indent)
4673 ;; Viewing registers
4675 (defun viper-ket-function (arg)
4676 "Function called by \], the ket. View registers and call \]\]."
4677 (interactive "P")
4678 (let ((reg (read-char)))
4679 (cond ((viper-valid-register reg '(letter Letter))
4680 (view-register (downcase reg)))
4681 ((viper-valid-register reg '(digit))
4682 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4683 (with-output-to-temp-buffer " *viper-info*"
4684 (princ (format "Register %c contains the string:\n" reg))
4685 (princ text))
4687 ((viper= ?\] reg)
4688 (viper-next-heading arg))
4689 (t (error
4690 viper-InvalidRegister reg)))))
4692 (defun viper-brac-function (arg)
4693 "Function called by \[, the brac. View textmarkers and call \[\[."
4694 (interactive "P")
4695 (let ((reg (read-char)))
4696 (cond ((viper= ?\[ reg)
4697 (viper-prev-heading arg))
4698 ((viper= ?\] reg)
4699 (viper-heading-end arg))
4700 ((viper-valid-register reg '(letter))
4701 (let* ((val (get-register (viper-int-to-char (1+ (- reg ?a)))))
4702 (buf (if (not (markerp val))
4703 (error viper-EmptyTextmarker reg)
4704 (marker-buffer val)))
4705 (pos (marker-position val))
4706 line-no text (s pos) (e pos))
4707 (with-output-to-temp-buffer " *viper-info*"
4708 (if (and buf pos)
4709 (progn
4710 (with-current-buffer buf
4711 (setq line-no (1+ (count-lines (point-min) val)))
4712 (goto-char pos)
4713 (beginning-of-line)
4714 (if (re-search-backward "[^ \t]" nil t)
4715 (progn
4716 (beginning-of-line)
4717 (setq s (point))))
4718 (goto-char pos)
4719 (forward-line 1)
4720 (if (re-search-forward "[^ \t]" nil t)
4721 (progn
4722 (end-of-line)
4723 (setq e (point))))
4724 (setq text (buffer-substring s e))
4725 (setq text (format "%s<%c>%s"
4726 (substring text 0 (- pos s))
4727 reg (substring text (- pos s)))))
4728 (princ
4729 (format
4730 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4731 reg (buffer-name buf) line-no))
4732 (princ (format "Here is some text around %c:\n\n %s"
4733 reg text)))
4734 (princ (format viper-EmptyTextmarker reg))))
4736 (t (error viper-InvalidTextmarker reg)))))
4740 (defun viper-delete-backward-word (arg)
4741 "Delete previous word."
4742 (interactive "p")
4743 (save-excursion
4744 (push-mark nil t)
4745 (backward-word arg)
4746 (delete-region (point) (mark t))
4747 (pop-mark)))
4751 ;; Get viper standard value of SYMBOL. If symbol is customized, get its
4752 ;; standard value. Otherwise, get the value saved in the alist STORAGE. If
4753 ;; STORAGE is nil, use viper-saved-user-settings.
4754 (defun viper-standard-value (symbol &optional storage)
4755 (or (eval (car (get symbol 'customized-value)))
4756 (eval (car (get symbol 'saved-value)))
4757 (nth 1 (assoc symbol (or storage viper-saved-user-settings)))))
4761 (defun viper-set-expert-level (&optional dont-change-unless)
4762 "Sets the expert level for a Viper user.
4763 Can be called interactively to change (temporarily or permanently) the
4764 current expert level.
4766 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4767 the level should not be changed, unless its current value is
4768 meaningless (i.e., not one of 1,2,3,4,5).
4770 User level determines the setting of Viper variables that are most
4771 sensitive for VI-style look-and-feel."
4773 (interactive)
4775 (if (not (natnump viper-expert-level)) (setq viper-expert-level 0))
4777 (save-window-excursion
4778 (delete-other-windows)
4779 ;; if 0 < viper-expert-level < viper-max-expert-level
4780 ;; & dont-change-unless = t -- use it; else ask
4781 (viper-ask-level dont-change-unless))
4783 (setq viper-always t
4784 viper-ex-style-motion t
4785 viper-ex-style-editing t
4786 viper-want-ctl-h-help nil)
4788 (cond ((eq viper-expert-level 1) ; novice or beginner
4789 (global-set-key ; in emacs-state
4790 viper-toggle-key
4791 (if (viper-window-display-p) 'viper-iconify 'suspend-emacs))
4792 (setq viper-no-multiple-ESC t
4793 viper-re-search t
4794 viper-vi-style-in-minibuffer t
4795 viper-search-wrap-around t
4796 viper-electric-mode nil
4797 viper-want-emacs-keys-in-vi nil
4798 viper-want-emacs-keys-in-insert nil))
4800 ((and (> viper-expert-level 1) (< viper-expert-level 5))
4801 ;; intermediate to guru
4802 (setq viper-no-multiple-ESC (if (viper-window-display-p)
4803 t 'twice)
4804 viper-electric-mode t
4805 viper-want-emacs-keys-in-vi t
4806 viper-want-emacs-keys-in-insert (> viper-expert-level 2))
4808 (if (eq viper-expert-level 4) ; respect user's ex-style motion
4809 ; and viper-no-multiple-ESC
4810 (progn
4811 (setq-default
4812 viper-ex-style-editing
4813 (viper-standard-value 'viper-ex-style-editing)
4814 viper-ex-style-motion
4815 (viper-standard-value 'viper-ex-style-motion))
4816 (setq viper-ex-style-motion
4817 (viper-standard-value 'viper-ex-style-motion)
4818 viper-ex-style-editing
4819 (viper-standard-value 'viper-ex-style-editing)
4820 viper-re-search
4821 (viper-standard-value 'viper-re-search)
4822 viper-no-multiple-ESC
4823 (viper-standard-value 'viper-no-multiple-ESC)))))
4825 ;; A wizard!!
4826 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4827 ;; user toggle the values of variables.
4828 (t (setq-default viper-ex-style-editing
4829 (viper-standard-value 'viper-ex-style-editing)
4830 viper-ex-style-motion
4831 (viper-standard-value 'viper-ex-style-motion))
4832 (setq viper-want-ctl-h-help
4833 (viper-standard-value 'viper-want-ctl-h-help)
4834 viper-always
4835 (viper-standard-value 'viper-always)
4836 viper-no-multiple-ESC
4837 (viper-standard-value 'viper-no-multiple-ESC)
4838 viper-ex-style-motion
4839 (viper-standard-value 'viper-ex-style-motion)
4840 viper-ex-style-editing
4841 (viper-standard-value 'viper-ex-style-editing)
4842 viper-re-search
4843 (viper-standard-value 'viper-re-search)
4844 viper-electric-mode
4845 (viper-standard-value 'viper-electric-mode)
4846 viper-want-emacs-keys-in-vi
4847 (viper-standard-value 'viper-want-emacs-keys-in-vi)
4848 viper-want-emacs-keys-in-insert
4849 (viper-standard-value 'viper-want-emacs-keys-in-insert))))
4851 (viper-set-mode-vars-for viper-current-state)
4852 (if (or viper-always
4853 (and (> viper-expert-level 0) (> 5 viper-expert-level)))
4854 (viper-set-hooks)))
4857 ;; Ask user expert level.
4858 (defun viper-ask-level (dont-change-unless)
4859 (let ((ask-buffer " *viper-ask-level*")
4860 level-changed repeated)
4861 (save-window-excursion
4862 (switch-to-buffer ask-buffer)
4864 (while (or (> viper-expert-level viper-max-expert-level)
4865 (< viper-expert-level 1)
4866 (null dont-change-unless))
4867 (erase-buffer)
4868 (if repeated
4869 (progn
4870 (message "Invalid user level")
4871 (beep 1))
4872 (setq repeated t))
4873 (setq dont-change-unless t
4874 level-changed t)
4875 (insert "
4876 Please specify your level of familiarity with the venomous VI PERil
4877 \(and the VI Plan for Emacs Rescue).
4878 You can change it at any time by typing `M-x viper-set-expert-level RET'
4880 1 -- BEGINNER: Almost all Emacs features are suppressed.
4881 Feels almost like straight Vi. File name completion and
4882 command history in the minibuffer are thrown in as a bonus.
4883 To use Emacs productively, you must reach level 3 or higher.
4884 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4885 so most Emacs commands can be used when Viper is in Vi state.
4886 Good progress---you are well on the way to level 3!
4887 3 -- GRAND MASTER: Like 2, but most Emacs commands are available also
4888 in Viper's insert state.
4889 4 -- GURU: Like 3, but user settings are respected for viper-no-multiple-ESC,
4890 viper-ex-style-motion, viper-ex-style-editing, and
4891 viper-re-search variables. Adjust these settings to your taste.
4892 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4893 viper-electric-mode, viper-want-ctl-h-help, viper-want-emacs-keys-in-vi,
4894 and viper-want-emacs-keys-in-insert. Adjust these to your taste.
4896 Please, specify your level now: ")
4898 (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
4899 ) ; end while
4901 ;; tell the user if level was changed
4902 (and level-changed
4903 (progn
4904 (insert
4905 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4906 viper-expert-level))
4907 (if (y-or-n-p "Do you wish to make this change permanent? ")
4908 ;; save the setting for viper-expert-level
4909 (viper-save-setting
4910 'viper-expert-level
4911 (format "Saving user level %d ..." viper-expert-level)
4912 viper-custom-file-name))
4914 (bury-buffer) ; remove ask-buffer from screen
4915 (message "")
4919 (defun viper-nil ()
4920 (interactive)
4921 (beep 1))
4924 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4925 (defun viper-register-to-point (char &optional enforce-buffer)
4926 "Like `jump-to-register', but switches to another buffer in another window."
4927 (interactive "cViper register to point: ")
4928 (let ((val (get-register char)))
4929 (cond
4930 ((and (fboundp 'frame-configuration-p)
4931 (frame-configuration-p val))
4932 (set-frame-configuration val))
4933 ((window-configuration-p val)
4934 (set-window-configuration val))
4935 ((viper-valid-marker val)
4936 (if (and enforce-buffer
4937 (not (equal (current-buffer) (marker-buffer val))))
4938 (error (concat viper-EmptyTextmarker " in this buffer")
4939 (viper-int-to-char (1- (+ char ?a)))))
4940 (pop-to-buffer (marker-buffer val))
4941 (goto-char val))
4942 ((and (consp val) (eq (car val) 'file))
4943 (find-file (cdr val)))
4945 (error viper-EmptyTextmarker (viper-int-to-char (1- (+ char ?a))))))))
4948 (defun viper-save-kill-buffer ()
4949 "Save then kill current buffer."
4950 (interactive)
4951 (if (< viper-expert-level 2)
4952 (save-buffers-kill-emacs)
4953 (save-buffer)
4954 (kill-buffer (current-buffer))))
4958 ;;; Bug Report
4960 (defun viper-submit-report ()
4961 "Submit bug report on Viper."
4962 (interactive)
4963 (let ((reporter-prompt-for-summary-p t)
4964 (viper-device-type (viper-device-type))
4965 color-display-p frame-parameters
4966 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4967 varlist salutation window-config)
4969 ;; If mode info is needed, add variable to `let' and then set it below,
4970 ;; like we did with color-display-p.
4971 (setq color-display-p (if (viper-window-display-p)
4972 (viper-color-display-p)
4973 'non-x)
4974 minibuffer-vi-face (if (viper-has-face-support-p)
4975 (viper-get-face viper-minibuffer-vi-face)
4976 'non-x)
4977 minibuffer-insert-face (if (viper-has-face-support-p)
4978 (viper-get-face
4979 viper-minibuffer-insert-face)
4980 'non-x)
4981 minibuffer-emacs-face (if (viper-has-face-support-p)
4982 (viper-get-face
4983 viper-minibuffer-emacs-face)
4984 'non-x)
4985 frame-parameters (if (fboundp 'frame-parameters)
4986 (frame-parameters (selected-frame))))
4988 (setq varlist (list 'viper-vi-minibuffer-minor-mode
4989 'viper-insert-minibuffer-minor-mode
4990 'viper-vi-intercept-minor-mode
4991 'viper-vi-local-user-minor-mode
4992 'viper-vi-kbd-minor-mode
4993 'viper-vi-global-user-minor-mode
4994 'viper-vi-state-modifier-minor-mode
4995 'viper-vi-diehard-minor-mode
4996 'viper-vi-basic-minor-mode
4997 'viper-replace-minor-mode
4998 'viper-insert-intercept-minor-mode
4999 'viper-insert-local-user-minor-mode
5000 'viper-insert-kbd-minor-mode
5001 'viper-insert-global-user-minor-mode
5002 'viper-insert-state-modifier-minor-mode
5003 'viper-insert-diehard-minor-mode
5004 'viper-insert-basic-minor-mode
5005 'viper-emacs-intercept-minor-mode
5006 'viper-emacs-local-user-minor-mode
5007 'viper-emacs-kbd-minor-mode
5008 'viper-emacs-global-user-minor-mode
5009 'viper-emacs-state-modifier-minor-mode
5010 'viper-automatic-iso-accents
5011 'viper-special-input-method
5012 'viper-want-emacs-keys-in-insert
5013 'viper-want-emacs-keys-in-vi
5014 'viper-keep-point-on-undo
5015 'viper-no-multiple-ESC
5016 'viper-electric-mode
5017 'viper-ESC-key
5018 'viper-want-ctl-h-help
5019 'viper-ex-style-editing
5020 'viper-delete-backwards-in-replace
5021 'viper-vi-style-in-minibuffer
5022 'viper-vi-state-hook
5023 'viper-insert-state-hook
5024 'viper-replace-state-hook
5025 'viper-emacs-state-hook
5026 'ex-cycle-other-window
5027 'ex-cycle-through-non-files
5028 'viper-expert-level
5029 'major-mode
5030 'viper-device-type
5031 'color-display-p
5032 'frame-parameters
5033 'minibuffer-vi-face
5034 'minibuffer-insert-face
5035 'minibuffer-emacs-face
5037 (setq salutation "
5038 Congratulations! You may have unearthed a bug in Viper!
5039 Please mail a concise, accurate summary of the problem to the address above.
5041 -------------------------------------------------------------------")
5042 (setq window-config (current-window-configuration))
5043 (with-output-to-temp-buffer " *viper-info*"
5044 (switch-to-buffer " *viper-info*")
5045 (delete-other-windows)
5046 (princ "
5047 PLEASE FOLLOW THESE PROCEDURES
5048 ------------------------------
5050 Before reporting a bug, please verify that it is related to Viper, and is
5051 not caused by other packages you are using.
5053 Don't report compilation warnings, unless you are certain that there is a
5054 problem. These warnings are normal and unavoidable.
5056 Please note that users should not modify variables and keymaps other than
5057 those advertised in the manual. Such `customization' is likely to crash
5058 Viper, as it would any other improperly customized Emacs package.
5060 If you are reporting an error message received while executing one of the
5061 Viper commands, type:
5063 M-x set-variable <Return> debug-on-error <Return> t <Return>
5065 Then reproduce the error. The above command will cause Emacs to produce a
5066 back trace of the execution that leads to the error. Please include this
5067 trace in your bug report.
5069 If you believe that one of Viper's commands goes into an infinite loop
5070 \(e.g., Emacs freezes\), type:
5072 M-x set-variable <Return> debug-on-quit <Return> t <Return>
5074 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
5075 the current command. Include the resulting back trace in the bug report.
5077 Mail anyway (y or n)? ")
5078 (if (y-or-n-p "Mail anyway? ")
5080 (set-window-configuration window-config)
5081 (error "Bug report aborted")))
5083 (require 'reporter)
5084 (set-window-configuration window-config)
5086 (reporter-submit-bug-report "kifer@cs.stonybrook.edu"
5087 (viper-version)
5088 varlist
5089 nil 'delete-other-windows
5090 salutation)
5096 ;; arch-tag: 739a6450-5fda-44d0-88b0-325053d888c2
5097 ;;; viper-cmd.el ends here