ob-gnuplot.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
  2. ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating gnuplot source code.
  19. ;;
  20. ;; This differs from most standard languages in that
  21. ;;
  22. ;; 1) we are generally only going to return results of type "file"
  23. ;;
  24. ;; 2) we are adding the "file" and "cmdline" header arguments
  25. ;;; Requirements:
  26. ;; - gnuplot :: http://www.gnuplot.info/
  27. ;;
  28. ;; - gnuplot-mode :: http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html
  29. ;;; Code:
  30. (require 'ob)
  31. (eval-when-compile (require 'cl))
  32. (declare-function org-time-string-to-time "org" (s &optional buffer pos))
  33. (declare-function org-combine-plists "org" (&rest plists))
  34. (declare-function orgtbl-to-generic "org-table" (table params))
  35. (declare-function gnuplot-mode "ext:gnuplot-mode" ())
  36. (declare-function gnuplot-send-string-to-gnuplot "ext:gnuplot-mode" (str txt))
  37. (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot-mode" ())
  38. (defvar org-babel-default-header-args:gnuplot
  39. '((:results . "file") (:exports . "results") (:session . nil))
  40. "Default arguments to use when evaluating a gnuplot source block.")
  41. (defvar org-babel-header-args:gnuplot
  42. '((title . :any)
  43. (lines . :any)
  44. (sets . :any)
  45. (x-labels . :any)
  46. (y-labels . :any)
  47. (timefmt . :any)
  48. (time-ind . :any)
  49. (missing . :any)
  50. (term . :any))
  51. "Gnuplot specific header args.")
  52. (defvar org-babel-gnuplot-timestamp-fmt nil) ; Dynamically scoped.
  53. (defvar *org-babel-gnuplot-missing* nil)
  54. (defcustom *org-babel-gnuplot-terms*
  55. '((eps . "postscript eps"))
  56. "List of file extensions and the associated gnuplot terminal."
  57. :group 'org-babel
  58. :type '(repeat (cons (symbol :tag "File extension")
  59. (string :tag "Gnuplot terminal"))))
  60. (defun org-babel-gnuplot-process-vars (params)
  61. "Extract variables from PARAMS and process the variables.
  62. Dumps all vectors into files and returns an association list
  63. of variable names and the related value to be used in the gnuplot
  64. code."
  65. (let ((*org-babel-gnuplot-missing* (cdr (assoc :missing params))))
  66. (mapcar
  67. (lambda (pair)
  68. (cons
  69. (car pair) ;; variable name
  70. (let* ((val (cdr pair)) ;; variable value
  71. (lp (listp val)))
  72. (if lp
  73. (org-babel-gnuplot-table-to-data
  74. (let* ((first (car val))
  75. (tablep (or (listp first) (symbolp first))))
  76. (if tablep val (mapcar 'list val)))
  77. (org-babel-temp-file "gnuplot-") params)
  78. val))))
  79. (mapcar #'cdr (org-babel-get-header params :var)))))
  80. (defun org-babel-expand-body:gnuplot (body params)
  81. "Expand BODY according to PARAMS, return the expanded body."
  82. (save-window-excursion
  83. (let* ((vars (org-babel-gnuplot-process-vars params))
  84. (out-file (cdr (assoc :file params)))
  85. (prologue (cdr (assoc :prologue params)))
  86. (epilogue (cdr (assoc :epilogue params)))
  87. (term (or (cdr (assoc :term params))
  88. (when out-file
  89. (let ((ext (file-name-extension out-file)))
  90. (or (cdr (assoc (intern (downcase ext))
  91. *org-babel-gnuplot-terms*))
  92. ext)))))
  93. (cmdline (cdr (assoc :cmdline params)))
  94. (title (cdr (assoc :title params)))
  95. (lines (cdr (assoc :line params)))
  96. (sets (cdr (assoc :set params)))
  97. (x-labels (cdr (assoc :xlabels params)))
  98. (y-labels (cdr (assoc :ylabels params)))
  99. (timefmt (cdr (assoc :timefmt params)))
  100. (time-ind (or (cdr (assoc :timeind params))
  101. (when timefmt 1)))
  102. (add-to-body (lambda (text) (setq body (concat text "\n" body))))
  103. output)
  104. ;; append header argument settings to body
  105. (when title (funcall add-to-body (format "set title '%s'" title)))
  106. (when lines (mapc (lambda (el) (funcall add-to-body el)) lines))
  107. (when sets
  108. (mapc (lambda (el) (funcall add-to-body (format "set %s" el))) sets))
  109. (when x-labels
  110. (funcall add-to-body
  111. (format "set xtics (%s)"
  112. (mapconcat (lambda (pair)
  113. (format "\"%s\" %d"
  114. (cdr pair) (car pair)))
  115. x-labels ", "))))
  116. (when y-labels
  117. (funcall add-to-body
  118. (format "set ytics (%s)"
  119. (mapconcat (lambda (pair)
  120. (format "\"%s\" %d"
  121. (cdr pair) (car pair)))
  122. y-labels ", "))))
  123. (when time-ind
  124. (funcall add-to-body "set xdata time")
  125. (funcall add-to-body (concat "set timefmt \""
  126. (or timefmt
  127. "%Y-%m-%d-%H:%M:%S") "\"")))
  128. (when out-file
  129. ;; set the terminal at the top of the block
  130. (funcall add-to-body (format "set output \"%s\"" out-file))
  131. ;; and close the terminal at the bottom of the block
  132. (setq body (concat body "\nset output\n")))
  133. (when term (funcall add-to-body (format "set term %s" term)))
  134. ;; insert variables into code body: this should happen last
  135. ;; placing the variables at the *top* of the code in case their
  136. ;; values are used later
  137. (funcall add-to-body
  138. (mapconcat #'identity
  139. (org-babel-variable-assignments:gnuplot params)
  140. "\n"))
  141. ;; replace any variable names preceded by '$' with the actual
  142. ;; value of the variable
  143. (mapc (lambda (pair)
  144. (setq body (replace-regexp-in-string
  145. (format "\\$%s" (car pair)) (cdr pair) body)))
  146. vars)
  147. (when prologue (funcall add-to-body prologue))
  148. (when epilogue (setq body (concat body "\n" epilogue))))
  149. body))
  150. (defun org-babel-execute:gnuplot (body params)
  151. "Execute a block of Gnuplot code.
  152. This function is called by `org-babel-execute-src-block'."
  153. (require 'gnuplot)
  154. (let ((session (cdr (assoc :session params)))
  155. (result-type (cdr (assoc :results params)))
  156. (out-file (cdr (assoc :file params)))
  157. (body (org-babel-expand-body:gnuplot body params))
  158. output)
  159. (save-window-excursion
  160. ;; evaluate the code body with gnuplot
  161. (if (string= session "none")
  162. (let ((script-file (org-babel-temp-file "gnuplot-script-")))
  163. (with-temp-file script-file
  164. (insert (concat body "\n")))
  165. (message "gnuplot \"%s\"" script-file)
  166. (setq output
  167. (shell-command-to-string
  168. (format
  169. "gnuplot \"%s\""
  170. (org-babel-process-file-name
  171. script-file
  172. (if (member system-type '(cygwin windows-nt ms-dos))
  173. t nil)))))
  174. (message output))
  175. (with-temp-buffer
  176. (insert (concat body "\n"))
  177. (gnuplot-mode)
  178. (gnuplot-send-buffer-to-gnuplot)))
  179. (if (member "output" (split-string result-type))
  180. output
  181. nil)))) ;; signal that output has already been written to file
  182. (defun org-babel-prep-session:gnuplot (session params)
  183. "Prepare SESSION according to the header arguments in PARAMS."
  184. (let* ((session (org-babel-gnuplot-initiate-session session))
  185. (var-lines (org-babel-variable-assignments:gnuplot params)))
  186. (message "%S" session)
  187. (org-babel-comint-in-buffer session
  188. (mapc (lambda (var-line)
  189. (insert var-line) (comint-send-input nil t)
  190. (org-babel-comint-wait-for-output session)
  191. (sit-for .1) (goto-char (point-max))) var-lines))
  192. session))
  193. (defun org-babel-load-session:gnuplot (session body params)
  194. "Load BODY into SESSION."
  195. (save-window-excursion
  196. (let ((buffer (org-babel-prep-session:gnuplot session params)))
  197. (with-current-buffer buffer
  198. (goto-char (process-mark (get-buffer-process (current-buffer))))
  199. (insert (org-babel-chomp body)))
  200. buffer)))
  201. (defun org-babel-variable-assignments:gnuplot (params)
  202. "Return list of gnuplot statements assigning the block's variables."
  203. (mapcar
  204. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  205. (org-babel-gnuplot-process-vars params)))
  206. (defvar gnuplot-buffer)
  207. (defun org-babel-gnuplot-initiate-session (&optional session params)
  208. "Initiate a gnuplot session.
  209. If there is not a current inferior-process-buffer in SESSION
  210. then create one. Return the initialized session. The current
  211. `gnuplot-mode' doesn't provide support for multiple sessions."
  212. (require 'gnuplot)
  213. (unless (string= session "none")
  214. (save-window-excursion
  215. (gnuplot-send-string-to-gnuplot "" "line")
  216. gnuplot-buffer)))
  217. (defun org-babel-gnuplot-quote-timestamp-field (s)
  218. "Convert S from timestamp to Unix time and export to gnuplot."
  219. (format-time-string org-babel-gnuplot-timestamp-fmt
  220. (org-time-string-to-time s)))
  221. (defvar org-table-number-regexp)
  222. (defvar org-ts-regexp3)
  223. (defun org-babel-gnuplot-quote-tsv-field (s)
  224. "Quote S for export to gnuplot."
  225. (unless (stringp s)
  226. (setq s (format "%s" s)))
  227. (if (string-match org-table-number-regexp s) s
  228. (if (string-match org-ts-regexp3 s)
  229. (org-babel-gnuplot-quote-timestamp-field s)
  230. (if (zerop (length s))
  231. (or *org-babel-gnuplot-missing* s)
  232. (if (string-match "[ \"]" s)
  233. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"")
  234. "\"")
  235. s)))))
  236. (defun org-babel-gnuplot-table-to-data (table data-file params)
  237. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  238. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  239. (with-temp-file data-file
  240. (insert (let ((org-babel-gnuplot-timestamp-fmt
  241. (or (plist-get params :timefmt) "%Y-%m-%d-%H:%M:%S")))
  242. (orgtbl-to-generic
  243. table
  244. (org-combine-plists
  245. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  246. params)))))
  247. data-file)
  248. (provide 'ob-gnuplot)
  249. ;;; ob-gnuplot.el ends here