Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2009-2014 Free Software Foundation, Inc. |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
86fbb8ca CD |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
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. | |
15 | ||
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. | |
20 | ||
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/>. | |
23 | ||
24 | ;;; Commentary: | |
25 | ||
26 | ;; Org-Babel support for evaluating asymptote source code. | |
27 | ;; | |
28 | ;; This differs from most standard languages in that | |
29 | ;; | |
30 | ;; 1) there is no such thing as a "session" in asymptote | |
31 | ;; | |
32 | ;; 2) we are generally only going to return results of type "file" | |
33 | ;; | |
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 | |
37 | ||
38 | ;;; Requirements: | |
39 | ||
40 | ;; - The asymptote program :: http://asymptote.sourceforge.net/ | |
41 | ;; | |
42 | ;; - asy-mode :: Major mode for editing asymptote files | |
43 | ||
44 | ;;; Code: | |
45 | (require 'ob) | |
46 | (eval-when-compile (require 'cl)) | |
47 | ||
48 | (declare-function orgtbl-to-generic "org-table" (table params)) | |
49 | (declare-function org-combine-plists "org" (&rest plists)) | |
50 | ||
3ab2c837 | 51 | (defvar org-babel-tangle-lang-exts) |
86fbb8ca CD |
52 | (add-to-list 'org-babel-tangle-lang-exts '("asymptote" . "asy")) |
53 | ||
54 | (defvar org-babel-default-header-args:asymptote | |
55 | '((:results . "file") (:exports . "results")) | |
56 | "Default arguments when evaluating an Asymptote source block.") | |
57 | ||
86fbb8ca CD |
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'." | |
afe98dfa | 61 | (let* ((result-params (split-string (or (cdr (assoc :results params)) ""))) |
86fbb8ca CD |
62 | (out-file (cdr (assoc :file params))) |
63 | (format (or (and out-file | |
64 | (string-match ".+\\.\\(.+\\)" out-file) | |
65 | (match-string 1 out-file)) | |
66 | "pdf")) | |
67 | (cmdline (cdr (assoc :cmdline params))) | |
afe98dfa CD |
68 | (in-file (org-babel-temp-file "asymptote-")) |
69 | (cmd | |
70 | (concat "asy " | |
71 | (if out-file | |
72 | (concat | |
73 | "-globalwrite -f " format | |
74 | " -o " (org-babel-process-file-name out-file)) | |
75 | "-V") | |
76 | " " cmdline | |
77 | " " (org-babel-process-file-name in-file)))) | |
86fbb8ca | 78 | (with-temp-file in-file |
afe98dfa CD |
79 | (insert (org-babel-expand-body:generic |
80 | body params | |
81 | (org-babel-variable-assignments:asymptote params)))) | |
86fbb8ca | 82 | (message cmd) (shell-command cmd) |
3ab2c837 | 83 | nil)) ;; signal that output has already been written to file |
86fbb8ca CD |
84 | |
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")) | |
89 | ||
afe98dfa | 90 | (defun org-babel-variable-assignments:asymptote (params) |
8223b1d2 | 91 | "Return list of asymptote statements assigning the block's variables." |
afe98dfa CD |
92 | (mapcar #'org-babel-asymptote-var-to-asymptote |
93 | (mapcar #'cdr (org-babel-get-header params :var)))) | |
94 | ||
86fbb8ca CD |
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)) | |
e66ba1df BG |
100 | (val (let ((v (cdr pair))) |
101 | (if (symbolp v) (symbol-name v) v)))) | |
86fbb8ca CD |
102 | (cond |
103 | ((integerp val) | |
104 | (format "int %S=%S;" var val)) | |
105 | ((floatp val) | |
106 | (format "real %S=%S;" var val)) | |
107 | ((stringp val) | |
108 | (format "string %S=\"%s\";" var val)) | |
e66ba1df BG |
109 | ((and (listp val) (not (listp (car val)))) |
110 | (let* ((type (org-babel-asymptote-define-type val)) | |
111 | (fmt (if (eq 'string type) "\"%s\"" "%s")) | |
112 | (vect (mapconcat (lambda (e) (format fmt e)) val ", "))) | |
113 | (format "%s[] %S={%s};" type var vect))) | |
86fbb8ca | 114 | ((listp val) |
e66ba1df BG |
115 | (let* ((type (org-babel-asymptote-define-type val)) |
116 | (fmt (if (eq 'string type) "\"%s\"" "%s")) | |
117 | (array (mapconcat (lambda (row) | |
118 | (concat "{" | |
119 | (mapconcat (lambda (e) (format fmt e)) | |
120 | row ", ") | |
121 | "}")) | |
122 | val ","))) | |
123 | (format "%S[][] %S={%s};" type var array)))))) | |
86fbb8ca CD |
124 | |
125 | (defun org-babel-asymptote-define-type (data) | |
126 | "Determine type of DATA. | |
e66ba1df BG |
127 | |
128 | DATA is a list. Return type as a symbol. | |
129 | ||
130 | The type is `string' if any element in DATA is | |
8223b1d2 | 131 | a string. Otherwise, it is either `real', if some elements are |
e66ba1df BG |
132 | floats, or `int'." |
133 | (let* ((type 'int) | |
134 | find-type ; for byte-compiler | |
135 | (find-type | |
136 | (function | |
137 | (lambda (row) | |
138 | (catch 'exit | |
139 | (mapc (lambda (el) | |
140 | (cond ((listp el) (funcall find-type el)) | |
141 | ((stringp el) (throw 'exit (setq type 'string))) | |
142 | ((floatp el) (setq type 'real)))) | |
143 | row)))))) | |
144 | (funcall find-type data) type)) | |
86fbb8ca CD |
145 | |
146 | (provide 'ob-asymptote) | |
147 | ||
5b409b39 | 148 | |
86fbb8ca CD |
149 | |
150 | ;;; ob-asymptote.el ends here |