1 ;;; ob-scheme.el --- org-babel functions for Scheme
3 ;; Copyright (C) 2010-2011 Free Software Foundation
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, scheme
7 ;; Homepage: http://orgmode.org
11 ;; This program 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, or (at your option)
16 ;; This program 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; Now working with SBCL for both session and external evaluation.
30 ;; This certainly isn't optimally robust, but it seems to be working
31 ;; for the basic use cases.
35 ;; - a working scheme implementation
36 ;; (e.g. guile http://www.gnu.org/software/guile/guile.html)
38 ;; - for session based evaluation cmuscheme.el is required which is
46 (eval-when-compile (require 'cl
))
48 (declare-function run-scheme
"ext:cmuscheme" (cmd))
50 (defvar org-babel-default-header-args
:scheme
'()
51 "Default header arguments for scheme code blocks.")
53 (defvar org-babel-scheme-eoe
"org-babel-scheme-eoe"
54 "String to indicate that evaluation has completed.")
56 (defcustom org-babel-scheme-cmd
"guile"
57 "Name of command used to evaluate scheme blocks."
61 (defun org-babel-expand-body:scheme
(body params
)
62 "Expand BODY according to PARAMS, return the expanded body."
63 (let ((vars (mapcar #'cdr
(org-babel-get-header params
:var
))))
64 (if (> (length vars
) 0)
67 (lambda (var) (format "%S" (print `(,(car var
) ',(cdr var
)))))
72 (defvar scheme-program-name
)
73 (defun org-babel-execute:scheme
(body params
)
74 "Execute a block of Scheme code with org-babel.
75 This function is called by `org-babel-execute-src-block'"
76 (let* ((result-type (cdr (assoc :result-type params
)))
77 (org-babel-scheme-cmd (or (cdr (assoc :scheme params
))
78 org-babel-scheme-cmd
))
79 (full-body (org-babel-expand-body:scheme body params
)))
81 (if (not (string= (cdr (assoc :session params
)) "none"))
83 (let ((session (org-babel-prep-session:scheme
84 (cdr (assoc :session params
)) params
)))
85 (org-babel-comint-with-output
86 (session (format "%S" org-babel-scheme-eoe
) t body
)
89 (insert (org-babel-chomp line
)) (comint-send-input nil t
))
90 (list body
(format "%S" org-babel-scheme-eoe
)))))
91 ;; external evaluation
92 (let ((script-file (org-babel-temp-file "scheme-script-")))
93 (with-temp-file script-file
95 ;; return the value or the output
96 (if (string= result-type
"value")
97 (format "(display %s)" full-body
)
100 (format "%s %s" org-babel-scheme-cmd
101 (org-babel-process-file-name script-file
)) ""))))))
103 (defun org-babel-prep-session:scheme
(session params
)
104 "Prepare SESSION according to the header arguments specified in PARAMS."
105 (let* ((session (org-babel-scheme-initiate-session session
))
106 (vars (mapcar #'cdr
(org-babel-get-header params
:var
)))
109 (lambda (var) (format "%S" (print `(define ,(car var
) ',(cdr var
)))))
112 (org-babel-comint-in-buffer session
113 (sit-for .5) (goto-char (point-max))
115 (insert var
) (comint-send-input nil t
)
116 (org-babel-comint-wait-for-output session
)
117 (sit-for .1) (goto-char (point-max))) var-lines
)))
120 (defun org-babel-scheme-initiate-session (&optional session
)
121 "If there is not a current inferior-process-buffer in SESSION
122 then create. Return the initialized session."
124 (unless (string= session
"none")
125 (let ((session-buffer (save-window-excursion
126 (run-scheme org-babel-scheme-cmd
)
127 (rename-buffer session
)
129 (if (org-babel-comint-buffer-livep session-buffer
)
130 (progn (sit-for .25) session-buffer
)
132 (org-babel-scheme-initiate-session session
)))))
138 ;;; ob-scheme.el ends here