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