From 2b172d6e961181d18bf8e23e8906ed9a384ecc1e Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 21 Mar 2016 13:38:33 +0100 Subject: [PATCH] Fixes a segmentation fault caused by Gtk::TextView::on_key_press_event. Have once in a while experienced this issue, but only on OS X --- src/source.cc | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/source.cc b/src/source.cc index 6de9cda..564fbde 100644 --- a/src/source.cc +++ b/src/source.cc @@ -1283,9 +1283,27 @@ bool Source::View::on_key_press_event_basic(GdkEventKey* key) { return true; } - bool stop=Gsv::View::on_key_press_event(key); + //Workaround for TextView::on_key_press_event bug sometimes causing segmentation faults TODO: figure out the bug and create pull request to gtk + //Note: valgrind reports issues on TextView::on_key_press_event as well + auto unicode=gdk_keyval_to_unicode(key->keyval); + if(unicode>=32 && unicode!=127) { + if(get_buffer()->get_has_selection()) { + Gtk::TextIter selection_start, selection_end; + get_buffer()->get_selection_bounds(selection_start, selection_end); + get_buffer()->erase(selection_start, selection_end); + } + get_buffer()->insert_at_cursor(Glib::ustring(1, unicode)); + get_source_buffer()->end_user_action(); + + //Trick to make the cursor visible right after insertion: + set_cursor_visible(false); + set_cursor_visible(); + + return true; + } + get_source_buffer()->end_user_action(); - return stop; + return Gsv::View::on_key_press_event(key); } //Bracket language indentation