ob-gnuplot.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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: 0.01
  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. dumping all vectors into files and returning 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) (make-temp-file "org-babel-gnuplot") params)
  57. (cdr pair))))
  58. (org-babel-ref-variables params)))
  59. (defun org-babel-expand-body:gnuplot (body params &optional processed-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 with org-babel. This function is
  117. called by `org-babel-execute-src-block'."
  118. (message "executing Gnuplot source code block")
  119. (require 'gnuplot)
  120. (let ((session (cdr (assoc :session params)))
  121. (result-type (cdr (assoc :results params)))
  122. (out-file (cdr (assoc :file params)))
  123. (body (org-babel-expand-body:gnuplot body params))
  124. output)
  125. (save-window-excursion
  126. ;; evaluate the code body with gnuplot
  127. (if (string= session "none")
  128. (let ((script-file (make-temp-file "org-babel-gnuplot-script")))
  129. (with-temp-file script-file
  130. (insert (concat body "\n")))
  131. (message "gnuplot \"%s\"" script-file)
  132. (setq output
  133. (shell-command-to-string (format "gnuplot \"%s\"" script-file)))
  134. (message output))
  135. (with-temp-buffer
  136. (insert (concat body "\n"))
  137. (gnuplot-mode)
  138. (gnuplot-send-buffer-to-gnuplot)))
  139. (if (member "output" (split-string result-type))
  140. output
  141. out-file))))
  142. (defun org-babel-prep-session:gnuplot (session params)
  143. "Prepare SESSION according to the header arguments specified in PARAMS."
  144. (let* ((session (org-babel-gnuplot-initiate-session session))
  145. (vars (org-babel-ref-variables params))
  146. (var-lines (mapcar
  147. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  148. vars)))
  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. (defvar gnuplot-buffer)
  165. (defun org-babel-gnuplot-initiate-session (&optional session params)
  166. "If there is not a current inferior-process-buffer in SESSION
  167. then create one. Return the initialized session. The current
  168. `gnuplot-mode' doesn't provide support for multiple sessions."
  169. (require 'gnuplot)
  170. (unless (string= session "none")
  171. (save-window-excursion
  172. (gnuplot-send-string-to-gnuplot "" "line")
  173. gnuplot-buffer)))
  174. (defun org-babel-gnuplot-quote-timestamp-field (s)
  175. "Convert field S from timestamp to Unix time and export to gnuplot."
  176. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  177. (defvar org-table-number-regexp)
  178. (defvar org-ts-regexp3)
  179. (defun org-babel-gnuplot-quote-tsv-field (s)
  180. "Quote field S for export to gnuplot."
  181. (unless (stringp s)
  182. (setq s (format "%s" s)))
  183. (if (string-match org-table-number-regexp s) s
  184. (if (string-match org-ts-regexp3 s)
  185. (org-babel-gnuplot-quote-timestamp-field s)
  186. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  187. (defun org-babel-gnuplot-table-to-data (table data-file params)
  188. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  189. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  190. (with-temp-file data-file
  191. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  192. (setq org-babel-gnuplot-timestamp-fmt (or
  193. (plist-get params :timefmt)
  194. "%Y-%m-%d-%H:%M:%S"))
  195. (insert (orgtbl-to-generic
  196. table
  197. (org-combine-plists
  198. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  199. params))))
  200. data-file)
  201. (provide 'ob-gnuplot)
  202. ;; arch-tag: 50490ace-a9e1-4b29-a6e5-0db9f16c610b
  203. ;;; ob-gnuplot.el ends here