forked from jkitchin/scimax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-db-agenda.el
205 lines (172 loc) · 6.25 KB
/
org-db-agenda.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
;;; org-db-agenda.el --- Agenda from org-db
;;; Commentary:
;; TODO: add scheduled tasks
(require 'org-archive)
(require 'org-db)
(require 'pcache)
(defun org-db-agenda--candidates (before-date)
"Get headings with deadlines before BEFORE-DATE
Examples:
today Due by today
+1w Due in a week
+1m Due in a month
"
(interactive)
(let* ((deadline-headings (with-org-db
(emacsql org-db [:select [headlines:level headlines:title headlines:tags
files:filename headlines:begin
headlines:deadline
files:last-updated
headlines:todo-keyword]
:from headlines
:inner :join files
:on (= files:rowid headlines:filename-id)
;only get deadlines before now
:where (and
(= headlines:todo-keyword "TODO")
(is headlines:archivedp (not nil))
(< headlines:deadline $s1))
:order :by headlines:deadline :desc]
(float-time (org-read-date t t before-date)))))
(todo-headings (with-org-db
(emacsql org-db [:select [headlines:level headlines:title headlines:tags
files:filename headlines:begin
headlines:deadline
files:last-updated
headlines:todo-keyword]
:from headlines
:inner :join files
:on (= files:rowid headlines:filename-id)
;; headlines with TODO state but no deadline, and not archived
:where (and
(= headlines:todo-keyword "TODO")
(is headlines:deadline (not nil))
(is headlines:archivedp (not nil)))
:order :by files:last-updated :desc])))
(ignores (pcache-get (pcache-repository :file "org-db-agenda-ignore") 'ignores))
(candidates (cl-loop for (level title tags filename begin deadline last-updated todo-keyword)
in (append deadline-headings todo-headings)
when (not (member filename ignores))
collect
(cons
(format "%28s|%100s|%20s|%s|%s"
(s-pad-right 28 " "
(if deadline
(format-time-string
"<%Y-%m-%d %a %H:%M:%S>"
(seconds-to-time deadline))
" "))
(s-pad-right 100 " " (concat (make-string
level
(string-to-char "*"))
" "
todo-keyword " "
title))
(s-pad-right 20 " " (or tags ""))
filename last-updated)
(list
:file filename
:last-updated last-updated
:begin begin
:title title)))))
candidates))
(defun org-db-agenda-transformer (candidate)
"Add colors to candidates."
(let* ((now (float-time (current-time)))
(ts (string-match org-element--timestamp-regexp candidate))
(es (when ts (float-time (org-timestamp-to-time
(org-timestamp-from-string
(match-string 0 candidate)))))))
(cond
((null es) candidate)
((> es now) (propertize candidate 'face '(:foreground "green4")))
((< es now) (propertize candidate 'face '(:foreground "dark red"))))))
(ivy-configure 'org-db-agenda :display-transformer-fn
#'org-db-agenda-transformer)
(defun org-db-agenda--done (candidate)
"Mark the current candidate as done."
(let ((plist (cdr candidate)))
(find-file (plist-get plist :file))
(goto-char (plist-get plist :begin))
(org-todo "DONE")
(save-buffer)
(org-db-update-buffer t)))
(defun org-db-agenda--archive (candidate)
"Mark the current candidate as done."
(let ((plist (cdr candidate)))
(find-file (plist-get plist :file))
(goto-char (plist-get plist :begin))
(org-archive-set-tag)
(save-buffer)
(org-db-update-buffer t)))
(defun org-db-agenda--archive-subtree (candidate)
"Mark the current candidate as done."
(let ((plist (cdr candidate)))
(find-file (plist-get plist :file))
(goto-char (plist-get plist :begin))
(org-archive-subtree)
(save-buffer)
(org-db-update-buffer t)))
(defun org-db-agenda--ignore (candidate)
"add CANDIDATE to ignore list."
(let* ((plist (cdr candidate))
(repo (pcache-repository :file "org-db-agenda-ignore"))
(ignores (pcache-get repo 'ignores)))
(setq ignores (delete-dups (append ignores (list (plist-get plist :file)))))
(pcache-put repo 'ignores ignores)))
(defun org-db-agenda--update (candidate)
"Update current entry"
(let* ((plist (cdr candidate))
(fname (plist-get plist :file)))
(if (file-exists-p fname)
(with-current-buffer (find-file-noselect fname)
(org-db-update-buffer t))
(org-db-remove-file fname))))
(defun org-db-agenda (before)
(interactive (list (read-string "Before (e.g. +2w)" "+2w")))
(let* ((candidates (org-db-agenda--candidates before)))
(ivy-read "heading: " candidates
:caller 'org-db-agenda
:action
'(1
("o" org-db-headings--open "Open to heading.")
("d" org-db-agenda--done "Mark entry done")
("a" org-db-agenda--archive "Add archive tag")
("A" org-db-agenda--archive-subtree "Archive subtree")
("i" org-db-agenda--ignore "Ignore this file")
("u" org-db-agenda--update "Update file in database")
("U" (lambda (_) (org-db-agenda-refresh "+2w")) "Refresh agenda database")))))
(defun scimax-all-headings-done ()
"Mark all entries in buffer as DONE."
(interactive)
(org-map-entries
(lambda ()
(when (org-get-repeat)
(org-todo -1))
(when (string= (org-get-todo-state) "TODO")
(org-todo "DONE")))))
(defun scimax-archive-todo-headings ()
"Make todo entries archived."
(interactive)
(org-map-entries
(lambda ()
(when (string= (org-get-todo-state) "TODO")
(org-archive-set-tag)))))
(defun org-db-agenda-refresh (before)
"Refresh agenda files."
(interactive (list (read-string "Before (e.g. +2w)" "+2w")))
(cl-loop for candidate in (org-db-agenda--candidates before) do
(let* ((fname (plist-get (cdr candidate) :file))
(open (get-file-buffer fname))
(buf (when (file-exists-p fname) (find-file-noselect fname))))
(if (null buf)
(let* ((filename-id (org-db-get-filename-id fname)))
(with-org-db
(emacsql org-db [:delete :from files :where (= rowid $s2)] filename-id)
(org-db-log "Removed %s from the database." fname)))
(with-current-buffer buf
(org-db-update-buffer t))
(when (null open)
(kill-buffer buf))))))
(provide 'org-db-agenda)
;;; org-db-agenda.el ends here