Merge branch 'maint'
[org-mode.git] / lisp / ob-ditaa.el
blob609bac43d4c86c219d890e475d00b56c3b743e8b
1 ;;; ob-ditaa.el --- org-babel functions for ditaa evaluation
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating ditaa source code.
28 ;; This differs from most standard languages in that
30 ;; 1) there is no such thing as a "session" in ditaa
32 ;; 2) we are generally only going to return results of type "file"
34 ;; 3) we are adding the "file" and "cmdline" header arguments
36 ;; 4) there are no variables (at least for now)
38 ;;; Code:
39 (require 'ob)
41 (defvar org-babel-default-header-args:ditaa
42 '((:results . "file")
43 (:exports . "results")
44 (:java . "-Dfile.encoding=UTF-8"))
45 "Default arguments for evaluating a ditaa source block.")
47 (defcustom org-ditaa-jar-path (expand-file-name
48 "ditaa.jar"
49 (file-name-as-directory
50 (expand-file-name
51 "scripts"
52 (file-name-as-directory
53 (expand-file-name
54 "../contrib"
55 (file-name-directory (org-find-library-dir "org")))))))
56 "Path to the ditaa jar executable."
57 :group 'org-babel
58 :type 'string)
60 (defcustom org-ditaa-eps-jar-path
61 (expand-file-name "DitaaEps.jar" (file-name-directory org-ditaa-jar-path))
62 "Path to the DitaaEps.jar executable."
63 :group 'org-babel
64 :type 'string)
66 (defcustom org-ditaa-jar-option "-jar"
67 "Option for the ditaa jar file.
68 Do not leave leading or trailing spaces in this string."
69 :group 'org-babel
70 :version "24.1"
71 :type 'string)
73 (defun org-babel-execute:ditaa (body params)
74 "Execute a block of Ditaa code with org-babel.
75 This function is called by `org-babel-execute-src-block'."
76 (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
77 (out-file ((lambda (el)
78 (or el
79 (error
80 "ditaa code block requires :file header argument")))
81 (cdr (assoc :file params))))
82 (cmdline (cdr (assoc :cmdline params)))
83 (java (cdr (assoc :java params)))
84 (in-file (org-babel-temp-file "ditaa-"))
85 (eps (cdr (assoc :eps params)))
86 (cmd (concat "java " java " " org-ditaa-jar-option " "
87 (shell-quote-argument
88 (expand-file-name
89 (if eps org-ditaa-eps-jar-path org-ditaa-jar-path)))
90 " " cmdline
91 " " (org-babel-process-file-name in-file)
92 " " (org-babel-process-file-name out-file)))
93 (pdf-cmd (when (and (or (string= (file-name-extension out-file) "pdf")
94 (cdr (assoc :pdf params))))
95 (concat
96 "epstopdf"
97 " " (org-babel-process-file-name (concat in-file ".eps"))
98 " -o=" (org-babel-process-file-name out-file)))))
99 (unless (file-exists-p org-ditaa-jar-path)
100 (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
101 (with-temp-file in-file (insert body))
102 (message cmd) (shell-command cmd)
103 (when pdf-cmd (message pdf-cmd) (shell-command pdf-cmd))
104 nil)) ;; signal that output has already been written to file
106 (defun org-babel-prep-session:ditaa (session params)
107 "Return an error because ditaa does not support sessions."
108 (error "Ditaa does not support sessions"))
110 (provide 'ob-ditaa)
114 ;;; ob-ditaa.el ends here