*** empty log message ***
[bpt/emacs.git] / lisp / tmm.el
CommitLineData
be010748 1;;; tmm.el --- text mode access to menu-bar
20062d6b 2
0d30b337 3;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002, 2003,
d7a0267c 4;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
20062d6b
RS
5
6;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
fc225f66 7;; Maintainer: FSF
97895167 8;; Keywords: convenience
20062d6b 9
d440e474 10;; This file is part of GNU Emacs.
20062d6b
RS
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267 23;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
20062d6b 26
b578f267 27;;; Commentary:
20062d6b 28
97895167 29;; This package provides text mode access to the menu bar.
20062d6b 30
b578f267 31;;; Code:
20062d6b
RS
32
33(require 'electric)
20062d6b 34
4bef9110
SE
35(defgroup tmm nil
36 "Text mode access to menu-bar."
37 :prefix "tmm-"
38 :group 'menu)
39
20062d6b
RS
40;;; The following will be localized, added only to pacify the compiler.
41(defvar tmm-short-cuts)
fc225f66 42(defvar tmm-old-mb-map nil)
20062d6b 43(defvar tmm-old-comp-map)
93253b0e 44(defvar tmm-c-prompt nil)
20062d6b 45(defvar tmm-km-list)
670ce6ea 46(defvar tmm-next-shortcut-digit)
20062d6b
RS
47(defvar tmm-table-undef)
48
e6a5c7de 49;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
b46324e6 50;;;###autoload (define-key global-map [f10] 'tmm-menubar)
77cc5db0 51;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
afb1835e 52
20062d6b 53;;;###autoload
77cc5db0 54(defun tmm-menubar (&optional x-position)
20062d6b 55 "Text-mode emulation of looking and choosing from a menubar.
77cc5db0
RS
56See the documentation for `tmm-prompt'.
57X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
58we make that menu bar item (the one at that position) the default choice."
20062d6b
RS
59 (interactive)
60 (run-hooks 'menu-bar-update-hook)
fc225f66 61 ;; Obey menu-bar-final-items; put those items last.
77cc5db0
RS
62 (let ((menu-bar (tmm-get-keybind [menu-bar]))
63 menu-bar-item)
fc225f66
RS
64 (let ((list menu-bar-final-items))
65 (while list
66 (let ((item (car list)))
67 ;; ITEM is the name of an item that we want to put last.
68 ;; Find it in MENU-BAR and move it to the end.
69 (let ((this-one (assq item menu-bar)))
70 (setq menu-bar (append (delq this-one menu-bar)
71 (list this-one)))))
72 (setq list (cdr list))))
77cc5db0 73 (if x-position
93253b0e
NR
74 (let ((tail menu-bar) (column 0)
75 this-one name)
573e00ef 76 (while (and tail (<= column x-position))
77cc5db0 77 (setq this-one (car tail))
573e00ef
NR
78 (if (and (consp this-one)
79 (consp (cdr this-one))
93253b0e
NR
80 (setq name ; nil if menu-item
81 (cond ((stringp (nth 1 this-one))
82 (nth 1 this-one))
83 ((stringp (nth 2 this-one))
84 (nth 2 this-one)))))
85 (setq column (+ column (length name) 1)))
77cc5db0
RS
86 (setq tail (cdr tail)))
87 (setq menu-bar-item (car this-one))))
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 "==>"
97895167 100 "*String to insert between shortcut and menu item.
670ce6ea 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"
670ce6ea
RS
115 "*Help text to insert on the top of the completion buffer.
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)
97895167
PJ
122 "*What letters to use as menu shortcuts.
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
670ce6ea 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
20062d6b 142;;;###autoload
bdbc7685 143(defun tmm-prompt (menu &optional in-popup default-item)
20062d6b 144 "Text-mode emulation of calling the bindings in keymap.
77cc5db0
RS
145Creates a text-mode menu of possible choices. You can access the elements
146in the menu in two ways:
147 *) via history mechanism from minibuffer;
20062d6b
RS
148 *) Or via completion-buffer that is automatically shown.
149The last alternative is currently a hack, you cannot use mouse reliably.
bdbc7685
RS
150
151MENU is like the MENU argument to `x-popup-menu': either a
152keymap or an alist of alists.
153DEFAULT-ITEM, if non-nil, specifies an initial default choice.
154Its value should be an event that has a binding in MENU."
155 ;; If the optional argument IN-POPUP is t,
156 ;; then MENU is an alist of elements of the form (STRING . VALUE).
157 ;; That is used for recursive calls only.
158 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
159 ; so it doesn't have a name.
160 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
161 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
162 chosen-string choice
163 (not-menu (not (keymapp menu))))
20062d6b 164 (run-hooks 'activate-menubar-hook)
bdbc7685
RS
165 ;; Compute tmm-km-list from MENU.
166 ;; tmm-km-list is an alist of (STRING . MEANING).
167 ;; It has no other elements.
168 ;; The order of elements in tmm-km-list is the order of the menu bar.
e07436e1
DL
169 (mapc (lambda (elt)
170 (if (stringp elt)
171 (setq gl-str elt)
172 (and (listp elt) (tmm-get-keymap elt not-menu))))
bdbc7685
RS
173 menu)
174 ;; Choose an element of tmm-km-list; put it in choice.
175 (if (and not-menu (= 1 (length tmm-km-list)))
176 ;; If this is the top-level of an x-popup-menu menu,
177 ;; and there is just one pane, choose that one silently.
178 ;; This way we only ask the user one question,
179 ;; for which element of that pane.
180 (setq choice (cdr (car tmm-km-list)))
ae3f2f3c
RS
181 (unless tmm-km-list
182 (error "Empty menu reached"))
bdbc7685
RS
183 (and tmm-km-list
184 (let ((index-of-default 0))
185 (if tmm-mid-prompt
186 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
187 t)
188 ;; Find the default item's index within the menu bar.
189 ;; We use this to decide the initial minibuffer contents
190 ;; and initial history position.
191 (if default-item
192 (let ((tail menu))
193 (while (and tail
194 (not (eq (car-safe (car tail)) default-item)))
195 ;; Be careful to count only the elements of MENU
196 ;; that actually constitute menu bar items.
197 (if (and (consp (car tail))
d0bca3c9
GM
198 (or (stringp (car-safe (cdr (car tail))))
199 (eq (car-safe (cdr (car tail))) 'menu-item)))
bdbc7685
RS
200 (setq index-of-default (1+ index-of-default)))
201 (setq tail (cdr tail)))))
04a5d30f
NR
202 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
203 (setq history
204 (reverse (delq nil
205 (mapcar
206 (lambda (elt)
207 (if (string-match prompt (car elt))
208 (car elt)))
209 tmm-km-list)))))
bdbc7685
RS
210 (setq history-len (length history))
211 (setq history (append history history history history))
212 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
213 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
573e00ef
NR
214 (if default-item
215 (setq out (car (nth index-of-default tmm-km-list)))
216 (save-excursion
217 (unwind-protect
218 (setq out
219 (completing-read
220 (concat gl-str
221 " (up/down to change, PgUp to menu): ")
222 tmm-km-list nil t nil
223 (cons 'history
224 (- (* 2 history-len) index-of-default))))
225 (save-excursion
226 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
227 (if (get-buffer "*Completions*")
228 (progn
229 (set-buffer "*Completions*")
230 (use-local-map tmm-old-comp-map)
231 (bury-buffer (current-buffer))))))))))
bdbc7685
RS
232 (setq choice (cdr (assoc out tmm-km-list)))
233 (and (null choice)
234 (> (length out) (length tmm-c-prompt))
235 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
236 (setq out (substring out (length tmm-c-prompt))
237 choice (cdr (assoc out tmm-km-list))))
ae3f2f3c 238 (and (null choice) out
bdbc7685
RS
239 (setq out (try-completion out tmm-km-list)
240 choice (cdr (assoc out tmm-km-list)))))
241 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
242 (setq chosen-string (car choice))
243 (setq choice (cdr choice))
244 (cond (in-popup
245 ;; We just did the inner level of a -popup menu.
246 choice)
247 ;; We just did the outer level. Do the inner level now.
97895167 248 (not-menu (tmm-prompt choice t))
bdbc7685
RS
249 ;; We just handled a menu keymap and found another keymap.
250 ((keymapp choice)
251 (if (symbolp choice)
252 (setq choice (indirect-function choice)))
253 (condition-case nil
254 (require 'mouse)
255 (error nil))
256 (condition-case nil
257 (x-popup-menu nil choice) ; Get the shortcuts
258 (error nil))
259 (tmm-prompt choice))
260 ;; We just handled a menu keymap and found a command.
261 (choice
262 (if chosen-string
3132f319
KH
263 (progn
264 (setq last-command-event chosen-string)
265 (call-interactively choice))
bdbc7685 266 choice)))))
20062d6b 267
20062d6b
RS
268(defun tmm-add-shortcuts (list)
269 "Adds shortcuts to cars of elements of the list.
270Takes a list of lists with a string as car, returns list with
fc225f66
RS
271shortcuts added to these cars.
272Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
670ce6ea
RS
273 (let ((tmm-next-shortcut-digit ?0))
274 (mapcar 'tmm-add-one-shortcut (reverse list))))
20062d6b 275
670ce6ea
RS
276(defsubst tmm-add-one-shortcut (elt)
277;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
04a5d30f
NR
278 (cond
279 ((eq (cddr elt) 'ignore)
280 (cons (concat " " (make-string (length tmm-mid-prompt) ?\-)
281 (car elt))
282 (cdr elt)))
283 (t
284 (let* ((str (car elt))
285 (paren (string-match "(" str))
286 (pos 0) (word 0) char)
287 (catch 'done ; ??? is this slow?
288 (while (and (or (not tmm-shortcut-words) ; no limit on words
289 (< word tmm-shortcut-words)) ; try n words
290 (setq pos (string-match "\\w+" str pos)) ; get next word
291 (not (and paren (> pos paren)))) ; don't go past "(binding.."
292 (if (or (= pos 0)
293 (/= (aref str (1- pos)) ?.)) ; avoid file extensions
294 (let ((shortcut-style
295 (if (listp tmm-shortcut-style) ; convert to list
296 tmm-shortcut-style
297 (list tmm-shortcut-style))))
298 (while shortcut-style ; try upcase and downcase variants
299 (setq char (funcall (car shortcut-style) (aref str pos)))
300 (if (not (memq char tmm-short-cuts)) (throw 'done char))
301 (setq shortcut-style (cdr shortcut-style)))))
302 (setq word (1+ word))
303 (setq pos (match-end 0)))
304 (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
305 (setq char tmm-next-shortcut-digit)
306 (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
307 (if (not (memq char tmm-short-cuts)) (throw 'done char)))
308 (setq char nil))
309 (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
310 (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
311 ;; keep them lined up in columns
a322861f 312 (make-string (1+ (length tmm-mid-prompt)) ?\s))
04a5d30f
NR
313 str)
314 (cdr elt))))))
670ce6ea
RS
315
316;; This returns the old map.
fe03654a 317(defun tmm-define-keys (minibuffer)
670ce6ea
RS
318 (let ((map (make-sparse-keymap)))
319 (suppress-keymap map t)
e07436e1
DL
320 (mapc
321 (lambda (c)
322 (if (listp tmm-shortcut-style)
323 (define-key map (char-to-string c) 'tmm-shortcut)
324 ;; only one kind of letters are shortcuts, so map both upcase and
325 ;; downcase input to the same
326 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
327 (define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
670ce6ea
RS
328 tmm-short-cuts)
329 (if minibuffer
330 (progn
331 (define-key map [pageup] 'tmm-goto-completions)
332 (define-key map [prior] 'tmm-goto-completions)
333 (define-key map "\ev" 'tmm-goto-completions)
334 (define-key map "\C-n" 'next-history-element)
335 (define-key map "\C-p" 'previous-history-element)))
336 (prog1 (current-local-map)
337 (use-local-map (append map (current-local-map))))))
338
339(defun tmm-completion-delete-prompt ()
340 (set-buffer standard-output)
341 (goto-char 1)
342 (delete-region 1 (search-forward "Possible completions are:\n")))
b46324e6 343
04a5d30f
NR
344(defun tmm-remove-inactive-mouse-face ()
345 "Remove the mouse-face property from inactive menu items."
346 (let ((inhibit-read-only t)
347 (inactive-string
348 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
349 next)
350 (save-excursion
351 (goto-char (point-min))
352 (while (not (eobp))
353 (setq next (next-single-char-property-change (point) 'mouse-face))
354 (when (looking-at inactive-string)
355 (remove-text-properties (point) next '(mouse-face))
e43cbeae 356 (add-text-properties (point) next '(face tmm-inactive)))
04a5d30f
NR
357 (goto-char next)))
358 (set-buffer-modified-p nil)))
359
20062d6b
RS
360(defun tmm-add-prompt ()
361 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
afb1835e 362 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
04a5d30f
NR
363 (unless tmm-c-prompt
364 (error "No active menu entries"))
b46324e6 365 (let ((win (selected-window)))
670ce6ea 366 (setq tmm-old-mb-map (tmm-define-keys t))
20062d6b 367 ;; Get window and hide it for electric mode to get correct size
97895167 368 (save-window-excursion
fc225f66
RS
369 (let ((completions
370 (mapcar 'car minibuffer-completion-table)))
670ce6ea
RS
371 (or tmm-completion-prompt
372 (add-hook 'completion-setup-hook
373 'tmm-completion-delete-prompt 'append))
fc225f66 374 (with-output-to-temp-buffer "*Completions*"
670ce6ea
RS
375 (display-completion-list completions))
376 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
04a5d30f
NR
377 (set-buffer "*Completions*")
378 (tmm-remove-inactive-mouse-face)
0595722b 379 (when tmm-completion-prompt
0595722b
GM
380 (let ((buffer-read-only nil))
381 (goto-char (point-min))
382 (insert tmm-completion-prompt))))
ca85cf8a 383 (save-selected-window
20062d6b
RS
384 (other-window 1) ; Electric-pop-up-window does
385 ; not work in minibuffer
ca85cf8a
RS
386 (Electric-pop-up-window "*Completions*")
387 (with-current-buffer "*Completions*"
388 (setq tmm-old-comp-map (tmm-define-keys nil))))
20062d6b
RS
389 (insert tmm-c-prompt)))
390
391(defun tmm-delete-map ()
afb1835e 392 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
fc225f66
RS
393 (if tmm-old-mb-map
394 (use-local-map tmm-old-mb-map)))
20062d6b
RS
395
396(defun tmm-shortcut ()
fc225f66 397 "Choose the shortcut that the user typed."
20062d6b 398 (interactive)
670ce6ea
RS
399 (let ((c last-command-char) s)
400 (if (symbolp tmm-shortcut-style)
401 (setq c (funcall tmm-shortcut-style c)))
402 (if (memq c tmm-short-cuts)
fc225f66
RS
403 (if (equal (buffer-name) "*Completions*")
404 (progn
b74f6606 405 (goto-char (point-min))
fc225f66 406 (re-search-forward
670ce6ea 407 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
fc225f66 408 (choose-completion))
c2a8d4a7
GM
409 ;; In minibuffer
410 (delete-region (minibuffer-prompt-end) (point-max))
e07436e1
DL
411 (mapc (lambda (elt)
412 (if (string=
97895167 413 (substring (car elt) 0
e07436e1
DL
414 (min (1+ (length tmm-mid-prompt))
415 (length (car elt))))
416 (concat (char-to-string c) tmm-mid-prompt))
417 (setq s (car elt))))
fc225f66
RS
418 tmm-km-list)
419 (insert s)
420 (exit-minibuffer)))))
20062d6b
RS
421
422(defun tmm-goto-completions ()
423 (interactive)
fbe91bbd
GM
424 (let ((prompt-end (minibuffer-prompt-end)))
425 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
426 (delete-region prompt-end (point-max)))
fc225f66 427 (switch-to-buffer-other-window "*Completions*")
20062d6b
RS
428 (search-forward tmm-c-prompt)
429 (search-backward tmm-c-prompt))
430
97895167 431(defun tmm-get-keymap (elt &optional in-x-menu)
20062d6b
RS
432 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
433The values are deduced from the argument ELT, that should be an
fc225f66 434element of keymap, an `x-popup-menu' argument, or an element of
20062d6b 435`x-popup-menu' argument (when IN-X-MENU is not-nil).
77cc5db0
RS
436This function adds the element only if it is not already present.
437It uses the free variable `tmm-table-undef' to keep undefined keys."
7ae862e2 438 (let (km str cache plist filter visible enable (event (car elt)))
20062d6b
RS
439 (setq elt (cdr elt))
440 (if (eq elt 'undefined)
441 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
42d140b4
KH
442 (unless (assoc event tmm-table-undef)
443 (cond ((if (listp elt)
444 (or (keymapp elt) (eq (car elt) 'lambda))
445 (fboundp elt))
446 (setq km elt))
0595722b 447
42d140b4
KH
448 ((if (listp (cdr-safe elt))
449 (or (keymapp (cdr-safe elt))
450 (eq (car (cdr-safe elt)) 'lambda))
451 (fboundp (cdr-safe elt)))
452 (setq km (cdr elt))
453 (and (stringp (car elt)) (setq str (car elt))))
0595722b 454
42d140b4
KH
455 ((if (listp (cdr-safe (cdr-safe elt)))
456 (or (keymapp (cdr-safe (cdr-safe elt)))
457 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
458 (fboundp (cdr-safe (cdr-safe elt))))
459 (setq km (cdr (cdr elt)))
460 (and (stringp (car elt)) (setq str (car elt)))
461 (and str
462 (stringp (cdr (car (cdr elt)))) ; keyseq cache
463 (setq cache (cdr (car (cdr elt))))
464 cache (setq str (concat str cache))))
0595722b 465
42d140b4 466 ((eq (car-safe elt) 'menu-item)
0595722b 467 ;; (menu-item TITLE COMMAND KEY ...)
dd81ca0d 468 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
0595722b
GM
469 (when (consp (car-safe plist))
470 (setq plist (cdr-safe plist)))
42d140b4 471 (setq km (nth 2 elt))
c19cc275 472 (setq str (eval (nth 1 elt)))
dd81ca0d
RS
473 (setq filter (plist-get plist :filter))
474 (if filter
475 (setq km (funcall filter km)))
97895167
PJ
476 (setq visible (plist-get plist :visible))
477 (if visible
478 (setq km (and (eval visible) km)))
7ae862e2
NR
479 (setq enable (plist-get plist :enable))
480 (if enable
04a5d30f 481 (setq km (if (eval enable) km 'ignore)))
42d140b4 482 (and str
ae3f2f3c 483 (consp (nth 3 elt))
437d9247
RS
484 (stringp (cdr (nth 3 elt))) ; keyseq cache
485 (setq cache (cdr (nth 3 elt)))
42d140b4
KH
486 cache
487 (setq str (concat str cache))))
0595722b 488
42d140b4
KH
489 ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
490 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
491 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
492 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
493 ; New style of easy-menu
494 (setq km (cdr (cdr (cdr elt))))
495 (and (stringp (car elt)) (setq str (car elt)))
496 (and str
497 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
498 (setq cache (cdr (car (cdr (cdr elt)))))
499 cache (setq str (concat str cache))))
0595722b 500
42d140b4
KH
501 ((stringp event) ; x-popup or x-popup element
502 (if (or in-x-menu (stringp (car-safe elt)))
503 (setq str event event nil km elt)
504 (setq str event event nil km (cons 'keymap elt))
505 ))))
20062d6b 506 (and km (stringp km) (setq str km))
2a9f2437
RS
507 ;; Verify that the command is enabled;
508 ;; if not, don't mention it.
509 (when (and km (symbolp km) (get km 'menu-enable))
04a5d30f 510 (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
20062d6b
RS
511 (and km str
512 (or (assoc str tmm-km-list)
c19cc275 513 (push (cons str (cons event km)) tmm-km-list))))))
20062d6b 514
20062d6b 515(defun tmm-get-keybind (keyseq)
fc225f66 516 "Return the current binding of KEYSEQ, merging prefix definitions.
91a5e367 517If KEYSEQ is a prefix key that has local and global bindings,
fc225f66
RS
518we merge them into a single keymap which shows the proper order of the menu.
519However, for the menu bar itself, the value does not take account
520of `menu-bar-final-items'."
93253b0e 521 (let (allbind bind minorbind localbind globalbind)
fc225f66
RS
522 (setq bind (key-binding keyseq))
523 ;; If KEYSEQ is a prefix key, then BIND is either nil
524 ;; or a symbol defined as a keymap (which satisfies keymapp).
525 (if (keymapp bind)
526 (setq bind nil))
527 ;; If we have a non-keymap definition, return that.
528 (or bind
529 (progn
530 ;; Otherwise, it is a prefix, so make a list of the subcommands.
531 ;; Make a list of all the bindings in all the keymaps.
93253b0e
NR
532 (setq minorbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
533 (setq localbind (local-key-binding keyseq))
534 (setq globalbind (cdr (global-key-binding keyseq)))
535
536 ;; If items have been redefined/undefined locally, remove them from
537 ;; the global list.
538 (dolist (minor minorbind)
539 (dolist (item (cdr minor))
540 (setq globalbind (assq-delete-all (car item) globalbind))))
541 (dolist (item (cdr localbind))
542 (setq globalbind (assq-delete-all (car item) globalbind)))
543
544 (setq globalbind (cons 'keymap globalbind))
545 (setq allbind (cons globalbind (cons localbind minorbind)))
546
fc225f66 547 ;; Merge all the elements of ALLBIND into one keymap.
e07436e1
DL
548 (mapc (lambda (in)
549 (if (and (symbolp in) (keymapp in))
550 (setq in (symbol-function in)))
551 (and in (keymapp in)
552 (if (keymapp bind)
553 (setq bind (nconc bind (copy-sequence (cdr in))))
554 (setq bind (copy-sequence in)))))
fc225f66
RS
555 allbind)
556 ;; Return that keymap.
557 bind))))
20062d6b
RS
558
559(add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
560
20062d6b
RS
561(provide 'tmm)
562
ab5796a9 563;;; arch-tag: e7ddbdb6-4b95-4da3-afbe-ad6063d112f4
20062d6b 564;;; tmm.el ends here