dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ob-dot.el (3100B)


      1 ;;; ob-dot.el --- Babel Functions for dot            -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2009-2023 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     (mapc
     56      (lambda (pair)
     57        (let ((name (symbol-name (car pair)))
     58 	     (value (cdr pair)))
     59 	 (setq body
     60 	       (replace-regexp-in-string
     61 		(concat "$" (regexp-quote name))
     62 		(if (stringp value) value (format "%S" value))
     63 		body
     64 		t
     65 		t))))
     66      vars)
     67     body))
     68 
     69 (defun org-babel-execute:dot (body params)
     70   "Execute a block of Dot code with org-babel.
     71 This function is called by `org-babel-execute-src-block'."
     72   (let* ((out-file (cdr (or (assq :file params)
     73 			    (error "You need to specify a :file parameter"))))
     74 	 (cmdline (or (cdr (assq :cmdline params))
     75 		      (format "-T%s" (file-name-extension out-file))))
     76 	 (cmd (or (cdr (assq :cmd params)) "dot"))
     77 	 (coding-system-for-read 'utf-8) ;use utf-8 with sub-processes
     78 	 (coding-system-for-write 'utf-8)
     79 	 (in-file (org-babel-temp-file "dot-")))
     80     (with-temp-file in-file
     81       (insert (org-babel-expand-body:dot body params)))
     82     (org-babel-eval
     83      (concat cmd
     84 	     " " (org-babel-process-file-name in-file)
     85 	     " " cmdline
     86 	     " -o " (org-babel-process-file-name out-file)) "")
     87     nil)) ;; signal that output has already been written to file
     88 
     89 (defun org-babel-prep-session:dot (_session _params)
     90   "Return an error because Dot does not support sessions."
     91   (error "Dot does not support sessions"))
     92 
     93 (provide 'ob-dot)
     94 
     95 ;;; ob-dot.el ends here