aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJuri Linkov2020-04-21 02:42:16 +0300
committerJuri Linkov2020-04-21 02:42:16 +0300
commita64da75961fbce7dc071af37058de710bb13c26e (patch)
treed9fb5d483a42b00bd56b8b9abf4d79b6a37d1a41
parent692ad40539805e435a16b90067fa5917e4fea9f8 (diff)
downloademacs-a64da75961fbce7dc071af37058de710bb13c26e.tar.gz
emacs-a64da75961fbce7dc071af37058de710bb13c26e.zip
Add image-auto-resize defcustoms to image-mode.el
* lisp/image-mode.el (image-auto-resize) (image-auto-resize-on-window-resize): New defcustoms. (image-mode-map): Bind "sb" to image-transform-fit-both. (image-mode): Set image-transform-resize to image-auto-resize initially. (image-mode--setup-mode): Add hook on image-auto-resize-on-window-resize. (image-toggle-display-image): Check if image-transform-resize is t. (image-transform-properties): Check image-transform-resize for nil and t. (image-transform-fit-both): New command. (image-transform-reset): Reset image-transform-resize to image-auto-resize. * doc/emacs/files.texi (Image Mode): Mention image-auto-resize and image-auto-resize-on-window-resize. https://lists.gnu.org/archive/html/emacs-devel/2020-04/msg01160.html
-rw-r--r--doc/emacs/files.texi18
-rw-r--r--etc/NEWS5
-rw-r--r--lisp/image-mode.el50
3 files changed, 61 insertions, 12 deletions
diff --git a/doc/emacs/files.texi b/doc/emacs/files.texi
index 7d57555ce33..8d75b569edb 100644
--- a/doc/emacs/files.texi
+++ b/doc/emacs/files.texi
@@ -2122,12 +2122,18 @@ to toggle between displaying the file as an image in the Emacs buffer,
2122and displaying its underlying text (or raw byte) representation. 2122and displaying its underlying text (or raw byte) representation.
2123Additionally you can type @kbd{C-c C-x} (@code{image-toggle-hex-display}) 2123Additionally you can type @kbd{C-c C-x} (@code{image-toggle-hex-display})
2124to toggle between displaying the file as an image in the Emacs buffer, 2124to toggle between displaying the file as an image in the Emacs buffer,
2125and displaying it in hex representation. 2125and displaying it in hex representation. Displaying the file as an
2126Displaying the file as an image works only if Emacs is compiled with 2126image works only if Emacs is compiled with support for displaying
2127support for displaying such images. If the displayed image is wider 2127such images.
2128or taller than the frame, the usual point motion keys (@kbd{C-f}, 2128
2129@kbd{C-p}, and so forth) cause different parts of the image to be 2129If the displayed image is wider or taller than the frame, the usual
2130displayed. You can press @kbd{n} (@code{image-next-file}) and @kbd{p} 2130point motion keys (@kbd{C-f}, @kbd{C-p}, and so forth) cause different
2131parts of the image to be displayed. But by default the image is
2132resized automatically to fit to the window. You can configure this by
2133using two options @code{image-auto-resize} and
2134@code{image-auto-resize-on-window-resize}.
2135
2136You can press @kbd{n} (@code{image-next-file}) and @kbd{p}
2131(@code{image-previous-file}) to visit the next image file and the 2137(@code{image-previous-file}) to visit the next image file and the
2132previous image file in the same directory, respectively. 2138previous image file in the same directory, respectively.
2133 2139
diff --git a/etc/NEWS b/etc/NEWS
index fe8a8d8775b..1d630a3e91b 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -3523,9 +3523,12 @@ functions.
3523*** 'image-mode' now uses this library to automatically rotate images 3523*** 'image-mode' now uses this library to automatically rotate images
3524according to the orientation in the Exif data, if any. 3524according to the orientation in the Exif data, if any.
3525 3525
3526+++
3526*** In 'image-mode' the image is resized automatically to fit in window. 3527*** In 'image-mode' the image is resized automatically to fit in window.
3527The image will resize upon first display and whenever the window's 3528The image will resize upon first display and whenever the window's
3528dimensions change. 3529dimensions change. Two user options 'image-auto-resize' and
3530'image-auto-resize-on-window-resize' can define resizing parameters or
3531disable auto-resizing.
3529 3532
3530--- 3533---
3531*** New library image-converter. 3534*** New library image-converter.
diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index fbce1193cd0..6ce4e74c7ed 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -53,11 +53,38 @@ See `image-mode-winprops'.")
53 "Special hook run when image data is requested in a new window. 53 "Special hook run when image data is requested in a new window.
54It is called with one argument, the initial WINPROPS.") 54It is called with one argument, the initial WINPROPS.")
55 55
56(defcustom image-auto-resize t
57 "Non-nil to resize the image upon first display.
58Its value should be one of the following:
59 - nil, meaning no resizing.
60 - t, meaning to fit the image to the window height and width.
61 - `fit-height', meaning to fit the image to the window height.
62 - `fit-width', meaning to fit the image to the window width.
63 - A number, which is a scale factor (the default size is 1)."
64 :type '(choice (const :tag "No resizing" nil)
65 (other :tag "Fit height and width" t)
66 (const :tag "Fit height" fit-height)
67 (const :tag "Fit width" fit-width)
68 (number :tag "Scale factor" 1))
69 :version "27.1"
70 :group 'image)
71
72(defcustom image-auto-resize-on-window-resize 1
73 "Non-nil to resize the image whenever the window's dimensions change.
74This will always keep the image fit to the window.
75When non-nil, the value should be a number of seconds to wait before
76resizing according to the value specified in `image-auto-resize'."
77 :type '(choice (const :tag "No auto-resize on window size change" nil)
78 (integer :tag "Wait for number of seconds before resize" 1))
79 :version "27.1"
80 :group 'image)
81
56;; FIXME this doesn't seem mature yet. Document in manual when it is. 82;; FIXME this doesn't seem mature yet. Document in manual when it is.
57(defvar-local image-transform-resize nil 83(defvar-local image-transform-resize nil
58 "The image resize operation. 84 "The image resize operation.
59Its value should be one of the following: 85Its value should be one of the following:
60 - nil, meaning no resizing. 86 - nil, meaning no resizing.
87 - t, meaning to fit the image to the window height and width.
61 - `fit-height', meaning to fit the image to the window height. 88 - `fit-height', meaning to fit the image to the window height.
62 - `fit-width', meaning to fit the image to the window width. 89 - `fit-width', meaning to fit the image to the window width.
63 - A number, which is a scale factor (the default size is 1).") 90 - A number, which is a scale factor (the default size is 1).")
@@ -425,6 +452,7 @@ call."
425 452
426 ;; Transformation keys 453 ;; Transformation keys
427 (define-key map "sf" 'image-mode-fit-frame) 454 (define-key map "sf" 'image-mode-fit-frame)
455 (define-key map "sb" 'image-transform-fit-both)
428 (define-key map "sh" 'image-transform-fit-to-height) 456 (define-key map "sh" 'image-transform-fit-to-height)
429 (define-key map "sw" 'image-transform-fit-to-width) 457 (define-key map "sw" 'image-transform-fit-to-width)
430 (define-key map "sr" 'image-transform-set-rotation) 458 (define-key map "sr" 'image-transform-set-rotation)
@@ -482,6 +510,8 @@ call."
482 :help "Resize image to match the window height"] 510 :help "Resize image to match the window height"]
483 ["Fit to Window Width" image-transform-fit-to-width 511 ["Fit to Window Width" image-transform-fit-to-width
484 :help "Resize image to match the window width"] 512 :help "Resize image to match the window width"]
513 ["Fit to Window Height and Width" image-transform-fit-both
514 :help "Resize image to match the window height and width"]
485 ["Rotate Image..." image-transform-set-rotation 515 ["Rotate Image..." image-transform-set-rotation
486 :help "Rotate the image"] 516 :help "Rotate the image"]
487 ["Reset Transformations" image-transform-reset 517 ["Reset Transformations" image-transform-reset
@@ -569,6 +599,7 @@ Key bindings:
569 599
570 (major-mode-suspend) 600 (major-mode-suspend)
571 (setq major-mode 'image-mode) 601 (setq major-mode 'image-mode)
602 (setq image-transform-resize image-auto-resize)
572 603
573 (if (not (image-get-display-property)) 604 (if (not (image-get-display-property))
574 (progn 605 (progn
@@ -611,7 +642,8 @@ Key bindings:
611 642
612 (add-hook 'change-major-mode-hook #'image-toggle-display-text nil t) 643 (add-hook 'change-major-mode-hook #'image-toggle-display-text nil t)
613 (add-hook 'after-revert-hook #'image-after-revert-hook nil t) 644 (add-hook 'after-revert-hook #'image-after-revert-hook nil t)
614 (add-hook 'window-state-change-functions #'image--window-state-change nil t) 645 (when image-auto-resize-on-window-resize
646 (add-hook 'window-state-change-functions #'image--window-state-change nil t))
615 647
616 (run-mode-hooks 'image-mode-hook) 648 (run-mode-hooks 'image-mode-hook)
617 (let ((image (image-get-display-property)) 649 (let ((image (image-get-display-property))
@@ -768,7 +800,7 @@ was inserted."
768 filename)) 800 filename))
769 ;; If we have a `fit-width' or a `fit-height', don't limit 801 ;; If we have a `fit-width' or a `fit-height', don't limit
770 ;; the size of the image to the window size. 802 ;; the size of the image to the window size.
771 (edges (and (null image-transform-resize) 803 (edges (and (eq image-transform-resize t)
772 (window-inside-pixel-edges (get-buffer-window)))) 804 (window-inside-pixel-edges (get-buffer-window))))
773 (type (if (image--imagemagick-wanted-p filename) 805 (type (if (image--imagemagick-wanted-p filename)
774 'imagemagick 806 'imagemagick
@@ -878,7 +910,9 @@ Otherwise, display the image by calling `image-mode'."
878 ;; image resizing happens later during redisplay. So if those 910 ;; image resizing happens later during redisplay. So if those
879 ;; consecutive calls happen without any redisplay between them, 911 ;; consecutive calls happen without any redisplay between them,
880 ;; the costly operation of image resizing should happen only once. 912 ;; the costly operation of image resizing should happen only once.
881 (run-with-idle-timer 1 nil #'image-fit-to-window window)) 913 (when (numberp image-auto-resize-on-window-resize)
914 (run-with-idle-timer image-auto-resize-on-window-resize nil
915 #'image-fit-to-window window)))
882 916
883(defun image-fit-to-window (window) 917(defun image-fit-to-window (window)
884 "Adjust size of image to display it exactly in WINDOW boundaries." 918 "Adjust size of image to display it exactly in WINDOW boundaries."
@@ -1282,7 +1316,7 @@ These properties are determined by the Image mode variables
1282`image-transform-resize' and `image-transform-rotation'. The 1316`image-transform-resize' and `image-transform-rotation'. The
1283return value is suitable for appending to an image spec." 1317return value is suitable for appending to an image spec."
1284 (setq image-transform-scale 1.0) 1318 (setq image-transform-scale 1.0)
1285 (when (or image-transform-resize 1319 (when (or (not (memq image-transform-resize '(nil t)))
1286 (/= image-transform-rotation 0.0)) 1320 (/= image-transform-rotation 0.0))
1287 ;; Note: `image-size' looks up and thus caches the untransformed 1321 ;; Note: `image-size' looks up and thus caches the untransformed
1288 ;; image. There's no easy way to prevent that. 1322 ;; image. There's no easy way to prevent that.
@@ -1328,6 +1362,12 @@ return value is suitable for appending to an image spec."
1328 (setq image-transform-resize 'fit-width) 1362 (setq image-transform-resize 'fit-width)
1329 (image-toggle-display-image)) 1363 (image-toggle-display-image))
1330 1364
1365(defun image-transform-fit-both ()
1366 "Fit the current image both to the height and width of the current window."
1367 (interactive)
1368 (setq image-transform-resize t)
1369 (image-toggle-display-image))
1370
1331(defun image-transform-set-rotation (rotation) 1371(defun image-transform-set-rotation (rotation)
1332 "Prompt for an angle ROTATION, and rotate the image by that amount. 1372 "Prompt for an angle ROTATION, and rotate the image by that amount.
1333ROTATION should be in degrees." 1373ROTATION should be in degrees."
@@ -1338,7 +1378,7 @@ ROTATION should be in degrees."
1338(defun image-transform-reset () 1378(defun image-transform-reset ()
1339 "Display the current image with the default size and rotation." 1379 "Display the current image with the default size and rotation."
1340 (interactive) 1380 (interactive)
1341 (setq image-transform-resize nil 1381 (setq image-transform-resize image-auto-resize
1342 image-transform-rotation 0.0 1382 image-transform-rotation 0.0
1343 image-transform-scale 1) 1383 image-transform-scale 1)
1344 (image-toggle-display-image)) 1384 (image-toggle-display-image))