Fix terminal key focus stolen from split window

ober

3f293624bf66281b432842afa08ad87ef3be63b3

diff --git a/lib/jerboa-emacs/qt/app.sls b/lib/jerboa-emacs/qt/app.sls
index 40fb299..9dfa0c8 100644
--- a/lib/jerboa-emacs/qt/app.sls
+++ b/lib/jerboa-emacs/qt/app.sls
@@ -774,13 +774,16 @@
                                    [count (qt-stacked-widget-count
                                             container)]
                                    [tw (qt-terminal-widget term)])
-                              (qt-stacked-widget-set-current-index!
-                                container
-                                (- count 1))
                               (unless (hash-get terminal-key-installed tw)
                                 ((app-state-key-handler app) tw)
                                 (hash-put! terminal-key-installed tw #t))
-                              (qt-widget-set-focus! tw)))))]
+                              (if (> count 1)
+                                  (begin
+                                    (qt-stacked-widget-set-current-index!
+                                      container
+                                      (- count 1))
+                                    (qt-widget-set-focus! tw))
+                                  (qt-widget-set-focus! editor))))))]
                      [(image-buffer? buf)
                       (qt-show-image-buffer! editor buf)
                       (let ([win (hash-get *editor-window-map* editor)])
diff --git a/src/jerboa-emacs/qt/app.ss b/src/jerboa-emacs/qt/app.ss
index 658afc7..71e7046 100644
--- a/src/jerboa-emacs/qt/app.ss
+++ b/src/jerboa-emacs/qt/app.ss
@@ -833,14 +833,18 @@
                             (let* ((container (qt-edit-window-container win))
                                    (count (qt-stacked-widget-count container))
                                    (tw (qt-terminal-widget term)))
-                              ;; Switch to the last index (terminal)
-                              (qt-stacked-widget-set-current-index! container (- count 1))
-                              ;; Install consuming key filter on terminal widget if not yet done
-                              ;; (guards against double-install which would double-fire events)
+                              ;; Always install consuming key filter (idempotent via guard)
                               (unless (hash-get terminal-key-installed tw)
                                 ((app-state-key-handler app) tw)
                                 (hash-put! terminal-key-installed tw #t))
-                              (qt-widget-set-focus! tw))))))
+                              (if (> count 1)
+                                ;; Terminal widget lives in THIS container — show and focus it
+                                (begin
+                                  (qt-stacked-widget-set-current-index! container (- count 1))
+                                  (qt-widget-set-focus! tw))
+                                ;; Terminal widget is in another window (e.g. after C-x 2).
+                                ;; Just show the editor in this window — don't steal focus.
+                                (qt-widget-set-focus! editor)))))))
                   ;; Image buffers
                   ((image-buffer? buf)
                    (qt-show-image-buffer! editor buf)