3

Suppose I used C-x r w to run window-configuration-to-register, and suppose I saved it to the register k. Later, when I read back the register, I get this.

ELISP> (get-register ?k)
(#<window-configuration> #<marker at 5433 in *ielm*>)

Is there any way to serialize #<window-configuration> such that I could write code that prefills a registers with various window configurations on start up? I want to be able to use C-x r j to jump-to-register to a few known window configurations.

g-gundam
  • 1,096
  • 1
  • 3
  • 13
  • 1
    The beginnings are described in the Emacs Lisp Reference manual in [Window Configurations](https://www.gnu.org/software/emacs/manual/html_node/elisp/Window-Configurations.html) which also includes the description of a few functions that can be used "in order to store a window configuration on disk and read it back in another Emacs session ...", although I suspect there are many details to work out. Looking forward to your answer :-) – NickD Sep 27 '22 at 15:54
  • 1
    I don't think the question is really about registers. If so, consider removing that tag. I think it's about persisting a window config in a way that it can be reread (loaded) and thus restored from disk. That's independent of using registers. – Drew Sep 27 '22 at 19:11
  • 1
    NickD - Thanks for the tip. I was able to figure something out after following that link. Drew - Focusing on window configs and forgetting about registers was a good call. It turned out to be easier to solve the higher-level problem without using the existing register infrastructure. I'll provide a public answer to my own question soon. – g-gundam Sep 28 '22 at 10:52

1 Answers1

1

The unfortunate thing about the function window-configuration-to-register is that it returns an opaque #<window-configuration> data structure that is not easily serialized. Instead, one should use the function window-state-get which will return a window configuration as an elisp data structure. Calling it with the following parameters will return a data structure that is amenable to being saved in an elisp source file.

(window-state-get (frame-root-window) t)

The return value of the above function can be passed to window-state-put to restore the window configuration later. To make this more generally usable, I came up with the following interactive functions.

(defvar w/configs (make-hash-table :test #'equal)
  "This is a hash-table where window configurations used by w/save and w/load are stored.")

(defun w/save (key)
  "Save the current window configuration to w/configs."
  (interactive "sName for window configuration: ")
  (puthash key
           (window-state-get (frame-root-window) t)
           w/configs))

(defun w/load (key)
  "Load a named window configuration from w/configs."
  (interactive
   (let* ((completion-ignore-case t)
          (completions '())
          (nothing (maphash (lambda (k v) (push k completions)) w/configs)))
     (list (completing-read "Choose: " completions nil t))))
  (window-state-put (gethash key w/configs)))

These two functions can be used to save and load window configurations into the hash-table w/configs. If there are window configurations you use often, you can preload w/configs with those settings. Binding these functions to keys is left as an exercise for the reader.

This solution doesn't use registers, but that's OK. It does solve the higher-level problem of saving and loading window configurations in elisp data structures. Whether the register system is used or a hash-table is used is an implementation detail.

g-gundam
  • 1,096
  • 1
  • 3
  • 13