Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(require 'org-archive)
(defun line-content-as-string ()
(line-beginning-position) (line-end-position))))
(defun org-child-list ()
(if (= (org-outline-level) 0)
(outline-next-visible-heading 1)
;; (org-forward-heading-same-level 1)
(let ((child-list (list (line-content-as-string))))
(while (org-goto-sibling)
(setq child-list (cons (line-content-as-string) child-list)))
(defun fa/org-struct-subtree (&optional start-value)
(let ((archive-tree start-value))
(while (org-up-heading-safe)
(let ((heading
(line-beginning-position) (line-end-position))))
(if (eq archive-tree nil)
(setq archive-tree (list heading))
(setq archive-tree (cons heading archive-tree))))))
(defun org-archive-subtree-hierarchical ()
(let ((org-tree (fa/org-struct-subtree))
(this-buffer (current-buffer))
(file (abbreviate-file-name
(or (buffer-file-name (buffer-base-buffer))
(error "No file associated to buffer")))))
(setq location (org-get-local-archive-location)
afile (org-extract-archive-file location)
heading (org-extract-archive-heading location)
infile-p (equal file (abbreviate-file-name (or afile ""))))
(unless afile
(error "Invalid `org-archive-location'"))
(if (> (length afile) 0)
(setq newfile-p (not (file-exists-p afile))
visiting (find-buffer-visiting afile)
buffer (or visiting (find-file-noselect afile)))
(setq buffer (current-buffer)))
(unless buffer
(error "Cannot access file \"%s\"" afile))
(set-buffer buffer)
(switch-to-buffer buffer)
(goto-char (point-min))
(while (not (equal org-tree nil))
(let ((child-list (org-child-list)))
(if (member (car org-tree) child-list)
(search-forward (car org-tree) nil t)
(setq org-tree (cdr org-tree)))
(org-insert-struct org-tree)
(setq org-tree nil)
;; Save and kill the buffer, if it is not the same buffer.
(when (not (eq this-buffer buffer))
(message "Subtree archived %s"
(concat "in file: " (abbreviate-file-name afile))))))
(defun org-insert-struct (struct)
(when struct
(insert (car struct))
(org-insert-struct (cdr struct))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment