Subject: bug#69565: Fit Image Width menu

From: Juri Linkov <[email-masked]>
Date: Fri, 15 Mar 2024 09:28:37 +0200

> OTOH, I'm worried that both 'image-transform-fit-to-width'
> and 'image-transform-fit-to-height' were declared obsolete.
>
> So I'm not going to revive them.
>
> Instead I'll implement a more useful item
> "Fill Image to Window" that will fill the image
> as much as possible to the window without leaving
> any glaring empty space.  This means applying
> either fit-to-width to vertical images (portrait orientation),
> or fit-to-height to horizontal images (landscape orientation).

This works nicely:

diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 3bf4dd1ad7c..3039d13d9af 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -559,6 +559,8 @@ image-mode-menu
      :help "Resize image to match the window height and width"]
     ["Fit Image to Window (Scale down only)" image-transform-fit-both
      :help "Scale image down to match the window height and width"]
+    ["Fill Window with Image" image-transform-fill-window
+     :help "Resize image to fill either width or height of the window"]
     ["Zoom In" image-increase-size
      :help "Enlarge the image"]
     ["Zoom Out" image-decrease-size
@@ -1591,6 +1593,18 @@ image-transform-fit-to-window
   (setq image-transform-resize 'fit-window)
   (image-toggle-display-image))
 
+(defun image-transform-fill-window ()
+  "Fill the window with the image while keeping image proportions.
+This means filling the window with the image as much as possible
+without leaving empty space around image edges.  Then you can use
+either horizontal or vertical scrolling to see the remaining parts
+of the image."
+  (interactive nil image-mode)
+  (let ((size (image-display-size (image-get-display-property) t)))
+    (setq image-transform-resize
+          (if (> (car size) (cdr size)) 'fit-height 'fit-width)))
+  (image-toggle-display-image))
+
 (defun image-transform-set-rotation (rotation)
   "Prompt for an angle ROTATION, and rotate the image by that amount.
 ROTATION should be in degrees."