1 ;;; tmm.el - text mode access to menu-bar
3 ;; Copyright (C) 1994, 1995 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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 ;;; Commentary ============================================================
26 ;;; To use this package add
28 ;;; (autoload 'tmm-menubar 'tmm "Text mode substitute for menubar" t)
29 ;;; (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 modelling definition after
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-old-comp-map
)
43 (defvar tmm-table-undef
)
45 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
49 "Text-mode emulation of looking and choosing from a menubar.
50 See the documentation for `tmm-prompt'."
52 (run-hooks 'menu-bar-update-hook
)
53 ;; Obey menu-bar-final-items; put those items last.
54 (let ((menu-bar (tmm-get-keybind [menu-bar
])))
55 (let ((list menu-bar-final-items
))
57 (let ((item (car list
)))
58 ;; ITEM is the name of an item that we want to put last.
59 ;; Find it in MENU-BAR and move it to the end.
60 (let ((this-one (assq item menu-bar
)))
61 (setq menu-bar
(append (delq this-one menu-bar
)
63 (setq list
(cdr list
))))
64 (tmm-prompt menu-bar
)))
66 (defvar tmm-mid-prompt
"==>"
67 "String to insert between shortcut and menu item or nil.")
69 (defvar tmm-mb-map nil
70 "A place to store minibuffer map.")
72 (defvar tmm-completion-prompt
73 "Press PageUp Key to reach this buffer from the minibuffer.
74 Alternatively, you can use Up/Down keys (or your History keys) to change
75 the item in the minibuffer, and press RET when you are done, or press the
76 marked letters to pick up your choice. Type ESC ESC to cancel.
78 "What insert on top of completion buffer.")
81 (defun tmm-prompt (bind &optional in-popup
)
82 "Text-mode emulation of calling the bindings in keymap.
83 Creates a text-mode menu of possible choices. You can access the elements
85 *) Either via history mechanism from minibuffer;
86 *) Or via completion-buffer that is automatically shown.
87 The last alternative is currently a hack, you cannot use mouse reliably.
88 If the optional argument IN-POPUP is set, is argument-compatible with
89 `x-popup-menu', otherwise the argument BIND should be a cdr of sparse keymap."
90 (if in-popup
(if bind
(setq bind in-popup
) (x-popup-menu nil in-popup
)))
91 (let (gl-str tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
92 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
)
93 (run-hooks 'activate-menubar-hook
)
94 (mapcar (function (lambda (elt)
97 (and (listp elt
) (tmm-get-keymap elt in-popup
)))))
102 (setq tmm-km-list
(tmm-add-shortcuts tmm-km-list
))
104 (setq history
(reverse (mapcar 'car tmm-km-list
)))
105 (setq history-len
(length history
))
106 (setq history
(append history history history history
))
107 (setq tmm-c-prompt
(nth (1- history-len
) history
))
108 (add-hook 'minibuffer-setup-hook
'tmm-add-prompt
)
112 (concat gl-str
" (up/down to change, PgUp to menu): ")
113 tmm-km-list nil t nil
114 (cons 'history
(* 2 history-len
))))
116 (set-buffer "*Completions*")
117 (use-local-map tmm-old-comp-map
)
118 (bury-buffer (current-buffer)))
120 (setq bind
(cdr (assoc out tmm-km-list
)))
122 (> (length out
) (length tmm-c-prompt
))
123 (string= (substring out
0 (length tmm-c-prompt
)) tmm-c-prompt
)
124 (setq out
(substring out
(length tmm-c-prompt
))
125 bind
(cdr (assoc out tmm-km-list
))))
127 (setq out
(try-completion out tmm-km-list
)
128 bind
(cdr (assoc out tmm-km-list
))))
129 (setq last-command-event
(car bind
))
130 (setq bind
(cdr bind
))
132 (if in-popup
(tmm-prompt t bind
)
140 (x-popup-menu nil bind
) ; Get the shortcuts
143 (tmm-prompt (symbol-value bind
))
145 (if last-command-event
146 (call-interactively bind
)
151 (defun tmm-add-shortcuts (list)
152 "Adds shortcuts to cars of elements of the list.
153 Takes a list of lists with a string as car, returns list with
154 shortcuts added to these cars.
155 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
156 (let ((next-shortcut-number 0))
157 (mapcar (lambda (elt)
158 (let ((str (car elt
)) f b
)
159 (setq f
(upcase (substring str
0 1)))
160 ;; If does not work, try beginning of the other word
161 (if (and (member f tmm-short-cuts
)
162 (string-match " \\([^ ]\\)" str
))
163 (setq f
(upcase (substring
165 (setq b
(match-beginning 1)) (1+ b
)))))
166 ;; If we don't have an unique letter shortcut,
167 ;; pick a digit as a shortcut instead.
168 (if (member f tmm-short-cuts
)
169 (if (< next-shortcut-number
10)
170 (setq f
(format "%d" next-shortcut-number
)
171 next-shortcut-number
(1+ next-shortcut-number
))
175 (setq tmm-short-cuts
(cons f tmm-short-cuts
))
176 (cons (concat f tmm-mid-prompt str
) (cdr elt
)))))
179 (defun tmm-add-prompt ()
180 (remove-hook 'minibuffer-setup-hook
'tmm-add-prompt
)
181 (make-local-hook 'minibuffer-exit-hook
)
182 (add-hook 'minibuffer-exit-hook
'tmm-delete-map nil t
)
183 (let ((map (make-sparse-keymap)) (win (selected-window)))
184 (mapcar (lambda (str)
185 (define-key map str
'tmm-shortcut
)
186 (define-key map
(downcase str
) 'tmm-shortcut
))
188 (setq tmm-old-mb-map
(current-local-map))
189 (use-local-map (append map
(cdr tmm-old-mb-map
)))
190 (define-key (current-local-map) [pageup] 'tmm-goto-completions)
191 (define-key (current-local-map) [prior] 'tmm-goto-completions)
192 (define-key (current-local-map) "\ev" 'tmm-goto-completions)
193 (define-key (current-local-map) "\e\e" 'abort-recursive-edit)
194 (define-key (current-local-map) "\C-n" 'next-history-element)
195 (define-key (current-local-map) "\C-p" 'previous-history-element)
196 ;; Get window and hide it for electric mode to get correct size
197 (save-window-excursion
199 (mapcar 'car minibuffer-completion-table)))
200 (with-output-to-temp-buffer "*Completions*"
201 (display-completion-list completions)))
202 (set-buffer "*Completions*")
204 (insert tmm-completion-prompt)
207 (other-window 1) ; Electric-pop-up-window does
208 ; not work in minibuffer
209 (set-buffer (window-buffer (Electric-pop-up-window "*Completions*")))
210 (setq tmm-old-comp-map (current-local-map))
211 (use-local-map (append map (cdr tmm-old-comp-map)))
212 (select-window win) ; Cannot use
213 ; save-window-excursion, since
214 ; it restores the size
216 (insert tmm-c-prompt)))
218 (defun tmm-delete-map ()
219 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
221 (use-local-map tmm-old-mb-map)))
223 (defun tmm-shortcut ()
224 "Choose the shortcut that the user typed."
226 (let ((c (upcase (char-to-string last-command-char))) s)
227 (if (member c tmm-short-cuts)
228 (if (equal (buffer-name) "*Completions*")
230 (beginning-of-buffer)
232 (concat "\\(^\\|[ \t]\\)" c tmm-mid-prompt))
234 (erase-buffer) ; In minibuffer
235 (mapcar (lambda (elt)
237 (substring (car elt) 0
238 (min (1+ (length tmm-mid-prompt))
240 (concat c tmm-mid-prompt))
244 (exit-minibuffer)))))
246 (defun tmm-goto-completions ()
248 (setq tmm-c-prompt (buffer-string))
250 (switch-to-buffer-other-window "*Completions*")
251 (search-forward tmm-c-prompt)
252 (search-backward tmm-c-prompt))
255 (defun tmm-get-keymap (elt &optional in-x-menu)
256 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
257 The values are deduced from the argument ELT, that should be an
258 element of keymap, an `x-popup-menu' argument, or an element of
259 `x-popup-menu' argument (when IN-X-MENU is not-nil).
260 Does it only if it is not already there. Uses free variable
261 `tmm-table-undef' to keep undefined keys."
262 (let (km str cache (event (car elt)))
264 (if (eq elt 'undefined)
265 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
267 (assoc event tmm-table-undef)
272 (and (if (listp (cdr-safe elt))
273 (keymapp (cdr-safe elt))
274 (fboundp (cdr-safe elt)))
276 (and (stringp (car elt)) (setq str (car elt))))
277 (and (if (listp (cdr-safe (cdr-safe elt)))
278 (keymapp (cdr-safe (cdr-safe elt)))
279 (fboundp (cdr-safe (cdr-safe elt))))
280 (setq km (cdr (cdr elt)))
281 (and (stringp (car elt)) (setq str (car elt)))
283 (stringp (cdr (car (cdr elt)))) ; keyseq cache
284 (setq cache (cdr (car (cdr elt))))
285 cache (setq str (concat str cache))) str))
286 (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
287 (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
288 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
289 ; New style of easy-menu
290 (setq km (cdr (cdr (cdr elt))))
291 (and (stringp (car elt)) (setq str (car elt)))
293 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
294 (setq cache (cdr (car (cdr (cdr elt)))))
295 cache (setq str (concat str cache)))
297 (and (stringp event) ; x-popup or x-popup element
298 (if (or in-x-menu (stringp (car-safe elt)))
299 (setq str event event nil km elt)
300 (setq str event event nil km (cons 'keymap elt))
302 (and km (stringp km) (setq str km))
304 (or (assoc str tmm-km-list)
306 (cons (cons str (cons event km)) tmm-km-list)))
310 (defun tmm-get-keybind (keyseq)
311 "Return the current binding of KEYSEQ, merging prefix definitions.
312 If KEYSEQ is a prefix key that has local and gloibal bindings,
313 we merge them into a single keymap which shows the proper order of the menu.
314 However, for the menu bar itself, the value does not take account
315 of `menu-bar-final-items'."
317 (setq bind (key-binding keyseq))
318 ;; If KEYSEQ is a prefix key, then BIND is either nil
319 ;; or a symbol defined as a keymap (which satisfies keymapp).
322 ;; If we have a non-keymap definition, return that.
325 ;; Otherwise, it is a prefix, so make a list of the subcommands.
326 ;; Make a list of all the bindings in all the keymaps.
327 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
328 (setq allbind (cons (local-key-binding keyseq) allbind))
329 (setq allbind (cons (global-key-binding keyseq) allbind))
330 ;; Merge all the elements of ALLBIND into one keymap.
332 (if (and (symbolp in) (keymapp in))
333 (setq in (symbol-function in)))
336 (setq bind (nconc bind (copy-sequence (cdr in))))
337 (setq bind (copy-sequence in)))))
339 ;; Return that keymap.
342 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))