1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009-2013 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-show-all-today nil
71 "If non-nil, will show the consistency graph of all habits on
72 today's agenda, even if they are not scheduled."
76 (defcustom org-habit-today-glyph ?
!
77 "Glyph character used to identify today."
82 (defcustom org-habit-completed-glyph ?
*
83 "Glyph character used to show completed days on which a task was done."
88 (defcustom org-habit-show-done-always-green nil
89 "Non-nil means DONE days will always be green in the consistency graph.
90 It will be green even if it was done after the deadline."
94 (defface org-habit-clear-face
95 '((((background light
)) (:background
"#8270f9"))
96 (((background dark
)) (:background
"blue")))
97 "Face for days on which a task shouldn't be done yet."
100 (defface org-habit-clear-future-face
101 '((((background light
)) (:background
"#d6e4fc"))
102 (((background dark
)) (:background
"midnightblue")))
103 "Face for future days on which a task shouldn't be done yet."
107 (defface org-habit-ready-face
108 '((((background light
)) (:background
"#4df946"))
109 (((background dark
)) (:background
"forestgreen")))
110 "Face for days on which a task should start to be done."
113 (defface org-habit-ready-future-face
114 '((((background light
)) (:background
"#acfca9"))
115 (((background dark
)) (:background
"darkgreen")))
116 "Face for days on which a task should start to be done."
120 (defface org-habit-alert-face
121 '((((background light
)) (:background
"#f5f946"))
122 (((background dark
)) (:background
"gold")))
123 "Face for days on which a task is due."
126 (defface org-habit-alert-future-face
127 '((((background light
)) (:background
"#fafca9"))
128 (((background dark
)) (:background
"darkgoldenrod")))
129 "Face for days on which a task is due."
133 (defface org-habit-overdue-face
134 '((((background light
)) (:background
"#f9372d"))
135 (((background dark
)) (:background
"firebrick")))
136 "Face for days on which a task is overdue."
139 (defface org-habit-overdue-future-face
140 '((((background light
)) (:background
"#fc9590"))
141 (((background dark
)) (:background
"darkred")))
142 "Face for days on which a task is overdue."
146 (defun org-habit-duration-to-days (ts)
147 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts
)
148 ;; lead time is specified.
149 (floor (* (string-to-number (match-string 1 ts
))
150 (cdr (assoc (match-string 2 ts
)
151 '(("d" .
1) ("w" .
7)
152 ("m" .
30.4) ("y" .
365.25))))))
153 (error "Invalid duration string: %s" ts
)))
155 (defun org-is-habit-p (&optional pom
)
156 "Is the task at POM or point a habit?"
157 (string= "habit" (org-entry-get (or pom
(point)) "STYLE")))
159 (defun org-habit-parse-todo (&optional pom
)
160 "Parse the TODO surrounding point for its habit-related data.
161 Returns a list with the following elements:
163 0: Scheduled date for the habit (may be in the past)
164 1: \".+\"-style repeater for the schedule, in days
165 2: Optional deadline (nil if not present)
166 3: If deadline, the repeater for the deadline, otherwise nil
167 4: A list of all the past dates this todo was mark closed
169 This list represents a \"habit\" for the rest of this module."
171 (if pom
(goto-char pom
))
172 (assert (org-is-habit-p (point)))
173 (let* ((scheduled (org-get-scheduled-time (point)))
174 (scheduled-repeat (org-get-repeat org-scheduled-string
))
175 (end (org-entry-end-position))
176 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
177 closed-dates deadline dr-days sr-days
)
179 (setq scheduled
(time-to-days scheduled
))
180 (error "Habit %s has no scheduled date" habit-entry
))
181 (unless scheduled-repeat
183 "Habit '%s' has no scheduled repeat period or has an incorrect one"
185 (setq sr-days
(org-habit-duration-to-days scheduled-repeat
))
186 (unless (> sr-days
0)
187 (error "Habit %s scheduled repeat period is less than 1d" habit-entry
))
188 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat
)
189 (setq dr-days
(org-habit-duration-to-days
190 (match-string-no-properties 1 scheduled-repeat
)))
191 (if (<= dr-days sr-days
)
192 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
193 habit-entry scheduled-repeat
))
194 (setq deadline
(+ scheduled
(- dr-days sr-days
))))
195 (org-back-to-heading t
)
196 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days
))
197 (reversed org-log-states-order-reversed
)
198 (search (if reversed
're-search-forward
're-search-backward
))
199 (limit (if reversed end
(point)))
201 (unless reversed
(goto-char end
))
202 (while (and (< count maxdays
)
203 (funcall search
"- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t
))
205 (org-time-string-to-time (match-string-no-properties 1)))
207 (setq count
(1+ count
))))
208 (list scheduled sr-days deadline dr-days closed-dates
))))
210 (defsubst org-habit-scheduled
(habit)
212 (defsubst org-habit-scheduled-repeat
(habit)
214 (defsubst org-habit-deadline
(habit)
215 (let ((deadline (nth 2 habit
)))
218 (+ (org-habit-scheduled habit
)
219 (1- (org-habit-scheduled-repeat habit
)))
220 (org-habit-scheduled habit
)))))
221 (defsubst org-habit-deadline-repeat
(habit)
223 (org-habit-scheduled-repeat habit
)))
224 (defsubst org-habit-done-dates
(habit)
227 (defsubst org-habit-get-priority
(habit &optional moment
)
228 "Determine the relative priority of a habit.
229 This must take into account not just urgency, but consistency as well."
231 (now (if moment
(time-to-days moment
) (org-today)))
232 (scheduled (org-habit-scheduled habit
))
233 (deadline (org-habit-deadline habit
)))
234 ;; add 10 for every day past the scheduled date, and subtract for every
236 (setq pri
(+ pri
(* (- now scheduled
) 10)))
237 ;; add 50 if the deadline is today
238 (if (and (/= scheduled deadline
)
240 (setq pri
(+ pri
50)))
241 ;; add 100 for every day beyond the deadline date, and subtract 10 for
242 ;; every day before it
243 (let ((slip (- now
(1- deadline
))))
245 (setq pri
(+ pri
(* slip
100)))
246 (setq pri
(+ pri
(* slip
10)))))
249 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep
)
250 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
251 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
252 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
254 Habits are assigned colors on the following basis:
255 Blue Task is before the scheduled date.
256 Green Task is on or after scheduled date, but before the
257 end of the schedule's repeat period.
258 Yellow If the task has a deadline, then it is after schedule's
259 repeat period, but before the deadline.
260 Orange The task has reached the deadline day, or if there is
261 no deadline, the end of the schedule's repeat period.
262 Red The task has gone beyond the deadline day or the
263 schedule's repeat period."
264 (let* ((scheduled (or scheduled-days
(org-habit-scheduled habit
)))
265 (s-repeat (org-habit-scheduled-repeat habit
))
266 (scheduled-end (+ scheduled
(1- s-repeat
)))
267 (d-repeat (org-habit-deadline-repeat habit
))
268 (deadline (if scheduled-days
269 (+ scheduled-days
(- d-repeat s-repeat
))
270 (org-habit-deadline habit
)))
271 (m-days (or now-days
(time-to-days (current-time)))))
273 ((< m-days scheduled
)
274 '(org-habit-clear-face . org-habit-clear-future-face
))
276 '(org-habit-ready-face . org-habit-ready-future-face
))
279 '(org-habit-ready-face . org-habit-ready-future-face
)
280 '(org-habit-alert-face . org-habit-alert-future-face
)))
281 ((and org-habit-show-done-always-green donep
)
282 '(org-habit-ready-face . org-habit-ready-future-face
))
283 (t '(org-habit-overdue-face . org-habit-overdue-future-face
)))))
285 (defun org-habit-build-graph (habit starting current ending
)
286 "Build a graph for the given HABIT, from STARTING to ENDING.
287 CURRENT gives the current time between STARTING and ENDING, for
288 the purpose of drawing the graph. It need not be the actual
290 (let* ((done-dates (sort (org-habit-done-dates habit
) '<))
291 (scheduled (org-habit-scheduled habit
))
292 (s-repeat (org-habit-scheduled-repeat habit
))
293 (start (time-to-days starting
))
294 (now (time-to-days current
))
295 (end (time-to-days ending
))
296 (graph (make-string (1+ (- end start
)) ?\
))
299 (while (and done-dates
(< (car done-dates
) start
))
300 (setq last-done-date
(car done-dates
)
301 done-dates
(cdr done-dates
)))
303 (let* ((in-the-past-p (< start now
))
304 (todayp (= start now
))
305 (donep (and done-dates
306 (= start
(car done-dates
))))
307 (faces (if (and in-the-past-p
309 (not (< scheduled now
)))
310 '(org-habit-clear-face . org-habit-clear-future-face
)
312 habit start
(and in-the-past-p
314 (+ last-done-date s-repeat
)
319 (let ((done-time (time-add
322 (- start
(time-to-days starting
))))))
324 (aset graph index org-habit-completed-glyph
)
327 index
(1+ index
) 'help-echo
328 (format-time-string (org-time-stamp-format) done-time
) graph
)
329 (while (and done-dates
330 (= start
(car done-dates
)))
331 (setq last-done-date
(car done-dates
)
332 done-dates
(cdr done-dates
))))
334 (aset graph index org-habit-today-glyph
)))
335 (setq face
(if (or in-the-past-p todayp
)
338 (if (and in-the-past-p
339 (not (eq face
'org-habit-overdue-face
))
341 (setq face
(cdr faces
)))
342 (put-text-property index
(1+ index
) 'face face graph
))
343 (setq start
(1+ start
)
347 (defun org-habit-insert-consistency-graphs (&optional line
)
348 "Insert consistency graph for any habitual tasks."
349 (let ((inhibit-read-only t
) l c
350 (buffer-invisibility-spec '(org-link))
351 (moment (time-subtract (current-time)
352 (list 0 (* 3600 org-extend-today-until
) 0)))
354 ;; Disable filters; this helps with alignment if there are links.
356 (when (overlay-get ol
'invisible
)
357 (overlay-put ol
'invisible nil
)
358 (setq disabled-overlays
(cons ol disabled-overlays
))))
359 (overlays-in (point-min) (point-max)))
361 (goto-char (if line
(point-at-bol) (point-min)))
363 (let ((habit (get-text-property (point) 'org-habit-p
)))
365 (move-to-column org-habit-graph-column t
)
366 (delete-char (min (+ 1 org-habit-preceding-days
367 org-habit-following-days
)
368 (- (line-end-position) (point))))
369 (insert-before-markers
370 (org-habit-build-graph
372 (time-subtract moment
(days-to-time org-habit-preceding-days
))
374 (time-add moment
(days-to-time org-habit-following-days
))))))
376 (mapc (lambda (ol) (overlay-put ol
'invisible t
))
379 (defun org-habit-toggle-habits ()
380 "Toggle display of habits in an agenda buffer."
382 (org-agenda-check-type t
'agenda
)
383 (setq org-habit-show-habits
(not org-habit-show-habits
))
385 (org-agenda-set-mode-name)
386 (message "Habits turned %s"
387 (if org-habit-show-habits
"on" "off")))
389 (org-defkey org-agenda-mode-map
"K" 'org-habit-toggle-habits
)
393 ;;; org-habit.el ends here