(save-match-data): Use save-match-data-internal
[emacs.git] / lisp / emulation / vi.el
blob265ab94f43fe3d70611c8c044af2041263a4dab9
1 ;;; vi.el --- major mode for emulating "vi" editor under GNU Emacs.
3 ; This file is in the public domain because the authors distributed it
4 ; without a copyright notice before the US signed the Bern Convention.
6 ;; Author: Neal Ziring <nz@rsch.wisc.edu>
7 ;; Felix S. T. Wu <wu@crys.wisc.edu>
8 ;; Keywords: emulations
10 ;;; Commentary:
12 ; Originally written by : seismo!wucs!nz@rsch.wisc.edu (Neal Ziring)
13 ; Extensively redesigned and rewritten by wu@crys.wisc.edu (Felix S.T. Wu)
14 ; Last revision: 01/07/87 Wed (for GNU Emacs 18.33)
16 ; INSTALLATION PROCEDURE:
17 ; 1) Add a global key binding for command "vi-mode" (I use ESC ESC instead of
18 ; the single ESC used in real "vi", so I can access other ESC prefixed emacs
19 ; commands while I'm in "vi"), say, by putting the following line in your
20 ; ".emacs" file:
21 ; (define-key global-map "\e\e" 'vi-mode) ;quick switch into vi-mode
22 ; 2) If you wish you can define "find-file-hooks" to enter "vi" automatically
23 ; after a file is loaded into the buffer. For example, I defined it as:
24 ; (setq find-file-hooks (list
25 ; (function (lambda ()
26 ; (if (not (or (eq major-mode 'Info-mode)
27 ; (eq major-mode 'vi-mode)))
28 ; (vi-mode))))))
29 ; 3) In your .emacs file you can define the command "vi-mode" to be "autoload"
30 ; or you can execute the "load" command to load "vi" directly.
31 ; 4) Read the comments for command "vi-mode" before you start using it.
33 ; COULD DO
34 ; 1). A general 'define-operator' function to replace current hack
35 ; 2). In operator handling, should allow other point moving Emacs commands
36 ; (such as ESC <, ESC >) to be used as arguments.
38 ;;; Code:
40 (defun vi-switch-mode (arg mode-char)
41 "Switch the major mode of current buffer as specified by the following char \\{vi-tilde-map}"
42 (interactive "P\nc")
43 (let ((mode-cmd (lookup-key vi-tilde-map (char-to-string mode-char))))
44 (if (null mode-cmd)
45 (with-output-to-temp-buffer "*Help*"
46 (princ (substitute-command-keys "Possible major modes to switch to: \\{vi-tilde-map}"))
47 (save-excursion
48 (set-buffer standard-output)
49 (help-mode)))
50 (setq prefix-arg arg) ; prefix arg will be passed down
51 (command-execute mode-cmd nil) ; may need to save mode-line-format etc
52 (force-mode-line-update)))) ; just in case
55 (if (null (where-is-internal 'vi-switch-mode (current-local-map)))
56 (define-key ctl-x-map "~" 'vi-switch-mode))
58 (defvar vi-tilde-map nil
59 "Keymap used for \\[vi-switch-mode] prefix key. Link to various major modes.")
61 (if vi-tilde-map
62 nil
63 (setq vi-tilde-map (make-keymap))
64 (define-key vi-tilde-map "a" 'abbrev-mode)
65 (define-key vi-tilde-map "c" 'c-mode)
66 (define-key vi-tilde-map "d" 'vi-debugging)
67 (define-key vi-tilde-map "e" 'emacs-lisp-mode)
68 (define-key vi-tilde-map "f" 'auto-fill-mode)
69 (define-key vi-tilde-map "g" 'prolog-mode)
70 (define-key vi-tilde-map "h" 'hanoi)
71 (define-key vi-tilde-map "i" 'info-mode)
72 (define-key vi-tilde-map "l" 'lisp-mode)
73 (define-key vi-tilde-map "n" 'nroff-mode)
74 (define-key vi-tilde-map "o" 'overwrite-mode)
75 (define-key vi-tilde-map "O" 'outline-mode)
76 (define-key vi-tilde-map "P" 'picture-mode)
77 (define-key vi-tilde-map "r" 'vi-readonly-mode)
78 (define-key vi-tilde-map "t" 'text-mode)
79 (define-key vi-tilde-map "v" 'vi-mode)
80 (define-key vi-tilde-map "x" 'tex-mode)
81 (define-key vi-tilde-map "~" 'vi-back-to-old-mode))
83 (defun vi-debugging (arg)
84 "Toggle debug-on-error flag. If prefix arg is given, set t."
85 (interactive "P")
86 (if arg
87 (setq debug-on-error t)
88 (setq debug-on-error (not debug-on-error)))
89 (if debug-on-error
90 (message "Debug-on-error ...")
91 (message "NO more debug-on-error")))
93 (defun vi-back-to-old-mode ()
94 "Go back to the previous mode without setting up for insertion."
95 (interactive)
96 (if vi-mode-old-major-mode
97 (progn
98 (setq mode-name vi-mode-old-mode-name)
99 (use-local-map vi-mode-old-local-map)
100 (setq major-mode vi-mode-old-major-mode)
101 (setq case-fold-search vi-mode-old-case-fold)
102 (force-mode-line-update))))
104 (defun vi-readonly-mode ()
105 "Toggle current buffer's readonly flag."
106 (interactive)
107 (setq buffer-read-only (not buffer-read-only)))
109 (defvar vi-com-map nil
110 "Keymap used in Evi's command state
111 Command state includes most of the vi editing commands, with some Emacs
112 command extensions.")
114 (put 'vi-undefined 'suppress-keymap t)
115 (if vi-com-map nil
116 (setq vi-com-map (make-keymap))
117 ;;(fillarray vi-com-map 'vi-undefined)
118 (define-key vi-com-map "\C-@" 'vi-mark-region) ; extension
119 (define-key vi-com-map "\C-a" 'vi-ask-for-info) ; extension
120 (define-key vi-com-map "\C-b" 'vi-backward-windowful)
121 (define-key vi-com-map "\C-c" 'vi-do-old-mode-C-c-command) ; extension
122 (define-key vi-com-map "\C-d" 'vi-scroll-down-window)
123 (define-key vi-com-map "\C-e" 'vi-expose-line-below)
124 (define-key vi-com-map "\C-f" 'vi-forward-windowful)
125 (define-key vi-com-map "\C-g" 'keyboard-quit)
126 (define-key vi-com-map "\C-i" 'indent-relative-maybe) ; TAB
127 (define-key vi-com-map "\C-j" 'vi-next-line) ; LFD
128 (define-key vi-com-map "\C-k" 'vi-kill-line) ; extension
129 (define-key vi-com-map "\C-l" 'recenter)
130 (define-key vi-com-map "\C-m" 'vi-next-line-first-nonwhite) ; RET
131 (define-key vi-com-map "\C-n" 'vi-next-line)
132 (define-key vi-com-map "\C-o" 'vi-split-open-line)
133 (define-key vi-com-map "\C-p" 'previous-line)
134 (define-key vi-com-map "\C-q" 'vi-query-replace) ; extension
135 (define-key vi-com-map "\C-r" 'vi-isearch-backward) ; modification
136 (define-key vi-com-map "\C-s" 'vi-isearch-forward) ; extension
137 (define-key vi-com-map "\C-t" 'vi-transpose-objects) ; extension
138 (define-key vi-com-map "\C-u" 'vi-scroll-up-window)
139 (define-key vi-com-map "\C-v" 'scroll-up) ; extension
140 (define-key vi-com-map "\C-w" 'vi-kill-region) ; extension
141 (define-key vi-com-map "\C-x" 'Control-X-prefix) ; extension
142 (define-key vi-com-map "\C-y" 'vi-expose-line-above)
143 (define-key vi-com-map "\C-z" 'suspend-emacs)
145 (define-key vi-com-map "\e" 'ESC-prefix); C-[ (ESC)
146 (define-key vi-com-map "\C-\\" 'vi-unimplemented)
147 (define-key vi-com-map "\C-]" 'find-tag)
148 (define-key vi-com-map "\C-^" 'vi-locate-def) ; extension
149 (define-key vi-com-map "\C-_" 'vi-undefined)
151 (define-key vi-com-map " " 'forward-char)
152 (define-key vi-com-map "!" 'vi-operator)
153 (define-key vi-com-map "\"" 'vi-char-argument)
154 (define-key vi-com-map "#" 'universal-argument) ; extension
155 (define-key vi-com-map "$" 'end-of-line)
156 (define-key vi-com-map "%" 'vi-find-matching-paren)
157 (define-key vi-com-map "&" 'vi-unimplemented)
158 (define-key vi-com-map "'" 'vi-goto-line-mark)
159 (define-key vi-com-map "(" 'backward-sexp)
160 (define-key vi-com-map ")" 'forward-sexp)
161 (define-key vi-com-map "*" 'vi-name-last-change-or-macro) ; extension
162 (define-key vi-com-map "+" 'vi-next-line-first-nonwhite)
163 (define-key vi-com-map "," 'vi-reverse-last-find-char)
164 (define-key vi-com-map "-" 'vi-previous-line-first-nonwhite)
165 (define-key vi-com-map "." 'vi-redo-last-change-command)
166 (define-key vi-com-map "/" 'vi-search-forward)
167 (define-key vi-com-map "0" 'beginning-of-line)
169 (define-key vi-com-map "1" 'vi-digit-argument)
170 (define-key vi-com-map "2" 'vi-digit-argument)
171 (define-key vi-com-map "3" 'vi-digit-argument)
172 (define-key vi-com-map "4" 'vi-digit-argument)
173 (define-key vi-com-map "5" 'vi-digit-argument)
174 (define-key vi-com-map "6" 'vi-digit-argument)
175 (define-key vi-com-map "7" 'vi-digit-argument)
176 (define-key vi-com-map "8" 'vi-digit-argument)
177 (define-key vi-com-map "9" 'vi-digit-argument)
179 (define-key vi-com-map ":" 'vi-ex-cmd)
180 (define-key vi-com-map ";" 'vi-repeat-last-find-char)
181 (define-key vi-com-map "<" 'vi-operator)
182 (define-key vi-com-map "=" 'vi-operator)
183 (define-key vi-com-map ">" 'vi-operator)
184 (define-key vi-com-map "?" 'vi-search-backward)
185 (define-key vi-com-map "@" 'vi-call-named-change-or-macro) ; extension
187 (define-key vi-com-map "A" 'vi-append-at-end-of-line)
188 (define-key vi-com-map "B" 'vi-backward-blank-delimited-word)
189 (define-key vi-com-map "C" 'vi-change-rest-of-line)
190 (define-key vi-com-map "D" 'vi-kill-line)
191 (define-key vi-com-map "E" 'vi-end-of-blank-delimited-word)
192 (define-key vi-com-map "F" 'vi-backward-find-char)
193 (define-key vi-com-map "G" 'vi-goto-line)
194 (define-key vi-com-map "H" 'vi-home-window-line)
195 (define-key vi-com-map "I" 'vi-insert-before-first-nonwhite)
196 (define-key vi-com-map "J" 'vi-join-lines)
197 (define-key vi-com-map "K" 'vi-undefined)
198 (define-key vi-com-map "L" 'vi-last-window-line)
199 (define-key vi-com-map "M" 'vi-middle-window-line)
200 (define-key vi-com-map "N" 'vi-reverse-last-search)
201 (define-key vi-com-map "O" 'vi-open-above)
202 (define-key vi-com-map "P" 'vi-put-before)
203 (define-key vi-com-map "Q" 'vi-quote-words) ; extension
204 (define-key vi-com-map "R" 'vi-replace-chars)
205 (define-key vi-com-map "S" 'vi-substitute-lines)
206 (define-key vi-com-map "T" 'vi-backward-upto-char)
207 (define-key vi-com-map "U" 'vi-unimplemented)
208 (define-key vi-com-map "V" 'vi-undefined)
209 (define-key vi-com-map "W" 'vi-forward-blank-delimited-word)
210 (define-key vi-com-map "X" 'call-last-kbd-macro) ; modification/extension
211 (define-key vi-com-map "Y" 'vi-yank-line)
212 (define-key vi-com-map "Z" (make-sparse-keymap)) ;allow below prefix command
213 (define-key vi-com-map "ZZ" 'vi-save-all-and-exit)
215 (define-key vi-com-map "[" 'vi-unimplemented)
216 (define-key vi-com-map "\\" 'vi-operator) ; extension for vi-narrow-op
217 (define-key vi-com-map "]" 'vi-unimplemented)
218 (define-key vi-com-map "^" 'back-to-indentation)
219 (define-key vi-com-map "_" 'vi-undefined)
220 (define-key vi-com-map "`" 'vi-goto-char-mark)
222 (define-key vi-com-map "a" 'vi-insert-after)
223 (define-key vi-com-map "b" 'backward-word)
224 (define-key vi-com-map "c" 'vi-operator)
225 (define-key vi-com-map "d" 'vi-operator)
226 (define-key vi-com-map "e" 'vi-end-of-word)
227 (define-key vi-com-map "f" 'vi-forward-find-char)
228 (define-key vi-com-map "g" 'vi-beginning-of-buffer) ; extension
229 (define-key vi-com-map "h" 'backward-char)
230 (define-key vi-com-map "i" 'vi-insert-before)
231 (define-key vi-com-map "j" 'vi-next-line)
232 (define-key vi-com-map "k" 'previous-line)
233 (define-key vi-com-map "l" 'forward-char)
234 (define-key vi-com-map "m" 'vi-set-mark)
235 (define-key vi-com-map "n" 'vi-repeat-last-search)
236 (define-key vi-com-map "o" 'vi-open-below)
237 (define-key vi-com-map "p" 'vi-put-after)
238 (define-key vi-com-map "q" 'vi-replace)
239 (define-key vi-com-map "r" 'vi-replace-1-char)
240 (define-key vi-com-map "s" 'vi-substitute-chars)
241 (define-key vi-com-map "t" 'vi-forward-upto-char)
242 (define-key vi-com-map "u" 'undo)
243 (define-key vi-com-map "v" 'vi-verify-spelling)
244 (define-key vi-com-map "w" 'vi-forward-word)
245 (define-key vi-com-map "x" 'vi-kill-char)
246 (define-key vi-com-map "y" 'vi-operator)
247 (define-key vi-com-map "z" 'vi-adjust-window)
249 (define-key vi-com-map "{" 'backward-paragraph)
250 (define-key vi-com-map "|" 'vi-goto-column)
251 (define-key vi-com-map "}" 'forward-paragraph)
252 (define-key vi-com-map "~" 'vi-change-case)
253 (define-key vi-com-map "\177" 'delete-backward-char))
255 (put 'backward-char 'point-moving-unit 'char)
256 (put 'vi-next-line 'point-moving-unit 'line)
257 (put 'next-line 'point-moving-unit 'line)
258 (put 'forward-line 'point-moving-unit 'line)
259 (put 'previous-line 'point-moving-unit 'line)
260 (put 'vi-isearch-backward 'point-moving-unit 'search)
261 (put 'vi-search-backward 'point-moving-unit 'search)
262 (put 'vi-isearch-forward 'point-moving-unit 'search)
263 (put 'vi-search-forward 'point-moving-unit 'search)
264 (put 'forward-char 'point-moving-unit 'char)
265 (put 'end-of-line 'point-moving-unit 'char)
266 (put 'vi-find-matching-paren 'point-moving-unit 'match)
267 (put 'vi-goto-line-mark 'point-moving-unit 'line)
268 (put 'backward-sexp 'point-moving-unit 'sexp)
269 (put 'forward-sexp 'point-moving-unit 'sexp)
270 (put 'vi-next-line-first-nonwhite 'point-moving-unit 'line)
271 (put 'vi-previous-line-first-nonwhite 'point-moving-unit 'line)
272 (put 'vi-reverse-last-find-char 'point-moving-unit 'rev-find)
273 (put 'vi-re-search-forward 'point-moving-unit 'search)
274 (put 'beginning-of-line 'point-moving-unit 'char)
275 (put 'vi-beginning-of-buffer 'point-moving-unit 'char)
276 (put 'vi-repeat-last-find-char 'point-moving-unit 'find)
277 (put 'vi-re-search-backward 'point-moving-unit 'search)
278 (put 'vi-backward-blank-delimited-word 'point-moving-unit 'WORD)
279 (put 'vi-end-of-blank-delimited-word 'point-moving-unit 'match)
280 (put 'vi-backward-find-char 'point-moving-unit 'find)
281 (put 'vi-goto-line 'point-moving-unit 'line)
282 (put 'vi-home-window-line 'point-moving-unit 'line)
283 (put 'vi-last-window-line 'point-moving-unit 'line)
284 (put 'vi-middle-window-line 'point-moving-unit 'line)
285 (put 'vi-reverse-last-search 'point-moving-unit 'rev-search)
286 (put 'vi-backward-upto-char 'point-moving-unit 'find)
287 (put 'vi-forward-blank-delimited-word 'point-moving-unit 'WORD)
288 (put 'back-to-indentation 'point-moving-unit 'char)
289 (put 'vi-goto-char-mark 'point-moving-unit 'char)
290 (put 'backward-word 'point-moving-unit 'word)
291 (put 'vi-end-of-word 'point-moving-unit 'match)
292 (put 'vi-forward-find-char 'point-moving-unit 'find)
293 (put 'backward-char 'point-moving-unit 'char)
294 (put 'vi-forward-char 'point-moving-unit 'char)
295 (put 'vi-repeat-last-search 'point-moving-unit 'search)
296 (put 'vi-forward-upto-char 'point-moving-unit 'find)
297 (put 'vi-forward-word 'point-moving-unit 'word)
298 (put 'vi-goto-column 'point-moving-unit 'match)
299 (put 'forward-paragraph 'point-moving-unit 'paragraph)
300 (put 'backward-paragraph 'point-moving-unit 'paragraph)
302 ;;; region mark commands
303 (put 'mark-page 'point-moving-unit 'region)
304 (put 'mark-paragraph 'point-moving-unit 'region)
305 (put 'mark-word 'point-moving-unit 'region)
306 (put 'mark-sexp 'point-moving-unit 'region)
307 (put 'mark-defun 'point-moving-unit 'region)
308 (put 'mark-whole-buffer 'point-moving-unit 'region)
309 (put 'mark-end-of-sentence 'point-moving-unit 'region)
310 (put 'mark-c-function 'point-moving-unit 'region)
313 (defvar vi-mark-alist nil
314 "Alist of (NAME . MARK), marks are local to each buffer.")
316 (defvar vi-scroll-amount (/ (window-height) 2)
317 "Default amount of lines for scrolling (used by \"^D\"/\"^U\").")
319 (defvar vi-shift-width 4
320 "Shift amount for \"<\"/\">\" operators.")
322 (defvar vi-ins-point nil ; integer
323 "Last insertion point. Should use `mark' instead.")
325 (defvar vi-ins-length nil ; integer
326 "Length of last insertion.")
328 (defvar vi-ins-repetition nil ; integer
329 "The repetition required for last insertion.")
331 (defvar vi-ins-overwrt-p nil ; boolean
332 "T if last insertion was a replace actually.")
334 (defvar vi-ins-prefix-code nil ; ready-to-eval sexp
335 "Code to be eval'ed before (redo-)insertion begins.")
337 (defvar vi-last-find-char nil ; cons cell
338 "Save last direction, char and upto-flag used for char finding.")
340 (defvar vi-last-change-command nil ; cons cell
341 "Save commands for redoing last changes. Each command is in (FUNC . ARGS)
342 form that is ready to be `apply'ed.")
344 (defvar vi-last-shell-command nil ; last shell op command line
345 "Save last shell command given for \"!\" operator.")
347 (defvar vi-insert-state nil ; boolean
348 "Non-nil if it is in insert state.")
350 ; in "loaddefs.el"
351 ;(defvar search-last-string ""
352 ; "Last string search for by a search command.")
354 (defvar vi-search-last-command nil ; (re-)search-forward(backward)
355 "Save last search command for possible redo.")
357 (defvar vi-mode-old-local-map nil
358 "Save the local-map used before entering vi-mode.")
360 (defvar vi-mode-old-mode-name nil
361 "Save the mode-name before entering vi-mode.")
363 (defvar vi-mode-old-major-mode nil
364 "Save the major-mode before entering vi-mode.")
366 (defvar vi-mode-old-case-fold nil)
368 ;(defconst vi-add-to-mode-line-1
369 ; '(overwrite-mode nil " Insert"))
371 ;; Value is same as vi-add-to-mode-line-1 when in vi mode,
372 ;; but nil in other buffers.
373 ;(defvar vi-add-to-mode-line nil)
375 (defun vi-mode-setup ()
376 "Setup a buffer for vi-mode by creating necessary buffer-local variables."
377 ; (make-local-variable 'vi-add-to-mode-line)
378 ; (setq vi-add-to-mode-line vi-add-to-mode-line-1)
379 ; (or (memq vi-add-to-mode-line minor-mode-alist)
380 ; (setq minor-mode-alist (cons vi-add-to-mode-line minor-mode-alist)))
381 (make-local-variable 'vi-scroll-amount)
382 (setq vi-scroll-amount (/ (window-height) 2))
383 (make-local-variable 'vi-shift-width)
384 (setq vi-shift-width 4)
385 (make-local-variable 'vi-ins-point)
386 (make-local-variable 'vi-ins-length)
387 (make-local-variable 'vi-ins-repetition)
388 (make-local-variable 'vi-ins-overwrt-p)
389 (make-local-variable 'vi-ins-prefix-code)
390 (make-local-variable 'vi-last-change-command)
391 (make-local-variable 'vi-last-shell-command)
392 (make-local-variable 'vi-last-find-char)
393 (make-local-variable 'vi-mark-alist)
394 (make-local-variable 'vi-insert-state)
395 (make-local-variable 'vi-mode-old-local-map)
396 (make-local-variable 'vi-mode-old-mode-name)
397 (make-local-variable 'vi-mode-old-major-mode)
398 (make-local-variable 'vi-mode-old-case-fold)
399 (run-hooks 'vi-mode-hook))
401 ;;;###autoload
402 (defun vi-mode ()
403 "Major mode that acts like the `vi' editor.
404 The purpose of this mode is to provide you the combined power of vi (namely,
405 the \"cross product\" effect of commands and repeat last changes) and Emacs.
407 This command redefines nearly all keys to look like vi commands.
408 It records the previous major mode, and any vi command for input
409 \(`i', `a', `s', etc.) switches back to that mode.
410 Thus, ordinary Emacs (in whatever major mode you had been using)
411 is \"input\" mode as far as vi is concerned.
413 To get back into vi from \"input\" mode, you must issue this command again.
414 Therefore, it is recommended that you assign it to a key.
416 Major differences between this mode and real vi :
418 * Limitations and unsupported features
419 - Search patterns with line offset (e.g. /pat/+3 or /pat/z.) are
420 not supported.
421 - Ex commands are not implemented; try ':' to get some hints.
422 - No line undo (i.e. the 'U' command), but multi-undo is a standard feature.
424 * Modifications
425 - The stopping positions for some point motion commands (word boundary,
426 pattern search) are slightly different from standard 'vi'.
427 Also, no automatic wrap around at end of buffer for pattern searching.
428 - Since changes are done in two steps (deletion then insertion), you need
429 to undo twice to completely undo a change command. But this is not needed
430 for undoing a repeated change command.
431 - No need to set/unset 'magic', to search for a string with regular expr
432 in it just put a prefix arg for the search commands. Replace cmds too.
433 - ^R is bound to incremental backward search, so use ^L to redraw screen.
435 * Extensions
436 - Some standard (or modified) Emacs commands were integrated, such as
437 incremental search, query replace, transpose objects, and keyboard macros.
438 - In command state, ^X links to the 'ctl-x-map', and ESC can be linked to
439 esc-map or set undefined. These can give you the full power of Emacs.
440 - See vi-com-map for those keys that are extensions to standard vi, e.g.
441 `vi-name-last-change-or-macro', `vi-verify-spelling', `vi-locate-def',
442 `vi-mark-region', and 'vi-quote-words'. Some of them are quite handy.
443 - Use \\[vi-switch-mode] to switch among different modes quickly.
445 Syntax table and abbrevs while in vi mode remain as they were in Emacs."
446 (interactive)
447 (if (null vi-mode-old-major-mode) ; very first call for current buffer
448 (vi-mode-setup))
450 (if (eq major-mode 'vi-mode)
451 (message "Already in vi-mode." (ding))
452 (setq vi-mode-old-local-map (current-local-map))
453 (setq vi-mode-old-mode-name mode-name)
454 (setq vi-mode-old-major-mode major-mode)
455 (setq vi-mode-old-case-fold case-fold-search) ; this is needed !!
456 (setq case-fold-search nil) ; exact case match in searching
457 (use-local-map vi-com-map)
458 (setq major-mode 'vi-mode)
459 (setq mode-name "VI")
460 (force-mode-line-update) ; force mode line update
461 (if vi-insert-state ; this is a return from insertion
462 (vi-end-of-insert-state))))
464 (defun vi-ding()
465 "Ding !"
466 (interactive)
467 (ding))
469 (defun vi-save-all-and-exit ()
470 "Save all modified buffers without asking, then exits emacs."
471 (interactive)
472 (save-some-buffers t)
473 (kill-emacs))
475 ;; to be used by "ex" commands
476 (defvar vi-replaced-string nil)
477 (defvar vi-replacing-string nil)
479 (defun vi-ex-cmd ()
480 "Ex commands are not implemented in Evi mode. For some commonly used ex
481 commands, you can use the following alternatives for similar effect :
482 w C-x C-s (save-buffer)
483 wq C-x C-c (save-buffers-kill-emacs)
484 w fname C-x C-w (write-file)
485 e fname C-x C-f (find-file)
486 r fname C-x i (insert-file)
487 s/old/new use q (vi-replace) to do unconditional replace
488 use C-q (vi-query-replace) to do query replace
489 set sw=n M-x set-variable vi-shift-width n "
490 (interactive)
491 ;; (let ((cmd (read-string ":")) (lines 1))
492 ;; (cond ((string-match "s"))))
493 (with-output-to-temp-buffer "*Help*"
494 (princ (documentation 'vi-ex-cmd))
495 (save-excursion
496 (set-buffer standard-output)
497 (help-mode))))
499 (defun vi-undefined ()
500 (interactive)
501 (message "Command key \"%s\" is undefined in Evi."
502 (single-key-description last-command-char))
503 (ding))
505 (defun vi-unimplemented ()
506 (interactive)
507 (message "Command key \"%s\" is not implemented in Evi."
508 (single-key-description last-command-char))
509 (ding))
511 ;;;;;
512 (defun vi-goto-insert-state (repetition &optional prefix-code do-it-now-p)
513 "Go into insert state, the text entered will be repeated if REPETITION > 1.
514 If PREFIX-CODE is given, do it before insertion begins if DO-IT-NOW-P is T.
515 In any case, the prefix-code will be done before each 'redo-insert'.
516 This function expects 'overwrite-mode' being set properly beforehand."
517 (if do-it-now-p (apply (car prefix-code) (cdr prefix-code)))
518 (setq vi-ins-point (point))
519 (setq vi-ins-repetition repetition)
520 (setq vi-ins-prefix-code prefix-code)
521 (setq mode-name vi-mode-old-mode-name)
522 (setq case-fold-search vi-mode-old-case-fold)
523 (use-local-map vi-mode-old-local-map)
524 (setq major-mode vi-mode-old-major-mode)
525 (force-mode-line-update)
526 (setq vi-insert-state t))
528 (defun vi-end-of-insert-state ()
529 "Terminate insertion and set up last change command."
530 (if (or (< (point) vi-ins-point) ;Check if there is any effective change
531 (and (= (point) vi-ins-point) (null vi-ins-prefix-code))
532 (<= vi-ins-repetition 0))
533 (vi-goto-command-state t)
534 (if (> vi-ins-repetition 1)
535 (progn
536 (let ((str (buffer-substring vi-ins-point (point))))
537 (while (> vi-ins-repetition 1)
538 (insert str)
539 (setq vi-ins-repetition (1- vi-ins-repetition))))))
540 (vi-set-last-change-command 'vi-first-redo-insertion vi-ins-point (point)
541 overwrite-mode vi-ins-prefix-code)
542 (vi-goto-command-state t)))
544 (defun vi-first-redo-insertion (begin end &optional overwrite-p prefix-code)
545 "Redo last insertion the first time. Extract the string and save it for
546 future redoes. Do prefix-code if it's given, use overwrite mode if asked."
547 (let ((str (buffer-substring begin end)))
548 (if prefix-code (apply (car prefix-code) (cdr prefix-code)))
549 (if overwrite-p (delete-region (point) (+ (point) (length str))))
550 (insert str)
551 (vi-set-last-change-command 'vi-more-redo-insertion str overwrite-p prefix-code)))
553 (defun vi-more-redo-insertion (str &optional overwrite-p prefix-code)
554 "Redo more insertion : copy string from STR to point, use overwrite mode
555 if overwrite-p is T; apply prefix-code first if it's non-nil."
556 (if prefix-code (apply (car prefix-code) (cdr prefix-code)))
557 (if overwrite-p (delete-region (point) (+ (point) (length str))))
558 (insert str))
560 (defun vi-goto-command-state (&optional from-insert-state-p)
561 "Go to vi-mode command state. If optional arg exists, means return from
562 insert state."
563 (use-local-map vi-com-map)
564 (setq vi-insert-state nil)
565 (if from-insert-state-p
566 (if overwrite-mode
567 (overwrite-mode 0)
568 ; (set-minor-mode 'ins "Insert" nil)
571 (defun vi-kill-line (arg)
572 "kill specified number of lines (=d$), text saved in the kill ring."
573 (interactive "*P")
574 (kill-line arg)
575 (vi-set-last-change-command 'kill-line arg))
577 (defun vi-kill-region (start end)
578 (interactive "*r")
579 (kill-region start end)
580 (vi-set-last-change-command 'kill-region))
582 (defun vi-append-at-end-of-line (arg)
583 "go to end of line and then go into vi insert state."
584 (interactive "*p")
585 (vi-goto-insert-state arg '(end-of-line) t))
587 (defun vi-change-rest-of-line (arg)
588 "Change the rest of (ARG) lines (= c$ in vi)."
589 (interactive "*P")
590 (vi-goto-insert-state 1 (list 'kill-line arg) t))
592 (defun vi-insert-before-first-nonwhite (arg)
593 "(= ^i in vi)"
594 (interactive "*p")
595 (vi-goto-insert-state arg '(back-to-indentation) t))
597 (defun vi-open-above (arg)
598 "open new line(s) above current line and enter insert state."
599 (interactive "*p")
600 (vi-goto-insert-state 1
601 (list (function (lambda (x)
602 (or (beginning-of-line)
603 (open-line x)))) arg)
606 (defun vi-open-below (arg)
607 "open new line(s) and go into insert mode on the last line."
608 (interactive "*p")
609 (vi-goto-insert-state 1
610 (list (function (lambda (x)
611 (or (end-of-line)
612 (open-line x)
613 (forward-line x)))) arg)
616 (defun vi-insert-after (arg)
617 "start vi insert state after cursor."
618 (interactive "*p")
619 (vi-goto-insert-state arg
620 (list (function (lambda ()
621 (if (not (eolp)) (forward-char)))))
624 (defun vi-insert-before (arg)
625 "enter insert state before the cursor."
626 (interactive "*p")
627 (vi-goto-insert-state arg))
629 (defun vi-goto-line (arg)
630 "Go to ARGth line."
631 (interactive "P")
632 (if (null (vi-raw-numeric-prefix arg))
633 (end-of-buffer)
634 (goto-line (vi-prefix-numeric-value arg))))
636 (defun vi-beginning-of-buffer ()
637 "Move point to the beginning of current buffer."
638 (interactive)
639 (goto-char (point-min)))
641 ;;;;; not used now
642 ;;(defvar regexp-search t ; string
643 ;; "*T if search string can contain regular expressions. (= set magic in vi)")
644 ;;;;;
646 (defun vi-isearch-forward (arg)
647 "Incremental search forward. Use regexp version if ARG is non-nil."
648 (interactive "P")
649 (let ((scmd (if arg 'isearch-forward-regexp 'isearch-forward))
650 (opoint (point)))
651 (call-interactively scmd)
652 (if (= opoint (point))
654 (setq vi-search-last-command (if arg 're-search-forward 'search-forward)))))
656 (defun vi-isearch-backward (arg)
657 "Incremental search backward. Use regexp version if ARG is non-nil."
658 (interactive "P")
659 (let ((scmd (if arg 'isearch-backward-regexp 'isearch-backward))
660 (opoint (point)))
661 (call-interactively scmd)
662 (if (= opoint (point))
664 (setq vi-search-last-command (if arg 're-search-backward 'search-backward)))))
666 (defun vi-search-forward (arg string)
667 "Nonincremental search forward. Use regexp version if ARG is non-nil."
668 (interactive (if current-prefix-arg
669 (list t (read-string "regexp/" nil))
670 (list nil (read-string "/" nil))))
671 (setq vi-search-last-command (if arg 're-search-forward 'search-forward))
672 (if (> (length string) 0) (setq search-last-string string))
673 (funcall vi-search-last-command search-last-string nil nil 1))
675 (defun vi-search-backward (arg string)
676 "Nonincremental search backward. Use regexp version if ARG is non-nil."
677 (interactive (if current-prefix-arg
678 (list t (read-string "regexp?" nil))
679 (list nil (read-string "?" nil))))
680 (setq vi-search-last-command (if arg 're-search-backward 'search-backward))
681 (if (> (length string) 0) (setq search-last-string string))
682 (funcall vi-search-last-command search-last-string nil nil 1))
684 (defun vi-repeat-last-search (arg &optional search-command search-string)
685 "Repeat last search command. If optional search-command/string are given,
686 use those instead of the ones saved."
687 (interactive "p")
688 (if (null search-command) (setq search-command vi-search-last-command))
689 (if (null search-string) (setq search-string search-last-string))
690 (if (null search-command)
691 (message "No last search command to repeat." (ding))
692 (funcall search-command search-string nil nil arg)))
694 (defun vi-reverse-last-search (arg &optional search-command search-string)
695 "Redo last search command in reverse direction. If the optional search args
696 are given, use those instead of the ones saved."
697 (interactive "p")
698 (if (null search-command) (setq search-command vi-search-last-command))
699 (if (null search-string) (setq search-string search-last-string))
700 (if (null search-command)
701 (message "No last search command to repeat." (ding))
702 (funcall (cond ((eq search-command 're-search-forward) 're-search-backward)
703 ((eq search-command 're-search-backward) 're-search-forward)
704 ((eq search-command 'search-forward) 'search-backward)
705 ((eq search-command 'search-backward) 'search-forward))
706 search-string nil nil arg)))
708 (defun vi-join-lines (arg)
709 "join ARG lines from current line (default 2), cleaning up white space."
710 (interactive "P")
711 (if (null (vi-raw-numeric-prefix arg))
712 (delete-indentation t)
713 (setq count (vi-prefix-numeric-value arg))
714 (while (>= count 2)
715 (delete-indentation t)
716 (setq count (1- count))))
717 (vi-set-last-change-command 'vi-join-lines arg))
719 (defun vi-backward-kill-line ()
720 "kill the current line. Only works in insert state."
721 (interactive)
722 (if (not vi-insert-state)
724 (beginning-of-line 1)
725 (kill-line nil)))
727 (defun vi-abort-ins ()
728 "abort insert state, kill inserted text and go back to command state."
729 (interactive)
730 (if (not vi-insert-state)
732 (if (> (point) vi-ins-point)
733 (kill-region vi-ins-point (point)))
734 (vi-goto-command-state t)))
736 (defun vi-backward-windowful (count)
737 "Backward COUNT windowfuls. Default is one."
738 (interactive "p")
739 ; (set-mark-command nil)
740 (while (> count 0)
741 (scroll-down nil)
742 (setq count (1- count))))
744 (defun vi-scroll-down-window (count)
745 "Scrolls down window COUNT lines.
746 If COUNT is nil (actually, non-integer), scrolls default amount.
747 The given COUNT is remembered for future scrollings."
748 (interactive "P")
749 (if (integerp count)
750 (setq vi-scroll-amount count))
751 (scroll-up vi-scroll-amount))
753 (defun vi-expose-line-below (count)
754 "Expose COUNT more lines below the current window. Default COUNT is 1."
755 (interactive "p")
756 (scroll-up count))
758 (defun vi-forward-windowful (count)
759 "Forward COUNT windowfuls. Default is one."
760 (interactive "p")
761 ; (set-mark-command nil)
762 (while (> count 0)
763 (scroll-up nil)
764 (setq count (1- count))))
766 (defun vi-next-line (count)
767 "Go down count lines, try to keep at the same column."
768 (interactive "p")
769 (setq this-command 'next-line) ; this is a needed trick
770 (if (= (point) (or (line-move count) (point)))
771 (ding) ; no moving, already at end of buffer
772 (setq last-command 'next-line)))
774 (defun vi-next-line-first-nonwhite (count)
775 "Go down COUNT lines. Stop at first non-white."
776 (interactive "p")
777 (if (= (point) (progn (forward-line count) (back-to-indentation) (point)))
778 (ding))) ; no moving, already at end of buffer
780 (defun vi-previous-line-first-nonwhite (count)
781 "Go up COUNT lines. Stop at first non-white."
782 (interactive "p")
783 (previous-line count)
784 (back-to-indentation))
786 (defun vi-scroll-up-window (count)
787 "Scrolls up window COUNT lines.
788 If COUNT is nil (actually, non-integer), scrolls default amount.
789 The given COUNT is remembered for future scrollings."
790 (interactive "P")
791 (if (integerp count)
792 (setq vi-scroll-amount count))
793 (scroll-down vi-scroll-amount))
795 (defun vi-expose-line-above (count)
796 "Expose COUNT more lines above the current window. Default COUNT is 1."
797 (interactive "p")
798 (scroll-down count))
800 (defun vi-char-argument (arg)
801 "Get following character (could be any CHAR) as part of the prefix argument.
802 Possible prefix-arg cases are NIL, INTEGER, (NIL . CHAR) or (INTEGER . CHAR)."
803 (interactive "P")
804 (let ((char (read-char)))
805 (cond ((null arg) (setq prefix-arg (cons nil char)))
806 ((integerp arg) (setq prefix-arg (cons arg char)))
807 ; This can happen only if the user changed his/her mind for CHAR,
808 ; Or there are some leading "universal-argument"s
809 (t (setq prefix-arg (cons (car arg) char))))))
811 (defun vi-goto-mark (mark-char &optional line-flag)
812 "Go to marked position or line (if line-flag is given).
813 Goto mark '@' means jump into and pop the top mark on the mark ring."
814 (cond ((char-equal mark-char last-command-char) ; `` or ''
815 (exchange-point-and-mark) (if line-flag (back-to-indentation)))
816 ((char-equal mark-char ?@) ; jump and pop mark
817 (set-mark-command t) (if line-flag (back-to-indentation)))
819 (let ((mark (vi-get-mark mark-char)))
820 (if (null mark)
821 (message "Mark register undefined." (vi-ding))
822 (set-mark-command nil)
823 (goto-char mark)
824 (if line-flag (back-to-indentation)))))))
826 (defun vi-goto-line-mark (char)
827 "Go to the line (at first non-white) marked by next char."
828 (interactive "c")
829 (vi-goto-mark char t))
831 (defun vi-goto-char-mark (char)
832 "Go to the char position marked by next mark-char."
833 (interactive "c")
834 (vi-goto-mark char))
836 (defun vi-digit-argument (arg)
837 "Set numeric prefix argument."
838 (interactive "P")
839 (cond ((null arg) (digit-argument arg))
840 ((integerp arg) (digit-argument nil)
841 (setq prefix-arg (* prefix-arg arg)))
842 (t (digit-argument nil) ; in (NIL . CHAR) or (NUM . CHAR) form
843 (setq prefix-arg (cons (* prefix-arg
844 (if (null (car arg)) 1 (car arg)))
845 (cdr arg))))))
847 (defun vi-raw-numeric-prefix (arg)
848 "Return the raw value of numeric part prefix argument."
849 (if (consp arg) (car arg) arg))
851 (defun vi-prefix-numeric-value (arg)
852 "Return numeric meaning of the raw prefix argument. This is a modification
853 to the standard one provided in `callint.c' to handle (_ . CHAR) cases."
854 (cond ((null arg) 1)
855 ((integerp arg) arg)
856 ((consp arg) (if (car arg) (car arg) 1))))
858 (defun vi-reverse-last-find-char (count &optional find-arg)
859 "Reverse last f F t T operation COUNT times. If the optional FIND-ARG
860 is given, it is used instead of the saved one."
861 (interactive "p")
862 (if (null find-arg) (setq find-arg vi-last-find-char))
863 (if (null find-arg)
864 (message "No last find char to repeat." (ding))
865 (vi-find-char (cons (* (car find-arg) -1) (cdr find-arg)) count))) ;6/13/86
867 (defun vi-find-char (arg count)
868 "Find in DIRECTION (1/-1) for CHAR of COUNT'th times on current line.
869 If UPTO-FLAG is T, stop before the char. ARG = (DIRECTION.CHAR.UPTO-FLAG."
870 (let* ((direction (car arg)) (char (car (cdr arg)))
871 (upto-flag (cdr (cdr arg))) (pos (+ (point) direction)))
872 (if (catch 'exit-find-char
873 (while t
874 (cond ((null (char-after pos)) (throw 'exit-find-char nil))
875 ((char-equal (char-after pos) ?\n) (throw 'exit-find-char nil))
876 ((char-equal char (char-after pos)) (setq count (1- count))
877 (if (= count 0)
878 (throw 'exit-find-char
879 (if upto-flag
880 (setq pos (- pos direction))
881 pos)))))
882 (setq pos (+ pos direction))))
883 (goto-char pos)
884 (ding))))
886 (defun vi-repeat-last-find-char (count &optional find-arg)
887 "Repeat last f F t T operation COUNT times. If optional FIND-ARG is given,
888 it is used instead of the saved one."
889 (interactive "p")
890 (if (null find-arg) (setq find-arg vi-last-find-char))
891 (if (null find-arg)
892 (message "No last find char to repeat." (ding))
893 (vi-find-char find-arg count)))
895 (defun vi-backward-find-char (count char)
896 "Find the COUNT'th CHAR backward on current line."
897 (interactive "p\nc")
898 (setq vi-last-find-char (cons -1 (cons char nil)))
899 (vi-repeat-last-find-char count))
901 (defun vi-forward-find-char (count char)
902 "Find the COUNT'th CHAR forward on current line."
903 (interactive "p\nc")
904 (setq vi-last-find-char (cons 1 (cons char nil)))
905 (vi-repeat-last-find-char count))
907 (defun vi-backward-upto-char (count char)
908 "Find upto the COUNT'th CHAR backward on current line."
909 (interactive "p\nc")
910 (setq vi-last-find-char (cons -1 (cons char t)))
911 (vi-repeat-last-find-char count))
913 (defun vi-forward-upto-char (count char)
914 "Find upto the COUNT'th CHAR forward on current line."
915 (interactive "p\nc")
916 (setq vi-last-find-char (cons 1 (cons char t)))
917 (vi-repeat-last-find-char count))
919 (defun vi-end-of-word (count)
920 "Move forward until encountering the end of a word.
921 With argument, do this that many times."
922 (interactive "p")
923 (if (not (eobp)) (forward-char))
924 (if (re-search-forward "\\W*\\w+\\>" nil t count)
925 (backward-char)))
927 (defun vi-replace-1-char (count char)
928 "Replace char after point by CHAR. Repeat COUNT times."
929 (interactive "p\nc")
930 (delete-char count nil) ; don't save in kill ring
931 (setq last-command-char char)
932 (self-insert-command count)
933 (vi-set-last-change-command 'vi-replace-1-char count char))
935 (defun vi-replace-chars (arg)
936 "Replace chars over old ones."
937 (interactive "*p")
938 (overwrite-mode 1)
939 (vi-goto-insert-state arg))
941 (defun vi-substitute-chars (count)
942 "Substitute COUNT chars by the input chars, enter insert state."
943 (interactive "*p")
944 (vi-goto-insert-state 1 (list (function (lambda (c) ; this is a bit tricky
945 (delete-region (point)
946 (+ (point) c))))
947 count) t))
949 (defun vi-substitute-lines (count)
950 "Substitute COUNT lines by the input chars. (=cc in vi)"
951 (interactive "*p")
952 (vi-goto-insert-state 1 (list 'vi-delete-op 'next-line (1- count)) t))
954 (defun vi-prefix-char-value (arg)
955 "Get the char part of the current prefix argument."
956 (cond ((null arg) nil)
957 ((integerp arg) nil)
958 ((consp arg) (cdr arg))
959 (t nil)))
961 (defun vi-operator (arg)
962 "Handling vi operators (d/c/</>/!/=/y). Current implementation requires
963 the key bindings of the operators being fixed."
964 (interactive "P")
965 (catch 'vi-exit-op
966 (let ((this-op-char last-command-char))
967 (setq last-command-char (read-char))
968 (setq this-command (lookup-key vi-com-map (char-to-string last-command-char)))
969 (if (not (eq this-command 'vi-digit-argument))
970 (setq prefix-arg arg)
971 (vi-digit-argument arg)
972 (setq last-command-char (read-char))
973 (setq this-command (lookup-key vi-com-map (char-to-string last-command-char))))
974 (cond ((char-equal this-op-char last-command-char) ; line op
975 (vi-execute-op this-op-char 'next-line
976 (cons (1- (vi-prefix-numeric-value prefix-arg))
977 (vi-prefix-char-value prefix-arg))))
978 ;; We assume any command that has no property 'point-moving-unit'
979 ;; as having that property with the value 'CHAR'. 3/12/86
980 (t ;; (get this-command 'point-moving-unit)
981 (vi-execute-op this-op-char this-command prefix-arg))))))
982 ;; (t (throw 'vi-exit-op (ding)))))))
984 (defun vi-execute-op (op-char motion-command arg)
985 "Execute vi edit operator as specified by OP-CHAR, the operand is the region
986 determined by the MOTION-COMMAND with ARG."
987 (cond ((= op-char ?d)
988 (if (vi-delete-op motion-command arg)
989 (vi-set-last-change-command 'vi-delete-op (vi-repeat-command-of motion-command) arg)))
990 ((= op-char ?c)
991 (if (vi-delete-op motion-command arg)
992 (vi-goto-insert-state 1 (list 'vi-delete-op
993 (vi-repeat-command-of motion-command) arg) nil)))
994 ((= op-char ?y)
995 (if (vi-yank-op motion-command arg)
996 (vi-set-last-change-command 'vi-yank-op (vi-repeat-command-of motion-command) arg)))
997 ((= op-char ?!)
998 (if (vi-shell-op motion-command arg)
999 (vi-set-last-change-command 'vi-shell-op (vi-repeat-command-of motion-command) arg vi-last-shell-command)))
1000 ((= op-char ?<)
1001 (if (vi-shift-op motion-command arg (- vi-shift-width))
1002 (vi-set-last-change-command 'vi-shift-op (vi-repeat-command-of motion-command) arg (- vi-shift-width))))
1003 ((= op-char ?>)
1004 (if (vi-shift-op motion-command arg vi-shift-width)
1005 (vi-set-last-change-command 'vi-shift-op (vi-repeat-command-of motion-command) arg vi-shift-width)))
1006 ((= op-char ?=)
1007 (if (vi-indent-op motion-command arg)
1008 (vi-set-last-change-command 'vi-indent-op (vi-repeat-command-of motion-command) arg)))
1009 ((= op-char ?\\)
1010 (vi-narrow-op motion-command arg))))
1012 (defun vi-repeat-command-of (command)
1013 "Return the command for redo the given command."
1014 (let ((cmd-type (get command 'point-moving-unit)))
1015 (cond ((eq cmd-type 'search) 'vi-repeat-last-search)
1016 ((eq cmd-type 'find) 'vi-repeat-last-find-char)
1017 (t command))))
1019 (defun vi-effective-range (motion-command arg)
1020 "Return (begin . end) of the range spanned by executing the given
1021 MOTION-COMMAND with ARG.
1022 MOTION-COMMAND in ready-to-eval list form is not yet supported."
1023 (save-excursion
1024 (let ((begin (point)) end opoint
1025 (moving-unit (get motion-command 'point-moving-unit)))
1026 (setq prefix-arg arg)
1027 (setq opoint (point))
1028 (command-execute motion-command nil)
1029 ;; Check if there is any effective motion. Note that for single line operation
1030 ;; the motion-command causes no effective point movement (since it moves up or
1031 ;; down zero lines), but it should be counted as effectively moved.
1032 (if (and (= (point) opoint) (not (eq moving-unit 'line)))
1033 (cons opoint opoint) ; no effective motion
1034 (if (eq moving-unit 'region)
1035 (setq begin (or (mark) (point))))
1036 (if (<= begin (point))
1037 (setq end (point))
1038 (setq end begin)
1039 (setq begin (point)))
1040 (cond ((or (eq moving-unit 'match) (eq moving-unit 'find))
1041 (setq end (1+ end)))
1042 ((eq moving-unit 'line)
1043 (goto-char begin) (beginning-of-line) (setq begin (point))
1044 (goto-char end) (next-line 1) (beginning-of-line) (setq end (point))))
1045 (if (> end (point-max)) (setq end (point-max))) ; force in buffer region
1046 (cons begin end)))))
1048 (defun vi-delete-op (motion-command arg)
1049 "Delete range specified by MOTION-COMMAND with ARG."
1050 (let* ((range (vi-effective-range motion-command arg))
1051 (begin (car range)) (end (cdr range)) reg)
1052 (if (= begin end)
1053 nil ; point not moved, abort op
1054 (setq reg (vi-prefix-char-value arg))
1055 (if (null reg)
1056 (kill-region begin end) ; kill ring as unnamed registers
1057 (if (and (>= reg ?A) (<= reg ?Z))
1058 (append-to-register (downcase reg) begin end t)
1059 (copy-to-register reg begin end t)))
1060 t)))
1062 (defun vi-yank-op (motion-command arg)
1063 "Yank (in vi sense) range specified by MOTION-COMMAND with ARG."
1064 (let* ((range (vi-effective-range motion-command arg))
1065 (begin (car range)) (end (cdr range)) reg)
1066 (if (= begin end)
1067 nil ; point not moved, abort op
1068 (setq reg (vi-prefix-char-value arg))
1069 (if (null reg)
1070 (copy-region-as-kill begin end); kill ring as unnamed registers
1071 (if (and (>= reg ?A) (<= reg ?Z))
1072 (append-to-register (downcase reg) begin end nil)
1073 (copy-to-register reg begin end nil)))
1074 t)))
1076 (defun vi-yank-line (arg)
1077 "Yank (in vi sense) lines (= `yy' command)."
1078 (interactive "*P")
1079 (setq arg (cons (1- (vi-prefix-numeric-value arg)) (vi-prefix-char-value arg)))
1080 (if (vi-yank-op 'next-line arg)
1081 (vi-set-last-change-command 'vi-yank-op 'next-line arg)))
1083 (defun vi-string-end-with-nl-p (string)
1084 "See if STRING ends with a newline char.
1085 Used in checking whether the yanked text should be put back as lines or not."
1086 (= (aref string (1- (length string))) ?\n))
1088 (defun vi-put-before (arg &optional after-p)
1089 "Put yanked (in vi sense) text back before/above cursor.
1090 If a numeric prefix value (currently it should be >1) is given, put back
1091 text as lines. If the optional after-p is given, put after/below the cursor."
1092 (interactive "P")
1093 (let ((reg (vi-prefix-char-value arg)) put-text)
1094 (if (and reg (or (< reg ?1) (> reg ?9)) (null (get-register reg)))
1095 (error "Nothing in register %c" reg)
1096 (if (null reg) (setq reg ?1)) ; the default is the last text killed
1097 (setq put-text
1098 (cond
1099 ((and (>= reg ?1) (<= reg ?9))
1100 (setq this-command 'yank) ; So we may yank-pop !!
1101 (current-kill (- reg ?0 1) 'do-not-rotate))
1102 ((stringp (get-register reg)) (get-register reg))
1103 (t (error "Register %c is not containing text string" reg))))
1104 (if (vi-string-end-with-nl-p put-text) ; put back text as lines
1105 (if after-p
1106 (progn (next-line 1) (beginning-of-line))
1107 (beginning-of-line))
1108 (if after-p (forward-char 1)))
1109 (push-mark (point))
1110 (insert put-text)
1111 (exchange-point-and-mark)
1112 ;; (back-to-indentation) ; this is not allowed if we allow yank-pop
1113 (vi-set-last-change-command 'vi-put-before arg after-p))))
1115 (defun vi-put-after (arg)
1116 "Put yanked (in vi sense) text back after/below cursor."
1117 (interactive "P")
1118 (vi-put-before arg t))
1120 (defun vi-shell-op (motion-command arg &optional shell-command)
1121 "Perform shell command (as filter).
1122 Performs command on range specified by MOTION-COMMAND
1123 with ARG. If SHELL-COMMAND is not given, ask for one from minibuffer.
1124 If char argument is given, it directs the output to a *temp* buffer."
1125 (let* ((range (vi-effective-range motion-command arg))
1126 (begin (car range)) (end (cdr range)))
1127 (if (= begin end)
1128 nil ; point not moved, abort op
1129 (cond ((null shell-command)
1130 (setq shell-command (read-string "!" nil))
1131 (setq vi-last-shell-command shell-command)))
1132 (shell-command-on-region begin end shell-command (not (vi-prefix-char-value arg)))
1133 t)))
1135 (defun vi-shift-op (motion-command arg amount)
1136 "Perform shift command on range specified by MOTION-COMMAND with ARG for
1137 AMOUNT on each line. Negative amount means shift left.
1138 SPECIAL FEATURE: char argument can be used to specify shift amount(1-9)."
1139 (let* ((range (vi-effective-range motion-command arg))
1140 (begin (car range)) (end (cdr range)))
1141 (if (= begin end)
1142 nil ; point not moved, abort op
1143 (if (vi-prefix-char-value arg)
1144 (setq amount (if (> amount 0)
1145 (- (vi-prefix-char-value arg) ?0)
1146 (- ?0 (vi-prefix-char-value arg)))))
1147 (indent-rigidly begin end amount)
1148 t)))
1150 (defun vi-indent-op (motion-command arg)
1151 "Perform indent command on range specified by MOTION-COMMAND with ARG."
1152 (let* ((range (vi-effective-range motion-command arg))
1153 (begin (car range)) (end (cdr range)))
1154 (if (= begin end)
1155 nil ; point not moved, abort op
1156 (indent-region begin end nil) ; insert TAB as indent command
1157 t)))
1159 (defun vi-narrow-op (motion-command arg)
1160 "Narrow to region specified by MOTION-COMMAND with ARG."
1161 (let* ((range (vi-effective-range motion-command arg))
1162 (begin (car range)) (end (cdr range)) reg)
1163 (if (= begin end)
1164 nil ; point not moved, abort op
1165 (narrow-to-region begin end))))
1167 (defun vi-get-mark (char)
1168 "Return contents of vi mark register named CHAR, or nil if undefined."
1169 (cdr (assq char vi-mark-alist)))
1171 (defun vi-set-mark (char)
1172 "Set contents of vi mark register named CHAR to current point.
1173 '@' is the special anonymous mark register."
1174 (interactive "c")
1175 (if (char-equal char ?@)
1176 (set-mark-command nil)
1177 (let ((aelt (assq char vi-mark-alist)))
1178 (if aelt
1179 (move-marker (cdr aelt) (point)) ; fixed 6/12/86
1180 (setq aelt (cons char (copy-marker (point))))
1181 (setq vi-mark-alist (cons aelt vi-mark-alist))))))
1183 (defun vi-find-matching-paren ()
1184 "Locate the matching paren. It's a hack right now."
1185 (interactive)
1186 (cond ((looking-at "[[({]") (forward-sexp 1) (backward-char 1))
1187 ((looking-at "[])}]") (forward-char 1) (backward-sexp 1))
1188 (t (ding))))
1190 (defun vi-backward-blank-delimited-word (count)
1191 "Backward COUNT blank-delimited words."
1192 (interactive "p")
1193 (if (re-search-backward "[ \t\n\`][^ \t\n\`]+" nil t count)
1194 (if (not (bobp)) (forward-char 1))))
1196 (defun vi-forward-blank-delimited-word (count)
1197 "Forward COUNT blank-delimited words."
1198 (interactive "p")
1199 (if (re-search-forward "[^ \t\n]*[ \t\n]+[^ \t\n]" nil t count)
1200 (if (not (eobp)) (backward-char 1))))
1202 (defun vi-end-of-blank-delimited-word (count)
1203 "Forward to the end of the COUNT'th blank-delimited word."
1204 (interactive "p")
1205 (if (re-search-forward "[^ \t\n\']+[ \t\n\']" nil t count)
1206 (if (not (eobp)) (backward-char 2))))
1208 (defun vi-home-window-line (arg)
1209 "To window home or arg'th line from the top of the window."
1210 (interactive "p")
1211 (move-to-window-line (1- arg))
1212 (back-to-indentation))
1214 (defun vi-last-window-line (arg)
1215 "To window last line or arg'th line from the bottom of the window."
1216 (interactive "p")
1217 (move-to-window-line (- arg))
1218 (back-to-indentation))
1220 (defun vi-middle-window-line ()
1221 "To the middle line of the window."
1222 (interactive)
1223 (move-to-window-line nil)
1224 (back-to-indentation))
1226 (defun vi-forward-word (count)
1227 "Stop at the beginning of the COUNT'th words from point."
1228 (interactive "p")
1229 (if (re-search-forward "\\w*\\W+\\<" nil t count)
1231 (vi-ding)))
1233 (defun vi-set-last-change-command (fun &rest args)
1234 "Set (FUN . ARGS) as the `last-change-command'."
1235 (setq vi-last-change-command (cons fun args)))
1237 (defun vi-redo-last-change-command (count &optional command)
1238 "Redo last change command COUNT times. If the optional COMMAND is given,
1239 it is used instead of the current `last-change-command'."
1240 (interactive "p")
1241 (if (null command)
1242 (setq command vi-last-change-command))
1243 (if (null command)
1244 (message "No last change command available.")
1245 (while (> count 0)
1246 (apply (car command) (cdr command))
1247 (setq count (1- count)))))
1249 (defun vi-kill-char (count)
1250 "Kill COUNT chars from current point."
1251 (interactive "*p")
1252 (delete-char count t) ; save in kill ring
1253 (vi-set-last-change-command 'delete-char count t))
1255 (defun vi-transpose-objects (arg unit)
1256 "Transpose objects.
1257 The following char specifies unit of objects to be
1258 transposed -- \"c\" for chars, \"l\" for lines, \"w\" for words, \"s\" for
1259 sexp, \"p\" for paragraph.
1260 For the use of the prefix-arg, refer to individual functions called."
1261 (interactive "*P\nc")
1262 (if (char-equal unit ??)
1263 (progn
1264 (message "Transpose: c(har), l(ine), p(aragraph), s(-exp), w(ord),")
1265 (setq unit (read-char))))
1266 (vi-set-last-change-command 'vi-transpose-objects arg unit)
1267 (cond ((char-equal unit ?c) (transpose-chars arg))
1268 ((char-equal unit ?l) (transpose-lines (vi-prefix-numeric-value arg)))
1269 ((char-equal unit ?p) (transpose-paragraphs (vi-prefix-numeric-value arg)))
1270 ((char-equal unit ?s) (transpose-sexps (vi-prefix-numeric-value arg)))
1271 ((char-equal unit ?w) (transpose-words (vi-prefix-numeric-value arg)))
1272 (t (vi-transpose-objects arg ??))))
1274 (defun vi-query-replace (arg)
1275 "Query replace, use regexp version if ARG is non-nil."
1276 (interactive "*P")
1277 (let ((rcmd (if arg 'query-replace-regexp 'query-replace)))
1278 (call-interactively rcmd nil)))
1280 (defun vi-replace (arg)
1281 "Replace strings, use regexp version if ARG is non-nil."
1282 (interactive "*P")
1283 (let ((rcmd (if arg 'replace-regexp 'replace-string)))
1284 (call-interactively rcmd nil)))
1286 (defun vi-adjust-window (arg position)
1287 "Move current line to the top/center/bottom of the window."
1288 (interactive "p\nc")
1289 (cond ((char-equal position ?\r) (recenter 0))
1290 ((char-equal position ?-) (recenter -1))
1291 ((char-equal position ?.) (recenter (/ (window-height) 2)))
1292 (t (message "Move current line to: \\r(top) -(bottom) .(middle)")
1293 (setq position (read-char))
1294 (vi-adjust-window arg position))))
1296 (defun vi-goto-column (col)
1297 "Go to given column of the current line."
1298 (interactive "p")
1299 (let ((opoint (point)))
1300 (beginning-of-line)
1301 (while (> col 1)
1302 (if (eolp)
1303 (setq col 0)
1304 (forward-char 1)
1305 (setq col (1- col))))
1306 (if (= col 1)
1308 (goto-char opoint)
1309 (ding))))
1311 (defun vi-name-last-change-or-macro (arg char)
1312 "Give name to the last change command or just defined kbd macro.
1313 If prefix ARG is given, name last macro, otherwise name last change command.
1314 The following CHAR will be the name for the command or macro."
1315 (interactive "P\nc")
1316 (if arg
1317 (name-last-kbd-macro (intern (char-to-string char)))
1318 (if (eq (car vi-last-change-command) 'vi-first-redo-insertion)
1319 (let* ((args (cdr vi-last-change-command)) ; save the insertion text
1320 (str (buffer-substring (nth 0 args) (nth 1 args)))
1321 (overwrite-p (nth 2 args))
1322 (prefix-code (nth 3 args)))
1323 (vi-set-last-change-command 'vi-more-redo-insertion str
1324 overwrite-p prefix-code)))
1325 (fset (intern (char-to-string char)) vi-last-change-command)))
1327 (defun vi-call-named-change-or-macro (count char)
1328 "Execute COUNT times the keyboard macro definition named by the following CHAR."
1329 (interactive "p\nc")
1330 (if (stringp (symbol-function (intern (char-to-string char))))
1331 (execute-kbd-macro (intern (char-to-string char)) count)
1332 (vi-redo-last-change-command count (symbol-function (intern (char-to-string char))))))
1334 (defun vi-change-case (arg) ; could be made as an operator ?
1335 "Change the case of the char after point."
1336 (interactive "*p")
1337 (catch 'exit
1338 (if (looking-at "[a-z]")
1339 (upcase-region (point) (+ (point) arg))
1340 (if (looking-at "[A-Z]")
1341 (downcase-region (point) (+ (point) arg))
1342 (ding)
1343 (throw 'exit nil)))
1344 (vi-set-last-change-command 'vi-change-case arg) ;should avoid redundant save
1345 (forward-char arg)))
1347 (defun vi-ask-for-info (char)
1348 "Inquire status info. The next CHAR will specify the particular info requested."
1349 (interactive "c")
1350 (cond ((char-equal char ?l) (what-line))
1351 ((char-equal char ?c) (what-cursor-position))
1352 ((char-equal char ?p) (what-page))
1353 (t (message "Ask for: l(ine number), c(ursor position), p(age number)")
1354 (setq char (read-char))
1355 (vi-ask-for-info char))))
1357 (defun vi-mark-region (arg region)
1358 "Mark region appropriately. The next char REGION is d(efun),s(-exp),b(uffer),
1359 p(aragraph), P(age), f(unction in C/Pascal etc.), w(ord), e(nd of sentence),
1360 l(ines)."
1361 (interactive "p\nc")
1362 (cond ((char-equal region ?d) (mark-defun))
1363 ((char-equal region ?s) (mark-sexp arg))
1364 ((char-equal region ?b) (mark-whole-buffer))
1365 ((char-equal region ?p) (mark-paragraph))
1366 ((char-equal region ?P) (mark-page arg))
1367 ((char-equal region ?f) (mark-c-function))
1368 ((char-equal region ?w) (mark-word arg))
1369 ((char-equal region ?e) (mark-end-of-sentence arg))
1370 ((char-equal region ?l) (vi-mark-lines arg))
1371 (t (message "Mark: d(efun),s(-exp),b(uf),p(arag),P(age),f(unct),w(ord),e(os),l(ines)")
1372 (setq region (read-char))
1373 (vi-mark-region arg region))))
1375 (defun vi-mark-lines (num)
1376 "Mark NUM of lines from current line as current region."
1377 (beginning-of-line 1)
1378 (push-mark)
1379 (end-of-line num))
1381 (defun vi-verify-spelling (arg unit)
1382 "Verify spelling for the objects specified by char UNIT : [b(uffer),
1383 r(egion), s(tring), w(ord) ]."
1384 (interactive "P\nc")
1385 (setq prefix-arg arg) ; seems not needed
1386 (cond ((char-equal unit ?b) (call-interactively 'spell-buffer))
1387 ((char-equal unit ?r) (call-interactively 'spell-region))
1388 ((char-equal unit ?s) (call-interactively 'spell-string))
1389 ((char-equal unit ?w) (call-interactively 'spell-word))
1390 (t (message "Spell check: b(uffer), r(egion), s(tring), w(ord)")
1391 (setq unit (read-char))
1392 (vi-verify-spelling arg unit))))
1394 (defun vi-do-old-mode-C-c-command (arg)
1395 "This is a hack for accessing mode specific C-c commands in vi-mode."
1396 (interactive "P")
1397 (let ((cmd (lookup-key vi-mode-old-local-map
1398 (concat "\C-c" (char-to-string (read-char))))))
1399 (if (catch 'exit-vi-mode ; kludge hack due to dynamic binding
1400 ; of case-fold-search
1401 (if (null cmd)
1402 (progn (ding) nil)
1403 (let ((case-fold-search vi-mode-old-case-fold)) ; a hack
1404 (setq prefix-arg arg)
1405 (command-execute cmd nil)
1406 nil)))
1407 (progn
1408 (vi-back-to-old-mode)
1409 (setq prefix-arg arg)
1410 (command-execute cmd nil)))))
1412 (defun vi-quote-words (arg char)
1413 "Quote ARG words from the word point is on with pattern specified by CHAR.
1414 Currently, CHAR could be [,{,(,\",',`,<,*, etc."
1415 (interactive "*p\nc")
1416 (while (not (string-match "[[({<\"'`*]" (char-to-string char)))
1417 (message "Enter any of [,{,(,<,\",',`,* as quoting character.")
1418 (setq char (read-char)))
1419 (vi-set-last-change-command 'vi-quote-words arg char)
1420 (if (not (looking-at "\\<")) (forward-word -1))
1421 (insert char)
1422 (cond ((char-equal char ?[) (setq char ?]))
1423 ((char-equal char ?{) (setq char ?}))
1424 ((char-equal char ?<) (setq char ?>))
1425 ((char-equal char ?() (setq char ?)))
1426 ((char-equal char ?`) (setq char ?')))
1427 (vi-end-of-word arg)
1428 (forward-char 1)
1429 (insert char))
1431 (defun vi-locate-def ()
1432 "Locate definition in current file for the name before the point.
1433 It assumes a `(def..' always starts at the beginning of a line."
1434 (interactive)
1435 (let (name)
1436 (save-excursion
1437 (setq name (buffer-substring (progn (vi-backward-blank-delimited-word 1)
1438 (skip-chars-forward "^a-zA-Z")
1439 (point))
1440 (progn (vi-end-of-blank-delimited-word 1)
1441 (forward-char)
1442 (skip-chars-backward "^a-zA-Z")
1443 (point)))))
1444 (set-mark-command nil)
1445 (goto-char (point-min))
1446 (if (re-search-forward (concat "^(def[unvarconst ]*" name) nil t)
1448 (message "No definition for \"%s\" in current file." name (ding))
1449 (set-mark-command t))))
1451 (defun vi-split-open-line (arg)
1452 "Insert a newline and leave point before it.
1453 With ARG, inserts that many newlines."
1454 (interactive "*p")
1455 (vi-goto-insert-state 1
1456 (list (function (lambda (arg)
1457 (let ((flag (and (bolp) (not (bobp)))))
1458 (if flag (forward-char -1))
1459 (while (> arg 0)
1460 (save-excursion
1461 (insert ?\n)
1462 (if fill-prefix (insert fill-prefix)))
1463 (setq arg (1- arg)))
1464 (if flag (forward-char 1))))) arg)
1467 ;;; vi.el ends here