anchor.diff:                  x.SetPrompt ("(reverse-i-search)`" + s + "': ")
   
               member private x.ReverseSearch () =
  -                let mutable search_backward = true
  -
  -                if cursor = text.Length then
  -                    // The cursor is at the end of the string
  -                    let p = (text.ToString()).LastIndexOf (search)
  -                    if p <> -1 then
  -                        match_at <- p
  -                        cursor <- p
  -                        x.ForceCursor (cursor)
  -                        search_backward <- false
  -                else
  -                    // The cursor is somewhere in the middle of the string
  -                    let start = if cursor = match_at then cursor - 1 else cursor
  -                    if start <> -1 then
  -                        let p = (text.ToString()).LastIndexOf (search, start)
  +                match searchState with
  +                | Some(search) ->
  +                    let mutable search_backward = true
  +
  +                    if cursor = text.Length then
  +                        // The cursor is at the end of the string
  +                        let p = (text.ToString()).LastIndexOf (search.Term)
                           if p <> -1 then
  -                            match_at <- p
  +                            searchState <- Some({ search with MatchAt = p })
                               cursor <- p
                               x.ForceCursor (cursor)
                               search_backward <- false
  -
  -                if search_backward then
  -                    // Need to search backwards in history
  -                    x.HistoryUpdateLine ()
  -                    let (newHistory, searchResult) = History.searchBackward search state.History
  -                    state <- { state with History = newHistory }
  -                    match searchResult with
  -                    | Some(_) ->
  -                        match_at <- -1
  -                        x.SetText searchResult
  +                    else
  +                        // The cursor is somewhere in the middle of the string
  +                        let start = if cursor = search.MatchAt then cursor - 1 else cursor
  +                        if start <> -1 then
  +                            let p = (text.ToString()).LastIndexOf (search.Term, start)
  +                            if p <> -1 then
  +                                searchState <- Some({ search with MatchAt = p })
  +                                cursor <- p
  +                                x.ForceCursor (cursor)
  +                                search_backward <- false
  +
  +                    if search_backward then
  +                        // Need to search backwards in history
  +                        x.HistoryUpdateLine ()
  +                        let (newHistory, searchResult) = History.searchBackward search.Term state.History
  +                        state <- { state with History = newHistory }
  +                        match searchResult with
  +                        | Some(_) ->
  +                            searchState <- Some({ search with MatchAt = -1 })
  +                            x.SetText searchResult
  +                            x.ReverseSearch ()
  +                        | None -> ()
  +                | None ->
  +                    failwith "No search in progress"
  +
  +            member private x.CmdReverseSearch () =
  +                match searchState with
  +                | None ->
  +                    searchState <- Some { MatchAt = -1; Term = ""; Direction = SearchDirection.Backward}
  +                    x.SetSearchPrompt ("")
  +                | Some(search) ->
  +                    if search.Term = "" then
  +                        match previousSearch with
  +                        | None | Some("") ->
  +                            ()
  +                        | Some(previousTerm) -> 
  +                            searchState <- Some { search with Term = previousTerm}
  +                            x.SetSearchPrompt previousTerm
  +                            x.ReverseSearch ()
  +                    else
                           x.ReverseSearch ()
  -                    | None -> ()
  -
  -
  -        
  -            member private x.CmdReverseSearch () =
  -                match searching with
  -                | None ->
  -                    match_at <- -1
  -                    last_search <- search
  -                    searching <- Some(SearchDirection.Backward)
  -                    search <- ""
  -                    x.SetSearchPrompt ("")
  -                | Some(_) ->
  -                    if search = "" then
  -                        if last_search <> "" && last_search <> null then
  -                            search <- last_search
  -                            x.SetSearchPrompt (search)
  -
  -                            x.ReverseSearch ()
  -                    else
  -                        x.ReverseSearch ()
  -
  -
  -            member private x.SearchAppend (c:char) =
  -                search <- search + (string)c
  -                x.SetSearchPrompt (search)
  +
  +
  +            member private x.SearchAppend (c:char) search =
  +                let newTerm = search.Term + (string)c
  +                searchState <- Some { search with Term = newTerm }
  +                x.SetSearchPrompt newTerm
   
                   //
                   // If the new typed data still matches the current text, stay here
  
positive.code:                 x.ForceCursor (cursor)
         
              member public x.Edit prompt initial =
                  edit_thread <- Thread.CurrentThread;
                  searching <- None;
                  let cancelHandler = new ConsoleCancelEventHandler(x.InterruptEdit)
                  Console.CancelKeyPress.AddHandler cancelHandler
              
  
negative.code:                         handler_index <- handler_index + 1
  
                      if handled then
                          match (searchState, last_command) with
                          | ( _, Command.ReverseSearch) -> ()
                          | (Some(search), _) -> 
                              previousSearch <- Some(search.Term)
                              searchState <- None
                              x.SetPrompt (specified_prompt)
                          | _ -> ()
                     
  