ob-gnuplot.el 9.8 KB

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