org-babel-exp.el 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ;;; org-babel-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 'org-babel)
  26. (require 'org-exp-blocks)
  27. (org-export-blocks-add-block '(src org-babel-exp-src-blocks nil))
  28. (add-to-list 'org-export-interblocks '(src org-babel-exp-inline-src-blocks))
  29. (add-to-list 'org-export-interblocks '(lob org-babel-exp-lob-one-liners))
  30. (defvar org-babel-function-def-export-keyword "function"
  31. "When exporting a source block function, this keyword will
  32. appear in the exported version in the place of source name
  33. line. A source block is considered to be a source block function
  34. if the source name is present and is followed by a parenthesized
  35. argument list. The parentheses may be empty or contain
  36. whitespace. An example is the following which generates n random
  37. (uniform) numbers.
  38. #+source: rand(n)
  39. #+begin_src R
  40. runif(n)
  41. #+end_src
  42. ")
  43. (defvar org-babel-function-def-export-indent 4
  44. "When exporting a source block function, the block contents
  45. will be indented by this many characters. See
  46. `org-babel-function-def-export-name' for the definition of a
  47. source block function.")
  48. (defvar obe-marker nil)
  49. (defun org-babel-exp-src-blocks (body &rest headers)
  50. "Process src block for export. Depending on the 'export'
  51. headers argument in replace the source code block with...
  52. both ---- display the code and the results
  53. code ---- the default, display the code inside the block but do
  54. not process
  55. results - just like none only the block is run on export ensuring
  56. that it's results are present in the org-mode buffer
  57. none ----- do not display either code or results upon export"
  58. (interactive)
  59. (message "org-babel-exp processing...")
  60. (or (and (re-search-backward org-babel-src-block-regexp progress-marker t)
  61. (setq progress-marker (match-end 0))
  62. (org-babel-exp-do-export (org-babel-get-src-block-info) 'block))
  63. (save-excursion
  64. (forward-line 0)
  65. (and (org-babel-where-is-src-block-head)
  66. (goto-char (org-babel-where-is-src-block-head))
  67. (org-babel-exp-do-export (org-babel-get-src-block-info) 'block)))
  68. (and (re-search-backward org-block-regexp progress-marker t)
  69. (setq progress-marker (match-end 0))
  70. (match-string 0))
  71. (error "Unmatched block [bug in `org-babel-exp-src-blocks'].")))
  72. (defun org-babel-exp-inline-src-blocks (start end)
  73. "Process inline src blocks between START and END for export.
  74. See `org-babel-exp-src-blocks' for export options, currently the
  75. options and are taken from `org-babel-defualt-inline-header-args'."
  76. (interactive)
  77. (save-excursion
  78. (goto-char start)
  79. (while (and (< (point) end)
  80. (re-search-forward org-babel-inline-src-block-regexp end t))
  81. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  82. (replacement (save-match-data
  83. (org-babel-exp-do-export info 'inline))))
  84. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  85. (replace-match replacement t t nil 1)))))
  86. (defun org-babel-exp-lob-one-liners (start end)
  87. "Process #+lob (Library of Babel) calls between START and END for export.
  88. See `org-babel-exp-src-blocks' for export options. Currently the
  89. options are taken from `org-babel-default-header-args'."
  90. (interactive)
  91. (let (replacement)
  92. (save-excursion
  93. (goto-char start)
  94. (while (and (< (point) end)
  95. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  96. (setq replacement
  97. (save-match-data
  98. (org-babel-exp-do-export
  99. (list "emacs-lisp" "results"
  100. (org-babel-merge-params
  101. org-babel-default-header-args
  102. (org-babel-parse-header-arguments
  103. (org-babel-clean-text-properties
  104. (concat ":var results="
  105. (mapconcat #'identity (org-babel-lob-get-info) " "))))))
  106. 'lob)))
  107. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  108. (replace-match replacement t t)))))
  109. (defun org-babel-exp-do-export (info type)
  110. (case (intern (or (cdr (assoc :exports (third info))) "code"))
  111. ('none "")
  112. ('code (org-babel-exp-code info type))
  113. ('results (org-babel-exp-results info type))
  114. ('both (concat (org-babel-exp-code info type)
  115. "\n\n"
  116. (org-babel-exp-results info type)))))
  117. (defun org-babel-exp-code (info type)
  118. (let ((lang (first info))
  119. (body (second info))
  120. (switches (fourth info))
  121. (name (fifth info))
  122. (args (sixth info))
  123. (function-def-line ""))
  124. (case type
  125. ('inline (format "=%s=" (second info)))
  126. ('block
  127. (let ((str (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC\n" lang switches body
  128. (if (string-match "\n$" body) "" "\n"))))
  129. (add-text-properties 0 (length str) (list 'org-caption name) str)
  130. str))
  131. ('lob (save-excursion
  132. (re-search-backward org-babel-lob-one-liner-regexp)
  133. (format "#+BEGIN_SRC org-babel-lob\n%s\n#+END_SRC"
  134. (first (org-babel-lob-get-info))))))))
  135. (defun org-babel-exp-results (info type)
  136. (let ((lang (first info))
  137. (body (second info))
  138. (params
  139. ;; lets ensure that we lookup references in the original file
  140. (mapcar (lambda (pair)
  141. (if (and org-current-export-file
  142. (eq (car pair) :var)
  143. (string-match org-babel-ref-split-regexp (cdr pair)))
  144. `(:var . ,(concat (match-string 1 (cdr pair))
  145. "=" org-current-export-file
  146. ":" (match-string 2 (cdr pair))))
  147. pair))
  148. (third info))))
  149. (case type
  150. ('inline
  151. (let ((raw (org-babel-execute-src-block
  152. nil info '((:results . "silent"))))
  153. (result-params (split-string (cdr (assoc :results params)))))
  154. (cond ;; respect the value of the :results header argument
  155. ((member "file" result-params)
  156. (org-babel-result-to-file raw))
  157. ((or (member "raw" result-params) (member "org" result-params))
  158. raw)
  159. ((member "code" result-params)
  160. (format "src_%s{%s}" lang raw))
  161. (t
  162. (if (stringp raw)
  163. (if (= 0 (length raw)) "=(no results)="
  164. (format "=%s=" raw))
  165. (format "=%S=" raw))))))
  166. ('block
  167. (org-babel-execute-src-block
  168. nil nil (org-babel-merge-params params '((:results . "replace"))))
  169. "")
  170. ('lob
  171. (save-excursion
  172. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  173. (org-babel-execute-src-block
  174. nil (list lang body (org-babel-merge-params params '((:results . "replace"))))) "")))))
  175. (provide 'org-babel-exp)
  176. ;;; org-babel-exp.el ends here