Merge from emacs-24; up to 2012-12-23T02:41:17Z!rgm@gnu.org
[bpt/emacs.git] / lisp / tmm.el
CommitLineData
4d0463b9 1;;; tmm.el --- text mode access to menu-bar -*- lexical-binding: t -*-
20062d6b 2
ab422c4d 3;; Copyright (C) 1994-1996, 2000-2013 Free Software Foundation, Inc.
20062d6b
RS
4
5;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
fc225f66 6;; Maintainer: FSF
97895167 7;; Keywords: convenience
20062d6b 8
d440e474 9;; This file is part of GNU Emacs.
20062d6b 10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
20062d6b 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
20062d6b
RS
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20062d6b 23
b578f267 24;;; Commentary:
20062d6b 25
97895167 26;; This package provides text mode access to the menu bar.
20062d6b 27
b578f267 28;;; Code:
20062d6b
RS
29
30(require 'electric)
20062d6b 31
4bef9110
SE
32(defgroup tmm nil
33 "Text mode access to menu-bar."
34 :prefix "tmm-"
35 :group 'menu)
36
20062d6b
RS
37;;; The following will be localized, added only to pacify the compiler.
38(defvar tmm-short-cuts)
fc225f66 39(defvar tmm-old-mb-map nil)
93253b0e 40(defvar tmm-c-prompt nil)
20062d6b 41(defvar tmm-km-list)
670ce6ea 42(defvar tmm-next-shortcut-digit)
20062d6b
RS
43(defvar tmm-table-undef)
44
e6a5c7de 45;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
77cc5db0 46;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
afb1835e 47
20062d6b 48;;;###autoload
77cc5db0 49(defun tmm-menubar (&optional x-position)
20062d6b 50 "Text-mode emulation of looking and choosing from a menubar.
77cc5db0
RS
51See the documentation for `tmm-prompt'.
52X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
53we make that menu bar item (the one at that position) the default choice."
20062d6b
RS
54 (interactive)
55 (run-hooks 'menu-bar-update-hook)
fc225f66 56 ;; Obey menu-bar-final-items; put those items last.
4d0463b9
SM
57 (let ((menu-bar '())
58 (menu-end '())
77cc5db0 59 menu-bar-item)
4d0463b9
SM
60 (map-keymap
61 (lambda (key binding)
62 (push (cons key binding)
63 ;; If KEY is the name of an item that we want to put last,
64 ;; move it to the end.
65 (if (memq key menu-bar-final-items)
66 menu-end
67 menu-bar)))
68 (tmm-get-keybind [menu-bar]))
69 (setq menu-bar `(keymap ,@(nreverse menu-bar) ,@(nreverse menu-end)))
77cc5db0 70 (if x-position
4d0463b9
SM
71 (let ((column 0))
72 (catch 'done
73 (map-keymap
74 (lambda (key binding)
75 (when (> column x-position)
76 (setq menu-bar-item key)
77 (throw 'done nil))
78 (pcase binding
79 ((or `(,(and (pred stringp) name) . ,_) ;Simple menu item.
80 `(menu-item ,name ,_cmd ;Extended menu item.
81 . ,(and props
82 (guard (let ((visible
83 (plist-get props :visible)))
84 (or (null visible)
85 (eval visible)))))))
86 (setq column (+ column (length name) 1)))))
87 menu-bar))))
77cc5db0
RS
88 (tmm-prompt menu-bar nil menu-bar-item)))
89
8e735883 90;;;###autoload
77cc5db0
RS
91(defun tmm-menubar-mouse (event)
92 "Text-mode emulation of looking and choosing from a menubar.
93This command is used when you click the mouse in the menubar
94on a console which has no window system but does have a mouse.
95See the documentation for `tmm-prompt'."
96 (interactive "e")
97 (tmm-menubar (car (posn-x-y (event-start event)))))
20062d6b 98
4bef9110 99(defcustom tmm-mid-prompt "==>"
c03aab72 100 "String to insert between shortcut and menu item.
3ae704f4 101If nil, there will be no shortcuts. It should not consist only of spaces,
4bef9110
SE
102or else the correct item might not be found in the `*Completions*' buffer."
103 :type 'string
104 :group 'tmm)
20062d6b
RS
105
106(defvar tmm-mb-map nil
107 "A place to store minibuffer map.")
108
97895167 109(defcustom tmm-completion-prompt
e5ba1eb9 110 "Press PageUp key to reach this buffer from the minibuffer.
20062d6b 111Alternatively, you can use Up/Down keys (or your History keys) to change
97895167 112the item in the minibuffer, and press RET when you are done, or press the
10fe2d38 113marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
20062d6b 114"
c03aab72 115 "Help text to insert on the top of the completion buffer.
670ce6ea 116To save space, you can set this to nil,
4bef9110
SE
117in which case the standard introduction text is deleted too."
118 :type '(choice string (const nil))
119 :group 'tmm)
670ce6ea 120
4bef9110 121(defcustom tmm-shortcut-style '(downcase upcase)
c03aab72 122 "What letters to use as menu shortcuts.
97895167 123Must be either one of the symbols `downcase' or `upcase',
4bef9110
SE
124or else a list of the two in the order you prefer."
125 :type '(choice (const downcase)
126 (const upcase)
127 (repeat (choice (const downcase) (const upcase))))
128 :group 'tmm)
670ce6ea 129
4bef9110 130(defcustom tmm-shortcut-words 2
c03aab72 131 "How many successive words to try for shortcuts, nil means all.
97895167 132If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
4bef9110
SE
133specify nil for this variable."
134 :type '(choice integer (const nil))
135 :group 'tmm)
20062d6b 136
e43cbeae 137(defface tmm-inactive
1c8c1295 138 '((t :inherit shadow))
04a5d30f
NR
139 "Face used for inactive menu items."
140 :group 'tmm)
141
4d0463b9
SM
142(defun tmm--completion-table (items)
143 (lambda (string pred action)
144 (if (eq action 'metadata)
145 '(metadata (display-sort-function . identity))
146 (complete-with-action action items string pred))))
147
20062d6b 148;;;###autoload
bdbc7685 149(defun tmm-prompt (menu &optional in-popup default-item)
20062d6b 150 "Text-mode emulation of calling the bindings in keymap.
77cc5db0
RS
151Creates a text-mode menu of possible choices. You can access the elements
152in the menu in two ways:
153 *) via history mechanism from minibuffer;
20062d6b
RS
154 *) Or via completion-buffer that is automatically shown.
155The last alternative is currently a hack, you cannot use mouse reliably.
bdbc7685
RS
156
157MENU is like the MENU argument to `x-popup-menu': either a
158keymap or an alist of alists.
159DEFAULT-ITEM, if non-nil, specifies an initial default choice.
160Its value should be an event that has a binding in MENU."
161 ;; If the optional argument IN-POPUP is t,
162 ;; then MENU is an alist of elements of the form (STRING . VALUE).
163 ;; That is used for recursive calls only.
164 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
165 ; so it doesn't have a name.
166 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
3ae704f4 167 tmm-old-mb-map tmm-short-cuts
bdbc7685
RS
168 chosen-string choice
169 (not-menu (not (keymapp menu))))
20062d6b 170 (run-hooks 'activate-menubar-hook)
bdbc7685
RS
171 ;; Compute tmm-km-list from MENU.
172 ;; tmm-km-list is an alist of (STRING . MEANING).
173 ;; It has no other elements.
174 ;; The order of elements in tmm-km-list is the order of the menu bar.
70edffb1
SM
175 (if (not not-menu)
176 (map-keymap (lambda (k v) (tmm-get-keymap (cons k v))) menu)
177 (dolist (elt menu)
178 (cond
179 ((stringp elt) (setq gl-str elt))
180 ((listp elt) (tmm-get-keymap elt not-menu))
181 ((vectorp elt)
182 (dotimes (i (length elt))
183 (tmm-get-keymap (cons i (aref elt i)) not-menu))))))
4d0463b9 184 (setq tmm-km-list (nreverse tmm-km-list))
bdbc7685
RS
185 ;; Choose an element of tmm-km-list; put it in choice.
186 (if (and not-menu (= 1 (length tmm-km-list)))
187 ;; If this is the top-level of an x-popup-menu menu,
188 ;; and there is just one pane, choose that one silently.
189 ;; This way we only ask the user one question,
190 ;; for which element of that pane.
191 (setq choice (cdr (car tmm-km-list)))
ae3f2f3c
RS
192 (unless tmm-km-list
193 (error "Empty menu reached"))
bdbc7685
RS
194 (and tmm-km-list
195 (let ((index-of-default 0))
196 (if tmm-mid-prompt
197 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
198 t)
199 ;; Find the default item's index within the menu bar.
200 ;; We use this to decide the initial minibuffer contents
201 ;; and initial history position.
202 (if default-item
a4e03d18 203 (let ((tail menu) visible)
bdbc7685
RS
204 (while (and tail
205 (not (eq (car-safe (car tail)) default-item)))
206 ;; Be careful to count only the elements of MENU
207 ;; that actually constitute menu bar items.
208 (if (and (consp (car tail))
d0bca3c9 209 (or (stringp (car-safe (cdr (car tail))))
a4e03d18
NR
210 (and
211 (eq (car-safe (cdr (car tail))) 'menu-item)
212 (progn
213 (setq visible
214 (plist-get
215 (nthcdr 4 (car tail)) :visible))
216 (or (not visible) (eval visible))))))
bdbc7685
RS
217 (setq index-of-default (1+ index-of-default)))
218 (setq tail (cdr tail)))))
04a5d30f
NR
219 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
220 (setq history
221 (reverse (delq nil
222 (mapcar
223 (lambda (elt)
224 (if (string-match prompt (car elt))
225 (car elt)))
226 tmm-km-list)))))
bdbc7685
RS
227 (setq history-len (length history))
228 (setq history (append history history history history))
229 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
3ae704f4
SM
230 (setq out
231 (if default-item
232 (car (nth index-of-default tmm-km-list))
233 (minibuffer-with-setup-hook #'tmm-add-prompt
234 (completing-read
235 (concat gl-str
236 " (up/down to change, PgUp to menu): ")
4d0463b9 237 (tmm--completion-table tmm-km-list) nil t nil
3ae704f4
SM
238 (cons 'history
239 (- (* 2 history-len) index-of-default))))))))
bdbc7685
RS
240 (setq choice (cdr (assoc out tmm-km-list)))
241 (and (null choice)
7287f2f3 242 (string-prefix-p tmm-c-prompt out)
bdbc7685
RS
243 (setq out (substring out (length tmm-c-prompt))
244 choice (cdr (assoc out tmm-km-list))))
ae3f2f3c 245 (and (null choice) out
bdbc7685
RS
246 (setq out (try-completion out tmm-km-list)
247 choice (cdr (assoc out tmm-km-list)))))
248 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
249 (setq chosen-string (car choice))
250 (setq choice (cdr choice))
251 (cond (in-popup
252 ;; We just did the inner level of a -popup menu.
253 choice)
254 ;; We just did the outer level. Do the inner level now.
97895167 255 (not-menu (tmm-prompt choice t))
bdbc7685
RS
256 ;; We just handled a menu keymap and found another keymap.
257 ((keymapp choice)
258 (if (symbolp choice)
259 (setq choice (indirect-function choice)))
260 (condition-case nil
261 (require 'mouse)
262 (error nil))
bdbc7685
RS
263 (tmm-prompt choice))
264 ;; We just handled a menu keymap and found a command.
265 (choice
266 (if chosen-string
3132f319
KH
267 (progn
268 (setq last-command-event chosen-string)
269 (call-interactively choice))
bdbc7685 270 choice)))))
20062d6b 271
20062d6b 272(defun tmm-add-shortcuts (list)
3ae704f4 273 "Add shortcuts to cars of elements of the list.
20062d6b 274Takes a list of lists with a string as car, returns list with
fc225f66
RS
275shortcuts added to these cars.
276Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
670ce6ea
RS
277 (let ((tmm-next-shortcut-digit ?0))
278 (mapcar 'tmm-add-one-shortcut (reverse list))))
20062d6b 279
670ce6ea
RS
280(defsubst tmm-add-one-shortcut (elt)
281;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
04a5d30f
NR
282 (cond
283 ((eq (cddr elt) 'ignore)
284 (cons (concat " " (make-string (length tmm-mid-prompt) ?\-)
285 (car elt))
286 (cdr elt)))
287 (t
288 (let* ((str (car elt))
289 (paren (string-match "(" str))
290 (pos 0) (word 0) char)
291 (catch 'done ; ??? is this slow?
292 (while (and (or (not tmm-shortcut-words) ; no limit on words
293 (< word tmm-shortcut-words)) ; try n words
294 (setq pos (string-match "\\w+" str pos)) ; get next word
295 (not (and paren (> pos paren)))) ; don't go past "(binding.."
296 (if (or (= pos 0)
297 (/= (aref str (1- pos)) ?.)) ; avoid file extensions
298 (let ((shortcut-style
299 (if (listp tmm-shortcut-style) ; convert to list
300 tmm-shortcut-style
301 (list tmm-shortcut-style))))
302 (while shortcut-style ; try upcase and downcase variants
303 (setq char (funcall (car shortcut-style) (aref str pos)))
304 (if (not (memq char tmm-short-cuts)) (throw 'done char))
305 (setq shortcut-style (cdr shortcut-style)))))
306 (setq word (1+ word))
307 (setq pos (match-end 0)))
308 (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
309 (setq char tmm-next-shortcut-digit)
310 (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
311 (if (not (memq char tmm-short-cuts)) (throw 'done char)))
312 (setq char nil))
313 (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
314 (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
315 ;; keep them lined up in columns
a322861f 316 (make-string (1+ (length tmm-mid-prompt)) ?\s))
04a5d30f
NR
317 str)
318 (cdr elt))))))
670ce6ea
RS
319
320;; This returns the old map.
fe03654a 321(defun tmm-define-keys (minibuffer)
670ce6ea
RS
322 (let ((map (make-sparse-keymap)))
323 (suppress-keymap map t)
a2754b6c
SM
324 (dolist (c tmm-short-cuts)
325 (if (listp tmm-shortcut-style)
326 (define-key map (char-to-string c) 'tmm-shortcut)
327 ;; only one kind of letters are shortcuts, so map both upcase and
328 ;; downcase input to the same
329 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
330 (define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
670ce6ea
RS
331 (if minibuffer
332 (progn
333 (define-key map [pageup] 'tmm-goto-completions)
334 (define-key map [prior] 'tmm-goto-completions)
335 (define-key map "\ev" 'tmm-goto-completions)
336 (define-key map "\C-n" 'next-history-element)
337 (define-key map "\C-p" 'previous-history-element)))
338 (prog1 (current-local-map)
339 (use-local-map (append map (current-local-map))))))
340
341(defun tmm-completion-delete-prompt ()
7287f2f3 342 (with-current-buffer standard-output
9241efbe 343 (goto-char (point-min))
7287f2f3 344 (delete-region (point) (search-forward "Possible completions are:\n"))))
b46324e6 345
04a5d30f
NR
346(defun tmm-remove-inactive-mouse-face ()
347 "Remove the mouse-face property from inactive menu items."
348 (let ((inhibit-read-only t)
349 (inactive-string
350 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
351 next)
352 (save-excursion
353 (goto-char (point-min))
354 (while (not (eobp))
355 (setq next (next-single-char-property-change (point) 'mouse-face))
356 (when (looking-at inactive-string)
357 (remove-text-properties (point) next '(mouse-face))
e43cbeae 358 (add-text-properties (point) next '(face tmm-inactive)))
04a5d30f
NR
359 (goto-char next)))
360 (set-buffer-modified-p nil)))
361
20062d6b 362(defun tmm-add-prompt ()
04a5d30f
NR
363 (unless tmm-c-prompt
364 (error "No active menu entries"))
362b9d48
GM
365 (setq tmm-old-mb-map (tmm-define-keys t))
366 ;; Get window and hide it for electric mode to get correct size
7287f2f3
SM
367 (or tmm-completion-prompt
368 (add-hook 'completion-setup-hook
369 'tmm-completion-delete-prompt 'append))
370 (unwind-protect
371 (minibuffer-completion-help)
372 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
373 (with-current-buffer "*Completions*"
362b9d48
GM
374 (tmm-remove-inactive-mouse-face)
375 (when tmm-completion-prompt
7287f2f3
SM
376 (let ((inhibit-read-only t))
377 (goto-char (point-min))
378 (insert tmm-completion-prompt))))
362b9d48 379 (insert tmm-c-prompt))
20062d6b 380
20062d6b 381(defun tmm-shortcut ()
fc225f66 382 "Choose the shortcut that the user typed."
20062d6b 383 (interactive)
8989a920 384 (let ((c last-command-event) s)
670ce6ea
RS
385 (if (symbolp tmm-shortcut-style)
386 (setq c (funcall tmm-shortcut-style c)))
387 (if (memq c tmm-short-cuts)
fc225f66
RS
388 (if (equal (buffer-name) "*Completions*")
389 (progn
b74f6606 390 (goto-char (point-min))
fc225f66 391 (re-search-forward
670ce6ea 392 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
fc225f66 393 (choose-completion))
c2a8d4a7
GM
394 ;; In minibuffer
395 (delete-region (minibuffer-prompt-end) (point-max))
a2754b6c
SM
396 (dolist (elt tmm-km-list)
397 (if (string=
398 (substring (car elt) 0
399 (min (1+ (length tmm-mid-prompt))
400 (length (car elt))))
401 (concat (char-to-string c) tmm-mid-prompt))
402 (setq s (car elt))))
fc225f66
RS
403 (insert s)
404 (exit-minibuffer)))))
20062d6b
RS
405
406(defun tmm-goto-completions ()
3ae704f4 407 "Jump to the completions buffer."
20062d6b 408 (interactive)
fbe91bbd
GM
409 (let ((prompt-end (minibuffer-prompt-end)))
410 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
3ae704f4 411 ;; FIXME: Why?
fbe91bbd 412 (delete-region prompt-end (point-max)))
fc225f66 413 (switch-to-buffer-other-window "*Completions*")
20062d6b
RS
414 (search-forward tmm-c-prompt)
415 (search-backward tmm-c-prompt))
416
97895167 417(defun tmm-get-keymap (elt &optional in-x-menu)
3ae704f4 418 "Prepend (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
20062d6b 419The values are deduced from the argument ELT, that should be an
fc225f66 420element of keymap, an `x-popup-menu' argument, or an element of
20062d6b 421`x-popup-menu' argument (when IN-X-MENU is not-nil).
77cc5db0
RS
422This function adds the element only if it is not already present.
423It uses the free variable `tmm-table-undef' to keep undefined keys."
dc9ed794 424 (let (km str plist filter visible enable (event (car elt)))
20062d6b
RS
425 (setq elt (cdr elt))
426 (if (eq elt 'undefined)
427 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
42d140b4
KH
428 (unless (assoc event tmm-table-undef)
429 (cond ((if (listp elt)
430 (or (keymapp elt) (eq (car elt) 'lambda))
dc9ed794 431 (and (symbolp elt) (fboundp elt)))
42d140b4 432 (setq km elt))
0595722b 433
42d140b4
KH
434 ((if (listp (cdr-safe elt))
435 (or (keymapp (cdr-safe elt))
436 (eq (car (cdr-safe elt)) 'lambda))
dc9ed794 437 (and (symbolp (cdr-safe elt)) (fboundp (cdr-safe elt))))
42d140b4
KH
438 (setq km (cdr elt))
439 (and (stringp (car elt)) (setq str (car elt))))
0595722b 440
42d140b4
KH
441 ((if (listp (cdr-safe (cdr-safe elt)))
442 (or (keymapp (cdr-safe (cdr-safe elt)))
443 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
dc9ed794 444 (and (symbolp (cdr-safe (cdr-safe elt)))
70edffb1 445 (fboundp (cdr-safe (cdr-safe elt)))))
e9bed1ef 446 (setq km (cddr elt))
dc9ed794 447 (and (stringp (car elt)) (setq str (car elt))))
0595722b 448
42d140b4 449 ((eq (car-safe elt) 'menu-item)
0595722b 450 ;; (menu-item TITLE COMMAND KEY ...)
dd81ca0d 451 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
0595722b
GM
452 (when (consp (car-safe plist))
453 (setq plist (cdr-safe plist)))
42d140b4 454 (setq km (nth 2 elt))
c19cc275 455 (setq str (eval (nth 1 elt)))
dd81ca0d
RS
456 (setq filter (plist-get plist :filter))
457 (if filter
458 (setq km (funcall filter km)))
97895167
PJ
459 (setq visible (plist-get plist :visible))
460 (if visible
461 (setq km (and (eval visible) km)))
7ae862e2
NR
462 (setq enable (plist-get plist :enable))
463 (if enable
dc9ed794 464 (setq km (if (eval enable) km 'ignore))))
0595722b 465
42d140b4
KH
466 ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
467 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
468 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
dc9ed794
SM
469 (and (symbolp (cdr-safe (cdr-safe (cdr-safe elt))))
470 (fboundp (cdr-safe (cdr-safe (cdr-safe elt))))))
70edffb1 471 ; New style of easy-menu
e9bed1ef 472 (setq km (cdr (cddr elt)))
dc9ed794 473 (and (stringp (car elt)) (setq str (car elt))))
0595722b 474
42d140b4 475 ((stringp event) ; x-popup or x-popup element
70edffb1
SM
476 (setq str event)
477 (setq event nil)
478 (setq km (if (or in-x-menu (stringp (car-safe elt)))
479 elt (cons 'keymap elt)))))
362b9d48 480 (unless (or (eq km 'ignore) (null str))
dc9ed794
SM
481 (let ((binding (where-is-internal km nil t)))
482 (when binding
483 (setq binding (key-description binding))
484 ;; Try to align the keybindings.
485 (let ((colwidth (min 30 (- (/ (window-width) 2) 10))))
486 (setq str
487 (concat str
488 (make-string (max 2 (- colwidth
489 (string-width str)
490 (string-width binding)))
491 ?\s)
492 binding)))))))
20062d6b 493 (and km (stringp km) (setq str km))
2a9f2437
RS
494 ;; Verify that the command is enabled;
495 ;; if not, don't mention it.
496 (when (and km (symbolp km) (get km 'menu-enable))
04a5d30f 497 (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
20062d6b
RS
498 (and km str
499 (or (assoc str tmm-km-list)
c19cc275 500 (push (cons str (cons event km)) tmm-km-list))))))
20062d6b 501
20062d6b 502(defun tmm-get-keybind (keyseq)
fc225f66 503 "Return the current binding of KEYSEQ, merging prefix definitions.
91a5e367 504If KEYSEQ is a prefix key that has local and global bindings,
fc225f66
RS
505we merge them into a single keymap which shows the proper order of the menu.
506However, for the menu bar itself, the value does not take account
507of `menu-bar-final-items'."
4d0463b9 508 (lookup-key (cons 'keymap (nreverse (current-active-maps))) keyseq))
20062d6b 509
20062d6b
RS
510(provide 'tmm)
511
20062d6b 512;;; tmm.el ends here