ob-exp.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. ;;; ob-exp.el --- Exportation of Babel Source Blocks -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2016 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-core)
  20. (declare-function org-babel-lob-get-info "ob-lob" (&optional datum))
  21. (declare-function org-element-at-point "org-element" ())
  22. (declare-function org-element-context "org-element" (&optional element))
  23. (declare-function org-element-property "org-element" (property element))
  24. (declare-function org-element-type "org-element" (element))
  25. (declare-function org-export-copy-buffer "ox" ())
  26. (declare-function org-fill-template "org" (template alist))
  27. (declare-function org-get-indentation "org" (&optional line))
  28. (declare-function org-heading-components "org" ())
  29. (declare-function org-in-commented-heading-p "org" (&optional no-inheritance))
  30. (defvar org-src-preserve-indentation)
  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. When set to `inline-only', only inline code blocks will
  35. be executed."
  36. :group 'org-babel
  37. :version "24.1"
  38. :type '(choice (const :tag "Never" nil)
  39. (const :tag "Only inline code" inline-only)
  40. (const :tag "Always" t)))
  41. (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
  42. (defmacro org-babel-exp--at-source (&rest body)
  43. "Evaluate BODY at the source of the Babel block at point.
  44. Source is located in `org-babel-exp-reference-buffer'. The value
  45. returned is the value of the last form in BODY. Assume that
  46. point is at the beginning of the Babel block."
  47. (declare (indent 1) (debug body))
  48. `(let ((source (get-text-property (point) 'org-reference)))
  49. (with-current-buffer org-babel-exp-reference-buffer
  50. (org-with-wide-buffer
  51. (goto-char source)
  52. ,@body))))
  53. (defun org-babel-exp-src-block ()
  54. "Process source block for export.
  55. Depending on the \":export\" header argument, replace the source
  56. code block like this:
  57. both ---- display the code and the results
  58. code ---- the default, display the code inside the block but do
  59. not process
  60. results - just like none only the block is run on export ensuring
  61. that its results are present in the Org mode buffer
  62. none ---- do not display either code or results upon export
  63. Assume point is at block opening line."
  64. (interactive)
  65. (save-excursion
  66. (let* ((info (org-babel-get-src-block-info 'light))
  67. (lang (nth 0 info))
  68. (raw-params (nth 2 info))
  69. hash)
  70. ;; bail if we couldn't get any info from the block
  71. (unless noninteractive
  72. (message "org-babel-exp process %s at position %d..."
  73. lang
  74. (line-beginning-position)))
  75. (when info
  76. ;; if we're actually going to need the parameters
  77. (when (member (cdr (assq :exports (nth 2 info))) '("both" "results"))
  78. (let ((lang-headers (intern (concat "org-babel-default-header-args:"
  79. lang))))
  80. (org-babel-exp--at-source
  81. (setf (nth 2 info)
  82. (org-babel-process-params
  83. (apply #'org-babel-merge-params
  84. org-babel-default-header-args
  85. (and (boundp lang-headers) (eval lang-headers t))
  86. (append (org-babel-params-from-properties lang)
  87. (list raw-params)))))))
  88. (setf hash (org-babel-sha1-hash info)))
  89. (org-babel-exp-do-export info 'block hash)))))
  90. (defcustom org-babel-exp-call-line-template
  91. ""
  92. "Template used to export call lines.
  93. This template may be customized to include the call line name
  94. with any export markup. The template is filled out using
  95. `org-fill-template', and the following %keys may be used.
  96. line --- call line
  97. An example value would be \"\\n: call: %line\" to export the call line
  98. wrapped in a verbatim environment.
  99. Note: the results are inserted separately after the contents of
  100. this template."
  101. :group 'org-babel
  102. :type 'string)
  103. (defun org-babel-exp-process-buffer ()
  104. "Execute all Babel blocks in current buffer."
  105. (interactive)
  106. (when org-export-babel-evaluate
  107. (save-window-excursion
  108. (let ((case-fold-search t)
  109. (regexp (if (eq org-export-babel-evaluate 'inline-only)
  110. "\\(call\\|src\\)_"
  111. "\\(call\\|src\\)_\\|^[ \t]*#\\+\\(BEGIN_SRC\\|CALL:\\)"))
  112. ;; Get a pristine copy of current buffer so Babel
  113. ;; references are properly resolved and source block
  114. ;; context is preserved.
  115. (org-babel-exp-reference-buffer (org-export-copy-buffer)))
  116. (unwind-protect
  117. (save-excursion
  118. ;; First attach to every source block their original
  119. ;; position, so that they can be retrieved within
  120. ;; `org-babel-exp-reference-buffer', even after heavy
  121. ;; modifications on current buffer.
  122. ;;
  123. ;; False positives are harmless, so we don't check if
  124. ;; we're really at some Babel object. Moreover,
  125. ;; `line-end-position' ensures that we propertize
  126. ;; a noticeable part of the object, without affecting
  127. ;; multiple objects on the same line.
  128. (goto-char (point-min))
  129. (while (re-search-forward regexp nil t)
  130. (let ((s (match-beginning 0)))
  131. (put-text-property s (line-end-position) 'org-reference s)))
  132. ;; Evaluate from top to bottom every Babel block
  133. ;; encountered.
  134. (goto-char (point-min))
  135. (while (re-search-forward regexp nil t)
  136. (unless (save-match-data (org-in-commented-heading-p))
  137. (let* ((element (save-match-data (org-element-context)))
  138. (type (org-element-type element))
  139. (begin
  140. (copy-marker (org-element-property :begin element)))
  141. (end
  142. (copy-marker
  143. (save-excursion
  144. (goto-char (org-element-property :end element))
  145. (skip-chars-backward " \r\t\n")
  146. (point)))))
  147. (pcase type
  148. (`inline-src-block
  149. (let* ((info
  150. (org-babel-get-src-block-info nil element))
  151. (params (nth 2 info)))
  152. (setf (nth 1 info)
  153. (if (and (cdr (assoc :noweb params))
  154. (string= "yes"
  155. (cdr (assq :noweb params))))
  156. (org-babel-expand-noweb-references
  157. info org-babel-exp-reference-buffer)
  158. (nth 1 info)))
  159. (goto-char begin)
  160. (let ((replacement
  161. (org-babel-exp-do-export info 'inline)))
  162. (if (equal replacement "")
  163. ;; Replacement code is empty: remove
  164. ;; inline source block, including extra
  165. ;; white space that might have been
  166. ;; created when inserting results.
  167. (delete-region begin
  168. (progn (goto-char end)
  169. (skip-chars-forward " \t")
  170. (point)))
  171. ;; Otherwise: remove inline src block but
  172. ;; preserve following white spaces. Then
  173. ;; insert value.
  174. (delete-region begin end)
  175. (insert replacement)))))
  176. ((or `babel-call `inline-babel-call)
  177. (org-babel-exp-do-export (org-babel-lob-get-info element)
  178. 'lob)
  179. (let ((rep
  180. (org-fill-template
  181. org-babel-exp-call-line-template
  182. `(("line" .
  183. ,(org-element-property :value element))))))
  184. ;; If replacement is empty, completely remove
  185. ;; the object/element, including any extra
  186. ;; white space that might have been created
  187. ;; when including results.
  188. (if (equal rep "")
  189. (delete-region
  190. begin
  191. (progn (goto-char end)
  192. (if (not (eq type 'babel-call))
  193. (progn (skip-chars-forward " \t")
  194. (point))
  195. (skip-chars-forward " \r\t\n")
  196. (line-beginning-position))))
  197. ;; Otherwise, preserve trailing
  198. ;; spaces/newlines and then, insert
  199. ;; replacement string.
  200. (goto-char begin)
  201. (delete-region begin end)
  202. (insert rep))))
  203. (`src-block
  204. (let ((match-start (copy-marker (match-beginning 0)))
  205. (ind (org-get-indentation)))
  206. ;; Take care of matched block: compute
  207. ;; replacement string. In particular, a nil
  208. ;; REPLACEMENT means the block is left as-is
  209. ;; while an empty string removes the block.
  210. (let ((replacement
  211. (progn (goto-char match-start)
  212. (org-babel-exp-src-block))))
  213. (cond ((not replacement) (goto-char end))
  214. ((equal replacement "")
  215. (goto-char end)
  216. (skip-chars-forward " \r\t\n")
  217. (beginning-of-line)
  218. (delete-region begin (point)))
  219. (t
  220. (goto-char match-start)
  221. (delete-region (point)
  222. (save-excursion
  223. (goto-char end)
  224. (line-end-position)))
  225. (insert replacement)
  226. (if (or org-src-preserve-indentation
  227. (org-element-property
  228. :preserve-indent element))
  229. ;; Indent only code block
  230. ;; markers.
  231. (save-excursion
  232. (skip-chars-backward " \r\t\n")
  233. (indent-line-to ind)
  234. (goto-char match-start)
  235. (indent-line-to ind))
  236. ;; Indent everything.
  237. (indent-rigidly
  238. match-start (point) ind)))))
  239. (set-marker match-start nil))))
  240. (set-marker begin nil)
  241. (set-marker end nil)))))
  242. (kill-buffer org-babel-exp-reference-buffer)
  243. (remove-text-properties (point-min) (point-max) '(org-reference)))))))
  244. (defun org-babel-exp-do-export (info type &optional hash)
  245. "Return a string with the exported content of a code block.
  246. The function respects the value of the :exports header argument."
  247. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  248. (unless (equal "none" session)
  249. (org-babel-exp-results info type 'silent)))))
  250. (clean (lambda () (if (eq type 'inline)
  251. (org-babel-remove-inline-result)
  252. (org-babel-remove-result info)))))
  253. (pcase (or (cdr (assq :exports (nth 2 info))) "code")
  254. ("none" (funcall silently) (funcall clean) "")
  255. ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
  256. ("results" (org-babel-exp-results info type nil hash) "")
  257. ("both"
  258. (org-babel-exp-results info type nil hash)
  259. (org-babel-exp-code info type)))))
  260. (defcustom org-babel-exp-code-template
  261. "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
  262. "Template used to export the body of code blocks.
  263. This template may be customized to include additional information
  264. such as the code block name, or the values of particular header
  265. arguments. The template is filled out using `org-fill-template',
  266. and the following %keys may be used.
  267. lang ------ the language of the code block
  268. name ------ the name of the code block
  269. body ------ the body of the code block
  270. switches -- the switches associated to the code block
  271. flags ----- the flags passed to the code block
  272. In addition to the keys mentioned above, every header argument
  273. defined for the code block may be used as a key and will be
  274. replaced with its value."
  275. :group 'org-babel
  276. :type 'string)
  277. (defcustom org-babel-exp-inline-code-template
  278. "src_%lang[%switches%flags]{%body}"
  279. "Template used to export the body of inline code blocks.
  280. This template may be customized to include additional information
  281. such as the code block name, or the values of particular header
  282. arguments. The template is filled out using `org-fill-template',
  283. and the following %keys may be used.
  284. lang ------ the language of the code block
  285. name ------ the name of the code block
  286. body ------ the body of the code block
  287. switches -- the switches associated to the code block
  288. flags ----- the flags passed to the code block
  289. In addition to the keys mentioned above, every header argument
  290. defined for the code block may be used as a key and will be
  291. replaced with its value."
  292. :group 'org-babel
  293. :type 'string
  294. :version "25.1"
  295. :package-version '(Org . "8.3"))
  296. (defun org-babel-exp-code (info type)
  297. "Return the original code block formatted for export."
  298. (setf (nth 1 info)
  299. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  300. (replace-regexp-in-string
  301. (org-babel-noweb-wrap) "" (nth 1 info))
  302. (if (org-babel-noweb-p (nth 2 info) :export)
  303. (org-babel-expand-noweb-references
  304. info org-babel-exp-reference-buffer)
  305. (nth 1 info))))
  306. (org-fill-template
  307. (if (eq type 'inline)
  308. org-babel-exp-inline-code-template
  309. org-babel-exp-code-template)
  310. `(("lang" . ,(nth 0 info))
  311. ("body" . ,(nth 1 info))
  312. ("switches" . ,(let ((f (nth 3 info)))
  313. (and (org-string-nw-p f) (concat " " f))))
  314. ("flags" . ,(let ((f (assq :flags (nth 2 info))))
  315. (and f (concat " " (cdr f)))))
  316. ,@(mapcar (lambda (pair)
  317. (cons (substring (symbol-name (car pair)) 1)
  318. (format "%S" (cdr pair))))
  319. (nth 2 info))
  320. ("name" . ,(or (nth 4 info) "")))))
  321. (defun org-babel-exp-results (info type &optional silent hash)
  322. "Evaluate and return the results of the current code block for export.
  323. Results are prepared in a manner suitable for export by Org mode.
  324. This function is called by `org-babel-exp-do-export'. The code
  325. block will be evaluated. Optional argument SILENT can be used to
  326. inhibit insertion of results into the buffer."
  327. (unless (and hash (equal hash (org-babel-current-result-hash)))
  328. (let ((lang (nth 0 info))
  329. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  330. (org-babel-expand-noweb-references
  331. info org-babel-exp-reference-buffer)
  332. (nth 1 info)))
  333. (info (copy-sequence info))
  334. (org-babel-current-src-block-location (point-marker)))
  335. ;; Skip code blocks which we can't evaluate.
  336. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  337. (org-babel-eval-wipe-error-buffer)
  338. (setf (nth 1 info) body)
  339. (setf (nth 2 info)
  340. (org-babel-exp--at-source
  341. (org-babel-process-params
  342. (org-babel-merge-params
  343. (nth 2 info)
  344. `((:results . ,(if silent "silent" "replace")))))))
  345. (pcase type
  346. (`block (org-babel-execute-src-block nil info))
  347. (`inline
  348. ;; Position the point on the inline source block
  349. ;; allowing `org-babel-insert-result' to check that the
  350. ;; block is inline.
  351. (goto-char (nth 5 info))
  352. (org-babel-execute-src-block nil info))
  353. (`lob
  354. (save-excursion
  355. (goto-char (nth 5 info))
  356. (let (org-confirm-babel-evaluate)
  357. (org-babel-execute-src-block nil info)))))))))
  358. (provide 'ob-exp)
  359. ;;; ob-exp.el ends here