org-babel-comint.el 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. (defvar org-babel-comint-buffer nil
  32. "the buffer currently in use")
  33. (defun org-babel-comint-set-buffer (buffer)
  34. (setq org-babel-comint-buffer buffer))
  35. (defun org-babel-ensure-buffer-livep ()
  36. (unless (and (buffer-live-p org-babel-comint-buffer) (get-buffer org-babel-comint-buffer) (get-buffer-process org-babel-comint-buffer))
  37. (error "`org-babel-comint-buffer' doesn't exist or has no process")))
  38. (defmacro org-babel-comint-in-buffer (&rest body)
  39. `(save-window-excursion
  40. (save-match-data
  41. (org-babel-ensure-buffer-livep)
  42. (set-buffer org-babel-comint-buffer)
  43. ,@body)))
  44. (defun org-babel-comint-wait-for-output ()
  45. "Wait until output arrives"
  46. (org-babel-comint-in-buffer
  47. (while (progn
  48. (goto-char comint-last-input-end)
  49. (not (re-search-forward comint-prompt-regexp nil t)))
  50. (accept-process-output (get-buffer-process org-babel-comint-buffer)))))
  51. (defun org-babel-comint-input-command (cmd)
  52. "Pass CMD to `org-babel-comint-buffer'"
  53. (org-babel-comint-in-buffer
  54. (goto-char (process-mark (get-buffer-process org-babel-comint-buffer)))
  55. (insert cmd)
  56. (comint-send-input)
  57. (org-babel-comint-wait-for-output)))
  58. (defun org-babel-comint-command-to-string (buffer cmd)
  59. (let ((buffer (org-babel-comint-buffer-buffer buffer)))
  60. (org-babel-comint-input-command buffer cmd)
  61. (org-babel-comint-last-value buffer)))
  62. (defun org-babel-comint-last-value ()
  63. "Return the last value passed to BUFFER"
  64. (org-babel-comint-in-buffer
  65. (goto-char (process-mark (get-buffer-process org-babel-comint-buffer)))
  66. (forward-line 0)
  67. (org-babel-clean-text-properties (buffer-substring comint-last-input-end (- (point) 1)))))
  68. ;;; debugging functions
  69. (defun org-babel-comint-pmark ()
  70. (org-babel-comint-in-buffer (comint-goto-process-mark) (point)))
  71. ;;; org-babel-comint.el ends here