org-habit.el 17 KB

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