Subject: bug#32850: 27.0.50; window-swap-states doesn't swap window prev/next-buffers

From: Juri Linkov <[email-masked]>
Date: Sun, 28 Oct 2018 00:39:14 +0300

>>> The most simple way to find out is to define such a list in window.c
>>> and have 'set-window-prev-buffers' holler if the prev_buffers argument
>>> is equal to that list.
>>
>> I can't find out how '(nil nil nil)' gets into the prev-buffers,
>
> Did you try what I proposed?  If that doesn't catch it, I have no
> idea.  IIUC mark_discard_killed_buffers just removes entries from that
> list, it cannot introduce any nil valued elements.
>
>> so to avoid the error, I propose to install this patch:
>
> Well this will just cache the underlying problem.  But I have no good
> idea how to investigate this issue any furher, so install if you like.

Now I found a reproducible test case:

1. create and remember a window-configuration

2. switch to a different window-configuration

3. kill the buffer that was in prev-buffers of the
   first window-configuration

4. restore the first window-configuration

5. look into its prev-buffers, it contains:

   (#<killed buffer> #<marker in no buffer> #<marker in no buffer>)

6. (window-state-get nil t) on the window with such prev-buffers
    produces:

    (nil nil nil)

I believe this patch should fix the problem:

diff --git a/lisp/window.el b/lisp/window.el
index 27d7e42280..ad8164052f 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -5552,9 +5552,14 @@ window--state-get-1
 	 (buffer (window-buffer window))
 	 (selected (eq window (selected-window)))
 	 (next-buffers (when (window-live-p window)
-			 (window-next-buffers window)))
+	                 (delq nil (mapcar (lambda (buffer)
+                                             (and (buffer-live-p buffer) buffer))
+                                           (window-next-buffers window)))))
 	 (prev-buffers (when (window-live-p window)
-			 (window-prev-buffers window)))
+	                 (delq nil (mapcar (lambda (entry)
+                                             (and (buffer-live-p (nth 0 entry))
+                                                  entry))
+                                           (window-prev-buffers window)))))
 	 (head
 	  `(,type
             ,@(unless (window-next-sibling window) `((last . t)))