org-habit.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. ;;; org-habit.el --- The habit tracking code for Org-mode
  2. ;; Copyright (C) 2009-2012 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-today-glyph ?!
  56. "Glyph character used to identify today."
  57. :group 'org-habit
  58. :version "24.1"
  59. :type 'character)
  60. (defcustom org-habit-completed-glyph ?*
  61. "Glyph character used to show completed days on which a task was done."
  62. :group 'org-habit
  63. :version "24.1"
  64. :type 'character)
  65. (defface org-habit-clear-face
  66. '((((background light)) (:background "#8270f9"))
  67. (((background dark)) (:background "blue")))
  68. "Face for days on which a task shouldn't be done yet."
  69. :group 'org-habit
  70. :group 'org-faces)
  71. (defface org-habit-clear-future-face
  72. '((((background light)) (:background "#d6e4fc"))
  73. (((background dark)) (:background "midnightblue")))
  74. "Face for future days on which a task shouldn't be done yet."
  75. :group 'org-habit
  76. :group 'org-faces)
  77. (defface org-habit-ready-face
  78. '((((background light)) (:background "#4df946"))
  79. (((background dark)) (:background "forestgreen")))
  80. "Face for days on which a task should start to be done."
  81. :group 'org-habit
  82. :group 'org-faces)
  83. (defface org-habit-ready-future-face
  84. '((((background light)) (:background "#acfca9"))
  85. (((background dark)) (:background "darkgreen")))
  86. "Face for days on which a task should start to be done."
  87. :group 'org-habit
  88. :group 'org-faces)
  89. (defface org-habit-alert-face
  90. '((((background light)) (:background "#f5f946"))
  91. (((background dark)) (:background "gold")))
  92. "Face for days on which a task is due."
  93. :group 'org-habit
  94. :group 'org-faces)
  95. (defface org-habit-alert-future-face
  96. '((((background light)) (:background "#fafca9"))
  97. (((background dark)) (:background "darkgoldenrod")))
  98. "Face for days on which a task is due."
  99. :group 'org-habit
  100. :group 'org-faces)
  101. (defface org-habit-overdue-face
  102. '((((background light)) (:background "#f9372d"))
  103. (((background dark)) (:background "firebrick")))
  104. "Face for days on which a task is overdue."
  105. :group 'org-habit
  106. :group 'org-faces)
  107. (defface org-habit-overdue-future-face
  108. '((((background light)) (:background "#fc9590"))
  109. (((background dark)) (:background "darkred")))
  110. "Face for days on which a task is overdue."
  111. :group 'org-habit
  112. :group 'org-faces)
  113. (defun org-habit-duration-to-days (ts)
  114. (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
  115. ;; lead time is specified.
  116. (floor (* (string-to-number (match-string 1 ts))
  117. (cdr (assoc (match-string 2 ts)
  118. '(("d" . 1) ("w" . 7)
  119. ("m" . 30.4) ("y" . 365.25))))))
  120. (error "Invalid duration string: %s" ts)))
  121. (defun org-is-habit-p (&optional pom)
  122. "Is the task at POM or point a habit?"
  123. (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
  124. (defun org-habit-parse-todo (&optional pom)
  125. "Parse the TODO surrounding point for its habit-related data.
  126. Returns a list with the following elements:
  127. 0: Scheduled date for the habit (may be in the past)
  128. 1: \".+\"-style repeater for the schedule, in days
  129. 2: Optional deadline (nil if not present)
  130. 3: If deadline, the repeater for the deadline, otherwise nil
  131. 4: A list of all the past dates this todo was mark closed
  132. This list represents a \"habit\" for the rest of this module."
  133. (save-excursion
  134. (if pom (goto-char pom))
  135. (assert (org-is-habit-p (point)))
  136. (let* ((scheduled (org-get-scheduled-time (point)))
  137. (scheduled-repeat (org-get-repeat org-scheduled-string))
  138. (end (org-entry-end-position))
  139. (habit-entry (org-no-properties (nth 4 (org-heading-components))))
  140. closed-dates deadline dr-days sr-days)
  141. (if scheduled
  142. (setq scheduled (time-to-days scheduled))
  143. (error "Habit %s has no scheduled date" habit-entry))
  144. (unless scheduled-repeat
  145. (error
  146. "Habit '%s' has no scheduled repeat period or has an incorrect one"
  147. habit-entry))
  148. (setq sr-days (org-habit-duration-to-days scheduled-repeat))
  149. (unless (> sr-days 0)
  150. (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
  151. (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
  152. (setq dr-days (org-habit-duration-to-days
  153. (match-string-no-properties 1 scheduled-repeat)))
  154. (if (<= dr-days sr-days)
  155. (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
  156. habit-entry scheduled-repeat))
  157. (setq deadline (+ scheduled (- dr-days sr-days))))
  158. (org-back-to-heading t)
  159. (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
  160. (reversed org-log-states-order-reversed)
  161. (search (if reversed 're-search-forward 're-search-backward))
  162. (limit (if reversed end (point)))
  163. (count 0))
  164. (unless reversed (goto-char end))
  165. (while (and (< count maxdays)
  166. (funcall search "- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t))
  167. (push (time-to-days
  168. (org-time-string-to-time (match-string-no-properties 1)))
  169. closed-dates)
  170. (setq count (1+ count))))
  171. (list scheduled sr-days deadline dr-days closed-dates))))
  172. (defsubst org-habit-scheduled (habit)
  173. (nth 0 habit))
  174. (defsubst org-habit-scheduled-repeat (habit)
  175. (nth 1 habit))
  176. (defsubst org-habit-deadline (habit)
  177. (let ((deadline (nth 2 habit)))
  178. (or deadline
  179. (if (nth 3 habit)
  180. (+ (org-habit-scheduled habit)
  181. (1- (org-habit-scheduled-repeat habit)))
  182. (org-habit-scheduled habit)))))
  183. (defsubst org-habit-deadline-repeat (habit)
  184. (or (nth 3 habit)
  185. (org-habit-scheduled-repeat habit)))
  186. (defsubst org-habit-done-dates (habit)
  187. (nth 4 habit))
  188. (defsubst org-habit-get-priority (habit &optional moment)
  189. "Determine the relative priority of a habit.
  190. This must take into account not just urgency, but consistency as well."
  191. (let ((pri 1000)
  192. (now (if moment (time-to-days moment) (org-today)))
  193. (scheduled (org-habit-scheduled habit))
  194. (deadline (org-habit-deadline habit)))
  195. ;; add 10 for every day past the scheduled date, and subtract for every
  196. ;; day before it
  197. (setq pri (+ pri (* (- now scheduled) 10)))
  198. ;; add 50 if the deadline is today
  199. (if (and (/= scheduled deadline)
  200. (= now deadline))
  201. (setq pri (+ pri 50)))
  202. ;; add 100 for every day beyond the deadline date, and subtract 10 for
  203. ;; every day before it
  204. (let ((slip (- now (1- deadline))))
  205. (if (> slip 0)
  206. (setq pri (+ pri (* slip 100)))
  207. (setq pri (+ pri (* slip 10)))))
  208. pri))
  209. (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
  210. "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
  211. NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
  212. SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
  213. Habits are assigned colors on the following basis:
  214. Blue Task is before the scheduled date.
  215. Green Task is on or after scheduled date, but before the
  216. end of the schedule's repeat period.
  217. Yellow If the task has a deadline, then it is after schedule's
  218. repeat period, but before the deadline.
  219. Orange The task has reached the deadline day, or if there is
  220. no deadline, the end of the schedule's repeat period.
  221. Red The task has gone beyond the deadline day or the
  222. schedule's repeat period."
  223. (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
  224. (s-repeat (org-habit-scheduled-repeat habit))
  225. (scheduled-end (+ scheduled (1- s-repeat)))
  226. (d-repeat (org-habit-deadline-repeat habit))
  227. (deadline (if scheduled-days
  228. (+ scheduled-days (- d-repeat s-repeat))
  229. (org-habit-deadline habit)))
  230. (m-days (or now-days (time-to-days (current-time)))))
  231. (cond
  232. ((< m-days scheduled)
  233. '(org-habit-clear-face . org-habit-clear-future-face))
  234. ((< m-days deadline)
  235. '(org-habit-ready-face . org-habit-ready-future-face))
  236. ((= m-days deadline)
  237. (if donep
  238. '(org-habit-ready-face . org-habit-ready-future-face)
  239. '(org-habit-alert-face . org-habit-alert-future-face)))
  240. (t
  241. '(org-habit-overdue-face . org-habit-overdue-future-face)))))
  242. (defun org-habit-build-graph (habit starting current ending)
  243. "Build a graph for the given HABIT, from STARTING to ENDING.
  244. CURRENT gives the current time between STARTING and ENDING, for
  245. the purpose of drawing the graph. It need not be the actual
  246. current time."
  247. (let* ((done-dates (sort (org-habit-done-dates habit) '<))
  248. (scheduled (org-habit-scheduled habit))
  249. (s-repeat (org-habit-scheduled-repeat habit))
  250. (start (time-to-days starting))
  251. (now (time-to-days current))
  252. (end (time-to-days ending))
  253. (graph (make-string (1+ (- end start)) ?\ ))
  254. (index 0)
  255. last-done-date)
  256. (while (and done-dates (< (car done-dates) start))
  257. (setq last-done-date (car done-dates)
  258. done-dates (cdr done-dates)))
  259. (while (< start end)
  260. (let* ((in-the-past-p (< start now))
  261. (todayp (= start now))
  262. (donep (and done-dates
  263. (= start (car done-dates))))
  264. (faces (if (and in-the-past-p
  265. (not last-done-date)
  266. (not (< scheduled now)))
  267. '(org-habit-clear-face . org-habit-clear-future-face)
  268. (org-habit-get-faces
  269. habit start (and in-the-past-p
  270. (if last-done-date
  271. (+ last-done-date s-repeat)
  272. scheduled))
  273. donep)))
  274. markedp face)
  275. (if donep
  276. (let ((done-time (time-add
  277. starting
  278. (days-to-time
  279. (- start (time-to-days starting))))))
  280. (aset graph index org-habit-completed-glyph)
  281. (setq markedp t)
  282. (put-text-property
  283. index (1+ index) 'help-echo
  284. (format-time-string (org-time-stamp-format) done-time) graph)
  285. (while (and done-dates
  286. (= start (car done-dates)))
  287. (setq last-done-date (car done-dates)
  288. done-dates (cdr done-dates))))
  289. (if todayp
  290. (aset graph index org-habit-today-glyph)))
  291. (setq face (if (or in-the-past-p todayp)
  292. (car faces)
  293. (cdr faces)))
  294. (if (and in-the-past-p
  295. (not (eq face 'org-habit-overdue-face))
  296. (not markedp))
  297. (setq face (cdr faces)))
  298. (put-text-property index (1+ index) 'face face graph))
  299. (setq start (1+ start)
  300. index (1+ index)))
  301. graph))
  302. (defun org-habit-insert-consistency-graphs (&optional line)
  303. "Insert consistency graph for any habitual tasks."
  304. (let ((inhibit-read-only t) l c
  305. (buffer-invisibility-spec '(org-link))
  306. (moment (time-subtract (current-time)
  307. (list 0 (* 3600 org-extend-today-until) 0)))
  308. disabled-overlays)
  309. ;; Disable filters; this helps with alignment if there are links.
  310. (mapc (lambda (ol)
  311. (when (overlay-get ol 'invisible)
  312. (overlay-put ol 'invisible nil)
  313. (setq disabled-overlays (cons ol disabled-overlays))))
  314. (overlays-in (point-min) (point-max)))
  315. (save-excursion
  316. (goto-char (if line (point-at-bol) (point-min)))
  317. (while (not (eobp))
  318. (let ((habit (get-text-property (point) 'org-habit-p)))
  319. (when habit
  320. (move-to-column org-habit-graph-column t)
  321. (delete-char (min (+ 1 org-habit-preceding-days
  322. org-habit-following-days)
  323. (- (line-end-position) (point))))
  324. (insert-before-markers
  325. (org-habit-build-graph
  326. habit
  327. (time-subtract moment (days-to-time org-habit-preceding-days))
  328. moment
  329. (time-add moment (days-to-time org-habit-following-days))))))
  330. (forward-line)))
  331. (mapc (lambda (ol) (overlay-put ol 'invisible t))
  332. disabled-overlays)))
  333. (defun org-habit-toggle-habits ()
  334. "Toggle display of habits in an agenda buffer."
  335. (interactive)
  336. (org-agenda-check-type t 'agenda)
  337. (setq org-habit-show-habits (not org-habit-show-habits))
  338. (org-agenda-redo)
  339. (org-agenda-set-mode-name)
  340. (message "Habits turned %s"
  341. (if org-habit-show-habits "on" "off")))
  342. (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
  343. (provide 'org-habit)
  344. ;;; org-habit.el ends here