4

Preface this by saying I program but I'm not a programmer, and I especially don't know lisp.

I use org capture during code reviews, and we use line numbers as a way or organizing changes that need to be made. Is there a way to have org-capture include the line number that point was at when org capture was called?

  • This issue was raised before. Looks like the template language did not implement the (line-number-at-pos) function. – Emacs User Nov 19 '15 at 20:23
  • I saw that thread too, but that is a different issue. They were modifying org-store-link so that links included the line number, and the OP was disappointed that the link didn't jump to the line number. I don't want it in a link, I just want the number. – PaulB Nov 19 '15 at 21:37
  • If all you need is the line number, then use \n%(number-to-string (line-number-at-pos))\n in the template. – Emacs User Nov 19 '15 at 22:47
  • That's close! but it actually the gives me the line number of the caputre buffer, which is always 3 in my template. Any idea how to get it to use the line number from the buffer in which org-capture is called? – PaulB Nov 20 '15 at 22:22
  • You're probably doing it in the capture template and not in the link template or link types. Org website has examples of creating new link types. Make one with line number and use that in your capture template. That was the issue I gave you the link for earlier. – Emacs User Nov 20 '15 at 22:38
3

The org-capture function puts some properties into the org-capture-plist, i. a. :original-buffer, :original-file and :original-file-nondirectory, which all point to the buffer/file where the capture has been initiated from. The following addition to your capture template will insert the line-number at point in the original buffer:

%(with-current-buffer (org-capture-get :original-buffer) (number-to-string (line-number-at-pos)))
1

Well this might not be the best answer, but what I did was wrap org-capture in a function that sets a global variable to the line number right before calling org-capture.

Here's the function I used to set the plus-line-capture:

(defun org-plus-line-capture ()
  (interactive)
  (setq plus-line-capture (number-to-string (line-number-at-pos)))
  (org-capture))

Then in the capture template you can do this:

[[file://%F::%(concat plus-line-capture)][%^{Link Text}]]
  • %(with-current-buffer (org-capture-get :original-file-nondirectory) (number-to-string (line-number-at-pos))) in your capture template will do the same without any variable assignment. – mutbuerger Jul 22 '16 at 12:00
  • Well if this works, then this seems like the actual answer to the question; yes-no? – lucidquiet Jul 25 '16 at 18:35
  • You're right, I'll add an answer. – mutbuerger Jul 25 '16 at 18:52

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.