ob-scheme.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. ;;; ob-scheme.el --- Babel Functions for Scheme -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Michael Gauland
  5. ;; Keywords: literate programming, reproducible research, scheme
  6. ;; URL: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Now working with SBCL for both session and external evaluation.
  20. ;;
  21. ;; This certainly isn't optimally robust, but it seems to be working
  22. ;; for the basic use cases.
  23. ;;; Requirements:
  24. ;; - a working scheme implementation
  25. ;; (e.g. guile https://www.gnu.org/software/guile/guile.html)
  26. ;;
  27. ;; - for session based evaluation geiser is required, which is available from
  28. ;; ELPA.
  29. ;;; Code:
  30. (require 'org-macs)
  31. (org-assert-version)
  32. (require 'ob)
  33. (require 'geiser nil t)
  34. (require 'geiser-impl nil t)
  35. (defvar geiser-repl--repl) ; Defined in geiser-repl.el
  36. (defvar geiser-impl--implementation) ; Defined in geiser-impl.el
  37. (defvar geiser-scheme-implementation) ; Defined in geiser-impl.el
  38. (defvar geiser-default-implementation) ; Defined in geiser-impl.el
  39. (defvar geiser-active-implementations) ; Defined in geiser-impl.el
  40. (defvar geiser-debug-show-debug-p) ; Defined in geiser-debug.el
  41. (defvar geiser-debug-jump-to-debug-p) ; Defined in geiser-debug.el
  42. (defvar geiser-repl-use-other-window) ; Defined in geiser-repl.el
  43. (defvar geiser-repl-window-allow-split) ; Defined in geiser-repl.el
  44. (declare-function run-geiser "ext:geiser-repl" (impl))
  45. (declare-function geiser-mode "ext:geiser-mode" ())
  46. (declare-function geiser-eval-region "ext:geiser-mode"
  47. (start end &optional and-go raw nomsg))
  48. (declare-function geiser-eval-region/wait "ext:geiser-mode"
  49. (start end &optional timeout))
  50. (declare-function geiser-repl-exit "ext:geiser-repl" (&optional arg))
  51. (declare-function geiser-eval--retort-output "ext:geiser-eval" (ret))
  52. (declare-function geiser-eval--retort-result-str "ext:geiser-eval" (ret prefix))
  53. (defcustom org-babel-scheme-null-to 'hline
  54. "Replace `null' and empty lists in scheme tables with this before returning."
  55. :group 'org-babel
  56. :version "26.1"
  57. :package-version '(Org . "9.1")
  58. :type 'symbol)
  59. (defvar org-babel-default-header-args:scheme '()
  60. "Default header arguments for scheme code blocks.")
  61. (defun org-babel-expand-body:scheme (body params)
  62. "Expand BODY according to PARAMS, return the expanded body."
  63. (let ((vars (org-babel--get-vars params))
  64. (prepends (cdr (assq :prologue params)))
  65. (postpends (cdr (assq :epilogue params))))
  66. (concat (and prepends (concat prepends "\n"))
  67. (if (null vars) body
  68. (format "(let (%s)\n%s\n)"
  69. (mapconcat
  70. (lambda (var)
  71. (format "%S" (print `(,(car var) ',(cdr var)))))
  72. vars
  73. "\n ")
  74. body))
  75. (and postpends (concat "\n" postpends)))))
  76. (defvar org-babel-scheme-repl-map (make-hash-table :test #'equal)
  77. "Map of scheme sessions to session names.")
  78. (defun org-babel-scheme-cleanse-repl-map ()
  79. "Remove dead buffers from the REPL map."
  80. (maphash
  81. (lambda (x y) (unless (buffer-name y) (remhash x org-babel-scheme-repl-map)))
  82. org-babel-scheme-repl-map))
  83. (defun org-babel-scheme-get-session-buffer (session-name)
  84. "Look up the scheme buffer for a session; return nil if it doesn't exist."
  85. (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
  86. (gethash session-name org-babel-scheme-repl-map))
  87. (defun org-babel-scheme-set-session-buffer (session-name buffer)
  88. "Record the scheme buffer used for a given session."
  89. (puthash session-name buffer org-babel-scheme-repl-map))
  90. (defun org-babel-scheme-get-buffer-impl (buffer)
  91. "Return the scheme implementation geiser associates with the buffer."
  92. (with-current-buffer (set-buffer buffer)
  93. 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. (when name
  101. (rename-buffer name t)
  102. (org-babel-scheme-set-session-buffer name (current-buffer)))
  103. (current-buffer)))))
  104. (defun org-babel-scheme-make-session-name (buffer name impl)
  105. "Generate a name for the session buffer.
  106. For a named session, the buffer name will be the session name.
  107. If the session is unnamed (nil), generate a name.
  108. If the session is `none', use nil for the session name, and
  109. org-babel-scheme-execute-with-geiser will use a temporary session."
  110. (cond ((not name) (concat buffer " " (symbol-name impl) " REPL"))
  111. ((string= name "none") nil)
  112. (name)))
  113. (defmacro org-babel-scheme-capture-current-message (&rest body)
  114. "Capture current message in both interactive and noninteractive mode."
  115. `(if noninteractive
  116. (let ((original-message (symbol-function 'message))
  117. (current-message nil))
  118. (unwind-protect
  119. (progn
  120. (defun message (&rest args)
  121. (setq current-message (apply original-message args)))
  122. ,@body
  123. current-message)
  124. (fset 'message original-message)))
  125. (progn
  126. ,@body
  127. (current-message))))
  128. (defun org-babel-scheme-execute-with-geiser (code output impl repl)
  129. "Execute code in specified REPL.
  130. If the REPL doesn't exist, create it using the given scheme
  131. implementation.
  132. Returns the output of executing the code if the OUTPUT parameter
  133. is true; otherwise returns the last value."
  134. (let ((result nil))
  135. (with-temp-buffer
  136. (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl))
  137. (newline)
  138. (insert code)
  139. (geiser-mode)
  140. (let ((geiser-repl-window-allow-split nil)
  141. (geiser-repl-use-other-window nil))
  142. (let ((repl-buffer (save-current-buffer
  143. (org-babel-scheme-get-repl impl repl))))
  144. (when (not (eq impl (org-babel-scheme-get-buffer-impl
  145. (current-buffer))))
  146. (message "Implementation mismatch: %s (%s) %s (%s)" impl (symbolp impl)
  147. (org-babel-scheme-get-buffer-impl (current-buffer))
  148. (symbolp (org-babel-scheme-get-buffer-impl
  149. (current-buffer)))))
  150. (setq geiser-repl--repl repl-buffer)
  151. (setq geiser-impl--implementation nil)
  152. (let ((geiser-debug-jump-to-debug-p nil)
  153. (geiser-debug-show-debug-p nil))
  154. ;; `geiser-eval-region/wait' was introduced to await the
  155. ;; result of async evaluation in geiser version 0.22.
  156. (let ((ret (funcall (if (fboundp 'geiser-eval-region/wait)
  157. #'geiser-eval-region/wait
  158. #'geiser-eval-region)
  159. (point-min)
  160. (point-max))))
  161. (setq result (if output
  162. (or (geiser-eval--retort-output ret)
  163. "Geiser Interpreter produced no output")
  164. (geiser-eval--retort-result-str ret "")))))
  165. (when (not repl)
  166. (save-current-buffer (set-buffer repl-buffer)
  167. (geiser-repl-exit))
  168. (set-process-query-on-exit-flag (get-buffer-process repl-buffer) nil)
  169. (kill-buffer repl-buffer)))))
  170. result))
  171. (defun org-babel-scheme--table-or-string (results)
  172. "Convert RESULTS into an appropriate elisp value.
  173. If the results look like a list or tuple, then convert them into an
  174. Emacs-lisp table, otherwise return the results as a string."
  175. (let ((res (org-babel-script-escape results)))
  176. (cond ((listp res)
  177. (mapcar (lambda (el)
  178. (if (or (null el) (eq el 'null))
  179. org-babel-scheme-null-to
  180. el))
  181. res))
  182. (t res))))
  183. (defun org-babel-execute:scheme (body params)
  184. "Execute a block of Scheme code with org-babel.
  185. This function is called by `org-babel-execute-src-block'."
  186. (let* ((source-buffer (current-buffer))
  187. (source-buffer-name (replace-regexp-in-string ;; zap surrounding *
  188. "^ ?\\*\\([^*]+\\)\\*" "\\1"
  189. (buffer-name source-buffer))))
  190. (save-excursion
  191. (let* ((result-type (cdr (assq :result-type params)))
  192. (impl (or (when (cdr (assq :scheme params))
  193. (intern (cdr (assq :scheme params))))
  194. geiser-scheme-implementation
  195. geiser-default-implementation
  196. (car geiser-active-implementations)))
  197. (session (org-babel-scheme-make-session-name
  198. source-buffer-name (cdr (assq :session params)) impl))
  199. (full-body (org-babel-expand-body:scheme body params))
  200. (result-params (cdr (assq :result-params params)))
  201. (result
  202. (org-babel-scheme-execute-with-geiser
  203. full-body ; code
  204. (string= result-type "output") ; output?
  205. impl ; implementation
  206. (and (not (string= session "none")) session)))) ; session
  207. (let ((table
  208. (org-babel-reassemble-table
  209. result
  210. (org-babel-pick-name (cdr (assq :colname-names params))
  211. (cdr (assq :colnames params)))
  212. (org-babel-pick-name (cdr (assq :rowname-names params))
  213. (cdr (assq :rownames params))))))
  214. (org-babel-result-cond result-params
  215. result
  216. (org-babel-scheme--table-or-string table)))))))
  217. (provide 'ob-scheme)
  218. ;;; ob-scheme.el ends here