Browse Source

Slight cleanup of copy and cut text

merge-requests/413/head
eidheim 3 years ago
parent
commit
50291c2fbe
  1. 49
      src/source_base.cpp
  2. 2
      src/source_base.hpp

49
src/source_base.cpp

@ -205,21 +205,15 @@ void Source::CommonView::cut() {
else {
Gtk::TextIter start, end;
get_buffer()->get_selection_bounds(start, end);
if(start.get_line() == end.get_line() || start.starts_line())
if(start.starts_line() && end.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;
}
else if(copy_with_first_line_indentation())
get_buffer()->erase(start, end);
else
get_buffer()->cut_clipboard(Gtk::Clipboard::get());
}
keep_clipboard = true;
}
void Source::CommonView::cut_lines() {
@ -283,21 +277,8 @@ void Source::CommonView::copy() {
}
Gtk::Clipboard::get()->set_text(selection);
}
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());
}
}
else if(!copy_with_first_line_indentation())
get_buffer()->copy_clipboard(Gtk::Clipboard::get());
}
void Source::CommonView::copy_lines() {
@ -326,6 +307,20 @@ void Source::CommonView::copy_lines() {
Gtk::Clipboard::get()->set_text(get_buffer()->get_text(start, end));
}
bool Source::CommonView::copy_with_first_line_indentation() {
Gtk::TextIter start, end;
get_buffer()->get_selection_bounds(start, end);
auto line_start = start;
while(!line_start.starts_line() && line_start.backward_char() && (*line_start == ' ' || *line_start == '\t')) {
}
if(!line_start.starts_line())
return false;
Gtk::Clipboard::get()->set_text(get_buffer()->get_text(line_start, end));
return true;
}
Source::BaseView::BaseView(const boost::filesystem::path &file_path, const Glib::RefPtr<Gsv::Language> &language) : CommonView(language), file_path(file_path), status_diagnostics(0, 0, 0) {
get_style_context()->add_class("juci_source_view");

2
src/source_base.hpp

@ -73,6 +73,8 @@ namespace Source {
GtkSourceSearchContext *search_context;
GtkSourceSearchSettings *search_settings;
static void search_occurrences_updated(GtkWidget *widget, GParamSpec *property, gpointer data);
bool copy_with_first_line_indentation();
};
class BaseView : public CommonView {

Loading…
Cancel
Save