Merge branch 'maint'
[org-mode.git] / lisp / ob-ditaa.el
blobd3d76e57ac1a1ade2bf146578a59c92e4f966cd1
1 ;;; ob-ditaa.el --- org-babel functions for ditaa evaluation
3 ;; Copyright (C) 2009-2013 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)
40 (require 'org-compat)
42 (defvar org-babel-default-header-args:ditaa
43 '((:results . "file")
44 (:exports . "results")
45 (:java . "-Dfile.encoding=UTF-8"))
46 "Default arguments for evaluating a ditaa source block.")
48 (defcustom org-ditaa-jar-path (expand-file-name
49 "ditaa.jar"
50 (file-name-as-directory
51 (expand-file-name
52 "scripts"
53 (file-name-as-directory
54 (expand-file-name
55 "../contrib"
56 (file-name-directory (org-find-library-dir "org")))))))
57 "Path to the ditaa jar executable."
58 :group 'org-babel
59 :type 'string)
61 (defcustom org-ditaa-eps-jar-path
62 (expand-file-name "DitaaEps.jar" (file-name-directory org-ditaa-jar-path))
63 "Path to the DitaaEps.jar executable."
64 :group 'org-babel
65 :version "24.4"
66 :package-version '(Org . "8.0")
67 :type 'string)
69 (defcustom org-ditaa-jar-option "-jar"
70 "Option for the ditaa jar file.
71 Do not leave leading or trailing spaces in this string."
72 :group 'org-babel
73 :version "24.1"
74 :type 'string)
76 (defun org-babel-execute:ditaa (body params)
77 "Execute a block of Ditaa code with org-babel.
78 This function is called by `org-babel-execute-src-block'."
79 (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
80 (out-file ((lambda (el)
81 (or el
82 (error
83 "ditaa code block requires :file header argument")))
84 (cdr (assoc :file params))))
85 (cmdline (cdr (assoc :cmdline params)))
86 (java (cdr (assoc :java params)))
87 (in-file (org-babel-temp-file "ditaa-"))
88 (eps (cdr (assoc :eps params)))
89 (cmd (concat "java " java " " org-ditaa-jar-option " "
90 (shell-quote-argument
91 (expand-file-name
92 (if eps org-ditaa-eps-jar-path org-ditaa-jar-path)))
93 " " cmdline
94 " " (org-babel-process-file-name in-file)
95 " " (org-babel-process-file-name out-file)))
96 (pdf-cmd (when (and (or (string= (file-name-extension out-file) "pdf")
97 (cdr (assoc :pdf params))))
98 (concat
99 "epstopdf"
100 " " (org-babel-process-file-name (concat in-file ".eps"))
101 " -o=" (org-babel-process-file-name out-file)))))
102 (unless (file-exists-p org-ditaa-jar-path)
103 (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
104 (with-temp-file in-file (insert body))
105 (message cmd) (shell-command cmd)
106 (when pdf-cmd (message pdf-cmd) (shell-command pdf-cmd))
107 nil)) ;; signal that output has already been written to file
109 (defun org-babel-prep-session:ditaa (session params)
110 "Return an error because ditaa does not support sessions."
111 (error "Ditaa does not support sessions"))
113 (provide 'ob-ditaa)
117 ;;; ob-ditaa.el ends here