1 ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation
3 ;; Copyright (C) 2009-2011 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/>.
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
40 ;; - The asymptote program :: http://asymptote.sourceforge.net/
42 ;; - asy-mode :: Major mode for editing asymptote files
46 (eval-when-compile (require 'cl
))
48 (declare-function orgtbl-to-generic
"org-table" (table params
))
49 (declare-function org-combine-plists
"org" (&rest plists
))
51 (defvar org-babel-tangle-lang-exts
)
52 (add-to-list 'org-babel-tangle-lang-exts
'("asymptote" .
"asy"))
54 (defvar org-babel-default-header-args
:asymptote
55 '((:results .
"file") (:exports .
"results"))
56 "Default arguments when evaluating an Asymptote source block.")
58 (defun org-babel-execute:asymptote
(body params
)
59 "Execute a block of Asymptote code.
60 This function is called by `org-babel-execute-src-block'."
61 (let* ((result-params (split-string (or (cdr (assoc :results params
)) "")))
62 (out-file (cdr (assoc :file params
)))
63 (format (or (and out-file
64 (string-match ".+\\.\\(.+\\)" out-file
)
65 (match-string 1 out-file
))
67 (cmdline (cdr (assoc :cmdline params
)))
68 (in-file (org-babel-temp-file "asymptote-"))
73 "-globalwrite -f " format
74 " -o " (org-babel-process-file-name out-file
))
77 " " (org-babel-process-file-name in-file
))))
78 (with-temp-file in-file
79 (insert (org-babel-expand-body:generic
81 (org-babel-variable-assignments:asymptote params
))))
82 (message cmd
) (shell-command cmd
)
83 nil
)) ;; signal that output has already been written to file
85 (defun org-babel-prep-session:asymptote
(session params
)
86 "Return an error if the :session header argument is set.
87 Asymptote does not support sessions"
88 (error "Asymptote does not support sessions"))
90 (defun org-babel-variable-assignments:asymptote
(params)
91 "Return list of asymptote statements assigning the block's variables"
92 (mapcar #'org-babel-asymptote-var-to-asymptote
93 (mapcar #'cdr
(org-babel-get-header params
:var
))))
95 (defun org-babel-asymptote-var-to-asymptote (pair)
96 "Convert an elisp value into an Asymptote variable.
97 The elisp value PAIR is converted into Asymptote code specifying
98 a variable of the same value."
99 (let ((var (car pair
))
100 (val (if (symbolp (cdr pair
))
101 (symbol-name (cdr pair
))
105 (format "int %S=%S;" var val
))
107 (format "real %S=%S;" var val
))
109 (format "string %S=\"%s\";" var val
))
111 (let* ((dimension-2-p (cdr val
))
112 (dim (if dimension-2-p
"[][]" "[]"))
113 (type (org-babel-asymptote-define-type val
))
114 (array (org-babel-asymptote-table-to-array
116 (if dimension-2-p
'(:lstart
"{" :lend
"}," :llend
"}")))))
117 (format "%S%s %S=%s;" type dim var array
))))))
119 (defun org-babel-asymptote-table-to-array (table type params
)
120 "Convert values of TABLE into a string of an asymptote array.
122 TABLE is a list whose atoms are assumed to be of type
123 TYPE. PARAMS is a plist of parameters that can influence the
126 Empty cells are ignored."
127 (labels ((atom-to-string (table)
130 ((not (listp (car table
)))
131 (cons (if (or (eq type
'string
)
132 (and (stringp (car table
))
133 (not (string= (car table
) ""))))
134 (format "\"%s\"" (car table
))
135 (format "%s" (car table
)))
136 (atom-to-string (cdr table
))))
138 (cons (atom-to-string (car table
))
139 (atom-to-string (cdr table
))))))
140 ;; Remove any empty row
141 (fix-empty-lines (table)
142 (delq nil
(mapcar (lambda (l) (delq "" l
)) table
))))
144 (fix-empty-lines (atom-to-string table
))
145 (org-combine-plists '(:hline nil
:sep
"," :tstart
"{" :tend
"}") params
))))
147 (defun org-babel-asymptote-define-type (data)
148 "Determine type of DATA.
150 DATA is a list. Return type as a symbol.
152 The type is `string' if any element in DATA is
153 a string. Otherwise, it is either `real', if some elements are
160 (cond ((listp el
) (funcall find-type el
))
161 ((stringp el
) (throw 'exit
(setq type
'string
)))
162 ((floatp el
) (setq type
'real
))))
164 (funcall find-type data
) type
))
166 (provide 'ob-asymptote
)
170 ;;; ob-asymptote.el ends here