1

It's simple to find the number of lines from the cursor to top of the screen, eg:

(- (line-number-at-pos (point)) (line-number-at-pos (window-start)))) 

However when the hideshow library is used to hide certain text, this is no longer valid in that my cursor may have an on-screen offset of N lines from the top of the screen, which is a much larger number if the hidden text is not taken into account.

Is there a way to calculate the number of lines the cursor is from the top of the screen that takes hidden text into account?


n.b. This question initially referred to "narrowing", which is why so many of the comments are about that.

10
  • 2
    How is it no longer valid when narrowing is in effect? Commented Mar 25, 2018 at 11:58
  • you'd like for the narrowed buffer to show the line numbers relative to the entire buffer, correct? Commented Mar 25, 2018 at 12:11
  • 1
    Narrowing only affects the part of the buffer that's not visible, so I think what you refer to as "narrowing" is not what Emacs usually calls narrowing, maybe you're referring to code-folding or something like that? Commented Mar 25, 2018 at 14:35
  • 1
    FWIW: a) you can replace (- (line-number-at-pos X) (line-number-at-pos Y)) with the more efficient (count-lines Y X); b) the function line-number-at-pos was extended with an optional ABSOLUTE argument in Emacs 26 which returns the line number disregarding any narrowing in effect. Commented Mar 25, 2018 at 14:40
  • 1
    Ok, so the question has nothing to do with narrowing, and is actually about text which has been hidden via hideshow.el. I have edited the question accordingly. If you have not yet done so you should read the manual page on narrowing (see my previous comment) and experiment with that feature, so that you understand what that term actually means. Commented Mar 27, 2018 at 0:45

1 Answer 1

2

M-x what-line gives both the relative line number and the narrow buffer number in the mini buffer echo area, for example:

M-x what-line line 189 (narrowed line 1) 
3
  • Is there a way to call this from elisp so I can use it in code? Commented Mar 25, 2018 at 12:42
  • Naturally it can be called like in: (global-set-key (kbd "C-c #") (lambda() (interactive)(what-line))) Commented Mar 25, 2018 at 12:52
  • Yes, but if I want a number value output to use within elisp code, checked the code and seems I might be best to copy out the relevant parts. Commented Mar 25, 2018 at 12:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.