ob-exp.el 10 KB

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