Merge branch 'maint'
[org-mode.git] / lisp / ob-scheme.el
blob2095534e2024c8e955644a71e8a6bc2edcbe5179
1 ;;; ob-scheme.el --- org-babel functions for Scheme
3 ;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
6 ;; Michael Gauland
7 ;; Keywords: literate programming, reproducible research, scheme
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Now working with SBCL for both session and external evaluation.
29 ;; This certainly isn't optimally robust, but it seems to be working
30 ;; for the basic use cases.
32 ;;; Requirements:
34 ;; - a working scheme implementation
35 ;; (e.g. guile http://www.gnu.org/software/guile/guile.html)
37 ;; - for session based evaluation geiser is required, which is available from
38 ;; ELPA.
40 ;;; Code:
41 (require 'ob)
42 (require 'geiser nil t)
43 (defvar geiser-repl--repl) ; Defined in geiser-repl.el
44 (defvar geiser-impl--implementation) ; Defined in geiser-impl.el
45 (defvar geiser-default-implementation) ; Defined in geiser-impl.el
46 (defvar geiser-active-implementations) ; Defined in geiser-impl.el
48 (declare-function run-geiser "geiser-repl" (impl))
49 (declare-function geiser-mode "geiser-mode" ())
50 (declare-function geiser-eval-region "geiser-mode" (start end &optional and-go raw nomsg))
51 (declare-function geiser-repl-exit "geiser-repl" (&optional arg))
53 (defvar org-babel-default-header-args:scheme '()
54 "Default header arguments for scheme code blocks.")
56 (defun org-babel-expand-body:scheme (body params)
57 "Expand BODY according to PARAMS, return the expanded body."
58 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
59 (if (> (length vars) 0)
60 (concat "(let ("
61 (mapconcat
62 (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
63 vars "\n ")
64 ")\n" body ")")
65 body)))
68 (defvar org-babel-scheme-repl-map (make-hash-table :test 'equal)
69 "Map of scheme sessions to session names.")
71 (defun org-babel-scheme-cleanse-repl-map ()
72 "Remove dead buffers from the REPL map."
73 (maphash
74 (lambda (x y)
75 (when (not (buffer-name y))
76 (remhash x org-babel-scheme-repl-map)))
77 org-babel-scheme-repl-map))
79 (defun org-babel-scheme-get-session-buffer (session-name)
80 "Look up the scheme buffer for a session; return nil if it doesn't exist."
81 (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
82 (gethash session-name org-babel-scheme-repl-map))
84 (defun org-babel-scheme-set-session-buffer (session-name buffer)
85 "Record the scheme buffer used for a given session."
86 (puthash session-name buffer org-babel-scheme-repl-map))
88 (defun org-babel-scheme-get-buffer-impl (buffer)
89 "Returns the scheme implementation geiser associates with the buffer."
90 (with-current-buffer (set-buffer buffer)
91 geiser-impl--implementation))
93 (defun org-babel-scheme-get-repl (impl name)
94 "Switch to a scheme REPL, creating it if it doesn't exist:"
95 (let ((buffer (org-babel-scheme-get-session-buffer name)))
96 (or buffer
97 (progn
98 (run-geiser impl)
99 (if name
100 (progn
101 (rename-buffer name t)
102 (org-babel-scheme-set-session-buffer name (current-buffer))))
103 (current-buffer)))))
105 (defun org-babel-scheme-make-session-name (buffer name impl)
106 "Generate a name for the session buffer.
108 For a named session, the buffer name will be the session name.
110 If the session is unnamed (nil), generate a name.
112 If the session is 'none', use nil for the session name, and
113 org-babel-scheme-execute-with-geiser will use a temporary session."
114 (let ((result
115 (cond ((not name)
116 (concat buffer " " (symbol-name impl) " REPL"))
117 ((string= name "none") nil)
118 (name))))
119 result))
121 (defmacro org-babel-scheme-capture-current-message (&rest body)
122 "Capture current message in both interactive and noninteractive mode"
123 `(if noninteractive
124 (let ((original-message (symbol-function 'message))
125 (current-message nil))
126 (unwind-protect
127 (progn
128 (defun message (&rest args)
129 (setq current-message (apply original-message args)))
130 ,@body
131 current-message)
132 (fset 'message original-message)))
133 (progn
134 ,@body
135 (current-message))))
137 (defun org-babel-scheme-execute-with-geiser (code output impl repl)
138 "Execute code in specified REPL. If the REPL doesn't exist, create it
139 using the given scheme implementation.
141 Returns the output of executing the code if the output parameter
142 is true; otherwise returns the last value."
143 (let ((result nil))
144 (with-temp-buffer
145 (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl))
146 (newline)
147 (insert (if output
148 (format "(with-output-to-string (lambda () %s))" code)
149 code))
150 (geiser-mode)
151 (let ((repl-buffer (save-current-buffer
152 (org-babel-scheme-get-repl impl repl))))
153 (when (not (eq impl (org-babel-scheme-get-buffer-impl
154 (current-buffer))))
155 (message "Implementation mismatch: %s (%s) %s (%s)" impl (symbolp impl)
156 (org-babel-scheme-get-buffer-impl (current-buffer))
157 (symbolp (org-babel-scheme-get-buffer-impl
158 (current-buffer)))))
159 (setq geiser-repl--repl repl-buffer)
160 (setq geiser-impl--implementation nil)
161 (setq result (org-babel-scheme-capture-current-message
162 (geiser-eval-region (point-min) (point-max))))
163 (setq result
164 (if (and (stringp result) (equal (substring result 0 3) "=> "))
165 (replace-regexp-in-string "^=> " "" result)
166 "\"An error occurred.\""))
167 (when (not repl)
168 (save-current-buffer (set-buffer repl-buffer)
169 (geiser-repl-exit))
170 (set-process-query-on-exit-flag (get-buffer-process repl-buffer) nil)
171 (kill-buffer repl-buffer))
172 (setq result (if (or (string= result "#<void>")
173 (string= result "#<unspecified>"))
175 (read result)))))
176 result))
178 (defun org-babel-execute:scheme (body params)
179 "Execute a block of Scheme code with org-babel.
180 This function is called by `org-babel-execute-src-block'"
181 (let* ((source-buffer (current-buffer))
182 (source-buffer-name (replace-regexp-in-string ;; zap surrounding *
183 "^ ?\\*\\([^*]+\\)\\*" "\\1"
184 (buffer-name source-buffer))))
185 (save-excursion
186 (org-babel-reassemble-table
187 (let* ((result-type (cdr (assoc :result-type params)))
188 (impl (or (when (cdr (assoc :scheme params))
189 (intern (cdr (assoc :scheme params))))
190 geiser-default-implementation
191 (car geiser-active-implementations)))
192 (session (org-babel-scheme-make-session-name
193 source-buffer-name (cdr (assoc :session params)) impl))
194 (full-body (org-babel-expand-body:scheme body params)))
195 (org-babel-scheme-execute-with-geiser
196 full-body ; code
197 (string= result-type "output") ; output?
198 impl ; implementation
199 (and (not (string= session "none")) session))) ; session
200 (org-babel-pick-name (cdr (assoc :colname-names params))
201 (cdr (assoc :colnames params)))
202 (org-babel-pick-name (cdr (assoc :rowname-names params))
203 (cdr (assoc :rownames params)))))))
205 (provide 'ob-scheme)
207 ;;; ob-scheme.el ends here