ob-exp.el 16 KB

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