Merge branch 'origin-hotfix-7.8.06'
[org-mode.git] / lisp / org-habit.el
blob6b4776662e26ed14e6910498e2054f1977780559
1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009-2012 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 ;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file contains the habit tracking code for Org-mode
29 ;;; Code:
31 (require 'org)
32 (require 'org-agenda)
34 (eval-when-compile
35 (require 'cl))
37 (defgroup org-habit nil
38 "Options concerning habit tracking in Org-mode."
39 :tag "Org Habit"
40 :group 'org-progress)
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."
45 :group 'org-habit
46 :type 'integer)
48 (defcustom org-habit-preceding-days 21
49 "Number of days before today to appear in consistency graphs."
50 :group 'org-habit
51 :type 'integer)
53 (defcustom org-habit-following-days 7
54 "Number of days after today to appear in consistency graphs."
55 :group 'org-habit
56 :type 'integer)
58 (defcustom org-habit-show-habits t
59 "If non-nil, show habits in agenda buffers."
60 :group 'org-habit
61 :type 'boolean)
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."
67 :group 'org-habit
68 :type 'boolean)
70 (defcustom org-habit-today-glyph ?!
71 "Glyph character used to identify today."
72 :group 'org-habit
73 :version "24.1"
74 :type 'character)
76 (defcustom org-habit-completed-glyph ?*
77 "Glyph character used to show completed days on which a task was done."
78 :group 'org-habit
79 :version "24.1"
80 :type 'character)
82 (defface org-habit-clear-face
83 '((((background light)) (:background "#8270f9"))
84 (((background dark)) (:background "blue")))
85 "Face for days on which a task shouldn't be done yet."
86 :group 'org-habit
87 :group 'org-faces)
88 (defface org-habit-clear-future-face
89 '((((background light)) (:background "#d6e4fc"))
90 (((background dark)) (:background "midnightblue")))
91 "Face for future days on which a task shouldn't be done yet."
92 :group 'org-habit
93 :group 'org-faces)
95 (defface org-habit-ready-face
96 '((((background light)) (:background "#4df946"))
97 (((background dark)) (:background "forestgreen")))
98 "Face for days on which a task should start to be done."
99 :group 'org-habit
100 :group 'org-faces)
101 (defface org-habit-ready-future-face
102 '((((background light)) (:background "#acfca9"))
103 (((background dark)) (:background "darkgreen")))
104 "Face for days on which a task should start to be done."
105 :group 'org-habit
106 :group 'org-faces)
108 (defface org-habit-alert-face
109 '((((background light)) (:background "#f5f946"))
110 (((background dark)) (:background "gold")))
111 "Face for days on which a task is due."
112 :group 'org-habit
113 :group 'org-faces)
114 (defface org-habit-alert-future-face
115 '((((background light)) (:background "#fafca9"))
116 (((background dark)) (:background "darkgoldenrod")))
117 "Face for days on which a task is due."
118 :group 'org-habit
119 :group 'org-faces)
121 (defface org-habit-overdue-face
122 '((((background light)) (:background "#f9372d"))
123 (((background dark)) (:background "firebrick")))
124 "Face for days on which a task is overdue."
125 :group 'org-habit
126 :group 'org-faces)
127 (defface org-habit-overdue-future-face
128 '((((background light)) (:background "#fc9590"))
129 (((background dark)) (:background "darkred")))
130 "Face for days on which a task is overdue."
131 :group 'org-habit
132 :group 'org-faces)
134 (defun org-habit-duration-to-days (ts)
135 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
136 ;; lead time is specified.
137 (floor (* (string-to-number (match-string 1 ts))
138 (cdr (assoc (match-string 2 ts)
139 '(("d" . 1) ("w" . 7)
140 ("m" . 30.4) ("y" . 365.25))))))
141 (error "Invalid duration string: %s" ts)))
143 (defun org-is-habit-p (&optional pom)
144 "Is the task at POM or point a habit?"
145 (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
147 (defun org-habit-parse-todo (&optional pom)
148 "Parse the TODO surrounding point for its habit-related data.
149 Returns a list with the following elements:
151 0: Scheduled date for the habit (may be in the past)
152 1: \".+\"-style repeater for the schedule, in days
153 2: Optional deadline (nil if not present)
154 3: If deadline, the repeater for the deadline, otherwise nil
155 4: A list of all the past dates this todo was mark closed
157 This list represents a \"habit\" for the rest of this module."
158 (save-excursion
159 (if pom (goto-char pom))
160 (assert (org-is-habit-p (point)))
161 (let* ((scheduled (org-get-scheduled-time (point)))
162 (scheduled-repeat (org-get-repeat org-scheduled-string))
163 (end (org-entry-end-position))
164 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
165 closed-dates deadline dr-days sr-days)
166 (if scheduled
167 (setq scheduled (time-to-days scheduled))
168 (error "Habit %s has no scheduled date" habit-entry))
169 (unless scheduled-repeat
170 (error
171 "Habit '%s' has no scheduled repeat period or has an incorrect one"
172 habit-entry))
173 (setq sr-days (org-habit-duration-to-days scheduled-repeat))
174 (unless (> sr-days 0)
175 (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
176 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
177 (setq dr-days (org-habit-duration-to-days
178 (match-string-no-properties 1 scheduled-repeat)))
179 (if (<= dr-days sr-days)
180 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
181 habit-entry scheduled-repeat))
182 (setq deadline (+ scheduled (- dr-days sr-days))))
183 (org-back-to-heading t)
184 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
185 (reversed org-log-states-order-reversed)
186 (search (if reversed 're-search-forward 're-search-backward))
187 (limit (if reversed end (point)))
188 (count 0))
189 (unless reversed (goto-char end))
190 (while (and (< count maxdays)
191 (funcall search "- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t))
192 (push (time-to-days
193 (org-time-string-to-time (match-string-no-properties 1)))
194 closed-dates)
195 (setq count (1+ count))))
196 (list scheduled sr-days deadline dr-days closed-dates))))
198 (defsubst org-habit-scheduled (habit)
199 (nth 0 habit))
200 (defsubst org-habit-scheduled-repeat (habit)
201 (nth 1 habit))
202 (defsubst org-habit-deadline (habit)
203 (let ((deadline (nth 2 habit)))
204 (or deadline
205 (if (nth 3 habit)
206 (+ (org-habit-scheduled habit)
207 (1- (org-habit-scheduled-repeat habit)))
208 (org-habit-scheduled habit)))))
209 (defsubst org-habit-deadline-repeat (habit)
210 (or (nth 3 habit)
211 (org-habit-scheduled-repeat habit)))
212 (defsubst org-habit-done-dates (habit)
213 (nth 4 habit))
215 (defsubst org-habit-get-priority (habit &optional moment)
216 "Determine the relative priority of a habit.
217 This must take into account not just urgency, but consistency as well."
218 (let ((pri 1000)
219 (now (if moment (time-to-days moment) (org-today)))
220 (scheduled (org-habit-scheduled habit))
221 (deadline (org-habit-deadline habit)))
222 ;; add 10 for every day past the scheduled date, and subtract for every
223 ;; day before it
224 (setq pri (+ pri (* (- now scheduled) 10)))
225 ;; add 50 if the deadline is today
226 (if (and (/= scheduled deadline)
227 (= now deadline))
228 (setq pri (+ pri 50)))
229 ;; add 100 for every day beyond the deadline date, and subtract 10 for
230 ;; every day before it
231 (let ((slip (- now (1- deadline))))
232 (if (> slip 0)
233 (setq pri (+ pri (* slip 100)))
234 (setq pri (+ pri (* slip 10)))))
235 pri))
237 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
238 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
239 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
240 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
242 Habits are assigned colors on the following basis:
243 Blue Task is before the scheduled date.
244 Green Task is on or after scheduled date, but before the
245 end of the schedule's repeat period.
246 Yellow If the task has a deadline, then it is after schedule's
247 repeat period, but before the deadline.
248 Orange The task has reached the deadline day, or if there is
249 no deadline, the end of the schedule's repeat period.
250 Red The task has gone beyond the deadline day or the
251 schedule's repeat period."
252 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
253 (s-repeat (org-habit-scheduled-repeat habit))
254 (scheduled-end (+ scheduled (1- s-repeat)))
255 (d-repeat (org-habit-deadline-repeat habit))
256 (deadline (if scheduled-days
257 (+ scheduled-days (- d-repeat s-repeat))
258 (org-habit-deadline habit)))
259 (m-days (or now-days (time-to-days (current-time)))))
260 (cond
261 ((< m-days scheduled)
262 '(org-habit-clear-face . org-habit-clear-future-face))
263 ((< m-days deadline)
264 '(org-habit-ready-face . org-habit-ready-future-face))
265 ((= m-days deadline)
266 (if donep
267 '(org-habit-ready-face . org-habit-ready-future-face)
268 '(org-habit-alert-face . org-habit-alert-future-face)))
270 '(org-habit-overdue-face . org-habit-overdue-future-face)))))
272 (defun org-habit-build-graph (habit starting current ending)
273 "Build a graph for the given HABIT, from STARTING to ENDING.
274 CURRENT gives the current time between STARTING and ENDING, for
275 the purpose of drawing the graph. It need not be the actual
276 current time."
277 (let* ((done-dates (sort (org-habit-done-dates habit) '<))
278 (scheduled (org-habit-scheduled habit))
279 (s-repeat (org-habit-scheduled-repeat habit))
280 (start (time-to-days starting))
281 (now (time-to-days current))
282 (end (time-to-days ending))
283 (graph (make-string (1+ (- end start)) ?\ ))
284 (index 0)
285 last-done-date)
286 (while (and done-dates (< (car done-dates) start))
287 (setq last-done-date (car done-dates)
288 done-dates (cdr done-dates)))
289 (while (< start end)
290 (let* ((in-the-past-p (< start now))
291 (todayp (= start now))
292 (donep (and done-dates
293 (= start (car done-dates))))
294 (faces (if (and in-the-past-p
295 (not last-done-date)
296 (not (< scheduled now)))
297 '(org-habit-clear-face . org-habit-clear-future-face)
298 (org-habit-get-faces
299 habit start (and in-the-past-p
300 (if last-done-date
301 (+ last-done-date s-repeat)
302 scheduled))
303 donep)))
304 markedp face)
305 (if donep
306 (let ((done-time (time-add
307 starting
308 (days-to-time
309 (- start (time-to-days starting))))))
311 (aset graph index org-habit-completed-glyph)
312 (setq markedp t)
313 (put-text-property
314 index (1+ index) 'help-echo
315 (format-time-string (org-time-stamp-format) done-time) graph)
316 (while (and done-dates
317 (= start (car done-dates)))
318 (setq last-done-date (car done-dates)
319 done-dates (cdr done-dates))))
320 (if todayp
321 (aset graph index org-habit-today-glyph)))
322 (setq face (if (or in-the-past-p todayp)
323 (car faces)
324 (cdr faces)))
325 (if (and in-the-past-p
326 (not (eq face 'org-habit-overdue-face))
327 (not markedp))
328 (setq face (cdr faces)))
329 (put-text-property index (1+ index) 'face face graph))
330 (setq start (1+ start)
331 index (1+ index)))
332 graph))
334 (defun org-habit-insert-consistency-graphs (&optional line)
335 "Insert consistency graph for any habitual tasks."
336 (let ((inhibit-read-only t) l c
337 (buffer-invisibility-spec '(org-link))
338 (moment (time-subtract (current-time)
339 (list 0 (* 3600 org-extend-today-until) 0)))
340 disabled-overlays)
341 ;; Disable filters; this helps with alignment if there are links.
342 (mapc (lambda (ol)
343 (when (overlay-get ol 'invisible)
344 (overlay-put ol 'invisible nil)
345 (setq disabled-overlays (cons ol disabled-overlays))))
346 (overlays-in (point-min) (point-max)))
347 (save-excursion
348 (goto-char (if line (point-at-bol) (point-min)))
349 (while (not (eobp))
350 (let ((habit (get-text-property (point) 'org-habit-p)))
351 (when habit
352 (move-to-column org-habit-graph-column t)
353 (delete-char (min (+ 1 org-habit-preceding-days
354 org-habit-following-days)
355 (- (line-end-position) (point))))
356 (insert-before-markers
357 (org-habit-build-graph
358 habit
359 (time-subtract moment (days-to-time org-habit-preceding-days))
360 moment
361 (time-add moment (days-to-time org-habit-following-days))))))
362 (forward-line)))
363 (mapc (lambda (ol) (overlay-put ol 'invisible t))
364 disabled-overlays)))
366 (defun org-habit-toggle-habits ()
367 "Toggle display of habits in an agenda buffer."
368 (interactive)
369 (org-agenda-check-type t 'agenda)
370 (setq org-habit-show-habits (not org-habit-show-habits))
371 (org-agenda-redo)
372 (org-agenda-set-mode-name)
373 (message "Habits turned %s"
374 (if org-habit-show-habits "on" "off")))
376 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
378 (provide 'org-habit)
380 ;;; org-habit.el ends here