From c6d6cfec431b684eb5cad0ff35ae2433b833568f Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 30 Nov 2022 09:58:10 +0100 Subject: [PATCH] Improved paste indentation by coping or cutting with starting indentation if possible --- src/source_base.cpp | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/source_base.cpp b/src/source_base.cpp index c55dfd5..e111587 100644 --- a/src/source_base.cpp +++ b/src/source_base.cpp @@ -202,8 +202,23 @@ void Source::CommonView::cut() { Gtk::Clipboard::get()->set_text(selection); get_buffer()->erase(start, end); } - else - get_buffer()->cut_clipboard(Gtk::Clipboard::get()); + else { + Gtk::TextIter start, end; + get_buffer()->get_selection_bounds(start, end); + if(start.get_line() == end.get_line() || start.starts_line()) + get_buffer()->cut_clipboard(Gtk::Clipboard::get()); + else { // In order to improve paste indentation: copy entire starting line if only whitespaces before start iter + auto iter = start; + while(!iter.starts_line() && iter.backward_char() && (*iter == ' ' || *iter == '\t')) { + } + if(iter.starts_line()) { + Gtk::Clipboard::get()->set_text(get_buffer()->get_text(iter, end)); + get_buffer()->erase(start, end); + } + else + get_buffer()->cut_clipboard(Gtk::Clipboard::get()); + } + } keep_clipboard = true; } @@ -268,8 +283,21 @@ void Source::CommonView::copy() { } Gtk::Clipboard::get()->set_text(selection); } - else - get_buffer()->copy_clipboard(Gtk::Clipboard::get()); + else { + Gtk::TextIter start, end; + get_buffer()->get_selection_bounds(start, end); + if(start.get_line() == end.get_line() || start.starts_line()) + get_buffer()->copy_clipboard(Gtk::Clipboard::get()); + else { // In order to improve paste indentation: copy entire starting line if only whitespaces before start iter + auto iter = start; + while(!iter.starts_line() && iter.backward_char() && (*iter == ' ' || *iter == '\t')) { + } + if(iter.starts_line()) + Gtk::Clipboard::get()->set_text(get_buffer()->get_text(iter, end)); + else + get_buffer()->copy_clipboard(Gtk::Clipboard::get()); + } + } } void Source::CommonView::copy_lines() {