ob-exp.el 15 KB

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