ob-exp.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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: 0.01
  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. (defvar org-babel-function-def-export-keyword "function"
  38. "When exporting a source block function, this keyword will
  39. appear in the exported version in the place of source name
  40. line. A source block is considered to be a source block function
  41. if the source name is present and is followed by a parenthesized
  42. argument list. The parentheses may be empty or contain
  43. whitespace. An example is the following which generates n random
  44. \(uniform) numbers.
  45. #+source: rand(n)
  46. #+begin_src R
  47. runif(n)
  48. #+end_src")
  49. (defvar org-babel-function-def-export-indent 4
  50. "When exporting a source block function, the block contents
  51. will be indented by this many characters. See
  52. `org-babel-function-def-export-name' for the definition of a
  53. source block function.")
  54. (defun org-babel-exp-src-blocks (body &rest headers)
  55. "Process src block for export. Depending on the 'export'
  56. headers argument in replace the source code block with...
  57. both ---- display the code and the results
  58. code ---- the default, display the code inside the block but do
  59. not process
  60. results - just like none only the block is run on export ensuring
  61. that it's results are present in the org-mode buffer
  62. none ----- do not display either code or results upon export"
  63. (interactive)
  64. (message "org-babel-exp processing...")
  65. (save-excursion
  66. (goto-char (match-beginning 0))
  67. (let* ((info (org-babel-get-src-block-info))
  68. (params (nth 2 info)))
  69. ;; expand noweb references in the original file
  70. (setf (nth 1 info)
  71. (if (and (cdr (assoc :noweb params))
  72. (string= "yes" (cdr (assoc :noweb params))))
  73. (org-babel-expand-noweb-references
  74. info (get-file-buffer org-current-export-file))
  75. (nth 1 info)))
  76. (org-babel-exp-do-export info 'block))))
  77. (defun org-babel-exp-inline-src-blocks (start end)
  78. "Process inline src blocks between START and END for export.
  79. See `org-babel-exp-src-blocks' for export options, currently the
  80. options and are taken from `org-babel-default-inline-header-args'."
  81. (interactive)
  82. (save-excursion
  83. (goto-char start)
  84. (while (and (< (point) end)
  85. (re-search-forward org-babel-inline-src-block-regexp end t))
  86. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  87. (params (nth 2 info))
  88. (replacement
  89. (save-match-data
  90. (if (org-babel-in-example-or-verbatim)
  91. (buffer-substring (match-beginning 0) (match-end 0))
  92. ;; expand noweb references in the original file
  93. (setf (nth 1 info)
  94. (if (and (cdr (assoc :noweb params))
  95. (string= "yes" (cdr (assoc :noweb params))))
  96. (org-babel-expand-noweb-references
  97. info (get-file-buffer org-current-export-file))
  98. (nth 1 info)))
  99. (org-babel-exp-do-export info 'inline)))))
  100. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  101. (replace-match replacement t t nil 1)))))
  102. (defun org-exp-res/src-name-cleanup ()
  103. "Cleanup leftover #+results and #+srcname lines as part of the
  104. org export cycle. This should only be called after all block
  105. processing has taken place."
  106. (interactive)
  107. (save-excursion
  108. (goto-char (point-min))
  109. (while (org-re-search-forward-unprotected
  110. (concat
  111. "\\("org-babel-source-name-regexp"\\|"org-babel-result-regexp"\\)")
  112. nil t)
  113. (delete-region
  114. (progn (beginning-of-line) (point))
  115. (progn (end-of-line) (+ 1 (point)))))))
  116. (defun org-babel-in-example-or-verbatim ()
  117. "Return true if the point is currently in an escaped portion of
  118. an org-mode buffer code which should be treated as normal
  119. org-mode text."
  120. (or (org-in-indented-comment-line)
  121. (save-excursion
  122. (save-match-data
  123. (goto-char (point-at-bol))
  124. (looking-at "[ \t]*:[ \t]")))
  125. (org-in-regexps-block-p "^[ \t]*#\\+begin_src" "^[ \t]*#\\+end_src")))
  126. (defun org-babel-exp-lob-one-liners (start end)
  127. "Process #+lob (Library of Babel) calls between START and END for export.
  128. See `org-babel-exp-src-blocks' for export options. Currently the
  129. options are taken from `org-babel-default-header-args'."
  130. (interactive)
  131. (let (replacement)
  132. (save-excursion
  133. (goto-char start)
  134. (while (and (< (point) end)
  135. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  136. (setq replacement
  137. (let ((lob-info (org-babel-lob-get-info)))
  138. (save-match-data
  139. (org-babel-exp-do-export
  140. (list "emacs-lisp" "results"
  141. (org-babel-merge-params
  142. org-babel-default-header-args
  143. (org-babel-parse-header-arguments
  144. (org-babel-clean-text-properties
  145. (concat ":var results="
  146. (mapconcat #'identity
  147. (butlast lob-info) " ")))))
  148. (car (last lob-info)))
  149. 'lob))))
  150. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  151. (replace-match replacement t t)))))
  152. (defun org-babel-exp-do-export (info type)
  153. "Return a string containing the exported content of the current
  154. code block respecting the value of the :exports header argument."
  155. (flet ((silently () (let ((session (cdr (assoc :session (nth 2 info)))))
  156. (when (and session
  157. (not (equal "none" session))
  158. (not (assoc :noeval (nth 2 info))))
  159. (org-babel-exp-results info type 'silent))))
  160. (clean () (org-babel-remove-result info)))
  161. (case (intern (or (cdr (assoc :exports (nth 2 info))) "code"))
  162. ('none (silently) (clean) "")
  163. ('code (silently) (clean) (org-babel-exp-code info type))
  164. ('results (org-babel-exp-results info type))
  165. ('both (concat (org-babel-exp-code info type)
  166. "\n\n"
  167. (org-babel-exp-results info type))))))
  168. (defvar backend)
  169. (defun org-babel-exp-code (info type)
  170. "Return the code the current code block in a manner suitable
  171. for exportation by org-mode. This function is called by
  172. `org-babel-exp-do-export'. The code block will not be
  173. evaluated."
  174. (let ((lang (nth 0 info))
  175. (body (nth 1 info))
  176. (switches (nth 3 info))
  177. (name (nth 4 info))
  178. (args (mapcar
  179. #'cdr
  180. (org-remove-if-not (lambda (el) (eq :var (car el))) (nth 2 info)))))
  181. (case type
  182. ('inline (format "=%s=" body))
  183. ('block
  184. (let ((str
  185. (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  186. (if (and body (string-match "\n$" body))
  187. "" "\n"))))
  188. (when name
  189. (add-text-properties
  190. 0 (length str)
  191. (list 'org-caption
  192. (format "%s(%s)"
  193. name
  194. (mapconcat #'identity args ", ")))
  195. str))
  196. str))
  197. ('lob
  198. (let ((call-line (and (string-match "results=" (car args))
  199. (substring (car args) (match-end 0)))))
  200. (cond
  201. ((eq backend 'html)
  202. (format "\n#+HTML: <label class=\"org-src-name\">%s</label>\n"
  203. call-line))
  204. ((format ": %s\n" call-line))))))))
  205. (defun org-babel-exp-results (info type &optional silent)
  206. "Return the results of the current code block in a manner
  207. suitable for exportation by org-mode. This function is called by
  208. `org-babel-exp-do-export'. The code block will be evaluated.
  209. Optional argument SILENT can be used to inhibit insertion of
  210. results into the buffer."
  211. (let ((lang (nth 0 info))
  212. (body (nth 1 info))
  213. (params
  214. ;; lets ensure that we lookup references in the original file
  215. (mapcar
  216. (lambda (pair)
  217. (if (and org-current-export-file
  218. (eq (car pair) :var)
  219. (string-match org-babel-ref-split-regexp (cdr pair))
  220. (equal :ob-must-be-reference
  221. (org-babel-ref-literal (match-string 2 (cdr pair)))))
  222. `(:var . ,(concat (match-string 1 (cdr pair))
  223. "=" org-current-export-file
  224. ":" (match-string 2 (cdr pair))))
  225. pair))
  226. (nth 2 info))))
  227. ;; skip code blocks which we can't evaluate
  228. (when (fboundp (intern (concat "org-babel-execute:" lang)))
  229. (case type
  230. ('inline
  231. (let ((raw (org-babel-execute-src-block
  232. nil info '((:results . "silent"))))
  233. (result-params (split-string (cdr (assoc :results params)))))
  234. (unless silent
  235. (cond ;; respect the value of the :results header argument
  236. ((member "file" result-params)
  237. (org-babel-result-to-file raw))
  238. ((or (member "raw" result-params) (member "org" result-params))
  239. (format "%s" raw))
  240. ((member "code" result-params)
  241. (format "src_%s{%s}" lang raw))
  242. (t
  243. (if (stringp raw)
  244. (if (= 0 (length raw)) "=(no results)="
  245. (format "%s" raw))
  246. (format "%S" raw)))))))
  247. ('block
  248. (org-babel-execute-src-block
  249. nil info (org-babel-merge-params
  250. params `((:results . ,(if silent "silent" "replace")))))
  251. "")
  252. ('lob
  253. (save-excursion
  254. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  255. (org-babel-execute-src-block
  256. nil info (org-babel-merge-params
  257. params `((:results . ,(if silent "silent" "replace")))))
  258. ""))))))
  259. (provide 'ob-exp)
  260. ;; arch-tag: 523abf4c-76d1-44ed-9f27-e3bddf34bf0f
  261. ;;; ob-exp.el ends here