1 ;;; vip.el --- a VI Package for GNU Emacs
3 ;; Copyright (C) 1986-1988, 1992-1993, 1998, 2001-2013 Free Software
6 ;; Author: Masahiko Sato <ms@sail.stanford.edu>
7 ;; Keywords: emulations
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; A full-featured vi(1) emulator.
28 ;; In Japan, the author's address is: masahiko@sato.riec.tohoku.junet
30 ;; Send suggestions and bug reports to one of the above addresses.
31 ;; When you report a bug, be sure to include the version number of VIP and
32 ;; Emacs you are using.
34 ;; Execute info command by typing "M-x info" to get information on VIP.
39 "A VI Package for GNU Emacs."
45 (defvar vip-emacs-local-map nil
46 "Local map used in Emacs mode. (Buffer-specific.)")
48 (defvar vip-insert-local-map nil
49 "Local map used in insert command mode. (Buffer-specific.)")
51 (make-variable-buffer-local 'vip-emacs-local-map
)
52 (make-variable-buffer-local 'vip-insert-local-map
)
54 (defvar vip-insert-point nil
55 "Remember insert point as a marker. (Buffer-specific.)")
57 (set-default 'vip-insert-point
(make-marker))
58 (make-variable-buffer-local 'vip-insert-point
)
60 (defvar vip-com-point nil
61 "Remember com point as a marker. (Buffer-specific.)")
63 (set-default 'vip-com-point
(make-marker))
64 (make-variable-buffer-local 'vip-com-point
)
66 (defvar vip-current-mode nil
67 "Current mode. One of `emacs-mode', `vi-mode', `insert-mode'.")
69 (make-variable-buffer-local 'vip-current-mode
)
70 (setq-default vip-current-mode
'emacs-mode
)
72 (defvar vip-emacs-mode-line-buffer-identification nil
73 "Value of mode-line-buffer-identification in Emacs mode within vip.")
74 (make-variable-buffer-local 'vip-emacs-mode-line-buffer-identification
)
75 (setq-default vip-emacs-mode-line-buffer-identification
78 (defvar vip-current-major-mode nil
79 "vip-current-major-mode is the major-mode vi considers it is now.
82 (make-variable-buffer-local 'vip-current-major-mode
)
84 (defvar vip-last-shell-com nil
85 "Last shell command executed by ! command.")
87 (defvar vip-use-register nil
88 "Name of register to store deleted or yanked strings.")
91 "How to reexecute last destructive command. Value is list (M-COM VAL COM).")
93 (defcustom vip-shift-width
8
94 "The number of columns shifted by > and < command."
98 (defcustom vip-re-replace nil
99 "If t then do regexp replace, if nil then do string replace."
103 (defvar vip-d-char nil
104 "The character remembered by the vi \"r\" command.")
106 (defvar vip-f-char nil
107 "For use by \";\" command.")
109 (defvar vip-F-char nil
110 "For use by \".\" command.")
112 (defvar vip-f-forward nil
113 "For use by \";\" command.")
115 (defvar vip-f-offset nil
116 "For use by \";\" command.")
118 (defcustom vip-search-wrap-around t
119 "If t, search wraps around."
123 (defcustom vip-re-search nil
124 "If t, search is reg-exp search, otherwise vanilla search."
128 (defvar vip-s-string nil
129 "Last vip search string.")
131 (defvar vip-s-forward nil
132 "If t, search is forward.")
134 (defcustom vip-case-fold-search nil
135 "If t, search ignores cases."
139 (defcustom vip-re-query-replace nil
140 "If t then do regexp replace, if nil then do string replace."
144 (defcustom vip-open-with-indent nil
145 "If t, indent when open a new line."
149 (defcustom vip-help-in-insert-mode nil
150 "If t then C-h is bound to help-command in insert mode.
151 If nil then it is bound to `delete-backward-char'."
155 (defvar vip-quote-string
"> "
156 "String inserted at the beginning of region.")
158 (defvar vip-tags-file-name
"TAGS")
160 (defvar vip-inhibit-startup-message nil
)
162 (defvar vip-startup-file
(locate-user-emacs-file "vip" ".vip")
163 "Filename used as startup file for vip.")
168 (let ((map (make-keymap)))
169 (define-key map
"\C-a" 'beginning-of-line
)
170 (define-key map
"\C-b" 'vip-scroll-back
)
171 (define-key map
"\C-c" 'vip-ctl-c
)
172 (define-key map
"\C-d" 'vip-scroll-up
)
173 (define-key map
"\C-e" 'vip-scroll-up-one
)
174 (define-key map
"\C-f" 'vip-scroll
)
175 (define-key map
"\C-g" 'vip-keyboard-quit
)
176 (define-key map
"\C-h" 'help-command
)
177 (define-key map
"\C-m" 'vip-scroll-back
)
178 (define-key map
"\C-n" 'vip-other-window
)
179 (define-key map
"\C-o" 'vip-open-line-at-point
)
180 (define-key map
"\C-u" 'vip-scroll-down
)
181 (define-key map
"\C-x" 'vip-ctl-x
)
182 (define-key map
"\C-y" 'vip-scroll-down-one
)
183 (define-key map
"\C-z" 'vip-change-mode-to-emacs
)
184 (define-key map
"\e" 'vip-ESC
)
186 (define-key map
[?\S-\
] 'vip-scroll-back
)
187 (define-key map
" " 'vip-scroll
)
188 (define-key map
"!" 'vip-command-argument
)
189 (define-key map
"\"" 'vip-command-argument
)
190 (define-key map
"#" 'vip-command-argument
)
191 (define-key map
"$" 'vip-goto-eol
)
192 (define-key map
"%" 'vip-paren-match
)
193 (define-key map
"&" 'vip-nil
)
194 (define-key map
"'" 'vip-goto-mark-and-skip-white
)
195 (define-key map
"(" 'vip-backward-sentence
)
196 (define-key map
")" 'vip-forward-sentence
)
197 (define-key map
"*" 'call-last-kbd-macro
)
198 (define-key map
"+" 'vip-next-line-at-bol
)
199 (define-key map
"," 'vip-repeat-find-opposite
)
200 (define-key map
"-" 'vip-previous-line-at-bol
)
201 (define-key map
"." 'vip-repeat
)
202 (define-key map
"/" 'vip-search-forward
)
204 (define-key map
"0" 'vip-beginning-of-line
)
205 (define-key map
"1" 'vip-digit-argument
)
206 (define-key map
"2" 'vip-digit-argument
)
207 (define-key map
"3" 'vip-digit-argument
)
208 (define-key map
"4" 'vip-digit-argument
)
209 (define-key map
"5" 'vip-digit-argument
)
210 (define-key map
"6" 'vip-digit-argument
)
211 (define-key map
"7" 'vip-digit-argument
)
212 (define-key map
"8" 'vip-digit-argument
)
213 (define-key map
"9" 'vip-digit-argument
)
215 (define-key map
":" 'vip-ex
)
216 (define-key map
";" 'vip-repeat-find
)
217 (define-key map
"<" 'vip-command-argument
)
218 (define-key map
"=" 'vip-command-argument
)
219 (define-key map
">" 'vip-command-argument
)
220 (define-key map
"?" 'vip-search-backward
)
221 (define-key map
"@" 'vip-nil
)
223 (define-key map
"A" 'vip-Append
)
224 (define-key map
"B" 'vip-backward-Word
)
225 (define-key map
"C" 'vip-ctl-c-equivalent
)
226 (define-key map
"D" 'vip-kill-line
)
227 (define-key map
"E" 'vip-end-of-Word
)
228 (define-key map
"F" 'vip-find-char-backward
)
229 (define-key map
"G" 'vip-goto-line
)
230 (define-key map
"H" 'vip-window-top
)
231 (define-key map
"I" 'vip-Insert
)
232 (define-key map
"J" 'vip-join-lines
)
233 (define-key map
"K" 'vip-kill-buffer
)
234 (define-key map
"L" 'vip-window-bottom
)
235 (define-key map
"M" 'vip-window-middle
)
236 (define-key map
"N" 'vip-search-Next
)
237 (define-key map
"O" 'vip-Open-line
)
238 (define-key map
"P" 'vip-Put-back
)
239 (define-key map
"Q" 'vip-query-replace
)
240 (define-key map
"R" 'vip-replace-string
)
241 (define-key map
"S" 'vip-switch-to-buffer-other-window
)
242 (define-key map
"T" 'vip-goto-char-backward
)
243 (define-key map
"U" 'vip-nil
)
244 (define-key map
"V" 'vip-find-file-other-window
)
245 (define-key map
"W" 'vip-forward-Word
)
246 (define-key map
"X" 'vip-ctl-x-equivalent
)
247 (define-key map
"Y" 'vip-yank-line
)
248 (define-key map
"ZZ" 'save-buffers-kill-emacs
)
250 (define-key map
"[" 'vip-nil
)
251 (define-key map
"\\" 'vip-escape-to-emacs
)
252 (define-key map
"]" 'vip-nil
)
253 (define-key map
"^" 'vip-bol-and-skip-white
)
254 (define-key map
"_" 'vip-nil
)
255 (define-key map
"`" 'vip-goto-mark
)
257 (define-key map
"a" 'vip-append
)
258 (define-key map
"b" 'vip-backward-word
)
259 (define-key map
"c" 'vip-command-argument
)
260 (define-key map
"d" 'vip-command-argument
)
261 (define-key map
"e" 'vip-end-of-word
)
262 (define-key map
"f" 'vip-find-char-forward
)
263 (define-key map
"g" 'vip-info-on-file
)
264 (define-key map
"h" 'vip-backward-char
)
265 (define-key map
"i" 'vip-insert
)
266 (define-key map
"j" 'vip-next-line
)
267 (define-key map
"k" 'vip-previous-line
)
268 (define-key map
"l" 'vip-forward-char
)
269 (define-key map
"m" 'vip-mark-point
)
270 (define-key map
"n" 'vip-search-next
)
271 (define-key map
"o" 'vip-open-line
)
272 (define-key map
"p" 'vip-put-back
)
273 (define-key map
"q" 'vip-nil
)
274 (define-key map
"r" 'vip-replace-char
)
275 (define-key map
"s" 'vip-switch-to-buffer
)
276 (define-key map
"t" 'vip-goto-char-forward
)
277 (define-key map
"u" 'vip-undo
)
278 (define-key map
"v" 'vip-find-file
)
279 (define-key map
"w" 'vip-forward-word
)
280 (define-key map
"x" 'vip-delete-char
)
281 (define-key map
"y" 'vip-command-argument
)
282 (define-key map
"zH" 'vip-line-to-top
)
283 (define-key map
"zM" 'vip-line-to-middle
)
284 (define-key map
"zL" 'vip-line-to-bottom
)
285 (define-key map
"z\C-m" 'vip-line-to-top
)
286 (define-key map
"z." 'vip-line-to-middle
)
287 (define-key map
"z-" 'vip-line-to-bottom
)
289 (define-key map
"{" 'vip-backward-paragraph
)
290 (define-key map
"|" 'vip-goto-col
)
291 (define-key map
"}" 'vip-forward-paragraph
)
292 (define-key map
"~" 'vip-nil
)
293 (define-key map
"\177" 'vip-delete-backward-char
)
296 (defun vip-version ()
298 (message "VIP version 3.5 of September 15, 1987"))
305 "Set up bindings for C-x 7 and C-z that are useful for VIP users."
306 (define-key ctl-x-map
"7" 'vip-buffer-in-two-windows
)
307 (global-set-key "\C-z" 'vip-change-mode-to-vi
))
309 (defmacro vip-loop
(count body
)
310 "(COUNT BODY) Execute BODY COUNT times."
311 `(let ((count ,count
))
314 (setq count
(1- count
)))))
316 (defun vip-push-mark-silent (&optional location
)
317 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
321 (setq mark-ring
(cons (copy-marker (mark-marker)) mark-ring
))
322 (if (> (length mark-ring
) mark-ring-max
)
324 (move-marker (car (nthcdr mark-ring-max mark-ring
)) nil
)
325 (setcdr (nthcdr (1- mark-ring-max
) mark-ring
) nil
))))
326 (set-mark (or location
(point))))
328 (defun vip-goto-col (arg)
329 "Go to ARG's column."
331 (let ((val (vip-p-val arg
))
332 (com (vip-getcom arg
)))
335 (if (> val
(1+ (current-column))) (error "")))
336 (if com
(move-marker vip-com-point
(point)))
338 (forward-char (1- val
))
339 (if com
(vip-execute-com 'vip-goto-col val com
))))
341 (defun vip-copy-keymap (map)
342 (if (null map
) (make-sparse-keymap) (copy-keymap map
)))
347 (defun vip-change-mode (new-mode)
348 "Change mode to NEW-MODE---either emacs-mode, vi-mode, or insert-mode."
349 (or (eq new-mode vip-current-mode
)
351 (cond ((eq new-mode
'vi-mode
)
352 (if (eq vip-current-mode
'insert-mode
)
354 (vip-copy-region-as-kill (point) vip-insert-point
)
355 (vip-repeat-insert-command))
356 (setq vip-emacs-local-map
(current-local-map)
357 vip-emacs-mode-line-buffer-identification
358 mode-line-buffer-identification
359 vip-insert-local-map
(vip-copy-keymap
360 (current-local-map))))
361 (vip-change-mode-line "Vi: ")
362 (use-local-map vip-mode-map
))
363 ((eq new-mode
'insert-mode
)
364 (move-marker vip-insert-point
(point))
365 (if (eq vip-current-mode
'emacs-mode
)
366 (setq vip-emacs-local-map
(current-local-map)
367 vip-emacs-mode-line-buffer-identification
368 mode-line-buffer-identification
369 vip-insert-local-map
(vip-copy-keymap
370 (current-local-map)))
371 (setq vip-insert-local-map
(vip-copy-keymap
372 vip-emacs-local-map
)))
373 (vip-change-mode-line "Insert")
374 (use-local-map vip-insert-local-map
)
375 (define-key vip-insert-local-map
"\e" 'vip-change-mode-to-vi
)
376 (define-key vip-insert-local-map
"\C-z" 'vip-ESC
)
377 (define-key vip-insert-local-map
"\C-h"
378 (if vip-help-in-insert-mode
'help-command
379 'delete-backward-char
))
380 (define-key vip-insert-local-map
"\C-w"
381 'vip-delete-backward-word
))
382 ((eq new-mode
'emacs-mode
)
383 (vip-change-mode-line "Emacs:")
384 (use-local-map vip-emacs-local-map
)))
385 (setq vip-current-mode new-mode
)
386 (force-mode-line-update))))
388 (defun vip-copy-region-as-kill (beg end
)
389 "If BEG and END do not belong to the same buffer, it copies empty region."
391 (copy-region-as-kill beg end
)
392 (error (copy-region-as-kill beg beg
))))
394 (defun vip-change-mode-line (string)
395 "Assuming that the mode line format contains the string \"Emacs:\", this
396 function replaces the string by \"Vi: \" etc."
397 (setq mode-line-buffer-identification
398 (if (string= string
"Emacs:")
399 vip-emacs-mode-line-buffer-identification
400 (list (concat string
" %17b")))))
404 "Turn on VIP emulation of VI."
406 (if (not vip-inhibit-startup-message
)
408 (switch-to-buffer "VIP Startup Message")
411 "VIP is a Vi emulation package for GNU Emacs. VIP provides most Vi commands
412 including Ex commands. VIP is however different from Vi in several points.
413 You can get more information on VIP by:
414 1. Typing `M-x info' and selecting menu item \"vip\".
415 2. Typing `C-h k' followed by a key whose description you want.
416 3. Printing VIP manual which can be found as GNU/man/vip.texinfo
417 4. Printing VIP Reference Card which can be found as GNU/etc/vipcard.tex
419 This startup message appears whenever you load VIP unless you type `y' now.
420 Type `n' to quit this window for now.\n")
421 (goto-char (point-min))
422 (if (y-or-n-p "Inhibit VIP startup message? ")
426 (substitute-in-file-name vip-startup-file
))
427 (goto-char (point-max))
428 (insert "\n(setq vip-inhibit-startup-message t)\n")
430 (kill-buffer (current-buffer)))
431 (message "VIP startup message inhibited.")
433 (kill-buffer (current-buffer))
435 (setq vip-inhibit-startup-message t
)))
436 (vip-change-mode-to-vi))
438 (defun vip-change-mode-to-vi ()
439 "Change mode to vi mode."
441 (vip-change-mode 'vi-mode
))
443 (defun vip-change-mode-to-insert ()
444 "Change mode to insert mode."
446 (vip-change-mode 'insert-mode
))
448 (defun vip-change-mode-to-emacs ()
449 "Change mode to Emacs mode."
451 (vip-change-mode 'emacs-mode
))
454 ;; escape to emacs mode temporarily
456 (defun vip-escape-to-emacs (arg &optional events
)
457 "Escape to Emacs mode for one Emacs command.
458 ARG is used as the prefix value for the executed command. If
459 EVENTS is a list of events, which become the beginning of the command."
461 (let (com key
(old-map (current-local-map)))
462 (if events
(setq unread-command-events events
))
463 (setq prefix-arg arg
)
464 (use-local-map vip-emacs-local-map
)
466 (setq com
(key-binding (setq key
(read-key-sequence nil
))))
467 (use-local-map old-map
))
468 (command-execute com prefix-arg
)
469 (setq prefix-arg nil
) ;; reset prefix arg
472 (defun vip-message-conditions (conditions)
473 "Print CONDITIONS as a message."
474 (let ((case (car conditions
)) (msg (cdr conditions
)))
477 (message "%s %s" case
(prin1-to-string msg
)))
481 "Emulate ESC key in Emacs mode."
483 (vip-escape-to-emacs arg
'(?\e
)))
485 (defun vip-ctl-c (arg)
486 "Emulate C-c key in Emacs mode."
488 (vip-escape-to-emacs arg
'(?\C-c
)))
490 (defun vip-ctl-x (arg)
491 "Emulate C-x key in Emacs mode."
493 (vip-escape-to-emacs arg
'(?\C-x
)))
495 (defun vip-ctl-h (arg)
496 "Emulate C-h key in Emacs mode."
498 (vip-escape-to-emacs arg
'(?\C-h
)))
501 ;; prefix argument for vi mode
503 ;; In vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
504 ;; represents the numeric value of the prefix argument and COM represents
505 ;; command prefix such as "c", "d", "m" and "y".
507 (defun vip-prefix-arg-value (char value com
)
508 "Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value
509 obtained so far, and COM is the command part obtained so far."
510 (while (and (>= char ?
0) (<= char ?
9))
511 (setq value
(+ (* (if (numberp value
) value
0) 10) (- char ?
0)))
512 (setq char
(read-char)))
513 (setq prefix-arg value
)
514 (if com
(setq prefix-arg
(cons prefix-arg com
)))
516 (vip-describe-arg prefix-arg
)
517 (setq char
(read-char)))
518 (setq unread-command-events
(list char
)))
520 (defun vip-prefix-arg-com (char value com
)
521 "Vi operator as prefix argument."
524 (or (= char ?c
) (= char ?d
) (= char ?y
)
525 (= char ?
!) (= char ?
<) (= char ?
>) (= char ?
=)
526 (= char ?
#) (= char ?r
) (= char ?R
) (= char ?
\")))
528 ;; this means that we already have a command character, so we
529 ;; construct a com list and exit while. however, if char is "
532 ;; new com is (CHAR . OLDCOM)
533 (if (or (= char ?
#) (= char ?
\")) (error ""))
534 (setq com
(cons char com
))
536 ;; if com is nil we set com as char, and read more. again, if char
537 ;; is ", we read the name of register and store it in vip-use-register.
538 ;; if char is !, =, or #, a complete com is formed so we exit while.
539 (cond ((or (= char ?
!) (= char ?
=))
541 (setq char
(read-char))
544 ;; read a char and encode it as com
545 (setq com
(+ 128 (read-char)))
546 (setq char
(read-char))
548 ((or (= char ?
<) (= char ?
>))
550 (setq char
(read-char))
551 (if (= com char
) (setq com
(cons char com
)))
554 (let ((reg (read-char)))
555 (if (or (and (<= ?A reg
) (<= reg ?z
))
556 (and (<= ?
1 reg
) (<= reg ?
9)))
557 (setq vip-use-register reg
)
559 (setq char
(read-char))))
562 (setq char
(read-char)))))))
564 ;; com is a single char, so we construct prefix-arg
565 ;; and if char is ?, describe prefix arg, otherwise exit by
566 ;; pushing the char back
568 (setq prefix-arg
(cons value com
))
570 (vip-describe-arg prefix-arg
)
571 (setq char
(read-char)))
572 (setq unread-command-events
(list char
)))
573 ;; as com is non-nil, this means that we have a command to execute
574 (if (or (= (car com
) ?r
) (= (car com
) ?R
))
575 ;; execute appropriate region command.
576 (let ((char (car com
)) (com (cdr com
)))
577 (setq prefix-arg
(cons value com
))
578 (if (= char ?r
) (vip-region prefix-arg
)
579 (vip-Region prefix-arg
))
581 (setq prefix-arg nil
))
582 ;; otherwise, reset prefix arg and call appropriate command
583 (setq value
(if (null value
) 1 value
))
584 (setq prefix-arg nil
)
585 (cond ((equal com
'(?c . ?c
)) (vip-line (cons value ?C
)))
586 ((equal com
'(?d . ?d
)) (vip-line (cons value ?D
)))
587 ((equal com
'(?d . ?y
)) (vip-yank-defun))
588 ((equal com
'(?y . ?y
)) (vip-line (cons value ?Y
)))
589 ((equal com
'(?
< . ?
<)) (vip-line (cons value ?
<)))
590 ((equal com
'(?
> . ?
>)) (vip-line (cons value ?
>)))
591 ((equal com
'(?
! . ?
!)) (vip-line (cons value ?
!)))
592 ((equal com
'(?
= . ?
=)) (vip-line (cons value ?
=)))
595 (defun vip-describe-arg (arg)
597 (setq val
(vip-P-val arg
)
598 com
(vip-getcom arg
))
601 (message "Value is nil, and command is nil.")
602 (message "Value is nil, and command is %c." com
))
604 (message "Value is %d, and command is nil." val
)
605 (message "Value is %d, and command is %c." val com
)))))
607 (defun vip-digit-argument (arg)
608 "Begin numeric argument for the next command."
610 (vip-prefix-arg-value last-command-event nil
611 (if (consp arg
) (cdr arg
) nil
)))
613 (defun vip-command-argument (arg)
614 "Accept a motion command as an argument."
616 (condition-case conditions
619 (cond ((null arg
) nil
)
620 ((consp arg
) (car arg
))
622 (t (error "strange arg")))
623 (cond ((null arg
) nil
)
624 ((consp arg
) (cdr arg
))
626 (t (error "strange arg"))))
628 (setq vip-use-register nil
)
629 (signal 'quit nil
))))
631 (defun vip-p-val (arg)
632 "Get value part of prefix-argument ARG."
634 ((consp arg
) (if (null (car arg
)) 1 (car arg
)))
637 (defun vip-P-val (arg)
638 "Get value part of prefix-argument ARG."
639 (cond ((consp arg
) (car arg
))
642 (defun vip-getcom (arg)
643 "Get com part of prefix-argument ARG."
644 (cond ((null arg
) nil
)
645 ((consp arg
) (cdr arg
))
648 (defun vip-getCom (arg)
649 "Get com part of prefix-argument ARG and modify it."
650 (let ((com (vip-getcom arg
)))
651 (cond ((equal com ?c
) ?C
)
657 ;; repeat last destructive command
659 (defun vip-append-to-register (reg start end
)
660 "Append region to text in register REG.
661 START and END are buffer positions indicating what to append."
662 (set-register reg
(concat (or (get-register reg
) "")
663 (buffer-substring start end
))))
665 (defun vip-execute-com (m-com val com
)
666 "(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set
667 to vip-d-com for later use by vip-repeat"
668 (let ((reg vip-use-register
))
670 (cond ((= com ?c
) (vip-change vip-com-point
(point)))
671 ((= com
(- ?c
)) (vip-change-subr vip-com-point
(point)))
672 ((or (= com ?C
) (= com
(- ?C
)))
674 (set-mark vip-com-point
)
675 (vip-enlarge-region (mark) (point))
678 (cond ((and (<= ?a vip-use-register
)
679 (<= vip-use-register ?z
))
681 vip-use-register
(mark) (point) nil
))
682 ((and (<= ?A vip-use-register
)
683 (<= vip-use-register ?Z
))
684 (vip-append-to-register
685 (+ vip-use-register
32) (mark) (point)))
686 (t (setq vip-use-register nil
)
688 (setq vip-use-register nil
)))
689 (delete-region (mark) (point)))
691 (if (= com ?C
) (vip-change-mode-to-insert) (yank)))
695 (cond ((and (<= ?a vip-use-register
)
696 (<= vip-use-register ?z
))
698 vip-use-register vip-com-point
(point) nil
))
699 ((and (<= ?A vip-use-register
)
700 (<= vip-use-register ?Z
))
701 (vip-append-to-register
702 (+ vip-use-register
32) vip-com-point
(point)))
703 (t (setq vip-use-register nil
)
705 (setq vip-use-register nil
)))
707 (if (eq last-command
'd-command
) 'kill-region nil
))
708 (kill-region vip-com-point
(point))
709 (setq this-command
'd-command
))
712 (set-mark vip-com-point
)
713 (vip-enlarge-region (mark) (point))
716 (cond ((and (<= ?a vip-use-register
)
717 (<= vip-use-register ?z
))
719 vip-use-register
(mark) (point) nil
))
720 ((and (<= ?A vip-use-register
)
721 (<= vip-use-register ?Z
))
722 (vip-append-to-register
723 (+ vip-use-register
32) (mark) (point)))
724 (t (setq vip-use-register nil
)
726 (setq vip-use-register nil
)))
728 (if (eq last-command
'D-command
) 'kill-region nil
))
729 (kill-region (mark) (point))
730 (if (eq m-com
'vip-line
) (setq this-command
'D-command
)))
731 (back-to-indentation))
735 (cond ((and (<= ?a vip-use-register
)
736 (<= vip-use-register ?z
))
738 vip-use-register vip-com-point
(point) nil
))
739 ((and (<= ?A vip-use-register
)
740 (<= vip-use-register ?Z
))
741 (vip-append-to-register
742 (+ vip-use-register
32) vip-com-point
(point)))
743 (t (setq vip-use-register nil
)
745 (setq vip-use-register nil
)))
746 (setq last-command nil
)
747 (copy-region-as-kill vip-com-point
(point))
748 (goto-char vip-com-point
))
751 (set-mark vip-com-point
)
752 (vip-enlarge-region (mark) (point))
755 (cond ((and (<= ?a vip-use-register
)
756 (<= vip-use-register ?z
))
758 vip-use-register
(mark) (point) nil
))
759 ((and (<= ?A vip-use-register
)
760 (<= vip-use-register ?Z
))
761 (vip-append-to-register
762 (+ vip-use-register
32) (mark) (point)))
763 (t (setq vip-use-register nil
)
765 (setq vip-use-register nil
)))
766 (setq last-command nil
)
767 (copy-region-as-kill (mark) (point)))
768 (goto-char vip-com-point
))
769 ((or (= com ?
!) (= com
(- ?
!)))
771 (set-mark vip-com-point
)
772 (vip-enlarge-region (mark) (point))
773 (shell-command-on-region
776 (setq vip-last-shell-com
(vip-read-string "!"))
781 (set-mark vip-com-point
)
782 (vip-enlarge-region (mark) (point))
783 (if (> (mark) (point)) (exchange-point-and-mark))
784 (indent-region (mark) (point) nil
)))
787 (set-mark vip-com-point
)
788 (vip-enlarge-region (mark) (point))
789 (indent-rigidly (mark) (point) (- vip-shift-width
)))
790 (goto-char vip-com-point
))
793 (set-mark vip-com-point
)
794 (vip-enlarge-region (mark) (point))
795 (indent-rigidly (mark) (point) vip-shift-width
))
796 (goto-char vip-com-point
))
798 ;; this is special command #
799 (vip-special-prefix-com (- com
128)))))
800 (setq vip-d-com
(list m-com val
(if (or (= com ?c
) (= com ?C
) (= com ?
!))
804 (defun vip-repeat (arg)
805 "(ARG) Re-execute last destructive command. vip-d-com has the form
806 \(COM ARG CH REG), where COM is the command to be re-executed, ARG is the
807 argument for COM, CH is a flag for repeat, and REG is optional and if exists
808 is the name of the register for COM."
810 (if (eq last-command
'vip-undo
)
811 ;; if the last command was vip-undo, then undo-more
813 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
814 ;; its prefix value is used as new prefix value for the command.
815 (let ((m-com (car vip-d-com
))
816 (val (vip-P-val arg
))
817 (com (car (cdr (cdr vip-d-com
))))
818 (reg (nth 3 vip-d-com
)))
819 (if (null val
) (setq val
(car (cdr vip-d-com
))))
820 (if (null m-com
) (error "No previous command to repeat"))
821 (setq vip-use-register reg
)
822 (funcall m-com
(cons val com
)))))
824 (defun vip-special-prefix-com (char)
825 "This command is invoked interactively by the key sequence #<char>"
827 (downcase-region (min vip-com-point
(point))
828 (max vip-com-point
(point))))
830 (upcase-region (min vip-com-point
(point))
831 (max vip-com-point
(point))))
833 (set-mark vip-com-point
)
834 (vip-global-execute))
836 (set-mark vip-com-point
)
838 ((= char ?s
) (ispell-region vip-com-point
(point)))))
844 "Undo previous change."
849 (setq this-command
'vip-undo
))
851 (defun vip-undo-more ()
852 "Continue undoing previous changes."
853 (message "undo more!")
855 (setq this-command
'vip-undo
))
860 (defun vip-string-tail (str)
861 (if (or (null str
) (string= str
"")) nil
864 (defun vip-yank-defun ()
866 (copy-region-as-kill (point) (mark)))
868 (defun vip-enlarge-region (beg end
)
869 "Enlarge region between BEG and END."
871 (progn (goto-char beg
) (set-mark end
))
875 (exchange-point-and-mark)
876 (if (or (not (eobp)) (not (bolp))) (with-no-warnings (next-line 1)))
878 (if (> beg end
) (exchange-point-and-mark)))
880 (defun vip-global-execute ()
881 "Call last keyboard macro for each line in the region."
882 (if (> (point) (mark)) (exchange-point-and-mark))
884 (call-last-kbd-macro)
885 (while (< (point) (mark))
888 (call-last-kbd-macro)))
890 (defun vip-quote-region ()
891 "Quote region by inserting the user supplied string at the beginning of
892 each line in the region."
893 (setq vip-quote-string
895 (vip-read-string (format "quote string (default %s): "
897 (if (string= str
"") vip-quote-string str
)))
898 (vip-enlarge-region (point) (mark))
899 (if (> (point) (mark)) (exchange-point-and-mark))
900 (insert vip-quote-string
)
903 (while (and (< (point) (mark)) (bolp))
904 (insert vip-quote-string
)
908 (defun vip-end-with-a-newline-p (string)
909 "Check if the string ends with a newline."
910 (or (string= string
"")
911 (= (aref string
(1- (length string
))) ?
\n)))
913 (defvar vip-save-minibuffer-local-map
)
915 (defun vip-read-string (prompt &optional init
)
916 (setq vip-save-minibuffer-local-map
(copy-keymap minibuffer-local-map
))
917 (define-key minibuffer-local-map
"\C-h" 'backward-char
)
918 (define-key minibuffer-local-map
"\C-w" 'backward-word
)
919 (define-key minibuffer-local-map
"\e" 'exit-minibuffer
)
921 (condition-case conditions
922 (setq str
(read-string prompt init
))
924 (setq minibuffer-local-map vip-save-minibuffer-local-map
)
926 (setq minibuffer-local-map vip-save-minibuffer-local-map
)
930 ;; insertion commands
932 (defun vip-repeat-insert-command ()
933 "This function is called when mode changes from insertion mode to
934 vi command mode. It will repeat the insertion command if original insertion
935 command was invoked with argument > 1."
936 (let ((i-com (car vip-d-com
)) (val (car (cdr vip-d-com
))))
937 (if (and val
(> val
1)) ;; first check that val is non-nil
939 (setq vip-d-com
(list i-com
(1- val
) ?r
))
941 (setq vip-d-com
(list i-com val ?r
))))))
943 (defun vip-insert (arg) ""
945 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
946 (setq vip-d-com
(list 'vip-insert val ?r
))
947 (if com
(vip-loop val
(yank))
948 (vip-change-mode-to-insert))))
950 (defun vip-append (arg)
951 "Append after point."
953 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
954 (setq vip-d-com
(list 'vip-append val ?r
))
955 (if (not (eolp)) (forward-char))
957 (vip-loop val
(yank))
958 (vip-change-mode-to-insert))))
960 (defun vip-Append (arg)
961 "Append at end of line."
963 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
964 (setq vip-d-com
(list 'vip-Append val ?r
))
967 (vip-loop val
(yank))
968 (vip-change-mode-to-insert))))
970 (defun vip-Insert (arg)
971 "Insert before first non-white."
973 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
974 (setq vip-d-com
(list 'vip-Insert val ?r
))
975 (back-to-indentation)
977 (vip-loop val
(yank))
978 (vip-change-mode-to-insert))))
980 (defun vip-open-line (arg)
983 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
984 (setq vip-d-com
(list 'vip-open-line val ?r
))
985 (let ((col (current-indentation)))
991 (if vip-open-with-indent
(indent-to col
))
995 (if vip-open-with-indent
(indent-to col
))
996 (vip-change-mode-to-insert)))))
998 (defun vip-Open-line (arg)
1001 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1002 (setq vip-d-com
(list 'vip-Open-line val ?r
))
1003 (let ((col (current-indentation)))
1009 (if vip-open-with-indent
(indent-to col
))
1013 (if vip-open-with-indent
(indent-to col
))
1014 (vip-change-mode-to-insert)))))
1016 (defun vip-open-line-at-point (arg)
1017 "Open line at point."
1019 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1020 (setq vip-d-com
(list 'vip-open-line-at-point val ?r
))
1027 (vip-change-mode-to-insert))))
1029 (defun vip-substitute (arg)
1030 "Substitute characters."
1032 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1037 (vip-change-subr (mark) (point))
1038 (vip-change (mark) (point))))
1039 (setq vip-d-com
(list 'vip-substitute val ?r
))))
1041 (defun vip-substitute-line (arg)
1044 (vip-line (cons arg ?C
)))
1049 (defun vip-line (arg)
1050 (let ((val (car arg
)) (com (cdr arg
)))
1051 (move-marker vip-com-point
(point))
1052 (with-no-warnings (next-line (1- val
)))
1053 (vip-execute-com 'vip-line val com
)))
1055 (defun vip-yank-line (arg)
1056 "Yank ARG lines (in vi's sense)"
1058 (let ((val (vip-p-val arg
)))
1059 (vip-line (cons val ?Y
))))
1064 (defun vip-region (arg)
1066 (let ((val (vip-P-val arg
))
1067 (com (vip-getcom arg
)))
1068 (move-marker vip-com-point
(point))
1069 (exchange-point-and-mark)
1070 (vip-execute-com 'vip-region val com
)))
1072 (defun vip-Region (arg)
1074 (let ((val (vip-P-val arg
))
1075 (com (vip-getCom arg
)))
1076 (move-marker vip-com-point
(point))
1077 (exchange-point-and-mark)
1078 (vip-execute-com 'vip-Region val com
)))
1080 (defun vip-replace-char (arg)
1081 "Replace the following ARG chars by the character read."
1083 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1084 (setq vip-d-com
(list 'vip-replace-char val ?r
))
1085 (vip-replace-char-subr (if (equal com ?r
) vip-d-char
(read-char)) val
)))
1087 (defun vip-replace-char-subr (char arg
)
1089 (setq vip-d-char char
)
1090 (vip-loop (if (> arg
0) arg
(- arg
)) (insert char
))
1091 (backward-char arg
))
1093 (defun vip-replace-string ()
1094 "Replace string. If you supply null string as the string to be replaced,
1095 the query replace mode will toggle between string replace and regexp replace."
1098 (setq str
(vip-read-string
1099 (if vip-re-replace
"Replace regexp: " "Replace string: ")))
1100 (if (string= str
"")
1102 (setq vip-re-replace
(not vip-re-replace
))
1103 (message "Replace mode changed to %s."
1104 (if vip-re-replace
"regexp replace"
1109 ;; (vip-read-string (format "Replace regexp \"%s\" with: " str)))
1110 (while (re-search-forward str nil t
)
1111 (replace-match (vip-read-string
1112 (format "Replace regexp \"%s\" with: " str
))
1117 (vip-read-string (format "Replace \"%s\" with: " str
))))))))
1120 ;; basic cursor movement. j, k, l, m commands.
1122 (defun vip-forward-char (arg)
1123 "Move point right ARG characters (left if ARG negative).On reaching end
1124 of buffer, stop and signal error."
1126 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1127 (if com
(move-marker vip-com-point
(point)))
1129 (if com
(vip-execute-com 'vip-forward-char val com
))))
1131 (defun vip-backward-char (arg)
1132 "Move point left ARG characters (right if ARG negative). On reaching
1133 beginning of buffer, stop and signal error."
1135 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1136 (if com
(move-marker vip-com-point
(point)))
1138 (if com
(vip-execute-com 'vip-backward-char val com
))))
1143 (defun vip-forward-word (arg)
1146 (let ((val (vip-p-val arg
))
1147 (com (vip-getcom arg
)))
1148 (if com
(move-marker vip-com-point
(point)))
1150 (skip-chars-forward " \t\n")
1153 (if (or (= com ?c
) (= com
(- ?c
)))
1154 (progn (backward-word 1) (forward-word 1)))
1155 (if (or (= com ?d
) (= com ?y
))
1159 (skip-chars-forward " \t")))
1160 (vip-execute-com 'vip-forward-word val com
)))))
1162 (defun vip-end-of-word (arg)
1163 "Move point to end of current word."
1165 (let ((val (vip-p-val arg
))
1166 (com (vip-getcom arg
)))
1167 (if com
(move-marker vip-com-point
(point)))
1174 (vip-execute-com 'vip-end-of-word val com
)))))
1176 (defun vip-backward-word (arg)
1179 (let ((val (vip-p-val arg
))
1180 (com (vip-getcom arg
)))
1181 (if com
(move-marker vip-com-point
(point)))
1183 (if com
(vip-execute-com 'vip-backward-word val com
))))
1185 (defun vip-forward-Word (arg)
1186 "Forward word delimited by white character."
1188 (let ((val (vip-p-val arg
))
1189 (com (vip-getcom arg
)))
1190 (if com
(move-marker vip-com-point
(point)))
1191 (re-search-forward "[^ \t\n]*[ \t\n]+" nil t val
)
1194 (if (or (= com ?c
) (= com
(- ?c
)))
1195 (progn (backward-word 1) (forward-word 1)))
1196 (if (or (= com ?d
) (= com ?y
))
1200 (skip-chars-forward " \t")))
1201 (vip-execute-com 'vip-forward-Word val com
)))))
1203 (defun vip-end-of-Word (arg)
1204 "Move forward to end of word delimited by white character."
1206 (let ((val (vip-p-val arg
))
1207 (com (vip-getcom arg
)))
1208 (if com
(move-marker vip-com-point
(point)))
1210 (if (re-search-forward "[^ \t\n]+" nil t val
) (backward-char))
1214 (vip-execute-com 'vip-end-of-Word val com
)))))
1216 (defun vip-backward-Word (arg)
1217 "Backward word delimited by white character."
1219 (let ((val (vip-p-val arg
))
1220 (com (vip-getcom arg
)))
1221 (if com
(move-marker vip-com-point
(point)))
1222 (if (re-search-backward "[ \t\n]+[^ \t\n]+" nil t val
)
1224 (goto-char (point-min)))
1225 (if com
(vip-execute-com 'vip-backward-Word val com
))))
1227 (defun vip-beginning-of-line (arg)
1228 "Go to beginning of line."
1230 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1231 (if com
(move-marker vip-com-point
(point)))
1232 (beginning-of-line val
)
1233 (if com
(vip-execute-com 'vip-beginning-of-line val com
))))
1235 (defun vip-bol-and-skip-white (arg)
1236 "Beginning of line at first non-white character."
1238 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1239 (if com
(move-marker vip-com-point
(point)))
1240 (back-to-indentation)
1241 (if com
(vip-execute-com 'vip-bol-and-skip-white val com
))))
1243 (defun vip-goto-eol (arg)
1244 "Go to end of line."
1246 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1247 (if com
(move-marker vip-com-point
(point)))
1249 (if com
(vip-execute-com 'vip-goto-eol val com
))))
1251 (defun vip-next-line (arg)
1254 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1255 (if com
(move-marker vip-com-point
(point)))
1257 (setq this-command
'next-line
)
1258 (if com
(vip-execute-com 'vip-next-line val com
))))
1260 (defun vip-next-line-at-bol (arg)
1261 "Next line at beginning of line."
1263 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1264 (if com
(move-marker vip-com-point
(point)))
1265 (with-no-warnings (next-line val
))
1266 (back-to-indentation)
1267 (if com
(vip-execute-com 'vip-next-line-at-bol val com
))))
1269 (defun vip-previous-line (arg)
1270 "Go to previous line."
1272 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1273 (if com
(move-marker vip-com-point
(point)))
1274 (with-no-warnings (next-line (- val
)))
1275 (setq this-command
'previous-line
)
1276 (if com
(vip-execute-com 'vip-previous-line val com
))))
1278 (defun vip-previous-line-at-bol (arg)
1279 "Previous line at beginning of line."
1281 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1282 (if com
(move-marker vip-com-point
(point)))
1283 (with-no-warnings (next-line (- val
)))
1284 (back-to-indentation)
1285 (if com
(vip-execute-com 'vip-previous-line val com
))))
1287 (defun vip-change-to-eol (arg)
1288 "Change to end of line."
1290 (vip-goto-eol (cons arg ?c
)))
1292 (defun vip-kill-line (arg)
1295 (vip-goto-eol (cons arg ?d
)))
1300 (defun vip-goto-line (arg)
1301 "Go to ARG's line. Without ARG go to end of buffer."
1303 (let ((val (vip-P-val arg
)) (com (vip-getCom arg
)))
1304 (move-marker vip-com-point
(point))
1307 (goto-char (point-max))
1308 (goto-char (point-min))
1309 (forward-line (1- val
)))
1310 (back-to-indentation)
1311 (if com
(vip-execute-com 'vip-goto-line val com
))))
1313 (defun vip-find-char (arg char forward offset
)
1314 "Find ARG's occurrence of CHAR on the current line. If FORWARD then
1315 search is forward, otherwise backward. OFFSET is used to adjust point
1317 (let ((arg (if forward arg
(- arg
))) point
)
1322 ;; forward search begins here
1323 (if (eolp) (error "") (point))
1324 ;; forward search ends here
1325 (progn (with-no-warnings (next-line 1)) (beginning-of-line) (point)))
1327 ;; backward search begins from here
1328 (if (bolp) (error "") (point))
1329 ;; backward search ends here
1330 (progn (beginning-of-line) (point))))
1331 ;; if arg > 0, point is forwarded before search.
1332 (if (> arg
0) (goto-char (1+ (point-min)))
1333 (goto-char (point-max)))
1334 (let ((case-fold-search nil
))
1335 (search-forward (char-to-string char
) nil
0 arg
))
1336 (setq point
(point))
1337 (if (or (and (> arg
0) (= point
(point-max)))
1338 (and (< arg
0) (= point
(point-min))))
1340 (goto-char (+ point
(if (> arg
0) (if offset -
2 -
1) (if offset
1 0))))))
1342 (defun vip-find-char-forward (arg)
1343 "Find char on the line. If called interactively read the char to find
1344 from the terminal, and if called from vip-repeat, the char last used is
1345 used. This behavior is controlled by the sign of prefix numeric value."
1347 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1349 ;; this means that the function was called interactively
1350 (setq vip-f-char
(read-char)
1354 (if com
(move-marker vip-com-point
(point)))
1355 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t nil
)
1359 (setq vip-F-char vip-f-char
);; set new vip-F-char
1361 (vip-execute-com 'vip-find-char-forward val com
)))))
1363 (defun vip-goto-char-forward (arg)
1364 "Go up to char ARG forward on line."
1366 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1368 ;; this means that the function was called interactively
1369 (setq vip-f-char
(read-char)
1373 (if com
(move-marker vip-com-point
(point)))
1374 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t t
)
1378 (setq vip-F-char vip-f-char
);; set new vip-F-char
1380 (vip-execute-com 'vip-goto-char-forward val com
)))))
1382 (defun vip-find-char-backward (arg)
1383 "Find char ARG on line backward."
1385 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1387 ;; this means that the function was called interactively
1388 (setq vip-f-char
(read-char)
1392 (if com
(move-marker vip-com-point
(point)))
1394 val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil nil
)
1398 (setq vip-F-char vip-f-char
);; set new vip-F-char
1399 (vip-execute-com 'vip-find-char-backward val com
)))))
1401 (defun vip-goto-char-backward (arg)
1402 "Go up to char ARG backward on line."
1404 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1406 ;; this means that the function was called interactively
1407 (setq vip-f-char
(read-char)
1411 (if com
(move-marker vip-com-point
(point)))
1412 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil t
)
1416 (setq vip-F-char vip-f-char
);; set new vip-F-char
1417 (vip-execute-com 'vip-goto-char-backward val com
)))))
1419 (defun vip-repeat-find (arg)
1420 "Repeat previous find command."
1422 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1423 (if com
(move-marker vip-com-point
(point)))
1424 (vip-find-char val vip-f-char vip-f-forward vip-f-offset
)
1427 (if vip-f-forward
(forward-char))
1428 (vip-execute-com 'vip-repeat-find val com
)))))
1430 (defun vip-repeat-find-opposite (arg)
1431 "Repeat previous find command in the opposite direction."
1433 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1434 (if com
(move-marker vip-com-point
(point)))
1435 (vip-find-char val vip-f-char
(not vip-f-forward
) vip-f-offset
)
1438 (if vip-f-forward
(forward-char))
1439 (vip-execute-com 'vip-repeat-find-opposite val com
)))))
1442 ;; window scrolling etc.
1444 (defun vip-other-window (arg)
1445 "Switch to other window."
1448 (or (not (eq vip-current-mode
'emacs-mode
))
1449 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
1450 (vip-change-mode-to-vi)))
1452 (defun vip-window-top (arg)
1453 "Go to home window line."
1455 (let ((val (vip-p-val arg
))
1456 (com (vip-getCom arg
)))
1457 (if com
(move-marker vip-com-point
(point)))
1458 (move-to-window-line (1- val
))
1459 (if com
(vip-execute-com 'vip-window-top val com
))))
1461 (defun vip-window-middle (arg)
1462 "Go to middle window line."
1464 (let ((val (vip-p-val arg
))
1465 (com (vip-getCom arg
)))
1466 (if com
(move-marker vip-com-point
(point)))
1467 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val
)))
1468 (if com
(vip-execute-com 'vip-window-middle val com
))))
1470 (defun vip-window-bottom (arg)
1471 "Go to last window line."
1473 (let ((val (vip-p-val arg
))
1474 (com (vip-getCom arg
)))
1475 (if com
(move-marker vip-com-point
(point)))
1476 (move-to-window-line (- val
))
1477 (if com
(vip-execute-com 'vip-window-bottom val com
))))
1479 (defun vip-line-to-top (arg)
1480 "Put current line on the home line."
1482 (recenter (1- arg
)))
1484 (defun vip-line-to-middle (arg)
1485 "Put current line on the middle line."
1487 (recenter (+ (1- arg
) (/ (1- (window-height)) 2))))
1489 (defun vip-line-to-bottom (arg)
1490 "Put current line on the last line."
1492 (recenter (- (window-height) (1+ arg
))))
1497 (defun vip-paren-match (arg)
1498 "Go to the matching parenthesis."
1500 (let ((com (vip-getcom arg
)))
1502 (if (or (> arg
99) (< arg
1))
1503 (error "Prefix must be between 1 and 99")
1505 (if (> (point-max) 80000)
1506 (* (/ (point-max) 100) arg
)
1507 (/ (* (point-max) arg
) 100)))
1508 (back-to-indentation))
1509 (cond ((looking-at "[\(\[{]")
1510 (if com
(move-marker vip-com-point
(point)))
1513 (vip-execute-com 'vip-paren-match nil com
)
1515 ((looking-at "[])}]")
1517 (if com
(move-marker vip-com-point
(point)))
1519 (if com
(vip-execute-com 'vip-paren-match nil com
)))
1523 ;; sentence and paragraph
1525 (defun vip-forward-sentence (arg)
1528 (let ((val (vip-p-val arg
))
1529 (com (vip-getcom arg
)))
1530 (if com
(move-marker vip-com-point
(point)))
1531 (forward-sentence val
)
1532 (if com
(vip-execute-com 'vip-forward-sentence nil com
))))
1534 (defun vip-backward-sentence (arg)
1535 "Backward sentence."
1537 (let ((val (vip-p-val arg
))
1538 (com (vip-getcom arg
)))
1539 (if com
(move-marker vip-com-point
(point)))
1540 (backward-sentence val
)
1541 (if com
(vip-execute-com 'vip-backward-sentence nil com
))))
1543 (defun vip-forward-paragraph (arg)
1544 "Forward paragraph."
1546 (let ((val (vip-p-val arg
))
1547 (com (vip-getCom arg
)))
1548 (if com
(move-marker vip-com-point
(point)))
1549 (forward-paragraph val
)
1550 (if com
(vip-execute-com 'vip-forward-paragraph nil com
))))
1552 (defun vip-backward-paragraph (arg)
1553 "Backward paragraph."
1555 (let ((val (vip-p-val arg
))
1556 (com (vip-getCom arg
)))
1557 (if com
(move-marker vip-com-point
(point)))
1558 (backward-paragraph val
)
1559 (if com
(vip-execute-com 'vip-backward-paragraph nil com
))))
1564 (defun vip-scroll (arg)
1565 "Scroll to next screen."
1570 (setq arg
(1- arg
)))
1573 (setq arg
(1+ arg
)))))
1575 (defun vip-scroll-back (arg)
1576 "Scroll to previous screen."
1578 (vip-scroll (- arg
)))
1580 (defun vip-scroll-down (arg)
1581 "Scroll up half screen."
1583 (if (null arg
) (scroll-down (/ (window-height) 2))
1586 (defun vip-scroll-down-one (arg)
1587 "Scroll up one line."
1591 (defun vip-scroll-up (arg)
1592 "Scroll down half screen."
1594 (if (null arg
) (scroll-up (/ (window-height) 2))
1597 (defun vip-scroll-up-one (arg)
1598 "Scroll down one line."
1605 (defun vip-buffer-in-two-windows ()
1606 "Show current buffer in two windows."
1608 (delete-other-windows)
1609 (split-window-below))
1614 (defun vip-search-forward (arg)
1615 "Search a string forward. ARG is used to find the ARG's occurrence
1616 of the string. Default is vanilla search. Search mode can be toggled by
1617 giving null search string."
1619 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1620 (setq vip-s-forward t
1621 vip-s-string
(vip-read-string (if vip-re-search
"RE-/" "/")))
1622 (if (string= vip-s-string
"")
1624 (setq vip-re-search
(not vip-re-search
))
1625 (message "Search mode changed to %s search."
1626 (if vip-re-search
"regular expression"
1628 (vip-search vip-s-string t val
)
1631 (move-marker vip-com-point
(mark))
1632 (vip-execute-com 'vip-search-next val com
))))))
1634 (defun vip-search-backward (arg)
1635 "Search a string backward. ARG is used to find the ARG's occurrence
1636 of the string. Default is vanilla search. Search mode can be toggled by
1637 giving null search string."
1639 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1640 (setq vip-s-forward nil
1641 vip-s-string
(vip-read-string (if vip-re-search
"RE-?" "?")))
1642 (if (string= vip-s-string
"")
1644 (setq vip-re-search
(not vip-re-search
))
1645 (message "Search mode changed to %s search."
1646 (if vip-re-search
"regular expression"
1648 (vip-search vip-s-string nil val
)
1651 (move-marker vip-com-point
(mark))
1652 (vip-execute-com 'vip-search-next val com
))))))
1654 (defun vip-search (string forward arg
&optional no-offset init-point
)
1655 "(STRING FORWARD COUNT &optional NO-OFFSET) Search COUNT's occurrence of
1656 STRING. Search will be forward if FORWARD, otherwise backward."
1657 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
))
1658 (null-arg (null (vip-P-val arg
))) (offset (not no-offset
))
1659 (case-fold-search vip-case-fold-search
)
1660 (start-point (or init-point
(point))))
1662 (condition-case conditions
1664 (if (and offset
(not (eobp))) (forward-char))
1667 (re-search-forward string nil nil val
)
1668 (re-search-backward string
))
1669 (search-forward string nil nil val
)
1670 (search-backward string
))
1671 (push-mark start-point
))
1673 (if (and null-arg vip-search-wrap-around
)
1675 (goto-char (point-min))
1676 (vip-search string forward
(cons 1 com
) t start-point
))
1677 (goto-char start-point
)
1678 (signal 'search-failed
(cdr conditions
)))))
1679 (condition-case conditions
1682 (re-search-backward string nil nil val
)
1683 (search-backward string nil nil val
))
1684 (push-mark start-point
))
1686 (if (and null-arg vip-search-wrap-around
)
1688 (goto-char (point-max))
1689 (vip-search string forward
(cons 1 com
) t start-point
))
1690 (goto-char start-point
)
1691 (signal 'search-failed
(cdr conditions
))))))))
1693 (defun vip-search-next (arg)
1694 "Repeat previous search."
1696 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1697 (if (null vip-s-string
) (error "No previous search string"))
1698 (vip-search vip-s-string vip-s-forward arg
)
1699 (if com
(vip-execute-com 'vip-search-next val com
))))
1701 (defun vip-search-Next (arg)
1702 "Repeat previous search in the reverse direction."
1704 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1705 (if (null vip-s-string
) (error "No previous search string"))
1706 (vip-search vip-s-string
(not vip-s-forward
) arg
)
1707 (if com
(vip-execute-com 'vip-search-Next val com
))))
1710 ;; visiting and killing files, buffers
1712 (defun vip-switch-to-buffer ()
1713 "Switch to buffer in the current window."
1718 (format "switch to buffer \(%s\): "
1719 (buffer-name (other-buffer (current-buffer))))))
1720 (switch-to-buffer buffer
)
1721 (vip-change-mode-to-vi)))
1723 (defun vip-switch-to-buffer-other-window ()
1724 "Switch to buffer in another window."
1729 (format "Switch to buffer \(%s\): "
1730 (buffer-name (other-buffer (current-buffer))))))
1731 (switch-to-buffer-other-window buffer
)
1732 (vip-change-mode-to-vi)))
1734 (defun vip-kill-buffer ()
1737 (let (buffer buffer-name
)
1740 (format "Kill buffer \(%s\): "
1741 (buffer-name (current-buffer)))))
1743 (if (null buffer-name
)
1745 (get-buffer buffer-name
)))
1746 (if (null buffer
) (error "Buffer %s nonexistent" buffer-name
))
1747 (if (or (not (buffer-modified-p buffer
))
1748 (y-or-n-p "Buffer is modified, are you sure? "))
1749 (kill-buffer buffer
)
1750 (error "Buffer not killed"))))
1752 (defun vip-find-file ()
1753 "Visit file in the current window."
1756 (setq file
(read-file-name "visit file: "))
1757 (switch-to-buffer (find-file-noselect file
))
1758 (vip-change-mode-to-vi)))
1760 (defun vip-find-file-other-window ()
1761 "Visit file in another window."
1764 (setq file
(read-file-name "Visit file: "))
1765 (switch-to-buffer-other-window (find-file-noselect file
))
1766 (vip-change-mode-to-vi)))
1768 (defun vip-info-on-file ()
1769 "Give information of the file associated to the current buffer."
1771 (message "\"%s\" line %d of %d"
1772 (if (buffer-file-name) (buffer-file-name) "")
1773 (1+ (count-lines (point-min) (point)))
1774 (1+ (count-lines (point-min) (point-max)))))
1779 (defun vip-yank (text)
1780 "yank TEXT silently."
1782 (vip-push-mark-silent (point))
1784 (exchange-point-and-mark))
1785 (skip-chars-forward " \t"))
1787 (defun vip-put-back (arg)
1788 "Put back after point/below line."
1790 (let ((val (vip-p-val arg
))
1791 (text (if vip-use-register
1792 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1793 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1794 (get-register vip-use-register
))
1797 (if vip-use-register
1798 (let ((reg vip-use-register
))
1799 (setq vip-use-register nil
)
1800 (error "Nothing in register %c" reg
))
1802 (setq vip-use-register nil
)
1803 (if (vip-end-with-a-newline-p text
)
1805 (with-no-warnings (next-line 1))
1806 (beginning-of-line))
1807 (if (and (not (eolp)) (not (eobp))) (forward-char)))
1808 (setq vip-d-com
(list 'vip-put-back val nil vip-use-register
))
1809 (vip-loop val
(vip-yank text
))))
1811 (defun vip-Put-back (arg)
1812 "Put back at point/above line."
1814 (let ((val (vip-p-val arg
))
1815 (text (if vip-use-register
1816 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1817 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1818 (get-register vip-use-register
))
1821 (if vip-use-register
1822 (let ((reg vip-use-register
))
1823 (setq vip-use-register nil
)
1824 (error "Nothing in register %c" reg
))
1826 (setq vip-use-register nil
)
1827 (if (vip-end-with-a-newline-p text
) (beginning-of-line))
1828 (setq vip-d-com
(list 'vip-Put-back val nil vip-use-register
))
1829 (vip-loop val
(vip-yank text
))))
1831 (defun vip-delete-char (arg)
1834 (let ((val (vip-p-val arg
)))
1835 (setq vip-d-com
(list 'vip-delete-char val nil
))
1836 (if vip-use-register
1838 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1839 (vip-append-to-register
1840 (+ vip-use-register
32) (point) (- (point) val
))
1841 (copy-to-register vip-use-register
(point) (- (point) val
) nil
))
1842 (setq vip-use-register nil
)))
1843 (delete-char val t
)))
1845 (defun vip-delete-backward-char (arg)
1846 "Delete previous character."
1848 (let ((val (vip-p-val arg
)))
1849 (setq vip-d-com
(list 'vip-delete-backward-char val nil
))
1850 (if vip-use-register
1852 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1853 (vip-append-to-register
1854 (+ vip-use-register
32) (point) (+ (point) val
))
1855 (copy-to-register vip-use-register
(point) (+ (point) val
) nil
))
1856 (setq vip-use-register nil
)))
1857 (delete-backward-char val t
)))
1862 (defun vip-join-lines (arg)
1863 "Join this line to next, if ARG is nil. Otherwise, join ARG lines"
1865 (let ((val (vip-P-val arg
)))
1866 (setq vip-d-com
(list 'vip-join-lines val nil
))
1867 (vip-loop (if (null val
) 1 (1- val
))
1873 (delete-region (point) (1- (point)))
1874 (fixup-whitespace)))))))
1877 ;; making small changes
1879 (defvar vip-c-string
)
1881 (defun vip-change (beg end
)
1883 (vip-read-string (format "%s => " (buffer-substring beg end
))))
1884 (vip-change-subr beg end
))
1886 (defun vip-change-subr (beg end
)
1887 (if vip-use-register
1889 (copy-to-register vip-use-register beg end nil
)
1890 (setq vip-use-register nil
)))
1891 (kill-region beg end
)
1892 (setq this-command
'vip-change
)
1893 (insert vip-c-string
))
1898 (defun vip-query-replace ()
1899 "Query replace. If you supply null string as the string to be replaced,
1900 the query replace mode will toggle between string replace and regexp replace."
1903 (setq str
(vip-read-string
1904 (if vip-re-query-replace
"Query replace regexp: "
1905 "Query replace: ")))
1906 (if (string= str
"")
1908 (setq vip-re-query-replace
(not vip-re-query-replace
))
1909 (message "Query replace mode changed to %s."
1910 (if vip-re-query-replace
"regexp replace"
1912 (if vip-re-query-replace
1913 (query-replace-regexp
1915 (vip-read-string (format "Query replace regexp \"%s\" with: " str
)))
1918 (vip-read-string (format "Query replace \"%s\" with: " str
)))))))
1923 (defun vip-mark-beginning-of-buffer ()
1926 (goto-char (point-min))
1927 (exchange-point-and-mark)
1928 (message "mark set at the beginning of buffer"))
1930 (defun vip-mark-end-of-buffer ()
1933 (goto-char (point-max))
1934 (exchange-point-and-mark)
1935 (message "mark set at the end of buffer"))
1937 (defun vip-mark-point (char)
1939 (cond ((and (<= ?a char
) (<= char ?z
))
1940 (point-to-register (- char
(- ?a ?\C-a
)) nil
))
1941 ((= char ?
<) (vip-mark-beginning-of-buffer))
1942 ((= char ?
>) (vip-mark-end-of-buffer))
1943 ((= char ?.
) (push-mark))
1944 ((= char ?
,) (set-mark-command 1))
1945 ((= char ?D
) (mark-defun))
1948 (defun vip-goto-mark (arg)
1951 (let ((char (read-char)) (com (vip-getcom arg
)))
1952 (vip-goto-mark-subr char com nil
)))
1954 (defun vip-goto-mark-and-skip-white (arg)
1955 "Go to mark and skip to first non-white on line."
1957 (let ((char (read-char)) (com (vip-getCom arg
)))
1958 (vip-goto-mark-subr char com t
)))
1960 (defun vip-goto-mark-subr (char com skip-white
)
1961 (cond ((and (<= ?a char
) (<= char ?z
))
1962 (let ((buff (current-buffer)))
1963 (if com
(move-marker vip-com-point
(point)))
1964 (goto-char (register-to-point (- char
(- ?a ?\C-a
))))
1965 (if skip-white
(back-to-indentation))
1966 (vip-change-mode-to-vi)
1968 (if (equal buff
(current-buffer))
1969 (vip-execute-com (if skip-white
1970 'vip-goto-mark-and-skip-white
1973 (switch-to-buffer buff
)
1974 (goto-char vip-com-point
)
1975 (vip-change-mode-to-vi)
1977 ((and (not skip-white
) (= char ?
`))
1978 (if com
(move-marker vip-com-point
(point)))
1979 (exchange-point-and-mark)
1980 (if com
(vip-execute-com 'vip-goto-mark nil com
)))
1981 ((and skip-white
(= char ?
'))
1982 (if com
(move-marker vip-com-point
(point)))
1983 (exchange-point-and-mark)
1984 (back-to-indentation)
1985 (if com
(vip-execute-com 'vip-goto-mark-and-skip-white nil com
)))
1988 (defun vip-exchange-point-and-mark ()
1990 (exchange-point-and-mark)
1991 (back-to-indentation))
1993 (defun vip-keyboard-quit ()
1994 "Abort partially formed or running command."
1996 (setq vip-use-register nil
)
1999 (defun vip-ctl-c-equivalent (arg)
2000 "Emulate C-c in Emacs mode."
2002 (vip-ctl-key-equivalent "\C-c" arg
))
2004 (defun vip-ctl-x-equivalent (arg)
2005 "Emulate C-x in Emacs mode."
2007 (vip-ctl-key-equivalent "\C-x" arg
))
2009 (defun vip-ctl-key-equivalent (key arg
)
2010 (let ((char (read-char)))
2011 (if (and (<= ?A char
) (<= char ?Z
))
2012 (setq char
(- char
(- ?A ?\C-a
))))
2013 (vip-escape-to-emacs arg
(list (aref key
0) char
))))
2015 ;; commands in insertion mode
2017 (defun vip-delete-backward-word (arg)
2018 "Delete previous word."
2023 (delete-region (point) (mark))))
2026 ;; implement ex commands
2028 (defvar ex-token-type nil
2029 "type of token. if non-nil, gives type of address. if nil, it
2032 (defvar ex-token nil
2035 (defvar ex-addresses nil
2036 "list of ex addresses")
2041 (defvar ex-buffer nil
2042 "name of ex buffer")
2044 (defvar ex-count nil
2045 "value of ex count")
2047 (defvar ex-g-flag nil
2048 "flag for global command")
2050 (defvar ex-g-variant nil
2051 "if t global command is executed on lines not matching ex-g-pat")
2053 (defvar ex-reg-exp nil
2054 "save reg-exp used in substitute")
2057 "replace pattern for substitute")
2059 (defvar ex-g-pat nil
2060 "pattern for global command")
2062 (defvar ex-map
(make-sparse-keymap)
2063 "save commands for mapped keys")
2068 (defvar ex-file nil
)
2070 (defvar ex-variant nil
)
2072 (defvar ex-offset nil
)
2074 (defvar ex-append nil
)
2080 (defun vip-looking-back (str)
2081 "returns t if looking back reg-exp STR before point."
2082 (and (save-excursion (re-search-backward str nil t
))
2083 (= (point) (match-end 0))))
2085 (defun vip-check-sub (str)
2086 "check if ex-token is an initial segment of STR"
2087 (let ((length (length ex-token
)))
2088 (if (and (<= length
(length str
))
2089 (string= ex-token
(substring str
0 length
)))
2091 (setq ex-token-type
"non-command"))))
2093 (defun vip-get-ex-com-subr ()
2094 "get a complete ex command"
2096 (re-search-forward "[a-z][a-z]*")
2097 (setq ex-token-type
"command")
2098 (setq ex-token
(buffer-substring (point) (mark)))
2099 (exchange-point-and-mark)
2100 (cond ((looking-at "a")
2101 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
2102 ((looking-at "ar") (vip-check-sub "args"))
2103 (t (vip-check-sub "append"))))
2104 ((looking-at "[bh]") (setq ex-token-type
"non-command"))
2106 (if (looking-at "co") (vip-check-sub "copy")
2107 (vip-check-sub "change")))
2108 ((looking-at "d") (vip-check-sub "delete"))
2110 (if (looking-at "ex") (vip-check-sub "ex")
2111 (vip-check-sub "edit")))
2112 ((looking-at "f") (vip-check-sub "file"))
2113 ((looking-at "g") (vip-check-sub "global"))
2114 ((looking-at "i") (vip-check-sub "insert"))
2115 ((looking-at "j") (vip-check-sub "join"))
2116 ((looking-at "l") (vip-check-sub "list"))
2118 (cond ((looking-at "map") (vip-check-sub "map"))
2119 ((looking-at "mar") (vip-check-sub "mark"))
2120 (t (vip-check-sub "move"))))
2122 (if (looking-at "nu") (vip-check-sub "number")
2123 (vip-check-sub "next")))
2124 ((looking-at "o") (vip-check-sub "open"))
2126 (cond ((looking-at "pre") (vip-check-sub "preserve"))
2127 ((looking-at "pu") (vip-check-sub "put"))
2128 (t (vip-check-sub "print"))))
2129 ((looking-at "q") (vip-check-sub "quit"))
2131 (cond ((looking-at "rec") (vip-check-sub "recover"))
2132 ((looking-at "rew") (vip-check-sub "rewind"))
2133 (t (vip-check-sub "read"))))
2135 (cond ((looking-at "se") (vip-check-sub "set"))
2136 ((looking-at "sh") (vip-check-sub "shell"))
2137 ((looking-at "so") (vip-check-sub "source"))
2138 ((looking-at "st") (vip-check-sub "stop"))
2139 (t (vip-check-sub "substitute"))))
2141 (if (looking-at "ta") (vip-check-sub "tag")
2142 (vip-check-sub "t")))
2144 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
2145 ((looking-at "unm") (vip-check-sub "unmap"))
2146 (t (vip-check-sub "undo"))))
2148 (cond ((looking-at "ve") (vip-check-sub "version"))
2149 ((looking-at "vi") (vip-check-sub "visual"))
2150 (t (vip-check-sub "v"))))
2152 (if (looking-at "wq") (vip-check-sub "wq")
2153 (vip-check-sub "write")))
2154 ((looking-at "x") (vip-check-sub "xit"))
2155 ((looking-at "y") (vip-check-sub "yank"))
2156 ((looking-at "z") (vip-check-sub "z")))
2157 (exchange-point-and-mark))
2159 (defun vip-get-ex-token ()
2160 "get an ex-token which is either an address or a command.
2161 a token has type \(command, address, end-mark\) and value."
2162 (with-current-buffer " *ex-working-space*"
2163 (skip-chars-forward " \t")
2164 (cond ((looking-at "[k#]")
2165 (setq ex-token-type
"command")
2166 (setq ex-token
(char-to-string (following-char)))
2168 ((looking-at "[a-z]") (vip-get-ex-com-subr))
2171 (setq ex-token-type
"dot"))
2172 ((looking-at "[0-9]")
2174 (re-search-forward "[0-9]*")
2176 (cond ((string= ex-token-type
"plus") "add-number")
2177 ((string= ex-token-type
"minus") "sub-number")
2179 (setq ex-token
(string-to-number (buffer-substring (point) (mark)))))
2182 (setq ex-token-type
"end"))
2185 (setq ex-token-type
"whole"))
2187 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
2191 (setq ex-token-type
"plus"))
2192 ((looking-at "+[0-9]")
2194 (setq ex-token-type
"plus"))
2196 (error "Badly formed address"))))
2198 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
2202 (setq ex-token-type
"minus"))
2203 ((looking-at "-[0-9]")
2205 (setq ex-token-type
"minus"))
2207 (error "Badly formed address"))))
2212 (while (and (not (eolp)) cont
)
2213 ;;(re-search-forward "[^/]*/")
2214 (re-search-forward "[^/]*\\(/\\|\n\\)")
2215 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2218 (setq ex-token
(buffer-substring (point) (mark)))
2219 (if (looking-at "/") (forward-char 1))
2220 (setq ex-token-type
"search-forward"))
2225 (while (and (not (eolp)) cont
)
2226 ;;(re-search-forward "[^\\?]*\\?")
2227 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
2228 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
2231 (if (not (looking-at "\n")) (forward-char 1))))
2232 (setq ex-token-type
"search-backward")
2233 (setq ex-token
(buffer-substring (1- (point)) (mark))))
2236 (setq ex-token-type
"comma"))
2239 (setq ex-token-type
"semi-colon"))
2240 ((looking-at "[!=><&~]")
2241 (setq ex-token-type
"command")
2242 (setq ex-token
(char-to-string (following-char)))
2245 (setq ex-token-type
"goto-mark")
2247 (cond ((looking-at "'") (setq ex-token nil
))
2248 ((looking-at "[a-z]") (setq ex-token
(following-char)))
2249 (t (error "Marks are ' and a-z")))
2252 (setq ex-token-type
"end-mark")
2253 (setq ex-token
"goto"))
2255 (error "invalid token")))))
2257 (defun vip-ex (&optional string
)
2258 "ex commands within VIP."
2263 (let ((com-str (or string
(vip-read-string ":")))
2264 (address nil
) (cont t
) (dot (point)))
2265 (with-current-buffer (get-buffer-create " *ex-working-space*")
2266 (delete-region (point-min) (point-max))
2267 (insert com-str
"\n")
2268 (goto-char (point-min)))
2269 (setq ex-token-type
"")
2270 (setq ex-addresses nil
)
2273 (cond ((or (string= ex-token-type
"command")
2274 (string= ex-token-type
"end-mark"))
2275 (if address
(setq ex-addresses
(cons address ex-addresses
)))
2276 (cond ((string= ex-token
"global")
2279 ((string= ex-token
"v")
2283 (vip-execute-ex-command)
2284 (with-current-buffer " *ex-working-space*"
2285 (skip-chars-forward " \t")
2286 (cond ((looking-at "|")
2290 (t (error "Extra character at end of a command")))))))
2291 ((string= ex-token-type
"non-command")
2292 (error "%s: Not an editor command" ex-token
))
2293 ((string= ex-token-type
"whole")
2295 (cons (point-max) (cons (point-min) ex-addresses
))))
2296 ((string= ex-token-type
"comma")
2298 (cons (if (null address
) (point) address
) ex-addresses
)))
2299 ((string= ex-token-type
"semi-colon")
2300 (if address
(setq dot address
))
2302 (cons (if (null address
) (point) address
) ex-addresses
)))
2303 (t (let ((ans (vip-get-ex-address-subr address dot
)))
2304 (if ans
(setq address ans
))))))))
2306 (defun vip-get-ex-pat ()
2307 "get a regular expression and set ex-variant if found"
2308 (with-current-buffer " *ex-working-space*"
2309 (skip-chars-forward " \t")
2310 (if (looking-at "!")
2312 (setq ex-g-variant
(not ex-g-variant
)
2313 ex-g-flag
(not ex-g-flag
))
2315 (skip-chars-forward " \t")))
2316 (if (looking-at "/")
2321 (while (and (not (eolp)) cont
)
2322 (re-search-forward "[^/]*\\(/\\|\n\\)")
2323 ;;(re-search-forward "[^/]*/")
2324 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2327 (if (= (mark) (point)) ""
2328 (buffer-substring (1- (point)) (mark))))
2330 (setq ex-token nil
))))
2332 (defun vip-get-ex-command ()
2334 (with-current-buffer " *ex-working-space*"
2335 (if (looking-at "/") (forward-char 1))
2336 (skip-chars-forward " \t")
2337 (cond ((looking-at "[a-z]")
2338 (vip-get-ex-com-subr)
2339 (if (string= ex-token-type
"non-command")
2340 (error "%s: not an editor command" ex-token
)))
2341 ((looking-at "[!=><&~]")
2342 (setq ex-token
(char-to-string (following-char)))
2344 (t (error "Could not find an ex command")))))
2346 (defun vip-get-ex-opt-gc ()
2347 "get an ex option g or c"
2348 (with-current-buffer " *ex-working-space*"
2349 (if (looking-at "/") (forward-char 1))
2350 (skip-chars-forward " \t")
2351 (cond ((looking-at "g")
2361 (defun vip-default-ex-addresses (&optional whole-flag
)
2362 "compute default addresses. whole-flag means whole buffer."
2363 (cond ((null ex-addresses
)
2366 (cons (point-max) (cons (point-min) nil
))
2367 (cons (point) (cons (point) nil
)))))
2368 ((null (cdr ex-addresses
))
2370 (cons (car ex-addresses
) ex-addresses
)))))
2372 (defun vip-get-ex-address ()
2373 "get an ex-address as a marker and set ex-flag if a flag is found"
2374 (let ((address (point-marker)) (cont t
))
2379 (cond ((string= ex-token-type
"command")
2380 (if (or (string= ex-token
"print") (string= ex-token
"list")
2381 (string= ex-token
"#"))
2385 (error "address expected")))
2386 ((string= ex-token-type
"end-mark")
2388 ((string= ex-token-type
"whole")
2389 (error "a trailing address is expected"))
2390 ((string= ex-token-type
"comma")
2391 (error "Extra characters after an address"))
2392 (t (let ((ans (vip-get-ex-address-subr address
(point-marker))))
2393 (if ans
(setq address ans
))))))
2396 (defun vip-get-ex-address-subr (old-address dot
)
2397 "returns an address as a point"
2398 (let ((address nil
))
2399 (if (null old-address
) (setq old-address dot
))
2400 (cond ((string= ex-token-type
"dot")
2402 ((string= ex-token-type
"add-number")
2404 (goto-char old-address
)
2405 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
2406 (setq address
(point-marker))))
2407 ((string= ex-token-type
"sub-number")
2409 (goto-char old-address
)
2410 (forward-line (- ex-token
))
2411 (setq address
(point-marker))))
2412 ((string= ex-token-type
"abs-number")
2414 (goto-char (point-min))
2415 (if (= ex-token
0) (setq address
0)
2416 (forward-line (1- ex-token
))
2417 (setq address
(point-marker)))))
2418 ((string= ex-token-type
"end")
2419 (setq address
(point-max-marker)))
2420 ((string= ex-token-type
"plus") t
);; do nothing
2421 ((string= ex-token-type
"minus") t
);; do nothing
2422 ((string= ex-token-type
"search-forward")
2424 (ex-search-address t
)
2425 (setq address
(point-marker))))
2426 ((string= ex-token-type
"search-backward")
2428 (ex-search-address nil
)
2429 (setq address
(point-marker))))
2430 ((string= ex-token-type
"goto-mark")
2433 (exchange-point-and-mark)
2434 (goto-char (register-to-point (- ex-token
(- ?a ?\C-a
)))))
2435 (setq address
(point-marker)))))
2438 (defun ex-search-address (forward)
2439 "search pattern and set address"
2440 (if (string= ex-token
"")
2441 (if (null vip-s-string
) (error "No previous search string")
2442 (setq ex-token vip-s-string
))
2443 (setq vip-s-string ex-token
))
2447 (re-search-forward ex-token
))
2449 (re-search-backward ex-token
)))
2451 (defun vip-get-ex-buffer ()
2452 "get a buffer name and set ex-count and ex-flag if found"
2453 (setq ex-buffer nil
)
2456 (with-current-buffer " *ex-working-space*"
2457 (skip-chars-forward " \t")
2458 (if (looking-at "[a-zA-Z]")
2460 (setq ex-buffer
(following-char))
2462 (skip-chars-forward " \t")))
2463 (if (looking-at "[0-9]")
2466 (re-search-forward "[0-9][0-9]*")
2467 (setq ex-count
(string-to-number (buffer-substring (point) (mark))))
2468 (skip-chars-forward " \t")))
2469 (if (looking-at "[pl#]")
2473 (if (not (looking-at "[\n|]"))
2474 (error "Invalid extra characters"))))
2476 (defun vip-get-ex-count ()
2477 (setq ex-variant nil
2480 (with-current-buffer " *ex-working-space*"
2481 (skip-chars-forward " \t")
2482 (if (looking-at "!")
2486 (skip-chars-forward " \t")
2487 (if (looking-at "[0-9]")
2490 (re-search-forward "[0-9][0-9]*")
2491 (setq ex-count
(string-to-number (buffer-substring (point) (mark))))
2492 (skip-chars-forward " \t")))
2493 (if (looking-at "[pl#]")
2497 (if (not (looking-at "[\n|]"))
2498 (error "Invalid extra characters"))))
2500 (defun vip-get-ex-file ()
2501 "get a file name and set ex-variant, ex-append and ex-offset if found"
2506 (with-current-buffer " *ex-working-space*"
2507 (skip-chars-forward " \t")
2508 (if (looking-at "!")
2512 (skip-chars-forward " \t")))
2513 (if (looking-at ">>")
2518 (skip-chars-forward " \t")))
2519 (if (looking-at "+")
2523 (re-search-forward "[ \t\n]")
2525 (setq ex-offset
(buffer-substring (point) (mark)))
2527 (skip-chars-forward " \t")))
2529 (re-search-forward "[ \t\n]")
2531 (setq ex-file
(buffer-substring (point) (mark)))))
2533 (defun vip-execute-ex-command ()
2534 "execute ex command using the value of addresses."
2535 (cond ((string= ex-token
"goto") (ex-goto))
2536 ((string= ex-token
"copy") (ex-copy nil
))
2537 ((string= ex-token
"delete") (ex-delete))
2538 ((string= ex-token
"edit") (ex-edit))
2539 ((string= ex-token
"file") (vip-info-on-file))
2540 ;((string= ex-token "global") (ex-global nil))
2541 ((string= ex-token
"join") (ex-line "join"))
2542 ((string= ex-token
"k") (ex-mark))
2543 ((string= ex-token
"mark") (ex-mark))
2544 ((string= ex-token
"map") (ex-map))
2545 ((string= ex-token
"move") (ex-copy t
))
2546 ((string= ex-token
"put") (ex-put))
2547 ((string= ex-token
"quit") (ex-quit))
2548 ((string= ex-token
"read") (ex-read))
2549 ((string= ex-token
"set") (ex-set))
2550 ((string= ex-token
"shell") (ex-shell))
2551 ((string= ex-token
"substitute") (ex-substitute))
2552 ((string= ex-token
"stop") (suspend-emacs))
2553 ((string= ex-token
"t") (ex-copy nil
))
2554 ((string= ex-token
"tag") (ex-tag))
2555 ((string= ex-token
"undo") (vip-undo))
2556 ((string= ex-token
"unmap") (ex-unmap))
2557 ;((string= ex-token "v") (ex-global t))
2558 ((string= ex-token
"version") (vip-version))
2559 ((string= ex-token
"visual") (ex-edit))
2560 ((string= ex-token
"write") (ex-write nil
))
2561 ((string= ex-token
"wq") (ex-write t
))
2562 ((string= ex-token
"yank") (ex-yank))
2563 ((string= ex-token
"!") (ex-command))
2564 ((string= ex-token
"=") (ex-line-no))
2565 ((string= ex-token
">") (ex-line "right"))
2566 ((string= ex-token
"<") (ex-line "left"))
2567 ((string= ex-token
"&") (ex-substitute t
))
2568 ((string= ex-token
"~") (ex-substitute t t
))
2569 ((or (string= ex-token
"append")
2570 (string= ex-token
"args")
2571 (string= ex-token
"change")
2572 (string= ex-token
"insert")
2573 (string= ex-token
"open")
2575 (error "%s: no such command from VIP" ex-token
))
2576 ((or (string= ex-token
"abbreviate")
2577 (string= ex-token
"list")
2578 (string= ex-token
"next")
2579 (string= ex-token
"print")
2580 (string= ex-token
"preserve")
2581 (string= ex-token
"recover")
2582 (string= ex-token
"rewind")
2583 (string= ex-token
"source")
2584 (string= ex-token
"unabbreviate")
2585 (string= ex-token
"xit")
2586 (string= ex-token
"z")
2588 (error "%s: not implemented in VIP" ex-token
))
2589 (t (error "%s: Not an editor command" ex-token
))))
2593 (if (null ex-addresses
)
2594 (setq ex-addresses
(cons (point) nil
)))
2596 (goto-char (car ex-addresses
))
2597 (beginning-of-line))
2599 (defun ex-copy (del-flag)
2600 "ex copy and move command. DEL-FLAG means delete."
2601 (vip-default-ex-addresses)
2602 (let ((address (vip-get-ex-address))
2603 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2607 (vip-enlarge-region (mark) (point))
2608 (if del-flag
(kill-region (point) (mark))
2609 (copy-region-as-kill (point) (mark)))
2612 (with-output-to-temp-buffer "*copy text*"
2614 (if (or del-flag ex-g-flag ex-g-variant
)
2616 (buffer-substring (point) (mark)))))
2619 (vip-read-string "[Hit return to continue] ")
2620 (save-excursion (kill-buffer "*copy text*")))
2622 (save-excursion (kill-buffer "*copy text*"))
2623 (signal 'quit nil
))))))
2625 (goto-char (point-min))
2628 (insert (current-kill 0))))
2632 (vip-default-ex-addresses)
2634 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2635 (if (> beg end
) (error "First address exceeds second"))
2637 (vip-enlarge-region beg end
)
2638 (exchange-point-and-mark)
2642 (forward-line (1- ex-count
)))
2644 (vip-enlarge-region (point) (mark))
2646 ;; show text to be deleted and ask for confirmation
2648 (with-output-to-temp-buffer " *delete text*"
2649 (princ (buffer-substring (point) (mark))))
2650 (condition-case conditions
2651 (vip-read-string "[Hit return to continue] ")
2653 (save-excursion (kill-buffer " *delete text*"))
2655 (save-excursion (kill-buffer " *delete text*")))
2657 (if (and (<= ?A ex-buffer
) (<= ex-buffer ?Z
))
2658 (vip-append-to-register
2659 (+ ex-buffer
32) (point) (mark))
2660 (copy-to-register ex-buffer
(point) (mark) nil
)))
2661 (delete-region (point) (mark))))))
2666 (if (and (not ex-variant
) (buffer-modified-p) buffer-file-name
)
2667 (error "No write since last change \(:e! overrides\)"))
2668 (vip-change-mode-to-emacs)
2670 (find-file-noselect (concat default-directory ex-file
)))
2671 (vip-change-mode-to-vi)
2672 (goto-char (point-min))
2675 (with-current-buffer " *ex-working-space*"
2676 (delete-region (point-min) (point-max))
2677 (insert ex-offset
"\n")
2678 (goto-char (point-min)))
2679 (goto-char (vip-get-ex-address))
2680 (beginning-of-line))))
2682 (defun ex-global (variant)
2684 (if (or ex-g-flag ex-g-variant
)
2685 (error "Global within global not allowed")
2693 (error "Missing regular expression for global command"))
2694 (if (string= ex-token
"")
2695 (if (null vip-s-string
) (error "No previous search string")
2696 (setq ex-g-pat vip-s-string
))
2697 (setq ex-g-pat ex-token
2698 vip-s-string ex-token
))
2699 (if (null ex-addresses
)
2700 (setq ex-addresses
(list (point-max) (point-min))))
2701 (let ((marks nil
) (mark-count 0)
2702 com-str
(end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2703 (if (> beg end
) (error "First address exceeds second"))
2705 (vip-enlarge-region beg end
)
2706 (exchange-point-and-mark)
2707 (let ((cont t
) (limit (point-marker)))
2708 (exchange-point-and-mark)
2709 ;; skip the last line if empty
2711 (if (and (eobp) (not (bobp))) (backward-char 1))
2712 (while (and cont
(not (bobp)) (>= (point) limit
))
2716 (let ((found (re-search-backward ex-g-pat
(mark) t
)))
2717 (if (or (and ex-g-flag found
)
2718 (and ex-g-variant
(not found
)))
2721 (setq mark-count
(1+ mark-count
))
2722 (setq marks
(cons (point-marker) marks
)))))
2724 (if (bobp) (setq cont nil
)
2727 (with-current-buffer " *ex-working-space*"
2728 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
2730 (goto-char (car marks
))
2731 ;; report progress of execution on a slow machine.
2732 ;;(message "Executing global command...")
2733 ;;(if (zerop (% mark-count 10))
2734 ;; (message "Executing global command...%d" mark-count))
2736 (setq mark-count
(1- mark-count
))
2737 (setq marks
(cdr marks
)))))
2738 ;;(message "Executing global command...done")))
2740 (defun ex-line (com)
2741 "ex line commands. COM is join, shift-right or shift-left."
2742 (vip-default-ex-addresses)
2744 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
2745 (if (> beg end
) (error "First address exceeds second"))
2747 (vip-enlarge-region beg end
)
2748 (exchange-point-and-mark)
2752 (forward-line ex-count
)))
2754 ;; show text to be joined and ask for confirmation
2756 (with-output-to-temp-buffer " *text*"
2757 (princ (buffer-substring (point) (mark))))
2758 (condition-case conditions
2760 (vip-read-string "[Hit return to continue] ")
2761 (ex-line-subr com
(point) (mark)))
2764 (save-excursion (kill-buffer " *text*")))
2765 (ex-line-subr com
(point) (mark)))
2766 (setq point
(point)))
2767 (goto-char (1- point
))
2768 (beginning-of-line)))
2770 (defun ex-line-subr (com beg end
)
2771 (cond ((string= com
"join")
2772 (goto-char (min beg end
))
2773 (while (and (not (eobp)) (< (point) (max beg end
)))
2775 (if (and (<= (point) (max beg end
)) (not (eobp)))
2778 (delete-region (point) (1- (point)))
2779 (if (not ex-variant
) (fixup-whitespace))))))
2780 ((or (string= com
"right") (string= com
"left"))
2782 (min beg end
) (max beg end
)
2783 (if (string= com
"right") vip-shift-width
(- vip-shift-width
)))
2784 (goto-char (max beg end
))
2791 (if (null ex-addresses
)
2793 (cons (point) nil
)))
2794 (with-current-buffer " *ex-working-space*"
2795 (skip-chars-forward " \t")
2796 (if (looking-at "[a-z]")
2798 (setq char
(following-char))
2800 (skip-chars-forward " \t")
2801 (if (not (looking-at "[\n|]"))
2802 (error "Extra characters at end of \"k\" command")))
2803 (if (looking-at "[\n|]")
2804 (error "\"k\" requires a following letter")
2805 (error "Mark must specify a letter"))))
2807 (goto-char (car ex-addresses
))
2808 (point-to-register (- char
(- ?a ?\C-a
)) nil
))))
2813 (with-current-buffer " *ex-working-space*"
2814 (skip-chars-forward " \t")
2815 (setq char
(char-to-string (following-char)))
2817 (skip-chars-forward " \t")
2818 (if (looking-at "[\n|]") (error "Missing rhs"))
2823 (setq string
(buffer-substring (mark) (point))))
2824 (if (not (lookup-key ex-map char
))
2825 (define-key ex-map char
2826 (or (lookup-key vip-mode-map char
) 'vip-nil
)))
2827 (define-key vip-mode-map char
2833 (list 'execute-kbd-macro string
'count
))))))))
2838 (with-current-buffer " *ex-working-space*"
2839 (skip-chars-forward " \t")
2840 (setq char
(char-to-string (following-char)))
2842 (skip-chars-forward " \t")
2843 (if (not (looking-at "[\n|]")) (error "Macro must be a character")))
2844 (if (not (lookup-key ex-map char
))
2845 (error "That macro wasn't mapped"))
2846 (define-key vip-mode-map char
(lookup-key ex-map char
))
2847 (define-key ex-map char nil
)))
2851 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
2853 (setq vip-use-register ex-buffer
)
2855 (if (= point
0) (vip-Put-back 1) (vip-put-back 1))))
2860 (with-current-buffer " *ex-working-space*"
2861 (skip-chars-forward " \t")
2862 (setq char
(following-char)))
2863 (if (= char ?
!) (kill-emacs t
) (save-buffers-kill-emacs))))
2867 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
2868 (variant nil
) command file
)
2870 (if (not (= point
0)) (with-no-warnings (next-line 1)))
2872 (with-current-buffer " *ex-working-space*"
2873 (skip-chars-forward " \t")
2874 (if (looking-at "!")
2878 (skip-chars-forward " \t")
2881 (setq command
(buffer-substring (mark) (point))))
2883 (re-search-forward "[ \t\n]")
2885 (setq file
(buffer-substring (point) (mark)))))
2887 (shell-command command t
)
2889 (insert-file file
)))))
2893 (read-variable "Variable: ")
2894 (eval (read-minibuffer "Value: ")))))
2898 (vip-change-mode-to-emacs)
2901 (defun ex-substitute (&optional repeat r-flag
)
2903 If REPEAT use previous reg-exp which is ex-reg-exp or
2905 (let (pat repl
(opt-g nil
) (opt-c nil
) (matched-pos nil
))
2906 (if repeat
(setq ex-token nil
) (vip-get-ex-pat))
2908 (setq pat
(if r-flag vip-s-string ex-reg-exp
)
2910 (setq pat
(if (string= ex-token
"") vip-s-string ex-token
))
2911 (setq vip-s-string pat
2919 (while (vip-get-ex-opt-gc)
2920 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
2924 (if ex-addresses
(goto-char (car ex-addresses
)))
2926 (forward-line (1- ex-count
))
2927 (setq ex-addresses
(cons (point) (cons (mark) nil
))))
2928 (if (null ex-addresses
)
2929 (setq ex-addresses
(cons (point) (cons (point) nil
)))
2930 (if (null (cdr ex-addresses
))
2931 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
2933 (let ((beg (car ex-addresses
)) (end (car (cdr ex-addresses
)))
2936 (vip-enlarge-region beg end
)
2937 (let ((limit (save-excursion
2938 (goto-char (max (point) (mark)))
2940 (goto-char (min (point) (mark)))
2941 (while (< (point) limit
)
2943 (setq eol-mark
(point-marker))
2947 (while (and (not (eolp))
2948 (re-search-forward pat eol-mark t
))
2949 (if (or (not opt-c
) (y-or-n-p "Replace? "))
2951 (setq matched-pos
(point))
2952 (replace-match repl
))))
2955 (if (and (re-search-forward pat eol-mark t
)
2956 (or (not opt-c
) (y-or-n-p "Replace? ")))
2958 (setq matched-pos
(point))
2959 (replace-match repl
)))
2962 (if matched-pos
(goto-char matched-pos
))
2964 (if opt-c
(message "done"))))
2969 (with-current-buffer " *ex-working-space*"
2970 (skip-chars-forward " \t")
2972 (skip-chars-forward "^ |\t\n")
2973 (setq tag
(buffer-substring (mark) (point))))
2974 (if (not (string= tag
"")) (setq ex-tag tag
))
2975 (vip-change-mode-to-emacs)
2976 (condition-case conditions
2978 (if (string= tag
"")
2980 (find-tag-other-window ex-tag
))
2981 (vip-change-mode-to-vi))
2983 (vip-change-mode-to-vi)
2984 (vip-message-conditions conditions
)))))
2986 (defun ex-write (q-flag)
2988 (vip-default-ex-addresses t
)
2990 (if (string= ex-file
"")
2992 (if (null buffer-file-name
)
2993 (error "No file associated with this buffer"))
2994 (setq ex-file buffer-file-name
))
2995 (setq ex-file
(expand-file-name ex-file
)))
2996 (if (and (not (string= ex-file
(buffer-file-name)))
2997 (file-exists-p ex-file
)
2999 (error "\"%s\" File exists - use w! to override" ex-file
))
3000 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3001 (if (> beg end
) (error "First address exceeds second"))
3003 (vip-enlarge-region beg end
)
3004 (write-region (point) (mark) ex-file ex-append t
)))
3005 (if (null buffer-file-name
) (setq buffer-file-name ex-file
))
3006 (if q-flag
(save-buffers-kill-emacs)))
3010 (vip-default-ex-addresses)
3012 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3013 (if (> beg end
) (error "First address exceeds second"))
3015 (vip-enlarge-region beg end
)
3016 (exchange-point-and-mark)
3017 (if (or ex-g-flag ex-g-variant
) (error "Can't yank within global"))
3021 (forward-line (1- ex-count
)))
3023 (vip-enlarge-region (point) (mark))
3024 (if ex-flag
(error "Extra characters at end of command"))
3026 (copy-to-register ex-buffer
(point) (mark) nil
))
3027 (copy-region-as-kill (point) (mark)))))
3029 (defun ex-command ()
3030 "execute shell command"
3032 (with-current-buffer " *ex-working-space*"
3033 (skip-chars-forward " \t")
3036 (setq command
(buffer-substring (mark) (point))))
3037 (if (null ex-addresses
)
3038 (shell-command command
)
3039 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3040 (if (null beg
) (setq beg end
))
3044 (vip-enlarge-region (point) (mark))
3045 (shell-command-on-region (point) (mark) command t t
))
3048 (defun ex-line-no ()
3053 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
3055 (if (file-exists-p vip-startup-file
) (load vip-startup-file
))
3059 ;;; vip.el ends here