ob-exp.el 16 KB

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