ob-comint.el 5.1 KB

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