unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* line-start?
@ 2020-02-06 10:24 Bastien
  2020-02-06 13:00 ` line-start? Bastien
  0 siblings, 1 reply; 12+ messages in thread
From: Bastien @ 2020-02-06 10:24 UTC (permalink / raw)
  To: emacs-devel

Hi,

I am trying to get the position of the first visible character
on the current line, when the window is scrolled horizontally.

Something similar to (window-start) but for the current line.

What would be the idiomatic way to get this information?

Thanks,

-- 
 Bastien



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

* Re: line-start?
  2020-02-06 10:24 line-start? Bastien
@ 2020-02-06 13:00 ` Bastien
  2020-02-06 18:25   ` line-start? Eli Zaretskii
  0 siblings, 1 reply; 12+ messages in thread
From: Bastien @ 2020-02-06 13:00 UTC (permalink / raw)
  To: emacs-devel

Bastien <bzg@gnu.org> writes:

> Something similar to (window-start) but for the current line.

FWIW, I've found (window-hscroll) which gives me what I want.

-- 
 Bastien



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

* Re: line-start?
  2020-02-06 13:00 ` line-start? Bastien
@ 2020-02-06 18:25   ` Eli Zaretskii
  2020-02-07  6:42     ` line-start? Bastien
  0 siblings, 1 reply; 12+ messages in thread
From: Eli Zaretskii @ 2020-02-06 18:25 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

> From: Bastien <bzg@gnu.org>
> Date: Thu, 06 Feb 2020 14:00:26 +0100
> 
> > Something similar to (window-start) but for the current line.
> 
> FWIW, I've found (window-hscroll) which gives me what I want.

If you need a buffer position, then window-hscroll is not what you
want, because it returns the number of columns the window is
hscrolled.  Also, when auto-hscroll-mode is 'current-line', the value
returned by window-hscroll is only correct for the current line,
i.e. the line which includes point.

If you need a buffer position of the first (leftmost) visible
character, then I think you want this instead:

  (nth 1 (posn-at-x-y 0 (cdr (posn-x-y (posn-at-point)))))



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

* Re: line-start?
  2020-02-06 18:25   ` line-start? Eli Zaretskii
@ 2020-02-07  6:42     ` Bastien
  2020-02-07  9:20       ` line-start? Eli Zaretskii
  0 siblings, 1 reply; 12+ messages in thread
From: Bastien @ 2020-02-07  6:42 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Hi Eli,

Eli Zaretskii <eliz@gnu.org> writes:

> If you need a buffer position of the first (leftmost) visible
> character, then I think you want this instead:
>
>   (nth 1 (posn-at-x-y 0 (cdr (posn-x-y (posn-at-point)))))

Thanks a lot -- this is indeed more precise.  (window-hscroll)
does not handle rescaled text very well, whereas this solution
does.

However, when the cursor is right under the header-line, the
expression above returns 'header-line, so I could not use it
for my purpose.

Also, the same expression returns 'header-line when the cursor
is on the _second_ line below the 'header-line -- I haven't
looked for why it does so, but if it is reproducible, maybe
there is something inconsistent here.

Thanks again anyway!

-- 
 Bastien



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

* Re: line-start?
  2020-02-07  6:42     ` line-start? Bastien
@ 2020-02-07  9:20       ` Eli Zaretskii
  2020-02-07 10:11         ` line-start? Bastien
  0 siblings, 1 reply; 12+ messages in thread
From: Eli Zaretskii @ 2020-02-07  9:20 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

> From: Bastien <bzg@gnu.org>
> Cc: emacs-devel@gnu.org
> Date: Fri, 07 Feb 2020 07:42:28 +0100
> 
> >   (nth 1 (posn-at-x-y 0 (cdr (posn-x-y (posn-at-point)))))
> 
> Thanks a lot -- this is indeed more precise.  (window-hscroll)
> does not handle rescaled text very well, whereas this solution
> does.

Once again, window-hscroll just returns the number of columns by which
the window is hscrolled, it doesn't return the buffer position.  For
example, window-hscroll will return the same value for each line in
the window, although the buffer position of the beginning of each line
is different.  Are you looking for columns or for buffer positions?

> However, when the cursor is right under the header-line, the
> expression above returns 'header-line, so I could not use it
> for my purpose.
> 
> Also, the same expression returns 'header-line when the cursor
> is on the _second_ line below the 'header-line -- I haven't
> looked for why it does so, but if it is reproducible, maybe
> there is something inconsistent here.

Yes, you need to correct the Y coordinate returned by posn-at-point
when the window has a header line.  The correction can be calculated
like this:

  (- (nth 1 (window-edges nil t nil t))
     (nth 1 (window-edges nil nil nil t)))



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

* Re: line-start?
  2020-02-07  9:20       ` line-start? Eli Zaretskii
@ 2020-02-07 10:11         ` Bastien
  2020-02-07 10:24           ` line-start? Eli Zaretskii
  0 siblings, 1 reply; 12+ messages in thread
From: Bastien @ 2020-02-07 10:11 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

> Once again, window-hscroll just returns the number of columns by which
> the window is hscrolled, it doesn't return the buffer position.  For
> example, window-hscroll will return the same value for each line in
> the window, although the buffer position of the beginning of each line
> is different.  Are you looking for columns or for buffer positions?

I was looking for columns.

The use case was this one: given a line, display the content of this
line in the header line.  When horizontally scrolled, the header has
to display a substring of the line under the cursor, something like
(substring (current-line-string) offset-in-columns).

When the text is not scaled, (window-hscroll) does the job of giving
me the columns for the offset.

When the text is scaled, your function does a better job at finding
the position of the first visible char in the current line, from which
I get the offset: (- first-visible-char-in-current-line (point-at-bol))

> Yes, you need to correct the Y coordinate returned by posn-at-point
> when the window has a header line.  The correction can be calculated
> like this:
>
>   (- (nth 1 (window-edges nil t nil t))
>      (nth 1 (window-edges nil nil nil t)))

Nice!  My initial use-case is gone, but I like this function:

(defun window-line-start (&optional pos window)
  "Return the position of the visual start of the line.
POS defaults to point in WINDOW; WINDOW defaults to the current window."
  (nth 1 (posn-at-x-y
	  0 (+ (cdr (posn-x-y (posn-at-point pos window)))
	       (- (nth 1 (window-edges nil t nil t))
		  (nth 1 (window-edges nil nil nil t)))))))

My use-case is gone, I went with overlays instead of the header line.

But I guess `window-line-start' could perhaps be useful to others.

-- 
 Bastien



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

* Re: line-start?
  2020-02-07 10:11         ` line-start? Bastien
@ 2020-02-07 10:24           ` Eli Zaretskii
  2020-02-07 10:48             ` line-start? Bastien
  0 siblings, 1 reply; 12+ messages in thread
From: Eli Zaretskii @ 2020-02-07 10:24 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

> From: Bastien <bzg@gnu.org>
> Cc: emacs-devel@gnu.org
> Date: Fri, 07 Feb 2020 11:11:15 +0100
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > Once again, window-hscroll just returns the number of columns by which
> > the window is hscrolled, it doesn't return the buffer position.  For
> > example, window-hscroll will return the same value for each line in
> > the window, although the buffer position of the beginning of each line
> > is different.  Are you looking for columns or for buffer positions?
> 
> I was looking for columns.

Then (nth 1) of what posn-at-x-y returns is not what you want.  You
should use the (car (nth 6)) instead.

> The use case was this one: given a line, display the content of this
> line in the header line.  When horizontally scrolled, the header has
> to display a substring of the line under the cursor, something like
> (substring (current-line-string) offset-in-columns).

But using the buffer position, you could use buffer-substring instead,
right?

> (defun window-line-start (&optional pos window)
>   "Return the position of the visual start of the line.
> POS defaults to point in WINDOW; WINDOW defaults to the current window."
>   (nth 1 (posn-at-x-y
> 	  0 (+ (cdr (posn-x-y (posn-at-point pos window)))
> 	       (- (nth 1 (window-edges nil t nil t))
> 		  (nth 1 (window-edges nil nil nil t)))))))
> 
> My use-case is gone, I went with overlays instead of the header line.
> 
> But I guess `window-line-start' could perhaps be useful to others.

Maybe.



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

* Re: line-start?
  2020-02-07 10:24           ` line-start? Eli Zaretskii
@ 2020-02-07 10:48             ` Bastien
  2020-02-07 13:43               ` line-start? Eli Zaretskii
  0 siblings, 1 reply; 12+ messages in thread
From: Bastien @ 2020-02-07 10:48 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

> Then (nth 1) of what posn-at-x-y returns is not what you want.  You
> should use the (car (nth 6)) instead.

FWIW I just tried this:

(car (nth 6 (posn-at-x-y
	0 (+ (cdr (posn-x-y (posn-at-point pos window)))
	     (- (nth 1 (window-edges nil t nil t))
		(nth 1 (window-edges nil nil nil t)))))))

but it always returns 0.

The version with (nth 1 ...) works fine, though.

>> The use case was this one: given a line, display the content of this
>> line in the header line.  When horizontally scrolled, the header has
>> to display a substring of the line under the cursor, something like
>> (substring (current-line-string) offset-in-columns).
>
> But using the buffer position, you could use buffer-substring instead,
> right?

Yes, sure, but I still need to know the window line start to get the
offset.

-- 
 Bastien



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

* Re: line-start?
  2020-02-07 10:48             ` line-start? Bastien
@ 2020-02-07 13:43               ` Eli Zaretskii
  2020-02-07 14:46                 ` line-start? Bastien
  0 siblings, 1 reply; 12+ messages in thread
From: Eli Zaretskii @ 2020-02-07 13:43 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

> From: Bastien <bzg@gnu.org>
> Cc: emacs-devel@gnu.org
> Date: Fri, 07 Feb 2020 11:48:02 +0100
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > Then (nth 1) of what posn-at-x-y returns is not what you want.  You
> > should use the (car (nth 6)) instead.
> 
> FWIW I just tried this:
> 
> (car (nth 6 (posn-at-x-y
> 	0 (+ (cdr (posn-x-y (posn-at-point pos window)))
> 	     (- (nth 1 (window-edges nil t nil t))
> 		(nth 1 (window-edges nil nil nil t)))))))
> 
> but it always returns 0.

Ah, yes: you will have to add (window-hscroll) to that.

> The version with (nth 1 ...) works fine, though.

But that gives a buffer position, not column number.

> >> The use case was this one: given a line, display the content of this
> >> line in the header line.  When horizontally scrolled, the header has
> >> to display a substring of the line under the cursor, something like
> >> (substring (current-line-string) offset-in-columns).
> >
> > But using the buffer position, you could use buffer-substring instead,
> > right?
> 
> Yes, sure, but I still need to know the window line start to get the
> offset.

No, buffer position is absolute.



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

* Re: line-start?
  2020-02-07 13:43               ` line-start? Eli Zaretskii
@ 2020-02-07 14:46                 ` Bastien
  2020-02-07 15:56                   ` line-start? Eli Zaretskii
  0 siblings, 1 reply; 12+ messages in thread
From: Bastien @ 2020-02-07 14:46 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

> But that gives a buffer position, not column number.

(- (window-line-start) (line-beginning-position))

gives me the column number.

-- 
 Bastien



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

* Re: line-start?
  2020-02-07 14:46                 ` line-start? Bastien
@ 2020-02-07 15:56                   ` Eli Zaretskii
  2020-02-07 17:42                     ` line-start? Bastien
  0 siblings, 1 reply; 12+ messages in thread
From: Eli Zaretskii @ 2020-02-07 15:56 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

> From: Bastien <bzg@gnu.org>
> Cc: emacs-devel@gnu.org
> Date: Fri, 07 Feb 2020 15:46:09 +0100
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > But that gives a buffer position, not column number.
> 
> (- (window-line-start) (line-beginning-position))
> 
> gives me the column number.

That's not the column number, that's the ordinal number of the
character on a line.  It doesn't take into account characters that
take more than one column, it doesn't take into account characters
that are composed to produce a single grapheme cluster (or even a
single glyph), and it doesn't take into account variable-size fonts.

Of course, if all these are unimportant in your use case, you can
disregard them.  But a general-purpose function cannot disregard them.



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

* Re: line-start?
  2020-02-07 15:56                   ` line-start? Eli Zaretskii
@ 2020-02-07 17:42                     ` Bastien
  0 siblings, 0 replies; 12+ messages in thread
From: Bastien @ 2020-02-07 17:42 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel

Eli Zaretskii <eliz@gnu.org> writes:

> That's not the column number, that's the ordinal number of the
> character on a line.  It doesn't take into account characters that
> take more than one column, it doesn't take into account characters
> that are composed to produce a single grapheme cluster (or even a
> single glyph), and it doesn't take into account variable-size fonts.

Fair enough.

> Of course, if all these are unimportant in your use case, you can
> disregard them.  But a general-purpose function cannot disregard
> them.

Agreed!

-- 
 Bastien



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

end of thread, other threads:[~2020-02-07 17:42 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-02-06 10:24 line-start? Bastien
2020-02-06 13:00 ` line-start? Bastien
2020-02-06 18:25   ` line-start? Eli Zaretskii
2020-02-07  6:42     ` line-start? Bastien
2020-02-07  9:20       ` line-start? Eli Zaretskii
2020-02-07 10:11         ` line-start? Bastien
2020-02-07 10:24           ` line-start? Eli Zaretskii
2020-02-07 10:48             ` line-start? Bastien
2020-02-07 13:43               ` line-start? Eli Zaretskii
2020-02-07 14:46                 ` line-start? Bastien
2020-02-07 15:56                   ` line-start? Eli Zaretskii
2020-02-07 17:42                     ` line-start? Bastien

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