Make get_tab_offset and substitute_tabs global
This commit is contained in:
parent
598932c9ef
commit
7391e5085a
4 changed files with 40 additions and 38 deletions
14
editor.cpp
14
editor.cpp
|
@ -228,15 +228,15 @@ bool Editor::take_action() {
|
|||
case ESC:
|
||||
mode = NORMAL;
|
||||
file.set_line(cur.r, current_insert);
|
||||
print_text(cur.r, Treap::substitute_tabs(current_insert));
|
||||
print_text(cur.r, substitute_tabs(current_insert));
|
||||
current_insert = "";
|
||||
break;
|
||||
case BS:
|
||||
if(cur.c > 0) {
|
||||
cur.c -= 1;
|
||||
current_insert.erase(cur.c - Treap::get_tab_offset(cur.c, current_insert), 1);
|
||||
current_insert.erase(cur.c - get_tab_offset(cur.c, current_insert), 1);
|
||||
}
|
||||
print_text(cur.r, Treap::substitute_tabs(current_insert));
|
||||
print_text(cur.r, substitute_tabs(current_insert));
|
||||
break;
|
||||
case ENTER:
|
||||
file.set_line(cur.r, current_insert);
|
||||
|
@ -246,14 +246,14 @@ bool Editor::take_action() {
|
|||
print_file(file_offset);
|
||||
break;
|
||||
case '\t':
|
||||
current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), "\t");
|
||||
current_insert.insert(cur.c - get_tab_offset(cur.c, current_insert), "\t");
|
||||
cur.c += TAB_SIZE - (cur.c % TAB_SIZE);
|
||||
print_text(cur.r, Treap::substitute_tabs(current_insert));
|
||||
print_text(cur.r, substitute_tabs(current_insert));
|
||||
break;
|
||||
default:
|
||||
current_insert.insert(cur.c - Treap::get_tab_offset(cur.c, current_insert), string(1, ch));
|
||||
current_insert.insert(cur.c - get_tab_offset(cur.c, current_insert), string(1, ch));
|
||||
cur.c += 1;
|
||||
print_text(cur.r, Treap::substitute_tabs(current_insert));
|
||||
print_text(cur.r, substitute_tabs(current_insert));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -16,6 +16,10 @@ struct position {
|
|||
position operator+(count_type offset) { return {r + offset, c}; }
|
||||
};
|
||||
|
||||
// String utilities
|
||||
string substitute_tabs(string s);
|
||||
count_type get_tab_offset(count_type c, string text);
|
||||
|
||||
//// Treap
|
||||
// Treap node representation of a line
|
||||
struct line {
|
||||
|
@ -41,9 +45,7 @@ class Treap {
|
|||
count_type bulk_find(vector<string> *vec, line *l, count_type k, count_type count);
|
||||
|
||||
public:
|
||||
static string substitute_tabs(string s);
|
||||
static count_type get_tab_offset(count_type c, string text);
|
||||
count_type get_tab_offset(position p) { return get_tab_offset(p.c, get_line(p.r, 0)); }
|
||||
count_type get_tab_offset(position p) { return ::get_tab_offset(p.c, get_line(p.r, 0)); }
|
||||
|
||||
// General operations
|
||||
Treap() { srand(120); root = nullptr; }
|
||||
|
@ -109,6 +111,7 @@ class Editor{
|
|||
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>
|
||||
|
@ -138,8 +141,8 @@ public:
|
|||
|
||||
selection = Selection(&file);
|
||||
current_insert = "";
|
||||
print_file(0);
|
||||
}
|
||||
void print_file(count_type start = 0);
|
||||
|
||||
// The big function
|
||||
bool take_action();
|
||||
|
|
28
main.cpp
28
main.cpp
|
@ -1,5 +1,31 @@
|
|||
#include "everything.hpp"
|
||||
|
||||
// Substitute tab characters into regular tabs
|
||||
string substitute_tabs(string s) {
|
||||
string ret = "";
|
||||
for(count_type i = 0; i < s.size(); ++i) {
|
||||
if(s[i] == '\t') {
|
||||
ret += ' ';
|
||||
while(ret.size() % TAB_SIZE != 0)
|
||||
ret += ' ';
|
||||
}
|
||||
else
|
||||
ret += s[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
// Get how much tabs changed position on a row
|
||||
count_type get_tab_offset(count_type c, string text) {
|
||||
count_type tab_offset = 0;
|
||||
for(count_type i = 0; i + tab_offset < c; ++i) {
|
||||
if(text[i] == '\t')
|
||||
tab_offset += TAB_SIZE - (i+tab_offset) % TAB_SIZE - 1;
|
||||
if(i + tab_offset > c)
|
||||
tab_offset -= i + tab_offset - c;
|
||||
}
|
||||
return tab_offset;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
@ -29,8 +55,6 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
Editor ed(filename, file);
|
||||
|
||||
ed.print_file();
|
||||
|
||||
// Main loop
|
||||
while(ed.take_action())
|
||||
refresh();
|
||||
|
|
25
treap.cpp
25
treap.cpp
|
@ -6,31 +6,6 @@ count_type Treap::get_size(line* l) {
|
|||
return l->size;
|
||||
}
|
||||
|
||||
string Treap::substitute_tabs(string s) {
|
||||
string ret = "";
|
||||
for(count_type i = 0; i < s.size(); ++i) {
|
||||
if(s[i] == '\t') {
|
||||
ret += ' ';
|
||||
while(ret.size() % TAB_SIZE != 0)
|
||||
ret += ' ';
|
||||
}
|
||||
else
|
||||
ret += s[i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
// Get how much tabs changed position on a row
|
||||
count_type Treap::get_tab_offset(count_type c, string text) {
|
||||
count_type tab_offset = 0;
|
||||
for(count_type i = 0; i + tab_offset < c; ++i) {
|
||||
if(text[i] == '\t')
|
||||
tab_offset += TAB_SIZE - (i+tab_offset) % TAB_SIZE - 1;
|
||||
if(i + tab_offset > c)
|
||||
tab_offset -= i + tab_offset - c;
|
||||
}
|
||||
return tab_offset;
|
||||
}
|
||||
|
||||
// Split treap by k-th element
|
||||
two_lines Treap::split(line *l, count_type k) {
|
||||
if(l == nullptr) return {nullptr, nullptr};
|
||||
|
|
Loading…
Reference in a new issue