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