From 64b67eeddeaab2fa0d6f7597320bbdeaf6f3038a Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 9 Sep 2020 14:17:10 +0200 Subject: [PATCH] Can now paste in terminal --- src/source_base.cpp | 6 ++--- src/terminal.cpp | 57 +++++++++++++++++++++++++++++++++++++++++---- src/terminal.hpp | 2 ++ src/window.cpp | 6 +++-- 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/src/source_base.cpp b/src/source_base.cpp index 371c0bc..62e41b0 100644 --- a/src/source_base.cpp +++ b/src/source_base.cpp @@ -797,7 +797,7 @@ void Source::BaseView::paste() { std::string text = Gtk::Clipboard::get()->wait_for_text(); - //Replace carriage returns (which leads to crash) with newlines + // Replace carriage returns (which leads to crash) with newlines for(size_t c = 0; c < text.size(); c++) { if(text[c] == '\r') { if((c + 1) < text.size() && text[c + 1] == '\n') @@ -807,7 +807,7 @@ void Source::BaseView::paste() { } } - //Exception for when pasted text is only whitespaces + // Exception for when pasted text is only whitespaces bool only_whitespaces = true; for(auto &chr : text) { if(chr != '\n' && chr != '\r' && chr != ' ' && chr != '\t') { @@ -926,7 +926,7 @@ void Source::BaseView::paste() { paste_line = false; } } - // add final newline if present in text + // Add final newline if present in text if(text.size() > 0 && text.back() == '\n') get_buffer()->insert_at_cursor('\n' + prefix_tabs); get_buffer()->end_user_action(); diff --git a/src/terminal.cpp b/src/terminal.cpp index c73c545..21a2ed7 100644 --- a/src/terminal.cpp +++ b/src/terminal.cpp @@ -584,11 +584,15 @@ bool Terminal::on_key_press_event(GdkEventKey *event) { if(processes.size() > 0 || debug_is_running) { auto unicode = gdk_keyval_to_unicode(event->keyval); if(unicode >= 32 && unicode != 126 && unicode != 0) { + if(scroll_to_bottom) + scroll_to_bottom(); get_buffer()->place_cursor(get_buffer()->end()); stdin_buffer += unicode; - get_buffer()->insert_at_cursor(stdin_buffer.substr(stdin_buffer.size() - 1)); + get_buffer()->insert_at_cursor(Glib::ustring() + unicode); } else if(event->keyval == GDK_KEY_BackSpace) { + if(scroll_to_bottom) + scroll_to_bottom(); get_buffer()->place_cursor(get_buffer()->end()); if(stdin_buffer.size() > 0 && get_buffer()->get_char_count() > 0) { auto iter = get_buffer()->end(); @@ -598,18 +602,63 @@ bool Terminal::on_key_press_event(GdkEventKey *event) { } } else if(event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter) { + if(scroll_to_bottom) + scroll_to_bottom(); get_buffer()->place_cursor(get_buffer()->end()); stdin_buffer += '\n'; + get_buffer()->insert_at_cursor("\n"); if(debug_is_running) { #ifdef JUCI_ENABLE_DEBUG - Project::current->debug_write(stdin_buffer); + Project::current->debug_write(stdin_buffer.raw()); #endif } else - processes.back()->write(stdin_buffer); - get_buffer()->insert_at_cursor(stdin_buffer.substr(stdin_buffer.size() - 1)); + processes.back()->write(stdin_buffer.raw()); stdin_buffer.clear(); } } return true; } + +void Terminal::paste() { + std::string text = Gtk::Clipboard::get()->wait_for_text(); + if(text.empty()) + return; + + // Replace carriage returns (which leads to crash) with newlines + for(size_t c = 0; c < text.size(); c++) { + if(text[c] == '\r') { + if((c + 1) < text.size() && text[c + 1] == '\n') + text.replace(c, 2, "\n"); + else + text.replace(c, 1, "\n"); + } + } + + std::string after_last_newline_str; + auto last_newline = text.rfind('\n'); + + LockGuard lock(processes_mutex); + bool debug_is_running = false; +#ifdef JUCI_ENABLE_DEBUG + debug_is_running = Project::current ? Project::current->debug_is_running() : false; +#endif + if(processes.size() > 0 || debug_is_running) { + if(scroll_to_bottom) + scroll_to_bottom(); + get_buffer()->place_cursor(get_buffer()->end()); + get_buffer()->insert_at_cursor(text); + if(last_newline != std::string::npos) { + if(debug_is_running) { +#ifdef JUCI_ENABLE_DEBUG + Project::current->debug_write(stdin_buffer.raw() + text.substr(0, last_newline + 1)); +#endif + } + else + processes.back()->write(stdin_buffer.raw() + text.substr(0, last_newline + 1)); + stdin_buffer = text.substr(last_newline + 1); + } + else + stdin_buffer += text; + } +} diff --git a/src/terminal.hpp b/src/terminal.hpp index 41d0386..0269fb9 100644 --- a/src/terminal.hpp +++ b/src/terminal.hpp @@ -36,6 +36,8 @@ public: std::function scroll_to_bottom; + void paste(); + protected: bool on_motion_notify_event(GdkEventMotion *motion_event) override; bool on_button_press_event(GdkEventButton *button_event) override; diff --git a/src/window.cpp b/src/window.cpp index ed73397..3fac2d9 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -683,13 +683,15 @@ void Window::set_menu_actions() { if(auto entry = dynamic_cast(widget)) entry->paste_clipboard(); else if(auto view = dynamic_cast(widget)) { - auto source_view = dynamic_cast(view); - if(source_view) { + if(auto source_view = dynamic_cast(view)) { source_view->disable_spellcheck = true; source_view->paste(); source_view->disable_spellcheck = false; source_view->hide_tooltips(); } + else if(auto terminal = dynamic_cast(view)) { + terminal->paste(); + } else if(view->get_editable()) view->get_buffer()->paste_clipboard(Gtk::Clipboard::get()); }