ob-emacs-lisp.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 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 both as the optional LEXICAL argument to
  32. `eval', and as the value for `lexical-binding' in buffers created
  33. by `org-edit-src-code'.")
  34. (defun org-babel-expand-body:emacs-lisp (body params)
  35. "Expand BODY according to PARAMS, return the expanded body."
  36. (let ((vars (org-babel--get-vars params))
  37. (print-level nil)
  38. (print-length nil))
  39. (if (null vars) (concat body "\n")
  40. (format "(let (%s)\n%s\n)"
  41. (mapconcat
  42. (lambda (var)
  43. (format "%S" (print `(,(car var) ',(cdr var)))))
  44. vars "\n ")
  45. body))))
  46. (defun org-babel-execute:emacs-lisp (body params)
  47. "Execute a block of emacs-lisp code with Babel."
  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. (org-babel-emacs-lisp-lexical lexical))))
  59. (org-babel-result-cond result-params
  60. (let ((print-level nil)
  61. (print-length nil))
  62. (if (or (member "scalar" result-params)
  63. (member "verbatim" result-params))
  64. (format "%S" result)
  65. (format "%s" result)))
  66. (org-babel-reassemble-table
  67. result
  68. (org-babel-pick-name (cdr (assq :colname-names params))
  69. (cdr (assq :colnames params)))
  70. (org-babel-pick-name (cdr (assq :rowname-names params))
  71. (cdr (assq :rownames params)))))))
  72. (defun org-babel-emacs-lisp-lexical (lexical)
  73. "Interpret :lexical source block argument.
  74. Convert LEXICAL into the form appropriate for `lexical-binding'
  75. and the LEXICAL argument to `eval'."
  76. (if (listp lexical)
  77. lexical
  78. (not (null (member lexical '("yes" "t"))))))
  79. (defun org-babel-edit-prep:emacs-lisp (info)
  80. "Set `lexical-binding' in Org edit buffer.
  81. Set `lexical-binding' in Org edit buffer according to the
  82. corresponding :lexical source block argument."
  83. (setq lexical-binding
  84. (org-babel-emacs-lisp-lexical
  85. (org-babel-read
  86. (cdr (assq :lexical (nth 2 info)))))))
  87. (org-babel-make-language-alias "elisp" "emacs-lisp")
  88. (provide 'ob-emacs-lisp)
  89. ;;; ob-emacs-lisp.el ends here