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)
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.
27 (defvar viper-minibuffer-current-face
)
28 (defvar viper-minibuffer-insert-face
)
29 (defvar viper-minibuffer-vi-face
)
30 (defvar viper-minibuffer-emacs-face
)
32 (defvar viper-mode-string
)
33 (defvar viper-custom-file-name
)
34 (defvar iso-accents-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
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
))
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
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 (memq (if (and (viper-characterp com
) (< com
0))
83 (- com
) com
) (, nms
)))))))
85 ;; Variables for defining VI commands
87 ;; Modifying commands that can be prefixes to movement commands
88 (defconst viper-prefix-commands
'(?c ?d ?y ?
! ?
= ?
# ?
< ?
> ?
\"))
89 ;; define viper-prefix-command-p
90 (viper-test-com-defun viper-prefix-command)
92 ;; Commands that are pairs eg. dd. r and R here are a hack
93 (defconst viper-charpair-commands
'(?c ?d ?y ?
! ?
= ?
< ?
> ?r ?R
))
94 ;; define viper-charpair-command-p
95 (viper-test-com-defun viper-charpair-command)
97 (defconst viper-movement-commands
'(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
98 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
99 ?^ ?
( ?
) ?- ?
+ ?| ?
{ ?
} ?
[ ?
] ?
' ?
`
100 ?\
; ?, ?0 ?? ?/ ?\ ?\C-m
105 ;; define viper-movement-command-p
106 (viper-test-com-defun viper-movement-command)
109 (defconst viper-digit-commands
'(?
1 ?
2 ?
3 ?
4 ?
5 ?
6 ?
7 ?
8 ?
9))
111 ;; define viper-digit-command-p
112 (viper-test-com-defun viper-digit-command)
114 ;; Commands that can be repeated by . (dotted)
115 (defconst viper-dotable-commands
'(?c ?d ?C ?s ?S ?D ?
> ?
<))
116 ;; define viper-dotable-command-p
117 (viper-test-com-defun viper-dotable-command)
119 ;; Commands that can follow a #
120 (defconst viper-hash-commands
'(?c ?C ?g ?q ?s
))
121 ;; define viper-hash-command-p
122 (viper-test-com-defun viper-hash-command)
124 ;; Commands that may have registers as prefix
125 (defconst viper-regsuffix-commands
'(?d ?y ?Y ?D ?p ?P ?x ?X
))
126 ;; define viper-regsuffix-command-p
127 (viper-test-com-defun viper-regsuffix-command)
129 (defconst viper-vi-commands
(append viper-movement-commands
131 viper-dotable-commands
132 viper-charpair-commands
134 viper-prefix-commands
135 viper-regsuffix-commands
)
136 "The list of all commands in Vi-state.")
137 ;; define viper-vi-command-p
138 (viper-test-com-defun viper-vi-command)
140 ;; Where viper saves mark. This mark is resurrected by m^
141 (defvar viper-saved-mark nil
)
149 ;; Runs viper-after-change-functions inside after-change-functions
150 (defun viper-after-change-sentinel (beg end len
)
151 (run-hook-with-args 'viper-after-change-functions beg end len
))
153 ;; Runs viper-before-change-functions inside before-change-functions
154 (defun viper-before-change-sentinel (beg end
)
155 (run-hook-with-args 'viper-before-change-functions beg end
))
157 (defsubst viper-post-command-sentinel
()
158 (run-hooks 'viper-post-command-hooks
)
159 (if (eq viper-current-state
'vi-state
)
160 (viper-restore-cursor-color 'after-insert-mode
)))
162 (defsubst viper-pre-command-sentinel
()
163 (run-hooks 'viper-pre-command-hooks
))
165 ;; Needed so that Viper will be able to figure the last inserted
166 ;; chunk of text with reasonable accuracy.
167 (defsubst viper-insert-state-post-command-sentinel
()
168 (if (and (memq viper-current-state
'(insert-state replace-state
))
170 (>= (point) viper-insert-point
))
171 (setq viper-last-posn-while-in-insert-state
(point-marker)))
172 (or (viper-overlay-p viper-replace-overlay
)
174 (viper-set-replace-overlay (point-min) (point-min))
175 (viper-hide-replace-overlay)))
176 (if (eq viper-current-state
'insert-state
)
177 (let ((has-saved-cursor-color-in-insert-mode
178 (stringp (viper-get-saved-cursor-color-in-insert-mode))))
179 (or has-saved-cursor-color-in-insert-mode
180 (string= (viper-get-cursor-color) viper-insert-state-cursor-color
)
181 ;; save current color, if not already saved
182 (viper-save-cursor-color 'before-insert-mode
))
183 ;; set insert mode cursor color
184 (viper-change-cursor-color viper-insert-state-cursor-color
)))
186 (if (and (memq this-command
'(dabbrev-expand hippie-expand
))
187 (integerp viper-pre-command-point
)
188 (markerp viper-insert-point
)
189 (marker-position viper-insert-point
)
190 (> viper-insert-point viper-pre-command-point
))
191 (viper-move-marker-locally viper-insert-point viper-pre-command-point
))
194 (defsubst viper-insert-state-pre-command-sentinel
()
195 (or (memq this-command
'(self-insert-command))
196 (memq (viper-event-key last-command-event
)
197 '(up down left right
(meta f
) (meta b
)
198 (control n
) (control p
) (control f
) (control b
)))
199 (viper-restore-cursor-color 'after-insert-mode
))
200 (if (and (memq this-command
'(dabbrev-expand hippie-expand
))
201 (markerp viper-insert-point
)
202 (marker-position viper-insert-point
))
203 (setq viper-pre-command-point
(marker-position viper-insert-point
))))
205 (defsubst viper-R-state-post-command-sentinel
()
206 ;; Restoring cursor color is needed despite
207 ;; viper-replace-state-pre-command-sentinel: When you jump to another buffer
208 ;; in another frame, the pre-command hook won't change cursor color to
209 ;; default in that other frame. So, if the second frame cursor was red and
210 ;; we set the point outside the replacement region, then the cursor color
211 ;; will remain red. Restoring the default, below, prevents this.
212 (if (and (<= (viper-replace-start) (point))
213 (<= (point) (viper-replace-end)))
214 (viper-change-cursor-color viper-replace-overlay-cursor-color
)
215 (viper-restore-cursor-color 'after-replace-mode
)
218 ;; to speed up, don't change cursor color before self-insert
219 ;; and common move commands
220 (defsubst viper-replace-state-pre-command-sentinel
()
221 (or (memq this-command
'(self-insert-command))
222 (memq (viper-event-key last-command-event
)
223 '(up down left right
(meta f
) (meta b
)
224 (control n
) (control p
) (control f
) (control b
)))
225 (viper-restore-cursor-color 'after-replace-mode
)))
227 (defun viper-replace-state-post-command-sentinel ()
228 ;; Restoring cursor color is needed despite
229 ;; viper-replace-state-pre-command-sentinel: When one jumps to another buffer
230 ;; in another frame, the pre-command hook won't change cursor color to
231 ;; default in that other frame. So, if the second frame cursor was red and
232 ;; we set the point outside the replacement region, then the cursor color
233 ;; will remain red. Restoring the default, below, fixes this problem.
235 ;; We optimize for self-insert-command's here, since they either don't change
236 ;; cursor color or, if they terminate replace mode, the color will be changed
237 ;; in viper-finish-change
238 (or (memq this-command
'(self-insert-command))
239 (viper-restore-cursor-color 'after-replace-mode
))
241 ((eq viper-current-state
'replace-state
)
242 ;; delete characters to compensate for inserted chars.
243 (let ((replace-boundary (viper-replace-end)))
245 (goto-char viper-last-posn-in-replace-region
)
246 (viper-trim-replace-chars-to-delete-if-necessary)
247 (delete-char viper-replace-chars-to-delete
)
248 (setq viper-replace-chars-to-delete
0)
249 ;; terminate replace mode if reached replace limit
250 (if (= viper-last-posn-in-replace-region
(viper-replace-end))
251 (viper-finish-change)))
253 (if (viper-pos-within-region
254 (point) (viper-replace-start) replace-boundary
)
256 ;; the state may have changed in viper-finish-change above
257 (if (eq viper-current-state
'replace-state
)
258 (viper-change-cursor-color viper-replace-overlay-cursor-color
))
259 (setq viper-last-posn-in-replace-region
(point-marker))))
261 ;; terminate replace mode if changed Viper states.
262 (t (viper-finish-change))))
267 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
268 (defun viper-change-state (new-state)
269 ;; Keep viper-post/pre-command-hooks fresh.
270 ;; We remove then add viper-post/pre-command-sentinel since it is very
271 ;; desirable that viper-pre-command-sentinel is the last hook and
272 ;; viper-post-command-sentinel is the first hook.
274 (make-local-hook 'viper-after-change-functions
)
275 (make-local-hook 'viper-before-change-functions
)
276 (make-local-hook 'viper-post-command-hooks
)
277 (make-local-hook 'viper-pre-command-hooks
)
279 (remove-hook 'post-command-hook
'viper-post-command-sentinel
)
280 (add-hook 'post-command-hook
'viper-post-command-sentinel
)
281 (remove-hook 'pre-command-hook
'viper-pre-command-sentinel
)
282 (add-hook 'pre-command-hook
'viper-pre-command-sentinel t
)
283 ;; These hooks will be added back if switching to insert/replace mode
284 (remove-hook 'viper-post-command-hooks
285 'viper-insert-state-post-command-sentinel
'local
)
286 (remove-hook 'viper-pre-command-hooks
287 'viper-insert-state-pre-command-sentinel
'local
)
288 (setq viper-intermediate-command nil
)
289 (cond ((eq new-state
'vi-state
)
290 (cond ((member viper-current-state
'(insert-state replace-state
))
292 ;; move viper-last-posn-while-in-insert-state
293 ;; This is a normal hook that is executed in insert/replace
294 ;; states after each command. In Vi/Emacs state, it does
295 ;; nothing. We need to execute it here to make sure that
296 ;; the last posn was recorded when we hit ESC.
297 ;; It may be left unrecorded if the last thing done in
298 ;; insert/repl state was dabbrev-expansion or abbrev
299 ;; expansion caused by hitting ESC
300 (viper-insert-state-post-command-sentinel)
302 (condition-case conds
304 (viper-save-last-insertion
306 viper-last-posn-while-in-insert-state
)
307 (if viper-began-as-replace
308 (setq viper-began-as-replace nil
)
309 ;; repeat insert commands if numerical arg > 1
311 (viper-repeat-insert-command))))
313 (viper-message-conditions conds
)))
315 (if (> (length viper-last-insertion
) 0)
316 (viper-push-onto-ring viper-last-insertion
317 'viper-insertion-ring
))
319 (if viper-ex-style-editing
320 (or (bolp) (backward-char 1))))
324 ((memq new-state
'(insert-state replace-state
))
325 (if (memq viper-current-state
'(emacs-state vi-state
))
326 (viper-move-marker-locally 'viper-insert-point
(point)))
327 (viper-move-marker-locally
328 'viper-last-posn-while-in-insert-state
(point))
329 (add-hook 'viper-post-command-hooks
330 'viper-insert-state-post-command-sentinel t
'local
)
331 (add-hook 'viper-pre-command-hooks
332 'viper-insert-state-pre-command-sentinel t
'local
))
335 ;; Nothing needs to be done to switch to emacs mode! Just set some
336 ;; variables, which is already done in viper-change-state-to-emacs!
339 ;; always turn off iso-accents-mode in vi-state, or else we won't be able to
340 ;; use the keys `,',^ , as they will do accents instead of Vi actions.
341 (cond ((eq new-state
'vi-state
) (viper-set-iso-accents-mode nil
));accents off
342 (viper-automatic-iso-accents (viper-set-iso-accents-mode t
));accents on
343 (t (viper-set-iso-accents-mode nil
)))
344 ;; Always turn off quail mode in vi state
345 (cond ((eq new-state
'vi-state
) (viper-set-input-method nil
)) ;intl input off
346 (viper-special-input-method (viper-set-input-method t
)) ;intl input on
347 (t (viper-set-input-method nil
)))
349 (setq viper-current-state new-state
)
351 (viper-update-syntax-classes)
352 (viper-normalize-minor-mode-map-alist)
353 (viper-adjust-keys-for new-state
)
354 (viper-set-mode-vars-for new-state
)
355 (viper-refresh-mode-line)
360 (defun viper-adjust-keys-for (state)
361 "Make necessary adjustments to keymaps before entering STATE."
362 (cond ((memq state
'(insert-state replace-state
))
363 (if viper-auto-indent
365 (define-key viper-insert-basic-map
"\C-m" 'viper-autoindent
)
366 (if viper-want-emacs-keys-in-insert
368 (define-key viper-insert-basic-map
"\C-j" nil
)
370 (define-key viper-insert-basic-map
"\C-j" 'viper-autoindent
)))
371 (define-key viper-insert-basic-map
"\C-m" nil
)
372 (define-key viper-insert-basic-map
"\C-j" nil
))
374 (setq viper-insert-diehard-minor-mode
375 (not viper-want-emacs-keys-in-insert
))
377 (if viper-want-ctl-h-help
379 (define-key viper-insert-basic-map
"\C-h" 'help-command
)
380 (define-key viper-replace-map
"\C-h" 'help-command
))
381 (define-key viper-insert-basic-map
382 "\C-h" 'viper-del-backward-char-in-insert
)
383 (define-key viper-replace-map
384 "\C-h" 'viper-del-backward-char-in-replace
))
385 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
386 (define-key viper-insert-basic-map
387 [backspace] 'viper-del-backward-char-in-insert)
388 (define-key viper-replace-map
389 [backspace] 'viper-del-backward-char-in-replace
)
390 ) ; end insert/replace case
392 (setq viper-vi-diehard-minor-mode
(not viper-want-emacs-keys-in-vi
))
393 (if viper-want-ctl-h-help
394 (define-key viper-vi-basic-map
"\C-h" 'help-command
)
395 (define-key viper-vi-basic-map
"\C-h" 'viper-backward-char
))
396 ;; In XEmacs, C-h overrides backspace, so we make sure it doesn't.
397 (define-key viper-vi-basic-map
[backspace] 'viper-backward-char))
401 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
402 ;; This ensures that Viper bindings are in effect, regardless of which minor
403 ;; modes were turned on by the user or by other packages.
404 (defun viper-normalize-minor-mode-map-alist ()
405 (setq minor-mode-map-alist
406 (viper-append-filter-alist
408 (cons 'viper-vi-intercept-minor-mode viper-vi-intercept-map)
409 (cons 'viper-vi-minibuffer-minor-mode viper-minibuffer-map)
410 (cons 'viper-vi-local-user-minor-mode viper-vi-local-user-map)
411 (cons 'viper-vi-kbd-minor-mode viper-vi-kbd-map)
412 (cons 'viper-vi-global-user-minor-mode viper-vi-global-user-map)
413 (cons 'viper-vi-state-modifier-minor-mode
415 (cdr (assoc major-mode
416 viper-vi-state-modifier-alist)))
417 (cdr (assoc major-mode viper-vi-state-modifier-alist))
419 (cons 'viper-vi-diehard-minor-mode viper-vi-diehard-map)
420 (cons 'viper-vi-basic-minor-mode viper-vi-basic-map)
421 (cons 'viper-insert-intercept-minor-mode
422 viper-insert-intercept-map)
423 (cons 'viper-replace-minor-mode viper-replace-map)
424 ;; viper-insert-minibuffer-minor-mode must come after
425 ;; viper-replace-minor-mode
426 (cons 'viper-insert-minibuffer-minor-mode
427 viper-minibuffer-map)
428 (cons 'viper-insert-local-user-minor-mode
429 viper-insert-local-user-map)
430 (cons 'viper-insert-kbd-minor-mode viper-insert-kbd-map)
431 (cons 'viper-insert-global-user-minor-mode
432 viper-insert-global-user-map)
433 (cons 'viper-insert-state-modifier-minor-mode
435 (cdr (assoc major-mode
436 viper-insert-state-modifier-alist)))
437 (cdr (assoc major-mode
438 viper-insert-state-modifier-alist))
440 (cons 'viper-insert-diehard-minor-mode viper-insert-diehard-map)
441 (cons 'viper-insert-basic-minor-mode viper-insert-basic-map)
442 (cons 'viper-emacs-intercept-minor-mode
443 viper-emacs-intercept-map)
444 (cons 'viper-emacs-local-user-minor-mode
445 viper-emacs-local-user-map)
446 (cons 'viper-emacs-kbd-minor-mode viper-emacs-kbd-map)
447 (cons 'viper-emacs-global-user-minor-mode
448 viper-emacs-global-user-map)
449 (cons 'viper-emacs-state-modifier-minor-mode
452 (assoc major-mode viper-emacs-state-modifier-alist)))
454 (assoc major-mode viper-emacs-state-modifier-alist))
457 minor-mode-map-alist)))
463 ;; Viper mode-changing commands and utilities
465 ;; Modifies mode-line-buffer-identification.
466 (defun viper-refresh-mode-line ()
467 (setq viper-mode-string
468 (cond ((eq viper-current-state 'emacs-state) viper-emacs-state-id)
469 ((eq viper-current-state 'vi-state) viper-vi-state-id)
470 ((eq viper-current-state 'replace-state) viper-replace-state-id)
471 ((eq viper-current-state 'insert-state) viper-insert-state-id)))
473 ;; Sets Viper mode string in global-mode-string
474 (force-mode-line-update))
477 ;; Switch from Insert state to Vi state.
478 (defun viper-exit-insert-state ()
480 (viper-change-state-to-vi))
482 (defun viper-set-mode-vars-for (state)
483 "Sets Viper minor mode variables to put Viper's state STATE in effect."
486 (setq viper-vi-minibuffer-minor-mode nil
487 viper-insert-minibuffer-minor-mode nil
488 viper-vi-intercept-minor-mode nil
489 viper-insert-intercept-minor-mode nil
491 viper-vi-local-user-minor-mode nil
492 viper-vi-kbd-minor-mode nil
493 viper-vi-global-user-minor-mode nil
494 viper-vi-state-modifier-minor-mode nil
495 viper-vi-diehard-minor-mode nil
496 viper-vi-basic-minor-mode nil
498 viper-replace-minor-mode nil
500 viper-insert-local-user-minor-mode nil
501 viper-insert-kbd-minor-mode nil
502 viper-insert-global-user-minor-mode nil
503 viper-insert-state-modifier-minor-mode nil
504 viper-insert-diehard-minor-mode nil
505 viper-insert-basic-minor-mode nil
506 viper-emacs-intercept-minor-mode t
507 viper-emacs-local-user-minor-mode t
508 viper-emacs-kbd-minor-mode (not (viper-is-in-minibuffer))
509 viper-emacs-global-user-minor-mode t
510 viper-emacs-state-modifier-minor-mode t
514 (if (eq state 'vi-state) ; adjust for vi-state
516 viper-vi-intercept-minor-mode t
517 viper-vi-minibuffer-minor-mode (viper-is-in-minibuffer)
518 viper-vi-local-user-minor-mode t
519 viper-vi-kbd-minor-mode (not (viper-is-in-minibuffer))
520 viper-vi-global-user-minor-mode t
521 viper-vi-state-modifier-minor-mode t
522 ;; don't let the diehard keymap block command completion
523 ;; and other things in the minibuffer
524 viper-vi-diehard-minor-mode (not
525 (or viper-want-emacs-keys-in-vi
526 (viper-is-in-minibuffer)))
527 viper-vi-basic-minor-mode t
528 viper-emacs-intercept-minor-mode nil
529 viper-emacs-local-user-minor-mode nil
530 viper-emacs-kbd-minor-mode nil
531 viper-emacs-global-user-minor-mode nil
532 viper-emacs-state-modifier-minor-mode nil
535 ;; Insert and Replace states
536 (if (member state '(insert-state replace-state))
538 viper-insert-intercept-minor-mode t
539 viper-replace-minor-mode (eq state 'replace-state)
540 viper-insert-minibuffer-minor-mode (viper-is-in-minibuffer)
541 viper-insert-local-user-minor-mode t
542 viper-insert-kbd-minor-mode (not (viper-is-in-minibuffer))
543 viper-insert-global-user-minor-mode t
544 viper-insert-state-modifier-minor-mode t
545 ;; don't let the diehard keymap block command completion
546 ;; and other things in the minibuffer
547 viper-insert-diehard-minor-mode (not
549 viper-want-emacs-keys-in-insert
550 (viper-is-in-minibuffer)))
551 viper-insert-basic-minor-mode t
552 viper-emacs-intercept-minor-mode nil
553 viper-emacs-local-user-minor-mode nil
554 viper-emacs-kbd-minor-mode nil
555 viper-emacs-global-user-minor-mode nil
556 viper-emacs-state-modifier-minor-mode nil
560 (if (viper-has-face-support-p)
561 (setq viper-minibuffer-current-face
562 (cond ((eq state 'emacs-state) viper-minibuffer-emacs-face)
563 ((eq state 'vi-state) viper-minibuffer-vi-face)
564 ((memq state '(insert-state replace-state))
565 viper-minibuffer-insert-face))))
567 (if (viper-is-in-minibuffer)
568 (viper-set-minibuffer-overlay))
571 ;; This also takes care of the annoying incomplete lines in files.
572 ;; Also, this fixes `undo' to work vi-style for complex commands.
573 (defun viper-change-state-to-vi ()
574 "Change Viper state to Vi."
576 (if (and viper-first-time (not (viper-is-in-minibuffer)))
578 (if overwrite-mode (overwrite-mode nil))
579 (or (viper-overlay-p viper-replace-overlay)
580 (viper-set-replace-overlay (point-min) (point-min)))
581 (viper-hide-replace-overlay)
582 (if abbrev-mode (expand-abbrev))
583 (if (and auto-fill-function (> (current-column) fill-column))
584 (funcall auto-fill-function))
585 ;; don't leave whitespace lines around
586 (if (and (memq last-command
588 viper-open-line viper-Open-line
589 viper-replace-state-exit-cmd))
590 (viper-over-whitespace-line))
591 (indent-to-left-margin))
592 (viper-add-newline-at-eob-if-necessary)
594 (viper-change-state 'vi-state)
596 (viper-restore-cursor-color 'after-insert-mode)
598 ;; Protect against user errors in hooks
599 (condition-case conds
600 (run-hooks 'viper-vi-state-hook)
602 (viper-message-conditions conds)))))
604 (defun viper-change-state-to-insert ()
605 "Change Viper state to Insert."
607 (viper-change-state 'insert-state)
609 (or (viper-overlay-p viper-replace-overlay)
610 (viper-set-replace-overlay (point-min) (point-min)))
611 (viper-hide-replace-overlay)
613 (let ((has-saved-cursor-color-in-insert-mode
614 (stringp (viper-get-saved-cursor-color-in-insert-mode))))
615 (or has-saved-cursor-color-in-insert-mode
616 (string= (viper-get-cursor-color) viper-insert-state-cursor-color)
617 (viper-save-cursor-color 'before-insert-mode))
618 (viper-change-cursor-color viper-insert-state-cursor-color))
620 ;; Protect against user errors in hooks
621 (condition-case conds
622 (run-hooks 'viper-insert-state-hook)
624 (viper-message-conditions conds))))
626 (defsubst viper-downgrade-to-insert ()
627 (setq viper-current-state 'insert-state
628 viper-replace-minor-mode nil))
632 ;; Change to replace state. When the end of replacement region is reached,
633 ;; replace state changes to insert state.
634 (defun viper-change-state-to-replace (&optional non-R-cmd)
635 (viper-change-state 'replace-state)
636 ;; Run insert-state-hook
637 (condition-case conds
638 (run-hooks 'viper-insert-state-hook 'viper-replace-state-hook)
640 (viper-message-conditions conds)))
643 (viper-start-replace)
644 ;; 'R' is implemented using Emacs's overwrite-mode
645 (viper-start-R-mode))
649 (defun viper-change-state-to-emacs ()
650 "Change Viper state to Emacs."
652 (or (viper-overlay-p viper-replace-overlay)
653 (viper-set-replace-overlay (point-min) (point-min)))
654 (viper-hide-replace-overlay)
655 (viper-change-state 'emacs-state)
657 ;; Protect agains user errors in hooks
658 (condition-case conds
659 (run-hooks 'viper-emacs-state-hook)
661 (viper-message-conditions conds))))
663 ;; escape to emacs mode termporarily
664 (defun viper-escape-to-emacs (arg &optional events)
665 "Escape to Emacs state from Vi state for one Emacs command.
666 ARG is used as the prefix value for the executed command. If
667 EVENTS is a list of events, which become the beginning of the command."
669 (if (= last-command-char ?\\)
670 (message "Switched to EMACS state for the next command..."))
671 (viper-escape-to-state arg events 'emacs-state))
673 ;; escape to Vi mode termporarily
674 (defun viper-escape-to-vi (arg)
675 "Escape from Emacs state to Vi state for one Vi 1-character command.
676 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
677 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
678 `viper-escape-to-vi' is used."
680 (message "Switched to VI state for the next command...")
681 (viper-escape-to-state arg nil 'vi-state))
683 ;; Escape to STATE mode for one Emacs command.
684 (defun viper-escape-to-state (arg events state)
685 ;;(let (com key prefix-arg)
687 ;; this temporarily turns off Viper's minor mode keymaps
688 (viper-set-mode-vars-for state)
689 (viper-normalize-minor-mode-map-alist)
690 (if events (viper-set-unread-command-events events))
692 ;; protect against keyboard quit and other errors
694 (let (viper-vi-kbd-minor-mode
695 viper-insert-kbd-minor-mode
696 viper-emacs-kbd-minor-mode)
699 (setq com (key-binding (setq key
701 (read-key-sequence nil)
702 (read-key-sequence nil t)))))
703 ;; In case of binding indirection--chase definitions.
704 ;; Have to do it here because we execute this command under
705 ;; different keymaps, so command-execute may not do the
707 (while (vectorp com) (setq com (key-binding com))))
709 ;; Execute command com in the original Viper state, not in state
710 ;; `state'. Otherwise, if we switch buffers while executing the
711 ;; escaped to command, Viper's mode vars will remain those of
712 ;; `state'. When we return to the orig buffer, the bindings will be
714 (viper-set-mode-vars-for viper-current-state)
716 ;; this-command, last-command-char, last-command-event
717 (setq this-command com)
718 (if viper-xemacs-p ; XEmacs represents key sequences as vectors
719 (setq last-command-event
720 (viper-copy-event (viper-seq-last-elt key))
721 last-command-char (event-to-character last-command-event))
722 ;; Emacs represents them as sequences (str or vec)
723 (setq last-command-event
724 (viper-copy-event (viper-seq-last-elt key))
725 last-command-char last-command-event))
729 (setq prefix-arg (or prefix-arg arg))
730 (command-execute com)))
734 ;; set state in the new buffer
735 (viper-set-mode-vars-for viper-current-state))
737 (defun viper-exec-form-in-vi (form)
738 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
739 (let ((buff (current-buffer))
741 (viper-set-mode-vars-for 'vi-state)
744 (let (viper-vi-kbd-minor-mode) ; execute without kbd macros
745 (setq result (eval form))
750 (if (not (equal buff (current-buffer))) ; cmd switched buffer
753 (viper-set-mode-vars-for viper-current-state)))
754 (viper-set-mode-vars-for viper-current-state)
757 (defun viper-exec-form-in-emacs (form)
758 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
759 Similar to viper-escape-to-emacs, but accepts forms rather than keystrokes."
760 (let ((buff (current-buffer))
762 (viper-set-mode-vars-for 'emacs-state)
763 (setq result (eval form))
764 (if (not (equal buff (current-buffer))) ; cmd switched buffer
767 (viper-set-mode-vars-for viper-current-state)))
768 (viper-set-mode-vars-for viper-current-state)
772 ;; This is needed because minor modes sometimes override essential Viper
773 ;; bindings. By letting Viper know which files these modes are in, it will
774 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
775 (defun viper-harness-minor-mode (load-file)
776 "Familiarize Viper with a minor mode defined in LOAD_FILE.
777 Minor modes that have their own keymaps may overshadow Viper keymaps.
778 This function is designed to make Viper aware of the packages that define
781 (viper-harness-minor-mode load-file)
783 LOAD-FILE is a name of the file where the specific minor mode is defined.
784 Suffixes such as .el or .elc should be stripped."
786 (interactive "sEnter name of the load file: ")
788 (eval-after-load load-file '(viper-normalize-minor-mode-map-alist))
790 ;; Change the default for minor-mode-map-alist each time a harnessed minor
791 ;; mode adds its own keymap to the a-list.
793 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist))
797 (defun viper-ESC (arg)
798 "Emulate ESC key in Emacs.
799 Prevents multiple escape keystrokes if viper-no-multiple-ESC is true.
800 If viper-no-multiple-ESC is 'twice double ESC would ding in vi-state.
801 Other ESC sequences are emulated via the current Emacs's major mode
802 keymap. This is more convenient on TTYs, since this won't block
803 function keys such as up,down, etc. ESC will also will also work as
804 a Meta key in this case. When viper-no-multiple-ESC is nil, ESC functions
805 as a Meta key and any number of multiple escapes is allowed."
808 (cond ((and (not viper-no-multiple-ESC) (eq viper-current-state 'vi-state))
809 (setq char (viper-read-char-exclusive))
810 (viper-escape-to-emacs arg (list ?\e char) ))
811 ((and (eq viper-no-multiple-ESC 'twice)
812 (eq viper-current-state 'vi-state))
813 (setq char (viper-read-char-exclusive))
814 (if (= char (string-to-char viper-ESC-key))
816 (viper-escape-to-emacs arg (list ?\e char) )))
820 (defun viper-alternate-Meta-key (arg)
821 "Simulate Emacs Meta key."
823 (sit-for 1) (message "ESC-")
824 (viper-escape-to-emacs arg '(?\e)))
826 (defun viper-toggle-key-action ()
827 "Action bound to `viper-toggle-key'."
829 (if (and (< viper-expert-level 2) (equal viper-toggle-key "\C-z"))
830 (if (viper-window-display-p)
833 (viper-change-state-to-emacs)))
836 ;; Intercept ESC sequences on dumb terminals.
837 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
839 ;; Check if last key was ESC and if so try to reread it as a function key.
840 ;; But only if there are characters to read during a very short time.
841 ;; Returns the last event, if any.
842 (defun viper-envelop-ESC-key ()
843 (let ((event last-input-event)
846 (if (viper-ESC-event-p event)
848 (if (viper-fast-keysequence-p)
850 (let (minor-mode-map-alist)
851 (viper-set-unread-command-events event)
854 (ad-get-orig-definition 'read-key-sequence) nil))
856 ;; If keyseq translates into something that still has ESC
857 ;; at the beginning, separate ESC from the rest of the seq.
858 ;; In XEmacs we check for events that are keypress meta-key
859 ;; and convert them into [escape key]
861 ;; This is needed for the following reason:
862 ;; If ESC is the first symbol, we interpret it as if the
863 ;; user typed ESC and then quickly some other symbols.
864 ;; If ESC is not the first one, then the key sequence
865 ;; entered was apparently translated into a function key or
866 ;; something (e.g., one may have
867 ;; (define-key function-key-map "\e[192z" [f11])
868 ;; which would translate the escape-sequence generated by
869 ;; f11 in an xterm window into the symbolic key f11.
871 ;; If `first-key' is not an ESC event, we make it into the
872 ;; last-command-event in order to pretend that this key was
873 ;; pressed. This is needed to allow arrow keys to be bound to
874 ;; macros. Otherwise, viper-exec-mapped-kbd-macro will think
875 ;; that the last event was ESC and so it'll execute whatever is
876 ;; bound to ESC. (Viper macros can't be bound to
878 (let* ((first-key (elt keyseq 0))
879 (key-mod (event-modifiers first-key)))
880 (cond ((viper-ESC-event-p first-key)
881 ;; put keys following ESC on the unread list
882 ;; and return ESC as the key-sequence
883 (viper-set-unread-command-events (subseq keyseq 1))
884 (setq last-input-event event
885 keyseq (if viper-emacs-p
887 (vector (character-to-event ?\e)))))
889 (key-press-event-p first-key)
890 (equal '(meta) key-mod))
891 (viper-set-unread-command-events
893 (character-to-event (event-key first-key)))
895 (setq last-input-event event
896 keyseq (vector (character-to-event ?\e))))
898 (setq last-command-event
899 (viper-copy-event first-key)))
903 ;; this is escape event with nothing after it
904 ;; put in unread-command-event and then re-read
905 (viper-set-unread-command-events event)
907 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
909 ;; not an escape event
910 (setq keyseq (vector event)))
915 ;; Listen to ESC key.
916 ;; If a sequence of keys starting with ESC is issued with very short delays,
917 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
918 (defun viper-intercept-ESC-key ()
919 "Function that implements ESC key in Viper emulation of Vi."
921 (let ((cmd (or (key-binding (viper-envelop-ESC-key))
922 '(lambda () (interactive) (error "")))))
924 ;; call the actual function to execute ESC (if no other symbols followed)
925 ;; or the key bound to the ESC sequence (if the sequence was issued
926 ;; with very short delay between characters.
927 (if (eq cmd 'viper-intercept-ESC-key)
929 (cond ((eq viper-current-state 'vi-state)
931 ((eq viper-current-state 'insert-state)
932 'viper-exit-insert-state)
933 ((eq viper-current-state 'replace-state)
934 'viper-replace-state-exit-cmd)
935 (t 'viper-change-state-to-vi)
937 (call-interactively cmd)))
942 ;; prefix argument for Vi mode
944 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
945 ;; represents the numeric value of the prefix argument and COM represents
946 ;; command prefix such as "c", "d", "m" and "y".
948 ;; Get value part of prefix-argument ARG.
949 (defsubst viper-p-val (arg)
952 (if (or (null (car arg)) (equal (car arg) '(nil)))
956 ;; Get raw value part of prefix-argument ARG.
957 (defsubst viper-P-val (arg)
958 (cond ((consp arg) (car arg))
961 ;; Get com part of prefix-argument ARG.
962 (defsubst viper-getcom (arg)
963 (cond ((null arg) nil)
964 ((consp arg) (cdr arg))
967 ;; Get com part of prefix-argument ARG and modify it.
968 (defun viper-getCom (arg)
969 (let ((com (viper-getcom arg)))
970 (cond ((equal com ?c) ?c)
971 ;; Previously, ?c was being converted to ?C, but this prevented
972 ;; multiline replace regions.
973 ;;((equal com ?c) ?C)
979 ;; Compute numeric prefix arg value.
980 ;; Invoked by EVENT. COM is the command part obtained so far.
981 (defun viper-prefix-arg-value (event-char com)
982 (let ((viper-intermediate-command 'viper-digit-argument)
985 (while (and (viper-characterp event-char)
986 (>= event-char ?0) (<= event-char ?9))
987 (setq value (+ (* (if (integerp value) value 0) 10) (- event-char ?0)))
988 (setq event-char (viper-read-event-convert-to-char)))
990 (setq prefix-arg value)
991 (if com (setq prefix-arg (cons prefix-arg com)))
992 (while (eq event-char ?U)
993 (viper-describe-arg prefix-arg)
994 (setq event-char (viper-read-event-convert-to-char)))
996 (if (or com (and (not (eq viper-current-state 'vi-state))
997 ;; make sure it is a Vi command
998 (viper-characterp event-char)
999 (viper-vi-command-p event-char)
1001 ;; If appears to be one of the vi commands,
1002 ;; then execute it with funcall and clear prefix-arg in order to not
1003 ;; confuse subsequent commands
1005 ;; last-command-char is the char we want emacs to think was typed
1006 ;; last. If com is not nil, the viper-digit-argument command was
1007 ;; called from within viper-prefix-arg command, such as `d', `w',
1008 ;; etc., i.e., the user typed, say, d2. In this case, `com' would be
1009 ;; `d', `w', etc. If viper-digit-argument was invoked by
1010 ;; viper-escape-to-vi (which is indicated by the fact that the
1011 ;; current state is not vi-state), then `event-char' represents the
1012 ;; vi command to be executed (e.g., `d', `w', etc). Again,
1013 ;; last-command-char must make emacs believe that this is the command
1015 (cond ((eq event-char 'return) (setq event-char ?\C-m))
1016 ((eq event-char 'delete) (setq event-char ?\C-?))
1017 ((eq event-char 'backspace) (setq event-char ?\C-h))
1018 ((eq event-char 'space) (setq event-char ?\ )))
1019 (setq last-command-char (or com event-char))
1020 (setq func (viper-exec-form-in-vi
1021 (` (key-binding (char-to-string (, event-char))))))
1022 (funcall func prefix-arg)
1023 (setq prefix-arg nil))
1024 ;; some other command -- let emacs do it in its own way
1025 (viper-set-unread-command-events event-char))
1029 ;; Vi operator as prefix argument."
1030 (defun viper-prefix-arg-com (char value com)
1036 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
1037 viper-buffer-search-char)))
1039 ;; this means that we already have a command character, so we
1040 ;; construct a com list and exit while. however, if char is "
1043 ;; new com is (CHAR . OLDCOM)
1044 (if (memq char '(?# ?\")) (error ""))
1045 (setq com (cons char com))
1047 ;; If com is nil we set com as char, and read more. Again, if char is
1048 ;; ", we read the name of register and store it in viper-use-register.
1049 ;; if char is !, =, or #, a complete com is formed so we exit the while
1051 (cond ((memq char '(?! ?=))
1053 (setq char (read-char))
1056 ;; read a char and encode it as com
1057 (setq com (+ 128 (read-char)))
1058 (setq char (read-char)))
1060 (let ((reg (read-char)))
1061 (if (viper-valid-register reg)
1062 (setq viper-use-register reg)
1064 (setq char (read-char))))
1067 (setq char (read-char))))))
1070 ;; `com' is a single char, so we construct the command argument
1071 ;; and if `char' is `?', we describe the arg; otherwise
1072 ;; we prepare the command that will be executed at the end.
1074 (setq cmd-info (cons value com))
1076 (viper-describe-arg cmd-info)
1077 (setq char (read-char)))
1078 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
1079 ;; execute it at the very end
1080 (or (viper-movement-command-p char)
1081 (viper-digit-command-p char)
1082 (viper-regsuffix-command-p char)
1083 (= char ?!) ; bang command
1085 (setq cmd-to-exec-at-end
1086 (viper-exec-form-in-vi
1087 (` (key-binding (char-to-string (, char)))))))
1089 ;; as com is non-nil, this means that we have a command to execute
1090 (if (memq (car com) '(?r ?R))
1091 ;; execute apropriate region command.
1092 (let ((char (car com)) (com (cdr com)))
1093 (setq prefix-arg (cons value com))
1094 (if (= char ?r) (viper-region prefix-arg)
1095 (viper-Region prefix-arg))
1097 (setq prefix-arg nil))
1098 ;; otherwise, reset prefix arg and call appropriate command
1099 (setq value (if (null value) 1 value))
1100 (setq prefix-arg nil)
1102 ;; If we change ?C to ?c here, then cc will enter replacement mode
1103 ;; rather than deleting lines. However, it will affect 1 less line than
1104 ;; normal. We decided to not use replacement mode here and follow Vi,
1105 ;; since replacement mode on n full lines can be achieved with nC.
1106 ((equal com '(?c . ?c)) (viper-line (cons value ?C)))
1107 ((equal com '(?d . ?d)) (viper-line (cons value ?D)))
1108 ((equal com '(?d . ?y)) (viper-yank-defun))
1109 ((equal com '(?y . ?y)) (viper-line (cons value ?Y)))
1110 ((equal com '(?< . ?<)) (viper-line (cons value ?<)))
1111 ((equal com '(?> . ?>)) (viper-line (cons value ?>)))
1112 ((equal com '(?! . ?!)) (viper-line (cons value ?!)))
1113 ((equal com '(?= . ?=)) (viper-line (cons value ?=)))
1116 (if cmd-to-exec-at-end
1118 (setq last-command-char char)
1119 (setq last-command-event
1121 (if viper-xemacs-p (character-to-event char) char)))
1123 (funcall cmd-to-exec-at-end cmd-info)
1128 (defun viper-describe-arg (arg)
1130 (setq val (viper-P-val arg)
1131 com (viper-getcom arg))
1134 (message "Value is nil, and command is nil")
1135 (message "Value is nil, and command is `%c'" com))
1137 (message "Value is `%d', and command is nil" val)
1138 (message "Value is `%d', and command is `%c'" val com)))))
1140 (defun viper-digit-argument (arg)
1141 "Begin numeric argument for the next command."
1143 (viper-leave-region-active)
1144 (viper-prefix-arg-value
1145 last-command-char (if (consp arg) (cdr arg) nil)))
1147 (defun viper-command-argument (arg)
1148 "Accept a motion command as an argument."
1150 (let ((viper-intermediate-command 'viper-command-argument))
1152 (viper-prefix-arg-com
1154 (cond ((null arg) nil)
1155 ((consp arg) (car arg))
1156 ((integerp arg) arg)
1157 (t (error viper-InvalidCommandArgument)))
1158 (cond ((null arg) nil)
1159 ((consp arg) (cdr arg))
1160 ((integerp arg) nil)
1161 (t (error viper-InvalidCommandArgument))))
1162 (quit (setq viper-use-register nil)
1163 (signal 'quit nil)))
1164 (viper-deactivate-mark)))
1167 ;; repeat last destructive command
1169 ;; Append region to text in register REG.
1170 ;; START and END are buffer positions indicating what to append.
1171 (defsubst viper-append-to-register (reg start end)
1172 (set-register reg (concat (if (stringp (get-register reg))
1173 (get-register reg) "")
1174 (buffer-substring start end))))
1176 ;; Saves last inserted text for possible use by viper-repeat command.
1177 (defun viper-save-last-insertion (beg end)
1179 (setq viper-last-insertion (buffer-substring beg end))
1181 ;; beg or end marker are somehow screwed up
1182 (setq viper-last-insertion nil)))
1183 (setq viper-last-insertion (buffer-substring beg end))
1184 (or (< (length viper-d-com) 5)
1185 (setcar (nthcdr 4 viper-d-com) viper-last-insertion))
1186 (or (null viper-command-ring)
1187 (ring-empty-p viper-command-ring)
1189 (setcar (nthcdr 4 (viper-current-ring-item viper-command-ring))
1190 viper-last-insertion)
1191 ;; del most recent elt, if identical to the second most-recent
1192 (viper-cleanup-ring viper-command-ring)))
1195 (defsubst viper-yank-last-insertion ()
1196 "Inserts the text saved by the previous viper-save-last-insertion command."
1198 (insert viper-last-insertion)
1202 ;; define functions to be executed
1204 ;; invoked by the `C' command
1205 (defun viper-exec-change (m-com com)
1206 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1207 (set-marker viper-com-point (point) (current-buffer)))
1208 ;; handle C cmd at the eol and at eob.
1209 (if (or (and (eolp) (= viper-com-point (point)))
1210 (= viper-com-point (point-max)))
1212 (insert " ")(backward-char 1)))
1213 (if (= viper-com-point (point))
1214 (viper-forward-char-carefully))
1215 (set-mark viper-com-point)
1216 (if (eq m-com 'viper-next-line-at-bol)
1217 (viper-enlarge-region (mark t) (point)))
1218 (if (< (point) (mark t))
1219 (exchange-point-and-mark))
1220 (if (eq (preceding-char) ?\n)
1221 (viper-backward-char-carefully)) ; give back the newline
1223 (viper-change (mark t) (point))
1224 (viper-change-subr (mark t) (point))))
1226 ;; this is invoked by viper-substitute-line
1227 (defun viper-exec-Change (m-com com)
1229 (set-mark viper-com-point)
1230 (viper-enlarge-region (mark t) (point))
1231 (if viper-use-register
1233 (cond ((viper-valid-register viper-use-register '(letter digit))
1235 viper-use-register (mark t) (point) nil))
1236 ((viper-valid-register viper-use-register '(Letter))
1237 (viper-append-to-register
1238 (downcase viper-use-register) (mark t) (point)))
1239 (t (setq viper-use-register nil)
1240 (error viper-InvalidRegister viper-use-register)))
1241 (setq viper-use-register nil)))
1242 (delete-region (mark t) (point)))
1245 (viper-change-state-to-insert)
1246 (viper-yank-last-insertion)))
1248 (defun viper-exec-delete (m-com com)
1249 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1250 (set-marker viper-com-point (point) (current-buffer)))
1251 (let (chars-deleted)
1252 (if viper-use-register
1254 (cond ((viper-valid-register viper-use-register '(letter digit))
1256 viper-use-register viper-com-point (point) nil))
1257 ((viper-valid-register viper-use-register '(Letter))
1258 (viper-append-to-register
1259 (downcase viper-use-register) viper-com-point (point)))
1260 (t (setq viper-use-register nil)
1261 (error viper-InvalidRegister viper-use-register)))
1262 (setq viper-use-register nil)))
1264 (if (eq last-command 'd-command) 'kill-region nil))
1265 (setq chars-deleted (abs (- (point) viper-com-point)))
1266 (if (> chars-deleted viper-change-notification-threshold)
1267 (message "Deleted %d characters" chars-deleted))
1268 (kill-region viper-com-point (point))
1269 (setq this-command 'd-command)
1270 (if viper-ex-style-motion
1271 (if (and (eolp) (not (bolp))) (backward-char 1)))))
1273 (defun viper-exec-Delete (m-com com)
1275 (set-mark viper-com-point)
1276 (viper-enlarge-region (mark t) (point))
1277 (let (lines-deleted)
1278 (if viper-use-register
1280 (cond ((viper-valid-register viper-use-register '(letter digit))
1282 viper-use-register (mark t) (point) nil))
1283 ((viper-valid-register viper-use-register '(Letter))
1284 (viper-append-to-register
1285 (downcase viper-use-register) (mark t) (point)))
1286 (t (setq viper-use-register nil)
1287 (error viper-InvalidRegister viper-use-register)))
1288 (setq viper-use-register nil)))
1290 (if (eq last-command 'D-command) 'kill-region nil))
1291 (setq lines-deleted (count-lines (point) viper-com-point))
1292 (if (> lines-deleted viper-change-notification-threshold)
1293 (message "Deleted %d lines" lines-deleted))
1294 (kill-region (mark t) (point))
1295 (if (eq m-com 'viper-line) (setq this-command 'D-command)))
1296 (back-to-indentation)))
1299 (defun viper-exec-yank (m-com com)
1300 (or (and (markerp viper-com-point) (marker-position viper-com-point))
1301 (set-marker viper-com-point (point) (current-buffer)))
1303 (if viper-use-register
1305 (cond ((viper-valid-register viper-use-register '(letter digit))
1307 viper-use-register viper-com-point (point) nil))
1308 ((viper-valid-register viper-use-register '(Letter))
1309 (viper-append-to-register
1310 (downcase viper-use-register) viper-com-point (point)))
1311 (t (setq viper-use-register nil)
1312 (error viper-InvalidRegister viper-use-register)))
1313 (setq viper-use-register nil)))
1314 (setq last-command nil)
1315 (copy-region-as-kill viper-com-point (point))
1316 (setq chars-saved (abs (- (point) viper-com-point)))
1317 (if (> chars-saved viper-change-notification-threshold)
1318 (message "Saved %d characters" chars-saved))
1319 (goto-char viper-com-point)))
1322 (defun viper-exec-Yank (m-com com)
1324 (set-mark viper-com-point)
1325 (viper-enlarge-region (mark t) (point))
1327 (if viper-use-register
1329 (cond ((viper-valid-register viper-use-register '(letter digit))
1331 viper-use-register (mark t) (point) nil))
1332 ((viper-valid-register viper-use-register '(Letter))
1333 (viper-append-to-register
1334 (downcase viper-use-register) (mark t) (point)))
1335 (t (setq viper-use-register nil)
1336 (error viper-InvalidRegister viper-use-register)))
1337 (setq viper-use-register nil)))
1338 (setq last-command nil)
1339 (copy-region-as-kill (mark t) (point))
1340 (setq lines-saved (count-lines (mark t) (point)))
1341 (if (> lines-saved viper-change-notification-threshold)
1342 (message "Saved %d lines" lines-saved))))
1343 (viper-deactivate-mark)
1344 (goto-char viper-com-point))
1346 (defun viper-exec-bang (m-com com)
1348 (set-mark viper-com-point)
1349 (viper-enlarge-region (mark t) (point))
1350 (exchange-point-and-mark)
1351 (shell-command-on-region
1354 (setq viper-last-shell-com
1355 (viper-read-string-with-history
1358 'viper-shell-history
1359 (car viper-shell-history)
1361 viper-last-shell-com)
1364 (defun viper-exec-equals (m-com com)
1366 (set-mark viper-com-point)
1367 (viper-enlarge-region (mark t) (point))
1368 (if (> (mark t) (point)) (exchange-point-and-mark))
1369 (indent-region (mark t) (point) nil)))
1371 (defun viper-exec-shift (m-com com)
1373 (set-mark viper-com-point)
1374 (viper-enlarge-region (mark t) (point))
1375 (if (> (mark t) (point)) (exchange-point-and-mark))
1376 (indent-rigidly (mark t) (point)
1379 (- viper-shift-width))))
1380 ;; return point to where it was before shift
1381 (goto-char viper-com-point))
1383 ;; this is needed because some commands fake com by setting it to ?r, which
1384 ;; denotes repeated insert command.
1385 (defsubst viper-exec-dummy (m-com com)
1388 (defun viper-exec-buffer-search (m-com com)
1389 (setq viper-s-string (buffer-substring (point) viper-com-point))
1390 (setq viper-s-forward t)
1391 (setq viper-search-history (cons viper-s-string viper-search-history))
1392 (setq viper-intermediate-command 'viper-exec-buffer-search)
1393 (viper-search viper-s-string viper-s-forward 1))
1395 (defvar viper-exec-array (make-vector 128 nil))
1397 ;; Using a dispatch array allows adding functions like buffer search
1398 ;; without affecting other functions. Buffer search can now be bound
1399 ;; to any character.
1401 (aset viper-exec-array ?c 'viper-exec-change)
1402 (aset viper-exec-array ?C 'viper-exec-Change)
1403 (aset viper-exec-array ?d 'viper-exec-delete)
1404 (aset viper-exec-array ?D 'viper-exec-Delete)
1405 (aset viper-exec-array ?y 'viper-exec-yank)
1406 (aset viper-exec-array ?Y 'viper-exec-Yank)
1407 (aset viper-exec-array ?r 'viper-exec-dummy)
1408 (aset viper-exec-array ?! 'viper-exec-bang)
1409 (aset viper-exec-array ?< 'viper-exec-shift)
1410 (aset viper-exec-array ?> 'viper-exec-shift)
1411 (aset viper-exec-array ?= 'viper-exec-equals)
1415 ;; This function is called by various movement commands to execute a
1416 ;; destructive command on the region specified by the movement command. For
1417 ;; instance, if the user types cw, then the command viper-forward-word will
1418 ;; call viper-execute-com to execute viper-exec-change, which eventually will
1419 ;; call viper-change to invoke the replace mode on the region.
1421 ;; The var viper-d-com is set to (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS)
1422 ;; via a call to viper-set-destructive-command, for later use by viper-repeat.
1423 (defun viper-execute-com (m-com val com)
1424 (let ((reg viper-use-register))
1425 ;; this is the special command `#'
1427 (viper-special-prefix-com (- com 128))
1428 (let ((fn (aref viper-exec-array (if (< com 0) (- com) com))))
1430 (error "%c: %s" com viper-InvalidViCommand)
1431 (funcall fn m-com com))))
1432 (if (viper-dotable-command-p com)
1433 (viper-set-destructive-command
1435 (if (memq com (list ?c ?C ?!)) (- com) com)
1440 (defun viper-repeat (arg)
1441 "Re-execute last destructive command.
1442 Use the info in viper-d-com, which has the form
1443 \(com val ch reg inserted-text command-keys\),
1444 where `com' is the command to be re-executed, `val' is the
1445 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1446 if it exists, it is the name of the register for `com'.
1447 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
1449 (let ((save-point (point)) ; save point before repeating prev cmd
1450 ;; Pass along that we are repeating a destructive command
1451 ;; This tells viper-set-destructive-command not to update
1452 ;; viper-command-ring
1453 (viper-intermediate-command 'viper-repeat))
1454 (if (eq last-command 'viper-undo)
1455 ;; if the last command was viper-undo, then undo-more
1457 ;; otherwise execute the command stored in viper-d-com. if arg is
1458 ;; non-nil its prefix value is used as new prefix value for the command.
1459 (let ((m-com (car viper-d-com))
1460 (val (viper-P-val arg))
1461 (com (nth 2 viper-d-com))
1462 (reg (nth 3 viper-d-com)))
1463 (if (null val) (setq val (nth 1 viper-d-com)))
1464 (if (null m-com) (error "No previous command to repeat."))
1465 (setq viper-use-register reg)
1466 (if (nth 4 viper-d-com) ; text inserted by command
1467 (setq viper-last-insertion (nth 4 viper-d-com)
1468 viper-d-char (nth 4 viper-d-com)))
1469 (funcall m-com (cons val com))
1470 (cond ((and (< save-point (point)) viper-keep-point-on-repeat)
1471 (goto-char save-point)) ; go back to before repeat.
1472 ((and (< save-point (point)) viper-ex-style-editing)
1473 (or (bolp) (backward-char 1))))
1474 (if (and (eolp) (not (bolp)))
1477 (viper-adjust-undo) ; take care of undo
1478 ;; If the prev cmd was rotating the command ring, this means that `.' has
1479 ;; just executed a command from that ring. So, push it on the ring again.
1480 ;; If we are just executing previous command , then don't push viper-d-com
1481 ;; because viper-d-com is not fully constructed in this case (its keys and
1482 ;; the inserted text may be nil). Besides, in this case, the command
1483 ;; executed by `.' is already on the ring.
1484 (if (eq last-command 'viper-display-current-destructive-command)
1485 (viper-push-onto-ring viper-d-com 'viper-command-ring))
1486 (viper-deactivate-mark)
1489 (defun viper-repeat-from-history ()
1490 "Repeat a destructive command from history.
1491 Doesn't change viper-command-ring in any way, so `.' will work as before
1492 executing this command.
1493 This command is supposed to be bound to a two-character Vi macro where
1494 the second character is a digit 0 to 9. The digit indicates which
1495 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1496 invokes the command before that, etc."
1498 (let* ((viper-intermediate-command 'repeating-display-destructive-command)
1499 (idx (cond (viper-this-kbd-macro
1501 (symbol-name (elt viper-this-kbd-macro 1))))
1504 (viper-d-com viper-d-com))
1506 (or (and (numberp num) (<= 0 num) (<= num 9))
1511 "`viper-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1513 (setq viper-d-com (viper-special-ring-rotate1 viper-command-ring -1))
1514 (setq num (1- num)))
1517 (viper-special-ring-rotate1 viper-command-ring 1)
1518 (setq num (1+ num)))
1522 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1523 ;; The chars that can follow `#' are determined by viper-hash-command-p
1524 (defun viper-special-prefix-com (char)
1526 (downcase-region (min viper-com-point (point))
1527 (max viper-com-point (point))))
1529 (upcase-region (min viper-com-point (point))
1530 (max viper-com-point (point))))
1532 (push-mark viper-com-point t)
1533 (viper-global-execute))
1535 (push-mark viper-com-point t)
1536 (viper-quote-region))
1537 ((= char ?s) (funcall viper-spell-function viper-com-point (point)))
1538 (t (error "#%c: %s" char viper-InvalidViCommand))))
1543 (defun viper-undo ()
1544 "Undo previous change."
1547 (let ((modified (buffer-modified-p))
1548 (before-undo-pt (point-marker))
1549 (after-change-functions after-change-functions)
1550 undo-beg-posn undo-end-posn)
1552 ;; no need to remove this hook, since this var has scope inside a let.
1553 (add-hook 'after-change-functions
1554 '(lambda (beg end len)
1555 (setq undo-beg-posn beg
1556 undo-end-posn (or end beg))))
1560 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1561 undo-end-posn (or undo-end-posn undo-beg-posn))
1563 (goto-char undo-beg-posn)
1565 (if (and viper-keep-point-on-undo
1566 (pos-visible-in-window-p before-undo-pt))
1568 (push-mark (point-marker) t)
1569 (viper-sit-for-short 300)
1570 (goto-char undo-end-posn)
1571 (viper-sit-for-short 300)
1572 (if (and (> (viper-chars-in-region undo-beg-posn before-undo-pt) 1)
1573 (> (viper-chars-in-region undo-end-posn before-undo-pt) 1))
1574 (goto-char before-undo-pt)
1575 (goto-char undo-beg-posn)))
1576 (push-mark before-undo-pt t))
1577 (if (and (eolp) (not (bolp))) (backward-char 1))
1578 (if (not modified) (set-buffer-modified-p t)))
1579 (setq this-command 'viper-undo))
1581 ;; Continue undoing previous changes.
1582 (defun viper-undo-more ()
1583 (message "undo more!")
1587 (message "No further undo information in this buffer")))
1588 (if (and (eolp) (not (bolp))) (backward-char 1))
1589 (setq this-command 'viper-undo))
1591 ;; The following two functions are used to set up undo properly.
1592 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1593 ;; they are undone all at once.
1594 (defun viper-adjust-undo ()
1595 (if viper-undo-needs-adjustment
1596 (let ((inhibit-quit t)
1598 (setq viper-undo-needs-adjustment nil)
1599 (if (listp buffer-undo-list)
1600 (if (setq tmp (memq viper-buffer-undo-list-mark buffer-undo-list))
1602 (setq tmp2 (cdr tmp)) ; the part after mark
1604 ;; cut tail from buffer-undo-list temporarily by direct
1605 ;; manipulation with pointers in buffer-undo-list
1608 (setq buffer-undo-list (delq nil buffer-undo-list))
1609 (setq buffer-undo-list
1610 (delq viper-buffer-undo-list-mark buffer-undo-list))
1611 ;; restore tail of buffer-undo-list
1612 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1613 (setq buffer-undo-list (delq nil buffer-undo-list)))))
1617 (defun viper-set-complex-command-for-undo ()
1618 (if (listp buffer-undo-list)
1619 (if (not viper-undo-needs-adjustment)
1620 (let ((inhibit-quit t))
1621 (setq buffer-undo-list
1622 (cons viper-buffer-undo-list-mark buffer-undo-list))
1623 (setq viper-undo-needs-adjustment t)))))
1628 (defun viper-display-current-destructive-command ()
1629 (let ((text (nth 4 viper-d-com))
1630 (keys (nth 5 viper-d-com))
1633 (setq this-command 'viper-display-current-destructive-command)
1635 (message " `.' runs %s%s"
1636 (concat "`" (viper-array-to-string keys) "'")
1637 (viper-abbreviate-string
1640 (cond ((characterp text) (char-to-string text))
1641 ((stringp text) text)
1646 " inserting `" "'" " ......."))
1650 ;; don't change viper-d-com if it was viper-repeat command invoked with `.'
1651 ;; or in some other way (non-interactively).
1652 (defun viper-set-destructive-command (list)
1653 (or (eq viper-intermediate-command 'viper-repeat)
1655 (setq viper-d-com list)
1656 (setcar (nthcdr 5 viper-d-com)
1657 (viper-array-to-string (if (arrayp viper-this-command-keys)
1658 viper-this-command-keys
1659 (this-command-keys))))
1660 (viper-push-onto-ring viper-d-com 'viper-command-ring)))
1661 (setq viper-this-command-keys nil))
1663 (defun viper-prev-destructive-command (next)
1664 "Find previous destructive command in the history of destructive commands.
1665 With prefix argument, find next destructive command."
1667 (let (cmd viper-intermediate-command)
1668 (if (eq last-command 'viper-display-current-destructive-command)
1669 ;; repeated search through command history
1670 (setq viper-intermediate-command
1671 'repeating-display-destructive-command)
1672 ;; first search through command history--set temp ring
1673 (setq viper-temp-command-ring (copy-list viper-command-ring)))
1675 (viper-special-ring-rotate1 viper-temp-command-ring 1)
1676 (viper-special-ring-rotate1 viper-temp-command-ring -1)))
1679 (setq viper-d-com cmd))
1680 (viper-display-current-destructive-command)))
1682 (defun viper-next-destructive-command ()
1683 "Find next destructive command in the history of destructive commands."
1685 (viper-prev-destructive-command 'next))
1687 (defun viper-insert-prev-from-insertion-ring (arg)
1688 "Cycle through insertion ring in the direction of older insertions.
1689 Undoes previous insertion and inserts new.
1690 With prefix argument, cycles in the direction of newer elements.
1691 In minibuffer, this command executes whatever the invocation key is bound
1692 to in the global map, instead of cycling through the insertion ring."
1694 (let (viper-intermediate-command)
1695 (if (eq last-command 'viper-insert-from-insertion-ring)
1696 (progn ; repeated search through insertion history
1697 (setq viper-intermediate-command 'repeating-insertion-from-ring)
1698 (if (eq viper-current-state 'replace-state)
1700 (if viper-last-inserted-string-from-insertion-ring
1701 (backward-delete-char
1702 (length viper-last-inserted-string-from-insertion-ring))))
1704 ;;first search through insertion history
1705 (setq viper-temp-insertion-ring (copy-list viper-insertion-ring)))
1706 (setq this-command 'viper-insert-from-insertion-ring)
1707 ;; so that things will be undone properly
1708 (setq buffer-undo-list (cons nil buffer-undo-list))
1709 (setq viper-last-inserted-string-from-insertion-ring
1710 (viper-special-ring-rotate1 viper-temp-insertion-ring (if arg 1 -1)))
1712 ;; this change of viper-intermediate-command must come after
1713 ;; viper-special-ring-rotate1, so that the ring will rotate, but before the
1715 (setq viper-intermediate-command nil)
1716 (if viper-last-inserted-string-from-insertion-ring
1717 (insert viper-last-inserted-string-from-insertion-ring))
1720 (defun viper-insert-next-from-insertion-ring ()
1721 "Cycle through insertion ring in the direction of older insertions.
1722 Undo previous insertion and inserts new."
1724 (viper-insert-prev-from-insertion-ring 'next))
1727 ;; some region utilities
1729 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1730 (defun viper-add-newline-at-eob-if-necessary ()
1733 ;; make sure all lines end with newline, unless in the minibuffer or
1734 ;; when requested otherwise (require-final-newline is nil)
1737 require-final-newline
1738 (not (viper-is-in-minibuffer))
1739 (not buffer-read-only))
1742 (defun viper-yank-defun ()
1744 (copy-region-as-kill (point) (mark t)))
1746 ;; Enlarge region between BEG and END.
1747 (defun viper-enlarge-region (beg end)
1748 (or beg (setq beg end)) ; if beg is nil, set to end
1749 (or end (setq end beg)) ; if end is nil, set to beg
1752 (progn (goto-char beg) (set-mark end))
1756 (exchange-point-and-mark)
1757 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1758 (if (not (eobp)) (beginning-of-line))
1759 (if (> beg end) (exchange-point-and-mark)))
1762 ;; Quote region by each line with a user supplied string.
1763 (defun viper-quote-region ()
1764 (let ((quote-str viper-quote-string)
1765 (donot-change-dafault t))
1767 (viper-read-string-with-history
1770 'viper-quote-region-history
1771 (cond ((string-match "tex.*-mode" (symbol-name major-mode)) "%%")
1772 ((string-match "java.*-mode" (symbol-name major-mode)) "//")
1773 ((string-match "perl.*-mode" (symbol-name major-mode)) "#")
1774 ((string-match "lisp.*-mode" (symbol-name major-mode)) ";;")
1775 ((memq major-mode '(c-mode cc-mode c++-mode)) "//")
1776 ((memq major-mode '(sh-mode shell-mode)) "#")
1777 (t (setq donot-change-dafault nil)
1779 (or donot-change-dafault
1780 (setq viper-quote-string quote-str))
1781 (viper-enlarge-region (point) (mark t))
1782 (if (> (point) (mark t)) (exchange-point-and-mark))
1786 (while (and (< (point) (mark t)) (bolp))
1791 ;; Tells whether BEG is on the same line as END.
1792 ;; If one of the args is nil, it'll return nil.
1793 (defun viper-same-line (beg end)
1794 (let ((selective-display nil)
1797 (if (and beg end (> beg end))
1802 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
1805 ;; This 'if' is needed because Emacs treats the next empty line
1806 ;; as part of the previous line.
1807 (if (= (viper-line-pos 'start) end)
1809 (<= (+ incr (count-lines beg end)) 1))))
1813 ;; Check if the string ends with a newline.
1814 (defun viper-end-with-a-newline-p (string)
1815 (or (string= string "")
1816 (= (viper-seq-last-elt string) ?\n)))
1818 (defun viper-tmp-insert-at-eob (msg)
1819 (let ((savemax (point-max)))
1823 (goto-char savemax) (delete-region (point) (point-max))
1828 ;;; Minibuffer business
1830 (defsubst viper-set-minibuffer-style ()
1831 (add-hook 'minibuffer-setup-hook 'viper-minibuffer-setup-sentinel))
1834 (defun viper-minibuffer-setup-sentinel ()
1835 (let ((hook (if viper-vi-style-in-minibuffer
1836 'viper-change-state-to-insert
1837 'viper-change-state-to-emacs)))
1841 ;; Interpret last event in the local map first; if fails, use exit-minibuffer.
1842 ;; Run viper-minibuffer-exit-hook before exiting.
1843 (defun viper-exit-minibuffer ()
1844 "Exit minibuffer Viper way."
1847 (setq command (local-key-binding (char-to-string last-command-char)))
1848 (run-hooks 'viper-minibuffer-exit-hook)
1850 (command-execute command)
1851 (exit-minibuffer))))
1854 (defcustom viper-smart-suffix-list
1855 '("" "tex" "c" "cc" "C" "el" "java" "html" "htm" "pl" "flr" "P" "p")
1856 "*List of suffixes that Viper tries to append to filenames ending with a `.'.
1857 This is useful when you the current directory contains files with the same
1858 prefix and many different suffixes. Usually, only one of the suffixes
1859 represents an editable file. However, file completion will stop at the `.'
1860 The smart suffix feature lets you hit RET in such a case, and Viper will
1861 select the appropriate suffix.
1863 Suffixes are tried in the order given and the first suffix for which a
1864 corresponding file exists is selected. If no file exists for any of the
1865 suffixes, the user is asked to confirm.
1867 To turn this feature off, set this variable to nil."
1868 :type '(repeat string)
1872 ;; Try to add a suitable suffix to files whose name ends with a `.'
1873 ;; Useful when the user hits RET on a non-completed file name.
1874 ;; Used as a minibuffer exit hook in read-file-name
1875 (defun viper-file-add-suffix ()
1877 (len (length viper-smart-suffix-list))
1878 (file (buffer-string))
1880 (goto-char (point-max))
1881 (if (and viper-smart-suffix-list (string-match "\\.$" file))
1883 (while (and (not found) (< count len))
1884 (setq suff (nth count viper-smart-suffix-list)
1887 (format "%s%s" (substitute-in-file-name file) suff))
1894 (viper-tmp-insert-at-eob " [Please complete file name]")
1896 (while (not (memq cmd
1897 '(exit-minibuffer viper-exit-minibuffer)))
1899 (key-binding (setq key (read-key-sequence nil))))
1900 (cond ((eq cmd 'self-insert-command)
1902 (insert (events-to-keys key))
1904 ((memq cmd '(exit-minibuffer viper-exit-minibuffer))
1906 (t (command-execute cmd)))
1911 (defun viper-minibuffer-trim-tail ()
1912 "Delete junk at the end of the first line of the minibuffer input.
1913 Remove this function from `viper-minibuffer-exit-hook', if this causes
1915 (if (viper-is-in-minibuffer)
1917 (goto-char (point-min))
1919 (delete-region (point) (point-max)))))
1922 ;;; Reading string with history
1924 (defun viper-read-string-with-history (prompt &optional initial
1925 history-var default keymap
1927 ;; Read string, prompting with PROMPT and inserting the INITIAL
1928 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
1929 ;; input is an empty string.
1930 ;; Default value is displayed until the user types something in the
1932 ;; KEYMAP is used, if given, instead of minibuffer-local-map.
1933 ;; INIT-MESSAGE is the message temporarily displayed after entering the
1935 (let ((minibuffer-setup-hook
1937 (if (stringp init-message)
1938 (viper-tmp-insert-at-eob init-message))
1939 (if (stringp initial)
1941 ;; don't wait if we have unread events or in kbd macro
1942 (or unread-command-events
1947 (viper-minibuffer-setup-sentinel)))
1952 (setq keymap (or keymap minibuffer-local-map)
1953 initial (or initial "")
1954 temp-msg (if default
1955 (format "(default: %s) " default)
1958 (setq viper-incomplete-ex-cmd nil)
1959 (setq val (read-from-minibuffer prompt
1960 (concat temp-msg initial val padding)
1961 keymap nil history-var))
1962 (setq minibuffer-setup-hook nil
1963 padding (viper-array-to-string (this-command-keys))
1965 ;; the following tries to be smart about what to put in history
1966 (if (not (string= val (car (eval history-var))))
1967 (set history-var (cons val (eval history-var))))
1968 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
1969 (string= (nth 0 (eval history-var)) ""))
1970 (set history-var (cdr (eval history-var))))
1971 ;; If the user enters nothing but the prev cmd wasn't viper-ex,
1972 ;; viper-command-argument, or `! shell-command', this probably means
1973 ;; that the user typed something then erased. Return "" in this case, not
1974 ;; the default---the default is too confusing in this case.
1975 (cond ((and (string= val "")
1976 (not (string= prompt "!")) ; was a `! shell-command'
1977 (not (memq last-command
1979 viper-command-argument
1983 ((string= val "") (or default ""))
1989 ;; insertion commands
1991 ;; Called when state changes from Insert Vi command mode.
1992 ;; Repeats the insertion command if Insert state was entered with prefix
1994 (defun viper-repeat-insert-command ()
1995 (let ((i-com (car viper-d-com))
1996 (val (nth 1 viper-d-com))
1997 (char (nth 2 viper-d-com)))
1998 (if (and val (> val 1)) ; first check that val is non-nil
2000 (setq viper-d-com (list i-com (1- val) ?r nil nil nil))
2002 (setq viper-d-com (list i-com val char nil nil nil))
2005 (defun viper-insert (arg)
2006 "Insert before point."
2008 (viper-set-complex-command-for-undo)
2009 (let ((val (viper-p-val arg))
2010 (com (viper-getcom arg)))
2011 (viper-set-destructive-command (list 'viper-insert val ?r nil nil nil))
2013 (viper-loop val (viper-yank-last-insertion))
2014 (viper-change-state-to-insert))))
2016 (defun viper-append (arg)
2017 "Append after point."
2019 (viper-set-complex-command-for-undo)
2020 (let ((val (viper-p-val arg))
2021 (com (viper-getcom arg)))
2022 (viper-set-destructive-command (list 'viper-append val ?r nil nil nil))
2023 (if (not (eolp)) (forward-char))
2025 (viper-loop val (viper-yank-last-insertion))
2026 (viper-change-state-to-insert))))
2028 (defun viper-Append (arg)
2029 "Append at end of line."
2031 (viper-set-complex-command-for-undo)
2032 (let ((val (viper-p-val arg))
2033 (com (viper-getcom arg)))
2034 (viper-set-destructive-command (list 'viper-Append val ?r nil nil nil))
2037 (viper-loop val (viper-yank-last-insertion))
2038 (viper-change-state-to-insert))))
2040 (defun viper-Insert (arg)
2041 "Insert before first non-white."
2043 (viper-set-complex-command-for-undo)
2044 (let ((val (viper-p-val arg))
2045 (com (viper-getcom arg)))
2046 (viper-set-destructive-command (list 'viper-Insert val ?r nil nil nil))
2047 (back-to-indentation)
2049 (viper-loop val (viper-yank-last-insertion))
2050 (viper-change-state-to-insert))))
2052 (defun viper-open-line (arg)
2055 (viper-set-complex-command-for-undo)
2056 (let ((val (viper-p-val arg))
2057 (com (viper-getcom arg)))
2058 (viper-set-destructive-command (list 'viper-open-line val ?r nil nil nil))
2059 (let ((col (current-indentation)))
2064 (if viper-auto-indent
2067 (if viper-electric-mode
2068 (indent-according-to-mode)
2071 (viper-yank-last-insertion))
2074 (if viper-auto-indent
2077 (if viper-electric-mode
2078 (indent-according-to-mode)
2080 (viper-change-state-to-insert)))))
2082 (defun viper-Open-line (arg)
2085 (viper-set-complex-command-for-undo)
2086 (let ((val (viper-p-val arg))
2087 (com (viper-getcom arg)))
2088 (viper-set-destructive-command (list 'viper-Open-line val ?r nil nil nil))
2089 (let ((col (current-indentation)))
2094 (if viper-auto-indent
2097 (if viper-electric-mode
2098 (indent-according-to-mode)
2101 (viper-yank-last-insertion))
2104 (if viper-auto-indent
2107 (if viper-electric-mode
2108 (indent-according-to-mode)
2111 (viper-change-state-to-insert)))))
2113 (defun viper-open-line-at-point (arg)
2114 "Open line at point."
2116 (viper-set-complex-command-for-undo)
2117 (let ((val (viper-p-val arg))
2118 (com (viper-getcom arg)))
2119 (viper-set-destructive-command
2120 (list 'viper-open-line-at-point val ?r nil nil nil))
2124 (viper-yank-last-insertion))
2126 (viper-change-state-to-insert))))
2128 (defun viper-substitute (arg)
2129 "Substitute characters."
2131 (let ((val (viper-p-val arg))
2132 (com (viper-getcom arg)))
2136 (viper-change-subr (mark t) (point))
2137 (viper-change (mark t) (point)))
2138 (viper-set-destructive-command (list 'viper-substitute val ?r nil nil nil))
2141 ;; Command bound to S
2142 (defun viper-substitute-line (arg)
2145 (viper-set-complex-command-for-undo)
2146 (viper-line (cons arg ?C)))
2148 ;; Prepare for replace
2149 (defun viper-start-replace ()
2150 (setq viper-began-as-replace t
2151 viper-sitting-in-replace t
2152 viper-replace-chars-to-delete 0)
2154 'viper-after-change-functions 'viper-replace-mode-spy-after t 'local)
2156 'viper-before-change-functions 'viper-replace-mode-spy-before t 'local)
2157 ;; this will get added repeatedly, but no harm
2158 (add-hook 'after-change-functions 'viper-after-change-sentinel t)
2159 (add-hook 'before-change-functions 'viper-before-change-sentinel t)
2160 (viper-move-marker-locally
2161 'viper-last-posn-in-replace-region (viper-replace-start))
2163 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel
2166 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2167 ;; guard against a smartie who switched from R-replace to normal replace
2169 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2170 (if overwrite-mode (overwrite-mode nil))
2174 (defun viper-replace-mode-spy-before (beg end)
2175 (setq viper-replace-region-chars-deleted (viper-chars-in-region beg end))
2178 ;; Invoked as an after-change-function to calculate how many chars have to be
2179 ;; deleted. This function may be called several times within a single command,
2180 ;; if this command performs several separate buffer changes. Therefore, if
2181 ;; adds up the number of chars inserted and subtracts the number of chars
2183 (defun viper-replace-mode-spy-after (beg end length)
2184 (if (memq viper-intermediate-command
2185 '(dabbrev-expand hippie-expand repeating-insertion-from-ring))
2186 ;; Take special care of text insertion from insertion ring inside
2187 ;; replacement overlays.
2189 (setq viper-replace-chars-to-delete 0)
2190 (viper-move-marker-locally
2191 'viper-last-posn-in-replace-region (point)))
2193 (let* ((real-end (min end (viper-replace-end)))
2194 (column-shift (- (save-excursion (goto-char real-end)
2196 (save-excursion (goto-char beg)
2201 (setq chars-deleted viper-replace-region-chars-deleted))
2202 (setq viper-replace-region-chars-deleted 0)
2203 (setq viper-replace-chars-to-delete
2204 (+ viper-replace-chars-to-delete
2206 ;; if column shift is bigger, due to a TAB insertion, take
2207 ;; column-shift instead of the number of inserted chars
2208 (max (viper-chars-in-region beg real-end)
2209 ;; This test accounts for Chinese/Japanese/... chars,
2210 ;; which occupy 2 columns instead of one. If we use
2211 ;; column-shift here, we may delete two chars instead of
2212 ;; one when the user types one Chinese character.
2213 ;; Deleting two would be OK, if they were European chars,
2214 ;; but it is not OK if they are Chinese chars.
2215 ;; Since it is hard to
2216 ;; figure out which characters are being deleted in any
2217 ;; given region, we decided to treat Eastern and European
2218 ;; characters equally, even though Eastern chars may
2219 ;; occupy more columns.
2220 (if (memq this-command '(self-insert-command
2221 quoted-insert viper-insert-tab))
2224 ;; the number of deleted chars
2227 (viper-move-marker-locally
2228 'viper-last-posn-in-replace-region
2229 (max (if (> end (viper-replace-end)) (viper-replace-end) end)
2230 (or (marker-position viper-last-posn-in-replace-region)
2231 (viper-replace-start))
2236 ;; Make sure we don't delete more than needed.
2237 ;; This is executed at viper-last-posn-in-replace-region
2238 (defsubst viper-trim-replace-chars-to-delete-if-necessary ()
2239 (setq viper-replace-chars-to-delete
2241 (min viper-replace-chars-to-delete
2242 ;; Don't delete more than to the end of repl overlay
2243 (viper-chars-in-region
2244 (viper-replace-end) viper-last-posn-in-replace-region)
2245 ;; point is viper-last-posn-in-replace-region now
2246 ;; So, this limits deletion to the end of line
2247 (viper-chars-in-region (point) (viper-line-pos 'end))
2251 ;; Delete stuff between viper-last-posn-in-replace-region and the end of
2252 ;; viper-replace-overlay-marker, if viper-last-posn-in-replace-region is within
2253 ;; the overlay and current point is before the end of the overlay.
2254 ;; Don't delete anything if current point is past the end of the overlay.
2255 (defun viper-finish-change ()
2257 'viper-after-change-functions 'viper-replace-mode-spy-after 'local)
2259 'viper-before-change-functions 'viper-replace-mode-spy-before 'local)
2261 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2263 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2264 (viper-restore-cursor-color 'after-replace-mode)
2265 (setq viper-sitting-in-replace nil) ; just in case we'll need to know it
2267 (if (and viper-replace-overlay
2268 (viper-pos-within-region viper-last-posn-in-replace-region
2269 (viper-replace-start)
2270 (viper-replace-end))
2271 (< (point) (viper-replace-end)))
2273 viper-last-posn-in-replace-region (viper-replace-end))))
2275 (if (eq viper-current-state 'replace-state)
2276 (viper-downgrade-to-insert))
2277 ;; replace mode ended => nullify viper-last-posn-in-replace-region
2278 (viper-move-marker-locally 'viper-last-posn-in-replace-region nil)
2279 (viper-hide-replace-overlay)
2280 (viper-refresh-mode-line)
2281 (viper-put-string-on-kill-ring viper-last-replace-region)
2284 ;; Make STRING be the first element of the kill ring.
2285 (defun viper-put-string-on-kill-ring (string)
2286 (setq kill-ring (cons string kill-ring))
2287 (if (> (length kill-ring) kill-ring-max)
2288 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2289 (setq kill-ring-yank-pointer kill-ring))
2291 (defun viper-finish-R-mode ()
2293 'viper-post-command-hooks 'viper-R-state-post-command-sentinel 'local)
2295 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel 'local)
2296 (viper-downgrade-to-insert))
2298 (defun viper-start-R-mode ()
2299 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2302 'viper-post-command-hooks 'viper-R-state-post-command-sentinel t 'local)
2304 'viper-pre-command-hooks 'viper-replace-state-pre-command-sentinel t 'local)
2305 ;; guard against a smartie who switched from R-replace to normal replace
2307 'viper-post-command-hooks 'viper-replace-state-post-command-sentinel 'local)
2312 (defun viper-replace-state-exit-cmd ()
2313 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2314 These keys are ESC, RET, and LineFeed"
2316 (if overwrite-mode ; if in replace mode invoked via 'R'
2317 (viper-finish-R-mode)
2318 (viper-finish-change))
2320 (if (eq this-command 'viper-intercept-ESC-key)
2321 (setq com 'viper-exit-insert-state)
2322 (viper-set-unread-command-events last-input-char)
2323 (setq com (key-binding (read-key-sequence nil))))
2325 (condition-case conds
2326 (command-execute com)
2328 (viper-message-conditions conds)))
2330 (viper-hide-replace-overlay))
2333 (defun viper-replace-state-carriage-return ()
2334 "Carriage return in Viper replace state."
2336 ;; If Emacs start supporting overlay maps, as it currently supports
2337 ;; text-property maps, we could do away with viper-replace-minor-mode and
2338 ;; just have keymap attached to replace overlay. Then the "if part" of this
2339 ;; statement can be deleted.
2340 (if (or (< (point) (viper-replace-start))
2341 (> (point) (viper-replace-end)))
2342 (let (viper-replace-minor-mode com)
2343 (viper-set-unread-command-events last-input-char)
2344 (setq com (key-binding (read-key-sequence nil)))
2345 (condition-case conds
2346 (command-execute com)
2348 (viper-message-conditions conds))))
2349 (if (not viper-allow-multiline-replace-regions)
2350 (viper-replace-state-exit-cmd)
2351 (if (viper-same-line (point) (viper-replace-end))
2352 (viper-replace-state-exit-cmd)
2353 ;; delete the rest of line
2354 (delete-region (point) (viper-line-pos 'end))
2357 (if (eobp) (error "Last line in buffer")))
2358 ;; skip to the next line
2360 (back-to-indentation)
2364 ;; This is the function bound to 'R'---unlimited replace.
2365 ;; Similar to Emacs's own overwrite-mode.
2366 (defun viper-overwrite (arg)
2367 "Begin overwrite mode."
2369 (let ((val (viper-p-val arg))
2370 (com (viper-getcom arg)) (len))
2371 (viper-set-destructive-command (list 'viper-overwrite val ?r nil nil nil))
2374 ;; Viper saves inserted text in viper-last-insertion
2375 (setq len (length viper-last-insertion))
2377 (viper-loop val (viper-yank-last-insertion)))
2378 (setq last-command 'viper-overwrite)
2379 (viper-set-complex-command-for-undo)
2380 (viper-set-replace-overlay (point) (viper-line-pos 'end))
2381 (viper-change-state-to-replace)
2387 (defun viper-line (arg)
2388 (let ((val (car arg))
2390 (viper-move-marker-locally 'viper-com-point (point))
2392 (viper-next-line-carefully (1- val)))
2393 ;; the following ensures that dd, cc, D, yy will do the right thing on the
2394 ;; last line of buffer when this line has no \n.
2395 (viper-add-newline-at-eob-if-necessary)
2396 (viper-execute-com 'viper-line val com))
2397 (if (and (eobp) (not (bobp))) (forward-line -1))
2400 (defun viper-yank-line (arg)
2401 "Yank ARG lines (in Vi's sense)."
2403 (let ((val (viper-p-val arg)))
2404 (viper-line (cons val ?Y))))
2409 (defun viper-region (arg)
2410 "Execute command on a region."
2412 (let ((val (viper-P-val arg))
2413 (com (viper-getcom arg)))
2414 (viper-move-marker-locally 'viper-com-point (point))
2415 (exchange-point-and-mark)
2416 (viper-execute-com 'viper-region val com)))
2418 (defun viper-Region (arg)
2419 "Execute command on a Region."
2421 (let ((val (viper-P-val arg))
2422 (com (viper-getCom arg)))
2423 (viper-move-marker-locally 'viper-com-point (point))
2424 (exchange-point-and-mark)
2425 (viper-execute-com 'viper-Region val com)))
2427 (defun viper-replace-char (arg)
2428 "Replace the following ARG chars by the character read."
2430 (if (and (eolp) (bolp)) (error "No character to replace here"))
2431 (let ((val (viper-p-val arg))
2432 (com (viper-getcom arg)))
2433 (viper-replace-char-subr com val)
2434 (if (and (eolp) (not (bolp))) (forward-char 1))
2435 (setq viper-this-command-keys
2436 (format "%sr" (if (integerp arg) arg "")))
2437 (viper-set-destructive-command
2438 (list 'viper-replace-char val ?r nil viper-d-char nil))
2441 (defun viper-replace-char-subr (com arg)
2443 (setq char (if (equal com ?r)
2446 (let (inhibit-quit) ; preserve consistency of undo-list and iso-accents
2447 (if (and viper-automatic-iso-accents (memq char '(?' ?\" ?^ ?~)))
2448 ;; get European characters
2450 (viper-set-iso-accents-mode t)
2451 (viper-set-unread-command-events char)
2452 (setq char (aref (read-key-sequence nil) 0))
2453 (viper-set-iso-accents-mode nil)))
2454 (viper-set-complex-command-for-undo)
2455 (if (eq char ?\C-m) (setq char ?\n))
2456 (if (and viper-special-input-method (fboundp 'quail-start-translation))
2457 ;; get Intl. characters
2459 (viper-set-input-method t)
2460 (setq last-command-event
2462 (if viper-xemacs-p (character-to-event char) char)))
2468 (quail-start-translation 1)
2469 (quail-start-translation)))
2471 ;; quail translation failed
2472 (if (and (not (stringp quail-current-str))
2473 (not (viper-characterp quail-current-str)))
2478 (viper-set-input-method nil)
2479 (error "Composing character failed, changes undone")))
2480 ;; quail translation seems ok
2482 ;;(setq char quail-current-str))
2483 (setq char (viper-char-at-pos 'backward)))
2484 (setq viper-d-char char)
2485 (viper-loop (1- (if (> arg 0) arg (- arg)))
2488 (viper-set-input-method nil))
2490 (setq viper-d-char char)
2491 (viper-loop (if (> arg 0) arg (- arg))
2494 (backward-char arg))))
2497 ;; basic cursor movement. j, k, l, h commands.
2499 (defun viper-forward-char (arg)
2500 "Move point right ARG characters (left if ARG negative).
2501 On reaching end of line, stop and signal error."
2503 (viper-leave-region-active)
2504 (let ((val (viper-p-val arg))
2505 (com (viper-getcom arg)))
2506 (if com (viper-move-marker-locally 'viper-com-point (point)))
2507 (if viper-ex-style-motion
2509 ;; the boundary condition check gets weird here because
2510 ;; forward-char may be the parameter of a delete, and 'dl' works
2511 ;; just like 'x' for the last char on a line, so we have to allow
2512 ;; the forward motion before the 'viper-execute-com', but, of
2513 ;; course, 'dl' doesn't work on an empty line, so we have to
2514 ;; catch that condition before 'viper-execute-com'
2515 (if (and (eolp) (bolp)) (error "") (forward-char val))
2516 (if com (viper-execute-com 'viper-forward-char val com))
2517 (if (eolp) (progn (backward-char 1) (error ""))))
2519 (if com (viper-execute-com 'viper-forward-char val com)))))
2521 (defun viper-backward-char (arg)
2522 "Move point left ARG characters (right if ARG negative).
2523 On reaching beginning of line, stop and signal error."
2525 (viper-leave-region-active)
2526 (let ((val (viper-p-val arg))
2527 (com (viper-getcom arg)))
2528 (if com (viper-move-marker-locally 'viper-com-point (point)))
2529 (if viper-ex-style-motion
2531 (if (bolp) (error "") (backward-char val))
2532 (if com (viper-execute-com 'viper-backward-char val com)))
2534 (if com (viper-execute-com 'viper-backward-char val com)))))
2536 ;; Like forward-char, but doesn't move at end of buffer.
2537 ;; Returns distance traveled
2538 ;; (positive or 0, if arg positive; negative if arg negative).
2539 (defun viper-forward-char-carefully (&optional arg)
2540 (setq arg (or arg 1))
2545 (if (< (point) pt) ; arg was negative
2546 (- (viper-chars-in-region pt (point)))
2547 (viper-chars-in-region pt (point)))))
2549 ;; Like backward-char, but doesn't move at beg of buffer.
2550 ;; Returns distance traveled
2551 ;; (negative or 0, if arg positive; positive if arg negative).
2552 (defun viper-backward-char-carefully (&optional arg)
2553 (setq arg (or arg 1))
2558 (if (> (point) pt) ; arg was negative
2559 (viper-chars-in-region pt (point))
2560 (- (viper-chars-in-region pt (point))))))
2562 (defun viper-next-line-carefully (arg)
2571 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators for
2572 ;; word movement. When executed with a destructive command, \n is usually left
2573 ;; untouched for the last word. Viper uses syntax table to determine what is a
2574 ;; word and what is a separator. However, \n is always a separator. Also, if
2575 ;; viper-syntax-preference is 'vi, then `_' is part of the word.
2578 (defun viper-skip-separators (forward)
2581 (viper-skip-all-separators-forward 'within-line)
2582 (if (looking-at "\n")
2585 (viper-skip-all-separators-forward 'within-line))))
2586 ;; check for eob and white space before it. move off of eob
2587 (if (and (eobp) (save-excursion
2588 (viper-backward-char-carefully)
2589 (viper-looking-at-separator)))
2590 (viper-backward-char-carefully))
2591 (viper-skip-all-separators-backward 'within-line)
2592 (viper-backward-char-carefully)
2593 (if (looking-at "\n")
2594 (viper-skip-all-separators-backward 'within-line)
2595 (or (bobp) (forward-char)))))
2597 (defun viper-forward-word-kernel (val)
2599 (cond ((viper-looking-at-alpha)
2600 (viper-skip-alpha-forward "_")
2601 (viper-skip-separators t))
2602 ((viper-looking-at-separator)
2603 (viper-skip-separators t))
2604 ((not (viper-looking-at-alphasep))
2605 (viper-skip-nonalphasep-forward)
2606 (viper-skip-separators t)))
2607 (setq val (1- val))))
2609 ;; first skip non-newline separators backward, then skip \n. Then, if TWICE is
2610 ;; non-nil, skip non-\n back again, but don't overshoot the limit LIM.
2611 (defun viper-separator-skipback-special (twice lim)
2612 (let ((prev-char (viper-char-at-pos 'backward))
2613 (saved-point (point)))
2614 ;; skip non-newline separators backward
2615 (while (and (not (memq prev-char '(nil \n)))
2617 ;; must be non-newline separator
2618 (if (eq viper-syntax-preference 'strict-vi)
2619 (memq prev-char '(?\ ?\t))
2620 (memq (char-syntax prev-char) '(?\ ?-))))
2621 (viper-backward-char-carefully)
2622 (setq prev-char (viper-char-at-pos 'backward)))
2624 (if (and (< lim (point)) (eq prev-char ?\n))
2626 ;; If we skipped to the next word and the prefix of this line doesn't
2627 ;; consist of separators preceded by a newline, then don't skip backwards
2629 (goto-char saved-point))
2630 (setq prev-char (viper-char-at-pos 'backward))
2632 ;; skip again, but make sure we don't overshoot the limit
2634 (while (and (not (memq prev-char '(nil \n)))
2636 ;; must be non-newline separator
2637 (if (eq viper-syntax-preference 'strict-vi)
2638 (memq prev-char '(?\ ?\t))
2639 (memq (char-syntax prev-char) '(?\ ?-))))
2640 (viper-backward-char-carefully)
2641 (setq prev-char (viper-char-at-pos 'backward))))
2644 (viper-forward-char-carefully))
2648 (defun viper-forward-word (arg)
2651 (viper-leave-region-active)
2652 (let ((val (viper-p-val arg))
2653 (com (viper-getcom arg)))
2654 (if com (viper-move-marker-locally 'viper-com-point (point)))
2655 (viper-forward-word-kernel val)
2657 (cond ((memq com (list ?c (- ?c)))
2658 (viper-separator-skipback-special 'twice viper-com-point))
2659 ;; Yank words including the whitespace, but not newline
2660 ((memq com (list ?y (- ?y)))
2661 (viper-separator-skipback-special nil viper-com-point))
2662 ((viper-dotable-command-p com)
2663 (viper-separator-skipback-special nil viper-com-point)))
2664 (viper-execute-com 'viper-forward-word val com)))))
2667 (defun viper-forward-Word (arg)
2668 "Forward word delimited by white characters."
2670 (viper-leave-region-active)
2671 (let ((val (viper-p-val arg))
2672 (com (viper-getcom arg)))
2673 (if com (viper-move-marker-locally 'viper-com-point (point)))
2675 (viper-skip-nonseparators 'forward)
2676 (viper-skip-separators t))
2678 (cond ((memq com (list ?c (- ?c)))
2679 (viper-separator-skipback-special 'twice viper-com-point))
2680 ;; Yank words including the whitespace, but not newline
2681 ((memq com (list ?y (- ?y)))
2682 (viper-separator-skipback-special nil viper-com-point))
2683 ((viper-dotable-command-p com)
2684 (viper-separator-skipback-special nil viper-com-point)))
2685 (viper-execute-com 'viper-forward-Word val com)))))
2688 ;; this is a bit different from Vi, but Vi's end of word
2689 ;; makes no sense whatsoever
2690 (defun viper-end-of-word-kernel ()
2691 (if (viper-end-of-word-p) (forward-char))
2692 (if (viper-looking-at-separator)
2693 (viper-skip-all-separators-forward))
2695 (cond ((viper-looking-at-alpha) (viper-skip-alpha-forward "_"))
2696 ((not (viper-looking-at-alphasep)) (viper-skip-nonalphasep-forward)))
2697 (viper-backward-char-carefully))
2699 (defun viper-end-of-word-p ()
2702 (cond ((viper-looking-at-alpha)
2704 (not (viper-looking-at-alpha)))
2705 ((not (viper-looking-at-alphasep))
2707 (viper-looking-at-alphasep))))))
2710 (defun viper-end-of-word (arg &optional careful)
2711 "Move point to end of current word."
2713 (viper-leave-region-active)
2714 (let ((val (viper-p-val arg))
2715 (com (viper-getcom arg)))
2716 (if com (viper-move-marker-locally 'viper-com-point (point)))
2717 (viper-loop val (viper-end-of-word-kernel))
2721 (viper-execute-com 'viper-end-of-word val com)))))
2723 (defun viper-end-of-Word (arg)
2724 "Forward to end of word delimited by white character."
2726 (viper-leave-region-active)
2727 (let ((val (viper-p-val arg))
2728 (com (viper-getcom arg)))
2729 (if com (viper-move-marker-locally 'viper-com-point (point)))
2731 (viper-end-of-word-kernel)
2732 (viper-skip-nonseparators 'forward)
2737 (viper-execute-com 'viper-end-of-Word val com)))))
2739 (defun viper-backward-word-kernel (val)
2741 (viper-backward-char-carefully)
2742 (cond ((viper-looking-at-alpha)
2743 (viper-skip-alpha-backward "_"))
2744 ((viper-looking-at-separator)
2746 (viper-skip-separators nil)
2747 (viper-backward-char-carefully)
2748 (cond ((viper-looking-at-alpha)
2749 (viper-skip-alpha-backward "_"))
2750 ((not (viper-looking-at-alphasep))
2751 (viper-skip-nonalphasep-backward))
2752 ((bobp)) ; could still be at separator, but at beg of buffer
2753 (t (forward-char))))
2754 ((not (viper-looking-at-alphasep))
2755 (viper-skip-nonalphasep-backward)))
2756 (setq val (1- val))))
2758 (defun viper-backward-word (arg)
2761 (viper-leave-region-active)
2762 (let ((val (viper-p-val arg))
2763 (com (viper-getcom arg)))
2766 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2768 (viper-move-marker-locally 'viper-com-point (point))
2769 (if i (forward-char))))
2770 (viper-backward-word-kernel val)
2771 (if com (viper-execute-com 'viper-backward-word val com))))
2773 (defun viper-backward-Word (arg)
2774 "Backward word delimited by white character."
2776 (viper-leave-region-active)
2777 (let ((val (viper-p-val arg))
2778 (com (viper-getcom arg)))
2781 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2783 (viper-move-marker-locally 'viper-com-point (point))
2784 (if i (forward-char))))
2786 (viper-skip-separators nil) ; nil means backward here
2787 (viper-skip-nonseparators 'backward))
2788 (if com (viper-execute-com 'viper-backward-Word val com))))
2794 (defun viper-beginning-of-line (arg)
2795 "Go to beginning of line."
2797 (viper-leave-region-active)
2798 (let ((val (viper-p-val arg))
2799 (com (viper-getcom arg)))
2800 (if com (viper-move-marker-locally 'viper-com-point (point)))
2801 (beginning-of-line val)
2802 (if com (viper-execute-com 'viper-beginning-of-line val com))))
2804 (defun viper-bol-and-skip-white (arg)
2805 "Beginning of line at first non-white character."
2807 (viper-leave-region-active)
2808 (let ((val (viper-p-val arg))
2809 (com (viper-getcom arg)))
2810 (if com (viper-move-marker-locally 'viper-com-point (point)))
2811 (forward-to-indentation (1- val))
2812 (if com (viper-execute-com 'viper-bol-and-skip-white val com))))
2814 (defun viper-goto-eol (arg)
2815 "Go to end of line."
2817 (viper-leave-region-active)
2818 (let ((val (viper-p-val arg))
2819 (com (viper-getcom arg)))
2820 (if com (viper-move-marker-locally 'viper-com-point (point)))
2822 (if com (viper-execute-com 'viper-goto-eol val com))
2823 (if viper-ex-style-motion
2824 (if (and (eolp) (not (bolp))
2825 ;; a fix for viper-change-to-eol
2826 (not (equal viper-current-state 'insert-state)))
2831 (defun viper-goto-col (arg)
2832 "Go to ARG's column."
2834 (viper-leave-region-active)
2835 (let ((val (viper-p-val arg))
2836 (com (viper-getcom arg))
2839 (viper-chars-in-region
2840 (viper-line-pos 'start) (viper-line-pos 'end)))
2841 (if com (viper-move-marker-locally 'viper-com-point (point)))
2843 (forward-char (1- (min line-len val)))
2844 (while (> (current-column) (1- val))
2846 (if com (viper-execute-com 'viper-goto-col val com))
2849 (if (> val (current-column)) (error "")))
2853 (defun viper-next-line (arg)
2856 (viper-leave-region-active)
2857 (let ((val (viper-p-val arg))
2858 (com (viper-getCom arg)))
2859 (if com (viper-move-marker-locally 'viper-com-point (point)))
2861 (if viper-ex-style-motion
2862 (if (and (eolp) (not (bolp))) (backward-char 1)))
2863 (setq this-command 'next-line)
2864 (if com (viper-execute-com 'viper-next-line val com))))
2866 (defun viper-next-line-at-bol (arg)
2867 "Next line at beginning of line."
2869 (viper-leave-region-active)
2872 (if (eobp) (error "Last line in buffer")))
2873 (let ((val (viper-p-val arg))
2874 (com (viper-getCom arg)))
2875 (if com (viper-move-marker-locally 'viper-com-point (point)))
2877 (back-to-indentation)
2878 (if com (viper-execute-com 'viper-next-line-at-bol val com))))
2880 (defun viper-previous-line (arg)
2881 "Go to previous line."
2883 (viper-leave-region-active)
2884 (let ((val (viper-p-val arg))
2885 (com (viper-getCom arg)))
2886 (if com (viper-move-marker-locally 'viper-com-point (point)))
2888 (if viper-ex-style-motion
2889 (if (and (eolp) (not (bolp))) (backward-char 1)))
2890 (setq this-command 'previous-line)
2891 (if com (viper-execute-com 'viper-previous-line val com))))
2894 (defun viper-previous-line-at-bol (arg)
2895 "Previous line at beginning of line."
2897 (viper-leave-region-active)
2900 (if (bobp) (error "First line in buffer")))
2901 (let ((val (viper-p-val arg))
2902 (com (viper-getCom arg)))
2903 (if com (viper-move-marker-locally 'viper-com-point (point)))
2904 (forward-line (- val))
2905 (back-to-indentation)
2906 (if com (viper-execute-com 'viper-previous-line val com))))
2908 (defun viper-change-to-eol (arg)
2909 "Change to end of line."
2911 (viper-goto-eol (cons arg ?c)))
2913 (defun viper-kill-line (arg)
2916 (viper-goto-eol (cons arg ?d)))
2918 (defun viper-erase-line (arg)
2921 (viper-beginning-of-line (cons arg ?d)))
2926 (defun viper-goto-line (arg)
2927 "Go to ARG's line. Without ARG go to end of buffer."
2929 (let ((val (viper-P-val arg))
2930 (com (viper-getCom arg)))
2931 (viper-move-marker-locally 'viper-com-point (point))
2932 (viper-deactivate-mark)
2935 (goto-char (point-max))
2936 (goto-char (point-min))
2937 (forward-line (1- val)))
2939 ;; positioning is done twice: before and after command execution
2940 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2941 (back-to-indentation)
2943 (if com (viper-execute-com 'viper-goto-line val com))
2945 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2946 (back-to-indentation)
2949 ;; Find ARG's occurrence of CHAR on the current line.
2950 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
2951 ;; adjust point after search.
2952 (defun viper-find-char (arg char forward offset)
2953 (or (char-or-string-p char) (error ""))
2954 (let ((arg (if forward arg (- arg)))
2955 (cmd (if (eq viper-intermediate-command 'viper-repeat)
2957 (viper-array-to-string (this-command-keys))))
2958 point region-beg region-end)
2961 (if (> arg 0) ; forward
2963 (setq region-beg (point))
2964 (if viper-allow-multiline-replace-regions
2965 (viper-forward-paragraph 1)
2967 (setq region-end (point)))
2968 (setq region-end (point))
2969 (if viper-allow-multiline-replace-regions
2970 (viper-backward-paragraph 1)
2971 (beginning-of-line))
2972 (setq region-beg (point)))
2973 (if (or (and (< arg 0)
2974 (< (- region-end region-beg)
2975 (if viper-allow-multiline-replace-regions
2979 (< (- region-end region-beg)
2980 (if viper-allow-multiline-replace-regions
2983 (error "Command `%s': At %s of %s"
2985 (if (> arg 0) "end" "beginning")
2986 (if viper-allow-multiline-replace-regions
2987 "paragraph" "line")))
2988 (narrow-to-region region-beg region-end)
2989 ;; if arg > 0, point is forwarded before search.
2990 (if (> arg 0) (goto-char (1+ (point-min)))
2991 (goto-char (point-max)))
2992 (if (let ((case-fold-search nil))
2993 (search-forward (char-to-string char) nil 0 arg))
2994 (setq point (point))
2995 (error "Command `%s': `%c' not found" cmd char))))
2998 (backward-char (if offset 2 1))
2999 (forward-char (if offset 1 0)))))
3001 (defun viper-find-char-forward (arg)
3002 "Find char on the line.
3003 If called interactively read the char to find from the terminal, and if
3004 called from viper-repeat, the char last used is used. This behaviour is
3005 controlled by the sign of prefix numeric value."
3007 (let ((val (viper-p-val arg))
3008 (com (viper-getcom arg))
3009 (cmd-representation (nth 5 viper-d-com)))
3011 ;; this means that the function was called interactively
3012 (setq viper-f-char (read-char)
3015 ;; viper-repeat --- set viper-F-char from command-keys
3016 (setq viper-F-char (if (stringp cmd-representation)
3017 (viper-seq-last-elt cmd-representation)
3019 viper-f-char viper-F-char)
3021 (if com (viper-move-marker-locally 'viper-com-point (point)))
3023 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t nil)
3027 (setq viper-F-char viper-f-char) ; set new viper-F-char
3029 (viper-execute-com 'viper-find-char-forward val com)))))
3031 (defun viper-goto-char-forward (arg)
3032 "Go up to char ARG forward on line."
3034 (let ((val (viper-p-val arg))
3035 (com (viper-getcom arg))
3036 (cmd-representation (nth 5 viper-d-com)))
3038 ;; this means that the function was called interactively
3039 (setq viper-f-char (read-char)
3042 ;; viper-repeat --- set viper-F-char from command-keys
3043 (setq viper-F-char (if (stringp cmd-representation)
3044 (viper-seq-last-elt cmd-representation)
3046 viper-f-char viper-F-char)
3048 (if com (viper-move-marker-locally 'viper-com-point (point)))
3050 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) t t)
3054 (setq viper-F-char viper-f-char) ; set new viper-F-char
3056 (viper-execute-com 'viper-goto-char-forward val com)))))
3058 (defun viper-find-char-backward (arg)
3059 "Find char ARG on line backward."
3061 (let ((val (viper-p-val arg))
3062 (com (viper-getcom arg))
3063 (cmd-representation (nth 5 viper-d-com)))
3065 ;; this means that the function was called interactively
3066 (setq viper-f-char (read-char)
3069 ;; viper-repeat --- set viper-F-char from command-keys
3070 (setq viper-F-char (if (stringp cmd-representation)
3071 (viper-seq-last-elt cmd-representation)
3073 viper-f-char viper-F-char)
3075 (if com (viper-move-marker-locally 'viper-com-point (point)))
3077 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil nil)
3081 (setq viper-F-char viper-f-char) ; set new viper-F-char
3082 (viper-execute-com 'viper-find-char-backward val com)))))
3084 (defun viper-goto-char-backward (arg)
3085 "Go up to char ARG backward on line."
3087 (let ((val (viper-p-val arg))
3088 (com (viper-getcom arg))
3089 (cmd-representation (nth 5 viper-d-com)))
3091 ;; this means that the function was called interactively
3092 (setq viper-f-char (read-char)
3095 ;; viper-repeat --- set viper-F-char from command-keys
3096 (setq viper-F-char (if (stringp cmd-representation)
3097 (viper-seq-last-elt cmd-representation)
3099 viper-f-char viper-F-char)
3101 (if com (viper-move-marker-locally 'viper-com-point (point)))
3103 val (if (> (viper-p-val arg) 0) viper-f-char viper-F-char) nil t)
3107 (setq viper-F-char viper-f-char) ; set new viper-F-char
3108 (viper-execute-com 'viper-goto-char-backward val com)))))
3110 (defun viper-repeat-find (arg)
3111 "Repeat previous find command."
3113 (let ((val (viper-p-val arg))
3114 (com (viper-getcom arg)))
3115 (viper-deactivate-mark)
3116 (if com (viper-move-marker-locally 'viper-com-point (point)))
3117 (viper-find-char val viper-f-char viper-f-forward viper-f-offset)
3120 (if viper-f-forward (forward-char))
3121 (viper-execute-com 'viper-repeat-find val com)))))
3123 (defun viper-repeat-find-opposite (arg)
3124 "Repeat previous find command in the opposite direction."
3126 (let ((val (viper-p-val arg))
3127 (com (viper-getcom arg)))
3128 (viper-deactivate-mark)
3129 (if com (viper-move-marker-locally 'viper-com-point (point)))
3130 (viper-find-char val viper-f-char (not viper-f-forward) viper-f-offset)
3133 (if viper-f-forward (forward-char))
3134 (viper-execute-com 'viper-repeat-find-opposite val com)))))
3137 ;; window scrolling etc.
3139 (defun viper-window-top (arg)
3140 "Go to home window line."
3142 (let ((val (viper-p-val arg))
3143 (com (viper-getCom arg)))
3144 (viper-leave-region-active)
3145 (if com (viper-move-marker-locally 'viper-com-point (point)))
3147 (move-to-window-line (1- val))
3149 ;; positioning is done twice: before and after command execution
3150 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3151 (back-to-indentation)
3153 (if com (viper-execute-com 'viper-window-top val com))
3155 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3156 (back-to-indentation)
3159 (defun viper-window-middle (arg)
3160 "Go to middle window line."
3162 (let ((val (viper-p-val arg))
3163 (com (viper-getCom arg)))
3164 (viper-leave-region-active)
3165 (if com (viper-move-marker-locally 'viper-com-point (point)))
3167 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
3169 ;; positioning is done twice: before and after command execution
3170 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3171 (back-to-indentation)
3173 (if com (viper-execute-com 'viper-window-middle val com))
3175 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3176 (back-to-indentation)
3179 (defun viper-window-bottom (arg)
3180 "Go to last window line."
3182 (let ((val (viper-p-val arg))
3183 (com (viper-getCom arg)))
3184 (viper-leave-region-active)
3185 (if com (viper-move-marker-locally 'viper-com-point (point)))
3187 (move-to-window-line (- val))
3189 ;; positioning is done twice: before and after command execution
3190 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3191 (back-to-indentation)
3193 (if com (viper-execute-com 'viper-window-bottom val com))
3195 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
3196 (back-to-indentation)
3199 (defun viper-line-to-top (arg)
3200 "Put current line on the home line."
3202 (recenter (1- arg)))
3204 (defun viper-line-to-middle (arg)
3205 "Put current line on the middle line."
3207 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
3209 (defun viper-line-to-bottom (arg)
3210 "Put current line on the last line."
3212 (recenter (- (window-height) (1+ arg))))
3214 ;; If point is within viper-search-scroll-threshold of window top or bottom,
3215 ;; scroll up or down 1/7 of window height, depending on whether we are at the
3216 ;; bottom or at the top of the window. This function is called by viper-search
3217 ;; (which is called from viper-search-forward/backward/next). If the value of
3218 ;; viper-search-scroll-threshold is negative - don't scroll.
3219 (defun viper-adjust-window ()
3220 (let ((win-height (if viper-emacs-p
3221 (1- (window-height)) ; adjust for modeline
3222 (window-displayed-height)))
3224 at-top-p at-bottom-p
3225 min-scroll direction)
3227 (move-to-window-line 0) ; top
3229 (<= (count-lines pt (point))
3230 viper-search-scroll-threshold))
3231 (move-to-window-line -1) ; bottom
3233 (<= (count-lines pt (point)) viper-search-scroll-threshold))
3235 (cond (at-top-p (setq min-scroll (1- viper-search-scroll-threshold)
3237 (at-bottom-p (setq min-scroll (1+ viper-search-scroll-threshold)
3241 (* (max min-scroll (/ win-height 7)) direction)))
3246 ;; must correct this to only match ( to ) etc. On the other hand
3247 ;; it is good that paren match gets confused, because that way you
3248 ;; catch _all_ imbalances.
3250 (defun viper-paren-match (arg)
3251 "Go to the matching parenthesis."
3253 (viper-leave-region-active)
3254 (let ((com (viper-getcom arg))
3255 (parse-sexp-ignore-comments viper-parse-sexp-ignore-comments)
3258 (if (or (> arg 99) (< arg 1))
3259 (error "Prefix must be between 1 and 99")
3261 (if (> (point-max) 80000)
3262 (* (/ (point-max) 100) arg)
3263 (/ (* (point-max) arg) 100)))
3264 (back-to-indentation))
3265 (let (beg-lim end-lim)
3266 (if (and (eolp) (not (bolp))) (forward-char -1))
3267 (if (not (looking-at "[][(){}]"))
3268 (setq anchor-point (point)))
3271 (setq beg-lim (point))
3273 (setq end-lim (point)))
3274 (cond ((re-search-forward "[][(){}]" end-lim t)
3276 ((re-search-backward "[][(){}]" beg-lim t))
3278 (error "No matching character on line"))))
3279 (cond ((looking-at "[\(\[{]")
3280 (if com (viper-move-marker-locally 'viper-com-point (point)))
3283 (viper-execute-com 'viper-paren-match nil com)
3288 (viper-move-marker-locally 'viper-com-point anchor-point)
3290 (viper-execute-com 'viper-paren-match nil com)
3292 ((looking-at "[])}]")
3294 (if com (viper-move-marker-locally 'viper-com-point (point)))
3296 (if com (viper-execute-com 'viper-paren-match nil com)))
3299 (defun viper-toggle-parse-sexp-ignore-comments ()
3301 (setq viper-parse-sexp-ignore-comments
3302 (not viper-parse-sexp-ignore-comments))
3304 "From now on, `%%' will %signore parentheses inside comment fields"
3305 (if viper-parse-sexp-ignore-comments "" "NOT "))))
3308 ;; sentence, paragraph and heading
3310 (defun viper-forward-sentence (arg)
3313 (or (eq last-command this-command)
3315 (let ((val (viper-p-val arg))
3316 (com (viper-getcom arg)))
3317 (if com (viper-move-marker-locally 'viper-com-point (point)))
3318 (forward-sentence val)
3319 (if com (viper-execute-com 'viper-forward-sentence nil com))))
3321 (defun viper-backward-sentence (arg)
3322 "Backward sentence."
3324 (or (eq last-command this-command)
3326 (let ((val (viper-p-val arg))
3327 (com (viper-getcom arg)))
3328 (if com (viper-move-marker-locally 'viper-com-point (point)))
3329 (backward-sentence val)
3330 (if com (viper-execute-com 'viper-backward-sentence nil com))))
3332 (defun viper-forward-paragraph (arg)
3333 "Forward paragraph."
3335 (or (eq last-command this-command)
3337 (let ((val (viper-p-val arg))
3338 ;; if you want d} operate on whole lines, change viper-getcom to
3339 ;; viper-getCom below
3340 (com (viper-getcom arg)))
3341 (if com (viper-move-marker-locally 'viper-com-point (point)))
3342 (forward-paragraph val)
3346 (viper-execute-com 'viper-forward-paragraph nil com)))))
3348 (defun viper-backward-paragraph (arg)
3349 "Backward paragraph."
3351 (or (eq last-command this-command)
3353 (let ((val (viper-p-val arg))
3354 ;; if you want d{ operate on whole lines, change viper-getcom to
3355 ;; viper-getCom below
3356 (com (viper-getcom arg)))
3357 (if com (viper-move-marker-locally 'viper-com-point (point)))
3358 (backward-paragraph val)
3362 (viper-execute-com 'viper-backward-paragraph nil com)
3363 (backward-char 1)))))
3365 ;; should be mode-specific
3366 (defun viper-prev-heading (arg)
3368 (let ((val (viper-p-val arg))
3369 (com (viper-getCom arg)))
3370 (if com (viper-move-marker-locally 'viper-com-point (point)))
3371 (re-search-backward viper-heading-start nil t val)
3372 (goto-char (match-beginning 0))
3373 (if com (viper-execute-com 'viper-prev-heading nil com))))
3375 (defun viper-heading-end (arg)
3377 (let ((val (viper-p-val arg))
3378 (com (viper-getCom arg)))
3379 (if com (viper-move-marker-locally 'viper-com-point (point)))
3380 (re-search-forward viper-heading-end nil t val)
3381 (goto-char (match-beginning 0))
3382 (if com (viper-execute-com 'viper-heading-end nil com))))
3384 (defun viper-next-heading (arg)
3386 (let ((val (viper-p-val arg))
3387 (com (viper-getCom arg)))
3388 (if com (viper-move-marker-locally 'viper-com-point (point)))
3390 (re-search-forward viper-heading-start nil t val)
3391 (goto-char (match-beginning 0))
3392 (if com (viper-execute-com 'viper-next-heading nil com))))
3397 (defun viper-scroll-screen (arg)
3398 "Scroll to next screen."
3404 (setq arg (1- arg)))
3407 (setq arg (1+ arg))))
3411 (message "End of buffer")
3412 (goto-char (point-max)))
3413 (message "Beginning of buffer")
3414 (goto-char (point-min))))
3417 (defun viper-scroll-screen-back (arg)
3418 "Scroll to previous screen."
3420 (viper-scroll-screen (- arg)))
3422 (defun viper-scroll-down (arg)
3423 "Pull down half screen."
3427 (scroll-down (/ (window-height) 2))
3430 (message "Beginning of buffer")
3431 (goto-char (point-min)))))
3433 (defun viper-scroll-down-one (arg)
3434 "Scroll up one line."
3438 (defun viper-scroll-up (arg)
3439 "Pull up half screen."
3443 (scroll-up (/ (window-height) 2))
3446 (message "End of buffer")
3447 (goto-char (point-max)))))
3449 (defun viper-scroll-up-one (arg)
3450 "Scroll down one line."
3457 (defun viper-if-string (prompt)
3458 (if (memq viper-intermediate-command
3459 '(viper-command-argument viper-digit-argument viper-repeat))
3460 (setq viper-this-command-keys (this-command-keys)))
3461 (let ((s (viper-read-string-with-history
3464 'viper-search-history
3465 (car viper-search-history))))
3466 (if (not (string= s ""))
3467 (setq viper-s-string s))))
3470 (defun viper-toggle-search-style (arg)
3471 "Toggle the value of viper-case-fold-search/viper-re-search.
3472 Without prefix argument, will ask which search style to toggle. With prefix
3473 arg 1,toggles viper-case-fold-search; with arg 2 toggles viper-re-search.
3475 Although this function is bound to \\[viper-toggle-search-style], the most
3476 convenient way to use it is to bind `//' to the macro
3477 `1 M-x viper-toggle-search-style' and `///' to
3478 `2 M-x viper-toggle-search-style'. In this way, hitting `//' quickly will
3479 toggle case-fold-search and hitting `/' three times witth toggle regexp
3480 search. Macros are more convenient in this case because they don't affect
3481 the Emacs binding of `/'."
3484 (cond ((or (eq arg 1)
3486 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3487 (if viper-case-fold-search
3488 "case-insensitive" "case-sensitive")
3489 (if viper-case-fold-search
3491 "case-insensitive")))))
3492 (setq viper-case-fold-search (null viper-case-fold-search))
3493 (if viper-case-fold-search
3494 (setq msg "Search becomes case-insensitive")
3495 (setq msg "Search becomes case-sensitive")))
3498 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3500 "regexp-search" "vanilla-search")
3503 "regexp-search")))))
3504 (setq viper-re-search (null viper-re-search))
3506 (setq msg "Search becomes regexp-style")
3507 (setq msg "Search becomes vanilla-style")))
3509 (setq msg "Search style remains unchanged")))
3512 (defun viper-set-searchstyle-toggling-macros (unset)
3513 "Set the macros for toggling the search style in Viper's vi-state.
3514 The macro that toggles case sensitivity is bound to `//', and the one that
3515 toggles regexp search is bound to `///'.
3516 With a prefix argument, this function unsets the macros. "
3521 ;; toggle case sensitivity in search
3522 (viper-record-kbd-macro
3524 [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]
3526 ;; toggle regexp/vanila search
3527 (viper-record-kbd-macro
3529 [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]
3533 "// and /// now toggle case-sensitivity and regexp search")))
3534 (viper-unrecord-kbd-macro "//" 'vi-state)
3536 (viper-unrecord-kbd-macro "///" 'vi-state))))
3539 (defun viper-set-parsing-style-toggling-macro (unset)
3540 "Set `%%%' to be a macro that toggles whether comment fields should be parsed for matching parentheses.
3541 This is used in conjunction with the `%' command.
3543 With a prefix argument, unsets the macro."
3548 ;; Make %%% toggle parsing comments for matching parentheses
3549 (viper-record-kbd-macro
3551 [(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]
3555 "%%%%%% now toggles whether comments should be parsed for matching parentheses")))
3556 (viper-unrecord-kbd-macro "%%%" 'vi-state))))
3559 (defun viper-set-emacs-state-searchstyle-macros (unset &optional arg-majormode)
3560 "Set the macros for toggling the search style in Viper's emacs-state.
3561 The macro that toggles case sensitivity is bound to `//', and the one that
3562 toggles regexp search is bound to `///'.
3563 With a prefix argument, this function unsets the macros.
3564 If the optional prefix argument is non-nil and specifies a valid major mode,
3565 this sets the macros only in the macros in that major mode. Otherwise,
3566 the macros are set in the current major mode.
3567 \(When unsetting the macros, the second argument has no effect.\)"
3572 ;; toggle case sensitivity in search
3573 (viper-record-kbd-macro
3575 [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]
3576 (or arg-majormode major-mode))
3577 ;; toggle regexp/vanila search
3578 (viper-record-kbd-macro
3580 [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]
3581 (or arg-majormode major-mode))
3584 "// and /// now toggle case-sensitivity and regexp search.")))
3585 (viper-unrecord-kbd-macro "//" 'emacs-state)
3587 (viper-unrecord-kbd-macro "///" 'emacs-state))))
3590 (defun viper-search-forward (arg)
3591 "Search a string forward.
3592 ARG is used to find the ARG's occurrence of the string.
3593 Null string will repeat previous search."
3595 (let ((val (viper-P-val arg))
3596 (com (viper-getcom arg))
3597 (old-str viper-s-string))
3598 (setq viper-s-forward t)
3599 (viper-if-string "/")
3600 ;; this is not used at present, but may be used later
3601 (if (or (not (equal old-str viper-s-string))
3602 (not (markerp viper-local-search-start-marker))
3603 (not (marker-buffer viper-local-search-start-marker)))
3604 (setq viper-local-search-start-marker (point-marker)))
3605 (viper-search viper-s-string t val)
3608 (viper-move-marker-locally 'viper-com-point (mark t))
3609 (viper-execute-com 'viper-search-next val com)))))
3611 (defun viper-search-backward (arg)
3612 "Search a string backward.
3613 ARG is used to find the ARG's occurrence of the string.
3614 Null string will repeat previous search."
3616 (let ((val (viper-P-val arg))
3617 (com (viper-getcom arg))
3618 (old-str viper-s-string))
3619 (setq viper-s-forward nil)
3620 (viper-if-string "?")
3621 ;; this is not used at present, but may be used later
3622 (if (or (not (equal old-str viper-s-string))
3623 (not (markerp viper-local-search-start-marker))
3624 (not (marker-buffer viper-local-search-start-marker)))
3625 (setq viper-local-search-start-marker (point-marker)))
3626 (viper-search viper-s-string nil val)
3629 (viper-move-marker-locally 'viper-com-point (mark t))
3630 (viper-execute-com 'viper-search-next val com)))))
3633 ;; Search for COUNT's occurrence of STRING.
3634 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3635 ;; INIT-POINT is the position where search is to start.
3637 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3638 (defun viper-search (string forward arg
3639 &optional no-offset init-point fail-if-not-found)
3640 (if (not (equal string ""))
3641 (let ((val (viper-p-val arg))
3642 (com (viper-getcom arg))
3643 (offset (not no-offset))
3644 (case-fold-search viper-case-fold-search)
3645 (start-point (or init-point (point))))
3646 (viper-deactivate-mark)
3650 (if offset (viper-forward-char-carefully))
3653 (re-search-forward string nil nil val)
3654 (re-search-backward string))
3655 (search-forward string nil nil val)
3656 (search-backward string))
3657 (if (not (equal start-point (point)))
3658 (push-mark start-point t)))
3660 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3662 (message "Search wrapped around BOTTOM of buffer")
3663 (goto-char (point-min))
3664 (viper-search string forward (cons 1 com) t start-point 'fail)
3665 ;; don't wait in macros
3666 (or executing-kbd-macro
3667 (memq viper-intermediate-command
3669 viper-digit-argument
3670 viper-command-argument))
3672 ;; delete the wrap-around message
3675 (goto-char start-point)
3676 (error "`%s': %s not found"
3678 (if viper-re-search "Pattern" "String"))
3684 (re-search-backward string nil nil val)
3685 (search-backward string nil nil val))
3686 (if (not (equal start-point (point)))
3687 (push-mark start-point t)))
3689 (if (and (not fail-if-not-found) viper-search-wrap-around-t)
3691 (message "Search wrapped around TOP of buffer")
3692 (goto-char (point-max))
3693 (viper-search string forward (cons 1 com) t start-point 'fail)
3694 ;; don't wait in macros
3695 (or executing-kbd-macro
3696 (memq viper-intermediate-command
3698 viper-digit-argument
3699 viper-command-argument))
3701 ;; delete the wrap-around message
3704 (goto-char start-point)
3705 (error "`%s': %s not found"
3707 (if viper-re-search "Pattern" "String"))
3709 ;; pull up or down if at top/bottom of window
3710 (viper-adjust-window)
3711 ;; highlight the result of search
3712 ;; don't wait and don't highlight in macros
3713 (or executing-kbd-macro
3714 (memq viper-intermediate-command
3715 '(viper-repeat viper-digit-argument viper-command-argument))
3716 (viper-flash-search-pattern))
3719 (defun viper-search-next (arg)
3720 "Repeat previous search."
3722 (let ((val (viper-p-val arg))
3723 (com (viper-getcom arg)))
3724 (if (null viper-s-string) (error viper-NoPrevSearch))
3725 (viper-search viper-s-string viper-s-forward arg)
3728 (viper-move-marker-locally 'viper-com-point (mark t))
3729 (viper-execute-com 'viper-search-next val com)))))
3731 (defun viper-search-Next (arg)
3732 "Repeat previous search in the reverse direction."
3734 (let ((val (viper-p-val arg))
3735 (com (viper-getcom arg)))
3736 (if (null viper-s-string) (error viper-NoPrevSearch))
3737 (viper-search viper-s-string (not viper-s-forward) arg)
3740 (viper-move-marker-locally 'viper-com-point (mark t))
3741 (viper-execute-com 'viper-search-Next val com)))))
3744 ;; Search contents of buffer defined by one of Viper's motion commands.
3745 ;; Repeatable via `n' and `N'.
3746 (defun viper-buffer-search-enable (&optional c)
3747 (cond (c (setq viper-buffer-search-char c))
3748 ((null viper-buffer-search-char)
3749 (setq viper-buffer-search-char ?g)))
3750 (define-key viper-vi-basic-map
3751 (cond ((viper-characterp viper-buffer-search-char)
3752 (char-to-string viper-buffer-search-char))
3753 (t (error "viper-buffer-search-char: wrong value type, %s"
3754 viper-buffer-search-char)))
3755 'viper-command-argument)
3756 (aset viper-exec-array viper-buffer-search-char 'viper-exec-buffer-search)
3757 (setq viper-prefix-commands
3758 (cons viper-buffer-search-char viper-prefix-commands)))
3760 ;; This is a Viper wraper for isearch-forward.
3761 (defun viper-isearch-forward (arg)
3762 "Do incremental search forward."
3764 ;; emacs bug workaround
3765 (if (listp arg) (setq arg (car arg)))
3766 (viper-exec-form-in-emacs (list 'isearch-forward arg)))
3768 ;; This is a Viper wraper for isearch-backward."
3769 (defun viper-isearch-backward (arg)
3770 "Do incremental search backward."
3772 ;; emacs bug workaround
3773 (if (listp arg) (setq arg (car arg)))
3774 (viper-exec-form-in-emacs (list 'isearch-backward arg)))
3777 ;; visiting and killing files, buffers
3779 (defun viper-switch-to-buffer ()
3780 "Switch to buffer in the current window."
3782 (let ((other-buffer (other-buffer (current-buffer)))
3785 (funcall viper-read-buffer-function
3786 "Switch to buffer in this window: " other-buffer))
3787 (switch-to-buffer buffer)))
3789 (defun viper-switch-to-buffer-other-window ()
3790 "Switch to buffer in another window."
3792 (let ((other-buffer (other-buffer (current-buffer)))
3795 (funcall viper-read-buffer-function
3796 "Switch to buffer in another window: " other-buffer))
3797 (switch-to-buffer-other-window buffer)))
3799 (defun viper-kill-buffer ()
3802 (let (buffer buffer-name)
3804 (funcall viper-read-buffer-function
3805 (format "Kill buffer \(%s\): "
3806 (buffer-name (current-buffer)))))
3808 (if (null buffer-name)
3810 (get-buffer buffer-name)))
3811 (if (null buffer) (error "`%s': No such buffer" buffer-name))
3812 (if (or (not (buffer-modified-p buffer))
3815 "Buffer `%s' is modified, are you sure you want to kill it? "
3817 (kill-buffer buffer)
3818 (error "Buffer not killed"))))
3824 (defsubst viper-yank (text)
3825 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
3827 (setq this-command 'yank))
3829 (defun viper-put-back (arg)
3830 "Put back after point/below line."
3832 (let ((val (viper-p-val arg))
3833 (text (if viper-use-register
3834 (cond ((viper-valid-register viper-use-register '(digit))
3836 (- viper-use-register ?1) 'do-not-rotate))
3837 ((viper-valid-register viper-use-register)
3838 (get-register (downcase viper-use-register)))
3839 (t (error viper-InvalidRegister viper-use-register)))
3841 sv-point chars-inserted lines-inserted)
3843 (if viper-use-register
3844 (let ((reg viper-use-register))
3845 (setq viper-use-register nil)
3846 (error viper-EmptyRegister reg))
3848 (setq viper-use-register nil)
3849 (if (viper-end-with-a-newline-p text)
3855 (beginning-of-line))
3856 (if (not (eolp)) (viper-forward-char-carefully)))
3857 (set-marker (viper-mark-marker) (point) (current-buffer))
3858 (viper-set-destructive-command
3859 (list 'viper-put-back val nil viper-use-register nil nil))
3860 (setq sv-point (point))
3861 (viper-loop val (viper-yank text))
3862 (setq chars-inserted (abs (- (point) sv-point))
3863 lines-inserted (abs (count-lines (point) sv-point)))
3864 (if (or (> chars-inserted viper-change-notification-threshold)
3865 (> lines-inserted viper-change-notification-threshold))
3866 (message "Inserted %d character(s), %d line(s)"
3867 chars-inserted lines-inserted)))
3868 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3869 ;; newline; it leaves the cursor at the beginning when the text contains
3871 (if (viper-same-line (point) (mark))
3872 (or (= (point) (mark)) (viper-backward-char-carefully))
3873 (exchange-point-and-mark)
3875 (back-to-indentation)))
3876 (viper-deactivate-mark))
3878 (defun viper-Put-back (arg)
3879 "Put back at point/above line."
3881 (let ((val (viper-p-val arg))
3882 (text (if viper-use-register
3883 (cond ((viper-valid-register viper-use-register '(digit))
3885 (- viper-use-register ?1) 'do-not-rotate))
3886 ((viper-valid-register viper-use-register)
3887 (get-register (downcase viper-use-register)))
3888 (t (error viper-InvalidRegister viper-use-register)))
3890 sv-point chars-inserted lines-inserted)
3892 (if viper-use-register
3893 (let ((reg viper-use-register))
3894 (setq viper-use-register nil)
3895 (error viper-EmptyRegister reg))
3897 (setq viper-use-register nil)
3898 (if (viper-end-with-a-newline-p text) (beginning-of-line))
3899 (viper-set-destructive-command
3900 (list 'viper-Put-back val nil viper-use-register nil nil))
3901 (set-marker (viper-mark-marker) (point) (current-buffer))
3902 (setq sv-point (point))
3903 (viper-loop val (viper-yank text))
3904 (setq chars-inserted (abs (- (point) sv-point))
3905 lines-inserted (abs (count-lines (point) sv-point)))
3906 (if (or (> chars-inserted viper-change-notification-threshold)
3907 (> lines-inserted viper-change-notification-threshold))
3908 (message "Inserted %d character(s), %d line(s)"
3909 chars-inserted lines-inserted)))
3910 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3911 ;; newline; it leaves the cursor at the beginning when the text contains
3913 (if (viper-same-line (point) (mark))
3914 (or (= (point) (mark)) (viper-backward-char-carefully))
3915 (exchange-point-and-mark)
3917 (back-to-indentation)))
3918 (viper-deactivate-mark))
3921 ;; Copy region to kill-ring.
3922 ;; If BEG and END do not belong to the same buffer, copy empty region.
3923 (defun viper-copy-region-as-kill (beg end)
3925 (copy-region-as-kill beg end)
3926 (error (copy-region-as-kill beg beg))))
3929 (defun viper-delete-char (arg)
3930 "Delete next character."
3932 (let ((val (viper-p-val arg))
3934 (viper-set-destructive-command
3935 (list 'viper-delete-char val nil nil nil nil))
3936 (if (and viper-ex-style-editing
3937 (> val (viper-chars-in-region (point) (viper-line-pos 'end))))
3938 (setq val (viper-chars-in-region (point) (viper-line-pos 'end))))
3939 (if (and viper-ex-style-motion (eolp))
3940 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
3942 (viper-forward-char-carefully val)
3943 (setq end-del-pos (point)))
3944 (if viper-use-register
3946 (cond ((viper-valid-register viper-use-register '((Letter)))
3947 (viper-append-to-register
3948 (downcase viper-use-register) (point) end-del-pos))
3949 ((viper-valid-register viper-use-register)
3951 viper-use-register (point) end-del-pos nil))
3952 (t (error viper-InvalidRegister viper-use-register)))
3953 (setq viper-use-register nil)))
3956 (if viper-ex-style-motion
3957 (if (and (eolp) (not (bolp))) (backward-char 1)))
3960 (defun viper-delete-backward-char (arg)
3961 "Delete previous character. On reaching beginning of line, stop and beep."
3963 (let ((val (viper-p-val arg))
3965 (viper-set-destructive-command
3966 (list 'viper-delete-backward-char val nil nil nil nil))
3968 viper-ex-style-editing
3969 (> val (viper-chars-in-region (viper-line-pos 'start) (point))))
3970 (setq val (viper-chars-in-region (viper-line-pos 'start) (point))))
3972 (viper-backward-char-carefully val)
3973 (setq end-del-pos (point)))
3974 (if viper-use-register
3976 (cond ((viper-valid-register viper-use-register '(Letter))
3977 (viper-append-to-register
3978 (downcase viper-use-register) end-del-pos (point)))
3979 ((viper-valid-register viper-use-register)
3981 viper-use-register end-del-pos (point) nil))
3982 (t (error viper-InvalidRegister viper-use-register)))
3983 (setq viper-use-register nil)))
3984 (if (and (bolp) viper-ex-style-editing)
3986 (delete-backward-char val t)))
3988 (defun viper-del-backward-char-in-insert ()
3989 "Delete 1 char backwards while in insert mode."
3991 (if (and viper-ex-style-editing (bolp))
3993 (delete-backward-char 1 t)))
3995 (defun viper-del-backward-char-in-replace ()
3996 "Delete one character in replace mode.
3997 If `viper-delete-backwards-in-replace' is t, then DEL key actually deletes
3998 charecters. If it is nil, then the cursor just moves backwards, similarly
3999 to Vi. The variable `viper-ex-style-editing', if t, doesn't let the
4000 cursor move past the beginning of line."
4002 (cond (viper-delete-backwards-in-replace
4004 (delete-backward-char 1 t))
4005 (viper-ex-style-editing
4010 (delete-backward-char 1 t))))
4011 (viper-ex-style-editing
4016 (backward-char 1))))
4022 (defun viper-join-lines (arg)
4023 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
4025 (let ((val (viper-P-val arg)))
4026 (viper-set-destructive-command
4027 (list 'viper-join-lines val nil nil nil nil))
4028 (viper-loop (if (null val) 1 (1- val))
4033 (delete-region (point) (1- (point)))
4035 ;; fixup-whitespace sometimes does not leave space
4036 ;; between objects, so we insert it as in Vi
4037 (or (looking-at " ")
4045 (defun viper-change (beg end)
4046 (if (markerp beg) (setq beg (marker-position beg)))
4047 (if (markerp end) (setq end (marker-position end)))
4048 ;; beg is sometimes (mark t), which may be nil
4049 (or beg (setq beg end))
4051 (viper-set-complex-command-for-undo)
4052 (if viper-use-register
4054 (copy-to-register viper-use-register beg end nil)
4055 (setq viper-use-register nil)))
4056 (viper-set-replace-overlay beg end)
4057 (setq last-command nil) ; separate repl text from prev kills
4059 (if (= (viper-replace-start) (point-max))
4060 (error "End of buffer"))
4062 (setq viper-last-replace-region
4063 (buffer-substring (viper-replace-start)
4064 (viper-replace-end)))
4066 ;; protect against error while inserting "@" and other disasters
4067 ;; (e.g., read-only buff)
4068 (condition-case conds
4069 (if (or viper-allow-multiline-replace-regions
4070 (viper-same-line (viper-replace-start)
4071 (viper-replace-end)))
4073 ;; tabs cause problems in replace, so untabify
4074 (goto-char (viper-replace-end))
4075 (insert-before-markers "@") ; put placeholder after the TAB
4076 (untabify (viper-replace-start) (point))
4077 ;; del @, don't put on kill ring
4078 (delete-backward-char 1)
4080 (viper-set-replace-overlay-glyphs
4081 viper-replace-region-start-delimiter
4082 viper-replace-region-end-delimiter)
4083 ;; this move takes care of the last posn in the overlay, which
4084 ;; has to be shifted because of insert. We can't simply insert
4085 ;; "$" before-markers because then overlay-start will shift the
4086 ;; beginning of the overlay in case we are replacing a single
4087 ;; character. This fixes the bug with `s' and `cl' commands.
4088 (viper-move-replace-overlay (viper-replace-start) (point))
4089 (goto-char (viper-replace-start))
4090 (viper-change-state-to-replace t))
4091 (kill-region (viper-replace-start)
4092 (viper-replace-end))
4093 (viper-hide-replace-overlay)
4094 (viper-change-state-to-insert))
4095 (error ;; make sure that the overlay doesn't stay.
4096 ;; go back to the original point
4097 (goto-char (viper-replace-start))
4098 (viper-hide-replace-overlay)
4099 (viper-message-conditions conds))))
4102 (defun viper-change-subr (beg end)
4103 ;; beg is sometimes (mark t), which may be nil
4104 (or beg (setq beg end))
4105 (if viper-use-register
4107 (copy-to-register viper-use-register beg end nil)
4108 (setq viper-use-register nil)))
4109 (kill-region beg end)
4110 (setq this-command 'viper-change)
4111 (viper-yank-last-insertion))
4113 (defun viper-toggle-case (arg)
4114 "Toggle character case."
4116 (let ((val (viper-p-val arg)) (c))
4117 (viper-set-destructive-command
4118 (list 'viper-toggle-case val nil nil nil nil))
4120 (setq c (following-char))
4122 (if (eq c (upcase c))
4123 (insert-char (downcase c) 1)
4124 (insert-char (upcase c) 1))
4125 (if (eolp) (backward-char 1))
4126 (setq val (1- val)))))
4131 (defun viper-query-replace ()
4133 If a null string is suplied as the string to be replaced,
4134 the query replace mode will toggle between string replace
4135 and regexp replace."
4138 (setq str (viper-read-string-with-history
4139 (if viper-re-query-replace "Query replace regexp: "
4142 'viper-replace1-history
4143 (car viper-replace1-history) ; default
4145 (if (string= str "")
4147 (setq viper-re-query-replace (not viper-re-query-replace))
4148 (message "Query replace mode changed to %s"
4149 (if viper-re-query-replace "regexp replace"
4151 (if viper-re-query-replace
4152 (query-replace-regexp
4154 (viper-read-string-with-history
4155 (format "Query replace regexp `%s' with: " str)
4157 'viper-replace1-history
4158 (car viper-replace1-history) ; default
4162 (viper-read-string-with-history
4163 (format "Query replace `%s' with: " str)
4165 'viper-replace1-history
4166 (car viper-replace1-history) ; default
4172 (defun viper-mark-beginning-of-buffer ()
4173 "Mark beginning of buffer."
4176 (goto-char (point-min))
4177 (exchange-point-and-mark)
4178 (message "Mark set at the beginning of buffer"))
4180 (defun viper-mark-end-of-buffer ()
4181 "Mark end of buffer."
4184 (goto-char (point-max))
4185 (exchange-point-and-mark)
4186 (message "Mark set at the end of buffer"))
4188 (defun viper-mark-point ()
4189 "Set mark at point of buffer."
4191 (let ((char (read-char)))
4192 (cond ((and (<= ?a char) (<= char ?z))
4193 (point-to-register (1+ (- char ?a))))
4194 ((= char ?<) (viper-mark-beginning-of-buffer))
4195 ((= char ?>) (viper-mark-end-of-buffer))
4196 ((= char ?.) (viper-set-mark-if-necessary))
4197 ((= char ?,) (viper-cycle-through-mark-ring))
4198 ((= char ?^) (push-mark viper-saved-mark t t))
4199 ((= char ?D) (mark-defun))
4203 ;; Algorithm: If first invocation of this command save mark on ring, goto
4204 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
4205 ;; making it into the new mark, M1.
4206 ;; Push this mark back and set mark to the original point position, p1.
4207 ;; So, if you hit '' or `` then you can return to p1.
4209 ;; If repeated command, pop top elt from the ring into mark and
4210 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
4211 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
4212 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
4214 (defun viper-cycle-through-mark-ring ()
4215 "Visit previous locations on the mark ring.
4216 One can use `` and '' to temporarily jump 1 step back."
4217 (let* ((sv-pt (point)))
4218 ;; if repeated `m,' command, pop the previously saved mark.
4219 ;; Prev saved mark is actually prev saved point. It is used if the
4220 ;; user types `` or '' and is discarded
4221 ;; from the mark ring by the next `m,' command.
4222 ;; In any case, go to the previous or previously saved mark.
4223 ;; Then push the current mark (popped off the ring) and set current
4224 ;; point to be the mark. Current pt as mark is discarded by the next
4226 (if (eq last-command 'viper-cycle-through-mark-ring)
4228 ;; save current mark if the first iteration
4229 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4231 (push-mark (mark t) t)) )
4233 (set-mark-command 1)
4234 ;; don't duplicate mark on the ring
4235 (setq mark-ring (delete (viper-mark-marker) mark-ring))
4237 (viper-deactivate-mark)
4238 (setq this-command 'viper-cycle-through-mark-ring)
4242 (defun viper-goto-mark (arg)
4245 (let ((char (read-char))
4246 (com (viper-getcom arg)))
4247 (viper-goto-mark-subr char com nil)))
4249 (defun viper-goto-mark-and-skip-white (arg)
4250 "Go to mark and skip to first non-white character on line."
4252 (let ((char (read-char))
4253 (com (viper-getCom arg)))
4254 (viper-goto-mark-subr char com t)))
4256 (defun viper-goto-mark-subr (char com skip-white)
4259 (error "Empty buffer")
4261 (cond ((viper-valid-register char '(letter))
4262 (let* ((buff (current-buffer))
4263 (reg (1+ (- char ?a)))
4264 (text-marker (get-register reg)))
4265 (if com (viper-move-marker-locally 'viper-com-point (point)))
4266 (if (not (viper-valid-marker text-marker))
4267 (error viper-EmptyTextmarker char))
4268 (if (and (viper-same-line (point) viper-last-jump)
4269 (= (point) viper-last-jump-ignore))
4270 (push-mark viper-last-jump t)
4271 (push-mark nil t)) ; no msg
4272 (viper-register-to-point reg)
4273 (setq viper-last-jump (point-marker))
4275 (back-to-indentation)
4276 (setq viper-last-jump-ignore (point))))
4278 (if (equal buff (current-buffer))
4279 (viper-execute-com (if skip-white
4280 'viper-goto-mark-and-skip-white
4283 (switch-to-buffer buff)
4284 (goto-char viper-com-point)
4285 (viper-change-state-to-vi)
4287 ((and (not skip-white) (= char ?`))
4288 (if com (viper-move-marker-locally 'viper-com-point (point)))
4289 (if (and (viper-same-line (point) viper-last-jump)
4290 (= (point) viper-last-jump-ignore))
4291 (goto-char viper-last-jump))
4292 (if (null (mark t)) (error "Mark is not set in this buffer"))
4293 (if (= (point) (mark t)) (pop-mark))
4294 (exchange-point-and-mark)
4295 (setq viper-last-jump (point-marker)
4296 viper-last-jump-ignore 0)
4297 (if com (viper-execute-com 'viper-goto-mark nil com)))
4298 ((and skip-white (= char ?'))
4299 (if com (viper-move-marker-locally 'viper-com-point (point)))
4300 (if (and (viper-same-line (point) viper-last-jump)
4301 (= (point) viper-last-jump-ignore))
4302 (goto-char viper-last-jump))
4303 (if (= (point) (mark t)) (pop-mark))
4304 (exchange-point-and-mark)
4305 (setq viper-last-jump (point))
4306 (back-to-indentation)
4307 (setq viper-last-jump-ignore (point))
4308 (if com (viper-execute-com 'viper-goto-mark-and-skip-white nil com)))
4309 (t (error viper-InvalidTextmarker char))))
4311 (defun viper-insert-tab ()
4315 (defun viper-exchange-point-and-mark ()
4317 (exchange-point-and-mark)
4318 (back-to-indentation))
4320 ;; Input Mode Indentation
4322 ;; Returns t, if the string before point matches the regexp STR.
4323 (defsubst viper-looking-back (str)
4324 (and (save-excursion (re-search-backward str nil t))
4325 (= (point) (match-end 0))))
4328 (defun viper-forward-indent ()
4329 "Indent forward -- `C-t' in Vi."
4332 (indent-to (+ (current-column) viper-shift-width)))
4334 (defun viper-backward-indent ()
4335 "Backtab, C-d in VI"
4338 (let ((p (point)) (c (current-column)) bol (indent t))
4339 (if (viper-looking-back "[0^]")
4341 (if (eq ?^ (preceding-char))
4342 (setq viper-preserve-indent t))
4343 (delete-backward-char 1)
4349 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4350 (delete-region (point) p)
4352 (indent-to (- c viper-shift-width)))
4353 (if (or (bolp) (viper-looking-back "[^ \t]"))
4354 (setq viper-cted nil)))))
4356 (defun viper-autoindent ()
4357 "Auto Indentation, Vi-style."
4359 (let ((col (current-indentation)))
4360 (if abbrev-mode (expand-abbrev))
4361 (if viper-preserve-indent
4362 (setq viper-preserve-indent nil)
4363 (setq viper-current-indent col))
4364 ;; don't leave whitespace lines around
4365 (if (memq last-command
4367 viper-open-line viper-Open-line
4368 viper-replace-state-exit-cmd))
4369 (indent-to-left-margin))
4370 ;; use \n instead of newline, or else <Return> will move the insert point
4373 (if viper-auto-indent
4376 (if (and viper-electric-mode
4378 (memq major-mode '(fundamental-mode
4380 paragraph-indent-text-mode ))))
4381 (indent-according-to-mode)
4382 (indent-to viper-current-indent))
4387 ;; Viewing registers
4389 (defun viper-ket-function (arg)
4390 "Function called by \], the ket. View registers and call \]\]."
4392 (let ((reg (read-char)))
4393 (cond ((viper-valid-register reg '(letter Letter))
4394 (view-register (downcase reg)))
4395 ((viper-valid-register reg '(digit))
4396 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4397 (with-output-to-temp-buffer " *viper-info*"
4398 (princ (format "Register %c contains the string:\n" reg))
4402 (viper-next-heading arg))
4404 viper-InvalidRegister reg)))))
4406 (defun viper-brac-function (arg)
4407 "Function called by \[, the brac. View textmarkers and call \[\["
4409 (let ((reg (read-char)))
4411 (viper-prev-heading arg))
4413 (viper-heading-end arg))
4414 ((viper-valid-register reg '(letter))
4415 (let* ((val (get-register (1+ (- reg ?a))))
4416 (buf (if (not (markerp val))
4417 (error viper-EmptyTextmarker reg)
4418 (marker-buffer val)))
4419 (pos (marker-position val))
4420 line-no text (s pos) (e pos))
4421 (with-output-to-temp-buffer " *viper-info*"
4426 (setq line-no (1+ (count-lines (point-min) val)))
4429 (if (re-search-backward "[^ \t]" nil t)
4435 (if (re-search-forward "[^ \t]" nil t)
4439 (setq text (buffer-substring s e))
4440 (setq text (format "%s<%c>%s"
4441 (substring text 0 (- pos s))
4442 reg (substring text (- pos s)))))
4445 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4446 reg (buffer-name buf) line-no))
4447 (princ (format "Here is some text around %c:\n\n %s"
4449 (princ (format viper-EmptyTextmarker reg))))
4451 (t (error viper-InvalidTextmarker reg)))))
4455 ;; commands in insertion mode
4457 (defun viper-delete-backward-word (arg)
4458 "Delete previous word."
4463 (delete-region (point) (mark t))
4467 (defun viper-set-expert-level (&optional dont-change-unless)
4468 "Sets the expert level for a Viper user.
4469 Can be called interactively to change (temporarily or permanently) the
4470 current expert level.
4472 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4473 the level should not be changed, unless its current value is
4474 meaningless (i.e., not one of 1,2,3,4,5).
4476 User level determines the setting of Viper variables that are most
4477 sensitive for VI-style look-and-feel."
4481 (if (not (natnump viper-expert-level)) (setq viper-expert-level 0))
4483 (save-window-excursion
4484 (delete-other-windows)
4485 ;; if 0 < viper-expert-level < viper-max-expert-level
4486 ;; & dont-change-unless = t -- use it; else ask
4487 (viper-ask-level dont-change-unless))
4489 (setq viper-always t
4490 viper-ex-style-motion t
4491 viper-ex-style-editing t
4492 viper-want-ctl-h-help nil)
4494 (cond ((eq viper-expert-level 1) ; novice or beginner
4495 (global-set-key ; in emacs-state
4497 (if (viper-window-display-p) 'viper-iconify 'suspend-emacs))
4498 (setq viper-no-multiple-ESC t
4500 viper-vi-style-in-minibuffer t
4501 viper-search-wrap-around-t t
4502 viper-electric-mode nil
4503 viper-want-emacs-keys-in-vi nil
4504 viper-want-emacs-keys-in-insert nil))
4506 ((and (> viper-expert-level 1) (< viper-expert-level 5))
4507 ;; intermediate to guru
4508 (setq viper-no-multiple-ESC (if (viper-window-display-p)
4510 viper-electric-mode t
4511 viper-want-emacs-keys-in-vi t
4512 viper-want-emacs-keys-in-insert (> viper-expert-level 2))
4514 (if (eq viper-expert-level 4) ; respect user's ex-style motion
4515 ; and viper-no-multiple-ESC
4518 viper-ex-style-editing
4519 (viper-standard-value 'viper-ex-style-editing)
4520 viper-ex-style-motion
4521 (viper-standard-value 'viper-ex-style-motion))
4522 (setq viper-ex-style-motion
4523 (viper-standard-value 'viper-ex-style-motion)
4524 viper-ex-style-editing
4525 (viper-standard-value 'viper-ex-style-editing)
4527 (viper-standard-value 'viper-re-search)
4528 viper-no-multiple-ESC
4529 (viper-standard-value 'viper-no-multiple-ESC)))))
4532 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4533 ;; user toggle the values of variables.
4534 (t (setq-default viper-ex-style-editing
4535 (viper-standard-value 'viper-ex-style-editing)
4536 viper-ex-style-motion
4537 (viper-standard-value 'viper-ex-style-motion))
4538 (setq viper-want-ctl-h-help
4539 (viper-standard-value 'viper-want-ctl-h-help)
4541 (viper-standard-value 'viper-always)
4542 viper-no-multiple-ESC
4543 (viper-standard-value 'viper-no-multiple-ESC)
4544 viper-ex-style-motion
4545 (viper-standard-value 'viper-ex-style-motion)
4546 viper-ex-style-editing
4547 (viper-standard-value 'viper-ex-style-editing)
4549 (viper-standard-value 'viper-re-search)
4551 (viper-standard-value 'viper-electric-mode)
4552 viper-want-emacs-keys-in-vi
4553 (viper-standard-value 'viper-want-emacs-keys-in-vi)
4554 viper-want-emacs-keys-in-insert
4555 (viper-standard-value 'viper-want-emacs-keys-in-insert))))
4557 (viper-set-mode-vars-for viper-current-state)
4558 (if (or viper-always
4559 (and (> viper-expert-level 0) (> 5 viper-expert-level)))
4562 ;; Ask user expert level.
4563 (defun viper-ask-level (dont-change-unless)
4564 (let ((ask-buffer " *viper-ask-level*")
4565 level-changed repeated)
4566 (save-window-excursion
4567 (switch-to-buffer ask-buffer)
4569 (while (or (> viper-expert-level viper-max-expert-level)
4570 (< viper-expert-level 1)
4571 (null dont-change-unless))
4575 (message "Invalid user level")
4578 (setq dont-change-unless t
4581 Please specify your level of familiarity with the venomous VI PERil
4582 (and the VI Plan for Emacs Rescue).
4583 You can change it at any time by typing `M-x viper-set-expert-level RET'
4585 1 -- BEGINNER: Almost all Emacs features are suppressed.
4586 Feels almost like straight Vi. File name completion and
4587 command history in the minibuffer are thrown in as a bonus.
4588 To use Emacs productively, you must reach level 3 or higher.
4589 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4590 so most Emacs commands can be used when Viper is in Vi state.
4591 Good progress---you are well on the way to level 3!
4592 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
4593 in Viper's insert state.
4594 4 -- GURU: Like 3, but user settings are respected for viper-no-multiple-ESC,
4595 viper-ex-style-motion, viper-ex-style-editing, and
4596 viper-re-search variables. Adjust these settings to your taste.
4597 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4598 viper-electric-mode, viper-want-ctl-h-help, viper-want-emacs-keys-in-vi,
4599 and viper-want-emacs-keys-in-insert. Adjust these to your taste.
4601 Please, specify your level now: ")
4603 (setq viper-expert-level (- (viper-read-char-exclusive) ?0))
4606 ;; tell the user if level was changed
4610 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4611 viper-expert-level))
4612 (if (y-or-n-p "Do you wish to make this change permanent? ")
4613 ;; save the setting for viper-expert-level
4616 (format "Saving user level %d ..." viper-expert-level)
4617 viper-custom-file-name))
4619 (bury-buffer) ; remove ask-buffer from screen
4629 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4630 (defun viper-register-to-point (char &optional enforce-buffer)
4631 "Like jump-to-register, but switches to another buffer in another window."
4632 (interactive "cViper register to point: ")
4633 (let ((val (get-register char)))
4635 ((and (fboundp 'frame-configuration-p)
4636 (frame-configuration-p val))
4637 (set-frame-configuration val))
4638 ((window-configuration-p val)
4639 (set-window-configuration val))
4640 ((viper-valid-marker val)
4641 (if (and enforce-buffer
4642 (not (equal (current-buffer) (marker-buffer val))))
4643 (error (concat viper-EmptyTextmarker " in this buffer")
4645 (pop-to-buffer (marker-buffer val))
4647 ((and (consp val) (eq (car val) 'file))
4648 (find-file (cdr val)))
4650 (error viper-EmptyTextmarker (1- (+ char ?a)))))))
4653 (defun viper-save-kill-buffer ()
4654 "Save then kill current buffer. "
4656 (if (< viper-expert-level 2)
4657 (save-buffers-kill-emacs)
4659 (kill-buffer (current-buffer))))
4665 (defun viper-submit-report ()
4666 "Submit bug report on Viper."
4668 (let ((reporter-prompt-for-summary-p t)
4669 (viper-device-type (viper-device-type))
4670 color-display-p frame-parameters
4671 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4672 varlist salutation window-config)
4674 ;; If mode info is needed, add variable to `let' and then set it below,
4675 ;; like we did with color-display-p.
4676 (setq color-display-p (if (viper-window-display-p)
4677 (viper-color-display-p)
4679 minibuffer-vi-face (if (viper-has-face-support-p)
4680 (viper-get-face viper-minibuffer-vi-face)
4682 minibuffer-insert-face (if (viper-has-face-support-p)
4684 viper-minibuffer-insert-face)
4686 minibuffer-emacs-face (if (viper-has-face-support-p)
4688 viper-minibuffer-emacs-face)
4690 frame-parameters (if (fboundp 'frame-parameters)
4691 (frame-parameters (selected-frame))))
4693 (setq varlist (list 'viper-vi-minibuffer-minor-mode
4694 'viper-insert-minibuffer-minor-mode
4695 'viper-vi-intercept-minor-mode
4696 'viper-vi-local-user-minor-mode
4697 'viper-vi-kbd-minor-mode
4698 'viper-vi-global-user-minor-mode
4699 'viper-vi-state-modifier-minor-mode
4700 'viper-vi-diehard-minor-mode
4701 'viper-vi-basic-minor-mode
4702 'viper-replace-minor-mode
4703 'viper-insert-intercept-minor-mode
4704 'viper-insert-local-user-minor-mode
4705 'viper-insert-kbd-minor-mode
4706 'viper-insert-global-user-minor-mode
4707 'viper-insert-state-modifier-minor-mode
4708 'viper-insert-diehard-minor-mode
4709 'viper-insert-basic-minor-mode
4710 'viper-emacs-intercept-minor-mode
4711 'viper-emacs-local-user-minor-mode
4712 'viper-emacs-kbd-minor-mode
4713 'viper-emacs-global-user-minor-mode
4714 'viper-emacs-state-modifier-minor-mode
4715 'viper-automatic-iso-accents
4716 'viper-special-input-method
4717 'viper-want-emacs-keys-in-insert
4718 'viper-want-emacs-keys-in-vi
4719 'viper-keep-point-on-undo
4720 'viper-no-multiple-ESC
4721 'viper-electric-mode
4723 'viper-want-ctl-h-help
4724 'viper-ex-style-editing
4725 'viper-delete-backwards-in-replace
4726 'viper-vi-style-in-minibuffer
4727 'viper-vi-state-hook
4728 'viper-insert-state-hook
4729 'viper-replace-state-hook
4730 'viper-emacs-state-hook
4731 'ex-cycle-other-window
4732 'ex-cycle-through-non-files
4739 'minibuffer-insert-face
4740 'minibuffer-emacs-face
4743 Congratulations! You may have unearthed a bug in Viper!
4744 Please mail a concise, accurate summary of the problem to the address above.
4746 -------------------------------------------------------------------")
4747 (setq window-config (current-window-configuration))
4748 (with-output-to-temp-buffer " *viper-info*"
4749 (switch-to-buffer " *viper-info*")
4750 (delete-other-windows)
4752 PLEASE FOLLOW THESE PROCEDURES
4753 ------------------------------
4755 Before reporting a bug, please verify that it is related to Viper, and is
4756 not cause by other packages you are using.
4758 Don't report compilation warnings, unless you are certain that there is a
4759 problem. These warnings are normal and unavoidable.
4761 Please note that users should not modify variables and keymaps other than
4762 those advertised in the manual. Such `customization' is likely to crash
4763 Viper, as it would any other improperly customized Emacs package.
4765 If you are reporting an error message received while executing one of the
4766 Viper commands, type:
4768 M-x set-variable <Return> debug-on-error <Return> t <Return>
4770 Then reproduce the error. The above command will cause Emacs to produce a
4771 back trace of the execution that leads to the error. Please include this
4772 trace in your bug report.
4774 If you believe that one of Viper's commands goes into an infinite loop
4775 \(e.g., Emacs freezes\), type:
4777 M-x set-variable <Return> debug-on-quit <Return> t <Return>
4779 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
4780 the current command. Include the resulting back trace in the bug report.
4782 Mail anyway (y or n)? ")
4783 (if (y-or-n-p "Mail anyway? ")
4785 (set-window-configuration window-config)
4786 (error "Bug report aborted")))
4789 (set-window-configuration window-config)
4791 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
4794 nil 'delete-other-windows
4801 ;; Smoothes out the difference between Emacs' unread-command-events
4802 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
4803 ;; events or a sequence of keys.
4805 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
4806 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
4807 ;; into an event. Below, we delete nil from event lists, since nil is the most
4808 ;; common symbol that might appear in this wrong context.
4809 (defun viper-set-unread-command-events (arg)
4812 unread-command-events
4814 (cond ((eventp arg) (list arg))
4817 (listify-key-sequence arg))
4819 "viper-set-unread-command-events: Invalid argument, %S"
4821 (if (not (eventp nil))
4822 (setq new-events (delq nil new-events)))
4823 (append new-events unread-command-events)))
4826 unread-command-events
4828 (cond ((viper-characterp arg) (list (character-to-event arg)))
4829 ((eventp arg) (list arg))
4830 ((stringp arg) (mapcar 'character-to-event arg))
4831 ((vectorp arg) (append arg nil)) ; turn into list
4832 ((listp arg) (viper-eventify-list-xemacs arg))
4834 "viper-set-unread-command-events: Invalid argument, %S" arg)))
4835 unread-command-events))))
4837 ;; list is assumed to be a list of events of characters
4838 (defun viper-eventify-list-xemacs (lis)
4841 (cond ((viper-characterp elt) (character-to-event elt))
4844 "viper-eventify-list-xemacs: can't convert to event, %S"
4850 ;;; viper-cmd.el ends here