ob-exp.el 15 KB

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