ob-exp.el 15 KB

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