diff --git a/editor.cpp b/editor.cpp index 76983ba..35de0fc 100644 --- a/editor.cpp +++ b/editor.cpp @@ -228,15 +228,15 @@ bool Editor::take_action() { case ESC: mode = NORMAL; file.set_line(cur.r, current_insert); - print_text(cur.r, Treap::substitute_tabs(current_insert)); + print_text(cur.r, substitute_tabs(current_insert)); current_insert = ""; break; case BS: if(cur.c > 0) { cur.c -= 1; - current_insert.erase(cur.c - Treap::get_tab_offset(cur.c, current_insert), 1); + current_insert.erase(cur.c - get_tab_offset(cur.c, current_insert), 1); } - print_text(cur.r, Treap::substitute_tabs(current_insert)); + print_text(cur.r, substitute_tabs(current_insert)); break; case ENTER: file.set_line(cur.r, current_insert); @@ -246,14 +246,14 @@ bool Editor::take_action() { print_file(file_offset); break; case '\t': - current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), "\t"); + current_insert.insert(cur.c - get_tab_offset(cur.c, current_insert), "\t"); cur.c += TAB_SIZE - (cur.c % TAB_SIZE); - print_text(cur.r, Treap::substitute_tabs(current_insert)); + print_text(cur.r, substitute_tabs(current_insert)); break; default: - current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), string(1, ch)); + current_insert.insert(cur.c - get_tab_offset(cur.c, current_insert), string(1, ch)); cur.c += 1; - print_text(cur.r, Treap::substitute_tabs(current_insert)); + print_text(cur.r, substitute_tabs(current_insert)); break; } break; diff --git a/everything.hpp b/everything.hpp index 125ecd2..414958f 100644 --- a/everything.hpp +++ b/everything.hpp @@ -16,6 +16,10 @@ struct position { position operator+(count_type offset) { return {r + offset, c}; } }; +// String utilities +string substitute_tabs(string s); +count_type get_tab_offset(count_type c, string text); + //// Treap // Treap node representation of a line struct line { @@ -41,9 +45,7 @@ class Treap { count_type bulk_find(vector *vec, line *l, count_type k, count_type count); public: - static string substitute_tabs(string s); - static count_type get_tab_offset(count_type c, string text); - count_type get_tab_offset(position p) { return get_tab_offset(p.c, get_line(p.r, 0)); } + count_type get_tab_offset(position p) { return ::get_tab_offset(p.c, get_line(p.r, 0)); } // General operations Treap() { srand(120); root = nullptr; } @@ -109,6 +111,7 @@ class Editor{ void clear_line(count_type i); void print_text(count_type i, string text); void print_current_line() { print_text(cur.r, file.get_line(file_offset + cur.r)); } + void print_file(count_type start); // User input template @@ -138,8 +141,8 @@ public: selection = Selection(&file); current_insert = ""; + print_file(0); } - void print_file(count_type start = 0); // The big function bool take_action(); diff --git a/main.cpp b/main.cpp index ebd0ec7..c150a6a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,31 @@ #include "everything.hpp" +// Substitute tab characters into regular tabs +string substitute_tabs(string s) { + string ret = ""; + for(count_type i = 0; i < s.size(); ++i) { + if(s[i] == '\t') { + ret += ' '; + while(ret.size() % TAB_SIZE != 0) + ret += ' '; + } + else + ret += s[i]; + } + return ret; +} +// Get how much tabs changed position on a row +count_type get_tab_offset(count_type c, string text) { + count_type tab_offset = 0; + for(count_type i = 0; i + tab_offset < c; ++i) { + if(text[i] == '\t') + tab_offset += TAB_SIZE - (i+tab_offset) % TAB_SIZE - 1; + if(i + tab_offset > c) + tab_offset -= i + tab_offset - c; + } + return tab_offset; +} + int main(int argc, char* argv[]) { @@ -29,8 +55,6 @@ int main(int argc, char* argv[]) { Editor ed(filename, file); - ed.print_file(); - // Main loop while(ed.take_action()) refresh(); diff --git a/treap.cpp b/treap.cpp index d14e82e..46fd65d 100644 --- a/treap.cpp +++ b/treap.cpp @@ -6,31 +6,6 @@ count_type Treap::get_size(line* l) { return l->size; } -string Treap::substitute_tabs(string s) { - string ret = ""; - for(count_type i = 0; i < s.size(); ++i) { - if(s[i] == '\t') { - ret += ' '; - while(ret.size() % TAB_SIZE != 0) - ret += ' '; - } - else - ret += s[i]; - } - return ret; -} -// Get how much tabs changed position on a row -count_type Treap::get_tab_offset(count_type c, string text) { - count_type tab_offset = 0; - for(count_type i = 0; i + tab_offset < c; ++i) { - if(text[i] == '\t') - tab_offset += TAB_SIZE - (i+tab_offset) % TAB_SIZE - 1; - if(i + tab_offset > c) - tab_offset -= i + tab_offset - c; - } - return tab_offset; -} - // Split treap by k-th element two_lines Treap::split(line *l, count_type k) { if(l == nullptr) return {nullptr, nullptr};