passing as many tests as before
[org-mode.git] / lisp / ob-emacs-lisp.el
blob3ba14f667a30aef7ec6e8cc0285d84e67e4ccea7
1 ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
3 ;; Copyright (C) 2009-2012 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)
30 (eval-when-compile (require 'ob-comint))
32 (defvar org-babel-default-header-args:emacs-lisp
33 '((:hlines . "yes") (:colnames . "no"))
34 "Default arguments for evaluating an emacs-lisp source block.")
36 (declare-function orgtbl-to-generic "org-table" (table params))
38 (defun org-babel-expand-body:emacs-lisp (body params)
39 "Expand BODY according to PARAMS, return the expanded body."
40 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
41 (result-params (cdr (assoc :result-params params)))
42 (print-level nil) (print-length nil)
43 (body (if (> (length vars) 0)
44 (concat "(let ("
45 (mapconcat
46 (lambda (var)
47 (format "%S" (print `(,(car var) ',(cdr var)))))
48 vars "\n ")
49 ")\n" body "\n)")
50 (concat body "\n"))))
51 (if (or (member "code" result-params)
52 (member "pp" result-params))
53 (concat "(pp " body ")") 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 ((lambda (result)
59 (org-babel-result-cond (cdr (assoc :result-params params))
60 (let ((print-level nil)
61 (print-length nil))
62 (if (or (member "scalar" (cdr (assoc :result-params params)))
63 (member "verbatim" (cdr (assoc :result-params params))))
64 (format "%S" result)
65 (format "%s" result)))
66 (org-babel-reassemble-table
67 result
68 (org-babel-pick-name (cdr (assoc :colname-names params))
69 (cdr (assoc :colnames params)))
70 (org-babel-pick-name (cdr (assoc :rowname-names params))
71 (cdr (assoc :rownames params))))))
72 (eval (read (format (if (member "output"
73 (cdr (assoc :result-params params)))
74 "(with-output-to-string %s)"
75 "(progn %s)")
76 (org-babel-expand-body:emacs-lisp body params)))))))
78 (provide 'ob-emacs-lisp)
82 ;;; ob-emacs-lisp.el ends here