org-habit.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 "SCHEDULED"))
  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. (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
  149. (setq dr-days (org-habit-duration-to-days
  150. (match-string-no-properties 1 scheduled-repeat)))
  151. (if (<= dr-days sr-days)
  152. (error "Habit's deadline repeat period is less than or equal to scheduled"))
  153. (setq deadline (time-add scheduled
  154. (days-to-time (- dr-days sr-days)))))
  155. (org-back-to-heading t)
  156. (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
  157. (push (org-time-string-to-time (match-string-no-properties 1))
  158. closed-dates))
  159. (list scheduled sr-days deadline dr-days closed-dates))))
  160. (defsubst org-habit-scheduled (habit)
  161. (nth 0 habit))
  162. (defsubst org-habit-scheduled-repeat (habit)
  163. (nth 1 habit))
  164. (defsubst org-habit-deadline (habit)
  165. (nth 2 habit))
  166. (defsubst org-habit-deadline-repeat (habit)
  167. (nth 3 habit))
  168. (defsubst org-habit-done-dates (habit)
  169. (nth 4 habit))
  170. (defsubst org-habit-get-priority (habit)
  171. "Determine the relative priority of a habit.
  172. This must take into account not just urgency, but consistency as well."
  173. (let ((pri 1000)
  174. (days (time-to-days
  175. (time-subtract (current-time)
  176. (list 0 (* 3600 org-extend-today-until) 0))))
  177. (s-days (time-to-days (org-habit-scheduled habit)))
  178. (d-days (time-to-days (org-habit-deadline habit))))
  179. ;; add 10 for every day past the scheduled date, and subtract for every
  180. ;; day before it
  181. (let ((slip (- days s-days)))
  182. (if (> slip 0)
  183. (setq pri (+ pri (* slip 10)))
  184. (setq pri (+ pri (* slip 10)))))
  185. ;; add 20 for every day beyond the deadline date, and subtract 5 for every
  186. ;; day before it
  187. (if (/= s-days d-days)
  188. ;; add 100 if the deadline is today
  189. (if (= days d-days)
  190. (setq pri (+ pri 100))))
  191. (let ((slip (- days d-days)))
  192. (if (> slip 0)
  193. (setq pri (+ pri (* slip 20)))
  194. (setq pri (+ pri (* slip 5)))))
  195. pri))
  196. (defun org-habit-get-faces (habit &optional moment scheduled-time donep)
  197. "Return faces for HABIT relative to MOMENT and SCHEDULED-TIME.
  198. MOMENT defaults to the current time if it is nil.
  199. SCHEDULED-TIME defaults to the habit's actual scheduled time if nil.
  200. Habits are assigned colors on the following basis:
  201. Blue Task is before the scheduled date.
  202. Green Task is on or after scheduled date, but before the
  203. end of the schedule's repeat period.
  204. Yellow If the task has a deadline, then it is after schedule's
  205. repeat period, but before the deadline.
  206. Orange The task has reached the deadline day, or if there is
  207. no deadline, the end of the schedule's repeat period.
  208. Red The task has gone beyond the deadline day or the
  209. schedule's repeat period."
  210. (unless moment (setq moment (current-time)))
  211. (let* ((scheduled (or scheduled-time (org-habit-scheduled habit)))
  212. (s-repeat (org-habit-scheduled-repeat habit))
  213. (scheduled-end (time-add scheduled (days-to-time s-repeat)))
  214. (d-repeat (org-habit-deadline-repeat habit))
  215. (deadline (if (and scheduled-time d-repeat)
  216. (time-add scheduled-time
  217. (days-to-time (- d-repeat s-repeat)))
  218. (org-habit-deadline habit))))
  219. (cond
  220. ((time-less-p moment scheduled)
  221. '(org-habit-clear-face . org-habit-clear-future-face))
  222. ((time-less-p moment scheduled-end)
  223. '(org-habit-ready-face . org-habit-ready-future-face))
  224. ((and deadline
  225. (time-less-p moment deadline))
  226. (if donep
  227. '(org-habit-ready-face . org-habit-ready-future-face)
  228. '(org-habit-warning-face . org-habit-warning-future-face)))
  229. ((= (time-to-days moment)
  230. (if deadline
  231. (time-to-days deadline)
  232. (time-to-days scheduled-end)))
  233. (if donep
  234. '(org-habit-ready-face . org-habit-ready-future-face)
  235. '(org-habit-alert-face . org-habit-alert-future-face)))
  236. (t
  237. '(org-habit-overdue-face . org-habit-overdue-future-face)))))
  238. (defun org-habit-build-graph (habit &optional starting current ending)
  239. "Build a color graph for the given HABIT, from STARTING to ENDING."
  240. (let ((done-dates (sort (org-habit-done-dates habit) 'time-less-p))
  241. (scheduled (org-habit-scheduled habit))
  242. (s-repeat (org-habit-scheduled-repeat habit))
  243. (day starting)
  244. (current-days (time-to-days current))
  245. last-done-date
  246. (graph (make-string (1+ (- (time-to-days ending)
  247. (time-to-days starting))) ?\ ))
  248. (index 0))
  249. (if done-dates
  250. (while (time-less-p (car done-dates) starting)
  251. (setq last-done-date (car done-dates)
  252. done-dates (cdr done-dates))))
  253. (while (time-less-p day ending)
  254. (let* ((now-days (time-to-days day))
  255. (in-the-past-p (< now-days current-days))
  256. (todayp (= now-days current-days))
  257. (donep (and done-dates
  258. (= now-days (time-to-days (car done-dates)))))
  259. (faces (if (and in-the-past-p
  260. (not last-done-date)
  261. (not (time-less-p scheduled current)))
  262. '(org-habit-clear-face . org-habit-clear-future-face)
  263. (org-habit-get-faces
  264. habit day (and in-the-past-p
  265. (if last-done-date
  266. (time-add last-done-date
  267. (days-to-time s-repeat))
  268. scheduled))
  269. donep)))
  270. markedp face)
  271. (if donep
  272. (progn
  273. (aset graph index ?*)
  274. (setq last-done-date (car done-dates)
  275. done-dates (cdr done-dates)
  276. markedp t))
  277. (if todayp
  278. (aset graph index ?!)))
  279. (setq face (if (or in-the-past-p
  280. todayp)
  281. (car faces)
  282. (cdr faces)))
  283. (if (and in-the-past-p
  284. (not (eq face 'org-habit-overdue-face))
  285. (not markedp))
  286. (setq face (cdr faces)))
  287. (put-text-property index (1+ index) 'face face graph))
  288. (setq day (time-add day (days-to-time 1))
  289. index (1+ index)))
  290. graph))
  291. (defun org-habit-insert-consistency-graphs (&optional line)
  292. "Insert consistency graph for any habitual tasks."
  293. (let ((inhibit-read-only t) l c
  294. (moment (time-subtract (current-time)
  295. (list 0 (* 3600 org-extend-today-until) 0))))
  296. (save-excursion
  297. (goto-char (if line (point-at-bol) (point-min)))
  298. (while (not (eobp))
  299. (let ((habit (get-text-property (point) 'org-habit-p)))
  300. (when habit
  301. (move-to-column org-habit-graph-column t)
  302. (delete-char (min (+ 1 org-habit-preceding-days
  303. org-habit-following-days)
  304. (- (line-end-position) (point))))
  305. (insert (org-habit-build-graph
  306. habit
  307. (time-subtract moment
  308. (days-to-time org-habit-preceding-days))
  309. moment
  310. (time-add moment
  311. (days-to-time org-habit-following-days))))))
  312. (forward-line)))))
  313. (defun org-habit-toggle-habits ()
  314. "Toggle display of habits in an agenda buffer."
  315. (interactive)
  316. (org-agenda-check-type t 'agenda)
  317. (setq org-habit-show-habits (not org-habit-show-habits))
  318. (org-agenda-redo)
  319. (org-agenda-set-mode-name)
  320. (message "Habits turned %s"
  321. (if org-habit-show-habits "on" "off")))
  322. (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
  323. (provide 'org-habit)
  324. ;; arch-tag:
  325. ;;; org-habit.el ends here