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