1995-10-30 17:35:01 +00:00
|
|
|
;;; tmm.el --- text mode access to menu-bar
|
1995-03-11 03:57:25 +00:00
|
|
|
|
2012-01-05 09:46:05 +00:00
|
|
|
;; Copyright (C) 1994-1996, 2000-2012 Free Software Foundation, Inc.
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
;; Author: Ilya Zakharevich <ilya@math.mps.ohio-state.edu>
|
1995-10-30 16:33:49 +00:00
|
|
|
;; Maintainer: FSF
|
2002-01-27 11:17:06 +00:00
|
|
|
;; Keywords: convenience
|
1995-03-11 03:57:25 +00:00
|
|
|
|
1995-03-11 03:58:31 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
1995-03-11 03:57:25 +00:00
|
|
|
|
2008-05-06 08:06:51 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1995-03-11 03:57:25 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 08:06:51 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
;; 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
|
2008-05-06 08:06:51 +00:00
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
1995-03-11 03:57:25 +00:00
|
|
|
|
1996-01-14 07:34:30 +00:00
|
|
|
;;; Commentary:
|
1995-03-11 03:57:25 +00:00
|
|
|
|
2002-01-27 11:17:06 +00:00
|
|
|
;; This package provides text mode access to the menu bar.
|
1995-03-11 03:57:25 +00:00
|
|
|
|
1996-01-14 07:34:30 +00:00
|
|
|
;;; Code:
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
(require 'electric)
|
|
|
|
|
1998-03-07 18:19:38 +00:00
|
|
|
(defgroup tmm nil
|
|
|
|
"Text mode access to menu-bar."
|
|
|
|
:prefix "tmm-"
|
|
|
|
:group 'menu)
|
|
|
|
|
1995-03-11 03:57:25 +00:00
|
|
|
;;; The following will be localized, added only to pacify the compiler.
|
|
|
|
(defvar tmm-short-cuts)
|
1995-10-30 16:33:49 +00:00
|
|
|
(defvar tmm-old-mb-map nil)
|
2007-04-03 01:07:41 +00:00
|
|
|
(defvar tmm-c-prompt nil)
|
1995-03-11 03:57:25 +00:00
|
|
|
(defvar tmm-km-list)
|
1996-09-01 19:47:48 +00:00
|
|
|
(defvar tmm-next-shortcut-digit)
|
1995-03-11 03:57:25 +00:00
|
|
|
(defvar tmm-table-undef)
|
|
|
|
|
1995-08-28 00:15:59 +00:00
|
|
|
;;;###autoload (define-key global-map "\M-`" 'tmm-menubar)
|
1996-01-02 05:59:20 +00:00
|
|
|
;;;###autoload (define-key global-map [menu-bar mouse-1] 'tmm-menubar-mouse)
|
1995-08-03 17:39:53 +00:00
|
|
|
|
1995-03-11 03:57:25 +00:00
|
|
|
;;;###autoload
|
1996-01-02 05:59:20 +00:00
|
|
|
(defun tmm-menubar (&optional x-position)
|
1995-03-11 03:57:25 +00:00
|
|
|
"Text-mode emulation of looking and choosing from a menubar.
|
1996-01-02 05:59:20 +00:00
|
|
|
See the documentation for `tmm-prompt'.
|
|
|
|
X-POSITION, if non-nil, specifies a horizontal position within the menu bar;
|
|
|
|
we make that menu bar item (the one at that position) the default choice."
|
1995-03-11 03:57:25 +00:00
|
|
|
(interactive)
|
|
|
|
(run-hooks 'menu-bar-update-hook)
|
1995-10-30 16:33:49 +00:00
|
|
|
;; Obey menu-bar-final-items; put those items last.
|
1996-01-02 05:59:20 +00:00
|
|
|
(let ((menu-bar (tmm-get-keybind [menu-bar]))
|
|
|
|
menu-bar-item)
|
1995-10-30 16:33:49 +00:00
|
|
|
(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))))
|
1996-01-02 05:59:20 +00:00
|
|
|
(if x-position
|
2007-04-03 01:07:41 +00:00
|
|
|
(let ((tail menu-bar) (column 0)
|
2007-04-03 10:09:45 +00:00
|
|
|
this-one name visible)
|
2007-03-30 06:11:38 +00:00
|
|
|
(while (and tail (<= column x-position))
|
1996-01-02 05:59:20 +00:00
|
|
|
(setq this-one (car tail))
|
2007-03-30 06:11:38 +00:00
|
|
|
(if (and (consp this-one)
|
|
|
|
(consp (cdr this-one))
|
2007-04-03 10:09:45 +00:00
|
|
|
(setq name ;simple menu
|
2007-04-03 01:07:41 +00:00
|
|
|
(cond ((stringp (nth 1 this-one))
|
|
|
|
(nth 1 this-one))
|
2007-04-03 10:09:45 +00:00
|
|
|
;extended menu
|
|
|
|
((stringp (nth 2 this-one))
|
|
|
|
(setq visible (plist-get
|
|
|
|
(nthcdr 4 this-one) :visible))
|
|
|
|
(unless (and visible (not (eval visible)))
|
|
|
|
(nth 2 this-one))))))
|
2007-04-03 01:07:41 +00:00
|
|
|
(setq column (+ column (length name) 1)))
|
1996-01-02 05:59:20 +00:00
|
|
|
(setq tail (cdr tail)))
|
|
|
|
(setq menu-bar-item (car this-one))))
|
|
|
|
(tmm-prompt menu-bar nil menu-bar-item)))
|
|
|
|
|
1996-01-09 23:20:11 +00:00
|
|
|
;;;###autoload
|
1996-01-02 05:59:20 +00:00
|
|
|
(defun tmm-menubar-mouse (event)
|
|
|
|
"Text-mode emulation of looking and choosing from a menubar.
|
|
|
|
This command is used when you click the mouse in the menubar
|
|
|
|
on a console which has no window system but does have a mouse.
|
|
|
|
See the documentation for `tmm-prompt'."
|
|
|
|
(interactive "e")
|
|
|
|
(tmm-menubar (car (posn-x-y (event-start event)))))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
1998-03-07 18:19:38 +00:00
|
|
|
(defcustom tmm-mid-prompt "==>"
|
2007-09-10 03:46:53 +00:00
|
|
|
"String to insert between shortcut and menu item.
|
2011-11-16 02:26:00 +00:00
|
|
|
If nil, there will be no shortcuts. It should not consist only of spaces,
|
1998-03-07 18:19:38 +00:00
|
|
|
or else the correct item might not be found in the `*Completions*' buffer."
|
|
|
|
:type 'string
|
|
|
|
:group 'tmm)
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
(defvar tmm-mb-map nil
|
|
|
|
"A place to store minibuffer map.")
|
|
|
|
|
2002-01-27 11:17:06 +00:00
|
|
|
(defcustom tmm-completion-prompt
|
2002-04-22 15:30:18 +00:00
|
|
|
"Press PageUp key to reach this buffer from the minibuffer.
|
1995-03-11 03:57:25 +00:00
|
|
|
Alternatively, you can use Up/Down keys (or your History keys) to change
|
2002-01-27 11:17:06 +00:00
|
|
|
the item in the minibuffer, and press RET when you are done, or press the
|
1995-11-13 03:20:37 +00:00
|
|
|
marked letters to pick up your choice. Type C-g or ESC ESC ESC to cancel.
|
1995-03-11 03:57:25 +00:00
|
|
|
"
|
2007-09-10 03:46:53 +00:00
|
|
|
"Help text to insert on the top of the completion buffer.
|
1996-09-01 19:47:48 +00:00
|
|
|
To save space, you can set this to nil,
|
1998-03-07 18:19:38 +00:00
|
|
|
in which case the standard introduction text is deleted too."
|
|
|
|
:type '(choice string (const nil))
|
|
|
|
:group 'tmm)
|
1996-09-01 19:47:48 +00:00
|
|
|
|
1998-03-07 18:19:38 +00:00
|
|
|
(defcustom tmm-shortcut-style '(downcase upcase)
|
2007-09-10 03:46:53 +00:00
|
|
|
"What letters to use as menu shortcuts.
|
2002-01-27 11:17:06 +00:00
|
|
|
Must be either one of the symbols `downcase' or `upcase',
|
1998-03-07 18:19:38 +00:00
|
|
|
or else a list of the two in the order you prefer."
|
|
|
|
:type '(choice (const downcase)
|
|
|
|
(const upcase)
|
|
|
|
(repeat (choice (const downcase) (const upcase))))
|
|
|
|
:group 'tmm)
|
1996-09-01 19:47:48 +00:00
|
|
|
|
1998-03-07 18:19:38 +00:00
|
|
|
(defcustom tmm-shortcut-words 2
|
2007-09-10 03:46:53 +00:00
|
|
|
"How many successive words to try for shortcuts, nil means all.
|
2002-01-27 11:17:06 +00:00
|
|
|
If you use only one of `downcase' or `upcase' for `tmm-shortcut-style',
|
1998-03-07 18:19:38 +00:00
|
|
|
specify nil for this variable."
|
|
|
|
:type '(choice integer (const nil))
|
|
|
|
:group 'tmm)
|
1995-03-11 03:57:25 +00:00
|
|
|
|
2005-06-06 16:28:26 +00:00
|
|
|
(defface tmm-inactive
|
2005-06-06 12:24:51 +00:00
|
|
|
'((t :inherit shadow))
|
2005-06-03 11:24:06 +00:00
|
|
|
"Face used for inactive menu items."
|
|
|
|
:group 'tmm)
|
|
|
|
|
1995-03-11 03:57:25 +00:00
|
|
|
;;;###autoload
|
1996-01-02 06:35:43 +00:00
|
|
|
(defun tmm-prompt (menu &optional in-popup default-item)
|
1995-03-11 03:57:25 +00:00
|
|
|
"Text-mode emulation of calling the bindings in keymap.
|
1996-01-02 05:59:20 +00:00
|
|
|
Creates a text-mode menu of possible choices. You can access the elements
|
|
|
|
in the menu in two ways:
|
|
|
|
*) via history mechanism from minibuffer;
|
1995-03-11 03:57:25 +00:00
|
|
|
*) Or via completion-buffer that is automatically shown.
|
|
|
|
The last alternative is currently a hack, you cannot use mouse reliably.
|
1996-01-02 06:35:43 +00:00
|
|
|
|
|
|
|
MENU is like the MENU argument to `x-popup-menu': either a
|
|
|
|
keymap or an alist of alists.
|
|
|
|
DEFAULT-ITEM, if non-nil, specifies an initial default choice.
|
|
|
|
Its value should be an event that has a binding in MENU."
|
|
|
|
;; If the optional argument IN-POPUP is t,
|
|
|
|
;; then MENU is an alist of elements of the form (STRING . VALUE).
|
|
|
|
;; That is used for recursive calls only.
|
|
|
|
(let ((gl-str "Menu bar") ;; The menu bar itself is not a menu keymap
|
|
|
|
; so it doesn't have a name.
|
|
|
|
tmm-km-list out history history-len tmm-table-undef tmm-c-prompt
|
2011-11-16 02:26:00 +00:00
|
|
|
tmm-old-mb-map tmm-short-cuts
|
1996-01-02 06:35:43 +00:00
|
|
|
chosen-string choice
|
|
|
|
(not-menu (not (keymapp menu))))
|
1995-03-11 03:57:25 +00:00
|
|
|
(run-hooks 'activate-menubar-hook)
|
1996-01-02 06:35:43 +00:00
|
|
|
;; Compute tmm-km-list from MENU.
|
|
|
|
;; tmm-km-list is an alist of (STRING . MEANING).
|
|
|
|
;; It has no other elements.
|
|
|
|
;; The order of elements in tmm-km-list is the order of the menu bar.
|
2000-09-17 19:44:42 +00:00
|
|
|
(mapc (lambda (elt)
|
2011-01-15 20:03:38 +00:00
|
|
|
(cond
|
|
|
|
((stringp elt) (setq gl-str elt))
|
|
|
|
((listp elt) (tmm-get-keymap elt not-menu))
|
|
|
|
((vectorp elt)
|
|
|
|
(dotimes (i (length elt))
|
|
|
|
(tmm-get-keymap (cons i (aref elt i)) not-menu)))))
|
|
|
|
menu)
|
1996-01-02 06:35:43 +00:00
|
|
|
;; Choose an element of tmm-km-list; put it in choice.
|
|
|
|
(if (and not-menu (= 1 (length tmm-km-list)))
|
|
|
|
;; If this is the top-level of an x-popup-menu menu,
|
|
|
|
;; and there is just one pane, choose that one silently.
|
|
|
|
;; This way we only ask the user one question,
|
|
|
|
;; for which element of that pane.
|
|
|
|
(setq choice (cdr (car tmm-km-list)))
|
1998-04-21 04:51:23 +00:00
|
|
|
(unless tmm-km-list
|
|
|
|
(error "Empty menu reached"))
|
1996-01-02 06:35:43 +00:00
|
|
|
(and tmm-km-list
|
|
|
|
(let ((index-of-default 0))
|
|
|
|
(if tmm-mid-prompt
|
|
|
|
(setq tmm-km-list (tmm-add-shortcuts tmm-km-list))
|
|
|
|
t)
|
|
|
|
;; Find the default item's index within the menu bar.
|
|
|
|
;; We use this to decide the initial minibuffer contents
|
|
|
|
;; and initial history position.
|
|
|
|
(if default-item
|
2007-04-03 10:09:45 +00:00
|
|
|
(let ((tail menu) visible)
|
1996-01-02 06:35:43 +00:00
|
|
|
(while (and tail
|
|
|
|
(not (eq (car-safe (car tail)) default-item)))
|
|
|
|
;; Be careful to count only the elements of MENU
|
|
|
|
;; that actually constitute menu bar items.
|
|
|
|
(if (and (consp (car tail))
|
2000-05-29 15:50:01 +00:00
|
|
|
(or (stringp (car-safe (cdr (car tail))))
|
2007-04-03 10:09:45 +00:00
|
|
|
(and
|
|
|
|
(eq (car-safe (cdr (car tail))) 'menu-item)
|
|
|
|
(progn
|
|
|
|
(setq visible
|
|
|
|
(plist-get
|
|
|
|
(nthcdr 4 (car tail)) :visible))
|
|
|
|
(or (not visible) (eval visible))))))
|
1996-01-02 06:35:43 +00:00
|
|
|
(setq index-of-default (1+ index-of-default)))
|
|
|
|
(setq tail (cdr tail)))))
|
2005-06-03 11:24:06 +00:00
|
|
|
(let ((prompt (concat "^." (regexp-quote tmm-mid-prompt))))
|
|
|
|
(setq history
|
|
|
|
(reverse (delq nil
|
|
|
|
(mapcar
|
|
|
|
(lambda (elt)
|
|
|
|
(if (string-match prompt (car elt))
|
|
|
|
(car elt)))
|
|
|
|
tmm-km-list)))))
|
1996-01-02 06:35:43 +00:00
|
|
|
(setq history-len (length history))
|
|
|
|
(setq history (append history history history history))
|
|
|
|
(setq tmm-c-prompt (nth (- history-len 1 index-of-default) history))
|
2011-11-16 02:26:00 +00:00
|
|
|
(setq out
|
|
|
|
(if default-item
|
|
|
|
(car (nth index-of-default tmm-km-list))
|
|
|
|
(minibuffer-with-setup-hook #'tmm-add-prompt
|
|
|
|
(completing-read
|
|
|
|
(concat gl-str
|
|
|
|
" (up/down to change, PgUp to menu): ")
|
|
|
|
tmm-km-list nil t nil
|
|
|
|
(cons 'history
|
|
|
|
(- (* 2 history-len) index-of-default))))))))
|
1996-01-02 06:35:43 +00:00
|
|
|
(setq choice (cdr (assoc out tmm-km-list)))
|
|
|
|
(and (null choice)
|
|
|
|
(> (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))
|
|
|
|
choice (cdr (assoc out tmm-km-list))))
|
1998-04-21 04:51:23 +00:00
|
|
|
(and (null choice) out
|
1996-01-02 06:35:43 +00:00
|
|
|
(setq out (try-completion out tmm-km-list)
|
|
|
|
choice (cdr (assoc out tmm-km-list)))))
|
|
|
|
;; CHOICE is now (STRING . MEANING). Separate the two parts.
|
|
|
|
(setq chosen-string (car choice))
|
|
|
|
(setq choice (cdr choice))
|
|
|
|
(cond (in-popup
|
|
|
|
;; We just did the inner level of a -popup menu.
|
|
|
|
choice)
|
|
|
|
;; We just did the outer level. Do the inner level now.
|
2002-01-27 11:17:06 +00:00
|
|
|
(not-menu (tmm-prompt choice t))
|
1996-01-02 06:35:43 +00:00
|
|
|
;; We just handled a menu keymap and found another keymap.
|
|
|
|
((keymapp choice)
|
|
|
|
(if (symbolp choice)
|
|
|
|
(setq choice (indirect-function choice)))
|
|
|
|
(condition-case nil
|
|
|
|
(require 'mouse)
|
|
|
|
(error nil))
|
|
|
|
(tmm-prompt choice))
|
|
|
|
;; We just handled a menu keymap and found a command.
|
|
|
|
(choice
|
|
|
|
(if chosen-string
|
1996-01-24 21:59:32 +00:00
|
|
|
(progn
|
|
|
|
(setq last-command-event chosen-string)
|
|
|
|
(call-interactively choice))
|
1996-01-02 06:35:43 +00:00
|
|
|
choice)))))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
(defun tmm-add-shortcuts (list)
|
2011-11-16 02:26:00 +00:00
|
|
|
"Add shortcuts to cars of elements of the list.
|
1995-03-11 03:57:25 +00:00
|
|
|
Takes a list of lists with a string as car, returns list with
|
1995-10-30 16:33:49 +00:00
|
|
|
shortcuts added to these cars.
|
|
|
|
Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
|
1996-09-01 19:47:48 +00:00
|
|
|
(let ((tmm-next-shortcut-digit ?0))
|
|
|
|
(mapcar 'tmm-add-one-shortcut (reverse list))))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
1996-09-01 19:47:48 +00:00
|
|
|
(defsubst tmm-add-one-shortcut (elt)
|
|
|
|
;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
|
2005-06-03 11:24:06 +00:00
|
|
|
(cond
|
|
|
|
((eq (cddr elt) 'ignore)
|
|
|
|
(cons (concat " " (make-string (length tmm-mid-prompt) ?\-)
|
|
|
|
(car elt))
|
|
|
|
(cdr elt)))
|
|
|
|
(t
|
|
|
|
(let* ((str (car elt))
|
|
|
|
(paren (string-match "(" str))
|
|
|
|
(pos 0) (word 0) char)
|
|
|
|
(catch 'done ; ??? is this slow?
|
|
|
|
(while (and (or (not tmm-shortcut-words) ; no limit on words
|
|
|
|
(< word tmm-shortcut-words)) ; try n words
|
|
|
|
(setq pos (string-match "\\w+" str pos)) ; get next word
|
|
|
|
(not (and paren (> pos paren)))) ; don't go past "(binding.."
|
|
|
|
(if (or (= pos 0)
|
|
|
|
(/= (aref str (1- pos)) ?.)) ; avoid file extensions
|
|
|
|
(let ((shortcut-style
|
|
|
|
(if (listp tmm-shortcut-style) ; convert to list
|
|
|
|
tmm-shortcut-style
|
|
|
|
(list tmm-shortcut-style))))
|
|
|
|
(while shortcut-style ; try upcase and downcase variants
|
|
|
|
(setq char (funcall (car shortcut-style) (aref str pos)))
|
|
|
|
(if (not (memq char tmm-short-cuts)) (throw 'done char))
|
|
|
|
(setq shortcut-style (cdr shortcut-style)))))
|
|
|
|
(setq word (1+ word))
|
|
|
|
(setq pos (match-end 0)))
|
|
|
|
(while (<= tmm-next-shortcut-digit ?9) ; no letter shortcut, pick a digit
|
|
|
|
(setq char tmm-next-shortcut-digit)
|
|
|
|
(setq tmm-next-shortcut-digit (1+ tmm-next-shortcut-digit))
|
|
|
|
(if (not (memq char tmm-short-cuts)) (throw 'done char)))
|
|
|
|
(setq char nil))
|
|
|
|
(if char (setq tmm-short-cuts (cons char tmm-short-cuts)))
|
|
|
|
(cons (concat (if char (concat (char-to-string char) tmm-mid-prompt)
|
|
|
|
;; keep them lined up in columns
|
2006-11-27 14:12:34 +00:00
|
|
|
(make-string (1+ (length tmm-mid-prompt)) ?\s))
|
2005-06-03 11:24:06 +00:00
|
|
|
str)
|
|
|
|
(cdr elt))))))
|
1996-09-01 19:47:48 +00:00
|
|
|
|
|
|
|
;; This returns the old map.
|
1996-03-28 18:16:43 +00:00
|
|
|
(defun tmm-define-keys (minibuffer)
|
1996-09-01 19:47:48 +00:00
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(suppress-keymap map t)
|
2000-09-17 19:44:42 +00:00
|
|
|
(mapc
|
|
|
|
(lambda (c)
|
|
|
|
(if (listp tmm-shortcut-style)
|
|
|
|
(define-key map (char-to-string c) 'tmm-shortcut)
|
|
|
|
;; only one kind of letters are shortcuts, so map both upcase and
|
|
|
|
;; downcase input to the same
|
|
|
|
(define-key map (char-to-string (downcase c)) 'tmm-shortcut)
|
|
|
|
(define-key map (char-to-string (upcase c)) 'tmm-shortcut)))
|
1996-09-01 19:47:48 +00:00
|
|
|
tmm-short-cuts)
|
|
|
|
(if minibuffer
|
|
|
|
(progn
|
|
|
|
(define-key map [pageup] 'tmm-goto-completions)
|
|
|
|
(define-key map [prior] 'tmm-goto-completions)
|
|
|
|
(define-key map "\ev" 'tmm-goto-completions)
|
|
|
|
(define-key map "\C-n" 'next-history-element)
|
|
|
|
(define-key map "\C-p" 'previous-history-element)))
|
|
|
|
(prog1 (current-local-map)
|
|
|
|
(use-local-map (append map (current-local-map))))))
|
|
|
|
|
|
|
|
(defun tmm-completion-delete-prompt ()
|
|
|
|
(set-buffer standard-output)
|
2008-04-14 18:13:16 +00:00
|
|
|
(goto-char (point-min))
|
|
|
|
(delete-region (point) (search-forward "Possible completions are:\n")))
|
1995-11-10 17:29:36 +00:00
|
|
|
|
2005-06-03 11:24:06 +00:00
|
|
|
(defun tmm-remove-inactive-mouse-face ()
|
|
|
|
"Remove the mouse-face property from inactive menu items."
|
|
|
|
(let ((inhibit-read-only t)
|
|
|
|
(inactive-string
|
|
|
|
(concat " " (make-string (length tmm-mid-prompt) ?\-)))
|
|
|
|
next)
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (not (eobp))
|
|
|
|
(setq next (next-single-char-property-change (point) 'mouse-face))
|
|
|
|
(when (looking-at inactive-string)
|
|
|
|
(remove-text-properties (point) next '(mouse-face))
|
2005-06-06 16:28:26 +00:00
|
|
|
(add-text-properties (point) next '(face tmm-inactive)))
|
2005-06-03 11:24:06 +00:00
|
|
|
(goto-char next)))
|
|
|
|
(set-buffer-modified-p nil)))
|
|
|
|
|
1995-03-11 03:57:25 +00:00
|
|
|
(defun tmm-add-prompt ()
|
1995-08-03 17:39:53 +00:00
|
|
|
(add-hook 'minibuffer-exit-hook 'tmm-delete-map nil t)
|
2005-06-03 11:24:06 +00:00
|
|
|
(unless tmm-c-prompt
|
|
|
|
(error "No active menu entries"))
|
2011-01-15 20:03:38 +00:00
|
|
|
(setq tmm-old-mb-map (tmm-define-keys t))
|
|
|
|
;; Get window and hide it for electric mode to get correct size
|
|
|
|
(save-window-excursion
|
|
|
|
(let ((completions
|
|
|
|
(mapcar 'car minibuffer-completion-table)))
|
|
|
|
(or tmm-completion-prompt
|
|
|
|
(add-hook 'completion-setup-hook
|
|
|
|
'tmm-completion-delete-prompt 'append))
|
|
|
|
(unwind-protect
|
|
|
|
(with-output-to-temp-buffer "*Completions*"
|
|
|
|
(display-completion-list completions))
|
|
|
|
(remove-hook 'completion-setup-hook 'tmm-completion-delete-prompt)))
|
|
|
|
(set-buffer "*Completions*")
|
|
|
|
(tmm-remove-inactive-mouse-face)
|
|
|
|
(when tmm-completion-prompt
|
|
|
|
(let ((buffer-read-only nil))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(insert tmm-completion-prompt))))
|
|
|
|
(save-selected-window
|
|
|
|
(other-window 1) ; Electric-pop-up-window does
|
1995-03-11 03:57:25 +00:00
|
|
|
; not work in minibuffer
|
2011-11-16 02:26:00 +00:00
|
|
|
(Electric-pop-up-window "*Completions*"))
|
2011-01-15 20:03:38 +00:00
|
|
|
(insert tmm-c-prompt))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
(defun tmm-delete-map ()
|
1995-08-03 17:39:53 +00:00
|
|
|
(remove-hook 'minibuffer-exit-hook 'tmm-delete-map t)
|
1995-10-30 16:33:49 +00:00
|
|
|
(if tmm-old-mb-map
|
|
|
|
(use-local-map tmm-old-mb-map)))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
(defun tmm-shortcut ()
|
1995-10-30 16:33:49 +00:00
|
|
|
"Choose the shortcut that the user typed."
|
1995-03-11 03:57:25 +00:00
|
|
|
(interactive)
|
2009-01-09 04:44:15 +00:00
|
|
|
(let ((c last-command-event) s)
|
1996-09-01 19:47:48 +00:00
|
|
|
(if (symbolp tmm-shortcut-style)
|
|
|
|
(setq c (funcall tmm-shortcut-style c)))
|
|
|
|
(if (memq c tmm-short-cuts)
|
1995-10-30 16:33:49 +00:00
|
|
|
(if (equal (buffer-name) "*Completions*")
|
|
|
|
(progn
|
2005-05-21 04:50:59 +00:00
|
|
|
(goto-char (point-min))
|
1995-10-30 16:33:49 +00:00
|
|
|
(re-search-forward
|
1996-09-01 19:47:48 +00:00
|
|
|
(concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
|
1995-10-30 16:33:49 +00:00
|
|
|
(choose-completion))
|
2000-01-11 22:04:24 +00:00
|
|
|
;; In minibuffer
|
|
|
|
(delete-region (minibuffer-prompt-end) (point-max))
|
2000-09-17 19:44:42 +00:00
|
|
|
(mapc (lambda (elt)
|
|
|
|
(if (string=
|
2002-01-27 11:17:06 +00:00
|
|
|
(substring (car elt) 0
|
2000-09-17 19:44:42 +00:00
|
|
|
(min (1+ (length tmm-mid-prompt))
|
|
|
|
(length (car elt))))
|
|
|
|
(concat (char-to-string c) tmm-mid-prompt))
|
|
|
|
(setq s (car elt))))
|
1995-10-30 16:33:49 +00:00
|
|
|
tmm-km-list)
|
|
|
|
(insert s)
|
|
|
|
(exit-minibuffer)))))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
(defun tmm-goto-completions ()
|
2011-11-16 02:26:00 +00:00
|
|
|
"Jump to the completions buffer."
|
1995-03-11 03:57:25 +00:00
|
|
|
(interactive)
|
2000-01-17 15:32:56 +00:00
|
|
|
(let ((prompt-end (minibuffer-prompt-end)))
|
|
|
|
(setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
|
2011-11-16 02:26:00 +00:00
|
|
|
;; FIXME: Why?
|
2000-01-17 15:32:56 +00:00
|
|
|
(delete-region prompt-end (point-max)))
|
1995-10-30 16:33:49 +00:00
|
|
|
(switch-to-buffer-other-window "*Completions*")
|
1995-03-11 03:57:25 +00:00
|
|
|
(search-forward tmm-c-prompt)
|
|
|
|
(search-backward tmm-c-prompt))
|
|
|
|
|
2002-01-27 11:17:06 +00:00
|
|
|
(defun tmm-get-keymap (elt &optional in-x-menu)
|
2011-11-16 02:26:00 +00:00
|
|
|
"Prepend (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.
|
1995-03-11 03:57:25 +00:00
|
|
|
The values are deduced from the argument ELT, that should be an
|
1995-10-30 16:33:49 +00:00
|
|
|
element of keymap, an `x-popup-menu' argument, or an element of
|
1995-03-11 03:57:25 +00:00
|
|
|
`x-popup-menu' argument (when IN-X-MENU is not-nil).
|
1996-01-02 05:59:20 +00:00
|
|
|
This function adds the element only if it is not already present.
|
|
|
|
It uses the free variable `tmm-table-undef' to keep undefined keys."
|
2010-05-11 20:07:12 +00:00
|
|
|
(let (km str plist filter visible enable (event (car elt)))
|
1995-03-11 03:57:25 +00:00
|
|
|
(setq elt (cdr elt))
|
|
|
|
(if (eq elt 'undefined)
|
|
|
|
(setq tmm-table-undef (cons (cons event nil) tmm-table-undef))
|
1998-04-13 18:55:05 +00:00
|
|
|
(unless (assoc event tmm-table-undef)
|
|
|
|
(cond ((if (listp elt)
|
|
|
|
(or (keymapp elt) (eq (car elt) 'lambda))
|
2010-05-11 20:07:12 +00:00
|
|
|
(and (symbolp elt) (fboundp elt)))
|
1998-04-13 18:55:05 +00:00
|
|
|
(setq km elt))
|
2001-05-18 13:10:43 +00:00
|
|
|
|
1998-04-13 18:55:05 +00:00
|
|
|
((if (listp (cdr-safe elt))
|
|
|
|
(or (keymapp (cdr-safe elt))
|
|
|
|
(eq (car (cdr-safe elt)) 'lambda))
|
2010-05-11 20:07:12 +00:00
|
|
|
(and (symbolp (cdr-safe elt)) (fboundp (cdr-safe elt))))
|
1998-04-13 18:55:05 +00:00
|
|
|
(setq km (cdr elt))
|
|
|
|
(and (stringp (car elt)) (setq str (car elt))))
|
2001-05-18 13:10:43 +00:00
|
|
|
|
1998-04-13 18:55:05 +00:00
|
|
|
((if (listp (cdr-safe (cdr-safe elt)))
|
|
|
|
(or (keymapp (cdr-safe (cdr-safe elt)))
|
|
|
|
(eq (car (cdr-safe (cdr-safe elt))) 'lambda))
|
2010-05-11 20:07:12 +00:00
|
|
|
(and (symbolp (cdr-safe (cdr-safe elt)))
|
|
|
|
(fboundp (cdr-safe (cdr-safe elt)))))
|
2008-10-29 20:57:03 +00:00
|
|
|
(setq km (cddr elt))
|
2010-05-11 20:07:12 +00:00
|
|
|
(and (stringp (car elt)) (setq str (car elt))))
|
2001-05-18 13:10:43 +00:00
|
|
|
|
1998-04-13 18:55:05 +00:00
|
|
|
((eq (car-safe elt) 'menu-item)
|
2001-05-18 13:10:43 +00:00
|
|
|
;; (menu-item TITLE COMMAND KEY ...)
|
1998-06-14 18:46:20 +00:00
|
|
|
(setq plist (cdr-safe (cdr-safe (cdr-safe elt))))
|
2001-05-18 13:10:43 +00:00
|
|
|
(when (consp (car-safe plist))
|
|
|
|
(setq plist (cdr-safe plist)))
|
1998-04-13 18:55:05 +00:00
|
|
|
(setq km (nth 2 elt))
|
2000-12-02 21:31:12 +00:00
|
|
|
(setq str (eval (nth 1 elt)))
|
1998-06-14 18:46:20 +00:00
|
|
|
(setq filter (plist-get plist :filter))
|
|
|
|
(if filter
|
|
|
|
(setq km (funcall filter km)))
|
2002-01-27 11:17:06 +00:00
|
|
|
(setq visible (plist-get plist :visible))
|
|
|
|
(if visible
|
|
|
|
(setq km (and (eval visible) km)))
|
2005-05-13 23:23:42 +00:00
|
|
|
(setq enable (plist-get plist :enable))
|
|
|
|
(if enable
|
2010-05-11 20:07:12 +00:00
|
|
|
(setq km (if (eval enable) km 'ignore))))
|
2001-05-18 13:10:43 +00:00
|
|
|
|
1998-04-13 18:55:05 +00:00
|
|
|
((if (listp (cdr-safe (cdr-safe (cdr-safe elt))))
|
|
|
|
(or (keymapp (cdr-safe (cdr-safe (cdr-safe elt))))
|
|
|
|
(eq (car (cdr-safe (cdr-safe (cdr-safe elt)))) 'lambda))
|
2010-05-11 20:07:12 +00:00
|
|
|
(and (symbolp (cdr-safe (cdr-safe (cdr-safe elt))))
|
|
|
|
(fboundp (cdr-safe (cdr-safe (cdr-safe elt))))))
|
1998-04-13 18:55:05 +00:00
|
|
|
; New style of easy-menu
|
2008-10-29 20:57:03 +00:00
|
|
|
(setq km (cdr (cddr elt)))
|
2010-05-11 20:07:12 +00:00
|
|
|
(and (stringp (car elt)) (setq str (car elt))))
|
2001-05-18 13:10:43 +00:00
|
|
|
|
1998-04-13 18:55:05 +00:00
|
|
|
((stringp event) ; x-popup or x-popup element
|
|
|
|
(if (or in-x-menu (stringp (car-safe elt)))
|
|
|
|
(setq str event event nil km elt)
|
2010-05-11 20:07:12 +00:00
|
|
|
(setq str event event nil km (cons 'keymap elt)))))
|
2011-01-15 20:03:38 +00:00
|
|
|
(unless (or (eq km 'ignore) (null str))
|
2010-05-11 20:07:12 +00:00
|
|
|
(let ((binding (where-is-internal km nil t)))
|
|
|
|
(when binding
|
|
|
|
(setq binding (key-description binding))
|
|
|
|
;; Try to align the keybindings.
|
|
|
|
(let ((colwidth (min 30 (- (/ (window-width) 2) 10))))
|
|
|
|
(setq str
|
|
|
|
(concat str
|
|
|
|
(make-string (max 2 (- colwidth
|
|
|
|
(string-width str)
|
|
|
|
(string-width binding)))
|
|
|
|
?\s)
|
|
|
|
binding)))))))
|
1995-03-11 03:57:25 +00:00
|
|
|
(and km (stringp km) (setq str km))
|
1997-08-24 04:00:25 +00:00
|
|
|
;; Verify that the command is enabled;
|
|
|
|
;; if not, don't mention it.
|
|
|
|
(when (and km (symbolp km) (get km 'menu-enable))
|
2005-06-03 11:24:06 +00:00
|
|
|
(setq km (if (eval (get km 'menu-enable)) km 'ignore)))
|
1995-03-11 03:57:25 +00:00
|
|
|
(and km str
|
|
|
|
(or (assoc str tmm-km-list)
|
2000-12-02 21:31:12 +00:00
|
|
|
(push (cons str (cons event km)) tmm-km-list))))))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
|
|
|
(defun tmm-get-keybind (keyseq)
|
1995-10-30 16:33:49 +00:00
|
|
|
"Return the current binding of KEYSEQ, merging prefix definitions.
|
1996-01-04 23:50:50 +00:00
|
|
|
If KEYSEQ is a prefix key that has local and global bindings,
|
1995-10-30 16:33:49 +00:00
|
|
|
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'."
|
2007-04-03 01:07:41 +00:00
|
|
|
(let (allbind bind minorbind localbind globalbind)
|
1995-10-30 16:33:49 +00:00
|
|
|
(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.
|
2007-04-03 01:07:41 +00:00
|
|
|
(setq minorbind (mapcar 'cdr (minor-mode-key-binding keyseq)))
|
|
|
|
(setq localbind (local-key-binding keyseq))
|
2007-04-14 12:34:35 +00:00
|
|
|
(setq globalbind (copy-sequence (cdr (global-key-binding keyseq))))
|
2007-04-03 01:07:41 +00:00
|
|
|
|
|
|
|
;; If items have been redefined/undefined locally, remove them from
|
|
|
|
;; the global list.
|
|
|
|
(dolist (minor minorbind)
|
|
|
|
(dolist (item (cdr minor))
|
2007-04-13 03:57:37 +00:00
|
|
|
(setq globalbind (assq-delete-all (car-safe item) globalbind))))
|
2007-04-03 01:07:41 +00:00
|
|
|
(dolist (item (cdr localbind))
|
2007-04-13 03:57:37 +00:00
|
|
|
(setq globalbind (assq-delete-all (car-safe item) globalbind)))
|
2007-04-03 01:07:41 +00:00
|
|
|
|
|
|
|
(setq globalbind (cons 'keymap globalbind))
|
|
|
|
(setq allbind (cons globalbind (cons localbind minorbind)))
|
|
|
|
|
1995-10-30 16:33:49 +00:00
|
|
|
;; Merge all the elements of ALLBIND into one keymap.
|
2000-09-17 19:44:42 +00:00
|
|
|
(mapc (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)))))
|
1995-10-30 16:33:49 +00:00
|
|
|
allbind)
|
|
|
|
;; Return that keymap.
|
|
|
|
bind))))
|
1995-03-11 03:57:25 +00:00
|
|
|
|
2007-09-10 03:46:53 +00:00
|
|
|
;; Huh? What's that about? --Stef
|
1995-03-11 03:57:25 +00:00
|
|
|
(add-hook 'calendar-load-hook (lambda () (require 'cal-menu)))
|
|
|
|
|
|
|
|
(provide 'tmm)
|
|
|
|
|
|
|
|
;;; tmm.el ends here
|