Ejemplo n.º 1
0
 def insert_object(self, data):
     """
     Insert any object into the selection and adjust selection
     to cover the inserted object.
     """
     if isinstance(self.selection, mutable.Selection):
         if isinstance(self.selection.container, (mutable.Document, mutable.List)):
             self.selection.splice([data])
             return True
     else:
         mutable.get_document(self.selection).replace(self.selection, data)
         self.selection = data
         return True
Ejemplo n.º 2
0
 def insert_inline_deep(self, target, block, which):
     parent = find_parent_deep(mutable.get_object(self.selection), which)
     if parent is None:
         if self.insert_object(block):
             self.selection = target
     else:
         document = mutable.get_document(parent)
         document.replace(parent, block)
         document.replace(target, parent)
         self.selection = next_selection(parent)
Ejemplo n.º 3
0
    def on_keydown(self, key, modifiers, text):
        selection = self.selection
        overlay = self.overlay
        shift = 'shift' in modifiers
        ctrl  = 'ctrl' in modifiers
        alt   = 'alt' in modifiers
        if ctrl and key == 's':
            document = mutable.get_document(mutable.get_object(selection))
            fileformat_flat.save_file(document.filename, mutable, document)
            return
        if ctrl and key == 'a':
            self.selection = mutable.extend(selection)
            overlay.dirty = True
            return
        if ctrl and key == 'left':
            self.selection = walk_prev_selection(selection)
            overlay.dirty = True
            return
        if ctrl and key == 'right':
            self.selection = walk_next_selection(selection)
            overlay.dirty = True
            return
        if isinstance(selection, mutable.Selection):
            if key == 'left':
                if selection.head > 0:
                    selection.head -= 1
                else:
                    self.selection = prev_selection(mutable.get_object(selection))
                    overlay.dirty = True
                    return
            elif key == 'right':
                if selection.head < selection.last:
                    selection.head += 1
                else:
                    self.selection = next_selection(mutable.get_object(selection))
                    overlay.dirty = True
                    return
            elif key == 'home':
                selection.head = 0
            elif key == 'end':
                selection.head = selection.last
            if key in ('left', 'right', 'home', 'end'):
                selection.tail = selection.tail if shift else selection.head
                overlay.dirty = True
#            if isinstance(selection.container, mutable.String):
#                if not ctrl and len(text) > 0 and text.isalnum() or text in ' ':
#                    selection.splice(text)
#                    selection.start = selection.stop
            if key == 'backspace' or key == 'delete':
                if selection.start == selection.stop:
                    selection.start -= (key == 'backspace')
                    selection.stop  += (key == 'delete')
                selection.splice(u'')
                selection.stop = selection.start
                return
        else:
            parent = selection.parent
            index  = parent.index(selection)
            if key == 'left':
                self.selection = prev_selection(selection)
                overlay.dirty = True
            if key == 'right':
                self.selection = next_selection(selection)
                overlay.dirty = True

        if text == '=':
            target = mkstruct(u"null")
            block = mkstruct(u"assign:slot:value", target, mkstruct(u"null"))
            return self.insert_inline(target, block, expressions)
        if alt and key == 'r':
            target = mkstruct(u"null")
            block = mkstruct(u"return:value", target)
            return self.insert_inline_deep(target, block, expressions)
        if text == '(':
            target = mkstruct(u"null")
            block = mkstruct(u"call:callee:arguments", target, mutable.List([]))
            return self.insert_inline(target, block, expressions)
        if key == 'space':
            self.selection = next_selection(mutable.get_object(selection))
            overlay.dirty = True
        elif len(text) > 0 and not self.insert_text(text):
            if text.isalpha():
                target = mutable.String(u"")
                if self.insert_object(mkstruct(u'variable:name', target)):
                    self.selection = mutable.Selection(target, 0)
                    self.insert_text(text)
            elif text.isdigit():
                target = mutable.String(u"")
                if self.insert_object(mkstruct(u'number:value', target)):
                    self.selection = mutable.Selection(target, 0)
                    self.insert_text(text)