ob-emacs-lisp.el 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 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. (save-window-excursion
  49. (let* ((lexical (cdr (assq :lexical params)))
  50. (result-params (cdr (assq :result-params params)))
  51. (body (format (if (member "output" result-params)
  52. "(with-output-to-string %s\n)"
  53. "(progn %s\n)")
  54. (org-babel-expand-body:emacs-lisp body params)))
  55. (result (eval (read (if (or (member "code" result-params)
  56. (member "pp" result-params))
  57. (concat "(pp " body ")")
  58. body))
  59. (org-babel-emacs-lisp-lexical lexical))))
  60. (org-babel-result-cond result-params
  61. (let ((print-level nil)
  62. (print-length nil))
  63. (if (or (member "scalar" result-params)
  64. (member "verbatim" result-params))
  65. (format "%S" result)
  66. (format "%s" result)))
  67. (org-babel-reassemble-table
  68. result
  69. (org-babel-pick-name (cdr (assq :colname-names params))
  70. (cdr (assq :colnames params)))
  71. (org-babel-pick-name (cdr (assq :rowname-names params))
  72. (cdr (assq :rownames params))))))))
  73. (defun org-babel-emacs-lisp-lexical (lexical)
  74. "Interpret :lexical source block argument.
  75. Convert LEXICAL into the form appropriate for `lexical-binding'
  76. and the LEXICAL argument to `eval'."
  77. (if (listp lexical)
  78. lexical
  79. (not (null (member lexical '("yes" "t"))))))
  80. (defun org-babel-edit-prep:emacs-lisp (info)
  81. "Set `lexical-binding' in Org edit buffer.
  82. Set `lexical-binding' in Org edit buffer according to the
  83. corresponding :lexical source block argument."
  84. (setq lexical-binding
  85. (org-babel-emacs-lisp-lexical
  86. (org-babel-read
  87. (cdr (assq :lexical (nth 2 info)))))))
  88. (org-babel-make-language-alias "elisp" "emacs-lisp")
  89. (provide 'ob-emacs-lisp)
  90. ;;; ob-emacs-lisp.el ends here