org-babel-exp.el 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. ;;; org-babel-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program 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, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; for more information see the comments in org-babel.el
  24. ;;; Code:
  25. (require 'org-babel)
  26. (require 'org-exp-blocks)
  27. (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
  28. (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
  29. (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
  30. (defvar org-babel-function-def-export-keyword "function"
  31. "When exporting a source block function, this keyword will
  32. appear in the exported version in the place of source name
  33. line. A source block is considered to be a source block function
  34. if the source name is present and is followed by a parenthesized
  35. argument list. The parentheses may be empty or contain
  36. whitespace. An example is the following which generates n random
  37. (uniform) numbers.
  38. #+source: rand(n)
  39. #+begin_src R
  40. runif(n)
  41. #+end_src
  42. ")
  43. (defvar org-babel-function-def-export-indent 4
  44. "When exporting a source block function, the block contents
  45. will be indented by this many characters. See
  46. `org-babel-function-def-export-name' for the definition of a
  47. source block function.")
  48. (defvar obe-marker nil)
  49. (defun org-babel-exp-src-blocks (body &rest headers)
  50. "Process src block for export. Depending on the 'export'
  51. headers argument in replace the source code block with...
  52. both ---- display the code and the results
  53. code ---- the default, display the code inside the block but do
  54. not process
  55. results - just like none only the block is run on export ensuring
  56. that it's results are present in the org-mode buffer
  57. none ----- do not display either code or results upon export"
  58. (interactive)
  59. (message "org-babel-exp processing...")
  60. (when (member (first headers) org-babel-interpreters)
  61. (save-excursion
  62. (goto-char (match-beginning 0))
  63. (org-babel-exp-do-export (org-babel-get-src-block-info) 'block))))
  64. (defun org-babel-exp-inline-src-blocks (start end)
  65. "Process inline src blocks between START and END for export.
  66. See `org-babel-exp-src-blocks' for export options, currently the
  67. options and are taken from `org-babel-defualt-inline-header-args'."
  68. (interactive)
  69. (save-excursion
  70. (goto-char start)
  71. (while (and (< (point) end)
  72. (re-search-forward org-babel-inline-src-block-regexp end t))
  73. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  74. (replacement (save-match-data
  75. (org-babel-exp-do-export info 'inline))))
  76. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  77. (replace-match replacement t t nil 1)))))
  78. (defun org-babel-exp-lob-one-liners (start end)
  79. "Process #+lob (Library of Babel) calls between START and END for export.
  80. See `org-babel-exp-src-blocks' for export options. Currently the
  81. options are taken from `org-babel-default-header-args'."
  82. (interactive)
  83. (let (replacement)
  84. (save-excursion
  85. (goto-char start)
  86. (while (and (< (point) end)
  87. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  88. (setq replacement
  89. (save-match-data
  90. (org-babel-exp-do-export
  91. (list "emacs-lisp" "results"
  92. (org-babel-merge-params
  93. org-babel-default-header-args
  94. (org-babel-parse-header-arguments
  95. (org-babel-clean-text-properties
  96. (concat ":var results="
  97. (mapconcat #'identity
  98. (org-babel-lob-get-info) " "))))))
  99. 'lob)))
  100. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  101. (replace-match replacement t t)))))
  102. (defun org-babel-exp-do-export (info type)
  103. "Return a string containing the exported content of the current
  104. code block respecting the value of the :exports header argument."
  105. (flet ((silently () (when (cdr (assoc :session (third info)))
  106. (org-babel-exp-results info type 'silent))))
  107. (case (intern (or (cdr (assoc :exports (third info))) "code"))
  108. ('none (silently) "")
  109. ('code (silently) (org-babel-exp-code info type))
  110. ('results (org-babel-exp-results info type))
  111. ('both (concat (org-babel-exp-code info type)
  112. "\n\n"
  113. (org-babel-exp-results info type))))))
  114. (defun org-babel-exp-code (info type)
  115. "Return the code the current code block in a manner suitable
  116. for exportation by org-mode. This function is called by
  117. `org-babel-exp-do-export'. The code block will not be
  118. evaluated."
  119. (let ((lang (first info))
  120. (body (second info))
  121. (switches (fourth info))
  122. (name (fifth info))
  123. (args (mapcar
  124. #'cdr
  125. (remove-if-not (lambda (el) (eq :var (car el))) (third info)))))
  126. (case type
  127. ('inline (format "=%s=" body))
  128. ('block
  129. (let ((str
  130. (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  131. (if (and body (string-match "\n$" body))
  132. "" "\n"))))
  133. (when name
  134. (add-text-properties
  135. 0 (length str)
  136. (list 'org-caption
  137. (format "%s(%s)"
  138. name
  139. (mapconcat #'identity args ", ")))
  140. str))
  141. str))
  142. ('lob
  143. (let ((call-line (and (string-match "results=" (car args))
  144. (substring (car args) (match-end 0)))))
  145. (cond
  146. ((eq backend 'html)
  147. (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
  148. call-line))
  149. ((t (format ": %s\n" call-line)))))))))
  150. (defun org-babel-exp-results (info type &optional silent)
  151. "Return the results of the current code block in a manner
  152. suitable for exportation by org-mode. This function is called by
  153. `org-babel-exp-do-export'. The code block will be evaluated.
  154. Optional argument SILENT can be used to inhibit insertion of
  155. results into the buffer."
  156. (let ((lang (first info))
  157. (body (second info))
  158. (params
  159. ;; lets ensure that we lookup references in the original file
  160. (mapcar
  161. (lambda (pair)
  162. (if (and org-current-export-file
  163. (eq (car pair) :var)
  164. (string-match org-babel-ref-split-regexp (cdr pair))
  165. (null (org-babel-ref-literal (match-string 2 (cdr pair)))))
  166. `(:var . ,(concat (match-string 1 (cdr pair))
  167. "=" org-current-export-file
  168. ":" (match-string 2 (cdr pair))))
  169. pair))
  170. (third info))))
  171. (case type
  172. ('inline
  173. (let ((raw (org-babel-execute-src-block
  174. nil info '((:results . "silent"))))
  175. (result-params (split-string (cdr (assoc :results params)))))
  176. (unless silent
  177. (cond ;; respect the value of the :results header argument
  178. ((member "file" result-params)
  179. (org-babel-result-to-file raw))
  180. ((or (member "raw" result-params) (member "org" result-params))
  181. (format "%s" raw))
  182. ((member "code" result-params)
  183. (format "src_%s{%s}" lang raw))
  184. (t
  185. (if (stringp raw)
  186. (if (= 0 (length raw)) "=(no results)="
  187. (format "=%s=" raw))
  188. (format "=%S=" raw)))))))
  189. ('block
  190. (org-babel-execute-src-block
  191. nil nil (org-babel-merge-params
  192. params `((:results . ,(if silent "silent" "replace")))))
  193. "")
  194. ('lob
  195. (save-excursion
  196. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  197. (org-babel-execute-src-block
  198. nil (list lang body
  199. (org-babel-merge-params
  200. params `((:results . ,(if silent "silent" "replace")))))) "")))))
  201. (provide 'org-babel-exp)
  202. ;;; org-babel-exp.el ends here