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

From: Juri Linkov <[email-masked]>
Date: Thu, 27 Dec 2018 01:42:54 +0200

Using new hooks I noticed only one problem that when the same image buffer is
displayed in several windows then using `other-window' to switch between them,
the selection-change hook is not always called.

diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 92ba577b4f..0d6a0b8d04 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -574,6 +574,10 @@ image-mode
 
 	(add-hook 'change-major-mode-hook #'image-toggle-display-text nil t)
 	(add-hook 'after-revert-hook #'image-after-revert-hook nil t)
+	(add-hook 'window-size-change-functions (debounce #'image-window-change 1) nil t)
+	(add-hook 'window-state-change-functions (debounce #'image-window-change 1) nil t)
+	(add-hook 'window-selection-change-functions (debounce #'image-window-change 1) nil t)
+
 	(run-mode-hooks 'image-mode-hook)
 	(let ((image (image-get-display-property))
 	      (msg1 (substitute-command-keys
@@ -828,6 +832,19 @@ image-after-revert-hook
           (get-buffer-window-list (current-buffer) 'nomini 'visible))
     (image-toggle-display-image)))
 
+(defun image-window-change (window)
+  (when (and (eq window (selected-window)) (derived-mode-p 'image-mode))
+    (let ((spec (image-get-display-property)))
+      (when (eq (car-safe spec) 'image)
+        (let* ((image-width  (plist-get (cdr spec) :max-width))
+               (image-height (plist-get (cdr spec) :max-height))
+               (window-edges (window-inside-pixel-edges window))
+               (window-width  (- (nth 2 window-edges) (nth 0 window-edges)))
+               (window-height (- (nth 3 window-edges) (nth 1 window-edges))))
+          (when (or (not (= image-width  window-width))
+                    (not (= image-height window-height)))
+            (image-toggle-display-image)))))))
+
 
 ;;; Animated images