ob-eukleides.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. ;;; ob-eukleides.el --- Org-babel functions for eukleides evaluation
  2. ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
  3. ;; Author: Luis Anaya
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is not part of GNU Emacs.
  7. ;; This program is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating eukleides script.
  19. ;;
  20. ;; Inspired by Ian Yang's org-export-blocks-format-eukleides
  21. ;; http://www.emacswiki.org/emacs/org-export-blocks-format-eukleides.el
  22. ;;; Requirements:
  23. ;; eukleides | http://eukleides.org
  24. ;; eukleides | `org-eukleides-path' should point to the eukleides executablexs
  25. ;;; Code:
  26. (require 'ob)
  27. (require 'ob-eval)
  28. (defvar org-babel-default-header-args:eukleides
  29. '((:results . "file") (:exports . "results"))
  30. "Default arguments for evaluating a eukleides source block.")
  31. (defcustom org-eukleides-path nil
  32. "Path to the eukleides executable file."
  33. :group 'org-babel
  34. :type 'string)
  35. (defcustom org-eukleides-eps-to-raster nil
  36. "Command used to convert EPS to raster. Nil for no conversion."
  37. :group 'org-babel
  38. :type '(choice
  39. (repeat :tag "Shell Command Sequence" (string :tag "Shell Command"))
  40. (const :tag "sam2p" "a=%s;b=%s;sam2p ${a} ${b}" )
  41. (const :tag "NetPNM" "a=%s;b=%s;pstopnm -stdout ${a} | pnmtopng > ${b}" )
  42. (const :tag "None" nil)))
  43. (defun org-babel-execute:eukleides (body params)
  44. "Execute a block of eukleides code with org-babel.
  45. This function is called by `org-babel-execute-src-block'."
  46. (let* ((result-params (split-string (or (cdr (assq :results params)) "")))
  47. (out-file (or (cdr (assq :file params))
  48. (error "Eukleides requires a \":file\" header argument")))
  49. (cmdline (cdr (assq :cmdline params)))
  50. (in-file (org-babel-temp-file "eukleides-"))
  51. (java (or (cdr (assq :java params)) ""))
  52. (cmd (if (not org-eukleides-path)
  53. (error "`org-eukleides-path' is not set")
  54. (concat (expand-file-name org-eukleides-path)
  55. " -b --output="
  56. (org-babel-process-file-name
  57. (concat
  58. (file-name-sans-extension out-file) ".eps"))
  59. " "
  60. (org-babel-process-file-name in-file)))))
  61. (unless (file-exists-p org-eukleides-path)
  62. (error "Could not find eukleides at %s" org-eukleides-path))
  63. (if (string= (file-name-extension out-file) "png")
  64. (if org-eukleides-eps-to-raster
  65. (shell-command (format org-eukleides-eps-to-raster
  66. (concat (file-name-sans-extension out-file) ".eps")
  67. (concat (file-name-sans-extension out-file) ".png")))
  68. (error "Conversion to PNG not supported. Use a file with an EPS name")))
  69. (with-temp-file in-file (insert body))
  70. (message "%s" cmd) (org-babel-eval cmd "")
  71. nil)) ;; signal that output has already been written to file
  72. (defun org-babel-prep-session:eukleides (session params)
  73. "Return an error because eukleides does not support sessions."
  74. (error "Eukleides does not support sessions"))
  75. (provide 'ob-eukleides)
  76. ;;; ob-eukleides.el ends here