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