zeed/everything.hpp

154 lines
3.5 KiB
C++

#pragma once
#include <ncurses.h>
#include <vector>
#include <fstream>
#include <iostream>
#define TAB_SIZE 8
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}; }
};
// String utilities
count_type get_tab_length(count_type c);
string substitute_tabs(string s);
count_type get_tab_offset(count_type c, string text);
//// Treap
// Treap node representation of a line
struct line {
count_type priority, size; // Treap innards
string text; // Content
line *left, *right; // Sons
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);
line* find(count_type k);
count_type bulk_find(vector<string> *vec, line *l, count_type k, count_type count);
public:
// General operations
Treap() { srand(120); root = nullptr; }
count_type size() { return get_size(root); }
void clear();
// Line get operations
string get_line(count_type r, bool substitute_tab = 1);
vector<string> bulk_get_line(count_type r, count_type count);
count_type get_tab_offset(position p) { return ::get_tab_offset(p.c, get_line(p.r, 0)); }
// Line set operations
void split_line(position p);
void insert(count_type k, string s);
void remove(count_type k);
void set_line(count_type k, string s) { find(k)->text = s; }
// Substring operations
void insert(position p, string t);
void remove(position p, count_type len = 1);
};
//// Selection
class Selection {
Treap *file;
Treap content;
template <typename F, typename G>
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;
// Editation steps
string last_find, last_replace;
Selection selection;
string current_insert;
// Write to file
void save();
// Printing
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 <typename F>
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<bool, position> 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);
current_insert = "";
print_file(0);
}
// The big function
bool take_action();
};