ob-comint.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. ;;; ob-comint.el --- org-babel functions for interaction with comint buffers
  2. ;; Copyright (C) 2009-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, comint
  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. ;; These functions build on comint to ease the sending and receiving
  19. ;; of commands and results from comint buffers.
  20. ;; Note that the buffers in this file are analogous to sessions in
  21. ;; org-babel at large.
  22. ;;; Code:
  23. (require 'ob)
  24. (require 'comint)
  25. (eval-when-compile (require 'cl))
  26. (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
  27. (declare-function tramp-flush-directory-property "tramp" (vec directory))
  28. (defun org-babel-comint-buffer-livep (buffer)
  29. "Check if BUFFER is a comint buffer with a live process."
  30. (let ((buffer (if buffer (get-buffer buffer))))
  31. (and buffer (buffer-live-p buffer) (get-buffer-process buffer) buffer)))
  32. (defmacro org-babel-comint-in-buffer (buffer &rest body)
  33. "Check BUFFER and execute BODY.
  34. BUFFER is checked with `org-babel-comint-buffer-livep'. BODY is
  35. executed inside the protection of `save-excursion' and
  36. `save-match-data'."
  37. (declare (indent 1))
  38. `(save-excursion
  39. (save-match-data
  40. (unless (org-babel-comint-buffer-livep ,buffer)
  41. (error "buffer %s doesn't exist or has no process" ,buffer))
  42. (set-buffer ,buffer)
  43. ,@body)))
  44. (def-edebug-spec org-babel-comint-in-buffer (form body))
  45. (defmacro org-babel-comint-with-output (meta &rest body)
  46. "Evaluate BODY in BUFFER and return process output.
  47. Will wait until EOE-INDICATOR appears in the output, then return
  48. all process output. If REMOVE-ECHO and FULL-BODY are present and
  49. non-nil, then strip echo'd body from the returned output. META
  50. should be a list containing the following where the last two
  51. elements are optional.
  52. (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
  53. This macro ensures that the filter is removed in case of an error
  54. or user `keyboard-quit' during execution of body."
  55. (declare (indent 1))
  56. (let ((buffer (car meta))
  57. (eoe-indicator (cadr meta))
  58. (remove-echo (cadr (cdr meta)))
  59. (full-body (cadr (cdr (cdr meta)))))
  60. `(org-babel-comint-in-buffer ,buffer
  61. (let ((string-buffer "") dangling-text raw)
  62. (flet ((my-filt (text)
  63. (setq string-buffer (concat string-buffer text))))
  64. ;; setup filter
  65. (add-hook 'comint-output-filter-functions 'my-filt)
  66. (unwind-protect
  67. (progn
  68. ;; got located, and save dangling text
  69. (goto-char (process-mark (get-buffer-process (current-buffer))))
  70. (let ((start (point))
  71. (end (point-max)))
  72. (setq dangling-text (buffer-substring start end))
  73. (delete-region start end))
  74. ;; pass FULL-BODY to process
  75. ,@body
  76. ;; wait for end-of-evaluation indicator
  77. (while (progn
  78. (goto-char comint-last-input-end)
  79. (not (save-excursion
  80. (and (re-search-forward
  81. (regexp-quote ,eoe-indicator) nil t)
  82. (re-search-forward
  83. comint-prompt-regexp nil t)))))
  84. (accept-process-output (get-buffer-process (current-buffer)))
  85. ;; thought the following this would allow async
  86. ;; background running, but I was wrong...
  87. ;; (run-with-timer .5 .5 'accept-process-output
  88. ;; (get-buffer-process (current-buffer)))
  89. )
  90. ;; replace cut dangling text
  91. (goto-char (process-mark (get-buffer-process (current-buffer))))
  92. (insert dangling-text))
  93. ;; remove filter
  94. (remove-hook 'comint-output-filter-functions 'my-filt)))
  95. ;; remove echo'd FULL-BODY from input
  96. (if (and ,remove-echo ,full-body
  97. (string-match
  98. (replace-regexp-in-string
  99. "\n" "[\r\n]+" (regexp-quote (or ,full-body "")))
  100. string-buffer))
  101. (setq raw (substring string-buffer (match-end 0))))
  102. (split-string string-buffer comint-prompt-regexp)))))
  103. (def-edebug-spec org-babel-comint-with-output (form body))
  104. (defun org-babel-comint-input-command (buffer cmd)
  105. "Pass CMD to BUFFER.
  106. The input will not be echoed."
  107. (org-babel-comint-in-buffer buffer
  108. (goto-char (process-mark (get-buffer-process buffer)))
  109. (insert cmd)
  110. (comint-send-input)
  111. (org-babel-comint-wait-for-output buffer)))
  112. (defun org-babel-comint-wait-for-output (buffer)
  113. "Wait until output arrives from BUFFER.
  114. Note: this is only safe when waiting for the result of a single
  115. statement (not large blocks of code)."
  116. (org-babel-comint-in-buffer buffer
  117. (while (progn
  118. (goto-char comint-last-input-end)
  119. (not (and (re-search-forward comint-prompt-regexp nil t)
  120. (goto-char (match-beginning 0))
  121. (string= (face-name (face-at-point))
  122. "comint-highlight-prompt"))))
  123. (accept-process-output (get-buffer-process buffer)))))
  124. (defun org-babel-comint-eval-invisibly-and-wait-for-file
  125. (buffer file string &optional period)
  126. "Evaluate STRING in BUFFER invisibly.
  127. Don't return until FILE exists. Code in STRING must ensure that
  128. FILE exists at end of evaluation."
  129. (unless (org-babel-comint-buffer-livep buffer)
  130. (error "buffer %s doesn't exist or has no process" buffer))
  131. (if (file-exists-p file) (delete-file file))
  132. (process-send-string
  133. (get-buffer-process buffer)
  134. (if (string-match "\n$" string) string (concat string "\n")))
  135. ;; From Tramp 2.1.19 the following cache flush is not necessary
  136. (if (file-remote-p default-directory)
  137. (let (v)
  138. (with-parsed-tramp-file-name default-directory nil
  139. (tramp-flush-directory-property v ""))))
  140. (while (not (file-exists-p file)) (sit-for (or period 0.25))))
  141. (provide 'ob-comint)
  142. ;;; ob-comint.el ends here