]> git.eshelyaron.com Git - emacs.git/commitdiff
tmm.el: Simplify
authorEshel Yaron <me@eshelyaron.com>
Mon, 29 Jul 2024 08:56:02 +0000 (10:56 +0200)
committerEshel Yaron <me@eshelyaron.com>
Mon, 29 Jul 2024 08:56:02 +0000 (10:56 +0200)
lisp/tmm.el

index b04afc4881a115b96276ffec2229b4f774d977e4..9f696c881e1527befbde35687740877678949f56 100644 (file)
@@ -37,7 +37,6 @@
 
 ;;; The following will be localized, added only to pacify the compiler.
 (defvar tmm-short-cuts)
-(defvar tmm-old-mb-map nil)
 (defvar tmm-c-prompt nil)
 (defvar tmm-km-list)
 (defvar tmm-next-shortcut-digit)
@@ -77,25 +76,13 @@ See the documentation for `tmm-prompt'."
 
 (defcustom tmm-mid-prompt "==>"
   "String to insert between shortcut and menu item.
-If nil, there will be no shortcuts.  It should not consist only of spaces,
-or else the correct item might not be found in the `*Completions*' buffer."
+It should not consist only of spaces.  If nil, don't use shortcuts."
   :type '(choice (const :tag "No shortcuts" nil)
                  string))
 
 (defvar tmm-mb-map nil
   "A place to store minibuffer map.")
 
-(defcustom 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
-marked letters to pick up your choice.  Type C-g or ESC ESC ESC to cancel.
-"
-  "Help text to insert on the top of the completion buffer.
-To save space, you can set this to nil,
-in which case the standard introduction text is deleted too."
-  :type '(choice string (const nil)))
-
 (defcustom tmm-shortcut-style '(downcase upcase)
   "What letters to use as menu shortcuts.
 Must be either one of the symbols `downcase' or `upcase',
@@ -140,7 +127,7 @@ instead of executing it."
   (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-len tmm-table-undef tmm-c-prompt
-       tmm-old-mb-map tmm-short-cuts
+       tmm-short-cuts
        chosen-string choice
        (not-menu (not (keymapp menu))))
     (run-hooks 'activate-menubar-hook)
@@ -211,7 +198,10 @@ instead of executing it."
              (setq out
                    (if default-item
                        (car (nth index-of-default tmm-km-list))
-                     (minibuffer-with-setup-hook #'tmm-add-prompt
+                     (minibuffer-with-setup-hook
+                         (lambda ()
+                           (tmm-define-keys)
+                           (minibuffer-completion-help))
                        ;; tmm-km-list is reversed, because history
                        ;; needs it in LIFO order.  But default list
                        ;; needs it in non-reverse order, so that the
@@ -219,9 +209,8 @@ instead of executing it."
                        ;; values in the order they are shown on
                        ;; the menu bar.  So pass the DEFAULT arg the
                        ;; reversed copy of the list.
-                       (completing-read-default
-                        (concat gl-str
-                                " (up/down to change, PgUp to menu): ")
+                       (completing-read
+                        (format-prompt gl-str nil)
                         (tmm--completion-table tmm-km-list) nil t nil
                         'tmm--history (reverse tmm--history)))))))
       (setq choice (cdr (assoc out tmm-km-list)))
@@ -302,8 +291,7 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
                     str)
             (cdr elt))))))
 
-;; This returns the old map.
-(defun tmm-define-keys (minibuffer)
+(defun tmm-define-keys ()
   (let ((map (make-sparse-keymap)))
     (suppress-keymap map t)
     (dolist (c tmm-short-cuts)
@@ -313,15 +301,7 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
         ;; 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)))
-    (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))))))
+    (use-local-map (append map (current-local-map)))))
 
 (defun tmm-completion-delete-prompt ()
   (with-current-buffer standard-output
@@ -358,64 +338,23 @@ Stores a list of all the shortcuts in the free variable `tmm-short-cuts'."
         (goto-char next)))
     (set-buffer-modified-p nil)))
 
-(defun tmm-add-prompt ()
-  (unless tmm-c-prompt
-    (error "No active menu entries"))
-  (setq tmm-old-mb-map (tmm-define-keys t))
-  (or tmm-completion-prompt
-      (add-hook 'completion-setup-hook
-                #'tmm-completion-delete-prompt 'append))
-  (unwind-protect
-      (minibuffer-completion-help)
-    (remove-hook 'completion-setup-hook #'tmm-completion-delete-prompt))
-  (with-current-buffer "*Completions*"
-    (tmm-remove-inactive-mouse-face)
-    (when tmm-completion-prompt
-      (let ((inhibit-read-only t)
-           (window (get-buffer-window "*Completions*")))
-       (goto-char (point-min))
-       (insert tmm-completion-prompt)
-       (when window
-         ;; Try to show everything just inserted and preserve height of
-         ;; *Completions* window.  This should fix a behavior described
-         ;; in Bug#1291.
-         (fit-window-to-buffer window nil nil nil nil t))))))
-
 (defun tmm-shortcut ()
   "Choose the shortcut that the user typed."
   (interactive)
   (let ((c last-command-event) s)
-    (if (symbolp tmm-shortcut-style)
-        (setq c (funcall tmm-shortcut-style c)))
-    (if (memq c tmm-short-cuts)
-       (if (equal (buffer-name) "*Completions*")
-           (progn
-             (goto-char (point-min))
-             (re-search-forward
-              (concat "\\(^\\|[ \t]\\)" (char-to-string c) tmm-mid-prompt))
-             (choose-completion))
-         ;; In minibuffer
-         (delete-region (minibuffer-prompt-end) (point-max))
-         (dolist (elt tmm-km-list)
-            (if (string=
-                 (substring (car elt) 0
-                            (min (1+ (length tmm-mid-prompt))
-                                 (length (car elt))))
-                 (concat (char-to-string c) tmm-mid-prompt))
-                (setq s (car elt))))
-         (insert s)
-         (exit-minibuffer)))))
-
-(defun tmm-goto-completions ()
-  "Jump to the completions buffer."
-  (interactive)
-  (let ((prompt-end (minibuffer-prompt-end)))
-    (setq tmm-c-prompt (buffer-substring prompt-end (point-max)))
-    ;; FIXME: Why?
-    (delete-region prompt-end (point-max)))
-  (switch-to-buffer-other-window "*Completions*")
-  (search-forward tmm-c-prompt)
-  (search-backward tmm-c-prompt))
+    (when (functionp tmm-shortcut-style)
+      (setq c (funcall tmm-shortcut-style c)))
+    (when (memq c tmm-short-cuts)
+      (delete-region (minibuffer-prompt-end) (point-max))
+      (dolist (elt tmm-km-list)
+        (if (string=
+             (substring (car elt) 0
+                        (min (1+ (length tmm-mid-prompt))
+                             (length (car elt))))
+             (concat (char-to-string c) tmm-mid-prompt))
+            (setq s (car elt))))
+      (insert s)
+      (exit-minibuffer))))
 
 (defun tmm-get-keymap (elt &optional in-x-menu)
   "Prepend (DOCSTRING EVENT BINDING) to free variable `tmm-km-list'.