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