From fe621726829e0607f3748f34e221972f0a8ba300 Mon Sep 17 00:00:00 2001 From: Matuush Date: Wed, 16 Apr 2025 21:12:54 +0200 Subject: [PATCH] Unite everything with file_offset --- main.cpp | 91 ++++++++++++++++++++++++++++--------------------------- treap.hpp | 3 ++ 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/main.cpp b/main.cpp index 79bfa84..26a7926 100644 --- a/main.cpp +++ b/main.cpp @@ -13,7 +13,10 @@ using std::string; #define adds(s) addstr(s.c_str()) enum mode_type { INSERT, NORMAL, SELECT }; -struct position { count_type r, c; }; +struct position { + count_type r, c; + position operator+(count_type offset) { return {r + offset, c}; } +}; // Global variables treap file; @@ -23,23 +26,24 @@ position clp; treap selection; // Accessing the file -string get_line(count_type r, bool clean = true) { - string line = (*file.find(file_offset+r)).text; - if(!clean) +string get_line(count_type r, bool substitute_tab = true) { + string line = file.get(r); + if(!substitute_tab) return line; for(count_type i = 0; i < line.size(); ++i) if(line[i] == '\t') line[i] = ' '; return line; } -void set(position p, char ch) { file.find(file_offset+p.r)->text[p.c] = ch; } -void new_line(count_type r, string text = "") { file.insert(file_offset+r, text); } -void insert(position p, string t) { file.find(file_offset+p.r)->text.insert(p.c, t); } +void set(position p, char ch) { file.find(p.r)->text[p.c] = ch; } +void new_line(count_type r, string text = "") { file.insert(r, text); } +void insert(position p, string t) { file.find(p.r)->text.insert(p.c, t); } void insert(position p, char ch) { insert(p, string{ch}); } +void remove(position p, count_type len=1) { file.find(p.r)->text.erase(p.c,len); } +void remove(count_type r) { file.remove(r); } void append(string t) { file.append(t); } -void remove(position p, count_type len=1) { file.find(file_offset+p.r)->text.erase(p.c,len); } -void remove(count_type r) { file.remove(file_offset+r); } +// Taking user input count_type get_number() { char ch = '0'; string s = ""; @@ -68,26 +72,26 @@ bool save(string filename) { return 1; for(count_type i = 0; i < file.size(); ++i) - outfile << get_line(i-file_offset, 0) << std::endl; + outfile << get_line(i, 0) << std::endl; return 0; } // Clear line void clear_line(count_type r) { - move(r, 0); + move(r - file_offset, 0); clrtoeol(); } // Print line void print_line(count_type r) { clear_line(r); - move(r, 0); + move(r - file_offset, 0); adds(get_line(r)); } // Print file content void print_file(count_type start = 0) { - for(count_type i = start; i < LINES; i++) - if(file_offset+i < file.size()) + for(count_type i = start; i < file_offset + LINES; i++) + if(i < file.size()) print_line(i); else clear_line(i); @@ -101,9 +105,9 @@ void split_line(position p) { } // Copying -void copy_selection() { +void copy_selection(position p = cur + file_offset) { // Determine first and last selected position - position start = cur, end = clp; + position start = p, end = clp; if(clp.r < cur.r || (clp.r == cur.r && clp.c < cur.c)) start = clp, end = cur; @@ -128,30 +132,29 @@ void copy_selection() { } } // Pasting -void paste_selection(position p = cur) { +void paste_selection(position p = cur + file_offset) { if(selection.size() == 0) return; // Insert last line inside of this - insert(p, selection.find(selection.size()-1)->text); + insert(p, selection.get(selection.size()-1)); if(selection.size() == 1) return; // Insert first line in front and split split_line(p); - insert(p, selection.find(0)->text); + insert(p, selection.get(0)); if(selection.size() == 2) return; // Insert lines for(count_type i = selection.size()-2; i > 0; --i) - new_line(p.r, selection.find(i)->text); + new_line(p.r, selection.get(i)); } -// TODO hledání a nahrazování // TODO undo // Jump to end of line void jump_line_end() { - count_type line_size = get_line(cur.r).size(); + count_type line_size = get_line(file_offset+cur.r).size(); if(cur.c > line_size) cur.c = line_size; } @@ -195,7 +198,7 @@ void move_cursor(char ch) { break; case 'l': - if(cur.c < get_line(cur.r).size()) { + if(cur.c < get_line(file_offset+cur.r).size()) { cur.c += 1; move(cur); } @@ -246,26 +249,26 @@ int main(int argc, char* argv[]) { char ch = getch(); switch(mode) { case NORMAL: - print_line(cur.r); + print_line(file_offset + cur.r); switch(ch) { case 'h': case 'j': case 'k': case 'l': move_cursor(ch); break; case 'g': jump(get_number()); - print_file(); + print_file(file_offset); break; case 'd': - remove(cur.r); - print_file(cur.r); + remove(file_offset + cur.r); + print_file(file_offset + cur.r); break; case 'x': - remove(cur); - print_line(cur.r); + remove(cur + file_offset); + print_line(file_offset + cur.r); break; case 'o': - new_line(cur.r); - print_file(cur.r); + new_line(file_offset + cur.r); + print_file(file_offset + cur.r); break; case 'q': run = false; @@ -278,11 +281,11 @@ int main(int argc, char* argv[]) { break; case 'v': mode = SELECT; - clp = cur; + clp = cur + file_offset; break; case 'p': - paste_selection(); - print_file(cur.r); + paste_selection(cur + file_offset); + print_file(file_offset + cur.r); break; default: break; @@ -292,39 +295,39 @@ int main(int argc, char* argv[]) { switch(ch) { case ESC: mode = NORMAL; - print_line(cur.r); + print_line(file_offset + cur.r); break; case BS: if(cur.c > 0) { cur.c -= 1; - remove(cur); + remove(cur + file_offset); } - print_line(cur.r); + print_line(file_offset + cur.r); break; case ENTER: - split_line(cur); - print_file(cur.r++); + split_line(cur + file_offset); + print_file(file_offset + cur.r++); jump_line_end(); break; default: - insert(cur, ch); + insert(cur + file_offset, ch); cur.c += 1; - print_line(cur.r); + print_line(file_offset + cur.r); break; } break; case SELECT: - print_line(cur.r); + print_line(file_offset + cur.r); switch(ch) { case 'h': case 'j': case 'k': case 'l': move_cursor(ch); break; case 'g': jump(get_number()); - print_file(); + print_file(file_offset); break; case 'v': - copy_selection(); + copy_selection(cur + file_offset); mode = NORMAL; break; default: diff --git a/treap.hpp b/treap.hpp index 6beb16c..b12bf66 100644 --- a/treap.hpp +++ b/treap.hpp @@ -90,6 +90,9 @@ public: // Don't find index k, but k-th line -> +1 return find(root, k+1); } + string get(count_type k) { + return find(k)->text; + } void clear() { while(size() > 0) { auto two = split(root, 1);