Merge from origin/emacs-25
[emacs.git] / lisp / org / ob-asymptote.el
blob2d27757fe12b74ed9b300bbc90dc0fe8ae2deb9c
1 ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation
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/>.
24 ;;; Commentary:
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
38 ;;; Requirements:
40 ;; - The asymptote program :: http://asymptote.sourceforge.net/
42 ;; - asy-mode :: Major mode for editing asymptote files
44 ;;; Code:
45 (require 'ob)
46 (eval-when-compile (require 'cl))
48 (declare-function orgtbl-to-generic "org-table"
49 (table params &optional backend))
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))
67 "pdf"))
68 (cmdline (cdr (assoc :cmdline params)))
69 (in-file (org-babel-temp-file "asymptote-"))
70 (cmd
71 (concat "asy "
72 (if out-file
73 (concat
74 "-globalwrite -f " format
75 " -o " (org-babel-process-file-name out-file))
76 "-V")
77 " " cmdline
78 " " (org-babel-process-file-name in-file))))
79 (with-temp-file in-file
80 (insert (org-babel-expand-body:generic
81 body params
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 (let ((v (cdr pair)))
102 (if (symbolp v) (symbol-name v) v))))
103 (cond
104 ((integerp val)
105 (format "int %S=%S;" var val))
106 ((floatp val)
107 (format "real %S=%S;" var val))
108 ((stringp val)
109 (format "string %S=\"%s\";" var val))
110 ((and (listp val) (not (listp (car val))))
111 (let* ((type (org-babel-asymptote-define-type val))
112 (fmt (if (eq 'string type) "\"%s\"" "%s"))
113 (vect (mapconcat (lambda (e) (format fmt e)) val ", ")))
114 (format "%s[] %S={%s};" type var vect)))
115 ((listp val)
116 (let* ((type (org-babel-asymptote-define-type val))
117 (fmt (if (eq 'string type) "\"%s\"" "%s"))
118 (array (mapconcat (lambda (row)
119 (concat "{"
120 (mapconcat (lambda (e) (format fmt e))
121 row ", ")
122 "}"))
123 val ",")))
124 (format "%S[][] %S={%s};" type var array))))))
126 (defun org-babel-asymptote-define-type (data)
127 "Determine type of DATA.
129 DATA is a list. Return type as a symbol.
131 The type is `string' if any element in DATA is
132 a string. Otherwise, it is either `real', if some elements are
133 floats, or `int'."
134 (let* ((type 'int)
135 find-type ; for byte-compiler
136 (find-type
137 (function
138 (lambda (row)
139 (catch 'exit
140 (mapc (lambda (el)
141 (cond ((listp el) (funcall find-type el))
142 ((stringp el) (throw 'exit (setq type 'string)))
143 ((floatp el) (setq type 'real))))
144 row))))))
145 (funcall find-type data) type))
147 (provide 'ob-asymptote)
151 ;;; ob-asymptote.el ends here