ob-dot.el (3312B)
1 ;;; ob-dot.el --- Babel Functions for dot -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2009-2024 Free Software Foundation, Inc. 4 5 ;; Author: Eric Schulte 6 ;; Maintainer: Justin Abrahms <justin@abrah.ms> 7 ;; Keywords: literate programming, reproducible research 8 ;; URL: https://orgmode.org 9 10 ;; This file is part of GNU Emacs. 11 12 ;; GNU Emacs is free software: you can redistribute it and/or modify 13 ;; it under the terms of the GNU General Public License as published by 14 ;; the Free Software Foundation, either version 3 of the License, or 15 ;; (at your option) any later version. 16 17 ;; GNU Emacs is distributed in the hope that it will be useful, 18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 ;; GNU General Public License for more details. 21 22 ;; You should have received a copy of the GNU General Public License 23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 24 25 ;;; Commentary: 26 27 ;; Org-Babel support for evaluating dot source code. 28 ;; 29 ;; For information on dot see https://www.graphviz.org/ 30 ;; 31 ;; This differs from most standard languages in that 32 ;; 33 ;; 1) there is no such thing as a "session" in dot 34 ;; 35 ;; 2) we are generally only going to return results of type "file" 36 ;; 37 ;; 3) we are adding the "file" and "cmdline" header arguments 38 ;; 39 ;; 4) there are no variables (at least for now) 40 41 ;;; Code: 42 43 (require 'org-macs) 44 (org-assert-version) 45 46 (require 'ob) 47 48 (defvar org-babel-default-header-args:dot 49 '((:results . "file") (:exports . "results")) 50 "Default arguments to use when evaluating a dot source block.") 51 52 (defun org-babel-expand-body:dot (body params) 53 "Expand BODY according to PARAMS, return the expanded body." 54 (let ((vars (org-babel--get-vars params)) 55 (prologue (cdr (assq :prologue params))) 56 (epilogue (cdr (assq :epilogue params)))) 57 (mapc 58 (lambda (pair) 59 (let ((name (symbol-name (car pair))) 60 (value (cdr pair))) 61 (setq body 62 (replace-regexp-in-string 63 (concat "$" (regexp-quote name)) 64 (if (stringp value) value (format "%S" value)) 65 body 66 t 67 t)))) 68 vars) 69 (concat 70 (and prologue (concat prologue "\n")) 71 body 72 (and epilogue (concat "\n" epilogue "\n"))))) 73 74 (defun org-babel-execute:dot (body params) 75 "Execute Dot BODY with org-babel according to PARAMS. 76 This function is called by `org-babel-execute-src-block'." 77 (let* ((out-file (cdr (or (assq :file params) 78 (error "You need to specify a :file parameter")))) 79 (cmdline (or (cdr (assq :cmdline params)) 80 (format "-T%s" (file-name-extension out-file)))) 81 (cmd (or (cdr (assq :cmd params)) "dot")) 82 (coding-system-for-read 'utf-8) ;use utf-8 with sub-processes 83 (coding-system-for-write 'utf-8) 84 (in-file (org-babel-temp-file "dot-"))) 85 (with-temp-file in-file 86 (insert (org-babel-expand-body:dot body params))) 87 (org-babel-eval 88 (concat cmd 89 " " (org-babel-process-file-name in-file) 90 " " cmdline 91 " -o " (org-babel-process-file-name out-file)) "") 92 nil)) ;; signal that output has already been written to file 93 94 (defun org-babel-prep-session:dot (_session _params) 95 "Return an error because Dot does not support sessions." 96 (error "Dot does not support sessions")) 97 98 (provide 'ob-dot) 99 100 ;;; ob-dot.el ends here