1

I'm trying to fill a SourceView's buffer with text and then scroll to make a specific line visible, like this:

lines = '....'.split('\n')
line_number = 76 # For instance, assuming lines has at least this many lines
buffer = view.get_buffer()
for line in lines:
  buffer.insert(end_iter, line + '\n')
iter = buffer.get_iter_at_line()
mark = buffer.get_mark('insert')
buffer.move_mark(mark, iter)
mark = buffer.get_mark('selection_bound')
buffer.move_mark(mark, iter)
view.scroll_to_mark(mark, 0.3, True, 0, 0.5)

This scrolls to more-or-less random places in the buffer. Is there something I'm doing wrong here? Or does this just not work?

Tom
  • 7,269
  • 1
  • 42
  • 69
  • I am not sure about `scroll_to_mark`, but `scroll_to_iter` works for me. – theGtknerd Feb 22 '18 at 18:30
  • `scroll_to_iter` doesn't work for me. I believe this is because I am calling it immediately after filling the text buffer, and `scroll_to_iter` relies on calculated line heights which are calculated in an idle handler. This is described in the documentation for `scroll_to_iter`, which suggests using `scroll_to_mark` to "avoid oddness". I just get a different type of oddness. – Tom Feb 23 '18 at 11:05
  • 1
    You have applied tags `pygtk`, `gtk3`, and `pyobject`. These are not compatible. Either it is pygtk and gtk2, or then gtk3 and pyobject. Please fix for clarity. – theGtknerd Feb 23 '18 at 12:03

1 Answers1

3

You are right, scroll_to_iter depends on the idle recalculate. For that matter, scroll_to_mark does too. This works for me:

from gi.repository import GLib
#........ code here
GLib.idle_add(view.scroll_to_mark, mark, 0.1, True, 0.0, 0.5)
theGtknerd
  • 3,647
  • 1
  • 13
  • 34
  • This seems a little strange, as gtksourceview-3 and introspection + GLib require an extra parameter - priority. Even providing that parameter as `GLib.PRIORITY_DEFAULT_IDLE`, this trick (`idle_add`) does not work. – jcoppens Jul 25 '18 at 19:14