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
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; 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/>.
27 ;; Org-Babel support for evaluating asymptote source code.
29 ;; This differs from most standard languages in that
31 ;; 1) there is no such thing as a "session" in asymptote
33 ;; 2) we are generally only going to return results of type "file"
35 ;; 3) we are adding the "file" and "cmdline" header arguments, if file
36 ;; is omitted then the -V option is passed to the asy command for
37 ;; interactive viewing
41 ;; - The asymptote program :: http://asymptote.sourceforge.net/
43 ;; - asy-mode :: Major mode for editing asymptote files
47 (eval-when-compile (require 'cl
))
49 (declare-function orgtbl-to-generic
"org-table" (table params
))
50 (declare-function org-combine-plists
"org" (&rest plists
))
52 (defvar org-babel-tangle-lang-exts
)
53 (add-to-list 'org-babel-tangle-lang-exts
'("asymptote" .
"asy"))
55 (defvar org-babel-default-header-args
:asymptote
56 '((:results .
"file") (:exports .
"results"))
57 "Default arguments when evaluating an Asymptote source block.")
59 (defun org-babel-execute:asymptote
(body params
)
60 "Execute a block of Asymptote code.
61 This function is called by `org-babel-execute-src-block'."
62 (let* ((result-params (split-string (or (cdr (assoc :results params
)) "")))
63 (out-file (cdr (assoc :file params
)))
64 (format (or (and out-file
65 (string-match ".+\\.\\(.+\\)" out-file
)
66 (match-string 1 out-file
))
68 (cmdline (cdr (assoc :cmdline params
)))
69 (in-file (org-babel-temp-file "asymptote-"))
74 "-globalwrite -f " format
75 " -o " (org-babel-process-file-name out-file
))
78 " " (org-babel-process-file-name in-file
))))
79 (with-temp-file in-file
80 (insert (org-babel-expand-body:generic
82 (org-babel-variable-assignments:asymptote params
))))
83 (message cmd
) (shell-command cmd
)
84 nil
)) ;; signal that output has already been written to file
86 (defun org-babel-prep-session:asymptote
(session params
)
87 "Return an error if the :session header argument is set.
88 Asymptote does not support sessions"
89 (error "Asymptote does not support sessions"))
91 (defun org-babel-variable-assignments:asymptote
(params)
92 "Return list of asymptote statements assigning the block's variables"
93 (mapcar #'org-babel-asymptote-var-to-asymptote
94 (mapcar #'cdr
(org-babel-get-header params
:var
))))
96 (defun org-babel-asymptote-var-to-asymptote (pair)
97 "Convert an elisp value into an Asymptote variable.
98 The elisp value PAIR is converted into Asymptote code specifying
99 a variable of the same value."
100 (let ((var (car pair
))
101 (val (if (symbolp (cdr pair
))
102 (symbol-name (cdr pair
))
106 (format "int %S=%S;" var val
))
108 (format "real %S=%S;" var val
))
110 (format "string %S=\"%s\";" var val
))
112 (let* ((dimension-2-p (not (null (cdr val
))))
113 (dim (if dimension-2-p
"[][]" "[]"))
114 (type (org-babel-asymptote-define-type val
))
115 (array (org-babel-asymptote-table-to-array
117 (if dimension-2-p
'(:lstart
"{" :lend
"}," :llend
"}")))))
118 (format "%S%s %S=%s;" type dim var array
))))))
120 (defun org-babel-asymptote-table-to-array (table params
)
121 "Convert values of an elisp table into a string of an asymptote array.
122 Empty cells are ignored."
123 (labels ((atom-to-string (table)
126 ((not (listp (car table
)))
127 (cons (if (and (stringp (car table
))
128 (not (string= (car table
) "")))
129 (format "\"%s\"" (car table
))
130 (format "%s" (car table
)))
131 (atom-to-string (cdr table
))))
133 (cons (atom-to-string (car table
))
134 (atom-to-string (cdr table
))))))
135 ;; Remove any empty row
136 (fix-empty-lines (table)
137 (delq nil
(mapcar (lambda (l) (delq "" l
)) table
))))
139 (fix-empty-lines (atom-to-string table
))
140 (org-combine-plists '(:hline nil
:sep
"," :tstart
"{" :tend
"}") params
))))
142 (defun org-babel-asymptote-define-type (data)
143 "Determine type of DATA.
144 DATA is a list. Type symbol is returned as 'symbol. The type is
145 usually the type of the first atom encountered, except for arrays
146 of int, where every cell must be of int type."
147 (labels ((anything-but-int (el)
150 ((not (listp (car el
)))
152 ((floatp (car el
)) 'real
)
153 ((stringp (car el
)) 'string
)
155 (anything-but-int (cdr el
)))))
157 (or (anything-but-int (car el
))
158 (anything-but-int (cdr el
)))))))
159 (or (anything-but-int data
) 'int
)))
161 (provide 'ob-asymptote
)
165 ;;; ob-asymptote.el ends here