1 ;;; tmm.el --- text mode access to menu-bar -*- lexical-binding: t -*-
3 ;; Copyright (C) 1994-1996, 2000-2013 Free Software Foundation, Inc.
5 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
7 ;; Keywords: convenience
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This package provides text mode access to the menu bar.
33 "Text mode access to menu-bar."
37 ;;; The following will be localized, added only to pacify the compiler.
38 (defvar tmm-short-cuts
)
39 (defvar tmm-old-mb-map nil
)
40 (defvar tmm-c-prompt nil
)
42 (defvar tmm-next-shortcut-digit
)
43 (defvar tmm-table-undef
)
45 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
46 ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
49 (defun tmm-menubar (&optional x-position
)
50 "Text-mode emulation of looking and choosing from a menubar.
51 See the documentation for `tmm-prompt'.
52 X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
53 we make that menu bar item (the one at that position) the default choice.
55 Note that \\[menu-bar-open] by default drops down TTY menus; if you want it
56 to invoke `tmm-menubar' instead, customize the variable
57 \`tty-menu-open-use-tmm' to a non-nil value."
59 (run-hooks 'menu-bar-update-hook
)
60 ;; Obey menu-bar-final-items; put those items last.
66 (push (cons key binding
)
67 ;; If KEY is the name of an item that we want to put last,
68 ;; move it to the end.
69 (if (memq key menu-bar-final-items
)
72 (tmm-get-keybind [menu-bar
]))
73 (setq menu-bar
`(keymap ,@(nreverse menu-bar
) ,@(nreverse menu-end
)))
79 (when (> column x-position
)
80 (setq menu-bar-item key
)
83 ((or `(,(and (pred stringp
) name
) .
,_
) ;Simple menu item.
84 `(menu-item ,name
,_cmd
;Extended menu item.
87 (plist-get props
:visible
)))
90 (setq column
(+ column
(length name
) 1)))))
92 (tmm-prompt menu-bar nil menu-bar-item
)))
95 (defun tmm-menubar-mouse (event)
96 "Text-mode emulation of looking and choosing from a menubar.
97 This command is used when you click the mouse in the menubar
98 on a console which has no window system but does have a mouse.
99 See the documentation for `tmm-prompt'."
101 (tmm-menubar (car (posn-x-y (event-start event
)))))
103 (defcustom tmm-mid-prompt
"==>"
104 "String to insert between shortcut and menu item.
105 If nil, there will be no shortcuts. It should not consist only of spaces,
106 or else the correct item might not be found in the `*Completions*' buffer."
110 (defvar tmm-mb-map nil
111 "A place to store minibuffer map.")
113 (defcustom tmm-completion-prompt
114 "Press PageUp key to reach this buffer from the minibuffer.
115 Alternatively, you can use Up/Down keys (or your History keys) to change
116 the item in the minibuffer, and press RET when you are done, or press the
117 marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
119 "Help text to insert on the top of the completion buffer.
120 To save space, you can set this to nil,
121 in which case the standard introduction text is deleted too."
122 :type
'(choice string
(const nil
))
125 (defcustom tmm-shortcut-style
'(downcase upcase
)
126 "What letters to use as menu shortcuts.
127 Must be either one of the symbols `downcase' or `upcase',
128 or else a list of the two in the order you prefer."
129 :type
'(choice (const downcase
)
131 (repeat (choice (const downcase
) (const upcase
))))
134 (defcustom tmm-shortcut-words
2
135 "How many successive words to try for shortcuts, nil means all.
136 If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
137 specify nil for this variable."
138 :type
'(choice integer
(const nil
))
141 (defface tmm-inactive
142 '((t :inherit shadow
))
143 "Face used for inactive menu items."
146 (defun tmm--completion-table (items)
147 (lambda (string pred action
)
148 (if (eq action
'metadata
)
149 '(metadata (display-sort-function . identity
))
150 (complete-with-action action items string pred
))))
152 (defvar tmm--history nil
)
155 (defun tmm-prompt (menu &optional in-popup default-item
)
156 "Text-mode emulation of calling the bindings in keymap.
157 Creates a text-mode menu of possible choices. You can access the elements
158 in the menu in two ways:
159 *) via history mechanism from minibuffer;
160 *) Or via completion-buffer that is automatically shown.
161 The last alternative is currently a hack, you cannot use mouse reliably.
163 MENU is like the MENU argument to `x-popup-menu': either a
164 keymap or an alist of alists.
165 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
166 Its value should be an event that has a binding in MENU."
167 ;; If the optional argument IN-POPUP is t,
168 ;; then MENU is an alist of elements of the form (STRING . VALUE).
169 ;; That is used for recursive calls only.
170 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
171 ; so it doesn't have a name.
172 tmm-km-list out history-len tmm-table-undef tmm-c-prompt
173 tmm-old-mb-map tmm-short-cuts
175 (not-menu (not (keymapp menu
))))
176 (run-hooks 'activate-menubar-hook
)
177 ;; Compute tmm-km-list from MENU.
178 ;; tmm-km-list is an alist of (STRING . MEANING).
179 ;; It has no other elements.
180 ;; The order of elements in tmm-km-list is the order of the menu bar.
182 (map-keymap (lambda (k v
) (tmm-get-keymap (cons k v
))) menu
)
185 ((stringp elt
) (setq gl-str elt
))
186 ((listp elt
) (tmm-get-keymap elt not-menu
))
188 (dotimes (i (length elt
))
189 (tmm-get-keymap (cons i
(aref elt i
)) not-menu
))))))
190 (setq tmm-km-list
(nreverse tmm-km-list
))
191 ;; Choose an element of tmm-km-list; put it in choice.
192 (if (and not-menu
(= 1 (length tmm-km-list
)))
193 ;; If this is the top-level of an x-popup-menu menu,
194 ;; and there is just one pane, choose that one silently.
195 ;; This way we only ask the user one question,
196 ;; for which element of that pane.
197 (setq choice
(cdr (car tmm-km-list
)))
199 (error "Empty menu reached"))
201 (let ((index-of-default 0))
203 (setq tmm-km-list
(tmm-add-shortcuts tmm-km-list
))
205 ;; Find the default item's index within the menu bar.
206 ;; We use this to decide the initial minibuffer contents
207 ;; and initial history position.
209 (let ((tail menu
) visible
)
211 (not (eq (car-safe (car tail
)) default-item
)))
212 ;; Be careful to count only the elements of MENU
213 ;; that actually constitute menu bar items.
214 (if (and (consp (car tail
))
215 (or (stringp (car-safe (cdr (car tail
))))
217 (eq (car-safe (cdr (car tail
))) 'menu-item
)
221 (nthcdr 4 (car tail
)) :visible
))
222 (or (not visible
) (eval visible
))))))
223 (setq index-of-default
(1+ index-of-default
)))
224 (setq tail
(cdr tail
)))))
225 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt
))))
230 (if (string-match prompt
(car elt
))
233 (setq history-len
(length tmm--history
))
234 (setq tmm--history
(append tmm--history tmm--history
235 tmm--history tmm--history
))
236 (setq tmm-c-prompt
(nth (- history-len
1 index-of-default
)
240 (car (nth index-of-default tmm-km-list
))
241 (minibuffer-with-setup-hook #'tmm-add-prompt
244 " (up/down to change, PgUp to menu): ")
245 (tmm--completion-table tmm-km-list
) nil t nil
247 (- (* 2 history-len
) index-of-default
))))))))
248 (setq choice
(cdr (assoc out tmm-km-list
)))
250 (string-prefix-p tmm-c-prompt out
)
251 (setq out
(substring out
(length tmm-c-prompt
))
252 choice
(cdr (assoc out tmm-km-list
))))
253 (and (null choice
) out
254 (setq out
(try-completion out tmm-km-list
)
255 choice
(cdr (assoc out tmm-km-list
)))))
256 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
257 (setq chosen-string
(car choice
))
258 (setq choice
(cdr choice
))
260 ;; We just did the inner level of a -popup menu.
262 ;; We just did the outer level. Do the inner level now.
263 (not-menu (tmm-prompt choice t
))
264 ;; We just handled a menu keymap and found another keymap.
267 (setq choice
(indirect-function choice
)))
272 ;; We just handled a menu keymap and found a command.
276 (setq last-command-event chosen-string
)
277 (call-interactively choice
))
280 (defun tmm-add-shortcuts (list)
281 "Add shortcuts to cars of elements of the list.
282 Takes a list of lists with a string as car, returns list with
283 shortcuts added to these cars.
284 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
285 (let ((tmm-next-shortcut-digit ?
0))
286 (mapcar 'tmm-add-one-shortcut
(reverse list
))))
288 (defsubst tmm-add-one-shortcut
(elt)
289 ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
291 ((eq (cddr elt
) 'ignore
)
292 (cons (concat " " (make-string (length tmm-mid-prompt
) ?\-
)
296 (let* ((str (car elt
))
297 (paren (string-match "(" str
))
298 (pos 0) (word 0) char
)
299 (catch 'done
; ??? is this slow?
300 (while (and (or (not tmm-shortcut-words
) ; no limit on words
301 (< word tmm-shortcut-words
)) ; try n words
302 (setq pos
(string-match "\\w+" str pos
)) ; get next word
303 (not (and paren
(> pos paren
)))) ; don't go past "(binding.."
305 (/= (aref str
(1- pos
)) ?.
)) ; avoid file extensions
306 (let ((shortcut-style
307 (if (listp tmm-shortcut-style
) ; convert to list
309 (list tmm-shortcut-style
))))
310 (while shortcut-style
; try upcase and downcase variants
311 (setq char
(funcall (car shortcut-style
) (aref str pos
)))
312 (if (not (memq char tmm-short-cuts
)) (throw 'done char
))
313 (setq shortcut-style
(cdr shortcut-style
)))))
314 (setq word
(1+ word
))
315 (setq pos
(match-end 0)))
316 (while (<= tmm-next-shortcut-digit ?
9) ; no letter shortcut, pick a digit
317 (setq char tmm-next-shortcut-digit
)
318 (setq tmm-next-shortcut-digit
(1+ tmm-next-shortcut-digit
))
319 (if (not (memq char tmm-short-cuts
)) (throw 'done char
)))
321 (if char
(setq tmm-short-cuts
(cons char tmm-short-cuts
)))
322 (cons (concat (if char
(concat (char-to-string char
) tmm-mid-prompt
)
323 ;; keep them lined up in columns
324 (make-string (1+ (length tmm-mid-prompt
)) ?\s
))
328 ;; This returns the old map.
329 (defun tmm-define-keys (minibuffer)
330 (let ((map (make-sparse-keymap)))
331 (suppress-keymap map t
)
332 (dolist (c tmm-short-cuts
)
333 (if (listp tmm-shortcut-style
)
334 (define-key map
(char-to-string c
) 'tmm-shortcut
)
335 ;; only one kind of letters are shortcuts, so map both upcase and
336 ;; downcase input to the same
337 (define-key map
(char-to-string (downcase c
)) 'tmm-shortcut
)
338 (define-key map
(char-to-string (upcase c
)) 'tmm-shortcut
)))
341 (define-key map
[pageup] 'tmm-goto-completions)
342 (define-key map [prior] 'tmm-goto-completions)
343 (define-key map "\ev" 'tmm-goto-completions)
344 (define-key map "\C-n" 'next-history-element)
345 (define-key map "\C-p" 'previous-history-element)))
346 (prog1 (current-local-map)
347 (use-local-map (append map (current-local-map))))))
349 (defun tmm-completion-delete-prompt ()
350 (with-current-buffer standard-output
351 (goto-char (point-min))
352 (delete-region (point) (search-forward "Possible completions are:\n"))))
354 (defun tmm-remove-inactive-mouse-face ()
355 "Remove the mouse-face property from inactive menu items."
356 (let ((inhibit-read-only t)
358 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
361 (goto-char (point-min))
363 (setq next (next-single-char-property-change (point) 'mouse-face))
364 (when (looking-at inactive-string)
365 (remove-text-properties (point) next '(mouse-face))
366 (add-text-properties (point) next '(face tmm-inactive)))
368 (set-buffer-modified-p nil)))
370 (defun tmm-add-prompt ()
372 (error "No active menu entries"))
373 (setq tmm-old-mb-map (tmm-define-keys t))
374 ;; Get window and hide it for electric mode to get correct size
375 (or tmm-completion-prompt
376 (add-hook 'completion-setup-hook
377 'tmm-completion-delete-prompt 'append))
379 (minibuffer-completion-help)
380 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
381 (with-current-buffer "*Completions*"
382 (tmm-remove-inactive-mouse-face)
383 (when tmm-completion-prompt
384 (let ((inhibit-read-only t))
385 (goto-char (point-min))
386 (insert tmm-completion-prompt))))
387 (insert tmm-c-prompt))
389 (defun tmm-shortcut ()
390 "Choose the shortcut that the user typed."
392 (let ((c last-command-event) s)
393 (if (symbolp tmm-shortcut-style)
394 (setq c (funcall tmm-shortcut-style c)))
395 (if (memq c tmm-short-cuts)
396 (if (equal (buffer-name) "*Completions*")
398 (goto-char (point-min))
400 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
403 (delete-region (minibuffer-prompt-end) (point-max))
404 (dolist (elt tmm-km-list)
406 (substring (car elt) 0
407 (min (1+ (length tmm-mid-prompt))
409 (concat (char-to-string c) tmm-mid-prompt))
412 (exit-minibuffer)))))
414 (defun tmm-goto-completions ()
415 "Jump to the completions buffer."
417 (let ((prompt-end (minibuffer-prompt-end)))
418 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
420 (delete-region prompt-end (point-max)))
421 (switch-to-buffer-other-window "*Completions*")
422 (search-forward tmm-c-prompt)
423 (search-backward tmm-c-prompt))
425 (defun tmm-get-keymap (elt &optional in-x-menu)
426 "Prepend (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
427 The values are deduced from the argument ELT, that should be an
428 element of keymap, an `x-popup-menu' argument, or an element of
429 `x-popup-menu' argument (when IN-X-MENU is not-nil).
430 This function adds the element only if it is not already present.
431 It uses the free variable `tmm-table-undef' to keep undefined keys."
432 (let (km str plist filter visible enable (event (car elt)))
434 (if (eq elt 'undefined)
435 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
436 (unless (assoc event tmm-table-undef)
437 (cond ((if (listp elt)
438 (or (keymapp elt) (eq (car elt) 'lambda))
439 (and (symbolp elt) (fboundp elt)))
442 ((if (listp (cdr-safe elt))
443 (or (keymapp (cdr-safe elt))
444 (eq (car (cdr-safe elt)) 'lambda))
445 (and (symbolp (cdr-safe elt)) (fboundp (cdr-safe elt))))
447 (and (stringp (car elt)) (setq str (car elt))))
449 ((if (listp (cdr-safe (cdr-safe elt)))
450 (or (keymapp (cdr-safe (cdr-safe elt)))
451 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
452 (and (symbolp (cdr-safe (cdr-safe elt)))
453 (fboundp (cdr-safe (cdr-safe elt)))))
455 (and (stringp (car elt)) (setq str (car elt))))
457 ((eq (car-safe elt) 'menu-item)
458 ;; (menu-item TITLE COMMAND KEY ...)
459 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
460 (when (consp (car-safe plist))
461 (setq plist (cdr-safe plist)))
462 (setq km (nth 2 elt))
463 (setq str (eval (nth 1 elt)))
464 (setq filter (plist-get plist :filter))
466 (setq km (funcall filter km)))
467 (setq visible (plist-get plist :visible))
469 (setq km (and (eval visible) km)))
470 (setq enable (plist-get plist :enable))
472 (setq km (if (eval enable) km 'ignore))))
474 ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
475 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
476 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
477 (and (symbolp (cdr-safe (cdr-safe (cdr-safe elt))))
478 (fboundp (cdr-safe (cdr-safe (cdr-safe elt))))))
479 ; New style of easy-menu
480 (setq km (cdr (cddr elt)))
481 (and (stringp (car elt)) (setq str (car elt))))
483 ((stringp event) ; x-popup or x-popup element
486 (setq km (if (or in-x-menu (stringp (car-safe elt)))
487 elt (cons 'keymap elt)))))
488 (unless (or (eq km 'ignore) (null str))
489 (let ((binding (where-is-internal km nil t)))
491 (setq binding (key-description binding))
492 ;; Try to align the keybindings.
493 (let ((colwidth (min 30 (- (/ (window-width) 2) 10))))
496 (make-string (max 2 (- colwidth
498 (string-width binding)))
501 (and km (stringp km) (setq str km))
502 ;; Verify that the command is enabled;
503 ;; if not, don't mention it.
504 (when (and km (symbolp km) (get km 'menu-enable))
505 (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
507 (or (assoc str tmm-km-list)
508 (push (cons str (cons event km)) tmm-km-list))))))
510 (defun tmm-get-keybind (keyseq)
511 "Return the current binding of KEYSEQ, merging prefix definitions.
512 If KEYSEQ is a prefix key that has local and global bindings,
513 we merge them into a single keymap which shows the proper order of the menu.
514 However, for the menu bar itself, the value does not take account
515 of `menu-bar-final-items'."
516 (lookup-key (cons 'keymap (nreverse (current-active-maps))) keyseq))