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: Fri, 22 Mar 2024 18:15:52 +0200 Organization: LINKOV.NET Message-ID: <86zfuq5jxj.fsf@mail.linkov.net> References: <87bm59mglk.fsf@mail.linkov.net> <86fsohmqn9.fsf@mail.linkov.net> <9df28298-fedc-0cfe-7243-c04868115e90@gmx.at> <86fsoezsw3.fsf@mail.linkov.net> <86sfsewvxr.fsf@mail.linkov.net> <9d741b43-e3bb-669b-b345-6b877c902b33@gmx.at> <86fsoaq4lo.fsf@mail.linkov.net> <86tucp7dbp.fsf@mail.linkov.net> <37e2129c-464f-7f0a-0870-f7360ca21dc3@gmx.at> <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> 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="5327"; 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 Fri Mar 22 17:19:44 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 1rnhcN-00019Q-Vf for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 22 Mar 2024 17:19:44 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rnhcA-00070p-Cy; Fri, 22 Mar 2024 12:19:30 -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 1rnhc1-0006yj-F2 for bug-gnu-emacs@gnu.org; Fri, 22 Mar 2024 12:19:21 -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 1rnhc1-0001ft-34 for bug-gnu-emacs@gnu.org; Fri, 22 Mar 2024 12:19:21 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rnhcf-0007mM-M0 for bug-gnu-emacs@gnu.org; Fri, 22 Mar 2024 12:20:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 22 Mar 2024 16:20:01 +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.171112434929821 (code B ref 33871); Fri, 22 Mar 2024 16:20:01 +0000 Original-Received: (at 33871) by debbugs.gnu.org; 22 Mar 2024 16:19:09 +0000 Original-Received: from localhost ([127.0.0.1]:34472 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rnhbp-0007ku-0U for submit@debbugs.gnu.org; Fri, 22 Mar 2024 12:19:09 -0400 Original-Received: from relay4-d.mail.gandi.net ([217.70.183.196]:35123) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rnhbm-0007k1-I6 for 33871@debbugs.gnu.org; Fri, 22 Mar 2024 12:19:07 -0400 Original-Received: by mail.gandi.net (Postfix) with ESMTPSA id 322E1E0002; Fri, 22 Mar 2024 16:17:58 +0000 (UTC) In-Reply-To: <532c59dc-59af-4454-b926-2f80fe711fe9@gmx.at> (martin rudalics's message of "Fri, 22 Mar 2024 10:26:19 +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:281952 Archived-At: --=-=-= Content-Type: text/plain >> 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. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=tab-bar-select-restore-context-2.patch 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))) --=-=-=--