1 ;;; tmm.el --- text mode access to menu-bar
3 ;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
8 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
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 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This package provides text mode access to the menu bar.
34 "Text mode access to menu-bar."
38 ;;; The following will be localized, added only to pacify the compiler.
39 (defvar tmm-short-cuts
)
40 (defvar tmm-old-mb-map nil
)
41 (defvar tmm-old-comp-map
)
42 (defvar tmm-c-prompt nil
)
44 (defvar tmm-next-shortcut-digit
)
45 (defvar tmm-table-undef
)
47 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
48 ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
51 (defun tmm-menubar (&optional x-position
)
52 "Text-mode emulation of looking and choosing from a menubar.
53 See the documentation for `tmm-prompt'.
54 X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
55 we make that menu bar item (the one at that position) the default choice."
57 (run-hooks 'menu-bar-update-hook
)
58 ;; Obey menu-bar-final-items; put those items last.
59 (let ((menu-bar (tmm-get-keybind [menu-bar
]))
61 (let ((list menu-bar-final-items
))
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
)
69 (setq list
(cdr list
))))
71 (let ((tail menu-bar
) (column 0)
72 this-one name visible
)
73 (while (and tail
(<= column x-position
))
74 (setq this-one
(car tail
))
75 (if (and (consp this-one
)
76 (consp (cdr this-one
))
77 (setq name
;simple menu
78 (cond ((stringp (nth 1 this-one
))
81 ((stringp (nth 2 this-one
))
82 (setq visible
(plist-get
83 (nthcdr 4 this-one
) :visible
))
84 (unless (and visible
(not (eval visible
)))
86 (setq column
(+ column
(length name
) 1)))
87 (setq tail
(cdr tail
)))
88 (setq menu-bar-item
(car this-one
))))
89 (tmm-prompt menu-bar nil menu-bar-item
)))
92 (defun tmm-menubar-mouse (event)
93 "Text-mode emulation of looking and choosing from a menubar.
94 This command is used when you click the mouse in the menubar
95 on a console which has no window system but does have a mouse.
96 See the documentation for `tmm-prompt'."
98 (tmm-menubar (car (posn-x-y (event-start event
)))))
100 (defcustom tmm-mid-prompt
"==>"
101 "String to insert between shortcut and menu item.
102 If nil, there will be no shortcuts. It should not consist only of spaces,
103 or else the correct item might not be found in the `*Completions*' buffer."
107 (defvar tmm-mb-map nil
108 "A place to store minibuffer map.")
110 (defcustom tmm-completion-prompt
111 "Press PageUp key to reach this buffer from the minibuffer.
112 Alternatively, you can use Up/Down keys (or your History keys) to change
113 the item in the minibuffer, and press RET when you are done, or press the
114 marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
116 "Help text to insert on the top of the completion buffer.
117 To save space, you can set this to nil,
118 in which case the standard introduction text is deleted too."
119 :type
'(choice string
(const nil
))
122 (defcustom tmm-shortcut-style
'(downcase upcase
)
123 "What letters to use as menu shortcuts.
124 Must be either one of the symbols `downcase' or `upcase',
125 or else a list of the two in the order you prefer."
126 :type
'(choice (const downcase
)
128 (repeat (choice (const downcase
) (const upcase
))))
131 (defcustom tmm-shortcut-words
2
132 "How many successive words to try for shortcuts, nil means all.
133 If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
134 specify nil for this variable."
135 :type
'(choice integer
(const nil
))
138 (defface tmm-inactive
139 '((t :inherit shadow
))
140 "Face used for inactive menu items."
144 (defun tmm-prompt (menu &optional in-popup default-item
)
145 "Text-mode emulation of calling the bindings in keymap.
146 Creates a text-mode menu of possible choices. You can access the elements
147 in the menu in two ways:
148 *) via history mechanism from minibuffer;
149 *) Or via completion-buffer that is automatically shown.
150 The last alternative is currently a hack, you cannot use mouse reliably.
152 MENU is like the MENU argument to `x-popup-menu': either a
153 keymap or an alist of alists.
154 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
155 Its 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
164 (not-menu (not (keymapp menu
))))
165 (run-hooks 'activate-menubar-hook
)
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.
174 ((listp elt
) (tmm-get-keymap elt not-menu
))
176 (dotimes (i (length elt
))
177 (tmm-get-keymap (cons i
(aref elt i
)) not-menu
))))))
179 ;; Choose an element of tmm-km-list; put it in choice.
180 (if (and not-menu
(= 1 (length tmm-km-list
)))
181 ;; If this is the top-level of an x-popup-menu menu,
182 ;; and there is just one pane, choose that one silently.
183 ;; This way we only ask the user one question,
184 ;; for which element of that pane.
185 (setq choice
(cdr (car tmm-km-list
)))
187 (error "Empty menu reached"))
189 (let ((index-of-default 0))
191 (setq tmm-km-list
(tmm-add-shortcuts tmm-km-list
))
193 ;; Find the default item's index within the menu bar.
194 ;; We use this to decide the initial minibuffer contents
195 ;; and initial history position.
197 (let ((tail menu
) visible
)
199 (not (eq (car-safe (car tail
)) default-item
)))
200 ;; Be careful to count only the elements of MENU
201 ;; that actually constitute menu bar items.
202 (if (and (consp (car tail
))
203 (or (stringp (car-safe (cdr (car tail
))))
205 (eq (car-safe (cdr (car tail
))) 'menu-item
)
209 (nthcdr 4 (car tail
)) :visible
))
210 (or (not visible
) (eval visible
))))))
211 (setq index-of-default
(1+ index-of-default
)))
212 (setq tail
(cdr tail
)))))
213 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt
))))
218 (if (string-match prompt
(car elt
))
221 (setq history-len
(length history
))
222 (setq history
(append history history history history
))
223 (setq tmm-c-prompt
(nth (- history-len
1 index-of-default
) history
))
224 (add-hook 'minibuffer-setup-hook
'tmm-add-prompt
)
226 (setq out
(car (nth index-of-default tmm-km-list
)))
232 " (up/down to change, PgUp to menu): ")
233 tmm-km-list nil t nil
235 (- (* 2 history-len
) index-of-default
))))
236 (remove-hook 'minibuffer-setup-hook
'tmm-add-prompt
)
237 (if (get-buffer "*Completions*")
238 (with-current-buffer "*Completions*"
239 (use-local-map tmm-old-comp-map
)
240 (bury-buffer (current-buffer)))))))))
241 (setq choice
(cdr (assoc out tmm-km-list
)))
243 (> (length out
) (length tmm-c-prompt
))
244 (string= (substring out
0 (length tmm-c-prompt
)) tmm-c-prompt
)
245 (setq out
(substring out
(length tmm-c-prompt
))
246 choice
(cdr (assoc out tmm-km-list
))))
247 (and (null choice
) out
248 (setq out
(try-completion out tmm-km-list
)
249 choice
(cdr (assoc out tmm-km-list
)))))
250 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
251 (setq chosen-string
(car choice
))
252 (setq choice
(cdr choice
))
254 ;; We just did the inner level of a -popup menu.
256 ;; We just did the outer level. Do the inner level now.
257 (not-menu (tmm-prompt choice t
))
258 ;; We just handled a menu keymap and found another keymap.
261 (setq choice
(indirect-function choice
)))
266 (x-popup-menu nil choice
) ; Get the shortcuts
269 ;; We just handled a menu keymap and found a command.
273 (setq last-command-event chosen-string
)
274 (call-interactively choice
))
277 (defun tmm-add-shortcuts (list)
278 "Adds shortcuts to cars of elements of the list.
279 Takes a list of lists with a string as car, returns list with
280 shortcuts added to these cars.
281 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
282 (let ((tmm-next-shortcut-digit ?
0))
283 (mapcar 'tmm-add-one-shortcut
(reverse list
))))
285 (defsubst tmm-add-one-shortcut
(elt)
286 ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
288 ((eq (cddr elt
) 'ignore
)
289 (cons (concat " " (make-string (length tmm-mid-prompt
) ?\-
)
293 (let* ((str (car elt
))
294 (paren (string-match "(" str
))
295 (pos 0) (word 0) char
)
296 (catch 'done
; ??? is this slow?
297 (while (and (or (not tmm-shortcut-words
) ; no limit on words
298 (< word tmm-shortcut-words
)) ; try n words
299 (setq pos
(string-match "\\w+" str pos
)) ; get next word
300 (not (and paren
(> pos paren
)))) ; don't go past "(binding.."
302 (/= (aref str
(1- pos
)) ?.
)) ; avoid file extensions
303 (let ((shortcut-style
304 (if (listp tmm-shortcut-style
) ; convert to list
306 (list tmm-shortcut-style
))))
307 (while shortcut-style
; try upcase and downcase variants
308 (setq char
(funcall (car shortcut-style
) (aref str pos
)))
309 (if (not (memq char tmm-short-cuts
)) (throw 'done char
))
310 (setq shortcut-style
(cdr shortcut-style
)))))
311 (setq word
(1+ word
))
312 (setq pos
(match-end 0)))
313 (while (<= tmm-next-shortcut-digit ?
9) ; no letter shortcut, pick a digit
314 (setq char tmm-next-shortcut-digit
)
315 (setq tmm-next-shortcut-digit
(1+ tmm-next-shortcut-digit
))
316 (if (not (memq char tmm-short-cuts
)) (throw 'done char
)))
318 (if char
(setq tmm-short-cuts
(cons char tmm-short-cuts
)))
319 (cons (concat (if char
(concat (char-to-string char
) tmm-mid-prompt
)
320 ;; keep them lined up in columns
321 (make-string (1+ (length tmm-mid-prompt
)) ?\s
))
325 ;; This returns the old map.
326 (defun tmm-define-keys (minibuffer)
327 (let ((map (make-sparse-keymap)))
328 (suppress-keymap map t
)
331 (if (listp tmm-shortcut-style
)
332 (define-key map
(char-to-string c
) 'tmm-shortcut
)
333 ;; only one kind of letters are shortcuts, so map both upcase and
334 ;; downcase input to the same
335 (define-key map
(char-to-string (downcase c
)) 'tmm-shortcut
)
336 (define-key map
(char-to-string (upcase c
)) 'tmm-shortcut
)))
340 (define-key map
[pageup] 'tmm-goto-completions)
341 (define-key map [prior] 'tmm-goto-completions)
342 (define-key map "\ev" 'tmm-goto-completions)
343 (define-key map "\C-n" 'next-history-element)
344 (define-key map "\C-p" 'previous-history-element)))
345 (prog1 (current-local-map)
346 (use-local-map (append map (current-local-map))))))
348 (defun tmm-completion-delete-prompt ()
349 (set-buffer standard-output)
350 (goto-char (point-min))
351 (delete-region (point) (search-forward "Possible completions are:\n")))
353 (defun tmm-remove-inactive-mouse-face ()
354 "Remove the mouse-face property from inactive menu items."
355 (let ((inhibit-read-only t)
357 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
360 (goto-char (point-min))
362 (setq next (next-single-char-property-change (point) 'mouse-face))
363 (when (looking-at inactive-string)
364 (remove-text-properties (point) next '(mouse-face))
365 (add-text-properties (point) next '(face tmm-inactive)))
367 (set-buffer-modified-p nil)))
369 (defun tmm-add-prompt ()
370 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
371 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
373 (error "No active menu entries"))
374 (let ((win (selected-window)))
375 (setq tmm-old-mb-map (tmm-define-keys t))
376 ;; Get window and hide it for electric mode to get correct size
377 (save-window-excursion
379 (mapcar 'car minibuffer-completion-table)))
380 (or tmm-completion-prompt
381 (add-hook 'completion-setup-hook
382 'tmm-completion-delete-prompt 'append))
384 (with-output-to-temp-buffer "*Completions*"
385 (display-completion-list completions))
386 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt)))
387 (set-buffer "*Completions*")
388 (tmm-remove-inactive-mouse-face)
389 (when tmm-completion-prompt
390 (let ((buffer-read-only nil))
391 (goto-char (point-min))
392 (insert tmm-completion-prompt))))
393 (save-selected-window
394 (other-window 1) ; Electric-pop-up-window does
395 ; not work in minibuffer
396 (Electric-pop-up-window "*Completions*")
397 (with-current-buffer "*Completions*"
398 (setq tmm-old-comp-map (tmm-define-keys nil))))
399 (insert tmm-c-prompt)))
401 (defun tmm-delete-map ()
402 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
404 (use-local-map tmm-old-mb-map)))
406 (defun tmm-shortcut ()
407 "Choose the shortcut that the user typed."
409 (let ((c last-command-event) s)
410 (if (symbolp tmm-shortcut-style)
411 (setq c (funcall tmm-shortcut-style c)))
412 (if (memq c tmm-short-cuts)
413 (if (equal (buffer-name) "*Completions*")
415 (goto-char (point-min))
417 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
420 (delete-region (minibuffer-prompt-end) (point-max))
423 (substring (car elt) 0
424 (min (1+ (length tmm-mid-prompt))
426 (concat (char-to-string c) tmm-mid-prompt))
430 (exit-minibuffer)))))
432 (defun tmm-goto-completions ()
434 (let ((prompt-end (minibuffer-prompt-end)))
435 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
436 (delete-region prompt-end (point-max)))
437 (switch-to-buffer-other-window "*Completions*")
438 (search-forward tmm-c-prompt)
439 (search-backward tmm-c-prompt))
441 (defun tmm-get-keymap (elt &optional in-x-menu)
442 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
443 The values are deduced from the argument ELT, that should be an
444 element of keymap, an `x-popup-menu' argument, or an element of
445 `x-popup-menu' argument (when IN-X-MENU is not-nil).
446 This function adds the element only if it is not already present.
447 It uses the free variable `tmm-table-undef' to keep undefined keys."
448 (let (km str cache plist filter visible enable (event (car elt)))
450 (if (eq elt 'undefined)
451 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
452 (unless (assoc event tmm-table-undef)
453 (cond ((if (listp elt)
454 (or (keymapp elt) (eq (car elt) 'lambda))
455 (and (symbolp elt) (fboundp elt)))
458 ((if (listp (cdr-safe elt))
459 (or (keymapp (cdr-safe elt))
460 (eq (car (cdr-safe elt)) 'lambda))
461 (and (symbolp (cdr-safe elt)) (fboundp (cdr-safe elt))))
463 (and (stringp (car elt)) (setq str (car elt))))
465 ((if (listp (cdr-safe (cdr-safe elt)))
466 (or (keymapp (cdr-safe (cdr-safe elt)))
467 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
468 (and (symbolp (cdr-safe (cdr-safe elt)))
469 (fboundp (cdr-safe (cdr-safe elt)))))
471 (and (stringp (car elt)) (setq str (car elt)))
473 (stringp (cdr-safe (cadr elt))) ; keyseq cache
474 (setq cache (cdr (cadr elt)))
475 cache (setq str (concat str cache))))
477 ((eq (car-safe elt) 'menu-item)
478 ;; (menu-item TITLE COMMAND KEY ...)
479 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
480 (when (consp (car-safe plist))
481 (setq plist (cdr-safe plist)))
482 (setq km (nth 2 elt))
483 (setq str (eval (nth 1 elt)))
484 (setq filter (plist-get plist :filter))
486 (setq km (funcall filter km)))
487 (setq visible (plist-get plist :visible))
489 (setq km (and (eval visible) km)))
490 (setq enable (plist-get plist :enable))
492 (setq km (if (eval enable) km 'ignore)))
495 (stringp (cdr (nth 3 elt))) ; keyseq cache
496 (setq cache (cdr (nth 3 elt)))
498 (setq str (concat str cache))))
500 ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
501 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
502 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
503 (and (symbolp (cdr-safe (cdr-safe (cdr-safe elt))))
504 (fboundp (cdr-safe (cdr-safe (cdr-safe elt))))))
505 ; New style of easy-menu
506 (setq km (cdr (cddr elt)))
507 (and (stringp (car elt)) (setq str (car elt)))
509 (stringp (cdr-safe (car (cddr elt)))) ; keyseq cache
510 (setq cache (cdr (car (cdr (cdr elt)))))
511 cache (setq str (concat str cache))))
513 ((stringp event) ; x-popup or x-popup element
514 (if (or in-x-menu (stringp (car-safe elt)))
515 (setq str event event nil km elt)
516 (setq str event event nil km (cons 'keymap elt))))))
517 (and km (stringp km) (setq str km))
518 ;; Verify that the command is enabled;
519 ;; if not, don't mention it.
520 (when (and km (symbolp km) (get km 'menu-enable))
521 (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
523 (or (assoc str tmm-km-list)
524 (push (cons str (cons event km)) tmm-km-list))))))
526 (defun tmm-get-keybind (keyseq)
527 "Return the current binding of KEYSEQ, merging prefix definitions.
528 If KEYSEQ is a prefix key that has local and global bindings,
529 we merge them into a single keymap which shows the proper order of the menu.
530 However, for the menu bar itself, the value does not take account
531 of `menu-bar-final-items'."
532 (let (allbind bind minorbind localbind globalbind)
533 (setq bind (key-binding keyseq))
534 ;; If KEYSEQ is a prefix key, then BIND is either nil
535 ;; or a symbol defined as a keymap (which satisfies keymapp).
538 ;; If we have a non-keymap definition, return that.
541 ;; Otherwise, it is a prefix, so make a list of the subcommands.
542 ;; Make a list of all the bindings in all the keymaps.
543 (setq minorbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
544 (setq localbind (local-key-binding keyseq))
545 (setq globalbind (copy-sequence (cdr (global-key-binding keyseq))))
547 ;; If items have been redefined/undefined locally, remove them from
549 (dolist (minor minorbind)
550 (dolist (item (cdr minor))
551 (setq globalbind (assq-delete-all (car-safe item) globalbind))))
552 (dolist (item (cdr localbind))
553 (setq globalbind (assq-delete-all (car-safe item) globalbind)))
555 (setq globalbind (cons 'keymap globalbind))
556 (setq allbind (cons globalbind (cons localbind minorbind)))
558 ;; Merge all the elements of ALLBIND into one keymap.
560 (if (and (symbolp in) (keymapp in))
561 (setq in (symbol-function in)))
564 (setq bind (nconc bind (copy-sequence (cdr in))))
565 (setq bind (copy-sequence in)))))
567 ;; Return that keymap.
570 ;; Huh? What's that about? --Stef
571 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
575 ;; arch-tag: e7ddbdb6-4b95-4da3-afbe-ad6063d112f4