1 ;;; vip.el --- a VI Package for GNU Emacs
3 ;; Author: Masahiko Sato <ms@sail.stanford.edu>
5 ;; Last-Modified: 15 Sep 1987
9 ;; In Japan, the author's address is: masahiko@sato.riec.tohoku.junet
11 ;; Send suggestions and bug reports to one of the above addresses.
12 ;; When you report a bug, be sure to include the version number of VIP and
13 ;; Emacs you are using.
15 ;; Execute info command by typing "M-x info" to get information on VIP.
21 (defvar vip-emacs-local-map nil
22 "Local map used in emacs mode. \(buffer specific\)")
24 (defvar vip-insert-local-map nil
25 "Local map used in insert command mode. \(buffer specific\)")
27 (make-variable-buffer-local 'vip-emacs-local-map
)
28 (make-variable-buffer-local 'vip-insert-local-map
)
30 (defvar vip-insert-point nil
31 "Remember insert point as a marker. \(buffer specific\)")
33 (set-default 'vip-insert-point
(make-marker))
34 (make-variable-buffer-local 'vip-insert-point
)
36 (defvar vip-com-point nil
37 "Remember com point as a marker. \(buffer specific\)")
39 (set-default 'vip-com-point
(make-marker))
40 (make-variable-buffer-local 'vip-com-point
)
42 (defvar vip-current-mode nil
43 "Current mode. One of emacs-mode, vi-mode, insert-mode.")
45 (make-variable-buffer-local 'vip-current-mode
)
46 (setq-default vip-current-mode
'emacs-mode
)
48 (defvar vip-emacs-mode-line-buffer-identification nil
49 "value of mode-line-buffer-identification in emacs-mode.")
50 (make-variable-buffer-local 'vip-emacs-mode-line-buffer-identification
)
51 (setq-default vip-emacs-mode-line-buffer-identification
54 (defvar vip-current-major-mode nil
55 "vip-current-major-mode is the major-mode vi considers it is now.
58 (make-variable-buffer-local 'vip-current-major-mode
)
60 (defvar vip-last-shell-com nil
61 "last shell command executed by ! command")
63 (defvar vip-use-register nil
64 "name of register to store deleted or yanked strings.")
67 "If non-nil, it's value is a list (M-COM VAL COM), and is used to
68 re-execute last destrcutive command")
70 (defconst vip-shift-width
8
71 "*The number of colums shifted by > and < command.")
73 (defconst vip-re-replace nil
74 "*If t then do regexp replace, if nil then do string replace.")
76 (defvar vip-d-char nil
77 "The character remenbered by the vi \"r\" command")
79 (defvar vip-f-char nil
80 "for use by \";\" command")
82 (defvar vip-F-char nil
83 "for use by \".\" command")
85 (defvar vip-f-forward nil
86 "for use by \";\" command")
88 (defvar vip-f-offset nil
89 "for use by \";\" command")
91 (defconst vip-search-wrap-around t
92 "*if t, search wraps around")
94 (defconst vip-re-search nil
95 "*if t, search is reg-exp search, otherwise vanilla search.")
97 (defvar vip-s-string nil
100 (defvar vip-s-forward nil
101 "if t, search is forward.")
103 (defconst vip-case-fold-search nil
104 "*if t, search ignores cases.")
106 (defconst vip-re-query-replace nil
107 "*If t then do regexp replace, if nil then do string replace.")
109 (defconst vip-open-with-indent nil
110 "*if t, indent when open a new line.")
112 (defconst vip-help-in-insert-mode nil
113 "*if t then C-h is bound to help-command in insert mode, if nil then it is
114 bound to delete-backward-char.")
116 (defvar vip-quote-string
"> "
117 "string inserted at the beginning of region")
119 (defvar vip-tags-file-name
"TAGS")
121 (defvar vip-inhibit-startup-message nil
)
125 (global-set-key "\C-z" 'vip-change-mode-to-vi
)
127 (defmacro vip-loop
(count body
)
128 "(COUNT BODY) Execute BODY COUNT times."
129 (list 'let
(list (list 'count count
))
130 (list 'while
(list '> 'count
0)
132 (list 'setq
'count
(list '1-
'count
)))))
134 (defun vip-push-mark-silent (&optional location
)
135 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
139 (setq mark-ring
(cons (copy-marker (mark-marker)) mark-ring
))
140 (if (> (length mark-ring
) mark-ring-max
)
142 (move-marker (car (nthcdr mark-ring-max mark-ring
)) nil
)
143 (setcdr (nthcdr (1- mark-ring-max
) mark-ring
) nil
))))
144 (set-mark (or location
(point))))
146 (defun vip-goto-col (arg)
147 "Go to ARG's column."
149 (let ((val (vip-p-val arg
))
150 (com (vip-getcom arg
)))
153 (if (> val
(1+ (current-column))) (error "")))
154 (if com
(move-marker vip-com-point
(point)))
156 (forward-char (1- val
))
157 (if com
(vip-execute-com 'vip-goto-col val com
))))
159 (defun vip-refresh-mode-line ()
161 (set-buffer-modified-p (buffer-modified-p)))
163 (defun vip-copy-keymap (map)
164 (if (null map
) (make-sparse-keymap) (copy-keymap map
)))
169 (defun vip-change-mode (new-mode)
170 "Change mode to NEW-MODE. NEW-MODE is either emacs-mode, vi-mode,
172 (or (eq new-mode vip-current-mode
)
174 (cond ((eq new-mode
'vi-mode
)
175 (if (eq vip-current-mode
'insert-mode
)
177 (vip-copy-region-as-kill (point) vip-insert-point
)
178 (vip-repeat-insert-command))
179 (setq vip-emacs-local-map
(current-local-map)
180 vip-emacs-mode-line-buffer-identification
181 mode-line-buffer-identification
182 vip-insert-local-map
(vip-copy-keymap
183 (current-local-map))))
184 (vip-change-mode-line "Vi: ")
185 (use-local-map vip-mode-map
))
186 ((eq new-mode
'insert-mode
)
187 (move-marker vip-insert-point
(point))
188 (if (eq vip-current-mode
'emacs-mode
)
189 (setq vip-emacs-local-map
(current-local-map)
190 vip-emacs-mode-line-buffer-identification
191 mode-line-buffer-identification
192 vip-insert-local-map
(vip-copy-keymap
193 (current-local-map)))
194 (setq vip-insert-local-map
(vip-copy-keymap
195 vip-emacs-local-map
)))
196 (vip-change-mode-line "Insert")
197 (use-local-map vip-insert-local-map
)
198 (define-key vip-insert-local-map
"\e" 'vip-change-mode-to-vi
)
199 (define-key vip-insert-local-map
"\C-z" 'vip-ESC
)
200 (define-key vip-insert-local-map
"\C-h"
201 (if vip-help-in-insert-mode
'help-command
202 'delete-backward-char
))
203 (define-key vip-insert-local-map
"\C-w"
204 'vip-delete-backward-word
))
205 ((eq new-mode
'emacs-mode
)
206 (vip-change-mode-line "Emacs:")
207 (use-local-map vip-emacs-local-map
)))
208 (setq vip-current-mode new-mode
)
209 (vip-refresh-mode-line))))
211 (defun vip-copy-region-as-kill (beg end
)
212 "If BEG and END do not belong to the same buffer, it copies empty region."
214 (copy-region-as-kill beg end
)
215 (error (copy-region-as-kill beg beg
))))
217 (defun vip-change-mode-line (string)
218 "Assuming that the mode line format contains the string \"Emacs:\", this
219 function replaces the string by \"Vi: \" etc."
220 (setq mode-line-buffer-identification
221 (if (string= string
"Emacs:")
222 vip-emacs-mode-line-buffer-identification
223 (list (concat string
" %17b")))))
227 "Turn on VIP emulation of VI."
229 (if (not vip-inhibit-startup-message
)
231 (switch-to-buffer "VIP Startup Message")
234 "VIP is a Vi emulation package for GNU Emacs. VIP provides most Vi commands
235 including Ex commands. VIP is however different from Vi in several points.
236 You can get more information on VIP by:
237 1. Typing `M-x info' and selecting menu item \"vip\".
238 2. Typing `C-h k' followed by a key whose description you want.
239 3. Printing VIP manual which can be found as GNU/man/vip.texinfo
240 4. Printing VIP Reference Card which can be found as GNU/etc/vipcard.tex
242 This startup message appears whenever you load VIP unless you type `y' now.
243 Type `n' to quit this window for now.\n")
244 (goto-char (point-min))
245 (if (y-or-n-p "Inhibit VIP startup message? ")
249 (find-file-noselect (substitute-in-file-name "~/.vip")))
250 (goto-char (point-max))
251 (insert "\n(setq vip-inhibit-startup-message t)\n")
253 (kill-buffer (current-buffer)))
254 (message "VIP startup message inhibited.")
256 (kill-buffer (current-buffer))
258 (setq vip-inhibit-startup-message t
)))
259 (vip-change-mode-to-vi))
261 (defun vip-change-mode-to-vi ()
262 "Change mode to vi mode."
264 (vip-change-mode 'vi-mode
))
266 (defun vip-change-mode-to-insert ()
267 "Change mode to insert mode."
269 (vip-change-mode 'insert-mode
))
271 (defun vip-change-mode-to-emacs ()
272 "Change mode to emacs mode."
274 (vip-change-mode 'emacs-mode
))
277 ;; escape to emacs mode termporarilly
279 (defun vip-get-editor-command (l-map g-map
&optional str
)
280 "Read characters from keyboard until an editor command is formed, using
281 local keymap L-MAP and global keymap G-MAP. If the command is a
282 self-insert-command, the character just read is returned instead. Optional
283 string STR is used as initial input string."
284 (let (char l-bind g-bind
)
286 (if (or (null str
) (string= str
""))
288 (string-to-char str
)))
289 (setq last-command-char char
)
290 (setq l-bind
(vip-binding-of char l-map
))
292 ;; since local binding is empty, we concentrate on global one.
294 (setq g-bind
(vip-binding-of char g-map
))
296 nil
;; return nil, since both bindings are void.
298 (vip-get-editor-command nil g-bind
(vip-string-tail str
))
299 (if (eq g-bind
'self-insert-command
) char g-bind
))))
300 ;; local binding is nonvoid
302 ;; since l-bind is a keymap, we consider g-bind as well.
304 (setq g-bind
(vip-binding-of char g-map
))
306 (vip-get-editor-command l-bind nil
(vip-string-tail str
))
308 ;; both bindings are keymap
309 (vip-get-editor-command l-bind g-bind
(vip-string-tail str
))
310 ;; l-bind is a keymap, so we neglect g-bind
311 (vip-get-editor-command l-bind nil
(vip-string-tail str
)))))
312 ;; l-bind is a command
313 (if (eq l-bind
'self-insert-command
) char l-bind
)))))
315 (defun vip-binding-of (char map
)
316 "Return key-binding of CHAR under keymap MAP. It is nil if the binding
317 is void, or a command, or a keymap"
318 (let ((val (if (listp map
)
319 (cdr (assq char map
))
321 (cond ((null val
) nil
)
323 (if (symbolp val
) (symbol-function val
) val
))
325 ;; otherwise, it is a function which is either a real function or
326 ;; a keymap fset to val.
327 (let ((fun (symbol-function val
)))
328 (if (or (null fun
) (keymapp fun
)) fun val
))))))
330 (defun vip-escape-to-emacs (arg &optional char
)
331 "Escape to emacs mode and execute one emacs command and then return to
332 vi mode. ARG is used as the prefix value for the executed command. If
333 CHAR is given it becomes the first character of the command."
335 (let (com (buff (current-buffer)) (first t
))
336 (if char
(setq unread-command-char char
))
337 (setq prefix-arg arg
)
338 (while (or first
(>= unread-command-char
0))
339 ;; this while loop is executed until unread command char will be
342 (setq com
(vip-get-editor-command vip-emacs-local-map global-map
))
344 (vip-loop (vip-p-val prefix-arg
)
345 (insert (char-to-string com
)))
346 (command-execute com prefix-arg
)))
347 (setq prefix-arg nil
) ;; reset prefix arg
350 (defun vip-message-conditions (conditions)
351 "Print CONDITIONS as a message."
352 (let ((case (car conditions
)) (msg (cdr conditions
)))
355 (message "%s %s" case
(prin1-to-string msg
)))
359 "Emulate ESC key in Emacs mode."
361 (vip-escape-to-emacs arg ?\e
))
363 (defun vip-ctl-c (arg)
364 "Emulate C-c key in Emacs mode."
366 (vip-escape-to-emacs arg ?\C-c
))
368 (defun vip-ctl-x (arg)
369 "Emulate C-x key in Emacs mode."
371 (vip-escape-to-emacs arg ?\C-x
))
373 (defun vip-ctl-h (arg)
374 "Emulate C-h key in Emacs mode."
376 (vip-escape-to-emacs arg ?\C-h
))
379 ;; prefix argmument for vi mode
381 ;; In vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
382 ;; represents the numeric value of the prefix argument and COM represents
383 ;; command prefix such as "c", "d", "m" and "y".
385 (defun vip-prefix-arg-value (char value com
)
386 "Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value
387 obtained so far, and COM is the command part obtained so far."
388 (while (and (>= char ?
0) (<= char ?
9))
389 (setq value
(+ (* (if (numberp value
) value
0) 10) (- char ?
0)))
390 (setq char
(read-char)))
391 (setq prefix-arg value
)
392 (if com
(setq prefix-arg
(cons prefix-arg com
)))
394 (vip-describe-arg prefix-arg
)
395 (setq char
(read-char)))
396 (setq unread-command-char char
))
398 (defun vip-prefix-arg-com (char value com
)
399 "Vi operator as prefix argument."
402 (or (= char ?c
) (= char ?d
) (= char ?y
)
403 (= char ?
!) (= char ?
<) (= char ?
>) (= char ?
=)
404 (= char ?
#) (= char ?r
) (= char ?R
) (= char ?
\")))
406 ;; this means that we already have a command character, so we
407 ;; construct a com list and exit while. however, if char is "
410 ;; new com is (CHAR . OLDCOM)
411 (if (or (= char ?
#) (= char ?
\")) (error ""))
412 (setq com
(cons char com
))
414 ;; if com is nil we set com as char, and read more. again, if char
415 ;; is ", we read the name of register and store it in vip-use-register.
416 ;; if char is !, =, or #, a copmlete com is formed so we exit while.
417 (cond ((or (= char ?
!) (= char ?
=))
419 (setq char
(read-char))
422 ;; read a char and encode it as com
423 (setq com
(+ 128 (read-char)))
424 (setq char
(read-char))
426 ((or (= char ?
<) (= char ?
>))
428 (setq char
(read-char))
429 (if (= com char
) (setq com
(cons char com
)))
432 (let ((reg (read-char)))
433 (if (or (and (<= ?A reg
) (<= reg ?z
))
434 (and (<= ?
1 reg
) (<= reg ?
9)))
435 (setq vip-use-register reg
)
437 (setq char
(read-char))))
440 (setq char
(read-char)))))))
442 ;; com is a single char, so we construct prefix-arg
443 ;; and if char is ?, describe prefix arg, otherwise exit by
444 ;; pushing the char back
446 (setq prefix-arg
(cons value com
))
448 (vip-describe-arg prefix-arg
)
449 (setq char
(read-char)))
450 (setq unread-command-char char
))
451 ;; as com is non-nil, this means that we have a command to execute
452 (if (or (= (car com
) ?r
) (= (car com
) ?R
))
453 ;; execute apropriate region command.
454 (let ((char (car com
)) (com (cdr com
)))
455 (setq prefix-arg
(cons value com
))
456 (if (= char ?r
) (vip-region prefix-arg
)
457 (vip-Region prefix-arg
))
459 (setq prefix-arg nil
))
460 ;; otherwise, reset prefix arg and call appropriate command
461 (setq value
(if (null value
) 1 value
))
462 (setq prefix-arg nil
)
463 (cond ((equal com
'(?c . ?c
)) (vip-line (cons value ?C
)))
464 ((equal com
'(?d . ?d
)) (vip-line (cons value ?D
)))
465 ((equal com
'(?d . ?y
)) (vip-yank-defun))
466 ((equal com
'(?y . ?y
)) (vip-line (cons value ?Y
)))
467 ((equal com
'(?
< . ?
<)) (vip-line (cons value ?
<)))
468 ((equal com
'(?
> . ?
>)) (vip-line (cons value ?
>)))
469 ((equal com
'(?
! . ?
!)) (vip-line (cons value ?
!)))
470 ((equal com
'(?
= . ?
=)) (vip-line (cons value ?
=)))
473 (defun vip-describe-arg (arg)
475 (setq val
(vip-P-val arg
)
476 com
(vip-getcom arg
))
479 (message "Value is nil, and commmand is nil.")
480 (message "Value is nil, and command is %c." com
))
482 (message "Value is %d, and command is nil." val
)
483 (message "Value is %d, and command is %c." val com
)))))
485 (defun vip-digit-argument (arg)
486 "Begin numeric argument for the next command."
488 (vip-prefix-arg-value last-command-char nil
489 (if (consp arg
) (cdr arg
) nil
)))
491 (defun vip-command-argument (arg)
492 "Accept a motion command as an argument."
494 (condition-case conditions
497 (cond ((null arg
) nil
)
498 ((consp arg
) (car arg
))
500 (t (error "strange arg")))
501 (cond ((null arg
) nil
)
502 ((consp arg
) (cdr arg
))
504 (t (error "strange arg"))))
506 (setq vip-use-register nil
)
507 (signal 'quit nil
))))
509 (defun vip-p-val (arg)
510 "Get value part of prefix-argument ARG."
512 ((consp arg
) (if (null (car arg
)) 1 (car arg
)))
515 (defun vip-P-val (arg)
516 "Get value part of prefix-argument ARG."
517 (cond ((consp arg
) (car arg
))
520 (defun vip-getcom (arg)
521 "Get com part of prefix-argument ARG."
522 (cond ((null arg
) nil
)
523 ((consp arg
) (cdr arg
))
526 (defun vip-getCom (arg)
527 "Get com part of prefix-argument ARG and modify it."
528 (let ((com (vip-getcom arg
)))
529 (cond ((equal com ?c
) ?C
)
535 ;; repeat last destructive command
537 (defun vip-append-to-register (reg start end
)
538 "Append region to text in register REG.
539 START and END are buffer positions indicating what to append."
540 (set-register reg
(concat (or (get-register reg
) "")
541 (buffer-substring start end
))))
543 (defun vip-execute-com (m-com val com
)
544 "(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set
545 to vip-d-com for later use by vip-repeat"
546 (let ((reg vip-use-register
))
548 (cond ((= com ?c
) (vip-change vip-com-point
(point)))
549 ((= com
(- ?c
)) (vip-change-subr vip-com-point
(point)))
550 ((or (= com ?C
) (= com
(- ?C
)))
552 (set-mark vip-com-point
)
553 (vip-enlarge-region (mark) (point))
556 (cond ((and (<= ?a vip-use-register
)
557 (<= vip-use-register ?z
))
559 vip-use-register
(mark) (point) nil
))
560 ((and (<= ?A vip-use-register
)
561 (<= vip-use-register ?Z
))
562 (vip-append-to-register
563 (+ vip-use-register
32) (mark) (point)))
564 (t (setq vip-use-register nil
)
566 (setq vip-use-register nil
)))
567 (delete-region (mark) (point)))
569 (if (= com ?C
) (vip-change-mode-to-insert) (yank)))
573 (cond ((and (<= ?a vip-use-register
)
574 (<= vip-use-register ?z
))
576 vip-use-register vip-com-point
(point) nil
))
577 ((and (<= ?A vip-use-register
)
578 (<= vip-use-register ?Z
))
579 (vip-append-to-register
580 (+ vip-use-register
32) vip-com-point
(point)))
581 (t (setq vip-use-register nil
)
583 (setq vip-use-register nil
)))
585 (if (eq last-command
'd-command
) 'kill-region nil
))
586 (kill-region vip-com-point
(point))
587 (setq this-command
'd-command
))
590 (set-mark vip-com-point
)
591 (vip-enlarge-region (mark) (point))
594 (cond ((and (<= ?a vip-use-register
)
595 (<= vip-use-register ?z
))
597 vip-use-register
(mark) (point) nil
))
598 ((and (<= ?A vip-use-register
)
599 (<= vip-use-register ?Z
))
600 (vip-append-to-register
601 (+ vip-use-register
32) (mark) (point)))
602 (t (setq vip-use-register nil
)
604 (setq vip-use-register nil
)))
606 (if (eq last-command
'D-command
) 'kill-region nil
))
607 (kill-region (mark) (point))
608 (if (eq m-com
'vip-line
) (setq this-command
'D-command
)))
609 (back-to-indentation))
613 (cond ((and (<= ?a vip-use-register
)
614 (<= vip-use-register ?z
))
616 vip-use-register vip-com-point
(point) nil
))
617 ((and (<= ?A vip-use-register
)
618 (<= vip-use-register ?Z
))
619 (vip-append-to-register
620 (+ vip-use-register
32) vip-com-point
(point)))
621 (t (setq vip-use-register nil
)
623 (setq vip-use-register nil
)))
624 (setq last-command nil
)
625 (copy-region-as-kill vip-com-point
(point))
626 (goto-char vip-com-point
))
629 (set-mark vip-com-point
)
630 (vip-enlarge-region (mark) (point))
633 (cond ((and (<= ?a vip-use-register
)
634 (<= vip-use-register ?z
))
636 vip-use-register
(mark) (point) nil
))
637 ((and (<= ?A vip-use-register
)
638 (<= vip-use-register ?Z
))
639 (vip-append-to-register
640 (+ vip-use-register
32) (mark) (point)))
641 (t (setq vip-use-register nil
)
643 (setq vip-use-register nil
)))
644 (setq last-command nil
)
645 (copy-region-as-kill (mark) (point)))
646 (goto-char vip-com-point
))
647 ((or (= com ?
!) (= com
(- ?
!)))
649 (set-mark vip-com-point
)
650 (vip-enlarge-region (mark) (point))
651 (shell-command-on-region
654 (setq vip-last-shell-com
(vip-read-string "!"))
659 (set-mark vip-com-point
)
660 (vip-enlarge-region (mark) (point))
661 (if (> (mark) (point)) (exchange-point-and-mark))
662 (indent-region (mark) (point) nil
)))
665 (set-mark vip-com-point
)
666 (vip-enlarge-region (mark) (point))
667 (indent-rigidly (mark) (point) (- vip-shift-width
)))
668 (goto-char vip-com-point
))
671 (set-mark vip-com-point
)
672 (vip-enlarge-region (mark) (point))
673 (indent-rigidly (mark) (point) vip-shift-width
))
674 (goto-char vip-com-point
))
676 ;; this is special command #
677 (vip-special-prefix-com (- com
128)))))
678 (setq vip-d-com
(list m-com val
(if (or (= com ?c
) (= com ?C
) (= com ?
!))
682 (defun vip-repeat (arg)
683 "(ARG) Re-excute last destructive command. vip-d-com has the form
684 (COM ARG CH REG), where COM is the command to be re-executed, ARG is the
685 argument for COM, CH is a flag for repeat, and REG is optional and if exists
686 is the name of the register for COM."
688 (if (eq last-command
'vip-undo
)
689 ;; if the last command was vip-undo, then undo-more
691 ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
692 ;; its prefix value is used as new prefix value for the command.
693 (let ((m-com (car vip-d-com
))
694 (val (vip-P-val arg
))
695 (com (car (cdr (cdr vip-d-com
))))
696 (reg (nth 3 vip-d-com
)))
697 (if (null val
) (setq val
(car (cdr vip-d-com
))))
698 (if (null m-com
) (error "No previous command to repeat."))
699 (setq vip-use-register reg
)
700 (funcall m-com
(cons val com
)))))
702 (defun vip-special-prefix-com (char)
703 "This command is invoked interactively by the key sequence #<char>"
705 (downcase-region (min vip-com-point
(point))
706 (max vip-com-point
(point))))
708 (upcase-region (min vip-com-point
(point))
709 (max vip-com-point
(point))))
711 (set-mark vip-com-point
)
712 (vip-global-execute))
714 (set-mark vip-com-point
)
716 ((= char ?s
) (spell-region vip-com-point
(point)))))
722 "Undo previous change."
727 (setq this-command
'vip-undo
))
729 (defun vip-undo-more ()
730 "Continue undoing previous changes."
731 (message "undo more!")
733 (setq this-command
'vip-undo
))
738 (defun vip-string-tail (str)
739 (if (or (null str
) (string= str
"")) nil
742 (defun vip-yank-defun ()
744 (copy-region-as-kill (point) (mark)))
746 (defun vip-enlarge-region (beg end
)
747 "Enlarge region between BEG and END."
749 (progn (goto-char beg
) (set-mark end
))
753 (exchange-point-and-mark)
754 (if (or (not (eobp)) (not (bolp))) (next-line 1))
756 (if (> beg end
) (exchange-point-and-mark)))
758 (defun vip-global-execute ()
759 "Call last keyboad macro for each line in the region."
760 (if (> (point) (mark)) (exchange-point-and-mark))
762 (call-last-kbd-macro)
763 (while (< (point) (mark))
766 (call-last-kbd-macro)))
768 (defun vip-quote-region ()
769 "Quote region by inserting the user supplied string at the beginning of
770 each line in the region."
771 (setq vip-quote-string
773 (vip-read-string (format "quote string \(default \"%s\"\): "
775 (if (string= str
"") vip-quote-string str
)))
776 (vip-enlarge-region (point) (mark))
777 (if (> (point) (mark)) (exchange-point-and-mark))
778 (insert vip-quote-string
)
781 (while (and (< (point) (mark)) (bolp))
782 (insert vip-quote-string
)
786 (defun vip-end-with-a-newline-p (string)
787 "Check if the string ends with a newline."
788 (or (string= text
"")
789 (= (aref string
(1- (length string
))) ?
\n)))
791 (defun vip-read-string (prompt &optional init
)
792 (setq save-minibuffer-local-map
(copy-keymap minibuffer-local-map
))
793 (define-key minibuffer-local-map
"\C-h" 'backward-char
)
794 (define-key minibuffer-local-map
"\C-w" 'backward-word
)
795 (define-key minibuffer-local-map
"\e" 'exit-minibuffer
)
797 (condition-case conditions
798 (setq str
(read-string prompt init
))
800 (setq minibuffer-local-map save-minibuffer-local-map
)
802 (setq minibuffer-local-map save-minibuffer-local-map
)
806 ;; insertion commands
808 (defun vip-repeat-insert-command ()
809 "This function is called when mode changes from insertion mode to
810 vi command mode. It will repeat the insertion command if original insertion
811 command was invoked with argument > 1."
812 (let ((i-com (car vip-d-com
)) (val (car (cdr vip-d-com
))))
813 (if (and val
(> val
1)) ;; first check that val is non-nil
815 (setq vip-d-com
(list i-com
(1- val
) ?r
))
817 (setq vip-d-com
(list i-com val ?r
))))))
819 (defun vip-insert (arg) ""
821 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
822 (setq vip-d-com
(list 'vip-insert val ?r
))
823 (if com
(vip-loop val
(yank))
824 (vip-change-mode-to-insert))))
826 (defun vip-append (arg)
827 "Append after point."
829 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
830 (setq vip-d-com
(list 'vip-append val ?r
))
831 (if (not (eolp)) (forward-char))
833 (vip-loop val
(yank))
834 (vip-change-mode-to-insert))))
836 (defun vip-Append (arg)
837 "Append at end of line."
839 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
840 (setq vip-d-com
(list 'vip-Append val ?r
))
843 (vip-loop val
(yank))
844 (vip-change-mode-to-insert))))
846 (defun vip-Insert (arg)
847 "Insert before first non-white."
849 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
850 (setq vip-d-com
(list 'vip-Insert val ?r
))
851 (back-to-indentation)
853 (vip-loop val
(yank))
854 (vip-change-mode-to-insert))))
856 (defun vip-open-line (arg)
859 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
860 (setq vip-d-com
(list 'vip-open-line val ?r
))
861 (let ((col (current-indentation)))
867 (if vip-open-with-indent
(indent-to col
))
871 (if vip-open-with-indent
(indent-to col
))
872 (vip-change-mode-to-insert)))))
874 (defun vip-Open-line (arg)
877 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
878 (setq vip-d-com
(list 'vip-Open-line val ?r
))
879 (let ((col (current-indentation)))
885 (if vip-open-with-indent
(indent-to col
))
889 (if vip-open-with-indent
(indent-to col
))
890 (vip-change-mode-to-insert)))))
892 (defun vip-open-line-at-point (arg)
893 "Open line at point."
895 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
896 (setq vip-d-com
(list 'vip-open-line-at-point val ?r
))
903 (vip-change-mode-to-insert))))
905 (defun vip-substitute (arg)
906 "Substitute characters."
908 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
913 (vip-change-subr (mark) (point))
914 (vip-change (mark) (point))))
915 (setq vip-d-com
(list 'vip-substitute val ?r
))))
917 (defun vip-substitute-line (arg)
920 (vip-line (cons arg ?C
)))
925 (defun vip-line (arg)
926 (let ((val (car arg
)) (com (cdr arg
)))
927 (move-marker vip-com-point
(point))
929 (vip-execute-com 'vip-line val com
)))
931 (defun vip-yank-line (arg)
932 "Yank ARG lines (in vi's sense)"
934 (let ((val (vip-p-val arg
)))
935 (vip-line (cons val ?Y
))))
940 (defun vip-region (arg)
942 (let ((val (vip-P-val arg
))
943 (com (vip-getcom arg
)))
944 (move-marker vip-com-point
(point))
945 (exchange-point-and-mark)
946 (vip-execute-com 'vip-region val com
)))
948 (defun vip-Region (arg)
950 (let ((val (vip-P-val arg
))
951 (com (vip-getCom arg
)))
952 (move-marker vip-com-point
(point))
953 (exchange-point-and-mark)
954 (vip-execute-com 'vip-Region val com
)))
956 (defun vip-replace-char (arg)
957 "Replace the following ARG chars by the character read."
959 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
960 (setq vip-d-com
(list 'vip-replace-char val ?r
))
961 (vip-replace-char-subr (if (equal com ?r
) vip-d-char
(read-char)) val
)))
963 (defun vip-replace-char-subr (char arg
)
965 (setq vip-d-char char
)
966 (vip-loop (if (> arg
0) arg
(- arg
)) (insert char
))
969 (defun vip-replace-string ()
970 "Replace string. If you supply null string as the string to be replaced,
971 the query replace mode will toggle between string replace and regexp replace."
974 (setq str
(vip-read-string
975 (if vip-re-replace
"Replace regexp: " "Replace string: ")))
978 (setq vip-re-replace
(not vip-re-replace
))
979 (message (format "Replace mode changed to %s."
980 (if vip-re-replace
"regexp replace"
985 (vip-read-string (format "Replace regexp \"%s\" with: " str
)))
988 (vip-read-string (format "Replace \"%s\" with: " str
)))))))
991 ;; basic cursor movement. j, k, l, m commands.
993 (defun vip-forward-char (arg)
994 "Move point right ARG characters (left if ARG negative).On reaching end
995 of buffer, stop and signal error."
997 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
998 (if com
(move-marker vip-com-point
(point)))
1000 (if com
(vip-execute-com 'vip-forward-char val com
))))
1002 (defun vip-backward-char (arg)
1003 "Move point left ARG characters (right if ARG negative). On reaching
1004 beginning of buffer, stop and signal error."
1006 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1007 (if com
(move-marker vip-com-point
(point)))
1009 (if com
(vip-execute-com 'vip-backward-char val com
))))
1014 (defun vip-forward-word (arg)
1017 (let ((val (vip-p-val arg
))
1018 (com (vip-getcom arg
)))
1019 (if com
(move-marker vip-com-point
(point)))
1021 (skip-chars-forward " \t\n")
1024 (if (or (= com ?c
) (= com
(- ?c
)))
1025 (progn (backward-word 1) (forward-word 1)))
1026 (if (or (= com ?d
) (= com ?y
))
1030 (skip-chars-forward " \t")))
1031 (vip-execute-com 'vip-forward-word val com
)))))
1033 (defun vip-end-of-word (arg)
1034 "Move point to end of current word."
1036 (let ((val (vip-p-val arg
))
1037 (com (vip-getcom arg
)))
1038 (if com
(move-marker vip-com-point
(point)))
1045 (vip-execute-com 'vip-end-of-word val com
)))))
1047 (defun vip-backward-word (arg)
1050 (let ((val (vip-p-val arg
))
1051 (com (vip-getcom arg
)))
1052 (if com
(move-marker vip-com-point
(point)))
1054 (if com
(vip-execute-com 'vip-backward-word val com
))))
1056 (defun vip-forward-Word (arg)
1057 "Forward word delimited by white character."
1059 (let ((val (vip-p-val arg
))
1060 (com (vip-getcom arg
)))
1061 (if com
(move-marker vip-com-point
(point)))
1062 (re-search-forward "[^ \t\n]*[ \t\n]+" nil t val
)
1065 (if (or (= com ?c
) (= com
(- ?c
)))
1066 (progn (backward-word 1) (forward-word 1)))
1067 (if (or (= com ?d
) (= com ?y
))
1071 (skip-chars-forward " \t")))
1072 (vip-execute-com 'vip-forward-Word val com
)))))
1074 (defun vip-end-of-Word (arg)
1075 "Move forward to end of word delimited by white character."
1077 (let ((val (vip-p-val arg
))
1078 (com (vip-getcom arg
)))
1079 (if com
(move-marker vip-com-point
(point)))
1081 (if (re-search-forward "[^ \t\n]+" nil t val
) (backward-char))
1085 (vip-execute-com 'vip-end-of-Word val com
)))))
1087 (defun vip-backward-Word (arg)
1088 "Backward word delimited by white character."
1090 (let ((val (vip-p-val arg
))
1091 (com (vip-getcom arg
)))
1092 (if com
(move-marker vip-com-point
(point)))
1093 (if (re-search-backward "[ \t\n]+[^ \t\n]+" nil t val
)
1095 (goto-char (point-min)))
1096 (if com
(vip-execute-com 'vip-backward-Word val com
))))
1098 (defun vip-beginning-of-line (arg)
1099 "Go to beginning of line."
1101 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1102 (if com
(move-marker vip-com-point
(point)))
1103 (beginning-of-line val
)
1104 (if com
(vip-execute-com 'vip-beginning-of-line val com
))))
1106 (defun vip-bol-and-skip-white (arg)
1107 "Beginning of line at first non-white character."
1109 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1110 (if com
(move-marker vip-com-point
(point)))
1111 (back-to-indentation)
1112 (if com
(vip-execute-com 'vip-bol-and-skip-white val com
))))
1114 (defun vip-goto-eol (arg)
1115 "Go to end of line."
1117 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1118 (if com
(move-marker vip-com-point
(point)))
1120 (if com
(vip-execute-com 'vip-goto-eol val com
))))
1122 (defun vip-next-line (arg)
1125 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1126 (if com
(move-marker vip-com-point
(point)))
1128 (setq this-command
'next-line
)
1129 (if com
(vip-execute-com 'vip-next-line val com
))))
1131 (defun vip-next-line-at-bol (arg)
1132 "Next line at beginning of line."
1134 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1135 (if com
(move-marker vip-com-point
(point)))
1137 (back-to-indentation)
1138 (if com
(vip-execute-com 'vip-next-line-at-bol val com
))))
1140 (defun vip-previous-line (arg)
1141 "Go to previous line."
1143 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1144 (if com
(move-marker vip-com-point
(point)))
1146 (setq this-command
'previous-line
)
1147 (if com
(vip-execute-com 'vip-previous-line val com
))))
1149 (defun vip-previous-line-at-bol (arg)
1150 "Previous line at beginning of line."
1152 (let ((val (vip-p-val arg
)) (com (vip-getCom arg
)))
1153 (if com
(move-marker vip-com-point
(point)))
1155 (back-to-indentation)
1156 (if com
(vip-execute-com 'vip-previous-line val com
))))
1158 (defun vip-change-to-eol (arg)
1159 "Change to end of line."
1161 (vip-goto-eol (cons arg ?c
)))
1163 (defun vip-kill-line (arg)
1166 (vip-goto-eol (cons arg ?d
)))
1171 (defun vip-goto-line (arg)
1172 "Go to ARG's line. Without ARG go to end of buffer."
1174 (let ((val (vip-P-val arg
)) (com (vip-getCom arg
)))
1175 (move-marker vip-com-point
(point))
1178 (goto-char (point-max))
1179 (goto-char (point-min))
1180 (forward-line (1- val
)))
1181 (back-to-indentation)
1182 (if com
(vip-execute-com 'vip-goto-line val com
))))
1184 (defun vip-find-char (arg char forward offset
)
1185 "Find ARG's occurence of CHAR on the current line. If FORWARD then
1186 search is forward, otherwise backward. OFFSET is used to adjust point
1188 (let ((arg (if forward arg
(- arg
))) point
)
1193 ;; forward search begins here
1194 (if (eolp) (error "") (point))
1195 ;; forward search ends here
1196 (progn (next-line 1) (beginning-of-line) (point)))
1198 ;; backward search begins from here
1199 (if (bolp) (error "") (point))
1200 ;; backward search ends here
1201 (progn (beginning-of-line) (point))))
1202 ;; if arg > 0, point is forwarded before search.
1203 (if (> arg
0) (goto-char (1+ (point-min)))
1204 (goto-char (point-max)))
1205 (let ((case-fold-search nil
))
1206 (search-forward (char-to-string char
) nil
0 arg
))
1207 (setq point
(point))
1208 (if (or (and (> arg
0) (= point
(point-max)))
1209 (and (< arg
0) (= point
(point-min))))
1211 (goto-char (+ point
(if (> arg
0) (if offset -
2 -
1) (if offset
1 0))))))
1213 (defun vip-find-char-forward (arg)
1214 "Find char on the line. If called interactively read the char to find
1215 from the terminal, and if called from vip-repeat, the char last used is
1216 used. This behaviour is controlled by the sign of prefix numeric value."
1218 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1220 ;; this means that the function was called interactively
1221 (setq vip-f-char
(read-char)
1225 (if com
(move-marker vip-com-point
(point)))
1226 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t nil
)
1230 (setq vip-F-char vip-f-char
);; set new vip-F-char
1232 (vip-execute-com 'vip-find-char-forward val com
)))))
1234 (defun vip-goto-char-forward (arg)
1235 "Go up to char ARG forward on line."
1237 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1239 ;; this means that the function was called interactively
1240 (setq vip-f-char
(read-char)
1244 (if com
(move-marker vip-com-point
(point)))
1245 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) t t
)
1249 (setq vip-F-char vip-f-char
);; set new vip-F-char
1251 (vip-execute-com 'vip-goto-char-forward val com
)))))
1253 (defun vip-find-char-backward (arg)
1254 "Find char ARG on line backward."
1256 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1258 ;; this means that the function was called interactively
1259 (setq vip-f-char
(read-char)
1263 (if com
(move-marker vip-com-point
(point)))
1265 val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil nil
)
1269 (setq vip-F-char vip-f-char
);; set new vip-F-char
1270 (vip-execute-com 'vip-find-char-backward val com
)))))
1272 (defun vip-goto-char-backward (arg)
1273 "Go up to char ARG backward on line."
1275 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1277 ;; this means that the function was called interactively
1278 (setq vip-f-char
(read-char)
1282 (if com
(move-marker vip-com-point
(point)))
1283 (vip-find-char val
(if (> (vip-p-val arg
) 0) vip-f-char vip-F-char
) nil t
)
1287 (setq vip-F-char vip-f-char
);; set new vip-F-char
1288 (vip-execute-com 'vip-goto-char-backward val com
)))))
1290 (defun vip-repeat-find (arg)
1291 "Repeat previous find command."
1293 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1294 (if com
(move-marker vip-com-point
(point)))
1295 (vip-find-char val vip-f-char vip-f-forward vip-f-offset
)
1298 (if vip-f-forward
(forward-char))
1299 (vip-execute-com 'vip-repeat-find val com
)))))
1301 (defun vip-repeat-find-opposite (arg)
1302 "Repeat previous find command in the opposite direction."
1304 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1305 (if com
(move-marker vip-com-point
(point)))
1306 (vip-find-char val vip-f-char
(not vip-f-forward
) vip-f-offset
)
1309 (if vip-f-forward
(forward-char))
1310 (vip-execute-com 'vip-repeat-find-opposite val com
)))))
1313 ;; window scrolling etc.
1315 (defun vip-other-window (arg)
1316 "Switch to other window."
1319 (or (not (eq vip-current-mode
'emacs-mode
))
1320 (string= (buffer-name (current-buffer)) " *Minibuf-1*")
1321 (vip-change-mode-to-vi)))
1323 (defun vip-window-top (arg)
1324 "Go to home window line."
1326 (let ((val (vip-p-val arg
))
1327 (com (vip-getCom arg
)))
1328 (if com
(move-marker vip-com-point
(point)))
1329 (move-to-window-line (1- val
))
1330 (if com
(vip-execute-com 'vip-window-top val com
))))
1332 (defun vip-window-middle (arg)
1333 "Go to middle window line."
1335 (let ((val (vip-p-val arg
))
1336 (com (vip-getCom arg
)))
1337 (if com
(move-marker vip-com-point
(point)))
1338 (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val
)))
1339 (if com
(vip-execute-com 'vip-window-middle val com
))))
1341 (defun vip-window-bottom (arg)
1342 "Go to last window line."
1344 (let ((val (vip-p-val arg
))
1345 (com (vip-getCom arg
)))
1346 (if com
(move-marker vip-com-point
(point)))
1347 (move-to-window-line (- val
))
1348 (if com
(vip-execute-com 'vip-window-bottom val com
))))
1350 (defun vip-line-to-top (arg)
1351 "Put current line on the home line."
1353 (recenter (1- arg
)))
1355 (defun vip-line-to-middle (arg)
1356 "Put current line on the middle line."
1358 (recenter (+ (1- arg
) (/ (1- (window-height)) 2))))
1360 (defun vip-line-to-bottom (arg)
1361 "Put current line on the last line."
1363 (recenter (- (window-height) (1+ arg
))))
1368 (defun vip-paren-match (arg)
1369 "Go to the matching parenthesis."
1371 (let ((com (vip-getcom arg
)))
1373 (if (or (> arg
99) (< arg
1))
1374 (error "Prefix must be between 1 and 99.")
1376 (if (> (point-max) 80000)
1377 (* (/ (point-max) 100) arg
)
1378 (/ (* (point-max) arg
) 100)))
1379 (back-to-indentation))
1380 (cond ((looking-at "[\(\[{]")
1381 (if com
(move-marker vip-com-point
(point)))
1384 (vip-execute-com 'vip-paren-match nil com
)
1386 ((looking-at "[])}]")
1388 (if com
(move-marker vip-com-point
(point)))
1390 (if com
(vip-execute-com 'vip-paren-match nil com
)))
1394 ;; sentence and paragraph
1396 (defun vip-forward-sentence (arg)
1399 (let ((val (vip-p-val arg
))
1400 (com (vip-getcom arg
)))
1401 (if com
(move-marker vip-com-point
(point)))
1402 (forward-sentence val
)
1403 (if com
(vip-execute-com 'vip-forward-sentence nil com
))))
1405 (defun vip-backward-sentence (arg)
1406 "Backward sentence."
1408 (let ((val (vip-p-val arg
))
1409 (com (vip-getcom arg
)))
1410 (if com
(move-marker vip-com-point
(point)))
1411 (backward-sentence val
)
1412 (if com
(vip-execute-com 'vip-backward-sentence nil com
))))
1414 (defun vip-forward-paragraph (arg)
1415 "Forward paragraph."
1417 (let ((val (vip-p-val arg
))
1418 (com (vip-getCom arg
)))
1419 (if com
(move-marker vip-com-point
(point)))
1420 (forward-paragraph val
)
1421 (if com
(vip-execute-com 'vip-forward-paragraph nil com
))))
1423 (defun vip-backward-paragraph (arg)
1424 "Backward paragraph."
1426 (let ((val (vip-p-val arg
))
1427 (com (vip-getCom arg
)))
1428 (if com
(move-marker vip-com-point
(point)))
1429 (backward-paragraph val
)
1430 (if com
(vip-execute-com 'vip-backward-paragraph nil com
))))
1435 (defun vip-scroll (arg)
1436 "Scroll to next screen."
1441 (setq arg
(1- arg
)))
1444 (setq arg
(1+ arg
)))))
1446 (defun vip-scroll-back (arg)
1447 "Scroll to previous screen."
1449 (vip-scroll (- arg
)))
1451 (defun vip-scroll-down (arg)
1452 "Scroll up half screen."
1454 (if (null arg
) (scroll-down (/ (window-height) 2))
1457 (defun vip-scroll-down-one (arg)
1458 "Scroll up one line."
1462 (defun vip-scroll-up (arg)
1463 "Scroll down half screen."
1465 (if (null arg
) (scroll-up (/ (window-height) 2))
1468 (defun vip-scroll-up-one (arg)
1469 "Scroll down one line."
1476 (defun vip-buffer-in-two-windows ()
1477 "Show current buffer in two windows."
1479 (delete-other-windows)
1480 (split-window-vertically nil
))
1485 (defun vip-search-forward (arg)
1486 "Search a string forward. ARG is used to find the ARG's occurence
1487 of the string. Default is vanilla search. Search mode can be toggled by
1488 giving null search string."
1490 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1491 (setq vip-s-forward t
1492 vip-s-string
(vip-read-string (if vip-re-search
"RE-/" "/")))
1493 (if (string= vip-s-string
"")
1495 (setq vip-re-search
(not vip-re-search
))
1496 (message (format "Search mode changed to %s search."
1497 (if vip-re-search
"regular expression"
1499 (vip-search vip-s-string t val
)
1502 (move-marker vip-com-point
(mark))
1503 (vip-execute-com 'vip-search-next val com
))))))
1505 (defun vip-search-backward (arg)
1506 "Search a string backward. ARG is used to find the ARG's occurence
1507 of the string. Default is vanilla search. Search mode can be toggled by
1508 giving null search string."
1510 (let ((val (vip-P-val arg
)) (com (vip-getcom arg
)))
1511 (setq vip-s-forward nil
1512 vip-s-string
(vip-read-string (if vip-re-search
"RE-?" "?")))
1513 (if (string= vip-s-string
"")
1515 (setq vip-re-search
(not vip-re-search
))
1516 (message (format "Search mode changed to %s search."
1517 (if vip-re-search
"regular expression"
1519 (vip-search vip-s-string nil val
)
1522 (move-marker vip-com-point
(mark))
1523 (vip-execute-com 'vip-search-next val com
))))))
1525 (defun vip-search (string forward arg
&optional no-offset init-point
)
1526 "(STRING FORWARD COUNT &optional NO-OFFSET) Search COUNT's occurrence of
1527 STRING. Search will be forward if FORWARD, otherwise backward."
1528 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
))
1529 (null-arg (null (vip-P-val arg
))) (offset (not no-offset
))
1530 (case-fold-search vip-case-fold-search
)
1531 (start-point (or init-point
(point))))
1533 (condition-case conditions
1535 (if (and offset
(not (eobp))) (forward-char))
1538 (re-search-forward string nil nil val
)
1539 (re-search-backward string
))
1540 (search-forward string nil nil val
)
1541 (search-backward string
))
1542 (push-mark start-point
))
1544 (if (and null-arg vip-search-wrap-around
)
1546 (goto-char (point-min))
1547 (vip-search string forward
(cons 1 com
) t start-point
))
1548 (goto-char start-point
)
1549 (signal 'search-failed
(cdr conditions
)))))
1550 (condition-case conditions
1553 (re-search-backward string nil nil val
)
1554 (search-backward string nil nil val
))
1555 (push-mark start-point
))
1557 (if (and null-arg vip-search-wrap-around
)
1559 (goto-char (point-max))
1560 (vip-search string forward
(cons 1 com
) t start-point
))
1561 (goto-char start-point
)
1562 (signal 'search-failed
(cdr conditions
))))))))
1564 (defun vip-search-next (arg)
1565 "Repeat previous search."
1567 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1568 (if (null vip-s-string
) (error "No previous search string."))
1569 (vip-search vip-s-string vip-s-forward arg
)
1570 (if com
(vip-execute-com 'vip-search-next val com
))))
1572 (defun vip-search-Next (arg)
1573 "Repeat previous search in the reverse direction."
1575 (let ((val (vip-p-val arg
)) (com (vip-getcom arg
)))
1576 (if (null vip-s-string
) (error "No previous search string."))
1577 (vip-search vip-s-string
(not vip-s-forward
) arg
)
1578 (if com
(vip-execute-com 'vip-search-Next val com
))))
1581 ;; visiting and killing files, buffers
1583 (defun vip-switch-to-buffer ()
1584 "Switch to buffer in the current window."
1589 (format "switch to buffer \(%s\): "
1590 (buffer-name (other-buffer (current-buffer))))))
1591 (switch-to-buffer buffer
)
1592 (vip-change-mode-to-vi)))
1594 (defun vip-switch-to-buffer-other-window ()
1595 "Switch to buffer in another window."
1600 (format "Switch to buffer \(%s\): "
1601 (buffer-name (other-buffer (current-buffer))))))
1602 (switch-to-buffer-other-window buffer
)
1603 (vip-change-mode-to-vi)))
1605 (defun vip-kill-buffer ()
1608 (let (buffer buffer-name
)
1611 (format "Kill buffer \(%s\): "
1612 (buffer-name (current-buffer)))))
1614 (if (null buffer-name
)
1616 (get-buffer buffer-name
)))
1617 (if (null buffer
) (error "Buffer %s nonexistent." buffer-name
))
1618 (if (or (not (buffer-modified-p buffer
))
1619 (y-or-n-p "Buffer is modified, are you sure? "))
1620 (kill-buffer buffer
)
1621 (error "Buffer not killed."))))
1623 (defun vip-find-file ()
1624 "Visit file in the current window."
1627 (setq file
(read-file-name "visit file: "))
1628 (switch-to-buffer (find-file-noselect file
))
1629 (vip-change-mode-to-vi)))
1631 (defun vip-find-file-other-window ()
1632 "Visit file in another window."
1635 (setq file
(read-file-name "Visit file: "))
1636 (switch-to-buffer-other-window (find-file-noselect file
))
1637 (vip-change-mode-to-vi)))
1639 (defun vip-info-on-file ()
1640 "Give information of the file associated to the current buffer."
1642 (message "\"%s\" line %d of %d"
1643 (if (buffer-file-name) (buffer-file-name) "")
1644 (1+ (count-lines (point-min) (point)))
1645 (1+ (count-lines (point-min) (point-max)))))
1650 (defun vip-yank (text)
1651 "yank TEXT silently."
1653 (vip-push-mark-silent (point))
1655 (exchange-point-and-mark))
1656 (skip-chars-forward " \t"))
1658 (defun vip-put-back (arg)
1659 "Put back after point/below line."
1661 (let ((val (vip-p-val arg
))
1662 (text (if vip-use-register
1663 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1664 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1665 (get-register vip-use-register
))
1668 (if vip-use-register
1669 (let ((reg vip-use-register
))
1670 (setq vip-use-register nil
)
1671 (error "Nothing in register %c" reg
))
1673 (setq vip-use-register nil
)
1674 (if (vip-end-with-a-newline-p text
)
1677 (beginning-of-line))
1678 (if (and (not (eolp)) (not (eobp))) (forward-char)))
1679 (setq vip-d-com
(list 'vip-put-back val nil vip-use-register
))
1680 (vip-loop val
(vip-yank text
))))
1682 (defun vip-Put-back (arg)
1683 "Put back at point/above line."
1685 (let ((val (vip-p-val arg
))
1686 (text (if vip-use-register
1687 (if (and (<= ?
1 vip-use-register
) (<= vip-use-register ?
9))
1688 (current-kill (- vip-use-register ?
1) 'do-not-rotate
)
1689 (get-register vip-use-register
))
1692 (if vip-use-register
1693 (let ((reg vip-use-register
))
1694 (setq vip-use-register nil
)
1695 (error "Nothing in register %c" reg
))
1697 (setq vip-use-register nil
)
1698 (if (vip-end-with-a-newline-p text
) (beginning-of-line))
1699 (setq vip-d-com
(list 'vip-Put-back val nil vip-use-register
))
1700 (vip-loop val
(vip-yank text
))))
1702 (defun vip-delete-char (arg)
1705 (let ((val (vip-p-val arg
)))
1706 (setq vip-d-com
(list 'vip-delete-char val nil
))
1707 (if vip-use-register
1709 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1710 (vip-append-to-register
1711 (+ vip-use-register
32) (point) (- (point) val
) nil
)
1712 (copy-to-register vip-use-register
(point) (- (point) val
) nil
))
1713 (setq vip-use-register nil
)))
1714 (delete-char val t
)))
1716 (defun vip-delete-backward-char (arg)
1717 "Delete previous character."
1719 (let ((val (vip-p-val arg
)))
1720 (setq vip-d-com
(list 'vip-delete-backward-char val nil
))
1721 (if vip-use-register
1723 (if (and (<= ?A vip-use-register
) (<= vip-use-register ?Z
))
1724 (vip-append-to-register
1725 (+ vip-use-register
32) (point) (+ (point) val
) nil
)
1726 (copy-to-register vip-use-register
(point) (+ (point) val
) nil
))
1727 (setq vip-use-register nil
)))
1728 (delete-backward-char val t
)))
1733 (defun vip-join-lines (arg)
1734 "Join this line to next, if ARG is nil. Otherwise, join ARG lines"
1736 (let ((val (vip-P-val arg
)))
1737 (setq vip-d-com
(list 'vip-join-lines val nil
))
1738 (vip-loop (if (null val
) 1 (1- val
))
1744 (delete-region (point) (1- (point)))
1745 (fixup-whitespace)))))))
1748 ;; making small changes
1750 (defun vip-change (beg end
)
1752 (vip-read-string (format "%s => " (buffer-substring beg end
))))
1753 (vip-change-subr beg end
))
1755 (defun vip-change-subr (beg end
)
1756 (if vip-use-register
1758 (copy-to-register vip-use-register beg end nil
)
1759 (setq vip-use-register nil
)))
1760 (kill-region beg end
)
1761 (setq this-command
'vip-change
)
1767 (defun vip-query-replace ()
1768 "Query replace. If you supply null string as the string to be replaced,
1769 the query replace mode will toggle between string replace and regexp replace."
1772 (setq str
(vip-read-string
1773 (if vip-re-query-replace
"Query replace regexp: "
1774 "Query replace: ")))
1775 (if (string= str
"")
1777 (setq vip-re-query-replace
(not vip-re-query-replace
))
1778 (message "Query replace mode changed to %s."
1779 (if vip-re-query-replace
"regexp replace"
1781 (if vip-re-query-replace
1782 (query-replace-regexp
1784 (vip-read-string (format "Query replace regexp \"%s\" with: " str
)))
1787 (vip-read-string (format "Query replace \"%s\" with: " str
)))))))
1792 (defun vip-mark-beginning-of-buffer ()
1795 (goto-char (point-min))
1796 (exchange-point-and-mark)
1797 (message "mark set at the beginning of buffer"))
1799 (defun vip-mark-end-of-buffer ()
1802 (goto-char (point-max))
1803 (exchange-point-and-mark)
1804 (message "mark set at the end of buffer"))
1806 (defun vip-mark-point (char)
1808 (cond ((and (<= ?a char
) (<= char ?z
))
1809 (point-to-register (- char
(- ?a ?\C-a
))))
1810 ((= char ?
<) (vip-mark-beginning-of-buffer))
1811 ((= char ?
>) (vip-mark-end-of-buffer))
1812 ((= char ?.
) (push-mark))
1813 ((= char ?
,) (set-mark-command 1))
1814 ((= char ?D
) (mark-defun))
1817 (defun vip-goto-mark (arg)
1820 (let ((char (read-char)) (com (vip-getcom arg
)))
1821 (vip-goto-mark-subr char com nil
)))
1823 (defun vip-goto-mark-and-skip-white (arg)
1824 "Go to mark and skip to first non-white on line."
1826 (let ((char (read-char)) (com (vip-getCom arg
)))
1827 (vip-goto-mark-subr char com t
)))
1829 (defun vip-goto-mark-subr (char com skip-white
)
1830 (cond ((and (<= ?a char
) (<= char ?z
))
1831 (let ((buff (current-buffer)))
1832 (if com
(move-marker vip-com-point
(point)))
1833 (goto-char (register-to-point (- char
(- ?a ?\C-a
))))
1834 (if skip-white
(back-to-indentation))
1835 (vip-change-mode-to-vi)
1837 (if (equal buff
(current-buffer))
1838 (vip-execute-com (if skip-white
1839 'vip-goto-mark-and-skip-white
1842 (switch-to-buffer buff
)
1843 (goto-char vip-com-point
)
1844 (vip-change-mode-to-vi)
1846 ((and (not skip-white
) (= char ?
`))
1847 (if com
(move-marker vip-com-point
(point)))
1848 (exchange-point-and-mark)
1849 (if com
(vip-execute-com 'vip-goto-mark nil com
)))
1850 ((and skip-white
(= char ?
'))
1851 (if com
(move-marker vip-com-point
(point)))
1852 (exchange-point-and-mark)
1853 (back-to-indentation)
1854 (if com
(vip-execute-com 'vip-goto-mark-and-skip-white nil com
)))
1857 (defun vip-exchange-point-and-mark ()
1859 (exchange-point-and-mark)
1860 (back-to-indentation))
1862 (defun vip-keyboard-quit ()
1863 "Abort partially formed or running command."
1865 (setq vip-use-register nil
)
1868 (defun vip-ctl-c-equivalent (arg)
1869 "Emulate C-c in Emacs mode."
1871 (vip-ctl-key-equivalent "\C-c" arg
))
1873 (defun vip-ctl-x-equivalent (arg)
1874 "Emulate C-x in Emacs mode."
1876 (vip-ctl-key-equivalent "\C-x" arg
))
1878 (defun vip-ctl-key-equivalent (key arg
)
1879 (let ((char (read-char)))
1880 (if (and (<= ?A char
) (<= char ?Z
))
1881 (setq char
(- char
(- ?A ?\C-a
))))
1882 (setq prefix-arg arg
)
1884 (vip-get-editor-command
1885 vip-emacs-local-map global-map
1886 (format "%s%s" key
(char-to-string char
))))))
1889 ;; commands in insertion mode
1891 (defun vip-delete-backward-word (arg)
1892 "Delete previous word."
1897 (delete-region (point) (mark))))
1902 (set 'vip-mode-map
(make-keymap))
1904 (define-key vip-mode-map
"\C-a" 'beginning-of-line
)
1905 (define-key vip-mode-map
"\C-b" 'vip-scroll-back
)
1906 (define-key vip-mode-map
"\C-c" 'vip-ctl-c
)
1907 (define-key vip-mode-map
"\C-d" 'vip-scroll-up
)
1908 (define-key vip-mode-map
"\C-e" 'vip-scroll-up-one
)
1909 (define-key vip-mode-map
"\C-f" 'vip-scroll
)
1910 (define-key vip-mode-map
"\C-g" 'vip-keyboard-quit
)
1911 (define-key vip-mode-map
"\C-h" 'help-command
)
1912 (define-key vip-mode-map
"\C-m" 'vip-scroll-back
)
1913 (define-key vip-mode-map
"\C-n" 'vip-other-window
)
1914 (define-key vip-mode-map
"\C-o" 'vip-open-line-at-point
)
1915 (define-key vip-mode-map
"\C-u" 'vip-scroll-down
)
1916 (define-key vip-mode-map
"\C-x" 'vip-ctl-x
)
1917 (define-key vip-mode-map
"\C-y" 'vip-scroll-down-one
)
1918 (define-key vip-mode-map
"\C-z" 'vip-change-mode-to-emacs
)
1919 (define-key vip-mode-map
"\e" 'vip-ESC
)
1921 (define-key vip-mode-map
" " 'vip-scroll
)
1922 (define-key vip-mode-map
"!" 'vip-command-argument
)
1923 (define-key vip-mode-map
"\"" 'vip-command-argument
)
1924 (define-key vip-mode-map
"#" 'vip-command-argument
)
1925 (define-key vip-mode-map
"$" 'vip-goto-eol
)
1926 (define-key vip-mode-map
"%" 'vip-paren-match
)
1927 (define-key vip-mode-map
"&" 'vip-nil
)
1928 (define-key vip-mode-map
"'" 'vip-goto-mark-and-skip-white
)
1929 (define-key vip-mode-map
"(" 'vip-backward-sentence
)
1930 (define-key vip-mode-map
")" 'vip-forward-sentence
)
1931 (define-key vip-mode-map
"*" 'call-last-kbd-macro
)
1932 (define-key vip-mode-map
"+" 'vip-next-line-at-bol
)
1933 (define-key vip-mode-map
"," 'vip-repeat-find-opposite
)
1934 (define-key vip-mode-map
"-" 'vip-previous-line-at-bol
)
1935 (define-key vip-mode-map
"." 'vip-repeat
)
1936 (define-key vip-mode-map
"/" 'vip-search-forward
)
1938 (define-key vip-mode-map
"0" 'vip-beginning-of-line
)
1939 (define-key vip-mode-map
"1" 'vip-digit-argument
)
1940 (define-key vip-mode-map
"2" 'vip-digit-argument
)
1941 (define-key vip-mode-map
"3" 'vip-digit-argument
)
1942 (define-key vip-mode-map
"4" 'vip-digit-argument
)
1943 (define-key vip-mode-map
"5" 'vip-digit-argument
)
1944 (define-key vip-mode-map
"6" 'vip-digit-argument
)
1945 (define-key vip-mode-map
"7" 'vip-digit-argument
)
1946 (define-key vip-mode-map
"8" 'vip-digit-argument
)
1947 (define-key vip-mode-map
"9" 'vip-digit-argument
)
1949 (define-key vip-mode-map
":" 'vip-ex
)
1950 (define-key vip-mode-map
";" 'vip-repeat-find
)
1951 (define-key vip-mode-map
"<" 'vip-command-argument
)
1952 (define-key vip-mode-map
"=" 'vip-command-argument
)
1953 (define-key vip-mode-map
">" 'vip-command-argument
)
1954 (define-key vip-mode-map
"?" 'vip-search-backward
)
1955 (define-key vip-mode-map
"@" 'vip-nil
)
1957 (define-key vip-mode-map
"A" 'vip-Append
)
1958 (define-key vip-mode-map
"B" 'vip-backward-Word
)
1959 (define-key vip-mode-map
"C" 'vip-ctl-c-equivalent
)
1960 (define-key vip-mode-map
"D" 'vip-kill-line
)
1961 (define-key vip-mode-map
"E" 'vip-end-of-Word
)
1962 (define-key vip-mode-map
"F" 'vip-find-char-backward
)
1963 (define-key vip-mode-map
"G" 'vip-goto-line
)
1964 (define-key vip-mode-map
"H" 'vip-window-top
)
1965 (define-key vip-mode-map
"I" 'vip-Insert
)
1966 (define-key vip-mode-map
"J" 'vip-join-lines
)
1967 (define-key vip-mode-map
"K" 'vip-kill-buffer
)
1968 (define-key vip-mode-map
"L" 'vip-window-bottom
)
1969 (define-key vip-mode-map
"M" 'vip-window-middle
)
1970 (define-key vip-mode-map
"N" 'vip-search-Next
)
1971 (define-key vip-mode-map
"O" 'vip-Open-line
)
1972 (define-key vip-mode-map
"P" 'vip-Put-back
)
1973 (define-key vip-mode-map
"Q" 'vip-query-replace
)
1974 (define-key vip-mode-map
"R" 'vip-replace-string
)
1975 (define-key vip-mode-map
"S" 'vip-switch-to-buffer-other-window
)
1976 (define-key vip-mode-map
"T" 'vip-goto-char-backward
)
1977 (define-key vip-mode-map
"U" 'vip-nil
)
1978 (define-key vip-mode-map
"V" 'vip-find-file-other-window
)
1979 (define-key vip-mode-map
"W" 'vip-forward-Word
)
1980 (define-key vip-mode-map
"X" 'vip-ctl-x-equivalent
)
1981 (define-key vip-mode-map
"Y" 'vip-yank-line
)
1982 (define-key vip-mode-map
"ZZ" 'save-buffers-kill-emacs
)
1984 (define-key vip-mode-map
"[" 'vip-nil
)
1985 (define-key vip-mode-map
"\\" 'vip-escape-to-emacs
)
1986 (define-key vip-mode-map
"]" 'vip-nil
)
1987 (define-key vip-mode-map
"^" 'vip-bol-and-skip-white
)
1988 (define-key vip-mode-map
"_" 'vip-nil
)
1989 (define-key vip-mode-map
"`" 'vip-goto-mark
)
1991 (define-key vip-mode-map
"a" 'vip-append
)
1992 (define-key vip-mode-map
"b" 'vip-backward-word
)
1993 (define-key vip-mode-map
"c" 'vip-command-argument
)
1994 (define-key vip-mode-map
"d" 'vip-command-argument
)
1995 (define-key vip-mode-map
"e" 'vip-end-of-word
)
1996 (define-key vip-mode-map
"f" 'vip-find-char-forward
)
1997 (define-key vip-mode-map
"g" 'vip-info-on-file
)
1998 (define-key vip-mode-map
"h" 'vip-backward-char
)
1999 (define-key vip-mode-map
"i" 'vip-insert
)
2000 (define-key vip-mode-map
"j" 'vip-next-line
)
2001 (define-key vip-mode-map
"k" 'vip-previous-line
)
2002 (define-key vip-mode-map
"l" 'vip-forward-char
)
2003 (define-key vip-mode-map
"m" 'vip-mark-point
)
2004 (define-key vip-mode-map
"n" 'vip-search-next
)
2005 (define-key vip-mode-map
"o" 'vip-open-line
)
2006 (define-key vip-mode-map
"p" 'vip-put-back
)
2007 (define-key vip-mode-map
"q" 'vip-nil
)
2008 (define-key vip-mode-map
"r" 'vip-replace-char
)
2009 (define-key vip-mode-map
"s" 'vip-switch-to-buffer
)
2010 (define-key vip-mode-map
"t" 'vip-goto-char-forward
)
2011 (define-key vip-mode-map
"u" 'vip-undo
)
2012 (define-key vip-mode-map
"v" 'vip-find-file
)
2013 (define-key vip-mode-map
"w" 'vip-forward-word
)
2014 (define-key vip-mode-map
"x" 'vip-delete-char
)
2015 (define-key vip-mode-map
"y" 'vip-command-argument
)
2016 (define-key vip-mode-map
"zH" 'vip-line-to-top
)
2017 (define-key vip-mode-map
"zM" 'vip-line-to-middle
)
2018 (define-key vip-mode-map
"zL" 'vip-line-to-bottom
)
2019 (define-key vip-mode-map
"z\C-m" 'vip-line-to-top
)
2020 (define-key vip-mode-map
"z." 'vip-line-to-middle
)
2021 (define-key vip-mode-map
"z-" 'vip-line-to-bottom
)
2023 (define-key vip-mode-map
"{" 'vip-backward-paragraph
)
2024 (define-key vip-mode-map
"|" 'vip-goto-col
)
2025 (define-key vip-mode-map
"}" 'vip-forward-paragraph
)
2026 (define-key vip-mode-map
"~" 'vip-nil
)
2027 (define-key vip-mode-map
"\177" 'vip-delete-backward-char
)
2029 (define-key ctl-x-map
"3" 'vip-buffer-in-two-windows
)
2030 (define-key ctl-x-map
"\C-i" 'insert-file
)
2032 (defun vip-version ()
2034 (message "VIP version 3.5 of September 15, 1987"))
2037 ;; implement ex commands
2039 (defvar ex-token-type nil
2040 "type of token. if non-nil, gives type of address. if nil, it
2043 (defvar ex-token nil
2046 (defvar ex-addresses nil
2047 "list of ex addresses")
2052 (defvar ex-buffer nil
2053 "name of ex buffer")
2055 (defvar ex-count nil
2056 "value of ex count")
2058 (defvar ex-g-flag nil
2059 "flag for global command")
2061 (defvar ex-g-variant nil
2062 "if t global command is executed on lines not matching ex-g-pat")
2064 (defvar ex-reg-exp nil
2065 "save reg-exp used in substitute")
2068 "replace pattern for substitute")
2070 (defvar ex-g-pat nil
2071 "pattern for global command")
2073 (defvar ex-map
(make-sparse-keymap)
2074 "save commnads for mapped keys")
2079 (defvar ex-file nil
)
2081 (defvar ex-variant nil
)
2083 (defvar ex-offset nil
)
2085 (defvar ex-append nil
)
2091 (defun vip-looking-back (str)
2092 "returns t if looking back reg-exp STR before point."
2093 (and (save-excursion (re-search-backward str nil t
))
2094 (= (point) (match-end 0))))
2096 (defun vip-check-sub (str)
2097 "check if ex-token is an initial segment of STR"
2098 (let ((length (length ex-token
)))
2099 (if (and (<= length
(length str
))
2100 (string= ex-token
(substring str
0 length
)))
2102 (setq ex-token-type
"non-command"))))
2104 (defun vip-get-ex-com-subr ()
2105 "get a complete ex command"
2107 (re-search-forward "[a-z][a-z]*")
2108 (setq ex-token-type
"command")
2109 (setq ex-token
(buffer-substring (point) (mark)))
2110 (exchange-point-and-mark)
2111 (cond ((looking-at "a")
2112 (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
2113 ((looking-at "ar") (vip-check-sub "args"))
2114 (t (vip-check-sub "append"))))
2115 ((looking-at "[bh]") (setq ex-token-type
"non-command"))
2117 (if (looking-at "co") (vip-check-sub "copy")
2118 (vip-check-sub "change")))
2119 ((looking-at "d") (vip-check-sub "delete"))
2121 (if (looking-at "ex") (vip-check-sub "ex")
2122 (vip-check-sub "edit")))
2123 ((looking-at "f") (vip-check-sub "file"))
2124 ((looking-at "g") (vip-check-sub "global"))
2125 ((looking-at "i") (vip-check-sub "insert"))
2126 ((looking-at "j") (vip-check-sub "join"))
2127 ((looking-at "l") (vip-check-sub "list"))
2129 (cond ((looking-at "map") (vip-check-sub "map"))
2130 ((looking-at "mar") (vip-check-sub "mark"))
2131 (t (vip-check-sub "move"))))
2133 (if (looking-at "nu") (vip-check-sub "number")
2134 (vip-check-sub "next")))
2135 ((looking-at "o") (vip-check-sub "open"))
2137 (cond ((looking-at "pre") (vip-check-sub "preserve"))
2138 ((looking-at "pu") (vip-check-sub "put"))
2139 (t (vip-check-sub "print"))))
2140 ((looking-at "q") (vip-check-sub "quit"))
2142 (cond ((looking-at "rec") (vip-check-sub "recover"))
2143 ((looking-at "rew") (vip-check-sub "rewind"))
2144 (t (vip-check-sub "read"))))
2146 (cond ((looking-at "se") (vip-check-sub "set"))
2147 ((looking-at "sh") (vip-check-sub "shell"))
2148 ((looking-at "so") (vip-check-sub "source"))
2149 ((looking-at "st") (vip-check-sub "stop"))
2150 (t (vip-check-sub "substitute"))))
2152 (if (looking-at "ta") (vip-check-sub "tag")
2153 (vip-check-sub "t")))
2155 (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
2156 ((looking-at "unm") (vip-check-sub "unmap"))
2157 (t (vip-check-sub "undo"))))
2159 (cond ((looking-at "ve") (vip-check-sub "version"))
2160 ((looking-at "vi") (vip-check-sub "visual"))
2161 (t (vip-check-sub "v"))))
2163 (if (looking-at "wq") (vip-check-sub "wq")
2164 (vip-check-sub "write")))
2165 ((looking-at "x") (vip-check-sub "xit"))
2166 ((looking-at "y") (vip-check-sub "yank"))
2167 ((looking-at "z") (vip-check-sub "z")))
2168 (exchange-point-and-mark))
2170 (defun vip-get-ex-token ()
2171 "get an ex-token which is either an address or a command.
2172 a token has type \(command, address, end-mark\) and value."
2173 (save-window-excursion
2174 (set-buffer " *ex-working-space*")
2175 (skip-chars-forward " \t")
2176 (cond ((looking-at "[k#]")
2177 (setq ex-token-type
"command")
2178 (setq ex-token
(char-to-string (following-char)))
2180 ((looking-at "[a-z]") (vip-get-ex-com-subr))
2183 (setq ex-token-type
"dot"))
2184 ((looking-at "[0-9]")
2186 (re-search-forward "[0-9]*")
2188 (cond ((string= ex-token-type
"plus") "add-number")
2189 ((string= ex-token-type
"minus") "sub-number")
2191 (setq ex-token
(string-to-int (buffer-substring (point) (mark)))))
2194 (setq ex-token-type
"end"))
2197 (setq ex-token-type
"whole"))
2199 (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
2203 (setq ex-token-type
"plus"))
2204 ((looking-at "+[0-9]")
2206 (setq ex-token-type
"plus"))
2208 (error "Badly formed address"))))
2210 (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
2214 (setq ex-token-type
"minus"))
2215 ((looking-at "-[0-9]")
2217 (setq ex-token-type
"minus"))
2219 (error "Badly formed address"))))
2224 (while (and (not (eolp)) cont
)
2225 ;;(re-search-forward "[^/]*/")
2226 (re-search-forward "[^/]*\\(/\\|\n\\)")
2227 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2230 (setq ex-token
(buffer-substring (point) (mark)))
2231 (if (looking-at "/") (forward-char 1))
2232 (setq ex-token-type
"search-forward"))
2237 (while (and (not (eolp)) cont
)
2238 ;;(re-search-forward "[^\\?]*\\?")
2239 (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
2240 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
2243 (if (not (looking-at "\n")) (forward-char 1))))
2244 (setq ex-token-type
"search-backward")
2245 (setq ex-token
(buffer-substring (1- (point)) (mark))))
2248 (setq ex-token-type
"comma"))
2251 (setq ex-token-type
"semi-colon"))
2252 ((looking-at "[!=><&~]")
2253 (setq ex-token-type
"command")
2254 (setq ex-token
(char-to-string (following-char)))
2257 (setq ex-token-type
"goto-mark")
2259 (cond ((looking-at "'") (setq ex-token nil
))
2260 ((looking-at "[a-z]") (setq ex-token
(following-char)))
2261 (t (error "Marks are ' and a-z")))
2264 (setq ex-token-type
"end-mark")
2265 (setq ex-token
"goto"))
2267 (error "illegal token")))))
2269 (defun vip-ex (&optional string
)
2270 "ex commands within VIP."
2275 (let ((com-str (or string
(vip-read-string ":")))
2276 (address nil
) (cont t
) (dot (point)))
2277 (save-window-excursion
2278 (set-buffer (get-buffer-create " *ex-working-space*"))
2279 (delete-region (point-min) (point-max))
2280 (insert com-str
"\n")
2281 (goto-char (point-min)))
2282 (setq ex-token-type
"")
2283 (setq ex-addresses nil
)
2286 (cond ((or (string= ex-token-type
"command")
2287 (string= ex-token-type
"end-mark"))
2288 (if address
(setq ex-addresses
(cons address ex-addresses
)))
2289 (cond ((string= ex-token
"global")
2292 ((string= ex-token
"v")
2296 (vip-execute-ex-command)
2297 (save-window-excursion
2298 (set-buffer " *ex-working-space*")
2299 (skip-chars-forward " \t")
2300 (cond ((looking-at "|")
2304 (t (error "Extra character at end of a command")))))))
2305 ((string= ex-token-type
"non-command")
2306 (error (format "%s: Not an editor command" ex-token
)))
2307 ((string= ex-token-type
"whole")
2309 (cons (point-max) (cons (point-min) ex-addresses
))))
2310 ((string= ex-token-type
"comma")
2312 (cons (if (null address
) (point) address
) ex-addresses
)))
2313 ((string= ex-token-type
"semi-colon")
2314 (if address
(setq dot address
))
2316 (cons (if (null address
) (point) address
) ex-addresses
)))
2317 (t (let ((ans (vip-get-ex-address-subr address dot
)))
2318 (if ans
(setq address ans
))))))))
2320 (defun vip-get-ex-pat ()
2321 "get a regular expression and set ex-variant if found"
2322 (save-window-excursion
2323 (set-buffer " *ex-working-space*")
2324 (skip-chars-forward " \t")
2325 (if (looking-at "!")
2327 (setq ex-g-variant
(not ex-g-variant
)
2328 ex-g-flag
(not ex-g-flag
))
2330 (skip-chars-forward " \t")))
2331 (if (looking-at "/")
2336 (while (and (not (eolp)) cont
)
2337 (re-search-forward "[^/]*\\(/\\|\n\\)")
2338 ;;(re-search-forward "[^/]*/")
2339 (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
2342 (if (= (mark) (point)) ""
2343 (buffer-substring (1- (point)) (mark))))
2345 (setq ex-token nil
))))
2347 (defun vip-get-ex-command ()
2349 (save-window-excursion
2350 (set-buffer " *ex-working-space*")
2351 (if (looking-at "/") (forward-char 1))
2352 (skip-chars-forward " \t")
2353 (cond ((looking-at "[a-z]")
2354 (vip-get-ex-com-subr)
2355 (if (string= ex-token-type
"non-command")
2356 (error "%s: not an editor command" ex-token
)))
2357 ((looking-at "[!=><&~]")
2358 (setq ex-token
(char-to-string (following-char)))
2360 (t (error "Could not find an ex command")))))
2362 (defun vip-get-ex-opt-gc ()
2363 "get an ex option g or c"
2364 (save-window-excursion
2365 (set-buffer " *ex-working-space*")
2366 (if (looking-at "/") (forward-char 1))
2367 (skip-chars-forward " \t")
2368 (cond ((looking-at "g")
2378 (defun vip-default-ex-addresses (&optional whole-flag
)
2379 "compute default addresses. whole-flag means whole buffer."
2380 (cond ((null ex-addresses
)
2383 (cons (point-max) (cons (point-min) nil
))
2384 (cons (point) (cons (point) nil
)))))
2385 ((null (cdr ex-addresses
))
2387 (cons (car ex-addresses
) ex-addresses
)))))
2389 (defun vip-get-ex-address ()
2390 "get an ex-address as a marker and set ex-flag if a flag is found"
2391 (let ((address (point-marker)) (cont t
))
2396 (cond ((string= ex-token-type
"command")
2397 (if (or (string= ex-token
"print") (string= ex-token
"list")
2398 (string= ex-token
"#"))
2402 (error "address expected")))
2403 ((string= ex-token-type
"end-mark")
2405 ((string= ex-token-type
"whole")
2406 (error "a trailing address is expected"))
2407 ((string= ex-token-type
"comma")
2408 (error "Extra characters after an address"))
2409 (t (let ((ans (vip-get-ex-address-subr address
(point-marker))))
2410 (if ans
(setq address ans
))))))
2413 (defun vip-get-ex-address-subr (old-address dot
)
2414 "returns an address as a point"
2415 (let ((address nil
))
2416 (if (null old-address
) (setq old-address dot
))
2417 (cond ((string= ex-token-type
"dot")
2419 ((string= ex-token-type
"add-number")
2421 (goto-char old-address
)
2422 (forward-line (if (= old-address
0) (1- ex-token
) ex-token
))
2423 (setq address
(point-marker))))
2424 ((string= ex-token-type
"sub-number")
2426 (goto-char old-address
)
2427 (forward-line (- ex-token
))
2428 (setq address
(point-marker))))
2429 ((string= ex-token-type
"abs-number")
2431 (goto-char (point-min))
2432 (if (= ex-token
0) (setq address
0)
2433 (forward-line (1- ex-token
))
2434 (setq address
(point-marker)))))
2435 ((string= ex-token-type
"end")
2436 (setq address
(point-max-marker)))
2437 ((string= ex-token-type
"plus") t
);; do nothing
2438 ((string= ex-token-type
"minus") t
);; do nothing
2439 ((string= ex-token-type
"search-forward")
2441 (ex-search-address t
)
2442 (setq address
(point-marker))))
2443 ((string= ex-token-type
"search-backward")
2445 (ex-search-address nil
)
2446 (setq address
(point-marker))))
2447 ((string= ex-token-type
"goto-mark")
2450 (exchange-point-and-mark)
2451 (goto-char (register-to-point (- ex-token
(- ?a ?\C-a
)))))
2452 (setq address
(point-marker)))))
2455 (defun ex-search-address (forward)
2456 "search pattern and set address"
2457 (if (string= ex-token
"")
2458 (if (null vip-s-string
) (error "No previous search string")
2459 (setq ex-token vip-s-string
))
2460 (setq vip-s-string ex-token
))
2464 (re-search-forward ex-token
))
2466 (re-search-backward ex-token
)))
2468 (defun vip-get-ex-buffer ()
2469 "get a buffer name and set ex-count and ex-flag if found"
2470 (setq ex-buffer nil
)
2473 (save-window-excursion
2474 (set-buffer " *ex-working-space*")
2475 (skip-chars-forward " \t")
2476 (if (looking-at "[a-zA-Z]")
2478 (setq ex-buffer
(following-char))
2480 (skip-chars-forward " \t")))
2481 (if (looking-at "[0-9]")
2484 (re-search-forward "[0-9][0-9]*")
2485 (setq ex-count
(string-to-int (buffer-substring (point) (mark))))
2486 (skip-chars-forward " \t")))
2487 (if (looking-at "[pl#]")
2491 (if (not (looking-at "[\n|]"))
2492 (error "Illegal extra characters"))))
2494 (defun vip-get-ex-count ()
2495 (setq ex-variant nil
2498 (save-window-excursion
2499 (set-buffer " *ex-working-space*")
2500 (skip-chars-forward " \t")
2501 (if (looking-at "!")
2505 (skip-chars-forward " \t")
2506 (if (looking-at "[0-9]")
2509 (re-search-forward "[0-9][0-9]*")
2510 (setq ex-count
(string-to-int (buffer-substring (point) (mark))))
2511 (skip-chars-forward " \t")))
2512 (if (looking-at "[pl#]")
2516 (if (not (looking-at "[\n|]"))
2517 (error "Illegal extra characters"))))
2519 (defun vip-get-ex-file ()
2520 "get a file name and set ex-variant, ex-append and ex-offset if found"
2525 (save-window-excursion
2526 (set-buffer " *ex-working-space*")
2527 (skip-chars-forward " \t")
2528 (if (looking-at "!")
2532 (skip-chars-forward " \t")))
2533 (if (looking-at ">>")
2538 (skip-chars-forward " \t")))
2539 (if (looking-at "+")
2543 (re-search-forward "[ \t\n]")
2545 (setq ex-offset
(buffer-substring (point) (mark)))
2547 (skip-chars-forward " \t")))
2549 (re-search-forward "[ \t\n]")
2551 (setq ex-file
(buffer-substring (point) (mark)))))
2553 (defun vip-execute-ex-command ()
2554 "execute ex command using the value of addresses."
2555 (cond ((string= ex-token
"goto") (ex-goto))
2556 ((string= ex-token
"copy") (ex-copy nil
))
2557 ((string= ex-token
"delete") (ex-delete))
2558 ((string= ex-token
"edit") (ex-edit))
2559 ((string= ex-token
"file") (vip-info-on-file))
2560 ;((string= ex-token "global") (ex-global nil))
2561 ((string= ex-token
"join") (ex-line "join"))
2562 ((string= ex-token
"k") (ex-mark))
2563 ((string= ex-token
"mark") (ex-mark))
2564 ((string= ex-token
"map") (ex-map))
2565 ((string= ex-token
"move") (ex-copy t
))
2566 ((string= ex-token
"put") (ex-put))
2567 ((string= ex-token
"quit") (ex-quit))
2568 ((string= ex-token
"read") (ex-read))
2569 ((string= ex-token
"set") (ex-set))
2570 ((string= ex-token
"shell") (ex-shell))
2571 ((string= ex-token
"substitute") (ex-substitute))
2572 ((string= ex-token
"stop") (suspend-emacs))
2573 ((string= ex-token
"t") (ex-copy nil
))
2574 ((string= ex-token
"tag") (ex-tag))
2575 ((string= ex-token
"undo") (vip-undo))
2576 ((string= ex-token
"unmap") (ex-unmap))
2577 ;((string= ex-token "v") (ex-global t))
2578 ((string= ex-token
"version") (vip-version))
2579 ((string= ex-token
"visual") (ex-edit))
2580 ((string= ex-token
"write") (ex-write nil
))
2581 ((string= ex-token
"wq") (ex-write t
))
2582 ((string= ex-token
"yank") (ex-yank))
2583 ((string= ex-token
"!") (ex-command))
2584 ((string= ex-token
"=") (ex-line-no))
2585 ((string= ex-token
">") (ex-line "right"))
2586 ((string= ex-token
"<") (ex-line "left"))
2587 ((string= ex-token
"&") (ex-substitute t
))
2588 ((string= ex-token
"~") (ex-substitute t t
))
2589 ((or (string= ex-token
"append")
2590 (string= ex-token
"args")
2591 (string= ex-token
"change")
2592 (string= ex-token
"insert")
2593 (string= ex-token
"open")
2595 (error (format "%s: no such command from VIP" ex-token
)))
2596 ((or (string= ex-token
"abbreviate")
2597 (string= ex-token
"list")
2598 (string= ex-token
"next")
2599 (string= ex-token
"print")
2600 (string= ex-token
"preserve")
2601 (string= ex-token
"recover")
2602 (string= ex-token
"rewind")
2603 (string= ex-token
"source")
2604 (string= ex-token
"unabbreviate")
2605 (string= ex-token
"xit")
2606 (string= ex-token
"z")
2608 (error (format "%s: not implemented in VIP" ex-token
)))
2609 (t (error (format "%s: Not an editor command" ex-token
)))))
2613 (if (null ex-addresses
)
2614 (setq ex-addresses
(cons (point) nil
)))
2616 (goto-char (car ex-addresses
))
2617 (beginning-of-line))
2619 (defun ex-copy (del-flag)
2620 "ex copy and move command. DEL-FLAG means delete."
2621 (vip-default-ex-addresses)
2622 (let ((address (vip-get-ex-address))
2623 (end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2627 (vip-enlarge-region (mark) (point))
2628 (if del-flag
(kill-region (point) (mark))
2629 (copy-region-as-kill (point) (mark)))
2632 (with-output-to-temp-buffer "*copy text*"
2634 (if (or del-flag ex-g-flag ex-g-variant
)
2636 (buffer-substring (point) (mark)))))
2639 (vip-read-string "[Hit return to continue] ")
2640 (save-excursion (kill-buffer "*copy text*")))
2642 (save-excursion (kill-buffer "*copy text*"))
2643 (signal 'quit nil
))))))
2645 (goto-char (point-min))
2648 (insert (current-kill 0))))
2652 (vip-default-ex-addresses)
2654 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2655 (if (> beg end
) (error "First address exceeds second"))
2657 (vip-enlarge-region beg end
)
2658 (exchange-point-and-mark)
2662 (forward-line (1- ex-count
)))
2664 (vip-enlarge-region (point) (mark))
2666 ;; show text to be deleted and ask for confirmation
2668 (with-output-to-temp-buffer " *delete text*"
2669 (princ (buffer-substring (point) (mark))))
2670 (condition-case conditions
2671 (vip-read-string "[Hit return to continue] ")
2673 (save-excursion (kill-buffer " *delete text*"))
2675 (save-excursion (kill-buffer " *delete text*")))
2677 (if (and (<= ?A ex-buffer
) (<= ex-buffer ?Z
))
2678 (vip-append-to-register
2679 (+ ex-buffer
32) (point) (mark) nil
)
2680 (copy-to-register ex-buffer
(point) (mark) nil
)))
2681 (delete-region (point) (mark))))))
2686 (if (and (not ex-variant
) (buffer-modified-p) buffer-file-name
)
2687 (error "No write since last change \(:e! overrides\)"))
2688 (vip-change-mode-to-emacs)
2690 (find-file-noselect (concat default-directory ex-file
)))
2691 (vip-change-mode-to-vi)
2692 (goto-char (point-min))
2695 (save-window-excursion
2696 (set-buffer " *ex-working-space*")
2697 (delete-region (point-min) (point-max))
2698 (insert ex-offset
"\n")
2699 (goto-char (point-min)))
2700 (goto-char (vip-get-ex-address))
2701 (beginning-of-line))))
2703 (defun ex-global (variant)
2705 (if (or ex-g-flag ex-g-variant
)
2706 (error "Global within global not allowed")
2714 (error "Missing regular expression for global command"))
2715 (if (string= ex-token
"")
2716 (if (null vip-s-string
) (error "No previous search string")
2717 (setq ex-g-pat vip-s-string
))
2718 (setq ex-g-pat ex-token
2719 vip-s-string ex-token
))
2720 (if (null ex-addresses
)
2721 (setq ex-addresses
(list (point-max) (point-min))))
2722 (let ((marks nil
) (mark-count 0)
2723 com-str
(end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
2724 (if (> beg end
) (error "First address exceeds second"))
2726 (vip-enlarge-region beg end
)
2727 (exchange-point-and-mark)
2728 (let ((cont t
) (limit (point-marker)))
2729 (exchange-point-and-mark)
2730 ;; skip the last line if empty
2732 (if (and (eobp) (not (bobp))) (backward-char 1))
2733 (while (and cont
(not (bobp)) (>= (point) limit
))
2737 (let ((found (re-search-backward ex-g-pat
(mark) t
)))
2738 (if (or (and ex-g-flag found
)
2739 (and ex-g-variant
(not found
)))
2742 (setq mark-count
(1+ mark-count
))
2743 (setq marks
(cons (point-marker) marks
)))))
2745 (if (bobp) (setq cont nil
)
2748 (save-window-excursion
2749 (set-buffer " *ex-working-space*")
2750 (setq com-str
(buffer-substring (1+ (point)) (1- (point-max)))))
2752 (goto-char (car marks
))
2753 ; report progress of execution on a slow machine.
2754 ;(message "Executing global command...")
2755 ;(if (zerop (% mark-count 10))
2756 ;(message "Executing global command...%d" mark-count))
2758 (setq mark-count
(1- mark-count
))
2759 (setq marks
(cdr marks
)))))
2760 ;(message "Executing global command...done")))
2762 (defun ex-line (com)
2763 "ex line commands. COM is join, shift-right or shift-left."
2764 (vip-default-ex-addresses)
2766 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))) point
)
2767 (if (> beg end
) (error "First address exceeds second"))
2769 (vip-enlarge-region beg end
)
2770 (exchange-point-and-mark)
2774 (forward-line ex-count
)))
2776 ;; show text to be joined and ask for confirmation
2778 (with-output-to-temp-buffer " *text*"
2779 (princ (buffer-substring (point) (mark))))
2780 (condition-case conditions
2782 (vip-read-string "[Hit return to continue] ")
2783 (ex-line-subr com
(point) (mark)))
2786 (save-excursion (kill-buffer " *text*")))
2787 (ex-line-subr com
(point) (mark)))
2788 (setq point
(point)))
2789 (goto-char (1- point
))
2790 (beginning-of-line)))
2792 (defun ex-line-subr (com beg end
)
2793 (cond ((string= com
"join")
2794 (goto-char (min beg end
))
2795 (while (and (not (eobp)) (< (point) (max beg end
)))
2797 (if (and (<= (point) (max beg end
)) (not (eobp)))
2800 (delete-region (point) (1- (point)))
2801 (if (not ex-variant
) (fixup-whitespace))))))
2802 ((or (string= com
"right") (string= com
"left"))
2804 (min beg end
) (max beg end
)
2805 (if (string= com
"right") vip-shift-width
(- vip-shift-width
)))
2806 (goto-char (max beg end
))
2813 (if (null ex-addresses
)
2815 (cons (point) nil
)))
2816 (save-window-excursion
2817 (set-buffer " *ex-working-space*")
2818 (skip-chars-forward " \t")
2819 (if (looking-at "[a-z]")
2821 (setq char
(following-char))
2823 (skip-chars-forward " \t")
2824 (if (not (looking-at "[\n|]"))
2825 (error "Extra characters at end of \"k\" command")))
2826 (if (looking-at "[\n|]")
2827 (error "\"k\" requires a following letter")
2828 (error "Mark must specify a letter"))))
2830 (goto-char (car ex-addresses
))
2831 (point-to-register (- char
(- ?a ?\C-a
))))))
2836 (save-window-excursion
2837 (set-buffer " *ex-working-space*")
2838 (skip-chars-forward " \t")
2839 (setq char
(char-to-string (following-char)))
2841 (skip-chars-forward " \t")
2842 (if (looking-at "[\n|]") (error "Missing rhs"))
2846 (setq string
(buffer-substring (mark) (point))))
2847 (if (not (lookup-key ex-map char
))
2848 (define-key ex-map char
2849 (or (lookup-key vip-mode-map char
) 'vip-nil
)))
2850 (define-key vip-mode-map char
2856 (list 'execute-kbd-macro string
'count
))))))))
2861 (save-window-excursion
2862 (set-buffer " *ex-working-space*")
2863 (skip-chars-forward " \t")
2864 (setq char
(char-to-string (following-char)))
2866 (skip-chars-forward " \t")
2867 (if (not (looking-at "[\n|]")) (error "Macro must be a character")))
2868 (if (not (lookup-key ex-map char
))
2869 (error "That macro wasn't mapped"))
2870 (define-key vip-mode-map char
(lookup-key ex-map char
))
2871 (define-key ex-map char nil
)))
2875 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
))))
2877 (setq vip-use-register ex-buffer
)
2879 (if (= point
0) (vip-Put-back 1) (vip-put-back 1))))
2884 (save-window-excursion
2885 (set-buffer " *ex-working-space*")
2886 (skip-chars-forward " \t")
2887 (setq char
(following-char)))
2888 (if (= char ?
!) (kill-emacs t
) (save-buffers-kill-emacs))))
2892 (let ((point (if (null ex-addresses
) (point) (car ex-addresses
)))
2893 (variant nil
) command file
)
2895 (if (not (= point
0)) (next-line 1))
2897 (save-window-excursion
2898 (set-buffer " *ex-working-space*")
2899 (skip-chars-forward " \t")
2900 (if (looking-at "!")
2904 (skip-chars-forward " \t")
2907 (setq command
(buffer-substring (mark) (point))))
2909 (re-search-forward "[ \t\n]")
2911 (setq file
(buffer-substring (point) (mark)))))
2913 (shell-command command t
)
2914 (insert-file file
))))
2918 (read-variable "Variable: ")
2919 (eval (read-minibuffer "Value: ")))))
2923 (vip-change-mode-to-emacs)
2926 (defun ex-substitute (&optional repeat r-flag
)
2927 "ex substitute. if REPEAT use previous reg-exp which is ex-reg-exp or
2929 (let (pat repl
(opt-g nil
) (opt-c nil
) (matched-pos nil
))
2930 (if repeat
(setq ex-token nil
) (vip-get-ex-pat))
2932 (setq pat
(if r-flag vip-s-string ex-reg-exp
)
2934 (setq pat
(if (string= ex-token
"") vip-s-string ex-token
))
2935 (setq vip-s-string pat
2943 (while (vip-get-ex-opt-gc)
2944 (if (string= ex-token
"g") (setq opt-g t
) (setq opt-c t
)))
2948 (if ex-addresses
(goto-char (car ex-addresses
)))
2950 (forward-line (1- ex-count
))
2951 (setq ex-addresses
(cons (point) (cons (mark) nil
))))
2952 (if (null ex-addresses
)
2953 (setq ex-addresses
(cons (point) (cons (point) nil
)))
2954 (if (null (cdr ex-addresses
))
2955 (setq ex-addresses
(cons (car ex-addresses
) ex-addresses
)))))
2957 (let ((beg (car ex-addresses
)) (end (car (cdr ex-addresses
)))
2960 (vip-enlarge-region beg end
)
2961 (let ((limit (save-excursion
2962 (goto-char (max (point) (mark)))
2964 (goto-char (min (point) (mark)))
2965 (while (< (point) limit
)
2967 (setq eol-mark
(point-marker))
2971 (while (and (not (eolp))
2972 (re-search-forward pat eol-mark t
))
2973 (if (or (not opt-c
) (y-or-n-p "Replace? "))
2975 (setq matched-pos
(point))
2976 (replace-match repl
))))
2979 (if (and (re-search-forward pat eol-mark t
)
2980 (or (not opt-c
) (y-or-n-p "Replace? ")))
2982 (setq matched-pos
(point))
2983 (replace-match repl
)))
2986 (if matched-pos
(goto-char matched-pos
))
2988 (if opt-c
(message "done"))))
2993 (save-window-excursion
2994 (set-buffer " *ex-working-space*")
2995 (skip-chars-forward " \t")
2997 (skip-chars-forward "^ |\t\n")
2998 (setq tag
(buffer-substring (mark) (point))))
2999 (if (not (string= tag
"")) (setq ex-tag tag
))
3000 (vip-change-mode-to-emacs)
3001 (condition-case conditions
3003 (if (string= tag
"")
3005 (find-tag-other-window ex-tag
))
3006 (vip-change-mode-to-vi))
3008 (vip-change-mode-to-vi)
3009 (vip-message-conditions conditions
)))))
3011 (defun ex-write (q-flag)
3013 (vip-default-ex-addresses t
)
3015 (if (string= ex-file
"")
3017 (if (null buffer-file-name
)
3018 (error "No file associated with this buffer"))
3019 (setq ex-file buffer-file-name
))
3020 (setq ex-file
(expand-file-name ex-file
)))
3021 (if (and (not (string= ex-file
(buffer-file-name)))
3022 (file-exists-p ex-file
)
3024 (error (format "\"%s\" File exists - use w! to override" ex-file
)))
3025 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3026 (if (> beg end
) (error "First address exceeds second"))
3028 (vip-enlarge-region beg end
)
3029 (write-region (point) (mark) ex-file ex-append t
)))
3030 (if (null buffer-file-name
) (setq buffer-file-name ex-file
))
3031 (if q-flag
(save-buffers-kill-emacs)))
3035 (vip-default-ex-addresses)
3037 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3038 (if (> beg end
) (error "First address exceeds second"))
3040 (vip-enlarge-region beg end
)
3041 (exchange-point-and-mark)
3042 (if (or ex-g-flag ex-g-variant
) (error "Can't yank within global"))
3046 (forward-line (1- ex-count
)))
3048 (vip-enlarge-region (point) (mark))
3049 (if ex-flag
(error "Extra chacters at end of command"))
3051 (copy-to-register ex-buffer
(point) (mark) nil
))
3052 (copy-region-as-kill (point) (mark)))))
3054 (defun ex-command ()
3055 "execute shell command"
3057 (save-window-excursion
3058 (set-buffer " *ex-working-space*")
3059 (skip-chars-forward " \t")
3062 (setq command
(buffer-substring (mark) (point))))
3063 (if (null ex-addresses
)
3064 (shell-command command
)
3065 (let ((end (car ex-addresses
)) (beg (car (cdr ex-addresses
))))
3066 (if (null beg
) (setq beg end
))
3070 (vip-enlarge-region (point) (mark))
3071 (shell-command-on-region (point) (mark) command t
))
3074 (defun ex-line-no ()
3079 (if (null ex-addresses
) (point-max) (car ex-addresses
))))))
3081 (if (file-exists-p "~/.vip") (load "~/.vip"))
3083 ;;; vip.el ends here