ob-exp.el 16 KB

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