master a1f5b8cf863: Don't immediately show *Completions* for icomplete-in-buffer

Sean Whitton <[email protected]> Mon, 3 Aug 2026 06:50:53 -0400 (EDT)
Newsgroups gmane.emacs.diffs
Message-ID <[email protected]>
branch: master
commit a1f5b8cf863778d867e6ef318cc493cae4ca76f4
Author: Sean Whitton <[email protected]>
Commit: Sean Whitton <[email protected]>

    Don't immediately show *Completions* for icomplete-in-buffer
    
    * lisp/minibuffer.el (completion--icomplete-in-buffer-p):
    New function.
    (completion--do-completion, completion--in-region-1):
    Use it (bug#81537).
    * etc/NEWS: Say that NEWS.30 advice-add isn't needed anymore.
    * lisp/icomplete.el (icomplete-in-buffer): Remove advice-add
    instructions.
---
 etc/NEWS           | 10 ++++++++++
 lisp/icomplete.el  | 10 +---------
 lisp/minibuffer.el | 34 +++++++++++++++++++++++-----------
 3 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 23b97971105..ead3f7fb72c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -97,6 +97,16 @@ You can restore the previous behavior by removing
 ---
 *** The variable 'completion-ignore-case' is now customizable.
 
+---
+*** 'icomplete-in-buffer' users need no longer advise 'completion-at-point'.
+The "*Completions*" buffer no longer pops up in addition to Icomplete's
+in-buffer display of completions, by default.
+Therefore, the instructions in the NEWS file for Emacs 30.1 to add
+
+    (advice-add 'completion-at-point :after #'minibuffer-hide-completions)
+
+to your initialization file no longer apply, and you may remove it.
+
 
 * Editing Changes in Emacs 32.1
 
diff --git a/lisp/icomplete.el b/lisp/icomplete.el
index 9a1202c30af..c8da0e9bf9b 100644
--- a/lisp/icomplete.el
+++ b/lisp/icomplete.el
@@ -154,15 +154,7 @@ See `icomplete-delay-completions-threshold'."
 (defcustom icomplete-in-buffer nil
   "If non-nil, use Icomplete when completing in buffers other than minibuffer.
 This affects commands like `completion-in-region', but not commands
-that use their own completions setup.
-
-If you would prefer to see only Icomplete's in-buffer display, but do
-not want the \"*Completions*\" buffer to pop up in those cases, add
-this advice to your init file:
-
-  (advice-add \\='completion-at-point
-              :after #\\='minibuffer-hide-completions)
-"
+that use their own completions setup."
   :type 'boolean)
 
 (defcustom icomplete-minibuffer-setup-hook nil
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 7ef6f1383ab..24156ded8b3 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -1581,6 +1581,9 @@ Calls `completion-boundaries' with STRING, COLLECTION, PRED, SUFFIX."
          (end (+ (length string) (cdr boundaries))))
     (>= start pos end)))
 
+(defun completion--icomplete-in-buffer-p ()
+  (eq (bound-and-true-p icomplete--in-region-buffer) (current-buffer)))
+
 (defun completion--do-completion (beg end &optional
                                       try-completion-function expect-exact)
   "Do the completion and return a summary of what happened.
@@ -1671,7 +1674,7 @@ when the buffer's text is already an exact match."
                   (when (and threshold
                              (not completed)
                              (not only-changed-boundaries))
-                   (completion-all-sorted-completions beg end))))
+                    (completion-all-sorted-completions beg end))))
             (completion--flush-all-sorted-completions)
             (cond
              ((and (consp (cdr comps)) ;; There's something to cycle.
@@ -1686,6 +1689,7 @@ when the buffer's text is already an exact match."
               (minibuffer-force-complete beg end))
              ((or completed only-changed-boundaries)
               (cond
+               ((completion--icomplete-in-buffer-p)) ; Bug#81537.
                ((pcase completion-auto-help
                   ('visible (minibuffer--completions-visible))
                   ('always t))
@@ -1699,14 +1703,16 @@ when the buffer's text is already an exact match."
                                           'exact 'unknown))))))
              ;; Show the completion table, if requested.
              ((not exact)
-	      (if (pcase completion-auto-help
-                    ('lazy (eq this-command last-command))
-                    (_ completion-auto-help))
+	      (if (if (or (eq completion-auto-help 'lazy)
+                          (completion--icomplete-in-buffer-p)) ; Bug#81537.
+                      (eq this-command last-command)
+                    completion-auto-help)
                   (minibuffer-completion-help beg end)
                 (completion--message "Next char not unique")))
              ;; If the last exact completion and this one were the same, it
              ;; means we've already given a "Complete, but not unique" message
-             ;; and the user's hit TAB again, so now we give him help.
+             ;; and the user's hit TAB again, so now we give him help
+             ;; (even if `completion--icomplete-in-buffer-p' is non-nil).
              (t
               (when (and (eq this-command last-command) completion-auto-help)
                 (minibuffer-completion-help beg end))
@@ -1774,12 +1780,18 @@ scroll the window of possible completions."
    (t (prog1 (pcase (completion--do-completion beg end)
                (#b000 nil)
                (_     t))
-        (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))))))
+        ;; FIXME: This part of the fix for bug#81537 reintroduces
+        ;; bug#67001 for `icomplete-in-region' users.  It's not as bad
+        ;; for them because Icomplete users probably expect to have to
+        ;; C-g out of completion before using other bindings, but maybe
+        ;; we can still fix it.  --spwhitton
+        (unless (completion--icomplete-in-buffer-p)
+          (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