org-depend.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. ;;; org-depend.el --- TODO dependencies for Org-mode
  2. ;; Copyright (C) 2008-2012 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 the string
  57. ;; "chain-find-next(KEYWORD[,OPTIONS])", then switching that entry
  58. ;; to DONE do the following:
  59. ;; - All siblings are of the entry are collected into a temporary
  60. ;; list and then filtered and sorted according to OPTIONS
  61. ;; - The first sibling on the list is changed into KEYWORD state
  62. ;; - The sibling also gets the same TRIGGER property
  63. ;; "chain-find-next", so the chain can continue.
  64. ;;
  65. ;; OPTIONS should be a comma separated string without spaces, and
  66. ;; can contain following options:
  67. ;;
  68. ;; - from-top the candidate list is all of the siblings in
  69. ;; the current subtree
  70. ;;
  71. ;; - from-bottom candidate list are all siblings from bottom up
  72. ;;
  73. ;; - from-current candidate list are all siblings from current item
  74. ;; until end of subtree, then wrapped around from
  75. ;; first sibling
  76. ;;
  77. ;; - no-wrap candidate list are siblings from current one down
  78. ;;
  79. ;; - todo-only Only consider siblings that have a todo keyword
  80. ;; -
  81. ;; - todo-and-done-only
  82. ;; Same as above but also include done items.
  83. ;;
  84. ;; - priority-up sort by highest priority
  85. ;; - priority-down sort by lowest priority
  86. ;; - effort-up sort by highest effort
  87. ;; - effort-down sort by lowest effort
  88. ;;
  89. ;; Default OPTIONS are from-top
  90. ;;
  91. ;;
  92. ;; 4) If the TRIGGER property contains any other words like
  93. ;; XYZ(KEYWORD), these are treated as entry id's with keywords. That
  94. ;; means Org-mode will search for an entry with the ID property XYZ
  95. ;; and switch that entry to KEYWORD as well.
  96. ;;
  97. ;; Blocking
  98. ;; --------
  99. ;;
  100. ;; 1) If an entry contains a BLOCKER property that contains the word
  101. ;; "previous-sibling", the sibling above the current entry is
  102. ;; checked when you try to mark it DONE. If it is still in a TODO
  103. ;; state, the current state change is blocked.
  104. ;;
  105. ;; 2) If the BLOCKER property contains any other words, these are
  106. ;; treated as entry id's. That means Org-mode will search for an
  107. ;; entry with the ID property exactly equal to this word. If any
  108. ;; of these entries is not yet marked DONE, the current state change
  109. ;; will be blocked.
  110. ;;
  111. ;; 3) Whenever a state change is blocked, an org-mark is pushed, so that
  112. ;; you can find the offending entry with `C-c &'.
  113. ;;
  114. ;;; Example:
  115. ;;
  116. ;; When trying this example, make sure that the settings for TODO keywords
  117. ;; have been activated, i.e. include the following line and press C-c C-c
  118. ;; on the line before working with the example:
  119. ;;
  120. ;; #+TYP_TODO: TODO NEXT | DONE
  121. ;;
  122. ;; * TODO Win a million in Las Vegas
  123. ;; The "third" TODO (see above) cannot become a TODO without this money.
  124. ;;
  125. ;; :PROPERTIES:
  126. ;; :ID: I-cannot-do-it-without-money
  127. ;; :END:
  128. ;;
  129. ;; * Do this by doing a chain of TODO's
  130. ;; ** NEXT This is the first in this chain
  131. ;; :PROPERTIES:
  132. ;; :TRIGGER: chain-siblings(NEXT)
  133. ;; :END:
  134. ;;
  135. ;; ** This is the second in this chain
  136. ;;
  137. ;; ** This is the third in this chain
  138. ;; :PROPERTIES:
  139. ;; :BLOCKER: I-cannot-do-it-without-money
  140. ;; :END:
  141. ;;
  142. ;; ** This is the forth in this chain
  143. ;; When this is DONE, we will also trigger entry XYZ-is-my-id
  144. ;; :PROPERTIES:
  145. ;; :TRIGGER: XYZ-is-my-id(TODO)
  146. ;; :END:
  147. ;;
  148. ;; ** This is the fifth in this chain
  149. ;;
  150. ;; * Start writing report
  151. ;; :PROPERTIES:
  152. ;; :ID: XYZ-is-my-id
  153. ;; :END:
  154. ;;
  155. ;;
  156. (require 'org)
  157. (eval-when-compile
  158. (require 'cl))
  159. (defcustom org-depend-tag-blocked t
  160. "Whether to indicate blocked TODO items by a special tag."
  161. :group 'org
  162. :type 'boolean)
  163. (defcustom org-depend-find-next-options
  164. "from-current,todo-only,priority-up"
  165. "Default options for chain-find-next trigger"
  166. :group 'org
  167. :type 'string)
  168. (defmacro org-depend-act-on-sibling (trigger-val &rest rest)
  169. "Perform a set of actions on the next sibling, if it exists,
  170. copying the sibling spec TRIGGER-VAL to the next sibling."
  171. `(catch 'exit
  172. (save-excursion
  173. (goto-char pos)
  174. ;; find the sibling, exit if no more siblings
  175. (condition-case nil
  176. (outline-forward-same-level 1)
  177. (error (throw 'exit t)))
  178. ;; mark the sibling TODO
  179. ,@rest
  180. ;; make sure the sibling will continue the chain
  181. (org-entry-add-to-multivalued-property
  182. nil "TRIGGER" ,trigger-val))))
  183. (defvar org-depend-doing-chain-find-next nil)
  184. (defun org-depend-trigger-todo (change-plist)
  185. "Trigger new TODO entries after the current is switched to DONE.
  186. This does two different kinds of triggers:
  187. - If the current entry contains a TRIGGER property that contains
  188. \"chain-siblings(KEYWORD)\", it goes to the next sibling, marks it
  189. KEYWORD and also installs the \"chain-sibling\" trigger to continue
  190. the chain.
  191. - If the current entry contains a TRIGGER property that contains
  192. \"chain-siblings-scheduled\", we go to the next sibling and copy
  193. the scheduled time from the current task, also installing the property
  194. in the sibling.
  195. - Any other word (space-separated) like XYZ(KEYWORD) in the TRIGGER
  196. property is seen as an entry id. Org-mode finds the entry with the
  197. corresponding ID property and switches it to the state TODO as well."
  198. ;; Get information from the plist
  199. (let* ((type (plist-get change-plist :type))
  200. (pos (plist-get change-plist :position))
  201. (from (plist-get change-plist :from))
  202. (to (plist-get change-plist :to))
  203. (org-log-done nil) ; IMPROTANT!: no logging during automatic trigger!
  204. trigger triggers tr p1 kwd)
  205. (catch 'return
  206. (unless (eq type 'todo-state-change)
  207. ;; We are only handling todo-state-change....
  208. (throw 'return t))
  209. (unless (and (member from org-not-done-keywords)
  210. (member to org-done-keywords))
  211. ;; This is not a change from TODO to DONE, ignore it
  212. (throw 'return t))
  213. ;; OK, we just switched from a TODO state to a DONE state
  214. ;; Lets see if this entry has a TRIGGER property.
  215. ;; If yes, split it up on whitespace.
  216. (setq trigger (org-entry-get pos "TRIGGER")
  217. triggers (and trigger (org-split-string trigger "[ \t]+")))
  218. ;; Go through all the triggers
  219. (while (setq tr (pop triggers))
  220. (cond
  221. ((and (not org-depend-doing-chain-find-next)
  222. (string-match "\\`chain-find-next(\\b\\(.+?\\)\\b\\(.*\\))\\'" tr))
  223. ;; smarter sibling selection
  224. (let* ((org-depend-doing-chain-find-next t)
  225. (kwd (match-string 1 tr))
  226. (options (match-string 2 tr))
  227. (options (if (or (null options)
  228. (equal options ""))
  229. org-depend-find-next-options
  230. options))
  231. (todo-only (string-match "todo-only" options))
  232. (todo-and-done-only (string-match "todo-and-done-only"
  233. options))
  234. (from-top (string-match "from-top" options))
  235. (from-bottom (string-match "from-bottom" options))
  236. (from-current (string-match "from-current" options))
  237. (no-wrap (string-match "no-wrap" options))
  238. (priority-up (string-match "priority-up" options))
  239. (priority-down (string-match "priority-down" options))
  240. (effort-up (string-match "effort-up" options))
  241. (effort-down (string-match "effort-down" options)))
  242. (save-excursion
  243. (org-back-to-heading t)
  244. (let ((this-item (point)))
  245. ;; go up to the parent headline, then advance to next child
  246. (org-up-heading-safe)
  247. (let ((end (save-excursion (org-end-of-subtree t)
  248. (point)))
  249. (done nil)
  250. (items '()))
  251. (outline-next-heading)
  252. (while (not done)
  253. (if (not (looking-at org-complex-heading-regexp))
  254. (setq done t)
  255. (let ((todo-kwd (match-string 2))
  256. (tags (match-string 5))
  257. (priority (org-get-priority (or (match-string 3) "")))
  258. (effort (when (or effort-up effort-down)
  259. (let ((effort (org-get-effort)))
  260. (when effort
  261. (org-duration-string-to-minutes effort))))))
  262. (push (list (point) todo-kwd priority tags effort)
  263. items))
  264. (unless (org-goto-sibling)
  265. (setq done t))))
  266. ;; massage the list according to options
  267. (setq items
  268. (cond (from-top (nreverse items))
  269. (from-bottom items)
  270. ((or from-current no-wrap)
  271. (let* ((items (nreverse items))
  272. (pos (position this-item items :key #'first))
  273. (items-before (subseq items 0 pos))
  274. (items-after (subseq items pos)))
  275. (if no-wrap items-after
  276. (append items-after items-before))))
  277. (t (nreverse items))))
  278. (setq items (remove-if
  279. (lambda (item)
  280. (or (equal (first item) this-item)
  281. (and (not todo-and-done-only)
  282. (member (second item) org-done-keywords))
  283. (and (or todo-only
  284. todo-and-done-only)
  285. (null (second item)))))
  286. items))
  287. (setq items
  288. (sort
  289. items
  290. (lambda (item1 item2)
  291. (let* ((p1 (third item1))
  292. (p2 (third item2))
  293. (e1 (fifth item1))
  294. (e2 (fifth item2))
  295. (p1-lt (< p1 p2))
  296. (p1-gt (> p1 p2))
  297. (e1-lt (and e1 (or (not e2) (< e1 e2))))
  298. (e2-gt (and e2 (or (not e1) (> e1 e2)))))
  299. (cond (priority-up
  300. (or p1-gt
  301. (and (equal p1 p2)
  302. (or (and effort-up e1-gt)
  303. (and effort-down e1-lt)))))
  304. (priority-down
  305. (or p1-lt
  306. (and (equal p1 p2)
  307. (or (and effort-up e1-gt)
  308. (and effort-down e1-lt)))))
  309. (effort-up
  310. (or e1-gt (and (equal e1 e2) p1-gt)))
  311. (effort-down
  312. (or e1-lt (and (equal e1 e2) p1-gt))))))))
  313. (when items
  314. (goto-char (first (first items)))
  315. (org-entry-add-to-multivalued-property nil "TRIGGER" tr)
  316. (org-todo kwd)))))))
  317. ((string-match "\\`chain-siblings(\\(.*?\\))\\'" tr)
  318. ;; This is a TODO chain of siblings
  319. (setq kwd (match-string 1 tr))
  320. (org-depend-act-on-sibling (format "chain-siblings(%s)" kwd)
  321. (org-todo kwd)))
  322. ((string-match "\\`\\(\\S-+\\)(\\(.*?\\))\\'" tr)
  323. ;; This seems to be ENTRY_ID(KEYWORD)
  324. (setq id (match-string 1 tr)
  325. kwd (match-string 2 tr)
  326. p1 (org-find-entry-with-id id))
  327. (when p1
  328. ;; there is an entry with this ID, mark it TODO
  329. (save-excursion
  330. (goto-char p1)
  331. (org-todo kwd))))
  332. ((string-match "\\`chain-siblings-scheduled\\'" tr)
  333. (let ((time (org-get-scheduled-time pos)))
  334. (when time
  335. (org-depend-act-on-sibling
  336. "chain-siblings-scheduled"
  337. (org-schedule nil time))))))))))
  338. (defun org-depend-block-todo (change-plist)
  339. "Block turning an entry into a TODO.
  340. This checks for a BLOCKER property in an entry and checks
  341. all the entries listed there. If any of them is not done,
  342. block changing the current entry into a TODO entry. If the property contains
  343. the word \"previous-sibling\", the sibling above the current entry is checked.
  344. Any other words are treated as entry id's. If an entry exists with the
  345. this ID property, that entry is also checked."
  346. ;; Get information from the plist
  347. (let* ((type (plist-get change-plist :type))
  348. (pos (plist-get change-plist :position))
  349. (from (plist-get change-plist :from))
  350. (to (plist-get change-plist :to))
  351. (org-log-done nil) ; IMPROTANT!: no logging during automatic trigger
  352. blocker blockers bl p1
  353. (proceed-p
  354. (catch 'return
  355. ;; If this is not a todo state change, or if this entry is
  356. ;; DONE, do not block
  357. (when (or (not (eq type 'todo-state-change))
  358. (member from (cons 'done org-done-keywords))
  359. (member to (cons 'todo org-not-done-keywords))
  360. (not to))
  361. (throw 'return t))
  362. ;; OK, the plan is to switch from nothing to TODO
  363. ;; Lets see if we will allow it. Find the BLOCKER property
  364. ;; and split it on whitespace.
  365. (setq blocker (org-entry-get pos "BLOCKER")
  366. blockers (and blocker (org-split-string blocker "[ \t]+")))
  367. ;; go through all the blockers
  368. (while (setq bl (pop blockers))
  369. (cond
  370. ((equal bl "previous-sibling")
  371. ;; the sibling is required to be DONE.
  372. (catch 'ignore
  373. (save-excursion
  374. (goto-char pos)
  375. ;; find the older sibling, exit if no more siblings
  376. (condition-case nil
  377. (outline-backward-same-level 1)
  378. (error (throw 'ignore t)))
  379. ;; Check if this entry is not yet done and block
  380. (unless (org-entry-is-done-p)
  381. ;; return nil, to indicate that we block the change!
  382. (org-mark-ring-push)
  383. (throw 'return nil)))))
  384. ((setq p1 (org-find-entry-with-id bl))
  385. ;; there is an entry with this ID, check it out
  386. (save-excursion
  387. (goto-char p1)
  388. (unless (org-entry-is-done-p)
  389. ;; return nil, to indicate that we block the change!
  390. (org-mark-ring-push)
  391. (throw 'return nil))))))
  392. t ; return t to indicate that we are not blocking
  393. )))
  394. (when org-depend-tag-blocked
  395. (org-toggle-tag "blocked" (if proceed-p 'off 'on)))
  396. proceed-p))
  397. (add-hook 'org-trigger-hook 'org-depend-trigger-todo)
  398. (add-hook 'org-blocker-hook 'org-depend-block-todo)
  399. (provide 'org-depend)
  400. ;;; org-depend.el ends here