Merge branch 'master' of orgmode.org:org-mode
[org-mode.git] / lisp / ob-lisp.el
blob97e8a97b24b0dcb50a71266f18954e273ac79e42
1 ;;; ob-lisp.el --- org-babel functions for common lisp evaluation
3 ;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;; Author: Joel Boehland, Eric Schulte, David T. O'Toole <dto@gnu.org>
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.7
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;;; Commentary:
27 ;;; support for evaluating common lisp code, relies on slime for all eval
29 ;;; Requirements:
31 ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
32 ;; See http://common-lisp.net/project/slime/
34 ;;; Code:
35 (require 'ob)
37 (declare-function slime-eval "ext:slime" (sexp &optional package))
39 (defvar org-babel-tangle-lang-exts)
40 (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
42 (defvar org-babel-default-header-args:lisp '())
43 (defvar org-babel-header-arg-names:lisp '(package))
45 (defcustom org-babel-lisp-dir-fmt
46 "(let ((*default-pathname-defaults* #P%S)) %%s)"
47 "Format string used to wrap code bodies to set the current directory.
48 For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
49 current directory string."
50 :group 'org-babel
51 :type 'string)
53 (defun org-babel-expand-body:lisp (body params)
54 "Expand BODY according to PARAMS, return the expanded body."
55 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
56 (result-params (cdr (assoc :result-params params)))
57 (print-level nil) (print-length nil)
58 (body (org-babel-trim
59 (if (> (length vars) 0)
60 (concat "(let ("
61 (mapconcat
62 (lambda (var)
63 (format "(%S (quote %S))" (car var) (cdr var)))
64 vars "\n ")
65 ")\n" body ")")
66 body))))
67 (if (or (member "code" result-params)
68 (member "pp" result-params))
69 (format "(pprint %s)" body)
70 body)))
72 (defun org-babel-execute:lisp (body params)
73 "Execute a block of Common Lisp code with Babel."
74 (require 'slime)
75 (org-babel-reassemble-table
76 ((lambda (result)
77 (if (member "output" (cdr (assoc :result-params params)))
78 (car result)
79 (condition-case nil
80 (read (org-bable-lisp-vector-to-list (cadr result)))
81 (error (cadr result)))))
82 (with-temp-buffer
83 (insert (org-babel-expand-body:lisp body params))
84 (slime-eval `(swank:eval-and-grab-output
85 ,(let ((dir (if (assoc :dir params)
86 (cdr (assoc :dir params))
87 default-directory)))
88 (format
89 (if dir (format org-babel-lisp-dir-fmt dir) "(progn %s)")
90 (buffer-substring-no-properties
91 (point-min) (point-max)))))
92 (cdr (assoc :package params)))))
93 (org-babel-pick-name (cdr (assoc :colname-names params))
94 (cdr (assoc :colnames params)))
95 (org-babel-pick-name (cdr (assoc :rowname-names params))
96 (cdr (assoc :rownames params)))))
98 (defun org-bable-lisp-vector-to-list (results)
99 ;; TODO: better would be to replace #(...) with [...]
100 (replace-regexp-in-string "#(" "(" results))
102 (provide 'ob-lisp)
104 ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
106 ;;; ob-lisp.el ends here