org-babel-exp.el 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. (defun org-babel-exp-src-blocks (body &rest headers)
  31. "Process src block for export. Depending on the 'export'
  32. headers argument in replace the source code block with...
  33. both ---- display the code and the results
  34. code ---- the default, display the code inside the block but do
  35. not process
  36. results - just like none only the block is run on export ensuring
  37. that it's results are present in the org-mode buffer
  38. none ----- do not display either code or results upon export"
  39. (interactive)
  40. (message "org-babel-exp processing...")
  41. (let ((info (save-excursion
  42. (if (re-search-backward org-babel-src-block-regexp nil t)
  43. (org-babel-get-src-block-info)
  44. (error "Failed to find src block.")))))
  45. (org-babel-exp-do-export info 'block)))
  46. (defun org-babel-exp-inline-src-blocks (start end)
  47. "Process inline src blocks between START and END for export.
  48. See `org-babel-exp-src-blocks' for export options, currently the
  49. options and are taken from `org-babel-defualt-inline-header-args'."
  50. (interactive)
  51. (save-excursion
  52. (goto-char start)
  53. (while (and (< (point) end)
  54. (re-search-forward org-babel-inline-src-block-regexp end t))
  55. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  56. (replacement (save-match-data
  57. (org-babel-exp-do-export info 'inline))))
  58. (setq end (+ end (- (length replacement) (length (match-string 1)))))
  59. (replace-match replacement t t nil 1)))))
  60. (defun org-babel-exp-lob-one-liners (start end)
  61. "Process #+lob (Library of Babel) calls between START and END for export.
  62. See `org-babel-exp-src-blocks' for export options. Currently the
  63. options are taken from `org-babel-default-header-args'."
  64. (interactive)
  65. (let (replacement)
  66. (save-excursion
  67. (goto-char start)
  68. (while (and (< (point) end)
  69. (re-search-forward org-babel-lob-one-liner-regexp nil t))
  70. (setq replacement
  71. (save-match-data
  72. (org-babel-exp-do-export
  73. (list "emacs-lisp" "results"
  74. (org-babel-merge-params
  75. org-babel-default-header-args
  76. (org-babel-parse-header-arguments
  77. (org-babel-clean-text-properties
  78. (concat ":var results="
  79. (mapconcat #'identity (org-babel-lob-get-info) " "))))))
  80. 'lob)))
  81. (setq end (+ end (- (length replacement) (length (match-string 0)))))
  82. (replace-match replacement t t)))))
  83. (defun org-babel-exp-do-export (info type)
  84. (case (intern (or (cdr (assoc :exports (third info))) "code"))
  85. ('none "")
  86. ('code (org-babel-exp-code info type))
  87. ('results (org-babel-exp-results info type))
  88. ('both (concat (org-babel-exp-code info type)
  89. "\n\n"
  90. (org-babel-exp-results info type)))))
  91. (defun org-babel-exp-code (info type)
  92. (let ((lang (first info))
  93. (body (second info))
  94. (switches (fourth info)))
  95. (case type
  96. ('inline (format "=%s=" (second info)))
  97. ('block (format "#+BEGIN_SRC %s %s\n%s%s#+END_SRC" lang switches body
  98. (if (string-match "\n$" body) "" "\n")))
  99. ('lob (save-excursion
  100. (re-search-backward org-babel-lob-one-liner-regexp)
  101. (format "#+BEGIN_SRC org-babel-lob\n%s\n#+END_SRC"
  102. (first (org-babel-lob-get-info))))))))
  103. (defun org-babel-exp-results (info type)
  104. (let ((lang (first info))
  105. (body (second info))
  106. (params
  107. ;; lets ensure that we lookup references in the original file
  108. (mapcar (lambda (pair)
  109. (if (and org-current-export-file
  110. (eq (car pair) :var)
  111. (string-match org-babel-ref-split-regexp (cdr pair)))
  112. `(:var . ,(concat (match-string 1 (cdr pair))
  113. "=" org-current-export-file
  114. ":" (match-string 2 (cdr pair))))
  115. pair))
  116. (third info))))
  117. (case type
  118. ('inline
  119. (let ((raw (org-babel-execute-src-block
  120. nil info '((:results . "silent"))))
  121. (result-params (split-string (cdr (assoc :results params)))))
  122. (cond ;; respect the value of the :results header argument
  123. ((member "file" result-params)
  124. (org-babel-result-to-file raw))
  125. ((or (member "raw" result-params) (member "org" result-params))
  126. raw)
  127. ((member "code" result-params)
  128. (format "src_%s{%s}" lang raw))
  129. (t
  130. (if (stringp raw)
  131. (if (= 0 (length raw)) "=(no results)="
  132. (format "=%s=" raw))
  133. (format "=%S=" raw))))))
  134. ('block
  135. (save-excursion ;; org-exp-blocks places us at the end of the block
  136. (re-search-backward org-babel-src-block-regexp nil t)
  137. (org-babel-execute-src-block
  138. nil nil (org-babel-merge-params params '((:results . "replace")))) ""))
  139. ('lob
  140. (save-excursion
  141. (re-search-backward org-babel-lob-one-liner-regexp nil t)
  142. (org-babel-execute-src-block
  143. nil (list lang body (org-babel-merge-params params '((:results . "replace"))))) "")))))
  144. (provide 'org-babel-exp)
  145. ;;; org-babel-exp.el ends here