unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Auto-resizing of images in image-mode
@ 2020-04-14 18:13 Eli Zaretskii
  2020-04-16 21:53 ` Juri Linkov
  0 siblings, 1 reply; 20+ messages in thread
From: Eli Zaretskii @ 2020-04-14 18:13 UTC (permalink / raw)
  To: Juri Linkov; +Cc: emacs-devel

Lately I noticed that visiting an image file automatically resizes it
to fit in the window, and resizing the window then scales the image to
fit.

This is a good feature (at least IMO), but:

  . It is completely undocumented, AFAICT: not a word about this in
    NEWS, and nothing in the manual.  So much so that I originally
    thought it was some inadvertent side effect of an unrelated
    change.

  . Moreover, the manual makes a point of describing what cursor
    motion commands do when an image is displayed that is larger than
    the window, but this can no longer be seen when visiting an image
    file, because it is always smaller than the window.

  . There's no way to turn this behavior off, so it's practically
    impossible to view an image in its original size (unless you
    manually invoke insert-image).  It is not outlandish to envision
    users who might want to opt out.

  . The time interval after which the idle timer resizes the image is
    fixed and cannot be customized.  The 1 sec interval looks too long
    to me, FWIW, but if it were customizable, my personal preferences
    would not have mattered.

Can we please make this useful feature perfect by fixing these minor
deficiencies?

Thanks.



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

* Re: Auto-resizing of images in image-mode
  2020-04-14 18:13 Auto-resizing of images in image-mode Eli Zaretskii
@ 2020-04-16 21:53 ` Juri Linkov
  2020-04-17  6:45   ` Eli Zaretskii
  0 siblings, 1 reply; 20+ messages in thread
From: Juri Linkov @ 2020-04-16 21:53 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

> Lately I noticed that visiting an image file automatically resizes it
> to fit in the window, and resizing the window then scales the image to
> fit.
>
> This is a good feature (at least IMO), but:
>
>   . It is completely undocumented, AFAICT: not a word about this in
>     NEWS, and nothing in the manual.  So much so that I originally
>     thought it was some inadvertent side effect of an unrelated
>     change.

Strange, I remember adding a NEWS entry for this feature, but now
after checking I see a NEWS entry in b31a966e88 but it's for another
feature installed in 1805b4cc08.  Sorry for messing up.
How about this:

  ** Image mode

  *** 'image-mode' now on resizing the window automatically resizes the
      image to fit in the new dimensions of the window.

>   . Moreover, the manual makes a point of describing what cursor
>     motion commands do when an image is displayed that is larger than
>     the window, but this can no longer be seen when visiting an image
>     file, because it is always smaller than the window.

It was difficult to find this place in the manual.  I failed to find
a node about images in the Table of Contents.  Then I tried other
commands to search the manual for the string "image" and finally found
the node about images in an unexpected place named "File Conveniences".
I propose to create a separate node "Images" or "Image mode".

But still can't find anything about motion commands in this node.

>   . There's no way to turn this behavior off, so it's practically
>     impossible to view an image in its original size (unless you
>     manually invoke insert-image).  It is not outlandish to envision
>     users who might want to opt out.

It's impossible to view an image in its original size even without this feature.
A few years ago image-mode was changed to resize the image to fit in the
window when visiting an image file.  What I added to this in emacs-27 is
only to do the same (fit the image in the window) on window resizing
as well.

There is an option 'image-transform-resize' added a long ago,
but I don't understand its logic: when it's 'nil' then an image
is resized initially after visiting.  And it has this comment:

  ;; FIXME this doesn't seem mature yet. Document in manual when it is.

Maybe a new emacs-27 feature (resize image on window resize) should
depend on the same option using the same logic.  IOW, first we need
to decide how to disable the initial image resizing, then use the
same solution to disable the image resizing on window resize.

>   . The time interval after which the idle timer resizes the image is
>     fixed and cannot be customized.  The 1 sec interval looks too long
>     to me, FWIW, but if it were customizable, my personal preferences
>     would not have mattered.

A new option could be added with a name like image-transform-resize-delay.



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

* Re: Auto-resizing of images in image-mode
  2020-04-16 21:53 ` Juri Linkov
@ 2020-04-17  6:45   ` Eli Zaretskii
  2020-04-18 23:45     ` Juri Linkov
  0 siblings, 1 reply; 20+ messages in thread
From: Eli Zaretskii @ 2020-04-17  6:45 UTC (permalink / raw)
  To: Juri Linkov; +Cc: emacs-devel

> From: Juri Linkov <juri@linkov.net>
> Cc: emacs-devel@gnu.org
> Date: Fri, 17 Apr 2020 00:53:48 +0300
> 
>   ** Image mode
> 
>   *** 'image-mode' now on resizing the window automatically resizes the
>       image to fit in the new dimensions of the window.

Something is wrong with that sentence ;-)

Also, please try to make the first line a complete sentence, so that
reading NEWS in Outline mode could be easier.  And I think we should
also say that the image will resize upon first display and whenever
the window's dimensions change.  This is implied by the text you
wrote, but it could be interpreted to mean the size is done once only,
when the file is visited.

> >   . Moreover, the manual makes a point of describing what cursor
> >     motion commands do when an image is displayed that is larger than
> >     the window, but this can no longer be seen when visiting an image
> >     file, because it is always smaller than the window.
> 
> It was difficult to find this place in the manual.

Sorry, I should have given better instructions.  In my defense, I
thought it was easy to find that place, since I found it immediately
by typing "i images RET" in Info.

> I propose to create a separate node "Images" or "Image mode".

Fine with me, but then let's describe in that node much more
image-related features than we have now.  The Image mode is nowadays
very feature-rich, and most of those features are not in the manual.

> But still can't find anything about motion commands in this node.

Sorry.  It's this text:

                                                     If the displayed
  image is wider or taller than the frame, the usual point motion keys
  (‘C-f’, ‘C-p’, and so forth) cause different parts of the image to be
  displayed.

> >   . There's no way to turn this behavior off, so it's practically
> >     impossible to view an image in its original size (unless you
> >     manually invoke insert-image).  It is not outlandish to envision
> >     users who might want to opt out.
> 
> It's impossible to view an image in its original size even without this feature.
> A few years ago image-mode was changed to resize the image to fit in the
> window when visiting an image file.

Maybe so, but Emacs 26 still shows the images in their original size,
AFAICT.  So this is new in Emacs 27, at least for me.  I think that's
because previously the automatic resizing worked only in Emacs built
with ImageMagick, whereas in Emacs 27 we can resize natively.  I build
my Emacs without ImageMagick, and I wonder how many others do, due to
ImageMagick's instability.

Isn't it possible to have an option or a special command to visit an
image without the automatic resizing?  E.g., "C-u C-x C-f" could do
that for image files.

> What I added to this in emacs-27 is only to do the same (fit the
> image in the window) on window resizing as well.

Well, even if the window-relative resizing was not the cause of the
scaling, I think we should allow to disable such resizing, because in
Emacs 27 much more people will be exposed to this feature.

> There is an option 'image-transform-resize' added a long ago,
> but I don't understand its logic: when it's 'nil' then an image
> is resized initially after visiting.  And it has this comment:
> 
>   ;; FIXME this doesn't seem mature yet. Document in manual when it is.

AFAIU, it was implemented for ImageMagick only, and so it should be
rethought nowadays.

> Maybe a new emacs-27 feature (resize image on window resize) should
> depend on the same option using the same logic.  IOW, first we need
> to decide how to disable the initial image resizing, then use the
> same solution to disable the image resizing on window resize.

That's possible, yes.  My point is that we should have an option to
disable the resizing, so perhaps using this option as that knob will
do.  The resizing could be annoying in some special situations, for
example when the same image is displayed in two adjacent windows --
when you enlarge one window, the image shrinks because the other
window shrinks.

> >   . The time interval after which the idle timer resizes the image is
> >     fixed and cannot be customized.  The 1 sec interval looks too long
> >     to me, FWIW, but if it were customizable, my personal preferences
> >     would not have mattered.
> 
> A new option could be added with a name like image-transform-resize-delay.

Let's do that on the emacs-27 branch, please.

TIA



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

* Re: Auto-resizing of images in image-mode
  2020-04-17  6:45   ` Eli Zaretskii
@ 2020-04-18 23:45     ` Juri Linkov
  2020-04-19  6:58       ` Štěpán Němec
                         ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Juri Linkov @ 2020-04-18 23:45 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

>>   ** Image mode
>>
>>   *** 'image-mode' now on resizing the window automatically resizes the
>>       image to fit in the new dimensions of the window.
>
> Something is wrong with that sentence ;-)
>
> Also, please try to make the first line a complete sentence, so that
> reading NEWS in Outline mode could be easier.  And I think we should
> also say that the image will resize upon first display and whenever
> the window's dimensions change.  This is implied by the text you
> wrote, but it could be interpreted to mean the size is done once only,
> when the file is visited.

Please see in commit 4df8a61117 if everything is correct
according to your comments.

>> I propose to create a separate node "Images" or "Image mode".
>
> Fine with me, but then let's describe in that node much more
> image-related features than we have now.  The Image mode is nowadays
> very feature-rich, and most of those features are not in the manual.

Yes, all its features will be described in the node that I added in the
same commit.

>> >   . Moreover, the manual makes a point of describing what cursor
>> >     motion commands do when an image is displayed that is larger than
>> >     the window, but this can no longer be seen when visiting an image
>> >     file, because it is always smaller than the window.
>
>                                                      If the displayed
>   image is wider or taller than the frame, the usual point motion keys
>   (‘C-f’, ‘C-p’, and so forth) cause different parts of the image to be
>   displayed.

Oh, I looked for the word "larger" but missed its hyponyms
"wider" and "taller", sorry.

>> It's impossible to view an image in its original size even without this feature.
>> A few years ago image-mode was changed to resize the image to fit in the
>> window when visiting an image file.
>
> Maybe so, but Emacs 26 still shows the images in their original size,
> AFAICT.  So this is new in Emacs 27, at least for me.  I think that's
> because previously the automatic resizing worked only in Emacs built
> with ImageMagick, whereas in Emacs 27 we can resize natively.  I build
> my Emacs without ImageMagick, and I wonder how many others do, due to
> ImageMagick's instability.

I'm not sure how it could be new in Emacs 27 because
image-transform-resize was added in year 2014.

> Isn't it possible to have an option or a special command to visit an
> image without the automatic resizing?  E.g., "C-u C-x C-f" could do
> that for image files.

Better would be to have two ways to view an image without the
automatic resizing:

1. an option to disable automatic resizing permanently.
   There is already a defvar 'image-transform-resize'
   but its 'nil' value doesn't disable automatic resizing,
   so a new defcustom is needed.  Please see it below.

2. an easy to type key in image-mode-map to toggle automatic resizing.
   Some programs use 'F' key for "Best Fit" but 'F' is already used
   in image-mode-map for image-goto-frame.

Also there are no keys for image-transform-fit-to-width,
image-transform-fit-to-height and image-transform-reset.
I thought about using a prefix key 'f', so using 'f w' to fit width,
but 'f' is already taken too.

But anyway their docstrings say:

  This command has no effect unless Emacs is compiled with
  ImageMagick support.

So these commands should be fixed to work without ImageMagick.

>> >   . The time interval after which the idle timer resizes the image is
>> >     fixed and cannot be customized.  The 1 sec interval looks too long
>> >     to me, FWIW, but if it were customizable, my personal preferences
>> >     would not have mattered.
>>
>> A new option could be added with a name like image-transform-resize-delay.
>
> Let's do that on the emacs-27 branch, please.

Then three new defcustoms like these could be added:

(defcustom image-resize t
  "Non-nil to resize the image upon first display.
Its value should be one of the following:
 - nil, meaning no resizing.
 - t, meaning to fit the image to the window height and width.
 - `fit-height', meaning to fit the image to the window height.
 - `fit-width', meaning to fit the image to the window width.
 - A number, which is a scale factor (the default size is 1)."
  :type '(choice (const :tag "No resizing" nil)
                 (other :tag "Fit height and width" t)
		 (const :tag "Fit height" fit-height)
		 (const :tag "Fit width" fit-width)
		 (number :tag "Scale factor" 1))
  :version "27.1"
  :group 'image)

(defcustom image-window-resize t
  "Non-nil to resize the image whenever the window's dimensions change."
  :type 'boolean
  :version "27.1"
  :group 'image)

(defcustom image-window-resize-delay 1
  "Number of seconds to wait before resizing according to `image-window-resize'."
  :type 'integer
  :version "27.1"
  :group 'image)

If this looks good, I could send a complete patch.



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

* Re: Auto-resizing of images in image-mode
  2020-04-18 23:45     ` Juri Linkov
@ 2020-04-19  6:58       ` Štěpán Němec
  2020-04-19 23:17         ` Juri Linkov
  2020-04-19 12:34       ` Alan Third
  2020-04-19 14:06       ` Eli Zaretskii
  2 siblings, 1 reply; 20+ messages in thread
From: Štěpán Němec @ 2020-04-19  6:58 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Eli Zaretskii, emacs-devel

On Sun, 19 Apr 2020 02:45:18 +0300
Juri Linkov wrote:

> Better would be to have two ways to view an image without the
> automatic resizing:
>
> 1. an option to disable automatic resizing permanently.
>    There is already a defvar 'image-transform-resize'
>    but its 'nil' value doesn't disable automatic resizing,
>    so a new defcustom is needed.  Please see it below.
>
> 2. an easy to type key in image-mode-map to toggle automatic resizing.
>    Some programs use 'F' key for "Best Fit" but 'F' is already used
>    in image-mode-map for image-goto-frame.

+1

> Also there are no keys for image-transform-fit-to-width,
> image-transform-fit-to-height and image-transform-reset.
> I thought about using a prefix key 'f', so using 'f w' to fit width,
> but 'f' is already taken too.

's' ("scale") is free, used e.g. by the imv image viewer to cycle
between different scaling modes (original size, shrink/fit to window).

-- 
Štěpán



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

* Re: Auto-resizing of images in image-mode
  2020-04-18 23:45     ` Juri Linkov
  2020-04-19  6:58       ` Štěpán Němec
@ 2020-04-19 12:34       ` Alan Third
  2020-04-19 23:18         ` Juri Linkov
  2020-04-19 14:06       ` Eli Zaretskii
  2 siblings, 1 reply; 20+ messages in thread
From: Alan Third @ 2020-04-19 12:34 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Eli Zaretskii, emacs-devel

On Sun, Apr 19, 2020 at 02:45:18AM +0300, Juri Linkov wrote:
> >> It's impossible to view an image in its original size even without this feature.
> >> A few years ago image-mode was changed to resize the image to fit in the
> >> window when visiting an image file.
> >
> > Maybe so, but Emacs 26 still shows the images in their original size,
> > AFAICT.  So this is new in Emacs 27, at least for me.  I think that's
> > because previously the automatic resizing worked only in Emacs built
> > with ImageMagick, whereas in Emacs 27 we can resize natively.  I build
> > my Emacs without ImageMagick, and I wonder how many others do, due to
> > ImageMagick's instability.
> 
> I'm not sure how it could be new in Emacs 27 because
> image-transform-resize was added in year 2014.

Before Emacs 27 any image scaling and rotation required imagemagick.
Emacs 27 can (mostly) handle it without imagemagick now. So yes, this
behaviour is new to people who don’t build with imagemagick.

> But anyway their docstrings say:
> 
>   This command has no effect unless Emacs is compiled with
>   ImageMagick support.
> 
> So these commands should be fixed to work without ImageMagick.

It should generally be a case of removing any checks for imagemagick
since the dependence on it is no longer there. In some cases I think
the docstring says it relies on imagemagick, but the function works in
Emacs 27 without it anyway.

-- 
Alan Third



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

* Re: Auto-resizing of images in image-mode
  2020-04-18 23:45     ` Juri Linkov
  2020-04-19  6:58       ` Štěpán Němec
  2020-04-19 12:34       ` Alan Third
@ 2020-04-19 14:06       ` Eli Zaretskii
  2020-04-19 23:19         ` Juri Linkov
  2 siblings, 1 reply; 20+ messages in thread
From: Eli Zaretskii @ 2020-04-19 14:06 UTC (permalink / raw)
  To: Juri Linkov; +Cc: emacs-devel

> From: Juri Linkov <juri@linkov.net>
> Cc: emacs-devel@gnu.org
> Date: Sun, 19 Apr 2020 02:45:18 +0300
> 
> Please see in commit 4df8a61117 if everything is correct
> according to your comments.

LGTM, thanks.

> > Maybe so, but Emacs 26 still shows the images in their original size,
> > AFAICT.  So this is new in Emacs 27, at least for me.  I think that's
> > because previously the automatic resizing worked only in Emacs built
> > with ImageMagick, whereas in Emacs 27 we can resize natively.  I build
> > my Emacs without ImageMagick, and I wonder how many others do, due to
> > ImageMagick's instability.
> 
> I'm not sure how it could be new in Emacs 27 because
> image-transform-resize was added in year 2014.

It's new for me, because I build Emacs without ImageMagick.

> Better would be to have two ways to view an image without the
> automatic resizing:
> 
> 1. an option to disable automatic resizing permanently.
>    There is already a defvar 'image-transform-resize'
>    but its 'nil' value doesn't disable automatic resizing,
>    so a new defcustom is needed.  Please see it below.
> 
> 2. an easy to type key in image-mode-map to toggle automatic resizing.
>    Some programs use 'F' key for "Best Fit" but 'F' is already used
>    in image-mode-map for image-goto-frame.

That's fine with me, thanks.

> Also there are no keys for image-transform-fit-to-width,
> image-transform-fit-to-height and image-transform-reset.
> I thought about using a prefix key 'f', so using 'f w' to fit width,
> but 'f' is already taken too.
> 
> But anyway their docstrings say:
> 
>   This command has no effect unless Emacs is compiled with
>   ImageMagick support.
> 
> So these commands should be fixed to work without ImageMagick.

Right.  But that's for master, I think.

> Then three new defcustoms like these could be added:
> 
> (defcustom image-resize t
>   "Non-nil to resize the image upon first display.
> Its value should be one of the following:
>  - nil, meaning no resizing.
>  - t, meaning to fit the image to the window height and width.
>  - `fit-height', meaning to fit the image to the window height.
>  - `fit-width', meaning to fit the image to the window width.
>  - A number, which is a scale factor (the default size is 1)."
>   :type '(choice (const :tag "No resizing" nil)
>                  (other :tag "Fit height and width" t)
> 		 (const :tag "Fit height" fit-height)
> 		 (const :tag "Fit width" fit-width)
> 		 (number :tag "Scale factor" 1))
>   :version "27.1"
>   :group 'image)
> 
> (defcustom image-window-resize t
>   "Non-nil to resize the image whenever the window's dimensions change."
>   :type 'boolean
>   :version "27.1"
>   :group 'image)
> 
> (defcustom image-window-resize-delay 1
>   "Number of seconds to wait before resizing according to `image-window-resize'."
>   :type 'integer
>   :version "27.1"
>   :group 'image)
> 
> If this looks good, I could send a complete patch.

Please do, and thanks.



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

* Re: Auto-resizing of images in image-mode
  2020-04-19  6:58       ` Štěpán Němec
@ 2020-04-19 23:17         ` Juri Linkov
  2020-04-20 10:31           ` Štěpán Němec
  2020-04-20 13:09           ` Stefan Monnier
  0 siblings, 2 replies; 20+ messages in thread
From: Juri Linkov @ 2020-04-19 23:17 UTC (permalink / raw)
  To: Štěpán Němec; +Cc: Eli Zaretskii, emacs-devel

>> Also there are no keys for image-transform-fit-to-width,
>> image-transform-fit-to-height and image-transform-reset.
>> I thought about using a prefix key 'f', so using 'f w' to fit width,
>> but 'f' is already taken too.
>
> 's' ("scale") is free, used e.g. by the imv image viewer to cycle
> between different scaling modes (original size, shrink/fit to window).

Thanks for the suggestion.  So I added these keys:

s f		image-mode-fit-frame
s h		image-transform-fit-to-height
s w		image-transform-fit-to-width
s r		image-transform-set-rotation
s 0		image-transform-reset
s s		image-transform-set-scale

But then I discovered that doc-view uses other keys:

s b             doc-view-set-slice-from-bounding-box
s m             doc-view-set-slice-using-mouse
s r             doc-view-reset-slice
s s             doc-view-set-slice

and

F               doc-view-fit-window-to-page
H               doc-view-fit-height-to-window
P               doc-view-fit-page-to-window
W               doc-view-fit-width-to-window

Is it important to maintain compatibility between doc-view and image-mode?

There was only one conflicting key is 'F' that is bound to image-goto-frame
in image-mode-map, and now the prefix 's' is different too.



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

* Re: Auto-resizing of images in image-mode
  2020-04-19 12:34       ` Alan Third
@ 2020-04-19 23:18         ` Juri Linkov
  0 siblings, 0 replies; 20+ messages in thread
From: Juri Linkov @ 2020-04-19 23:18 UTC (permalink / raw)
  To: Alan Third; +Cc: Eli Zaretskii, emacs-devel

> Before Emacs 27 any image scaling and rotation required imagemagick.
> Emacs 27 can (mostly) handle it without imagemagick now. So yes, this
> behaviour is new to people who don’t build with imagemagick.

Aha, this explains everything because I used imagemagick in older versions.

>> But anyway their docstrings say:
>>
>>   This command has no effect unless Emacs is compiled with
>>   ImageMagick support.
>>
>> So these commands should be fixed to work without ImageMagick.
>
> It should generally be a case of removing any checks for imagemagick
> since the dependence on it is no longer there. In some cases I think
> the docstring says it relies on imagemagick, but the function works in
> Emacs 27 without it anyway.

So I removed checks for imagemagick and mentions from docstrings
after confirming that all commands work in Emacs 27 without imagemagick.



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

* Re: Auto-resizing of images in image-mode
  2020-04-19 14:06       ` Eli Zaretskii
@ 2020-04-19 23:19         ` Juri Linkov
  2020-04-19 23:58           ` Juri Linkov
  0 siblings, 1 reply; 20+ messages in thread
From: Juri Linkov @ 2020-04-19 23:19 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

>> Also there are no keys for image-transform-fit-to-width,
>> image-transform-fit-to-height and image-transform-reset.
>> I thought about using a prefix key 'f', so using 'f w' to fit width,
>> but 'f' is already taken too.
>> 
>> But anyway their docstrings say:
>> 
>>   This command has no effect unless Emacs is compiled with
>>   ImageMagick support.
>> 
>> So these commands should be fixed to work without ImageMagick.
>
> Right.  But that's for master, I think.

After testing these commands it became clear that they all already work
without ImageMagick, so I just removed mentions of ImageMagick
from docstrings and these conditionals from menus:

	 :visible (eq image-type 'imagemagick)

So what remains to do in emacs-27 is to add defcustoms.

As for master, there is much work remains to do to fix the mess
what is image-mode currently is:

1. Figure out why there are two rotating commands:
   image-rotate bound to 'r' in image-map, and
   image-transform-set-rotation in image-mode.

2. Try to make sense out of commented code:

    ;; Not yet implemented.
    ;; (defvar image-transform-minor-mode-map
    ;;   (let ((map (make-sparse-keymap)))
    ;;     ;; (define-key map  [(control ?+)] 'image-scale-in)
    ;;     ;; (define-key map  [(control ?-)] 'image-scale-out)
    ;;     ;; (define-key map  [(control ?=)] 'image-scale-none)
    ;;     ;; (define-key map "c f h" 'image-scale-fit-height)
    ;;     ;; (define-key map "c ]" 'image-rotate-right)
    ;;     map)
    ;;   "Minor mode keymap `image-transform-mode'.")
    ;;
    ;; (define-minor-mode image-transform-mode
    ;;   "Minor mode for scaling and rotating images.
    ;; With a prefix argument ARG, enable the mode if ARG is positive,
    ;; and disable it otherwise.  If called from Lisp, enable the mode
    ;; if ARG is omitted or nil.  This minor mode requires Emacs to have
    ;; been compiled with ImageMagick support."
    ;;   nil "image-transform" image-transform-minor-mode-map)

>> (defcustom image-window-resize t
>>   "Non-nil to resize the image whenever the window's dimensions change."
>>   :type 'boolean
>>   :version "27.1"
>>   :group 'image)
>> 
>> (defcustom image-window-resize-delay 1
>>   "Number of seconds to wait before resizing according to `image-window-resize'."
>>   :type 'integer
>>   :version "27.1"
>>   :group 'image)
>> 
>> If this looks good, I could send a complete patch.
>
> Please do, and thanks.

It seems image-window-resize and image-window-resize-delay could be
combined into one defcustom that will be either nil or number, will post
the patch ASAP.



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

* Re: Auto-resizing of images in image-mode
  2020-04-19 23:19         ` Juri Linkov
@ 2020-04-19 23:58           ` Juri Linkov
  2020-04-20 14:39             ` Eli Zaretskii
  0 siblings, 1 reply; 20+ messages in thread
From: Juri Linkov @ 2020-04-19 23:58 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 277 bytes --]

>>> If this looks good, I could send a complete patch.
>>
>> Please do, and thanks.
>
> It seems image-window-resize and image-window-resize-delay could be
> combined into one defcustom that will be either nil or number, will post
> the patch ASAP.

Here is a complete patch:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: image-auto-resize.patch --]
[-- Type: text/x-diff, Size: 5270 bytes --]

diff --git a/lisp/image-mode.el b/lisp/image-mode.el
index 22d7d91314..2d07c59494 100644
--- a/lisp/image-mode.el
+++ b/lisp/image-mode.el
@@ -53,11 +53,38 @@ image-mode-new-window-functions
   "Special hook run when image data is requested in a new window.
 It is called with one argument, the initial WINPROPS.")
 
+(defcustom image-auto-resize t
+  "Non-nil to resize the image upon first display.
+Its value should be one of the following:
+ - nil, meaning no resizing.
+ - t, meaning to fit the image to the window height and width.
+ - `fit-height', meaning to fit the image to the window height.
+ - `fit-width', meaning to fit the image to the window width.
+ - A number, which is a scale factor (the default size is 1)."
+  :type '(choice (const :tag "No resizing" nil)
+                 (other :tag "Fit height and width" t)
+		 (const :tag "Fit height" fit-height)
+		 (const :tag "Fit width" fit-width)
+		 (number :tag "Scale factor" 1))
+  :version "27.1"
+  :group 'image)
+
+(defcustom image-keep-auto-resize 1
+  "Non-nil to resize the image whenever the window's dimensions change.
+This will always keep the image fit into the window.
+When non-nil, the value should be a number of seconds to wait before
+resizing according to the value specified in `image-auto-resize'."
+  :type '(choice (const :tag "No auto-resize on window size change" nil)
+                 (integer :tag "Wait for number of seconds before resize" 1))
+  :version "27.1"
+  :group 'image)
+
 ;; FIXME this doesn't seem mature yet. Document in manual when it is.
 (defvar-local image-transform-resize nil
   "The image resize operation.
 Its value should be one of the following:
  - nil, meaning no resizing.
+ - t, meaning to fit the image to the window height and width.
  - `fit-height', meaning to fit the image to the window height.
  - `fit-width', meaning to fit the image to the window width.
  - A number, which is a scale factor (the default size is 1).")
@@ -418,6 +445,7 @@ image-multi-frame
 
     ;; Transformation keys
     (define-key map "sf" 'image-mode-fit-frame)
+    (define-key map "sb" 'image-transform-fit-both)
     (define-key map "sh" 'image-transform-fit-to-height)
     (define-key map "sw" 'image-transform-fit-to-width)
     (define-key map "sr" 'image-transform-set-rotation)
@@ -459,2 +504,2 @@ image-mode-map
 	 :help "Resize image to match the window height"]
 	["Fit to Window Width" image-transform-fit-to-width
	 :help "Resize image to match the window width"]
+	["Fit to Window Height and Width" image-transform-fit-both
+	 :help "Resize image to match the window height and width"]
 	["Rotate Image..." image-transform-set-rotation
 	 :help "Rotate the image"]
 
@@ -557,6 +599,7 @@ image-mode
 
   (major-mode-suspend)
   (setq major-mode 'image-mode)
+  (setq image-transform-resize image-auto-resize)
 
   (if (not (image-get-display-property))
       (progn
@@ -599,7 +642,8 @@ image-mode--setup-mode
 
   (add-hook 'change-major-mode-hook #'image-toggle-display-text nil t)
   (add-hook 'after-revert-hook #'image-after-revert-hook nil t)
-  (add-hook 'window-state-change-functions #'image--window-state-change nil t)
+  (when image-keep-auto-resize
+    (add-hook 'window-state-change-functions #'image--window-state-change nil t))
 
   (run-mode-hooks 'image-mode-hook)
   (let ((image (image-get-display-property))
@@ -756,7 +800,7 @@ image-toggle-display-image
 	    filename))
 	 ;; If we have a `fit-width' or a `fit-height', don't limit
 	 ;; the size of the image to the window size.
-	 (edges (and (null image-transform-resize)
+	 (edges (and (eq image-transform-resize t)
 		     (window-inside-pixel-edges (get-buffer-window))))
 	 (type (if (image--imagemagick-wanted-p filename)
 		   'imagemagick
@@ -865,7 +910,9 @@ image--window-state-change
   ;; image resizing happens later during redisplay.  So if those
   ;; consecutive calls happen without any redisplay between them,
   ;; the costly operation of image resizing should happen only once.
-  (run-with-idle-timer 1 nil #'image-fit-to-window window))
+  (when (numberp image-keep-auto-resize)
+    (run-with-idle-timer image-keep-auto-resize nil
+                         #'image-fit-to-window window)))
 
 (defun image-fit-to-window (window)
   "Adjust size of image to display it exactly in WINDOW boundaries."
@@ -1268,7 +1314,7 @@ image-transform-properties
 `image-transform-resize' and `image-transform-rotation'.  The
 return value is suitable for appending to an image spec."
   (setq image-transform-scale 1.0)
-  (when (or image-transform-resize
+  (when (or (not (memq image-transform-resize '(nil t)))
 	    (/= image-transform-rotation 0.0))
     ;; Note: `image-size' looks up and thus caches the untransformed
     ;; image.  There's no easy way to prevent that.
@@ -1302,5 +1345,11 @@ image-transform-properties
   (setq image-transform-resize 'fit-width)
   (image-toggle-display-image))
 
+(defun image-transform-fit-both ()
+  "Fit the current image both to the height and width of the current window."
+  (interactive)
+  (setq image-transform-resize t)
+  (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."

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

* Re: Auto-resizing of images in image-mode
  2020-04-19 23:17         ` Juri Linkov
@ 2020-04-20 10:31           ` Štěpán Němec
  2020-04-20 15:29             ` Eli Zaretskii
  2020-04-20 23:44             ` Juri Linkov
  2020-04-20 13:09           ` Stefan Monnier
  1 sibling, 2 replies; 20+ messages in thread
From: Štěpán Němec @ 2020-04-20 10:31 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Eli Zaretskii, emacs-devel

On Mon, 20 Apr 2020 02:17:02 +0300
Juri Linkov wrote:

>>> Also there are no keys for image-transform-fit-to-width,
>>> image-transform-fit-to-height and image-transform-reset.
>>> I thought about using a prefix key 'f', so using 'f w' to fit width,
>>> but 'f' is already taken too.
>>
>> 's' ("scale") is free, used e.g. by the imv image viewer to cycle
>> between different scaling modes (original size, shrink/fit to window).
>
> Thanks for the suggestion.  So I added these keys:
>
> s f		image-mode-fit-frame
> s h		image-transform-fit-to-height
> s w		image-transform-fit-to-width
> s r		image-transform-set-rotation
> s 0		image-transform-reset
> s s		image-transform-set-scale
>
> But then I discovered that doc-view uses other keys:
>
> s b             doc-view-set-slice-from-bounding-box
> s m             doc-view-set-slice-using-mouse
> s r             doc-view-reset-slice
> s s             doc-view-set-slice
>
> and
>
> F               doc-view-fit-window-to-page
> H               doc-view-fit-height-to-window
> P               doc-view-fit-page-to-window
> W               doc-view-fit-width-to-window
>
> Is it important to maintain compatibility between doc-view and image-mode?
>
> There was only one conflicting key is 'F' that is bound to image-goto-frame
> in image-mode-map, and now the prefix 's' is different too.

I see, that's unfortunate.

I don't use doc-view mode, but consistency seems important, esp. given
that its keymap iherits from image-mode-map.

OTOH I really like grouping similar functionality to a common prefix, as
it helps both mnemonics and discoverability/documentation, so I think
I'd prefer changing doc-view mode to follow image-mode's example here;
possibly changing the s prefix to something else (in the parent, i.e.
image-mode) so it doesn't conflict with the slice prefix. But changing
UI defaults is always a delicate matter...

-- 
Štěpán



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

* Re: Auto-resizing of images in image-mode
  2020-04-19 23:17         ` Juri Linkov
  2020-04-20 10:31           ` Štěpán Němec
@ 2020-04-20 13:09           ` Stefan Monnier
  2020-04-21 21:28             ` Juri Linkov
  1 sibling, 1 reply; 20+ messages in thread
From: Stefan Monnier @ 2020-04-20 13:09 UTC (permalink / raw)
  To: Juri Linkov; +Cc: Eli Zaretskii, Štěpán Němec, emacs-devel

> Is it important to maintain compatibility between doc-view and image-mode?

I think it's more important to align the keybinding of those two modes
than it is to preserve existing key bindings.


        Stefan




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

* Re: Auto-resizing of images in image-mode
  2020-04-19 23:58           ` Juri Linkov
@ 2020-04-20 14:39             ` Eli Zaretskii
  2020-04-20 23:43               ` Juri Linkov
  0 siblings, 1 reply; 20+ messages in thread
From: Eli Zaretskii @ 2020-04-20 14:39 UTC (permalink / raw)
  To: Juri Linkov; +Cc: emacs-devel

> From: Juri Linkov <juri@linkov.net>
> Cc: emacs-devel@gnu.org
> Date: Mon, 20 Apr 2020 02:58:31 +0300
> 
> Here is a complete patch:

Thanks, LGTM.

> +(defcustom image-keep-auto-resize 1

I'd name this something like image-auto-resize-on-window-resize.  the
"keep" part has lower mnemonic value, IMO.



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

* Re: Auto-resizing of images in image-mode
  2020-04-20 10:31           ` Štěpán Němec
@ 2020-04-20 15:29             ` Eli Zaretskii
  2020-04-20 23:44             ` Juri Linkov
  1 sibling, 0 replies; 20+ messages in thread
From: Eli Zaretskii @ 2020-04-20 15:29 UTC (permalink / raw)
  To: Štěpán Němec; +Cc: emacs-devel, juri

> From: Štěpán Němec <stepnem@gmail.com>
> Cc: Eli Zaretskii <eliz@gnu.org>,  emacs-devel@gnu.org
> Date: Mon, 20 Apr 2020 12:31:44 +0200
> 
> > F               doc-view-fit-window-to-page
> > H               doc-view-fit-height-to-window
> > P               doc-view-fit-page-to-window
> > W               doc-view-fit-width-to-window
> >
> > Is it important to maintain compatibility between doc-view and image-mode?
> >
> > There was only one conflicting key is 'F' that is bound to image-goto-frame
> > in image-mode-map, and now the prefix 's' is different too.
> 
> I see, that's unfortunate.

I agree, but I don't think it's a catastrophe: after all, doc-view is
not really about showing images.



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

* Re: Auto-resizing of images in image-mode
  2020-04-20 14:39             ` Eli Zaretskii
@ 2020-04-20 23:43               ` Juri Linkov
  2020-04-21 13:51                 ` Eli Zaretskii
  0 siblings, 1 reply; 20+ messages in thread
From: Juri Linkov @ 2020-04-20 23:43 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

>> Here is a complete patch:
>
> Thanks, LGTM.
>
>> +(defcustom image-keep-auto-resize 1
>
> I'd name this something like image-auto-resize-on-window-resize.  the
> "keep" part has lower mnemonic value, IMO.

Now pushed patch to emacs-27 after renaming to image-auto-resize-on-window-resize,
and updated the documentation.  Please check if everything is right in the manual.



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

* Re: Auto-resizing of images in image-mode
  2020-04-20 10:31           ` Štěpán Němec
  2020-04-20 15:29             ` Eli Zaretskii
@ 2020-04-20 23:44             ` Juri Linkov
  1 sibling, 0 replies; 20+ messages in thread
From: Juri Linkov @ 2020-04-20 23:44 UTC (permalink / raw)
  To: Štěpán Němec; +Cc: Eli Zaretskii, emacs-devel

> I don't use doc-view mode, but consistency seems important, esp. given
> that its keymap iherits from image-mode-map.
>
> OTOH I really like grouping similar functionality to a common prefix, as
> it helps both mnemonics and discoverability/documentation, so I think
> I'd prefer changing doc-view mode to follow image-mode's example here;
> possibly changing the s prefix to something else (in the parent, i.e.
> image-mode) so it doesn't conflict with the slice prefix. But changing
> UI defaults is always a delicate matter...

Some unfinished code in image-mode.el

  ;;     ;; (define-key map "c f h" 'image-scale-fit-height)
  ;;     ;; (define-key map "c ]" 'image-rotate-right)

uses the prefix "c", but I see no mnemonics in "c".

There is some mnemonics in the prefix "t" (transform):

  t 0		image-transform-reset
  t b		image-transform-fit-both
  t f		image-mode-fit-frame
  t h		image-transform-fit-to-height
  t r		image-transform-set-rotation
  t s		image-transform-set-scale
  t w		image-transform-fit-to-width

but I'm still not sure if this is the best possible outcome.



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

* Re: Auto-resizing of images in image-mode
  2020-04-20 23:43               ` Juri Linkov
@ 2020-04-21 13:51                 ` Eli Zaretskii
  0 siblings, 0 replies; 20+ messages in thread
From: Eli Zaretskii @ 2020-04-21 13:51 UTC (permalink / raw)
  To: Juri Linkov; +Cc: emacs-devel

> From: Juri Linkov <juri@linkov.net>
> Cc: emacs-devel@gnu.org
> Date: Tue, 21 Apr 2020 02:43:09 +0300
> 
> Now pushed patch to emacs-27 after renaming to image-auto-resize-on-window-resize,
> and updated the documentation.  Please check if everything is right in the manual.

Thanks, I made some minor modifications of the text.



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

* Re: Auto-resizing of images in image-mode
  2020-04-20 13:09           ` Stefan Monnier
@ 2020-04-21 21:28             ` Juri Linkov
  2020-04-22  5:34               ` Tassilo Horn
  0 siblings, 1 reply; 20+ messages in thread
From: Juri Linkov @ 2020-04-21 21:28 UTC (permalink / raw)
  To: Tassilo Horn
  Cc: Eli Zaretskii, Štěpán Němec, Stefan Monnier,
	emacs-devel

>> Is it important to maintain compatibility between doc-view and image-mode?
>
> I think it's more important to align the keybinding of those two modes
> than it is to preserve existing key bindings.

's' is too good prefix key to give up on so easily.
It has nice mnemonics:

s h		set height
s w		set width
s r		set rotation
s s		set scale

So if preserving old keybindings is not a requirement,
then doc-view slice commands could be rebound to the prefix 'c'
that has good mnemonics too, where 'c' means 'cut', 'clip'.

@Tassilo: Do you agree this would be a change for the better?

diff --git a/lisp/doc-view.el b/lisp/doc-view.el
index 8b3d5527f0..a65e06d4d8 100644
--- a/lisp/doc-view.el
+++ b/lisp/doc-view.el
@@ -421,10 +421,10 @@ doc-view-mode-map
     ;; Killing the buffer (and the process)
     (define-key map (kbd "K")         'doc-view-kill-proc)
     ;; Slicing the image
-    (define-key map (kbd "s s")       'doc-view-set-slice)
-    (define-key map (kbd "s m")       'doc-view-set-slice-using-mouse)
-    (define-key map (kbd "s b")       'doc-view-set-slice-from-bounding-box)
-    (define-key map (kbd "s r")       'doc-view-reset-slice)
+    (define-key map (kbd "c s")       'doc-view-set-slice)
+    (define-key map (kbd "c m")       'doc-view-set-slice-using-mouse)
+    (define-key map (kbd "c b")       'doc-view-set-slice-from-bounding-box)
+    (define-key map (kbd "c r")       'doc-view-reset-slice)
     ;; Searching
     (define-key map (kbd "C-s")       'doc-view-search)
     (define-key map (kbd "<find>")    'doc-view-search)



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

* Re: Auto-resizing of images in image-mode
  2020-04-21 21:28             ` Juri Linkov
@ 2020-04-22  5:34               ` Tassilo Horn
  0 siblings, 0 replies; 20+ messages in thread
From: Tassilo Horn @ 2020-04-22  5:34 UTC (permalink / raw)
  To: emacs-devel

Am Di, 21. Apr 2020, um 23:28, schrieb Juri Linkov:
> So if preserving old keybindings is not a requirement,
> then doc-view slice commands could be rebound to the prefix 'c'
> that has good mnemonics too, where 'c' means 'cut', 'clip'.
> 
> @Tassilo: Do you agree this would be a change for the better?

Yes, if that allows for consistent key bindings for all image related functions between doc-view and image-mode, then that's a good thing to do.

Thanks,
Tassilo



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

end of thread, other threads:[~2020-04-22  5:34 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-14 18:13 Auto-resizing of images in image-mode Eli Zaretskii
2020-04-16 21:53 ` Juri Linkov
2020-04-17  6:45   ` Eli Zaretskii
2020-04-18 23:45     ` Juri Linkov
2020-04-19  6:58       ` Štěpán Němec
2020-04-19 23:17         ` Juri Linkov
2020-04-20 10:31           ` Štěpán Němec
2020-04-20 15:29             ` Eli Zaretskii
2020-04-20 23:44             ` Juri Linkov
2020-04-20 13:09           ` Stefan Monnier
2020-04-21 21:28             ` Juri Linkov
2020-04-22  5:34               ` Tassilo Horn
2020-04-19 12:34       ` Alan Third
2020-04-19 23:18         ` Juri Linkov
2020-04-19 14:06       ` Eli Zaretskii
2020-04-19 23:19         ` Juri Linkov
2020-04-19 23:58           ` Juri Linkov
2020-04-20 14:39             ` Eli Zaretskii
2020-04-20 23:43               ` Juri Linkov
2020-04-21 13:51                 ` Eli Zaretskii

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).