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