Subject: Re: bug#18385: 24.3.93; posn-at-point doesn't account for tab-width
From: Eli Zaretskii <[email-masked]>
To: Dmitry <[email-masked]>
Cc: [email-masked]
Date: Tue, 02 Sep 2014 18:24:12 +0300
List-Id: <debbugs-submit.debbugs.gnu.org>

> From: Dmitry <[email-masked]>
> Date: Tue, 02 Sep 2014 01:53:12 +0400
> 
> 1. New, empty buffer.
> 2. (insert "a\tb")
> 3. (posn-actual-col-row (posn-at-point))
> => (3 . 0)
> 
> It should probably return (9 . 0).

No, it should return (3 . 0), as it does.  You misunderstand the
contract of this function (which is not surprising, since the issue is
a subtle one, and the documentation, while it tries to be accurate,
has a hard time communicating its intent due to inherent ambiguity of
the related terminology).

This sentence from the doc string of posn-actual-col-row says it all:

  These are the actual row number in the window and character number
  in that row.

"Character number in that row".  IOW, it counts characters, not visual
columns.  This function, and the data in its POSITION argument which
it accesses, are designed to make it easy to find the glyph (or
"display element") in a screen line, so it simply provides the ordinal
number of the "thing at point" on its screen line, disregarding the
screen dimensions of that thing.

So this is not a bug, but intended, if obscure, behavior.

> I'm not 100% this is actually a bug, but (posn-actual-col-row
> (posn-at-point)) returns the "visually" correct column values in the
> more complex cases (after text with `display' or `compose-region' called
> on it), so not accounting for tab-width looks surprising.

As long as posn-actual-col-row deals with characters of the same
dimensions (i.e. the same font), it will always produce seemingly
accurate "column" counts, no matter whether these characters come from
a buffer, a display property, or an overlay string.  (It counts
characters on display, so the source from which they came is
irrelevant.)  But as soon as you have something in the line whose
glyph is larger or smaller than the other characters in that line, the
"column" produced by the function will be skewed, because it's
actually not a visual column, but a count of "display elements" from
the beginning of the screen line.  E.g., try insert-image or put a
display property which uses ':align-to' or ':width', and you will see
that the image and the stretch of whitespace produced by those are
counted as a single "column", no matter what are their actual
dimensions.

IOW, posn-actual-col-row is not reliable when you want screen
coordinates in row/column units.

> Originally: https://github.com/company-mode/company-mode/issues/175

And you were right to resolve that by using posn-col-row instead.
That function translates pixel coordinates into row/column units,
which is much closer to what you want.

(Yes, it's not easy to do the job of the display engine.)