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

From: Juri Linkov <[email-masked]>
Date: Thu, 25 Oct 2018 02:39:45 +0300

>> Then we desperately need functions that will serialize window
>> configurations to writable window states to save them in the
>> desktop file.
>
> What's wrong with
>
> (save-window-excursion
>   (set-window-configuration configuration)
>   (window-state-get nil t))

Is it really a good solution for serialization of tens of window configurations?
Is such code suitable to be placed to desktop.el?

(let (window-configurations window-states current-window-configuration
      ;; Let-bind there hooks to nil to prevent them from running
      (window-configuration-change-hook nil)
      (window-size-change-functions nil))

  ;; As an example, populate a list of window configurations.
  ;; In reality, all these configurations are different.
  (dotimes (_ (+ 10 (random 10)))
    (push (current-window-configuration) window-configurations))

  ;; Preserve the current window configuration
  (setq current-window-configuration (current-window-configuration))
  ;; Serialize window configurations for saving in the desktop file
  (setq window-states
        (mapcar (lambda (window-configuration)
                  (save-window-excursion
                    (set-window-configuration window-configuration)
                    (window-state-get nil t)))
                window-configurations))
  ;; Restore the previous window-configuration
  (set-window-configuration current-window-configuration)
  ;; Now window-states can be saved to the desktop file

  ;; Restore window-states from the desktop file
  ;; First, preserve the current window configuration
  (setq current-window-configuration (current-window-configuration))
  ;; Restore the saved list from the desktop file
  (setq window-configurations
        (mapcar (lambda (window-state)
                  (save-window-excursion
                    (window-state-put window-state nil 'safe)
                    (current-window-configuration)))
                window-states))
  ;; Restore the previous window-configuration
  (set-window-configuration current-window-configuration)

  ;; Now window-configurations are available for another session
  window-configurations)

While testing without let-binding window-configuration-change-hook to nil,
window-configuration-change-hook was called 50 times, observable with:

(add-hook 'window-configuration-change-hook
          (lambda () (message "window-configuration-change-hook called")))

when let-bound to nil, these hooks are not called.

But after let-binding window-size-change-functions to nil,
and testing with

(add-hook 'window-size-change-functions
          (lambda (_) (message "window-size-change-functions called")))

window-size-change-functions is still called once.
I don't understand why.