Commit | Line | Data |
---|---|---|
3ab2c837 | 1 | ;;; ob-lisp.el --- org-babel functions for common lisp evaluation |
afe98dfa | 2 | |
b73f1974 | 3 | ;; Copyright (C) 2009-2012 Free Software Foundation, Inc. |
afe98dfa | 4 | |
c7557a0f GM |
5 | ;; Author: Joel Boehland |
6 | ;; Eric Schulte | |
7 | ;; David T. O'Toole <dto@gnu.org> | |
3ab2c837 | 8 | ;; Keywords: literate programming, reproducible research |
afe98dfa | 9 | ;; Homepage: http://orgmode.org |
afe98dfa | 10 | |
3ab2c837 | 11 | ;; This file is part of GNU Emacs. |
afe98dfa | 12 | |
3ab2c837 | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
afe98dfa | 14 | ;; it under the terms of the GNU General Public License as published by |
3ab2c837 BG |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
17 | ||
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
afe98dfa CD |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
3ab2c837 | 22 | |
afe98dfa | 23 | ;; You should have received a copy of the GNU General Public License |
3ab2c837 | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
afe98dfa CD |
25 | |
26 | ;;; Commentary: | |
27 | ||
3ab2c837 | 28 | ;;; support for evaluating common lisp code, relies on slime for all eval |
afe98dfa CD |
29 | |
30 | ;;; Requirements: | |
31 | ||
32 | ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.) | |
33 | ;; See http://common-lisp.net/project/slime/ | |
34 | ||
35 | ;;; Code: | |
36 | (require 'ob) | |
acedf35c CD |
37 | |
38 | (declare-function slime-eval "ext:slime" (sexp &optional package)) | |
afe98dfa | 39 | |
3ab2c837 BG |
40 | (defvar org-babel-tangle-lang-exts) |
41 | (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp")) | |
afe98dfa | 42 | |
3ab2c837 BG |
43 | (defvar org-babel-default-header-args:lisp '()) |
44 | (defvar org-babel-header-arg-names:lisp '(package)) | |
45 | ||
46 | (defcustom org-babel-lisp-dir-fmt | |
47 | "(let ((*default-pathname-defaults* #P%S)) %%s)" | |
48 | "Format string used to wrap code bodies to set the current directory. | |
49 | For example a value of \"(progn ;; %s\\n %%s)\" would ignore the | |
50 | current directory string." | |
acedf35c CD |
51 | :group 'org-babel |
52 | :type 'string) | |
afe98dfa CD |
53 | |
54 | (defun org-babel-expand-body:lisp (body params) | |
55 | "Expand BODY according to PARAMS, return the expanded body." | |
3ab2c837 BG |
56 | (let* ((vars (mapcar #'cdr (org-babel-get-header params :var))) |
57 | (result-params (cdr (assoc :result-params params))) | |
58 | (print-level nil) (print-length nil) | |
59 | (body (org-babel-trim | |
60 | (if (> (length vars) 0) | |
61 | (concat "(let (" | |
62 | (mapconcat | |
63 | (lambda (var) | |
64 | (format "(%S (quote %S))" (car var) (cdr var))) | |
65 | vars "\n ") | |
66 | ")\n" body ")") | |
67 | body)))) | |
68 | (if (or (member "code" result-params) | |
69 | (member "pp" result-params)) | |
70 | (format "(pprint %s)" body) | |
afe98dfa CD |
71 | body))) |
72 | ||
73 | (defun org-babel-execute:lisp (body params) | |
3ab2c837 | 74 | "Execute a block of Common Lisp code with Babel." |
acedf35c | 75 | (require 'slime) |
3ab2c837 BG |
76 | (org-babel-reassemble-table |
77 | ((lambda (result) | |
78 | (if (member "output" (cdr (assoc :result-params params))) | |
79 | (car result) | |
80 | (condition-case nil | |
e66ba1df | 81 | (read (org-babel-lisp-vector-to-list (cadr result))) |
3ab2c837 BG |
82 | (error (cadr result))))) |
83 | (with-temp-buffer | |
84 | (insert (org-babel-expand-body:lisp body params)) | |
85 | (slime-eval `(swank:eval-and-grab-output | |
86 | ,(let ((dir (if (assoc :dir params) | |
87 | (cdr (assoc :dir params)) | |
88 | default-directory))) | |
89 | (format | |
90 | (if dir (format org-babel-lisp-dir-fmt dir) "(progn %s)") | |
91 | (buffer-substring-no-properties | |
92 | (point-min) (point-max))))) | |
93 | (cdr (assoc :package params))))) | |
94 | (org-babel-pick-name (cdr (assoc :colname-names params)) | |
95 | (cdr (assoc :colnames params))) | |
96 | (org-babel-pick-name (cdr (assoc :rowname-names params)) | |
97 | (cdr (assoc :rownames params))))) | |
98 | ||
e66ba1df | 99 | (defun org-babel-lisp-vector-to-list (results) |
3ab2c837 BG |
100 | ;; TODO: better would be to replace #(...) with [...] |
101 | (replace-regexp-in-string "#(" "(" results)) | |
afe98dfa CD |
102 | |
103 | (provide 'ob-lisp) | |
104 | ||
5b409b39 | 105 | |
afe98dfa CD |
106 | |
107 | ;;; ob-lisp.el ends here |