ob-dot.el 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;;; ob-dot.el --- Babel Functions for dot -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Maintainer: Justin Abrahms <justin@abrah.ms>
  5. ;; Keywords: literate programming, reproducible research
  6. ;; URL: https://orgmode.org
  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 <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating dot source code.
  20. ;;
  21. ;; For information on dot see https://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 'org-macs)
  34. (org-assert-version)
  35. (require 'ob)
  36. (defvar org-babel-default-header-args:dot
  37. '((:results . "file") (:exports . "results"))
  38. "Default arguments to use when evaluating a dot source block.")
  39. (defun org-babel-expand-body:dot (body params)
  40. "Expand BODY according to PARAMS, return the expanded body."
  41. (let ((vars (org-babel--get-vars params)))
  42. (mapc
  43. (lambda (pair)
  44. (let ((name (symbol-name (car pair)))
  45. (value (cdr pair)))
  46. (setq body
  47. (replace-regexp-in-string
  48. (concat "$" (regexp-quote name))
  49. (if (stringp value) value (format "%S" value))
  50. body
  51. t
  52. t))))
  53. vars)
  54. body))
  55. (defun org-babel-execute:dot (body params)
  56. "Execute a block of Dot code with org-babel.
  57. This function is called by `org-babel-execute-src-block'."
  58. (let* ((out-file (cdr (or (assq :file params)
  59. (error "You need to specify a :file parameter"))))
  60. (cmdline (or (cdr (assq :cmdline params))
  61. (format "-T%s" (file-name-extension out-file))))
  62. (cmd (or (cdr (assq :cmd params)) "dot"))
  63. (coding-system-for-read 'utf-8) ;use utf-8 with sub-processes
  64. (coding-system-for-write 'utf-8)
  65. (in-file (org-babel-temp-file "dot-")))
  66. (with-temp-file in-file
  67. (insert (org-babel-expand-body:dot body params)))
  68. (org-babel-eval
  69. (concat cmd
  70. " " (org-babel-process-file-name in-file)
  71. " " cmdline
  72. " -o " (org-babel-process-file-name out-file)) "")
  73. nil)) ;; signal that output has already been written to file
  74. (defun org-babel-prep-session:dot (_session _params)
  75. "Return an error because Dot does not support sessions."
  76. (error "Dot does not support sessions"))
  77. (provide 'ob-dot)
  78. ;;; ob-dot.el ends here