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