Beispiel #1
0
    def insert_list_item(self, text_view, _data=None):
        """Insert list item or mark a selection as list item"""

        text_buffer = text_view.get_buffer()
        if text_buffer.get_has_selection():
            (start, end) = text_buffer.get_selection_bounds()
            if start.starts_line():
                with user_action(text_buffer):
                    text = text_buffer.get_text(start, end, False)
                    if text.startswith(("- ", "* ", "+ ")):
                        delete_end = start.copy()
                        delete_end.forward_chars(2)
                        text_buffer.delete(start, delete_end)
                    else:
                        text_buffer.insert(start, "- ")
        else:
            helptext = _("Item")
            text_length = len(helptext)

            cursor_mark = text_buffer.get_insert()
            cursor_iter = text_buffer.get_iter_at_mark(cursor_mark)

            start_ext = cursor_iter.copy()
            start_ext.backward_lines(3)
            text = text_buffer.get_text(cursor_iter, start_ext, False)
            lines = text.splitlines()

            with user_action(text_buffer):
                for line in reversed(lines):
                    if line and line.startswith(("- ", "* ", "+ ")):
                        if cursor_iter.starts_line():
                            text_buffer.insert_at_cursor(line[:2] + helptext)
                        else:
                            text_buffer.insert_at_cursor("\n" + line[:2] +
                                                         helptext)
                        break
                    else:
                        if not lines[-1] and not lines[-2]:
                            text_buffer.insert_at_cursor("- " + helptext)
                        elif not lines[-1]:
                            if cursor_iter.starts_line():
                                text_buffer.insert_at_cursor("- " + helptext)
                            else:
                                text_buffer.insert_at_cursor("\n- " + helptext)
                        else:
                            text_buffer.insert_at_cursor("\n\n- " + helptext)
                        break

            self.__select_text(text_view, 0, text_length)
Beispiel #2
0
    def set_text(self, text):
        """Set text and clear undo history"""

        text_buffer = self.get_buffer()
        with user_action(text_buffer):
            text_buffer.set_text(text)
        self.undo_redo.clear()
Beispiel #3
0
 def replace(self, searchindex, _inloop=False):
     with user_action(self.textbuffer):
         self.__do_replace(self.matches[searchindex])
     active = self.active
     self.search(scroll=False)
     self.active = active
     self.scrollto(self.active)
Beispiel #4
0
    def insert_horizontal_rule(self, text_view, _data=None):
        """Insert horizontal rule"""

        text_buffer = text_view.get_buffer()
        with user_action(text_buffer):
            text_buffer.insert_at_cursor("\n\n---\n")
        text_view.scroll_mark_onscreen(text_buffer.get_insert())
Beispiel #5
0
    def _on_shift_tab(self):
        """Delete last character if it is a tab"""
        text_buffer = self.get_buffer()
        pen_iter = text_buffer.get_end_iter()
        pen_iter.backward_char()
        end_iter = text_buffer.get_end_iter()

        if pen_iter.get_char() == "\t":
            with user_action(text_buffer):
                text_buffer.delete(pen_iter, end_iter)
Beispiel #6
0
    def insert_header(self, text_view, _data=None):
        """Insert header or mark a selection as a list header"""

        text_buffer = text_view.get_buffer()
        with user_action(text_buffer):
            if text_buffer.get_has_selection():
                (start, end) = text_buffer.get_selection_bounds()
                text = text_buffer.get_text(start, end, False)
                text_buffer.delete(start, end)
            else:
                text = _("Header")

            text_buffer.insert_at_cursor("#" + " " + text)

        self.__select_text(text_view, 0, len(text))
Beispiel #7
0
    def __wrap(text_view, wrap, helptext=""):
        """Inserts wrap format to the selected text (helper text when nothing selected)"""
        text_buffer = text_view.get_buffer()
        with user_action(text_buffer):
            if text_buffer.get_has_selection():
                # Find current highlighting
                (start, end) = text_buffer.get_selection_bounds()
                moved = False
                if (start.get_offset() >= len(wrap) and end.get_offset() <=
                        text_buffer.get_char_count() - len(wrap)):
                    moved = True
                    ext_start = start.copy()
                    ext_start.backward_chars(len(wrap))
                    ext_end = end.copy()
                    ext_end.forward_chars(len(wrap))
                    text = text_buffer.get_text(ext_start, ext_end, True)
                else:
                    text = text_buffer.get_text(start, end, True)

                if moved and text.startswith(wrap) and text.endswith(wrap):
                    text = text[len(wrap):-len(wrap)]
                    new_text = text
                    text_buffer.delete(ext_start, ext_end)
                    move_back = 0
                else:
                    if moved:
                        text = text[len(wrap):-len(wrap)]
                    new_text = text.lstrip().rstrip()
                    text = text.replace(new_text, wrap + new_text + wrap)

                    text_buffer.delete(start, end)
                    move_back = len(wrap)

                text_buffer.insert_at_cursor(text)
                text_length = len(new_text)

            else:
                text_buffer.insert_at_cursor(wrap + helptext + wrap)
                text_length = len(helptext)
                move_back = len(wrap)

        cursor_mark = text_buffer.get_insert()
        cursor_iter = text_buffer.get_iter_at_mark(cursor_mark)
        cursor_iter.backward_chars(move_back)
        text_buffer.move_mark_by_name('selection_bound', cursor_iter)
        cursor_iter.backward_chars(text_length)
        text_buffer.move_mark_by_name('insert', cursor_iter)
Beispiel #8
0
 def replace_all(self, _widget=None, _data=None):
     with user_action(self.textbuffer):
         for match in reversed(self.matches):
             self.__do_replace(match)
     self.search(scroll=False)