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