From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ken Mankoff Subject: Re: Smart archiving of subtrees with parent headlines Date: Mon, 12 Feb 2018 07:54:11 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:47814) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1el80J-0006Gy-5U for emacs-orgmode@gnu.org; Mon, 12 Feb 2018 01:54:21 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1el80F-0001w0-0q for emacs-orgmode@gnu.org; Mon, 12 Feb 2018 01:54:19 -0500 Received: from mail-lf0-x235.google.com ([2a00:1450:4010:c07::235]:37912) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1el80E-0001uh-MF for emacs-orgmode@gnu.org; Mon, 12 Feb 2018 01:54:14 -0500 Received: by mail-lf0-x235.google.com with SMTP id g72so18990039lfg.5 for ; Sun, 11 Feb 2018 22:54:14 -0800 (PST) In-reply-to: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Mark Edgington Cc: emacs-orgmode On 2018-02-09 at 16:42, Mark Edgington wrote: > I have looked at a few threads related to the archiving of subtrees, > but haven't found anything that matches what I think would be a very > sensible archiving behavior. [...] Does the attached file here work for you? I use it and it seems to do what you describe. https://lists.gnu.org/archive/html/emacs-orgmode/2014-08/msg00109.html My full archive setup from my emacs.org follows. It is based on the above code but tweaked in ways by past-me that present-me doesn't fully understand, but it works for me. (setq org-archive-location (concat org-directory "/archive/%s_archive::")) (define-key org-mode-map (kbd "C-c C-x C-s") nil) (setq org-archive-default-command 'kdm/org-archive-local-or-hierarchical) ;; C-c C-x C-a ;; only do hierarchical archiving if default var used. If archiving into ;; local file, then just use default org-archive-subtree command (defun kdm/org-archive-local-or-hierarchical () "Archive locally if location set to local file; Otherwise use org-archive-subtree-hierarchical" (interactive) (if (let ((arch-file (org-extract-archive-file)) (this-file (buffer-file-name))) (equal arch-file this-file)) (org-archive-subtree) (org-archive-subtree-hierarchical))) (require 'org-archive) (defun line-content-as-string () "Returns the content of the current line as a string" (save-excursion (beginning-of-line) (buffer-substring-no-properties (line-beginning-position) (line-end-position)))) (defun org-child-list () "This function returns all children of a heading as a list. " (interactive) (save-excursion ;; this only works with org-version > 8.0, since in previous ;; org-mode versions the function (org-outline-level) returns ;; gargabe when the point is not on a heading. (if (= (org-outline-level) 0) (outline-next-visible-heading 1) (org-goto-first-child)) (let ((child-list (list (line-content-as-string)))) (while (org-goto-sibling) (setq child-list (cons (line-content-as-string) child-list))) child-list))) (defun fa/org-struct-subtree () "This function returns the tree structure in which a subtree belongs as a list." (interactive) (let ((archive-tree nil)) (save-excursion (while (org-up-heading-safe) (let ((heading (buffer-substring-no-properties (line-beginning-position) (line-end-position)))) (if (eq archive-tree nil) (setq archive-tree (list heading)) (setq archive-tree (cons heading archive-tree)))))) archive-tree)) (defun org-archive-subtree-hierarchical () "This function archives a subtree hierarchical" (interactive) (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"))))) (save-excursion (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)) (org-cut-subtree) (set-buffer buffer) (org-mode) (goto-char (point-min)) (while (not (equal org-tree nil)) (let ((child-list (org-child-list))) (if (member (car org-tree) child-list) (progn (search-forward (car org-tree) nil t) (setq org-tree (cdr org-tree))) (progn (newline) (org-insert-struct org-tree) (setq org-tree nil))))) (newline) (org-yank) ;; Save and kill the buffer, if it is not the same buffer. (when (not (eq this-buffer buffer)) (save-buffer) (kill-buffer)) (message "Subtree archived %s" (concat "in file: " (abbreviate-file-name afile)))))) (defun org-insert-struct (struct) "TODO" (interactive) (when struct (insert (car struct)) (newline) (org-insert-struct (cdr struct)))) -k. > So here's what I'm trying to figure out how to do: > > Say I start with an org-file that looks like: > > #+ARCHIVE: ::* Archived > > * Foo > * Tasks > * Task1 > * Thoughts > * Thought1 > * Thought2 > > * Archived > > Now I put the point (i.e. cursor) on Thought1 and run > org-super-archive (the magical command I'm hoping to find). The result > should be: > > #+ARCHIVE: ::* Archived > > * Foo > * Tasks > * Task1 > * Thoughts > * Thought2 > > * Archived > * Foo > * Thoughts > * Thought1 > > Now I move the point to Thought2 and again run org-super-archive, > which should give me: > > #+ARCHIVE: ::* Archived > > * Foo > * Tasks > * Task1 > * Thoughts > > * Archived > * Foo > * Thoughts > * Thought1 > * Thought2 > > So the basic operation I'm seeking is the ability to archive a subtree > to whatever target location is specified with #+ARCHIVE (or > org-archive-location) where (1) the full path of the archived subtree > is mirrored beneath that target location, and (2) the subtree will be > merged into an existing path under the target if an appropriate path > already exists (e.g. a path consisting of all of the subtree's parent > headlines, regardless of the content of the bodies of these > headlines). > > As an added bonus, it would be nice if it were possible to choose > whether or not the "full path" of a subtree to be archived will > include the org-file name as the root of the path. This would be > useful in cases where you archive from multiple org-files to a single > archive.org file.