org-babel-gnuplot.el 7.2 KB

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