From mboxrd@z Thu Jan 1 00:00:00 1970 From: Toby Cubitt Subject: Selectively archive by timestamp Date: Mon, 29 Sep 2014 15:16:41 +0100 Message-ID: <20140929141641.GA32551@c3po> Reply-To: Toby Cubitt Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="d6Gm4EdcadzBjdND" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:46435) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XYbl2-00086L-UH for emacs-orgmode@gnu.org; Mon, 29 Sep 2014 10:17:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XYbky-0006eP-Hj for emacs-orgmode@gnu.org; Mon, 29 Sep 2014 10:16:56 -0400 Received: from starfish.geekisp.com ([216.168.135.166]:6043) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1XYbky-0006dr-CU for emacs-orgmode@gnu.org; Mon, 29 Sep 2014 10:16:52 -0400 Content-Disposition: inline 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 --d6Gm4EdcadzBjdND Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Sometimes I want to selectively archive all entries in a subtree with timestamps in the past, whilst keeping all entries with timestamps in the future. `org-archive-subtree' only lets you selectively archive entries depending on whether they have open TODO items. The attached patch adds a new `org-archive-all-old' counterpart to `org-archive-all-done' which does timestamp-selective archiving. It also extends `org-archive-subtree' so it can optionally be invoked for timestamp-based archiving instead of TODO-based archiving. Toby -- Dr T. S. Cubitt Royal Society University Research Fellow Fellow of Churchill College, Cambridge Centre for Quantum Information DAMTP, University of Cambridge email: tsc25@cantab.net web: www.dr-qubit.org --d6Gm4EdcadzBjdND Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Add-org-archive-all-old-to-archive-entries-with-time.patch" >From 3183bcf9c005a0d5633dcc8be1719e55e3dfa8c5 Mon Sep 17 00:00:00 2001 From: "Toby S. Cubitt" Date: Fri, 17 Jan 2014 15:14:13 +0000 Subject: [PATCH] Add org-archive-all-old to archive entries with timestamps before today. Can be invoked from org-archive-subtree command with double prefix-arg. --- doc/org.texi | 4 ++++ lisp/org-archive.el | 68 +++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/doc/org.texi b/doc/org.texi index 7d98d51..d2e61a8 100644 --- a/doc/org.texi +++ b/doc/org.texi @@ -7469,6 +7469,10 @@ the archive. To do this, each subtree is checked for open TODO entries. If none are found, the command offers to move it to the archive location. If the cursor is @emph{not} on a headline when this command is invoked, the level 1 trees will be checked. +@orgkey{C-u C-u C-c C-x C-s} +As above, but check subtree for timestamps instead of TODO entries. The +command will offer to archive the subtree if it @emph{does} contain a +timestamp, and that timestamp is in the past. @end table @cindex archive locations diff --git a/lisp/org-archive.el b/lisp/org-archive.el index 700e59b..418af3a 100644 --- a/lisp/org-archive.el +++ b/lisp/org-archive.el @@ -198,9 +198,11 @@ The archive can be a certain top-level heading in the current file, or in a different file. The tree will be moved to that location, the subtree heading be marked DONE, and the current time will be added. -When called with prefix argument FIND-DONE, find whole trees without any +When called with a single prefix argument FIND-DONE, find whole trees without any open TODO items and archive them (after getting confirmation from the user). -If the cursor is not at a headline when this command is called, try all level +When called with a double prefix argument, find whole trees with timestamps before +today and archive them (after getting confirmation from the user). +If the cursor is not at a headline when these commands are called, try all level 1 trees. If the cursor is on a headline, only try the direct children of this heading." (interactive "P") @@ -213,8 +215,10 @@ this heading." (org-archive-subtree ,find-done)) org-loop-over-headlines-in-active-region cl (if (outline-invisible-p) (org-end-of-subtree nil t)))) - (if find-done - (org-archive-all-done) + (cond + ((equal find-done '(4)) (org-archive-all-done)) + ((equal find-done '(16)) (org-archive-all-old)) + (t ;; Save all relevant TODO keyword-relatex variables (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler (tr-org-todo-keywords-1 org-todo-keywords-1) @@ -375,7 +379,7 @@ this heading." (message "Subtree archived %s" (if (eq this-buffer buffer) (concat "under heading: " heading) - (concat "in file: " (abbreviate-file-name afile)))))) + (concat "in file: " (abbreviate-file-name afile))))))) (org-reveal) (if (looking-at "^[ \t]*$") (outline-next-visible-heading 1)))) @@ -456,13 +460,50 @@ sibling does not exist, it will be created at the end of the subtree." If the cursor is not on a headline, try all level 1 trees. If it is on a headline, try all direct children. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag." - (let ((re org-not-done-heading-regexp) re1 - (rea (concat ".*:" org-archive-tag ":")) + (org-archive-all-matches + (lambda (beg end) + (unless (re-search-forward org-not-done-heading-regexp end t) + "no open TODO items")) + tag)) + +(defun org-archive-all-old (&optional tag) + "Archive sublevels of the current tree with timestamps prior to today. +If the cursor is not on a headline, try all level 1 trees. If +it is on a headline, try all direct children. +When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag." + (org-archive-all-matches + (lambda (beg end) + (let (ts) + (and (re-search-forward org-ts-regexp end t) + (setq ts (match-string 0)) + (< (org-time-stamp-to-now ts) 0) + (if (not (looking-at + (concat "--\\(" org-ts-regexp "\\)"))) + (concat "old timestamp " ts) + (setq ts (concat "old timestamp " ts (match-string 0))) + (and (< (org-time-stamp-to-now (match-string 1)) 0) + ts))))) + tag)) + +(defun org-archive-all-matches (predicate &optional tag) + "Archive sublevels of the current tree that match PREDICATE. + +PREDICATE is a function of two arguments, BEG and END, which +specify the beginning and end of the headline being considered. +It is called with point positioned at BEG. The headline will be +archived if PREDICATE returns non-nil. If the return value of +PREDICATE is a string, it should describe the reason for +archiving the heading. + +If the cursor is not on a headline, try all level 1 trees. If it +is on a headline, try all direct children. When TAG is non-nil, +don't move trees, but mark them with the ARCHIVE tag." + (let ((rea (concat ".*:" org-archive-tag ":")) re1 (begm (make-marker)) (endm (make-marker)) - (question (if tag "Set ARCHIVE tag (no open TODO items)? " - "Move subtree to archive (no open TODO items)? ")) - beg end (cntarch 0)) + (question (if tag "Set ARCHIVE tag? " + "Move subtree to archive? ")) + reason beg end (cntarch 0)) (if (org-at-heading-p) (progn (setq re1 (concat "^" (regexp-quote @@ -482,11 +523,14 @@ When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag." (setq beg (match-beginning 0) end (save-excursion (org-end-of-subtree t) (point))) (goto-char beg) - (if (re-search-forward re end t) + (if (not (setq reason (funcall predicate beg end))) (goto-char end) (goto-char beg) (if (and (or (not tag) (not (looking-at rea))) - (y-or-n-p question)) + (y-or-n-p + (if (stringp reason) + (concat question "(" reason ")") + question))) (progn (if tag (org-toggle-tag org-archive-tag 'on) -- 1.8.5.5 --d6Gm4EdcadzBjdND--