org-habit.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. ;;; org-habit.el --- The habit tracking code for Org-mode
  2. ;; Copyright (C) 2009 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. ;; Version: 6.33trans
  7. ;;
  8. ;; This file is part of GNU Emacs.
  9. ;;
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; This file contains the habit tracking code for Org-mode
  24. (require 'org)
  25. (require 'org-agenda)
  26. (eval-when-compile
  27. (require 'cl)
  28. (require 'calendar))
  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. (defface org-habit-clear-face
  57. '((((background light)) (:background "slateblue"))
  58. (((background dark)) (:background "blue")))
  59. "Face for days on which a task shouldn't be done yet."
  60. :group 'org-habit
  61. :group 'org-faces)
  62. (defface org-habit-clear-future-face
  63. '((((background light)) (:background "powderblue"))
  64. (((background dark)) (:background "midnightblue")))
  65. "Face for future days on which a task shouldn't be done yet."
  66. :group 'org-habit
  67. :group 'org-faces)
  68. (defface org-habit-ready-face
  69. '((((background light)) (:background "green"))
  70. (((background dark)) (:background "forestgreen")))
  71. "Face for days on which a task should start to be done."
  72. :group 'org-habit
  73. :group 'org-faces)
  74. (defface org-habit-ready-future-face
  75. '((((background light)) (:background "palegreen"))
  76. (((background dark)) (:background "darkgreen")))
  77. "Face for days on which a task should start to be done."
  78. :group 'org-habit
  79. :group 'org-faces)
  80. (defface org-habit-alert-face
  81. '((((background light)) (:background "yellow"))
  82. (((background dark)) (:background "gold")))
  83. "Face for days on which a task is due."
  84. :group 'org-habit
  85. :group 'org-faces)
  86. (defface org-habit-alert-future-face
  87. '((((background light)) (:background "palegoldenrod"))
  88. (((background dark)) (:background "darkgoldenrod")))
  89. "Face for days on which a task is due."
  90. :group 'org-habit
  91. :group 'org-faces)
  92. (defface org-habit-overdue-face
  93. '((((background light)) (:background "red"))
  94. (((background dark)) (:background "firebrick")))
  95. "Face for days on which a task is overdue."
  96. :group 'org-habit
  97. :group 'org-faces)
  98. (defface org-habit-overdue-future-face
  99. '((((background light)) (:background "mistyrose"))
  100. (((background dark)) (:background "darkred")))
  101. "Face for days on which a task is overdue."
  102. :group 'org-habit
  103. :group 'org-faces)
  104. (defun org-habit-duration-to-days (ts)
  105. (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
  106. ;; lead time is specified.
  107. (floor (* (string-to-number (match-string 1 ts))
  108. (cdr (assoc (match-string 2 ts)
  109. '(("d" . 1) ("w" . 7)
  110. ("m" . 30.4) ("y" . 365.25))))))
  111. (error "Invalid duration string: %s" ts)))
  112. (defun org-is-habit-p (&optional pom)
  113. (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
  114. (defun org-habit-parse-todo (&optional pom)
  115. "Parse the TODO surrounding point for its habit-related data.
  116. Returns a list with the following elements:
  117. 0: Scheduled date for the habit (may be in the past)
  118. 1: \".+\"-style repeater for the schedule, in days
  119. 2: Optional deadline (nil if not present)
  120. 3: If deadline, the repeater for the deadline, otherwise nil
  121. 4: A list of all the past dates this todo was mark closed
  122. This list represents a \"habit\" for the rest of this module."
  123. (save-excursion
  124. (if pom (goto-char pom))
  125. (assert (org-is-habit-p (point)))
  126. (let* ((scheduled (org-get-scheduled-time (point)))
  127. (scheduled-repeat (org-get-repeat org-scheduled-string))
  128. (sr-days (org-habit-duration-to-days scheduled-repeat))
  129. (end (org-entry-end-position))
  130. (habit-entry (org-no-properties (nth 5 (org-heading-components))))
  131. closed-dates deadline dr-days)
  132. (if scheduled
  133. (setq scheduled (time-to-days scheduled))
  134. (error "Habit %s has no scheduled date" habit-entry))
  135. (unless scheduled-repeat
  136. (error "Habit %s has no scheduled repeat period" habit-entry))
  137. (unless (> sr-days 0)
  138. (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
  139. (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
  140. (setq dr-days (org-habit-duration-to-days
  141. (match-string-no-properties 1 scheduled-repeat)))
  142. (if (<= dr-days sr-days)
  143. (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
  144. habit-entry scheduled-repeat))
  145. (setq deadline (+ scheduled (- dr-days sr-days))))
  146. (org-back-to-heading t)
  147. (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
  148. (push (time-to-days
  149. (org-time-string-to-time (match-string-no-properties 1)))
  150. closed-dates))
  151. (list scheduled sr-days deadline dr-days closed-dates))))
  152. (defsubst org-habit-scheduled (habit)
  153. (nth 0 habit))
  154. (defsubst org-habit-scheduled-repeat (habit)
  155. (nth 1 habit))
  156. (defsubst org-habit-deadline (habit)
  157. (let ((deadline (nth 2 habit)))
  158. (or deadline
  159. (+ (org-habit-scheduled habit)
  160. (1- (org-habit-scheduled-repeat habit))))))
  161. (defsubst org-habit-deadline-repeat (habit)
  162. (or (nth 3 habit)
  163. (org-habit-scheduled-repeat habit)))
  164. (defsubst org-habit-done-dates (habit)
  165. (nth 4 habit))
  166. (defsubst org-habit-get-priority (habit &optional moment)
  167. "Determine the relative priority of a habit.
  168. This must take into account not just urgency, but consistency as well."
  169. (let ((pri 1000)
  170. (now (time-to-days
  171. (or moment
  172. (time-subtract (current-time)
  173. (list 0 (* 3600 org-extend-today-until) 0)))))
  174. (scheduled (org-habit-scheduled habit))
  175. (deadline (org-habit-deadline habit)))
  176. ;; add 10 for every day past the scheduled date, and subtract for every
  177. ;; day before it
  178. (setq pri (+ pri (* (- now scheduled) 10)))
  179. ;; add 50 if the deadline is today
  180. (if (and (/= scheduled deadline)
  181. (= now deadline))
  182. (setq pri (+ pri 50)))
  183. ;; add 100 for every day beyond the deadline date, and subtract 10 for
  184. ;; every day before it
  185. (let ((slip (- now (1- deadline))))
  186. (if (> slip 0)
  187. (setq pri (+ pri (* slip 100)))
  188. (setq pri (+ pri (* slip 10)))))
  189. pri))
  190. (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
  191. "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
  192. NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
  193. SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
  194. Habits are assigned colors on the following basis:
  195. Blue Task is before the scheduled date.
  196. Green Task is on or after scheduled date, but before the
  197. end of the schedule's repeat period.
  198. Yellow If the task has a deadline, then it is after schedule's
  199. repeat period, but before the deadline.
  200. Orange The task has reached the deadline day, or if there is
  201. no deadline, the end of the schedule's repeat period.
  202. Red The task has gone beyond the deadline day or the
  203. schedule's repeat period."
  204. (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
  205. (s-repeat (org-habit-scheduled-repeat habit))
  206. (scheduled-end (+ scheduled (1- s-repeat)))
  207. (d-repeat (org-habit-deadline-repeat habit))
  208. (deadline (if scheduled-days
  209. (+ scheduled-days (- d-repeat s-repeat))
  210. (org-habit-deadline habit)))
  211. (m-days (or now-days (time-to-days (current-time)))))
  212. (cond
  213. ((< m-days scheduled)
  214. '(org-habit-clear-face . org-habit-clear-future-face))
  215. ((< m-days deadline)
  216. '(org-habit-ready-face . org-habit-ready-future-face))
  217. ((= m-days deadline)
  218. (if donep
  219. '(org-habit-ready-face . org-habit-ready-future-face)
  220. '(org-habit-alert-face . org-habit-alert-future-face)))
  221. (t
  222. '(org-habit-overdue-face . org-habit-overdue-future-face)))))
  223. (defun org-habit-build-graph (habit starting current ending)
  224. "Build a graph for the given HABIT, from STARTING to ENDING.
  225. CURRENT gives the current time between STARTING and ENDING, for
  226. the purpose of drawing the graph. It need not be the actual
  227. current time."
  228. (let* ((done-dates (sort (org-habit-done-dates habit) '<))
  229. (scheduled (org-habit-scheduled habit))
  230. (s-repeat (org-habit-scheduled-repeat habit))
  231. (start (time-to-days starting))
  232. (now (time-to-days current))
  233. (end (time-to-days ending))
  234. (graph (make-string (1+ (- end start)) ?\ ))
  235. (index 0)
  236. last-done-date)
  237. (while (and done-dates (< (car done-dates) start))
  238. (setq last-done-date (car done-dates)
  239. done-dates (cdr done-dates)))
  240. (while (< start end)
  241. (let* ((in-the-past-p (< start now))
  242. (todayp (= start now))
  243. (donep (and done-dates
  244. (= start (car done-dates))))
  245. (faces (if (and in-the-past-p
  246. (not last-done-date)
  247. (not (< scheduled now)))
  248. '(org-habit-clear-face . org-habit-clear-future-face)
  249. (org-habit-get-faces
  250. habit start (and in-the-past-p
  251. (if last-done-date
  252. (+ last-done-date s-repeat)
  253. scheduled))
  254. donep)))
  255. markedp face)
  256. (if donep
  257. (progn
  258. (aset graph index ?*)
  259. (setq markedp t)
  260. (while (and done-dates
  261. (= start (car done-dates)))
  262. (setq last-done-date (car done-dates)
  263. done-dates (cdr done-dates))))
  264. (if todayp
  265. (aset graph index ?!)))
  266. (setq face (if (or in-the-past-p todayp)
  267. (car faces)
  268. (cdr faces)))
  269. (if (and in-the-past-p
  270. (not (eq face 'org-habit-overdue-face))
  271. (not markedp))
  272. (setq face (cdr faces)))
  273. (put-text-property index (1+ index) 'face face graph))
  274. (setq start (1+ start)
  275. index (1+ index)))
  276. graph))
  277. (defun org-habit-insert-consistency-graphs (&optional line)
  278. "Insert consistency graph for any habitual tasks."
  279. (let ((inhibit-read-only t) l c
  280. (moment (time-subtract (current-time)
  281. (list 0 (* 3600 org-extend-today-until) 0))))
  282. (save-excursion
  283. (goto-char (if line (point-at-bol) (point-min)))
  284. (while (not (eobp))
  285. (let ((habit (get-text-property (point) 'org-habit-p)))
  286. (when habit
  287. (move-to-column org-habit-graph-column t)
  288. (delete-char (min (+ 1 org-habit-preceding-days
  289. org-habit-following-days)
  290. (- (line-end-position) (point))))
  291. (insert (org-habit-build-graph
  292. habit
  293. (time-subtract moment
  294. (days-to-time org-habit-preceding-days))
  295. moment
  296. (time-add moment
  297. (days-to-time org-habit-following-days))))))
  298. (forward-line)))))
  299. (defun org-habit-toggle-habits ()
  300. "Toggle display of habits in an agenda buffer."
  301. (interactive)
  302. (org-agenda-check-type t 'agenda)
  303. (setq org-habit-show-habits (not org-habit-show-habits))
  304. (org-agenda-redo)
  305. (org-agenda-set-mode-name)
  306. (message "Habits turned %s"
  307. (if org-habit-show-habits "on" "off")))
  308. (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
  309. (provide 'org-habit)
  310. ;; arch-tag: 64e070d9-bd09-4917-bd44-44465f5ed348
  311. ;;; org-habit.el ends here