ob-emacs-lisp.el 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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: 7.5
  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. (eval-when-compile (require 'ob-comint))
  23. (defvar org-babel-default-header-args:emacs-lisp
  24. '((:hlines . "yes") (:colnames . "no"))
  25. "Default arguments for evaluating an emacs-lisp source block.")
  26. (declare-function orgtbl-to-generic "org-table" (table params))
  27. (defun org-babel-expand-body:emacs-lisp (body params)
  28. "Expand BODY according to PARAMS, return the expanded body."
  29. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  30. (result-params (cdr (assoc :result-params params)))
  31. (print-level nil) (print-length nil)
  32. (body (if (> (length vars) 0)
  33. (concat "(let ("
  34. (mapconcat
  35. (lambda (var)
  36. (format "%S" (print `(,(car var) ',(cdr var)))))
  37. vars "\n ")
  38. ")\n" body ")")
  39. body)))
  40. (if (or (member "code" result-params)
  41. (member "pp" result-params))
  42. (concat "(pp " body ")") body)))
  43. (defun org-babel-execute:emacs-lisp (body params)
  44. "Execute a block of emacs-lisp code with Babel."
  45. (save-window-excursion
  46. (org-babel-reassemble-table
  47. (eval (read (format "(progn %s)"
  48. (org-babel-expand-body:emacs-lisp body params))))
  49. (org-babel-pick-name (cdr (assoc :colname-names params))
  50. (cdr (assoc :colnames params)))
  51. (org-babel-pick-name (cdr (assoc :rowname-names params))
  52. (cdr (assoc :rownames params))))))
  53. (provide 'ob-emacs-lisp)
  54. ;; arch-tag: e9a3acca-dc84-472a-9f5a-23c35befbcd6
  55. ;;; ob-emacs-lisp.el ends here