Subject: bug#40856: Feature request: support arbitrary propertized strings in wrap-prefix specifications

From: Clément Pit-Claudel <[email-masked]>
Date: Sun, 26 Apr 2020 12:09:31 -0400

On 26/04/2020 11.33, Eli Zaretskii wrote:
>> From: Clément Pit-Claudel <[email-masked]>
>> Date: Sat, 25 Apr 2020 17:24:19 -0400
>>
>> The wrap-prefix property is very convenient, but it's hard to use for alignment when the width of the item to align to is not easy to compute.  For example, to get the following effect, it's easy to use 'wrap-prefix "  "
>>
>> - Test
>>   wrapped
>>   wrapped
>>
>> But take a more complex case, like this:
>>
>> xyz
>> ⟝ abc
>>    wrapped
>>    wrapped
>>
>> Depending on the font in use, the ⟝ character may be more or less wide.  So, the wrap prefix can't just be "  " here; it needs to be calculated using `window-text-pixel-size`, which is costly if it needs to be repeated for many lines.  Additionally, this calculation becomes obsolete and needs to be redone as soon as the user changes the font size.
> 
> wrap-prefix can have value that uses :align-to, so I don't see why
> you'd need to call window-text-pixel-size many times.  You could call> it once, and then use the result in the subsequent values of prefix.

True, if the prefix is the same on every line.
But that calculation isn't very robust to changes in font sizes, other face options, and windows.

>> Specified spaces can already measure the width of an image and many other elements.  Could they be extended to measure an arbitrary string?  Concretely, one would use (:space :width "- ") in the first case and (:space :width "⟝ ") in the second case, and get perfect alignment.
> 
> It should be possible to do that, but I'm not sure it will be cheaper
> than calling window-text-pixel-size or something similar, because
> (unlike with images) Emacs doesn't have the width already calculated
> and stashed somewhere, it would need to lay out the string and
> calculate the resulting pixel width each time it sees such a display
> spec.

I think that would be great; the cost should be reasonable, since 1. it only applies to the part of the buffer that is being displayed and 2. it won't be costlier than using the stirng being measured as the wrap prefix.
IOW, if we expect that it wouldn't be too costly to use a given string as the wrap-prefix, then it shouldn't be too costly to use a specified space of that width as the wrap prefix.

Thanks a lot,
Clément.