Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-lisp.el
blobf550a54e1e33a28e0a200f2704a914b49a7686b4
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.3
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)
44 (require 'slime)
46 (defvar org-babel-default-header-args:lisp '()
47 "Default header arguments for lisp code blocks.")
49 (defcustom org-babel-lisp-cmd "sbcl --script"
50 "Name of command used to evaluate lisp blocks.")
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 (if (> (length vars) 0)
56 (concat "(let ("
57 (mapconcat
58 (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
59 vars "\n ")
60 ")\n" body ")")
61 body)))
63 (defun org-babel-execute:lisp (body params)
64 "Execute a block of Lisp code with org-babel.
65 This function is called by `org-babel-execute-src-block'"
66 (message "executing Lisp source code block")
67 (let* ((session (org-babel-lisp-initiate-session
68 (cdr (assoc :session params))))
69 (result-type (cdr (assoc :result-type params)))
70 (full-body (org-babel-expand-body:lisp body params)))
71 (read
72 (if session
73 ;; session evaluation
74 (save-window-excursion
75 (cadr (slime-eval `(swank:eval-and-grab-output ,full-body))))
76 ;; external evaluation
77 (let ((script-file (org-babel-temp-file "lisp-script-")))
78 (with-temp-file script-file
79 (insert
80 ;; return the value or the output
81 (if (string= result-type "value")
82 (format "(print %s)" full-body)
83 full-body)))
84 (org-babel-eval
85 (format "%s %s" org-babel-lisp-cmd
86 (org-babel-process-file-name script-file)) ""))))))
88 ;; This function should be used to assign any variables in params in
89 ;; the context of the session environment.
90 (defun org-babel-prep-session:lisp (session params)
91 "Prepare SESSION according to the header arguments specified in PARAMS."
92 (error "not yet implemented"))
94 (defun org-babel-lisp-initiate-session (&optional session)
95 "If there is not a current inferior-process-buffer in SESSION
96 then create. Return the initialized session."
97 (unless (string= session "none")
98 (save-window-excursion
99 (or (slime-connected-p)
100 (slime-process)))))
102 (provide 'ob-lisp)
104 ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
106 ;;; ob-lisp.el ends here