def find_pattern_on_line(lines, n, max_wrap_lines):
    """
    Finds a forward/reply pattern within the given lines on text on the given
    line number and returns a tuple with the type ('reply' or 'forward') and
    line number of where the pattern ends. The returned line number may be
    different from the given line number in case the pattern wraps over
    multiple lines.

    Returns (None, None) if no pattern was found.
    """
    for typ, regexes in COMPILED_PATTERN_MAP.items():
        for regex in regexes:
            for m in range(max_wrap_lines):
                match_line = join_wrapped_lines(lines[n:n+1+m])
                if match_line.startswith('>'):
                    match_line = match_line[1:].strip()
                if regex.match(match_line.strip()):
                    return n+m, typ
    return None, None
Finds a forward/reply pattern within the given lines on text on the given
    line number and returns a tuple with the type ('reply' or 'forward') and
    line number of where the pattern ends. The returned line number may be
    different from the given line number in case the pattern wraps over
    multiple lines.

    Returns (None, None) if no pattern was found.