lisp/org-table.el: fix table alignment
[org-mode/org-tableheadings.git] / lisp / ob-scheme.el
blobceef8e3a2980d1d168fe16f8b62ac552c29c4641
1 ;;; ob-scheme.el --- Babel Functions for Scheme -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2019 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
6 ;; Michael Gauland
7 ;; Keywords: literate programming, reproducible research, scheme
8 ;; Homepage: https://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 (require 'geiser-impl nil t)
44 (defvar geiser-repl--repl) ; Defined in geiser-repl.el
45 (defvar geiser-impl--implementation) ; Defined in geiser-impl.el
46 (defvar geiser-default-implementation) ; Defined in geiser-impl.el
47 (defvar geiser-active-implementations) ; Defined in geiser-impl.el
48 (defvar geiser-debug-show-debug-p) ; Defined in geiser-debug.el
49 (defvar geiser-debug-jump-to-debug-p) ; Defined in geiser-debug.el
50 (defvar geiser-repl-use-other-window) ; Defined in geiser-repl.el
51 (defvar geiser-repl-window-allow-split) ; Defined in geiser-repl.el
53 (declare-function run-geiser "ext:geiser-repl" (impl))
54 (declare-function geiser-mode "ext:geiser-mode" ())
55 (declare-function geiser-eval-region "ext:geiser-mode"
56 (start end &optional and-go raw nomsg))
57 (declare-function geiser-repl-exit "ext:geiser-repl" (&optional arg))
58 (declare-function geiser-eval--retort-output "ext:geiser-eval" (ret))
59 (declare-function geiser-eval--retort-result-str "ext:geiser-eval" (ret prefix))
61 (defcustom org-babel-scheme-null-to 'hline
62 "Replace `null' and empty lists in scheme tables with this before returning."
63 :group 'org-babel
64 :version "26.1"
65 :package-version '(Org . "9.1")
66 :type 'symbol)
68 (defvar org-babel-default-header-args:scheme '()
69 "Default header arguments for scheme code blocks.")
71 (defun org-babel-expand-body:scheme (body params)
72 "Expand BODY according to PARAMS, return the expanded body."
73 (let ((vars (org-babel--get-vars params))
74 (prepends (cdr (assq :prologue params))))
75 (concat (and prepends (concat prepends "\n"))
76 (if (null vars) body
77 (format "(let (%s)\n%s\n)"
78 (mapconcat
79 (lambda (var)
80 (format "%S" (print `(,(car var) ',(cdr var)))))
81 vars
82 "\n ")
83 body)))))
86 (defvar org-babel-scheme-repl-map (make-hash-table :test #'equal)
87 "Map of scheme sessions to session names.")
89 (defun org-babel-scheme-cleanse-repl-map ()
90 "Remove dead buffers from the REPL map."
91 (maphash
92 (lambda (x y) (unless (buffer-name y) (remhash x org-babel-scheme-repl-map)))
93 org-babel-scheme-repl-map))
95 (defun org-babel-scheme-get-session-buffer (session-name)
96 "Look up the scheme buffer for a session; return nil if it doesn't exist."
97 (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
98 (gethash session-name org-babel-scheme-repl-map))
100 (defun org-babel-scheme-set-session-buffer (session-name buffer)
101 "Record the scheme buffer used for a given session."
102 (puthash session-name buffer org-babel-scheme-repl-map))
104 (defun org-babel-scheme-get-buffer-impl (buffer)
105 "Returns the scheme implementation geiser associates with the buffer."
106 (with-current-buffer (set-buffer buffer)
107 geiser-impl--implementation))
109 (defun org-babel-scheme-get-repl (impl name)
110 "Switch to a scheme REPL, creating it if it doesn't exist:"
111 (let ((buffer (org-babel-scheme-get-session-buffer name)))
112 (or buffer
113 (progn
114 (run-geiser impl)
115 (when name
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-params (cdr (assq :result-params params)))
216 (result
217 (org-babel-scheme-execute-with-geiser
218 full-body ; code
219 (string= result-type "output") ; output?
220 impl ; implementation
221 (and (not (string= session "none")) session)))) ; session
222 (let ((table
223 (org-babel-reassemble-table
224 result
225 (org-babel-pick-name (cdr (assq :colname-names params))
226 (cdr (assq :colnames params)))
227 (org-babel-pick-name (cdr (assq :rowname-names params))
228 (cdr (assq :rownames params))))))
229 (org-babel-result-cond result-params
230 result
231 (org-babel-scheme--table-or-string table)))))))
233 (provide 'ob-scheme)
235 ;;; ob-scheme.el ends here