ob-comint.el 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. ;;; ob-comint.el --- Babel Functions for Interaction with Comint Buffers -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2017 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-core)
  24. (require 'org-compat)
  25. (require 'comint)
  26. (require 'tramp)
  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-excursion' and
  35. `save-match-data'."
  36. (declare (indent 1))
  37. `(progn
  38. (unless (org-babel-comint-buffer-livep ,buffer)
  39. (error "Buffer %s does not exist or has no process" ,buffer))
  40. (save-match-data
  41. (with-current-buffer ,buffer
  42. (save-excursion
  43. (let ((comint-input-filter (lambda (_input) nil)))
  44. ,@body))))))
  45. (def-edebug-spec org-babel-comint-in-buffer (form body))
  46. (defmacro org-babel-comint-with-output (meta &rest body)
  47. "Evaluate BODY in BUFFER and return process output.
  48. Will wait until EOE-INDICATOR appears in the output, then return
  49. all process output. If REMOVE-ECHO and FULL-BODY are present and
  50. non-nil, then strip echo'd body from the returned output. META
  51. should be a list containing the following where the last two
  52. elements are optional.
  53. (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
  54. This macro ensures that the filter is removed in case of an error
  55. or user `keyboard-quit' during execution of body."
  56. (declare (indent 1))
  57. (let ((buffer (nth 0 meta))
  58. (eoe-indicator (nth 1 meta))
  59. (remove-echo (nth 2 meta))
  60. (full-body (nth 3 meta)))
  61. `(org-babel-comint-in-buffer ,buffer
  62. (let* ((string-buffer "")
  63. (comint-output-filter-functions
  64. (cons (lambda (text) (setq string-buffer (concat string-buffer text)))
  65. comint-output-filter-functions))
  66. dangling-text)
  67. ;; got located, and save dangling text
  68. (goto-char (process-mark (get-buffer-process (current-buffer))))
  69. (let ((start (point))
  70. (end (point-max)))
  71. (setq dangling-text (buffer-substring start end))
  72. (delete-region start end))
  73. ;; pass FULL-BODY to process
  74. ,@body
  75. ;; wait for end-of-evaluation indicator
  76. (while (progn
  77. (goto-char comint-last-input-end)
  78. (not (save-excursion
  79. (and (re-search-forward
  80. (regexp-quote ,eoe-indicator) nil t)
  81. (re-search-forward
  82. comint-prompt-regexp nil t)))))
  83. (accept-process-output (get-buffer-process (current-buffer)))
  84. ;; thought the following this would allow async
  85. ;; background running, but I was wrong...
  86. ;; (run-with-timer .5 .5 'accept-process-output
  87. ;; (get-buffer-process (current-buffer)))
  88. )
  89. ;; replace cut dangling text
  90. (goto-char (process-mark (get-buffer-process (current-buffer))))
  91. (insert dangling-text)
  92. ;; remove echo'd FULL-BODY from input
  93. (when (and ,remove-echo ,full-body
  94. (string-match
  95. (replace-regexp-in-string
  96. "\n" "[\r\n]+" (regexp-quote (or ,full-body "")))
  97. string-buffer))
  98. (setq string-buffer (substring string-buffer (match-end 0))))
  99. (split-string string-buffer comint-prompt-regexp)))))
  100. (def-edebug-spec org-babel-comint-with-output (sexp body))
  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. (defun org-babel-comint-eval-invisibly-and-wait-for-file
  122. (buffer file string &optional period)
  123. "Evaluate STRING in BUFFER invisibly.
  124. Don't return until FILE exists. Code in STRING must ensure that
  125. FILE exists at end of evaluation."
  126. (unless (org-babel-comint-buffer-livep buffer)
  127. (error "Buffer %s does not exist or has no process" buffer))
  128. (when (file-exists-p file) (delete-file file))
  129. (process-send-string
  130. (get-buffer-process buffer)
  131. (if (= (aref string (1- (length string))) ?\n) string (concat string "\n")))
  132. ;; From Tramp 2.1.19 the following cache flush is not necessary
  133. (when (file-remote-p default-directory)
  134. (with-parsed-tramp-file-name default-directory nil
  135. (tramp-flush-directory-property v "")))
  136. (while (not (file-exists-p file)) (sit-for (or period 0.25))))
  137. (provide 'ob-comint)
  138. ;;; ob-comint.el ends here