babel: remove all language file reference to define language variables
[org-mode.git] / lisp / babel / langs / ob-asymptote.el
blob53b42bfd42f0fe527dc3661f4979e0541dd77482
1 ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 0.01
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; Org-Babel support for evaluating asymptote source code.
31 ;; This differs from most standard languages in that
33 ;; 1) there is no such thing as a "session" in asymptote
35 ;; 2) we are generally only going to return results of type "file"
37 ;; 3) we are adding the "file" and "cmdline" header arguments, if file
38 ;; is omitted then the -V option is passed to the asy command for
39 ;; interactive viewing
41 ;;; Requirements:
43 ;; - The asymptote program :: http://asymptote.sourceforge.net/
45 ;; - asy-mode :: Major mode for editing asymptote files
47 ;;; Code:
48 (require 'ob)
50 (defvar org-babel-default-header-args:asymptote
51 '((:results . "file") (:exports . "results"))
52 "Default arguments to use when evaluating a asymptote source block.")
54 (defun org-babel-expand-body:asymptote (body params &optional processed-params)
55 "Expand BODY according to PARAMS, return the expanded body."
56 (let ((vars (second (or processed-params
57 (org-babel-process-params params)))))
58 (concat (mapconcat 'org-babel-asymptote-var-to-asymptote vars "\n")
59 "\n" body "\n")))
61 (defun org-babel-execute:asymptote (body params)
62 "Execute a block of Asymptote code with org-babel. This function is
63 called by `org-babel-execute-src-block'."
64 (message "executing Asymptote source code block")
65 (let* ((processed-params (org-babel-process-params params))
66 (result-params (split-string (or (cdr (assoc :results params)) "")))
67 (out-file (cdr (assoc :file params)))
68 (format (or (and out-file
69 (string-match ".+\\.\\(.+\\)" out-file)
70 (match-string 1 out-file))
71 "pdf"))
72 (cmdline (cdr (assoc :cmdline params)))
73 (in-file (make-temp-file "org-babel-asymptote"))
74 (cmd (concat "asy "
75 (if out-file
76 (concat "-globalwrite -f " format " -o " out-file)
77 "-V")
78 " " cmdline " " in-file)))
79 (with-temp-file in-file
80 (insert (org-babel-expand-body:asymptote body params processed-params)))
81 (message cmd) (shell-command cmd)
82 out-file))
84 (defun org-babel-prep-session:asymptote (session params)
85 "Prepare a session named SESSION according to PARAMS."
86 (error "Asymptote does not support sessions"))
88 (defun org-babel-asymptote-var-to-asymptote (pair)
89 "Convert an elisp val into a string of asymptote code specifying a var
90 of the same value."
91 (let ((var (car pair))
92 (val (if (symbolp (cdr pair))
93 (symbol-name (cdr pair))
94 (cdr pair))))
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 ((listp val)
103 (let* ((dimension-2-p (not (null (cdr val))))
104 (dim (if dimension-2-p "[][]" "[]"))
105 (type (org-babel-asymptote-define-type val))
106 (array (org-babel-asymptote-table-to-array
108 (if dimension-2-p '(:lstart "{" :lend "}," :llend "}")))))
109 (format "%S%s %S=%s;" type dim var array))))))
111 (defun org-babel-asymptote-table-to-array (table params)
112 "Convert values of an elisp table into a string of an asymptote array.
113 Empty cells are ignored."
114 (labels ((atom-to-string (table)
115 (cond
116 ((null table) '())
117 ((not (listp (car table)))
118 (cons (if (and (stringp (car table))
119 (not (string= (car table) "")))
120 (format "\"%s\"" (car table))
121 (format "%s" (car table)))
122 (atom-to-string (cdr table))))
124 (cons (atom-to-string (car table))
125 (atom-to-string (cdr table))))))
126 ;; Remove any empty row
127 (fix-empty-lines (table)
128 (delq nil (mapcar (lambda (l) (delq "" l)) table))))
129 (orgtbl-to-generic
130 (fix-empty-lines (atom-to-string table))
131 (org-combine-plists '(:hline nil :sep "," :tstart "{" :tend "}") params))))
133 (defun org-babel-asymptote-define-type (data)
134 "Determine type of DATA. DATA is a list. Type symbol is
135 returned as 'symbol. The type is usually the type of the first
136 atom encountered, except for arrays of int where every cell must
137 be of int type."
138 (labels ((anything-but-int (el)
139 (cond
140 ((null el) nil)
141 ((not (listp (car el)))
142 (cond
143 ((floatp (car el)) 'real)
144 ((stringp (car el)) 'string)
146 (anything-but-int (cdr el)))))
148 (or (anything-but-int (car el))
149 (anything-but-int (cdr el)))))))
150 (or (anything-but-int data) 'int)))
152 (provide 'ob-asymptote)
153 ;;; ob-asymptote.el ends here