ob-exp.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.5
  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. ;; See the online documentation for more information
  20. ;;
  21. ;; http://orgmode.org/worg/org-contrib/babel/
  22. ;;; Code:
  23. (require 'ob)
  24. (require 'org-exp-blocks)
  25. (eval-when-compile
  26. (require 'cl))
  27. (defvar obe-marker nil)
  28. (defvar org-current-export-file)
  29. (defvar org-babel-lob-one-liner-regexp)
  30. (defvar org-babel-ref-split-regexp)
  31. (declare-function org-babel-lob-get-info "ob-lob" ())
  32. (declare-function org-babel-eval-wipe-error-buffer "ob-eval" ())
  33. (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
  34. (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
  35. (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
  36. (org-export-blocks-add-block '(src org-babel-exp-src-block nil))
  37. (defcustom org-export-babel-evaluate t
  38. "Switch controlling code evaluation during export.
  39. When set to nil no code will be evaluated as part of the export
  40. process."
  41. :group 'org-babel
  42. :type 'boolean)
  43. (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
  44. (defvar org-babel-function-def-export-keyword "function"
  45. "The keyword to substitute for the source name line on export.
  46. When exporting a source block function, this keyword will
  47. appear in the exported version in the place of source name
  48. line. A source block is considered to be a source block function
  49. if the source name is present and is followed by a parenthesized
  50. argument list. The parentheses may be empty or contain
  51. whitespace. An example is the following which generates n random
  52. \(uniform) numbers.
  53. #+source: rand(n)
  54. #+begin_src R
  55. runif(n)
  56. #+end_src")
  57. (defvar org-babel-function-def-export-indent 4
  58. "Number of characters to indent a source block on export.
  59. When exporting a source block function, the block contents will
  60. be indented by this many characters. See
  61. `org-babel-function-def-export-name' for the definition of a
  62. source block function.")
  63. (defmacro org-babel-exp-in-export-file (lang &rest body)
  64. (declare (indent 1))
  65. `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" ,lang)))
  66. (heading (nth 4 (ignore-errors (org-heading-components))))
  67. (link (when org-current-export-file
  68. (org-make-link-string
  69. (if heading
  70. (concat org-current-export-file "::" heading)
  71. org-current-export-file))))
  72. (export-buffer (current-buffer)) results)
  73. (when link
  74. ;; resolve parameters in the original file so that
  75. ;; headline and file-wide parameters are included, attempt
  76. ;; to go to the same heading in the original file
  77. (set-buffer (get-file-buffer org-current-export-file))
  78. (save-restriction
  79. (condition-case nil
  80. (org-open-link-from-string link)
  81. (error (when heading
  82. (goto-char (point-min))
  83. (re-search-forward (regexp-quote heading) nil t))))
  84. (setq results ,@body))
  85. (set-buffer export-buffer)
  86. results)))
  87. (defun org-babel-exp-src-block (body &rest headers)
  88. "Process source block for export.
  89. Depending on the 'export' headers argument in replace the source
  90. code block with...
  91. both ---- display the code and the results
  92. code ---- the default, display the code inside the block but do
  93. not process
  94. results - just like none only the block is run on export ensuring
  95. that it's results are present in the org-mode buffer
  96. none ----- do not display either code or results upon export"
  97. (interactive)
  98. (message "org-babel-exp processing...")
  99. (save-excursion
  100. (goto-char (match-beginning 0))
  101. (let* ((info (org-babel-get-src-block-info 'light))
  102. (lang (nth 0 info))
  103. (raw-params (nth 2 info)) hash)
  104. ;; bail if we couldn't get any info from the block
  105. (when info
  106. ;; if we're actually going to need the parameters
  107. (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
  108. (org-babel-exp-in-export-file lang
  109. (setf (nth 2 info)
  110. (org-babel-process-params
  111. (org-babel-merge-params
  112. org-babel-default-header-args
  113. (org-babel-params-from-buffer)
  114. (org-babel-params-from-properties lang)
  115. (if (boundp lang-headers) (eval lang-headers) nil)
  116. raw-params))))
  117. (setf hash (org-babel-sha1-hash info)))
  118. ;; expand noweb references in the original file
  119. (setf (nth 1 info)
  120. (if (and (cdr (assoc :noweb (nth 2 info)))
  121. (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
  122. (org-babel-expand-noweb-references
  123. info (get-file-buffer org-current-export-file))
  124. (nth 1 info)))
  125. (org-babel-exp-do-export info 'block hash)))))
  126. (defun org-babel-exp-inline-src-blocks (start end)
  127. "Process inline source blocks between START and END for export.
  128. See `org-babel-exp-src-block' for export options, currently the
  129. options and are taken from `org-babel-default-inline-header-args'."
  130. (interactive)
  131. (save-excursion
  132. (goto-char start)
  133. (while (and (< (point) end)
  134. (re-search-forward org-babel-inline-src-block-regexp end t))
  135. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  136. (params (nth 2 info)) code-replacement)
  137. (save-match-data
  138. (goto-char (match-beginning 2))
  139. (when (not (org-babel-in-example-or-verbatim))
  140. ;; expand noweb references in the original file
  141. (setf (nth 1 info)
  142. (if (and (cdr (assoc :noweb params))
  143. (string= "yes" (cdr (assoc :noweb params))))
  144. (org-babel-expand-noweb-references
  145. info (get-file-buffer org-current-export-file))
  146. (nth 1 info)))
  147. (setq code-replacement (org-babel-exp-do-export info 'inline))))
  148. (if code-replacement
  149. (replace-match code-replacement nil nil nil 1)
  150. (org-babel-examplize-region (match-beginning 1) (match-end 1))
  151. (forward-char 2))))))
  152. (defun org-exp-res/src-name-cleanup ()
  153. "Clean up #+results and #+srcname lines for export.
  154. This function should only be called after all block processing
  155. has taken place."
  156. (interactive)
  157. (save-excursion
  158. (goto-char (point-min))
  159. (while (org-re-search-forward-unprotected
  160. (concat
  161. "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
  162. nil t)
  163. (delete-region
  164. (progn (beginning-of-line) (point))
  165. (progn (end-of-line) (+ 1 (point)))))))
  166. (defun org-babel-in-example-or-verbatim ()
  167. "Return true if point is in example or verbatim code.
  168. Example and verbatim code include escaped portions of
  169. an org-mode buffer code that should be treated as normal
  170. org-mode text."
  171. (or (org-in-indented-comment-line)
  172. (save-excursion
  173. (save-match-data
  174. (goto-char (point-at-bol))
  175. (looking-at "[ \t]*:[ \t]")))
  176. (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  177. (defun org-babel-exp-lob-one-liners (start end)
  178. "Process Library of Babel calls between START and END for export.
  179. See `org-babel-exp-src-block' for export options. Currently the
  180. options are taken from `org-babel-default-header-args'."
  181. (interactive)
  182. (let (replacement)
  183. (save-excursion
  184. (goto-char start)
  185. (while (and (< (point) end)
  186. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  187. (setq replacement
  188. (let ((lob-info (org-babel-lob-get-info)))
  189. (save-match-data
  190. (org-babel-exp-do-export
  191. (list "emacs-lisp" "results"
  192. (org-babel-merge-params
  193. org-babel-default-header-args
  194. (org-babel-params-from-buffer)
  195. (org-babel-params-from-properties)
  196. (org-babel-parse-header-arguments
  197. (org-babel-clean-text-properties
  198. (concat ":var results="
  199. (mapconcat #'identity
  200. (butlast lob-info) " ")))))
  201. (car (last lob-info)))
  202. 'lob))))
  203. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  204. (if replacement (replace-match replacement t t))))))
  205. (defun org-babel-exp-do-export (info type &optional hash)
  206. "Return a string with the exported content of a code block.
  207. The function respects the value of the :exports header argument."
  208. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  209. (when (not (and session (equal "none" session)))
  210. (org-babel-exp-results info type 'silent))))
  211. (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
  212. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  213. ('none (silently) (clean) "")
  214. ('code (silently) (clean) nil)
  215. ('results (org-babel-exp-results info type nil hash) "")
  216. ('both (org-babel-exp-results info type nil hash) nil))))
  217. (defun org-babel-exp-results (info type &optional silent hash)
  218. "Evaluate and return the results of the current code block for export.
  219. Results are prepared in a manner suitable for export by org-mode.
  220. This function is called by `org-babel-exp-do-export'. The code
  221. block will be evaluated. Optional argument SILENT can be used to
  222. inhibit insertion of results into the buffer."
  223. (when (and org-export-babel-evaluate
  224. (not (and hash
  225. (equal hash (org-babel-exp-in-export-file (nth 0 info)
  226. (org-babel-result-hash info))))))
  227. (let ((lang (nth 0 info))
  228. (body (nth 1 info)))
  229. ;; skip code blocks which we can't evaluate
  230. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  231. (org-babel-eval-wipe-error-buffer)
  232. (prog1 nil
  233. (setf (nth 2 info)
  234. (org-babel-exp-in-export-file lang
  235. (org-babel-process-params
  236. (org-babel-merge-params
  237. (nth 2 info)
  238. `((:results . ,(if silent "silent" "replace")))))))
  239. (cond
  240. ((or (equal type 'block) (equal type 'inline))
  241. (org-babel-execute-src-block nil info))
  242. ((equal type 'lob)
  243. (save-excursion
  244. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  245. (org-babel-execute-src-block nil info)))))))))
  246. (provide 'ob-exp)
  247. ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
  248. ;;; ob-exp.el ends here