ob-asymptote.el 6.2 KB

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