org.el: tiny docstring fix.
[org-mode.git] / lisp / ob-lisp.el
blob8525d82c04220c61c5ce9432ae0faf367df1a503
1 ;;; ob-lisp.el --- org-babel functions for common lisp evaluation
3 ;; Copyright (C) 2009-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
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;;; support for evaluating common lisp code, relies on slime for all eval
28 ;;; Requirements:
30 ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
31 ;; See http://common-lisp.net/project/slime/
33 ;;; Code:
34 (require 'ob)
36 (declare-function slime-eval "ext:slime" (sexp &optional package))
38 (defvar org-babel-tangle-lang-exts)
39 (add-to-list 'org-babel-tangle-lang-exts '("lisp" . "lisp"))
41 (defvar org-babel-default-header-args:lisp '())
42 (defvar org-babel-header-arg-names:lisp '(package))
44 (defcustom org-babel-lisp-dir-fmt
45 "(let ((*default-pathname-defaults* #P%S)) %%s)"
46 "Format string used to wrap code bodies to set the current directory.
47 For example a value of \"(progn ;; %s\\n %%s)\" would ignore the
48 current directory string."
49 :group 'org-babel
50 :type 'string)
52 (defun org-babel-expand-body:lisp (body params)
53 "Expand BODY according to PARAMS, return the expanded body."
54 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
55 (result-params (cdr (assoc :result-params params)))
56 (print-level nil) (print-length nil)
57 (body (org-babel-trim
58 (if (> (length vars) 0)
59 (concat "(let ("
60 (mapconcat
61 (lambda (var)
62 (format "(%S (quote %S))" (car var) (cdr var)))
63 vars "\n ")
64 ")\n" body ")")
65 body))))
66 (if (or (member "code" result-params)
67 (member "pp" result-params))
68 (format "(pprint %s)" body)
69 body)))
71 (defun org-babel-execute:lisp (body params)
72 "Execute a block of Common Lisp code with Babel."
73 (require 'slime)
74 (org-babel-reassemble-table
75 ((lambda (result)
76 (if (member "output" (cdr (assoc :result-params params)))
77 (car result)
78 (condition-case nil
79 (read (org-bable-lisp-vector-to-list (cadr result)))
80 (error (cadr result)))))
81 (with-temp-buffer
82 (insert (org-babel-expand-body:lisp body params))
83 (slime-eval `(swank:eval-and-grab-output
84 ,(let ((dir (if (assoc :dir params)
85 (cdr (assoc :dir params))
86 default-directory)))
87 (format
88 (if dir (format org-babel-lisp-dir-fmt dir) "(progn %s)")
89 (buffer-substring-no-properties
90 (point-min) (point-max)))))
91 (cdr (assoc :package params)))))
92 (org-babel-pick-name (cdr (assoc :colname-names params))
93 (cdr (assoc :colnames params)))
94 (org-babel-pick-name (cdr (assoc :rowname-names params))
95 (cdr (assoc :rownames params)))))
97 (defun org-bable-lisp-vector-to-list (results)
98 ;; TODO: better would be to replace #(...) with [...]
99 (replace-regexp-in-string "#(" "(" results))
101 (provide 'ob-lisp)
105 ;;; ob-lisp.el ends here