Subject: Re: bug#74166: [PATCH] Fix tmm "previous menu" shortcut

From: Manuel Giraud <[email-masked]>
Date: Sat, 02 Nov 2024 19:43:33 +0100

Juri Linkov <[email-masked]> writes:

>> +(defun tmm-clear-self-insert-and-exit ()
>> +  "Clear the minibuffer content before self insert and exit."
>> +  (interactive)
>> +  (delete-region (minibuffer-prompt-end) (point-max))
>> +  (self-insert-and-exit))
>
> There is a special function 'delete-minibuffer-contents'
> like is used e.g. in 'read-char-from-minibuffer-insert-char'
> and 'y-or-n-p-insert-y'.

Thanks.  Here is a new version that uses this.
>From c4f2e844b6c9008f2a176f95a20b0f3caf0b9710 Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Fri, 1 Nov 2024 19:27:31 +0100
Subject: [PATCH] Fix tmm "previous menu" shortcut

Bug#74166
* lisp/tmm.el (tmm-clear-self-insert-and-exit): New function to
clear the minibuffer content then call `self-insert-and-exit'.
(tmm-define-keys): Use it.
(tmm-goto-completions): Explain a why.
---
 lisp/tmm.el | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/lisp/tmm.el b/lisp/tmm.el
index 80991b246b6..c73eb8c637e 100644
--- a/lisp/tmm.el
+++ b/lisp/tmm.el
@@ -336,6 +336,12 @@ tmm-add-one-shortcut
                  str))
        (cdr elt))))))
 
+(defun tmm-clear-self-insert-and-exit ()
+  "Clear the minibuffer contents then self insert and exit."
+  (interactive)
+  (delete-minibuffer-contents)
+  (self-insert-and-exit))
+
 ;; This returns the old map.
 (defun tmm-define-keys (minibuffer)
   (let ((map (make-sparse-keymap)))
@@ -354,7 +360,7 @@ tmm-define-keys
       (define-key map "\C-n" 'next-history-element)
       (define-key map "\C-p" 'previous-history-element)
       ;; Previous menu shortcut (see `tmm-prompt').
-      (define-key map "^" 'self-insert-and-exit))
+      (define-key map "^" 'tmm-clear-self-insert-and-exit))
     (prog1 (current-local-map)
       (use-local-map (append map (current-local-map))))))
 
@@ -452,10 +458,10 @@ tmm-shortcut
 (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)))
+  (setq tmm-c-prompt (buffer-substring (minibuffer-prompt-end) (point-max)))
+  ;; Clear minibuffer old contents before using *Completions* buffer for
+  ;; selection.
+  (delete-minibuffer-contents)
   (switch-to-buffer-other-window "*Completions*")
   (search-forward tmm-c-prompt)
   (search-backward tmm-c-prompt))
-- 
2.47.0

-- 
Manuel Giraud