Subject: Re: bug#39379: 27.0.60; Fix for #38457 broke ido-vertical-mode

From: Eli Zaretskii <[email-masked]>
Date: Wed, 12 Feb 2020 20:18:33 +0200

> Cc: [email-masked], [email-masked]
> From: Dmitry Gutov <[email-masked]>
> Date: Wed, 12 Feb 2020 01:42:22 +0200
> 
> The patch more or less works, with the exception of the case where POS 
> is EOB (e.g. when the sole completion is fully typed out). But that 
> should be fixable as well.

Right, updated patch below.

> The reason I used after-string, though, is that icomplete-mode does 
> that. And either it should have the same problems, or ido creates some 
> circumstances where the problems show up.
> 
> The former seems to be the case, though. Like, if I set 
> max-mini-window-height to 1, then icomplete-mode also exhibits bug#39433.

OK, but this is an old problem, because I see the same in Emacs 26.3.
It wasn't caused by the new implementation of set-minibuffer-message.

> And while there is no xxx-vertical-mode for icomplete-mode, we would 
> probably want one to be written someday. Sounds like when that happens, 
> icomplete-vertical-mode would trigger this same bug as well unless we 
> fix it in the display engine or find some other general way to avoid it.

Well, we hardly ever fix future bugs.  And limiting mini-windows to
such small height is asking for trouble.

But okay, I'm fine with leaving this bug open, and will try to fix the
problem on master.  I do want to install the patch below, because it
fixes a problem directly caused by set-minibuffer-message, so we
should fix this in Emacs 27.  Any objections?

diff --git a/lisp/ido.el b/lisp/ido.el
index 6707d81407..d74ea69ae3 100644
--- a/lisp/ido.el
+++ b/lisp/ido.el
@@ -4728,16 +4728,11 @@ ido-exhibit
 	(let ((inf (ido-completions contents)))
 	  (setq ido-show-confirm-message nil)
 	  (ido-trace "inf" inf)
-          (when ido--overlay
-            (delete-overlay ido--overlay))
-          (let ((o (make-overlay (point-max) (point-max) nil t t)))
-            (when (> (length inf) 0)
-              ;; For hacks that redefine ido-completions function (bug#39379)
-              (when (eq (aref inf 0) ?\n)
-                (setq inf (concat " " inf)))
-              (put-text-property 0 1 'cursor t inf))
-            (overlay-put o 'after-string inf)
-            (setq ido--overlay o)))
+          (let ((pos (point)))
+            (insert inf)
+            (if (< pos (point-max))
+                (put-text-property pos (1+ pos) 'minibuffer-message t)))
+          )
 	))))
 
 (defun ido-completions (name)
diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index 0589211877..767fc8dff8 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -763,6 +763,16 @@ minibuffer-message-clear-timeout
 (defvar minibuffer-message-timer nil)
 (defvar minibuffer-message-overlay nil)
 
+(defun minibuffer--message-overlay-pos ()
+  "Return position where `set-minibuffer-message' shall put message overlay."
+  ;; Starting from point, look for non-nil 'minibuffer-message'
+  ;; property, and return its position.  If none found, return the EOB
+  ;; position.
+  (let* ((pt (point))
+         (propval (get-text-property pt 'minibuffer-message)))
+    (if propval pt
+      (next-single-property-change pt 'minibuffer-message nil (point-max)))))
+
 (defun set-minibuffer-message (message)
   "Temporarily display MESSAGE at the end of the minibuffer.
 The text is displayed for `minibuffer-message-clear-timeout' seconds
@@ -784,8 +794,9 @@ set-minibuffer-message
 
       (clear-minibuffer-message)
 
-      (setq minibuffer-message-overlay
-            (make-overlay (point-max) (point-max) nil t t))
+      (let ((ovpos (minibuffer--message-overlay-pos)))
+        (setq minibuffer-message-overlay
+              (make-overlay ovpos ovpos nil t t)))
       (unless (zerop (length message))
         ;; The current C cursor code doesn't know to use the overlay's
         ;; marker's stickiness to figure out whether to place the cursor