Skip to content

Instantly share code, notes, and snippets.

@gregnewman
Created December 15, 2009 14:15
Show Gist options
  • Save gregnewman/256965 to your computer and use it in GitHub Desktop.
Save gregnewman/256965 to your computer and use it in GitHub Desktop.
(defvar org-my-archive-expiry-days 1
"The number of days after which a completed task should be auto-archived.
This can be 0 for immediate, or a floating point value.")
(defconst org-my-ts-regexp "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]>\r\n]*?\\)[]>]"
"Regular expression for fast inactive time stamp matching.")
(defun org-my-closing-time ()
(let* ((state-regexp
(concat "- State \"\\(?:" (regexp-opt org-done-keywords)
"\\)\"\\s-*\\[\\([^]\n]+\\)\\]"))
(regexp (concat "\\(" state-regexp "\\|" org-my-ts-regexp "\\)"))
(end (save-excursion
(outline-next-heading)
(point)))
begin
end-time)
(goto-char (line-beginning-position))
(while (re-search-forward regexp end t)
(let ((moment (org-parse-time-string (match-string 1))))
(if (or (not end-time)
(time-less-p (apply #'encode-time end-time)
(apply #'encode-time moment)))
(setq end-time moment))))
(goto-char end)
end-time))
(defun org-my-archive-done-tasks ()
(interactive)
(save-excursion
(goto-char (point-min))
(let ((done-regexp
(concat "^\\*\\* \\(" (regexp-opt org-done-keywords) "\\) ")))
(while (re-search-forward done-regexp nil t)
(if (>= (time-to-number-of-days
(time-subtract (current-time)
(apply #'encode-time (org-my-closing-time))))
org-my-archive-expiry-days)
(org-archive-subtree))))
(save-buffer)))
(defalias 'archive-done-tasks 'org-my-archive-done-tasks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment