org-habit.el 16 KB

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