Merge branch 'origin-maint'
[org-mode.git] / lisp / ob-emacs-lisp.el
blobc8af606200281b84c11525bc305332d842f2eef9
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 (if (or (member "scalar" (cdr (assoc :result-params params)))
60 (member "verbatim" (cdr (assoc :result-params params))))
61 (let ((print-level nil)
62 (print-length nil))
63 (format "%S" result))
64 (org-babel-reassemble-table
65 result
66 (org-babel-pick-name (cdr (assoc :colname-names params))
67 (cdr (assoc :colnames params)))
68 (org-babel-pick-name (cdr (assoc :rowname-names params))
69 (cdr (assoc :rownames params))))))
70 (eval (read (format (if (member "output"
71 (cdr (assoc :result-params params)))
72 "(with-output-to-string %s)"
73 "(progn %s)")
74 (org-babel-expand-body:emacs-lisp body params)))))))
76 (provide 'ob-emacs-lisp)
80 ;;; ob-emacs-lisp.el ends here