ob-asymptote.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; ob-asymptote.el --- Babel Functions for Asymptote -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Maintainer: Luc Pellissier <luc.pellissier@crans.org>
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating asymptote source code.
  20. ;;
  21. ;; This differs from most standard languages in that
  22. ;;
  23. ;; 1) there is no such thing as a "session" in asymptote
  24. ;;
  25. ;; 2) we are generally only going to return results of type "file"
  26. ;;
  27. ;; 3) we are adding the "file" and "cmdline" header arguments, if file
  28. ;; is omitted then the -V option is passed to the asy command for
  29. ;; interactive viewing
  30. ;;; Requirements:
  31. ;; - The asymptote program :: http://asymptote.sourceforge.net/
  32. ;;
  33. ;; - asy-mode :: Major mode for editing asymptote files
  34. ;;; Code:
  35. (require 'ob)
  36. (defvar org-babel-tangle-lang-exts)
  37. (add-to-list 'org-babel-tangle-lang-exts '("asymptote" . "asy"))
  38. (defvar org-babel-default-header-args:asymptote
  39. '((:results . "file") (:exports . "results"))
  40. "Default arguments when evaluating an Asymptote source block.")
  41. (defun org-babel-execute:asymptote (body params)
  42. "Execute a block of Asymptote code.
  43. This function is called by `org-babel-execute-src-block'."
  44. (let* ((out-file (cdr (assq :file params)))
  45. (format (or (file-name-extension out-file)
  46. "pdf"))
  47. (cmdline (cdr (assq :cmdline params)))
  48. (in-file (org-babel-temp-file "asymptote-"))
  49. (cmd
  50. (concat "asy "
  51. (if out-file
  52. (concat
  53. "-globalwrite -f " format
  54. " -o " (org-babel-process-file-name out-file))
  55. "-V")
  56. " " cmdline
  57. " " (org-babel-process-file-name in-file))))
  58. (with-temp-file in-file
  59. (insert (org-babel-expand-body:generic
  60. body params
  61. (org-babel-variable-assignments:asymptote params))))
  62. (message cmd) (shell-command cmd)
  63. nil)) ;; signal that output has already been written to file
  64. (defun org-babel-prep-session:asymptote (_session _params)
  65. "Return an error if the :session header argument is set.
  66. Asymptote does not support sessions."
  67. (error "Asymptote does not support sessions"))
  68. (defun org-babel-variable-assignments:asymptote (params)
  69. "Return list of asymptote statements assigning the block's variables."
  70. (mapcar #'org-babel-asymptote-var-to-asymptote
  71. (org-babel--get-vars params)))
  72. (defun org-babel-asymptote-var-to-asymptote (pair)
  73. "Convert an elisp value into an Asymptote variable.
  74. The elisp value PAIR is converted into Asymptote code specifying
  75. a variable of the same value."
  76. (let ((var (car pair))
  77. (val (let ((v (cdr pair)))
  78. (if (symbolp v) (symbol-name v) v))))
  79. (cond
  80. ((integerp val)
  81. (format "int %S=%S;" var val))
  82. ((floatp val)
  83. (format "real %S=%S;" var val))
  84. ((stringp val)
  85. (format "string %S=\"%s\";" var val))
  86. ((and (listp val) (not (listp (car val))))
  87. (let* ((type (org-babel-asymptote-define-type val))
  88. (fmt (if (eq 'string type) "\"%s\"" "%s"))
  89. (vect (mapconcat (lambda (e) (format fmt e)) val ", ")))
  90. (format "%s[] %S={%s};" type var vect)))
  91. ((listp val)
  92. (let* ((type (org-babel-asymptote-define-type val))
  93. (fmt (if (eq 'string type) "\"%s\"" "%s"))
  94. (array (mapconcat (lambda (row)
  95. (concat "{"
  96. (mapconcat (lambda (e) (format fmt e))
  97. row ", ")
  98. "}"))
  99. val ",")))
  100. (format "%S[][] %S={%s};" type var array))))))
  101. (defun org-babel-asymptote-define-type (data)
  102. "Determine type of DATA.
  103. DATA is a list. Return type as a symbol.
  104. The type is `string' if any element in DATA is a string.
  105. Otherwise, it is either `real', if some elements are floats, or
  106. `int'."
  107. (letrec ((type 'int)
  108. (find-type
  109. (lambda (row)
  110. (dolist (e row type)
  111. (cond ((listp e) (setq type (funcall find-type e)))
  112. ((stringp e) (throw 'exit 'string))
  113. ((floatp e) (setq type 'real)))))))
  114. (catch 'exit (funcall find-type data)) type))
  115. (provide 'ob-asymptote)
  116. ;;; ob-asymptote.el ends here