1;;; vi.el --- major mode for emulating "vi" editor under GNU Emacs 2 3;; This file is in the public domain because the authors distributed it 4;; without a copyright notice before the US signed the Bern Convention. 5 6;; This file is part of GNU Emacs. 7 8;; Author: Neal Ziring <nz@rsch.wisc.edu> 9;; Felix S. T. Wu <wu@crys.wisc.edu> 10;; Keywords: emulations 11;; Obsolete-since: 24.5 12 13;;; Commentary: 14 15;; This file is obsolete. Consider using viper instead. 16 17;; Originally written by : seismo!wucs!nz@rsch.wisc.edu (Neal Ziring) 18;; Extensively redesigned and rewritten by wu@crys.wisc.edu (Felix S.T. Wu) 19;; Last revision: 01/07/87 Wed (for GNU Emacs 18.33) 20 21;; INSTALLATION PROCEDURE: 22;; 1) Add a global key binding for command "vi-mode" (I use ESC ESC instead of 23;; the single ESC used in real "vi", so I can access other ESC prefixed emacs 24;; commands while I'm in "vi"), say, by putting the following line in your 25;; ".emacs" file: 26;; (define-key global-map "\e\e" 'vi-mode) ;quick switch into vi-mode 27;; 2) If you wish you can define "find-file-hook" to enter "vi" automatically 28;; after a file is loaded into the buffer. For example, I defined it as: 29;; (setq find-file-hook (list 30;; (function (lambda () 31;; (if (not (or (eq major-mode 'Info-mode) 32;; (eq major-mode 'vi-mode))) 33;; (vi-mode)))))) 34;; 3) In your init file you can define the command "vi-mode" to be "autoload" 35;; or you can execute the "load" command to load "vi" directly. 36;; 4) Read the comments for command "vi-mode" before you start using it. 37 38;; COULD DO 39;; 1). A general 'define-operator' function to replace current hack 40;; 2). In operator handling, should allow other point moving Emacs commands 41;; (such as ESC <, ESC >) to be used as arguments. 42 43;;; Code: 44 45(defvar vi-mode-old-major-mode) 46(defvar vi-mode-old-mode-name) 47(defvar vi-mode-old-local-map) 48(defvar vi-mode-old-case-fold) 49 50(if (null (where-is-internal 'vi-switch-mode (current-local-map))) 51 (define-key ctl-x-map "~" 'vi-switch-mode)) 52 53(defvar vi-tilde-map nil 54 "Keymap used for \\[vi-switch-mode] prefix key. Link to various major modes.") 55 56(if vi-tilde-map 57 nil 58 (setq vi-tilde-map (make-keymap)) 59 (define-key vi-tilde-map "a" 'abbrev-mode) 60 (define-key vi-tilde-map "c" 'c-mode) 61 (define-key vi-tilde-map "d" 'vi-debugging) 62 (define-key vi-tilde-map "e" 'emacs-lisp-mode) 63 (define-key vi-tilde-map "f" 'auto-fill-mode) 64 (define-key vi-tilde-map "g" 'prolog-mode) 65 (define-key vi-tilde-map "h" 'hanoi) 66 (define-key vi-tilde-map "i" 'info-mode) 67 (define-key vi-tilde-map "l" 'lisp-mode) 68 (define-key vi-tilde-map "n" 'nroff-mode) 69 (define-key vi-tilde-map "o" 'overwrite-mode) 70 (define-key vi-tilde-map "O" 'outline-mode) 71 (define-key vi-tilde-map "P" 'picture-mode) 72 (define-key vi-tilde-map "r" 'vi-readonly-mode) 73 (define-key vi-tilde-map "t" 'text-mode) 74 (define-key vi-tilde-map "v" 'vi-mode) 75 (define-key vi-tilde-map "x" 'tex-mode) 76 (define-key vi-tilde-map "~" 'vi-back-to-old-mode)) 77 78(defun vi-switch-mode (arg mode-char) 79 "Switch the major mode of current buffer as specified by the following char \\{vi-tilde-map}" 80 (interactive "P\nc") 81 (let ((mode-cmd (lookup-key vi-tilde-map (char-to-string mode-char)))) 82 (if (null mode-cmd) 83 (with-output-to-temp-buffer "*Help*" 84 (princ (substitute-command-keys "Possible major modes to switch to: \\{vi-tilde-map}")) 85 (with-current-buffer standard-output 86 (help-mode))) 87 (setq prefix-arg arg) ; prefix arg will be passed down 88 (command-execute mode-cmd nil) ; may need to save mode-line-format etc 89 (force-mode-line-update)))) ; just in case 90 91 92(defun vi-debugging (arg) 93 "Toggle debug-on-error flag. If prefix arg is given, set t." 94 (interactive "P") 95 (if arg 96 (setq debug-on-error t) 97 (setq debug-on-error (not debug-on-error))) 98 (if debug-on-error 99 (message "Debug-on-error ...") 100 (message "NO more debug-on-error"))) 101 102(defun vi-back-to-old-mode () 103 "Go back to the previous mode without setting up for insertion." 104 (interactive) 105 (if vi-mode-old-major-mode 106 (progn 107 (setq mode-name vi-mode-old-mode-name) 108 (use-local-map vi-mode-old-local-map) 109 (setq major-mode vi-mode-old-major-mode) 110 (setq case-fold-search vi-mode-old-case-fold) 111 (force-mode-line-update)))) 112 113(defun vi-readonly-mode () 114 "Toggle current buffer's readonly flag." 115 (interactive) 116 (setq buffer-read-only (not buffer-read-only))) 117 118(defvar vi-com-map nil 119 "Keymap used in Evi's command state 120Command state includes most of the vi editing commands, with some Emacs 121command extensions.") 122 123(put 'vi-undefined 'suppress-keymap t) 124(if vi-com-map nil 125 (setq vi-com-map (make-keymap)) 126;;(fillarray vi-com-map 'vi-undefined) 127 (define-key vi-com-map "\C-@" 'vi-mark-region) ; extension 128 (define-key vi-com-map "\C-a" 'vi-ask-for-info) ; extension 129 (define-key vi-com-map "\C-b" 'vi-backward-windowful) 130 (define-key vi-com-map "\C-c" 'vi-do-old-mode-C-c-command) ; extension 131 (define-key vi-com-map "\C-d" 'vi-scroll-down-window) 132 (define-key vi-com-map "\C-e" 'vi-expose-line-below) 133 (define-key vi-com-map "\C-f" 'vi-forward-windowful) 134 (define-key vi-com-map "\C-g" 'keyboard-quit) 135 (define-key vi-com-map "\C-i" 'indent-relative-first-indent-point) ; TAB 136 (define-key vi-com-map "\C-j" 'vi-next-line) ; LFD 137 (define-key vi-com-map "\C-k" 'vi-kill-line) ; extension 138 (define-key vi-com-map "\C-l" 'recenter) 139 (define-key vi-com-map "\C-m" 'vi-next-line-first-nonwhite) ; RET 140 (define-key vi-com-map "\C-n" 'vi-next-line) 141 (define-key vi-com-map "\C-o" 'vi-split-open-line) 142 (define-key vi-com-map "\C-p" 'previous-line) 143 (define-key vi-com-map "\C-q" 'vi-query-replace) ; extension 144 (define-key vi-com-map "\C-r" 'vi-isearch-backward) ; modification 145 (define-key vi-com-map "\C-s" 'vi-isearch-forward) ; extension 146 (define-key vi-com-map "\C-t" 'vi-transpose-objects) ; extension 147 (define-key vi-com-map "\C-u" 'vi-scroll-up-window) 148 (define-key vi-com-map "\C-v" 'scroll-up-command) ; extension 149 (define-key vi-com-map "\C-w" 'vi-kill-region) ; extension 150 (define-key vi-com-map "\C-x" 'Control-X-prefix) ; extension 151 (define-key vi-com-map "\C-y" 'vi-expose-line-above) 152 (define-key vi-com-map "\C-z" 'suspend-emacs) 153 154 (define-key vi-com-map "\e" 'ESC-prefix); C-[ (ESC) 155 (define-key vi-com-map "\C-\\" 'vi-unimplemented) 156 (define-key vi-com-map "\C-]" 'find-tag) 157 (define-key vi-com-map "\C-^" 'vi-locate-def) ; extension 158 (define-key vi-com-map "\C-_" 'vi-undefined) 159 160 (define-key vi-com-map " " 'forward-char) 161 (define-key vi-com-map "!" 'vi-operator) 162 (define-key vi-com-map "\"" 'vi-char-argument) 163 (define-key vi-com-map "#" 'universal-argument) ; extension 164 (define-key vi-com-map "$" 'end-of-line) 165 (define-key vi-com-map "%" 'vi-find-matching-paren) 166 (define-key vi-com-map "&" 'vi-unimplemented) 167 (define-key vi-com-map "'" 'vi-goto-line-mark) 168 (define-key vi-com-map "(" 'backward-sexp) 169 (define-key vi-com-map ")" 'forward-sexp) 170 (define-key vi-com-map "*" 'vi-name-last-change-or-macro) ; extension 171 (define-key vi-com-map "+" 'vi-next-line-first-nonwhite) 172 (define-key vi-com-map "," 'vi-reverse-last-find-char) 173 (define-key vi-com-map "-" 'vi-previous-line-first-nonwhite) 174 (define-key vi-com-map "." 'vi-redo-last-change-command) 175 (define-key vi-com-map "/" 'vi-search-forward) 176 (define-key vi-com-map "0" 'beginning-of-line) 177 178 (define-key vi-com-map "1" 'vi-digit-argument) 179 (define-key vi-com-map "2" 'vi-digit-argument) 180 (define-key vi-com-map "3" 'vi-digit-argument) 181 (define-key vi-com-map "4" 'vi-digit-argument) 182 (define-key vi-com-map "5" 'vi-digit-argument) 183 (define-key vi-com-map "6" 'vi-digit-argument) 184 (define-key vi-com-map "7" 'vi-digit-argument) 185 (define-key vi-com-map "8" 'vi-digit-argument) 186 (define-key vi-com-map "9" 'vi-digit-argument) 187 188 (define-key vi-com-map ":" 'vi-ex-cmd) 189 (define-key vi-com-map ";" 'vi-repeat-last-find-char) 190 (define-key vi-com-map "<" 'vi-operator) 191 (define-key vi-com-map "=" 'vi-operator) 192 (define-key vi-com-map ">" 'vi-operator) 193 (define-key vi-com-map "?" 'vi-search-backward) 194 (define-key vi-com-map "@" 'vi-call-named-change-or-macro) ; extension 195 196 (define-key vi-com-map "A" 'vi-append-at-end-of-line) 197 (define-key vi-com-map "B" 'vi-backward-blank-delimited-word) 198 (define-key vi-com-map "C" 'vi-change-rest-of-line) 199 (define-key vi-com-map "D" 'vi-kill-line) 200 (define-key vi-com-map "E" 'vi-end-of-blank-delimited-word) 201 (define-key vi-com-map "F" 'vi-backward-find-char) 202 (define-key vi-com-map "G" 'vi-goto-line) 203 (define-key vi-com-map "H" 'vi-home-window-line) 204 (define-key vi-com-map "I" 'vi-insert-before-first-nonwhite) 205 (define-key vi-com-map "J" 'vi-join-lines) 206 (define-key vi-com-map "K" 'vi-undefined) 207 (define-key vi-com-map "L" 'vi-last-window-line) 208 (define-key vi-com-map "M" 'vi-middle-window-line) 209 (define-key vi-com-map "N" 'vi-reverse-last-search) 210 (define-key vi-com-map "O" 'vi-open-above) 211 (define-key vi-com-map "P" 'vi-put-before) 212 (define-key vi-com-map "Q" 'vi-quote-words) ; extension 213 (define-key vi-com-map "R" 'vi-replace-chars) 214 (define-key vi-com-map "S" 'vi-substitute-lines) 215 (define-key vi-com-map "T" 'vi-backward-upto-char) 216 (define-key vi-com-map "U" 'vi-unimplemented) 217 (define-key vi-com-map "V" 'vi-undefined) 218 (define-key vi-com-map "W" 'vi-forward-blank-delimited-word) 219 (define-key vi-com-map "X" 'call-last-kbd-macro) ; modification/extension 220 (define-key vi-com-map "Y" 'vi-yank-line) 221 (define-key vi-com-map "Z" (make-sparse-keymap)) ;allow below prefix command 222 (define-key vi-com-map "ZZ" 'vi-save-all-and-exit) 223 224 (define-key vi-com-map "[" 'vi-unimplemented) 225 (define-key vi-com-map "\\" 'vi-operator) ; extension for vi-narrow-op 226 (define-key vi-com-map "]" 'vi-unimplemented) 227 (define-key vi-com-map "^" 'back-to-indentation) 228 (define-key vi-com-map "_" 'vi-undefined) 229 (define-key vi-com-map "`" 'vi-goto-char-mark) 230 231 (define-key vi-com-map "a" 'vi-insert-after) 232 (define-key vi-com-map "b" 'backward-word) 233 (define-key vi-com-map "c" 'vi-operator) 234 (define-key vi-com-map "d" 'vi-operator) 235 (define-key vi-com-map "e" 'vi-end-of-word) 236 (define-key vi-com-map "f" 'vi-forward-find-char) 237 (define-key vi-com-map "g" 'vi-beginning-of-buffer) ; extension 238 (define-key vi-com-map "h" 'backward-char) 239 (define-key vi-com-map "i" 'vi-insert-before) 240 (define-key vi-com-map "j" 'vi-next-line) 241 (define-key vi-com-map "k" 'previous-line) 242 (define-key vi-com-map "l" 'forward-char) 243 (define-key vi-com-map "m" 'vi-set-mark) 244 (define-key vi-com-map "n" 'vi-repeat-last-search) 245 (define-key vi-com-map "o" 'vi-open-below) 246 (define-key vi-com-map "p" 'vi-put-after) 247 (define-key vi-com-map "q" 'vi-replace) 248 (define-key vi-com-map "r" 'vi-replace-1-char) 249 (define-key vi-com-map "s" 'vi-substitute-chars) 250 (define-key vi-com-map "t" 'vi-forward-upto-char) 251 (define-key vi-com-map "u" 'undo) 252 (define-key vi-com-map "v" 'vi-verify-spelling) 253 (define-key vi-com-map "w" 'vi-forward-word) 254 (define-key vi-com-map "x" 'vi-kill-char) 255 (define-key vi-com-map "y" 'vi-operator) 256 (define-key vi-com-map "z" 'vi-adjust-window) 257 258 (define-key vi-com-map "{" 'backward-paragraph) 259 (define-key vi-com-map "|" 'vi-goto-column) 260 (define-key vi-com-map "}" 'forward-paragraph) 261 (define-key vi-com-map "~" 'vi-change-case) 262 (define-key vi-com-map "\177" 'delete-backward-char)) 263 264(put 'backward-char 'point-moving-unit 'char) 265(put 'vi-next-line 'point-moving-unit 'line) 266(put 'next-line 'point-moving-unit 'line) 267(put 'forward-line 'point-moving-unit 'line) 268(put 'previous-line 'point-moving-unit 'line) 269(put 'vi-isearch-backward 'point-moving-unit 'search) 270(put 'vi-search-backward 'point-moving-unit 'search) 271(put 'vi-isearch-forward 'point-moving-unit 'search) 272(put 'vi-search-forward 'point-moving-unit 'search) 273(put 'forward-char 'point-moving-unit 'char) 274(put 'end-of-line 'point-moving-unit 'char) 275(put 'vi-find-matching-paren 'point-moving-unit 'match) 276(put 'vi-goto-line-mark 'point-moving-unit 'line) 277(put 'backward-sexp 'point-moving-unit 'sexp) 278(put 'forward-sexp 'point-moving-unit 'sexp) 279(put 'vi-next-line-first-nonwhite 'point-moving-unit 'line) 280(put 'vi-previous-line-first-nonwhite 'point-moving-unit 'line) 281(put 'vi-reverse-last-find-char 'point-moving-unit 'rev-find) 282(put 'vi-re-search-forward 'point-moving-unit 'search) 283(put 'beginning-of-line 'point-moving-unit 'char) 284(put 'vi-beginning-of-buffer 'point-moving-unit 'char) 285(put 'vi-repeat-last-find-char 'point-moving-unit 'find) 286(put 'vi-re-search-backward 'point-moving-unit 'search) 287(put 'vi-backward-blank-delimited-word 'point-moving-unit 'WORD) 288(put 'vi-end-of-blank-delimited-word 'point-moving-unit 'match) 289(put 'vi-backward-find-char 'point-moving-unit 'find) 290(put 'vi-goto-line 'point-moving-unit 'line) 291(put 'vi-home-window-line 'point-moving-unit 'line) 292(put 'vi-last-window-line 'point-moving-unit 'line) 293(put 'vi-middle-window-line 'point-moving-unit 'line) 294(put 'vi-reverse-last-search 'point-moving-unit 'rev-search) 295(put 'vi-backward-upto-char 'point-moving-unit 'find) 296(put 'vi-forward-blank-delimited-word 'point-moving-unit 'WORD) 297(put 'back-to-indentation 'point-moving-unit 'char) 298(put 'vi-goto-char-mark 'point-moving-unit 'char) 299(put 'backward-word 'point-moving-unit 'word) 300(put 'vi-end-of-word 'point-moving-unit 'match) 301(put 'vi-forward-find-char 'point-moving-unit 'find) 302(put 'backward-char 'point-moving-unit 'char) 303(put 'vi-forward-char 'point-moving-unit 'char) 304(put 'vi-repeat-last-search 'point-moving-unit 'search) 305(put 'vi-forward-upto-char 'point-moving-unit 'find) 306(put 'vi-forward-word 'point-moving-unit 'word) 307(put 'vi-goto-column 'point-moving-unit 'match) 308(put 'forward-paragraph 'point-moving-unit 'paragraph) 309(put 'backward-paragraph 'point-moving-unit 'paragraph) 310 311;;; region mark commands 312(put 'mark-page 'point-moving-unit 'region) 313(put 'mark-paragraph 'point-moving-unit 'region) 314(put 'mark-word 'point-moving-unit 'region) 315(put 'mark-sexp 'point-moving-unit 'region) 316(put 'mark-defun 'point-moving-unit 'region) 317(put 'mark-whole-buffer 'point-moving-unit 'region) 318(put 'mark-end-of-sentence 'point-moving-unit 'region) 319(put 'c-mark-function 'point-moving-unit 'region) 320;;; 321 322(defvar vi-mark-alist nil 323 "Alist of (NAME . MARK), marks are local to each buffer.") 324 325(defvar vi-scroll-amount (/ (window-height) 2) 326 "Default amount of lines for scrolling (used by \"^D\"/\"^U\").") 327 328(defvar vi-shift-width 4 329 "Shift amount for \"<\"/\">\" operators.") 330 331(defvar vi-ins-point nil ; integer 332 "Last insertion point. Should use `mark' instead.") 333 334(defvar vi-ins-length nil ; integer 335 "Length of last insertion.") 336 337(defvar vi-ins-repetition nil ; integer 338 "The repetition required for last insertion.") 339 340(defvar vi-ins-overwrt-p nil ; boolean 341 "T if last insertion was a replace actually.") 342 343(defvar vi-ins-prefix-code nil ; ready-to-eval sexp 344 "Code to be eval'ed before (redo-)insertion begins.") 345 346(defvar vi-last-find-char nil ; cons cell 347 "Save last direction, char and upto-flag used for char finding.") 348 349(defvar vi-last-change-command nil ; cons cell 350 "Save commands for redoing last changes. Each command is in (FUNC . ARGS) 351form that is ready to be `apply'ed.") 352 353(defvar vi-last-shell-command nil ; last shell op command line 354 "Save last shell command given for \"!\" operator.") 355 356(defvar vi-insert-state nil ; boolean 357 "Non-nil if it is in insert state.") 358 359; in "loaddefs.el" 360;(defvar search-last-string "" 361; "Last string search for by a search command.") 362 363(defvar vi-search-last-command nil ; (re-)search-forward(backward) 364 "Save last search command for possible redo.") 365 366(defvar vi-mode-old-local-map nil 367 "Save the local-map used before entering vi-mode.") 368 369(defvar vi-mode-old-mode-name nil 370 "Save the mode-name before entering vi-mode.") 371 372(defvar vi-mode-old-major-mode nil 373 "Save the major-mode before entering vi-mode.") 374 375(defvar vi-mode-old-case-fold nil) 376 377;(defconst vi-add-to-mode-line-1 378; '(overwrite-mode nil " Insert")) 379 380;; Value is same as vi-add-to-mode-line-1 when in vi mode, 381;; but nil in other buffers. 382;(defvar vi-add-to-mode-line nil) 383 384(defun vi-mode-setup () 385 "Setup a buffer for vi-mode by creating necessary buffer-local variables." 386; (make-local-variable 'vi-add-to-mode-line) 387; (setq vi-add-to-mode-line vi-add-to-mode-line-1) 388; (or (memq vi-add-to-mode-line minor-mode-alist) 389; (setq minor-mode-alist (cons vi-add-to-mode-line minor-mode-alist))) 390 (make-local-variable 'vi-scroll-amount) 391 (setq vi-scroll-amount (/ (window-height) 2)) 392 (make-local-variable 'vi-shift-width) 393 (setq vi-shift-width 4) 394 (make-local-variable 'vi-ins-point) 395 (make-local-variable 'vi-ins-length) 396 (make-local-variable 'vi-ins-repetition) 397 (make-local-variable 'vi-ins-overwrt-p) 398 (make-local-variable 'vi-ins-prefix-code) 399 (make-local-variable 'vi-last-change-command) 400 (make-local-variable 'vi-last-shell-command) 401 (make-local-variable 'vi-last-find-char) 402 (make-local-variable 'vi-mark-alist) 403 (make-local-variable 'vi-insert-state) 404 (make-local-variable 'vi-mode-old-local-map) 405 (make-local-variable 'vi-mode-old-mode-name) 406 (make-local-variable 'vi-mode-old-major-mode) 407 (make-local-variable 'vi-mode-old-case-fold) 408 (run-mode-hooks 'vi-mode-hook)) 409 410;;;###autoload 411(defun vi-mode () 412 "Major mode that acts like the `vi' editor. 413The purpose of this mode is to provide you the combined power of vi (namely, 414the \"cross product\" effect of commands and repeat last changes) and Emacs. 415 416This command redefines nearly all keys to look like vi commands. 417It records the previous major mode, and any vi command for input 418\(`i', `a', `s', etc.) switches back to that mode. 419Thus, ordinary Emacs (in whatever major mode you had been using) 420is \"input\" mode as far as vi is concerned. 421 422To get back into vi from \"input\" mode, you must issue this command again. 423Therefore, it is recommended that you assign it to a key. 424 425Major differences between this mode and real vi : 426 427* Limitations and unsupported features 428 - Search patterns with line offset (e.g. /pat/+3 or /pat/z.) are 429 not supported. 430 - Ex commands are not implemented; try ':' to get some hints. 431 - No line undo (i.e. the `U' command), but multi-undo is a standard feature. 432 433* Modifications 434 - The stopping positions for some point motion commands (word boundary, 435 pattern search) are slightly different from standard `vi'. 436 Also, no automatic wrap around at end of buffer for pattern searching. 437 - Since changes are done in two steps (deletion then insertion), you need 438 to undo twice to completely undo a change command. But this is not needed 439 for undoing a repeated change command. 440 - No need to set/unset `magic', to search for a string with regular expr 441 in it just put a prefix arg for the search commands. Replace cmds too. 442 - ^R is bound to incremental backward search, so use ^L to redraw screen. 443 444* Extensions 445 - Some standard (or modified) Emacs commands were integrated, such as 446 incremental search, query replace, transpose objects, and keyboard macros. 447 - In command state, ^X links to the `ctl-x-map', and ESC can be linked to 448 esc-map or set undefined. These can give you the full power of Emacs. 449 - See vi-com-map for those keys that are extensions to standard vi, e.g. 450 `vi-name-last-change-or-macro', `vi-verify-spelling', `vi-locate-def', 451 `vi-mark-region', and `vi-quote-words'. Some of them are quite handy. 452 - Use \\[vi-switch-mode] to switch among different modes quickly. 453 454Syntax table and abbrevs while in vi mode remain as they were in Emacs." 455 (interactive) 456 (if (null vi-mode-old-major-mode) ; very first call for current buffer 457 (vi-mode-setup)) 458 459 (if (eq major-mode 'vi-mode) 460 (progn (ding) (message "Already in vi-mode.")) 461 (setq vi-mode-old-local-map (current-local-map)) 462 (setq vi-mode-old-mode-name mode-name) 463 (setq vi-mode-old-major-mode major-mode) 464 (setq vi-mode-old-case-fold case-fold-search) ; this is needed !! 465 (setq case-fold-search nil) ; exact case match in searching 466 (use-local-map vi-com-map) 467 (setq major-mode 'vi-mode) 468 (setq mode-name "VI") 469 (force-mode-line-update) ; force mode line update 470 (if vi-insert-state ; this is a return from insertion 471 (vi-end-of-insert-state)))) 472 473(defun vi-ding() 474 "Ding !" 475 (interactive) 476 (ding)) 477 478(defun vi-save-all-and-exit () 479 "Save all modified buffers without asking, then exits emacs." 480 (interactive) 481 (save-some-buffers t) 482 (kill-emacs)) 483 484;; to be used by "ex" commands 485(defvar vi-replaced-string nil) 486(defvar vi-replacing-string nil) 487 488(defun vi-ex-cmd () 489 "Ex commands are not implemented in Evi mode. For some commonly used ex 490commands, you can use the following alternatives for similar effect : 491w C-x C-s (save-buffer) 492wq C-x C-c (save-buffers-kill-emacs) 493w fname C-x C-w (write-file) 494e fname C-x C-f (find-file) 495r fname C-x i (insert-file) 496s/old/new use q (vi-replace) to do unconditional replace 497 use C-q (vi-query-replace) to do query replace 498set sw=n M-x set-variable vi-shift-width n " 499 (interactive) 500;; (let ((cmd (read-string ":")) (lines 1)) 501;; (cond ((string-match "s")))) 502 (with-output-to-temp-buffer "*Help*" 503 (princ (documentation 'vi-ex-cmd)) 504 (with-current-buffer standard-output 505 (help-mode)))) 506 507(defun vi-undefined () 508 (interactive) 509 (message "Command key \"%s\" is undefined in Evi." 510 (single-key-description last-command-event)) 511 (ding)) 512 513(defun vi-unimplemented () 514 (interactive) 515 (message "Command key \"%s\" is not implemented in Evi." 516 (single-key-description last-command-event)) 517 (ding)) 518 519;;;;; 520(defun vi-goto-insert-state (repetition &optional prefix-code do-it-now-p) 521 "Go into insert state, the text entered will be repeated if REPETITION > 1. 522If PREFIX-CODE is given, do it before insertion begins if DO-IT-NOW-P is T. 523In any case, the prefix-code will be done before each `redo-insert'. 524This function expects `overwrite-mode' being set properly beforehand." 525 (if do-it-now-p (apply (car prefix-code) (cdr prefix-code))) 526 (setq vi-ins-point (point)) 527 (setq vi-ins-repetition repetition) 528 (setq vi-ins-prefix-code prefix-code) 529 (setq mode-name vi-mode-old-mode-name) 530 (setq case-fold-search vi-mode-old-case-fold) 531 (use-local-map vi-mode-old-local-map) 532 (setq major-mode vi-mode-old-major-mode) 533 (force-mode-line-update) 534 (setq vi-insert-state t)) 535 536(defun vi-end-of-insert-state () 537 "Terminate insertion and set up last change command." 538 (if (or (< (point) vi-ins-point) ;Check if there is any effective change 539 (and (= (point) vi-ins-point) (null vi-ins-prefix-code)) 540 (<= vi-ins-repetition 0)) 541 (vi-goto-command-state t) 542 (if (> vi-ins-repetition 1) 543 (progn 544 (let ((str (buffer-substring vi-ins-point (point)))) 545 (while (> vi-ins-repetition 1) 546 (insert str) 547 (setq vi-ins-repetition (1- vi-ins-repetition)))))) 548 (vi-set-last-change-command 'vi-first-redo-insertion vi-ins-point (point) 549 overwrite-mode vi-ins-prefix-code) 550 (vi-goto-command-state t))) 551 552(defun vi-first-redo-insertion (begin end &optional overwrite-p prefix-code) 553 "Redo last insertion the first time. Extract the string and save it for 554future redoes. Do prefix-code if it's given, use overwrite mode if asked." 555 (let ((str (buffer-substring begin end))) 556 (if prefix-code (apply (car prefix-code) (cdr prefix-code))) 557 (if overwrite-p (delete-region (point) (+ (point) (length str)))) 558 (insert str) 559 (vi-set-last-change-command 'vi-more-redo-insertion str overwrite-p prefix-code))) 560 561(defun vi-more-redo-insertion (str &optional overwrite-p prefix-code) 562 "Redo more insertion : copy string from STR to point, use overwrite mode 563if overwrite-p is T; apply prefix-code first if it's non-nil." 564 (if prefix-code (apply (car prefix-code) (cdr prefix-code))) 565 (if overwrite-p (delete-region (point) (+ (point) (length str)))) 566 (insert str)) 567 568(defun vi-goto-command-state (&optional from-insert-state-p) 569 "Go to vi-mode command state. If optional arg exists, means return from 570insert state." 571 (use-local-map vi-com-map) 572 (setq vi-insert-state nil) 573 (if from-insert-state-p 574 (if overwrite-mode 575 (overwrite-mode 0) 576; (set-minor-mode 'ins "Insert" nil) 577 ))) 578 579(defun vi-kill-line (arg) 580 "kill specified number of lines (=d$), text saved in the kill ring." 581 (interactive "*P") 582 (kill-line arg) 583 (vi-set-last-change-command 'kill-line arg)) 584 585(defun vi-kill-region (start end) 586 (interactive "*r") 587 (kill-region start end) 588 (vi-set-last-change-command 'kill-region)) 589 590(defun vi-append-at-end-of-line (arg) 591 "go to end of line and then go into vi insert state." 592 (interactive "*p") 593 (vi-goto-insert-state arg '(end-of-line) t)) 594 595(defun vi-change-rest-of-line (arg) 596 "Change the rest of (ARG) lines (= c$ in vi)." 597 (interactive "*P") 598 (vi-goto-insert-state 1 (list 'kill-line arg) t)) 599 600(defun vi-insert-before-first-nonwhite (arg) 601 "(= ^i in vi)" 602 (interactive "*p") 603 (vi-goto-insert-state arg '(back-to-indentation) t)) 604 605(defun vi-open-above (arg) 606 "open new line(s) above current line and enter insert state." 607 (interactive "*p") 608 (vi-goto-insert-state 1 609 (list (function (lambda (x) 610 (or (beginning-of-line) 611 (open-line x)))) arg) 612 t)) 613 614(defun vi-open-below (arg) 615 "open new line(s) and go into insert mode on the last line." 616 (interactive "*p") 617 (vi-goto-insert-state 1 618 (list (function (lambda (x) 619 (or (end-of-line) 620 (open-line x) 621 (forward-line x)))) arg) 622 t)) 623 624(defun vi-insert-after (arg) 625 "start vi insert state after cursor." 626 (interactive "*p") 627 (vi-goto-insert-state arg 628 (list (function (lambda () 629 (if (not (eolp)) (forward-char))))) 630 t)) 631 632(defun vi-insert-before (arg) 633 "enter insert state before the cursor." 634 (interactive "*p") 635 (vi-goto-insert-state arg)) 636 637(defun vi-goto-line (arg) 638 "Go to ARGth line." 639 (interactive "P") 640 (if (null (vi-raw-numeric-prefix arg)) 641 (with-no-warnings 642 (end-of-buffer)) 643 (with-no-warnings (goto-line (vi-prefix-numeric-value arg))))) 644 645(defun vi-beginning-of-buffer () 646 "Move point to the beginning of current buffer." 647 (interactive) 648 (goto-char (point-min))) 649 650;;;;; not used now 651;;(defvar regexp-search t ; string 652;; "*T if search string can contain regular expressions. (= set magic in vi)") 653;;;;; 654 655(defun vi-isearch-forward (arg) 656 "Incremental search forward. Use regexp version if ARG is non-nil." 657 (interactive "P") 658 (let ((scmd (if arg 'isearch-forward-regexp 'isearch-forward)) 659 (opoint (point))) 660 (call-interactively scmd) 661 (if (= opoint (point)) 662 nil 663 (setq vi-search-last-command (if arg 're-search-forward 'search-forward))))) 664 665(defun vi-isearch-backward (arg) 666 "Incremental search backward. Use regexp version if ARG is non-nil." 667 (interactive "P") 668 (let ((scmd (if arg 'isearch-backward-regexp 'isearch-backward)) 669 (opoint (point))) 670 (call-interactively scmd) 671 (if (= opoint (point)) 672 nil 673 (setq vi-search-last-command (if arg 're-search-backward 'search-backward))))) 674 675(defun vi-search-forward (arg string) 676 "Nonincremental search forward. Use regexp version if ARG is non-nil." 677 (interactive (if current-prefix-arg 678 (list t (read-string "regexp/" nil)) 679 (list nil (read-string "/" nil)))) 680 (setq vi-search-last-command (if arg 're-search-forward 'search-forward)) 681 (if (> (length string) 0) 682 (isearch-update-ring string arg)) 683 (funcall vi-search-last-command string nil nil 1)) 684 685(defun vi-search-backward (arg string) 686 "Nonincremental search backward. Use regexp version if ARG is non-nil." 687 (interactive (if current-prefix-arg 688 (list t (read-string "regexp?" nil)) 689 (list nil (read-string "?" nil)))) 690 (setq vi-search-last-command (if arg 're-search-backward 'search-backward)) 691 (if (> (length string) 0) 692 (isearch-update-ring string arg)) 693 (funcall vi-search-last-command string nil nil 1)) 694 695(defun vi-repeat-last-search (arg &optional search-command search-string) 696 "Repeat last search command. 697If optional search-command/string are given, 698use those instead of the ones saved." 699 (interactive "p") 700 (if (null search-command) (setq search-command vi-search-last-command)) 701 (if (null search-string) 702 (setq search-string 703 (car (if (memq search-command 704 '(re-search-forward re-search-backward)) 705 regexp-search-ring 706 search-ring)))) 707 (if (null search-command) 708 (progn (ding) (message "No last search command to repeat.")) 709 (funcall search-command search-string nil nil arg))) 710 711(defun vi-reverse-last-search (arg &optional search-command search-string) 712 "Redo last search command in reverse direction. 713If the optional search args are given, use those instead of the ones saved." 714 (interactive "p") 715 (if (null search-command) (setq search-command vi-search-last-command)) 716 (if (null search-string) 717 (setq search-string 718 (car (if (memq search-command 719 '(re-search-forward re-search-backward)) 720 regexp-search-ring 721 search-ring)))) 722 (if (null search-command) 723 (progn (ding) (message "No last search command to repeat.")) 724 (funcall (cond ((eq search-command 're-search-forward) 're-search-backward) 725 ((eq search-command 're-search-backward) 're-search-forward) 726 ((eq search-command 'search-forward) 'search-backward) 727 ((eq search-command 'search-backward) 'search-forward)) 728 search-string nil nil arg))) 729 730(defun vi-join-lines (arg) 731 "join ARG lines from current line (default 2), cleaning up white space." 732 (interactive "P") 733 (if (null (vi-raw-numeric-prefix arg)) 734 (delete-indentation t) 735 (let ((count (vi-prefix-numeric-value arg))) 736 (while (>= count 2) 737 (delete-indentation t) 738 (setq count (1- count))))) 739 (vi-set-last-change-command 'vi-join-lines arg)) 740 741(defun vi-backward-kill-line () 742 "kill the current line. Only works in insert state." 743 (interactive) 744 (if (not vi-insert-state) 745 nil 746 (beginning-of-line 1) 747 (kill-line nil))) 748 749(defun vi-abort-ins () 750 "abort insert state, kill inserted text and go back to command state." 751 (interactive) 752 (if (not vi-insert-state) 753 nil 754 (if (> (point) vi-ins-point) 755 (kill-region vi-ins-point (point))) 756 (vi-goto-command-state t))) 757 758(defun vi-backward-windowful (count) 759 "Backward COUNT windowfuls. Default is one." 760 (interactive "p") 761; (set-mark-command nil) 762 (while (> count 0) 763 (scroll-down nil) 764 (setq count (1- count)))) 765 766(defun vi-scroll-down-window (count) 767 "Scrolls down window COUNT lines. 768If COUNT is nil (actually, non-integer), scrolls default amount. 769The given COUNT is remembered for future scrollings." 770 (interactive "P") 771 (if (integerp count) 772 (setq vi-scroll-amount count)) 773 (scroll-up vi-scroll-amount)) 774 775(defun vi-expose-line-below (count) 776 "Expose COUNT more lines below the current window. Default COUNT is 1." 777 (interactive "p") 778 (scroll-up count)) 779 780(defun vi-forward-windowful (count) 781 "Forward COUNT windowfuls. Default is one." 782 (interactive "p") 783; (set-mark-command nil) 784 (while (> count 0) 785 (scroll-up nil) 786 (setq count (1- count)))) 787 788(defun vi-next-line (count) 789 "Go down count lines, try to keep at the same column." 790 (interactive "p") 791 (setq this-command 'next-line) ; this is a needed trick 792 (if (= (point) (progn (line-move count) (point))) 793 (ding) ; no moving, already at end of buffer 794 (setq last-command 'next-line))) 795 796(defun vi-next-line-first-nonwhite (count) 797 "Go down COUNT lines. Stop at first non-white." 798 (interactive "p") 799 (if (= (point) (progn (forward-line count) (back-to-indentation) (point))) 800 (ding))) ; no moving, already at end of buffer 801 802(defun vi-previous-line-first-nonwhite (count) 803 "Go up COUNT lines. Stop at first non-white." 804 (interactive "p") 805 (forward-line (- count)) 806 (back-to-indentation)) 807 808(defun vi-scroll-up-window (count) 809 "Scrolls up window COUNT lines. 810If COUNT is nil (actually, non-integer), scrolls default amount. 811The given COUNT is remembered for future scrollings." 812 (interactive "P") 813 (if (integerp count) 814 (setq vi-scroll-amount count)) 815 (scroll-down vi-scroll-amount)) 816 817(defun vi-expose-line-above (count) 818 "Expose COUNT more lines above the current window. Default COUNT is 1." 819 (interactive "p") 820 (scroll-down count)) 821 822(defun vi-char-argument (arg) 823 "Get following character (could be any CHAR) as part of the prefix argument. 824Possible prefix-arg cases are nil, INTEGER, (nil . CHAR) or (INTEGER . CHAR)." 825 (interactive "P") 826 (let ((char (read-char))) 827 (cond ((null arg) (setq prefix-arg (cons nil char))) 828 ((integerp arg) (setq prefix-arg (cons arg char))) 829 ; This can happen only if the user changed his/her mind for CHAR, 830 ; Or there are some leading "universal-argument"s 831 (t (setq prefix-arg (cons (car arg) char)))))) 832 833(defun vi-goto-mark (mark-char &optional line-flag) 834 "Go to marked position or line (if line-flag is given). 835Goto mark `@' means jump into and pop the top mark on the mark ring." 836 (cond ((char-equal mark-char last-command-event) ; `` or '' 837 (exchange-point-and-mark) (if line-flag (back-to-indentation))) 838 ((char-equal mark-char ?@) ; jump and pop mark 839 (set-mark-command t) (if line-flag (back-to-indentation))) 840 (t 841 (let ((mark (vi-get-mark mark-char))) 842 (if (null mark) 843 (progn (vi-ding) (message "Mark register undefined.")) 844 (set-mark-command nil) 845 (goto-char mark) 846 (if line-flag (back-to-indentation))))))) 847 848(defun vi-goto-line-mark (char) 849 "Go to the line (at first non-white) marked by next char." 850 (interactive "c") 851 (vi-goto-mark char t)) 852 853(defun vi-goto-char-mark (char) 854 "Go to the char position marked by next mark-char." 855 (interactive "c") 856 (vi-goto-mark char)) 857 858(defun vi-digit-argument (arg) 859 "Set numeric prefix argument." 860 (interactive "P") 861 (cond ((null arg) (digit-argument arg)) 862 ((integerp arg) (digit-argument nil) 863 (setq prefix-arg (* prefix-arg arg))) 864 (t (digit-argument nil) ; in (NIL . CHAR) or (NUM . CHAR) form 865 (setq prefix-arg (cons (* prefix-arg 866 (if (null (car arg)) 1 (car arg))) 867 (cdr arg)))))) 868 869(defun vi-raw-numeric-prefix (arg) 870 "Return the raw value of numeric part prefix argument." 871 (if (consp arg) (car arg) arg)) 872 873(defun vi-prefix-numeric-value (arg) 874 "Return numeric meaning of the raw prefix argument. This is a modification 875to the standard one provided in `callint.c' to handle (_ . CHAR) cases." 876 (cond ((null arg) 1) 877 ((integerp arg) arg) 878 ((consp arg) (if (car arg) (car arg) 1)))) 879 880(defun vi-reverse-last-find-char (count &optional find-arg) 881 "Reverse last f F t T operation COUNT times. If the optional FIND-ARG 882is given, it is used instead of the saved one." 883 (interactive "p") 884 (if (null find-arg) (setq find-arg vi-last-find-char)) 885 (if (null find-arg) 886 (progn (ding) (message "No last find char to repeat.")) 887 (vi-find-char (cons (* (car find-arg) -1) (cdr find-arg)) count))) ;6/13/86 888 889(defun vi-find-char (arg count) 890 "Find in DIRECTION (1/-1) for CHAR of COUNT'th times on current line. 891If UPTO-FLAG is T, stop before the char. ARG = (DIRECTION.CHAR.UPTO-FLAG." 892 (let* ((direction (car arg)) (char (car (cdr arg))) 893 (upto-flag (cdr (cdr arg))) (pos (+ (point) direction))) 894 (if (catch 'exit-find-char 895 (while t 896 (cond ((null (char-after pos)) (throw 'exit-find-char nil)) 897 ((char-equal (char-after pos) ?\n) (throw 'exit-find-char nil)) 898 ((char-equal char (char-after pos)) (setq count (1- count)) 899 (if (= count 0) 900 (throw 'exit-find-char 901 (if upto-flag 902 (setq pos (- pos direction)) 903 pos))))) 904 (setq pos (+ pos direction)))) 905 (goto-char pos) 906 (ding)))) 907 908(defun vi-repeat-last-find-char (count &optional find-arg) 909 "Repeat last f F t T operation COUNT times. If optional FIND-ARG is given, 910it is used instead of the saved one." 911 (interactive "p") 912 (if (null find-arg) (setq find-arg vi-last-find-char)) 913 (if (null find-arg) 914 (progn (ding) (message "No last find char to repeat.")) 915 (vi-find-char find-arg count))) 916 917(defun vi-backward-find-char (count char) 918 "Find the COUNT'th CHAR backward on current line." 919 (interactive "p\nc") 920 (setq vi-last-find-char (cons -1 (cons char nil))) 921 (vi-repeat-last-find-char count)) 922 923(defun vi-forward-find-char (count char) 924 "Find the COUNT'th CHAR forward on current line." 925 (interactive "p\nc") 926 (setq vi-last-find-char (cons 1 (cons char nil))) 927 (vi-repeat-last-find-char count)) 928 929(defun vi-backward-upto-char (count char) 930 "Find upto the COUNT'th CHAR backward on current line." 931 (interactive "p\nc") 932 (setq vi-last-find-char (cons -1 (cons char t))) 933 (vi-repeat-last-find-char count)) 934 935(defun vi-forward-upto-char (count char) 936 "Find upto the COUNT'th CHAR forward on current line." 937 (interactive "p\nc") 938 (setq vi-last-find-char (cons 1 (cons char t))) 939 (vi-repeat-last-find-char count)) 940 941(defun vi-end-of-word (count) 942 "Move forward until encountering the end of a word. 943With argument, do this that many times." 944 (interactive "p") 945 (if (not (eobp)) (forward-char)) 946 (if (re-search-forward "\\W*\\w+\\>" nil t count) 947 (backward-char))) 948 949(defun vi-replace-1-char (count char) 950 "Replace char after point by CHAR. Repeat COUNT times." 951 (interactive "p\nc") 952 (delete-char count nil) ; don't save in kill ring 953 (setq last-command-event char) 954 (self-insert-command count) 955 (vi-set-last-change-command 'vi-replace-1-char count char)) 956 957(defun vi-replace-chars (arg) 958 "Replace chars over old ones." 959 (interactive "*p") 960 (overwrite-mode 1) 961 (vi-goto-insert-state arg)) 962 963(defun vi-substitute-chars (count) 964 "Substitute COUNT chars by the input chars, enter insert state." 965 (interactive "*p") 966 (vi-goto-insert-state 1 (list (function (lambda (c) ; this is a bit tricky 967 (delete-region (point) 968 (+ (point) c)))) 969 count) t)) 970 971(defun vi-substitute-lines (count) 972 "Substitute COUNT lines by the input chars. (=cc in vi)" 973 (interactive "*p") 974 (vi-goto-insert-state 1 (list 'vi-delete-op 'next-line (1- count)) t)) 975 976(defun vi-prefix-char-value (arg) 977 "Get the char part of the current prefix argument." 978 (cond ((null arg) nil) 979 ((integerp arg) nil) 980 ((consp arg) (cdr arg)) 981 (t nil))) 982 983(defun vi-operator (arg) 984 "Handling vi operators (d/c/</>/!/=/y). Current implementation requires 985the key bindings of the operators being fixed." 986 (interactive "P") 987 (catch 'vi-exit-op 988 (let ((this-op-char last-command-event)) 989 (setq last-command-event (read-char)) 990 (setq this-command (lookup-key vi-com-map (char-to-string last-command-event))) 991 (if (not (eq this-command 'vi-digit-argument)) 992 (setq prefix-arg arg) 993 (vi-digit-argument arg) 994 (setq last-command-event (read-char)) 995 (setq this-command (lookup-key vi-com-map (char-to-string last-command-event)))) 996 (cond ((char-equal this-op-char last-command-event) ; line op 997 (vi-execute-op this-op-char 'next-line 998 (cons (1- (vi-prefix-numeric-value prefix-arg)) 999 (vi-prefix-char-value prefix-arg)))) 1000 ;; We assume any command that has no property 'point-moving-unit' 1001 ;; as having that property with the value 'CHAR'. 3/12/86 1002 (t ;; (get this-command 'point-moving-unit) 1003 (vi-execute-op this-op-char this-command prefix-arg)))))) 1004 ;; (t (throw 'vi-exit-op (ding))))))) 1005 1006(defun vi-execute-op (op-char motion-command arg) 1007 "Execute vi edit operator as specified by OP-CHAR, the operand is the region 1008determined by the MOTION-COMMAND with ARG." 1009 (cond ((= op-char ?d) 1010 (if (vi-delete-op motion-command arg) 1011 (vi-set-last-change-command 'vi-delete-op (vi-repeat-command-of motion-command) arg))) 1012 ((= op-char ?c) 1013 (if (vi-delete-op motion-command arg) 1014 (vi-goto-insert-state 1 (list 'vi-delete-op 1015 (vi-repeat-command-of motion-command) arg) nil))) 1016 ((= op-char ?y) 1017 (if (vi-yank-op motion-command arg) 1018 (vi-set-last-change-command 'vi-yank-op (vi-repeat-command-of motion-command) arg))) 1019 ((= op-char ?!) 1020 (if (vi-shell-op motion-command arg) 1021 (vi-set-last-change-command 'vi-shell-op (vi-repeat-command-of motion-command) arg vi-last-shell-command))) 1022 ((= op-char ?<) 1023 (if (vi-shift-op motion-command arg (- vi-shift-width)) 1024 (vi-set-last-change-command 'vi-shift-op (vi-repeat-command-of motion-command) arg (- vi-shift-width)))) 1025 ((= op-char ?>) 1026 (if (vi-shift-op motion-command arg vi-shift-width) 1027 (vi-set-last-change-command 'vi-shift-op (vi-repeat-command-of motion-command) arg vi-shift-width))) 1028 ((= op-char ?=) 1029 (if (vi-indent-op motion-command arg) 1030 (vi-set-last-change-command 'vi-indent-op (vi-repeat-command-of motion-command) arg))) 1031 ((= op-char ?\\) 1032 (vi-narrow-op motion-command arg)))) 1033 1034(defun vi-repeat-command-of (command) 1035 "Return the command for redo the given command." 1036 (let ((cmd-type (get command 'point-moving-unit))) 1037 (cond ((eq cmd-type 'search) 'vi-repeat-last-search) 1038 ((eq cmd-type 'find) 'vi-repeat-last-find-char) 1039 (t command)))) 1040 1041(defun vi-effective-range (motion-command arg) 1042 "Return (begin . end) of the range spanned by executing the given 1043MOTION-COMMAND with ARG. 1044 MOTION-COMMAND in ready-to-eval list form is not yet supported." 1045 (save-excursion 1046 (let ((begin (point)) end opoint 1047 (moving-unit (get motion-command 'point-moving-unit))) 1048 (setq prefix-arg arg) 1049 (setq opoint (point)) 1050 (command-execute motion-command nil) 1051;; Check if there is any effective motion. Note that for single line operation 1052;; the motion-command causes no effective point movement (since it moves up or 1053;; down zero lines), but it should be counted as effectively moved. 1054 (if (and (= (point) opoint) (not (eq moving-unit 'line))) 1055 (cons opoint opoint) ; no effective motion 1056 (if (eq moving-unit 'region) 1057 (setq begin (or (mark) (point)))) 1058 (if (<= begin (point)) 1059 (setq end (point)) 1060 (setq end begin) 1061 (setq begin (point))) 1062 (cond ((or (eq moving-unit 'match) (eq moving-unit 'find)) 1063 (setq end (1+ end))) 1064 ((eq moving-unit 'line) 1065 (goto-char begin) (beginning-of-line) (setq begin (point)) 1066 (goto-char end) (forward-line 1) (beginning-of-line) (setq end (point)))) 1067 (if (> end (point-max)) (setq end (point-max))) ; force in buffer region 1068 (cons begin end))))) 1069 1070(defun vi-delete-op (motion-command arg) 1071 "Delete range specified by MOTION-COMMAND with ARG." 1072 (let* ((range (vi-effective-range motion-command arg)) 1073 (begin (car range)) (end (cdr range)) reg) 1074 (if (= begin end) 1075 nil ; point not moved, abort op 1076 (setq reg (vi-prefix-char-value arg)) 1077 (if (null reg) 1078 (kill-region begin end) ; kill ring as unnamed registers 1079 (if (and (>= reg ?A) (<= reg ?Z)) 1080 (append-to-register (downcase reg) begin end t) 1081 (copy-to-register reg begin end t))) 1082 t))) 1083 1084(defun vi-yank-op (motion-command arg) 1085 "Yank (in vi sense) range specified by MOTION-COMMAND with ARG." 1086 (let* ((range (vi-effective-range motion-command arg)) 1087 (begin (car range)) (end (cdr range)) reg) 1088 (if (= begin end) 1089 nil ; point not moved, abort op 1090 (setq reg (vi-prefix-char-value arg)) 1091 (if (null reg) 1092 (copy-region-as-kill begin end); kill ring as unnamed registers 1093 (if (and (>= reg ?A) (<= reg ?Z)) 1094 (append-to-register (downcase reg) begin end nil) 1095 (copy-to-register reg begin end nil))) 1096 t))) 1097 1098(defun vi-yank-line (arg) 1099 "Yank (in vi sense) lines (= `yy' command)." 1100 (interactive "*P") 1101 (setq arg (cons (1- (vi-prefix-numeric-value arg)) (vi-prefix-char-value arg))) 1102 (if (vi-yank-op 'next-line arg) 1103 (vi-set-last-change-command 'vi-yank-op 'next-line arg))) 1104 1105(defun vi-string-end-with-nl-p (string) 1106 "See if STRING ends with a newline char. 1107Used in checking whether the yanked text should be put back as lines or not." 1108 (= (aref string (1- (length string))) ?\n)) 1109 1110(defun vi-put-before (arg &optional after-p) 1111 "Put yanked (in vi sense) text back before/above cursor. 1112If a numeric prefix value (currently it should be >1) is given, put back 1113text as lines. If the optional after-p is given, put after/below the cursor." 1114 (interactive "P") 1115 (let ((reg (vi-prefix-char-value arg)) put-text) 1116 (if (and reg (or (< reg ?1) (> reg ?9)) (null (get-register reg))) 1117 (error "Nothing in register %c" reg) 1118 (if (null reg) (setq reg ?1)) ; the default is the last text killed 1119 (setq put-text 1120 (cond 1121 ((and (>= reg ?1) (<= reg ?9)) 1122 (setq this-command 'yank) ; So we may yank-pop !! 1123 (current-kill (- reg ?0 1) 'do-not-rotate)) 1124 ((stringp (get-register reg)) (get-register reg)) 1125 (t (error "Register %c is not containing text string" reg)))) 1126 (if (vi-string-end-with-nl-p put-text) ; put back text as lines 1127 (if after-p 1128 (progn (forward-line 1) (beginning-of-line)) 1129 (beginning-of-line)) 1130 (if after-p (forward-char 1))) 1131 (push-mark) 1132 (insert put-text) 1133 (exchange-point-and-mark) 1134;; (back-to-indentation) ; this is not allowed if we allow yank-pop 1135 (vi-set-last-change-command 'vi-put-before arg after-p)))) 1136 1137(defun vi-put-after (arg) 1138 "Put yanked (in vi sense) text back after/below cursor." 1139 (interactive "P") 1140 (vi-put-before arg t)) 1141 1142(defun vi-shell-op (motion-command arg &optional shell-command) 1143 "Perform shell command (as filter). 1144Performs command on range specified by MOTION-COMMAND 1145with ARG. If SHELL-COMMAND is not given, ask for one from minibuffer. 1146If char argument is given, it directs the output to a *temp* buffer." 1147 (let* ((range (vi-effective-range motion-command arg)) 1148 (begin (car range)) (end (cdr range))) 1149 (if (= begin end) 1150 nil ; point not moved, abort op 1151 (cond ((null shell-command) 1152 (setq shell-command (read-string "!" nil)) 1153 (setq vi-last-shell-command shell-command))) 1154 (shell-command-on-region begin end shell-command (not (vi-prefix-char-value arg)) 1155 (not (vi-prefix-char-value arg))) 1156 t))) 1157 1158(defun vi-shift-op (motion-command arg amount) 1159 "Perform shift command on range specified by MOTION-COMMAND with ARG for 1160AMOUNT on each line. Negative amount means shift left. 1161SPECIAL FEATURE: char argument can be used to specify shift amount(1-9)." 1162 (let* ((range (vi-effective-range motion-command arg)) 1163 (begin (car range)) (end (cdr range))) 1164 (if (= begin end) 1165 nil ; point not moved, abort op 1166 (if (vi-prefix-char-value arg) 1167 (setq amount (if (> amount 0) 1168 (- (vi-prefix-char-value arg) ?0) 1169 (- ?0 (vi-prefix-char-value arg))))) 1170 (indent-rigidly begin end amount) 1171 t))) 1172 1173(defun vi-indent-op (motion-command arg) 1174 "Perform indent command on range specified by MOTION-COMMAND with ARG." 1175 (let* ((range (vi-effective-range motion-command arg)) 1176 (begin (car range)) (end (cdr range))) 1177 (if (= begin end) 1178 nil ; point not moved, abort op 1179 (indent-region begin end nil) ; insert TAB as indent command 1180 t))) 1181 1182(defun vi-narrow-op (motion-command arg) 1183 "Narrow to region specified by MOTION-COMMAND with ARG." 1184 (let* ((range (vi-effective-range motion-command arg)) 1185 (begin (car range)) (end (cdr range)) reg) 1186 (if (= begin end) 1187 nil ; point not moved, abort op 1188 (narrow-to-region begin end)))) 1189 1190(defun vi-get-mark (char) 1191 "Return contents of vi mark register named CHAR, or nil if undefined." 1192 (cdr (assq char vi-mark-alist))) 1193 1194(defun vi-set-mark (char) 1195 "Set contents of vi mark register named CHAR to current point. 1196'@' is the special anonymous mark register." 1197 (interactive "c") 1198 (if (char-equal char ?@) 1199 (set-mark-command nil) 1200 (let ((aelt (assq char vi-mark-alist))) 1201 (if aelt 1202 (move-marker (cdr aelt) (point)) ; fixed 6/12/86 1203 (setq aelt (cons char (point-marker))) 1204 (setq vi-mark-alist (cons aelt vi-mark-alist)))))) 1205 1206(defun vi-find-matching-paren () 1207 "Locate the matching paren. It's a hack right now." 1208 (interactive) 1209 (cond ((looking-at "[[({]") (forward-sexp 1) (backward-char 1)) 1210 ((looking-at "[])}]") (forward-char 1) (backward-sexp 1)) 1211 (t (ding)))) 1212 1213(defun vi-backward-blank-delimited-word (count) 1214 "Backward COUNT blank-delimited words." 1215 (interactive "p") 1216 (if (re-search-backward "[ \t\n`][^ \t\n`]+" nil t count) 1217 (if (not (bobp)) (forward-char 1)))) 1218 1219(defun vi-forward-blank-delimited-word (count) 1220 "Forward COUNT blank-delimited words." 1221 (interactive "p") 1222 (if (re-search-forward "[^ \t\n]*[ \t\n]+[^ \t\n]" nil t count) 1223 (if (not (eobp)) (backward-char 1)))) 1224 1225(defun vi-end-of-blank-delimited-word (count) 1226 "Forward to the end of the COUNT'th blank-delimited word." 1227 (interactive "p") 1228 (if (re-search-forward "[^ \t\n\']+[ \t\n\']" nil t count) 1229 (if (not (eobp)) (backward-char 2)))) 1230 1231(defun vi-home-window-line (arg) 1232 "To window home or arg'th line from the top of the window." 1233 (interactive "p") 1234 (move-to-window-line (1- arg)) 1235 (back-to-indentation)) 1236 1237(defun vi-last-window-line (arg) 1238 "To window last line or arg'th line from the bottom of the window." 1239 (interactive "p") 1240 (move-to-window-line (- arg)) 1241 (back-to-indentation)) 1242 1243(defun vi-middle-window-line () 1244 "To the middle line of the window." 1245 (interactive) 1246 (move-to-window-line nil) 1247 (back-to-indentation)) 1248 1249(defun vi-forward-word (count) 1250 "Stop at the beginning of the COUNT'th words from point." 1251 (interactive "p") 1252 (if (re-search-forward "\\w*\\W+\\<" nil t count) 1253 t 1254 (vi-ding))) 1255 1256(defun vi-set-last-change-command (fun &rest args) 1257 "Set (FUN . ARGS) as the `last-change-command'." 1258 (setq vi-last-change-command (cons fun args))) 1259 1260(defun vi-redo-last-change-command (count &optional command) 1261 "Redo last change command COUNT times. If the optional COMMAND is given, 1262it is used instead of the current `last-change-command'." 1263 (interactive "p") 1264 (if (null command) 1265 (setq command vi-last-change-command)) 1266 (if (null command) 1267 (message "No last change command available.") 1268 (while (> count 0) 1269 (apply (car command) (cdr command)) 1270 (setq count (1- count))))) 1271 1272(defun vi-kill-char (count) 1273 "Kill COUNT chars from current point." 1274 (interactive "*p") 1275 (delete-char count t) ; save in kill ring 1276 (vi-set-last-change-command 'delete-char count t)) 1277 1278(defun vi-transpose-objects (arg unit) 1279 "Transpose objects. 1280The following char specifies unit of objects to be 1281transposed -- \"c\" for chars, \"l\" for lines, \"w\" for words, \"s\" for 1282 sexp, \"p\" for paragraph. 1283For the use of the prefix-arg, refer to individual functions called." 1284 (interactive "*P\nc") 1285 (if (char-equal unit ??) 1286 (progn 1287 (message "Transpose: c(har), l(ine), p(aragraph), s(-exp), w(ord),") 1288 (setq unit (read-char)))) 1289 (vi-set-last-change-command 'vi-transpose-objects arg unit) 1290 (cond ((char-equal unit ?c) (transpose-chars arg)) 1291 ((char-equal unit ?l) (transpose-lines (vi-prefix-numeric-value arg))) 1292 ((char-equal unit ?p) (transpose-paragraphs (vi-prefix-numeric-value arg))) 1293 ((char-equal unit ?s) (transpose-sexps (vi-prefix-numeric-value arg))) 1294 ((char-equal unit ?w) (transpose-words (vi-prefix-numeric-value arg))) 1295 (t (vi-transpose-objects arg ??)))) 1296 1297(defun vi-query-replace (arg) 1298 "Query replace, use regexp version if ARG is non-nil." 1299 (interactive "*P") 1300 (let ((rcmd (if arg 'query-replace-regexp 'query-replace))) 1301 (call-interactively rcmd nil))) 1302 1303(defun vi-replace (arg) 1304 "Replace strings, use regexp version if ARG is non-nil." 1305 (interactive "*P") 1306 (let ((rcmd (if arg 'replace-regexp 'replace-string))) 1307 (call-interactively rcmd nil))) 1308 1309(defun vi-adjust-window (arg position) 1310 "Move current line to the top/center/bottom of the window." 1311 (interactive "p\nc") 1312 (cond ((char-equal position ?\r) (recenter 0)) 1313 ((char-equal position ?-) (recenter -1)) 1314 ((char-equal position ?.) (recenter (/ (window-height) 2))) 1315 (t (message "Move current line to: \\r(top) -(bottom) .(middle)") 1316 (setq position (read-char)) 1317 (vi-adjust-window arg position)))) 1318 1319(defun vi-goto-column (col) 1320 "Go to given column of the current line." 1321 (interactive "p") 1322 (let ((opoint (point))) 1323 (beginning-of-line) 1324 (while (> col 1) 1325 (if (eolp) 1326 (setq col 0) 1327 (forward-char 1) 1328 (setq col (1- col)))) 1329 (if (= col 1) 1330 t 1331 (goto-char opoint) 1332 (ding)))) 1333 1334(defun vi-name-last-change-or-macro (arg char) 1335 "Give name to the last change command or just defined kbd macro. 1336If prefix ARG is given, name last macro, otherwise name last change command. 1337The following CHAR will be the name for the command or macro." 1338 (interactive "P\nc") 1339 (if arg 1340 (name-last-kbd-macro (intern (char-to-string char))) 1341 (if (eq (car vi-last-change-command) 'vi-first-redo-insertion) 1342 (let* ((args (cdr vi-last-change-command)) ; save the insertion text 1343 (str (buffer-substring (nth 0 args) (nth 1 args))) 1344 (overwrite-p (nth 2 args)) 1345 (prefix-code (nth 3 args))) 1346 (vi-set-last-change-command 'vi-more-redo-insertion str 1347 overwrite-p prefix-code))) 1348 (fset (intern (char-to-string char)) vi-last-change-command))) 1349 1350(defun vi-call-named-change-or-macro (count char) 1351 "Execute COUNT times the keyboard macro definition named by the following CHAR." 1352 (interactive "p\nc") 1353 (if (stringp (symbol-function (intern (char-to-string char)))) 1354 (execute-kbd-macro (intern (char-to-string char)) count) 1355 (vi-redo-last-change-command count (symbol-function (intern (char-to-string char)))))) 1356 1357(defun vi-change-case (arg) ; could be made as an operator ? 1358 "Change the case of the char after point." 1359 (interactive "*p") 1360 (catch 'exit 1361 (if (looking-at "[a-z]") 1362 (upcase-region (point) (+ (point) arg)) 1363 (if (looking-at "[A-Z]") 1364 (downcase-region (point) (+ (point) arg)) 1365 (ding) 1366 (throw 'exit nil))) 1367 (vi-set-last-change-command 'vi-change-case arg) ;should avoid redundant save 1368 (forward-char arg))) 1369 1370(defun vi-ask-for-info (char) 1371 "Inquire status info. The next CHAR will specify the particular info requested." 1372 (interactive "c") 1373 (cond ((char-equal char ?l) (what-line)) 1374 ((char-equal char ?c) (what-cursor-position)) 1375 ((char-equal char ?p) (what-page)) 1376 (t (message "Ask for: l(ine number), c(ursor position), p(age number)") 1377 (setq char (read-char)) 1378 (vi-ask-for-info char)))) 1379 1380(declare-function c-mark-function "cc-cmds" ()) 1381 1382(defun vi-mark-region (arg region) 1383 "Mark region appropriately. The next char REGION is d(efun),s(-exp),b(uffer), 1384p(aragraph), P(age), f(unction in C/Pascal etc.), w(ord), e(nd of sentence), 1385l(ines)." 1386 (interactive "p\nc") 1387 (cond ((char-equal region ?d) (mark-defun)) 1388 ((char-equal region ?s) (mark-sexp arg)) 1389 ((char-equal region ?b) (with-no-warnings (mark-whole-buffer))) 1390 ((char-equal region ?p) (mark-paragraph)) 1391 ((char-equal region ?P) (mark-page arg)) 1392 ((char-equal region ?f) (c-mark-function)) 1393 ((char-equal region ?w) (mark-word arg)) 1394 ((char-equal region ?e) (mark-end-of-sentence arg)) 1395 ((char-equal region ?l) (vi-mark-lines arg)) 1396 (t (message "Mark: d(efun),s(-exp),b(uf),p(arag),P(age),f(unct),w(ord),e(os),l(ines)") 1397 (setq region (read-char)) 1398 (vi-mark-region arg region)))) 1399 1400(defun vi-mark-lines (num) 1401 "Mark NUM of lines from current line as current region." 1402 (beginning-of-line 1) 1403 (push-mark) 1404 (end-of-line num)) 1405 1406(defun vi-verify-spelling (arg unit) 1407 "Verify spelling for the objects specified by char UNIT : [b(uffer), 1408r(egion), s(tring), w(ord) ]." 1409 (interactive "P\nc") 1410 (setq prefix-arg arg) ; seems not needed 1411 (cond ((char-equal unit ?b) (call-interactively 'spell-buffer)) 1412 ((char-equal unit ?r) (call-interactively 'spell-region)) 1413 ((char-equal unit ?s) (call-interactively 'spell-string)) 1414 ((char-equal unit ?w) (call-interactively 'spell-word)) 1415 (t (message "Spell check: b(uffer), r(egion), s(tring), w(ord)") 1416 (setq unit (read-char)) 1417 (vi-verify-spelling arg unit)))) 1418 1419(defun vi-do-old-mode-C-c-command (arg) 1420 "This is a hack for accessing mode specific C-c commands in vi-mode." 1421 (interactive "P") 1422 (let ((cmd (lookup-key vi-mode-old-local-map 1423 (concat "\C-c" (char-to-string (read-char)))))) 1424 (if (catch 'exit-vi-mode ; kludge hack due to dynamic binding 1425 ; of case-fold-search 1426 (if (null cmd) 1427 (progn (ding) nil) 1428 (let ((case-fold-search vi-mode-old-case-fold)) ; a hack 1429 (setq prefix-arg arg) 1430 (command-execute cmd nil) 1431 nil))) 1432 (progn 1433 (vi-back-to-old-mode) 1434 (setq prefix-arg arg) 1435 (command-execute cmd nil))))) 1436 1437(defun vi-quote-words (arg char) 1438 "Quote ARG words from the word point is on with pattern specified by CHAR. 1439Currently, CHAR could be [,{,(,\",',`,<,*, etc." 1440 (interactive "*p\nc") 1441 (while (not (string-match "[[({<\"'`*]" (char-to-string char))) 1442 (message "Enter any of [,{,(,<,\",',`,* as quoting character.") 1443 (setq char (read-char))) 1444 (vi-set-last-change-command 'vi-quote-words arg char) 1445 (if (not (looking-at "\\<")) (forward-word -1)) 1446 (insert char) 1447 (cond ((char-equal char ?\[) (setq char ?\])) 1448 ((char-equal char ?{) (setq char ?})) 1449 ((char-equal char ?<) (setq char ?>)) 1450 ((char-equal char ?\() (setq char ?\))) 1451 ((char-equal char ?`) (setq char ?'))) 1452 (vi-end-of-word arg) 1453 (forward-char 1) 1454 (insert char)) 1455 1456(defun vi-locate-def () 1457 "Locate definition in current file for the name before the point. 1458It assumes a `(def..' always starts at the beginning of a line." 1459 (interactive) 1460 (let (name) 1461 (save-excursion 1462 (setq name (buffer-substring (progn (vi-backward-blank-delimited-word 1) 1463 (skip-chars-forward "^a-zA-Z") 1464 (point)) 1465 (progn (vi-end-of-blank-delimited-word 1) 1466 (forward-char) 1467 (skip-chars-backward "^a-zA-Z") 1468 (point))))) 1469 (set-mark-command nil) 1470 (goto-char (point-min)) 1471 (if (re-search-forward (concat "^(def[unvarconst ]*" name) nil t) 1472 nil 1473 (ding) 1474 (message "No definition for \"%s\" in current file." name) 1475 (set-mark-command t)))) 1476 1477(defun vi-split-open-line (arg) 1478 "Insert a newline and leave point before it. 1479With ARG, inserts that many newlines." 1480 (interactive "*p") 1481 (vi-goto-insert-state 1 1482 (list (function (lambda (arg) 1483 (let ((flag (and (bolp) (not (bobp))))) 1484 (if flag (forward-char -1)) 1485 (while (> arg 0) 1486 (save-excursion 1487 (insert ?\n) 1488 (if fill-prefix (insert fill-prefix))) 1489 (setq arg (1- arg))) 1490 (if flag (forward-char 1))))) arg) 1491 t)) 1492 1493(provide 'vi) 1494 1495;;; vi.el ends here 1496