ob-exp.el 9.8 KB

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