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