org-taskjuggler.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. ;;; org-taskjuggler.el --- TaskJuggler exporter for org-mode
  2. ;;
  3. ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: org-taskjuggler.el
  7. ;; Version: 6.34trans
  8. ;; Author: Christian Egli
  9. ;; Maintainer: Christian Egli
  10. ;; Keywords: org, taskjuggler, project planning
  11. ;; Description: Converts an org-mode buffer into a taskjuggler project plan
  12. ;; URL:
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs 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.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;; Commentary:
  25. ;;
  26. ;; This library implements a TaskJuggler exporter for org-mode.
  27. ;; TaskJuggler uses a text format to define projects, tasks and
  28. ;; resources, so it is a natural fit for org-mode. It can produce all
  29. ;; sorts of reports for tasks or resources in either HTML, CSV or PDF.
  30. ;; The current version of TaskJuggler requires KDE but the next
  31. ;; version is implemented in Ruby and should therefore run on any
  32. ;; platform.
  33. ;;
  34. ;; The exporter is a bit different from other exporters, such as the
  35. ;; HTML and LaTeX exporters for example, in that it does not export
  36. ;; all the nodes of a document or strictly follow the order of the
  37. ;; nodes in the document.
  38. ;;
  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.
  43. ;;
  44. ;; * Installation
  45. ;;
  46. ;; Put this file into your load-path and the following line into your
  47. ;; ~/.emacs:
  48. ;;
  49. ;; (require 'org-taskjuggler)
  50. ;;
  51. ;; The interactive functions are similar to those of the HTML and LaTeX
  52. ;; exporters:
  53. ;;
  54. ;; M-x `org-export-as-taskjuggler'
  55. ;; M-x `org-export-as-taskjuggler-and-open'
  56. ;;
  57. ;; * Tasks
  58. ;;
  59. ;; Let's illustrate the usage with a small example. Create your tasks
  60. ;; as you usually do with org-mode. Assign efforts to each task using
  61. ;; properties (it's easiest to do this in the column view). You should
  62. ;; end up with something similar to the example by Peter Jones in
  63. ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
  64. ;; Now mark the top node of your tasks with a tag named
  65. ;; "taskjuggler_project" (or whatever you customized
  66. ;; `org-export-taskjuggler-project-tag' to). You are now ready to
  67. ;; export the project plan with `org-export-as-taskjuggler-and-open'
  68. ;; which will export the project plan and open a gant chart in
  69. ;; TaskJugglerUI.
  70. ;;
  71. ;; * Resources
  72. ;;
  73. ;; Next you can define resources and assign those to work on specific
  74. ;; tasks. You can group your resources hierarchically. Tag the top
  75. ;; node of the resources with "taskjuggler_resource" (or whatever you
  76. ;; customized `org-export-taskjuggler-resource-tag' to). You can
  77. ;; optionally assign an ID to the resources (using the standard org
  78. ;; properties commands) or you can let the exporter generate IDs
  79. ;; automatically (the exporter picks the first word of the headline as
  80. ;; the ID as long as it is unique, see the documentation of
  81. ;; `org-taskjuggler-get-unique-id'). Using that ID you can then
  82. ;; allocate resources to tasks. This is again done with the "allocate"
  83. ;; property on the tasks. Do this in column view or when on the task
  84. ;; type
  85. ;;
  86. ;; C-c C-x p allocate RET <ID> RET
  87. ;;
  88. ;; Once the allocations are done you can again export to TaskJuggler
  89. ;; and check in the Resource Allocation Graph which person is working
  90. ;; on what task at what time.
  91. ;;
  92. ;; * Export of properties
  93. ;;
  94. ;; The exporter also takes TODO state information into consideration,
  95. ;; i.e. if a task is marked as done it will have the corresponding
  96. ;; attribute in TaskJuggler ("complete 100"). Also it will export any
  97. ;; property on a task resource or resource node which is known to
  98. ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
  99. ;; journalentry, rate for resources or account, start, note, duration,
  100. ;; end, journalentry, milestone, reference, responsible, scheduling,
  101. ;; etc for tasks.
  102. ;;
  103. ;; * Dependencies
  104. ;;
  105. ;; The exporter will handle dependencies that are defined in the tasks
  106. ;; either with the ORDERED attribute (see TODO dependencies in the Org
  107. ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
  108. ;; alternatively with a depends attribute. Both the BLOCKER and the
  109. ;; depends attribute can be either "previous-sibling" or a reference
  110. ;; to an ID which is defined for another task in the project. BLOCKER
  111. ;; and the depends attribute can define multiple dependencies
  112. ;; separated by either space or comma. You can also specify optional
  113. ;; attributes on the dependency by simply appending it. The following
  114. ;; examples should illustrate this:
  115. ;;
  116. ;; * Training material
  117. ;; :PROPERTIES:
  118. ;; :ID: training_material
  119. ;; :ORDERED: t
  120. ;; :END:
  121. ;; ** Markup Guidelines
  122. ;; :PROPERTIES:
  123. ;; :Effort: 2.0
  124. ;; :END:
  125. ;; ** Workflow Guidelines
  126. ;; :PROPERTIES:
  127. ;; :Effort: 2.0
  128. ;; :END:
  129. ;; * Presentation
  130. ;; :PROPERTIES:
  131. ;; :Effort: 2.0
  132. ;; :BLOCKER: training_material { gapduration 1d } some_other_task
  133. ;; :END:
  134. ;;
  135. ;;;; * TODO
  136. ;; - Look at org-file-properties, org-global-properties and
  137. ;; org-global-properties-fixed
  138. ;; - What about property inheritance and org-property-inherit-p?
  139. ;; - Use TYPE_TODO as an way to assign resources
  140. ;; - Make sure multiple dependency definitions (i.e. BLOCKER on
  141. ;; previous-sibling and on a specific ID) in multiple attributes
  142. ;; are properly exported.
  143. ;; - Fix compiler warnings about reference and assignment to free
  144. ;; variable `old-level' in org-taskjuggler-close-maybe
  145. ;;
  146. ;;; Code:
  147. (eval-when-compile
  148. (require 'cl))
  149. (require 'org)
  150. (require 'org-exp)
  151. ;;; User variables:
  152. (defgroup org-export-taskjuggler nil
  153. "Options for exporting Org-mode files to TaskJuggler."
  154. :tag "Org Export TaskJuggler"
  155. :group 'org-export)
  156. (defcustom org-export-taskjuggler-extension ".tjp"
  157. "Extension of TaskJuggler files."
  158. :group 'org-export-taskjuggler
  159. :type 'string)
  160. (defcustom org-export-taskjuggler-project-tag "taskjuggler_project"
  161. "Tag, property or todo used to find the tree containing all
  162. the tasks for the project."
  163. :group 'org-export-taskjuggler
  164. :type 'string)
  165. (defcustom org-export-taskjuggler-resource-tag "taskjuggler_resource"
  166. "Tag, property or todo used to find the tree containing all the
  167. resources for the project."
  168. :group 'org-export-taskjuggler
  169. :type 'string)
  170. (defcustom org-export-taskjuggler-default-project-version "1.0"
  171. "Default version string for the project."
  172. :group 'org-export-taskjuggler
  173. :type 'string)
  174. (defcustom org-export-taskjuggler-default-project-duration 280
  175. "Default project duration if no start and end date have been defined
  176. in the root node of the task tree, i.e. the tree that has been marked
  177. with `org-export-taskjuggler-project-tag'"
  178. :group 'org-export-taskjuggler
  179. :type 'integer)
  180. (defcustom org-export-taskjuggler-default-reports
  181. '("taskreport \"Gantt Chart\" {
  182. headline \"Project Gantt Chart\"
  183. columns hierarchindex, name, start, end, effort, duration, completed, chart
  184. timeformat \"%Y-%m-%d\"
  185. hideresource 1
  186. loadunit shortauto
  187. }"
  188. "resourcereport \"Resource Graph\" {
  189. headline \"Resource Allocation Graph\"
  190. columns no, name, utilization, freeload, chart
  191. loadunit shortauto
  192. sorttasks startup
  193. hidetask ~isleaf()
  194. }")
  195. "Default reports for the project."
  196. :group 'org-export-taskjuggler
  197. :type '(repeat (string :tag "Report")))
  198. (defcustom org-export-taskjuggler-default-global-properties
  199. "shift s40 \"Part time shift\" {
  200. workinghours wed, thu, fri off
  201. }
  202. "
  203. "Default global properties for the project. Here you typically
  204. define global properties such as shifts, accounts, rates,
  205. vacation, macros and flags. Any property that is allowed within
  206. the TaskJuggler file can be inserted. You could for example
  207. include another TaskJuggler file.
  208. The global properties are inserted after the project declaration
  209. but before any resource and task declarations."
  210. :group 'org-export-taskjuggler
  211. :type '(string :tag "Preamble"))
  212. ;;; Hooks
  213. (defvar org-export-taskjuggler-final-hook nil
  214. "Hook run at the end of TaskJuggler export, in the new buffer.")
  215. ;;; Autoload functions:
  216. ;;;###autoload
  217. (defun org-export-as-taskjuggler ()
  218. "Export parts of the current buffer as a TaskJuggler file.
  219. The exporter looks for a tree with tag, property or todo that
  220. matches `org-export-taskjuggler-project-tag' and takes this as
  221. the tasks for this project. The first node of this tree defines
  222. the project properties such as project name and project period.
  223. If there is a tree with tag, property or todo that matches
  224. `org-export-taskjuggler-resource-tag' this three is taken as
  225. resources for the project. If no resources are specified, a
  226. default resource is created and allocated to the project. Also
  227. the taskjuggler project will be created with default reports as
  228. defined in `org-export-taskjuggler-default-reports'."
  229. (interactive)
  230. (message "Exporting...")
  231. (setq-default org-done-keywords org-done-keywords)
  232. (let* ((tasks
  233. (org-taskjuggler-resolve-dependencies
  234. (org-taskjuggler-assign-task-ids
  235. (org-map-entries
  236. '(org-taskjuggler-components)
  237. org-export-taskjuggler-project-tag nil 'archive 'comment))))
  238. (resources
  239. (org-taskjuggler-assign-resource-ids
  240. (org-map-entries
  241. '(org-taskjuggler-components)
  242. org-export-taskjuggler-resource-tag nil 'archive 'comment)))
  243. (filename (expand-file-name
  244. (concat
  245. (file-name-sans-extension
  246. (file-name-nondirectory buffer-file-name))
  247. org-export-taskjuggler-extension)))
  248. (buffer (find-file-noselect filename))
  249. (old-level 0)
  250. task resource)
  251. (unless tasks
  252. (error "No tasks specified"))
  253. ;; add a default resource
  254. (unless resources
  255. (setq resources
  256. `((("ID" . ,(user-login-name))
  257. ("headline" . ,user-full-name)
  258. ("level" . 1)))))
  259. ;; add a default allocation to the first task if none was given
  260. (unless (assoc "allocate" (car tasks))
  261. (let ((task (car tasks))
  262. (resource-id (cdr (assoc "ID" (car resources)))))
  263. (setcar tasks (push (cons "allocate" resource-id) task))))
  264. ;; add a default start date to the first task if none was given
  265. (unless (assoc "start" (car tasks))
  266. (let ((task (car tasks))
  267. (time-string (format-time-string "%Y-%m-%d")))
  268. (setcar tasks (push (cons "start" time-string) task))))
  269. ;; add a default version if none was given
  270. (unless (assoc "version" (car tasks))
  271. (let ((task (car tasks))
  272. (version org-export-taskjuggler-default-project-version))
  273. (setcar tasks (push (cons "version" version) task))))
  274. (with-current-buffer buffer
  275. (erase-buffer)
  276. (org-taskjuggler-open-project (car tasks))
  277. (insert org-export-taskjuggler-default-global-properties)
  278. (insert "\n")
  279. (dolist (resource resources)
  280. (let ((level (cdr (assoc "level" resource))))
  281. (org-taskjuggler-close-maybe level)
  282. (org-taskjuggler-open-resource resource)
  283. (setq old-level level)))
  284. (org-taskjuggler-close-maybe 1)
  285. (setq old-level 0)
  286. (dolist (task tasks)
  287. (let ((level (cdr (assoc "level" task))))
  288. (org-taskjuggler-close-maybe level)
  289. (org-taskjuggler-open-task task)
  290. (setq old-level level)))
  291. (org-taskjuggler-close-maybe 1)
  292. (org-taskjuggler-insert-reports)
  293. (save-buffer)
  294. (or (org-export-push-to-kill-ring "TaskJuggler")
  295. (message "Exporting... done"))
  296. (current-buffer))))
  297. ;;;###autoload
  298. (defun org-export-as-taskjuggler-and-open ()
  299. "Export the current buffer as a TaskJuggler file and open it
  300. with the TaskJuggler GUI."
  301. (interactive)
  302. (let ((file-name (buffer-file-name (org-export-as-taskjuggler)))
  303. (command "TaskJugglerUI"))
  304. (start-process-shell-command command nil command file-name)))
  305. (defun org-taskjuggler-parent-is-ordered-p ()
  306. "Return true if the parent of the current node has a property
  307. \"ORDERED\". Return nil otherwise."
  308. (save-excursion
  309. (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
  310. (defun org-taskjuggler-components ()
  311. "Return an alist containing all the pertinent information for
  312. the current node such as the headline, the level, todo state
  313. information, all the properties, etc."
  314. (let* ((props (org-entry-properties))
  315. (components (org-heading-components))
  316. (level (nth 1 components))
  317. (headline (nth 4 components))
  318. (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
  319. (push (cons "level" level) props)
  320. (push (cons "headline" headline) props)
  321. (push (cons "parent-ordered" parent-ordered) props)))
  322. (defun org-taskjuggler-assign-task-ids (tasks)
  323. "Given a list of tasks return the same list assigning a unique id
  324. and the full path to each task. Taskjuggler takes hierarchical ids.
  325. For that reason we have to make ids locally unique and we have to keep
  326. a path to the current task."
  327. (let ((previous-level 0)
  328. unique-ids unique-id
  329. path
  330. task resolved-tasks tmp)
  331. (dolist (task tasks resolved-tasks)
  332. (let ((level (cdr (assoc "level" task))))
  333. (cond
  334. ((< previous-level level)
  335. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  336. (dotimes (tmp (- level previous-level))
  337. (push (list unique-id) unique-ids)
  338. (push unique-id path)))
  339. ((= previous-level level)
  340. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  341. (push unique-id (car unique-ids))
  342. (setcar path unique-id))
  343. ((> previous-level level)
  344. (dotimes (tmp (- previous-level level))
  345. (pop unique-ids)
  346. (pop path))
  347. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  348. (push unique-id (car unique-ids))
  349. (setcar path unique-id)))
  350. (push (cons "unique-id" unique-id) task)
  351. (push (cons "path" (mapconcat 'identity (reverse path) ".")) task)
  352. (setq previous-level level)
  353. (setq resolved-tasks (append resolved-tasks (list task)))))))
  354. (defun org-taskjuggler-assign-resource-ids (resources &optional unique-ids)
  355. "Given a list of resources return the same list, assigning a
  356. unique id to each resource."
  357. (cond
  358. ((null resources) nil)
  359. (t
  360. (let* ((resource (car resources))
  361. (unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
  362. (push (cons "unique-id" unique-id) resource)
  363. (cons resource
  364. (org-taskjuggler-assign-resource-ids (cdr resources)
  365. (cons unique-id unique-ids)))))))
  366. (defun org-taskjuggler-resolve-dependencies (tasks)
  367. (let ((previous-level 0)
  368. siblings
  369. task resolved-tasks)
  370. (dolist (task tasks resolved-tasks)
  371. (let* ((level (cdr (assoc "level" task)))
  372. (depends (cdr (assoc "depends" task)))
  373. (parent-ordered (cdr (assoc "parent-ordered" task)))
  374. (blocker (cdr (assoc "BLOCKER" task)))
  375. (blocked-on-previous
  376. (and blocker (string-match "previous-sibling" blocker)))
  377. (dependencies
  378. (org-taskjuggler-resolve-explicit-dependencies
  379. (append
  380. (and depends (org-taskjuggler-tokenize-dependencies depends))
  381. (and blocker (org-taskjuggler-tokenize-dependencies blocker)))
  382. tasks))
  383. previous-sibling)
  384. ; update previous sibling info
  385. (cond
  386. ((< previous-level level)
  387. (dotimes (tmp (- level previous-level))
  388. (push task siblings)))
  389. ((= previous-level level)
  390. (setq previous-sibling (car siblings))
  391. (setcar siblings task))
  392. ((> previous-level level)
  393. (dotimes (tmp (- previous-level level))
  394. (pop siblings))
  395. (setq previous-sibling (car siblings))
  396. (setcar siblings task)))
  397. ; insert a dependency on previous sibling if the parent is
  398. ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
  399. (when (or (and previous-sibling parent-ordered) blocked-on-previous)
  400. (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
  401. ; store dependency information
  402. (when dependencies
  403. (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
  404. (setq previous-level level)
  405. (setq resolved-tasks (append resolved-tasks (list task)))))))
  406. (defun org-taskjuggler-tokenize-dependencies (dependencies)
  407. "Split a dependency property value DEPENDENCIES into the
  408. individual dependencies and return them as a list while keeping
  409. the optional arguments (such as gapduration) for the
  410. dependencies. A dependency will have to match `[-a-zA-Z0-9_]+'."
  411. (cond
  412. ((string-match "^ *$" dependencies) nil)
  413. ((string-match "^[ \t]*\\([-a-zA-Z0-9_]+\\([ \t]*{[^}]+}\\)?\\)[ \t,]*" dependencies)
  414. (cons
  415. (substring dependencies (match-beginning 1) (match-end 1))
  416. (org-taskjuggler-tokenize-dependencies (substring dependencies (match-end 0)))))
  417. (t (error (format "invalid dependency id %s" dependencies)))))
  418. (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
  419. "For each dependency in DEPENDENCIES try to find a
  420. corresponding task with a matching ID in TASKS. Return a list
  421. containing the resolved links for all DEPENDENCIES where a
  422. matching tasks was found. If the dependency is
  423. \"previous-sibling\" it is ignored (as this is dealt with in
  424. `org-taskjuggler-resolve-dependencies'). If there is no matching
  425. task the dependency is silently ignored."
  426. (unless (null dependencies)
  427. (let*
  428. ;; the dependency might have optional attributes such as "{
  429. ;; gapduration 5d }", so only use the first string as id for the
  430. ;; dependency
  431. ((dependency (car dependencies))
  432. (id (car (split-string dependency)))
  433. (optional-attributes
  434. (mapconcat 'identity (cdr (split-string dependency)) " "))
  435. (path (org-taskjuggler-find-task-with-id id tasks)))
  436. (cond
  437. ;; ignore previous sibling dependencies
  438. ((equal (car dependencies) "previous-sibling")
  439. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
  440. ;; if the id is found in another task use its path
  441. ((not (null path))
  442. (cons (mapconcat 'identity (list path optional-attributes) " ")
  443. (org-taskjuggler-resolve-explicit-dependencies
  444. (cdr dependencies) tasks)))
  445. ;; silently ignore all other dependencies
  446. (t (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))))))
  447. (defun org-taskjuggler-find-task-with-id (id tasks)
  448. "Find ID in tasks. If found return the path of task. Otherwise
  449. return nil."
  450. (let ((task-id (cdr (assoc "ID" (car tasks))))
  451. (path (cdr (assoc "path" (car tasks)))))
  452. (cond
  453. ((null tasks) nil)
  454. ((equal task-id id) path)
  455. (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
  456. (defun org-taskjuggler-get-unique-id (item unique-ids)
  457. "Return a unique id for an ITEM which can be a task or a resource.
  458. The id is derived from the headline and made unique against
  459. UNIQUE-IDS. If the (downcased) first token of the headline is not
  460. unique try to add more (downcased) tokens of the headline or
  461. finally add more underscore characters (\"_\")."
  462. (let* ((headline (cdr (assoc "headline" item)))
  463. (parts (split-string headline))
  464. (id (org-taskjuggler-clean-id (downcase (pop parts)))))
  465. ; try to add more parts of the headline to make it unique
  466. (while (member id unique-ids)
  467. (setq id (concat id "_" (org-taskjuggler-clean-id (downcase (pop parts))))))
  468. ; if its still not unique add "_"
  469. (while (member id unique-ids)
  470. (setq id (concat id "_")))
  471. id))
  472. (defun org-taskjuggler-clean-id (id)
  473. "Clean and return ID to make it acceptable for taskjuggler."
  474. (and id (replace-regexp-in-string "[^a-zA-Z0-9_]" "_" id)))
  475. (defun org-taskjuggler-open-project (project)
  476. "Insert a the beginning of project declaration. All valid
  477. attributes from the PROJECT alist are inserted. If no end date is
  478. specified it is calculated
  479. `org-export-taskjuggler-default-project-duration' days from now."
  480. (let* ((unique-id (cdr (assoc "unique-id" project)))
  481. (headline (cdr (assoc "headline" project)))
  482. (version (cdr (assoc "version" project)))
  483. (start (cdr (assoc "start" project)))
  484. (end (cdr (assoc "end" project))))
  485. (insert
  486. (format "project %s \"%s\" \"%s\" %s +%sd {\n }\n"
  487. unique-id headline version start
  488. org-export-taskjuggler-default-project-duration))))
  489. (defun org-taskjuggler-filter-and-join (items)
  490. "Filter all nil elements from ITEMS and join the remaining ones
  491. with separator \"\n\"."
  492. (let ((filtered-items (remq nil items)))
  493. (and filtered-items (mapconcat 'identity filtered-items "\n"))))
  494. (defun org-taskjuggler-get-attributes (item attributes)
  495. "Return all attribute as a single formated string. ITEM is an
  496. alist representing either a resource or a task. ATTRIBUTES is a
  497. list of symbols. Only entries from ITEM are considered that are
  498. listed in ATTRIBUTES."
  499. (org-taskjuggler-filter-and-join
  500. (mapcar
  501. (lambda (attribute)
  502. (org-taskjuggler-filter-and-join
  503. (org-taskjuggler-get-attribute item attribute)))
  504. attributes)))
  505. (defun org-taskjuggler-get-attribute (item attribute)
  506. "Return a list of strings containing the properly formatted
  507. taskjuggler declaration for a given ATTRIBUTE in ITEM (an alist).
  508. If the ATTRIBUTE is not in ITEM return nil."
  509. (cond
  510. ((null item) nil)
  511. ((equal (symbol-name attribute) (car (car item)))
  512. (cons (format "%s %s" (symbol-name attribute) (cdr (car item)))
  513. (org-taskjuggler-get-attribute (cdr item) attribute)))
  514. (t (org-taskjuggler-get-attribute (cdr item) attribute))))
  515. (defun org-taskjuggler-open-resource (resource)
  516. (let ((id (org-taskjuggler-clean-id (cdr (assoc "ID" resource))))
  517. (unique-id (org-taskjuggler-clean-id (cdr (assoc "unique-id" resource))))
  518. (headline (cdr (assoc "headline" resource)))
  519. (attributes '(limits vacation shift booking efficiency journalentry rate)))
  520. (insert
  521. (concat
  522. "resource " (or id unique-id) " \"" headline "\" {\n "
  523. (org-taskjuggler-get-attributes resource attributes) "\n"))))
  524. (defun org-taskjuggler-clean-effort (effort)
  525. "Translate effort strings into a format acceptable to taskjuggler,
  526. i.e. REAL UNIT. If the effort string is something like 5:30 it
  527. will be assumed to be hours and will be translated into 5.5h.
  528. Otherwise if it contains something like 3.0 it is assumed to be
  529. days and will be translated into 3.0d. Other formats that
  530. taskjuggler supports (like weeks, months and years) are currently
  531. not supported."
  532. (cond
  533. ((null effort) effort)
  534. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" effort)
  535. (let ((hours (string-to-number (match-string 1 effort)))
  536. (minutes (string-to-number (match-string 2 effort))))
  537. (format "%dh" (+ hours (/ minutes 60.0)))))
  538. ((string-match "\\([0-9]+\\).\\([0-9]+\\)" effort) (concat effort "d"))
  539. (t (error "Not a valid effort (%s)" effort))))
  540. (defun org-taskjuggler-get-priority (priority)
  541. "Return a priority between 1 and 1000 based on PRIORITY, an
  542. org-mode priority string."
  543. (max 1 (/ (* 1000 (- org-lowest-priority (string-to-char priority)))
  544. (- org-lowest-priority org-highest-priority))))
  545. (defun org-taskjuggler-open-task (task)
  546. (let* ((unique-id (cdr (assoc "unique-id" task)))
  547. (headline (cdr (assoc "headline" task)))
  548. (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
  549. (depends (cdr (assoc "depends" task)))
  550. (allocate (cdr (assoc "allocate" task)))
  551. (priority-raw (cdr (assoc "PRIORITY" task)))
  552. (priority (and priority-raw (org-taskjuggler-get-priority priority-raw)))
  553. (state (cdr (assoc "TODO" task)))
  554. (complete (or (and (member state org-done-keywords) "100")
  555. (cdr (assoc "complete" task))))
  556. (parent-ordered (cdr (assoc "parent-ordered" task)))
  557. (previous-sibling (cdr (assoc "previous-sibling" task)))
  558. (attributes
  559. '(account start note duration endbuffer endcredit end
  560. flags journalentry length maxend maxstart milestone
  561. minend minstart period reference responsible
  562. scheduling startbuffer startcredit statusnote)))
  563. (insert
  564. (concat
  565. "task " unique-id " \"" headline "\" {\n"
  566. (if (and parent-ordered previous-sibling)
  567. (format " depends %s\n" previous-sibling)
  568. (and depends (format " depends %s\n" depends)))
  569. (and allocate (format " purge allocations\n allocate %s\n" allocate))
  570. (and complete (format " complete %s\n" complete))
  571. (and effort (format " effort %s\n" effort))
  572. (and priority (format " priority %s\n" priority))
  573. (org-taskjuggler-get-attributes task attributes)
  574. "\n"))))
  575. (defun org-taskjuggler-close-maybe (level)
  576. (while (> old-level level)
  577. (insert "}\n")
  578. (setq old-level (1- old-level)))
  579. (when (= old-level level)
  580. (insert "}\n")))
  581. (defun org-taskjuggler-insert-reports ()
  582. (let (report)
  583. (dolist (report org-export-taskjuggler-default-reports)
  584. (insert report "\n"))))
  585. (provide 'org-taskjuggler)
  586. ;;; org-taskjuggler.el ends here