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