diff --git a/src/source.cc b/src/source.cc index 5d1bbab..5f98680 100644 --- a/src/source.cc +++ b/src/source.cc @@ -132,6 +132,55 @@ void Source::View::replace_all(const std::string &replacement) { gtk_source_search_context_replace_all(search_context, replacement.c_str(), replacement.size(), NULL); } +void Source::View::paste() { + Gtk::Clipboard::get()->request_text([this](const Glib::ustring& text){ + const std::regex spaces_regex(std::string("^(")+Singleton::Config::source()->tab_char+"*)(.*)$"); + auto line=get_line_before_insert(); + std::smatch sm; + std::string prefix; + if(std::regex_match(line, sm, spaces_regex) && sm[2].str().size()==0) { + prefix=sm[1].str(); + + Glib::ustring::size_type start_line=0; + Glib::ustring::size_type end_line=0; + bool paste_line=false; + bool first_paste_line=true; + size_t tabs=0; + get_source_buffer()->begin_user_action(); + for(Glib::ustring::size_type c=0;ctab_char) + tabs++; + else + break; + } + get_buffer()->insert_at_cursor(text.substr(start_line+tabs, end_line-start_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; + } + } + get_source_buffer()->end_user_action(); + } + else + get_buffer()->paste_clipboard(Gtk::Clipboard::get()); + }); +} + void Source::View::set_status(const std::string &status) { this->status=status; if(on_update_status) diff --git a/src/source.h b/src/source.h index dba74ec..5d0df3a 100644 --- a/src/source.h +++ b/src/source.h @@ -56,6 +56,8 @@ namespace Source { void replace_forward(const std::string &replacement); void replace_backward(const std::string &replacement); void replace_all(const std::string &replacement); + + void paste(); boost::filesystem::path file_path; diff --git a/src/window.cc b/src/window.cc index bce688b..cbdccfb 100644 --- a/src/window.cc +++ b/src/window.cc @@ -138,7 +138,7 @@ void Window::create_menu() { if(auto entry=dynamic_cast(widget)) entry->paste_clipboard(); else if(notebook.get_current_page()!=-1) - notebook.get_current_view()->get_buffer()->paste_clipboard(Gtk::Clipboard::get()); + notebook.get_current_view()->paste(); }); menu.action_group->add(Gtk::Action::create("EditFind", "Find"), Gtk::AccelKey(menu.key_map["edit_find"]), [this]() { search_and_replace_entry();