ob-scheme.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. ;;; ob-scheme.el --- Babel Functions for Scheme -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2021 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Michael Gauland
  5. ;; Keywords: literate programming, reproducible research, scheme
  6. ;; Homepage: 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 'ob)
  31. (require 'geiser nil t)
  32. (require 'geiser-impl nil t)
  33. (defvar geiser-repl--repl) ; Defined in geiser-repl.el
  34. (defvar geiser-impl--implementation) ; Defined in geiser-impl.el
  35. (defvar geiser-scheme-implementation) ; Defined in geiser-impl.el
  36. (defvar geiser-default-implementation) ; Defined in geiser-impl.el
  37. (defvar geiser-active-implementations) ; Defined in geiser-impl.el
  38. (defvar geiser-debug-show-debug-p) ; Defined in geiser-debug.el
  39. (defvar geiser-debug-jump-to-debug-p) ; Defined in geiser-debug.el
  40. (defvar geiser-repl-use-other-window) ; Defined in geiser-repl.el
  41. (defvar geiser-repl-window-allow-split) ; Defined in geiser-repl.el
  42. (declare-function run-geiser "ext:geiser-repl" (impl))
  43. (declare-function geiser-mode "ext:geiser-mode" ())
  44. (declare-function geiser-eval-region "ext:geiser-mode"
  45. (start end &optional and-go raw nomsg))
  46. (declare-function geiser-repl-exit "ext:geiser-repl" (&optional arg))
  47. (declare-function geiser-eval--retort-output "ext:geiser-eval" (ret))
  48. (declare-function geiser-eval--retort-result-str "ext:geiser-eval" (ret prefix))
  49. (defcustom org-babel-scheme-null-to 'hline
  50. "Replace `null' and empty lists in scheme tables with this before returning."
  51. :group 'org-babel
  52. :version "26.1"
  53. :package-version '(Org . "9.1")
  54. :type 'symbol)
  55. (defvar org-babel-default-header-args:scheme '()
  56. "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. (prepends (cdr (assq :prologue params)))
  61. (postpends (cdr (assq :epilogue params))))
  62. (concat (and prepends (concat prepends "\n"))
  63. (if (null vars) body
  64. (format "(let (%s)\n%s\n)"
  65. (mapconcat
  66. (lambda (var)
  67. (format "%S" (print `(,(car var) ',(cdr var)))))
  68. vars
  69. "\n ")
  70. body))
  71. (and postpends (concat "\n" postpends)))))
  72. (defvar org-babel-scheme-repl-map (make-hash-table :test #'equal)
  73. "Map of scheme sessions to session names.")
  74. (defun org-babel-scheme-cleanse-repl-map ()
  75. "Remove dead buffers from the REPL map."
  76. (maphash
  77. (lambda (x y) (unless (buffer-name y) (remhash x org-babel-scheme-repl-map)))
  78. org-babel-scheme-repl-map))
  79. (defun org-babel-scheme-get-session-buffer (session-name)
  80. "Look up the scheme buffer for a session; return nil if it doesn't exist."
  81. (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
  82. (gethash session-name org-babel-scheme-repl-map))
  83. (defun org-babel-scheme-set-session-buffer (session-name buffer)
  84. "Record the scheme buffer used for a given session."
  85. (puthash session-name buffer org-babel-scheme-repl-map))
  86. (defun org-babel-scheme-get-buffer-impl (buffer)
  87. "Return the scheme implementation geiser associates with the buffer."
  88. (with-current-buffer (set-buffer buffer)
  89. geiser-impl--implementation))
  90. (defun org-babel-scheme-get-repl (impl name)
  91. "Switch to a scheme REPL, creating it if it doesn't exist:"
  92. (let ((buffer (org-babel-scheme-get-session-buffer name)))
  93. (or buffer
  94. (progn
  95. (run-geiser impl)
  96. (when name
  97. (rename-buffer name t)
  98. (org-babel-scheme-set-session-buffer name (current-buffer)))
  99. (current-buffer)))))
  100. (defun org-babel-scheme-make-session-name (buffer name impl)
  101. "Generate a name for the session buffer.
  102. For a named session, the buffer name will be the session name.
  103. If the session is unnamed (nil), generate a name.
  104. If the session is `none', use nil for the session name, and
  105. org-babel-scheme-execute-with-geiser will use a temporary session."
  106. (cond ((not name) (concat buffer " " (symbol-name impl) " REPL"))
  107. ((string= name "none") nil)
  108. (name)))
  109. (defmacro org-babel-scheme-capture-current-message (&rest body)
  110. "Capture current message in both interactive and noninteractive mode."
  111. `(if noninteractive
  112. (let ((original-message (symbol-function 'message))
  113. (current-message nil))
  114. (unwind-protect
  115. (progn
  116. (defun message (&rest args)
  117. (setq current-message (apply original-message args)))
  118. ,@body
  119. current-message)
  120. (fset 'message original-message)))
  121. (progn
  122. ,@body
  123. (current-message))))
  124. (defun org-babel-scheme-execute-with-geiser (code output impl repl)
  125. "Execute code in specified REPL.
  126. If the REPL doesn't exist, create it using the given scheme
  127. implementation.
  128. Returns the output of executing the code if the OUTPUT parameter
  129. is true; otherwise returns the last value."
  130. (let ((result nil))
  131. (with-temp-buffer
  132. (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl))
  133. (newline)
  134. (insert code)
  135. (geiser-mode)
  136. (let ((geiser-repl-window-allow-split nil)
  137. (geiser-repl-use-other-window nil))
  138. (let ((repl-buffer (save-current-buffer
  139. (org-babel-scheme-get-repl impl repl))))
  140. (when (not (eq impl (org-babel-scheme-get-buffer-impl
  141. (current-buffer))))
  142. (message "Implementation mismatch: %s (%s) %s (%s)" impl (symbolp impl)
  143. (org-babel-scheme-get-buffer-impl (current-buffer))
  144. (symbolp (org-babel-scheme-get-buffer-impl
  145. (current-buffer)))))
  146. (setq geiser-repl--repl repl-buffer)
  147. (setq geiser-impl--implementation nil)
  148. (let ((geiser-debug-jump-to-debug-p nil)
  149. (geiser-debug-show-debug-p nil))
  150. (let ((ret (geiser-eval-region (point-min) (point-max))))
  151. (setq result (if output
  152. (or (geiser-eval--retort-output ret)
  153. "Geiser Interpreter produced no output")
  154. (geiser-eval--retort-result-str ret "")))))
  155. (when (not repl)
  156. (save-current-buffer (set-buffer repl-buffer)
  157. (geiser-repl-exit))
  158. (set-process-query-on-exit-flag (get-buffer-process repl-buffer) nil)
  159. (kill-buffer repl-buffer)))))
  160. result))
  161. (defun org-babel-scheme--table-or-string (results)
  162. "Convert RESULTS into an appropriate elisp value.
  163. If the results look like a list or tuple, then convert them into an
  164. Emacs-lisp table, otherwise return the results as a string."
  165. (let ((res (org-babel-script-escape results)))
  166. (cond ((listp res)
  167. (mapcar (lambda (el)
  168. (if (or (null el) (eq el 'null))
  169. org-babel-scheme-null-to
  170. el))
  171. res))
  172. (t res))))
  173. (defun org-babel-execute:scheme (body params)
  174. "Execute a block of Scheme code with org-babel.
  175. This function is called by `org-babel-execute-src-block'."
  176. (let* ((source-buffer (current-buffer))
  177. (source-buffer-name (replace-regexp-in-string ;; zap surrounding *
  178. "^ ?\\*\\([^*]+\\)\\*" "\\1"
  179. (buffer-name source-buffer))))
  180. (save-excursion
  181. (let* ((result-type (cdr (assq :result-type params)))
  182. (impl (or (when (cdr (assq :scheme params))
  183. (intern (cdr (assq :scheme params))))
  184. geiser-scheme-implementation
  185. geiser-default-implementation
  186. (car geiser-active-implementations)))
  187. (session (org-babel-scheme-make-session-name
  188. source-buffer-name (cdr (assq :session params)) impl))
  189. (full-body (org-babel-expand-body:scheme body params))
  190. (result-params (cdr (assq :result-params params)))
  191. (result
  192. (org-babel-scheme-execute-with-geiser
  193. full-body ; code
  194. (string= result-type "output") ; output?
  195. impl ; implementation
  196. (and (not (string= session "none")) session)))) ; session
  197. (let ((table
  198. (org-babel-reassemble-table
  199. result
  200. (org-babel-pick-name (cdr (assq :colname-names params))
  201. (cdr (assq :colnames params)))
  202. (org-babel-pick-name (cdr (assq :rowname-names params))
  203. (cdr (assq :rownames params))))))
  204. (org-babel-result-cond result-params
  205. result
  206. (org-babel-scheme--table-or-string table)))))))
  207. (provide 'ob-scheme)
  208. ;;; ob-scheme.el ends here