1 ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
3 ;; Copyright (C) 2009-2011 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
44 (eval-when-compile (require 'cl
))
46 (declare-function org-time-string-to-time
"org" (s))
47 (declare-function org-combine-plists
"org" (&rest plists
))
48 (declare-function orgtbl-to-generic
"org-table" (table params
))
49 (declare-function gnuplot-mode
"ext:gnuplot-mode" ())
50 (declare-function gnuplot-send-string-to-gnuplot
"ext:gnuplot-mode" (str txt
))
51 (declare-function gnuplot-send-buffer-to-gnuplot
"ext:gnuplot-mode" ())
53 (defvar org-babel-default-header-args
:gnuplot
54 '((:results .
"file") (:exports .
"results") (:session . nil
))
55 "Default arguments to use when evaluating a gnuplot source block.")
57 (defvar org-babel-gnuplot-timestamp-fmt nil
)
59 (defun org-babel-gnuplot-process-vars (params)
60 "Extract variables from PARAMS and process the variables.
61 Dumps all vectors into files and returns an association list
62 of variable names and the related value to be used in the gnuplot
67 (car pair
) ;; variable name
68 (if (listp (cdr pair
)) ;; variable value
69 (org-babel-gnuplot-table-to-data
70 (cdr pair
) (org-babel-temp-file "gnuplot-") params
)
72 (mapcar #'cdr
(org-babel-get-header params
:var
))))
74 (defun org-babel-expand-body:gnuplot
(body params
)
75 "Expand BODY according to PARAMS, return the expanded body."
76 (save-window-excursion
77 (let* ((vars (org-babel-gnuplot-process-vars params
))
78 (out-file (cdr (assoc :file params
)))
79 (term (or (cdr (assoc :term params
))
80 (when out-file
(file-name-extension out-file
))))
81 (cmdline (cdr (assoc :cmdline params
)))
82 (title (plist-get params
:title
))
83 (lines (plist-get params
:line
))
84 (sets (plist-get params
:set
))
85 (x-labels (plist-get params
:xlabels
))
86 (y-labels (plist-get params
:ylabels
))
87 (timefmt (plist-get params
:timefmt
))
88 (time-ind (or (plist-get params
:timeind
)
91 (flet ((add-to-body (text)
92 (setq body
(concat text
"\n" body
))))
93 ;; append header argument settings to body
94 (when title
(add-to-body (format "set title '%s'" title
))) ;; title
95 (when lines
(mapc (lambda (el) (add-to-body el
)) lines
)) ;; line
97 (mapc (lambda (el) (add-to-body (format "set %s" el
))) sets
))
100 (format "set xtics (%s)"
101 (mapconcat (lambda (pair)
102 (format "\"%s\" %d" (cdr pair
) (car pair
)))
106 (format "set ytics (%s)"
107 (mapconcat (lambda (pair)
108 (format "\"%s\" %d" (cdr pair
) (car pair
)))
111 (add-to-body "set xdata time")
112 (add-to-body (concat "set timefmt \""
114 "%Y-%m-%d-%H:%M:%S") "\"")))
115 (when out-file
(add-to-body (format "set output \"%s\"" out-file
)))
116 (when term
(add-to-body (format "set term %s" term
)))
117 ;; insert variables into code body: this should happen last
118 ;; placing the variables at the *top* of the code in case their
119 ;; values are used later
120 (add-to-body (mapconcat #'identity
121 (org-babel-variable-assignments:gnuplot params
)
123 ;; replace any variable names preceded by '$' with the actual
124 ;; value of the variable
126 (setq body
(replace-regexp-in-string
127 (format "\\$%s" (car pair
)) (cdr pair
) body
)))
131 (defun org-babel-execute:gnuplot
(body params
)
132 "Execute a block of Gnuplot code.
133 This function is called by `org-babel-execute-src-block'."
135 (let ((session (cdr (assoc :session params
)))
136 (result-type (cdr (assoc :results params
)))
137 (out-file (cdr (assoc :file params
)))
138 (body (org-babel-expand-body:gnuplot body params
))
140 (save-window-excursion
141 ;; evaluate the code body with gnuplot
142 (if (string= session
"none")
143 (let ((script-file (org-babel-temp-file "gnuplot-script-")))
144 (with-temp-file script-file
145 (insert (concat body
"\n")))
146 (message "gnuplot \"%s\"" script-file
)
148 (shell-command-to-string
151 (org-babel-process-file-name
153 (if (member system-type
'(cygwin windows-nt ms-dos
))
157 (insert (concat body
"\n"))
159 (gnuplot-send-buffer-to-gnuplot)))
160 (if (member "output" (split-string result-type
))
162 nil
)))) ;; signal that output has already been written to file
164 (defun org-babel-prep-session:gnuplot
(session params
)
165 "Prepare SESSION according to the header arguments in PARAMS."
166 (let* ((session (org-babel-gnuplot-initiate-session session
))
167 (var-lines (org-babel-variable-assignments:gnuplot params
)))
168 (message "%S" session
)
169 (org-babel-comint-in-buffer session
170 (mapc (lambda (var-line)
171 (insert var-line
) (comint-send-input nil t
)
172 (org-babel-comint-wait-for-output session
)
173 (sit-for .1) (goto-char (point-max))) var-lines
))
176 (defun org-babel-load-session:gnuplot
(session body params
)
177 "Load BODY into SESSION."
178 (save-window-excursion
179 (let ((buffer (org-babel-prep-session:gnuplot session params
)))
180 (with-current-buffer buffer
181 (goto-char (process-mark (get-buffer-process (current-buffer))))
182 (insert (org-babel-chomp body
)))
185 (defun org-babel-variable-assignments:gnuplot
(params)
186 "Return list of gnuplot statements assigning the block's variables"
188 (lambda (pair) (format "%s = \"%s\"" (car pair
) (cdr pair
)))
189 (org-babel-gnuplot-process-vars params
)))
191 (defvar gnuplot-buffer
)
192 (defun org-babel-gnuplot-initiate-session (&optional session params
)
193 "Initiate a gnuplot session.
194 If there is not a current inferior-process-buffer in SESSION
195 then create one. Return the initialized session. The current
196 `gnuplot-mode' doesn't provide support for multiple sessions."
198 (unless (string= session
"none")
199 (save-window-excursion
200 (gnuplot-send-string-to-gnuplot "" "line")
203 (defun org-babel-gnuplot-quote-timestamp-field (s)
204 "Convert S from timestamp to Unix time and export to gnuplot."
205 (format-time-string org-babel-gnuplot-timestamp-fmt
(org-time-string-to-time s
)))
207 (defvar org-table-number-regexp
)
208 (defvar org-ts-regexp3
)
209 (defun org-babel-gnuplot-quote-tsv-field (s)
210 "Quote S for export to gnuplot."
212 (setq s
(format "%s" s
)))
213 (if (string-match org-table-number-regexp s
) s
214 (if (string-match org-ts-regexp3 s
)
215 (org-babel-gnuplot-quote-timestamp-field s
)
216 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"") "\""))))
218 (defun org-babel-gnuplot-table-to-data (table data-file params
)
219 "Export TABLE to DATA-FILE in a format readable by gnuplot.
220 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
221 (with-temp-file data-file
222 (make-local-variable 'org-babel-gnuplot-timestamp-fmt
)
223 (setq org-babel-gnuplot-timestamp-fmt
(or
224 (plist-get params
:timefmt
)
225 "%Y-%m-%d-%H:%M:%S"))
226 (insert (orgtbl-to-generic
229 '(:sep
"\t" :fmt org-babel-gnuplot-quote-tsv-field
)
233 (provide 'ob-gnuplot
)
237 ;;; ob-gnuplot.el ends here