diff --git a/headers/keybindings.h b/headers/keybindings.h index 85cb5dc..dcce1e9 100644 --- a/headers/keybindings.h +++ b/headers/keybindings.h @@ -19,5 +19,4 @@ #define REFRESH ctrl('R') #define KEY_TAB 9 #define CMD 'c' -#define OFF 'u' #define SWITCH 'r' diff --git a/main.cpp b/main.cpp index 68f68f3..9c3a07f 100755 --- a/main.cpp +++ b/main.cpp @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) goto loop; } - eligible = isc(argv[1]); + eligible = isc(argv[1]); // syntax highlighting if (argc > 2 && (strcmp(argv[2], "-ro") == 0 || strcmp(argv[2], "--read-only") == 0)) { read_fread_sl(fi); @@ -86,12 +86,12 @@ int main(int argc, char *argv[]) getyx(text_win, y, x); rx = x + ofx; mv_curs(*it, rx); - +stats(); #ifdef DEBUG - print_text(); // debug only + print_text(y); // debug only char tmp[80]; - snprintf(tmp, 79, "st %u | end %u | cpt %u | len %u | gapLen %u | x %u ", - it->gps, it->gpe, it->cpt, it->len, gaplen(*it), x); + snprintf(tmp, 79, "st %u | end %u | cpt %u | len %u | maxx %u | ofx %ld ", + it->gps, it->gpe, it->cpt, it->len, maxx, ofx); print2header(tmp, 1); wmove(text_win, y, x); #endif @@ -102,40 +102,16 @@ int main(int argc, char *argv[]) break; ++it; ofx = 0; // invalidated - if (y == (maxy - 1) && ry < curnum) { - ofy++; - wscrl(text_win, 1); - wscrl(ln_win, 1); - mvwprintw(ln_win, maxy - 1, 0, "%3u", ry + 2); - wrefresh(ln_win); - wmove(text_win, y, 0); - print_line(*it); -#ifdef HIGHLIGHT - wmove(text_win, y, 0); - apply(y); -#endif - wmove(text_win, y, x); - } else + if (y == (maxy - 1) && ry < curnum) + scrolldown(); + else wmove(text_win, y + 1, x); break; case UP: - if (y == 0 && ofy != 0) { - wscrl(text_win, -1); // scroll up - wscrl(ln_win, -1); - mvwprintw(ln_win, 0, 0, "%3u", ry); - --ofy; - wmove(text_win, 0, 0); - --it; - print_line(*it); -#ifdef HIGHLIGHT - wmove(text_win, y, 0); - apply(y); -#endif - wmove(text_win, 0, x); - wrefresh(ln_win); - ofx = 0; - } else if (y != 0) { + if (y == 0 && ofy != 0) + scrollup(); + else if (y != 0) { wmove(text_win, y - 1, x); --it; ofx = 0; @@ -143,7 +119,7 @@ int main(int argc, char *argv[]) break; case LEFT: - if (x == 0 && rx >= maxx - 1) { // line has been wrapped + if (x == 0 && ofx > 0) { // line has been wrapped wmove(text_win, y, 0); wclrtoeol(text_win); print_line(*it); @@ -153,19 +129,23 @@ int main(int argc, char *argv[]) #endif ofx -= maxx - 1; wmove(text_win, y, maxx - 1); - } else if (x > 0) - wmove(text_win, y, x - 1); - else if (y > 0) { // x = 0 + } else if (x > 0) { + if (it->buffer[it->gps - 1] == '\t') { + wmove(text_win, y, x - 8); + ofx += 7; + } else + wmove(text_win, y, x - 1); + } else if (y > 0) { // x = 0 if (ofx > 0) // revert wrap print_line(*it); --it; - wmove(text_win, y - 1, min(it->len, maxx) - 1); - ofx = 0; + --y; + eol(); } break; case RIGHT: - if (x == maxx - 1 && x < it->len - 1) { + if (x == maxx - 1 && rx < it->len - 1) { wmove(text_win, y, 0); wclrtoeol(text_win); // printline() with custom start @@ -174,11 +154,15 @@ int main(int argc, char *argv[]) ofx += maxx - 1; wmove(text_win, y, 0); - } else if (ry != curnum ? rx < it->len - 1 : rx < it->len) - wmove(text_win, y, x + 1); - else if (ry < curnum) { + } else if (ry != curnum ? rx < it->len - 1 : rx < it->len) { + if (it->buffer[it->gpe + 1] == '\t') { + ofx -= 8 - x % 8 - 1; + wmove(text_win, y, x + 8 - x % 8); + } else + wmove(text_win, y, x + 1); + } else if (ry < curnum) { wmove(text_win, y, 0); - if (ofx > 0) + if (ofx > 0) // revert wrap print_line(*it); wmove(text_win, y + 1, 0); ++it; @@ -189,7 +173,14 @@ int main(int argc, char *argv[]) case BACKSPACE: if (x != 0) { eras(*it); - mvwdelch(text_win, y, x - 1); + if (it->buffer[it->gps] == '\t') { + wmove(text_win, y, 0); + print_line(*it); + wclrtoeol(text_win); + wmove(text_win, y, x - 8); + ofx += 7; + } else + mvwdelch(text_win, y, x - 1); } else if (y != 0) { // delete previous line's \n std::list::iterator tmp = it; --it; @@ -207,7 +198,7 @@ int main(int argc, char *argv[]) case DELETE: wdelch(text_win); mv_curs(*it, rx + 1); - if (rx + (unsigned)1 <= it->len) + if (rx + 1u <= it->len) mveras(*it, rx + 1); break; @@ -217,8 +208,11 @@ int main(int argc, char *argv[]) case HOME: wmove(text_win, y, 0); - x = 0; rx = ofx; - if (ofx >= it->len - maxx) { // line has been wrapped + if (ofx < 0) { + ofx = 0; + break; + } + if (ofx >= (long)it->len - maxx) { // line has been wrapped wclrtoeol(text_win); print_line(*it); #ifdef HIGHLIGHT @@ -231,16 +225,7 @@ int main(int argc, char *argv[]) break; case END: - wmove(text_win, y, ofx + (ry != curnum ? it->len - 1 : it->len)); - if (it->len >= maxx + ofx) { - wmove(text_win, y, 0); - wclrtoeol(text_win); - // TODO: use actual printline() - data2(*it, it->len - maxx, it->len); - waddnstr(text_win, lnbuf, maxx - 1); - - ofx += it->len - maxx; - } + eol(); break; case SAVE: @@ -259,7 +244,7 @@ int main(int argc, char *argv[]) strcpy(argv[1], input_header("File to open: ")); std::list::iterator iter; unsigned i; - for (iter = text.begin(), i = 0; iter != text.end() && i < curnum; ++iter, ++i) { + for (iter = text.begin(), i = 0; iter != text.end() && i <= curnum; ++iter, ++i) { iter->len = iter->gps = 0; iter->gpe = iter->cpt; } @@ -267,11 +252,6 @@ int main(int argc, char *argv[]) it = text.begin(); wclear(text_win); goto read; - } else if (ch == OFF) { // calculate x offset - x = sizeofline(y); - print2header(itoa(x), 1); - ofx = (long)it->len - (long)x; - print2header(itoa(ofx), 2); } wtimeout(text_win, -1); wmove(text_win, y, x); diff --git a/utils/init.c b/utils/init.c index 45ecf38..1ac3285 100644 --- a/utils/init.c +++ b/utils/init.c @@ -1,6 +1,6 @@ #include "sizes.c" -#define name "Yocto 0.8-beta3" +#define name "Yocto 0.8-beta4" void init_curses() { diff --git a/utils/key_func.cpp b/utils/key_func.cpp index b08e8bb..e53dcd4 100755 --- a/utils/key_func.cpp +++ b/utils/key_func.cpp @@ -1,27 +1,13 @@ #include "io.cpp" -long memusg() -{ - size_t memusg = 0, tmp; - char buffer[1024]; - FILE *file = fopen("/proc/self/smaps", "r"); - while (fscanf(file, " %1023s", buffer) == 1) - if (strcmp(buffer, "Pss:") == 0) { - fscanf(file, " %lu", &tmp); - memusg += tmp; - } - fclose(file); - return memusg; -} - void stats() { char *_tmp = (char*)malloc(256); unsigned sumlen = 0; for (auto &i : text) sumlen += i.len; - snprintf(_tmp, maxx-2, "length %u y %u x %u sum len %u lines %lu ", - it->len, ry, x, sumlen, curnum); + snprintf(_tmp, maxx-2, "length %u y %u x %u sum len %u lines %lu ofx %ld ", + it->len, ry, x, sumlen, curnum, ofx); print2header(_tmp, 1); free(_tmp); _tmp = 0; @@ -161,3 +147,54 @@ void enter() } ofx = 0; } + +// TODO: handle better lines with uncalculated offsets +void eol() +{ + if (it->len < (long)maxx + ofx) { + x = sizeofline(y); + ofx = (long)it->len - (long)x; + } else { // wrap line + wmove(text_win, y, 0); + wclrtoeol(text_win); + // TODO: use actual printline() + data2(*it, it->len - maxx - ofx, it->len); + waddnstr(text_win, lnbuf, maxx + ofx - 1); + ofx = (long)it->len - (long)maxx - ofx; + } +} + +void scrolldown() +{ + ofy++; + wscrl(text_win, 1); + wscrl(ln_win, 1); + mvwprintw(ln_win, maxy - 1, 0, "%3u", ry + 2); + wrefresh(ln_win); + wmove(text_win, y, 0); + print_line(*it); +#ifdef HIGHLIGHT + wmove(text_win, y, 0); + apply(y); +#endif + wmove(text_win, y, x); + +} + +void scrollup() +{ + wscrl(text_win, -1); // scroll up + wscrl(ln_win, -1); + mvwprintw(ln_win, 0, 0, "%3u", ry); + --ofy; + wmove(text_win, 0, 0); + --it; + print_line(*it); +#ifdef HIGHLIGHT + wmove(text_win, y, 0); + apply(y); +#endif + wmove(text_win, 0, x); + wrefresh(ln_win); + ofx = 0; +} diff --git a/utils/sizes.c b/utils/sizes.c index d203930..0a749f5 100644 --- a/utils/sizes.c +++ b/utils/sizes.c @@ -30,5 +30,20 @@ unsigned sizeofline(unsigned y) { wmove(text_win, y, i); while ((winch(text_win) & A_CHARTEXT) == ' ') wmove(text_win, y, --i); + wmove(text_win, y, i + 1); return i + 2; } + +long memusg() +{ + size_t memusg = 0, tmp; + char buffer[1024]; + FILE *file = fopen("/proc/self/smaps", "r"); + while (fscanf(file, " %1023s", buffer) == 1) + if (strcmp(buffer, "Pss:") == 0) { + fscanf(file, " %lu", &tmp); + memusg += tmp; + } + fclose(file); + return memusg; +}