org-taskjuggler.el 26 KB

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