Merge from emacs-24; up to 2012-04-22T13:58:00Z!cyd@gnu.org
[bpt/emacs.git] / lisp / org / ob-emacs-lisp.el
CommitLineData
86fbb8ca
CD
1;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation
2
c7e9ed79 3;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
86fbb8ca
CD
4
5;; Author: Eric Schulte
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
86fbb8ca
CD
8
9;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24;;; Commentary:
25
26;; Org-Babel support for evaluating emacs-lisp code
27
28;;; Code:
29(require 'ob)
eeb4145e 30(eval-when-compile (require 'ob-comint))
86fbb8ca
CD
31
32(defvar org-babel-default-header-args:emacs-lisp
33 '((:hlines . "yes") (:colnames . "no"))
34 "Default arguments for evaluating an emacs-lisp source block.")
35
86fbb8ca
CD
36(declare-function orgtbl-to-generic "org-table" (table params))
37
afe98dfa 38(defun org-babel-expand-body:emacs-lisp (body params)
86fbb8ca 39 "Expand BODY according to PARAMS, return the expanded body."
afe98dfa
CD
40 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
41 (result-params (cdr (assoc :result-params params)))
86fbb8ca
CD
42 (print-level nil) (print-length nil)
43 (body (if (> (length vars) 0)
153ae947
BG
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"))))
86fbb8ca
CD
51 (if (or (member "code" result-params)
52 (member "pp" result-params))
53 (concat "(pp " body ")") body)))
54
55(defun org-babel-execute:emacs-lisp (body params)
56 "Execute a block of emacs-lisp code with Babel."
57 (save-window-excursion
3ab2c837
BG
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)))))))
86fbb8ca
CD
75
76(provide 'ob-emacs-lisp)
77
5b409b39 78
86fbb8ca
CD
79
80;;; ob-emacs-lisp.el ends here