unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* unsolicited patch to image-mode.el -- "fit to window"
@ 2015-07-12 16:50 Greg Minshall
  2015-07-12 16:56 ` Zack Piper
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Greg Minshall @ 2015-07-12 16:50 UTC (permalink / raw)
  To: emacs-devel

hi.  the following patch, from the emacs-24 branch, adds an
(image-transform-fit-to-window) to imagemagick mode, similar to
(image-transform-fit-to-width) and (image-transform-fit-to-height).
and, similar to those, a "Fit to Window" menu item.  basically, you get
the smaller of the (image-transfer-fit-to-height) and
(image-transfer-fit-to-width) (which is typically what i want when using
either of those two functions).

(i also have a much smaller patch to image.el which increases the chance
of using imagemagick mode (as opposed to, e.g., jpeg mode) on an image
file.  i'm happy to submit it if there's any interest.)

apologies if there is any protocol i'm missing; i'm a new kid in town.

cheers, Greg Minshall

ps -- details: possibly i went overboard in the lambda's below...
----
diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 2ed7fbe..8811c72 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -385,6 +385,9 @@ call."
 	["Fit to Window Width" image-transform-fit-to-width
 	 :visible (eq image-type 'imagemagick)
 	 :help "Resize image to match the window width"]
+	["Fit to Window" image-transform-fit-to-window
+	 :visible (eq image-type 'imagemagick)
+	 :help "Resize image to fit in the window"]
 	["Rotate Image..." image-transform-set-rotation
 	 :visible (eq image-type 'imagemagick)
 	 :help "Rotate the image"]
@@ -895,6 +898,7 @@ Its value should be one of the following:
  - nil, meaning no resizing.
  - `fit-height', meaning to fit the image to the window height.
  - `fit-width', meaning to fit the image to the window width.
+ - `fit-window', meaning to fit the image to the window.
  - A number, which is a scale factor (the default size is 1).")
 
 (defvar image-transform-scale 1.0
@@ -1007,17 +1011,18 @@ of a rotated image."
   (when (and (not (numberp image-transform-resize))
 	     (boundp 'image-type)
 	     (eq image-type 'imagemagick))
-    (let ((size (image-display-size (image-get-display-property) t)))
-      (cond ((eq image-transform-resize 'fit-width)
-	     (cl-assert (= (car size)
-			(- (nth 2 (window-inside-pixel-edges))
+    (let ((size (image-display-size (image-get-display-property) t))
+          (width (- (nth 2 (window-inside-pixel-edges))
 			   (nth 0 (window-inside-pixel-edges))))
-		     t))
+          (height (- (nth 3 (window-inside-pixel-edges))
+			   (nth 1 (window-inside-pixel-edges)))))
+      (cond ((eq image-transform-resize 'fit-width)
+	     (cl-assert (= (car size) width) t))
 	    ((eq image-transform-resize 'fit-height)
-	     (cl-assert (= (cdr size)
-			(- (nth 3 (window-inside-pixel-edges))
-			   (nth 1 (window-inside-pixel-edges))))
-		     t))))))
+	     (cl-assert (= (cdr size) height) t) )
+	    ((eq image-transform-resize 'fit-window)
+	     (cl-assert (or (= (car size) width)
+                            (= (cdr size) height)) t))))))
 
 (defun image-transform-properties (spec)
   "Return rescaling/rotation properties for image SPEC.
@@ -1032,7 +1037,26 @@ compiled with ImageMagick support."
 	    (/= image-transform-rotation 0.0))
     ;; Note: `image-size' looks up and thus caches the untransformed
     ;; image.  There's no easy way to prevent that.
-    (let* ((size (image-size spec t))
+    (let* ((size (image-size spec t))   ; (car size) == width;
+                                        ; (cdr size) == height
+           (ifwidth (lambda () 
+                      (image-transform-fit-width
+                       (car size) (cdr size)
+                       (- (nth 2 (window-inside-pixel-edges))
+                          (nth 0 (window-inside-pixel-edges))))))
+           (ifheight (lambda ()
+                       (let ((res (image-transform-fit-width
+                                   (cdr size) (car size)
+                                   (- (nth 3 (window-inside-pixel-edges))
+                                      (nth 1 (window-inside-pixel-edges))))))
+                         (cons (cdr res) (car res)))))
+           (ifwindow (lambda ()
+                       (let ((ifw (funcall ifwidth))
+                             (ifh (funcall ifheight)))
+                         (if (< (/ (float (car ifw)) (car size))
+                                (/ (float (cdr ifh)) (cdr size)))
+                             ifw
+                           ifh))))
 	   (resized
 	    (cond
 	     ((numberp image-transform-resize)
@@ -1040,16 +1064,11 @@ compiled with ImageMagick support."
 		(setq image-transform-scale image-transform-resize)
 		(cons nil (floor (* image-transform-resize (cdr size))))))
 	     ((eq image-transform-resize 'fit-width)
-	      (image-transform-fit-width
-	       (car size) (cdr size)
-	       (- (nth 2 (window-inside-pixel-edges))
-		  (nth 0 (window-inside-pixel-edges)))))
+              (funcall ifwidth))
 	     ((eq image-transform-resize 'fit-height)
-	      (let ((res (image-transform-fit-width
-			  (cdr size) (car size)
-			  (- (nth 3 (window-inside-pixel-edges))
-			     (nth 1 (window-inside-pixel-edges))))))
-		(cons (cdr res) (car res)))))))
+              (funcall ifheight))
+             ((eq image-transform-resize 'fit-window)
+              (funcall ifwindow)))))
       `(,@(when (car resized)
 	    (list :width (car resized)))
 	,@(when (cdr resized)
@@ -1081,6 +1100,14 @@ ImageMagick support."
   (setq image-transform-resize 'fit-width)
   (image-toggle-display-image))
 
+(defun image-transform-fit-to-window ()
+  "Fit the current image to the width of the current window.
+This command has no effect unless Emacs is compiled with
+ImageMagick support."
+  (interactive)
+  (setq image-transform-resize 'fit-window)
+  (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.  This command has no effect unless



^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-08-19 14:17 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-12 16:50 unsolicited patch to image-mode.el -- "fit to window" Greg Minshall
2015-07-12 16:56 ` Zack Piper
2015-07-13  5:19 ` Greg Minshall
2015-07-14 23:23   ` Stefan Monnier
2015-07-23 17:52     ` Greg Minshall
2015-07-23 18:26       ` martin rudalics
2015-07-24  3:46         ` Greg Minshall
2015-07-24  5:30           ` martin rudalics
2015-07-24  9:03             ` Greg Minshall
2015-07-25  8:27               ` martin rudalics
2015-07-25  8:31                 ` martin rudalics
2015-08-19 14:17       ` Ted Zlatanov
2015-08-06 22:28 ` Stefan Monnier

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).