ob-gnuplot.el 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. ;;; ob-gnuplot.el --- org-babel functions for gnuplot evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.5
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating gnuplot source code.
  20. ;;
  21. ;; This differs from most standard languages in that
  22. ;;
  23. ;; 1) we are generally only going to return results of type "file"
  24. ;;
  25. ;; 2) we are adding the "file" and "cmdline" header arguments
  26. ;;; Requirements:
  27. ;; - gnuplot :: http://www.gnuplot.info/
  28. ;;
  29. ;; - gnuplot-mode :: http://cars9.uchicago.edu/~ravel/software/gnuplot-mode.html
  30. ;;; Code:
  31. (require 'ob)
  32. (require 'ob-ref)
  33. (require 'ob-comint)
  34. (eval-when-compile (require 'cl))
  35. (declare-function org-time-string-to-time "org" (s))
  36. (declare-function org-combine-plists "org" (&rest plists))
  37. (declare-function orgtbl-to-generic "org-table" (table params))
  38. (declare-function gnuplot-mode "ext:gnuplot-mode" ())
  39. (declare-function gnuplot-send-string-to-gnuplot "ext:gnuplot-mode" (str txt))
  40. (declare-function gnuplot-send-buffer-to-gnuplot "ext:gnuplot-mode" ())
  41. (defvar org-babel-default-header-args:gnuplot
  42. '((:results . "file") (:exports . "results") (:session . nil))
  43. "Default arguments to use when evaluating a gnuplot source block.")
  44. (defvar org-babel-gnuplot-timestamp-fmt nil)
  45. (defun org-babel-gnuplot-process-vars (params)
  46. "Extract variables from PARAMS and process the variables.
  47. Dumps all vectors into files and returns an association list
  48. of variable names and the related value to be used in the gnuplot
  49. code."
  50. (mapcar
  51. (lambda (pair)
  52. (cons
  53. (car pair) ;; variable name
  54. (if (listp (cdr pair)) ;; variable value
  55. (org-babel-gnuplot-table-to-data
  56. (cdr pair) (org-babel-temp-file "gnuplot-") params)
  57. (cdr pair))))
  58. (mapcar #'cdr (org-babel-get-header params :var))))
  59. (defun org-babel-expand-body:gnuplot (body params)
  60. "Expand BODY according to PARAMS, return the expanded body."
  61. (save-window-excursion
  62. (let* ((vars (org-babel-gnuplot-process-vars params))
  63. (out-file (cdr (assoc :file params)))
  64. (term (or (cdr (assoc :term params))
  65. (when out-file (file-name-extension out-file))))
  66. (cmdline (cdr (assoc :cmdline params)))
  67. (title (plist-get params :title))
  68. (lines (plist-get params :line))
  69. (sets (plist-get params :set))
  70. (x-labels (plist-get params :xlabels))
  71. (y-labels (plist-get params :ylabels))
  72. (timefmt (plist-get params :timefmt))
  73. (time-ind (or (plist-get params :timeind)
  74. (when timefmt 1)))
  75. output)
  76. (flet ((add-to-body (text)
  77. (setq body (concat text "\n" body))))
  78. ;; append header argument settings to body
  79. (when title (add-to-body (format "set title '%s'" title))) ;; title
  80. (when lines (mapc (lambda (el) (add-to-body el)) lines)) ;; line
  81. (when sets
  82. (mapc (lambda (el) (add-to-body (format "set %s" el))) sets))
  83. (when x-labels
  84. (add-to-body
  85. (format "set xtics (%s)"
  86. (mapconcat (lambda (pair)
  87. (format "\"%s\" %d" (cdr pair) (car pair)))
  88. x-labels ", "))))
  89. (when y-labels
  90. (add-to-body
  91. (format "set ytics (%s)"
  92. (mapconcat (lambda (pair)
  93. (format "\"%s\" %d" (cdr pair) (car pair)))
  94. y-labels ", "))))
  95. (when time-ind
  96. (add-to-body "set xdata time")
  97. (add-to-body (concat "set timefmt \""
  98. (or timefmt
  99. "%Y-%m-%d-%H:%M:%S") "\"")))
  100. (when out-file (add-to-body (format "set output \"%s\"" out-file)))
  101. (when term (add-to-body (format "set term %s" term)))
  102. ;; insert variables into code body: this should happen last
  103. ;; placing the variables at the *top* of the code in case their
  104. ;; values are used later
  105. (add-to-body (mapconcat #'identity
  106. (org-babel-variable-assignments:gnuplot params)
  107. "\n"))
  108. ;; replace any variable names preceded by '$' with the actual
  109. ;; value of the variable
  110. (mapc (lambda (pair)
  111. (setq body (replace-regexp-in-string
  112. (format "\\$%s" (car pair)) (cdr pair) body)))
  113. vars))
  114. body)))
  115. (defun org-babel-execute:gnuplot (body params)
  116. "Execute a block of Gnuplot code.
  117. This function is called by `org-babel-execute-src-block'."
  118. (require 'gnuplot)
  119. (let ((session (cdr (assoc :session params)))
  120. (result-type (cdr (assoc :results params)))
  121. (out-file (cdr (assoc :file params)))
  122. (body (org-babel-expand-body:gnuplot body params))
  123. output)
  124. (save-window-excursion
  125. ;; evaluate the code body with gnuplot
  126. (if (string= session "none")
  127. (let ((script-file (org-babel-temp-file "gnuplot-script-")))
  128. (with-temp-file script-file
  129. (insert (concat body "\n")))
  130. (message "gnuplot \"%s\"" script-file)
  131. (setq output
  132. (shell-command-to-string
  133. (format
  134. "gnuplot \"%s\""
  135. (org-babel-process-file-name script-file))))
  136. (message output))
  137. (with-temp-buffer
  138. (insert (concat body "\n"))
  139. (gnuplot-mode)
  140. (gnuplot-send-buffer-to-gnuplot)))
  141. (if (member "output" (split-string result-type))
  142. output
  143. nil)))) ;; signal that output has already been written to file
  144. (defun org-babel-prep-session:gnuplot (session params)
  145. "Prepare SESSION according to the header arguments in PARAMS."
  146. (let* ((session (org-babel-gnuplot-initiate-session session))
  147. (var-lines (org-babel-variable-assignments:gnuplot params)))
  148. (message "%S" session)
  149. (org-babel-comint-in-buffer session
  150. (mapc (lambda (var-line)
  151. (insert var-line) (comint-send-input nil t)
  152. (org-babel-comint-wait-for-output session)
  153. (sit-for .1) (goto-char (point-max))) var-lines))
  154. session))
  155. (defun org-babel-load-session:gnuplot (session body params)
  156. "Load BODY into SESSION."
  157. (save-window-excursion
  158. (let ((buffer (org-babel-prep-session:gnuplot session params)))
  159. (with-current-buffer buffer
  160. (goto-char (process-mark (get-buffer-process (current-buffer))))
  161. (insert (org-babel-chomp body)))
  162. buffer)))
  163. (defun org-babel-variable-assignments:gnuplot (params)
  164. "Return list of gnuplot statements assigning the block's variables"
  165. (mapcar
  166. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  167. (org-babel-gnuplot-process-vars params)))
  168. (defvar gnuplot-buffer)
  169. (defun org-babel-gnuplot-initiate-session (&optional session params)
  170. "Initiate a gnuplot session.
  171. If there is not a current inferior-process-buffer in SESSION
  172. then create one. Return the initialized session. The current
  173. `gnuplot-mode' doesn't provide support for multiple sessions."
  174. (require 'gnuplot)
  175. (unless (string= session "none")
  176. (save-window-excursion
  177. (gnuplot-send-string-to-gnuplot "" "line")
  178. gnuplot-buffer)))
  179. (defun org-babel-gnuplot-quote-timestamp-field (s)
  180. "Convert S from timestamp to Unix time and export to gnuplot."
  181. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  182. (defvar org-table-number-regexp)
  183. (defvar org-ts-regexp3)
  184. (defun org-babel-gnuplot-quote-tsv-field (s)
  185. "Quote S for export to gnuplot."
  186. (unless (stringp s)
  187. (setq s (format "%s" s)))
  188. (if (string-match org-table-number-regexp s) s
  189. (if (string-match org-ts-regexp3 s)
  190. (org-babel-gnuplot-quote-timestamp-field s)
  191. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  192. (defun org-babel-gnuplot-table-to-data (table data-file params)
  193. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  194. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  195. (with-temp-file data-file
  196. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  197. (setq org-babel-gnuplot-timestamp-fmt (or
  198. (plist-get params :timefmt)
  199. "%Y-%m-%d-%H:%M:%S"))
  200. (insert (orgtbl-to-generic
  201. table
  202. (org-combine-plists
  203. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  204. params))))
  205. data-file)
  206. (provide 'ob-gnuplot)
  207. ;; arch-tag: 50490ace-a9e1-4b29-a6e5-0db9f16c610b
  208. ;;; ob-gnuplot.el ends here