org-babel-comint.el 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 exist and currently have a process call
  29. IGNITE from within BUFFER."
  30. (unless (and (buffer-live-p buffer) (get-buffer buffer) (get-buffer-process buffer))
  31. (save-excursion
  32. (get-buffer-create buffer)
  33. (funcall ignite)
  34. (setf buffer (current-buffer))
  35. (org-babel-comint-wait-for-output)
  36. (org-babel-comint-input-command ""))))
  37. (defun org-babel-comint-command-to-string (buffer command)
  38. "Send COMMAND to BUFFER's process, and return the results as a string."
  39. (org-babel-comint-input-command buffer command)
  40. (org-babel-comint-last-output buffer))
  41. (defun org-babel-comint-input-command (buffer command)
  42. "Pass COMMAND to the process running in BUFFER."
  43. (save-excursion
  44. (save-match-data
  45. (set-buffer buffer)
  46. (goto-char (process-mark (get-buffer-process (current-buffer))))
  47. (insert command)
  48. (comint-send-input)
  49. (org-babel-comint-wait-for-output))))
  50. (defun org-babel-comint-wait-for-output (buffer)
  51. "Wait until output arrives"
  52. (save-excursion
  53. (save-match-data
  54. (set-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 (current-buffer)))))))
  59. (defun org-babel-comint-last-output (buffer)
  60. "Return BUFFER's the last output as a string"
  61. (save-excursion
  62. (save-match-data
  63. (set-buffer buffer)
  64. (goto-char (process-mark (get-buffer-process (current-buffer))))
  65. (forward-line 0)
  66. (let ((raw (buffer-substring comint-last-input-end (- (point) 1)))
  67. output output-flag)
  68. (mapconcat
  69. (lambda (el)
  70. (if (stringp el)
  71. (format "%s" el)
  72. (format "%S" el)))
  73. (delq nil
  74. (mapcar
  75. (lambda (line)
  76. (unless (string-match "^>" line)
  77. (and (string-match "\\[[[:digit:]]+\\] *\\(.*\\)$" line)
  78. (match-string 1 line))))
  79. ;; drop first, because it's the last line of input
  80. (cdr (split-string raw "[\n\r]")))) "\n")))))
  81. ;;; org-babel-comint.el ends here