ob-exp.el 12 KB

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