def tabify(s):
    """Convert all groups of 4 spaces in `s` to tabs. Return the result."""
    tabwidth = 4
    tabtoken = ' ' * tabwidth
    out = ''
    for line in s.split('\n'):
        prefix = ''
        newLine = ''
        inPrefix = True
        for c in line:
            if inPrefix and c == ' ':
                prefix += ' '
            elif inPrefix and c == '\t':
                prefix += tabtoken
            elif inPrefix:
                inPrefix = False
            if not inPrefix:
                newLine += c
        newPrefix = '\t' * ((len(prefix) + tabwidth - 1) // tabwidth)
        out += newPrefix
        out += newLine
        out += '\n'
    return out
