Instantly share code, notes, and snippets.

Embed
What would you like to do?
Archive subtrees under the same hierarchy as original in the archive files.
(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))))))))
@nerrons

This comment has been minimized.

Show comment
Hide comment
@nerrons

nerrons Mar 7, 2017

It seems that only the level 1 titles will be also recorded in the _archive file, but not level 2, level 3, etc.

For example, when I have
* L1
** L2
*** L3
and I org-archive-subtree when my cursor is on ** L3, this is what I get in the _archive file:
* L1
** L3

Is there any ways to fix it?
Thank you so much for your work.

Org mode version 9.0.5
GNU Emacs 25.1.1 (x86_64-apple-darwin16.1.0, NS appkit-1504.60 Version 10.12.1 (Build 16B2555)) of 2016-11-27

nerrons commented Mar 7, 2017

It seems that only the level 1 titles will be also recorded in the _archive file, but not level 2, level 3, etc.

For example, when I have
* L1
** L2
*** L3
and I org-archive-subtree when my cursor is on ** L3, this is what I get in the _archive file:
* L1
** L3

Is there any ways to fix it?
Thank you so much for your work.

Org mode version 9.0.5
GNU Emacs 25.1.1 (x86_64-apple-darwin16.1.0, NS appkit-1504.60 Version 10.12.1 (Build 16B2555)) of 2016-11-27

@Fuco1

This comment has been minimized.

Show comment
Hide comment
@Fuco1

Fuco1 Apr 20, 2017

@tsktsktsk64 It works for me as expected on Emacs 24.5.1 with Org mode 9.0.5.

I think some function might have changed in new Emacs to make this stop working.

You could try to edebug the form and then step through it to see where it fails. I will fix it one day when I migrate to Emacs 25 for sure :)

Owner

Fuco1 commented Apr 20, 2017

@tsktsktsk64 It works for me as expected on Emacs 24.5.1 with Org mode 9.0.5.

I think some function might have changed in new Emacs to make this stop working.

You could try to edebug the form and then step through it to see where it fails. I will fix it one day when I migrate to Emacs 25 for sure :)

@Fuco1

This comment has been minimized.

Show comment
Hide comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment