ob-gnuplot.el 9.1 KB

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