dotemacs

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

ob-eukleides.el (3788B)


      1 ;;; ob-eukleides.el --- Org-babel functions for eukleides evaluation
      2 
      3 ;; Copyright (C) 2010-2021  Free Software Foundation, Inc.
      4 
      5 ;; Author: Luis Anaya
      6 ;; Keywords: literate programming, reproducible research
      7 ;; Homepage: https://git.sr.ht/~bzg/org-contrib
      8 
      9 ;; This file is not part of GNU Emacs.
     10 
     11 ;; This program 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 ;; This program 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 eukleides script.
     27 ;;
     28 ;; Inspired by Ian Yang's org-export-blocks-format-eukleides
     29 ;; https://www.emacswiki.org/emacs/org-export-blocks-format-eukleides.el
     30 
     31 ;;; Requirements:
     32 
     33 ;; eukleides     | http://eukleides.org
     34 ;; eukleides     | `org-eukleides-path' should point to the eukleides executablexs
     35 
     36 ;;; Code:
     37 (require 'ob)
     38 (require 'ob-eval)
     39 
     40 (defvar org-babel-default-header-args:eukleides
     41   '((:results . "file") (:exports . "results"))
     42   "Default arguments for evaluating a eukleides source block.")
     43 
     44 (defcustom org-eukleides-path nil
     45   "Path to the eukleides executable file."
     46   :group 'org-babel
     47   :type 'string)
     48 
     49 (defcustom org-eukleides-eps-to-raster nil
     50   "Command used to convert EPS to raster. Nil for no conversion."
     51   :group 'org-babel
     52   :type '(choice
     53          (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
     54          (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b}" )
     55          (const :tag "NetPNM"  "a=%s;b=%s;pstopnm -stdout ${a} | pnmtopng  > ${b}" )
     56          (const :tag "None" nil)))
     57 
     58 (defun org-babel-execute:eukleides (body params)
     59   "Execute a block of eukleides code with org-babel.
     60 This function is called by `org-babel-execute-src-block'."
     61   (let* ((result-params (split-string (or (cdr (assq :results params)) "")))
     62 	 (out-file (or (cdr (assq :file params))
     63 		       (error "Eukleides requires a \":file\" header argument")))
     64 	 (cmdline (cdr (assq :cmdline params)))
     65 	 (in-file (org-babel-temp-file "eukleides-"))
     66 	 (java (or (cdr (assq :java params)) ""))
     67 	 (cmd (if (not org-eukleides-path)
     68 		  (error "`org-eukleides-path' is not set")
     69 		(concat (expand-file-name org-eukleides-path)
     70                 " -b --output="
     71                 (org-babel-process-file-name
     72                  (concat
     73                   (file-name-sans-extension out-file) ".eps"))
     74                 " "
     75                 (org-babel-process-file-name in-file)))))
     76     (unless (file-exists-p org-eukleides-path)
     77       (error "Could not find eukleides at %s" org-eukleides-path))
     78 
     79     (if (string= (file-name-extension out-file) "png")
     80         (if org-eukleides-eps-to-raster
     81             (shell-command (format org-eukleides-eps-to-raster
     82                                     (concat (file-name-sans-extension out-file) ".eps")
     83                                     (concat (file-name-sans-extension out-file) ".png")))
     84           (error "Conversion to PNG not supported.  Use a file with an EPS name")))
     85 
     86     (with-temp-file in-file (insert body))
     87     (message "%s" cmd) (org-babel-eval cmd "")
     88     nil)) ;; signal that output has already been written to file
     89 
     90 (defun org-babel-prep-session:eukleides (session params)
     91   "Return an error because eukleides does not support sessions."
     92   (error "Eukleides does not support sessions"))
     93 
     94 (provide 'ob-eukleides)
     95 
     96 
     97 
     98 ;;; ob-eukleides.el ends here