contrib/lisp/: Fix some local variables settings
[org-mode.git] / contrib / lisp / ox-taskjuggler.el
blob8db45e93dec60fa646bb21f8a73a71e06ed7ce90
1 ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
2 ;;
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Emacs Lisp Archive Entry
6 ;; Filename: ox-taskjuggler.el
7 ;; Author: Christian Egli
8 ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
9 ;; Maintainer: Christian Egli
10 ;; Keywords: org, taskjuggler, project planning
11 ;; Description: Converts an Org mode buffer into a TaskJuggler project plan
13 ;; This program is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
26 ;; Commentary:
28 ;; This library implements a TaskJuggler exporter for Org mode.
29 ;; TaskJuggler uses a text format to define projects, tasks and
30 ;; resources, so it is a natural fit for Org mode. It can produce all
31 ;; sorts of reports for tasks or resources in either HTML, CSV or PDF.
32 ;; The current version of TaskJuggler requires KDE but the next
33 ;; version is implemented in Ruby and should therefore run on any
34 ;; platform.
36 ;; The exporter does not export all the nodes of a document or
37 ;; strictly follow the order of the nodes in the document.
39 ;; Instead the TaskJuggler exporter looks for a tree that defines the
40 ;; tasks and a optionally tree that defines the resources for this
41 ;; project. It then creates a TaskJuggler file based on these trees
42 ;; and the attributes defined in all the nodes.
44 ;; * Installation
46 ;; Put this file into your load-path and the following line into your
47 ;; ~/.emacs:
49 ;; (add-to-list 'org-export-backends 'taskjuggler)
51 ;; or customize `org-export-backends' variable.
53 ;; The interactive functions are the following:
55 ;; M-x `org-taskjuggler-export'
56 ;; M-x `org-taskjuggler-export-and-open'
58 ;; * Tasks
60 ;; Let's illustrate the usage with a small example. Create your tasks
61 ;; as you usually do with org-mode. Assign efforts to each task using
62 ;; properties (it's easiest to do this in the column view). You
63 ;; should end up with something similar to the example by Peter Jones
64 ;; in:
66 ;; 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-taskjuggler-project-tag' to). You are now ready to export the
71 ;; project plan with `org-taskjuggler-export-and-open' which will
72 ;; export the project plan and open a Gantt chart in TaskJugglerUI.
74 ;; * Resources
76 ;; Next you can define resources and assign those to work on specific
77 ;; tasks. You can group your resources hierarchically. Tag the top
78 ;; node of the resources with "taskjuggler_resource" (or whatever you
79 ;; customized `org-taskjuggler-resource-tag' to). You can optionally
80 ;; assign an identifier (named "resource_id") to the resources (using
81 ;; the standard org properties commands) or you can let the exporter
82 ;; generate identifiers automatically (the exporter picks the first
83 ;; word of the headline as the identifier as long as it is unique, see
84 ;; the documentation of `org-taskjuggler--build-unique-id'). Using that
85 ;; identifier you can then allocate resources to tasks. This is again
86 ;; done with the "allocate" property on the tasks. Do this in column
87 ;; view or when on the task type
89 ;; C-c C-x p allocate RET <resource_id> RET
91 ;; Once the allocations are done you can again export to TaskJuggler
92 ;; and check in the Resource Allocation Graph which person is working
93 ;; on what task at what time.
95 ;; * Export of properties
97 ;; The exporter also takes TODO state information into consideration,
98 ;; i.e. if a task is marked as done it will have the corresponding
99 ;; attribute in TaskJuggler ("complete 100"). Also it will export any
100 ;; property on a task resource or resource node which is known to
101 ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
102 ;; journalentry, rate for resources or account, start, note, duration,
103 ;; end, journalentry, milestone, reference, responsible, scheduling,
104 ;; etc for tasks.
106 ;; * Dependencies
108 ;; The exporter will handle dependencies that are defined in the tasks
109 ;; either with the ORDERED attribute (see TODO dependencies in the Org
110 ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
111 ;; alternatively with a depends attribute. Both the BLOCKER and the
112 ;; depends attribute can be either "previous-sibling" or a reference
113 ;; to an identifier (named "task_id") which is defined for another
114 ;; task in the project. BLOCKER and the depends attribute can define
115 ;; multiple dependencies separated by either space or comma. You can
116 ;; also specify optional attributes on the dependency by simply
117 ;; appending it. The following examples should illustrate this:
119 ;; * Training material
120 ;; :PROPERTIES:
121 ;; :task_id: training_material
122 ;; :ORDERED: t
123 ;; :END:
124 ;; ** Markup Guidelines
125 ;; :PROPERTIES:
126 ;; :Effort: 2d
127 ;; :END:
128 ;; ** Workflow Guidelines
129 ;; :PROPERTIES:
130 ;; :Effort: 2d
131 ;; :END:
132 ;; * Presentation
133 ;; :PROPERTIES:
134 ;; :Effort: 2d
135 ;; :BLOCKER: training_material { gapduration 1d } some_other_task
136 ;; :END:
138 ;;;; * TODO
139 ;; - Look at org-file-properties, org-global-properties and
140 ;; org-global-properties-fixed
141 ;; - What about property inheritance and org-property-inherit-p?
142 ;; - Use TYPE_TODO as an way to assign resources
144 ;;; Code:
146 (eval-when-compile (require 'cl))
148 (require 'ox)
152 ;;; User Variables
154 (defgroup org-export-taskjuggler nil
155 "Options specific for TaskJuggler export back-end."
156 :tag "Org Export TaskJuggler"
157 :group 'org-export)
159 (defcustom org-taskjuggler-extension ".tjp"
160 "Extension of TaskJuggler files."
161 :group 'org-export-taskjuggler
162 :type 'string)
164 (defcustom org-taskjuggler-project-tag "taskjuggler_project"
165 "Tag marking project's tasks.
166 This tag is used to find the tree containing all the tasks for
167 the project."
168 :group 'org-export-taskjuggler
169 :type 'string)
171 (defcustom org-taskjuggler-resource-tag "taskjuggler_resource"
172 "Tag marking project's resources.
173 This tag is used to find the tree containing all the resources
174 for the project."
175 :group 'org-export-taskjuggler
176 :type 'string)
178 (defcustom org-taskjuggler-report-tag "taskjuggler_report"
179 "Tag marking project's reports.
180 This tag is used to find the tree containing all the reports for
181 the project."
182 :group 'org-export-taskjuggler
183 :type 'string)
185 (defcustom org-taskjuggler-target-version 2.4
186 "Which version of TaskJuggler the exporter is targeting."
187 :group 'org-export-taskjuggler
188 :type 'number)
190 (defcustom org-taskjuggler-default-project-version "1.0"
191 "Default version string for the project.
192 This value can also be set with the \":VERSION:\" property
193 associated to the headline defining the project."
194 :group 'org-export-taskjuggler
195 :type 'string)
197 (defcustom org-taskjuggler-default-project-duration 280
198 "Default project duration.
199 The value will be used if no start and end date have been defined
200 in the root node of the task tree, i.e. the tree that has been
201 marked with `org-taskjuggler-project-tag'"
202 :group 'org-export-taskjuggler
203 :type 'integer)
205 (defcustom org-taskjuggler-default-reports
206 '("taskreport \"Gantt Chart\" {
207 headline \"Project Gantt Chart\"
208 columns hierarchindex, name, start, end, effort, duration, completed, chart
209 timeformat \"%Y-%m-%d\"
210 hideresource 1
211 loadunit shortauto
213 "resourcereport \"Resource Graph\" {
214 headline \"Resource Allocation Graph\"
215 columns no, name, utilization, freeload, chart
216 loadunit shortauto
217 sorttasks startup
218 hidetask ~isleaf()
220 "Default reports for the project."
221 :group 'org-export-taskjuggler
222 :type '(repeat (string :tag "Report")))
224 (defcustom org-taskjuggler-default-global-header ""
225 "Default global header for the project.
226 This goes before project declaration, and might be useful for
227 early macros."
228 :group 'org-export-taskjuggler
229 :type '(string :tag "Preamble"))
231 (defcustom org-taskjuggler-default-global-properties
232 "shift s40 \"Part time shift\" {
233 workinghours wed, thu, fri off
236 "Default global properties for the project.
238 Here you typically define global properties such as shifts,
239 accounts, rates, vacation, macros and flags. Any property that
240 is allowed within the TaskJuggler file can be inserted. You
241 could for example include another TaskJuggler file.
243 The global properties are inserted after the project declaration
244 but before any resource and task declarations."
245 :group 'org-export-taskjuggler
246 :type '(string :tag "Preamble"))
248 (defcustom org-taskjuggler-valid-task-attributes
249 '(account start note duration endbuffer endcredit end
250 flags journalentry length limits maxend maxstart minend
251 minstart period reference responsible scheduling
252 startbuffer startcredit statusnote chargeset charge)
253 "Valid attributes for Taskjuggler tasks.
254 If one of these appears as a property for a headline, it will be
255 exported with the corresponding task."
256 :group 'org-export-taskjuggler)
258 (defcustom org-taskjuggler-valid-resource-attributes
259 '(limits vacation shift booking efficiency journalentry rate
260 workinghours flags)
261 "Valid attributes for Taskjuggler resources.
262 If one of these appears as a property for a headline, it will be
263 exported with the corresponding resource."
264 :group 'org-export-taskjuggler)
266 (defcustom org-taskjuggler-valid-report-attributes
267 '(headline columns definitions timeformat hideresource hidetask
268 loadunit sorttasks formats period)
269 "Valid attributes for Taskjuggler reports.
270 If one of these appears as a property for a headline, it will be
271 exported with the corresponding report."
272 :group 'org-export-taskjuggler)
274 (defcustom org-taskjuggler-keep-project-as-task t
275 "Non-nil keeps the project headline as an umbrella task for all tasks.
276 Setting this to nil will allow maintaining completely separated
277 task buckets, while still sharing the same resources pool."
278 :group 'org-export-taskjuggler
279 :type 'boolean)
283 ;;; Hooks
285 (defvar org-taskjuggler-final-hook nil
286 "Hook run after a TaskJuggler files has been saved.
287 This hook is run with the name of the file as argument.")
291 ;;; Back-End Definition
293 (org-export-define-backend taskjuggler
294 ((template . org-taskjuggler-project-plan))
295 :menu-entry
296 (?J "Export to TaskJuggler"
297 ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
298 (?o "As TJP file and open"
299 (lambda (a s v b)
300 (if a (org-taskjuggler-export a s v)
301 (org-taskjuggler-export-and-open s v))))))
302 ;; This property will be used to store unique ids in communication
303 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
304 :options-alist ((:taskjuggler-unique-ids nil nil nil)))
308 ;;; Unique IDs
310 (defun org-taskjuggler-assign-task-ids (tasks info)
311 "Assign a unique ID to each task in TASKS.
312 TASKS is a list of headlines. Return value is an alist between
313 headlines and their associated ID. IDs are hierarchical, which
314 means they only need to be unique among the task siblings."
315 (let* (alist
316 (build-id
317 (lambda (tasks local-ids)
318 (org-element-map tasks 'headline
319 (lambda (task)
320 (let ((id (org-taskjuggler--build-unique-id task local-ids)))
321 (push id local-ids)
322 (push (cons task id) alist)
323 (funcall build-id (org-element-contents task) nil)))
324 info nil 'headline))))
325 (funcall build-id tasks nil)
326 alist))
328 (defun org-taskjuggler-assign-resource-ids (resources info)
329 "Assign a unique ID to each resource within RESOURCES.
330 RESOURCES is a list of headlines. Return value is an alist
331 between headlines and their associated ID."
332 (let (ids)
333 (org-element-map resources 'headline
334 (lambda (resource)
335 (let ((id (org-taskjuggler--build-unique-id resource ids)))
336 (push id ids)
337 (cons resource id)))
338 info)))
342 ;;; Accessors
344 (defun org-taskjuggler-get-project (info)
345 "Return project in parse tree.
346 INFO is a plist used as a communication channel. First headline
347 in buffer with `org-taskjuggler-project-tag' defines the project.
348 If no such task is defined, pick the first headline in buffer.
349 If there is no headline at all, return nil."
350 (or (org-element-map (plist-get info :parse-tree) 'headline
351 (lambda (hl)
352 (and (member org-taskjuggler-project-tag
353 (org-export-get-tags hl info))
354 hl))
355 info t)
356 (org-element-map tree 'headline 'identity info t)))
358 (defun org-taskjuggler-get-id (item info)
359 "Return id for task or resource ITEM.
360 ITEM is a headline. Return value is a string."
361 (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
363 (defun org-taskjuggler-get-name (item)
364 "Return name for task or resource ITEM.
365 ITEM is a headline. Return value is a string."
366 ;; Quote double quotes in name.
367 (replace-regexp-in-string
368 "\"" "\\\"" (org-element-property :raw-value item) t t))
370 (defun org-taskjuggler-get-start (item)
371 "Return start date for task or resource ITEM.
372 ITEM is a headline. Return value is a string or nil if ITEM
373 doesn't have any start date defined.."
374 (let ((scheduled (org-element-property :scheduled item)))
375 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))))
377 (defun org-taskjuggler-get-end (item)
378 "Return end date for task or resource ITEM.
379 ITEM is a headline. Return value is a string or nil if ITEM
380 doesn't have any end date defined.."
381 (let ((deadline (org-element-property :deadline item)))
382 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
386 ;;; Internal Functions
388 (defun org-taskjuggler--indent-string (s)
389 "Indent string S by 2 spaces.
390 Return new string. If S is the empty string, return it."
391 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
393 (defun org-taskjuggler--build-attributes (item attributes)
394 "Return attributes string for task, resource or report ITEM.
395 ITEM is a headline. ATTRIBUTES is a list of symbols
396 representing valid attributes for ITEM."
397 (mapconcat
398 (lambda (attribute)
399 (let ((value (org-element-property
400 (intern (upcase (format ":%s" attribute)))
401 item)))
402 (and value (format "%s %s\n" attribute value))))
403 (remq nil attributes) ""))
405 (defun org-taskjuggler--build-unique-id (item unique-ids)
406 "Return a unique id for a given task or a resource.
407 ITEM is an `headline' type element representing the task or
408 resource. Its id is derived from its name and made unique
409 against UNIQUE-IDS. If the (downcased) first token of the
410 headline is not unique try to add more (downcased) tokens of the
411 headline or finally add more underscore characters (\"_\")."
412 (let* ((parts (org-split-string (org-element-property :raw-value item)))
413 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
414 ;; Try to add more parts of the headline to make it unique.
415 (while (and (car parts) (member id unique-ids))
416 (setq id (concat id "_"
417 (org-taskjuggler--clean-id (downcase (pop parts))))))
418 ;; If it's still not unique, add "_".
419 (while (member id unique-ids)
420 (setq id (concat id "_")))
421 id))
423 (defun org-taskjuggler--clean-id (id)
424 "Clean and return ID to make it acceptable for TaskJuggler.
425 ID is a string."
426 ;; Replace non-ascii by "_".
427 (replace-regexp-in-string
428 "[^a-zA-Z0-9_]" "_"
429 ;; Make sure id doesn't start with a number.
430 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
434 ;;; Dependencies
436 (defun org-taskjuggler-resolve-dependencies (task info)
437 "Return a list of all tasks TASK depends on.
438 TASK is a headline. INFO is a plist used as a communication
439 channel."
440 (let ((deps-ids
441 ;; Get all dependencies specified in BLOCKER and DEPENDS task
442 ;; properties. Clean options from them.
443 (let ((deps (concat (org-element-property :BLOCKER task)
444 (org-element-property :DEPENDS task))))
445 (and deps
446 (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
447 "[ ,]* +"))))
448 depends)
449 (when deps-ids
450 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
451 ;; Add them to DEPENDS.
452 (let* ((project (org-taskjuggler-get-project info))
453 (tasks (if org-taskjuggler-keep-project-as-task project
454 (org-element-contents project))))
455 (setq depends
456 (org-element-map tasks 'headline
457 (lambda (task)
458 (let ((task-id (org-element-property :TASK_ID task)))
459 (and task-id (member task-id deps-ids) task)))
460 info)))
461 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
462 ;; belongs to DEPS-ID, add it to DEPENDS.
463 (when (and (member-ignore-case "previous-sibling" deps-ids)
464 (not (org-export-first-sibling-p task info)))
465 (let ((prev (org-export-get-previous-element task info)))
466 (and (not (memq prev depends)) (push prev depends)))))
467 ;; Check ORDERED status of parent.
468 (let ((parent (org-export-get-parent task)))
469 (when (and parent
470 (org-element-property :ORDERED parent)
471 (not (org-export-first-sibling-p task info)))
472 (push (org-export-get-previous-element task info) depends)))
473 ;; Return dependencies.
474 depends))
476 (defun org-taskjuggler-format-dependencies (dependencies task info)
477 "Format DEPENDENCIES to match TaskJuggler syntax.
478 DEPENDENCIES is list of dependencies for TASK, as returned by
479 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
480 INFO is a plist used as a communication channel. Return value
481 doesn't include leading \"depends\"."
482 (let ((dep-str (concat (org-element-property :BLOCKER task)
484 (org-element-property :DEPENDS task)))
485 (get-path
486 (lambda (dep)
487 ;; Return path to DEP relatively to TASK.
488 (let ((parent (org-export-get-parent dep))
489 (exclamations 1)
490 (option
491 (let ((id (org-element-property :TASK_ID dep)))
492 (and id
493 (string-match (concat id " +\\({.*?}\\)") dep-str)
494 (org-match-string-no-properties 1))))
495 path)
496 ;; Compute number of exclamation marks.
497 (while (not (org-element-map parent 'headline
498 (lambda (task) (eq task dep))))
499 (incf exclamations)
500 (setq parent (org-export-get-parent parent)))
501 ;; Build path from DEP to PARENT.
502 (while (not (eq parent dep))
503 (push (org-taskjuggler-get-id dep info) path)
504 (setq dep (org-export-get-parent dep)))
505 ;; Return full path. Add dependency options, if any.
506 (concat (make-string exclamations ?!)
507 (mapconcat 'identity path ".")
508 (and option (concat " " option)))))))
509 ;; Return dependencies string, without the leading "depends".
510 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
514 ;;; Translator Functions
516 (defun org-taskjuggler-project-plan (contents info)
517 "Build TaskJuggler project plan.
518 CONTENTS is ignored. INFO is a plist holding export options.
519 Return complete project plan as a string in TaskJuggler syntax."
520 (let* ((tree (plist-get info :parse-tree))
521 (project (or (org-taskjuggler-get-project info)
522 (error "No project specified"))))
523 (concat
524 ;; 1. Insert header.
525 (org-element-normalize-string org-taskjuggler-default-global-header)
526 ;; 2. Insert project.
527 (org-taskjuggler--build-project project info)
528 ;; 3. Insert global properties.
529 (org-element-normalize-string org-taskjuggler-default-global-properties)
530 ;; 4. Insert resources. Provide a default one if none is
531 ;; specified.
532 (let ((main-resources
533 ;; Collect contents from various trees marked with
534 ;; `org-taskjuggler-resource-tag'. Only gather top level
535 ;; resources.
536 (apply 'append
537 (org-element-map tree 'headline
538 (lambda (hl)
539 (and (member org-taskjuggler-resource-tag
540 (org-export-get-tags hl info))
541 (org-element-map (org-element-contents hl) 'headline
542 'identity info nil 'headline)))
543 info nil 'headline))))
544 ;; Assign a unique ID to each resource. Store it under
545 ;; `:taskjuggler-unique-ids' property in INFO.
546 (setq info
547 (plist-put info :taskjuggler-unique-ids
548 (org-taskjuggler-assign-resource-ids
549 main-resources info)))
550 (concat
551 (if main-resources
552 (mapconcat
553 (lambda (resource) (org-taskjuggler--build-resource resource info))
554 main-resources "")
555 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
556 ;; 5. Insert tasks.
557 (let ((main-tasks
558 ;; If `org-taskjuggler-keep-project-as-task' is
559 ;; non-nil, there is only one task. Otherwise, every
560 ;; direct children of PROJECT is a top level task.
561 (if org-taskjuggler-keep-project-as-task (list project)
562 (or (org-element-map (org-element-contents project) 'headline
563 'identity info nil 'headline)
564 (error "No task specified")))))
565 ;; Assign a unique ID to each task. Add it to
566 ;; `:taskjuggler-unique-ids' property in INFO.
567 (setq info
568 (plist-put info :taskjuggler-unique-ids
569 (append
570 (org-taskjuggler-assign-task-ids main-tasks info)
571 (plist-get info :taskjuggler-unique-ids))))
572 ;; If no resource is allocated among tasks, allocate one to
573 ;; the first task.
574 (unless (org-element-map main-tasks 'headline
575 (lambda (task) (org-element-property :ALLOCATE task))
576 info t)
577 (org-element-put-property
578 (car main-tasks) :ALLOCATE
579 (or (org-taskjuggler-get-id (car main-resources) info)
580 (user-login-name))))
581 (mapconcat
582 (lambda (task) (org-taskjuggler--build-task task info))
583 main-tasks ""))
584 ;; 6. Insert reports. If no report is defined, insert default
585 ;; reports.
586 (let ((main-reports
587 ;; Collect contents from various trees marked with
588 ;; `org-taskjuggler-report-tag'. Only gather top level
589 ;; reports.
590 (apply 'append
591 (org-element-map tree 'headline
592 (lambda (hl)
593 (and (member org-taskjuggler-report-tag
594 (org-export-get-tags hl info))
595 (org-element-map (org-element-contents hl)
596 'headline 'identity info nil 'headline)))
597 info nil 'headline))))
598 (if main-reports
599 (mapconcat
600 (lambda (report) (org-taskjuggler--build-report report info))
601 main-reports "")
602 (mapconcat 'org-element-normalize-string
603 org-taskjuggler-default-reports ""))))))))
605 (defun org-taskjuggler--build-project (project info)
606 "Return a project declaration.
607 PROJECT is a headline. INFO is a plist used as a communication
608 channel. If no start date is specified, start today. If no end
609 date is specified, end `org-taskjuggler-default-project-duration'
610 days from now."
611 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
612 (org-taskjuggler-get-id project info)
613 (org-taskjuggler-get-name project)
614 ;; Version is obtained through :TASKJUGGLER_VERSION:
615 ;; property or `org-taskjuggler-default-project-version'.
616 (or (org-element-property :VERSION project)
617 org-taskjuggler-default-project-version)
618 (or (org-taskjuggler-get-start project)
619 (format-time-string "%Y-%m-%d"))
620 (let ((end (org-taskjuggler-get-end project)))
621 (or (and end (format "- %s" end))
622 (format "+%sd" org-taskjuggler-default-project-duration)))))
624 (defun org-taskjuggler--build-resource (resource info)
625 "Return a resource declaration.
627 RESOURCE is a headline. INFO is a plist used as a communication
628 channel.
630 All valid attributes from RESOURCE are inserted. If RESOURCE
631 defines a property \"resource_id\" it will be used as the id for
632 this resource. Otherwise it will use the ID property. If
633 neither is defined a unique id will be associated to it."
634 (concat
635 ;; Opening resource.
636 (format "resource %s \"%s\" {\n"
637 (org-taskjuggler--clean-id
638 (or (org-element-property :RESOURCE_ID resource)
639 (org-element-property :ID resource)
640 (org-taskjuggler-get-id resource info)))
641 (org-taskjuggler-get-name resource))
642 ;; Add attributes.
643 (org-taskjuggler--indent-string
644 (org-taskjuggler--build-attributes
645 resource org-taskjuggler-valid-resource-attributes))
646 ;; Add inner resources.
647 (org-taskjuggler--indent-string
648 (mapconcat
649 'identity
650 (org-element-map (org-element-contents resource) 'headline
651 (lambda (hl) (org-taskjuggler--build-resource hl info))
652 info nil 'headline)
653 ""))
654 ;; Closing resource.
655 "}\n"))
657 (defun org-taskjuggler--build-report (report)
658 "Return a report declaration.
659 REPORT is a headline. INFO is a plist used as a communication
660 channel."
661 (concat
662 ;; Opening report.
663 (format "%s \"%s\" {\n"
664 (or (org-element-property :REPORT_KIND report) "taskreport")
665 (org-taskjuggler-get-name report))
666 ;; Add attributes.
667 (org-taskjuggler--indent-string
668 (org-taskjuggler--build-attributes
669 report org-taskjuggler-valid-report-attributes))
670 ;; Add inner reports.
671 (org-taskjuggler--indent-string
672 (mapconcat
673 'identity
674 (org-element-map (org-element-contents report) 'headline
675 (lambda (hl) (org-taskjuggler--build-report hl info))
676 info nil 'headline)
677 ""))
678 ;; Closing report.
679 "}\n"))
681 (defun org-taskjuggler--build-task (task info)
682 "Return a task declaration.
684 TASK is a headline. INFO is a plist used as a communication
685 channel.
687 All valid attributes from TASK are inserted. If TASK defines
688 a property \"task_id\" it will be used as the id for this task.
689 Otherwise it will use the ID property. If neither is defined
690 a unique id will be associated to it."
691 (let* ((allocate (org-element-property :ALLOCATE task))
692 (complete
693 (if (eq (org-element-property :todo-type task) 'done) "100"
694 (org-element-property :COMPLETE task)))
695 (depends (org-taskjuggler-resolve-dependencies task info))
696 (effort (org-element-property :EFFORT task))
697 (milestone
698 (or (org-element-property :MILESTONE task)
699 (and (org-element-map (org-element-contents task) 'headline
700 'identity info t) ; Has task any child?
701 (not (or effort
702 (org-element-property :LENGTH task)
703 (org-element-property :DURATION task)
704 (and (org-taskjuggler-get-start task)
705 (org-taskjuggler-get-end task))
706 (org-element-property :PERIOD task))))))
707 (priority
708 (let ((pri (org-element-property :priority task)))
709 (and pri
710 (max 1 (/ (* 1000 (- org-lowest-priority pri))
711 (- org-lowest-priority org-highest-priority)))))))
712 (concat
713 ;; Opening task.
714 (format "task %s \"%s\" {\n"
715 (org-taskjuggler-get-id task info)
716 (org-taskjuggler-get-name task))
717 ;; Add default attributes.
718 (and depends
719 (format " depends %s\n"
720 (org-taskjuggler-format-dependencies depends task info)))
721 (and allocate
722 (format " purge %s\n allocate %s\n"
723 ;; Compatibility for previous TaskJuggler versions.
724 (if (>= org-export-taskjuggler-target-version 3.0) "allocate"
725 "allocations")
726 allocate))
727 (and complete (format " complete %s\n" comptete))
728 (and effort
729 (format " effort %s\n"
730 (let* ((minutes (org-duration-string-to-minutes effort))
731 (hours (/ minutes 60.0)))
732 (format "%.1fh" hours))))
733 (and priority (format " priority %s\n" complete))
734 (and milestone " milestone\n")
735 ;; Add other valid attributes.
736 (org-taskjuggler--indent-string
737 (org-taskjuggler--build-attributes
738 task org-taskjuggler-valid-task-attributes))
739 ;; Add inner tasks.
740 (org-taskjuggler--indent-string
741 (mapconcat 'identity
742 (org-element-map (org-element-contents task) 'headline
743 (lambda (hl) (org-taskjuggler--build-task hl info))
744 info nil 'headline)
745 ""))
746 ;; Closing task.
747 "}\n")))
751 ;;; Interactive Functions
753 ;;;###autoload
754 (defun org-taskjuggler-export (&optional async subtreep visible-only)
755 "Export current buffer to a TaskJuggler file.
757 The exporter looks for a tree with tag that matches
758 `org-taskjuggler-project-tag' and takes this as the tasks for
759 this project. The first node of this tree defines the project
760 properties such as project name and project period.
762 If there is a tree with tag that matches
763 `org-taskjuggler-resource-tag' this tree is taken as resources
764 for the project. If no resources are specified, a default
765 resource is created and allocated to the project.
767 Also the TaskJuggler project will be created with default reports
768 as defined in `org-taskjuggler-default-reports'.
770 If narrowing is active in the current buffer, only export its
771 narrowed part.
773 If a region is active, export that region.
775 A non-nil optional argument ASYNC means the process should happen
776 asynchronously. The resulting file should be accessible through
777 the `org-export-stack' interface.
779 When optional argument SUBTREEP is non-nil, export the sub-tree
780 at point, extracting information from the headline properties
781 first.
783 When optional argument VISIBLE-ONLY is non-nil, don't export
784 contents of hidden elements.
786 Return output file's name."
787 (interactive)
788 (let ((outfile
789 (org-export-output-file-name org-taskjuggler-extension subtreep)))
790 (if async
791 (org-export-async-start
792 (lambda (f)
793 (org-export-add-to-stack f 'taskjuggler)
794 (run-hook-with-args 'org-taskjuggler-final-hook f))
795 `(expand-file-name
796 (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
797 (org-export-to-file 'taskjuggler outfile subtreep visible-only)
798 (run-hook-with-args 'org-taskjuggler-final-hook outfile)
799 outfile)))
801 ;;;###autoload
802 (defun org-taskjuggler-export-and-open (&optional subtreep visible-only)
803 "Export current buffer to a TaskJuggler file and open it.
805 The exporter looks for a tree with tag that matches
806 `org-taskjuggler-project-tag' and takes this as the tasks for
807 this project. The first node of this tree defines the project
808 properties such as project name and project period.
810 If there is a tree with tag that matches
811 `org-taskjuggler-resource-tag' this tree is taken as resources
812 for the project. If no resources are specified, a default
813 resource is created and allocated to the project.
815 Also the TaskJuggler project will be created with default reports
816 as defined in `org-taskjuggler-default-reports'.
818 If narrowing is active in the current buffer, only export its
819 narrowed part.
821 If a region is active, export that region.
823 When optional argument SUBTREEP is non-nil, export the sub-tree
824 at point, extracting information from the headline properties
825 first.
827 When optional argument VISIBLE-ONLY is non-nil, don't export
828 contents of hidden elements.
830 Open file with the TaskJuggler GUI."
831 (interactive)
832 (let* ((file (org-taskjuggler-export nil subtreep visible-only))
833 (process-name "TaskJugglerUI")
834 (command (concat process-name " " file)))
835 (start-process-shell-command process-name nil command)))
838 (provide 'ox-taskjuggler)
840 ;; Local variables:
841 ;; sentence-end-double-space: t
842 ;; End:
844 ;;; ox-taskjuggler.el ends here