ob-exp.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Dan Davison
  5. ;; Keywords: literate programming, reproducible research
  6. ;; Homepage: http://orgmode.org
  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. (def-edebug-spec org-babel-exp-in-export-file (form body))
  66. (defun org-babel-exp-src-block (body &rest headers)
  67. "Process source block for export.
  68. Depending on the 'export' headers argument in replace the source
  69. code block with...
  70. both ---- display the code and the results
  71. code ---- the default, display the code inside the block but do
  72. not process
  73. results - just like none only the block is run on export ensuring
  74. that it's results are present in the org-mode buffer
  75. none ----- do not display either code or results upon export"
  76. (interactive)
  77. (unless noninteractive (message "org-babel-exp processing..."))
  78. (save-excursion
  79. (goto-char (match-beginning 0))
  80. (let* ((info (org-babel-get-src-block-info 'light))
  81. (lang (nth 0 info))
  82. (raw-params (nth 2 info)) hash)
  83. ;; bail if we couldn't get any info from the block
  84. (when info
  85. ;; if we're actually going to need the parameters
  86. (when (member (cdr (assoc :exports (nth 2 info))) '("both" "results"))
  87. (org-babel-exp-in-export-file lang
  88. (setf (nth 2 info)
  89. (org-babel-process-params
  90. (org-babel-merge-params
  91. org-babel-default-header-args
  92. (org-babel-params-from-buffer)
  93. (org-babel-params-from-properties lang)
  94. (if (boundp lang-headers) (eval lang-headers) nil)
  95. raw-params))))
  96. (setf hash (org-babel-sha1-hash info)))
  97. ;; expand noweb references in the original file
  98. (setf (nth 1 info)
  99. (if (and (cdr (assoc :noweb (nth 2 info)))
  100. (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
  101. (org-babel-expand-noweb-references
  102. info (get-file-buffer org-current-export-file))
  103. (nth 1 info)))
  104. (org-babel-exp-do-export info 'block hash)))))
  105. (defun org-babel-exp-inline-src-blocks (start end)
  106. "Process inline source blocks between START and END for export.
  107. See `org-babel-exp-src-block' for export options, currently the
  108. options and are taken from `org-babel-default-inline-header-args'."
  109. (interactive)
  110. (save-excursion
  111. (goto-char start)
  112. (while (and (< (point) end)
  113. (re-search-forward org-babel-inline-src-block-regexp end t))
  114. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  115. (params (nth 2 info)))
  116. (save-match-data
  117. (goto-char (match-beginning 2))
  118. (unless (org-babel-in-example-or-verbatim)
  119. ;; expand noweb references in the original file
  120. (setf (nth 1 info)
  121. (if (and (cdr (assoc :noweb params))
  122. (string= "yes" (cdr (assoc :noweb params))))
  123. (org-babel-expand-noweb-references
  124. info (get-file-buffer org-current-export-file))
  125. (nth 1 info)))
  126. (let ((code-replacement (save-match-data
  127. (org-babel-exp-do-export info 'inline))))
  128. (if code-replacement
  129. (replace-match code-replacement nil nil nil 1)
  130. (org-babel-examplize-region (match-beginning 1) (match-end 1))
  131. (forward-char 2)))))))))
  132. (defun org-exp-res/src-name-cleanup ()
  133. "Clean up #+results and #+srcname lines for export.
  134. This function should only be called after all block processing
  135. has taken place."
  136. (interactive)
  137. (save-excursion
  138. (goto-char (point-min))
  139. (while (org-re-search-forward-unprotected
  140. (concat
  141. "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
  142. nil t)
  143. (delete-region
  144. (progn (beginning-of-line) (point))
  145. (progn (end-of-line) (+ 1 (point)))))))
  146. (defun org-babel-in-example-or-verbatim ()
  147. "Return true if point is in example or verbatim code.
  148. Example and verbatim code include escaped portions of
  149. an org-mode buffer code that should be treated as normal
  150. org-mode text."
  151. (or (org-in-indented-comment-line)
  152. (save-match-data
  153. (save-excursion
  154. (goto-char (point-at-bol))
  155. (looking-at "[ \t]*:[ \t]")))
  156. (org-in-verbatim-emphasis)
  157. (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  158. (defvar org-babel-default-lob-header-args)
  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. (save-excursion
  165. (goto-char start)
  166. (while (and (< (point) end)
  167. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  168. (unless (and (match-string 12) (org-babel-in-example-or-verbatim))
  169. (let* ((lob-info (org-babel-lob-get-info))
  170. (inlinep (match-string 11))
  171. (inline-start (match-end 11))
  172. (inline-end (match-end 0))
  173. (rep (let ((lob-info (org-babel-lob-get-info)))
  174. (save-match-data
  175. (org-babel-exp-do-export
  176. (list "emacs-lisp" "results"
  177. (org-babel-merge-params
  178. org-babel-default-header-args
  179. org-babel-default-lob-header-args
  180. (org-babel-params-from-buffer)
  181. (org-babel-params-from-properties)
  182. (org-babel-parse-header-arguments
  183. (org-babel-clean-text-properties
  184. (concat ":var results="
  185. (mapconcat #'identity
  186. (butlast lob-info) " ")))))
  187. "" nil (car (last lob-info)))
  188. 'lob)))))
  189. (setq end (+ end (- (length rep)
  190. (- (length (match-string 0))
  191. (length (or (match-string 11) ""))))))
  192. (if inlinep
  193. (save-excursion
  194. (goto-char inline-start)
  195. (delete-region inline-start inline-end)
  196. (insert rep))
  197. (replace-match rep t t)))))))
  198. (defun org-babel-exp-do-export (info type &optional hash)
  199. "Return a string with the exported content of a code block.
  200. The function respects the value of the :exports header argument."
  201. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  202. (when (not (and session (equal "none" session)))
  203. (org-babel-exp-results info type 'silent))))
  204. (clean () (unless (eq type 'inline) (org-babel-remove-result info))))
  205. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  206. ('none (silently) (clean) "")
  207. ('code (silently) (clean) (org-babel-exp-code info))
  208. ('results (org-babel-exp-results info type nil hash) "")
  209. ('both (org-babel-exp-results info type nil hash)
  210. (org-babel-exp-code info)))))
  211. (defun org-babel-exp-code (info)
  212. "Return the original code block formatted for export."
  213. (org-fill-template
  214. "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
  215. `(("lang" . ,(nth 0 info))
  216. ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
  217. ("body" . ,(nth 1 info)))))
  218. (defun org-babel-exp-results (info type &optional silent hash)
  219. "Evaluate and return the results of the current code block for export.
  220. Results are prepared in a manner suitable for export by org-mode.
  221. This function is called by `org-babel-exp-do-export'. The code
  222. block will be evaluated. Optional argument SILENT can be used to
  223. inhibit insertion of results into the buffer."
  224. (when (and org-export-babel-evaluate
  225. (not (and hash (equal hash (org-babel-current-result-hash)))))
  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. ((equal type 'block)
  240. (org-babel-execute-src-block nil info))
  241. ((equal type 'inline)
  242. ;; position the point on the inline source block allowing
  243. ;; `org-babel-insert-result' to check that the block is
  244. ;; inline
  245. (re-search-backward "[ \f\t\n\r\v]" nil t)
  246. (re-search-forward org-babel-inline-src-block-regexp nil t)
  247. (re-search-backward "src_" nil t)
  248. (org-babel-execute-src-block nil info))
  249. ((equal type 'lob)
  250. (save-excursion
  251. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  252. (org-babel-execute-src-block nil info)))))))))
  253. (provide 'ob-exp)
  254. ;;; ob-exp.el ends here