1 ;;; viper-keym.el --- Viper keymaps
3 ;; Copyright (C) 1994-1997, 2000-2011 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (defvar viper-current-state
)
30 (defvar viper-mode-string
)
31 (defvar viper-expert-level
)
32 (defvar viper-ex-style-editing
)
33 (defvar viper-ex-style-motion
)
36 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
41 (declare-function viper-ex
"viper-ex" (arg &optional string
))
42 (declare-function viper-normalize-minor-mode-map-alist
"viper-cmd" ())
43 (declare-function viper-set-mode-vars-for
"viper-cmd" (state))
48 ;;; Emacs keys in other states.
50 (defcustom viper-want-emacs-keys-in-insert t
51 "*Set to nil if you want complete Vi compatibility in insert mode.
52 Complete compatibility with Vi is not recommended for power use of Viper."
56 (defcustom viper-want-emacs-keys-in-vi t
57 "*Set to nil if you want complete Vi compatibility in Vi mode.
58 Full Vi compatibility is not recommended for power use of Viper."
62 (defcustom viper-no-multiple-ESC t
63 "*If true, multiple ESC in Vi mode will cause bell to ring.
64 This is set to t on a windowing terminal and to 'twice on a dumb
65 terminal (unless the user level is 1, 2, or 5). On a dumb terminal, this
66 enables cursor keys and is generally more convenient, as terminals usually
67 don't have a convenient Meta key.
68 Setting viper-no-multiple-ESC to nil will allow as many multiple ESC,
69 as is allowed by the major mode in effect."
73 (defcustom viper-want-ctl-h-help nil
74 "*If non-nil, C-h gets bound to help-command; otherwise, C-h gets the usual Vi bindings."
81 ;; Keymaps for vital things like \e and C-z.
83 (defvar viper-vi-intercept-map
(make-sparse-keymap))
84 (defvar viper-insert-intercept-map
(make-sparse-keymap))
85 (defvar viper-emacs-intercept-map
(make-sparse-keymap))
87 ;; keymap used to zap all keymaps other than function-key-map,
88 ;; device-function-key-map, etc.
89 (defvar viper-overriding-map
(make-sparse-keymap))
91 (viper-deflocalvar viper-vi-local-user-map
(make-sparse-keymap)
92 "Keymap for user-defined local bindings.
93 Useful for changing bindings such as ZZ in certain major modes.
94 For instance, in letter-mode, one may want to bind ZZ to
95 mh-send-letter. In a newsreader such as gnus, tin, or rn, ZZ could be bound
96 to save-buffers-kill-emacs then post article, etc.")
97 (put 'viper-vi-local-user-map
'permanent-local t
)
99 (defvar viper-vi-global-user-map
(make-sparse-keymap)
100 "Keymap for user-defined global bindings.
101 These bindings are seen in all Viper buffers.")
103 (defvar viper-vi-basic-map
(make-keymap)
104 "This is the main keymap in effect in Viper's Vi state.
105 This map is global, shared by all buffers.")
107 (defvar viper-vi-kbd-map
(make-sparse-keymap)
108 "This keymap keeps keyboard macros defined via the :map command.")
110 (defvar viper-vi-diehard-map
(make-sparse-keymap)
111 "This keymap is in use when the user asks Viper to simulate Vi very closely.
112 This happens when viper-expert-level is 1 or 2. See viper-set-expert-level.")
115 (viper-deflocalvar viper-insert-local-user-map
(make-sparse-keymap)
116 "Auxiliary map for per-buffer user-defined keybindings in Insert state.")
117 (put 'viper-insert-local-user-map
'permanent-local t
)
119 (defvar viper-insert-global-user-map
(make-sparse-keymap)
120 "Auxiliary map for global user-defined bindings in Insert state.")
122 (defvar viper-insert-basic-map
(make-sparse-keymap)
123 "The basic insert-mode keymap.")
125 (defvar viper-insert-diehard-map
(make-keymap)
126 "Map used when user wants vi-style keys in insert mode.
127 Most of the Emacs keys are suppressed. This map overshadows
128 viper-insert-basic-map. Not recommended, except for novice users.")
130 (defvar viper-insert-kbd-map
(make-sparse-keymap)
131 "This keymap keeps VI-style kbd macros for insert mode.")
133 (defvar viper-replace-map
(make-sparse-keymap)
134 "Map used in Viper's replace state.")
136 (defvar viper-emacs-global-user-map
(make-sparse-keymap)
137 "Auxiliary map for global user-defined bindings in Emacs state.")
139 (defvar viper-emacs-kbd-map
(make-sparse-keymap)
140 "This keymap keeps Vi-style kbd macros for Emacs mode.")
142 (viper-deflocalvar viper-emacs-local-user-map
(make-sparse-keymap)
143 "Auxiliary map for local user-defined bindings in Emacs state.")
144 (put 'viper-emacs-local-user-map
'permanent-local t
)
146 ;; This keymap should stay empty
147 (defvar viper-empty-keymap
(make-sparse-keymap))
149 ;; This was the main Vi mode in old versions of VIP which may have been
150 ;; extensively used by VIP users. We declare it as a global var
151 ;; and, after .viper is loaded, we add this keymap to viper-vi-basic-map.
152 (defvar viper-mode-map
(make-sparse-keymap))
154 ;; Some important keys used in viper
155 (defcustom viper-toggle-key
[(control ?z
)] ; "\C-z"
156 "The key used to change states from Emacs to Vi and back.
157 In insert mode, this key also functions as Meta.
159 Enter as a sexp. Examples: \"\\C-z\", [(control ?z)]."
162 :set
(lambda (symbol value
)
163 (let ((old-value (if (boundp 'viper-toggle-key
)
168 (with-current-buffer buf
169 (when (and (boundp 'viper-insert-basic-map
)
170 (keymapp viper-insert-basic-map
))
172 (define-key viper-insert-basic-map old-value nil
))
173 (define-key viper-insert-basic-map value
'viper-escape-to-vi
))
174 (when (and (boundp 'viper-vi-intercept-map
)
175 (keymapp viper-vi-intercept-map
))
177 (define-key viper-vi-intercept-map old-value nil
))
179 viper-vi-intercept-map value
'viper-toggle-key-action
))
180 (when (and (boundp 'viper-emacs-intercept-map
)
181 (keymapp viper-emacs-intercept-map
))
182 (define-key viper-emacs-intercept-map old-value nil
)
184 viper-emacs-intercept-map value
'viper-change-state-to-vi
))
187 (set-default symbol value
)
190 (defcustom viper-quoted-insert-key
"\C-v"
191 "The key used to quote special characters when inserting them in Insert state."
195 (defvar viper-ESC-key
(kbd "ESC")
199 ;;; Variables used by minor modes
201 ;; Association list of the form
202 ;; ((major-mode . keymap) (major-mode . keymap) ...)
203 ;; Viper uses these keymaps to make user-requested adjustments
204 ;; to its Vi state in various major modes.")
205 (defvar viper-vi-state-modifier-alist nil
)
207 ;; Association list of the form
208 ;; ((major-mode . keymap) (major-mode . keymap) ...)
209 ;; Viper uses these keymaps to make user-requested adjustments
210 ;; to its Insert state in various major modes.")
211 (defvar viper-insert-state-modifier-alist nil
)
213 ;; Association list of the form
214 ;; ((major-mode . keymap) (major-mode . keymap) ...)
215 ;; Viper uses these keymaps to make user-requested adjustments
216 ;; to its Emacs state in various major modes.
217 (defvar viper-emacs-state-modifier-alist nil
)
219 ;; The list of viper keymaps. Set by viper-normalize-minor-mode-map-alist
220 (viper-deflocalvar viper--key-maps nil
)
221 (viper-deflocalvar viper--intercept-key-maps nil
)
223 ;; Tells viper-add-local-keys to create a new viper-vi-local-user-map for new
224 ;; buffers. Not a user option.
225 (viper-deflocalvar viper-need-new-vi-local-map t
"")
226 (put 'viper-need-new-vi-local-map
'permanent-local t
)
228 ;; Tells viper-add-local-keys to create a new viper-insert-local-user-map for
229 ;; new buffers. Not a user option.
230 (viper-deflocalvar viper-need-new-insert-local-map t
"")
231 (put 'viper-need-new-insert-local-map
'permanent-local t
)
233 ;; Tells viper-add-local-keys to create a new viper-emacs-local-user-map for
234 ;; new buffers. Not a user option.
235 (viper-deflocalvar viper-need-new-emacs-local-map t
"")
236 (put 'viper-need-new-emacs-local-map
'permanent-local t
)
240 ;; Insert mode keymap
242 ;; for novice users, pretend you are the real vi.
243 (define-key viper-insert-diehard-map
"\t" 'viper-insert-tab
)
244 (define-key viper-insert-diehard-map
"\C-a" 'self-insert-command
)
245 (define-key viper-insert-diehard-map
"\C-b" 'self-insert-command
)
246 (define-key viper-insert-diehard-map
"\C-c" 'viper-change-state-to-vi
)
247 (define-key viper-insert-diehard-map
"\C-e" 'self-insert-command
)
248 (define-key viper-insert-diehard-map
"\C-f" 'self-insert-command
)
249 (define-key viper-insert-diehard-map
"\C-g" 'self-insert-command
)
250 (define-key viper-insert-diehard-map
"\C-i" 'self-insert-command
)
251 (define-key viper-insert-diehard-map
"\C-k" 'self-insert-command
)
252 (define-key viper-insert-diehard-map
"\C-l" 'self-insert-command
)
253 (define-key viper-insert-diehard-map
"\C-n" 'self-insert-command
)
254 (define-key viper-insert-diehard-map
"\C-o" 'self-insert-command
)
255 (define-key viper-insert-diehard-map
"\C-p" 'self-insert-command
)
256 (define-key viper-insert-diehard-map
"\C-q" 'self-insert-command
)
257 (define-key viper-insert-diehard-map
"\C-r" 'self-insert-command
)
258 (define-key viper-insert-diehard-map
"\C-s" 'self-insert-command
)
259 (define-key viper-insert-diehard-map
"\C-u" 'viper-erase-line
)
260 (define-key viper-insert-diehard-map
"\C-x" 'self-insert-command
)
261 (define-key viper-insert-diehard-map
"\C-y" 'self-insert-command
)
262 (define-key viper-insert-diehard-map
"\C-z" 'self-insert-command
)
263 (define-key viper-insert-diehard-map
"\C-]" 'self-insert-command
)
264 (define-key viper-insert-diehard-map
"\C-_" 'self-insert-command
)
268 (define-key viper-insert-diehard-map
(make-string 1 i
) 'self-insert-command
)
271 ;; Insert mode map when user wants emacs style
272 (define-key viper-insert-basic-map
"\C-d" 'viper-backward-indent
)
273 (define-key viper-insert-basic-map
"\C-w" 'viper-delete-backward-word
)
274 (define-key viper-insert-basic-map
"\C-t" 'viper-forward-indent
)
275 (define-key viper-insert-basic-map viper-quoted-insert-key
'quoted-insert
)
276 (define-key viper-insert-basic-map
"\C-?" 'viper-del-backward-char-in-insert
)
277 (define-key viper-insert-basic-map
[backspace] 'viper-del-backward-char-in-insert)
278 (define-key viper-insert-basic-map "\C-\\" 'viper-alternate-Meta-key)
279 (define-key viper-insert-basic-map viper-toggle-key 'viper-escape-to-vi)
280 (define-key viper-insert-basic-map "\C-c\M-p"
281 'viper-insert-prev-from-insertion-ring)
282 (define-key viper-insert-basic-map "\C-c\M-n"
283 'viper-insert-next-from-insertion-ring)
287 (define-key viper-replace-map "\C-t" 'viper-forward-indent)
288 (define-key viper-replace-map "\C-j" 'viper-replace-state-carriage-return)
289 (define-key viper-replace-map "\C-m" 'viper-replace-state-carriage-return)
290 (define-key viper-replace-map "\C-?" 'viper-del-backward-char-in-replace)
291 (define-key viper-replace-map [backspace] 'viper-del-backward-char-in-replace
)
297 (define-key viper-vi-basic-map
"\C-^" (lambda ()
298 (interactive) (viper-ex nil
"e#")))
299 (define-key viper-vi-basic-map
"\C-b" 'viper-scroll-screen-back
)
300 (define-key viper-vi-basic-map
"\C-d" 'viper-scroll-up
)
301 (define-key viper-vi-basic-map
"\C-e" 'viper-scroll-up-one
)
302 (define-key viper-vi-basic-map
"\C-f" 'viper-scroll-screen
)
303 (define-key viper-vi-basic-map
"\C-m" 'viper-next-line-at-bol
)
304 (define-key viper-vi-basic-map
"\C-u" 'viper-scroll-down
)
305 (define-key viper-vi-basic-map
"\C-y" 'viper-scroll-down-one
)
306 ;;(define-key viper-vi-basic-map "\C-s" 'viper-isearch-forward)
307 ;;(define-key viper-vi-basic-map "\C-r" 'viper-isearch-backward)
308 (define-key viper-vi-basic-map
"\C-c/" 'viper-toggle-search-style
)
309 (define-key viper-vi-basic-map
"\C-c\C-g" 'viper-info-on-file
)
311 (define-key viper-vi-basic-map
"\C-c\M-p" 'viper-prev-destructive-command
)
312 (define-key viper-vi-basic-map
"\C-c\M-n" 'viper-next-destructive-command
)
315 (define-key viper-vi-basic-map
" " 'viper-forward-char
)
316 (define-key viper-vi-basic-map
"!" 'viper-command-argument
)
317 (define-key viper-vi-basic-map
"\"" 'viper-command-argument
)
318 (define-key viper-vi-basic-map
"#" 'viper-command-argument
)
319 (define-key viper-vi-basic-map
"$" 'viper-goto-eol
)
320 (define-key viper-vi-basic-map
"%" 'viper-paren-match
)
321 (define-key viper-vi-basic-map
"&" (lambda ()
322 (interactive) (viper-ex nil
"&")))
323 (define-key viper-vi-basic-map
"'" 'viper-goto-mark-and-skip-white
)
324 (define-key viper-vi-basic-map
"(" 'viper-backward-sentence
)
325 (define-key viper-vi-basic-map
")" 'viper-forward-sentence
)
326 (define-key viper-vi-basic-map
"*" 'call-last-kbd-macro
)
327 (define-key viper-vi-basic-map
"+" 'viper-next-line-at-bol
)
328 (define-key viper-vi-basic-map
"," 'viper-repeat-find-opposite
)
329 (define-key viper-vi-basic-map
"-" 'viper-previous-line-at-bol
)
330 (define-key viper-vi-basic-map
"." 'viper-repeat
)
331 (define-key viper-vi-basic-map
"/" 'viper-search-forward
)
333 (define-key viper-vi-basic-map
"0" 'viper-beginning-of-line
)
334 (define-key viper-vi-basic-map
"1" 'viper-digit-argument
)
335 (define-key viper-vi-basic-map
"2" 'viper-digit-argument
)
336 (define-key viper-vi-basic-map
"3" 'viper-digit-argument
)
337 (define-key viper-vi-basic-map
"4" 'viper-digit-argument
)
338 (define-key viper-vi-basic-map
"5" 'viper-digit-argument
)
339 (define-key viper-vi-basic-map
"6" 'viper-digit-argument
)
340 (define-key viper-vi-basic-map
"7" 'viper-digit-argument
)
341 (define-key viper-vi-basic-map
"8" 'viper-digit-argument
)
342 (define-key viper-vi-basic-map
"9" 'viper-digit-argument
)
344 (define-key viper-vi-basic-map
":" 'viper-ex
)
345 (define-key viper-vi-basic-map
";" 'viper-repeat-find
)
346 (define-key viper-vi-basic-map
"<" 'viper-command-argument
)
347 (define-key viper-vi-basic-map
"=" 'viper-command-argument
)
348 (define-key viper-vi-basic-map
">" 'viper-command-argument
)
349 (define-key viper-vi-basic-map
"?" 'viper-search-backward
)
350 (define-key viper-vi-basic-map
"@" 'viper-register-macro
)
352 (define-key viper-vi-basic-map
"A" 'viper-Append
)
353 (define-key viper-vi-basic-map
"B" 'viper-backward-Word
)
354 (define-key viper-vi-basic-map
"C" 'viper-change-to-eol
)
355 (define-key viper-vi-basic-map
"D" 'viper-kill-line
)
356 (define-key viper-vi-basic-map
"E" 'viper-end-of-Word
)
357 (define-key viper-vi-basic-map
"F" 'viper-find-char-backward
)
358 (define-key viper-vi-basic-map
"G" 'viper-goto-line
)
359 (define-key viper-vi-basic-map
"H" 'viper-window-top
)
360 (define-key viper-vi-basic-map
"I" 'viper-Insert
)
361 (define-key viper-vi-basic-map
"J" 'viper-join-lines
)
362 (define-key viper-vi-basic-map
"K" 'viper-nil
)
363 (define-key viper-vi-basic-map
"L" 'viper-window-bottom
)
364 (define-key viper-vi-basic-map
"M" 'viper-window-middle
)
365 (define-key viper-vi-basic-map
"N" 'viper-search-Next
)
366 (define-key viper-vi-basic-map
"O" 'viper-Open-line
)
367 (define-key viper-vi-basic-map
"P" 'viper-Put-back
)
368 (define-key viper-vi-basic-map
"Q" 'viper-query-replace
)
369 (define-key viper-vi-basic-map
"R" 'viper-overwrite
)
370 (define-key viper-vi-basic-map
"S" 'viper-substitute-line
)
371 (define-key viper-vi-basic-map
"T" 'viper-goto-char-backward
)
372 (define-key viper-vi-basic-map
"U" 'viper-undo
)
373 (define-key viper-vi-basic-map
"V" 'find-file-other-window
)
374 (define-key viper-vi-basic-map
"W" 'viper-forward-Word
)
375 (define-key viper-vi-basic-map
"X" 'viper-delete-backward-char
)
376 (define-key viper-vi-basic-map
"Y" 'viper-yank-line
)
377 (define-key viper-vi-basic-map
"ZZ" 'viper-save-kill-buffer
)
379 (define-key viper-vi-basic-map
"\\" 'viper-escape-to-emacs
)
380 (define-key viper-vi-basic-map
"[" 'viper-brac-function
)
381 (define-key viper-vi-basic-map
"]" 'viper-ket-function
)
382 (define-key viper-vi-basic-map
"\C-\\" 'viper-alternate-Meta-key
)
383 (define-key viper-vi-basic-map
"^" 'viper-bol-and-skip-white
)
384 (define-key viper-vi-basic-map
"`" 'viper-goto-mark
)
386 (define-key viper-vi-basic-map
"a" 'viper-append
)
387 (define-key viper-vi-basic-map
"b" 'viper-backward-word
)
388 (define-key viper-vi-basic-map
"c" 'viper-command-argument
)
389 (define-key viper-vi-basic-map
"d" 'viper-command-argument
)
390 (define-key viper-vi-basic-map
"e" 'viper-end-of-word
)
391 (define-key viper-vi-basic-map
"f" 'viper-find-char-forward
)
392 (define-key viper-vi-basic-map
"g" 'viper-nil
)
393 (define-key viper-vi-basic-map
"h" 'viper-backward-char
)
394 (define-key viper-vi-basic-map
[backspace] 'viper-backward-char)
395 (define-key viper-vi-basic-map "i" 'viper-insert)
396 (define-key viper-vi-basic-map "j" 'viper-next-line)
397 (define-key viper-vi-basic-map "k" 'viper-previous-line)
398 (define-key viper-vi-basic-map "l" 'viper-forward-char)
399 (define-key viper-vi-basic-map "m" 'viper-mark-point)
400 (define-key viper-vi-basic-map "n" 'viper-search-next)
401 (define-key viper-vi-basic-map "o" 'viper-open-line)
402 (define-key viper-vi-basic-map "p" 'viper-put-back)
403 (define-key viper-vi-basic-map "q" 'viper-nil)
404 (define-key viper-vi-basic-map "r" 'viper-replace-char)
405 (define-key viper-vi-basic-map "s" 'viper-substitute)
406 (define-key viper-vi-basic-map "t" 'viper-goto-char-forward)
407 (define-key viper-vi-basic-map "u" 'viper-undo)
408 (define-key viper-vi-basic-map "v" 'find-file)
409 (define-key viper-vi-basic-map "\C-v" 'find-file-other-frame)
410 (define-key viper-vi-basic-map "w" 'viper-forward-word)
411 (define-key viper-vi-basic-map "x" 'viper-delete-char)
412 (define-key viper-vi-basic-map "y" 'viper-command-argument)
413 (define-key viper-vi-basic-map "zH" 'viper-line-to-top)
414 (define-key viper-vi-basic-map "zM" 'viper-line-to-middle)
415 (define-key viper-vi-basic-map "zL" 'viper-line-to-bottom)
416 (define-key viper-vi-basic-map "z\C-m" 'viper-line-to-top)
417 (define-key viper-vi-basic-map "z." 'viper-line-to-middle)
418 (define-key viper-vi-basic-map "z-" 'viper-line-to-bottom)
420 (define-key viper-vi-basic-map "{" 'viper-backward-paragraph)
421 (define-key viper-vi-basic-map "|" 'viper-goto-col)
422 (define-key viper-vi-basic-map "}" 'viper-forward-paragraph)
423 (define-key viper-vi-basic-map "~" 'viper-toggle-case)
424 (define-key viper-vi-basic-map "\C-?" 'viper-backward-char)
425 (define-key viper-vi-basic-map "_" 'viper-nil)
427 ;;; This is viper-vi-diehard-map. Used when viper-vi-diehard-minor-mode is on.
429 (define-key viper-vi-diehard-map "\C-a" 'viper-nil)
430 (define-key viper-vi-diehard-map "\C-c" 'viper-nil)
431 (define-key viper-vi-diehard-map "\C-g" 'viper-info-on-file)
432 (define-key viper-vi-diehard-map "\C-i" 'viper-nil)
433 (define-key viper-vi-diehard-map "\C-k" 'viper-nil)
434 (define-key viper-vi-diehard-map "\C-l" 'redraw-display)
435 (define-key viper-vi-diehard-map "\C-n" 'viper-next-line)
436 (define-key viper-vi-diehard-map "\C-o" 'viper-nil)
437 (define-key viper-vi-diehard-map "\C-p" 'viper-previous-line)
438 (define-key viper-vi-diehard-map "\C-q" 'viper-nil)
439 (define-key viper-vi-diehard-map "\C-r" 'redraw-display)
440 (define-key viper-vi-diehard-map "\C-s" 'viper-nil)
441 (define-key viper-vi-diehard-map "\C-t" 'viper-nil)
442 (define-key viper-vi-diehard-map "\C-v" 'viper-nil)
443 (define-key viper-vi-diehard-map "\C-w" 'viper-nil)
444 (define-key viper-vi-diehard-map "@" 'viper-nil)
445 (define-key viper-vi-diehard-map "_" 'viper-nil)
446 (define-key viper-vi-diehard-map "*" 'viper-nil)
447 (define-key viper-vi-diehard-map "#" 'viper-nil)
448 (define-key viper-vi-diehard-map "\C-_" 'viper-nil)
449 (define-key viper-vi-diehard-map "\C-]" 'viper-nil) ; This is actually tags.
452 ;;; Minibuffer keymap
455 (defvar viper-minibuffer-map (make-sparse-keymap)
456 "Keymap used to modify keys when Minibuffer is in Insert state.")
458 (define-key viper-minibuffer-map "\C-m" 'viper-exit-minibuffer)
459 (define-key viper-minibuffer-map "\C-j" 'viper-exit-minibuffer)
461 ;; Map used to read Ex-style commands.
462 (defvar viper-ex-cmd-map (make-sparse-keymap))
463 (define-key viper-ex-cmd-map " " 'ex-cmd-read-exit)
464 (define-key viper-ex-cmd-map "\t" 'ex-cmd-complete)
466 ;; Keymap for reading file names in Ex-style commands.
467 (defvar ex-read-filename-map (make-sparse-keymap))
468 (define-key ex-read-filename-map " " 'viper-complete-filename-or-exit)
469 (define-key ex-read-filename-map "!" 'viper-handle-!)
472 (defvar viper-slash-and-colon-map (make-sparse-keymap)
473 "This map redefines `/' and `:' to behave as in Vi.
474 Useful in some modes, such as Gnus, MH, etc.")
475 (define-key viper-slash-and-colon-map ":" 'viper-ex)
476 (define-key viper-slash-and-colon-map "/" 'viper-search-forward)
478 (defvar viper-comint-mode-modifier-map (make-sparse-keymap)
479 "This map modifies comint mode.")
480 (define-key viper-comint-mode-modifier-map "\C-m" 'viper-exec-key-in-emacs)
481 (define-key viper-comint-mode-modifier-map "\C-d" 'viper-exec-key-in-emacs)
483 (defvar viper-dired-modifier-map (make-sparse-keymap)
484 "This map modifies Dired behavior.")
485 (define-key viper-dired-modifier-map ":" 'viper-ex)
486 (define-key viper-dired-modifier-map "/" 'viper-search-forward)
488 (defvar viper-gnus-modifier-map (make-sparse-keymap)
489 "This map modifies Gnus behavior.")
490 (define-key viper-gnus-modifier-map ":" 'viper-ex)
496 (defun viper-add-local-keys (state alist)
497 "Override some vi-state or insert-state bindings in the current buffer.
498 The effect is seen in the current buffer only.
499 Useful for customizing mailer buffers, gnus, etc.
500 STATE is 'vi-state, 'insert-state, or 'emacs-state
501 ALIST is of the form ((key . func) (key . func) ...)
502 Normally, this would be called from a hook to a major mode or
503 on a per buffer basis.
505 (viper-add-local-keys state '((key-str . func) (key-str . func)...)) "
508 (cond ((eq state 'vi-state)
509 (if viper-need-new-vi-local-map
510 (setq viper-vi-local-user-map (make-sparse-keymap)))
511 (setq viper-need-new-vi-local-map nil
512 map viper-vi-local-user-map))
513 ((eq state 'insert-state)
514 (if viper-need-new-insert-local-map
515 (setq viper-insert-local-user-map (make-sparse-keymap)))
516 (setq viper-need-new-insert-local-map nil
517 map viper-insert-local-user-map))
518 ((eq state 'emacs-state)
519 (if viper-need-new-emacs-local-map
520 (setq viper-emacs-local-user-map (make-sparse-keymap)))
521 (setq viper-need-new-emacs-local-map nil
522 map viper-emacs-local-user-map))
525 "Invalid state in viper-add-local-keys: %S. Valid states: vi-state, insert-state or emacs-state" state)))
527 (viper-modify-keymap map alist)
528 (viper-normalize-minor-mode-map-alist)
529 (viper-set-mode-vars-for viper-current-state)))
531 (defun viper-zap-local-keys ()
532 "Unconditionally reset Viper viper-*-local-user-map's.
533 Rarely useful, but if you made a mistake by switching to a mode that adds
534 undesirable local keys, e.g., comint-mode, then this function can restore
537 (setq viper-vi-local-user-map (make-sparse-keymap)
538 viper-need-new-vi-local-map nil
539 viper-insert-local-user-map (make-sparse-keymap)
540 viper-need-new-insert-local-map nil
541 viper-emacs-local-user-map (make-sparse-keymap)
542 viper-need-new-emacs-local-map nil)
543 (viper-normalize-minor-mode-map-alist))
546 (defun viper-modify-major-mode (mode state keymap)
547 "Modify key bindings in a major-mode in a Viper state using a keymap.
549 If the default for a major mode is emacs-state, then modifications to this
550 major mode may not take effect until the buffer switches state to Vi,
551 Insert or Emacs. If this happens, add viper-change-state-to-emacs to this
552 major mode's hook. If no such hook exists, you may have to put an advice on
553 the function that invokes the major mode. See viper-set-hooks for hints.
555 The above needs not to be done for major modes that come up in Vi or Insert
558 Arguments: (major-mode viper-state keymap)"
560 (cond ((eq state 'vi-state) 'viper-vi-state-modifier-alist)
561 ((eq state 'insert-state) 'viper-insert-state-modifier-alist)
562 ((eq state 'emacs-state) 'viper-emacs-state-modifier-alist)))
564 (if (setq elt (assoc mode (eval alist)))
565 (set alist (delq elt (eval alist))))
566 (set alist (cons (cons mode keymap) (eval alist)))
568 ;; Normalization usually doesn't help here, since one needs to
569 ;; normalize in the actual buffer where changes to the keymap are
570 ;; to take place. However, it doesn't hurt, and it helps whenever this
571 ;; function is actually called from within the affected buffer.
572 (viper-normalize-minor-mode-map-alist)
574 (viper-set-mode-vars-for viper-current-state)))
577 ;; Displays variables that control Viper's keymaps
578 (defun viper-debug-keymaps ()
580 (with-output-to-temp-buffer " *viper-debug*"
581 (princ (format "Buffer name: %s\n\n" (buffer-name)))
582 (princ "Variables: \n")
583 (princ (format "major-mode: %S\n" major-mode))
584 (princ (format "viper-current-state: %S\n" viper-current-state))
585 (princ (format "viper-mode-string: %S\n\n" viper-mode-string))
586 (princ (format "viper-vi-intercept-minor-mode: %S\n"
587 viper-vi-intercept-minor-mode))
588 (princ (format "viper-insert-intercept-minor-mode: %S\n"
589 viper-insert-intercept-minor-mode))
590 (princ (format "viper-emacs-intercept-minor-mode: %S\n"
591 viper-emacs-intercept-minor-mode))
592 (princ (format "viper-vi-minibuffer-minor-mode: %S\n"
593 viper-vi-minibuffer-minor-mode))
594 (princ (format "viper-insert-minibuffer-minor-mode: %S\n\n"
595 viper-insert-minibuffer-minor-mode))
596 (princ (format "viper-vi-local-user-minor-mode: %S\n"
597 viper-vi-local-user-minor-mode))
598 (princ (format "viper-vi-global-user-minor-mode: %S\n"
599 viper-vi-global-user-minor-mode))
600 (princ (format "viper-vi-kbd-minor-mode: %S\n" viper-vi-kbd-minor-mode))
601 (princ (format "viper-vi-state-modifier-minor-mode: %S\n"
602 viper-vi-state-modifier-minor-mode))
603 (princ (format "viper-vi-diehard-minor-mode: %S\n"
604 viper-vi-diehard-minor-mode))
605 (princ (format "viper-vi-basic-minor-mode: %S\n" viper-vi-basic-minor-mode))
606 (princ (format "viper-replace-minor-mode: %S\n" viper-replace-minor-mode))
607 (princ (format "viper-insert-local-user-minor-mode: %S\n"
608 viper-insert-local-user-minor-mode))
609 (princ (format "viper-insert-global-user-minor-mode: %S\n"
610 viper-insert-global-user-minor-mode))
611 (princ (format "viper-insert-kbd-minor-mode: %S\n"
612 viper-insert-kbd-minor-mode))
613 (princ (format "viper-insert-state-modifier-minor-mode: %S\n"
614 viper-insert-state-modifier-minor-mode))
615 (princ (format "viper-insert-diehard-minor-mode: %S\n"
616 viper-insert-diehard-minor-mode))
617 (princ (format "viper-insert-basic-minor-mode: %S\n"
618 viper-insert-basic-minor-mode))
619 (princ (format "viper-emacs-local-user-minor-mode: %S\n"
620 viper-emacs-local-user-minor-mode))
621 (princ (format "viper-emacs-kbd-minor-mode: %S\n"
622 viper-emacs-kbd-minor-mode))
623 (princ (format "viper-emacs-global-user-minor-mode: %S\n"
624 viper-emacs-global-user-minor-mode))
625 (princ (format "viper-emacs-state-modifier-minor-mode: %S\n"
626 viper-emacs-state-modifier-minor-mode))
628 (princ (format "\nviper-expert-level %S\n" viper-expert-level))
629 (princ (format "viper-no-multiple-ESC %S\n" viper-no-multiple-ESC))
630 (princ (format "viper-always %S\n" viper-always))
631 (princ (format "viper-ex-style-motion %S\n"
632 viper-ex-style-motion))
633 (princ (format "viper-ex-style-editing %S\n"
634 viper-ex-style-editing))
635 (princ (format "viper-want-emacs-keys-in-vi %S\n"
636 viper-want-emacs-keys-in-vi))
637 (princ (format "viper-want-emacs-keys-in-insert %S\n"
638 viper-want-emacs-keys-in-insert))
639 (princ (format "viper-want-ctl-h-help %S\n" viper-want-ctl-h-help))
642 (princ (format "Default value for minor-mode-map-alist: \n%S\n\n"
643 (default-value 'minor-mode-map-alist)))
644 (princ (format "Actual value for minor-mode-map-alist: \n%S\n"
645 minor-mode-map-alist))
651 (defun viper-add-keymap (mapsrc mapdst)
652 "Add contents of mapsrc to mapdst. It is assumed that mapsrc is sparse."
653 (if (featurep 'xemacs)
654 ;; Emacs 22 has map-keymap.
655 (map-keymap (lambda (key binding) (define-key mapdst key binding))
657 (mapc (lambda (p) (define-key mapdst (vector (car p)) (cdr p)))
660 (defun viper-modify-keymap (map alist)
661 "Modifies MAP with bindings specified in the ALIST. The alist has the
662 form ((key . function) (key . function) ... )."
663 (mapcar (lambda (p) (define-key map (eval (car p)) (cdr p)))
667 (provide 'viper-keym)
671 ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
675 ;;; viper-keym.el ends here