Babel C, C++, D support for non-homogeneous input tables
[org-mode.git] / lisp / ob-gnuplot.el
blob7d7db2a5bb3946750f960987623b9264c9e4ab32
1 ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
3 ;; Copyright (C) 2009-2014 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-header-args:gnuplot
56 '((title . :any)
57 (lines . :any)
58 (sets . :any)
59 (x-labels . :any)
60 (y-labels . :any)
61 (timefmt . :any)
62 (time-ind . :any)
63 (missing . :any)
64 (term . :any))
65 "Gnuplot specific header args.")
67 (defvar org-babel-gnuplot-timestamp-fmt nil)
69 (defvar *org-babel-gnuplot-missing* nil)
71 (defcustom *org-babel-gnuplot-terms*
72 '((eps . "postscript eps"))
73 "List of file extensions and the associated gnuplot terminal."
74 :group 'org-babel
75 :type '(repeat (cons (symbol :tag "File extension")
76 (string :tag "Gnuplot terminal"))))
78 (defun org-babel-gnuplot-process-vars (params)
79 "Extract variables from PARAMS and process the variables.
80 Dumps all vectors into files and returns an association list
81 of variable names and the related value to be used in the gnuplot
82 code."
83 (let ((*org-babel-gnuplot-missing* (cdr (assoc :missing params))))
84 (mapcar
85 (lambda (pair)
86 (cons
87 (car pair) ;; variable name
88 (let* ((val (cdr pair)) ;; variable value
89 (lp (listp val)))
90 (if lp
91 (org-babel-gnuplot-table-to-data
92 (let* ((first (car val))
93 (tablep (or (listp first) (symbolp first))))
94 (if tablep val (mapcar 'list val)))
95 (org-babel-temp-file "gnuplot-") params)
96 val))))
97 (mapcar #'cdr (org-babel-get-header params :var)))))
99 (defun org-babel-expand-body:gnuplot (body params)
100 "Expand BODY according to PARAMS, return the expanded body."
101 (save-window-excursion
102 (let* ((vars (org-babel-gnuplot-process-vars params))
103 (out-file (cdr (assoc :file params)))
104 (prologue (cdr (assoc :prologue params)))
105 (epilogue (cdr (assoc :epilogue params)))
106 (term (or (cdr (assoc :term params))
107 (when out-file
108 (let ((ext (file-name-extension out-file)))
109 (or (cdr (assoc (intern (downcase ext))
110 *org-babel-gnuplot-terms*))
111 ext)))))
112 (cmdline (cdr (assoc :cmdline params)))
113 (title (cdr (assoc :title params)))
114 (lines (cdr (assoc :line params)))
115 (sets (cdr (assoc :set params)))
116 (x-labels (cdr (assoc :xlabels params)))
117 (y-labels (cdr (assoc :ylabels params)))
118 (timefmt (cdr (assoc :timefmt params)))
119 (time-ind (or (cdr (assoc :timeind params))
120 (when timefmt 1)))
121 (add-to-body (lambda (text) (setq body (concat text "\n" body))))
122 output)
123 ;; append header argument settings to body
124 (when title (funcall add-to-body (format "set title '%s'" title)))
125 (when lines (mapc (lambda (el) (funcall add-to-body el)) lines))
126 (when sets
127 (mapc (lambda (el) (funcall add-to-body (format "set %s" el))) sets))
128 (when x-labels
129 (funcall add-to-body
130 (format "set xtics (%s)"
131 (mapconcat (lambda (pair)
132 (format "\"%s\" %d"
133 (cdr pair) (car pair)))
134 x-labels ", "))))
135 (when y-labels
136 (funcall add-to-body
137 (format "set ytics (%s)"
138 (mapconcat (lambda (pair)
139 (format "\"%s\" %d"
140 (cdr pair) (car pair)))
141 y-labels ", "))))
142 (when time-ind
143 (funcall add-to-body "set xdata time")
144 (funcall add-to-body (concat "set timefmt \""
145 (or timefmt
146 "%Y-%m-%d-%H:%M:%S") "\"")))
147 (when out-file
148 ;; set the terminal at the top of the block
149 (funcall add-to-body (format "set output \"%s\"" out-file))
150 ;; and close the terminal at the bottom of the block
151 (setq body (concat body "\nset output\n")))
152 (when term (funcall add-to-body (format "set term %s" term)))
153 ;; insert variables into code body: this should happen last
154 ;; placing the variables at the *top* of the code in case their
155 ;; values are used later
156 (funcall add-to-body
157 (mapconcat #'identity
158 (org-babel-variable-assignments:gnuplot params)
159 "\n"))
160 ;; replace any variable names preceded by '$' with the actual
161 ;; value of the variable
162 (mapc (lambda (pair)
163 (setq body (replace-regexp-in-string
164 (format "\\$%s" (car pair)) (cdr pair) body)))
165 vars)
166 (when prologue (funcall add-to-body prologue))
167 (when epilogue (setq body (concat body "\n" epilogue))))
168 body))
170 (defun org-babel-execute:gnuplot (body params)
171 "Execute a block of Gnuplot code.
172 This function is called by `org-babel-execute-src-block'."
173 (require 'gnuplot)
174 (let ((session (cdr (assoc :session params)))
175 (result-type (cdr (assoc :results params)))
176 (out-file (cdr (assoc :file params)))
177 (body (org-babel-expand-body:gnuplot body params))
178 output)
179 (save-window-excursion
180 ;; evaluate the code body with gnuplot
181 (if (string= session "none")
182 (let ((script-file (org-babel-temp-file "gnuplot-script-")))
183 (with-temp-file script-file
184 (insert (concat body "\n")))
185 (message "gnuplot \"%s\"" script-file)
186 (setq output
187 (shell-command-to-string
188 (format
189 "gnuplot \"%s\""
190 (org-babel-process-file-name
191 script-file
192 (if (member system-type '(cygwin windows-nt ms-dos))
193 t nil)))))
194 (message output))
195 (with-temp-buffer
196 (insert (concat body "\n"))
197 (gnuplot-mode)
198 (gnuplot-send-buffer-to-gnuplot)))
199 (if (member "output" (split-string result-type))
200 output
201 nil)))) ;; signal that output has already been written to file
203 (defun org-babel-prep-session:gnuplot (session params)
204 "Prepare SESSION according to the header arguments in PARAMS."
205 (let* ((session (org-babel-gnuplot-initiate-session session))
206 (var-lines (org-babel-variable-assignments:gnuplot params)))
207 (message "%S" session)
208 (org-babel-comint-in-buffer session
209 (mapc (lambda (var-line)
210 (insert var-line) (comint-send-input nil t)
211 (org-babel-comint-wait-for-output session)
212 (sit-for .1) (goto-char (point-max))) var-lines))
213 session))
215 (defun org-babel-load-session:gnuplot (session body params)
216 "Load BODY into SESSION."
217 (save-window-excursion
218 (let ((buffer (org-babel-prep-session:gnuplot session params)))
219 (with-current-buffer buffer
220 (goto-char (process-mark (get-buffer-process (current-buffer))))
221 (insert (org-babel-chomp body)))
222 buffer)))
224 (defun org-babel-variable-assignments:gnuplot (params)
225 "Return list of gnuplot statements assigning the block's variables."
226 (mapcar
227 (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
228 (org-babel-gnuplot-process-vars params)))
230 (defvar gnuplot-buffer)
231 (defun org-babel-gnuplot-initiate-session (&optional session params)
232 "Initiate a gnuplot session.
233 If there is not a current inferior-process-buffer in SESSION
234 then create one. Return the initialized session. The current
235 `gnuplot-mode' doesn't provide support for multiple sessions."
236 (require 'gnuplot)
237 (unless (string= session "none")
238 (save-window-excursion
239 (gnuplot-send-string-to-gnuplot "" "line")
240 gnuplot-buffer)))
242 (defun org-babel-gnuplot-quote-timestamp-field (s)
243 "Convert S from timestamp to Unix time and export to gnuplot."
244 (format-time-string org-babel-gnuplot-timestamp-fmt
245 (org-time-string-to-time s)))
247 (defvar org-table-number-regexp)
248 (defvar org-ts-regexp3)
249 (defun org-babel-gnuplot-quote-tsv-field (s)
250 "Quote S for export to gnuplot."
251 (unless (stringp s)
252 (setq s (format "%s" s)))
253 (if (string-match org-table-number-regexp s) s
254 (if (string-match org-ts-regexp3 s)
255 (org-babel-gnuplot-quote-timestamp-field s)
256 (if (zerop (length s))
257 (or *org-babel-gnuplot-missing* s)
258 (if (string-match "[ \"]" s)
259 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"")
260 "\"")
261 s)))))
263 (defun org-babel-gnuplot-table-to-data (table data-file params)
264 "Export TABLE to DATA-FILE in a format readable by gnuplot.
265 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
266 (with-temp-file data-file
267 (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
268 (setq org-babel-gnuplot-timestamp-fmt (or
269 (plist-get params :timefmt)
270 "%Y-%m-%d-%H:%M:%S"))
271 (insert (orgtbl-to-generic
272 table
273 (org-combine-plists
274 '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
275 params))))
276 data-file)
278 (provide 'ob-gnuplot)
282 ;;; ob-gnuplot.el ends here