From mboxrd@z Thu Jan 1 00:00:00 1970 From: Florian Adamsky Subject: Archive subtrees hierarchical (keep the parent structure) Date: Mon, 04 Aug 2014 21:29:53 +0200 Message-ID: <87bns012a6.fsf@voyager.localdomain> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:55932) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XENxJ-0005Pw-M4 for emacs-orgmode@gnu.org; Mon, 04 Aug 2014 15:30:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XENxE-0001un-AB for emacs-orgmode@gnu.org; Mon, 04 Aug 2014 15:30:01 -0400 Received: from haktar.org ([46.38.251.222]:53985) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XENxE-0001uV-0F for emacs-orgmode@gnu.org; Mon, 04 Aug 2014 15:29:56 -0400 Received: from localhost (p57927CBF.dip0.t-ipconnect.de [87.146.124.191]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: cit) by haktar.org (Postfix) with ESMTPSA id 649A419F7DA for ; Mon, 4 Aug 2014 21:29:54 +0200 (CEST) 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Dear all, some of my org-mode files are getting bigger and bigger. So, I decided to use the archive feature to remove old stuff. However, I was not happy with the current archive feature, because it just puts subtrees unorganized in the archive file. I was more looking for a way to archive a subtree, but keep the parent structure. Means, if the point is at the subtree "*** FOO" in the following example: * A ** B *** FOO then it should copy the heading "* A" and "** B" to the archive file and then move "*** Foo" to it. The only thing that I found was a feature request from Florian Lindner [fn:1]. A couple of days I was given it a shot and tried to implement that myself. Attached you'll find my attempt. It is a bit hackish, but it works for me. I think the attached code misses two features: 1. it only copies the parent headings with tags, but ignores properties and stuff like that 2. it ignores org-reverse-note-order, but that should not be too hard to add. Before I work on it again, I would like to hear your comments. Have I implemented functions that are already in org-mode? Is this feature useful for other people? Does it in more complicated org-mode files? Best regards Footnotes: [fn:1] https://lists.gnu.org/archive/html/emacs-orgmode/2014-05/msg01214.html -- Florian Adamsky http://florian.adamsky.it/ --=-=-= Content-Type: application/emacs-lisp Content-Disposition: attachment; filename=org-archive-subtree-hierarchical.el Content-Transfer-Encoding: quoted-printable (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 (=3D (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)))) --=-=-=--