Merge from emacs-23; up to 2010-06-15T03:34:12Z!rgm@gnu.org.
[bpt/emacs.git] / lisp / org / ob-lisp.el
CommitLineData
afe98dfa
CD
1;;; ob-lisp.el --- org-babel functions for Common Lisp
2
73b0cd50 3;; Copyright (C) 2010-2011 Free Software Foundation
afe98dfa 4
acedf35c 5;; Author: David T. O'Toole <dto@gnu.org>, Eric Schulte
afe98dfa
CD
6;; Keywords: literate programming, reproducible research, lisp
7;; Homepage: http://orgmode.org
acedf35c 8;; Version: 7.4
afe98dfa 9
acedf35c 10;;; License:
afe98dfa 11
acedf35c 12;; This program is free software; you can redistribute it and/or modify
afe98dfa 13;; it under the terms of the GNU General Public License as published by
acedf35c
CD
14;; the Free Software Foundation; either version 3, or (at your option)
15;; any later version.
16;;
17;; This program is distributed in the hope that it will be useful,
afe98dfa
CD
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.
acedf35c 21;;
afe98dfa 22;; You should have received a copy of the GNU General Public License
acedf35c
CD
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
afe98dfa
CD
26
27;;; Commentary:
28
29;; Now working with SBCL for both session and external evaluation.
30;;
31;; This certainly isn't optimally robust, but it seems to be working
32;; for the basic use cases.
33
34;;; Requirements:
35
36;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
37;; See http://common-lisp.net/project/slime/
38
39;;; Code:
40(require 'ob)
41(require 'ob-ref)
42(require 'ob-comint)
43(require 'ob-eval)
acedf35c
CD
44
45(declare-function slime-eval "ext:slime" (sexp &optional package))
46(declare-function slime-process "ext:slime" (&optional connection))
537c0c72 47(declare-function slime-connected-p "ext:slime" ())
afe98dfa
CD
48
49(defvar org-babel-default-header-args:lisp '()
50 "Default header arguments for lisp code blocks.")
51
52(defcustom org-babel-lisp-cmd "sbcl --script"
acedf35c
CD
53 "Name of command used to evaluate lisp blocks."
54 :group 'org-babel
55 :type 'string)
afe98dfa
CD
56
57(defun org-babel-expand-body:lisp (body params)
58 "Expand BODY according to PARAMS, return the expanded body."
59 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
60 (if (> (length vars) 0)
61 (concat "(let ("
62 (mapconcat
63 (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
64 vars "\n ")
65 ")\n" body ")")
66 body)))
67
68(defun org-babel-execute:lisp (body params)
69 "Execute a block of Lisp code with org-babel.
70This function is called by `org-babel-execute-src-block'"
acedf35c 71 (require 'slime)
afe98dfa
CD
72 (message "executing Lisp source code block")
73 (let* ((session (org-babel-lisp-initiate-session
74 (cdr (assoc :session params))))
75 (result-type (cdr (assoc :result-type params)))
76 (full-body (org-babel-expand-body:lisp body params)))
77 (read
78 (if session
79 ;; session evaluation
80 (save-window-excursion
81 (cadr (slime-eval `(swank:eval-and-grab-output ,full-body))))
82 ;; external evaluation
83 (let ((script-file (org-babel-temp-file "lisp-script-")))
84 (with-temp-file script-file
85 (insert
86 ;; return the value or the output
87 (if (string= result-type "value")
88 (format "(print %s)" full-body)
89 full-body)))
90 (org-babel-eval
91 (format "%s %s" org-babel-lisp-cmd
92 (org-babel-process-file-name script-file)) ""))))))
93
94;; This function should be used to assign any variables in params in
95;; the context of the session environment.
96(defun org-babel-prep-session:lisp (session params)
97 "Prepare SESSION according to the header arguments specified in PARAMS."
98 (error "not yet implemented"))
99
100(defun org-babel-lisp-initiate-session (&optional session)
101 "If there is not a current inferior-process-buffer in SESSION
102then create. Return the initialized session."
acedf35c 103 (require 'slime)
afe98dfa
CD
104 (unless (string= session "none")
105 (save-window-excursion
106 (or (slime-connected-p)
107 (slime-process)))))
108
109(provide 'ob-lisp)
110
afe98dfa
CD
111
112;;; ob-lisp.el ends here