config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ob-ditaa.el (4182B)


      1 ;;; ob-ditaa.el --- Babel Functions for ditaa        -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2009-2024 Free Software Foundation, Inc.
      4 
      5 ;; Author: Eric Schulte
      6 ;; Keywords: literate programming, reproducible research
      7 ;; URL: https://orgmode.org
      8 
      9 ;; This file is part of GNU Emacs.
     10 
     11 ;; GNU Emacs is free software: you can redistribute it and/or modify
     12 ;; it under the terms of the GNU General Public License as published by
     13 ;; the Free Software Foundation, either version 3 of the License, or
     14 ;; (at your option) any later version.
     15 
     16 ;; GNU Emacs is distributed in the hope that it will be useful,
     17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 ;; GNU General Public License for more details.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     23 
     24 ;;; Commentary:
     25 
     26 ;; Org-Babel support for evaluating ditaa source code.
     27 ;;
     28 ;; This differs from most standard languages in that
     29 ;;
     30 ;; 1) there is no such thing as a "session" in ditaa
     31 ;;
     32 ;; 2) we are generally only going to return results of type "file"
     33 ;;
     34 ;; 3) we are adding the "file" and "cmdline" header arguments
     35 ;;
     36 ;; 4) there are no variables (at least for now)
     37 
     38 ;;; Code:
     39 
     40 (require 'org-macs)
     41 (org-assert-version)
     42 
     43 (require 'ob)
     44 (require 'org-compat)
     45 
     46 (defvar org-babel-default-header-args:ditaa
     47   '((:results . "file")
     48     (:exports . "results")
     49     (:java . "-Dfile.encoding=UTF-8"))
     50   "Default arguments for evaluating a ditaa source block.")
     51 
     52 (defcustom org-ditaa-jar-path (expand-file-name
     53 			       "ditaa.jar"
     54 			       (file-name-as-directory
     55 				(expand-file-name
     56 				 "scripts"
     57 				 (file-name-as-directory
     58 				  (expand-file-name
     59 				   "../contrib"
     60 				   (file-name-directory (org-find-library-dir "org")))))))
     61   "Path to the ditaa jar executable."
     62   :group 'org-babel
     63   :type 'string)
     64 
     65 (defcustom org-babel-ditaa-java-cmd "java"
     66   "Java executable to use when evaluating ditaa blocks."
     67   :group 'org-babel
     68   :type 'string)
     69 
     70 (defcustom org-ditaa-eps-jar-path
     71   (expand-file-name "DitaaEps.jar" (file-name-directory org-ditaa-jar-path))
     72   "Path to the DitaaEps.jar executable."
     73   :group 'org-babel
     74   :version "24.4"
     75   :package-version '(Org . "8.0")
     76   :type 'string)
     77 
     78 (defcustom org-ditaa-jar-option "-jar"
     79   "Option for the ditaa jar file.
     80 Do not leave leading or trailing spaces in this string."
     81   :group 'org-babel
     82   :version "24.1"
     83   :type 'string)
     84 
     85 (defun org-babel-execute:ditaa (body params)
     86   "Execute BODY of Ditaa code with org-babel according to PARAMS.
     87 This function is called by `org-babel-execute-src-block'."
     88   (let* ((out-file (or (cdr (assq :file params))
     89 		       (error
     90 			"Ditaa code block requires :file header argument")))
     91 	 (cmdline (cdr (assq :cmdline params)))
     92 	 (java (cdr (assq :java params)))
     93 	 (in-file (org-babel-temp-file "ditaa-"))
     94 	 (eps (cdr (assq :eps params)))
     95 	 (eps-file (when eps
     96 		     (org-babel-process-file-name (concat in-file ".eps"))))
     97 	 (pdf-cmd (when (and (or (string= (file-name-extension out-file) "pdf")
     98 				 (cdr (assq :pdf params))))
     99 		    (concat
    100 		     "epstopdf"
    101 		     " " eps-file
    102 		     " -o=" (org-babel-process-file-name out-file))))
    103 	 (cmd (concat org-babel-ditaa-java-cmd
    104 		      " " java " " org-ditaa-jar-option " "
    105 		      (shell-quote-argument
    106 		       (expand-file-name
    107 			(if eps org-ditaa-eps-jar-path org-ditaa-jar-path)))
    108 		      " " cmdline
    109 		      " " (org-babel-process-file-name in-file)
    110 		      " " (if pdf-cmd
    111 			      eps-file
    112 			    (org-babel-process-file-name out-file)))))
    113     (unless (file-exists-p org-ditaa-jar-path)
    114       (error "Could not find ditaa.jar at %s" org-ditaa-jar-path))
    115     (with-temp-file in-file (insert body))
    116     (unless noninteractive (message cmd))
    117     (shell-command cmd)
    118     (when pdf-cmd (unless noninteractive (message pdf-cmd)) (shell-command pdf-cmd))
    119     nil)) ;; signal that output has already been written to file
    120 
    121 (defun org-babel-prep-session:ditaa (_session _params)
    122   "Return an error because ditaa does not support sessions."
    123   (error "Ditaa does not support sessions"))
    124 
    125 (provide 'ob-ditaa)
    126 
    127 ;;; ob-ditaa.el ends here