ob-scheme.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <http://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 http://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. (declare-function run-geiser "ext:geiser-repl" (impl))
  37. (declare-function geiser-mode "ext:geiser-mode" ())
  38. (declare-function geiser-eval-region "ext:geiser-mode"
  39. (start end &optional and-go raw nomsg))
  40. (declare-function geiser-repl-exit "ext:geiser-repl" (&optional arg))
  41. (defvar org-babel-default-header-args:scheme '()
  42. "Default header arguments for scheme code blocks.")
  43. (defun org-babel-expand-body:scheme (body params)
  44. "Expand BODY according to PARAMS, return the expanded body."
  45. (let ((vars (org-babel--get-vars params)))
  46. (if (> (length vars) 0)
  47. (concat "(let ("
  48. (mapconcat
  49. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  50. vars "\n ")
  51. ")\n" body ")")
  52. body)))
  53. (defvar org-babel-scheme-repl-map (make-hash-table :test 'equal)
  54. "Map of scheme sessions to session names.")
  55. (defun org-babel-scheme-cleanse-repl-map ()
  56. "Remove dead buffers from the REPL map."
  57. (maphash
  58. (lambda (x y)
  59. (when (not (buffer-name y))
  60. (remhash x org-babel-scheme-repl-map)))
  61. org-babel-scheme-repl-map))
  62. (defun org-babel-scheme-get-session-buffer (session-name)
  63. "Look up the scheme buffer for a session; return nil if it doesn't exist."
  64. (org-babel-scheme-cleanse-repl-map) ; Prune dead sessions
  65. (gethash session-name org-babel-scheme-repl-map))
  66. (defun org-babel-scheme-set-session-buffer (session-name buffer)
  67. "Record the scheme buffer used for a given session."
  68. (puthash session-name buffer org-babel-scheme-repl-map))
  69. (defun org-babel-scheme-get-buffer-impl (buffer)
  70. "Returns the scheme implementation geiser associates with the buffer."
  71. (with-current-buffer (set-buffer buffer)
  72. geiser-impl--implementation))
  73. (defun org-babel-scheme-get-repl (impl name)
  74. "Switch to a scheme REPL, creating it if it doesn't exist:"
  75. (let ((buffer (org-babel-scheme-get-session-buffer name)))
  76. (or buffer
  77. (progn
  78. (run-geiser impl)
  79. (if name
  80. (progn
  81. (rename-buffer name t)
  82. (org-babel-scheme-set-session-buffer name (current-buffer))))
  83. (current-buffer)))))
  84. (defun org-babel-scheme-make-session-name (buffer name impl)
  85. "Generate a name for the session buffer.
  86. For a named session, the buffer name will be the session name.
  87. If the session is unnamed (nil), generate a name.
  88. If the session is `none', use nil for the session name, and
  89. org-babel-scheme-execute-with-geiser will use a temporary session."
  90. (let ((result
  91. (cond ((not name)
  92. (concat buffer " " (symbol-name impl) " REPL"))
  93. ((string= name "none") nil)
  94. (name))))
  95. result))
  96. (defmacro org-babel-scheme-capture-current-message (&rest body)
  97. "Capture current message in both interactive and noninteractive mode"
  98. `(if noninteractive
  99. (let ((original-message (symbol-function 'message))
  100. (current-message nil))
  101. (unwind-protect
  102. (progn
  103. (defun message (&rest args)
  104. (setq current-message (apply original-message args)))
  105. ,@body
  106. current-message)
  107. (fset 'message original-message)))
  108. (progn
  109. ,@body
  110. (current-message))))
  111. (defun org-babel-scheme-execute-with-geiser (code output impl repl)
  112. "Execute code in specified REPL. If the REPL doesn't exist, create it
  113. using the given scheme implementation.
  114. Returns the output of executing the code if the output parameter
  115. is true; otherwise returns the last value."
  116. (let ((result nil))
  117. (with-temp-buffer
  118. (insert (format ";; -*- geiser-scheme-implementation: %s -*-" impl))
  119. (newline)
  120. (insert (if output
  121. (format "(with-output-to-string (lambda () %s))" code)
  122. code))
  123. (geiser-mode)
  124. (let ((repl-buffer (save-current-buffer
  125. (org-babel-scheme-get-repl impl repl))))
  126. (when (not (eq impl (org-babel-scheme-get-buffer-impl
  127. (current-buffer))))
  128. (message "Implementation mismatch: %s (%s) %s (%s)" impl (symbolp impl)
  129. (org-babel-scheme-get-buffer-impl (current-buffer))
  130. (symbolp (org-babel-scheme-get-buffer-impl
  131. (current-buffer)))))
  132. (setq geiser-repl--repl repl-buffer)
  133. (setq geiser-impl--implementation nil)
  134. (setq result (org-babel-scheme-capture-current-message
  135. (geiser-eval-region (point-min) (point-max))))
  136. (setq result
  137. (if (and (stringp result) (equal (substring result 0 3) "=> "))
  138. (replace-regexp-in-string "^=> " "" result)
  139. "\"An error occurred.\""))
  140. (when (not repl)
  141. (save-current-buffer (set-buffer repl-buffer)
  142. (geiser-repl-exit))
  143. (set-process-query-on-exit-flag (get-buffer-process repl-buffer) nil)
  144. (kill-buffer repl-buffer))
  145. (setq result (if (or (string= result "#<void>")
  146. (string= result "#<unspecified>"))
  147. nil
  148. result))))
  149. result))
  150. (defun org-babel-execute:scheme (body params)
  151. "Execute a block of Scheme code with org-babel.
  152. This function is called by `org-babel-execute-src-block'"
  153. (let* ((source-buffer (current-buffer))
  154. (source-buffer-name (replace-regexp-in-string ;; zap surrounding *
  155. "^ ?\\*\\([^*]+\\)\\*" "\\1"
  156. (buffer-name source-buffer))))
  157. (save-excursion
  158. (org-babel-reassemble-table
  159. (let* ((result-type (cdr (assq :result-type params)))
  160. (impl (or (when (cdr (assq :scheme params))
  161. (intern (cdr (assq :scheme params))))
  162. geiser-default-implementation
  163. (car geiser-active-implementations)))
  164. (session (org-babel-scheme-make-session-name
  165. source-buffer-name (cdr (assq :session params)) impl))
  166. (full-body (org-babel-expand-body:scheme body params)))
  167. (org-babel-scheme-execute-with-geiser
  168. full-body ; code
  169. (string= result-type "output") ; output?
  170. impl ; implementation
  171. (and (not (string= session "none")) session))) ; session
  172. (org-babel-pick-name (cdr (assq :colname-names params))
  173. (cdr (assq :colnames params)))
  174. (org-babel-pick-name (cdr (assq :rowname-names params))
  175. (cdr (assq :rownames params)))))))
  176. (provide 'ob-scheme)
  177. ;;; ob-scheme.el ends here