(thing-at-point): Use `thing-at-point' property, if any.
[emacs.git] / lisp / emulation / viper-cmd.el
blobd8ff9bdaa67b5981e794bb6b9ec561f9fb4f0660
1 ;;; viper-cmd.el --- Vi command support for Viper
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
5 ;; Code
7 (provide 'viper-cmd)
8 (require 'advice)
10 ;; Compiler pacifier
11 (defvar vip-minibuffer-current-face)
12 (defvar vip-minibuffer-insert-face)
13 (defvar vip-minibuffer-vi-face)
14 (defvar vip-minibuffer-emacs-face)
15 (defvar viper-always)
16 (defvar vip-mode-string )
17 (defvar iso-accents-mode)
18 (defvar zmacs-region-stays)
19 (defvar mark-even-if-inactive)
21 ;; loading happens only in non-interactive compilation
22 ;; in order to spare non-viperized emacs from being viperized
23 (if noninteractive
24 (eval-when-compile
25 (let ((load-path (cons (expand-file-name ".") load-path)))
26 (or (featurep 'viper-util)
27 (load "viper-util.el" nil nil 'nosuffix))
28 (or (featurep 'viper-keym)
29 (load "viper-keym.el" nil nil 'nosuffix))
30 (or (featurep 'viper-mous)
31 (load "viper-mous.el" nil nil 'nosuffix))
32 (or (featurep 'viper-macs)
33 (load "viper-macs.el" nil nil 'nosuffix))
34 (or (featurep 'viper-ex)
35 (load "viper-ex.el" nil nil 'nosuffix))
36 )))
37 ;; end pacifier
40 (require 'viper-util)
41 (require 'viper-keym)
42 (require 'viper-mous)
43 (require 'viper-macs)
44 (require 'viper-ex)
48 ;; Generic predicates
50 ;; These test functions are shamelessly lifted from vip 4.4.2 by Aamod Sane
52 ;; generate test functions
53 ;; given symbol foo, foo-p is the test function, foos is the set of
54 ;; Viper command keys
55 ;; (macroexpand '(vip-test-com-defun foo))
56 ;; (defun foo-p (com) (consp (memq (if (< com 0) (- com) com) foos)))
58 (defmacro vip-test-com-defun (name)
59 (let* ((snm (symbol-name name))
60 (nm-p (intern (concat snm "-p")))
61 (nms (intern (concat snm "s"))))
62 (` (defun (, nm-p) (com)
63 (consp (memq (if (< com 0) (- com) com) (, nms)))))))
65 ;; Variables for defining VI commands
67 ;; Modifying commands that can be prefixes to movement commands
68 (defconst vip-prefix-commands '(?c ?d ?y ?! ?= ?# ?< ?> ?\"))
69 ;; define vip-prefix-command-p
70 (vip-test-com-defun vip-prefix-command)
72 ;; Commands that are pairs eg. dd. r and R here are a hack
73 (defconst vip-charpair-commands '(?c ?d ?y ?! ?= ?< ?> ?r ?R))
74 ;; define vip-charpair-command-p
75 (vip-test-com-defun vip-charpair-command)
77 (defconst vip-movement-commands '(?b ?B ?e ?E ?f ?F ?G ?h ?H ?j ?k ?l
78 ?H ?M ?L ?n ?t ?T ?w ?W ?$ ?%
79 ?^ ?( ?) ?- ?+ ?| ?{ ?} ?[ ?] ?' ?`
80 ?; ?, ?0 ?? ?/
82 "Movement commands")
83 ;; define vip-movement-command-p
84 (vip-test-com-defun vip-movement-command)
86 (defconst vip-digit-commands '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9)
87 "Digit commands")
88 ;; define vip-digit-command-p
89 (vip-test-com-defun vip-digit-command)
91 ;; Commands that can be repeated by . (dotted)
92 (defconst vip-dotable-commands '(?c ?d ?C ?s ?S ?D ?> ?<))
93 ;; define vip-dotable-command-p
94 (vip-test-com-defun vip-dotable-command)
96 ;; Commands that can follow a #
97 (defconst vip-hash-commands '(?c ?C ?g ?q ?s))
98 ;; define vip-hash-command-p
99 (vip-test-com-defun vip-hash-command)
101 ;; Commands that may have registers as prefix
102 (defconst vip-regsuffix-commands '(?d ?y ?Y ?D ?p ?P ?x ?X))
103 ;; define vip-regsuffix-command-p
104 (vip-test-com-defun vip-regsuffix-command)
106 (defconst vip-vi-commands (append vip-movement-commands
107 vip-digit-commands
108 vip-dotable-commands
109 vip-charpair-commands
110 vip-hash-commands
111 vip-prefix-commands
112 vip-regsuffix-commands)
113 "The list of all commands in Vi-state.")
114 ;; define vip-vi-command-p
115 (vip-test-com-defun vip-vi-command)
118 ;;; CODE
120 ;; sentinels
122 ;; Runs vip-after-change-functions inside after-change-functions
123 (defun vip-after-change-sentinel (beg end len)
124 (let ((list vip-after-change-functions))
125 (while list
126 (funcall (car list) beg end len)
127 (setq list (cdr list)))))
129 ;; Runs vip-before-change-functions inside before-change-functions
130 (defun vip-before-change-sentinel (beg end)
131 (let ((list vip-before-change-functions))
132 (while list
133 (funcall (car list) beg end)
134 (setq list (cdr list)))))
136 (defsubst vip-post-command-sentinel ()
137 (run-hooks 'vip-post-command-hooks))
139 (defsubst vip-pre-command-sentinel ()
140 (run-hooks 'vip-pre-command-hooks))
142 ;; Needed so that Viper will be able to figure the last inserted
143 ;; chunk of text with reasonable accuracy.
144 (defsubst vip-insert-state-post-command-sentinel ()
145 (if (and (memq vip-current-state '(insert-state replace-state))
146 vip-insert-point
147 (>= (point) vip-insert-point))
148 (setq vip-last-posn-while-in-insert-state (point-marker)))
149 (if (eq vip-current-state 'insert-state)
150 (progn
151 (or (stringp vip-saved-cursor-color)
152 (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
153 (setq vip-saved-cursor-color (vip-get-cursor-color)))
154 (if (stringp vip-saved-cursor-color)
155 (vip-change-cursor-color vip-insert-state-cursor-color))
157 (if (and (eq this-command 'dabbrev-expand)
158 (integerp vip-pre-command-point)
159 (> vip-insert-point vip-pre-command-point))
160 (move-marker vip-insert-point vip-pre-command-point))
163 (defsubst vip-insert-state-pre-command-sentinel ()
164 (or (memq this-command '(self-insert-command))
165 (memq (vip-event-key last-command-event)
166 '(up down left right (meta f) (meta b)
167 (control n) (control p) (control f) (control b)))
168 (vip-restore-cursor-color-after-insert))
169 (if (and (eq this-command 'dabbrev-expand)
170 (markerp vip-insert-point)
171 (marker-position vip-insert-point))
172 (setq vip-pre-command-point (marker-position vip-insert-point))))
174 (defsubst vip-R-state-post-command-sentinel ()
175 ;; Restoring cursor color is needed despite
176 ;; vip-replace-state-pre-command-sentinel: When you jump to another buffer in
177 ;; another frame, the pre-command hook won't change cursor color to default
178 ;; in that other frame. So, if the second frame cursor was red and we set
179 ;; the point outside the replacement region, then the cursor color will
180 ;; remain red. Restoring the default, below, prevents this.
181 (if (and (<= (vip-replace-start) (point))
182 (<= (point) (vip-replace-end)))
183 (vip-change-cursor-color vip-replace-overlay-cursor-color)
184 (vip-restore-cursor-color-after-replace)
187 ;; to speed up, don't change cursor color before self-insert
188 ;; and common move commands
189 (defsubst vip-replace-state-pre-command-sentinel ()
190 (or (memq this-command '(self-insert-command))
191 (memq (vip-event-key last-command-event)
192 '(up down left right (meta f) (meta b)
193 (control n) (control p) (control f) (control b)))
194 (vip-restore-cursor-color-after-replace)))
196 (defun vip-replace-state-post-command-sentinel ()
197 ;; Restoring cursor color is needed despite
198 ;; vip-replace-state-pre-command-sentinel: When one jumps to another buffer
199 ;; in another frame, the pre-command hook won't change cursor color to
200 ;; default in that other frame. So, if the second frame cursor was red and
201 ;; we set the point outside the replacement region, then the cursor color
202 ;; will remain red. Restoring the default, below, fixes this problem.
204 ;; We optimize for self-insert-command's here, since they either don't change
205 ;; cursor color or, if they terminate replace mode, the color will be changed
206 ;; in vip-finish-change
207 (or (memq this-command '(self-insert-command))
208 (vip-restore-cursor-color-after-replace))
209 (cond
210 ((eq vip-current-state 'replace-state)
211 ;; delete characters to compensate for inserted chars.
212 (let ((replace-boundary (vip-replace-end)))
213 (save-excursion
214 (goto-char vip-last-posn-in-replace-region)
215 (delete-char vip-replace-chars-to-delete)
216 (setq vip-replace-chars-to-delete 0
217 vip-replace-chars-deleted 0)
218 ;; terminate replace mode if reached replace limit
219 (if (= vip-last-posn-in-replace-region
220 (vip-replace-end))
221 (vip-finish-change vip-last-posn-in-replace-region)))
223 (if (and (<= (vip-replace-start) (point))
224 (<= (point) replace-boundary))
225 (progn
226 ;; the state may have changed in vip-finish-change above
227 (if (eq vip-current-state 'replace-state)
228 (vip-change-cursor-color vip-replace-overlay-cursor-color))
229 (setq vip-last-posn-in-replace-region (point-marker))))
232 (t ;; terminate replace mode if changed Viper states.
233 (vip-finish-change vip-last-posn-in-replace-region))))
236 ;; changing mode
238 ;; Change state to NEW-STATE---either emacs-state, vi-state, or insert-state.
239 (defun vip-change-state (new-state)
240 ;; Keep vip-post/pre-command-hooks fresh.
241 ;; We remove then add vip-post/pre-command-sentinel since it is very
242 ;; desirable that vip-pre-command-sentinel is the last hook and
243 ;; vip-post-command-sentinel is the first hook.
244 (remove-hook 'post-command-hook 'vip-post-command-sentinel)
245 (add-hook 'post-command-hook 'vip-post-command-sentinel)
246 (remove-hook 'pre-command-hook 'vip-pre-command-sentinel)
247 (add-hook 'pre-command-hook 'vip-pre-command-sentinel t)
248 ;; These hooks will be added back if switching to insert/replace mode
249 (vip-remove-hook 'vip-post-command-hooks
250 'vip-insert-state-post-command-sentinel)
251 (vip-remove-hook 'vip-pre-command-hooks
252 'vip-insert-state-pre-command-sentinel)
253 (cond ((eq new-state 'vi-state)
254 (cond ((member vip-current-state '(insert-state replace-state))
256 ;; move vip-last-posn-while-in-insert-state
257 ;; This is a normal hook that is executed in insert/replace
258 ;; states after each command. In Vi/Emacs state, it does
259 ;; nothing. We need to execute it here to make sure that
260 ;; the last posn was recorded when we hit ESC.
261 ;; It may be left unrecorded if the last thing done in
262 ;; insert/repl state was dabbrev-expansion or abbrev
263 ;; expansion caused by hitting ESC
264 (vip-insert-state-post-command-sentinel)
266 (condition-case conds
267 (progn
268 (vip-save-last-insertion
269 vip-insert-point
270 vip-last-posn-while-in-insert-state)
271 (if vip-began-as-replace
272 (setq vip-began-as-replace nil)
273 ;; repeat insert commands if numerical arg > 1
274 (save-excursion
275 (vip-repeat-insert-command))))
276 (error
277 (vip-message-conditions conds)))
279 (if (> (length vip-last-insertion) 0)
280 (vip-push-onto-ring vip-last-insertion
281 'vip-insertion-ring))
283 (if vip-ex-style-editing-in-insert
284 (or (bolp) (backward-char 1))))
287 ;; insert or replace
288 ((memq new-state '(insert-state replace-state))
289 (if (memq vip-current-state '(emacs-state vi-state))
290 (vip-move-marker-locally 'vip-insert-point (point)))
291 (vip-move-marker-locally 'vip-last-posn-while-in-insert-state (point))
292 (vip-add-hook 'vip-post-command-hooks
293 'vip-insert-state-post-command-sentinel t)
294 (vip-add-hook 'vip-pre-command-hooks
295 'vip-insert-state-pre-command-sentinel t))
296 ) ; outermost cond
298 ;; Nothing needs to be done to switch to emacs mode! Just set some
299 ;; variables, which is already done in vip-change-state-to-emacs!
301 (setq vip-current-state new-state)
302 (vip-normalize-minor-mode-map-alist)
303 (vip-adjust-keys-for new-state)
304 (vip-set-mode-vars-for new-state)
305 (vip-refresh-mode-line)
310 (defun vip-adjust-keys-for (state)
311 "Make necessary adjustments to keymaps before entering STATE."
312 (cond ((memq state '(insert-state replace-state))
313 (if vip-auto-indent
314 (progn
315 (define-key vip-insert-basic-map "\C-m" 'vip-autoindent)
316 (if vip-want-emacs-keys-in-insert
317 ;; expert
318 (define-key vip-insert-basic-map "\C-j" nil)
319 ;; novice
320 (define-key vip-insert-basic-map "\C-j" 'vip-autoindent)))
321 (define-key vip-insert-basic-map "\C-m" nil)
322 (define-key vip-insert-basic-map "\C-j" nil))
324 (setq vip-insert-diehard-minor-mode
325 (not vip-want-emacs-keys-in-insert))
327 (if vip-want-ctl-h-help
328 (progn
329 (define-key vip-insert-basic-map [(control h)] 'help-command)
330 (define-key vip-replace-map [(control h)] 'help-command))
331 (define-key vip-insert-basic-map
332 [(control h)] 'vip-del-backward-char-in-insert)
333 (define-key vip-replace-map
334 [(control h)] 'vip-del-backward-char-in-replace)))
336 (t ; Vi state
337 (setq vip-vi-diehard-minor-mode (not vip-want-emacs-keys-in-vi))
338 (if vip-want-ctl-h-help
339 (define-key vip-vi-basic-map [(control h)] 'help-command)
340 (define-key vip-vi-basic-map [(control h)] 'vip-backward-char)))
344 ;; Normalizes minor-mode-map-alist by putting Viper keymaps first.
345 ;; This ensures that Viper bindings are in effect, regardless of which minor
346 ;; modes were turned on by the user or by other packages.
347 (defun vip-normalize-minor-mode-map-alist ()
348 (setq minor-mode-map-alist
349 (vip-append-filter-alist
350 (list
351 (cons 'vip-vi-intercept-minor-mode vip-vi-intercept-map)
352 (cons 'vip-vi-minibuffer-minor-mode vip-minibuffer-map)
353 (cons 'vip-vi-local-user-minor-mode vip-vi-local-user-map)
354 (cons 'vip-vi-kbd-minor-mode vip-vi-kbd-map)
355 (cons 'vip-vi-global-user-minor-mode vip-vi-global-user-map)
356 (cons 'vip-vi-state-modifier-minor-mode
357 (if (keymapp
358 (cdr (assoc major-mode vip-vi-state-modifier-alist)))
359 (cdr (assoc major-mode vip-vi-state-modifier-alist))
360 vip-empty-keymap))
361 (cons 'vip-vi-diehard-minor-mode vip-vi-diehard-map)
362 (cons 'vip-vi-basic-minor-mode vip-vi-basic-map)
363 (cons 'vip-insert-intercept-minor-mode vip-insert-intercept-map)
364 (cons 'vip-replace-minor-mode vip-replace-map)
365 ;; vip-insert-minibuffer-minor-mode must come after
366 ;; vip-replace-minor-mode
367 (cons 'vip-insert-minibuffer-minor-mode
368 vip-minibuffer-map)
369 (cons 'vip-insert-local-user-minor-mode
370 vip-insert-local-user-map)
371 (cons 'vip-insert-kbd-minor-mode vip-insert-kbd-map)
372 (cons 'vip-insert-global-user-minor-mode
373 vip-insert-global-user-map)
374 (cons 'vip-insert-state-modifier-minor-mode
375 (if (keymapp
376 (cdr
377 (assoc major-mode vip-insert-state-modifier-alist)))
378 (cdr
379 (assoc major-mode vip-insert-state-modifier-alist))
380 vip-empty-keymap))
381 (cons 'vip-insert-diehard-minor-mode vip-insert-diehard-map)
382 (cons 'vip-insert-basic-minor-mode vip-insert-basic-map)
383 (cons 'vip-emacs-intercept-minor-mode
384 vip-emacs-intercept-map)
385 (cons 'vip-emacs-local-user-minor-mode
386 vip-emacs-local-user-map)
387 (cons 'vip-emacs-kbd-minor-mode vip-emacs-kbd-map)
388 (cons 'vip-emacs-global-user-minor-mode
389 vip-emacs-global-user-map)
390 (cons 'vip-emacs-state-modifier-minor-mode
391 (if (keymapp
392 (cdr
393 (assoc major-mode vip-emacs-state-modifier-alist)))
394 (cdr
395 (assoc major-mode vip-emacs-state-modifier-alist))
396 vip-empty-keymap))
398 minor-mode-map-alist)))
404 ;; Viper mode-changing commands and utilities
406 ;; Modifies mode-line-buffer-identification.
407 (defun vip-refresh-mode-line ()
408 (setq vip-mode-string
409 (cond ((eq vip-current-state 'emacs-state) vip-emacs-state-id)
410 ((eq vip-current-state 'vi-state) vip-vi-state-id)
411 ((eq vip-current-state 'replace-state) vip-replace-state-id)
412 ((eq vip-current-state 'insert-state) vip-insert-state-id)))
414 ;; Sets Viper mode string in global-mode-string
415 (force-mode-line-update))
418 ;; Switch from Insert state to Vi state.
419 (defun vip-exit-insert-state ()
420 (interactive)
421 (vip-change-state-to-vi))
423 (defun vip-set-mode-vars-for (state)
424 "Sets Viper minor mode variables to put Viper's state STATE in effect."
426 ;; Emacs state
427 (setq vip-vi-minibuffer-minor-mode nil
428 vip-insert-minibuffer-minor-mode nil
429 vip-vi-intercept-minor-mode nil
430 vip-insert-intercept-minor-mode nil
432 vip-vi-local-user-minor-mode nil
433 vip-vi-kbd-minor-mode nil
434 vip-vi-global-user-minor-mode nil
435 vip-vi-state-modifier-minor-mode nil
436 vip-vi-diehard-minor-mode nil
437 vip-vi-basic-minor-mode nil
439 vip-replace-minor-mode nil
441 vip-insert-local-user-minor-mode nil
442 vip-insert-kbd-minor-mode nil
443 vip-insert-global-user-minor-mode nil
444 vip-insert-state-modifier-minor-mode nil
445 vip-insert-diehard-minor-mode nil
446 vip-insert-basic-minor-mode nil
447 vip-emacs-intercept-minor-mode t
448 vip-emacs-local-user-minor-mode t
449 vip-emacs-kbd-minor-mode (not (vip-is-in-minibuffer))
450 vip-emacs-global-user-minor-mode t
451 vip-emacs-state-modifier-minor-mode t
454 ;; Vi state
455 (if (eq state 'vi-state) ; adjust for vi-state
456 (setq
457 vip-vi-intercept-minor-mode t
458 vip-vi-minibuffer-minor-mode (vip-is-in-minibuffer)
459 vip-vi-local-user-minor-mode t
460 vip-vi-kbd-minor-mode (not (vip-is-in-minibuffer))
461 vip-vi-global-user-minor-mode t
462 vip-vi-state-modifier-minor-mode t
463 ;; don't let the diehard keymap block command completion
464 ;; and other things in the minibuffer
465 vip-vi-diehard-minor-mode (not
466 (or vip-want-emacs-keys-in-vi
467 (vip-is-in-minibuffer)))
468 vip-vi-basic-minor-mode t
469 vip-emacs-intercept-minor-mode nil
470 vip-emacs-local-user-minor-mode nil
471 vip-emacs-kbd-minor-mode nil
472 vip-emacs-global-user-minor-mode nil
473 vip-emacs-state-modifier-minor-mode nil
476 ;; Insert and Replace states
477 (if (member state '(insert-state replace-state))
478 (setq
479 vip-insert-intercept-minor-mode t
480 vip-replace-minor-mode (eq state 'replace-state)
481 vip-insert-minibuffer-minor-mode (vip-is-in-minibuffer)
482 vip-insert-local-user-minor-mode t
483 vip-insert-kbd-minor-mode (not (vip-is-in-minibuffer))
484 vip-insert-global-user-minor-mode t
485 vip-insert-state-modifier-minor-mode t
486 ;; don't let the diehard keymap block command completion
487 ;; and other things in the minibuffer
488 vip-insert-diehard-minor-mode (not
489 (or vip-want-emacs-keys-in-insert
490 (vip-is-in-minibuffer)))
491 vip-insert-basic-minor-mode t
492 vip-emacs-intercept-minor-mode nil
493 vip-emacs-local-user-minor-mode nil
494 vip-emacs-kbd-minor-mode nil
495 vip-emacs-global-user-minor-mode nil
496 vip-emacs-state-modifier-minor-mode nil
499 ;; minibuffer faces
500 (if (vip-has-face-support-p)
501 (setq vip-minibuffer-current-face
502 (cond ((eq state 'emacs-state) vip-minibuffer-emacs-face)
503 ((eq state 'vi-state) vip-minibuffer-vi-face)
504 ((memq state '(insert-state replace-state))
505 vip-minibuffer-insert-face))))
507 (if (vip-is-in-minibuffer)
508 (vip-set-minibuffer-overlay))
511 ;; This also takes care of the annoying incomplete lines in files.
512 ;; Also, this fixes `undo' to work vi-style for complex commands.
513 (defun vip-change-state-to-vi ()
514 "Change Viper state to Vi."
515 (interactive)
516 (if (and vip-first-time (not (vip-is-in-minibuffer)))
517 (viper-mode)
518 (if overwrite-mode (overwrite-mode nil))
519 (if abbrev-mode (expand-abbrev))
520 (if (and auto-fill-function (> (current-column) fill-column))
521 (funcall auto-fill-function))
522 ;; don't leave whitespace lines around
523 (if (and (memq last-command
524 '(vip-autoindent
525 vip-open-line vip-Open-line
526 vip-replace-state-exit-cmd))
527 (vip-over-whitespace-line))
528 (indent-to-left-margin))
529 (vip-add-newline-at-eob-if-necessary)
530 (if vip-undo-needs-adjustment (vip-adjust-undo))
531 (vip-change-state 'vi-state)
533 ;; always turn off iso-accents-mode, or else we won't be able to use the
534 ;; keys `,',^ in Vi state, as they will do accents instead of Vi actions.
535 (if (and (boundp 'iso-accents-mode) iso-accents-mode)
536 (iso-accents-mode -1))
538 (vip-restore-cursor-color-after-insert)
540 ;; Protection against user errors in hooks
541 (condition-case conds
542 (run-hooks 'vip-vi-state-hook)
543 (error
544 (vip-message-conditions conds)))))
546 (defun vip-change-state-to-insert ()
547 "Change Viper state to Insert."
548 (interactive)
549 (vip-change-state 'insert-state)
550 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
551 (iso-accents-mode 1)) ; turn iso accents on
553 (or (stringp vip-saved-cursor-color)
554 (string= (vip-get-cursor-color) vip-insert-state-cursor-color)
555 (setq vip-saved-cursor-color (vip-get-cursor-color)))
556 ;; Commented out, because if vip-change-state-to-insert is executed
557 ;; non-interactively then the old cursor color may get lost. Same old Emacs
558 ;; bug related to local variables?
559 ;;;(if (stringp vip-saved-cursor-color)
560 ;;; (vip-change-cursor-color vip-insert-state-cursor-color))
561 ;; Protection against user errors in hooks
562 (condition-case conds
563 (run-hooks 'vip-insert-state-hook)
564 (error
565 (vip-message-conditions conds))))
567 (defsubst vip-downgrade-to-insert ()
568 (setq vip-current-state 'insert-state
569 vip-replace-minor-mode nil)
574 ;; Change to replace state. When the end of replacement region is reached,
575 ;; replace state changes to insert state.
576 (defun vip-change-state-to-replace (&optional non-R-cmd)
577 (vip-change-state 'replace-state)
578 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
579 (iso-accents-mode 1)) ; turn iso accents on
580 ;; Run insert-state-hook
581 (condition-case conds
582 (run-hooks 'vip-insert-state-hook 'vip-replace-state-hook)
583 (error
584 (vip-message-conditions conds)))
586 (if non-R-cmd
587 (vip-start-replace)
588 ;; 'R' is implemented using Emacs's overwrite-mode
589 (vip-start-R-mode))
593 (defun vip-change-state-to-emacs ()
594 "Change Viper state to Emacs."
595 (interactive)
596 (vip-change-state 'emacs-state)
597 (if (and vip-automatic-iso-accents (fboundp 'iso-accents-mode))
598 (iso-accents-mode 1)) ; turn iso accents on
600 ;; Protection agains user errors in hooks
601 (condition-case conds
602 (run-hooks 'vip-emacs-state-hook)
603 (error
604 (vip-message-conditions conds))))
606 ;; escape to emacs mode termporarily
607 (defun vip-escape-to-emacs (arg &optional events)
608 "Escape to Emacs state from Vi state for one Emacs command.
609 ARG is used as the prefix value for the executed command. If
610 EVENTS is a list of events, which become the beginning of the command."
611 (interactive "P")
612 (if (= last-command-char ?\\)
613 (message "Switched to EMACS state for the next command..."))
614 (vip-escape-to-state arg events 'emacs-state))
616 ;; escape to Vi mode termporarily
617 (defun vip-escape-to-vi (arg)
618 "Escape from Emacs state to Vi state for one Vi 1-character command.
619 If the Vi command that the user types has a prefix argument, e.g., `d2w', then
620 Vi's prefix argument will be used. Otherwise, the prefix argument passed to
621 `vip-escape-to-vi' is used."
622 (interactive "P")
623 (message "Switched to VI state for the next command...")
624 (vip-escape-to-state arg nil 'vi-state))
626 ;; Escape to STATE mode for one Emacs command.
627 (defun vip-escape-to-state (arg events state)
628 ;;(let (com key prefix-arg)
629 (let (com key)
630 ;; this temporarily turns off Viper's minor mode keymaps
631 (vip-set-mode-vars-for state)
632 (vip-normalize-minor-mode-map-alist)
633 (if events (vip-set-unread-command-events events))
635 ;; protect against keyboard quit and other errors
636 (condition-case nil
637 (let (vip-vi-kbd-minor-mode
638 vip-insert-kbd-minor-mode
639 vip-emacs-kbd-minor-mode)
640 (unwind-protect
641 (progn
642 (setq com (key-binding (setq key
643 (if vip-xemacs-p
644 (read-key-sequence nil)
645 (read-key-sequence nil t)))))
646 ;; In case of binding indirection--chase definitions.
647 ;; Have to do it here because we execute this command under
648 ;; different keymaps, so command-execute may not do the
649 ;; right thing there
650 (while (vectorp com) (setq com (key-binding com))))
651 nil)
652 ;; Execute command com in the original Viper state, not in state
653 ;; `state'. Otherwise, if we switch buffers while executing the
654 ;; escaped to command, Viper's mode vars will remain those of
655 ;; `state'. When we return to the orig buffer, the bindings will be
656 ;; screwed up.
657 (vip-set-mode-vars-for vip-current-state)
659 ;; this-command, last-command-char, last-command-event
660 (setq this-command com)
661 (if vip-xemacs-p ; XEmacs represents key sequences as vectors
662 (setq last-command-event (vip-copy-event (vip-seq-last-elt key))
663 last-command-char (event-to-character last-command-event))
664 ;; Emacs represents them as sequences (str or vec)
665 (setq last-command-event (vip-copy-event (vip-seq-last-elt key))
666 last-command-char last-command-event))
668 (if (commandp com)
669 (progn
670 (setq prefix-arg (or prefix-arg arg))
671 (command-execute com)))
673 (quit (ding))
674 (error (beep 1))))
675 ;; set state in the new buffer
676 (vip-set-mode-vars-for vip-current-state))
678 (defun vip-exec-form-in-vi (form)
679 "Execute FORM in Vi state, regardless of the Ccurrent Vi state."
680 (let ((buff (current-buffer))
681 result)
682 (vip-set-mode-vars-for 'vi-state)
684 (condition-case nil
685 (setq result (eval form))
686 (error
687 (signal 'quit nil)))
689 (if (not (equal buff (current-buffer))) ; cmd switched buffer
690 (save-excursion
691 (set-buffer buff)
692 (vip-set-mode-vars-for vip-current-state)))
693 (vip-set-mode-vars-for vip-current-state)
694 result))
696 (defun vip-exec-form-in-emacs (form)
697 "Execute FORM in Emacs, temporarily disabling Viper's minor modes.
698 Similar to vip-escape-to-emacs, but accepts forms rather than keystrokes."
699 (let ((buff (current-buffer))
700 result)
701 (vip-set-mode-vars-for 'emacs-state)
702 (setq result (eval form))
703 (if (not (equal buff (current-buffer))) ; cmd switched buffer
704 (save-excursion
705 (set-buffer buff)
706 (vip-set-mode-vars-for vip-current-state)))
707 (vip-set-mode-vars-for vip-current-state)
708 result))
711 ;; This is needed because minor modes sometimes override essential Viper
712 ;; bindings. By letting Viper know which files these modes are in, it will
713 ;; arrange to reorganize minor-mode-map-alist so that things will work right.
714 (defun vip-harness-minor-mode (load-file)
715 "Familiarize Viper with a minor mode defined in LOAD_FILE.
716 Minor modes that have their own keymaps may overshadow Viper keymaps.
717 This function is designed to make Viper aware of the packages that define
718 such minor modes.
719 Usage:
720 (vip-harness-minor-mode load-file)
722 LOAD-FILE is a name of the file where the specific minor mode is defined.
723 Suffixes such as .el or .elc should be stripped."
725 (interactive "sEnter name of the load file: ")
727 (vip-eval-after-load load-file '(vip-normalize-minor-mode-map-alist))
729 ;; Change the default for minor-mode-map-alist each time a harnessed minor
730 ;; mode adds its own keymap to the a-list.
731 (vip-eval-after-load
732 load-file '(setq-default minor-mode-map-alist minor-mode-map-alist))
736 (defun vip-ESC (arg)
737 "Emulate ESC key in Emacs.
738 Prevents multiple escape keystrokes if vip-no-multiple-ESC is true.
739 If vip-no-multiple-ESC is 'twice double ESC would ding in vi-state.
740 Other ESC sequences are emulated via the current Emacs's major mode
741 keymap. This is more convenient on TTYs, since this won't block
742 function keys such as up,down, etc. ESC will also will also work as
743 a Meta key in this case. When vip-no-multiple-ESC is nil, ESC functions
744 as a Meta key and any number of multiple escapes is allowed."
745 (interactive "P")
746 (let (char)
747 (cond ((and (not vip-no-multiple-ESC) (eq vip-current-state 'vi-state))
748 (setq char (vip-read-char-exclusive))
749 (vip-escape-to-emacs arg (list ?\e char) ))
750 ((and (eq vip-no-multiple-ESC 'twice)
751 (eq vip-current-state 'vi-state))
752 (setq char (vip-read-char-exclusive))
753 (if (= char (string-to-char vip-ESC-key))
754 (ding)
755 (vip-escape-to-emacs arg (list ?\e char) )))
756 (t (ding)))
759 (defun vip-alternate-Meta-key (arg)
760 "Simulate Emacs Meta key."
761 (interactive "P")
762 (sit-for 1) (message "ESC-")
763 (vip-escape-to-emacs arg '(?\e)))
765 (defun vip-toggle-key-action ()
766 "Action bound to `vip-toggle-key'."
767 (interactive)
768 (if (and (< vip-expert-level 2) (equal vip-toggle-key "\C-z"))
769 (if (vip-window-display-p)
770 (vip-iconify)
771 (suspend-emacs))
772 (vip-change-state-to-emacs)))
775 ;; Intercept ESC sequences on dumb terminals.
776 ;; Based on the idea contributed by Marcelino Veiga Tuimil <mveiga@dit.upm.es>
778 ;; Check if last key was ESC and if so try to reread it as a function key.
779 ;; But only if there are characters to read during a very short time.
780 ;; Returns the last event, if any.
781 (defun vip-envelop-ESC-key ()
782 (let ((event last-input-event)
783 (keyseq [nil])
784 inhibit-quit)
785 (if (vip-ESC-event-p event)
786 (progn
787 (if (vip-fast-keysequence-p)
788 (progn
789 (let (minor-mode-map-alist)
790 (vip-set-unread-command-events event)
791 (setq keyseq
792 (funcall
793 (ad-get-orig-definition 'read-key-sequence) nil))
794 ) ; let
795 ;; If keyseq translates into something that still has ESC
796 ;; at the beginning, separate ESC from the rest of the seq.
797 ;; In XEmacs we check for events that are keypress meta-key
798 ;; and convert them into [escape key]
800 ;; This is needed for the following reason:
801 ;; If ESC is the first symbol, we interpret it as if the
802 ;; user typed ESC and then quickly some other symbols.
803 ;; If ESC is not the first one, then the key sequence
804 ;; entered was apparently translated into a function key or
805 ;; something (e.g., one may have
806 ;; (define-key function-key-map "\e[192z" [f11])
807 ;; which would translate the escape-sequence generated by
808 ;; f11 in an xterm window into the symbolic key f11.
810 ;; If `first-key' is not an ESC event, we make it into the
811 ;; last-command-event in order to pretend that this key was
812 ;; pressed. This is needed to allow arrow keys to be bound to
813 ;; macros. Otherwise, vip-exec-mapped-kbd-macro will think that
814 ;; the last event was ESC and so it'll execute whatever is
815 ;; bound to ESC. (Viper macros can't be bound to
816 ;; ESC-sequences).
817 (let* ((first-key (elt keyseq 0))
818 (key-mod (event-modifiers first-key)))
819 (cond ((vip-ESC-event-p first-key)
820 ;; put keys following ESC on the unread list
821 ;; and return ESC as the key-sequence
822 (vip-set-unread-command-events (subseq keyseq 1))
823 (setq last-input-event event
824 keyseq (if vip-emacs-p
825 "\e"
826 (vector (character-to-event ?\e)))))
827 ((and vip-xemacs-p
828 (key-press-event-p first-key)
829 (equal '(meta) key-mod))
830 (vip-set-unread-command-events
831 (vconcat (vector
832 (character-to-event (event-key first-key)))
833 (subseq keyseq 1)))
834 (setq last-input-event event
835 keyseq (vector (character-to-event ?\e))))
836 ((eventp first-key)
837 (setq last-command-event (vip-copy-event first-key)))
839 ) ; end progn
841 ;; this is escape event with nothing after it
842 ;; put in unread-command-event and then re-read
843 (vip-set-unread-command-events event)
844 (setq keyseq
845 (funcall (ad-get-orig-definition 'read-key-sequence) nil))
847 ;; not an escape event
848 (setq keyseq (vector event)))
849 keyseq))
853 ;; Listen to ESC key.
854 ;; If a sequence of keys starting with ESC is issued with very short delays,
855 ;; interpret these keys in Emacs mode, so ESC won't be interpreted as a Vi key.
856 (defun vip-intercept-ESC-key ()
857 "Function that implements ESC key in Viper emulation of Vi."
858 (interactive)
859 (let ((cmd (or (key-binding (vip-envelop-ESC-key))
860 '(lambda () (interactive) (error "")))))
862 ;; call the actual function to execute ESC (if no other symbols followed)
863 ;; or the key bound to the ESC sequence (if the sequence was issued
864 ;; with very short delay between characters.
865 (if (eq cmd 'vip-intercept-ESC-key)
866 (setq cmd
867 (cond ((eq vip-current-state 'vi-state)
868 'vip-ESC)
869 ((eq vip-current-state 'insert-state)
870 'vip-exit-insert-state)
871 ((eq vip-current-state 'replace-state)
872 'vip-replace-state-exit-cmd)
873 (t 'vip-change-state-to-vi)
875 (call-interactively cmd)))
880 ;; prefix argument for Vi mode
882 ;; In Vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
883 ;; represents the numeric value of the prefix argument and COM represents
884 ;; command prefix such as "c", "d", "m" and "y".
886 ;; Get value part of prefix-argument ARG.
887 (defsubst vip-p-val (arg)
888 (cond ((null arg) 1)
889 ((consp arg)
890 (if (or (null (car arg)) (equal (car arg) '(nil)))
891 1 (car arg)))
892 (t arg)))
894 ;; Get raw value part of prefix-argument ARG.
895 (defsubst vip-P-val (arg)
896 (cond ((consp arg) (car arg))
897 (t arg)))
899 ;; Get com part of prefix-argument ARG.
900 (defsubst vip-getcom (arg)
901 (cond ((null arg) nil)
902 ((consp arg) (cdr arg))
903 (t nil)))
905 ;; Get com part of prefix-argument ARG and modify it.
906 (defun vip-getCom (arg)
907 (let ((com (vip-getcom arg)))
908 (cond ((equal com ?c) ?C)
909 ((equal com ?d) ?D)
910 ((equal com ?y) ?Y)
911 (t com))))
914 ;; Compute numeric prefix arg value.
915 ;; Invoked by EVENT. COM is the command part obtained so far.
916 (defun vip-prefix-arg-value (event com)
917 (let (value func)
918 ;; read while number
919 (while (and (vip-characterp event) (>= event ?0) (<= event ?9))
920 (setq value (+ (* (if (integerp value) value 0) 10) (- event ?0)))
921 (setq event (vip-read-event-convert-to-char)))
923 (setq prefix-arg value)
924 (if com (setq prefix-arg (cons prefix-arg com)))
925 (while (eq event ?U)
926 (vip-describe-arg prefix-arg)
927 (setq event (vip-read-event-convert-to-char)))
929 (if (or com (and (not (eq vip-current-state 'vi-state))
930 ;; make sure it is a Vi command
931 (vip-characterp event) (vip-vi-command-p event)
933 ;; If appears to be one of the vi commands,
934 ;; then execute it with funcall and clear prefix-arg in order to not
935 ;; confuse subsequent commands
936 (progn
937 ;; last-command-char is the char we want emacs to think was typed
938 ;; last. If com is not nil, the vip-digit-argument command was called
939 ;; from within vip-prefix-arg command, such as `d', `w', etc., i.e.,
940 ;; the user typed, say, d2. In this case, `com' would be `d', `w',
941 ;; etc.
942 ;; If vip-digit-argument was invoked by vip-escape-to-vi (which is
943 ;; indicated by the fact that the current state is not vi-state),
944 ;; then `event' represents the vi command to be executed (e.g., `d',
945 ;; `w', etc). Again, last-command-char must make emacs believe that
946 ;; this is the command we typed.
947 (setq last-command-char (or com event))
948 (setq func (vip-exec-form-in-vi
949 (` (key-binding (char-to-string (, event))))))
950 (funcall func prefix-arg)
951 (setq prefix-arg nil))
952 ;; some other command -- let emacs do it in its own way
953 (vip-set-unread-command-events event))
957 ;; Vi operator as prefix argument."
958 (defun vip-prefix-arg-com (char value com)
959 (let ((cont t)
960 cmd-info mv-or-digit-cmd)
961 (while (and cont
962 (memq char
963 (list ?c ?d ?y ?! ?< ?> ?= ?# ?r ?R ?\"
964 vip-buffer-search-char)))
965 (if com
966 ;; this means that we already have a command character, so we
967 ;; construct a com list and exit while. however, if char is "
968 ;; it is an error.
969 (progn
970 ;; new com is (CHAR . OLDCOM)
971 (if (memq char '(?# ?\")) (error ""))
972 (setq com (cons char com))
973 (setq cont nil))
974 ;; If com is nil we set com as char, and read more. Again, if char
975 ;; is ", we read the name of register and store it in vip-use-register.
976 ;; if char is !, =, or #, a complete com is formed so we exit the
977 ;; while loop.
978 (cond ((memq char '(?! ?=))
979 (setq com char)
980 (setq char (read-char))
981 (setq cont nil))
982 ((= char ?#)
983 ;; read a char and encode it as com
984 (setq com (+ 128 (read-char)))
985 (setq char (read-char)))
986 ((= char ?\")
987 (let ((reg (read-char)))
988 (if (vip-valid-register reg)
989 (setq vip-use-register reg)
990 (error ""))
991 (setq char (read-char))))
993 (setq com char)
994 (setq char (read-char))))))
996 (if (atom com)
997 ;; `com' is a single char, so we construct the command argument
998 ;; and if `char' is `?', we describe the arg; otherwise
999 ;; we prepare the command that will be executed at the end.
1000 (progn
1001 (setq cmd-info (cons value com))
1002 (while (= char ?U)
1003 (vip-describe-arg cmd-info)
1004 (setq char (read-char)))
1005 ;; `char' is a movement cmd, a digit arg cmd, or a register cmd---so we
1006 ;; execute it at the very end
1007 (or (vip-movement-command-p char)
1008 (vip-digit-command-p char)
1009 (vip-regsuffix-command-p char)
1010 (error ""))
1011 (setq mv-or-digit-cmd
1012 (vip-exec-form-in-vi
1013 (` (key-binding (char-to-string (, char)))))))
1015 ;; as com is non-nil, this means that we have a command to execute
1016 (if (memq (car com) '(?r ?R))
1017 ;; execute apropriate region command.
1018 (let ((char (car com)) (com (cdr com)))
1019 (setq prefix-arg (cons value com))
1020 (if (= char ?r) (vip-region prefix-arg)
1021 (vip-Region prefix-arg))
1022 ;; reset prefix-arg
1023 (setq prefix-arg nil))
1024 ;; otherwise, reset prefix arg and call appropriate command
1025 (setq value (if (null value) 1 value))
1026 (setq prefix-arg nil)
1027 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
1028 ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
1029 ((equal com '(?d . ?y)) (vip-yank-defun))
1030 ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
1031 ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
1032 ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
1033 ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
1034 ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
1035 (t (error "")))))
1037 (if mv-or-digit-cmd
1038 (progn
1039 (setq last-command-char char)
1040 (setq last-command-event
1041 (vip-copy-event
1042 (if vip-xemacs-p (character-to-event char) char)))
1043 (condition-case nil
1044 (funcall mv-or-digit-cmd cmd-info)
1045 (error
1046 (error "")))))
1049 (defun vip-describe-arg (arg)
1050 (let (val com)
1051 (setq val (vip-P-val arg)
1052 com (vip-getcom arg))
1053 (if (null val)
1054 (if (null com)
1055 (message "Value is nil, and command is nil")
1056 (message "Value is nil, and command is `%c'" com))
1057 (if (null com)
1058 (message "Value is `%d', and command is nil" val)
1059 (message "Value is `%d', and command is `%c'" val com)))))
1061 (defun vip-digit-argument (arg)
1062 "Begin numeric argument for the next command."
1063 (interactive "P")
1064 (vip-leave-region-active)
1065 (vip-prefix-arg-value
1066 last-command-char (if (consp arg) (cdr arg) nil)))
1068 (defun vip-command-argument (arg)
1069 "Accept a motion command as an argument."
1070 (interactive "P")
1071 (let ((vip-inside-command-argument-action t))
1072 (condition-case nil
1073 (vip-prefix-arg-com
1074 last-command-char
1075 (cond ((null arg) nil)
1076 ((consp arg) (car arg))
1077 ((integerp arg) arg)
1078 (t (error vip-InvalidCommandArgument)))
1079 (cond ((null arg) nil)
1080 ((consp arg) (cdr arg))
1081 ((integerp arg) nil)
1082 (t (error vip-InvalidCommandArgument))))
1083 (quit (setq vip-use-register nil)
1084 (signal 'quit nil)))
1085 (vip-deactivate-mark)))
1088 ;; repeat last destructive command
1090 ;; Append region to text in register REG.
1091 ;; START and END are buffer positions indicating what to append.
1092 (defsubst vip-append-to-register (reg start end)
1093 (set-register reg (concat (if (stringp (get-register reg))
1094 (get-register reg) "")
1095 (buffer-substring start end))))
1097 ;; Saves last inserted text for possible use by vip-repeat command.
1098 (defun vip-save-last-insertion (beg end)
1099 (setq vip-last-insertion (buffer-substring beg end))
1100 (or (< (length vip-d-com) 5)
1101 (setcar (nthcdr 4 vip-d-com) vip-last-insertion))
1102 (or (null vip-command-ring)
1103 (ring-empty-p vip-command-ring)
1104 (progn
1105 (setcar (nthcdr 4 (vip-current-ring-item vip-command-ring))
1106 vip-last-insertion)
1107 ;; del most recent elt, if identical to the second most-recent
1108 (vip-cleanup-ring vip-command-ring)))
1111 (defsubst vip-yank-last-insertion ()
1112 "Inserts the text saved by the previous vip-save-last-insertion command."
1113 (condition-case nil
1114 (insert vip-last-insertion)
1115 (error nil)))
1118 ;; define functions to be executed
1120 ;; invoked by the `C' command
1121 (defun vip-exec-change (m-com com)
1122 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1123 (set-marker vip-com-point (point) (current-buffer)))
1124 ;; handle C cmd at the eol and at eob.
1125 (if (or (and (eolp) (= vip-com-point (point)))
1126 (= vip-com-point (point-max)))
1127 (progn
1128 (insert " ")(backward-char 1)))
1129 (if (= vip-com-point (point))
1130 (vip-forward-char-carefully))
1131 (if (= com ?c)
1132 (vip-change vip-com-point (point))
1133 (vip-change-subr vip-com-point (point))))
1135 ;; this is invoked by vip-substitute-line
1136 (defun vip-exec-Change (m-com com)
1137 (save-excursion
1138 (set-mark vip-com-point)
1139 (vip-enlarge-region (mark t) (point))
1140 (if vip-use-register
1141 (progn
1142 (cond ((vip-valid-register vip-use-register '(letter digit))
1143 ;;(vip-valid-register vip-use-register '(letter)
1144 (copy-to-register
1145 vip-use-register (mark t) (point) nil))
1146 ((vip-valid-register vip-use-register '(Letter))
1147 (vip-append-to-register
1148 (downcase vip-use-register) (mark t) (point)))
1149 (t (setq vip-use-register nil)
1150 (error vip-InvalidRegister vip-use-register)))
1151 (setq vip-use-register nil)))
1152 (delete-region (mark t) (point)))
1153 (open-line 1)
1154 (if (= com ?C) (vip-change-mode-to-insert) (vip-yank-last-insertion)))
1156 (defun vip-exec-delete (m-com com)
1157 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1158 (set-marker vip-com-point (point) (current-buffer)))
1159 (if vip-use-register
1160 (progn
1161 (cond ((vip-valid-register vip-use-register '(letter digit))
1162 ;;(vip-valid-register vip-use-register '(letter))
1163 (copy-to-register
1164 vip-use-register vip-com-point (point) nil))
1165 ((vip-valid-register vip-use-register '(Letter))
1166 (vip-append-to-register
1167 (downcase vip-use-register) vip-com-point (point)))
1168 (t (setq vip-use-register nil)
1169 (error vip-InvalidRegister vip-use-register)))
1170 (setq vip-use-register nil)))
1171 (setq last-command
1172 (if (eq last-command 'd-command) 'kill-region nil))
1173 (kill-region vip-com-point (point))
1174 (setq this-command 'd-command)
1175 (if vip-ex-style-motion
1176 (if (and (eolp) (not (bolp))) (backward-char 1))))
1178 (defun vip-exec-Delete (m-com com)
1179 (save-excursion
1180 (set-mark vip-com-point)
1181 (vip-enlarge-region (mark t) (point))
1182 (if vip-use-register
1183 (progn
1184 (cond ((vip-valid-register vip-use-register '(letter digit))
1185 ;;(vip-valid-register vip-use-register '(letter))
1186 (copy-to-register
1187 vip-use-register (mark t) (point) nil))
1188 ((vip-valid-register vip-use-register '(Letter))
1189 (vip-append-to-register
1190 (downcase vip-use-register) (mark t) (point)))
1191 (t (setq vip-use-register nil)
1192 (error vip-InvalidRegister vip-use-register)))
1193 (setq vip-use-register nil)))
1194 (setq last-command
1195 (if (eq last-command 'D-command) 'kill-region nil))
1196 (kill-region (mark t) (point))
1197 (if (eq m-com 'vip-line) (setq this-command 'D-command)))
1198 (back-to-indentation))
1200 (defun vip-exec-yank (m-com com)
1201 (or (and (markerp vip-com-point) (marker-position vip-com-point))
1202 (set-marker vip-com-point (point) (current-buffer)))
1203 (if vip-use-register
1204 (progn
1205 (cond ((vip-valid-register vip-use-register '(letter digit))
1206 ;; (vip-valid-register vip-use-register '(letter))
1207 (copy-to-register
1208 vip-use-register vip-com-point (point) nil))
1209 ((vip-valid-register vip-use-register '(Letter))
1210 (vip-append-to-register
1211 (downcase vip-use-register) vip-com-point (point)))
1212 (t (setq vip-use-register nil)
1213 (error vip-InvalidRegister vip-use-register)))
1214 (setq vip-use-register nil)))
1215 (setq last-command nil)
1216 (copy-region-as-kill vip-com-point (point))
1217 (goto-char vip-com-point))
1219 (defun vip-exec-Yank (m-com com)
1220 (save-excursion
1221 (set-mark vip-com-point)
1222 (vip-enlarge-region (mark t) (point))
1223 (if vip-use-register
1224 (progn
1225 (cond ((vip-valid-register vip-use-register '(letter digit))
1226 (copy-to-register
1227 vip-use-register (mark t) (point) nil))
1228 ((vip-valid-register vip-use-register '(Letter))
1229 (vip-append-to-register
1230 (downcase vip-use-register) (mark t) (point)))
1231 (t (setq vip-use-register nil)
1232 (error vip-InvalidRegister vip-use-register)))
1233 (setq vip-use-register nil)))
1234 (setq last-command nil)
1235 (copy-region-as-kill (mark t) (point)))
1236 (vip-deactivate-mark)
1237 (goto-char vip-com-point))
1239 (defun vip-exec-bang (m-com com)
1240 (save-excursion
1241 (set-mark vip-com-point)
1242 (vip-enlarge-region (mark t) (point))
1243 (shell-command-on-region
1244 (mark t) (point)
1245 (if (= com ?!)
1246 (setq vip-last-shell-com
1247 (vip-read-string-with-history
1250 'vip-shell-history
1251 (car vip-shell-history)
1253 vip-last-shell-com)
1254 t)))
1256 (defun vip-exec-equals (m-com com)
1257 (save-excursion
1258 (set-mark vip-com-point)
1259 (vip-enlarge-region (mark t) (point))
1260 (if (> (mark t) (point)) (exchange-point-and-mark))
1261 (indent-region (mark t) (point) nil)))
1263 (defun vip-exec-shift (m-com com)
1264 (save-excursion
1265 (set-mark vip-com-point)
1266 (vip-enlarge-region (mark t) (point))
1267 (if (> (mark t) (point)) (exchange-point-and-mark))
1268 (indent-rigidly (mark t) (point)
1269 (if (= com ?>)
1270 vip-shift-width
1271 (- vip-shift-width))))
1272 ;; return point to where it was before shift
1273 (goto-char vip-com-point))
1275 ;; this is needed because some commands fake com by setting it to ?r, which
1276 ;; denotes repeated insert command.
1277 (defsubst vip-exec-dummy (m-com com)
1278 nil)
1280 (defun vip-exec-buffer-search (m-com com)
1281 (setq vip-s-string (buffer-substring (point) vip-com-point))
1282 (setq vip-s-forward t)
1283 (setq vip-search-history (cons vip-s-string vip-search-history))
1284 (vip-search vip-s-string vip-s-forward 1))
1286 (defvar vip-exec-array (make-vector 128 nil))
1288 ;; Using a dispatch array allows adding functions like buffer search
1289 ;; without affecting other functions. Buffer search can now be bound
1290 ;; to any character.
1292 (aset vip-exec-array ?c 'vip-exec-change)
1293 (aset vip-exec-array ?C 'vip-exec-Change)
1294 (aset vip-exec-array ?d 'vip-exec-delete)
1295 (aset vip-exec-array ?D 'vip-exec-Delete)
1296 (aset vip-exec-array ?y 'vip-exec-yank)
1297 (aset vip-exec-array ?Y 'vip-exec-Yank)
1298 (aset vip-exec-array ?r 'vip-exec-dummy)
1299 (aset vip-exec-array ?! 'vip-exec-bang)
1300 (aset vip-exec-array ?< 'vip-exec-shift)
1301 (aset vip-exec-array ?> 'vip-exec-shift)
1302 (aset vip-exec-array ?= 'vip-exec-equals)
1306 ;; This function is called by various movement commands to execute a
1307 ;; destructive command on the region specified by the movement command. For
1308 ;; instance, if the user types cw, then the command vip-forward-word will
1309 ;; call vip-execute-com to execute vip-exec-change, which eventually will
1310 ;; call vip-change to invoke the replace mode on the region.
1312 ;; The list (M-COM VAL COM REG INSETED-TEXT COMMAND-KEYS) is set to
1313 ;; vip-d-com for later use by vip-repeat.
1314 (defun vip-execute-com (m-com val com)
1315 (let ((reg vip-use-register))
1316 ;; this is the special command `#'
1317 (if (> com 128)
1318 (vip-special-prefix-com (- com 128))
1319 (let ((fn (aref vip-exec-array (if (< com 0) (- com) com))))
1320 (if (null fn)
1321 (error "%c: %s" com vip-InvalidViCommand)
1322 (funcall fn m-com com))))
1323 (if (vip-dotable-command-p com)
1324 (vip-set-destructive-command
1325 (list m-com val
1326 (if (memq com (list ?c ?C ?!)) (- com) com)
1327 reg nil nil)))
1331 (defun vip-repeat (arg)
1332 "Re-execute last destructive command.
1333 Use the info in vip-d-com, which has the form
1334 \(com val ch reg inserted-text command-keys\),
1335 where `com' is the command to be re-executed, `val' is the
1336 argument to `com', `ch' is a flag for repeat, and `reg' is optional;
1337 if it exists, it is the name of the register for `com'.
1338 If the prefix argument, ARG, is non-nil, it is used instead of `val'."
1339 (interactive "P")
1340 (let ((save-point (point)) ; save point before repeating prev cmd
1341 ;; Pass along that we are repeating a destructive command
1342 ;; This tells vip-set-destructive-command not to update
1343 ;; vip-command-ring
1344 (vip-intermediate-command 'vip-repeat))
1345 (if (eq last-command 'vip-undo)
1346 ;; if the last command was vip-undo, then undo-more
1347 (vip-undo-more)
1348 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
1349 ;; its prefix value is used as new prefix value for the command.
1350 (let ((m-com (car vip-d-com))
1351 (val (vip-P-val arg))
1352 (com (nth 2 vip-d-com))
1353 (reg (nth 3 vip-d-com)))
1354 (if (null val) (setq val (nth 1 vip-d-com)))
1355 (if (null m-com) (error "No previous command to repeat."))
1356 (setq vip-use-register reg)
1357 (if (nth 4 vip-d-com) ; text inserted by command
1358 (setq vip-last-insertion (nth 4 vip-d-com)
1359 vip-d-char (nth 4 vip-d-com)))
1360 (funcall m-com (cons val com))
1361 (if (and vip-keep-point-on-repeat (< save-point (point)))
1362 (goto-char save-point)) ; go back to before repeat.
1363 (if (and (eolp) (not (bolp)))
1364 (backward-char 1))
1366 (if vip-undo-needs-adjustment (vip-adjust-undo)) ; take care of undo
1367 ;; If the prev cmd was rotating the command ring, this means that `.' has
1368 ;; just executed a command from that ring. So, push it on the ring again.
1369 ;; If we are just executing previous command , then don't push vip-d-com
1370 ;; because vip-d-com is not fully constructed in this case (its keys and
1371 ;; the inserted text may be nil). Besides, in this case, the command
1372 ;; executed by `.' is already on the ring.
1373 (if (eq last-command 'vip-display-current-destructive-command)
1374 (vip-push-onto-ring vip-d-com 'vip-command-ring))
1375 (vip-deactivate-mark)
1378 (defun vip-repeat-from-history ()
1379 "Repeat a destructive command from history.
1380 Doesn't change vip-command-ring in any way, so `.' will work as before
1381 executing this command.
1382 This command is supposed to be bound to a two-character Vi macro where
1383 the second character is a digit 0 to 9. The digit indicates which
1384 history command to execute. `<char>0' is equivalent to `.', `<char>1'
1385 invokes the command before that, etc."
1386 (interactive)
1387 (let* ((vip-intermediate-command 'repeating-display-destructive-command)
1388 (idx (cond (vip-this-kbd-macro
1389 (string-to-number
1390 (symbol-name (elt vip-this-kbd-macro 1))))
1391 (t 0)))
1392 (num idx)
1393 (vip-d-com vip-d-com))
1395 (or (and (numberp num) (<= 0 num) (<= num 9))
1396 (progn
1397 (setq idx 0
1398 num 0)
1399 (message
1400 "`vip-repeat-from-history' must be invoked as a Vi macro bound to `<key><digit>'")))
1401 (while (< 0 num)
1402 (setq vip-d-com (vip-special-ring-rotate1 vip-command-ring -1))
1403 (setq num (1- num)))
1404 (vip-repeat nil)
1405 (while (> idx num)
1406 (vip-special-ring-rotate1 vip-command-ring 1)
1407 (setq num (1+ num)))
1411 ;; The hash-command. It is invoked interactively by the key sequence #<char>.
1412 ;; The chars that can follow `#' are determined by vip-hash-command-p
1413 (defun vip-special-prefix-com (char)
1414 (cond ((= char ?c)
1415 (downcase-region (min vip-com-point (point))
1416 (max vip-com-point (point))))
1417 ((= char ?C)
1418 (upcase-region (min vip-com-point (point))
1419 (max vip-com-point (point))))
1420 ((= char ?g)
1421 (push-mark vip-com-point t)
1422 (vip-global-execute))
1423 ((= char ?q)
1424 (push-mark vip-com-point t)
1425 (vip-quote-region))
1426 ((= char ?s) (funcall vip-spell-function vip-com-point (point)))
1427 (t (error "#%c: %s" char vip-InvalidViCommand))))
1430 ;; undoing
1432 (defun vip-undo ()
1433 "Undo previous change."
1434 (interactive)
1435 (message "undo!")
1436 (let ((modified (buffer-modified-p))
1437 (before-undo-pt (point-marker))
1438 (after-change-functions after-change-functions)
1439 undo-beg-posn undo-end-posn)
1441 ;; no need to remove this hook, since this var has scope inside a let.
1442 (add-hook 'after-change-functions
1443 '(lambda (beg end len)
1444 (setq undo-beg-posn beg
1445 undo-end-posn (or end beg))))
1447 (undo-start)
1448 (undo-more 2)
1449 (setq undo-beg-posn (or undo-beg-posn before-undo-pt)
1450 undo-end-posn (or undo-end-posn undo-beg-posn))
1452 (goto-char undo-beg-posn)
1453 (sit-for 0)
1454 (if (and vip-keep-point-on-undo
1455 (pos-visible-in-window-p before-undo-pt))
1456 (progn
1457 (push-mark (point-marker) t)
1458 (vip-sit-for-short 300)
1459 (goto-char undo-end-posn)
1460 (vip-sit-for-short 300)
1461 (if (and (> (abs (- undo-beg-posn before-undo-pt)) 1)
1462 (> (abs (- undo-end-posn before-undo-pt)) 1))
1463 (goto-char before-undo-pt)
1464 (goto-char undo-beg-posn)))
1465 (push-mark before-undo-pt t))
1466 (if (and (eolp) (not (bolp))) (backward-char 1))
1467 (if (not modified) (set-buffer-modified-p t)))
1468 (setq this-command 'vip-undo))
1470 ;; Continue undoing previous changes.
1471 (defun vip-undo-more ()
1472 (message "undo more!")
1473 (condition-case nil
1474 (undo-more 1)
1475 (error (beep)
1476 (message "No further undo information in this buffer")))
1477 (if (and (eolp) (not (bolp))) (backward-char 1))
1478 (setq this-command 'vip-undo))
1480 ;; The following two functions are used to set up undo properly.
1481 ;; In VI, unlike Emacs, if you open a line, say, and add a bunch of lines,
1482 ;; they are undone all at once.
1483 (defun vip-adjust-undo ()
1484 (let ((inhibit-quit t)
1485 tmp tmp2)
1486 (setq vip-undo-needs-adjustment nil)
1487 (if (listp buffer-undo-list)
1488 (if (setq tmp (memq vip-buffer-undo-list-mark buffer-undo-list))
1489 (progn
1490 (setq tmp2 (cdr tmp)) ; the part after mark
1492 ;; cut tail from buffer-undo-list temporarily by direct
1493 ;; manipulation with pointers in buffer-undo-list
1494 (setcdr tmp nil)
1496 (setq buffer-undo-list (delq nil buffer-undo-list))
1497 (setq buffer-undo-list
1498 (delq vip-buffer-undo-list-mark buffer-undo-list))
1499 ;; restore tail of buffer-undo-list
1500 (setq buffer-undo-list (nconc buffer-undo-list tmp2)))
1501 (setq buffer-undo-list (delq nil buffer-undo-list))))))
1504 (defun vip-set-complex-command-for-undo ()
1505 (if (listp buffer-undo-list)
1506 (if (not vip-undo-needs-adjustment)
1507 (let ((inhibit-quit t))
1508 (setq buffer-undo-list
1509 (cons vip-buffer-undo-list-mark buffer-undo-list))
1510 (setq vip-undo-needs-adjustment t)))))
1515 (defun vip-display-current-destructive-command ()
1516 (let ((text (nth 4 vip-d-com))
1517 (keys (nth 5 vip-d-com))
1518 (max-text-len 30))
1520 (setq this-command 'vip-display-current-destructive-command)
1522 (message " `.' runs %s%s"
1523 (concat "`" (vip-array-to-string keys) "'")
1524 (vip-abbreviate-string text max-text-len
1525 " inserting `" "'" " ......."))
1529 ;; don't change vip-d-com if it was vip-repeat command invoked with `.'
1530 ;; or in some other way (non-interactively).
1531 (defun vip-set-destructive-command (list)
1532 (or (eq vip-intermediate-command 'vip-repeat)
1533 (progn
1534 (setq vip-d-com list)
1535 (setcar (nthcdr 5 vip-d-com)
1536 (vip-array-to-string (this-command-keys)))
1537 (vip-push-onto-ring vip-d-com 'vip-command-ring))))
1539 (defun vip-prev-destructive-command (next)
1540 "Find previous destructive command in the history of destructive commands.
1541 With prefix argument, find next destructive command."
1542 (interactive "P")
1543 (let (cmd vip-intermediate-command)
1544 (if (eq last-command 'vip-display-current-destructive-command)
1545 ;; repeated search through command history
1546 (setq vip-intermediate-command 'repeating-display-destructive-command)
1547 ;; first search through command history--set temp ring
1548 (setq vip-temp-command-ring (copy-list vip-command-ring)))
1549 (setq cmd (if next
1550 (vip-special-ring-rotate1 vip-temp-command-ring 1)
1551 (vip-special-ring-rotate1 vip-temp-command-ring -1)))
1552 (if (null cmd)
1554 (setq vip-d-com cmd))
1555 (vip-display-current-destructive-command)))
1557 (defun vip-next-destructive-command ()
1558 "Find next destructive command in the history of destructive commands."
1559 (interactive)
1560 (vip-prev-destructive-command 'next))
1562 (defun vip-insert-prev-from-insertion-ring (arg)
1563 "Cycle through insertion ring in the direction of older insertions.
1564 Undoes previous insertion and inserts new.
1565 With prefix argument, cycles in the direction of newer elements.
1566 In minibuffer, this command executes whatever the invocation key is bound
1567 to in the global map, instead of cycling through the insertion ring."
1568 (interactive "P")
1569 (let (vip-intermediate-command)
1570 (if (eq last-command 'vip-insert-from-insertion-ring)
1571 (progn ; repeated search through insertion history
1572 (setq vip-intermediate-command 'repeating-insertion-from-ring)
1573 (if (eq vip-current-state 'replace-state)
1574 (undo 1)
1575 (if vip-last-inserted-string-from-insertion-ring
1576 (backward-delete-char
1577 (length vip-last-inserted-string-from-insertion-ring))))
1579 ;;first search through insertion history
1580 (setq vip-temp-insertion-ring (copy-list vip-insertion-ring)))
1581 (setq this-command 'vip-insert-from-insertion-ring)
1582 ;; so that things will be undone properly
1583 (setq buffer-undo-list (cons nil buffer-undo-list))
1584 (setq vip-last-inserted-string-from-insertion-ring
1585 (vip-special-ring-rotate1 vip-temp-insertion-ring (if arg 1 -1)))
1587 ;; this change of vip-intermediate-command must come after
1588 ;; vip-special-ring-rotate1, so that the ring will rotate, but before the
1589 ;; insertion.
1590 (setq vip-intermediate-command nil)
1591 (if vip-last-inserted-string-from-insertion-ring
1592 (insert vip-last-inserted-string-from-insertion-ring))
1595 (defun vip-insert-next-from-insertion-ring ()
1596 "Cycle through insertion ring in the direction of older insertions.
1597 Undo previous insertion and inserts new."
1598 (interactive)
1599 (vip-insert-prev-from-insertion-ring 'next))
1602 ;; some region utilities
1604 ;; If at the last line of buffer, add \\n before eob, if newline is missing.
1605 (defun vip-add-newline-at-eob-if-necessary ()
1606 (save-excursion
1607 (end-of-line)
1608 ;; make sure all lines end with newline, unless in the minibuffer or
1609 ;; when requested otherwise (require-final-newline is nil)
1610 (if (and (eobp)
1611 (not (bolp))
1612 require-final-newline
1613 (not (vip-is-in-minibuffer))
1614 (not buffer-read-only))
1615 (insert "\n"))))
1617 (defun vip-yank-defun ()
1618 (mark-defun)
1619 (copy-region-as-kill (point) (mark t)))
1621 ;; Enlarge region between BEG and END.
1622 (defun vip-enlarge-region (beg end)
1623 (or beg (setq beg end)) ; if beg is nil, set to end
1624 (or end (setq end beg)) ; if end is nil, set to beg
1626 (if (< beg end)
1627 (progn (goto-char beg) (set-mark end))
1628 (goto-char end)
1629 (set-mark beg))
1630 (beginning-of-line)
1631 (exchange-point-and-mark)
1632 (if (or (not (eobp)) (not (bolp))) (forward-line 1))
1633 (if (not (eobp)) (beginning-of-line))
1634 (if (> beg end) (exchange-point-and-mark)))
1637 ;; Quote region by each line with a user supplied string.
1638 (defun vip-quote-region ()
1639 (setq vip-quote-string
1640 (vip-read-string-with-history
1641 "Quote string: "
1643 'vip-quote-region-history
1644 vip-quote-string))
1645 (vip-enlarge-region (point) (mark t))
1646 (if (> (point) (mark t)) (exchange-point-and-mark))
1647 (insert vip-quote-string)
1648 (beginning-of-line)
1649 (forward-line 1)
1650 (while (and (< (point) (mark t)) (bolp))
1651 (insert vip-quote-string)
1652 (beginning-of-line)
1653 (forward-line 1)))
1655 ;; Tells whether BEG is on the same line as END.
1656 ;; If one of the args is nil, it'll return nil.
1657 (defun vip-same-line (beg end)
1658 (let ((selective-display nil)
1659 (incr 0)
1660 temp)
1661 (if (and beg end (> beg end))
1662 (setq temp beg
1663 beg end
1664 end temp))
1665 (if (and beg end)
1666 (cond ((or (> beg (point-max)) (> end (point-max))) ; out of range
1667 nil)
1669 ;; This 'if' is needed because Emacs treats the next empty line
1670 ;; as part of the previous line.
1671 (if (= (vip-line-pos 'start) end)
1672 (setq incr 1))
1673 (<= (+ incr (count-lines beg end)) 1))))
1677 ;; Check if the string ends with a newline.
1678 (defun vip-end-with-a-newline-p (string)
1679 (or (string= string "")
1680 (= (vip-seq-last-elt string) ?\n)))
1682 (defun vip-tmp-insert-at-eob (msg)
1683 (let ((savemax (point-max)))
1684 (goto-char savemax)
1685 (insert msg)
1686 (sit-for 2)
1687 (goto-char savemax) (delete-region (point) (point-max))
1692 ;;; Minibuffer business
1694 (defsubst vip-set-minibuffer-style ()
1695 (add-hook 'minibuffer-setup-hook 'vip-minibuffer-setup-sentinel))
1698 (defun vip-minibuffer-setup-sentinel ()
1699 (let ((hook (if vip-vi-style-in-minibuffer
1700 'vip-change-state-to-insert
1701 'vip-change-state-to-emacs)))
1702 (funcall hook)
1705 ;; Interpret last event in the local map
1706 (defun vip-exit-minibuffer ()
1707 (interactive)
1708 (let (command)
1709 (setq command (local-key-binding (char-to-string last-command-char)))
1710 (if command
1711 (command-execute command)
1712 (exit-minibuffer))))
1715 ;;; Reading string with history
1717 (defun vip-read-string-with-history (prompt &optional initial
1718 history-var default keymap)
1719 ;; Read string, prompting with PROMPT and inserting the INITIAL
1720 ;; value. Uses HISTORY-VAR. DEFAULT is the default value to accept if the
1721 ;; input is an empty string. Use KEYMAP, if given, or the
1722 ;; minibuffer-local-map.
1723 ;; Default value is displayed until the user types something in the
1724 ;; minibuffer.
1725 (let ((minibuffer-setup-hook
1726 '(lambda ()
1727 (if (stringp initial)
1728 (progn
1729 ;; don't wait if we have unread events or in kbd macro
1730 (or unread-command-events
1731 executing-kbd-macro
1732 (sit-for 840))
1733 (erase-buffer)
1734 (insert initial)))
1735 (vip-minibuffer-setup-sentinel)))
1736 (val "")
1737 (padding "")
1738 temp-msg)
1740 (setq keymap (or keymap minibuffer-local-map)
1741 initial (or initial "")
1742 temp-msg (if default
1743 (format "(default: %s) " default)
1744 ""))
1746 (setq vip-incomplete-ex-cmd nil)
1747 (setq val (read-from-minibuffer prompt
1748 (concat temp-msg initial val padding)
1749 keymap nil history-var))
1750 (setq minibuffer-setup-hook nil
1751 padding (vip-array-to-string (this-command-keys))
1752 temp-msg "")
1753 ;; the following tries to be smart about what to put in history
1754 (if (not (string= val (car (eval history-var))))
1755 (set history-var (cons val (eval history-var))))
1756 (if (or (string= (nth 0 (eval history-var)) (nth 1 (eval history-var)))
1757 (string= (nth 0 (eval history-var)) ""))
1758 (set history-var (cdr (eval history-var))))
1759 ;; If the user enters nothing but the prev cmd wasn't vip-ex,
1760 ;; vip-command-argument, or `! shell-command', this probably means
1761 ;; that the user typed something then erased. Return "" in this case, not
1762 ;; the default---the default is too confusing in this case.
1763 (cond ((and (string= val "")
1764 (not (string= prompt "!")) ; was a `! shell-command'
1765 (not (memq last-command
1766 '(vip-ex
1767 vip-command-argument
1771 ((string= val "") (or default ""))
1772 (t val))
1777 ;; insertion commands
1779 ;; Called when state changes from Insert Vi command mode.
1780 ;; Repeats the insertion command if Insert state was entered with prefix
1781 ;; argument > 1.
1782 (defun vip-repeat-insert-command ()
1783 (let ((i-com (car vip-d-com))
1784 (val (nth 1 vip-d-com))
1785 (char (nth 2 vip-d-com)))
1786 (if (and val (> val 1)) ; first check that val is non-nil
1787 (progn
1788 (setq vip-d-com (list i-com (1- val) ?r nil nil nil))
1789 (vip-repeat nil)
1790 (setq vip-d-com (list i-com val char nil nil nil))
1791 ))))
1793 (defun vip-insert (arg)
1794 "Insert before point."
1795 (interactive "P")
1796 (vip-set-complex-command-for-undo)
1797 (let ((val (vip-p-val arg))
1798 (com (vip-getcom arg)))
1799 (vip-set-destructive-command (list 'vip-insert val ?r nil nil nil))
1800 (if com
1801 (vip-loop val (vip-yank-last-insertion))
1802 (vip-change-state-to-insert))))
1804 (defun vip-append (arg)
1805 "Append after point."
1806 (interactive "P")
1807 (vip-set-complex-command-for-undo)
1808 (let ((val (vip-p-val arg))
1809 (com (vip-getcom arg)))
1810 (vip-set-destructive-command (list 'vip-append val ?r nil nil nil))
1811 (if (not (eolp)) (forward-char))
1812 (if (equal com ?r)
1813 (vip-loop val (vip-yank-last-insertion))
1814 (vip-change-state-to-insert))))
1816 (defun vip-Append (arg)
1817 "Append at end of line."
1818 (interactive "P")
1819 (vip-set-complex-command-for-undo)
1820 (let ((val (vip-p-val arg))
1821 (com (vip-getcom arg)))
1822 (vip-set-destructive-command (list 'vip-Append val ?r nil nil nil))
1823 (end-of-line)
1824 (if (equal com ?r)
1825 (vip-loop val (vip-yank-last-insertion))
1826 (vip-change-state-to-insert))))
1828 (defun vip-Insert (arg)
1829 "Insert before first non-white."
1830 (interactive "P")
1831 (vip-set-complex-command-for-undo)
1832 (let ((val (vip-p-val arg))
1833 (com (vip-getcom arg)))
1834 (vip-set-destructive-command (list 'vip-Insert val ?r nil nil nil))
1835 (back-to-indentation)
1836 (if (equal com ?r)
1837 (vip-loop val (vip-yank-last-insertion))
1838 (vip-change-state-to-insert))))
1840 (defun vip-open-line (arg)
1841 "Open line below."
1842 (interactive "P")
1843 (vip-set-complex-command-for-undo)
1844 (let ((val (vip-p-val arg))
1845 (com (vip-getcom arg)))
1846 (vip-set-destructive-command (list 'vip-open-line val ?r nil nil nil))
1847 (let ((col (current-indentation)))
1848 (if (equal com ?r)
1849 (vip-loop val
1850 (progn
1851 (end-of-line)
1852 (newline 1)
1853 (if vip-auto-indent
1854 (progn
1855 (setq vip-cted t)
1856 (if vip-electric-mode
1857 (indent-according-to-mode)
1858 (indent-to col))
1860 (vip-yank-last-insertion)))
1861 (end-of-line)
1862 (newline 1)
1863 (if vip-auto-indent
1864 (progn
1865 (setq vip-cted t)
1866 (if vip-electric-mode
1867 (indent-according-to-mode)
1868 (indent-to col))))
1869 (vip-change-state-to-insert)))))
1871 (defun vip-Open-line (arg)
1872 "Open line above."
1873 (interactive "P")
1874 (vip-set-complex-command-for-undo)
1875 (let ((val (vip-p-val arg))
1876 (com (vip-getcom arg)))
1877 (vip-set-destructive-command (list 'vip-Open-line val ?r nil nil nil))
1878 (let ((col (current-indentation)))
1879 (if (equal com ?r)
1880 (vip-loop val
1881 (progn
1882 (beginning-of-line)
1883 (open-line 1)
1884 (if vip-auto-indent
1885 (progn
1886 (setq vip-cted t)
1887 (if vip-electric-mode
1888 (indent-according-to-mode)
1889 (indent-to col))
1891 (vip-yank-last-insertion)))
1892 (beginning-of-line)
1893 (open-line 1)
1894 (if vip-auto-indent
1895 (progn
1896 (setq vip-cted t)
1897 (if vip-electric-mode
1898 (indent-according-to-mode)
1899 (indent-to col))
1901 (vip-change-state-to-insert)))))
1903 (defun vip-open-line-at-point (arg)
1904 "Open line at point."
1905 (interactive "P")
1906 (vip-set-complex-command-for-undo)
1907 (let ((val (vip-p-val arg))
1908 (com (vip-getcom arg)))
1909 (vip-set-destructive-command
1910 (list 'vip-open-line-at-point val ?r nil nil nil))
1911 (if (equal com ?r)
1912 (vip-loop val
1913 (progn
1914 (open-line 1)
1915 (vip-yank-last-insertion)))
1916 (open-line 1)
1917 (vip-change-state-to-insert))))
1919 (defun vip-substitute (arg)
1920 "Substitute characters."
1921 (interactive "P")
1922 (let ((val (vip-p-val arg))
1923 (com (vip-getcom arg)))
1924 (push-mark nil t)
1925 (forward-char val)
1926 (if (equal com ?r)
1927 (vip-change-subr (mark t) (point))
1928 (vip-change (mark t) (point)))
1929 (vip-set-destructive-command (list 'vip-substitute val ?r nil nil nil))
1932 (defun vip-substitute-line (arg)
1933 "Substitute lines."
1934 (interactive "p")
1935 (vip-set-complex-command-for-undo)
1936 (vip-line (cons arg ?C)))
1938 ;; Prepare for replace
1939 (defun vip-start-replace ()
1940 (setq vip-began-as-replace t
1941 vip-sitting-in-replace t
1942 vip-replace-chars-to-delete 0
1943 vip-replace-chars-deleted 0)
1944 (vip-add-hook 'vip-after-change-functions 'vip-replace-mode-spy-after t)
1945 (vip-add-hook 'vip-before-change-functions 'vip-replace-mode-spy-before t)
1946 ;; this will get added repeatedly, but no harm
1947 (add-hook 'after-change-functions 'vip-after-change-sentinel t)
1948 (add-hook 'before-change-functions 'vip-before-change-sentinel t)
1949 (vip-move-marker-locally 'vip-last-posn-in-replace-region
1950 (vip-replace-start))
1951 (vip-add-hook
1952 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel t)
1953 (vip-add-hook
1954 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
1955 ;; guard against a smartie who switched from R-replace to normal replace
1956 (vip-remove-hook
1957 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
1958 (if overwrite-mode (overwrite-mode nil))
1962 ;; checks how many chars were deleted by the last change
1963 (defun vip-replace-mode-spy-before (beg end)
1964 (setq vip-replace-chars-deleted
1965 (- end beg
1966 (max 0 (- end (vip-replace-end)))
1967 (max 0 (- (vip-replace-start) beg))
1970 ;; Invoked as an after-change-function to set up parameters of the last change
1971 (defun vip-replace-mode-spy-after (beg end length)
1972 (if (memq vip-intermediate-command '(repeating-insertion-from-ring))
1973 (progn
1974 (setq vip-replace-chars-to-delete 0)
1975 (vip-move-marker-locally
1976 'vip-last-posn-in-replace-region (point)))
1978 (let (beg-col end-col real-end chars-to-delete)
1979 (setq real-end (min end (vip-replace-end)))
1980 (save-excursion
1981 (goto-char beg)
1982 (setq beg-col (current-column))
1983 (goto-char real-end)
1984 (setq end-col (current-column)))
1986 ;; If beg of change is outside the replacement region, then don't
1987 ;; delete anything in the repl region (set chars-to-delete to 0).
1989 ;; This works fine except that we have to take special care of
1990 ;; dabbrev-expand. The problem stems from new-dabbrev.el, which
1991 ;; sometimes simply shifts the repl region rightwards, without
1992 ;; deleting an equal amount of characters.
1994 ;; The reason why new-dabbrev.el causes this are this:
1995 ;; if one dinamically completes a partial word that starts before the
1996 ;; replacement region (but ends inside) then new-dabbrev.el first
1997 ;; moves cursor backwards, to the beginning of the word to be
1998 ;; completed (say, pt A). Then it inserts the
1999 ;; completed word and then deletes the old, incomplete part.
2000 ;; Since the complete word is inserted at position before the repl
2001 ;; region, the next If-statement would have set chars-to-delete to 0
2002 ;; unless we check for the current command, which must be
2003 ;; dabbrev-expand.
2005 ;; In fact, it might be also useful to have overlays for insert
2006 ;; regions as well, since this will let us capture the situation when
2007 ;; dabbrev-expand goes back past the insertion point to find the
2008 ;; beginning of the word to be expanded.
2009 (if (or (and (<= (vip-replace-start) beg)
2010 (<= beg (vip-replace-end)))
2011 (and (= length 0) (eq this-command 'dabbrev-expand)))
2012 (setq chars-to-delete
2013 (max (- end-col beg-col) (- real-end beg) 0))
2014 (setq chars-to-delete 0))
2016 ;; if beg = last change position, it means that we are within the
2017 ;; same command that does multiple changes. Moreover, it means
2018 ;; that we have two subsequent changes (insert/delete) that
2019 ;; complement each other.
2020 (if (= beg (marker-position vip-last-posn-in-replace-region))
2021 (setq vip-replace-chars-to-delete
2022 (- (+ chars-to-delete vip-replace-chars-to-delete)
2023 vip-replace-chars-deleted))
2024 (setq vip-replace-chars-to-delete chars-to-delete))
2026 (vip-move-marker-locally
2027 'vip-last-posn-in-replace-region
2028 (max (if (> end (vip-replace-end)) (vip-replace-start) end)
2029 (or (marker-position vip-last-posn-in-replace-region)
2030 (vip-replace-start))
2033 (setq vip-replace-chars-to-delete
2034 (max 0
2035 (min vip-replace-chars-to-delete
2036 (- (vip-replace-end) vip-last-posn-in-replace-region)
2037 (- (vip-line-pos 'end) vip-last-posn-in-replace-region)
2042 ;; Delete stuff between posn and the end of vip-replace-overlay-marker, if
2043 ;; posn is within the overlay.
2044 (defun vip-finish-change (posn)
2045 (vip-remove-hook 'vip-after-change-functions 'vip-replace-mode-spy-after)
2046 (vip-remove-hook 'vip-before-change-functions 'vip-replace-mode-spy-before)
2047 (vip-remove-hook 'vip-post-command-hooks
2048 'vip-replace-state-post-command-sentinel)
2049 (vip-remove-hook
2050 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2051 (vip-restore-cursor-color-after-replace)
2052 (setq vip-sitting-in-replace nil) ; just in case we'll need to know it
2053 (save-excursion
2054 (if (and
2055 vip-replace-overlay
2056 (>= posn (vip-replace-start))
2057 (< posn (vip-replace-end)))
2058 (delete-region posn (vip-replace-end)))
2061 (if (eq vip-current-state 'replace-state)
2062 (vip-downgrade-to-insert))
2063 ;; replace mode ended => nullify vip-last-posn-in-replace-region
2064 (vip-move-marker-locally 'vip-last-posn-in-replace-region nil)
2065 (vip-hide-replace-overlay)
2066 (vip-refresh-mode-line)
2067 (vip-put-string-on-kill-ring vip-last-replace-region)
2070 ;; Make STRING be the first element of the kill ring.
2071 (defun vip-put-string-on-kill-ring (string)
2072 (setq kill-ring (cons string kill-ring))
2073 (if (> (length kill-ring) kill-ring-max)
2074 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
2075 (setq kill-ring-yank-pointer kill-ring))
2077 (defun vip-finish-R-mode ()
2078 (vip-remove-hook 'vip-post-command-hooks 'vip-R-state-post-command-sentinel)
2079 (vip-remove-hook
2080 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel)
2081 (vip-downgrade-to-insert))
2083 (defun vip-start-R-mode ()
2084 ;; Leave arg as 1, not t: XEmacs insists that it must be a pos number
2085 (overwrite-mode 1)
2086 (vip-add-hook
2087 'vip-post-command-hooks 'vip-R-state-post-command-sentinel t)
2088 (vip-add-hook
2089 'vip-pre-command-hooks 'vip-replace-state-pre-command-sentinel t)
2090 ;; guard against a smartie who switched from R-replace to normal replace
2091 (vip-remove-hook
2092 'vip-post-command-hooks 'vip-replace-state-post-command-sentinel)
2097 (defun vip-replace-state-exit-cmd ()
2098 "Binding for keys that cause Replace state to switch to Vi or to Insert.
2099 These keys are ESC, RET, and LineFeed"
2100 (interactive)
2101 (if overwrite-mode ;; If you are in replace mode invoked via 'R'
2102 (vip-finish-R-mode)
2103 (vip-finish-change vip-last-posn-in-replace-region))
2104 (let (com)
2105 (if (eq this-command 'vip-intercept-ESC-key)
2106 (setq com 'vip-exit-insert-state)
2107 (vip-set-unread-command-events last-input-char)
2108 (setq com (key-binding (read-key-sequence nil))))
2110 (condition-case conds
2111 (command-execute com)
2112 (error
2113 (vip-message-conditions conds)))
2115 (vip-hide-replace-overlay))
2117 (defun vip-replace-state-carriage-return ()
2118 "Implements carriage return in Viper replace state."
2119 (interactive)
2120 ;; If Emacs start supporting overlay maps, as it currently supports
2121 ;; text-property maps, we could do away with vip-replace-minor-mode and
2122 ;; just have keymap attached to replace overlay. Then the "if part" of this
2123 ;; statement can be deleted.
2124 (if (or (< (point) (vip-replace-start))
2125 (> (point) (vip-replace-end)))
2126 (let (vip-replace-minor-mode com)
2127 (vip-set-unread-command-events last-input-char)
2128 (setq com (key-binding (read-key-sequence nil)))
2129 (condition-case conds
2130 (command-execute com)
2131 (error
2132 (vip-message-conditions conds))))
2133 (if (not vip-allow-multiline-replace-regions)
2134 (vip-replace-state-exit-cmd)
2135 (if (vip-same-line (point) (vip-replace-end))
2136 (vip-replace-state-exit-cmd)
2137 (vip-kill-line nil)
2138 (vip-next-line-at-bol nil)))))
2141 ;; This is the function bound to 'R'---unlimited replace.
2142 ;; Similar to Emacs's own overwrite-mode.
2143 (defun vip-overwrite (arg)
2144 "Begin overwrite mode."
2145 (interactive "P")
2146 (let ((val (vip-p-val arg))
2147 (com (vip-getcom arg)) (len))
2148 (vip-set-destructive-command (list 'vip-overwrite val ?r nil nil nil))
2149 (if com
2150 (progn
2151 ;; Viper saves inserted text in vip-last-insertion
2152 (setq len (length vip-last-insertion))
2153 (delete-char len)
2154 (vip-loop val (vip-yank-last-insertion)))
2155 (setq last-command 'vip-overwrite)
2156 (vip-set-complex-command-for-undo)
2157 (vip-set-replace-overlay (point) (vip-line-pos 'end))
2158 (vip-change-state-to-replace)
2162 ;; line commands
2164 (defun vip-line (arg)
2165 (let ((val (car arg))
2166 (com (cdr arg)))
2167 (vip-move-marker-locally 'vip-com-point (point))
2168 (if (not (eobp))
2169 (vip-next-line-carefully (1- val)))
2170 ;; this ensures that dd, cc, D, yy will do the right thing on the last
2171 ;; line of buffer when this line has no \n.
2172 (vip-add-newline-at-eob-if-necessary)
2173 (vip-execute-com 'vip-line val com))
2174 (if (and (eobp) (not (bobp))) (forward-line -1))
2177 (defun vip-yank-line (arg)
2178 "Yank ARG lines (in Vi's sense)."
2179 (interactive "P")
2180 (let ((val (vip-p-val arg)))
2181 (vip-line (cons val ?Y))))
2184 ;; region commands
2186 (defun vip-region (arg)
2187 "Execute command on a region."
2188 (interactive "P")
2189 (let ((val (vip-P-val arg))
2190 (com (vip-getcom arg)))
2191 (vip-move-marker-locally 'vip-com-point (point))
2192 (exchange-point-and-mark)
2193 (vip-execute-com 'vip-region val com)))
2195 (defun vip-Region (arg)
2196 "Execute command on a Region."
2197 (interactive "P")
2198 (let ((val (vip-P-val arg))
2199 (com (vip-getCom arg)))
2200 (vip-move-marker-locally 'vip-com-point (point))
2201 (exchange-point-and-mark)
2202 (vip-execute-com 'vip-Region val com)))
2204 (defun vip-replace-char (arg)
2205 "Replace the following ARG chars by the character read."
2206 (interactive "P")
2207 (if (and (eolp) (bolp)) (error "No character to replace here"))
2208 (let ((val (vip-p-val arg))
2209 (com (vip-getcom arg)))
2210 (vip-replace-char-subr com val)
2211 (if (and (eolp) (not (bolp))) (forward-char 1))
2212 (vip-set-destructive-command
2213 (list 'vip-replace-char val ?r nil vip-d-char nil))
2216 (defun vip-replace-char-subr (com arg)
2217 (let ((take-care-of-iso-accents
2218 (and (boundp 'iso-accents-mode) vip-automatic-iso-accents))
2219 char)
2220 (setq char (if (equal com ?r)
2221 vip-d-char
2222 (read-char)))
2223 (if (and take-care-of-iso-accents (memq char '(?' ?\" ?^ ?~)))
2224 ;; get European characters
2225 (progn
2226 (iso-accents-mode 1)
2227 (vip-set-unread-command-events char)
2228 (setq char (aref (read-key-sequence nil) 0))
2229 (iso-accents-mode -1)))
2230 (delete-char arg t)
2231 (setq vip-d-char char)
2232 (vip-loop (if (> arg 0) arg (- arg))
2233 (if (eq char ?\C-m) (insert "\n") (insert char)))
2234 (backward-char arg)))
2237 ;; basic cursor movement. j, k, l, h commands.
2239 (defun vip-forward-char (arg)
2240 "Move point right ARG characters (left if ARG negative).
2241 On reaching end of line, stop and signal error."
2242 (interactive "P")
2243 (vip-leave-region-active)
2244 (let ((val (vip-p-val arg))
2245 (com (vip-getcom arg)))
2246 (if com (vip-move-marker-locally 'vip-com-point (point)))
2247 (if vip-ex-style-motion
2248 (progn
2249 ;; the boundary condition check gets weird here because
2250 ;; forward-char may be the parameter of a delete, and 'dl' works
2251 ;; just like 'x' for the last char on a line, so we have to allow
2252 ;; the forward motion before the 'vip-execute-com', but, of
2253 ;; course, 'dl' doesn't work on an empty line, so we have to
2254 ;; catch that condition before 'vip-execute-com'
2255 (if (and (eolp) (bolp)) (error "") (forward-char val))
2256 (if com (vip-execute-com 'vip-forward-char val com))
2257 (if (eolp) (progn (backward-char 1) (error ""))))
2258 (forward-char val)
2259 (if com (vip-execute-com 'vip-forward-char val com)))))
2261 (defun vip-backward-char (arg)
2262 "Move point left ARG characters (right if ARG negative).
2263 On reaching beginning of line, stop and signal error."
2264 (interactive "P")
2265 (vip-leave-region-active)
2266 (let ((val (vip-p-val arg))
2267 (com (vip-getcom arg)))
2268 (if com (vip-move-marker-locally 'vip-com-point (point)))
2269 (if vip-ex-style-motion
2270 (progn
2271 (if (bolp) (error "") (backward-char val))
2272 (if com (vip-execute-com 'vip-backward-char val com)))
2273 (backward-char val)
2274 (if com (vip-execute-com 'vip-backward-char val com)))))
2276 ;; Like forward-char, but doesn't move at end of buffer.
2277 (defun vip-forward-char-carefully (&optional arg)
2278 (setq arg (or arg 1))
2279 (if (>= (point-max) (+ (point) arg))
2280 (forward-char arg)
2281 (goto-char (point-max))))
2283 ;; Like backward-char, but doesn't move at end of buffer.
2284 (defun vip-backward-char-carefully (&optional arg)
2285 (setq arg (or arg 1))
2286 (if (<= (point-min) (- (point) arg))
2287 (backward-char arg)
2288 (goto-char (point-min))))
2290 (defun vip-next-line-carefully (arg)
2291 (condition-case nil
2292 (next-line arg)
2293 (error nil)))
2297 ;;; Word command
2299 ;; Words are formed from alpha's and nonalphas - <sp>,\t\n are separators
2300 ;; for word movement. When executed with a destructive command, \n is
2301 ;; usually left untouched for the last word.
2302 ;; Viper uses syntax table to determine what is a word and what is a
2303 ;; separator. However, \n is always a separator. Also, if vip-syntax-preference
2304 ;; is 'vi, then `_' is part of the word.
2306 ;; skip only one \n
2307 (defun vip-skip-separators (forward)
2308 (if forward
2309 (progn
2310 (vip-skip-all-separators-forward 'within-line)
2311 (if (looking-at "\n")
2312 (progn
2313 (forward-char)
2314 (vip-skip-all-separators-forward 'within-line))))
2315 (vip-skip-all-separators-backward 'within-line)
2316 (backward-char)
2317 (if (looking-at "\n")
2318 (vip-skip-all-separators-backward 'within-line)
2319 (forward-char))))
2321 (defun vip-forward-word-kernel (val)
2322 (while (> val 0)
2323 (cond ((vip-looking-at-alpha)
2324 (vip-skip-alpha-forward "_")
2325 (vip-skip-separators t))
2326 ((vip-looking-at-separator)
2327 (vip-skip-separators t))
2328 ((not (vip-looking-at-alphasep))
2329 (vip-skip-nonalphasep-forward)
2330 (vip-skip-separators t)))
2331 (setq val (1- val))))
2333 ;; first search backward for pat. Then skip chars backwards using aux-pat
2334 (defun vip-fwd-skip (pat aux-pat lim)
2335 (if (and (save-excursion
2336 (re-search-backward pat lim t))
2337 (= (point) (match-end 0)))
2338 (goto-char (match-beginning 0)))
2339 (skip-chars-backward aux-pat lim)
2340 (if (= (point) lim)
2341 (vip-forward-char-carefully))
2345 (defun vip-forward-word (arg)
2346 "Forward word."
2347 (interactive "P")
2348 (vip-leave-region-active)
2349 (let ((val (vip-p-val arg))
2350 (com (vip-getcom arg)))
2351 (if com (vip-move-marker-locally 'vip-com-point (point)))
2352 (vip-forward-word-kernel val)
2353 (if com (progn
2354 (cond ((memq com (list ?c (- ?c)))
2355 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
2356 ;; Yank words including the whitespace, but not newline
2357 ((memq com (list ?y (- ?y)))
2358 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
2359 ((vip-dotable-command-p com)
2360 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
2361 (vip-execute-com 'vip-forward-word val com)))))
2364 (defun vip-forward-Word (arg)
2365 "Forward word delimited by white characters."
2366 (interactive "P")
2367 (vip-leave-region-active)
2368 (let ((val (vip-p-val arg))
2369 (com (vip-getcom arg)))
2370 (if com (vip-move-marker-locally 'vip-com-point (point)))
2371 (vip-loop val
2372 (progn
2373 (vip-skip-nonseparators 'forward)
2374 (vip-skip-separators t)))
2375 (if com (progn
2376 (cond ((memq com (list ?c (- ?c)))
2377 (vip-fwd-skip "\n[ \t]*" " \t" vip-com-point))
2378 ;; Yank words including the whitespace, but not newline
2379 ((memq com (list ?y (- ?y)))
2380 (vip-fwd-skip "\n[ \t]*" "" vip-com-point))
2381 ((vip-dotable-command-p com)
2382 (vip-fwd-skip "\n[ \t]*" "" vip-com-point)))
2383 (vip-execute-com 'vip-forward-Word val com)))))
2386 ;; this is a bit different from Vi, but Vi's end of word
2387 ;; makes no sense whatsoever
2388 (defun vip-end-of-word-kernel ()
2389 (if (vip-end-of-word-p) (forward-char))
2390 (if (vip-looking-at-separator)
2391 (vip-skip-all-separators-forward))
2393 (cond ((vip-looking-at-alpha) (vip-skip-alpha-forward "_"))
2394 ((not (vip-looking-at-alphasep)) (vip-skip-nonalphasep-forward)))
2395 (vip-backward-char-carefully))
2397 (defun vip-end-of-word-p ()
2398 (or (eobp)
2399 (save-excursion
2400 (cond ((vip-looking-at-alpha)
2401 (forward-char)
2402 (not (vip-looking-at-alpha)))
2403 ((not (vip-looking-at-alphasep))
2404 (forward-char)
2405 (vip-looking-at-alphasep))))))
2408 (defun vip-end-of-word (arg &optional careful)
2409 "Move point to end of current word."
2410 (interactive "P")
2411 (vip-leave-region-active)
2412 (let ((val (vip-p-val arg))
2413 (com (vip-getcom arg)))
2414 (if com (vip-move-marker-locally 'vip-com-point (point)))
2415 (vip-loop val (vip-end-of-word-kernel))
2416 (if com
2417 (progn
2418 (forward-char)
2419 (vip-execute-com 'vip-end-of-word val com)))))
2421 (defun vip-end-of-Word (arg)
2422 "Forward to end of word delimited by white character."
2423 (interactive "P")
2424 (vip-leave-region-active)
2425 (let ((val (vip-p-val arg))
2426 (com (vip-getcom arg)))
2427 (if com (vip-move-marker-locally 'vip-com-point (point)))
2428 (vip-loop val
2429 (progn
2430 (vip-end-of-word-kernel)
2431 (vip-skip-nonseparators 'forward)
2432 (backward-char)))
2433 (if com
2434 (progn
2435 (forward-char)
2436 (vip-execute-com 'vip-end-of-Word val com)))))
2438 (defun vip-backward-word-kernel (val)
2439 (while (> val 0)
2440 (backward-char)
2441 (cond ((vip-looking-at-alpha)
2442 (vip-skip-alpha-backward "_"))
2443 ((vip-looking-at-separator)
2444 (forward-char)
2445 (vip-skip-separators nil)
2446 (backward-char)
2447 (cond ((vip-looking-at-alpha)
2448 (vip-skip-alpha-backward "_"))
2449 ((not (vip-looking-at-alphasep))
2450 (vip-skip-nonalphasep-backward))
2451 (t (forward-char))))
2452 ((not (vip-looking-at-alphasep))
2453 (vip-skip-nonalphasep-backward)))
2454 (setq val (1- val))))
2456 (defun vip-backward-word (arg)
2457 "Backward word."
2458 (interactive "P")
2459 (vip-leave-region-active)
2460 (let ((val (vip-p-val arg))
2461 (com (vip-getcom arg)))
2462 (if com
2463 (let (i)
2464 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2465 (backward-char))
2466 (vip-move-marker-locally 'vip-com-point (point))
2467 (if i (forward-char))))
2468 (vip-backward-word-kernel val)
2469 (if com (vip-execute-com 'vip-backward-word val com))))
2471 (defun vip-backward-Word (arg)
2472 "Backward word delimited by white character."
2473 (interactive "P")
2474 (vip-leave-region-active)
2475 (let ((val (vip-p-val arg))
2476 (com (vip-getcom arg)))
2477 (if com
2478 (let (i)
2479 (if (setq i (save-excursion (backward-char) (looking-at "\n")))
2480 (backward-char))
2481 (vip-move-marker-locally 'vip-com-point (point))
2482 (if i (forward-char))))
2483 (vip-loop val
2484 (progn
2485 (vip-skip-separators nil)
2486 (vip-skip-nonseparators 'backward)))
2487 (if com (vip-execute-com 'vip-backward-Word val com))))
2491 ;; line commands
2493 (defun vip-beginning-of-line (arg)
2494 "Go to beginning of line."
2495 (interactive "P")
2496 (vip-leave-region-active)
2497 (let ((val (vip-p-val arg))
2498 (com (vip-getcom arg)))
2499 (if com (vip-move-marker-locally 'vip-com-point (point)))
2500 (beginning-of-line val)
2501 (if com (vip-execute-com 'vip-beginning-of-line val com))))
2503 (defun vip-bol-and-skip-white (arg)
2504 "Beginning of line at first non-white character."
2505 (interactive "P")
2506 (vip-leave-region-active)
2507 (let ((val (vip-p-val arg))
2508 (com (vip-getcom arg)))
2509 (if com (vip-move-marker-locally 'vip-com-point (point)))
2510 (forward-to-indentation (1- val))
2511 (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
2513 (defun vip-goto-eol (arg)
2514 "Go to end of line."
2515 (interactive "P")
2516 (vip-leave-region-active)
2517 (let ((val (vip-p-val arg))
2518 (com (vip-getcom arg)))
2519 (if com (vip-move-marker-locally 'vip-com-point (point)))
2520 (end-of-line val)
2521 (if com (vip-execute-com 'vip-goto-eol val com))
2522 (if vip-ex-style-motion
2523 (if (and (eolp) (not (bolp))
2524 ;; a fix for vip-change-to-eol
2525 (not (equal vip-current-state 'insert-state)))
2526 (backward-char 1)
2527 ))))
2530 (defun vip-goto-col (arg)
2531 "Go to ARG's column."
2532 (interactive "P")
2533 (vip-leave-region-active)
2534 (let ((val (vip-p-val arg))
2535 (com (vip-getcom arg))
2536 line-len)
2537 (setq line-len (- (vip-line-pos 'end) (vip-line-pos 'start)))
2538 (if com (vip-move-marker-locally 'vip-com-point (point)))
2539 (beginning-of-line)
2540 (forward-char (1- (min line-len val)))
2541 (while (> (current-column) (1- val))
2542 (backward-char 1))
2543 (if com (vip-execute-com 'vip-goto-col val com))
2544 (save-excursion
2545 (end-of-line)
2546 (if (> val (current-column)) (error "")))
2550 (defun vip-next-line (arg)
2551 "Go to next line."
2552 (interactive "P")
2553 (vip-leave-region-active)
2554 (let ((val (vip-p-val arg))
2555 (com (vip-getCom arg)))
2556 (if com (vip-move-marker-locally 'vip-com-point (point)))
2557 (next-line val)
2558 (if vip-ex-style-motion
2559 (if (and (eolp) (not (bolp))) (backward-char 1)))
2560 (setq this-command 'next-line)
2561 (if com (vip-execute-com 'vip-next-line val com))))
2563 (defun vip-next-line-at-bol (arg)
2564 "Next line at beginning of line."
2565 (interactive "P")
2566 (vip-leave-region-active)
2567 (save-excursion
2568 (end-of-line)
2569 (if (eobp) (error "Last line in buffer")))
2570 (let ((val (vip-p-val arg))
2571 (com (vip-getCom arg)))
2572 (if com (vip-move-marker-locally 'vip-com-point (point)))
2573 (forward-line val)
2574 (back-to-indentation)
2575 (if com (vip-execute-com 'vip-next-line-at-bol val com))))
2577 (defun vip-previous-line (arg)
2578 "Go to previous line."
2579 (interactive "P")
2580 (vip-leave-region-active)
2581 (let ((val (vip-p-val arg))
2582 (com (vip-getCom arg)))
2583 (if com (vip-move-marker-locally 'vip-com-point (point)))
2584 (previous-line val)
2585 (if vip-ex-style-motion
2586 (if (and (eolp) (not (bolp))) (backward-char 1)))
2587 (setq this-command 'previous-line)
2588 (if com (vip-execute-com 'vip-previous-line val com))))
2591 (defun vip-previous-line-at-bol (arg)
2592 "Previous line at beginning of line."
2593 (interactive "P")
2594 (vip-leave-region-active)
2595 (save-excursion
2596 (beginning-of-line)
2597 (if (bobp) (error "First line in buffer")))
2598 (let ((val (vip-p-val arg))
2599 (com (vip-getCom arg)))
2600 (if com (vip-move-marker-locally 'vip-com-point (point)))
2601 (forward-line (- val))
2602 (back-to-indentation)
2603 (if com (vip-execute-com 'vip-previous-line val com))))
2605 (defun vip-change-to-eol (arg)
2606 "Change to end of line."
2607 (interactive "P")
2608 (vip-goto-eol (cons arg ?c)))
2610 (defun vip-kill-line (arg)
2611 "Delete line."
2612 (interactive "P")
2613 (vip-goto-eol (cons arg ?d)))
2615 (defun vip-erase-line (arg)
2616 "Erase line."
2617 (interactive "P")
2618 (vip-beginning-of-line (cons arg ?d)))
2621 ;;; Moving around
2623 (defun vip-goto-line (arg)
2624 "Go to ARG's line. Without ARG go to end of buffer."
2625 (interactive "P")
2626 (let ((val (vip-P-val arg))
2627 (com (vip-getCom arg)))
2628 (vip-move-marker-locally 'vip-com-point (point))
2629 (vip-deactivate-mark)
2630 (push-mark nil t)
2631 (if (null val)
2632 (goto-char (point-max))
2633 (goto-char (point-min))
2634 (forward-line (1- val)))
2636 ;; positioning is done twice: before and after command execution
2637 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2638 (back-to-indentation)
2640 (if com (vip-execute-com 'vip-goto-line val com))
2642 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2643 (back-to-indentation)
2646 ;; Find ARG's occurrence of CHAR on the current line.
2647 ;; If FORWARD then search is forward, otherwise backward. OFFSET is used to
2648 ;; adjust point after search.
2649 (defun vip-find-char (arg char forward offset)
2650 (or (char-or-string-p char) (error ""))
2651 (let ((arg (if forward arg (- arg)))
2652 (cmd (if (eq vip-intermediate-command 'vip-repeat)
2653 (nth 5 vip-d-com)
2654 (vip-array-to-string (this-command-keys))))
2655 point)
2656 (save-excursion
2657 (save-restriction
2658 (if (> arg 0)
2659 (narrow-to-region
2660 ;; forward search begins here
2661 (if (eolp) (error "Command `%s': At end of line" cmd) (point))
2662 ;; forward search ends here
2663 (progn (end-of-line) (point)))
2664 (narrow-to-region
2665 ;; backward search begins from here
2666 (if (bolp)
2667 (error "Command `%s': At beginning of line" cmd) (point))
2668 ;; backward search ends here
2669 (progn (beginning-of-line) (point))))
2670 ;; if arg > 0, point is forwarded before search.
2671 (if (> arg 0) (goto-char (1+ (point-min)))
2672 (goto-char (point-max)))
2673 (if (let ((case-fold-search nil))
2674 (search-forward (char-to-string char) nil 0 arg))
2675 (setq point (point))
2676 (error "Command `%s': `%c' not found" cmd char))))
2677 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
2679 (defun vip-find-char-forward (arg)
2680 "Find char on the line.
2681 If called interactively read the char to find from the terminal, and if
2682 called from vip-repeat, the char last used is used. This behaviour is
2683 controlled by the sign of prefix numeric value."
2684 (interactive "P")
2685 (let ((val (vip-p-val arg))
2686 (com (vip-getcom arg))
2687 (cmd-representation (nth 5 vip-d-com)))
2688 (if (> val 0)
2689 ;; this means that the function was called interactively
2690 (setq vip-f-char (read-char)
2691 vip-f-forward t
2692 vip-f-offset nil)
2693 ;; vip-repeat --- set vip-F-char from command-keys
2694 (setq vip-F-char (if (stringp cmd-representation)
2695 (vip-seq-last-elt cmd-representation)
2696 vip-F-char)
2697 vip-f-char vip-F-char)
2698 (setq val (- val)))
2699 (if com (vip-move-marker-locally 'vip-com-point (point)))
2700 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
2701 (setq val (- val))
2702 (if com
2703 (progn
2704 (setq vip-F-char vip-f-char) ; set new vip-F-char
2705 (forward-char)
2706 (vip-execute-com 'vip-find-char-forward val com)))))
2708 (defun vip-goto-char-forward (arg)
2709 "Go up to char ARG forward on line."
2710 (interactive "P")
2711 (let ((val (vip-p-val arg))
2712 (com (vip-getcom arg))
2713 (cmd-representation (nth 5 vip-d-com)))
2714 (if (> val 0)
2715 ;; this means that the function was called interactively
2716 (setq vip-f-char (read-char)
2717 vip-f-forward t
2718 vip-f-offset t)
2719 ;; vip-repeat --- set vip-F-char from command-keys
2720 (setq vip-F-char (if (stringp cmd-representation)
2721 (vip-seq-last-elt cmd-representation)
2722 vip-F-char)
2723 vip-f-char vip-F-char)
2724 (setq val (- val)))
2725 (if com (vip-move-marker-locally 'vip-com-point (point)))
2726 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
2727 (setq val (- val))
2728 (if com
2729 (progn
2730 (setq vip-F-char vip-f-char) ; set new vip-F-char
2731 (forward-char)
2732 (vip-execute-com 'vip-goto-char-forward val com)))))
2734 (defun vip-find-char-backward (arg)
2735 "Find char ARG on line backward."
2736 (interactive "P")
2737 (let ((val (vip-p-val arg))
2738 (com (vip-getcom arg))
2739 (cmd-representation (nth 5 vip-d-com)))
2740 (if (> val 0)
2741 ;; this means that the function was called interactively
2742 (setq vip-f-char (read-char)
2743 vip-f-forward nil
2744 vip-f-offset nil)
2745 ;; vip-repeat --- set vip-F-char from command-keys
2746 (setq vip-F-char (if (stringp cmd-representation)
2747 (vip-seq-last-elt cmd-representation)
2748 vip-F-char)
2749 vip-f-char vip-F-char)
2750 (setq val (- val)))
2751 (if com (vip-move-marker-locally 'vip-com-point (point)))
2752 (vip-find-char
2753 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
2754 (setq val (- val))
2755 (if com
2756 (progn
2757 (setq vip-F-char vip-f-char) ; set new vip-F-char
2758 (vip-execute-com 'vip-find-char-backward val com)))))
2760 (defun vip-goto-char-backward (arg)
2761 "Go up to char ARG backward on line."
2762 (interactive "P")
2763 (let ((val (vip-p-val arg))
2764 (com (vip-getcom arg))
2765 (cmd-representation (nth 5 vip-d-com)))
2766 (if (> val 0)
2767 ;; this means that the function was called interactively
2768 (setq vip-f-char (read-char)
2769 vip-f-forward nil
2770 vip-f-offset t)
2771 ;; vip-repeat --- set vip-F-char from command-keys
2772 (setq vip-F-char (if (stringp cmd-representation)
2773 (vip-seq-last-elt cmd-representation)
2774 vip-F-char)
2775 vip-f-char vip-F-char)
2776 (setq val (- val)))
2777 (if com (vip-move-marker-locally 'vip-com-point (point)))
2778 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
2779 (setq val (- val))
2780 (if com
2781 (progn
2782 (setq vip-F-char vip-f-char) ; set new vip-F-char
2783 (vip-execute-com 'vip-goto-char-backward val com)))))
2785 (defun vip-repeat-find (arg)
2786 "Repeat previous find command."
2787 (interactive "P")
2788 (let ((val (vip-p-val arg))
2789 (com (vip-getcom arg)))
2790 (vip-deactivate-mark)
2791 (if com (vip-move-marker-locally 'vip-com-point (point)))
2792 (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
2793 (if com
2794 (progn
2795 (if vip-f-forward (forward-char))
2796 (vip-execute-com 'vip-repeat-find val com)))))
2798 (defun vip-repeat-find-opposite (arg)
2799 "Repeat previous find command in the opposite direction."
2800 (interactive "P")
2801 (let ((val (vip-p-val arg))
2802 (com (vip-getcom arg)))
2803 (vip-deactivate-mark)
2804 (if com (vip-move-marker-locally 'vip-com-point (point)))
2805 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
2806 (if com
2807 (progn
2808 (if vip-f-forward (forward-char))
2809 (vip-execute-com 'vip-repeat-find-opposite val com)))))
2812 ;; window scrolling etc.
2814 (defun vip-other-window (arg)
2815 "Switch to other window."
2816 (interactive "p")
2817 (other-window arg)
2818 (or (not (eq vip-current-state 'emacs-state))
2819 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
2820 (vip-change-state-to-vi)))
2822 (defun vip-window-top (arg)
2823 "Go to home window line."
2824 (interactive "P")
2825 (let ((val (vip-p-val arg))
2826 (com (vip-getCom arg)))
2827 (if com (vip-move-marker-locally 'vip-com-point (point)))
2828 (push-mark nil t)
2829 (move-to-window-line (1- val))
2831 ;; positioning is done twice: before and after command execution
2832 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2833 (back-to-indentation)
2835 (if com (vip-execute-com 'vip-window-top val com))
2837 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2838 (back-to-indentation)
2841 (defun vip-window-middle (arg)
2842 "Go to middle window line."
2843 (interactive "P")
2844 (let ((val (vip-p-val arg))
2845 (com (vip-getCom arg))
2846 lines)
2847 (if com (vip-move-marker-locally 'vip-com-point (point)))
2848 (push-mark nil t)
2849 (if (not (pos-visible-in-window-p (point-max)))
2850 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
2851 (setq lines (count-lines (window-start) (point-max)))
2852 (move-to-window-line (+ (/ lines 2) (1- val))))
2854 ;; positioning is done twice: before and after command execution
2855 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2856 (back-to-indentation)
2858 (if com (vip-execute-com 'vip-window-middle val com))
2860 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2861 (back-to-indentation)
2864 (defun vip-window-bottom (arg)
2865 "Go to last window line."
2866 (interactive "P")
2867 (let ((val (vip-p-val arg))
2868 (com (vip-getCom arg)))
2869 (if com (vip-move-marker-locally 'vip-com-point (point)))
2870 (push-mark nil t)
2871 (move-to-window-line (- val))
2873 ;; positioning is done twice: before and after command execution
2874 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2875 (back-to-indentation)
2877 (if com (vip-execute-com 'vip-window-bottom val com))
2879 (if (and (eobp) (bolp) (not (bobp))) (forward-line -1))
2880 (back-to-indentation)
2883 (defun vip-line-to-top (arg)
2884 "Put current line on the home line."
2885 (interactive "p")
2886 (recenter (1- arg)))
2888 (defun vip-line-to-middle (arg)
2889 "Put current line on the middle line."
2890 (interactive "p")
2891 (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
2893 (defun vip-line-to-bottom (arg)
2894 "Put current line on the last line."
2895 (interactive "p")
2896 (recenter (- (window-height) (1+ arg))))
2898 ;; If point is within vip-search-scroll-threshold of window top or bottom,
2899 ;; scroll up or down 1/7 of window height, depending on whether we are at the
2900 ;; bottom or at the top of the window. This function is called by vip-search
2901 ;; (which is called from vip-search-forward/backward/next). If the value of
2902 ;; vip-search-scroll-threshold is negative - don't scroll.
2903 (defun vip-adjust-window ()
2904 (let ((win-height (if vip-emacs-p
2905 (1- (window-height)) ; adjust for modeline
2906 (window-displayed-height)))
2907 (pt (point))
2908 at-top-p at-bottom-p
2909 min-scroll direction)
2910 (save-excursion
2911 (move-to-window-line 0) ; top
2912 (setq at-top-p
2913 (<= (count-lines pt (point))
2914 vip-search-scroll-threshold))
2915 (move-to-window-line -1) ; bottom
2916 (setq at-bottom-p
2917 (<= (count-lines pt (point)) vip-search-scroll-threshold))
2919 (cond (at-top-p (setq min-scroll (1- vip-search-scroll-threshold)
2920 direction 1))
2921 (at-bottom-p (setq min-scroll (1+ vip-search-scroll-threshold)
2922 direction -1)))
2923 (if min-scroll
2924 (recenter
2925 (* (max min-scroll (/ win-height 7)) direction)))
2929 ;; paren match
2930 ;; must correct this to only match ( to ) etc. On the other hand
2931 ;; it is good that paren match gets confused, because that way you
2932 ;; catch _all_ imbalances.
2934 (defun vip-paren-match (arg)
2935 "Go to the matching parenthesis."
2936 (interactive "P")
2937 (vip-leave-region-active)
2938 (let ((com (vip-getcom arg))
2939 (parse-sexp-ignore-comments vip-parse-sexp-ignore-comments)
2940 anchor-point)
2941 (if (integerp arg)
2942 (if (or (> arg 99) (< arg 1))
2943 (error "Prefix must be between 1 and 99")
2944 (goto-char
2945 (if (> (point-max) 80000)
2946 (* (/ (point-max) 100) arg)
2947 (/ (* (point-max) arg) 100)))
2948 (back-to-indentation))
2949 (let (beg-lim end-lim)
2950 (if (and (eolp) (not (bolp))) (forward-char -1))
2951 (if (not (looking-at "[][(){}]"))
2952 (setq anchor-point (point)))
2953 (save-excursion
2954 (beginning-of-line)
2955 (setq beg-lim (point))
2956 (end-of-line)
2957 (setq end-lim (point)))
2958 (cond ((re-search-forward "[][(){}]" end-lim t)
2959 (backward-char) )
2960 ((re-search-backward "[][(){}]" beg-lim t))
2962 (error "No matching character on line"))))
2963 (cond ((looking-at "[\(\[{]")
2964 (if com (vip-move-marker-locally 'vip-com-point (point)))
2965 (forward-sexp 1)
2966 (if com
2967 (vip-execute-com 'vip-paren-match nil com)
2968 (backward-char)))
2969 (anchor-point
2970 (if com
2971 (progn
2972 (vip-move-marker-locally 'vip-com-point anchor-point)
2973 (forward-char 1)
2974 (vip-execute-com 'vip-paren-match nil com)
2976 ((looking-at "[])}]")
2977 (forward-char)
2978 (if com (vip-move-marker-locally 'vip-com-point (point)))
2979 (backward-sexp 1)
2980 (if com (vip-execute-com 'vip-paren-match nil com)))
2981 (t (error ""))))))
2983 (defun vip-toggle-parse-sexp-ignore-comments ()
2984 (interactive)
2985 (setq vip-parse-sexp-ignore-comments (not vip-parse-sexp-ignore-comments))
2986 (prin1 (format "`%%' will %signore parentheses inside the comments"
2987 (if vip-parse-sexp-ignore-comments "" "NOT ")))
2991 ;; sentence ,paragraph and heading
2993 (defun vip-forward-sentence (arg)
2994 "Forward sentence."
2995 (interactive "P")
2996 (push-mark nil t)
2997 (let ((val (vip-p-val arg))
2998 (com (vip-getcom arg)))
2999 (if com (vip-move-marker-locally 'vip-com-point (point)))
3000 (forward-sentence val)
3001 (if com (vip-execute-com 'vip-forward-sentence nil com))))
3003 (defun vip-backward-sentence (arg)
3004 "Backward sentence."
3005 (interactive "P")
3006 (push-mark nil t)
3007 (let ((val (vip-p-val arg))
3008 (com (vip-getcom arg)))
3009 (if com (vip-move-marker-locally 'vip-com-point (point)))
3010 (backward-sentence val)
3011 (if com (vip-execute-com 'vip-backward-sentence nil com))))
3013 (defun vip-forward-paragraph (arg)
3014 "Forward paragraph."
3015 (interactive "P")
3016 (push-mark nil t)
3017 (let ((val (vip-p-val arg))
3018 (com (vip-getCom arg)))
3019 (if com (vip-move-marker-locally 'vip-com-point (point)))
3020 (forward-paragraph val)
3021 (if com
3022 (progn
3023 (backward-char 1)
3024 (vip-execute-com 'vip-forward-paragraph nil com)))))
3026 (defun vip-backward-paragraph (arg)
3027 "Backward paragraph."
3028 (interactive "P")
3029 (push-mark nil t)
3030 (let ((val (vip-p-val arg))
3031 (com (vip-getCom arg)))
3032 (if com (vip-move-marker-locally 'vip-com-point (point)))
3033 (backward-paragraph val)
3034 (if com
3035 (progn
3036 (forward-char 1)
3037 (vip-execute-com 'vip-backward-paragraph nil com)
3038 (backward-char 1)))))
3040 ;; should be mode-specific etc.
3042 (defun vip-prev-heading (arg)
3043 (interactive "P")
3044 (let ((val (vip-p-val arg))
3045 (com (vip-getCom arg)))
3046 (if com (vip-move-marker-locally 'vip-com-point (point)))
3047 (re-search-backward vip-heading-start nil t val)
3048 (goto-char (match-beginning 0))
3049 (if com (vip-execute-com 'vip-prev-heading nil com))))
3051 (defun vip-heading-end (arg)
3052 (interactive "P")
3053 (let ((val (vip-p-val arg))
3054 (com (vip-getCom arg)))
3055 (if com (vip-move-marker-locally 'vip-com-point (point)))
3056 (re-search-forward vip-heading-end nil t val)
3057 (goto-char (match-beginning 0))
3058 (if com (vip-execute-com 'vip-heading-end nil com))))
3060 (defun vip-next-heading (arg)
3061 (interactive "P")
3062 (let ((val (vip-p-val arg))
3063 (com (vip-getCom arg)))
3064 (if com (vip-move-marker-locally 'vip-com-point (point)))
3065 (end-of-line)
3066 (re-search-forward vip-heading-start nil t val)
3067 (goto-char (match-beginning 0))
3068 (if com (vip-execute-com 'vip-next-heading nil com))))
3071 ;; scrolling
3073 (defun vip-scroll-screen (arg)
3074 "Scroll to next screen."
3075 (interactive "p")
3076 (condition-case nil
3077 (if (> arg 0)
3078 (while (> arg 0)
3079 (scroll-up)
3080 (setq arg (1- arg)))
3081 (while (> 0 arg)
3082 (scroll-down)
3083 (setq arg (1+ arg))))
3084 (error (beep 1)
3085 (if (> arg 0)
3086 (progn
3087 (message "End of buffer")
3088 (goto-char (point-max)))
3089 (message "Beginning of buffer")
3090 (goto-char (point-min))))
3093 (defun vip-scroll-screen-back (arg)
3094 "Scroll to previous screen."
3095 (interactive "p")
3096 (vip-scroll-screen (- arg)))
3098 (defun vip-scroll-down (arg)
3099 "Pull down half screen."
3100 (interactive "P")
3101 (condition-case nil
3102 (if (null arg)
3103 (scroll-down (/ (window-height) 2))
3104 (scroll-down arg))
3105 (error (beep 1)
3106 (message "Beginning of buffer")
3107 (goto-char (point-min)))))
3109 (defun vip-scroll-down-one (arg)
3110 "Scroll up one line."
3111 (interactive "p")
3112 (scroll-down arg))
3114 (defun vip-scroll-up (arg)
3115 "Pull up half screen."
3116 (interactive "P")
3117 (condition-case nil
3118 (if (null arg)
3119 (scroll-up (/ (window-height) 2))
3120 (scroll-up arg))
3121 (error (beep 1)
3122 (message "End of buffer")
3123 (goto-char (point-max)))))
3125 (defun vip-scroll-up-one (arg)
3126 "Scroll down one line."
3127 (interactive "p")
3128 (scroll-up arg))
3131 ;; searching
3133 (defun vip-if-string (prompt)
3134 (let ((s (vip-read-string-with-history
3135 prompt
3136 nil ; no initial
3137 'vip-search-history
3138 (car vip-search-history))))
3139 (if (not (string= s ""))
3140 (setq vip-s-string s))))
3143 (defun vip-toggle-search-style (arg)
3144 "Toggle the value of vip-case-fold-search/vip-re-search.
3145 Without prefix argument, will ask which search style to toggle. With prefix
3146 arg 1,toggles vip-case-fold-search; with arg 2 toggles vip-re-search.
3148 Although this function is bound to \\[vip-toggle-search-style], the most
3149 convenient way to use it is to bind `//' to the macro
3150 `1 M-x vip-toggle-search-style' and `///' to
3151 `2 M-x vip-toggle-search-style'. In this way, hitting `//' quickly will
3152 toggle case-fold-search and hitting `/' three times witth toggle regexp
3153 search. Macros are more convenient in this case because they don't affect
3154 the Emacs binding of `/'."
3155 (interactive "P")
3156 (let (msg)
3157 (cond ((or (eq arg 1)
3158 (and (null arg)
3159 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3160 (if vip-case-fold-search
3161 "case-insensitive" "case-sensitive")
3162 (if vip-case-fold-search
3163 "case-sensitive"
3164 "case-insensitive")))))
3165 (setq vip-case-fold-search (null vip-case-fold-search))
3166 (if vip-case-fold-search
3167 (setq msg "Search becomes case-insensitive")
3168 (setq msg "Search becomes case-sensitive")))
3169 ((or (eq arg 2)
3170 (and (null arg)
3171 (y-or-n-p (format "Search style: '%s'. Want '%s'? "
3172 (if vip-re-search
3173 "regexp-search" "vanilla-search")
3174 (if vip-re-search
3175 "vanilla-search"
3176 "regexp-search")))))
3177 (setq vip-re-search (null vip-re-search))
3178 (if vip-re-search
3179 (setq msg "Search becomes regexp-style")
3180 (setq msg "Search becomes vanilla-style")))
3182 (setq msg "Search style remains unchanged")))
3183 (prin1 msg t)))
3185 (defun vip-set-vi-search-style-macros (unset)
3186 "Set the macros for toggling the search style in Viper's vi-state.
3187 The macro that toggles case sensitivity is bound to `//', and the one that
3188 toggles regexp search is bound to `///'.
3189 With a prefix argument, this function unsets the macros. "
3190 (interactive "P")
3191 (or noninteractive
3192 (if (not unset)
3193 (progn
3194 ;; toggle case sensitivity in search
3195 (vip-record-kbd-macro
3196 "//" 'vi-state
3197 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3199 ;; toggle regexp/vanila search
3200 (vip-record-kbd-macro
3201 "///" 'vi-state
3202 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3204 (if (interactive-p)
3205 (message
3206 "// and /// now toggle case-sensitivity and regexp search.")))
3207 (vip-unrecord-kbd-macro "//" 'vi-state)
3208 (sit-for 2)
3209 (vip-unrecord-kbd-macro "///" 'vi-state))))
3211 (defun vip-set-emacs-search-style-macros (unset &optional arg-majormode)
3212 "Set the macros for toggling the search style in Viper's emacs-state.
3213 The macro that toggles case sensitivity is bound to `//', and the one that
3214 toggles regexp search is bound to `///'.
3215 With a prefix argument, this function unsets the macros.
3216 If the optional prefix argument is non-nil and specifies a valid major mode,
3217 this sets the macros only in the macros in that major mode. Otherwise,
3218 the macros are set in the current major mode.
3219 \(When unsetting the macros, the second argument has no effect.\)"
3220 (interactive "P")
3221 (or noninteractive
3222 (if (not unset)
3223 (progn
3224 ;; toggle case sensitivity in search
3225 (vip-record-kbd-macro
3226 "//" 'emacs-state
3227 [1 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3228 (or arg-majormode major-mode))
3229 ;; toggle regexp/vanila search
3230 (vip-record-kbd-macro
3231 "///" 'emacs-state
3232 [2 (meta x) v i p - t o g g l e - s e a r c h - s t y l e return]
3233 (or arg-majormode major-mode))
3234 (if (interactive-p)
3235 (message
3236 "// and /// now toggle case-sensitivity and regexp search.")))
3237 (vip-unrecord-kbd-macro "//" 'emacs-state)
3238 (sit-for 2)
3239 (vip-unrecord-kbd-macro "///" 'emacs-state))))
3242 (defun vip-search-forward (arg)
3243 "Search a string forward.
3244 ARG is used to find the ARG's occurrence of the string.
3245 Null string will repeat previous search."
3246 (interactive "P")
3247 (let ((val (vip-P-val arg))
3248 (com (vip-getcom arg))
3249 (old-str vip-s-string))
3250 (setq vip-s-forward t)
3251 (vip-if-string "/")
3252 ;; this is not used at present, but may be used later
3253 (if (or (not (equal old-str vip-s-string))
3254 (not (markerp vip-local-search-start-marker))
3255 (not (marker-buffer vip-local-search-start-marker)))
3256 (setq vip-local-search-start-marker (point-marker)))
3257 (vip-search vip-s-string t val)
3258 (if com
3259 (progn
3260 (vip-move-marker-locally 'vip-com-point (mark t))
3261 (vip-execute-com 'vip-search-next val com)))))
3263 (defun vip-search-backward (arg)
3264 "Search a string backward.
3265 ARG is used to find the ARG's occurrence of the string.
3266 Null string will repeat previous search."
3267 (interactive "P")
3268 (let ((val (vip-P-val arg))
3269 (com (vip-getcom arg))
3270 (old-str vip-s-string))
3271 (setq vip-s-forward nil)
3272 (vip-if-string "?")
3273 ;; this is not used at present, but may be used later
3274 (if (or (not (equal old-str vip-s-string))
3275 (not (markerp vip-local-search-start-marker))
3276 (not (marker-buffer vip-local-search-start-marker)))
3277 (setq vip-local-search-start-marker (point-marker)))
3278 (vip-search vip-s-string nil val)
3279 (if com
3280 (progn
3281 (vip-move-marker-locally 'vip-com-point (mark t))
3282 (vip-execute-com 'vip-search-next val com)))))
3285 ;; Search for COUNT's occurrence of STRING.
3286 ;; Search is forward if FORWARD is non-nil, otherwise backward.
3287 ;; INIT-POINT is the position where search is to start.
3288 ;; Arguments:
3289 ;; (STRING FORW COUNT &optional NO-OFFSET INIT-POINT LIMIT FAIL-IF-NOT-FOUND)
3290 (defun vip-search (string forward arg
3291 &optional no-offset init-point fail-if-not-found)
3292 (if (not (equal string ""))
3293 (let ((val (vip-p-val arg))
3294 (com (vip-getcom arg))
3295 (offset (not no-offset))
3296 (case-fold-search vip-case-fold-search)
3297 (start-point (or init-point (point))))
3298 (vip-deactivate-mark)
3299 (if forward
3300 (condition-case nil
3301 (progn
3302 (if offset (vip-forward-char-carefully))
3303 (if vip-re-search
3304 (progn
3305 (re-search-forward string nil nil val)
3306 (re-search-backward string))
3307 (search-forward string nil nil val)
3308 (search-backward string))
3309 (if (not (equal start-point (point)))
3310 (push-mark start-point t)))
3311 (search-failed
3312 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
3313 (progn
3314 (message "Search wrapped around BOTTOM of buffer")
3315 (goto-char (point-min))
3316 (vip-search string forward (cons 1 com) t start-point 'fail)
3317 ;; don't wait in macros
3318 (or executing-kbd-macro (sit-for 2))
3319 ;; delete the wrap-around message
3320 (message "")
3322 (goto-char start-point)
3323 (error "`%s': %s not found"
3324 string
3325 (if vip-re-search "Pattern" "String"))
3327 ;; backward
3328 (condition-case nil
3329 (progn
3330 (if vip-re-search
3331 (re-search-backward string nil nil val)
3332 (search-backward string nil nil val))
3333 (if (not (equal start-point (point)))
3334 (push-mark start-point t)))
3335 (search-failed
3336 (if (and (not fail-if-not-found) vip-search-wrap-around-t)
3337 (progn
3338 (message "Search wrapped around TOP of buffer")
3339 (goto-char (point-max))
3340 (vip-search string forward (cons 1 com) t start-point 'fail)
3341 ;; don't wait in macros
3342 (or executing-kbd-macro (sit-for 2))
3343 ;; delete the wrap-around message
3344 (message "")
3346 (goto-char start-point)
3347 (error "`%s': %s not found"
3348 string
3349 (if vip-re-search "Pattern" "String"))
3350 ))))
3351 ;; pull up or down if at top/bottom of window
3352 (vip-adjust-window)
3353 ;; highlight the result of search
3354 ;; don't wait and don't highlight in macros
3355 (or executing-kbd-macro
3356 vip-inside-command-argument-action
3357 (vip-flash-search-pattern))
3360 (defun vip-search-next (arg)
3361 "Repeat previous search."
3362 (interactive "P")
3363 (let ((val (vip-p-val arg))
3364 (com (vip-getcom arg)))
3365 (if (null vip-s-string) (error vip-NoPrevSearch))
3366 (vip-search vip-s-string vip-s-forward arg)
3367 (if com
3368 (progn
3369 (vip-move-marker-locally 'vip-com-point (mark t))
3370 (vip-execute-com 'vip-search-next val com)))))
3372 (defun vip-search-Next (arg)
3373 "Repeat previous search in the reverse direction."
3374 (interactive "P")
3375 (let ((val (vip-p-val arg))
3376 (com (vip-getcom arg)))
3377 (if (null vip-s-string) (error vip-NoPrevSearch))
3378 (vip-search vip-s-string (not vip-s-forward) arg)
3379 (if com
3380 (progn
3381 (vip-move-marker-locally 'vip-com-point (mark t))
3382 (vip-execute-com 'vip-search-Next val com)))))
3385 ;; Search contents of buffer defined by one of Viper's motion commands.
3386 ;; Repeatable via `n' and `N'.
3387 (defun vip-buffer-search-enable (&optional c)
3388 (cond (c (setq vip-buffer-search-char c))
3389 ((null vip-buffer-search-char)
3390 (setq vip-buffer-search-char ?g)))
3391 (define-key vip-vi-basic-map
3392 (char-to-string vip-buffer-search-char) 'vip-command-argument)
3393 (aset vip-exec-array vip-buffer-search-char 'vip-exec-buffer-search)
3394 (setq vip-prefix-commands (cons vip-buffer-search-char vip-prefix-commands)))
3396 ;; This is a Viper wraper for isearch-forward.
3397 (defun vip-isearch-forward (arg)
3398 "Do incremental search forward."
3399 (interactive "P")
3400 ;; emacs bug workaround
3401 (if (listp arg) (setq arg (car arg)))
3402 (vip-exec-form-in-emacs (list 'isearch-forward arg)))
3404 ;; This is a Viper wraper for isearch-backward."
3405 (defun vip-isearch-backward (arg)
3406 "Do incremental search backward."
3407 (interactive "P")
3408 ;; emacs bug workaround
3409 (if (listp arg) (setq arg (car arg)))
3410 (vip-exec-form-in-emacs (list 'isearch-backward arg)))
3413 ;; visiting and killing files, buffers
3415 (defun vip-switch-to-buffer ()
3416 "Switch to buffer in the current window."
3417 (interactive)
3418 (let (buffer)
3419 (setq buffer
3420 (read-buffer
3421 (format "Switch to buffer in this window \(%s\): "
3422 (buffer-name (other-buffer (current-buffer))))))
3423 (switch-to-buffer buffer)
3426 (defun vip-switch-to-buffer-other-window ()
3427 "Switch to buffer in another window."
3428 (interactive)
3429 (let (buffer)
3430 (setq buffer
3431 (read-buffer
3432 (format "Switch to buffer in another window \(%s\): "
3433 (buffer-name (other-buffer (current-buffer))))))
3434 (switch-to-buffer-other-window buffer)
3437 (defun vip-kill-buffer ()
3438 "Kill a buffer."
3439 (interactive)
3440 (let (buffer buffer-name)
3441 (setq buffer-name
3442 (read-buffer
3443 (format "Kill buffer \(%s\): "
3444 (buffer-name (current-buffer)))))
3445 (setq buffer
3446 (if (null buffer-name)
3447 (current-buffer)
3448 (get-buffer buffer-name)))
3449 (if (null buffer) (error "`%s': No such buffer" buffer-name))
3450 (if (or (not (buffer-modified-p buffer))
3451 (y-or-n-p
3452 (format
3453 "Buffer `%s' is modified, are you sure you want to kill it? "
3454 buffer-name)))
3455 (kill-buffer buffer)
3456 (error "Buffer not killed"))))
3459 (defvar vip-smart-suffix-list
3460 '("" "tex" "c" "cc" "C" "el" "java" "html" "htm" "pl" "P" "p")
3461 "*List of suffixes that Viper automatically tries to append to filenames ending with a `.'.
3462 This is useful when you the current directory contains files with the same
3463 prefix and many different suffixes. Usually, only one of the suffixes
3464 represents an editable file. However, file completion will stop at the `.'
3465 The smart suffix feature lets you hit RET in such a case, and Viper will
3466 select the appropriate suffix.
3468 Suffixes are tried in the order given and the first suffix for which a
3469 corresponding file exists is selected. If no file exists for any of the
3470 suffixes, the user is asked to confirm.
3472 To turn this feature off, set this variable to nil.")
3474 ;; Try to add suffix to files ending with a `.'
3475 ;; Useful when the user hits RET on a non-completed file name.
3476 (defun vip-file-add-suffix ()
3477 (let ((count 0)
3478 (len (length vip-smart-suffix-list))
3479 (file (buffer-string))
3480 found key cmd suff)
3481 (goto-char (point-max))
3482 (if (and vip-smart-suffix-list (string-match "\\.$" file))
3483 (progn
3484 (while (and (not found) (< count len))
3485 (setq suff (nth count vip-smart-suffix-list)
3486 count (1+ count))
3487 (if (file-exists-p (format "%s%s" file suff))
3488 (progn
3489 (setq found t)
3490 (insert suff))))
3492 (if found
3494 (vip-tmp-insert-at-eob " [Please complete file name]")
3495 (unwind-protect
3496 (while (not (memq cmd '(exit-minibuffer vip-exit-minibuffer)))
3497 (setq cmd
3498 (key-binding (setq key (read-key-sequence nil))))
3499 (cond ((eq cmd 'self-insert-command)
3500 (if vip-xemacs-p
3501 (insert (events-to-keys key))
3502 (insert key)))
3503 ((memq cmd '(exit-minibuffer vip-exit-minibuffer))
3504 nil)
3505 (t (command-execute cmd)))
3513 ;; yank and pop
3515 (defsubst vip-yank (text)
3516 "Yank TEXT silently. This works correctly with Emacs's yank-pop command."
3517 (insert text)
3518 (setq this-command 'yank))
3520 (defun vip-put-back (arg)
3521 "Put back after point/below line."
3522 (interactive "P")
3523 (let ((val (vip-p-val arg))
3524 (text (if vip-use-register
3525 (cond ((vip-valid-register vip-use-register '(digit))
3526 (current-kill (- vip-use-register ?1) 'do-not-rotate))
3527 ((vip-valid-register vip-use-register)
3528 (get-register (downcase vip-use-register)))
3529 (t (error vip-InvalidRegister vip-use-register)))
3530 (current-kill 0))))
3531 (if (null text)
3532 (if vip-use-register
3533 (let ((reg vip-use-register))
3534 (setq vip-use-register nil)
3535 (error vip-EmptyRegister reg))
3536 (error "")))
3537 (setq vip-use-register nil)
3538 (if (vip-end-with-a-newline-p text)
3539 (progn
3540 (end-of-line)
3541 (if (eobp)
3542 (insert "\n")
3543 (forward-line 1))
3544 (beginning-of-line))
3545 (if (not (eolp)) (vip-forward-char-carefully)))
3546 (set-marker (vip-mark-marker) (point) (current-buffer))
3547 (vip-set-destructive-command
3548 (list 'vip-put-back val nil vip-use-register nil nil))
3549 (vip-loop val (vip-yank text)))
3550 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3551 ;; newline; it leaves the cursor at the beginning when the text contains
3552 ;; a newline
3553 (if (vip-same-line (point) (mark))
3554 (or (= (point) (mark)) (vip-backward-char-carefully))
3555 (exchange-point-and-mark)
3556 (if (bolp)
3557 (back-to-indentation)))
3558 (vip-deactivate-mark))
3560 (defun vip-Put-back (arg)
3561 "Put back at point/above line."
3562 (interactive "P")
3563 (let ((val (vip-p-val arg))
3564 (text (if vip-use-register
3565 (cond ((vip-valid-register vip-use-register '(digit))
3566 (current-kill (- vip-use-register ?1) 'do-not-rotate))
3567 ((vip-valid-register vip-use-register)
3568 (get-register (downcase vip-use-register)))
3569 (t (error vip-InvalidRegister vip-use-register)))
3570 (current-kill 0))))
3571 (if (null text)
3572 (if vip-use-register
3573 (let ((reg vip-use-register))
3574 (setq vip-use-register nil)
3575 (error vip-EmptyRegister reg))
3576 (error "")))
3577 (setq vip-use-register nil)
3578 (if (vip-end-with-a-newline-p text) (beginning-of-line))
3579 (vip-set-destructive-command
3580 (list 'vip-Put-back val nil vip-use-register nil nil))
3581 (set-marker (vip-mark-marker) (point) (current-buffer))
3582 (vip-loop val (vip-yank text)))
3583 ;; Vi puts cursor on the last char when the yanked text doesn't contain a
3584 ;; newline; it leaves the cursor at the beginning when the text contains
3585 ;; a newline
3586 (if (vip-same-line (point) (mark))
3587 (or (= (point) (mark)) (vip-backward-char-carefully))
3588 (exchange-point-and-mark)
3589 (if (bolp)
3590 (back-to-indentation)))
3591 (vip-deactivate-mark))
3594 ;; Copy region to kill-ring.
3595 ;; If BEG and END do not belong to the same buffer, copy empty region.
3596 (defun vip-copy-region-as-kill (beg end)
3597 (condition-case nil
3598 (copy-region-as-kill beg end)
3599 (error (copy-region-as-kill beg beg))))
3602 (defun vip-delete-char (arg)
3603 "Delete character."
3604 (interactive "P")
3605 (let ((val (vip-p-val arg)))
3606 (vip-set-destructive-command (list 'vip-delete-char val nil nil nil nil))
3607 (if (> val 1)
3608 (save-excursion
3609 (let ((here (point)))
3610 (end-of-line)
3611 (if (> val (- (point) here))
3612 (setq val (- (point) here))))))
3613 (if (and (eq val 0) (not vip-ex-style-motion)) (setq val 1))
3614 (if (and vip-ex-style-motion (eolp))
3615 (if (bolp) (error "") (setq val 0))) ; not bol---simply back 1 ch
3616 (if vip-use-register
3617 (progn
3618 (cond ((vip-valid-register vip-use-register '((Letter)))
3619 (vip-append-to-register
3620 (downcase vip-use-register) (point) (- (point) val)))
3621 ((vip-valid-register vip-use-register)
3622 (copy-to-register
3623 vip-use-register (point) (- (point) val) nil))
3624 (t (error vip-InvalidRegister vip-use-register)))
3625 (setq vip-use-register nil)))
3626 (if vip-ex-style-motion
3627 (progn
3628 (delete-char val t)
3629 (if (and (eolp) (not (bolp))) (backward-char 1)))
3630 (if (eolp)
3631 (delete-backward-char val t)
3632 (delete-char val t)))))
3634 (defun vip-delete-backward-char (arg)
3635 "Delete previous character. On reaching beginning of line, stop and beep."
3636 (interactive "P")
3637 (let ((val (vip-p-val arg)))
3638 (vip-set-destructive-command
3639 (list 'vip-delete-backward-char val nil nil nil nil))
3640 (if (> val 1)
3641 (save-excursion
3642 (let ((here (point)))
3643 (beginning-of-line)
3644 (if (> val (- here (point)))
3645 (setq val (- here (point)))))))
3646 (if vip-use-register
3647 (progn
3648 (cond ((vip-valid-register vip-use-register '(Letter))
3649 (vip-append-to-register
3650 (downcase vip-use-register) (point) (+ (point) val)))
3651 ((vip-valid-register vip-use-register)
3652 (copy-to-register
3653 vip-use-register (point) (+ (point) val) nil))
3654 (t (error vip-InvalidRegister vip-use-register)))
3655 (setq vip-use-register nil)))
3656 (if (bolp) (ding)
3657 (delete-backward-char val t))))
3659 (defun vip-del-backward-char-in-insert ()
3660 "Delete 1 char backwards while in insert mode."
3661 (interactive)
3662 (if (and vip-ex-style-editing-in-insert (bolp))
3663 (beep 1)
3664 (delete-backward-char 1 t)))
3666 (defun vip-del-backward-char-in-replace ()
3667 "Delete one character in replace mode.
3668 If `vip-delete-backwards-in-replace' is t, then DEL key actually deletes
3669 charecters. If it is nil, then the cursor just moves backwards, similarly
3670 to Vi. The variable `vip-ex-style-editing-in-insert', if t, doesn't let the
3671 cursor move past the beginning of line."
3672 (interactive)
3673 (cond (vip-delete-backwards-in-replace
3674 (cond ((not (bolp))
3675 (delete-backward-char 1 t))
3676 (vip-ex-style-editing-in-insert
3677 (beep 1))
3678 ((bobp)
3679 (beep 1))
3681 (delete-backward-char 1 t))))
3682 (vip-ex-style-editing-in-insert
3683 (if (bolp)
3684 (beep 1)
3685 (backward-char 1)))
3687 (backward-char 1))))
3691 ;; join lines.
3693 (defun vip-join-lines (arg)
3694 "Join this line to next, if ARG is nil. Otherwise, join ARG lines."
3695 (interactive "*P")
3696 (let ((val (vip-P-val arg)))
3697 (vip-set-destructive-command (list 'vip-join-lines val nil nil nil nil))
3698 (vip-loop (if (null val) 1 (1- val))
3699 (progn
3700 (end-of-line)
3701 (if (not (eobp))
3702 (progn
3703 (forward-line 1)
3704 (delete-region (point) (1- (point)))
3705 (fixup-whitespace)))))))
3708 ;; Replace state
3710 (defun vip-change (beg end)
3711 (if (markerp beg) (setq beg (marker-position beg)))
3712 (if (markerp end) (setq end (marker-position end)))
3713 ;; beg is sometimes (mark t), which may be nil
3714 (or beg (setq beg end))
3716 (vip-set-complex-command-for-undo)
3717 (if vip-use-register
3718 (progn
3719 (copy-to-register vip-use-register beg end nil)
3720 (setq vip-use-register nil)))
3721 (vip-set-replace-overlay beg end)
3722 (setq last-command nil) ; separate repl text from prev kills
3724 (if (= (vip-replace-start) (point-max))
3725 (error "End of buffer"))
3727 (setq vip-last-replace-region
3728 (buffer-substring (vip-replace-start)
3729 (vip-replace-end)))
3731 ;; protect against error while inserting "@" and other disasters
3732 ;; (e.g., read-only buff)
3733 (condition-case conds
3734 (if (or vip-allow-multiline-replace-regions
3735 (vip-same-line (vip-replace-start)
3736 (vip-replace-end)))
3737 (progn
3738 ;; tabs cause problems in replace, so untabify
3739 (goto-char (vip-replace-end))
3740 (insert-before-markers "@") ; put placeholder after the TAB
3741 (untabify (vip-replace-start) (point))
3742 ;; del @, don't put on kill ring
3743 (delete-backward-char 1)
3745 (vip-set-replace-overlay-glyphs
3746 vip-replace-region-start-delimiter
3747 vip-replace-region-end-delimiter)
3748 ;; this move takes care of the last posn in the overlay, which
3749 ;; has to be shifted because of insert. We can't simply insert
3750 ;; "$" before-markers because then overlay-start will shift the
3751 ;; beginning of the overlay in case we are replacing a single
3752 ;; character. This fixes the bug with `s' and `cl' commands.
3753 (vip-move-replace-overlay (vip-replace-start) (point))
3754 (goto-char (vip-replace-start))
3755 (vip-change-state-to-replace t))
3756 (kill-region (vip-replace-start)
3757 (vip-replace-end))
3758 (vip-hide-replace-overlay)
3759 (vip-change-state-to-insert))
3760 (error ;; make sure that the overlay doesn't stay.
3761 ;; go back to the original point
3762 (goto-char (vip-replace-start))
3763 (vip-hide-replace-overlay)
3764 (vip-message-conditions conds))))
3767 (defun vip-change-subr (beg end)
3768 ;; beg is sometimes (mark t), which may be nil
3769 (or beg (setq beg end))
3771 (if vip-use-register
3772 (progn
3773 (copy-to-register vip-use-register beg end nil)
3774 (setq vip-use-register nil)))
3775 (kill-region beg end)
3776 (setq this-command 'vip-change)
3777 (vip-yank-last-insertion))
3779 (defun vip-toggle-case (arg)
3780 "Toggle character case."
3781 (interactive "P")
3782 (let ((val (vip-p-val arg)) (c))
3783 (vip-set-destructive-command (list 'vip-toggle-case val nil nil nil nil))
3784 (while (> val 0)
3785 (setq c (following-char))
3786 (delete-char 1 nil)
3787 (if (eq c (upcase c))
3788 (insert-char (downcase c) 1)
3789 (insert-char (upcase c) 1))
3790 (if (eolp) (backward-char 1))
3791 (setq val (1- val)))))
3794 ;; query replace
3796 (defun vip-query-replace ()
3797 "Query replace.
3798 If a null string is suplied as the string to be replaced,
3799 the query replace mode will toggle between string replace
3800 and regexp replace."
3801 (interactive)
3802 (let (str)
3803 (setq str (vip-read-string-with-history
3804 (if vip-re-query-replace "Query replace regexp: "
3805 "Query replace: ")
3806 nil ; no initial
3807 'vip-replace1-history
3808 (car vip-replace1-history) ; default
3810 (if (string= str "")
3811 (progn
3812 (setq vip-re-query-replace (not vip-re-query-replace))
3813 (message "Query replace mode changed to %s"
3814 (if vip-re-query-replace "regexp replace"
3815 "string replace")))
3816 (if vip-re-query-replace
3817 (query-replace-regexp
3819 (vip-read-string-with-history
3820 (format "Query replace regexp `%s' with: " str)
3821 nil ; no initial
3822 'vip-replace1-history
3823 (car vip-replace1-history) ; default
3825 (query-replace
3827 (vip-read-string-with-history
3828 (format "Query replace `%s' with: " str)
3829 nil ; no initial
3830 'vip-replace1-history
3831 (car vip-replace1-history) ; default
3832 ))))))
3835 ;; marking
3837 (defun vip-mark-beginning-of-buffer ()
3838 "Mark beginning of buffer."
3839 (interactive)
3840 (push-mark (point))
3841 (goto-char (point-min))
3842 (exchange-point-and-mark)
3843 (message "Mark set at the beginning of buffer"))
3845 (defun vip-mark-end-of-buffer ()
3846 "Mark end of buffer."
3847 (interactive)
3848 (push-mark (point))
3849 (goto-char (point-max))
3850 (exchange-point-and-mark)
3851 (message "Mark set at the end of buffer"))
3853 (defun vip-mark-point ()
3854 "Set mark at point of buffer."
3855 (interactive)
3856 (let ((char (read-char)))
3857 (cond ((and (<= ?a char) (<= char ?z))
3858 (point-to-register (1+ (- char ?a))))
3859 ((= char ?<) (vip-mark-beginning-of-buffer))
3860 ((= char ?>) (vip-mark-end-of-buffer))
3861 ((= char ?.) (vip-set-mark-if-necessary))
3862 ((= char ?,) (vip-cycle-through-mark-ring))
3863 ((= char ?D) (mark-defun))
3864 (t (error ""))
3867 ;; Algorithm: If first invocation of this command save mark on ring, goto
3868 ;; mark, M0, and pop the most recent elt from the mark ring into mark,
3869 ;; making it into the new mark, M1.
3870 ;; Push this mark back and set mark to the original point position, p1.
3871 ;; So, if you hit '' or `` then you can return to p1.
3873 ;; If repeated command, pop top elt from the ring into mark and
3874 ;; jump there. This forgets the position, p1, and puts M1 back into mark.
3875 ;; Then we save the current pos, which is M0, jump to M1 and pop M2 from
3876 ;; the ring into mark. Push M2 back on the ring and set mark to M0.
3877 ;; etc.
3878 (defun vip-cycle-through-mark-ring ()
3879 "Visit previous locations on the mark ring.
3880 One can use `` and '' to temporarily jump 1 step back."
3881 (let* ((sv-pt (point)))
3882 ;; if repeated `m,' command, pop the previously saved mark.
3883 ;; Prev saved mark is actually prev saved point. It is used if the
3884 ;; user types `` or '' and is discarded
3885 ;; from the mark ring by the next `m,' command.
3886 ;; In any case, go to the previous or previously saved mark.
3887 ;; Then push the current mark (popped off the ring) and set current
3888 ;; point to be the mark. Current pt as mark is discarded by the next
3889 ;; m, command.
3890 (if (eq last-command 'vip-cycle-through-mark-ring)
3892 ;; save current mark if the first iteration
3893 (setq mark-ring (delete (vip-mark-marker) mark-ring))
3894 (if (mark t)
3895 (push-mark (mark t) t)) )
3896 (pop-mark)
3897 (set-mark-command 1)
3898 ;; don't duplicate mark on the ring
3899 (setq mark-ring (delete (vip-mark-marker) mark-ring))
3900 (push-mark sv-pt t)
3901 (vip-deactivate-mark)
3902 (setq this-command 'vip-cycle-through-mark-ring)
3906 (defun vip-goto-mark (arg)
3907 "Go to mark."
3908 (interactive "P")
3909 (let ((char (read-char))
3910 (com (vip-getcom arg)))
3911 (vip-goto-mark-subr char com nil)))
3913 (defun vip-goto-mark-and-skip-white (arg)
3914 "Go to mark and skip to first non-white character on line."
3915 (interactive "P")
3916 (let ((char (read-char))
3917 (com (vip-getCom arg)))
3918 (vip-goto-mark-subr char com t)))
3920 (defun vip-goto-mark-subr (char com skip-white)
3921 (if (eobp)
3922 (if (bobp)
3923 (error "Empty buffer")
3924 (backward-char 1)))
3925 (cond ((vip-valid-register char '(letter))
3926 (let* ((buff (current-buffer))
3927 (reg (1+ (- char ?a)))
3928 (text-marker (get-register reg)))
3929 (if com (vip-move-marker-locally 'vip-com-point (point)))
3930 (if (not (vip-valid-marker text-marker))
3931 (error vip-EmptyTextmarker char))
3932 (if (and (vip-same-line (point) vip-last-jump)
3933 (= (point) vip-last-jump-ignore))
3934 (push-mark vip-last-jump t)
3935 (push-mark nil t)) ; no msg
3936 (vip-register-to-point reg)
3937 (setq vip-last-jump (point-marker))
3938 (cond (skip-white
3939 (back-to-indentation)
3940 (setq vip-last-jump-ignore (point))))
3941 (if com
3942 (if (equal buff (current-buffer))
3943 (vip-execute-com (if skip-white
3944 'vip-goto-mark-and-skip-white
3945 'vip-goto-mark)
3946 nil com)
3947 (switch-to-buffer buff)
3948 (goto-char vip-com-point)
3949 (vip-change-state-to-vi)
3950 (error "")))))
3951 ((and (not skip-white) (= char ?`))
3952 (if com (vip-move-marker-locally 'vip-com-point (point)))
3953 (if (and (vip-same-line (point) vip-last-jump)
3954 (= (point) vip-last-jump-ignore))
3955 (goto-char vip-last-jump))
3956 (if (null (mark t)) (error "Mark is not set in this buffer"))
3957 (if (= (point) (mark t)) (pop-mark))
3958 (exchange-point-and-mark)
3959 (setq vip-last-jump (point-marker)
3960 vip-last-jump-ignore 0)
3961 (if com (vip-execute-com 'vip-goto-mark nil com)))
3962 ((and skip-white (= char ?'))
3963 (if com (vip-move-marker-locally 'vip-com-point (point)))
3964 (if (and (vip-same-line (point) vip-last-jump)
3965 (= (point) vip-last-jump-ignore))
3966 (goto-char vip-last-jump))
3967 (if (= (point) (mark t)) (pop-mark))
3968 (exchange-point-and-mark)
3969 (setq vip-last-jump (point))
3970 (back-to-indentation)
3971 (setq vip-last-jump-ignore (point))
3972 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
3973 (t (error vip-InvalidTextmarker char))))
3975 (defun vip-insert-tab ()
3976 (interactive)
3977 (insert-tab))
3979 (defun vip-exchange-point-and-mark ()
3980 (interactive)
3981 (exchange-point-and-mark)
3982 (back-to-indentation))
3984 ;; Input Mode Indentation
3986 ;; Returns t, if the string before point matches the regexp STR.
3987 (defsubst vip-looking-back (str)
3988 (and (save-excursion (re-search-backward str nil t))
3989 (= (point) (match-end 0))))
3992 (defun vip-forward-indent ()
3993 "Indent forward -- `C-t' in Vi."
3994 (interactive)
3995 (setq vip-cted t)
3996 (indent-to (+ (current-column) vip-shift-width)))
3998 (defun vip-backward-indent ()
3999 "Backtab, C-d in VI"
4000 (interactive)
4001 (if vip-cted
4002 (let ((p (point)) (c (current-column)) bol (indent t))
4003 (if (vip-looking-back "[0^]")
4004 (progn
4005 (if (eq ?^ (preceding-char))
4006 (setq vip-preserve-indent t))
4007 (delete-backward-char 1)
4008 (setq p (point))
4009 (setq indent nil)))
4010 (save-excursion
4011 (beginning-of-line)
4012 (setq bol (point)))
4013 (if (re-search-backward "[^ \t]" bol 1) (forward-char))
4014 (delete-region (point) p)
4015 (if indent
4016 (indent-to (- c vip-shift-width)))
4017 (if (or (bolp) (vip-looking-back "[^ \t]"))
4018 (setq vip-cted nil)))))
4020 (defun vip-autoindent ()
4021 "Auto Indentation, Vi-style."
4022 (interactive)
4023 (let ((col (current-indentation)))
4024 (if abbrev-mode (expand-abbrev))
4025 (if vip-preserve-indent
4026 (setq vip-preserve-indent nil)
4027 (setq vip-current-indent col))
4028 ;; don't leave whitespace lines around
4029 (if (memq last-command
4030 '(vip-autoindent
4031 vip-open-line vip-Open-line
4032 vip-replace-state-exit-cmd))
4033 (indent-to-left-margin))
4034 ;; use \n instead of newline, or else <Return> will move the insert point
4035 ;;(newline 1)
4036 (insert "\n")
4037 (if vip-auto-indent
4038 (progn
4039 (setq vip-cted t)
4040 (if (and vip-electric-mode
4041 (not (eq major-mode 'fundamental-mode)))
4042 (indent-according-to-mode)
4043 (indent-to vip-current-indent))
4048 ;; Viewing registers
4050 (defun vip-ket-function (arg)
4051 "Function called by \], the ket. View registers and call \]\]."
4052 (interactive "P")
4053 (let ((reg (read-char)))
4054 (cond ((vip-valid-register reg '(letter Letter))
4055 (view-register (downcase reg)))
4056 ((vip-valid-register reg '(digit))
4057 (let ((text (current-kill (- reg ?1) 'do-not-rotate)))
4058 (save-excursion
4059 (set-buffer (get-buffer-create "*Output*"))
4060 (delete-region (point-min) (point-max))
4061 (insert (format "Register %c contains the string:\n" reg))
4062 (insert text)
4063 (goto-char (point-min)))
4064 (display-buffer "*Output*")))
4065 ((= ?\] reg)
4066 (vip-next-heading arg))
4067 (t (error
4068 vip-InvalidRegister reg)))))
4070 (defun vip-brac-function (arg)
4071 "Function called by \[, the brac. View textmarkers and call \[\["
4072 (interactive "P")
4073 (let ((reg (read-char)))
4074 (cond ((= ?\[ reg)
4075 (vip-prev-heading arg))
4076 ((= ?\] reg)
4077 (vip-heading-end arg))
4078 ((vip-valid-register reg '(letter))
4079 (let* ((val (get-register (1+ (- reg ?a))))
4080 (buf (if (not val)
4081 (error vip-EmptyTextmarker reg)
4082 (marker-buffer val)))
4083 (pos (marker-position val))
4084 line-no text (s pos) (e pos))
4085 (save-excursion
4086 (set-buffer (get-buffer-create "*Output*"))
4087 (delete-region (point-min) (point-max))
4088 (if (and buf pos)
4089 (progn
4090 (save-excursion
4091 (set-buffer buf)
4092 (setq line-no (1+ (count-lines (point-min) val)))
4093 (goto-char pos)
4094 (beginning-of-line)
4095 (if (re-search-backward "[^ \t]" nil t)
4096 (progn
4097 (beginning-of-line)
4098 (setq s (point))))
4099 (goto-char pos)
4100 (forward-line 1)
4101 (if (re-search-forward "[^ \t]" nil t)
4102 (progn
4103 (end-of-line)
4104 (setq e (point))))
4105 (setq text (buffer-substring s e))
4106 (setq text (format "%s<%c>%s"
4107 (substring text 0 (- pos s))
4108 reg (substring text (- pos s)))))
4109 (insert
4110 (format
4111 "Textmarker `%c' is in buffer `%s' at line %d.\n"
4112 reg (buffer-name buf) line-no))
4113 (insert (format "Here is some text around %c:\n\n %s"
4114 reg text)))
4115 (insert (format vip-EmptyTextmarker reg)))
4116 (goto-char (point-min)))
4117 (display-buffer "*Output*")))
4118 (t (error vip-InvalidTextmarker reg)))))
4122 ;; commands in insertion mode
4124 (defun vip-delete-backward-word (arg)
4125 "Delete previous word."
4126 (interactive "p")
4127 (save-excursion
4128 (push-mark nil t)
4129 (backward-word arg)
4130 (delete-region (point) (mark t))
4131 (pop-mark)))
4134 (defun vip-set-expert-level (&optional dont-change-unless)
4135 "Sets the expert level for a Viper user.
4136 Can be called interactively to change (temporarily or permanently) the
4137 current expert level.
4139 The optional argument DONT-CHANGE-UNLESS, if not nil, says that
4140 the level should not be changed, unless its current value is
4141 meaningless (i.e., not one of 1,2,3,4,5).
4143 User level determines the setting of Viper variables that are most
4144 sensitive for VI-style look-and-feel."
4146 (interactive)
4148 (if (not (natnump vip-expert-level)) (setq vip-expert-level 0))
4150 (save-window-excursion
4151 (delete-other-windows)
4152 ;; if 0 < vip-expert-level < vip-max-expert-level
4153 ;; & dont-change-unless = t -- use it; else ask
4154 (vip-ask-level dont-change-unless))
4156 (setq viper-always t
4157 vip-ex-style-motion t
4158 vip-ex-style-editing-in-insert t
4159 vip-want-ctl-h-help nil)
4161 (cond ((eq vip-expert-level 1) ; novice or beginner
4162 (global-set-key ; in emacs-state
4163 vip-toggle-key
4164 (if (vip-window-display-p) 'vip-iconify 'suspend-emacs))
4165 (setq vip-no-multiple-ESC t
4166 vip-re-search t
4167 vip-vi-style-in-minibuffer t
4168 vip-search-wrap-around-t t
4169 vip-electric-mode nil
4170 vip-want-emacs-keys-in-vi nil
4171 vip-want-emacs-keys-in-insert nil))
4173 ((and (> vip-expert-level 1) (< vip-expert-level 5))
4174 ;; intermediate to guru
4175 (setq vip-no-multiple-ESC (if (vip-window-display-p)
4176 t 'twice)
4177 vip-electric-mode t
4178 vip-want-emacs-keys-in-vi t
4179 vip-want-emacs-keys-in-insert (> vip-expert-level 2))
4181 (if (eq vip-expert-level 4) ; respect user's ex-style motion
4182 ; and vip-no-multiple-ESC
4183 (progn
4184 (setq-default vip-ex-style-editing-in-insert
4185 (cdr (assoc 'vip-ex-style-editing-in-insert
4186 vip-saved-user-settings))
4187 vip-ex-style-motion
4188 (cdr (assoc 'vip-ex-style-motion
4189 vip-saved-user-settings)))
4190 (setq vip-ex-style-motion
4191 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4192 vip-ex-style-editing-in-insert
4193 (cdr (assoc 'vip-ex-style-editing-in-insert
4194 vip-saved-user-settings))
4195 vip-re-search
4196 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4197 vip-no-multiple-ESC
4198 (cdr (assoc 'vip-no-multiple-ESC
4199 vip-saved-user-settings))))))
4201 ;; A wizard!!
4202 ;; Ideally, if 5 is selected, a buffer should pop up to let the
4203 ;; user toggle the values of variables.
4204 (t (setq-default vip-ex-style-editing-in-insert
4205 (cdr (assoc 'vip-ex-style-editing-in-insert
4206 vip-saved-user-settings))
4207 vip-ex-style-motion
4208 (cdr (assoc 'vip-ex-style-motion
4209 vip-saved-user-settings)))
4210 (setq vip-want-ctl-h-help
4211 (cdr (assoc 'vip-want-ctl-h-help vip-saved-user-settings))
4212 viper-always
4213 (cdr (assoc 'viper-always vip-saved-user-settings))
4214 vip-no-multiple-ESC
4215 (cdr (assoc 'vip-no-multiple-ESC vip-saved-user-settings))
4216 vip-ex-style-motion
4217 (cdr (assoc 'vip-ex-style-motion vip-saved-user-settings))
4218 vip-ex-style-editing-in-insert
4219 (cdr (assoc 'vip-ex-style-editing-in-insert
4220 vip-saved-user-settings))
4221 vip-re-search
4222 (cdr (assoc 'vip-re-search vip-saved-user-settings))
4223 vip-electric-mode
4224 (cdr (assoc 'vip-electric-mode
4225 vip-saved-user-settings))
4226 vip-want-emacs-keys-in-vi
4227 (cdr (assoc 'vip-want-emacs-keys-in-vi
4228 vip-saved-user-settings))
4229 vip-want-emacs-keys-in-insert
4230 (cdr (assoc 'vip-want-emacs-keys-in-insert
4231 vip-saved-user-settings)))))
4232 (vip-set-mode-vars-for vip-current-state)
4233 (if (or viper-always
4234 (and (> vip-expert-level 0) (> 5 vip-expert-level)))
4235 (vip-set-hooks)))
4237 ;; Ask user expert level.
4238 (defun vip-ask-level (dont-change-unless)
4239 (let ((ask-buffer " *vip-ask-level*")
4240 level-changed repeated)
4241 (save-window-excursion
4242 (switch-to-buffer ask-buffer)
4244 (or (eq this-command 'vip-set-expert-level)
4245 (and
4246 (<= vip-expert-level vip-max-expert-level)
4247 (>= vip-expert-level 1))
4248 (progn
4249 (insert "
4251 *** Important Notice for VIP users***
4253 This is VIPER
4255 @joke
4256 Viper Is a Package for Emacs Rebels,
4257 a VI Plan for Emacs Rescue,
4258 and a venomous VI PERil.
4259 @end joke
4261 Technically speaking, Viper is a new Vi emulator that replaces
4262 the old VIP package.
4264 Viper emulates Vi much better than VIP. It also significantly
4265 extends and improves upon Vi in many useful ways.
4267 Although many VIP settings in your ~/.vip are compatible with Viper,
4268 you may have to change some of them. Please refer to the documentation,
4269 which can be obtained by executing
4271 :help
4273 when Viper is in Vi state.
4275 If you will be so lucky as to find a bug, report it via the command
4277 :submitReport
4279 Type any key to continue... ")
4281 (read-char)
4282 (erase-buffer)))
4284 (while (or (> vip-expert-level vip-max-expert-level)
4285 (< vip-expert-level 1)
4286 (null dont-change-unless))
4287 (erase-buffer)
4288 (if repeated
4289 (progn
4290 (message "Invalid user level")
4291 (beep 1))
4292 (setq repeated t))
4293 (setq dont-change-unless t
4294 level-changed t)
4295 (insert "
4296 Please specify your level of familiarity with the venomous VI PERil
4297 (and the VI Plan for Emacs Rescue).
4298 You can change it at any time by typing `M-x vip-set-expert-level RET'
4300 1 -- BEGINNER: Almost all Emacs features are suppressed.
4301 Feels almost like straight Vi. File name completion and
4302 command history in the minibuffer are thrown in as a bonus.
4303 To use Emacs productively, you must reach level 3 or higher.
4304 2 -- MASTER: C-c now has its standard Emacs meaning in Vi command state,
4305 so most Emacs commands can be used when Viper is in Vi state.
4306 Good progress---you are well on the way to level 3!
4307 3 -- GRAND MASTER: Like 3, but most Emacs commands are available also
4308 in Viper's insert state.
4309 4 -- GURU: Like 3, but user settings are respected for vip-no-multiple-ESC,
4310 vip-re-search, vip-ex-style-motion, & vip-ex-style-editing-in-insert
4311 variables. Adjust these settings to your taste.
4312 5 -- WIZARD: Like 4, but user settings are also respected for viper-always,
4313 vip-electric-mode, vip-want-ctl-h-help, vip-want-emacs-keys-in-vi,
4314 and vip-want-emacs-keys-in-insert. Adjust these to your taste.
4316 Please, specify your level now: ")
4318 (setq vip-expert-level (- (vip-read-char-exclusive) ?0))
4319 ) ; end while
4321 ;; tell the user if level was changed
4322 (and level-changed
4323 (progn
4324 (insert
4325 (format "\n\n\n\n\n\t\tYou have selected user level %d"
4326 vip-expert-level))
4327 (if (y-or-n-p "Do you wish to make this change permanent? ")
4328 ;; save the setting for vip-expert-level
4329 (vip-save-setting
4330 'vip-expert-level
4331 (format "Saving user level %d ..." vip-expert-level)
4332 vip-custom-file-name))
4334 (bury-buffer) ; remove ask-buffer from screen
4335 (message "")
4339 (defun vip-nil ()
4340 (interactive)
4341 (beep 1))
4344 ;; if ENFORCE-BUFFER is not nil, error if CHAR is a marker in another buffer
4345 (defun vip-register-to-point (char &optional enforce-buffer)
4346 "Like jump-to-register, but switches to another buffer in another window."
4347 (interactive "cViper register to point: ")
4348 (let ((val (get-register char)))
4349 (cond
4350 ((and (fboundp 'frame-configuration-p)
4351 (frame-configuration-p val))
4352 (set-frame-configuration val))
4353 ((window-configuration-p val)
4354 (set-window-configuration val))
4355 ((vip-valid-marker val)
4356 (if (and enforce-buffer
4357 (not (equal (current-buffer) (marker-buffer val))))
4358 (error (concat vip-EmptyTextmarker " in this buffer")
4359 (1- (+ char ?a))))
4360 (pop-to-buffer (marker-buffer val))
4361 (goto-char val))
4362 ((and (consp val) (eq (car val) 'file))
4363 (find-file (cdr val)))
4365 (error vip-EmptyTextmarker (1- (+ char ?a)))))))
4368 (defun vip-save-kill-buffer ()
4369 "Save then kill current buffer. "
4370 (interactive)
4371 (if (< vip-expert-level 2)
4372 (save-buffers-kill-emacs)
4373 (save-buffer)
4374 (kill-buffer (current-buffer))))
4378 ;;; Bug Report
4380 (defun vip-submit-report ()
4381 "Submit bug report on Viper."
4382 (interactive)
4383 (let ((reporter-prompt-for-summary-p t)
4384 (vip-device-type (vip-device-type))
4385 color-display-p frame-parameters
4386 minibuffer-emacs-face minibuffer-vi-face minibuffer-insert-face
4387 varlist salutation window-config)
4389 ;; If mode info is needed, add variable to `let' and then set it below,
4390 ;; like we did with color-display-p.
4391 (setq color-display-p (if (vip-window-display-p)
4392 (vip-color-display-p)
4393 'non-x)
4394 minibuffer-vi-face (if (vip-has-face-support-p)
4395 (vip-get-face vip-minibuffer-vi-face)
4396 'non-x)
4397 minibuffer-insert-face (if (vip-has-face-support-p)
4398 (vip-get-face vip-minibuffer-insert-face)
4399 'non-x)
4400 minibuffer-emacs-face (if (vip-has-face-support-p)
4401 (vip-get-face vip-minibuffer-emacs-face)
4402 'non-x)
4403 frame-parameters (if (fboundp 'frame-parameters)
4404 (frame-parameters (selected-frame))))
4406 (setq varlist (list 'vip-vi-minibuffer-minor-mode
4407 'vip-insert-minibuffer-minor-mode
4408 'vip-vi-intercept-minor-mode
4409 'vip-vi-local-user-minor-mode
4410 'vip-vi-kbd-minor-mode
4411 'vip-vi-global-user-minor-mode
4412 'vip-vi-state-modifier-minor-mode
4413 'vip-vi-diehard-minor-mode
4414 'vip-vi-basic-minor-mode
4415 'vip-replace-minor-mode
4416 'vip-insert-intercept-minor-mode
4417 'vip-insert-local-user-minor-mode
4418 'vip-insert-kbd-minor-mode
4419 'vip-insert-global-user-minor-mode
4420 'vip-insert-state-modifier-minor-mode
4421 'vip-insert-diehard-minor-mode
4422 'vip-insert-basic-minor-mode
4423 'vip-emacs-intercept-minor-mode
4424 'vip-emacs-local-user-minor-mode
4425 'vip-emacs-kbd-minor-mode
4426 'vip-emacs-global-user-minor-mode
4427 'vip-emacs-state-modifier-minor-mode
4428 'vip-automatic-iso-accents
4429 'vip-want-emacs-keys-in-insert
4430 'vip-want-emacs-keys-in-vi
4431 'vip-keep-point-on-undo
4432 'vip-no-multiple-ESC
4433 'vip-electric-mode
4434 'vip-ESC-key
4435 'vip-want-ctl-h-help
4436 'vip-ex-style-editing-in-insert
4437 'vip-delete-backwards-in-replace
4438 'vip-vi-style-in-minibuffer
4439 'vip-vi-state-hook
4440 'vip-insert-state-hook
4441 'vip-replace-state-hook
4442 'vip-emacs-state-hook
4443 'ex-cycle-other-window
4444 'ex-cycle-through-non-files
4445 'vip-expert-level
4446 'major-mode
4447 'vip-device-type
4448 'color-display-p
4449 'frame-parameters
4450 'minibuffer-vi-face
4451 'minibuffer-insert-face
4452 'minibuffer-emacs-face
4454 (setq salutation "
4455 Congratulations! You may have unearthed a bug in Viper!
4456 Please mail a concise, accurate summary of the problem to the address above.
4458 -------------------------------------------------------------------")
4459 (setq window-config (current-window-configuration))
4460 (with-output-to-temp-buffer " *vip-info*"
4461 (switch-to-buffer " *vip-info*")
4462 (delete-other-windows)
4463 (princ "
4464 PLEASE FOLLOW THESE PROCEDURES
4465 ------------------------------
4467 Before reporting a bug, please verify that it is related to Viper, and is
4468 not cause by other packages you are using.
4470 Don't report compilation warnings, unless you are certain that there is a
4471 problem. These warnings are normal and unavoidable.
4473 Please note that users should not modify variables and keymaps other than
4474 those advertised in the manual. Such `customization' is likely to crash
4475 Viper, as it would any other improperly customized Emacs package.
4477 If you are reporting an error message received while executing one of the
4478 Viper commands, type:
4480 M-x set-variable <Return> debug-on-error <Return> t <Return>
4482 Then reproduce the error. The above command will cause Emacs to produce a
4483 back trace of the execution that leads to the error. Please include this
4484 trace in your bug report.
4486 If you believe that one of Viper's commands goes into an infinite loop
4487 \(e.g., Emacs freezes\), type:
4489 M-x set-variable <Return> debug-on-quit <Return> t <Return>
4491 Then reproduce the problem. Wait for a few seconds, then type C-g to abort
4492 the current command. Include the resulting back trace in the bug report.
4494 Mail anyway (y or n)? ")
4495 (if (y-or-n-p "Mail anyway? ")
4497 (set-window-configuration window-config)
4498 (error "Bug report aborted")))
4500 (require 'reporter)
4501 (set-window-configuration window-config)
4503 (reporter-submit-bug-report "kifer@cs.sunysb.edu"
4504 (vip-version)
4505 varlist
4506 nil 'delete-other-windows
4507 salutation)
4513 ;; Smoothes out the difference between Emacs' unread-command-events
4514 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
4515 ;; events or a sequence of keys.
4517 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
4518 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
4519 ;; into an event. Below, we delete nil from event lists, since nil is the most
4520 ;; common symbol that might appear in this wrong context.
4521 (defun vip-set-unread-command-events (arg)
4522 (if vip-emacs-p
4523 (setq
4524 unread-command-events
4525 (let ((new-events
4526 (cond ((eventp arg) (list arg))
4527 ((listp arg) arg)
4528 ((sequencep arg)
4529 (listify-key-sequence arg))
4530 (t (error
4531 "vip-set-unread-command-events: Invalid argument, %S"
4532 arg)))))
4533 (if (not (eventp nil))
4534 (setq new-events (delq nil new-events)))
4535 (append new-events unread-command-events)))
4536 ;; XEmacs
4537 (setq
4538 unread-command-events
4539 (append
4540 (cond ((vip-characterp arg) (list (character-to-event arg)))
4541 ((eventp arg) (list arg))
4542 ((stringp arg) (mapcar 'character-to-event arg))
4543 ((vectorp arg) (append arg nil)) ; turn into list
4544 ((listp arg) (vip-eventify-list-xemacs arg))
4545 (t (error
4546 "vip-set-unread-command-events: Invalid argument, %S" arg)))
4547 unread-command-events))))
4549 ;; list is assumed to be a list of events of characters
4550 (defun vip-eventify-list-xemacs (lis)
4551 (mapcar
4552 (function (lambda (elt)
4553 (cond ((vip-characterp elt) (character-to-event elt))
4554 ((eventp elt) elt)
4555 (t (error
4556 "vip-eventify-list-xemacs: can't convert to event, %S"
4557 elt)))))
4558 lis))
4562 ;;; viper-cmd.el ends here