org-eww: Fix docstrings
[org-mode/org-tableheadings.git] / lisp / ob-asymptote.el
blob7c6e781f0ef8cbef25560e85266ec1ca5933274b
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/>.
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)
47 (defvar org-babel-tangle-lang-exts)
48 (add-to-list 'org-babel-tangle-lang-exts '("asymptote" . "asy"))
50 (defvar org-babel-default-header-args:asymptote
51 '((:results . "file") (:exports . "results"))
52 "Default arguments when evaluating an Asymptote source block.")
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 (assoc :file params)))
58 (format (or (and out-file
59 (string-match ".+\\.\\(.+\\)" out-file)
60 (match-string 1 out-file))
61 "pdf"))
62 (cmdline (cdr (assoc :cmdline params)))
63 (in-file (org-babel-temp-file "asymptote-"))
64 (cmd
65 (concat "asy "
66 (if out-file
67 (concat
68 "-globalwrite -f " format
69 " -o " (org-babel-process-file-name out-file))
70 "-V")
71 " " cmdline
72 " " (org-babel-process-file-name in-file))))
73 (with-temp-file in-file
74 (insert (org-babel-expand-body:generic
75 body params
76 (org-babel-variable-assignments:asymptote params))))
77 (message cmd) (shell-command cmd)
78 nil)) ;; signal that output has already been written to file
80 (defun org-babel-prep-session:asymptote (_session _params)
81 "Return an error if the :session header argument is set.
82 Asymptote does not support sessions"
83 (error "Asymptote does not support sessions"))
85 (defun org-babel-variable-assignments:asymptote (params)
86 "Return list of asymptote statements assigning the block's variables."
87 (mapcar #'org-babel-asymptote-var-to-asymptote
88 (org-babel--get-vars params)))
90 (defun org-babel-asymptote-var-to-asymptote (pair)
91 "Convert an elisp value into an Asymptote variable.
92 The elisp value PAIR is converted into Asymptote code specifying
93 a variable of the same value."
94 (let ((var (car pair))
95 (val (let ((v (cdr pair)))
96 (if (symbolp v) (symbol-name v) v))))
97 (cond
98 ((integerp val)
99 (format "int %S=%S;" var val))
100 ((floatp val)
101 (format "real %S=%S;" var val))
102 ((stringp val)
103 (format "string %S=\"%s\";" var val))
104 ((and (listp val) (not (listp (car val))))
105 (let* ((type (org-babel-asymptote-define-type val))
106 (fmt (if (eq 'string type) "\"%s\"" "%s"))
107 (vect (mapconcat (lambda (e) (format fmt e)) val ", ")))
108 (format "%s[] %S={%s};" type var vect)))
109 ((listp val)
110 (let* ((type (org-babel-asymptote-define-type val))
111 (fmt (if (eq 'string type) "\"%s\"" "%s"))
112 (array (mapconcat (lambda (row)
113 (concat "{"
114 (mapconcat (lambda (e) (format fmt e))
115 row ", ")
116 "}"))
117 val ",")))
118 (format "%S[][] %S={%s};" type var array))))))
120 (defun org-babel-asymptote-define-type (data)
121 "Determine type of DATA.
123 DATA is a list. Return type as a symbol.
125 The type is `string' if any element in DATA is a string.
126 Otherwise, it is either `real', if some elements are floats, or
127 `int'."
128 (letrec ((type 'int)
129 (find-type
130 (lambda (row)
131 (dolist (e row type)
132 (cond ((listp e) (setq type (funcall find-type e)))
133 ((stringp e) (throw 'exit 'string))
134 ((floatp e) (setq type 'real)))))))
135 (catch 'exit (funcall find-type data)) type))
137 (provide 'ob-asymptote)
141 ;;; ob-asymptote.el ends here