ob-gnuplot.el 9.4 KB

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