org-babel-gnuplot.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. (org-babel-add-interpreter "gnuplot")
  33. (add-to-list 'org-babel-tangle-langs '("gnuplot" "gnuplot"))
  34. (defvar org-babel-default-header-args:gnuplot '((:results . "file") (:exports . "results"))
  35. "Default arguments to use when evaluating a gnuplot source block.")
  36. (defvar org-babel-gnuplot-timestamp-fmt nil)
  37. (defun org-babel-execute:gnuplot (body params)
  38. "Execute a block of Gnuplot code with org-babel. This function is
  39. called by `org-babel-execute-src-block'."
  40. (message "executing Gnuplot source code block")
  41. (let* ((vars (org-babel-ref-variables params))
  42. (result-params (split-string (or (cdr (assoc :results params)) "")))
  43. (out-file (cdr (assoc :file params)))
  44. (cmdline (cdr (assoc :cmdline params)))
  45. (in-file (make-temp-file "org-babel-ditaa"))
  46. (title (plist-get params :title))
  47. (lines (plist-get params :line))
  48. (sets (plist-get params :set))
  49. (x-labels (plist-get params :xlabels))
  50. (y-labels (plist-get params :ylabels))
  51. (time-ind (plist-get params :timeind)))
  52. ;; insert variables into code body
  53. (mapc
  54. (lambda (pair)
  55. (message "resolving %S" pair) ;; debugging
  56. (setq body
  57. (replace-regexp-in-string
  58. (regexp-quote (format "%s" (car pair)))
  59. (if (listp (cdr pair))
  60. (org-babel-gnuplot-table-to-data
  61. (cdr pair) (make-temp-file "org-babel-gnuplot") params)
  62. (cdr pair)) body)))
  63. vars)
  64. ;; append header argument settings to body
  65. (when title (add-to-script (format "set title '%s'" title))) ;; title
  66. (when lines (mapc (lambda (el) (add-to-script el)) lines)) ;; line
  67. (when sets ;; set
  68. (mapc (lambda (el) (add-to-script (format "set %s" el))) sets))
  69. (when x-labels ;; x labels (xtics)
  70. (add-to-script
  71. (format "set xtics (%s)"
  72. (mapconcat (lambda (pair)
  73. (format "\"%s\" %d" (cdr pair) (car pair)))
  74. x-labels ", "))))
  75. (when y-labels ;; y labels (ytics)
  76. (add-to-script
  77. (format "set ytics (%s)"
  78. (mapconcat (lambda (pair)
  79. (format "\"%s\" %d" (cdr pair) (car pair)))
  80. y-labels ", "))))
  81. (when time-ind ;; timestamp index
  82. (add-to-script "set xdata time")
  83. (add-to-script (concat "set timefmt \""
  84. (or timefmt ;; timefmt passed to gnuplot
  85. "%Y-%m-%d-%H:%M:%S") "\"")))
  86. ;; evaluate the code body with gnuplot
  87. (with-temp-buffer
  88. (insert (concat body "\n"))
  89. (gnuplot-mode)
  90. (gnuplot-send-buffer-to-gnuplot))
  91. out-file))
  92. (defun org-babel-prep-session:gnuplot (session params))
  93. (defun org-babel-gnuplot-quote-timestamp-field (s)
  94. "Convert field S from timestamp to Unix time and export to gnuplot."
  95. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  96. (defun org-babel-gnuplot-quote-tsv-field (s)
  97. "Quote field S for export to gnuplot."
  98. (unless (stringp s)
  99. (setq s (format "%s" s)))
  100. (if (string-match org-table-number-regexp s) s
  101. (if (string-match org-ts-regexp3 s)
  102. (org-babel-gnuplot-quote-timestamp-field s)
  103. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  104. (defun org-babel-gnuplot-table-to-data (table data-file params)
  105. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  106. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  107. (with-temp-file data-file
  108. (message "table = %S" table)
  109. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  110. (setq org-babel-gnuplot-timestamp-fmt (or
  111. (plist-get params :timefmt)
  112. "%Y-%m-%d-%H:%M:%S"))
  113. (insert (orgtbl-to-generic
  114. table
  115. (org-combine-plists
  116. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  117. params))))
  118. data-file)
  119. (provide 'org-babel-gnuplot)
  120. ;;; org-babel-gnuplot.el ends here