bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Juri Linkov
Subject: bug#33871: 27.0.50; Revert Dired window saved in window configuration
Date: Fri, 22 Mar 2024 18:15:52 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu)

>> There are two scenarios that this patch will fix:
>
> I reconstructed the buggy behaviors and checked that the patch fixes
> them.

Thanks for confirming.

>>> IIUC the original Bug#33871 is still unsolved.
>>
>> The proposed patch will close bug#33871.
>
> Can you amend the original scenario you posted there in a way that one
> can see how your patch can be used to fix it. That's where I stumbled
> initially because I didn't know what to do.

The original scenario was:

(progn (dired "/tmp") (dired-next-line 1)
       (split-window) (other-window 1)
       (dired "/tmp") (dired-next-line 2)
       (let ((w-c (current-window-configuration))
             (p-m (point-marker)))
         (view-emacs-todo) (delete-other-windows)
         (with-current-buffer (get-buffer "tmp")
           (revert-buffer))
         (set-window-configuration w-c)
         (goto-char p-m)))

Ok, let's add new code to separate functions
'window-set-context' and 'window-use-context'.
Below is the patch with these new functions.
Maybe they could be moved from tab-bar.el to window.el?

Then the amended scenario will be:

(progn (dired "/tmp") (dired-next-line 1)
       (split-window) (other-window 1)
       (dired "/tmp") (dired-next-line 2)
       (window-set-context)
       (let ((w-c (current-window-configuration))
             (p-m (point-marker)))
         (view-emacs-todo) (delete-other-windows)
         (with-current-buffer (get-buffer "tmp")
           (revert-buffer))
         (set-window-configuration w-c)
         (window-use-context)))

>>> Bug#33532 as well.
>>
>> Bug#33532 could be closed as well due to new context functions.
>>
>>> What about Bug#68235 and Bug#69093?
>>
>> Bug#68235 is closed, but the patch will solve the issue
>> mentioned in https://debbugs.gnu.org/68235#79
>> starting with the words:
>> "BTW, there is another problem when the same buffer is displayed
>> in two tabs/window-configurations."
>>
>> Bug#69093 is completely fixed.
>
> Is there any place where we should amend the documentation for these?

If code is ok, I could update the documentation.

> Which are the cases where the context function will fail to work?

I'm not aware of any such cases.

diff --git a/lisp/dired.el b/lisp/dired.el
index 9e3b888df14..cb2fc97d0ae 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-set-context-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-use-context-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..528c85f45d6 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1283,6 +1283,58 @@ frameset-filter-tabs
 
 (push '(tabs . frameset-filter-tabs) frameset-filter-alist)
 
+(defun window-set-context-default-function (w)
+  "Set context to the front/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-use-context-default-function (w context)
+  "Restore context by the front/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))
+          (unless (eq point (point))
+            (warn "!!! %S -> %S r=%S f=%S" point (point) r f))
+          (setq point (point)))))
+    (set-window-point w point)))
+
+(defvar window-set-context-function 'window-set-context-default-function)
+(defvar window-use-context-function 'window-use-context-default-function)
+
+(add-to-list 'window-persistent-parameters '(context . writable))
+
+(defun window-set-context ()
+  (walk-windows
+   (lambda (w)
+     (with-current-buffer (window-buffer w)
+       (when-let (((functionp window-set-context-function))
+                  (context (funcall window-set-context-function w)))
+         (set-window-parameter w 'context (cons (buffer-name) context)))))
+   'nomini))
+
+(defun window-use-context ()
+  (walk-windows
+   (lambda (w)
+     (with-current-buffer (window-buffer w)
+       (when-let (((functionp window-use-context-function))
+                  (context (window-parameter w 'context))
+                  ((equal (buffer-name) (car context))))
+         (funcall window-use-context-function w (cdr context))
+         (set-window-parameter w 'context nil))))
+   'nomini))
+
 (defun tab-bar--tab (&optional frame)
   "Make a new tab data structure that can be added to tabs on the FRAME."
   (let* ((tab (tab-bar--current-tab-find nil frame))
@@ -1292,6 +1344,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-set-context))
+
     `(tab
       (name . ,(if tab-explicit-name
                    (alist-get 'name tab)
@@ -1442,6 +1497,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 restored 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 +1600,9 @@ tab-bar-select-tab
             (select-window (get-mru-window)))
           (window-state-put ws nil 'safe)))
 
+        (when tab-bar-select-restore-context
+          (window-use-context))
+
         ;; Select the minibuffer when it was active before switching tabs
         (when (and minibuffer-was-active (active-minibuffer-window))
           (select-window (active-minibuffer-window)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]