Subject: bug#32777: 27.0.50; window-buffer gets wrong point

From: martin rudalics <[email-masked]>
Date: Sun, 23 Dec 2018 10:40:13 +0100

> As for how to proceed: I would love for us to have a way of selecting
 > a window temporarily, in way that doesn't involve saving/restoring its
 > window-point.

It depends on what your semantics of "selecting a window temporarily"
are.  The only invariance frame/window management requires is that
(frame-selected-window (selected-frame)) equals (selected-window).
Otherwise you can freely set things like selected_window and
selected_frame directly.

Things get problematic when you want to, for example, show the line
number of a buffer in the mode line since in that case you need the
corresponding window's point which is stored in the window structure
only if the window is not the selected one.  So the most important
prerequisite for what you want is that redisplay itself always has a
very strong opinion of which window is the selected one and which
buffer is current.

One of the most unexpected things redisplay currently does is to run
Fselect_frame from unwind_format_mode_line which if I'm not mistaken
could wind up calling do_switch_frame with TRACK non-zero and thus
cause a Fredirect_frame_focus from within redisplay.  I think that
redisplay should not call 'select-frame' or 'select-window' for such
reasons.

 > But maybe this is a pipe dream, so I think we should
 > try your alternative #2 on master and see what it breaks.

To elaborate further on what I stated there as

 >> But (1) does not handle the case where the
 >> buffer of W2 is not shown anywhere else when x_consider_frame_title is
 >> called.  So IMHO (2) should be the correct approach but I have no idea
 >> what further consequences it could have.

consider the following scenario with emacs -Q:

C-x 5 2

C-x 5 o

C-x b RET

M-: (with-current-buffer "*scratch*" (goto-char (point-min)))

M-: (with-current-buffer "*scratch*" (point))

The value of evaluating the second form depends on the position of
point of the window displaying *scratch* and not on where the
'goto-char' went before.  So this is a quite nasty bug IMHO.

Unfortunately, curing it is non-trivial to avoid that some window
point gets moved unexpectedly, see the attached patch.  People using
multiple frames pretty please try to run it and report any bad
experiences here ASAP.

Thanks, martin
diff --git a/src/xdisp.c b/src/xdisp.c
index 94742c2..315dce2 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -11860,7 +11860,7 @@ static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
   Vmode_line_unwind_vector = Qnil;
 
   if (NILP (vector))
-    vector = make_nil_vector (10);
+    vector = make_nil_vector (12);
 
   ASET (vector, 0, make_fixnum (mode_line_target));
   ASET (vector, 1, make_fixnum (MODE_LINE_NOPROP_LEN (0)));
@@ -11877,12 +11877,24 @@ static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
   ASET (vector, 7, owin);
   if (target_frame)
     {
+      Lisp_Object buffer = XWINDOW (target_frame->selected_window)->contents;
+      struct buffer *b = XBUFFER (buffer);
+      struct buffer *cb = current_buffer;
+
       /* Similarly to `with-selected-window', if the operation selects
 	 a window on another frame, we must restore that frame's
 	 selected window, and (for a tty) the top-frame.  */
       ASET (vector, 8, target_frame->selected_window);
       if (FRAME_TERMCAP_P (target_frame))
 	ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
+
+      /* If we select a window on another frame, make sure that that
+	 selection does not leave its buffer's point modified when
+	 unwinding (Bug#32777).  */
+      ASET (vector, 10, buffer);
+      current_buffer = b;
+      ASET (vector, 11, build_marker (current_buffer, PT, PT_BYTE));
+      current_buffer = cb;
     }
 
   return vector;
@@ -11913,12 +11925,26 @@ static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
 	{
 	  Lisp_Object frame
 	    = WINDOW_FRAME (XWINDOW (target_frame_window));
+	  Lisp_Object buffer = AREF (vector, 10);
 
 	  if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
 	    Fselect_window (target_frame_window, Qt);
 
 	  if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
 	    Fselect_frame (old_top_frame, Qt);
+
+	  if (BUFFER_LIVE_P (XBUFFER (buffer))
+	      && !EQ (XWINDOW (old_window)->contents, buffer))
+	    {
+	      /* Restore point of target_frame_window's buffer
+		 (Bug#32777).  */
+	      struct buffer *cb = current_buffer;
+
+	      current_buffer = XBUFFER (buffer);
+	      set_point_from_marker (AREF (vector, 11));
+	      ASET (vector, 11, Qnil);
+	      current_buffer = cb;
+	    }
 	}
 
       Fselect_window (old_window, Qt);