* test/lisp/net/tramp-tests.el (tramp-test17-dired-with-wildcards): New test.
[emacs.git] / lisp / org / ob-scheme.el
blobcd8c3860e25880a962ceeeeb58b10e0b629e6eb4
1 ;;; ob-scheme.el --- Babel Functions for Scheme -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2017 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 "ext:geiser-repl" (impl))
49 (declare-function geiser-mode "ext:geiser-mode" ())
50 (declare-function geiser-eval-region "ext:geiser-mode"
51 (start end &optional and-go raw nomsg))
52 (declare-function geiser-repl-exit "ext:geiser-repl" (&optional arg))
54 (defvar org-babel-default-header-args:scheme '()
55 "Default header arguments for scheme code blocks.")
57 (defun org-babel-expand-body:scheme (body params)
58 "Expand BODY according to PARAMS, return the expanded body."
59 (let ((vars (org-babel--get-vars params)))
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)))
69 (defvar org-babel-scheme-repl-map (make-hash-table :test 'equal)
70 "Map of scheme sessions to session names.")
72 (defun org-babel-scheme-cleanse-repl-map ()
73 "Remove dead buffers from the REPL map."
74 (maphash
75 (lambda (x y)
76 (when (not (buffer-name y))
77 (remhash x org-babel-scheme-repl-map)))
78 org-babel-scheme-repl-map))
80 (defun org-babel-scheme-get-session-buffer (session-name)
81 "Look up the scheme buffer for a session; return nil if it doesn't exist."
82 (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
83 (gethash session-name org-babel-scheme-repl-map))
85 (defun org-babel-scheme-set-session-buffer (session-name buffer)
86 "Record the scheme buffer used for a given session."
87 (puthash session-name buffer org-babel-scheme-repl-map))
89 (defun org-babel-scheme-get-buffer-impl (buffer)
90 "Returns the scheme implementation geiser associates with the buffer."
91 (with-current-buffer (set-buffer buffer)
92 geiser-impl--implementation))
94 (defun org-babel-scheme-get-repl (impl name)
95 "Switch to a scheme REPL, creating it if it doesn't exist:"
96 (let ((buffer (org-babel-scheme-get-session-buffer name)))
97 (or buffer
98 (progn
99 (run-geiser impl)
100 (if name
101 (progn
102 (rename-buffer name t)
103 (org-babel-scheme-set-session-buffer name (current-buffer))))
104 (current-buffer)))))
106 (defun org-babel-scheme-make-session-name (buffer name impl)
107 "Generate a name for the session buffer.
109 For a named session, the buffer name will be the session name.
111 If the session is unnamed (nil), generate a name.
113 If the session is `none', use nil for the session name, and
114 org-babel-scheme-execute-with-geiser will use a temporary session."
115 (let ((result
116 (cond ((not name)
117 (concat buffer " " (symbol-name impl) " REPL"))
118 ((string= name "none") nil)
119 (name))))
120 result))
122 (defmacro org-babel-scheme-capture-current-message (&rest body)
123 "Capture current message in both interactive and noninteractive mode"
124 `(if noninteractive
125 (let ((original-message (symbol-function 'message))
126 (current-message nil))
127 (unwind-protect
128 (progn
129 (defun message (&rest args)
130 (setq current-message (apply original-message args)))
131 ,@body
132 current-message)
133 (fset 'message original-message)))
134 (progn
135 ,@body
136 (current-message))))
138 (defun org-babel-scheme-execute-with-geiser (code output impl repl)
139 "Execute code in specified REPL. If the REPL doesn't exist, create it
140 using the given scheme implementation.
142 Returns the output of executing the code if the output parameter
143 is true; otherwise returns the last value."
144 (let ((result nil))
145 (with-temp-buffer
146 (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl))
147 (newline)
148 (insert (if output
149 (format "(with-output-to-string (lambda () %s))" code)
150 code))
151 (geiser-mode)
152 (let ((repl-buffer (save-current-buffer
153 (org-babel-scheme-get-repl impl repl))))
154 (when (not (eq impl (org-babel-scheme-get-buffer-impl
155 (current-buffer))))
156 (message "Implementation mismatch: %s (%s) %s (%s)" impl (symbolp impl)
157 (org-babel-scheme-get-buffer-impl (current-buffer))
158 (symbolp (org-babel-scheme-get-buffer-impl
159 (current-buffer)))))
160 (setq geiser-repl--repl repl-buffer)
161 (setq geiser-impl--implementation nil)
162 (setq result (org-babel-scheme-capture-current-message
163 (geiser-eval-region (point-min) (point-max))))
164 (setq result
165 (if (and (stringp result) (equal (substring result 0 3) "=> "))
166 (replace-regexp-in-string "^=> " "" result)
167 "\"An error occurred.\""))
168 (when (not repl)
169 (save-current-buffer (set-buffer repl-buffer)
170 (geiser-repl-exit))
171 (set-process-query-on-exit-flag (get-buffer-process repl-buffer) nil)
172 (kill-buffer repl-buffer))
173 (setq result (if (or (string= result "#<void>")
174 (string= result "#<unspecified>"))
176 result))))
177 result))
179 (defun org-babel-execute:scheme (body params)
180 "Execute a block of Scheme code with org-babel.
181 This function is called by `org-babel-execute-src-block'"
182 (let* ((source-buffer (current-buffer))
183 (source-buffer-name (replace-regexp-in-string ;; zap surrounding *
184 "^ ?\\*\\([^*]+\\)\\*" "\\1"
185 (buffer-name source-buffer))))
186 (save-excursion
187 (org-babel-reassemble-table
188 (let* ((result-type (cdr (assq :result-type params)))
189 (impl (or (when (cdr (assq :scheme params))
190 (intern (cdr (assq :scheme params))))
191 geiser-default-implementation
192 (car geiser-active-implementations)))
193 (session (org-babel-scheme-make-session-name
194 source-buffer-name (cdr (assq :session params)) impl))
195 (full-body (org-babel-expand-body:scheme body params)))
196 (org-babel-scheme-execute-with-geiser
197 full-body ; code
198 (string= result-type "output") ; output?
199 impl ; implementation
200 (and (not (string= session "none")) session))) ; session
201 (org-babel-pick-name (cdr (assq :colname-names params))
202 (cdr (assq :colnames params)))
203 (org-babel-pick-name (cdr (assq :rowname-names params))
204 (cdr (assq :rownames params)))))))
206 (provide 'ob-scheme)
208 ;;; ob-scheme.el ends here