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 (defface org-habit-clear-face
71 '((((background light
)) (:background
"#8270f9"))
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
"#d6e4fc"))
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
"#4df946"))
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
"#acfca9"))
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
"#f5f946"))
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
"#fafca9"))
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
"#f9372d"))
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
"#fc9590"))
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 "Is the task at POM or point a habit?"
133 (string= "habit" (org-entry-get (or pom
(point)) "STYLE")))
135 (defun org-habit-parse-todo (&optional pom
)
136 "Parse the TODO surrounding point for its habit-related data.
137 Returns a list with the following elements:
139 0: Scheduled date for the habit (may be in the past)
140 1: \".+\"-style repeater for the schedule, in days
141 2: Optional deadline (nil if not present)
142 3: If deadline, the repeater for the deadline, otherwise nil
143 4: A list of all the past dates this todo was mark closed
145 This list represents a \"habit\" for the rest of this module."
147 (if pom
(goto-char pom
))
148 (assert (org-is-habit-p (point)))
149 (let* ((scheduled (org-get-scheduled-time (point)))
150 (scheduled-repeat (org-get-repeat org-scheduled-string
))
151 (end (org-entry-end-position))
152 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
153 closed-dates deadline dr-days sr-days
)
155 (setq scheduled
(time-to-days scheduled
))
156 (error "Habit %s has no scheduled date" habit-entry
))
157 (unless scheduled-repeat
159 "Habit '%s' has no scheduled repeat period or has an incorrect one"
161 (setq sr-days
(org-habit-duration-to-days scheduled-repeat
))
162 (unless (> sr-days
0)
163 (error "Habit %s scheduled repeat period is less than 1d" habit-entry
))
164 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat
)
165 (setq dr-days
(org-habit-duration-to-days
166 (match-string-no-properties 1 scheduled-repeat
)))
167 (if (<= dr-days sr-days
)
168 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
169 habit-entry scheduled-repeat
))
170 (setq deadline
(+ scheduled
(- dr-days sr-days
))))
171 (org-back-to-heading t
)
172 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days
))
173 (reversed org-log-states-order-reversed
)
174 (search (if reversed
're-search-forward
're-search-backward
))
175 (limit (if reversed end
(point)))
177 (unless reversed
(goto-char end
))
178 (while (and (< count maxdays
)
179 (funcall search
"- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t
))
181 (org-time-string-to-time (match-string-no-properties 1)))
183 (setq count
(1+ count
))))
184 (list scheduled sr-days deadline dr-days closed-dates
))))
186 (defsubst org-habit-scheduled
(habit)
188 (defsubst org-habit-scheduled-repeat
(habit)
190 (defsubst org-habit-deadline
(habit)
191 (let ((deadline (nth 2 habit
)))
194 (+ (org-habit-scheduled habit
)
195 (1- (org-habit-scheduled-repeat habit
)))
196 (org-habit-scheduled habit
)))))
197 (defsubst org-habit-deadline-repeat
(habit)
199 (org-habit-scheduled-repeat habit
)))
200 (defsubst org-habit-done-dates
(habit)
203 (defsubst org-habit-get-priority
(habit &optional moment
)
204 "Determine the relative priority of a habit.
205 This must take into account not just urgency, but consistency as well."
207 (now (if moment
(time-to-days moment
) (org-today)))
208 (scheduled (org-habit-scheduled habit
))
209 (deadline (org-habit-deadline habit
)))
210 ;; add 10 for every day past the scheduled date, and subtract for every
212 (setq pri
(+ pri
(* (- now scheduled
) 10)))
213 ;; add 50 if the deadline is today
214 (if (and (/= scheduled deadline
)
216 (setq pri
(+ pri
50)))
217 ;; add 100 for every day beyond the deadline date, and subtract 10 for
218 ;; every day before it
219 (let ((slip (- now
(1- deadline
))))
221 (setq pri
(+ pri
(* slip
100)))
222 (setq pri
(+ pri
(* slip
10)))))
225 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep
)
226 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
227 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
228 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
230 Habits are assigned colors on the following basis:
231 Blue Task is before the scheduled date.
232 Green Task is on or after scheduled date, but before the
233 end of the schedule's repeat period.
234 Yellow If the task has a deadline, then it is after schedule's
235 repeat period, but before the deadline.
236 Orange The task has reached the deadline day, or if there is
237 no deadline, the end of the schedule's repeat period.
238 Red The task has gone beyond the deadline day or the
239 schedule's repeat period."
240 (let* ((scheduled (or scheduled-days
(org-habit-scheduled habit
)))
241 (s-repeat (org-habit-scheduled-repeat habit
))
242 (scheduled-end (+ scheduled
(1- s-repeat
)))
243 (d-repeat (org-habit-deadline-repeat habit
))
244 (deadline (if scheduled-days
245 (+ scheduled-days
(- d-repeat s-repeat
))
246 (org-habit-deadline habit
)))
247 (m-days (or now-days
(time-to-days (current-time)))))
249 ((< m-days scheduled
)
250 '(org-habit-clear-face . org-habit-clear-future-face
))
252 '(org-habit-ready-face . org-habit-ready-future-face
))
255 '(org-habit-ready-face . org-habit-ready-future-face
)
256 '(org-habit-alert-face . org-habit-alert-future-face
)))
258 '(org-habit-overdue-face . org-habit-overdue-future-face
)))))
260 (defun org-habit-build-graph (habit starting current ending
)
261 "Build a graph for the given HABIT, from STARTING to ENDING.
262 CURRENT gives the current time between STARTING and ENDING, for
263 the purpose of drawing the graph. It need not be the actual
265 (let* ((done-dates (sort (org-habit-done-dates habit
) '<))
266 (scheduled (org-habit-scheduled habit
))
267 (s-repeat (org-habit-scheduled-repeat habit
))
268 (start (time-to-days starting
))
269 (now (time-to-days current
))
270 (end (time-to-days ending
))
271 (graph (make-string (1+ (- end start
)) ?\
))
274 (while (and done-dates
(< (car done-dates
) start
))
275 (setq last-done-date
(car done-dates
)
276 done-dates
(cdr done-dates
)))
278 (let* ((in-the-past-p (< start now
))
279 (todayp (= start now
))
280 (donep (and done-dates
281 (= start
(car done-dates
))))
282 (faces (if (and in-the-past-p
284 (not (< scheduled now
)))
285 '(org-habit-clear-face . org-habit-clear-future-face
)
287 habit start
(and in-the-past-p
289 (+ last-done-date s-repeat
)
294 (let ((done-time (time-add
297 (- start
(time-to-days starting
))))))
299 (aset graph index ?
*)
302 index
(1+ index
) 'help-echo
303 (format-time-string (org-time-stamp-format) done-time
) graph
)
304 (while (and done-dates
305 (= start
(car done-dates
)))
306 (setq last-done-date
(car done-dates
)
307 done-dates
(cdr done-dates
))))
309 (aset graph index ?
!)))
310 (setq face
(if (or in-the-past-p todayp
)
313 (if (and in-the-past-p
314 (not (eq face
'org-habit-overdue-face
))
316 (setq face
(cdr faces
)))
317 (put-text-property index
(1+ index
) 'face face graph
))
318 (setq start
(1+ start
)
322 (defun org-habit-insert-consistency-graphs (&optional line
)
323 "Insert consistency graph for any habitual tasks."
324 (let ((inhibit-read-only t
) l c
325 (buffer-invisibility-spec '(org-link))
326 (moment (time-subtract (current-time)
327 (list 0 (* 3600 org-extend-today-until
) 0))))
329 (goto-char (if line
(point-at-bol) (point-min)))
331 (let ((habit (get-text-property (point) 'org-habit-p
)))
333 (move-to-column org-habit-graph-column t
)
334 (delete-char (min (+ 1 org-habit-preceding-days
335 org-habit-following-days
)
336 (- (line-end-position) (point))))
337 (insert (org-habit-build-graph
339 (time-subtract moment
340 (days-to-time org-habit-preceding-days
))
343 (days-to-time org-habit-following-days
))))))
346 (defun org-habit-toggle-habits ()
347 "Toggle display of habits in an agenda buffer."
349 (org-agenda-check-type t
'agenda
)
350 (setq org-habit-show-habits
(not org-habit-show-habits
))
352 (org-agenda-set-mode-name)
353 (message "Habits turned %s"
354 (if org-habit-show-habits
"on" "off")))
356 (org-defkey org-agenda-mode-map
"K" 'org-habit-toggle-habits
)
360 ;;; org-habit.el ends here