org-habit.el 11 KB

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