Browse Source

extracted the "capture all output from comint session" behavior into a macro

Eric Schulte 16 years ago
parent
commit
dd0392a4f2
4 changed files with 71 additions and 63 deletions
  1. 13 33
      lisp/langs/org-babel-ruby.el
  2. 25 0
      lisp/org-babel-comint.el
  3. 20 17
      lisp/org-babel.el
  4. 13 13
      org-babel.org

+ 13 - 33
lisp/langs/org-babel-ruby.el

@@ -97,39 +97,19 @@ then create.  Return the initialized session."
 'output then return a list of the outputs of the statements in
 BODY, if RESULT-TYPE equals 'value then return the value of the
 last statement in BODY."
-  (org-babel-comint-in-buffer buffer
-    (let ((string-buffer "")
-          (full-body (mapconcat #'org-babel-chomp
-                                (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
-          results)
-      (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
-        ;; setup filter
-        (add-hook 'comint-output-filter-functions 'my-filt)
-        ;; pass FULL-BODY to process
-        (goto-char (process-mark (get-buffer-process buffer)))
-        (insert full-body)
-        (comint-send-input)
-        ;; wait for end-of-evaluation indicator
-        (while (progn
-                 (goto-char comint-last-input-end)
-                 (not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
-                                           (re-search-forward (regexp-quote org-babel-ruby-eoe-indicator) nil t)))))
-          (accept-process-output (get-buffer-process buffer)))
-        ;; remove filter
-        (remove-hook 'comint-output-filter-functions 'my-filt))
-      ;; remove echo'd FULL-BODY from input
-      (if (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote full-body)) string-buffer)
-          (setq string-buffer (substring string-buffer (match-end 0))))
-      ;; split results with `comint-prompt-regexp'
-      (setq results (cdr (member org-babel-ruby-eoe-indicator
-                                 (reverse (mapcar #'org-babel-ruby-read-string
-                                                  (mapcar #'org-babel-trim
-                                                          (split-string string-buffer comint-prompt-regexp)))))))
-      (message "near-final=%S" results)
-      (case result-type
-        (output (mapconcat #'identity (reverse (cdr results)) "\n"))
-        (value (car results))
-        (t (reverse results))))))
+  (let* ((full-body (mapconcat #'org-babel-chomp
+                               (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
+         (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
+                (insert full-body) (comint-send-input)))
+         results)
+    ;; split results with `comint-prompt-regexp'
+    (setq results (cdr (member org-babel-ruby-eoe-indicator
+                               (reverse (mapcar #'org-babel-ruby-read-string
+                                                (mapcar #'org-babel-trim raw))))))
+    (case result-type
+      (output (mapconcat #'identity (reverse (cdr results)) "\n"))
+      (value (car results))
+      (t (reverse results)))))
 
 (defun org-babel-ruby-read-string (string)
   "Strip \\\"s from around ruby string"

+ 25 - 0
lisp/org-babel-comint.el

@@ -52,6 +52,31 @@ body inside the protection of `save-window-excursion' and
        (set-buffer buffer)
        ,@body)))
 
+(defmacro org-babel-comint-with-output (buffer eoe-indicator remove-echo &rest body)
+  "Evaluate BODY in BUFFER, wait until EOE-INDICATOR appears in
+output, then return all process output."
+  (declare (indent 3))
+  `(org-babel-comint-in-buffer buffer
+     (let ((string-buffer ""))
+       (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
+         ;; setup filter
+         (add-hook 'comint-output-filter-functions 'my-filt)
+         ;; pass FULL-BODY to process
+         (goto-char (process-mark (get-buffer-process (current-buffer))))
+         (progn ,@body)
+         ;; wait for end-of-evaluation indicator
+         (while (progn
+                  (goto-char comint-last-input-end)
+                  (not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
+                                            (re-search-forward (regexp-quote ,eoe-indicator) nil t)))))
+           (accept-process-output (get-buffer-process (current-buffer))))
+         ;; remove filter
+         (remove-hook 'comint-output-filter-functions 'my-filt))
+       ;; remove echo'd FULL-BODY from input
+       (if (and ,remove-echo (string-match (replace-regexp-in-string "\n" "\r\n" (regexp-quote ,full-body)) string-buffer))
+           (setq raw (substring string-buffer (match-end 0))))
+       (split-string string-buffer comint-prompt-regexp))))
+
 (defun org-babel-comint-input-command (buffer cmd)
   "Pass CMD to BUFFER  The input will not be echoed."
   (org-babel-comint-in-buffer buffer

+ 20 - 17
lisp/org-babel.el

@@ -299,16 +299,19 @@ relies on `org-babel-insert-result'."
   (interactive)
   (save-excursion
     (goto-char (org-babel-where-is-src-block-result)) (forward-line 1)
-    (delete-region (point)
-                   (save-excursion
-                     (if (org-at-table-p)
-                         (org-table-end)
-                       (while (if (looking-at "\\(: \\|\\[\\[\\)")
-                                  (progn (while (looking-at "\\(: \\|\\[\\[\\)")
-                                           (forward-line 1)) t))
-                         (forward-line 1))
-                       (forward-line -1)
-                       (point))))))
+    (delete-region (point) (org-babel-result-end))))
+
+(defun org-babel-result-end ()
+  "Return the point at the end of the current set of results"
+  (save-excursion
+    (if (org-at-table-p)
+        (org-table-end)
+      (while (if (looking-at "\\(: \\|\\[\\[\\)")
+                 (progn (while (looking-at "\\(: \\|\\[\\[\\)")
+                          (forward-line 1)) t))
+        (forward-line 1))
+      (forward-line -1)
+      (point))))
 
 (defun org-babel-result-to-file (result)
   "Return an `org-mode' link with the path being the value or
@@ -347,17 +350,17 @@ string.
 
 This is taken almost directly from `org-read-prop'."
   (if (and (stringp cell) (not (equal cell "")))
-      (if (org-babel-number-p cell)
-          (string-to-number cell)
-        (if (or (equal "(" (substring cell 0 1))
-                (equal "'" (substring cell 0 2)))
-            (read cell)
-          (progn (set-text-properties 0 (length cell) nil cell) cell)))
+      (or (org-babel-number-p cell)
+          (if (or (equal "(" (substring cell 0 1))
+                  (equal "'" (substring cell 0 2)))
+              (read cell)
+            (progn (set-text-properties 0 (length cell) nil cell) cell)))
     cell))
 
 (defun org-babel-number-p (string)
   "Return t if STRING represents a number"
-  (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string))
+  (if (string-match "^[[:digit:]]*\\.?[[:digit:]]*$" string)
+      (string-to-number string)))
 
 (defun org-babel-import-elisp-from-file (file-name)
   "Read the results located at FILE-NAME into an elisp table.  If

+ 13 - 13
org-babel.org

@@ -1699,19 +1699,7 @@ This could probably be added to [[file:lisp/org-babel-script.el][org-babel-scrip
 (see [[* file result types][file result types]])
 
 
-* Bugs [11/15]
-
-** TODO when reading results from =#+resname= line
-
-Errors when trying to read from resname lines.
-
-#+resname: bug-in-resname
-: 8
-
-#+srcname: bug-in-resname-reader
-#+begin_src emacs-lisp :var buggy=bug-in-resname() :results silent
-buggy
-#+end_src
+* Bugs [12/15]
 
 ** TODO non-orgtbl formatted lists
 for example
@@ -1745,6 +1733,18 @@ even a third"
    org-babel-execute:R. (I never learned how to do this properly: org-R
    jumps all over the place...)
 
+** DONE when reading results from =#+resname= line
+
+Errors when trying to read from resname lines.
+
+#+resname: bug-in-resname
+: 8
+
+#+srcname: bug-in-resname-reader
+#+begin_src emacs-lisp :var buggy=bug-in-resname() :results silent
+buggy
+#+end_src
+
 ** DONE R-code broke on "org-babel" rename
 
 #+srcname: bug-R-babels