org-babel-gnuplot.el 8.4 KB

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