94 lines
2.2 KiB
C++
94 lines
2.2 KiB
C++
#include <ncurses.h>
|
|
#include <curses.h>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
using std::string;
|
|
|
|
typedef unsigned long long count_type;
|
|
|
|
//// 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<line*, line*> 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); }
|
|
};
|
|
|
|
// Position
|
|
struct position {
|
|
count_type r, c;
|
|
position operator+(count_type offset) { return {r + offset, c}; }
|
|
};
|
|
|
|
|
|
//// Editor
|
|
// Editor macros
|
|
#define ESC 27
|
|
#define ENTER 10
|
|
#define BS 127
|
|
#define adds(s) addstr(s.c_str())
|
|
|
|
// 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 new_line(count_type r, string text);
|
|
void insert(position p, string t);
|
|
void insert(position p, char ch);
|
|
void remove(position p, count_type len);
|
|
void remove(count_type r);
|
|
void append(string t);
|
|
count_type get_size();
|
|
|
|
// File operations
|
|
bool load(string filename);
|
|
bool save(string filename);
|
|
|
|
// Edit operations
|
|
void clear_line(count_type r);
|
|
void print_line(count_type r);
|
|
void print_file(count_type start);
|
|
void split_line(position p);
|
|
void print_input(string text);
|
|
count_type get_number(string prompt);
|
|
string get_string(string prompt);
|
|
|
|
// Selection
|
|
class Selection {
|
|
treap content; // selection
|
|
template <typename F, typename G>
|
|
void apply_on_selection(position p, F func, G func2);
|
|
public:
|
|
position pos; // selection initial position
|
|
void remove_selection(position p);
|
|
void copy_selection(position p);
|
|
void paste_selection(position p);
|
|
};
|