From cea7f27dcbcbe62951cfca23788ad195dee07741 Mon Sep 17 00:00:00 2001 From: ikozyris <80053394+ikozyris@users.noreply.github.com> Date: Thu, 24 Oct 2024 21:59:03 +0300 Subject: [PATCH] fix: go back from tab by creating prevword(), which will be used later what the function says it does Also create release definition, seperately from the debug --- Makefile | 2 +- main.cpp | 4 +++- utils/key_func.cpp | 11 ++++++----- utils/sizes.c | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 55d5036..a7a2258 100755 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ CC = g++ --std=c++20 # -DHIGHLIGHT Enable syntax highlighting # -lncursesw Links to ncurses library for wide characters (unicode) -OPTIM = -Ofast -flto -march=native +OPTIM = -Ofast -flto -march=native -DRELEASE DEBUG = -g #-DDEBUG #CXXFLAGS = -Wall -Wextra -pedantic-errors $(DEBUG) -DHIGHLIGHT -lncursesw # Debug only CXXFLAGS = -Wall -Wextra -pedantic $(OPTIM) -DHIGHLIGHT -lncursesw diff --git a/main.cpp b/main.cpp index a2208f1..2307ae0 100755 --- a/main.cpp +++ b/main.cpp @@ -77,9 +77,11 @@ int main(int argc, char *argv[]) rx = x + ofx; mv_curs(*it, rx); +#ifndef RELEASE + stats(); +#endif #ifdef DEBUG print_text(y); // debug only - stats(); wmove(text_win, y, x); #endif //goto stop; diff --git a/utils/key_func.cpp b/utils/key_func.cpp index 45bb974..4e4c8cf 100755 --- a/utils/key_func.cpp +++ b/utils/key_func.cpp @@ -6,10 +6,13 @@ void stats() unsigned sumlen = 0; for (auto &i : text) sumlen += i.len; - //snprintf(_tmp, min(maxx, 256), "maxx %u len %u ofx %ld wrap %u x: %u | y: %u ", - // maxx, it->len, ofx, !wrap.empty() ? wrap.back() : 0, x, y); +#ifndef RELEASE + snprintf(_tmp, min(maxx, 256), "maxx %u len %u ofx %ld wrap %u x: %u | y: %u ", + maxx, it->len, ofx, !wrap.empty() ? wrap.back() : 0, x, y); +#else snprintf(_tmp, min(maxx, 256), "length %u y %u x %u sum len %u lines %lu ofx %ld ", it->len, ry, x, sumlen, curnum, ofx); +#endif print2header(_tmp, 1); free(_tmp); wmove(text_win, y, x); @@ -226,10 +229,8 @@ void left() #endif wmove(text_win, y, maxx - 1); } else if (x > 0) { - // TODO: use prevword() to get actual offset if (it->buffer[it->gps - 1] == '\t') { - wmove(text_win, y, x - 8); - ofx += 7; + ofx += prevword(x, y); return; } wmove(text_win, y, x - 1); diff --git a/utils/sizes.c b/utils/sizes.c index 70e6b97..abeb833 100644 --- a/utils/sizes.c +++ b/utils/sizes.c @@ -68,3 +68,20 @@ long calc_offset_act(unsigned pos, unsigned i, const gap_buf &buf) get_off(x, i, buf); return (long)i - (long)x; } + +// Go to end of previous word and return +unsigned prevword(unsigned x, unsigned y) +{ + unsigned i = x; + // skip current word + while ((winch(text_win) & A_CHARTEXT) != ' ' && i > 0) + wmove(text_win, y, --i); + // go past any spaces + while ((winch(text_win) & A_CHARTEXT) == ' ' && i > 0) + wmove(text_win, y, --i); + if ((winch(text_win) & A_CHARTEXT) == ' ') { + return x - 1; + } + wmove(text_win, y, ++i); + return x - i - 1; +}