Subject: bug#27427: 26.0.50; Native line numbers lead to display error in company-mode popup

From: Eli Zaretskii <[email-masked]>
Date: Fri, 23 Jun 2017 12:10:58 +0300

> Cc: [email-masked], [email-masked]
> From: Dmitry Gutov <[email-masked]>
> Date: Fri, 23 Jun 2017 02:17:24 +0300
> 
> On 6/22/17 5:55 PM, Eli Zaretskii wrote:
> 
> >> Yep! And there are zero outstanding bug reports related to this.
> > 
> > Well, except this one ;-)
> 
> But it's not about line-prefix, it's about a new feature in Emacs that 
> was just introduced in a way that's different from those that came before.

Sorry, I thought that by "related to this" you alluded to the
zero-column-at-BOL assumption.

> > Keeping the numbers out of the margins was my explicit design goal,
> > because some packages want the margins, and we don't have a good
> > solution for "sharing" margins.
> 
> It's not without problems, though, as it introduces a new, different 
> type of margin, doesn't it?

Not really.  The display engine was always inserting stuff at the
beginning of a visual line.  Some features that use this include
line-prefix and wrap-prefix, the left truncation glyphs, and
overlay-arrow.  It's just that AFAIU company-mode has coded
workarounds for each one of them, and now there's one more feature to
deal with.

> Why not put it outside of the window bounds next to the "actual"
> margins? That's one option.

That option requires a thorough redesign of the canvas geometry used
by the display engine: currently there's no area for it to put glyphs
except the 3 existing ones -- the two margins and the text area.

> > So from my POV putting the numbers in
> > the margins would be a step backward.  It will probably also create
> > major havoc for the few packages that do display in the margins,
> 
> Not any more havoc than linum or nlinum which many people install and 
> use, and are apparently fine with them taking up one of the margins.

As long as only one mode uses the margins, no problems indeed.  Once
there are two or more, they need to work around one another, and that
doesn't work very well, sometimes not at all.

> But of course, it would be ideal if you could also introduce a facility 
> that would allow sharing of margins (and hopefully also fringes) between 
> different modes.

Alas, we don't have any consensus for how should this be done.

> > Not sure what you mean by "chromeless" here, but if I understand you
> > correctly, Martin's work is already on master.
> 
> I think so, yes.

I'd urge you to see if company-mode can switch to using these
facilities, as working against the display engine works only up to a
point, and doubtless causes many maintenance headaches.

> >> 1. The first visual line containing the popup has the line number at its
> >> beginning. And as such, the popup line is shifted to the right.
> > 
> > That's the "BOL at non-zero column" issue, right?
> 
> Yes, but also the lack of ability to disable the line numbering on a 
> line-by-line basis (which could be one solution for this problem).

If providing such a facility would solve the problem, it should be
easy to code.  E.g., we could have a display-line-numbers-disable
property which a Lisp program could put on the first visible character
of a visual line, and a non-nil value of that property would signal to
the display engine not to produce the line-number glyphs for that
visual line.  Would that be okay?  (Note that line-number display
produces glyphs even for lines for which no number should be
displayed, such as continuation lines and empty screen lines beyond
EOB, so company-mode would need to decide whether it needs to disable
those as well.  Which is why I wrote "the first visible character of a
visual line" above, emphasis on "visual".)

> Imagine of the line numbers were displayed using line-prefix (I'm not 
> really suggesting that, but...). Then the popup could pick them up and 
> include in the overlay's `display' property text. The user wouldn't see 
> any difference.

You can do similar things with line numbers: Lisp code can count lines
as well as C code, and the fact that the line numbers are being
displayed is easily detected from Lisp.

> Suppose X is non-zero because of line numbering. Then adding an offset 
> of X to the position of the first popup line should fix it.
> 
> What if X is non-zero because of line-prefix, though? If it's because of 
> line-prefix variable, okay, we don't support it.
> 
> What if X is non-zero because of the line-prefix text property? We'd get 
> an "array out of bounds" error somewhere, because the first popup line 
> is currently positioned correctly.
> 
> So when should we add an X offset to the first popup line's position?
> 
> Suppose X is non-nil because of line numbering and the line-prefix text 
> property both? Org uses the line-prefix text property in certain 
> configurations, so it's a real possibility.
> 
> One idea would be to first create an overlay on the first visual line 
> which overrides the line-prefix property to zero, *then* measure
> 
> (car (posn-col-row (posn-at-point (beginning-of-visual-line))))
> 
> , then delete the said overlay and proceed. Doesn't that sound like a 
> mess already?

(And you didn't yet consider the possibility of _both_ line numbers
and line-prefix.  Yes, that should be possible, and I intended that to
be supported.)

Anyway, the mess should be expected in any feature which attempts to
change the display in significant ways, like company-mode does.
Experience shows that this only works up to a point.  Which is why The
Right Way to implement such features is to introduce infrastructure
(on the C level) which will allow the display engine to do the layout
job as those features need.  Native display of line numbers is one
step in that direction, from my POV.  I hope that Martin's work on
child frames will allow company-mode to go in a similar direction as
well.

However, this bug report is about letting the current implementation
of company-mode live in peace with native display of line numbers.  So
if you tell me that the above-mentioned property will allow such a
coexistence, I will code it.

Thanks.