ob-asymptote.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation
  2. ;; Copyright (C) 2009-2011 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. (declare-function orgtbl-to-generic "org-table" (table params))
  37. (declare-function org-combine-plists "org" (&rest plists))
  38. (defvar org-babel-tangle-lang-exts)
  39. (add-to-list 'org-babel-tangle-lang-exts '("asymptote" . "asy"))
  40. (defvar org-babel-default-header-args:asymptote
  41. '((:results . "file") (:exports . "results"))
  42. "Default arguments when evaluating an Asymptote source block.")
  43. (defun org-babel-execute:asymptote (body params)
  44. "Execute a block of Asymptote code.
  45. This function is called by `org-babel-execute-src-block'."
  46. (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
  47. (out-file (cdr (assoc :file params)))
  48. (format (or (and out-file
  49. (string-match ".+\\.\\(.+\\)" out-file)
  50. (match-string 1 out-file))
  51. "pdf"))
  52. (cmdline (cdr (assoc :cmdline params)))
  53. (in-file (org-babel-temp-file "asymptote-"))
  54. (cmd
  55. (concat "asy "
  56. (if out-file
  57. (concat
  58. "-globalwrite -f " format
  59. " -o " (org-babel-process-file-name out-file))
  60. "-V")
  61. " " cmdline
  62. " " (org-babel-process-file-name in-file))))
  63. (with-temp-file in-file
  64. (insert (org-babel-expand-body:generic
  65. body params
  66. (org-babel-variable-assignments:asymptote params))))
  67. (message cmd) (shell-command cmd)
  68. nil)) ;; signal that output has already been written to file
  69. (defun org-babel-prep-session:asymptote (session params)
  70. "Return an error if the :session header argument is set.
  71. Asymptote does not support sessions"
  72. (error "Asymptote does not support sessions"))
  73. (defun org-babel-variable-assignments:asymptote (params)
  74. "Return list of asymptote statements assigning the block's variables"
  75. (mapcar #'org-babel-asymptote-var-to-asymptote
  76. (mapcar #'cdr (org-babel-get-header params :var))))
  77. (defun org-babel-asymptote-var-to-asymptote (pair)
  78. "Convert an elisp value into an Asymptote variable.
  79. The elisp value PAIR is converted into Asymptote code specifying
  80. a variable of the same value."
  81. (let ((var (car pair))
  82. (val (if (symbolp (cdr pair))
  83. (symbol-name (cdr pair))
  84. (cdr pair))))
  85. (cond
  86. ((integerp val)
  87. (format "int %S=%S;" var val))
  88. ((floatp val)
  89. (format "real %S=%S;" var val))
  90. ((stringp val)
  91. (format "string %S=\"%s\";" var val))
  92. ((listp val)
  93. (let* ((dimension-2-p (cdr val))
  94. (dim (if dimension-2-p "[][]" "[]"))
  95. (type (org-babel-asymptote-define-type val))
  96. (array (org-babel-asymptote-table-to-array
  97. val type
  98. (if dimension-2-p '(:lstart "{" :lend "}," :llend "}")))))
  99. (format "%S%s %S=%s;" type dim var array))))))
  100. (defun org-babel-asymptote-table-to-array (table type params)
  101. "Convert values of TABLE into a string of an asymptote array.
  102. TABLE is a list whose atoms are assumed to be of type
  103. TYPE. PARAMS is a plist of parameters that can influence the
  104. conversion.
  105. Empty cells are ignored."
  106. (labels ((atom-to-string (table)
  107. (cond
  108. ((null table) '())
  109. ((not (listp (car table)))
  110. (cons (if (or (eq type 'string)
  111. (and (stringp (car table))
  112. (not (string= (car table) ""))))
  113. (format "\"%s\"" (car table))
  114. (format "%s" (car table)))
  115. (atom-to-string (cdr table))))
  116. (t
  117. (cons (atom-to-string (car table))
  118. (atom-to-string (cdr table))))))
  119. ;; Remove any empty row
  120. (fix-empty-lines (table)
  121. (delq nil (mapcar (lambda (l) (delq "" l)) table))))
  122. (orgtbl-to-generic
  123. (fix-empty-lines (atom-to-string table))
  124. (org-combine-plists '(:hline nil :sep "," :tstart "{" :tend "}") params))))
  125. (defun org-babel-asymptote-define-type (data)
  126. "Determine type of DATA.
  127. DATA is a list. Return type as a symbol.
  128. The type is `string' if any element in DATA is
  129. a string. Otherwise, it is either `float', if some elements are
  130. floats, or `int'."
  131. (let* ((type 'int)
  132. (find-type
  133. (lambda (row)
  134. (catch 'exit
  135. (mapc (lambda (el)
  136. (cond ((listp el) (funcall find-type el))
  137. ((stringp el) (throw 'exit (setq type 'string)))
  138. ((floatp el) (setq type 'float))))
  139. row)))))
  140. (funcall find-type data) type))
  141. (provide 'ob-asymptote)
  142. ;;; ob-asymptote.el ends here