adding defgroup for org-babel
[org-mode.git] / lisp / ob-asymptote.el
blobda11fe440559526fb77bdc665cef6ff754a1faa0
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: 0.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 to use when evaluating a 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 with org-babel. This function is
67 called by `org-babel-execute-src-block'."
68 (message "executing Asymptote source code block")
69 (let* ((processed-params (org-babel-process-params params))
70 (result-params (split-string (or (cdr (assoc :results params)) "")))
71 (out-file (cdr (assoc :file params)))
72 (format (or (and out-file
73 (string-match ".+\\.\\(.+\\)" out-file)
74 (match-string 1 out-file))
75 "pdf"))
76 (cmdline (cdr (assoc :cmdline params)))
77 (in-file (make-temp-file "org-babel-asymptote"))
78 (cmd (concat "asy "
79 (if out-file
80 (concat "-globalwrite -f " format " -o " out-file)
81 "-V")
82 " " cmdline " " in-file)))
83 (with-temp-file in-file
84 (insert (org-babel-expand-body:asymptote body params processed-params)))
85 (message cmd) (shell-command cmd)
86 out-file))
88 (defun org-babel-prep-session:asymptote (session params)
89 "Prepare a session named SESSION according to PARAMS."
90 (error "Asymptote does not support sessions"))
92 (defun org-babel-asymptote-var-to-asymptote (pair)
93 "Convert an elisp val into a string of asymptote code specifying a var
94 of the same value."
95 (let ((var (car pair))
96 (val (if (symbolp (cdr pair))
97 (symbol-name (cdr pair))
98 (cdr pair))))
99 (cond
100 ((integerp val)
101 (format "int %S=%S;" var val))
102 ((floatp val)
103 (format "real %S=%S;" var val))
104 ((stringp val)
105 (format "string %S=\"%s\";" var val))
106 ((listp val)
107 (let* ((dimension-2-p (not (null (cdr val))))
108 (dim (if dimension-2-p "[][]" "[]"))
109 (type (org-babel-asymptote-define-type val))
110 (array (org-babel-asymptote-table-to-array
112 (if dimension-2-p '(:lstart "{" :lend "}," :llend "}")))))
113 (format "%S%s %S=%s;" type dim var array))))))
115 (defun org-babel-asymptote-table-to-array (table params)
116 "Convert values of an elisp table into a string of an asymptote array.
117 Empty cells are ignored."
118 (labels ((atom-to-string (table)
119 (cond
120 ((null table) '())
121 ((not (listp (car table)))
122 (cons (if (and (stringp (car table))
123 (not (string= (car table) "")))
124 (format "\"%s\"" (car table))
125 (format "%s" (car table)))
126 (atom-to-string (cdr table))))
128 (cons (atom-to-string (car table))
129 (atom-to-string (cdr table))))))
130 ;; Remove any empty row
131 (fix-empty-lines (table)
132 (delq nil (mapcar (lambda (l) (delq "" l)) table))))
133 (orgtbl-to-generic
134 (fix-empty-lines (atom-to-string table))
135 (org-combine-plists '(:hline nil :sep "," :tstart "{" :tend "}") params))))
137 (defun org-babel-asymptote-define-type (data)
138 "Determine type of DATA. DATA is a list. Type symbol is
139 returned as 'symbol. The type is usually the type of the first
140 atom encountered, except for arrays of int where every cell must
141 be of int type."
142 (labels ((anything-but-int (el)
143 (cond
144 ((null el) nil)
145 ((not (listp (car el)))
146 (cond
147 ((floatp (car el)) 'real)
148 ((stringp (car el)) 'string)
150 (anything-but-int (cdr el)))))
152 (or (anything-but-int (car el))
153 (anything-but-int (cdr el)))))))
154 (or (anything-but-int data) 'int)))
156 (provide 'ob-asymptote)
158 ;; arch-tag: f2f5bd0d-78e8-412b-8e6c-6dadc94cc06b
160 ;;; ob-asymptote.el ends here