org-babel-gnuplot.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. (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. ;; evaluate the code body with gnuplot
  115. (if (string= session "none")
  116. (let ((script-file (make-temp-file "org-babel-gnuplot-script")))
  117. (with-temp-file script-file
  118. (insert (concat body "\n")))
  119. (message "gnuplot \"%s\"" script-file)
  120. (message (shell-command-to-string (format "gnuplot \"%s\"" script-file))))
  121. (with-temp-buffer
  122. (insert (concat body "\n"))
  123. (gnuplot-mode)
  124. (gnuplot-send-buffer-to-gnuplot)))
  125. out-file))))
  126. (defun org-babel-prep-session:gnuplot (session params)
  127. "Prepare SESSION according to the header arguments specified in PARAMS."
  128. (let* ((session (org-babel-gnuplot-initiate-session session))
  129. (vars (org-babel-ref-variables params))
  130. (var-lines (mapconc
  131. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  132. vars)))
  133. (org-babel-comint-in-buffer session
  134. (mapc (lambda (var-line)
  135. (insert var-line) (comint-send-input nil t)
  136. (org-babel-comint-wait-for-output session)
  137. (sit-for .1) (goto-char (point-max))) var-lines))))
  138. (defun org-babel-gnuplot-initiate-session (&optional session)
  139. "If there is not a current inferior-process-buffer in SESSION
  140. then create. Return the initialized session. The current
  141. `gnuplot-mode' doesn't provide support for multiple sessions."
  142. (unless (string= session "none")
  143. (save-window-excursion (gnuplot-send-string-to-gnuplot "" "line")
  144. (current-buffer))))
  145. (defun org-babel-gnuplot-quote-timestamp-field (s)
  146. "Convert field S from timestamp to Unix time and export to gnuplot."
  147. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  148. (defun org-babel-gnuplot-quote-tsv-field (s)
  149. "Quote field S for export to gnuplot."
  150. (unless (stringp s)
  151. (setq s (format "%s" s)))
  152. (if (string-match org-table-number-regexp s) s
  153. (if (string-match org-ts-regexp3 s)
  154. (org-babel-gnuplot-quote-timestamp-field s)
  155. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  156. (defun org-babel-gnuplot-table-to-data (table data-file params)
  157. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  158. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  159. (with-temp-file data-file
  160. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  161. (setq org-babel-gnuplot-timestamp-fmt (or
  162. (plist-get params :timefmt)
  163. "%Y-%m-%d-%H:%M:%S"))
  164. (insert (orgtbl-to-generic
  165. table
  166. (org-combine-plists
  167. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  168. params))))
  169. data-file)
  170. (provide 'org-babel-gnuplot)
  171. ;;; org-babel-gnuplot.el ends here