Mercurial > emacs
comparison lisp/emulation/vi.el @ 49598:0d8b17d428b5
Trailing whitepace deleted.
author | Juanma Barranquero <lekktu@gmail.com> |
---|---|
date | Tue, 04 Feb 2003 13:24:35 +0000 |
parents | 853c3674f20a |
children | 695cf19ef79e d7ddb3e565de |
comparison
equal
deleted
inserted
replaced
49597:e88404e8f2cf | 49598:0d8b17d428b5 |
---|---|
28 ; (if (not (or (eq major-mode 'Info-mode) | 28 ; (if (not (or (eq major-mode 'Info-mode) |
29 ; (eq major-mode 'vi-mode))) | 29 ; (eq major-mode 'vi-mode))) |
30 ; (vi-mode)))))) | 30 ; (vi-mode)))))) |
31 ; 3) In your .emacs file you can define the command "vi-mode" to be "autoload" | 31 ; 3) In your .emacs file you can define the command "vi-mode" to be "autoload" |
32 ; or you can execute the "load" command to load "vi" directly. | 32 ; or you can execute the "load" command to load "vi" directly. |
33 ; 4) Read the comments for command "vi-mode" before you start using it. | 33 ; 4) Read the comments for command "vi-mode" before you start using it. |
34 ; | 34 ; |
35 ; COULD DO | 35 ; COULD DO |
36 ; 1). A general 'define-operator' function to replace current hack | 36 ; 1). A general 'define-operator' function to replace current hack |
37 ; 2). In operator handling, should allow other point moving Emacs commands | 37 ; 2). In operator handling, should allow other point moving Emacs commands |
38 ; (such as ESC <, ESC >) to be used as arguments. | 38 ; (such as ESC <, ESC >) to be used as arguments. |
151 | 151 |
152 (define-key vi-com-map "\e" 'ESC-prefix); C-[ (ESC) | 152 (define-key vi-com-map "\e" 'ESC-prefix); C-[ (ESC) |
153 (define-key vi-com-map "\C-\\" 'vi-unimplemented) | 153 (define-key vi-com-map "\C-\\" 'vi-unimplemented) |
154 (define-key vi-com-map "\C-]" 'find-tag) | 154 (define-key vi-com-map "\C-]" 'find-tag) |
155 (define-key vi-com-map "\C-^" 'vi-locate-def) ; extension | 155 (define-key vi-com-map "\C-^" 'vi-locate-def) ; extension |
156 (define-key vi-com-map "\C-_" 'vi-undefined) | 156 (define-key vi-com-map "\C-_" 'vi-undefined) |
157 | 157 |
158 (define-key vi-com-map " " 'forward-char) | 158 (define-key vi-com-map " " 'forward-char) |
159 (define-key vi-com-map "!" 'vi-operator) | 159 (define-key vi-com-map "!" 'vi-operator) |
160 (define-key vi-com-map "\"" 'vi-char-argument) | 160 (define-key vi-com-map "\"" 'vi-char-argument) |
161 (define-key vi-com-map "#" 'universal-argument) ; extension | 161 (define-key vi-com-map "#" 'universal-argument) ; extension |
199 (define-key vi-com-map "F" 'vi-backward-find-char) | 199 (define-key vi-com-map "F" 'vi-backward-find-char) |
200 (define-key vi-com-map "G" 'vi-goto-line) | 200 (define-key vi-com-map "G" 'vi-goto-line) |
201 (define-key vi-com-map "H" 'vi-home-window-line) | 201 (define-key vi-com-map "H" 'vi-home-window-line) |
202 (define-key vi-com-map "I" 'vi-insert-before-first-nonwhite) | 202 (define-key vi-com-map "I" 'vi-insert-before-first-nonwhite) |
203 (define-key vi-com-map "J" 'vi-join-lines) | 203 (define-key vi-com-map "J" 'vi-join-lines) |
204 (define-key vi-com-map "K" 'vi-undefined) | 204 (define-key vi-com-map "K" 'vi-undefined) |
205 (define-key vi-com-map "L" 'vi-last-window-line) | 205 (define-key vi-com-map "L" 'vi-last-window-line) |
206 (define-key vi-com-map "M" 'vi-middle-window-line) | 206 (define-key vi-com-map "M" 'vi-middle-window-line) |
207 (define-key vi-com-map "N" 'vi-reverse-last-search) | 207 (define-key vi-com-map "N" 'vi-reverse-last-search) |
208 (define-key vi-com-map "O" 'vi-open-above) | 208 (define-key vi-com-map "O" 'vi-open-above) |
209 (define-key vi-com-map "P" 'vi-put-before) | 209 (define-key vi-com-map "P" 'vi-put-before) |
210 (define-key vi-com-map "Q" 'vi-quote-words) ; extension | 210 (define-key vi-com-map "Q" 'vi-quote-words) ; extension |
211 (define-key vi-com-map "R" 'vi-replace-chars) | 211 (define-key vi-com-map "R" 'vi-replace-chars) |
212 (define-key vi-com-map "S" 'vi-substitute-lines) | 212 (define-key vi-com-map "S" 'vi-substitute-lines) |
213 (define-key vi-com-map "T" 'vi-backward-upto-char) | 213 (define-key vi-com-map "T" 'vi-backward-upto-char) |
214 (define-key vi-com-map "U" 'vi-unimplemented) | 214 (define-key vi-com-map "U" 'vi-unimplemented) |
215 (define-key vi-com-map "V" 'vi-undefined) | 215 (define-key vi-com-map "V" 'vi-undefined) |
216 (define-key vi-com-map "W" 'vi-forward-blank-delimited-word) | 216 (define-key vi-com-map "W" 'vi-forward-blank-delimited-word) |
217 (define-key vi-com-map "X" 'call-last-kbd-macro) ; modification/extension | 217 (define-key vi-com-map "X" 'call-last-kbd-macro) ; modification/extension |
218 (define-key vi-com-map "Y" 'vi-yank-line) | 218 (define-key vi-com-map "Y" 'vi-yank-line) |
219 (define-key vi-com-map "Z" (make-sparse-keymap)) ;allow below prefix command | 219 (define-key vi-com-map "Z" (make-sparse-keymap)) ;allow below prefix command |
220 (define-key vi-com-map "ZZ" 'vi-save-all-and-exit) | 220 (define-key vi-com-map "ZZ" 'vi-save-all-and-exit) |
245 (define-key vi-com-map "q" 'vi-replace) | 245 (define-key vi-com-map "q" 'vi-replace) |
246 (define-key vi-com-map "r" 'vi-replace-1-char) | 246 (define-key vi-com-map "r" 'vi-replace-1-char) |
247 (define-key vi-com-map "s" 'vi-substitute-chars) | 247 (define-key vi-com-map "s" 'vi-substitute-chars) |
248 (define-key vi-com-map "t" 'vi-forward-upto-char) | 248 (define-key vi-com-map "t" 'vi-forward-upto-char) |
249 (define-key vi-com-map "u" 'undo) | 249 (define-key vi-com-map "u" 'undo) |
250 (define-key vi-com-map "v" 'vi-verify-spelling) | 250 (define-key vi-com-map "v" 'vi-verify-spelling) |
251 (define-key vi-com-map "w" 'vi-forward-word) | 251 (define-key vi-com-map "w" 'vi-forward-word) |
252 (define-key vi-com-map "x" 'vi-kill-char) | 252 (define-key vi-com-map "x" 'vi-kill-char) |
253 (define-key vi-com-map "y" 'vi-operator) | 253 (define-key vi-com-map "y" 'vi-operator) |
254 (define-key vi-com-map "z" 'vi-adjust-window) | 254 (define-key vi-com-map "z" 'vi-adjust-window) |
255 | 255 |
353 | 353 |
354 (defvar vi-insert-state nil ; boolean | 354 (defvar vi-insert-state nil ; boolean |
355 "Non-nil if it is in insert state.") | 355 "Non-nil if it is in insert state.") |
356 | 356 |
357 ; in "loaddefs.el" | 357 ; in "loaddefs.el" |
358 ;(defvar search-last-string "" | 358 ;(defvar search-last-string "" |
359 ; "Last string search for by a search command.") | 359 ; "Last string search for by a search command.") |
360 | 360 |
361 (defvar vi-search-last-command nil ; (re-)search-forward(backward) | 361 (defvar vi-search-last-command nil ; (re-)search-forward(backward) |
362 "Save last search command for possible redo.") | 362 "Save last search command for possible redo.") |
363 | 363 |
364 (defvar vi-mode-old-local-map nil | 364 (defvar vi-mode-old-local-map nil |
365 "Save the local-map used before entering vi-mode.") | 365 "Save the local-map used before entering vi-mode.") |
366 | 366 |
367 (defvar vi-mode-old-mode-name nil | 367 (defvar vi-mode-old-mode-name nil |
368 "Save the mode-name before entering vi-mode.") | 368 "Save the mode-name before entering vi-mode.") |
369 | 369 |
370 (defvar vi-mode-old-major-mode nil | 370 (defvar vi-mode-old-major-mode nil |
371 "Save the major-mode before entering vi-mode.") | 371 "Save the major-mode before entering vi-mode.") |
372 | 372 |
373 (defvar vi-mode-old-case-fold nil) | 373 (defvar vi-mode-old-case-fold nil) |
374 | 374 |
375 ;(defconst vi-add-to-mode-line-1 | 375 ;(defconst vi-add-to-mode-line-1 |
376 ; '(overwrite-mode nil " Insert")) | 376 ; '(overwrite-mode nil " Insert")) |
377 | 377 |
378 ;; Value is same as vi-add-to-mode-line-1 when in vi mode, | 378 ;; Value is same as vi-add-to-mode-line-1 when in vi mode, |
379 ;; but nil in other buffers. | 379 ;; but nil in other buffers. |
402 (make-local-variable 'vi-mode-old-local-map) | 402 (make-local-variable 'vi-mode-old-local-map) |
403 (make-local-variable 'vi-mode-old-mode-name) | 403 (make-local-variable 'vi-mode-old-mode-name) |
404 (make-local-variable 'vi-mode-old-major-mode) | 404 (make-local-variable 'vi-mode-old-major-mode) |
405 (make-local-variable 'vi-mode-old-case-fold) | 405 (make-local-variable 'vi-mode-old-case-fold) |
406 (run-hooks 'vi-mode-hook)) | 406 (run-hooks 'vi-mode-hook)) |
407 | 407 |
408 ;;;###autoload | 408 ;;;###autoload |
409 (defun vi-mode () | 409 (defun vi-mode () |
410 "Major mode that acts like the `vi' editor. | 410 "Major mode that acts like the `vi' editor. |
411 The purpose of this mode is to provide you the combined power of vi (namely, | 411 The purpose of this mode is to provide you the combined power of vi (namely, |
412 the \"cross product\" effect of commands and repeat last changes) and Emacs. | 412 the \"cross product\" effect of commands and repeat last changes) and Emacs. |
446 esc-map or set undefined. These can give you the full power of Emacs. | 446 esc-map or set undefined. These can give you the full power of Emacs. |
447 - See vi-com-map for those keys that are extensions to standard vi, e.g. | 447 - See vi-com-map for those keys that are extensions to standard vi, e.g. |
448 `vi-name-last-change-or-macro', `vi-verify-spelling', `vi-locate-def', | 448 `vi-name-last-change-or-macro', `vi-verify-spelling', `vi-locate-def', |
449 `vi-mark-region', and 'vi-quote-words'. Some of them are quite handy. | 449 `vi-mark-region', and 'vi-quote-words'. Some of them are quite handy. |
450 - Use \\[vi-switch-mode] to switch among different modes quickly. | 450 - Use \\[vi-switch-mode] to switch among different modes quickly. |
451 | 451 |
452 Syntax table and abbrevs while in vi mode remain as they were in Emacs." | 452 Syntax table and abbrevs while in vi mode remain as they were in Emacs." |
453 (interactive) | 453 (interactive) |
454 (if (null vi-mode-old-major-mode) ; very first call for current buffer | 454 (if (null vi-mode-old-major-mode) ; very first call for current buffer |
455 (vi-mode-setup)) | 455 (vi-mode-setup)) |
456 | 456 |
537 (if (or (< (point) vi-ins-point) ;Check if there is any effective change | 537 (if (or (< (point) vi-ins-point) ;Check if there is any effective change |
538 (and (= (point) vi-ins-point) (null vi-ins-prefix-code)) | 538 (and (= (point) vi-ins-point) (null vi-ins-prefix-code)) |
539 (<= vi-ins-repetition 0)) | 539 (<= vi-ins-repetition 0)) |
540 (vi-goto-command-state t) | 540 (vi-goto-command-state t) |
541 (if (> vi-ins-repetition 1) | 541 (if (> vi-ins-repetition 1) |
542 (progn | 542 (progn |
543 (let ((str (buffer-substring vi-ins-point (point)))) | 543 (let ((str (buffer-substring vi-ins-point (point)))) |
544 (while (> vi-ins-repetition 1) | 544 (while (> vi-ins-repetition 1) |
545 (insert str) | 545 (insert str) |
546 (setq vi-ins-repetition (1- vi-ins-repetition)))))) | 546 (setq vi-ins-repetition (1- vi-ins-repetition)))))) |
547 (vi-set-last-change-command 'vi-first-redo-insertion vi-ins-point (point) | 547 (vi-set-last-change-command 'vi-first-redo-insertion vi-ins-point (point) |
583 | 583 |
584 (defun vi-kill-region (start end) | 584 (defun vi-kill-region (start end) |
585 (interactive "*r") | 585 (interactive "*r") |
586 (kill-region start end) | 586 (kill-region start end) |
587 (vi-set-last-change-command 'kill-region)) | 587 (vi-set-last-change-command 'kill-region)) |
588 | 588 |
589 (defun vi-append-at-end-of-line (arg) | 589 (defun vi-append-at-end-of-line (arg) |
590 "go to end of line and then go into vi insert state." | 590 "go to end of line and then go into vi insert state." |
591 (interactive "*p") | 591 (interactive "*p") |
592 (vi-goto-insert-state arg '(end-of-line) t)) | 592 (vi-goto-insert-state arg '(end-of-line) t)) |
593 | 593 |
649 ;;(defvar regexp-search t ; string | 649 ;;(defvar regexp-search t ; string |
650 ;; "*T if search string can contain regular expressions. (= set magic in vi)") | 650 ;; "*T if search string can contain regular expressions. (= set magic in vi)") |
651 ;;;;; | 651 ;;;;; |
652 | 652 |
653 (defun vi-isearch-forward (arg) | 653 (defun vi-isearch-forward (arg) |
654 "Incremental search forward. Use regexp version if ARG is non-nil." | 654 "Incremental search forward. Use regexp version if ARG is non-nil." |
655 (interactive "P") | 655 (interactive "P") |
656 (let ((scmd (if arg 'isearch-forward-regexp 'isearch-forward)) | 656 (let ((scmd (if arg 'isearch-forward-regexp 'isearch-forward)) |
657 (opoint (point))) | 657 (opoint (point))) |
658 (call-interactively scmd) | 658 (call-interactively scmd) |
659 (if (= opoint (point)) | 659 (if (= opoint (point)) |
722 (funcall (cond ((eq search-command 're-search-forward) 're-search-backward) | 722 (funcall (cond ((eq search-command 're-search-forward) 're-search-backward) |
723 ((eq search-command 're-search-backward) 're-search-forward) | 723 ((eq search-command 're-search-backward) 're-search-forward) |
724 ((eq search-command 'search-forward) 'search-backward) | 724 ((eq search-command 'search-forward) 'search-backward) |
725 ((eq search-command 'search-backward) 'search-forward)) | 725 ((eq search-command 'search-backward) 'search-forward)) |
726 search-string nil nil arg))) | 726 search-string nil nil arg))) |
727 | 727 |
728 (defun vi-join-lines (arg) | 728 (defun vi-join-lines (arg) |
729 "join ARG lines from current line (default 2), cleaning up white space." | 729 "join ARG lines from current line (default 2), cleaning up white space." |
730 (interactive "P") | 730 (interactive "P") |
731 (if (null (vi-raw-numeric-prefix arg)) | 731 (if (null (vi-raw-numeric-prefix arg)) |
732 (delete-indentation t) | 732 (delete-indentation t) |
840 (if (null mark) | 840 (if (null mark) |
841 (message "Mark register undefined." (vi-ding)) | 841 (message "Mark register undefined." (vi-ding)) |
842 (set-mark-command nil) | 842 (set-mark-command nil) |
843 (goto-char mark) | 843 (goto-char mark) |
844 (if line-flag (back-to-indentation))))))) | 844 (if line-flag (back-to-indentation))))))) |
845 | 845 |
846 (defun vi-goto-line-mark (char) | 846 (defun vi-goto-line-mark (char) |
847 "Go to the line (at first non-white) marked by next char." | 847 "Go to the line (at first non-white) marked by next char." |
848 (interactive "c") | 848 (interactive "c") |
849 (vi-goto-mark char t)) | 849 (vi-goto-mark char t)) |
850 | 850 |
1060 (cond ((or (eq moving-unit 'match) (eq moving-unit 'find)) | 1060 (cond ((or (eq moving-unit 'match) (eq moving-unit 'find)) |
1061 (setq end (1+ end))) | 1061 (setq end (1+ end))) |
1062 ((eq moving-unit 'line) | 1062 ((eq moving-unit 'line) |
1063 (goto-char begin) (beginning-of-line) (setq begin (point)) | 1063 (goto-char begin) (beginning-of-line) (setq begin (point)) |
1064 (goto-char end) (next-line 1) (beginning-of-line) (setq end (point)))) | 1064 (goto-char end) (next-line 1) (beginning-of-line) (setq end (point)))) |
1065 (if (> end (point-max)) (setq end (point-max))) ; force in buffer region | 1065 (if (> end (point-max)) (setq end (point-max))) ; force in buffer region |
1066 (cons begin end))))) | 1066 (cons begin end))))) |
1067 | 1067 |
1068 (defun vi-delete-op (motion-command arg) | 1068 (defun vi-delete-op (motion-command arg) |
1069 "Delete range specified by MOTION-COMMAND with ARG." | 1069 "Delete range specified by MOTION-COMMAND with ARG." |
1070 (let* ((range (vi-effective-range motion-command arg)) | 1070 (let* ((range (vi-effective-range motion-command arg)) |
1104 "See if STRING ends with a newline char. | 1104 "See if STRING ends with a newline char. |
1105 Used in checking whether the yanked text should be put back as lines or not." | 1105 Used in checking whether the yanked text should be put back as lines or not." |
1106 (= (aref string (1- (length string))) ?\n)) | 1106 (= (aref string (1- (length string))) ?\n)) |
1107 | 1107 |
1108 (defun vi-put-before (arg &optional after-p) | 1108 (defun vi-put-before (arg &optional after-p) |
1109 "Put yanked (in vi sense) text back before/above cursor. | 1109 "Put yanked (in vi sense) text back before/above cursor. |
1110 If a numeric prefix value (currently it should be >1) is given, put back | 1110 If a numeric prefix value (currently it should be >1) is given, put back |
1111 text as lines. If the optional after-p is given, put after/below the cursor." | 1111 text as lines. If the optional after-p is given, put after/below the cursor." |
1112 (interactive "P") | 1112 (interactive "P") |
1113 (let ((reg (vi-prefix-char-value arg)) put-text) | 1113 (let ((reg (vi-prefix-char-value arg)) put-text) |
1114 (if (and reg (or (< reg ?1) (> reg ?9)) (null (get-register reg))) | 1114 (if (and reg (or (< reg ?1) (> reg ?9)) (null (get-register reg))) |
1300 (defun vi-replace (arg) | 1300 (defun vi-replace (arg) |
1301 "Replace strings, use regexp version if ARG is non-nil." | 1301 "Replace strings, use regexp version if ARG is non-nil." |
1302 (interactive "*P") | 1302 (interactive "*P") |
1303 (let ((rcmd (if arg 'replace-regexp 'replace-string))) | 1303 (let ((rcmd (if arg 'replace-regexp 'replace-string))) |
1304 (call-interactively rcmd nil))) | 1304 (call-interactively rcmd nil))) |
1305 | 1305 |
1306 (defun vi-adjust-window (arg position) | 1306 (defun vi-adjust-window (arg position) |
1307 "Move current line to the top/center/bottom of the window." | 1307 "Move current line to the top/center/bottom of the window." |
1308 (interactive "p\nc") | 1308 (interactive "p\nc") |
1309 (cond ((char-equal position ?\r) (recenter 0)) | 1309 (cond ((char-equal position ?\r) (recenter 0)) |
1310 ((char-equal position ?-) (recenter -1)) | 1310 ((char-equal position ?-) (recenter -1)) |
1327 t | 1327 t |
1328 (goto-char opoint) | 1328 (goto-char opoint) |
1329 (ding)))) | 1329 (ding)))) |
1330 | 1330 |
1331 (defun vi-name-last-change-or-macro (arg char) | 1331 (defun vi-name-last-change-or-macro (arg char) |
1332 "Give name to the last change command or just defined kbd macro. | 1332 "Give name to the last change command or just defined kbd macro. |
1333 If prefix ARG is given, name last macro, otherwise name last change command. | 1333 If prefix ARG is given, name last macro, otherwise name last change command. |
1334 The following CHAR will be the name for the command or macro." | 1334 The following CHAR will be the name for the command or macro." |
1335 (interactive "P\nc") | 1335 (interactive "P\nc") |
1336 (if arg | 1336 (if arg |
1337 (name-last-kbd-macro (intern (char-to-string char))) | 1337 (name-last-kbd-macro (intern (char-to-string char))) |
1341 (overwrite-p (nth 2 args)) | 1341 (overwrite-p (nth 2 args)) |
1342 (prefix-code (nth 3 args))) | 1342 (prefix-code (nth 3 args))) |
1343 (vi-set-last-change-command 'vi-more-redo-insertion str | 1343 (vi-set-last-change-command 'vi-more-redo-insertion str |
1344 overwrite-p prefix-code))) | 1344 overwrite-p prefix-code))) |
1345 (fset (intern (char-to-string char)) vi-last-change-command))) | 1345 (fset (intern (char-to-string char)) vi-last-change-command))) |
1346 | 1346 |
1347 (defun vi-call-named-change-or-macro (count char) | 1347 (defun vi-call-named-change-or-macro (count char) |
1348 "Execute COUNT times the keyboard macro definition named by the following CHAR." | 1348 "Execute COUNT times the keyboard macro definition named by the following CHAR." |
1349 (interactive "p\nc") | 1349 (interactive "p\nc") |
1350 (if (stringp (symbol-function (intern (char-to-string char)))) | 1350 (if (stringp (symbol-function (intern (char-to-string char)))) |
1351 (execute-kbd-macro (intern (char-to-string char)) count) | 1351 (execute-kbd-macro (intern (char-to-string char)) count) |