Merge from origin/emacs-25
[emacs.git] / lisp / org / ob-gnuplot.el
blobabf45af852398b21e1c798468e9f230b0319306e
1 ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
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/>.
24 ;;; Commentary:
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
34 ;;; Requirements:
36 ;; - gnuplot :: http://www.gnuplot.info/
38 ;; - gnuplot-mode :: http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html
40 ;;; Code:
41 (require 'ob)
42 (eval-when-compile (require 'cl))
44 (declare-function org-time-string-to-time "org" (s &optional buffer pos))
45 (declare-function org-combine-plists "org" (&rest plists))
46 (declare-function orgtbl-to-generic "org-table"
47 (table params &optional backend))
48 (declare-function gnuplot-mode "ext:gnuplot-mode" ())
49 (declare-function gnuplot-send-string-to-gnuplot "ext:gnuplot-mode" (str txt))
50 (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot-mode" ())
52 (defvar org-babel-default-header-args:gnuplot
53 '((:results . "file") (:exports . "results") (:session . nil))
54 "Default arguments to use when evaluating a gnuplot source block.")
56 (defvar org-babel-header-args:gnuplot
57 '((title . :any)
58 (lines . :any)
59 (sets . :any)
60 (x-labels . :any)
61 (y-labels . :any)
62 (timefmt . :any)
63 (time-ind . :any)
64 (missing . :any)
65 (term . :any))
66 "Gnuplot specific header args.")
68 (defvar org-babel-gnuplot-timestamp-fmt nil)
70 (defvar *org-babel-gnuplot-missing* nil)
72 (defcustom *org-babel-gnuplot-terms*
73 '((eps . "postscript eps"))
74 "List of file extensions and the associated gnuplot terminal."
75 :group 'org-babel
76 :type '(repeat (cons (symbol :tag "File extension")
77 (string :tag "Gnuplot terminal"))))
79 (defun org-babel-gnuplot-process-vars (params)
80 "Extract variables from PARAMS and process the variables.
81 Dumps all vectors into files and returns an association list
82 of variable names and the related value to be used in the gnuplot
83 code."
84 (let ((*org-babel-gnuplot-missing* (cdr (assoc :missing params))))
85 (mapcar
86 (lambda (pair)
87 (cons
88 (car pair) ;; variable name
89 (let* ((val (cdr pair)) ;; variable value
90 (lp (listp val)))
91 (if lp
92 (org-babel-gnuplot-table-to-data
93 (let* ((first (car val))
94 (tablep (or (listp first) (symbolp first))))
95 (if tablep val (mapcar 'list val)))
96 (org-babel-temp-file "gnuplot-") params)
97 val))))
98 (mapcar #'cdr (org-babel-get-header params :var)))))
100 (defun org-babel-expand-body:gnuplot (body params)
101 "Expand BODY according to PARAMS, return the expanded body."
102 (save-window-excursion
103 (let* ((vars (org-babel-gnuplot-process-vars params))
104 (out-file (cdr (assoc :file params)))
105 (prologue (cdr (assoc :prologue params)))
106 (epilogue (cdr (assoc :epilogue params)))
107 (term (or (cdr (assoc :term params))
108 (when out-file
109 (let ((ext (file-name-extension out-file)))
110 (or (cdr (assoc (intern (downcase ext))
111 *org-babel-gnuplot-terms*))
112 ext)))))
113 (cmdline (cdr (assoc :cmdline params)))
114 (title (cdr (assoc :title params)))
115 (lines (cdr (assoc :line params)))
116 (sets (cdr (assoc :set params)))
117 (x-labels (cdr (assoc :xlabels params)))
118 (y-labels (cdr (assoc :ylabels params)))
119 (timefmt (cdr (assoc :timefmt params)))
120 (time-ind (or (cdr (assoc :timeind params))
121 (when timefmt 1)))
122 (missing (cdr (assoc :missing params)))
123 (add-to-body (lambda (text) (setq body (concat text "\n" body))))
124 output)
125 ;; append header argument settings to body
126 (when title (funcall add-to-body (format "set title '%s'" title)))
127 (when lines (mapc (lambda (el) (funcall add-to-body el)) lines))
128 (when missing
129 (funcall add-to-body (format "set datafile missing '%s'" missing)))
130 (when sets
131 (mapc (lambda (el) (funcall add-to-body (format "set %s" el))) sets))
132 (when x-labels
133 (funcall add-to-body
134 (format "set xtics (%s)"
135 (mapconcat (lambda (pair)
136 (format "\"%s\" %d"
137 (cdr pair) (car pair)))
138 x-labels ", "))))
139 (when y-labels
140 (funcall add-to-body
141 (format "set ytics (%s)"
142 (mapconcat (lambda (pair)
143 (format "\"%s\" %d"
144 (cdr pair) (car pair)))
145 y-labels ", "))))
146 (when time-ind
147 (funcall add-to-body "set xdata time")
148 (funcall add-to-body (concat "set timefmt \""
149 (or timefmt
150 "%Y-%m-%d-%H:%M:%S") "\"")))
151 (when out-file
152 ;; set the terminal at the top of the block
153 (funcall add-to-body (format "set output \"%s\"" out-file))
154 ;; and close the terminal at the bottom of the block
155 (setq body (concat body "\nset output\n")))
156 (when term (funcall add-to-body (format "set term %s" term)))
157 ;; insert variables into code body: this should happen last
158 ;; placing the variables at the *top* of the code in case their
159 ;; values are used later
160 (funcall add-to-body
161 (mapconcat #'identity
162 (org-babel-variable-assignments:gnuplot params)
163 "\n"))
164 ;; replace any variable names preceded by '$' with the actual
165 ;; value of the variable
166 (mapc (lambda (pair)
167 (setq body (replace-regexp-in-string
168 (format "\\$%s" (car pair)) (cdr pair) body)))
169 vars)
170 (when prologue (funcall add-to-body prologue))
171 (when epilogue (setq body (concat body "\n" epilogue))))
172 body))
174 (defun org-babel-execute:gnuplot (body params)
175 "Execute a block of Gnuplot code.
176 This function is called by `org-babel-execute-src-block'."
177 (require 'gnuplot)
178 (let ((session (cdr (assoc :session params)))
179 (result-type (cdr (assoc :results params)))
180 (out-file (cdr (assoc :file params)))
181 (body (org-babel-expand-body:gnuplot body params))
182 output)
183 (save-window-excursion
184 ;; evaluate the code body with gnuplot
185 (if (string= session "none")
186 (let ((script-file (org-babel-temp-file "gnuplot-script-")))
187 (with-temp-file script-file
188 (insert (concat body "\n")))
189 (message "gnuplot \"%s\"" script-file)
190 (setq output
191 (shell-command-to-string
192 (format
193 "gnuplot \"%s\""
194 (org-babel-process-file-name
195 script-file
196 (if (member system-type '(cygwin windows-nt ms-dos))
197 t nil)))))
198 (message output))
199 (with-temp-buffer
200 (insert (concat body "\n"))
201 (gnuplot-mode)
202 (gnuplot-send-buffer-to-gnuplot)))
203 (if (member "output" (split-string result-type))
204 output
205 nil)))) ;; signal that output has already been written to file
207 (defun org-babel-prep-session:gnuplot (session params)
208 "Prepare SESSION according to the header arguments in PARAMS."
209 (let* ((session (org-babel-gnuplot-initiate-session session))
210 (var-lines (org-babel-variable-assignments:gnuplot params)))
211 (message "%S" session)
212 (org-babel-comint-in-buffer session
213 (mapc (lambda (var-line)
214 (insert var-line) (comint-send-input nil t)
215 (org-babel-comint-wait-for-output session)
216 (sit-for .1) (goto-char (point-max))) var-lines))
217 session))
219 (defun org-babel-load-session:gnuplot (session body params)
220 "Load BODY into SESSION."
221 (save-window-excursion
222 (let ((buffer (org-babel-prep-session:gnuplot session params)))
223 (with-current-buffer buffer
224 (goto-char (process-mark (get-buffer-process (current-buffer))))
225 (insert (org-babel-chomp body)))
226 buffer)))
228 (defun org-babel-variable-assignments:gnuplot (params)
229 "Return list of gnuplot statements assigning the block's variables."
230 (mapcar
231 (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
232 (org-babel-gnuplot-process-vars params)))
234 (defvar gnuplot-buffer)
235 (defun org-babel-gnuplot-initiate-session (&optional session params)
236 "Initiate a gnuplot session.
237 If there is not a current inferior-process-buffer in SESSION
238 then create one. Return the initialized session. The current
239 `gnuplot-mode' doesn't provide support for multiple sessions."
240 (require 'gnuplot)
241 (unless (string= session "none")
242 (save-window-excursion
243 (gnuplot-send-string-to-gnuplot "" "line")
244 gnuplot-buffer)))
246 (defun org-babel-gnuplot-quote-timestamp-field (s)
247 "Convert S from timestamp to Unix time and export to gnuplot."
248 (format-time-string org-babel-gnuplot-timestamp-fmt
249 (org-time-string-to-time s)))
251 (defvar org-table-number-regexp)
252 (defvar org-ts-regexp3)
253 (defun org-babel-gnuplot-quote-tsv-field (s)
254 "Quote S for export to gnuplot."
255 (unless (stringp s)
256 (setq s (format "%s" s)))
257 (if (string-match org-table-number-regexp s) s
258 (if (string-match org-ts-regexp3 s)
259 (org-babel-gnuplot-quote-timestamp-field s)
260 (if (zerop (length s))
261 (or *org-babel-gnuplot-missing* s)
262 (if (string-match "[ \"]" s)
263 (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"")
264 "\"")
265 s)))))
267 (defun org-babel-gnuplot-table-to-data (table data-file params)
268 "Export TABLE to DATA-FILE in a format readable by gnuplot.
269 Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
270 (with-temp-file data-file
271 (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
272 (setq org-babel-gnuplot-timestamp-fmt (or
273 (plist-get params :timefmt)
274 "%Y-%m-%d-%H:%M:%S"))
275 (insert (orgtbl-to-generic
276 table
277 (org-combine-plists
278 '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
279 params))))
280 data-file)
282 (provide 'ob-gnuplot)
286 ;;; ob-gnuplot.el ends here