ob-emacs-lisp.el 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://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 <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating emacs-lisp code
  19. ;;; Code:
  20. (require 'ob)
  21. (defconst org-babel-header-args:emacs-lisp '((lexical . :any))
  22. "Emacs-lisp specific header arguments.")
  23. (defvar org-babel-default-header-args:emacs-lisp '((:lexical . "no"))
  24. "Default arguments for evaluating an emacs-lisp source block.
  25. A value of \"yes\" or t causes src blocks to be eval'd using
  26. lexical scoping. It can also be an alist mapping symbols to
  27. their value. It is used as the optional LEXICAL argument to
  28. `eval', which see.")
  29. (defun org-babel-expand-body:emacs-lisp (body params)
  30. "Expand BODY according to PARAMS, return the expanded body."
  31. (let ((vars (org-babel--get-vars params))
  32. (print-level nil)
  33. (print-length nil))
  34. (if (null vars) (concat body "\n")
  35. (format "(let (%s)\n%s\n)"
  36. (mapconcat
  37. (lambda (var)
  38. (format "%S" (print `(,(car var) ',(cdr var)))))
  39. vars "\n ")
  40. 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. (let* ((lexical (cdr (assq :lexical params)))
  45. (result-params (cdr (assq :result-params params)))
  46. (body (format (if (member "output" result-params)
  47. "(with-output-to-string %s\n)"
  48. "(progn %s\n)")
  49. (org-babel-expand-body:emacs-lisp body params)))
  50. (result (eval (read (if (or (member "code" result-params)
  51. (member "pp" result-params))
  52. (concat "(pp " body ")")
  53. body))
  54. (if (listp lexical)
  55. lexical
  56. (member lexical '("yes" "t"))))))
  57. (org-babel-result-cond result-params
  58. (let ((print-level nil)
  59. (print-length nil))
  60. (if (or (member "scalar" result-params)
  61. (member "verbatim" result-params))
  62. (format "%S" result)
  63. (format "%s" result)))
  64. (org-babel-reassemble-table
  65. result
  66. (org-babel-pick-name (cdr (assq :colname-names params))
  67. (cdr (assq :colnames params)))
  68. (org-babel-pick-name (cdr (assq :rowname-names params))
  69. (cdr (assq :rownames params))))))))
  70. (org-babel-make-language-alias "elisp" "emacs-lisp")
  71. (provide 'ob-emacs-lisp)
  72. ;;; ob-emacs-lisp.el ends here