Preserve typeahead when checking terminal input

ober

62131efce239a0f81ded335b003fd165f7c5ef29

diff --git a/lineedit.ss b/lineedit.ss
index 2fdf301..2654b29 100644
--- a/lineedit.ss
+++ b/lineedit.ss
@@ -1415,7 +1415,16 @@
 ;; Check if a port is a terminal
 (def (tty? port)
   (with-catch (lambda (e) #f)
-    (lambda () (tty-mode-set! port #t #t #f #f 0) #t)))
+    (lambda ()
+      ;; tty-mode-set! can flush queued typeahead on some terminals.  Keep this
+      ;; predicate side-effect free so input typed while a command is running is
+      ;; still available to the next prompt.
+      (let ([fd (with-catch (lambda (e) #f)
+                            (lambda () (port-file-descriptor port)))])
+        (cond
+          [(fixnum? fd) (= (ffi-isatty fd) 1)]
+          [(eq? port (current-input-port)) (= (ffi-isatty 0) 1)]
+          [else #f])))))
 
 ;; Calculate visible width (ignoring ANSI escapes)
 (def (visible-width str)