ob-scheme.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. ;;; ob-scheme.el --- org-babel functions for Scheme
  2. ;; Copyright (C) 2010-2012 Free Software Foundation
  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. :type 'string)
  43. (defun org-babel-expand-body:scheme (body params)
  44. "Expand BODY according to PARAMS, return the expanded body."
  45. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  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 scheme-program-name)
  54. (defun org-babel-execute:scheme (body params)
  55. "Execute a block of Scheme code with org-babel.
  56. This function is called by `org-babel-execute-src-block'"
  57. (let* ((result-type (cdr (assoc :result-type params)))
  58. (org-babel-scheme-cmd (or (cdr (assoc :scheme params))
  59. org-babel-scheme-cmd))
  60. (full-body (org-babel-expand-body:scheme body params)))
  61. (read
  62. (if (not (string= (cdr (assoc :session params)) "none"))
  63. ;; session evaluation
  64. (let ((session (org-babel-prep-session:scheme
  65. (cdr (assoc :session params)) params)))
  66. (org-babel-comint-with-output
  67. (session (format "%S" org-babel-scheme-eoe) t body)
  68. (mapc
  69. (lambda (line)
  70. (insert (org-babel-chomp line)) (comint-send-input nil t))
  71. (list body (format "%S" org-babel-scheme-eoe)))))
  72. ;; external evaluation
  73. (let ((script-file (org-babel-temp-file "scheme-script-")))
  74. (with-temp-file script-file
  75. (insert
  76. ;; return the value or the output
  77. (if (string= result-type "value")
  78. (format "(display %s)" full-body)
  79. full-body)))
  80. (org-babel-eval
  81. (format "%s %s" org-babel-scheme-cmd
  82. (org-babel-process-file-name script-file)) ""))))))
  83. (defun org-babel-prep-session:scheme (session params)
  84. "Prepare SESSION according to the header arguments specified in PARAMS."
  85. (let* ((session (org-babel-scheme-initiate-session session))
  86. (vars (mapcar #'cdr (org-babel-get-header params :var)))
  87. (var-lines
  88. (mapcar
  89. (lambda (var) (format "%S" (print `(define ,(car var) ',(cdr var)))))
  90. vars)))
  91. (when session
  92. (org-babel-comint-in-buffer session
  93. (sit-for .5) (goto-char (point-max))
  94. (mapc (lambda (var)
  95. (insert var) (comint-send-input nil t)
  96. (org-babel-comint-wait-for-output session)
  97. (sit-for .1) (goto-char (point-max))) var-lines)))
  98. session))
  99. (defun org-babel-scheme-initiate-session (&optional session)
  100. "If there is not a current inferior-process-buffer in SESSION
  101. then create. Return the initialized session."
  102. (require 'cmuscheme)
  103. (unless (string= session "none")
  104. (let ((session-buffer (save-window-excursion
  105. (run-scheme org-babel-scheme-cmd)
  106. (rename-buffer session)
  107. (current-buffer))))
  108. (if (org-babel-comint-buffer-livep session-buffer)
  109. (progn (sit-for .25) session-buffer)
  110. (sit-for .5)
  111. (org-babel-scheme-initiate-session session)))))
  112. (provide 'ob-scheme)
  113. ;;; ob-scheme.el ends here