ob-asymptote.el 6.2 KB

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