1 ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
3 ;; Copyright (C) 2009-2012 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
)
90 (add-to-body (lambda (text) (setq body
(concat text
"\n" body
))))
92 ;; append header argument settings to body
93 (when title
(funcall add-to-body
(format "set title '%s'" title
))) ;; title
94 (when lines
(mapc (lambda (el) (funcall add-to-body el
)) lines
)) ;; line
96 (mapc (lambda (el) (funcall add-to-body
(format "set %s" el
))) sets
))
99 (format "set xtics (%s)"
100 (mapconcat (lambda (pair)
101 (format "\"%s\" %d" (cdr pair
) (car pair
)))
105 (format "set ytics (%s)"
106 (mapconcat (lambda (pair)
107 (format "\"%s\" %d" (cdr pair
) (car pair
)))
110 (funcall add-to-body
"set xdata time")
111 (funcall add-to-body
(concat "set timefmt \""
113 "%Y-%m-%d-%H:%M:%S") "\"")))
114 (when out-file
(funcall add-to-body
(format "set output \"%s\"" out-file
)))
115 (when term
(funcall add-to-body
(format "set term %s" term
)))
116 ;; insert variables into code body: this should happen last
117 ;; placing the variables at the *top* of the code in case their
118 ;; values are used later
119 (funcall add-to-body
(mapconcat #'identity
120 (org-babel-variable-assignments:gnuplot params
)
122 ;; replace any variable names preceded by '$' with the actual
123 ;; value of the variable
125 (setq body
(replace-regexp-in-string
126 (format "\\$%s" (car pair
)) (cdr pair
) body
)))
130 (defun org-babel-execute:gnuplot
(body params
)
131 "Execute a block of Gnuplot code.
132 This function is called by `org-babel-execute-src-block'."
134 (let ((session (cdr (assoc :session params
)))
135 (result-type (cdr (assoc :results params
)))
136 (out-file (cdr (assoc :file params
)))
137 (body (org-babel-expand-body:gnuplot body params
))
139 (save-window-excursion
140 ;; evaluate the code body with gnuplot
141 (if (string= session
"none")
142 (let ((script-file (org-babel-temp-file "gnuplot-script-")))
143 (with-temp-file script-file
144 (insert (concat body
"\n")))
145 (message "gnuplot \"%s\"" script-file
)
147 (shell-command-to-string
150 (org-babel-process-file-name
152 (if (member system-type
'(cygwin windows-nt ms-dos
))
156 (insert (concat body
"\n"))
158 (gnuplot-send-buffer-to-gnuplot)))
159 (if (member "output" (split-string result-type
))
161 nil
)))) ;; signal that output has already been written to file
163 (defun org-babel-prep-session:gnuplot
(session params
)
164 "Prepare SESSION according to the header arguments in PARAMS."
165 (let* ((session (org-babel-gnuplot-initiate-session session
))
166 (var-lines (org-babel-variable-assignments:gnuplot params
)))
167 (message "%S" session
)
168 (org-babel-comint-in-buffer session
169 (mapc (lambda (var-line)
170 (insert var-line
) (comint-send-input nil t
)
171 (org-babel-comint-wait-for-output session
)
172 (sit-for .1) (goto-char (point-max))) var-lines
))
175 (defun org-babel-load-session:gnuplot
(session body params
)
176 "Load BODY into SESSION."
177 (save-window-excursion
178 (let ((buffer (org-babel-prep-session:gnuplot session params
)))
179 (with-current-buffer buffer
180 (goto-char (process-mark (get-buffer-process (current-buffer))))
181 (insert (org-babel-chomp body
)))
184 (defun org-babel-variable-assignments:gnuplot
(params)
185 "Return list of gnuplot statements assigning the block's variables."
187 (lambda (pair) (format "%s = \"%s\"" (car pair
) (cdr pair
)))
188 (org-babel-gnuplot-process-vars params
)))
190 (defvar gnuplot-buffer
)
191 (defun org-babel-gnuplot-initiate-session (&optional session params
)
192 "Initiate a gnuplot session.
193 If there is not a current inferior-process-buffer in SESSION
194 then create one. Return the initialized session. The current
195 `gnuplot-mode' doesn't provide support for multiple sessions."
197 (unless (string= session
"none")
198 (save-window-excursion
199 (gnuplot-send-string-to-gnuplot "" "line")
202 (defun org-babel-gnuplot-quote-timestamp-field (s)
203 "Convert S from timestamp to Unix time and export to gnuplot."
204 (format-time-string org-babel-gnuplot-timestamp-fmt
(org-time-string-to-time s
)))
206 (defvar org-table-number-regexp
)
207 (defvar org-ts-regexp3
)
208 (defun org-babel-gnuplot-quote-tsv-field (s)
209 "Quote S for export to gnuplot."
211 (setq s
(format "%s" s
)))
212 (if (string-match org-table-number-regexp s
) s
213 (if (string-match org-ts-regexp3 s
)
214 (org-babel-gnuplot-quote-timestamp-field s
)
215 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"") "\""))))
217 (defun org-babel-gnuplot-table-to-data (table data-file params
)
218 "Export TABLE to DATA-FILE in a format readable by gnuplot.
219 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
220 (with-temp-file data-file
221 (make-local-variable 'org-babel-gnuplot-timestamp-fmt
)
222 (setq org-babel-gnuplot-timestamp-fmt
(or
223 (plist-get params
:timefmt
)
224 "%Y-%m-%d-%H:%M:%S"))
225 (insert (orgtbl-to-generic
228 '(:sep
"\t" :fmt org-babel-gnuplot-quote-tsv-field
)
232 (provide 'ob-gnuplot
)
236 ;;; ob-gnuplot.el ends here