 
 : push-end ( obj gb -- ) [ length ] keep insert* ;
 
-: pop-elem ( position gb -- elem ) [ nth ] 2keep delete* ;
+: pop-elem ( pos gb -- elem ) [ nth ] 2keep delete* ;
 
 : pop-start ( gb -- elem ) 0 swap pop-elem ;
 


: move-gap-back-inside? ( i gb -- i gb ? )
    ! is it cheaper to move the gap inside than around?
    2dup [ gap-start>> swap 2 * - ] keep [ buffer-length ] keep gap-end>> - <= ;

: move-gap-forward-inside? ( i gb -- i gb ? )
    ! is it cheaper to move the gap inside than around?
    2dup [ gap-end>> [ 2 * ] dip - ] keep [ gap-start>> ] keep buffer-length + <= ;

: move-gap-forward-inside ( i gb -- )
    [ dup gap-length neg swap gap-end>> rot ] keep seq>> copy-elements ;

: move-gap-back-inside ( i gb -- )
    [ dup gap-length swap gap-start>> 1 - rot 1 - ] keep seq>> copy-elements ;

: move-gap-forward-around ( i gb -- )
    0 over move-gap-back-inside [

: increment-gap-start ( gb n -- )
    over gap-start>> + swap gap-start<< ;

M: number insert*
    [ 1array ] 2dip insert* ;

M: sequence insert*
    prepare-insert [ insert-elements ] 2keep swap length increment-gap-start ;

: delete* ( pos gb -- )
    tuck move-gap dup gap-end>> 1 + over gap-end<< ?decrease ;

! -------- stack/queue operations -----------

