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