Merge branch 'maint'
[org-mode.git] / lisp / ob-scheme.el
blob89dd003319fec70c13c1d2451c127d9f66e56df8
1 ;;; ob-scheme.el --- org-babel functions for Scheme
3 ;; Copyright (C) 2010-2013 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, scheme
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Now working with SBCL for both session and external evaluation.
28 ;; This certainly isn't optimally robust, but it seems to be working
29 ;; for the basic use cases.
31 ;;; Requirements:
33 ;; - a working scheme implementation
34 ;; (e.g. guile http://www.gnu.org/software/guile/guile.html)
36 ;; - for session based evaluation cmuscheme.el is required which is
37 ;; included in Emacs
39 ;;; Code:
40 (require 'ob)
41 (eval-when-compile (require 'cl))
43 (declare-function run-scheme "ext:cmuscheme" (cmd))
45 (defvar org-babel-default-header-args:scheme '()
46 "Default header arguments for scheme code blocks.")
48 (defvar org-babel-scheme-eoe "org-babel-scheme-eoe"
49 "String to indicate that evaluation has completed.")
51 (defcustom org-babel-scheme-cmd "guile"
52 "Name of command used to evaluate scheme blocks."
53 :group 'org-babel
54 :version "24.1"
55 :type 'string)
57 (defun org-babel-expand-body:scheme (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 (defvar scheme-program-name)
69 (defun org-babel-execute:scheme (body params)
70 "Execute a block of Scheme code with org-babel.
71 This function is called by `org-babel-execute-src-block'"
72 (let* ((result-type (cdr (assoc :result-type params)))
73 (org-babel-scheme-cmd (or (cdr (assoc :scheme params))
74 org-babel-scheme-cmd))
75 (full-body (org-babel-expand-body:scheme body params))
76 (result (if (not (string= (cdr (assoc :session params)) "none"))
77 ;; session evaluation
78 (let ((session (org-babel-prep-session:scheme
79 (cdr (assoc :session params)) params)))
80 (org-babel-comint-with-output
81 (session (format "%S" org-babel-scheme-eoe) t body)
82 (mapc
83 (lambda (line)
84 (insert (org-babel-chomp line))
85 (comint-send-input nil t))
86 (list body (format "%S" org-babel-scheme-eoe)))))
87 ;; external evaluation
88 (let ((script-file (org-babel-temp-file "scheme-script-")))
89 (with-temp-file script-file
90 (insert
91 ;; return the value or the output
92 (if (string= result-type "value")
93 (format "(display %s)" full-body)
94 full-body)))
95 (org-babel-eval
96 (format "%s %s" org-babel-scheme-cmd
97 (org-babel-process-file-name script-file)) "")))))
98 (org-babel-result-cond (cdr (assoc :result-params params))
99 result (read result))))
101 (defun org-babel-prep-session:scheme (session params)
102 "Prepare SESSION according to the header arguments specified in PARAMS."
103 (let* ((session (org-babel-scheme-initiate-session session))
104 (vars (mapcar #'cdr (org-babel-get-header params :var)))
105 (var-lines
106 (mapcar
107 (lambda (var) (format "%S" (print `(define ,(car var) ',(cdr var)))))
108 vars)))
109 (when session
110 (org-babel-comint-in-buffer session
111 (sit-for .5) (goto-char (point-max))
112 (mapc (lambda (var)
113 (insert var) (comint-send-input nil t)
114 (org-babel-comint-wait-for-output session)
115 (sit-for .1) (goto-char (point-max))) var-lines)))
116 session))
118 (defun org-babel-scheme-initiate-session (&optional session)
119 "If there is not a current inferior-process-buffer in SESSION
120 then create. Return the initialized session."
121 (require 'cmuscheme)
122 (unless (string= session "none")
123 (let ((session-buffer (save-window-excursion
124 (run-scheme org-babel-scheme-cmd)
125 (rename-buffer session)
126 (current-buffer))))
127 (if (org-babel-comint-buffer-livep session-buffer)
128 (progn (sit-for .25) session-buffer)
129 (sit-for .5)
130 (org-babel-scheme-initiate-session session)))))
132 (provide 'ob-scheme)
136 ;;; ob-scheme.el ends here