ob-exp.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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."
  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-excursion
  139. ;; If match is inline, point is at its
  140. ;; end. Move backward so
  141. ;; `org-element-context' can get the
  142. ;; object, not the following one.
  143. (backward-char)
  144. (save-match-data (org-element-context))))
  145. (type (org-element-type element)))
  146. (when (memq type '(babel-call inline-babel-call inline-src-block))
  147. (let ((beg-el (org-element-property :begin element))
  148. (end-el (org-element-property :end element)))
  149. (case type
  150. (inline-src-block
  151. (let* ((info (org-babel-parse-inline-src-block-match))
  152. (params (nth 2 info)))
  153. (setf (nth 1 info)
  154. (if (and (cdr (assoc :noweb params))
  155. (string= "yes" (cdr (assoc :noweb params))))
  156. (org-babel-expand-noweb-references
  157. info (org-babel-exp-get-export-buffer))
  158. (nth 1 info)))
  159. (goto-char beg-el)
  160. (let ((replacement (org-babel-exp-do-export info 'inline)))
  161. (if (equal replacement "")
  162. ;; Replacement code is empty: completely
  163. ;; remove inline src block, including extra
  164. ;; white space that might have been created
  165. ;; when inserting results.
  166. (delete-region beg-el
  167. (progn (goto-char end-el)
  168. (skip-chars-forward " \t")
  169. (point)))
  170. ;; Otherwise: remove inline src block but
  171. ;; preserve following white spaces. Then
  172. ;; insert value.
  173. (delete-region beg-el
  174. (progn (goto-char end-el)
  175. (skip-chars-backward " \t")
  176. (point)))
  177. (insert replacement)))))
  178. ((babel-call inline-babel-call)
  179. (let* ((lob-info (org-babel-lob-get-info))
  180. (results
  181. (org-babel-exp-do-export
  182. (list "emacs-lisp" "results"
  183. (org-babel-merge-params
  184. org-babel-default-header-args
  185. org-babel-default-lob-header-args
  186. (org-babel-params-from-properties)
  187. (org-babel-parse-header-arguments
  188. (org-no-properties
  189. (concat ":var results="
  190. (mapconcat 'identity
  191. (butlast lob-info)
  192. " ")))))
  193. "" nil (car (last lob-info)))
  194. 'lob))
  195. (rep (org-fill-template
  196. org-babel-exp-call-line-template
  197. `(("line" . ,(nth 0 lob-info))))))
  198. ;; If replacement is empty, completely remove the
  199. ;; object/element, including any extra white space
  200. ;; that might have been created when including
  201. ;; results.
  202. (if (equal rep "")
  203. (delete-region
  204. beg-el
  205. (progn (goto-char end-el)
  206. (if (not (eq type 'babel-call))
  207. (progn (skip-chars-forward " \t") (point))
  208. (skip-chars-forward " \r\t\n")
  209. (line-beginning-position))))
  210. ;; Otherwise, preserve following white
  211. ;; spaces/newlines and then, insert replacement
  212. ;; string.
  213. (goto-char beg-el)
  214. (delete-region beg-el
  215. (progn (goto-char end-el)
  216. (skip-chars-backward " \r\t\n")
  217. (point)))
  218. (insert rep)))))))))))))
  219. (defvar org-src-preserve-indentation) ; From org-src.el
  220. (defun org-export-blocks-preprocess ()
  221. "Execute all blocks in visible part of buffer."
  222. (interactive)
  223. (save-window-excursion
  224. (let ((case-fold-search t)
  225. (pos (point-min)))
  226. (goto-char pos)
  227. (while (re-search-forward "^[ \t]*#\\+BEGIN_SRC" nil t)
  228. (let ((element (save-match-data (org-element-at-point))))
  229. (when (eq (org-element-type element) 'src-block)
  230. (let* ((match-start (copy-marker (match-beginning 0)))
  231. (begin (copy-marker (org-element-property :begin element)))
  232. ;; Make sure we don't remove any blank lines after
  233. ;; the block when replacing it.
  234. (block-end (save-excursion
  235. (goto-char (org-element-property :end element))
  236. (skip-chars-backward " \r\t\n")
  237. (copy-marker (line-end-position))))
  238. (ind (org-get-indentation))
  239. (headers
  240. (cons
  241. (org-element-property :language element)
  242. (let ((params (org-element-property :parameters element)))
  243. (and params (org-split-string params "[ \t]+")))))
  244. (preserve-indent
  245. (or org-src-preserve-indentation
  246. (org-element-property :preserve-indent element))))
  247. ;; Execute all non-block elements between POS and
  248. ;; current block.
  249. (org-babel-exp-non-block-elements pos begin)
  250. ;; Take care of matched block: compute replacement
  251. ;; string. In particular, a nil REPLACEMENT means the
  252. ;; block should be left as-is while an empty string
  253. ;; should remove the block.
  254. (let ((replacement (progn (goto-char match-start)
  255. (org-babel-exp-src-block headers))))
  256. (cond ((not replacement) (goto-char block-end))
  257. ((equal replacement "")
  258. (delete-region begin
  259. (progn (goto-char block-end)
  260. (skip-chars-forward " \r\t\n")
  261. (if (eobp) (point)
  262. (line-beginning-position)))))
  263. (t
  264. (goto-char match-start)
  265. (delete-region (point) block-end)
  266. (insert replacement)
  267. (if preserve-indent
  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-code-rigidly match-start (point) ind)))))
  275. (setq pos (line-beginning-position))
  276. ;; Cleanup markers.
  277. (set-marker match-start nil)
  278. (set-marker begin nil)
  279. (set-marker block-end nil)))))
  280. ;; Eventually execute all non-block Babel elements between last
  281. ;; src-block and end of buffer.
  282. (org-babel-exp-non-block-elements pos (point-max)))))
  283. (defun org-babel-in-example-or-verbatim ()
  284. "Return true if point is in example or verbatim code.
  285. Example and verbatim code include escaped portions of
  286. an org-mode buffer code that should be treated as normal
  287. org-mode text."
  288. (or (save-match-data
  289. (save-excursion
  290. (goto-char (point-at-bol))
  291. (looking-at "[ \t]*:[ \t]")))
  292. (org-in-verbatim-emphasis)
  293. (org-in-block-p org-list-forbidden-blocks)
  294. (org-between-regexps-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  295. (defun org-babel-exp-do-export (info type &optional hash)
  296. "Return a string with the exported content of a code block.
  297. The function respects the value of the :exports header argument."
  298. (let ((silently (lambda () (let ((session (cdr (assoc :session (nth 2 info)))))
  299. (when (not (and session (equal "none" session)))
  300. (org-babel-exp-results info type 'silent)))))
  301. (clean (lambda () (unless (eq type 'inline) (org-babel-remove-result info)))))
  302. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  303. ('none (funcall silently) (funcall clean) "")
  304. ('code (funcall silently) (funcall clean) (org-babel-exp-code info))
  305. ('results (org-babel-exp-results info type nil hash) "")
  306. ('both (org-babel-exp-results info type nil hash)
  307. (org-babel-exp-code info)))))
  308. (defcustom org-babel-exp-code-template
  309. "#+BEGIN_SRC %lang%flags\n%body\n#+END_SRC"
  310. "Template used to export the body of code blocks.
  311. This template may be customized to include additional information
  312. such as the code block name, or the values of particular header
  313. arguments. The template is filled out using `org-fill-template',
  314. and the following %keys may be used.
  315. lang ------ the language of the code block
  316. name ------ the name of the code block
  317. body ------ the body of the code block
  318. flags ----- the flags passed to the code block
  319. In addition to the keys mentioned above, every header argument
  320. defined for the code block may be used as a key and will be
  321. replaced with its value."
  322. :group 'org-babel
  323. :type 'string)
  324. (defun org-babel-exp-code (info)
  325. "Return the original code block formatted for export."
  326. (setf (nth 1 info)
  327. (if (string= "strip-export" (cdr (assoc :noweb (nth 2 info))))
  328. (replace-regexp-in-string
  329. (org-babel-noweb-wrap) "" (nth 1 info))
  330. (if (org-babel-noweb-p (nth 2 info) :export)
  331. (org-babel-expand-noweb-references
  332. info (org-babel-exp-get-export-buffer))
  333. (nth 1 info))))
  334. (org-fill-template
  335. org-babel-exp-code-template
  336. `(("lang" . ,(nth 0 info))
  337. ("body" . ,(org-escape-code-in-string (nth 1 info)))
  338. ,@(mapcar (lambda (pair)
  339. (cons (substring (symbol-name (car pair)) 1)
  340. (format "%S" (cdr pair))))
  341. (nth 2 info))
  342. ("flags" . ,((lambda (f) (when f (concat " " f))) (nth 3 info)))
  343. ("name" . ,(or (nth 4 info) "")))))
  344. (defun org-babel-exp-results (info type &optional silent hash)
  345. "Evaluate and return the results of the current code block for export.
  346. Results are prepared in a manner suitable for export by org-mode.
  347. This function is called by `org-babel-exp-do-export'. The code
  348. block will be evaluated. Optional argument SILENT can be used to
  349. inhibit insertion of results into the buffer."
  350. (when (and org-export-babel-evaluate
  351. (not (and hash (equal hash (org-babel-current-result-hash)))))
  352. (let ((lang (nth 0 info))
  353. (body (if (org-babel-noweb-p (nth 2 info) :eval)
  354. (org-babel-expand-noweb-references
  355. info (org-babel-exp-get-export-buffer))
  356. (nth 1 info)))
  357. (info (copy-sequence info)))
  358. ;; skip code blocks which we can't evaluate
  359. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  360. (org-babel-eval-wipe-error-buffer)
  361. (prog1 nil
  362. (setf (nth 1 info) body)
  363. (setf (nth 2 info)
  364. (org-babel-exp-in-export-file lang
  365. (org-babel-process-params
  366. (org-babel-merge-params
  367. (nth 2 info)
  368. `((:results . ,(if silent "silent" "replace")))))))
  369. (cond
  370. ((equal type 'block)
  371. (org-babel-execute-src-block nil info))
  372. ((equal type 'inline)
  373. ;; position the point on the inline source block allowing
  374. ;; `org-babel-insert-result' to check that the block is
  375. ;; inline
  376. (re-search-backward "[ \f\t\n\r\v]" nil t)
  377. (re-search-forward org-babel-inline-src-block-regexp nil t)
  378. (re-search-backward "src_" nil t)
  379. (org-babel-execute-src-block nil info))
  380. ((equal type 'lob)
  381. (save-excursion
  382. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  383. (org-babel-execute-src-block nil info)))))))))
  384. (provide 'ob-exp)
  385. ;;; ob-exp.el ends here