ob-scheme.el 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. ;;; ob-scheme.el --- Babel Functions for Scheme -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2018 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. (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. (if name
  94. (progn
  95. (rename-buffer name t)
  96. (org-babel-scheme-set-session-buffer name (current-buffer))))
  97. (current-buffer)))))
  98. (defun org-babel-scheme-make-session-name (buffer name impl)
  99. "Generate a name for the session buffer.
  100. For a named session, the buffer name will be the session name.
  101. If the session is unnamed (nil), generate a name.
  102. If the session is `none', use nil for the session name, and
  103. org-babel-scheme-execute-with-geiser will use a temporary session."
  104. (cond ((not name) (concat buffer " " (symbol-name impl) " REPL"))
  105. ((string= name "none") nil)
  106. (name)))
  107. (defmacro org-babel-scheme-capture-current-message (&rest body)
  108. "Capture current message in both interactive and noninteractive mode"
  109. `(if noninteractive
  110. (let ((original-message (symbol-function 'message))
  111. (current-message nil))
  112. (unwind-protect
  113. (progn
  114. (defun message (&rest args)
  115. (setq current-message (apply original-message args)))
  116. ,@body
  117. current-message)
  118. (fset 'message original-message)))
  119. (progn
  120. ,@body
  121. (current-message))))
  122. (defun org-babel-scheme-execute-with-geiser (code output impl repl)
  123. "Execute code in specified REPL. If the REPL doesn't exist, create it
  124. using the given scheme implementation.
  125. Returns the output of executing the code if the output parameter
  126. is true; otherwise returns the last value."
  127. (let ((result nil))
  128. (with-temp-buffer
  129. (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl))
  130. (newline)
  131. (insert code)
  132. (geiser-mode)
  133. (let ((geiser-repl-window-allow-split nil)
  134. (geiser-repl-use-other-window nil))
  135. (let ((repl-buffer (save-current-buffer
  136. (org-babel-scheme-get-repl impl repl))))
  137. (when (not (eq impl (org-babel-scheme-get-buffer-impl
  138. (current-buffer))))
  139. (message "Implementation mismatch: %s (%s) %s (%s)" impl (symbolp impl)
  140. (org-babel-scheme-get-buffer-impl (current-buffer))
  141. (symbolp (org-babel-scheme-get-buffer-impl
  142. (current-buffer)))))
  143. (setq geiser-repl--repl repl-buffer)
  144. (setq geiser-impl--implementation nil)
  145. (let ((geiser-debug-jump-to-debug-p nil)
  146. (geiser-debug-show-debug-p nil))
  147. (let ((ret (geiser-eval-region (point-min) (point-max))))
  148. (setq result (if output
  149. (geiser-eval--retort-output ret)
  150. (geiser-eval--retort-result-str ret "")))))
  151. (when (not repl)
  152. (save-current-buffer (set-buffer repl-buffer)
  153. (geiser-repl-exit))
  154. (set-process-query-on-exit-flag (get-buffer-process repl-buffer) nil)
  155. (kill-buffer repl-buffer)))))
  156. result))
  157. (defun org-babel-scheme--table-or-string (results)
  158. "Convert RESULTS into an appropriate elisp value.
  159. If the results look like a list or tuple, then convert them into an
  160. Emacs-lisp table, otherwise return the results as a string."
  161. (let ((res (org-babel-script-escape results)))
  162. (cond ((listp res)
  163. (mapcar (lambda (el)
  164. (if (or (null el) (eq el 'null))
  165. org-babel-scheme-null-to
  166. el))
  167. res))
  168. (t res))))
  169. (defun org-babel-execute:scheme (body params)
  170. "Execute a block of Scheme code with org-babel.
  171. This function is called by `org-babel-execute-src-block'"
  172. (let* ((source-buffer (current-buffer))
  173. (source-buffer-name (replace-regexp-in-string ;; zap surrounding *
  174. "^ ?\\*\\([^*]+\\)\\*" "\\1"
  175. (buffer-name source-buffer))))
  176. (save-excursion
  177. (let* ((result-type (cdr (assq :result-type params)))
  178. (impl (or (when (cdr (assq :scheme params))
  179. (intern (cdr (assq :scheme params))))
  180. geiser-default-implementation
  181. (car geiser-active-implementations)))
  182. (session (org-babel-scheme-make-session-name
  183. source-buffer-name (cdr (assq :session params)) impl))
  184. (full-body (org-babel-expand-body:scheme body 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-scheme--table-or-string table))))))
  199. (provide 'ob-scheme)
  200. ;;; ob-scheme.el ends here