| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
(defgroup vip nil |
|---|
| 41 |
"A VI Package for GNU Emacs." |
|---|
| 42 |
:prefix "vip-" |
|---|
| 43 |
:group 'emulations) |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
(defvar vip-emacs-local-map nil |
|---|
| 48 |
"Local map used in Emacs mode. (Buffer-specific.)") |
|---|
| 49 |
|
|---|
| 50 |
(defvar vip-insert-local-map nil |
|---|
| 51 |
"Local map used in insert command mode. (Buffer-specific.)") |
|---|
| 52 |
|
|---|
| 53 |
(make-variable-buffer-local 'vip-emacs-local-map) |
|---|
| 54 |
(make-variable-buffer-local 'vip-insert-local-map) |
|---|
| 55 |
|
|---|
| 56 |
(defvar vip-insert-point nil |
|---|
| 57 |
"Remember insert point as a marker. (Buffer-specific.)") |
|---|
| 58 |
|
|---|
| 59 |
(set-default 'vip-insert-point (make-marker)) |
|---|
| 60 |
(make-variable-buffer-local 'vip-insert-point) |
|---|
| 61 |
|
|---|
| 62 |
(defvar vip-com-point nil |
|---|
| 63 |
"Remember com point as a marker. (Buffer-specific.)") |
|---|
| 64 |
|
|---|
| 65 |
(set-default 'vip-com-point (make-marker)) |
|---|
| 66 |
(make-variable-buffer-local 'vip-com-point) |
|---|
| 67 |
|
|---|
| 68 |
(defvar vip-current-mode nil |
|---|
| 69 |
"Current mode. One of `emacs-mode', `vi-mode', `insert-mode'.") |
|---|
| 70 |
|
|---|
| 71 |
(make-variable-buffer-local 'vip-current-mode) |
|---|
| 72 |
(setq-default vip-current-mode 'emacs-mode) |
|---|
| 73 |
|
|---|
| 74 |
(defvar vip-emacs-mode-line-buffer-identification nil |
|---|
| 75 |
"Value of mode-line-buffer-identification in Emacs mode within vip.") |
|---|
| 76 |
(make-variable-buffer-local 'vip-emacs-mode-line-buffer-identification) |
|---|
| 77 |
(setq-default vip-emacs-mode-line-buffer-identification |
|---|
| 78 |
'("Emacs: %17b")) |
|---|
| 79 |
|
|---|
| 80 |
(defvar vip-current-major-mode nil |
|---|
| 81 |
"vip-current-major-mode is the major-mode vi considers it is now. |
|---|
| 82 |
\(buffer specific\)") |
|---|
| 83 |
|
|---|
| 84 |
(make-variable-buffer-local 'vip-current-major-mode) |
|---|
| 85 |
|
|---|
| 86 |
(defvar vip-last-shell-com nil |
|---|
| 87 |
"Last shell command executed by ! command.") |
|---|
| 88 |
|
|---|
| 89 |
(defvar vip-use-register nil |
|---|
| 90 |
"Name of register to store deleted or yanked strings.") |
|---|
| 91 |
|
|---|
| 92 |
(defvar vip-d-com nil |
|---|
| 93 |
"How to reexecute last destructive command. Value is list (M-COM VAL COM).") |
|---|
| 94 |
|
|---|
| 95 |
(defcustom vip-shift-width 8 |
|---|
| 96 |
"*The number of columns shifted by > and < command." |
|---|
| 97 |
:type 'integer |
|---|
| 98 |
:group 'vip) |
|---|
| 99 |
|
|---|
| 100 |
(defcustom vip-re-replace nil |
|---|
| 101 |
"*If t then do regexp replace, if nil then do string replace." |
|---|
| 102 |
:type 'boolean |
|---|
| 103 |
:group 'vip) |
|---|
| 104 |
|
|---|
| 105 |
(defvar vip-d-char nil |
|---|
| 106 |
"The character remembered by the vi \"r\" command.") |
|---|
| 107 |
|
|---|
| 108 |
(defvar vip-f-char nil |
|---|
| 109 |
"For use by \";\" command.") |
|---|
| 110 |
|
|---|
| 111 |
(defvar vip-F-char nil |
|---|
| 112 |
"For use by \".\" command.") |
|---|
| 113 |
|
|---|
| 114 |
(defvar vip-f-forward nil |
|---|
| 115 |
"For use by \";\" command.") |
|---|
| 116 |
|
|---|
| 117 |
(defvar vip-f-offset nil |
|---|
| 118 |
"For use by \";\" command.") |
|---|
| 119 |
|
|---|
| 120 |
(defcustom vip-search-wrap-around t |
|---|
| 121 |
"*If t, search wraps around." |
|---|
| 122 |
:type 'boolean |
|---|
| 123 |
:group 'vip) |
|---|
| 124 |
|
|---|
| 125 |
(defcustom vip-re-search nil |
|---|
| 126 |
"*If t, search is reg-exp search, otherwise vanilla search." |
|---|
| 127 |
:type 'boolean |
|---|
| 128 |
:group 'vip) |
|---|
| 129 |
|
|---|
| 130 |
(defvar vip-s-string nil |
|---|
| 131 |
"Last vip search string.") |
|---|
| 132 |
|
|---|
| 133 |
(defvar vip-s-forward nil |
|---|
| 134 |
"If t, search is forward.") |
|---|
| 135 |
|
|---|
| 136 |
(defcustom vip-case-fold-search nil |
|---|
| 137 |
"*If t, search ignores cases." |
|---|
| 138 |
:type 'boolean |
|---|
| 139 |
:group 'vip) |
|---|
| 140 |
|
|---|
| 141 |
(defcustom vip-re-query-replace nil |
|---|
| 142 |
"*If t then do regexp replace, if nil then do string replace." |
|---|
| 143 |
:type 'boolean |
|---|
| 144 |
:group 'vip) |
|---|
| 145 |
|
|---|
| 146 |
(defcustom vip-open-with-indent nil |
|---|
| 147 |
"*If t, indent when open a new line." |
|---|
| 148 |
:type 'boolean |
|---|
| 149 |
:group 'vip) |
|---|
| 150 |
|
|---|
| 151 |
(defcustom vip-help-in-insert-mode nil |
|---|
| 152 |
"*If t then C-h is bound to help-command in insert mode. |
|---|
| 153 |
If nil then it is bound to `delete-backward-char'." |
|---|
| 154 |
:type 'boolean |
|---|
| 155 |
:group 'vip) |
|---|
| 156 |
|
|---|
| 157 |
(defvar vip-quote-string "> " |
|---|
| 158 |
"String inserted at the beginning of region.") |
|---|
| 159 |
|
|---|
| 160 |
(defvar vip-tags-file-name "TAGS") |
|---|
| 161 |
|
|---|
| 162 |
(defvar vip-inhibit-startup-message nil) |
|---|
| 163 |
|
|---|
| 164 |
(defvar vip-startup-file (convert-standard-filename "~/.vip") |
|---|
| 165 |
"Filename used as startup file for vip.") |
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
(defvar vip-mode-map (make-keymap)) |
|---|
| 170 |
|
|---|
| 171 |
(define-key vip-mode-map "\C-a" 'beginning-of-line) |
|---|
| 172 |
(define-key vip-mode-map "\C-b" 'vip-scroll-back) |
|---|
| 173 |
(define-key vip-mode-map "\C-c" 'vip-ctl-c) |
|---|
| 174 |
(define-key vip-mode-map "\C-d" 'vip-scroll-up) |
|---|
| 175 |
(define-key vip-mode-map "\C-e" 'vip-scroll-up-one) |
|---|
| 176 |
(define-key vip-mode-map "\C-f" 'vip-scroll) |
|---|
| 177 |
(define-key vip-mode-map "\C-g" 'vip-keyboard-quit) |
|---|
| 178 |
(define-key vip-mode-map "\C-h" 'help-command) |
|---|
| 179 |
(define-key vip-mode-map "\C-m" 'vip-scroll-back) |
|---|
| 180 |
(define-key vip-mode-map "\C-n" 'vip-other-window) |
|---|
| 181 |
(define-key vip-mode-map "\C-o" 'vip-open-line-at-point) |
|---|
| 182 |
(define-key vip-mode-map "\C-u" 'vip-scroll-down) |
|---|
| 183 |
(define-key vip-mode-map "\C-x" 'vip-ctl-x) |
|---|
| 184 |
(define-key vip-mode-map "\C-y" 'vip-scroll-down-one) |
|---|
| 185 |
(define-key vip-mode-map "\C-z" 'vip-change-mode-to-emacs) |
|---|
| 186 |
(define-key vip-mode-map "\e" 'vip-ESC) |
|---|
| 187 |
|
|---|
| 188 |
(define-key vip-mode-map " " 'vip-scroll) |
|---|
| 189 |
(define-key vip-mode-map "!" 'vip-command-argument) |
|---|
| 190 |
(define-key vip-mode-map "\"" 'vip-command-argument) |
|---|
| 191 |
(define-key vip-mode-map "#" 'vip-command-argument) |
|---|
| 192 |
(define-key vip-mode-map "$" 'vip-goto-eol) |
|---|
| 193 |
(define-key vip-mode-map "%" 'vip-paren-match) |
|---|
| 194 |
(define-key vip-mode-map "&" 'vip-nil) |
|---|
| 195 |
(define-key vip-mode-map "'" 'vip-goto-mark-and-skip-white) |
|---|
| 196 |
(define-key vip-mode-map "(" 'vip-backward-sentence) |
|---|
| 197 |
(define-key vip-mode-map ")" 'vip-forward-sentence) |
|---|
| 198 |
(define-key vip-mode-map "*" 'call-last-kbd-macro) |
|---|
| 199 |
(define-key vip-mode-map "+" 'vip-next-line-at-bol) |
|---|
| 200 |
(define-key vip-mode-map "," 'vip-repeat-find-opposite) |
|---|
| 201 |
(define-key vip-mode-map "-" 'vip-previous-line-at-bol) |
|---|
| 202 |
(define-key vip-mode-map "." 'vip-repeat) |
|---|
| 203 |
(define-key vip-mode-map "/" 'vip-search-forward) |
|---|
| 204 |
|
|---|
| 205 |
(define-key vip-mode-map "0" 'vip-beginning-of-line) |
|---|
| 206 |
(define-key vip-mode-map "1" 'vip-digit-argument) |
|---|
| 207 |
(define-key vip-mode-map "2" 'vip-digit-argument) |
|---|
| 208 |
(define-key vip-mode-map "3" 'vip-digit-argument) |
|---|
| 209 |
(define-key vip-mode-map "4" 'vip-digit-argument) |
|---|
| 210 |
(define-key vip-mode-map "5" 'vip-digit-argument) |
|---|
| 211 |
(define-key vip-mode-map "6" 'vip-digit-argument) |
|---|
| 212 |
(define-key vip-mode-map "7" 'vip-digit-argument) |
|---|
| 213 |
(define-key vip-mode-map "8" 'vip-digit-argument) |
|---|
| 214 |
(define-key vip-mode-map "9" 'vip-digit-argument) |
|---|
| 215 |
|
|---|
| 216 |
(define-key vip-mode-map ":" 'vip-ex) |
|---|
| 217 |
(define-key vip-mode-map ";" 'vip-repeat-find) |
|---|
| 218 |
(define-key vip-mode-map "<" 'vip-command-argument) |
|---|
| 219 |
(define-key vip-mode-map "=" 'vip-command-argument) |
|---|
| 220 |
(define-key vip-mode-map ">" 'vip-command-argument) |
|---|
| 221 |
(define-key vip-mode-map "?" 'vip-search-backward) |
|---|
| 222 |
(define-key vip-mode-map "@" 'vip-nil) |
|---|
| 223 |
|
|---|
| 224 |
(define-key vip-mode-map "A" 'vip-Append) |
|---|
| 225 |
(define-key vip-mode-map "B" 'vip-backward-Word) |
|---|
| 226 |
(define-key vip-mode-map "C" 'vip-ctl-c-equivalent) |
|---|
| 227 |
(define-key vip-mode-map "D" 'vip-kill-line) |
|---|
| 228 |
(define-key vip-mode-map "E" 'vip-end-of-Word) |
|---|
| 229 |
(define-key vip-mode-map "F" 'vip-find-char-backward) |
|---|
| 230 |
(define-key vip-mode-map "G" 'vip-goto-line) |
|---|
| 231 |
(define-key vip-mode-map "H" 'vip-window-top) |
|---|
| 232 |
(define-key vip-mode-map "I" 'vip-Insert) |
|---|
| 233 |
(define-key vip-mode-map "J" 'vip-join-lines) |
|---|
| 234 |
(define-key vip-mode-map "K" 'vip-kill-buffer) |
|---|
| 235 |
(define-key vip-mode-map "L" 'vip-window-bottom) |
|---|
| 236 |
(define-key vip-mode-map "M" 'vip-window-middle) |
|---|
| 237 |
(define-key vip-mode-map "N" 'vip-search-Next) |
|---|
| 238 |
(define-key vip-mode-map "O" 'vip-Open-line) |
|---|
| 239 |
(define-key vip-mode-map "P" 'vip-Put-back) |
|---|
| 240 |
(define-key vip-mode-map "Q" 'vip-query-replace) |
|---|
| 241 |
(define-key vip-mode-map "R" 'vip-replace-string) |
|---|
| 242 |
(define-key vip-mode-map "S" 'vip-switch-to-buffer-other-window) |
|---|
| 243 |
(define-key vip-mode-map "T" 'vip-goto-char-backward) |
|---|
| 244 |
(define-key vip-mode-map "U" 'vip-nil) |
|---|
| 245 |
(define-key vip-mode-map "V" 'vip-find-file-other-window) |
|---|
| 246 |
(define-key vip-mode-map "W" 'vip-forward-Word) |
|---|
| 247 |
(define-key vip-mode-map "X" 'vip-ctl-x-equivalent) |
|---|
| 248 |
(define-key vip-mode-map "Y" 'vip-yank-line) |
|---|
| 249 |
(define-key vip-mode-map "ZZ" 'save-buffers-kill-emacs) |
|---|
| 250 |
|
|---|
| 251 |
(define-key vip-mode-map "[" 'vip-nil) |
|---|
| 252 |
(define-key vip-mode-map "\\" 'vip-escape-to-emacs) |
|---|
| 253 |
(define-key vip-mode-map "]" 'vip-nil) |
|---|
| 254 |
(define-key vip-mode-map "^" 'vip-bol-and-skip-white) |
|---|
| 255 |
(define-key vip-mode-map "_" 'vip-nil) |
|---|
| 256 |
(define-key vip-mode-map "`" 'vip-goto-mark) |
|---|
| 257 |
|
|---|
| 258 |
(define-key vip-mode-map "a" 'vip-append) |
|---|
| 259 |
(define-key vip-mode-map "b" 'vip-backward-word) |
|---|
| 260 |
(define-key vip-mode-map "c" 'vip-command-argument) |
|---|
| 261 |
(define-key vip-mode-map "d" 'vip-command-argument) |
|---|
| 262 |
(define-key vip-mode-map "e" 'vip-end-of-word) |
|---|
| 263 |
(define-key vip-mode-map "f" 'vip-find-char-forward) |
|---|
| 264 |
(define-key vip-mode-map "g" 'vip-info-on-file) |
|---|
| 265 |
(define-key vip-mode-map "h" 'vip-backward-char) |
|---|
| 266 |
(define-key vip-mode-map "i" 'vip-insert) |
|---|
| 267 |
(define-key vip-mode-map "j" 'vip-next-line) |
|---|
| 268 |
(define-key vip-mode-map "k" 'vip-previous-line) |
|---|
| 269 |
(define-key vip-mode-map "l" 'vip-forward-char) |
|---|
| 270 |
(define-key vip-mode-map "m" 'vip-mark-point) |
|---|
| 271 |
(define-key vip-mode-map "n" 'vip-search-next) |
|---|
| 272 |
(define-key vip-mode-map "o" 'vip-open-line) |
|---|
| 273 |
(define-key vip-mode-map "p" 'vip-put-back) |
|---|
| 274 |
(define-key vip-mode-map "q" 'vip-nil) |
|---|
| 275 |
(define-key vip-mode-map "r" 'vip-replace-char) |
|---|
| 276 |
(define-key vip-mode-map "s" 'vip-switch-to-buffer) |
|---|
| 277 |
(define-key vip-mode-map "t" 'vip-goto-char-forward) |
|---|
| 278 |
(define-key vip-mode-map "u" 'vip-undo) |
|---|
| 279 |
(define-key vip-mode-map "v" 'vip-find-file) |
|---|
| 280 |
(define-key vip-mode-map "w" 'vip-forward-word) |
|---|
| 281 |
(define-key vip-mode-map "x" 'vip-delete-char) |
|---|
| 282 |
(define-key vip-mode-map "y" 'vip-command-argument) |
|---|
| 283 |
(define-key vip-mode-map "zH" 'vip-line-to-top) |
|---|
| 284 |
(define-key vip-mode-map "zM" 'vip-line-to-middle) |
|---|
| 285 |
(define-key vip-mode-map "zL" 'vip-line-to-bottom) |
|---|
| 286 |
(define-key vip-mode-map "z\C-m" 'vip-line-to-top) |
|---|
| 287 |
(define-key vip-mode-map "z." 'vip-line-to-middle) |
|---|
| 288 |
(define-key vip-mode-map "z-" 'vip-line-to-bottom) |
|---|
| 289 |
|
|---|
| 290 |
(define-key vip-mode-map "{" 'vip-backward-paragraph) |
|---|
| 291 |
(define-key vip-mode-map "|" 'vip-goto-col) |
|---|
| 292 |
(define-key vip-mode-map "}" 'vip-forward-paragraph) |
|---|
| 293 |
(define-key vip-mode-map "~" 'vip-nil) |
|---|
| 294 |
(define-key vip-mode-map "\177" 'vip-delete-backward-char) |
|---|
| 295 |
|
|---|
| 296 |
(defun vip-version () |
|---|
| 297 |
(interactive) |
|---|
| 298 |
(message "VIP version 3.5 of September 15, 1987")) |
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 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)) |
|---|
| 308 |
|
|---|
| 309 |
(defmacro vip-loop (count body) |
|---|
| 310 |
"(COUNT BODY) Execute BODY COUNT times." |
|---|
| 311 |
(list 'let (list (list 'count count)) |
|---|
| 312 |
(list 'while (list '> 'count 0) |
|---|
| 313 |
body |
|---|
| 314 |
(list 'setq 'count (list '1- 'count))))) |
|---|
| 315 |
|
|---|
| 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)) |
|---|
| 320 |
nil |
|---|
| 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)))) |
|---|
| 327 |
|
|---|
| 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)))) |
|---|
| 340 |
|
|---|
| 341 |
(defun vip-copy-keymap (map) |
|---|
| 342 |
(if (null map) (make-sparse-keymap) (copy-keymap map))) |
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 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)))) |
|---|
| 387 |
|
|---|
| 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)))) |
|---|
| 393 |
|
|---|
| 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"))))) |
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 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 |
|---|
| 418 |
|
|---|
| 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 |
(save-excursion |
|---|
| 425 |
(set-buffer |
|---|
| 426 |
(find-file-noselect |
|---|
| 427 |
(substitute-in-file-name vip-startup-file))) |
|---|
| 428 |
(goto-char (point-max)) |
|---|
| 429 |
(insert "\n(setq vip-inhibit-startup-message t)\n") |
|---|
| 430 |
(save-buffer) |
|---|
| 431 |
(kill-buffer (current-buffer))) |
|---|
| 432 |
(message "VIP startup message inhibited.") |
|---|
| 433 |
(sit-for 2))) |
|---|
| 434 |
(kill-buffer (current-buffer)) |
|---|
| 435 |
(message "") |
|---|
| 436 |
(setq vip-inhibit-startup-message t))) |
|---|
| 437 |
(vip-change-mode-to-vi)) |
|---|
| 438 |
|
|---|
| 439 |
(defun vip-change-mode-to-vi () |
|---|
| 440 |
"Change mode to vi mode." |
|---|
| 441 |
(interactive) |
|---|
| 442 |
(vip-change-mode 'vi-mode)) |
|---|
| 443 |
|
|---|
| 444 |
(defun vip-change-mode-to-insert () |
|---|
| 445 |
"Change mode to insert mode." |
|---|
| 446 |
(interactive) |
|---|
| 447 |
(vip-change-mode 'insert-mode)) |
|---|
| 448 |
|
|---|
| 449 |
(defun vip-change-mode-to-emacs () |
|---|
| 450 |
"Change mode to Emacs mode." |
|---|
| 451 |
(interactive) |
|---|
| 452 |
(vip-change-mode 'emacs-mode)) |
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
(defun vip-escape-to-emacs (arg &optional events) |
|---|
| 458 |
"Escape to Emacs mode for one Emacs command. |
|---|
| 459 |
ARG is used as the prefix value for the executed command. If |
|---|
| 460 |
EVENTS is a list of events, which become the beginning of the command." |
|---|
| 461 |
(interactive "P") |
|---|
| 462 |
(let (com key (old-map (current-local-map))) |
|---|
| 463 |
(if events (setq unread-command-events events)) |
|---|
| 464 |
(setq prefix-arg arg) |
|---|
| 465 |
(use-local-map vip-emacs-local-map) |
|---|
| 466 |
(unwind-protect |
|---|
| 467 |
(setq com (key-binding (setq key (read-key-sequence nil)))) |
|---|
| 468 |
(use-local-map old-map)) |
|---|
| 469 |
(command-execute com prefix-arg) |
|---|
| 470 |
(setq prefix-arg nil) |
|---|
| 471 |
)) |
|---|
| 472 |
|
|---|
| 473 |
(defun vip-message-conditions (conditions) |
|---|
| 474 |
"Print CONDITIONS as a message." |
|---|
| 475 |
(let ((case (car conditions)) (msg (cdr conditions))) |
|---|
| 476 |
(if (null msg) |
|---|
| 477 |
(message "%s" case) |
|---|
| 478 |
(message "%s %s" case (prin1-to-string msg))) |
|---|
| 479 |
(ding))) |
|---|
| 480 |
|
|---|
| 481 |
(defun vip-ESC (arg) |
|---|
| 482 |
"Emulate ESC key in Emacs mode." |
|---|
| 483 |
(interactive "P") |
|---|
| 484 |
(vip-escape-to-emacs arg '(?\e))) |
|---|
| 485 |
|
|---|
| 486 |
(defun vip-ctl-c (arg) |
|---|
| 487 |
"Emulate C-c key in Emacs mode." |
|---|
| 488 |
(interactive "P") |
|---|
| 489 |
(vip-escape-to-emacs arg '(?\C-c))) |
|---|
| 490 |
|
|---|
| 491 |
(defun vip-ctl-x (arg) |
|---|
| 492 |
"Emulate C-x key in Emacs mode." |
|---|
| 493 |
(interactive "P") |
|---|
| 494 |
(vip-escape-to-emacs arg '(?\C-x))) |
|---|
| 495 |
|
|---|
| 496 |
(defun vip-ctl-h (arg) |
|---|
| 497 |
"Emulate C-h key in Emacs mode." |
|---|
| 498 |
(interactive "P") |
|---|
| 499 |
(vip-escape-to-emacs arg '(?\C-h))) |
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
(defun vip-prefix-arg-value (char value com) |
|---|
| 509 |
"Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value |
|---|
| 510 |
obtained so far, and COM is the command part obtained so far." |
|---|
| 511 |
(while (and (>= char ?0) (<= char ?9)) |
|---|
| 512 |
(setq value (+ (* (if (numberp value) value 0) 10) (- char ?0))) |
|---|
| 513 |
(setq char (read-char))) |
|---|
| 514 |
(setq prefix-arg value) |
|---|
| 515 |
(if com (setq prefix-arg (cons prefix-arg com))) |
|---|
| 516 |
(while (= char ?U) |
|---|
| 517 |
(vip-describe-arg prefix-arg) |
|---|
| 518 |
(setq char (read-char))) |
|---|
| 519 |
(setq unread-command-events (list char))) |
|---|
| 520 |
|
|---|
| 521 |
(defun vip-prefix-arg-com (char value com) |
|---|
| 522 |
"Vi operator as prefix argument." |
|---|
| 523 |
(let ((cont t)) |
|---|
| 524 |
(while (and cont |
|---|
| 525 |
(or (= char ?c) (= char ?d) (= char ?y) |
|---|
| 526 |
(= char ?!) (= char ?<) (= char ?>) (= char ?=) |
|---|
| 527 |
(= char ?#) (= char ?r) (= char ?R) (= char ?\"))) |
|---|
| 528 |
(if com |
|---|
| 529 |
;; this means that we already have a command character, so we |
|---|
| 530 |
;; construct a com list and exit while. however, if char is " |
|---|
| 531 |
|
|---|
| 532 |
(progn |
|---|
| 533 |
|
|---|
| 534 |
(if (or (= char ?#) (= char ?\")) (error "")) |
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
, we read the name of register and store it in vip-use-register. |
|---|
| 539 |
|
|---|
| 540 |
(cond ((or (= char ?!) (= char ?=)) |
|---|
| 541 |
(setq com char) |
|---|
| 542 |
(setq char (read-char)) |
|---|
| 543 |
(setq cont nil)) |
|---|
| 544 |
((= char ?#) |
|---|
| 545 |
|
|---|
| 546 |
(setq com (+ 128 (read-char))) |
|---|
| 547 |
(setq char (read-char)) |
|---|
| 548 |
(setq cont nil)) |
|---|
| 549 |
((or (= char ?<) (= char ?>)) |
|---|
| 550 |
(setq com char) |
|---|
| 551 |
(setq char (read-char)) |
|---|
| 552 |
(if (= com char) (setq com (cons char com))) |
|---|
| 553 |
(setq cont nil)) |
|---|
| 554 |
((= char ?\") |
|---|
| 555 |
(let ((reg (read-char))) |
|---|
| 556 |
(if (or (and (<= ?A reg) (<= reg ?z)) |
|---|
| 557 |
(and (<= ?1 reg) (<= reg ?9))) |
|---|
| 558 |
(setq vip-use-register reg) |
|---|
| 559 |
(error "")) |
|---|
| 560 |
(setq char (read-char)))) |
|---|
| 561 |
(t |
|---|
| 562 |
(setq com char) |
|---|
| 563 |
(setq char (read-char))))))) |
|---|
| 564 |
(if (atom com) |
|---|
| 565 |
;; com is a single char, so we construct prefix-arg |
|---|
| 566 |
;; and if char is ?, describe prefix arg, otherwise exit by |
|---|
| 567 |
;; pushing the char back |
|---|
| 568 |
(progn |
|---|
| 569 |
(setq prefix-arg (cons value com)) |
|---|
| 570 |
(while (= char ?U) |
|---|
| 571 |
(vip-describe-arg prefix-arg) |
|---|
| 572 |
(setq char (read-char))) |
|---|
| 573 |
(setq unread-command-events (list char))) |
|---|
| 574 |
;; as com is non-nil, this means that we have a command to execute |
|---|
| 575 |
(if (or (= (car com) ?r) (= (car com) ?R)) |
|---|
| 576 |
;; execute appropriate region command. |
|---|
| 577 |
(let ((char (car com)) (com (cdr com))) |
|---|
| 578 |
(setq prefix-arg (cons value com)) |
|---|
| 579 |
(if (= char ?r) (vip-region prefix-arg) |
|---|
| 580 |
(vip-Region prefix-arg)) |
|---|
| 581 |
;; reset prefix-arg |
|---|
| 582 |
(setq prefix-arg nil)) |
|---|
| 583 |
;; otherwise, reset prefix arg and call appropriate command |
|---|
| 584 |
(setq value (if (null value) 1 value)) |
|---|
| 585 |
(setq prefix-arg nil) |
|---|
| 586 |
(cond ((equal com '(?c . ?c)) (vip-line (cons value ?C))) |
|---|
| 587 |
((equal com '(?d . ?d)) (vip-line (cons value ?D))) |
|---|
| 588 |
((equal com '(?d . ?y)) (vip-yank-defun)) |
|---|
| 589 |
((equal com '(?y . ?y)) (vip-line (cons value ?Y))) |
|---|
| 590 |
((equal com '(?< . ?<)) (vip-line (cons value ?<))) |
|---|
| 591 |
((equal com '(?> . ?>)) (vip-line (cons value ?>))) |
|---|
| 592 |
((equal com '(?! . ?!)) (vip-line (cons value ?!))) |
|---|
| 593 |
((equal com '(?= . ?=)) (vip-line (cons value ?=))) |
|---|
| 594 |
(t (error "")))))) |
|---|
| 595 |
|
|---|
| 596 |
(defun vip-describe-arg (arg) |
|---|
| 597 |
(let (val com) |
|---|
| 598 |
(setq val (vip-P-val arg) |
|---|
| 599 |
com (vip-getcom arg)) |
|---|
| 600 |
(if (null val) |
|---|
| 601 |
(if (null com) |
|---|
| 602 |
(message "Value is nil, and command is nil.") |
|---|
| 603 |
(message "Value is nil, and command is %c." com)) |
|---|
| 604 |
(if (null com) |
|---|
| 605 |
(message "Value is %d, and command is nil." val) |
|---|
| 606 |
(message "Value is %d, and command is %c." val com))))) |
|---|
| 607 |
|
|---|
| 608 |
(defun vip-digit-argument (arg) |
|---|
| 609 |
"Begin numeric argument for the next command." |
|---|
| 610 |
(interactive "P") |
|---|
| 611 |
(vip-prefix-arg-value last-command-char nil |
|---|
| 612 |
(if (consp arg) (cdr arg) nil))) |
|---|
| 613 |
|
|---|
| 614 |
(defun vip-command-argument (arg) |
|---|
| 615 |
"Accept a motion command as an argument." |
|---|
| 616 |
(interactive "P") |
|---|
| 617 |
(condition-case conditions |
|---|
| 618 |
(vip-prefix-arg-com |
|---|
| 619 |
last-command-char |
|---|
| 620 |
(cond ((null arg) nil) |
|---|
| 621 |
((consp arg) (car arg)) |
|---|
| 622 |
((numberp arg) arg) |
|---|
| 623 |
(t (error "strange arg"))) |
|---|
| 624 |
(cond ((null arg) nil) |
|---|
| 625 |
((consp arg) (cdr arg)) |
|---|
| 626 |
((numberp arg) nil) |
|---|
| 627 |
(t (error "strange arg")))) |
|---|
| 628 |
(quit |
|---|
| 629 |
(setq vip-use-register nil) |
|---|
| 630 |
(signal 'quit nil)))) |
|---|
| 631 |
|
|---|
| 632 |
(defun vip-p-val (arg) |
|---|
| 633 |
"Get value part of prefix-argument ARG." |
|---|
| 634 |
(cond ((null arg) 1) |
|---|
| 635 |
((consp arg) (if (null (car arg)) 1 (car arg))) |
|---|
| 636 |
(t arg))) |
|---|
| 637 |
|
|---|
| 638 |
(defun vip-P-val (arg) |
|---|
| 639 |
"Get value part of prefix-argument ARG." |
|---|
| 640 |
(cond ((consp arg) (car arg)) |
|---|
| 641 |
(t arg))) |
|---|
| 642 |
|
|---|
| 643 |
(defun vip-getcom (arg) |
|---|
| 644 |
"Get com part of prefix-argument ARG." |
|---|
| 645 |
(cond ((null arg) nil) |
|---|
| 646 |
((consp arg) (cdr arg)) |
|---|
| 647 |
(t nil))) |
|---|
| 648 |
|
|---|
| 649 |
(defun vip-getCom (arg) |
|---|
| 650 |
"Get com part of prefix-argument ARG and modify it." |
|---|
| 651 |
(let ((com (vip-getcom arg))) |
|---|
| 652 |
(cond ((equal com ?c) ?C) |
|---|
| 653 |
((equal com ?d) ?D) |
|---|
| 654 |
((equal com ?y) ?Y) |
|---|
| 655 |
(t com)))) |
|---|
| 656 |
|
|---|
| 657 |
|
|---|
| 658 |
;; repeat last destructive command |
|---|
| 659 |
|
|---|
| 660 |
(defun vip-append-to-register (reg start end) |
|---|
| 661 |
"Append region to text in register REG. |
|---|
| 662 |
START and END are buffer positions indicating what to append." |
|---|
| 663 |
(set-register reg (concat (or (get-register reg) "") |
|---|
| 664 |
(buffer-substring start end)))) |
|---|
| 665 |
|
|---|
| 666 |
(defun vip-execute-com (m-com val com) |
|---|
| 667 |
"(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set |
|---|
| 668 |
to vip-d-com for later use by vip-repeat" |
|---|
| 669 |
(let ((reg vip-use-register)) |
|---|
| 670 |
(if com |
|---|
| 671 |
(cond ((= com ?c) (vip-change vip-com-point (point))) |
|---|
| 672 |
((= com (- ?c)) (vip-change-subr vip-com-point (point))) |
|---|
| 673 |
((or (= com ?C) (= com (- ?C))) |
|---|
| 674 |
(save-excursion |
|---|
| 675 |
(set-mark vip-com-point) |
|---|
| 676 |
(vip-enlarge-region (mark) (point)) |
|---|
| 677 |
(if vip-use-register |
|---|
| 678 |
(progn |
|---|
| 679 |
(cond ((and (<= ?a vip-use-register) |
|---|
| 680 |
(<= vip-use-register ?z)) |
|---|
| 681 |
(copy-to-register |
|---|
| 682 |
vip-use-register (mark) (point) nil)) |
|---|
| 683 |
((and (<= ?A vip-use-register) |
|---|
| 684 |
(<= vip-use-register ?Z)) |
|---|
| 685 |
(vip-append-to-register |
|---|
| 686 |
(+ vip-use-register 32) (mark) (point))) |
|---|
| 687 |
(t (setq vip-use-register nil) |
|---|
| 688 |
(error ""))) |
|---|
| 689 |
(setq vip-use-register nil))) |
|---|
| 690 |
(delete-region (mark) (point))) |
|---|
| 691 |
(open-line 1) |
|---|
| 692 |
(if (= com ?C) (vip-change-mode-to-insert) (yank))) |
|---|
| 693 |
((= com ?d) |
|---|
| 694 |
(if vip-use-register |
|---|
| 695 |
(progn |
|---|
| 696 |
(cond ((and (<= ?a vip-use-register) |
|---|
| 697 |
(<= vip-use-register ?z)) |
|---|
| 698 |
(copy-to-register |
|---|
| 699 |
vip-use-register vip-com-point (point) nil)) |
|---|
| 700 |
((and (<= ?A vip-use-register) |
|---|
| 701 |
(<= vip-use-register ?Z)) |
|---|
| 702 |
(vip-append-to-register |
|---|
| 703 |
(+ vip-use-register 32) vip-com-point (point))) |
|---|
| 704 |
(t (setq vip-use-register nil) |
|---|
| 705 |
(error ""))) |
|---|
| 706 |
(setq vip-use-register nil))) |
|---|
| 707 |
(setq last-command |
|---|
| 708 |
(if (eq last-command 'd-command) 'kill-region nil)) |
|---|
| 709 |
(kill-region vip-com-point (point)) |
|---|
| 710 |
(setq this-command 'd-command)) |
|---|
| 711 |
((= com ?D) |
|---|
| 712 |
(save-excursion |
|---|
| 713 |
(set-mark vip-com-point) |
|---|
| 714 |
(vip-enlarge-region (mark) (point)) |
|---|
| 715 |
(if vip-use-register |
|---|
| 716 |
(progn |
|---|
| 717 |
(cond ((and (<= ?a vip-use-register) |
|---|
| 718 |
(<= vip-use-register ?z)) |
|---|
| 719 |
(copy-to-register |
|---|
| 720 |
vip-use-register (mark) (point) nil)) |
|---|
| 721 |
((and (<= ?A vip-use-register) |
|---|
| 722 |
(<= vip-use-register ?Z)) |
|---|
| 723 |
(vip-append-to-register |
|---|
| 724 |
(+ vip-use-register 32) (mark) (point))) |
|---|
| 725 |
(t (setq vip-use-register nil) |
|---|
| 726 |
(error ""))) |
|---|
| 727 |
(setq vip-use-register nil))) |
|---|
| 728 |
(setq last-command |
|---|
| 729 |
(if (eq last-command 'D-command) 'kill-region nil)) |
|---|
| 730 |
(kill-region (mark) (point)) |
|---|
| 731 |
(if (eq m-com 'vip-line) (setq this-command 'D-command))) |
|---|
| 732 |
(back-to-indentation)) |
|---|
| 733 |
((= com ?y) |
|---|
| 734 |
(if vip-use-register |
|---|
| 735 |
(progn |
|---|
| 736 |
(cond ((and (<= ?a vip-use-register) |
|---|
| 737 |
(<= vip-use-register ?z)) |
|---|
| 738 |
(copy-to-register |
|---|
| 739 |
vip-use-register vip-com-point (point) nil)) |
|---|
| 740 |
((and (<= ?A vip-use-register) |
|---|
| 741 |
(<= vip-use-register ?Z)) |
|---|
| 742 |
(vip-append-to-register |
|---|
| 743 |
(+ vip-use-register 32) vip-com-point (point))) |
|---|
| 744 |
(t (setq vip-use-register nil) |
|---|
| 745 |
(error ""))) |
|---|
| 746 |
(setq vip-use-register nil))) |
|---|
| 747 |
(setq last-command nil) |
|---|
| 748 |
(copy-region-as-kill vip-com-point (point)) |
|---|
| 749 |
(goto-char vip-com-point)) |
|---|
| 750 |
((= com ?Y) |
|---|
| 751 |
(save-excursion |
|---|
| 752 |
(set-mark vip-com-point) |
|---|
| 753 |
(vip-enlarge-region (mark) (point)) |
|---|
| 754 |
(if vip-use-register |
|---|
| 755 |
(progn |
|---|
| 756 |
(cond ((and (<= ?a vip-use-register) |
|---|
| 757 |
(<= vip-use-register ?z)) |
|---|
| 758 |
(copy-to-register |
|---|
| 759 |
vip-use-register (mark) (point) nil)) |
|---|
| 760 |
((and (<= ?A vip-use-register) |
|---|
| 761 |
(<= vip-use-register ?Z)) |
|---|
| 762 |
(vip-append-to-register |
|---|
| 763 |
(+ vip-use-register 32) (mark) (point))) |
|---|
| 764 |
(t (setq vip-use-register nil) |
|---|
| 765 |
(error ""))) |
|---|
| 766 |
(setq vip-use-register nil))) |
|---|
| 767 |
(setq last-command nil) |
|---|
| 768 |
(copy-region-as-kill (mark) (point))) |
|---|
| 769 |
(goto-char vip-com-point)) |
|---|
| 770 |
((or (= com ?!) (= com (- ?!))) |
|---|
| 771 |
(save-excursion |
|---|
| 772 |
(set-mark vip-com-point) |
|---|
| 773 |
(vip-enlarge-region (mark) (point)) |
|---|
| 774 |
(shell-command-on-region |
|---|
| 775 |
(mark) (point) |
|---|
| 776 |
(if (= com ?!) |
|---|
| 777 |
(setq vip-last-shell-com (vip-read-string "!")) |
|---|
| 778 |
vip-last-shell-com) |
|---|
| 779 |
t))) |
|---|
| 780 |
((= com ?=) |
|---|
| 781 |
(save-excursion |
|---|
| 782 |
(set-mark vip-com-point) |
|---|
| 783 |
(vip-enlarge-region (mark) (point)) |
|---|
| 784 |
(if (> (mark) (point)) (exchange-point-and-mark)) |
|---|
| 785 |
(indent-region (mark) (point) nil))) |
|---|
| 786 |
((= com ?<) |
|---|
| 787 |
(save-excursion |
|---|
| 788 |
(set-mark vip-com-point) |
|---|
| 789 |
(vip-enlarge-region (mark) (point)) |
|---|
| 790 |
(indent-rigidly (mark) (point) (- vip-shift-width))) |
|---|
| 791 |
(goto-char vip-com-point)) |
|---|
| 792 |
((= com ?>) |
|---|
| 793 |
(save-excursion |
|---|
| 794 |
(set-mark vip-com-point) |
|---|
| 795 |
(vip-enlarge-region (mark) (point)) |
|---|
| 796 |
(indent-rigidly (mark) (point) vip-shift-width)) |
|---|
| 797 |
(goto-char vip-com-point)) |
|---|
| 798 |
((>= com 128) |
|---|
| 799 |
;; this is special command # |
|---|
| 800 |
(vip-special-prefix-com (- com 128))))) |
|---|
| 801 |
(setq vip-d-com (list m-com val (if (or (= com ?c) (= com ?C) (= com ?!)) |
|---|
|
|---|