ob-exp.el 16 KB

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