ob-emacs-lisp.el 2.9 KB

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