Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-scheme.el
blobf67080adfd3006b5bf93577aae512acfa1678ec9
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 <https://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 https://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
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."
62 :group 'org-babel
63 :version "26.1"
64 :package-version '(Org . "9.1")
65 :type 'symbol)
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"))
75 (if (null vars) body
76 (format "(let (%s)\n%s\n)"
77 (mapconcat
78 (lambda (var)
79 (format "%S" (print `(,(car var) ',(cdr var)))))
80 vars
81 "\n ")
82 body)))))
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."
90 (maphash
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)))
111 (or buffer
112 (progn
113 (run-geiser impl)
114 (if name
115 (progn
116 (rename-buffer name t)
117 (org-babel-scheme-set-session-buffer name (current-buffer))))
118 (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)
131 (name)))
133 (defmacro org-babel-scheme-capture-current-message (&rest body)
134 "Capture current message in both interactive and noninteractive mode"
135 `(if noninteractive
136 (let ((original-message (symbol-function 'message))
137 (current-message nil))
138 (unwind-protect
139 (progn
140 (defun message (&rest args)
141 (setq current-message (apply original-message args)))
142 ,@body
143 current-message)
144 (fset 'message original-message)))
145 (progn
146 ,@body
147 (current-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."
155 (let ((result nil))
156 (with-temp-buffer
157 (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl))
158 (newline)
159 (insert code)
160 (geiser-mode)
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
166 (current-buffer))))
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
170 (current-buffer)))))
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 "")))))
179 (when (not repl)
180 (save-current-buffer (set-buffer repl-buffer)
181 (geiser-repl-exit))
182 (set-process-query-on-exit-flag (get-buffer-process repl-buffer) nil)
183 (kill-buffer repl-buffer)))))
184 result))
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)))
191 (cond ((listp res)
192 (mapcar (lambda (el)
193 (if (or (null el) (eq el 'null))
194 org-babel-scheme-null-to
195 el))
196 res))
197 (t res))))
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))))
206 (save-excursion
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))
215 (result
216 (org-babel-scheme-execute-with-geiser
217 full-body ; code
218 (string= result-type "output") ; output?
219 impl ; implementation
220 (and (not (string= session "none")) session)))) ; session
221 (let ((table
222 (org-babel-reassemble-table
223 result
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))))))
230 (provide 'ob-scheme)
232 ;;; ob-scheme.el ends here