 CONSTANT: PAGE V{ }
 CONSTANT: URL V{ }
 
-! XXX: page titles and urls in history
+: find-url ( url items -- i item )
+    [ dup array? [ first ] when = ] with find ;
+
+: nth-url ( i items -- url )
+    ?nth dup array? [ first ] when ;
+
+: stack-url ( delta -- url )
+    URL ?first STACK find-url drop
+    [ + STACK nth-url ] [ drop f ] if* ;
 
 : add-stack ( args -- )
-    dup STACK index [ drop ] [
-        URL ?first STACK index [
-            over PAGE keys index [
-                1 + dup STACK ?nth pick = [ 2drop ] [
+    dup dup array? [ first ] when
+    dup STACK find-url drop [
+        2drop
+    ] [
+        URL ?first STACK find-url drop [
+            over PAGE find-url drop [
+                1 + dup STACK nth-url rot = [
+                    2drop
+                ] [
                     STACK [ length ] [ delete-slice ] bi
                     STACK push
                     STACK length 10 > [

        0 swap remove-nth!
    ] when dupd remove! push ;

: gemini-history ( -- )
    HISTORY [ 1 + swap "[%d] %s\n" printf ] each-index
    LINKS delete-all HISTORY LINKS push-all ;

: gemini-print ( url body meta -- )
    f pre [

        "cat" swap 2array prefix run-pipeline drop
    ] [ 2drop ] if ;

CONSTANT: COMMANDS {
    T{ command
        { name "back" }
