Commit | Line | Data |
---|---|---|
afe98dfa CD |
1 | ;;; ob-scheme.el --- org-babel functions for Scheme |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2010-2013 Free Software Foundation, Inc. |
afe98dfa CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research, scheme | |
7 | ;; Homepage: http://orgmode.org | |
afe98dfa | 8 | |
c7557a0f | 9 | ;; This file is part of GNU Emacs. |
afe98dfa | 10 | |
c7557a0f | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
afe98dfa | 12 | ;; it under the terms of the GNU General Public License as published by |
c7557a0f GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
afe98dfa CD |
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. | |
c7557a0f | 20 | |
afe98dfa | 21 | ;; You should have received a copy of the GNU General Public License |
c7557a0f | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
afe98dfa CD |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;; Now working with SBCL for both session and external evaluation. | |
27 | ;; | |
28 | ;; This certainly isn't optimally robust, but it seems to be working | |
29 | ;; for the basic use cases. | |
30 | ||
31 | ;;; Requirements: | |
32 | ||
33 | ;; - a working scheme implementation | |
34 | ;; (e.g. guile http://www.gnu.org/software/guile/guile.html) | |
c7557a0f | 35 | ;; |
afe98dfa CD |
36 | ;; - for session based evaluation cmuscheme.el is required which is |
37 | ;; included in Emacs | |
38 | ||
39 | ;;; Code: | |
40 | (require 'ob) | |
41 | (require 'ob-ref) | |
42 | (require 'ob-comint) | |
43 | (require 'ob-eval) | |
44 | (eval-when-compile (require 'cl)) | |
45 | ||
46 | (declare-function run-scheme "ext:cmuscheme" (cmd)) | |
47 | ||
48 | (defvar org-babel-default-header-args:scheme '() | |
49 | "Default header arguments for scheme code blocks.") | |
50 | ||
51 | (defvar org-babel-scheme-eoe "org-babel-scheme-eoe" | |
52 | "String to indicate that evaluation has completed.") | |
53 | ||
54 | (defcustom org-babel-scheme-cmd "guile" | |
55 | "Name of command used to evaluate scheme blocks." | |
56 | :group 'org-babel | |
372d7b21 | 57 | :version "24.1" |
afe98dfa CD |
58 | :type 'string) |
59 | ||
60 | (defun org-babel-expand-body:scheme (body params) | |
61 | "Expand BODY according to PARAMS, return the expanded body." | |
62 | (let ((vars (mapcar #'cdr (org-babel-get-header params :var)))) | |
63 | (if (> (length vars) 0) | |
64 | (concat "(let (" | |
65 | (mapconcat | |
66 | (lambda (var) (format "%S" (print `(,(car var) ',(cdr var))))) | |
67 | vars "\n ") | |
68 | ")\n" body ")") | |
69 | body))) | |
70 | ||
71 | (defvar scheme-program-name) | |
72 | (defun org-babel-execute:scheme (body params) | |
73 | "Execute a block of Scheme code with org-babel. | |
74 | This function is called by `org-babel-execute-src-block'" | |
75 | (let* ((result-type (cdr (assoc :result-type params))) | |
76 | (org-babel-scheme-cmd (or (cdr (assoc :scheme params)) | |
77 | org-babel-scheme-cmd)) | |
78 | (full-body (org-babel-expand-body:scheme body params))) | |
79 | (read | |
80 | (if (not (string= (cdr (assoc :session params)) "none")) | |
81 | ;; session evaluation | |
82 | (let ((session (org-babel-prep-session:scheme | |
83 | (cdr (assoc :session params)) params))) | |
84 | (org-babel-comint-with-output | |
85 | (session (format "%S" org-babel-scheme-eoe) t body) | |
86 | (mapc | |
87 | (lambda (line) | |
88 | (insert (org-babel-chomp line)) (comint-send-input nil t)) | |
89 | (list body (format "%S" org-babel-scheme-eoe))))) | |
90 | ;; external evaluation | |
91 | (let ((script-file (org-babel-temp-file "scheme-script-"))) | |
92 | (with-temp-file script-file | |
93 | (insert | |
94 | ;; return the value or the output | |
95 | (if (string= result-type "value") | |
96 | (format "(display %s)" full-body) | |
97 | full-body))) | |
98 | (org-babel-eval | |
99 | (format "%s %s" org-babel-scheme-cmd | |
100 | (org-babel-process-file-name script-file)) "")))))) | |
101 | ||
102 | (defun org-babel-prep-session:scheme (session params) | |
103 | "Prepare SESSION according to the header arguments specified in PARAMS." | |
104 | (let* ((session (org-babel-scheme-initiate-session session)) | |
105 | (vars (mapcar #'cdr (org-babel-get-header params :var))) | |
106 | (var-lines | |
107 | (mapcar | |
108 | (lambda (var) (format "%S" (print `(define ,(car var) ',(cdr var))))) | |
109 | vars))) | |
110 | (when session | |
111 | (org-babel-comint-in-buffer session | |
112 | (sit-for .5) (goto-char (point-max)) | |
113 | (mapc (lambda (var) | |
114 | (insert var) (comint-send-input nil t) | |
115 | (org-babel-comint-wait-for-output session) | |
116 | (sit-for .1) (goto-char (point-max))) var-lines))) | |
117 | session)) | |
118 | ||
119 | (defun org-babel-scheme-initiate-session (&optional session) | |
120 | "If there is not a current inferior-process-buffer in SESSION | |
121 | then create. Return the initialized session." | |
122 | (require 'cmuscheme) | |
123 | (unless (string= session "none") | |
124 | (let ((session-buffer (save-window-excursion | |
125 | (run-scheme org-babel-scheme-cmd) | |
126 | (rename-buffer session) | |
127 | (current-buffer)))) | |
128 | (if (org-babel-comint-buffer-livep session-buffer) | |
129 | (progn (sit-for .25) session-buffer) | |
130 | (sit-for .5) | |
131 | (org-babel-scheme-initiate-session session))))) | |
132 | ||
133 | (provide 'ob-scheme) | |
134 | ||
5b409b39 | 135 | |
afe98dfa CD |
136 | |
137 | ;;; ob-scheme.el ends here |