import re

# Strip prefixes in subject
#
# When replying to a reply, Claws Mail strips the "Re: " reply
# marker of the subject line before prefixing with its own.
# Claws Mail also knows several localized variants from various
# mailers, such as "Aw: " (German Outlook), "Odp: " (Polish Outlook)
# and so on.
#
# However, it doesn't know all, and as of 3.8.1, adding new ones
# is not possible via a config option.
#
# This function is there to add new markers. It will also strip
# an already messed up original subject line, provided that all
# prefixes are defined below.
# So, for example, replying to a mail with
#    Subject: R: Re: R: Re: Aw: R: Re: Old topic
# will result in 
#    Subject: Re: Old topic
#
# This is a slightly adapted version of a script provided
# by Michael Gmelin and Slavko on Claws Mail's users mailing list.
def strip_subject_prefixes():
    # A list of prefixes to strip during reply. Add the ones that are
    # interesting for you here.
    prefixes = ["Re", "R", "Odp", "Aw"]
    
    # Build up regex to match unwanted prefixes
    prefix_string = "|".join(prefixes)
    regex_str = r"^(Re|Fwd|Fw):( (%s):)+" % prefix_string
    
    # Get a string with those prefixes stripped
    new_subject = re.sub(regex_str, r"\1:",	clawsmail.compose_window.get_subject())
    
    # Set this string to be the new subject
    clawsmail.compose_window.set_subject(new_subject)
    
    # Normally, when the subject or body is modified, the mail gets marked
    # as modified, wich results in a popup dialog when the compose window is 
    # just closed. We don't want to treat the automatic modification of the
    # subject line from above to trigger such a popup, so we override the
    # modification marker.
    # Note that this affects only for the modifications done so far. As
    # soon as the user starts to modify the mail manually, it will be
    # set again.
    clawsmail.compose_window.set_modified(False)


strip_subject_prefixes()