library-of-babel: adding vc-log by Luke Crook
[org-mode/org-jambu.git] / lisp / org-habit.el
blob13689cc5a936265184a0d4f5261ee2c05b49c0ad
1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw at gnu dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.5
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file contains the habit tracking code for Org-mode
30 ;;; Code:
32 (require 'org)
33 (require 'org-agenda)
35 (eval-when-compile
36 (require 'cl))
38 (defgroup org-habit nil
39 "Options concerning habit tracking in Org-mode."
40 :tag "Org Habit"
41 :group 'org-progress)
43 (defcustom org-habit-graph-column 40
44 "The absolute column at which to insert habit consistency graphs.
45 Note that consistency graphs will overwrite anything else in the buffer."
46 :group 'org-habit
47 :type 'integer)
49 (defcustom org-habit-preceding-days 21
50 "Number of days before today to appear in consistency graphs."
51 :group 'org-habit
52 :type 'integer)
54 (defcustom org-habit-following-days 7
55 "Number of days after today to appear in consistency graphs."
56 :group 'org-habit
57 :type 'integer)
59 (defcustom org-habit-show-habits t
60 "If non-nil, show habits in agenda buffers."
61 :group 'org-habit
62 :type 'boolean)
64 (defcustom org-habit-show-habits-only-for-today t
65 "If non-nil, only show habits on today's agenda, and not for future days.
66 Note that even when shown for future days, the graph is always
67 relative to the current effective date."
68 :group 'org-habit
69 :type 'boolean)
71 (defface org-habit-clear-face
72 '((((background light)) (:background "#8270f9"))
73 (((background dark)) (:background "blue")))
74 "Face for days on which a task shouldn't be done yet."
75 :group 'org-habit
76 :group 'org-faces)
77 (defface org-habit-clear-future-face
78 '((((background light)) (:background "#d6e4fc"))
79 (((background dark)) (:background "midnightblue")))
80 "Face for future days on which a task shouldn't be done yet."
81 :group 'org-habit
82 :group 'org-faces)
84 (defface org-habit-ready-face
85 '((((background light)) (:background "#4df946"))
86 (((background dark)) (:background "forestgreen")))
87 "Face for days on which a task should start to be done."
88 :group 'org-habit
89 :group 'org-faces)
90 (defface org-habit-ready-future-face
91 '((((background light)) (:background "#acfca9"))
92 (((background dark)) (:background "darkgreen")))
93 "Face for days on which a task should start to be done."
94 :group 'org-habit
95 :group 'org-faces)
97 (defface org-habit-alert-face
98 '((((background light)) (:background "#f5f946"))
99 (((background dark)) (:background "gold")))
100 "Face for days on which a task is due."
101 :group 'org-habit
102 :group 'org-faces)
103 (defface org-habit-alert-future-face
104 '((((background light)) (:background "#fafca9"))
105 (((background dark)) (:background "darkgoldenrod")))
106 "Face for days on which a task is due."
107 :group 'org-habit
108 :group 'org-faces)
110 (defface org-habit-overdue-face
111 '((((background light)) (:background "#f9372d"))
112 (((background dark)) (:background "firebrick")))
113 "Face for days on which a task is overdue."
114 :group 'org-habit
115 :group 'org-faces)
116 (defface org-habit-overdue-future-face
117 '((((background light)) (:background "#fc9590"))
118 (((background dark)) (:background "darkred")))
119 "Face for days on which a task is overdue."
120 :group 'org-habit
121 :group 'org-faces)
123 (defun org-habit-duration-to-days (ts)
124 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
125 ;; lead time is specified.
126 (floor (* (string-to-number (match-string 1 ts))
127 (cdr (assoc (match-string 2 ts)
128 '(("d" . 1) ("w" . 7)
129 ("m" . 30.4) ("y" . 365.25))))))
130 (error "Invalid duration string: %s" ts)))
132 (defun org-is-habit-p (&optional pom)
133 "Is the task at POM or point a habit?"
134 (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
136 (defun org-habit-parse-todo (&optional pom)
137 "Parse the TODO surrounding point for its habit-related data.
138 Returns a list with the following elements:
140 0: Scheduled date for the habit (may be in the past)
141 1: \".+\"-style repeater for the schedule, in days
142 2: Optional deadline (nil if not present)
143 3: If deadline, the repeater for the deadline, otherwise nil
144 4: A list of all the past dates this todo was mark closed
146 This list represents a \"habit\" for the rest of this module."
147 (save-excursion
148 (if pom (goto-char pom))
149 (assert (org-is-habit-p (point)))
150 (let* ((scheduled (org-get-scheduled-time (point)))
151 (scheduled-repeat (org-get-repeat org-scheduled-string))
152 (end (org-entry-end-position))
153 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
154 closed-dates deadline dr-days sr-days)
155 (if scheduled
156 (setq scheduled (time-to-days scheduled))
157 (error "Habit %s has no scheduled date" habit-entry))
158 (unless scheduled-repeat
159 (error
160 "Habit '%s' has no scheduled repeat period or has an incorrect one"
161 habit-entry))
162 (setq sr-days (org-habit-duration-to-days scheduled-repeat))
163 (unless (> sr-days 0)
164 (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
165 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
166 (setq dr-days (org-habit-duration-to-days
167 (match-string-no-properties 1 scheduled-repeat)))
168 (if (<= dr-days sr-days)
169 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
170 habit-entry scheduled-repeat))
171 (setq deadline (+ scheduled (- dr-days sr-days))))
172 (org-back-to-heading t)
173 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
174 (reversed org-log-states-order-reversed)
175 (search (if reversed 're-search-forward 're-search-backward))
176 (limit (if reversed end (point)))
177 (count 0))
178 (unless reversed (goto-char end))
179 (while (and (< count maxdays)
180 (funcall search "- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t))
181 (push (time-to-days
182 (org-time-string-to-time (match-string-no-properties 1)))
183 closed-dates)
184 (setq count (1+ count))))
185 (list scheduled sr-days deadline dr-days closed-dates))))
187 (defsubst org-habit-scheduled (habit)
188 (nth 0 habit))
189 (defsubst org-habit-scheduled-repeat (habit)
190 (nth 1 habit))
191 (defsubst org-habit-deadline (habit)
192 (let ((deadline (nth 2 habit)))
193 (or deadline
194 (if (nth 3 habit)
195 (+ (org-habit-scheduled habit)
196 (1- (org-habit-scheduled-repeat habit)))
197 (org-habit-scheduled habit)))))
198 (defsubst org-habit-deadline-repeat (habit)
199 (or (nth 3 habit)
200 (org-habit-scheduled-repeat habit)))
201 (defsubst org-habit-done-dates (habit)
202 (nth 4 habit))
204 (defsubst org-habit-get-priority (habit &optional moment)
205 "Determine the relative priority of a habit.
206 This must take into account not just urgency, but consistency as well."
207 (let ((pri 1000)
208 (now (if moment (time-to-days moment) (org-today)))
209 (scheduled (org-habit-scheduled habit))
210 (deadline (org-habit-deadline habit)))
211 ;; add 10 for every day past the scheduled date, and subtract for every
212 ;; day before it
213 (setq pri (+ pri (* (- now scheduled) 10)))
214 ;; add 50 if the deadline is today
215 (if (and (/= scheduled deadline)
216 (= now deadline))
217 (setq pri (+ pri 50)))
218 ;; add 100 for every day beyond the deadline date, and subtract 10 for
219 ;; every day before it
220 (let ((slip (- now (1- deadline))))
221 (if (> slip 0)
222 (setq pri (+ pri (* slip 100)))
223 (setq pri (+ pri (* slip 10)))))
224 pri))
226 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
227 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
228 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
229 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
231 Habits are assigned colors on the following basis:
232 Blue Task is before the scheduled date.
233 Green Task is on or after scheduled date, but before the
234 end of the schedule's repeat period.
235 Yellow If the task has a deadline, then it is after schedule's
236 repeat period, but before the deadline.
237 Orange The task has reached the deadline day, or if there is
238 no deadline, the end of the schedule's repeat period.
239 Red The task has gone beyond the deadline day or the
240 schedule's repeat period."
241 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
242 (s-repeat (org-habit-scheduled-repeat habit))
243 (scheduled-end (+ scheduled (1- s-repeat)))
244 (d-repeat (org-habit-deadline-repeat habit))
245 (deadline (if scheduled-days
246 (+ scheduled-days (- d-repeat s-repeat))
247 (org-habit-deadline habit)))
248 (m-days (or now-days (time-to-days (current-time)))))
249 (cond
250 ((< m-days scheduled)
251 '(org-habit-clear-face . org-habit-clear-future-face))
252 ((< m-days deadline)
253 '(org-habit-ready-face . org-habit-ready-future-face))
254 ((= m-days deadline)
255 (if donep
256 '(org-habit-ready-face . org-habit-ready-future-face)
257 '(org-habit-alert-face . org-habit-alert-future-face)))
259 '(org-habit-overdue-face . org-habit-overdue-future-face)))))
261 (defun org-habit-build-graph (habit starting current ending)
262 "Build a graph for the given HABIT, from STARTING to ENDING.
263 CURRENT gives the current time between STARTING and ENDING, for
264 the purpose of drawing the graph. It need not be the actual
265 current time."
266 (let* ((done-dates (sort (org-habit-done-dates habit) '<))
267 (scheduled (org-habit-scheduled habit))
268 (s-repeat (org-habit-scheduled-repeat habit))
269 (start (time-to-days starting))
270 (now (time-to-days current))
271 (end (time-to-days ending))
272 (graph (make-string (1+ (- end start)) ?\ ))
273 (index 0)
274 last-done-date)
275 (while (and done-dates (< (car done-dates) start))
276 (setq last-done-date (car done-dates)
277 done-dates (cdr done-dates)))
278 (while (< start end)
279 (let* ((in-the-past-p (< start now))
280 (todayp (= start now))
281 (donep (and done-dates
282 (= start (car done-dates))))
283 (faces (if (and in-the-past-p
284 (not last-done-date)
285 (not (< scheduled now)))
286 '(org-habit-clear-face . org-habit-clear-future-face)
287 (org-habit-get-faces
288 habit start (and in-the-past-p
289 (if last-done-date
290 (+ last-done-date s-repeat)
291 scheduled))
292 donep)))
293 markedp face)
294 (if donep
295 (let ((done-time (time-add
296 starting
297 (days-to-time
298 (- start (time-to-days starting))))))
300 (aset graph index ?*)
301 (setq markedp t)
302 (put-text-property
303 index (1+ index) 'help-echo
304 (format-time-string (org-time-stamp-format) done-time) graph)
305 (while (and done-dates
306 (= start (car done-dates)))
307 (setq last-done-date (car done-dates)
308 done-dates (cdr done-dates))))
309 (if todayp
310 (aset graph index ?!)))
311 (setq face (if (or in-the-past-p todayp)
312 (car faces)
313 (cdr faces)))
314 (if (and in-the-past-p
315 (not (eq face 'org-habit-overdue-face))
316 (not markedp))
317 (setq face (cdr faces)))
318 (put-text-property index (1+ index) 'face face graph))
319 (setq start (1+ start)
320 index (1+ index)))
321 graph))
323 (defun org-habit-insert-consistency-graphs (&optional line)
324 "Insert consistency graph for any habitual tasks."
325 (let ((inhibit-read-only t) l c
326 (buffer-invisibility-spec '(org-link))
327 (moment (time-subtract (current-time)
328 (list 0 (* 3600 org-extend-today-until) 0))))
329 (save-excursion
330 (goto-char (if line (point-at-bol) (point-min)))
331 (while (not (eobp))
332 (let ((habit (get-text-property (point) 'org-habit-p)))
333 (when habit
334 (move-to-column org-habit-graph-column t)
335 (delete-char (min (+ 1 org-habit-preceding-days
336 org-habit-following-days)
337 (- (line-end-position) (point))))
338 (insert (org-habit-build-graph
339 habit
340 (time-subtract moment
341 (days-to-time org-habit-preceding-days))
342 moment
343 (time-add moment
344 (days-to-time org-habit-following-days))))))
345 (forward-line)))))
347 (defun org-habit-toggle-habits ()
348 "Toggle display of habits in an agenda buffer."
349 (interactive)
350 (org-agenda-check-type t 'agenda)
351 (setq org-habit-show-habits (not org-habit-show-habits))
352 (org-agenda-redo)
353 (org-agenda-set-mode-name)
354 (message "Habits turned %s"
355 (if org-habit-show-habits "on" "off")))
357 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
359 (provide 'org-habit)
361 ;; arch-tag: 64e070d9-bd09-4917-bd44-44465f5ed348
363 ;;; org-habit.el ends here