org-babel-gnuplot.el 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. ;;; Code:
  31. (require 'org-babel)
  32. (require 'gnuplot)
  33. (org-babel-add-interpreter "gnuplot")
  34. (add-to-list 'org-babel-tangle-langs '("gnuplot" "gnuplot"))
  35. (defvar org-babel-default-header-args:gnuplot '((:results . "file") (:exports . "results"))
  36. "Default arguments to use when evaluating a gnuplot source block.")
  37. (defvar org-babel-gnuplot-timestamp-fmt nil)
  38. (defun org-babel-gnuplot-process-vars (params)
  39. "Extract variables from PARAMS and process the variables
  40. dumping all vectors into files returning an association list of
  41. variable names and the value to be used in the gnuplot code."
  42. (mapcar
  43. (lambda (pair)
  44. (cons
  45. (car pair) ;; variable name
  46. (if (listp (cdr pair)) ;; variable value
  47. (org-babel-gnuplot-table-to-data
  48. (cdr pair) (make-temp-file "org-babel-gnuplot") params)
  49. (cdr pair))))
  50. (org-babel-ref-variables params)))
  51. (defun org-babel-execute:gnuplot (body params)
  52. "Execute a block of Gnuplot code with org-babel. This function is
  53. called by `org-babel-execute-src-block' via multiple-value-bind."
  54. (message "executing Gnuplot source code block")
  55. (let* ((vars (org-babel-gnuplot-process-vars params))
  56. (out-file (cdr (assoc :file params)))
  57. (term (or (cdr (assoc :term params))
  58. (when out-file (file-name-extension out-file))))
  59. (cmdline (cdr (assoc :cmdline params)))
  60. (in-file (make-temp-file "org-babel-ditaa"))
  61. (title (plist-get params :title))
  62. (lines (plist-get params :line))
  63. (sets (plist-get params :set))
  64. (x-labels (plist-get params :xlabels))
  65. (y-labels (plist-get params :ylabels))
  66. (timefmt (plist-get params :timefmt))
  67. (time-ind (or (plist-get params :timeind)
  68. (when timefmt 1))))
  69. (flet ((add-to-body (text)
  70. (setq body (concat text "\n" body))))
  71. ;; append header argument settings to body
  72. (when title (add-to-body (format "set title '%s'" title))) ;; title
  73. (when lines (mapc (lambda (el) (add-to-body el)) lines)) ;; line
  74. (when sets
  75. (mapc (lambda (el) (add-to-body (format "set %s" el))) sets))
  76. (when x-labels
  77. (add-to-body
  78. (format "set xtics (%s)"
  79. (mapconcat (lambda (pair)
  80. (format "\"%s\" %d" (cdr pair) (car pair)))
  81. x-labels ", "))))
  82. (when y-labels
  83. (add-to-body
  84. (format "set ytics (%s)"
  85. (mapconcat (lambda (pair)
  86. (format "\"%s\" %d" (cdr pair) (car pair)))
  87. y-labels ", "))))
  88. (when time-ind
  89. (add-to-body "set xdata time")
  90. (add-to-body (concat "set timefmt \""
  91. (or timefmt
  92. "%Y-%m-%d-%H:%M:%S") "\"")))
  93. (when out-file (add-to-body (format "set output \"%s\"" out-file)))
  94. (when term (add-to-body (format "set term %s" term)))
  95. ;; insert variables into code body: this should happen last
  96. ;; placing the variables at the *top* of the code in case their
  97. ;; values are used later
  98. (add-to-body (mapconcat
  99. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  100. vars "\n"))
  101. ;; evaluate the code body with gnuplot
  102. (with-temp-buffer
  103. (insert (concat body "\n"))
  104. (gnuplot-mode)
  105. (gnuplot-send-buffer-to-gnuplot))
  106. out-file)))
  107. (defun org-babel-prep-session:gnuplot (session params)
  108. "Prepare SESSION according to the header arguments specified in PARAMS."
  109. (let* ((session (org-babel-gnuplot-initiate-session session))
  110. (vars (org-babel-ref-variables params))
  111. (var-lines (mapconc
  112. (lambda (pair) (format "%s = \"%s\"" (car pair) (cdr pair)))
  113. vars)))
  114. (org-babel-comint-in-buffer session
  115. (mapc (lambda (var-line)
  116. (insert var-line) (comint-send-input nil t)
  117. (org-babel-comint-wait-for-output session)
  118. (sit-for .1) (goto-char (point-max))) var-lines))))
  119. (defun org-babel-gnuplot-initiate-session (&optional session)
  120. "If there is not a current inferior-process-buffer in SESSION
  121. then create. Return the initialized session. The current
  122. `gnuplot-mode' doesn't provide support for multiple sessions."
  123. (unless (string= session "none")
  124. (save-window-excursion (gnuplot-send-string-to-gnuplot "" "line")
  125. (current-buffer))))
  126. (defun org-babel-gnuplot-quote-timestamp-field (s)
  127. "Convert field S from timestamp to Unix time and export to gnuplot."
  128. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  129. (defun org-babel-gnuplot-quote-tsv-field (s)
  130. "Quote field S for export to gnuplot."
  131. (unless (stringp s)
  132. (setq s (format "%s" s)))
  133. (if (string-match org-table-number-regexp s) s
  134. (if (string-match org-ts-regexp3 s)
  135. (org-babel-gnuplot-quote-timestamp-field s)
  136. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  137. (defun org-babel-gnuplot-table-to-data (table data-file params)
  138. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  139. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  140. (with-temp-file data-file
  141. (message "table = %S" table)
  142. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  143. (setq org-babel-gnuplot-timestamp-fmt (or
  144. (plist-get params :timefmt)
  145. "%Y-%m-%d-%H:%M:%S"))
  146. (insert (orgtbl-to-generic
  147. table
  148. (org-combine-plists
  149. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  150. params))))
  151. data-file)
  152. (provide 'org-babel-gnuplot)
  153. ;;; org-babel-gnuplot.el ends here