Merge branch 'maint'
[org-mode.git] / lisp / ob-emacs-lisp.el
blob3092122891fca36655f315dd4f47fb7544c0f729
1 ;;; ob-emacs-lisp.el --- Babel Functions for Emacs-lisp Code -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2016 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 <http://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 (result-params (cdr (assoc :result-params params)))
46 (print-level nil) (print-length nil)
47 (body (if (> (length vars) 0)
48 (concat "(let ("
49 (mapconcat
50 (lambda (var)
51 (format "%S" (print `(,(car var) ',(cdr var)))))
52 vars "\n ")
53 ")\n" body "\n)")
54 (concat body "\n"))))
55 (if (or (member "code" result-params)
56 (member "pp" result-params))
57 (concat "(pp " body ")") body)))
59 (defun org-babel-execute:emacs-lisp (body params)
60 "Execute a block of emacs-lisp code with Babel."
61 (save-window-excursion
62 (let* ((lexical (cdr (assq :lexical params)))
63 (result
64 (eval (read (format (if (member "output"
65 (cdr (assq :result-params params)))
66 "(with-output-to-string %s)"
67 "(progn %s)")
68 (org-babel-expand-body:emacs-lisp
69 body params)))
71 (if (listp lexical)
72 lexical
73 (member lexical '("yes" "t"))))))
74 (org-babel-result-cond (cdr (assoc :result-params params))
75 (let ((print-level nil)
76 (print-length nil))
77 (if (or (member "scalar" (cdr (assoc :result-params params)))
78 (member "verbatim" (cdr (assoc :result-params params))))
79 (format "%S" result)
80 (format "%s" result)))
81 (org-babel-reassemble-table
82 result
83 (org-babel-pick-name (cdr (assoc :colname-names params))
84 (cdr (assoc :colnames params)))
85 (org-babel-pick-name (cdr (assoc :rowname-names params))
86 (cdr (assoc :rownames params))))))))
88 (org-babel-make-language-alias "elisp" "emacs-lisp")
90 (provide 'ob-emacs-lisp)
94 ;;; ob-emacs-lisp.el ends here