org-compat.el (org-file-equal-p): New compatibility function
[org-mode.git] / contrib / oldexp / org-taskjuggler.el
blobaebdd3d18e8acd66ff415f7a622666222834ced2
1 ;;; org-taskjuggler.el --- TaskJuggler exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: org-taskjuggler.el
7 ;; Author: Christian Egli
8 ;; Maintainer: Christian Egli
9 ;; Keywords: org, taskjuggler, project planning
10 ;; Description: Converts an org-mode buffer into a taskjuggler project plan
11 ;; URL:
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Commentary:
30 ;; This library implements a TaskJuggler exporter for org-mode.
31 ;; TaskJuggler uses a text format to define projects, tasks and
32 ;; resources, so it is a natural fit for org-mode. It can produce all
33 ;; sorts of reports for tasks or resources in either HTML, CSV or PDF.
34 ;; The current version of TaskJuggler requires KDE but the next
35 ;; version is implemented in Ruby and should therefore run on any
36 ;; platform.
38 ;; The exporter is a bit different from other exporters, such as the
39 ;; HTML and LaTeX exporters for example, in that it does not export
40 ;; all the nodes of a document or strictly follow the order of the
41 ;; nodes in the document.
43 ;; Instead the TaskJuggler exporter looks for a tree that defines the
44 ;; tasks and a optionally tree that defines the resources for this
45 ;; project. It then creates a TaskJuggler file based on these trees
46 ;; and the attributes defined in all the nodes.
48 ;; * Installation
50 ;; Put this file into your load-path and the following line into your
51 ;; ~/.emacs:
53 ;; (require 'org-taskjuggler)
55 ;; The interactive functions are similar to those of the HTML and LaTeX
56 ;; exporters:
58 ;; M-x `org-export-as-taskjuggler'
59 ;; M-x `org-export-as-taskjuggler-and-open'
61 ;; * Tasks
63 ;; Let's illustrate the usage with a small example. Create your tasks
64 ;; as you usually do with org-mode. Assign efforts to each task using
65 ;; properties (it's easiest to do this in the column view). You should
66 ;; end up with something similar to the example by Peter Jones in
67 ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
68 ;; Now mark the top node of your tasks with a tag named
69 ;; "taskjuggler_project" (or whatever you customized
70 ;; `org-export-taskjuggler-project-tag' to). You are now ready to
71 ;; export the project plan with `org-export-as-taskjuggler-and-open'
72 ;; which will export the project plan and open a Gantt chart in
73 ;; TaskJugglerUI.
75 ;; * Resources
77 ;; Next you can define resources and assign those to work on specific
78 ;; tasks. You can group your resources hierarchically. Tag the top
79 ;; node of the resources with "taskjuggler_resource" (or whatever you
80 ;; customized `org-export-taskjuggler-resource-tag' to). You can
81 ;; optionally assign an identifier (named "resource_id") to the
82 ;; resources (using the standard org properties commands) or you can
83 ;; let the exporter generate identifiers automatically (the exporter
84 ;; picks the first word of the headline as the identifier as long as
85 ;; it is unique, see the documentation of
86 ;; `org-taskjuggler-get-unique-id'). Using that identifier you can
87 ;; then allocate resources to tasks. This is again done with the
88 ;; "allocate" property on the tasks. Do this in column view or when on
89 ;; the task type
91 ;; C-c C-x p allocate RET <resource_id> RET
93 ;; Once the allocations are done you can again export to TaskJuggler
94 ;; and check in the Resource Allocation Graph which person is working
95 ;; on what task at what time.
97 ;; * Export of properties
99 ;; The exporter also takes TODO state information into consideration,
100 ;; i.e. if a task is marked as done it will have the corresponding
101 ;; attribute in TaskJuggler ("complete 100"). Also it will export any
102 ;; property on a task resource or resource node which is known to
103 ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
104 ;; journalentry, rate for resources or account, start, note, duration,
105 ;; end, journalentry, milestone, reference, responsible, scheduling,
106 ;; etc for tasks.
108 ;; * Dependencies
110 ;; The exporter will handle dependencies that are defined in the tasks
111 ;; either with the ORDERED attribute (see TODO dependencies in the Org
112 ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
113 ;; alternatively with a depends attribute. Both the BLOCKER and the
114 ;; depends attribute can be either "previous-sibling" or a reference
115 ;; to an identifier (named "task_id") which is defined for another
116 ;; task in the project. BLOCKER and the depends attribute can define
117 ;; multiple dependencies separated by either space or comma. You can
118 ;; also specify optional attributes on the dependency by simply
119 ;; appending it. The following examples should illustrate this:
121 ;; * Training material
122 ;; :PROPERTIES:
123 ;; :task_id: training_material
124 ;; :ORDERED: t
125 ;; :END:
126 ;; ** Markup Guidelines
127 ;; :PROPERTIES:
128 ;; :Effort: 2d
129 ;; :END:
130 ;; ** Workflow Guidelines
131 ;; :PROPERTIES:
132 ;; :Effort: 2d
133 ;; :END:
134 ;; * Presentation
135 ;; :PROPERTIES:
136 ;; :Effort: 2d
137 ;; :BLOCKER: training_material { gapduration 1d } some_other_task
138 ;; :END:
140 ;;;; * TODO
141 ;; - Look at org-file-properties, org-global-properties and
142 ;; org-global-properties-fixed
143 ;; - What about property inheritance and org-property-inherit-p?
144 ;; - Use TYPE_TODO as an way to assign resources
145 ;; - Make sure multiple dependency definitions (i.e. BLOCKER on
146 ;; previous-sibling and on a specific task_id) in multiple
147 ;; attributes are properly exported.
149 ;;; Code:
151 (eval-when-compile
152 (require 'cl))
154 (require 'org)
155 (require 'org-exp)
157 ;;; User variables:
159 (defgroup org-export-taskjuggler nil
160 "Options for exporting Org-mode files to TaskJuggler."
161 :tag "Org Export TaskJuggler"
162 :group 'org-export)
164 (defcustom org-export-taskjuggler-extension ".tjp"
165 "Extension of TaskJuggler files."
166 :group 'org-export-taskjuggler
167 :version "24.1"
168 :type 'string)
170 (defcustom org-export-taskjuggler-project-tag "taskjuggler_project"
171 "Tag, property or todo used to find the tree containing all
172 the tasks for the project."
173 :group 'org-export-taskjuggler
174 :version "24.1"
175 :type 'string)
177 (defcustom org-export-taskjuggler-resource-tag "taskjuggler_resource"
178 "Tag, property or todo used to find the tree containing all the
179 resources for the project."
180 :group 'org-export-taskjuggler
181 :version "24.1"
182 :type 'string)
184 (defcustom org-export-taskjuggler-report-tag "taskjuggler_report"
185 "Tag, property or todo used to find the tree containing all the
186 reports for the project."
187 :group 'org-export-taskjuggler
188 :version "24.1"
189 :type 'string)
191 (defcustom org-export-taskjuggler-target-version 2.4
192 "Which version of TaskJuggler the exporter is targeting."
193 :group 'org-export-taskjuggler
194 :version "24.1"
195 :type 'number)
197 (defcustom org-export-taskjuggler-default-project-version "1.0"
198 "Default version string for the project."
199 :group 'org-export-taskjuggler
200 :version "24.1"
201 :type 'string)
203 (defcustom org-export-taskjuggler-default-project-duration 280
204 "Default project duration if no start and end date have been defined
205 in the root node of the task tree, i.e. the tree that has been marked
206 with `org-export-taskjuggler-project-tag'"
207 :group 'org-export-taskjuggler
208 :version "24.1"
209 :type 'integer)
211 (defcustom org-export-taskjuggler-default-reports
212 '("taskreport \"Gantt Chart\" {
213 headline \"Project Gantt Chart\"
214 columns hierarchindex, name, start, end, effort, duration, completed, chart
215 timeformat \"%Y-%m-%d\"
216 hideresource 1
217 loadunit shortauto
219 "resourcereport \"Resource Graph\" {
220 headline \"Resource Allocation Graph\"
221 columns no, name, utilization, freeload, chart
222 loadunit shortauto
223 sorttasks startup
224 hidetask ~isleaf()
226 "Default reports for the project."
227 :group 'org-export-taskjuggler
228 :version "24.1"
229 :type '(repeat (string :tag "Report")))
231 (defcustom org-export-taskjuggler-default-global-header
233 "Default global header for the project. This goes before
234 project declaration, and might be useful for early macros"
235 :group 'org-export-taskjuggler
236 :version "24.1"
237 :type '(string :tag "Preamble"))
239 (defcustom org-export-taskjuggler-default-global-properties
240 "shift s40 \"Part time shift\" {
241 workinghours wed, thu, fri off
244 "Default global properties for the project. Here you typically
245 define global properties such as shifts, accounts, rates,
246 vacation, macros and flags. Any property that is allowed within
247 the TaskJuggler file can be inserted. You could for example
248 include another TaskJuggler file.
250 The global properties are inserted after the project declaration
251 but before any resource and task declarations."
252 :group 'org-export-taskjuggler
253 :version "24.1"
254 :type '(string :tag "Preamble"))
256 (defcustom org-export-taskjuggler-valid-task-attributes
257 '(account start note duration endbuffer endcredit end
258 flags journalentry length limits maxend maxstart minend
259 minstart period reference responsible scheduling
260 startbuffer startcredit statusnote chargeset charge)
261 "Valid attributes for Taskjuggler tasks. If one of these
262 appears as a property for a headline, it will be exported with
263 the corresponding task."
264 :group 'org-export-taskjuggler)
266 (defcustom org-export-taskjuggler-valid-resource-attributes
267 '(limits vacation shift booking efficiency journalentry rate
268 workinghours flags)
269 "Valid attributes for Taskjuggler resources. If one of these
270 appears as a property for a headline, it will be exported with
271 the corresponding resource."
272 :group 'org-export-taskjuggler)
274 (defcustom org-export-taskjuggler-valid-report-attributes
275 '(headline columns definitions timeformat hideresource hidetask
276 loadunit sorttasks formats period)
277 "Valid attributes for Taskjuggler reports. If one of these
278 appears as a property for a headline, it will be exported with
279 the corresponding report."
280 :group 'org-export-taskjuggler)
282 (defcustom org-export-taskjuggler-keep-project-as-task t
283 "Whether to keep the project headline as an umbrella task for
284 all declared tasks. Setting this to nil will allow maintaining
285 completely separated task buckets, while still sharing the same
286 resources pool."
287 :group 'org-export-taskjuggler
288 :type 'boolean)
290 ;;; Hooks
292 (defvar org-export-taskjuggler-final-hook nil
293 "Hook run at the end of TaskJuggler export, in the new buffer.")
295 ;;; Autoload functions:
297 ;; avoid compiler warning about free variable
298 (defvar org-export-taskjuggler-old-level)
300 (defun org-export-as-taskjuggler (&optional arg hidden ext-plist
301 to-buffer body-only pub-dir)
302 "Export parts of the current buffer as a TaskJuggler file.
303 The exporter looks for a tree with tag, property or todo that
304 matches `org-export-taskjuggler-project-tag' and takes this as
305 the tasks for this project. The first node of this tree defines
306 the project properties such as project name and project period.
307 If there is a tree with tag, property or todo that matches
308 `org-export-taskjuggler-resource-tag' this three is taken as
309 resources for the project. If no resources are specified, a
310 default resource is created and allocated to the project. Also
311 the taskjuggler project will be created with default reports as
312 defined in `org-export-taskjuggler-default-reports'."
313 (interactive "P")
315 (message "Exporting...")
316 (setq-default org-done-keywords org-done-keywords)
317 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
318 ext-plist
319 (org-infile-export-plist)))
320 (org-export-opt-plist opt-plist)
321 (tasks
322 (org-taskjuggler-resolve-dependencies
323 (org-taskjuggler-assign-task-ids
324 (org-taskjuggler-compute-task-leafiness
325 (org-map-entries
326 'org-taskjuggler-components
327 org-export-taskjuggler-project-tag nil 'archive 'comment)))))
328 (resources
329 (org-taskjuggler-assign-resource-ids
330 (org-map-entries
331 'org-taskjuggler-components
332 org-export-taskjuggler-resource-tag nil 'archive 'comment)))
333 (reports
334 (org-map-entries
335 'org-taskjuggler-components
336 org-export-taskjuggler-report-tag nil 'archive 'comment))
337 (filename (if to-buffer
339 (concat (file-name-as-directory
340 (or pub-dir
341 (org-export-directory :tj opt-plist)))
342 (file-name-sans-extension
343 (file-name-nondirectory buffer-file-name))
344 org-export-taskjuggler-extension)))
345 (buffer (if to-buffer
346 (cond
347 ((eq to-buffer 'string)
348 (get-buffer-create "*Org Taskjuggler Export*"))
349 (t (get-buffer-create to-buffer)))
350 (find-file-noselect filename)))
351 (old-buffer (current-buffer))
352 (org-export-taskjuggler-old-level 0)
353 task resource)
354 (unless tasks
355 (error "No tasks specified"))
356 ;; add a default resource
357 (unless resources
358 (setq resources
359 `((("resource_id" . ,(user-login-name))
360 ("HEADLINE" . ,user-full-name)
361 ("level" . 1)))))
362 ;; add a default allocation to the first task if none was given
363 (unless (assoc "allocate" (car tasks))
364 (let ((task (car tasks))
365 (resource-id (cdr (assoc "resource_id" (car resources)))))
366 (setcar tasks (push (cons "allocate" resource-id) task))))
367 ;; add a default start date to the first task if none was given
368 (unless (assoc "start" (car tasks))
369 (let ((task (car tasks))
370 (time-string (format-time-string "%Y-%m-%d")))
371 (setcar tasks (push (cons "start" time-string) task))))
372 ;; add a default version if none was given
373 (unless (assoc "version" (car tasks))
374 (let ((task (car tasks))
375 (version org-export-taskjuggler-default-project-version))
376 (setcar tasks (push (cons "version" version) task))))
377 (with-current-buffer buffer
378 (erase-buffer)
379 (org-install-letbind)
380 ;; create local variables for all options, to make sure all called
381 ;; functions get the correct information
382 (mapc (lambda (x)
383 (set (make-local-variable (nth 2 x))
384 (plist-get opt-plist (car x))))
385 org-export-plist-vars)
387 (org-clone-local-variables old-buffer "^org-")
388 (insert org-export-taskjuggler-default-global-header)
389 (org-taskjuggler-open-project
390 (if org-export-taskjuggler-keep-project-as-task
391 (car tasks)
392 (pop tasks)))
393 (insert org-export-taskjuggler-default-global-properties)
394 (insert "\n")
395 (dolist (resource resources)
396 (let ((level (cdr (assoc "level" resource))))
397 (org-taskjuggler-close-maybe level)
398 (org-taskjuggler-open-resource resource)
399 (setq org-export-taskjuggler-old-level level)))
400 (org-taskjuggler-close-maybe 1)
401 (setq org-export-taskjuggler-old-level 0)
402 (dolist (task tasks)
403 (let ((level (cdr (assoc "level" task))))
404 (org-taskjuggler-close-maybe level)
405 (org-taskjuggler-open-task task)
406 (setq org-export-taskjuggler-old-level level)))
407 (org-taskjuggler-close-maybe
408 (if org-export-taskjuggler-keep-project-as-task
409 1 2))
410 (org-taskjuggler-insert-reports reports)
411 (or to-buffer (save-buffer))
412 (or (org-export-push-to-kill-ring "TaskJuggler")
413 (message "Exporting... done"))
414 (if (eq to-buffer 'string)
415 (prog1 (buffer-substring (point-min) (point-max))
416 (kill-buffer (current-buffer)))
417 (current-buffer)))))
419 (defun org-export-as-taskjuggler-and-open ()
420 "Export the current buffer as a TaskJuggler file and open it
421 with the TaskJuggler GUI."
422 (interactive)
423 (let* ((file-name (buffer-file-name (org-export-as-taskjuggler)))
424 (process-name "TaskJugglerUI")
425 (command (concat process-name " " file-name)))
426 (start-process-shell-command process-name nil command)))
428 (defun org-taskjuggler-targeting-tj3-p ()
429 "Return true if we are targeting TaskJuggler III."
430 (>= org-export-taskjuggler-target-version 3.0))
432 (defun org-taskjuggler-parent-is-ordered-p ()
433 "Return true if the parent of the current node has a property
434 \"ORDERED\". Return nil otherwise."
435 (save-excursion
436 (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
438 (defun org-taskjuggler-date (date)
439 (let ((time (parse-time-string date)))
440 (format "%d-%02d-%02d" (nth 5 time) (nth 4 time) (nth 3 time))))
442 (defun org-taskjuggler-components ()
443 "Return an alist containing all the pertinent information for
444 the current node such as the headline, the level, todo state
445 information, all the properties, etc."
446 (let* ((props (org-entry-properties))
447 (components (org-heading-components))
448 (level (nth 1 components))
449 (headline
450 (replace-regexp-in-string
451 "\"" "\\\"" (nth 4 components) t t)) ; quote double quotes in headlines
452 (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
453 (let ((scheduled (assoc "SCHEDULED" props))
454 (deadline (assoc "DEADLINE" props)))
455 (when scheduled
456 (push (cons "start" (org-taskjuggler-date (cdr scheduled))) props))
457 (when deadline
458 (push (cons "end" (org-taskjuggler-date (cdr deadline))) props)))
459 (push (cons "level" level) props)
460 (push (cons "HEADLINE" headline) props)
461 (push (cons "parent-ordered" parent-ordered) props)))
463 (defun org-taskjuggler-assign-task-ids (tasks)
464 "Given a list of tasks return the same list assigning a unique id
465 and the full path to each task. Taskjuggler takes hierarchical ids.
466 For that reason we have to make ids locally unique and we have to keep
467 a path to the current task."
468 (let ((previous-level 0)
469 unique-ids unique-id
470 path
471 task resolved-tasks tmp)
472 (dolist (task tasks resolved-tasks)
473 (let ((level (cdr (assoc "level" task))))
474 (cond
475 ((< previous-level level)
476 (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
477 (dotimes (tmp (- level previous-level))
478 (push (list unique-id) unique-ids)
479 (push unique-id path)))
480 ((= previous-level level)
481 (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
482 (push unique-id (car unique-ids))
483 (setcar path unique-id))
484 ((> previous-level level)
485 (dotimes (tmp (- previous-level level))
486 (pop unique-ids)
487 (pop path))
488 (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
489 (push unique-id (car unique-ids))
490 (setcar path unique-id)))
491 (push (cons "unique-id" unique-id) task)
492 (push (cons "path"
493 (mapconcat 'identity
494 (if org-export-taskjuggler-keep-project-as-task
495 (reverse path)
496 (cdr (reverse path))) ".")) task)
497 (setq previous-level level)
498 (setq resolved-tasks (append resolved-tasks (list task)))))))
500 (defun org-taskjuggler-compute-task-leafiness (tasks)
501 "Figure out if each task is a leaf by looking at it's level,
502 and the level of its successor. If the successor is higher (ie
503 deeper), then it's not a leaf."
504 (let (new-list)
505 (while (car tasks)
506 (let ((task (car tasks))
507 (successor (car (cdr tasks))))
508 (cond
509 ;; if a task has no successors it is a leaf
510 ((null successor)
511 (push (cons (cons "leaf-node" t) task) new-list))
512 ;; if the successor has a lower level than task it is a leaf
513 ((<= (cdr (assoc "level" successor)) (cdr (assoc "level" task)))
514 (push (cons (cons "leaf-node" t) task) new-list))
515 ;; otherwise examine the rest of the tasks
516 (t (push task new-list))))
517 (setq tasks (cdr tasks)))
518 (nreverse new-list)))
520 (defun org-taskjuggler-assign-resource-ids (resources)
521 "Given a list of resources return the same list, assigning a
522 unique id to each resource."
523 (let (unique-ids new-list)
524 (dolist (resource resources new-list)
525 (let ((unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
526 (push (cons "unique-id" unique-id) resource)
527 (push unique-id unique-ids)
528 (push resource new-list)))
529 (nreverse new-list)))
531 (defun org-taskjuggler-resolve-dependencies (tasks)
532 (let ((previous-level 0)
533 siblings
534 task resolved-tasks)
535 (dolist (task tasks resolved-tasks)
536 (let* ((level (cdr (assoc "level" task)))
537 (depends (cdr (assoc "depends" task)))
538 (parent-ordered (cdr (assoc "parent-ordered" task)))
539 (blocker (cdr (assoc "BLOCKER" task)))
540 (blocked-on-previous
541 (and blocker (string-match "previous-sibling" blocker)))
542 (dependencies
543 (org-taskjuggler-resolve-explicit-dependencies
544 (append
545 (and depends (org-taskjuggler-tokenize-dependencies depends))
546 (and blocker (org-taskjuggler-tokenize-dependencies blocker)))
547 tasks))
548 previous-sibling)
549 ; update previous sibling info
550 (cond
551 ((< previous-level level)
552 (dotimes (tmp (- level previous-level))
553 (push task siblings)))
554 ((= previous-level level)
555 (setq previous-sibling (car siblings))
556 (setcar siblings task))
557 ((> previous-level level)
558 (dotimes (tmp (- previous-level level))
559 (pop siblings))
560 (setq previous-sibling (car siblings))
561 (setcar siblings task)))
562 ; insert a dependency on previous sibling if the parent is
563 ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
564 (when (or (and previous-sibling parent-ordered) blocked-on-previous)
565 (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
566 ; store dependency information
567 (when dependencies
568 (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
569 (setq previous-level level)
570 (setq resolved-tasks (append resolved-tasks (list task)))))))
572 (defun org-taskjuggler-tokenize-dependencies (dependencies)
573 "Split a dependency property value DEPENDENCIES into the
574 individual dependencies and return them as a list while keeping
575 the optional arguments (such as gapduration) for the
576 dependencies. A dependency will have to match `[-a-zA-Z0-9_]+'."
577 (cond
578 ((string-match "^ *$" dependencies) nil)
579 ((string-match "^[ \t]*\\([-a-zA-Z0-9_]+\\([ \t]*{[^}]+}\\)?\\)[ \t,]*" dependencies)
580 (cons
581 (substring dependencies (match-beginning 1) (match-end 1))
582 (org-taskjuggler-tokenize-dependencies (substring dependencies (match-end 0)))))
583 (t (error (format "invalid dependency id %s" dependencies)))))
585 (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
586 "For each dependency in DEPENDENCIES try to find a
587 corresponding task with a matching property \"task_id\" in TASKS.
588 Return a list containing the resolved links for all DEPENDENCIES
589 where a matching tasks was found. If the dependency is
590 \"previous-sibling\" it is ignored (as this is dealt with in
591 `org-taskjuggler-resolve-dependencies'). If there is no matching
592 task the dependency is ignored and a warning is displayed ."
593 (unless (null dependencies)
594 (let*
595 ;; the dependency might have optional attributes such as "{
596 ;; gapduration 5d }", so only use the first string as id for the
597 ;; dependency
598 ((dependency (car dependencies))
599 (id (car (split-string dependency)))
600 (optional-attributes
601 (mapconcat 'identity (cdr (split-string dependency)) " "))
602 (path (org-taskjuggler-find-task-with-id id tasks)))
603 (cond
604 ;; ignore previous sibling dependencies
605 ((equal (car dependencies) "previous-sibling")
606 (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
607 ;; if the id is found in another task use its path
608 ((not (null path))
609 (cons (mapconcat 'identity (list path optional-attributes) " ")
610 (org-taskjuggler-resolve-explicit-dependencies
611 (cdr dependencies) tasks)))
612 ;; warn about dangling dependency but otherwise ignore it
613 (t (display-warning
614 'org-export-taskjuggler
615 (format "No task with matching property \"task_id\" found for id %s" id))
616 (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))))))
618 (defun org-taskjuggler-find-task-with-id (id tasks)
619 "Find ID in tasks. If found return the path of task. Otherwise
620 return nil."
621 (let ((task-id (cdr (assoc "task_id" (car tasks))))
622 (path (cdr (assoc "path" (car tasks)))))
623 (cond
624 ((null tasks) nil)
625 ((equal task-id id) path)
626 (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
628 (defun org-taskjuggler-get-unique-id (item unique-ids)
629 "Return a unique id for an ITEM which can be a task or a resource.
630 The id is derived from the headline and made unique against
631 UNIQUE-IDS. If the (downcased) first token of the headline is not
632 unique try to add more (downcased) tokens of the headline or
633 finally add more underscore characters (\"_\")."
634 (let* ((headline (cdr (assoc "HEADLINE" item)))
635 (parts (split-string headline))
636 (id (org-taskjuggler-clean-id (downcase (pop parts)))))
637 ; try to add more parts of the headline to make it unique
638 (while (and (member id unique-ids) (car parts))
639 (setq id (concat id "_" (org-taskjuggler-clean-id (downcase (pop parts))))))
640 ; if its still not unique add "_"
641 (while (member id unique-ids)
642 (setq id (concat id "_")))
643 id))
645 (defun org-taskjuggler-clean-id (id)
646 "Clean and return ID to make it acceptable for taskjuggler."
647 (and id
648 ;; replace non-ascii by _
649 (replace-regexp-in-string
650 "[^a-zA-Z0-9_]" "_"
651 ;; make sure id doesn't start with a number
652 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id))))
654 (defun org-taskjuggler-open-project (project)
655 "Insert the beginning of a project declaration. All valid
656 attributes from the PROJECT alist are inserted. If no end date is
657 specified it is calculated
658 `org-export-taskjuggler-default-project-duration' days from now."
659 (let* ((unique-id (cdr (assoc "unique-id" project)))
660 (headline (cdr (assoc "HEADLINE" project)))
661 (version (cdr (assoc "version" project)))
662 (start (cdr (assoc "start" project)))
663 (end (cdr (assoc "end" project))))
664 (insert
665 (format "project %s \"%s\" \"%s\" %s %s {\n }\n"
666 unique-id headline version start
667 (or (and end (format "- %s" end))
668 (format "+%sd"
669 org-export-taskjuggler-default-project-duration))))))
671 (defun org-taskjuggler-filter-and-join (items)
672 "Filter all nil elements from ITEMS and join the remaining ones
673 with separator \"\n\"."
674 (let ((filtered-items (remq nil items)))
675 (and filtered-items (mapconcat 'identity filtered-items "\n"))))
677 (defun org-taskjuggler-get-attributes (item attributes)
678 "Return all attribute as a single formatted string. ITEM is an
679 alist representing either a resource or a task. ATTRIBUTES is a
680 list of symbols. Only entries from ITEM are considered that are
681 listed in ATTRIBUTES."
682 (org-taskjuggler-filter-and-join
683 (mapcar
684 (lambda (attribute)
685 (org-taskjuggler-filter-and-join
686 (org-taskjuggler-get-attribute item attribute)))
687 attributes)))
689 (defun org-taskjuggler-get-attribute (item attribute)
690 "Return a list of strings containing the properly formatted
691 taskjuggler declaration for a given ATTRIBUTE in ITEM (an alist).
692 If the ATTRIBUTE is not in ITEM return nil."
693 (cond
694 ((null item) nil)
695 ((equal (symbol-name attribute) (car (car item)))
696 (cons (format "%s %s" (symbol-name attribute) (cdr (car item)))
697 (org-taskjuggler-get-attribute (cdr item) attribute)))
698 (t (org-taskjuggler-get-attribute (cdr item) attribute))))
700 (defun org-taskjuggler-open-resource (resource)
701 "Insert the beginning of a resource declaration. All valid
702 attributes from the RESOURCE alist are inserted. If the RESOURCE
703 defines a property \"resource_id\" it will be used as the id for
704 this resource. Otherwise it will use the ID property. If neither
705 is defined it will calculate a unique id for the resource using
706 `org-taskjuggler-get-unique-id'."
707 (let ((id (org-taskjuggler-clean-id
708 (or (cdr (assoc "resource_id" resource))
709 (cdr (assoc "ID" resource))
710 (cdr (assoc "unique-id" resource)))))
711 (headline (cdr (assoc "HEADLINE" resource)))
712 (attributes org-export-taskjuggler-valid-resource-attributes))
713 (insert
714 (concat
715 "resource " id " \"" headline "\" {\n "
716 (org-taskjuggler-get-attributes resource attributes) "\n"))))
718 (defun org-taskjuggler-clean-effort (effort)
719 "Translate effort strings into a format acceptable to taskjuggler,
720 i.e. REAL UNIT. A valid effort string can be anything that is
721 accepted by `org-duration-string-to-minutesĀ“."
722 (cond
723 ((null effort) effort)
724 (t (let* ((minutes (org-duration-string-to-minutes effort))
725 (hours (/ minutes 60.0)))
726 (format "%.1fh" hours)))))
728 (defun org-taskjuggler-get-priority (priority)
729 "Return a priority between 1 and 1000 based on PRIORITY, an
730 org-mode priority string."
731 (max 1 (/ (* 1000 (- org-lowest-priority (string-to-char priority)))
732 (- org-lowest-priority org-highest-priority))))
734 (defun org-taskjuggler-open-task (task)
735 (let* ((unique-id (cdr (assoc "unique-id" task)))
736 (headline (cdr (assoc "HEADLINE" task)))
737 (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
738 (depends (cdr (assoc "depends" task)))
739 (allocate (cdr (assoc "allocate" task)))
740 (priority-raw (cdr (assoc "PRIORITY" task)))
741 (priority (and priority-raw (org-taskjuggler-get-priority priority-raw)))
742 (state (cdr (assoc "TODO" task)))
743 (complete (or (and (member state org-done-keywords) "100")
744 (cdr (assoc "complete" task))))
745 (parent-ordered (cdr (assoc "parent-ordered" task)))
746 (previous-sibling (cdr (assoc "previous-sibling" task)))
747 (milestone (or (cdr (assoc "milestone" task))
748 (and (assoc "leaf-node" task)
749 (not (or effort
750 (cdr (assoc "length" task))
751 (cdr (assoc "duration" task))
752 (and (cdr (assoc "start" task))
753 (cdr (assoc "end" task)))
754 (cdr (assoc "period" task)))))))
755 (attributes org-export-taskjuggler-valid-task-attributes))
756 (insert
757 (concat
758 "task " unique-id " \"" headline "\" {\n"
759 (if (and parent-ordered previous-sibling)
760 (format " depends %s\n" previous-sibling)
761 (and depends (format " depends %s\n" depends)))
762 (and allocate (format " purge %s\n allocate %s\n"
763 (or (and (org-taskjuggler-targeting-tj3-p) "allocate")
764 "allocations")
765 allocate))
766 (and complete (format " complete %s\n" complete))
767 (and effort (format " effort %s\n" effort))
768 (and priority (format " priority %s\n" priority))
769 (and milestone (format " milestone\n"))
771 (org-taskjuggler-get-attributes task attributes)
772 "\n"))))
774 (defun org-taskjuggler-open-report (report)
775 (let* ((kind (or (cdr (assoc "report-kind" report)) "taskreport"))
776 (headline (cdr (assoc "HEADLINE" report)))
777 (attributes org-export-taskjuggler-valid-report-attributes))
778 (insert
779 (concat
780 kind " \"" headline "\" {\n"
781 (org-taskjuggler-get-attributes report attributes)
782 "\n}\n"))))
784 (defun org-taskjuggler-close-maybe (level)
785 (while (> org-export-taskjuggler-old-level level)
786 (insert "}\n")
787 (setq org-export-taskjuggler-old-level (1- org-export-taskjuggler-old-level)))
788 (when (= org-export-taskjuggler-old-level level)
789 (insert "}\n")))
791 (defun org-taskjuggler-insert-reports (reports)
792 (if reports
793 (dolist (report (cdr reports))
794 (org-taskjuggler-open-report report))
795 (let (report)
796 (dolist (report org-export-taskjuggler-default-reports)
797 (insert report "\n")))))
799 (provide 'org-taskjuggler)
801 ;; Local variables:
802 ;; generated-autoload-file: "org-loaddefs.el"
803 ;; End:
805 ;;; org-taskjuggler.el ends here