Implement custom tab-size

This commit is contained in:
Matúš Púll 2025-04-20 18:56:31 +02:00
parent fca89c9f55
commit 8beb95963d

View file

@ -7,6 +7,8 @@
using std::string;
#define TAB_SIZE 2
#define ESC 27
#define ENTER 10
#define BS 127
@ -35,16 +37,32 @@ string get_line(count_type r, bool substitute_tab = true) {
string line = file.get(r);
if(!substitute_tab)
return line;
for(count_type i = 0; i < line.size(); ++i)
string ret = "";
for(count_type i = 0; i < line.size(); ++i) {
if(line[i] == '\t')
line[i] = ' ';
return line;
for(int j = 0; j < TAB_SIZE; j++)
ret += ' ';
else
ret += line[i];
}
void set(position p, char ch) { file.find(p.r)->text[p.c] = ch; }
return ret;
}
count_type get_tab_offset(position p) {
count_type tab_offset = 0;
string line = get_line(p.r, false);
for(count_type i = 0; i + tab_offset < p.c; ++i) {
if(line[i] == '\t')
tab_offset += TAB_SIZE - 1;
if(i + tab_offset > p.c)
tab_offset -= i + tab_offset - p.c;
}
return tab_offset;
}
void set(position p, char ch) { file.find(p.r)->text[p.c-get_tab_offset(p)] = ch; }
void new_line(count_type r, string text = "") { file.insert(r, text); }
void insert(position p, string t) { file.find(p.r)->text.insert(p.c, t); }
void insert(position p, string t) { file.find(p.r)->text.insert(p.c-get_tab_offset(p), t); }
void insert(position p, char ch) { insert(p, string{ch}); }
void remove(position p, count_type len=1) { file.find(p.r)->text.erase(p.c,len); }
void remove(position p, count_type len=1) { file.find(p.r)->text.erase(p.c-get_tab_offset(p),len); }
void remove(count_type r) { file.remove(r); }
void append(string t) { file.append(t); }
count_type get_size() { return file.size(); }
@ -338,6 +356,7 @@ int main(int argc, char* argv[]) {
break;
case 'o':
new_line(file_offset + cur.r);
jump_line_end();
print_file(file_offset + cur.r);
break;
case 'q':
@ -407,6 +426,11 @@ int main(int argc, char* argv[]) {
print_file(file_offset + cur.r++);
jump_line_end();
break;
case '\t':
insert(cur + file_offset, ch);
cur.c += TAB_SIZE;
print_line(file_offset + cur.r);
break;
default:
insert(cur + file_offset, ch);
cur.c += 1;