ob-exp.el 9.7 KB

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