Subject: 25.0.50; reusing windows in pop-to-buffer while in completion-in-region-mode opens new frames

From: [email-masked] (João Távora)
Date: Sat, 12 Mar 2016 16:32:50 +0000

Hi maintainers,

Emacs -Q and use just this customization

  (setq display-buffer-base-action
        `(display-buffer-reuse-window . ((inhibit-same-window . t))))

Do something that enters completion-in-region-mode. While in that
transient mode, invoke something that pops a buffer.

  d i s p l a  M-x complete-symbol RET
  M-x pop-to-buffer RET *Messages* RET

A new frame is created even though pop-up-frames is nil. I believe the
reason is that the *Completions* window is "soft-dedicated" in
minibuffer.el.

My fix would be to exit the transient completion-in-region-mode before
displaying other buffers. This can be done by setting
display-buffer-alist appropriately in completion-in-region-mode, much
like it sets and resets minor-mode-overriding-map-alist.

I've tried this in an extension of mine, SLY, with a mode very similar
to completion-in-region-mode, but the same technique doesn't work there
for some reason.

Anyway, the _failed_ patch after my signature.
João

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 2814d02..c115476 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -2024,6 +2024,9 @@ completion-in-region-mode
   (setq minor-mode-overriding-map-alist
         (delq (assq 'completion-in-region-mode minor-mode-overriding-map-alist)
               minor-mode-overriding-map-alist))
+  (setq display-buffer-alist
+        (delq (assq 'completion--in-region-mode-display-guard-p display-buffer-alist)
+              display-buffer-alist))
   (if (null completion-in-region-mode)
       (progn
         (setq completion-in-region--data nil)
@@ -2035,7 +2038,11 @@ completion-in-region-mode
 	  completion-in-region-mode-predicate)
     (add-hook 'post-command-hook #'completion-in-region--postch)
     (push `(completion-in-region-mode . ,completion-in-region-mode-map)
-          minor-mode-overriding-map-alist)))
+          minor-mode-overriding-map-alist)
+    (push `(completion--in-region-mode-display-guard-p
+            (completion--in-region-mode-teardown-before-displaying-others
+             . ,display-buffer-alist))
+          display-buffer-alist)))
 
 ;; Define-minor-mode added our keymap to minor-mode-map-alist, but we want it
 ;; on minor-mode-overriding-map-alist instead.
@@ -2043,6 +2050,17 @@ completion-in-region-mode
       (delq (assq 'completion-in-region-mode minor-mode-map-alist)
             minor-mode-map-alist))
 
+;; Do some crazy `display-buffer-alist' antics to prevent the
+;; soft-dedication of *Completions* to interfere with normal
+;; display-buffer
+(defun completion--in-region-mode-display-guard-p (buffer-name _alist)
+  (not (equal "*Completions*" buffer-name)))
+
+(defun completion--in-region-mode-teardown-before-displaying-others (_buffer _action)
+  (completion-in-region-mode -1)
+  ;; explicitly return nil, for clarity
+  nil)
+
 (defvar completion-at-point-functions '(tags-completion-at-point-function)
   "Special hook to find the completion table for the thing at point.
 Each function on this hook is called in turn without any argument and should