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.

  • 2
    How is it no longer valid when narrowing is in effect? – phils Mar 25 '18 at 11:58
  • you'd like for the narrowed buffer to show the line numbers relative to the entire buffer, correct? – manandearth Mar 25 '18 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? – Stefan Mar 25 '18 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. – Basil Mar 25 '18 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. – phils Mar 27 '18 at 0:45
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)
  • Is there a way to call this from elisp so I can use it in code? – ideasman42 Mar 25 '18 at 12:42
  • Naturally it can be called like in: (global-set-key (kbd "C-c #") (lambda() (interactive)(what-line))) – manandearth Mar 25 '18 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. – ideasman42 Mar 25 '18 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.