(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / tmm.el
blob51e04941730d6c06ed4259d927d1254ed72e416a
1 ;;; tmm.el --- text mode access to menu-bar
3 ;; Copyright (C) 1994, 1995, 1996, 2000, 2001, 2002
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
7 ;; Maintainer: FSF
8 ;; Keywords: convenience
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;; This package provides text mode access to the menu bar.
31 ;;; Code:
33 (require 'electric)
35 (defgroup tmm nil
36 "Text mode access to menu-bar."
37 :prefix "tmm-"
38 :group 'menu)
40 ;;; The following will be localized, added only to pacify the compiler.
41 (defvar tmm-short-cuts)
42 (defvar tmm-old-mb-map nil)
43 (defvar tmm-old-comp-map)
44 (defvar tmm-c-prompt)
45 (defvar tmm-km-list)
46 (defvar tmm-next-shortcut-digit)
47 (defvar tmm-table-undef)
49 ;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
50 ;;;###autoload (define-key global-map [f10] 'tmm-menubar)
51 ;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
53 ;;;###autoload
54 (defun tmm-menubar (&optional x-position)
55 "Text-mode emulation of looking and choosing from a menubar.
56 See the documentation for `tmm-prompt'.
57 X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
58 we make that menu bar item (the one at that position) the default choice."
59 (interactive)
60 (run-hooks 'menu-bar-update-hook)
61 ;; Obey menu-bar-final-items; put those items last.
62 (let ((menu-bar (tmm-get-keybind [menu-bar]))
63 menu-bar-item)
64 (let ((list menu-bar-final-items))
65 (while list
66 (let ((item (car list)))
67 ;; ITEM is the name of an item that we want to put last.
68 ;; Find it in MENU-BAR and move it to the end.
69 (let ((this-one (assq item menu-bar)))
70 (setq menu-bar (append (delq this-one menu-bar)
71 (list this-one)))))
72 (setq list (cdr list))))
73 (if x-position
74 (let ((tail menu-bar)
75 this-one
76 (column 0))
77 (while (and tail (< column x-position))
78 (setq this-one (car tail))
79 (if (and (consp (car tail))
80 (consp (cdr (car tail)))
81 (stringp (nth 1 (car tail))))
82 (setq column (+ column
83 (length (nth 1 (car tail)))
84 1)))
85 (setq tail (cdr tail)))
86 (setq menu-bar-item (car this-one))))
87 (tmm-prompt menu-bar nil menu-bar-item)))
89 ;;;###autoload
90 (defun tmm-menubar-mouse (event)
91 "Text-mode emulation of looking and choosing from a menubar.
92 This command is used when you click the mouse in the menubar
93 on a console which has no window system but does have a mouse.
94 See the documentation for `tmm-prompt'."
95 (interactive "e")
96 (tmm-menubar (car (posn-x-y (event-start event)))))
98 (defcustom tmm-mid-prompt "==>"
99 "*String to insert between shortcut and menu item.
100 If nil, there will be no shortcuts. It should not consist only of spaces,
101 or else the correct item might not be found in the `*Completions*' buffer."
102 :type 'string
103 :group 'tmm)
105 (defvar tmm-mb-map nil
106 "A place to store minibuffer map.")
108 (defcustom tmm-completion-prompt
109 "Press PageUp key to reach this buffer from the minibuffer.
110 Alternatively, you can use Up/Down keys (or your History keys) to change
111 the item in the minibuffer, and press RET when you are done, or press the
112 marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
114 "*Help text to insert on the top of the completion buffer.
115 To save space, you can set this to nil,
116 in which case the standard introduction text is deleted too."
117 :type '(choice string (const nil))
118 :group 'tmm)
120 (defcustom tmm-shortcut-style '(downcase upcase)
121 "*What letters to use as menu shortcuts.
122 Must be either one of the symbols `downcase' or `upcase',
123 or else a list of the two in the order you prefer."
124 :type '(choice (const downcase)
125 (const upcase)
126 (repeat (choice (const downcase) (const upcase))))
127 :group 'tmm)
129 (defcustom tmm-shortcut-words 2
130 "*How many successive words to try for shortcuts, nil means all.
131 If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
132 specify nil for this variable."
133 :type '(choice integer (const nil))
134 :group 'tmm)
136 (require 'font-lock)
137 (defface tmm-inactive-face
138 '((t :inherit font-lock-comment-face))
139 "Face used for inactive menu items."
140 :group 'tmm)
142 ;;;###autoload
143 (defun tmm-prompt (menu &optional in-popup default-item)
144 "Text-mode emulation of calling the bindings in keymap.
145 Creates a text-mode menu of possible choices. You can access the elements
146 in the menu in two ways:
147 *) via history mechanism from minibuffer;
148 *) Or via completion-buffer that is automatically shown.
149 The last alternative is currently a hack, you cannot use mouse reliably.
151 MENU is like the MENU argument to `x-popup-menu': either a
152 keymap or an alist of alists.
153 DEFAULT-ITEM, if non-nil, specifies an initial default choice.
154 Its value should be an event that has a binding in MENU."
155 ;; If the optional argument IN-POPUP is t,
156 ;; then MENU is an alist of elements of the form (STRING . VALUE).
157 ;; That is used for recursive calls only.
158 (let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
159 ; so it doesn't have a name.
160 tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
161 tmm-old-mb-map tmm-old-comp-map tmm-short-cuts
162 chosen-string choice
163 (not-menu (not (keymapp menu))))
164 (run-hooks 'activate-menubar-hook)
165 ;; Compute tmm-km-list from MENU.
166 ;; tmm-km-list is an alist of (STRING . MEANING).
167 ;; It has no other elements.
168 ;; The order of elements in tmm-km-list is the order of the menu bar.
169 (mapc (lambda (elt)
170 (if (stringp elt)
171 (setq gl-str elt)
172 (and (listp elt) (tmm-get-keymap elt not-menu))))
173 menu)
174 ;; Choose an element of tmm-km-list; put it in choice.
175 (if (and not-menu (= 1 (length tmm-km-list)))
176 ;; If this is the top-level of an x-popup-menu menu,
177 ;; and there is just one pane, choose that one silently.
178 ;; This way we only ask the user one question,
179 ;; for which element of that pane.
180 (setq choice (cdr (car tmm-km-list)))
181 (unless tmm-km-list
182 (error "Empty menu reached"))
183 (and tmm-km-list
184 (let ((index-of-default 0))
185 (if tmm-mid-prompt
186 (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
188 ;; Find the default item's index within the menu bar.
189 ;; We use this to decide the initial minibuffer contents
190 ;; and initial history position.
191 (if default-item
192 (let ((tail menu))
193 (while (and tail
194 (not (eq (car-safe (car tail)) default-item)))
195 ;; Be careful to count only the elements of MENU
196 ;; that actually constitute menu bar items.
197 (if (and (consp (car tail))
198 (or (stringp (car-safe (cdr (car tail))))
199 (eq (car-safe (cdr (car tail))) 'menu-item)))
200 (setq index-of-default (1+ index-of-default)))
201 (setq tail (cdr tail)))))
202 (let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
203 (setq history
204 (reverse (delq nil
205 (mapcar
206 (lambda (elt)
207 (if (string-match prompt (car elt))
208 (car elt)))
209 tmm-km-list)))))
210 (setq history-len (length history))
211 (setq history (append history history history history))
212 (setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
213 (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
214 (save-excursion
215 (unwind-protect
216 (setq out
217 (completing-read
218 (concat gl-str " (up/down to change, PgUp to menu): ")
219 tmm-km-list nil t nil
220 (cons 'history (- (* 2 history-len) index-of-default))))
221 (save-excursion
222 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
223 (if (get-buffer "*Completions*")
224 (progn
225 (set-buffer "*Completions*")
226 (use-local-map tmm-old-comp-map)
227 (bury-buffer (current-buffer)))))
228 ))))
229 (setq choice (cdr (assoc out tmm-km-list)))
230 (and (null choice)
231 (> (length out) (length tmm-c-prompt))
232 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
233 (setq out (substring out (length tmm-c-prompt))
234 choice (cdr (assoc out tmm-km-list))))
235 (and (null choice) out
236 (setq out (try-completion out tmm-km-list)
237 choice (cdr (assoc out tmm-km-list)))))
238 ;; CHOICE is now (STRING . MEANING). Separate the two parts.
239 (setq chosen-string (car choice))
240 (setq choice (cdr choice))
241 (cond (in-popup
242 ;; We just did the inner level of a -popup menu.
243 choice)
244 ;; We just did the outer level. Do the inner level now.
245 (not-menu (tmm-prompt choice t))
246 ;; We just handled a menu keymap and found another keymap.
247 ((keymapp choice)
248 (if (symbolp choice)
249 (setq choice (indirect-function choice)))
250 (condition-case nil
251 (require 'mouse)
252 (error nil))
253 (condition-case nil
254 (x-popup-menu nil choice) ; Get the shortcuts
255 (error nil))
256 (tmm-prompt choice))
257 ;; We just handled a menu keymap and found a command.
258 (choice
259 (if chosen-string
260 (progn
261 (setq last-command-event chosen-string)
262 (call-interactively choice))
263 choice)))))
265 (defun tmm-add-shortcuts (list)
266 "Adds shortcuts to cars of elements of the list.
267 Takes a list of lists with a string as car, returns list with
268 shortcuts added to these cars.
269 Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
270 (let ((tmm-next-shortcut-digit ?0))
271 (mapcar 'tmm-add-one-shortcut (reverse list))))
273 (defsubst tmm-add-one-shortcut (elt)
274 ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
275 (cond
276 ((eq (cddr elt) 'ignore)
277 (cons (concat " " (make-string (length tmm-mid-prompt) ?\-)
278 (car elt))
279 (cdr elt)))
281 (let* ((str (car elt))
282 (paren (string-match "(" str))
283 (pos 0) (word 0) char)
284 (catch 'done ; ??? is this slow?
285 (while (and (or (not tmm-shortcut-words) ; no limit on words
286 (< word tmm-shortcut-words)) ; try n words
287 (setq pos (string-match "\\w+" str pos)) ; get next word
288 (not (and paren (> pos paren)))) ; don't go past "(binding.."
289 (if (or (= pos 0)
290 (/= (aref str (1- pos)) ?.)) ; avoid file extensions
291 (let ((shortcut-style
292 (if (listp tmm-shortcut-style) ; convert to list
293 tmm-shortcut-style
294 (list tmm-shortcut-style))))
295 (while shortcut-style ; try upcase and downcase variants
296 (setq char (funcall (car shortcut-style) (aref str pos)))
297 (if (not (memq char tmm-short-cuts)) (throw 'done char))
298 (setq shortcut-style (cdr shortcut-style)))))
299 (setq word (1+ word))
300 (setq pos (match-end 0)))
301 (while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
302 (setq char tmm-next-shortcut-digit)
303 (setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
304 (if (not (memq char tmm-short-cuts)) (throw 'done char)))
305 (setq char nil))
306 (if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
307 (cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
308 ;; keep them lined up in columns
309 (make-string (1+ (length tmm-mid-prompt)) ?\ ))
310 str)
311 (cdr elt))))))
313 ;; This returns the old map.
314 (defun tmm-define-keys (minibuffer)
315 (let ((map (make-sparse-keymap)))
316 (suppress-keymap map t)
317 (mapc
318 (lambda (c)
319 (if (listp tmm-shortcut-style)
320 (define-key map (char-to-string c) 'tmm-shortcut)
321 ;; only one kind of letters are shortcuts, so map both upcase and
322 ;; downcase input to the same
323 (define-key map (char-to-string (downcase c)) 'tmm-shortcut)
324 (define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
325 tmm-short-cuts)
326 (if minibuffer
327 (progn
328 (define-key map [pageup] 'tmm-goto-completions)
329 (define-key map [prior] 'tmm-goto-completions)
330 (define-key map "\ev" 'tmm-goto-completions)
331 (define-key map "\C-n" 'next-history-element)
332 (define-key map "\C-p" 'previous-history-element)))
333 (prog1 (current-local-map)
334 (use-local-map (append map (current-local-map))))))
336 (defun tmm-completion-delete-prompt ()
337 (set-buffer standard-output)
338 (goto-char 1)
339 (delete-region 1 (search-forward "Possible completions are:\n")))
341 (defun tmm-remove-inactive-mouse-face ()
342 "Remove the mouse-face property from inactive menu items."
343 (let ((inhibit-read-only t)
344 (inactive-string
345 (concat " " (make-string (length tmm-mid-prompt) ?\-)))
346 next)
347 (save-excursion
348 (goto-char (point-min))
349 (while (not (eobp))
350 (setq next (next-single-char-property-change (point) 'mouse-face))
351 (when (looking-at inactive-string)
352 (remove-text-properties (point) next '(mouse-face))
353 (add-text-properties (point) next '(face tmm-inactive-face)))
354 (goto-char next)))
355 (set-buffer-modified-p nil)))
357 (defun tmm-add-prompt ()
358 (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
359 (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
360 (unless tmm-c-prompt
361 (error "No active menu entries"))
362 (let ((win (selected-window)))
363 (setq tmm-old-mb-map (tmm-define-keys t))
364 ;; Get window and hide it for electric mode to get correct size
365 (save-window-excursion
366 (let ((completions
367 (mapcar 'car minibuffer-completion-table)))
368 (or tmm-completion-prompt
369 (add-hook 'completion-setup-hook
370 'tmm-completion-delete-prompt 'append))
371 (with-output-to-temp-buffer "*Completions*"
372 (display-completion-list completions))
373 (remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt))
374 (set-buffer "*Completions*")
375 (tmm-remove-inactive-mouse-face)
376 (when tmm-completion-prompt
377 (let ((buffer-read-only nil))
378 (goto-char (point-min))
379 (insert tmm-completion-prompt))))
380 (save-selected-window
381 (other-window 1) ; Electric-pop-up-window does
382 ; not work in minibuffer
383 (Electric-pop-up-window "*Completions*")
384 (with-current-buffer "*Completions*"
385 (setq tmm-old-comp-map (tmm-define-keys nil))))
386 (insert tmm-c-prompt)))
388 (defun tmm-delete-map ()
389 (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
390 (if tmm-old-mb-map
391 (use-local-map tmm-old-mb-map)))
393 (defun tmm-shortcut ()
394 "Choose the shortcut that the user typed."
395 (interactive)
396 (let ((c last-command-char) s)
397 (if (symbolp tmm-shortcut-style)
398 (setq c (funcall tmm-shortcut-style c)))
399 (if (memq c tmm-short-cuts)
400 (if (equal (buffer-name) "*Completions*")
401 (progn
402 (goto-char (point-min))
403 (re-search-forward
404 (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
405 (choose-completion))
406 ;; In minibuffer
407 (delete-region (minibuffer-prompt-end) (point-max))
408 (mapc (lambda (elt)
409 (if (string=
410 (substring (car elt) 0
411 (min (1+ (length tmm-mid-prompt))
412 (length (car elt))))
413 (concat (char-to-string c) tmm-mid-prompt))
414 (setq s (car elt))))
415 tmm-km-list)
416 (insert s)
417 (exit-minibuffer)))))
419 (defun tmm-goto-completions ()
420 (interactive)
421 (let ((prompt-end (minibuffer-prompt-end)))
422 (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
423 (delete-region prompt-end (point-max)))
424 (switch-to-buffer-other-window "*Completions*")
425 (search-forward tmm-c-prompt)
426 (search-backward tmm-c-prompt))
428 (defun tmm-get-keymap (elt &optional in-x-menu)
429 "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
430 The values are deduced from the argument ELT, that should be an
431 element of keymap, an `x-popup-menu' argument, or an element of
432 `x-popup-menu' argument (when IN-X-MENU is not-nil).
433 This function adds the element only if it is not already present.
434 It uses the free variable `tmm-table-undef' to keep undefined keys."
435 (let (km str cache plist filter visible enable (event (car elt)))
436 (setq elt (cdr elt))
437 (if (eq elt 'undefined)
438 (setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
439 (unless (assoc event tmm-table-undef)
440 (cond ((if (listp elt)
441 (or (keymapp elt) (eq (car elt) 'lambda))
442 (fboundp elt))
443 (setq km elt))
445 ((if (listp (cdr-safe elt))
446 (or (keymapp (cdr-safe elt))
447 (eq (car (cdr-safe elt)) 'lambda))
448 (fboundp (cdr-safe elt)))
449 (setq km (cdr elt))
450 (and (stringp (car elt)) (setq str (car elt))))
452 ((if (listp (cdr-safe (cdr-safe elt)))
453 (or (keymapp (cdr-safe (cdr-safe elt)))
454 (eq (car (cdr-safe (cdr-safe elt))) 'lambda))
455 (fboundp (cdr-safe (cdr-safe elt))))
456 (setq km (cdr (cdr elt)))
457 (and (stringp (car elt)) (setq str (car elt)))
458 (and str
459 (stringp (cdr (car (cdr elt)))) ; keyseq cache
460 (setq cache (cdr (car (cdr elt))))
461 cache (setq str (concat str cache))))
463 ((eq (car-safe elt) 'menu-item)
464 ;; (menu-item TITLE COMMAND KEY ...)
465 (setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
466 (when (consp (car-safe plist))
467 (setq plist (cdr-safe plist)))
468 (setq km (nth 2 elt))
469 (setq str (eval (nth 1 elt)))
470 (setq filter (plist-get plist :filter))
471 (if filter
472 (setq km (funcall filter km)))
473 (setq visible (plist-get plist :visible))
474 (if visible
475 (setq km (and (eval visible) km)))
476 (setq enable (plist-get plist :enable))
477 (if enable
478 (setq km (if (eval enable) km 'ignore)))
479 (and str
480 (consp (nth 3 elt))
481 (stringp (cdr (nth 3 elt))) ; keyseq cache
482 (setq cache (cdr (nth 3 elt)))
483 cache
484 (setq str (concat str cache))))
486 ((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
487 (or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
488 (eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
489 (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
490 ; New style of easy-menu
491 (setq km (cdr (cdr (cdr elt))))
492 (and (stringp (car elt)) (setq str (car elt)))
493 (and str
494 (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
495 (setq cache (cdr (car (cdr (cdr elt)))))
496 cache (setq str (concat str cache))))
498 ((stringp event) ; x-popup or x-popup element
499 (if (or in-x-menu (stringp (car-safe elt)))
500 (setq str event event nil km elt)
501 (setq str event event nil km (cons 'keymap elt))
502 ))))
503 (and km (stringp km) (setq str km))
504 ;; Verify that the command is enabled;
505 ;; if not, don't mention it.
506 (when (and km (symbolp km) (get km 'menu-enable))
507 (setq km (if (eval (get km 'menu-enable)) km 'ignore)))
508 (and km str
509 (or (assoc str tmm-km-list)
510 (push (cons str (cons event km)) tmm-km-list))))))
512 (defun tmm-get-keybind (keyseq)
513 "Return the current binding of KEYSEQ, merging prefix definitions.
514 If KEYSEQ is a prefix key that has local and global bindings,
515 we merge them into a single keymap which shows the proper order of the menu.
516 However, for the menu bar itself, the value does not take account
517 of `menu-bar-final-items'."
518 (let (allbind bind)
519 (setq bind (key-binding keyseq))
520 ;; If KEYSEQ is a prefix key, then BIND is either nil
521 ;; or a symbol defined as a keymap (which satisfies keymapp).
522 (if (keymapp bind)
523 (setq bind nil))
524 ;; If we have a non-keymap definition, return that.
525 (or bind
526 (progn
527 ;; Otherwise, it is a prefix, so make a list of the subcommands.
528 ;; Make a list of all the bindings in all the keymaps.
529 (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
530 (setq allbind (cons (local-key-binding keyseq) allbind))
531 (setq allbind (cons (global-key-binding keyseq) allbind))
532 ;; Merge all the elements of ALLBIND into one keymap.
533 (mapc (lambda (in)
534 (if (and (symbolp in) (keymapp in))
535 (setq in (symbol-function in)))
536 (and in (keymapp in)
537 (if (keymapp bind)
538 (setq bind (nconc bind (copy-sequence (cdr in))))
539 (setq bind (copy-sequence in)))))
540 allbind)
541 ;; Return that keymap.
542 bind))))
544 (add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
546 (provide 'tmm)
548 ;;; arch-tag: e7ddbdb6-4b95-4da3-afbe-ad6063d112f4
549 ;;; tmm.el ends here