emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Auto-resizing of images in image-mode


From: Juri Linkov
Subject: Re: Auto-resizing of images in image-mode
Date: Mon, 20 Apr 2020 02:58:31 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu)

>>> If this looks good, I could send a complete patch.
>>
>> Please do, and thanks.
>
> It seems image-window-resize and image-window-resize-delay could be
> combined into one defcustom that will be either nil or number, will post
> the patch ASAP.

Here is a complete patch:

diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 22d7d91314..2d07c59494 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -53,11 +53,38 @@ image-mode-new-window-functions
   "Special hook run when image data is requested in a new window.
 It is called with one argument, the initial WINPROPS.")
 
+(defcustom image-auto-resize t
+  "Non-nil to resize the image upon first display.
+Its value should be one of the following:
+ - nil, meaning no resizing.
+ - t, meaning to fit the image to the window height and width.
+ - `fit-height', meaning to fit the image to the window height.
+ - `fit-width', meaning to fit the image to the window width.
+ - A number, which is a scale factor (the default size is 1)."
+  :type '(choice (const :tag "No resizing" nil)
+                 (other :tag "Fit height and width" t)
+                (const :tag "Fit height" fit-height)
+                (const :tag "Fit width" fit-width)
+                (number :tag "Scale factor" 1))
+  :version "27.1"
+  :group 'image)
+
+(defcustom image-keep-auto-resize 1
+  "Non-nil to resize the image whenever the window's dimensions change.
+This will always keep the image fit into the window.
+When non-nil, the value should be a number of seconds to wait before
+resizing according to the value specified in `image-auto-resize'."
+  :type '(choice (const :tag "No auto-resize on window size change" nil)
+                 (integer :tag "Wait for number of seconds before resize" 1))
+  :version "27.1"
+  :group 'image)
+
 ;; FIXME this doesn't seem mature yet. Document in manual when it is.
 (defvar-local image-transform-resize nil
   "The image resize operation.
 Its value should be one of the following:
  - nil, meaning no resizing.
+ - t, meaning to fit the image to the window height and width.
  - `fit-height', meaning to fit the image to the window height.
  - `fit-width', meaning to fit the image to the window width.
  - A number, which is a scale factor (the default size is 1).")
@@ -418,6 +445,7 @@ image-multi-frame
 
     ;; Transformation keys
     (define-key map "sf" 'image-mode-fit-frame)
+    (define-key map "sb" 'image-transform-fit-both)
     (define-key map "sh" 'image-transform-fit-to-height)
     (define-key map "sw" 'image-transform-fit-to-width)
     (define-key map "sr" 'image-transform-set-rotation)
@@ -459,2 +504,2 @@ image-mode-map
         :help "Resize image to match the window height"]
        ["Fit to Window Width" image-transform-fit-to-width
         :help "Resize image to match the window width"]
+       ["Fit to Window Height and Width" image-transform-fit-both
+        :help "Resize image to match the window height and width"]
        ["Rotate Image..." image-transform-set-rotation
         :help "Rotate the image"]
 
@@ -557,6 +599,7 @@ image-mode
 
   (major-mode-suspend)
   (setq major-mode 'image-mode)
+  (setq image-transform-resize image-auto-resize)
 
   (if (not (image-get-display-property))
       (progn
@@ -599,7 +642,8 @@ image-mode--setup-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-state-change-functions #'image--window-state-change nil t)
+  (when image-keep-auto-resize
+    (add-hook 'window-state-change-functions #'image--window-state-change nil 
t))
 
   (run-mode-hooks 'image-mode-hook)
   (let ((image (image-get-display-property))
@@ -756,7 +800,7 @@ image-toggle-display-image
            filename))
         ;; If we have a `fit-width' or a `fit-height', don't limit
         ;; the size of the image to the window size.
-        (edges (and (null image-transform-resize)
+        (edges (and (eq image-transform-resize t)
                     (window-inside-pixel-edges (get-buffer-window))))
         (type (if (image--imagemagick-wanted-p filename)
                   'imagemagick
@@ -865,7 +910,9 @@ image--window-state-change
   ;; image resizing happens later during redisplay.  So if those
   ;; consecutive calls happen without any redisplay between them,
   ;; the costly operation of image resizing should happen only once.
-  (run-with-idle-timer 1 nil #'image-fit-to-window window))
+  (when (numberp image-keep-auto-resize)
+    (run-with-idle-timer image-keep-auto-resize nil
+                         #'image-fit-to-window window)))
 
 (defun image-fit-to-window (window)
   "Adjust size of image to display it exactly in WINDOW boundaries."
@@ -1268,7 +1314,7 @@ image-transform-properties
 `image-transform-resize' and `image-transform-rotation'.  The
 return value is suitable for appending to an image spec."
   (setq image-transform-scale 1.0)
-  (when (or image-transform-resize
+  (when (or (not (memq image-transform-resize '(nil t)))
            (/= image-transform-rotation 0.0))
     ;; Note: `image-size' looks up and thus caches the untransformed
     ;; image.  There's no easy way to prevent that.
@@ -1302,5 +1345,11 @@ image-transform-properties
   (setq image-transform-resize 'fit-width)
   (image-toggle-display-image))
 
+(defun image-transform-fit-both ()
+  "Fit the current image both to the height and width of the current window."
+  (interactive)
+  (setq image-transform-resize t)
+  (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."

reply via email to

[Prev in Thread] Current Thread [Next in Thread]