Ejemplo n.º 1
0
def replace_all(view, is_selection):
    matcher = get_matcher(view, *get_find_params(view))
    replace = unicode(active_replace_widgets[view].entry.get_text())

    if not matcher:
        return

    buf = view.get_buffer()
    if is_selection:
        start, end = buf.get_selection_bounds()
        start.order(end)
    else:
        start, end = buf.get_bounds()

    end_mark = buf.create_mark(None, end)

    cursor = buf.get_iter_at_mark(buf.get_insert())
    line, offset = cursor.get_line(), cursor.get_line_offset()

    count = 0
    it = start
    with text_buffer_user_action(buf):
        while True:
            it = find_search_tag(view, it, False)
            if not it or it.compare(buf.get_iter_at_mark(end_mark)) > 0:
                break

            do_replace(view, matcher, it, replace)
            it = buf.get_iter_at_mark(buf.get_insert())
            count += 1

    if not count:
        view.get_toplevel().message('Nothing to replace', 'info', parent=view)
    elif count == 1:
        view.get_toplevel().message('One occurrence was replaced', 'done', parent=view)
    else:
        view.get_toplevel().message('%d occurrences were replaced' % count, 'done', parent=view)

    cursor = buf.get_iter_at_mark(buf.get_insert())
    cursor.set_line(line)
    cursor.set_line_offset(offset)
    buf.place_cursor(cursor)
    scroll_to_buffer_cursor(view)
    view.grab_focus()
Ejemplo n.º 2
0
def replace_next(view):
    buf = view.get_buffer()
    cursor = get_leftmost_cursor(buf)
    it = find_search_tag(view, cursor, True)
    if it:
        view.grab_focus()
        if it.equal(cursor):
            matcher = get_matcher(view, *get_find_params(view))
            replace = unicode(active_replace_widgets[view].entry.get_text())
            with text_buffer_user_action(buf):
                do_replace(view, matcher, it, replace)

            it = find_search_tag(view, buf.get_iter_at_mark(buf.get_insert()), True)

        if it:
            buf.place_cursor(it)

        scroll_to_buffer_cursor(view)
    else:
        view.get_toplevel().message('Replace what?', 'warn', parent=view)