def auto_newline(buffer):
    """
    Insert \\n at the cursor position. Also add necessary padding.
    """
    insert_text = buffer.insert_text
    if buffer.document.current_line_after_cursor:
        insert_text('\n')
    else:
        current_line = buffer.document.current_line_before_cursor.rstrip()
        insert_text('\n')
        unindent = current_line.rstrip().endswith(' pass')
        current_line2 = current_line[4:] if unindent else current_line
        for c in current_line2:
            if c.isspace():
                insert_text(c)
            else:
                break
        if current_line[-1:] == ':':
            for x in range(4):
                insert_text(' ')
