1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw at gnu dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; 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 (defcustom org-habit-today-glyph ?
!
71 "Glyph character used to identify today."
76 (defcustom org-habit-completed-glyph ?
*
77 "Glyph character used to show completed days on which a task was done."
82 (defface org-habit-clear-face
83 '((((background light
)) (:background
"#8270f9"))
84 (((background dark
)) (:background
"blue")))
85 "Face for days on which a task shouldn't be done yet."
88 (defface org-habit-clear-future-face
89 '((((background light
)) (:background
"#d6e4fc"))
90 (((background dark
)) (:background
"midnightblue")))
91 "Face for future days on which a task shouldn't be done yet."
95 (defface org-habit-ready-face
96 '((((background light
)) (:background
"#4df946"))
97 (((background dark
)) (:background
"forestgreen")))
98 "Face for days on which a task should start to be done."
101 (defface org-habit-ready-future-face
102 '((((background light
)) (:background
"#acfca9"))
103 (((background dark
)) (:background
"darkgreen")))
104 "Face for days on which a task should start to be done."
108 (defface org-habit-alert-face
109 '((((background light
)) (:background
"#f5f946"))
110 (((background dark
)) (:background
"gold")))
111 "Face for days on which a task is due."
114 (defface org-habit-alert-future-face
115 '((((background light
)) (:background
"#fafca9"))
116 (((background dark
)) (:background
"darkgoldenrod")))
117 "Face for days on which a task is due."
121 (defface org-habit-overdue-face
122 '((((background light
)) (:background
"#f9372d"))
123 (((background dark
)) (:background
"firebrick")))
124 "Face for days on which a task is overdue."
127 (defface org-habit-overdue-future-face
128 '((((background light
)) (:background
"#fc9590"))
129 (((background dark
)) (:background
"darkred")))
130 "Face for days on which a task is overdue."
134 (defun org-habit-duration-to-days (ts)
135 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts
)
136 ;; lead time is specified.
137 (floor (* (string-to-number (match-string 1 ts
))
138 (cdr (assoc (match-string 2 ts
)
139 '(("d" .
1) ("w" .
7)
140 ("m" .
30.4) ("y" .
365.25))))))
141 (error "Invalid duration string: %s" ts
)))
143 (defun org-is-habit-p (&optional pom
)
144 "Is the task at POM or point a habit?"
145 (string= "habit" (org-entry-get (or pom
(point)) "STYLE")))
147 (defun org-habit-parse-todo (&optional pom
)
148 "Parse the TODO surrounding point for its habit-related data.
149 Returns a list with the following elements:
151 0: Scheduled date for the habit (may be in the past)
152 1: \".+\"-style repeater for the schedule, in days
153 2: Optional deadline (nil if not present)
154 3: If deadline, the repeater for the deadline, otherwise nil
155 4: A list of all the past dates this todo was mark closed
157 This list represents a \"habit\" for the rest of this module."
159 (if pom
(goto-char pom
))
160 (assert (org-is-habit-p (point)))
161 (let* ((scheduled (org-get-scheduled-time (point)))
162 (scheduled-repeat (org-get-repeat org-scheduled-string
))
163 (end (org-entry-end-position))
164 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
165 closed-dates deadline dr-days sr-days
)
167 (setq scheduled
(time-to-days scheduled
))
168 (error "Habit %s has no scheduled date" habit-entry
))
169 (unless scheduled-repeat
171 "Habit '%s' has no scheduled repeat period or has an incorrect one"
173 (setq sr-days
(org-habit-duration-to-days scheduled-repeat
))
174 (unless (> sr-days
0)
175 (error "Habit %s scheduled repeat period is less than 1d" habit-entry
))
176 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat
)
177 (setq dr-days
(org-habit-duration-to-days
178 (match-string-no-properties 1 scheduled-repeat
)))
179 (if (<= dr-days sr-days
)
180 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
181 habit-entry scheduled-repeat
))
182 (setq deadline
(+ scheduled
(- dr-days sr-days
))))
183 (org-back-to-heading t
)
184 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days
))
185 (reversed org-log-states-order-reversed
)
186 (search (if reversed
're-search-forward
're-search-backward
))
187 (limit (if reversed end
(point)))
189 (unless reversed
(goto-char end
))
190 (while (and (< count maxdays
)
191 (funcall search
"- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t
))
193 (org-time-string-to-time (match-string-no-properties 1)))
195 (setq count
(1+ count
))))
196 (list scheduled sr-days deadline dr-days closed-dates
))))
198 (defsubst org-habit-scheduled
(habit)
200 (defsubst org-habit-scheduled-repeat
(habit)
202 (defsubst org-habit-deadline
(habit)
203 (let ((deadline (nth 2 habit
)))
206 (+ (org-habit-scheduled habit
)
207 (1- (org-habit-scheduled-repeat habit
)))
208 (org-habit-scheduled habit
)))))
209 (defsubst org-habit-deadline-repeat
(habit)
211 (org-habit-scheduled-repeat habit
)))
212 (defsubst org-habit-done-dates
(habit)
215 (defsubst org-habit-get-priority
(habit &optional moment
)
216 "Determine the relative priority of a habit.
217 This must take into account not just urgency, but consistency as well."
219 (now (if moment
(time-to-days moment
) (org-today)))
220 (scheduled (org-habit-scheduled habit
))
221 (deadline (org-habit-deadline habit
)))
222 ;; add 10 for every day past the scheduled date, and subtract for every
224 (setq pri
(+ pri
(* (- now scheduled
) 10)))
225 ;; add 50 if the deadline is today
226 (if (and (/= scheduled deadline
)
228 (setq pri
(+ pri
50)))
229 ;; add 100 for every day beyond the deadline date, and subtract 10 for
230 ;; every day before it
231 (let ((slip (- now
(1- deadline
))))
233 (setq pri
(+ pri
(* slip
100)))
234 (setq pri
(+ pri
(* slip
10)))))
237 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep
)
238 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
239 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
240 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
242 Habits are assigned colors on the following basis:
243 Blue Task is before the scheduled date.
244 Green Task is on or after scheduled date, but before the
245 end of the schedule's repeat period.
246 Yellow If the task has a deadline, then it is after schedule's
247 repeat period, but before the deadline.
248 Orange The task has reached the deadline day, or if there is
249 no deadline, the end of the schedule's repeat period.
250 Red The task has gone beyond the deadline day or the
251 schedule's repeat period."
252 (let* ((scheduled (or scheduled-days
(org-habit-scheduled habit
)))
253 (s-repeat (org-habit-scheduled-repeat habit
))
254 (scheduled-end (+ scheduled
(1- s-repeat
)))
255 (d-repeat (org-habit-deadline-repeat habit
))
256 (deadline (if scheduled-days
257 (+ scheduled-days
(- d-repeat s-repeat
))
258 (org-habit-deadline habit
)))
259 (m-days (or now-days
(time-to-days (current-time)))))
261 ((< m-days scheduled
)
262 '(org-habit-clear-face . org-habit-clear-future-face
))
264 '(org-habit-ready-face . org-habit-ready-future-face
))
267 '(org-habit-ready-face . org-habit-ready-future-face
)
268 '(org-habit-alert-face . org-habit-alert-future-face
)))
270 '(org-habit-overdue-face . org-habit-overdue-future-face
)))))
272 (defun org-habit-build-graph (habit starting current ending
)
273 "Build a graph for the given HABIT, from STARTING to ENDING.
274 CURRENT gives the current time between STARTING and ENDING, for
275 the purpose of drawing the graph. It need not be the actual
277 (let* ((done-dates (sort (org-habit-done-dates habit
) '<))
278 (scheduled (org-habit-scheduled habit
))
279 (s-repeat (org-habit-scheduled-repeat habit
))
280 (start (time-to-days starting
))
281 (now (time-to-days current
))
282 (end (time-to-days ending
))
283 (graph (make-string (1+ (- end start
)) ?\
))
286 (while (and done-dates
(< (car done-dates
) start
))
287 (setq last-done-date
(car done-dates
)
288 done-dates
(cdr done-dates
)))
290 (let* ((in-the-past-p (< start now
))
291 (todayp (= start now
))
292 (donep (and done-dates
293 (= start
(car done-dates
))))
294 (faces (if (and in-the-past-p
296 (not (< scheduled now
)))
297 '(org-habit-clear-face . org-habit-clear-future-face
)
299 habit start
(and in-the-past-p
301 (+ last-done-date s-repeat
)
306 (let ((done-time (time-add
309 (- start
(time-to-days starting
))))))
311 (aset graph index org-habit-completed-glyph
)
314 index
(1+ index
) 'help-echo
315 (format-time-string (org-time-stamp-format) done-time
) graph
)
316 (while (and done-dates
317 (= start
(car done-dates
)))
318 (setq last-done-date
(car done-dates
)
319 done-dates
(cdr done-dates
))))
321 (aset graph index org-habit-today-glyph
)))
322 (setq face
(if (or in-the-past-p todayp
)
325 (if (and in-the-past-p
326 (not (eq face
'org-habit-overdue-face
))
328 (setq face
(cdr faces
)))
329 (put-text-property index
(1+ index
) 'face face graph
))
330 (setq start
(1+ start
)
334 (defun org-habit-insert-consistency-graphs (&optional line
)
335 "Insert consistency graph for any habitual tasks."
336 (let ((inhibit-read-only t
) l c
337 (buffer-invisibility-spec '(org-link))
338 (moment (time-subtract (current-time)
339 (list 0 (* 3600 org-extend-today-until
) 0)))
341 ;; Disable filters; this helps with alignment if there are links.
343 (when (overlay-get ol
'invisible
)
344 (overlay-put ol
'invisible nil
)
345 (setq disabled-overlays
(cons ol disabled-overlays
))))
346 (overlays-in (point-min) (point-max)))
348 (goto-char (if line
(point-at-bol) (point-min)))
350 (let ((habit (get-text-property (point) 'org-habit-p
)))
352 (move-to-column org-habit-graph-column t
)
353 (delete-char (min (+ 1 org-habit-preceding-days
354 org-habit-following-days
)
355 (- (line-end-position) (point))))
356 (insert-before-markers
357 (org-habit-build-graph
359 (time-subtract moment
(days-to-time org-habit-preceding-days
))
361 (time-add moment
(days-to-time org-habit-following-days
))))))
363 (mapc (lambda (ol) (overlay-put ol
'invisible t
))
366 (defun org-habit-toggle-habits ()
367 "Toggle display of habits in an agenda buffer."
369 (org-agenda-check-type t
'agenda
)
370 (setq org-habit-show-habits
(not org-habit-show-habits
))
372 (org-agenda-set-mode-name)
373 (message "Habits turned %s"
374 (if org-habit-show-habits
"on" "off")))
376 (org-defkey org-agenda-mode-map
"K" 'org-habit-toggle-habits
)
380 ;;; org-habit.el ends here