This repository has been archived by the owner on Sep 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
org-zk-derived-tasks.el
66 lines (56 loc) · 2.22 KB
/
org-zk-derived-tasks.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
(defvar org-zk-derived-task-file "/home/leon/org/derived_tasks.org"
"File to add derived tasks to")
(defvar org-zk-derived-tasks
(make-hash-table :size 10 :test 'equal)
"Hash-table of derived task hooks")
(defvar org-zk-derived-tasks-interval (* 30 60)
"Update derived tasks every n seconds.")
(defclass org-zk-derived-task ()
((title :initarg :title)
(priority :initarg :priority)
(tags :initarg :tags :initform '())
(predicate :initarg :predicate)))
(defmethod org-zk-derived-task-due-p ((task org-zk-derived-task))
"Check if a derived task is due."
(funcall (oref task predicate)))
(defmethod org-zk-derived-task-new-p ((task org-zk-derived-task) current)
"Check if a derived task is not already active in `org-zk-derived-task-file'"
(with-slots (title) task
(not (--any
(and (string= (plist-get it :title) title)
(eq (plist-get it :todo-type) 'todo))
current))))
(defmacro def-org-zk-derived-task (title priority tags &rest body)
"Helper macro for defining derived tasks."
`(puthash
,title
(make-instance
'org-zk-derived-task
:title ,title
:priority ,priority
:tags ,tags
:predicate (lambda () ,@body))
org-zk-derived-tasks))
(defun org-zk-derived-tasks-new ()
"Generate a list of new derived tasks not already contained in
the `org-zk-derived-task-file'."
(let ((current (plist-get
(org-el-cache-get org-zk-cache org-zk-derived-task-file)
:headlines)))
(->> (hash-table-values org-zk-derived-tasks)
(-filter 'org-zk-derived-task-due-p)
(--filter (org-zk-derived-task-new-p it current)))))
(defun org-zk-derived-tasks-update ()
"Add new tasks to the `org-zk-derived-task-file'."
(-if-let (tasks (org-zk-derived-tasks-new))
(org-zk-in-file org-zk-derived-task-file
(save-excursion
(goto-char (point-max))
(dolist (task tasks)
(with-slots (title priority tags) task
(org-insert-heading nil nil t)
(insert (format "NEXT [#%s] %s" priority title))
(org-set-tags tags))))
(save-buffer))))
(run-at-time "10 min" org-zk-derived-tasks-interval 'org-zk-derived-tasks-update)
(provide 'org-zk-derived-tasks)