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