ob-emacs-lisp.el 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 (if (member "output"
  48. (cdr (assoc :result-params params)))
  49. "(with-output-to-string %s)"
  50. "(progn %s)")
  51. (org-babel-expand-body:emacs-lisp body params))))
  52. (org-babel-pick-name (cdr (assoc :colname-names params))
  53. (cdr (assoc :colnames params)))
  54. (org-babel-pick-name (cdr (assoc :rowname-names params))
  55. (cdr (assoc :rownames params))))))
  56. (provide 'ob-emacs-lisp)
  57. ;; arch-tag: e9a3acca-dc84-472a-9f5a-23c35befbcd6
  58. ;;; ob-emacs-lisp.el ends here