Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / tmm.el
CommitLineData
be010748 1;;; tmm.el --- text mode access to menu-bar
20062d6b 2
73b0cd50 3;; Copyright (C) 1994-1996, 2000-2011 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)
20062d6b 40(defvar tmm-old-comp-map)
93253b0e 41(defvar tmm-c-prompt nil)
20062d6b 42(defvar tmm-km-list)
670ce6ea 43(defvar tmm-next-shortcut-digit)
20062d6b
RS
44(defvar tmm-table-undef)
45
e6a5c7de 46;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
77cc5db0 47;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
afb1835e 48
20062d6b 49;;;###autoload
77cc5db0 50(defun tmm-menubar (&optional x-position)
20062d6b 51 "Text-mode emulation of looking and choosing from a menubar.
77cc5db0
RS
52See the documentation for `tmm-prompt'.
53X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
54we make that menu bar item (the one at that position) the default choice."
20062d6b
RS
55 (interactive)
56 (run-hooks 'menu-bar-update-hook)
fc225f66 57 ;; Obey menu-bar-final-items; put those items last.
77cc5db0
RS
58 (let ((menu-bar (tmm-get-keybind [menu-bar]))
59 menu-bar-item)
fc225f66
RS
60 (let ((list menu-bar-final-items))
61 (while list
62 (let ((item (car list)))
63 ;; ITEM is the name of an item that we want to put last.
64 ;; Find it in MENU-BAR and move it to the end.
65 (let ((this-one (assq item menu-bar)))
66 (setq menu-bar (append (delq this-one menu-bar)
67 (list this-one)))))
68 (setq list (cdr list))))
77cc5db0 69 (if x-position
93253b0e 70 (let ((tail menu-bar) (column 0)
a4e03d18 71 this-one name visible)
573e00ef 72 (while (and tail (<= column x-position))
77cc5db0 73 (setq this-one (car tail))
573e00ef
NR
74 (if (and (consp this-one)
75 (consp (cdr this-one))
a4e03d18 76 (setq name ;simple menu
93253b0e
NR
77 (cond ((stringp (nth 1 this-one))
78 (nth 1 this-one))
a4e03d18
NR
79 ;extended menu
80 ((stringp (nth 2 this-one))
81 (setq visible (plist-get
82 (nthcdr 4 this-one) :visible))
83 (unless (and visible (not (eval visible)))
84 (nth 2 this-one))))))
93253b0e 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 "==>"
c03aab72 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"
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
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 169 (mapc (lambda (elt)
362b9d48
GM
170 (cond
171 ((stringp elt) (setq gl-str elt))
172 ((listp elt) (tmm-get-keymap elt not-menu))
173 ((vectorp elt)
174 (dotimes (i (length elt))
175 (tmm-get-keymap (cons i (aref elt i)) not-menu)))))
176 menu)
bdbc7685
RS
177 ;; Choose an element of tmm-km-list; put it in choice.
178 (if (and not-menu (= 1 (length tmm-km-list)))
179 ;; If this is the top-level of an x-popup-menu menu,
180 ;; and there is just one pane, choose that one silently.
181 ;; This way we only ask the user one question,
182 ;; for which element of that pane.
183 (setq choice (cdr (car tmm-km-list)))
ae3f2f3c
RS
184 (unless tmm-km-list
185 (error "Empty menu reached"))
bdbc7685
RS
186 (and tmm-km-list
187 (let ((index-of-default 0))
188 (if tmm-mid-prompt
189 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
190 t)
191 ;; Find the default item's index within the menu bar.
192 ;; We use this to decide the initial minibuffer contents
193 ;; and initial history position.
194 (if default-item
a4e03d18 195 (let ((tail menu) visible)
bdbc7685
RS
196 (while (and tail
197 (not (eq (car-safe (car tail)) default-item)))
198 ;; Be careful to count only the elements of MENU
199 ;; that actually constitute menu bar items.
200 (if (and (consp (car tail))
d0bca3c9 201 (or (stringp (car-safe (cdr (car tail))))
a4e03d18
NR
202 (and
203 (eq (car-safe (cdr (car tail))) 'menu-item)
204 (progn
205 (setq visible
206 (plist-get
207 (nthcdr 4 (car tail)) :visible))
208 (or (not visible) (eval visible))))))
bdbc7685
RS
209 (setq index-of-default (1+ index-of-default)))
210 (setq tail (cdr tail)))))
04a5d30f
NR
211 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
212 (setq history
213 (reverse (delq nil
214 (mapcar
215 (lambda (elt)
216 (if (string-match prompt (car elt))
217 (car elt)))
218 tmm-km-list)))))
bdbc7685
RS
219 (setq history-len (length history))
220 (setq history (append history history history history))
221 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
222 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
573e00ef
NR
223 (if default-item
224 (setq out (car (nth index-of-default tmm-km-list)))
225 (save-excursion
226 (unwind-protect
227 (setq out
228 (completing-read
229 (concat gl-str
230 " (up/down to change, PgUp to menu): ")
231 tmm-km-list nil t nil
232 (cons 'history
233 (- (* 2 history-len) index-of-default))))
c03aab72
SM
234 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
235 (if (get-buffer "*Completions*")
236 (with-current-buffer "*Completions*"
237 (use-local-map tmm-old-comp-map)
238 (bury-buffer (current-buffer)))))))))
bdbc7685
RS
239 (setq choice (cdr (assoc out tmm-km-list)))
240 (and (null choice)
241 (> (length out) (length tmm-c-prompt))
242 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
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
RS
272(defun tmm-add-shortcuts (list)
273 "Adds shortcuts to cars of elements of the list.
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)
e07436e1
DL
324 (mapc
325 (lambda (c)
326 (if (listp tmm-shortcut-style)
327 (define-key map (char-to-string c) 'tmm-shortcut)
328 ;; only one kind of letters are shortcuts, so map both upcase and
329 ;; downcase input to the same
330 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
331 (define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
670ce6ea
RS
332 tmm-short-cuts)
333 (if minibuffer
334 (progn
335 (define-key map [pageup] 'tmm-goto-completions)
336 (define-key map [prior] 'tmm-goto-completions)
337 (define-key map "\ev" 'tmm-goto-completions)
338 (define-key map "\C-n" 'next-history-element)
339 (define-key map "\C-p" 'previous-history-element)))
340 (prog1 (current-local-map)
341 (use-local-map (append map (current-local-map))))))
342
343(defun tmm-completion-delete-prompt ()
344 (set-buffer standard-output)
9241efbe
SM
345 (goto-char (point-min))
346 (delete-region (point) (search-forward "Possible completions are:\n")))
b46324e6 347
04a5d30f
NR
348(defun tmm-remove-inactive-mouse-face ()
349 "Remove the mouse-face property from inactive menu items."
350 (let ((inhibit-read-only t)
351 (inactive-string
352 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
353 next)
354 (save-excursion
355 (goto-char (point-min))
356 (while (not (eobp))
357 (setq next (next-single-char-property-change (point) 'mouse-face))
358 (when (looking-at inactive-string)
359 (remove-text-properties (point) next '(mouse-face))
e43cbeae 360 (add-text-properties (point) next '(face tmm-inactive)))
04a5d30f
NR
361 (goto-char next)))
362 (set-buffer-modified-p nil)))
363
20062d6b
RS
364(defun tmm-add-prompt ()
365 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
afb1835e 366 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
04a5d30f
NR
367 (unless tmm-c-prompt
368 (error "No active menu entries"))
362b9d48
GM
369 (setq tmm-old-mb-map (tmm-define-keys t))
370 ;; Get window and hide it for electric mode to get correct size
371 (save-window-excursion
372 (let ((completions
373 (mapcar 'car minibuffer-completion-table)))
374 (or tmm-completion-prompt
375 (add-hook 'completion-setup-hook
376 'tmm-completion-delete-prompt 'append))
377 (unwind-protect
378 (with-output-to-temp-buffer "*Completions*"
379 (display-completion-list completions))
380 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt)))
381 (set-buffer "*Completions*")
382 (tmm-remove-inactive-mouse-face)
383 (when tmm-completion-prompt
384 (let ((buffer-read-only nil))
385 (goto-char (point-min))
386 (insert tmm-completion-prompt))))
387 (save-selected-window
388 (other-window 1) ; Electric-pop-up-window does
20062d6b 389 ; not work in minibuffer
362b9d48
GM
390 (Electric-pop-up-window "*Completions*")
391 (with-current-buffer "*Completions*"
392 (setq tmm-old-comp-map (tmm-define-keys nil))))
393 (insert tmm-c-prompt))
20062d6b
RS
394
395(defun tmm-delete-map ()
afb1835e 396 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
fc225f66
RS
397 (if tmm-old-mb-map
398 (use-local-map tmm-old-mb-map)))
20062d6b
RS
399
400(defun tmm-shortcut ()
fc225f66 401 "Choose the shortcut that the user typed."
20062d6b 402 (interactive)
8989a920 403 (let ((c last-command-event) s)
670ce6ea
RS
404 (if (symbolp tmm-shortcut-style)
405 (setq c (funcall tmm-shortcut-style c)))
406 (if (memq c tmm-short-cuts)
fc225f66
RS
407 (if (equal (buffer-name) "*Completions*")
408 (progn
b74f6606 409 (goto-char (point-min))
fc225f66 410 (re-search-forward
670ce6ea 411 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
fc225f66 412 (choose-completion))
c2a8d4a7
GM
413 ;; In minibuffer
414 (delete-region (minibuffer-prompt-end) (point-max))
e07436e1
DL
415 (mapc (lambda (elt)
416 (if (string=
97895167 417 (substring (car elt) 0
e07436e1
DL
418 (min (1+ (length tmm-mid-prompt))
419 (length (car elt))))
420 (concat (char-to-string c) tmm-mid-prompt))
421 (setq s (car elt))))
fc225f66
RS
422 tmm-km-list)
423 (insert s)
424 (exit-minibuffer)))))
20062d6b
RS
425
426(defun tmm-goto-completions ()
427 (interactive)
fbe91bbd
GM
428 (let ((prompt-end (minibuffer-prompt-end)))
429 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
430 (delete-region prompt-end (point-max)))
fc225f66 431 (switch-to-buffer-other-window "*Completions*")
20062d6b
RS
432 (search-forward tmm-c-prompt)
433 (search-backward tmm-c-prompt))
434
97895167 435(defun tmm-get-keymap (elt &optional in-x-menu)
20062d6b
RS
436 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
437The values are deduced from the argument ELT, that should be an
fc225f66 438element of keymap, an `x-popup-menu' argument, or an element of
20062d6b 439`x-popup-menu' argument (when IN-X-MENU is not-nil).
77cc5db0
RS
440This function adds the element only if it is not already present.
441It uses the free variable `tmm-table-undef' to keep undefined keys."
dc9ed794 442 (let (km str plist filter visible enable (event (car elt)))
20062d6b
RS
443 (setq elt (cdr elt))
444 (if (eq elt 'undefined)
445 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
42d140b4
KH
446 (unless (assoc event tmm-table-undef)
447 (cond ((if (listp elt)
448 (or (keymapp elt) (eq (car elt) 'lambda))
dc9ed794 449 (and (symbolp elt) (fboundp elt)))
42d140b4 450 (setq km elt))
0595722b 451
42d140b4
KH
452 ((if (listp (cdr-safe elt))
453 (or (keymapp (cdr-safe elt))
454 (eq (car (cdr-safe elt)) 'lambda))
dc9ed794 455 (and (symbolp (cdr-safe elt)) (fboundp (cdr-safe elt))))
42d140b4
KH
456 (setq km (cdr elt))
457 (and (stringp (car elt)) (setq str (car elt))))
0595722b 458
42d140b4
KH
459 ((if (listp (cdr-safe (cdr-safe elt)))
460 (or (keymapp (cdr-safe (cdr-safe elt)))
461 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
dc9ed794
SM
462 (and (symbolp (cdr-safe (cdr-safe elt)))
463 (fboundp (cdr-safe (cdr-safe elt)))))
e9bed1ef 464 (setq km (cddr elt))
dc9ed794 465 (and (stringp (car elt)) (setq str (car elt))))
0595722b 466
42d140b4 467 ((eq (car-safe elt) 'menu-item)
0595722b 468 ;; (menu-item TITLE COMMAND KEY ...)
dd81ca0d 469 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
0595722b
GM
470 (when (consp (car-safe plist))
471 (setq plist (cdr-safe plist)))
42d140b4 472 (setq km (nth 2 elt))
c19cc275 473 (setq str (eval (nth 1 elt)))
dd81ca0d
RS
474 (setq filter (plist-get plist :filter))
475 (if filter
476 (setq km (funcall filter km)))
97895167
PJ
477 (setq visible (plist-get plist :visible))
478 (if visible
479 (setq km (and (eval visible) km)))
7ae862e2
NR
480 (setq enable (plist-get plist :enable))
481 (if enable
dc9ed794 482 (setq km (if (eval enable) km 'ignore))))
0595722b 483
42d140b4
KH
484 ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
485 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
486 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
dc9ed794
SM
487 (and (symbolp (cdr-safe (cdr-safe (cdr-safe elt))))
488 (fboundp (cdr-safe (cdr-safe (cdr-safe elt))))))
42d140b4 489 ; New style of easy-menu
e9bed1ef 490 (setq km (cdr (cddr elt)))
dc9ed794 491 (and (stringp (car elt)) (setq str (car elt))))
0595722b 492
42d140b4
KH
493 ((stringp event) ; x-popup or x-popup element
494 (if (or in-x-menu (stringp (car-safe elt)))
495 (setq str event event nil km elt)
dc9ed794 496 (setq str event event nil km (cons 'keymap elt)))))
362b9d48 497 (unless (or (eq km 'ignore) (null str))
dc9ed794
SM
498 (let ((binding (where-is-internal km nil t)))
499 (when binding
500 (setq binding (key-description binding))
501 ;; Try to align the keybindings.
502 (let ((colwidth (min 30 (- (/ (window-width) 2) 10))))
503 (setq str
504 (concat str
505 (make-string (max 2 (- colwidth
506 (string-width str)
507 (string-width binding)))
508 ?\s)
509 binding)))))))
20062d6b 510 (and km (stringp km) (setq str km))
2a9f2437
RS
511 ;; Verify that the command is enabled;
512 ;; if not, don't mention it.
513 (when (and km (symbolp km) (get km 'menu-enable))
04a5d30f 514 (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
20062d6b
RS
515 (and km str
516 (or (assoc str tmm-km-list)
c19cc275 517 (push (cons str (cons event km)) tmm-km-list))))))
20062d6b 518
20062d6b 519(defun tmm-get-keybind (keyseq)
fc225f66 520 "Return the current binding of KEYSEQ, merging prefix definitions.
91a5e367 521If KEYSEQ is a prefix key that has local and global bindings,
fc225f66
RS
522we merge them into a single keymap which shows the proper order of the menu.
523However, for the menu bar itself, the value does not take account
524of `menu-bar-final-items'."
93253b0e 525 (let (allbind bind minorbind localbind globalbind)
fc225f66
RS
526 (setq bind (key-binding keyseq))
527 ;; If KEYSEQ is a prefix key, then BIND is either nil
528 ;; or a symbol defined as a keymap (which satisfies keymapp).
529 (if (keymapp bind)
530 (setq bind nil))
531 ;; If we have a non-keymap definition, return that.
532 (or bind
533 (progn
534 ;; Otherwise, it is a prefix, so make a list of the subcommands.
535 ;; Make a list of all the bindings in all the keymaps.
93253b0e
NR
536 (setq minorbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
537 (setq localbind (local-key-binding keyseq))
e47b7019 538 (setq globalbind (copy-sequence (cdr (global-key-binding keyseq))))
93253b0e
NR
539
540 ;; If items have been redefined/undefined locally, remove them from
541 ;; the global list.
542 (dolist (minor minorbind)
543 (dolist (item (cdr minor))
0bb48bff 544 (setq globalbind (assq-delete-all (car-safe item) globalbind))))
93253b0e 545 (dolist (item (cdr localbind))
0bb48bff 546 (setq globalbind (assq-delete-all (car-safe item) globalbind)))
93253b0e
NR
547
548 (setq globalbind (cons 'keymap globalbind))
549 (setq allbind (cons globalbind (cons localbind minorbind)))
550
fc225f66 551 ;; Merge all the elements of ALLBIND into one keymap.
e07436e1
DL
552 (mapc (lambda (in)
553 (if (and (symbolp in) (keymapp in))
554 (setq in (symbol-function in)))
555 (and in (keymapp in)
556 (if (keymapp bind)
557 (setq bind (nconc bind (copy-sequence (cdr in))))
558 (setq bind (copy-sequence in)))))
fc225f66
RS
559 allbind)
560 ;; Return that keymap.
561 bind))))
20062d6b 562
c03aab72 563;; Huh? What's that about? --Stef
20062d6b
RS
564(add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
565
20062d6b
RS
566(provide 'tmm)
567
20062d6b 568;;; tmm.el ends here