ob-comint.el 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. ;;; ob-comint.el --- org-babel functions for interaction with comint buffers
  2. ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, comint
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs 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 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs 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. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; These functions build on comint to ease the sending and receiving
  20. ;; of commands and results from comint buffers.
  21. ;; Note that the buffers in this file are analogous to sessions in
  22. ;; org-babel at large.
  23. ;;; Code:
  24. (require 'ob)
  25. (require 'comint)
  26. (eval-when-compile (require 'cl))
  27. (defun org-babel-comint-buffer-livep (buffer)
  28. "Check if BUFFER is a comint buffer with a live process."
  29. (let ((buffer (if buffer (get-buffer buffer))))
  30. (and buffer (buffer-live-p buffer) (get-buffer-process buffer) buffer)))
  31. (defmacro org-babel-comint-in-buffer (buffer &rest body)
  32. "Check BUFFER and execute BODY.
  33. BUFFER is checked with `org-babel-comint-buffer-livep'. BODY is
  34. executed inside the protection of `save-window-excursion' and
  35. `save-match-data'."
  36. (declare (indent 1))
  37. `(save-excursion
  38. (save-match-data
  39. (unless (org-babel-comint-buffer-livep ,buffer)
  40. (error "buffer %s doesn't exist or has no process" ,buffer))
  41. (set-buffer ,buffer)
  42. ,@body)))
  43. (defmacro org-babel-comint-with-output (meta &rest body)
  44. "Evaluate BODY in BUFFER and return process output.
  45. Will wait until EOE-INDICATOR appears in the output, then return
  46. all process output. If REMOVE-ECHO and FULL-BODY are present and
  47. non-nil, then strip echo'd body from the returned output. META
  48. should be a list containing the following where the last two
  49. elements are optional.
  50. (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
  51. This macro ensures that the filter is removed in case of an error
  52. or user `keyboard-quit' during execution of body."
  53. (declare (indent 1))
  54. (let ((buffer (car meta))
  55. (eoe-indicator (cadr meta))
  56. (remove-echo (cadr (cdr meta)))
  57. (full-body (cadr (cdr (cdr meta)))))
  58. `(org-babel-comint-in-buffer ,buffer
  59. (let ((string-buffer "") dangling-text raw)
  60. (flet ((my-filt (text)
  61. (setq string-buffer (concat string-buffer text))))
  62. ;; setup filter
  63. (add-hook 'comint-output-filter-functions 'my-filt)
  64. (unwind-protect
  65. (progn
  66. ;; got located, and save dangling text
  67. (goto-char (process-mark (get-buffer-process (current-buffer))))
  68. (let ((start (point))
  69. (end (point-max)))
  70. (setq dangling-text (buffer-substring start end))
  71. (delete-region start end))
  72. ;; pass FULL-BODY to process
  73. ,@body
  74. ;; wait for end-of-evaluation indicator
  75. (while (progn
  76. (goto-char comint-last-input-end)
  77. (not (save-excursion
  78. (and (re-search-forward
  79. comint-prompt-regexp nil t)
  80. (re-search-forward
  81. (regexp-quote ,eoe-indicator) nil t)))))
  82. (accept-process-output (get-buffer-process (current-buffer)))
  83. ;; thought the following this would allow async
  84. ;; background running, but I was wrong...
  85. ;; (run-with-timer .5 .5 'accept-process-output
  86. ;; (get-buffer-process (current-buffer)))
  87. )
  88. ;; replace cut dangling text
  89. (goto-char (process-mark (get-buffer-process (current-buffer))))
  90. (insert dangling-text))
  91. ;; remove filter
  92. (remove-hook 'comint-output-filter-functions 'my-filt)))
  93. ;; remove echo'd FULL-BODY from input
  94. (if (and ,remove-echo ,full-body
  95. (string-match
  96. (replace-regexp-in-string
  97. "\n" "[\r\n]+" (regexp-quote (or ,full-body "")))
  98. string-buffer))
  99. (setq raw (substring string-buffer (match-end 0))))
  100. (split-string string-buffer comint-prompt-regexp)))))
  101. (defun org-babel-comint-input-command (buffer cmd)
  102. "Pass CMD to BUFFER.
  103. The input will not be echoed."
  104. (org-babel-comint-in-buffer buffer
  105. (goto-char (process-mark (get-buffer-process buffer)))
  106. (insert cmd)
  107. (comint-send-input)
  108. (org-babel-comint-wait-for-output buffer)))
  109. (defun org-babel-comint-wait-for-output (buffer)
  110. "Wait until output arrives from BUFFER.
  111. Note: this is only safe when waiting for the result of a single
  112. statement (not large blocks of code)."
  113. (org-babel-comint-in-buffer buffer
  114. (while (progn
  115. (goto-char comint-last-input-end)
  116. (not (and (re-search-forward comint-prompt-regexp nil t)
  117. (goto-char (match-beginning 0))
  118. (string= (face-name (face-at-point))
  119. "comint-highlight-prompt"))))
  120. (accept-process-output (get-buffer-process buffer)))))
  121. (provide 'ob-comint)
  122. ;; arch-tag: 9adddce6-0864-4be3-b0b5-6c5157dc7889
  123. ;;; ob-comint.el ends here