ob-exp.el 14 KB

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