From: Ken Mankoff <mankoff@gmail.com>
To: Mark Edgington <edgimar@gmail.com>
Cc: emacs-orgmode <emacs-orgmode@gnu.org>
Subject: Re: Smart archiving of subtrees with parent headlines
Date: Mon, 12 Feb 2018 07:54:11 +0100 [thread overview]
Message-ID: <m2vaf2k8a4.fsf@gmail.com> (raw)
In-Reply-To: <CAMsBe8oZkxYT9_EQkejD7TuzbdLztJYN6nvwtgz3PLi2m6Hgyg@mail.gmail.com>
On 2018-02-09 at 16:42, Mark Edgington <edgimar@gmail.com> 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.
next prev parent reply other threads:[~2018-02-12 6:54 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-09 16:42 Smart archiving of subtrees with parent headlines Mark Edgington
2018-02-10 4:27 ` numbchild
2018-02-10 14:04 ` Eric S Fraga
2018-02-12 6:54 ` Ken Mankoff [this message]
2018-02-12 21:06 ` Mark Edgington
2018-02-12 22:44 ` Mark Edgington
2018-04-26 23:34 ` Bastien
2018-05-01 0:35 ` Mark Edgington
2018-05-01 9:01 ` Bastien
2019-03-28 18:13 ` Ken Mankoff
2019-03-29 2:23 ` Mark Edgington
2019-03-29 7:18 ` Ken Mankoff
2019-04-02 7:32 ` Ken Mankoff
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m2vaf2k8a4.fsf@gmail.com \
--to=mankoff@gmail.com \
--cc=edgimar@gmail.com \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).