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