org-babel-comint.el 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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-window-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. (defun org-babel-comint-input-command (buffer cmd)
  46. "Pass CMD to BUFFER The input will not be echoed."
  47. (org-babel-comint-in-buffer buffer
  48. (goto-char (process-mark (get-buffer-process buffer)))
  49. (insert cmd)
  50. (comint-send-input)
  51. (org-babel-comint-wait-for-output buffer)))
  52. (defun org-babel-comint-wait-for-output (buffer)
  53. "Wait until output arrives. Note: this is only safe when
  54. waiting for the result of a single statement (not large blocks of
  55. code)."
  56. (org-babel-comint-in-buffer buffer
  57. (while (progn
  58. (goto-char comint-last-input-end)
  59. (not (and (re-search-forward comint-prompt-regexp nil t)
  60. (goto-char (match-beginning 0))
  61. (string= (face-name (face-at-point))
  62. "comint-highlight-prompt"))))
  63. (accept-process-output (get-buffer-process buffer)))))
  64. (provide 'org-babel-comint)
  65. ;;; org-babel-comint.el ends here