I have list of buffer positions and I want to get the numbers of the lines at those positions. Is there a better way to do that then using with-current-buffer
, goto-point
and get-thing-at-point 'line
? I want the code to be as fast as possible.
1 Answer
For a faster method to get line number at position -- (with-current-buffer BUFFER (goto-char POSITION) (string-to-number (format-mode-line "%l")))
-- see the following thread, which cites some limitations / disadvantages in the comments:
A faster method to obtain `line-number-at-pos` in large buffers
The traditional method is to use line-number-at-pos
, which is a compiled Lisp function in simple.el
:
(line-number-at-pos &optional POS ABSOLUTE)
Return buffer line number at position POS.
If POS is nil, use current buffer location.
If ABSOLUTE is nil, the default, counting starts
at (point-min), so the value refers to the contents of the
accessible portion of the (potentially narrowed) buffer. If
ABSOLUTE is non-nil, ignore any narrowing and return the
absolute line number.
-
This is likely the answer. But the question is unclear. If the OP agrees that this is what s?he's looking for then the question should be edited to make it clear. Otherwise, the question should likely be closed/deleted.– DrewCommented Oct 10, 2019 at 17:08
emacs -Q
):goto-point
,get-thing-at-point
. So the question is unclear, and risks being closed. It's also unclear because you don't show your code: what code you find not to be "snappy". And why do you mentionwith-current-buffer
?goto-char
instead ofgoto-point
(I often make this confusion myself). And probablything-at-point
...