ob-gnuplot.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.01trans
  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
  106. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  107. vars "\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. out-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. (vars (org-babel-ref-variables params))
  148. (var-lines (mapcar
  149. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  150. vars)))
  151. (message "%S" session)
  152. (org-babel-comint-in-buffer session
  153. (mapc (lambda (var-line)
  154. (insert var-line) (comint-send-input nil t)
  155. (org-babel-comint-wait-for-output session)
  156. (sit-for .1) (goto-char (point-max))) var-lines))
  157. session))
  158. (defun org-babel-load-session:gnuplot (session body params)
  159. "Load BODY into SESSION."
  160. (save-window-excursion
  161. (let ((buffer (org-babel-prep-session:gnuplot session params)))
  162. (with-current-buffer buffer
  163. (goto-char (process-mark (get-buffer-process (current-buffer))))
  164. (insert (org-babel-chomp body)))
  165. buffer)))
  166. (defvar gnuplot-buffer)
  167. (defun org-babel-gnuplot-initiate-session (&optional session params)
  168. "Initiate a gnuplot session.
  169. If there is not a current inferior-process-buffer in SESSION
  170. then create one. Return the initialized session. The current
  171. `gnuplot-mode' doesn't provide support for multiple sessions."
  172. (require 'gnuplot)
  173. (unless (string= session "none")
  174. (save-window-excursion
  175. (gnuplot-send-string-to-gnuplot "" "line")
  176. gnuplot-buffer)))
  177. (defun org-babel-gnuplot-quote-timestamp-field (s)
  178. "Convert S from timestamp to Unix time and export to gnuplot."
  179. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  180. (defvar org-table-number-regexp)
  181. (defvar org-ts-regexp3)
  182. (defun org-babel-gnuplot-quote-tsv-field (s)
  183. "Quote S for export to gnuplot."
  184. (unless (stringp s)
  185. (setq s (format "%s" s)))
  186. (if (string-match org-table-number-regexp s) s
  187. (if (string-match org-ts-regexp3 s)
  188. (org-babel-gnuplot-quote-timestamp-field s)
  189. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  190. (defun org-babel-gnuplot-table-to-data (table data-file params)
  191. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  192. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  193. (with-temp-file data-file
  194. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  195. (setq org-babel-gnuplot-timestamp-fmt (or
  196. (plist-get params :timefmt)
  197. "%Y-%m-%d-%H:%M:%S"))
  198. (insert (orgtbl-to-generic
  199. table
  200. (org-combine-plists
  201. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  202. params))))
  203. data-file)
  204. (provide 'ob-gnuplot)
  205. ;; arch-tag: 50490ace-a9e1-4b29-a6e5-0db9f16c610b
  206. ;;; ob-gnuplot.el ends here