org-babel-gnuplot.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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-execute:gnuplot (body params)
  39. "Execute a block of Gnuplot code with org-babel. This function is
  40. called by `org-babel-execute-src-block'."
  41. (message "executing Gnuplot source code block")
  42. (let* ((vars (org-babel-ref-variables params))
  43. (result-params (split-string (or (cdr (assoc :results params)) "")))
  44. (out-file (cdr (assoc :file params)))
  45. (cmdline (cdr (assoc :cmdline params)))
  46. (in-file (make-temp-file "org-babel-ditaa"))
  47. (title (plist-get params :title))
  48. (lines (plist-get params :line))
  49. (sets (plist-get params :set))
  50. (x-labels (plist-get params :xlabels))
  51. (y-labels (plist-get params :ylabels))
  52. (time-ind (plist-get params :timeind)))
  53. ;; insert variables into code body
  54. (mapc
  55. (lambda (pair)
  56. (message "resolving %S" pair) ;; debugging
  57. (setq body
  58. (replace-regexp-in-string
  59. (regexp-quote (format "%s" (car pair)))
  60. (if (listp (cdr pair))
  61. (org-babel-gnuplot-table-to-data
  62. (cdr pair) (make-temp-file "org-babel-gnuplot") params)
  63. (cdr pair)) body)))
  64. vars)
  65. ;; append header argument settings to body
  66. (when title (add-to-script (format "set title '%s'" title))) ;; title
  67. (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line
  68. (when sets ;; set
  69. (mapc (lambda (el) (add-to-script (format "set %s" el))) sets))
  70. (when x-labels ;; x labels (xtics)
  71. (add-to-script
  72. (format "set xtics (%s)"
  73. (mapconcat (lambda (pair)
  74. (format "\"%s\" %d" (cdr pair) (car pair)))
  75. x-labels ", "))))
  76. (when y-labels ;; y labels (ytics)
  77. (add-to-script
  78. (format "set ytics (%s)"
  79. (mapconcat (lambda (pair)
  80. (format "\"%s\" %d" (cdr pair) (car pair)))
  81. y-labels ", "))))
  82. (when time-ind ;; timestamp index
  83. (add-to-script "set xdata time")
  84. (add-to-script (concat "set timefmt \""
  85. (or timefmt ;; timefmt passed to gnuplot
  86. "%Y-%m-%d-%H:%M:%S") "\"")))
  87. ;; evaluate the code body with gnuplot
  88. (with-temp-buffer
  89. (insert (concat body "\n"))
  90. (gnuplot-mode)
  91. (gnuplot-send-buffer-to-gnuplot))
  92. out-file))
  93. (defun org-babel-prep-session:gnuplot (session params)
  94. "Prepare SESSION according to the header arguments specified in PARAMS."
  95. (let* ((session (org-babel-gnuplot-initiate-session session))
  96. (vars (org-babel-ref-variables params))
  97. (var-lines (mapcar ;; define any variables
  98. (lambda (pair)
  99. (format "%s=%s"
  100. (car pair)
  101. (org-babel-ruby-var-to-ruby (cdr pair))))
  102. vars)))
  103. ;; (message "vars=%S" vars) ;; debugging
  104. (org-babel-comint-in-buffer session
  105. (sit-for .5) (goto-char (point-max))
  106. (mapc (lambda (var)
  107. (insert var) (comint-send-input nil t)
  108. (org-babel-comint-wait-for-output session)
  109. (sit-for .1) (goto-char (point-max))) var-lines))))
  110. (defun org-babel-gnuplot-initiate-session (&optional session)
  111. "If there is not a current inferior-process-buffer in SESSION
  112. then create. Return the initialized session."
  113. (unless (string= session "none")
  114. (let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
  115. (if (org-babel-comint-buffer-livep session-buffer)
  116. session-buffer
  117. (sit-for .5)
  118. (org-babel-ruby-initiate-session session)))))
  119. (defun org-babel-gnuplot-quote-timestamp-field (s)
  120. "Convert field S from timestamp to Unix time and export to gnuplot."
  121. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  122. (defun org-babel-gnuplot-quote-tsv-field (s)
  123. "Quote field S for export to gnuplot."
  124. (unless (stringp s)
  125. (setq s (format "%s" s)))
  126. (if (string-match org-table-number-regexp s) s
  127. (if (string-match org-ts-regexp3 s)
  128. (org-babel-gnuplot-quote-timestamp-field s)
  129. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  130. (defun org-babel-gnuplot-table-to-data (table data-file params)
  131. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  132. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  133. (with-temp-file data-file
  134. (message "table = %S" table)
  135. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  136. (setq org-babel-gnuplot-timestamp-fmt (or
  137. (plist-get params :timefmt)
  138. "%Y-%m-%d-%H:%M:%S"))
  139. (insert (orgtbl-to-generic
  140. table
  141. (org-combine-plists
  142. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  143. params))))
  144. data-file)
  145. (provide 'org-babel-gnuplot)
  146. ;;; org-babel-gnuplot.el ends here