ob-scheme.el 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. (eval-when-compile (require 'cl))
  31. (declare-function run-scheme "ext:cmuscheme" (cmd))
  32. (defvar org-babel-default-header-args:scheme '()
  33. "Default header arguments for scheme code blocks.")
  34. (defvar org-babel-scheme-eoe "org-babel-scheme-eoe"
  35. "String to indicate that evaluation has completed.")
  36. (defcustom org-babel-scheme-cmd "guile"
  37. "Name of command used to evaluate scheme blocks."
  38. :group 'org-babel
  39. :version "24.1"
  40. :type 'string)
  41. (defun org-babel-expand-body:scheme (body params)
  42. "Expand BODY according to PARAMS, return the expanded body."
  43. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  44. (if (> (length vars) 0)
  45. (concat "(let ("
  46. (mapconcat
  47. (lambda (var) (format "%S" (print `(,(car var) ',(cdr var)))))
  48. vars "\n ")
  49. ")\n" body ")")
  50. body)))
  51. (defvar scheme-program-name)
  52. (defun org-babel-execute:scheme (body params)
  53. "Execute a block of Scheme code with org-babel.
  54. This function is called by `org-babel-execute-src-block'"
  55. (let* ((result-type (cdr (assoc :result-type params)))
  56. (org-babel-scheme-cmd (or (cdr (assoc :scheme params))
  57. org-babel-scheme-cmd))
  58. (full-body (org-babel-expand-body:scheme body params)))
  59. (read
  60. (if (not (string= (cdr (assoc :session params)) "none"))
  61. ;; session evaluation
  62. (let ((session (org-babel-prep-session:scheme
  63. (cdr (assoc :session params)) params)))
  64. (org-babel-comint-with-output
  65. (session (format "%S" org-babel-scheme-eoe) t body)
  66. (mapc
  67. (lambda (line)
  68. (insert (org-babel-chomp line)) (comint-send-input nil t))
  69. (list body (format "%S" org-babel-scheme-eoe)))))
  70. ;; external evaluation
  71. (let ((script-file (org-babel-temp-file "scheme-script-")))
  72. (with-temp-file script-file
  73. (insert
  74. ;; return the value or the output
  75. (if (string= result-type "value")
  76. (format "(display %s)" full-body)
  77. full-body)))
  78. (org-babel-eval
  79. (format "%s %s" org-babel-scheme-cmd
  80. (org-babel-process-file-name script-file)) ""))))))
  81. (defun org-babel-prep-session:scheme (session params)
  82. "Prepare SESSION according to the header arguments specified in PARAMS."
  83. (let* ((session (org-babel-scheme-initiate-session session))
  84. (vars (mapcar #'cdr (org-babel-get-header params :var)))
  85. (var-lines
  86. (mapcar
  87. (lambda (var) (format "%S" (print `(define ,(car var) ',(cdr var)))))
  88. vars)))
  89. (when session
  90. (org-babel-comint-in-buffer session
  91. (sit-for .5) (goto-char (point-max))
  92. (mapc (lambda (var)
  93. (insert var) (comint-send-input nil t)
  94. (org-babel-comint-wait-for-output session)
  95. (sit-for .1) (goto-char (point-max))) var-lines)))
  96. session))
  97. (defun org-babel-scheme-initiate-session (&optional session)
  98. "If there is not a current inferior-process-buffer in SESSION
  99. then create. Return the initialized session."
  100. (require 'cmuscheme)
  101. (unless (string= session "none")
  102. (let ((session-buffer (save-window-excursion
  103. (run-scheme org-babel-scheme-cmd)
  104. (rename-buffer session)
  105. (current-buffer))))
  106. (if (org-babel-comint-buffer-livep session-buffer)
  107. (progn (sit-for .25) session-buffer)
  108. (sit-for .5)
  109. (org-babel-scheme-initiate-session session)))))
  110. (provide 'ob-scheme)
  111. ;;; ob-scheme.el ends here