ob-emacs-lisp.el (4133B)
1 ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2009-2023 Free Software Foundation, Inc. 4 5 ;; Author: Eric Schulte 6 ;; Keywords: literate programming, reproducible research 7 ;; URL: https://orgmode.org 8 9 ;; This file is part of GNU Emacs. 10 11 ;; GNU Emacs is free software: you can redistribute it and/or modify 12 ;; it under the terms of the GNU General Public License as published by 13 ;; the Free Software Foundation, either version 3 of the License, or 14 ;; (at your option) any later version. 15 16 ;; GNU Emacs is distributed in the hope that it will be useful, 17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 ;; GNU General Public License for more details. 20 21 ;; You should have received a copy of the GNU General Public License 22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. 23 24 ;;; Commentary: 25 26 ;; Org-Babel support for evaluating emacs-lisp code 27 28 ;;; Code: 29 30 (require 'org-macs) 31 (org-assert-version) 32 33 (require 'ob-core) 34 35 (declare-function org-babel--get-vars "ob" (params)) 36 (declare-function org-babel-result-cond "ob" (result-params scalar-form &rest table-forms)) 37 (declare-function org-babel-reassemble-table "ob" (table colnames rownames)) 38 (declare-function org-babel-pick-name "ob" (names selector)) 39 40 (defconst org-babel-header-args:emacs-lisp '((lexical . :any)) 41 "Emacs-lisp specific header arguments.") 42 43 (defvar org-babel-default-header-args:emacs-lisp '((:lexical . "no")) 44 "Default arguments for evaluating an emacs-lisp source block. 45 46 A value of \"yes\" or t causes source blocks to be eval'd using 47 lexical scoping. It can also be an alist mapping symbols to 48 their value. It is used both as the optional LEXICAL argument to 49 `eval', and as the value for `lexical-binding' in buffers created 50 by `org-edit-src-code'.") 51 52 (defun org-babel-expand-body:emacs-lisp (body params) 53 "Expand BODY according to PARAMS, return the expanded body." 54 (let ((vars (org-babel--get-vars params)) 55 (print-level nil) 56 (print-length nil)) 57 (if (null vars) (concat body "\n") 58 (format "(let (%s)\n%s\n)" 59 (mapconcat 60 (lambda (var) 61 (format "%S" `(,(car var) ',(cdr var)))) 62 vars "\n ") 63 body)))) 64 65 (defun org-babel-execute:emacs-lisp (body params) 66 "Execute a block of emacs-lisp code with Babel." 67 (let* ((lexical (cdr (assq :lexical params))) 68 (result-params (cdr (assq :result-params params))) 69 (body (format (if (member "output" result-params) 70 "(with-output-to-string %s\n)" 71 "(progn %s\n)") 72 (org-babel-expand-body:emacs-lisp body params))) 73 (result (eval (read (if (or (member "code" result-params) 74 (member "pp" result-params)) 75 (concat "(pp " body ")") 76 body)) 77 (org-babel-emacs-lisp-lexical lexical)))) 78 (org-babel-result-cond result-params 79 (let ((print-level nil) 80 (print-length nil)) 81 (if (or (member "scalar" result-params) 82 (member "verbatim" result-params)) 83 (format "%S" result) 84 (format "%s" result))) 85 (org-babel-reassemble-table 86 result 87 (org-babel-pick-name (cdr (assq :colname-names params)) 88 (cdr (assq :colnames params))) 89 (org-babel-pick-name (cdr (assq :rowname-names params)) 90 (cdr (assq :rownames params))))))) 91 92 (defun org-babel-emacs-lisp-lexical (lexical) 93 "Interpret :lexical source block argument. 94 Convert LEXICAL into the form appropriate for `lexical-binding' 95 and the LEXICAL argument to `eval'." 96 (if (listp lexical) 97 lexical 98 (not (null (member lexical '("yes" "t")))))) 99 100 (defun org-babel-edit-prep:emacs-lisp (info) 101 "Set `lexical-binding' in Org edit buffer. 102 Set `lexical-binding' in Org edit buffer according to the 103 corresponding :lexical source block argument." 104 (setq lexical-binding 105 (org-babel-emacs-lisp-lexical 106 (org-babel-read 107 (cdr (assq :lexical (nth 2 info))))))) 108 109 (org-babel-make-language-alias "elisp" "emacs-lisp") 110 111 (provide 'ob-emacs-lisp) 112 113 ;;; ob-emacs-lisp.el ends here