#include #include #include #include #define TAB_SIZE 2 using std::string; 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); public: Treap() { srand(120); root = nullptr; } line* find(count_type k); string get(count_type k) { return find(k)->text; } void clear(); void insert(count_type k, string s); void append(string s) { insert(size(), s); } void remove(count_type k); void pop() { remove(size()); } count_type size() { return get_size(root); } // Treap access functions string get_line(count_type r, bool substitute_tab = 1); count_type get_tab_offset(position p); 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 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; // selection initial 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); count_type get_number(string prompt); string get_string(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(); };