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>
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)
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.
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
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
)
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)
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."
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
]))
62 (let ((list menu-bar-final-items
))
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
)
70 (setq list
(cdr list
))))
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
)))
83 (setq tail
(cdr tail
)))
84 (setq menu-bar-item
(car this-one
))))
85 (tmm-prompt menu-bar nil menu-bar-item
)))
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'."
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.")
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
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)
140 (and (listp elt
) (tmm-get-keymap elt not-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
)))
150 (let ((index-of-default 0))
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.
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
)
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
))))
179 (remove-hook 'minibuffer-setup-hook
'tmm-add-prompt
)
180 (if (get-buffer "*Completions*")
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
)))
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
))))
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
))
199 ;; We just did the inner level of a -popup menu.
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.
206 (setq choice
(indirect-function choice
)))
211 (x-popup-menu nil choice
) ; Get the shortcuts
214 ;; We just handled a menu keymap and found a command.
218 (setq last-command-event chosen-string
)
219 (call-interactively 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
))
247 (setq tmm-short-cuts
(cons f tmm-short-cuts
))
248 (cons (concat f tmm-mid-prompt str
) (cdr elt
)))))
251 (defun tmm-define-keys ()
252 (mapcar (lambda (str)
253 (define-key (current-local-map) str
'tmm-shortcut
)
254 (define-key (current-local-map) (downcase str
) 'tmm-shortcut
))
256 (define-key (current-local-map) [pageup] 'tmm-goto-completions)
257 (define-key (current-local-map) [prior] 'tmm-goto-completions)
258 (define-key (current-local-map) "\ev" 'tmm-goto-completions)
259 (define-key (current-local-map) "\C-n" 'next-history-element)
260 (define-key (current-local-map) "\C-p" 'previous-history-element))
262 (defun tmm-add-prompt ()
263 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
264 (make-local-hook 'minibuffer-exit-hook)
265 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
266 (let ((win (selected-window)))
267 (setq tmm-old-mb-map (current-local-map))
268 (use-local-map (append (make-sparse-keymap) tmm-old-mb-map))
270 ;; Get window and hide it for electric mode to get correct size
271 (save-window-excursion
273 (mapcar 'car minibuffer-completion-table)))
274 (with-output-to-temp-buffer "*Completions*"
275 (display-completion-list completions)))
276 (set-buffer "*Completions*")
278 (insert tmm-completion-prompt)
281 (other-window 1) ; Electric-pop-up-window does
282 ; not work in minibuffer
283 (set-buffer (window-buffer (Electric-pop-up-window "*Completions*")))
284 (setq tmm-old-comp-map (current-local-map))
285 (use-local-map (append (make-sparse-keymap) tmm-old-comp-map))
287 (select-window win) ; Cannot use
288 ; save-window-excursion, since
289 ; it restores the size
291 (insert tmm-c-prompt)))
293 (defun tmm-delete-map ()
294 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
296 (use-local-map tmm-old-mb-map)))
298 (defun tmm-shortcut ()
299 "Choose the shortcut that the user typed."
301 (let ((c (upcase (char-to-string last-command-char))) s)
302 (if (member c tmm-short-cuts)
303 (if (equal (buffer-name) "*Completions*")
305 (beginning-of-buffer)
307 (concat "\\(^\\|[ \t]\\)" c tmm-mid-prompt))
309 (erase-buffer) ; In minibuffer
310 (mapcar (lambda (elt)
312 (substring (car elt) 0
313 (min (1+ (length tmm-mid-prompt))
315 (concat c tmm-mid-prompt))
319 (exit-minibuffer)))))
321 (defun tmm-goto-completions ()
323 (setq tmm-c-prompt (buffer-string))
325 (switch-to-buffer-other-window "*Completions*")
326 (search-forward tmm-c-prompt)
327 (search-backward tmm-c-prompt))
330 (defun tmm-get-keymap (elt &optional in-x-menu)
331 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
332 The values are deduced from the argument ELT, that should be an
333 element of keymap, an `x-popup-menu' argument, or an element of
334 `x-popup-menu' argument (when IN-X-MENU is not-nil).
335 This function adds the element only if it is not already present.
336 It uses the free variable `tmm-table-undef' to keep undefined keys."
337 (let (km str cache (event (car elt)))
339 (if (eq elt 'undefined)
340 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
342 (assoc event tmm-table-undef)
347 (and (if (listp (cdr-safe elt))
348 (keymapp (cdr-safe elt))
349 (fboundp (cdr-safe elt)))
351 (and (stringp (car elt)) (setq str (car elt))))
352 (and (if (listp (cdr-safe (cdr-safe elt)))
353 (keymapp (cdr-safe (cdr-safe elt)))
354 (fboundp (cdr-safe (cdr-safe elt))))
355 (setq km (cdr (cdr elt)))
356 (and (stringp (car elt)) (setq str (car elt)))
358 (stringp (cdr (car (cdr elt)))) ; keyseq cache
359 (setq cache (cdr (car (cdr elt))))
360 cache (setq str (concat str cache))) str))
361 (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
362 (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
363 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
364 ; New style of easy-menu
365 (setq km (cdr (cdr (cdr elt))))
366 (and (stringp (car elt)) (setq str (car elt)))
368 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
369 (setq cache (cdr (car (cdr (cdr elt)))))
370 cache (setq str (concat str cache)))
372 (and (stringp event) ; x-popup or x-popup element
373 (if (or in-x-menu (stringp (car-safe elt)))
374 (setq str event event nil km elt)
375 (setq str event event nil km (cons 'keymap elt))
377 (and km (stringp km) (setq str km))
379 (or (assoc str tmm-km-list)
381 (cons (cons str (cons event km)) tmm-km-list)))
385 (defun tmm-get-keybind (keyseq)
386 "Return the current binding of KEYSEQ, merging prefix definitions.
387 If KEYSEQ is a prefix key that has local and global bindings,
388 we merge them into a single keymap which shows the proper order of the menu.
389 However, for the menu bar itself, the value does not take account
390 of `menu-bar-final-items'."
392 (setq bind (key-binding keyseq))
393 ;; If KEYSEQ is a prefix key, then BIND is either nil
394 ;; or a symbol defined as a keymap (which satisfies keymapp).
397 ;; If we have a non-keymap definition, return that.
400 ;; Otherwise, it is a prefix, so make a list of the subcommands.
401 ;; Make a list of all the bindings in all the keymaps.
402 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
403 (setq allbind (cons (local-key-binding keyseq) allbind))
404 (setq allbind (cons (global-key-binding keyseq) allbind))
405 ;; Merge all the elements of ALLBIND into one keymap.
407 (if (and (symbolp in) (keymapp in))
408 (setq in (symbol-function in)))
411 (setq bind (nconc bind (copy-sequence (cdr in))))
412 (setq bind (copy-sequence in)))))
414 ;; Return that keymap.
417 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))