ob-exp.el 16 KB

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