anchor.diff:  -include("imem_seco.hrl").
   -include("imem_sql.hrl").
   
  +-define(KeyMax,[]).     %% value bigger than any possible sort key {SortFun(Recs),Id}
  +-define(KeyMin,{}).     %% value smaller than any possible sort key {SortFun(Recs),Id}
   
   -record(ctx,    { %% session context
                     skey
                   , id
  -                , bl                  %% block length
  +                , bl                  %% block length -> State
                   , stmtRef             %% statement reference
                   , isSec               %% secure mode or not
  -                , rowFun              %% RowFun
  +                , rowFun              %% RowFun -> State
  +                , sortFun             %% SortFun -> State
  +                , sortSpec            %% SortSpec (based on statement full map)
                   , replyTo             %% reply pid
                   }).
   
   -record(state,  { %% fsm combined state
                     ctx                 %% statement & fetch context
  -                , tableId             %% ets table id 
  +                , tableId             %% ets raw buffer table id 
  +                , indexId             %% ets index table id 
  +                , bl                  %% block_length (passed to init)
  +                , gl                  %% gui max length (row count) = gui_max(#state.bl)
                   , stmtRef             %% statement reference
                   , rowFun              %% RowFun
  -                , bl                  %% block_length (passed to init)
  -                , gl                  %% gui max length (row count) = gui_max(#state.bl)
  -                , bufTop = 0          %% sequence number of top buffer row (or 0)
  -                , bufBot = 0          %% sequence number of bottom buffer row (or 0)
  +                , sortSpec            %% from imem statement, changed by gui events
  +                , sortFun             %% from imem statement, follows sortSpec (calculated by imem statement)
  +                , filterSpec = {}     %% {FType,[ColF|ColFs]}  changed by gui events FType= and|or  ColF = [{Col,["value1".."valuen"]}]
  +                , filterFun           %% follows filterSpec
  +
  +                , rawCnt = 0          %% buffer row count
  +                , rawTop = 99999999   %% id of top buffer row 
  +                , rawBot = 0          %% id of bottom buffer row
  +                , dirtyCnt = 0        %% count of dirty rows in buffer
                   , dirtyTop = 99999999 %% record id of first dirty row in buffer
                   , dirtyBot = 0        %% record id of last dirty row in buffer
  -                , dirtyCount = 0      %% count of dirty rows in buffer
  -                , rawTop = 0          %% sequence number of top raw gui row (or 0)
  -                , rawBot = 0          %% sequence number of bot raw gui row (or 0)
  +
  +                , indCnt = 0          %% count of indexed buffer entries (after filtering) 
  +                , indTop = []         %% smallest index after filtering, initialized to big value
  +                , indBot = {}         %% biggest index after filtering, initialized to small value
  +
  +                , bufCnt = 0          %% buffer row count           (either rawCnt or indCnt, depending on nav)
  +                , bufTop              %% id of top buffer row       (either rawTop or indTop, depending on nav)
  +                , bufBot              %% id of bottom buffer row    (either rawBot or indBot, depending on nav)
  +
  +                , guiCnt = 0          %% count of scrollable entries in gui 
  +                , guiTop              %% top gui pointer (Id for raw / SortKey for ind)
  +                , guiBot              %% bottom gui pointer (Id for raw / SortKey for ind)
  +                , guiCol = false      %% index collision (stale view in gui)
  +
  +                , nav = raw           %% navigation   raw | ind
  +                , pfc=0               %% pending fetch count (in flight to DB or back)
                   , tailMode = false    %% tailMode scheduled
  -                , pfc=0               %% pending fetch count (in flight to DB or back)
  +                , stack = undefined   %% command stack {button,Button,ReplyTo}
                   , replyTo             %% reply pid
  -                , stack=undefined     %% command stack {Tab,button,Button,ReplyTo}
                   }).
   
   -record(gres,   { %% response sent back to gui
  -                  operation           %% rep (replace) | app (append) | prp (prepend) | nop | cre (create filter/sort table)
  -                , bufBot=0            %% current buffer bottom (last sequence number)
  -                , rawTop=0            %% first row id in gui (smaller ones must be clipped away)
  -                , rawBot=0            %% last row id in gui (larger ones must be clipped away)
  -                , message = []        %% help message
  +                  operation           %% rep (replace) | app (append) | prp (prepend) | nop | close
  +                , cnt = 0             %% current buffer size (raw table or index table size)
  +                , toolTip = ""        %% current buffer sizes RawCnt/IndCnt plus status information
  +                , message = ""        %% error message
                   , beep = false        %% alert with a beep if true
                   , state = empty       %% determines color of buffer size indicator
                   , loop = undefined    %% gui should come back with this command
                   , rows = []           %% rows to show (append / prepend / merge)
  +                , keep = 0            %% row count to be kept
                   }).
   
   -define(block_size,10).
  
positive.code: %%          {stop, Reason, NewStateData}
  %% --------------------------------------------------------------------
  
  handle_event(_Event, empty, StateData) ->
      {no_reply, empty, StateData, infinity}.
  
  %% --------------------------------------------------------------------
  %% Func: handle_sync_event/4 handling sync "send_all_state_event""
  
negative.code:                 );"
                  , 0, 'Imem', IsSec)),
  
          ?assertEqual(ok, insert_range(SKey, 111, def, 'Imem', IsSec)),
  
  
          % Fsm = start(SKey,"fsm test",10,false,"select * from def;",self()),
          Fsm = start(SKey,"fsm test",10,false,"select * from def order by col1;",self()),
          ?Log("test client pid ~p~n", [self()]),
          ?Log("fsm object id ~p~n", [Fsm]),
          Fsm:?button('>'),
          receive_respond(Fsm),
          Fsm:?button('>'),
          receive_respond(Fsm),
          Fsm:?button('>'),
          receive_respond(Fsm),
          Fsm:?button('>'),
          receive_respond(Fsm),
          Fsm:?button('>|...'),
          receive_respond(Fsm),
          Fsm:?button('<'),
          receive_respond(Fsm),
          Fsm:?button('>|'),
          receive_respond(Fsm),
          Fsm:?button('<'),
          receive_respond(Fsm),
          Fsm:?button('<'),
          receive_respond(Fsm),
          Fsm:?button('<'),
          receive_respond(Fsm),
          Fsm:?button('<'),
          receive_respond(Fsm),
          Fsm:?button('<'),
          receive_respond(Fsm),
          Fsm:?button(close),
  
          ?assertEqual(ok, imem_sql:exec(SKey, "drop table def;", 0, 'Imem', IsSec)),
  
  