org-babel-exp.el 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ;;; org-babel-exp.el --- Exportation of org-babel source blocks
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  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. (defun org-babel-exp-src-blocks (body &rest headers)
  30. "Process src block for export. Depending on the 'export'
  31. headers argument in replace the source code block with...
  32. both ---- display the code and the results
  33. code ---- the default, display the code inside the block but do
  34. not process
  35. results - just like none only the block is run on export ensuring
  36. that it's results are present in the org-mode buffer
  37. none ----- do not display either code or results upon export"
  38. (interactive)
  39. (unless headers (error "org-babel can't process a source block without knowing the source code"))
  40. (message "org-babel-exp processing...")
  41. (let* ((lang (car headers))
  42. (lang-headers (intern (concat "org-babel-default-header-args:" lang)))
  43. (params (org-babel-merge-params
  44. org-babel-default-header-args
  45. (org-babel-params-from-properties)
  46. (if (boundp lang-headers) (eval lang-headers) nil)
  47. (org-babel-parse-header-arguments
  48. (mapconcat #'identity (cdr headers) " ")))))
  49. (org-babel-exp-do-export lang body params)))
  50. (defun org-babel-exp-inline-src-blocks (start end)
  51. "Process inline src blocks between START and END for export.
  52. See `org-babel-exp-src-blocks' for export options, currently the
  53. options and are taken from `org-babel-defualt-inline-header-args'."
  54. (interactive)
  55. (save-excursion
  56. (goto-char start)
  57. (while (and (< (point) end) (re-search-forward org-babel-inline-src-block-regexp end t))
  58. (let* ((info (save-match-data (org-babel-parse-inline-src-block-match)))
  59. (replacement (save-match-data
  60. (org-babel-exp-do-export (first info) (second info) (third info) t))))
  61. (setf end (+ end (- (length replacement)
  62. (+ 6 (length (first info)) (length (second info))))))
  63. (replace-match replacement t t)))))
  64. (defun org-babel-exp-do-export (lang body params &optional inline)
  65. (case (intern (or (cdr (assoc :exports params)) "code"))
  66. ('none "")
  67. ('code (org-babel-exp-code body lang params inline))
  68. ('results (save-excursion
  69. ;; org-exp-blocks places us at the end of the block
  70. (re-search-backward org-babel-src-block-regexp nil t)
  71. (org-babel-execute-src-block) ""))
  72. ('both (concat (org-babel-exp-code body lang params inline)
  73. "\n\n"
  74. (org-babel-exp-results body lang params inline)))))
  75. (defun org-babel-exp-code (body lang params &optional inline)
  76. (if inline
  77. (format "=%s=" body)
  78. (format "#+BEGIN_SRC %s\n%s%s\n#+END_SRC" lang body
  79. (if (string-match "\n$" body) "" "\n"))))
  80. (provide 'org-babel-exp)
  81. ;;; org-babel-exp.el ends here