From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#33871: 27.0.50; Revert Dired window saved in window configuration Date: Thu, 28 Mar 2024 19:54:52 +0200 Organization: LINKOV.NET Message-ID: <86sf0actmb.fsf@mail.linkov.net> References: <87bm59mglk.fsf@mail.linkov.net> <86zfw07tbw.fsf@mail.linkov.net> <96fd3170-e5d2-4d16-93ec-c6fff3efb787@gmx.at> <86wmr2w7to.fsf@mail.linkov.net> <3808f9f8-624a-449c-8572-085582395859@gmx.at> <86y1bffvya.fsf@mail.linkov.net> <8634skstio.fsf@mail.linkov.net> <86msqr79n8.fsf@mail.linkov.net> <532c59dc-59af-4454-b926-2f80fe711fe9@gmx.at> <86zfuq5jxj.fsf@mail.linkov.net> <5cf2b85e-9e24-4a7d-b175-ce140580df32@gmx.at> <86plvksv1e.fsf@mail.linkov.net> <578de52d-364b-4961-b4e2-7f91cdcaa0b9@gmx.at> <86frwf8w4q.fsf@mail.linkov.net> <128b074a-b766-47eb-92ff-79814c900a06@gmx.at> <86v85adzmd.fsf@mail.linkov.net> <86le62vpew.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40032"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/30.0.50 (x86_64-pc-linux-gnu) Cc: 33871@debbugs.gnu.org To: martin rudalics Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Mar 28 19:06:18 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rpu8n-000A9Y-Lb for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 28 Mar 2024 19:06:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rpu8Z-0006qa-ET; Thu, 28 Mar 2024 14:06:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rpu8Y-0006pw-3W for bug-gnu-emacs@gnu.org; Thu, 28 Mar 2024 14:06:02 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rpu8X-00030h-R7 for bug-gnu-emacs@gnu.org; Thu, 28 Mar 2024 14:06:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rpu8Y-00021a-9c for bug-gnu-emacs@gnu.org; Thu, 28 Mar 2024 14:06:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 28 Mar 2024 18:06:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 33871 X-GNU-PR-Package: emacs Original-Received: via spool by 33871-submit@debbugs.gnu.org id=B33871.17116491117660 (code B ref 33871); Thu, 28 Mar 2024 18:06:02 +0000 Original-Received: (at 33871) by debbugs.gnu.org; 28 Mar 2024 18:05:11 +0000 Original-Received: from localhost ([127.0.0.1]:41100 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rpu7h-0001z6-1S for submit@debbugs.gnu.org; Thu, 28 Mar 2024 14:05:11 -0400 Original-Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]:51941) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rpu7a-0001xd-6G for 33871@debbugs.gnu.org; Thu, 28 Mar 2024 14:05:03 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 2EC0BE0008; Thu, 28 Mar 2024 18:04:55 +0000 (UTC) In-Reply-To: (martin rudalics's message of "Thu, 28 Mar 2024 10:18:12 +0100") X-GND-Sasl: juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:282248 Archived-At: --=-=-= Content-Type: text/plain >> So now here is the final patch: > > For 'window-point-context-set' I'd rather write something like: > > Call function specified by `window-point-context-set-function' for every > live window on the selected frame with that window as sole argument and > that window's buffer current. 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'. Remember the returned context in the window > parameter `context'. Thanks for the suggestion, this is added in a new patch. > I still don't get why you use 'with-current-buffer' here. IIUC usually > a buffer does not have 'window-point-context-set-function' set and so a > simple > > (buffer-local-value 'window-point-context-set-function (window-buffer w)) > > should suffice. Ok, let's use buffer-local-value: --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=window-point-context.patch 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))) --=-=-=--