ob-scheme.el 4.8 KB

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