Subject: Re: bug#33871: 27.0.50; Revert Dired window saved in window configuration

From: Juri Linkov <[email-masked]>
Date: Thu, 28 Mar 2024 09:40:47 +0200

>> 'window-restore-killed-buffer-windows' already restores
>> point and start of a window, so there is no problem.
>
> OK.  I won't insist.

So now here is the final patch:

diff --git a/lisp/window.el b/lisp/window.el
index df55a7ca673..1ec45027f4c 100644
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -10834,6 +10872,77 @@ window--adjust-process-windows
                 (set-process-window-size process (cdr size) (car size))))))))))
 
 (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
+
+
+;;; Window point context
+
+(defun window-point-context-set ()
+  "Set context near the window point.
+Call `window-point-context-set-function' to get the context near the window
+point of every window on the frame.  This function is called with the
+window argument.  During the call the current buffer is set to the window
+buffer, but its window is not selected.  It's expected that the called
+function will use the window point to return a data structure that will
+help later to find the same location again.  After the call remember the
+returned context in the window parameter `context'."
+  (walk-windows
+   (lambda (w)
+     (with-current-buffer (window-buffer w)
+       (when-let (((functionp window-point-context-set-function))
+                  (context (funcall window-point-context-set-function w)))
+         (set-window-parameter w 'context (cons (buffer-name) context)))))
+   'nomini))
+
+(defun window-point-context-use ()
+  "Use context to relocate the window point.
+Call `window-point-context-use-function' to move the window point according
+to the previously saved context.  For every window on the frame this
+function is called with two agreements: the window and the context data
+structure saved by `window-point-context-set-function' in the window
+parameter `context'.  During the call the current buffer is set to the
+window buffer, but its window is not selected.  It's expected that the
+called function will set the window point to the location found by using
+the provided context."
+  (walk-windows
+   (lambda (w)
+     (with-current-buffer (window-buffer w)
+       (when-let (((functionp window-point-context-use-function))
+                  (context (window-parameter w 'context))
+                  ((equal (buffer-name) (car context))))
+         (funcall window-point-context-use-function w (cdr context))
+         (set-window-parameter w 'context nil))))
+   'nomini))
+
+(add-to-list 'window-persistent-parameters '(context . writable))
+
+(defun window-point-context-set-default-function (w)
+  "Set context of file buffers to the front and rear strings."
+  (when buffer-file-name
+    (let ((point (window-point w)))
+      `((front-context-string
+         . ,(buffer-substring-no-properties
+             point (min (+ point 16) (point-max))))
+        (rear-context-string
+         . ,(buffer-substring-no-properties
+             point (max (- point 16) (point-min))))))))
+
+(defun window-point-context-use-default-function (w context)
+  "Restore context of file buffers by the front and rear strings."
+  (let ((point (window-point w)))
+    (save-excursion
+      (goto-char point)
+      (when-let ((f (alist-get 'front-context-string context))
+                 ((search-forward f (point-max) t)))
+        (goto-char (match-beginning 0))
+        (when-let ((r (alist-get 'rear-context-string context))
+                   ((search-backward r (point-min) t)))
+          (goto-char (match-end 0))
+          (setq point (point)))))
+    (set-window-point w point)))
+
+(defvar window-point-context-set-function 'window-point-context-set-default-function)
+(defvar window-point-context-use-function 'window-point-context-use-default-function)
+
 
 ;; Some of these are in tutorial--default-keys, so update that if you
 ;; change these.
diff --git a/lisp/dired.el b/lisp/dired.el
index 9e3b888df14..3eb4351c93e 100644
--- a/lisp/dired.el
+++ b/lisp/dired.el
@@ -2743,6 +2745,24 @@ dired-mode
               '(dired-font-lock-keywords t nil nil beginning-of-line))
   (setq-local desktop-save-buffer 'dired-desktop-buffer-misc-data)
   (setq-local grep-read-files-function #'dired-grep-read-files)
+  (setq-local window-point-context-set-function
+              (lambda (w)
+                (let ((point (window-point w)))
+                  (save-excursion
+                    (goto-char point)
+                    (if-let ((f (dired-get-filename nil t)))
+                        `((dired-filename . ,f))
+                      `((position . ,(point))))))))
+  (setq-local window-point-context-use-function
+              (lambda (w context)
+                (let ((point (window-point w)))
+                  (save-excursion
+                    (if-let ((f (alist-get 'dired-filename context)))
+                        (dired-goto-file f)
+                      (when-let ((p (alist-get 'position context)))
+                        (goto-char p)))
+                    (setq point (point)))
+                  (set-window-point w point))))
   (setq dired-switches-alist nil)
   (hack-dir-local-variables-non-file-buffer) ; before sorting
   (dired-sort-other dired-actual-switches t)
diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index fa22500a04e..ca0c92e903f 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1292,6 +1292,9 @@ tab-bar--tab
                                            frame 'buffer-list)))
          (bbl (seq-filter #'buffer-live-p (frame-parameter
                                            frame 'buried-buffer-list))))
+    (when tab-bar-select-restore-context
+      (window-point-context-set))
+
     `(tab
       (name . ,(if tab-explicit-name
                    (alist-get 'name tab)
@@ -1442,6 +1445,12 @@ tab-bar-select-restore-windows
           (setq buffer-read-only t)
           (set-window-buffer window new-buffer))))))
 
+(defcustom tab-bar-select-restore-context t
+  "Non-nil to restore context of the selected tab."
+  :type 'boolean
+  :group 'tab-bar
+  :version "30.1")
+
 (defvar tab-bar-minibuffer-restore-tab nil
   "Tab number for `tab-bar-minibuffer-restore-tab'.")
 
@@ -1539,6 +1548,9 @@ tab-bar-select-tab
             (select-window (get-mru-window)))
           (window-state-put ws nil 'safe)))
 
+        (when tab-bar-select-restore-context
+          (window-point-context-use))
+
         ;; Select the minibuffer when it was active before switching tabs
         (when (and minibuffer-was-active (active-minibuffer-window))
           (select-window (active-minibuffer-window)))