ob-asymptote.el 4.6 KB

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