예제 #1
0
    def insert_cbk(self, range, text):
        
        """External editor invokes that callback to notify VoiceCode
        of a deletion event.

        **INPUTS**
                
        (INT, INT) *range* -- Start and end position of text to be
        replaced by the insertion. If end of range is None, default to 
        the end of the buffer.

        STR *text* -- Text to be inserted

        **OUTPUTS**
        
        *none* -- 
        """
        if tracing('SourceBuffWithDiffs.insert_cbk'):
            debug.trace('SourceBuffWithDiffs.insert_cbk',
                'buff %s: replacing range %s with "%s"' \
                % (self.name(), repr(range), text))
        if self.undoing:
            debug.trace('SourceBuffWithDiffs.insert_cbk',
                'in process of undoing')
            self.during_undo(text = text, range = range)
        else:
            if self._not_cached('get_text'):
# if we don't have the buffer contents cached, we don't know what text 
# was replaced, so we can't create a reverse diff, and all our previous
# change_history is invalid
                debug.trace('SourceBuffWithDiffs.insert_cbk',
                    'not cached')
                if range[1] != range[0] and range[1] != range[0] + 1:
                    debug.trace('SourceBuffWithDiffs.insert_cbk',
                        'non-empty change')
                    self.clear_stacks()
            else:
# we need the old text, so we have to do all this processing before
# calling SourceBuffCached.insert_cbk
                range_non_nil = [range[0], range[1]]
                if range_non_nil[1] == None:
                   range_non_nil[1] = len(self._get_cache('get_text')) - 1
                replaced = self.cache['get_text'][range_non_nil[0]:range_non_nil[1]]
                if tracing('SourceBuffWithDiffs.insert_cbk'):
                    debug.trace('SourceBuffWithDiffs.insert_cbk',
                        'replaced text "%s"' % replaced)
# don't record non-changes
                if replaced != text:
# for the reverse diff, we need the range of the new text
# The start of the new text is the same as the start of the old text,
# but the end is offset from the start by one more than the length of
# the new text
                    start = range_non_nil[0]
# we use the same convention for ranges as Python's slice
                    end = start + len(text)
                    reverse = ReverseBufferChange(replaced, (start, end))
                    self._push_change(reverse)

        SourceBuffCached.insert_cbk(self, range, text)