I would like to write a function that scrapes the current buffer for any comments with the word "TODO" and runs org-capture, saving a link to that TODO comment in a file. The end goal would be sort of a one-stop location for TODO items like some modern IDEs have:

enter image description here

Ideally, I want the link to show the text of the todo comment. So the following buffer:

int main(/* TODO argc, argv */) {
    // TODO Implement me
}

Would produce the following links in my org file (or something similar):

* TODO main.cpp:1: "argc, argv"
* TODO main.cpp:2: "Implement me"

I might be able to add a custom org-capture-template to handle this, something like:

(setq org-capture-templates
      '(("t" "TODO" entry (file "~/notes/todo.org")
         "* TODO [[%l][<text of link>]]"
         :immediate-finish t)))

And my scraping function could look like:

(defun org-add-todos-list-from-buffer ()
  "Look for TODO comments in current buffer, and add them to org"
  (interactive)
  (dolist (match (matches-in-buffer (format "%s *TODO.*%s?"
                                            (comment-string-strip comment-start nil nil)
                                            (comment-string-strip comment-end nil nil))))
    (org-capture nil "t"))) ;; Somehow pass the desired description to the capture template here?

(matches-in-buffer comes from https://emacs.stackexchange.com/a/7156/11719)

The problem is that I'm not sure how to get from org-capture to my formatted link. I haven't found a template format option that does what I want (the closest thing I think would be to un-mangle the content of %l). I could create the formatted text in my scrape function, but then I don't think I can pass that to the template (unless there is a way to programmatically fill in a prompt?). I suppose I could use something other than org-capture but I don't want to reinvent the wheel. (Maybe there is already a package out there that does exactly what I want?)

Anyone have any tips?

  • 1
    FWIW, I do the above with M-x grep RET TODO *.el RET – Stefan Jul 7 '17 at 21:04
  • Heh, yeah... I may be over-engineering this a bit. – 0x5453 Jul 7 '17 at 21:22
  • templates can use %(SEXP) Evaluate Elisp SEXP and replace with the result. – Lyn Headley Jul 12 '17 at 2:47
  • @LynHeadley I've tried messing with that a little bit, but it seems like the expression is evaluated from inside the buffer where the link is pasted, not the one from which it is captured. So by that point I've already lost the text that I care about. – 0x5453 Jul 12 '17 at 12:37
  • Can't you get around that by setting a global variable before calling org-capture? – Lyn Headley Jul 12 '17 at 19:24

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.