Implement copying and pasting
This commit is contained in:
parent
079e7669af
commit
76eac35213
1 changed files with 74 additions and 1 deletions
75
main.cpp
75
main.cpp
|
@ -18,6 +18,8 @@ enum mode_type { INSERT, NORMAL, SELECT };
|
|||
treap file;
|
||||
int row = 0, col = 0;
|
||||
int file_offset = 0;
|
||||
int clp_r, clp_c;
|
||||
treap selection;
|
||||
|
||||
// Accessing the file
|
||||
string get_line(int r) {
|
||||
|
@ -96,8 +98,51 @@ void split_line(int r, int s) {
|
|||
new_line(r+1, newline);
|
||||
}
|
||||
|
||||
// Copying
|
||||
void copy_selection() {
|
||||
// Determine first and last selected position
|
||||
int start_r = row, start_c = col, end_r = clp_r, end_c = clp_c;
|
||||
if(clp_r < row || (clp_r == row && clp_c < col))
|
||||
start_r = clp_r, start_c = clp_c, end_r = row, end_c = col;
|
||||
|
||||
// Clear previous selection
|
||||
selection.clear();
|
||||
|
||||
// Add all complete lines in between
|
||||
for(int r = start_r+1; r < end_r; ++r)
|
||||
selection.append(get_line(r));
|
||||
|
||||
// Add first line end
|
||||
string start_line = get_line(start_r);
|
||||
int size = ((start_r == end_r) ? end_c+1 : start_line.size()) - start_c;
|
||||
string start = start_line.substr(start_c, size);
|
||||
selection.insert(0, start);
|
||||
|
||||
// Add last line start
|
||||
if(start_r < end_r) {
|
||||
string end_line = get_line(end_r);
|
||||
string end = end_line.substr(0, end_c+1);
|
||||
selection.append(end);
|
||||
}
|
||||
}
|
||||
// Pasting
|
||||
void paste_selection() {
|
||||
if(selection.size() == 0) return;
|
||||
|
||||
// Insert last line inside of this
|
||||
insert(row, col, selection.find(selection.size()-1)->text);
|
||||
if(selection.size() == 1) return;
|
||||
|
||||
// Insert first line in front and split
|
||||
split_line(row, col);
|
||||
insert(row, col, selection.find(0)->text);
|
||||
if(selection.size() == 2) return;
|
||||
|
||||
// Insert lines
|
||||
for(int i = selection.size()-2; i > 0; --i)
|
||||
new_line(row, selection.find(i)->text);
|
||||
}
|
||||
|
||||
// TODO kopirování
|
||||
// TODO hledání a nahrazování
|
||||
// TODO undo
|
||||
|
||||
|
@ -208,6 +253,14 @@ int main(int argc, char* argv[]) {
|
|||
case 'i':
|
||||
mode = INSERT;
|
||||
break;
|
||||
case 'v':
|
||||
mode = SELECT;
|
||||
clp_r = row, clp_c = col;
|
||||
break;
|
||||
case 'p':
|
||||
paste_selection();
|
||||
print_file(row);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -234,6 +287,26 @@ int main(int argc, char* argv[]) {
|
|||
break;
|
||||
}
|
||||
break;
|
||||
case SELECT:
|
||||
print_line(row);
|
||||
switch(ch) {
|
||||
case 'h': case 'j': case 'k': case 'l':
|
||||
move_cursor(ch);
|
||||
break;
|
||||
case 'g':
|
||||
jump(get_number());
|
||||
print_file();
|
||||
break;
|
||||
case 'v':
|
||||
copy_selection();
|
||||
mode = NORMAL;
|
||||
break;
|
||||
default:
|
||||
addch('d');
|
||||
mode = NORMAL;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue