1 ;;; vip.el --- a VI Package for GNU Emacs
3 ;; Copyright (C) 1986-1988, 1992-1993, 1998, 2001-2012
4 ;; Free Software Foundation, Inc.
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
(convert-standard-filename "~/.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
" " 'vip-scroll
)
187 (define-key map
"!" 'vip-command-argument
)
188 (define-key map
"\"" 'vip-command-argument
)
189 (define-key map
"#" 'vip-command-argument
)
190 (define-key map
"$" 'vip-goto-eol
)
191 (define-key map
"%" 'vip-paren-match
)
192 (define-key map
"&" 'vip-nil
)
193 (define-key map
"'" 'vip-goto-mark-and-skip-white
)
194 (define-key map
"(" 'vip-backward-sentence
)
195 (define-key map
")" 'vip-forward-sentence
)
196 (define-key map
"*" 'call-last-kbd-macro
)
197 (define-key map
"+" 'vip-next-line-at-bol
)
198 (define-key map
"," 'vip-repeat-find-opposite
)
199 (define-key map
"-" 'vip-previous-line-at-bol
)
200 (define-key map
"." 'vip-repeat
)
201 (define-key map
"/" 'vip-search-forward
)
203 (define-key map
"0" 'vip-beginning-of-line
)
204 (define-key map
"1" 'vip-digit-argument
)
205 (define-key map
"2" 'vip-digit-argument
)
206 (define-key map
"3" 'vip-digit-argument
)
207 (define-key map
"4" 'vip-digit-argument
)
208 (define-key map
"5" 'vip-digit-argument
)
209 (define-key map
"6" 'vip-digit-argument
)
210 (define-key map
"7" 'vip-digit-argument
)
211 (define-key map
"8" 'vip-digit-argument
)
212 (define-key map
"9" 'vip-digit-argument
)
214 (define-key map
":" 'vip-ex
)
215 (define-key map
";" 'vip-repeat-find
)
216 (define-key map
"<" 'vip-command-argument
)
217 (define-key map
"=" 'vip-command-argument
)
218 (define-key map
">" 'vip-command-argument
)
219 (define-key map
"?" 'vip-search-backward
)
220 (define-key map
"@" 'vip-nil
)
222 (define-key map
"A" 'vip-Append
)
223 (define-key map
"B" 'vip-backward-Word
)
224 (define-key map
"C" 'vip-ctl-c-equivalent
)
225 (define-key map
"D" 'vip-kill-line
)
226 (define-key map
"E" 'vip-end-of-Word
)
227 (define-key map
"F" 'vip-find-char-backward
)
228 (define-key map
"G" 'vip-goto-line
)
229 (define-key map
"H" 'vip-window-top
)
230 (define-key map
"I" 'vip-Insert
)
231 (define-key map
"J" 'vip-join-lines
)
232 (define-key map
"K" 'vip-kill-buffer
)
233 (define-key map
"L" 'vip-window-bottom
)
234 (define-key map
"M" 'vip-window-middle
)
235 (define-key map
"N" 'vip-search-Next
)
236 (define-key map
"O" 'vip-Open-line
)
237 (define-key map
"P" 'vip-Put-back
)
238 (define-key map
"Q" 'vip-query-replace
)
239 (define-key map
"R" 'vip-replace-string
)
240 (define-key map
"S" 'vip-switch-to-buffer-other-window
)
241 (define-key map
"T" 'vip-goto-char-backward
)
242 (define-key map
"U" 'vip-nil
)
243 (define-key map
"V" 'vip-find-file-other-window
)
244 (define-key map
"W" 'vip-forward-Word
)
245 (define-key map
"X" 'vip-ctl-x-equivalent
)
246 (define-key map
"Y" 'vip-yank-line
)
247 (define-key map
"ZZ" 'save-buffers-kill-emacs
)
249 (define-key map
"[" 'vip-nil
)
250 (define-key map
"\\" 'vip-escape-to-emacs
)
251 (define-key map
"]" 'vip-nil
)
252 (define-key map
"^" 'vip-bol-and-skip-white
)
253 (define-key map
"_" 'vip-nil
)
254 (define-key map
"`" 'vip-goto-mark
)
256 (define-key map
"a" 'vip-append
)
257 (define-key map
"b" 'vip-backward-word
)
258 (define-key map
"c" 'vip-command-argument
)
259 (define-key map
"d" 'vip-command-argument
)
260 (define-key map
"e" 'vip-end-of-word
)
261 (define-key map
"f" 'vip-find-char-forward
)
262 (define-key map
"g" 'vip-info-on-file
)
263 (define-key map
"h" 'vip-backward-char
)
264 (define-key map
"i" 'vip-insert
)
265 (define-key map
"j" 'vip-next-line
)
266 (define-key map
"k" 'vip-previous-line
)
267 (define-key map
"l" 'vip-forward-char
)
268 (define-key map
"m" 'vip-mark-point
)
269 (define-key map
"n" 'vip-search-next
)
270 (define-key map
"o" 'vip-open-line
)
271 (define-key map
"p" 'vip-put-back
)
272 (define-key map
"q" 'vip-nil
)
273 (define-key map
"r" 'vip-replace-char
)
274 (define-key map
"s" 'vip-switch-to-buffer
)
275 (define-key map
"t" 'vip-goto-char-forward
)
276 (define-key map
"u" 'vip-undo
)
277 (define-key map
"v" 'vip-find-file
)
278 (define-key map
"w" 'vip-forward-word
)
279 (define-key map
"x" 'vip-delete-char
)
280 (define-key map
"y" 'vip-command-argument
)
281 (define-key map
"zH" 'vip-line-to-top
)
282 (define-key map
"zM" 'vip-line-to-middle
)
283 (define-key map
"zL" 'vip-line-to-bottom
)
284 (define-key map
"z\C-m" 'vip-line-to-top
)
285 (define-key map
"z." 'vip-line-to-middle
)
286 (define-key map
"z-" 'vip-line-to-bottom
)
288 (define-key map
"{" 'vip-backward-paragraph
)
289 (define-key map
"|" 'vip-goto-col
)
290 (define-key map
"}" 'vip-forward-paragraph
)
291 (define-key map
"~" 'vip-nil
)
292 (define-key map
"\177" 'vip-delete-backward-char
)
295 (defun vip-version ()
297 (message "VIP version 3.5 of September 15, 1987"))
304 "Set up bindings for C-x 7 and C-z that are useful for VIP users."
305 (define-key ctl-x-map
"7" 'vip-buffer-in-two-windows
)
306 (global-set-key "\C-z" 'vip-change-mode-to-vi
))
308 (defmacro vip-loop
(count body
)
309 "(COUNT BODY) Execute BODY COUNT times."
310 (list 'let
(list (list 'count count
))
311 (list 'while
(list '> 'count
0)
313 (list 'setq
'count
(list '1-
'count
)))))
315 (defun vip-push-mark-silent (&optional location
)
316 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
320 (setq mark-ring
(cons (copy-marker (mark-marker)) mark-ring
))
321 (if (> (length mark-ring
) mark-ring-max
)
323 (move-marker (car (nthcdr mark-ring-max mark-ring
)) nil
)
324 (setcdr (nthcdr (1- mark-ring-max
) mark-ring
) nil
))))
325 (set-mark (or location
(point))))
327 (defun vip-goto-col (arg)
328 "Go to ARG's column."
330 (let ((val (vip-p-val arg
))
331 (com (vip-getcom arg
)))
334 (if (> val
(1+ (current-column))) (error "")))
335 (if com
(move-marker vip-com-point
(point)))
337 (forward-char (1- val
))
338 (if com
(vip-execute-com 'vip-goto-col val com
))))
340 (defun vip-copy-keymap (map)
341 (if (null map
) (make-sparse-keymap) (copy-keymap map
)))
346 (defun vip-change-mode (new-mode)
347 "Change mode to NEW-MODE---either emacs-mode, vi-mode, or insert-mode."
348 (or (eq new-mode vip-current-mode
)
350 (cond ((eq new-mode
'vi-mode
)
351 (if (eq vip-current-mode
'insert-mode
)
353 (vip-copy-region-as-kill (point) vip-insert-point
)
354 (vip-repeat-insert-command))
355 (setq vip-emacs-local-map
(current-local-map)
356 vip-emacs-mode-line-buffer-identification
357 mode-line-buffer-identification
358 vip-insert-local-map
(vip-copy-keymap
359 (current-local-map))))
360 (vip-change-mode-line "Vi: ")
361 (use-local-map vip-mode-map
))
362 ((eq new-mode
'insert-mode
)
363 (move-marker vip-insert-point
(point))
364 (if (eq vip-current-mode
'emacs-mode
)
365 (setq vip-emacs-local-map
(current-local-map)
366 vip-emacs-mode-line-buffer-identification
367 mode-line-buffer-identification
368 vip-insert-local-map
(vip-copy-keymap
369 (current-local-map)))
370 (setq vip-insert-local-map
(vip-copy-keymap
371 vip-emacs-local-map
)))
372 (vip-change-mode-line "Insert")
373 (use-local-map vip-insert-local-map
)
374 (define-key vip-insert-local-map
"\e" 'vip-change-mode-to-vi
)
375 (define-key vip-insert-local-map
"\C-z" 'vip-ESC
)
376 (define-key vip-insert-local-map
"\C-h"
377 (if vip-help-in-insert-mode
'help-command
378 'delete-backward-char
))
379 (define-key vip-insert-local-map
"\C-w"
380 'vip-delete-backward-word
))
381 ((eq new-mode
'emacs-mode
)
382 (vip-change-mode-line "Emacs:")
383 (use-local-map vip-emacs-local-map
)))
384 (setq vip-current-mode new-mode
)
385 (force-mode-line-update))))
387 (defun vip-copy-region-as-kill (beg end
)
388 "If BEG and END do not belong to the same buffer, it copies empty region."
390 (copy-region-as-kill beg end
)
391 (error (copy-region-as-kill beg beg
))))
393 (defun vip-change-mode-line (string)
394 "Assuming that the mode line format contains the string \"Emacs:\", this
395 function replaces the string by \"Vi: \" etc."
396 (setq mode-line-buffer-identification
397 (if (string= string
"Emacs:")
398 vip-emacs-mode-line-buffer-identification
399 (list (concat string
" %17b")))))
403 "Turn on VIP emulation of VI."
405 (if (not vip-inhibit-startup-message
)
407 (switch-to-buffer "VIP Startup Message")
410 "VIP is a Vi emulation package for GNU Emacs. VIP provides most Vi commands
411 including Ex commands. VIP is however different from Vi in several points.
412 You can get more information on VIP by:
413 1. Typing `M-x info' and selecting menu item \"vip\".
414 2. Typing `C-h k' followed by a key whose description you want.
415 3. Printing VIP manual which can be found as GNU/man/vip.texinfo
416 4. Printing VIP Reference Card which can be found as GNU/etc/vipcard.tex
418 This startup message appears whenever you load VIP unless you type `y' now.
419 Type `n' to quit this window for now.\n")
420 (goto-char (point-min))
421 (if (y-or-n-p "Inhibit VIP startup message? ")
425 (substitute-in-file-name vip-startup-file
))
426 (goto-char (point-max))
427 (insert "\n(setq vip-inhibit-startup-message t)\n")
429 (kill-buffer (current-buffer)))
430 (message "VIP startup message inhibited.")
432 (kill-buffer (current-buffer))
434 (setq vip-inhibit-startup-message t
)))
435 (vip-change-mode-to-vi))
437 (defun vip-change-mode-to-vi ()
438 "Change mode to vi mode."
440 (vip-change-mode 'vi-mode
))
442 (defun vip-change-mode-to-insert ()
443 "Change mode to insert mode."
445 (vip-change-mode 'insert-mode
))
447 (defun vip-change-mode-to-emacs ()
448 "Change mode to Emacs mode."
450 (vip-change-mode 'emacs-mode
))
453 ;; escape to emacs mode temporarily
455 (defun vip-escape-to-emacs (arg &optional events
)
456 "Escape to Emacs mode for one Emacs command.
457 ARG is used as the prefix value for the executed command. If
458 EVENTS is a list of events, which become the beginning of the command."
460 (let (com key
(old-map (current-local-map)))
461 (if events
(setq unread-command-events events
))
462 (setq prefix-arg arg
)
463 (use-local-map vip-emacs-local-map
)
465 (setq com
(key-binding (setq key
(read-key-sequence nil
))))
466 (use-local-map old-map
))
467 (command-execute com prefix-arg
)
468 (setq prefix-arg nil
) ;; reset prefix arg
471 (defun vip-message-conditions (conditions)
472 "Print CONDITIONS as a message."
473 (let ((case (car conditions
)) (msg (cdr conditions
)))
476 (message "%s %s" case
(prin1-to-string msg
)))
480 "Emulate ESC key in Emacs mode."
482 (vip-escape-to-emacs arg
'(?\e
)))
484 (defun vip-ctl-c (arg)
485 "Emulate C-c key in Emacs mode."
487 (vip-escape-to-emacs arg
'(?\C-c
)))
489 (defun vip-ctl-x (arg)
490 "Emulate C-x key in Emacs mode."
492 (vip-escape-to-emacs arg
'(?\C-x
)))
494 (defun vip-ctl-h (arg)
495 "Emulate C-h key in Emacs mode."
497 (vip-escape-to-emacs arg
'(?\C-h
)))
500 ;; prefix argument for vi mode
502 ;; In vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
503 ;; represents the numeric value of the prefix argument and COM represents
504 ;; command prefix such as "c", "d", "m" and "y".
506 (defun vip-prefix-arg-value (char value com
)
507 "Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value
508 obtained so far, and COM is the command part obtained so far."
509 (while (and (>= char ?
0) (<= char ?
9))
510 (setq value
(+ (* (if (numberp value
) value
0) 10) (- char ?
0)))
511 (setq char
(read-char)))
512 (setq prefix-arg value
)
513 (if com
(setq prefix-arg
(cons prefix-arg com
)))
515 (vip-describe-arg prefix-arg
)
516 (setq char
(read-char)))
517 (setq unread-command-events
(list char
)))
519 (defun vip-prefix-arg-com (char value com
)
520 "Vi operator as prefix argument."
523 (or (= char ?c
) (= char ?d
) (= char ?y
)
524 (= char ?
!) (= char ?
<) (= char ?
>) (= char ?
=)
525 (= char ?
#) (= char ?r
) (= char ?R
) (= char ?
\")))
527 ;; this means that we already have a command character, so we
528 ;; construct a com list and exit while. however, if char is "
531 ;; new com is (CHAR . OLDCOM)
532 (if (or (= char ?
#) (= char ?
\")) (error ""))
533 (setq com
(cons char com
))
535 ;; if com is nil we set com as char, and read more. again, if char
536 ;; is ", we read the name of register and store it in vip-use-register.
537 ;; if char is !, =, or #, a complete com is formed so we exit while.
538 (cond ((or (= char ?
!) (= char ?
=))
540 (setq char
(read-char))
543 ;; read a char and encode it as com
544 (setq com
(+ 128 (read-char)))
545 (setq char
(read-char))
547 ((or (= char ?
<) (= char ?
>))
549 (setq char
(read-char))
550 (if (= com char
) (setq com
(cons char com
)))
553 (let ((reg (read-char)))
554 (if (or (and (<= ?A reg
) (<= reg ?z
))
555 (and (<= ?
1 reg
) (<= reg ?
9)))
556 (setq vip-use-register reg
)
558 (setq char
(read-char))))
561 (setq char
(read-char)))))))
563 ;; com is a single char, so we construct prefix-arg
564 ;; and if char is ?, describe prefix arg, otherwise exit by
565 ;; pushing the char back
567 (setq prefix-arg
(cons value com
))
569 (vip-describe-arg prefix-arg
)
570 (setq char
(read-char)))
571 (setq unread-command-events
(list char
)))
572 ;; as com is non-nil, this means that we have a command to execute
573 (if (or (= (car com
) ?r
) (= (car com
) ?R
))
574 ;; execute appropriate region command.
575 (let ((char (car com
)) (com (cdr com
)))
576 (setq prefix-arg
(cons value com
))
577 (if (= char ?r
) (vip-region prefix-arg
)
578 (vip-Region prefix-arg
))
580 (setq prefix-arg nil
))
581 ;; otherwise, reset prefix arg and call appropriate command
582 (setq value
(if (null value
) 1 value
))
583 (setq prefix-arg nil
)
584 (cond ((equal com
'(?c . ?c
)) (vip-line (cons value ?C
)))
585 ((equal com
'(?d . ?d
)) (vip-line (cons value ?D
)))
586 ((equal com
'(?d . ?y
)) (vip-yank-defun))
587 ((equal com
'(?y . ?y
)) (vip-line (cons value ?Y
)))
588 ((equal com
'(?
< . ?
<)) (vip-line (cons value ?
<)))
589 ((equal com
'(?
> . ?
>)) (vip-line (cons value ?
>)))
590 ((equal com
'(?
! . ?
!)) (vip-line (cons value ?
!)))
591 ((equal com
'(?
= . ?
=)) (vip-line (cons value ?
=)))
594 (defun vip-describe-arg (arg)
596 (setq val
(vip-P-val arg
)
597 com
(vip-getcom arg
))
600 (message "Value is nil, and command is nil.")
601 (message "Value is nil, and command is %c." com
))
603 (message "Value is %d, and command is nil." val
)
604 (message "Value is %d, and command is %c." val com
)))))
606 (defun vip-digit-argument (arg)
607 "Begin numeric argument for the next command."
609 (vip-prefix-arg-value last-command-event nil
610 (if (consp arg
) (cdr arg
) nil
)))
612 (defun vip-command-argument (arg)
613 "Accept a motion command as an argument."
615 (condition-case conditions
618 (cond ((null arg
) nil
)
619 ((consp arg
) (car arg
))
621 (t (error "strange arg")))
622 (cond ((null arg
) nil
)
623 ((consp arg
) (cdr arg
))
625 (t (error "strange arg"))))
627 (setq vip-use-register nil
)
628 (signal 'quit nil
))))
630 (defun vip-p-val (arg)
631 "Get value part of prefix-argument ARG."
633 ((consp arg
) (if (null (car arg
)) 1 (car arg
)))
636 (defun vip-P-val (arg)
637 "Get value part of prefix-argument ARG."
638 (cond ((consp arg
) (car arg
))
641 (defun vip-getcom (arg)
642 "Get com part of prefix-argument ARG."
643 (cond ((null arg
) nil
)
644 ((consp arg
) (cdr arg
))
647 (defun vip-getCom (arg)
648 "Get com part of prefix-argument ARG and modify it."
649 (let ((com (vip-getcom arg
)))
650 (cond ((equal com ?c
) ?C
)
656 ;; repeat last destructive command
658 (defun vip-append-to-register (reg start end
)
659 "Append region to text in register REG.
660 START and END are buffer positions indicating what to append."
661 (set-register reg
(concat (or (get-register reg
) "")
662 (buffer-substring start end
))))
664 (defun vip-execute-com (m-com val com
)
665 "(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set
666 to vip-d-com for later use by vip-repeat"
667 (let ((reg vip-use-register
))
669 (cond ((= com ?c
) (vip-change vip-com-point
(point)))
670 ((= com
(- ?c
)) (vip-change-subr vip-com-point
(point)))
671 ((or (= com ?C
) (= com
(- ?C
)))
673 (set-mark vip-com-point
)
674 (vip-enlarge-region (mark) (point))
677 (cond ((and (<= ?a vip-use-register
)
678 (<= vip-use-register ?z
))
680 vip-use-register
(mark) (point) nil
))
681 ((and (<= ?A vip-use-register
)
682 (<= vip-use-register ?Z
))
683 (vip-append-to-register
684 (+ vip-use-register
32) (mark) (point)))
685 (t (setq vip-use-register nil
)
687 (setq vip-use-register nil
)))
688 (delete-region (mark) (point)))
690 (if (= com ?C
) (vip-change-mode-to-insert) (yank)))
694 (cond ((and (<= ?a vip-use-register
)
695 (<= vip-use-register ?z
))
697 vip-use-register vip-com-point
(point) nil
))
698 ((and (<= ?A vip-use-register
)
699 (<= vip-use-register ?Z
))
700 (vip-append-to-register
701 (+ vip-use-register
32) vip-com-point
(point)))
702 (t (setq vip-use-register nil
)
704 (setq vip-use-register nil
)))
706 (if (eq last-command
'd-command
) 'kill-region nil
))
707 (kill-region vip-com-point
(point))
708 (setq this-command
'd-command
))
711 (set-mark vip-com-point
)
712 (vip-enlarge-region (mark) (point))
715 (cond ((and (<= ?a vip-use-register
)
716 (<= vip-use-register ?z
))
718 vip-use-register
(mark) (point) nil
))
719 ((and (<= ?A vip-use-register
)
720 (<= vip-use-register ?Z
))
721 (vip-append-to-register
722 (+ vip-use-register
32) (mark) (point)))
723 (t (setq vip-use-register nil
)
725 (setq vip-use-register nil
)))
727 (if (eq last-command
'D-command
) 'kill-region nil
))
728 (kill-region (mark) (point))
729 (if (eq m-com
'vip-line
) (setq this-command
'D-command
)))
730 (back-to-indentation))
734 (cond ((and (<= ?a vip-use-register
)
735 (<= vip-use-register ?z
))
737 vip-use-register vip-com-point
(point) nil
))
738 ((and (<= ?A vip-use-register
)
739 (<= vip-use-register ?Z
))
740 (vip-append-to-register
741 (+ vip-use-register
32) vip-com-point
(point)))
742 (t (setq vip-use-register nil
)
744 (setq vip-use-register nil
)))
745 (setq last-command nil
)
746 (copy-region-as-kill vip-com-point
(point))
747 (goto-char vip-com-point
))
750 (set-mark vip-com-point
)
751 (vip-enlarge-region (mark) (point))
754 (cond ((and (<= ?a vip-use-register
)
755 (<= vip-use-register ?z
))
757 vip-use-register
(mark) (point) nil
))
758 ((and (<= ?A vip-use-register
)
759 (<= vip-use-register ?Z
))
760 (vip-append-to-register
761 (+ vip-use-register
32) (mark) (point)))
762 (t (setq vip-use-register nil
)
764 (setq vip-use-register nil
)))
765 (setq last-command nil
)
766 (copy-region-as-kill (mark) (point)))
767 (goto-char vip-com-point
))
768 ((or (= com ?
!) (= com
(- ?
!)))
770 (set-mark vip-com-point
)
771 (vip-enlarge-region (mark) (point))
772 (shell-command-on-region
775 (setq vip-last-shell-com
(vip-read-string "!"))
780 (set-mark vip-com-point
)
781 (vip-enlarge-region (mark) (point))
782 (if (> (mark) (point)) (exchange-point-and-mark))
783 (indent-region (mark) (point) nil
)))
786 (set-mark vip-com-point
)
787 (vip-enlarge-region (mark) (point))
788 (indent-rigidly (mark) (point) (- vip-shift-width
)))
789 (goto-char vip-com-point
))
792 (set-mark vip-com-point
)
793 (vip-enlarge-region (mark) (point))
794 (indent-rigidly (mark) (point) vip-shift-width
))
795 (goto-char vip-com-point
))
797 ;; this is special command #
798 (vip-special-prefix-com (- com
128)))))
799 (setq vip-d-com
(list m-com val
(if (or (= com ?c
) (= com ?C
) (= com ?
!))
803 (defun vip-repeat (arg)
804 "(ARG) Re-execute last destructive command. vip-d-com has the form
805 \(COM ARG CH REG), where COM is the command to be re-executed, ARG is the
806 argument for COM, CH is a flag for repeat, and REG is optional and if exists
807 is the name of the register for COM."
809 (if (eq last-command
'vip-undo
)
810 ;; if the last command was vip-undo, then undo-more
812 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
813 ;; its prefix value is used as new prefix value for the command.
814 (let ((m-com (car vip-d-com
))
815 (val (vip-P-val arg
))
816 (com (car (cdr (cdr vip-d-com
))))
817 (reg (nth 3 vip-d-com
)))
818 (if (null val
) (setq val
(car (cdr vip-d-com
))))
819 (if (null m-com
) (error "No previous command to repeat"))
820 (setq vip-use-register reg
)
821 (funcall m-com
(cons val com
)))))
823 (defun vip-special-prefix-com (char)
824 "This command is invoked interactively by the key sequence #<char>"
826 (downcase-region (min vip-com-point
(point))
827 (max vip-com-point
(point))))
829 (upcase-region (min vip-com-point
(point))
830 (max vip-com-point
(point))))
832 (set-mark vip-com-point
)
833 (vip-global-execute))
835 (set-mark vip-com-point
)
837 ((= char ?s
) (ispell-region vip-com-point
(point)))))
843 "Undo previous change."
848 (setq this-command
'vip-undo
))
850 (defun vip-undo-more ()
851 "Continue undoing previous changes."
852 (message "undo more!")
854 (setq this-command
'vip-undo
))
859 (defun vip-string-tail (str)
860 (if (or (null str
) (string= str
"")) nil
863 (defun vip-yank-defun ()
865 (copy-region-as-kill (point) (mark)))
867 (defun vip-enlarge-region (beg end
)
868 "Enlarge region between BEG and END."
870 (progn (goto-char beg
) (set-mark end
))
874 (exchange-point-and-mark)
875 (if (or (not (eobp)) (not (bolp))) (with-no-warnings (next-line 1)))
877 (if (> beg end
) (exchange-point-and-mark)))
879 (defun vip-global-execute ()
880 "Call last keyboard macro for each line in the region."
881 (if (> (point) (mark)) (exchange-point-and-mark))
883 (call-last-kbd-macro)
884 (while (< (point) (mark))
887 (call-last-kbd-macro)))
889 (defun vip-quote-region ()
890 "Quote region by inserting the user supplied string at the beginning of
891 each line in the region."
892 (setq vip-quote-string
894 (vip-read-string (format "quote string (default %s): "
896 (if (string= str
"") vip-quote-string str
)))
897 (vip-enlarge-region (point) (mark))
898 (if (> (point) (mark)) (exchange-point-and-mark))
899 (insert vip-quote-string
)
902 (while (and (< (point) (mark)) (bolp))
903 (insert vip-quote-string
)
907 (defun vip-end-with-a-newline-p (string)
908 "Check if the string ends with a newline."
909 (or (string= string
"")
910 (= (aref string
(1- (length string
))) ?
\n)))
912 (defvar vip-save-minibuffer-local-map
)
914 (defun vip-read-string (prompt &optional init
)
915 (setq vip-save-minibuffer-local-map
(copy-keymap minibuffer-local-map
))
916 (define-key minibuffer-local-map
"\C-h" 'backward-char
)
917 (define-key minibuffer-local-map
"\C-w" 'backward-word
)
918 (define-key minibuffer-local-map
"\e" 'exit-minibuffer
)
920 (condition-case conditions
921 (setq str
(read-string prompt init
))
923 (setq minibuffer-local-map vip-save-minibuffer-local-map
)
925 (setq minibuffer-local-map vip-save-minibuffer-local-map
)
929 ;; insertion commands
931 (defun vip-repeat-insert-command ()
932 "This function is called when mode changes from insertion mode to
933 vi command mode. It will repeat the insertion command if original insertion
934 command was invoked with argument > 1."
935 (let ((i-com (car vip-d-com
)) (val (car (cdr vip-d-com
))))
936 (if (and val
(> val
1)) ;; first check that val is non-nil
938 (setq vip-d-com
(list i-com
(1- val
) ?r
))
940 (setq vip-d-com
(list i-com val ?r
))))))
942 (defun vip-insert (arg) ""
944 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
945 (setq vip-d-com
(list 'vip-insert val ?r
))
946 (if com
(vip-loop val
(yank))
947 (vip-change-mode-to-insert))))
949 (defun vip-append (arg)
950 "Append after point."
952 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
953 (setq vip-d-com
(list 'vip-append val ?r
))
954 (if (not (eolp)) (forward-char))
956 (vip-loop val
(yank))
957 (vip-change-mode-to-insert))))
959 (defun vip-Append (arg)
960 "Append at end of line."
962 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
963 (setq vip-d-com
(list 'vip-Append val ?r
))
966 (vip-loop val
(yank))
967 (vip-change-mode-to-insert))))
969 (defun vip-Insert (arg)
970 "Insert before first non-white."
972 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
973 (setq vip-d-com
(list 'vip-Insert val ?r
))
974 (back-to-indentation)
976 (vip-loop val
(yank))
977 (vip-change-mode-to-insert))))
979 (defun vip-open-line (arg)
982 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
983 (setq vip-d-com
(list 'vip-open-line val ?r
))
984 (let ((col (current-indentation)))
990 (if vip-open-with-indent
(indent-to col
))
994 (if vip-open-with-indent
(indent-to col
))
995 (vip-change-mode-to-insert)))))
997 (defun vip-Open-line (arg)
1000 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1001 (setq vip-d-com
(list 'vip-Open-line val ?r
))
1002 (let ((col (current-indentation)))
1008 (if vip-open-with-indent
(indent-to col
))
1012 (if vip-open-with-indent
(indent-to col
))
1013 (vip-change-mode-to-insert)))))
1015 (defun vip-open-line-at-point (arg)
1016 "Open line at point."
1018 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1019 (setq vip-d-com
(list 'vip-open-line-at-point val ?r
))
1026 (vip-change-mode-to-insert))))
1028 (defun vip-substitute (arg)
1029 "Substitute characters."
1031 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1036 (vip-change-subr (mark) (point))
1037 (vip-change (mark) (point))))
1038 (setq vip-d-com
(list 'vip-substitute val ?r
))))
1040 (defun vip-substitute-line (arg)
1043 (vip-line (cons arg ?C
)))
1048 (defun vip-line (arg)
1049 (let ((val (car arg
)) (com (cdr arg
)))
1050 (move-marker vip-com-point
(point))
1051 (with-no-warnings (next-line (1- val
)))
1052 (vip-execute-com 'vip-line val com
)))
1054 (defun vip-yank-line (arg)
1055 "Yank ARG lines (in vi's sense)"
1057 (let ((val (vip-p-val arg
)))
1058 (vip-line (cons val ?Y
))))
1063 (defun vip-region (arg)
1065 (let ((val (vip-P-val arg
))
1066 (com (vip-getcom arg
)))
1067 (move-marker vip-com-point
(point))
1068 (exchange-point-and-mark)
1069 (vip-execute-com 'vip-region val com
)))
1071 (defun vip-Region (arg)
1073 (let ((val (vip-P-val arg
))
1074 (com (vip-getCom arg
)))
1075 (move-marker vip-com-point
(point))
1076 (exchange-point-and-mark)
1077 (vip-execute-com 'vip-Region val com
)))
1079 (defun vip-replace-char (arg)
1080 "Replace the following ARG chars by the character read."
1082 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1083 (setq vip-d-com
(list 'vip-replace-char val ?r
))
1084 (vip-replace-char-subr (if (equal com ?r
) vip-d-char
(read-char)) val
)))
1086 (defun vip-replace-char-subr (char arg
)
1088 (setq vip-d-char char
)
1089 (vip-loop (if (> arg
0) arg
(- arg
)) (insert char
))
1090 (backward-char arg
))
1092 (defun vip-replace-string ()
1093 "Replace string. If you supply null string as the string to be replaced,
1094 the query replace mode will toggle between string replace and regexp replace."
1097 (setq str
(vip-read-string
1098 (if vip-re-replace
"Replace regexp: " "Replace string: ")))
1099 (if (string= str
"")
1101 (setq vip-re-replace
(not vip-re-replace
))
1102 (message "Replace mode changed to %s."
1103 (if vip-re-replace
"regexp replace"
1108 ;; (vip-read-string (format "Replace regexp \"%s\" with: " str)))
1109 (while (re-search-forward str nil t
)
1110 (replace-match (vip-read-string
1111 (format "Replace regexp \"%s\" with: " str
))
1116 (vip-read-string (format "Replace \"%s\" with: " str
))))))))
1119 ;; basic cursor movement. j, k, l, m commands.
1121 (defun vip-forward-char (arg)
1122 "Move point right ARG characters (left if ARG negative).On reaching end
1123 of buffer, stop and signal error."
1125 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1126 (if com
(move-marker vip-com-point
(point)))
1128 (if com
(vip-execute-com 'vip-forward-char val com
))))
1130 (defun vip-backward-char (arg)
1131 "Move point left ARG characters (right if ARG negative). On reaching
1132 beginning of buffer, stop and signal error."
1134 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1135 (if com
(move-marker vip-com-point
(point)))
1137 (if com
(vip-execute-com 'vip-backward-char val com
))))
1142 (defun vip-forward-word (arg)
1145 (let ((val (vip-p-val arg
))
1146 (com (vip-getcom arg
)))
1147 (if com
(move-marker vip-com-point
(point)))
1149 (skip-chars-forward " \t\n")
1152 (if (or (= com ?c
) (= com
(- ?c
)))
1153 (progn (backward-word 1) (forward-word 1)))
1154 (if (or (= com ?d
) (= com ?y
))
1158 (skip-chars-forward " \t")))
1159 (vip-execute-com 'vip-forward-word val com
)))))
1161 (defun vip-end-of-word (arg)
1162 "Move point to end of current word."
1164 (let ((val (vip-p-val arg
))
1165 (com (vip-getcom arg
)))
1166 (if com
(move-marker vip-com-point
(point)))
1173 (vip-execute-com 'vip-end-of-word val com
)))))
1175 (defun vip-backward-word (arg)
1178 (let ((val (vip-p-val arg
))
1179 (com (vip-getcom arg
)))
1180 (if com
(move-marker vip-com-point
(point)))
1182 (if com
(vip-execute-com 'vip-backward-word val com
))))
1184 (defun vip-forward-Word (arg)
1185 "Forward word delimited by white character."
1187 (let ((val (vip-p-val arg
))
1188 (com (vip-getcom arg
)))
1189 (if com
(move-marker vip-com-point
(point)))
1190 (re-search-forward "[^ \t\n]*[ \t\n]+" nil t val
)
1193 (if (or (= com ?c
) (= com
(- ?c
)))
1194 (progn (backward-word 1) (forward-word 1)))
1195 (if (or (= com ?d
) (= com ?y
))
1199 (skip-chars-forward " \t")))
1200 (vip-execute-com 'vip-forward-Word val com
)))))
1202 (defun vip-end-of-Word (arg)
1203 "Move forward to end of word delimited by white character."
1205 (let ((val (vip-p-val arg
))
1206 (com (vip-getcom arg
)))
1207 (if com
(move-marker vip-com-point
(point)))
1209 (if (re-search-forward "[^ \t\n]+" nil t val
) (backward-char))
1213 (vip-execute-com 'vip-end-of-Word val com
)))))
1215 (defun vip-backward-Word (arg)
1216 "Backward word delimited by white character."
1218 (let ((val (vip-p-val arg
))
1219 (com (vip-getcom arg
)))
1220 (if com
(move-marker vip-com-point
(point)))
1221 (if (re-search-backward "[ \t\n]+[^ \t\n]+" nil t val
)
1223 (goto-char (point-min)))
1224 (if com
(vip-execute-com 'vip-backward-Word val com
))))
1226 (defun vip-beginning-of-line (arg)
1227 "Go to beginning of line."
1229 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1230 (if com
(move-marker vip-com-point
(point)))
1231 (beginning-of-line val
)
1232 (if com
(vip-execute-com 'vip-beginning-of-line val com
))))
1234 (defun vip-bol-and-skip-white (arg)
1235 "Beginning of line at first non-white character."
1237 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1238 (if com
(move-marker vip-com-point
(point)))
1239 (back-to-indentation)
1240 (if com
(vip-execute-com 'vip-bol-and-skip-white val com
))))
1242 (defun vip-goto-eol (arg)
1243 "Go to end of line."
1245 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1246 (if com
(move-marker vip-com-point
(point)))
1248 (if com
(vip-execute-com 'vip-goto-eol val com
))))
1250 (defun vip-next-line (arg)
1253 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1254 (if com
(move-marker vip-com-point
(point)))
1256 (setq this-command
'next-line
)
1257 (if com
(vip-execute-com 'vip-next-line val com
))))
1259 (defun vip-next-line-at-bol (arg)
1260 "Next line at beginning of line."
1262 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1263 (if com
(move-marker vip-com-point
(point)))
1264 (with-no-warnings (next-line val
))
1265 (back-to-indentation)
1266 (if com
(vip-execute-com 'vip-next-line-at-bol val com
))))
1268 (defun vip-previous-line (arg)
1269 "Go to previous line."
1271 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1272 (if com
(move-marker vip-com-point
(point)))
1273 (with-no-warnings (next-line (- val
)))
1274 (setq this-command
'previous-line
)
1275 (if com
(vip-execute-com 'vip-previous-line val com
))))
1277 (defun vip-previous-line-at-bol (arg)
1278 "Previous line at beginning of line."
1280 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1281 (if com
(move-marker vip-com-point
(point)))
1282 (with-no-warnings (next-line (- val
)))
1283 (back-to-indentation)
1284 (if com
(vip-execute-com 'vip-previous-line val com
))))
1286 (defun vip-change-to-eol (arg)
1287 "Change to end of line."
1289 (vip-goto-eol (cons arg ?c
)))
1291 (defun vip-kill-line (arg)
1294 (vip-goto-eol (cons arg ?d
)))
1299 (defun vip-goto-line (arg)
1300 "Go to ARG's line. Without ARG go to end of buffer."
1302 (let ((val (vip-P-val arg
)) (com (vip-getCom arg
)))
1303 (move-marker vip-com-point
(point))
1306 (goto-char (point-max))
1307 (goto-char (point-min))
1308 (forward-line (1- val
)))
1309 (back-to-indentation)
1310 (if com
(vip-execute-com 'vip-goto-line val com
))))
1312 (defun vip-find-char (arg char forward offset
)
1313 "Find ARG's occurrence of CHAR on the current line. If FORWARD then
1314 search is forward, otherwise backward. OFFSET is used to adjust point
1316 (let ((arg (if forward arg
(- arg
))) point
)
1321 ;; forward search begins here
1322 (if (eolp) (error "") (point))
1323 ;; forward search ends here
1324 (progn (with-no-warnings (next-line 1)) (beginning-of-line) (point)))
1326 ;; backward search begins from here
1327 (if (bolp) (error "") (point))
1328 ;; backward search ends here
1329 (progn (beginning-of-line) (point))))
1330 ;; if arg > 0, point is forwarded before search.
1331 (if (> arg
0) (goto-char (1+ (point-min)))
1332 (goto-char (point-max)))
1333 (let ((case-fold-search nil
))
1334 (search-forward (char-to-string char
) nil
0 arg
))
1335 (setq point
(point))
1336 (if (or (and (> arg
0) (= point
(point-max)))
1337 (and (< arg
0) (= point
(point-min))))
1339 (goto-char (+ point
(if (> arg
0) (if offset -
2 -
1) (if offset
1 0))))))
1341 (defun vip-find-char-forward (arg)
1342 "Find char on the line. If called interactively read the char to find
1343 from the terminal, and if called from vip-repeat, the char last used is
1344 used. This behavior is controlled by the sign of prefix numeric value."
1346 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1348 ;; this means that the function was called interactively
1349 (setq vip-f-char
(read-char)
1353 (if com
(move-marker vip-com-point
(point)))
1354 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t nil
)
1358 (setq vip-F-char vip-f-char
);; set new vip-F-char
1360 (vip-execute-com 'vip-find-char-forward val com
)))))
1362 (defun vip-goto-char-forward (arg)
1363 "Go up to char ARG forward on line."
1365 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1367 ;; this means that the function was called interactively
1368 (setq vip-f-char
(read-char)
1372 (if com
(move-marker vip-com-point
(point)))
1373 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t t
)
1377 (setq vip-F-char vip-f-char
);; set new vip-F-char
1379 (vip-execute-com 'vip-goto-char-forward val com
)))))
1381 (defun vip-find-char-backward (arg)
1382 "Find char ARG on line backward."
1384 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1386 ;; this means that the function was called interactively
1387 (setq vip-f-char
(read-char)
1391 (if com
(move-marker vip-com-point
(point)))
1393 val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil nil
)
1397 (setq vip-F-char vip-f-char
);; set new vip-F-char
1398 (vip-execute-com 'vip-find-char-backward val com
)))))
1400 (defun vip-goto-char-backward (arg)
1401 "Go up to char ARG backward on line."
1403 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1405 ;; this means that the function was called interactively
1406 (setq vip-f-char
(read-char)
1410 (if com
(move-marker vip-com-point
(point)))
1411 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil t
)
1415 (setq vip-F-char vip-f-char
);; set new vip-F-char
1416 (vip-execute-com 'vip-goto-char-backward val com
)))))
1418 (defun vip-repeat-find (arg)
1419 "Repeat previous find command."
1421 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1422 (if com
(move-marker vip-com-point
(point)))
1423 (vip-find-char val vip-f-char vip-f-forward vip-f-offset
)
1426 (if vip-f-forward
(forward-char))
1427 (vip-execute-com 'vip-repeat-find val com
)))))
1429 (defun vip-repeat-find-opposite (arg)
1430 "Repeat previous find command in the opposite direction."
1432 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1433 (if com
(move-marker vip-com-point
(point)))
1434 (vip-find-char val vip-f-char
(not vip-f-forward
) vip-f-offset
)
1437 (if vip-f-forward
(forward-char))
1438 (vip-execute-com 'vip-repeat-find-opposite val com
)))))
1441 ;; window scrolling etc.
1443 (defun vip-other-window (arg)
1444 "Switch to other window."
1447 (or (not (eq vip-current-mode
'emacs-mode
))
1448 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
1449 (vip-change-mode-to-vi)))
1451 (defun vip-window-top (arg)
1452 "Go to home window line."
1454 (let ((val (vip-p-val arg
))
1455 (com (vip-getCom arg
)))
1456 (if com
(move-marker vip-com-point
(point)))
1457 (move-to-window-line (1- val
))
1458 (if com
(vip-execute-com 'vip-window-top val com
))))
1460 (defun vip-window-middle (arg)
1461 "Go to middle window line."
1463 (let ((val (vip-p-val arg
))
1464 (com (vip-getCom arg
)))
1465 (if com
(move-marker vip-com-point
(point)))
1466 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val
)))
1467 (if com
(vip-execute-com 'vip-window-middle val com
))))
1469 (defun vip-window-bottom (arg)
1470 "Go to last window line."
1472 (let ((val (vip-p-val arg
))
1473 (com (vip-getCom arg
)))
1474 (if com
(move-marker vip-com-point
(point)))
1475 (move-to-window-line (- val
))
1476 (if com
(vip-execute-com 'vip-window-bottom val com
))))
1478 (defun vip-line-to-top (arg)
1479 "Put current line on the home line."
1481 (recenter (1- arg
)))
1483 (defun vip-line-to-middle (arg)
1484 "Put current line on the middle line."
1486 (recenter (+ (1- arg
) (/ (1- (window-height)) 2))))
1488 (defun vip-line-to-bottom (arg)
1489 "Put current line on the last line."
1491 (recenter (- (window-height) (1+ arg
))))
1496 (defun vip-paren-match (arg)
1497 "Go to the matching parenthesis."
1499 (let ((com (vip-getcom arg
)))
1501 (if (or (> arg
99) (< arg
1))
1502 (error "Prefix must be between 1 and 99")
1504 (if (> (point-max) 80000)
1505 (* (/ (point-max) 100) arg
)
1506 (/ (* (point-max) arg
) 100)))
1507 (back-to-indentation))
1508 (cond ((looking-at "[\(\[{]")
1509 (if com
(move-marker vip-com-point
(point)))
1512 (vip-execute-com 'vip-paren-match nil com
)
1514 ((looking-at "[])}]")
1516 (if com
(move-marker vip-com-point
(point)))
1518 (if com
(vip-execute-com 'vip-paren-match nil com
)))
1522 ;; sentence and paragraph
1524 (defun vip-forward-sentence (arg)
1527 (let ((val (vip-p-val arg
))
1528 (com (vip-getcom arg
)))
1529 (if com
(move-marker vip-com-point
(point)))
1530 (forward-sentence val
)
1531 (if com
(vip-execute-com 'vip-forward-sentence nil com
))))
1533 (defun vip-backward-sentence (arg)
1534 "Backward sentence."
1536 (let ((val (vip-p-val arg
))
1537 (com (vip-getcom arg
)))
1538 (if com
(move-marker vip-com-point
(point)))
1539 (backward-sentence val
)
1540 (if com
(vip-execute-com 'vip-backward-sentence nil com
))))
1542 (defun vip-forward-paragraph (arg)
1543 "Forward paragraph."
1545 (let ((val (vip-p-val arg
))
1546 (com (vip-getCom arg
)))
1547 (if com
(move-marker vip-com-point
(point)))
1548 (forward-paragraph val
)
1549 (if com
(vip-execute-com 'vip-forward-paragraph nil com
))))
1551 (defun vip-backward-paragraph (arg)
1552 "Backward paragraph."
1554 (let ((val (vip-p-val arg
))
1555 (com (vip-getCom arg
)))
1556 (if com
(move-marker vip-com-point
(point)))
1557 (backward-paragraph val
)
1558 (if com
(vip-execute-com 'vip-backward-paragraph nil com
))))
1563 (defun vip-scroll (arg)
1564 "Scroll to next screen."
1569 (setq arg
(1- arg
)))
1572 (setq arg
(1+ arg
)))))
1574 (defun vip-scroll-back (arg)
1575 "Scroll to previous screen."
1577 (vip-scroll (- arg
)))
1579 (defun vip-scroll-down (arg)
1580 "Scroll up half screen."
1582 (if (null arg
) (scroll-down (/ (window-height) 2))
1585 (defun vip-scroll-down-one (arg)
1586 "Scroll up one line."
1590 (defun vip-scroll-up (arg)
1591 "Scroll down half screen."
1593 (if (null arg
) (scroll-up (/ (window-height) 2))
1596 (defun vip-scroll-up-one (arg)
1597 "Scroll down one line."
1604 (defun vip-buffer-in-two-windows ()
1605 "Show current buffer in two windows."
1607 (delete-other-windows)
1608 (split-window-below))
1613 (defun vip-search-forward (arg)
1614 "Search a string forward. ARG is used to find the ARG's occurrence
1615 of the string. Default is vanilla search. Search mode can be toggled by
1616 giving null search string."
1618 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1619 (setq vip-s-forward t
1620 vip-s-string
(vip-read-string (if vip-re-search
"RE-/" "/")))
1621 (if (string= vip-s-string
"")
1623 (setq vip-re-search
(not vip-re-search
))
1624 (message "Search mode changed to %s search."
1625 (if vip-re-search
"regular expression"
1627 (vip-search vip-s-string t val
)
1630 (move-marker vip-com-point
(mark))
1631 (vip-execute-com 'vip-search-next val com
))))))
1633 (defun vip-search-backward (arg)
1634 "Search a string backward. ARG is used to find the ARG's occurrence
1635 of the string. Default is vanilla search. Search mode can be toggled by
1636 giving null search string."
1638 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1639 (setq vip-s-forward nil
1640 vip-s-string
(vip-read-string (if vip-re-search
"RE-?" "?")))
1641 (if (string= vip-s-string
"")
1643 (setq vip-re-search
(not vip-re-search
))
1644 (message "Search mode changed to %s search."
1645 (if vip-re-search
"regular expression"
1647 (vip-search vip-s-string nil val
)
1650 (move-marker vip-com-point
(mark))
1651 (vip-execute-com 'vip-search-next val com
))))))
1653 (defun vip-search (string forward arg
&optional no-offset init-point
)
1654 "(STRING FORWARD COUNT &optional NO-OFFSET) Search COUNT's occurrence of
1655 STRING. Search will be forward if FORWARD, otherwise backward."
1656 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
))
1657 (null-arg (null (vip-P-val arg
))) (offset (not no-offset
))
1658 (case-fold-search vip-case-fold-search
)
1659 (start-point (or init-point
(point))))
1661 (condition-case conditions
1663 (if (and offset
(not (eobp))) (forward-char))
1666 (re-search-forward string nil nil val
)
1667 (re-search-backward string
))
1668 (search-forward string nil nil val
)
1669 (search-backward string
))
1670 (push-mark start-point
))
1672 (if (and null-arg vip-search-wrap-around
)
1674 (goto-char (point-min))
1675 (vip-search string forward
(cons 1 com
) t start-point
))
1676 (goto-char start-point
)
1677 (signal 'search-failed
(cdr conditions
)))))
1678 (condition-case conditions
1681 (re-search-backward string nil nil val
)
1682 (search-backward string nil nil val
))
1683 (push-mark start-point
))
1685 (if (and null-arg vip-search-wrap-around
)
1687 (goto-char (point-max))
1688 (vip-search string forward
(cons 1 com
) t start-point
))
1689 (goto-char start-point
)
1690 (signal 'search-failed
(cdr conditions
))))))))
1692 (defun vip-search-next (arg)
1693 "Repeat previous search."
1695 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1696 (if (null vip-s-string
) (error "No previous search string"))
1697 (vip-search vip-s-string vip-s-forward arg
)
1698 (if com
(vip-execute-com 'vip-search-next val com
))))
1700 (defun vip-search-Next (arg)
1701 "Repeat previous search in the reverse direction."
1703 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1704 (if (null vip-s-string
) (error "No previous search string"))
1705 (vip-search vip-s-string
(not vip-s-forward
) arg
)
1706 (if com
(vip-execute-com 'vip-search-Next val com
))))
1709 ;; visiting and killing files, buffers
1711 (defun vip-switch-to-buffer ()
1712 "Switch to buffer in the current window."
1717 (format "switch to buffer \(%s\): "
1718 (buffer-name (other-buffer (current-buffer))))))
1719 (switch-to-buffer buffer
)
1720 (vip-change-mode-to-vi)))
1722 (defun vip-switch-to-buffer-other-window ()
1723 "Switch to buffer in another window."
1728 (format "Switch to buffer \(%s\): "
1729 (buffer-name (other-buffer (current-buffer))))))
1730 (switch-to-buffer-other-window buffer
)
1731 (vip-change-mode-to-vi)))
1733 (defun vip-kill-buffer ()
1736 (let (buffer buffer-name
)
1739 (format "Kill buffer \(%s\): "
1740 (buffer-name (current-buffer)))))
1742 (if (null buffer-name
)
1744 (get-buffer buffer-name
)))
1745 (if (null buffer
) (error "Buffer %s nonexistent" buffer-name
))
1746 (if (or (not (buffer-modified-p buffer
))
1747 (y-or-n-p "Buffer is modified, are you sure? "))
1748 (kill-buffer buffer
)
1749 (error "Buffer not killed"))))
1751 (defun vip-find-file ()
1752 "Visit file in the current window."
1755 (setq file
(read-file-name "visit file: "))
1756 (switch-to-buffer (find-file-noselect file
))
1757 (vip-change-mode-to-vi)))
1759 (defun vip-find-file-other-window ()
1760 "Visit file in another window."
1763 (setq file
(read-file-name "Visit file: "))
1764 (switch-to-buffer-other-window (find-file-noselect file
))
1765 (vip-change-mode-to-vi)))
1767 (defun vip-info-on-file ()
1768 "Give information of the file associated to the current buffer."
1770 (message "\"%s\" line %d of %d"
1771 (if (buffer-file-name) (buffer-file-name) "")
1772 (1+ (count-lines (point-min) (point)))
1773 (1+ (count-lines (point-min) (point-max)))))
1778 (defun vip-yank (text)
1779 "yank TEXT silently."
1781 (vip-push-mark-silent (point))
1783 (exchange-point-and-mark))
1784 (skip-chars-forward " \t"))
1786 (defun vip-put-back (arg)
1787 "Put back after point/below line."
1789 (let ((val (vip-p-val arg
))
1790 (text (if vip-use-register
1791 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1792 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1793 (get-register vip-use-register
))
1796 (if vip-use-register
1797 (let ((reg vip-use-register
))
1798 (setq vip-use-register nil
)
1799 (error "Nothing in register %c" reg
))
1801 (setq vip-use-register nil
)
1802 (if (vip-end-with-a-newline-p text
)
1804 (with-no-warnings (next-line 1))
1805 (beginning-of-line))
1806 (if (and (not (eolp)) (not (eobp))) (forward-char)))
1807 (setq vip-d-com
(list 'vip-put-back val nil vip-use-register
))
1808 (vip-loop val
(vip-yank text
))))
1810 (defun vip-Put-back (arg)
1811 "Put back at point/above line."
1813 (let ((val (vip-p-val arg
))
1814 (text (if vip-use-register
1815 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1816 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1817 (get-register vip-use-register
))
1820 (if vip-use-register
1821 (let ((reg vip-use-register
))
1822 (setq vip-use-register nil
)
1823 (error "Nothing in register %c" reg
))
1825 (setq vip-use-register nil
)
1826 (if (vip-end-with-a-newline-p text
) (beginning-of-line))
1827 (setq vip-d-com
(list 'vip-Put-back val nil vip-use-register
))
1828 (vip-loop val
(vip-yank text
))))
1830 (defun vip-delete-char (arg)
1833 (let ((val (vip-p-val arg
)))
1834 (setq vip-d-com
(list 'vip-delete-char val nil
))
1835 (if vip-use-register
1837 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1838 (vip-append-to-register
1839 (+ vip-use-register
32) (point) (- (point) val
))
1840 (copy-to-register vip-use-register
(point) (- (point) val
) nil
))
1841 (setq vip-use-register nil
)))
1842 (delete-char val t
)))
1844 (defun vip-delete-backward-char (arg)
1845 "Delete previous character."
1847 (let ((val (vip-p-val arg
)))
1848 (setq vip-d-com
(list 'vip-delete-backward-char val nil
))
1849 (if vip-use-register
1851 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1852 (vip-append-to-register
1853 (+ vip-use-register
32) (point) (+ (point) val
))
1854 (copy-to-register vip-use-register
(point) (+ (point) val
) nil
))
1855 (setq vip-use-register nil
)))
1856 (delete-backward-char val t
)))
1861 (defun vip-join-lines (arg)
1862 "Join this line to next, if ARG is nil. Otherwise, join ARG lines"
1864 (let ((val (vip-P-val arg
)))
1865 (setq vip-d-com
(list 'vip-join-lines val nil
))
1866 (vip-loop (if (null val
) 1 (1- val
))
1872 (delete-region (point) (1- (point)))
1873 (fixup-whitespace)))))))
1876 ;; making small changes
1878 (defvar vip-c-string
)
1880 (defun vip-change (beg end
)
1882 (vip-read-string (format "%s => " (buffer-substring beg end
))))
1883 (vip-change-subr beg end
))
1885 (defun vip-change-subr (beg end
)
1886 (if vip-use-register
1888 (copy-to-register vip-use-register beg end nil
)
1889 (setq vip-use-register nil
)))
1890 (kill-region beg end
)
1891 (setq this-command
'vip-change
)
1892 (insert vip-c-string
))
1897 (defun vip-query-replace ()
1898 "Query replace. If you supply null string as the string to be replaced,
1899 the query replace mode will toggle between string replace and regexp replace."
1902 (setq str
(vip-read-string
1903 (if vip-re-query-replace
"Query replace regexp: "
1904 "Query replace: ")))
1905 (if (string= str
"")
1907 (setq vip-re-query-replace
(not vip-re-query-replace
))
1908 (message "Query replace mode changed to %s."
1909 (if vip-re-query-replace
"regexp replace"
1911 (if vip-re-query-replace
1912 (query-replace-regexp
1914 (vip-read-string (format "Query replace regexp \"%s\" with: " str
)))
1917 (vip-read-string (format "Query replace \"%s\" with: " str
)))))))
1922 (defun vip-mark-beginning-of-buffer ()
1925 (goto-char (point-min))
1926 (exchange-point-and-mark)
1927 (message "mark set at the beginning of buffer"))
1929 (defun vip-mark-end-of-buffer ()
1932 (goto-char (point-max))
1933 (exchange-point-and-mark)
1934 (message "mark set at the end of buffer"))
1936 (defun vip-mark-point (char)
1938 (cond ((and (<= ?a char
) (<= char ?z
))
1939 (point-to-register (- char
(- ?a ?\C-a
)) nil
))
1940 ((= char ?
<) (vip-mark-beginning-of-buffer))
1941 ((= char ?
>) (vip-mark-end-of-buffer))
1942 ((= char ?.
) (push-mark))
1943 ((= char ?
,) (set-mark-command 1))
1944 ((= char ?D
) (mark-defun))
1947 (defun vip-goto-mark (arg)
1950 (let ((char (read-char)) (com (vip-getcom arg
)))
1951 (vip-goto-mark-subr char com nil
)))
1953 (defun vip-goto-mark-and-skip-white (arg)
1954 "Go to mark and skip to first non-white on line."
1956 (let ((char (read-char)) (com (vip-getCom arg
)))
1957 (vip-goto-mark-subr char com t
)))
1959 (defun vip-goto-mark-subr (char com skip-white
)
1960 (cond ((and (<= ?a char
) (<= char ?z
))
1961 (let ((buff (current-buffer)))
1962 (if com
(move-marker vip-com-point
(point)))
1963 (goto-char (register-to-point (- char
(- ?a ?\C-a
))))
1964 (if skip-white
(back-to-indentation))
1965 (vip-change-mode-to-vi)
1967 (if (equal buff
(current-buffer))
1968 (vip-execute-com (if skip-white
1969 'vip-goto-mark-and-skip-white
1972 (switch-to-buffer buff
)
1973 (goto-char vip-com-point
)
1974 (vip-change-mode-to-vi)
1976 ((and (not skip-white
) (= char ?
`))
1977 (if com
(move-marker vip-com-point
(point)))
1978 (exchange-point-and-mark)
1979 (if com
(vip-execute-com 'vip-goto-mark nil com
)))
1980 ((and skip-white
(= char ?
'))
1981 (if com
(move-marker vip-com-point
(point)))
1982 (exchange-point-and-mark)
1983 (back-to-indentation)
1984 (if com
(vip-execute-com 'vip-goto-mark-and-skip-white nil com
)))
1987 (defun vip-exchange-point-and-mark ()
1989 (exchange-point-and-mark)
1990 (back-to-indentation))
1992 (defun vip-keyboard-quit ()
1993 "Abort partially formed or running command."
1995 (setq vip-use-register nil
)
1998 (defun vip-ctl-c-equivalent (arg)
1999 "Emulate C-c in Emacs mode."
2001 (vip-ctl-key-equivalent "\C-c" arg
))
2003 (defun vip-ctl-x-equivalent (arg)
2004 "Emulate C-x in Emacs mode."
2006 (vip-ctl-key-equivalent "\C-x" arg
))
2008 (defun vip-ctl-key-equivalent (key arg
)
2009 (let ((char (read-char)))
2010 (if (and (<= ?A char
) (<= char ?Z
))
2011 (setq char
(- char
(- ?A ?\C-a
))))
2012 (vip-escape-to-emacs arg
(list (aref key
0) char
))))
2014 ;; commands in insertion mode
2016 (defun vip-delete-backward-word (arg)
2017 "Delete previous word."
2022 (delete-region (point) (mark))))
2025 ;; implement ex commands
2027 (defvar ex-token-type nil
2028 "type of token. if non-nil, gives type of address. if nil, it
2031 (defvar ex-token nil
2034 (defvar ex-addresses nil
2035 "list of ex addresses")
2040 (defvar ex-buffer nil
2041 "name of ex buffer")
2043 (defvar ex-count nil
2044 "value of ex count")
2046 (defvar ex-g-flag nil
2047 "flag for global command")
2049 (defvar ex-g-variant nil
2050 "if t global command is executed on lines not matching ex-g-pat")
2052 (defvar ex-reg-exp nil
2053 "save reg-exp used in substitute")
2056 "replace pattern for substitute")
2058 (defvar ex-g-pat nil
2059 "pattern for global command")
2061 (defvar ex-map
(make-sparse-keymap)
2062 "save commands for mapped keys")
2067 (defvar ex-file nil
)
2069 (defvar ex-variant nil
)
2071 (defvar ex-offset nil
)
2073 (defvar ex-append nil
)
2079 (defun vip-looking-back (str)
2080 "returns t if looking back reg-exp STR before point."
2081 (and (save-excursion (re-search-backward str nil t
))
2082 (= (point) (match-end 0))))
2084 (defun vip-check-sub (str)
2085 "check if ex-token is an initial segment of STR"
2086 (let ((length (length ex-token
)))
2087 (if (and (<= length
(length str
))
2088 (string= ex-token
(substring str
0 length
)))
2090 (setq ex-token-type
"non-command"))))
2092 (defun vip-get-ex-com-subr ()
2093 "get a complete ex command"
2095 (re-search-forward "[a-z][a-z]*")
2096 (setq ex-token-type
"command")
2097 (setq ex-token
(buffer-substring (point) (mark)))
2098 (exchange-point-and-mark)
2099 (cond ((looking-at "a")
2100 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
2101 ((looking-at "ar") (vip-check-sub "args"))
2102 (t (vip-check-sub "append"))))
2103 ((looking-at "[bh]") (setq ex-token-type
"non-command"))
2105 (if (looking-at "co") (vip-check-sub "copy")
2106 (vip-check-sub "change")))
2107 ((looking-at "d") (vip-check-sub "delete"))
2109 (if (looking-at "ex") (vip-check-sub "ex")
2110 (vip-check-sub "edit")))
2111 ((looking-at "f") (vip-check-sub "file"))
2112 ((looking-at "g") (vip-check-sub "global"))
2113 ((looking-at "i") (vip-check-sub "insert"))
2114 ((looking-at "j") (vip-check-sub "join"))
2115 ((looking-at "l") (vip-check-sub "list"))
2117 (cond ((looking-at "map") (vip-check-sub "map"))
2118 ((looking-at "mar") (vip-check-sub "mark"))
2119 (t (vip-check-sub "move"))))
2121 (if (looking-at "nu") (vip-check-sub "number")
2122 (vip-check-sub "next")))
2123 ((looking-at "o") (vip-check-sub "open"))
2125 (cond ((looking-at "pre") (vip-check-sub "preserve"))
2126 ((looking-at "pu") (vip-check-sub "put"))
2127 (t (vip-check-sub "print"))))
2128 ((looking-at "q") (vip-check-sub "quit"))
2130 (cond ((looking-at "rec") (vip-check-sub "recover"))
2131 ((looking-at "rew") (vip-check-sub "rewind"))
2132 (t (vip-check-sub "read"))))
2134 (cond ((looking-at "se") (vip-check-sub "set"))
2135 ((looking-at "sh") (vip-check-sub "shell"))
2136 ((looking-at "so") (vip-check-sub "source"))
2137 ((looking-at "st") (vip-check-sub "stop"))
2138 (t (vip-check-sub "substitute"))))
2140 (if (looking-at "ta") (vip-check-sub "tag")
2141 (vip-check-sub "t")))
2143 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
2144 ((looking-at "unm") (vip-check-sub "unmap"))
2145 (t (vip-check-sub "undo"))))
2147 (cond ((looking-at "ve") (vip-check-sub "version"))
2148 ((looking-at "vi") (vip-check-sub "visual"))
2149 (t (vip-check-sub "v"))))
2151 (if (looking-at "wq") (vip-check-sub "wq")
2152 (vip-check-sub "write")))
2153 ((looking-at "x") (vip-check-sub "xit"))
2154 ((looking-at "y") (vip-check-sub "yank"))
2155 ((looking-at "z") (vip-check-sub "z")))
2156 (exchange-point-and-mark))
2158 (defun vip-get-ex-token ()
2159 "get an ex-token which is either an address or a command.
2160 a token has type \(command, address, end-mark\) and value."
2161 (with-current-buffer " *ex-working-space*"
2162 (skip-chars-forward " \t")
2163 (cond ((looking-at "[k#]")
2164 (setq ex-token-type
"command")
2165 (setq ex-token
(char-to-string (following-char)))
2167 ((looking-at "[a-z]") (vip-get-ex-com-subr))
2170 (setq ex-token-type
"dot"))
2171 ((looking-at "[0-9]")
2173 (re-search-forward "[0-9]*")
2175 (cond ((string= ex-token-type
"plus") "add-number")
2176 ((string= ex-token-type
"minus") "sub-number")
2178 (setq ex-token
(string-to-number (buffer-substring (point) (mark)))))
2181 (setq ex-token-type
"end"))
2184 (setq ex-token-type
"whole"))
2186 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
2190 (setq ex-token-type
"plus"))
2191 ((looking-at "+[0-9]")
2193 (setq ex-token-type
"plus"))
2195 (error "Badly formed address"))))
2197 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
2201 (setq ex-token-type
"minus"))
2202 ((looking-at "-[0-9]")
2204 (setq ex-token-type
"minus"))
2206 (error "Badly formed address"))))
2211 (while (and (not (eolp)) cont
)
2212 ;;(re-search-forward "[^/]*/")
2213 (re-search-forward "[^/]*\\(/\\|\n\\)")
2214 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2217 (setq ex-token
(buffer-substring (point) (mark)))
2218 (if (looking-at "/") (forward-char 1))
2219 (setq ex-token-type
"search-forward"))
2224 (while (and (not (eolp)) cont
)
2225 ;;(re-search-forward "[^\\?]*\\?")
2226 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
2227 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
2230 (if (not (looking-at "\n")) (forward-char 1))))
2231 (setq ex-token-type
"search-backward")
2232 (setq ex-token
(buffer-substring (1- (point)) (mark))))
2235 (setq ex-token-type
"comma"))
2238 (setq ex-token-type
"semi-colon"))
2239 ((looking-at "[!=><&~]")
2240 (setq ex-token-type
"command")
2241 (setq ex-token
(char-to-string (following-char)))
2244 (setq ex-token-type
"goto-mark")
2246 (cond ((looking-at "'") (setq ex-token nil
))
2247 ((looking-at "[a-z]") (setq ex-token
(following-char)))
2248 (t (error "Marks are ' and a-z")))
2251 (setq ex-token-type
"end-mark")
2252 (setq ex-token
"goto"))
2254 (error "invalid token")))))
2256 (defun vip-ex (&optional string
)
2257 "ex commands within VIP."
2262 (let ((com-str (or string
(vip-read-string ":")))
2263 (address nil
) (cont t
) (dot (point)))
2264 (with-current-buffer (get-buffer-create " *ex-working-space*")
2265 (delete-region (point-min) (point-max))
2266 (insert com-str
"\n")
2267 (goto-char (point-min)))
2268 (setq ex-token-type
"")
2269 (setq ex-addresses nil
)
2272 (cond ((or (string= ex-token-type
"command")
2273 (string= ex-token-type
"end-mark"))
2274 (if address
(setq ex-addresses
(cons address ex-addresses
)))
2275 (cond ((string= ex-token
"global")
2278 ((string= ex-token
"v")
2282 (vip-execute-ex-command)
2283 (with-current-buffer " *ex-working-space*"
2284 (skip-chars-forward " \t")
2285 (cond ((looking-at "|")
2289 (t (error "Extra character at end of a command")))))))
2290 ((string= ex-token-type
"non-command")
2291 (error "%s: Not an editor command" ex-token
))
2292 ((string= ex-token-type
"whole")
2294 (cons (point-max) (cons (point-min) ex-addresses
))))
2295 ((string= ex-token-type
"comma")
2297 (cons (if (null address
) (point) address
) ex-addresses
)))
2298 ((string= ex-token-type
"semi-colon")
2299 (if address
(setq dot address
))
2301 (cons (if (null address
) (point) address
) ex-addresses
)))
2302 (t (let ((ans (vip-get-ex-address-subr address dot
)))
2303 (if ans
(setq address ans
))))))))
2305 (defun vip-get-ex-pat ()
2306 "get a regular expression and set ex-variant if found"
2307 (with-current-buffer " *ex-working-space*"
2308 (skip-chars-forward " \t")
2309 (if (looking-at "!")
2311 (setq ex-g-variant
(not ex-g-variant
)
2312 ex-g-flag
(not ex-g-flag
))
2314 (skip-chars-forward " \t")))
2315 (if (looking-at "/")
2320 (while (and (not (eolp)) cont
)
2321 (re-search-forward "[^/]*\\(/\\|\n\\)")
2322 ;;(re-search-forward "[^/]*/")
2323 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2326 (if (= (mark) (point)) ""
2327 (buffer-substring (1- (point)) (mark))))
2329 (setq ex-token nil
))))
2331 (defun vip-get-ex-command ()
2333 (with-current-buffer " *ex-working-space*"
2334 (if (looking-at "/") (forward-char 1))
2335 (skip-chars-forward " \t")
2336 (cond ((looking-at "[a-z]")
2337 (vip-get-ex-com-subr)
2338 (if (string= ex-token-type
"non-command")
2339 (error "%s: not an editor command" ex-token
)))
2340 ((looking-at "[!=><&~]")
2341 (setq ex-token
(char-to-string (following-char)))
2343 (t (error "Could not find an ex command")))))
2345 (defun vip-get-ex-opt-gc ()
2346 "get an ex option g or c"
2347 (with-current-buffer " *ex-working-space*"
2348 (if (looking-at "/") (forward-char 1))
2349 (skip-chars-forward " \t")
2350 (cond ((looking-at "g")
2360 (defun vip-default-ex-addresses (&optional whole-flag
)
2361 "compute default addresses. whole-flag means whole buffer."
2362 (cond ((null ex-addresses
)
2365 (cons (point-max) (cons (point-min) nil
))
2366 (cons (point) (cons (point) nil
)))))
2367 ((null (cdr ex-addresses
))
2369 (cons (car ex-addresses
) ex-addresses
)))))
2371 (defun vip-get-ex-address ()
2372 "get an ex-address as a marker and set ex-flag if a flag is found"
2373 (let ((address (point-marker)) (cont t
))
2378 (cond ((string= ex-token-type
"command")
2379 (if (or (string= ex-token
"print") (string= ex-token
"list")
2380 (string= ex-token
"#"))
2384 (error "address expected")))
2385 ((string= ex-token-type
"end-mark")
2387 ((string= ex-token-type
"whole")
2388 (error "a trailing address is expected"))
2389 ((string= ex-token-type
"comma")
2390 (error "Extra characters after an address"))
2391 (t (let ((ans (vip-get-ex-address-subr address
(point-marker))))
2392 (if ans
(setq address ans
))))))
2395 (defun vip-get-ex-address-subr (old-address dot
)
2396 "returns an address as a point"
2397 (let ((address nil
))
2398 (if (null old-address
) (setq old-address dot
))
2399 (cond ((string= ex-token-type
"dot")
2401 ((string= ex-token-type
"add-number")
2403 (goto-char old-address
)
2404 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
2405 (setq address
(point-marker))))
2406 ((string= ex-token-type
"sub-number")
2408 (goto-char old-address
)
2409 (forward-line (- ex-token
))
2410 (setq address
(point-marker))))
2411 ((string= ex-token-type
"abs-number")
2413 (goto-char (point-min))
2414 (if (= ex-token
0) (setq address
0)
2415 (forward-line (1- ex-token
))
2416 (setq address
(point-marker)))))
2417 ((string= ex-token-type
"end")
2418 (setq address
(point-max-marker)))
2419 ((string= ex-token-type
"plus") t
);; do nothing
2420 ((string= ex-token-type
"minus") t
);; do nothing
2421 ((string= ex-token-type
"search-forward")
2423 (ex-search-address t
)
2424 (setq address
(point-marker))))
2425 ((string= ex-token-type
"search-backward")
2427 (ex-search-address nil
)
2428 (setq address
(point-marker))))
2429 ((string= ex-token-type
"goto-mark")
2432 (exchange-point-and-mark)
2433 (goto-char (register-to-point (- ex-token
(- ?a ?\C-a
)))))
2434 (setq address
(point-marker)))))
2437 (defun ex-search-address (forward)
2438 "search pattern and set address"
2439 (if (string= ex-token
"")
2440 (if (null vip-s-string
) (error "No previous search string")
2441 (setq ex-token vip-s-string
))
2442 (setq vip-s-string ex-token
))
2446 (re-search-forward ex-token
))
2448 (re-search-backward ex-token
)))
2450 (defun vip-get-ex-buffer ()
2451 "get a buffer name and set ex-count and ex-flag if found"
2452 (setq ex-buffer nil
)
2455 (with-current-buffer " *ex-working-space*"
2456 (skip-chars-forward " \t")
2457 (if (looking-at "[a-zA-Z]")
2459 (setq ex-buffer
(following-char))
2461 (skip-chars-forward " \t")))
2462 (if (looking-at "[0-9]")
2465 (re-search-forward "[0-9][0-9]*")
2466 (setq ex-count
(string-to-number (buffer-substring (point) (mark))))
2467 (skip-chars-forward " \t")))
2468 (if (looking-at "[pl#]")
2472 (if (not (looking-at "[\n|]"))
2473 (error "Invalid extra characters"))))
2475 (defun vip-get-ex-count ()
2476 (setq ex-variant nil
2479 (with-current-buffer " *ex-working-space*"
2480 (skip-chars-forward " \t")
2481 (if (looking-at "!")
2485 (skip-chars-forward " \t")
2486 (if (looking-at "[0-9]")
2489 (re-search-forward "[0-9][0-9]*")
2490 (setq ex-count
(string-to-number (buffer-substring (point) (mark))))
2491 (skip-chars-forward " \t")))
2492 (if (looking-at "[pl#]")
2496 (if (not (looking-at "[\n|]"))
2497 (error "Invalid extra characters"))))
2499 (defun vip-get-ex-file ()
2500 "get a file name and set ex-variant, ex-append and ex-offset if found"
2505 (with-current-buffer " *ex-working-space*"
2506 (skip-chars-forward " \t")
2507 (if (looking-at "!")
2511 (skip-chars-forward " \t")))
2512 (if (looking-at ">>")
2517 (skip-chars-forward " \t")))
2518 (if (looking-at "+")
2522 (re-search-forward "[ \t\n]")
2524 (setq ex-offset
(buffer-substring (point) (mark)))
2526 (skip-chars-forward " \t")))
2528 (re-search-forward "[ \t\n]")
2530 (setq ex-file
(buffer-substring (point) (mark)))))
2532 (defun vip-execute-ex-command ()
2533 "execute ex command using the value of addresses."
2534 (cond ((string= ex-token
"goto") (ex-goto))
2535 ((string= ex-token
"copy") (ex-copy nil
))
2536 ((string= ex-token
"delete") (ex-delete))
2537 ((string= ex-token
"edit") (ex-edit))
2538 ((string= ex-token
"file") (vip-info-on-file))
2539 ;((string= ex-token "global") (ex-global nil))
2540 ((string= ex-token
"join") (ex-line "join"))
2541 ((string= ex-token
"k") (ex-mark))
2542 ((string= ex-token
"mark") (ex-mark))
2543 ((string= ex-token
"map") (ex-map))
2544 ((string= ex-token
"move") (ex-copy t
))
2545 ((string= ex-token
"put") (ex-put))
2546 ((string= ex-token
"quit") (ex-quit))
2547 ((string= ex-token
"read") (ex-read))
2548 ((string= ex-token
"set") (ex-set))
2549 ((string= ex-token
"shell") (ex-shell))
2550 ((string= ex-token
"substitute") (ex-substitute))
2551 ((string= ex-token
"stop") (suspend-emacs))
2552 ((string= ex-token
"t") (ex-copy nil
))
2553 ((string= ex-token
"tag") (ex-tag))
2554 ((string= ex-token
"undo") (vip-undo))
2555 ((string= ex-token
"unmap") (ex-unmap))
2556 ;((string= ex-token "v") (ex-global t))
2557 ((string= ex-token
"version") (vip-version))
2558 ((string= ex-token
"visual") (ex-edit))
2559 ((string= ex-token
"write") (ex-write nil
))
2560 ((string= ex-token
"wq") (ex-write t
))
2561 ((string= ex-token
"yank") (ex-yank))
2562 ((string= ex-token
"!") (ex-command))
2563 ((string= ex-token
"=") (ex-line-no))
2564 ((string= ex-token
">") (ex-line "right"))
2565 ((string= ex-token
"<") (ex-line "left"))
2566 ((string= ex-token
"&") (ex-substitute t
))
2567 ((string= ex-token
"~") (ex-substitute t t
))
2568 ((or (string= ex-token
"append")
2569 (string= ex-token
"args")
2570 (string= ex-token
"change")
2571 (string= ex-token
"insert")
2572 (string= ex-token
"open")
2574 (error "%s: no such command from VIP" ex-token
))
2575 ((or (string= ex-token
"abbreviate")
2576 (string= ex-token
"list")
2577 (string= ex-token
"next")
2578 (string= ex-token
"print")
2579 (string= ex-token
"preserve")
2580 (string= ex-token
"recover")
2581 (string= ex-token
"rewind")
2582 (string= ex-token
"source")
2583 (string= ex-token
"unabbreviate")
2584 (string= ex-token
"xit")
2585 (string= ex-token
"z")
2587 (error "%s: not implemented in VIP" ex-token
))
2588 (t (error "%s: Not an editor command" ex-token
))))
2592 (if (null ex-addresses
)
2593 (setq ex-addresses
(cons (point) nil
)))
2595 (goto-char (car ex-addresses
))
2596 (beginning-of-line))
2598 (defun ex-copy (del-flag)
2599 "ex copy and move command. DEL-FLAG means delete."
2600 (vip-default-ex-addresses)
2601 (let ((address (vip-get-ex-address))
2602 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2606 (vip-enlarge-region (mark) (point))
2607 (if del-flag
(kill-region (point) (mark))
2608 (copy-region-as-kill (point) (mark)))
2611 (with-output-to-temp-buffer "*copy text*"
2613 (if (or del-flag ex-g-flag ex-g-variant
)
2615 (buffer-substring (point) (mark)))))
2618 (vip-read-string "[Hit return to continue] ")
2619 (save-excursion (kill-buffer "*copy text*")))
2621 (save-excursion (kill-buffer "*copy text*"))
2622 (signal 'quit nil
))))))
2624 (goto-char (point-min))
2627 (insert (current-kill 0))))
2631 (vip-default-ex-addresses)
2633 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2634 (if (> beg end
) (error "First address exceeds second"))
2636 (vip-enlarge-region beg end
)
2637 (exchange-point-and-mark)
2641 (forward-line (1- ex-count
)))
2643 (vip-enlarge-region (point) (mark))
2645 ;; show text to be deleted and ask for confirmation
2647 (with-output-to-temp-buffer " *delete text*"
2648 (princ (buffer-substring (point) (mark))))
2649 (condition-case conditions
2650 (vip-read-string "[Hit return to continue] ")
2652 (save-excursion (kill-buffer " *delete text*"))
2654 (save-excursion (kill-buffer " *delete text*")))
2656 (if (and (<= ?A ex-buffer
) (<= ex-buffer ?Z
))
2657 (vip-append-to-register
2658 (+ ex-buffer
32) (point) (mark))
2659 (copy-to-register ex-buffer
(point) (mark) nil
)))
2660 (delete-region (point) (mark))))))
2665 (if (and (not ex-variant
) (buffer-modified-p) buffer-file-name
)
2666 (error "No write since last change \(:e! overrides\)"))
2667 (vip-change-mode-to-emacs)
2669 (find-file-noselect (concat default-directory ex-file
)))
2670 (vip-change-mode-to-vi)
2671 (goto-char (point-min))
2674 (with-current-buffer " *ex-working-space*"
2675 (delete-region (point-min) (point-max))
2676 (insert ex-offset
"\n")
2677 (goto-char (point-min)))
2678 (goto-char (vip-get-ex-address))
2679 (beginning-of-line))))
2681 (defun ex-global (variant)
2683 (if (or ex-g-flag ex-g-variant
)
2684 (error "Global within global not allowed")
2692 (error "Missing regular expression for global command"))
2693 (if (string= ex-token
"")
2694 (if (null vip-s-string
) (error "No previous search string")
2695 (setq ex-g-pat vip-s-string
))
2696 (setq ex-g-pat ex-token
2697 vip-s-string ex-token
))
2698 (if (null ex-addresses
)
2699 (setq ex-addresses
(list (point-max) (point-min))))
2700 (let ((marks nil
) (mark-count 0)
2701 com-str
(end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2702 (if (> beg end
) (error "First address exceeds second"))
2704 (vip-enlarge-region beg end
)
2705 (exchange-point-and-mark)
2706 (let ((cont t
) (limit (point-marker)))
2707 (exchange-point-and-mark)
2708 ;; skip the last line if empty
2710 (if (and (eobp) (not (bobp))) (backward-char 1))
2711 (while (and cont
(not (bobp)) (>= (point) limit
))
2715 (let ((found (re-search-backward ex-g-pat
(mark) t
)))
2716 (if (or (and ex-g-flag found
)
2717 (and ex-g-variant
(not found
)))
2720 (setq mark-count
(1+ mark-count
))
2721 (setq marks
(cons (point-marker) marks
)))))
2723 (if (bobp) (setq cont nil
)
2726 (with-current-buffer " *ex-working-space*"
2727 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
2729 (goto-char (car marks
))
2730 ;; report progress of execution on a slow machine.
2731 ;;(message "Executing global command...")
2732 ;;(if (zerop (% mark-count 10))
2733 ;; (message "Executing global command...%d" mark-count))
2735 (setq mark-count
(1- mark-count
))
2736 (setq marks
(cdr marks
)))))
2737 ;;(message "Executing global command...done")))
2739 (defun ex-line (com)
2740 "ex line commands. COM is join, shift-right or shift-left."
2741 (vip-default-ex-addresses)
2743 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
2744 (if (> beg end
) (error "First address exceeds second"))
2746 (vip-enlarge-region beg end
)
2747 (exchange-point-and-mark)
2751 (forward-line ex-count
)))
2753 ;; show text to be joined and ask for confirmation
2755 (with-output-to-temp-buffer " *text*"
2756 (princ (buffer-substring (point) (mark))))
2757 (condition-case conditions
2759 (vip-read-string "[Hit return to continue] ")
2760 (ex-line-subr com
(point) (mark)))
2763 (save-excursion (kill-buffer " *text*")))
2764 (ex-line-subr com
(point) (mark)))
2765 (setq point
(point)))
2766 (goto-char (1- point
))
2767 (beginning-of-line)))
2769 (defun ex-line-subr (com beg end
)
2770 (cond ((string= com
"join")
2771 (goto-char (min beg end
))
2772 (while (and (not (eobp)) (< (point) (max beg end
)))
2774 (if (and (<= (point) (max beg end
)) (not (eobp)))
2777 (delete-region (point) (1- (point)))
2778 (if (not ex-variant
) (fixup-whitespace))))))
2779 ((or (string= com
"right") (string= com
"left"))
2781 (min beg end
) (max beg end
)
2782 (if (string= com
"right") vip-shift-width
(- vip-shift-width
)))
2783 (goto-char (max beg end
))
2790 (if (null ex-addresses
)
2792 (cons (point) nil
)))
2793 (with-current-buffer " *ex-working-space*"
2794 (skip-chars-forward " \t")
2795 (if (looking-at "[a-z]")
2797 (setq char
(following-char))
2799 (skip-chars-forward " \t")
2800 (if (not (looking-at "[\n|]"))
2801 (error "Extra characters at end of \"k\" command")))
2802 (if (looking-at "[\n|]")
2803 (error "\"k\" requires a following letter")
2804 (error "Mark must specify a letter"))))
2806 (goto-char (car ex-addresses
))
2807 (point-to-register (- char
(- ?a ?\C-a
)) nil
))))
2812 (with-current-buffer " *ex-working-space*"
2813 (skip-chars-forward " \t")
2814 (setq char
(char-to-string (following-char)))
2816 (skip-chars-forward " \t")
2817 (if (looking-at "[\n|]") (error "Missing rhs"))
2822 (setq string
(buffer-substring (mark) (point))))
2823 (if (not (lookup-key ex-map char
))
2824 (define-key ex-map char
2825 (or (lookup-key vip-mode-map char
) 'vip-nil
)))
2826 (define-key vip-mode-map char
2832 (list 'execute-kbd-macro string
'count
))))))))
2837 (with-current-buffer " *ex-working-space*"
2838 (skip-chars-forward " \t")
2839 (setq char
(char-to-string (following-char)))
2841 (skip-chars-forward " \t")
2842 (if (not (looking-at "[\n|]")) (error "Macro must be a character")))
2843 (if (not (lookup-key ex-map char
))
2844 (error "That macro wasn't mapped"))
2845 (define-key vip-mode-map char
(lookup-key ex-map char
))
2846 (define-key ex-map char nil
)))
2850 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
2852 (setq vip-use-register ex-buffer
)
2854 (if (= point
0) (vip-Put-back 1) (vip-put-back 1))))
2859 (with-current-buffer " *ex-working-space*"
2860 (skip-chars-forward " \t")
2861 (setq char
(following-char)))
2862 (if (= char ?
!) (kill-emacs t
) (save-buffers-kill-emacs))))
2866 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
2867 (variant nil
) command file
)
2869 (if (not (= point
0)) (with-no-warnings (next-line 1)))
2871 (with-current-buffer " *ex-working-space*"
2872 (skip-chars-forward " \t")
2873 (if (looking-at "!")
2877 (skip-chars-forward " \t")
2880 (setq command
(buffer-substring (mark) (point))))
2882 (re-search-forward "[ \t\n]")
2884 (setq file
(buffer-substring (point) (mark)))))
2886 (shell-command command t
)
2888 (insert-file file
)))))
2892 (read-variable "Variable: ")
2893 (eval (read-minibuffer "Value: ")))))
2897 (vip-change-mode-to-emacs)
2900 (defun ex-substitute (&optional repeat r-flag
)
2902 If REPEAT use previous reg-exp which is ex-reg-exp or
2904 (let (pat repl
(opt-g nil
) (opt-c nil
) (matched-pos nil
))
2905 (if repeat
(setq ex-token nil
) (vip-get-ex-pat))
2907 (setq pat
(if r-flag vip-s-string ex-reg-exp
)
2909 (setq pat
(if (string= ex-token
"") vip-s-string ex-token
))
2910 (setq vip-s-string pat
2918 (while (vip-get-ex-opt-gc)
2919 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
2923 (if ex-addresses
(goto-char (car ex-addresses
)))
2925 (forward-line (1- ex-count
))
2926 (setq ex-addresses
(cons (point) (cons (mark) nil
))))
2927 (if (null ex-addresses
)
2928 (setq ex-addresses
(cons (point) (cons (point) nil
)))
2929 (if (null (cdr ex-addresses
))
2930 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
2932 (let ((beg (car ex-addresses
)) (end (car (cdr ex-addresses
)))
2935 (vip-enlarge-region beg end
)
2936 (let ((limit (save-excursion
2937 (goto-char (max (point) (mark)))
2939 (goto-char (min (point) (mark)))
2940 (while (< (point) limit
)
2942 (setq eol-mark
(point-marker))
2946 (while (and (not (eolp))
2947 (re-search-forward pat eol-mark t
))
2948 (if (or (not opt-c
) (y-or-n-p "Replace? "))
2950 (setq matched-pos
(point))
2951 (replace-match repl
))))
2954 (if (and (re-search-forward pat eol-mark t
)
2955 (or (not opt-c
) (y-or-n-p "Replace? ")))
2957 (setq matched-pos
(point))
2958 (replace-match repl
)))
2961 (if matched-pos
(goto-char matched-pos
))
2963 (if opt-c
(message "done"))))
2968 (with-current-buffer " *ex-working-space*"
2969 (skip-chars-forward " \t")
2971 (skip-chars-forward "^ |\t\n")
2972 (setq tag
(buffer-substring (mark) (point))))
2973 (if (not (string= tag
"")) (setq ex-tag tag
))
2974 (vip-change-mode-to-emacs)
2975 (condition-case conditions
2977 (if (string= tag
"")
2979 (find-tag-other-window ex-tag
))
2980 (vip-change-mode-to-vi))
2982 (vip-change-mode-to-vi)
2983 (vip-message-conditions conditions
)))))
2985 (defun ex-write (q-flag)
2987 (vip-default-ex-addresses t
)
2989 (if (string= ex-file
"")
2991 (if (null buffer-file-name
)
2992 (error "No file associated with this buffer"))
2993 (setq ex-file buffer-file-name
))
2994 (setq ex-file
(expand-file-name ex-file
)))
2995 (if (and (not (string= ex-file
(buffer-file-name)))
2996 (file-exists-p ex-file
)
2998 (error "\"%s\" File exists - use w! to override" ex-file
))
2999 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3000 (if (> beg end
) (error "First address exceeds second"))
3002 (vip-enlarge-region beg end
)
3003 (write-region (point) (mark) ex-file ex-append t
)))
3004 (if (null buffer-file-name
) (setq buffer-file-name ex-file
))
3005 (if q-flag
(save-buffers-kill-emacs)))
3009 (vip-default-ex-addresses)
3011 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3012 (if (> beg end
) (error "First address exceeds second"))
3014 (vip-enlarge-region beg end
)
3015 (exchange-point-and-mark)
3016 (if (or ex-g-flag ex-g-variant
) (error "Can't yank within global"))
3020 (forward-line (1- ex-count
)))
3022 (vip-enlarge-region (point) (mark))
3023 (if ex-flag
(error "Extra characters at end of command"))
3025 (copy-to-register ex-buffer
(point) (mark) nil
))
3026 (copy-region-as-kill (point) (mark)))))
3028 (defun ex-command ()
3029 "execute shell command"
3031 (with-current-buffer " *ex-working-space*"
3032 (skip-chars-forward " \t")
3035 (setq command
(buffer-substring (mark) (point))))
3036 (if (null ex-addresses
)
3037 (shell-command command
)
3038 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3039 (if (null beg
) (setq beg end
))
3043 (vip-enlarge-region (point) (mark))
3044 (shell-command-on-region (point) (mark) command t
))
3047 (defun ex-line-no ()
3052 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
3054 (if (file-exists-p vip-startup-file
) (load vip-startup-file
))
3058 ;;; vip.el ends here