-
-
Save Fuco1/e86fb5e0a5bb71ceafccedb5ca22fcfb to your computer and use it in GitHub Desktop.
| (defadvice org-archive-subtree (around fix-hierarchy activate) | |
| (let* ((fix-archive-p (and (not current-prefix-arg) | |
| (not (use-region-p)))) | |
| (afile (org-extract-archive-file (org-get-local-archive-location))) | |
| (buffer (or (find-buffer-visiting afile) (find-file-noselect afile)))) | |
| ad-do-it | |
| (when fix-archive-p | |
| (with-current-buffer buffer | |
| (goto-char (point-max)) | |
| (while (org-up-heading-safe)) | |
| (let* ((olpath (org-entry-get (point) "ARCHIVE_OLPATH")) | |
| (path (and olpath (split-string olpath "/"))) | |
| (level 1) | |
| tree-text) | |
| (when olpath | |
| (org-mark-subtree) | |
| (setq tree-text (buffer-substring (region-beginning) (region-end))) | |
| (let (this-command) (org-cut-subtree)) | |
| (goto-char (point-min)) | |
| (save-restriction | |
| (widen) | |
| (-each path | |
| (lambda (heading) | |
| (if (re-search-forward | |
| (rx-to-string | |
| `(: bol (repeat ,level "*") (1+ " ") ,heading)) nil t) | |
| (org-narrow-to-subtree) | |
| (goto-char (point-max)) | |
| (unless (looking-at "^") | |
| (insert "\n")) | |
| (insert (make-string level ?*) | |
| " " | |
| heading | |
| "\n")) | |
| (cl-incf level))) | |
| (widen) | |
| (org-end-of-subtree t t) | |
| (org-paste-subtree level tree-text)))))))) |
Unfortunately this advice does nothing related to date trees. It will recreate the parents the same way as they exist in the original file. If you mark task 3 as done it will be placed under Heading1 > task2 > task3.
To make it archive in a date tree you would need to write a new function (as andersjohansson points out it doesn't need to be an advice).
@Fuco1: Thanks for having answered 👍
Thanks for the quick response, @Fuco1, as I don't know Elisp, I will have to continue as I am :) Thanks anyway!
The advice still works however it adds the archived subtree to the kill ring.
It also only works if the org archive locations isn't under another subtree.
I put in the advice in a small emacs package that has a function to enable and disable the advice:
https://gitlab.com/Thaodan/emacs.d-lisp/-/blob/master/org-archive-subtree-hierarchy.el
Thanks @daviderestivo! If my archive format is datetree, and I mark as DONE the following tasks on different days
What can I expect to get in my datetree?
Thanks!