Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-emacs-lisp.el --- org-babel functions for emacs-lisp code evaluation |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2009-2014 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) | |
30 | ||
31 | (defvar org-babel-default-header-args:emacs-lisp | |
32 | '((:hlines . "yes") (:colnames . "no")) | |
33 | "Default arguments for evaluating an emacs-lisp source block.") | |
34 | ||
86fbb8ca CD |
35 | (declare-function orgtbl-to-generic "org-table" (table params)) |
36 | ||
afe98dfa | 37 | (defun org-babel-expand-body:emacs-lisp (body params) |
86fbb8ca | 38 | "Expand BODY according to PARAMS, return the expanded body." |
afe98dfa CD |
39 | (let* ((vars (mapcar #'cdr (org-babel-get-header params :var))) |
40 | (result-params (cdr (assoc :result-params params))) | |
86fbb8ca CD |
41 | (print-level nil) (print-length nil) |
42 | (body (if (> (length vars) 0) | |
8223b1d2 BG |
43 | (concat "(let (" |
44 | (mapconcat | |
45 | (lambda (var) | |
46 | (format "%S" (print `(,(car var) ',(cdr var))))) | |
47 | vars "\n ") | |
48 | ")\n" body "\n)") | |
153ae947 | 49 | (concat body "\n")))) |
86fbb8ca CD |
50 | (if (or (member "code" result-params) |
51 | (member "pp" result-params)) | |
52 | (concat "(pp " body ")") body))) | |
53 | ||
54 | (defun org-babel-execute:emacs-lisp (body params) | |
55 | "Execute a block of emacs-lisp code with Babel." | |
56 | (save-window-excursion | |
666ffc7e SM |
57 | (let ((result |
58 | (eval (read (format (if (member "output" | |
59 | (cdr (assoc :result-params params))) | |
60 | "(with-output-to-string %s)" | |
61 | "(progn %s)") | |
62 | (org-babel-expand-body:emacs-lisp | |
63 | body params)))))) | |
64 | (org-babel-result-cond (cdr (assoc :result-params params)) | |
65 | (let ((print-level nil) | |
66 | (print-length nil)) | |
67 | (if (or (member "scalar" (cdr (assoc :result-params params))) | |
68 | (member "verbatim" (cdr (assoc :result-params params)))) | |
69 | (format "%S" result) | |
70 | (format "%s" result))) | |
71 | (org-babel-reassemble-table | |
72 | result | |
73 | (org-babel-pick-name (cdr (assoc :colname-names params)) | |
74 | (cdr (assoc :colnames params))) | |
75 | (org-babel-pick-name (cdr (assoc :rowname-names params)) | |
76 | (cdr (assoc :rownames params)))))))) | |
86fbb8ca CD |
77 | |
78 | (provide 'ob-emacs-lisp) | |
79 | ||
5b409b39 | 80 | |
86fbb8ca CD |
81 | |
82 | ;;; ob-emacs-lisp.el ends here |