# File lib/rbreadline.rb, line 7217
  def _rl_isearch_init(direction)
    cxt = _rl_scxt_alloc(RL_SEARCH_ISEARCH, 0)
    if (direction < 0)
      cxt.sflags |= SF_REVERSE
    end

    cxt.search_terminators = @_rl_isearch_terminators ? @_rl_isearch_terminators :
      @default_isearch_terminators

    # Create an arrary of pointers to the lines that we want to search.
    hlist = history_list()
    rl_maybe_replace_line()
    i = 0
    if (hlist)
      i += 1 while(hlist[i])
    end

    # Allocate space for this many lines, +1 for the current input line,
    #   and remember those lines.
    cxt.hlen = i
    cxt.lines = []
    for i in 0 ... cxt.hlen
      cxt.lines[i] = hlist[i].line
    end

    if (@_rl_saved_line_for_history)
      cxt.lines[i] = @_rl_saved_line_for_history.line.dup
    else
      # Keep track of this so we can free it.
      cxt.allocated_line = @rl_line_buffer.dup
      cxt.lines << cxt.allocated_line
    end

    cxt.hlen+=1

    # The line where we start the search.
    cxt.history_pos = cxt.save_line

    rl_save_prompt()

    # Initialize search parameters.
    cxt.search_string_size = 128
    cxt.search_string_index = 0
    cxt.search_string = ""

    # Normalize DIRECTION into 1 or -1.
    cxt.direction = (direction >= 0) ? 1 : -1

    cxt.sline = @rl_line_buffer
    cxt.sline_len = cxt.sline.delete(0.chr).length
    cxt.sline_index = @rl_point

    @_rl_iscxt = cxt     # save globally
    cxt
  end