org-babel-comint.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. (and (buffer-live-p buffer) (get-buffer buffer) (get-buffer-process buffer)))
  33. (defmacro org-babel-comint-in-buffer (buffer &rest body)
  34. "Check BUFFER with `org-babel-comint-buffer-livep' then execute
  35. body inside the protection of `save-window-excursion' and
  36. `save-match-data'."
  37. (declare (indent 1))
  38. `(save-window-excursion
  39. (save-match-data
  40. (unless (org-babel-comint-buffer-livep buffer)
  41. (error (format "buffer %s doesn't exist or has no process" buffer)))
  42. (set-buffer buffer)
  43. ,@body)))
  44. (defun org-babel-comint-append-output-filter (text)
  45. (setq string-buffer (concat string-buffer text)))
  46. (defmacro org-babel-comint-with-output (&rest body)
  47. `(let ((string-buffer ""))
  48. (add-hook 'comint-output-filter-functions 'org-babel-comint-append-output-filter)
  49. (condition-case nil (progn ,@body) (t))
  50. (remove-hook 'comint-output-filter-functions 'org-babel-comint-append-output-filter)
  51. string-buffer))
  52. (defun org-babel-comint-command-to-output (buffer cmd)
  53. "Pass CMD to BUFFER using `org-babel-comint-input-command', and
  54. then return the a list of the output(s) generated by CMD."
  55. (let ((raw (org-babel-comint-with-output
  56. (org-babel-comint-input-command buffer cmd))))
  57. (mapcar #'org-babel-chomp
  58. ;; split the output along prompts
  59. (split-string
  60. ;; remove CMD if it is at the front of the output
  61. (if (string= cmd (substring raw 0 (length cmd)))
  62. (substring raw (length cmd))
  63. raw)
  64. comint-prompt-regexp))))
  65. (defun org-babel-comint-input-command (buffer cmd)
  66. "Pass CMD to BUFFER The input will not be echoed."
  67. (org-babel-comint-in-buffer buffer
  68. (goto-char (process-mark (get-buffer-process buffer)))
  69. (insert cmd)
  70. (comint-send-input)
  71. (org-babel-comint-wait-for-output buffer)))
  72. (defun org-babel-comint-wait-for-output (buffer)
  73. "Wait until output arrives"
  74. (org-babel-comint-in-buffer buffer
  75. (while (progn
  76. (goto-char comint-last-input-end)
  77. (not (and (re-search-forward comint-prompt-regexp nil t)
  78. (goto-char (match-beginning 0))
  79. (string= (face-name (face-at-point))
  80. "comint-highlight-prompt"))))
  81. (accept-process-output (get-buffer-process buffer)))))
  82. (defun org-babel-comint-command-to-last (buffer cmd)
  83. "Pass CMD to BUFFER using `org-babel-comint-input-command', and
  84. then return the result as a string using
  85. `org-babel-comint-last-value'."
  86. (org-babel-comint-input-command buffer cmd)
  87. (org-babel-comint-last-value buffer))
  88. (defun org-babel-comint-last-value (buffer)
  89. "Return the last comint output in BUFFER as a string."
  90. (org-babel-comint-in-buffer buffer
  91. (goto-char (process-mark (get-buffer-process buffer)))
  92. (forward-line 0)
  93. (org-babel-clean-text-properties
  94. (buffer-substring (+ comint-last-input-end
  95. ;; because comint insists on echoing input
  96. (- comint-last-input-end
  97. comint-last-input-start))
  98. (- (point) 1)))))
  99. (provide 'org-babel-comint)
  100. ;;; org-babel-comint.el ends here