org-babel-comint.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;;; org-babel-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 'org-babel)
  30. (require 'comint)
  31. (defun org-babel-comint-buffer-livep (buffer)
  32. (let ((buffer (if buffer (get-buffer buffer))))
  33. (and buffer (buffer-live-p buffer) (get-buffer-process buffer) buffer)))
  34. (defmacro org-babel-comint-in-buffer (buffer &rest body)
  35. "Check BUFFER with `org-babel-comint-buffer-livep' then execute
  36. body inside the protection of `save-window-excursion' and
  37. `save-match-data'."
  38. (declare (indent 1))
  39. `(save-excursion
  40. (save-match-data
  41. (unless (org-babel-comint-buffer-livep ,buffer)
  42. (error (format "buffer %s doesn't exist or has no process" ,buffer)))
  43. (set-buffer ,buffer)
  44. ,@body)))
  45. (defmacro org-babel-comint-with-output (buffer eoe-indicator remove-echo &rest body)
  46. "Evaluate BODY in BUFFER, wait until EOE-INDICATOR appears in
  47. output, then return all process output. This ensures that the
  48. filter is removed in case of an error or user `keyboard-quit'
  49. during execution of body."
  50. (declare (indent 3))
  51. `(org-babel-comint-in-buffer ,buffer
  52. (let ((string-buffer ""))
  53. (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
  54. ;; setup filter
  55. (add-hook 'comint-output-filter-functions 'my-filt)
  56. (unwind-protect
  57. (progn
  58. ;; pass FULL-BODY to process
  59. (goto-char (process-mark (get-buffer-process (current-buffer))))
  60. ,@body
  61. ;; wait for end-of-evaluation indicator
  62. (while (progn
  63. (goto-char comint-last-input-end)
  64. (not (save-excursion
  65. (and (re-search-forward comint-prompt-regexp nil t)
  66. (re-search-forward (regexp-quote ,eoe-indicator) nil t)))))
  67. (accept-process-output (get-buffer-process (current-buffer)))
  68. ;; ;; thought this would allow async background running, but I was wrong...
  69. ;; (run-with-timer .5 .5 'accept-process-output (get-buffer-process (current-buffer)))
  70. ))
  71. ;; remove filter
  72. (remove-hook 'comint-output-filter-functions 'my-filt)))
  73. ;; remove echo'd FULL-BODY from input
  74. (if (and ,remove-echo
  75. (string-match
  76. (replace-regexp-in-string "\n" "\r\n" (regexp-quote ,full-body)) string-buffer))
  77. (setq raw (substring string-buffer (match-end 0))))
  78. (split-string string-buffer comint-prompt-regexp))))
  79. (defun org-babel-comint-input-command (buffer cmd)
  80. "Pass CMD to BUFFER The input will not be echoed."
  81. (org-babel-comint-in-buffer buffer
  82. (goto-char (process-mark (get-buffer-process buffer)))
  83. (insert cmd)
  84. (comint-send-input)
  85. (org-babel-comint-wait-for-output buffer)))
  86. (defun org-babel-comint-wait-for-output (buffer)
  87. "Wait until output arrives. Note: this is only safe when
  88. waiting for the result of a single statement (not large blocks of
  89. code)."
  90. (org-babel-comint-in-buffer buffer
  91. (while (progn
  92. (goto-char comint-last-input-end)
  93. (not (and (re-search-forward comint-prompt-regexp nil t)
  94. (goto-char (match-beginning 0))
  95. (string= (face-name (face-at-point))
  96. "comint-highlight-prompt"))))
  97. (accept-process-output (get-buffer-process buffer)))))
  98. (provide 'org-babel-comint)
  99. ;;; org-babel-comint.el ends here