Fix some trivial typos
[org-mode.git] / lisp / org-habit.el
blobfb9f132596530f9be946295276953380f33c3d0a
1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009 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: 6.32trans
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 (require 'org)
31 (require 'org-agenda)
32 (eval-when-compile
33 (require 'cl)
34 (require 'calendar))
36 (defgroup org-habit nil
37 "Options concerning habit tracking in Org-mode."
38 :tag "Org Habit"
39 :group 'org-progress)
41 (defcustom org-habit-graph-column 40
42 "The absolute column at which to insert habit consistency graphs.
43 Note that consistency graphs will overwrite anything else in the buffer."
44 :group 'org-habit
45 :type 'integer)
47 (defcustom org-habit-preceding-days 21
48 "Number of days before today to appear in consistency graphs."
49 :group 'org-habit
50 :type 'integer)
52 (defcustom org-habit-following-days 7
53 "Number of days after today to appear in consistency graphs."
54 :group 'org-habit
55 :type 'integer)
57 (defcustom org-habit-show-habits t
58 "If non-nil, show habits in agenda buffers."
59 :group 'org-habit
60 :type 'boolean)
62 (defcustom org-habit-show-habits-only-for-today t
63 "If non-nil, only show habits on today's agenda, and not for future days.
64 Note that even when shown for future days, the graph is always
65 relative to the current effective date."
66 :group 'org-habit
67 :type 'boolean)
69 (defface org-habit-clear-face
70 '((((background light)) (:background "slateblue"))
71 (((background dark)) (:background "blue")))
72 "Face for days on which a task shouldn't be done yet."
73 :group 'org-habit
74 :group 'org-faces)
75 (defface org-habit-clear-future-face
76 '((((background light)) (:background "powderblue"))
77 (((background dark)) (:background "midnightblue")))
78 "Face for future days on which a task shouldn't be done yet."
79 :group 'org-habit
80 :group 'org-faces)
82 (defface org-habit-ready-face
83 '((((background light)) (:background "green"))
84 (((background dark)) (:background "forestgreen")))
85 "Face for days on which a task should start to be done."
86 :group 'org-habit
87 :group 'org-faces)
88 (defface org-habit-ready-future-face
89 '((((background light)) (:background "palegreen"))
90 (((background dark)) (:background "darkgreen")))
91 "Face for days on which a task should start to be done."
92 :group 'org-habit
93 :group 'org-faces)
95 (defface org-habit-alert-face
96 '((((background light)) (:background "yellow"))
97 (((background dark)) (:background "gold")))
98 "Face for days on which a task is due."
99 :group 'org-habit
100 :group 'org-faces)
101 (defface org-habit-alert-future-face
102 '((((background light)) (:background "palegoldenrod"))
103 (((background dark)) (:background "darkgoldenrod")))
104 "Face for days on which a task is due."
105 :group 'org-habit
106 :group 'org-faces)
108 (defface org-habit-overdue-face
109 '((((background light)) (:background "red"))
110 (((background dark)) (:background "firebrick")))
111 "Face for days on which a task is overdue."
112 :group 'org-habit
113 :group 'org-faces)
114 (defface org-habit-overdue-future-face
115 '((((background light)) (:background "mistyrose"))
116 (((background dark)) (:background "darkred")))
117 "Face for days on which a task is overdue."
118 :group 'org-habit
119 :group 'org-faces)
121 (defun org-habit-duration-to-days (ts)
122 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
123 ;; lead time is specified.
124 (floor (* (string-to-number (match-string 1 ts))
125 (cdr (assoc (match-string 2 ts)
126 '(("d" . 1) ("w" . 7)
127 ("m" . 30.4) ("y" . 365.25))))))
128 (error "Invalid duration string: %s" ts)))
130 (defun org-is-habit-p (&optional pom)
131 (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
133 (defun org-habit-parse-todo (&optional pom)
134 "Parse the TODO surrounding point for its habit-related data.
135 Returns a list with the following elements:
137 0: Scheduled date for the habit (may be in the past)
138 1: \".+\"-style repeater for the schedule, in days
139 2: Optional deadline (nil if not present)
140 3: If deadline, the repeater for the deadline, otherwise nil
141 4: A list of all the past dates this todo was mark closed
143 This list represents a \"habit\" for the rest of this module."
144 (save-excursion
145 (if pom (goto-char pom))
146 (assert (org-is-habit-p (point)))
147 (let* ((scheduled (org-get-scheduled-time (point)))
148 (scheduled-repeat (org-get-repeat org-scheduled-string))
149 (sr-days (org-habit-duration-to-days scheduled-repeat))
150 (end (org-entry-end-position))
151 closed-dates deadline dr-days)
152 (if scheduled
153 (setq scheduled (time-to-days scheduled))
154 (error "Habit has no scheduled date"))
155 (unless scheduled-repeat
156 (error "Habit has no scheduled repeat period"))
157 (unless (> sr-days 0)
158 (error "Habit's scheduled repeat period is less than 1d"))
159 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
160 (setq dr-days (org-habit-duration-to-days
161 (match-string-no-properties 1 scheduled-repeat)))
162 (if (<= dr-days sr-days)
163 (error "Habit's deadline repeat period is less than or equal to scheduled"))
164 (setq deadline (+ scheduled (- dr-days sr-days))))
165 (org-back-to-heading t)
166 (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
167 (push (time-to-days
168 (org-time-string-to-time (match-string-no-properties 1)))
169 closed-dates))
170 (list scheduled sr-days deadline dr-days closed-dates))))
172 (defsubst org-habit-scheduled (habit)
173 (nth 0 habit))
174 (defsubst org-habit-scheduled-repeat (habit)
175 (nth 1 habit))
176 (defsubst org-habit-deadline (habit)
177 (let ((deadline (nth 2 habit)))
178 (or deadline
179 (+ (org-habit-scheduled habit)
180 (1- (org-habit-scheduled-repeat habit))))))
181 (defsubst org-habit-deadline-repeat (habit)
182 (or (nth 3 habit)
183 (org-habit-scheduled-repeat habit)))
184 (defsubst org-habit-done-dates (habit)
185 (nth 4 habit))
187 (defsubst org-habit-get-priority (habit &optional moment)
188 "Determine the relative priority of a habit.
189 This must take into account not just urgency, but consistency as well."
190 (let ((pri 1000)
191 (now (time-to-days
192 (or moment
193 (time-subtract (current-time)
194 (list 0 (* 3600 org-extend-today-until) 0)))))
195 (scheduled (org-habit-scheduled habit))
196 (deadline (org-habit-deadline habit)))
197 ;; add 10 for every day past the scheduled date, and subtract for every
198 ;; day before it
199 (setq pri (+ pri (* (- now scheduled) 10)))
200 ;; add 50 if the deadline is today
201 (if (and (/= scheduled deadline)
202 (= now deadline))
203 (setq pri (+ pri 50)))
204 ;; add 100 for every day beyond the deadline date, and subtract 10 for
205 ;; every day before it
206 (let ((slip (- now (1- deadline))))
207 (if (> slip 0)
208 (setq pri (+ pri (* slip 100)))
209 (setq pri (+ pri (* slip 10)))))
210 pri))
212 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
213 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
214 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
215 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
217 Habits are assigned colors on the following basis:
218 Blue Task is before the scheduled date.
219 Green Task is on or after scheduled date, but before the
220 end of the schedule's repeat period.
221 Yellow If the task has a deadline, then it is after schedule's
222 repeat period, but before the deadline.
223 Orange The task has reached the deadline day, or if there is
224 no deadline, the end of the schedule's repeat period.
225 Red The task has gone beyond the deadline day or the
226 schedule's repeat period."
227 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
228 (s-repeat (org-habit-scheduled-repeat habit))
229 (scheduled-end (+ scheduled (1- s-repeat)))
230 (d-repeat (org-habit-deadline-repeat habit))
231 (deadline (if scheduled-days
232 (+ scheduled-days (- d-repeat s-repeat))
233 (org-habit-deadline habit)))
234 (m-days (or now-days (time-to-days (current-time)))))
235 (cond
236 ((< m-days scheduled)
237 '(org-habit-clear-face . org-habit-clear-future-face))
238 ((< m-days deadline)
239 '(org-habit-ready-face . org-habit-ready-future-face))
240 ((= m-days deadline)
241 (if donep
242 '(org-habit-ready-face . org-habit-ready-future-face)
243 '(org-habit-alert-face . org-habit-alert-future-face)))
245 '(org-habit-overdue-face . org-habit-overdue-future-face)))))
247 (defun org-habit-build-graph (habit starting current ending)
248 "Build a graph for the given HABIT, from STARTING to ENDING.
249 CURRENT gives the current time between STARTING and ENDING, for
250 the purpose of drawing the graph. It need not be the actual
251 current time."
252 (let* ((done-dates (sort (org-habit-done-dates habit) '<))
253 (scheduled (org-habit-scheduled habit))
254 (s-repeat (org-habit-scheduled-repeat habit))
255 (start (time-to-days starting))
256 (now (time-to-days current))
257 (end (time-to-days ending))
258 (graph (make-string (1+ (- end start)) ?\ ))
259 (index 0)
260 last-done-date)
261 (while (and done-dates (< (car done-dates) start))
262 (setq last-done-date (car done-dates)
263 done-dates (cdr done-dates)))
264 (while (< start end)
265 (let* ((in-the-past-p (< start now))
266 (todayp (= start now))
267 (donep (and done-dates
268 (= start (car done-dates))))
269 (faces (if (and in-the-past-p
270 (not last-done-date)
271 (not (< scheduled now)))
272 '(org-habit-clear-face . org-habit-clear-future-face)
273 (org-habit-get-faces
274 habit start (and in-the-past-p
275 (if last-done-date
276 (+ last-done-date s-repeat)
277 scheduled))
278 donep)))
279 markedp face)
280 (if donep
281 (progn
282 (aset graph index ?*)
283 (setq markedp t)
284 (while (and done-dates
285 (= start (car done-dates)))
286 (setq last-done-date (car done-dates)
287 done-dates (cdr done-dates))))
288 (if todayp
289 (aset graph index ?!)))
290 (setq face (if (or in-the-past-p todayp)
291 (car faces)
292 (cdr faces)))
293 (if (and in-the-past-p
294 (not (eq face 'org-habit-overdue-face))
295 (not markedp))
296 (setq face (cdr faces)))
297 (put-text-property index (1+ index) 'face face graph))
298 (setq start (1+ start)
299 index (1+ index)))
300 graph))
302 (defun org-habit-insert-consistency-graphs (&optional line)
303 "Insert consistency graph for any habitual tasks."
304 (let ((inhibit-read-only t) l c
305 (moment (time-subtract (current-time)
306 (list 0 (* 3600 org-extend-today-until) 0))))
307 (save-excursion
308 (goto-char (if line (point-at-bol) (point-min)))
309 (while (not (eobp))
310 (let ((habit (get-text-property (point) 'org-habit-p)))
311 (when habit
312 (move-to-column org-habit-graph-column t)
313 (delete-char (min (+ 1 org-habit-preceding-days
314 org-habit-following-days)
315 (- (line-end-position) (point))))
316 (insert (org-habit-build-graph
317 habit
318 (time-subtract moment
319 (days-to-time org-habit-preceding-days))
320 moment
321 (time-add moment
322 (days-to-time org-habit-following-days))))))
323 (forward-line)))))
325 (defun org-habit-toggle-habits ()
326 "Toggle display of habits in an agenda buffer."
327 (interactive)
328 (org-agenda-check-type t 'agenda)
329 (setq org-habit-show-habits (not org-habit-show-habits))
330 (org-agenda-redo)
331 (org-agenda-set-mode-name)
332 (message "Habits turned %s"
333 (if org-habit-show-habits "on" "off")))
335 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
337 (provide 'org-habit)
339 ;; arch-tag: 64e070d9-bd09-4917-bd44-44465f5ed348
341 ;;; org-habit.el ends here