From 4266514dc88345d5c1bc1ab6dd8576dd47caf57f Mon Sep 17 00:00:00 2001 From: shipmints Date: Wed, 12 Mar 2025 12:14:50 -0400 Subject: [PATCH] '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) --- doc/lispref/windows.texi | 5 +++++ etc/NEWS | 6 ++++++ lisp/window.el | 25 ++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/doc/lispref/windows.texi b/doc/lispref/windows.texi index 5c0db6d4877..1ebf84c3261 100644 --- a/doc/lispref/windows.texi +++ b/doc/lispref/windows.texi @@ -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 diff --git a/etc/NEWS b/etc/NEWS index 19899349ec1..c4a55786cf8 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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 +++ diff --git a/lisp/window.el b/lisp/window.el index 1e8f4e323bc..0e6090a6ec1 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -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)