Subject: bug#66394: 29.1; Make register-read-with-preview more useful

From: Stefan Monnier <[email-masked]>
Date: Sun, 17 Dec 2023 18:20:56 -0500

> From 6a15a8b7b795b8377ef3537dc7bbd5ba26c0e159 Mon Sep 17 00:00:00 2001
> From: Thierry Volpiatto <[email-masked]>
> Date: Thu, 14 Dec 2023 16:23:26 +0100
> Subject: [PATCH 3/4] Save and restore win confs after register commands
>
> When several windows are open, the register preview may eat other
> windows and break the current window configuration.
>
> * lisp/register.el (register-read-with-preview-fancy): Do it.
> ---
>  lisp/register.el | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/lisp/register.el b/lisp/register.el
> index 8f0c6a7105d..1fadbe81056 100644
> --- a/lisp/register.el
> +++ b/lisp/register.el
> @@ -473,6 +473,7 @@ display such a window regardless."
>           (buffer1 "*Register quick preview*")
>           (buf (if register-use-preview buffer buffer1))
>           (pat "")
> +         (winconf (current-window-configuration))
>           (map (let ((m (make-sparse-keymap)))
>                  (set-keymap-parent m minibuffer-local-map)
>                  m))
> @@ -573,6 +574,7 @@ display such a window regardless."
>                        nil "No register specified")
>             (string-to-char result))
>        (when timer (cancel-timer timer))
> +      (set-window-configuration winconf)
>        (let ((w (get-buffer-window buf)))
>          (and (window-live-p w) (delete-window w)))
>        (and (get-buffer buf) (kill-buffer buf)))))

While save&restore of window configuration often works well, it
misbehaves in various cases (such as when the buffer is displayed in
another frame, which case case `set-window-configuration` will do you
no good).

The official way to undo a `display-buffer` is with
`quit-(restore-)window`.  See (info "(elisp)Quitting Windows").

        Stefan