ob-asymptote.el 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ;;; ob-asymptote.el --- Babel Functions for Asymptote -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://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 <http://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. (eval-when-compile (require 'cl))
  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 (assoc :file params)))
  45. (format (or (and out-file
  46. (string-match ".+\\.\\(.+\\)" out-file)
  47. (match-string 1 out-file))
  48. "pdf"))
  49. (cmdline (cdr (assoc :cmdline params)))
  50. (in-file (org-babel-temp-file "asymptote-"))
  51. (cmd
  52. (concat "asy "
  53. (if out-file
  54. (concat
  55. "-globalwrite -f " format
  56. " -o " (org-babel-process-file-name out-file))
  57. "-V")
  58. " " cmdline
  59. " " (org-babel-process-file-name in-file))))
  60. (with-temp-file in-file
  61. (insert (org-babel-expand-body:generic
  62. body params
  63. (org-babel-variable-assignments:asymptote params))))
  64. (message cmd) (shell-command cmd)
  65. nil)) ;; signal that output has already been written to file
  66. (defun org-babel-prep-session:asymptote (_session _params)
  67. "Return an error if the :session header argument is set.
  68. Asymptote does not support sessions"
  69. (error "Asymptote does not support sessions"))
  70. (defun org-babel-variable-assignments:asymptote (params)
  71. "Return list of asymptote statements assigning the block's variables."
  72. (mapcar #'org-babel-asymptote-var-to-asymptote
  73. (mapcar #'cdr (org-babel-get-header params :var))))
  74. (defun org-babel-asymptote-var-to-asymptote (pair)
  75. "Convert an elisp value into an Asymptote variable.
  76. The elisp value PAIR is converted into Asymptote code specifying
  77. a variable of the same value."
  78. (let ((var (car pair))
  79. (val (let ((v (cdr pair)))
  80. (if (symbolp v) (symbol-name v) v))))
  81. (cond
  82. ((integerp val)
  83. (format "int %S=%S;" var val))
  84. ((floatp val)
  85. (format "real %S=%S;" var val))
  86. ((stringp val)
  87. (format "string %S=\"%s\";" var val))
  88. ((and (listp val) (not (listp (car val))))
  89. (let* ((type (org-babel-asymptote-define-type val))
  90. (fmt (if (eq 'string type) "\"%s\"" "%s"))
  91. (vect (mapconcat (lambda (e) (format fmt e)) val ", ")))
  92. (format "%s[] %S={%s};" type var vect)))
  93. ((listp val)
  94. (let* ((type (org-babel-asymptote-define-type val))
  95. (fmt (if (eq 'string type) "\"%s\"" "%s"))
  96. (array (mapconcat (lambda (row)
  97. (concat "{"
  98. (mapconcat (lambda (e) (format fmt e))
  99. row ", ")
  100. "}"))
  101. val ",")))
  102. (format "%S[][] %S={%s};" type var array))))))
  103. (defun org-babel-asymptote-define-type (data)
  104. "Determine type of DATA.
  105. DATA is a list. Return type as a symbol.
  106. The type is `string' if any element in DATA is
  107. a string. Otherwise, it is either `real', if some elements are
  108. floats, or `int'."
  109. (let* ((type 'int)
  110. find-type ; for byte-compiler
  111. (find-type
  112. (function
  113. (lambda (row)
  114. (catch 'exit
  115. (mapc (lambda (el)
  116. (cond ((listp el) (funcall find-type el))
  117. ((stringp el) (throw 'exit (setq type 'string)))
  118. ((floatp el) (setq type 'real))))
  119. row))))))
  120. (funcall find-type data) type))
  121. (provide 'ob-asymptote)
  122. ;;; ob-asymptote.el ends here