1 ;;; ob-gnuplot.el --- Babel Functions for Gnuplot -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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
43 (declare-function org-time-string-to-time
"org" (s &optional buffer pos
))
44 (declare-function org-combine-plists
"org" (&rest plists
))
45 (declare-function orgtbl-to-generic
"org-table" (table params
))
46 (declare-function gnuplot-mode
"ext:gnuplot-mode" ())
47 (declare-function gnuplot-send-string-to-gnuplot
"ext:gnuplot-mode" (str txt
))
48 (declare-function gnuplot-send-buffer-to-gnuplot
"ext:gnuplot-mode" ())
50 (defvar org-babel-default-header-args
:gnuplot
51 '((:results .
"file") (:exports .
"results") (:session . nil
))
52 "Default arguments to use when evaluating a gnuplot source block.")
54 (defvar org-babel-header-args
:gnuplot
64 "Gnuplot specific header args.")
66 (defvar org-babel-gnuplot-timestamp-fmt nil
) ; Dynamically scoped.
68 (defvar *org-babel-gnuplot-missing
* nil
)
70 (defcustom *org-babel-gnuplot-terms
*
71 '((eps .
"postscript eps"))
72 "List of file extensions and the associated gnuplot terminal."
74 :type
'(repeat (cons (symbol :tag
"File extension")
75 (string :tag
"Gnuplot terminal"))))
77 (defun org-babel-gnuplot-process-vars (params)
78 "Extract variables from PARAMS and process the variables.
79 Dumps all vectors into files and returns an association list
80 of variable names and the related value to be used in the gnuplot
82 (let ((*org-babel-gnuplot-missing
* (cdr (assoc :missing params
))))
86 (car pair
) ;; variable name
87 (let* ((val (cdr pair
)) ;; variable value
90 (org-babel-gnuplot-table-to-data
91 (let* ((first (car val
))
92 (tablep (or (listp first
) (symbolp first
))))
93 (if tablep val
(mapcar 'list val
)))
94 (org-babel-temp-file "gnuplot-") params
)
96 (org-babel--get-vars params
))))
98 (defun org-babel-expand-body:gnuplot
(body params
)
99 "Expand BODY according to PARAMS, return the expanded body."
100 (save-window-excursion
101 (let* ((vars (org-babel-gnuplot-process-vars params
))
102 (out-file (cdr (assoc :file params
)))
103 (prologue (cdr (assoc :prologue params
)))
104 (epilogue (cdr (assoc :epilogue params
)))
105 (term (or (cdr (assoc :term params
))
107 (let ((ext (file-name-extension out-file
)))
108 (or (cdr (assoc (intern (downcase ext
))
109 *org-babel-gnuplot-terms
*))
111 (title (cdr (assoc :title params
)))
112 (lines (cdr (assoc :line params
)))
113 (sets (cdr (assoc :set params
)))
114 (x-labels (cdr (assoc :xlabels params
)))
115 (y-labels (cdr (assoc :ylabels params
)))
116 (timefmt (cdr (assoc :timefmt params
)))
117 (time-ind (or (cdr (assoc :timeind params
))
119 (add-to-body (lambda (text) (setq body
(concat text
"\n" body
)))))
120 ;; append header argument settings to body
121 (when title
(funcall add-to-body
(format "set title '%s'" title
)))
122 (when lines
(mapc (lambda (el) (funcall add-to-body el
)) lines
))
124 (mapc (lambda (el) (funcall add-to-body
(format "set %s" el
))) sets
))
127 (format "set xtics (%s)"
128 (mapconcat (lambda (pair)
130 (cdr pair
) (car pair
)))
134 (format "set ytics (%s)"
135 (mapconcat (lambda (pair)
137 (cdr pair
) (car pair
)))
140 (funcall add-to-body
"set xdata time")
141 (funcall add-to-body
(concat "set timefmt \""
143 "%Y-%m-%d-%H:%M:%S") "\"")))
145 ;; set the terminal at the top of the block
146 (funcall add-to-body
(format "set output \"%s\"" out-file
))
147 ;; and close the terminal at the bottom of the block
148 (setq body
(concat body
"\nset output\n")))
149 (when term
(funcall add-to-body
(format "set term %s" term
)))
150 ;; insert variables into code body: this should happen last
151 ;; placing the variables at the *top* of the code in case their
152 ;; values are used later
154 (mapconcat #'identity
155 (org-babel-variable-assignments:gnuplot params
)
157 ;; replace any variable names preceded by '$' with the actual
158 ;; value of the variable
160 (setq body
(replace-regexp-in-string
161 (format "\\$%s" (car pair
)) (cdr pair
) body
)))
163 (when prologue
(funcall add-to-body prologue
))
164 (when epilogue
(setq body
(concat body
"\n" epilogue
))))
167 (defun org-babel-execute:gnuplot
(body params
)
168 "Execute a block of Gnuplot code.
169 This function is called by `org-babel-execute-src-block'."
171 (let ((session (cdr (assoc :session params
)))
172 (result-type (cdr (assoc :results params
)))
173 (body (org-babel-expand-body:gnuplot body params
))
175 (save-window-excursion
176 ;; evaluate the code body with gnuplot
177 (if (string= session
"none")
178 (let ((script-file (org-babel-temp-file "gnuplot-script-")))
179 (with-temp-file script-file
180 (insert (concat body
"\n")))
181 (message "gnuplot \"%s\"" script-file
)
183 (shell-command-to-string
186 (org-babel-process-file-name
188 (if (member system-type
'(cygwin windows-nt ms-dos
))
192 (insert (concat body
"\n"))
194 (gnuplot-send-buffer-to-gnuplot)))
195 (if (member "output" (split-string result-type
))
197 nil
)))) ;; signal that output has already been written to file
199 (defun org-babel-prep-session:gnuplot
(session params
)
200 "Prepare SESSION according to the header arguments in PARAMS."
201 (let* ((session (org-babel-gnuplot-initiate-session session
))
202 (var-lines (org-babel-variable-assignments:gnuplot params
)))
203 (message "%S" session
)
204 (org-babel-comint-in-buffer session
205 (dolist (var-line var-lines
)
207 (comint-send-input nil t
)
208 (org-babel-comint-wait-for-output session
)
210 (goto-char (point-max))))
213 (defun org-babel-load-session:gnuplot
(session body params
)
214 "Load BODY into SESSION."
215 (save-window-excursion
216 (let ((buffer (org-babel-prep-session:gnuplot session params
)))
217 (with-current-buffer buffer
218 (goto-char (process-mark (get-buffer-process (current-buffer))))
219 (insert (org-babel-chomp body
)))
222 (defun org-babel-variable-assignments:gnuplot
(params)
223 "Return list of gnuplot statements assigning the block's variables."
225 (lambda (pair) (format "%s = \"%s\"" (car pair
) (cdr pair
)))
226 (org-babel-gnuplot-process-vars params
)))
228 (defvar gnuplot-buffer
)
229 (defun org-babel-gnuplot-initiate-session (&optional session _params
)
230 "Initiate a gnuplot session.
231 If there is not a current inferior-process-buffer in SESSION
232 then create one. Return the initialized session. The current
233 `gnuplot-mode' doesn't provide support for multiple sessions."
235 (unless (string= session
"none")
236 (save-window-excursion
237 (gnuplot-send-string-to-gnuplot "" "line")
240 (defun org-babel-gnuplot-quote-timestamp-field (s)
241 "Convert S from timestamp to Unix time and export to gnuplot."
242 (format-time-string org-babel-gnuplot-timestamp-fmt
243 (org-time-string-to-time s
)))
245 (defvar org-table-number-regexp
)
246 (defvar org-ts-regexp3
)
247 (defun org-babel-gnuplot-quote-tsv-field (s)
248 "Quote S for export to gnuplot."
250 (setq s
(format "%s" s
)))
251 (if (string-match org-table-number-regexp s
) s
252 (if (string-match org-ts-regexp3 s
)
253 (org-babel-gnuplot-quote-timestamp-field s
)
254 (if (zerop (length s
))
255 (or *org-babel-gnuplot-missing
* s
)
256 (if (string-match "[ \"]" s
)
257 (concat "\"" (mapconcat 'identity
(split-string s
"\"") "\"\"")
261 (defun org-babel-gnuplot-table-to-data (table data-file params
)
262 "Export TABLE to DATA-FILE in a format readable by gnuplot.
263 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
264 (with-temp-file data-file
265 (insert (let ((org-babel-gnuplot-timestamp-fmt
266 (or (plist-get params
:timefmt
) "%Y-%m-%d-%H:%M:%S")))
270 '(:sep
"\t" :fmt org-babel-gnuplot-quote-tsv-field
)
274 (provide 'ob-gnuplot
)
278 ;;; ob-gnuplot.el ends here