org-depend.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ;;; org-depend.el --- TODO dependencies for Org-mode
  2. ;; Copyright (C) 2008 Free Software Foundation, Inc.
  3. ;;
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 0.08
  8. ;;
  9. ;; This file is not part of GNU Emacs.
  10. ;;
  11. ;; This file 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, or (at your option)
  14. ;; 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; see the file COPYING. If not, write to the
  21. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;; Boston, MA 02110-1301, USA.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; WARNING: This file is just a PROOF OF CONCEPT, not a supported part
  28. ;; of Org-mode.
  29. ;;
  30. ;; This is an example implementation of TODO dependencies in Org-mode.
  31. ;; It uses the new hooks in version 5.13 of Org-mode,
  32. ;; `org-trigger-hook' and `org-blocker-hook'.
  33. ;;
  34. ;; It implements the following:
  35. ;;
  36. ;; Triggering
  37. ;; ----------
  38. ;;
  39. ;; 1) If an entry contains a TRIGGER property that contains the string
  40. ;; "chain-siblings(KEYWORD)", then switching that entry to DONE does
  41. ;; do the following:
  42. ;; - The sibling following this entry switched to todo-state KEYWORD.
  43. ;; - The sibling also gets a TRIGGER property "chain-sibling(KEYWORD)",
  44. ;; property, to make sure that, when *it* is DONE, the chain will
  45. ;; continue.
  46. ;;
  47. ;; 2) If an entry contains a TRIGGER property that contains the string
  48. ;; "chain-siblings-scheduled", then switching that entry to DONE does
  49. ;; the following actions, similarly to "chain-siblings(KEYWORD)":
  50. ;; - The sibling receives the same scheduled time as the entry
  51. ;; marked as DONE (or, in the case, in which there is no scheduled
  52. ;; time, the sibling does not get any either).
  53. ;; - The sibling also gets the same TRIGGER property
  54. ;; "chain-siblings-scheduled", so the chain can continue.
  55. ;;
  56. ;; 3) If the TRIGGER property contains any other words like
  57. ;; XYZ(KEYWORD), these are treated as entry id's with keywords. That
  58. ;; means Org-mode will search for an entry with the ID property XYZ
  59. ;; and switch that entry to KEYWORD as well.
  60. ;;
  61. ;; Blocking
  62. ;; --------
  63. ;;
  64. ;; 1) If an entry contains a BLOCKER property that contains the word
  65. ;; "previous-sibling", the sibling above the current entry is
  66. ;; checked when you try to mark it DONE. If it is still in a TODO
  67. ;; state, the current state change is blocked.
  68. ;;
  69. ;; 2) If the BLOCKER property contains any other words, these are
  70. ;; treated as entry id's. That means Org-mode will search for an
  71. ;; entry with the ID property exactly equal to this word. If any
  72. ;; of these entries is not yet marked DONE, the current state change
  73. ;; will be blocked.
  74. ;;
  75. ;; 3) Whenever a state change is blocked, an org-mark is pushed, so that
  76. ;; you can find the offending entry with `C-c &'.
  77. ;;
  78. ;;; Example:
  79. ;;
  80. ;; When trying this example, make sure that the settings for TODO keywords
  81. ;; have been activated, i.e. include the following line and press C-c C-c
  82. ;; on the line before working with the example:
  83. ;;
  84. ;; #+TYP_TODO: TODO NEXT | DONE
  85. ;;
  86. ;; * TODO Win a million in Las Vegas
  87. ;; The "third" TODO (see above) cannot become a TODO without this money.
  88. ;;
  89. ;; :PROPERTIES:
  90. ;; :ID: I-cannot-do-it-without-money
  91. ;; :END:
  92. ;;
  93. ;; * Do this by doing a chain of TODO's
  94. ;; ** NEXT This is the first in this chain
  95. ;; :PROPERTIES:
  96. ;; :TRIGGER: chain-siblings(NEXT)
  97. ;; :END:
  98. ;;
  99. ;; ** This is the second in this chain
  100. ;;
  101. ;; ** This is the third in this chain
  102. ;; :PROPERTIES:
  103. ;; :BLOCKER: I-cannot-do-it-without-money
  104. ;; :END:
  105. ;;
  106. ;; ** This is the forth in this chain
  107. ;; When this is DONE, we will also trigger entry XYZ-is-my-id
  108. ;; :PROPERTIES:
  109. ;; :TRIGGER: XYZ-is-my-id(TODO)
  110. ;; :END:
  111. ;;
  112. ;; ** This is the fifth in this chain
  113. ;;
  114. ;; * Start writing report
  115. ;; :PROPERTIES:
  116. ;; :ID: XYZ-is-my-id
  117. ;; :END:
  118. ;;
  119. ;;
  120. (require 'org)
  121. (defcustom org-depend-tag-blocked t
  122. "Whether to indicate blocked TODO items by a special tag."
  123. :group 'org
  124. :type 'boolean)
  125. (defmacro org-depend-act-on-sibling (trigger-val &rest rest)
  126. "Perform a set of actions on the next sibling, if it exists,
  127. copying the sibling spec TRIGGER-VAL to the next sibling."
  128. `(catch 'exit
  129. (save-excursion
  130. (goto-char pos)
  131. ;; find the sibling, exit if no more siblings
  132. (condition-case nil
  133. (outline-forward-same-level 1)
  134. (error (throw 'exit t)))
  135. ;; mark the sibling TODO
  136. ,@rest
  137. ;; make sure the sibling will continue the chain
  138. (org-entry-add-to-multivalued-property
  139. nil "TRIGGER" ,trigger-val))))
  140. (defun org-depend-trigger-todo (change-plist)
  141. "Trigger new TODO entries after the current is switched to DONE.
  142. This does two different kinds of triggers:
  143. - If the current entry contains a TRIGGER property that contains
  144. \"chain-siblings(KEYWORD)\", it goes to the next sibling, marks it
  145. KEYWORD and also installs the \"chain-sibling\" trigger to continue
  146. the chain.
  147. - If the current entry contains a TRIGGER property that contains
  148. \"chain-siblings-scheduled\", we go to the next sibling and copy
  149. the scheduled time from the current task, also installing the property
  150. in the sibling.
  151. - Any other word (space-separated) like XYZ(KEYWORD) in the TRIGGER
  152. property is seen as an entry id. Org-mode finds the entry with the
  153. corresponding ID property and switches it to the state TODO as well."
  154. ;; Get information from the plist
  155. (let* ((type (plist-get change-plist :type))
  156. (pos (plist-get change-plist :position))
  157. (from (plist-get change-plist :from))
  158. (to (plist-get change-plist :to))
  159. (org-log-done nil) ; IMPROTANT!: no logging during automatic trigger!
  160. trigger triggers tr p1 kwd)
  161. (catch 'return
  162. (unless (eq type 'todo-state-change)
  163. ;; We are only handling todo-state-change....
  164. (throw 'return t))
  165. (unless (and (member from org-not-done-keywords)
  166. (member to org-done-keywords))
  167. ;; This is not a change from TODO to DONE, ignore it
  168. (throw 'return t))
  169. ;; OK, we just switched from a TODO state to a DONE state
  170. ;; Lets see if this entry has a TRIGGER property.
  171. ;; If yes, split it up on whitespace.
  172. (setq trigger (org-entry-get pos "TRIGGER")
  173. triggers (and trigger (org-split-string trigger "[ \t]+")))
  174. ;; Go through all the triggers
  175. (while (setq tr (pop triggers))
  176. (cond
  177. ((string-match "\\`chain-siblings(\\(.*?\\))\\'" tr)
  178. ;; This is a TODO chain of siblings
  179. (setq kwd (match-string 1 tr))
  180. (org-depend-act-on-sibling (format "chain-siblings(%s)" kwd)
  181. (org-todo kwd)))
  182. ((string-match "\\`\\(\\S-+\\)(\\(.*?\\))\\'" tr)
  183. ;; This seems to be ENTRY_ID(KEYWORD)
  184. (setq id (match-string 1 tr)
  185. kwd (match-string 2 tr)
  186. p1 (org-find-entry-with-id id))
  187. (when p1
  188. ;; there is an entry with this ID, mark it TODO
  189. (save-excursion
  190. (goto-char p1)
  191. (org-todo kwd))))
  192. ((string-match "\\`chain-siblings-scheduled\\'" tr)
  193. (let ((time (org-get-scheduled-time pos)))
  194. (when time
  195. (org-depend-act-on-sibling
  196. "chain-siblings-scheduled"
  197. (org-schedule nil time))))))))))
  198. (defun org-depend-block-todo (change-plist)
  199. "Block turning an entry into a TODO.
  200. This checks for a BLOCKER property in an entry and checks
  201. all the entries listed there. If any of them is not done,
  202. block changing the current entry into a TODO entry. If the property contains
  203. the word \"previous-sibling\", the sibling above the current entry is checked.
  204. Any other words are treated as entry id's. If an entry exists with the
  205. this ID property, that entry is also checked."
  206. ;; Get information from the plist
  207. (let* ((type (plist-get change-plist :type))
  208. (pos (plist-get change-plist :position))
  209. (from (plist-get change-plist :from))
  210. (to (plist-get change-plist :to))
  211. (org-log-done nil) ; IMPROTANT!: no logging during automatic trigger
  212. blocker blockers bl p1
  213. (proceed-p
  214. (catch 'return
  215. (unless (eq type 'todo-state-change)
  216. ;; We are not handling this kind of change
  217. (throw 'return t))
  218. (unless (and (not from) (member to org-not-done-keywords))
  219. ;; This is not a change from nothing to TODO, ignore it
  220. (throw 'return t))
  221. ;; OK, the plan is to switch from nothing to TODO
  222. ;; Lets see if we will allow it. Find the BLOCKER property
  223. ;; and split it on whitespace.
  224. (setq blocker (org-entry-get pos "BLOCKER")
  225. blockers (and blocker (org-split-string blocker "[ \t]+")))
  226. ;; go through all the blockers
  227. (while (setq bl (pop blockers))
  228. (cond
  229. ((equal bl "previous-sibling")
  230. ;; the sibling is required to be DONE.
  231. (catch 'ignore
  232. (save-excursion
  233. (goto-char pos)
  234. ;; find the older sibling, exit if no more siblings
  235. (condition-case nil
  236. (outline-backward-same-level 1)
  237. (error (throw 'ignore t)))
  238. ;; Check if this entry is not yet done and block
  239. (unless (org-entry-is-done-p)
  240. ;; return nil, to indicate that we block the change!
  241. (org-mark-ring-push)
  242. (throw 'return nil)))))
  243. ((setq p1 (org-find-entry-with-id bl))
  244. ;; there is an entry with this ID, check it out
  245. (save-excursion
  246. (goto-char p1)
  247. (unless (org-entry-is-done-p)
  248. ;; return nil, to indicate that we block the change!
  249. (org-mark-ring-push)
  250. (throw 'return nil))))))
  251. t ; return t to indicate that we are not blocking
  252. )))
  253. (when org-depend-tag-blocked
  254. (org-toggle-tag "blocked" (if proceed-p 'off 'on)))
  255. proceed-p))
  256. (add-hook 'org-trigger-hook 'org-depend-trigger-todo)
  257. (add-hook 'org-blocker-hook 'org-depend-block-todo)
  258. (provide 'org-depend)
  259. ;;; org-depend.el ends here