ob-exp.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.4
  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 (&rest body)
  64. `(let* ((lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  65. (heading (nth 4 (ignore-errors (org-heading-components))))
  66. (link (when org-current-export-file
  67. (org-make-link-string
  68. (if heading
  69. (concat org-current-export-file "::" heading)
  70. org-current-export-file))))
  71. (export-buffer (current-buffer)) results)
  72. (when link
  73. ;; resolve parameters in the original file so that
  74. ;; headline and file-wide parameters are included, attempt
  75. ;; to go to the same heading in the original file
  76. (set-buffer (get-file-buffer org-current-export-file))
  77. (save-restriction
  78. (condition-case nil
  79. (org-open-link-from-string link)
  80. (error (when heading
  81. (goto-char (point-min))
  82. (re-search-forward (regexp-quote heading) nil t))))
  83. (setq results ,@body))
  84. (set-buffer export-buffer)
  85. results)))
  86. (defun org-babel-exp-src-block (body &rest headers)
  87. "Process source block for export.
  88. Depending on the 'export' headers argument in replace the source
  89. code block with...
  90. both ---- display the code and the results
  91. code ---- the default, display the code inside the block but do
  92. not process
  93. results - just like none only the block is run on export ensuring
  94. that it's results are present in the org-mode buffer
  95. none ----- do not display either code or results upon export"
  96. (interactive)
  97. (message "org-babel-exp processing...")
  98. (save-excursion
  99. (goto-char (match-beginning 0))
  100. (let* ((info (org-babel-get-src-block-info 'light))
  101. (lang (nth 0 info))
  102. (raw-params (nth 2 info)))
  103. ;; bail if we couldn't get any info from the block
  104. (when info
  105. (org-babel-exp-in-export-file
  106. (setf (nth 2 info)
  107. (org-babel-merge-params
  108. org-babel-default-header-args
  109. (org-babel-params-from-buffer)
  110. (org-babel-params-from-properties lang)
  111. (if (boundp lang-headers) (eval lang-headers) nil)
  112. raw-params)))
  113. ;; expand noweb references in the original file
  114. (setf (nth 1 info)
  115. (if (and (cdr (assoc :noweb (nth 2 info)))
  116. (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
  117. (org-babel-expand-noweb-references
  118. info (get-file-buffer org-current-export-file))
  119. (nth 1 info)))
  120. (org-babel-exp-do-export info 'block)))))
  121. (defun org-babel-exp-inline-src-blocks (start end)
  122. "Process inline source blocks between START and END for export.
  123. See `org-babel-exp-src-block' for export options, currently the
  124. options and are taken from `org-babel-default-inline-header-args'."
  125. (interactive)
  126. (save-excursion
  127. (goto-char start)
  128. (while (and (< (point) end)
  129. (re-search-forward org-babel-inline-src-block-regexp end t))
  130. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  131. (params (nth 2 info)) code-replacement)
  132. (save-match-data
  133. (goto-char (match-beginning 2))
  134. (if (org-babel-in-example-or-verbatim)
  135. (buffer-substring (match-beginning 0) (match-end 0))
  136. ;; expand noweb references in the original file
  137. (setf (nth 1 info)
  138. (if (and (cdr (assoc :noweb params))
  139. (string= "yes" (cdr (assoc :noweb params))))
  140. (org-babel-expand-noweb-references
  141. info (get-file-buffer org-current-export-file))
  142. (nth 1 info)))
  143. (setq code-replacement (org-babel-exp-do-export info 'inline))))
  144. (if code-replacement
  145. (replace-match code-replacement nil nil nil 1)
  146. (org-babel-examplize-region (match-beginning 1) (match-end 1))
  147. (forward-char 2))))))
  148. (defun org-exp-res/src-name-cleanup ()
  149. "Clean up #+results and #+srcname lines for export.
  150. This function should only be called after all block processing
  151. has taken place."
  152. (interactive)
  153. (save-excursion
  154. (goto-char (point-min))
  155. (while (org-re-search-forward-unprotected
  156. (concat
  157. "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
  158. nil t)
  159. (delete-region
  160. (progn (beginning-of-line) (point))
  161. (progn (end-of-line) (+ 1 (point)))))))
  162. (defun org-babel-in-example-or-verbatim ()
  163. "Return true if point is in example or verbatim code.
  164. Example and verbatim code include escaped portions of
  165. an org-mode buffer code that should be treated as normal
  166. org-mode text."
  167. (or (org-in-indented-comment-line)
  168. (save-excursion
  169. (save-match-data
  170. (goto-char (point-at-bol))
  171. (looking-at "[ \t]*:[ \t]")))
  172. (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  173. (defun org-babel-exp-lob-one-liners (start end)
  174. "Process Library of Babel calls between START and END for export.
  175. See `org-babel-exp-src-block' for export options. Currently the
  176. options are taken from `org-babel-default-header-args'."
  177. (interactive)
  178. (let (replacement)
  179. (save-excursion
  180. (goto-char start)
  181. (while (and (< (point) end)
  182. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  183. (setq replacement
  184. (let ((lob-info (org-babel-lob-get-info)))
  185. (save-match-data
  186. (org-babel-exp-do-export
  187. (list "emacs-lisp" "results"
  188. (org-babel-merge-params
  189. org-babel-default-header-args
  190. (org-babel-params-from-buffer)
  191. (org-babel-params-from-properties)
  192. (org-babel-parse-header-arguments
  193. (org-babel-clean-text-properties
  194. (concat ":var results="
  195. (mapconcat #'identity
  196. (butlast lob-info) " ")))))
  197. (car (last lob-info)))
  198. 'lob))))
  199. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  200. (if replacement (replace-match replacement t t))))))
  201. (defun org-babel-exp-do-export (info type)
  202. "Return a string with the exported content of a code block.
  203. The function respects the value of the :exports header argument."
  204. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  205. (when (and session (not (equal "none" session)))
  206. (org-babel-exp-results info type 'silent))))
  207. (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
  208. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  209. ('none (silently) (clean) "")
  210. ('code (silently) (clean) nil)
  211. ('results (org-babel-exp-results info type) "")
  212. ('both (org-babel-exp-results info type) nil))))
  213. (defun org-babel-exp-results (info type &optional silent)
  214. "Evaluate and return the results of the current code block for export.
  215. Results are prepared in a manner suitable for export by org-mode.
  216. This function is called by `org-babel-exp-do-export'. The code
  217. block will be evaluated. Optional argument SILENT can be used to
  218. inhibit insertion of results into the buffer."
  219. (when org-export-babel-evaluate
  220. (let ((lang (nth 0 info))
  221. (body (nth 1 info)))
  222. (setf (nth 2 info) (org-babel-exp-in-export-file
  223. (org-babel-process-params (nth 2 info))))
  224. ;; skip code blocks which we can't evaluate
  225. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  226. (org-babel-eval-wipe-error-buffer)
  227. (prog1 nil
  228. (setf (nth 2 info)
  229. (org-babel-merge-params
  230. (nth 2 info)
  231. `((:results . ,(if silent "silent" "replace")))))
  232. (cond
  233. ((or (equal type 'block) (equal type 'inline))
  234. (org-babel-execute-src-block nil info))
  235. ((equal type 'lob)
  236. (save-excursion
  237. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  238. (org-babel-execute-src-block nil info)))))))))
  239. (provide 'ob-exp)
  240. ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
  241. ;;; ob-exp.el ends here