Refactor org-odt-format-table-cell
[org-mode.git] / lisp / org-habit.el
blob78865035c7ecc34da9da2b802601f11ce7528071
1 ;;; org-habit.el --- The habit tracking code for Org-mode
3 ;; Copyright (C) 2009-2011 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 (defface org-habit-clear-face
71 '((((background light)) (:background "#8270f9"))
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 "#d6e4fc"))
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 "#4df946"))
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 "#acfca9"))
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 "#f5f946"))
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 "#fafca9"))
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 "#f9372d"))
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 "#fc9590"))
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 "Is the task at POM or point a habit?"
133 (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
135 (defun org-habit-parse-todo (&optional pom)
136 "Parse the TODO surrounding point for its habit-related data.
137 Returns a list with the following elements:
139 0: Scheduled date for the habit (may be in the past)
140 1: \".+\"-style repeater for the schedule, in days
141 2: Optional deadline (nil if not present)
142 3: If deadline, the repeater for the deadline, otherwise nil
143 4: A list of all the past dates this todo was mark closed
145 This list represents a \"habit\" for the rest of this module."
146 (save-excursion
147 (if pom (goto-char pom))
148 (assert (org-is-habit-p (point)))
149 (let* ((scheduled (org-get-scheduled-time (point)))
150 (scheduled-repeat (org-get-repeat org-scheduled-string))
151 (end (org-entry-end-position))
152 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
153 closed-dates deadline dr-days sr-days)
154 (if scheduled
155 (setq scheduled (time-to-days scheduled))
156 (error "Habit %s has no scheduled date" habit-entry))
157 (unless scheduled-repeat
158 (error
159 "Habit '%s' has no scheduled repeat period or has an incorrect one"
160 habit-entry))
161 (setq sr-days (org-habit-duration-to-days scheduled-repeat))
162 (unless (> sr-days 0)
163 (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
164 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
165 (setq dr-days (org-habit-duration-to-days
166 (match-string-no-properties 1 scheduled-repeat)))
167 (if (<= dr-days sr-days)
168 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
169 habit-entry scheduled-repeat))
170 (setq deadline (+ scheduled (- dr-days sr-days))))
171 (org-back-to-heading t)
172 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
173 (reversed org-log-states-order-reversed)
174 (search (if reversed 're-search-forward 're-search-backward))
175 (limit (if reversed end (point)))
176 (count 0))
177 (unless reversed (goto-char end))
178 (while (and (< count maxdays)
179 (funcall search "- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t))
180 (push (time-to-days
181 (org-time-string-to-time (match-string-no-properties 1)))
182 closed-dates)
183 (setq count (1+ count))))
184 (list scheduled sr-days deadline dr-days closed-dates))))
186 (defsubst org-habit-scheduled (habit)
187 (nth 0 habit))
188 (defsubst org-habit-scheduled-repeat (habit)
189 (nth 1 habit))
190 (defsubst org-habit-deadline (habit)
191 (let ((deadline (nth 2 habit)))
192 (or deadline
193 (if (nth 3 habit)
194 (+ (org-habit-scheduled habit)
195 (1- (org-habit-scheduled-repeat habit)))
196 (org-habit-scheduled habit)))))
197 (defsubst org-habit-deadline-repeat (habit)
198 (or (nth 3 habit)
199 (org-habit-scheduled-repeat habit)))
200 (defsubst org-habit-done-dates (habit)
201 (nth 4 habit))
203 (defsubst org-habit-get-priority (habit &optional moment)
204 "Determine the relative priority of a habit.
205 This must take into account not just urgency, but consistency as well."
206 (let ((pri 1000)
207 (now (if moment (time-to-days moment) (org-today)))
208 (scheduled (org-habit-scheduled habit))
209 (deadline (org-habit-deadline habit)))
210 ;; add 10 for every day past the scheduled date, and subtract for every
211 ;; day before it
212 (setq pri (+ pri (* (- now scheduled) 10)))
213 ;; add 50 if the deadline is today
214 (if (and (/= scheduled deadline)
215 (= now deadline))
216 (setq pri (+ pri 50)))
217 ;; add 100 for every day beyond the deadline date, and subtract 10 for
218 ;; every day before it
219 (let ((slip (- now (1- deadline))))
220 (if (> slip 0)
221 (setq pri (+ pri (* slip 100)))
222 (setq pri (+ pri (* slip 10)))))
223 pri))
225 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
226 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
227 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
228 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
230 Habits are assigned colors on the following basis:
231 Blue Task is before the scheduled date.
232 Green Task is on or after scheduled date, but before the
233 end of the schedule's repeat period.
234 Yellow If the task has a deadline, then it is after schedule's
235 repeat period, but before the deadline.
236 Orange The task has reached the deadline day, or if there is
237 no deadline, the end of the schedule's repeat period.
238 Red The task has gone beyond the deadline day or the
239 schedule's repeat period."
240 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
241 (s-repeat (org-habit-scheduled-repeat habit))
242 (scheduled-end (+ scheduled (1- s-repeat)))
243 (d-repeat (org-habit-deadline-repeat habit))
244 (deadline (if scheduled-days
245 (+ scheduled-days (- d-repeat s-repeat))
246 (org-habit-deadline habit)))
247 (m-days (or now-days (time-to-days (current-time)))))
248 (cond
249 ((< m-days scheduled)
250 '(org-habit-clear-face . org-habit-clear-future-face))
251 ((< m-days deadline)
252 '(org-habit-ready-face . org-habit-ready-future-face))
253 ((= m-days deadline)
254 (if donep
255 '(org-habit-ready-face . org-habit-ready-future-face)
256 '(org-habit-alert-face . org-habit-alert-future-face)))
258 '(org-habit-overdue-face . org-habit-overdue-future-face)))))
260 (defun org-habit-build-graph (habit starting current ending)
261 "Build a graph for the given HABIT, from STARTING to ENDING.
262 CURRENT gives the current time between STARTING and ENDING, for
263 the purpose of drawing the graph. It need not be the actual
264 current time."
265 (let* ((done-dates (sort (org-habit-done-dates habit) '<))
266 (scheduled (org-habit-scheduled habit))
267 (s-repeat (org-habit-scheduled-repeat habit))
268 (start (time-to-days starting))
269 (now (time-to-days current))
270 (end (time-to-days ending))
271 (graph (make-string (1+ (- end start)) ?\ ))
272 (index 0)
273 last-done-date)
274 (while (and done-dates (< (car done-dates) start))
275 (setq last-done-date (car done-dates)
276 done-dates (cdr done-dates)))
277 (while (< start end)
278 (let* ((in-the-past-p (< start now))
279 (todayp (= start now))
280 (donep (and done-dates
281 (= start (car done-dates))))
282 (faces (if (and in-the-past-p
283 (not last-done-date)
284 (not (< scheduled now)))
285 '(org-habit-clear-face . org-habit-clear-future-face)
286 (org-habit-get-faces
287 habit start (and in-the-past-p
288 (if last-done-date
289 (+ last-done-date s-repeat)
290 scheduled))
291 donep)))
292 markedp face)
293 (if donep
294 (let ((done-time (time-add
295 starting
296 (days-to-time
297 (- start (time-to-days starting))))))
299 (aset graph index ?*)
300 (setq markedp t)
301 (put-text-property
302 index (1+ index) 'help-echo
303 (format-time-string (org-time-stamp-format) done-time) graph)
304 (while (and done-dates
305 (= start (car done-dates)))
306 (setq last-done-date (car done-dates)
307 done-dates (cdr done-dates))))
308 (if todayp
309 (aset graph index ?!)))
310 (setq face (if (or in-the-past-p todayp)
311 (car faces)
312 (cdr faces)))
313 (if (and in-the-past-p
314 (not (eq face 'org-habit-overdue-face))
315 (not markedp))
316 (setq face (cdr faces)))
317 (put-text-property index (1+ index) 'face face graph))
318 (setq start (1+ start)
319 index (1+ index)))
320 graph))
322 (defun org-habit-insert-consistency-graphs (&optional line)
323 "Insert consistency graph for any habitual tasks."
324 (let ((inhibit-read-only t) l c
325 (buffer-invisibility-spec '(org-link))
326 (moment (time-subtract (current-time)
327 (list 0 (* 3600 org-extend-today-until) 0))))
328 (save-excursion
329 (goto-char (if line (point-at-bol) (point-min)))
330 (while (not (eobp))
331 (let ((habit (get-text-property (point) 'org-habit-p)))
332 (when habit
333 (move-to-column org-habit-graph-column t)
334 (delete-char (min (+ 1 org-habit-preceding-days
335 org-habit-following-days)
336 (- (line-end-position) (point))))
337 (insert (org-habit-build-graph
338 habit
339 (time-subtract moment
340 (days-to-time org-habit-preceding-days))
341 moment
342 (time-add moment
343 (days-to-time org-habit-following-days))))))
344 (forward-line)))))
346 (defun org-habit-toggle-habits ()
347 "Toggle display of habits in an agenda buffer."
348 (interactive)
349 (org-agenda-check-type t 'agenda)
350 (setq org-habit-show-habits (not org-habit-show-habits))
351 (org-agenda-redo)
352 (org-agenda-set-mode-name)
353 (message "Habits turned %s"
354 (if org-habit-show-habits "on" "off")))
356 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
358 (provide 'org-habit)
360 ;;; org-habit.el ends here