ob-emacs-lisp.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  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. ;; Org-Babel support for evaluating emacs-lisp code
  20. ;;; Code:
  21. (require 'ob)
  22. (defvar org-babel-default-header-args:emacs-lisp
  23. '((:hlines . "yes") (:colnames . "no"))
  24. "Default arguments to use when evaluating an emacs-lisp source block.")
  25. (declare-function org-babel-comint-with-output "ob-comint" (&rest body))
  26. (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
  27. (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
  28. (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
  29. (declare-function orgtbl-to-generic "org-table" (table params))
  30. (defun org-babel-expand-body:emacs-lisp (body params &optional processed-params)
  31. "Expand BODY according to PARAMS, return the expanded body."
  32. (let* ((processed-params (or processed-params (org-babel-process-params params)))
  33. (vars (nth 1 processed-params))
  34. (result-params (nth 2 processed-params))
  35. (print-level nil) (print-length nil)
  36. (body (if (> (length vars) 0)
  37. (concat "(let ("
  38. (mapconcat
  39. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  40. vars "\n ")
  41. ")\n" body ")")
  42. body)))
  43. (if (or (member "code" result-params)
  44. (member "pp" result-params))
  45. (concat "(pp " body ")") body)))
  46. (defun org-babel-execute:emacs-lisp (body params)
  47. "Execute a block of emacs-lisp code with org-babel."
  48. (message "executing emacs-lisp code block...")
  49. (save-window-excursion
  50. (let ((processed-params (org-babel-process-params params)))
  51. (org-babel-reassemble-table
  52. (eval (read (format "(progn %s)"
  53. (org-babel-expand-body:emacs-lisp
  54. body params processed-params))))
  55. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  56. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params)))))))
  57. (provide 'ob-emacs-lisp)
  58. ;; arch-tag: e9a3acca-dc84-472a-9f5a-23c35befbcd6
  59. ;;; ob-emacs-lisp.el ends here