ob-exp.el 11 KB

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