org-babel-gnuplot.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ;; insert variables into code body
  47. (mapc
  48. (lambda (pair)
  49. (message "resolving %S" pair) ;; debugging
  50. (setq body
  51. (replace-regexp-in-string
  52. (regexp-quote (format "%s" (car pair)))
  53. (if (listp (cdr pair))
  54. (org-babel-gnuplot-table-to-data
  55. (cdr pair) (make-temp-file "org-babel-gnuplot") params)
  56. (cdr pair)) body)))
  57. vars)
  58. (with-temp-buffer ;; evaluate the code body with gnuplot
  59. (insert (concat body "\n"))
  60. (gnuplot-mode)
  61. (gnuplot-send-buffer-to-gnuplot))
  62. out-file))
  63. (defun org-babel-prep-session:gnuplot (session params))
  64. (defun org-babel-gnuplot-quote-timestamp-field (s)
  65. "Convert field S from timestamp to Unix time and export to gnuplot."
  66. (format-time-string org-babel-gnuplot-timestamp-fmt (org-time-string-to-time s)))
  67. (defun org-babel-gnuplot-quote-tsv-field (s)
  68. "Quote field S for export to gnuplot."
  69. (unless (stringp s)
  70. (setq s (format "%s" s)))
  71. (if (string-match org-table-number-regexp s) s
  72. (if (string-match org-ts-regexp3 s)
  73. (org-babel-gnuplot-quote-timestamp-field s)
  74. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\""))))
  75. (defun org-babel-gnuplot-table-to-data (table data-file params)
  76. "Export TABLE to DATA-FILE in a format readable by gnuplot.
  77. Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE."
  78. (with-temp-file data-file
  79. (message "table = %S" table)
  80. (make-local-variable 'org-babel-gnuplot-timestamp-fmt)
  81. (setq org-babel-gnuplot-timestamp-fmt (or
  82. (plist-get params :timefmt)
  83. "%Y-%m-%d-%H:%M:%S"))
  84. (insert (orgtbl-to-generic
  85. table
  86. (org-combine-plists
  87. '(:sep "\t" :fmt org-babel-gnuplot-quote-tsv-field)
  88. params))))
  89. data-file)
  90. (provide 'org-babel-gnuplot)
  91. ;;; org-babel-gnuplot.el ends here