ob-scheme.el 7.5 KB

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