ob-org.el 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; ob-org.el --- org-babel functions for org code block evaluation
  2. ;; Copyright (C) 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This is the simplest of code blocks, where upon evaluation the
  20. ;; contents of the code block are returned in a raw result.
  21. ;;; Code:
  22. (require 'ob)
  23. (declare-function org-load-modules-maybe "org" (&optional force))
  24. (declare-function org-get-local-variables "org" ())
  25. (defvar org-babel-default-header-args:org
  26. '((:results . "raw silent") (:exports . "results"))
  27. "Default arguments for evaluating a org source block.")
  28. (defvar org-babel-org-default-header
  29. "#+TITLE: default empty header\n"
  30. "Default header inserted during export of org blocks.")
  31. (defun org-babel-expand-body:org (body params)
  32. "Expand BODY according to PARAMS, return the expanded body." body)
  33. (defun org-babel-execute:org (body params)
  34. "Execute a block of Org code with.
  35. This function is called by `org-babel-execute-src-block'."
  36. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  37. (body (replace-regexp-in-string "^," "" body)))
  38. (cond
  39. ((member "latex" result-params) (org-babel-org-export body "latex"))
  40. ((member "html" result-params) (org-babel-org-export body "html"))
  41. ((member "ascii" result-params) (org-babel-org-export body "ascii"))
  42. (t body))))
  43. (defvar org-local-vars)
  44. (defun org-babel-org-export (body fmt)
  45. "Export BODY to FMT using Org-mode's export facilities. "
  46. (when (get-buffer " org-mode-tmp")
  47. (error "Nested call to org-export: from org code block exporting results"))
  48. (let ((tmp-file (org-babel-temp-file "org-")))
  49. (with-temp-buffer
  50. (insert org-babel-org-default-header)
  51. (insert body)
  52. (write-file tmp-file)
  53. (org-load-modules-maybe)
  54. (unless org-local-vars
  55. (setq org-local-vars (org-get-local-variables)))
  56. (eval ;; convert to fmt -- mimicking `org-run-like-in-org-mode'
  57. (list 'let org-local-vars
  58. (list (intern (concat "org-export-as-" fmt))
  59. nil nil nil ''string t))))))
  60. (defun org-babel-prep-session:org (session params)
  61. "Return an error because org does not support sessions."
  62. (error "Org does not support sessions"))
  63. (provide 'ob-org)
  64. ;; arch-tag: 130af5fe-cc56-46bd-9508-fa0ebd94cb1f
  65. ;;; ob-org.el ends here