]> code.delx.au - gnu-emacs/blob - lisp/tmm.el
(path-separator, grep-null-device, grep-regexp-alist)
[gnu-emacs] / lisp / tmm.el
1 ;;; tmm.el --- text mode access to menu-bar
2
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
4
5 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
6 ;; Maintainer: FSF
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
27 ;; To use this package add
28
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.
34
35 ;;; Code:
36
37 (require 'electric)
38
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)
46
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)
50
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)))
86
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)))))
95
96 (defvar tmm-mid-prompt "==>"
97 "String to insert between shortcut and menu item or nil.")
98
99 (defvar tmm-mb-map nil
100 "A place to store minibuffer map.")
101
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.
107 "
108 "String to insert at top of completion buffer.
109 If this is nil, delete even the usual help text
110 and show just the alternatives.")
111
112 ;;;###autoload
113 (defun tmm-prompt (menu &optional in-popup default-item)
114 "Text-mode emulation of calling the bindings in keymap.
115 Creates a text-mode menu of possible choices. You can access the elements
116 in the menu in two ways:
117 *) via history mechanism from minibuffer;
118 *) Or via completion-buffer that is automatically shown.
119 The last alternative is currently a hack, you cannot use mouse reliably.
120
121 MENU is like the MENU argument to `x-popup-menu': either a
122 keymap or an alist of alists.
123 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
124 Its value should be an event that has a binding in MENU."
125 ;; If the optional argument IN-POPUP is t,
126 ;; then MENU is an alist of elements of the form (STRING . VALUE).
127 ;; That is used for recursive calls only.
128 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
129 ; so it doesn't have a name.
130 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
131 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
132 chosen-string choice
133 (not-menu (not (keymapp menu))))
134 (run-hooks 'activate-menubar-hook)
135 ;; Compute tmm-km-list from MENU.
136 ;; tmm-km-list is an alist of (STRING . MEANING).
137 ;; It has no other elements.
138 ;; The order of elements in tmm-km-list is the order of the menu bar.
139 (mapcar (function (lambda (elt)
140 (if (stringp elt)
141 (setq gl-str elt)
142 (and (listp elt) (tmm-get-keymap elt not-menu)))))
143 menu)
144 ;; Choose an element of tmm-km-list; put it in choice.
145 (if (and not-menu (= 1 (length tmm-km-list)))
146 ;; If this is the top-level of an x-popup-menu menu,
147 ;; and there is just one pane, choose that one silently.
148 ;; This way we only ask the user one question,
149 ;; for which element of that pane.
150 (setq choice (cdr (car tmm-km-list)))
151 (and tmm-km-list
152 (let ((index-of-default 0))
153 (if tmm-mid-prompt
154 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
155 t)
156 ;; Find the default item's index within the menu bar.
157 ;; We use this to decide the initial minibuffer contents
158 ;; and initial history position.
159 (if default-item
160 (let ((tail menu))
161 (while (and tail
162 (not (eq (car-safe (car tail)) default-item)))
163 ;; Be careful to count only the elements of MENU
164 ;; that actually constitute menu bar items.
165 (if (and (consp (car tail))
166 (stringp (car-safe (cdr (car tail)))))
167 (setq index-of-default (1+ index-of-default)))
168 (setq tail (cdr tail)))))
169 (setq history (reverse (mapcar 'car tmm-km-list)))
170 (setq history-len (length history))
171 (setq history (append history history history history))
172 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
173 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
174 (unwind-protect
175 (setq out
176 (completing-read
177 (concat gl-str " (up/down to change, PgUp to menu): ")
178 tmm-km-list nil t nil
179 (cons 'history (- (* 2 history-len) index-of-default))))
180 (save-excursion
181 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
182 (if (get-buffer "*Completions*")
183 (progn
184 (set-buffer "*Completions*")
185 (use-local-map tmm-old-comp-map)
186 (bury-buffer (current-buffer)))))
187 )))
188 (setq choice (cdr (assoc out tmm-km-list)))
189 (and (null choice)
190 (> (length out) (length tmm-c-prompt))
191 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
192 (setq out (substring out (length tmm-c-prompt))
193 choice (cdr (assoc out tmm-km-list))))
194 (and (null choice)
195 (setq out (try-completion out tmm-km-list)
196 choice (cdr (assoc out tmm-km-list)))))
197 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
198 (setq chosen-string (car choice))
199 (setq choice (cdr choice))
200 (cond (in-popup
201 ;; We just did the inner level of a -popup menu.
202 choice)
203 ;; We just did the outer level. Do the inner level now.
204 (not-menu (tmm-prompt choice t))
205 ;; We just handled a menu keymap and found another keymap.
206 ((keymapp choice)
207 (if (symbolp choice)
208 (setq choice (indirect-function choice)))
209 (condition-case nil
210 (require 'mouse)
211 (error nil))
212 (condition-case nil
213 (x-popup-menu nil choice) ; Get the shortcuts
214 (error nil))
215 (tmm-prompt choice))
216 ;; We just handled a menu keymap and found a command.
217 (choice
218 (if chosen-string
219 (progn
220 (setq last-command-event chosen-string)
221 (call-interactively choice))
222 choice)))))
223
224
225 (defun tmm-add-shortcuts (list)
226 "Adds shortcuts to cars of elements of the list.
227 Takes a list of lists with a string as car, returns list with
228 shortcuts added to these cars.
229 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
230 (let ((next-shortcut-number 0))
231 (mapcar (lambda (elt)
232 (let ((str (car elt)) f b)
233 (setq f (upcase (substring str 0 1)))
234 ;; If does not work, try beginning of the other word
235 (if (and (member f tmm-short-cuts)
236 (string-match " \\([^ ]\\)" str))
237 (setq f (upcase (substring
238 str
239 (setq b (match-beginning 1)) (1+ b)))))
240 ;; If we don't have an unique letter shortcut,
241 ;; pick a digit as a shortcut instead.
242 (if (member f tmm-short-cuts)
243 (if (< next-shortcut-number 10)
244 (setq f (format "%d" next-shortcut-number)
245 next-shortcut-number (1+ next-shortcut-number))
246 (setq f nil)))
247 (if (null f)
248 elt
249 (setq tmm-short-cuts (cons f tmm-short-cuts))
250 (cons (concat f tmm-mid-prompt str) (cdr elt)))))
251 (reverse list))))
252
253 (defun tmm-define-keys (minibuffer)
254 (mapcar (lambda (str)
255 (define-key (current-local-map) str 'tmm-shortcut)
256 (define-key (current-local-map) (downcase str) 'tmm-shortcut))
257 tmm-short-cuts)
258 (if minibuffer
259 (progn
260 (define-key (current-local-map) [pageup] 'tmm-goto-completions)
261 (define-key (current-local-map) [prior] 'tmm-goto-completions)
262 (define-key (current-local-map) "\ev" 'tmm-goto-completions)
263 (define-key (current-local-map) "\C-n" 'next-history-element)
264 (define-key (current-local-map) "\C-p" 'previous-history-element))))
265
266 (defun tmm-add-prompt ()
267 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
268 (make-local-hook 'minibuffer-exit-hook)
269 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
270 (let ((win (selected-window)))
271 (setq tmm-old-mb-map (current-local-map))
272 (use-local-map (append (make-sparse-keymap) tmm-old-mb-map))
273 (tmm-define-keys t)
274 ;; Get window and hide it for electric mode to get correct size
275 (save-window-excursion
276 (let ((completions
277 (mapcar 'car minibuffer-completion-table)))
278 (with-output-to-temp-buffer "*Completions*"
279 (display-completion-list completions)))
280 (set-buffer "*Completions*")
281 (goto-char 1)
282 (if tmm-completion-prompt
283 (insert tmm-completion-prompt)
284 ;; Delete even the usual help info that all completion buffers have.
285 (goto-char 1)
286 (delete-region 1 (search-forward "Possible completions are:\n")))
287 )
288 (save-excursion
289 (other-window 1) ; Electric-pop-up-window does
290 ; not work in minibuffer
291 (set-buffer (window-buffer (Electric-pop-up-window "*Completions*")))
292 (setq tmm-old-comp-map (current-local-map))
293 (use-local-map (append (make-sparse-keymap) tmm-old-comp-map))
294 (tmm-define-keys nil)
295 (select-window win) ; Cannot use
296 ; save-window-excursion, since
297 ; it restores the size
298 )
299 (insert tmm-c-prompt)))
300
301 (defun tmm-delete-map ()
302 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
303 (if tmm-old-mb-map
304 (use-local-map tmm-old-mb-map)))
305
306 (defun tmm-shortcut ()
307 "Choose the shortcut that the user typed."
308 (interactive)
309 (let ((c (upcase (char-to-string last-command-char))) s)
310 (if (member c tmm-short-cuts)
311 (if (equal (buffer-name) "*Completions*")
312 (progn
313 (beginning-of-buffer)
314 (re-search-forward
315 (concat "\\(^\\|[ \t]\\)" c tmm-mid-prompt))
316 (choose-completion))
317 (erase-buffer) ; In minibuffer
318 (mapcar (lambda (elt)
319 (if (string=
320 (substring (car elt) 0
321 (min (1+ (length tmm-mid-prompt))
322 (length (car elt))))
323 (concat c tmm-mid-prompt))
324 (setq s (car elt))))
325 tmm-km-list)
326 (insert s)
327 (exit-minibuffer)))))
328
329 (defun tmm-goto-completions ()
330 (interactive)
331 (setq tmm-c-prompt (buffer-string))
332 (erase-buffer)
333 (switch-to-buffer-other-window "*Completions*")
334 (search-forward tmm-c-prompt)
335 (search-backward tmm-c-prompt))
336
337
338 (defun tmm-get-keymap (elt &optional in-x-menu)
339 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
340 The values are deduced from the argument ELT, that should be an
341 element of keymap, an `x-popup-menu' argument, or an element of
342 `x-popup-menu' argument (when IN-X-MENU is not-nil).
343 This function adds the element only if it is not already present.
344 It uses the free variable `tmm-table-undef' to keep undefined keys."
345 (let (km str cache (event (car elt)))
346 (setq elt (cdr elt))
347 (if (eq elt 'undefined)
348 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
349 (or
350 (assoc event tmm-table-undef)
351 (and (if (listp elt)
352 (keymapp elt)
353 (fboundp elt))
354 (setq km elt))
355 (and (if (listp (cdr-safe elt))
356 (keymapp (cdr-safe elt))
357 (fboundp (cdr-safe elt)))
358 (setq km (cdr elt))
359 (and (stringp (car elt)) (setq str (car elt))))
360 (and (if (listp (cdr-safe (cdr-safe elt)))
361 (keymapp (cdr-safe (cdr-safe elt)))
362 (fboundp (cdr-safe (cdr-safe elt))))
363 (setq km (cdr (cdr elt)))
364 (and (stringp (car elt)) (setq str (car elt)))
365 (or (and str
366 (stringp (cdr (car (cdr elt)))) ; keyseq cache
367 (setq cache (cdr (car (cdr elt))))
368 cache (setq str (concat str cache))) str))
369 (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
370 (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
371 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
372 ; New style of easy-menu
373 (setq km (cdr (cdr (cdr elt))))
374 (and (stringp (car elt)) (setq str (car elt)))
375 (or (and str
376 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
377 (setq cache (cdr (car (cdr (cdr elt)))))
378 cache (setq str (concat str cache)))
379 str))
380 (and (stringp event) ; x-popup or x-popup element
381 (if (or in-x-menu (stringp (car-safe elt)))
382 (setq str event event nil km elt)
383 (setq str event event nil km (cons 'keymap elt))
384 )))
385 (and km (stringp km) (setq str km))
386 (and km str
387 (or (assoc str tmm-km-list)
388 (setq tmm-km-list
389 (cons (cons str (cons event km)) tmm-km-list)))
390 ))))
391
392
393 (defun tmm-get-keybind (keyseq)
394 "Return the current binding of KEYSEQ, merging prefix definitions.
395 If KEYSEQ is a prefix key that has local and global bindings,
396 we merge them into a single keymap which shows the proper order of the menu.
397 However, for the menu bar itself, the value does not take account
398 of `menu-bar-final-items'."
399 (let (allbind bind)
400 (setq bind (key-binding keyseq))
401 ;; If KEYSEQ is a prefix key, then BIND is either nil
402 ;; or a symbol defined as a keymap (which satisfies keymapp).
403 (if (keymapp bind)
404 (setq bind nil))
405 ;; If we have a non-keymap definition, return that.
406 (or bind
407 (progn
408 ;; Otherwise, it is a prefix, so make a list of the subcommands.
409 ;; Make a list of all the bindings in all the keymaps.
410 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
411 (setq allbind (cons (local-key-binding keyseq) allbind))
412 (setq allbind (cons (global-key-binding keyseq) allbind))
413 ;; Merge all the elements of ALLBIND into one keymap.
414 (mapcar (lambda (in)
415 (if (and (symbolp in) (keymapp in))
416 (setq in (symbol-function in)))
417 (and in (keymapp in)
418 (if (keymapp bind)
419 (setq bind (nconc bind (copy-sequence (cdr in))))
420 (setq bind (copy-sequence in)))))
421 allbind)
422 ;; Return that keymap.
423 bind))))
424
425 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
426
427 (provide 'tmm)
428
429 ;;; tmm.el ends here