ob-gnuplot.el 8.9 KB

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