def __init__(self, entry): self.entry = entry self.view = entry.view() self.findstr = None self.replacestr = None self.search_boundaries = utils.Struct({'start': None, 'end': None}) self.find_result = utils.Struct({'start': None, 'end': None}) self.unescapes = [['\\n', '\n'], ['\\r', '\r'], ['\\t', '\t']] self.from_start = False self.search_start_mark = None
def get_current_replace(self): buf = self.view.get_buffer() bounds = utils.Struct({'start': buf.get_iter_at_mark(self.find_result.start), 'end': buf.get_iter_at_mark(self.find_result.end)}) if not bounds.start.equal(bounds.end): text = bounds.start.get_text(bounds.end) return self.get_replace(text) else: return self.replacestr
def replace(self, findstr, replaceall=False, replacestr=None): if findstr: self.set_find(findstr) if replacestr != None: self.set_replace(replacestr) # First find something buf = self.view.get_buffer() if replaceall: startmark = buf.create_mark(None, buf.get_iter_at_mark(buf.get_insert()), False) ret = (yield self.find_first(select=not replaceall)) if not ret: yield commander.commands.result.DONE self.scroll_back = False # Then ask for the replacement string if not self.replacestr: try: if replaceall: self.scroll_back = True self.select_last_result() replacestr, words, modifier = ( yield commander.commands.result.Prompt('Replace with:')) self.set_replace(replacestr) except GeneratorExit as e: if replaceall: self._restore_cursor(startmark) self.cancel() raise e # On replace all, wrap it in begin/end user action if replaceall: buf.begin_user_action() try: while True: if not replaceall: rep, words, modifier = ( yield commander.commands.result.Prompt( 'Replace next [%s]:' % (saxutils.escape(self.get_current_replace()), ))) if rep: self.set_replace(rep) bounds = utils.Struct({ 'start': buf.get_iter_at_mark(self.find_result.start), 'end': buf.get_iter_at_mark(self.find_result.end) }) # If there is a selection, replace it with the replacement string if not bounds.start.equal(bounds.end) and ( replaceall or not (modifier & Gdk.ModifierType.CONTROL_MASK)): text = bounds.start.get_text(bounds.end) repl = self.get_replace(text) buf.begin_user_action() buf.delete(bounds.start, bounds.end) buf.insert(bounds.start, repl) buf.end_user_action() # Find next if not self.find_next(select=not replaceall): if not replaceall: self.entry.info_show( '<i>Search hit end of the document</i>', True) break except GeneratorExit as e: if replaceall: self._restore_cursor(startmark) buf.end_user_action() self.cancel() raise e if replaceall: if self.scroll_back: self._restore_cursor(startmark) buf.end_user_action() self.cancel() yield commander.commands.result.DONE
if replaceall: buf.begin_user_action() try: while True: if not replaceall: rep, words, modifier = (yield commands.result.Prompt( 'Replace next [%s]:' % (saxutils.escape(self.get_current_replace()), ))) if rep: self.set_replace(rep) bounds = utils.Struct({ 'start': buf.get_iter_at_mark(self.find_result.start), 'end': buf.get_iter_at_mark(self.find_result.end) }) # If there is a selection, replace it with the replacement string if not bounds.start.equal(bounds.end) and ( replaceall or not (modifier & gtk.gdk.CONTROL_MASK)): text = bounds.start.get_text(bounds.end) repl = self.get_replace(text) buf.begin_user_action() buf.delete(bounds.start, bounds.end) buf.insert(bounds.start, repl) buf.end_user_action() # Find next