Automatic text insertion while typing



Hello,

I use GtkSourceView widget in my (python) project and I have a problem.
I don't know if this is correct place to ask for such question, but I'll
try.

I need to control the input while user is typing in gtksourceview
widget. This is needed for automatic indentation when user types some
special character or automatic deletion of N chars when backspace or
other key is pressed.

To achieve this I register signal "changed" for the TextBuffer:

    buffer.connect('changed', self.on_buffer_changed, view)

The callback function may look like this:

    def on_buffer_changed(self, buffer, view):
        insert_mark = buffer.get_insert()
        iter = buffer.get_iter_at_mark(insert_mark)
	iter.backward_char()
        char = iter.get_char()
        if char == '#':
            buffer.insert_at_cursor(' ')

In this example I wanted a whitespace to be inserted after the '#'
character when it is typed. If I do as written here, I get a warning:

./app.py:134: GtkWarning: Invalid text buffer iterator: either the
iterator is uninitialized, or the characters/pixbufs/widgets in the
buffer have been modified since the iterator was created.
You must use marks, character numbers, or line numbers to preserve a
position across buffer modifications.
You can apply tags and insert marks without invalidating your iterators,
but any mutation that affects 'indexable' buffer contents (contents that
can be referred to by character offset)
will invalidate all outstanding iterators
  gtk.main()

(app.py:8027): GtkSourceView-CRITICAL **: gtk_source_buffer_move_cursor:
assertion `gtk_text_iter_get_buffer (iter) == buffer' failed


I haven't found any example on the internet about such situation,
documentation didn't help too. Is there any recipes to be applied for
such do-something-while-typing situations? Thanks for help a lot.

--
Matynas Jocius
http://mjoc.sig.lt





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]