ob-exp.el 16 KB

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