ob-scheme.el 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ;;; ob-scheme.el --- Babel Functions for Scheme -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
  3. ;; Authors: Eric Schulte
  4. ;; Michael Gauland
  5. ;; Keywords: literate programming, reproducible research, scheme
  6. ;; Homepage: http://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. (defvar geiser-repl--repl) ; Defined in geiser-repl.el
  33. (defvar geiser-impl--implementation) ; Defined in geiser-impl.el
  34. (defvar geiser-default-implementation) ; Defined in geiser-impl.el
  35. (defvar geiser-active-implementations) ; Defined in geiser-impl.el
  36. (defvar geiser-debug-show-debug-p) ; Defined in geiser-debug.el
  37. (defvar geiser-debug-jump-to-debug-p) ; Defined in geiser-debug.el
  38. (defvar geiser-repl-use-other-window) ; Defined in geiser-repl.el
  39. (defvar geiser-repl-window-allow-split) ; Defined in geiser-repl.el
  40. (declare-function run-geiser "ext:geiser-repl" (impl))
  41. (declare-function geiser-mode "ext:geiser-mode" ())
  42. (declare-function geiser-eval-region "ext:geiser-mode"
  43. (start end &optional and-go raw nomsg))
  44. (declare-function geiser-repl-exit "ext:geiser-repl" (&optional arg))
  45. (declare-function geiser-eval--retort-output "ext:geiser-eval" (ret))
  46. (declare-function geiser-eval--retort-result-str "ext:geiser-eval" (ret prefix))
  47. (defcustom org-babel-scheme-null-to 'hline
  48. "Replace `null' and empty lists in scheme tables with this before returning."
  49. :group 'org-babel
  50. :version "26.1"
  51. :package-version '(Org . "9.1")
  52. :type 'symbol)
  53. (defvar org-babel-default-header-args:scheme '()
  54. "Default header arguments for scheme code blocks.")
  55. (defun org-babel-expand-body:scheme (body params)
  56. "Expand BODY according to PARAMS, return the expanded body."
  57. (let ((vars (org-babel--get-vars params))
  58. (prepends (cdr (assq :prologue params))))
  59. (concat (and prepends (concat prepends "\n"))
  60. (if (null vars) body
  61. (format "(let (%s)\n%s\n)"
  62. (mapconcat
  63. (lambda (var)
  64. (format "%S" (print `(,(car var) ',(cdr var)))))
  65. vars
  66. "\n ")
  67. body)))))
  68. (defvar org-babel-scheme-repl-map (make-hash-table :test #'equal)
  69. "Map of scheme sessions to session names.")
  70. (defun org-babel-scheme-cleanse-repl-map ()
  71. "Remove dead buffers from the REPL map."
  72. (maphash
  73. (lambda (x y) (unless (buffer-name y) (remhash x org-babel-scheme-repl-map)))
  74. org-babel-scheme-repl-map))
  75. (defun org-babel-scheme-get-session-buffer (session-name)
  76. "Look up the scheme buffer for a session; return nil if it doesn't exist."
  77. (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
  78. (gethash session-name org-babel-scheme-repl-map))
  79. (defun org-babel-scheme-set-session-buffer (session-name buffer)
  80. "Record the scheme buffer used for a given session."
  81. (puthash session-name buffer org-babel-scheme-repl-map))
  82. (defun org-babel-scheme-get-buffer-impl (buffer)
  83. "Returns the scheme implementation geiser associates with the buffer."
  84. (with-current-buffer (set-buffer buffer)
  85. geiser-impl--implementation))
  86. (defun org-babel-scheme-get-repl (impl name)
  87. "Switch to a scheme REPL, creating it if it doesn't exist:"
  88. (let ((buffer (org-babel-scheme-get-session-buffer name)))
  89. (or buffer
  90. (progn
  91. (run-geiser impl)
  92. (if name
  93. (progn
  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
  185. (org-babel-scheme-execute-with-geiser
  186. full-body ; code
  187. (string= result-type "output") ; output?
  188. impl ; implementation
  189. (and (not (string= session "none")) session)))) ; session
  190. (let ((table
  191. (org-babel-reassemble-table
  192. result
  193. (org-babel-pick-name (cdr (assq :colname-names params))
  194. (cdr (assq :colnames params)))
  195. (org-babel-pick-name (cdr (assq :rowname-names params))
  196. (cdr (assq :rownames params))))))
  197. (org-babel-scheme--table-or-string table))))))
  198. (provide 'ob-scheme)
  199. ;;; ob-scheme.el ends here