1 ;;; org-habit.el --- The habit tracking code for Org -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2019 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw at gnu dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: https://orgmode.org
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 <https://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;; This file contains the habit tracking code for Org mode
35 (defgroup org-habit nil
36 "Options concerning habit tracking in Org mode."
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."
46 (defcustom org-habit-preceding-days
21
47 "Number of days before today to appear in consistency graphs."
51 (defcustom org-habit-following-days
7
52 "Number of days after today to appear in consistency graphs."
56 (defcustom org-habit-show-habits t
57 "If non-nil, show habits in agenda buffers."
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."
68 (defcustom org-habit-show-all-today nil
69 "If non-nil, will show the consistency graph of all habits on
70 today's agenda, even if they are not scheduled."
74 (defcustom org-habit-today-glyph ?
!
75 "Glyph character used to identify today."
80 (defcustom org-habit-completed-glyph ?
*
81 "Glyph character used to show completed days on which a task was done."
86 (defcustom org-habit-show-done-always-green nil
87 "Non-nil means DONE days will always be green in the consistency graph.
88 It will be green even if it was done after the deadline."
92 (defcustom org-habit-scheduled-past-days nil
93 "Value to use instead of `org-scheduled-past-days', for habits only.
95 If nil, `org-scheduled-past-days' is used.
97 Setting this to say 10000 is a way to make habits always show up
98 as a reminder, even if you set `org-scheduled-past-days' to a
99 small value because you regard scheduled items as a way of
100 \"turning on\" TODO items on a particular date, rather than as a
101 means of creating calendar-based reminders."
103 :type
'(choice integer
(const nil
))
104 :package-version
'(Org .
"9.3")
105 :safe
(lambda (v) (or (integerp v
) (null v
))))
107 (defface org-habit-clear-face
108 '((((background light
)) (:background
"#8270f9"))
109 (((background dark
)) (:background
"blue")))
110 "Face for days on which a task shouldn't be done yet."
113 (defface org-habit-clear-future-face
114 '((((background light
)) (:background
"#d6e4fc"))
115 (((background dark
)) (:background
"midnightblue")))
116 "Face for future days on which a task shouldn't be done yet."
120 (defface org-habit-ready-face
121 '((((background light
)) (:background
"#4df946"))
122 (((background dark
)) (:background
"forestgreen")))
123 "Face for days on which a task should start to be done."
126 (defface org-habit-ready-future-face
127 '((((background light
)) (:background
"#acfca9"))
128 (((background dark
)) (:background
"darkgreen")))
129 "Face for days on which a task should start to be done."
133 (defface org-habit-alert-face
134 '((((background light
)) (:background
"#f5f946"))
135 (((background dark
)) (:background
"gold")))
136 "Face for days on which a task is due."
139 (defface org-habit-alert-future-face
140 '((((background light
)) (:background
"#fafca9"))
141 (((background dark
)) (:background
"darkgoldenrod")))
142 "Face for days on which a task is due."
146 (defface org-habit-overdue-face
147 '((((background light
)) (:background
"#f9372d"))
148 (((background dark
)) (:background
"firebrick")))
149 "Face for days on which a task is overdue."
152 (defface org-habit-overdue-future-face
153 '((((background light
)) (:background
"#fc9590"))
154 (((background dark
)) (:background
"darkred")))
155 "Face for days on which a task is overdue."
159 (defun org-habit-duration-to-days (ts)
160 (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts
)
161 ;; lead time is specified.
162 (floor (* (string-to-number (match-string 1 ts
))
163 (cdr (assoc (match-string 2 ts
)
164 '(("d" .
1) ("w" .
7)
165 ("m" .
30.4) ("y" .
365.25))))))
166 (error "Invalid duration string: %s" ts
)))
168 (defun org-is-habit-p (&optional pom
)
169 "Is the task at POM or point a habit?"
170 (string= "habit" (org-entry-get (or pom
(point)) "STYLE")))
172 (defun org-habit-parse-todo (&optional pom
)
173 "Parse the TODO surrounding point for its habit-related data.
174 Returns a list with the following elements:
176 0: Scheduled date for the habit (may be in the past)
177 1: \".+\"-style repeater for the schedule, in days
178 2: Optional deadline (nil if not present)
179 3: If deadline, the repeater for the deadline, otherwise nil
180 4: A list of all the past dates this todo was mark closed
181 5: Repeater type as a string
183 This list represents a \"habit\" for the rest of this module."
185 (if pom
(goto-char pom
))
186 (cl-assert (org-is-habit-p (point)))
187 (let* ((scheduled (org-get-scheduled-time (point)))
188 (scheduled-repeat (org-get-repeat (org-entry-get (point) "SCHEDULED")))
189 (end (org-entry-end-position))
190 (habit-entry (org-no-properties (nth 4 (org-heading-components))))
191 closed-dates deadline dr-days sr-days sr-type
)
193 (setq scheduled
(time-to-days scheduled
))
194 (error "Habit %s has no scheduled date" habit-entry
))
195 (unless scheduled-repeat
197 "Habit `%s' has no scheduled repeat period or has an incorrect one"
199 (setq sr-days
(org-habit-duration-to-days scheduled-repeat
)
200 sr-type
(progn (string-match "[\\.+]?\\+" scheduled-repeat
)
201 (match-string-no-properties 0 scheduled-repeat
)))
202 (unless (> sr-days
0)
203 (error "Habit %s scheduled repeat period is less than 1d" habit-entry
))
204 (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat
)
205 (setq dr-days
(org-habit-duration-to-days
206 (match-string-no-properties 1 scheduled-repeat
)))
207 (if (<= dr-days sr-days
)
208 (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
209 habit-entry scheduled-repeat
))
210 (setq deadline
(+ scheduled
(- dr-days sr-days
))))
211 (org-back-to-heading t
)
212 (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days
))
213 (reversed org-log-states-order-reversed
)
214 (search (if reversed
're-search-forward
're-search-backward
))
215 (limit (if reversed end
(point)))
218 "^[ \t]*-[ \t]+\\(?:State \"%s\".*%s%s\\)"
219 (regexp-opt org-done-keywords
)
220 org-ts-regexp-inactive
221 (let ((value (cdr (assq 'done org-log-note-headings
))))
226 `(("%d" .
,org-ts-regexp-inactive
)
227 ("%D" .
,org-ts-regexp
)
230 ("%t" .
,org-ts-regexp-inactive
)
231 ("%T" .
,org-ts-regexp
)
233 ("%U" .
".*?")))))))))
234 (unless reversed
(goto-char end
))
235 (while (and (< count maxdays
) (funcall search re limit t
))
237 (org-time-string-to-time
238 (or (match-string-no-properties 1)
239 (match-string-no-properties 2))))
241 (setq count
(1+ count
))))
242 (list scheduled sr-days deadline dr-days closed-dates sr-type
))))
244 (defsubst org-habit-scheduled
(habit)
246 (defsubst org-habit-scheduled-repeat
(habit)
248 (defsubst org-habit-deadline
(habit)
249 (let ((deadline (nth 2 habit
)))
252 (+ (org-habit-scheduled habit
)
253 (1- (org-habit-scheduled-repeat habit
)))
254 (org-habit-scheduled habit
)))))
255 (defsubst org-habit-deadline-repeat
(habit)
257 (org-habit-scheduled-repeat habit
)))
258 (defsubst org-habit-done-dates
(habit)
260 (defsubst org-habit-repeat-type
(habit)
263 (defsubst org-habit-get-priority
(habit &optional moment
)
264 "Determine the relative priority of a habit.
265 This must take into account not just urgency, but consistency as well."
267 (now (if moment
(time-to-days moment
) (org-today)))
268 (scheduled (org-habit-scheduled habit
))
269 (deadline (org-habit-deadline habit
)))
270 ;; add 10 for every day past the scheduled date, and subtract for every
272 (setq pri
(+ pri
(* (- now scheduled
) 10)))
273 ;; add 50 if the deadline is today
274 (if (and (/= scheduled deadline
)
276 (setq pri
(+ pri
50)))
277 ;; add 100 for every day beyond the deadline date, and subtract 10 for
278 ;; every day before it
279 (let ((slip (- now
(1- deadline
))))
281 (setq pri
(+ pri
(* slip
100)))
282 (setq pri
(+ pri
(* slip
10)))))
285 (defun org-habit-get-faces (habit &optional now-days scheduled-days donep
)
286 "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
287 NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
288 SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
290 Habits are assigned colors on the following basis:
291 Blue Task is before the scheduled date.
292 Green Task is on or after scheduled date, but before the
293 end of the schedule's repeat period.
294 Yellow If the task has a deadline, then it is after schedule's
295 repeat period, but before the deadline.
296 Orange The task has reached the deadline day, or if there is
297 no deadline, the end of the schedule's repeat period.
298 Red The task has gone beyond the deadline day or the
299 schedule's repeat period."
300 (let* ((scheduled (or scheduled-days
(org-habit-scheduled habit
)))
301 (s-repeat (org-habit-scheduled-repeat habit
))
302 (d-repeat (org-habit-deadline-repeat habit
))
303 (deadline (if scheduled-days
304 (+ scheduled-days
(- d-repeat s-repeat
))
305 (org-habit-deadline habit
)))
306 (m-days (or now-days
(time-to-days nil
))))
308 ((< m-days scheduled
)
309 '(org-habit-clear-face . org-habit-clear-future-face
))
311 '(org-habit-ready-face . org-habit-ready-future-face
))
314 '(org-habit-ready-face . org-habit-ready-future-face
)
315 '(org-habit-alert-face . org-habit-alert-future-face
)))
316 ((and org-habit-show-done-always-green donep
)
317 '(org-habit-ready-face . org-habit-ready-future-face
))
318 (t '(org-habit-overdue-face . org-habit-overdue-future-face
)))))
320 (defun org-habit-build-graph (habit starting current ending
)
321 "Build a graph for the given HABIT, from STARTING to ENDING.
322 CURRENT gives the current time between STARTING and ENDING, for
323 the purpose of drawing the graph. It need not be the actual
325 (let* ((all-done-dates (sort (org-habit-done-dates habit
) #'<))
326 (done-dates all-done-dates
)
327 (scheduled (org-habit-scheduled habit
))
328 (s-repeat (org-habit-scheduled-repeat habit
))
329 (start (time-to-days starting
))
330 (now (time-to-days current
))
331 (end (time-to-days ending
))
332 (graph (make-string (1+ (- end start
)) ?\s
))
335 (while (and done-dates
(< (car done-dates
) start
))
336 (setq last-done-date
(car done-dates
)
337 done-dates
(cdr done-dates
)))
339 (let* ((in-the-past-p (< start now
))
340 (todayp (= start now
))
341 (donep (and done-dates
(= start
(car done-dates
))))
343 (if (and in-the-past-p
345 (not (< scheduled now
)))
346 '(org-habit-clear-face . org-habit-clear-future-face
)
351 ;; Compute scheduled time for habit at the time
352 ;; START was current.
353 (let ((type (org-habit-repeat-type habit
)))
355 ;; At the last done date, use current
356 ;; scheduling in all cases.
357 ((null done-dates
) scheduled
)
358 ((equal type
".+") (+ last-done-date s-repeat
))
360 ;; Since LAST-DONE-DATE, each done mark
361 ;; shifted scheduled date by S-REPEAT.
362 (- scheduled
(* (length done-dates
) s-repeat
)))
364 ;; Compute the scheduled time after the
365 ;; first repeat. This is the closest time
366 ;; past FIRST-DONE which can reach SCHEDULED
367 ;; by a number of S-REPEAT hops.
369 ;; Then, play TODO state change history from
370 ;; the beginning in order to find current
372 (let* ((first-done (car all-done-dates
))
373 (s (let ((shift (mod (- scheduled first-done
)
375 (+ (if (= shift
0) s-repeat shift
)
377 (if (= first-done last-done-date
) s
379 (dolist (done (cdr all-done-dates
) s
)
380 ;; Each repeat shifts S by any
381 ;; number of S-REPEAT hops it takes
382 ;; to get past DONE, with a minimum
384 (cl-incf s
(* (1+ (/ (max (- done s
) 0)
387 (when (= done last-done-date
)
388 (throw :exit s
))))))))))
393 (aset graph index org-habit-completed-glyph
)
395 (while (and done-dates
(= start
(car done-dates
)))
396 (setq last-done-date
(car done-dates
))
397 (setq done-dates
(cdr done-dates
))))
399 (aset graph index org-habit-today-glyph
)))
400 (setq face
(if (or in-the-past-p todayp
)
403 (when (and in-the-past-p
404 (not (eq face
'org-habit-overdue-face
))
406 (setq face
(cdr faces
)))
407 (put-text-property index
(1+ index
) 'face face graph
)
408 (put-text-property index
(1+ index
)
410 (concat (format-time-string
411 (org-time-stamp-format)
412 (time-add starting
(days-to-time (- start
(time-to-days starting
)))))
413 (if donep
" DONE" ""))
415 (setq start
(1+ start
)
419 (defun org-habit-insert-consistency-graphs (&optional line
)
420 "Insert consistency graph for any habitual tasks."
421 (let ((inhibit-read-only t
)
422 (buffer-invisibility-spec '(org-link))
423 (moment (time-subtract (current-time)
424 (list 0 (* 3600 org-extend-today-until
) 0))))
426 (goto-char (if line
(point-at-bol) (point-min)))
428 (let ((habit (get-text-property (point) 'org-habit-p
)))
430 (move-to-column org-habit-graph-column t
)
431 (delete-char (min (+ 1 org-habit-preceding-days
432 org-habit-following-days
)
433 (- (line-end-position) (point))))
434 (insert-before-markers
435 (org-habit-build-graph
437 (time-subtract moment
(days-to-time org-habit-preceding-days
))
439 (time-add moment
(days-to-time org-habit-following-days
))))))
442 (defun org-habit-toggle-habits ()
443 "Toggle display of habits in an agenda buffer."
445 (org-agenda-check-type t
'agenda
)
446 (setq org-habit-show-habits
(not org-habit-show-habits
))
448 (org-agenda-set-mode-name)
449 (message "Habits turned %s"
450 (if org-habit-show-habits
"on" "off")))
452 (defun org-habit-toggle-display-in-agenda (arg)
453 "Toggle display of habits in agenda.
454 With ARG toggle display of all vs. undone scheduled habits.
455 See `org-habit-show-all-today'."
458 (org-habit-toggle-habits)
459 (org-agenda-check-type t
'agenda
)
460 (setq org-habit-show-all-today
(not org-habit-show-all-today
))
461 (when org-habit-show-habits
(org-agenda-redo))))
463 (org-defkey org-agenda-mode-map
"K" 'org-habit-toggle-display-in-agenda
)
467 ;;; org-habit.el ends here