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