1 ;;; ob-scheme.el --- Babel Functions for Scheme -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
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 <https://www.gnu.org/licenses/>.
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.
34 ;; - a working scheme implementation
35 ;; (e.g. guile https://www.gnu.org/software/guile/guile.html)
37 ;; - for session based evaluation geiser is required, which is available from
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
47 (defvar geiser-debug-show-debug-p
) ; Defined in geiser-debug.el
48 (defvar geiser-debug-jump-to-debug-p
) ; Defined in geiser-debug.el
49 (defvar geiser-repl-use-other-window
) ; Defined in geiser-repl.el
50 (defvar geiser-repl-window-allow-split
) ; Defined in geiser-repl.el
52 (declare-function run-geiser
"ext:geiser-repl" (impl))
53 (declare-function geiser-mode
"ext:geiser-mode" ())
54 (declare-function geiser-eval-region
"ext:geiser-mode"
55 (start end
&optional and-go raw nomsg
))
56 (declare-function geiser-repl-exit
"ext:geiser-repl" (&optional arg
))
57 (declare-function geiser-eval--retort-output
"ext:geiser-eval" (ret))
58 (declare-function geiser-eval--retort-result-str
"ext:geiser-eval" (ret prefix
))
60 (defcustom org-babel-scheme-null-to
'hline
61 "Replace `null' and empty lists in scheme tables with this before returning."
64 :package-version
'(Org .
"9.1")
67 (defvar org-babel-default-header-args
:scheme
'()
68 "Default header arguments for scheme code blocks.")
70 (defun org-babel-expand-body:scheme
(body params
)
71 "Expand BODY according to PARAMS, return the expanded body."
72 (let ((vars (org-babel--get-vars params
))
73 (prepends (cdr (assq :prologue params
))))
74 (concat (and prepends
(concat prepends
"\n"))
76 (format "(let (%s)\n%s\n)"
79 (format "%S" (print `(,(car var
) ',(cdr var
)))))
85 (defvar org-babel-scheme-repl-map
(make-hash-table :test
#'equal
)
86 "Map of scheme sessions to session names.")
88 (defun org-babel-scheme-cleanse-repl-map ()
89 "Remove dead buffers from the REPL map."
91 (lambda (x y
) (unless (buffer-name y
) (remhash x org-babel-scheme-repl-map
)))
92 org-babel-scheme-repl-map
))
94 (defun org-babel-scheme-get-session-buffer (session-name)
95 "Look up the scheme buffer for a session; return nil if it doesn't exist."
96 (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
97 (gethash session-name org-babel-scheme-repl-map
))
99 (defun org-babel-scheme-set-session-buffer (session-name buffer
)
100 "Record the scheme buffer used for a given session."
101 (puthash session-name buffer org-babel-scheme-repl-map
))
103 (defun org-babel-scheme-get-buffer-impl (buffer)
104 "Returns the scheme implementation geiser associates with the buffer."
105 (with-current-buffer (set-buffer buffer
)
106 geiser-impl--implementation
))
108 (defun org-babel-scheme-get-repl (impl name
)
109 "Switch to a scheme REPL, creating it if it doesn't exist:"
110 (let ((buffer (org-babel-scheme-get-session-buffer name
)))
116 (rename-buffer name t
)
117 (org-babel-scheme-set-session-buffer name
(current-buffer))))
120 (defun org-babel-scheme-make-session-name (buffer name impl
)
121 "Generate a name for the session buffer.
123 For a named session, the buffer name will be the session name.
125 If the session is unnamed (nil), generate a name.
127 If the session is `none', use nil for the session name, and
128 org-babel-scheme-execute-with-geiser will use a temporary session."
129 (cond ((not name
) (concat buffer
" " (symbol-name impl
) " REPL"))
130 ((string= name
"none") nil
)
133 (defmacro org-babel-scheme-capture-current-message
(&rest body
)
134 "Capture current message in both interactive and noninteractive mode"
136 (let ((original-message (symbol-function 'message
))
137 (current-message nil
))
140 (defun message (&rest args
)
141 (setq current-message
(apply original-message args
)))
144 (fset 'message original-message
)))
149 (defun org-babel-scheme-execute-with-geiser (code output impl repl
)
150 "Execute code in specified REPL. If the REPL doesn't exist, create it
151 using the given scheme implementation.
153 Returns the output of executing the code if the output parameter
154 is true; otherwise returns the last value."
157 (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl
))
161 (let ((geiser-repl-window-allow-split nil
)
162 (geiser-repl-use-other-window nil
))
163 (let ((repl-buffer (save-current-buffer
164 (org-babel-scheme-get-repl impl repl
))))
165 (when (not (eq impl
(org-babel-scheme-get-buffer-impl
167 (message "Implementation mismatch: %s (%s) %s (%s)" impl
(symbolp impl
)
168 (org-babel-scheme-get-buffer-impl (current-buffer))
169 (symbolp (org-babel-scheme-get-buffer-impl
171 (setq geiser-repl--repl repl-buffer
)
172 (setq geiser-impl--implementation nil
)
173 (let ((geiser-debug-jump-to-debug-p nil
)
174 (geiser-debug-show-debug-p nil
))
175 (let ((ret (geiser-eval-region (point-min) (point-max))))
176 (setq result
(if output
177 (geiser-eval--retort-output ret
)
178 (geiser-eval--retort-result-str ret
"")))))
180 (save-current-buffer (set-buffer repl-buffer
)
182 (set-process-query-on-exit-flag (get-buffer-process repl-buffer
) nil
)
183 (kill-buffer repl-buffer
)))))
186 (defun org-babel-scheme--table-or-string (results)
187 "Convert RESULTS into an appropriate elisp value.
188 If the results look like a list or tuple, then convert them into an
189 Emacs-lisp table, otherwise return the results as a string."
190 (let ((res (org-babel-script-escape results
)))
193 (if (or (null el
) (eq el
'null
))
194 org-babel-scheme-null-to
199 (defun org-babel-execute:scheme
(body params
)
200 "Execute a block of Scheme code with org-babel.
201 This function is called by `org-babel-execute-src-block'"
202 (let* ((source-buffer (current-buffer))
203 (source-buffer-name (replace-regexp-in-string ;; zap surrounding *
204 "^ ?\\*\\([^*]+\\)\\*" "\\1"
205 (buffer-name source-buffer
))))
207 (let* ((result-type (cdr (assq :result-type params
)))
208 (impl (or (when (cdr (assq :scheme params
))
209 (intern (cdr (assq :scheme params
))))
210 geiser-default-implementation
211 (car geiser-active-implementations
)))
212 (session (org-babel-scheme-make-session-name
213 source-buffer-name
(cdr (assq :session params
)) impl
))
214 (full-body (org-babel-expand-body:scheme body params
))
216 (org-babel-scheme-execute-with-geiser
218 (string= result-type
"output") ; output?
219 impl
; implementation
220 (and (not (string= session
"none")) session
)))) ; session
222 (org-babel-reassemble-table
224 (org-babel-pick-name (cdr (assq :colname-names params
))
225 (cdr (assq :colnames params
)))
226 (org-babel-pick-name (cdr (assq :rowname-names params
))
227 (cdr (assq :rownames params
))))))
228 (org-babel-scheme--table-or-string table
))))))
232 ;;; ob-scheme.el ends here