ob-dot.el 2.9 KB

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