ob-dot.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ;;; ob-dot.el --- org-babel functions for dot evaluation
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating dot source code.
  19. ;;
  20. ;; For information on dot see http://www.graphviz.org/
  21. ;;
  22. ;; This differs from most standard languages in that
  23. ;;
  24. ;; 1) there is no such thing as a "session" in dot
  25. ;;
  26. ;; 2) we are generally only going to return results of type "file"
  27. ;;
  28. ;; 3) we are adding the "file" and "cmdline" header arguments
  29. ;;
  30. ;; 4) there are no variables (at least for now)
  31. ;;; Code:
  32. (require 'ob)
  33. (require 'ob-eval)
  34. (defvar org-babel-default-header-args:dot
  35. '((:results . "file") (:exports . "results"))
  36. "Default arguments to use when evaluating a dot source block.")
  37. (defun org-babel-expand-body:dot (body params)
  38. "Expand BODY according to PARAMS, return the expanded body."
  39. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  40. (mapc
  41. (lambda (pair)
  42. (let ((name (symbol-name (car pair)))
  43. (value (cdr pair)))
  44. (setq body
  45. (replace-regexp-in-string
  46. (concat "\$" (regexp-quote name))
  47. (if (stringp value) value (format "%S" value))
  48. body))))
  49. vars)
  50. body))
  51. (defun org-babel-execute:dot (body params)
  52. "Execute a block of Dot code with org-babel.
  53. This function is called by `org-babel-execute-src-block'."
  54. (let* ((result-params (cdr (assoc :result-params params)))
  55. (out-file (cdr (assoc :file params)))
  56. (cmdline (or (cdr (assoc :cmdline params))
  57. (format "-T%s" (file-name-extension out-file))))
  58. (cmd (or (cdr (assoc :cmd params)) "dot"))
  59. (in-file (org-babel-temp-file "dot-")))
  60. (with-temp-file in-file
  61. (insert (org-babel-expand-body:dot body params)))
  62. (org-babel-eval
  63. (concat cmd
  64. " " (org-babel-process-file-name in-file)
  65. " " cmdline
  66. " -o " (org-babel-process-file-name out-file)) "")
  67. nil)) ;; signal that output has already been written to file
  68. (defun org-babel-prep-session:dot (session params)
  69. "Return an error because Dot does not support sessions."
  70. (error "Dot does not support sessions"))
  71. (provide 'ob-dot)
  72. ;;; ob-dot.el ends here