ob-exp.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  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))
  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. (let ((results (org-babel-exp-do-export
  178. (org-babel-lob-get-info element)
  179. 'lob))
  180. (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. (lang
  208. (or (org-element-property :language element)
  209. (user-error
  210. "No language for src block: %s"
  211. (or (org-element-property :name element)
  212. "(unnamed)")))))
  213. ;; Take care of matched block: compute
  214. ;; replacement string. In particular, a nil
  215. ;; REPLACEMENT means the block is left as-is
  216. ;; while an empty string removes the block.
  217. (let ((replacement
  218. (progn (goto-char match-start)
  219. (org-babel-exp-src-block))))
  220. (cond ((not replacement) (goto-char end))
  221. ((equal replacement "")
  222. (goto-char end)
  223. (skip-chars-forward " \r\t\n")
  224. (beginning-of-line)
  225. (delete-region begin (point)))
  226. (t
  227. (goto-char match-start)
  228. (delete-region (point)
  229. (save-excursion
  230. (goto-char end)
  231. (line-end-position)))
  232. (insert replacement)
  233. (if (or org-src-preserve-indentation
  234. (org-element-property
  235. :preserve-indent element))
  236. ;; Indent only code block
  237. ;; markers.
  238. (save-excursion
  239. (skip-chars-backward " \r\t\n")
  240. (indent-line-to ind)
  241. (goto-char match-start)
  242. (indent-line-to ind))
  243. ;; Indent everything.
  244. (indent-rigidly
  245. match-start (point) ind)))))
  246. (set-marker match-start nil))))
  247. (set-marker begin nil)
  248. (set-marker end nil)))))
  249. (kill-buffer org-babel-exp-reference-buffer)
  250. (remove-text-properties (point-min) (point-max) '(org-reference)))))))
  251. (defun org-babel-exp-do-export (info type &optional hash)
  252. "Return a string with the exported content of a code block.
  253. The function respects the value of the :exports header argument."
  254. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  255. (unless (equal "none" session)
  256. (org-babel-exp-results info type 'silent)))))
  257. (clean (lambda () (if (eq type 'inline)
  258. (org-babel-remove-inline-result)
  259. (org-babel-remove-result info)))))
  260. (pcase (or (cdr (assq :exports (nth 2 info))) "code")
  261. ("none" (funcall silently) (funcall clean) "")
  262. ("code" (funcall silently) (funcall clean) (org-babel-exp-code info type))
  263. ("results" (org-babel-exp-results info type nil hash) "")
  264. ("both"
  265. (org-babel-exp-results info type nil hash)
  266. (org-babel-exp-code info type)))))
  267. (defcustom org-babel-exp-code-template
  268. "#+BEGIN_SRC %lang%switches%flags\n%body\n#+END_SRC"
  269. "Template used to export the body of code blocks.
  270. This template may be customized to include additional information
  271. such as the code block name, or the values of particular header
  272. arguments. The template is filled out using `org-fill-template',
  273. and the following %keys may be used.
  274. lang ------ the language of the code block
  275. name ------ the name of the code block
  276. body ------ the body of the code block
  277. switches -- the switches associated to the code block
  278. flags ----- the flags passed to the code block
  279. In addition to the keys mentioned above, every header argument
  280. defined for the code block may be used as a key and will be
  281. replaced with its value."
  282. :group 'org-babel
  283. :type 'string)
  284. (defcustom org-babel-exp-inline-code-template
  285. "src_%lang[%switches%flags]{%body}"
  286. "Template used to export the body of inline code blocks.
  287. This template may be customized to include additional information
  288. such as the code block name, or the values of particular header
  289. arguments. The template is filled out using `org-fill-template',
  290. and the following %keys may be used.
  291. lang ------ the language of the code block
  292. name ------ the name of the code block
  293. body ------ the body of the code block
  294. switches -- the switches associated to the code block
  295. flags ----- the flags passed to the code block
  296. In addition to the keys mentioned above, every header argument
  297. defined for the code block may be used as a key and will be
  298. replaced with its value."
  299. :group 'org-babel
  300. :type 'string
  301. :version "25.1"
  302. :package-version '(Org . "8.3"))
  303. (defun org-babel-exp-code (info type)
  304. "Return the original code block formatted for export."
  305. (setf (nth 1 info)
  306. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  307. (replace-regexp-in-string
  308. (org-babel-noweb-wrap) "" (nth 1 info))
  309. (if (org-babel-noweb-p (nth 2 info) :export)
  310. (org-babel-expand-noweb-references
  311. info org-babel-exp-reference-buffer)
  312. (nth 1 info))))
  313. (org-fill-template
  314. (if (eq type 'inline)
  315. org-babel-exp-inline-code-template
  316. org-babel-exp-code-template)
  317. `(("lang" . ,(nth 0 info))
  318. ("body" . ,(nth 1 info))
  319. ("switches" . ,(let ((f (nth 3 info)))
  320. (and (org-string-nw-p f) (concat " " f))))
  321. ("flags" . ,(let ((f (assq :flags (nth 2 info))))
  322. (and f (concat " " (cdr f)))))
  323. ,@(mapcar (lambda (pair)
  324. (cons (substring (symbol-name (car pair)) 1)
  325. (format "%S" (cdr pair))))
  326. (nth 2 info))
  327. ("name" . ,(or (nth 4 info) "")))))
  328. (defun org-babel-exp-results (info type &optional silent hash)
  329. "Evaluate and return the results of the current code block for export.
  330. Results are prepared in a manner suitable for export by Org mode.
  331. This function is called by `org-babel-exp-do-export'. The code
  332. block will be evaluated. Optional argument SILENT can be used to
  333. inhibit insertion of results into the buffer."
  334. (unless (and hash (equal hash (org-babel-current-result-hash)))
  335. (let ((lang (nth 0 info))
  336. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  337. (org-babel-expand-noweb-references
  338. info org-babel-exp-reference-buffer)
  339. (nth 1 info)))
  340. (info (copy-sequence info))
  341. (org-babel-current-src-block-location (point-marker)))
  342. ;; Skip code blocks which we can't evaluate.
  343. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  344. (org-babel-eval-wipe-error-buffer)
  345. (setf (nth 1 info) body)
  346. (setf (nth 2 info)
  347. (org-babel-exp--at-source
  348. (org-babel-process-params
  349. (org-babel-merge-params
  350. (nth 2 info)
  351. `((:results . ,(if silent "silent" "replace")))))))
  352. (pcase type
  353. (`block (org-babel-execute-src-block nil info))
  354. (`inline
  355. ;; Position the point on the inline source block
  356. ;; allowing `org-babel-insert-result' to check that the
  357. ;; block is inline.
  358. (goto-char (nth 5 info))
  359. (org-babel-execute-src-block nil info))
  360. (`lob
  361. (save-excursion
  362. (goto-char (nth 5 info))
  363. (let (org-confirm-babel-evaluate)
  364. (org-babel-execute-src-block nil info)))))))))
  365. (provide 'ob-exp)
  366. ;;; ob-exp.el ends here