Skip to content

Commit

Permalink
fix tabs (almost)
Browse files Browse the repository at this point in the history
Fixes include:
- Calculate offset of tab when traversing
- Properly delete tabs
- Better home/end

Also bump version (forgot it before actual release)
  • Loading branch information
ikozyris committed Aug 2, 2024
1 parent 8e84b6a commit e1c91be
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 84 deletions.
1 change: 0 additions & 1 deletion headers/keybindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@
#define REFRESH ctrl('R')
#define KEY_TAB 9
#define CMD 'c'
#define OFF 'u'
#define SWITCH 'r'
112 changes: 46 additions & 66 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -102,48 +102,24 @@ 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;
}
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);
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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<gap_buf>::iterator tmp = it;
--it;
Expand All @@ -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;

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -259,19 +244,14 @@ int main(int argc, char *argv[])
strcpy(argv[1], input_header("File to open: "));
std::list<gap_buf>::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;
}
curnum = 0;
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);
Expand Down
2 changes: 1 addition & 1 deletion utils/init.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "sizes.c"

#define name "Yocto 0.8-beta3"
#define name "Yocto 0.8-beta4"

void init_curses()
{
Expand Down
69 changes: 53 additions & 16 deletions utils/key_func.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions utils/sizes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e1c91be

Please sign in to comment.