emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: line-start?


From: Bastien
Subject: Re: line-start?
Date: Fri, 07 Feb 2020 11:11:15 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Eli Zaretskii <address@hidden> 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]