ob-exp.el 10 KB

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