]> git.eshelyaron.com Git - emacs.git/commitdiff
'window-state-normalize-buffer-name' option for `uniquify' buffers
authorshipmints <shipmints@gmail.com>
Wed, 12 Mar 2025 16:14:50 +0000 (12:14 -0400)
committerEshel Yaron <me@eshelyaron.com>
Sun, 23 Mar 2025 19:25:10 +0000 (20:25 +0100)
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)

(cherry picked from commit 4266514dc88345d5c1bc1ab6dd8576dd47caf57f)

doc/lispref/windows.texi
lisp/window.el

index 5c0db6d48779edd4a4882c7ac89a04d98d91fd50..1ebf84c3261a2f5143e2d72fa1dc21ccc1f21e16 100644 (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
index d5672ee6a2254fe3592bb81f47a7e6903612869b..4598b8ce3e2f1b2a195e51964a163b417eadefe6 100644 (file)
@@ -6248,6 +6248,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
@@ -6300,7 +6316,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))
@@ -6322,13 +6339,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)