0
|
1 ;; Keymap functions.
|
|
2 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
3 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
|
|
4
|
|
5 ;; This file is part of XEmacs.
|
|
6
|
|
7 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
8 ;; under the terms of the GNU General Public License as published by
|
|
9 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
10 ;; any later version.
|
|
11
|
|
12 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
15 ;; General Public License for more details.
|
|
16
|
|
17 ;; You should have received a copy of the GNU General Public License
|
16
|
18 ;; along with XEmacs; see the file COPYING. If not, write to the
|
70
|
19 ;; Free Software Foundation, 59 Temple Place - Suite 330,
|
16
|
20 ;; Boston, MA 02111-1307, USA.
|
0
|
21
|
|
22 ;;; Synched up with: FSF 19.28.
|
|
23 ;;; Note: FSF does not have a file keymap.el. This stuff is
|
|
24 ;;; in keymap.c.
|
|
25
|
|
26 ;Prevent the \{...} documentation construct
|
|
27 ;from mentioning keys that run this command.
|
|
28 (put 'undefined 'suppress-keymap t)
|
|
29
|
|
30 (defun undefined ()
|
|
31 (interactive)
|
|
32 (ding))
|
|
33
|
|
34 (defun suppress-keymap (map &optional nodigits)
|
|
35 "Make MAP override all normally self-inserting keys to be undefined.
|
|
36 Normally, as an exception, digits and minus-sign are set to make prefix args,
|
|
37 but optional second arg NODIGITS non-nil treats them like other chars."
|
|
38 (substitute-key-definition 'self-insert-command 'undefined map global-map)
|
|
39 (or nodigits
|
|
40 (let ((string (make-string 1 ?0)))
|
|
41 (define-key map "-" 'negative-argument)
|
|
42 ;; Make plain numbers do numeric args.
|
|
43 (while (<= (aref string 0) ?9)
|
|
44 (define-key map string 'digit-argument)
|
203
|
45 (incf (aref string 0))))))
|
0
|
46
|
|
47 (defun substitute-key-definition (olddef newdef keymap &optional oldmap prefix)
|
|
48 "Replace OLDDEF with NEWDEF for any keys in KEYMAP now defined as OLDDEF.
|
|
49 In other words, OLDDEF is replaced with NEWDEF wherever it appears.
|
|
50 Prefix keymaps are checked recursively. If optional fourth argument OLDMAP
|
|
51 is specified, we redefine in KEYMAP as NEWDEF those chars which are defined
|
|
52 as OLDDEF in OLDMAP, unless that keybinding is already present in keymap.
|
|
53 If optional fifth argument PREFIX is defined, then only those occurrences of
|
|
54 OLDDEF found in keymaps accessible through the keymap bound to PREFIX in
|
|
55 KEYMAP are redefined. See also `accessible-keymaps'."
|
|
56 (let ((maps (accessible-keymaps (or oldmap keymap) prefix))
|
|
57 (shadowing (not (null oldmap)))
|
|
58 prefix map)
|
|
59 (while maps
|
|
60 (setq prefix (car (car maps))
|
|
61 map (cdr (car maps))
|
|
62 maps (cdr maps))
|
|
63 ;; Substitute in this keymap
|
|
64 (map-keymap #'(lambda (key binding)
|
|
65 (if (eq binding olddef)
|
|
66 ;; The new bindings always go in KEYMAP even if we
|
|
67 ;; found them in OLDMAP or one of it's children.
|
|
68 ;; If KEYMAP will be shadowing OLDMAP, then do not
|
|
69 ;; redefine the key if there is another binding
|
|
70 ;; in KEYMAP that will shadow OLDDEF.
|
|
71 (or (and shadowing
|
|
72 (lookup-key keymap key))
|
|
73 ;; define-key will give an error if a prefix
|
|
74 ;; of the key is already defined. Otherwise
|
|
75 ;; it will define the key in the map.
|
|
76 ;; #### - Perhaps this should be protected?
|
|
77 (define-key
|
|
78 keymap
|
|
79 (vconcat prefix (list key))
|
|
80 newdef))))
|
|
81 map)
|
|
82 )))
|
|
83
|
203
|
84
|
|
85 ;; From Bill Dubuque <wgd@martigny.ai.mit.edu>
|
|
86
|
|
87 ;; This used to wrap forms into an interactive lambda. It is unclear
|
|
88 ;; to me why this is needed in this function. Anyway,
|
|
89 ;; `key-or-menu-binding' doesn't do it, so this function no longer
|
|
90 ;; does it, either.
|
0
|
91 (defun insert-key-binding (key) ; modeled after describe-key
|
203
|
92 "Insert the command bound to KEY."
|
0
|
93 (interactive "kInsert command bound to key: ")
|
203
|
94 (let ((defn (key-or-menu-binding key)))
|
0
|
95 (if (or (null defn) (integerp defn))
|
203
|
96 (error "%s is undefined" (key-description key))
|
0
|
97 (if (or (stringp defn) (vectorp defn))
|
|
98 (setq defn (key-binding defn))) ;; a keyboard macro
|
|
99 (insert (format "%s" defn)))))
|
|
100
|
203
|
101 ;; From Bill Dubuque <wgd@martigny.ai.mit.edu>
|
0
|
102 (defun read-command-or-command-sexp (prompt)
|
|
103 "Read a command symbol or command sexp.
|
|
104 A command sexp is wrapped in an interactive lambda if needed.
|
|
105 Prompts with PROMPT."
|
|
106 ;; Todo: it would be better if we could reject symbols that are not
|
|
107 ;; commandp (as does 'read-command') but that is not easy to do
|
|
108 ;; because we must supply arg4 = require-match = nil for sexp case.
|
|
109 (let ((result (car (read-from-string
|
|
110 (completing-read prompt obarray 'commandp)))))
|
|
111 (if (and (consp result)
|
|
112 (not (eq (car result) 'lambda)))
|
203
|
113 `(lambda ()
|
|
114 (interactive)
|
|
115 ,result)
|
0
|
116 result)))
|
|
117
|
|
118 (defun local-key-binding (keys)
|
|
119 "Return the binding for command KEYS in current local keymap only.
|
|
120 KEYS is a string, a vector of events, or a vector of key-description lists
|
|
121 as described in the documentation for the `define-key' function.
|
|
122 The binding is probably a symbol with a function definition; see
|
|
123 the documentation for `lookup-key' for more information."
|
|
124 (let ((map (current-local-map)))
|
|
125 (if map
|
|
126 (lookup-key map keys)
|
|
127 nil)))
|
|
128
|
|
129 (defun global-key-binding (keys)
|
|
130 "Return the binding for command KEYS in current global keymap only.
|
|
131 KEYS is a string or vector of events, a sequence of keystrokes.
|
|
132 The binding is probably a symbol with a function definition; see
|
|
133 the documentation for `lookup-key' for more information."
|
|
134 (lookup-key (current-global-map) keys))
|
|
135
|
|
136 ;; from Bill Dubuque <wgd@martigny.ai.mit.edu>
|
|
137 (defun global-set-key (key command)
|
|
138 "Give KEY a global binding as COMMAND.
|
|
139 COMMAND is a symbol naming an interactively-callable function.
|
|
140 KEY is a string, a vector of events, or a vector of key-description lists
|
|
141 as described in the documentation for the `define-key' function.
|
|
142 Note that if KEY has a local binding in the current buffer
|
|
143 that local binding will continue to shadow any global binding."
|
|
144 ;;(interactive "KSet key globally: \nCSet key %s to command: ")
|
|
145 (interactive (list (setq key (read-key-sequence "Set key globally: "))
|
|
146 ;; Command sexps are allowed here so that this arg
|
|
147 ;; may be supplied interactively via insert-key-binding.
|
|
148 (read-command-or-command-sexp
|
|
149 (format "Set key %s to command: "
|
|
150 (key-description key)))))
|
|
151 (define-key (current-global-map) key command)
|
|
152 nil)
|
|
153
|
|
154 ;; from Bill Dubuque <wgd@martigny.ai.mit.edu>
|
|
155 (defun local-set-key (key command)
|
|
156 "Give KEY a local binding as COMMAND.
|
|
157 COMMAND is a symbol naming an interactively-callable function.
|
|
158 KEY is a string, a vector of events, or a vector of key-description lists
|
|
159 as described in the documentation for the `define-key' function.
|
|
160 The binding goes in the current buffer's local map,
|
|
161 which is shared with other buffers in the same major mode."
|
|
162 ;;(interactive "KSet key locally: \nCSet key %s locally to command: ")
|
|
163 (interactive (list (setq key (read-key-sequence "Set key locally: "))
|
|
164 ;; Command sexps are allowed here so that this arg
|
|
165 ;; may be supplied interactively via insert-key-binding.
|
|
166 (read-command-or-command-sexp
|
|
167 (format "Set key %s locally to command: "
|
|
168 (key-description key)))))
|
|
169 (if (null (current-local-map))
|
|
170 (use-local-map (make-sparse-keymap)))
|
|
171 (define-key (current-local-map) key command)
|
|
172 nil)
|
|
173
|
|
174 (defun global-unset-key (key)
|
|
175 "Remove global binding of KEY.
|
|
176 KEY is a string, a vector of events, or a vector of key-description lists
|
|
177 as described in the documentation for the `define-key' function."
|
|
178 (interactive "kUnset key globally: ")
|
|
179 (global-set-key key nil))
|
|
180
|
|
181 (defun local-unset-key (key)
|
|
182 "Remove local binding of KEY.
|
|
183 KEY is a string, a vector of events, or a vector of key-description lists
|
|
184 as described in the documentation for the `define-key' function."
|
|
185 (interactive "kUnset key locally: ")
|
|
186 (if (current-local-map)
|
|
187 (define-key (current-local-map) key nil)))
|
|
188
|
|
189
|
|
190 ;; Yet more RMS brain-death.
|
|
191 (defun minor-mode-key-binding (key &optional accept-default)
|
|
192 "Find the visible minor mode bindings of KEY.
|
2
|
193 Return an alist of pairs (MODENAME . BINDING), where MODENAME is
|
0
|
194 the symbol which names the minor mode binding KEY, and BINDING is
|
|
195 KEY's definition in that mode. In particular, if KEY has no
|
|
196 minor-mode bindings, return nil. If the first binding is a
|
|
197 non-prefix, all subsequent bindings will be omitted, since they would
|
|
198 be ignored. Similarly, the list doesn't include non-prefix bindings
|
|
199 that come after prefix bindings.
|
|
200
|
|
201 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
|
|
202 bindings; see the description of `lookup-key' for more details about this."
|
|
203 (let ((tail minor-mode-map-alist)
|
|
204 a s v)
|
|
205 (while tail
|
|
206 (setq a (car tail)
|
|
207 tail (cdr tail))
|
|
208 (and (consp a)
|
|
209 (symbolp (setq s (car a)))
|
|
210 (boundp s)
|
|
211 (symbol-value s)
|
|
212 ;; indirect-function deals with autoloadable keymaps
|
|
213 (setq v (indirect-function (cdr a)))
|
|
214 (setq v (lookup-key v key accept-default))
|
|
215 ;; Terminate loop, with v set to non-nil value
|
|
216 (setq tail nil)))
|
|
217 v))
|
203
|
218
|
0
|
219
|
|
220 (defun current-minor-mode-maps ()
|
|
221 "Return a list of keymaps for the minor modes of the current buffer."
|
|
222 (let ((l '())
|
|
223 (tail minor-mode-map-alist)
|
|
224 a s v)
|
|
225 (while tail
|
|
226 (setq a (car tail)
|
|
227 tail (cdr tail))
|
|
228 (and (consp a)
|
|
229 (symbolp (setq s (car a)))
|
|
230 (boundp s)
|
|
231 (symbol-value s)
|
|
232 ;; indirect-function deals with autoloadable keymaps
|
|
233 (setq v (indirect-function (cdr a)))
|
|
234 (setq l (cons v l))))
|
|
235 (nreverse l)))
|
|
236
|
|
237
|
|
238 ;;#### What a crock
|
|
239 (defun define-prefix-command (name &optional mapvar)
|
|
240 "Define COMMAND as a prefix command.
|
|
241 A new sparse keymap is stored as COMMAND's function definition.
|
|
242 If second optional argument MAPVAR is not specified,
|
|
243 COMMAND's value (as well as its function definition) is set to the keymap.
|
|
244 If a second optional argument MAPVAR is given and is not `t',
|
|
245 the map is stored as its value.
|
|
246 Regardless of MAPVAR, COMMAND's function-value is always set to the keymap."
|
203
|
247 (let ((map (make-sparse-keymap name)))
|
0
|
248 (fset name map)
|
|
249 (cond ((not mapvar)
|
|
250 (set name map))
|
|
251 ((eq mapvar 't)
|
|
252 )
|
|
253 (t
|
|
254 (set mapvar map)))
|
|
255 name))
|
|
256
|
|
257
|
|
258 ;;; Converting vectors of events to a read-equivalent form.
|
|
259 ;;; This is used both by call-interactively (for the command history)
|
|
260 ;;; and by macros.el (for saving keyboard macros to a file).
|
|
261
|
203
|
262 ;; ### why does (events-to-keys [backspace]) return "\C-h"?
|
|
263 ;; BTW, this function is a mess, and macros.el does *not* use it, in
|
|
264 ;; spite of the above comment. `format-kbd-macro' is used to save
|
|
265 ;; keyboard macros to a file.
|
0
|
266 (defun events-to-keys (events &optional no-mice)
|
|
267 "Given a vector of event objects, returns a vector of key descriptors,
|
|
268 or a string (if they all fit in the ASCII range).
|
|
269 Optional arg NO-MICE means that button events are not allowed."
|
|
270 (if (and events (symbolp events)) (setq events (vector events)))
|
|
271 (cond ((stringp events)
|
|
272 events)
|
|
273 ((not (vectorp events))
|
|
274 (signal 'wrong-type-argument (list 'vectorp events)))
|
|
275 ((let* ((length (length events))
|
|
276 (string (make-string length 0))
|
|
277 c ce
|
|
278 (i 0))
|
|
279 (while (< i length)
|
|
280 (setq ce (aref events i))
|
|
281 (or (eventp ce) (setq ce (character-to-event ce)))
|
|
282 ;; Normalize `c' to `?c' and `(control k)' to `?\C-k'
|
|
283 ;; By passing t for the `allow-meta' arg we could get kbd macros
|
|
284 ;; with meta in them to translate to the string form instead of
|
|
285 ;; the list/symbol form; but I expect that would cause confusion,
|
|
286 ;; so let's use the list/symbol form whenever there's
|
|
287 ;; any ambiguity.
|
|
288 (setq c (event-to-character ce))
|
|
289 (if (and c
|
|
290 character-set-property
|
|
291 (key-press-event-p ce))
|
|
292 (cond ((symbolp (event-key ce))
|
|
293 (if (get (event-key ce) character-set-property)
|
|
294 ;; Don't use a string for `backspace' and `tab' to
|
|
295 ;; avoid that unpleasant little ambiguity.
|
|
296 (setq c nil)))
|
|
297 ((and (= (event-modifier-bits ce) 1) ;control
|
|
298 (integerp (event-key ce)))
|
|
299 (let* ((te (character-to-event c)))
|
|
300 (if (and (symbolp (event-key te))
|
|
301 (get (event-key te) character-set-property))
|
|
302 ;; Don't "normalize" (control i) to tab
|
|
303 ;; to avoid the ambiguity in the other direction
|
|
304 (setq c nil))
|
|
305 (deallocate-event te)))))
|
|
306 (if c
|
|
307 (aset string i c)
|
|
308 (setq i length string nil))
|
|
309 (setq i (1+ i)))
|
|
310 string))
|
|
311 (t
|
|
312 (let* ((length (length events))
|
|
313 (new (copy-sequence events))
|
|
314 event mods key
|
|
315 (i 0))
|
|
316 (while (< i length)
|
|
317 (setq event (aref events i))
|
|
318 (cond ((key-press-event-p event)
|
|
319 (setq mods (event-modifiers event)
|
|
320 key (event-key event))
|
|
321 (if (numberp key)
|
|
322 (setq key (intern (make-string 1 key))))
|
|
323 (aset new i (if mods
|
|
324 (nconc mods (cons key nil))
|
|
325 key)))
|
|
326 ((misc-user-event-p event)
|
|
327 (aset new i (list 'menu-selection
|
|
328 (event-function event)
|
|
329 (event-object event))))
|
|
330 ((or (button-press-event-p event)
|
|
331 (button-release-event-p event))
|
|
332 (if no-mice
|
|
333 (error
|
|
334 "Mouse events can't be saved in keyboard macros."))
|
|
335 (setq mods (event-modifiers event)
|
|
336 key (intern (concat "button"
|
|
337 (event-button event)
|
|
338 (if (button-release-event-p event)
|
|
339 "up" ""))))
|
|
340 (aset new i (if mods
|
|
341 (nconc mods (cons key nil))
|
|
342 key)))
|
|
343 ((or (and event (symbolp event))
|
|
344 (and (consp event) (symbolp (car event))))
|
|
345 (aset new i event))
|
|
346 (t
|
|
347 (signal 'wrong-type-argument (list 'eventp event))))
|
|
348 (setq i (1+ i)))
|
|
349 new))))
|
207
|
350
|
0
|
351
|
207
|
352 (defun next-key-event ()
|
|
353 "Return the next available keyboard event."
|
|
354 (let (event)
|
|
355 (while (not (key-press-event-p (setq event (next-event))))
|
|
356 (dispatch-event event))
|
|
357 event))
|
|
358
|
|
359 (defun key-sequence-list-description (keys)
|
|
360 "Convert a key sequence KEYS to the full [(modifiers... key)...] form.
|
|
361 Argument KEYS can be in any form accepted by `define-key' function."
|
|
362 (let ((vec
|
|
363 (cond ((vectorp keys)
|
|
364 keys)
|
|
365 ((stringp keys)
|
|
366 (vconcat keys))
|
|
367 (t
|
|
368 (vector keys))))
|
|
369 (event-to-list
|
|
370 #'(lambda (ev)
|
|
371 (append (event-modifiers ev) (list (event-key ev))))))
|
|
372 (mapvector
|
|
373 #'(lambda (key)
|
|
374 (cond ((key-press-event-p key)
|
|
375 (funcall event-to-list key))
|
|
376 ((characterp key)
|
|
377 (funcall event-to-list (character-to-event key)))
|
|
378 ((listp key)
|
|
379 key)
|
|
380 (t
|
|
381 (list key))))
|
|
382 vec)))
|
|
383
|
|
384
|
0
|
385 ;;; Support keyboard commands to turn on various modifiers.
|
185
|
386
|
0
|
387 ;;; These functions -- which are not commands -- each add one modifier
|
|
388 ;;; to the following event.
|
185
|
389
|
|
390 (defun event-apply-alt-modifier (ignore-prompt)
|
|
391 (event-apply-modifier 'alt))
|
|
392 (defun event-apply-super-modifier (ignore-prompt)
|
|
393 (event-apply-modifier 'super))
|
|
394 (defun event-apply-hyper-modifier (ignore-prompt)
|
|
395 (event-apply-modifier 'hyper))
|
|
396 (defun event-apply-shift-modifier (ignore-prompt)
|
|
397 (event-apply-modifier 'shift))
|
|
398 (defun event-apply-control-modifier (ignore-prompt)
|
|
399 (event-apply-modifier 'control))
|
|
400 (defun event-apply-meta-modifier (ignore-prompt)
|
|
401 (event-apply-modifier 'meta))
|
|
402
|
207
|
403 ;;; #### `key-translate-map' is ignored for now.
|
185
|
404 (defun event-apply-modifier (symbol)
|
|
405 "Return the next key event, with a modifier flag applied.
|
207
|
406 SYMBOL is the name of this modifier, as a symbol.
|
|
407 `function-key-map' is scanned for prefix bindings."
|
|
408 (let (events binding)
|
|
409 ;; read keystrokes scanning `function-key-map'
|
|
410 (while (keymapp
|
|
411 (setq binding
|
|
412 (lookup-key
|
|
413 function-key-map
|
|
414 (vconcat
|
|
415 (setq events
|
|
416 (append events (list (next-key-event)))))))))
|
|
417 (if binding ; found a binding
|
|
418 (progn
|
|
419 ;; allow for several modifiers
|
|
420 (if (and (symbolp binding) (fboundp binding))
|
|
421 (setq binding (funcall binding nil)))
|
|
422 (setq events (append binding nil))
|
|
423 ;; put remaining keystrokes back into input queue
|
|
424 (setq unread-command-events
|
|
425 (mapcar 'character-to-event (cdr events))))
|
|
426 (setq unread-command-events (cdr events)))
|
|
427 ;; add a modifier SYMBOL to the first keystroke or event
|
|
428 (vector
|
|
429 (append (list symbol)
|
|
430 (delq symbol
|
|
431 (aref (key-sequence-list-description (car events)) 0))))))
|
185
|
432
|
203
|
433 ;; This looks dirty. The following code should maybe go to another
|
|
434 ;; file, and `create-console-hook' should maybe default to nil.
|
185
|
435 (add-hook
|
|
436 'create-console-hook
|
207
|
437 #'(lambda (console)
|
185
|
438 (letf (((selected-console) console))
|
|
439 (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
|
|
440 (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
|
|
441 (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
|
|
442 (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
|
|
443 (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
|
|
444 (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier))))
|