ob-comint.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ;;; ob-comint.el --- Babel Functions for Interaction with Comint Buffers -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2021 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research, comint
  5. ;; Homepage: https://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 <https://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. (defun org-babel-comint-buffer-livep (buffer)
  27. "Check if BUFFER is a comint buffer with a live process."
  28. (let ((buffer (when 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 and execute BODY.
  32. BUFFER is checked with `org-babel-comint-buffer-livep'. BODY is
  33. executed inside the protection of `save-excursion' and
  34. `save-match-data'."
  35. (declare (indent 1) (debug t))
  36. `(progn
  37. (unless (org-babel-comint-buffer-livep ,buffer)
  38. (error "Buffer %s does not exist or has no process" ,buffer))
  39. (save-match-data
  40. (with-current-buffer ,buffer
  41. (save-excursion
  42. (let ((comint-input-filter (lambda (_input) nil)))
  43. ,@body))))))
  44. (defmacro org-babel-comint-with-output (meta &rest body)
  45. "Evaluate BODY in BUFFER and return process output.
  46. Will wait until EOE-INDICATOR appears in the output, then return
  47. all process output. If REMOVE-ECHO and FULL-BODY are present and
  48. non-nil, then strip echo'd body from the returned output. META
  49. should be a list containing the following where the last two
  50. elements are optional.
  51. (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
  52. This macro ensures that the filter is removed in case of an error
  53. or user `keyboard-quit' during execution of body."
  54. (declare (indent 1) (debug (sexp body)))
  55. (let ((buffer (nth 0 meta))
  56. (eoe-indicator (nth 1 meta))
  57. (remove-echo (nth 2 meta))
  58. (full-body (nth 3 meta)))
  59. `(org-babel-comint-in-buffer ,buffer
  60. (let* ((string-buffer "")
  61. (comint-output-filter-functions
  62. (cons (lambda (text) (setq string-buffer (concat string-buffer text)))
  63. comint-output-filter-functions))
  64. dangling-text)
  65. ;; got located, and save dangling text
  66. (goto-char (process-mark (get-buffer-process (current-buffer))))
  67. (let ((start (point))
  68. (end (point-max)))
  69. (setq dangling-text (buffer-substring start end))
  70. (delete-region start end))
  71. ;; pass FULL-BODY to process
  72. ,@body
  73. ;; wait for end-of-evaluation indicator
  74. (while (progn
  75. (goto-char comint-last-input-end)
  76. (not (save-excursion
  77. (and (re-search-forward
  78. (regexp-quote ,eoe-indicator) nil t)
  79. (re-search-forward
  80. comint-prompt-regexp nil t)))))
  81. (accept-process-output (get-buffer-process (current-buffer)))
  82. ;; thought the following this would allow async
  83. ;; background running, but I was wrong...
  84. ;; (run-with-timer .5 .5 'accept-process-output
  85. ;; (get-buffer-process (current-buffer)))
  86. )
  87. ;; replace cut dangling text
  88. (goto-char (process-mark (get-buffer-process (current-buffer))))
  89. (insert dangling-text)
  90. ;; remove echo'd FULL-BODY from input
  91. (when (and ,remove-echo ,full-body
  92. (string-match
  93. (replace-regexp-in-string
  94. "\n" "[\r\n]+" (regexp-quote (or ,full-body "")))
  95. string-buffer))
  96. (setq string-buffer (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.
  100. The input will not be echoed."
  101. (org-babel-comint-in-buffer buffer
  102. (goto-char (process-mark (get-buffer-process buffer)))
  103. (insert cmd)
  104. (comint-send-input)
  105. (org-babel-comint-wait-for-output buffer)))
  106. (defun org-babel-comint-wait-for-output (buffer)
  107. "Wait until output arrives from BUFFER.
  108. Note: this is only safe when waiting for the result of a single
  109. statement (not large blocks of code)."
  110. (org-babel-comint-in-buffer buffer
  111. (while (progn
  112. (goto-char comint-last-input-end)
  113. (not (and (re-search-forward comint-prompt-regexp nil t)
  114. (goto-char (match-beginning 0))
  115. (string= (face-name (face-at-point))
  116. "comint-highlight-prompt"))))
  117. (accept-process-output (get-buffer-process buffer)))))
  118. (defun org-babel-comint-eval-invisibly-and-wait-for-file
  119. (buffer file string &optional period)
  120. "Evaluate STRING in BUFFER invisibly.
  121. Don't return until FILE exists. Code in STRING must ensure that
  122. FILE exists at end of evaluation."
  123. (unless (org-babel-comint-buffer-livep buffer)
  124. (error "Buffer %s does not exist or has no process" buffer))
  125. (when (file-exists-p file) (delete-file file))
  126. (process-send-string
  127. (get-buffer-process buffer)
  128. (if (= (aref string (1- (length string))) ?\n) string (concat string "\n")))
  129. (while (not (file-exists-p file)) (sit-for (or period 0.25))))
  130. (provide 'ob-comint)
  131. ;;; ob-comint.el ends here