ob-exp.el 9.9 KB

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