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
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)
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.
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.
36 ;; Requires SLIME (Superior Lisp Interaction Mode for Emacs.)
37 ;; See http://common-lisp.net/project/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
&optional processed-params
)
53 "Expand BODY according to PARAMS, return the expanded body."
54 (let ((vars (nth 1 (or processed-params
(org-babel-process-params params
)))))
55 (if (> (length vars
) 0)
58 (lambda (var) (format "%S" (print `(,(car var
) ',(cdr var
)))))
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* ((processed-params (org-babel-process-params params
))
68 (session (org-babel-lisp-initiate-session (first processed-params
)))
69 (result-type (fourth processed-params
))
70 (full-body (org-babel-expand-body:lisp body params processed-params
)))
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
80 ;; return the value or the output
81 (if (string= result-type
"value")
82 (format "(print %s)" full-body
)
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)
104 ;; arch-tag: 18086168-009f-4947-bbb5-3532375d851d
106 ;;; ob-lisp.el ends here