commit | author | age
|
76bbd0
|
1 |
;;; ob-asymptote.el --- Babel Functions for Asymptote -*- lexical-binding: t; -*- |
C |
2 |
|
|
3 |
;; Copyright (C) 2009-2018 Free Software Foundation, Inc. |
|
4 |
|
|
5 |
;; Author: Eric Schulte |
|
6 |
;; Keywords: literate programming, reproducible research |
|
7 |
;; Homepage: https://orgmode.org |
|
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 <https://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 |
|
|
47 |
(defvar org-babel-tangle-lang-exts) |
|
48 |
(add-to-list 'org-babel-tangle-lang-exts '("asymptote" . "asy")) |
|
49 |
|
|
50 |
(defvar org-babel-default-header-args:asymptote |
|
51 |
'((:results . "file") (:exports . "results")) |
|
52 |
"Default arguments when evaluating an Asymptote source block.") |
|
53 |
|
|
54 |
(defun org-babel-execute:asymptote (body params) |
|
55 |
"Execute a block of Asymptote code. |
|
56 |
This function is called by `org-babel-execute-src-block'." |
|
57 |
(let* ((out-file (cdr (assq :file params))) |
|
58 |
(format (or (file-name-extension out-file) |
|
59 |
"pdf")) |
|
60 |
(cmdline (cdr (assq :cmdline params))) |
|
61 |
(in-file (org-babel-temp-file "asymptote-")) |
|
62 |
(cmd |
|
63 |
(concat "asy " |
|
64 |
(if out-file |
|
65 |
(concat |
|
66 |
"-globalwrite -f " format |
|
67 |
" -o " (org-babel-process-file-name out-file)) |
|
68 |
"-V") |
|
69 |
" " cmdline |
|
70 |
" " (org-babel-process-file-name in-file)))) |
|
71 |
(with-temp-file in-file |
|
72 |
(insert (org-babel-expand-body:generic |
|
73 |
body params |
|
74 |
(org-babel-variable-assignments:asymptote params)))) |
|
75 |
(message cmd) (shell-command cmd) |
|
76 |
nil)) ;; signal that output has already been written to file |
|
77 |
|
|
78 |
(defun org-babel-prep-session:asymptote (_session _params) |
|
79 |
"Return an error if the :session header argument is set. |
|
80 |
Asymptote does not support sessions" |
|
81 |
(error "Asymptote does not support sessions")) |
|
82 |
|
|
83 |
(defun org-babel-variable-assignments:asymptote (params) |
|
84 |
"Return list of asymptote statements assigning the block's variables." |
|
85 |
(mapcar #'org-babel-asymptote-var-to-asymptote |
|
86 |
(org-babel--get-vars params))) |
|
87 |
|
|
88 |
(defun org-babel-asymptote-var-to-asymptote (pair) |
|
89 |
"Convert an elisp value into an Asymptote variable. |
|
90 |
The elisp value PAIR is converted into Asymptote code specifying |
|
91 |
a variable of the same value." |
|
92 |
(let ((var (car pair)) |
|
93 |
(val (let ((v (cdr pair))) |
|
94 |
(if (symbolp v) (symbol-name v) v)))) |
|
95 |
(cond |
|
96 |
((integerp val) |
|
97 |
(format "int %S=%S;" var val)) |
|
98 |
((floatp val) |
|
99 |
(format "real %S=%S;" var val)) |
|
100 |
((stringp val) |
|
101 |
(format "string %S=\"%s\";" var val)) |
|
102 |
((and (listp val) (not (listp (car val)))) |
|
103 |
(let* ((type (org-babel-asymptote-define-type val)) |
|
104 |
(fmt (if (eq 'string type) "\"%s\"" "%s")) |
|
105 |
(vect (mapconcat (lambda (e) (format fmt e)) val ", "))) |
|
106 |
(format "%s[] %S={%s};" type var vect))) |
|
107 |
((listp val) |
|
108 |
(let* ((type (org-babel-asymptote-define-type val)) |
|
109 |
(fmt (if (eq 'string type) "\"%s\"" "%s")) |
|
110 |
(array (mapconcat (lambda (row) |
|
111 |
(concat "{" |
|
112 |
(mapconcat (lambda (e) (format fmt e)) |
|
113 |
row ", ") |
|
114 |
"}")) |
|
115 |
val ","))) |
|
116 |
(format "%S[][] %S={%s};" type var array)))))) |
|
117 |
|
|
118 |
(defun org-babel-asymptote-define-type (data) |
|
119 |
"Determine type of DATA. |
|
120 |
|
|
121 |
DATA is a list. Return type as a symbol. |
|
122 |
|
|
123 |
The type is `string' if any element in DATA is a string. |
|
124 |
Otherwise, it is either `real', if some elements are floats, or |
|
125 |
`int'." |
|
126 |
(letrec ((type 'int) |
|
127 |
(find-type |
|
128 |
(lambda (row) |
|
129 |
(dolist (e row type) |
|
130 |
(cond ((listp e) (setq type (funcall find-type e))) |
|
131 |
((stringp e) (throw 'exit 'string)) |
|
132 |
((floatp e) (setq type 'real))))))) |
|
133 |
(catch 'exit (funcall find-type data)) type)) |
|
134 |
|
|
135 |
(provide 'ob-asymptote) |
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
;;; ob-asymptote.el ends here |