ob-emacs-lisp.el 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating emacs-lisp code
  19. ;;; Code:
  20. (require 'ob)
  21. (eval-when-compile (require 'ob-comint))
  22. (defvar org-babel-default-header-args:emacs-lisp
  23. '((:hlines . "yes") (:colnames . "no"))
  24. "Default arguments for evaluating an emacs-lisp source block.")
  25. (declare-function orgtbl-to-generic "org-table" (table params))
  26. (defun org-babel-expand-body:emacs-lisp (body params)
  27. "Expand BODY according to PARAMS, return the expanded body."
  28. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  29. (result-params (cdr (assoc :result-params params)))
  30. (print-level nil) (print-length nil)
  31. (body (if (> (length vars) 0)
  32. (concat "(let ("
  33. (mapconcat
  34. (lambda (var)
  35. (format "%S" (print `(,(car var) ',(cdr var)))))
  36. vars "\n ")
  37. ")\n" body "\n)")
  38. (concat body "\n"))))
  39. (if (or (member "code" result-params)
  40. (member "pp" result-params))
  41. (concat "(pp " body ")") body)))
  42. (defun org-babel-execute:emacs-lisp (body params)
  43. "Execute a block of emacs-lisp code with Babel."
  44. (save-window-excursion
  45. ((lambda (result)
  46. (if (or (member "scalar" (cdr (assoc :result-params params)))
  47. (member "verbatim" (cdr (assoc :result-params params))))
  48. (let ((print-level nil)
  49. (print-length nil))
  50. (format "%S" result))
  51. (org-babel-reassemble-table
  52. result
  53. (org-babel-pick-name (cdr (assoc :colname-names params))
  54. (cdr (assoc :colnames params)))
  55. (org-babel-pick-name (cdr (assoc :rowname-names params))
  56. (cdr (assoc :rownames params))))))
  57. (eval (read (format (if (member "output"
  58. (cdr (assoc :result-params params)))
  59. "(with-output-to-string %s)"
  60. "(progn %s)")
  61. (org-babel-expand-body:emacs-lisp body params)))))))
  62. (provide 'ob-emacs-lisp)
  63. ;;; ob-emacs-lisp.el ends here