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