'Wrap long Fortran lines
Argument:
lines  --  a list of lines (without \n character)
A comment line is split at white space. Code lines are split with a more
complex rule to give nice results.'

def _wrap_fortran(self, lines):

    my_alnum = set((('_+-.' + string.digits) + string.ascii_letters))
    my_white = set('     DCTB ()')
    def split_pos_code(line, endpos):
        if (len(line) <= endpos):
            return len(line)
        pos = endpos
        split = (lambda pos: (((line[pos] in my_alnum) and (line[(pos - 1)] not in my_alnum)) or ((line[pos] not in my_alnum) and (line[(pos - 1)] in my_alnum)) or ((line[pos] in my_white) and (line[(pos - 1)] not in my_white)) or ((line[pos] not in my_white) and (line[(pos - 1)] in my_white))))
        while (not split(pos)):
            pos -= 1
            if (pos == 0):
                return endpos
        return pos
    result = []
    if (self._settings['source_format'] == 'free'):
        trailing = '    &'
    else:
        trailing = ''
    for line in lines:
        if line.startswith(self._lead['comment']):
            if (len(line) > 72):
                pos = line.rfind('    ', 6, 72)
                if (pos == (-1)):
                    pos = 72
                hunk = line[:pos]
                line = line[pos:].lstrip()
                result.append(hunk)
                while (len(line) > 0):
                    pos = line.rfind('    ', 0, 66)
                    if ((pos == (-1)) or (len(line) < 66)):
                        pos = 66
                    hunk = line[:pos]
                    line = line[pos:].lstrip()
                    result.append(('%s%s' % (self._lead['comment'], hunk)))
            else:
                result.append(line)
        elif line.startswith(self._lead['code']):
            pos = split_pos_code(line, 72)
            hunk = line[:pos].rstrip()
            line = line[pos:].lstrip()
            if line:
                hunk += trailing
            result.append(hunk)
            while (len(line) > 0):
                pos = split_pos_code(line, 65)
                hunk = line[:pos].rstrip()
                line = line[pos:].lstrip()
                if line:
                    hunk += trailing
                result.append(('%s%s' % (self._lead['cont'], hunk)))
        else:
            result.append(line)
    return result
