ob-exp.el 10 KB

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