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)
def delete_cbk(self, range): """External editor invokes that callback to notify VoiceCode of a deletion event. **INPUTS** (INT, INT) *range* -- Start and end pos of range to be deleted **OUTPUTS** *none* -- """ debug.trace('SourceBuffWithDiffs.delete_cbk', 'buff %s: deleting range = %s' % (self.name(), repr(range))) if self.undoing: debug.trace('SourceBuffWithDiffs.delete_cbk', 'in process of undoing') self.during_undo(text = "", range = range) else: if self.cache['get_text'] == None: # if we don't have the buffer contents cached, we don't know what text # was deleted, so we can't create a reverse diff, and all our previous # change_history is invalid debug.trace('SourceBuffWithDiffs.delete_cbk', 'not cached') if range[1] != range[0] and range[1] != range[0] + 1: debug.trace('SourceBuffWithDiffs.delete_cbk', 'non-empty change') self.clear_stacks() else: # we need the old text, so we have to do all this processing before # calling SourceBuffCached.delete_cbk deleted = self.cache['get_text'][range[0]:range[1]] # don't record deletions of nothing if tracing('SourceBuffWithDiffs.delete_cbk'): debug.trace('SourceBuffWithDiffs.delete_cbk', 'deleted text "%s"' % deleted) if deleted: # 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[0] end = range[0] # for deletions, the range of the new text is empty (technically, we reverse = ReverseBufferChange(deleted, (start, end)) self._push_change(reverse) SourceBuffCached.delete_cbk(self, range)
def contents_cbk(self, text): """External editor invokes that callback to inform VoiceCode of the buffer contents. **INPUTS** STR *text* -- Text to be inserted **OUTPUTS** *none* -- """ 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.contents_cbk', 'not cached') # if the text isn't cached, then the stacks should already be clear, # but just for good measure self.clear_stacks() if self.undoing: # this should REALLY never happen, but if it does, # there is no good way to handle it msg = 'WARNING: SourceBuffWithDiffs.contents_cbk called\n' msg = msg + 'while undoing changes, with NO CACHED TEXT.\n' msg = msg +'. Please report this as a bug\n' debug.critical_warning(msg) SourceBuffCached.contents_cbk(self, text) return # otherwise, treat this as an insert callback start, end, change = \ find_difference.find_difference(self.cache['get_text'], text) self.insert_cbk(range = (start, end), text = change)