Keep compiler happy
[org-mode.git] / lisp / org-habit.el
blob9aa8d52e2f29ef8e314fe31bc057cd4460a224e3
1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw at gnu dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.32trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the habit tracking code for Org-mode
31 (require 'org)
32 (require 'org-agenda)
33 (eval-when-compile
34 (require 'cl)
35 (require 'calendar))
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 (defface org-habit-clear-face
71 '((((background light)) (:background "slateblue"))
72 (((background dark)) (:background "blue")))
73 "Face for days on which a task shouldn't be done yet."
74 :group 'org-habit
75 :group 'org-faces)
76 (defface org-habit-clear-future-face
77 '((((background light)) (:background "powderblue"))
78 (((background dark)) (:background "midnightblue")))
79 "Face for future days on which a task shouldn't be done yet."
80 :group 'org-habit
81 :group 'org-faces)
83 (defface org-habit-ready-face
84 '((((background light)) (:background "green"))
85 (((background dark)) (:background "forestgreen")))
86 "Face for days on which a task should start to be done."
87 :group 'org-habit
88 :group 'org-faces)
89 (defface org-habit-ready-future-face
90 '((((background light)) (:background "palegreen"))
91 (((background dark)) (:background "darkgreen")))
92 "Face for days on which a task should start to be done."
93 :group 'org-habit
94 :group 'org-faces)
96 (defface org-habit-alert-face
97 '((((background light)) (:background "yellow"))
98 (((background dark)) (:background "gold")))
99 "Face for days on which a task is due."
100 :group 'org-habit
101 :group 'org-faces)
102 (defface org-habit-alert-future-face
103 '((((background light)) (:background "palegoldenrod"))
104 (((background dark)) (:background "darkgoldenrod")))
105 "Face for days on which a task is due."
106 :group 'org-habit
107 :group 'org-faces)
109 (defface org-habit-overdue-face
110 '((((background light)) (:background "red"))
111 (((background dark)) (:background "firebrick")))
112 "Face for days on which a task is overdue."
113 :group 'org-habit
114 :group 'org-faces)
115 (defface org-habit-overdue-future-face
116 '((((background light)) (:background "mistyrose"))
117 (((background dark)) (:background "darkred")))
118 "Face for days on which a task is overdue."
119 :group 'org-habit
120 :group 'org-faces)
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 (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
134 (defun org-habit-parse-todo (&optional pom)
135 "Parse the TODO surrounding point for its habit-related data.
136 Returns a list with the following elements:
138 0: Scheduled date for the habit (may be in the past)
139 1: \".+\"-style repeater for the schedule, in days
140 2: Optional deadline (nil if not present)
141 3: If deadline, the repeater for the deadline, otherwise nil
142 4: A list of all the past dates this todo was mark closed
144 This list represents a \"habit\" for the rest of this module."
145 (save-excursion
146 (if pom (goto-char pom))
147 (assert (org-is-habit-p (point)))
148 (let* ((scheduled (org-get-scheduled-time (point)))
149 (scheduled-repeat (org-get-repeat org-scheduled-string))
150 (sr-days (org-habit-duration-to-days scheduled-repeat))
151 (end (org-entry-end-position))
152 closed-dates deadline dr-days)
153 (if scheduled
154 (setq scheduled (time-to-days scheduled))
155 (error "Habit has no scheduled date"))
156 (unless scheduled-repeat
157 (error "Habit has no scheduled repeat period"))
158 (unless (> sr-days 0)
159 (error "Habit's scheduled repeat period is less than 1d"))
160 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
161 (setq dr-days (org-habit-duration-to-days
162 (match-string-no-properties 1 scheduled-repeat)))
163 (if (<= dr-days sr-days)
164 (error "Habit's deadline repeat period is less than or equal to scheduled"))
165 (setq deadline (+ scheduled (- dr-days sr-days))))
166 (org-back-to-heading t)
167 (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
168 (push (time-to-days
169 (org-time-string-to-time (match-string-no-properties 1)))
170 closed-dates))
171 (list scheduled sr-days deadline dr-days closed-dates))))
173 (defsubst org-habit-scheduled (habit)
174 (nth 0 habit))
175 (defsubst org-habit-scheduled-repeat (habit)
176 (nth 1 habit))
177 (defsubst org-habit-deadline (habit)
178 (let ((deadline (nth 2 habit)))
179 (or deadline
180 (+ (org-habit-scheduled habit)
181 (1- (org-habit-scheduled-repeat habit))))))
182 (defsubst org-habit-deadline-repeat (habit)
183 (or (nth 3 habit)
184 (org-habit-scheduled-repeat habit)))
185 (defsubst org-habit-done-dates (habit)
186 (nth 4 habit))
188 (defsubst org-habit-get-priority (habit &optional moment)
189 "Determine the relative priority of a habit.
190 This must take into account not just urgency, but consistency as well."
191 (let ((pri 1000)
192 (now (time-to-days
193 (or moment
194 (time-subtract (current-time)
195 (list 0 (* 3600 org-extend-today-until) 0)))))
196 (scheduled (org-habit-scheduled habit))
197 (deadline (org-habit-deadline habit)))
198 ;; add 10 for every day past the scheduled date, and subtract for every
199 ;; day before it
200 (setq pri (+ pri (* (- now scheduled) 10)))
201 ;; add 50 if the deadline is today
202 (if (and (/= scheduled deadline)
203 (= now deadline))
204 (setq pri (+ pri 50)))
205 ;; add 100 for every day beyond the deadline date, and subtract 10 for
206 ;; every day before it
207 (let ((slip (- now (1- deadline))))
208 (if (> slip 0)
209 (setq pri (+ pri (* slip 100)))
210 (setq pri (+ pri (* slip 10)))))
211 pri))
213 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
214 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
215 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
216 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
218 Habits are assigned colors on the following basis:
219 Blue Task is before the scheduled date.
220 Green Task is on or after scheduled date, but before the
221 end of the schedule's repeat period.
222 Yellow If the task has a deadline, then it is after schedule's
223 repeat period, but before the deadline.
224 Orange The task has reached the deadline day, or if there is
225 no deadline, the end of the schedule's repeat period.
226 Red The task has gone beyond the deadline day or the
227 schedule's repeat period."
228 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
229 (s-repeat (org-habit-scheduled-repeat habit))
230 (scheduled-end (+ scheduled (1- s-repeat)))
231 (d-repeat (org-habit-deadline-repeat habit))
232 (deadline (if scheduled-days
233 (+ scheduled-days (- d-repeat s-repeat))
234 (org-habit-deadline habit)))
235 (m-days (or now-days (time-to-days (current-time)))))
236 (cond
237 ((< m-days scheduled)
238 '(org-habit-clear-face . org-habit-clear-future-face))
239 ((< m-days deadline)
240 '(org-habit-ready-face . org-habit-ready-future-face))
241 ((= m-days deadline)
242 (if donep
243 '(org-habit-ready-face . org-habit-ready-future-face)
244 '(org-habit-alert-face . org-habit-alert-future-face)))
246 '(org-habit-overdue-face . org-habit-overdue-future-face)))))
248 (defun org-habit-build-graph (habit starting current ending)
249 "Build a graph for the given HABIT, from STARTING to ENDING.
250 CURRENT gives the current time between STARTING and ENDING, for
251 the purpose of drawing the graph. It need not be the actual
252 current time."
253 (let* ((done-dates (sort (org-habit-done-dates habit) '<))
254 (scheduled (org-habit-scheduled habit))
255 (s-repeat (org-habit-scheduled-repeat habit))
256 (start (time-to-days starting))
257 (now (time-to-days current))
258 (end (time-to-days ending))
259 (graph (make-string (1+ (- end start)) ?\ ))
260 (index 0)
261 last-done-date)
262 (while (and done-dates (< (car done-dates) start))
263 (setq last-done-date (car done-dates)
264 done-dates (cdr done-dates)))
265 (while (< start end)
266 (let* ((in-the-past-p (< start now))
267 (todayp (= start now))
268 (donep (and done-dates
269 (= start (car done-dates))))
270 (faces (if (and in-the-past-p
271 (not last-done-date)
272 (not (< scheduled now)))
273 '(org-habit-clear-face . org-habit-clear-future-face)
274 (org-habit-get-faces
275 habit start (and in-the-past-p
276 (if last-done-date
277 (+ last-done-date s-repeat)
278 scheduled))
279 donep)))
280 markedp face)
281 (if donep
282 (progn
283 (aset graph index ?*)
284 (setq markedp t)
285 (while (and done-dates
286 (= start (car done-dates)))
287 (setq last-done-date (car done-dates)
288 done-dates (cdr done-dates))))
289 (if todayp
290 (aset graph index ?!)))
291 (setq face (if (or in-the-past-p todayp)
292 (car faces)
293 (cdr faces)))
294 (if (and in-the-past-p
295 (not (eq face 'org-habit-overdue-face))
296 (not markedp))
297 (setq face (cdr faces)))
298 (put-text-property index (1+ index) 'face face graph))
299 (setq start (1+ start)
300 index (1+ index)))
301 graph))
303 (defun org-habit-insert-consistency-graphs (&optional line)
304 "Insert consistency graph for any habitual tasks."
305 (let ((inhibit-read-only t) l c
306 (moment (time-subtract (current-time)
307 (list 0 (* 3600 org-extend-today-until) 0))))
308 (save-excursion
309 (goto-char (if line (point-at-bol) (point-min)))
310 (while (not (eobp))
311 (let ((habit (get-text-property (point) 'org-habit-p)))
312 (when habit
313 (move-to-column org-habit-graph-column t)
314 (delete-char (min (+ 1 org-habit-preceding-days
315 org-habit-following-days)
316 (- (line-end-position) (point))))
317 (insert (org-habit-build-graph
318 habit
319 (time-subtract moment
320 (days-to-time org-habit-preceding-days))
321 moment
322 (time-add moment
323 (days-to-time org-habit-following-days))))))
324 (forward-line)))))
326 (defun org-habit-toggle-habits ()
327 "Toggle display of habits in an agenda buffer."
328 (interactive)
329 (org-agenda-check-type t 'agenda)
330 (setq org-habit-show-habits (not org-habit-show-habits))
331 (org-agenda-redo)
332 (org-agenda-set-mode-name)
333 (message "Habits turned %s"
334 (if org-habit-show-habits "on" "off")))
336 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
338 (provide 'org-habit)
340 ;; arch-tag:
342 ;;; org-habit.el ends here