org-habit.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. ;;; org-habit.el --- The habit tracking code for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2019 Free Software Foundation, Inc.
  3. ;; Author: John Wiegley <johnw at gnu dot org>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: https://orgmode.org
  6. ;;
  7. ;; This file is part of GNU Emacs.
  8. ;;
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20. ;;
  21. ;;; Commentary:
  22. ;; This file contains the habit tracking code for Org mode
  23. ;;; Code:
  24. (require 'cl-lib)
  25. (require 'org)
  26. (require 'org-agenda)
  27. (defgroup org-habit nil
  28. "Options concerning habit tracking in Org mode."
  29. :tag "Org Habit"
  30. :group 'org-progress)
  31. (defcustom org-habit-graph-column 40
  32. "The absolute column at which to insert habit consistency graphs.
  33. Note that consistency graphs will overwrite anything else in the buffer."
  34. :group 'org-habit
  35. :type 'integer)
  36. (defcustom org-habit-preceding-days 21
  37. "Number of days before today to appear in consistency graphs."
  38. :group 'org-habit
  39. :type 'integer)
  40. (defcustom org-habit-following-days 7
  41. "Number of days after today to appear in consistency graphs."
  42. :group 'org-habit
  43. :type 'integer)
  44. (defcustom org-habit-show-habits t
  45. "If non-nil, show habits in agenda buffers."
  46. :group 'org-habit
  47. :type 'boolean)
  48. (defcustom org-habit-show-habits-only-for-today t
  49. "If non-nil, only show habits on today's agenda, and not for future days.
  50. Note that even when shown for future days, the graph is always
  51. relative to the current effective date."
  52. :group 'org-habit
  53. :type 'boolean)
  54. (defcustom org-habit-show-all-today nil
  55. "If non-nil, will show the consistency graph of all habits on
  56. today's agenda, even if they are not scheduled."
  57. :group 'org-habit
  58. :type 'boolean)
  59. (defcustom org-habit-today-glyph ?!
  60. "Glyph character used to identify today."
  61. :group 'org-habit
  62. :version "24.1"
  63. :type 'character)
  64. (defcustom org-habit-completed-glyph ?*
  65. "Glyph character used to show completed days on which a task was done."
  66. :group 'org-habit
  67. :version "24.1"
  68. :type 'character)
  69. (defcustom org-habit-show-done-always-green nil
  70. "Non-nil means DONE days will always be green in the consistency graph.
  71. It will be green even if it was done after the deadline."
  72. :group 'org-habit
  73. :type 'boolean)
  74. (defcustom org-habit-scheduled-past-days nil
  75. "Value to use instead of `org-scheduled-past-days', for habits only.
  76. If nil, `org-scheduled-past-days' is used.
  77. Setting this to say 10000 is a way to make habits always show up
  78. as a reminder, even if you set `org-scheduled-past-days' to a
  79. small value because you regard scheduled items as a way of
  80. \"turning on\" TODO items on a particular date, rather than as a
  81. means of creating calendar-based reminders."
  82. :group 'org-habit
  83. :type '(choice integer (const nil))
  84. :package-version '(Org . "9.3")
  85. :safe (lambda (v) (or (integerp v) (null v))))
  86. (defface org-habit-clear-face
  87. '((((background light)) (:background "#8270f9"))
  88. (((background dark)) (:background "blue")))
  89. "Face for days on which a task shouldn't be done yet."
  90. :group 'org-habit
  91. :group 'org-faces)
  92. (defface org-habit-clear-future-face
  93. '((((background light)) (:background "#d6e4fc"))
  94. (((background dark)) (:background "midnightblue")))
  95. "Face for future days on which a task shouldn't be done yet."
  96. :group 'org-habit
  97. :group 'org-faces)
  98. (defface org-habit-ready-face
  99. '((((background light)) (:background "#4df946"))
  100. (((background dark)) (:background "forestgreen")))
  101. "Face for days on which a task should start to be done."
  102. :group 'org-habit
  103. :group 'org-faces)
  104. (defface org-habit-ready-future-face
  105. '((((background light)) (:background "#acfca9"))
  106. (((background dark)) (:background "darkgreen")))
  107. "Face for days on which a task should start to be done."
  108. :group 'org-habit
  109. :group 'org-faces)
  110. (defface org-habit-alert-face
  111. '((((background light)) (:background "#f5f946"))
  112. (((background dark)) (:background "gold")))
  113. "Face for days on which a task is due."
  114. :group 'org-habit
  115. :group 'org-faces)
  116. (defface org-habit-alert-future-face
  117. '((((background light)) (:background "#fafca9"))
  118. (((background dark)) (:background "darkgoldenrod")))
  119. "Face for days on which a task is due."
  120. :group 'org-habit
  121. :group 'org-faces)
  122. (defface org-habit-overdue-face
  123. '((((background light)) (:background "#f9372d"))
  124. (((background dark)) (:background "firebrick")))
  125. "Face for days on which a task is overdue."
  126. :group 'org-habit
  127. :group 'org-faces)
  128. (defface org-habit-overdue-future-face
  129. '((((background light)) (:background "#fc9590"))
  130. (((background dark)) (:background "darkred")))
  131. "Face for days on which a task is overdue."
  132. :group 'org-habit
  133. :group 'org-faces)
  134. (defun org-habit-duration-to-days (ts)
  135. (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
  136. ;; lead time is specified.
  137. (floor (* (string-to-number (match-string 1 ts))
  138. (cdr (assoc (match-string 2 ts)
  139. '(("d" . 1) ("w" . 7)
  140. ("m" . 30.4) ("y" . 365.25))))))
  141. (error "Invalid duration string: %s" ts)))
  142. (defun org-is-habit-p (&optional pom)
  143. "Is the task at POM or point a habit?"
  144. (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
  145. (defun org-habit-parse-todo (&optional pom)
  146. "Parse the TODO surrounding point for its habit-related data.
  147. Returns a list with the following elements:
  148. 0: Scheduled date for the habit (may be in the past)
  149. 1: \".+\"-style repeater for the schedule, in days
  150. 2: Optional deadline (nil if not present)
  151. 3: If deadline, the repeater for the deadline, otherwise nil
  152. 4: A list of all the past dates this todo was mark closed
  153. 5: Repeater type as a string
  154. This list represents a \"habit\" for the rest of this module."
  155. (save-excursion
  156. (if pom (goto-char pom))
  157. (cl-assert (org-is-habit-p (point)))
  158. (let* ((scheduled (org-get-scheduled-time (point)))
  159. (scheduled-repeat (org-get-repeat (org-entry-get (point) "SCHEDULED")))
  160. (end (org-entry-end-position))
  161. (habit-entry (org-no-properties (nth 4 (org-heading-components))))
  162. closed-dates deadline dr-days sr-days sr-type)
  163. (if scheduled
  164. (setq scheduled (time-to-days scheduled))
  165. (error "Habit %s has no scheduled date" habit-entry))
  166. (unless scheduled-repeat
  167. (error
  168. "Habit `%s' has no scheduled repeat period or has an incorrect one"
  169. habit-entry))
  170. (setq sr-days (org-habit-duration-to-days scheduled-repeat)
  171. sr-type (progn (string-match "[\\.+]?\\+" scheduled-repeat)
  172. (match-string-no-properties 0 scheduled-repeat)))
  173. (unless (> sr-days 0)
  174. (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
  175. (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
  176. (setq dr-days (org-habit-duration-to-days
  177. (match-string-no-properties 1 scheduled-repeat)))
  178. (if (<= dr-days sr-days)
  179. (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
  180. habit-entry scheduled-repeat))
  181. (setq deadline (+ scheduled (- dr-days sr-days))))
  182. (org-back-to-heading t)
  183. (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
  184. (reversed org-log-states-order-reversed)
  185. (search (if reversed 're-search-forward 're-search-backward))
  186. (limit (if reversed end (point)))
  187. (count 0)
  188. (re (format
  189. "^[ \t]*-[ \t]+\\(?:State \"%s\".*%s%s\\)"
  190. (regexp-opt org-done-keywords)
  191. org-ts-regexp-inactive
  192. (let ((value (cdr (assq 'done org-log-note-headings))))
  193. (if (not value) ""
  194. (concat "\\|"
  195. (org-replace-escapes
  196. (regexp-quote value)
  197. `(("%d" . ,org-ts-regexp-inactive)
  198. ("%D" . ,org-ts-regexp)
  199. ("%s" . "\"\\S-+\"")
  200. ("%S" . "\"\\S-+\"")
  201. ("%t" . ,org-ts-regexp-inactive)
  202. ("%T" . ,org-ts-regexp)
  203. ("%u" . ".*?")
  204. ("%U" . ".*?")))))))))
  205. (unless reversed (goto-char end))
  206. (while (and (< count maxdays) (funcall search re limit t))
  207. (push (time-to-days
  208. (org-time-string-to-time
  209. (or (match-string-no-properties 1)
  210. (match-string-no-properties 2))))
  211. closed-dates)
  212. (setq count (1+ count))))
  213. (list scheduled sr-days deadline dr-days closed-dates sr-type))))
  214. (defsubst org-habit-scheduled (habit)
  215. (nth 0 habit))
  216. (defsubst org-habit-scheduled-repeat (habit)
  217. (nth 1 habit))
  218. (defsubst org-habit-deadline (habit)
  219. (let ((deadline (nth 2 habit)))
  220. (or deadline
  221. (if (nth 3 habit)
  222. (+ (org-habit-scheduled habit)
  223. (1- (org-habit-scheduled-repeat habit)))
  224. (org-habit-scheduled habit)))))
  225. (defsubst org-habit-deadline-repeat (habit)
  226. (or (nth 3 habit)
  227. (org-habit-scheduled-repeat habit)))
  228. (defsubst org-habit-done-dates (habit)
  229. (nth 4 habit))
  230. (defsubst org-habit-repeat-type (habit)
  231. (nth 5 habit))
  232. (defsubst org-habit-get-priority (habit &optional moment)
  233. "Determine the relative priority of a habit.
  234. This must take into account not just urgency, but consistency as well."
  235. (let ((pri 1000)
  236. (now (if moment (time-to-days moment) (org-today)))
  237. (scheduled (org-habit-scheduled habit))
  238. (deadline (org-habit-deadline habit)))
  239. ;; add 10 for every day past the scheduled date, and subtract for every
  240. ;; day before it
  241. (setq pri (+ pri (* (- now scheduled) 10)))
  242. ;; add 50 if the deadline is today
  243. (if (and (/= scheduled deadline)
  244. (= now deadline))
  245. (setq pri (+ pri 50)))
  246. ;; add 100 for every day beyond the deadline date, and subtract 10 for
  247. ;; every day before it
  248. (let ((slip (- now (1- deadline))))
  249. (if (> slip 0)
  250. (setq pri (+ pri (* slip 100)))
  251. (setq pri (+ pri (* slip 10)))))
  252. pri))
  253. (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
  254. "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
  255. NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
  256. SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
  257. Habits are assigned colors on the following basis:
  258. Blue Task is before the scheduled date.
  259. Green Task is on or after scheduled date, but before the
  260. end of the schedule's repeat period.
  261. Yellow If the task has a deadline, then it is after schedule's
  262. repeat period, but before the deadline.
  263. Orange The task has reached the deadline day, or if there is
  264. no deadline, the end of the schedule's repeat period.
  265. Red The task has gone beyond the deadline day or the
  266. schedule's repeat period."
  267. (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
  268. (s-repeat (org-habit-scheduled-repeat habit))
  269. (d-repeat (org-habit-deadline-repeat habit))
  270. (deadline (if scheduled-days
  271. (+ scheduled-days (- d-repeat s-repeat))
  272. (org-habit-deadline habit)))
  273. (m-days (or now-days (time-to-days nil))))
  274. (cond
  275. ((< m-days scheduled)
  276. '(org-habit-clear-face . org-habit-clear-future-face))
  277. ((< m-days deadline)
  278. '(org-habit-ready-face . org-habit-ready-future-face))
  279. ((= m-days deadline)
  280. (if donep
  281. '(org-habit-ready-face . org-habit-ready-future-face)
  282. '(org-habit-alert-face . org-habit-alert-future-face)))
  283. ((and org-habit-show-done-always-green donep)
  284. '(org-habit-ready-face . org-habit-ready-future-face))
  285. (t '(org-habit-overdue-face . org-habit-overdue-future-face)))))
  286. (defun org-habit-build-graph (habit starting current ending)
  287. "Build a graph for the given HABIT, from STARTING to ENDING.
  288. CURRENT gives the current time between STARTING and ENDING, for
  289. the purpose of drawing the graph. It need not be the actual
  290. current time."
  291. (let* ((all-done-dates (sort (org-habit-done-dates habit) #'<))
  292. (done-dates all-done-dates)
  293. (scheduled (org-habit-scheduled habit))
  294. (s-repeat (org-habit-scheduled-repeat habit))
  295. (start (time-to-days starting))
  296. (now (time-to-days current))
  297. (end (time-to-days ending))
  298. (graph (make-string (1+ (- end start)) ?\s))
  299. (index 0)
  300. last-done-date)
  301. (while (and done-dates (< (car done-dates) start))
  302. (setq last-done-date (car done-dates)
  303. done-dates (cdr done-dates)))
  304. (while (< start end)
  305. (let* ((in-the-past-p (< start now))
  306. (todayp (= start now))
  307. (donep (and done-dates (= start (car done-dates))))
  308. (faces
  309. (if (and in-the-past-p
  310. (not last-done-date)
  311. (not (< scheduled now)))
  312. '(org-habit-clear-face . org-habit-clear-future-face)
  313. (org-habit-get-faces
  314. habit start
  315. (and in-the-past-p
  316. last-done-date
  317. ;; Compute scheduled time for habit at the time
  318. ;; START was current.
  319. (let ((type (org-habit-repeat-type habit)))
  320. (cond
  321. ;; At the last done date, use current
  322. ;; scheduling in all cases.
  323. ((null done-dates) scheduled)
  324. ((equal type ".+") (+ last-done-date s-repeat))
  325. ((equal type "+")
  326. ;; Since LAST-DONE-DATE, each done mark
  327. ;; shifted scheduled date by S-REPEAT.
  328. (- scheduled (* (length done-dates) s-repeat)))
  329. (t
  330. ;; Compute the scheduled time after the
  331. ;; first repeat. This is the closest time
  332. ;; past FIRST-DONE which can reach SCHEDULED
  333. ;; by a number of S-REPEAT hops.
  334. ;;
  335. ;; Then, play TODO state change history from
  336. ;; the beginning in order to find current
  337. ;; scheduled time.
  338. (let* ((first-done (car all-done-dates))
  339. (s (let ((shift (mod (- scheduled first-done)
  340. s-repeat)))
  341. (+ (if (= shift 0) s-repeat shift)
  342. first-done))))
  343. (if (= first-done last-done-date) s
  344. (catch :exit
  345. (dolist (done (cdr all-done-dates) s)
  346. ;; Each repeat shifts S by any
  347. ;; number of S-REPEAT hops it takes
  348. ;; to get past DONE, with a minimum
  349. ;; of one hop.
  350. (cl-incf s (* (1+ (/ (max (- done s) 0)
  351. s-repeat))
  352. s-repeat))
  353. (when (= done last-done-date)
  354. (throw :exit s))))))))))
  355. donep)))
  356. markedp face)
  357. (cond
  358. (donep
  359. (aset graph index org-habit-completed-glyph)
  360. (setq markedp t)
  361. (while (and done-dates (= start (car done-dates)))
  362. (setq last-done-date (car done-dates))
  363. (setq done-dates (cdr done-dates))))
  364. (todayp
  365. (aset graph index org-habit-today-glyph)))
  366. (setq face (if (or in-the-past-p todayp)
  367. (car faces)
  368. (cdr faces)))
  369. (when (and in-the-past-p
  370. (not (eq face 'org-habit-overdue-face))
  371. (not markedp))
  372. (setq face (cdr faces)))
  373. (put-text-property index (1+ index) 'face face graph)
  374. (put-text-property index (1+ index)
  375. 'help-echo
  376. (concat (format-time-string
  377. (org-time-stamp-format)
  378. (time-add starting (days-to-time (- start (time-to-days starting)))))
  379. (if donep " DONE" ""))
  380. graph))
  381. (setq start (1+ start)
  382. index (1+ index)))
  383. graph))
  384. (defun org-habit-insert-consistency-graphs (&optional line)
  385. "Insert consistency graph for any habitual tasks."
  386. (let ((inhibit-read-only t)
  387. (buffer-invisibility-spec '(org-link))
  388. (moment (time-subtract (current-time)
  389. (list 0 (* 3600 org-extend-today-until) 0))))
  390. (save-excursion
  391. (goto-char (if line (point-at-bol) (point-min)))
  392. (while (not (eobp))
  393. (let ((habit (get-text-property (point) 'org-habit-p)))
  394. (when habit
  395. (move-to-column org-habit-graph-column t)
  396. (delete-char (min (+ 1 org-habit-preceding-days
  397. org-habit-following-days)
  398. (- (line-end-position) (point))))
  399. (insert-before-markers
  400. (org-habit-build-graph
  401. habit
  402. (time-subtract moment (days-to-time org-habit-preceding-days))
  403. moment
  404. (time-add moment (days-to-time org-habit-following-days))))))
  405. (forward-line)))))
  406. (defun org-habit-toggle-habits ()
  407. "Toggle display of habits in an agenda buffer."
  408. (interactive)
  409. (org-agenda-check-type t 'agenda)
  410. (setq org-habit-show-habits (not org-habit-show-habits))
  411. (org-agenda-redo)
  412. (org-agenda-set-mode-name)
  413. (message "Habits turned %s"
  414. (if org-habit-show-habits "on" "off")))
  415. (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
  416. (provide 'org-habit)
  417. ;;; org-habit.el ends here