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