ob-scheme.el 8.8 KB

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