use org-babel-result-cond in JavaScript code blocks
[org-mode.git] / lisp / ob-gnuplot.el
blob4b3a1c60e98c65cb0f4d6ee65be3f594912efe72
1 ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
3 ;; Copyright (C) 2009-2013 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 gnuplot source code.
28 ;; This differs from most standard languages in that
30 ;; 1) we are generally only going to return results of type "file"
32 ;; 2) we are adding the "file" and "cmdline" header arguments
34 ;;; Requirements:
36 ;; - gnuplot :: http://www.gnuplot.info/
38 ;; - gnuplot-mode :: http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html
40 ;;; Code:
41 (require 'ob)
42 (eval-when-compile (require 'cl))
44 (declare-function org-time-string-to-time "org" (s))
45 (declare-function org-combine-plists "org" (&rest plists))
46 (declare-function orgtbl-to-generic "org-table" (table params))
47 (declare-function gnuplot-mode "ext:gnuplot-mode" ())
48 (declare-function gnuplot-send-string-to-gnuplot "ext:gnuplot-mode" (str txt))
49 (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot-mode" ())
51 (defvar org-babel-default-header-args:gnuplot
52 '((:results . "file") (:exports . "results") (:session . nil))
53 "Default arguments to use when evaluating a gnuplot source block.")
55 (defvar org-babel-gnuplot-timestamp-fmt nil)
57 (defun org-babel-gnuplot-process-vars (params)
58 "Extract variables from PARAMS and process the variables.
59 Dumps all vectors into files and returns an association list
60 of variable names and the related value to be used in the gnuplot
61 code."
62 (mapcar
63 (lambda (pair)
64 (cons
65 (car pair) ;; variable name
66 (if (listp (cdr pair)) ;; variable value
67 (org-babel-gnuplot-table-to-data
68 (cdr pair) (org-babel-temp-file "gnuplot-") params)
69 (cdr pair))))
70 (mapcar #'cdr (org-babel-get-header params :var))))
72 (defun org-babel-expand-body:gnuplot (body params)
73 "Expand BODY according to PARAMS, return the expanded body."
74 (save-window-excursion
75 (let* ((vars (org-babel-gnuplot-process-vars params))
76 (out-file (cdr (assoc :file params)))
77 (term (or (cdr (assoc :term params))
78 (when out-file (file-name-extension out-file))))
79 (cmdline (cdr (assoc :cmdline params)))
80 (title (plist-get params :title))
81 (lines (plist-get params :line))
82 (sets (plist-get params :set))
83 (x-labels (plist-get params :xlabels))
84 (y-labels (plist-get params :ylabels))
85 (timefmt (plist-get params :timefmt))
86 (time-ind (or (plist-get params :timeind)
87 (when timefmt 1)))
88 (add-to-body (lambda (text) (setq body (concat text "\n" body))))
89 output)
90 ;; append header argument settings to body
91 (when title (funcall add-to-body (format "set title '%s'" title))) ;; title
92 (when lines (mapc (lambda (el) (funcall add-to-body el)) lines)) ;; line
93 (when sets
94 (mapc (lambda (el) (funcall add-to-body (format "set %s" el))) sets))
95 (when x-labels
96 (funcall add-to-body
97 (format "set xtics (%s)"
98 (mapconcat (lambda (pair)
99 (format "\"%s\" %d" (cdr pair) (car pair)))
100 x-labels ", "))))
101 (when y-labels
102 (funcall add-to-body
103 (format "set ytics (%s)"
104 (mapconcat (lambda (pair)
105 (format "\"%s\" %d" (cdr pair) (car pair)))
106 y-labels ", "))))
107 (when time-ind
108 (funcall add-to-body "set xdata time")
109 (funcall add-to-body (concat "set timefmt \""
110 (or timefmt
111 "%Y-%m-%d-%H:%M:%S") "\"")))
112 (when out-file (funcall add-to-body (format "set output \"%s\"" out-file)))
113 (when term (funcall add-to-body (format "set term %s" term)))
114 ;; insert variables into code body: this should happen last
115 ;; placing the variables at the *top* of the code in case their
116 ;; values are used later
117 (funcall add-to-body (mapconcat #'identity
118 (org-babel-variable-assignments:gnuplot params)
119 "\n"))
120 ;; replace any variable names preceded by '$' with the actual
121 ;; value of the variable
122 (mapc (lambda (pair)
123 (setq body (replace-regexp-in-string
124 (format "\\$%s" (car pair)) (cdr pair) body)))
125 vars))
126 body))
128 (defun org-babel-execute:gnuplot (body params)
129 "Execute a block of Gnuplot code.
130 This function is called by `org-babel-execute-src-block'."
131 (require 'gnuplot)
132 (let ((session (cdr (assoc :session params)))
133 (result-type (cdr (assoc :results params)))
134 (out-file (cdr (assoc :file params)))
135 (body (org-babel-expand-body:gnuplot body params))
136 output)
137 (save-window-excursion
138 ;; evaluate the code body with gnuplot
139 (if (string= session "none")
140 (let ((script-file (org-babel-temp-file "gnuplot-script-")))
141 (with-temp-file script-file
142 (insert (concat body "\n")))
143 (message "gnuplot \"%s\"" script-file)
144 (setq output
145 (shell-command-to-string
146 (format
147 "gnuplot \"%s\""
148 (org-babel-process-file-name
149 script-file
150 (if (member system-type '(cygwin windows-nt ms-dos))
151 t nil)))))
152 (message output))
153 (with-temp-buffer
154 (insert (concat body "\n"))
155 (gnuplot-mode)
156 (gnuplot-send-buffer-to-gnuplot)))
157 (if (member "output" (split-string result-type))
158 output
159 nil)))) ;; signal that output has already been written to file
161 (defun org-babel-prep-session:gnuplot (session params)
162 "Prepare SESSION according to the header arguments in PARAMS."
163 (let* ((session (org-babel-gnuplot-initiate-session session))
164 (var-lines (org-babel-variable-assignments:gnuplot params)))
165 (message "%S" session)
166 (org-babel-comint-in-buffer session
167 (mapc (lambda (var-line)
168 (insert var-line) (comint-send-input nil t)
169 (org-babel-comint-wait-for-output session)
170 (sit-for .1) (goto-char (point-max))) var-lines))
171 session))
173 (defun org-babel-load-session:gnuplot (session body params)
174 "Load BODY into SESSION."
175 (save-window-excursion
176 (let ((buffer (org-babel-prep-session:gnuplot session params)))
177 (with-current-buffer buffer
178 (goto-char (process-mark (get-buffer-process (current-buffer))))
179 (insert (org-babel-chomp body)))
180 buffer)))
182 (defun org-babel-variable-assignments:gnuplot (params)
183 "Return list of gnuplot statements assigning the block's variables."
184 (mapcar
185 (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
186 (org-babel-gnuplot-process-vars params)))
188 (defvar gnuplot-buffer)
189 (defun org-babel-gnuplot-initiate-session (&optional session params)
190 "Initiate a gnuplot session.
191 If there is not a current inferior-process-buffer in SESSION
192 then create one. Return the initialized session. The current
193 `gnuplot-mode' doesn't provide support for multiple sessions."
194 (require 'gnuplot)
195 (unless (string= session "none")
196 (save-window-excursion
197 (gnuplot-send-string-to-gnuplot "" "line")
198 gnuplot-buffer)))
200 (defun org-babel-gnuplot-quote-timestamp-field (s)
201 "Convert S from timestamp to Unix time and export to gnuplot."
202 (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
204 (defvar org-table-number-regexp)
205 (defvar org-ts-regexp3)
206 (defun org-babel-gnuplot-quote-tsv-field (s)
207 "Quote S for export to gnuplot."
208 (unless (stringp s)
209 (setq s (format "%s" s)))
210 (if (string-match org-table-number-regexp s) s
211 (if (string-match org-ts-regexp3 s)
212 (org-babel-gnuplot-quote-timestamp-field s)
213 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
215 (defun org-babel-gnuplot-table-to-data (table data-file params)
216 "Export TABLE to DATA-FILE in a format readable by gnuplot.
217 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
218 (with-temp-file data-file
219 (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
220 (setq org-babel-gnuplot-timestamp-fmt (or
221 (plist-get params :timefmt)
222 "%Y-%m-%d-%H:%M:%S"))
223 (insert (orgtbl-to-generic
224 table
225 (org-combine-plists
226 '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
227 params))))
228 data-file)
230 (provide 'ob-gnuplot)
234 ;;; ob-gnuplot.el ends here