From 3c718b52f6efcf59537444afea96fc22c60ef57a Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 11 Aug 2015 13:42:12 +0200 Subject: [PATCH] Fixed crash in smart paste. --- src/source.cc | 26 +++++++++++++++++++++----- src/window.cc | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/source.cc b/src/source.cc index 9712118..bd19bb7 100644 --- a/src/source.cc +++ b/src/source.cc @@ -145,7 +145,7 @@ void Source::View::paste() { Glib::ustring::size_type end_line=0; bool paste_line=false; bool first_paste_line=true; - size_t tabs=0; + size_t first_paste_line_tabs=0; get_source_buffer()->begin_user_action(); for(Glib::ustring::size_type c=0;ctab_char) - tabs++; + first_paste_line_tabs++; else break; } - get_buffer()->insert_at_cursor(text.substr(start_line+tabs, end_line-start_line-tabs)); + get_buffer()->insert_at_cursor(text.substr(start_line+first_paste_line_tabs, end_line-start_line-first_paste_line_tabs)); + } + else { + std::string line=text.substr(start_line, end_line-start_line); + size_t paste_line_tabs=0; + for(auto chr: line) { + if(chr==Singleton::Config::source()->tab_char) + paste_line_tabs++; + else + break; + } + if(paste_line_tabs>=first_paste_line_tabs) + get_buffer()->insert_at_cursor("\n"+prefix+text.substr(start_line+first_paste_line_tabs, end_line-start_line-first_paste_line_tabs)); + else { + size_t tabs=0; + if(prefix.size()>(first_paste_line_tabs-paste_line_tabs)) + tabs=prefix.size()-(first_paste_line_tabs-paste_line_tabs); + get_buffer()->insert_at_cursor("\n"+prefix.substr(0, tabs)+text.substr(start_line+paste_line_tabs, end_line-start_line-paste_line_tabs)); + } } - else - get_buffer()->insert_at_cursor("\n"+prefix+text.substr(start_line+tabs, end_line-start_line-tabs)); start_line=end_line+1; first_paste_line=false; paste_line=false; diff --git a/src/window.cc b/src/window.cc index cbdccfb..5c14c1d 100644 --- a/src/window.cc +++ b/src/window.cc @@ -92,7 +92,7 @@ Window::Window() : box(Gtk::ORIENTATION_VERTICAL), notebook(directories), compil compile_success.connect([this](){ directories.open_folder(); - }); + }); INFO("Window created"); } // Window constructor