ob-gnuplot.el 10 KB

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