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
    AFAIK, there are no such functions, in vanilla Emacs (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 mention with-current-buffer?
    – Drew
    Oct 10 '19 at 17:04
  • I guess you meant goto-char instead of goto-point (I often make this confusion myself). And probably thing-at-point ...
    – JeanPierre
    Oct 10 '19 at 20:08

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.
    – Drew
    Oct 10 '19 at 17:08

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.