62 lines
1.9 KiB
C++
62 lines
1.9 KiB
C++
#include "everything.hpp"
|
|
|
|
// Selection manipulation
|
|
template <typename F, typename G>
|
|
void Selection::apply_on_selection(position p, F func, G func2) {
|
|
// Determine first and last selected position
|
|
position start = p, end = pos;
|
|
if(pos.r < p.r || (pos.r == p.r && pos.c < p.c))
|
|
start = pos, end = p;
|
|
|
|
// Clear previous selection
|
|
content.clear();
|
|
|
|
// Last line start
|
|
if(start.r < end.r)
|
|
func2(end.r, 0, end.c+1, file, &content);
|
|
|
|
// All complete lines in between
|
|
for(count_type r = end.r-1; r > start.r; --r)
|
|
func(r, file, &content);
|
|
|
|
// First line end
|
|
string start_line = file->get_line(start.r);
|
|
count_type size = ((start.r == end.r) ? end.c+1 : start_line.size()) - start.c;
|
|
func2(start.r, start.c, size, file, &content);
|
|
}
|
|
// Removing selection
|
|
void Selection::remove_selection(position p) {
|
|
apply_on_selection(p,
|
|
[](count_type r, Treap *file, Treap *sel){ file->remove(r); },
|
|
[](count_type r, count_type start_c, count_type size, Treap *file, Treap *sel) { file->remove({r, start_c}, size); }
|
|
);
|
|
}
|
|
// Copying selection
|
|
void Selection::copy_selection(position p) {
|
|
apply_on_selection(p,
|
|
[](count_type r, Treap *file, Treap *sel){ sel->insert(0, file->get_line(r)); },
|
|
[](count_type r, count_type start_c, count_type size, Treap *file, Treap *sel) { sel->insert(0, file->get_line(r).substr(start_c, size)); }
|
|
);
|
|
}
|
|
// Pasting selection
|
|
void Selection::paste_selection(position p) {
|
|
if(content.size() == 0) return;
|
|
|
|
// Insert last line inside of this
|
|
file->insert(p, content.get(content.size()-1));
|
|
if(content.size() == 1) return;
|
|
|
|
// Split line
|
|
string line = file->get_line(p.r);
|
|
string newline = line.substr(p.c, line.size());
|
|
file->remove(p, line.size()-p.c);
|
|
file->insert(p.r+1, newline);
|
|
|
|
// Insert first line in front and split
|
|
file->insert(p, content.get(0));
|
|
if(content.size() == 2) return;
|
|
|
|
// Insert lines
|
|
for(count_type i = content.size()-2; i > 0; --i)
|
|
file->insert(p.r, content.get(i));
|
|
}
|