[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
master 569d7f6 2/2: New user option image-auto-resize-max-scale-percent
From: |
Stefan Kangas |
Subject: |
master 569d7f6 2/2: New user option image-auto-resize-max-scale-percent |
Date: |
Sat, 6 Nov 2021 15:48:32 -0400 (EDT) |
branch: master
commit 569d7f6a73c939e4f0b9c42cfea95cd3ed5ca8d0
Author: Stefan Kangas <stefan@marxist.se>
Commit: Stefan Kangas <stefan@marxist.se>
New user option image-auto-resize-max-scale-percent
* lisp/image-mode.el (image-auto-resize-max-scale-percent): New
user option to limit how much 'fit-window' will scale up an image.
(image--scale-within-limits-p): New function.
(image-toggle-display-image): Respect above new user option.
---
etc/NEWS | 4 ++++
lisp/image-mode.el | 27 ++++++++++++++++++++++++++-
2 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/etc/NEWS b/etc/NEWS
index 149ddb5..8aba24a 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -395,6 +395,10 @@ The keybinding for 'image-transform-fit-to-width' is now
's i'.
*** User option 'image-auto-resize' can now be set to 'fit-window'.
This works like 'image-transform-fit-to-window'.
+*** New user option 'image-auto-resize-max-scale-percent'.
+The new 'fit-window' options will never scale an image more than this
+much (in percent). It is nil by default.
+
** Image-Dired
+++
diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index a911027..624c852 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -74,6 +74,15 @@ Resizing will always preserve the aspect ratio of the image."
:version "29.1"
:group 'image)
+(defcustom image-auto-resize-max-scale-percent nil
+ "Max size (in percent) to scale up to when `image-auto-resize' is
`fit-window'.
+Can be either a number larger than 100, or nil, which means no
+max size."
+ :type '(choice (const nil "No max")
+ natnum)
+ :version "29.1"
+ :group 'image)
+
(defcustom image-auto-resize-on-window-resize 1
"Non-nil to resize the image whenever the window's dimensions change.
This will always keep the image fit to the window.
@@ -810,6 +819,21 @@ Remove text properties that display the image."
(defvar tar-superior-buffer)
(declare-function image-flush "image.c" (spec &optional frame))
+(defun image--scale-within-limits-p (image)
+ "Return t if `fit-window' will scale image within the customized limits.
+The limits are given by the user option
+`image-auto-resize-max-scale-percent'."
+ (or (not image-auto-resize-max-scale-percent)
+ (let ((scale (/ image-auto-resize-max-scale-percent 100))
+ (mw (plist-get (cdr image) :max-width))
+ (mh (plist-get (cdr image) :max-height))
+ ;; Note: `image-size' looks up and thus caches the
+ ;; untransformed image. There's no easy way to
+ ;; prevent that.
+ (size (image-size image t)))
+ (or (<= mw (* (car size) scale))
+ (<= mh (* (cdr size) scale))))))
+
(defun image-toggle-display-image ()
"Show the image of the image file.
Turn the image data into a real image, but only if the whole file
@@ -893,7 +917,8 @@ was inserted."
:format (and filename data-p))))
;; Handle `fit-window'.
- (when (eq image-transform-resize 'fit-window)
+ (when (and (eq image-transform-resize 'fit-window)
+ (image--scale-within-limits-p image))
(setq image
(cons (car image)
(plist-put (cdr image) :width