Subject: Re: bug#38187: 27.0.50; No mouse-wheel scaling on images

From: Juri Linkov <[email-masked]>
Date: Mon, 18 Nov 2019 23:37:15 +0200

>> In image-mode currently ‘+’ is bound to the command ‘image-increase-size’,
>> the same command could be bound to ‘<C-mouse-4>’ (C-wheel-up) as well.
>> Same for ‘-’ (image-decrease-size) and ‘<C-mouse-5>’ (C-wheel-down).
>
> Right.  I think that's an obvious thing to put in `image-map' to make it
> easier to change the size of a single image.
>
> (It's not just in image-mode -- that map is on all images inserted by
> `insert-image'.)
>
> I don't have a mouse myself, so it'd be nice if somebody with one
> created a patch so that they could test it.  image-map is in image.el.

I tested this patch, and it works well:

diff --git a/lisp/image.el b/lisp/image.el
index ad2ee6c607..8e8e477865 100644
--- a/lisp/image.el
+++ b/lisp/image.el
@@ -158,6 +158,10 @@ image-map
   (let ((map (make-sparse-keymap)))
     (define-key map "-" 'image-decrease-size)
     (define-key map "+" 'image-increase-size)
+    (define-key map [C-wheel-down] 'image-decrease-size)
+    (define-key map [C-mouse-5]    'image-decrease-size)
+    (define-key map [C-wheel-up]   'image-increase-size)
+    (define-key map [C-mouse-4]    'image-increase-size)
     (define-key map "r" 'image-rotate)
     (define-key map "o" 'image-save)
     map))
@@ -993,23 +997,33 @@ imagemagick-enabled-types
 
 (imagemagick-register-types)
 
-(defun image-increase-size (n)
+(defun image-increase-size (&optional n event)
   "Increase the image size by a factor of N.
 If N is 3, then the image size will be increased by 30%.  The
 default is 20%."
-  (interactive "P")
-  (image--change-size (if n
-                          (1+ (/ n 10.0))
-                        1.2)))
+  (interactive (list current-prefix-arg last-nonmenu-event))
+  (let ((e (and (listp event) (event-start event))))
+    (save-window-excursion
+      (when e
+        (select-window (posn-window e))
+        (goto-char (posn-point e)))
+      (image--change-size (if n
+                              (1+ (/ (prefix-numeric-value n) 10.0))
+                            1.2)))))
 
-(defun image-decrease-size (n)
+(defun image-decrease-size (&optional n event)
   "Decrease the image size by a factor of N.
 If N is 3, then the image size will be decreased by 30%.  The
 default is 20%."
-  (interactive "P")
-  (image--change-size (if n
-                          (- 1 (/ n 10.0))
-                        0.8)))
+  (interactive (list current-prefix-arg last-nonmenu-event))
+  (let ((e (and (listp event) (event-start event))))
+    (save-window-excursion
+      (when e
+        (select-window (posn-window e))
+        (goto-char (posn-point e)))
+      (image--change-size (if n
+                              (- 1 (/ (prefix-numeric-value n) 10.0))
+                            0.8)))))
 
 (defun image--get-image ()
   "Return the image at point."