org-taskjuggler.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. (defcustom org-export-taskjuggler-default-global-properties
  92. "shift s40 \"Part time shift\" {
  93. workinghours wed, thu, fri off
  94. }
  95. "
  96. "Default global properties for the project. Here you typically
  97. define global properties such as shifts, accounts, rates,
  98. vacation, macros and flags. Any property that is allowed within
  99. the TaskJuggler file can be inserted. You could for example
  100. include another TaskJuggler file.
  101. The global properties are inserted after the project declaration
  102. but before any resource and task declarations."
  103. :group 'org-export-taskjuggler
  104. :type '(string :tag "Preamble"))
  105. ;;; Hooks
  106. (defvar org-export-taskjuggler-final-hook nil
  107. "Hook run at the end of TaskJuggler export, in the new buffer.")
  108. ;;; Autoload functions:
  109. ;;;###autoload
  110. (defun org-export-as-taskjuggler ()
  111. "Export parts of the current buffer as a TaskJuggler file.
  112. The exporter looks for a tree with tag, property or todo that
  113. matches `org-export-taskjuggler-project-tag' and takes this as
  114. the tasks for this project. The first node of this tree defines
  115. the project properties such as project name and project period.
  116. If there is a tree with tag, property or todo that matches
  117. `org-export-taskjuggler-resource-tag' this three is taken as
  118. resources for the project. If no resources are specified, a
  119. default resource is created and allocated to the project. Also
  120. the taskjuggler project will be created with default reports as
  121. defined in `org-export-taskjuggler-default-reports'."
  122. (interactive)
  123. (message "Exporting...")
  124. (setq-default org-done-keywords org-done-keywords)
  125. (let* ((tasks
  126. (org-taskjuggler-resolve-dependencies
  127. (org-taskjuggler-assign-task-ids
  128. (org-map-entries '(org-taskjuggler-components)
  129. org-export-taskjuggler-project-tag nil 'archive 'comment))))
  130. (resources
  131. (org-taskjuggler-assign-resource-ids
  132. (org-map-entries '(org-taskjuggler-components)
  133. org-export-taskjuggler-resource-tag nil 'archive 'comment)))
  134. (filename (expand-file-name
  135. (concat
  136. (file-name-sans-extension
  137. (file-name-nondirectory buffer-file-name))
  138. org-export-taskjuggler-extension)))
  139. (buffer (find-file-noselect filename))
  140. (old-level 0)
  141. task resource)
  142. ;; add a default resource
  143. (unless resources
  144. (setq resources
  145. `((("ID" . ,(user-login-name))
  146. ("headline" . ,user-full-name)
  147. ("level" . 1)))))
  148. ;; add a default allocation to the first task if none was given
  149. (unless (assoc "allocate" (car tasks))
  150. (let ((task (car tasks))
  151. (resource-id (cdr (assoc "ID" (car resources)))))
  152. (setcar tasks (push (cons "allocate" resource-id) task))))
  153. ;; add a default start date to the first task if none was given
  154. (unless (assoc "start" (car tasks))
  155. (let ((task (car tasks))
  156. (time-string (format-time-string "%Y-%m-%d")))
  157. (setcar tasks (push (cons "start" time-string) task))))
  158. ;; add a default version if none was given
  159. (unless (assoc "version" (car tasks))
  160. (let ((task (car tasks))
  161. (version org-export-taskjuggler-default-project-version))
  162. (setcar tasks (push (cons "version" version) task))))
  163. (with-current-buffer buffer
  164. (erase-buffer)
  165. (org-taskjuggler-open-project (car tasks))
  166. (insert org-export-taskjuggler-default-global-properties)
  167. (insert "\n")
  168. (dolist (resource resources)
  169. (let ((level (cdr (assoc "level" resource))))
  170. (org-taskjuggler-close-maybe level)
  171. (org-taskjuggler-open-resource resource)
  172. (setq old-level level)))
  173. (org-taskjuggler-close-maybe 1)
  174. (setq old-level 0)
  175. (dolist (task tasks)
  176. (let ((level (cdr (assoc "level" task))))
  177. (org-taskjuggler-close-maybe level)
  178. (org-taskjuggler-open-task task)
  179. (setq old-level level)))
  180. (org-taskjuggler-close-maybe 1)
  181. (org-taskjuggler-insert-reports)
  182. (save-buffer)
  183. (or (org-export-push-to-kill-ring "TaskJuggler")
  184. (message "Exporting... done"))
  185. (current-buffer))))
  186. ;;;###autoload
  187. (defun org-export-as-taskjuggler-and-open ()
  188. "Export the current buffer as a TaskJuggler file and open it with the TaskJuggler GUI."
  189. (interactive)
  190. (let ((file-name (buffer-file-name (org-export-as-taskjuggler)))
  191. (command "TaskJugglerUI"))
  192. (start-process-shell-command command nil command file-name)))
  193. (defun org-taskjuggler-parent-is-ordered-p ()
  194. "Return true if the parent of the current node has a property
  195. \"ORDERED\". Return nil otherwise."
  196. (save-excursion
  197. (and (org-up-heading-safe) (org-entry-get (point) "ORDERED"))))
  198. (defun org-taskjuggler-components ()
  199. "Return an alist containing all the pertinent information for
  200. the current node such as the headline, the level, todo state
  201. information, all the properties, etc."
  202. (let* ((props (org-entry-properties))
  203. (components (org-heading-components))
  204. (level (car components))
  205. (headline (nth 4 components))
  206. (parent-ordered (org-taskjuggler-parent-is-ordered-p)))
  207. (push (cons "level" level) props)
  208. (push (cons "headline" headline) props)
  209. (push (cons "parent-ordered" parent-ordered) props)))
  210. (defun org-taskjuggler-assign-task-ids (tasks)
  211. "Given a list of tasks return the same list assigning a unique id
  212. and the full path to each task. Taskjuggler takes hierarchical ids.
  213. For that reason we have to make ids locally unique and we have to keep
  214. a path to the current task."
  215. (let ((previous-level 0)
  216. unique-ids unique-id
  217. path
  218. task resolved-tasks tmp)
  219. (dolist (task tasks resolved-tasks)
  220. (let ((level (cdr (assoc "level" task))))
  221. (cond
  222. ((< previous-level level)
  223. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  224. (dotimes (tmp (- level previous-level))
  225. (push (list unique-id) unique-ids)
  226. (push unique-id path)))
  227. ((= previous-level level)
  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. ((> previous-level level)
  232. (dotimes (tmp (- previous-level level))
  233. (pop unique-ids)
  234. (pop path))
  235. (setq unique-id (org-taskjuggler-get-unique-id task (car unique-ids)))
  236. (push unique-id (car unique-ids))
  237. (setcar path unique-id)))
  238. (push (cons "unique-id" unique-id) task)
  239. (push (cons "path" (mapconcat 'identity (reverse path) ".")) task)
  240. (setq previous-level level)
  241. (setq resolved-tasks (append resolved-tasks (list task)))))))
  242. (defun org-taskjuggler-assign-resource-ids (resources &optional unique-ids)
  243. "Given a list of resources return the same list, assigning a
  244. unique id to each resource."
  245. (cond
  246. ((null resources) nil)
  247. (t
  248. (let* ((resource (car resources))
  249. (unique-id (org-taskjuggler-get-unique-id resource unique-ids)))
  250. (push (cons "unique-id" unique-id) resource)
  251. (cons resource (org-taskjuggler-assign-resource-ids (cdr resources)
  252. (cons unique-id unique-ids)))))))
  253. (defun org-taskjuggler-resolve-dependencies (tasks)
  254. (let ((previous-level 0)
  255. siblings
  256. task resolved-tasks)
  257. (dolist (task tasks resolved-tasks)
  258. (let* ((level (cdr (assoc "level" task)))
  259. (depends (cdr (assoc "depends" task)))
  260. (parent-ordered (cdr (assoc "parent-ordered" task)))
  261. (blocker (cdr (assoc "BLOCKER" task)))
  262. (blocked-on-previous (and blocker (string-match "previous-sibling" blocker)))
  263. (dependencies
  264. (org-taskjuggler-resolve-explicit-dependencies
  265. (append
  266. (and depends (split-string depends "[, \f\t\n\r\v]+" t))
  267. (and blocker (split-string blocker "[, \f\t\n\r\v]+" t))) tasks))
  268. previous-sibling)
  269. ; update previous sibling info
  270. (cond
  271. ((< previous-level level)
  272. (dotimes (tmp (- level previous-level))
  273. (push task siblings)))
  274. ((= previous-level level)
  275. (setq previous-sibling (car siblings))
  276. (setcar siblings task))
  277. ((> previous-level level)
  278. (dotimes (tmp (- previous-level level))
  279. (pop siblings))
  280. (setq previous-sibling (car siblings))
  281. (setcar siblings task)))
  282. ; insert a dependency on previous sibling if the parent is
  283. ; ordered or if the tasks has a BLOCKER attribute with value "previous-sibling"
  284. (when (or (and previous-sibling parent-ordered) blocked-on-previous)
  285. (push (format "!%s" (cdr (assoc "unique-id" previous-sibling))) dependencies))
  286. ; store dependency information
  287. (when dependencies
  288. (push (cons "depends" (mapconcat 'identity dependencies ", ")) task))
  289. (setq previous-level level)
  290. (setq resolved-tasks (append resolved-tasks (list task)))))))
  291. (defun org-taskjuggler-resolve-explicit-dependencies (dependencies tasks)
  292. (let (path)
  293. (cond
  294. ((null dependencies) nil)
  295. ; ignore previous sibling dependencies
  296. ((equal (car dependencies) "previous-sibling")
  297. (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks))
  298. ; if the id is found in another task use its path
  299. ((setq path (org-taskjuggler-find-task-with-id (car dependencies) tasks))
  300. (cons path (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks)))
  301. ; silently ignore all other dependencies
  302. (t (org-taskjuggler-resolve-explicit-dependencies (cdr dependencies) tasks)))))
  303. (defun org-taskjuggler-find-task-with-id (id tasks)
  304. "Find ID in tasks. If found return the path of task. Otherwise return nil."
  305. (let ((task-id (cdr (assoc "ID" (car tasks))))
  306. (path (cdr (assoc "path" (car tasks)))))
  307. (cond
  308. ((null tasks) nil)
  309. ((equal task-id id) path)
  310. (t (org-taskjuggler-find-task-with-id id (cdr tasks))))))
  311. (defun org-taskjuggler-get-unique-id (item unique-ids)
  312. "Return a unique id for an ITEM which can be a task or a resource.
  313. The id is derived from the headline and made unique against
  314. UNIQUE-IDS. If the first part of the headline is not unique try to add
  315. more parts of the headline or finally add more underscore characters (\"_\")."
  316. (let* ((headline (cdr (assoc "headline" item)))
  317. (parts (split-string headline))
  318. (id (org-taskjuggler-clean-id (downcase (pop parts)))))
  319. ; try to add more parts of the headline to make it unique
  320. (while (member id unique-ids)
  321. (setq id (concat id "_" (org-taskjuggler-clean-id (downcase (pop parts))))))
  322. ; if its still not unique add "_"
  323. (while (member id unique-ids)
  324. (setq id (concat id "_")))
  325. id))
  326. (defun org-taskjuggler-clean-id (id)
  327. "Clean and return ID to make it acceptable for taskjuggler."
  328. (and id (replace-regexp-in-string "[^a-zA-Z0-9_]" "_" id)))
  329. (defun org-taskjuggler-open-project (project)
  330. "Insert a the beginning of project declaration. All valid
  331. attributes from the PROJECT alist are inserted. If no end date is
  332. specified it is calculated
  333. `org-export-taskjuggler-default-project-duration' days from now."
  334. (let* ((unique-id (cdr (assoc "unique-id" project)))
  335. (headline (cdr (assoc "headline" project)))
  336. (version (cdr (assoc "version" project)))
  337. (start (cdr (assoc "start" project)))
  338. (end (cdr (assoc "end" project))))
  339. (insert
  340. (format "project %s \"%s\" \"%s\" %s +%sd {\n }\n"
  341. unique-id headline version start
  342. org-export-taskjuggler-default-project-duration))))
  343. (defun org-taskjuggler-filter-and-join (items)
  344. "Filter all nil elements from ITEMS and join the remaining ones
  345. with separator \"\n\"."
  346. (let ((filtered-items (remq nil items)))
  347. (and filtered-items (mapconcat 'identity filtered-items "\n"))))
  348. (defun org-taskjuggler-get-attributes (item attributes)
  349. "Return all attribute as a single formated string. ITEM is an alist
  350. representing either a resource or a task. ATTRIBUTES is a list of
  351. symbols. Only entries from ITEM are considered that are listed in
  352. ATTRIBUTES."
  353. (org-taskjuggler-filter-and-join
  354. (mapcar
  355. (lambda (attribute)
  356. (org-taskjuggler-filter-and-join
  357. (org-taskjuggler-get-attribute item attribute)))
  358. attributes)))
  359. (defun org-taskjuggler-get-attribute (item attribute)
  360. "Return a list of strings containing the properly formatted
  361. taskjuggler declaration for a given ATTRIBUTE in ITEM (an alist).
  362. If the ATTRIBUTE is not in ITEM return nil."
  363. (cond
  364. ((null item) nil)
  365. ((equal (symbol-name attribute) (car (car item)))
  366. (cons (or
  367. (and (equal attribute 'limits)
  368. (format "%s { %s }" (symbol-name attribute) (cdr (car item))))
  369. (format "%s %s" (symbol-name attribute) (cdr (car item))))
  370. (org-taskjuggler-get-attribute (cdr item) attribute)))
  371. (t (org-taskjuggler-get-attribute (cdr item) attribute))))
  372. (defun org-taskjuggler-open-resource (resource)
  373. (let ((id (org-taskjuggler-clean-id (cdr (assoc "ID" resource))))
  374. (unique-id (org-taskjuggler-clean-id (cdr (assoc "unique-id" resource))))
  375. (headline (cdr (assoc "headline" resource)))
  376. (attributes '(limits vacation shift booking efficiency journalentry rate)))
  377. (insert
  378. (concat
  379. "resource " (or id unique-id) " \"" headline "\" {\n "
  380. (org-taskjuggler-get-attributes resource attributes) "\n"))))
  381. (defun org-taskjuggler-clean-effort (effort)
  382. "Translate effort strings into a format acceptable to taskjuggler,
  383. i.e. REAL UNIT. If the effort string is something like 5:00 it
  384. will be assumed to be hours and will be translated into 5.0h.
  385. Otherwise if it contains something like 3.0 it is assumed to be
  386. days and will be translated into 3.0d. Other formats that taskjuggler
  387. supports (like weeks, months and years) are currently not supported."
  388. (cond
  389. ((null effort) effort)
  390. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" effort)
  391. ; FIXME: translate the minutes to a decimal
  392. (concat (match-string 1 effort) "." (match-string 2 effort) "h"))
  393. ((string-match "\\([0-9]+\\).\\([0-9]+\\)" effort) (concat effort "d"))
  394. (t (error "Not a valid effort (%s)" effort))))
  395. (defun org-taskjuggler-get-priority (priority)
  396. "Return a priority between 1 and 1000 based on PRIORITY, an
  397. org-mode priority string."
  398. (max 1 (/ (* 1000 (- org-lowest-priority (string-to-char priority)))
  399. (- org-lowest-priority org-highest-priority))))
  400. (defun org-taskjuggler-open-task (task)
  401. (let* ((unique-id (cdr (assoc "unique-id" task)))
  402. (headline (cdr (assoc "headline" task)))
  403. (effort (org-taskjuggler-clean-effort (cdr (assoc org-effort-property task))))
  404. (depends (cdr (assoc "depends" task)))
  405. (allocate (cdr (assoc "allocate" task)))
  406. (priority-raw (cdr (assoc "PRIORITY" task)))
  407. (priority (and priority-raw (org-taskjuggler-get-priority priority-raw)))
  408. (state (cdr (assoc "TODO" task)))
  409. (complete (or (and (member state org-done-keywords) "100")
  410. (cdr (assoc "complete" task))))
  411. (parent-ordered (cdr (assoc "parent-ordered" task)))
  412. (previous-sibling (cdr (assoc "previous-sibling" task)))
  413. (attributes
  414. '(account start note duration endbuffer endcredit end
  415. flags journalentry, length maxend maxstart milestone
  416. minend minstart period reference responsible
  417. scheduling startbuffer startcredit statusnote)))
  418. (insert
  419. (concat
  420. "task " unique-id " \"" headline "\" {\n"
  421. (if (and parent-ordered previous-sibling)
  422. (format " depends %s\n" previous-sibling)
  423. (and depends (format " depends %s\n" depends)))
  424. (and allocate (format " purge allocations\n allocate %s\n" allocate))
  425. (and complete (format " complete %s\n" complete))
  426. (and effort (format " effort %s\n" effort))
  427. (and priority (format " priority %s\n" priority))
  428. (org-taskjuggler-get-attributes task attributes)
  429. "\n"))))
  430. (defun org-taskjuggler-close-maybe (level)
  431. (while (> old-level level)
  432. (insert "}\n")
  433. (setq old-level (1- old-level)))
  434. (when (= old-level level)
  435. (insert "}\n")))
  436. (defun org-taskjuggler-insert-reports ()
  437. (let (report)
  438. (dolist (report org-export-taskjuggler-default-reports)
  439. (insert report "\n"))))
  440. (provide 'org-taskjuggler)
  441. ;;; org-taskjuggler.el ends here