Mercurial > emacs
annotate lisp/emulation/vip.el @ 3207:257a856de952
* s/linux.h (SIGNALS_VIA_CHARACTERS): Try this out for a bit.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Fri, 28 May 1993 07:39:58 +0000 |
parents | d98c49df2acd |
children | 093c4dc7e884 |
rev | line source |
---|---|
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
1 ;;; vip.el --- a VI Package for GNU Emacs |
11 | 2 |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
3 ;; Author: Masahiko Sato <ms@sail.stanford.edu> |
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
4 ;; Version: 3.5 |
812
485e82a8acb5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
773
diff
changeset
|
5 ;; Keywords: emulations |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
6 |
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
7 ;;; Commentary: |
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
8 |
2319
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
9 ;; A full-featured vi(1) emulator. |
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
10 ;; |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
11 ;; In Japan, the author's address is: masahiko@sato.riec.tohoku.junet |
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
12 ;; |
11 | 13 ;; Send suggestions and bug reports to one of the above addresses. |
14 ;; When you report a bug, be sure to include the version number of VIP and | |
15 ;; Emacs you are using. | |
16 | |
17 ;; Execute info command by typing "M-x info" to get information on VIP. | |
18 | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
19 ;;; Code: |
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
20 |
11 | 21 ;; external variables |
22 | |
23 (defvar vip-emacs-local-map nil | |
24 "Local map used in emacs mode. \(buffer specific\)") | |
25 | |
26 (defvar vip-insert-local-map nil | |
27 "Local map used in insert command mode. \(buffer specific\)") | |
28 | |
29 (make-variable-buffer-local 'vip-emacs-local-map) | |
30 (make-variable-buffer-local 'vip-insert-local-map) | |
31 | |
32 (defvar vip-insert-point nil | |
33 "Remember insert point as a marker. \(buffer specific\)") | |
34 | |
35 (set-default 'vip-insert-point (make-marker)) | |
36 (make-variable-buffer-local 'vip-insert-point) | |
37 | |
38 (defvar vip-com-point nil | |
39 "Remember com point as a marker. \(buffer specific\)") | |
40 | |
41 (set-default 'vip-com-point (make-marker)) | |
42 (make-variable-buffer-local 'vip-com-point) | |
43 | |
44 (defvar vip-current-mode nil | |
45 "Current mode. One of emacs-mode, vi-mode, insert-mode.") | |
46 | |
47 (make-variable-buffer-local 'vip-current-mode) | |
48 (setq-default vip-current-mode 'emacs-mode) | |
49 | |
50 (defvar vip-emacs-mode-line-buffer-identification nil | |
51 "value of mode-line-buffer-identification in emacs-mode.") | |
52 (make-variable-buffer-local 'vip-emacs-mode-line-buffer-identification) | |
53 (setq-default vip-emacs-mode-line-buffer-identification | |
54 '("Emacs: %17b")) | |
55 | |
56 (defvar vip-current-major-mode nil | |
57 "vip-current-major-mode is the major-mode vi considers it is now. | |
58 \(buffer specific\)") | |
59 | |
60 (make-variable-buffer-local 'vip-current-major-mode) | |
61 | |
62 (defvar vip-last-shell-com nil | |
63 "last shell command executed by ! command") | |
64 | |
65 (defvar vip-use-register nil | |
66 "name of register to store deleted or yanked strings.") | |
67 | |
68 (defvar vip-d-com nil | |
69 "If non-nil, it's value is a list (M-COM VAL COM), and is used to | |
70 re-execute last destrcutive command") | |
71 | |
72 (defconst vip-shift-width 8 | |
73 "*The number of colums shifted by > and < command.") | |
74 | |
75 (defconst vip-re-replace nil | |
76 "*If t then do regexp replace, if nil then do string replace.") | |
77 | |
78 (defvar vip-d-char nil | |
79 "The character remenbered by the vi \"r\" command") | |
80 | |
81 (defvar vip-f-char nil | |
82 "for use by \";\" command") | |
83 | |
84 (defvar vip-F-char nil | |
85 "for use by \".\" command") | |
86 | |
87 (defvar vip-f-forward nil | |
88 "for use by \";\" command") | |
89 | |
90 (defvar vip-f-offset nil | |
91 "for use by \";\" command") | |
92 | |
93 (defconst vip-search-wrap-around t | |
94 "*if t, search wraps around") | |
95 | |
96 (defconst vip-re-search nil | |
97 "*if t, search is reg-exp search, otherwise vanilla search.") | |
98 | |
99 (defvar vip-s-string nil | |
100 "last search string") | |
101 | |
102 (defvar vip-s-forward nil | |
103 "if t, search is forward.") | |
104 | |
105 (defconst vip-case-fold-search nil | |
106 "*if t, search ignores cases.") | |
107 | |
108 (defconst vip-re-query-replace nil | |
109 "*If t then do regexp replace, if nil then do string replace.") | |
110 | |
111 (defconst vip-open-with-indent nil | |
112 "*if t, indent when open a new line.") | |
113 | |
114 (defconst vip-help-in-insert-mode nil | |
115 "*if t then C-h is bound to help-command in insert mode, if nil then it is | |
116 bound to delete-backward-char.") | |
117 | |
118 (defvar vip-quote-string "> " | |
119 "string inserted at the beginning of region") | |
120 | |
121 (defvar vip-tags-file-name "TAGS") | |
122 | |
123 (defvar vip-inhibit-startup-message nil) | |
124 | |
125 ;; basic set up | |
126 | |
127 (global-set-key "\C-z" 'vip-change-mode-to-vi) | |
128 | |
129 (defmacro vip-loop (count body) | |
130 "(COUNT BODY) Execute BODY COUNT times." | |
131 (list 'let (list (list 'count count)) | |
132 (list 'while (list '> 'count 0) | |
133 body | |
134 (list 'setq 'count (list '1- 'count))))) | |
135 | |
136 (defun vip-push-mark-silent (&optional location) | |
137 "Set mark at LOCATION (point, by default) and push old mark on mark ring. | |
138 No message." | |
139 (if (null (mark)) | |
140 nil | |
141 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) | |
142 (if (> (length mark-ring) mark-ring-max) | |
143 (progn | |
144 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
145 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))) | |
146 (set-mark (or location (point)))) | |
147 | |
148 (defun vip-goto-col (arg) | |
149 "Go to ARG's column." | |
150 (interactive "P") | |
151 (let ((val (vip-p-val arg)) | |
152 (com (vip-getcom arg))) | |
153 (save-excursion | |
154 (end-of-line) | |
155 (if (> val (1+ (current-column))) (error ""))) | |
156 (if com (move-marker vip-com-point (point))) | |
157 (beginning-of-line) | |
158 (forward-char (1- val)) | |
159 (if com (vip-execute-com 'vip-goto-col val com)))) | |
160 | |
161 (defun vip-refresh-mode-line () | |
162 "Redraw mode line." | |
163 (set-buffer-modified-p (buffer-modified-p))) | |
164 | |
165 (defun vip-copy-keymap (map) | |
166 (if (null map) (make-sparse-keymap) (copy-keymap map))) | |
167 | |
168 | |
169 ;; changing mode | |
170 | |
171 (defun vip-change-mode (new-mode) | |
172 "Change mode to NEW-MODE. NEW-MODE is either emacs-mode, vi-mode, | |
173 or insert-mode." | |
174 (or (eq new-mode vip-current-mode) | |
175 (progn | |
176 (cond ((eq new-mode 'vi-mode) | |
177 (if (eq vip-current-mode 'insert-mode) | |
178 (progn | |
179 (vip-copy-region-as-kill (point) vip-insert-point) | |
180 (vip-repeat-insert-command)) | |
181 (setq vip-emacs-local-map (current-local-map) | |
182 vip-emacs-mode-line-buffer-identification | |
183 mode-line-buffer-identification | |
184 vip-insert-local-map (vip-copy-keymap | |
185 (current-local-map)))) | |
186 (vip-change-mode-line "Vi: ") | |
187 (use-local-map vip-mode-map)) | |
188 ((eq new-mode 'insert-mode) | |
189 (move-marker vip-insert-point (point)) | |
190 (if (eq vip-current-mode 'emacs-mode) | |
191 (setq vip-emacs-local-map (current-local-map) | |
192 vip-emacs-mode-line-buffer-identification | |
193 mode-line-buffer-identification | |
194 vip-insert-local-map (vip-copy-keymap | |
195 (current-local-map))) | |
196 (setq vip-insert-local-map (vip-copy-keymap | |
197 vip-emacs-local-map))) | |
198 (vip-change-mode-line "Insert") | |
199 (use-local-map vip-insert-local-map) | |
200 (define-key vip-insert-local-map "\e" 'vip-change-mode-to-vi) | |
201 (define-key vip-insert-local-map "\C-z" 'vip-ESC) | |
202 (define-key vip-insert-local-map "\C-h" | |
203 (if vip-help-in-insert-mode 'help-command | |
204 'delete-backward-char)) | |
205 (define-key vip-insert-local-map "\C-w" | |
206 'vip-delete-backward-word)) | |
207 ((eq new-mode 'emacs-mode) | |
208 (vip-change-mode-line "Emacs:") | |
209 (use-local-map vip-emacs-local-map))) | |
210 (setq vip-current-mode new-mode) | |
211 (vip-refresh-mode-line)))) | |
212 | |
213 (defun vip-copy-region-as-kill (beg end) | |
214 "If BEG and END do not belong to the same buffer, it copies empty region." | |
215 (condition-case nil | |
216 (copy-region-as-kill beg end) | |
217 (error (copy-region-as-kill beg beg)))) | |
218 | |
219 (defun vip-change-mode-line (string) | |
220 "Assuming that the mode line format contains the string \"Emacs:\", this | |
221 function replaces the string by \"Vi: \" etc." | |
222 (setq mode-line-buffer-identification | |
223 (if (string= string "Emacs:") | |
224 vip-emacs-mode-line-buffer-identification | |
225 (list (concat string " %17b"))))) | |
226 | |
258 | 227 ;;;###autoload |
11 | 228 (defun vip-mode () |
229 "Turn on VIP emulation of VI." | |
230 (interactive) | |
231 (if (not vip-inhibit-startup-message) | |
232 (progn | |
233 (switch-to-buffer "VIP Startup Message") | |
234 (erase-buffer) | |
235 (insert | |
236 "VIP is a Vi emulation package for GNU Emacs. VIP provides most Vi commands | |
237 including Ex commands. VIP is however different from Vi in several points. | |
238 You can get more information on VIP by: | |
239 1. Typing `M-x info' and selecting menu item \"vip\". | |
240 2. Typing `C-h k' followed by a key whose description you want. | |
241 3. Printing VIP manual which can be found as GNU/man/vip.texinfo | |
242 4. Printing VIP Reference Card which can be found as GNU/etc/vipcard.tex | |
243 | |
244 This startup message appears whenever you load VIP unless you type `y' now. | |
245 Type `n' to quit this window for now.\n") | |
246 (goto-char (point-min)) | |
247 (if (y-or-n-p "Inhibit VIP startup message? ") | |
248 (progn | |
249 (save-excursion | |
250 (set-buffer | |
251 (find-file-noselect (substitute-in-file-name "~/.vip"))) | |
252 (goto-char (point-max)) | |
253 (insert "\n(setq vip-inhibit-startup-message t)\n") | |
254 (save-buffer) | |
255 (kill-buffer (current-buffer))) | |
256 (message "VIP startup message inhibited.") | |
257 (sit-for 2))) | |
258 (kill-buffer (current-buffer)) | |
259 (message "") | |
260 (setq vip-inhibit-startup-message t))) | |
261 (vip-change-mode-to-vi)) | |
262 | |
263 (defun vip-change-mode-to-vi () | |
264 "Change mode to vi mode." | |
265 (interactive) | |
266 (vip-change-mode 'vi-mode)) | |
267 | |
268 (defun vip-change-mode-to-insert () | |
269 "Change mode to insert mode." | |
270 (interactive) | |
271 (vip-change-mode 'insert-mode)) | |
272 | |
273 (defun vip-change-mode-to-emacs () | |
274 "Change mode to emacs mode." | |
275 (interactive) | |
276 (vip-change-mode 'emacs-mode)) | |
277 | |
278 | |
2319
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
1821
diff
changeset
|
279 ;; escape to emacs mode temporarily |
11 | 280 |
281 (defun vip-get-editor-command (l-map g-map &optional str) | |
282 "Read characters from keyboard until an editor command is formed, using | |
283 local keymap L-MAP and global keymap G-MAP. If the command is a | |
284 self-insert-command, the character just read is returned instead. Optional | |
285 string STR is used as initial input string." | |
286 (let (char l-bind g-bind) | |
287 (setq char | |
288 (if (or (null str) (string= str "")) | |
289 (read-char) | |
290 (string-to-char str))) | |
291 (setq last-command-char char) | |
292 (setq l-bind (vip-binding-of char l-map)) | |
293 (if (null l-bind) | |
294 ;; since local binding is empty, we concentrate on global one. | |
295 (progn | |
296 (setq g-bind (vip-binding-of char g-map)) | |
297 (if (null g-bind) | |
298 nil ;; return nil, since both bindings are void. | |
299 (if (keymapp g-bind) | |
300 (vip-get-editor-command nil g-bind (vip-string-tail str)) | |
301 (if (eq g-bind 'self-insert-command) char g-bind)))) | |
302 ;; local binding is nonvoid | |
303 (if (keymapp l-bind) | |
304 ;; since l-bind is a keymap, we consider g-bind as well. | |
305 (progn | |
306 (setq g-bind (vip-binding-of char g-map)) | |
307 (if (null g-bind) | |
308 (vip-get-editor-command l-bind nil (vip-string-tail str)) | |
309 (if (keymapp g-bind) | |
310 ;; both bindings are keymap | |
311 (vip-get-editor-command l-bind g-bind (vip-string-tail str)) | |
312 ;; l-bind is a keymap, so we neglect g-bind | |
313 (vip-get-editor-command l-bind nil (vip-string-tail str))))) | |
314 ;; l-bind is a command | |
315 (if (eq l-bind 'self-insert-command) char l-bind))))) | |
316 | |
317 (defun vip-binding-of (char map) | |
318 "Return key-binding of CHAR under keymap MAP. It is nil if the binding | |
319 is void, or a command, or a keymap" | |
320 (let ((val (if (listp map) | |
321 (cdr (assq char map)) | |
322 (aref map char)))) | |
323 (cond ((null val) nil) | |
324 ((keymapp val) | |
325 (if (symbolp val) (symbol-function val) val)) | |
326 (t | |
327 ;; otherwise, it is a function which is either a real function or | |
328 ;; a keymap fset to val. | |
329 (let ((fun (symbol-function val))) | |
330 (if (or (null fun) (keymapp fun)) fun val)))))) | |
331 | |
332 (defun vip-escape-to-emacs (arg &optional char) | |
333 "Escape to emacs mode and execute one emacs command and then return to | |
334 vi mode. ARG is used as the prefix value for the executed command. If | |
335 CHAR is given it becomes the first character of the command." | |
336 (interactive "P") | |
337 (let (com (buff (current-buffer)) (first t)) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1625
diff
changeset
|
338 (if char (setq unread-command-events (list char))) |
11 | 339 (setq prefix-arg arg) |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1625
diff
changeset
|
340 (while (or first unread-command-events) |
11 | 341 ;; this while loop is executed until unread command char will be |
342 ;; exhausted. | |
343 (setq first nil) | |
344 (setq com (vip-get-editor-command vip-emacs-local-map global-map)) | |
345 (if (numberp com) | |
346 (vip-loop (vip-p-val prefix-arg) | |
347 (insert (char-to-string com))) | |
348 (command-execute com prefix-arg))) | |
349 (setq prefix-arg nil) ;; reset prefix arg | |
350 )) | |
351 | |
352 (defun vip-message-conditions (conditions) | |
353 "Print CONDITIONS as a message." | |
354 (let ((case (car conditions)) (msg (cdr conditions))) | |
355 (if (null msg) | |
356 (message "%s" case) | |
357 (message "%s %s" case (prin1-to-string msg))) | |
358 (ding))) | |
359 | |
360 (defun vip-ESC (arg) | |
361 "Emulate ESC key in Emacs mode." | |
362 (interactive "P") | |
363 (vip-escape-to-emacs arg ?\e)) | |
364 | |
365 (defun vip-ctl-c (arg) | |
366 "Emulate C-c key in Emacs mode." | |
367 (interactive "P") | |
368 (vip-escape-to-emacs arg ?\C-c)) | |
369 | |
370 (defun vip-ctl-x (arg) | |
371 "Emulate C-x key in Emacs mode." | |
372 (interactive "P") | |
373 (vip-escape-to-emacs arg ?\C-x)) | |
374 | |
375 (defun vip-ctl-h (arg) | |
376 "Emulate C-h key in Emacs mode." | |
377 (interactive "P") | |
378 (vip-escape-to-emacs arg ?\C-h)) | |
379 | |
380 | |
381 ;; prefix argmument for vi mode | |
382 | |
383 ;; In vi mode, prefix argument is a dotted pair (NUM . COM) where NUM | |
384 ;; represents the numeric value of the prefix argument and COM represents | |
385 ;; command prefix such as "c", "d", "m" and "y". | |
386 | |
387 (defun vip-prefix-arg-value (char value com) | |
388 "Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value | |
389 obtained so far, and COM is the command part obtained so far." | |
390 (while (and (>= char ?0) (<= char ?9)) | |
391 (setq value (+ (* (if (numberp value) value 0) 10) (- char ?0))) | |
392 (setq char (read-char))) | |
393 (setq prefix-arg value) | |
394 (if com (setq prefix-arg (cons prefix-arg com))) | |
395 (while (= char ?U) | |
396 (vip-describe-arg prefix-arg) | |
397 (setq char (read-char))) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1625
diff
changeset
|
398 (setq unread-command-events (list char))) |
11 | 399 |
400 (defun vip-prefix-arg-com (char value com) | |
401 "Vi operator as prefix argument." | |
402 (let ((cont t)) | |
403 (while (and cont | |
404 (or (= char ?c) (= char ?d) (= char ?y) | |
405 (= char ?!) (= char ?<) (= char ?>) (= char ?=) | |
406 (= char ?#) (= char ?r) (= char ?R) (= char ?\"))) | |
407 (if com | |
408 ;; this means that we already have a command character, so we | |
409 ;; construct a com list and exit while. however, if char is " | |
410 ;; it is an error. | |
411 (progn | |
412 ;; new com is (CHAR . OLDCOM) | |
413 (if (or (= char ?#) (= char ?\")) (error "")) | |
414 (setq com (cons char com)) | |
415 (setq cont nil)) | |
416 ;; if com is nil we set com as char, and read more. again, if char | |
417 ;; is ", we read the name of register and store it in vip-use-register. | |
418 ;; if char is !, =, or #, a copmlete com is formed so we exit while. | |
419 (cond ((or (= char ?!) (= char ?=)) | |
420 (setq com char) | |
421 (setq char (read-char)) | |
422 (setq cont nil)) | |
423 ((= char ?#) | |
424 ;; read a char and encode it as com | |
425 (setq com (+ 128 (read-char))) | |
426 (setq char (read-char)) | |
427 (setq cont nil)) | |
428 ((or (= char ?<) (= char ?>)) | |
429 (setq com char) | |
430 (setq char (read-char)) | |
431 (if (= com char) (setq com (cons char com))) | |
432 (setq cont nil)) | |
433 ((= char ?\") | |
434 (let ((reg (read-char))) | |
435 (if (or (and (<= ?A reg) (<= reg ?z)) | |
436 (and (<= ?1 reg) (<= reg ?9))) | |
437 (setq vip-use-register reg) | |
438 (error "")) | |
439 (setq char (read-char)))) | |
440 (t | |
441 (setq com char) | |
442 (setq char (read-char))))))) | |
443 (if (atom com) | |
444 ;; com is a single char, so we construct prefix-arg | |
445 ;; and if char is ?, describe prefix arg, otherwise exit by | |
446 ;; pushing the char back | |
447 (progn | |
448 (setq prefix-arg (cons value com)) | |
449 (while (= char ?U) | |
450 (vip-describe-arg prefix-arg) | |
451 (setq char (read-char))) | |
1821
04fb1d3d6992
JimB's changes since January 18th
Jim Blandy <jimb@redhat.com>
parents:
1625
diff
changeset
|
452 (setq unread-command-events (list char))) |
11 | 453 ;; as com is non-nil, this means that we have a command to execute |
454 (if (or (= (car com) ?r) (= (car com) ?R)) | |
455 ;; execute apropriate region command. | |
456 (let ((char (car com)) (com (cdr com))) | |
457 (setq prefix-arg (cons value com)) | |
458 (if (= char ?r) (vip-region prefix-arg) | |
459 (vip-Region prefix-arg)) | |
460 ;; reset prefix-arg | |
461 (setq prefix-arg nil)) | |
462 ;; otherwise, reset prefix arg and call appropriate command | |
463 (setq value (if (null value) 1 value)) | |
464 (setq prefix-arg nil) | |
465 (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C))) | |
466 ((equal com '(?d . ?d)) (vip-line (cons value ?D))) | |
467 ((equal com '(?d . ?y)) (vip-yank-defun)) | |
468 ((equal com '(?y . ?y)) (vip-line (cons value ?Y))) | |
469 ((equal com '(?< . ?<)) (vip-line (cons value ?<))) | |
470 ((equal com '(?> . ?>)) (vip-line (cons value ?>))) | |
471 ((equal com '(?! . ?!)) (vip-line (cons value ?!))) | |
472 ((equal com '(?= . ?=)) (vip-line (cons value ?=))) | |
473 (t (error "")))))) | |
474 | |
475 (defun vip-describe-arg (arg) | |
476 (let (val com) | |
477 (setq val (vip-P-val arg) | |
478 com (vip-getcom arg)) | |
479 (if (null val) | |
480 (if (null com) | |
481 (message "Value is nil, and commmand is nil.") | |
482 (message "Value is nil, and command is %c." com)) | |
483 (if (null com) | |
484 (message "Value is %d, and command is nil." val) | |
485 (message "Value is %d, and command is %c." val com))))) | |
486 | |
487 (defun vip-digit-argument (arg) | |
488 "Begin numeric argument for the next command." | |
489 (interactive "P") | |
490 (vip-prefix-arg-value last-command-char nil | |
491 (if (consp arg) (cdr arg) nil))) | |
492 | |
493 (defun vip-command-argument (arg) | |
494 "Accept a motion command as an argument." | |
495 (interactive "P") | |
496 (condition-case conditions | |
497 (vip-prefix-arg-com | |
498 last-command-char | |
499 (cond ((null arg) nil) | |
500 ((consp arg) (car arg)) | |
501 ((numberp arg) arg) | |
502 (t (error "strange arg"))) | |
503 (cond ((null arg) nil) | |
504 ((consp arg) (cdr arg)) | |
505 ((numberp arg) nil) | |
506 (t (error "strange arg")))) | |
507 (quit | |
508 (setq vip-use-register nil) | |
509 (signal 'quit nil)))) | |
510 | |
511 (defun vip-p-val (arg) | |
512 "Get value part of prefix-argument ARG." | |
513 (cond ((null arg) 1) | |
514 ((consp arg) (if (null (car arg)) 1 (car arg))) | |
515 (t arg))) | |
516 | |
517 (defun vip-P-val (arg) | |
518 "Get value part of prefix-argument ARG." | |
519 (cond ((consp arg) (car arg)) | |
520 (t arg))) | |
521 | |
522 (defun vip-getcom (arg) | |
523 "Get com part of prefix-argument ARG." | |
524 (cond ((null arg) nil) | |
525 ((consp arg) (cdr arg)) | |
526 (t nil))) | |
527 | |
528 (defun vip-getCom (arg) | |
529 "Get com part of prefix-argument ARG and modify it." | |
530 (let ((com (vip-getcom arg))) | |
531 (cond ((equal com ?c) ?C) | |
532 ((equal com ?d) ?D) | |
533 ((equal com ?y) ?Y) | |
534 (t com)))) | |
535 | |
536 | |
537 ;; repeat last destructive command | |
538 | |
539 (defun vip-append-to-register (reg start end) | |
540 "Append region to text in register REG. | |
541 START and END are buffer positions indicating what to append." | |
542 (set-register reg (concat (or (get-register reg) "") | |
543 (buffer-substring start end)))) | |
544 | |
545 (defun vip-execute-com (m-com val com) | |
546 "(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set | |
547 to vip-d-com for later use by vip-repeat" | |
548 (let ((reg vip-use-register)) | |
549 (if com | |
550 (cond ((= com ?c) (vip-change vip-com-point (point))) | |
551 ((= com (- ?c)) (vip-change-subr vip-com-point (point))) | |
552 ((or (= com ?C) (= com (- ?C))) | |
553 (save-excursion | |
554 (set-mark vip-com-point) | |
555 (vip-enlarge-region (mark) (point)) | |
556 (if vip-use-register | |
557 (progn | |
558 (cond ((and (<= ?a vip-use-register) | |
559 (<= vip-use-register ?z)) | |
560 (copy-to-register | |
561 vip-use-register (mark) (point) nil)) | |
562 ((and (<= ?A vip-use-register) | |
563 (<= vip-use-register ?Z)) | |
564 (vip-append-to-register | |
565 (+ vip-use-register 32) (mark) (point))) | |
566 (t (setq vip-use-register nil) | |
567 (error ""))) | |
568 (setq vip-use-register nil))) | |
569 (delete-region (mark) (point))) | |
570 (open-line 1) | |
571 (if (= com ?C) (vip-change-mode-to-insert) (yank))) | |
572 ((= com ?d) | |
573 (if vip-use-register | |
574 (progn | |
575 (cond ((and (<= ?a vip-use-register) | |
576 (<= vip-use-register ?z)) | |
577 (copy-to-register | |
578 vip-use-register vip-com-point (point) nil)) | |
579 ((and (<= ?A vip-use-register) | |
580 (<= vip-use-register ?Z)) | |
581 (vip-append-to-register | |
582 (+ vip-use-register 32) vip-com-point (point))) | |
583 (t (setq vip-use-register nil) | |
584 (error ""))) | |
585 (setq vip-use-register nil))) | |
586 (setq last-command | |
587 (if (eq last-command 'd-command) 'kill-region nil)) | |
588 (kill-region vip-com-point (point)) | |
589 (setq this-command 'd-command)) | |
590 ((= com ?D) | |
591 (save-excursion | |
592 (set-mark vip-com-point) | |
593 (vip-enlarge-region (mark) (point)) | |
594 (if vip-use-register | |
595 (progn | |
596 (cond ((and (<= ?a vip-use-register) | |
597 (<= vip-use-register ?z)) | |
598 (copy-to-register | |
599 vip-use-register (mark) (point) nil)) | |
600 ((and (<= ?A vip-use-register) | |
601 (<= vip-use-register ?Z)) | |
602 (vip-append-to-register | |
603 (+ vip-use-register 32) (mark) (point))) | |
604 (t (setq vip-use-register nil) | |
605 (error ""))) | |
606 (setq vip-use-register nil))) | |
607 (setq last-command | |
608 (if (eq last-command 'D-command) 'kill-region nil)) | |
609 (kill-region (mark) (point)) | |
610 (if (eq m-com 'vip-line) (setq this-command 'D-command))) | |
611 (back-to-indentation)) | |
612 ((= com ?y) | |
613 (if vip-use-register | |
614 (progn | |
615 (cond ((and (<= ?a vip-use-register) | |
616 (<= vip-use-register ?z)) | |
617 (copy-to-register | |
618 vip-use-register vip-com-point (point) nil)) | |
619 ((and (<= ?A vip-use-register) | |
620 (<= vip-use-register ?Z)) | |
621 (vip-append-to-register | |
622 (+ vip-use-register 32) vip-com-point (point))) | |
623 (t (setq vip-use-register nil) | |
624 (error ""))) | |
625 (setq vip-use-register nil))) | |
626 (setq last-command nil) | |
627 (copy-region-as-kill vip-com-point (point)) | |
628 (goto-char vip-com-point)) | |
629 ((= com ?Y) | |
630 (save-excursion | |
631 (set-mark vip-com-point) | |
632 (vip-enlarge-region (mark) (point)) | |
633 (if vip-use-register | |
634 (progn | |
635 (cond ((and (<= ?a vip-use-register) | |
636 (<= vip-use-register ?z)) | |
637 (copy-to-register | |
638 vip-use-register (mark) (point) nil)) | |
639 ((and (<= ?A vip-use-register) | |
640 (<= vip-use-register ?Z)) | |
641 (vip-append-to-register | |
642 (+ vip-use-register 32) (mark) (point))) | |
643 (t (setq vip-use-register nil) | |
644 (error ""))) | |
645 (setq vip-use-register nil))) | |
646 (setq last-command nil) | |
647 (copy-region-as-kill (mark) (point))) | |
648 (goto-char vip-com-point)) | |
649 ((or (= com ?!) (= com (- ?!))) | |
650 (save-excursion | |
651 (set-mark vip-com-point) | |
652 (vip-enlarge-region (mark) (point)) | |
653 (shell-command-on-region | |
654 (mark) (point) | |
655 (if (= com ?!) | |
656 (setq vip-last-shell-com (vip-read-string "!")) | |
657 vip-last-shell-com) | |
658 t))) | |
659 ((= com ?=) | |
660 (save-excursion | |
661 (set-mark vip-com-point) | |
662 (vip-enlarge-region (mark) (point)) | |
663 (if (> (mark) (point)) (exchange-point-and-mark)) | |
664 (indent-region (mark) (point) nil))) | |
665 ((= com ?<) | |
666 (save-excursion | |
667 (set-mark vip-com-point) | |
668 (vip-enlarge-region (mark) (point)) | |
669 (indent-rigidly (mark) (point) (- vip-shift-width))) | |
670 (goto-char vip-com-point)) | |
671 ((= com ?>) | |
672 (save-excursion | |
673 (set-mark vip-com-point) | |
674 (vip-enlarge-region (mark) (point)) | |
675 (indent-rigidly (mark) (point) vip-shift-width)) | |
676 (goto-char vip-com-point)) | |
677 ((>= com 128) | |
678 ;; this is special command # | |
679 (vip-special-prefix-com (- com 128))))) | |
680 (setq vip-d-com (list m-com val (if (or (= com ?c) (= com ?C) (= com ?!)) | |
681 (- com) com) | |
682 reg)))) | |
683 | |
684 (defun vip-repeat (arg) | |
685 "(ARG) Re-excute last destructive command. vip-d-com has the form | |
686 (COM ARG CH REG), where COM is the command to be re-executed, ARG is the | |
687 argument for COM, CH is a flag for repeat, and REG is optional and if exists | |
688 is the name of the register for COM." | |
689 (interactive "P") | |
690 (if (eq last-command 'vip-undo) | |
691 ;; if the last command was vip-undo, then undo-more | |
692 (vip-undo-more) | |
693 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil | |
694 ;; its prefix value is used as new prefix value for the command. | |
695 (let ((m-com (car vip-d-com)) | |
696 (val (vip-P-val arg)) | |
697 (com (car (cdr (cdr vip-d-com)))) | |
698 (reg (nth 3 vip-d-com))) | |
699 (if (null val) (setq val (car (cdr vip-d-com)))) | |
700 (if (null m-com) (error "No previous command to repeat.")) | |
701 (setq vip-use-register reg) | |
702 (funcall m-com (cons val com))))) | |
703 | |
704 (defun vip-special-prefix-com (char) | |
705 "This command is invoked interactively by the key sequence #<char>" | |
706 (cond ((= char ?c) | |
707 (downcase-region (min vip-com-point (point)) | |
708 (max vip-com-point (point)))) | |
709 ((= char ?C) | |
710 (upcase-region (min vip-com-point (point)) | |
711 (max vip-com-point (point)))) | |
712 ((= char ?g) | |
713 (set-mark vip-com-point) | |
714 (vip-global-execute)) | |
715 ((= char ?q) | |
716 (set-mark vip-com-point) | |
717 (vip-quote-region)) | |
718 ((= char ?s) (spell-region vip-com-point (point))))) | |
719 | |
720 | |
721 ;; undoing | |
722 | |
723 (defun vip-undo () | |
724 "Undo previous change." | |
725 (interactive) | |
726 (message "undo!") | |
727 (undo-start) | |
728 (undo-more 2) | |
729 (setq this-command 'vip-undo)) | |
730 | |
731 (defun vip-undo-more () | |
732 "Continue undoing previous changes." | |
733 (message "undo more!") | |
734 (undo-more 1) | |
735 (setq this-command 'vip-undo)) | |
736 | |
737 | |
738 ;; utilities | |
739 | |
740 (defun vip-string-tail (str) | |
741 (if (or (null str) (string= str "")) nil | |
742 (substring str 1))) | |
743 | |
744 (defun vip-yank-defun () | |
745 (mark-defun) | |
746 (copy-region-as-kill (point) (mark))) | |
747 | |
748 (defun vip-enlarge-region (beg end) | |
749 "Enlarge region between BEG and END." | |
750 (if (< beg end) | |
751 (progn (goto-char beg) (set-mark end)) | |
752 (goto-char end) | |
753 (set-mark beg)) | |
754 (beginning-of-line) | |
755 (exchange-point-and-mark) | |
756 (if (or (not (eobp)) (not (bolp))) (next-line 1)) | |
757 (beginning-of-line) | |
758 (if (> beg end) (exchange-point-and-mark))) | |
759 | |
760 (defun vip-global-execute () | |
761 "Call last keyboad macro for each line in the region." | |
762 (if (> (point) (mark)) (exchange-point-and-mark)) | |
763 (beginning-of-line) | |
764 (call-last-kbd-macro) | |
765 (while (< (point) (mark)) | |
766 (forward-line 1) | |
767 (beginning-of-line) | |
768 (call-last-kbd-macro))) | |
769 | |
770 (defun vip-quote-region () | |
771 "Quote region by inserting the user supplied string at the beginning of | |
772 each line in the region." | |
773 (setq vip-quote-string | |
774 (let ((str | |
775 (vip-read-string (format "quote string \(default \"%s\"\): " | |
776 vip-quote-string)))) | |
777 (if (string= str "") vip-quote-string str))) | |
778 (vip-enlarge-region (point) (mark)) | |
779 (if (> (point) (mark)) (exchange-point-and-mark)) | |
780 (insert vip-quote-string) | |
781 (beginning-of-line) | |
782 (forward-line 1) | |
783 (while (and (< (point) (mark)) (bolp)) | |
784 (insert vip-quote-string) | |
785 (beginning-of-line) | |
786 (forward-line 1))) | |
787 | |
788 (defun vip-end-with-a-newline-p (string) | |
789 "Check if the string ends with a newline." | |
790 (or (string= text "") | |
791 (= (aref string (1- (length string))) ?\n))) | |
792 | |
793 (defun vip-read-string (prompt &optional init) | |
794 (setq save-minibuffer-local-map (copy-keymap minibuffer-local-map)) | |
795 (define-key minibuffer-local-map "\C-h" 'backward-char) | |
796 (define-key minibuffer-local-map "\C-w" 'backward-word) | |
797 (define-key minibuffer-local-map "\e" 'exit-minibuffer) | |
798 (let (str) | |
799 (condition-case conditions | |
800 (setq str (read-string prompt init)) | |
801 (quit | |
802 (setq minibuffer-local-map save-minibuffer-local-map) | |
803 (signal 'quit nil))) | |
804 (setq minibuffer-local-map save-minibuffer-local-map) | |
805 str)) | |
806 | |
807 | |
808 ;; insertion commands | |
809 | |
810 (defun vip-repeat-insert-command () | |
811 "This function is called when mode changes from insertion mode to | |
812 vi command mode. It will repeat the insertion command if original insertion | |
813 command was invoked with argument > 1." | |
814 (let ((i-com (car vip-d-com)) (val (car (cdr vip-d-com)))) | |
815 (if (and val (> val 1)) ;; first check that val is non-nil | |
816 (progn | |
817 (setq vip-d-com (list i-com (1- val) ?r)) | |
818 (vip-repeat nil) | |
819 (setq vip-d-com (list i-com val ?r)))))) | |
820 | |
821 (defun vip-insert (arg) "" | |
822 (interactive "P") | |
823 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
824 (setq vip-d-com (list 'vip-insert val ?r)) | |
825 (if com (vip-loop val (yank)) | |
826 (vip-change-mode-to-insert)))) | |
827 | |
828 (defun vip-append (arg) | |
829 "Append after point." | |
830 (interactive "P") | |
831 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
832 (setq vip-d-com (list 'vip-append val ?r)) | |
833 (if (not (eolp)) (forward-char)) | |
834 (if (equal com ?r) | |
835 (vip-loop val (yank)) | |
836 (vip-change-mode-to-insert)))) | |
837 | |
838 (defun vip-Append (arg) | |
839 "Append at end of line." | |
840 (interactive "P") | |
841 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
842 (setq vip-d-com (list 'vip-Append val ?r)) | |
843 (end-of-line) | |
844 (if (equal com ?r) | |
845 (vip-loop val (yank)) | |
846 (vip-change-mode-to-insert)))) | |
847 | |
848 (defun vip-Insert (arg) | |
849 "Insert before first non-white." | |
850 (interactive "P") | |
851 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
852 (setq vip-d-com (list 'vip-Insert val ?r)) | |
853 (back-to-indentation) | |
854 (if (equal com ?r) | |
855 (vip-loop val (yank)) | |
856 (vip-change-mode-to-insert)))) | |
857 | |
858 (defun vip-open-line (arg) | |
859 "Open line below." | |
860 (interactive "P") | |
861 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
862 (setq vip-d-com (list 'vip-open-line val ?r)) | |
863 (let ((col (current-indentation))) | |
864 (if (equal com ?r) | |
865 (vip-loop val | |
866 (progn | |
867 (end-of-line) | |
868 (newline 1) | |
869 (if vip-open-with-indent (indent-to col)) | |
870 (yank))) | |
871 (end-of-line) | |
872 (newline 1) | |
873 (if vip-open-with-indent (indent-to col)) | |
874 (vip-change-mode-to-insert))))) | |
875 | |
876 (defun vip-Open-line (arg) | |
877 "Open line above." | |
878 (interactive "P") | |
879 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
880 (setq vip-d-com (list 'vip-Open-line val ?r)) | |
881 (let ((col (current-indentation))) | |
882 (if (equal com ?r) | |
883 (vip-loop val | |
884 (progn | |
885 (beginning-of-line) | |
886 (open-line 1) | |
887 (if vip-open-with-indent (indent-to col)) | |
888 (yank))) | |
889 (beginning-of-line) | |
890 (open-line 1) | |
891 (if vip-open-with-indent (indent-to col)) | |
892 (vip-change-mode-to-insert))))) | |
893 | |
894 (defun vip-open-line-at-point (arg) | |
895 "Open line at point." | |
896 (interactive "P") | |
897 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
898 (setq vip-d-com (list 'vip-open-line-at-point val ?r)) | |
899 (if (equal com ?r) | |
900 (vip-loop val | |
901 (progn | |
902 (open-line 1) | |
903 (yank))) | |
904 (open-line 1) | |
905 (vip-change-mode-to-insert)))) | |
906 | |
907 (defun vip-substitute (arg) | |
908 "Substitute characters." | |
909 (interactive "P") | |
910 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
911 (save-excursion | |
912 (set-mark (point)) | |
913 (forward-char val) | |
914 (if (equal com ?r) | |
915 (vip-change-subr (mark) (point)) | |
916 (vip-change (mark) (point)))) | |
917 (setq vip-d-com (list 'vip-substitute val ?r)))) | |
918 | |
919 (defun vip-substitute-line (arg) | |
920 "Substitute lines." | |
921 (interactive "p") | |
922 (vip-line (cons arg ?C))) | |
923 | |
924 | |
925 ;; line command | |
926 | |
927 (defun vip-line (arg) | |
928 (let ((val (car arg)) (com (cdr arg))) | |
929 (move-marker vip-com-point (point)) | |
930 (next-line (1- val)) | |
931 (vip-execute-com 'vip-line val com))) | |
932 | |
933 (defun vip-yank-line (arg) | |
934 "Yank ARG lines (in vi's sense)" | |
935 (interactive "P") | |
936 (let ((val (vip-p-val arg))) | |
937 (vip-line (cons val ?Y)))) | |
938 | |
939 | |
940 ;; region command | |
941 | |
942 (defun vip-region (arg) | |
943 (interactive "P") | |
944 (let ((val (vip-P-val arg)) | |
945 (com (vip-getcom arg))) | |
946 (move-marker vip-com-point (point)) | |
947 (exchange-point-and-mark) | |
948 (vip-execute-com 'vip-region val com))) | |
949 | |
950 (defun vip-Region (arg) | |
951 (interactive "P") | |
952 (let ((val (vip-P-val arg)) | |
953 (com (vip-getCom arg))) | |
954 (move-marker vip-com-point (point)) | |
955 (exchange-point-and-mark) | |
956 (vip-execute-com 'vip-Region val com))) | |
957 | |
958 (defun vip-replace-char (arg) | |
959 "Replace the following ARG chars by the character read." | |
960 (interactive "P") | |
961 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
962 (setq vip-d-com (list 'vip-replace-char val ?r)) | |
963 (vip-replace-char-subr (if (equal com ?r) vip-d-char (read-char)) val))) | |
964 | |
965 (defun vip-replace-char-subr (char arg) | |
966 (delete-char arg t) | |
967 (setq vip-d-char char) | |
968 (vip-loop (if (> arg 0) arg (- arg)) (insert char)) | |
969 (backward-char arg)) | |
970 | |
971 (defun vip-replace-string () | |
972 "Replace string. If you supply null string as the string to be replaced, | |
973 the query replace mode will toggle between string replace and regexp replace." | |
974 (interactive) | |
975 (let (str) | |
976 (setq str (vip-read-string | |
977 (if vip-re-replace "Replace regexp: " "Replace string: "))) | |
978 (if (string= str "") | |
979 (progn | |
980 (setq vip-re-replace (not vip-re-replace)) | |
981 (message (format "Replace mode changed to %s." | |
982 (if vip-re-replace "regexp replace" | |
983 "string replace")))) | |
984 (if vip-re-replace | |
985 (replace-regexp | |
986 str | |
987 (vip-read-string (format "Replace regexp \"%s\" with: " str))) | |
988 (replace-string | |
989 str | |
990 (vip-read-string (format "Replace \"%s\" with: " str))))))) | |
991 | |
992 | |
993 ;; basic cursor movement. j, k, l, m commands. | |
994 | |
995 (defun vip-forward-char (arg) | |
996 "Move point right ARG characters (left if ARG negative).On reaching end | |
997 of buffer, stop and signal error." | |
998 (interactive "P") | |
999 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1000 (if com (move-marker vip-com-point (point))) | |
1001 (forward-char val) | |
1002 (if com (vip-execute-com 'vip-forward-char val com)))) | |
1003 | |
1004 (defun vip-backward-char (arg) | |
1005 "Move point left ARG characters (right if ARG negative). On reaching | |
1006 beginning of buffer, stop and signal error." | |
1007 (interactive "P") | |
1008 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1009 (if com (move-marker vip-com-point (point))) | |
1010 (backward-char val) | |
1011 (if com (vip-execute-com 'vip-backward-char val com)))) | |
1012 | |
1013 | |
1014 ;; word command | |
1015 | |
1016 (defun vip-forward-word (arg) | |
1017 "Forward word." | |
1018 (interactive "P") | |
1019 (let ((val (vip-p-val arg)) | |
1020 (com (vip-getcom arg))) | |
1021 (if com (move-marker vip-com-point (point))) | |
1022 (forward-word val) | |
1023 (skip-chars-forward " \t\n") | |
1024 (if com | |
1025 (progn | |
1026 (if (or (= com ?c) (= com (- ?c))) | |
1027 (progn (backward-word 1) (forward-word 1))) | |
1028 (if (or (= com ?d) (= com ?y)) | |
1029 (progn | |
1030 (backward-word 1) | |
1031 (forward-word 1) | |
1032 (skip-chars-forward " \t"))) | |
1033 (vip-execute-com 'vip-forward-word val com))))) | |
1034 | |
1035 (defun vip-end-of-word (arg) | |
1036 "Move point to end of current word." | |
1037 (interactive "P") | |
1038 (let ((val (vip-p-val arg)) | |
1039 (com (vip-getcom arg))) | |
1040 (if com (move-marker vip-com-point (point))) | |
1041 (forward-char) | |
1042 (forward-word val) | |
1043 (backward-char) | |
1044 (if com | |
1045 (progn | |
1046 (forward-char) | |
1047 (vip-execute-com 'vip-end-of-word val com))))) | |
1048 | |
1049 (defun vip-backward-word (arg) | |
1050 "Backward word." | |
1051 (interactive "P") | |
1052 (let ((val (vip-p-val arg)) | |
1053 (com (vip-getcom arg))) | |
1054 (if com (move-marker vip-com-point (point))) | |
1055 (backward-word val) | |
1056 (if com (vip-execute-com 'vip-backward-word val com)))) | |
1057 | |
1058 (defun vip-forward-Word (arg) | |
1059 "Forward word delimited by white character." | |
1060 (interactive "P") | |
1061 (let ((val (vip-p-val arg)) | |
1062 (com (vip-getcom arg))) | |
1063 (if com (move-marker vip-com-point (point))) | |
1064 (re-search-forward "[^ \t\n]*[ \t\n]+" nil t val) | |
1065 (if com | |
1066 (progn | |
1067 (if (or (= com ?c) (= com (- ?c))) | |
1068 (progn (backward-word 1) (forward-word 1))) | |
1069 (if (or (= com ?d) (= com ?y)) | |
1070 (progn | |
1071 (backward-word 1) | |
1072 (forward-word 1) | |
1073 (skip-chars-forward " \t"))) | |
1074 (vip-execute-com 'vip-forward-Word val com))))) | |
1075 | |
1076 (defun vip-end-of-Word (arg) | |
1077 "Move forward to end of word delimited by white character." | |
1078 (interactive "P") | |
1079 (let ((val (vip-p-val arg)) | |
1080 (com (vip-getcom arg))) | |
1081 (if com (move-marker vip-com-point (point))) | |
1082 (forward-char) | |
1083 (if (re-search-forward "[^ \t\n]+" nil t val) (backward-char)) | |
1084 (if com | |
1085 (progn | |
1086 (forward-char) | |
1087 (vip-execute-com 'vip-end-of-Word val com))))) | |
1088 | |
1089 (defun vip-backward-Word (arg) | |
1090 "Backward word delimited by white character." | |
1091 (interactive "P") | |
1092 (let ((val (vip-p-val arg)) | |
1093 (com (vip-getcom arg))) | |
1094 (if com (move-marker vip-com-point (point))) | |
1095 (if (re-search-backward "[ \t\n]+[^ \t\n]+" nil t val) | |
1096 (forward-char) | |
1097 (goto-char (point-min))) | |
1098 (if com (vip-execute-com 'vip-backward-Word val com)))) | |
1099 | |
1100 (defun vip-beginning-of-line (arg) | |
1101 "Go to beginning of line." | |
1102 (interactive "P") | |
1103 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1104 (if com (move-marker vip-com-point (point))) | |
1105 (beginning-of-line val) | |
1106 (if com (vip-execute-com 'vip-beginning-of-line val com)))) | |
1107 | |
1108 (defun vip-bol-and-skip-white (arg) | |
1109 "Beginning of line at first non-white character." | |
1110 (interactive "P") | |
1111 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1112 (if com (move-marker vip-com-point (point))) | |
1113 (back-to-indentation) | |
1114 (if com (vip-execute-com 'vip-bol-and-skip-white val com)))) | |
1115 | |
1116 (defun vip-goto-eol (arg) | |
1117 "Go to end of line." | |
1118 (interactive "P") | |
1119 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1120 (if com (move-marker vip-com-point (point))) | |
1121 (end-of-line val) | |
1122 (if com (vip-execute-com 'vip-goto-eol val com)))) | |
1123 | |
1124 (defun vip-next-line (arg) | |
1125 "Go to next line." | |
1126 (interactive "P") | |
1127 (let ((val (vip-p-val arg)) (com (vip-getCom arg))) | |
1128 (if com (move-marker vip-com-point (point))) | |
1129 (line-move val) | |
1130 (setq this-command 'next-line) | |
1131 (if com (vip-execute-com 'vip-next-line val com)))) | |
1132 | |
1133 (defun vip-next-line-at-bol (arg) | |
1134 "Next line at beginning of line." | |
1135 (interactive "P") | |
1136 (let ((val (vip-p-val arg)) (com (vip-getCom arg))) | |
1137 (if com (move-marker vip-com-point (point))) | |
1138 (next-line val) | |
1139 (back-to-indentation) | |
1140 (if com (vip-execute-com 'vip-next-line-at-bol val com)))) | |
1141 | |
1142 (defun vip-previous-line (arg) | |
1143 "Go to previous line." | |
1144 (interactive "P") | |
1145 (let ((val (vip-p-val arg)) (com (vip-getCom arg))) | |
1146 (if com (move-marker vip-com-point (point))) | |
1147 (next-line (- val)) | |
1148 (setq this-command 'previous-line) | |
1149 (if com (vip-execute-com 'vip-previous-line val com)))) | |
1150 | |
1151 (defun vip-previous-line-at-bol (arg) | |
1152 "Previous line at beginning of line." | |
1153 (interactive "P") | |
1154 (let ((val (vip-p-val arg)) (com (vip-getCom arg))) | |
1155 (if com (move-marker vip-com-point (point))) | |
1156 (next-line (- val)) | |
1157 (back-to-indentation) | |
1158 (if com (vip-execute-com 'vip-previous-line val com)))) | |
1159 | |
1160 (defun vip-change-to-eol (arg) | |
1161 "Change to end of line." | |
1162 (interactive "P") | |
1163 (vip-goto-eol (cons arg ?c))) | |
1164 | |
1165 (defun vip-kill-line (arg) | |
1166 "Delete line." | |
1167 (interactive "P") | |
1168 (vip-goto-eol (cons arg ?d))) | |
1169 | |
1170 | |
1171 ;; moving around | |
1172 | |
1173 (defun vip-goto-line (arg) | |
1174 "Go to ARG's line. Without ARG go to end of buffer." | |
1175 (interactive "P") | |
1176 (let ((val (vip-P-val arg)) (com (vip-getCom arg))) | |
1177 (move-marker vip-com-point (point)) | |
1178 (set-mark (point)) | |
1179 (if (null val) | |
1180 (goto-char (point-max)) | |
1181 (goto-char (point-min)) | |
1182 (forward-line (1- val))) | |
1183 (back-to-indentation) | |
1184 (if com (vip-execute-com 'vip-goto-line val com)))) | |
1185 | |
1186 (defun vip-find-char (arg char forward offset) | |
1187 "Find ARG's occurence of CHAR on the current line. If FORWARD then | |
1188 search is forward, otherwise backward. OFFSET is used to adjust point | |
1189 after search." | |
1190 (let ((arg (if forward arg (- arg))) point) | |
1191 (save-excursion | |
1192 (save-restriction | |
1193 (if (> arg 0) | |
1194 (narrow-to-region | |
1195 ;; forward search begins here | |
1196 (if (eolp) (error "") (point)) | |
1197 ;; forward search ends here | |
1198 (progn (next-line 1) (beginning-of-line) (point))) | |
1199 (narrow-to-region | |
1200 ;; backward search begins from here | |
1201 (if (bolp) (error "") (point)) | |
1202 ;; backward search ends here | |
1203 (progn (beginning-of-line) (point)))) | |
1204 ;; if arg > 0, point is forwarded before search. | |
1205 (if (> arg 0) (goto-char (1+ (point-min))) | |
1206 (goto-char (point-max))) | |
1207 (let ((case-fold-search nil)) | |
1208 (search-forward (char-to-string char) nil 0 arg)) | |
1209 (setq point (point)) | |
1210 (if (or (and (> arg 0) (= point (point-max))) | |
1211 (and (< arg 0) (= point (point-min)))) | |
1212 (error "")))) | |
1213 (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0)))))) | |
1214 | |
1215 (defun vip-find-char-forward (arg) | |
1216 "Find char on the line. If called interactively read the char to find | |
1217 from the terminal, and if called from vip-repeat, the char last used is | |
1218 used. This behaviour is controlled by the sign of prefix numeric value." | |
1219 (interactive "P") | |
1220 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1221 (if (> val 0) | |
1222 ;; this means that the function was called interactively | |
1223 (setq vip-f-char (read-char) | |
1224 vip-f-forward t | |
1225 vip-f-offset nil) | |
1226 (setq val (- val))) | |
1227 (if com (move-marker vip-com-point (point))) | |
1228 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil) | |
1229 (setq val (- val)) | |
1230 (if com | |
1231 (progn | |
1232 (setq vip-F-char vip-f-char);; set new vip-F-char | |
1233 (forward-char) | |
1234 (vip-execute-com 'vip-find-char-forward val com))))) | |
1235 | |
1236 (defun vip-goto-char-forward (arg) | |
1237 "Go up to char ARG forward on line." | |
1238 (interactive "P") | |
1239 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1240 (if (> val 0) | |
1241 ;; this means that the function was called interactively | |
1242 (setq vip-f-char (read-char) | |
1243 vip-f-forward t | |
1244 vip-f-offset t) | |
1245 (setq val (- val))) | |
1246 (if com (move-marker vip-com-point (point))) | |
1247 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t) | |
1248 (setq val (- val)) | |
1249 (if com | |
1250 (progn | |
1251 (setq vip-F-char vip-f-char);; set new vip-F-char | |
1252 (forward-char) | |
1253 (vip-execute-com 'vip-goto-char-forward val com))))) | |
1254 | |
1255 (defun vip-find-char-backward (arg) | |
1256 "Find char ARG on line backward." | |
1257 (interactive "P") | |
1258 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1259 (if (> val 0) | |
1260 ;; this means that the function was called interactively | |
1261 (setq vip-f-char (read-char) | |
1262 vip-f-forward nil | |
1263 vip-f-offset nil) | |
1264 (setq val (- val))) | |
1265 (if com (move-marker vip-com-point (point))) | |
1266 (vip-find-char | |
1267 val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil) | |
1268 (setq val (- val)) | |
1269 (if com | |
1270 (progn | |
1271 (setq vip-F-char vip-f-char);; set new vip-F-char | |
1272 (vip-execute-com 'vip-find-char-backward val com))))) | |
1273 | |
1274 (defun vip-goto-char-backward (arg) | |
1275 "Go up to char ARG backward on line." | |
1276 (interactive "P") | |
1277 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1278 (if (> val 0) | |
1279 ;; this means that the function was called interactively | |
1280 (setq vip-f-char (read-char) | |
1281 vip-f-forward nil | |
1282 vip-f-offset t) | |
1283 (setq val (- val))) | |
1284 (if com (move-marker vip-com-point (point))) | |
1285 (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t) | |
1286 (setq val (- val)) | |
1287 (if com | |
1288 (progn | |
1289 (setq vip-F-char vip-f-char);; set new vip-F-char | |
1290 (vip-execute-com 'vip-goto-char-backward val com))))) | |
1291 | |
1292 (defun vip-repeat-find (arg) | |
1293 "Repeat previous find command." | |
1294 (interactive "P") | |
1295 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1296 (if com (move-marker vip-com-point (point))) | |
1297 (vip-find-char val vip-f-char vip-f-forward vip-f-offset) | |
1298 (if com | |
1299 (progn | |
1300 (if vip-f-forward (forward-char)) | |
1301 (vip-execute-com 'vip-repeat-find val com))))) | |
1302 | |
1303 (defun vip-repeat-find-opposite (arg) | |
1304 "Repeat previous find command in the opposite direction." | |
1305 (interactive "P") | |
1306 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1307 (if com (move-marker vip-com-point (point))) | |
1308 (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset) | |
1309 (if com | |
1310 (progn | |
1311 (if vip-f-forward (forward-char)) | |
1312 (vip-execute-com 'vip-repeat-find-opposite val com))))) | |
1313 | |
1314 | |
1315 ;; window scrolling etc. | |
1316 | |
1317 (defun vip-other-window (arg) | |
1318 "Switch to other window." | |
1319 (interactive "p") | |
1320 (other-window arg) | |
1321 (or (not (eq vip-current-mode 'emacs-mode)) | |
1322 (string= (buffer-name (current-buffer)) " *Minibuf-1*") | |
1323 (vip-change-mode-to-vi))) | |
1324 | |
1325 (defun vip-window-top (arg) | |
1326 "Go to home window line." | |
1327 (interactive "P") | |
1328 (let ((val (vip-p-val arg)) | |
1329 (com (vip-getCom arg))) | |
1330 (if com (move-marker vip-com-point (point))) | |
1331 (move-to-window-line (1- val)) | |
1332 (if com (vip-execute-com 'vip-window-top val com)))) | |
1333 | |
1334 (defun vip-window-middle (arg) | |
1335 "Go to middle window line." | |
1336 (interactive "P") | |
1337 (let ((val (vip-p-val arg)) | |
1338 (com (vip-getCom arg))) | |
1339 (if com (move-marker vip-com-point (point))) | |
1340 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val))) | |
1341 (if com (vip-execute-com 'vip-window-middle val com)))) | |
1342 | |
1343 (defun vip-window-bottom (arg) | |
1344 "Go to last window line." | |
1345 (interactive "P") | |
1346 (let ((val (vip-p-val arg)) | |
1347 (com (vip-getCom arg))) | |
1348 (if com (move-marker vip-com-point (point))) | |
1349 (move-to-window-line (- val)) | |
1350 (if com (vip-execute-com 'vip-window-bottom val com)))) | |
1351 | |
1352 (defun vip-line-to-top (arg) | |
1353 "Put current line on the home line." | |
1354 (interactive "p") | |
1355 (recenter (1- arg))) | |
1356 | |
1357 (defun vip-line-to-middle (arg) | |
1358 "Put current line on the middle line." | |
1359 (interactive "p") | |
1360 (recenter (+ (1- arg) (/ (1- (window-height)) 2)))) | |
1361 | |
1362 (defun vip-line-to-bottom (arg) | |
1363 "Put current line on the last line." | |
1364 (interactive "p") | |
1365 (recenter (- (window-height) (1+ arg)))) | |
1366 | |
1367 | |
1368 ;; paren match | |
1369 | |
1370 (defun vip-paren-match (arg) | |
1371 "Go to the matching parenthesis." | |
1372 (interactive "P") | |
1373 (let ((com (vip-getcom arg))) | |
1374 (if (numberp arg) | |
1375 (if (or (> arg 99) (< arg 1)) | |
1376 (error "Prefix must be between 1 and 99.") | |
1377 (goto-char | |
1378 (if (> (point-max) 80000) | |
1379 (* (/ (point-max) 100) arg) | |
1380 (/ (* (point-max) arg) 100))) | |
1381 (back-to-indentation)) | |
1382 (cond ((looking-at "[\(\[{]") | |
1383 (if com (move-marker vip-com-point (point))) | |
1384 (forward-sexp 1) | |
1385 (if com | |
1386 (vip-execute-com 'vip-paren-match nil com) | |
1387 (backward-char))) | |
1388 ((looking-at "[])}]") | |
1389 (forward-char) | |
1390 (if com (move-marker vip-com-point (point))) | |
1391 (backward-sexp 1) | |
1392 (if com (vip-execute-com 'vip-paren-match nil com))) | |
1393 (t (error "")))))) | |
1394 | |
1395 | |
1396 ;; sentence and paragraph | |
1397 | |
1398 (defun vip-forward-sentence (arg) | |
1399 "Forward sentence." | |
1400 (interactive "P") | |
1401 (let ((val (vip-p-val arg)) | |
1402 (com (vip-getcom arg))) | |
1403 (if com (move-marker vip-com-point (point))) | |
1404 (forward-sentence val) | |
1405 (if com (vip-execute-com 'vip-forward-sentence nil com)))) | |
1406 | |
1407 (defun vip-backward-sentence (arg) | |
1408 "Backward sentence." | |
1409 (interactive "P") | |
1410 (let ((val (vip-p-val arg)) | |
1411 (com (vip-getcom arg))) | |
1412 (if com (move-marker vip-com-point (point))) | |
1413 (backward-sentence val) | |
1414 (if com (vip-execute-com 'vip-backward-sentence nil com)))) | |
1415 | |
1416 (defun vip-forward-paragraph (arg) | |
1417 "Forward paragraph." | |
1418 (interactive "P") | |
1419 (let ((val (vip-p-val arg)) | |
1420 (com (vip-getCom arg))) | |
1421 (if com (move-marker vip-com-point (point))) | |
1422 (forward-paragraph val) | |
1423 (if com (vip-execute-com 'vip-forward-paragraph nil com)))) | |
1424 | |
1425 (defun vip-backward-paragraph (arg) | |
1426 "Backward paragraph." | |
1427 (interactive "P") | |
1428 (let ((val (vip-p-val arg)) | |
1429 (com (vip-getCom arg))) | |
1430 (if com (move-marker vip-com-point (point))) | |
1431 (backward-paragraph val) | |
1432 (if com (vip-execute-com 'vip-backward-paragraph nil com)))) | |
1433 | |
1434 | |
1435 ;; scrolling | |
1436 | |
1437 (defun vip-scroll (arg) | |
1438 "Scroll to next screen." | |
1439 (interactive "p") | |
1440 (if (> arg 0) | |
1441 (while (> arg 0) | |
1442 (scroll-up) | |
1443 (setq arg (1- arg))) | |
1444 (while (> 0 arg) | |
1445 (scroll-down) | |
1446 (setq arg (1+ arg))))) | |
1447 | |
1448 (defun vip-scroll-back (arg) | |
1449 "Scroll to previous screen." | |
1450 (interactive "p") | |
1451 (vip-scroll (- arg))) | |
1452 | |
1453 (defun vip-scroll-down (arg) | |
1454 "Scroll up half screen." | |
1455 (interactive "P") | |
1456 (if (null arg) (scroll-down (/ (window-height) 2)) | |
1457 (scroll-down arg))) | |
1458 | |
1459 (defun vip-scroll-down-one (arg) | |
1460 "Scroll up one line." | |
1461 (interactive "p") | |
1462 (scroll-down arg)) | |
1463 | |
1464 (defun vip-scroll-up (arg) | |
1465 "Scroll down half screen." | |
1466 (interactive "P") | |
1467 (if (null arg) (scroll-up (/ (window-height) 2)) | |
1468 (scroll-up arg))) | |
1469 | |
1470 (defun vip-scroll-up-one (arg) | |
1471 "Scroll down one line." | |
1472 (interactive "p") | |
1473 (scroll-up arg)) | |
1474 | |
1475 | |
1476 ;; splitting window | |
1477 | |
1478 (defun vip-buffer-in-two-windows () | |
1479 "Show current buffer in two windows." | |
1480 (interactive) | |
1481 (delete-other-windows) | |
1482 (split-window-vertically nil)) | |
1483 | |
1484 | |
1485 ;; searching | |
1486 | |
1487 (defun vip-search-forward (arg) | |
1488 "Search a string forward. ARG is used to find the ARG's occurence | |
1489 of the string. Default is vanilla search. Search mode can be toggled by | |
1490 giving null search string." | |
1491 (interactive "P") | |
1492 (let ((val (vip-P-val arg)) (com (vip-getcom arg))) | |
1493 (setq vip-s-forward t | |
1494 vip-s-string (vip-read-string (if vip-re-search "RE-/" "/"))) | |
1495 (if (string= vip-s-string "") | |
1496 (progn | |
1497 (setq vip-re-search (not vip-re-search)) | |
1498 (message (format "Search mode changed to %s search." | |
1499 (if vip-re-search "regular expression" | |
1500 "vanilla")))) | |
1501 (vip-search vip-s-string t val) | |
1502 (if com | |
1503 (progn | |
1504 (move-marker vip-com-point (mark)) | |
1505 (vip-execute-com 'vip-search-next val com)))))) | |
1506 | |
1507 (defun vip-search-backward (arg) | |
1508 "Search a string backward. ARG is used to find the ARG's occurence | |
1509 of the string. Default is vanilla search. Search mode can be toggled by | |
1510 giving null search string." | |
1511 (interactive "P") | |
1512 (let ((val (vip-P-val arg)) (com (vip-getcom arg))) | |
1513 (setq vip-s-forward nil | |
1514 vip-s-string (vip-read-string (if vip-re-search "RE-?" "?"))) | |
1515 (if (string= vip-s-string "") | |
1516 (progn | |
1517 (setq vip-re-search (not vip-re-search)) | |
1518 (message (format "Search mode changed to %s search." | |
1519 (if vip-re-search "regular expression" | |
1520 "vanilla")))) | |
1521 (vip-search vip-s-string nil val) | |
1522 (if com | |
1523 (progn | |
1524 (move-marker vip-com-point (mark)) | |
1525 (vip-execute-com 'vip-search-next val com)))))) | |
1526 | |
1527 (defun vip-search (string forward arg &optional no-offset init-point) | |
1528 "(STRING FORWARD COUNT &optional NO-OFFSET) Search COUNT's occurrence of | |
1529 STRING. Search will be forward if FORWARD, otherwise backward." | |
1530 (let ((val (vip-p-val arg)) (com (vip-getcom arg)) | |
1531 (null-arg (null (vip-P-val arg))) (offset (not no-offset)) | |
1532 (case-fold-search vip-case-fold-search) | |
1533 (start-point (or init-point (point)))) | |
1534 (if forward | |
1535 (condition-case conditions | |
1536 (progn | |
1537 (if (and offset (not (eobp))) (forward-char)) | |
1538 (if vip-re-search | |
1539 (progn | |
1540 (re-search-forward string nil nil val) | |
1541 (re-search-backward string)) | |
1542 (search-forward string nil nil val) | |
1543 (search-backward string)) | |
1544 (push-mark start-point)) | |
1545 (search-failed | |
1546 (if (and null-arg vip-search-wrap-around) | |
1547 (progn | |
1548 (goto-char (point-min)) | |
1549 (vip-search string forward (cons 1 com) t start-point)) | |
1550 (goto-char start-point) | |
1551 (signal 'search-failed (cdr conditions))))) | |
1552 (condition-case conditions | |
1553 (progn | |
1554 (if vip-re-search | |
1555 (re-search-backward string nil nil val) | |
1556 (search-backward string nil nil val)) | |
1557 (push-mark start-point)) | |
1558 (search-failed | |
1559 (if (and null-arg vip-search-wrap-around) | |
1560 (progn | |
1561 (goto-char (point-max)) | |
1562 (vip-search string forward (cons 1 com) t start-point)) | |
1563 (goto-char start-point) | |
1564 (signal 'search-failed (cdr conditions)))))))) | |
1565 | |
1566 (defun vip-search-next (arg) | |
1567 "Repeat previous search." | |
1568 (interactive "P") | |
1569 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1570 (if (null vip-s-string) (error "No previous search string.")) | |
1571 (vip-search vip-s-string vip-s-forward arg) | |
1572 (if com (vip-execute-com 'vip-search-next val com)))) | |
1573 | |
1574 (defun vip-search-Next (arg) | |
1575 "Repeat previous search in the reverse direction." | |
1576 (interactive "P") | |
1577 (let ((val (vip-p-val arg)) (com (vip-getcom arg))) | |
1578 (if (null vip-s-string) (error "No previous search string.")) | |
1579 (vip-search vip-s-string (not vip-s-forward) arg) | |
1580 (if com (vip-execute-com 'vip-search-Next val com)))) | |
1581 | |
1582 | |
1583 ;; visiting and killing files, buffers | |
1584 | |
1585 (defun vip-switch-to-buffer () | |
1586 "Switch to buffer in the current window." | |
1587 (interactive) | |
1588 (let (buffer) | |
1589 (setq buffer | |
1590 (read-buffer | |
1591 (format "switch to buffer \(%s\): " | |
1592 (buffer-name (other-buffer (current-buffer)))))) | |
1593 (switch-to-buffer buffer) | |
1594 (vip-change-mode-to-vi))) | |
1595 | |
1596 (defun vip-switch-to-buffer-other-window () | |
1597 "Switch to buffer in another window." | |
1598 (interactive) | |
1599 (let (buffer) | |
1600 (setq buffer | |
1601 (read-buffer | |
1602 (format "Switch to buffer \(%s\): " | |
1603 (buffer-name (other-buffer (current-buffer)))))) | |
1604 (switch-to-buffer-other-window buffer) | |
1605 (vip-change-mode-to-vi))) | |
1606 | |
1607 (defun vip-kill-buffer () | |
1608 "Kill a buffer." | |
1609 (interactive) | |
1610 (let (buffer buffer-name) | |
1611 (setq buffer-name | |
1612 (read-buffer | |
1613 (format "Kill buffer \(%s\): " | |
1614 (buffer-name (current-buffer))))) | |
1615 (setq buffer | |
1616 (if (null buffer-name) | |
1617 (current-buffer) | |
1618 (get-buffer buffer-name))) | |
1619 (if (null buffer) (error "Buffer %s nonexistent." buffer-name)) | |
1620 (if (or (not (buffer-modified-p buffer)) | |
1621 (y-or-n-p "Buffer is modified, are you sure? ")) | |
1622 (kill-buffer buffer) | |
1623 (error "Buffer not killed.")))) | |
1624 | |
1625 (defun vip-find-file () | |
1626 "Visit file in the current window." | |
1627 (interactive) | |
1628 (let (file) | |
1629 (setq file (read-file-name "visit file: ")) | |
1630 (switch-to-buffer (find-file-noselect file)) | |
1631 (vip-change-mode-to-vi))) | |
1632 | |
1633 (defun vip-find-file-other-window () | |
1634 "Visit file in another window." | |
1635 (interactive) | |
1636 (let (file) | |
1637 (setq file (read-file-name "Visit file: ")) | |
1638 (switch-to-buffer-other-window (find-file-noselect file)) | |
1639 (vip-change-mode-to-vi))) | |
1640 | |
1641 (defun vip-info-on-file () | |
1642 "Give information of the file associated to the current buffer." | |
1643 (interactive) | |
1644 (message "\"%s\" line %d of %d" | |
1645 (if (buffer-file-name) (buffer-file-name) "") | |
1646 (1+ (count-lines (point-min) (point))) | |
1647 (1+ (count-lines (point-min) (point-max))))) | |
1648 | |
1649 | |
1650 ;; yank and pop | |
1651 | |
1652 (defun vip-yank (text) | |
1653 "yank TEXT silently." | |
1654 (save-excursion | |
1655 (vip-push-mark-silent (point)) | |
1656 (insert text) | |
1657 (exchange-point-and-mark)) | |
1658 (skip-chars-forward " \t")) | |
1659 | |
1660 (defun vip-put-back (arg) | |
1661 "Put back after point/below line." | |
1662 (interactive "P") | |
1663 (let ((val (vip-p-val arg)) | |
1664 (text (if vip-use-register | |
1665 (if (and (<= ?1 vip-use-register) (<= vip-use-register ?9)) | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
1666 (current-kill (- vip-use-register ?1) 'do-not-rotate) |
11 | 1667 (get-register vip-use-register)) |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
1668 (current-kill 0)))) |
11 | 1669 (if (null text) |
1670 (if vip-use-register | |
1671 (let ((reg vip-use-register)) | |
1672 (setq vip-use-register nil) | |
1673 (error "Nothing in register %c" reg)) | |
1674 (error ""))) | |
1675 (setq vip-use-register nil) | |
1676 (if (vip-end-with-a-newline-p text) | |
1677 (progn | |
1678 (next-line 1) | |
1679 (beginning-of-line)) | |
1680 (if (and (not (eolp)) (not (eobp))) (forward-char))) | |
1681 (setq vip-d-com (list 'vip-put-back val nil vip-use-register)) | |
1682 (vip-loop val (vip-yank text)))) | |
1683 | |
1684 (defun vip-Put-back (arg) | |
1685 "Put back at point/above line." | |
1686 (interactive "P") | |
1687 (let ((val (vip-p-val arg)) | |
1688 (text (if vip-use-register | |
1689 (if (and (<= ?1 vip-use-register) (<= vip-use-register ?9)) | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
1690 (current-kill (- vip-use-register ?1) 'do-not-rotate) |
11 | 1691 (get-register vip-use-register)) |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
1692 (current-kill 0)))) |
11 | 1693 (if (null text) |
1694 (if vip-use-register | |
1695 (let ((reg vip-use-register)) | |
1696 (setq vip-use-register nil) | |
1697 (error "Nothing in register %c" reg)) | |
1698 (error ""))) | |
1699 (setq vip-use-register nil) | |
1700 (if (vip-end-with-a-newline-p text) (beginning-of-line)) | |
1701 (setq vip-d-com (list 'vip-Put-back val nil vip-use-register)) | |
1702 (vip-loop val (vip-yank text)))) | |
1703 | |
1704 (defun vip-delete-char (arg) | |
1705 "Delete character." | |
1706 (interactive "P") | |
1707 (let ((val (vip-p-val arg))) | |
1708 (setq vip-d-com (list 'vip-delete-char val nil)) | |
1709 (if vip-use-register | |
1710 (progn | |
1711 (if (and (<= ?A vip-use-register) (<= vip-use-register ?Z)) | |
1712 (vip-append-to-register | |
1625
376427dea90a
* bytecomp.el: Declare unread-command-char an obsolete variable.
Jim Blandy <jimb@redhat.com>
parents:
1577
diff
changeset
|
1713 (+ vip-use-register 32) (point) (- (point) val)) |
11 | 1714 (copy-to-register vip-use-register (point) (- (point) val) nil)) |
1715 (setq vip-use-register nil))) | |
1716 (delete-char val t))) | |
1717 | |
1718 (defun vip-delete-backward-char (arg) | |
1719 "Delete previous character." | |
1720 (interactive "P") | |
1721 (let ((val (vip-p-val arg))) | |
1722 (setq vip-d-com (list 'vip-delete-backward-char val nil)) | |
1723 (if vip-use-register | |
1724 (progn | |
1725 (if (and (<= ?A vip-use-register) (<= vip-use-register ?Z)) | |
1726 (vip-append-to-register | |
1625
376427dea90a
* bytecomp.el: Declare unread-command-char an obsolete variable.
Jim Blandy <jimb@redhat.com>
parents:
1577
diff
changeset
|
1727 (+ vip-use-register 32) (point) (+ (point) val)) |
11 | 1728 (copy-to-register vip-use-register (point) (+ (point) val) nil)) |
1729 (setq vip-use-register nil))) | |
1730 (delete-backward-char val t))) | |
1731 | |
1732 | |
1733 ;; join lines. | |
1734 | |
1735 (defun vip-join-lines (arg) | |
1736 "Join this line to next, if ARG is nil. Otherwise, join ARG lines" | |
1737 (interactive "*P") | |
1738 (let ((val (vip-P-val arg))) | |
1739 (setq vip-d-com (list 'vip-join-lines val nil)) | |
1740 (vip-loop (if (null val) 1 (1- val)) | |
1741 (progn | |
1742 (end-of-line) | |
1743 (if (not (eobp)) | |
1744 (progn | |
1745 (forward-line 1) | |
1746 (delete-region (point) (1- (point))) | |
1747 (fixup-whitespace))))))) | |
1748 | |
1749 | |
1750 ;; making small changes | |
1751 | |
1752 (defun vip-change (beg end) | |
1753 (setq c-string | |
1754 (vip-read-string (format "%s => " (buffer-substring beg end)))) | |
1755 (vip-change-subr beg end)) | |
1756 | |
1757 (defun vip-change-subr (beg end) | |
1758 (if vip-use-register | |
1759 (progn | |
1760 (copy-to-register vip-use-register beg end nil) | |
1761 (setq vip-use-register nil))) | |
1762 (kill-region beg end) | |
1763 (setq this-command 'vip-change) | |
1764 (insert c-string)) | |
1765 | |
1766 | |
1767 ;; query replace | |
1768 | |
1769 (defun vip-query-replace () | |
1770 "Query replace. If you supply null string as the string to be replaced, | |
1771 the query replace mode will toggle between string replace and regexp replace." | |
1772 (interactive) | |
1773 (let (str) | |
1774 (setq str (vip-read-string | |
1775 (if vip-re-query-replace "Query replace regexp: " | |
1776 "Query replace: "))) | |
1777 (if (string= str "") | |
1778 (progn | |
1779 (setq vip-re-query-replace (not vip-re-query-replace)) | |
1780 (message "Query replace mode changed to %s." | |
1781 (if vip-re-query-replace "regexp replace" | |
1782 "string replace"))) | |
1783 (if vip-re-query-replace | |
1784 (query-replace-regexp | |
1785 str | |
1786 (vip-read-string (format "Query replace regexp \"%s\" with: " str))) | |
1787 (query-replace | |
1788 str | |
1789 (vip-read-string (format "Query replace \"%s\" with: " str))))))) | |
1790 | |
1791 | |
1792 ;; marking | |
1793 | |
1794 (defun vip-mark-beginning-of-buffer () | |
1795 (interactive) | |
1796 (set-mark (point)) | |
1797 (goto-char (point-min)) | |
1798 (exchange-point-and-mark) | |
1799 (message "mark set at the beginning of buffer")) | |
1800 | |
1801 (defun vip-mark-end-of-buffer () | |
1802 (interactive) | |
1803 (set-mark (point)) | |
1804 (goto-char (point-max)) | |
1805 (exchange-point-and-mark) | |
1806 (message "mark set at the end of buffer")) | |
1807 | |
1808 (defun vip-mark-point (char) | |
1809 (interactive "c") | |
1810 (cond ((and (<= ?a char) (<= char ?z)) | |
1625
376427dea90a
* bytecomp.el: Declare unread-command-char an obsolete variable.
Jim Blandy <jimb@redhat.com>
parents:
1577
diff
changeset
|
1811 (point-to-register (- char (- ?a ?\C-a)) nil)) |
11 | 1812 ((= char ?<) (vip-mark-beginning-of-buffer)) |
1813 ((= char ?>) (vip-mark-end-of-buffer)) | |
1814 ((= char ?.) (push-mark)) | |
1815 ((= char ?,) (set-mark-command 1)) | |
1816 ((= char ?D) (mark-defun)) | |
1817 (t (error "")))) | |
1818 | |
1819 (defun vip-goto-mark (arg) | |
1820 "Go to mark." | |
1821 (interactive "P") | |
1822 (let ((char (read-char)) (com (vip-getcom arg))) | |
1823 (vip-goto-mark-subr char com nil))) | |
1824 | |
1825 (defun vip-goto-mark-and-skip-white (arg) | |
1826 "Go to mark and skip to first non-white on line." | |
1827 (interactive "P") | |
1828 (let ((char (read-char)) (com (vip-getCom arg))) | |
1829 (vip-goto-mark-subr char com t))) | |
1830 | |
1831 (defun vip-goto-mark-subr (char com skip-white) | |
1832 (cond ((and (<= ?a char) (<= char ?z)) | |
1833 (let ((buff (current-buffer))) | |
1834 (if com (move-marker vip-com-point (point))) | |
1835 (goto-char (register-to-point (- char (- ?a ?\C-a)))) | |
1836 (if skip-white (back-to-indentation)) | |
1837 (vip-change-mode-to-vi) | |
1838 (if com | |
1839 (if (equal buff (current-buffer)) | |
1840 (vip-execute-com (if skip-white | |
1841 'vip-goto-mark-and-skip-white | |
1842 'vip-goto-mark) | |
1843 nil com) | |
1844 (switch-to-buffer buff) | |
1845 (goto-char vip-com-point) | |
1846 (vip-change-mode-to-vi) | |
1847 (error ""))))) | |
1848 ((and (not skip-white) (= char ?`)) | |
1849 (if com (move-marker vip-com-point (point))) | |
1850 (exchange-point-and-mark) | |
1851 (if com (vip-execute-com 'vip-goto-mark nil com))) | |
1852 ((and skip-white (= char ?')) | |
1853 (if com (move-marker vip-com-point (point))) | |
1854 (exchange-point-and-mark) | |
1855 (back-to-indentation) | |
1856 (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com))) | |
1857 (t (error "")))) | |
1858 | |
1859 (defun vip-exchange-point-and-mark () | |
1860 (interactive) | |
1861 (exchange-point-and-mark) | |
1862 (back-to-indentation)) | |
1863 | |
1864 (defun vip-keyboard-quit () | |
1865 "Abort partially formed or running command." | |
1866 (interactive) | |
1867 (setq vip-use-register nil) | |
1868 (keyboard-quit)) | |
1869 | |
1870 (defun vip-ctl-c-equivalent (arg) | |
1871 "Emulate C-c in Emacs mode." | |
1872 (interactive "P") | |
1873 (vip-ctl-key-equivalent "\C-c" arg)) | |
1874 | |
1875 (defun vip-ctl-x-equivalent (arg) | |
1876 "Emulate C-x in Emacs mode." | |
1877 (interactive "P") | |
1878 (vip-ctl-key-equivalent "\C-x" arg)) | |
1879 | |
1880 (defun vip-ctl-key-equivalent (key arg) | |
1881 (let ((char (read-char))) | |
1882 (if (and (<= ?A char) (<= char ?Z)) | |
1883 (setq char (- char (- ?A ?\C-a)))) | |
1884 (setq prefix-arg arg) | |
1885 (command-execute | |
1886 (vip-get-editor-command | |
1887 vip-emacs-local-map global-map | |
1888 (format "%s%s" key (char-to-string char)))))) | |
1889 | |
1890 | |
1891 ;; commands in insertion mode | |
1892 | |
1893 (defun vip-delete-backward-word (arg) | |
1894 "Delete previous word." | |
1895 (interactive "p") | |
1896 (save-excursion | |
1897 (set-mark (point)) | |
1898 (backward-word arg) | |
1899 (delete-region (point) (mark)))) | |
1900 | |
1901 | |
1902 ;; key bindings | |
1903 | |
1904 (set 'vip-mode-map (make-keymap)) | |
1905 | |
1906 (define-key vip-mode-map "\C-a" 'beginning-of-line) | |
1907 (define-key vip-mode-map "\C-b" 'vip-scroll-back) | |
1908 (define-key vip-mode-map "\C-c" 'vip-ctl-c) | |
1909 (define-key vip-mode-map "\C-d" 'vip-scroll-up) | |
1910 (define-key vip-mode-map "\C-e" 'vip-scroll-up-one) | |
1911 (define-key vip-mode-map "\C-f" 'vip-scroll) | |
1912 (define-key vip-mode-map "\C-g" 'vip-keyboard-quit) | |
1913 (define-key vip-mode-map "\C-h" 'help-command) | |
1914 (define-key vip-mode-map "\C-m" 'vip-scroll-back) | |
1915 (define-key vip-mode-map "\C-n" 'vip-other-window) | |
1916 (define-key vip-mode-map "\C-o" 'vip-open-line-at-point) | |
1917 (define-key vip-mode-map "\C-u" 'vip-scroll-down) | |
1918 (define-key vip-mode-map "\C-x" 'vip-ctl-x) | |
1919 (define-key vip-mode-map "\C-y" 'vip-scroll-down-one) | |
1920 (define-key vip-mode-map "\C-z" 'vip-change-mode-to-emacs) | |
1921 (define-key vip-mode-map "\e" 'vip-ESC) | |
1922 | |
1923 (define-key vip-mode-map " " 'vip-scroll) | |
1924 (define-key vip-mode-map "!" 'vip-command-argument) | |
1925 (define-key vip-mode-map "\"" 'vip-command-argument) | |
1926 (define-key vip-mode-map "#" 'vip-command-argument) | |
1927 (define-key vip-mode-map "$" 'vip-goto-eol) | |
1928 (define-key vip-mode-map "%" 'vip-paren-match) | |
1929 (define-key vip-mode-map "&" 'vip-nil) | |
1930 (define-key vip-mode-map "'" 'vip-goto-mark-and-skip-white) | |
1931 (define-key vip-mode-map "(" 'vip-backward-sentence) | |
1932 (define-key vip-mode-map ")" 'vip-forward-sentence) | |
1933 (define-key vip-mode-map "*" 'call-last-kbd-macro) | |
1934 (define-key vip-mode-map "+" 'vip-next-line-at-bol) | |
1935 (define-key vip-mode-map "," 'vip-repeat-find-opposite) | |
1936 (define-key vip-mode-map "-" 'vip-previous-line-at-bol) | |
1937 (define-key vip-mode-map "." 'vip-repeat) | |
1938 (define-key vip-mode-map "/" 'vip-search-forward) | |
1939 | |
1940 (define-key vip-mode-map "0" 'vip-beginning-of-line) | |
1941 (define-key vip-mode-map "1" 'vip-digit-argument) | |
1942 (define-key vip-mode-map "2" 'vip-digit-argument) | |
1943 (define-key vip-mode-map "3" 'vip-digit-argument) | |
1944 (define-key vip-mode-map "4" 'vip-digit-argument) | |
1945 (define-key vip-mode-map "5" 'vip-digit-argument) | |
1946 (define-key vip-mode-map "6" 'vip-digit-argument) | |
1947 (define-key vip-mode-map "7" 'vip-digit-argument) | |
1948 (define-key vip-mode-map "8" 'vip-digit-argument) | |
1949 (define-key vip-mode-map "9" 'vip-digit-argument) | |
1950 | |
1951 (define-key vip-mode-map ":" 'vip-ex) | |
1952 (define-key vip-mode-map ";" 'vip-repeat-find) | |
1953 (define-key vip-mode-map "<" 'vip-command-argument) | |
1954 (define-key vip-mode-map "=" 'vip-command-argument) | |
1955 (define-key vip-mode-map ">" 'vip-command-argument) | |
1956 (define-key vip-mode-map "?" 'vip-search-backward) | |
1957 (define-key vip-mode-map "@" 'vip-nil) | |
1958 | |
1959 (define-key vip-mode-map "A" 'vip-Append) | |
1960 (define-key vip-mode-map "B" 'vip-backward-Word) | |
1961 (define-key vip-mode-map "C" 'vip-ctl-c-equivalent) | |
1962 (define-key vip-mode-map "D" 'vip-kill-line) | |
1963 (define-key vip-mode-map "E" 'vip-end-of-Word) | |
1964 (define-key vip-mode-map "F" 'vip-find-char-backward) | |
1965 (define-key vip-mode-map "G" 'vip-goto-line) | |
1966 (define-key vip-mode-map "H" 'vip-window-top) | |
1967 (define-key vip-mode-map "I" 'vip-Insert) | |
1968 (define-key vip-mode-map "J" 'vip-join-lines) | |
1969 (define-key vip-mode-map "K" 'vip-kill-buffer) | |
1970 (define-key vip-mode-map "L" 'vip-window-bottom) | |
1971 (define-key vip-mode-map "M" 'vip-window-middle) | |
1972 (define-key vip-mode-map "N" 'vip-search-Next) | |
1973 (define-key vip-mode-map "O" 'vip-Open-line) | |
1974 (define-key vip-mode-map "P" 'vip-Put-back) | |
1975 (define-key vip-mode-map "Q" 'vip-query-replace) | |
1976 (define-key vip-mode-map "R" 'vip-replace-string) | |
1977 (define-key vip-mode-map "S" 'vip-switch-to-buffer-other-window) | |
1978 (define-key vip-mode-map "T" 'vip-goto-char-backward) | |
1979 (define-key vip-mode-map "U" 'vip-nil) | |
1980 (define-key vip-mode-map "V" 'vip-find-file-other-window) | |
1981 (define-key vip-mode-map "W" 'vip-forward-Word) | |
1982 (define-key vip-mode-map "X" 'vip-ctl-x-equivalent) | |
1983 (define-key vip-mode-map "Y" 'vip-yank-line) | |
1984 (define-key vip-mode-map "ZZ" 'save-buffers-kill-emacs) | |
1985 | |
1986 (define-key vip-mode-map "[" 'vip-nil) | |
1987 (define-key vip-mode-map "\\" 'vip-escape-to-emacs) | |
1988 (define-key vip-mode-map "]" 'vip-nil) | |
1989 (define-key vip-mode-map "^" 'vip-bol-and-skip-white) | |
1990 (define-key vip-mode-map "_" 'vip-nil) | |
1991 (define-key vip-mode-map "`" 'vip-goto-mark) | |
1992 | |
1993 (define-key vip-mode-map "a" 'vip-append) | |
1994 (define-key vip-mode-map "b" 'vip-backward-word) | |
1995 (define-key vip-mode-map "c" 'vip-command-argument) | |
1996 (define-key vip-mode-map "d" 'vip-command-argument) | |
1997 (define-key vip-mode-map "e" 'vip-end-of-word) | |
1998 (define-key vip-mode-map "f" 'vip-find-char-forward) | |
1999 (define-key vip-mode-map "g" 'vip-info-on-file) | |
2000 (define-key vip-mode-map "h" 'vip-backward-char) | |
2001 (define-key vip-mode-map "i" 'vip-insert) | |
2002 (define-key vip-mode-map "j" 'vip-next-line) | |
2003 (define-key vip-mode-map "k" 'vip-previous-line) | |
2004 (define-key vip-mode-map "l" 'vip-forward-char) | |
2005 (define-key vip-mode-map "m" 'vip-mark-point) | |
2006 (define-key vip-mode-map "n" 'vip-search-next) | |
2007 (define-key vip-mode-map "o" 'vip-open-line) | |
2008 (define-key vip-mode-map "p" 'vip-put-back) | |
2009 (define-key vip-mode-map "q" 'vip-nil) | |
2010 (define-key vip-mode-map "r" 'vip-replace-char) | |
2011 (define-key vip-mode-map "s" 'vip-switch-to-buffer) | |
2012 (define-key vip-mode-map "t" 'vip-goto-char-forward) | |
2013 (define-key vip-mode-map "u" 'vip-undo) | |
2014 (define-key vip-mode-map "v" 'vip-find-file) | |
2015 (define-key vip-mode-map "w" 'vip-forward-word) | |
2016 (define-key vip-mode-map "x" 'vip-delete-char) | |
2017 (define-key vip-mode-map "y" 'vip-command-argument) | |
2018 (define-key vip-mode-map "zH" 'vip-line-to-top) | |
2019 (define-key vip-mode-map "zM" 'vip-line-to-middle) | |
2020 (define-key vip-mode-map "zL" 'vip-line-to-bottom) | |
2021 (define-key vip-mode-map "z\C-m" 'vip-line-to-top) | |
2022 (define-key vip-mode-map "z." 'vip-line-to-middle) | |
2023 (define-key vip-mode-map "z-" 'vip-line-to-bottom) | |
2024 | |
2025 (define-key vip-mode-map "{" 'vip-backward-paragraph) | |
2026 (define-key vip-mode-map "|" 'vip-goto-col) | |
2027 (define-key vip-mode-map "}" 'vip-forward-paragraph) | |
2028 (define-key vip-mode-map "~" 'vip-nil) | |
2029 (define-key vip-mode-map "\177" 'vip-delete-backward-char) | |
2030 | |
2031 (define-key ctl-x-map "3" 'vip-buffer-in-two-windows) | |
2032 (define-key ctl-x-map "\C-i" 'insert-file) | |
2033 | |
2034 (defun vip-version () | |
2035 (interactive) | |
2036 (message "VIP version 3.5 of September 15, 1987")) | |
2037 | |
2038 | |
2039 ;; implement ex commands | |
2040 | |
2041 (defvar ex-token-type nil | |
2042 "type of token. if non-nil, gives type of address. if nil, it | |
2043 is a command.") | |
2044 | |
2045 (defvar ex-token nil | |
2046 "value of token.") | |
2047 | |
2048 (defvar ex-addresses nil | |
2049 "list of ex addresses") | |
2050 | |
2051 (defvar ex-flag nil | |
2052 "flag for ex flag") | |
2053 | |
2054 (defvar ex-buffer nil | |
2055 "name of ex buffer") | |
2056 | |
2057 (defvar ex-count nil | |
2058 "value of ex count") | |
2059 | |
2060 (defvar ex-g-flag nil | |
2061 "flag for global command") | |
2062 | |
2063 (defvar ex-g-variant nil | |
2064 "if t global command is executed on lines not matching ex-g-pat") | |
2065 | |
2066 (defvar ex-reg-exp nil | |
2067 "save reg-exp used in substitute") | |
2068 | |
2069 (defvar ex-repl nil | |
2070 "replace pattern for substitute") | |
2071 | |
2072 (defvar ex-g-pat nil | |
2073 "pattern for global command") | |
2074 | |
2075 (defvar ex-map (make-sparse-keymap) | |
2076 "save commnads for mapped keys") | |
2077 | |
2078 (defvar ex-tag nil | |
2079 "save ex tag") | |
2080 | |
2081 (defvar ex-file nil) | |
2082 | |
2083 (defvar ex-variant nil) | |
2084 | |
2085 (defvar ex-offset nil) | |
2086 | |
2087 (defvar ex-append nil) | |
2088 | |
2089 (defun vip-nil () | |
2090 (interactive) | |
2091 (error "")) | |
2092 | |
2093 (defun vip-looking-back (str) | |
2094 "returns t if looking back reg-exp STR before point." | |
2095 (and (save-excursion (re-search-backward str nil t)) | |
2096 (= (point) (match-end 0)))) | |
2097 | |
2098 (defun vip-check-sub (str) | |
2099 "check if ex-token is an initial segment of STR" | |
2100 (let ((length (length ex-token))) | |
2101 (if (and (<= length (length str)) | |
2102 (string= ex-token (substring str 0 length))) | |
2103 (setq ex-token str) | |
2104 (setq ex-token-type "non-command")))) | |
2105 | |
2106 (defun vip-get-ex-com-subr () | |
2107 "get a complete ex command" | |
2108 (set-mark (point)) | |
2109 (re-search-forward "[a-z][a-z]*") | |
2110 (setq ex-token-type "command") | |
2111 (setq ex-token (buffer-substring (point) (mark))) | |
2112 (exchange-point-and-mark) | |
2113 (cond ((looking-at "a") | |
2114 (cond ((looking-at "ab") (vip-check-sub "abbreviate")) | |
2115 ((looking-at "ar") (vip-check-sub "args")) | |
2116 (t (vip-check-sub "append")))) | |
2117 ((looking-at "[bh]") (setq ex-token-type "non-command")) | |
2118 ((looking-at "c") | |
2119 (if (looking-at "co") (vip-check-sub "copy") | |
2120 (vip-check-sub "change"))) | |
2121 ((looking-at "d") (vip-check-sub "delete")) | |
2122 ((looking-at "e") | |
2123 (if (looking-at "ex") (vip-check-sub "ex") | |
2124 (vip-check-sub "edit"))) | |
2125 ((looking-at "f") (vip-check-sub "file")) | |
2126 ((looking-at "g") (vip-check-sub "global")) | |
2127 ((looking-at "i") (vip-check-sub "insert")) | |
2128 ((looking-at "j") (vip-check-sub "join")) | |
2129 ((looking-at "l") (vip-check-sub "list")) | |
2130 ((looking-at "m") | |
2131 (cond ((looking-at "map") (vip-check-sub "map")) | |
2132 ((looking-at "mar") (vip-check-sub "mark")) | |
2133 (t (vip-check-sub "move")))) | |
2134 ((looking-at "n") | |
2135 (if (looking-at "nu") (vip-check-sub "number") | |
2136 (vip-check-sub "next"))) | |
2137 ((looking-at "o") (vip-check-sub "open")) | |
2138 ((looking-at "p") | |
2139 (cond ((looking-at "pre") (vip-check-sub "preserve")) | |
2140 ((looking-at "pu") (vip-check-sub "put")) | |
2141 (t (vip-check-sub "print")))) | |
2142 ((looking-at "q") (vip-check-sub "quit")) | |
2143 ((looking-at "r") | |
2144 (cond ((looking-at "rec") (vip-check-sub "recover")) | |
2145 ((looking-at "rew") (vip-check-sub "rewind")) | |
2146 (t (vip-check-sub "read")))) | |
2147 ((looking-at "s") | |
2148 (cond ((looking-at "se") (vip-check-sub "set")) | |
2149 ((looking-at "sh") (vip-check-sub "shell")) | |
2150 ((looking-at "so") (vip-check-sub "source")) | |
2151 ((looking-at "st") (vip-check-sub "stop")) | |
2152 (t (vip-check-sub "substitute")))) | |
2153 ((looking-at "t") | |
2154 (if (looking-at "ta") (vip-check-sub "tag") | |
2155 (vip-check-sub "t"))) | |
2156 ((looking-at "u") | |
2157 (cond ((looking-at "una") (vip-check-sub "unabbreviate")) | |
2158 ((looking-at "unm") (vip-check-sub "unmap")) | |
2159 (t (vip-check-sub "undo")))) | |
2160 ((looking-at "v") | |
2161 (cond ((looking-at "ve") (vip-check-sub "version")) | |
2162 ((looking-at "vi") (vip-check-sub "visual")) | |
2163 (t (vip-check-sub "v")))) | |
2164 ((looking-at "w") | |
2165 (if (looking-at "wq") (vip-check-sub "wq") | |
2166 (vip-check-sub "write"))) | |
2167 ((looking-at "x") (vip-check-sub "xit")) | |
2168 ((looking-at "y") (vip-check-sub "yank")) | |
2169 ((looking-at "z") (vip-check-sub "z"))) | |
2170 (exchange-point-and-mark)) | |
2171 | |
2172 (defun vip-get-ex-token () | |
2173 "get an ex-token which is either an address or a command. | |
2174 a token has type \(command, address, end-mark\) and value." | |
2175 (save-window-excursion | |
2176 (set-buffer " *ex-working-space*") | |
2177 (skip-chars-forward " \t") | |
2178 (cond ((looking-at "[k#]") | |
2179 (setq ex-token-type "command") | |
2180 (setq ex-token (char-to-string (following-char))) | |
2181 (forward-char 1)) | |
2182 ((looking-at "[a-z]") (vip-get-ex-com-subr)) | |
2183 ((looking-at "\\.") | |
2184 (forward-char 1) | |
2185 (setq ex-token-type "dot")) | |
2186 ((looking-at "[0-9]") | |
2187 (set-mark (point)) | |
2188 (re-search-forward "[0-9]*") | |
2189 (setq ex-token-type | |
2190 (cond ((string= ex-token-type "plus") "add-number") | |
2191 ((string= ex-token-type "minus") "sub-number") | |
2192 (t "abs-number"))) | |
2193 (setq ex-token (string-to-int (buffer-substring (point) (mark))))) | |
2194 ((looking-at "\\$") | |
2195 (forward-char 1) | |
2196 (setq ex-token-type "end")) | |
2197 ((looking-at "%") | |
2198 (forward-char 1) | |
2199 (setq ex-token-type "whole")) | |
2200 ((looking-at "+") | |
2201 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]")) | |
2202 (forward-char 1) | |
2203 (insert "1") | |
2204 (backward-char 1) | |
2205 (setq ex-token-type "plus")) | |
2206 ((looking-at "+[0-9]") | |
2207 (forward-char 1) | |
2208 (setq ex-token-type "plus")) | |
2209 (t | |
2210 (error "Badly formed address")))) | |
2211 ((looking-at "-") | |
2212 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]")) | |
2213 (forward-char 1) | |
2214 (insert "1") | |
2215 (backward-char 1) | |
2216 (setq ex-token-type "minus")) | |
2217 ((looking-at "-[0-9]") | |
2218 (forward-char 1) | |
2219 (setq ex-token-type "minus")) | |
2220 (t | |
2221 (error "Badly formed address")))) | |
2222 ((looking-at "/") | |
2223 (forward-char 1) | |
2224 (set-mark (point)) | |
2225 (let ((cont t)) | |
2226 (while (and (not (eolp)) cont) | |
2227 ;;(re-search-forward "[^/]*/") | |
2228 (re-search-forward "[^/]*\\(/\\|\n\\)") | |
2229 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/")) | |
2230 (setq cont nil)))) | |
2231 (backward-char 1) | |
2232 (setq ex-token (buffer-substring (point) (mark))) | |
2233 (if (looking-at "/") (forward-char 1)) | |
2234 (setq ex-token-type "search-forward")) | |
2235 ((looking-at "\\?") | |
2236 (forward-char 1) | |
2237 (set-mark (point)) | |
2238 (let ((cont t)) | |
2239 (while (and (not (eolp)) cont) | |
2240 ;;(re-search-forward "[^\\?]*\\?") | |
2241 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)") | |
2242 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?")) | |
2243 (setq cont nil)) | |
2244 (backward-char 1) | |
2245 (if (not (looking-at "\n")) (forward-char 1)))) | |
2246 (setq ex-token-type "search-backward") | |
2247 (setq ex-token (buffer-substring (1- (point)) (mark)))) | |
2248 ((looking-at ",") | |
2249 (forward-char 1) | |
2250 (setq ex-token-type "comma")) | |
2251 ((looking-at ";") | |
2252 (forward-char 1) | |
2253 (setq ex-token-type "semi-colon")) | |
2254 ((looking-at "[!=><&~]") | |
2255 (setq ex-token-type "command") | |
2256 (setq ex-token (char-to-string (following-char))) | |
2257 (forward-char 1)) | |
2258 ((looking-at "'") | |
2259 (setq ex-token-type "goto-mark") | |
2260 (forward-char 1) | |
2261 (cond ((looking-at "'") (setq ex-token nil)) | |
2262 ((looking-at "[a-z]") (setq ex-token (following-char))) | |
2263 (t (error "Marks are ' and a-z"))) | |
2264 (forward-char 1)) | |
2265 ((looking-at "\n") | |
2266 (setq ex-token-type "end-mark") | |
2267 (setq ex-token "goto")) | |
2268 (t | |
2269 (error "illegal token"))))) | |
2270 | |
2271 (defun vip-ex (&optional string) | |
2272 "ex commands within VIP." | |
2273 (interactive) | |
2274 (or string | |
2275 (setq ex-g-flag nil | |
2276 ex-g-variant nil)) | |
2277 (let ((com-str (or string (vip-read-string ":"))) | |
2278 (address nil) (cont t) (dot (point))) | |
2279 (save-window-excursion | |
2280 (set-buffer (get-buffer-create " *ex-working-space*")) | |
2281 (delete-region (point-min) (point-max)) | |
2282 (insert com-str "\n") | |
2283 (goto-char (point-min))) | |
2284 (setq ex-token-type "") | |
2285 (setq ex-addresses nil) | |
2286 (while cont | |
2287 (vip-get-ex-token) | |
2288 (cond ((or (string= ex-token-type "command") | |
2289 (string= ex-token-type "end-mark")) | |
2290 (if address (setq ex-addresses (cons address ex-addresses))) | |
2291 (cond ((string= ex-token "global") | |
2292 (ex-global nil) | |
2293 (setq cont nil)) | |
2294 ((string= ex-token "v") | |
2295 (ex-global t) | |
2296 (setq cont nil)) | |
2297 (t | |
2298 (vip-execute-ex-command) | |
2299 (save-window-excursion | |
2300 (set-buffer " *ex-working-space*") | |
2301 (skip-chars-forward " \t") | |
2302 (cond ((looking-at "|") | |
2303 (forward-char 1)) | |
2304 ((looking-at "\n") | |
2305 (setq cont nil)) | |
2306 (t (error "Extra character at end of a command"))))))) | |
2307 ((string= ex-token-type "non-command") | |
2308 (error (format "%s: Not an editor command" ex-token))) | |
2309 ((string= ex-token-type "whole") | |
2310 (setq ex-addresses | |
2311 (cons (point-max) (cons (point-min) ex-addresses)))) | |
2312 ((string= ex-token-type "comma") | |
2313 (setq ex-addresses | |
2314 (cons (if (null address) (point) address) ex-addresses))) | |
2315 ((string= ex-token-type "semi-colon") | |
2316 (if address (setq dot address)) | |
2317 (setq ex-addresses | |
2318 (cons (if (null address) (point) address) ex-addresses))) | |
2319 (t (let ((ans (vip-get-ex-address-subr address dot))) | |
2320 (if ans (setq address ans)))))))) | |
2321 | |
2322 (defun vip-get-ex-pat () | |
2323 "get a regular expression and set ex-variant if found" | |
2324 (save-window-excursion | |
2325 (set-buffer " *ex-working-space*") | |
2326 (skip-chars-forward " \t") | |
2327 (if (looking-at "!") | |
2328 (progn | |
2329 (setq ex-g-variant (not ex-g-variant) | |
2330 ex-g-flag (not ex-g-flag)) | |
2331 (forward-char 1) | |
2332 (skip-chars-forward " \t"))) | |
2333 (if (looking-at "/") | |
2334 (progn | |
2335 (forward-char 1) | |
2336 (set-mark (point)) | |
2337 (let ((cont t)) | |
2338 (while (and (not (eolp)) cont) | |
2339 (re-search-forward "[^/]*\\(/\\|\n\\)") | |
2340 ;;(re-search-forward "[^/]*/") | |
2341 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/")) | |
2342 (setq cont nil)))) | |
2343 (setq ex-token | |
2344 (if (= (mark) (point)) "" | |
2345 (buffer-substring (1- (point)) (mark)))) | |
2346 (backward-char 1)) | |
2347 (setq ex-token nil)))) | |
2348 | |
2349 (defun vip-get-ex-command () | |
2350 "get an ex command" | |
2351 (save-window-excursion | |
2352 (set-buffer " *ex-working-space*") | |
2353 (if (looking-at "/") (forward-char 1)) | |
2354 (skip-chars-forward " \t") | |
2355 (cond ((looking-at "[a-z]") | |
2356 (vip-get-ex-com-subr) | |
2357 (if (string= ex-token-type "non-command") | |
2358 (error "%s: not an editor command" ex-token))) | |
2359 ((looking-at "[!=><&~]") | |
2360 (setq ex-token (char-to-string (following-char))) | |
2361 (forward-char 1)) | |
2362 (t (error "Could not find an ex command"))))) | |
2363 | |
2364 (defun vip-get-ex-opt-gc () | |
2365 "get an ex option g or c" | |
2366 (save-window-excursion | |
2367 (set-buffer " *ex-working-space*") | |
2368 (if (looking-at "/") (forward-char 1)) | |
2369 (skip-chars-forward " \t") | |
2370 (cond ((looking-at "g") | |
2371 (setq ex-token "g") | |
2372 (forward-char 1) | |
2373 t) | |
2374 ((looking-at "c") | |
2375 (setq ex-token "c") | |
2376 (forward-char 1) | |
2377 t) | |
2378 (t nil)))) | |
2379 | |
2380 (defun vip-default-ex-addresses (&optional whole-flag) | |
2381 "compute default addresses. whole-flag means whole buffer." | |
2382 (cond ((null ex-addresses) | |
2383 (setq ex-addresses | |
2384 (if whole-flag | |
2385 (cons (point-max) (cons (point-min) nil)) | |
2386 (cons (point) (cons (point) nil))))) | |
2387 ((null (cdr ex-addresses)) | |
2388 (setq ex-addresses | |
2389 (cons (car ex-addresses) ex-addresses))))) | |
2390 | |
2391 (defun vip-get-ex-address () | |
2392 "get an ex-address as a marker and set ex-flag if a flag is found" | |
2393 (let ((address (point-marker)) (cont t)) | |
2394 (setq ex-token "") | |
2395 (setq ex-flag nil) | |
2396 (while cont | |
2397 (vip-get-ex-token) | |
2398 (cond ((string= ex-token-type "command") | |
2399 (if (or (string= ex-token "print") (string= ex-token "list") | |
2400 (string= ex-token "#")) | |
2401 (progn | |
2402 (setq ex-flag t) | |
2403 (setq cont nil)) | |
2404 (error "address expected"))) | |
2405 ((string= ex-token-type "end-mark") | |
2406 (setq cont nil)) | |
2407 ((string= ex-token-type "whole") | |
2408 (error "a trailing address is expected")) | |
2409 ((string= ex-token-type "comma") | |
2410 (error "Extra characters after an address")) | |
2411 (t (let ((ans (vip-get-ex-address-subr address (point-marker)))) | |
2412 (if ans (setq address ans)))))) | |
2413 address)) | |
2414 | |
2415 (defun vip-get-ex-address-subr (old-address dot) | |
2416 "returns an address as a point" | |
2417 (let ((address nil)) | |
2418 (if (null old-address) (setq old-address dot)) | |
2419 (cond ((string= ex-token-type "dot") | |
2420 (setq address dot)) | |
2421 ((string= ex-token-type "add-number") | |
2422 (save-excursion | |
2423 (goto-char old-address) | |
2424 (forward-line (if (= old-address 0) (1- ex-token) ex-token)) | |
2425 (setq address (point-marker)))) | |
2426 ((string= ex-token-type "sub-number") | |
2427 (save-excursion | |
2428 (goto-char old-address) | |
2429 (forward-line (- ex-token)) | |
2430 (setq address (point-marker)))) | |
2431 ((string= ex-token-type "abs-number") | |
2432 (save-excursion | |
2433 (goto-char (point-min)) | |
2434 (if (= ex-token 0) (setq address 0) | |
2435 (forward-line (1- ex-token)) | |
2436 (setq address (point-marker))))) | |
2437 ((string= ex-token-type "end") | |
2438 (setq address (point-max-marker))) | |
2439 ((string= ex-token-type "plus") t);; do nothing | |
2440 ((string= ex-token-type "minus") t);; do nothing | |
2441 ((string= ex-token-type "search-forward") | |
2442 (save-excursion | |
2443 (ex-search-address t) | |
2444 (setq address (point-marker)))) | |
2445 ((string= ex-token-type "search-backward") | |
2446 (save-excursion | |
2447 (ex-search-address nil) | |
2448 (setq address (point-marker)))) | |
2449 ((string= ex-token-type "goto-mark") | |
2450 (save-excursion | |
2451 (if (null ex-token) | |
2452 (exchange-point-and-mark) | |
2453 (goto-char (register-to-point (- ex-token (- ?a ?\C-a))))) | |
2454 (setq address (point-marker))))) | |
2455 address)) | |
2456 | |
2457 (defun ex-search-address (forward) | |
2458 "search pattern and set address" | |
2459 (if (string= ex-token "") | |
2460 (if (null vip-s-string) (error "No previous search string") | |
2461 (setq ex-token vip-s-string)) | |
2462 (setq vip-s-string ex-token)) | |
2463 (if forward | |
2464 (progn | |
2465 (forward-line 1) | |
2466 (re-search-forward ex-token)) | |
2467 (forward-line -1) | |
2468 (re-search-backward ex-token))) | |
2469 | |
2470 (defun vip-get-ex-buffer () | |
2471 "get a buffer name and set ex-count and ex-flag if found" | |
2472 (setq ex-buffer nil) | |
2473 (setq ex-count nil) | |
2474 (setq ex-flag nil) | |
2475 (save-window-excursion | |
2476 (set-buffer " *ex-working-space*") | |
2477 (skip-chars-forward " \t") | |
2478 (if (looking-at "[a-zA-Z]") | |
2479 (progn | |
2480 (setq ex-buffer (following-char)) | |
2481 (forward-char 1) | |
2482 (skip-chars-forward " \t"))) | |
2483 (if (looking-at "[0-9]") | |
2484 (progn | |
2485 (set-mark (point)) | |
2486 (re-search-forward "[0-9][0-9]*") | |
2487 (setq ex-count (string-to-int (buffer-substring (point) (mark)))) | |
2488 (skip-chars-forward " \t"))) | |
2489 (if (looking-at "[pl#]") | |
2490 (progn | |
2491 (setq ex-flag t) | |
2492 (forward-char 1))) | |
2493 (if (not (looking-at "[\n|]")) | |
2494 (error "Illegal extra characters")))) | |
2495 | |
2496 (defun vip-get-ex-count () | |
2497 (setq ex-variant nil | |
2498 ex-count nil | |
2499 ex-flag nil) | |
2500 (save-window-excursion | |
2501 (set-buffer " *ex-working-space*") | |
2502 (skip-chars-forward " \t") | |
2503 (if (looking-at "!") | |
2504 (progn | |
2505 (setq ex-variant t) | |
2506 (forward-char 1))) | |
2507 (skip-chars-forward " \t") | |
2508 (if (looking-at "[0-9]") | |
2509 (progn | |
2510 (set-mark (point)) | |
2511 (re-search-forward "[0-9][0-9]*") | |
2512 (setq ex-count (string-to-int (buffer-substring (point) (mark)))) | |
2513 (skip-chars-forward " \t"))) | |
2514 (if (looking-at "[pl#]") | |
2515 (progn | |
2516 (setq ex-flag t) | |
2517 (forward-char 1))) | |
2518 (if (not (looking-at "[\n|]")) | |
2519 (error "Illegal extra characters")))) | |
2520 | |
2521 (defun vip-get-ex-file () | |
2522 "get a file name and set ex-variant, ex-append and ex-offset if found" | |
2523 (setq ex-file nil | |
2524 ex-variant nil | |
2525 ex-append nil | |
2526 ex-offset nil) | |
2527 (save-window-excursion | |
2528 (set-buffer " *ex-working-space*") | |
2529 (skip-chars-forward " \t") | |
2530 (if (looking-at "!") | |
2531 (progn | |
2532 (setq ex-variant t) | |
2533 (forward-char 1) | |
2534 (skip-chars-forward " \t"))) | |
2535 (if (looking-at ">>") | |
2536 (progn | |
2537 (setq ex-append t | |
2538 ex-variant t) | |
2539 (forward-char 2) | |
2540 (skip-chars-forward " \t"))) | |
2541 (if (looking-at "+") | |
2542 (progn | |
2543 (forward-char 1) | |
2544 (set-mark (point)) | |
2545 (re-search-forward "[ \t\n]") | |
2546 (backward-char 1) | |
2547 (setq ex-offset (buffer-substring (point) (mark))) | |
2548 (forward-char 1) | |
2549 (skip-chars-forward " \t"))) | |
2550 (set-mark (point)) | |
2551 (re-search-forward "[ \t\n]") | |
2552 (backward-char 1) | |
2553 (setq ex-file (buffer-substring (point) (mark))))) | |
2554 | |
2555 (defun vip-execute-ex-command () | |
2556 "execute ex command using the value of addresses." | |
2557 (cond ((string= ex-token "goto") (ex-goto)) | |
2558 ((string= ex-token "copy") (ex-copy nil)) | |
2559 ((string= ex-token "delete") (ex-delete)) | |
2560 ((string= ex-token "edit") (ex-edit)) | |
2561 ((string= ex-token "file") (vip-info-on-file)) | |
2562 ;((string= ex-token "global") (ex-global nil)) | |
2563 ((string= ex-token "join") (ex-line "join")) | |
2564 ((string= ex-token "k") (ex-mark)) | |
2565 ((string= ex-token "mark") (ex-mark)) | |
2566 ((string= ex-token "map") (ex-map)) | |
2567 ((string= ex-token "move") (ex-copy t)) | |
2568 ((string= ex-token "put") (ex-put)) | |
2569 ((string= ex-token "quit") (ex-quit)) | |
2570 ((string= ex-token "read") (ex-read)) | |
2571 ((string= ex-token "set") (ex-set)) | |
2572 ((string= ex-token "shell") (ex-shell)) | |
2573 ((string= ex-token "substitute") (ex-substitute)) | |
2574 ((string= ex-token "stop") (suspend-emacs)) | |
2575 ((string= ex-token "t") (ex-copy nil)) | |
2576 ((string= ex-token "tag") (ex-tag)) | |
2577 ((string= ex-token "undo") (vip-undo)) | |
2578 ((string= ex-token "unmap") (ex-unmap)) | |
2579 ;((string= ex-token "v") (ex-global t)) | |
2580 ((string= ex-token "version") (vip-version)) | |
2581 ((string= ex-token "visual") (ex-edit)) | |
2582 ((string= ex-token "write") (ex-write nil)) | |
2583 ((string= ex-token "wq") (ex-write t)) | |
2584 ((string= ex-token "yank") (ex-yank)) | |
2585 ((string= ex-token "!") (ex-command)) | |
2586 ((string= ex-token "=") (ex-line-no)) | |
2587 ((string= ex-token ">") (ex-line "right")) | |
2588 ((string= ex-token "<") (ex-line "left")) | |
2589 ((string= ex-token "&") (ex-substitute t)) | |
2590 ((string= ex-token "~") (ex-substitute t t)) | |
2591 ((or (string= ex-token "append") | |
2592 (string= ex-token "args") | |
2593 (string= ex-token "change") | |
2594 (string= ex-token "insert") | |
2595 (string= ex-token "open") | |
2596 ) | |
2597 (error (format "%s: no such command from VIP" ex-token))) | |
2598 ((or (string= ex-token "abbreviate") | |
2599 (string= ex-token "list") | |
2600 (string= ex-token "next") | |
2601 (string= ex-token "print") | |
2602 (string= ex-token "preserve") | |
2603 (string= ex-token "recover") | |
2604 (string= ex-token "rewind") | |
2605 (string= ex-token "source") | |
2606 (string= ex-token "unabbreviate") | |
2607 (string= ex-token "xit") | |
2608 (string= ex-token "z") | |
2609 ) | |
2610 (error (format "%s: not implemented in VIP" ex-token))) | |
2611 (t (error (format "%s: Not an editor command" ex-token))))) | |
2612 | |
2613 (defun ex-goto () | |
2614 "ex goto command" | |
2615 (if (null ex-addresses) | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
2616 (setq ex-addresses (cons (point) nil))) |
11 | 2617 (push-mark (point)) |
2618 (goto-char (car ex-addresses)) | |
2619 (beginning-of-line)) | |
2620 | |
2621 (defun ex-copy (del-flag) | |
2622 "ex copy and move command. DEL-FLAG means delete." | |
2623 (vip-default-ex-addresses) | |
2624 (let ((address (vip-get-ex-address)) | |
2625 (end (car ex-addresses)) (beg (car (cdr ex-addresses)))) | |
2626 (goto-char end) | |
2627 (save-excursion | |
2628 (set-mark beg) | |
2629 (vip-enlarge-region (mark) (point)) | |
2630 (if del-flag (kill-region (point) (mark)) | |
2631 (copy-region-as-kill (point) (mark))) | |
2632 (if ex-flag | |
2633 (progn | |
2634 (with-output-to-temp-buffer "*copy text*" | |
2635 (princ | |
2636 (if (or del-flag ex-g-flag ex-g-variant) | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
2637 (current-kill 0) |
11 | 2638 (buffer-substring (point) (mark))))) |
2639 (condition-case nil | |
2640 (progn | |
2641 (vip-read-string "[Hit return to continue] ") | |
2642 (save-excursion (kill-buffer "*copy text*"))) | |
2643 (quit | |
2644 (save-excursion (kill-buffer "*copy text*")) | |
2645 (signal 'quit nil)))))) | |
2646 (if (= address 0) | |
2647 (goto-char (point-min)) | |
2648 (goto-char address) | |
2649 (forward-line 1)) | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
2650 (insert (current-kill 0)))) |
11 | 2651 |
2652 (defun ex-delete () | |
2653 "ex delete" | |
2654 (vip-default-ex-addresses) | |
2655 (vip-get-ex-buffer) | |
2656 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) | |
2657 (if (> beg end) (error "First address exceeds second")) | |
2658 (save-excursion | |
2659 (vip-enlarge-region beg end) | |
2660 (exchange-point-and-mark) | |
2661 (if ex-count | |
2662 (progn | |
2663 (set-mark (point)) | |
2664 (forward-line (1- ex-count))) | |
2665 (set-mark end)) | |
2666 (vip-enlarge-region (point) (mark)) | |
2667 (if ex-flag | |
2668 ;; show text to be deleted and ask for confirmation | |
2669 (progn | |
2670 (with-output-to-temp-buffer " *delete text*" | |
2671 (princ (buffer-substring (point) (mark)))) | |
2672 (condition-case conditions | |
2673 (vip-read-string "[Hit return to continue] ") | |
2674 (quit | |
2675 (save-excursion (kill-buffer " *delete text*")) | |
2676 (error ""))) | |
2677 (save-excursion (kill-buffer " *delete text*"))) | |
2678 (if ex-buffer | |
2679 (if (and (<= ?A ex-buffer) (<= ex-buffer ?Z)) | |
2680 (vip-append-to-register | |
1625
376427dea90a
* bytecomp.el: Declare unread-command-char an obsolete variable.
Jim Blandy <jimb@redhat.com>
parents:
1577
diff
changeset
|
2681 (+ ex-buffer 32) (point) (mark)) |
11 | 2682 (copy-to-register ex-buffer (point) (mark) nil))) |
2683 (delete-region (point) (mark)))))) | |
2684 | |
2685 (defun ex-edit () | |
2686 "ex-edit" | |
2687 (vip-get-ex-file) | |
2688 (if (and (not ex-variant) (buffer-modified-p) buffer-file-name) | |
2689 (error "No write since last change \(:e! overrides\)")) | |
2690 (vip-change-mode-to-emacs) | |
2691 (set-buffer | |
2692 (find-file-noselect (concat default-directory ex-file))) | |
2693 (vip-change-mode-to-vi) | |
2694 (goto-char (point-min)) | |
2695 (if ex-offset | |
2696 (progn | |
2697 (save-window-excursion | |
2698 (set-buffer " *ex-working-space*") | |
2699 (delete-region (point-min) (point-max)) | |
2700 (insert ex-offset "\n") | |
2701 (goto-char (point-min))) | |
2702 (goto-char (vip-get-ex-address)) | |
2703 (beginning-of-line)))) | |
2704 | |
2705 (defun ex-global (variant) | |
2706 "ex global command" | |
2707 (if (or ex-g-flag ex-g-variant) | |
2708 (error "Global within global not allowed") | |
2709 (if variant | |
2710 (setq ex-g-flag nil | |
2711 ex-g-variant t) | |
2712 (setq ex-g-flag t | |
2713 ex-g-variant nil))) | |
2714 (vip-get-ex-pat) | |
2715 (if (null ex-token) | |
2716 (error "Missing regular expression for global command")) | |
2717 (if (string= ex-token "") | |
2718 (if (null vip-s-string) (error "No previous search string") | |
2719 (setq ex-g-pat vip-s-string)) | |
2720 (setq ex-g-pat ex-token | |
2721 vip-s-string ex-token)) | |
2722 (if (null ex-addresses) | |
2723 (setq ex-addresses (list (point-max) (point-min)))) | |
2724 (let ((marks nil) (mark-count 0) | |
2725 com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses)))) | |
2726 (if (> beg end) (error "First address exceeds second")) | |
2727 (save-excursion | |
2728 (vip-enlarge-region beg end) | |
2729 (exchange-point-and-mark) | |
2730 (let ((cont t) (limit (point-marker))) | |
2731 (exchange-point-and-mark) | |
2732 ;; skip the last line if empty | |
2733 (beginning-of-line) | |
2734 (if (and (eobp) (not (bobp))) (backward-char 1)) | |
2735 (while (and cont (not (bobp)) (>= (point) limit)) | |
2736 (beginning-of-line) | |
2737 (set-mark (point)) | |
2738 (end-of-line) | |
2739 (let ((found (re-search-backward ex-g-pat (mark) t))) | |
2740 (if (or (and ex-g-flag found) | |
2741 (and ex-g-variant (not found))) | |
2742 (progn | |
2743 (end-of-line) | |
2744 (setq mark-count (1+ mark-count)) | |
2745 (setq marks (cons (point-marker) marks))))) | |
2746 (beginning-of-line) | |
2747 (if (bobp) (setq cont nil) | |
2748 (forward-line -1) | |
2749 (end-of-line))))) | |
2750 (save-window-excursion | |
2751 (set-buffer " *ex-working-space*") | |
2752 (setq com-str (buffer-substring (1+ (point)) (1- (point-max))))) | |
2753 (while marks | |
2754 (goto-char (car marks)) | |
2755 ; report progress of execution on a slow machine. | |
2756 ;(message "Executing global command...") | |
2757 ;(if (zerop (% mark-count 10)) | |
2758 ;(message "Executing global command...%d" mark-count)) | |
2759 (vip-ex com-str) | |
2760 (setq mark-count (1- mark-count)) | |
2761 (setq marks (cdr marks))))) | |
2762 ;(message "Executing global command...done"))) | |
2763 | |
2764 (defun ex-line (com) | |
2765 "ex line commands. COM is join, shift-right or shift-left." | |
2766 (vip-default-ex-addresses) | |
2767 (vip-get-ex-count) | |
2768 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point) | |
2769 (if (> beg end) (error "First address exceeds second")) | |
2770 (save-excursion | |
2771 (vip-enlarge-region beg end) | |
2772 (exchange-point-and-mark) | |
2773 (if ex-count | |
2774 (progn | |
2775 (set-mark (point)) | |
2776 (forward-line ex-count))) | |
2777 (if ex-flag | |
2778 ;; show text to be joined and ask for confirmation | |
2779 (progn | |
2780 (with-output-to-temp-buffer " *text*" | |
2781 (princ (buffer-substring (point) (mark)))) | |
2782 (condition-case conditions | |
2783 (progn | |
2784 (vip-read-string "[Hit return to continue] ") | |
2785 (ex-line-subr com (point) (mark))) | |
2786 (quit | |
2787 (ding))) | |
2788 (save-excursion (kill-buffer " *text*"))) | |
2789 (ex-line-subr com (point) (mark))) | |
2790 (setq point (point))) | |
2791 (goto-char (1- point)) | |
2792 (beginning-of-line))) | |
2793 | |
2794 (defun ex-line-subr (com beg end) | |
2795 (cond ((string= com "join") | |
2796 (goto-char (min beg end)) | |
2797 (while (and (not (eobp)) (< (point) (max beg end))) | |
2798 (end-of-line) | |
2799 (if (and (<= (point) (max beg end)) (not (eobp))) | |
2800 (progn | |
2801 (forward-line 1) | |
2802 (delete-region (point) (1- (point))) | |
2803 (if (not ex-variant) (fixup-whitespace)))))) | |
2804 ((or (string= com "right") (string= com "left")) | |
2805 (indent-rigidly | |
2806 (min beg end) (max beg end) | |
2807 (if (string= com "right") vip-shift-width (- vip-shift-width))) | |
2808 (goto-char (max beg end)) | |
2809 (end-of-line) | |
2810 (forward-char 1)))) | |
2811 | |
2812 (defun ex-mark () | |
2813 "ex mark" | |
2814 (let (char) | |
2815 (if (null ex-addresses) | |
2816 (setq ex-addresses | |
2817 (cons (point) nil))) | |
2818 (save-window-excursion | |
2819 (set-buffer " *ex-working-space*") | |
2820 (skip-chars-forward " \t") | |
2821 (if (looking-at "[a-z]") | |
2822 (progn | |
2823 (setq char (following-char)) | |
2824 (forward-char 1) | |
2825 (skip-chars-forward " \t") | |
2826 (if (not (looking-at "[\n|]")) | |
2827 (error "Extra characters at end of \"k\" command"))) | |
2828 (if (looking-at "[\n|]") | |
2829 (error "\"k\" requires a following letter") | |
2830 (error "Mark must specify a letter")))) | |
2831 (save-excursion | |
2832 (goto-char (car ex-addresses)) | |
1625
376427dea90a
* bytecomp.el: Declare unread-command-char an obsolete variable.
Jim Blandy <jimb@redhat.com>
parents:
1577
diff
changeset
|
2833 (point-to-register (- char (- ?a ?\C-a)) nil)))) |
11 | 2834 |
2835 (defun ex-map () | |
2836 "ex map" | |
2837 (let (char string) | |
2838 (save-window-excursion | |
2839 (set-buffer " *ex-working-space*") | |
2840 (skip-chars-forward " \t") | |
2841 (setq char (char-to-string (following-char))) | |
2842 (forward-char 1) | |
2843 (skip-chars-forward " \t") | |
2844 (if (looking-at "[\n|]") (error "Missing rhs")) | |
2845 (set-mark (point)) | |
2846 (end-of-buffer) | |
2847 (backward-char 1) | |
2848 (setq string (buffer-substring (mark) (point)))) | |
2849 (if (not (lookup-key ex-map char)) | |
2850 (define-key ex-map char | |
2851 (or (lookup-key vip-mode-map char) 'vip-nil))) | |
2852 (define-key vip-mode-map char | |
2853 (eval | |
2854 (list 'quote | |
2855 (cons 'lambda | |
2856 (list '(count) | |
2857 '(interactive "p") | |
2858 (list 'execute-kbd-macro string 'count)))))))) | |
2859 | |
2860 (defun ex-unmap () | |
2861 "ex unmap" | |
2862 (let (char) | |
2863 (save-window-excursion | |
2864 (set-buffer " *ex-working-space*") | |
2865 (skip-chars-forward " \t") | |
2866 (setq char (char-to-string (following-char))) | |
2867 (forward-char 1) | |
2868 (skip-chars-forward " \t") | |
2869 (if (not (looking-at "[\n|]")) (error "Macro must be a character"))) | |
2870 (if (not (lookup-key ex-map char)) | |
2871 (error "That macro wasn't mapped")) | |
2872 (define-key vip-mode-map char (lookup-key ex-map char)) | |
2873 (define-key ex-map char nil))) | |
2874 | |
2875 (defun ex-put () | |
2876 "ex put" | |
2877 (let ((point (if (null ex-addresses) (point) (car ex-addresses)))) | |
2878 (vip-get-ex-buffer) | |
2879 (setq vip-use-register ex-buffer) | |
2880 (goto-char point) | |
2881 (if (= point 0) (vip-Put-back 1) (vip-put-back 1)))) | |
2882 | |
2883 (defun ex-quit () | |
2884 "ex quit" | |
2885 (let (char) | |
2886 (save-window-excursion | |
2887 (set-buffer " *ex-working-space*") | |
2888 (skip-chars-forward " \t") | |
2889 (setq char (following-char))) | |
2890 (if (= char ?!) (kill-emacs t) (save-buffers-kill-emacs)))) | |
2891 | |
2892 (defun ex-read () | |
2893 "ex read" | |
2894 (let ((point (if (null ex-addresses) (point) (car ex-addresses))) | |
2895 (variant nil) command file) | |
2896 (goto-char point) | |
2897 (if (not (= point 0)) (next-line 1)) | |
2898 (beginning-of-line) | |
2899 (save-window-excursion | |
2900 (set-buffer " *ex-working-space*") | |
2901 (skip-chars-forward " \t") | |
2902 (if (looking-at "!") | |
2903 (progn | |
2904 (setq variant t) | |
2905 (forward-char 1) | |
2906 (skip-chars-forward " \t") | |
2907 (set-mark (point)) | |
2908 (end-of-line) | |
2909 (setq command (buffer-substring (mark) (point)))) | |
2910 (set-mark (point)) | |
2911 (re-search-forward "[ \t\n]") | |
2912 (backward-char 1) | |
2913 (setq file (buffer-substring (point) (mark))))) | |
2914 (if variant | |
2915 (shell-command command t) | |
2916 (insert-file file)))) | |
2917 | |
2918 (defun ex-set () | |
2919 (eval (list 'setq | |
2920 (read-variable "Variable: ") | |
2921 (eval (read-minibuffer "Value: "))))) | |
2922 | |
2923 (defun ex-shell () | |
2924 "ex shell" | |
2925 (vip-change-mode-to-emacs) | |
2926 (shell)) | |
2927 | |
2928 (defun ex-substitute (&optional repeat r-flag) | |
1577 | 2929 "ex substitute. |
2930 If REPEAT use previous reg-exp which is ex-reg-exp or | |
11 | 2931 vip-s-string" |
2932 (let (pat repl (opt-g nil) (opt-c nil) (matched-pos nil)) | |
2933 (if repeat (setq ex-token nil) (vip-get-ex-pat)) | |
2934 (if (null ex-token) | |
2935 (setq pat (if r-flag vip-s-string ex-reg-exp) | |
2936 repl ex-repl) | |
2937 (setq pat (if (string= ex-token "") vip-s-string ex-token)) | |
2938 (setq vip-s-string pat | |
2939 ex-reg-exp pat) | |
2940 (vip-get-ex-pat) | |
2941 (if (null ex-token) | |
2942 (setq ex-token "" | |
2943 ex-repl "") | |
2944 (setq repl ex-token | |
2945 ex-repl ex-token))) | |
2946 (while (vip-get-ex-opt-gc) | |
2947 (if (string= ex-token "g") (setq opt-g t) (setq opt-c t))) | |
2948 (vip-get-ex-count) | |
2949 (if ex-count | |
2950 (save-excursion | |
2951 (if ex-addresses (goto-char (car ex-addresses))) | |
2952 (set-mark (point)) | |
2953 (forward-line (1- ex-count)) | |
2954 (setq ex-addresses (cons (point) (cons (mark) nil)))) | |
2955 (if (null ex-addresses) | |
2956 (setq ex-addresses (cons (point) (cons (point) nil))) | |
2957 (if (null (cdr ex-addresses)) | |
2958 (setq ex-addresses (cons (car ex-addresses) ex-addresses))))) | |
2959 ;(setq G opt-g) | |
2960 (let ((beg (car ex-addresses)) (end (car (cdr ex-addresses))) | |
2961 (cont t) eol-mark) | |
2962 (save-excursion | |
2963 (vip-enlarge-region beg end) | |
2964 (let ((limit (save-excursion | |
2965 (goto-char (max (point) (mark))) | |
2966 (point-marker)))) | |
2967 (goto-char (min (point) (mark))) | |
2968 (while (< (point) limit) | |
2969 (end-of-line) | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
2970 (setq eol-mark (point-marker)) |
11 | 2971 (beginning-of-line) |
2972 (if opt-g | |
2973 (progn | |
2974 (while (and (not (eolp)) | |
2975 (re-search-forward pat eol-mark t)) | |
2976 (if (or (not opt-c) (y-or-n-p "Replace? ")) | |
2977 (progn | |
2978 (setq matched-pos (point)) | |
2979 (replace-match repl)))) | |
2980 (end-of-line) | |
2981 (forward-char)) | |
2982 (if (and (re-search-forward pat eol-mark t) | |
2983 (or (not opt-c) (y-or-n-p "Replace? "))) | |
2984 (progn | |
2985 (setq matched-pos (point)) | |
2986 (replace-match repl))) | |
2987 (end-of-line) | |
2988 (forward-char)))))) | |
2989 (if matched-pos (goto-char matched-pos)) | |
2990 (beginning-of-line) | |
2991 (if opt-c (message "done")))) | |
2992 | |
2993 (defun ex-tag () | |
2994 "ex tag" | |
2995 (let (tag) | |
2996 (save-window-excursion | |
2997 (set-buffer " *ex-working-space*") | |
2998 (skip-chars-forward " \t") | |
2999 (set-mark (point)) | |
3000 (skip-chars-forward "^ |\t\n") | |
3001 (setq tag (buffer-substring (mark) (point)))) | |
3002 (if (not (string= tag "")) (setq ex-tag tag)) | |
3003 (vip-change-mode-to-emacs) | |
3004 (condition-case conditions | |
3005 (progn | |
3006 (if (string= tag "") | |
3007 (find-tag ex-tag t) | |
3008 (find-tag-other-window ex-tag)) | |
3009 (vip-change-mode-to-vi)) | |
3010 (error | |
3011 (vip-change-mode-to-vi) | |
3012 (vip-message-conditions conditions))))) | |
3013 | |
3014 (defun ex-write (q-flag) | |
3015 "ex write" | |
3016 (vip-default-ex-addresses t) | |
3017 (vip-get-ex-file) | |
3018 (if (string= ex-file "") | |
3019 (progn | |
3020 (if (null buffer-file-name) | |
3021 (error "No file associated with this buffer")) | |
3022 (setq ex-file buffer-file-name)) | |
3023 (setq ex-file (expand-file-name ex-file))) | |
3024 (if (and (not (string= ex-file (buffer-file-name))) | |
3025 (file-exists-p ex-file) | |
3026 (not ex-variant)) | |
3027 (error (format "\"%s\" File exists - use w! to override" ex-file))) | |
3028 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) | |
3029 (if (> beg end) (error "First address exceeds second")) | |
3030 (save-excursion | |
3031 (vip-enlarge-region beg end) | |
3032 (write-region (point) (mark) ex-file ex-append t))) | |
3033 (if (null buffer-file-name) (setq buffer-file-name ex-file)) | |
3034 (if q-flag (save-buffers-kill-emacs))) | |
3035 | |
3036 (defun ex-yank () | |
3037 "ex yank" | |
3038 (vip-default-ex-addresses) | |
3039 (vip-get-ex-buffer) | |
3040 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) | |
3041 (if (> beg end) (error "First address exceeds second")) | |
3042 (save-excursion | |
3043 (vip-enlarge-region beg end) | |
3044 (exchange-point-and-mark) | |
3045 (if (or ex-g-flag ex-g-variant) (error "Can't yank within global")) | |
3046 (if ex-count | |
3047 (progn | |
3048 (set-mark (point)) | |
3049 (forward-line (1- ex-count))) | |
3050 (set-mark end)) | |
3051 (vip-enlarge-region (point) (mark)) | |
3052 (if ex-flag (error "Extra chacters at end of command")) | |
3053 (if ex-buffer | |
3054 (copy-to-register ex-buffer (point) (mark) nil)) | |
3055 (copy-region-as-kill (point) (mark))))) | |
3056 | |
3057 (defun ex-command () | |
3058 "execute shell command" | |
3059 (let (command) | |
3060 (save-window-excursion | |
3061 (set-buffer " *ex-working-space*") | |
3062 (skip-chars-forward " \t") | |
3063 (set-mark (point)) | |
3064 (end-of-line) | |
3065 (setq command (buffer-substring (mark) (point)))) | |
3066 (if (null ex-addresses) | |
3067 (shell-command command) | |
3068 (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) | |
3069 (if (null beg) (setq beg end)) | |
3070 (save-excursion | |
3071 (goto-char beg) | |
3072 (set-mark end) | |
3073 (vip-enlarge-region (point) (mark)) | |
3074 (shell-command-on-region (point) (mark) command t)) | |
3075 (goto-char beg))))) | |
3076 | |
3077 (defun ex-line-no () | |
3078 "print line number" | |
3079 (message "%d" | |
3080 (1+ (count-lines | |
3081 (point-min) | |
3082 (if (null ex-addresses) (point-max) (car ex-addresses)))))) | |
3083 | |
3084 (if (file-exists-p "~/.vip") (load "~/.vip")) | |
3085 | |
773
9c89fd7ddd41
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
258
diff
changeset
|
3086 ;;; vip.el ends here |