ob-emacs-lisp.el 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2019 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-core)
  21. (declare-function org-babel--get-vars "ob" (params))
  22. (declare-function org-babel-result-cond "ob" (result-params scalar-form &rest table-forms))
  23. (declare-function org-babel-reassemble-table "ob" (table colnames rownames))
  24. (declare-function org-babel-pick-name "ob" (names selector))
  25. (defconst org-babel-header-args:emacs-lisp '((lexical . :any))
  26. "Emacs-lisp specific header arguments.")
  27. (defvar org-babel-default-header-args:emacs-lisp '((:lexical . "no"))
  28. "Default arguments for evaluating an emacs-lisp source block.
  29. A value of \"yes\" or t causes source blocks to be eval'd using
  30. lexical scoping. It can also be an alist mapping symbols to
  31. their value. It is used as the optional LEXICAL argument to
  32. `eval', which see.")
  33. (defun org-babel-expand-body:emacs-lisp (body params)
  34. "Expand BODY according to PARAMS, return the expanded body."
  35. (let ((vars (org-babel--get-vars params))
  36. (print-level nil)
  37. (print-length nil))
  38. (if (null vars) (concat body "\n")
  39. (format "(let (%s)\n%s\n)"
  40. (mapconcat
  41. (lambda (var)
  42. (format "%S" (print `(,(car var) ',(cdr var)))))
  43. vars "\n ")
  44. body))))
  45. (defun org-babel-execute:emacs-lisp (body params)
  46. "Execute a block of emacs-lisp code with Babel."
  47. (save-window-excursion
  48. (let* ((lexical (cdr (assq :lexical params)))
  49. (result-params (cdr (assq :result-params params)))
  50. (body (format (if (member "output" result-params)
  51. "(with-output-to-string %s\n)"
  52. "(progn %s\n)")
  53. (org-babel-expand-body:emacs-lisp body params)))
  54. (result (eval (read (if (or (member "code" result-params)
  55. (member "pp" result-params))
  56. (concat "(pp " body ")")
  57. body))
  58. (if (listp lexical)
  59. lexical
  60. (member lexical '("yes" "t"))))))
  61. (org-babel-result-cond result-params
  62. (let ((print-level nil)
  63. (print-length nil))
  64. (if (or (member "scalar" result-params)
  65. (member "verbatim" result-params))
  66. (format "%S" result)
  67. (format "%s" result)))
  68. (org-babel-reassemble-table
  69. result
  70. (org-babel-pick-name (cdr (assq :colname-names params))
  71. (cdr (assq :colnames params)))
  72. (org-babel-pick-name (cdr (assq :rowname-names params))
  73. (cdr (assq :rownames params))))))))
  74. (org-babel-make-language-alias "elisp" "emacs-lisp")
  75. (provide 'ob-emacs-lisp)
  76. ;;; ob-emacs-lisp.el ends here