1 ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Org-Babel support for evaluating gnuplot source code.
29 ;; This differs from most standard languages in that
31 ;; 1) we are generally only going to return results of type "file"
33 ;; 2) we are adding the "file" and "cmdline" header arguments
37 ;; - gnuplot :: http://www.gnuplot.info/
39 ;; - gnuplot-mode :: http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html
45 (eval-when-compile (require 'cl
))
47 (declare-function org-time-string-to-time
"org" (s))
48 (declare-function org-combine-plists
"org" (&rest plists
))
49 (declare-function orgtbl-to-generic
"org-table" (table params
))
50 (declare-function gnuplot-mode
"ext:gnuplot-mode" ())
51 (declare-function gnuplot-send-string-to-gnuplot
"ext:gnuplot-mode" (str txt
))
52 (declare-function gnuplot-send-buffer-to-gnuplot
"ext:gnuplot-mode" ())
54 (defvar org-babel-default-header-args
:gnuplot
55 '((:results .
"file") (:exports .
"results") (:session . nil
))
56 "Default arguments to use when evaluating a gnuplot source block.")
58 (defvar org-babel-gnuplot-timestamp-fmt nil
)
60 (defun org-babel-gnuplot-process-vars (params)
61 "Extract variables from PARAMS and process the variables
62 dumping all vectors into files and returning an association list
63 of variable names and the related value to be used in the gnuplot
68 (car pair
) ;; variable name
69 (if (listp (cdr pair
)) ;; variable value
70 (org-babel-gnuplot-table-to-data
71 (cdr pair
) (make-temp-file "org-babel-gnuplot") params
)
73 (org-babel-ref-variables params
)))
75 (defun org-babel-expand-body:gnuplot
(body params
&optional processed-params
)
76 "Expand BODY according to PARAMS, return the expanded body."
77 (save-window-excursion
78 (let* ((vars (org-babel-gnuplot-process-vars params
))
79 (out-file (cdr (assoc :file params
)))
80 (term (or (cdr (assoc :term params
))
81 (when out-file
(file-name-extension out-file
))))
82 (cmdline (cdr (assoc :cmdline params
)))
83 (title (plist-get params
:title
))
84 (lines (plist-get params
:line
))
85 (sets (plist-get params
:set
))
86 (x-labels (plist-get params
:xlabels
))
87 (y-labels (plist-get params
:ylabels
))
88 (timefmt (plist-get params
:timefmt
))
89 (time-ind (or (plist-get params
:timeind
)
92 (flet ((add-to-body (text)
93 (setq body
(concat text
"\n" body
))))
94 ;; append header argument settings to body
95 (when title
(add-to-body (format "set title '%s'" title
))) ;; title
96 (when lines
(mapc (lambda (el) (add-to-body el
)) lines
)) ;; line
98 (mapc (lambda (el) (add-to-body (format "set %s" el
))) sets
))
101 (format "set xtics (%s)"
102 (mapconcat (lambda (pair)
103 (format "\"%s\" %d" (cdr pair
) (car pair
)))
107 (format "set ytics (%s)"
108 (mapconcat (lambda (pair)
109 (format "\"%s\" %d" (cdr pair
) (car pair
)))
112 (add-to-body "set xdata time")
113 (add-to-body (concat "set timefmt \""
115 "%Y-%m-%d-%H:%M:%S") "\"")))
116 (when out-file
(add-to-body (format "set output \"%s\"" out-file
)))
117 (when term
(add-to-body (format "set term %s" term
)))
118 ;; insert variables into code body: this should happen last
119 ;; placing the variables at the *top* of the code in case their
120 ;; values are used later
121 (add-to-body (mapconcat
122 (lambda (pair) (format "%s = \"%s\"" (car pair
) (cdr pair
)))
124 ;; replace any variable names preceded by '$' with the actual
125 ;; value of the variable
127 (setq body
(replace-regexp-in-string
128 (format "\\$%s" (car pair
)) (cdr pair
) body
)))
132 (defun org-babel-execute:gnuplot
(body params
)
133 "Execute a block of Gnuplot code with org-babel. This function is
134 called by `org-babel-execute-src-block'."
135 (message "executing Gnuplot source code block")
137 (let ((session (cdr (assoc :session params
)))
138 (result-type (cdr (assoc :results params
)))
139 (out-file (cdr (assoc :file params
)))
140 (body (org-babel-expand-body:gnuplot body params
))
142 (save-window-excursion
143 ;; evaluate the code body with gnuplot
144 (if (string= session
"none")
145 (let ((script-file (make-temp-file "org-babel-gnuplot-script")))
146 (with-temp-file script-file
147 (insert (concat body
"\n")))
148 (message "gnuplot \"%s\"" script-file
)
150 (shell-command-to-string (format "gnuplot \"%s\"" script-file
)))
153 (insert (concat body
"\n"))
155 (gnuplot-send-buffer-to-gnuplot)))
156 (if (member "output" (split-string result-type
))
160 (defun org-babel-prep-session:gnuplot
(session params
)
161 "Prepare SESSION according to the header arguments specified in PARAMS."
162 (let* ((session (org-babel-gnuplot-initiate-session session
))
163 (vars (org-babel-ref-variables params
))
165 (lambda (pair) (format "%s = \"%s\"" (car pair
) (cdr pair
)))
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 (defvar gnuplot-buffer
)
185 (defun org-babel-gnuplot-initiate-session (&optional session params
)
186 "If there is not a current inferior-process-buffer in SESSION
187 then create one. Return the initialized session. The current
188 `gnuplot-mode' doesn't provide support for multiple sessions."
190 (unless (string= session
"none")
191 (save-window-excursion
192 (gnuplot-send-string-to-gnuplot "" "line")
195 (defun org-babel-gnuplot-quote-timestamp-field (s)
196 "Convert field S from timestamp to Unix time and export to gnuplot."
197 (format-time-string org-babel-gnuplot-timestamp-fmt
(org-time-string-to-time s
)))
199 (defvar org-table-number-regexp
)
200 (defvar org-ts-regexp3
)
201 (defun org-babel-gnuplot-quote-tsv-field (s)
202 "Quote field S for export to gnuplot."
204 (setq s
(format "%s" s
)))
205 (if (string-match org-table-number-regexp s
) s
206 (if (string-match org-ts-regexp3 s
)
207 (org-babel-gnuplot-quote-timestamp-field s
)
208 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"") "\""))))
210 (defun org-babel-gnuplot-table-to-data (table data-file params
)
211 "Export TABLE to DATA-FILE in a format readable by gnuplot.
212 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
213 (with-temp-file data-file
214 (make-local-variable 'org-babel-gnuplot-timestamp-fmt
)
215 (setq org-babel-gnuplot-timestamp-fmt
(or
216 (plist-get params
:timefmt
)
217 "%Y-%m-%d-%H:%M:%S"))
218 (insert (orgtbl-to-generic
221 '(:sep
"\t" :fmt org-babel-gnuplot-quote-tsv-field
)
225 (provide 'ob-gnuplot
)
227 ;; arch-tag: 50490ace-a9e1-4b29-a6e5-0db9f16c610b
229 ;;; ob-gnuplot.el ends here