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