ob-exp.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. (org-open-link-from-string link)
  102. (setf (nth 2 info)
  103. (org-babel-merge-params
  104. org-babel-default-header-args
  105. (org-babel-params-from-buffer)
  106. (org-babel-params-from-properties lang)
  107. (if (boundp lang-headers) (eval lang-headers) nil)
  108. raw-params)))
  109. (set-buffer export-buffer))
  110. ;; expand noweb references in the original file
  111. (setf (nth 1 info)
  112. (if (and (cdr (assoc :noweb (nth 2 info)))
  113. (string= "yes" (cdr (assoc :noweb (nth 2 info)))))
  114. (org-babel-expand-noweb-references
  115. info (get-file-buffer org-current-export-file))
  116. (nth 1 info))))
  117. (org-babel-exp-do-export info 'block))))
  118. (defun org-babel-exp-inline-src-blocks (start end)
  119. "Process inline source blocks between START and END for export.
  120. See `org-babel-exp-src-blocks' for export options, currently the
  121. options and are taken from `org-babel-default-inline-header-args'."
  122. (interactive)
  123. (save-excursion
  124. (goto-char start)
  125. (while (and (< (point) end)
  126. (re-search-forward org-babel-inline-src-block-regexp end t))
  127. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  128. (params (nth 2 info))
  129. (replacement
  130. (save-match-data
  131. (if (org-babel-in-example-or-verbatim)
  132. (buffer-substring (match-beginning 0) (match-end 0))
  133. ;; expand noweb references in the original file
  134. (setf (nth 1 info)
  135. (if (and (cdr (assoc :noweb params))
  136. (string= "yes" (cdr (assoc :noweb params))))
  137. (org-babel-expand-noweb-references
  138. info (get-file-buffer org-current-export-file))
  139. (nth 1 info)))
  140. (org-babel-exp-do-export info 'inline)))))
  141. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  142. (replace-match replacement t t nil 1)))))
  143. (defun org-exp-res/src-name-cleanup ()
  144. "Clean up #+results and #+srcname lines for export.
  145. This function should only be called after all block processing
  146. has taken place."
  147. (interactive)
  148. (save-excursion
  149. (goto-char (point-min))
  150. (while (org-re-search-forward-unprotected
  151. (concat
  152. "\\("org-babel-src-name-regexp"\\|"org-babel-result-regexp"\\)")
  153. nil t)
  154. (delete-region
  155. (progn (beginning-of-line) (point))
  156. (progn (end-of-line) (+ 1 (point)))))))
  157. (defun org-babel-in-example-or-verbatim ()
  158. "Return true if point is in example or verbatim code.
  159. Example and verbatim code include escaped portions of
  160. an org-mode buffer code that should be treated as normal
  161. org-mode text."
  162. (or (org-in-indented-comment-line)
  163. (save-excursion
  164. (save-match-data
  165. (goto-char (point-at-bol))
  166. (looking-at "[ \t]*:[ \t]")))
  167. (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  168. (defun org-babel-exp-lob-one-liners (start end)
  169. "Process Library of Babel calls between START and END for export.
  170. See `org-babel-exp-src-blocks' for export options. Currently the
  171. options are taken from `org-babel-default-header-args'."
  172. (interactive)
  173. (let (replacement)
  174. (save-excursion
  175. (goto-char start)
  176. (while (and (< (point) end)
  177. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  178. (setq replacement
  179. (let ((lob-info (org-babel-lob-get-info)))
  180. (save-match-data
  181. (org-babel-exp-do-export
  182. (list "emacs-lisp" "results"
  183. (org-babel-merge-params
  184. org-babel-default-header-args
  185. (org-babel-params-from-buffer)
  186. (org-babel-params-from-properties)
  187. (org-babel-parse-header-arguments
  188. (org-babel-clean-text-properties
  189. (concat ":var results="
  190. (mapconcat #'identity
  191. (butlast lob-info) " ")))))
  192. (car (last lob-info)))
  193. 'lob))))
  194. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  195. (replace-match replacement t t)))))
  196. (defun org-babel-exp-do-export (info type)
  197. "Return a string with the exported content of a code block.
  198. The function respects the value of the :exports header argument."
  199. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  200. (when (and session
  201. (not (equal "none" session)))
  202. (org-babel-exp-results info type 'silent))))
  203. (clean () (org-babel-remove-result info)))
  204. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  205. ('none (silently) (clean) "")
  206. ('code (silently) (clean) (org-babel-exp-code info type))
  207. ('results (org-babel-exp-results info type))
  208. ('both (concat (org-babel-exp-code info type)
  209. "\n\n"
  210. (org-babel-exp-results info type))))))
  211. (defvar backend)
  212. (defun org-babel-exp-code (info type)
  213. "Prepare and return code in the current code block for export.
  214. Code is prepared in a manner suitable for export by
  215. org-mode. This function is called by `org-babel-exp-do-export'.
  216. The code block is not evaluated."
  217. (let ((lang (nth 0 info))
  218. (body (nth 1 info))
  219. (switches (nth 3 info))
  220. (name (nth 4 info))
  221. (args (mapcar
  222. #'cdr
  223. (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
  224. (case type
  225. ('inline (format "=%s=" body))
  226. ('block
  227. (let ((str
  228. (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  229. (if (and body (string-match "\n$" body))
  230. "" "\n"))))
  231. (when name
  232. (add-text-properties
  233. 0 (length str)
  234. (list 'org-caption
  235. (format "%s(%s)"
  236. name
  237. (mapconcat #'identity args ", ")))
  238. str))
  239. str))
  240. ('lob
  241. (let ((call-line (and (string-match "results=" (car args))
  242. (substring (car args) (match-end 0)))))
  243. (cond
  244. ((eq backend 'html)
  245. (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
  246. call-line))
  247. ((format ": %s\n" call-line))))))))
  248. (defun org-babel-exp-results (info type &optional silent)
  249. "Evaluate and return the results of the current code block for export.
  250. Results are prepared in a manner suitable for export by org-mode.
  251. This function is called by `org-babel-exp-do-export'. The code
  252. block will be evaluated. Optional argument SILENT can be used to
  253. inhibit insertion of results into the buffer."
  254. (if org-export-babel-evaluate
  255. (let ((lang (nth 0 info))
  256. (body (nth 1 info))
  257. (params
  258. ;; lets ensure that we lookup references in the original file
  259. (mapcar
  260. (lambda (pair)
  261. (if (and org-current-export-file
  262. (eq (car pair) :var)
  263. (string-match org-babel-ref-split-regexp (cdr pair))
  264. (equal :ob-must-be-reference
  265. (org-babel-ref-literal
  266. (match-string 2 (cdr pair)))))
  267. `(:var . ,(concat (match-string 1 (cdr pair))
  268. "=" org-current-export-file
  269. ":" (match-string 2 (cdr pair))))
  270. pair))
  271. (nth 2 info))))
  272. ;; skip code blocks which we can't evaluate
  273. (if (fboundp (intern (concat "org-babel-execute:" lang)))
  274. (case type
  275. ('inline
  276. (let ((raw (org-babel-execute-src-block
  277. nil info '((:results . "silent"))))
  278. (result-params (split-string
  279. (cdr (assoc :results params)))))
  280. (unless silent
  281. (cond ;; respect the value of the :results header argument
  282. ((member "file" result-params)
  283. (org-babel-result-to-file raw))
  284. ((or (member "raw" result-params)
  285. (member "org" result-params))
  286. (format "%s" raw))
  287. ((member "code" result-params)
  288. (format "src_%s{%s}" lang raw))
  289. (t
  290. (if (stringp raw)
  291. (if (= 0 (length raw)) "=(no results)="
  292. (format "%s" raw))
  293. (format "%S" raw)))))))
  294. ('block
  295. (org-babel-execute-src-block
  296. nil info (org-babel-merge-params
  297. params
  298. `((:results . ,(if silent "silent" "replace")))))
  299. "")
  300. ('lob
  301. (save-excursion
  302. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  303. (org-babel-execute-src-block
  304. nil info (org-babel-merge-params
  305. params
  306. `((:results . ,(if silent "silent" "replace")))))
  307. "")))
  308. ""))
  309. ""))
  310. (provide 'ob-exp)
  311. ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
  312. ;;; ob-exp.el ends here