Add 2010 to copyright years.
[bpt/emacs.git] / lisp / emulation / keypad.el
CommitLineData
72cc582e
KS
1;;; keypad.el --- simplified keypad bindings
2
7cb0c23b 3;; Copyright (C) 2002, 2003, 2004, 2005, 2006,
114f9c96 4;; 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
72cc582e
KS
5
6;; Author: Kim F. Storm <storm@cua.dk>
7;; Keywords: keyboard convenience
8
9;; This file is part of GNU Emacs.
10
ed0f493f 11;; GNU Emacs is free software: you can redistribute it and/or modify
72cc582e 12;; it under the terms of the GNU General Public License as published by
ed0f493f
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
72cc582e
KS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
ed0f493f 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
72cc582e
KS
23
24;;; Commentary:
25
26;; The keypad package allows easy binding of the keypad keys to
27;; various commonly used sets of commands.
28;;
29;; With the following setup, the keypad can be used for numeric data
6df44fac
KS
30;; entry when NumLock is off, and to give numeric prefix arguments to
31;; emacs commands, when NumLock on on.
72cc582e 32;;
6df44fac
KS
33;; keypad-setup => Plain Numeric Keypad
34;; keypad-numlock-setup => Prefix numeric args
72cc582e
KS
35;;
36;; +--------+--------+--------+
6df44fac
KS
37;; | M-7 | M-8 | M-9 | <- numlock on
38;; | 7 | 8 | 9 | <- numlock off
72cc582e
KS
39;; +--------+--------+--------+
40;; | M-4 | M-5 | M-6 |
41;; | 4 | 5 | 6 |
42;; +--------+--------+--------+
43;; | M-1 | M-2 | M-3 |
44;; | 1 | 2 | 3 |
45;; +--------+--------+--------+
46;; | M-0 | M-- |
47;; | 0 | . |
48;; +-----------------+--------+
49
6df44fac
KS
50;; The following keypad setup is used for navigation together with
51;; modes like cua-mode which uses shifted movement keys to extend the
52;; region.
72cc582e 53;;
6df44fac
KS
54;; keypad-setup => Cursor keys
55;; keypad-shifted-setup => Shifted cursor keys
72cc582e
KS
56;;
57;; +--------+--------+--------+
6df44fac
KS
58;; | S-home | S-up | S-PgUp | <- shifted, numlock off
59;; | Home | up | PgUp | <- unshifted, numlock off
72cc582e
KS
60;; +--------+--------+--------+
61;; | S-left |S-space |S-right |
62;; | left | space | right |
63;; +--------+--------+--------+
64;; | S-end | S-down | S-PgDn |
65;; | end | down | PgDn |
66;; +--------+--------+--------+
67;; | S-insert |S-delete|
68;; | insert | delete |
69;; +-----------------+--------+
70
6df44fac
KS
71;; The following setup binds the unshifted keypad keys to plain
72;; numeric keys when NumLock is either on or off, but the decimal key
73;; produces either a . (NumLock off) or a , (NumLock on). This is
74;; useful for e.g. Danish users where the decimal separator is a
75;; comma.
76;;
77;; keypad-setup => Plain Numeric Keypad
78;; keypad-numlock-setup => Numeric Keypad with Decimal key: ,
79;;
80;; +--------+--------+--------+
81;; | 7 | 8 | 9 | <- numlock on
82;; | 7 | 8 | 9 | <- numlock off
83;; +--------+--------+--------+
84;; | 4 | 5 | 6 |
85;; | 4 | 5 | 6 |
86;; +--------+--------+--------+
87;; | 1 | 2 | 3 |
88;; | 1 | 2 | 3 |
89;; +--------+--------+--------+
90;; | 0 | , |
91;; | 0 | . |
92;; +-----------------+--------+
72cc582e
KS
93
94;;; Code:
95
96(provide 'keypad)
97
98;;; Customization
99
100;;;###autoload
101(defcustom keypad-setup nil
6df44fac
KS
102 "Specifies the keypad setup for unshifted keypad keys when NumLock is off.
103When selecting the plain numeric keypad setup, the character returned by the
104decimal key must be specified."
72cc582e
KS
105 :set (lambda (symbol value)
106 (if value
6df44fac 107 (keypad-setup value nil nil value)))
72cc582e 108 :initialize 'custom-initialize-default
72cc582e 109 :link '(emacs-commentary-link "keypad.el")
bf247b6e 110 :version "22.1"
6df44fac
KS
111 :type '(choice (const :tag "Plain numeric keypad" numeric)
112 (character :tag "Numeric Keypad with Decimal Key"
113 :match (lambda (widget value) (integerp value))
114 :value ?.)
a1506d29 115 (const :tag "Numeric prefix arguments" prefix)
72cc582e
KS
116 (const :tag "Cursor keys" cursor)
117 (const :tag "Shifted cursor keys" S-cursor)
f31023f9 118 (const :tag "Unspecified/User-defined" none)
6df44fac 119 (other :tag "Keep existing bindings" nil))
3efa1193 120 :require 'keypad
72cc582e
KS
121 :group 'keyboard)
122
6df44fac
KS
123;;;###autoload
124(defcustom keypad-numlock-setup nil
125 "Specifies the keypad setup for unshifted keypad keys when NumLock is on.
126When selecting the plain numeric keypad setup, the character returned by the
127decimal key must be specified."
128 :set (lambda (symbol value)
129 (if value
130 (keypad-setup value t nil value)))
131 :initialize 'custom-initialize-default
132 :link '(emacs-commentary-link "keypad.el")
bf247b6e 133 :version "22.1"
6df44fac
KS
134 :type '(choice (const :tag "Plain numeric keypad" numeric)
135 (character :tag "Numeric Keypad with Decimal Key"
136 :match (lambda (widget value) (integerp value))
137 :value ?.)
a1506d29 138 (const :tag "Numeric prefix arguments" prefix)
6df44fac
KS
139 (const :tag "Cursor keys" cursor)
140 (const :tag "Shifted cursor keys" S-cursor)
f31023f9 141 (const :tag "Unspecified/User-defined" none)
6df44fac 142 (other :tag "Keep existing bindings" nil))
3efa1193 143 :require 'keypad
72cc582e
KS
144 :group 'keyboard)
145
146;;;###autoload
147(defcustom keypad-shifted-setup nil
6df44fac
KS
148 "Specifies the keypad setup for shifted keypad keys when NumLock is off.
149When selecting the plain numeric keypad setup, the character returned by the
150decimal key must be specified."
72cc582e
KS
151 :set (lambda (symbol value)
152 (if value
6df44fac 153 (keypad-setup value nil t value)))
72cc582e 154 :initialize 'custom-initialize-default
72cc582e 155 :link '(emacs-commentary-link "keypad.el")
bf247b6e 156 :version "22.1"
6df44fac
KS
157 :type '(choice (const :tag "Plain numeric keypad" numeric)
158 (character :tag "Numeric Keypad with Decimal Key"
159 :match (lambda (widget value) (integerp value))
160 :value ?.)
a1506d29 161 (const :tag "Numeric prefix arguments" prefix)
72cc582e
KS
162 (const :tag "Cursor keys" cursor)
163 (const :tag "Shifted cursor keys" S-cursor)
f31023f9 164 (const :tag "Unspecified/User-defined" none)
6df44fac 165 (other :tag "Keep existing bindings" nil))
3efa1193 166 :require 'keypad
72cc582e
KS
167 :group 'keyboard)
168
6df44fac
KS
169;;;###autoload
170(defcustom keypad-numlock-shifted-setup nil
171 "Specifies the keypad setup for shifted keypad keys when NumLock is off.
172When selecting the plain numeric keypad setup, the character returned by the
173decimal key must be specified."
174 :set (lambda (symbol value)
175 (if value
176 (keypad-setup value t t value)))
177 :initialize 'custom-initialize-default
178 :link '(emacs-commentary-link "keypad.el")
bf247b6e 179 :version "22.1"
6df44fac
KS
180 :type '(choice (const :tag "Plain numeric keypad" numeric)
181 (character :tag "Numeric Keypad with Decimal Key"
182 :match (lambda (widget value) (integerp value))
183 :value ?.)
a1506d29 184 (const :tag "Numeric prefix arguments" prefix)
6df44fac
KS
185 (const :tag "Cursor keys" cursor)
186 (const :tag "Shifted cursor keys" S-cursor)
f31023f9 187 (const :tag "Unspecified/User-defined" none)
6df44fac 188 (other :tag "Keep existing bindings" nil))
3efa1193 189 :require 'keypad
72cc582e
KS
190 :group 'keyboard)
191
6df44fac 192
72cc582e 193;;;###autoload
6df44fac 194(defun keypad-setup (setup &optional numlock shift decimal)
7cb0c23b 195 "Set keypad bindings in `function-key-map' according to SETUP.
72cc582e 196If optional second argument NUMLOCK is non-nil, the NumLock On bindings
7cb0c23b 197are changed. Otherwise, the NumLock Off bindings are changed.
6df44fac
KS
198If optional third argument SHIFT is non-nil, the shifted keypad
199keys are bound.
72cc582e
KS
200
201 Setup Binding
202 -------------------------------------------------------------
203 'prefix Command prefix argument, i.e. M-0 .. M-9 and M--
204 'S-cursor Bind shifted keypad keys to the shifted cursor movement keys.
205 'cursor Bind keypad keys to the cursor movement keys.
6df44fac 206 'numeric Plain numeric keypad, i.e. 0 .. 9 and . (or DECIMAL arg)
f31023f9
KS
207 'none Removes all bindings for keypad keys in function-key-map;
208 this enables any user-defined bindings for the keypad keys
209 in the global and local keymaps.
72cc582e 210
6df44fac 211If SETUP is 'numeric and the optional fourth argument DECIMAL is non-nil,
72cc582e 212the decimal key on the keypad is mapped to DECIMAL instead of `.'"
6df44fac
KS
213 (let* ((i 0)
214 (var (cond
215 ((and (not numlock) (not shift)) 'keypad-setup)
216 ((and (not numlock) shift) 'keypad-shifted-setup)
217 ((and numlock (not shift)) 'keypad-numlock-setup)
218 ((and numlock shift) 'keypad-numlock-shifted-setup)))
219 (kp (cond
220 ((eq var 'keypad-setup)
221 [kp-delete kp-insert kp-end kp-down kp-next kp-left
222 kp-space kp-right kp-home kp-up kp-prior])
223 ((eq var 'keypad-shifted-setup)
224 [S-kp-decimal S-kp-0 S-kp-1 S-kp-2 S-kp-3 S-kp-4
225 S-kp-5 S-kp-6 S-kp-7 S-kp-8 S-kp-9])
226 ((eq var 'keypad-numlock-setup)
227 [kp-decimal kp-0 kp-1 kp-2 kp-3 kp-4
228 kp-5 kp-6 kp-7 kp-8 kp-9])
229 ((eq var 'keypad-numlock-shifted-setup)
230 [S-kp-delete S-kp-insert S-kp-end S-kp-down S-kp-next S-kp-left
231 S-kp-space S-kp-right S-kp-home S-kp-up S-kp-prior])))
232 (bind
233 (cond
234 ((or (eq setup 'numeric)
9dbbce44 235 (characterp setup))
6df44fac
KS
236 (if (eq decimal 'numeric)
237 (setq decimal nil))
238 (vector (or decimal ?.) ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
239 ((eq setup 'prefix)
240 [?\M-- ?\M-0 ?\M-1 ?\M-2 ?\M-3 ?\M-4
241 ?\M-5 ?\M-6 ?\M-7 ?\M-8 ?\M-9])
242 ((eq setup 'cursor)
243 [delete insert end down next left
244 space right home up prior])
245 ((eq setup 'S-cursor)
a1506d29 246 [S-delete S-insert S-end S-down S-next S-left
6df44fac
KS
247 S-space S-right S-home S-up S-prior])
248 ((eq setup 'none)
249 nil)
250 (t
251 (signal 'error (list "Unknown keypad setup: " setup))))))
252
253 (set var setup)
72cc582e
KS
254
255 ;; Bind the keys in KP list to BIND list in function-key-map.
256 ;; If BIND is nil, all bindings for the keys are removed.
257 (if (not (boundp 'function-key-map))
258 (setq function-key-map (make-sparse-keymap)))
259
260 (while (< i 11)
261 (define-key function-key-map (vector (aref kp i))
262 (if bind (vector (aref bind i))))
2fe7c8dd
KS
263 (if (= i 6)
264 (cond ((eq (aref kp i) 'kp-space)
265 (define-key function-key-map [kp-begin]
266 (if bind (vector (aref bind i)))))
267 ((eq (aref kp i) 'S-kp-space)
268 (define-key function-key-map [S-kp-begin]
269 (if bind (vector (aref bind i)))))))
a1506d29 270
72cc582e
KS
271 (setq i (1+ i)))))
272
cbee283d 273;; arch-tag: 0899d2bd-9e12-4b4e-9aef-d0014d3b6414
72cc582e 274;;; keypad.el ends here