Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation |
2 | ||
3ab2c837 | 3 | ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
3ab2c837 | 8 | ;; Version: 7.7 |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
12 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation, either version 3 of the License, or | |
15 | ;; (at your option) any later version. | |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Org-Babel support for evaluating emacs-lisp code | |
28 | ||
29 | ;;; Code: | |
30 | (require 'ob) | |
eeb4145e | 31 | (eval-when-compile (require 'ob-comint)) |
86fbb8ca CD |
32 | |
33 | (defvar org-babel-default-header-args:emacs-lisp | |
34 | '((:hlines . "yes") (:colnames . "no")) | |
35 | "Default arguments for evaluating an emacs-lisp source block.") | |
36 | ||
86fbb8ca CD |
37 | (declare-function orgtbl-to-generic "org-table" (table params)) |
38 | ||
afe98dfa | 39 | (defun org-babel-expand-body:emacs-lisp (body params) |
86fbb8ca | 40 | "Expand BODY according to PARAMS, return the expanded body." |
afe98dfa CD |
41 | (let* ((vars (mapcar #'cdr (org-babel-get-header params :var))) |
42 | (result-params (cdr (assoc :result-params params))) | |
86fbb8ca CD |
43 | (print-level nil) (print-length nil) |
44 | (body (if (> (length vars) 0) | |
45 | (concat "(let (" | |
46 | (mapconcat | |
afe98dfa CD |
47 | (lambda (var) |
48 | (format "%S" (print `(,(car var) ',(cdr var))))) | |
86fbb8ca CD |
49 | vars "\n ") |
50 | ")\n" body ")") | |
51 | body))) | |
52 | (if (or (member "code" result-params) | |
53 | (member "pp" result-params)) | |
54 | (concat "(pp " body ")") body))) | |
55 | ||
56 | (defun org-babel-execute:emacs-lisp (body params) | |
57 | "Execute a block of emacs-lisp code with Babel." | |
58 | (save-window-excursion | |
3ab2c837 BG |
59 | ((lambda (result) |
60 | (if (or (member "scalar" (cdr (assoc :result-params params))) | |
61 | (member "verbatim" (cdr (assoc :result-params params)))) | |
62 | (let ((print-level nil) | |
63 | (print-length nil)) | |
64 | (format "%S" result)) | |
65 | (org-babel-reassemble-table | |
66 | result | |
67 | (org-babel-pick-name (cdr (assoc :colname-names params)) | |
68 | (cdr (assoc :colnames params))) | |
69 | (org-babel-pick-name (cdr (assoc :rowname-names params)) | |
70 | (cdr (assoc :rownames params)))))) | |
71 | (eval (read (format (if (member "output" | |
72 | (cdr (assoc :result-params params))) | |
73 | "(with-output-to-string %s)" | |
74 | "(progn %s)") | |
75 | (org-babel-expand-body:emacs-lisp body params))))))) | |
86fbb8ca CD |
76 | |
77 | (provide 'ob-emacs-lisp) | |
78 | ||
5b409b39 | 79 | |
86fbb8ca CD |
80 | |
81 | ;;; ob-emacs-lisp.el ends here |