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/>.
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
36 ;; - gnuplot :: http://www.gnuplot.info/
38 ;; - gnuplot-mode :: http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html
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
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."
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
83 (let ((*org-babel-gnuplot-missing
* (cdr (assoc :missing params
))))
87 (car pair
) ;; variable name
88 (if (listp (cdr pair
)) ;; variable value
89 (org-babel-gnuplot-table-to-data
90 (cdr pair
) (org-babel-temp-file "gnuplot-") params
)
92 (mapcar #'cdr
(org-babel-get-header params
:var
)))))
94 (defun org-babel-expand-body:gnuplot
(body params
)
95 "Expand BODY according to PARAMS, return the expanded body."
96 (save-window-excursion
97 (let* ((vars (org-babel-gnuplot-process-vars params
))
98 (out-file (cdr (assoc :file params
)))
99 (prologue (cdr (assoc :prologue params
)))
100 (epilogue (cdr (assoc :epilogue params
)))
101 (term (or (cdr (assoc :term params
))
103 (let ((ext (file-name-extension out-file
)))
104 (or (cdr (assoc (intern (downcase ext
))
105 *org-babel-gnuplot-terms
*))
107 (cmdline (cdr (assoc :cmdline params
)))
108 (title (cdr (assoc :title params
)))
109 (lines (cdr (assoc :line params
)))
110 (sets (cdr (assoc :set params
)))
111 (x-labels (cdr (assoc :xlabels params
)))
112 (y-labels (cdr (assoc :ylabels params
)))
113 (timefmt (cdr (assoc :timefmt params
)))
114 (time-ind (or (cdr (assoc :timeind params
))
116 (add-to-body (lambda (text) (setq body
(concat text
"\n" body
))))
118 ;; append header argument settings to body
119 (when title
(funcall add-to-body
(format "set title '%s'" title
)))
120 (when lines
(mapc (lambda (el) (funcall add-to-body el
)) lines
))
122 (mapc (lambda (el) (funcall add-to-body
(format "set %s" el
))) sets
))
125 (format "set xtics (%s)"
126 (mapconcat (lambda (pair)
128 (cdr pair
) (car pair
)))
132 (format "set ytics (%s)"
133 (mapconcat (lambda (pair)
135 (cdr pair
) (car pair
)))
138 (funcall add-to-body
"set xdata time")
139 (funcall add-to-body
(concat "set timefmt \""
141 "%Y-%m-%d-%H:%M:%S") "\"")))
143 ;; set the terminal at the top of the block
144 (funcall add-to-body
(format "set output \"%s\"" out-file
))
145 ;; and close the terminal at the bottom of the block
146 (setq body
(concat body
"\nset output\n")))
147 (when term
(funcall add-to-body
(format "set term %s" term
)))
148 ;; insert variables into code body: this should happen last
149 ;; placing the variables at the *top* of the code in case their
150 ;; values are used later
152 (mapconcat #'identity
153 (org-babel-variable-assignments:gnuplot params
)
155 ;; replace any variable names preceded by '$' with the actual
156 ;; value of the variable
158 (setq body
(replace-regexp-in-string
159 (format "\\$%s" (car pair
)) (cdr pair
) body
)))
161 (when prologue
(funcall add-to-body prologue
))
162 (when epilogue
(setq body
(concat body
"\n" epilogue
))))
165 (defun org-babel-execute:gnuplot
(body params
)
166 "Execute a block of Gnuplot code.
167 This function is called by `org-babel-execute-src-block'."
169 (let ((session (cdr (assoc :session params
)))
170 (result-type (cdr (assoc :results params
)))
171 (out-file (cdr (assoc :file params
)))
172 (body (org-babel-expand-body:gnuplot body params
))
174 (save-window-excursion
175 ;; evaluate the code body with gnuplot
176 (if (string= session
"none")
177 (let ((script-file (org-babel-temp-file "gnuplot-script-")))
178 (with-temp-file script-file
179 (insert (concat body
"\n")))
180 (message "gnuplot \"%s\"" script-file
)
182 (shell-command-to-string
185 (org-babel-process-file-name
187 (if (member system-type
'(cygwin windows-nt ms-dos
))
191 (insert (concat body
"\n"))
193 (gnuplot-send-buffer-to-gnuplot)))
194 (if (member "output" (split-string result-type
))
196 nil
)))) ;; signal that output has already been written to file
198 (defun org-babel-prep-session:gnuplot
(session params
)
199 "Prepare SESSION according to the header arguments in PARAMS."
200 (let* ((session (org-babel-gnuplot-initiate-session session
))
201 (var-lines (org-babel-variable-assignments:gnuplot params
)))
202 (message "%S" session
)
203 (org-babel-comint-in-buffer session
204 (mapc (lambda (var-line)
205 (insert var-line
) (comint-send-input nil t
)
206 (org-babel-comint-wait-for-output session
)
207 (sit-for .1) (goto-char (point-max))) var-lines
))
210 (defun org-babel-load-session:gnuplot
(session body params
)
211 "Load BODY into SESSION."
212 (save-window-excursion
213 (let ((buffer (org-babel-prep-session:gnuplot session params
)))
214 (with-current-buffer buffer
215 (goto-char (process-mark (get-buffer-process (current-buffer))))
216 (insert (org-babel-chomp body
)))
219 (defun org-babel-variable-assignments:gnuplot
(params)
220 "Return list of gnuplot statements assigning the block's variables."
222 (lambda (pair) (format "%s = \"%s\"" (car pair
) (cdr pair
)))
223 (org-babel-gnuplot-process-vars params
)))
225 (defvar gnuplot-buffer
)
226 (defun org-babel-gnuplot-initiate-session (&optional session params
)
227 "Initiate a gnuplot session.
228 If there is not a current inferior-process-buffer in SESSION
229 then create one. Return the initialized session. The current
230 `gnuplot-mode' doesn't provide support for multiple sessions."
232 (unless (string= session
"none")
233 (save-window-excursion
234 (gnuplot-send-string-to-gnuplot "" "line")
237 (defun org-babel-gnuplot-quote-timestamp-field (s)
238 "Convert S from timestamp to Unix time and export to gnuplot."
239 (format-time-string org-babel-gnuplot-timestamp-fmt
240 (org-time-string-to-time s
)))
242 (defvar org-table-number-regexp
)
243 (defvar org-ts-regexp3
)
244 (defun org-babel-gnuplot-quote-tsv-field (s)
245 "Quote S for export to gnuplot."
247 (setq s
(format "%s" s
)))
248 (if (string-match org-table-number-regexp s
) s
249 (if (string-match org-ts-regexp3 s
)
250 (org-babel-gnuplot-quote-timestamp-field s
)
251 (if (zerop (length s
))
252 (or *org-babel-gnuplot-missing
* s
)
253 (if (string-match "[ \"]" "?")
254 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"")
258 (defun org-babel-gnuplot-table-to-data (table data-file params
)
259 "Export TABLE to DATA-FILE in a format readable by gnuplot.
260 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
261 (with-temp-file data-file
262 (make-local-variable 'org-babel-gnuplot-timestamp-fmt
)
263 (setq org-babel-gnuplot-timestamp-fmt
(or
264 (plist-get params
:timefmt
)
265 "%Y-%m-%d-%H:%M:%S"))
266 (insert (orgtbl-to-generic
269 '(:sep
"\t" :fmt org-babel-gnuplot-quote-tsv-field
)
273 (provide 'ob-gnuplot
)
277 ;;; ob-gnuplot.el ends here