org-habit.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. ;;; org-habit.el --- The habit tracking code for Org-mode
  2. ;; Copyright (C) 2009-2014 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. This list represents a \"habit\" for the rest of this module."
  143. (save-excursion
  144. (if pom (goto-char pom))
  145. (assert (org-is-habit-p (point)))
  146. (let* ((scheduled (org-get-scheduled-time (point)))
  147. (scheduled-repeat (org-get-repeat org-scheduled-string))
  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)
  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. (unless (> sr-days 0)
  160. (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
  161. (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
  162. (setq dr-days (org-habit-duration-to-days
  163. (match-string-no-properties 1 scheduled-repeat)))
  164. (if (<= dr-days sr-days)
  165. (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
  166. habit-entry scheduled-repeat))
  167. (setq deadline (+ scheduled (- dr-days sr-days))))
  168. (org-back-to-heading t)
  169. (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
  170. (reversed org-log-states-order-reversed)
  171. (search (if reversed 're-search-forward 're-search-backward))
  172. (limit (if reversed end (point)))
  173. (count 0))
  174. (unless reversed (goto-char end))
  175. (while (and (< count maxdays)
  176. (funcall search (format "- State \"%s\".*\\[\\([^]]+\\)\\]"
  177. (regexp-opt org-done-keywords))
  178. limit t))
  179. (push (time-to-days
  180. (org-time-string-to-time (match-string-no-properties 1)))
  181. closed-dates)
  182. (setq count (1+ count))))
  183. (list scheduled sr-days deadline dr-days closed-dates))))
  184. (defsubst org-habit-scheduled (habit)
  185. (nth 0 habit))
  186. (defsubst org-habit-scheduled-repeat (habit)
  187. (nth 1 habit))
  188. (defsubst org-habit-deadline (habit)
  189. (let ((deadline (nth 2 habit)))
  190. (or deadline
  191. (if (nth 3 habit)
  192. (+ (org-habit-scheduled habit)
  193. (1- (org-habit-scheduled-repeat habit)))
  194. (org-habit-scheduled habit)))))
  195. (defsubst org-habit-deadline-repeat (habit)
  196. (or (nth 3 habit)
  197. (org-habit-scheduled-repeat habit)))
  198. (defsubst org-habit-done-dates (habit)
  199. (nth 4 habit))
  200. (defsubst org-habit-get-priority (habit &optional moment)
  201. "Determine the relative priority of a habit.
  202. This must take into account not just urgency, but consistency as well."
  203. (let ((pri 1000)
  204. (now (if moment (time-to-days moment) (org-today)))
  205. (scheduled (org-habit-scheduled habit))
  206. (deadline (org-habit-deadline habit)))
  207. ;; add 10 for every day past the scheduled date, and subtract for every
  208. ;; day before it
  209. (setq pri (+ pri (* (- now scheduled) 10)))
  210. ;; add 50 if the deadline is today
  211. (if (and (/= scheduled deadline)
  212. (= now deadline))
  213. (setq pri (+ pri 50)))
  214. ;; add 100 for every day beyond the deadline date, and subtract 10 for
  215. ;; every day before it
  216. (let ((slip (- now (1- deadline))))
  217. (if (> slip 0)
  218. (setq pri (+ pri (* slip 100)))
  219. (setq pri (+ pri (* slip 10)))))
  220. pri))
  221. (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
  222. "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
  223. NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
  224. SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
  225. Habits are assigned colors on the following basis:
  226. Blue Task is before the scheduled date.
  227. Green Task is on or after scheduled date, but before the
  228. end of the schedule's repeat period.
  229. Yellow If the task has a deadline, then it is after schedule's
  230. repeat period, but before the deadline.
  231. Orange The task has reached the deadline day, or if there is
  232. no deadline, the end of the schedule's repeat period.
  233. Red The task has gone beyond the deadline day or the
  234. schedule's repeat period."
  235. (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
  236. (s-repeat (org-habit-scheduled-repeat habit))
  237. (scheduled-end (+ scheduled (1- s-repeat)))
  238. (d-repeat (org-habit-deadline-repeat habit))
  239. (deadline (if scheduled-days
  240. (+ scheduled-days (- d-repeat s-repeat))
  241. (org-habit-deadline habit)))
  242. (m-days (or now-days (time-to-days (current-time)))))
  243. (cond
  244. ((< m-days scheduled)
  245. '(org-habit-clear-face . org-habit-clear-future-face))
  246. ((< m-days deadline)
  247. '(org-habit-ready-face . org-habit-ready-future-face))
  248. ((= m-days deadline)
  249. (if donep
  250. '(org-habit-ready-face . org-habit-ready-future-face)
  251. '(org-habit-alert-face . org-habit-alert-future-face)))
  252. ((and org-habit-show-done-always-green donep)
  253. '(org-habit-ready-face . org-habit-ready-future-face))
  254. (t '(org-habit-overdue-face . org-habit-overdue-future-face)))))
  255. (defun org-habit-build-graph (habit starting current ending)
  256. "Build a graph for the given HABIT, from STARTING to ENDING.
  257. CURRENT gives the current time between STARTING and ENDING, for
  258. the purpose of drawing the graph. It need not be the actual
  259. current time."
  260. (let* ((done-dates (sort (org-habit-done-dates habit) '<))
  261. (scheduled (org-habit-scheduled habit))
  262. (s-repeat (org-habit-scheduled-repeat habit))
  263. (start (time-to-days starting))
  264. (now (time-to-days current))
  265. (end (time-to-days ending))
  266. (graph (make-string (1+ (- end start)) ?\ ))
  267. (index 0)
  268. last-done-date)
  269. (while (and done-dates (< (car done-dates) start))
  270. (setq last-done-date (car done-dates)
  271. done-dates (cdr done-dates)))
  272. (while (< start end)
  273. (let* ((in-the-past-p (< start now))
  274. (todayp (= start now))
  275. (donep (and done-dates
  276. (= start (car done-dates))))
  277. (faces (if (and in-the-past-p
  278. (not last-done-date)
  279. (not (< scheduled now)))
  280. '(org-habit-clear-face . org-habit-clear-future-face)
  281. (org-habit-get-faces
  282. habit start (and in-the-past-p
  283. (if last-done-date
  284. (+ last-done-date s-repeat)
  285. scheduled))
  286. donep)))
  287. markedp face)
  288. (if donep
  289. (let ((done-time (time-add
  290. starting
  291. (days-to-time
  292. (- start (time-to-days starting))))))
  293. (aset graph index org-habit-completed-glyph)
  294. (setq markedp t)
  295. (put-text-property
  296. index (1+ index) 'help-echo
  297. (format-time-string (org-time-stamp-format) done-time) graph)
  298. (while (and done-dates
  299. (= start (car done-dates)))
  300. (setq last-done-date (car done-dates)
  301. done-dates (cdr done-dates))))
  302. (if todayp
  303. (aset graph index org-habit-today-glyph)))
  304. (setq face (if (or in-the-past-p todayp)
  305. (car faces)
  306. (cdr faces)))
  307. (if (and in-the-past-p
  308. (not (eq face 'org-habit-overdue-face))
  309. (not markedp))
  310. (setq face (cdr faces)))
  311. (put-text-property index (1+ index) 'face face graph))
  312. (setq start (1+ start)
  313. index (1+ index)))
  314. graph))
  315. (defun org-habit-insert-consistency-graphs (&optional line)
  316. "Insert consistency graph for any habitual tasks."
  317. (let ((inhibit-read-only t) l c
  318. (buffer-invisibility-spec '(org-link))
  319. (moment (time-subtract (current-time)
  320. (list 0 (* 3600 org-extend-today-until) 0)))
  321. disabled-overlays)
  322. ;; Disable filters; this helps with alignment if there are links.
  323. (mapc (lambda (ol)
  324. (when (overlay-get ol 'invisible)
  325. (overlay-put ol 'invisible nil)
  326. (setq disabled-overlays (cons ol disabled-overlays))))
  327. (overlays-in (point-min) (point-max)))
  328. (save-excursion
  329. (goto-char (if line (point-at-bol) (point-min)))
  330. (while (not (eobp))
  331. (let ((habit (get-text-property (point) 'org-habit-p)))
  332. (when habit
  333. (move-to-column org-habit-graph-column t)
  334. (delete-char (min (+ 1 org-habit-preceding-days
  335. org-habit-following-days)
  336. (- (line-end-position) (point))))
  337. (insert-before-markers
  338. (org-habit-build-graph
  339. habit
  340. (time-subtract moment (days-to-time org-habit-preceding-days))
  341. moment
  342. (time-add moment (days-to-time org-habit-following-days))))))
  343. (forward-line)))
  344. (mapc (lambda (ol) (overlay-put ol 'invisible t))
  345. disabled-overlays)))
  346. (defun org-habit-toggle-habits ()
  347. "Toggle display of habits in an agenda buffer."
  348. (interactive)
  349. (org-agenda-check-type t 'agenda)
  350. (setq org-habit-show-habits (not org-habit-show-habits))
  351. (org-agenda-redo)
  352. (org-agenda-set-mode-name)
  353. (message "Habits turned %s"
  354. (if org-habit-show-habits "on" "off")))
  355. (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
  356. (provide 'org-habit)
  357. ;;; org-habit.el ends here