'Move current line or selected text'

def __move_line_or_selection(self, after_current_line=True):

    cursor = self.textCursor()
    cursor.beginEditBlock()
    (start_pos, end_pos) = self.__save_selection()
    last_line = False
    cursor.setPosition(start_pos)
    cursor.movePosition(QTextCursor.StartOfBlock)
    start_pos = cursor.position()
    cursor.setPosition(end_pos)
    if ((not cursor.atBlockStart()) or (end_pos == start_pos)):
        cursor.movePosition(QTextCursor.EndOfBlock)
        cursor.movePosition(QTextCursor.NextBlock)
    end_pos = cursor.position()
    if cursor.atEnd():
        if ((not cursor.atBlockStart()) or (end_pos == start_pos)):
            last_line = True
    cursor.setPosition(start_pos)
    if (cursor.atStart() and (not after_current_line)):
        cursor.endEditBlock()
        self.setTextCursor(cursor)
        self.__restore_selection(start_pos, end_pos)
        return
    cursor.setPosition(end_pos, QTextCursor.KeepAnchor)
    if (last_line and after_current_line):
        cursor.endEditBlock()
        self.setTextCursor(cursor)
        self.__restore_selection(start_pos, end_pos)
        return
    sel_text = to_text_string(cursor.selectedText())
    cursor.removeSelectedText()
    if after_current_line:
        text = to_text_string(cursor.block().text())
        sel_text = (os.linesep + sel_text[0:(-1)])
        cursor.movePosition(QTextCursor.EndOfBlock)
        start_pos += (len(text) + 1)
        end_pos += len(text)
        if (not cursor.atEnd()):
            end_pos += 1
    else:
        if last_line:
            cursor.deletePreviousChar()
            sel_text = (sel_text + os.linesep)
            cursor.movePosition(QTextCursor.StartOfBlock)
            end_pos += 1
        else:
            cursor.movePosition(QTextCursor.PreviousBlock)
        text = to_text_string(cursor.block().text())
        start_pos -= (len(text) + 1)
        end_pos -= (len(text) + 1)
    cursor.insertText(sel_text)
    cursor.endEditBlock()
    self.setTextCursor(cursor)
    self.__restore_selection(start_pos, end_pos)
