org-e-icalendar: Create vevents for timestamps within headline's title
[org-mode.git] / lisp / org-taskjuggler.el
blob58b4d29a3c0e5a27dd625376581f778f34314519
1 ;;; org-taskjuggler.el --- TaskJuggler exporter for org-mode
2 ;;
3 ;; Copyright (C) 2007-2012 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 ;;;###autoload
301 (defun org-export-as-taskjuggler (&optional arg hidden ext-plist
302 to-buffer body-only pub-dir)
303 "Export parts of the current buffer as a TaskJuggler file.
304 The exporter looks for a tree with tag, property or todo that
305 matches `org-export-taskjuggler-project-tag' and takes this as
306 the tasks for this project. The first node of this tree defines
307 the project properties such as project name and project period.
308 If there is a tree with tag, property or todo that matches
309 `org-export-taskjuggler-resource-tag' this three is taken as
310 resources for the project. If no resources are specified, a
311 default resource is created and allocated to the project. Also
312 the taskjuggler project will be created with default reports as
313 defined in `org-export-taskjuggler-default-reports'."
314 (interactive "P")
316 (message "Exporting...")
317 (setq-default org-done-keywords org-done-keywords)
318 (let* ((opt-plist (org-combine-plists (org-default-export-plist)
319 ext-plist
320 (org-infile-export-plist)))
321 (org-export-opt-plist opt-plist)
322 (tasks
323 (org-taskjuggler-resolve-dependencies
324 (org-taskjuggler-assign-task-ids
325 (org-taskjuggler-compute-task-leafiness
326 (org-map-entries
327 'org-taskjuggler-components
328 org-export-taskjuggler-project-tag nil 'archive 'comment)))))
329 (resources
330 (org-taskjuggler-assign-resource-ids
331 (org-map-entries
332 'org-taskjuggler-components
333 org-export-taskjuggler-resource-tag nil 'archive 'comment)))
334 (reports
335 (org-map-entries
336 'org-taskjuggler-components
337 org-export-taskjuggler-report-tag nil 'archive 'comment))
338 (filename (if to-buffer
340 (concat (file-name-as-directory
341 (or pub-dir
342 (org-export-directory :tj opt-plist)))
343 (file-name-sans-extension
344 (file-name-nondirectory buffer-file-name))
345 org-export-taskjuggler-extension)))
346 (buffer (if to-buffer
347 (cond
348 ((eq to-buffer 'string)
349 (get-buffer-create "*Org Taskjuggler Export*"))
350 (t (get-buffer-create to-buffer)))
351 (find-file-noselect filename)))
352 (old-buffer (current-buffer))
353 (org-export-taskjuggler-old-level 0)
354 task resource)
355 (unless tasks
356 (error "No tasks specified"))
357 ;; add a default resource
358 (unless resources
359 (setq resources
360 `((("resource_id" . ,(user-login-name))
361 ("HEADLINE" . ,user-full-name)
362 ("level" . 1)))))
363 ;; add a default allocation to the first task if none was given
364 (unless (assoc "allocate" (car tasks))
365 (let ((task (car tasks))
366 (resource-id (cdr (assoc "resource_id" (car resources)))))
367 (setcar tasks (push (cons "allocate" resource-id) task))))
368 ;; add a default start date to the first task if none was given
369 (unless (assoc "start" (car tasks))
370 (let ((task (car tasks))
371 (time-string (format-time-string "%Y-%m-%d")))
372 (setcar tasks (push (cons "start" time-string) task))))
373 ;; add a default version if none was given
374 (unless (assoc "version" (car tasks))
375 (let ((task (car tasks))
376 (version org-export-taskjuggler-default-project-version))
377 (setcar tasks (push (cons "version" version) task))))
378 (with-current-buffer buffer
379 (erase-buffer)
380 (org-install-letbind)
381 ;; create local variables for all options, to make sure all called
382 ;; functions get the correct information
383 (mapc (lambda (x)
384 (set (make-local-variable (nth 2 x))
385 (plist-get opt-plist (car x))))
386 org-export-plist-vars)
388 (org-clone-local-variables old-buffer "^org-")
389 (insert org-export-taskjuggler-default-global-header)
390 (org-taskjuggler-open-project
391 (if org-export-taskjuggler-keep-project-as-task
392 (car tasks)
393 (pop tasks)))
394 (insert org-export-taskjuggler-default-global-properties)
395 (insert "\n")
396 (dolist (resource resources)
397 (let ((level (cdr (assoc "level" resource))))
398 (org-taskjuggler-close-maybe level)
399 (org-taskjuggler-open-resource resource)
400 (setq org-export-taskjuggler-old-level level)))
401 (org-taskjuggler-close-maybe 1)
402 (setq org-export-taskjuggler-old-level 0)
403 (dolist (task tasks)
404 (let ((level (cdr (assoc "level" task))))
405 (org-taskjuggler-close-maybe level)
406 (org-taskjuggler-open-task task)
407 (setq org-export-taskjuggler-old-level level)))
408 (org-taskjuggler-close-maybe
409 (if org-export-taskjuggler-keep-project-as-task
410 1 2))
411 (org-taskjuggler-insert-reports reports)
412 (or to-buffer (save-buffer))
413 (or (org-export-push-to-kill-ring "TaskJuggler")
414 (message "Exporting... done"))
415 (if (eq to-buffer 'string)
416 (prog1 (buffer-substring (point-min) (point-max))
417 (kill-buffer (current-buffer)))
418 (current-buffer)))))
420 ;;;###autoload
421 (defun org-export-as-taskjuggler-and-open ()
422 "Export the current buffer as a TaskJuggler file and open it
423 with the TaskJuggler GUI."
424 (interactive)
425 (let* ((file-name (buffer-file-name (org-export-as-taskjuggler)))
426 (process-name "TaskJugglerUI")
427 (command (concat process-name " " file-name)))
428 (start-process-shell-command process-name nil command)))
430 (defun org-taskjuggler-targeting-tj3-p ()
431 "Return true if we are targeting TaskJuggler III."
432 (>= org-export-taskjuggler-target-version 3.0))
434 (defun org-taskjuggler-parent-is-ordered-p ()
435 "Return true if the parent of the current node has a property
436 \"ORDERED\". Return nil otherwise."
437 (save-excursion
438 (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
440 (defun org-taskjuggler-date (date)
441 (let ((time (parse-time-string date)))
442 (format "%d-%02d-%02d" (nth 5 time) (nth 4 time) (nth 3 time))))
444 (defun org-taskjuggler-components ()
445 "Return an alist containing all the pertinent information for
446 the current node such as the headline, the level, todo state
447 information, all the properties, etc."
448 (let* ((props (org-entry-properties))
449 (components (org-heading-components))
450 (level (nth 1 components))
451 (headline
452 (replace-regexp-in-string
453 "\"" "\\\"" (nth 4 components) t t)) ; quote double quotes in headlines
454 (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
455 (let ((scheduled (assoc "SCHEDULED" props))
456 (deadline (assoc "DEADLINE" props)))
457 (when scheduled
458 (push (cons "start" (org-taskjuggler-date (cdr scheduled))) props))
459 (when deadline
460 (push (cons "end" (org-taskjuggler-date (cdr deadline))) props)))
461 (push (cons "level" level) props)
462 (push (cons "HEADLINE" headline) props)
463 (push (cons "parent-ordered" parent-ordered) props)))
465 (defun org-taskjuggler-assign-task-ids (tasks)
466 "Given a list of tasks return the same list assigning a unique id
467 and the full path to each task. Taskjuggler takes hierarchical ids.
468 For that reason we have to make ids locally unique and we have to keep
469 a path to the current task."
470 (let ((previous-level 0)
471 unique-ids unique-id
472 path
473 task resolved-tasks tmp)
474 (dolist (task tasks resolved-tasks)
475 (let ((level (cdr (assoc "level" task))))
476 (cond
477 ((< previous-level level)
478 (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
479 (dotimes (tmp (- level previous-level))
480 (push (list unique-id) unique-ids)
481 (push unique-id path)))
482 ((= previous-level level)
483 (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
484 (push unique-id (car unique-ids))
485 (setcar path unique-id))
486 ((> previous-level level)
487 (dotimes (tmp (- previous-level level))
488 (pop unique-ids)
489 (pop path))
490 (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
491 (push unique-id (car unique-ids))
492 (setcar path unique-id)))
493 (push (cons "unique-id" unique-id) task)
494 (push (cons "path"
495 (mapconcat 'identity
496 (if org-export-taskjuggler-keep-project-as-task
497 (reverse path)
498 (cdr (reverse path))) ".")) task)
499 (setq previous-level level)
500 (setq resolved-tasks (append resolved-tasks (list task)))))))
502 (defun org-taskjuggler-compute-task-leafiness (tasks)
503 "Figure out if each task is a leaf by looking at it's level,
504 and the level of its successor. If the successor is higher (ie
505 deeper), then it's not a leaf."
506 (let (new-list)
507 (while (car tasks)
508 (let ((task (car tasks))
509 (successor (car (cdr tasks))))
510 (cond
511 ;; if a task has no successors it is a leaf
512 ((null successor)
513 (push (cons (cons "leaf-node" t) task) new-list))
514 ;; if the successor has a lower level than task it is a leaf
515 ((<= (cdr (assoc "level" successor)) (cdr (assoc "level" task)))
516 (push (cons (cons "leaf-node" t) task) new-list))
517 ;; otherwise examine the rest of the tasks
518 (t (push task new-list))))
519 (setq tasks (cdr tasks)))
520 (nreverse new-list)))
522 (defun org-taskjuggler-assign-resource-ids (resources)
523 "Given a list of resources return the same list, assigning a
524 unique id to each resource."
525 (let (unique-ids new-list)
526 (dolist (resource resources new-list)
527 (let ((unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
528 (push (cons "unique-id" unique-id) resource)
529 (push unique-id unique-ids)
530 (push resource new-list)))
531 (nreverse new-list)))
533 (defun org-taskjuggler-resolve-dependencies (tasks)
534 (let ((previous-level 0)
535 siblings
536 task resolved-tasks)
537 (dolist (task tasks resolved-tasks)
538 (let* ((level (cdr (assoc "level" task)))
539 (depends (cdr (assoc "depends" task)))
540 (parent-ordered (cdr (assoc "parent-ordered" task)))
541 (blocker (cdr (assoc "BLOCKER" task)))
542 (blocked-on-previous
543 (and blocker (string-match "previous-sibling" blocker)))
544 (dependencies
545 (org-taskjuggler-resolve-explicit-dependencies
546 (append
547 (and depends (org-taskjuggler-tokenize-dependencies depends))
548 (and blocker (org-taskjuggler-tokenize-dependencies blocker)))
549 tasks))
550 previous-sibling)
551 ; update previous sibling info
552 (cond
553 ((< previous-level level)
554 (dotimes (tmp (- level previous-level))
555 (push task siblings)))
556 ((= previous-level level)
557 (setq previous-sibling (car siblings))
558 (setcar siblings task))
559 ((> previous-level level)
560 (dotimes (tmp (- previous-level level))
561 (pop siblings))
562 (setq previous-sibling (car siblings))
563 (setcar siblings task)))
564 ; insert a dependency on previous sibling if the parent is
565 ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
566 (when (or (and previous-sibling parent-ordered) blocked-on-previous)
567 (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
568 ; store dependency information
569 (when dependencies
570 (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
571 (setq previous-level level)
572 (setq resolved-tasks (append resolved-tasks (list task)))))))
574 (defun org-taskjuggler-tokenize-dependencies (dependencies)
575 "Split a dependency property value DEPENDENCIES into the
576 individual dependencies and return them as a list while keeping
577 the optional arguments (such as gapduration) for the
578 dependencies. A dependency will have to match `[-a-zA-Z0-9_]+'."
579 (cond
580 ((string-match "^ *$" dependencies) nil)
581 ((string-match "^[ \t]*\\([-a-zA-Z0-9_]+\\([ \t]*{[^}]+}\\)?\\)[ \t,]*" dependencies)
582 (cons
583 (substring dependencies (match-beginning 1) (match-end 1))
584 (org-taskjuggler-tokenize-dependencies (substring dependencies (match-end 0)))))
585 (t (error (format "invalid dependency id %s" dependencies)))))
587 (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
588 "For each dependency in DEPENDENCIES try to find a
589 corresponding task with a matching property \"task_id\" in TASKS.
590 Return a list containing the resolved links for all DEPENDENCIES
591 where a matching tasks was found. If the dependency is
592 \"previous-sibling\" it is ignored (as this is dealt with in
593 `org-taskjuggler-resolve-dependencies'). If there is no matching
594 task the dependency is ignored and a warning is displayed ."
595 (unless (null dependencies)
596 (let*
597 ;; the dependency might have optional attributes such as "{
598 ;; gapduration 5d }", so only use the first string as id for the
599 ;; dependency
600 ((dependency (car dependencies))
601 (id (car (split-string dependency)))
602 (optional-attributes
603 (mapconcat 'identity (cdr (split-string dependency)) " "))
604 (path (org-taskjuggler-find-task-with-id id tasks)))
605 (cond
606 ;; ignore previous sibling dependencies
607 ((equal (car dependencies) "previous-sibling")
608 (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
609 ;; if the id is found in another task use its path
610 ((not (null path))
611 (cons (mapconcat 'identity (list path optional-attributes) " ")
612 (org-taskjuggler-resolve-explicit-dependencies
613 (cdr dependencies) tasks)))
614 ;; warn about dangling dependency but otherwise ignore it
615 (t (display-warning
616 'org-export-taskjuggler
617 (format "No task with matching property \"task_id\" found for id %s" id))
618 (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))))))
620 (defun org-taskjuggler-find-task-with-id (id tasks)
621 "Find ID in tasks. If found return the path of task. Otherwise
622 return nil."
623 (let ((task-id (cdr (assoc "task_id" (car tasks))))
624 (path (cdr (assoc "path" (car tasks)))))
625 (cond
626 ((null tasks) nil)
627 ((equal task-id id) path)
628 (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
630 (defun org-taskjuggler-get-unique-id (item unique-ids)
631 "Return a unique id for an ITEM which can be a task or a resource.
632 The id is derived from the headline and made unique against
633 UNIQUE-IDS. If the (downcased) first token of the headline is not
634 unique try to add more (downcased) tokens of the headline or
635 finally add more underscore characters (\"_\")."
636 (let* ((headline (cdr (assoc "HEADLINE" item)))
637 (parts (split-string headline))
638 (id (org-taskjuggler-clean-id (downcase (pop parts)))))
639 ; try to add more parts of the headline to make it unique
640 (while (and (member id unique-ids) (car parts))
641 (setq id (concat id "_" (org-taskjuggler-clean-id (downcase (pop parts))))))
642 ; if its still not unique add "_"
643 (while (member id unique-ids)
644 (setq id (concat id "_")))
645 id))
647 (defun org-taskjuggler-clean-id (id)
648 "Clean and return ID to make it acceptable for taskjuggler."
649 (and id
650 ;; replace non-ascii by _
651 (replace-regexp-in-string
652 "[^a-zA-Z0-9_]" "_"
653 ;; make sure id doesn't start with a number
654 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id))))
656 (defun org-taskjuggler-open-project (project)
657 "Insert the beginning of a project declaration. All valid
658 attributes from the PROJECT alist are inserted. If no end date is
659 specified it is calculated
660 `org-export-taskjuggler-default-project-duration' days from now."
661 (let* ((unique-id (cdr (assoc "unique-id" project)))
662 (headline (cdr (assoc "HEADLINE" project)))
663 (version (cdr (assoc "version" project)))
664 (start (cdr (assoc "start" project)))
665 (end (cdr (assoc "end" project))))
666 (insert
667 (format "project %s \"%s\" \"%s\" %s %s {\n }\n"
668 unique-id headline version start
669 (or (and end (format "- %s" end))
670 (format "+%sd"
671 org-export-taskjuggler-default-project-duration))))))
673 (defun org-taskjuggler-filter-and-join (items)
674 "Filter all nil elements from ITEMS and join the remaining ones
675 with separator \"\n\"."
676 (let ((filtered-items (remq nil items)))
677 (and filtered-items (mapconcat 'identity filtered-items "\n"))))
679 (defun org-taskjuggler-get-attributes (item attributes)
680 "Return all attribute as a single formatted string. ITEM is an
681 alist representing either a resource or a task. ATTRIBUTES is a
682 list of symbols. Only entries from ITEM are considered that are
683 listed in ATTRIBUTES."
684 (org-taskjuggler-filter-and-join
685 (mapcar
686 (lambda (attribute)
687 (org-taskjuggler-filter-and-join
688 (org-taskjuggler-get-attribute item attribute)))
689 attributes)))
691 (defun org-taskjuggler-get-attribute (item attribute)
692 "Return a list of strings containing the properly formatted
693 taskjuggler declaration for a given ATTRIBUTE in ITEM (an alist).
694 If the ATTRIBUTE is not in ITEM return nil."
695 (cond
696 ((null item) nil)
697 ((equal (symbol-name attribute) (car (car item)))
698 (cons (format "%s %s" (symbol-name attribute) (cdr (car item)))
699 (org-taskjuggler-get-attribute (cdr item) attribute)))
700 (t (org-taskjuggler-get-attribute (cdr item) attribute))))
702 (defun org-taskjuggler-open-resource (resource)
703 "Insert the beginning of a resource declaration. All valid
704 attributes from the RESOURCE alist are inserted. If the RESOURCE
705 defines a property \"resource_id\" it will be used as the id for
706 this resource. Otherwise it will use the ID property. If neither
707 is defined it will calculate a unique id for the resource using
708 `org-taskjuggler-get-unique-id'."
709 (let ((id (org-taskjuggler-clean-id
710 (or (cdr (assoc "resource_id" resource))
711 (cdr (assoc "ID" resource))
712 (cdr (assoc "unique-id" resource)))))
713 (headline (cdr (assoc "HEADLINE" resource)))
714 (attributes org-export-taskjuggler-valid-resource-attributes))
715 (insert
716 (concat
717 "resource " id " \"" headline "\" {\n "
718 (org-taskjuggler-get-attributes resource attributes) "\n"))))
720 (defun org-taskjuggler-clean-effort (effort)
721 "Translate effort strings into a format acceptable to taskjuggler,
722 i.e. REAL UNIT. A valid effort string can be anything that is
723 accepted by `org-duration-string-to-minutesĀ“."
724 (cond
725 ((null effort) effort)
726 (t (let* ((minutes (org-duration-string-to-minutes effort))
727 (hours (/ minutes 60.0)))
728 (format "%.1fh" hours)))))
730 (defun org-taskjuggler-get-priority (priority)
731 "Return a priority between 1 and 1000 based on PRIORITY, an
732 org-mode priority string."
733 (max 1 (/ (* 1000 (- org-lowest-priority (string-to-char priority)))
734 (- org-lowest-priority org-highest-priority))))
736 (defun org-taskjuggler-open-task (task)
737 (let* ((unique-id (cdr (assoc "unique-id" task)))
738 (headline (cdr (assoc "HEADLINE" task)))
739 (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
740 (depends (cdr (assoc "depends" task)))
741 (allocate (cdr (assoc "allocate" task)))
742 (priority-raw (cdr (assoc "PRIORITY" task)))
743 (priority (and priority-raw (org-taskjuggler-get-priority priority-raw)))
744 (state (cdr (assoc "TODO" task)))
745 (complete (or (and (member state org-done-keywords) "100")
746 (cdr (assoc "complete" task))))
747 (parent-ordered (cdr (assoc "parent-ordered" task)))
748 (previous-sibling (cdr (assoc "previous-sibling" task)))
749 (milestone (or (cdr (assoc "milestone" task))
750 (and (assoc "leaf-node" task)
751 (not (or effort
752 (cdr (assoc "length" task))
753 (cdr (assoc "duration" task))
754 (and (cdr (assoc "start" task))
755 (cdr (assoc "end" task)))
756 (cdr (assoc "period" task)))))))
757 (attributes org-export-taskjuggler-valid-task-attributes))
758 (insert
759 (concat
760 "task " unique-id " \"" headline "\" {\n"
761 (if (and parent-ordered previous-sibling)
762 (format " depends %s\n" previous-sibling)
763 (and depends (format " depends %s\n" depends)))
764 (and allocate (format " purge %s\n allocate %s\n"
765 (or (and (org-taskjuggler-targeting-tj3-p) "allocate")
766 "allocations")
767 allocate))
768 (and complete (format " complete %s\n" complete))
769 (and effort (format " effort %s\n" effort))
770 (and priority (format " priority %s\n" priority))
771 (and milestone (format " milestone\n"))
773 (org-taskjuggler-get-attributes task attributes)
774 "\n"))))
776 (defun org-taskjuggler-open-report (report)
777 (let* ((kind (or (cdr (assoc "report-kind" report)) "taskreport"))
778 (headline (cdr (assoc "HEADLINE" report)))
779 (attributes org-export-taskjuggler-valid-report-attributes))
780 (insert
781 (concat
782 kind " \"" headline "\" {\n"
783 (org-taskjuggler-get-attributes report attributes)
784 "\n}\n"))))
786 (defun org-taskjuggler-close-maybe (level)
787 (while (> org-export-taskjuggler-old-level level)
788 (insert "}\n")
789 (setq org-export-taskjuggler-old-level (1- org-export-taskjuggler-old-level)))
790 (when (= org-export-taskjuggler-old-level level)
791 (insert "}\n")))
793 (defun org-taskjuggler-insert-reports (reports)
794 (if reports
795 (dolist (report (cdr reports))
796 (org-taskjuggler-open-report report))
797 (let (report)
798 (dolist (report org-export-taskjuggler-default-reports)
799 (insert report "\n")))))
801 (provide 'org-taskjuggler)
803 ;; Local variables:
804 ;; generated-autoload-file: "org-loaddefs.el"
805 ;; End:
807 ;;; org-taskjuggler.el ends here