Release 7.01
[org-mode.git] / lisp / ob-asymptote.el
blob043bc4c5ff7325fe131096c666a24fdfb5922186
1 ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.01
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating asymptote source code.
29 ;; This differs from most standard languages in that
31 ;; 1) there is no such thing as a "session" in asymptote
33 ;; 2) we are generally only going to return results of type "file"
35 ;; 3) we are adding the "file" and "cmdline" header arguments, if file
36 ;; is omitted then the -V option is passed to the asy command for
37 ;; interactive viewing
39 ;;; Requirements:
41 ;; - The asymptote program :: http://asymptote.sourceforge.net/
43 ;; - asy-mode :: Major mode for editing asymptote files
45 ;;; Code:
46 (require 'ob)
47 (eval-when-compile (require 'cl))
49 (declare-function orgtbl-to-generic "org-table" (table params))
50 (declare-function org-combine-plists "org" (&rest plists))
52 (add-to-list 'org-babel-tangle-lang-exts '("asymptote" . "asy"))
54 (defvar org-babel-default-header-args:asymptote
55 '((:results . "file") (:exports . "results"))
56 "Default arguments when evaluating an Asymptote source block.")
58 (defun org-babel-expand-body:asymptote (body params &optional processed-params)
59 "Expand BODY according to PARAMS, return the expanded body."
60 (let ((vars (nth 1 (or processed-params
61 (org-babel-process-params params)))))
62 (concat (mapconcat 'org-babel-asymptote-var-to-asymptote vars "\n")
63 "\n" body "\n")))
65 (defun org-babel-execute:asymptote (body params)
66 "Execute a block of Asymptote code.
67 This function is called by `org-babel-execute-src-block'."
68 (let* ((processed-params (org-babel-process-params params))
69 (result-params (split-string (or (cdr (assoc :results params)) "")))
70 (out-file (cdr (assoc :file params)))
71 (format (or (and out-file
72 (string-match ".+\\.\\(.+\\)" out-file)
73 (match-string 1 out-file))
74 "pdf"))
75 (cmdline (cdr (assoc :cmdline params)))
76 (in-file (make-temp-file "org-babel-asymptote"))
77 (cmd (concat "asy "
78 (if out-file
79 (concat "-globalwrite -f " format " -o " out-file)
80 "-V")
81 " " cmdline " " in-file)))
82 (with-temp-file in-file
83 (insert (org-babel-expand-body:asymptote body params processed-params)))
84 (message cmd) (shell-command cmd)
85 out-file))
87 (defun org-babel-prep-session:asymptote (session params)
88 "Return an error if the :session header argument is set.
89 Asymptote does not support sessions"
90 (error "Asymptote does not support sessions"))
92 (defun org-babel-asymptote-var-to-asymptote (pair)
93 "Convert an elisp value into an Asymptote variable.
94 The elisp value PAIR is converted into Asymptote code specifying
95 a variable of the same value."
96 (let ((var (car pair))
97 (val (if (symbolp (cdr pair))
98 (symbol-name (cdr pair))
99 (cdr pair))))
100 (cond
101 ((integerp val)
102 (format "int %S=%S;" var val))
103 ((floatp val)
104 (format "real %S=%S;" var val))
105 ((stringp val)
106 (format "string %S=\"%s\";" var val))
107 ((listp val)
108 (let* ((dimension-2-p (not (null (cdr val))))
109 (dim (if dimension-2-p "[][]" "[]"))
110 (type (org-babel-asymptote-define-type val))
111 (array (org-babel-asymptote-table-to-array
113 (if dimension-2-p '(:lstart "{" :lend "}," :llend "}")))))
114 (format "%S%s %S=%s;" type dim var array))))))
116 (defun org-babel-asymptote-table-to-array (table params)
117 "Convert values of an elisp table into a string of an asymptote array.
118 Empty cells are ignored."
119 (labels ((atom-to-string (table)
120 (cond
121 ((null table) '())
122 ((not (listp (car table)))
123 (cons (if (and (stringp (car table))
124 (not (string= (car table) "")))
125 (format "\"%s\"" (car table))
126 (format "%s" (car table)))
127 (atom-to-string (cdr table))))
129 (cons (atom-to-string (car table))
130 (atom-to-string (cdr table))))))
131 ;; Remove any empty row
132 (fix-empty-lines (table)
133 (delq nil (mapcar (lambda (l) (delq "" l)) table))))
134 (orgtbl-to-generic
135 (fix-empty-lines (atom-to-string table))
136 (org-combine-plists '(:hline nil :sep "," :tstart "{" :tend "}") params))))
138 (defun org-babel-asymptote-define-type (data)
139 "Determine type of DATA.
140 DATA is a list. Type symbol is returned as 'symbol. The type is
141 usually the type of the first atom encountered, except for arrays
142 of int, where every cell must be of int type."
143 (labels ((anything-but-int (el)
144 (cond
145 ((null el) nil)
146 ((not (listp (car el)))
147 (cond
148 ((floatp (car el)) 'real)
149 ((stringp (car el)) 'string)
151 (anything-but-int (cdr el)))))
153 (or (anything-but-int (car el))
154 (anything-but-int (cdr el)))))))
155 (or (anything-but-int data) 'int)))
157 (provide 'ob-asymptote)
159 ;; arch-tag: f2f5bd0d-78e8-412b-8e6c-6dadc94cc06b
161 ;;; ob-asymptote.el ends here