2000-10-29 Michael Kifer <kifer@cs.sunysb.edu>
[emacs.git] / lisp / emulation / viper-cmd.el
blobb97995b5458c60292f1a5702a54a0d93983cc62a
1 ;;; viper-cmd.el --- Vi command support for Viper
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
7 ;; it under the terms of the GNU General Public License as published by
8 ;; the Free Software Foundation; either version 2, or (at your option)
9 ;; any later version.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;; GNU General Public License for more details.
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with GNU Emacs; see the file COPYING. If not, write to the
18 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 ;; Boston, MA 02111-1307, USA.
21 ;; Code
23 (provide 'viper-cmd)
24 (require 'advice)
26 ;; Compiler pacifier
27 (defvar viper-minibuffer-current-face)
28 (defvar viper-minibuffer-insert-face)
29 (defvar viper-minibuffer-vi-face)
30 (defvar viper-minibuffer-emacs-face)
31 (defvar viper-always)
32 (defvar viper-mode-string)
33 (defvar viper-custom-file-name)
34 (defvar iso-accents-mode)
35 (defvar quail-mode)
36 (defvar quail-current-str)
37 (defvar zmacs-region-stays)
38 (defvar mark-even-if-inactive)
40 ;; loading happens only in non-interactive compilation
41 ;; in order to spare non-viperized emacs from being viperized
42 (if noninteractive
43 (eval-when-compile
44 (let ((load-path (cons (expand-file-name ".") load-path)))
45 (or (featurep 'viper-util)
46 (load "viper-util.el" nil nil 'nosuffix))
47 (or (featurep 'viper-keym)
48 (load "viper-keym.el" nil nil 'nosuffix))
49 (or (featurep 'viper-mous)
50 (load "viper-mous.el" nil nil 'nosuffix))
51 (or (featurep 'viper-macs)
52 (load "viper-macs.el" nil nil 'nosuffix))
53 (or (featurep 'viper-ex)
54 (load "viper-ex.el" nil nil 'nosuffix))
55 )))
56 ;; end pacifier
59 (require 'viper-util)
60 (require 'viper-keym)
61 (require 'viper-mous)
62 (require 'viper-macs)
63 (require 'viper-ex)
67 ;; Generic predicates
69 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
71 ;; generate test functions
72 ;; given symbol foo, foo-p is the test function, foos is the set of
73 ;; Viper command keys
74 ;; (macroexpand '(viper-test-com-defun foo))
75 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
77 (defmacro viper-test-com-defun (name)
78 (let* ((snm (symbol-name name))
79 (nm-p (intern (concat snm "-p")))
80 (nms (intern (concat snm "s"))))
81 `(defun ,nm-p (com)
82 (consp (viper-memq-char
83 (if (and (viper-characterp com) (< com 0))
84 (- com) com)
85 ,nms)
86 ))))
88 ;; Variables for defining VI commands
90 ;; Modifying commands that can be prefixes to movement commands
91 (defconst viper-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
92 ;; define viper-prefix-command-p
93 (viper-test-com-defun viper-prefix-command)
95 ;; Commands that are pairs eg. dd. r and R here are a hack
96 (defconst viper-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
97 ;; define viper-charpair-command-p
98 (viper-test-com-defun viper-charpair-command)
100 (defconst viper-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
101 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
102 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
103 ?\; ?, ?0 ?? ?/ ?\ ?\C-m
104 space return
105 delete backspace
107 "Movement commands")
108 ;; define viper-movement-command-p
109 (viper-test-com-defun viper-movement-command)
111 ;; Vi digit commands
112 (defconst viper-digit-commands '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
114 ;; define viper-digit-command-p
115 (viper-test-com-defun viper-digit-command)
117 ;; Commands that can be repeated by . (dotted)
118 (defconst viper-dotable-commands '(?c ?d ?C ?s ?S ?D ?> ?<))
119 ;; define viper-dotable-command-p
120 (viper-test-com-defun viper-dotable-command)
122 ;; Commands that can follow a #
123 (defconst viper-hash-commands '(?c ?C ?g ?q ?s))
124 ;; define viper-hash-command-p
125 (viper-test-com-defun viper-hash-command)
127 ;; Commands that may have registers as prefix
128 (defconst viper-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
129 ;; define viper-regsuffix-command-p
130 (viper-test-com-defun viper-regsuffix-command)
132 (defconst viper-vi-commands (append viper-movement-commands
133 viper-digit-commands
134 viper-dotable-commands
135 viper-charpair-commands
136 viper-hash-commands
137 viper-prefix-commands
138 viper-regsuffix-commands)
139 "The list of all commands in Vi-state.")
140 ;; define viper-vi-command-p
141 (viper-test-com-defun viper-vi-command)
143 ;; Where viper saves mark. This mark is resurrected by m^
144 (defvar viper-saved-mark nil)
148 ;;; CODE
150 ;; sentinels
152 ;; Runs viper-after-change-functions inside after-change-functions
153 (defun viper-after-change-sentinel (beg end len)
154 (run-hook-with-args 'viper-after-change-functions beg end len))
156 ;; Runs viper-before-change-functions inside before-change-functions
157 (defun viper-before-change-sentinel (beg end)
158 (run-hook-with-args 'viper-before-change-functions beg end))
160 (defsubst viper-post-command-sentinel ()
161 (run-hooks 'viper-post-command-hooks)
162 (if (eq viper-current-state 'vi-state)
163 (viper-restore-cursor-color 'after-insert-mode)))
165 (defsubst viper-pre-command-sentinel ()
166 (run-hooks 'viper-pre-command-hooks))
168 ;; Needed so that Viper will be able to figure the last inserted
169 ;; chunk of text with reasonable accuracy.
170 (defsubst viper-insert-state-post-command-sentinel ()
171 (if (and (memq viper-current-state '(insert-state replace-state))
172 viper-insert-point
173 (>= (point) viper-insert-point))
174 (setq viper-last-posn-while-in-insert-state (point-marker)))
175 (or (viper-overlay-p viper-replace-overlay)
176 (progn
177 (viper-set-replace-overlay (point-min) (point-min))
178 (viper-hide-replace-overlay)))
179 (if (eq viper-current-state 'insert-state)
180 (let ((has-saved-cursor-color-in-insert-mode
181 (stringp (viper-get-saved-cursor-color-in-insert-mode))))
182 (or has-saved-cursor-color-in-insert-mode
183 (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
184 ;; save current color, if not already saved
185 (viper-save-cursor-color 'before-insert-mode))
186 ;; set insert mode cursor color
187 (viper-change-cursor-color viper-insert-state-cursor-color)))
189 (if (and (memq this-command '(dabbrev-expand hippie-expand))
190 (integerp viper-pre-command-point)
191 (markerp viper-insert-point)
192 (marker-position viper-insert-point)
193 (> viper-insert-point viper-pre-command-point))
194 (viper-move-marker-locally viper-insert-point viper-pre-command-point))
197 (defsubst viper-preserve-cursor-color ()
198 (or (memq this-command '(self-insert-command
199 viper-del-backward-char-in-insert
200 viper-del-backward-char-in-replace
201 viper-delete-backward-char
202 viper-join-lines
203 viper-delete-char))
204 (memq (viper-event-key last-command-event)
205 '(up down left right (meta f) (meta b)
206 (control n) (control p) (control f) (control b)))))
208 (defsubst viper-insert-state-pre-command-sentinel ()
209 (or (viper-preserve-cursor-color)
210 (viper-restore-cursor-color 'after-insert-mode))
211 (if (and (memq this-command '(dabbrev-expand hippie-expand))
212 (markerp viper-insert-point)
213 (marker-position viper-insert-point))
214 (setq viper-pre-command-point (marker-position viper-insert-point))))
216 (defsubst viper-R-state-post-command-sentinel ()
217 ;; Restoring cursor color is needed despite
218 ;; viper-replace-state-pre-command-sentinel: When you jump to another buffer
219 ;; in another frame, the pre-command hook won't change cursor color to
220 ;; default in that other frame. So, if the second frame cursor was red and
221 ;; we set the point outside the replacement region, then the cursor color
222 ;; will remain red. Restoring the default, below, prevents this.
223 (if (and (<= (viper-replace-start) (point))
224 (<= (point) (viper-replace-end)))
225 (viper-change-cursor-color viper-replace-overlay-cursor-color)
226 (viper-restore-cursor-color 'after-replace-mode)
229 ;; to speed up, don't change cursor color before self-insert
230 ;; and common move commands
231 (defsubst viper-replace-state-pre-command-sentinel ()
232 (or (viper-preserve-cursor-color)
233 (viper-restore-cursor-color 'after-replace-mode)))
236 ;; Make sure we don't delete more than needed.
237 ;; This is executed at viper-last-posn-in-replace-region
238 (defsubst viper-trim-replace-chars-to-delete-if-necessary ()
239 (setq viper-replace-chars-to-delete
240 (max 0
241 (min viper-replace-chars-to-delete
242 ;; Don't delete more than to the end of repl overlay
243 (viper-chars-in-region
244 (viper-replace-end) viper-last-posn-in-replace-region)
245 ;; point is viper-last-posn-in-replace-region now
246 ;; So, this limits deletion to the end of line
247 (viper-chars-in-region (point) (viper-line-pos 'end))
248 ))))
251 (defun viper-replace-state-post-command-sentinel ()
252 ;; Restoring cursor color is needed despite
253 ;; viper-replace-state-pre-command-sentinel: When one jumps to another buffer
254 ;; in another frame, the pre-command hook won't change cursor color to
255 ;; default in that other frame. So, if the second frame cursor was red and
256 ;; we set the point outside the replacement region, then the cursor color
257 ;; will remain red. Restoring the default, below, fixes this problem.
259 ;; We optimize for some commands, like self-insert-command,
260 ;; viper-delete-backward-char, etc., since they either don't change
261 ;; cursor color or, if they terminate replace mode, the color will be changed
262 ;; in viper-finish-change
263 (or (viper-preserve-cursor-color)
264 (viper-restore-cursor-color 'after-replace-mode))
265 (cond
266 ((eq viper-current-state 'replace-state)
267 ;; delete characters to compensate for inserted chars.
268 (let ((replace-boundary (viper-replace-end)))
269 (save-excursion
270 (goto-char viper-last-posn-in-replace-region)
271 (viper-trim-replace-chars-to-delete-if-necessary)
272 (delete-char viper-replace-chars-to-delete)
273 (setq viper-replace-chars-to-delete 0)
274 ;; terminate replace mode if reached replace limit
275 (if (= viper-last-posn-in-replace-region (viper-replace-end))
276 (viper-finish-change)))
278 (if (viper-pos-within-region
279 (point) (viper-replace-start) replace-boundary)
280 (progn
281 ;; the state may have changed in viper-finish-change above
282 (if (eq viper-current-state 'replace-state)
283 (viper-change-cursor-color viper-replace-overlay-cursor-color))
284 (setq viper-last-posn-in-replace-region (point-marker))))
286 ;; terminate replace mode if changed Viper states.
287 (t (viper-finish-change))))
290 ;; changing mode
292 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
293 (defun viper-change-state (new-state)
294 ;; Keep viper-post/pre-command-hooks fresh.
295 ;; We remove then add viper-post/pre-command-sentinel since it is very
296 ;; desirable that viper-pre-command-sentinel is the last hook and
297 ;; viper-post-command-sentinel is the first hook.
299 (make-local-hook 'viper-after-change-functions)
300 (make-local-hook 'viper-before-change-functions)
301 (make-local-hook 'viper-post-command-hooks)
302 (make-local-hook 'viper-pre-command-hooks)
304 (remove-hook 'post-command-hook 'viper-post-command-sentinel)
305 (add-hook 'post-command-hook 'viper-post-command-sentinel)
306 (remove-hook 'pre-command-hook 'viper-pre-command-sentinel)
307 (add-hook 'pre-command-hook 'viper-pre-command-sentinel t)
308 ;; These hooks will be added back if switching to insert/replace mode
309 (remove-hook 'viper-post-command-hooks
310 'viper-insert-state-post-command-sentinel 'local)
311 (remove-hook 'viper-pre-command-hooks
312 'viper-insert-state-pre-command-sentinel 'local)
313 (setq viper-intermediate-command nil)
314 (cond ((eq new-state 'vi-state)
315 (cond ((member viper-current-state '(insert-state replace-state))
317 ;; move viper-last-posn-while-in-insert-state
318 ;; This is a normal hook that is executed in insert/replace
319 ;; states after each command. In Vi/Emacs state, it does
320 ;; nothing. We need to execute it here to make sure that
321 ;; the last posn was recorded when we hit ESC.
322 ;; It may be left unrecorded if the last thing done in
323 ;; insert/repl state was dabbrev-expansion or abbrev
324 ;; expansion caused by hitting ESC
325 (viper-insert-state-post-command-sentinel)
327 (condition-case conds
328 (progn
329 (viper-save-last-insertion
330 viper-insert-point
331 viper-last-posn-while-in-insert-state)
332 (if viper-began-as-replace
333 (setq viper-began-as-replace nil)
334 ;; repeat insert commands if numerical arg > 1
335 (save-excursion
336 (viper-repeat-insert-command))))
337 (error
338 (viper-message-conditions conds)))
340 (if (> (length viper-last-insertion) 0)
341 (viper-push-onto-ring viper-last-insertion
342 'viper-insertion-ring))
344 (if viper-ESC-moves-cursor-back
345 (or (bolp) (backward-char 1))))
348 ;; insert or replace
349 ((memq new-state '(insert-state replace-state))
350 (if (memq viper-current-state '(emacs-state vi-state))
351 (viper-move-marker-locally 'viper-insert-point (point)))
352 (viper-move-marker-locally
353 'viper-last-posn-while-in-insert-state (point))
354 (add-hook 'viper-post-command-hooks
355 'viper-insert-state-post-command-sentinel t 'local)
356 (add-hook 'viper-pre-command-hooks
357 'viper-insert-state-pre-command-sentinel t 'local))
358 ) ; outermost cond
360 ;; Nothing needs to be done to switch to emacs mode! Just set some
361 ;; variables, which is already done in viper-change-state-to-emacs!
363 ;; ISO accents
364 ;; always turn off iso-accents-mode in vi-state, or else we won't be able to
365 ;; use the keys `,',^ , as they will do accents instead of Vi actions.
366 (cond ((eq new-state 'vi-state) (viper-set-iso-accents-mode nil));accents off
367 (viper-automatic-iso-accents (viper-set-iso-accents-mode t));accents on
368 (t (viper-set-iso-accents-mode nil)))
369 ;; Always turn off quail mode in vi state
370 (cond ((eq new-state 'vi-state) (viper-set-input-method nil)) ;intl input off
371 (viper-special-input-method (viper-set-input-method t)) ;intl input on
372 (t (viper-set-input-method nil)))
374 (setq viper-current-state new-state)
376 (viper-update-syntax-classes)
377 (viper-normalize-minor-mode-map-alist)
378 (viper-adjust-keys-for new-state)
379 (viper-set-mode-vars-for new-state)
380 (viper-refresh-mode-line)
385 (defun viper-adjust-keys-for (state)
386 "Make necessary adjustments to keymaps before entering STATE."
387 (cond ((memq state '(insert-state replace-state))
388 (if viper-auto-indent
389 (progn
390 (define-key viper-insert-basic-map "\C-m" 'viper-autoindent)
391 (if viper-want-emacs-keys-in-insert
392 ;; expert
393 (define-key viper-insert-basic-map "\C-j" nil)
394 ;; novice
395 (define-key viper-insert-basic-map "\C-j" 'viper-autoindent)))
396 (define-key viper-insert-basic-map "\C-m" nil)
397 (define-key viper-insert-basic-map "\C-j" nil))
399 (setq viper-insert-diehard-minor-mode
400 (not viper-want-emacs-keys-in-insert))
402 (if viper-want-ctl-h-help
403 (progn
404 (define-key viper-insert-basic-map "\C-h" 'help-command)
405 (define-key viper-replace-map "\C-h" 'help-command))
406 (define-key viper-insert-basic-map
407 "\C-h" 'viper-del-backward-char-in-insert)
408 (define-key viper-replace-map
409 "\C-h" 'viper-del-backward-char-in-replace))
410 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
411 (define-key viper-insert-basic-map
412 [backspace] 'viper-del-backward-char-in-insert)
413 (define-key viper-replace-map
414 [backspace] 'viper-del-backward-char-in-replace)
415 ) ; end insert/replace case
416 (t ; Vi state
417 (setq viper-vi-diehard-minor-mode (not viper-want-emacs-keys-in-vi))
418 (if viper-want-ctl-h-help
419 (define-key viper-vi-basic-map "\C-h" 'help-command)
420 (define-key viper-vi-basic-map "\C-h" 'viper-backward-char))
421 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
422 (define-key viper-vi-basic-map [backspace] 'viper-backward-char))
426 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
427 ;; This ensures that Viper bindings are in effect, regardless of which minor
428 ;; modes were turned on by the user or by other packages.
429 (defun viper-normalize-minor-mode-map-alist ()
430 (setq minor-mode-map-alist
431 (viper-append-filter-alist
432 (list (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map)
433 (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map)
434 (cons 'viper-vi-local-user-minor-mode viper-vi-local-user-map)
435 (cons 'viper-vi-kbd-minor-mode viper-vi-kbd-map)
436 (cons 'viper-vi-global-user-minor-mode viper-vi-global-user-map)
437 (cons 'viper-vi-state-modifier-minor-mode
438 (if (keymapp
439 (cdr (assoc major-mode
440 viper-vi-state-modifier-alist)))
441 (cdr (assoc major-mode viper-vi-state-modifier-alist))
442 viper-empty-keymap))
443 (cons 'viper-vi-diehard-minor-mode viper-vi-diehard-map)
444 (cons 'viper-vi-basic-minor-mode viper-vi-basic-map)
445 (cons 'viper-insert-intercept-minor-mode
446 viper-insert-intercept-map)
447 (cons 'viper-replace-minor-mode viper-replace-map)
448 ;; viper-insert-minibuffer-minor-mode must come after
449 ;; viper-replace-minor-mode
450 (cons 'viper-insert-minibuffer-minor-mode
451 viper-minibuffer-map)
452 (cons 'viper-insert-local-user-minor-mode
453 viper-insert-local-user-map)
454 (cons 'viper-insert-kbd-minor-mode viper-insert-kbd-map)
455 (cons 'viper-insert-global-user-minor-mode
456 viper-insert-global-user-map)
457 (cons 'viper-insert-state-modifier-minor-mode
458 (if (keymapp
459 (cdr (assoc major-mode
460 viper-insert-state-modifier-alist)))
461 (cdr (assoc major-mode
462 viper-insert-state-modifier-alist))
463 viper-empty-keymap))
464 (cons 'viper-insert-diehard-minor-mode viper-insert-diehard-map)
465 (cons 'viper-insert-basic-minor-mode viper-insert-basic-map)
466 (cons 'viper-emacs-intercept-minor-mode
467 viper-emacs-intercept-map)
468 (cons 'viper-emacs-local-user-minor-mode
469 viper-emacs-local-user-map)
470 (cons 'viper-emacs-kbd-minor-mode viper-emacs-kbd-map)
471 (cons 'viper-emacs-global-user-minor-mode
472 viper-emacs-global-user-map)
473 (cons 'viper-emacs-state-modifier-minor-mode
474 (if (keymapp
475 (cdr
476 (assoc major-mode viper-emacs-state-modifier-alist)))
477 (cdr
478 (assoc major-mode viper-emacs-state-modifier-alist))
479 viper-empty-keymap))
481 minor-mode-map-alist)))
485 ;; Viper mode-changing commands and utilities
487 ;; Modifies mode-line-buffer-identification.
488 (defun viper-refresh-mode-line ()
489 (setq viper-mode-string
490 (cond ((eq viper-current-state 'emacs-state) viper-emacs-state-id)
491 ((eq viper-current-state 'vi-state) viper-vi-state-id)
492 ((eq viper-current-state 'replace-state) viper-replace-state-id)
493 ((eq viper-current-state 'insert-state) viper-insert-state-id)))
495 ;; Sets Viper mode string in global-mode-string
496 (force-mode-line-update))
499 ;; Switch from Insert state to Vi state.
500 (defun viper-exit-insert-state ()
501 (interactive)
502 (viper-change-state-to-vi))
504 (defun viper-set-mode-vars-for (state)
505 "Sets Viper minor mode variables to put Viper's state STATE in effect."
507 ;; Emacs state
508 (setq viper-vi-minibuffer-minor-mode nil
509 viper-insert-minibuffer-minor-mode nil
510 viper-vi-intercept-minor-mode nil
511 viper-insert-intercept-minor-mode nil
513 viper-vi-local-user-minor-mode nil
514 viper-vi-kbd-minor-mode nil
515 viper-vi-global-user-minor-mode nil
516 viper-vi-state-modifier-minor-mode nil
517 viper-vi-diehard-minor-mode nil
518 viper-vi-basic-minor-mode nil
520 viper-replace-minor-mode nil
522 viper-insert-local-user-minor-mode nil
523 viper-insert-kbd-minor-mode nil
524 viper-insert-global-user-minor-mode nil
525 viper-insert-state-modifier-minor-mode nil
526 viper-insert-diehard-minor-mode nil
527 viper-insert-basic-minor-mode nil
528 viper-emacs-intercept-minor-mode t
529 viper-emacs-local-user-minor-mode t
530 viper-emacs-kbd-minor-mode (not (viper-is-in-minibuffer))
531 viper-emacs-global-user-minor-mode t
532 viper-emacs-state-modifier-minor-mode t
535 ;; Vi state
536 (if (eq state 'vi-state) ; adjust for vi-state
537 (setq
538 viper-vi-intercept-minor-mode t
539 viper-vi-minibuffer-minor-mode (viper-is-in-minibuffer)
540 viper-vi-local-user-minor-mode t
541 viper-vi-kbd-minor-mode (not (viper-is-in-minibuffer))
542 viper-vi-global-user-minor-mode t
543 viper-vi-state-modifier-minor-mode t
544 ;; don't let the diehard keymap block command completion
545 ;; and other things in the minibuffer
546 viper-vi-diehard-minor-mode (not
547 (or viper-want-emacs-keys-in-vi
548 (viper-is-in-minibuffer)))
549 viper-vi-basic-minor-mode t
550 viper-emacs-intercept-minor-mode nil
551 viper-emacs-local-user-minor-mode nil
552 viper-emacs-kbd-minor-mode nil
553 viper-emacs-global-user-minor-mode nil
554 viper-emacs-state-modifier-minor-mode nil
557 ;; Insert and Replace states
558 (if (member state '(insert-state replace-state))
559 (setq
560 viper-insert-intercept-minor-mode t
561 viper-replace-minor-mode (eq state 'replace-state)
562 viper-insert-minibuffer-minor-mode (viper-is-in-minibuffer)
563 viper-insert-local-user-minor-mode t
564 viper-insert-kbd-minor-mode (not (viper-is-in-minibuffer))
565 viper-insert-global-user-minor-mode t
566 viper-insert-state-modifier-minor-mode t
567 ;; don't let the diehard keymap block command completion
568 ;; and other things in the minibuffer
569 viper-insert-diehard-minor-mode (not
571 viper-want-emacs-keys-in-insert
572 (viper-is-in-minibuffer)))
573 viper-insert-basic-minor-mode t
574 viper-emacs-intercept-minor-mode nil
575 viper-emacs-local-user-minor-mode nil
576 viper-emacs-kbd-minor-mode nil
577 viper-emacs-global-user-minor-mode nil
578 viper-emacs-state-modifier-minor-mode nil
581 ;; minibuffer faces
582 (if (viper-has-face-support-p)
583 (setq viper-minibuffer-current-face
584 (cond ((eq state 'emacs-state) viper-minibuffer-emacs-face)
585 ((eq state 'vi-state) viper-minibuffer-vi-face)
586 ((memq state '(insert-state replace-state))
587 viper-minibuffer-insert-face))))
589 (if (viper-is-in-minibuffer)
590 (viper-set-minibuffer-overlay))
593 ;; This also takes care of the annoying incomplete lines in files.
594 ;; Also, this fixes `undo' to work vi-style for complex commands.
595 (defun viper-change-state-to-vi ()
596 "Change Viper state to Vi."
597 (interactive)
598 (if (and viper-first-time (not (viper-is-in-minibuffer)))
599 (viper-mode)
600 (if overwrite-mode (overwrite-mode nil))
601 (or (viper-overlay-p viper-replace-overlay)
602 (viper-set-replace-overlay (point-min) (point-min)))
603 (viper-hide-replace-overlay)
604 (if abbrev-mode (expand-abbrev))
605 (if (and auto-fill-function (> (current-column) fill-column))
606 (funcall auto-fill-function))
607 ;; don't leave whitespace lines around
608 (if (and (memq last-command
609 '(viper-autoindent
610 viper-open-line viper-Open-line
611 viper-replace-state-exit-cmd))
612 (viper-over-whitespace-line))
613 (indent-to-left-margin))
614 (viper-add-newline-at-eob-if-necessary)
615 (viper-adjust-undo)
616 (viper-change-state 'vi-state)
618 (viper-restore-cursor-color 'after-insert-mode)
620 ;; Protect against user errors in hooks
621 (condition-case conds
622 (run-hooks 'viper-vi-state-hook)
623 (error
624 (viper-message-conditions conds)))))
626 (defun viper-change-state-to-insert ()
627 "Change Viper state to Insert."
628 (interactive)
629 (viper-change-state 'insert-state)
631 (or (viper-overlay-p viper-replace-overlay)
632 (viper-set-replace-overlay (point-min) (point-min)))
633 (viper-hide-replace-overlay)
635 (let ((has-saved-cursor-color-in-insert-mode
636 (stringp (viper-get-saved-cursor-color-in-insert-mode))))
637 (or has-saved-cursor-color-in-insert-mode
638 (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
639 (viper-save-cursor-color 'before-insert-mode))
640 (viper-change-cursor-color viper-insert-state-cursor-color))
642 ;; Protect against user errors in hooks
643 (condition-case conds
644 (run-hooks 'viper-insert-state-hook)
645 (error
646 (viper-message-conditions conds))))
648 (defsubst viper-downgrade-to-insert ()
649 (setq viper-current-state 'insert-state
650 viper-replace-minor-mode nil))
654 ;; Change to replace state. When the end of replacement region is reached,
655 ;; replace state changes to insert state.
656 (defun viper-change-state-to-replace (&optional non-R-cmd)
657 (viper-change-state 'replace-state)
658 ;; Run insert-state-hook
659 (condition-case conds
660 (run-hooks 'viper-insert-state-hook 'viper-replace-state-hook)
661 (error
662 (viper-message-conditions conds)))
664 (if non-R-cmd
665 (viper-start-replace)
666 ;; 'R' is implemented using Emacs's overwrite-mode
667 (viper-start-R-mode))
671 (defun viper-change-state-to-emacs ()
672 "Change Viper state to Emacs."
673 (interactive)
674 (or (viper-overlay-p viper-replace-overlay)
675 (viper-set-replace-overlay (point-min) (point-min)))
676 (viper-hide-replace-overlay)
677 (viper-change-state 'emacs-state)
679 ;; Protect agains user errors in hooks
680 (condition-case conds
681 (run-hooks 'viper-emacs-state-hook)
682 (error
683 (viper-message-conditions conds))))
685 ;; escape to emacs mode termporarily
686 (defun viper-escape-to-emacs (arg &optional events)
687 "Escape to Emacs state from Vi state for one Emacs command.
688 ARG is used as the prefix value for the executed command. If
689 EVENTS is a list of events, which become the beginning of the command."
690 (interactive "P")
691 (if (viper= last-command-char ?\\)
692 (message "Switched to EMACS state for the next command..."))
693 (viper-escape-to-state arg events 'emacs-state))
695 ;; escape to Vi mode termporarily
696 (defun viper-escape-to-vi (arg)
697 "Escape from Emacs state to Vi state for one Vi 1-character command.
698 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
699 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
700 `viper-escape-to-vi' is used."
701 (interactive "P")
702 (message "Switched to VI state for the next command...")
703 (viper-escape-to-state arg nil 'vi-state))
705 ;; Escape to STATE mode for one Emacs command.
706 (defun viper-escape-to-state (arg events state)
707 ;;(let (com key prefix-arg)
708 (let (com key)
709 ;; this temporarily turns off Viper's minor mode keymaps
710 (viper-set-mode-vars-for state)
711 (viper-normalize-minor-mode-map-alist)
712 (if events (viper-set-unread-command-events events))
714 ;; protect against keyboard quit and other errors
715 (condition-case nil
716 (let (viper-vi-kbd-minor-mode
717 viper-insert-kbd-minor-mode
718 viper-emacs-kbd-minor-mode)
719 (unwind-protect
720 (progn
721 (setq com (key-binding (setq key
722 (if viper-xemacs-p
723 (read-key-sequence nil)
724 (read-key-sequence nil t)))))
725 ;; In case of binding indirection--chase definitions.
726 ;; Have to do it here because we execute this command under
727 ;; different keymaps, so command-execute may not do the
728 ;; right thing there
729 (while (vectorp com) (setq com (key-binding com))))
730 nil)
731 ;; Execute command com in the original Viper state, not in state
732 ;; `state'. Otherwise, if we switch buffers while executing the
733 ;; escaped to command, Viper's mode vars will remain those of
734 ;; `state'. When we return to the orig buffer, the bindings will be
735 ;; screwed up.
736 (viper-set-mode-vars-for viper-current-state)
738 ;; this-command, last-command-char, last-command-event
739 (setq this-command com)
740 (if viper-xemacs-p ; XEmacs represents key sequences as vectors
741 (setq last-command-event
742 (viper-copy-event (viper-seq-last-elt key))
743 last-command-char (event-to-character last-command-event))
744 ;; Emacs represents them as sequences (str or vec)
745 (setq last-command-event
746 (viper-copy-event (viper-seq-last-elt key))
747 last-command-char last-command-event))
749 (if (commandp com)
750 (progn
751 (setq prefix-arg (or prefix-arg arg))
752 (command-execute com)))
754 (quit (ding))
755 (error (beep 1))))
756 ;; set state in the new buffer
757 (viper-set-mode-vars-for viper-current-state))
759 (defun viper-exec-form-in-vi (form)
760 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
761 (let ((buff (current-buffer))
762 result)
763 (viper-set-mode-vars-for 'vi-state)
765 (condition-case nil
766 (let (viper-vi-kbd-minor-mode) ; execute without kbd macros
767 (setq result (eval form))
769 (error
770 (signal 'quit nil)))
772 (if (not (equal buff (current-buffer))) ; cmd switched buffer
773 (save-excursion
774 (set-buffer buff)
775 (viper-set-mode-vars-for viper-current-state)))
776 (viper-set-mode-vars-for viper-current-state)
777 result))
779 (defun viper-exec-form-in-emacs (form)
780 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
781 Similar to viper-escape-to-emacs, but accepts forms rather than keystrokes."
782 (let ((buff (current-buffer))
783 result)
784 (viper-set-mode-vars-for 'emacs-state)
785 (setq result (eval form))
786 (if (not (equal buff (current-buffer))) ; cmd switched buffer
787 (save-excursion
788 (set-buffer buff)
789 (viper-set-mode-vars-for viper-current-state)))
790 (viper-set-mode-vars-for viper-current-state)
791 result))
794 ;; This is needed because minor modes sometimes override essential Viper
795 ;; bindings. By letting Viper know which files these modes are in, it will
796 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
797 (defun viper-harness-minor-mode (load-file)
798 "Familiarize Viper with a minor mode defined in LOAD_FILE.
799 Minor modes that have their own keymaps may overshadow Viper keymaps.
800 This function is designed to make Viper aware of the packages that define
801 such minor modes.
802 Usage:
803 (viper-harness-minor-mode load-file)
805 LOAD-FILE is a name of the file where the specific minor mode is defined.
806 Suffixes such as .el or .elc should be stripped."
808 (interactive "sEnter name of the load file: ")
810 (eval-after-load load-file '(viper-normalize-minor-mode-map-alist))
812 ;; Change the default for minor-mode-map-alist each time a harnessed minor
813 ;; mode adds its own keymap to the a-list.
814 (eval-after-load
815 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist))
819 (defun viper-ESC (arg)
820 "Emulate ESC key in Emacs.
821 Prevents multiple escape keystrokes if viper-no-multiple-ESC is true.
822 If viper-no-multiple-ESC is 'twice double ESC would ding in vi-state.
823 Other ESC sequences are emulated via the current Emacs's major mode
824 keymap. This is more convenient on TTYs, since this won't block
825 function keys such as up,down, etc. ESC will also will also work as
826 a Meta key in this case. When viper-no-multiple-ESC is nil, ESC functions
827 as a Meta key and any number of multiple escapes is allowed."
828 (interactive "P")
829 (let (char)
830 (cond ((and (not viper-no-multiple-ESC) (eq viper-current-state 'vi-state))
831 (setq char (viper-read-char-exclusive))
832 (viper-escape-to-emacs arg (list ?\e char) ))
833 ((and (eq viper-no-multiple-ESC 'twice)
834 (eq viper-current-state 'vi-state))
835 (setq char (viper-read-char-exclusive))
836 (if (= char (string-to-char viper-ESC-key))
837 (ding)
838 (viper-escape-to-emacs arg (list ?\e char) )))
839 (t (ding)))
842 (defun viper-alternate-Meta-key (arg)
843 "Simulate Emacs Meta key."
844 (interactive "P")
845 (sit-for 1) (message "ESC-")
846 (viper-escape-to-emacs arg '(?\e)))
848 (defun viper-toggle-key-action ()
849 "Action bound to `viper-toggle-key'."
850 (interactive)
851 (if (and (< viper-expert-level 2) (equal viper-toggle-key "\C-z"))
852 (if (viper-window-display-p)
853 (viper-iconify)
854 (suspend-emacs))
855 (viper-change-state-to-emacs)))
858 ;; Intercept ESC sequences on dumb terminals.
859 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
861 ;; Check if last key was ESC and if so try to reread it as a function key.
862 ;; But only if there are characters to read during a very short time.
863 ;; Returns the last event, if any.
864 (defun viper-envelop-ESC-key ()
865 (let ((event last-input-event)
866 (keyseq [nil])
867 inhibit-quit)
868 (if (viper-ESC-event-p event)
869 (progn
870 (if (viper-fast-keysequence-p)
871 (progn
872 (let (minor-mode-map-alist)
873 (viper-set-unread-command-events event)
874 (setq keyseq
875 (funcall
876 (ad-get-orig-definition 'read-key-sequence) nil))
877 ) ; let
878 ;; If keyseq translates into something that still has ESC
879 ;; at the beginning, separate ESC from the rest of the seq.
880 ;; In XEmacs we check for events that are keypress meta-key
881 ;; and convert them into [escape key]
883 ;; This is needed for the following reason:
884 ;; If ESC is the first symbol, we interpret it as if the
885 ;; user typed ESC and then quickly some other symbols.
886 ;; If ESC is not the first one, then the key sequence
887 ;; entered was apparently translated into a function key or
888 ;; something (e.g., one may have
889 ;; (define-key function-key-map "\e[192z" [f11])
890 ;; which would translate the escape-sequence generated by
891 ;; f11 in an xterm window into the symbolic key f11.
893 ;; If `first-key' is not an ESC event, we make it into the
894 ;; last-command-event in order to pretend that this key was
895 ;; pressed. This is needed to allow arrow keys to be bound to
896 ;; macros. Otherwise, viper-exec-mapped-kbd-macro will think
897 ;; that the last event was ESC and so it'll execute whatever is
898 ;; bound to ESC. (Viper macros can't be bound to
899 ;; ESC-sequences).
900 (let* ((first-key (elt keyseq 0))
901 (key-mod (event-modifiers first-key)))
902 (cond ((and (viper-ESC-event-p first-key)
903 (not viper-translate-all-ESC-keysequences))
904 ;; put keys following ESC on the unread list
905 ;; and return ESC as the key-sequence
906 (viper-set-unread-command-events (subseq keyseq 1))
907 (setq last-input-event event
908 keyseq (if viper-emacs-p
909 "\e"
910 (vector (character-to-event ?\e)))))
911 ((and viper-xemacs-p
912 (key-press-event-p first-key)
913 (equal '(meta) key-mod))
914 (viper-set-unread-command-events
915 (vconcat (vector
916 (character-to-event (event-key first-key)))
917 (subseq keyseq 1)))
918 (setq last-input-event event
919 keyseq (vector (character-to-event ?\e))))
920 ((eventp first-key)
921 (setq last-command-event
922 (viper-copy-event first-key)))
924 ) ; end progn
926 ;; this is escape event with nothing after it
927 ;; put in unread-command-event and then re-read
928 (viper-set-unread-command-events event)
929 (setq keyseq
930 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
932 ;; not an escape event
933 (setq keyseq (vector event)))
934 keyseq))
938 ;; Listen to ESC key.
939 ;; If a sequence of keys starting with ESC is issued with very short delays,
940 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
941 (defun viper-intercept-ESC-key ()
942 "Function that implements ESC key in Viper emulation of Vi."
943 (interactive)
944 (let ((cmd (or (key-binding (viper-envelop-ESC-key))
945 '(lambda () (interactive) (error "")))))
947 ;; call the actual function to execute ESC (if no other symbols followed)
948 ;; or the key bound to the ESC sequence (if the sequence was issued
949 ;; with very short delay between characters.
950 (if (eq cmd 'viper-intercept-ESC-key)
951 (setq cmd
952 (cond ((eq viper-current-state 'vi-state)
953 'viper-ESC)
954 ((eq viper-current-state 'insert-state)
955 'viper-exit-insert-state)
956 ((eq viper-current-state 'replace-state)
957 'viper-replace-state-exit-cmd)
958 (t 'viper-change-state-to-vi)
960 (call-interactively cmd)))
965 ;; prefix argument for Vi mode
967 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
968 ;; represents the numeric value of the prefix argument and COM represents
969 ;; command prefix such as "c", "d", "m" and "y".
971 ;; Get value part of prefix-argument ARG.
972 (defsubst viper-p-val (arg)
973 (cond ((null arg) 1)
974 ((consp arg)
975 (if (or (null (car arg)) (equal (car arg) '(nil)))
976 1 (car arg)))
977 (t arg)))
979 ;; Get raw value part of prefix-argument ARG.
980 (defsubst viper-P-val (arg)
981 (cond ((consp arg) (car arg))
982 (t arg)))
984 ;; Get com part of prefix-argument ARG.
985 (defsubst viper-getcom (arg)
986 (cond ((null arg) nil)
987 ((consp arg) (cdr arg))
988 (t nil)))
990 ;; Get com part of prefix-argument ARG and modify it.
991 (defun viper-getCom (arg)
992 (let ((com (viper-getcom arg)))
993 (cond ((viper= com ?c) ?c)
994 ;; Previously, ?c was being converted to ?C, but this prevented
995 ;; multiline replace regions.
996 ;;((viper= com ?c) ?C)
997 ((viper= com ?d) ?D)
998 ((viper= com ?y) ?Y)
999 (t com))))
1002 ;; Compute numeric prefix arg value.
1003 ;; Invoked by EVENT-CHAR. COM is the command part obtained so far.
1004 (defun viper-prefix-arg-value (event-char com)
1005 (let ((viper-intermediate-command 'viper-digit-argument)
1006 value func)
1007 ;; read while number
1008 (while (and (viper-characterp event-char)
1009 (>= event-char ?0) (<= event-char ?9))
1010 (setq value (+ (* (if (integerp value) value 0) 10) (- event-char ?0)))
1011 (setq event-char (viper-read-event-convert-to-char)))
1013 (setq prefix-arg value)
1014 (if com (setq prefix-arg (cons prefix-arg com)))
1015 (while (eq event-char ?U)
1016 (viper-describe-arg prefix-arg)
1017 (setq event-char (viper-read-event-convert-to-char)))
1019 (if (or com (and (not (eq viper-current-state 'vi-state))
1020 ;; make sure it is a Vi command
1021 (viper-characterp event-char)
1022 (viper-vi-command-p event-char)
1024 ;; If appears to be one of the vi commands,
1025 ;; then execute it with funcall and clear prefix-arg in order to not
1026 ;; confuse subsequent commands
1027 (progn
1028 ;; last-command-char is the char we want emacs to think was typed
1029 ;; last. If com is not nil, the viper-digit-argument command was
1030 ;; called from within viper-prefix-arg command, such as `d', `w',
1031 ;; etc., i.e., the user typed, say, d2. In this case, `com' would be
1032 ;; `d', `w', etc. If viper-digit-argument was invoked by
1033 ;; viper-escape-to-vi (which is indicated by the fact that the
1034 ;; current state is not vi-state), then `event-char' represents the
1035 ;; vi command to be executed (e.g., `d', `w', etc). Again,
1036 ;; last-command-char must make emacs believe that this is the command
1037 ;; we typed.
1038 (cond ((eq event-char 'return) (setq event-char ?\C-m))
1039 ((eq event-char 'delete) (setq event-char ?\C-?))
1040 ((eq event-char 'backspace) (setq event-char ?\C-h))
1041 ((eq event-char 'space) (setq event-char ?\ )))
1042 (setq last-command-char (or com event-char))
1043 (setq func (viper-exec-form-in-vi
1044 `(key-binding (char-to-string ,event-char))))
1045 (funcall func prefix-arg)
1046 (setq prefix-arg nil))
1047 ;; some other command -- let emacs do it in its own way
1048 (viper-set-unread-command-events event-char))
1052 ;; Vi operator as prefix argument."
1053 (defun viper-prefix-arg-com (char value com)
1054 (let ((cont t)
1055 cmd-info
1056 cmd-to-exec-at-end)
1057 (while (and cont
1058 (viper-memq-char char
1059 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1060 viper-buffer-search-char)))
1061 (if com
1062 ;; this means that we already have a command character, so we
1063 ;; construct a com list and exit while. however, if char is "
1064 ;; it is an error.
1065 (progn
1066 ;; new com is (CHAR . OLDCOM)
1067 (if (viper-memq-char char '(?# ?\")) (error ""))
1068 (setq com (cons char com))
1069 (setq cont nil))
1070 ;; If com is nil we set com as char, and read more. Again, if char is
1071 ;; ", we read the name of register and store it in viper-use-register.
1072 ;; if char is !, =, or #, a complete com is formed so we exit the while
1073 ;; loop.
1074 (cond ((viper-memq-char char '(?! ?=))
1075 (setq com char)
1076 (setq char (read-char))
1077 (setq cont nil))
1078 ((viper= char ?#)
1079 ;; read a char and encode it as com
1080 (setq com (+ 128 (read-char)))
1081 (setq char (read-char)))
1082 ((viper= char ?\")
1083 (let ((reg (read-char)))
1084 (if (viper-valid-register reg)
1085 (setq viper-use-register reg)
1086 (error ""))
1087 (setq char (read-char))))
1089 (setq com char)
1090 (setq char (read-char))))))
1092 (if (atom com)
1093 ;; `com' is a single char, so we construct the command argument
1094 ;; and if `char' is `?', we describe the arg; otherwise
1095 ;; we prepare the command that will be executed at the end.
1096 (progn
1097 (setq cmd-info (cons value com))
1098 (while (viper= char ?U)
1099 (viper-describe-arg cmd-info)
1100 (setq char (read-char)))
1101 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
1102 ;; execute it at the very end
1103 (or (viper-movement-command-p char)
1104 (viper-digit-command-p char)
1105 (viper-regsuffix-command-p char)
1106 (viper= char ?!) ; bang command
1107 (error ""))
1108 (setq cmd-to-exec-at-end
1109 (viper-exec-form-in-vi
1110 `(key-binding (char-to-string ,char)))))
1112 ;; as com is non-nil, this means that we have a command to execute
1113 (if (viper-memq-char (car com) '(?r ?R))
1114 ;; execute apropriate region command.
1115 (let ((char (car com)) (com (cdr com)))
1116 (setq prefix-arg (cons value com))
1117 (if (viper= char ?r) (viper-region prefix-arg)
1118 (viper-Region prefix-arg))
1119 ;; reset prefix-arg
1120 (setq prefix-arg nil))
1121 ;; otherwise, reset prefix arg and call appropriate command
1122 (setq value (if (null value) 1 value))
1123 (setq prefix-arg nil)
1124 (cond
1125 ;; If we change ?C to ?c here, then cc will enter replacement mode
1126 ;; rather than deleting lines. However, it will affect 1 less line than
1127 ;; normal. We decided to not use replacement mode here and follow Vi,
1128 ;; since replacement mode on n full lines can be achieved with nC.
1129 ((equal com '(?c . ?c)) (viper-line (cons value ?C)))
1130 ((equal com '(?d . ?d)) (viper-line (cons value ?D)))
1131 ((equal com '(?d . ?y)) (viper-yank-defun))
1132 ((equal com '(?y . ?y)) (viper-line (cons value ?Y)))
1133 ((equal com '(?< . ?<)) (viper-line (cons value ?<)))
1134 ((equal com '(?> . ?>)) (viper-line (cons value ?>)))
1135 ((equal com '(?! . ?!)) (viper-line (cons value ?!)))
1136 ((equal com '(?= . ?=)) (viper-line (cons value ?=)))
1137 (t (error "")))))
1139 (if cmd-to-exec-at-end
1140 (progn
1141 (setq last-command-char char)
1142 (setq last-command-event
1143 (viper-copy-event
1144 (if viper-xemacs-p (character-to-event char) char)))
1145 (condition-case nil
1146 (funcall cmd-to-exec-at-end cmd-info)
1147 (error
1148 (error "")))))
1151 (defun viper-describe-arg (arg)
1152 (let (val com)
1153 (setq val (viper-P-val arg)
1154 com (viper-getcom arg))
1155 (if (null val)
1156 (if (null com)
1157 (message "Value is nil, and command is nil")
1158 (message "Value is nil, and command is `%c'" com))
1159 (if (null com)
1160 (message "Value is `%d', and command is nil" val)
1161 (message "Value is `%d', and command is `%c'" val com)))))
1163 (defun viper-digit-argument (arg)
1164 "Begin numeric argument for the next command."
1165 (interactive "P")
1166 (viper-leave-region-active)
1167 (viper-prefix-arg-value
1168 last-command-char (if (consp arg) (cdr arg) nil)))
1170 (defun viper-command-argument (arg)
1171 "Accept a motion command as an argument."
1172 (interactive "P")
1173 (let ((viper-intermediate-command 'viper-command-argument))
1174 (condition-case nil
1175 (viper-prefix-arg-com
1176 last-command-char
1177 (cond ((null arg) nil)
1178 ((consp arg) (car arg))
1179 ((integerp arg) arg)
1180 (t (error viper-InvalidCommandArgument)))
1181 (cond ((null arg) nil)
1182 ((consp arg) (cdr arg))
1183 ((integerp arg) nil)
1184 (t (error viper-InvalidCommandArgument))))
1185 (quit (setq viper-use-register nil)
1186 (signal 'quit nil)))
1187 (viper-deactivate-mark)))
1190 ;; repeat last destructive command
1192 ;; Append region to text in register REG.
1193 ;; START and END are buffer positions indicating what to append.
1194 (defsubst viper-append-to-register (reg start end)
1195 (set-register reg (concat (if (stringp (get-register reg))
1196 (get-register reg) "")
1197 (buffer-substring start end))))
1199 ;; Saves last inserted text for possible use by viper-repeat command.
1200 (defun viper-save-last-insertion (beg end)
1201 (condition-case nil
1202 (setq viper-last-insertion (buffer-substring beg end))
1203 (error
1204 ;; beg or end marker are somehow screwed up
1205 (setq viper-last-insertion nil)))
1206 (setq viper-last-insertion (buffer-substring beg end))
1207 (or (< (length viper-d-com) 5)
1208 (setcar (nthcdr 4 viper-d-com) viper-last-insertion))
1209 (or (null viper-command-ring)
1210 (ring-empty-p viper-command-ring)
1211 (progn
1212 (setcar (nthcdr 4 (viper-current-ring-item viper-command-ring))
1213 viper-last-insertion)
1214 ;; del most recent elt, if identical to the second most-recent
1215 (viper-cleanup-ring viper-command-ring)))
1218 (defsubst viper-yank-last-insertion ()
1219 "Inserts the text saved by the previous viper-save-last-insertion command."
1220 (condition-case nil
1221 (insert viper-last-insertion)
1222 (error nil)))
1225 ;; define functions to be executed
1227 ;; invoked by the `C' command
1228 (defun viper-exec-change (m-com com)
1229 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1230 (set-marker viper-com-point (point) (current-buffer)))
1231 ;; handle C cmd at the eol and at eob.
1232 (if (or (and (eolp) (= viper-com-point (point)))
1233 (= viper-com-point (point-max)))
1234 (progn
1235 (insert " ")(backward-char 1)))
1236 (if (= viper-com-point (point))
1237 (viper-forward-char-carefully))
1238 (set-mark viper-com-point)
1239 (if (eq m-com 'viper-next-line-at-bol)
1240 (viper-enlarge-region (mark t) (point)))
1241 (if (< (point) (mark t))
1242 (exchange-point-and-mark))
1243 (if (eq (preceding-char) ?\n)
1244 (viper-backward-char-carefully)) ; give back the newline
1245 (if (viper= com ?c)
1246 (viper-change (mark t) (point))
1247 (viper-change-subr (mark t) (point))))
1249 ;; this is invoked by viper-substitute-line
1250 (defun viper-exec-Change (m-com com)
1251 (save-excursion
1252 (set-mark viper-com-point)
1253 (viper-enlarge-region (mark t) (point))
1254 (if viper-use-register
1255 (progn
1256 (cond ((viper-valid-register viper-use-register '(letter digit))
1257 (copy-to-register
1258 viper-use-register (mark t) (point) nil))
1259 ((viper-valid-register viper-use-register '(Letter))
1260 (viper-append-to-register
1261 (downcase viper-use-register) (mark t) (point)))
1262 (t (setq viper-use-register nil)
1263 (error viper-InvalidRegister viper-use-register)))
1264 (setq viper-use-register nil)))
1265 (delete-region (mark t) (point)))
1266 (open-line 1)
1267 (if (viper= com ?C)
1268 (viper-change-state-to-insert)
1269 (viper-yank-last-insertion)))
1271 (defun viper-exec-delete (m-com com)
1272 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1273 (set-marker viper-com-point (point) (current-buffer)))
1274 (let (chars-deleted)
1275 (if viper-use-register
1276 (progn
1277 (cond ((viper-valid-register viper-use-register '(letter digit))
1278 (copy-to-register
1279 viper-use-register viper-com-point (point) nil))
1280 ((viper-valid-register viper-use-register '(Letter))
1281 (viper-append-to-register
1282 (downcase viper-use-register) viper-com-point (point)))
1283 (t (setq viper-use-register nil)
1284 (error viper-InvalidRegister viper-use-register)))
1285 (setq viper-use-register nil)))
1286 (setq last-command
1287 (if (eq last-command 'd-command) 'kill-region nil))
1288 (setq chars-deleted (abs (- (point) viper-com-point)))
1289 (if (> chars-deleted viper-change-notification-threshold)
1290 (message "Deleted %d characters" chars-deleted))
1291 (kill-region viper-com-point (point))
1292 (setq this-command 'd-command)
1293 (if viper-ex-style-motion
1294 (if (and (eolp) (not (bolp))) (backward-char 1)))))
1296 (defun viper-exec-Delete (m-com com)
1297 (save-excursion
1298 (set-mark viper-com-point)
1299 (viper-enlarge-region (mark t) (point))
1300 (let (lines-deleted)
1301 (if viper-use-register
1302 (progn
1303 (cond ((viper-valid-register viper-use-register '(letter digit))
1304 (copy-to-register
1305 viper-use-register (mark t) (point) nil))
1306 ((viper-valid-register viper-use-register '(Letter))
1307 (viper-append-to-register
1308 (downcase viper-use-register) (mark t) (point)))
1309 (t (setq viper-use-register nil)
1310 (error viper-InvalidRegister viper-use-register)))
1311 (setq viper-use-register nil)))
1312 (setq last-command
1313 (if (eq last-command 'D-command) 'kill-region nil))
1314 (setq lines-deleted (count-lines (point) viper-com-point))
1315 (if (> lines-deleted viper-change-notification-threshold)
1316 (message "Deleted %d lines" lines-deleted))
1317 (kill-region (mark t) (point))
1318 (if (eq m-com 'viper-line) (setq this-command 'D-command)))
1319 (back-to-indentation)))
1321 ;; save region
1322 (defun viper-exec-yank (m-com com)
1323 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1324 (set-marker viper-com-point (point) (current-buffer)))
1325 (let (chars-saved)
1326 (if viper-use-register
1327 (progn
1328 (cond ((viper-valid-register viper-use-register '(letter digit))
1329 (copy-to-register
1330 viper-use-register viper-com-point (point) nil))
1331 ((viper-valid-register viper-use-register '(Letter))
1332 (viper-append-to-register
1333 (downcase viper-use-register) viper-com-point (point)))
1334 (t (setq viper-use-register nil)
1335 (error viper-InvalidRegister viper-use-register)))
1336 (setq viper-use-register nil)))
1337 (setq last-command nil)
1338 (copy-region-as-kill viper-com-point (point))
1339 (setq chars-saved (abs (- (point) viper-com-point)))
1340 (if (> chars-saved viper-change-notification-threshold)
1341 (message "Saved %d characters" chars-saved))
1342 (goto-char viper-com-point)))
1344 ;; save lines
1345 (defun viper-exec-Yank (m-com com)
1346 (save-excursion
1347 (set-mark viper-com-point)
1348 (viper-enlarge-region (mark t) (point))
1349 (let (lines-saved)
1350 (if viper-use-register
1351 (progn
1352 (cond ((viper-valid-register viper-use-register '(letter digit))
1353 (copy-to-register
1354 viper-use-register (mark t) (point) nil))
1355 ((viper-valid-register viper-use-register '(Letter))
1356 (viper-append-to-register
1357 (downcase viper-use-register) (mark t) (point)))
1358 (t (setq viper-use-register nil)
1359 (error viper-InvalidRegister viper-use-register)))
1360 (setq viper-use-register nil)))
1361 (setq last-command nil)
1362 (copy-region-as-kill (mark t) (point))
1363 (setq lines-saved (count-lines (mark t) (point)))
1364 (if (> lines-saved viper-change-notification-threshold)
1365 (message "Saved %d lines" lines-saved))))
1366 (viper-deactivate-mark)
1367 (goto-char viper-com-point))
1369 (defun viper-exec-bang (m-com com)
1370 (save-excursion
1371 (set-mark viper-com-point)
1372 (viper-enlarge-region (mark t) (point))
1373 (exchange-point-and-mark)
1374 (shell-command-on-region
1375 (mark t) (point)
1376 (if (viper= com ?!)
1377 (setq viper-last-shell-com
1378 (viper-read-string-with-history
1381 'viper-shell-history
1382 (car viper-shell-history)
1384 viper-last-shell-com)
1385 t)))
1387 (defun viper-exec-equals (m-com com)
1388 (save-excursion
1389 (set-mark viper-com-point)
1390 (viper-enlarge-region (mark t) (point))
1391 (if (> (mark t) (point)) (exchange-point-and-mark))
1392 (indent-region (mark t) (point) nil)))
1394 (defun viper-exec-shift (m-com com)
1395 (save-excursion
1396 (set-mark viper-com-point)
1397 (viper-enlarge-region (mark t) (point))
1398 (if (> (mark t) (point)) (exchange-point-and-mark))
1399 (indent-rigidly (mark t) (point)
1400 (if (viper= com ?>)
1401 viper-shift-width
1402 (- viper-shift-width))))
1403 ;; return point to where it was before shift
1404 (goto-char viper-com-point))
1406 ;; this is needed because some commands fake com by setting it to ?r, which
1407 ;; denotes repeated insert command.
1408 (defsubst viper-exec-dummy (m-com com)
1409 nil)
1411 (defun viper-exec-buffer-search (m-com com)
1412 (setq viper-s-string (buffer-substring (point) viper-com-point))
1413 (setq viper-s-forward t)
1414 (setq viper-search-history (cons viper-s-string viper-search-history))
1415 (setq viper-intermediate-command 'viper-exec-buffer-search)
1416 (viper-search viper-s-string viper-s-forward 1))
1418 (defvar viper-exec-array (make-vector 128 nil))
1420 ;; Using a dispatch array allows adding functions like buffer search
1421 ;; without affecting other functions. Buffer search can now be bound
1422 ;; to any character.
1424 (aset viper-exec-array ?c 'viper-exec-change)
1425 (aset viper-exec-array ?C 'viper-exec-Change)
1426 (aset viper-exec-array ?d 'viper-exec-delete)
1427 (aset viper-exec-array ?D 'viper-exec-Delete)
1428 (aset viper-exec-array ?y 'viper-exec-yank)
1429 (aset viper-exec-array ?Y 'viper-exec-Yank)
1430 (aset viper-exec-array ?r 'viper-exec-dummy)
1431 (aset viper-exec-array ?! 'viper-exec-bang)
1432 (aset viper-exec-array ?< 'viper-exec-shift)
1433 (aset viper-exec-array ?> 'viper-exec-shift)
1434 (aset viper-exec-array ?= 'viper-exec-equals)
1438 ;; This function is called by various movement commands to execute a
1439 ;; destructive command on the region specified by the movement command. For
1440 ;; instance, if the user types cw, then the command viper-forward-word will
1441 ;; call viper-execute-com to execute viper-exec-change, which eventually will
1442 ;; call viper-change to invoke the replace mode on the region.
1444 ;; The var viper-d-com is set to (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS)
1445 ;; via a call to viper-set-destructive-command, for later use by viper-repeat.
1446 (defun viper-execute-com (m-com val com)
1447 (let ((reg viper-use-register))
1448 ;; this is the special command `#'
1449 (if (> com 128)
1450 (viper-special-prefix-com (- com 128))
1451 (let ((fn (aref viper-exec-array (if (< com 0) (- com) com))))
1452 (if (null fn)
1453 (error "%c: %s" com viper-InvalidViCommand)
1454 (funcall fn m-com com))))
1455 (if (viper-dotable-command-p com)
1456 (viper-set-destructive-command
1457 (list m-com val
1458 (if (viper-memq-char com (list ?c ?C ?!)) (- com) com)
1459 reg nil nil)))
1463 (defun viper-repeat (arg)
1464 "Re-execute last destructive command.
1465 Use the info in viper-d-com, which has the form
1466 \(com val ch reg inserted-text command-keys\),
1467 where `com' is the command to be re-executed, `val' is the
1468 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1469 if it exists, it is the name of the register for `com'.
1470 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
1471 (interactive "P")
1472 (let ((save-point (point)) ; save point before repeating prev cmd
1473 ;; Pass along that we are repeating a destructive command
1474 ;; This tells viper-set-destructive-command not to update
1475 ;; viper-command-ring
1476 (viper-intermediate-command 'viper-repeat))
1477 (if (eq last-command 'viper-undo)
1478 ;; if the last command was viper-undo, then undo-more
1479 (viper-undo-more)
1480 ;; otherwise execute the command stored in viper-d-com. if arg is
1481 ;; non-nil its prefix value is used as new prefix value for the command.
1482 (let ((m-com (car viper-d-com))
1483 (val (viper-P-val arg))
1484 (com (nth 2 viper-d-com))
1485 (reg (nth 3 viper-d-com)))
1486 (if (null val) (setq val (nth 1 viper-d-com)))
1487 (if (null m-com) (error "No previous command to repeat."))
1488 (setq viper-use-register reg)
1489 (if (nth 4 viper-d-com) ; text inserted by command
1490 (setq viper-last-insertion (nth 4 viper-d-com)
1491 viper-d-char (nth 4 viper-d-com)))
1492 (funcall m-com (cons val com))
1493 (cond ((and (< save-point (point)) viper-keep-point-on-repeat)
1494 (goto-char save-point)) ; go back to before repeat.
1495 ((and (< save-point (point)) viper-ex-style-editing)
1496 (or (bolp) (backward-char 1))))
1497 (if (and (eolp) (not (bolp)))
1498 (backward-char 1))
1500 (viper-adjust-undo) ; take care of undo
1501 ;; If the prev cmd was rotating the command ring, this means that `.' has
1502 ;; just executed a command from that ring. So, push it on the ring again.
1503 ;; If we are just executing previous command , then don't push viper-d-com
1504 ;; because viper-d-com is not fully constructed in this case (its keys and
1505 ;; the inserted text may be nil). Besides, in this case, the command
1506 ;; executed by `.' is already on the ring.
1507 (if (eq last-command 'viper-display-current-destructive-command)
1508 (viper-push-onto-ring viper-d-com 'viper-command-ring))
1509 (viper-deactivate-mark)
1512 (defun viper-repeat-from-history ()
1513 "Repeat a destructive command from history.
1514 Doesn't change viper-command-ring in any way, so `.' will work as before
1515 executing this command.
1516 This command is supposed to be bound to a two-character Vi macro where
1517 the second character is a digit 0 to 9. The digit indicates which
1518 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1519 invokes the command before that, etc."
1520 (interactive)
1521 (let* ((viper-intermediate-command 'repeating-display-destructive-command)
1522 (idx (cond (viper-this-kbd-macro
1523 (string-to-number
1524 (symbol-name (elt viper-this-kbd-macro 1))))
1525 (t 0)))
1526 (num idx)
1527 (viper-d-com viper-d-com))
1529 (or (and (numberp num) (<= 0 num) (<= num 9))
1530 (progn
1531 (setq idx 0
1532 num 0)
1533 (message
1534 "`viper-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1535 (while (< 0 num)
1536 (setq viper-d-com (viper-special-ring-rotate1 viper-command-ring -1))
1537 (setq num (1- num)))
1538 (viper-repeat nil)
1539 (while (> idx num)
1540 (viper-special-ring-rotate1 viper-command-ring 1)
1541 (setq num (1+ num)))
1545 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1546 ;; The chars that can follow `#' are determined by viper-hash-command-p
1547 (defun viper-special-prefix-com (char)
1548 (cond ((viper= char ?c)
1549 (downcase-region (min viper-com-point (point))
1550 (max viper-com-point (point))))
1551 ((viper= char ?C)
1552 (upcase-region (min viper-com-point (point))
1553 (max viper-com-point (point))))
1554 ((viper= char ?g)
1555 (push-mark viper-com-point t)
1556 (viper-global-execute))
1557 ((viper= char ?q)
1558 (push-mark viper-com-point t)
1559 (viper-quote-region))
1560 ((viper= char ?s)
1561 (funcall viper-spell-function viper-com-point (point)))
1562 (t (error "#%c: %s" char viper-InvalidViCommand))))
1565 ;; undoing
1567 (defun viper-undo ()
1568 "Undo previous change."
1569 (interactive)
1570 (message "undo!")
1571 (let ((modified (buffer-modified-p))
1572 (before-undo-pt (point-marker))
1573 (after-change-functions after-change-functions)
1574 undo-beg-posn undo-end-posn)
1576 ;; no need to remove this hook, since this var has scope inside a let.
1577 (add-hook 'after-change-functions
1578 '(lambda (beg end len)
1579 (setq undo-beg-posn beg
1580 undo-end-posn (or end beg))))
1582 (undo-start)
1583 (undo-more 2)
1584 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1585 undo-end-posn (or undo-end-posn undo-beg-posn))
1587 (goto-char undo-beg-posn)
1588 (sit-for 0)
1589 (if (and viper-keep-point-on-undo
1590 (pos-visible-in-window-p before-undo-pt))
1591 (progn
1592 (push-mark (point-marker) t)
1593 (viper-sit-for-short 300)
1594 (goto-char undo-end-posn)
1595 (viper-sit-for-short 300)
1596 (if (and (> (viper-chars-in-region undo-beg-posn before-undo-pt) 1)
1597 (> (viper-chars-in-region undo-end-posn before-undo-pt) 1))
1598 (goto-char before-undo-pt)
1599 (goto-char undo-beg-posn)))
1600 (push-mark before-undo-pt t))
1601 (if (and (eolp) (not (bolp))) (backward-char 1))
1602 (if (not modified) (set-buffer-modified-p t)))
1603 (setq this-command 'viper-undo))
1605 ;; Continue undoing previous changes.
1606 (defun viper-undo-more ()
1607 (message "undo more!")
1608 (condition-case nil
1609 (undo-more 1)
1610 (error (beep)
1611 (message "No further undo information in this buffer")))
1612 (if (and (eolp) (not (bolp))) (backward-char 1))
1613 (setq this-command 'viper-undo))
1615 ;; The following two functions are used to set up undo properly.
1616 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1617 ;; they are undone all at once.
1618 (defun viper-adjust-undo ()
1619 (if viper-undo-needs-adjustment
1620 (let ((inhibit-quit t)
1621 tmp tmp2)
1622 (setq viper-undo-needs-adjustment nil)
1623 (if (listp buffer-undo-list)
1624 (if (setq tmp (memq viper-buffer-undo-list-mark buffer-undo-list))
1625 (progn
1626 (setq tmp2 (cdr tmp)) ; the part after mark
1628 ;; cut tail from buffer-undo-list temporarily by direct
1629 ;; manipulation with pointers in buffer-undo-list
1630 (setcdr tmp nil)
1632 (setq buffer-undo-list (delq nil buffer-undo-list))
1633 (setq buffer-undo-list
1634 (delq viper-buffer-undo-list-mark buffer-undo-list))
1635 ;; restore tail of buffer-undo-list
1636 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1637 (setq buffer-undo-list (delq nil buffer-undo-list)))))
1641 (defun viper-set-complex-command-for-undo ()
1642 (if (listp buffer-undo-list)
1643 (if (not viper-undo-needs-adjustment)
1644 (let ((inhibit-quit t))
1645 (setq buffer-undo-list
1646 (cons viper-buffer-undo-list-mark buffer-undo-list))
1647 (setq viper-undo-needs-adjustment t)))))
1652 (defun viper-display-current-destructive-command ()
1653 (let ((text (nth 4 viper-d-com))
1654 (keys (nth 5 viper-d-com))
1655 (max-text-len 30))
1657 (setq this-command 'viper-display-current-destructive-command)
1659 (message " `.' runs %s%s"
1660 (concat "`" (viper-array-to-string keys) "'")
1661 (viper-abbreviate-string
1662 (if viper-xemacs-p
1663 (replace-in-string
1664 (cond ((characterp text) (char-to-string text))
1665 ((stringp text) text)
1666 (t ""))
1667 "\n" "^J")
1668 text)
1669 max-text-len
1670 " inserting `" "'" " ......."))
1674 ;; don't change viper-d-com if it was viper-repeat command invoked with `.'
1675 ;; or in some other way (non-interactively).
1676 (defun viper-set-destructive-command (list)
1677 (or (eq viper-intermediate-command 'viper-repeat)
1678 (progn
1679 (setq viper-d-com list)
1680 (setcar (nthcdr 5 viper-d-com)
1681 (viper-array-to-string (if (arrayp viper-this-command-keys)
1682 viper-this-command-keys
1683 (this-command-keys))))
1684 (viper-push-onto-ring viper-d-com 'viper-command-ring)))
1685 (setq viper-this-command-keys nil))
1688 (defun viper-prev-destructive-command (next)
1689 "Find previous destructive command in the history of destructive commands.
1690 With prefix argument, find next destructive command."
1691 (interactive "P")
1692 (let (cmd viper-intermediate-command)
1693 (if (eq last-command 'viper-display-current-destructive-command)
1694 ;; repeated search through command history
1695 (setq viper-intermediate-command
1696 'repeating-display-destructive-command)
1697 ;; first search through command history--set temp ring
1698 (setq viper-temp-command-ring (copy-list viper-command-ring)))
1699 (setq cmd (if next
1700 (viper-special-ring-rotate1 viper-temp-command-ring 1)
1701 (viper-special-ring-rotate1 viper-temp-command-ring -1)))
1702 (if (null cmd)
1704 (setq viper-d-com cmd))
1705 (viper-display-current-destructive-command)))
1708 (defun viper-next-destructive-command ()
1709 "Find next destructive command in the history of destructive commands."
1710 (interactive)
1711 (viper-prev-destructive-command 'next))
1714 (defun viper-insert-prev-from-insertion-ring (arg)
1715 "Cycle through insertion ring in the direction of older insertions.
1716 Undoes previous insertion and inserts new.
1717 With prefix argument, cycles in the direction of newer elements.
1718 In minibuffer, this command executes whatever the invocation key is bound
1719 to in the global map, instead of cycling through the insertion ring."
1720 (interactive "P")
1721 (let (viper-intermediate-command)
1722 (if (eq last-command 'viper-insert-from-insertion-ring)
1723 (progn ; repeated search through insertion history
1724 (setq viper-intermediate-command 'repeating-insertion-from-ring)
1725 (if (eq viper-current-state 'replace-state)
1726 (undo 1)
1727 (if viper-last-inserted-string-from-insertion-ring
1728 (backward-delete-char
1729 (length viper-last-inserted-string-from-insertion-ring))))
1731 ;;first search through insertion history
1732 (setq viper-temp-insertion-ring (copy-list viper-insertion-ring)))
1733 (setq this-command 'viper-insert-from-insertion-ring)
1734 ;; so that things will be undone properly
1735 (setq buffer-undo-list (cons nil buffer-undo-list))
1736 (setq viper-last-inserted-string-from-insertion-ring
1737 (viper-special-ring-rotate1 viper-temp-insertion-ring (if arg 1 -1)))
1739 ;; this change of viper-intermediate-command must come after
1740 ;; viper-special-ring-rotate1, so that the ring will rotate, but before the
1741 ;; insertion.
1742 (setq viper-intermediate-command nil)
1743 (if viper-last-inserted-string-from-insertion-ring
1744 (insert viper-last-inserted-string-from-insertion-ring))
1747 (defun viper-insert-next-from-insertion-ring ()
1748 "Cycle through insertion ring in the direction of older insertions.
1749 Undo previous insertion and inserts new."
1750 (interactive)
1751 (viper-insert-prev-from-insertion-ring 'next))
1755 ;; some region utilities
1757 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1758 (defun viper-add-newline-at-eob-if-necessary ()
1759 (save-excursion
1760 (end-of-line)
1761 ;; make sure all lines end with newline, unless in the minibuffer or
1762 ;; when requested otherwise (require-final-newline is nil)
1763 (if (and (eobp)
1764 (not (bolp))
1765 require-final-newline
1766 (not (viper-is-in-minibuffer))
1767 (not buffer-read-only))
1768 (insert "\n"))))
1770 (defun viper-yank-defun ()
1771 (mark-defun)
1772 (copy-region-as-kill (point) (mark t)))
1774 ;; Enlarge region between BEG and END.
1775 (defun viper-enlarge-region (beg end)
1776 (or beg (setq beg end)) ; if beg is nil, set to end
1777 (or end (setq end beg)) ; if end is nil, set to beg
1779 (if (< beg end)
1780 (progn (goto-char beg) (set-mark end))
1781 (goto-char end)
1782 (set-mark beg))
1783 (beginning-of-line)
1784 (exchange-point-and-mark)
1785 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1786 (if (not (eobp)) (beginning-of-line))
1787 (if (> beg end) (exchange-point-and-mark)))
1790 ;; Quote region by each line with a user supplied string.
1791 (defun viper-quote-region ()
1792 (let ((quote-str viper-quote-string)
1793 (donot-change-dafault t))
1794 (setq quote-str
1795 (viper-read-string-with-history
1796 "Quote string: "
1798 'viper-quote-region-history
1799 (cond ((string-match "tex.*-mode" (symbol-name major-mode)) "%%")
1800 ((string-match "java.*-mode" (symbol-name major-mode)) "//")
1801 ((string-match "perl.*-mode" (symbol-name major-mode)) "#")
1802 ((string-match "lisp.*-mode" (symbol-name major-mode)) ";;")
1803 ((memq major-mode '(c-mode cc-mode c++-mode)) "//")
1804 ((memq major-mode '(sh-mode shell-mode)) "#")
1805 (t (setq donot-change-dafault nil)
1806 quote-str))))
1807 (or donot-change-dafault
1808 (setq viper-quote-string quote-str))
1809 (viper-enlarge-region (point) (mark t))
1810 (if (> (point) (mark t)) (exchange-point-and-mark))
1811 (insert quote-str)
1812 (beginning-of-line)
1813 (forward-line 1)
1814 (while (and (< (point) (mark t)) (bolp))
1815 (insert quote-str)
1816 (beginning-of-line)
1817 (forward-line 1))))
1819 ;; Tells whether BEG is on the same line as END.
1820 ;; If one of the args is nil, it'll return nil.
1821 (defun viper-same-line (beg end)
1822 (let ((selective-display nil)
1823 (incr 0)
1824 temp)
1825 (if (and beg end (> beg end))
1826 (setq temp beg
1827 beg end
1828 end temp))
1829 (if (and beg end)
1830 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
1831 nil)
1833 ;; This 'if' is needed because Emacs treats the next empty line
1834 ;; as part of the previous line.
1835 (if (= (viper-line-pos 'start) end)
1836 (setq incr 1))
1837 (<= (+ incr (count-lines beg end)) 1))))
1841 ;; Check if the string ends with a newline.
1842 (defun viper-end-with-a-newline-p (string)
1843 (or (string= string "")
1844 (= (viper-seq-last-elt string) ?\n)))
1846 (defun viper-tmp-insert-at-eob (msg)
1847 (let ((savemax (point-max)))
1848 (goto-char savemax)
1849 (insert msg)
1850 (sit-for 2)
1851 (goto-char savemax) (delete-region (point) (point-max))
1856 ;;; Minibuffer business
1858 (defsubst viper-set-minibuffer-style ()
1859 (add-hook 'minibuffer-setup-hook 'viper-minibuffer-setup-sentinel))
1862 (defun viper-minibuffer-setup-sentinel ()
1863 (let ((hook (if viper-vi-style-in-minibuffer
1864 'viper-change-state-to-insert
1865 'viper-change-state-to-emacs)))
1866 (funcall hook)
1869 ;; Thie is a temp hook that uses free variables init-message and initial.
1870 ;; A dirty feature, but it is the simplest way to have it do the right thing.
1871 ;; The INIT-MESSAGE and INITIAL vars come from the scope set by
1872 ;; viper-read-string-with-history
1873 (defun viper-minibuffer-standard-hook ()
1874 (if (stringp init-message)
1875 (viper-tmp-insert-at-eob init-message))
1876 (if (stringp initial)
1877 (progn
1878 ;; don't wait if we have unread events or in kbd macro
1879 (or unread-command-events
1880 executing-kbd-macro
1881 (sit-for 840))
1882 (if (fboundp 'minibuffer-prompt-end)
1883 (delete-region (minibuffer-prompt-end) (point-max))
1884 (erase-buffer))
1885 (insert initial)))
1886 (viper-minibuffer-setup-sentinel))
1888 (defsubst viper-minibuffer-real-start ()
1889 (if (fboundp 'minibuffer-prompt-end)
1890 (minibuffer-prompt-end)
1891 (point-min)))
1894 ;; Interpret last event in the local map first; if fails, use exit-minibuffer.
1895 ;; Run viper-minibuffer-exit-hook before exiting.
1896 (defun viper-exit-minibuffer ()
1897 "Exit minibuffer Viper way."
1898 (interactive)
1899 (let (command)
1900 (setq command (local-key-binding (char-to-string last-command-char)))
1901 (run-hooks 'viper-minibuffer-exit-hook)
1902 (if command
1903 (command-execute command)
1904 (exit-minibuffer))))
1907 (defcustom viper-smart-suffix-list
1908 '("" "tex" "c" "cc" "C" "java" "el" "html" "htm" "xml"
1909 "pl" "flr" "P" "p" "h" "H")
1910 "*List of suffixes that Viper tries to append to filenames ending with a `.'.
1911 This is useful when you the current directory contains files with the same
1912 prefix and many different suffixes. Usually, only one of the suffixes
1913 represents an editable file. However, file completion will stop at the `.'
1914 The smart suffix feature lets you hit RET in such a case, and Viper will
1915 select the appropriate suffix.
1917 Suffixes are tried in the order given and the first suffix for which a
1918 corresponding file exists is selected. If no file exists for any of the
1919 suffixes, the user is asked to confirm.
1921 To turn this feature off, set this variable to nil."
1922 :type '(repeat string)
1923 :group 'viper-misc)
1926 ;; Try to add a suitable suffix to files whose name ends with a `.'
1927 ;; Useful when the user hits RET on a non-completed file name.
1928 ;; Used as a minibuffer exit hook in read-file-name
1929 (defun viper-file-add-suffix ()
1930 (let ((count 0)
1931 (len (length viper-smart-suffix-list))
1932 (file (buffer-substring-no-properties
1933 (viper-minibuffer-real-start) (point-max)))
1934 found key cmd suff)
1935 (goto-char (point-max))
1936 (if (and viper-smart-suffix-list (string-match "\\.$" file))
1937 (progn
1938 (while (and (not found) (< count len))
1939 (setq suff (nth count viper-smart-suffix-list)
1940 count (1+ count))
1941 (if (file-exists-p
1942 (format "%s%s" (substitute-in-file-name file) suff))
1943 (progn
1944 (setq found t)
1945 (insert suff))))
1947 (if found
1949 (viper-tmp-insert-at-eob " [Please complete file name]")
1950 (unwind-protect
1951 (while (not (memq cmd
1952 '(exit-minibuffer viper-exit-minibuffer)))
1953 (setq cmd
1954 (key-binding (setq key (read-key-sequence nil))))
1955 (cond ((eq cmd 'self-insert-command)
1956 (if viper-xemacs-p
1957 (insert (events-to-keys key))
1958 (insert key)))
1959 ((memq cmd '(exit-minibuffer viper-exit-minibuffer))
1960 nil)
1961 (t (command-execute cmd)))
1963 ))))
1966 (defun viper-minibuffer-trim-tail ()
1967 "Delete junk at the end of the first line of the minibuffer input.
1968 Remove this function from `viper-minibuffer-exit-hook', if this causes
1969 problems."
1970 (if (viper-is-in-minibuffer)
1971 (progn
1972 (goto-char (viper-minibuffer-real-start))
1973 (end-of-line)
1974 (delete-region (point) (point-max)))))
1977 ;;; Reading string with history
1979 (defun viper-read-string-with-history (prompt &optional initial
1980 history-var default keymap
1981 init-message)
1982 ;; Read string, prompting with PROMPT and inserting the INITIAL
1983 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
1984 ;; input is an empty string.
1985 ;; Default value is displayed until the user types something in the
1986 ;; minibuffer.
1987 ;; KEYMAP is used, if given, instead of minibuffer-local-map.
1988 ;; INIT-MESSAGE is the message temporarily displayed after entering the
1989 ;; minibuffer.
1990 (let ((minibuffer-setup-hook 'viper-minibuffer-standard-hook)
1991 (val "")
1992 (padding "")
1993 temp-msg)
1995 (setq keymap (or keymap minibuffer-local-map)
1996 initial (or initial "")
1997 temp-msg (if default
1998 (format "(default: %s) " default)
1999 ""))
2001 (setq viper-incomplete-ex-cmd nil)
2002 (setq val (read-from-minibuffer prompt
2003 (concat temp-msg initial val padding)
2004 keymap nil history-var))
2005 (setq minibuffer-setup-hook nil
2006 padding (viper-array-to-string (this-command-keys))
2007 temp-msg "")
2008 ;; the following tries to be smart about what to put in history
2009 (if (not (string= val (car (eval history-var))))
2010 (set history-var (cons val (eval history-var))))
2011 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
2012 (string= (nth 0 (eval history-var)) ""))
2013 (set history-var (cdr (eval history-var))))
2014 ;; If the user enters nothing but the prev cmd wasn't viper-ex,
2015 ;; viper-command-argument, or `! shell-command', this probably means
2016 ;; that the user typed something then erased. Return "" in this case, not
2017 ;; the default---the default is too confusing in this case.
2018 (cond ((and (string= val "")
2019 (not (string= prompt "!")) ; was a `! shell-command'
2020 (not (memq last-command
2021 '(viper-ex
2022 viper-command-argument
2026 ((string= val "") (or default ""))
2027 (t val))
2032 ;; insertion commands
2034 ;; Called when state changes from Insert Vi command mode.
2035 ;; Repeats the insertion command if Insert state was entered with prefix
2036 ;; argument > 1.
2037 (defun viper-repeat-insert-command ()
2038 (let ((i-com (car viper-d-com))
2039 (val (nth 1 viper-d-com))
2040 (char (nth 2 viper-d-com)))
2041 (if (and val (> val 1)) ; first check that val is non-nil
2042 (progn
2043 (setq viper-d-com (list i-com (1- val) ?r nil nil nil))
2044 (viper-repeat nil)
2045 (setq viper-d-com (list i-com val char nil nil nil))
2046 ))))
2048 (defun viper-insert (arg)
2049 "Insert before point."
2050 (interactive "P")
2051 (viper-set-complex-command-for-undo)
2052 (let ((val (viper-p-val arg))
2053 (com (viper-getcom arg)))
2054 (viper-set-destructive-command (list 'viper-insert val ?r nil nil nil))
2055 (if com
2056 (viper-loop val (viper-yank-last-insertion))
2057 (viper-change-state-to-insert))))
2059 (defun viper-append (arg)
2060 "Append after point."
2061 (interactive "P")
2062 (viper-set-complex-command-for-undo)
2063 (let ((val (viper-p-val arg))
2064 (com (viper-getcom arg)))
2065 (viper-set-destructive-command (list 'viper-append val ?r nil nil nil))
2066 (if (not (eolp)) (forward-char))
2067 (if (viper= com ?r)
2068 (viper-loop val (viper-yank-last-insertion))
2069 (viper-change-state-to-insert))))
2071 (defun viper-Append (arg)
2072 "Append at end of line."
2073 (interactive "P")
2074 (viper-set-complex-command-for-undo)
2075 (let ((val (viper-p-val arg))
2076 (com (viper-getcom arg)))
2077 (viper-set-destructive-command (list 'viper-Append val ?r nil nil nil))
2078 (end-of-line)
2079 (if (viper= com ?r)
2080 (viper-loop val (viper-yank-last-insertion))
2081 (viper-change-state-to-insert))))
2083 (defun viper-Insert (arg)
2084 "Insert before first non-white."
2085 (interactive "P")
2086 (viper-set-complex-command-for-undo)
2087 (let ((val (viper-p-val arg))
2088 (com (viper-getcom arg)))
2089 (viper-set-destructive-command (list 'viper-Insert val ?r nil nil nil))
2090 (back-to-indentation)
2091 (if (viper= com ?r)
2092 (viper-loop val (viper-yank-last-insertion))
2093 (viper-change-state-to-insert))))
2095 (defun viper-open-line (arg)
2096 "Open line below."
2097 (interactive "P")
2098 (viper-set-complex-command-for-undo)
2099 (let ((val (viper-p-val arg))
2100 (com (viper-getcom arg)))
2101 (viper-set-destructive-command (list 'viper-open-line val ?r nil nil nil))
2102 (let ((col (current-indentation)))
2103 (if (viper= com ?r)
2104 (viper-loop val
2105 (end-of-line)
2106 (newline 1)
2107 (if viper-auto-indent
2108 (progn
2109 (setq viper-cted t)
2110 (if viper-electric-mode
2111 (indent-according-to-mode)
2112 (indent-to col))
2114 (viper-yank-last-insertion))
2115 (end-of-line)
2116 (newline 1)
2117 (if viper-auto-indent
2118 (progn
2119 (setq viper-cted t)
2120 (if viper-electric-mode
2121 (indent-according-to-mode)
2122 (indent-to col))))
2123 (viper-change-state-to-insert)))))
2125 (defun viper-Open-line (arg)
2126 "Open line above."
2127 (interactive "P")
2128 (viper-set-complex-command-for-undo)
2129 (let ((val (viper-p-val arg))
2130 (com (viper-getcom arg)))
2131 (viper-set-destructive-command (list 'viper-Open-line val ?r nil nil nil))
2132 (let ((col (current-indentation)))
2133 (if (viper= com ?r)
2134 (viper-loop val
2135 (beginning-of-line)
2136 (open-line 1)
2137 (if viper-auto-indent
2138 (progn
2139 (setq viper-cted t)
2140 (if viper-electric-mode
2141 (indent-according-to-mode)
2142 (indent-to col))
2144 (viper-yank-last-insertion))
2145 (beginning-of-line)
2146 (open-line 1)
2147 (if viper-auto-indent
2148 (progn
2149 (setq viper-cted t)
2150 (if viper-electric-mode
2151 (indent-according-to-mode)
2152 (indent-to col))
2154 (viper-change-state-to-insert)))))
2156 (defun viper-open-line-at-point (arg)
2157 "Open line at point."
2158 (interactive "P")
2159 (viper-set-complex-command-for-undo)
2160 (let ((val (viper-p-val arg))
2161 (com (viper-getcom arg)))
2162 (viper-set-destructive-command
2163 (list 'viper-open-line-at-point val ?r nil nil nil))
2164 (if (viper= com ?r)
2165 (viper-loop val
2166 (open-line 1)
2167 (viper-yank-last-insertion))
2168 (open-line 1)
2169 (viper-change-state-to-insert))))
2171 (defun viper-substitute (arg)
2172 "Substitute characters."
2173 (interactive "P")
2174 (let ((val (viper-p-val arg))
2175 (com (viper-getcom arg)))
2176 (push-mark nil t)
2177 (forward-char val)
2178 (if (viper= com ?r)
2179 (viper-change-subr (mark t) (point))
2180 (viper-change (mark t) (point)))
2181 (viper-set-destructive-command (list 'viper-substitute val ?r nil nil nil))
2184 ;; Command bound to S
2185 (defun viper-substitute-line (arg)
2186 "Substitute lines."
2187 (interactive "p")
2188 (viper-set-complex-command-for-undo)
2189 (viper-line (cons arg ?C)))
2191 ;; Prepare for replace
2192 (defun viper-start-replace ()
2193 (setq viper-began-as-replace t
2194 viper-sitting-in-replace t
2195 viper-replace-chars-to-delete 0)
2196 (add-hook
2197 'viper-after-change-functions 'viper-replace-mode-spy-after t 'local)
2198 (add-hook
2199 'viper-before-change-functions 'viper-replace-mode-spy-before t 'local)
2200 ;; this will get added repeatedly, but no harm
2201 (add-hook 'after-change-functions 'viper-after-change-sentinel t)
2202 (add-hook 'before-change-functions 'viper-before-change-sentinel t)
2203 (viper-move-marker-locally
2204 'viper-last-posn-in-replace-region (viper-replace-start))
2205 (add-hook
2206 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel
2207 t 'local)
2208 (add-hook
2209 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2210 ;; guard against a smartie who switched from R-replace to normal replace
2211 (remove-hook
2212 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2213 (if overwrite-mode (overwrite-mode nil))
2217 (defun viper-replace-mode-spy-before (beg end)
2218 (setq viper-replace-region-chars-deleted (viper-chars-in-region beg end))
2221 ;; Invoked as an after-change-function to calculate how many chars have to be
2222 ;; deleted. This function may be called several times within a single command,
2223 ;; if this command performs several separate buffer changes. Therefore, if
2224 ;; adds up the number of chars inserted and subtracts the number of chars
2225 ;; deleted.
2226 (defun viper-replace-mode-spy-after (beg end length)
2227 (if (memq viper-intermediate-command
2228 '(dabbrev-expand hippie-expand repeating-insertion-from-ring))
2229 ;; Take special care of text insertion from insertion ring inside
2230 ;; replacement overlays.
2231 (progn
2232 (setq viper-replace-chars-to-delete 0)
2233 (viper-move-marker-locally
2234 'viper-last-posn-in-replace-region (point)))
2236 (let* ((real-end (min end (viper-replace-end)))
2237 (column-shift (- (save-excursion (goto-char real-end)
2238 (current-column))
2239 (save-excursion (goto-char beg)
2240 (current-column))))
2241 (chars-deleted 0))
2243 (if (> length 0)
2244 (setq chars-deleted viper-replace-region-chars-deleted))
2245 (setq viper-replace-region-chars-deleted 0)
2246 (setq viper-replace-chars-to-delete
2247 (+ viper-replace-chars-to-delete
2249 ;; if column shift is bigger, due to a TAB insertion, take
2250 ;; column-shift instead of the number of inserted chars
2251 (max (viper-chars-in-region beg real-end)
2252 ;; This test accounts for Chinese/Japanese/... chars,
2253 ;; which occupy 2 columns instead of one. If we use
2254 ;; column-shift here, we may delete two chars instead of
2255 ;; one when the user types one Chinese character.
2256 ;; Deleting two would be OK, if they were European chars,
2257 ;; but it is not OK if they are Chinese chars.
2258 ;; Since it is hard to
2259 ;; figure out which characters are being deleted in any
2260 ;; given region, we decided to treat Eastern and European
2261 ;; characters equally, even though Eastern chars may
2262 ;; occupy more columns.
2263 (if (memq this-command '(self-insert-command
2264 quoted-insert viper-insert-tab))
2265 column-shift
2267 ;; the number of deleted chars
2268 chars-deleted)))
2270 (viper-move-marker-locally
2271 'viper-last-posn-in-replace-region
2272 (max (if (> end (viper-replace-end)) (viper-replace-end) end)
2273 (or (marker-position viper-last-posn-in-replace-region)
2274 (viper-replace-start))
2280 ;; Delete stuff between viper-last-posn-in-replace-region and the end of
2281 ;; viper-replace-overlay-marker, if viper-last-posn-in-replace-region is within
2282 ;; the overlay and current point is before the end of the overlay.
2283 ;; Don't delete anything if current point is past the end of the overlay.
2284 (defun viper-finish-change ()
2285 (remove-hook
2286 'viper-after-change-functions 'viper-replace-mode-spy-after 'local)
2287 (remove-hook
2288 'viper-before-change-functions 'viper-replace-mode-spy-before 'local)
2289 (remove-hook
2290 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2291 (remove-hook
2292 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2293 (viper-restore-cursor-color 'after-replace-mode)
2294 (setq viper-sitting-in-replace nil) ; just in case we'll need to know it
2295 (save-excursion
2296 (if (and viper-replace-overlay
2297 (viper-pos-within-region viper-last-posn-in-replace-region
2298 (viper-replace-start)
2299 (viper-replace-end))
2300 (< (point) (viper-replace-end)))
2301 (delete-region
2302 viper-last-posn-in-replace-region (viper-replace-end))))
2304 (if (eq viper-current-state 'replace-state)
2305 (viper-downgrade-to-insert))
2306 ;; replace mode ended => nullify viper-last-posn-in-replace-region
2307 (viper-move-marker-locally 'viper-last-posn-in-replace-region nil)
2308 (viper-hide-replace-overlay)
2309 (viper-refresh-mode-line)
2310 (viper-put-string-on-kill-ring viper-last-replace-region)
2313 ;; Make STRING be the first element of the kill ring.
2314 (defun viper-put-string-on-kill-ring (string)
2315 (setq kill-ring (cons string kill-ring))
2316 (if (> (length kill-ring) kill-ring-max)
2317 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2318 (setq kill-ring-yank-pointer kill-ring))
2320 (defun viper-finish-R-mode ()
2321 (remove-hook
2322 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2323 (remove-hook
2324 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2325 (viper-downgrade-to-insert))
2327 (defun viper-start-R-mode ()
2328 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2329 (overwrite-mode 1)
2330 (add-hook
2331 'viper-post-command-hooks 'viper-R-state-post-command-sentinel t 'local)
2332 (add-hook
2333 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2334 ;; guard against a smartie who switched from R-replace to normal replace
2335 (remove-hook
2336 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2341 (defun viper-replace-state-exit-cmd ()
2342 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2343 These keys are ESC, RET, and LineFeed"
2344 (interactive)
2345 (if overwrite-mode ; if in replace mode invoked via 'R'
2346 (viper-finish-R-mode)
2347 (viper-finish-change))
2348 (let (com)
2349 (if (eq this-command 'viper-intercept-ESC-key)
2350 (setq com 'viper-exit-insert-state)
2351 (viper-set-unread-command-events last-input-char)
2352 (setq com (key-binding (read-key-sequence nil))))
2354 (condition-case conds
2355 (command-execute com)
2356 (error
2357 (viper-message-conditions conds)))
2359 (viper-hide-replace-overlay))
2362 (defun viper-replace-state-carriage-return ()
2363 "Carriage return in Viper replace state."
2364 (interactive)
2365 ;; If Emacs start supporting overlay maps, as it currently supports
2366 ;; text-property maps, we could do away with viper-replace-minor-mode and
2367 ;; just have keymap attached to replace overlay. Then the "if part" of this
2368 ;; statement can be deleted.
2369 (if (or (< (point) (viper-replace-start))
2370 (> (point) (viper-replace-end)))
2371 (let (viper-replace-minor-mode com)
2372 (viper-set-unread-command-events last-input-char)
2373 (setq com (key-binding (read-key-sequence nil)))
2374 (condition-case conds
2375 (command-execute com)
2376 (error
2377 (viper-message-conditions conds))))
2378 (if (not viper-allow-multiline-replace-regions)
2379 (viper-replace-state-exit-cmd)
2380 (if (viper-same-line (point) (viper-replace-end))
2381 (viper-replace-state-exit-cmd)
2382 ;; delete the rest of line
2383 (delete-region (point) (viper-line-pos 'end))
2384 (save-excursion
2385 (end-of-line)
2386 (if (eobp) (error "Last line in buffer")))
2387 ;; skip to the next line
2388 (forward-line 1)
2389 (back-to-indentation)
2390 ))))
2393 ;; This is the function bound to 'R'---unlimited replace.
2394 ;; Similar to Emacs's own overwrite-mode.
2395 (defun viper-overwrite (arg)
2396 "Begin overwrite mode."
2397 (interactive "P")
2398 (let ((val (viper-p-val arg))
2399 (com (viper-getcom arg)) (len))
2400 (viper-set-destructive-command (list 'viper-overwrite val ?r nil nil nil))
2401 (if com
2402 (progn
2403 ;; Viper saves inserted text in viper-last-insertion
2404 (setq len (length viper-last-insertion))
2405 (delete-char len)
2406 (viper-loop val (viper-yank-last-insertion)))
2407 (setq last-command 'viper-overwrite)
2408 (viper-set-complex-command-for-undo)
2409 (viper-set-replace-overlay (point) (viper-line-pos 'end))
2410 (viper-change-state-to-replace)
2414 ;; line commands
2416 (defun viper-line (arg)
2417 (let ((val (car arg))
2418 (com (cdr arg)))
2419 (viper-move-marker-locally 'viper-com-point (point))
2420 (if (not (eobp))
2421 (viper-next-line-carefully (1- val)))
2422 ;; the following ensures that dd, cc, D, yy will do the right thing on the
2423 ;; last line of buffer when this line has no \n.
2424 (viper-add-newline-at-eob-if-necessary)
2425 (viper-execute-com 'viper-line val com))
2426 (if (and (eobp) (not (bobp))) (forward-line -1))
2429 (defun viper-yank-line (arg)
2430 "Yank ARG lines (in Vi's sense)."
2431 (interactive "P")
2432 (let ((val (viper-p-val arg)))
2433 (viper-line (cons val ?Y))))
2436 ;; region commands
2438 (defun viper-region (arg)
2439 "Execute command on a region."
2440 (interactive "P")
2441 (let ((val (viper-P-val arg))
2442 (com (viper-getcom arg)))
2443 (viper-move-marker-locally 'viper-com-point (point))
2444 (exchange-point-and-mark)
2445 (viper-execute-com 'viper-region val com)))
2447 (defun viper-Region (arg)
2448 "Execute command on a Region."
2449 (interactive "P")
2450 (let ((val (viper-P-val arg))
2451 (com (viper-getCom arg)))
2452 (viper-move-marker-locally 'viper-com-point (point))
2453 (exchange-point-and-mark)
2454 (viper-execute-com 'viper-Region val com)))
2456 (defun viper-replace-char (arg)
2457 "Replace the following ARG chars by the character read."
2458 (interactive "P")
2459 (if (and (eolp) (bolp)) (error "No character to replace here"))
2460 (let ((val (viper-p-val arg))
2461 (com (viper-getcom arg)))
2462 (viper-replace-char-subr com val)
2463 (if (and (eolp) (not (bolp))) (forward-char 1))
2464 (setq viper-this-command-keys
2465 (format "%sr" (if (integerp arg) arg "")))
2466 (viper-set-destructive-command
2467 (list 'viper-replace-char val ?r nil viper-d-char nil))
2470 (defun viper-replace-char-subr (com arg)
2471 (let (char)
2472 (setq char (if (viper= com ?r)
2473 viper-d-char
2474 (read-char)))
2475 (let (inhibit-quit) ; preserve consistency of undo-list and iso-accents
2476 (if (and viper-automatic-iso-accents
2477 (viper-memq-char char '(?' ?\" ?^ ?~)))
2478 ;; get European characters
2479 (progn
2480 (viper-set-iso-accents-mode t)
2481 (viper-set-unread-command-events char)
2482 (setq char (aref (read-key-sequence nil) 0))
2483 (viper-set-iso-accents-mode nil)))
2484 (viper-set-complex-command-for-undo)
2485 (if (eq char ?\C-m) (setq char ?\n))
2486 (if (and viper-special-input-method (fboundp 'quail-start-translation))
2487 ;; get Intl. characters
2488 (progn
2489 (viper-set-input-method t)
2490 (setq last-command-event
2491 (viper-copy-event
2492 (if viper-xemacs-p (character-to-event char) char)))
2493 (delete-char 1 t)
2494 (condition-case nil
2495 (if com
2496 (insert char)
2497 (if viper-emacs-p
2498 (quail-start-translation 1)
2499 (quail-start-translation)))
2500 (error))
2501 ;; quail translation failed
2502 (if (and (not (stringp quail-current-str))
2503 (not (viper-characterp quail-current-str)))
2504 (progn
2505 (viper-adjust-undo)
2506 (undo-start)
2507 (undo-more 1)
2508 (viper-set-input-method nil)
2509 (error "Composing character failed, changes undone")))
2510 ;; quail translation seems ok
2511 (or com
2512 ;;(setq char quail-current-str))
2513 (setq char (viper-char-at-pos 'backward)))
2514 (setq viper-d-char char)
2515 (viper-loop (1- (if (> arg 0) arg (- arg)))
2516 (delete-char 1 t)
2517 (insert char))
2518 (viper-set-input-method nil))
2519 (delete-char arg t)
2520 (setq viper-d-char char)
2521 (viper-loop (if (> arg 0) arg (- arg))
2522 (insert char)))
2523 (viper-adjust-undo)
2524 (backward-char arg))))
2527 ;; basic cursor movement. j, k, l, h commands.
2529 (defun viper-forward-char (arg)
2530 "Move point right ARG characters (left if ARG negative).
2531 On reaching end of line, stop and signal error."
2532 (interactive "P")
2533 (viper-leave-region-active)
2534 (let ((val (viper-p-val arg))
2535 (com (viper-getcom arg)))
2536 (if com (viper-move-marker-locally 'viper-com-point (point)))
2537 (if viper-ex-style-motion
2538 (progn
2539 ;; the boundary condition check gets weird here because
2540 ;; forward-char may be the parameter of a delete, and 'dl' works
2541 ;; just like 'x' for the last char on a line, so we have to allow
2542 ;; the forward motion before the 'viper-execute-com', but, of
2543 ;; course, 'dl' doesn't work on an empty line, so we have to
2544 ;; catch that condition before 'viper-execute-com'
2545 (if (and (eolp) (bolp)) (error "") (forward-char val))
2546 (if com (viper-execute-com 'viper-forward-char val com))
2547 (if (eolp) (progn (backward-char 1) (error ""))))
2548 (forward-char val)
2549 (if com (viper-execute-com 'viper-forward-char val com)))))
2552 (defun viper-backward-char (arg)
2553 "Move point left ARG characters (right if ARG negative).
2554 On reaching beginning of line, stop and signal error."
2555 (interactive "P")
2556 (viper-leave-region-active)
2557 (let ((val (viper-p-val arg))
2558 (com (viper-getcom arg)))
2559 (if com (viper-move-marker-locally 'viper-com-point (point)))
2560 (if viper-ex-style-motion
2561 (progn
2562 (if (bolp) (error "") (backward-char val))
2563 (if com (viper-execute-com 'viper-backward-char val com)))
2564 (backward-char val)
2565 (if com (viper-execute-com 'viper-backward-char val com)))))
2568 ;; Like forward-char, but doesn't move at end of buffer.
2569 ;; Returns distance traveled
2570 ;; (positive or 0, if arg positive; negative if arg negative).
2571 (defun viper-forward-char-carefully (&optional arg)
2572 (setq arg (or arg 1))
2573 (let ((pt (point)))
2574 (condition-case nil
2575 (forward-char arg)
2576 (error))
2577 (if (< (point) pt) ; arg was negative
2578 (- (viper-chars-in-region pt (point)))
2579 (viper-chars-in-region pt (point)))))
2582 ;; Like backward-char, but doesn't move at beg of buffer.
2583 ;; Returns distance traveled
2584 ;; (negative or 0, if arg positive; positive if arg negative).
2585 (defun viper-backward-char-carefully (&optional arg)
2586 (setq arg (or arg 1))
2587 (let ((pt (point)))
2588 (condition-case nil
2589 (backward-char arg)
2590 (error))
2591 (if (> (point) pt) ; arg was negative
2592 (viper-chars-in-region pt (point))
2593 (- (viper-chars-in-region pt (point))))))
2595 (defun viper-next-line-carefully (arg)
2596 (condition-case nil
2597 (next-line arg)
2598 (error nil)))
2602 ;;; Word command
2604 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators for
2605 ;; word movement. When executed with a destructive command, \n is usually left
2606 ;; untouched for the last word. Viper uses syntax table to determine what is a
2607 ;; word and what is a separator. However, \n is always a separator. Also, if
2608 ;; viper-syntax-preference is 'vi, then `_' is part of the word.
2610 ;; skip only one \n
2611 (defun viper-skip-separators (forward)
2612 (if forward
2613 (progn
2614 (viper-skip-all-separators-forward 'within-line)
2615 (if (looking-at "\n")
2616 (progn
2617 (forward-char)
2618 (viper-skip-all-separators-forward 'within-line))))
2619 ;; check for eob and white space before it. move off of eob
2620 (if (and (eobp) (save-excursion
2621 (viper-backward-char-carefully)
2622 (viper-looking-at-separator)))
2623 (viper-backward-char-carefully))
2624 (viper-skip-all-separators-backward 'within-line)
2625 (viper-backward-char-carefully)
2626 (if (looking-at "\n")
2627 (viper-skip-all-separators-backward 'within-line)
2628 (or (bobp) (forward-char)))))
2631 (defun viper-forward-word-kernel (val)
2632 (while (> val 0)
2633 (cond ((viper-looking-at-alpha)
2634 (viper-skip-alpha-forward "_")
2635 (viper-skip-separators t))
2636 ((viper-looking-at-separator)
2637 (viper-skip-separators t))
2638 ((not (viper-looking-at-alphasep))
2639 (viper-skip-nonalphasep-forward)
2640 (viper-skip-separators t)))
2641 (setq val (1- val))))
2643 ;; first skip non-newline separators backward, then skip \n. Then, if TWICE is
2644 ;; non-nil, skip non-\n back again, but don't overshoot the limit LIM.
2645 (defun viper-separator-skipback-special (twice lim)
2646 (let ((prev-char (viper-char-at-pos 'backward))
2647 (saved-point (point)))
2648 ;; skip non-newline separators backward
2649 (while (and (not (viper-memq-char prev-char '(nil \n)))
2650 (< lim (point))
2651 ;; must be non-newline separator
2652 (if (eq viper-syntax-preference 'strict-vi)
2653 (viper-memq-char prev-char '(?\ ?\t))
2654 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2655 (viper-backward-char-carefully)
2656 (setq prev-char (viper-char-at-pos 'backward)))
2658 (if (and (< lim (point)) (eq prev-char ?\n))
2659 (backward-char)
2660 ;; If we skipped to the next word and the prefix of this line doesn't
2661 ;; consist of separators preceded by a newline, then don't skip backwards
2662 ;; at all.
2663 (goto-char saved-point))
2664 (setq prev-char (viper-char-at-pos 'backward))
2666 ;; skip again, but make sure we don't overshoot the limit
2667 (if twice
2668 (while (and (not (viper-memq-char prev-char '(nil \n)))
2669 (< lim (point))
2670 ;; must be non-newline separator
2671 (if (eq viper-syntax-preference 'strict-vi)
2672 (viper-memq-char prev-char '(?\ ?\t))
2673 (viper-memq-char (char-syntax prev-char) '(?\ ?-))))
2674 (viper-backward-char-carefully)
2675 (setq prev-char (viper-char-at-pos 'backward))))
2677 (if (= (point) lim)
2678 (viper-forward-char-carefully))
2682 (defun viper-forward-word (arg)
2683 "Forward word."
2684 (interactive "P")
2685 (viper-leave-region-active)
2686 (let ((val (viper-p-val arg))
2687 (com (viper-getcom arg)))
2688 (if com (viper-move-marker-locally 'viper-com-point (point)))
2689 (viper-forward-word-kernel val)
2690 (if com (progn
2691 (cond ((viper-memq-char com (list ?c (- ?c)))
2692 (viper-separator-skipback-special 'twice viper-com-point))
2693 ;; Yank words including the whitespace, but not newline
2694 ((viper-memq-char com (list ?y (- ?y)))
2695 (viper-separator-skipback-special nil viper-com-point))
2696 ((viper-dotable-command-p com)
2697 (viper-separator-skipback-special nil viper-com-point)))
2698 (viper-execute-com 'viper-forward-word val com)))))
2701 (defun viper-forward-Word (arg)
2702 "Forward word delimited by white characters."
2703 (interactive "P")
2704 (viper-leave-region-active)
2705 (let ((val (viper-p-val arg))
2706 (com (viper-getcom arg)))
2707 (if com (viper-move-marker-locally 'viper-com-point (point)))
2708 (viper-loop val
2709 (viper-skip-nonseparators 'forward)
2710 (viper-skip-separators t))
2711 (if com (progn
2712 (cond ((viper-memq-char com (list ?c (- ?c)))
2713 (viper-separator-skipback-special 'twice viper-com-point))
2714 ;; Yank words including the whitespace, but not newline
2715 ((viper-memq-char com (list ?y (- ?y)))
2716 (viper-separator-skipback-special nil viper-com-point))
2717 ((viper-dotable-command-p com)
2718 (viper-separator-skipback-special nil viper-com-point)))
2719 (viper-execute-com 'viper-forward-Word val com)))))
2722 ;; this is a bit different from Vi, but Vi's end of word
2723 ;; makes no sense whatsoever
2724 (defun viper-end-of-word-kernel ()
2725 (if (viper-end-of-word-p) (forward-char))
2726 (if (viper-looking-at-separator)
2727 (viper-skip-all-separators-forward))
2729 (cond ((viper-looking-at-alpha) (viper-skip-alpha-forward "_"))
2730 ((not (viper-looking-at-alphasep)) (viper-skip-nonalphasep-forward)))
2731 (viper-backward-char-carefully))
2733 (defun viper-end-of-word-p ()
2734 (or (eobp)
2735 (save-excursion
2736 (cond ((viper-looking-at-alpha)
2737 (forward-char)
2738 (not (viper-looking-at-alpha)))
2739 ((not (viper-looking-at-alphasep))
2740 (forward-char)
2741 (viper-looking-at-alphasep))))))
2744 (defun viper-end-of-word (arg &optional careful)
2745 "Move point to end of current word."
2746 (interactive "P")
2747 (viper-leave-region-active)
2748 (let ((val (viper-p-val arg))
2749 (com (viper-getcom arg)))
2750 (if com (viper-move-marker-locally 'viper-com-point (point)))
2751 (viper-loop val (viper-end-of-word-kernel))
2752 (if com
2753 (progn
2754 (forward-char)
2755 (viper-execute-com 'viper-end-of-word val com)))))
2757 (defun viper-end-of-Word (arg)
2758 "Forward to end of word delimited by white character."
2759 (interactive "P")
2760 (viper-leave-region-active)
2761 (let ((val (viper-p-val arg))
2762 (com (viper-getcom arg)))
2763 (if com (viper-move-marker-locally 'viper-com-point (point)))
2764 (viper-loop val
2765 (viper-end-of-word-kernel)
2766 (viper-skip-nonseparators 'forward)
2767 (backward-char))
2768 (if com
2769 (progn
2770 (forward-char)
2771 (viper-execute-com 'viper-end-of-Word val com)))))
2773 (defun viper-backward-word-kernel (val)
2774 (while (> val 0)
2775 (viper-backward-char-carefully)
2776 (cond ((viper-looking-at-alpha)
2777 (viper-skip-alpha-backward "_"))
2778 ((viper-looking-at-separator)
2779 (forward-char)
2780 (viper-skip-separators nil)
2781 (viper-backward-char-carefully)
2782 (cond ((viper-looking-at-alpha)
2783 (viper-skip-alpha-backward "_"))
2784 ((not (viper-looking-at-alphasep))
2785 (viper-skip-nonalphasep-backward))
2786 ((bobp)) ; could still be at separator, but at beg of buffer
2787 (t (forward-char))))
2788 ((not (viper-looking-at-alphasep))
2789 (viper-skip-nonalphasep-backward)))
2790 (setq val (1- val))))
2792 (defun viper-backward-word (arg)
2793 "Backward word."
2794 (interactive "P")
2795 (viper-leave-region-active)
2796 (let ((val (viper-p-val arg))
2797 (com (viper-getcom arg)))
2798 (if com
2799 (let (i)
2800 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2801 (backward-char))
2802 (viper-move-marker-locally 'viper-com-point (point))
2803 (if i (forward-char))))
2804 (viper-backward-word-kernel val)
2805 (if com (viper-execute-com 'viper-backward-word val com))))
2807 (defun viper-backward-Word (arg)
2808 "Backward word delimited by white character."
2809 (interactive "P")
2810 (viper-leave-region-active)
2811 (let ((val (viper-p-val arg))
2812 (com (viper-getcom arg)))
2813 (if com
2814 (let (i)
2815 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2816 (backward-char))
2817 (viper-move-marker-locally 'viper-com-point (point))
2818 (if i (forward-char))))
2819 (viper-loop val
2820 (viper-skip-separators nil) ; nil means backward here
2821 (viper-skip-nonseparators 'backward))
2822 (if com (viper-execute-com 'viper-backward-Word val com))))
2826 ;; line commands
2828 (defun viper-beginning-of-line (arg)
2829 "Go to beginning of line."
2830 (interactive "P")
2831 (viper-leave-region-active)
2832 (let ((val (viper-p-val arg))
2833 (com (viper-getcom arg)))
2834 (if com (viper-move-marker-locally 'viper-com-point (point)))
2835 (beginning-of-line val)
2836 (if com (viper-execute-com 'viper-beginning-of-line val com))))
2838 (defun viper-bol-and-skip-white (arg)
2839 "Beginning of line at first non-white character."
2840 (interactive "P")
2841 (viper-leave-region-active)
2842 (let ((val (viper-p-val arg))
2843 (com (viper-getcom arg)))
2844 (if com (viper-move-marker-locally 'viper-com-point (point)))
2845 (forward-to-indentation (1- val))
2846 (if com (viper-execute-com 'viper-bol-and-skip-white val com))))
2848 (defun viper-goto-eol (arg)
2849 "Go to end of line."
2850 (interactive "P")
2851 (viper-leave-region-active)
2852 (let ((val (viper-p-val arg))
2853 (com (viper-getcom arg)))
2854 (if com (viper-move-marker-locally 'viper-com-point (point)))
2855 (end-of-line val)
2856 (if com (viper-execute-com 'viper-goto-eol val com))
2857 (if viper-ex-style-motion
2858 (if (and (eolp) (not (bolp))
2859 ;; a fix for viper-change-to-eol
2860 (not (equal viper-current-state 'insert-state)))
2861 (backward-char 1)
2862 ))))
2865 (defun viper-goto-col (arg)
2866 "Go to ARG's column."
2867 (interactive "P")
2868 (viper-leave-region-active)
2869 (let ((val (viper-p-val arg))
2870 (com (viper-getcom arg))
2871 line-len)
2872 (setq line-len
2873 (viper-chars-in-region
2874 (viper-line-pos 'start) (viper-line-pos 'end)))
2875 (if com (viper-move-marker-locally 'viper-com-point (point)))
2876 (beginning-of-line)
2877 (forward-char (1- (min line-len val)))
2878 (while (> (current-column) (1- val))
2879 (backward-char 1))
2880 (if com (viper-execute-com 'viper-goto-col val com))
2881 (save-excursion
2882 (end-of-line)
2883 (if (> val (current-column)) (error "")))
2887 (defun viper-next-line (arg)
2888 "Go to next line."
2889 (interactive "P")
2890 (viper-leave-region-active)
2891 (let ((val (viper-p-val arg))
2892 (com (viper-getCom arg)))
2893 (if com (viper-move-marker-locally 'viper-com-point (point)))
2894 (next-line val)
2895 (if viper-ex-style-motion
2896 (if (and (eolp) (not (bolp))) (backward-char 1)))
2897 (setq this-command 'next-line)
2898 (if com (viper-execute-com 'viper-next-line val com))))
2900 (defun viper-next-line-at-bol (arg)
2901 "Next line at beginning of line."
2902 (interactive "P")
2903 (viper-leave-region-active)
2904 (save-excursion
2905 (end-of-line)
2906 (if (eobp) (error "Last line in buffer")))
2907 (let ((val (viper-p-val arg))
2908 (com (viper-getCom arg)))
2909 (if com (viper-move-marker-locally 'viper-com-point (point)))
2910 (forward-line val)
2911 (back-to-indentation)
2912 (if com (viper-execute-com 'viper-next-line-at-bol val com))))
2915 (defun viper-previous-line (arg)
2916 "Go to previous line."
2917 (interactive "P")
2918 (viper-leave-region-active)
2919 (let ((val (viper-p-val arg))
2920 (com (viper-getCom arg)))
2921 (if com (viper-move-marker-locally 'viper-com-point (point)))
2922 (previous-line val)
2923 (if viper-ex-style-motion
2924 (if (and (eolp) (not (bolp))) (backward-char 1)))
2925 (setq this-command 'previous-line)
2926 (if com (viper-execute-com 'viper-previous-line val com))))
2929 (defun viper-previous-line-at-bol (arg)
2930 "Previous line at beginning of line."
2931 (interactive "P")
2932 (viper-leave-region-active)
2933 (save-excursion
2934 (beginning-of-line)
2935 (if (bobp) (error "First line in buffer")))
2936 (let ((val (viper-p-val arg))
2937 (com (viper-getCom arg)))
2938 (if com (viper-move-marker-locally 'viper-com-point (point)))
2939 (forward-line (- val))
2940 (back-to-indentation)
2941 (if com (viper-execute-com 'viper-previous-line val com))))
2943 (defun viper-change-to-eol (arg)
2944 "Change to end of line."
2945 (interactive "P")
2946 (viper-goto-eol (cons arg ?c)))
2948 (defun viper-kill-line (arg)
2949 "Delete line."
2950 (interactive "P")
2951 (viper-goto-eol (cons arg ?d)))
2953 (defun viper-erase-line (arg)
2954 "Erase line."
2955 (interactive "P")
2956 (viper-beginning-of-line (cons arg ?d)))
2959 ;;; Moving around
2961 (defun viper-goto-line (arg)
2962 "Go to ARG's line. Without ARG go to end of buffer."
2963 (interactive "P")
2964 (let ((val (viper-P-val arg))
2965 (com (viper-getCom arg)))
2966 (viper-move-marker-locally 'viper-com-point (point))
2967 (viper-deactivate-mark)
2968 (push-mark nil t)
2969 (if (null val)
2970 (goto-char (point-max))
2971 (goto-char (point-min))
2972 (forward-line (1- val)))
2974 ;; positioning is done twice: before and after command execution
2975 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2976 (back-to-indentation)
2978 (if com (viper-execute-com 'viper-goto-line val com))
2980 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2981 (back-to-indentation)
2984 ;; Find ARG's occurrence of CHAR on the current line.
2985 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
2986 ;; adjust point after search.
2987 (defun viper-find-char (arg char forward offset)
2988 (or (char-or-string-p char) (error ""))
2989 (let ((arg (if forward arg (- arg)))
2990 (cmd (if (eq viper-intermediate-command 'viper-repeat)
2991 (nth 5 viper-d-com)
2992 (viper-array-to-string (this-command-keys))))
2993 point region-beg region-end)
2994 (save-excursion
2995 (save-restriction
2996 (if (> arg 0) ; forward
2997 (progn
2998 (setq region-beg (point))
2999 (if viper-allow-multiline-replace-regions
3000 (viper-forward-paragraph 1)
3001 (end-of-line))
3002 (setq region-end (point)))
3003 (setq region-end (point))
3004 (if viper-allow-multiline-replace-regions
3005 (viper-backward-paragraph 1)
3006 (beginning-of-line))
3007 (setq region-beg (point)))
3008 (if (or (and (< arg 0)
3009 (< (- region-end region-beg)
3010 (if viper-allow-multiline-replace-regions
3011 2 1))
3012 (bolp))
3013 (and (> arg 0)
3014 (< (- region-end region-beg)
3015 (if viper-allow-multiline-replace-regions
3016 3 2))
3017 (eolp)))
3018 (error "Command `%s': At %s of %s"
3020 (if (> arg 0) "end" "beginning")
3021 (if viper-allow-multiline-replace-regions
3022 "paragraph" "line")))
3023 (narrow-to-region region-beg region-end)
3024 ;; if arg > 0, point is forwarded before search.
3025 (if (> arg 0) (goto-char (1+ (point-min)))
3026 (goto-char (point-max)))
3027 (if (let ((case-fold-search nil))
3028 (search-forward (char-to-string char) nil 0 arg))
3029 (setq point (point))
3030 (error "Command `%s': `%c' not found" cmd char))))
3031 (goto-char point)
3032 (if (> arg 0)
3033 (backward-char (if offset 2 1))
3034 (forward-char (if offset 1 0)))))
3036 (defun viper-find-char-forward (arg)
3037 "Find char on the line.
3038 If called interactively read the char to find from the terminal, and if
3039 called from viper-repeat, the char last used is used. This behaviour is
3040 controlled by the sign of prefix numeric value."
3041 (interactive "P")
3042 (let ((val (viper-p-val arg))
3043 (com (viper-getcom arg))
3044 (cmd-representation (nth 5 viper-d-com)))
3045 (if (> val 0)
3046 ;; this means that the function was called interactively
3047 (setq viper-f-char (read-char)
3048 viper-f-forward t
3049 viper-f-offset nil)
3050 ;; viper-repeat --- set viper-F-char from command-keys
3051 (setq viper-F-char (if (stringp cmd-representation)
3052 (viper-seq-last-elt cmd-representation)
3053 viper-F-char)
3054 viper-f-char viper-F-char)
3055 (setq val (- val)))
3056 (if com (viper-move-marker-locally 'viper-com-point (point)))
3057 (viper-find-char
3058 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t nil)
3059 (setq val (- val))
3060 (if com
3061 (progn
3062 (setq viper-F-char viper-f-char) ; set new viper-F-char
3063 (forward-char)
3064 (viper-execute-com 'viper-find-char-forward val com)))))
3066 (defun viper-goto-char-forward (arg)
3067 "Go up to char ARG forward on line."
3068 (interactive "P")
3069 (let ((val (viper-p-val arg))
3070 (com (viper-getcom arg))
3071 (cmd-representation (nth 5 viper-d-com)))
3072 (if (> val 0)
3073 ;; this means that the function was called interactively
3074 (setq viper-f-char (read-char)
3075 viper-f-forward t
3076 viper-f-offset t)
3077 ;; viper-repeat --- set viper-F-char from command-keys
3078 (setq viper-F-char (if (stringp cmd-representation)
3079 (viper-seq-last-elt cmd-representation)
3080 viper-F-char)
3081 viper-f-char viper-F-char)
3082 (setq val (- val)))
3083 (if com (viper-move-marker-locally 'viper-com-point (point)))
3084 (viper-find-char
3085 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t t)
3086 (setq val (- val))
3087 (if com
3088 (progn
3089 (setq viper-F-char viper-f-char) ; set new viper-F-char
3090 (forward-char)
3091 (viper-execute-com 'viper-goto-char-forward val com)))))
3093 (defun viper-find-char-backward (arg)
3094 "Find char ARG on line backward."
3095 (interactive "P")
3096 (let ((val (viper-p-val arg))
3097 (com (viper-getcom arg))
3098 (cmd-representation (nth 5 viper-d-com)))
3099 (if (> val 0)
3100 ;; this means that the function was called interactively
3101 (setq viper-f-char (read-char)
3102 viper-f-forward nil
3103 viper-f-offset nil)
3104 ;; viper-repeat --- set viper-F-char from command-keys
3105 (setq viper-F-char (if (stringp cmd-representation)
3106 (viper-seq-last-elt cmd-representation)
3107 viper-F-char)
3108 viper-f-char viper-F-char)
3109 (setq val (- val)))
3110 (if com (viper-move-marker-locally 'viper-com-point (point)))
3111 (viper-find-char
3112 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil nil)
3113 (setq val (- val))
3114 (if com
3115 (progn
3116 (setq viper-F-char viper-f-char) ; set new viper-F-char
3117 (viper-execute-com 'viper-find-char-backward val com)))))
3119 (defun viper-goto-char-backward (arg)
3120 "Go up to char ARG backward on line."
3121 (interactive "P")
3122 (let ((val (viper-p-val arg))
3123 (com (viper-getcom arg))
3124 (cmd-representation (nth 5 viper-d-com)))
3125 (if (> val 0)
3126 ;; this means that the function was called interactively
3127 (setq viper-f-char (read-char)
3128 viper-f-forward nil
3129 viper-f-offset t)
3130 ;; viper-repeat --- set viper-F-char from command-keys
3131 (setq viper-F-char (if (stringp cmd-representation)
3132 (viper-seq-last-elt cmd-representation)
3133 viper-F-char)
3134 viper-f-char viper-F-char)
3135 (setq val (- val)))
3136 (if com (viper-move-marker-locally 'viper-com-point (point)))
3137 (viper-find-char
3138 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil t)
3139 (setq val (- val))
3140 (if com
3141 (progn
3142 (setq viper-F-char viper-f-char) ; set new viper-F-char
3143 (viper-execute-com 'viper-goto-char-backward val com)))))
3145 (defun viper-repeat-find (arg)
3146 "Repeat previous find command."
3147 (interactive "P")
3148 (let ((val (viper-p-val arg))
3149 (com (viper-getcom arg)))
3150 (viper-deactivate-mark)
3151 (if com (viper-move-marker-locally 'viper-com-point (point)))
3152 (viper-find-char val viper-f-char viper-f-forward viper-f-offset)
3153 (if com
3154 (progn
3155 (if viper-f-forward (forward-char))
3156 (viper-execute-com 'viper-repeat-find val com)))))
3158 (defun viper-repeat-find-opposite (arg)
3159 "Repeat previous find command in the opposite direction."
3160 (interactive "P")
3161 (let ((val (viper-p-val arg))
3162 (com (viper-getcom arg)))
3163 (viper-deactivate-mark)
3164 (if com (viper-move-marker-locally 'viper-com-point (point)))
3165 (viper-find-char val viper-f-char (not viper-f-forward) viper-f-offset)
3166 (if com
3167 (progn
3168 (if viper-f-forward (forward-char))
3169 (viper-execute-com 'viper-repeat-find-opposite val com)))))
3172 ;; window scrolling etc.
3174 (defun viper-window-top (arg)
3175 "Go to home window line."
3176 (interactive "P")
3177 (let ((val (viper-p-val arg))
3178 (com (viper-getCom arg)))
3179 (viper-leave-region-active)
3180 (if com (viper-move-marker-locally 'viper-com-point (point)))
3181 (push-mark nil t)
3182 (move-to-window-line (1- val))
3184 ;; positioning is done twice: before and after command execution
3185 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3186 (back-to-indentation)
3188 (if com (viper-execute-com 'viper-window-top val com))
3190 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3191 (back-to-indentation)
3194 (defun viper-window-middle (arg)
3195 "Go to middle window line."
3196 (interactive "P")
3197 (let ((val (viper-p-val arg))
3198 (com (viper-getCom arg)))
3199 (viper-leave-region-active)
3200 (if com (viper-move-marker-locally 'viper-com-point (point)))
3201 (push-mark nil t)
3202 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3204 ;; positioning is done twice: before and after command execution
3205 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3206 (back-to-indentation)
3208 (if com (viper-execute-com 'viper-window-middle val com))
3210 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3211 (back-to-indentation)
3214 (defun viper-window-bottom (arg)
3215 "Go to last window line."
3216 (interactive "P")
3217 (let ((val (viper-p-val arg))
3218 (com (viper-getCom arg)))
3219 (viper-leave-region-active)
3220 (if com (viper-move-marker-locally 'viper-com-point (point)))
3221 (push-mark nil t)
3222 (move-to-window-line (- val))
3224 ;; positioning is done twice: before and after command execution
3225 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3226 (back-to-indentation)
3228 (if com (viper-execute-com 'viper-window-bottom val com))
3230 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3231 (back-to-indentation)
3234 (defun viper-line-to-top (arg)
3235 "Put current line on the home line."
3236 (interactive "p")
3237 (recenter (1- arg)))
3239 (defun viper-line-to-middle (arg)
3240 "Put current line on the middle line."
3241 (interactive "p")
3242 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3244 (defun viper-line-to-bottom (arg)
3245 "Put current line on the last line."
3246 (interactive "p")
3247 (recenter (- (window-height) (1+ arg))))
3249 ;; If point is within viper-search-scroll-threshold of window top or bottom,
3250 ;; scroll up or down 1/7 of window height, depending on whether we are at the
3251 ;; bottom or at the top of the window. This function is called by viper-search
3252 ;; (which is called from viper-search-forward/backward/next). If the value of
3253 ;; viper-search-scroll-threshold is negative - don't scroll.
3254 (defun viper-adjust-window ()
3255 (let ((win-height (if viper-emacs-p
3256 (1- (window-height)) ; adjust for modeline
3257 (window-displayed-height)))
3258 (pt (point))
3259 at-top-p at-bottom-p
3260 min-scroll direction)
3261 (save-excursion
3262 (move-to-window-line 0) ; top
3263 (setq at-top-p
3264 (<= (count-lines pt (point))
3265 viper-search-scroll-threshold))
3266 (move-to-window-line -1) ; bottom
3267 (setq at-bottom-p
3268 (<= (count-lines pt (point)) viper-search-scroll-threshold))
3270 (cond (at-top-p (setq min-scroll (1- viper-search-scroll-threshold)
3271 direction 1))
3272 (at-bottom-p (setq min-scroll (1+ viper-search-scroll-threshold)
3273 direction -1)))
3274 (if min-scroll
3275 (recenter
3276 (* (max min-scroll (/ win-height 7)) direction)))
3280 ;; paren match
3281 ;; must correct this to only match ( to ) etc. On the other hand
3282 ;; it is good that paren match gets confused, because that way you
3283 ;; catch _all_ imbalances.
3285 (defun viper-paren-match (arg)
3286 "Go to the matching parenthesis."
3287 (interactive "P")
3288 (viper-leave-region-active)
3289 (let ((com (viper-getcom arg))
3290 (parse-sexp-ignore-comments viper-parse-sexp-ignore-comments)
3291 anchor-point)
3292 (if (integerp arg)
3293 (if (or (> arg 99) (< arg 1))
3294 (error "Prefix must be between 1 and 99")
3295 (goto-char
3296 (if (> (point-max) 80000)
3297 (* (/ (point-max) 100) arg)
3298 (/ (* (point-max) arg) 100)))
3299 (back-to-indentation))
3300 (let (beg-lim end-lim)
3301 (if (and (eolp) (not (bolp))) (forward-char -1))
3302 (if (not (looking-at "[][(){}]"))
3303 (setq anchor-point (point)))
3304 (save-excursion
3305 (beginning-of-line)
3306 (setq beg-lim (point))
3307 (end-of-line)
3308 (setq end-lim (point)))
3309 (cond ((re-search-forward "[][(){}]" end-lim t)
3310 (backward-char) )
3311 ((re-search-backward "[][(){}]" beg-lim t))
3313 (error "No matching character on line"))))
3314 (cond ((looking-at "[\(\[{]")
3315 (if com (viper-move-marker-locally 'viper-com-point (point)))
3316 (forward-sexp 1)
3317 (if com
3318 (viper-execute-com 'viper-paren-match nil com)
3319 (backward-char)))
3320 (anchor-point
3321 (if com
3322 (progn
3323 (viper-move-marker-locally 'viper-com-point anchor-point)
3324 (forward-char 1)
3325 (viper-execute-com 'viper-paren-match nil com)
3327 ((looking-at "[])}]")
3328 (forward-char)
3329 (if com (viper-move-marker-locally 'viper-com-point (point)))
3330 (backward-sexp 1)
3331 (if com (viper-execute-com 'viper-paren-match nil com)))
3332 (t (error ""))))))
3334 (defun viper-toggle-parse-sexp-ignore-comments ()
3335 (interactive)
3336 (setq viper-parse-sexp-ignore-comments
3337 (not viper-parse-sexp-ignore-comments))
3338 (princ (format
3339 "From now on, `%%' will %signore parentheses inside comment fields"
3340 (if viper-parse-sexp-ignore-comments "" "NOT "))))
3343 ;; sentence, paragraph and heading
3345 (defun viper-forward-sentence (arg)
3346 "Forward sentence."
3347 (interactive "P")
3348 (or (eq last-command this-command)
3349 (push-mark nil t))
3350 (let ((val (viper-p-val arg))
3351 (com (viper-getcom arg)))
3352 (if com (viper-move-marker-locally 'viper-com-point (point)))
3353 (forward-sentence val)
3354 (if com (viper-execute-com 'viper-forward-sentence nil com))))
3356 (defun viper-backward-sentence (arg)
3357 "Backward sentence."
3358 (interactive "P")
3359 (or (eq last-command this-command)
3360 (push-mark nil t))
3361 (let ((val (viper-p-val arg))
3362 (com (viper-getcom arg)))
3363 (if com (viper-move-marker-locally 'viper-com-point (point)))
3364 (backward-sentence val)
3365 (if com (viper-execute-com 'viper-backward-sentence nil com))))
3367 (defun viper-forward-paragraph (arg)
3368 "Forward paragraph."
3369 (interactive "P")
3370 (or (eq last-command this-command)
3371 (push-mark nil t))
3372 (let ((val (viper-p-val arg))
3373 ;; if you want d} operate on whole lines, change viper-getcom to
3374 ;; viper-getCom below
3375 (com (viper-getcom arg)))
3376 (if com (viper-move-marker-locally 'viper-com-point (point)))
3377 (forward-paragraph val)
3378 (if com
3379 (progn
3380 (backward-char 1)
3381 (viper-execute-com 'viper-forward-paragraph nil com)))))
3383 (defun viper-backward-paragraph (arg)
3384 "Backward paragraph."
3385 (interactive "P")
3386 (or (eq last-command this-command)
3387 (push-mark nil t))
3388 (let ((val (viper-p-val arg))
3389 ;; if you want d{ operate on whole lines, change viper-getcom to
3390 ;; viper-getCom below
3391 (com (viper-getcom arg)))
3392 (if com (viper-move-marker-locally 'viper-com-point (point)))
3393 (backward-paragraph val)
3394 (if com
3395 (progn
3396 (forward-char 1)
3397 (viper-execute-com 'viper-backward-paragraph nil com)
3398 (backward-char 1)))))
3400 ;; should be mode-specific
3401 (defun viper-prev-heading (arg)
3402 (interactive "P")
3403 (let ((val (viper-p-val arg))
3404 (com (viper-getCom arg)))
3405 (if com (viper-move-marker-locally 'viper-com-point (point)))
3406 (re-search-backward viper-heading-start nil t val)
3407 (goto-char (match-beginning 0))
3408 (if com (viper-execute-com 'viper-prev-heading nil com))))
3410 (defun viper-heading-end (arg)
3411 (interactive "P")
3412 (let ((val (viper-p-val arg))
3413 (com (viper-getCom arg)))
3414 (if com (viper-move-marker-locally 'viper-com-point (point)))
3415 (re-search-forward viper-heading-end nil t val)
3416 (goto-char (match-beginning 0))
3417 (if com (viper-execute-com 'viper-heading-end nil com))))
3419 (defun viper-next-heading (arg)
3420 (interactive "P")
3421 (let ((val (viper-p-val arg))
3422 (com (viper-getCom arg)))
3423 (if com (viper-move-marker-locally 'viper-com-point (point)))
3424 (end-of-line)
3425 (re-search-forward viper-heading-start nil t val)
3426 (goto-char (match-beginning 0))
3427 (if com (viper-execute-com 'viper-next-heading nil com))))
3430 ;; scrolling
3432 (defun viper-scroll-screen (arg)
3433 "Scroll to next screen."
3434 (interactive "p")
3435 (condition-case nil
3436 (if (> arg 0)
3437 (while (> arg 0)
3438 (scroll-up)
3439 (setq arg (1- arg)))
3440 (while (> 0 arg)
3441 (scroll-down)
3442 (setq arg (1+ arg))))
3443 (error (beep 1)
3444 (if (> arg 0)
3445 (progn
3446 (message "End of buffer")
3447 (goto-char (point-max)))
3448 (message "Beginning of buffer")
3449 (goto-char (point-min))))
3452 (defun viper-scroll-screen-back (arg)
3453 "Scroll to previous screen."
3454 (interactive "p")
3455 (viper-scroll-screen (- arg)))
3457 (defun viper-scroll-down (arg)
3458 "Pull down half screen."
3459 (interactive "P")
3460 (condition-case nil
3461 (if (null arg)
3462 (scroll-down (/ (window-height) 2))
3463 (scroll-down arg))
3464 (error (beep 1)
3465 (message "Beginning of buffer")
3466 (goto-char (point-min)))))
3468 (defun viper-scroll-down-one (arg)
3469 "Scroll up one line."
3470 (interactive "p")
3471 (scroll-down arg))
3473 (defun viper-scroll-up (arg)
3474 "Pull up half screen."
3475 (interactive "P")
3476 (condition-case nil
3477 (if (null arg)
3478 (scroll-up (/ (window-height) 2))
3479 (scroll-up arg))
3480 (error (beep 1)
3481 (message "End of buffer")
3482 (goto-char (point-max)))))
3484 (defun viper-scroll-up-one (arg)
3485 "Scroll down one line."
3486 (interactive "p")
3487 (scroll-up arg))
3490 ;; searching
3492 (defun viper-if-string (prompt)
3493 (if (memq viper-intermediate-command
3494 '(viper-command-argument viper-digit-argument viper-repeat))
3495 (setq viper-this-command-keys (this-command-keys)))
3496 (let ((s (viper-read-string-with-history
3497 prompt
3498 nil ; no initial
3499 'viper-search-history
3500 (car viper-search-history))))
3501 (if (not (string= s ""))
3502 (setq viper-s-string s))))
3505 (defun viper-toggle-search-style (arg)
3506 "Toggle the value of viper-case-fold-search/viper-re-search.
3507 Without prefix argument, will ask which search style to toggle. With prefix
3508 arg 1,toggles viper-case-fold-search; with arg 2 toggles viper-re-search.
3510 Although this function is bound to \\[viper-toggle-search-style], the most
3511 convenient way to use it is to bind `//' to the macro
3512 `1 M-x viper-toggle-search-style' and `///' to
3513 `2 M-x viper-toggle-search-style'. In this way, hitting `//' quickly will
3514 toggle case-fold-search and hitting `/' three times witth toggle regexp
3515 search. Macros are more convenient in this case because they don't affect
3516 the Emacs binding of `/'."
3517 (interactive "P")
3518 (let (msg)
3519 (cond ((or (eq arg 1)
3520 (and (null arg)
3521 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3522 (if viper-case-fold-search
3523 "case-insensitive" "case-sensitive")
3524 (if viper-case-fold-search
3525 "case-sensitive"
3526 "case-insensitive")))))
3527 (setq viper-case-fold-search (null viper-case-fold-search))
3528 (if viper-case-fold-search
3529 (setq msg "Search becomes case-insensitive")
3530 (setq msg "Search becomes case-sensitive")))
3531 ((or (eq arg 2)
3532 (and (null arg)
3533 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3534 (if viper-re-search
3535 "regexp-search" "vanilla-search")
3536 (if viper-re-search
3537 "vanilla-search"
3538 "regexp-search")))))
3539 (setq viper-re-search (null viper-re-search))
3540 (if viper-re-search
3541 (setq msg "Search becomes regexp-style")
3542 (setq msg "Search becomes vanilla-style")))
3544 (setq msg "Search style remains unchanged")))
3545 (princ msg t)))
3547 (defun viper-set-searchstyle-toggling-macros (unset)
3548 "Set the macros for toggling the search style in Viper's vi-state.
3549 The macro that toggles case sensitivity is bound to `//', and the one that
3550 toggles regexp search is bound to `///'.
3551 With a prefix argument, this function unsets the macros. "
3552 (interactive "P")
3553 (or noninteractive
3554 (if (not unset)
3555 (progn
3556 ;; toggle case sensitivity in search
3557 (viper-record-kbd-macro
3558 "//" 'vi-state
3559 [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]
3561 ;; toggle regexp/vanila search
3562 (viper-record-kbd-macro
3563 "///" 'vi-state
3564 [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]
3566 (if (interactive-p)
3567 (message
3568 "// and /// now toggle case-sensitivity and regexp search")))
3569 (viper-unrecord-kbd-macro "//" 'vi-state)
3570 (sit-for 2)
3571 (viper-unrecord-kbd-macro "///" 'vi-state))))
3574 (defun viper-set-parsing-style-toggling-macro (unset)
3575 "Set `%%%' to be a macro that toggles whether comment fields should be parsed for matching parentheses.
3576 This is used in conjunction with the `%' command.
3578 With a prefix argument, unsets the macro."
3579 (interactive "P")
3580 (or noninteractive
3581 (if (not unset)
3582 (progn
3583 ;; Make %%% toggle parsing comments for matching parentheses
3584 (viper-record-kbd-macro
3585 "%%%" 'vi-state
3586 [(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]
3588 (if (interactive-p)
3589 (message
3590 "%%%%%% now toggles whether comments should be parsed for matching parentheses")))
3591 (viper-unrecord-kbd-macro "%%%" 'vi-state))))
3594 (defun viper-set-emacs-state-searchstyle-macros (unset &optional arg-majormode)
3595 "Set the macros for toggling the search style in Viper's emacs-state.
3596 The macro that toggles case sensitivity is bound to `//', and the one that
3597 toggles regexp search is bound to `///'.
3598 With a prefix argument, this function unsets the macros.
3599 If the optional prefix argument is non-nil and specifies a valid major mode,
3600 this sets the macros only in the macros in that major mode. Otherwise,
3601 the macros are set in the current major mode.
3602 \(When unsetting the macros, the second argument has no effect.\)"
3603 (interactive "P")
3604 (or noninteractive
3605 (if (not unset)
3606 (progn
3607 ;; toggle case sensitivity in search
3608 (viper-record-kbd-macro
3609 "//" 'emacs-state
3610 [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]
3611 (or arg-majormode major-mode))
3612 ;; toggle regexp/vanila search
3613 (viper-record-kbd-macro
3614 "///" 'emacs-state
3615 [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]
3616 (or arg-majormode major-mode))
3617 (if (interactive-p)
3618 (message
3619 "// and /// now toggle case-sensitivity and regexp search.")))
3620 (viper-unrecord-kbd-macro "//" 'emacs-state)
3621 (sit-for 2)
3622 (viper-unrecord-kbd-macro "///" 'emacs-state))))
3625 (defun viper-search-forward (arg)
3626 "Search a string forward.
3627 ARG is used to find the ARG's occurrence of the string.
3628 Null string will repeat previous search."
3629 (interactive "P")
3630 (let ((val (viper-P-val arg))
3631 (com (viper-getcom arg))
3632 (old-str viper-s-string))
3633 (setq viper-s-forward t)
3634 (viper-if-string "/")
3635 ;; this is not used at present, but may be used later
3636 (if (or (not (equal old-str viper-s-string))
3637 (not (markerp viper-local-search-start-marker))
3638 (not (marker-buffer viper-local-search-start-marker)))
3639 (setq viper-local-search-start-marker (point-marker)))
3640 (viper-search viper-s-string t val)
3641 (if com
3642 (progn
3643 (viper-move-marker-locally 'viper-com-point (mark t))
3644 (viper-execute-com 'viper-search-next val com)))))
3646 (defun viper-search-backward (arg)
3647 "Search a string backward.
3648 ARG is used to find the ARG's occurrence of the string.
3649 Null string will repeat previous search."
3650 (interactive "P")
3651 (let ((val (viper-P-val arg))
3652 (com (viper-getcom arg))
3653 (old-str viper-s-string))
3654 (setq viper-s-forward nil)
3655 (viper-if-string "?")
3656 ;; this is not used at present, but may be used later
3657 (if (or (not (equal old-str viper-s-string))
3658 (not (markerp viper-local-search-start-marker))
3659 (not (marker-buffer viper-local-search-start-marker)))
3660 (setq viper-local-search-start-marker (point-marker)))
3661 (viper-search viper-s-string nil val)
3662 (if com
3663 (progn
3664 (viper-move-marker-locally 'viper-com-point (mark t))
3665 (viper-execute-com 'viper-search-next val com)))))
3668 ;; Search for COUNT's occurrence of STRING.
3669 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3670 ;; INIT-POINT is the position where search is to start.
3671 ;; Arguments:
3672 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3673 (defun viper-search (string forward arg
3674 &optional no-offset init-point fail-if-not-found)
3675 (if (not (equal string ""))
3676 (let ((val (viper-p-val arg))
3677 (com (viper-getcom arg))
3678 (offset (not no-offset))
3679 (case-fold-search viper-case-fold-search)
3680 (start-point (or init-point (point))))
3681 (viper-deactivate-mark)
3682 (if forward
3683 (condition-case nil
3684 (progn
3685 (if offset (viper-forward-char-carefully))
3686 (if viper-re-search
3687 (progn
3688 (re-search-forward string nil nil val)
3689 (re-search-backward string))
3690 (search-forward string nil nil val)
3691 (search-backward string))
3692 (if (not (equal start-point (point)))
3693 (push-mark start-point t)))
3694 (search-failed
3695 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3696 (progn
3697 (message "Search wrapped around BOTTOM of buffer")
3698 (goto-char (point-min))
3699 (viper-search string forward (cons 1 com) t start-point 'fail)
3700 ;; don't wait in macros
3701 (or executing-kbd-macro
3702 (memq viper-intermediate-command
3703 '(viper-repeat
3704 viper-digit-argument
3705 viper-command-argument))
3706 (sit-for 2))
3707 ;; delete the wrap-around message
3708 (message "")
3710 (goto-char start-point)
3711 (error "`%s': %s not found"
3712 string
3713 (if viper-re-search "Pattern" "String"))
3715 ;; backward
3716 (condition-case nil
3717 (progn
3718 (if viper-re-search
3719 (re-search-backward string nil nil val)
3720 (search-backward string nil nil val))
3721 (if (not (equal start-point (point)))
3722 (push-mark start-point t)))
3723 (search-failed
3724 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3725 (progn
3726 (message "Search wrapped around TOP of buffer")
3727 (goto-char (point-max))
3728 (viper-search string forward (cons 1 com) t start-point 'fail)
3729 ;; don't wait in macros
3730 (or executing-kbd-macro
3731 (memq viper-intermediate-command
3732 '(viper-repeat
3733 viper-digit-argument
3734 viper-command-argument))
3735 (sit-for 2))
3736 ;; delete the wrap-around message
3737 (message "")
3739 (goto-char start-point)
3740 (error "`%s': %s not found"
3741 string
3742 (if viper-re-search "Pattern" "String"))
3743 ))))
3744 ;; pull up or down if at top/bottom of window
3745 (viper-adjust-window)
3746 ;; highlight the result of search
3747 ;; don't wait and don't highlight in macros
3748 (or executing-kbd-macro
3749 (memq viper-intermediate-command
3750 '(viper-repeat viper-digit-argument viper-command-argument))
3751 (viper-flash-search-pattern))
3754 (defun viper-search-next (arg)
3755 "Repeat previous search."
3756 (interactive "P")
3757 (let ((val (viper-p-val arg))
3758 (com (viper-getcom arg)))
3759 (if (null viper-s-string) (error viper-NoPrevSearch))
3760 (viper-search viper-s-string viper-s-forward arg)
3761 (if com
3762 (progn
3763 (viper-move-marker-locally 'viper-com-point (mark t))
3764 (viper-execute-com 'viper-search-next val com)))))
3766 (defun viper-search-Next (arg)
3767 "Repeat previous search in the reverse direction."
3768 (interactive "P")
3769 (let ((val (viper-p-val arg))
3770 (com (viper-getcom arg)))
3771 (if (null viper-s-string) (error viper-NoPrevSearch))
3772 (viper-search viper-s-string (not viper-s-forward) arg)
3773 (if com
3774 (progn
3775 (viper-move-marker-locally 'viper-com-point (mark t))
3776 (viper-execute-com 'viper-search-Next val com)))))
3779 ;; Search contents of buffer defined by one of Viper's motion commands.
3780 ;; Repeatable via `n' and `N'.
3781 (defun viper-buffer-search-enable (&optional c)
3782 (cond (c (setq viper-buffer-search-char c))
3783 ((null viper-buffer-search-char)
3784 (setq viper-buffer-search-char ?g)))
3785 (define-key viper-vi-basic-map
3786 (cond ((viper-characterp viper-buffer-search-char)
3787 (char-to-string viper-buffer-search-char))
3788 (t (error "viper-buffer-search-char: wrong value type, %s"
3789 viper-buffer-search-char)))
3790 'viper-command-argument)
3791 (aset viper-exec-array viper-buffer-search-char 'viper-exec-buffer-search)
3792 (setq viper-prefix-commands
3793 (cons viper-buffer-search-char viper-prefix-commands)))
3795 ;; This is a Viper wraper for isearch-forward.
3796 (defun viper-isearch-forward (arg)
3797 "Do incremental search forward."
3798 (interactive "P")
3799 ;; emacs bug workaround
3800 (if (listp arg) (setq arg (car arg)))
3801 (viper-exec-form-in-emacs (list 'isearch-forward arg)))
3803 ;; This is a Viper wraper for isearch-backward."
3804 (defun viper-isearch-backward (arg)
3805 "Do incremental search backward."
3806 (interactive "P")
3807 ;; emacs bug workaround
3808 (if (listp arg) (setq arg (car arg)))
3809 (viper-exec-form-in-emacs (list 'isearch-backward arg)))
3812 ;; visiting and killing files, buffers
3814 (defun viper-switch-to-buffer ()
3815 "Switch to buffer in the current window."
3816 (interactive)
3817 (let ((other-buffer (other-buffer (current-buffer)))
3818 buffer)
3819 (setq buffer
3820 (funcall viper-read-buffer-function
3821 "Switch to buffer in this window: " other-buffer))
3822 (switch-to-buffer buffer)))
3824 (defun viper-switch-to-buffer-other-window ()
3825 "Switch to buffer in another window."
3826 (interactive)
3827 (let ((other-buffer (other-buffer (current-buffer)))
3828 buffer)
3829 (setq buffer
3830 (funcall viper-read-buffer-function
3831 "Switch to buffer in another window: " other-buffer))
3832 (switch-to-buffer-other-window buffer)))
3834 (defun viper-kill-buffer ()
3835 "Kill a buffer."
3836 (interactive)
3837 (let (buffer buffer-name)
3838 (setq buffer-name
3839 (funcall viper-read-buffer-function
3840 (format "Kill buffer \(%s\): "
3841 (buffer-name (current-buffer)))))
3842 (setq buffer
3843 (if (null buffer-name)
3844 (current-buffer)
3845 (get-buffer buffer-name)))
3846 (if (null buffer) (error "`%s': No such buffer" buffer-name))
3847 (if (or (not (buffer-modified-p buffer))
3848 (y-or-n-p
3849 (format
3850 "Buffer `%s' is modified, are you sure you want to kill it? "
3851 buffer-name)))
3852 (kill-buffer buffer)
3853 (error "Buffer not killed"))))
3857 ;; yank and pop
3859 (defsubst viper-yank (text)
3860 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
3861 (insert text)
3862 (setq this-command 'yank))
3864 (defun viper-put-back (arg)
3865 "Put back after point/below line."
3866 (interactive "P")
3867 (let ((val (viper-p-val arg))
3868 (text (if viper-use-register
3869 (cond ((viper-valid-register viper-use-register '(digit))
3870 (current-kill
3871 (- viper-use-register ?1) 'do-not-rotate))
3872 ((viper-valid-register viper-use-register)
3873 (get-register (downcase viper-use-register)))
3874 (t (error viper-InvalidRegister viper-use-register)))
3875 (current-kill 0)))
3876 sv-point chars-inserted lines-inserted)
3877 (if (null text)
3878 (if viper-use-register
3879 (let ((reg viper-use-register))
3880 (setq viper-use-register nil)
3881 (error viper-EmptyRegister reg))
3882 (error "")))
3883 (setq viper-use-register nil)
3884 (if (viper-end-with-a-newline-p text)
3885 (progn
3886 (end-of-line)
3887 (if (eobp)
3888 (insert "\n")
3889 (forward-line 1))
3890 (beginning-of-line))
3891 (if (not (eolp)) (viper-forward-char-carefully)))
3892 (set-marker (viper-mark-marker) (point) (current-buffer))
3893 (viper-set-destructive-command
3894 (list 'viper-put-back val nil viper-use-register nil nil))
3895 (setq sv-point (point))
3896 (viper-loop val (viper-yank text))
3897 (setq chars-inserted (abs (- (point) sv-point))
3898 lines-inserted (abs (count-lines (point) sv-point)))
3899 (if (or (> chars-inserted viper-change-notification-threshold)
3900 (> lines-inserted viper-change-notification-threshold))
3901 (message "Inserted %d character(s), %d line(s)"
3902 chars-inserted lines-inserted)))
3903 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3904 ;; newline; it leaves the cursor at the beginning when the text contains
3905 ;; a newline
3906 (if (viper-same-line (point) (mark))
3907 (or (= (point) (mark)) (viper-backward-char-carefully))
3908 (exchange-point-and-mark)
3909 (if (bolp)
3910 (back-to-indentation)))
3911 (viper-deactivate-mark))
3913 (defun viper-Put-back (arg)
3914 "Put back at point/above line."
3915 (interactive "P")
3916 (let ((val (viper-p-val arg))
3917 (text (if viper-use-register
3918 (cond ((viper-valid-register viper-use-register '(digit))
3919 (current-kill
3920 (- viper-use-register ?1) 'do-not-rotate))
3921 ((viper-valid-register viper-use-register)
3922 (get-register (downcase viper-use-register)))
3923 (t (error viper-InvalidRegister viper-use-register)))
3924 (current-kill 0)))
3925 sv-point chars-inserted lines-inserted)
3926 (if (null text)
3927 (if viper-use-register
3928 (let ((reg viper-use-register))
3929 (setq viper-use-register nil)
3930 (error viper-EmptyRegister reg))
3931 (error "")))
3932 (setq viper-use-register nil)
3933 (if (viper-end-with-a-newline-p text) (beginning-of-line))
3934 (viper-set-destructive-command
3935 (list 'viper-Put-back val nil viper-use-register nil nil))
3936 (set-marker (viper-mark-marker) (point) (current-buffer))
3937 (setq sv-point (point))
3938 (viper-loop val (viper-yank text))
3939 (setq chars-inserted (abs (- (point) sv-point))
3940 lines-inserted (abs (count-lines (point) sv-point)))
3941 (if (or (> chars-inserted viper-change-notification-threshold)
3942 (> lines-inserted viper-change-notification-threshold))
3943 (message "Inserted %d character(s), %d line(s)"
3944 chars-inserted lines-inserted)))
3945 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3946 ;; newline; it leaves the cursor at the beginning when the text contains
3947 ;; a newline
3948 (if (viper-same-line (point) (mark))
3949 (or (= (point) (mark)) (viper-backward-char-carefully))
3950 (exchange-point-and-mark)
3951 (if (bolp)
3952 (back-to-indentation)))
3953 (viper-deactivate-mark))
3956 ;; Copy region to kill-ring.
3957 ;; If BEG and END do not belong to the same buffer, copy empty region.
3958 (defun viper-copy-region-as-kill (beg end)
3959 (condition-case nil
3960 (copy-region-as-kill beg end)
3961 (error (copy-region-as-kill beg beg))))
3964 (defun viper-delete-char (arg)
3965 "Delete next character."
3966 (interactive "P")
3967 (let ((val (viper-p-val arg))
3968 end-del-pos)
3969 (viper-set-destructive-command
3970 (list 'viper-delete-char val nil nil nil nil))
3971 (if (and viper-ex-style-editing
3972 (> val (viper-chars-in-region (point) (viper-line-pos 'end))))
3973 (setq val (viper-chars-in-region (point) (viper-line-pos 'end))))
3974 (if (and viper-ex-style-motion (eolp))
3975 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
3976 (save-excursion
3977 (viper-forward-char-carefully val)
3978 (setq end-del-pos (point)))
3979 (if viper-use-register
3980 (progn
3981 (cond ((viper-valid-register viper-use-register '((Letter)))
3982 (viper-append-to-register
3983 (downcase viper-use-register) (point) end-del-pos))
3984 ((viper-valid-register viper-use-register)
3985 (copy-to-register
3986 viper-use-register (point) end-del-pos nil))
3987 (t (error viper-InvalidRegister viper-use-register)))
3988 (setq viper-use-register nil)))
3990 (delete-char val t)
3991 (if viper-ex-style-motion
3992 (if (and (eolp) (not (bolp))) (backward-char 1)))
3995 (defun viper-delete-backward-char (arg)
3996 "Delete previous character. On reaching beginning of line, stop and beep."
3997 (interactive "P")
3998 (let ((val (viper-p-val arg))
3999 end-del-pos)
4000 (viper-set-destructive-command
4001 (list 'viper-delete-backward-char val nil nil nil nil))
4002 (if (and
4003 viper-ex-style-editing
4004 (> val (viper-chars-in-region (viper-line-pos 'start) (point))))
4005 (setq val (viper-chars-in-region (viper-line-pos 'start) (point))))
4006 (save-excursion
4007 (viper-backward-char-carefully val)
4008 (setq end-del-pos (point)))
4009 (if viper-use-register
4010 (progn
4011 (cond ((viper-valid-register viper-use-register '(Letter))
4012 (viper-append-to-register
4013 (downcase viper-use-register) end-del-pos (point)))
4014 ((viper-valid-register viper-use-register)
4015 (copy-to-register
4016 viper-use-register end-del-pos (point) nil))
4017 (t (error viper-InvalidRegister viper-use-register)))
4018 (setq viper-use-register nil)))
4019 (if (and (bolp) viper-ex-style-editing)
4020 (ding))
4021 (delete-backward-char val t)))
4024 (defun viper-del-backward-char-in-insert ()
4025 "Delete 1 char backwards while in insert mode."
4026 (interactive)
4027 (if (and viper-ex-style-editing (bolp))
4028 (beep 1)
4029 (delete-backward-char 1 t)))
4032 (defun viper-del-backward-char-in-replace ()
4033 "Delete one character in replace mode.
4034 If `viper-delete-backwards-in-replace' is t, then DEL key actually deletes
4035 charecters. If it is nil, then the cursor just moves backwards, similarly
4036 to Vi. The variable `viper-ex-style-editing', if t, doesn't let the
4037 cursor move past the beginning of line."
4038 (interactive)
4039 (cond (viper-delete-backwards-in-replace
4040 (cond ((not (bolp))
4041 (delete-backward-char 1 t))
4042 (viper-ex-style-editing
4043 (beep 1))
4044 ((bobp)
4045 (beep 1))
4047 (delete-backward-char 1 t))))
4048 (viper-ex-style-editing
4049 (if (bolp)
4050 (beep 1)
4051 (backward-char 1)))
4053 (backward-char 1))))
4057 ;; join lines.
4059 (defun viper-join-lines (arg)
4060 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4061 (interactive "*P")
4062 (let ((val (viper-P-val arg)))
4063 (viper-set-destructive-command
4064 (list 'viper-join-lines val nil nil nil nil))
4065 (viper-loop (if (null val) 1 (1- val))
4066 (end-of-line)
4067 (if (not (eobp))
4068 (progn
4069 (forward-line 1)
4070 (delete-region (point) (1- (point)))
4071 (fixup-whitespace)
4072 ;; fixup-whitespace sometimes does not leave space
4073 ;; between objects, so we insert it as in Vi
4074 (or (looking-at " ")
4075 (insert " ")
4076 (backward-char 1))
4077 )))))
4080 ;; Replace state
4082 (defun viper-change (beg end)
4083 (if (markerp beg) (setq beg (marker-position beg)))
4084 (if (markerp end) (setq end (marker-position end)))
4085 ;; beg is sometimes (mark t), which may be nil
4086 (or beg (setq beg end))
4088 (viper-set-complex-command-for-undo)
4089 (if viper-use-register
4090 (progn
4091 (copy-to-register viper-use-register beg end nil)
4092 (setq viper-use-register nil)))
4093 (viper-set-replace-overlay beg end)
4094 (setq last-command nil) ; separate repl text from prev kills
4096 (if (= (viper-replace-start) (point-max))
4097 (error "End of buffer"))
4099 (setq viper-last-replace-region
4100 (buffer-substring (viper-replace-start)
4101 (viper-replace-end)))
4103 ;; protect against error while inserting "@" and other disasters
4104 ;; (e.g., read-only buff)
4105 (condition-case conds
4106 (if (or viper-allow-multiline-replace-regions
4107 (viper-same-line (viper-replace-start)
4108 (viper-replace-end)))
4109 (progn
4110 ;; tabs cause problems in replace, so untabify
4111 (goto-char (viper-replace-end))
4112 (insert-before-markers "@") ; put placeholder after the TAB
4113 (untabify (viper-replace-start) (point))
4114 ;; del @, don't put on kill ring
4115 (delete-backward-char 1)
4117 (viper-set-replace-overlay-glyphs
4118 viper-replace-region-start-delimiter
4119 viper-replace-region-end-delimiter)
4120 ;; this move takes care of the last posn in the overlay, which
4121 ;; has to be shifted because of insert. We can't simply insert
4122 ;; "$" before-markers because then overlay-start will shift the
4123 ;; beginning of the overlay in case we are replacing a single
4124 ;; character. This fixes the bug with `s' and `cl' commands.
4125 (viper-move-replace-overlay (viper-replace-start) (point))
4126 (goto-char (viper-replace-start))
4127 (viper-change-state-to-replace t))
4128 (kill-region (viper-replace-start)
4129 (viper-replace-end))
4130 (viper-hide-replace-overlay)
4131 (viper-change-state-to-insert))
4132 (error ;; make sure that the overlay doesn't stay.
4133 ;; go back to the original point
4134 (goto-char (viper-replace-start))
4135 (viper-hide-replace-overlay)
4136 (viper-message-conditions conds))))
4139 (defun viper-change-subr (beg end)
4140 ;; beg is sometimes (mark t), which may be nil
4141 (or beg (setq beg end))
4142 (if viper-use-register
4143 (progn
4144 (copy-to-register viper-use-register beg end nil)
4145 (setq viper-use-register nil)))
4146 (kill-region beg end)
4147 (setq this-command 'viper-change)
4148 (viper-yank-last-insertion))
4150 (defun viper-toggle-case (arg)
4151 "Toggle character case."
4152 (interactive "P")
4153 (let ((val (viper-p-val arg)) (c))
4154 (viper-set-destructive-command
4155 (list 'viper-toggle-case val nil nil nil nil))
4156 (while (> val 0)
4157 (setq c (following-char))
4158 (delete-char 1 nil)
4159 (if (eq c (upcase c))
4160 (insert-char (downcase c) 1)
4161 (insert-char (upcase c) 1))
4162 (if (eolp) (backward-char 1))
4163 (setq val (1- val)))))
4166 ;; query replace
4168 (defun viper-query-replace ()
4169 "Query replace.
4170 If a null string is suplied as the string to be replaced,
4171 the query replace mode will toggle between string replace
4172 and regexp replace."
4173 (interactive)
4174 (let (str)
4175 (setq str (viper-read-string-with-history
4176 (if viper-re-query-replace "Query replace regexp: "
4177 "Query replace: ")
4178 nil ; no initial
4179 'viper-replace1-history
4180 (car viper-replace1-history) ; default
4182 (if (string= str "")
4183 (progn
4184 (setq viper-re-query-replace (not viper-re-query-replace))
4185 (message "Query replace mode changed to %s"
4186 (if viper-re-query-replace "regexp replace"
4187 "string replace")))
4188 (if viper-re-query-replace
4189 (query-replace-regexp
4191 (viper-read-string-with-history
4192 (format "Query replace regexp `%s' with: " str)
4193 nil ; no initial
4194 'viper-replace1-history
4195 (car viper-replace1-history) ; default
4197 (query-replace
4199 (viper-read-string-with-history
4200 (format "Query replace `%s' with: " str)
4201 nil ; no initial
4202 'viper-replace1-history
4203 (car viper-replace1-history) ; default
4204 ))))))
4207 ;; marking
4209 (defun viper-mark-beginning-of-buffer ()
4210 "Mark beginning of buffer."
4211 (interactive)
4212 (push-mark (point))
4213 (goto-char (point-min))
4214 (exchange-point-and-mark)
4215 (message "Mark set at the beginning of buffer"))
4217 (defun viper-mark-end-of-buffer ()
4218 "Mark end of buffer."
4219 (interactive)
4220 (push-mark (point))
4221 (goto-char (point-max))
4222 (exchange-point-and-mark)
4223 (message "Mark set at the end of buffer"))
4225 (defun viper-mark-point ()
4226 "Set mark at point of buffer."
4227 (interactive)
4228 (let ((char (read-char)))
4229 (cond ((and (<= ?a char) (<= char ?z))
4230 (point-to-register (1+ (- char ?a))))
4231 ((viper= char ?<) (viper-mark-beginning-of-buffer))
4232 ((viper= char ?>) (viper-mark-end-of-buffer))
4233 ((viper= char ?.) (viper-set-mark-if-necessary))
4234 ((viper= char ?,) (viper-cycle-through-mark-ring))
4235 ((viper= char ?^) (push-mark viper-saved-mark t t))
4236 ((viper= char ?D) (mark-defun))
4237 (t (error ""))
4240 ;; Algorithm: If first invocation of this command save mark on ring, goto
4241 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4242 ;; making it into the new mark, M1.
4243 ;; Push this mark back and set mark to the original point position, p1.
4244 ;; So, if you hit '' or `` then you can return to p1.
4246 ;; If repeated command, pop top elt from the ring into mark and
4247 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4248 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4249 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4250 ;; etc.
4251 (defun viper-cycle-through-mark-ring ()
4252 "Visit previous locations on the mark ring.
4253 One can use `` and '' to temporarily jump 1 step back."
4254 (let* ((sv-pt (point)))
4255 ;; if repeated `m,' command, pop the previously saved mark.
4256 ;; Prev saved mark is actually prev saved point. It is used if the
4257 ;; user types `` or '' and is discarded
4258 ;; from the mark ring by the next `m,' command.
4259 ;; In any case, go to the previous or previously saved mark.
4260 ;; Then push the current mark (popped off the ring) and set current
4261 ;; point to be the mark. Current pt as mark is discarded by the next
4262 ;; m, command.
4263 (if (eq last-command 'viper-cycle-through-mark-ring)
4265 ;; save current mark if the first iteration
4266 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4267 (if (mark t)
4268 (push-mark (mark t) t)) )
4269 (pop-mark)
4270 (set-mark-command 1)
4271 ;; don't duplicate mark on the ring
4272 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4273 (push-mark sv-pt t)
4274 (viper-deactivate-mark)
4275 (setq this-command 'viper-cycle-through-mark-ring)
4279 (defun viper-goto-mark (arg)
4280 "Go to mark."
4281 (interactive "P")
4282 (let ((char (read-char))
4283 (com (viper-getcom arg)))
4284 (viper-goto-mark-subr char com nil)))
4286 (defun viper-goto-mark-and-skip-white (arg)
4287 "Go to mark and skip to first non-white character on line."
4288 (interactive "P")
4289 (let ((char (read-char))
4290 (com (viper-getCom arg)))
4291 (viper-goto-mark-subr char com t)))
4293 (defun viper-goto-mark-subr (char com skip-white)
4294 (if (eobp)
4295 (if (bobp)
4296 (error "Empty buffer")
4297 (backward-char 1)))
4298 (cond ((viper-valid-register char '(letter))
4299 (let* ((buff (current-buffer))
4300 (reg (1+ (- char ?a)))
4301 (text-marker (get-register reg)))
4302 ;; If marker points to file that had markers set (and those markers
4303 ;; were saved (as e.g., in session.el), then restore those markers
4304 (if (and (consp text-marker)
4305 (eq (car text-marker) 'file-query)
4306 (or (find-buffer-visiting (nth 1 text-marker))
4307 (y-or-n-p (format "Visit file %s again? "
4308 (nth 1 text-marker)))))
4309 (save-excursion
4310 (find-file (nth 1 text-marker))
4311 (when (and (<= (nth 2 text-marker) (point-max))
4312 (<= (point-min) (nth 2 text-marker)))
4313 (setq text-marker (copy-marker (nth 2 text-marker)))
4314 (set-register reg text-marker))))
4315 (if com (viper-move-marker-locally 'viper-com-point (point)))
4316 (if (not (viper-valid-marker text-marker))
4317 (error viper-EmptyTextmarker char))
4318 (if (and (viper-same-line (point) viper-last-jump)
4319 (= (point) viper-last-jump-ignore))
4320 (push-mark viper-last-jump t)
4321 (push-mark nil t)) ; no msg
4322 (viper-register-to-point reg)
4323 (setq viper-last-jump (point-marker))
4324 (cond (skip-white
4325 (back-to-indentation)
4326 (setq viper-last-jump-ignore (point))))
4327 (if com
4328 (if (equal buff (current-buffer))
4329 (viper-execute-com (if skip-white
4330 'viper-goto-mark-and-skip-white
4331 'viper-goto-mark)
4332 nil com)
4333 (switch-to-buffer buff)
4334 (goto-char viper-com-point)
4335 (viper-change-state-to-vi)
4336 (error "")))))
4337 ((and (not skip-white) (viper= char ?`))
4338 (if com (viper-move-marker-locally 'viper-com-point (point)))
4339 (if (and (viper-same-line (point) viper-last-jump)
4340 (= (point) viper-last-jump-ignore))
4341 (goto-char viper-last-jump))
4342 (if (null (mark t)) (error "Mark is not set in this buffer"))
4343 (if (= (point) (mark t)) (pop-mark))
4344 (exchange-point-and-mark)
4345 (setq viper-last-jump (point-marker)
4346 viper-last-jump-ignore 0)
4347 (if com (viper-execute-com 'viper-goto-mark nil com)))
4348 ((and skip-white (viper= char ?'))
4349 (if com (viper-move-marker-locally 'viper-com-point (point)))
4350 (if (and (viper-same-line (point) viper-last-jump)
4351 (= (point) viper-last-jump-ignore))
4352 (goto-char viper-last-jump))
4353 (if (= (point) (mark t)) (pop-mark))
4354 (exchange-point-and-mark)
4355 (setq viper-last-jump (point))
4356 (back-to-indentation)
4357 (setq viper-last-jump-ignore (point))
4358 (if com (viper-execute-com 'viper-goto-mark-and-skip-white nil com)))
4359 (t (error viper-InvalidTextmarker char))))
4361 (defun viper-insert-tab ()
4362 (interactive)
4363 (insert-tab))
4365 (defun viper-exchange-point-and-mark ()
4366 (interactive)
4367 (exchange-point-and-mark)
4368 (back-to-indentation))
4370 ;; Input Mode Indentation
4372 ;; Returns t, if the string before point matches the regexp STR.
4373 (defsubst viper-looking-back (str)
4374 (and (save-excursion (re-search-backward str nil t))
4375 (= (point) (match-end 0))))
4378 (defun viper-forward-indent ()
4379 "Indent forward -- `C-t' in Vi."
4380 (interactive)
4381 (setq viper-cted t)
4382 (indent-to (+ (current-column) viper-shift-width)))
4384 (defun viper-backward-indent ()
4385 "Backtab, C-d in VI"
4386 (interactive)
4387 (if viper-cted
4388 (let ((p (point)) (c (current-column)) bol (indent t))
4389 (if (viper-looking-back "[0^]")
4390 (progn
4391 (if (eq ?^ (preceding-char))
4392 (setq viper-preserve-indent t))
4393 (delete-backward-char 1)
4394 (setq p (point))
4395 (setq indent nil)))
4396 (save-excursion
4397 (beginning-of-line)
4398 (setq bol (point)))
4399 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4400 (delete-region (point) p)
4401 (if indent
4402 (indent-to (- c viper-shift-width)))
4403 (if (or (bolp) (viper-looking-back "[^ \t]"))
4404 (setq viper-cted nil)))))
4406 (defun viper-autoindent ()
4407 "Auto Indentation, Vi-style."
4408 (interactive)
4409 (let ((col (current-indentation)))
4410 (if abbrev-mode (expand-abbrev))
4411 (if viper-preserve-indent
4412 (setq viper-preserve-indent nil)
4413 (setq viper-current-indent col))
4414 ;; don't leave whitespace lines around
4415 (if (memq last-command
4416 '(viper-autoindent
4417 viper-open-line viper-Open-line
4418 viper-replace-state-exit-cmd))
4419 (indent-to-left-margin))
4420 ;; use \n instead of newline, or else <Return> will move the insert point
4421 ;;(newline 1)
4422 (insert "\n")
4423 (if viper-auto-indent
4424 (progn
4425 (setq viper-cted t)
4426 (if (and viper-electric-mode
4427 (not
4428 (memq major-mode '(fundamental-mode
4429 text-mode
4430 paragraph-indent-text-mode ))))
4431 (indent-according-to-mode)
4432 (indent-to viper-current-indent))
4437 ;; Viewing registers
4439 (defun viper-ket-function (arg)
4440 "Function called by \], the ket. View registers and call \]\]."
4441 (interactive "P")
4442 (let ((reg (read-char)))
4443 (cond ((viper-valid-register reg '(letter Letter))
4444 (view-register (downcase reg)))
4445 ((viper-valid-register reg '(digit))
4446 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4447 (with-output-to-temp-buffer " *viper-info*"
4448 (princ (format "Register %c contains the string:\n" reg))
4449 (princ text))
4451 ((viper= ?\] reg)
4452 (viper-next-heading arg))
4453 (t (error
4454 viper-InvalidRegister reg)))))
4456 (defun viper-brac-function (arg)
4457 "Function called by \[, the brac. View textmarkers and call \[\["
4458 (interactive "P")
4459 (let ((reg (read-char)))
4460 (cond ((viper= ?\[ reg)
4461 (viper-prev-heading arg))
4462 ((viper= ?\] reg)
4463 (viper-heading-end arg))
4464 ((viper-valid-register reg '(letter))
4465 (let* ((val (get-register (1+ (- reg ?a))))
4466 (buf (if (not (markerp val))
4467 (error viper-EmptyTextmarker reg)
4468 (marker-buffer val)))
4469 (pos (marker-position val))
4470 line-no text (s pos) (e pos))
4471 (with-output-to-temp-buffer " *viper-info*"
4472 (if (and buf pos)
4473 (progn
4474 (save-excursion
4475 (set-buffer buf)
4476 (setq line-no (1+ (count-lines (point-min) val)))
4477 (goto-char pos)
4478 (beginning-of-line)
4479 (if (re-search-backward "[^ \t]" nil t)
4480 (progn
4481 (beginning-of-line)
4482 (setq s (point))))
4483 (goto-char pos)
4484 (forward-line 1)
4485 (if (re-search-forward "[^ \t]" nil t)
4486 (progn
4487 (end-of-line)
4488 (setq e (point))))
4489 (setq text (buffer-substring s e))
4490 (setq text (format "%s<%c>%s"
4491 (substring text 0 (- pos s))
4492 reg (substring text (- pos s)))))
4493 (princ
4494 (format
4495 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4496 reg (buffer-name buf) line-no))
4497 (princ (format "Here is some text around %c:\n\n %s"
4498 reg text)))
4499 (princ (format viper-EmptyTextmarker reg))))
4501 (t (error viper-InvalidTextmarker reg)))))
4505 ;; commands in insertion mode
4507 (defun viper-delete-backward-word (arg)
4508 "Delete previous word."
4509 (interactive "p")
4510 (save-excursion
4511 (push-mark nil t)
4512 (backward-word arg)
4513 (delete-region (point) (mark t))
4514 (pop-mark)))
4517 (defun viper-set-expert-level (&optional dont-change-unless)
4518 "Sets the expert level for a Viper user.
4519 Can be called interactively to change (temporarily or permanently) the
4520 current expert level.
4522 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4523 the level should not be changed, unless its current value is
4524 meaningless (i.e., not one of 1,2,3,4,5).
4526 User level determines the setting of Viper variables that are most
4527 sensitive for VI-style look-and-feel."
4529 (interactive)
4531 (if (not (natnump viper-expert-level)) (setq viper-expert-level 0))
4533 (save-window-excursion
4534 (delete-other-windows)
4535 ;; if 0 < viper-expert-level < viper-max-expert-level
4536 ;; & dont-change-unless = t -- use it; else ask
4537 (viper-ask-level dont-change-unless))
4539 (setq viper-always t
4540 viper-ex-style-motion t
4541 viper-ex-style-editing t
4542 viper-want-ctl-h-help nil)
4544 (cond ((eq viper-expert-level 1) ; novice or beginner
4545 (global-set-key ; in emacs-state
4546 viper-toggle-key
4547 (if (viper-window-display-p) 'viper-iconify 'suspend-emacs))
4548 (setq viper-no-multiple-ESC t
4549 viper-re-search t
4550 viper-vi-style-in-minibuffer t
4551 viper-search-wrap-around-t t
4552 viper-electric-mode nil
4553 viper-want-emacs-keys-in-vi nil
4554 viper-want-emacs-keys-in-insert nil))
4556 ((and (> viper-expert-level 1) (< viper-expert-level 5))
4557 ;; intermediate to guru
4558 (setq viper-no-multiple-ESC (if (viper-window-display-p)
4559 t 'twice)
4560 viper-electric-mode t
4561 viper-want-emacs-keys-in-vi t
4562 viper-want-emacs-keys-in-insert (> viper-expert-level 2))
4564 (if (eq viper-expert-level 4) ; respect user's ex-style motion
4565 ; and viper-no-multiple-ESC
4566 (progn
4567 (setq-default
4568 viper-ex-style-editing
4569 (viper-standard-value 'viper-ex-style-editing)
4570 viper-ex-style-motion
4571 (viper-standard-value 'viper-ex-style-motion))
4572 (setq viper-ex-style-motion
4573 (viper-standard-value 'viper-ex-style-motion)
4574 viper-ex-style-editing
4575 (viper-standard-value 'viper-ex-style-editing)
4576 viper-re-search
4577 (viper-standard-value 'viper-re-search)
4578 viper-no-multiple-ESC
4579 (viper-standard-value 'viper-no-multiple-ESC)))))
4581 ;; A wizard!!
4582 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4583 ;; user toggle the values of variables.
4584 (t (setq-default viper-ex-style-editing
4585 (viper-standard-value 'viper-ex-style-editing)
4586 viper-ex-style-motion
4587 (viper-standard-value 'viper-ex-style-motion))
4588 (setq viper-want-ctl-h-help
4589 (viper-standard-value 'viper-want-ctl-h-help)
4590 viper-always
4591 (viper-standard-value 'viper-always)
4592 viper-no-multiple-ESC
4593 (viper-standard-value 'viper-no-multiple-ESC)
4594 viper-ex-style-motion
4595 (viper-standard-value 'viper-ex-style-motion)
4596 viper-ex-style-editing
4597 (viper-standard-value 'viper-ex-style-editing)
4598 viper-re-search
4599 (viper-standard-value 'viper-re-search)
4600 viper-electric-mode
4601 (viper-standard-value 'viper-electric-mode)
4602 viper-want-emacs-keys-in-vi
4603 (viper-standard-value 'viper-want-emacs-keys-in-vi)
4604 viper-want-emacs-keys-in-insert
4605 (viper-standard-value 'viper-want-emacs-keys-in-insert))))
4607 (viper-set-mode-vars-for viper-current-state)
4608 (if (or viper-always
4609 (and (> viper-expert-level 0) (> 5 viper-expert-level)))
4610 (viper-set-hooks)))
4613 ;; Ask user expert level.
4614 (defun viper-ask-level (dont-change-unless)
4615 (let ((ask-buffer " *viper-ask-level*")
4616 level-changed repeated)
4617 (save-window-excursion
4618 (switch-to-buffer ask-buffer)
4620 (while (or (> viper-expert-level viper-max-expert-level)
4621 (< viper-expert-level 1)
4622 (null dont-change-unless))
4623 (erase-buffer)
4624 (if repeated
4625 (progn
4626 (message "Invalid user level")
4627 (beep 1))
4628 (setq repeated t))
4629 (setq dont-change-unless t
4630 level-changed t)
4631 (insert "
4632 Please specify your level of familiarity with the venomous VI PERil
4633 (and the VI Plan for Emacs Rescue).
4634 You can change it at any time by typing `M-x viper-set-expert-level RET'
4636 1 -- BEGINNER: Almost all Emacs features are suppressed.
4637 Feels almost like straight Vi. File name completion and
4638 command history in the minibuffer are thrown in as a bonus.
4639 To use Emacs productively, you must reach level 3 or higher.
4640 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4641 so most Emacs commands can be used when Viper is in Vi state.
4642 Good progress---you are well on the way to level 3!
4643 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
4644 in Viper's insert state.
4645 4 -- GURU: Like 3, but user settings are respected for viper-no-multiple-ESC,
4646 viper-ex-style-motion, viper-ex-style-editing, and
4647 viper-re-search variables. Adjust these settings to your taste.
4648 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4649 viper-electric-mode, viper-want-ctl-h-help, viper-want-emacs-keys-in-vi,
4650 and viper-want-emacs-keys-in-insert. Adjust these to your taste.
4652 Please, specify your level now: ")
4654 (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
4655 ) ; end while
4657 ;; tell the user if level was changed
4658 (and level-changed
4659 (progn
4660 (insert
4661 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4662 viper-expert-level))
4663 (if (y-or-n-p "Do you wish to make this change permanent? ")
4664 ;; save the setting for viper-expert-level
4665 (viper-save-setting
4666 'viper-expert-level
4667 (format "Saving user level %d ..." viper-expert-level)
4668 viper-custom-file-name))
4670 (bury-buffer) ; remove ask-buffer from screen
4671 (message "")
4675 (defun viper-nil ()
4676 (interactive)
4677 (beep 1))
4680 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4681 (defun viper-register-to-point (char &optional enforce-buffer)
4682 "Like jump-to-register, but switches to another buffer in another window."
4683 (interactive "cViper register to point: ")
4684 (let ((val (get-register char)))
4685 (cond
4686 ((and (fboundp 'frame-configuration-p)
4687 (frame-configuration-p val))
4688 (set-frame-configuration val))
4689 ((window-configuration-p val)
4690 (set-window-configuration val))
4691 ((viper-valid-marker val)
4692 (if (and enforce-buffer
4693 (not (equal (current-buffer) (marker-buffer val))))
4694 (error (concat viper-EmptyTextmarker " in this buffer")
4695 (1- (+ char ?a))))
4696 (pop-to-buffer (marker-buffer val))
4697 (goto-char val))
4698 ((and (consp val) (eq (car val) 'file))
4699 (find-file (cdr val)))
4701 (error viper-EmptyTextmarker (1- (+ char ?a)))))))
4704 (defun viper-save-kill-buffer ()
4705 "Save then kill current buffer. "
4706 (interactive)
4707 (if (< viper-expert-level 2)
4708 (save-buffers-kill-emacs)
4709 (save-buffer)
4710 (kill-buffer (current-buffer))))
4714 ;;; Bug Report
4716 (defun viper-submit-report ()
4717 "Submit bug report on Viper."
4718 (interactive)
4719 (let ((reporter-prompt-for-summary-p t)
4720 (viper-device-type (viper-device-type))
4721 color-display-p frame-parameters
4722 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4723 varlist salutation window-config)
4725 ;; If mode info is needed, add variable to `let' and then set it below,
4726 ;; like we did with color-display-p.
4727 (setq color-display-p (if (viper-window-display-p)
4728 (viper-color-display-p)
4729 'non-x)
4730 minibuffer-vi-face (if (viper-has-face-support-p)
4731 (viper-get-face viper-minibuffer-vi-face)
4732 'non-x)
4733 minibuffer-insert-face (if (viper-has-face-support-p)
4734 (viper-get-face
4735 viper-minibuffer-insert-face)
4736 'non-x)
4737 minibuffer-emacs-face (if (viper-has-face-support-p)
4738 (viper-get-face
4739 viper-minibuffer-emacs-face)
4740 'non-x)
4741 frame-parameters (if (fboundp 'frame-parameters)
4742 (frame-parameters (selected-frame))))
4744 (setq varlist (list 'viper-vi-minibuffer-minor-mode
4745 'viper-insert-minibuffer-minor-mode
4746 'viper-vi-intercept-minor-mode
4747 'viper-vi-local-user-minor-mode
4748 'viper-vi-kbd-minor-mode
4749 'viper-vi-global-user-minor-mode
4750 'viper-vi-state-modifier-minor-mode
4751 'viper-vi-diehard-minor-mode
4752 'viper-vi-basic-minor-mode
4753 'viper-replace-minor-mode
4754 'viper-insert-intercept-minor-mode
4755 'viper-insert-local-user-minor-mode
4756 'viper-insert-kbd-minor-mode
4757 'viper-insert-global-user-minor-mode
4758 'viper-insert-state-modifier-minor-mode
4759 'viper-insert-diehard-minor-mode
4760 'viper-insert-basic-minor-mode
4761 'viper-emacs-intercept-minor-mode
4762 'viper-emacs-local-user-minor-mode
4763 'viper-emacs-kbd-minor-mode
4764 'viper-emacs-global-user-minor-mode
4765 'viper-emacs-state-modifier-minor-mode
4766 'viper-automatic-iso-accents
4767 'viper-special-input-method
4768 'viper-want-emacs-keys-in-insert
4769 'viper-want-emacs-keys-in-vi
4770 'viper-keep-point-on-undo
4771 'viper-no-multiple-ESC
4772 'viper-electric-mode
4773 'viper-ESC-key
4774 'viper-want-ctl-h-help
4775 'viper-ex-style-editing
4776 'viper-delete-backwards-in-replace
4777 'viper-vi-style-in-minibuffer
4778 'viper-vi-state-hook
4779 'viper-insert-state-hook
4780 'viper-replace-state-hook
4781 'viper-emacs-state-hook
4782 'ex-cycle-other-window
4783 'ex-cycle-through-non-files
4784 'viper-expert-level
4785 'major-mode
4786 'viper-device-type
4787 'color-display-p
4788 'frame-parameters
4789 'minibuffer-vi-face
4790 'minibuffer-insert-face
4791 'minibuffer-emacs-face
4793 (setq salutation "
4794 Congratulations! You may have unearthed a bug in Viper!
4795 Please mail a concise, accurate summary of the problem to the address above.
4797 -------------------------------------------------------------------")
4798 (setq window-config (current-window-configuration))
4799 (with-output-to-temp-buffer " *viper-info*"
4800 (switch-to-buffer " *viper-info*")
4801 (delete-other-windows)
4802 (princ "
4803 PLEASE FOLLOW THESE PROCEDURES
4804 ------------------------------
4806 Before reporting a bug, please verify that it is related to Viper, and is
4807 not cause by other packages you are using.
4809 Don't report compilation warnings, unless you are certain that there is a
4810 problem. These warnings are normal and unavoidable.
4812 Please note that users should not modify variables and keymaps other than
4813 those advertised in the manual. Such `customization' is likely to crash
4814 Viper, as it would any other improperly customized Emacs package.
4816 If you are reporting an error message received while executing one of the
4817 Viper commands, type:
4819 M-x set-variable <Return> debug-on-error <Return> t <Return>
4821 Then reproduce the error. The above command will cause Emacs to produce a
4822 back trace of the execution that leads to the error. Please include this
4823 trace in your bug report.
4825 If you believe that one of Viper's commands goes into an infinite loop
4826 \(e.g., Emacs freezes\), type:
4828 M-x set-variable <Return> debug-on-quit <Return> t <Return>
4830 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
4831 the current command. Include the resulting back trace in the bug report.
4833 Mail anyway (y or n)? ")
4834 (if (y-or-n-p "Mail anyway? ")
4836 (set-window-configuration window-config)
4837 (error "Bug report aborted")))
4839 (require 'reporter)
4840 (set-window-configuration window-config)
4842 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
4843 (viper-version)
4844 varlist
4845 nil 'delete-other-windows
4846 salutation)
4851 ;; Smoothes out the difference between Emacs' unread-command-events
4852 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
4853 ;; events or a sequence of keys.
4855 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
4856 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
4857 ;; into an event. Below, we delete nil from event lists, since nil is the most
4858 ;; common symbol that might appear in this wrong context.
4859 (defun viper-set-unread-command-events (arg)
4860 (if viper-emacs-p
4861 (setq
4862 unread-command-events
4863 (let ((new-events
4864 (cond ((eventp arg) (list arg))
4865 ((listp arg) arg)
4866 ((sequencep arg)
4867 (listify-key-sequence arg))
4868 (t (error
4869 "viper-set-unread-command-events: Invalid argument, %S"
4870 arg)))))
4871 (if (not (eventp nil))
4872 (setq new-events (delq nil new-events)))
4873 (append new-events unread-command-events)))
4874 ;; XEmacs
4875 (setq
4876 unread-command-events
4877 (append
4878 (cond ((viper-characterp arg) (list (character-to-event arg)))
4879 ((eventp arg) (list arg))
4880 ((stringp arg) (mapcar 'character-to-event arg))
4881 ((vectorp arg) (append arg nil)) ; turn into list
4882 ((listp arg) (viper-eventify-list-xemacs arg))
4883 (t (error
4884 "viper-set-unread-command-events: Invalid argument, %S" arg)))
4885 unread-command-events))))
4887 ;; list is assumed to be a list of events of characters
4888 (defun viper-eventify-list-xemacs (lis)
4889 (mapcar
4890 (lambda (elt)
4891 (cond ((viper-characterp elt) (character-to-event elt))
4892 ((eventp elt) elt)
4893 (t (error
4894 "viper-eventify-list-xemacs: can't convert to event, %S"
4895 elt))))
4896 lis))
4900 ;;; viper-cmd.el ends here