org-habit.el 13 KB

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