Subject: Re: bug#75776: 30.0.50; Displaying image slice in the right margin

From: Eli Zaretskii <[email-masked]>
Date: Thu, 23 Jan 2025 12:01:43 +0200

> From: Evgeny Zajcev <[email-masked]>
> Date: Thu, 23 Jan 2025 10:10:05 +0300
> 
> I'm having problem displaying an image slice in the right margin.  To
> reproduce this, download
> https://www.gnu.org/software/emacs/images/emacs.png to your ~/Downloads
> folder.  Activate right margin with M-x visual-fill-column RET.  And consider three pieces of code:
> 
> 1. (insert
>      (propertize "E" 'display `((slice 0 0 1.0 20) ,(create-image
>      "~/Downloads/emacs.png" nil nil :scale 1.0))))
> 
>    Displays image slice in the buffer as expected.
> 
> 2. (insert
>      (propertize "E" 'display `((margin right-margin) ,(create-image
>      "~/Downloads/emacs.png" nil nil :scale 1.0))))
>   
>    Displays image in the right margin as expected.
> 
> 3. (insert
>      (propertize "E" 'display `((margin right-margin)
>       ((slice 0 0 1.0 20)
>       ,(create-image "~/Downloads/emacs.png" nil nil :scale 1.0)))))
> 
>    Displays "E"
> 
> Documentation say that ((margin right-margin) SPEC) can be used to
> display image in the right margin, and SPEC is totally valid in the
> 3. because 2. works.
> 
> Is this a bug?

You need to construct the display property in a different order:

  (insert
     (propertize "E" 'display
                     `((slice 0 0 1.0 20)
		       ((margin left-margin)
		        ,(create-image "~/Downloads/emacs.png"
			  nil nil :scale 1.0)))))

This might be confusing, but the root cause is that 'slice' is not
valid in an image descriptor, it's a display spec.  Whereas the
documentation of marginal display says:

     A margin display specification looks like ‘((margin right-margin)
  SPEC)’ or ‘((margin left-margin) SPEC)’.  Here, SPEC is another display
  specification that says what to display in the margin.  Typically it is
  a string of text to display, or an image descriptor.

So Emacs wants an image descriptor, and that cannot include 'slice'.

In addition, you should use set-window-margins to ensure the margin is
wide enough to allow the display of the slice, because the Emacs
display engine crops images to the width of the available horizontal
screen estate.  So if the margin is not wide enough, you will see
empty space with your slice parameters, since the top-level corner of
the emacs.png image is just empty space.  I assume your real-life use
includes setting the window margins, because otherwise you will se no
margins at all.

Let me also point out the existing function insert-sliced-image, which
you could use to avoid all this low-level stuff.