org-habit.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.32
  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 date."
  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-alert-face
  82. '((((background light)) (:background "yellow"))
  83. (((background dark)) (:background "gold")))
  84. "Face for days on which a task is due."
  85. :group 'org-habit
  86. :group 'org-faces)
  87. (defface org-habit-alert-future-face
  88. '((((background light)) (:background "palegoldenrod"))
  89. (((background dark)) (:background "darkgoldenrod")))
  90. "Face for days on which a task is due."
  91. :group 'org-habit
  92. :group 'org-faces)
  93. (defface org-habit-overdue-face
  94. '((((background light)) (:background "red"))
  95. (((background dark)) (:background "firebrick")))
  96. "Face for days on which a task is overdue."
  97. :group 'org-habit
  98. :group 'org-faces)
  99. (defface org-habit-overdue-future-face
  100. '((((background light)) (:background "mistyrose"))
  101. (((background dark)) (:background "darkred")))
  102. "Face for days on which a task is overdue."
  103. :group 'org-habit
  104. :group 'org-faces)
  105. (defun org-habit-duration-to-days (ts)
  106. (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
  107. ;; lead time is specified.
  108. (floor (* (string-to-number (match-string 1 ts))
  109. (cdr (assoc (match-string 2 ts)
  110. '(("d" . 1) ("w" . 7)
  111. ("m" . 30.4) ("y" . 365.25))))))
  112. (error "Invalid duration string: %s" ts)))
  113. (defun org-is-habit-p (&optional pom)
  114. (string= "habit" (org-entry-get (or pom (point)) "STYLE")))
  115. (defun org-habit-parse-todo (&optional pom)
  116. "Parse the TODO surrounding point for its habit-related data.
  117. Returns a list with the following elements:
  118. 0: Scheduled date for the habit (may be in the past)
  119. 1: \".+\"-style repeater for the schedule, in days
  120. 2: Optional deadline (nil if not present)
  121. 3: If deadline, the repeater for the deadline, otherwise nil
  122. 4: A list of all the past dates this todo was mark closed
  123. This list represents a \"habit\" for the rest of this module."
  124. (save-excursion
  125. (if pom (goto-char pom))
  126. (assert (org-is-habit-p (point)))
  127. (let* ((scheduled (org-get-scheduled-time (point)))
  128. (scheduled-repeat (org-get-repeat org-scheduled-string))
  129. (sr-days (org-habit-duration-to-days scheduled-repeat))
  130. (end (org-entry-end-position))
  131. closed-dates deadline dr-days)
  132. (if scheduled
  133. (setq scheduled (time-to-days scheduled))
  134. (error "Habit has no scheduled date"))
  135. (unless scheduled-repeat
  136. (error "Habit has no scheduled repeat period"))
  137. (unless (> sr-days 0)
  138. (error "Habit's scheduled repeat period is less than 1d"))
  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"))
  144. (setq deadline (+ scheduled (- dr-days sr-days))))
  145. (org-back-to-heading t)
  146. (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
  147. (push (time-to-days
  148. (org-time-string-to-time (match-string-no-properties 1)))
  149. closed-dates))
  150. (list scheduled sr-days deadline dr-days closed-dates))))
  151. (defsubst org-habit-scheduled (habit)
  152. (nth 0 habit))
  153. (defsubst org-habit-scheduled-repeat (habit)
  154. (nth 1 habit))
  155. (defsubst org-habit-deadline (habit)
  156. (let ((deadline (nth 2 habit)))
  157. (or deadline
  158. (+ (org-habit-scheduled habit)
  159. (1- (org-habit-scheduled-repeat habit))))))
  160. (defsubst org-habit-deadline-repeat (habit)
  161. (or (nth 3 habit)
  162. (org-habit-scheduled-repeat habit)))
  163. (defsubst org-habit-done-dates (habit)
  164. (nth 4 habit))
  165. (defsubst org-habit-get-priority (habit &optional moment)
  166. "Determine the relative priority of a habit.
  167. This must take into account not just urgency, but consistency as well."
  168. (let ((pri 1000)
  169. (now (time-to-days
  170. (or moment
  171. (time-subtract (current-time)
  172. (list 0 (* 3600 org-extend-today-until) 0)))))
  173. (scheduled (org-habit-scheduled habit))
  174. (deadline (org-habit-deadline habit)))
  175. ;; add 10 for every day past the scheduled date, and subtract for every
  176. ;; day before it
  177. (setq pri (+ pri (* (- now scheduled) 10)))
  178. ;; add 50 if the deadline is today
  179. (if (and (/= scheduled deadline)
  180. (= now deadline))
  181. (setq pri (+ pri 50)))
  182. ;; add 100 for every day beyond the deadline date, and subtract 10 for
  183. ;; every day before it
  184. (let ((slip (- now (1- deadline))))
  185. (if (> slip 0)
  186. (setq pri (+ pri (* slip 100)))
  187. (setq pri (+ pri (* slip 10)))))
  188. pri))
  189. (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
  190. "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
  191. NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
  192. SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
  193. Habits are assigned colors on the following basis:
  194. Blue Task is before the scheduled date.
  195. Green Task is on or after scheduled date, but before the
  196. end of the schedule's repeat period.
  197. Yellow If the task has a deadline, then it is after schedule's
  198. repeat period, but before the deadline.
  199. Orange The task has reached the deadline day, or if there is
  200. no deadline, the end of the schedule's repeat period.
  201. Red The task has gone beyond the deadline day or the
  202. schedule's repeat period."
  203. (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
  204. (s-repeat (org-habit-scheduled-repeat habit))
  205. (scheduled-end (+ scheduled (1- s-repeat)))
  206. (d-repeat (org-habit-deadline-repeat habit))
  207. (deadline (if scheduled-days
  208. (+ scheduled-days (- d-repeat s-repeat))
  209. (org-habit-deadline habit)))
  210. (m-days (or now-days (time-to-days (current-time)))))
  211. (cond
  212. ((< m-days scheduled)
  213. '(org-habit-clear-face . org-habit-clear-future-face))
  214. ((< m-days deadline)
  215. '(org-habit-ready-face . org-habit-ready-future-face))
  216. ((= m-days deadline)
  217. (if donep
  218. '(org-habit-ready-face . org-habit-ready-future-face)
  219. '(org-habit-alert-face . org-habit-alert-future-face)))
  220. (t
  221. '(org-habit-overdue-face . org-habit-overdue-future-face)))))
  222. (defun org-habit-build-graph (habit starting current ending)
  223. "Build a graph for the given HABIT, from STARTING to ENDING.
  224. CURRENT gives the current time between STARTING and ENDING, for
  225. the purpose of drawing the graph. It need not be the actual
  226. current time."
  227. (let* ((done-dates (sort (org-habit-done-dates habit) '<))
  228. (scheduled (org-habit-scheduled habit))
  229. (s-repeat (org-habit-scheduled-repeat habit))
  230. (start (time-to-days starting))
  231. (now (time-to-days current))
  232. (end (time-to-days ending))
  233. (graph (make-string (1+ (- end start)) ?\ ))
  234. (index 0)
  235. last-done-date)
  236. (while (and done-dates (< (car done-dates) start))
  237. (setq last-done-date (car done-dates)
  238. done-dates (cdr done-dates)))
  239. (while (< start end)
  240. (let* ((in-the-past-p (< start now))
  241. (todayp (= start now))
  242. (donep (and done-dates
  243. (= start (car done-dates))))
  244. (faces (if (and in-the-past-p
  245. (not last-done-date)
  246. (not (< scheduled now)))
  247. '(org-habit-clear-face . org-habit-clear-future-face)
  248. (org-habit-get-faces
  249. habit start (and in-the-past-p
  250. (if last-done-date
  251. (+ last-done-date s-repeat)
  252. scheduled))
  253. donep)))
  254. markedp face)
  255. (if donep
  256. (progn
  257. (aset graph index ?*)
  258. (setq markedp t)
  259. (while (and done-dates
  260. (= start (car done-dates)))
  261. (setq last-done-date (car done-dates)
  262. done-dates (cdr done-dates))))
  263. (if todayp
  264. (aset graph index ?!)))
  265. (setq face (if (or in-the-past-p todayp)
  266. (car faces)
  267. (cdr faces)))
  268. (if (and in-the-past-p
  269. (not (eq face 'org-habit-overdue-face))
  270. (not markedp))
  271. (setq face (cdr faces)))
  272. (put-text-property index (1+ index) 'face face graph))
  273. (setq start (1+ start)
  274. index (1+ index)))
  275. graph))
  276. (defun org-habit-insert-consistency-graphs (&optional line)
  277. "Insert consistency graph for any habitual tasks."
  278. (let ((inhibit-read-only t) l c
  279. (moment (time-subtract (current-time)
  280. (list 0 (* 3600 org-extend-today-until) 0))))
  281. (save-excursion
  282. (goto-char (if line (point-at-bol) (point-min)))
  283. (while (not (eobp))
  284. (let ((habit (get-text-property (point) 'org-habit-p)))
  285. (when habit
  286. (move-to-column org-habit-graph-column t)
  287. (delete-char (min (+ 1 org-habit-preceding-days
  288. org-habit-following-days)
  289. (- (line-end-position) (point))))
  290. (insert (org-habit-build-graph
  291. habit
  292. (time-subtract moment
  293. (days-to-time org-habit-preceding-days))
  294. moment
  295. (time-add moment
  296. (days-to-time org-habit-following-days))))))
  297. (forward-line)))))
  298. (defun org-habit-toggle-habits ()
  299. "Toggle display of habits in an agenda buffer."
  300. (interactive)
  301. (org-agenda-check-type t 'agenda)
  302. (setq org-habit-show-habits (not org-habit-show-habits))
  303. (org-agenda-redo)
  304. (org-agenda-set-mode-name)
  305. (message "Habits turned %s"
  306. (if org-habit-show-habits "on" "off")))
  307. (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
  308. (provide 'org-habit)
  309. ;; arch-tag:
  310. ;;; org-habit.el ends here