'window-state-normalize-buffer-name' option for `uniquify' buffers

If 'window-state-normalize-buffer-name' is non-nil, 'window-state-get'
will normalize stored buffer names, making them easier to restore for
users that use 'uniquify' buffer naming.
* doc/lispref/windows.texi (Window Configurations): Document
'window-state-normalize-buffer-name'.
* lisp/window.el (window-state-normalize-buffer-name): New defvar.
(window--state-normalize-buffer-name): New function.
(window--state-get-1): Call 'window--state-normalize-buffer-name'
rather than 'buffer-name'.
* etc/NEWS: Announce 'window-state-normalize-buffer-name'.
(Bug#76980)
This commit is contained in:
shipmints 2025-03-12 12:14:50 -04:00 committed by Eli Zaretskii
parent 1aebb02611
commit 4266514dc8
3 changed files with 33 additions and 3 deletions

View file

@ -7000,6 +7000,11 @@ state will be written to disk and read back in another session.
Together, the argument @var{writable} and the variable
@code{window-persistent-parameters} specify which window parameters are
saved by this function. @xref{Window Parameters}.
Bind @code{window-state-normalize-buffer-name} to non-@code{nil} to
normalize buffer names under @file{uniquify} management by removing its
prefixes and suffixes. This helps restore window buffers across Emacs
sessions. @xref{Uniquify,,, emacs, The GNU Emacs Manual}.
@end defun
The value returned by @code{window-state-get} can be used in the same

View file

@ -292,6 +292,12 @@ return value windows whose buffers share their text with BUFFER-OR-NAME.
With such an entry, 'display-buffer-reuse-window' may also choose a
window whose buffer shares text with the buffer to display.
+++
*** New variable 'window-state-normalize-buffer-name'.
When bound to non-nil, 'window-state-get' will normalize 'uniquify'
managed buffer names by removing 'uniquify' prefixes and suffixes. This
helps restore window buffers across Emacs sessions.
** Frames
+++

View file

@ -6300,6 +6300,22 @@ specific buffers."
))
;;; Window states, how to get them and how to put them in a window.
(defvar window-state-normalize-buffer-name nil
"Non-nil means accommodate buffer names under `uniquify' management.
`uniquify' prefixes and suffixes will be removed.")
(defun window--state-normalize-buffer-name (buffer)
"Normalize BUFFER name, accommodating `uniquify'.
If BUFFER is under `uniquify' management, return its `buffer-name' with
its prefixes and suffixes removed; otherwise return BUFFER
`buffer-name'."
(or (and window-state-normalize-buffer-name
(fboundp 'uniquify-buffer-base-name)
(with-current-buffer buffer
(uniquify-buffer-base-name)))
(buffer-name buffer)))
(defun window--state-get-1 (window &optional writable)
"Helper function for `window-state-get'."
(let* ((type
@ -6352,7 +6368,8 @@ specific buffers."
(let ((point (window-point window))
(start (window-start window)))
`((buffer
,(if writable (buffer-name buffer) buffer)
,(if writable (window--state-normalize-buffer-name
buffer) buffer)
(selected . ,selected)
(hscroll . ,(window-hscroll window))
(fringes . ,(window-fringes window))
@ -6374,13 +6391,15 @@ specific buffers."
,@(when next-buffers
`((next-buffers
. ,(if writable
(mapcar #'buffer-name next-buffers)
(mapcar #'window--state-normalize-buffer-name
next-buffers)
next-buffers))))
,@(when prev-buffers
`((prev-buffers
. ,(if writable
(mapcar (lambda (entry)
(list (buffer-name (nth 0 entry))
(list (window--state-normalize-buffer-name
(nth 0 entry))
(marker-position (nth 1 entry))
(marker-position (nth 2 entry))))
prev-buffers)