org-habit.el 13 KB

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