     f >>caret f >>mark ; inline
 
 : prepare-last-line ( pane -- )
+    [ last-line>> ] keep
+    [ current>> f track-add ]
+    [ input>> [ 1 track-add ] when* ] bi
+    drop ; inline
+
+: init-current ( pane -- pane )
+    dup prototype>> clone >>current ; inline
+
+: next-line ( pane -- )
     clear-selection
-    [ last-line>> unparent ]
-    [
-        [ horizontal <track> ] dip
-        dup prototype>> clone >>current
-        [ current>> f track-add ]
-        [ input>> [ 1 track-add ] when* ]
-        [ swap [ >>last-line ] [ 1 track-add ] bi drop ]
-        tri
-    ]
+    [ input>> unparent ]
+    [ init-current prepare-last-line ]
     [ input>> [ request-focus ] when* ] tri ;
 
 : pane-caret&mark ( pane -- caret mark )

    [
        [ current>> [ unparent ] [ smash-line ] bi ] [ output>> ] bi
        add-incremental
    ] [ prepare-last-line ] bi ;

: pane-write ( seq pane -- )
    [ pane-nl ] [ current>> stream-write ]

    [ current>> clear-gadget ]
    bi ;

: init-prototype ( pane -- pane )
    <shelf> +baseline+ >>align >>prototype ; inline

: init-output ( pane -- pane )
    <incremental> [ >>output ] [ f track-add ] bi ; inline

: pane-theme ( pane -- pane )
    1 >>fill
    selection-color >>selection-color ; inline

: init-last-line ( pane -- pane )
    horizontal <track> [ >>last-line ] [ 1 track-add ] bi ; inline

: new-pane ( input class -- pane )
    [ vertical ] dip new-track
        swap >>input
        pane-theme
        init-prototype
        init-output
        init-current
        init-last-line ; inline

: <pane> ( -- pane ) f pane new-pane ;

