1 ;;; vip.el --- a VI Package for GNU Emacs
3 ;; Copyright (C) 1986-1988, 1992-1993, 1998, 2001-2014
4 ;; Free Software Foundation, Inc.
6 ;; Author: Masahiko Sato <ms@sail.stanford.edu>
7 ;; Keywords: emulations
8 ;; Obsolete-since: 24.5
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is obsolete. Consider using viper instead.
29 ;; A full-featured vi(1) emulator.
31 ;; In Japan, the author's address is: masahiko@sato.riec.tohoku.junet
33 ;; Send suggestions and bug reports to one of the above addresses.
34 ;; When you report a bug, be sure to include the version number of VIP and
35 ;; Emacs you are using.
37 ;; Execute info command by typing "M-x info" to get information on VIP.
42 "A VI Package for GNU Emacs."
48 (defvar vip-emacs-local-map nil
49 "Local map used in Emacs mode. (Buffer-specific.)")
51 (defvar vip-insert-local-map nil
52 "Local map used in insert command mode. (Buffer-specific.)")
54 (make-variable-buffer-local 'vip-emacs-local-map
)
55 (make-variable-buffer-local 'vip-insert-local-map
)
57 (defvar vip-insert-point nil
58 "Remember insert point as a marker. (Buffer-specific.)")
60 (set-default 'vip-insert-point
(make-marker))
61 (make-variable-buffer-local 'vip-insert-point
)
63 (defvar vip-com-point nil
64 "Remember com point as a marker. (Buffer-specific.)")
66 (set-default 'vip-com-point
(make-marker))
67 (make-variable-buffer-local 'vip-com-point
)
69 (defvar vip-current-mode nil
70 "Current mode. One of `emacs-mode', `vi-mode', `insert-mode'.")
72 (make-variable-buffer-local 'vip-current-mode
)
73 (setq-default vip-current-mode
'emacs-mode
)
75 (defvar vip-emacs-mode-line-buffer-identification nil
76 "Value of mode-line-buffer-identification in Emacs mode within vip.")
77 (make-variable-buffer-local 'vip-emacs-mode-line-buffer-identification
)
78 (setq-default vip-emacs-mode-line-buffer-identification
81 (defvar vip-current-major-mode nil
82 "vip-current-major-mode is the major-mode vi considers it is now.
85 (make-variable-buffer-local 'vip-current-major-mode
)
87 (defvar vip-last-shell-com nil
88 "Last shell command executed by ! command.")
90 (defvar vip-use-register nil
91 "Name of register to store deleted or yanked strings.")
94 "How to reexecute last destructive command. Value is list (M-COM VAL COM).")
96 (defcustom vip-shift-width
8
97 "The number of columns shifted by > and < command."
101 (defcustom vip-re-replace nil
102 "If t then do regexp replace, if nil then do string replace."
106 (defvar vip-d-char nil
107 "The character remembered by the vi \"r\" command.")
109 (defvar vip-f-char nil
110 "For use by \";\" command.")
112 (defvar vip-F-char nil
113 "For use by \".\" command.")
115 (defvar vip-f-forward nil
116 "For use by \";\" command.")
118 (defvar vip-f-offset nil
119 "For use by \";\" command.")
121 (defcustom vip-search-wrap-around t
122 "If t, search wraps around."
126 (defcustom vip-re-search nil
127 "If t, search is reg-exp search, otherwise vanilla search."
131 (defvar vip-s-string nil
132 "Last vip search string.")
134 (defvar vip-s-forward nil
135 "If t, search is forward.")
137 (defcustom vip-case-fold-search nil
138 "If t, search ignores cases."
142 (defcustom vip-re-query-replace nil
143 "If t then do regexp replace, if nil then do string replace."
147 (defcustom vip-open-with-indent nil
148 "If t, indent when open a new line."
152 (defcustom vip-help-in-insert-mode nil
153 "If t then C-h is bound to help-command in insert mode.
154 If nil then it is bound to `delete-backward-char'."
158 (defvar vip-quote-string
"> "
159 "String inserted at the beginning of region.")
161 (defvar vip-tags-file-name
"TAGS")
163 (defvar vip-inhibit-startup-message nil
)
165 (defvar vip-startup-file
(locate-user-emacs-file "vip" ".vip")
166 "Filename used as startup file for vip.")
171 (let ((map (make-keymap)))
172 (define-key map
"\C-a" 'beginning-of-line
)
173 (define-key map
"\C-b" 'vip-scroll-back
)
174 (define-key map
"\C-c" 'vip-ctl-c
)
175 (define-key map
"\C-d" 'vip-scroll-up
)
176 (define-key map
"\C-e" 'vip-scroll-up-one
)
177 (define-key map
"\C-f" 'vip-scroll
)
178 (define-key map
"\C-g" 'vip-keyboard-quit
)
179 (define-key map
"\C-h" 'help-command
)
180 (define-key map
"\C-m" 'vip-scroll-back
)
181 (define-key map
"\C-n" 'vip-other-window
)
182 (define-key map
"\C-o" 'vip-open-line-at-point
)
183 (define-key map
"\C-u" 'vip-scroll-down
)
184 (define-key map
"\C-x" 'vip-ctl-x
)
185 (define-key map
"\C-y" 'vip-scroll-down-one
)
186 (define-key map
"\C-z" 'vip-change-mode-to-emacs
)
187 (define-key map
"\e" 'vip-ESC
)
189 (define-key map
[?\S-\
] 'vip-scroll-back
)
190 (define-key map
" " 'vip-scroll
)
191 (define-key map
"!" 'vip-command-argument
)
192 (define-key map
"\"" 'vip-command-argument
)
193 (define-key map
"#" 'vip-command-argument
)
194 (define-key map
"$" 'vip-goto-eol
)
195 (define-key map
"%" 'vip-paren-match
)
196 (define-key map
"&" 'vip-nil
)
197 (define-key map
"'" 'vip-goto-mark-and-skip-white
)
198 (define-key map
"(" 'vip-backward-sentence
)
199 (define-key map
")" 'vip-forward-sentence
)
200 (define-key map
"*" 'call-last-kbd-macro
)
201 (define-key map
"+" 'vip-next-line-at-bol
)
202 (define-key map
"," 'vip-repeat-find-opposite
)
203 (define-key map
"-" 'vip-previous-line-at-bol
)
204 (define-key map
"." 'vip-repeat
)
205 (define-key map
"/" 'vip-search-forward
)
207 (define-key map
"0" 'vip-beginning-of-line
)
208 (define-key map
"1" 'vip-digit-argument
)
209 (define-key map
"2" 'vip-digit-argument
)
210 (define-key map
"3" 'vip-digit-argument
)
211 (define-key map
"4" 'vip-digit-argument
)
212 (define-key map
"5" 'vip-digit-argument
)
213 (define-key map
"6" 'vip-digit-argument
)
214 (define-key map
"7" 'vip-digit-argument
)
215 (define-key map
"8" 'vip-digit-argument
)
216 (define-key map
"9" 'vip-digit-argument
)
218 (define-key map
":" 'vip-ex
)
219 (define-key map
";" 'vip-repeat-find
)
220 (define-key map
"<" 'vip-command-argument
)
221 (define-key map
"=" 'vip-command-argument
)
222 (define-key map
">" 'vip-command-argument
)
223 (define-key map
"?" 'vip-search-backward
)
224 (define-key map
"@" 'vip-nil
)
226 (define-key map
"A" 'vip-Append
)
227 (define-key map
"B" 'vip-backward-Word
)
228 (define-key map
"C" 'vip-ctl-c-equivalent
)
229 (define-key map
"D" 'vip-kill-line
)
230 (define-key map
"E" 'vip-end-of-Word
)
231 (define-key map
"F" 'vip-find-char-backward
)
232 (define-key map
"G" 'vip-goto-line
)
233 (define-key map
"H" 'vip-window-top
)
234 (define-key map
"I" 'vip-Insert
)
235 (define-key map
"J" 'vip-join-lines
)
236 (define-key map
"K" 'vip-kill-buffer
)
237 (define-key map
"L" 'vip-window-bottom
)
238 (define-key map
"M" 'vip-window-middle
)
239 (define-key map
"N" 'vip-search-Next
)
240 (define-key map
"O" 'vip-Open-line
)
241 (define-key map
"P" 'vip-Put-back
)
242 (define-key map
"Q" 'vip-query-replace
)
243 (define-key map
"R" 'vip-replace-string
)
244 (define-key map
"S" 'vip-switch-to-buffer-other-window
)
245 (define-key map
"T" 'vip-goto-char-backward
)
246 (define-key map
"U" 'vip-nil
)
247 (define-key map
"V" 'vip-find-file-other-window
)
248 (define-key map
"W" 'vip-forward-Word
)
249 (define-key map
"X" 'vip-ctl-x-equivalent
)
250 (define-key map
"Y" 'vip-yank-line
)
251 (define-key map
"ZZ" 'save-buffers-kill-emacs
)
253 (define-key map
"[" 'vip-nil
)
254 (define-key map
"\\" 'vip-escape-to-emacs
)
255 (define-key map
"]" 'vip-nil
)
256 (define-key map
"^" 'vip-bol-and-skip-white
)
257 (define-key map
"_" 'vip-nil
)
258 (define-key map
"`" 'vip-goto-mark
)
260 (define-key map
"a" 'vip-append
)
261 (define-key map
"b" 'vip-backward-word
)
262 (define-key map
"c" 'vip-command-argument
)
263 (define-key map
"d" 'vip-command-argument
)
264 (define-key map
"e" 'vip-end-of-word
)
265 (define-key map
"f" 'vip-find-char-forward
)
266 (define-key map
"g" 'vip-info-on-file
)
267 (define-key map
"h" 'vip-backward-char
)
268 (define-key map
"i" 'vip-insert
)
269 (define-key map
"j" 'vip-next-line
)
270 (define-key map
"k" 'vip-previous-line
)
271 (define-key map
"l" 'vip-forward-char
)
272 (define-key map
"m" 'vip-mark-point
)
273 (define-key map
"n" 'vip-search-next
)
274 (define-key map
"o" 'vip-open-line
)
275 (define-key map
"p" 'vip-put-back
)
276 (define-key map
"q" 'vip-nil
)
277 (define-key map
"r" 'vip-replace-char
)
278 (define-key map
"s" 'vip-switch-to-buffer
)
279 (define-key map
"t" 'vip-goto-char-forward
)
280 (define-key map
"u" 'vip-undo
)
281 (define-key map
"v" 'vip-find-file
)
282 (define-key map
"w" 'vip-forward-word
)
283 (define-key map
"x" 'vip-delete-char
)
284 (define-key map
"y" 'vip-command-argument
)
285 (define-key map
"zH" 'vip-line-to-top
)
286 (define-key map
"zM" 'vip-line-to-middle
)
287 (define-key map
"zL" 'vip-line-to-bottom
)
288 (define-key map
"z\C-m" 'vip-line-to-top
)
289 (define-key map
"z." 'vip-line-to-middle
)
290 (define-key map
"z-" 'vip-line-to-bottom
)
292 (define-key map
"{" 'vip-backward-paragraph
)
293 (define-key map
"|" 'vip-goto-col
)
294 (define-key map
"}" 'vip-forward-paragraph
)
295 (define-key map
"~" 'vip-nil
)
296 (define-key map
"\177" 'vip-delete-backward-char
)
299 (defun vip-version ()
301 (message "VIP version 3.5 of September 15, 1987"))
308 "Set up bindings for C-x 7 and C-z that are useful for VIP users."
309 (define-key ctl-x-map
"7" 'vip-buffer-in-two-windows
)
310 (global-set-key "\C-z" 'vip-change-mode-to-vi
))
312 (defmacro vip-loop
(count body
)
313 "(COUNT BODY) Execute BODY COUNT times."
314 `(let ((count ,count
))
317 (setq count
(1- count
)))))
319 (defun vip-push-mark-silent (&optional location
)
320 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
324 (setq mark-ring
(cons (copy-marker (mark-marker)) mark-ring
))
325 (if (> (length mark-ring
) mark-ring-max
)
327 (move-marker (car (nthcdr mark-ring-max mark-ring
)) nil
)
328 (setcdr (nthcdr (1- mark-ring-max
) mark-ring
) nil
))))
329 (set-mark (or location
(point))))
331 (defun vip-goto-col (arg)
332 "Go to ARG's column."
334 (let ((val (vip-p-val arg
))
335 (com (vip-getcom arg
)))
338 (if (> val
(1+ (current-column))) (error "")))
339 (if com
(move-marker vip-com-point
(point)))
341 (forward-char (1- val
))
342 (if com
(vip-execute-com 'vip-goto-col val com
))))
344 (defun vip-copy-keymap (map)
345 (if (null map
) (make-sparse-keymap) (copy-keymap map
)))
350 (defun vip-change-mode (new-mode)
351 "Change mode to NEW-MODE---either emacs-mode, vi-mode, or insert-mode."
352 (or (eq new-mode vip-current-mode
)
354 (cond ((eq new-mode
'vi-mode
)
355 (if (eq vip-current-mode
'insert-mode
)
357 (vip-copy-region-as-kill (point) vip-insert-point
)
358 (vip-repeat-insert-command))
359 (setq vip-emacs-local-map
(current-local-map)
360 vip-emacs-mode-line-buffer-identification
361 mode-line-buffer-identification
362 vip-insert-local-map
(vip-copy-keymap
363 (current-local-map))))
364 (vip-change-mode-line "Vi: ")
365 (use-local-map vip-mode-map
))
366 ((eq new-mode
'insert-mode
)
367 (move-marker vip-insert-point
(point))
368 (if (eq vip-current-mode
'emacs-mode
)
369 (setq vip-emacs-local-map
(current-local-map)
370 vip-emacs-mode-line-buffer-identification
371 mode-line-buffer-identification
372 vip-insert-local-map
(vip-copy-keymap
373 (current-local-map)))
374 (setq vip-insert-local-map
(vip-copy-keymap
375 vip-emacs-local-map
)))
376 (vip-change-mode-line "Insert")
377 (use-local-map vip-insert-local-map
)
378 (define-key vip-insert-local-map
"\e" 'vip-change-mode-to-vi
)
379 (define-key vip-insert-local-map
"\C-z" 'vip-ESC
)
380 (define-key vip-insert-local-map
"\C-h"
381 (if vip-help-in-insert-mode
'help-command
382 'delete-backward-char
))
383 (define-key vip-insert-local-map
"\C-w"
384 'vip-delete-backward-word
))
385 ((eq new-mode
'emacs-mode
)
386 (vip-change-mode-line "Emacs:")
387 (use-local-map vip-emacs-local-map
)))
388 (setq vip-current-mode new-mode
)
389 (force-mode-line-update))))
391 (defun vip-copy-region-as-kill (beg end
)
392 "If BEG and END do not belong to the same buffer, it copies empty region."
394 (copy-region-as-kill beg end
)
395 (error (copy-region-as-kill beg beg
))))
397 (defun vip-change-mode-line (string)
398 "Assuming that the mode line format contains the string \"Emacs:\", this
399 function replaces the string by \"Vi: \" etc."
400 (setq mode-line-buffer-identification
401 (if (string= string
"Emacs:")
402 vip-emacs-mode-line-buffer-identification
403 (list (concat string
" %17b")))))
407 "Turn on VIP emulation of VI."
409 (if (not vip-inhibit-startup-message
)
411 (switch-to-buffer "VIP Startup Message")
414 "VIP is a Vi emulation package for GNU Emacs. VIP provides most Vi commands
415 including Ex commands. VIP is however different from Vi in several points.
416 You can get more information on VIP by:
417 1. Typing `M-x info' and selecting menu item \"vip\".
418 2. Typing `C-h k' followed by a key whose description you want.
419 3. Printing VIP manual which can be found as GNU/man/vip.texinfo
420 4. Printing VIP Reference Card which can be found as GNU/etc/vipcard.tex
422 This startup message appears whenever you load VIP unless you type `y' now.
423 Type `n' to quit this window for now.\n")
424 (goto-char (point-min))
425 (if (y-or-n-p "Inhibit VIP startup message? ")
429 (substitute-in-file-name vip-startup-file
))
430 (goto-char (point-max))
431 (insert "\n(setq vip-inhibit-startup-message t)\n")
433 (kill-buffer (current-buffer)))
434 (message "VIP startup message inhibited.")
436 (kill-buffer (current-buffer))
438 (setq vip-inhibit-startup-message t
)))
439 (vip-change-mode-to-vi))
441 (defun vip-change-mode-to-vi ()
442 "Change mode to vi mode."
444 (vip-change-mode 'vi-mode
))
446 (defun vip-change-mode-to-insert ()
447 "Change mode to insert mode."
449 (vip-change-mode 'insert-mode
))
451 (defun vip-change-mode-to-emacs ()
452 "Change mode to Emacs mode."
454 (vip-change-mode 'emacs-mode
))
457 ;; escape to emacs mode temporarily
459 (defun vip-escape-to-emacs (arg &optional events
)
460 "Escape to Emacs mode for one Emacs command.
461 ARG is used as the prefix value for the executed command. If
462 EVENTS is a list of events, which become the beginning of the command."
464 (let (com key
(old-map (current-local-map)))
465 (if events
(setq unread-command-events events
))
466 (setq prefix-arg arg
)
467 (use-local-map vip-emacs-local-map
)
469 (setq com
(key-binding (setq key
(read-key-sequence nil
))))
470 (use-local-map old-map
))
471 (command-execute com prefix-arg
)
472 (setq prefix-arg nil
) ;; reset prefix arg
475 (defun vip-message-conditions (conditions)
476 "Print CONDITIONS as a message."
477 (let ((case (car conditions
)) (msg (cdr conditions
)))
480 (message "%s %s" case
(prin1-to-string msg
)))
484 "Emulate ESC key in Emacs mode."
486 (vip-escape-to-emacs arg
'(?\e
)))
488 (defun vip-ctl-c (arg)
489 "Emulate C-c key in Emacs mode."
491 (vip-escape-to-emacs arg
'(?\C-c
)))
493 (defun vip-ctl-x (arg)
494 "Emulate C-x key in Emacs mode."
496 (vip-escape-to-emacs arg
'(?\C-x
)))
498 (defun vip-ctl-h (arg)
499 "Emulate C-h key in Emacs mode."
501 (vip-escape-to-emacs arg
'(?\C-h
)))
504 ;; prefix argument for vi mode
506 ;; In vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
507 ;; represents the numeric value of the prefix argument and COM represents
508 ;; command prefix such as "c", "d", "m" and "y".
510 (defun vip-prefix-arg-value (char value com
)
511 "Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value
512 obtained so far, and COM is the command part obtained so far."
513 (while (and (>= char ?
0) (<= char ?
9))
514 (setq value
(+ (* (if (numberp value
) value
0) 10) (- char ?
0)))
515 (setq char
(read-char)))
516 (setq prefix-arg value
)
517 (if com
(setq prefix-arg
(cons prefix-arg com
)))
519 (vip-describe-arg prefix-arg
)
520 (setq char
(read-char)))
521 (setq unread-command-events
(list char
)))
523 (defun vip-prefix-arg-com (char value com
)
524 "Vi operator as prefix argument."
527 (or (= char ?c
) (= char ?d
) (= char ?y
)
528 (= char ?
!) (= char ?
<) (= char ?
>) (= char ?
=)
529 (= char ?
#) (= char ?r
) (= char ?R
) (= char ?
\")))
531 ;; this means that we already have a command character, so we
532 ;; construct a com list and exit while. however, if char is "
535 ;; new com is (CHAR . OLDCOM)
536 (if (or (= char ?
#) (= char ?
\")) (error ""))
537 (setq com
(cons char com
))
539 ;; if com is nil we set com as char, and read more. again, if char
540 ;; is ", we read the name of register and store it in vip-use-register.
541 ;; if char is !, =, or #, a complete com is formed so we exit while.
542 (cond ((or (= char ?
!) (= char ?
=))
544 (setq char
(read-char))
547 ;; read a char and encode it as com
548 (setq com
(+ 128 (read-char)))
549 (setq char
(read-char))
551 ((or (= char ?
<) (= char ?
>))
553 (setq char
(read-char))
554 (if (= com char
) (setq com
(cons char com
)))
557 (let ((reg (read-char)))
558 (if (or (and (<= ?A reg
) (<= reg ?z
))
559 (and (<= ?
1 reg
) (<= reg ?
9)))
560 (setq vip-use-register reg
)
562 (setq char
(read-char))))
565 (setq char
(read-char)))))))
567 ;; com is a single char, so we construct prefix-arg
568 ;; and if char is ?, describe prefix arg, otherwise exit by
569 ;; pushing the char back
571 (setq prefix-arg
(cons value com
))
573 (vip-describe-arg prefix-arg
)
574 (setq char
(read-char)))
575 (setq unread-command-events
(list char
)))
576 ;; as com is non-nil, this means that we have a command to execute
577 (if (or (= (car com
) ?r
) (= (car com
) ?R
))
578 ;; execute appropriate region command.
579 (let ((char (car com
)) (com (cdr com
)))
580 (setq prefix-arg
(cons value com
))
581 (if (= char ?r
) (vip-region prefix-arg
)
582 (vip-Region prefix-arg
))
584 (setq prefix-arg nil
))
585 ;; otherwise, reset prefix arg and call appropriate command
586 (setq value
(if (null value
) 1 value
))
587 (setq prefix-arg nil
)
588 (cond ((equal com
'(?c . ?c
)) (vip-line (cons value ?C
)))
589 ((equal com
'(?d . ?d
)) (vip-line (cons value ?D
)))
590 ((equal com
'(?d . ?y
)) (vip-yank-defun))
591 ((equal com
'(?y . ?y
)) (vip-line (cons value ?Y
)))
592 ((equal com
'(?
< . ?
<)) (vip-line (cons value ?
<)))
593 ((equal com
'(?
> . ?
>)) (vip-line (cons value ?
>)))
594 ((equal com
'(?
! . ?
!)) (vip-line (cons value ?
!)))
595 ((equal com
'(?
= . ?
=)) (vip-line (cons value ?
=)))
598 (defun vip-describe-arg (arg)
600 (setq val
(vip-P-val arg
)
601 com
(vip-getcom arg
))
604 (message "Value is nil, and command is nil.")
605 (message "Value is nil, and command is %c." com
))
607 (message "Value is %d, and command is nil." val
)
608 (message "Value is %d, and command is %c." val com
)))))
610 (defun vip-digit-argument (arg)
611 "Begin numeric argument for the next command."
613 (vip-prefix-arg-value last-command-event nil
614 (if (consp arg
) (cdr arg
) nil
)))
616 (defun vip-command-argument (arg)
617 "Accept a motion command as an argument."
619 (condition-case conditions
622 (cond ((null arg
) nil
)
623 ((consp arg
) (car arg
))
625 (t (error "strange arg")))
626 (cond ((null arg
) nil
)
627 ((consp arg
) (cdr arg
))
629 (t (error "strange arg"))))
631 (setq vip-use-register nil
)
632 (signal 'quit nil
))))
634 (defun vip-p-val (arg)
635 "Get value part of prefix-argument ARG."
637 ((consp arg
) (if (null (car arg
)) 1 (car arg
)))
640 (defun vip-P-val (arg)
641 "Get value part of prefix-argument ARG."
642 (cond ((consp arg
) (car arg
))
645 (defun vip-getcom (arg)
646 "Get com part of prefix-argument ARG."
647 (cond ((null arg
) nil
)
648 ((consp arg
) (cdr arg
))
651 (defun vip-getCom (arg)
652 "Get com part of prefix-argument ARG and modify it."
653 (let ((com (vip-getcom arg
)))
654 (cond ((equal com ?c
) ?C
)
660 ;; repeat last destructive command
662 (defun vip-append-to-register (reg start end
)
663 "Append region to text in register REG.
664 START and END are buffer positions indicating what to append."
665 (set-register reg
(concat (or (get-register reg
) "")
666 (buffer-substring start end
))))
668 (defun vip-execute-com (m-com val com
)
669 "(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set
670 to vip-d-com for later use by vip-repeat"
671 (let ((reg vip-use-register
))
673 (cond ((= com ?c
) (vip-change vip-com-point
(point)))
674 ((= com
(- ?c
)) (vip-change-subr vip-com-point
(point)))
675 ((or (= com ?C
) (= com
(- ?C
)))
677 (set-mark vip-com-point
)
678 (vip-enlarge-region (mark) (point))
681 (cond ((and (<= ?a vip-use-register
)
682 (<= vip-use-register ?z
))
684 vip-use-register
(mark) (point) nil
))
685 ((and (<= ?A vip-use-register
)
686 (<= vip-use-register ?Z
))
687 (vip-append-to-register
688 (+ vip-use-register
32) (mark) (point)))
689 (t (setq vip-use-register nil
)
691 (setq vip-use-register nil
)))
692 (delete-region (mark) (point)))
694 (if (= com ?C
) (vip-change-mode-to-insert) (yank)))
698 (cond ((and (<= ?a vip-use-register
)
699 (<= vip-use-register ?z
))
701 vip-use-register vip-com-point
(point) nil
))
702 ((and (<= ?A vip-use-register
)
703 (<= vip-use-register ?Z
))
704 (vip-append-to-register
705 (+ vip-use-register
32) vip-com-point
(point)))
706 (t (setq vip-use-register nil
)
708 (setq vip-use-register nil
)))
710 (if (eq last-command
'd-command
) 'kill-region nil
))
711 (kill-region vip-com-point
(point))
712 (setq this-command
'd-command
))
715 (set-mark vip-com-point
)
716 (vip-enlarge-region (mark) (point))
719 (cond ((and (<= ?a vip-use-register
)
720 (<= vip-use-register ?z
))
722 vip-use-register
(mark) (point) nil
))
723 ((and (<= ?A vip-use-register
)
724 (<= vip-use-register ?Z
))
725 (vip-append-to-register
726 (+ vip-use-register
32) (mark) (point)))
727 (t (setq vip-use-register nil
)
729 (setq vip-use-register nil
)))
731 (if (eq last-command
'D-command
) 'kill-region nil
))
732 (kill-region (mark) (point))
733 (if (eq m-com
'vip-line
) (setq this-command
'D-command
)))
734 (back-to-indentation))
738 (cond ((and (<= ?a vip-use-register
)
739 (<= vip-use-register ?z
))
741 vip-use-register vip-com-point
(point) nil
))
742 ((and (<= ?A vip-use-register
)
743 (<= vip-use-register ?Z
))
744 (vip-append-to-register
745 (+ vip-use-register
32) vip-com-point
(point)))
746 (t (setq vip-use-register nil
)
748 (setq vip-use-register nil
)))
749 (setq last-command nil
)
750 (copy-region-as-kill vip-com-point
(point))
751 (goto-char vip-com-point
))
754 (set-mark vip-com-point
)
755 (vip-enlarge-region (mark) (point))
758 (cond ((and (<= ?a vip-use-register
)
759 (<= vip-use-register ?z
))
761 vip-use-register
(mark) (point) nil
))
762 ((and (<= ?A vip-use-register
)
763 (<= vip-use-register ?Z
))
764 (vip-append-to-register
765 (+ vip-use-register
32) (mark) (point)))
766 (t (setq vip-use-register nil
)
768 (setq vip-use-register nil
)))
769 (setq last-command nil
)
770 (copy-region-as-kill (mark) (point)))
771 (goto-char vip-com-point
))
772 ((or (= com ?
!) (= com
(- ?
!)))
774 (set-mark vip-com-point
)
775 (vip-enlarge-region (mark) (point))
776 (shell-command-on-region
779 (setq vip-last-shell-com
(vip-read-string "!"))
784 (set-mark vip-com-point
)
785 (vip-enlarge-region (mark) (point))
786 (if (> (mark) (point)) (exchange-point-and-mark))
787 (indent-region (mark) (point) nil
)))
790 (set-mark vip-com-point
)
791 (vip-enlarge-region (mark) (point))
792 (indent-rigidly (mark) (point) (- vip-shift-width
)))
793 (goto-char vip-com-point
))
796 (set-mark vip-com-point
)
797 (vip-enlarge-region (mark) (point))
798 (indent-rigidly (mark) (point) vip-shift-width
))
799 (goto-char vip-com-point
))
801 ;; this is special command #
802 (vip-special-prefix-com (- com
128)))))
803 (setq vip-d-com
(list m-com val
(if (or (= com ?c
) (= com ?C
) (= com ?
!))
807 (defun vip-repeat (arg)
808 "(ARG) Re-execute last destructive command. vip-d-com has the form
809 \(COM ARG CH REG), where COM is the command to be re-executed, ARG is the
810 argument for COM, CH is a flag for repeat, and REG is optional and if exists
811 is the name of the register for COM."
813 (if (eq last-command
'vip-undo
)
814 ;; if the last command was vip-undo, then undo-more
816 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
817 ;; its prefix value is used as new prefix value for the command.
818 (let ((m-com (car vip-d-com
))
819 (val (vip-P-val arg
))
820 (com (car (cdr (cdr vip-d-com
))))
821 (reg (nth 3 vip-d-com
)))
822 (if (null val
) (setq val
(car (cdr vip-d-com
))))
823 (if (null m-com
) (error "No previous command to repeat"))
824 (setq vip-use-register reg
)
825 (funcall m-com
(cons val com
)))))
827 (defun vip-special-prefix-com (char)
828 "This command is invoked interactively by the key sequence #<char>"
830 (downcase-region (min vip-com-point
(point))
831 (max vip-com-point
(point))))
833 (upcase-region (min vip-com-point
(point))
834 (max vip-com-point
(point))))
836 (set-mark vip-com-point
)
837 (vip-global-execute))
839 (set-mark vip-com-point
)
841 ((= char ?s
) (ispell-region vip-com-point
(point)))))
847 "Undo previous change."
852 (setq this-command
'vip-undo
))
854 (defun vip-undo-more ()
855 "Continue undoing previous changes."
856 (message "undo more!")
858 (setq this-command
'vip-undo
))
863 (defun vip-string-tail (str)
864 (if (or (null str
) (string= str
"")) nil
867 (defun vip-yank-defun ()
869 (copy-region-as-kill (point) (mark)))
871 (defun vip-enlarge-region (beg end
)
872 "Enlarge region between BEG and END."
874 (progn (goto-char beg
) (set-mark end
))
878 (exchange-point-and-mark)
879 (if (or (not (eobp)) (not (bolp))) (with-no-warnings (next-line 1)))
881 (if (> beg end
) (exchange-point-and-mark)))
883 (defun vip-global-execute ()
884 "Call last keyboard macro for each line in the region."
885 (if (> (point) (mark)) (exchange-point-and-mark))
887 (call-last-kbd-macro)
888 (while (< (point) (mark))
891 (call-last-kbd-macro)))
893 (defun vip-quote-region ()
894 "Quote region by inserting the user supplied string at the beginning of
895 each line in the region."
896 (setq vip-quote-string
898 (vip-read-string (format "quote string (default %s): "
900 (if (string= str
"") vip-quote-string str
)))
901 (vip-enlarge-region (point) (mark))
902 (if (> (point) (mark)) (exchange-point-and-mark))
903 (insert vip-quote-string
)
906 (while (and (< (point) (mark)) (bolp))
907 (insert vip-quote-string
)
911 (defun vip-end-with-a-newline-p (string)
912 "Check if the string ends with a newline."
913 (or (string= string
"")
914 (= (aref string
(1- (length string
))) ?
\n)))
916 (defvar vip-save-minibuffer-local-map
)
918 (defun vip-read-string (prompt &optional init
)
919 (setq vip-save-minibuffer-local-map
(copy-keymap minibuffer-local-map
))
920 (define-key minibuffer-local-map
"\C-h" 'backward-char
)
921 (define-key minibuffer-local-map
"\C-w" 'backward-word
)
922 (define-key minibuffer-local-map
"\e" 'exit-minibuffer
)
924 (condition-case conditions
925 (setq str
(read-string prompt init
))
927 (setq minibuffer-local-map vip-save-minibuffer-local-map
)
929 (setq minibuffer-local-map vip-save-minibuffer-local-map
)
933 ;; insertion commands
935 (defun vip-repeat-insert-command ()
936 "This function is called when mode changes from insertion mode to
937 vi command mode. It will repeat the insertion command if original insertion
938 command was invoked with argument > 1."
939 (let ((i-com (car vip-d-com
)) (val (car (cdr vip-d-com
))))
940 (if (and val
(> val
1)) ;; first check that val is non-nil
942 (setq vip-d-com
(list i-com
(1- val
) ?r
))
944 (setq vip-d-com
(list i-com val ?r
))))))
946 (defun vip-insert (arg) ""
948 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
949 (setq vip-d-com
(list 'vip-insert val ?r
))
950 (if com
(vip-loop val
(yank))
951 (vip-change-mode-to-insert))))
953 (defun vip-append (arg)
954 "Append after point."
956 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
957 (setq vip-d-com
(list 'vip-append val ?r
))
958 (if (not (eolp)) (forward-char))
960 (vip-loop val
(yank))
961 (vip-change-mode-to-insert))))
963 (defun vip-Append (arg)
964 "Append at end of line."
966 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
967 (setq vip-d-com
(list 'vip-Append val ?r
))
970 (vip-loop val
(yank))
971 (vip-change-mode-to-insert))))
973 (defun vip-Insert (arg)
974 "Insert before first non-white."
976 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
977 (setq vip-d-com
(list 'vip-Insert val ?r
))
978 (back-to-indentation)
980 (vip-loop val
(yank))
981 (vip-change-mode-to-insert))))
983 (defun vip-open-line (arg)
986 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
987 (setq vip-d-com
(list 'vip-open-line val ?r
))
988 (let ((col (current-indentation)))
994 (if vip-open-with-indent
(indent-to col
))
998 (if vip-open-with-indent
(indent-to col
))
999 (vip-change-mode-to-insert)))))
1001 (defun vip-Open-line (arg)
1004 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1005 (setq vip-d-com
(list 'vip-Open-line val ?r
))
1006 (let ((col (current-indentation)))
1012 (if vip-open-with-indent
(indent-to col
))
1016 (if vip-open-with-indent
(indent-to col
))
1017 (vip-change-mode-to-insert)))))
1019 (defun vip-open-line-at-point (arg)
1020 "Open line at point."
1022 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1023 (setq vip-d-com
(list 'vip-open-line-at-point val ?r
))
1030 (vip-change-mode-to-insert))))
1032 (defun vip-substitute (arg)
1033 "Substitute characters."
1035 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1040 (vip-change-subr (mark) (point))
1041 (vip-change (mark) (point))))
1042 (setq vip-d-com
(list 'vip-substitute val ?r
))))
1044 (defun vip-substitute-line (arg)
1047 (vip-line (cons arg ?C
)))
1052 (defun vip-line (arg)
1053 (let ((val (car arg
)) (com (cdr arg
)))
1054 (move-marker vip-com-point
(point))
1055 (with-no-warnings (next-line (1- val
)))
1056 (vip-execute-com 'vip-line val com
)))
1058 (defun vip-yank-line (arg)
1059 "Yank ARG lines (in vi's sense)"
1061 (let ((val (vip-p-val arg
)))
1062 (vip-line (cons val ?Y
))))
1067 (defun vip-region (arg)
1069 (let ((val (vip-P-val arg
))
1070 (com (vip-getcom arg
)))
1071 (move-marker vip-com-point
(point))
1072 (exchange-point-and-mark)
1073 (vip-execute-com 'vip-region val com
)))
1075 (defun vip-Region (arg)
1077 (let ((val (vip-P-val arg
))
1078 (com (vip-getCom arg
)))
1079 (move-marker vip-com-point
(point))
1080 (exchange-point-and-mark)
1081 (vip-execute-com 'vip-Region val com
)))
1083 (defun vip-replace-char (arg)
1084 "Replace the following ARG chars by the character read."
1086 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1087 (setq vip-d-com
(list 'vip-replace-char val ?r
))
1088 (vip-replace-char-subr (if (equal com ?r
) vip-d-char
(read-char)) val
)))
1090 (defun vip-replace-char-subr (char arg
)
1092 (setq vip-d-char char
)
1093 (vip-loop (if (> arg
0) arg
(- arg
)) (insert char
))
1094 (backward-char arg
))
1096 (defun vip-replace-string ()
1097 "Replace string. If you supply null string as the string to be replaced,
1098 the query replace mode will toggle between string replace and regexp replace."
1101 (setq str
(vip-read-string
1102 (if vip-re-replace
"Replace regexp: " "Replace string: ")))
1103 (if (string= str
"")
1105 (setq vip-re-replace
(not vip-re-replace
))
1106 (message "Replace mode changed to %s."
1107 (if vip-re-replace
"regexp replace"
1112 ;; (vip-read-string (format "Replace regexp \"%s\" with: " str)))
1113 (while (re-search-forward str nil t
)
1114 (replace-match (vip-read-string
1115 (format "Replace regexp \"%s\" with: " str
))
1120 (vip-read-string (format "Replace \"%s\" with: " str
))))))))
1123 ;; basic cursor movement. j, k, l, m commands.
1125 (defun vip-forward-char (arg)
1126 "Move point right ARG characters (left if ARG negative).On reaching end
1127 of buffer, stop and signal error."
1129 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1130 (if com
(move-marker vip-com-point
(point)))
1132 (if com
(vip-execute-com 'vip-forward-char val com
))))
1134 (defun vip-backward-char (arg)
1135 "Move point left ARG characters (right if ARG negative). On reaching
1136 beginning of buffer, stop and signal error."
1138 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1139 (if com
(move-marker vip-com-point
(point)))
1141 (if com
(vip-execute-com 'vip-backward-char val com
))))
1146 (defun vip-forward-word (arg)
1149 (let ((val (vip-p-val arg
))
1150 (com (vip-getcom arg
)))
1151 (if com
(move-marker vip-com-point
(point)))
1153 (skip-chars-forward " \t\n")
1156 (if (or (= com ?c
) (= com
(- ?c
)))
1157 (progn (backward-word 1) (forward-word 1)))
1158 (if (or (= com ?d
) (= com ?y
))
1162 (skip-chars-forward " \t")))
1163 (vip-execute-com 'vip-forward-word val com
)))))
1165 (defun vip-end-of-word (arg)
1166 "Move point to end of current word."
1168 (let ((val (vip-p-val arg
))
1169 (com (vip-getcom arg
)))
1170 (if com
(move-marker vip-com-point
(point)))
1177 (vip-execute-com 'vip-end-of-word val com
)))))
1179 (defun vip-backward-word (arg)
1182 (let ((val (vip-p-val arg
))
1183 (com (vip-getcom arg
)))
1184 (if com
(move-marker vip-com-point
(point)))
1186 (if com
(vip-execute-com 'vip-backward-word val com
))))
1188 (defun vip-forward-Word (arg)
1189 "Forward word delimited by white character."
1191 (let ((val (vip-p-val arg
))
1192 (com (vip-getcom arg
)))
1193 (if com
(move-marker vip-com-point
(point)))
1194 (re-search-forward "[^ \t\n]*[ \t\n]+" nil t val
)
1197 (if (or (= com ?c
) (= com
(- ?c
)))
1198 (progn (backward-word 1) (forward-word 1)))
1199 (if (or (= com ?d
) (= com ?y
))
1203 (skip-chars-forward " \t")))
1204 (vip-execute-com 'vip-forward-Word val com
)))))
1206 (defun vip-end-of-Word (arg)
1207 "Move forward to end of word delimited by white character."
1209 (let ((val (vip-p-val arg
))
1210 (com (vip-getcom arg
)))
1211 (if com
(move-marker vip-com-point
(point)))
1213 (if (re-search-forward "[^ \t\n]+" nil t val
) (backward-char))
1217 (vip-execute-com 'vip-end-of-Word val com
)))))
1219 (defun vip-backward-Word (arg)
1220 "Backward word delimited by white character."
1222 (let ((val (vip-p-val arg
))
1223 (com (vip-getcom arg
)))
1224 (if com
(move-marker vip-com-point
(point)))
1225 (if (re-search-backward "[ \t\n]+[^ \t\n]+" nil t val
)
1227 (goto-char (point-min)))
1228 (if com
(vip-execute-com 'vip-backward-Word val com
))))
1230 (defun vip-beginning-of-line (arg)
1231 "Go to beginning of line."
1233 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1234 (if com
(move-marker vip-com-point
(point)))
1235 (beginning-of-line val
)
1236 (if com
(vip-execute-com 'vip-beginning-of-line val com
))))
1238 (defun vip-bol-and-skip-white (arg)
1239 "Beginning of line at first non-white character."
1241 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1242 (if com
(move-marker vip-com-point
(point)))
1243 (back-to-indentation)
1244 (if com
(vip-execute-com 'vip-bol-and-skip-white val com
))))
1246 (defun vip-goto-eol (arg)
1247 "Go to end of line."
1249 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1250 (if com
(move-marker vip-com-point
(point)))
1252 (if com
(vip-execute-com 'vip-goto-eol val com
))))
1254 (defun vip-next-line (arg)
1257 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1258 (if com
(move-marker vip-com-point
(point)))
1260 (setq this-command
'next-line
)
1261 (if com
(vip-execute-com 'vip-next-line val com
))))
1263 (defun vip-next-line-at-bol (arg)
1264 "Next line at beginning of line."
1266 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1267 (if com
(move-marker vip-com-point
(point)))
1268 (with-no-warnings (next-line val
))
1269 (back-to-indentation)
1270 (if com
(vip-execute-com 'vip-next-line-at-bol val com
))))
1272 (defun vip-previous-line (arg)
1273 "Go to previous line."
1275 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1276 (if com
(move-marker vip-com-point
(point)))
1277 (with-no-warnings (next-line (- val
)))
1278 (setq this-command
'previous-line
)
1279 (if com
(vip-execute-com 'vip-previous-line val com
))))
1281 (defun vip-previous-line-at-bol (arg)
1282 "Previous line at beginning of line."
1284 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1285 (if com
(move-marker vip-com-point
(point)))
1286 (with-no-warnings (next-line (- val
)))
1287 (back-to-indentation)
1288 (if com
(vip-execute-com 'vip-previous-line val com
))))
1290 (defun vip-change-to-eol (arg)
1291 "Change to end of line."
1293 (vip-goto-eol (cons arg ?c
)))
1295 (defun vip-kill-line (arg)
1298 (vip-goto-eol (cons arg ?d
)))
1303 (defun vip-goto-line (arg)
1304 "Go to ARG's line. Without ARG go to end of buffer."
1306 (let ((val (vip-P-val arg
)) (com (vip-getCom arg
)))
1307 (move-marker vip-com-point
(point))
1310 (goto-char (point-max))
1311 (goto-char (point-min))
1312 (forward-line (1- val
)))
1313 (back-to-indentation)
1314 (if com
(vip-execute-com 'vip-goto-line val com
))))
1316 (defun vip-find-char (arg char forward offset
)
1317 "Find ARG's occurrence of CHAR on the current line. If FORWARD then
1318 search is forward, otherwise backward. OFFSET is used to adjust point
1320 (let ((arg (if forward arg
(- arg
))) point
)
1325 ;; forward search begins here
1326 (if (eolp) (error "") (point))
1327 ;; forward search ends here
1328 (progn (with-no-warnings (next-line 1)) (beginning-of-line) (point)))
1330 ;; backward search begins from here
1331 (if (bolp) (error "") (point))
1332 ;; backward search ends here
1333 (progn (beginning-of-line) (point))))
1334 ;; if arg > 0, point is forwarded before search.
1335 (if (> arg
0) (goto-char (1+ (point-min)))
1336 (goto-char (point-max)))
1337 (let ((case-fold-search nil
))
1338 (search-forward (char-to-string char
) nil
0 arg
))
1339 (setq point
(point))
1340 (if (or (and (> arg
0) (= point
(point-max)))
1341 (and (< arg
0) (= point
(point-min))))
1343 (goto-char (+ point
(if (> arg
0) (if offset -
2 -
1) (if offset
1 0))))))
1345 (defun vip-find-char-forward (arg)
1346 "Find char on the line. If called interactively read the char to find
1347 from the terminal, and if called from vip-repeat, the char last used is
1348 used. This behavior is controlled by the sign of prefix numeric value."
1350 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1352 ;; this means that the function was called interactively
1353 (setq vip-f-char
(read-char)
1357 (if com
(move-marker vip-com-point
(point)))
1358 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t nil
)
1362 (setq vip-F-char vip-f-char
);; set new vip-F-char
1364 (vip-execute-com 'vip-find-char-forward val com
)))))
1366 (defun vip-goto-char-forward (arg)
1367 "Go up to char ARG forward on line."
1369 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1371 ;; this means that the function was called interactively
1372 (setq vip-f-char
(read-char)
1376 (if com
(move-marker vip-com-point
(point)))
1377 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t t
)
1381 (setq vip-F-char vip-f-char
);; set new vip-F-char
1383 (vip-execute-com 'vip-goto-char-forward val com
)))))
1385 (defun vip-find-char-backward (arg)
1386 "Find char ARG on line backward."
1388 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1390 ;; this means that the function was called interactively
1391 (setq vip-f-char
(read-char)
1395 (if com
(move-marker vip-com-point
(point)))
1397 val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil nil
)
1401 (setq vip-F-char vip-f-char
);; set new vip-F-char
1402 (vip-execute-com 'vip-find-char-backward val com
)))))
1404 (defun vip-goto-char-backward (arg)
1405 "Go up to char ARG backward on line."
1407 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1409 ;; this means that the function was called interactively
1410 (setq vip-f-char
(read-char)
1414 (if com
(move-marker vip-com-point
(point)))
1415 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil t
)
1419 (setq vip-F-char vip-f-char
);; set new vip-F-char
1420 (vip-execute-com 'vip-goto-char-backward val com
)))))
1422 (defun vip-repeat-find (arg)
1423 "Repeat previous find command."
1425 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1426 (if com
(move-marker vip-com-point
(point)))
1427 (vip-find-char val vip-f-char vip-f-forward vip-f-offset
)
1430 (if vip-f-forward
(forward-char))
1431 (vip-execute-com 'vip-repeat-find val com
)))))
1433 (defun vip-repeat-find-opposite (arg)
1434 "Repeat previous find command in the opposite direction."
1436 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1437 (if com
(move-marker vip-com-point
(point)))
1438 (vip-find-char val vip-f-char
(not vip-f-forward
) vip-f-offset
)
1441 (if vip-f-forward
(forward-char))
1442 (vip-execute-com 'vip-repeat-find-opposite val com
)))))
1445 ;; window scrolling etc.
1447 (defun vip-other-window (arg)
1448 "Switch to other window."
1451 (or (not (eq vip-current-mode
'emacs-mode
))
1452 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
1453 (vip-change-mode-to-vi)))
1455 (defun vip-window-top (arg)
1456 "Go to home window line."
1458 (let ((val (vip-p-val arg
))
1459 (com (vip-getCom arg
)))
1460 (if com
(move-marker vip-com-point
(point)))
1461 (move-to-window-line (1- val
))
1462 (if com
(vip-execute-com 'vip-window-top val com
))))
1464 (defun vip-window-middle (arg)
1465 "Go to middle window line."
1467 (let ((val (vip-p-val arg
))
1468 (com (vip-getCom arg
)))
1469 (if com
(move-marker vip-com-point
(point)))
1470 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val
)))
1471 (if com
(vip-execute-com 'vip-window-middle val com
))))
1473 (defun vip-window-bottom (arg)
1474 "Go to last window line."
1476 (let ((val (vip-p-val arg
))
1477 (com (vip-getCom arg
)))
1478 (if com
(move-marker vip-com-point
(point)))
1479 (move-to-window-line (- val
))
1480 (if com
(vip-execute-com 'vip-window-bottom val com
))))
1482 (defun vip-line-to-top (arg)
1483 "Put current line on the home line."
1485 (recenter (1- arg
)))
1487 (defun vip-line-to-middle (arg)
1488 "Put current line on the middle line."
1490 (recenter (+ (1- arg
) (/ (1- (window-height)) 2))))
1492 (defun vip-line-to-bottom (arg)
1493 "Put current line on the last line."
1495 (recenter (- (window-height) (1+ arg
))))
1500 (defun vip-paren-match (arg)
1501 "Go to the matching parenthesis."
1503 (let ((com (vip-getcom arg
)))
1505 (if (or (> arg
99) (< arg
1))
1506 (error "Prefix must be between 1 and 99")
1508 (if (> (point-max) 80000)
1509 (* (/ (point-max) 100) arg
)
1510 (/ (* (point-max) arg
) 100)))
1511 (back-to-indentation))
1512 (cond ((looking-at "[\(\[{]")
1513 (if com
(move-marker vip-com-point
(point)))
1516 (vip-execute-com 'vip-paren-match nil com
)
1518 ((looking-at "[])}]")
1520 (if com
(move-marker vip-com-point
(point)))
1522 (if com
(vip-execute-com 'vip-paren-match nil com
)))
1526 ;; sentence and paragraph
1528 (defun vip-forward-sentence (arg)
1531 (let ((val (vip-p-val arg
))
1532 (com (vip-getcom arg
)))
1533 (if com
(move-marker vip-com-point
(point)))
1534 (forward-sentence val
)
1535 (if com
(vip-execute-com 'vip-forward-sentence nil com
))))
1537 (defun vip-backward-sentence (arg)
1538 "Backward sentence."
1540 (let ((val (vip-p-val arg
))
1541 (com (vip-getcom arg
)))
1542 (if com
(move-marker vip-com-point
(point)))
1543 (backward-sentence val
)
1544 (if com
(vip-execute-com 'vip-backward-sentence nil com
))))
1546 (defun vip-forward-paragraph (arg)
1547 "Forward paragraph."
1549 (let ((val (vip-p-val arg
))
1550 (com (vip-getCom arg
)))
1551 (if com
(move-marker vip-com-point
(point)))
1552 (forward-paragraph val
)
1553 (if com
(vip-execute-com 'vip-forward-paragraph nil com
))))
1555 (defun vip-backward-paragraph (arg)
1556 "Backward paragraph."
1558 (let ((val (vip-p-val arg
))
1559 (com (vip-getCom arg
)))
1560 (if com
(move-marker vip-com-point
(point)))
1561 (backward-paragraph val
)
1562 (if com
(vip-execute-com 'vip-backward-paragraph nil com
))))
1567 (defun vip-scroll (arg)
1568 "Scroll to next screen."
1573 (setq arg
(1- arg
)))
1576 (setq arg
(1+ arg
)))))
1578 (defun vip-scroll-back (arg)
1579 "Scroll to previous screen."
1581 (vip-scroll (- arg
)))
1583 (defun vip-scroll-down (arg)
1584 "Scroll up half screen."
1586 (if (null arg
) (scroll-down (/ (window-height) 2))
1589 (defun vip-scroll-down-one (arg)
1590 "Scroll up one line."
1594 (defun vip-scroll-up (arg)
1595 "Scroll down half screen."
1597 (if (null arg
) (scroll-up (/ (window-height) 2))
1600 (defun vip-scroll-up-one (arg)
1601 "Scroll down one line."
1608 (defun vip-buffer-in-two-windows ()
1609 "Show current buffer in two windows."
1611 (delete-other-windows)
1612 (split-window-below))
1617 (defun vip-search-forward (arg)
1618 "Search a string forward. ARG is used to find the ARG's occurrence
1619 of the string. Default is vanilla search. Search mode can be toggled by
1620 giving null search string."
1622 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1623 (setq vip-s-forward t
1624 vip-s-string
(vip-read-string (if vip-re-search
"RE-/" "/")))
1625 (if (string= vip-s-string
"")
1627 (setq vip-re-search
(not vip-re-search
))
1628 (message "Search mode changed to %s search."
1629 (if vip-re-search
"regular expression"
1631 (vip-search vip-s-string t val
)
1634 (move-marker vip-com-point
(mark))
1635 (vip-execute-com 'vip-search-next val com
))))))
1637 (defun vip-search-backward (arg)
1638 "Search a string backward. ARG is used to find the ARG's occurrence
1639 of the string. Default is vanilla search. Search mode can be toggled by
1640 giving null search string."
1642 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1643 (setq vip-s-forward nil
1644 vip-s-string
(vip-read-string (if vip-re-search
"RE-?" "?")))
1645 (if (string= vip-s-string
"")
1647 (setq vip-re-search
(not vip-re-search
))
1648 (message "Search mode changed to %s search."
1649 (if vip-re-search
"regular expression"
1651 (vip-search vip-s-string nil val
)
1654 (move-marker vip-com-point
(mark))
1655 (vip-execute-com 'vip-search-next val com
))))))
1657 (defun vip-search (string forward arg
&optional no-offset init-point
)
1658 "(STRING FORWARD COUNT &optional NO-OFFSET) Search COUNT's occurrence of
1659 STRING. Search will be forward if FORWARD, otherwise backward."
1660 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
))
1661 (null-arg (null (vip-P-val arg
))) (offset (not no-offset
))
1662 (case-fold-search vip-case-fold-search
)
1663 (start-point (or init-point
(point))))
1665 (condition-case conditions
1667 (if (and offset
(not (eobp))) (forward-char))
1670 (re-search-forward string nil nil val
)
1671 (re-search-backward string
))
1672 (search-forward string nil nil val
)
1673 (search-backward string
))
1674 (push-mark start-point
))
1676 (if (and null-arg vip-search-wrap-around
)
1678 (goto-char (point-min))
1679 (vip-search string forward
(cons 1 com
) t start-point
))
1680 (goto-char start-point
)
1681 (signal 'search-failed
(cdr conditions
)))))
1682 (condition-case conditions
1685 (re-search-backward string nil nil val
)
1686 (search-backward string nil nil val
))
1687 (push-mark start-point
))
1689 (if (and null-arg vip-search-wrap-around
)
1691 (goto-char (point-max))
1692 (vip-search string forward
(cons 1 com
) t start-point
))
1693 (goto-char start-point
)
1694 (signal 'search-failed
(cdr conditions
))))))))
1696 (defun vip-search-next (arg)
1697 "Repeat previous search."
1699 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1700 (if (null vip-s-string
) (error "No previous search string"))
1701 (vip-search vip-s-string vip-s-forward arg
)
1702 (if com
(vip-execute-com 'vip-search-next val com
))))
1704 (defun vip-search-Next (arg)
1705 "Repeat previous search in the reverse direction."
1707 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1708 (if (null vip-s-string
) (error "No previous search string"))
1709 (vip-search vip-s-string
(not vip-s-forward
) arg
)
1710 (if com
(vip-execute-com 'vip-search-Next val com
))))
1713 ;; visiting and killing files, buffers
1715 (defun vip-switch-to-buffer ()
1716 "Switch to buffer in the current window."
1721 (format "switch to buffer \(%s\): "
1722 (buffer-name (other-buffer (current-buffer))))))
1723 (switch-to-buffer buffer
)
1724 (vip-change-mode-to-vi)))
1726 (defun vip-switch-to-buffer-other-window ()
1727 "Switch to buffer in another window."
1732 (format "Switch to buffer \(%s\): "
1733 (buffer-name (other-buffer (current-buffer))))))
1734 (switch-to-buffer-other-window buffer
)
1735 (vip-change-mode-to-vi)))
1737 (defun vip-kill-buffer ()
1740 (let (buffer buffer-name
)
1743 (format "Kill buffer \(%s\): "
1744 (buffer-name (current-buffer)))))
1746 (if (null buffer-name
)
1748 (get-buffer buffer-name
)))
1749 (if (null buffer
) (error "Buffer %s nonexistent" buffer-name
))
1750 (if (or (not (buffer-modified-p buffer
))
1751 (y-or-n-p "Buffer is modified, are you sure? "))
1752 (kill-buffer buffer
)
1753 (error "Buffer not killed"))))
1755 (defun vip-find-file ()
1756 "Visit file in the current window."
1759 (setq file
(read-file-name "visit file: "))
1760 (switch-to-buffer (find-file-noselect file
))
1761 (vip-change-mode-to-vi)))
1763 (defun vip-find-file-other-window ()
1764 "Visit file in another window."
1767 (setq file
(read-file-name "Visit file: "))
1768 (switch-to-buffer-other-window (find-file-noselect file
))
1769 (vip-change-mode-to-vi)))
1771 (defun vip-info-on-file ()
1772 "Give information of the file associated to the current buffer."
1774 (message "\"%s\" line %d of %d"
1775 (if (buffer-file-name) (buffer-file-name) "")
1776 (1+ (count-lines (point-min) (point)))
1777 (1+ (count-lines (point-min) (point-max)))))
1782 (defun vip-yank (text)
1783 "yank TEXT silently."
1785 (vip-push-mark-silent (point))
1787 (exchange-point-and-mark))
1788 (skip-chars-forward " \t"))
1790 (defun vip-put-back (arg)
1791 "Put back after point/below line."
1793 (let ((val (vip-p-val arg
))
1794 (text (if vip-use-register
1795 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1796 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1797 (get-register vip-use-register
))
1800 (if vip-use-register
1801 (let ((reg vip-use-register
))
1802 (setq vip-use-register nil
)
1803 (error "Nothing in register %c" reg
))
1805 (setq vip-use-register nil
)
1806 (if (vip-end-with-a-newline-p text
)
1808 (with-no-warnings (next-line 1))
1809 (beginning-of-line))
1810 (if (and (not (eolp)) (not (eobp))) (forward-char)))
1811 (setq vip-d-com
(list 'vip-put-back val nil vip-use-register
))
1812 (vip-loop val
(vip-yank text
))))
1814 (defun vip-Put-back (arg)
1815 "Put back at point/above line."
1817 (let ((val (vip-p-val arg
))
1818 (text (if vip-use-register
1819 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1820 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1821 (get-register vip-use-register
))
1824 (if vip-use-register
1825 (let ((reg vip-use-register
))
1826 (setq vip-use-register nil
)
1827 (error "Nothing in register %c" reg
))
1829 (setq vip-use-register nil
)
1830 (if (vip-end-with-a-newline-p text
) (beginning-of-line))
1831 (setq vip-d-com
(list 'vip-Put-back val nil vip-use-register
))
1832 (vip-loop val
(vip-yank text
))))
1834 (defun vip-delete-char (arg)
1837 (let ((val (vip-p-val arg
)))
1838 (setq vip-d-com
(list 'vip-delete-char val nil
))
1839 (if vip-use-register
1841 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1842 (vip-append-to-register
1843 (+ vip-use-register
32) (point) (- (point) val
))
1844 (copy-to-register vip-use-register
(point) (- (point) val
) nil
))
1845 (setq vip-use-register nil
)))
1846 (delete-char val t
)))
1848 (defun vip-delete-backward-char (arg)
1849 "Delete previous character."
1851 (let ((val (vip-p-val arg
)))
1852 (setq vip-d-com
(list 'vip-delete-backward-char val nil
))
1853 (if vip-use-register
1855 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1856 (vip-append-to-register
1857 (+ vip-use-register
32) (point) (+ (point) val
))
1858 (copy-to-register vip-use-register
(point) (+ (point) val
) nil
))
1859 (setq vip-use-register nil
)))
1860 (delete-backward-char val t
)))
1865 (defun vip-join-lines (arg)
1866 "Join this line to next, if ARG is nil. Otherwise, join ARG lines"
1868 (let ((val (vip-P-val arg
)))
1869 (setq vip-d-com
(list 'vip-join-lines val nil
))
1870 (vip-loop (if (null val
) 1 (1- val
))
1876 (delete-region (point) (1- (point)))
1877 (fixup-whitespace)))))))
1880 ;; making small changes
1882 (defvar vip-c-string
)
1884 (defun vip-change (beg end
)
1886 (vip-read-string (format "%s => " (buffer-substring beg end
))))
1887 (vip-change-subr beg end
))
1889 (defun vip-change-subr (beg end
)
1890 (if vip-use-register
1892 (copy-to-register vip-use-register beg end nil
)
1893 (setq vip-use-register nil
)))
1894 (kill-region beg end
)
1895 (setq this-command
'vip-change
)
1896 (insert vip-c-string
))
1901 (defun vip-query-replace ()
1902 "Query replace. If you supply null string as the string to be replaced,
1903 the query replace mode will toggle between string replace and regexp replace."
1906 (setq str
(vip-read-string
1907 (if vip-re-query-replace
"Query replace regexp: "
1908 "Query replace: ")))
1909 (if (string= str
"")
1911 (setq vip-re-query-replace
(not vip-re-query-replace
))
1912 (message "Query replace mode changed to %s."
1913 (if vip-re-query-replace
"regexp replace"
1915 (if vip-re-query-replace
1916 (query-replace-regexp
1918 (vip-read-string (format "Query replace regexp \"%s\" with: " str
)))
1921 (vip-read-string (format "Query replace \"%s\" with: " str
)))))))
1926 (defun vip-mark-beginning-of-buffer ()
1929 (goto-char (point-min))
1930 (exchange-point-and-mark)
1931 (message "mark set at the beginning of buffer"))
1933 (defun vip-mark-end-of-buffer ()
1936 (goto-char (point-max))
1937 (exchange-point-and-mark)
1938 (message "mark set at the end of buffer"))
1940 (defun vip-mark-point (char)
1942 (cond ((and (<= ?a char
) (<= char ?z
))
1943 (point-to-register (- char
(- ?a ?\C-a
)) nil
))
1944 ((= char ?
<) (vip-mark-beginning-of-buffer))
1945 ((= char ?
>) (vip-mark-end-of-buffer))
1946 ((= char ?.
) (push-mark))
1947 ((= char ?
,) (set-mark-command 1))
1948 ((= char ?D
) (mark-defun))
1951 (defun vip-goto-mark (arg)
1954 (let ((char (read-char)) (com (vip-getcom arg
)))
1955 (vip-goto-mark-subr char com nil
)))
1957 (defun vip-goto-mark-and-skip-white (arg)
1958 "Go to mark and skip to first non-white on line."
1960 (let ((char (read-char)) (com (vip-getCom arg
)))
1961 (vip-goto-mark-subr char com t
)))
1963 (defun vip-goto-mark-subr (char com skip-white
)
1964 (cond ((and (<= ?a char
) (<= char ?z
))
1965 (let ((buff (current-buffer)))
1966 (if com
(move-marker vip-com-point
(point)))
1967 (goto-char (register-to-point (- char
(- ?a ?\C-a
))))
1968 (if skip-white
(back-to-indentation))
1969 (vip-change-mode-to-vi)
1971 (if (equal buff
(current-buffer))
1972 (vip-execute-com (if skip-white
1973 'vip-goto-mark-and-skip-white
1976 (switch-to-buffer buff
)
1977 (goto-char vip-com-point
)
1978 (vip-change-mode-to-vi)
1980 ((and (not skip-white
) (= char ?
`))
1981 (if com
(move-marker vip-com-point
(point)))
1982 (exchange-point-and-mark)
1983 (if com
(vip-execute-com 'vip-goto-mark nil com
)))
1984 ((and skip-white
(= char ?
'))
1985 (if com
(move-marker vip-com-point
(point)))
1986 (exchange-point-and-mark)
1987 (back-to-indentation)
1988 (if com
(vip-execute-com 'vip-goto-mark-and-skip-white nil com
)))
1991 (defun vip-exchange-point-and-mark ()
1993 (exchange-point-and-mark)
1994 (back-to-indentation))
1996 (defun vip-keyboard-quit ()
1997 "Abort partially formed or running command."
1999 (setq vip-use-register nil
)
2002 (defun vip-ctl-c-equivalent (arg)
2003 "Emulate C-c in Emacs mode."
2005 (vip-ctl-key-equivalent "\C-c" arg
))
2007 (defun vip-ctl-x-equivalent (arg)
2008 "Emulate C-x in Emacs mode."
2010 (vip-ctl-key-equivalent "\C-x" arg
))
2012 (defun vip-ctl-key-equivalent (key arg
)
2013 (let ((char (read-char)))
2014 (if (and (<= ?A char
) (<= char ?Z
))
2015 (setq char
(- char
(- ?A ?\C-a
))))
2016 (vip-escape-to-emacs arg
(list (aref key
0) char
))))
2018 ;; commands in insertion mode
2020 (defun vip-delete-backward-word (arg)
2021 "Delete previous word."
2026 (delete-region (point) (mark))))
2029 ;; implement ex commands
2031 (defvar ex-token-type nil
2032 "type of token. if non-nil, gives type of address. if nil, it
2035 (defvar ex-token nil
2038 (defvar ex-addresses nil
2039 "list of ex addresses")
2044 (defvar ex-buffer nil
2045 "name of ex buffer")
2047 (defvar ex-count nil
2048 "value of ex count")
2050 (defvar ex-g-flag nil
2051 "flag for global command")
2053 (defvar ex-g-variant nil
2054 "if t global command is executed on lines not matching ex-g-pat")
2056 (defvar ex-reg-exp nil
2057 "save reg-exp used in substitute")
2060 "replace pattern for substitute")
2062 (defvar ex-g-pat nil
2063 "pattern for global command")
2065 (defvar ex-map
(make-sparse-keymap)
2066 "save commands for mapped keys")
2071 (defvar ex-file nil
)
2073 (defvar ex-variant nil
)
2075 (defvar ex-offset nil
)
2077 (defvar ex-append nil
)
2083 (defun vip-looking-back (str)
2084 "returns t if looking back reg-exp STR before point."
2085 (and (save-excursion (re-search-backward str nil t
))
2086 (= (point) (match-end 0))))
2088 (defun vip-check-sub (str)
2089 "check if ex-token is an initial segment of STR"
2090 (let ((length (length ex-token
)))
2091 (if (and (<= length
(length str
))
2092 (string= ex-token
(substring str
0 length
)))
2094 (setq ex-token-type
"non-command"))))
2096 (defun vip-get-ex-com-subr ()
2097 "get a complete ex command"
2099 (re-search-forward "[a-z][a-z]*")
2100 (setq ex-token-type
"command")
2101 (setq ex-token
(buffer-substring (point) (mark)))
2102 (exchange-point-and-mark)
2103 (cond ((looking-at "a")
2104 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
2105 ((looking-at "ar") (vip-check-sub "args"))
2106 (t (vip-check-sub "append"))))
2107 ((looking-at "[bh]") (setq ex-token-type
"non-command"))
2109 (if (looking-at "co") (vip-check-sub "copy")
2110 (vip-check-sub "change")))
2111 ((looking-at "d") (vip-check-sub "delete"))
2113 (if (looking-at "ex") (vip-check-sub "ex")
2114 (vip-check-sub "edit")))
2115 ((looking-at "f") (vip-check-sub "file"))
2116 ((looking-at "g") (vip-check-sub "global"))
2117 ((looking-at "i") (vip-check-sub "insert"))
2118 ((looking-at "j") (vip-check-sub "join"))
2119 ((looking-at "l") (vip-check-sub "list"))
2121 (cond ((looking-at "map") (vip-check-sub "map"))
2122 ((looking-at "mar") (vip-check-sub "mark"))
2123 (t (vip-check-sub "move"))))
2125 (if (looking-at "nu") (vip-check-sub "number")
2126 (vip-check-sub "next")))
2127 ((looking-at "o") (vip-check-sub "open"))
2129 (cond ((looking-at "pre") (vip-check-sub "preserve"))
2130 ((looking-at "pu") (vip-check-sub "put"))
2131 (t (vip-check-sub "print"))))
2132 ((looking-at "q") (vip-check-sub "quit"))
2134 (cond ((looking-at "rec") (vip-check-sub "recover"))
2135 ((looking-at "rew") (vip-check-sub "rewind"))
2136 (t (vip-check-sub "read"))))
2138 (cond ((looking-at "se") (vip-check-sub "set"))
2139 ((looking-at "sh") (vip-check-sub "shell"))
2140 ((looking-at "so") (vip-check-sub "source"))
2141 ((looking-at "st") (vip-check-sub "stop"))
2142 (t (vip-check-sub "substitute"))))
2144 (if (looking-at "ta") (vip-check-sub "tag")
2145 (vip-check-sub "t")))
2147 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
2148 ((looking-at "unm") (vip-check-sub "unmap"))
2149 (t (vip-check-sub "undo"))))
2151 (cond ((looking-at "ve") (vip-check-sub "version"))
2152 ((looking-at "vi") (vip-check-sub "visual"))
2153 (t (vip-check-sub "v"))))
2155 (if (looking-at "wq") (vip-check-sub "wq")
2156 (vip-check-sub "write")))
2157 ((looking-at "x") (vip-check-sub "xit"))
2158 ((looking-at "y") (vip-check-sub "yank"))
2159 ((looking-at "z") (vip-check-sub "z")))
2160 (exchange-point-and-mark))
2162 (defun vip-get-ex-token ()
2163 "get an ex-token which is either an address or a command.
2164 a token has type \(command, address, end-mark\) and value."
2165 (with-current-buffer " *ex-working-space*"
2166 (skip-chars-forward " \t")
2167 (cond ((looking-at "[k#]")
2168 (setq ex-token-type
"command")
2169 (setq ex-token
(char-to-string (following-char)))
2171 ((looking-at "[a-z]") (vip-get-ex-com-subr))
2174 (setq ex-token-type
"dot"))
2175 ((looking-at "[0-9]")
2177 (re-search-forward "[0-9]*")
2179 (cond ((string= ex-token-type
"plus") "add-number")
2180 ((string= ex-token-type
"minus") "sub-number")
2182 (setq ex-token
(string-to-number (buffer-substring (point) (mark)))))
2185 (setq ex-token-type
"end"))
2188 (setq ex-token-type
"whole"))
2190 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
2194 (setq ex-token-type
"plus"))
2195 ((looking-at "+[0-9]")
2197 (setq ex-token-type
"plus"))
2199 (error "Badly formed address"))))
2201 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
2205 (setq ex-token-type
"minus"))
2206 ((looking-at "-[0-9]")
2208 (setq ex-token-type
"minus"))
2210 (error "Badly formed address"))))
2215 (while (and (not (eolp)) cont
)
2216 ;;(re-search-forward "[^/]*/")
2217 (re-search-forward "[^/]*\\(/\\|\n\\)")
2218 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2221 (setq ex-token
(buffer-substring (point) (mark)))
2222 (if (looking-at "/") (forward-char 1))
2223 (setq ex-token-type
"search-forward"))
2228 (while (and (not (eolp)) cont
)
2229 ;;(re-search-forward "[^\\?]*\\?")
2230 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
2231 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
2234 (if (not (looking-at "\n")) (forward-char 1))))
2235 (setq ex-token-type
"search-backward")
2236 (setq ex-token
(buffer-substring (1- (point)) (mark))))
2239 (setq ex-token-type
"comma"))
2242 (setq ex-token-type
"semi-colon"))
2243 ((looking-at "[!=><&~]")
2244 (setq ex-token-type
"command")
2245 (setq ex-token
(char-to-string (following-char)))
2248 (setq ex-token-type
"goto-mark")
2250 (cond ((looking-at "'") (setq ex-token nil
))
2251 ((looking-at "[a-z]") (setq ex-token
(following-char)))
2252 (t (error "Marks are ' and a-z")))
2255 (setq ex-token-type
"end-mark")
2256 (setq ex-token
"goto"))
2258 (error "invalid token")))))
2260 (defun vip-ex (&optional string
)
2261 "ex commands within VIP."
2266 (let ((com-str (or string
(vip-read-string ":")))
2267 (address nil
) (cont t
) (dot (point)))
2268 (with-current-buffer (get-buffer-create " *ex-working-space*")
2269 (delete-region (point-min) (point-max))
2270 (insert com-str
"\n")
2271 (goto-char (point-min)))
2272 (setq ex-token-type
"")
2273 (setq ex-addresses nil
)
2276 (cond ((or (string= ex-token-type
"command")
2277 (string= ex-token-type
"end-mark"))
2278 (if address
(setq ex-addresses
(cons address ex-addresses
)))
2279 (cond ((string= ex-token
"global")
2282 ((string= ex-token
"v")
2286 (vip-execute-ex-command)
2287 (with-current-buffer " *ex-working-space*"
2288 (skip-chars-forward " \t")
2289 (cond ((looking-at "|")
2293 (t (error "Extra character at end of a command")))))))
2294 ((string= ex-token-type
"non-command")
2295 (error "%s: Not an editor command" ex-token
))
2296 ((string= ex-token-type
"whole")
2298 (cons (point-max) (cons (point-min) ex-addresses
))))
2299 ((string= ex-token-type
"comma")
2301 (cons (if (null address
) (point) address
) ex-addresses
)))
2302 ((string= ex-token-type
"semi-colon")
2303 (if address
(setq dot address
))
2305 (cons (if (null address
) (point) address
) ex-addresses
)))
2306 (t (let ((ans (vip-get-ex-address-subr address dot
)))
2307 (if ans
(setq address ans
))))))))
2309 (defun vip-get-ex-pat ()
2310 "get a regular expression and set ex-variant if found"
2311 (with-current-buffer " *ex-working-space*"
2312 (skip-chars-forward " \t")
2313 (if (looking-at "!")
2315 (setq ex-g-variant
(not ex-g-variant
)
2316 ex-g-flag
(not ex-g-flag
))
2318 (skip-chars-forward " \t")))
2319 (if (looking-at "/")
2324 (while (and (not (eolp)) cont
)
2325 (re-search-forward "[^/]*\\(/\\|\n\\)")
2326 ;;(re-search-forward "[^/]*/")
2327 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2330 (if (= (mark) (point)) ""
2331 (buffer-substring (1- (point)) (mark))))
2333 (setq ex-token nil
))))
2335 (defun vip-get-ex-command ()
2337 (with-current-buffer " *ex-working-space*"
2338 (if (looking-at "/") (forward-char 1))
2339 (skip-chars-forward " \t")
2340 (cond ((looking-at "[a-z]")
2341 (vip-get-ex-com-subr)
2342 (if (string= ex-token-type
"non-command")
2343 (error "%s: not an editor command" ex-token
)))
2344 ((looking-at "[!=><&~]")
2345 (setq ex-token
(char-to-string (following-char)))
2347 (t (error "Could not find an ex command")))))
2349 (defun vip-get-ex-opt-gc ()
2350 "get an ex option g or c"
2351 (with-current-buffer " *ex-working-space*"
2352 (if (looking-at "/") (forward-char 1))
2353 (skip-chars-forward " \t")
2354 (cond ((looking-at "g")
2364 (defun vip-default-ex-addresses (&optional whole-flag
)
2365 "compute default addresses. whole-flag means whole buffer."
2366 (cond ((null ex-addresses
)
2369 (cons (point-max) (cons (point-min) nil
))
2370 (cons (point) (cons (point) nil
)))))
2371 ((null (cdr ex-addresses
))
2373 (cons (car ex-addresses
) ex-addresses
)))))
2375 (defun vip-get-ex-address ()
2376 "get an ex-address as a marker and set ex-flag if a flag is found"
2377 (let ((address (point-marker)) (cont t
))
2382 (cond ((string= ex-token-type
"command")
2383 (if (or (string= ex-token
"print") (string= ex-token
"list")
2384 (string= ex-token
"#"))
2388 (error "address expected")))
2389 ((string= ex-token-type
"end-mark")
2391 ((string= ex-token-type
"whole")
2392 (error "a trailing address is expected"))
2393 ((string= ex-token-type
"comma")
2394 (error "Extra characters after an address"))
2395 (t (let ((ans (vip-get-ex-address-subr address
(point-marker))))
2396 (if ans
(setq address ans
))))))
2399 (defun vip-get-ex-address-subr (old-address dot
)
2400 "returns an address as a point"
2401 (let ((address nil
))
2402 (if (null old-address
) (setq old-address dot
))
2403 (cond ((string= ex-token-type
"dot")
2405 ((string= ex-token-type
"add-number")
2407 (goto-char old-address
)
2408 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
2409 (setq address
(point-marker))))
2410 ((string= ex-token-type
"sub-number")
2412 (goto-char old-address
)
2413 (forward-line (- ex-token
))
2414 (setq address
(point-marker))))
2415 ((string= ex-token-type
"abs-number")
2417 (goto-char (point-min))
2418 (if (= ex-token
0) (setq address
0)
2419 (forward-line (1- ex-token
))
2420 (setq address
(point-marker)))))
2421 ((string= ex-token-type
"end")
2422 (setq address
(point-max-marker)))
2423 ((string= ex-token-type
"plus") t
);; do nothing
2424 ((string= ex-token-type
"minus") t
);; do nothing
2425 ((string= ex-token-type
"search-forward")
2427 (ex-search-address t
)
2428 (setq address
(point-marker))))
2429 ((string= ex-token-type
"search-backward")
2431 (ex-search-address nil
)
2432 (setq address
(point-marker))))
2433 ((string= ex-token-type
"goto-mark")
2436 (exchange-point-and-mark)
2437 (goto-char (register-to-point (- ex-token
(- ?a ?\C-a
)))))
2438 (setq address
(point-marker)))))
2441 (defun ex-search-address (forward)
2442 "search pattern and set address"
2443 (if (string= ex-token
"")
2444 (if (null vip-s-string
) (error "No previous search string")
2445 (setq ex-token vip-s-string
))
2446 (setq vip-s-string ex-token
))
2450 (re-search-forward ex-token
))
2452 (re-search-backward ex-token
)))
2454 (defun vip-get-ex-buffer ()
2455 "get a buffer name and set ex-count and ex-flag if found"
2456 (setq ex-buffer nil
)
2459 (with-current-buffer " *ex-working-space*"
2460 (skip-chars-forward " \t")
2461 (if (looking-at "[a-zA-Z]")
2463 (setq ex-buffer
(following-char))
2465 (skip-chars-forward " \t")))
2466 (if (looking-at "[0-9]")
2469 (re-search-forward "[0-9][0-9]*")
2470 (setq ex-count
(string-to-number (buffer-substring (point) (mark))))
2471 (skip-chars-forward " \t")))
2472 (if (looking-at "[pl#]")
2476 (if (not (looking-at "[\n|]"))
2477 (error "Invalid extra characters"))))
2479 (defun vip-get-ex-count ()
2480 (setq ex-variant nil
2483 (with-current-buffer " *ex-working-space*"
2484 (skip-chars-forward " \t")
2485 (if (looking-at "!")
2489 (skip-chars-forward " \t")
2490 (if (looking-at "[0-9]")
2493 (re-search-forward "[0-9][0-9]*")
2494 (setq ex-count
(string-to-number (buffer-substring (point) (mark))))
2495 (skip-chars-forward " \t")))
2496 (if (looking-at "[pl#]")
2500 (if (not (looking-at "[\n|]"))
2501 (error "Invalid extra characters"))))
2503 (defun vip-get-ex-file ()
2504 "get a file name and set ex-variant, ex-append and ex-offset if found"
2509 (with-current-buffer " *ex-working-space*"
2510 (skip-chars-forward " \t")
2511 (if (looking-at "!")
2515 (skip-chars-forward " \t")))
2516 (if (looking-at ">>")
2521 (skip-chars-forward " \t")))
2522 (if (looking-at "+")
2526 (re-search-forward "[ \t\n]")
2528 (setq ex-offset
(buffer-substring (point) (mark)))
2530 (skip-chars-forward " \t")))
2532 (re-search-forward "[ \t\n]")
2534 (setq ex-file
(buffer-substring (point) (mark)))))
2536 (defun vip-execute-ex-command ()
2537 "execute ex command using the value of addresses."
2538 (cond ((string= ex-token
"goto") (ex-goto))
2539 ((string= ex-token
"copy") (ex-copy nil
))
2540 ((string= ex-token
"delete") (ex-delete))
2541 ((string= ex-token
"edit") (ex-edit))
2542 ((string= ex-token
"file") (vip-info-on-file))
2543 ;((string= ex-token "global") (ex-global nil))
2544 ((string= ex-token
"join") (ex-line "join"))
2545 ((string= ex-token
"k") (ex-mark))
2546 ((string= ex-token
"mark") (ex-mark))
2547 ((string= ex-token
"map") (ex-map))
2548 ((string= ex-token
"move") (ex-copy t
))
2549 ((string= ex-token
"put") (ex-put))
2550 ((string= ex-token
"quit") (ex-quit))
2551 ((string= ex-token
"read") (ex-read))
2552 ((string= ex-token
"set") (ex-set))
2553 ((string= ex-token
"shell") (ex-shell))
2554 ((string= ex-token
"substitute") (ex-substitute))
2555 ((string= ex-token
"stop") (suspend-emacs))
2556 ((string= ex-token
"t") (ex-copy nil
))
2557 ((string= ex-token
"tag") (ex-tag))
2558 ((string= ex-token
"undo") (vip-undo))
2559 ((string= ex-token
"unmap") (ex-unmap))
2560 ;((string= ex-token "v") (ex-global t))
2561 ((string= ex-token
"version") (vip-version))
2562 ((string= ex-token
"visual") (ex-edit))
2563 ((string= ex-token
"write") (ex-write nil
))
2564 ((string= ex-token
"wq") (ex-write t
))
2565 ((string= ex-token
"yank") (ex-yank))
2566 ((string= ex-token
"!") (ex-command))
2567 ((string= ex-token
"=") (ex-line-no))
2568 ((string= ex-token
">") (ex-line "right"))
2569 ((string= ex-token
"<") (ex-line "left"))
2570 ((string= ex-token
"&") (ex-substitute t
))
2571 ((string= ex-token
"~") (ex-substitute t t
))
2572 ((or (string= ex-token
"append")
2573 (string= ex-token
"args")
2574 (string= ex-token
"change")
2575 (string= ex-token
"insert")
2576 (string= ex-token
"open")
2578 (error "%s: no such command from VIP" ex-token
))
2579 ((or (string= ex-token
"abbreviate")
2580 (string= ex-token
"list")
2581 (string= ex-token
"next")
2582 (string= ex-token
"print")
2583 (string= ex-token
"preserve")
2584 (string= ex-token
"recover")
2585 (string= ex-token
"rewind")
2586 (string= ex-token
"source")
2587 (string= ex-token
"unabbreviate")
2588 (string= ex-token
"xit")
2589 (string= ex-token
"z")
2591 (error "%s: not implemented in VIP" ex-token
))
2592 (t (error "%s: Not an editor command" ex-token
))))
2596 (if (null ex-addresses
)
2597 (setq ex-addresses
(cons (point) nil
)))
2599 (goto-char (car ex-addresses
))
2600 (beginning-of-line))
2602 (defun ex-copy (del-flag)
2603 "ex copy and move command. DEL-FLAG means delete."
2604 (vip-default-ex-addresses)
2605 (let ((address (vip-get-ex-address))
2606 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2610 (vip-enlarge-region (mark) (point))
2611 (if del-flag
(kill-region (point) (mark))
2612 (copy-region-as-kill (point) (mark)))
2615 (with-output-to-temp-buffer "*copy text*"
2617 (if (or del-flag ex-g-flag ex-g-variant
)
2619 (buffer-substring (point) (mark)))))
2622 (vip-read-string "[Hit return to continue] ")
2623 (save-excursion (kill-buffer "*copy text*")))
2625 (save-excursion (kill-buffer "*copy text*"))
2626 (signal 'quit nil
))))))
2628 (goto-char (point-min))
2631 (insert (current-kill 0))))
2635 (vip-default-ex-addresses)
2637 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2638 (if (> beg end
) (error "First address exceeds second"))
2640 (vip-enlarge-region beg end
)
2641 (exchange-point-and-mark)
2645 (forward-line (1- ex-count
)))
2647 (vip-enlarge-region (point) (mark))
2649 ;; show text to be deleted and ask for confirmation
2651 (with-output-to-temp-buffer " *delete text*"
2652 (princ (buffer-substring (point) (mark))))
2653 (condition-case conditions
2654 (vip-read-string "[Hit return to continue] ")
2656 (save-excursion (kill-buffer " *delete text*"))
2658 (save-excursion (kill-buffer " *delete text*")))
2660 (if (and (<= ?A ex-buffer
) (<= ex-buffer ?Z
))
2661 (vip-append-to-register
2662 (+ ex-buffer
32) (point) (mark))
2663 (copy-to-register ex-buffer
(point) (mark) nil
)))
2664 (delete-region (point) (mark))))))
2669 (if (and (not ex-variant
) (buffer-modified-p) buffer-file-name
)
2670 (error "No write since last change \(:e! overrides\)"))
2671 (vip-change-mode-to-emacs)
2673 (find-file-noselect (concat default-directory ex-file
)))
2674 (vip-change-mode-to-vi)
2675 (goto-char (point-min))
2678 (with-current-buffer " *ex-working-space*"
2679 (delete-region (point-min) (point-max))
2680 (insert ex-offset
"\n")
2681 (goto-char (point-min)))
2682 (goto-char (vip-get-ex-address))
2683 (beginning-of-line))))
2685 (defun ex-global (variant)
2687 (if (or ex-g-flag ex-g-variant
)
2688 (error "Global within global not allowed")
2696 (error "Missing regular expression for global command"))
2697 (if (string= ex-token
"")
2698 (if (null vip-s-string
) (error "No previous search string")
2699 (setq ex-g-pat vip-s-string
))
2700 (setq ex-g-pat ex-token
2701 vip-s-string ex-token
))
2702 (if (null ex-addresses
)
2703 (setq ex-addresses
(list (point-max) (point-min))))
2704 (let ((marks nil
) (mark-count 0)
2705 com-str
(end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2706 (if (> beg end
) (error "First address exceeds second"))
2708 (vip-enlarge-region beg end
)
2709 (exchange-point-and-mark)
2710 (let ((cont t
) (limit (point-marker)))
2711 (exchange-point-and-mark)
2712 ;; skip the last line if empty
2714 (if (and (eobp) (not (bobp))) (backward-char 1))
2715 (while (and cont
(not (bobp)) (>= (point) limit
))
2719 (let ((found (re-search-backward ex-g-pat
(mark) t
)))
2720 (if (or (and ex-g-flag found
)
2721 (and ex-g-variant
(not found
)))
2724 (setq mark-count
(1+ mark-count
))
2725 (setq marks
(cons (point-marker) marks
)))))
2727 (if (bobp) (setq cont nil
)
2730 (with-current-buffer " *ex-working-space*"
2731 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
2733 (goto-char (car marks
))
2734 ;; report progress of execution on a slow machine.
2735 ;;(message "Executing global command...")
2736 ;;(if (zerop (% mark-count 10))
2737 ;; (message "Executing global command...%d" mark-count))
2739 (setq mark-count
(1- mark-count
))
2740 (setq marks
(cdr marks
)))))
2741 ;;(message "Executing global command...done")))
2743 (defun ex-line (com)
2744 "ex line commands. COM is join, shift-right or shift-left."
2745 (vip-default-ex-addresses)
2747 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
2748 (if (> beg end
) (error "First address exceeds second"))
2750 (vip-enlarge-region beg end
)
2751 (exchange-point-and-mark)
2755 (forward-line ex-count
)))
2757 ;; show text to be joined and ask for confirmation
2759 (with-output-to-temp-buffer " *text*"
2760 (princ (buffer-substring (point) (mark))))
2761 (condition-case conditions
2763 (vip-read-string "[Hit return to continue] ")
2764 (ex-line-subr com
(point) (mark)))
2767 (save-excursion (kill-buffer " *text*")))
2768 (ex-line-subr com
(point) (mark)))
2769 (setq point
(point)))
2770 (goto-char (1- point
))
2771 (beginning-of-line)))
2773 (defun ex-line-subr (com beg end
)
2774 (cond ((string= com
"join")
2775 (goto-char (min beg end
))
2776 (while (and (not (eobp)) (< (point) (max beg end
)))
2778 (if (and (<= (point) (max beg end
)) (not (eobp)))
2781 (delete-region (point) (1- (point)))
2782 (if (not ex-variant
) (fixup-whitespace))))))
2783 ((or (string= com
"right") (string= com
"left"))
2785 (min beg end
) (max beg end
)
2786 (if (string= com
"right") vip-shift-width
(- vip-shift-width
)))
2787 (goto-char (max beg end
))
2794 (if (null ex-addresses
)
2796 (cons (point) nil
)))
2797 (with-current-buffer " *ex-working-space*"
2798 (skip-chars-forward " \t")
2799 (if (looking-at "[a-z]")
2801 (setq char
(following-char))
2803 (skip-chars-forward " \t")
2804 (if (not (looking-at "[\n|]"))
2805 (error "Extra characters at end of \"k\" command")))
2806 (if (looking-at "[\n|]")
2807 (error "\"k\" requires a following letter")
2808 (error "Mark must specify a letter"))))
2810 (goto-char (car ex-addresses
))
2811 (point-to-register (- char
(- ?a ?\C-a
)) nil
))))
2816 (with-current-buffer " *ex-working-space*"
2817 (skip-chars-forward " \t")
2818 (setq char
(char-to-string (following-char)))
2820 (skip-chars-forward " \t")
2821 (if (looking-at "[\n|]") (error "Missing rhs"))
2826 (setq string
(buffer-substring (mark) (point))))
2827 (if (not (lookup-key ex-map char
))
2828 (define-key ex-map char
2829 (or (lookup-key vip-mode-map char
) 'vip-nil
)))
2830 (define-key vip-mode-map char
2836 (list 'execute-kbd-macro string
'count
))))))))
2841 (with-current-buffer " *ex-working-space*"
2842 (skip-chars-forward " \t")
2843 (setq char
(char-to-string (following-char)))
2845 (skip-chars-forward " \t")
2846 (if (not (looking-at "[\n|]")) (error "Macro must be a character")))
2847 (if (not (lookup-key ex-map char
))
2848 (error "That macro wasn't mapped"))
2849 (define-key vip-mode-map char
(lookup-key ex-map char
))
2850 (define-key ex-map char nil
)))
2854 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
2856 (setq vip-use-register ex-buffer
)
2858 (if (= point
0) (vip-Put-back 1) (vip-put-back 1))))
2863 (with-current-buffer " *ex-working-space*"
2864 (skip-chars-forward " \t")
2865 (setq char
(following-char)))
2866 (if (= char ?
!) (kill-emacs t
) (save-buffers-kill-emacs))))
2870 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
2871 (variant nil
) command file
)
2873 (if (not (= point
0)) (with-no-warnings (next-line 1)))
2875 (with-current-buffer " *ex-working-space*"
2876 (skip-chars-forward " \t")
2877 (if (looking-at "!")
2881 (skip-chars-forward " \t")
2884 (setq command
(buffer-substring (mark) (point))))
2886 (re-search-forward "[ \t\n]")
2888 (setq file
(buffer-substring (point) (mark)))))
2890 (shell-command command t
)
2892 (insert-file file
)))))
2896 (read-variable "Variable: ")
2897 (eval (read-minibuffer "Value: ")))))
2901 (vip-change-mode-to-emacs)
2904 (defun ex-substitute (&optional repeat r-flag
)
2906 If REPEAT use previous reg-exp which is ex-reg-exp or
2908 (let (pat repl
(opt-g nil
) (opt-c nil
) (matched-pos nil
))
2909 (if repeat
(setq ex-token nil
) (vip-get-ex-pat))
2911 (setq pat
(if r-flag vip-s-string ex-reg-exp
)
2913 (setq pat
(if (string= ex-token
"") vip-s-string ex-token
))
2914 (setq vip-s-string pat
2922 (while (vip-get-ex-opt-gc)
2923 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
2927 (if ex-addresses
(goto-char (car ex-addresses
)))
2929 (forward-line (1- ex-count
))
2930 (setq ex-addresses
(cons (point) (cons (mark) nil
))))
2931 (if (null ex-addresses
)
2932 (setq ex-addresses
(cons (point) (cons (point) nil
)))
2933 (if (null (cdr ex-addresses
))
2934 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
2936 (let ((beg (car ex-addresses
)) (end (car (cdr ex-addresses
)))
2939 (vip-enlarge-region beg end
)
2940 (let ((limit (save-excursion
2941 (goto-char (max (point) (mark)))
2943 (goto-char (min (point) (mark)))
2944 (while (< (point) limit
)
2946 (setq eol-mark
(point-marker))
2950 (while (and (not (eolp))
2951 (re-search-forward pat eol-mark t
))
2952 (if (or (not opt-c
) (y-or-n-p "Replace? "))
2954 (setq matched-pos
(point))
2955 (replace-match repl
))))
2958 (if (and (re-search-forward pat eol-mark t
)
2959 (or (not opt-c
) (y-or-n-p "Replace? ")))
2961 (setq matched-pos
(point))
2962 (replace-match repl
)))
2965 (if matched-pos
(goto-char matched-pos
))
2967 (if opt-c
(message "done"))))
2972 (with-current-buffer " *ex-working-space*"
2973 (skip-chars-forward " \t")
2975 (skip-chars-forward "^ |\t\n")
2976 (setq tag
(buffer-substring (mark) (point))))
2977 (if (not (string= tag
"")) (setq ex-tag tag
))
2978 (vip-change-mode-to-emacs)
2979 (condition-case conditions
2981 (if (string= tag
"")
2983 (find-tag-other-window ex-tag
))
2984 (vip-change-mode-to-vi))
2986 (vip-change-mode-to-vi)
2987 (vip-message-conditions conditions
)))))
2989 (defun ex-write (q-flag)
2991 (vip-default-ex-addresses t
)
2993 (if (string= ex-file
"")
2995 (if (null buffer-file-name
)
2996 (error "No file associated with this buffer"))
2997 (setq ex-file buffer-file-name
))
2998 (setq ex-file
(expand-file-name ex-file
)))
2999 (if (and (not (string= ex-file
(buffer-file-name)))
3000 (file-exists-p ex-file
)
3002 (error "\"%s\" File exists - use w! to override" ex-file
))
3003 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3004 (if (> beg end
) (error "First address exceeds second"))
3006 (vip-enlarge-region beg end
)
3007 (write-region (point) (mark) ex-file ex-append t
)))
3008 (if (null buffer-file-name
) (setq buffer-file-name ex-file
))
3009 (if q-flag
(save-buffers-kill-emacs)))
3013 (vip-default-ex-addresses)
3015 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3016 (if (> beg end
) (error "First address exceeds second"))
3018 (vip-enlarge-region beg end
)
3019 (exchange-point-and-mark)
3020 (if (or ex-g-flag ex-g-variant
) (error "Can't yank within global"))
3024 (forward-line (1- ex-count
)))
3026 (vip-enlarge-region (point) (mark))
3027 (if ex-flag
(error "Extra characters at end of command"))
3029 (copy-to-register ex-buffer
(point) (mark) nil
))
3030 (copy-region-as-kill (point) (mark)))))
3032 (defun ex-command ()
3033 "execute shell command"
3035 (with-current-buffer " *ex-working-space*"
3036 (skip-chars-forward " \t")
3039 (setq command
(buffer-substring (mark) (point))))
3040 (if (null ex-addresses
)
3041 (shell-command command
)
3042 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3043 (if (null beg
) (setq beg end
))
3047 (vip-enlarge-region (point) (mark))
3048 (shell-command-on-region (point) (mark) command t t
))
3051 (defun ex-line-no ()
3056 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
3058 (if (file-exists-p vip-startup-file
) (load vip-startup-file
))
3062 ;;; vip.el ends here