gtkgtktextview

Multiple cursors with same gtkTextBuffer in different gtkTextView


I am trying to edit the same text (which I have stored in a GtkTextBuffer, but alternatives are welcome) with multiple cursors, each in a separate GtkTextView. I need all cursors to be able to edit the text (and be reflected in the other views). Moving one cursor (e.g., with the keyboard) should not move the other cursors.

For those familiar, I am trying to emulate the behaviour of emacs' split window.

What is the simplest way to do this?

Background

The Text Widget Overview states that

Each buffer can be displayed by any number of views.

except the cursor is stored in the GtkTextBuffer rather than the GtkTextView. In particular, moving the cursor in one view would change the cursor location in all other views.

More technical details

I'm actually using pygtk and gtksourceview2 in my specific example. In particular, the GtkTextView is a GtkSourceView (gtksourceview2.View in python). But my question is not pygtk specific.


Solution

  • You can't do that with GtkSourceView out of the box - you'll have to write it yourself, unfortunately.

    The way I would go about writing that, is to subclass GtkSourceBuffer. Make it 'wrap' the original GtkSourceBuffer, but maintain its own separate cursor position. Then create two of these 'wrapping' source buffers and put them in the source views.

    Although, that wouldn't work if you wanted to have all the cursors displayed in all the source views. Maybe you could subclass GtkSourceBuffer and just have it maintain a separate cursor position for each view it's added to.

    What would you do if you selected a different bit of text in each view? I'd think about it carefully before you start coding.

    A quick'n'dirty way would be to connect to the 'focus-in-event' and 'focus-out-event' signals of the source views. On losing the focus, have the view record its current cursor position. Then on gaining the focus, have it restore that cursor position. That way, it would be almost like each view had its own cursor position.