tmm.el 12.3 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; tmm.el --- text mode access to menu-bar
Richard M. Stallman's avatar
Richard M. Stallman committed
2 3 4 5

;; Copyright (C) 1994, 1995 Free Software Foundation, Inc.

;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
6
;; Maintainer: FSF
Richard M. Stallman's avatar
Richard M. Stallman committed
7

Richard M. Stallman's avatar
Richard M. Stallman committed
8
;; This file is part of GNU Emacs.
Richard M. Stallman's avatar
Richard M. Stallman committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary ============================================================

;;; To use this package add 

;;; (autoload 'tmm-menubar 'tmm "Text mode substitute for menubar" t) 
;;; (global-set-key [f10] 'tmm-menubar)

;;; to your .emacs file. You can also add your own access to different
;;; menus available in Window System Emacs modelling definition after
;;; tmm-menubar.

(require 'electric)

;;; The following will be localized, added only to pacify the compiler.
(defvar tmm-short-cuts)
39
(defvar tmm-old-mb-map nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
40 41 42 43 44
(defvar tmm-old-comp-map)
(defvar tmm-c-prompt)
(defvar tmm-km-list)
(defvar tmm-table-undef)

45
;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
46

Richard M. Stallman's avatar
Richard M. Stallman committed
47 48 49 50 51 52
;;;###autoload
(defun tmm-menubar ()
  "Text-mode emulation of looking and choosing from a menubar.
See the documentation for `tmm-prompt'."
  (interactive)
  (run-hooks 'menu-bar-update-hook)
53 54 55 56 57 58 59 60 61 62 63 64
  ;; Obey menu-bar-final-items; put those items last.
  (let ((menu-bar (tmm-get-keybind [menu-bar])))
    (let ((list menu-bar-final-items))
      (while list
	(let ((item (car list)))
	  ;; ITEM is the name of an item that we want to put last.
	  ;; Find it in MENU-BAR and move it to the end.
	  (let ((this-one (assq item menu-bar)))
	    (setq menu-bar (append (delq this-one menu-bar)
				   (list this-one)))))
	(setq list (cdr list))))
    (tmm-prompt menu-bar)))
Richard M. Stallman's avatar
Richard M. Stallman committed
65 66 67 68 69 70 71 72 73 74 75

(defvar tmm-mid-prompt "==>"
  "String to insert between shortcut and menu item or nil.")

(defvar tmm-mb-map nil
  "A place to store minibuffer map.")

(defvar tmm-completion-prompt 
  "Press PageUp Key to reach this buffer from the minibuffer.
Alternatively, you can use Up/Down keys (or your History keys) to change
the item in the minibuffer, and press RET when you are done, or press the 
76
marked letters to pick up your choice.  Type ESC ESC to cancel.
Richard M. Stallman's avatar
Richard M. Stallman committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90
"
  "What insert on top of completion buffer.")

;;;###autoload
(defun tmm-prompt (bind &optional in-popup)
  "Text-mode emulation of calling the bindings in keymap.
Creates a text-mode menu of possible choices. You can access the elements
in the menu:
   *)  Either via history mechanism from minibuffer;
   *)  Or via completion-buffer that is automatically shown.
The last alternative is currently a hack, you cannot use mouse reliably.
If the optional argument IN-POPUP is set, is argument-compatible with 
`x-popup-menu', otherwise the argument BIND should be a cdr of sparse keymap."
  (if in-popup (if bind (setq bind in-popup) (x-popup-menu nil in-popup)))
91
  (let (gl-str tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
Richard M. Stallman's avatar
Richard M. Stallman committed
92 93 94 95 96
	       tmm-old-mb-map tmm-old-comp-map tmm-short-cuts)
    (run-hooks 'activate-menubar-hook)
    (mapcar (function (lambda (elt)
			(if (stringp elt)
			    (setq gl-str elt)
97 98
			  (and (listp elt) (tmm-get-keymap elt in-popup)))))
	    bind)
Richard M. Stallman's avatar
Richard M. Stallman committed
99
    (and tmm-km-list
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	 (progn
	   (if tmm-mid-prompt
	       (setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
	     t)
	   (setq history (reverse (mapcar 'car tmm-km-list)))
	   (setq history-len (length history))
	   (setq history (append history history history history))
	   (setq tmm-c-prompt (nth (1- history-len) history))
	   (add-hook 'minibuffer-setup-hook 'tmm-add-prompt)
	   (unwind-protect
	       (setq out
		     (completing-read
		      (concat gl-str " (up/down to change, PgUp to menu): ")
		      tmm-km-list nil t nil
		      (cons 'history (* 2 history-len))))
	     (save-excursion
	       (set-buffer "*Completions*")
	       (use-local-map tmm-old-comp-map)
	       (bury-buffer (current-buffer)))
	     )))
Richard M. Stallman's avatar
Richard M. Stallman committed
120 121 122 123 124 125
    (setq bind (cdr (assoc out tmm-km-list)))
    (and (null bind)
	 (> (length out) (length tmm-c-prompt))
	 (string= (substring out 0 (length tmm-c-prompt)) tmm-c-prompt)
	 (setq out (substring out (length tmm-c-prompt))
	       bind (cdr (assoc out tmm-km-list))))
126 127 128
    (and (null bind)
	 (setq out (try-completion out tmm-km-list)
	       bind (cdr (assoc  out tmm-km-list))))
Richard M. Stallman's avatar
Richard M. Stallman committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    (setq last-command-event (car bind))
    (setq bind (cdr bind))
    (if bind
	 (if in-popup (tmm-prompt t bind)
	   (if (keymapp bind)
	       (if (listp bind)
		   (progn
		     (condition-case nil
			 (require 'mouse)
		       (error nil))
		     (condition-case nil
			 (x-popup-menu nil bind) ; Get the shortcuts
		       (error nil))
		     (tmm-prompt bind))
		 (tmm-prompt (symbol-value bind))
		 )
	     (if last-command-event
		 (call-interactively bind)
	       bind)))
      gl-str)))


(defun tmm-add-shortcuts (list)
  "Adds shortcuts to cars of elements of the list.
Takes a list of lists with a string as car, returns list with
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
shortcuts added to these cars.
Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
  (let ((next-shortcut-number 0))
    (mapcar (lambda (elt)
	      (let ((str (car elt)) f b)
		(setq f (upcase (substring str 0 1)))
		;; If does not work, try beginning of the other word
		(if (and (member f tmm-short-cuts)
			 (string-match " \\([^ ]\\)" str))
		    (setq f (upcase (substring
				     str
				     (setq b (match-beginning 1)) (1+ b)))))
		;; If we don't have an unique letter shortcut,
		;; pick a digit as a shortcut instead.
		(if (member f tmm-short-cuts)
		    (if (< next-shortcut-number 10)
			(setq f (format "%d" next-shortcut-number)
			      next-shortcut-number (1+ next-shortcut-number))
		      (setq f nil)))
		(if (null f)
		    elt
		  (setq tmm-short-cuts (cons f tmm-short-cuts))
		  (cons (concat f tmm-mid-prompt str) (cdr elt)))))
	    (reverse list))))
Richard M. Stallman's avatar
Richard M. Stallman committed
178 179 180

(defun tmm-add-prompt ()
  (remove-hook 'minibuffer-setup-hook 'tmm-add-prompt)
181 182
  (make-local-hook 'minibuffer-exit-hook)
  (add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
Richard M. Stallman's avatar
Richard M. Stallman committed
183 184 185 186 187 188 189
  (let ((map (make-sparse-keymap)) (win (selected-window)))
    (mapcar (lambda (str)
	      (define-key map str 'tmm-shortcut)
	      (define-key map (downcase str) 'tmm-shortcut))
	    tmm-short-cuts)
    (setq tmm-old-mb-map (current-local-map))
    (use-local-map (append map (cdr tmm-old-mb-map)))
190 191 192 193 194 195
    (define-key (current-local-map) [pageup] 'tmm-goto-completions)
    (define-key (current-local-map) [prior] 'tmm-goto-completions)
    (define-key (current-local-map) "\ev" 'tmm-goto-completions)
    (define-key (current-local-map) "\e\e" 'abort-recursive-edit)
    (define-key (current-local-map) "\C-n" 'next-history-element)
    (define-key (current-local-map) "\C-p" 'previous-history-element)
Richard M. Stallman's avatar
Richard M. Stallman committed
196 197
    ;; Get window and hide it for electric mode to get correct size
    (save-window-excursion 
198 199 200 201
      (let ((completions
	     (mapcar 'car minibuffer-completion-table)))
	(with-output-to-temp-buffer "*Completions*"
	  (display-completion-list completions)))
Richard M. Stallman's avatar
Richard M. Stallman committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
      (set-buffer "*Completions*")
      (goto-char 1)
      (insert tmm-completion-prompt)
      )
    (save-excursion
      (other-window 1)			; Electric-pop-up-window does
					; not work in minibuffer
      (set-buffer (window-buffer (Electric-pop-up-window "*Completions*")))
      (setq tmm-old-comp-map (current-local-map))
      (use-local-map (append map (cdr tmm-old-comp-map)))
      (select-window win)		; Cannot use
					; save-window-excursion, since
					; it restores the size
      )
    (insert tmm-c-prompt)))

(defun tmm-delete-map ()
219
  (remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
220 221
  (if tmm-old-mb-map
      (use-local-map tmm-old-mb-map)))
Richard M. Stallman's avatar
Richard M. Stallman committed
222 223

(defun tmm-shortcut ()
224
  "Choose the shortcut that the user typed."
Richard M. Stallman's avatar
Richard M. Stallman committed
225 226 227
  (interactive)
  (let ((c (upcase (char-to-string last-command-char))) s)
    (if (member c tmm-short-cuts)
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	(if (equal (buffer-name) "*Completions*")
	    (progn
	      (beginning-of-buffer)
	      (re-search-forward
	       (concat "\\(^\\|[ \t]\\)" c tmm-mid-prompt))
	      (choose-completion))
	  (erase-buffer)		; In minibuffer
	  (mapcar (lambda (elt)
		    (if (string=
			 (substring (car elt) 0 
				    (min (1+ (length tmm-mid-prompt))
					 (length (car elt))))
			 (concat c tmm-mid-prompt))
			(setq s (car elt))))
		  tmm-km-list)
	  (insert s)
	  (exit-minibuffer)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
245 246 247 248 249

(defun tmm-goto-completions ()
  (interactive)
  (setq tmm-c-prompt (buffer-string))
  (erase-buffer)
250
  (switch-to-buffer-other-window "*Completions*")
Richard M. Stallman's avatar
Richard M. Stallman committed
251 252 253 254 255 256 257
  (search-forward tmm-c-prompt)
  (search-backward tmm-c-prompt))


(defun tmm-get-keymap (elt &optional in-x-menu) 
  "Prepends (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
The values are deduced from the argument ELT, that should be an
258
element of keymap, an `x-popup-menu' argument, or an element of
Richard M. Stallman's avatar
Richard M. Stallman committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
`x-popup-menu' argument (when IN-X-MENU is not-nil).
Does it only if it is not already there. Uses free variable 
`tmm-table-undef' to keep undefined keys."
  (let (km str cache (event (car elt)))
    (setq elt (cdr elt))
    (if (eq elt 'undefined)
	(setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
      (or
       (assoc event tmm-table-undef)
       (and (if (listp elt)
		(keymapp elt)
	      (fboundp elt))
	    (setq km elt))
       (and (if (listp (cdr-safe elt))
		(keymapp (cdr-safe elt))
	      (fboundp (cdr-safe elt)))
	    (setq km (cdr elt))
	    (and (stringp (car elt)) (setq str (car elt))))
       (and (if (listp (cdr-safe (cdr-safe elt)))
		(keymapp (cdr-safe (cdr-safe elt)))
	      (fboundp (cdr-safe (cdr-safe elt))))
	    (setq km (cdr (cdr elt)))
	    (and (stringp (car elt)) (setq str (car elt)))
	    (or (and str
		     (stringp (cdr (car (cdr elt)))) ; keyseq cache
		     (setq cache (cdr (car (cdr elt))))
		     cache (setq str (concat str cache))) str))
       (and (if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
		(keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
	      (fboundp (cdr-safe (cdr-safe (cdr-safe elt)))))
					; New style of easy-menu
	    (setq km (cdr (cdr (cdr elt))))
	    (and (stringp (car elt)) (setq str (car elt)))
	    (or (and str
		     (stringp (cdr (car (cdr (cdr elt))))) ; keyseq cache
		     (setq cache (cdr (car (cdr (cdr elt)))))
		     cache (setq str (concat str cache)))
		str))
	    (and (stringp event)	; x-popup or x-popup element
		 (if (or in-x-menu (stringp (car-safe elt)))
		     (setq str event event nil km elt)
		   (setq str event event nil km (cons 'keymap elt))
		   )))
      (and km (stringp km) (setq str km))
      (and km str
	   (or (assoc str tmm-km-list)
	       (setq tmm-km-list 
		     (cons (cons str (cons event km)) tmm-km-list)))
	   ))))


(defun tmm-get-keybind (keyseq)
311 312 313 314 315
  "Return the current binding of KEYSEQ, merging prefix definitions.
If KEYSEQ is a prefix key that has local and gloibal bindings,
we merge them into a single keymap which shows the proper order of the menu.
However, for the menu bar itself, the value does not take account
of `menu-bar-final-items'."
Richard M. Stallman's avatar
Richard M. Stallman committed
316
  (let (allbind bind)
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    (setq bind (key-binding keyseq))
    ;; If KEYSEQ is a prefix key, then BIND is either nil
    ;; or a symbol defined as a keymap (which satisfies keymapp).
    (if (keymapp bind)
	(setq bind nil))
    ;; If we have a non-keymap definition, return that.
    (or bind
	(progn
	  ;; Otherwise, it is a prefix, so make a list of the subcommands.
	  ;; Make a list of all the bindings in all the keymaps.
	  (setq allbind (mapcar 'cdr (minor-mode-key-binding keyseq))) 
	  (setq allbind (cons (local-key-binding keyseq) allbind))
	  (setq allbind (cons (global-key-binding keyseq) allbind))
	  ;; Merge all the elements of ALLBIND into one keymap.
	  (mapcar (lambda (in)
		    (if (and (symbolp in) (keymapp in))
			(setq in (symbol-function in)))
		    (and in (keymapp in)
			 (if (keymapp bind)
			     (setq bind (nconc bind (copy-sequence (cdr in))))
			   (setq bind (copy-sequence in)))))
		  allbind)
	  ;; Return that keymap.
	  bind))))
Richard M. Stallman's avatar
Richard M. Stallman committed
341 342 343 344 345 346 347 348

(add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))


(provide 'tmm)


;;; tmm.el ends here