org-babel-comint.el 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. ;; This file should provide support for passing commands and results
  24. ;; to and from `comint-mode' buffers.
  25. ;;; Code:
  26. (require 'org-babel)
  27. (defun org-babel-comint-initiate-buffer (buffer ignite)
  28. "If BUFFER does not currently have a process use IGNITE to
  29. create one."
  30. (unless (and (buffer-live-p buffer) (get-buffer buffer))
  31. (save-excursion
  32. (eval ignite)
  33. (setf buffer (current-buffer))
  34. (org-babel-comint-wait-for-output)
  35. (org-babel-comint-input-command ""))))
  36. (defun org-babel-comint-command-to-string (buffer command)
  37. "Send COMMEND to BUFFER's process, and return the results as a string."
  38. (org-babel-comint-input-command buffer command)
  39. (org-babel-comint-last-output buffer))
  40. (defun org-babel-comint-input-command (buffer command)
  41. "Pass COMMAND to the process running in BUFFER."
  42. (save-excursion
  43. (save-match-data
  44. (set-buffer buffer)
  45. (goto-char (process-mark (get-buffer-process (current-buffer))))
  46. (insert command)
  47. (comint-send-input)
  48. (org-babel-comint-wait-for-output))))
  49. (defun org-babel-comint-wait-for-output (buffer)
  50. "Wait until output arrives"
  51. (save-excursion
  52. (save-match-data
  53. (set-buffer buffer)
  54. (while (progn
  55. (goto-char comint-last-input-end)
  56. (not (re-search-forward comint-prompt-regexp nil t)))
  57. (accept-process-output (get-buffer-process (current-buffer)))))))
  58. (defun org-babel-comint-last-output (buffer)
  59. "Return BUFFER's the last output as a string"
  60. (save-excursion
  61. (save-match-data
  62. (set-buffer buffer)
  63. (goto-char (process-mark (get-buffer-process (current-buffer))))
  64. (forward-line 0)
  65. (let ((raw (buffer-substring comint-last-input-end (- (point) 1)))
  66. output output-flag)
  67. (mapconcat
  68. (lambda (el)
  69. (if (stringp el)
  70. (format "%s" el)
  71. (format "%S" el)))
  72. (delq nil
  73. (mapcar
  74. (lambda (line)
  75. (unless (string-match "^>" line)
  76. (and (string-match "\\[[[:digit:]]+\\] *\\(.*\\)$" line)
  77. (match-string 1 line))))
  78. ;; drop first, because it's the last line of input
  79. (cdr (split-string raw "[\n\r]")))) "\n")))))
  80. ;;; org-babel-comint.el ends here