org-e-latex: Change syntax for images attributes
[org-mode.git] / lisp / ob-maxima.el
blob7e5a5562ad8a19e8b9ddb32eced9f022b1f32098
1 ;;; ob-maxima.el --- org-babel functions for maxima evaluation
3 ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
5 ;; Author: Eric S Fraga
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research, maxima
8 ;; Homepage: http://orgmode.org
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 maxima entries.
29 ;; This differs from most standard languages in that
31 ;; 1) there is no such thing as a "session" in maxima
33 ;; 2) we are adding the "cmdline" header argument
35 ;;; Code:
36 (require 'ob)
38 (defvar org-babel-tangle-lang-exts)
39 (add-to-list 'org-babel-tangle-lang-exts '("maxima" . "max"))
41 (defvar org-babel-default-header-args:maxima '())
43 (defcustom org-babel-maxima-command
44 (if (boundp 'maxima-command) maxima-command "maxima")
45 "Command used to call maxima on the shell."
46 :group 'org-babel)
48 (defun org-babel-maxima-expand (body params)
49 "Expand a block of Maxima code according to its header arguments."
50 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
51 (mapconcat 'identity
52 (list
53 ;; graphic output
54 (let ((graphic-file (org-babel-maxima-graphical-output-file params)))
55 (if graphic-file
56 (format
57 "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);"
58 graphic-file)
59 ""))
60 ;; variables
61 (mapconcat 'org-babel-maxima-var-to-maxima vars "\n")
62 ;; body
63 body
64 "gnuplot_close ()$")
65 "\n")))
67 (defun org-babel-execute:maxima (body params)
68 "Execute a block of Maxima entries with org-babel. This function is
69 called by `org-babel-execute-src-block'."
70 (message "executing Maxima source code block")
71 (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
72 (result
73 (let* ((cmdline (or (cdr (assoc :cmdline params)) ""))
74 (in-file (org-babel-temp-file "maxima-" ".max"))
75 (cmd (format "%s --very-quiet -r 'batchload(%S)$' %s"
76 org-babel-maxima-command in-file cmdline)))
77 (with-temp-file in-file (insert (org-babel-maxima-expand body params)))
78 (message cmd)
79 ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
80 (mapconcat
81 #'identity
82 (delq nil
83 (mapcar (lambda (line)
84 (unless (or (string-match "batch" line)
85 (string-match "^rat: replaced .*$" line)
86 (= 0 (length line)))
87 line))
88 (split-string raw "[\r\n]"))) "\n"))
89 (org-babel-eval cmd "")))))
90 (if (org-babel-maxima-graphical-output-file params)
91 nil
92 (org-babel-result-cond result-params
93 result
94 (let ((tmp-file (org-babel-temp-file "maxima-res-")))
95 (with-temp-file tmp-file (insert result))
96 (org-babel-import-elisp-from-file tmp-file))))))
99 (defun org-babel-prep-session:maxima (session params)
100 (error "Maxima does not support sessions"))
102 (defun org-babel-maxima-var-to-maxima (pair)
103 "Convert an elisp val into a string of maxima code specifying a var
104 of the same value."
105 (let ((var (car pair))
106 (val (cdr pair)))
107 (when (symbolp val)
108 (setq val (symbol-name val))
109 (when (= (length val) 1)
110 (setq val (string-to-char val))))
111 (format "%S: %s$" var
112 (org-babel-maxima-elisp-to-maxima val))))
114 (defun org-babel-maxima-graphical-output-file (params)
115 "Name of file to which maxima should send graphical output."
116 (and (member "graphics" (cdr (assq :result-params params)))
117 (cdr (assq :file params))))
119 (defun org-babel-maxima-elisp-to-maxima (val)
120 "Return a string of maxima code which evaluates to VAL."
121 (if (listp val)
122 (concat "[" (mapconcat #'org-babel-maxima-elisp-to-maxima val ", ") "]")
123 (format "%s" val)))
126 (provide 'ob-maxima)
130 ;;; ob-maxima.el ends here