ob-scheme.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; ob-scheme.el --- org-babel functions for Scheme
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, scheme
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Now working with SBCL for both session and external evaluation.
  19. ;;
  20. ;; This certainly isn't optimally robust, but it seems to be working
  21. ;; for the basic use cases.
  22. ;;; Requirements:
  23. ;; - a working scheme implementation
  24. ;; (e.g. guile http://www.gnu.org/software/guile/guile.html)
  25. ;;
  26. ;; - for session based evaluation cmuscheme.el is required which is
  27. ;; included in Emacs
  28. ;;; Code:
  29. (require 'ob)
  30. (require 'ob-ref)
  31. (require 'ob-comint)
  32. (require 'ob-eval)
  33. (eval-when-compile (require 'cl))
  34. (declare-function run-scheme "ext:cmuscheme" (cmd))
  35. (defvar org-babel-default-header-args:scheme '()
  36. "Default header arguments for scheme code blocks.")
  37. (defvar org-babel-scheme-eoe "org-babel-scheme-eoe"
  38. "String to indicate that evaluation has completed.")
  39. (defcustom org-babel-scheme-cmd "guile"
  40. "Name of command used to evaluate scheme blocks."
  41. :group 'org-babel
  42. :version "24.1"
  43. :type 'string)
  44. (defun org-babel-expand-body:scheme (body params)
  45. "Expand BODY according to PARAMS, return the expanded body."
  46. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  47. (if (> (length vars) 0)
  48. (concat "(let ("
  49. (mapconcat
  50. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  51. vars "\n ")
  52. ")\n" body ")")
  53. body)))
  54. (defvar scheme-program-name)
  55. (defun org-babel-execute:scheme (body params)
  56. "Execute a block of Scheme code with org-babel.
  57. This function is called by `org-babel-execute-src-block'"
  58. (let* ((result-type (cdr (assoc :result-type params)))
  59. (org-babel-scheme-cmd (or (cdr (assoc :scheme params))
  60. org-babel-scheme-cmd))
  61. (full-body (org-babel-expand-body:scheme body params)))
  62. (read
  63. (if (not (string= (cdr (assoc :session params)) "none"))
  64. ;; session evaluation
  65. (let ((session (org-babel-prep-session:scheme
  66. (cdr (assoc :session params)) params)))
  67. (org-babel-comint-with-output
  68. (session (format "%S" org-babel-scheme-eoe) t body)
  69. (mapc
  70. (lambda (line)
  71. (insert (org-babel-chomp line)) (comint-send-input nil t))
  72. (list body (format "%S" org-babel-scheme-eoe)))))
  73. ;; external evaluation
  74. (let ((script-file (org-babel-temp-file "scheme-script-")))
  75. (with-temp-file script-file
  76. (insert
  77. ;; return the value or the output
  78. (if (string= result-type "value")
  79. (format "(display %s)" full-body)
  80. full-body)))
  81. (org-babel-eval
  82. (format "%s %s" org-babel-scheme-cmd
  83. (org-babel-process-file-name script-file)) ""))))))
  84. (defun org-babel-prep-session:scheme (session params)
  85. "Prepare SESSION according to the header arguments specified in PARAMS."
  86. (let* ((session (org-babel-scheme-initiate-session session))
  87. (vars (mapcar #'cdr (org-babel-get-header params :var)))
  88. (var-lines
  89. (mapcar
  90. (lambda (var) (format "%S" (print `(define ,(car var) ',(cdr var)))))
  91. vars)))
  92. (when session
  93. (org-babel-comint-in-buffer session
  94. (sit-for .5) (goto-char (point-max))
  95. (mapc (lambda (var)
  96. (insert var) (comint-send-input nil t)
  97. (org-babel-comint-wait-for-output session)
  98. (sit-for .1) (goto-char (point-max))) var-lines)))
  99. session))
  100. (defun org-babel-scheme-initiate-session (&optional session)
  101. "If there is not a current inferior-process-buffer in SESSION
  102. then create. Return the initialized session."
  103. (require 'cmuscheme)
  104. (unless (string= session "none")
  105. (let ((session-buffer (save-window-excursion
  106. (run-scheme org-babel-scheme-cmd)
  107. (rename-buffer session)
  108. (current-buffer))))
  109. (if (org-babel-comint-buffer-livep session-buffer)
  110. (progn (sit-for .25) session-buffer)
  111. (sit-for .5)
  112. (org-babel-scheme-initiate-session session)))))
  113. (provide 'ob-scheme)
  114. ;;; ob-scheme.el ends here