org-taskjuggler.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. ;;
  28. ;; The interactive functions are similar to those of the HTML and LaTeX
  29. ;; exporters:
  30. ;;
  31. ;; M-x `org-export-as-taskjuggler'
  32. ;; M-x `org-export-as-taskjuggler-and-open'
  33. ;;
  34. ;;; TODO:
  35. ;; * Code cleanup
  36. ;; * Add documentation
  37. ;; * Try using plists instead of alists
  38. ;;
  39. ;;; Code:
  40. (eval-when-compile
  41. (require 'cl))
  42. (require 'org)
  43. (require 'org-exp)
  44. ;;; User variables:
  45. (defgroup org-export-taskjuggler nil
  46. "Options for exporting Org-mode files to TaskJuggler."
  47. :tag "Org Export TaskJuggler"
  48. :group 'org-export)
  49. (defcustom org-export-taskjuggler-extension ".tjp"
  50. "Extension of TaskJuggler files."
  51. :group 'org-export-taskjuggler
  52. :type 'string)
  53. (defcustom org-export-taskjuggler-project-tag "project"
  54. "Tag, property or todo used to find the tree containing all
  55. the tasks for the project."
  56. :group 'org-export-taskjuggler
  57. :type 'string)
  58. (defcustom org-export-taskjuggler-resource-tag "resource"
  59. "Tag, property or todo used to find the tree containing all the
  60. resources for the project."
  61. :group 'org-export-taskjuggler
  62. :type 'string)
  63. (defcustom org-export-taskjuggler-default-project-version "1.0"
  64. "Default version string for the project."
  65. :group 'org-export-taskjuggler
  66. :type 'string)
  67. (defcustom org-export-taskjuggler-default-project-duration 180
  68. "Default project duration if no start and end date have been defined
  69. in the root node of the task tree, i.e. the tree that has been marked
  70. with `org-export-taskjuggler-project-tag'"
  71. :group 'org-export-taskjuggler
  72. :type 'integer)
  73. (defcustom org-export-taskjuggler-default-reports
  74. '("taskreport \"Gantt Chart\" {
  75. headline \"Project Gantt Chart\"
  76. columns hierarchindex, name, start, end, effort, duration, completed, chart
  77. timeformat \"%Y-%m-%d\"
  78. hideresource 1
  79. loadunit days
  80. }"
  81. "resourcereport \"Resource Graph\" {
  82. headline \"Resource Allocation Graph\"
  83. columns no, name, utilization, freeload, chart
  84. loadunit days
  85. sorttasks startup
  86. hidetask ~isleaf()
  87. }")
  88. "Default reports for the project."
  89. :group 'org-export-taskjuggler
  90. :type '(repeat (string :tag "Report")))
  91. ;;; Hooks
  92. (defvar org-export-taskjuggler-final-hook nil
  93. "Hook run at the end of TaskJuggler export, in the new buffer.")
  94. ;;; Autoload functions:
  95. ;;;###autoload
  96. (defun org-export-as-taskjuggler ()
  97. "Export parts of the current buffer as a TaskJuggler file.
  98. The exporter looks for a tree with tag, property or todo that
  99. matches `org-export-taskjuggler-project-tag' and takes this as
  100. the tasks for this project. The first node of this tree defines
  101. the project properties such as project name and project period.
  102. If there is a tree with tag, property or todo that matches
  103. `org-export-taskjuggler-resource-tag' this three is taken as
  104. resources for the project. If no resources are specified, a
  105. default resource is created and allocated to the project. Also
  106. the taskjuggler project will be created with default reports as
  107. defined in `org-export-taskjuggler-default-reports'."
  108. (interactive)
  109. (message "Exporting...")
  110. (setq-default org-done-keywords org-done-keywords)
  111. (let* ((tasks
  112. (org-taskjuggler-resolve-dependencies
  113. (org-taskjuggler-assign-task-ids
  114. (org-map-entries '(org-taskjuggler-components)
  115. org-export-taskjuggler-project-tag nil 'archive 'comment))))
  116. (resources
  117. (org-taskjuggler-assign-resource-ids
  118. (org-map-entries '(org-taskjuggler-components)
  119. org-export-taskjuggler-resource-tag nil 'archive 'comment)))
  120. (filename (expand-file-name
  121. (concat
  122. (file-name-sans-extension
  123. (file-name-nondirectory buffer-file-name))
  124. org-export-taskjuggler-extension)))
  125. (buffer (find-file-noselect filename))
  126. (old-level 0)
  127. task resource)
  128. ;; add a default resource
  129. (unless resources
  130. (setq resources
  131. `((("ID" . ,(user-login-name))
  132. ("headline" . ,user-full-name)
  133. ("level" . 1)))))
  134. ;; add a default allocation to the first task if none was given
  135. (unless (assoc "allocate" (car tasks))
  136. (let ((task (car tasks))
  137. (resource-id (cdr (assoc "ID" (car resources)))))
  138. (setcar tasks (push (cons "allocate" resource-id) task))))
  139. ;; add a default start date to the first task if none was given
  140. (unless (assoc "start" (car tasks))
  141. (let ((task (car tasks))
  142. (time-string (format-time-string "%Y-%m-%d")))
  143. (setcar tasks (push (cons "start" time-string) task))))
  144. ;; add a default end date to the first task if none was given
  145. (unless (assoc "end" (car tasks))
  146. (let* ((task (car tasks))
  147. (now (current-time))
  148. (duration
  149. (days-to-time org-export-taskjuggler-default-project-duration))
  150. (time-string
  151. (format-time-string "%Y-%m-%d" (time-add now duration))))
  152. (setcar tasks (push (cons "end" time-string) task))))
  153. ;; add a default version if none was given
  154. (unless (assoc "version" (car tasks))
  155. (let ((task (car tasks))
  156. (version org-export-taskjuggler-default-project-version))
  157. (setcar tasks (push (cons "version" version) task))))
  158. (with-current-buffer buffer
  159. (erase-buffer)
  160. (org-taskjuggler-open-project (car tasks))
  161. (dolist (resource resources)
  162. (let ((level (cdr (assoc "level" resource))))
  163. (org-taskjuggler-close-maybe level)
  164. (org-taskjuggler-open-resource resource)
  165. (setq old-level level)))
  166. (org-taskjuggler-close-maybe 1)
  167. (setq old-level 0)
  168. (dolist (task tasks)
  169. (let ((level (cdr (assoc "level" task))))
  170. (org-taskjuggler-close-maybe level)
  171. (org-taskjuggler-open-task task)
  172. (setq old-level level)))
  173. (org-taskjuggler-close-maybe 1)
  174. (org-taskjuggler-insert-reports)
  175. (save-buffer)
  176. (or (org-export-push-to-kill-ring "TaskJuggler")
  177. (message "Exporting... done"))
  178. (current-buffer))))
  179. ;;;###autoload
  180. (defun org-export-as-taskjuggler-and-open ()
  181. "Export the current buffer as a TaskJuggler file and open it with the TaskJuggler GUI."
  182. (interactive)
  183. (let ((file-name (buffer-file-name (org-export-as-taskjuggler)))
  184. (command "TaskJugglerUI"))
  185. (start-process-shell-command command nil command file-name)))
  186. (defun org-taskjuggler-parent-is-ordered-p ()
  187. "Return true if the parent of the current node has a property
  188. \"ORDERED\". Return nil otherwise."
  189. (save-excursion
  190. (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
  191. (defun org-taskjuggler-components ()
  192. "Return an alist containing all the pertinent information for
  193. the current node such as the headline, the level, todo state
  194. information, all the properties, etc."
  195. (let* ((props (org-entry-properties))
  196. (components (org-heading-components))
  197. (level (car components))
  198. (headline (nth 4 components))
  199. (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
  200. (push (cons "level" level) props)
  201. (push (cons "headline" headline) props)
  202. (push (cons "parent-ordered" parent-ordered) props)))
  203. (defun org-taskjuggler-assign-task-ids (tasks)
  204. "Given a list of tasks return the same list assigning a unique id
  205. and the full path to each task. Taskjuggler takes hierarchical ids.
  206. For that reason we have to make ids locally unique and we have to keep
  207. a path to the current task."
  208. (let ((previous-level 0)
  209. unique-ids
  210. path
  211. task resolved-tasks tmp)
  212. (dolist (task tasks resolved-tasks)
  213. (let ((level (cdr (assoc "level" task))))
  214. (cond
  215. ((< previous-level level)
  216. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  217. (dotimes (tmp (- level previous-level))
  218. (push (list unique-id) unique-ids)
  219. (push unique-id path)))
  220. ((= previous-level level)
  221. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  222. (push unique-id (car unique-ids))
  223. (setcar path unique-id))
  224. ((> previous-level level)
  225. (dotimes (tmp (- previous-level level))
  226. (pop unique-ids)
  227. (pop path))
  228. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  229. (push unique-id (car unique-ids))
  230. (setcar path unique-id)))
  231. (push (cons "unique-id" unique-id) task)
  232. (push (cons "path" (mapconcat 'identity (reverse path) ".")) task)
  233. (setq previous-level level)
  234. (setq resolved-tasks (append resolved-tasks (list task)))))))
  235. (defun org-taskjuggler-assign-resource-ids (resources &optional unique-ids)
  236. "Given a list of resources return the same list, assigning a
  237. unique id to each resource."
  238. (cond
  239. ((null resources) nil)
  240. (t
  241. (let* ((resource (car resources))
  242. (unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
  243. (push (cons "unique-id" unique-id) resource)
  244. (cons resource (org-taskjuggler-assign-resource-ids (cdr resources)
  245. (cons unique-id unique-ids)))))))
  246. (defun org-taskjuggler-resolve-dependencies (tasks)
  247. (let ((previous-level 0)
  248. siblings
  249. task resolved-tasks)
  250. (dolist (task tasks resolved-tasks)
  251. (let* ((level (cdr (assoc "level" task)))
  252. (depends (cdr (assoc "depends" task)))
  253. (parent-ordered (cdr (assoc "parent-ordered" task)))
  254. (blocker (cdr (assoc "BLOCKER" task)))
  255. (blocked-on-previous (and blocker (string-match "previous-sibling" blocker)))
  256. (dependencies
  257. (org-taskjuggler-resolve-explicit-dependencies
  258. (append
  259. (and depends (split-string depends "[, \f\t\n\r\v]+" t))
  260. (and blocker (split-string blocker "[, \f\t\n\r\v]+" t))) tasks))
  261. previous-sibling)
  262. ; update previous sibling info
  263. (cond
  264. ((< previous-level level)
  265. (dotimes (tmp (- level previous-level))
  266. (push task siblings)))
  267. ((= previous-level level)
  268. (setq previous-sibling (car siblings))
  269. (setcar siblings task))
  270. ((> previous-level level)
  271. (dotimes (tmp (- previous-level level))
  272. (pop siblings))
  273. (setq previous-sibling (car siblings))
  274. (setcar siblings task)))
  275. ; insert a dependency on previous sibling if the parent is
  276. ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
  277. (when (or (and previous-sibling parent-ordered) blocked-on-previous)
  278. (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
  279. ; store dependency information
  280. (when dependencies
  281. (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
  282. (setq previous-level level)
  283. (setq resolved-tasks (append resolved-tasks (list task)))))))
  284. (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
  285. (let (path)
  286. (cond
  287. ((null dependencies) nil)
  288. ; ignore previous sibling dependencies
  289. ((equal (car dependencies) "previous-sibling")
  290. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
  291. ; if the id is found in another task use its path
  292. ((setq path (org-taskjuggler-find-task-with-id (car dependencies) tasks))
  293. (cons path (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks)))
  294. ; silently ignore all other dependencies
  295. (t (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks)))))
  296. (defun org-taskjuggler-find-task-with-id (id tasks)
  297. "Find ID in tasks. If found return the path of task. Otherwise return nil."
  298. (let ((task-id (cdr (assoc "ID" (car tasks))))
  299. (path (cdr (assoc "path" (car tasks)))))
  300. (cond
  301. ((null tasks) nil)
  302. ((equal task-id id) path)
  303. (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
  304. (defun org-taskjuggler-get-unique-id (item unique-ids)
  305. "Return a unique id for an ITEM which can be a task or a resource.
  306. The id is derived from the headline and made unique against
  307. UNIQUE-IDS. If the first part of the headline is not unique try to add
  308. more parts of the headline or finally add more underscore characters (\"_\")."
  309. (let* ((headline (cdr (assoc "headline" item)))
  310. (parts (split-string headline))
  311. (id (downcase (pop parts))))
  312. ; try to add more parts of the headline to make it unique
  313. (while (member id unique-ids)
  314. (setq id (concat id "_" (downcase (pop parts)))))
  315. ; if its still not unique add "_"
  316. (while (member id unique-ids)
  317. (setq id (concat id "_")))
  318. (org-taskjuggler-clean-id id)))
  319. (defun org-taskjuggler-clean-id (id)
  320. "Clean and return ID to make it acceptable for taskjuggler."
  321. (and id (replace-regexp-in-string "[^a-zA-Z0-9_]" "_" id)))
  322. (defun org-taskjuggler-open-project (project)
  323. (let ((unique-id (cdr (assoc "unique-id" project)))
  324. (headline (cdr (assoc "headline" project)))
  325. (version (cdr (assoc "version" project)))
  326. (start (cdr (assoc "start" project)))
  327. (end (cdr (assoc "end" project))))
  328. (insert
  329. (concat
  330. "project " unique-id
  331. " \"" headline "\" \"" version "\" " start " - " end " {\n }\n"))))
  332. (defun org-taskjuggler-open-resource (resource)
  333. (let ((id (org-taskjuggler-clean-id (cdr (assoc "ID" resource))))
  334. (unique-id (org-taskjuggler-clean-id (cdr (assoc "unique-id" resource))))
  335. (headline (cdr (assoc "headline" resource)))
  336. (limits (cdr (assoc "limits" resource)))
  337. (vacation (cdr (assoc "vacation" resource))))
  338. (insert
  339. (concat "resource " (or id unique-id) " \"" headline "\" {\n "
  340. (and limits (concat "\n limits { " limits " }\n"))
  341. (and vacation (concat "\n vacation " vacation "\n"))))))
  342. (defun org-taskjuggler-clean-effort (effort)
  343. "Translate effort strings into a format acceptable to taskjuggler,
  344. i.e. REAL UNIT. If the effort string is something like 5:00 it
  345. will be assumed to be hours and will be translated into 5.0h.
  346. Otherwise if it contains something like 3.0 it is assumed to be
  347. days and will be translated into 3.0d. Other formats that taskjuggler
  348. supports (like weeks, months and years) are currently not supported."
  349. (cond
  350. ((null effort) effort)
  351. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" effort)
  352. ; FIXME: translate the minutes to a decimal
  353. (concat (match-string 1 effort) "." (match-string 2 effort) "h"))
  354. ((string-match "\\([0-9]+\\).\\([0-9]+\\)" effort) (concat effort "d"))
  355. (t (error "Not a valid effort (%s)" effort))))
  356. (defun org-taskjuggler-open-task (task)
  357. (let* ((unique-id (cdr (assoc "unique-id" task)))
  358. (headline (cdr (assoc "headline" task)))
  359. (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
  360. (depends (cdr (assoc "depends" task)))
  361. (allocate (cdr (assoc "allocate" task)))
  362. (account (cdr (assoc "account" task)))
  363. (start (cdr (assoc "start" task)))
  364. (state (cdr (assoc "TODO" task)))
  365. (complete (or (and (member state org-done-keywords) "100")
  366. (cdr (assoc "complete" task))))
  367. (note (cdr (assoc "note" task)))
  368. (priority (cdr (assoc "priority" task)))
  369. (parent-ordered (cdr (assoc "parent-ordered" task)))
  370. (previous-sibling (cdr (assoc "previous-sibling" task))))
  371. (insert
  372. (concat
  373. "task " unique-id " \"" headline "\" {"
  374. (and effort (concat "\n effort " effort))
  375. (if (and parent-ordered previous-sibling)
  376. (concat "\n depends " previous-sibling)
  377. (and depends (concat "\n depends " depends)))
  378. (and allocate (concat "\n purge allocations\n allocate " allocate))
  379. (and account (concat "\n account " account))
  380. (and start (concat "\n start " start))
  381. (and complete (concat "\n complete " complete))
  382. (and note (concat "\n note " note))
  383. (and priority (concat "\n priority " priority))
  384. "\n"))))
  385. (defun org-taskjuggler-close-maybe (level)
  386. (while (> old-level level)
  387. (insert "}\n")
  388. (setq old-level (1- old-level)))
  389. (when (= old-level level)
  390. (insert "}\n")))
  391. (defun org-taskjuggler-insert-reports ()
  392. (let (report)
  393. (dolist (report org-export-taskjuggler-default-reports)
  394. (insert report "\n"))))
  395. (provide 'org-taskjuggler)
  396. ;;; org-taskjuggler.el ends here