Ejemplo n.º 1
0
    def pasteText(self, text):
        oldpos = self.cursor.get_x()
        node = self.get_node_from_cursor()
        next_node = node.next_term

        if self.hasSelection():
            self.deleteSelection()

        text = text.replace("\r\n","\r")
        text = text.replace("\n","\r")

        if self.cursor.inside():
            internal_position = self.cursor.pos
            node.insert(text, internal_position)
            self.cursor.pos += len(text)
        else:
            #XXX same code as in key_normal
            pos = 0
            if isinstance(node, BOS) or node.symbol.name == "\r" or isinstance(node.symbol, MagicTerminal):
                # insert new node: [bos] [newtext] [next node]
                old = node
                node = TextNode(Terminal(""))
                old.insert_after(node)
                self.cursor.pos = len(text)
            else:
                pos = len(node.symbol.name)
                self.cursor.pos += len(text)
            node.insert(text, pos)
            self.cursor.node = node

        self.relex(node)
        self.post_keypress("")
        self.reparse(node)

        self.cursor.fix()
        self.cursor.line += text.count("\r")
        self.changed = True
Ejemplo n.º 2
0
    def key_normal(self, text, undo_mode = True):
        indentation = 0

        if self.hasSelection():
            self.deleteSelection()

        edited_node = self.cursor.node

        if text == "\r":
            root = self.cursor.node.get_root()
            im = self.get_indentmanager(root)
            if im:
                bol = im.get_line_start(self.cursor.node)
                indentation = im.count_whitespace(bol)
            else:
                indentation = self.get_indentation(self.cursor.line)
            if indentation is None:
                indentation = 0
            text += " " * indentation

        node = self.get_node_from_cursor()
        if node.image and not node.plain_mode:
            self.leave_languagebox()
            node = self.get_node_from_cursor()
        # edit node
        if self.cursor.inside():
            internal_position = self.cursor.pos #len(node.symbol.name) - (x - self.cursor.x)
            node.insert(text, internal_position)
        else:
            # append to node: [node newtext] [next node]
            pos = 0
            if str(text).startswith("\r"):
                newnode = TextNode(Terminal(""))
                node.insert_after(newnode)
                node = newnode
                self.cursor.pos = 0
            elif isinstance(node, BOS) or node.symbol.name == "\r":
                # insert new node: [bos] [newtext] [next node]
                old = node
                if old.next_term:
                    # skip over IndentationTerminals
                    old = old.next_term
                    while isinstance(old.symbol, IndentationTerminal):
                        old = old.next_term
                    old = old.prev_term
                node = TextNode(Terminal(""))
                old.insert_after(node)
                self.cursor.pos = 0
            elif isinstance(node.symbol, MagicTerminal):
                old = node
                node = TextNode(Terminal(""))
                old.insert_after(node)
                self.cursor.pos = 0
            else:
                pos = self.cursor.pos#len(node.symbol.name)
            node.insert(text, pos)
            self.cursor.node = node
        self.cursor.pos += len(text)

        need_reparse = self.relex(node)
        self.cursor.fix()
        self.fix_cursor_on_image()
        temp = self.cursor.node
        self.cursor.node = edited_node
        need_reparse |= self.post_keypress(text)
        self.cursor.node = temp
        self.reparse(node, need_reparse)
        if undo_mode:
            self.undomanager.add('insert', text, self.cursor.copy())
        self.changed = True
        return indentation