forked from Trevoke/org-gtd.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-gtd-archive.el
103 lines (85 loc) · 3.58 KB
/
org-gtd-archive.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
;;; org-gtd-archive.el --- Logic to archive tasks -*- lexical-binding: t; coding: utf-8 -*-
;;
;; Copyright © 2019-2021 Aldric Giacomoni
;; Author: Aldric Giacomoni <[email protected]>
;; This file is not part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Archiving logic for org-gtd
;;
;;; Code:
(require 'org-archive)
(require 'org-element)
(require 'org-gtd-core)
(require 'org-gtd-agenda)
;;;###autoload
(defun org-gtd-archive-completed-items ()
"Archive everything that needs to be archived in your org-gtd."
(interactive)
(with-org-gtd-context
(org-gtd--archive-complete-projects)
(org-map-entries #'org-gtd--archive-completed-actions
"+LEVEL=2&+ORG_GTD=\"Actions\""
'agenda)
(org-map-entries #'org-gtd--archive-completed-actions
"+LEVEL=2&+ORG_GTD=\"Calendar\""
'agenda)
(org-map-entries #'org-gtd--archive-completed-actions
"+LEVEL=2&+ORG_GTD=\"Incubated\""
'agenda)))
(defun org-gtd--archive-complete-projects ()
"Archive all projects for which all actions/tasks are marked as done.
Done here is any done `org-todo-keyword'. For org-gtd this means DONE or CNCL."
(org-map-entries
(lambda ()
(let ((task-states (org-gtd--current-project-states)))
(when (or (org-gtd--project-complete-p task-states)
(org-gtd--project-canceled-p task-states))
(setq org-map-continue-from (org-element-property
:begin
(org-element-at-point)))
(org-archive-subtree-default))))
"+LEVEL=2&+ORG_GTD=\"Projects\""
'agenda))
(defun org-gtd--archive-completed-actions ()
"Private function. With point on heading, archive if entry is done."
(if (org-entry-is-done-p)
(progn
(setq org-map-continue-from (org-element-property
:begin
(org-element-at-point)))
(org-archive-subtree-default))))
(defun org-gtd--current-project-states ()
"Return a list of the task states for the current project."
(cdr (org-map-entries
(lambda ()
(org-entry-get
(org-gtd-projects--org-element-pom (org-element-at-point))
"TODO"))
t
'tree)))
(defun org-gtd--project-heading-p ()
"Determine if current heading is a project heading."
(not (org-entry-is-todo-p)))
(defun org-gtd--project-complete-p (task-states)
"Return t if project complete, nil otherwise.
A project is considered complete when all TASK-STATES are
marked with a done `org-todo-keyword'."
(seq-every-p (lambda (x) (string-equal x "DONE")) task-states))
(defun org-gtd--project-canceled-p (task-states)
"Return t if project canceled, nil otherwise.
A project is considered canceled when the last of the TASK-STATES is
marked with a canceled `org-todo-keyword'."
(string-equal "CNCL" (car (last task-states))))
(provide 'org-gtd-archive)
;;; org-gtd-archive.el ends here