1

I want to refile a headline to a datetree at 2016 -> 2016-10 October -> 2016-10-08 Saturday.

The datetree presently looks like:

* 2017
** 2017-11 November
*** 2017-11-30 Thursday
**** Something 1
**** Something 2

My refile target does not exist! But I am refiling in a datetree. So, I should be able to give an refile location of 'datetree' and it should be intelligent enough to create the datetree as needed. Or is it not?

1

Refiling doesn't know how to handle datetrees. Out of the box, datetrees are only accessible via the capture process. However, the following code will refile a heading from within a buffer to a datetree heading in the same buffer. Note, though, that it won't work properly if you're trying to refile from one date to another - that will require more code to remove the existing date information from the heading before moving it to the new location.

(defun org-refile-to-datetree ()
  (interactive)
  (let ((date (calendar-read-date)))
    (save-excursion
      (org-datetree-find-date-create date))
    (save-excursion
      (org-mark-subtree)
      (org-datetree-file-entry-under
       (buffer-substring (region-beginning) (region-end))
       date))
    (org-cut-subtree))) 
| improve this answer | |
  • Thanks. That works. Except that it does not 'refile' it copies (leaves the original in its place). And despite my #+STARTUP: lorgefile, it does not add the logbook entry. Any suggestions around that? – deshmukh Nov 30 '17 at 16:57
  • 1
    Oops = the revised code should move, not duplicate, the subtree. I'm not sure how to add the logfile. – Tyler Nov 30 '17 at 17:08
  • Thanks. That works. But lets wait to see if someone comes up with a solution that will make appropriate entries in the logbook. – deshmukh Dec 1 '17 at 9:57
0

I use this, which I adapted from this Stack Exchange post by Tom Hunt. It prompts for the date you would like to refile, with today the default date.

(defun org-read-datetree-date (d)
  "Parse a time string D and return a date to pass to the datetree functions."
  (let ((dtmp (nthcdr 3 (parse-time-string d))))
    (list (cadr dtmp) (car dtmp) (caddr dtmp))))

(defun org-refile-to-archive-datetree ()
  "Refile an entry to a datetree under an archive."
  (interactive)
  (require 'org-datetree)
  (let ((dest (save-excursion
                (org-datetree-find-date-create
                 (org-read-datetree-date (org-read-date t nil)))
                (point))))
    (org-refile nil nil (list nil (buffer-file-name) nil dest))))
| improve this answer | |

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.