(bookmark-locate): ;;;###autoload this alias.
[emacs.git] / lisp / tmm.el
blobb648b9223187fc83d11b41462bf202bd2d40730e
1 ;;; tmm.el --- text mode access to menu-bar
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
5 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
6 ;; Maintainer: FSF
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; To use this package add
29 ;; (autoload 'tmm-menubar 'tmm "Text mode substitute for menubar" t)
30 ;; (global-set-key [f10] 'tmm-menubar)
31 ;; to your .emacs file. You can also add your own access to different
32 ;; menus available in Window System Emacs modeling definition after
33 ;; tmm-menubar.
35 ;;; Code:
37 (require 'electric)
39 ;;; The following will be localized, added only to pacify the compiler.
40 (defvar tmm-short-cuts)
41 (defvar tmm-old-mb-map nil)
42 (defvar tmm-old-comp-map)
43 (defvar tmm-c-prompt)
44 (defvar tmm-km-list)
45 (defvar tmm-table-undef)
47 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
48 ;;;###autoload (define-key global-map [f10] 'tmm-menubar)
49 ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
51 ;;;###autoload
52 (defun tmm-menubar (&optional x-position)
53 "Text-mode emulation of looking and choosing from a menubar.
54 See the documentation for `tmm-prompt'.
55 X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
56 we make that menu bar item (the one at that position) the default choice."
57 (interactive)
58 (run-hooks 'menu-bar-update-hook)
59 ;; Obey menu-bar-final-items; put those items last.
60 (let ((menu-bar (tmm-get-keybind [menu-bar]))
61 menu-bar-item)
62 (let ((list menu-bar-final-items))
63 (while list
64 (let ((item (car list)))
65 ;; ITEM is the name of an item that we want to put last.
66 ;; Find it in MENU-BAR and move it to the end.
67 (let ((this-one (assq item menu-bar)))
68 (setq menu-bar (append (delq this-one menu-bar)
69 (list this-one)))))
70 (setq list (cdr list))))
71 (if x-position
72 (let ((tail menu-bar)
73 this-one
74 (column 0))
75 (while (and tail (< column x-position))
76 (setq this-one (car tail))
77 (if (and (consp (car tail))
78 (consp (cdr (car tail)))
79 (stringp (nth 1 (car tail))))
80 (setq column (+ column
81 (length (nth 1 (car tail)))
82 1)))
83 (setq tail (cdr tail)))
84 (setq menu-bar-item (car this-one))))
85 (tmm-prompt menu-bar nil menu-bar-item)))
87 ;;;###autoload
88 (defun tmm-menubar-mouse (event)
89 "Text-mode emulation of looking and choosing from a menubar.
90 This command is used when you click the mouse in the menubar
91 on a console which has no window system but does have a mouse.
92 See the documentation for `tmm-prompt'."
93 (interactive "e")
94 (tmm-menubar (car (posn-x-y (event-start event)))))
96 (defvar tmm-mid-prompt "==>"
97 "String to insert between shortcut and menu item or nil.")
99 (defvar tmm-mb-map nil
100 "A place to store minibuffer map.")
102 (defvar tmm-completion-prompt
103 "Press PageUp Key to reach this buffer from the minibuffer.
104 Alternatively, you can use Up/Down keys (or your History keys) to change
105 the item in the minibuffer, and press RET when you are done, or press the
106 marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
108 "What insert on top of completion buffer.")
110 ;;;###autoload
111 (defun tmm-prompt (menu &optional in-popup default-item)
112 "Text-mode emulation of calling the bindings in keymap.
113 Creates a text-mode menu of possible choices. You can access the elements
114 in the menu in two ways:
115 *) via history mechanism from minibuffer;
116 *) Or via completion-buffer that is automatically shown.
117 The last alternative is currently a hack, you cannot use mouse reliably.
119 MENU is like the MENU argument to `x-popup-menu': either a
120 keymap or an alist of alists.
121 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
122 Its value should be an event that has a binding in MENU."
123 ;; If the optional argument IN-POPUP is t,
124 ;; then MENU is an alist of elements of the form (STRING . VALUE).
125 ;; That is used for recursive calls only.
126 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
127 ; so it doesn't have a name.
128 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
129 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
130 chosen-string choice
131 (not-menu (not (keymapp menu))))
132 (run-hooks 'activate-menubar-hook)
133 ;; Compute tmm-km-list from MENU.
134 ;; tmm-km-list is an alist of (STRING . MEANING).
135 ;; It has no other elements.
136 ;; The order of elements in tmm-km-list is the order of the menu bar.
137 (mapcar (function (lambda (elt)
138 (if (stringp elt)
139 (setq gl-str elt)
140 (and (listp elt) (tmm-get-keymap elt not-menu)))))
141 menu)
142 ;; Choose an element of tmm-km-list; put it in choice.
143 (if (and not-menu (= 1 (length tmm-km-list)))
144 ;; If this is the top-level of an x-popup-menu menu,
145 ;; and there is just one pane, choose that one silently.
146 ;; This way we only ask the user one question,
147 ;; for which element of that pane.
148 (setq choice (cdr (car tmm-km-list)))
149 (and tmm-km-list
150 (let ((index-of-default 0))
151 (if tmm-mid-prompt
152 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
154 ;; Find the default item's index within the menu bar.
155 ;; We use this to decide the initial minibuffer contents
156 ;; and initial history position.
157 (if default-item
158 (let ((tail menu))
159 (while (and tail
160 (not (eq (car-safe (car tail)) default-item)))
161 ;; Be careful to count only the elements of MENU
162 ;; that actually constitute menu bar items.
163 (if (and (consp (car tail))
164 (stringp (car-safe (cdr (car tail)))))
165 (setq index-of-default (1+ index-of-default)))
166 (setq tail (cdr tail)))))
167 (setq history (reverse (mapcar 'car tmm-km-list)))
168 (setq history-len (length history))
169 (setq history (append history history history history))
170 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
171 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
172 (unwind-protect
173 (setq out
174 (completing-read
175 (concat gl-str " (up/down to change, PgUp to menu): ")
176 tmm-km-list nil t nil
177 (cons 'history (- (* 2 history-len) index-of-default))))
178 (save-excursion
179 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
180 (if (get-buffer "*Completions*")
181 (progn
182 (set-buffer "*Completions*")
183 (use-local-map tmm-old-comp-map)
184 (bury-buffer (current-buffer)))))
186 (setq choice (cdr (assoc out tmm-km-list)))
187 (and (null choice)
188 (> (length out) (length tmm-c-prompt))
189 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
190 (setq out (substring out (length tmm-c-prompt))
191 choice (cdr (assoc out tmm-km-list))))
192 (and (null choice)
193 (setq out (try-completion out tmm-km-list)
194 choice (cdr (assoc out tmm-km-list)))))
195 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
196 (setq chosen-string (car choice))
197 (setq choice (cdr choice))
198 (cond (in-popup
199 ;; We just did the inner level of a -popup menu.
200 choice)
201 ;; We just did the outer level. Do the inner level now.
202 (not-menu (tmm-prompt choice t))
203 ;; We just handled a menu keymap and found another keymap.
204 ((keymapp choice)
205 (if (symbolp choice)
206 (setq choice (indirect-function choice)))
207 (condition-case nil
208 (require 'mouse)
209 (error nil))
210 (condition-case nil
211 (x-popup-menu nil choice) ; Get the shortcuts
212 (error nil))
213 (tmm-prompt choice))
214 ;; We just handled a menu keymap and found a command.
215 (choice
216 (if chosen-string
217 (progn
218 (setq last-command-event chosen-string)
219 (call-interactively choice))
220 choice)))))
223 (defun tmm-add-shortcuts (list)
224 "Adds shortcuts to cars of elements of the list.
225 Takes a list of lists with a string as car, returns list with
226 shortcuts added to these cars.
227 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
228 (let ((next-shortcut-number 0))
229 (mapcar (lambda (elt)
230 (let ((str (car elt)) f b)
231 (setq f (upcase (substring str 0 1)))
232 ;; If does not work, try beginning of the other word
233 (if (and (member f tmm-short-cuts)
234 (string-match " \\([^ ]\\)" str))
235 (setq f (upcase (substring
237 (setq b (match-beginning 1)) (1+ b)))))
238 ;; If we don't have an unique letter shortcut,
239 ;; pick a digit as a shortcut instead.
240 (if (member f tmm-short-cuts)
241 (if (< next-shortcut-number 10)
242 (setq f (format "%d" next-shortcut-number)
243 next-shortcut-number (1+ next-shortcut-number))
244 (setq f nil)))
245 (if (null f)
247 (setq tmm-short-cuts (cons f tmm-short-cuts))
248 (cons (concat f tmm-mid-prompt str) (cdr elt)))))
249 (reverse list))))
251 (defun tmm-define-keys (minibuffer)
252 (mapcar (lambda (str)
253 (define-key (current-local-map) str 'tmm-shortcut)
254 (define-key (current-local-map) (downcase str) 'tmm-shortcut))
255 tmm-short-cuts)
256 (if minibuffer
257 (progn
258 (define-key (current-local-map) [pageup] 'tmm-goto-completions)
259 (define-key (current-local-map) [prior] 'tmm-goto-completions)
260 (define-key (current-local-map) "\ev" 'tmm-goto-completions)
261 (define-key (current-local-map) "\C-n" 'next-history-element)
262 (define-key (current-local-map) "\C-p" 'previous-history-element))))
264 (defun tmm-add-prompt ()
265 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
266 (make-local-hook 'minibuffer-exit-hook)
267 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
268 (let ((win (selected-window)))
269 (setq tmm-old-mb-map (current-local-map))
270 (use-local-map (append (make-sparse-keymap) tmm-old-mb-map))
271 (tmm-define-keys t)
272 ;; Get window and hide it for electric mode to get correct size
273 (save-window-excursion
274 (let ((completions
275 (mapcar 'car minibuffer-completion-table)))
276 (with-output-to-temp-buffer "*Completions*"
277 (display-completion-list completions)))
278 (set-buffer "*Completions*")
279 (goto-char 1)
280 (insert tmm-completion-prompt)
282 (save-excursion
283 (other-window 1) ; Electric-pop-up-window does
284 ; not work in minibuffer
285 (set-buffer (window-buffer (Electric-pop-up-window "*Completions*")))
286 (setq tmm-old-comp-map (current-local-map))
287 (use-local-map (append (make-sparse-keymap) tmm-old-comp-map))
288 (tmm-define-keys nil)
289 (select-window win) ; Cannot use
290 ; save-window-excursion, since
291 ; it restores the size
293 (insert tmm-c-prompt)))
295 (defun tmm-delete-map ()
296 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
297 (if tmm-old-mb-map
298 (use-local-map tmm-old-mb-map)))
300 (defun tmm-shortcut ()
301 "Choose the shortcut that the user typed."
302 (interactive)
303 (let ((c (upcase (char-to-string last-command-char))) s)
304 (if (member c tmm-short-cuts)
305 (if (equal (buffer-name) "*Completions*")
306 (progn
307 (beginning-of-buffer)
308 (re-search-forward
309 (concat "\\(^\\|[ \t]\\)" c tmm-mid-prompt))
310 (choose-completion))
311 (erase-buffer) ; In minibuffer
312 (mapcar (lambda (elt)
313 (if (string=
314 (substring (car elt) 0
315 (min (1+ (length tmm-mid-prompt))
316 (length (car elt))))
317 (concat c tmm-mid-prompt))
318 (setq s (car elt))))
319 tmm-km-list)
320 (insert s)
321 (exit-minibuffer)))))
323 (defun tmm-goto-completions ()
324 (interactive)
325 (setq tmm-c-prompt (buffer-string))
326 (erase-buffer)
327 (switch-to-buffer-other-window "*Completions*")
328 (search-forward tmm-c-prompt)
329 (search-backward tmm-c-prompt))
332 (defun tmm-get-keymap (elt &optional in-x-menu)
333 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
334 The values are deduced from the argument ELT, that should be an
335 element of keymap, an `x-popup-menu' argument, or an element of
336 `x-popup-menu' argument (when IN-X-MENU is not-nil).
337 This function adds the element only if it is not already present.
338 It uses the free variable `tmm-table-undef' to keep undefined keys."
339 (let (km str cache (event (car elt)))
340 (setq elt (cdr elt))
341 (if (eq elt 'undefined)
342 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
344 (assoc event tmm-table-undef)
345 (and (if (listp elt)
346 (keymapp elt)
347 (fboundp elt))
348 (setq km elt))
349 (and (if (listp (cdr-safe elt))
350 (keymapp (cdr-safe elt))
351 (fboundp (cdr-safe elt)))
352 (setq km (cdr elt))
353 (and (stringp (car elt)) (setq str (car elt))))
354 (and (if (listp (cdr-safe (cdr-safe elt)))
355 (keymapp (cdr-safe (cdr-safe elt)))
356 (fboundp (cdr-safe (cdr-safe elt))))
357 (setq km (cdr (cdr elt)))
358 (and (stringp (car elt)) (setq str (car elt)))
359 (or (and str
360 (stringp (cdr (car (cdr elt)))) ; keyseq cache
361 (setq cache (cdr (car (cdr elt))))
362 cache (setq str (concat str cache))) str))
363 (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
364 (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
365 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
366 ; New style of easy-menu
367 (setq km (cdr (cdr (cdr elt))))
368 (and (stringp (car elt)) (setq str (car elt)))
369 (or (and str
370 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
371 (setq cache (cdr (car (cdr (cdr elt)))))
372 cache (setq str (concat str cache)))
373 str))
374 (and (stringp event) ; x-popup or x-popup element
375 (if (or in-x-menu (stringp (car-safe elt)))
376 (setq str event event nil km elt)
377 (setq str event event nil km (cons 'keymap elt))
379 (and km (stringp km) (setq str km))
380 (and km str
381 (or (assoc str tmm-km-list)
382 (setq tmm-km-list
383 (cons (cons str (cons event km)) tmm-km-list)))
384 ))))
387 (defun tmm-get-keybind (keyseq)
388 "Return the current binding of KEYSEQ, merging prefix definitions.
389 If KEYSEQ is a prefix key that has local and global bindings,
390 we merge them into a single keymap which shows the proper order of the menu.
391 However, for the menu bar itself, the value does not take account
392 of `menu-bar-final-items'."
393 (let (allbind bind)
394 (setq bind (key-binding keyseq))
395 ;; If KEYSEQ is a prefix key, then BIND is either nil
396 ;; or a symbol defined as a keymap (which satisfies keymapp).
397 (if (keymapp bind)
398 (setq bind nil))
399 ;; If we have a non-keymap definition, return that.
400 (or bind
401 (progn
402 ;; Otherwise, it is a prefix, so make a list of the subcommands.
403 ;; Make a list of all the bindings in all the keymaps.
404 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
405 (setq allbind (cons (local-key-binding keyseq) allbind))
406 (setq allbind (cons (global-key-binding keyseq) allbind))
407 ;; Merge all the elements of ALLBIND into one keymap.
408 (mapcar (lambda (in)
409 (if (and (symbolp in) (keymapp in))
410 (setq in (symbol-function in)))
411 (and in (keymapp in)
412 (if (keymapp bind)
413 (setq bind (nconc bind (copy-sequence (cdr in))))
414 (setq bind (copy-sequence in)))))
415 allbind)
416 ;; Return that keymap.
417 bind))))
419 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
421 (provide 'tmm)
423 ;;; tmm.el ends here