org-habit.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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: 7.5
  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. ;;; Code:
  25. (require 'org)
  26. (require 'org-agenda)
  27. (eval-when-compile
  28. (require 'cl))
  29. (defgroup org-habit nil
  30. "Options concerning habit tracking in Org-mode."
  31. :tag "Org Habit"
  32. :group 'org-progress)
  33. (defcustom org-habit-graph-column 40
  34. "The absolute column at which to insert habit consistency graphs.
  35. Note that consistency graphs will overwrite anything else in the buffer."
  36. :group 'org-habit
  37. :type 'integer)
  38. (defcustom org-habit-preceding-days 21
  39. "Number of days before today to appear in consistency graphs."
  40. :group 'org-habit
  41. :type 'integer)
  42. (defcustom org-habit-following-days 7
  43. "Number of days after today to appear in consistency graphs."
  44. :group 'org-habit
  45. :type 'integer)
  46. (defcustom org-habit-show-habits t
  47. "If non-nil, show habits in agenda buffers."
  48. :group 'org-habit
  49. :type 'boolean)
  50. (defcustom org-habit-show-habits-only-for-today t
  51. "If non-nil, only show habits on today's agenda, and not for future days.
  52. Note that even when shown for future days, the graph is always
  53. relative to the current effective date."
  54. :group 'org-habit
  55. :type 'boolean)
  56. (defface org-habit-clear-face
  57. '((((background light)) (:background "#8270f9"))
  58. (((background dark)) (:background "blue")))
  59. "Face for days on which a task shouldn't be done yet."
  60. :group 'org-habit
  61. :group 'org-faces)
  62. (defface org-habit-clear-future-face
  63. '((((background light)) (:background "#d6e4fc"))
  64. (((background dark)) (:background "midnightblue")))
  65. "Face for future days on which a task shouldn't be done yet."
  66. :group 'org-habit
  67. :group 'org-faces)
  68. (defface org-habit-ready-face
  69. '((((background light)) (:background "#4df946"))
  70. (((background dark)) (:background "forestgreen")))
  71. "Face for days on which a task should start to be done."
  72. :group 'org-habit
  73. :group 'org-faces)
  74. (defface org-habit-ready-future-face
  75. '((((background light)) (:background "#acfca9"))
  76. (((background dark)) (:background "darkgreen")))
  77. "Face for days on which a task should start to be done."
  78. :group 'org-habit
  79. :group 'org-faces)
  80. (defface org-habit-alert-face
  81. '((((background light)) (:background "#f5f946"))
  82. (((background dark)) (:background "gold")))
  83. "Face for days on which a task is due."
  84. :group 'org-habit
  85. :group 'org-faces)
  86. (defface org-habit-alert-future-face
  87. '((((background light)) (:background "#fafca9"))
  88. (((background dark)) (:background "darkgoldenrod")))
  89. "Face for days on which a task is due."
  90. :group 'org-habit
  91. :group 'org-faces)
  92. (defface org-habit-overdue-face
  93. '((((background light)) (:background "#f9372d"))
  94. (((background dark)) (:background "firebrick")))
  95. "Face for days on which a task is overdue."
  96. :group 'org-habit
  97. :group 'org-faces)
  98. (defface org-habit-overdue-future-face
  99. '((((background light)) (:background "#fc9590"))
  100. (((background dark)) (:background "darkred")))
  101. "Face for days on which a task is overdue."
  102. :group 'org-habit
  103. :group 'org-faces)
  104. (defun org-habit-duration-to-days (ts)
  105. (if (string-match "\\([0-9]+\\)\\([dwmy]\\)" ts)
  106. ;; lead time is specified.
  107. (floor (* (string-to-number (match-string 1 ts))
  108. (cdr (assoc (match-string 2 ts)
  109. '(("d" . 1) ("w" . 7)
  110. ("m" . 30.4) ("y" . 365.25))))))
  111. (error "Invalid duration string: %s" ts)))
  112. (defun org-is-habit-p (&optional pom)
  113. "Is the task at POM or point a habit?"
  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. (end (org-entry-end-position))
  130. (habit-entry (org-no-properties (nth 4 (org-heading-components))))
  131. closed-dates deadline dr-days sr-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
  137. "Habit '%s' has no scheduled repeat period or has an incorrect one"
  138. habit-entry))
  139. (setq sr-days (org-habit-duration-to-days scheduled-repeat))
  140. (unless (> sr-days 0)
  141. (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
  142. (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
  143. (setq dr-days (org-habit-duration-to-days
  144. (match-string-no-properties 1 scheduled-repeat)))
  145. (if (<= dr-days sr-days)
  146. (error "Habit %s deadline repeat period is less than or equal to scheduled (%s)"
  147. habit-entry scheduled-repeat))
  148. (setq deadline (+ scheduled (- dr-days sr-days))))
  149. (org-back-to-heading t)
  150. (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
  151. (reversed org-log-states-order-reversed)
  152. (search (if reversed 're-search-forward 're-search-backward))
  153. (limit (if reversed end (point)))
  154. (count 0))
  155. (unless reversed (goto-char end))
  156. (while (and (< count maxdays)
  157. (funcall search "- State \"DONE\".*\\[\\([^]]+\\)\\]" limit t))
  158. (push (time-to-days
  159. (org-time-string-to-time (match-string-no-properties 1)))
  160. closed-dates)
  161. (setq count (1+ count))))
  162. (list scheduled sr-days deadline dr-days closed-dates))))
  163. (defsubst org-habit-scheduled (habit)
  164. (nth 0 habit))
  165. (defsubst org-habit-scheduled-repeat (habit)
  166. (nth 1 habit))
  167. (defsubst org-habit-deadline (habit)
  168. (let ((deadline (nth 2 habit)))
  169. (or deadline
  170. (if (nth 3 habit)
  171. (+ (org-habit-scheduled habit)
  172. (1- (org-habit-scheduled-repeat habit)))
  173. (org-habit-scheduled habit)))))
  174. (defsubst org-habit-deadline-repeat (habit)
  175. (or (nth 3 habit)
  176. (org-habit-scheduled-repeat habit)))
  177. (defsubst org-habit-done-dates (habit)
  178. (nth 4 habit))
  179. (defsubst org-habit-get-priority (habit &optional moment)
  180. "Determine the relative priority of a habit.
  181. This must take into account not just urgency, but consistency as well."
  182. (let ((pri 1000)
  183. (now (if moment (time-to-days moment) (org-today)))
  184. (scheduled (org-habit-scheduled habit))
  185. (deadline (org-habit-deadline habit)))
  186. ;; add 10 for every day past the scheduled date, and subtract for every
  187. ;; day before it
  188. (setq pri (+ pri (* (- now scheduled) 10)))
  189. ;; add 50 if the deadline is today
  190. (if (and (/= scheduled deadline)
  191. (= now deadline))
  192. (setq pri (+ pri 50)))
  193. ;; add 100 for every day beyond the deadline date, and subtract 10 for
  194. ;; every day before it
  195. (let ((slip (- now (1- deadline))))
  196. (if (> slip 0)
  197. (setq pri (+ pri (* slip 100)))
  198. (setq pri (+ pri (* slip 10)))))
  199. pri))
  200. (defun org-habit-get-faces (habit &optional now-days scheduled-days donep)
  201. "Return faces for HABIT relative to NOW-DAYS and SCHEDULED-DAYS.
  202. NOW-DAYS defaults to the current time's days-past-the-epoch if nil.
  203. SCHEDULED-DAYS defaults to the habit's actual scheduled days if nil.
  204. Habits are assigned colors on the following basis:
  205. Blue Task is before the scheduled date.
  206. Green Task is on or after scheduled date, but before the
  207. end of the schedule's repeat period.
  208. Yellow If the task has a deadline, then it is after schedule's
  209. repeat period, but before the deadline.
  210. Orange The task has reached the deadline day, or if there is
  211. no deadline, the end of the schedule's repeat period.
  212. Red The task has gone beyond the deadline day or the
  213. schedule's repeat period."
  214. (let* ((scheduled (or scheduled-days (org-habit-scheduled habit)))
  215. (s-repeat (org-habit-scheduled-repeat habit))
  216. (scheduled-end (+ scheduled (1- s-repeat)))
  217. (d-repeat (org-habit-deadline-repeat habit))
  218. (deadline (if scheduled-days
  219. (+ scheduled-days (- d-repeat s-repeat))
  220. (org-habit-deadline habit)))
  221. (m-days (or now-days (time-to-days (current-time)))))
  222. (cond
  223. ((< m-days scheduled)
  224. '(org-habit-clear-face . org-habit-clear-future-face))
  225. ((< m-days deadline)
  226. '(org-habit-ready-face . org-habit-ready-future-face))
  227. ((= m-days deadline)
  228. (if donep
  229. '(org-habit-ready-face . org-habit-ready-future-face)
  230. '(org-habit-alert-face . org-habit-alert-future-face)))
  231. (t
  232. '(org-habit-overdue-face . org-habit-overdue-future-face)))))
  233. (defun org-habit-build-graph (habit starting current ending)
  234. "Build a graph for the given HABIT, from STARTING to ENDING.
  235. CURRENT gives the current time between STARTING and ENDING, for
  236. the purpose of drawing the graph. It need not be the actual
  237. current time."
  238. (let* ((done-dates (sort (org-habit-done-dates habit) '<))
  239. (scheduled (org-habit-scheduled habit))
  240. (s-repeat (org-habit-scheduled-repeat habit))
  241. (start (time-to-days starting))
  242. (now (time-to-days current))
  243. (end (time-to-days ending))
  244. (graph (make-string (1+ (- end start)) ?\ ))
  245. (index 0)
  246. last-done-date)
  247. (while (and done-dates (< (car done-dates) start))
  248. (setq last-done-date (car done-dates)
  249. done-dates (cdr done-dates)))
  250. (while (< start end)
  251. (let* ((in-the-past-p (< start now))
  252. (todayp (= start now))
  253. (donep (and done-dates
  254. (= start (car done-dates))))
  255. (faces (if (and in-the-past-p
  256. (not last-done-date)
  257. (not (< scheduled now)))
  258. '(org-habit-clear-face . org-habit-clear-future-face)
  259. (org-habit-get-faces
  260. habit start (and in-the-past-p
  261. (if last-done-date
  262. (+ last-done-date s-repeat)
  263. scheduled))
  264. donep)))
  265. markedp face)
  266. (if donep
  267. (let ((done-time (time-add
  268. starting
  269. (days-to-time
  270. (- start (time-to-days starting))))))
  271. (aset graph index ?*)
  272. (setq markedp t)
  273. (put-text-property
  274. index (1+ index) 'help-echo
  275. (format-time-string (org-time-stamp-format) done-time) graph)
  276. (while (and done-dates
  277. (= start (car done-dates)))
  278. (setq last-done-date (car done-dates)
  279. done-dates (cdr done-dates))))
  280. (if todayp
  281. (aset graph index ?!)))
  282. (setq face (if (or in-the-past-p todayp)
  283. (car faces)
  284. (cdr faces)))
  285. (if (and in-the-past-p
  286. (not (eq face 'org-habit-overdue-face))
  287. (not markedp))
  288. (setq face (cdr faces)))
  289. (put-text-property index (1+ index) 'face face graph))
  290. (setq start (1+ start)
  291. index (1+ index)))
  292. graph))
  293. (defun org-habit-insert-consistency-graphs (&optional line)
  294. "Insert consistency graph for any habitual tasks."
  295. (let ((inhibit-read-only t) l c
  296. (buffer-invisibility-spec '(org-link))
  297. (moment (time-subtract (current-time)
  298. (list 0 (* 3600 org-extend-today-until) 0))))
  299. (save-excursion
  300. (goto-char (if line (point-at-bol) (point-min)))
  301. (while (not (eobp))
  302. (let ((habit (get-text-property (point) 'org-habit-p)))
  303. (when habit
  304. (move-to-column org-habit-graph-column t)
  305. (delete-char (min (+ 1 org-habit-preceding-days
  306. org-habit-following-days)
  307. (- (line-end-position) (point))))
  308. (insert (org-habit-build-graph
  309. habit
  310. (time-subtract moment
  311. (days-to-time org-habit-preceding-days))
  312. moment
  313. (time-add moment
  314. (days-to-time org-habit-following-days))))))
  315. (forward-line)))))
  316. (defun org-habit-toggle-habits ()
  317. "Toggle display of habits in an agenda buffer."
  318. (interactive)
  319. (org-agenda-check-type t 'agenda)
  320. (setq org-habit-show-habits (not org-habit-show-habits))
  321. (org-agenda-redo)
  322. (org-agenda-set-mode-name)
  323. (message "Habits turned %s"
  324. (if org-habit-show-habits "on" "off")))
  325. (org-defkey org-agenda-mode-map "K" 'org-habit-toggle-habits)
  326. (provide 'org-habit)
  327. ;; arch-tag: 64e070d9-bd09-4917-bd44-44465f5ed348
  328. ;;; org-habit.el ends here