Release 7.5
[org-mode.git] / lisp / ob-lisp.el
blobf009e1818d72811fbc3c901276a1f53289ccf73b
1 ;;; ob-lisp.el --- org-babel functions for Common Lisp
3 ;; Copyright (C) 2010 Free Software Foundation
5 ;; Author: David T. O'Toole <dto@gnu.org>, Eric Schulte
6 ;; Keywords: literate programming, reproducible research, lisp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.5
10 ;;; License:
12 ;; This program 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, or (at your option)
15 ;; any later version.
17 ;; This program 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; 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.
27 ;;; Commentary:
29 ;; Now working with SBCL for both session and external evaluation.
31 ;; This certainly isn't optimally robust, but it seems to be working
32 ;; for the basic use cases.
34 ;;; Requirements:
36 ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
37 ;; See http://common-lisp.net/project/slime/
39 ;;; Code:
40 (require 'ob)
41 (require 'ob-ref)
42 (require 'ob-comint)
43 (require 'ob-eval)
45 (declare-function slime-eval "ext:slime" (sexp &optional package))
46 (declare-function slime-process "ext:slime" (&optional connection))
47 (declare-function slime-connected-p "ext:slime" ())
49 (defvar org-babel-default-header-args:lisp '()
50 "Default header arguments for lisp code blocks.")
52 (defcustom org-babel-lisp-cmd "sbcl --script"
53 "Name of command used to evaluate lisp blocks."
54 :group 'org-babel
55 :type 'string)
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)))
68 (defun org-babel-execute:lisp (body params)
69 "Execute a block of Lisp code with org-babel.
70 This function is called by `org-babel-execute-src-block'"
71 (require 'slime)
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)) ""))))))
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"))
100 (defun org-babel-lisp-initiate-session (&optional session)
101 "If there is not a current inferior-process-buffer in SESSION
102 then create. Return the initialized session."
103 (require 'slime)
104 (unless (string= session "none")
105 (save-window-excursion
106 (or (slime-connected-p)
107 (slime-process)))))
109 (provide 'ob-lisp)
111 ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
113 ;;; ob-lisp.el ends here