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