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