ob-exp.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. ;;; ob-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  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. ;;; Commentary:
  19. ;; See the online documentation for more information
  20. ;;
  21. ;; http://orgmode.org/worg/org-contrib/babel/
  22. ;;; Code:
  23. (require 'ob)
  24. (require 'org-exp-blocks)
  25. (eval-when-compile
  26. (require 'cl))
  27. (defvar obe-marker nil)
  28. (defvar org-current-export-file)
  29. (defvar org-babel-lob-one-liner-regexp)
  30. (defvar org-babel-ref-split-regexp)
  31. (declare-function org-babel-lob-get-info "ob-lob" ())
  32. (declare-function org-babel-ref-literal "ob-ref" (ref))
  33. (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
  34. (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
  35. (add-hook 'org-export-blocks-postblock-hook 'org-exp-res/src-name-cleanup)
  36. (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
  37. (defcustom org-export-babel-evaluate t
  38. "Switch controlling code evaluation during export.
  39. When set to nil no code will be evaluated as part of the export
  40. process."
  41. :group 'org-babel
  42. :type 'boolean)
  43. (put 'org-export-babel-evaluate 'safe-local-variable (lambda (x) (eq x nil)))
  44. (defvar org-babel-function-def-export-keyword "function"
  45. "The keyword to substitute for the source name line on export.
  46. When exporting a source block function, this keyword will
  47. appear in the exported version in the place of source name
  48. line. A source block is considered to be a source block function
  49. if the source name is present and is followed by a parenthesized
  50. argument list. The parentheses may be empty or contain
  51. whitespace. An example is the following which generates n random
  52. \(uniform) numbers.
  53. #+source: rand(n)
  54. #+begin_src R
  55. runif(n)
  56. #+end_src")
  57. (defvar org-babel-function-def-export-indent 4
  58. "Number of characters to indent a source block on export.
  59. When exporting a source block function, the block contents will
  60. be indented by this many characters. See
  61. `org-babel-function-def-export-name' for the definition of a
  62. source block function.")
  63. (defun org-babel-exp-src-blocks (body &rest headers)
  64. "Process source block for export.
  65. Depending on the 'export' headers argument in replace the source
  66. code block with...
  67. both ---- display the code and the results
  68. code ---- the default, display the code inside the block but do
  69. not process
  70. results - just like none only the block is run on export ensuring
  71. that it's results are present in the org-mode buffer
  72. none ----- do not display either code or results upon export"
  73. (interactive)
  74. (message "org-babel-exp processing...")
  75. (save-excursion
  76. (goto-char (match-beginning 0))
  77. (let* ((raw-header (match-string 3))
  78. (info (org-babel-get-src-block-info))
  79. (lang (nth 0 info))
  80. (lang-headers
  81. (intern (concat "org-babel-default-header-args:" lang)))
  82. (raw-params
  83. (org-babel-parse-header-arguments
  84. (org-babel-clean-text-properties
  85. (mapconcat #'identity (cdr (split-string raw-header)) " "))))
  86. (heading (nth 4 (ignore-errors (org-heading-components))))
  87. (link (when org-current-export-file
  88. (org-make-link-string
  89. (if heading
  90. (concat org-current-export-file "::" heading)
  91. org-current-export-file))))
  92. (export-buffer (current-buffer)))
  93. ;; bail if we couldn't get any info from the block
  94. (when info
  95. (when link
  96. ;; resolve parameters in the original file so that headline
  97. ;; and file-wide parameters are included
  98. ;; attempt to go to the same heading in the original file
  99. (set-buffer (get-file-buffer org-current-export-file))
  100. (save-restriction
  101. (condition-case nil
  102. (org-open-link-from-string link)
  103. (error (when heading
  104. (goto-char (point-min))
  105. (re-search-forward (regexp-quote heading) nil t))))
  106. (setf (nth 2 info)
  107. (org-babel-merge-params
  108. org-babel-default-header-args
  109. (org-babel-params-from-buffer)
  110. (org-babel-params-from-properties lang)
  111. (if (boundp lang-headers) (eval lang-headers) nil)
  112. raw-params)))
  113. (set-buffer export-buffer))
  114. ;; expand noweb references in the original file
  115. (setf (nth 1 info)
  116. (if (and (cdr (assoc :noweb (nth 2 info)))
  117. (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
  118. (org-babel-expand-noweb-references
  119. info (get-file-buffer org-current-export-file))
  120. (nth 1 info))))
  121. (org-babel-exp-do-export info 'block))))
  122. (defun org-babel-exp-inline-src-blocks (start end)
  123. "Process inline source blocks between START and END for export.
  124. See `org-babel-exp-src-blocks' for export options, currently the
  125. options and are taken from `org-babel-default-inline-header-args'."
  126. (interactive)
  127. (save-excursion
  128. (goto-char start)
  129. (while (and (< (point) end)
  130. (re-search-forward org-babel-inline-src-block-regexp end t))
  131. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  132. (params (nth 2 info))
  133. (replacement
  134. (save-match-data
  135. (if (org-babel-in-example-or-verbatim)
  136. (buffer-substring (match-beginning 0) (match-end 0))
  137. ;; expand noweb references in the original file
  138. (setf (nth 1 info)
  139. (if (and (cdr (assoc :noweb params))
  140. (string= "yes" (cdr (assoc :noweb params))))
  141. (org-babel-expand-noweb-references
  142. info (get-file-buffer org-current-export-file))
  143. (nth 1 info)))
  144. (org-babel-exp-do-export info 'inline)))))
  145. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  146. (replace-match replacement t t nil 1)))))
  147. (defun org-exp-res/src-name-cleanup ()
  148. "Clean up #+results and #+srcname lines for export.
  149. This function should only be called after all block processing
  150. has taken place."
  151. (interactive)
  152. (save-excursion
  153. (goto-char (point-min))
  154. (while (org-re-search-forward-unprotected
  155. (concat
  156. "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
  157. nil t)
  158. (delete-region
  159. (progn (beginning-of-line) (point))
  160. (progn (end-of-line) (+ 1 (point)))))))
  161. (defun org-babel-in-example-or-verbatim ()
  162. "Return true if point is in example or verbatim code.
  163. Example and verbatim code include escaped portions of
  164. an org-mode buffer code that should be treated as normal
  165. org-mode text."
  166. (or (org-in-indented-comment-line)
  167. (save-excursion
  168. (save-match-data
  169. (goto-char (point-at-bol))
  170. (looking-at "[ \t]*:[ \t]")))
  171. (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  172. (defun org-babel-exp-lob-one-liners (start end)
  173. "Process Library of Babel calls between START and END for export.
  174. See `org-babel-exp-src-blocks' for export options. Currently the
  175. options are taken from `org-babel-default-header-args'."
  176. (interactive)
  177. (let (replacement)
  178. (save-excursion
  179. (goto-char start)
  180. (while (and (< (point) end)
  181. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  182. (setq replacement
  183. (let ((lob-info (org-babel-lob-get-info)))
  184. (save-match-data
  185. (org-babel-exp-do-export
  186. (list "emacs-lisp" "results"
  187. (org-babel-merge-params
  188. org-babel-default-header-args
  189. (org-babel-params-from-buffer)
  190. (org-babel-params-from-properties)
  191. (org-babel-parse-header-arguments
  192. (org-babel-clean-text-properties
  193. (concat ":var results="
  194. (mapconcat #'identity
  195. (butlast lob-info) " ")))))
  196. (car (last lob-info)))
  197. 'lob))))
  198. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  199. (replace-match replacement t t)))))
  200. (defun org-babel-exp-do-export (info type)
  201. "Return a string with the exported content of a code block.
  202. The function respects the value of the :exports header argument."
  203. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  204. (when (and session
  205. (not (equal "none" session)))
  206. (org-babel-exp-results info type 'silent))))
  207. (clean () (org-babel-remove-result info)))
  208. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  209. ('none (silently) (clean) "")
  210. ('code (silently) (clean) (org-babel-exp-code info type))
  211. ('results (org-babel-exp-results info type))
  212. ('both (concat (org-babel-exp-code info type)
  213. "\n\n"
  214. (org-babel-exp-results info type))))))
  215. (defvar backend)
  216. (defun org-babel-exp-code (info type)
  217. "Prepare and return code in the current code block for export.
  218. Code is prepared in a manner suitable for export by
  219. org-mode. This function is called by `org-babel-exp-do-export'.
  220. The code block is not evaluated."
  221. (let ((lang (nth 0 info))
  222. (body (nth 1 info))
  223. (switches (nth 3 info))
  224. (name (nth 4 info))
  225. (args (mapcar
  226. #'cdr
  227. (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
  228. (case type
  229. ('inline (format "=%s=" body))
  230. ('block
  231. (let ((str
  232. (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  233. (if (and body (string-match "\n$" body))
  234. "" "\n"))))
  235. (when name
  236. (add-text-properties
  237. 0 (length str)
  238. (list 'org-caption
  239. (format "%s(%s)"
  240. name
  241. (mapconcat #'identity args ", ")))
  242. str))
  243. str))
  244. ('lob
  245. (let ((call-line (and (string-match "results=" (car args))
  246. (substring (car args) (match-end 0)))))
  247. (cond
  248. ((eq backend 'html)
  249. (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
  250. call-line))
  251. ((format ": %s\n" call-line))))))))
  252. (defun org-babel-exp-results (info type &optional silent)
  253. "Evaluate and return the results of the current code block for export.
  254. Results are prepared in a manner suitable for export by org-mode.
  255. This function is called by `org-babel-exp-do-export'. The code
  256. block will be evaluated. Optional argument SILENT can be used to
  257. inhibit insertion of results into the buffer."
  258. (if org-export-babel-evaluate
  259. (let ((lang (nth 0 info))
  260. (body (nth 1 info))
  261. (params
  262. ;; lets ensure that we lookup references in the original file
  263. (mapcar
  264. (lambda (pair)
  265. (if (and org-current-export-file
  266. (eq (car pair) :var)
  267. (string-match org-babel-ref-split-regexp (cdr pair))
  268. (equal :ob-must-be-reference
  269. (org-babel-ref-literal
  270. (match-string 2 (cdr pair)))))
  271. `(:var . ,(concat (match-string 1 (cdr pair))
  272. "=" org-current-export-file
  273. ":" (match-string 2 (cdr pair))))
  274. pair))
  275. (nth 2 info))))
  276. ;; skip code blocks which we can't evaluate
  277. (if (fboundp (intern (concat "org-babel-execute:" lang)))
  278. (case type
  279. ('inline
  280. (let ((raw (org-babel-execute-src-block
  281. nil info '((:results . "silent"))))
  282. (result-params (split-string
  283. (cdr (assoc :results params)))))
  284. (unless silent
  285. (cond ;; respect the value of the :results header argument
  286. ((member "file" result-params)
  287. (org-babel-result-to-file raw))
  288. ((or (member "raw" result-params)
  289. (member "org" result-params))
  290. (format "%s" raw))
  291. ((member "code" result-params)
  292. (format "src_%s{%s}" lang raw))
  293. (t
  294. (if (stringp raw)
  295. (if (= 0 (length raw)) "=(no results)="
  296. (format "%s" raw))
  297. (format "%S" raw)))))))
  298. ('block
  299. (org-babel-execute-src-block
  300. nil info (org-babel-merge-params
  301. params
  302. `((:results . ,(if silent "silent" "replace")))))
  303. "")
  304. ('lob
  305. (save-excursion
  306. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  307. (org-babel-execute-src-block
  308. nil info (org-babel-merge-params
  309. params
  310. `((:results . ,(if silent "silent" "replace")))))
  311. "")))
  312. ""))
  313. ""))
  314. (provide 'ob-exp)
  315. ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
  316. ;;; ob-exp.el ends here