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

From: Juri Linkov <[email-masked]>
Date: Thu, 18 Oct 2018 00:58:29 +0300

>> I'd like to make it customizable by using the existing customization in
>> the arg WRITABLE of window-state-get and window-persistent-parameters,
>> even though formally prev/next-buffers is not a window parameter (maybe
>> it should be, I don't know).
>
> Always keep in mind that prev_buffers and next_buffers need special
> treatment in mark_object to make sure that dead buffers from window
> configurations stored somewhere (for example in a register) get their
> entries deleted and can be eventually reclaimed.  This is something I
> completely disregarded when writing the original code for navigating
> these buffer lists.  Stefan then wrote the code to do that in
> mark_object.

When testing the previous patch, I noticed that killed buffers automatically
disappear from prev/next-buffers lists, so there are no #<killed buffer> remains.
Is it handled by code in kill-buffer?

>> Done, with a small change: even though set-marker is idempotent in regard
>> to its POSITION arg (i.e. if POSITION is a marker, it creates an identical
>> marker), I added a check to not create a new one.  OTOH, get-buffer is
>> idempotent too, but it seems window-state-put never receives a structure
>> with buffer objects, and I'm not sure why window-state-get should always
>> use buffer-name regardless of the value WRITABLE, i.e. why should it return
>> buffer names as strings instead of buffer objects even when WRITABLE is nil?
>
> Maybe because I didn't care about non-writable states back then.  But
> you're obviously right.  Buffers can be renamed at will and in that
> case we might have a state with a non-existent buffer or even buffers
> with names switched and therefore invalid values of start or point.

What do you think about creating new functions to convert the existing
states from non-writable to writable and back?  Then in the same session
it would be more optimal to use window states with buffer/mark objects,
and states to save to the desktop could be serialized by such functions.

> So yes: Please use a buffer object for non-writable states.

Here is a new patch:

diff --git a/lisp/window.el b/lisp/window.el
index a7318308ef..77b650fda9 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -5579,7 +5579,7 @@ window--state-get-1
 		(let ((point (window-point window))
 		      (start (window-start window)))
 		  `((buffer
-		     ,(buffer-name buffer)
+		     ,(if writable (buffer-name buffer) buffer)
 		     (selected . ,selected)
 		     (hscroll . ,(window-hscroll window))
 		     (fringes . ,(window-fringes window))
@@ -5599,20 +5599,20 @@ window--state-get-1
                                  (with-current-buffer buffer
                                    (copy-marker start))))))))
             ,@(when next-buffers
-                `((next-buffers . ,(mapcar (lambda (buffer)
-                                           (buffer-name buffer))
-                                         next-buffers))))
+                `((next-buffers
+                   . ,(if writable
+                          (mapcar (lambda (buffer) (buffer-name buffer))
+                                  next-buffers)
+                        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))))))
+                `((prev-buffers
+                   . ,(if writable
+                          (mapcar (lambda (entry)
+                                    (list (buffer-name (nth 0 entry))
+                                          (marker-position (nth 1 entry))
+                                          (marker-position (nth 2 entry))))
+                                  prev-buffers)
+                        prev-buffers))))))
 	 (tail
 	  (when (memq type '(vc hc))
 	    (let (list)