Subject: Re: bug#32672: 27.0.50; image resize on window resizing

From: martin rudalics <[email-masked]>
Date: Sun, 16 Sep 2018 11:10:38 +0200

> I agree that the buffer list is more global than a window, but OTOH
 > it makes sense to use buffer-local hook for a buffer to get notified
 > when it comes to the top of the buffer list.  So this would be a good thing
 > to have, unless there is another hook that gets called when a buffer
 > becomes the current buffer, or at least displayed in a window.

I try to fix the problem you mentioned earlier, namely that

 > > it's called only when navigating away from the
 > > image-mode buffer, but not when coming back to the image-mode buffer, e.g.
 > > `C-x <left>', then resize the window, then type `C-x <right>',
 > > it's not called.  Also it's not called in another direction:
 > > `C-x <right>', then resize the window, then type `C-x <left>'.

Please tell me if the attached patch works and what else should be
done.

Thanks, martin
--- a/lisp/window.el
+++ b/lisp/window.el
@@ -4301,9 +4301,9 @@ set-window-buffer-start-and-point
   (setq window (window-normalize-window window t))
   (let ((selected (eq window (selected-window)))
 	(current (eq (window-buffer window) (current-buffer))))
-    (set-window-buffer window buffer)
     (when (and selected current)
       (set-buffer buffer))
+    (set-window-buffer window buffer)
     (when start
       ;; Don't force window-start here (even if POINT is nil).
       (set-window-start window start t))
diff --git a/src/window.c b/src/window.c
index 409b01f..33e1975 100644
--- a/src/window.c
+++ b/src/window.c
@@ -3573,40 +3573,36 @@ depends on the value of (window-start WINDOW), so if calling this
 
 This function runs `window-scroll-functions' before running
 `window-configuration-change-hook'.  */)
-  (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
+     (Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
 {
-  register Lisp_Object tem, buffer;
-  register struct window *w = decode_live_window (window);
+  Lisp_Object old_buffer, new_buffer;
+  struct window *w = decode_live_window (window);
 
   XSETWINDOW (window, w);
-  buffer = Fget_buffer (buffer_or_name);
-  CHECK_BUFFER (buffer);
-  if (!BUFFER_LIVE_P (XBUFFER (buffer)))
+  old_buffer = w->contents;
+  new_buffer = Fget_buffer (buffer_or_name);
+  CHECK_BUFFER (new_buffer);
+  if (!BUFFER_LIVE_P (XBUFFER (new_buffer)))
     error ("Attempt to display deleted buffer");
 
-  tem = w->contents;
-  if (NILP (tem))
-    error ("Window is deleted");
-  else
+  if (!EQ (old_buffer, new_buffer))
     {
-      if (!EQ (tem, buffer))
-	{
-	  if (EQ (w->dedicated, Qt))
-	    /* WINDOW is strongly dedicated to its buffer, signal an
-	       error.  */
-	    error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
-	  else
-	    /* WINDOW is weakly dedicated to its buffer, reset
-	       dedication.  */
-	    wset_dedicated (w, Qnil);
-
-	  call1 (Qrecord_window_buffer, window);
-	}
+      if (EQ (w->dedicated, Qt))
+	/* WINDOW is strongly dedicated to its buffer, signal an
+	   error.  */
+	error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (old_buffer), name)));
+      else
+	/* WINDOW is weakly dedicated to its buffer, reset
+	   dedication.  */
+	wset_dedicated (w, Qnil);
 
       unshow_buffer (w);
     }
 
-  set_window_buffer (window, buffer, true, !NILP (keep_margins));
+  set_window_buffer (window, new_buffer, true, !NILP (keep_margins));
+
+  if (!EQ (old_buffer, new_buffer))
+    call1 (Qrecord_window_buffer, window);
 
   return Qnil;
 }