Simplify printing and fix tabs
This commit is contained in:
parent
99c3e8cc0c
commit
d8981403ac
2 changed files with 26 additions and 21 deletions
40
editor.cpp
40
editor.cpp
|
@ -17,22 +17,18 @@ void Editor::clear_line(count_type r) {
|
|||
::move(r - file_offset, 0);
|
||||
clrtoeol();
|
||||
}
|
||||
// Print line
|
||||
void Editor::print_line(count_type r) {
|
||||
clear_line(r);
|
||||
::move(r - file_offset, 0);
|
||||
adds(file.get_line(r));
|
||||
// Print any text
|
||||
void Editor::print_text(count_type i, string text) {
|
||||
clear_line(i);
|
||||
::move(i, 0);
|
||||
adds(text);
|
||||
}
|
||||
// Print file content
|
||||
void Editor::print_file(count_type start) {
|
||||
auto lines = file.bulk_get_line(start, LINES);
|
||||
for(count_type i = 0; i < LINES; i++)
|
||||
if(i < lines.size()) {
|
||||
::move(i, 0);
|
||||
clrtoeol();
|
||||
::move(i, 0);
|
||||
adds(lines[i]);
|
||||
}
|
||||
if(i < lines.size())
|
||||
print_text(i, lines[i]);
|
||||
else
|
||||
clear_line(i);
|
||||
}
|
||||
|
@ -184,7 +180,7 @@ bool Editor::take_action() {
|
|||
case 'o':
|
||||
file.insert(file_offset + cur.r, "");
|
||||
jump_line_end();
|
||||
print_file(file_offset + cur.r);
|
||||
print_file(file_offset);
|
||||
break;
|
||||
case 'q':
|
||||
return false;
|
||||
|
@ -211,7 +207,7 @@ bool Editor::take_action() {
|
|||
}
|
||||
jump(result.second);
|
||||
}
|
||||
print_file(file_offset + cur.r);
|
||||
print_file(file_offset);
|
||||
break;
|
||||
case 's':
|
||||
last_find = get_string("to find");
|
||||
|
@ -227,23 +223,26 @@ bool Editor::take_action() {
|
|||
jump(result.second);
|
||||
replace(cur + file_offset, last_find.size(), last_replace);
|
||||
}
|
||||
print_file(file_offset + cur.r);
|
||||
print_file(file_offset);
|
||||
break;
|
||||
case 'p':
|
||||
selection.paste_selection(cur + file_offset);
|
||||
print_file(file_offset + cur.r);
|
||||
print_file(file_offset);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case INSERT:
|
||||
// TODO remake insert mode for better undo
|
||||
switch(ch) {
|
||||
case ESC:
|
||||
mode = NORMAL;
|
||||
// Change current row into current_insert TODO
|
||||
print_line(file_offset + cur.r);
|
||||
break;
|
||||
case BS:
|
||||
// Modify current_insert TODO
|
||||
if(cur.c > 0) {
|
||||
cur.c -= 1;
|
||||
file.remove(cur + file_offset);
|
||||
|
@ -251,17 +250,20 @@ bool Editor::take_action() {
|
|||
print_line(file_offset + cur.r);
|
||||
break;
|
||||
case ENTER:
|
||||
// Modify current_insert and update TODO
|
||||
file.split_line(cur + file_offset);
|
||||
print_file(file_offset + cur.r++);
|
||||
print_file(file_offset);
|
||||
cur.r++;
|
||||
jump_line_end();
|
||||
break;
|
||||
case '\t':
|
||||
// Modify current_insert TODO
|
||||
file.insert(cur + file_offset, "\t");
|
||||
cur.c += (cur.c - 1) % TAB_SIZE + 1;
|
||||
cur.c += TAB_SIZE - (cur.c % TAB_SIZE);
|
||||
print_line(file_offset + cur.r);
|
||||
break;
|
||||
default:
|
||||
// TODO remake for better undo
|
||||
// Modify current_insert TODO
|
||||
file.insert(cur + file_offset, string(1, ch));
|
||||
cur.c += 1;
|
||||
print_line(file_offset + cur.r);
|
||||
|
@ -286,7 +288,7 @@ bool Editor::take_action() {
|
|||
selection.remove_selection(cur + file_offset);
|
||||
if(file_offset + cur.r >= file.size())
|
||||
jump(cur + file_offset);
|
||||
print_file();
|
||||
print_file(file_offset);
|
||||
mode = NORMAL;
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -96,15 +96,17 @@ class Editor{
|
|||
string filename;
|
||||
Treap file;
|
||||
|
||||
// Selection
|
||||
// Editation steps
|
||||
string last_find, last_replace;
|
||||
Selection selection;
|
||||
string current_insert;
|
||||
|
||||
void save();
|
||||
|
||||
// Printing
|
||||
void clear_line(count_type r);
|
||||
void print_line(count_type r);
|
||||
void print_text(count_type i, string text);
|
||||
void print_line(count_type r) { print_text(r - file_offset, file.get_line(r)); }
|
||||
|
||||
// User input
|
||||
void print_input(string text);
|
||||
|
@ -134,6 +136,7 @@ public:
|
|||
file = _file;
|
||||
|
||||
selection = Selection(&file);
|
||||
current_insert = "";
|
||||
}
|
||||
void print_file(count_type start = 0);
|
||||
|
||||
|
|
Loading…
Reference in a new issue