From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#58343: 29.0.50; ELisp code run in "inconsitent" selected-window state Date: Thu, 06 Oct 2022 20:06:25 -0400 Message-ID: Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="17972"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Alan Mackenzie To: 58343@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Oct 07 02:07:15 2022 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 1ogatW-0004Ug-7b for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 07 Oct 2022 02:07:14 +0200 Original-Received: from localhost ([::1]:51360 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ogatV-0000Ft-56 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 06 Oct 2022 20:07:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53010) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogatK-0000Fj-Ke for bug-gnu-emacs@gnu.org; Thu, 06 Oct 2022 20:07:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:34775) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ogatK-0002cz-C7 for bug-gnu-emacs@gnu.org; Thu, 06 Oct 2022 20:07:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ogatJ-0001kN-O3; Thu, 06 Oct 2022 20:07:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: acm@muc.de, bug-gnu-emacs@gnu.org Resent-Date: Fri, 07 Oct 2022 00:07:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 58343 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org X-Debbugs-Original-Xcc: Alan Mackenzie Original-Received: via spool by submit@debbugs.gnu.org id=B.16651012066663 (code B ref -1); Fri, 07 Oct 2022 00:07:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 7 Oct 2022 00:06:46 +0000 Original-Received: from localhost ([127.0.0.1]:33852 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ogat3-0001jN-JL for submit@debbugs.gnu.org; Thu, 06 Oct 2022 20:06:46 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:53906) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ogasz-0001j6-25 for submit@debbugs.gnu.org; Thu, 06 Oct 2022 20:06:43 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49216) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogasy-0000Ej-42 for bug-gnu-emacs@gnu.org; Thu, 06 Oct 2022 20:06:40 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:31233) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogasv-0002XQ-5N for bug-gnu-emacs@gnu.org; Thu, 06 Oct 2022 20:06:39 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 5D2C2100142; Thu, 6 Oct 2022 20:06:34 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 55028100091; Thu, 6 Oct 2022 20:06:32 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1665101192; bh=qDjeLU8aXe44UFrY8YfWs1gp1wErj/+vR5ThiBiuoSw=; h=From:To:Subject:Date:From; b=H8/N28OgF/x5Zzl070q9rERvk8NTF/6Jt6fPlHD4Wa4k3iOJocN1oI1ol7SCHKqZw KX2xTi2G636HpUB5Fl2NzZ2ePFfRpw/tVlQ/Z7QAelxxTZdviXTMpgZfL6LgmdNAZG sjhNgqGsa0PbxIMevJ4EwsI8FONl0WGR653cyPsOZ994coz6DPH3jRe+7EqNF0yB8u yDZjqqZ6ttX1F2EqxCVs/dg3zbDA4axGQpDAyXOjzz0u/r6VgSMjWX454qsd/tXQTA WiDg1DPTYz/7TlLbXK3Wt4ffQgOv6tgtmLDo52b9cptbsaBjRxAmpUssZEOQMHbeON PLz5WIjiTBNOg== Original-Received: from milanesa (65-110-220-202.cpe.pppoe.ca [65.110.220.202]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 20F8A120CE9; Thu, 6 Oct 2022 20:06:32 -0400 (EDT) Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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" Xref: news.gmane.io gmane.emacs.bugs:244720 Archived-At: Package: Emacs Version: 29.0.50 Hi Alan and friends, In commit dfa3e6f424b20fe27d9041b2ce7d69811df5d8cd, Alan added the following code to do_switch_frame: diff --git a/src/frame.c b/src/frame.c index ccac18d23c2..dc8045f41e6 100644 --- a/src/frame.c +++ b/src/frame.c @@ -1564,6 +1564,13 @@ do_switch_frame (Lisp_Object frame, int track, int for_deletion, Lisp_Object nor if (! FRAME_MINIBUF_ONLY_P (XFRAME (selected_frame))) last_nonminibuf_frame = XFRAME (selected_frame); + /* If the selected window in the target frame is its mini-window, we move + to a different window, the most recently used one, unless there is a + valid active minibuffer in the mini-window. */ + if (EQ (f->selected_window, f->minibuffer_window) + && NILP (Fminibufferp (XWINDOW (f->minibuffer_window)->contents, Qt))) + Fset_frame_selected_window (frame, call1 (Qget_mru_window, frame), Qnil); + Fselect_window (f->selected_window, norecord); /* We want to make sure that the next event generates a frame-switch the problem with this is that it calls `Qget_mru_window` which is ELisp code, and that we're in the very middle of changing the selected frame, so we have already changed the `selected-frame` variable a few lines earlier, but the select-window has not yet been adjusted accordingly. Running ELisp code in a state where (selected-window) and (frame-selected-window) aren't equal is a recipe for problems. I have already wasted many hours in the past tracking down bugs linked to this kind of situation (back when the mode-line was processed in such an inconsistent state, for example), and I really don't want to go there. So we should arrange to run this `get-mru-window` function at some other time, for example a few lines earlier before we set `selected-frame`. I don't understand this code nearly enough to know how to move the code, because it probably interacts with the other statements in non-trivial ways, so the patch below is just a naive suggestion (it seems to work here without triggering my many sprinkled assertions checking that `EQ (XFRAME (selected_frame)->selected_window, selected_window)`, but it's a far cry from a confirmation that it's right). Hopefully someone here is aware of some of the potential pitfalls. Stefan diff --git a/src/frame.c b/src/frame.c index 91b9bec82c3..58b6ee50d23 100644 --- a/src/frame.c +++ b/src/frame.c @@ -1503,18 +1503,8 @@ do_switch_frame (Lisp_Object frame, int for_deletion, Lisp_Object norecord) sf->select_mini_window_flag = MINI_WINDOW_P (XWINDOW (sf->selected_window)); - selected_frame = frame; - move_minibuffers_onto_frame (sf, for_deletion); - if (f->select_mini_window_flag - && !NILP (Fminibufferp (XWINDOW (f->minibuffer_window)->contents, Qt))) - f->selected_window = f->minibuffer_window; - f->select_mini_window_flag = false; - - if (! FRAME_MINIBUF_ONLY_P (XFRAME (selected_frame))) - last_nonminibuf_frame = XFRAME (selected_frame); - /* If the selected window in the target frame is its mini-window, we move to a different window, the most recently used one, unless there is a valid active minibuffer in the mini-window. */ @@ -1528,6 +1518,16 @@ do_switch_frame (Lisp_Object frame, int for_deletion, Lisp_Object norecord) Fset_frame_selected_window (frame, w, Qnil); } + selected_frame = frame; + + if (f->select_mini_window_flag + && !NILP (Fminibufferp (XWINDOW (f->minibuffer_window)->contents, Qt))) + f->selected_window = f->minibuffer_window; + f->select_mini_window_flag = false; + + if (! FRAME_MINIBUF_ONLY_P (XFRAME (selected_frame))) + last_nonminibuf_frame = XFRAME (selected_frame); + Fselect_window (f->selected_window, norecord); /* We want to make sure that the next event generates a frame-switch