ob-gnuplot.el 8.9 KB

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