viper-keym.el 29.4 KB
Newer Older
Michael Kifer's avatar
Michael Kifer committed
1
;;; viper-keym.el --- Viper keymaps
Richard M. Stallman's avatar
Richard M. Stallman committed
2

3
;; Copyright (C) 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
4
;;   2005, 2006, 2007 Free Software Foundation, Inc.
Karl Heuer's avatar
Karl Heuer committed
5

6
;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7

Karl Heuer's avatar
Karl Heuer committed
8 9 10 11
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
12
;; the Free Software Foundation; either version 3, or (at your option)
Karl Heuer's avatar
Karl Heuer committed
13 14 15 16 17 18 19 20
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
21
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
22 23
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Karl Heuer's avatar
Karl Heuer committed
24

25 26 27
;;; Commentary:

;;; Code:
Michael Kifer's avatar
Michael Kifer committed
28

Michael Kifer's avatar
Michael Kifer committed
29 30 31
(provide 'viper-keym)

;; compiler pacifier
Michael Kifer's avatar
Michael Kifer committed
32
(defvar viper-always)
Michael Kifer's avatar
Michael Kifer committed
33 34
(defvar viper-current-state)
(defvar viper-mode-string)
Michael Kifer's avatar
Michael Kifer committed
35
(defvar viper-expert-level)
Michael Kifer's avatar
Michael Kifer committed
36
(defvar viper-ex-style-editing)
Michael Kifer's avatar
Michael Kifer committed
37
(defvar viper-ex-style-motion)
Michael Kifer's avatar
Michael Kifer committed
38

Michael Kifer's avatar
Michael Kifer committed
39 40 41 42 43 44
;; loading happens only in non-interactive compilation
;; in order to spare non-viperized emacs from being viperized
(if noninteractive
    (eval-when-compile
      (let ((load-path (cons (expand-file-name ".") load-path)))
	(or (featurep 'viper-util)
Glenn Morris's avatar
Glenn Morris committed
45
	    (load "viper-util.el" nil t 'nosuffix))
Michael Kifer's avatar
Michael Kifer committed
46
	)))
Michael Kifer's avatar
Michael Kifer committed
47 48
;; end pacifier

Karl Heuer's avatar
Karl Heuer committed
49 50
(require 'viper-util)

51 52 53
(declare-function viper-ex "viper-ex" (arg &optional string))
(declare-function viper-normalize-minor-mode-map-alist "viper-cmd" ())
(declare-function viper-set-mode-vars-for "viper-cmd" (state))
Michael Kifer's avatar
Michael Kifer committed
54

Karl Heuer's avatar
Karl Heuer committed
55 56
;;; Variables

57 58

;;; Emacs keys in other states.
Michael Kifer's avatar
Michael Kifer committed
59

Michael Kifer's avatar
Michael Kifer committed
60
(defcustom viper-want-emacs-keys-in-insert t
Michael Kifer's avatar
Michael Kifer committed
61
  "*Set to nil if you want complete Vi compatibility in insert mode.
Michael Kifer's avatar
Michael Kifer committed
62 63 64
Complete compatibility with Vi is not recommended for power use of Viper."
  :type 'boolean
  :group 'viper)
Michael Kifer's avatar
Michael Kifer committed
65

Michael Kifer's avatar
Michael Kifer committed
66
(defcustom viper-want-emacs-keys-in-vi t
Michael Kifer's avatar
Michael Kifer committed
67
  "*Set to nil if you want complete Vi compatibility in Vi mode.
Michael Kifer's avatar
Michael Kifer committed
68 69 70
Full Vi compatibility is not recommended for power use of Viper."
  :type 'boolean
  :group 'viper)
Michael Kifer's avatar
Michael Kifer committed
71

Michael Kifer's avatar
Michael Kifer committed
72
(defcustom viper-no-multiple-ESC  t
Michael Kifer's avatar
Michael Kifer committed
73 74
  "*If true, multiple ESC in Vi mode will cause bell to ring.
This is set to t on a windowing terminal and to 'twice on a dumb
Michael Kifer's avatar
Michael Kifer committed
75
terminal (unless the user level is 1, 2, or 5).  On a dumb terminal, this
Michael Kifer's avatar
Michael Kifer committed
76 77
enables cursor keys and is generally more convenient, as terminals usually
don't have a convenient Meta key.
Michael Kifer's avatar
Michael Kifer committed
78
Setting viper-no-multiple-ESC to nil will allow as many multiple ESC,
Michael Kifer's avatar
Michael Kifer committed
79 80
as is allowed by the major mode in effect."
  :type 'boolean
81
  :group 'viper)
Michael Kifer's avatar
Michael Kifer committed
82

Michael Kifer's avatar
Michael Kifer committed
83
(defcustom viper-want-ctl-h-help nil
Michael Kifer's avatar
Michael Kifer committed
84
  "*If non-nil, C-h gets bound to help-command; otherwise, C-h gets the usual Vi bindings."
Michael Kifer's avatar
Michael Kifer committed
85 86
  :type 'boolean
  :group 'viper)
Michael Kifer's avatar
Michael Kifer committed
87 88


Karl Heuer's avatar
Karl Heuer committed
89 90 91 92
;;; Keymaps

;; Keymaps for vital things like \e and C-z.
;; Not for users
Michael Kifer's avatar
Michael Kifer committed
93 94 95
(defvar viper-vi-intercept-map (make-sparse-keymap))
(defvar viper-insert-intercept-map (make-sparse-keymap))
(defvar viper-emacs-intercept-map (make-sparse-keymap))
96

Michael Kifer's avatar
Michael Kifer committed
97 98
;; keymap used to zap all keymaps other than function-key-map,
;; device-function-key-map, etc.
Michael Kifer's avatar
Michael Kifer committed
99
(defvar viper-overriding-map (make-sparse-keymap))
100

Michael Kifer's avatar
Michael Kifer committed
101
(viper-deflocalvar viper-vi-local-user-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
102 103 104
  "Keymap for user-defined local bindings.
Useful for changing bindings such as ZZ in certain major modes.
For instance, in letter-mode, one may want to bind ZZ to
Michael Kifer's avatar
Michael Kifer committed
105
mh-send-letter.  In a newsreader such as gnus, tin, or rn, ZZ could be bound
Karl Heuer's avatar
Karl Heuer committed
106
to save-buffers-kill-emacs then post article, etc.")
107
(put 'viper-vi-local-user-map 'permanent-local t)
Karl Heuer's avatar
Karl Heuer committed
108

Michael Kifer's avatar
Michael Kifer committed
109
(defvar viper-vi-global-user-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
110 111 112
  "Keymap for user-defined global bindings.
These bindings are seen in all Viper buffers.")

Michael Kifer's avatar
Michael Kifer committed
113
(defvar viper-vi-basic-map (make-keymap)
Karl Heuer's avatar
Karl Heuer committed
114 115 116
  "This is the main keymap in effect in Viper's Vi state.
This map is global, shared by all buffers.")

Michael Kifer's avatar
Michael Kifer committed
117
(defvar  viper-vi-kbd-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
118 119
  "This keymap keeps keyboard macros defined via the :map command.")

Michael Kifer's avatar
Michael Kifer committed
120
(defvar viper-vi-diehard-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
121
  "This keymap is in use when the user asks Viper to simulate Vi very closely.
Michael Kifer's avatar
Michael Kifer committed
122
This happens when viper-expert-level is 1 or 2.  See viper-set-expert-level.")
123

Karl Heuer's avatar
Karl Heuer committed
124

Michael Kifer's avatar
Michael Kifer committed
125
(viper-deflocalvar viper-insert-local-user-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
126
  "Auxiliary map for per-buffer user-defined keybindings in Insert state.")
127
(put 'viper-insert-local-user-map 'permanent-local t)
Karl Heuer's avatar
Karl Heuer committed
128

Michael Kifer's avatar
Michael Kifer committed
129
(defvar viper-insert-global-user-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
130 131
  "Auxiliary map for global user-defined bindings in Insert state.")

Michael Kifer's avatar
Michael Kifer committed
132
(defvar viper-insert-basic-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
133 134
  "The basic insert-mode keymap.")

Michael Kifer's avatar
Michael Kifer committed
135
(defvar viper-insert-diehard-map (make-keymap)
Karl Heuer's avatar
Karl Heuer committed
136
  "Map used when user wants vi-style keys in insert mode.
Michael Kifer's avatar
Michael Kifer committed
137 138
Most of the Emacs keys are suppressed.  This map overshadows
viper-insert-basic-map.  Not recommended, except for novice users.")
Karl Heuer's avatar
Karl Heuer committed
139

Michael Kifer's avatar
Michael Kifer committed
140
(defvar  viper-insert-kbd-map  (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
141 142
  "This keymap keeps VI-style kbd macros for insert mode.")

Michael Kifer's avatar
Michael Kifer committed
143
(defvar viper-replace-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
144
  "Map used in Viper's replace state.")
145

Michael Kifer's avatar
Michael Kifer committed
146
(defvar viper-emacs-global-user-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
147 148
  "Auxiliary map for global user-defined bindings in Emacs state.")

Michael Kifer's avatar
Michael Kifer committed
149
(defvar  viper-emacs-kbd-map  (make-sparse-keymap)
150
  "This keymap keeps Vi-style kbd macros for Emacs mode.")
151

Michael Kifer's avatar
Michael Kifer committed
152
(viper-deflocalvar viper-emacs-local-user-map  (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
153
  "Auxiliary map for local user-defined bindings in Emacs state.")
154
(put 'viper-emacs-local-user-map 'permanent-local t)
Karl Heuer's avatar
Karl Heuer committed
155 156

;; This keymap should stay empty
Michael Kifer's avatar
Michael Kifer committed
157
(defvar viper-empty-keymap (make-sparse-keymap))
Karl Heuer's avatar
Karl Heuer committed
158

159
;; This was the main Vi mode in old versions of VIP which may have been
Michael Kifer's avatar
Michael Kifer committed
160
;; extensively used by VIP users.  We declare it as a global var
Michael Kifer's avatar
Michael Kifer committed
161 162
;; and, after .viper is loaded, we add this keymap to viper-vi-basic-map.
(defvar viper-mode-map (make-sparse-keymap))
163

164 165
;; Some important keys used in viper
(defcustom viper-toggle-key [(control ?z)]  ; "\C-z"
166
  "The key used to change states from Emacs to Vi and back.
167 168
In insert mode, this key also functions as Meta.

169
Enter as a sexp.  Examples: \"\\C-z\", [(control ?z)]."
170 171 172 173 174 175
  :type 'sexp
  :group 'viper
  :set (lambda (symbol value)
	 (let ((old-value (if (boundp 'viper-toggle-key)
			      viper-toggle-key
			    [(control ?z)])))
176
	   (mapc
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
	    (lambda (buf)
	      (save-excursion
		(set-buffer buf)
		(when (and (boundp 'viper-insert-basic-map)
			   (keymapp viper-insert-basic-map))
		  (when old-value
		    (define-key viper-insert-basic-map old-value nil))
		  (define-key viper-insert-basic-map value 'viper-escape-to-vi))
		(when (and (boundp 'viper-vi-intercept-map)
			   (keymapp viper-vi-intercept-map))
		  (when old-value
		    (define-key viper-vi-intercept-map old-value nil))
		  (define-key
		    viper-vi-intercept-map value 'viper-toggle-key-action))
		(when (and (boundp 'viper-emacs-intercept-map)
			   (keymapp viper-emacs-intercept-map))
		  (define-key viper-emacs-intercept-map old-value nil)
		  (define-key
		    viper-emacs-intercept-map value 'viper-change-state-to-vi))
		))
	    (buffer-list))
	   (set-default symbol value)
           )))

(defcustom viper-quoted-insert-key "\C-v"
  "The key used to quote special characters when inserting them in Insert state."
  :type 'string
  :group 'viper)

206
(defcustom viper-ESC-key (if (viper-window-display-p) [(escape)] "\e")
207
  "Key used to ESC.
208 209
Enter as a sexp. Examples: \"\\e\", [(escape)].
If running in a terminal, [(escape)] is not understood, so must use \"\\e\"."
210 211 212 213 214 215
  :type 'sexp
  :group 'viper
  :set (lambda (symbol value)
	 (let ((old-value (if (boundp 'viper-ESC-key)
			      viper-ESC-key
			    [(escape)])))
216
	   (mapc
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	    (lambda (buf)
	      (save-excursion
		(set-buffer buf)
		(when (and (boundp 'viper-insert-intercept-map)
			   (keymapp viper-insert-intercept-map))
		  (when old-value
		    (define-key viper-insert-intercept-map old-value nil))
		  (define-key
		    viper-insert-intercept-map value 'viper-intercept-ESC-key))
		(when (and (boundp 'viper-vi-intercept-map)
			   (keymapp viper-vi-intercept-map))
		  (when old-value
		    (define-key viper-vi-intercept-map old-value nil))
		  (define-key
		    viper-vi-intercept-map value 'viper-intercept-ESC-key))
		))
	    (buffer-list))
	   (set-default symbol value)
           )))

Karl Heuer's avatar
Karl Heuer committed
237 238 239

;;; Variables used by minor modes

240
;; Association list of the form
Karl Heuer's avatar
Karl Heuer committed
241 242 243
;; ((major-mode . keymap) (major-mode . keymap) ...)
;; Viper uses these keymaps to make user-requested adjustments
;; to its Vi state in various major modes.")
Michael Kifer's avatar
Michael Kifer committed
244
(defvar viper-vi-state-modifier-alist nil)
Karl Heuer's avatar
Karl Heuer committed
245

246
;; Association list of the form
Karl Heuer's avatar
Karl Heuer committed
247 248 249
;; ((major-mode . keymap) (major-mode . keymap) ...)
;; Viper uses these keymaps to make user-requested adjustments
;; to its Insert state in various major modes.")
Michael Kifer's avatar
Michael Kifer committed
250
(defvar viper-insert-state-modifier-alist nil)
Karl Heuer's avatar
Karl Heuer committed
251

252
;; Association list of the form
Karl Heuer's avatar
Karl Heuer committed
253 254 255
;; ((major-mode . keymap) (major-mode . keymap) ...)
;; Viper uses these keymaps to make user-requested adjustments
;; to its Emacs state in various major modes.
Michael Kifer's avatar
Michael Kifer committed
256
(defvar viper-emacs-state-modifier-alist nil)
Karl Heuer's avatar
Karl Heuer committed
257

258 259 260 261
;; The list of viper keymaps. Set by viper-normalize-minor-mode-map-alist
(viper-deflocalvar viper--key-maps nil)
(viper-deflocalvar viper--intercept-key-maps nil)

Michael Kifer's avatar
Michael Kifer committed
262
;; Tells viper-add-local-keys to create a new viper-vi-local-user-map for new
Michael Kifer's avatar
Michael Kifer committed
263
;; buffers.  Not a user option.
Michael Kifer's avatar
Michael Kifer committed
264 265
(viper-deflocalvar viper-need-new-vi-local-map t "")
(put 'viper-need-new-vi-local-map  'permanent-local t)
Karl Heuer's avatar
Karl Heuer committed
266

Michael Kifer's avatar
Michael Kifer committed
267
;; Tells viper-add-local-keys to create a new viper-insert-local-user-map for
Michael Kifer's avatar
Michael Kifer committed
268
;; new buffers.  Not a user option.
Michael Kifer's avatar
Michael Kifer committed
269 270
(viper-deflocalvar viper-need-new-insert-local-map t "")
(put 'viper-need-new-insert-local-map  'permanent-local t)
Karl Heuer's avatar
Karl Heuer committed
271

Michael Kifer's avatar
Michael Kifer committed
272
;; Tells viper-add-local-keys to create a new viper-emacs-local-user-map for
Michael Kifer's avatar
Michael Kifer committed
273
;; new buffers.  Not a user option.
Michael Kifer's avatar
Michael Kifer committed
274 275
(viper-deflocalvar viper-need-new-emacs-local-map t "")
(put 'viper-need-new-emacs-local-map  'permanent-local t)
Karl Heuer's avatar
Karl Heuer committed
276 277 278 279 280 281



;; Insert mode keymap

;; for novice users, pretend you are the real vi.
Michael Kifer's avatar
Michael Kifer committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
(define-key viper-insert-diehard-map "\t"   'viper-insert-tab)
(define-key viper-insert-diehard-map "\C-a" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-b" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-c" 'viper-change-state-to-vi)
(define-key viper-insert-diehard-map "\C-e" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-f" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-g" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-i" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-k" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-l" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-n" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-o" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-p" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-q" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-r" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-s" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-u" 'viper-erase-line)
(define-key viper-insert-diehard-map "\C-x" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-y" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-z" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-]" 'self-insert-command)
(define-key viper-insert-diehard-map "\C-_" 'self-insert-command)
Karl Heuer's avatar
Karl Heuer committed
304 305 306

(let ((i ?\ ))
  (while (<= i ?~)
Michael Kifer's avatar
Michael Kifer committed
307
    (define-key viper-insert-diehard-map (make-string 1 i) 'self-insert-command)
Karl Heuer's avatar
Karl Heuer committed
308 309 310
    (setq i (1+ i))))

;; Insert mode map when user wants emacs style
Michael Kifer's avatar
Michael Kifer committed
311 312 313
(define-key viper-insert-basic-map "\C-d" 'viper-backward-indent)
(define-key viper-insert-basic-map "\C-w" 'viper-delete-backward-word)
(define-key viper-insert-basic-map "\C-t" 'viper-forward-indent)
314
(define-key viper-insert-basic-map viper-quoted-insert-key 'quoted-insert)
Michael Kifer's avatar
Michael Kifer committed
315
(define-key viper-insert-basic-map "\C-?" 'viper-del-backward-char-in-insert)
Michael Kifer's avatar
Michael Kifer committed
316
(define-key viper-insert-basic-map [backspace] 'viper-del-backward-char-in-insert)
Michael Kifer's avatar
Michael Kifer committed
317 318 319 320 321 322
(define-key viper-insert-basic-map "\C-\\" 'viper-alternate-Meta-key)
(define-key viper-insert-basic-map viper-toggle-key 'viper-escape-to-vi)
(define-key viper-insert-basic-map "\C-c\M-p"
  'viper-insert-prev-from-insertion-ring)
(define-key viper-insert-basic-map "\C-c\M-n"
  'viper-insert-next-from-insertion-ring)
Karl Heuer's avatar
Karl Heuer committed
323 324 325


;; Replace keymap
Michael Kifer's avatar
Michael Kifer committed
326 327 328 329
(define-key viper-replace-map "\C-t" 'viper-forward-indent)
(define-key viper-replace-map "\C-j" 'viper-replace-state-carriage-return)
(define-key viper-replace-map "\C-m" 'viper-replace-state-carriage-return)
(define-key viper-replace-map "\C-?" 'viper-del-backward-char-in-replace)
Michael Kifer's avatar
Michael Kifer committed
330
(define-key viper-replace-map [backspace] 'viper-del-backward-char-in-replace)
Karl Heuer's avatar
Karl Heuer committed
331 332 333 334 335



;; Vi keymaps

Michael Kifer's avatar
Michael Kifer committed
336 337
(define-key viper-vi-basic-map "\C-^" (lambda ()
					(interactive) (viper-ex nil "e#")))
Michael Kifer's avatar
Michael Kifer committed
338 339 340 341 342 343 344
(define-key viper-vi-basic-map "\C-b" 'viper-scroll-screen-back)
(define-key viper-vi-basic-map "\C-d" 'viper-scroll-up)
(define-key viper-vi-basic-map "\C-e" 'viper-scroll-up-one)
(define-key viper-vi-basic-map "\C-f" 'viper-scroll-screen)
(define-key viper-vi-basic-map "\C-m" 'viper-next-line-at-bol)
(define-key viper-vi-basic-map "\C-u" 'viper-scroll-down)
(define-key viper-vi-basic-map "\C-y" 'viper-scroll-down-one)
345 346
;;(define-key viper-vi-basic-map "\C-s" 'viper-isearch-forward)
;;(define-key viper-vi-basic-map "\C-r" 'viper-isearch-backward)
Michael Kifer's avatar
Michael Kifer committed
347
(define-key viper-vi-basic-map "\C-c/" 'viper-toggle-search-style)
Michael Kifer's avatar
Michael Kifer committed
348
(define-key viper-vi-basic-map "\C-c\C-g" 'viper-info-on-file)
Michael Kifer's avatar
Michael Kifer committed
349 350 351 352 353 354 355 356 357 358 359

(define-key viper-vi-basic-map "\C-c\M-p" 'viper-prev-destructive-command)
(define-key viper-vi-basic-map "\C-c\M-n" 'viper-next-destructive-command)


(define-key viper-vi-basic-map " " 'viper-forward-char)
(define-key viper-vi-basic-map "!" 'viper-command-argument)
(define-key viper-vi-basic-map "\"" 'viper-command-argument)
(define-key viper-vi-basic-map "#" 'viper-command-argument)
(define-key viper-vi-basic-map "$" 'viper-goto-eol)
(define-key viper-vi-basic-map "%" 'viper-paren-match)
Michael Kifer's avatar
Michael Kifer committed
360 361
(define-key viper-vi-basic-map "&" (lambda ()
				     (interactive) (viper-ex nil "&")))
Michael Kifer's avatar
Michael Kifer committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
(define-key viper-vi-basic-map "'" 'viper-goto-mark-and-skip-white)
(define-key viper-vi-basic-map "(" 'viper-backward-sentence)
(define-key viper-vi-basic-map ")" 'viper-forward-sentence)
(define-key viper-vi-basic-map "*" 'call-last-kbd-macro)
(define-key viper-vi-basic-map "+" 'viper-next-line-at-bol)
(define-key viper-vi-basic-map "," 'viper-repeat-find-opposite)
(define-key viper-vi-basic-map "-" 'viper-previous-line-at-bol)
(define-key viper-vi-basic-map "." 'viper-repeat)
(define-key viper-vi-basic-map "/" 'viper-search-forward)

(define-key viper-vi-basic-map "0" 'viper-beginning-of-line)
(define-key viper-vi-basic-map "1" 'viper-digit-argument)
(define-key viper-vi-basic-map "2" 'viper-digit-argument)
(define-key viper-vi-basic-map "3" 'viper-digit-argument)
(define-key viper-vi-basic-map "4" 'viper-digit-argument)
(define-key viper-vi-basic-map "5" 'viper-digit-argument)
(define-key viper-vi-basic-map "6" 'viper-digit-argument)
(define-key viper-vi-basic-map "7" 'viper-digit-argument)
(define-key viper-vi-basic-map "8" 'viper-digit-argument)
(define-key viper-vi-basic-map "9" 'viper-digit-argument)

(define-key viper-vi-basic-map ":" 'viper-ex)
(define-key viper-vi-basic-map ";" 'viper-repeat-find)
(define-key viper-vi-basic-map "<" 'viper-command-argument)
(define-key viper-vi-basic-map "=" 'viper-command-argument)
(define-key viper-vi-basic-map ">" 'viper-command-argument)
(define-key viper-vi-basic-map "?" 'viper-search-backward)
(define-key viper-vi-basic-map "@" 'viper-register-macro)

(define-key viper-vi-basic-map "A" 'viper-Append)
(define-key viper-vi-basic-map "B" 'viper-backward-Word)
(define-key viper-vi-basic-map "C" 'viper-change-to-eol)
(define-key viper-vi-basic-map "D" 'viper-kill-line)
(define-key viper-vi-basic-map "E" 'viper-end-of-Word)
(define-key viper-vi-basic-map "F" 'viper-find-char-backward)
(define-key viper-vi-basic-map "G" 'viper-goto-line)
(define-key viper-vi-basic-map "H" 'viper-window-top)
(define-key viper-vi-basic-map "I" 'viper-Insert)
(define-key viper-vi-basic-map "J" 'viper-join-lines)
(define-key viper-vi-basic-map "K" 'viper-nil)
(define-key viper-vi-basic-map "L" 'viper-window-bottom)
(define-key viper-vi-basic-map "M" 'viper-window-middle)
(define-key viper-vi-basic-map "N" 'viper-search-Next)
(define-key viper-vi-basic-map "O" 'viper-Open-line)
(define-key viper-vi-basic-map "P" 'viper-Put-back)
(define-key viper-vi-basic-map "Q" 'viper-query-replace)
(define-key viper-vi-basic-map "R" 'viper-overwrite)
(define-key viper-vi-basic-map "S" 'viper-substitute-line)
(define-key viper-vi-basic-map "T" 'viper-goto-char-backward)
(define-key viper-vi-basic-map "U" 'viper-undo)
(define-key viper-vi-basic-map "V" 'find-file-other-window)
(define-key viper-vi-basic-map "W" 'viper-forward-Word)
(define-key viper-vi-basic-map "X" 'viper-delete-backward-char)
(define-key viper-vi-basic-map "Y" 'viper-yank-line)
(define-key viper-vi-basic-map "ZZ" 'viper-save-kill-buffer)

(define-key viper-vi-basic-map "\\" 'viper-escape-to-emacs)
(define-key viper-vi-basic-map "[" 'viper-brac-function)
(define-key viper-vi-basic-map "]" 'viper-ket-function)
(define-key viper-vi-basic-map "\C-\\" 'viper-alternate-Meta-key)
(define-key viper-vi-basic-map "^" 'viper-bol-and-skip-white)
(define-key viper-vi-basic-map "`" 'viper-goto-mark)

(define-key viper-vi-basic-map "a" 'viper-append)
(define-key viper-vi-basic-map "b" 'viper-backward-word)
(define-key viper-vi-basic-map "c" 'viper-command-argument)
(define-key viper-vi-basic-map "d" 'viper-command-argument)
(define-key viper-vi-basic-map "e" 'viper-end-of-word)
(define-key viper-vi-basic-map "f" 'viper-find-char-forward)
(define-key viper-vi-basic-map "g" 'viper-nil)
(define-key viper-vi-basic-map "h" 'viper-backward-char)
Michael Kifer's avatar
Michael Kifer committed
433
(define-key viper-vi-basic-map [backspace] 'viper-backward-char)
Michael Kifer's avatar
Michael Kifer committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
(define-key viper-vi-basic-map "i" 'viper-insert)
(define-key viper-vi-basic-map "j" 'viper-next-line)
(define-key viper-vi-basic-map "k" 'viper-previous-line)
(define-key viper-vi-basic-map "l" 'viper-forward-char)
(define-key viper-vi-basic-map "m" 'viper-mark-point)
(define-key viper-vi-basic-map "n" 'viper-search-next)
(define-key viper-vi-basic-map "o" 'viper-open-line)
(define-key viper-vi-basic-map "p" 'viper-put-back)
(define-key viper-vi-basic-map "q" 'viper-nil)
(define-key viper-vi-basic-map "r" 'viper-replace-char)
(define-key viper-vi-basic-map "s" 'viper-substitute)
(define-key viper-vi-basic-map "t" 'viper-goto-char-forward)
(define-key viper-vi-basic-map "u" 'viper-undo)
(define-key viper-vi-basic-map "v" 'find-file)
(define-key viper-vi-basic-map "\C-v" 'find-file-other-frame)
(define-key viper-vi-basic-map "w" 'viper-forward-word)
(define-key viper-vi-basic-map "x" 'viper-delete-char)
(define-key viper-vi-basic-map "y" 'viper-command-argument)
(define-key viper-vi-basic-map "zH" 'viper-line-to-top)
(define-key viper-vi-basic-map "zM" 'viper-line-to-middle)
(define-key viper-vi-basic-map "zL" 'viper-line-to-bottom)
(define-key viper-vi-basic-map "z\C-m" 'viper-line-to-top)
(define-key viper-vi-basic-map "z." 'viper-line-to-middle)
(define-key viper-vi-basic-map "z-" 'viper-line-to-bottom)

(define-key viper-vi-basic-map "{" 'viper-backward-paragraph)
(define-key viper-vi-basic-map "|" 'viper-goto-col)
(define-key viper-vi-basic-map "}" 'viper-forward-paragraph)
(define-key viper-vi-basic-map "~" 'viper-toggle-case)
(define-key viper-vi-basic-map "\C-?" 'viper-backward-char)
(define-key viper-vi-basic-map "_" 'viper-nil)

Michael Kifer's avatar
Michael Kifer committed
466
;;; This is viper-vi-diehard-map.  Used when viper-vi-diehard-minor-mode is on.
Michael Kifer's avatar
Michael Kifer committed
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488

(define-key viper-vi-diehard-map "\C-a" 'viper-nil)
(define-key viper-vi-diehard-map "\C-c" 'viper-nil)
(define-key viper-vi-diehard-map "\C-g" 'viper-info-on-file)
(define-key viper-vi-diehard-map "\C-i" 'viper-nil)
(define-key viper-vi-diehard-map "\C-k" 'viper-nil)
(define-key viper-vi-diehard-map "\C-l" 'redraw-display)
(define-key viper-vi-diehard-map "\C-n" 'viper-next-line)
(define-key viper-vi-diehard-map "\C-o" 'viper-nil)
(define-key viper-vi-diehard-map "\C-p" 'viper-previous-line)
(define-key viper-vi-diehard-map "\C-q" 'viper-nil)
(define-key viper-vi-diehard-map "\C-r" 'redraw-display)
(define-key viper-vi-diehard-map "\C-s" 'viper-nil)
(define-key viper-vi-diehard-map "\C-t" 'viper-nil)
(define-key viper-vi-diehard-map "\C-v" 'viper-nil)
(define-key viper-vi-diehard-map "\C-w" 'viper-nil)
(define-key viper-vi-diehard-map "@" 'viper-nil)
(define-key viper-vi-diehard-map "_" 'viper-nil)
(define-key viper-vi-diehard-map "*" 'viper-nil)
(define-key viper-vi-diehard-map "#" 'viper-nil)
(define-key viper-vi-diehard-map "\C-_" 'viper-nil)
(define-key viper-vi-diehard-map "\C-]" 'viper-nil) ; This is actually tags.
Karl Heuer's avatar
Karl Heuer committed
489 490 491


;;; Minibuffer keymap
492

Karl Heuer's avatar
Karl Heuer committed
493

Michael Kifer's avatar
Michael Kifer committed
494
(defvar viper-minibuffer-map (make-sparse-keymap)
Karl Heuer's avatar
Karl Heuer committed
495
  "Keymap used to modify keys when Minibuffer is in Insert state.")
496

Michael Kifer's avatar
Michael Kifer committed
497 498
(define-key viper-minibuffer-map "\C-m" 'viper-exit-minibuffer)
(define-key viper-minibuffer-map "\C-j" 'viper-exit-minibuffer)
Karl Heuer's avatar
Karl Heuer committed
499 500

;; Map used to read Ex-style commands.
Michael Kifer's avatar
Michael Kifer committed
501 502 503
(defvar viper-ex-cmd-map (make-sparse-keymap))
(define-key viper-ex-cmd-map " "  'ex-cmd-read-exit)
(define-key viper-ex-cmd-map "\t" 'ex-cmd-complete)
Karl Heuer's avatar
Karl Heuer committed
504 505 506

;; Keymap for reading file names in Ex-style commands.
(defvar ex-read-filename-map (make-sparse-keymap))
Michael Kifer's avatar
Michael Kifer committed
507 508
(define-key ex-read-filename-map " " 'viper-complete-filename-or-exit)
(define-key ex-read-filename-map "!" 'viper-handle-!)
Karl Heuer's avatar
Karl Heuer committed
509

Michael Kifer's avatar
Michael Kifer committed
510
;; Some other maps
Michael Kifer's avatar
Michael Kifer committed
511
(defvar viper-slash-and-colon-map (make-sparse-keymap)
Michael Kifer's avatar
Michael Kifer committed
512 513
  "This map redefines `/' and `:' to behave as in Vi.
Useful in some modes, such as Gnus, MH, etc.")
Michael Kifer's avatar
Michael Kifer committed
514 515
(define-key viper-slash-and-colon-map ":" 'viper-ex)
(define-key viper-slash-and-colon-map "/" 'viper-search-forward)
Michael Kifer's avatar
Michael Kifer committed
516

Michael Kifer's avatar
Michael Kifer committed
517
(defvar viper-comint-mode-modifier-map (make-sparse-keymap)
Michael Kifer's avatar
Michael Kifer committed
518
  "This map modifies comint mode.")
519 520
(define-key viper-comint-mode-modifier-map "\C-m" 'viper-exec-key-in-emacs)
(define-key viper-comint-mode-modifier-map "\C-d" 'viper-exec-key-in-emacs)
Michael Kifer's avatar
Michael Kifer committed
521

Michael Kifer's avatar
Michael Kifer committed
522
(defvar viper-dired-modifier-map (make-sparse-keymap)
Michael Kifer's avatar
Michael Kifer committed
523
  "This map modifies Dired behavior.")
Michael Kifer's avatar
Michael Kifer committed
524 525
(define-key viper-dired-modifier-map ":" 'viper-ex)
(define-key viper-dired-modifier-map "/" 'viper-search-forward)
Michael Kifer's avatar
Michael Kifer committed
526

527 528 529 530
(defvar viper-gnus-modifier-map (make-sparse-keymap)
  "This map modifies Gnus behavior.")
(define-key viper-gnus-modifier-map ":" 'viper-ex)

Karl Heuer's avatar
Karl Heuer committed
531 532 533 534


;;; Code

Michael Kifer's avatar
Michael Kifer committed
535
(defun viper-add-local-keys (state alist)
Karl Heuer's avatar
Karl Heuer committed
536 537 538 539 540 541 542 543
  "Override some vi-state or insert-state bindings in the current buffer.
The effect is seen in the current buffer only.
Useful for customizing  mailer buffers, gnus, etc.
STATE is 'vi-state, 'insert-state, or 'emacs-state
ALIST is of the form ((key . func) (key . func) ...)
Normally, this would be called from a hook to a major mode or
on a per buffer basis.
Usage:
Michael Kifer's avatar
Michael Kifer committed
544
      (viper-add-local-keys state '((key-str . func) (key-str . func)...))   "
545

Karl Heuer's avatar
Karl Heuer committed
546 547
  (let (map)
    (cond ((eq state 'vi-state)
Michael Kifer's avatar
Michael Kifer committed
548 549 550 551
	   (if viper-need-new-vi-local-map
	       (setq viper-vi-local-user-map (make-sparse-keymap)))
	   (setq viper-need-new-vi-local-map nil
		 map viper-vi-local-user-map))
Karl Heuer's avatar
Karl Heuer committed
552
	  ((eq state 'insert-state)
Michael Kifer's avatar
Michael Kifer committed
553 554 555 556
	   (if viper-need-new-insert-local-map
	       (setq viper-insert-local-user-map (make-sparse-keymap)))
	   (setq viper-need-new-insert-local-map nil
		 map viper-insert-local-user-map))
Karl Heuer's avatar
Karl Heuer committed
557
	  ((eq state 'emacs-state)
Michael Kifer's avatar
Michael Kifer committed
558 559 560 561
	   (if viper-need-new-emacs-local-map
	       (setq viper-emacs-local-user-map (make-sparse-keymap)))
	   (setq viper-need-new-emacs-local-map nil
		 map viper-emacs-local-user-map))
562
	  (t
Karl Heuer's avatar
Karl Heuer committed
563
	   (error
Michael Kifer's avatar
Michael Kifer committed
564
	    "Invalid state in viper-add-local-keys: %S.  Valid states: vi-state, insert-state or emacs-state" state)))
Karl Heuer's avatar
Karl Heuer committed
565

Michael Kifer's avatar
Michael Kifer committed
566 567 568
    (viper-modify-keymap map alist)
    (viper-normalize-minor-mode-map-alist)
    (viper-set-mode-vars-for viper-current-state)))
Michael Kifer's avatar
Michael Kifer committed
569

Michael Kifer's avatar
Michael Kifer committed
570 571
(defun viper-zap-local-keys ()
  "Unconditionally reset Viper viper-*-local-user-map's.
572
Rarely useful, but if you made a mistake by switching to a mode that adds
Michael Kifer's avatar
Michael Kifer committed
573 574
undesirable local keys, e.g., comint-mode, then this function can restore
sanity."
Michael Kifer's avatar
Michael Kifer committed
575
  (interactive)
Michael Kifer's avatar
Michael Kifer committed
576 577 578 579 580 581 582
  (setq viper-vi-local-user-map (make-sparse-keymap)
	viper-need-new-vi-local-map nil
	viper-insert-local-user-map (make-sparse-keymap)
	viper-need-new-insert-local-map nil
	viper-emacs-local-user-map (make-sparse-keymap)
	viper-need-new-emacs-local-map nil)
  (viper-normalize-minor-mode-map-alist))
583

Karl Heuer's avatar
Karl Heuer committed
584

Michael Kifer's avatar
Michael Kifer committed
585
(defun viper-modify-major-mode (mode state keymap)
Karl Heuer's avatar
Karl Heuer committed
586 587 588 589
  "Modify key bindings in a major-mode in a Viper state using a keymap.

If the default for a major mode is emacs-state, then modifications to this
major mode may not take effect until the buffer switches state to Vi,
Michael Kifer's avatar
Michael Kifer committed
590 591 592
Insert or Emacs.  If this happens, add viper-change-state-to-emacs to this
major mode's hook.  If no such hook exists, you may have to put an advice on
the function that invokes the major mode.  See viper-set-hooks for hints.
Karl Heuer's avatar
Karl Heuer committed
593 594 595 596

The above needs not to be done for major modes that come up in Vi or Insert
state by default.

Michael Kifer's avatar
Michael Kifer committed
597
Arguments: (major-mode viper-state keymap)"
Karl Heuer's avatar
Karl Heuer committed
598
  (let ((alist
Michael Kifer's avatar
Michael Kifer committed
599 600 601
	 (cond ((eq state 'vi-state) 'viper-vi-state-modifier-alist)
	       ((eq state 'insert-state) 'viper-insert-state-modifier-alist)
	       ((eq state 'emacs-state) 'viper-emacs-state-modifier-alist)))
Karl Heuer's avatar
Karl Heuer committed
602 603 604 605
	elt)
    (if (setq elt (assoc mode (eval alist)))
	(set alist (delq elt (eval alist))))
    (set alist (cons (cons mode keymap) (eval alist)))
606

Karl Heuer's avatar
Karl Heuer committed
607 608
    ;; Normalization usually doesn't help here, since one needs to
    ;; normalize in the actual buffer where changes to the keymap are
Michael Kifer's avatar
Michael Kifer committed
609
    ;; to take place.  However, it doesn't hurt, and it helps whenever this
Michael Kifer's avatar
Michael Kifer committed
610
    ;; function is actually called from within the affected buffer.
Michael Kifer's avatar
Michael Kifer committed
611
    (viper-normalize-minor-mode-map-alist)
612

Michael Kifer's avatar
Michael Kifer committed
613
    (viper-set-mode-vars-for viper-current-state)))
Karl Heuer's avatar
Karl Heuer committed
614

615

616
;; Displays variables that control Viper's keymaps
Michael Kifer's avatar
Michael Kifer committed
617
(defun viper-debug-keymaps ()
Karl Heuer's avatar
Karl Heuer committed
618
  (interactive)
Michael Kifer's avatar
Michael Kifer committed
619
  (with-output-to-temp-buffer " *viper-debug*"
Karl Heuer's avatar
Karl Heuer committed
620 621 622
    (princ (format "Buffer name:  %s\n\n" (buffer-name)))
    (princ "Variables:  \n")
    (princ (format "major-mode:  %S\n" major-mode))
Michael Kifer's avatar
Michael Kifer committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
    (princ (format "viper-current-state:  %S\n" viper-current-state))
    (princ (format "viper-mode-string:  %S\n\n" viper-mode-string))
    (princ (format "viper-vi-intercept-minor-mode:  %S\n"
		   viper-vi-intercept-minor-mode))
    (princ (format "viper-insert-intercept-minor-mode:  %S\n"
		   viper-insert-intercept-minor-mode))
    (princ (format "viper-emacs-intercept-minor-mode:  %S\n"
		   viper-emacs-intercept-minor-mode))
    (princ (format "viper-vi-minibuffer-minor-mode:  %S\n"
		   viper-vi-minibuffer-minor-mode))
    (princ (format "viper-insert-minibuffer-minor-mode:  %S\n\n"
		   viper-insert-minibuffer-minor-mode))
    (princ (format "viper-vi-local-user-minor-mode:  %S\n"
		   viper-vi-local-user-minor-mode))
    (princ (format "viper-vi-global-user-minor-mode:  %S\n"
		   viper-vi-global-user-minor-mode))
    (princ (format "viper-vi-kbd-minor-mode:  %S\n" viper-vi-kbd-minor-mode))
    (princ (format "viper-vi-state-modifier-minor-mode:  %S\n"
		   viper-vi-state-modifier-minor-mode))
    (princ (format "viper-vi-diehard-minor-mode:  %S\n"
		   viper-vi-diehard-minor-mode))
    (princ (format "viper-vi-basic-minor-mode:  %S\n" viper-vi-basic-minor-mode))
    (princ (format "viper-replace-minor-mode:  %S\n" viper-replace-minor-mode))
    (princ (format "viper-insert-local-user-minor-mode:  %S\n"
		   viper-insert-local-user-minor-mode))
    (princ (format "viper-insert-global-user-minor-mode:  %S\n"
		   viper-insert-global-user-minor-mode))
    (princ (format "viper-insert-kbd-minor-mode:  %S\n"
651
		   viper-insert-kbd-minor-mode))
Michael Kifer's avatar
Michael Kifer committed
652 653 654 655 656 657 658 659 660 661 662 663 664 665
    (princ (format "viper-insert-state-modifier-minor-mode:  %S\n"
		   viper-insert-state-modifier-minor-mode))
    (princ (format "viper-insert-diehard-minor-mode:  %S\n"
		   viper-insert-diehard-minor-mode))
    (princ (format "viper-insert-basic-minor-mode:  %S\n"
		   viper-insert-basic-minor-mode))
    (princ (format "viper-emacs-local-user-minor-mode:  %S\n"
		   viper-emacs-local-user-minor-mode))
    (princ (format "viper-emacs-kbd-minor-mode:  %S\n"
		   viper-emacs-kbd-minor-mode))
    (princ (format "viper-emacs-global-user-minor-mode:  %S\n"
		   viper-emacs-global-user-minor-mode))
    (princ (format "viper-emacs-state-modifier-minor-mode:  %S\n"
		   viper-emacs-state-modifier-minor-mode))
666

Michael Kifer's avatar
Michael Kifer committed
667
    (princ (format "\nviper-expert-level  %S\n" viper-expert-level))
Michael Kifer's avatar
Michael Kifer committed
668
    (princ (format "viper-no-multiple-ESC  %S\n" viper-no-multiple-ESC))
Michael Kifer's avatar
Michael Kifer committed
669
    (princ (format "viper-always  %S\n" viper-always))
Michael Kifer's avatar
Michael Kifer committed
670 671
    (princ (format "viper-ex-style-motion  %S\n"
		   viper-ex-style-motion))
Michael Kifer's avatar
Michael Kifer committed
672 673
    (princ (format "viper-ex-style-editing  %S\n"
		   viper-ex-style-editing))
Michael Kifer's avatar
Michael Kifer committed
674
    (princ (format "viper-want-emacs-keys-in-vi  %S\n"
675
		   viper-want-emacs-keys-in-vi))
Michael Kifer's avatar
Michael Kifer committed
676
    (princ (format "viper-want-emacs-keys-in-insert  %S\n"
677
		   viper-want-emacs-keys-in-insert))
Michael Kifer's avatar
Michael Kifer committed
678
    (princ (format "viper-want-ctl-h-help  %S\n" viper-want-ctl-h-help))
679

Karl Heuer's avatar
Karl Heuer committed
680 681 682 683 684 685
    (princ "\n\n\n")
    (princ (format "Default value for minor-mode-map-alist:  \n%S\n\n"
		   (default-value 'minor-mode-map-alist)))
    (princ (format "Actual value for minor-mode-map-alist:  \n%S\n"
		   minor-mode-map-alist))
    ))
686

Karl Heuer's avatar
Karl Heuer committed
687 688

;;; Keymap utils
689 690

(defun viper-add-keymap (mapsrc mapdst)
Michael Kifer's avatar
Michael Kifer committed
691
  "Add contents of mapsrc to mapdst.  It is assumed that mapsrc is sparse."
692 693 694 695 696 697 698 699
  (viper-cond-compile-for-xemacs-or-emacs
   ;; xemacs
   (map-keymap (lambda (key binding) (define-key mapdst key binding))
	       mapsrc)
   ;; emacs
   (mapcar (lambda (p) (define-key mapdst (vector (car p)) (cdr p)))
	   (cdr mapsrc))
   ))
700

Michael Kifer's avatar
Michael Kifer committed
701
(defun viper-modify-keymap (map alist)
Michael Kifer's avatar
Michael Kifer committed
702
   "Modifies MAP with bindings specified in the ALIST.  The alist has the
Karl Heuer's avatar
Karl Heuer committed
703
form ((key . function) (key . function) ... )."
704
   (mapcar (lambda (p) (define-key map (eval (car p)) (cdr p)))
Karl Heuer's avatar
Karl Heuer committed
705 706 707
	   alist))


Michael Kifer's avatar
Michael Kifer committed
708
;;; Local Variables:
Michael Kifer's avatar
Michael Kifer committed
709
;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
Michael Kifer's avatar
Michael Kifer committed
710 711 712
;;; End:


Miles Bader's avatar
Miles Bader committed
713
;;; arch-tag: 43af4b2f-0bea-400b-889e-221ebc00acb1
714
;;; viper-keym.el ends here