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