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