Subject: Re: bug#35421: Also bind left image rotation key

From: "Basil L. Contovounesios" <[email-masked]>
Date: Wed, 17 Jul 2019 00:45:08 +0100

tags 35421 + patch
quit

"Basil L. Contovounesios" <[email-masked]> writes:

> I had a patch prepared for giving image-rotate an optional prefix
> argument, but then I got distracted by some imagemagick vs native
> transformation issues, so I was waiting for the native support to
> stabilise a bit before returning to this in earnest.  Should have
> something to show within a day or two.

With a slight delay due to a camping trip, here it is:

>From 38e676d04f12a69289d4095bb9f61247c4f08ae8 Mon Sep 17 00:00:00 2001
From: "Basil L. Contovounesios" <[email-masked]>
Date: Tue, 16 Jul 2019 22:51:27 +0100
Subject: [PATCH] Allow counter-clockwise rotations in image-rotate

* lisp/image.el (image-rotate): Extend with an optional argument
specifying the rotation in degrees (bug#35421).
* doc/lispref/display.texi (Showing Images):
* etc/NEWS: Document the change.
* test/lisp/image-tests.el (image-rotate): New test.
---
 doc/lispref/display.texi |  3 ++-
 etc/NEWS                 |  5 +++++
 lisp/image.el            | 22 +++++++++++++---------
 test/lisp/image-tests.el | 23 +++++++++++++++++++++++
 4 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi
index a38569f726..4b10788862 100644
--- a/doc/lispref/display.texi
+++ b/doc/lispref/display.texi
@@ -5992,7 +5992,8 @@ Showing Images
 of @samp{4} means to decrease the size by 40%.  The default is 20%.
 
 @item r
-Rotate the image by 90 degrees (@code{image-rotate}).
+Rotate the image by 90 degrees clockwise (@code{image-rotate}).
+A prefix means to rotate by 90 degrees counter-clockwise instead.
 
 @item o
 Save the image to a file (@code{image-save}).
diff --git a/etc/NEWS b/etc/NEWS
index 76ea1df821..4cc30dfcbd 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2237,6 +2237,11 @@ The image parameters 'image-transform-rotation',
 buffer-local, so each buffer could have its own values for these
 parameters.
 
++++
+*** The command 'image-rotate' now accepts a prefix argument.
+With a prefix argument, 'image-rotate' now rotates the image at point
+90 degrees counter-clockwise, instead of the default clockwise.
+
 ** Modules
 
 *** The function 'load' now behaves correctly when loading modules.
diff --git a/lisp/image.el b/lisp/image.el
index b58b1dc954..c3e28655c3 100644
--- a/lisp/image.el
+++ b/lisp/image.el
@@ -1028,16 +1028,20 @@ image--current-scaling
         (display-width (car (image-size image t))))
     (/ (float display-width) image-width)))
 
-(defun image-rotate ()
-  "Rotate the image under point by 90 degrees clockwise."
-  (interactive)
+(defun image-rotate (&optional angle)
+  "Rotate the image under point by ANGLE degrees clockwise.
+If nil, ANGLE defaults to 90.  Interactively, rotate the image 90
+degrees clockwise with no prefix argument, and counter-clockwise
+with a prefix argument.  Note that most image types support
+rotations by only multiples of 90 degrees."
+  (interactive (and current-prefix-arg '(-90)))
   (let ((image (image--get-imagemagick-and-warn)))
-    (plist-put (cdr image) :rotation
-               (float (mod (+ (or (plist-get (cdr image) :rotation) 0) 90)
-                           ;; We don't want to exceed 360 degrees
-                           ;; rotation, because it's not seen as valid
-                           ;; in exif data.
-                           360)))))
+    (setf (image-property image :rotation)
+          (float (mod (+ (or (image-property image :rotation) 0)
+                         (or angle 90))
+                      ;; We don't want to exceed 360 degrees rotation,
+                      ;; because it's not seen as valid in Exif data.
+                      360)))))
 
 (defun image-save ()
   "Save the image under point."
diff --git a/test/lisp/image-tests.el b/test/lisp/image-tests.el
index 5a5b8ea1f7..01c81e3022 100644
--- a/test/lisp/image-tests.el
+++ b/test/lisp/image-tests.el
@@ -21,6 +21,8 @@
 
 (require 'ert)
 (require 'image)
+(eval-when-compile
+  (require 'cl-lib))
 
 (defconst image-tests--emacs-images-directory
   (expand-file-name "../etc/images" (getenv "EMACS_TEST_DIRECTORY"))
@@ -53,4 +55,25 @@ image-type-from-file-header-test
 	       (expand-file-name "splash.svg"
 				 image-tests--emacs-images-directory)))))
 
+(ert-deftest image-rotate ()
+  "Test `image-rotate'."
+  (cl-letf* ((image (list 'image))
+             ((symbol-function 'image--get-imagemagick-and-warn)
+              (lambda () image)))
+    (let ((current-prefix-arg '(4)))
+      (call-interactively #'image-rotate))
+    (should (equal image '(image :rotation 270.0)))
+    (call-interactively #'image-rotate)
+    (should (equal image '(image :rotation 0.0)))
+    (image-rotate)
+    (should (equal image '(image :rotation 90.0)))
+    (image-rotate 0)
+    (should (equal image '(image :rotation 90.0)))
+    (image-rotate 1)
+    (should (equal image '(image :rotation 91.0)))
+    (image-rotate 1234.5)
+    (should (equal image '(image :rotation 245.5)))
+    (image-rotate -154.5)
+    (should (equal image '(image :rotation 91.0)))))
+
 ;;; image-tests.el ends here
-- 
2.20.1

I think this is a good solution for several reasons:

1. It does not require a new key binding, and it does not overly
   complicate the calling convention of the current key binding.

2. It turns image-rotate into a general image-rotating subroutine,
   which users and library authors alike can reuse.

2.1. It does not limit rotations of imagemagick images to multiples of
     90 degrees.

and I don't see any drawbacks, so I would like to push this to master,
subject to comments/objections.

Thanks,

-- 
Basil