1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009-2011 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."
75 (defcustom org-habit-completed-glyph ?
*
76 "Glyph character used to show completed days on which a task was done."
80 (defface org-habit-clear-face
81 '((((background light
)) (:background
"#8270f9"))
82 (((background dark
)) (:background
"blue")))
83 "Face for days on which a task shouldn't be done yet."
86 (defface org-habit-clear-future-face
87 '((((background light
)) (:background
"#d6e4fc"))
88 (((background dark
)) (:background
"midnightblue")))
89 "Face for future days on which a task shouldn't be done yet."
93 (defface org-habit-ready-face
94 '((((background light
)) (:background
"#4df946"))
95 (((background dark
)) (:background
"forestgreen")))
96 "Face for days on which a task should start to be done."
99 (defface org-habit-ready-future-face
100 '((((background light
)) (:background
"#acfca9"))
101 (((background dark
)) (:background
"darkgreen")))
102 "Face for days on which a task should start to be done."
106 (defface org-habit-alert-face
107 '((((background light
)) (:background
"#f5f946"))
108 (((background dark
)) (:background
"gold")))
109 "Face for days on which a task is due."
112 (defface org-habit-alert-future-face
113 '((((background light
)) (:background
"#fafca9"))
114 (((background dark
)) (:background
"darkgoldenrod")))
115 "Face for days on which a task is due."
119 (defface org-habit-overdue-face
120 '((((background light
)) (:background
"#f9372d"))
121 (((background dark
)) (:background
"firebrick")))
122 "Face for days on which a task is overdue."
125 (defface org-habit-overdue-future-face
126 '((((background light
)) (:background
"#fc9590"))
127 (((background dark
)) (:background
"darkred")))
128 "Face for days on which a task is overdue."
132 (defun org-habit-duration-to-days (ts)
133 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts
)
134 ;; lead time is specified.
135 (floor (* (string-to-number (match-string 1 ts
))
136 (cdr (assoc (match-string 2 ts
)
137 '(("d" .
1) ("w" .
7)
138 ("m" .
30.4) ("y" .
365.25))))))
139 (error "Invalid duration string: %s" ts
)))
141 (defun org-is-habit-p (&optional pom
)
142 "Is the task at POM or point a habit?"
143 (string= "habit" (org-entry-get (or pom
(point)) "STYLE")))
145 (defun org-habit-parse-todo (&optional pom
)
146 "Parse the TODO surrounding point for its habit-related data.
147 Returns a list with the following elements:
149 0: Scheduled date for the habit (may be in the past)
150 1: \".+\"-style repeater for the schedule, in days
151 2: Optional deadline (nil if not present)
152 3: If deadline, the repeater for the deadline, otherwise nil
153 4: A list of all the past dates this todo was mark closed
155 This list represents a \"habit\" for the rest of this module."
157 (if pom
(goto-char pom
))
158 (assert (org-is-habit-p (point)))
159 (let* ((scheduled (org-get-scheduled-time (point)))
160 (scheduled-repeat (org-get-repeat org-scheduled-string
))
161 (end (org-entry-end-position))
162 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
163 closed-dates deadline dr-days sr-days
)
165 (setq scheduled
(time-to-days scheduled
))
166 (error "Habit %s has no scheduled date" habit-entry
))
167 (unless scheduled-repeat
169 "Habit '%s' has no scheduled repeat period or has an incorrect one"
171 (setq sr-days
(org-habit-duration-to-days scheduled-repeat
))
172 (unless (> sr-days
0)
173 (error "Habit %s scheduled repeat period is less than 1d" habit-entry
))
174 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat
)
175 (setq dr-days
(org-habit-duration-to-days
176 (match-string-no-properties 1 scheduled-repeat
)))
177 (if (<= dr-days sr-days
)
178 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
179 habit-entry scheduled-repeat
))
180 (setq deadline
(+ scheduled
(- dr-days sr-days
))))
181 (org-back-to-heading t
)
182 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days
))
183 (reversed org-log-states-order-reversed
)
184 (search (if reversed
're-search-forward
're-search-backward
))
185 (limit (if reversed end
(point)))
187 (unless reversed
(goto-char end
))
188 (while (and (< count maxdays
)
189 (funcall search
"- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t
))
191 (org-time-string-to-time (match-string-no-properties 1)))
193 (setq count
(1+ count
))))
194 (list scheduled sr-days deadline dr-days closed-dates
))))
196 (defsubst org-habit-scheduled
(habit)
198 (defsubst org-habit-scheduled-repeat
(habit)
200 (defsubst org-habit-deadline
(habit)
201 (let ((deadline (nth 2 habit
)))
204 (+ (org-habit-scheduled habit
)
205 (1- (org-habit-scheduled-repeat habit
)))
206 (org-habit-scheduled habit
)))))
207 (defsubst org-habit-deadline-repeat
(habit)
209 (org-habit-scheduled-repeat habit
)))
210 (defsubst org-habit-done-dates
(habit)
213 (defsubst org-habit-get-priority
(habit &optional moment
)
214 "Determine the relative priority of a habit.
215 This must take into account not just urgency, but consistency as well."
217 (now (if moment
(time-to-days moment
) (org-today)))
218 (scheduled (org-habit-scheduled habit
))
219 (deadline (org-habit-deadline habit
)))
220 ;; add 10 for every day past the scheduled date, and subtract for every
222 (setq pri
(+ pri
(* (- now scheduled
) 10)))
223 ;; add 50 if the deadline is today
224 (if (and (/= scheduled deadline
)
226 (setq pri
(+ pri
50)))
227 ;; add 100 for every day beyond the deadline date, and subtract 10 for
228 ;; every day before it
229 (let ((slip (- now
(1- deadline
))))
231 (setq pri
(+ pri
(* slip
100)))
232 (setq pri
(+ pri
(* slip
10)))))
235 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep
)
236 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
237 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
238 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
240 Habits are assigned colors on the following basis:
241 Blue Task is before the scheduled date.
242 Green Task is on or after scheduled date, but before the
243 end of the schedule's repeat period.
244 Yellow If the task has a deadline, then it is after schedule's
245 repeat period, but before the deadline.
246 Orange The task has reached the deadline day, or if there is
247 no deadline, the end of the schedule's repeat period.
248 Red The task has gone beyond the deadline day or the
249 schedule's repeat period."
250 (let* ((scheduled (or scheduled-days
(org-habit-scheduled habit
)))
251 (s-repeat (org-habit-scheduled-repeat habit
))
252 (scheduled-end (+ scheduled
(1- s-repeat
)))
253 (d-repeat (org-habit-deadline-repeat habit
))
254 (deadline (if scheduled-days
255 (+ scheduled-days
(- d-repeat s-repeat
))
256 (org-habit-deadline habit
)))
257 (m-days (or now-days
(time-to-days (current-time)))))
259 ((< m-days scheduled
)
260 '(org-habit-clear-face . org-habit-clear-future-face
))
262 '(org-habit-ready-face . org-habit-ready-future-face
))
265 '(org-habit-ready-face . org-habit-ready-future-face
)
266 '(org-habit-alert-face . org-habit-alert-future-face
)))
268 '(org-habit-overdue-face . org-habit-overdue-future-face
)))))
270 (defun org-habit-build-graph (habit starting current ending
)
271 "Build a graph for the given HABIT, from STARTING to ENDING.
272 CURRENT gives the current time between STARTING and ENDING, for
273 the purpose of drawing the graph. It need not be the actual
275 (let* ((done-dates (sort (org-habit-done-dates habit
) '<))
276 (scheduled (org-habit-scheduled habit
))
277 (s-repeat (org-habit-scheduled-repeat habit
))
278 (start (time-to-days starting
))
279 (now (time-to-days current
))
280 (end (time-to-days ending
))
281 (graph (make-string (1+ (- end start
)) ?\
))
284 (while (and done-dates
(< (car done-dates
) start
))
285 (setq last-done-date
(car done-dates
)
286 done-dates
(cdr done-dates
)))
288 (let* ((in-the-past-p (< start now
))
289 (todayp (= start now
))
290 (donep (and done-dates
291 (= start
(car done-dates
))))
292 (faces (if (and in-the-past-p
294 (not (< scheduled now
)))
295 '(org-habit-clear-face . org-habit-clear-future-face
)
297 habit start
(and in-the-past-p
299 (+ last-done-date s-repeat
)
304 (let ((done-time (time-add
307 (- start
(time-to-days starting
))))))
309 (aset graph index org-habit-completed-glyph
)
312 index
(1+ index
) 'help-echo
313 (format-time-string (org-time-stamp-format) done-time
) graph
)
314 (while (and done-dates
315 (= start
(car done-dates
)))
316 (setq last-done-date
(car done-dates
)
317 done-dates
(cdr done-dates
))))
319 (aset graph index org-habit-today-glyph
)))
320 (setq face
(if (or in-the-past-p todayp
)
323 (if (and in-the-past-p
324 (not (eq face
'org-habit-overdue-face
))
326 (setq face
(cdr faces
)))
327 (put-text-property index
(1+ index
) 'face face graph
))
328 (setq start
(1+ start
)
332 (defun org-habit-insert-consistency-graphs (&optional line
)
333 "Insert consistency graph for any habitual tasks."
334 (let ((inhibit-read-only t
) l c
335 (buffer-invisibility-spec '(org-link))
336 (moment (time-subtract (current-time)
337 (list 0 (* 3600 org-extend-today-until
) 0))))
339 (goto-char (if line
(point-at-bol) (point-min)))
341 (let ((habit (get-text-property (point) 'org-habit-p
)))
343 (move-to-column org-habit-graph-column t
)
344 (delete-char (min (+ 1 org-habit-preceding-days
345 org-habit-following-days
)
346 (- (line-end-position) (point))))
347 (insert (org-habit-build-graph
349 (time-subtract moment
350 (days-to-time org-habit-preceding-days
))
353 (days-to-time org-habit-following-days
))))))
356 (defun org-habit-toggle-habits ()
357 "Toggle display of habits in an agenda buffer."
359 (org-agenda-check-type t
'agenda
)
360 (setq org-habit-show-habits
(not org-habit-show-habits
))
362 (org-agenda-set-mode-name)
363 (message "Habits turned %s"
364 (if org-habit-show-habits
"on" "off")))
366 (org-defkey org-agenda-mode-map
"K" 'org-habit-toggle-habits
)
370 ;;; org-habit.el ends here