ob-J: Do not use cl or cl-lib functions at runtime
[org-mode/org-tableheadings.git] / contrib / lisp / ox-taskjuggler.el
blob761e180f37402852af021eb58ba4bbd27554e157
1 ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
2 ;;
3 ;; Copyright (C) 2007-2014 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 file is not part of GNU Emacs.
15 ;; This program 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 ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Commentary:
30 ;; This library implements a TaskJuggler exporter for Org mode.
31 ;; TaskJuggler is a project planing tool that uses a text format to
32 ;; define projects, tasks and resources, so it is a natural fit for
33 ;; Org mode. It can produce all sorts of reports for tasks or
34 ;; resources in either HTML, CSV or PDF. TaskJuggler is implemented
35 ;; in Ruby and should therefore run on any platform.
37 ;; The exporter does not export all the nodes of a document or
38 ;; strictly follow the order of the nodes in the document.
40 ;; Instead the TaskJuggler exporter looks for a tree that defines the
41 ;; tasks and a optionally tree that defines the resources for this
42 ;; project. It then creates a TaskJuggler file based on these trees
43 ;; and the attributes defined in all the nodes.
45 ;; * Installation
47 ;; Put this file into your load-path and the following line into your
48 ;; ~/.emacs:
50 ;; (add-to-list 'org-export-backends 'taskjuggler)
52 ;; or customize `org-export-backends' variable.
54 ;; The interactive functions are the following:
56 ;; M-x `org-taskjuggler-export'
57 ;; M-x `org-taskjuggler-export-and-open'
59 ;; * Tasks
61 ;; Let's illustrate the usage with a small example. Create your tasks
62 ;; as you usually do with org-mode. Assign efforts to each task using
63 ;; properties (it's easiest to do this in the column view). You
64 ;; should end up with something similar to the example by Peter Jones
65 ;; in:
67 ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
69 ;; Now mark the top node of your tasks with a tag named
70 ;; "taskjuggler_project" (or whatever you customized
71 ;; `org-taskjuggler-project-tag' to). You are now ready to export the
72 ;; project plan with `org-taskjuggler-export-and-open' which will
73 ;; export the project plan and open a Gantt chart in 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-taskjuggler-resource-tag' to). You can optionally
81 ;; assign an identifier (named "resource_id") to the resources (using
82 ;; the standard org properties commands) or you can let the exporter
83 ;; generate identifiers automatically (the exporter picks the first
84 ;; word of the headline as the identifier as long as it is unique, see
85 ;; the documentation of `org-taskjuggler--build-unique-id'). Using that
86 ;; identifier you can then allocate resources to tasks. This is again
87 ;; done with the "allocate" property on the tasks. Do this in column
88 ;; view or when on the task type
90 ;; C-c C-x p allocate RET <resource_id> RET
92 ;; Once the allocations are done you can again export to TaskJuggler
93 ;; and check in the Resource Allocation Graph which person is working
94 ;; on what task at what time.
96 ;; * Export of properties
98 ;; The exporter also takes TODO state information into consideration,
99 ;; i.e. if a task is marked as done it will have the corresponding
100 ;; attribute in TaskJuggler ("complete 100"). Also it will export any
101 ;; property on a task resource or resource node which is known to
102 ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
103 ;; journalentry, rate for resources or account, start, note, duration,
104 ;; end, journalentry, milestone, reference, responsible, scheduling,
105 ;; etc for tasks.
107 ;; * Dependencies
109 ;; The exporter will handle dependencies that are defined in the tasks
110 ;; either with the ORDERED attribute (see TODO dependencies in the Org
111 ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
112 ;; alternatively with a depends attribute. Both the BLOCKER and the
113 ;; depends attribute can be either "previous-sibling" or a reference
114 ;; to an identifier (named "task_id") which is defined for another
115 ;; task in the project. BLOCKER and the depends attribute can define
116 ;; multiple dependencies separated by either space or comma. You can
117 ;; also specify optional attributes on the dependency by simply
118 ;; appending it. The following examples should illustrate this:
120 ;; * Training material
121 ;; :PROPERTIES:
122 ;; :task_id: training_material
123 ;; :ORDERED: t
124 ;; :END:
125 ;; ** Markup Guidelines
126 ;; :PROPERTIES:
127 ;; :Effort: 2d
128 ;; :END:
129 ;; ** Workflow Guidelines
130 ;; :PROPERTIES:
131 ;; :Effort: 2d
132 ;; :END:
133 ;; * Presentation
134 ;; :PROPERTIES:
135 ;; :Effort: 2d
136 ;; :BLOCKER: training_material { gapduration 1d } some_other_task
137 ;; :END:
139 ;;;; * TODO
140 ;; - Look at org-file-properties, org-global-properties and
141 ;; org-global-properties-fixed
142 ;; - What about property inheritance and org-property-inherit-p?
143 ;; - Use TYPE_TODO as an way to assign resources
144 ;; - Add support for org-export-with-planning
146 ;;; Code:
148 (eval-when-compile (require 'cl))
150 (require 'ox)
154 ;;; User Variables
156 (defgroup org-export-taskjuggler nil
157 "Options specific for TaskJuggler export back-end."
158 :tag "Org Export TaskJuggler"
159 :group 'org-export)
161 (defcustom org-taskjuggler-extension ".tjp"
162 "Extension of TaskJuggler files."
163 :group 'org-export-taskjuggler
164 :type 'string)
166 (defcustom org-taskjuggler-project-tag "taskjuggler_project"
167 "Tag marking project's tasks.
168 This tag is used to find the tree containing all the tasks for
169 the project."
170 :group 'org-export-taskjuggler
171 :type 'string)
173 (defcustom org-taskjuggler-resource-tag "taskjuggler_resource"
174 "Tag marking project's resources.
175 This tag is used to find the tree containing all the resources
176 for the project."
177 :group 'org-export-taskjuggler
178 :type 'string)
180 (defcustom org-taskjuggler-report-tag "taskjuggler_report"
181 "Tag marking project's reports.
182 This tag is used to find the tree containing all the reports for
183 the project."
184 :group 'org-export-taskjuggler
185 :type 'string)
187 (defcustom org-taskjuggler-target-version 3.0
188 "Which version of TaskJuggler the exporter is targeting.
189 By default a project plan is exported which conforms to version
190 3.x of TaskJuggler. For a project plan that is compatible with
191 versions of TaskJuggler older than 3.0 set this to 2.4.
193 If you change this variable be sure to also change
194 `org-taskjuggler-default-reports' as the format of reports has
195 changed considerably between version 2.x and 3.x of TaskJuggler"
196 :group 'org-export-taskjuggler
197 :type 'number)
199 (defcustom org-taskjuggler-default-project-version "1.0"
200 "Default version string for the project.
201 This value can also be set with the \":VERSION:\" property
202 associated to the headline defining the project."
203 :group 'org-export-taskjuggler
204 :type 'string)
206 (defcustom org-taskjuggler-default-project-duration 280
207 "Default project duration.
208 The value will be used if no start and end date have been defined
209 in the root node of the task tree, i.e. the tree that has been
210 marked with `org-taskjuggler-project-tag'"
211 :group 'org-export-taskjuggler
212 :type 'integer)
214 (defcustom org-taskjuggler-default-reports
215 '("textreport report \"Plan\" {
216 formats html
217 header '== %title =='
219 center -8<-
220 [#Plan Plan] | [#Resource_Allocation Resource Allocation]
221 ----
222 === Plan ===
223 <[report id=\"plan\"]>
224 ----
225 === Resource Allocation ===
226 <[report id=\"resourceGraph\"]>
227 ->8-
230 # A traditional Gantt chart with a project overview.
231 taskreport plan \"\" {
232 headline \"Project Plan\"
233 columns bsi, name, start, end, effort, chart
234 loadunit shortauto
235 hideresource 1
238 # A graph showing resource allocation. It identifies whether each
239 # resource is under- or over-allocated for.
240 resourcereport resourceGraph \"\" {
241 headline \"Resource Allocation Graph\"
242 columns no, name, effort, weekly
243 loadunit shortauto
244 hidetask ~(isleaf() & isleaf_())
245 sorttasks plan.start.up
247 "Default reports for the project.
248 These are sensible default reports to give a good out-of-the-box
249 result when exporting without defining any reports. \"%title\"
250 anywhere in the reports will be replaced with the document title.
251 If you want to define your own reports you can change them here
252 or simply define the default reports so that they include an
253 external report definition as follows:
255 include reports.tji
257 These default are made to work with tj3. If you are targeting
258 TaskJuggler 2.4 (see `org-taskjuggler-target-version') please
259 change these defaults to something like the following:
261 taskreport \"Gantt Chart\" {
262 headline \"Project Gantt Chart\"
263 columns hierarchindex, name, start, end, effort, duration, completed, chart
264 timeformat \"%Y-%m-%d\"
265 hideresource 1
266 loadunit shortauto
269 resourcereport \"Resource Graph\" {
270 headline \"Resource Allocation Graph\"
271 columns no, name, utilization, freeload, chart
272 loadunit shortauto
273 sorttasks startup
274 hidetask ~isleaf()
276 :group 'org-export-taskjuggler
277 :type '(repeat (string :tag "Report")))
279 (defcustom org-taskjuggler-default-global-header ""
280 "Default global header for the project.
281 This goes before project declaration, and might be useful for
282 early macros."
283 :group 'org-export-taskjuggler
284 :type '(string :tag "Preamble"))
286 (defcustom org-taskjuggler-default-global-properties
287 "shift s40 \"Part time shift\" {
288 workinghours wed, thu, fri off
291 "Default global properties for the project.
293 Here you typically define global properties such as shifts,
294 accounts, rates, vacation, macros and flags. Any property that
295 is allowed within the TaskJuggler file can be inserted. You
296 could for example include another TaskJuggler file.
298 The global properties are inserted after the project declaration
299 but before any resource and task declarations."
300 :group 'org-export-taskjuggler
301 :type '(string :tag "Preamble"))
303 (defcustom org-taskjuggler-valid-task-attributes
304 '(account start note duration endbuffer endcredit end
305 flags journalentry length limits maxend maxstart minend
306 minstart period reference responsible scheduling
307 startbuffer startcredit statusnote chargeset charge)
308 "Valid attributes for Taskjuggler tasks.
309 If one of these appears as a property for a headline, it will be
310 exported with the corresponding task."
311 :group 'org-export-taskjuggler)
313 (defcustom org-taskjuggler-valid-resource-attributes
314 '(limits vacation shift booking efficiency journalentry rate
315 workinghours flags)
316 "Valid attributes for Taskjuggler resources.
317 If one of these appears as a property for a headline, it will be
318 exported with the corresponding resource."
319 :group 'org-export-taskjuggler)
321 (defcustom org-taskjuggler-valid-report-attributes
322 '(headline columns definitions timeformat hideresource hidetask
323 loadunit sorttasks formats period)
324 "Valid attributes for Taskjuggler reports.
325 If one of these appears as a property for a headline, it will be
326 exported with the corresponding report."
327 :group 'org-export-taskjuggler)
329 (defcustom org-taskjuggler-process-command
330 "tj3 --silent --no-color --output-dir %o %f"
331 "Command to process a Taskjuggler file.
332 The command will be given to the shell as a command to process a
333 Taskjuggler file. \"%f\" in the command will be replaced by the
334 full file name, \"%o\" by the reports directory (see
335 `org-taskjuggler-reports-directory').
337 If you are targeting Taskjuggler 2.4 (see
338 `org-taskjuggler-target-version') this setting is ignored."
339 :group 'org-export-taskjuggler)
341 (defcustom org-taskjuggler-reports-directory "reports"
342 "Default directory to generate the Taskjuggler reports in.
343 The command `org-taskjuggler-process-command' generates the
344 reports and associated files such as CSS inside this directory.
346 If the directory is not an absolute path it is relative to the
347 directory of the exported file. The directory is created if it
348 doesn't exist.
350 If you are targeting Taskjuggler 2.4 (see
351 `org-taskjuggler-target-version') this setting is ignored."
352 :group 'org-export-taskjuggler)
354 (defcustom org-taskjuggler-keep-project-as-task t
355 "Non-nil keeps the project headline as an umbrella task for all tasks.
356 Setting this to nil will allow maintaining completely separated
357 task buckets, while still sharing the same resources pool."
358 :group 'org-export-taskjuggler
359 :type 'boolean)
363 ;;; Hooks
365 (defvar org-taskjuggler-final-hook nil
366 "Hook run after a TaskJuggler files has been saved.
367 This hook is run with the name of the file as argument.")
371 ;;; Back-End Definition
373 (org-export-define-backend 'taskjuggler
374 '((template . org-taskjuggler-project-plan))
375 :menu-entry
376 '(?J "Export to TaskJuggler"
377 ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
378 (?p "As TJP file and process"
379 (lambda (a s v b)
380 (if a (org-taskjuggler-export a s v)
381 (org-taskjuggler-export-and-process s v))))
382 (?o "As TJP file, process and open"
383 (lambda (a s v b)
384 (if a (org-taskjuggler-export a s v)
385 (org-taskjuggler-export-process-and-open s v))))))
386 ;; This property will be used to store unique ids in communication
387 ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
388 :options-alist '((:taskjuggler-unique-ids nil nil nil)))
392 ;;; Unique IDs
394 (defun org-taskjuggler-assign-task-ids (tasks info)
395 "Assign a unique ID to each task in TASKS.
396 TASKS is a list of headlines. INFO is a plist used as a
397 communication channel. Return value is an alist between
398 headlines and their associated ID. IDs are hierarchical, which
399 means they only need to be unique among the task siblings."
400 (let* (alist
401 build-id ; For byte-compiler.
402 (build-id
403 (lambda (tasks local-ids)
404 (org-element-map tasks 'headline
405 (lambda (task)
406 (let ((id (org-taskjuggler--build-unique-id task local-ids)))
407 (push id local-ids)
408 (push (cons task id) alist)
409 (funcall build-id (org-element-contents task) nil)))
410 info nil 'headline))))
411 (funcall build-id tasks nil)
412 alist))
414 (defun org-taskjuggler-assign-resource-ids (resources info)
415 "Assign a unique ID to each resource within RESOURCES.
416 RESOURCES is a list of headlines. INFO is a plist used as a
417 communication channel. Return value is an alist between
418 headlines and their associated ID."
419 (let (ids)
420 (org-element-map resources 'headline
421 (lambda (resource)
422 (let ((id (org-taskjuggler--build-unique-id resource ids)))
423 (push id ids)
424 (cons resource id)))
425 info)))
429 ;;; Accessors
431 (defun org-taskjuggler-get-project (info)
432 "Return project in parse tree.
433 INFO is a plist used as a communication channel. First headline
434 in buffer with `org-taskjuggler-project-tag' defines the project.
435 If no such task is defined, pick the first headline in buffer.
436 If there is no headline at all, return nil."
437 (let ((tree (plist-get info :parse-tree)))
438 (or (org-element-map tree 'headline
439 (lambda (hl)
440 (and (member org-taskjuggler-project-tag
441 (org-export-get-tags hl info))
442 hl))
443 info t)
444 (org-element-map tree 'headline 'identity info t))))
446 (defun org-taskjuggler-get-id (item info)
447 "Return id for task or resource ITEM.
448 ITEM is a headline. INFO is a plist used as a communication
449 channel. Return value is a string."
450 (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
452 (defun org-taskjuggler-get-name (item)
453 "Return name for task or resource ITEM.
454 ITEM is a headline. Return value is a string."
455 ;; Quote double quotes in name.
456 (replace-regexp-in-string
457 "\"" "\\\"" (org-element-property :raw-value item) t t))
459 (defun org-taskjuggler-get-start (item)
460 "Return start date for task or resource ITEM.
461 ITEM is a headline. Return value is a string or nil if ITEM
462 doesn't have any start date defined."
463 (let ((scheduled (org-element-property :scheduled item)))
465 (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))
466 (and (memq 'start org-taskjuggler-valid-task-attributes)
467 (org-element-property :START item)))))
469 (defun org-taskjuggler-get-end (item)
470 "Return end date for task or resource ITEM.
471 ITEM is a headline. Return value is a string or nil if ITEM
472 doesn't have any end date defined."
473 (let ((deadline (org-element-property :deadline item)))
474 (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
478 ;;; Internal Functions
480 (defun org-taskjuggler--indent-string (s)
481 "Indent string S by 2 spaces.
482 Return new string. If S is the empty string, return it."
483 (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
485 (defun org-taskjuggler--build-attributes (item attributes)
486 "Return attributes string for task, resource or report ITEM.
487 ITEM is a headline. ATTRIBUTES is a list of symbols
488 representing valid attributes for ITEM."
489 (mapconcat
490 (lambda (attribute)
491 (let ((value (org-element-property
492 (intern (upcase (format ":%s" attribute)))
493 item)))
494 (and value (format "%s %s\n" attribute value))))
495 (remq nil attributes) ""))
497 (defun org-taskjuggler--build-unique-id (item unique-ids)
498 "Return a unique id for a given task or a resource.
499 ITEM is an `headline' type element representing the task or
500 resource. Its id is derived from its name and made unique
501 against UNIQUE-IDS. If the (downcased) first token of the
502 headline is not unique try to add more (downcased) tokens of the
503 headline or finally add more underscore characters (\"_\")."
504 (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
505 ;; If an id is specified, use it, as long as it's unique.
506 (if (and id (not (member id unique-ids))) id
507 (let* ((parts (org-split-string (org-element-property :raw-value item)))
508 (id (org-taskjuggler--clean-id (downcase (pop parts)))))
509 ;; Try to add more parts of the headline to make it unique.
510 (while (and (car parts) (member id unique-ids))
511 (setq id (concat id "_"
512 (org-taskjuggler--clean-id (downcase (pop parts))))))
513 ;; If it's still not unique, add "_".
514 (while (member id unique-ids)
515 (setq id (concat id "_")))
516 id))))
518 (defun org-taskjuggler--clean-id (id)
519 "Clean and return ID to make it acceptable for TaskJuggler.
520 ID is a string."
521 ;; Replace non-ascii by "_".
522 (replace-regexp-in-string
523 "[^a-zA-Z0-9_]" "_"
524 ;; Make sure id doesn't start with a number.
525 (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
529 ;;; Dependencies
531 (defun org-taskjuggler-resolve-dependencies (task info)
532 "Return a list of all tasks TASK depends on.
533 TASK is a headline. INFO is a plist used as a communication
534 channel."
535 (let ((deps-ids
536 ;; Get all dependencies specified in BLOCKER and DEPENDS task
537 ;; properties. Clean options from them.
538 (let ((deps (concat (org-element-property :BLOCKER task)
539 (org-element-property :DEPENDS task))))
540 (and deps
541 (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
542 "[ ,]* +"))))
543 depends)
544 (when deps-ids
545 ;; Find tasks with :task_id: property matching id in DEPS-IDS.
546 ;; Add them to DEPENDS.
547 (let* ((project (org-taskjuggler-get-project info))
548 (tasks (if org-taskjuggler-keep-project-as-task project
549 (org-element-contents project))))
550 (setq depends
551 (org-element-map tasks 'headline
552 (lambda (task)
553 (let ((task-id (or (org-element-property :TASK_ID task)
554 (org-element-property :ID task))))
555 (and task-id (member task-id deps-ids) task)))
556 info)))
557 ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
558 ;; belongs to DEPS-ID, add it to DEPENDS.
559 (when (and (member-ignore-case "previous-sibling" deps-ids)
560 (not (org-export-first-sibling-p task info)))
561 (let ((prev (org-export-get-previous-element task info)))
562 (and (not (memq prev depends)) (push prev depends)))))
563 ;; Check ORDERED status of parent.
564 (let ((parent (org-export-get-parent task)))
565 (when (and parent
566 (org-element-property :ORDERED parent)
567 (not (org-export-first-sibling-p task info)))
568 (push (org-export-get-previous-element task info) depends)))
569 ;; Return dependencies.
570 depends))
572 (defun org-taskjuggler-format-dependencies (dependencies task info)
573 "Format DEPENDENCIES to match TaskJuggler syntax.
574 DEPENDENCIES is list of dependencies for TASK, as returned by
575 `org-taskjuggler-resolve-depedencies'. TASK is a headline.
576 INFO is a plist used as a communication channel. Return value
577 doesn't include leading \"depends\"."
578 (let* ((dep-str (concat (org-element-property :BLOCKER task)
580 (org-element-property :DEPENDS task)))
581 (get-path
582 (lambda (dep)
583 ;; Return path to DEP relatively to TASK.
584 (let ((parent (org-export-get-parent task))
585 (exclamations 1)
586 (option
587 (let ((id (org-element-property :TASK_ID dep)))
588 (and id
589 (string-match (concat id " +\\({.*?}\\)") dep-str)
590 (org-match-string-no-properties 1))))
591 path)
592 ;; Compute number of exclamation marks by looking for the
593 ;; common ancestor between TASK and DEP.
594 (while (not (org-element-map parent 'headline
595 (lambda (hl) (eq hl dep))))
596 (incf exclamations)
597 (setq parent (org-export-get-parent parent)))
598 ;; Build path from DEP to PARENT.
599 (while (not (eq parent dep))
600 (push (org-taskjuggler-get-id dep info) path)
601 (setq dep (org-export-get-parent dep)))
602 ;; Return full path. Add dependency options, if any.
603 (concat (make-string exclamations ?!)
604 (mapconcat 'identity path ".")
605 (and option (concat " " option)))))))
606 ;; Return dependencies string, without the leading "depends".
607 (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
611 ;;; Translator Functions
613 (defun org-taskjuggler-project-plan (contents info)
614 "Build TaskJuggler project plan.
615 CONTENTS is ignored. INFO is a plist holding export options.
616 Return complete project plan as a string in TaskJuggler syntax."
617 (let* ((tree (plist-get info :parse-tree))
618 (project (or (org-taskjuggler-get-project info)
619 (error "No project specified"))))
620 (concat
621 ;; 1. Insert header.
622 (org-element-normalize-string org-taskjuggler-default-global-header)
623 ;; 2. Insert project.
624 (org-taskjuggler--build-project project info)
625 ;; 3. Insert global properties.
626 (org-element-normalize-string org-taskjuggler-default-global-properties)
627 ;; 4. Insert resources. Provide a default one if none is
628 ;; specified.
629 (let ((main-resources
630 ;; Collect contents from various trees marked with
631 ;; `org-taskjuggler-resource-tag'. Only gather top level
632 ;; resources.
633 (apply 'append
634 (org-element-map tree 'headline
635 (lambda (hl)
636 (and (member org-taskjuggler-resource-tag
637 (org-export-get-tags hl info))
638 (org-element-map (org-element-contents hl) 'headline
639 'identity info nil 'headline)))
640 info nil 'headline))))
641 ;; Assign a unique ID to each resource. Store it under
642 ;; `:taskjuggler-unique-ids' property in INFO.
643 (setq info
644 (plist-put info :taskjuggler-unique-ids
645 (org-taskjuggler-assign-resource-ids
646 main-resources info)))
647 (concat
648 (if main-resources
649 (mapconcat
650 (lambda (resource) (org-taskjuggler--build-resource resource info))
651 main-resources "")
652 (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
653 ;; 5. Insert tasks.
654 (let ((main-tasks
655 ;; If `org-taskjuggler-keep-project-as-task' is
656 ;; non-nil, there is only one task. Otherwise, every
657 ;; direct children of PROJECT is a top level task.
658 (if org-taskjuggler-keep-project-as-task (list project)
659 (or (org-element-map (org-element-contents project) 'headline
660 'identity info nil 'headline)
661 (error "No task specified")))))
662 ;; Assign a unique ID to each task. Add it to
663 ;; `:taskjuggler-unique-ids' property in INFO.
664 (setq info
665 (plist-put info :taskjuggler-unique-ids
666 (append
667 (org-taskjuggler-assign-task-ids main-tasks info)
668 (plist-get info :taskjuggler-unique-ids))))
669 ;; If no resource is allocated among tasks, allocate one to
670 ;; the first task.
671 (unless (org-element-map main-tasks 'headline
672 (lambda (task) (org-element-property :ALLOCATE task))
673 info t)
674 (org-element-put-property
675 (car main-tasks) :ALLOCATE
676 (or (org-taskjuggler-get-id (car main-resources) info)
677 (user-login-name))))
678 (mapconcat
679 (lambda (task) (org-taskjuggler--build-task task info))
680 main-tasks ""))
681 ;; 6. Insert reports. If no report is defined, insert default
682 ;; reports.
683 (let ((main-reports
684 ;; Collect contents from various trees marked with
685 ;; `org-taskjuggler-report-tag'. Only gather top level
686 ;; reports.
687 (apply 'append
688 (org-element-map tree 'headline
689 (lambda (hl)
690 (and (member org-taskjuggler-report-tag
691 (org-export-get-tags hl info))
692 (org-element-map (org-element-contents hl)
693 'headline 'identity info nil 'headline)))
694 info nil 'headline))))
695 (if main-reports
696 (mapconcat
697 (lambda (report) (org-taskjuggler--build-report report info))
698 main-reports "")
699 ;; insert title in default reports
700 (let* ((title (org-export-data (plist-get info :title) info))
701 (report-title (if (string= title "")
702 (org-taskjuggler-get-name project)
703 title)))
704 (mapconcat
705 'org-element-normalize-string
706 (mapcar
707 (function
708 (lambda (report)
709 (replace-regexp-in-string "%title" report-title report t t)))
710 org-taskjuggler-default-reports) "")))))))))
712 (defun org-taskjuggler--build-project (project info)
713 "Return a project declaration.
714 PROJECT is a headline. INFO is a plist used as a communication
715 channel. If no start date is specified, start today. If no end
716 date is specified, end `org-taskjuggler-default-project-duration'
717 days from now."
718 (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
719 (org-taskjuggler-get-id project info)
720 (org-taskjuggler-get-name project)
721 ;; Version is obtained through :TASKJUGGLER_VERSION:
722 ;; property or `org-taskjuggler-default-project-version'.
723 (or (org-element-property :VERSION project)
724 org-taskjuggler-default-project-version)
725 (or (org-taskjuggler-get-start project)
726 (format-time-string "%Y-%m-%d"))
727 (let ((end (org-taskjuggler-get-end project)))
728 (or (and end (format "- %s" end))
729 (format "+%sd" org-taskjuggler-default-project-duration)))))
731 (defun org-taskjuggler--build-resource (resource info)
732 "Return a resource declaration.
734 RESOURCE is a headline. INFO is a plist used as a communication
735 channel.
737 All valid attributes from RESOURCE are inserted. If RESOURCE
738 defines a property \"resource_id\" it will be used as the id for
739 this resource. Otherwise it will use the ID property. If
740 neither is defined a unique id will be associated to it."
741 (concat
742 ;; Opening resource.
743 (format "resource %s \"%s\" {\n"
744 (org-taskjuggler--clean-id
745 (or (org-element-property :RESOURCE_ID resource)
746 (org-element-property :ID resource)
747 (org-taskjuggler-get-id resource info)))
748 (org-taskjuggler-get-name resource))
749 ;; Add attributes.
750 (org-taskjuggler--indent-string
751 (org-taskjuggler--build-attributes
752 resource org-taskjuggler-valid-resource-attributes))
753 ;; Add inner resources.
754 (org-taskjuggler--indent-string
755 (mapconcat
756 'identity
757 (org-element-map (org-element-contents resource) 'headline
758 (lambda (hl) (org-taskjuggler--build-resource hl info))
759 info nil 'headline)
760 ""))
761 ;; Closing resource.
762 "}\n"))
764 (defun org-taskjuggler--build-report (report info)
765 "Return a report declaration.
766 REPORT is a headline. INFO is a plist used as a communication
767 channel."
768 (concat
769 ;; Opening report.
770 (format "%s \"%s\" {\n"
771 (or (org-element-property :REPORT_KIND report) "taskreport")
772 (org-taskjuggler-get-name report))
773 ;; Add attributes.
774 (org-taskjuggler--indent-string
775 (org-taskjuggler--build-attributes
776 report org-taskjuggler-valid-report-attributes))
777 ;; Add inner reports.
778 (org-taskjuggler--indent-string
779 (mapconcat
780 'identity
781 (org-element-map (org-element-contents report) 'headline
782 (lambda (hl) (org-taskjuggler--build-report hl info))
783 info nil 'headline)
784 ""))
785 ;; Closing report.
786 "}\n"))
788 (defun org-taskjuggler--build-task (task info)
789 "Return a task declaration.
791 TASK is a headline. INFO is a plist used as a communication
792 channel.
794 All valid attributes from TASK are inserted. If TASK defines
795 a property \"task_id\" it will be used as the id for this task.
796 Otherwise it will use the ID property. If neither is defined
797 a unique id will be associated to it."
798 (let* ((allocate (org-element-property :ALLOCATE task))
799 (complete
800 (if (eq (org-element-property :todo-type task) 'done) "100"
801 (org-element-property :COMPLETE task)))
802 (depends (org-taskjuggler-resolve-dependencies task info))
803 (effort (let ((property
804 (intern (concat ":" (upcase org-effort-property)))))
805 (org-element-property property task)))
806 (milestone
807 (or (org-element-property :MILESTONE task)
808 (not (or (org-element-map (org-element-contents task) 'headline
809 'identity info t) ; Has task any child?
810 effort
811 (org-element-property :LENGTH task)
812 (org-element-property :DURATION task)
813 (and (org-taskjuggler-get-start task)
814 (org-taskjuggler-get-end task))
815 (org-element-property :PERIOD task)))))
816 (priority
817 (let ((pri (org-element-property :priority task)))
818 (and pri
819 (max 1 (/ (* 1000 (- org-lowest-priority pri))
820 (- org-lowest-priority org-highest-priority)))))))
821 (concat
822 ;; Opening task.
823 (format "task %s \"%s\" {\n"
824 (org-taskjuggler-get-id task info)
825 (org-taskjuggler-get-name task))
826 ;; Add default attributes.
827 (and depends
828 (format " depends %s\n"
829 (org-taskjuggler-format-dependencies depends task info)))
830 (and allocate
831 (format " purge %s\n allocate %s\n"
832 ;; Compatibility for previous TaskJuggler versions.
833 (if (>= org-taskjuggler-target-version 3.0) "allocate"
834 "allocations")
835 allocate))
836 (and complete (format " complete %s\n" complete))
837 (and effort
838 (format " effort %s\n"
839 (let* ((minutes (org-duration-string-to-minutes effort))
840 (hours (/ minutes 60.0)))
841 (format "%.1fh" hours))))
842 (and priority (format " priority %s\n" priority))
843 (and milestone " milestone\n")
844 ;; Add other valid attributes.
845 (org-taskjuggler--indent-string
846 (org-taskjuggler--build-attributes
847 task org-taskjuggler-valid-task-attributes))
848 ;; Add inner tasks.
849 (org-taskjuggler--indent-string
850 (mapconcat 'identity
851 (org-element-map (org-element-contents task) 'headline
852 (lambda (hl) (org-taskjuggler--build-task hl info))
853 info nil 'headline)
854 ""))
855 ;; Closing task.
856 "}\n")))
860 ;;; Interactive Functions
862 ;;;###autoload
863 (defun org-taskjuggler-export (&optional async subtreep visible-only)
864 "Export current buffer to a TaskJuggler file.
866 The exporter looks for a tree with tag that matches
867 `org-taskjuggler-project-tag' and takes this as the tasks for
868 this project. The first node of this tree defines the project
869 properties such as project name and project period.
871 If there is a tree with tag that matches
872 `org-taskjuggler-resource-tag' this tree is taken as resources
873 for the project. If no resources are specified, a default
874 resource is created and allocated to the project.
876 Also the TaskJuggler project will be created with default reports
877 as defined in `org-taskjuggler-default-reports'.
879 If narrowing is active in the current buffer, only export its
880 narrowed part.
882 If a region is active, export that region.
884 A non-nil optional argument ASYNC means the process should happen
885 asynchronously. The resulting file should be accessible through
886 the `org-export-stack' interface.
888 When optional argument SUBTREEP is non-nil, export the sub-tree
889 at point, extracting information from the headline properties
890 first.
892 When optional argument VISIBLE-ONLY is non-nil, don't export
893 contents of hidden elements.
895 Return output file's name."
896 (interactive)
897 (let ((outfile
898 (org-export-output-file-name org-taskjuggler-extension subtreep)))
899 (org-export-to-file 'taskjuggler outfile
900 async subtreep visible-only nil nil
901 (lambda (file)
902 (run-hook-with-args 'org-taskjuggler-final-hook file) nil))))
904 ;;;###autoload
905 (defun org-taskjuggler-export-and-process (&optional subtreep visible-only)
906 "Export current buffer to a TaskJuggler file and process it.
908 The exporter looks for a tree with tag that matches
909 `org-taskjuggler-project-tag' and takes this as the tasks for
910 this project. The first node of this tree defines the project
911 properties such as project name and project period.
913 If there is a tree with tag that matches
914 `org-taskjuggler-resource-tag' this tree is taken as resources
915 for the project. If no resources are specified, a default
916 resource is created and allocated to the project.
918 Also the TaskJuggler project will be created with default reports
919 as defined in `org-taskjuggler-default-reports'.
921 If narrowing is active in the current buffer, only export its
922 narrowed part.
924 If a region is active, export that region.
926 When optional argument SUBTREEP is non-nil, export the sub-tree
927 at point, extracting information from the headline properties
928 first.
930 When optional argument VISIBLE-ONLY is non-nil, don't export
931 contents of hidden elements.
933 Return a list of reports."
934 (interactive)
935 (let ((file (org-taskjuggler-export nil subtreep visible-only)))
936 (org-taskjuggler-compile file)))
938 ;;;###autoload
939 (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only)
940 "Export current buffer to a TaskJuggler file, process and open it.
942 Export and process the file using
943 `org-taskjuggler-export-and-process' and open the generated
944 reports with a browser.
946 If you are targeting TaskJuggler 2.4 (see
947 `org-taskjuggler-target-version') the processing and display of
948 the reports is done using the TaskJuggler GUI."
949 (interactive)
950 (if (< org-taskjuggler-target-version 3.0)
951 (let* ((process-name "TaskJugglerUI")
952 (command
953 (concat process-name " "
954 (org-taskjuggler-export nil subtreep visible-only))))
955 (start-process-shell-command process-name nil command))
956 (dolist (report (org-taskjuggler-export-and-process subtreep visible-only))
957 (org-open-file report))))
959 (defun org-taskjuggler-compile (file)
960 "Compile a TaskJuggler file.
962 FILE is the name of the file being compiled. Processing is done
963 through the command given in `org-taskjuggler-process-command'.
965 Return a list of reports."
966 (let* ((full-name (file-truename file))
967 (out-dir
968 (expand-file-name
969 org-taskjuggler-reports-directory (file-name-directory file)))
970 errors)
971 (message (format "Processing TaskJuggler file %s..." file))
972 (save-window-excursion
973 (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
974 (unless (file-directory-p out-dir)
975 (make-directory out-dir t))
976 (with-current-buffer outbuf (erase-buffer))
977 (shell-command
978 (replace-regexp-in-string
979 "%f" (shell-quote-argument full-name)
980 (replace-regexp-in-string
981 "%o" (shell-quote-argument out-dir)
982 org-taskjuggler-process-command t t) t t) outbuf)
983 ;; Collect standard errors from output buffer.
984 (setq errors (org-taskjuggler--collect-errors outbuf)))
985 (if (not errors)
986 (message "Process completed.")
987 (error (format "TaskJuggler failed with errors: %s" errors))))
988 (file-expand-wildcards (format "%s/*.html" out-dir))))
990 (defun org-taskjuggler--collect-errors (buffer)
991 "Collect some kind of errors from \"tj3\" command output.
993 BUFFER is the buffer containing output.
995 Return collected error types as a string, or nil if there was
996 none."
997 (with-current-buffer buffer
998 (save-excursion
999 (goto-char (point-min))
1000 (let ((case-fold-search t)
1001 (errors ""))
1002 (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t)
1003 (setq errors (concat errors " " (match-string 1))))
1004 (and (org-string-nw-p errors) (org-trim errors))))))
1007 (provide 'ox-taskjuggler)
1009 ;; Local variables:
1010 ;; sentence-end-double-space: t
1011 ;; End:
1013 ;;; ox-taskjuggler.el ends here