1 ;;; org-habit.el --- The habit tracking code for Org-mode
4 ;; Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw at gnu dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;; This file contains the habit tracking code for Org-mode
37 (defgroup org-habit nil
38 "Options concerning habit tracking in Org-mode."
42 (defcustom org-habit-graph-column
40
43 "The absolute column at which to insert habit consistency graphs.
44 Note that consistency graphs will overwrite anything else in the buffer."
48 (defcustom org-habit-preceding-days
21
49 "Number of days before today to appear in consistency graphs."
53 (defcustom org-habit-following-days
7
54 "Number of days after today to appear in consistency graphs."
58 (defcustom org-habit-show-habits t
59 "If non-nil, show habits in agenda buffers."
63 (defcustom org-habit-show-habits-only-for-today t
64 "If non-nil, only show habits on today's agenda, and not for future days.
65 Note that even when shown for future days, the graph is always
66 relative to the current effective date."
70 (defface org-habit-clear-face
71 '((((background light
)) (:background
"slateblue"))
72 (((background dark
)) (:background
"blue")))
73 "Face for days on which a task shouldn't be done yet."
76 (defface org-habit-clear-future-face
77 '((((background light
)) (:background
"powderblue"))
78 (((background dark
)) (:background
"midnightblue")))
79 "Face for future days on which a task shouldn't be done yet."
83 (defface org-habit-ready-face
84 '((((background light
)) (:background
"green"))
85 (((background dark
)) (:background
"forestgreen")))
86 "Face for days on which a task should start to be done."
89 (defface org-habit-ready-future-face
90 '((((background light
)) (:background
"palegreen"))
91 (((background dark
)) (:background
"darkgreen")))
92 "Face for days on which a task should start to be done."
96 (defface org-habit-alert-face
97 '((((background light
)) (:background
"yellow"))
98 (((background dark
)) (:background
"gold")))
99 "Face for days on which a task is due."
102 (defface org-habit-alert-future-face
103 '((((background light
)) (:background
"palegoldenrod"))
104 (((background dark
)) (:background
"darkgoldenrod")))
105 "Face for days on which a task is due."
109 (defface org-habit-overdue-face
110 '((((background light
)) (:background
"red"))
111 (((background dark
)) (:background
"firebrick")))
112 "Face for days on which a task is overdue."
115 (defface org-habit-overdue-future-face
116 '((((background light
)) (:background
"mistyrose"))
117 (((background dark
)) (:background
"darkred")))
118 "Face for days on which a task is overdue."
122 (defun org-habit-duration-to-days (ts)
123 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts
)
124 ;; lead time is specified.
125 (floor (* (string-to-number (match-string 1 ts
))
126 (cdr (assoc (match-string 2 ts
)
127 '(("d" .
1) ("w" .
7)
128 ("m" .
30.4) ("y" .
365.25))))))
129 (error "Invalid duration string: %s" ts
)))
131 (defun org-is-habit-p (&optional pom
)
132 (string= "habit" (org-entry-get (or pom
(point)) "STYLE")))
134 (defun org-habit-parse-todo (&optional pom
)
135 "Parse the TODO surrounding point for its habit-related data.
136 Returns a list with the following elements:
138 0: Scheduled date for the habit (may be in the past)
139 1: \".+\"-style repeater for the schedule, in days
140 2: Optional deadline (nil if not present)
141 3: If deadline, the repeater for the deadline, otherwise nil
142 4: A list of all the past dates this todo was mark closed
144 This list represents a \"habit\" for the rest of this module."
146 (if pom
(goto-char pom
))
147 (assert (org-is-habit-p (point)))
148 (let* ((scheduled (org-get-scheduled-time (point)))
149 (scheduled-repeat (org-get-repeat org-scheduled-string
))
150 (sr-days (org-habit-duration-to-days scheduled-repeat
))
151 (end (org-entry-end-position))
152 closed-dates deadline dr-days
)
154 (setq scheduled
(time-to-days scheduled
))
155 (error "Habit has no scheduled date"))
156 (unless scheduled-repeat
157 (error "Habit has no scheduled repeat period"))
158 (unless (> sr-days
0)
159 (error "Habit's scheduled repeat period is less than 1d"))
160 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat
)
161 (setq dr-days
(org-habit-duration-to-days
162 (match-string-no-properties 1 scheduled-repeat
)))
163 (if (<= dr-days sr-days
)
164 (error "Habit's deadline repeat period is less than or equal to scheduled"))
165 (setq deadline
(+ scheduled
(- dr-days sr-days
))))
166 (org-back-to-heading t
)
167 (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t
)
169 (org-time-string-to-time (match-string-no-properties 1)))
171 (list scheduled sr-days deadline dr-days closed-dates
))))
173 (defsubst org-habit-scheduled
(habit)
175 (defsubst org-habit-scheduled-repeat
(habit)
177 (defsubst org-habit-deadline
(habit)
178 (let ((deadline (nth 2 habit
)))
180 (+ (org-habit-scheduled habit
)
181 (1- (org-habit-scheduled-repeat habit
))))))
182 (defsubst org-habit-deadline-repeat
(habit)
184 (org-habit-scheduled-repeat habit
)))
185 (defsubst org-habit-done-dates
(habit)
188 (defsubst org-habit-get-priority
(habit &optional moment
)
189 "Determine the relative priority of a habit.
190 This must take into account not just urgency, but consistency as well."
194 (time-subtract (current-time)
195 (list 0 (* 3600 org-extend-today-until
) 0)))))
196 (scheduled (org-habit-scheduled habit
))
197 (deadline (org-habit-deadline habit
)))
198 ;; add 10 for every day past the scheduled date, and subtract for every
200 (setq pri
(+ pri
(* (- now scheduled
) 10)))
201 ;; add 50 if the deadline is today
202 (if (and (/= scheduled deadline
)
204 (setq pri
(+ pri
50)))
205 ;; add 100 for every day beyond the deadline date, and subtract 10 for
206 ;; every day before it
207 (let ((slip (- now
(1- deadline
))))
209 (setq pri
(+ pri
(* slip
100)))
210 (setq pri
(+ pri
(* slip
10)))))
213 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep
)
214 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
215 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
216 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
218 Habits are assigned colors on the following basis:
219 Blue Task is before the scheduled date.
220 Green Task is on or after scheduled date, but before the
221 end of the schedule's repeat period.
222 Yellow If the task has a deadline, then it is after schedule's
223 repeat period, but before the deadline.
224 Orange The task has reached the deadline day, or if there is
225 no deadline, the end of the schedule's repeat period.
226 Red The task has gone beyond the deadline day or the
227 schedule's repeat period."
228 (let* ((scheduled (or scheduled-days
(org-habit-scheduled habit
)))
229 (s-repeat (org-habit-scheduled-repeat habit
))
230 (scheduled-end (+ scheduled
(1- s-repeat
)))
231 (d-repeat (org-habit-deadline-repeat habit
))
232 (deadline (if scheduled-days
233 (+ scheduled-days
(- d-repeat s-repeat
))
234 (org-habit-deadline habit
)))
235 (m-days (or now-days
(time-to-days (current-time)))))
237 ((< m-days scheduled
)
238 '(org-habit-clear-face . org-habit-clear-future-face
))
240 '(org-habit-ready-face . org-habit-ready-future-face
))
243 '(org-habit-ready-face . org-habit-ready-future-face
)
244 '(org-habit-alert-face . org-habit-alert-future-face
)))
246 '(org-habit-overdue-face . org-habit-overdue-future-face
)))))
248 (defun org-habit-build-graph (habit starting current ending
)
249 "Build a graph for the given HABIT, from STARTING to ENDING.
250 CURRENT gives the current time between STARTING and ENDING, for
251 the purpose of drawing the graph. It need not be the actual
253 (let* ((done-dates (sort (org-habit-done-dates habit
) '<))
254 (scheduled (org-habit-scheduled habit
))
255 (s-repeat (org-habit-scheduled-repeat habit
))
256 (start (time-to-days starting
))
257 (now (time-to-days current
))
258 (end (time-to-days ending
))
259 (graph (make-string (1+ (- end start
)) ?\
))
262 (while (and done-dates
(< (car done-dates
) start
))
263 (setq last-done-date
(car done-dates
)
264 done-dates
(cdr done-dates
)))
266 (let* ((in-the-past-p (< start now
))
267 (todayp (= start now
))
268 (donep (and done-dates
269 (= start
(car done-dates
))))
270 (faces (if (and in-the-past-p
272 (not (< scheduled now
)))
273 '(org-habit-clear-face . org-habit-clear-future-face
)
275 habit start
(and in-the-past-p
277 (+ last-done-date s-repeat
)
283 (aset graph index ?
*)
285 (while (and done-dates
286 (= start
(car done-dates
)))
287 (setq last-done-date
(car done-dates
)
288 done-dates
(cdr done-dates
))))
290 (aset graph index ?
!)))
291 (setq face
(if (or in-the-past-p todayp
)
294 (if (and in-the-past-p
295 (not (eq face
'org-habit-overdue-face
))
297 (setq face
(cdr faces
)))
298 (put-text-property index
(1+ index
) 'face face graph
))
299 (setq start
(1+ start
)
303 (defun org-habit-insert-consistency-graphs (&optional line
)
304 "Insert consistency graph for any habitual tasks."
305 (let ((inhibit-read-only t
) l c
306 (moment (time-subtract (current-time)
307 (list 0 (* 3600 org-extend-today-until
) 0))))
309 (goto-char (if line
(point-at-bol) (point-min)))
311 (let ((habit (get-text-property (point) 'org-habit-p
)))
313 (move-to-column org-habit-graph-column t
)
314 (delete-char (min (+ 1 org-habit-preceding-days
315 org-habit-following-days
)
316 (- (line-end-position) (point))))
317 (insert (org-habit-build-graph
319 (time-subtract moment
320 (days-to-time org-habit-preceding-days
))
323 (days-to-time org-habit-following-days
))))))
326 (defun org-habit-toggle-habits ()
327 "Toggle display of habits in an agenda buffer."
329 (org-agenda-check-type t
'agenda
)
330 (setq org-habit-show-habits
(not org-habit-show-habits
))
332 (org-agenda-set-mode-name)
333 (message "Habits turned %s"
334 (if org-habit-show-habits
"on" "off")))
336 (org-defkey org-agenda-mode-map
"K" 'org-habit-toggle-habits
)
342 ;;; org-habit.el ends here