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
|
|
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
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)
|
|
45 (aset string 0 (1+ (aref string 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
|
|
84 ;; from Bill Dubuque <wgd@martigny.ai.mit.edu>
|
|
85 (defun insert-key-binding (key) ; modeled after describe-key
|
|
86 (interactive "kInsert command bound to key: ")
|
|
87 (let (defn)
|
|
88 ;; If the key typed was really a menu selection, grab the form out
|
|
89 ;; of the event object and intuit the function that would be called,
|
|
90 ;; and describe that instead.
|
|
91 (if (and (vectorp key) (= 1 (length key))
|
|
92 (or (misc-user-event-p (aref key 0))
|
|
93 (eq (car-safe (aref key 0)) 'menu-selection)))
|
|
94 (let ((event (aref key 0)))
|
|
95 (setq defn (if (eventp event)
|
|
96 (list (event-function event) (event-object event))
|
|
97 (cdr event)))
|
|
98 (if (eq (car defn) 'eval)
|
|
99 (setq defn (` (lambda ()
|
|
100 (interactive)
|
|
101 (, (car (cdr defn)))))))
|
|
102 (if (eq (car-safe defn) 'call-interactively)
|
|
103 (setq defn (car (cdr defn))))
|
|
104 (if (and (consp defn) (null (cdr defn)))
|
|
105 (setq defn (car defn))))
|
|
106 (setq defn (key-binding key)))
|
|
107 (if (or (null defn) (integerp defn))
|
|
108 (error "%s is undefined" (key-description key))
|
|
109 (if (or (stringp defn) (vectorp defn))
|
|
110 (setq defn (key-binding defn))) ;; a keyboard macro
|
|
111 (insert (format "%s" defn)))))
|
|
112
|
|
113 ;; from Bill Dubuque <wgd@martigny.ai.mit.edu>
|
|
114 (defun read-command-or-command-sexp (prompt)
|
|
115 "Read a command symbol or command sexp.
|
|
116 A command sexp is wrapped in an interactive lambda if needed.
|
|
117 Prompts with PROMPT."
|
|
118 ;; Todo: it would be better if we could reject symbols that are not
|
|
119 ;; commandp (as does 'read-command') but that is not easy to do
|
|
120 ;; because we must supply arg4 = require-match = nil for sexp case.
|
|
121 (let ((result (car (read-from-string
|
|
122 (completing-read prompt obarray 'commandp)))))
|
|
123 (if (and (consp result)
|
|
124 (not (eq (car result) 'lambda)))
|
|
125 (` (lambda ()
|
|
126 (interactive)
|
|
127 (, result)))
|
|
128 result)))
|
|
129
|
|
130 (defun local-key-binding (keys)
|
|
131 "Return the binding for command KEYS in current local keymap only.
|
|
132 KEYS is a string, a vector of events, or a vector of key-description lists
|
|
133 as described in the documentation for the `define-key' function.
|
|
134 The binding is probably a symbol with a function definition; see
|
|
135 the documentation for `lookup-key' for more information."
|
|
136 (let ((map (current-local-map)))
|
|
137 (if map
|
|
138 (lookup-key map keys)
|
|
139 nil)))
|
|
140
|
|
141 (defun global-key-binding (keys)
|
|
142 "Return the binding for command KEYS in current global keymap only.
|
|
143 KEYS is a string or vector of events, a sequence of keystrokes.
|
|
144 The binding is probably a symbol with a function definition; see
|
|
145 the documentation for `lookup-key' for more information."
|
|
146 (lookup-key (current-global-map) keys))
|
|
147
|
|
148 ;; from Bill Dubuque <wgd@martigny.ai.mit.edu>
|
|
149 (defun global-set-key (key command)
|
|
150 "Give KEY a global binding as COMMAND.
|
|
151 COMMAND is a symbol naming an interactively-callable function.
|
|
152 KEY is a string, a vector of events, or a vector of key-description lists
|
|
153 as described in the documentation for the `define-key' function.
|
|
154 Note that if KEY has a local binding in the current buffer
|
|
155 that local binding will continue to shadow any global binding."
|
|
156 ;;(interactive "KSet key globally: \nCSet key %s to command: ")
|
|
157 (interactive (list (setq key (read-key-sequence "Set key globally: "))
|
|
158 ;; Command sexps are allowed here so that this arg
|
|
159 ;; may be supplied interactively via insert-key-binding.
|
|
160 (read-command-or-command-sexp
|
|
161 (format "Set key %s to command: "
|
|
162 (key-description key)))))
|
|
163 (define-key (current-global-map) key command)
|
|
164 nil)
|
|
165
|
|
166 ;; from Bill Dubuque <wgd@martigny.ai.mit.edu>
|
|
167 (defun local-set-key (key command)
|
|
168 "Give KEY a local binding as COMMAND.
|
|
169 COMMAND is a symbol naming an interactively-callable function.
|
|
170 KEY is a string, a vector of events, or a vector of key-description lists
|
|
171 as described in the documentation for the `define-key' function.
|
|
172 The binding goes in the current buffer's local map,
|
|
173 which is shared with other buffers in the same major mode."
|
|
174 ;;(interactive "KSet key locally: \nCSet key %s locally to command: ")
|
|
175 (interactive (list (setq key (read-key-sequence "Set key locally: "))
|
|
176 ;; Command sexps are allowed here so that this arg
|
|
177 ;; may be supplied interactively via insert-key-binding.
|
|
178 (read-command-or-command-sexp
|
|
179 (format "Set key %s locally to command: "
|
|
180 (key-description key)))))
|
|
181 (if (null (current-local-map))
|
|
182 (use-local-map (make-sparse-keymap)))
|
|
183 (define-key (current-local-map) key command)
|
|
184 nil)
|
|
185
|
|
186 (defun global-unset-key (key)
|
|
187 "Remove global binding of KEY.
|
|
188 KEY is a string, a vector of events, or a vector of key-description lists
|
|
189 as described in the documentation for the `define-key' function."
|
|
190 (interactive "kUnset key globally: ")
|
|
191 (global-set-key key nil))
|
|
192
|
|
193 (defun local-unset-key (key)
|
|
194 "Remove local binding of KEY.
|
|
195 KEY is a string, a vector of events, or a vector of key-description lists
|
|
196 as described in the documentation for the `define-key' function."
|
|
197 (interactive "kUnset key locally: ")
|
|
198 (if (current-local-map)
|
|
199 (define-key (current-local-map) key nil)))
|
|
200
|
|
201
|
|
202 ;; Yet more RMS brain-death.
|
|
203 (defun minor-mode-key-binding (key &optional accept-default)
|
|
204 "Find the visible minor mode bindings of KEY.
|
2
|
205 Return an alist of pairs (MODENAME . BINDING), where MODENAME is
|
0
|
206 the symbol which names the minor mode binding KEY, and BINDING is
|
|
207 KEY's definition in that mode. In particular, if KEY has no
|
|
208 minor-mode bindings, return nil. If the first binding is a
|
|
209 non-prefix, all subsequent bindings will be omitted, since they would
|
|
210 be ignored. Similarly, the list doesn't include non-prefix bindings
|
|
211 that come after prefix bindings.
|
|
212
|
|
213 If optional argument ACCEPT-DEFAULT is non-nil, recognize default
|
|
214 bindings; see the description of `lookup-key' for more details about this."
|
|
215 (let ((tail minor-mode-map-alist)
|
|
216 a s v)
|
|
217 (while tail
|
|
218 (setq a (car tail)
|
|
219 tail (cdr tail))
|
|
220 (and (consp a)
|
|
221 (symbolp (setq s (car a)))
|
|
222 (boundp s)
|
|
223 (symbol-value s)
|
|
224 ;; indirect-function deals with autoloadable keymaps
|
|
225 (setq v (indirect-function (cdr a)))
|
|
226 (setq v (lookup-key v key accept-default))
|
|
227 ;; Terminate loop, with v set to non-nil value
|
|
228 (setq tail nil)))
|
|
229 v))
|
|
230
|
|
231
|
|
232 (defun current-minor-mode-maps ()
|
|
233 "Return a list of keymaps for the minor modes of the current buffer."
|
|
234 (let ((l '())
|
|
235 (tail minor-mode-map-alist)
|
|
236 a s v)
|
|
237 (while tail
|
|
238 (setq a (car tail)
|
|
239 tail (cdr tail))
|
|
240 (and (consp a)
|
|
241 (symbolp (setq s (car a)))
|
|
242 (boundp s)
|
|
243 (symbol-value s)
|
|
244 ;; indirect-function deals with autoloadable keymaps
|
|
245 (setq v (indirect-function (cdr a)))
|
|
246 (setq l (cons v l))))
|
|
247 (nreverse l)))
|
|
248
|
|
249
|
|
250 ;;#### What a crock
|
|
251 (defun define-prefix-command (name &optional mapvar)
|
|
252 "Define COMMAND as a prefix command.
|
|
253 A new sparse keymap is stored as COMMAND's function definition.
|
|
254 If second optional argument MAPVAR is not specified,
|
|
255 COMMAND's value (as well as its function definition) is set to the keymap.
|
|
256 If a second optional argument MAPVAR is given and is not `t',
|
|
257 the map is stored as its value.
|
|
258 Regardless of MAPVAR, COMMAND's function-value is always set to the keymap."
|
|
259 (let ((map (make-sparse-keymap)))
|
|
260 (set-keymap-name map name)
|
|
261 (fset name map)
|
|
262 (cond ((not mapvar)
|
|
263 (set name map))
|
|
264 ((eq mapvar 't)
|
|
265 )
|
|
266 (t
|
|
267 (set mapvar map)))
|
|
268 name))
|
|
269
|
|
270
|
|
271 ;;; Converting vectors of events to a read-equivalent form.
|
|
272 ;;; This is used both by call-interactively (for the command history)
|
|
273 ;;; and by macros.el (for saving keyboard macros to a file).
|
|
274
|
|
275 (defun events-to-keys (events &optional no-mice)
|
|
276 "Given a vector of event objects, returns a vector of key descriptors,
|
|
277 or a string (if they all fit in the ASCII range).
|
|
278 Optional arg NO-MICE means that button events are not allowed."
|
|
279 (if (and events (symbolp events)) (setq events (vector events)))
|
|
280 (cond ((stringp events)
|
|
281 events)
|
|
282 ((not (vectorp events))
|
|
283 (signal 'wrong-type-argument (list 'vectorp events)))
|
|
284 ((let* ((length (length events))
|
|
285 (string (make-string length 0))
|
|
286 c ce
|
|
287 (i 0))
|
|
288 (while (< i length)
|
|
289 (setq ce (aref events i))
|
|
290 (or (eventp ce) (setq ce (character-to-event ce)))
|
|
291 ;; Normalize `c' to `?c' and `(control k)' to `?\C-k'
|
|
292 ;; By passing t for the `allow-meta' arg we could get kbd macros
|
|
293 ;; with meta in them to translate to the string form instead of
|
|
294 ;; the list/symbol form; but I expect that would cause confusion,
|
|
295 ;; so let's use the list/symbol form whenever there's
|
|
296 ;; any ambiguity.
|
|
297 (setq c (event-to-character ce))
|
|
298 (if (and c
|
|
299 character-set-property
|
|
300 (key-press-event-p ce))
|
|
301 (cond ((symbolp (event-key ce))
|
|
302 (if (get (event-key ce) character-set-property)
|
|
303 ;; Don't use a string for `backspace' and `tab' to
|
|
304 ;; avoid that unpleasant little ambiguity.
|
|
305 (setq c nil)))
|
|
306 ((and (= (event-modifier-bits ce) 1) ;control
|
|
307 (integerp (event-key ce)))
|
|
308 (let* ((te (character-to-event c)))
|
|
309 (if (and (symbolp (event-key te))
|
|
310 (get (event-key te) character-set-property))
|
|
311 ;; Don't "normalize" (control i) to tab
|
|
312 ;; to avoid the ambiguity in the other direction
|
|
313 (setq c nil))
|
|
314 (deallocate-event te)))))
|
|
315 (if c
|
|
316 (aset string i c)
|
|
317 (setq i length string nil))
|
|
318 (setq i (1+ i)))
|
|
319 string))
|
|
320 (t
|
|
321 (let* ((length (length events))
|
|
322 (new (copy-sequence events))
|
|
323 event mods key
|
|
324 (i 0))
|
|
325 (while (< i length)
|
|
326 (setq event (aref events i))
|
|
327 (cond ((key-press-event-p event)
|
|
328 (setq mods (event-modifiers event)
|
|
329 key (event-key event))
|
|
330 (if (numberp key)
|
|
331 (setq key (intern (make-string 1 key))))
|
|
332 (aset new i (if mods
|
|
333 (nconc mods (cons key nil))
|
|
334 key)))
|
|
335 ((misc-user-event-p event)
|
|
336 (aset new i (list 'menu-selection
|
|
337 (event-function event)
|
|
338 (event-object event))))
|
|
339 ((or (button-press-event-p event)
|
|
340 (button-release-event-p event))
|
|
341 (if no-mice
|
|
342 (error
|
|
343 "Mouse events can't be saved in keyboard macros."))
|
|
344 (setq mods (event-modifiers event)
|
|
345 key (intern (concat "button"
|
|
346 (event-button event)
|
|
347 (if (button-release-event-p event)
|
|
348 "up" ""))))
|
|
349 (aset new i (if mods
|
|
350 (nconc mods (cons key nil))
|
|
351 key)))
|
|
352 ((or (and event (symbolp event))
|
|
353 (and (consp event) (symbolp (car event))))
|
|
354 (aset new i event))
|
|
355 (t
|
|
356 (signal 'wrong-type-argument (list 'eventp event))))
|
|
357 (setq i (1+ i)))
|
|
358 new))))
|
|
359
|
|
360 ;FSFmacs ####
|
|
361 ;;; Support keyboard commands to turn on various modifiers.
|
|
362 ;
|
|
363 ;;; These functions -- which are not commands -- each add one modifier
|
|
364 ;;; to the following event.
|
|
365 ;
|
|
366 ;(defun event-apply-alt-modifier (ignore-prompt)
|
|
367 ; (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
|
|
368 ;(defun event-apply-super-modifier (ignore-prompt)
|
|
369 ; (vector (event-apply-modifier (read-event) 'super 23 "s-")))
|
|
370 ;(defun event-apply-hyper-modifier (ignore-prompt)
|
|
371 ; (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
|
|
372 ;(defun event-apply-shift-modifier (ignore-prompt)
|
|
373 ; (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
|
|
374 ;(defun event-apply-control-modifier (ignore-prompt)
|
|
375 ; (vector (event-apply-modifier (read-event) 'control 26 "C-")))
|
|
376 ;(defun event-apply-meta-modifier (ignore-prompt)
|
|
377 ; (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
|
|
378 ;
|
|
379 ;(defun event-apply-modifier (event symbol lshiftby prefix)
|
|
380 ; "Apply a modifier flag to event EVENT.
|
|
381 ;SYMBOL is the name of this modifier, as a symbol.
|
|
382 ;LSHIFTBY is the numeric value of this modifier, in keyboard events.
|
|
383 ;PREFIX is the string that represents this modifier in an event type symbol."
|
|
384 ; (if (numberp event)
|
|
385 ; (cond ((eq symbol 'control)
|
|
386 ; (if (and (<= (downcase event) ?z)
|
|
387 ; (>= (downcase event) ?a))
|
|
388 ; (- (downcase event) ?a -1)
|
|
389 ; (if (and (<= (downcase event) ?Z)
|
|
390 ; (>= (downcase event) ?A))
|
|
391 ; (- (downcase event) ?A -1)
|
|
392 ; (logior (lsh 1 lshiftby) event))))
|
|
393 ; ((eq symbol 'shift)
|
|
394 ; (if (and (<= (downcase event) ?z)
|
|
395 ; (>= (downcase event) ?a))
|
|
396 ; (upcase event)
|
|
397 ; (logior (lsh 1 lshiftby) event)))
|
|
398 ; (t
|
|
399 ; (logior (lsh 1 lshiftby) event)))
|
|
400 ; (if (memq symbol (event-modifiers event))
|
|
401 ; event
|
|
402 ; (let ((event-type (if (symbolp event) event (car event))))
|
|
403 ; (setq event-type (intern (concat prefix (symbol-name event-type))))
|
|
404 ; (if (symbolp event)
|
|
405 ; event-type
|
|
406 ; (cons event-type (cdr event)))))))
|
|
407 ;
|
|
408 ;(define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
|
|
409 ;(define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
|
|
410 ;(define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
|
|
411 ;(define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
|
|
412 ;(define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
|
|
413 ;(define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
|