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