diff --git a/editor.cpp b/editor.cpp index f945018..a64e221 100644 --- a/editor.cpp +++ b/editor.cpp @@ -17,13 +17,6 @@ void Editor::clear_line(count_type r) { ::move(r - file_offset, 0); clrtoeol(); } -// Split line -void Editor::split_line(position p) { - 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); -} // Print line void Editor::print_line(count_type r) { clear_line(r); @@ -262,7 +255,7 @@ bool Editor::take_action() { print_line(file_offset + cur.r); break; case ENTER: - split_line(cur + file_offset); + file.split_line(cur + file_offset); print_file(file_offset + cur.r++); jump_line_end(); break; diff --git a/everything.hpp b/everything.hpp index ea71a90..c5be6db 100644 --- a/everything.hpp +++ b/everything.hpp @@ -54,6 +54,7 @@ public: void set(position p, char ch); void insert(position p, string t); void remove(position p, count_type len = 1); + void split_line(position p); }; //// Selection @@ -80,22 +81,24 @@ public: enum mode_type { INSERT, NORMAL, SELECT }; class Editor{ + // Editor state mode_type mode; - Treap file; // file representation + position cur; + count_type file_offset; + + // File string filename; - position cur; // cursor position - count_type file_offset; // terminal file offset - - // Selections - string last_find, last_replace; // last find/replace inputs + Treap file; + + // Selection + string last_find, last_replace; Selection selection; - // Write to file void save(); + // Printing void clear_line(count_type r); void print_line(count_type r); - void split_line(position p); // User input void print_input(string text); @@ -116,10 +119,12 @@ class Editor{ public: Editor(string _filename, Treap &_file) { mode = NORMAL; - filename = _filename; - file = _file; cur = {0, 0}; file_offset = 0; + + filename = _filename; + file = _file; + selection = Selection(&file); } void print_file(count_type start = 0); diff --git a/selection.cpp b/selection.cpp index df1a9c2..5139a2a 100644 --- a/selection.cpp +++ b/selection.cpp @@ -46,13 +46,8 @@ void Selection::paste_selection(position p) { 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->split_line(p); file->insert(p, content.get(0)); if(content.size() == 2) return; diff --git a/treap.cpp b/treap.cpp index 331b977..0688f5a 100644 --- a/treap.cpp +++ b/treap.cpp @@ -110,6 +110,7 @@ string Treap::get_line(count_type r, bool substitute_tab) { } return ret; } +// TODO tabs are a grid, not just N tabs count_type Treap::get_tab_offset(position p) { count_type tab_offset = 0; string line = get_line(p.r, false); @@ -130,3 +131,10 @@ void Treap::insert(position p, string t) { void Treap::remove(position p, count_type len) { find(p.r)->text.erase(p.c-get_tab_offset(p),len); } +// Split line +void Treap::split_line(position p) { + string line = get_line(p.r); + string newline = line.substr(p.c, line.size()); + remove(p, line.size()-p.c); + insert(p.r+1, newline); +}