Fix <RET> on table.el tables
[org-mode/org-tableheadings.git] / lisp / ob-emacs-lisp.el
blob7446af252335fbf1d9234bafc89796e789cbdff1
1 ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://orgmode.org
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;; Org-Babel support for evaluating emacs-lisp code
28 ;;; Code:
30 (require 'ob-core)
32 (declare-function org-babel--get-vars "ob" (params))
33 (declare-function org-babel-result-cond "ob" (result-params scalar-form &rest table-forms))
34 (declare-function org-babel-reassemble-table "ob" (table colnames rownames))
35 (declare-function org-babel-pick-name "ob" (names selector))
37 (defconst org-babel-header-args:emacs-lisp '((lexical . :any))
38 "Emacs-lisp specific header arguments.")
40 (defvar org-babel-default-header-args:emacs-lisp '((:lexical . "no"))
41 "Default arguments for evaluating an emacs-lisp source block.
43 A value of \"yes\" or t causes source blocks to be eval'd using
44 lexical scoping. It can also be an alist mapping symbols to
45 their value. It is used as the optional LEXICAL argument to
46 `eval', which see.")
48 (defun org-babel-expand-body:emacs-lisp (body params)
49 "Expand BODY according to PARAMS, return the expanded body."
50 (let ((vars (org-babel--get-vars params))
51 (print-level nil)
52 (print-length nil))
53 (if (null vars) (concat body "\n")
54 (format "(let (%s)\n%s\n)"
55 (mapconcat
56 (lambda (var)
57 (format "%S" (print `(,(car var) ',(cdr var)))))
58 vars "\n ")
59 body))))
61 (defun org-babel-execute:emacs-lisp (body params)
62 "Execute a block of emacs-lisp code with Babel."
63 (save-window-excursion
64 (let* ((lexical (cdr (assq :lexical params)))
65 (result-params (cdr (assq :result-params params)))
66 (body (format (if (member "output" result-params)
67 "(with-output-to-string %s\n)"
68 "(progn %s\n)")
69 (org-babel-expand-body:emacs-lisp body params)))
70 (result (eval (read (if (or (member "code" result-params)
71 (member "pp" result-params))
72 (concat "(pp " body ")")
73 body))
74 (if (listp lexical)
75 lexical
76 (member lexical '("yes" "t"))))))
77 (org-babel-result-cond result-params
78 (let ((print-level nil)
79 (print-length nil))
80 (if (or (member "scalar" result-params)
81 (member "verbatim" result-params))
82 (format "%S" result)
83 (format "%s" result)))
84 (org-babel-reassemble-table
85 result
86 (org-babel-pick-name (cdr (assq :colname-names params))
87 (cdr (assq :colnames params)))
88 (org-babel-pick-name (cdr (assq :rowname-names params))
89 (cdr (assq :rownames params))))))))
91 (org-babel-make-language-alias "elisp" "emacs-lisp")
93 (provide 'ob-emacs-lisp)
97 ;;; ob-emacs-lisp.el ends here