config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ob-emacs-lisp.el (4782B)


      1 ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2009-2024 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         (prologue (cdr (assq :prologue params)))
     58         (epilogue (cdr (assq :epilogue params))))
     59     (if (null vars) (concat body "\n")
     60       (format "(let (%s)\n%s%s%s\n)"
     61 	      (mapconcat
     62 	       (lambda (var)
     63 		 (format "%S" `(,(car var) ',(cdr var))))
     64 	       vars "\n      ")
     65               (if prologue (concat prologue "\n      ") "")
     66 	      body
     67               (if epilogue (concat "\n      " epilogue "\n") "")))))
     68 
     69 (defun org-babel-execute:emacs-lisp (body params)
     70   "Execute emacs-lisp code BODY according to PARAMS."
     71   (let* ((lexical (cdr (assq :lexical params)))
     72          (session (cdr (assq :session params)))
     73 	 (result-params (cdr (assq :result-params params)))
     74 	 (body (format (if (member "output" result-params)
     75 			   "(with-output-to-string %s\n)"
     76 			 "(progn %s\n)")
     77 		       (org-babel-expand-body:emacs-lisp body params)))
     78 	 (result (eval (read (if (or (member "code" result-params)
     79 				     (member "pp" result-params))
     80 				 (concat "(pp " body ")")
     81 			       body))
     82 		       (org-babel-emacs-lisp-lexical lexical))))
     83     (when (and session (not (equal session "none")))
     84       (error "ob-emacs-lisp backend does not support sessions"))
     85     (org-babel-result-cond result-params
     86       (let ((print-level nil)
     87             (print-length nil))
     88         (if (or (member "scalar" result-params)
     89                 (member "verbatim" result-params))
     90             (format "%S" result)
     91           (format "%s" result)))
     92       (org-babel-reassemble-table
     93        result
     94        (org-babel-pick-name (cdr (assq :colname-names params))
     95                             (cdr (assq :colnames params)))
     96        (org-babel-pick-name (cdr (assq :rowname-names params))
     97                             (cdr (assq :rownames params)))))))
     98 
     99 (defun org-babel-emacs-lisp-lexical (lexical)
    100   "Interpret :lexical source block argument.
    101 Convert LEXICAL into the form appropriate for `lexical-binding'
    102 and the LEXICAL argument to `eval'."
    103   (if (listp lexical)
    104       lexical
    105     (not (null (member lexical '("yes" "t"))))))
    106 
    107 (defun org-babel-edit-prep:emacs-lisp (info)
    108   "Set `lexical-binding' in Org edit buffer.
    109 Set `lexical-binding' in Org edit buffer according to the
    110 corresponding :lexical source block argument provide in the INFO
    111 channel, as returned by `org-babel-get-src-block-info'."
    112   (setq lexical-binding
    113         (org-babel-emacs-lisp-lexical
    114          (org-babel-read
    115           (cdr (assq :lexical (nth 2 info)))))))
    116 
    117 (defun org-babel-prep-session:emacs-lisp (_session _params)
    118   "Return an error because we do not support sessions."
    119   (error "ob-emacs-lisp backend does not support sessions"))
    120 
    121 (org-babel-make-language-alias "elisp" "emacs-lisp")
    122 
    123 (provide 'ob-emacs-lisp)
    124 
    125 ;;; ob-emacs-lisp.el ends here