Fix F10 behaviour. (Reported by Bernard Adrian.)
[emacs.git] / lisp / tmm.el
blob53b61a5184b89a05ce06c3b975de748ef799be36
1 ;;; tmm.el --- text mode access to menu-bar
3 ;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006 Free Software Foundation, Inc.
6 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
7 ;; Maintainer: FSF
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 2, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; This package provides text mode access to the menu bar.
31 ;;; Code:
33 (require 'electric)
35 (defgroup tmm nil
36 "Text mode access to menu-bar."
37 :prefix "tmm-"
38 :group 'menu)
40 ;;; The following will be localized, added only to pacify the compiler.
41 (defvar tmm-short-cuts)
42 (defvar tmm-old-mb-map nil)
43 (defvar tmm-old-comp-map)
44 (defvar tmm-c-prompt)
45 (defvar tmm-km-list)
46 (defvar tmm-next-shortcut-digit)
47 (defvar tmm-table-undef)
49 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
50 ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
52 ;;;###autoload
53 (defun tmm-menubar (&optional x-position)
54 "Text-mode emulation of looking and choosing from a menubar.
55 See the documentation for `tmm-prompt'.
56 X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
57 we make that menu bar item (the one at that position) the default choice."
58 (interactive)
59 (run-hooks 'menu-bar-update-hook)
60 ;; Obey menu-bar-final-items; put those items last.
61 (let ((menu-bar (tmm-get-keybind [menu-bar]))
62 menu-bar-item)
63 (let ((list menu-bar-final-items))
64 (while list
65 (let ((item (car list)))
66 ;; ITEM is the name of an item that we want to put last.
67 ;; Find it in MENU-BAR and move it to the end.
68 (let ((this-one (assq item menu-bar)))
69 (setq menu-bar (append (delq this-one menu-bar)
70 (list this-one)))))
71 (setq list (cdr list))))
72 (if x-position
73 (let ((tail menu-bar)
74 this-one
75 (column 0))
76 (while (and tail (< column x-position))
77 (setq this-one (car tail))
78 (if (and (consp (car tail))
79 (consp (cdr (car tail)))
80 (stringp (nth 1 (car tail))))
81 (setq column (+ column
82 (length (nth 1 (car tail)))
83 1)))
84 (setq tail (cdr tail)))
85 (setq menu-bar-item (car this-one))))
86 (tmm-prompt menu-bar nil menu-bar-item)))
88 ;;;###autoload
89 (defun tmm-menubar-mouse (event)
90 "Text-mode emulation of looking and choosing from a menubar.
91 This command is used when you click the mouse in the menubar
92 on a console which has no window system but does have a mouse.
93 See the documentation for `tmm-prompt'."
94 (interactive "e")
95 (tmm-menubar (car (posn-x-y (event-start event)))))
97 (defcustom tmm-mid-prompt "==>"
98 "*String to insert between shortcut and menu item.
99 If nil, there will be no shortcuts. It should not consist only of spaces,
100 or else the correct item might not be found in the `*Completions*' buffer."
101 :type 'string
102 :group 'tmm)
104 (defvar tmm-mb-map nil
105 "A place to store minibuffer map.")
107 (defcustom tmm-completion-prompt
108 "Press PageUp key to reach this buffer from the minibuffer.
109 Alternatively, you can use Up/Down keys (or your History keys) to change
110 the item in the minibuffer, and press RET when you are done, or press the
111 marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
113 "*Help text to insert on the top of the completion buffer.
114 To save space, you can set this to nil,
115 in which case the standard introduction text is deleted too."
116 :type '(choice string (const nil))
117 :group 'tmm)
119 (defcustom tmm-shortcut-style '(downcase upcase)
120 "*What letters to use as menu shortcuts.
121 Must be either one of the symbols `downcase' or `upcase',
122 or else a list of the two in the order you prefer."
123 :type '(choice (const downcase)
124 (const upcase)
125 (repeat (choice (const downcase) (const upcase))))
126 :group 'tmm)
128 (defcustom tmm-shortcut-words 2
129 "*How many successive words to try for shortcuts, nil means all.
130 If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
131 specify nil for this variable."
132 :type '(choice integer (const nil))
133 :group 'tmm)
135 (defface tmm-inactive
136 '((t :inherit shadow))
137 "Face used for inactive menu items."
138 :group 'tmm)
140 ;;;###autoload
141 (defun tmm-prompt (menu &optional in-popup default-item)
142 "Text-mode emulation of calling the bindings in keymap.
143 Creates a text-mode menu of possible choices. You can access the elements
144 in the menu in two ways:
145 *) via history mechanism from minibuffer;
146 *) Or via completion-buffer that is automatically shown.
147 The last alternative is currently a hack, you cannot use mouse reliably.
149 MENU is like the MENU argument to `x-popup-menu': either a
150 keymap or an alist of alists.
151 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
152 Its value should be an event that has a binding in MENU."
153 ;; If the optional argument IN-POPUP is t,
154 ;; then MENU is an alist of elements of the form (STRING . VALUE).
155 ;; That is used for recursive calls only.
156 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
157 ; so it doesn't have a name.
158 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
159 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
160 chosen-string choice
161 (not-menu (not (keymapp menu))))
162 (run-hooks 'activate-menubar-hook)
163 ;; Compute tmm-km-list from MENU.
164 ;; tmm-km-list is an alist of (STRING . MEANING).
165 ;; It has no other elements.
166 ;; The order of elements in tmm-km-list is the order of the menu bar.
167 (mapc (lambda (elt)
168 (if (stringp elt)
169 (setq gl-str elt)
170 (and (listp elt) (tmm-get-keymap elt not-menu))))
171 menu)
172 ;; Choose an element of tmm-km-list; put it in choice.
173 (if (and not-menu (= 1 (length tmm-km-list)))
174 ;; If this is the top-level of an x-popup-menu menu,
175 ;; and there is just one pane, choose that one silently.
176 ;; This way we only ask the user one question,
177 ;; for which element of that pane.
178 (setq choice (cdr (car tmm-km-list)))
179 (unless tmm-km-list
180 (error "Empty menu reached"))
181 (and tmm-km-list
182 (let ((index-of-default 0))
183 (if tmm-mid-prompt
184 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
186 ;; Find the default item's index within the menu bar.
187 ;; We use this to decide the initial minibuffer contents
188 ;; and initial history position.
189 (if default-item
190 (let ((tail menu))
191 (while (and tail
192 (not (eq (car-safe (car tail)) default-item)))
193 ;; Be careful to count only the elements of MENU
194 ;; that actually constitute menu bar items.
195 (if (and (consp (car tail))
196 (or (stringp (car-safe (cdr (car tail))))
197 (eq (car-safe (cdr (car tail))) 'menu-item)))
198 (setq index-of-default (1+ index-of-default)))
199 (setq tail (cdr tail)))))
200 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
201 (setq history
202 (reverse (delq nil
203 (mapcar
204 (lambda (elt)
205 (if (string-match prompt (car elt))
206 (car elt)))
207 tmm-km-list)))))
208 (setq history-len (length history))
209 (setq history (append history history history history))
210 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
211 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
212 (save-excursion
213 (unwind-protect
214 (setq out
215 (completing-read
216 (concat gl-str " (up/down to change, PgUp to menu): ")
217 tmm-km-list nil t nil
218 (cons 'history (- (* 2 history-len) index-of-default))))
219 (save-excursion
220 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
221 (if (get-buffer "*Completions*")
222 (progn
223 (set-buffer "*Completions*")
224 (use-local-map tmm-old-comp-map)
225 (bury-buffer (current-buffer)))))
226 ))))
227 (setq choice (cdr (assoc out tmm-km-list)))
228 (and (null choice)
229 (> (length out) (length tmm-c-prompt))
230 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
231 (setq out (substring out (length tmm-c-prompt))
232 choice (cdr (assoc out tmm-km-list))))
233 (and (null choice) out
234 (setq out (try-completion out tmm-km-list)
235 choice (cdr (assoc out tmm-km-list)))))
236 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
237 (setq chosen-string (car choice))
238 (setq choice (cdr choice))
239 (cond (in-popup
240 ;; We just did the inner level of a -popup menu.
241 choice)
242 ;; We just did the outer level. Do the inner level now.
243 (not-menu (tmm-prompt choice t))
244 ;; We just handled a menu keymap and found another keymap.
245 ((keymapp choice)
246 (if (symbolp choice)
247 (setq choice (indirect-function choice)))
248 (condition-case nil
249 (require 'mouse)
250 (error nil))
251 (condition-case nil
252 (x-popup-menu nil choice) ; Get the shortcuts
253 (error nil))
254 (tmm-prompt choice))
255 ;; We just handled a menu keymap and found a command.
256 (choice
257 (if chosen-string
258 (progn
259 (setq last-command-event chosen-string)
260 (call-interactively choice))
261 choice)))))
263 (defun tmm-add-shortcuts (list)
264 "Adds shortcuts to cars of elements of the list.
265 Takes a list of lists with a string as car, returns list with
266 shortcuts added to these cars.
267 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
268 (let ((tmm-next-shortcut-digit ?0))
269 (mapcar 'tmm-add-one-shortcut (reverse list))))
271 (defsubst tmm-add-one-shortcut (elt)
272 ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
273 (cond
274 ((eq (cddr elt) 'ignore)
275 (cons (concat " " (make-string (length tmm-mid-prompt) ?\-)
276 (car elt))
277 (cdr elt)))
279 (let* ((str (car elt))
280 (paren (string-match "(" str))
281 (pos 0) (word 0) char)
282 (catch 'done ; ??? is this slow?
283 (while (and (or (not tmm-shortcut-words) ; no limit on words
284 (< word tmm-shortcut-words)) ; try n words
285 (setq pos (string-match "\\w+" str pos)) ; get next word
286 (not (and paren (> pos paren)))) ; don't go past "(binding.."
287 (if (or (= pos 0)
288 (/= (aref str (1- pos)) ?.)) ; avoid file extensions
289 (let ((shortcut-style
290 (if (listp tmm-shortcut-style) ; convert to list
291 tmm-shortcut-style
292 (list tmm-shortcut-style))))
293 (while shortcut-style ; try upcase and downcase variants
294 (setq char (funcall (car shortcut-style) (aref str pos)))
295 (if (not (memq char tmm-short-cuts)) (throw 'done char))
296 (setq shortcut-style (cdr shortcut-style)))))
297 (setq word (1+ word))
298 (setq pos (match-end 0)))
299 (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
300 (setq char tmm-next-shortcut-digit)
301 (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
302 (if (not (memq char tmm-short-cuts)) (throw 'done char)))
303 (setq char nil))
304 (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
305 (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
306 ;; keep them lined up in columns
307 (make-string (1+ (length tmm-mid-prompt)) ?\ ))
308 str)
309 (cdr elt))))))
311 ;; This returns the old map.
312 (defun tmm-define-keys (minibuffer)
313 (let ((map (make-sparse-keymap)))
314 (suppress-keymap map t)
315 (mapc
316 (lambda (c)
317 (if (listp tmm-shortcut-style)
318 (define-key map (char-to-string c) 'tmm-shortcut)
319 ;; only one kind of letters are shortcuts, so map both upcase and
320 ;; downcase input to the same
321 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
322 (define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
323 tmm-short-cuts)
324 (if minibuffer
325 (progn
326 (define-key map [pageup] 'tmm-goto-completions)
327 (define-key map [prior] 'tmm-goto-completions)
328 (define-key map "\ev" 'tmm-goto-completions)
329 (define-key map "\C-n" 'next-history-element)
330 (define-key map "\C-p" 'previous-history-element)))
331 (prog1 (current-local-map)
332 (use-local-map (append map (current-local-map))))))
334 (defun tmm-completion-delete-prompt ()
335 (set-buffer standard-output)
336 (goto-char 1)
337 (delete-region 1 (search-forward "Possible completions are:\n")))
339 (defun tmm-remove-inactive-mouse-face ()
340 "Remove the mouse-face property from inactive menu items."
341 (let ((inhibit-read-only t)
342 (inactive-string
343 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
344 next)
345 (save-excursion
346 (goto-char (point-min))
347 (while (not (eobp))
348 (setq next (next-single-char-property-change (point) 'mouse-face))
349 (when (looking-at inactive-string)
350 (remove-text-properties (point) next '(mouse-face))
351 (add-text-properties (point) next '(face tmm-inactive)))
352 (goto-char next)))
353 (set-buffer-modified-p nil)))
355 (defun tmm-add-prompt ()
356 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
357 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
358 (unless tmm-c-prompt
359 (error "No active menu entries"))
360 (let ((win (selected-window)))
361 (setq tmm-old-mb-map (tmm-define-keys t))
362 ;; Get window and hide it for electric mode to get correct size
363 (save-window-excursion
364 (let ((completions
365 (mapcar 'car minibuffer-completion-table)))
366 (or tmm-completion-prompt
367 (add-hook 'completion-setup-hook
368 'tmm-completion-delete-prompt 'append))
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
380 ; not work in minibuffer
381 (Electric-pop-up-window "*Completions*")
382 (with-current-buffer "*Completions*"
383 (setq tmm-old-comp-map (tmm-define-keys nil))))
384 (insert tmm-c-prompt)))
386 (defun tmm-delete-map ()
387 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
388 (if tmm-old-mb-map
389 (use-local-map tmm-old-mb-map)))
391 (defun tmm-shortcut ()
392 "Choose the shortcut that the user typed."
393 (interactive)
394 (let ((c last-command-char) s)
395 (if (symbolp tmm-shortcut-style)
396 (setq c (funcall tmm-shortcut-style c)))
397 (if (memq c tmm-short-cuts)
398 (if (equal (buffer-name) "*Completions*")
399 (progn
400 (goto-char (point-min))
401 (re-search-forward
402 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
403 (choose-completion))
404 ;; In minibuffer
405 (delete-region (minibuffer-prompt-end) (point-max))
406 (mapc (lambda (elt)
407 (if (string=
408 (substring (car elt) 0
409 (min (1+ (length tmm-mid-prompt))
410 (length (car elt))))
411 (concat (char-to-string c) tmm-mid-prompt))
412 (setq s (car elt))))
413 tmm-km-list)
414 (insert s)
415 (exit-minibuffer)))))
417 (defun tmm-goto-completions ()
418 (interactive)
419 (let ((prompt-end (minibuffer-prompt-end)))
420 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
421 (delete-region prompt-end (point-max)))
422 (switch-to-buffer-other-window "*Completions*")
423 (search-forward tmm-c-prompt)
424 (search-backward tmm-c-prompt))
426 (defun tmm-get-keymap (elt &optional in-x-menu)
427 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
428 The values are deduced from the argument ELT, that should be an
429 element of keymap, an `x-popup-menu' argument, or an element of
430 `x-popup-menu' argument (when IN-X-MENU is not-nil).
431 This function adds the element only if it is not already present.
432 It uses the free variable `tmm-table-undef' to keep undefined keys."
433 (let (km str cache plist filter visible enable (event (car elt)))
434 (setq elt (cdr elt))
435 (if (eq elt 'undefined)
436 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
437 (unless (assoc event tmm-table-undef)
438 (cond ((if (listp elt)
439 (or (keymapp elt) (eq (car elt) 'lambda))
440 (fboundp elt))
441 (setq km elt))
443 ((if (listp (cdr-safe elt))
444 (or (keymapp (cdr-safe elt))
445 (eq (car (cdr-safe elt)) 'lambda))
446 (fboundp (cdr-safe elt)))
447 (setq km (cdr elt))
448 (and (stringp (car elt)) (setq str (car elt))))
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))
453 (fboundp (cdr-safe (cdr-safe elt))))
454 (setq km (cdr (cdr elt)))
455 (and (stringp (car elt)) (setq str (car elt)))
456 (and str
457 (stringp (cdr (car (cdr elt)))) ; keyseq cache
458 (setq cache (cdr (car (cdr elt))))
459 cache (setq str (concat str cache))))
461 ((eq (car-safe elt) 'menu-item)
462 ;; (menu-item TITLE COMMAND KEY ...)
463 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
464 (when (consp (car-safe plist))
465 (setq plist (cdr-safe plist)))
466 (setq km (nth 2 elt))
467 (setq str (eval (nth 1 elt)))
468 (setq filter (plist-get plist :filter))
469 (if filter
470 (setq km (funcall filter km)))
471 (setq visible (plist-get plist :visible))
472 (if visible
473 (setq km (and (eval visible) km)))
474 (setq enable (plist-get plist :enable))
475 (if enable
476 (setq km (if (eval enable) km 'ignore)))
477 (and str
478 (consp (nth 3 elt))
479 (stringp (cdr (nth 3 elt))) ; keyseq cache
480 (setq cache (cdr (nth 3 elt)))
481 cache
482 (setq str (concat str cache))))
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))
487 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
488 ; New style of easy-menu
489 (setq km (cdr (cdr (cdr elt))))
490 (and (stringp (car elt)) (setq str (car elt)))
491 (and str
492 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
493 (setq cache (cdr (car (cdr (cdr elt)))))
494 cache (setq str (concat str cache))))
496 ((stringp event) ; x-popup or x-popup element
497 (if (or in-x-menu (stringp (car-safe elt)))
498 (setq str event event nil km elt)
499 (setq str event event nil km (cons 'keymap elt))
500 ))))
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)))
506 (and km str
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 (let (allbind bind)
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.
527 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
528 (setq allbind (cons (local-key-binding keyseq) allbind))
529 (setq allbind (cons (global-key-binding keyseq) allbind))
530 ;; Merge all the elements of ALLBIND into one keymap.
531 (mapc (lambda (in)
532 (if (and (symbolp in) (keymapp in))
533 (setq in (symbol-function in)))
534 (and in (keymapp in)
535 (if (keymapp bind)
536 (setq bind (nconc bind (copy-sequence (cdr in))))
537 (setq bind (copy-sequence in)))))
538 allbind)
539 ;; Return that keymap.
540 bind))))
542 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
544 (provide 'tmm)
546 ;;; arch-tag: e7ddbdb6-4b95-4da3-afbe-ad6063d112f4
547 ;;; tmm.el ends here