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

From: Juri Linkov <[email-masked]>
Date: Mon, 15 Oct 2018 23:48:36 +0300

>> The root of the problem is not in window-swap-states, it's in
>> window-state-get that it it relies upon.  For a long time I have
>> been using a package that keeps a list of window-configurations
>> and switches between them in the same frame.  But the problem
>> is that we have no serialization for window-configurations, so
>> no way to save in the desktop file and restore in another session.
>>
>> With the invention of window-state-get I tried to replace all calls
>> of current-window-configuration with window-state-get (and
>> set-window-configuration with window-state-put), and it worked
>> perfectly (saved and restored serialized window-configurations
>> in the desktop file), but still the problem that prevents its use
>> is that it doesn't store prev/next-buffers that is a very important
>> feature.
>
> Please provide a patch that makes the behavior you want optional.

Are there cases where this behavior is undesirable?

> I could try to come up with a patch myself but since you already have
> coded such a thing why duplicate the efforts?  IIUC we also have to
> decide whether and how to expand your code to desktop saving and how
> to handle buffers that got killed in between saving and restoring.

This patch handles killed buffers, so it works for desktop saving as well
(because the desktop restores the frameset only after it reads all buffers):

diff --git a/lisp/window.el b/lisp/window.el
index 0a42dae6ca..a41d7a6f3c 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -5541,6 +5541,10 @@ window--state-get-1
 	   (t 'leaf)))
 	 (buffer (window-buffer window))
 	 (selected (eq window (selected-window)))
+	 (next-buffers (when (window-live-p window)
+			 (window-next-buffers window)))
+	 (prev-buffers (when (window-live-p window)
+			 (window-prev-buffers window)))
 	 (head
 	  `(,type
             ,@(unless (window-next-sibling window) `((last . t)))
@@ -5593,7 +5597,22 @@ window--state-get-1
 		     (start . ,(if writable
                                    start
                                  (with-current-buffer buffer
-                                   (copy-marker start))))))))))
+                                   (copy-marker start))))))))
+            ,@(when next-buffers
+                `((next-buffers . ,(mapcar (lambda (buffer)
+                                           (buffer-name buffer))
+                                         next-buffers))))
+            ,@(when prev-buffers
+                `((prev-buffers .
+                   ,(mapcar (lambda (entry)
+                              (list (buffer-name (nth 0 entry))
+                                    (if writable
+                                        (marker-position (nth 1 entry))
+                                      (nth 1 entry))
+                                    (if writable
+                                        (marker-position (nth 2 entry))
+                                      (nth 2 entry))))
+                            prev-buffers))))))
 	 (tail
 	  (when (memq type '(vc hc))
 	    (let (list)
@@ -5736,7 +5755,9 @@ window--state-put-2
     (let ((window (car item))
 	  (combination-limit (cdr (assq 'combination-limit item)))
 	  (parameters (cdr (assq 'parameters item)))
-	  (state (cdr (assq 'buffer item))))
+	  (state (cdr (assq 'buffer item)))
+	  (next-buffers (cdr (assq 'next-buffers item)))
+	  (prev-buffers (cdr (assq 'prev-buffers item))))
       (when combination-limit
 	(set-window-combination-limit window combination-limit))
       ;; Reset window's parameters and assign saved ones (we might want
@@ -5748,7 +5769,8 @@ window--state-put-2
 	  (set-window-parameter window (car parameter) (cdr parameter))))
       ;; Process buffer related state.
       (when state
-	(let ((buffer (get-buffer (car state))))
+	(let ((buffer (get-buffer (car state)))
+	      (state (cdr state)))
 	  (if buffer
 	      (with-current-buffer buffer
 		(set-window-buffer window buffer)
@@ -5817,7 +5839,26 @@ window--state-put-2
 		  (set-window-point window (cdr (assq 'point state))))
 		;; Select window if it's the selected one.
 		(when (cdr (assq 'selected state))
-		  (select-window window)))
+		  (select-window window))
+                (when next-buffers
+                  (set-window-next-buffers
+                   window
+                   (delq nil (mapcar (lambda (buffer)
+                                       (setq buffer (get-buffer buffer))
+                                       (when (buffer-live-p buffer) buffer))
+                                     next-buffers))))
+                (when prev-buffers
+                  (set-window-prev-buffers
+                   window
+                   (delq nil (mapcar (lambda (entry)
+                                       (let ((buffer (get-buffer (nth 0 entry))))
+                                         (when (buffer-live-p buffer)
+                                           (list buffer
+                                                 (set-marker (make-marker)
+                                                             (nth 1 entry) buffer)
+                                                 (set-marker (make-marker)
+                                                             (nth 2 entry) buffer)))))
+                                     prev-buffers)))))
 	    ;; We don't want to raise an error in case the buffer does
 	    ;; not exist anymore, so we switch to a previous one and
 	    ;; save the window with the intention of deleting it later