org-babel-comint.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-output-buffer nil
  32. "this is a string to buffer output, it should be set buffer local")
  33. (defvar org-babel-comint-output-ring nil
  34. "ring to hold comint output")
  35. (defvar org-babel-comint-output-ring-size 10
  36. "number of output to be help")
  37. (defun org-babel-comint-init (buffer)
  38. "Initialize a buffer to use org-babel-comint."
  39. (save-excursion
  40. (set-buffer buffer)
  41. (set (make-local-variable 'org-babel-comint-output-buffer) "")
  42. ))
  43. (defun org-babel-comint-buffer-livep (buffer)
  44. (and (buffer-live-p buffer) (get-buffer buffer) (get-buffer-process buffer)))
  45. (defmacro org-babel-comint-in-buffer (buffer &rest body)
  46. `(save-window-excursion
  47. (save-match-data
  48. (unless (org-babel-comint-buffer-livep buffer)
  49. (error (format "buffer %s doesn't exist or has no process" buffer)))
  50. (set-buffer buffer)
  51. ,@body)))
  52. (defun org-babel-comint-wait-for-output (buffer)
  53. "Wait until output arrives"
  54. (org-babel-comint-in-buffer buffer
  55. (while (progn
  56. (goto-char comint-last-input-end)
  57. (not (re-search-forward comint-prompt-regexp nil t)))
  58. (accept-process-output (get-buffer-process buffer)))))
  59. (defun org-babel-comint-input-command (buffer cmd)
  60. "Pass CMD to BUFFER The input will not be echoed."
  61. (org-babel-comint-in-buffer buffer
  62. (goto-char (process-mark (get-buffer-process buffer)))
  63. (insert cmd)
  64. (comint-send-input)
  65. (org-babel-comint-wait-for-output buffer)))
  66. (defun org-babel-comint-command-to-output (buffer cmd)
  67. "Pass CMD to BUFFER using `org-babel-comint-input-command', and
  68. then return the result as a string using
  69. `org-babel-comint-last-value'."
  70. (org-babel-comint-input-command buffer cmd)
  71. (org-babel-comint-last-value buffer))
  72. (defun org-babel-comint-command-to-last (buffer cmd)
  73. "Pass CMD to BUFFER using `org-babel-comint-input-command', and
  74. then return the result as a string using
  75. `org-babel-comint-last-value'."
  76. (org-babel-comint-input-command buffer cmd)
  77. (org-babel-comint-last-value buffer))
  78. (defun org-babel-comint-last-value (buffer)
  79. "Return the last comint output in BUFFER as a string."
  80. (org-babel-comint-in-buffer buffer
  81. (goto-char (process-mark (get-buffer-process buffer)))
  82. (forward-line 0)
  83. (org-babel-clean-text-properties
  84. (buffer-substring (+ comint-last-input-end
  85. ;; because comint insists on echoing input
  86. (- comint-last-input-end
  87. comint-last-input-start))
  88. (- (point) 1)))))
  89. ;; output filter
  90. ;;
  91. ;; This will collect output, stripping away echo'd inputs, splitting
  92. ;; it by `comint-prompt-regexp', then sticking it into the
  93. ;; `org-babel-comint-output-ring'.
  94. (defun org-babel-comint-hook ()
  95. (set (make-local-variable 'org-babel-comint-output-buffer) "")
  96. (set (make-local-variable 'org-babel-comint-output-ring) (make-ring 10)))
  97. (add-hook 'comint-mode-hook 'org-babel-comint-hook)
  98. (defun org-babel-comint-output-filter (text)
  99. "Filter the text of org-babel-comint"
  100. (setq org-babel-comint-output-buffer (concat org-babel-comint-output-buffer text))
  101. (let ((holder (split-string org-babel-comint-output-buffer comint-prompt-regexp)))
  102. (when (> (length holder) 1)
  103. (mapc (lambda (output) (ring-insert org-babel-comint-output-ring (org-babel-chomp output)))
  104. (butlast holder))
  105. (setq org-babel-comint-output-buffer (or (cdr (last holder)) "")))))
  106. (add-hook 'comint-output-filter-functions 'org-babel-comint-output-filter)
  107. ;; debugging functions
  108. (defun org-babel-show-output-buffer ()
  109. (interactive)
  110. (message org-babel-comint-output-buffer))
  111. (defun org-babel-show-output-ring-size ()
  112. (interactive)
  113. (message (format "ring is %d" (ring-size org-babel-comint-output-ring))))
  114. (defun org-babel-show-ring ()
  115. (interactive)
  116. (message (format "%S" (ring-elements org-babel-comint-output-ring))))
  117. (provide 'org-babel-comint)
  118. ;;; org-babel-comint.el ends here