Docbook export: Implement ordered lists with offset
[org-mode/org-tableheadings.git] / lisp / org-habit.el
blobbb8773e3d5245b904d76363a3b1448e9f1b2b57e
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: 6.35trans
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))
35 (defgroup org-habit nil
36 "Options concerning habit tracking in Org-mode."
37 :tag "Org Habit"
38 :group 'org-progress)
40 (defcustom org-habit-graph-column 40
41 "The absolute column at which to insert habit consistency graphs.
42 Note that consistency graphs will overwrite anything else in the buffer."
43 :group 'org-habit
44 :type 'integer)
46 (defcustom org-habit-preceding-days 21
47 "Number of days before today to appear in consistency graphs."
48 :group 'org-habit
49 :type 'integer)
51 (defcustom org-habit-following-days 7
52 "Number of days after today to appear in consistency graphs."
53 :group 'org-habit
54 :type 'integer)
56 (defcustom org-habit-show-habits t
57 "If non-nil, show habits in agenda buffers."
58 :group 'org-habit
59 :type 'boolean)
61 (defcustom org-habit-show-habits-only-for-today t
62 "If non-nil, only show habits on today's agenda, and not for future days.
63 Note that even when shown for future days, the graph is always
64 relative to the current effective date."
65 :group 'org-habit
66 :type 'boolean)
68 (defface org-habit-clear-face
69 '((((background light)) (:background "#8270f9"))
70 (((background dark)) (:background "blue")))
71 "Face for days on which a task shouldn't be done yet."
72 :group 'org-habit
73 :group 'org-faces)
74 (defface org-habit-clear-future-face
75 '((((background light)) (:background "#d6e4fc"))
76 (((background dark)) (:background "midnightblue")))
77 "Face for future days on which a task shouldn't be done yet."
78 :group 'org-habit
79 :group 'org-faces)
81 (defface org-habit-ready-face
82 '((((background light)) (:background "#4df946"))
83 (((background dark)) (:background "forestgreen")))
84 "Face for days on which a task should start to be done."
85 :group 'org-habit
86 :group 'org-faces)
87 (defface org-habit-ready-future-face
88 '((((background light)) (:background "#acfca9"))
89 (((background dark)) (:background "darkgreen")))
90 "Face for days on which a task should start to be done."
91 :group 'org-habit
92 :group 'org-faces)
94 (defface org-habit-alert-face
95 '((((background light)) (:background "#f5f946"))
96 (((background dark)) (:background "gold")))
97 "Face for days on which a task is due."
98 :group 'org-habit
99 :group 'org-faces)
100 (defface org-habit-alert-future-face
101 '((((background light)) (:background "#fafca9"))
102 (((background dark)) (:background "darkgoldenrod")))
103 "Face for days on which a task is due."
104 :group 'org-habit
105 :group 'org-faces)
107 (defface org-habit-overdue-face
108 '((((background light)) (:background "#f9372d"))
109 (((background dark)) (:background "firebrick")))
110 "Face for days on which a task is overdue."
111 :group 'org-habit
112 :group 'org-faces)
113 (defface org-habit-overdue-future-face
114 '((((background light)) (:background "#fc9590"))
115 (((background dark)) (:background "darkred")))
116 "Face for days on which a task is overdue."
117 :group 'org-habit
118 :group 'org-faces)
120 (defun org-habit-duration-to-days (ts)
121 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
122 ;; lead time is specified.
123 (floor (* (string-to-number (match-string 1 ts))
124 (cdr (assoc (match-string 2 ts)
125 '(("d" . 1) ("w" . 7)
126 ("m" . 30.4) ("y" . 365.25))))))
127 (error "Invalid duration string: %s" ts)))
129 (defun org-is-habit-p (&optional pom)
130 "Is the task at POM or point a habit?"
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 (habit-entry (org-no-properties (nth 5 (org-heading-components))))
152 closed-dates deadline dr-days)
153 (if scheduled
154 (setq scheduled (time-to-days scheduled))
155 (error "Habit %s has no scheduled date" habit-entry))
156 (unless scheduled-repeat
157 (error "Habit %s has no scheduled repeat period" habit-entry))
158 (unless (> sr-days 0)
159 (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
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 (%s)"
165 habit-entry scheduled-repeat))
166 (setq deadline (+ scheduled (- dr-days sr-days))))
167 (org-back-to-heading t)
168 (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
169 (push (time-to-days
170 (org-time-string-to-time (match-string-no-properties 1)))
171 closed-dates))
172 (list scheduled sr-days deadline dr-days closed-dates))))
174 (defsubst org-habit-scheduled (habit)
175 (nth 0 habit))
176 (defsubst org-habit-scheduled-repeat (habit)
177 (nth 1 habit))
178 (defsubst org-habit-deadline (habit)
179 (let ((deadline (nth 2 habit)))
180 (or deadline
181 (+ (org-habit-scheduled habit)
182 (1- (org-habit-scheduled-repeat habit))))))
183 (defsubst org-habit-deadline-repeat (habit)
184 (or (nth 3 habit)
185 (org-habit-scheduled-repeat habit)))
186 (defsubst org-habit-done-dates (habit)
187 (nth 4 habit))
189 (defsubst org-habit-get-priority (habit &optional moment)
190 "Determine the relative priority of a habit.
191 This must take into account not just urgency, but consistency as well."
192 (let ((pri 1000)
193 (now (time-to-days
194 (or moment
195 (time-subtract (current-time)
196 (list 0 (* 3600 org-extend-today-until) 0)))))
197 (scheduled (org-habit-scheduled habit))
198 (deadline (org-habit-deadline habit)))
199 ;; add 10 for every day past the scheduled date, and subtract for every
200 ;; day before it
201 (setq pri (+ pri (* (- now scheduled) 10)))
202 ;; add 50 if the deadline is today
203 (if (and (/= scheduled deadline)
204 (= now deadline))
205 (setq pri (+ pri 50)))
206 ;; add 100 for every day beyond the deadline date, and subtract 10 for
207 ;; every day before it
208 (let ((slip (- now (1- deadline))))
209 (if (> slip 0)
210 (setq pri (+ pri (* slip 100)))
211 (setq pri (+ pri (* slip 10)))))
212 pri))
214 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
215 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
216 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
217 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
219 Habits are assigned colors on the following basis:
220 Blue Task is before the scheduled date.
221 Green Task is on or after scheduled date, but before the
222 end of the schedule's repeat period.
223 Yellow If the task has a deadline, then it is after schedule's
224 repeat period, but before the deadline.
225 Orange The task has reached the deadline day, or if there is
226 no deadline, the end of the schedule's repeat period.
227 Red The task has gone beyond the deadline day or the
228 schedule's repeat period."
229 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
230 (s-repeat (org-habit-scheduled-repeat habit))
231 (scheduled-end (+ scheduled (1- s-repeat)))
232 (d-repeat (org-habit-deadline-repeat habit))
233 (deadline (if scheduled-days
234 (+ scheduled-days (- d-repeat s-repeat))
235 (org-habit-deadline habit)))
236 (m-days (or now-days (time-to-days (current-time)))))
237 (cond
238 ((< m-days scheduled)
239 '(org-habit-clear-face . org-habit-clear-future-face))
240 ((< m-days deadline)
241 '(org-habit-ready-face . org-habit-ready-future-face))
242 ((= m-days deadline)
243 (if donep
244 '(org-habit-ready-face . org-habit-ready-future-face)
245 '(org-habit-alert-face . org-habit-alert-future-face)))
247 '(org-habit-overdue-face . org-habit-overdue-future-face)))))
249 (defun org-habit-build-graph (habit starting current ending)
250 "Build a graph for the given HABIT, from STARTING to ENDING.
251 CURRENT gives the current time between STARTING and ENDING, for
252 the purpose of drawing the graph. It need not be the actual
253 current time."
254 (let* ((done-dates (sort (org-habit-done-dates habit) '<))
255 (scheduled (org-habit-scheduled habit))
256 (s-repeat (org-habit-scheduled-repeat habit))
257 (start (time-to-days starting))
258 (now (time-to-days current))
259 (end (time-to-days ending))
260 (graph (make-string (1+ (- end start)) ?\ ))
261 (index 0)
262 last-done-date)
263 (while (and done-dates (< (car done-dates) start))
264 (setq last-done-date (car done-dates)
265 done-dates (cdr done-dates)))
266 (while (< start end)
267 (let* ((in-the-past-p (< start now))
268 (todayp (= start now))
269 (donep (and done-dates
270 (= start (car done-dates))))
271 (faces (if (and in-the-past-p
272 (not last-done-date)
273 (not (< scheduled now)))
274 '(org-habit-clear-face . org-habit-clear-future-face)
275 (org-habit-get-faces
276 habit start (and in-the-past-p
277 (if last-done-date
278 (+ last-done-date s-repeat)
279 scheduled))
280 donep)))
281 markedp face)
282 (if donep
283 (progn
284 (aset graph index ?*)
285 (setq markedp t)
286 (while (and done-dates
287 (= start (car done-dates)))
288 (setq last-done-date (car done-dates)
289 done-dates (cdr done-dates))))
290 (if todayp
291 (aset graph index ?!)))
292 (setq face (if (or in-the-past-p todayp)
293 (car faces)
294 (cdr faces)))
295 (if (and in-the-past-p
296 (not (eq face 'org-habit-overdue-face))
297 (not markedp))
298 (setq face (cdr faces)))
299 (put-text-property index (1+ index) 'face face graph))
300 (setq start (1+ start)
301 index (1+ index)))
302 graph))
304 (defun org-habit-insert-consistency-graphs (&optional line)
305 "Insert consistency graph for any habitual tasks."
306 (let ((inhibit-read-only t) l c
307 (buffer-invisibility-spec '(org-link))
308 (moment (time-subtract (current-time)
309 (list 0 (* 3600 org-extend-today-until) 0))))
310 (save-excursion
311 (goto-char (if line (point-at-bol) (point-min)))
312 (while (not (eobp))
313 (let ((habit (get-text-property (point) 'org-habit-p)))
314 (when habit
315 (move-to-column org-habit-graph-column t)
316 (delete-char (min (+ 1 org-habit-preceding-days
317 org-habit-following-days)
318 (- (line-end-position) (point))))
319 (insert (org-habit-build-graph
320 habit
321 (time-subtract moment
322 (days-to-time org-habit-preceding-days))
323 moment
324 (time-add moment
325 (days-to-time org-habit-following-days))))))
326 (forward-line)))))
328 (defun org-habit-toggle-habits ()
329 "Toggle display of habits in an agenda buffer."
330 (interactive)
331 (org-agenda-check-type t 'agenda)
332 (setq org-habit-show-habits (not org-habit-show-habits))
333 (org-agenda-redo)
334 (org-agenda-set-mode-name)
335 (message "Habits turned %s"
336 (if org-habit-show-habits "on" "off")))
338 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
340 (provide 'org-habit)
342 ;; arch-tag: 64e070d9-bd09-4917-bd44-44465f5ed348
344 ;;; org-habit.el ends here