emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Myles English <mylesenglish@gmail.com>
To: Peter Sterner <psterner@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Capture template for datetree under existing headline
Date: Sun, 25 Sep 2016 15:32:58 +0100	[thread overview]
Message-ID: <87mviwyrqd.fsf@gmail.com> (raw)
In-Reply-To: <CABG0ipG8H0M9gKie82uEvvjprYUZMWtXPg+pGBvnaeTc3Ca53w@mail.gmail.com>


Hi Peter,

Peter Sterner writes:

> I want to have a capture template that generates a datetree under an
> existing headline in an org file with existing headlines. I tried this:
>
> ("j" "Journal" entry (file+datetree "~/workspace/org/notes.org" "Journal")
> "* %?\n Added %U\n %i\n%a")
>
> I tried creating the Journal headline before capturing. But org-mode
> generates a 2016 headline not placed under Journal.

This seems to work, by making a new capture target called
'file+headline+datetree'.

Capture template:

("j" "Journal" entry (file+headline+datetree "~/workspace/org/notes.org" "Journal")

Patch:

diff --git a/lisp/org-capture.el b/lisp/org-capture.el
index a89d171..1826d3e 100644
--- a/lisp/org-capture.el
+++ b/lisp/org-capture.el
@@ -923,6 +923,40 @@ Store them in the capture property list."
 	      (setq target-entry-p (and (derived-mode-p 'org-mode) (org-at-heading-p))))
 	  (error "No match for target regexp in file %s" (nth 1 target))))
 
+       ((eq (car target) 'file+headline+datetree)
+	;; copied from sexp for file+headline
+	(set-buffer (org-capture-target-buffer (nth 1 target)))
+	(org-capture-put-target-region-and-position)
+	(widen)
+	(let ((hd (nth 2 target)))
+	  (goto-char (point-min))
+	  (unless (derived-mode-p 'org-mode)
+	    (error
+	     "Target buffer \"%s\" for file+headline+datetree should be in Org mode"
+	     (current-buffer)))
+	  (if (re-search-forward
+	       (format org-complex-heading-regexp-format (regexp-quote hd))
+	       nil t)
+	      (goto-char (point-at-bol))
+	    (goto-char (point-max))
+	    (or (bolp) (insert "\n"))
+	    (insert "* " hd "\n")
+	    (beginning-of-line 0)))
+	(org-narrow-to-subtree)
+	(org-show-subtree)
+	;; copied from the sexp for file+datetree
+	(funcall
+	 #'org-datetree-find-date-create
+	 (calendar-gregorian-from-absolute
+	  (cond
+	   (org-overriding-default-time
+	    ;; use the overriding default time
+	    (time-to-days org-overriding-default-time))
+	   (t
+	    ;; current date, possibly corrected for late night workers
+	    (org-today))))
+	 'restrict))
+
        ((memq (car target) '(file+datetree file+datetree+prompt file+weektree file+weektree+prompt))
 	(require 'org-datetree)
 	(set-buffer (org-capture-target-buffer (nth 1 target)))

This results in:

* Journal
* 2016

unless the following property is set:

* Journal
  :PROPERTIES:
  :DATE_TREE:
  :END:
** 2016

An improvement might be to prompt for the headline to put the datetree
under.  It might have been possible to do this as a 'function' target.

Myles

      reply	other threads:[~2016-09-25 14:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-23 19:41 Capture template for datetree under existing headline Peter Sterner
2016-09-25 14:32 ` Myles English [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mviwyrqd.fsf@gmail.com \
    --to=mylesenglish@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=psterner@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).