org-habit.el 16 KB

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