#pragma once #include #include #include #include #include #define TAB_SIZE 2 using std::string; using std::vector; typedef unsigned long long count_type; // Position struct position { count_type r, c; position operator+(count_type offset) { return {r + offset, c}; } }; //// Treap // Treap node representation of a line struct line { count_type priority, size; string text; line *left, *right; line(count_type _p, string _t) : priority(_p), text(_t), size(1), left(nullptr), right(nullptr) {} }; typedef std::pair two_lines; // Treap data structure class Treap { line* root; count_type get_size(line* l); two_lines split(line *l, count_type k); line* join(line *a, line *b); line* join(two_lines two) { return join(two.first, two.second); } line* find(line *l, count_type k); line* find(count_type k); count_type bulk_find(vector *vec, line *l, count_type k, count_type count); string substitute_tabs(string s); public: // General operations Treap() { srand(120); root = nullptr; } count_type size() { return get_size(root); } void clear(); // Line operations string get_line(count_type r, bool substitute_tab = 1); vector bulk_get_line(count_type r, count_type count); count_type get_tab_offset(position p); void split_line(position p); void insert(count_type k, string s); void remove(count_type k); // Substring operations void insert(position p, string t); void remove(position p, count_type len = 1); }; //// Selection class Selection { Treap *file; Treap content; // selection template void apply_on_selection(position p, F func, G func2); public: Selection() {} Selection(Treap *_file) { file = _file; } position pos; // last selection position void remove_selection(position p); void copy_selection(position p); void paste_selection(position p); }; //// Editor #define ESC 27 #define ENTER 10 #define BS 127 #define adds(s) addstr(s.c_str()) enum mode_type { INSERT, NORMAL, SELECT }; class Editor{ // Editor state mode_type mode; position cur; count_type file_offset; // File string filename; Treap file; // Selection string last_find, last_replace; Selection selection; void save(); // Printing void clear_line(count_type r); void print_line(count_type r); // User input void print_input(string text); template string get_input(string prompt, F func); string get_string(string prompt); count_type get_number(string prompt); // Movement void jump_line_end(); void move(position p) { ::move(p.r, p.c); } void move_cursor(char ch); void jump(count_type r); void jump(position p) { jump(p.r); cur.c = p.c; } // Find and replace std::pair find(string text); void replace(position p, count_type length, string text) { file.remove(p, length); file.insert(p, text); } public: Editor(string _filename, Treap &_file) { mode = NORMAL; cur = {0, 0}; file_offset = 0; filename = _filename; file = _file; selection = Selection(&file); } void print_file(count_type start = 0); // The big function bool take_action(); };