Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-habit.el
blob394b4fb05db3c3eefe2ecc70430eda4c3d42968e
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.3
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 (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
174 (push (time-to-days
175 (org-time-string-to-time (match-string-no-properties 1)))
176 closed-dates))
177 (list scheduled sr-days deadline dr-days closed-dates))))
179 (defsubst org-habit-scheduled (habit)
180 (nth 0 habit))
181 (defsubst org-habit-scheduled-repeat (habit)
182 (nth 1 habit))
183 (defsubst org-habit-deadline (habit)
184 (let ((deadline (nth 2 habit)))
185 (or deadline
186 (if (nth 3 habit)
187 (+ (org-habit-scheduled habit)
188 (1- (org-habit-scheduled-repeat habit)))
189 (org-habit-scheduled habit)))))
190 (defsubst org-habit-deadline-repeat (habit)
191 (or (nth 3 habit)
192 (org-habit-scheduled-repeat habit)))
193 (defsubst org-habit-done-dates (habit)
194 (nth 4 habit))
196 (defsubst org-habit-get-priority (habit &optional moment)
197 "Determine the relative priority of a habit.
198 This must take into account not just urgency, but consistency as well."
199 (let ((pri 1000)
200 (now (time-to-days
201 (or moment
202 (time-subtract (current-time)
203 (list 0 (* 3600 org-extend-today-until) 0)))))
204 (scheduled (org-habit-scheduled habit))
205 (deadline (org-habit-deadline habit)))
206 ;; add 10 for every day past the scheduled date, and subtract for every
207 ;; day before it
208 (setq pri (+ pri (* (- now scheduled) 10)))
209 ;; add 50 if the deadline is today
210 (if (and (/= scheduled deadline)
211 (= now deadline))
212 (setq pri (+ pri 50)))
213 ;; add 100 for every day beyond the deadline date, and subtract 10 for
214 ;; every day before it
215 (let ((slip (- now (1- deadline))))
216 (if (> slip 0)
217 (setq pri (+ pri (* slip 100)))
218 (setq pri (+ pri (* slip 10)))))
219 pri))
221 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
222 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
223 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
224 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
226 Habits are assigned colors on the following basis:
227 Blue Task is before the scheduled date.
228 Green Task is on or after scheduled date, but before the
229 end of the schedule's repeat period.
230 Yellow If the task has a deadline, then it is after schedule's
231 repeat period, but before the deadline.
232 Orange The task has reached the deadline day, or if there is
233 no deadline, the end of the schedule's repeat period.
234 Red The task has gone beyond the deadline day or the
235 schedule's repeat period."
236 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
237 (s-repeat (org-habit-scheduled-repeat habit))
238 (scheduled-end (+ scheduled (1- s-repeat)))
239 (d-repeat (org-habit-deadline-repeat habit))
240 (deadline (if scheduled-days
241 (+ scheduled-days (- d-repeat s-repeat))
242 (org-habit-deadline habit)))
243 (m-days (or now-days (time-to-days (current-time)))))
244 (cond
245 ((< m-days scheduled)
246 '(org-habit-clear-face . org-habit-clear-future-face))
247 ((< m-days deadline)
248 '(org-habit-ready-face . org-habit-ready-future-face))
249 ((= m-days deadline)
250 (if donep
251 '(org-habit-ready-face . org-habit-ready-future-face)
252 '(org-habit-alert-face . org-habit-alert-future-face)))
254 '(org-habit-overdue-face . org-habit-overdue-future-face)))))
256 (defun org-habit-build-graph (habit starting current ending)
257 "Build a graph for the given HABIT, from STARTING to ENDING.
258 CURRENT gives the current time between STARTING and ENDING, for
259 the purpose of drawing the graph. It need not be the actual
260 current time."
261 (let* ((done-dates (sort (org-habit-done-dates habit) '<))
262 (scheduled (org-habit-scheduled habit))
263 (s-repeat (org-habit-scheduled-repeat habit))
264 (start (time-to-days starting))
265 (now (time-to-days current))
266 (end (time-to-days ending))
267 (graph (make-string (1+ (- end start)) ?\ ))
268 (index 0)
269 last-done-date)
270 (while (and done-dates (< (car done-dates) start))
271 (setq last-done-date (car done-dates)
272 done-dates (cdr done-dates)))
273 (while (< start end)
274 (let* ((in-the-past-p (< start now))
275 (todayp (= start now))
276 (donep (and done-dates
277 (= start (car done-dates))))
278 (faces (if (and in-the-past-p
279 (not last-done-date)
280 (not (< scheduled now)))
281 '(org-habit-clear-face . org-habit-clear-future-face)
282 (org-habit-get-faces
283 habit start (and in-the-past-p
284 (if last-done-date
285 (+ last-done-date s-repeat)
286 scheduled))
287 donep)))
288 markedp face)
289 (if donep
290 (let ((done-time (time-add
291 starting
292 (days-to-time
293 (- start (time-to-days starting))))))
295 (aset graph index ?*)
296 (setq markedp t)
297 (put-text-property
298 index (1+ index) 'help-echo
299 (format-time-string (org-time-stamp-format) done-time) graph)
300 (while (and done-dates
301 (= start (car done-dates)))
302 (setq last-done-date (car done-dates)
303 done-dates (cdr done-dates))))
304 (if todayp
305 (aset graph index ?!)))
306 (setq face (if (or in-the-past-p todayp)
307 (car faces)
308 (cdr faces)))
309 (if (and in-the-past-p
310 (not (eq face 'org-habit-overdue-face))
311 (not markedp))
312 (setq face (cdr faces)))
313 (put-text-property index (1+ index) 'face face graph))
314 (setq start (1+ start)
315 index (1+ index)))
316 graph))
318 (defun org-habit-insert-consistency-graphs (&optional line)
319 "Insert consistency graph for any habitual tasks."
320 (let ((inhibit-read-only t) l c
321 (buffer-invisibility-spec '(org-link))
322 (moment (time-subtract (current-time)
323 (list 0 (* 3600 org-extend-today-until) 0))))
324 (save-excursion
325 (goto-char (if line (point-at-bol) (point-min)))
326 (while (not (eobp))
327 (let ((habit (get-text-property (point) 'org-habit-p)))
328 (when habit
329 (move-to-column org-habit-graph-column t)
330 (delete-char (min (+ 1 org-habit-preceding-days
331 org-habit-following-days)
332 (- (line-end-position) (point))))
333 (insert (org-habit-build-graph
334 habit
335 (time-subtract moment
336 (days-to-time org-habit-preceding-days))
337 moment
338 (time-add moment
339 (days-to-time org-habit-following-days))))))
340 (forward-line)))))
342 (defun org-habit-toggle-habits ()
343 "Toggle display of habits in an agenda buffer."
344 (interactive)
345 (org-agenda-check-type t 'agenda)
346 (setq org-habit-show-habits (not org-habit-show-habits))
347 (org-agenda-redo)
348 (org-agenda-set-mode-name)
349 (message "Habits turned %s"
350 (if org-habit-show-habits "on" "off")))
352 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
354 (provide 'org-habit)
356 ;; arch-tag: 64e070d9-bd09-4917-bd44-44465f5ed348
358 ;;; org-habit.el ends here