Subject: [PATCH] ; Promptly disable 'completion-in-region-mode'

From: Eshel Yaron <[email-masked]>
Date: Wed, 08 Nov 2023 18:53:08 +0100

Tags: patch

This patch addresses an issue that Juri (CC'd) observed in Bug#66948,
where `completion-at-point` leaves TAB bound to `completion-at-point`
even when it doesn't show the completions list.

To reproduce:
1. emacs -Q
2. In the *scratch* buffer, insert the following:
--8<---------------cut here---------------start------------->8---
(defun foo ()
bar
--8<---------------cut here---------------end--------------->8---
3. With point after "bar", press C-M-i to invoke `completion-at-point`.
   This shows a message saying "No match".  That's expected.
4. Press TAB.  This again shows a message saying "No match".
   This is unexpected, instead TAB should indent "bar" as usual.

>From 846659798143826351de01d10b69613ec1d16c9a Mon Sep 17 00:00:00 2001
From: Eshel Yaron <[email-masked]>
Date: Wed, 8 Nov 2023 18:27:03 +0100
Subject: [PATCH] ; Promptly disable 'completion-in-region-mode'

Avoid keeping 'completion-in-region-mode' on when exiting
'completion--in-region-1' if the *Completions* buffer isn't shown.
Otherwise, the bindings from 'completion-in-region-mode-map' linger,
notable shadowing other bindings for TAB.

* lisp/minibuffer.el (completion--in-region-1): Disable
'completion-in-region-mode' if the *Completions* buffer isn't visible.
---
 lisp/minibuffer.el | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index bf1e014319e..d84e92fc013 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1561,11 +1561,12 @@ completion--in-region-1
    (t (prog1 (pcase (completion--do-completion beg end)
                (#b000 nil)
                (_     t))
-        (when (and (eq completion-auto-select t)
-                   (window-live-p minibuffer-scroll-window)
-                   (eq t (frame-visible-p (window-frame minibuffer-scroll-window))))
-          ;; When the completion list window was displayed, select it.
-          (switch-to-completions))))))
+        (if (window-live-p minibuffer-scroll-window)
+            (and (eq completion-auto-select t)
+                 (eq t (frame-visible-p (window-frame minibuffer-scroll-window)))
+                 ;; When the completion list window was displayed, select it.
+                 (switch-to-completions))
+          (completion-in-region-mode -1))))))
 
 (defun completion--cache-all-sorted-completions (beg end comps)
   (add-hook 'after-change-functions
-- 
2.42.0