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)))