Merge branch 'maint'
[org-mode.git] / lisp / ob-asymptote.el
blobca58ecb392b0873d2a4d93e6f2e9d15c54fa27f0
1 ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation
3 ;; Copyright (C) 2009-2015 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 asymptote source code.
28 ;; This differs from most standard languages in that
30 ;; 1) there is no such thing as a "session" in asymptote
32 ;; 2) we are generally only going to return results of type "file"
34 ;; 3) we are adding the "file" and "cmdline" header arguments, if file
35 ;; is omitted then the -V option is passed to the asy command for
36 ;; interactive viewing
38 ;;; Requirements:
40 ;; - The asymptote program :: http://asymptote.sourceforge.net/
42 ;; - asy-mode :: Major mode for editing asymptote files
44 ;;; Code:
45 (require 'ob)
46 (eval-when-compile (require 'cl))
48 (defvar org-babel-tangle-lang-exts)
49 (add-to-list 'org-babel-tangle-lang-exts '("asymptote" . "asy"))
51 (defvar org-babel-default-header-args:asymptote
52 '((:results . "file") (:exports . "results"))
53 "Default arguments when evaluating an Asymptote source block.")
55 (defun org-babel-execute:asymptote (body params)
56 "Execute a block of Asymptote code.
57 This function is called by `org-babel-execute-src-block'."
58 (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
59 (out-file (cdr (assoc :file params)))
60 (format (or (and out-file
61 (string-match ".+\\.\\(.+\\)" out-file)
62 (match-string 1 out-file))
63 "pdf"))
64 (cmdline (cdr (assoc :cmdline params)))
65 (in-file (org-babel-temp-file "asymptote-"))
66 (cmd
67 (concat "asy "
68 (if out-file
69 (concat
70 "-globalwrite -f " format
71 " -o " (org-babel-process-file-name out-file))
72 "-V")
73 " " cmdline
74 " " (org-babel-process-file-name in-file))))
75 (with-temp-file in-file
76 (insert (org-babel-expand-body:generic
77 body params
78 (org-babel-variable-assignments:asymptote params))))
79 (message cmd) (shell-command cmd)
80 nil)) ;; signal that output has already been written to file
82 (defun org-babel-prep-session:asymptote (session params)
83 "Return an error if the :session header argument is set.
84 Asymptote does not support sessions"
85 (error "Asymptote does not support sessions"))
87 (defun org-babel-variable-assignments:asymptote (params)
88 "Return list of asymptote statements assigning the block's variables."
89 (mapcar #'org-babel-asymptote-var-to-asymptote
90 (mapcar #'cdr (org-babel-get-header params :var))))
92 (defun org-babel-asymptote-var-to-asymptote (pair)
93 "Convert an elisp value into an Asymptote variable.
94 The elisp value PAIR is converted into Asymptote code specifying
95 a variable of the same value."
96 (let ((var (car pair))
97 (val (let ((v (cdr pair)))
98 (if (symbolp v) (symbol-name v) v))))
99 (cond
100 ((integerp val)
101 (format "int %S=%S;" var val))
102 ((floatp val)
103 (format "real %S=%S;" var val))
104 ((stringp val)
105 (format "string %S=\"%s\";" var val))
106 ((and (listp val) (not (listp (car val))))
107 (let* ((type (org-babel-asymptote-define-type val))
108 (fmt (if (eq 'string type) "\"%s\"" "%s"))
109 (vect (mapconcat (lambda (e) (format fmt e)) val ", ")))
110 (format "%s[] %S={%s};" type var vect)))
111 ((listp val)
112 (let* ((type (org-babel-asymptote-define-type val))
113 (fmt (if (eq 'string type) "\"%s\"" "%s"))
114 (array (mapconcat (lambda (row)
115 (concat "{"
116 (mapconcat (lambda (e) (format fmt e))
117 row ", ")
118 "}"))
119 val ",")))
120 (format "%S[][] %S={%s};" type var array))))))
122 (defun org-babel-asymptote-define-type (data)
123 "Determine type of DATA.
125 DATA is a list. Return type as a symbol.
127 The type is `string' if any element in DATA is
128 a string. Otherwise, it is either `real', if some elements are
129 floats, or `int'."
130 (let* ((type 'int)
131 find-type ; for byte-compiler
132 (find-type
133 (function
134 (lambda (row)
135 (catch 'exit
136 (mapc (lambda (el)
137 (cond ((listp el) (funcall find-type el))
138 ((stringp el) (throw 'exit (setq type 'string)))
139 ((floatp el) (setq type 'real))))
140 row))))))
141 (funcall find-type data) type))
143 (provide 'ob-asymptote)
147 ;;; ob-asymptote.el ends here