def find_next_matching_line(key, cursor):
    """
    Determine the distance into a file (searching forward from the
    given cursor) to the next match against a given key.  Used to
    determine which is the closest key to resync towards.

    Returns None if there is no key or no line found.

    If the key is already found in the backlog then there will be
    nothing appropriate to match further in the file, so return None.
    """

    if not key:
        return (None, None)
    line = cursor.find_next_match(key)
    if not line:
        return (None, None)

    distance = line.linenr - cursor.linenr
    return (line, distance)