diff --git a/lisp/window.el b/lisp/window.el index df55a7ca673..3998f869855 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -10834,6 +10878,78 @@ 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 function specified by `window-point-context-set-function' for every +live window on the selected frame with that window as sole argument. +The function called is supposed to return a context of the window's point +that can be later used as argument for `window-point-context-use-function'. +Remember the returned context in the window parameter `context'." + (walk-windows + (lambda (w) + (when-let ((fn (buffer-local-value 'window-point-context-set-function + (window-buffer w))) + ((functionp fn)) + (context (funcall fn w))) + (set-window-parameter w 'context (cons (buffer-name) context)))) + 'nomini)) + +(defun window-point-context-use () + "Use context to relocate the window point. +Call function specified by `window-point-context-use-function' to move the +window point according to the previously saved context. For every live +window on the selected frame this function is called with two arguments: +the window and the context data structure saved by +`window-point-context-set-function' in the window parameter `context'. +The function called is supposed to set the window point to the location +found by the provided context." + (walk-windows + (lambda (w) + (when-let ((fn (buffer-local-value 'window-point-context-use-function + (window-buffer w))) + ((functionp fn)) + (context (window-parameter w 'context)) + ((equal (buffer-name) (car context)))) + (funcall fn 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." + (with-current-buffer (window-buffer w) + (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." + (with-current-buffer (window-buffer w) + (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..0c73ae31e96 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -2743,6 +2745,26 @@ 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) + (with-current-buffer (window-buffer 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) + (with-current-buffer (window-buffer w) + (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..95bf3f9bc81 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,16 @@ 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 previous context in every window of the selected tab. +This will try to find the same position in the buffer where point was +before switching away from this tab. After selecting this tab again, +point will be moved to its previous place in the buffer even when +buffer was modified." + :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 +1552,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)))