ob-asymptote.el 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. ;;; ob-asymptote.el --- org-babel functions for asymptote evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  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 <http://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. (eval-when-compile (require 'cl))
  37. (declare-function orgtbl-to-generic "org-table" (table params))
  38. (declare-function org-combine-plists "org" (&rest plists))
  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 to use when evaluating a asymptote source block.")
  43. (defun org-babel-expand-body:asymptote (body params &optional processed-params)
  44. "Expand BODY according to PARAMS, return the expanded body."
  45. (let ((vars (nth 1 (or processed-params
  46. (org-babel-process-params params)))))
  47. (concat (mapconcat 'org-babel-asymptote-var-to-asymptote vars "\n")
  48. "\n" body "\n")))
  49. (defun org-babel-execute:asymptote (body params)
  50. "Execute a block of Asymptote code with org-babel. This function is
  51. called by `org-babel-execute-src-block'."
  52. (message "executing Asymptote source code block")
  53. (let* ((processed-params (org-babel-process-params params))
  54. (result-params (split-string (or (cdr (assoc :results params)) "")))
  55. (out-file (cdr (assoc :file params)))
  56. (format (or (and out-file
  57. (string-match ".+\\.\\(.+\\)" out-file)
  58. (match-string 1 out-file))
  59. "pdf"))
  60. (cmdline (cdr (assoc :cmdline params)))
  61. (in-file (make-temp-file "org-babel-asymptote"))
  62. (cmd (concat "asy "
  63. (if out-file
  64. (concat "-globalwrite -f " format " -o " out-file)
  65. "-V")
  66. " " cmdline " " in-file)))
  67. (with-temp-file in-file
  68. (insert (org-babel-expand-body:asymptote body params processed-params)))
  69. (message cmd) (shell-command cmd)
  70. out-file))
  71. (defun org-babel-prep-session:asymptote (session params)
  72. "Prepare a session named SESSION according to PARAMS."
  73. (error "Asymptote does not support sessions"))
  74. (defun org-babel-asymptote-var-to-asymptote (pair)
  75. "Convert an elisp val into a string of asymptote code specifying a var
  76. of the same value."
  77. (let ((var (car pair))
  78. (val (if (symbolp (cdr pair))
  79. (symbol-name (cdr pair))
  80. (cdr pair))))
  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. ((listp val)
  89. (let* ((dimension-2-p (not (null (cdr val))))
  90. (dim (if dimension-2-p "[][]" "[]"))
  91. (type (org-babel-asymptote-define-type val))
  92. (array (org-babel-asymptote-table-to-array
  93. val
  94. (if dimension-2-p '(:lstart "{" :lend "}," :llend "}")))))
  95. (format "%S%s %S=%s;" type dim var array))))))
  96. (defun org-babel-asymptote-table-to-array (table params)
  97. "Convert values of an elisp table into a string of an asymptote array.
  98. Empty cells are ignored."
  99. (labels ((atom-to-string (table)
  100. (cond
  101. ((null table) '())
  102. ((not (listp (car table)))
  103. (cons (if (and (stringp (car table))
  104. (not (string= (car table) "")))
  105. (format "\"%s\"" (car table))
  106. (format "%s" (car table)))
  107. (atom-to-string (cdr table))))
  108. (t
  109. (cons (atom-to-string (car table))
  110. (atom-to-string (cdr table))))))
  111. ;; Remove any empty row
  112. (fix-empty-lines (table)
  113. (delq nil (mapcar (lambda (l) (delq "" l)) table))))
  114. (orgtbl-to-generic
  115. (fix-empty-lines (atom-to-string table))
  116. (org-combine-plists '(:hline nil :sep "," :tstart "{" :tend "}") params))))
  117. (defun org-babel-asymptote-define-type (data)
  118. "Determine type of DATA. DATA is a list. Type symbol is
  119. returned as 'symbol. The type is usually the type of the first
  120. atom encountered, except for arrays of int where every cell must
  121. be of int type."
  122. (labels ((anything-but-int (el)
  123. (cond
  124. ((null el) nil)
  125. ((not (listp (car el)))
  126. (cond
  127. ((floatp (car el)) 'real)
  128. ((stringp (car el)) 'string)
  129. (t
  130. (anything-but-int (cdr el)))))
  131. (t
  132. (or (anything-but-int (car el))
  133. (anything-but-int (cdr el)))))))
  134. (or (anything-but-int data) 'int)))
  135. (provide 'ob-asymptote)
  136. ;; arch-tag: f2f5bd0d-78e8-412b-8e6c-6dadc94cc06b
  137. ;;; ob-asymptote.el ends here