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