ob-emacs-lisp.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; URL: 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 'org-macs)
  21. (org-assert-version)
  22. (require 'ob-core)
  23. (declare-function org-babel--get-vars "ob" (params))
  24. (declare-function org-babel-result-cond "ob" (result-params scalar-form &rest table-forms))
  25. (declare-function org-babel-reassemble-table "ob" (table colnames rownames))
  26. (declare-function org-babel-pick-name "ob" (names selector))
  27. (defconst org-babel-header-args:emacs-lisp '((lexical . :any))
  28. "Emacs-lisp specific header arguments.")
  29. (defvar org-babel-default-header-args:emacs-lisp '((:lexical . "no"))
  30. "Default arguments for evaluating an emacs-lisp source block.
  31. A value of \"yes\" or t causes source blocks to be eval'd using
  32. lexical scoping. It can also be an alist mapping symbols to
  33. their value. It is used both as the optional LEXICAL argument to
  34. `eval', and as the value for `lexical-binding' in buffers created
  35. by `org-edit-src-code'.")
  36. (defun org-babel-expand-body:emacs-lisp (body params)
  37. "Expand BODY according to PARAMS, return the expanded body."
  38. (let ((vars (org-babel--get-vars params))
  39. (print-level nil)
  40. (print-length nil))
  41. (if (null vars) (concat body "\n")
  42. (format "(let (%s)\n%s\n)"
  43. (mapconcat
  44. (lambda (var)
  45. (format "%S" (print `(,(car var) ',(cdr var)))))
  46. vars "\n ")
  47. body))))
  48. (defun org-babel-execute:emacs-lisp (body params)
  49. "Execute a block of emacs-lisp code with Babel."
  50. (let* ((lexical (cdr (assq :lexical params)))
  51. (result-params (cdr (assq :result-params params)))
  52. (body (format (if (member "output" result-params)
  53. "(with-output-to-string %s\n)"
  54. "(progn %s\n)")
  55. (org-babel-expand-body:emacs-lisp body params)))
  56. (result (eval (read (if (or (member "code" result-params)
  57. (member "pp" result-params))
  58. (concat "(pp " body ")")
  59. body))
  60. (org-babel-emacs-lisp-lexical lexical))))
  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. (defun org-babel-emacs-lisp-lexical (lexical)
  75. "Interpret :lexical source block argument.
  76. Convert LEXICAL into the form appropriate for `lexical-binding'
  77. and the LEXICAL argument to `eval'."
  78. (if (listp lexical)
  79. lexical
  80. (not (null (member lexical '("yes" "t"))))))
  81. (defun org-babel-edit-prep:emacs-lisp (info)
  82. "Set `lexical-binding' in Org edit buffer.
  83. Set `lexical-binding' in Org edit buffer according to the
  84. corresponding :lexical source block argument."
  85. (setq lexical-binding
  86. (org-babel-emacs-lisp-lexical
  87. (org-babel-read
  88. (cdr (assq :lexical (nth 2 info)))))))
  89. (org-babel-make-language-alias "elisp" "emacs-lisp")
  90. (provide 'ob-emacs-lisp)
  91. ;;; ob-emacs-lisp.el ends here