root/trunk/lisp/emulation/vi.el

Revision 4220, 56.9 kB (checked in by miyoshi, 9 months ago)

Sync up with Emacs22.2.

  • Property svn:eol-style set to LF
Line 
1 ;;; vi.el --- major mode for emulating "vi" editor under GNU Emacs
2
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.
5
6 ;; This file is part of GNU Emacs.
7
8 ;; Author: Neal Ziring <nz@rsch.wisc.edu>
9 ;;      Felix S. T. Wu <wu@crys.wisc.edu>
10 ;; Keywords: emulations
11
12 ;;; Commentary:
13
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)
17
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.
34
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.
39
40 ;;; Code:
41
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)
46
47 (if (null (where-is-internal 'vi-switch-mode (current-local-map)))
48     (define-key ctl-x-map "~" 'vi-switch-mode))
49
50 (defvar vi-tilde-map nil
51   "Keymap used for \\[vi-switch-mode] prefix key.  Link to various major modes.")
52
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))
74
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
88
89
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")))
99
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))))
110
111 (defun vi-readonly-mode ()
112   "Toggle current buffer's readonly flag."
113   (interactive)
114   (setq buffer-read-only (not buffer-read-only)))
115
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.")
120
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)
151
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)
157
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)
175
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)
185
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
193
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)
221
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)
228
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)
255
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))
261
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)
308
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 'c-mark-function 'point-moving-unit 'region)
318 ;;;
319
320 (defvar vi-mark-alist nil
321   "Alist of (NAME . MARK), marks are local to each buffer.")
322
323 (defvar vi-scroll-amount (/ (window-height) 2)
324   "Default amount of lines for scrolling (used by \"^D\"/\"^U\").")
325
326 (defvar vi-shift-width 4
327   "Shift amount for \"<\"/\">\" operators.")
328
329 (defvar vi-ins-point nil                ; integer
330   "Last insertion point.  Should use `mark' instead.")
331
332 (defvar vi-ins-length nil               ; integer
333   "Length of last insertion.")
334
335 (defvar vi-ins-repetition nil           ; integer
336   "The repetition required for last insertion.")
337
338 (defvar vi-ins-overwrt-p nil            ; boolean
339   "T if last insertion was a replace actually.")
340
341 (defvar vi-ins-prefix-code nil          ; ready-to-eval sexp
342   "Code to be eval'ed before (redo-)insertion begins.")
343
344 (defvar vi-last-find-char nil           ; cons cell
345   "Save last direction, char and upto-flag used for char finding.")
346
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.")
350
351 (defvar vi-last-shell-command nil       ; last shell op command line
352   "Save last shell command given for \"!\" operator.")
353
354 (defvar vi-insert-state nil             ; boolean
355   "Non-nil if it is in insert state.")
356
357 ; in "loaddefs.el"
358 ;(defvar search-last-string ""
359 ;  "Last string search for by a search command.")
360
361 (defvar vi-search-last-command nil      ; (re-)search-forward(backward)
362   "Save last search command for possible redo.")
363
364 (defvar vi-mode-old-local-map nil
365   "Save the local-map used before entering vi-mode.")
366
367 (defvar vi-mode-old-mode-name nil
368   "Save the mode-name before entering vi-mode.")
369
370 (defvar vi-mode-old-major-mode nil
371   "Save the major-mode before entering vi-mode.")
372
373 (defvar vi-mode-old-case-fold nil)
374
375 ;(defconst vi-add-to-mode-line-1
376 ;  '(overwrite-mode nil " Insert"))
377
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)
381
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))
407
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.
413
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.
419
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.
422
423 Major differences between this mode and real vi :
424
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.
430
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.
441
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.
451
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))
456
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))))
470
471 (defun vi-ding()
472   "Ding !"
473   (interactive)
474   (ding))
475
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))
481
482 ;; to be used by "ex" commands
483 (defvar vi-replaced-string nil)
484 (defvar vi-replacing-string nil)
485
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))))
505
506 (defun vi-undefined ()
507   (interactive)
508   (message "Command key \"%s\" is undefined in Evi."
509            (single-key-description last-command-char))
510   (ding))
511
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))
517
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))
534
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)))
550
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)))
559
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))
566
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)
576         )))
577
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))
583
584 (defun vi-kill-region (start end)
585   (interactive "*r")
586   (kill-region start end)
587   (vi-set-last-change-command 'kill-region))
588
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))
593
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))
598
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))
603
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)
611                         t))
612
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)
621                         t))
622
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)))))
629                          t))
630
631 (defun vi-insert-before (arg)
632   "enter insert state before the cursor."
633   (interactive "*p")
634   (vi-goto-insert-state arg))
635
636 (defun vi-goto-line (arg)
637    "Go to ARGth line."
638    (interactive "P")
639    (if (null (vi-raw-numeric-prefix arg))
640        (with-no-warnings
641          (end-of-buffer))
642      (goto-line (vi-prefix-numeric-value arg))))
643
644 (defun vi-beginning-of-buffer ()
645   "Move point to the beginning of current buffer."
646   (interactive)
647   (goto-char (point-min)))
648
649 ;;;;; not used now
650 ;;(defvar regexp-search t               ; string
651 ;;  "*T if search string can contain regular expressions. (= set magic in vi)")
652 ;;;;;
653
654 (defun vi-isearch-forward (arg)
655   "Incremental search forward.  Use regexp version if ARG is non-nil."
656   (interactive "P")
657   (let ((scmd (if arg 'isearch-forward-regexp 'isearch-forward))
658         (opoint (point)))
659     (call-interactively scmd)
660     (if (= opoint (point))
661         nil
662       (setq vi-search-last-command (if arg 're-search-forward 'search-forward)))))
663
664 (defun vi-isearch-backward (arg)
665   "Incremental search backward.  Use regexp version if ARG is non-nil."
666   (interactive "P")
667   (let ((scmd (if arg 'isearch-backward-regexp 'isearch-backward))
668         (opoint (point)))
669     (call-interactively scmd)
670     (if (= opoint (point))
671         nil
672       (setq vi-search-last-command (if arg 're-search-backward 'search-backward)))))
673
674 (defun vi-search-forward (arg string)
675    "Nonincremental search forward. Use regexp version if ARG is non-nil."
676    (interactive (if current-prefix-arg
677                     (list t (read-string "regexp/" nil))
678                   (list nil (read-string "/" nil))))
679    (setq vi-search-last-command (if arg 're-search-forward 'search-forward))
680    (if (> (length string) 0)
681        (isearch-update-ring string arg))
682    (funcall vi-search-last-command string nil nil 1))
683
684 (defun vi-search-backward (arg string)
685    "Nonincremental search backward.  Use regexp version if ARG is non-nil."
686    (interactive (if current-prefix-arg
687                     (list t (read-string "regexp?" nil))
688                   (list nil (read-string "?" nil))))
689    (setq vi-search-last-command (if arg 're-search-backward 'search-backward))
690    (if (> (length string) 0)
691        (isearch-update-ring string arg))
692    (funcall vi-search-last-command string nil nil 1))
693
694 (defun vi-repeat-last-search (arg &optional search-command search-string)
695   "Repeat last search command.
696 If optional search-command/string are given,
697 use those instead of the ones saved."
698   (interactive "p")
699   (if (null search-command) (setq search-command vi-search-last-command))
700   (if (null search-string)
701       (setq search-string
702             (car (if (memq search-command
703                            '(re-search-forward re-search-backward))
704                      regexp-search-ring
705                    search-ring))))
706   (if (null search-command)
707       (progn (ding) (message "No last search command to repeat."))
708     (funcall search-command search-string nil nil arg)))
709
710 (defun vi-reverse-last-search (arg &optional search-command search-string)
711   "Redo last search command in reverse direction.
712 If the optional search args are given, use those instead of the ones saved."
713   (interactive "p")
714   (if (null search-command) (setq search-command vi-search-last-command))
715   (if (null search-string)
716       (setq search-string
717             (car (if (memq search-command
718                            '(re-search-forward re-search-backward))
719                      regexp-search-ring
720                    search-ring))))
721   (if (null search-command)
722       (progn (ding) (message "No last search command to repeat."))
723     (funcall (cond ((eq search-command 're-search-forward) 're-search-backward)
724                    ((eq search-command 're-search-backward) 're-search-forward)
725                    ((eq search-command 'search-forward) 'search-backward)
726                    ((eq search-command 'search-backward) 'search-forward))
727              search-string nil nil arg)))
728
729 (defun vi-join-lines (arg)
730    "join ARG lines from current line (default 2), cleaning up white space."
731    (interactive "P")
732    (if (null (vi-raw-numeric-prefix arg))
733        (delete-indentation t)
734      (let ((count (vi-prefix-numeric-value arg)))
735        (while (>= count 2)
736          (delete-indentation t)
737          (setq count (1- count)))))
738    (vi-set-last-change-command 'vi-join-lines arg))
739
740 (defun vi-backward-kill-line ()
741    "kill the current line.  Only works in insert state."
742    (interactive)
743    (if (not vi-insert-state)
744        nil
745      (beginning-of-line 1)
746      (kill-line nil)))
747
748 (defun vi-abort-ins ()
749   "abort insert state, kill inserted text and go back to command state."
750   (interactive)
751   (if (not vi-insert-state)
752       nil
753     (if (> (point) vi-ins-point)
754            (kill-region vi-ins-point (point)))
755     (vi-goto-command-state t)))
756
757 (defun vi-backward-windowful (count)
758   "Backward COUNT windowfuls. Default is one."
759   (interactive "p")
760 ; (set-mark-command nil)
761   (while (> count 0)
762     (scroll-down nil)
763     (setq count (1- count))))
764
765 (defun vi-scroll-down-window (count)
766   "Scrolls down window COUNT lines.
767 If COUNT is nil (actually, non-integer), scrolls default amount.
768 The given COUNT is remembered for future scrollings."
769   (interactive "P")
770   (if (integerp count)
771       (setq vi-scroll-amount count))
772   (scroll-up vi-scroll-amount))
773
774 (defun vi-expose-line-below (count)
775   "Expose COUNT more lines below the current window.  Default COUNT is 1."
776   (interactive "p")
777   (scroll-up count))
778
779 (defun vi-forward-windowful (count)
780   "Forward COUNT windowfuls. Default is one."
781   (interactive "p")
782 ; (set-mark-command nil)
783   (while (> count 0)
784     (scroll-up nil)
785     (setq count (1- count))))
786
787 (defun vi-next-line (count)
788   "Go down count lines, try to keep at the same column."
789   (interactive "p")
790   (setq this-command 'next-line)        ; this is a needed trick
791   (if (= (point) (progn (line-move count) (point)))
792       (ding)                            ; no moving, already at end of buffer
793     (setq last-command 'next-line)))
794
795 (defun vi-next-line-first-nonwhite (count)
796   "Go down COUNT lines.  Stop at first non-white."
797   (interactive "p")
798   (if (= (point) (progn (forward-line count) (back-to-indentation) (point)))
799       (ding)))                          ; no moving, already at end of buffer
800
801 (defun vi-previous-line-first-nonwhite (count)
802   "Go up COUNT lines.  Stop at first non-white."
803   (interactive "p")
804   (previous-line count)
805   (back-to-indentation))
806
807 (defun vi-scroll-up-window (count)
808   "Scrolls up window COUNT lines.
809 If COUNT is nil (actually, non-integer), scrolls default amount.
810 The given COUNT is remembered for future scrollings."
811   (interactive "P")
812   (if (integerp count)
813       (setq vi-scroll-amount count))
814   (scroll-down vi-scroll-amount))
815
816 (defun vi-expose-line-above (count)
817   "Expose COUNT more lines above the current window.  Default COUNT is 1."
818   (interactive "p")
819   (scroll-down count))
820
821 (defun vi-char-argument (arg)
822   "Get following character (could be any CHAR) as part of the prefix argument.
823 Possible prefix-arg cases are nil, INTEGER, (nil . CHAR) or (INTEGER . CHAR)."
824   (interactive "P")
825   (let ((char (read-char)))
826     (cond ((null arg) (setq prefix-arg (cons nil char)))
827           ((integerp arg) (setq prefix-arg (cons arg char)))
828           ; This can happen only if the user changed his/her mind for CHAR,
829           ; Or there are some leading "universal-argument"s
830           (t (setq prefix-arg (cons (car arg) char))))))
831
832 (defun vi-goto-mark (mark-char &optional line-flag)
833   "Go to marked position or line (if line-flag is given).
834 Goto mark '@' means jump into and pop the top mark on the mark ring."
835   (cond ((char-equal mark-char last-command-char)       ; `` or ''
836          (exchange-point-and-mark