ob-scheme.el 4.7 KB

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