ob-comint.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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: 0.01
  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. (defun org-babel-comint-buffer-livep (buffer)
  27. "Check if BUFFER is a comint buffer with a live process."
  28. (let ((buffer (if buffer (get-buffer buffer))))
  29. (and buffer (buffer-live-p buffer) (get-buffer-process buffer) buffer)))
  30. (defmacro org-babel-comint-in-buffer (buffer &rest body)
  31. "Check BUFFER with `org-babel-comint-buffer-livep' then execute
  32. body inside the protection of `save-window-excursion' and
  33. `save-match-data'."
  34. (declare (indent 1))
  35. `(save-excursion
  36. (save-match-data
  37. (unless (org-babel-comint-buffer-livep ,buffer)
  38. (error (format "buffer %s doesn't exist or has no process" ,buffer)))
  39. (set-buffer ,buffer)
  40. ,@body)))
  41. (defmacro org-babel-comint-with-output (meta &rest body)
  42. "Evaluate BODY in BUFFER, wait until EOE-INDICATOR appears in
  43. output, then return all process output. If REMOVE-ECHO and
  44. FULL-BODY are present and non-nil, then strip echo'd body from
  45. the returned output. META should be a list containing the
  46. following where the last two elements are optional.
  47. (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
  48. This macro ensures that the filter is removed in case of an error
  49. or user `keyboard-quit' during execution of body."
  50. (declare (indent 1))
  51. (let ((buffer (car meta))
  52. (eoe-indicator (cadr meta))
  53. (remove-echo (cadr (cdr meta)))
  54. (full-body (cadr (cdr (cdr meta)))))
  55. `(org-babel-comint-in-buffer ,buffer
  56. (let ((string-buffer "") dangling-text)
  57. (flet ((my-filt (text)
  58. (setq string-buffer (concat string-buffer text))))
  59. ;; setup filter
  60. (add-hook 'comint-output-filter-functions 'my-filt)
  61. (unwind-protect
  62. (progn
  63. ;; got located, and save dangling text
  64. (goto-char (process-mark (get-buffer-process (current-buffer))))
  65. (let ((start (point))
  66. (end (point-max)))
  67. (setq dangling-text (buffer-substring start end))
  68. (delete-region start end))
  69. ;; pass FULL-BODY to process
  70. ,@body
  71. ;; wait for end-of-evaluation indicator
  72. (while (progn
  73. (goto-char comint-last-input-end)
  74. (not (save-excursion
  75. (and (re-search-forward
  76. comint-prompt-regexp nil t)
  77. (re-search-forward
  78. (regexp-quote ,eoe-indicator) nil t)))))
  79. (accept-process-output (get-buffer-process (current-buffer)))
  80. ;; thought the following this would allow async
  81. ;; background running, but I was wrong...
  82. ;; (run-with-timer .5 .5 'accept-process-output
  83. ;; (get-buffer-process (current-buffer)))
  84. )
  85. ;; replace cut dangling text
  86. (goto-char (process-mark (get-buffer-process (current-buffer))))
  87. (insert dangling-text))
  88. ;; remove filter
  89. (remove-hook 'comint-output-filter-functions 'my-filt)))
  90. ;; remove echo'd FULL-BODY from input
  91. (if (and ,remove-echo ,full-body
  92. (string-match
  93. (replace-regexp-in-string
  94. "\n" "[\r\n]+" (regexp-quote ,full-body))
  95. string-buffer))
  96. (setq raw (substring string-buffer (match-end 0))))
  97. (split-string string-buffer comint-prompt-regexp)))))
  98. (defun org-babel-comint-input-command (buffer cmd)
  99. "Pass CMD to BUFFER The input will not be echoed."
  100. (org-babel-comint-in-buffer buffer
  101. (goto-char (process-mark (get-buffer-process buffer)))
  102. (insert cmd)
  103. (comint-send-input)
  104. (org-babel-comint-wait-for-output buffer)))
  105. (defun org-babel-comint-wait-for-output (buffer)
  106. "Wait until output arrives from BUFFER. Note: this is only
  107. safe when waiting for the result of a single statement (not large
  108. blocks of code)."
  109. (org-babel-comint-in-buffer buffer
  110. (while (progn
  111. (goto-char comint-last-input-end)
  112. (not (and (re-search-forward comint-prompt-regexp nil t)
  113. (goto-char (match-beginning 0))
  114. (string= (face-name (face-at-point))
  115. "comint-highlight-prompt"))))
  116. (accept-process-output (get-buffer-process buffer)))))
  117. (provide 'ob-comint)
  118. ;; arch-tag: 9adddce6-0864-4be3-b0b5-6c5157dc7889
  119. ;;; ob-comint.el ends here