ob-emacs-lisp.el 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
  2. ;; Copyright (C) 2009-2013 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. (defvar org-babel-default-header-args:emacs-lisp
  22. '((:hlines . "yes") (:colnames . "no"))
  23. "Default arguments for evaluating an emacs-lisp source block.")
  24. (declare-function orgtbl-to-generic "org-table" (table params))
  25. (defun org-babel-expand-body:emacs-lisp (body params)
  26. "Expand BODY according to PARAMS, return the expanded body."
  27. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  28. (result-params (cdr (assoc :result-params params)))
  29. (print-level nil) (print-length nil)
  30. (body (if (> (length vars) 0)
  31. (concat "(let ("
  32. (mapconcat
  33. (lambda (var)
  34. (format "%S" (print `(,(car var) ',(cdr var)))))
  35. vars "\n ")
  36. ")\n" body "\n)")
  37. (concat body "\n"))))
  38. (if (or (member "code" result-params)
  39. (member "pp" result-params))
  40. (concat "(pp " body ")") body)))
  41. (defun org-babel-execute:emacs-lisp (body params)
  42. "Execute a block of emacs-lisp code with Babel."
  43. (save-window-excursion
  44. ((lambda (result)
  45. (org-babel-result-cond (cdr (assoc :result-params params))
  46. (let ((print-level nil)
  47. (print-length nil))
  48. (if (or (member "scalar" (cdr (assoc :result-params params)))
  49. (member "verbatim" (cdr (assoc :result-params params))))
  50. (format "%S" result)
  51. (format "%s" result)))
  52. (org-babel-reassemble-table
  53. result
  54. (org-babel-pick-name (cdr (assoc :colname-names params))
  55. (cdr (assoc :colnames params)))
  56. (org-babel-pick-name (cdr (assoc :rowname-names params))
  57. (cdr (assoc :rownames params))))))
  58. (eval (read (format (if (member "output"
  59. (cdr (assoc :result-params params)))
  60. "(with-output-to-string %s)"
  61. "(progn %s)")
  62. (org-babel-expand-body:emacs-lisp body params)))))))
  63. (provide 'ob-emacs-lisp)
  64. ;;; ob-emacs-lisp.el ends here