1 ;;; ob-asymptote.el --- Babel Functions for Asymptote -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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 (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* ((out-file (cdr (assoc :file params
)))
59 (format (or (and out-file
60 (string-match ".+\\.\\(.+\\)" out-file
)
61 (match-string 1 out-file
))
63 (cmdline (cdr (assoc :cmdline params
)))
64 (in-file (org-babel-temp-file "asymptote-"))
69 "-globalwrite -f " format
70 " -o " (org-babel-process-file-name out-file
))
73 " " (org-babel-process-file-name in-file
))))
74 (with-temp-file in-file
75 (insert (org-babel-expand-body:generic
77 (org-babel-variable-assignments:asymptote params
))))
78 (message cmd
) (shell-command cmd
)
79 nil
)) ;; signal that output has already been written to file
81 (defun org-babel-prep-session:asymptote
(_session _params
)
82 "Return an error if the :session header argument is set.
83 Asymptote does not support sessions"
84 (error "Asymptote does not support sessions"))
86 (defun org-babel-variable-assignments:asymptote
(params)
87 "Return list of asymptote statements assigning the block's variables."
88 (mapcar #'org-babel-asymptote-var-to-asymptote
89 (org-babel--get-vars params
)))
91 (defun org-babel-asymptote-var-to-asymptote (pair)
92 "Convert an elisp value into an Asymptote variable.
93 The elisp value PAIR is converted into Asymptote code specifying
94 a variable of the same value."
95 (let ((var (car pair
))
96 (val (let ((v (cdr pair
)))
97 (if (symbolp v
) (symbol-name v
) v
))))
100 (format "int %S=%S;" var val
))
102 (format "real %S=%S;" var val
))
104 (format "string %S=\"%s\";" var val
))
105 ((and (listp val
) (not (listp (car val
))))
106 (let* ((type (org-babel-asymptote-define-type val
))
107 (fmt (if (eq 'string type
) "\"%s\"" "%s"))
108 (vect (mapconcat (lambda (e) (format fmt e
)) val
", ")))
109 (format "%s[] %S={%s};" type var vect
)))
111 (let* ((type (org-babel-asymptote-define-type val
))
112 (fmt (if (eq 'string type
) "\"%s\"" "%s"))
113 (array (mapconcat (lambda (row)
115 (mapconcat (lambda (e) (format fmt e
))
119 (format "%S[][] %S={%s};" type var array
))))))
121 (defun org-babel-asymptote-define-type (data)
122 "Determine type of DATA.
124 DATA is a list. Return type as a symbol.
126 The type is `string' if any element in DATA is a string.
127 Otherwise, it is either `real', if some elements are floats, or
133 (cond ((listp e
) (setq type
(funcall find-type e
)))
134 ((stringp e
) (throw 'exit
'string
))
135 ((floatp e
) (setq type
'real
)))))))
136 (catch 'exit
(funcall find-type data
)) type
))
138 (provide 'ob-asymptote
)
142 ;;; ob-asymptote.el ends here