Subject: 28.0.50; quit-restore-buffer may jam
From: pillule <[email-masked]>
To: [email-masked]
Date: Tue, 11 May 2021 20:26:08 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hello there,

I think there is a small bug with quit-window / 
quit-restore-window :

with emacs -Q

;; set some rules
(setq display-buffer-alist
      '(("\\*\\(Backtrace\\|Messages\\)\\*"
        (display-buffer-in-side-window)
        (window-height . 0.2)
        (side . bottom))))

;; trigger a logical error to display the *Backtrace* buffer
(> vim emacs)

;; display the *Messages* buffer
(view-echo-area-messages)

;; mess the `quit-restore' window parameter
(kill-buffer (get-buffer "*Backtrace*"))

;; which looks now like that in the *Messages* window :
;; ((quit-restore) (window-slot . 0) (window-side . bottom))

;; try to quit the *Messages* window
(progn (select-window (view-echo-area-messages))
       (quit-window))

;; nothing happens :

The first `cond' of quit-restore-window end up to 't,
it calls (switch-to-prev-buffer window bury-or-kill)
but there is no previous-window available.

The first patch adress this, and a similar one : when there is a previous-buffer available, but it referes to the 
same buffer. (eg: try the recipe again but exchange the role of 
*Messages* and *Backtrace*, you will finish with a blank buffer for backtrace, that you can quit again to end up to the same point)

The second patch is a proposition to clarify a little the code.

diff -u --label /usr/share/emacs/28.0.50/lisp/window.el.gz --label /home/user/src/patches/window.el /tmp/jka-comHlW7wF /home/user/src/patches/window.el
--- /usr/share/emacs/28.0.50/lisp/window.el.gz
+++ /home/user/src/patches/window.el
@@ -5151,7 +5151,9 @@
       (set-window-parameter window 'quit-restore nil)
       ;; Make sure that WINDOW is no more dedicated.
       (set-window-dedicated-p window nil)
-      (switch-to-prev-buffer window bury-or-kill)))
+      (if prev-buffer
+          (switch-to-prev-buffer window bury-or-kill)
+        (window--delete window nil (eq bury-or-kill 'kill)))))
 
     ;; Deal with the buffer.
     (cond

Diff finished.  Tue May 11 21:26:38 2021
diff -u --label /usr/share/emacs/28.0.50/lisp/window.el.gz --label /home/user/src/patches/window.el /tmp/jka-comAXCNZa /home/user/src/patches/window.el
--- /usr/share/emacs/28.0.50/lisp/window.el.gz
+++ /home/user/src/patches/window.el
@@ -5073,14 +5073,9 @@
   (setq window (window-normalize-window window t))
   (let* ((buffer (window-buffer window))
 	 (quit-restore (window-parameter window 'quit-restore))
-	 (prev-buffer
-	  (let* ((prev-buffers (window-prev-buffers window))
-		 (prev-buffer (caar prev-buffers)))
-	    (and (or (not (eq prev-buffer buffer))
-		     (and (cdr prev-buffers)
-			  (not (eq (setq prev-buffer (cadr prev-buffers))
-				   buffer))))
-		 prev-buffer)))
+	 (prev-buffer (car (cl-find-if-not
+                            (lambda (buf) (eq (car buf) buffer))
+                            (window-prev-buffers window))))
 	 quad entry)
     (cond
      ((and (not prev-buffer)

Diff finished.  Tue May 11 22:00:30 2021

--