org-habit.el 11 KB

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