Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / org-habit.el
blobba248f871ef8e7e04c8d45ea63103a3dc04a9be7
1 ;;; org-habit.el --- The habit tracking code for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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
168 5: Repeater type as a string
170 This list represents a \"habit\" for the rest of this module."
171 (save-excursion
172 (if pom (goto-char pom))
173 (assert (org-is-habit-p (point)))
174 (let* ((scheduled (org-get-scheduled-time (point)))
175 (scheduled-repeat (org-get-repeat org-scheduled-string))
176 (end (org-entry-end-position))
177 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
178 closed-dates deadline dr-days sr-days sr-type)
179 (if scheduled
180 (setq scheduled (time-to-days scheduled))
181 (error "Habit %s has no scheduled date" habit-entry))
182 (unless scheduled-repeat
183 (error
184 "Habit `%s' has no scheduled repeat period or has an incorrect one"
185 habit-entry))
186 (setq sr-days (org-habit-duration-to-days scheduled-repeat)
187 sr-type (progn (string-match "[\\.+]?\\+" scheduled-repeat)
188 (match-string-no-properties 0 scheduled-repeat)))
189 (unless (> sr-days 0)
190 (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
191 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
192 (setq dr-days (org-habit-duration-to-days
193 (match-string-no-properties 1 scheduled-repeat)))
194 (if (<= dr-days sr-days)
195 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
196 habit-entry scheduled-repeat))
197 (setq deadline (+ scheduled (- dr-days sr-days))))
198 (org-back-to-heading t)
199 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
200 (reversed org-log-states-order-reversed)
201 (search (if reversed 're-search-forward 're-search-backward))
202 (limit (if reversed end (point)))
203 (count 0)
204 (re (format
205 "^[ \t]*-[ \t]+\\(?:State \"%s\".*%s%s\\)"
206 (regexp-opt org-done-keywords)
207 org-ts-regexp-inactive
208 (let ((value (cdr (assq 'done org-log-note-headings))))
209 (if (not value) ""
210 (concat "\\|"
211 (org-replace-escapes
212 (regexp-quote value)
213 `(("%d" . ,org-ts-regexp-inactive)
214 ("%D" . ,org-ts-regexp)
215 ("%s" . "\"\\S-+\"")
216 ("%S" . "\"\\S-+\"")
217 ("%t" . ,org-ts-regexp-inactive)
218 ("%T" . ,org-ts-regexp)
219 ("%u" . ".*?")
220 ("%U" . ".*?")))))))))
221 (unless reversed (goto-char end))
222 (while (and (< count maxdays) (funcall search re limit t))
223 (push (time-to-days
224 (org-time-string-to-time
225 (or (match-string-no-properties 1)
226 (match-string-no-properties 2))))
227 closed-dates)
228 (setq count (1+ count))))
229 (list scheduled sr-days deadline dr-days closed-dates sr-type))))
231 (defsubst org-habit-scheduled (habit)
232 (nth 0 habit))
233 (defsubst org-habit-scheduled-repeat (habit)
234 (nth 1 habit))
235 (defsubst org-habit-deadline (habit)
236 (let ((deadline (nth 2 habit)))
237 (or deadline
238 (if (nth 3 habit)
239 (+ (org-habit-scheduled habit)
240 (1- (org-habit-scheduled-repeat habit)))
241 (org-habit-scheduled habit)))))
242 (defsubst org-habit-deadline-repeat (habit)
243 (or (nth 3 habit)
244 (org-habit-scheduled-repeat habit)))
245 (defsubst org-habit-done-dates (habit)
246 (nth 4 habit))
247 (defsubst org-habit-repeat-type (habit)
248 (nth 5 habit))
250 (defsubst org-habit-get-priority (habit &optional moment)
251 "Determine the relative priority of a habit.
252 This must take into account not just urgency, but consistency as well."
253 (let ((pri 1000)
254 (now (if moment (time-to-days moment) (org-today)))
255 (scheduled (org-habit-scheduled habit))
256 (deadline (org-habit-deadline habit)))
257 ;; add 10 for every day past the scheduled date, and subtract for every
258 ;; day before it
259 (setq pri (+ pri (* (- now scheduled) 10)))
260 ;; add 50 if the deadline is today
261 (if (and (/= scheduled deadline)
262 (= now deadline))
263 (setq pri (+ pri 50)))
264 ;; add 100 for every day beyond the deadline date, and subtract 10 for
265 ;; every day before it
266 (let ((slip (- now (1- deadline))))
267 (if (> slip 0)
268 (setq pri (+ pri (* slip 100)))
269 (setq pri (+ pri (* slip 10)))))
270 pri))
272 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
273 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
274 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
275 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
277 Habits are assigned colors on the following basis:
278 Blue Task is before the scheduled date.
279 Green Task is on or after scheduled date, but before the
280 end of the schedule's repeat period.
281 Yellow If the task has a deadline, then it is after schedule's
282 repeat period, but before the deadline.
283 Orange The task has reached the deadline day, or if there is
284 no deadline, the end of the schedule's repeat period.
285 Red The task has gone beyond the deadline day or the
286 schedule's repeat period."
287 (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
288 (s-repeat (org-habit-scheduled-repeat habit))
289 (d-repeat (org-habit-deadline-repeat habit))
290 (deadline (if scheduled-days
291 (+ scheduled-days (- d-repeat s-repeat))
292 (org-habit-deadline habit)))
293 (m-days (or now-days (time-to-days (current-time)))))
294 (cond
295 ((< m-days scheduled)
296 '(org-habit-clear-face . org-habit-clear-future-face))
297 ((< m-days deadline)
298 '(org-habit-ready-face . org-habit-ready-future-face))
299 ((= m-days deadline)
300 (if donep
301 '(org-habit-ready-face . org-habit-ready-future-face)
302 '(org-habit-alert-face . org-habit-alert-future-face)))
303 ((and org-habit-show-done-always-green donep)
304 '(org-habit-ready-face . org-habit-ready-future-face))
305 (t '(org-habit-overdue-face . org-habit-overdue-future-face)))))
307 (defun org-habit-build-graph (habit starting current ending)
308 "Build a graph for the given HABIT, from STARTING to ENDING.
309 CURRENT gives the current time between STARTING and ENDING, for
310 the purpose of drawing the graph. It need not be the actual
311 current time."
312 (let* ((all-done-dates (sort (org-habit-done-dates habit) #'<))
313 (done-dates all-done-dates)
314 (scheduled (org-habit-scheduled habit))
315 (s-repeat (org-habit-scheduled-repeat habit))
316 (start (time-to-days starting))
317 (now (time-to-days current))
318 (end (time-to-days ending))
319 (graph (make-string (1+ (- end start)) ?\s))
320 (index 0)
321 last-done-date)
322 (while (and done-dates (< (car done-dates) start))
323 (setq last-done-date (car done-dates)
324 done-dates (cdr done-dates)))
325 (while (< start end)
326 (let* ((in-the-past-p (< start now))
327 (todayp (= start now))
328 (donep (and done-dates (= start (car done-dates))))
329 (faces
330 (if (and in-the-past-p
331 (not last-done-date)
332 (not (< scheduled now)))
333 '(org-habit-clear-face . org-habit-clear-future-face)
334 (org-habit-get-faces
335 habit start
336 (and in-the-past-p
337 last-done-date
338 ;; Compute scheduled time for habit at the time
339 ;; START was current.
340 (let ((type (org-habit-repeat-type habit)))
341 (cond
342 ;; At the last done date, use current
343 ;; scheduling in all cases.
344 ((null done-dates) scheduled)
345 ((equal type ".+") (+ last-done-date s-repeat))
346 ((equal type "+")
347 ;; Since LAST-DONE-DATE, each done mark
348 ;; shifted scheduled date by S-REPEAT.
349 (- scheduled (* (length done-dates) s-repeat)))
351 ;; Compute the scheduled time after the
352 ;; first repeat. This is the closest time
353 ;; past FIRST-DONE which can reach SCHEDULED
354 ;; by a number of S-REPEAT hops.
356 ;; Then, play TODO state change history from
357 ;; the beginning in order to find current
358 ;; scheduled time.
359 (let* ((first-done (car all-done-dates))
360 (s (let ((shift (mod (- scheduled first-done)
361 s-repeat)))
362 (+ (if (= shift 0) s-repeat shift)
363 first-done))))
364 (if (= first-done last-done-date) s
365 (catch :exit
366 (dolist (done (cdr all-done-dates) s)
367 ;; Each repeat shifts S by any
368 ;; number of S-REPEAT hops it takes
369 ;; to get past DONE, with a minimum
370 ;; of one hop.
371 (incf s
372 (* (1+ (/ (max (- done s) 0) s-repeat))
373 s-repeat))
374 (when (= done last-done-date)
375 (throw :exit s))))))))))
376 donep)))
377 markedp face)
378 (if donep
379 (let ((done-time (time-add
380 starting
381 (days-to-time
382 (- start (time-to-days starting))))))
384 (aset graph index org-habit-completed-glyph)
385 (setq markedp t)
386 (put-text-property
387 index (1+ index) 'help-echo
388 (format-time-string (org-time-stamp-format) done-time) graph)
389 (while (and done-dates
390 (= start (car done-dates)))
391 (setq last-done-date (car done-dates)
392 done-dates (cdr done-dates))))
393 (if todayp
394 (aset graph index org-habit-today-glyph)))
395 (setq face (if (or in-the-past-p todayp)
396 (car faces)
397 (cdr faces)))
398 (if (and in-the-past-p
399 (not (eq face 'org-habit-overdue-face))
400 (not markedp))
401 (setq face (cdr faces)))
402 (put-text-property index (1+ index) 'face face graph))
403 (setq start (1+ start)
404 index (1+ index)))
405 graph))
407 (defun org-habit-insert-consistency-graphs (&optional line)
408 "Insert consistency graph for any habitual tasks."
409 (let ((inhibit-read-only t)
410 (buffer-invisibility-spec '(org-link))
411 (moment (time-subtract (current-time)
412 (list 0 (* 3600 org-extend-today-until) 0))))
413 (save-excursion
414 (goto-char (if line (point-at-bol) (point-min)))
415 (while (not (eobp))
416 (let ((habit (get-text-property (point) 'org-habit-p)))
417 (when habit
418 (move-to-column org-habit-graph-column t)
419 (delete-char (min (+ 1 org-habit-preceding-days
420 org-habit-following-days)
421 (- (line-end-position) (point))))
422 (insert-before-markers
423 (org-habit-build-graph
424 habit
425 (time-subtract moment (days-to-time org-habit-preceding-days))
426 moment
427 (time-add moment (days-to-time org-habit-following-days))))))
428 (forward-line)))))
430 (defun org-habit-toggle-habits ()
431 "Toggle display of habits in an agenda buffer."
432 (interactive)
433 (org-agenda-check-type t 'agenda)
434 (setq org-habit-show-habits (not org-habit-show-habits))
435 (org-agenda-redo)
436 (org-agenda-set-mode-name)
437 (message "Habits turned %s"
438 (if org-habit-show-habits "on" "off")))
440 (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
442 (provide 'org-habit)
444 ;;; org-habit.el ends here