9

In Org-mode, I would like to have a capture template get stored like this:

* 2015
** 2015-05 May
*** 2015-05-21 Thursday     <--- I can get this with file+datetree
**** Todo today             <--- here's the problem
***** TODO something        <--- this is the captured content

Is that possible out of the box? If not, any ideas how to go about implementing it?

3
  • I'm not entirely clear on what it is you want to achieve. Why is there a "problem" in the line that says **** Todo today? It would be great if you could extend your post to include (1) an example of the output that your capture template currently produces, (2) a separate example of the output you would like to see, and (3) the capture template itself. Other than that, welcome to Emacs.SE!
    – itsjeyd
    May 21, 2015 at 11:04
  • I don't know how to get that line between the datetree headline (2015-05-21 Thursday) and the headline produced by the template (TODO something). Basically, what I need is file+datetree+olp. May 21, 2015 at 11:11
  • In case you saw the answer I posted earlier: It did not fully address your use case in the sense that it inserted a **** Todo today heading for every TODO item created using org-capture. I have since fixed this issue; please have another look at my answer.
    – itsjeyd
    May 21, 2015 at 13:22

1 Answer 1

13

You can achieve what you want by defining a custom function that returns the target location for template expansion:

(defun org-find-heading-in-datetree ()
  (org-datetree-find-date-create (calendar-current-date))
  (goto-char (point-at-eol))
  (when (not (re-search-forward
              (format org-complex-heading-regexp-format
                      (regexp-quote "Todo today")) nil t))
    (insert "\n**** Todo today\n"))
  (goto-char (point-max)))

Then set your template up like this:

(setq org-capture-templates
      '(("t" "Test" plain (file+function "~/org/test.org"
                                         org-find-heading-in-datetree)
         "***** TODO %?")))
3
  • 1
    calendar-current-date is Gregorian at the outset, so no need to convert from org-today to Gregorian.
    – lawlist
    May 21, 2015 at 20:24
  • @lawlist Thanks for pointing that out! I updated my answer.
    – itsjeyd
    May 21, 2015 at 22:19
  • 1
    This almost worked for me. The problem was that it would add the "***** Todo today" but then the "***** TODO" would be outside the datetree. Was able to fix this behavior by changing (goto-char (point-max)) to (goto-char (point-at-eol))
    – rking
    Oct 2, 2016 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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