org-babel-gnuplot.el 6.1 KB

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