ox-latex: Fix numbering of source blocks
[org-mode/org-tableheadings.git] / lisp / ob-emacs-lisp.el
blobeaa11246f8569b7c3496f45ff17d7add1a18aa7c
1 ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
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 (defvar org-babel-default-header-args:emacs-lisp nil
32 "Default arguments for evaluating an emacs-lisp source block.")
34 (defun org-babel-expand-body:emacs-lisp (body params)
35 "Expand BODY according to PARAMS, return the expanded body."
36 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
37 (result-params (cdr (assoc :result-params params)))
38 (print-level nil) (print-length nil)
39 (body (if (> (length vars) 0)
40 (concat "(let ("
41 (mapconcat
42 (lambda (var)
43 (format "%S" (print `(,(car var) ',(cdr var)))))
44 vars "\n ")
45 ")\n" body "\n)")
46 (concat body "\n"))))
47 (if (or (member "code" result-params)
48 (member "pp" result-params))
49 (concat "(pp " body ")") body)))
51 (defun org-babel-execute:emacs-lisp (body params)
52 "Execute a block of emacs-lisp code with Babel."
53 (save-window-excursion
54 (let ((result
55 (eval (read (format (if (member "output"
56 (cdr (assoc :result-params params)))
57 "(with-output-to-string %s)"
58 "(progn %s)")
59 (org-babel-expand-body:emacs-lisp
60 body params))))))
61 (org-babel-result-cond (cdr (assoc :result-params params))
62 (let ((print-level nil)
63 (print-length nil))
64 (if (or (member "scalar" (cdr (assoc :result-params params)))
65 (member "verbatim" (cdr (assoc :result-params params))))
66 (format "%S" result)
67 (format "%s" result)))
68 (org-babel-reassemble-table
69 result
70 (org-babel-pick-name (cdr (assoc :colname-names params))
71 (cdr (assoc :colnames params)))
72 (org-babel-pick-name (cdr (assoc :rowname-names params))
73 (cdr (assoc :rownames params))))))))
75 (provide 'ob-emacs-lisp)
79 ;;; ob-emacs-lisp.el ends here