lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ob-emacs-lisp.el
blob18b0d4841e8cc0604b416949b6e0fd1b29a82546
1 ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2019 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 both as the optional LEXICAL argument to
46 `eval', and as the value for `lexical-binding' in buffers created
47 by `org-edit-src-code'.")
49 (defun org-babel-expand-body:emacs-lisp (body params)
50 "Expand BODY according to PARAMS, return the expanded body."
51 (let ((vars (org-babel--get-vars params))
52 (print-level nil)
53 (print-length nil))
54 (if (null vars) (concat body "\n")
55 (format "(let (%s)\n%s\n)"
56 (mapconcat
57 (lambda (var)
58 (format "%S" (print `(,(car var) ',(cdr var)))))
59 vars "\n ")
60 body))))
62 (defun org-babel-execute:emacs-lisp (body params)
63 "Execute a block of emacs-lisp code with Babel."
64 (save-window-excursion
65 (let* ((lexical (cdr (assq :lexical params)))
66 (result-params (cdr (assq :result-params params)))
67 (body (format (if (member "output" result-params)
68 "(with-output-to-string %s\n)"
69 "(progn %s\n)")
70 (org-babel-expand-body:emacs-lisp body params)))
71 (result (eval (read (if (or (member "code" result-params)
72 (member "pp" result-params))
73 (concat "(pp " body ")")
74 body))
75 (org-babel-emacs-lisp-lexical lexical))))
76 (org-babel-result-cond result-params
77 (let ((print-level nil)
78 (print-length nil))
79 (if (or (member "scalar" result-params)
80 (member "verbatim" result-params))
81 (format "%S" result)
82 (format "%s" result)))
83 (org-babel-reassemble-table
84 result
85 (org-babel-pick-name (cdr (assq :colname-names params))
86 (cdr (assq :colnames params)))
87 (org-babel-pick-name (cdr (assq :rowname-names params))
88 (cdr (assq :rownames params))))))))
90 (defun org-babel-emacs-lisp-lexical (lexical)
91 "Interpret :lexical source block argument.
92 Convert LEXICAL into the form appropriate for `lexical-binding'
93 and the LEXICAL argument to `eval'."
94 (if (listp lexical)
95 lexical
96 (not (null (member lexical '("yes" "t"))))))
98 (defun org-babel-edit-prep:emacs-lisp (info)
99 "Set `lexical-binding' in Org edit buffer.
100 Set `lexical-binding' in Org edit buffer according to the
101 corresponding :lexical source block argument."
102 (setq lexical-binding
103 (org-babel-emacs-lisp-lexical
104 (org-babel-read
105 (cdr (assq :lexical (nth 2 info)))))))
107 (org-babel-make-language-alias "elisp" "emacs-lisp")
109 (provide 'ob-emacs-lisp)
113 ;;; ob-emacs-lisp.el ends here