From 8beb95963d44c06993156ffd82fbec28b8c8d3d7 Mon Sep 17 00:00:00 2001 From: Matuush Date: Sun, 20 Apr 2025 18:56:31 +0200 Subject: [PATCH] Implement custom tab-size --- main.cpp | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/main.cpp b/main.cpp index 6e931a9..d623a60 100644 --- a/main.cpp +++ b/main.cpp @@ -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]; + } + return ret; } -void set(position p, char ch) { file.find(p.r)->text[p.c] = ch; } +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;