From 8dc16195d581c8e12973462af951e31032c42a20 Mon Sep 17 00:00:00 2001 From: ElectricAlchemist Date: Tue, 29 Oct 2019 17:12:00 +0100 Subject: [PATCH] rowFree rowCpy rowAdd rowAddlast --- ste | Bin 22168 -> 22168 bytes ste.c | 145 +++++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 118 insertions(+), 27 deletions(-) diff --git a/ste b/ste index 460d0487b0fd993773aa572a955c45a30a0ad576..e76b79f24642e5cabd7c4c5036c8794b18269b87 100755 GIT binary patch delta 4726 zcmY*d3v?6LnVyFSWJlK6#=>?2*bXu=F&GQtfMP50xFa*77W>V*|ItI3|;BI5}-L-8R`J+w>&I*@QxCWaC&hg<^ZVq-nD)a3G8zm`4IQ;W7K& z87bYJbA12D{r>;{_rL#r$h+TUcfZN@m2$7n*>(G5-olxT^DUfd7O}N?1-vU3@a773 zps=sb6~Clr8MnXq@ZWv#pU*zi9^J8YPx`*L#w*(+YhLWPxZ}d@UF9x*tktC+moQD` z{U<#Qxrt5d6g{V^8d`8s7;lQ;l_jI;vVmM-a=q5|pZBQYB{; za@P7+$Ke)W$cUm%+3|UBl*m6xr9m z*b5d52i9RK=;5mS5zle(mhLKwp%`!iUqNBthW3F2G$Gb?ih3Q&ebXQYuO8s2_BAMi z6OM&D-xr}!bm2%CvCs+Hx#oo#{wg|&mh8g!3L9Jq3|FWtKo)iOOHIF z7_)alRFKLhvAl`FBnxTx0(9%b-C%WpBihiIxU(7))9dFzB7^hCjZT)m6iSSdDKRog z1P|0kvrU9+jTlj%sn>JV?6SD?&~%uEPeDz_C35flOR}+?Wj_yHmdTJ98Rvx~=u&nQ z{~)DCahIa=ijgzcb5-o+F{Gkz@&qS>$@R{Pbu~`U6}x@NxuPXmEiuG~MiS?U-=M~N z_#u$sRo(wfP-~Pg_asdEclN0K(o>i%u6SviPIKDJ(hh1^i3LKKZd zFpxu4|Kvh$G(gw;8>-V6-T@ufkzrA7mU$g(X!vitgAN@`M%dFbIU^aZY-{YiH4`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4CzCO_e+_x8Ni5w0%q<+qoUr1Ga{V;M+H-KjyiPD<0DOJU3_KdCoXt{46GUAzi;~ zL5a78wbgUkd)QF+lWZ8YI=ck|j1%8YVBDA`hpP6)6$w4-ReSx{{Rc$@-2Mj z1fH60O8%$%AH;&tJ!rB_9+Uc4OZEPKLMye_6i#W5WhOI+CjRYGxHY%JLsoAt^X`Ha zK0#^b`WBvE&^b(0Um2WJn6PAB%+YUI8qpsL78KI0#QdLe^7W43eBEK6qTgXJXKQrd zviXIS#B*pbal)wBlZ2_$Z=Y1a%KAgL0*?-vT=>+9qe|rhrGJz?q{~fV9OR~Nls}SI z&I=P-Ei;o{Rr+Vk=Fsj=UH%Myom)GXm$BS9qmM`pT<=W%u23^qUZQg=_O>2ev5n2x zx~2a1$WKB)U%$C^N%(*oxf54A4rm6t^fc4 diff --git a/ste.c b/ste.c index cff2d6a..864aa96 100644 --- a/ste.c +++ b/ste.c @@ -68,6 +68,7 @@ static int curRealToRender (row *rw, int c_x); static inline void rowInit (void); static void rowAddChar (row *rw, char c); static void rowDeleteChar (row *rw, int m); +static void rowCpy (row *to, row *from); /* Terminal operations */ static void termInit (void); @@ -79,14 +80,16 @@ static void fileOpen (char *filename); void fileSave (char *filename); /* buffer operations */ -static void rowAdd (char *s, int len); +static void rowAddLast (char *s, int len); /* garbage */ -//inline void bufferFree (void); -static int whatsThat (void); /* testing */ -void updateInfo (void); +static void updateInfo (void); +static void rowAddRow (int pos); +static int whatsThat (void); +static void rowFree (row *rw); + /* --------------------------------- main ------------------------------------ */ int main (int argc, char *argv[]) @@ -130,6 +133,14 @@ int main (int argc, char *argv[]) case (KEY_DC): rowDeleteChar(&rows.rw[t.cur.y + t.cur.off_y], 1); break; + case (KEY_ENTER): + case (10): + case ('\r'): + rowAddRow(t.cur.y + t.cur.off_y); + break; + case (KEY_END): + t.cur.y = rows.rownum; + break; default: if (c == KEY_STAB) c = '\t'; rowAddChar(&rows.rw[t.cur.y + t.cur.off_y], c); @@ -167,16 +178,13 @@ void termInit (void) /* Start color mode */ start_color(); init_pair(2, COLOR_BLACK, COLOR_CYAN); - init_pair(1, COLOR_BLACK, COLOR_WHITE); + init_pair(1, COLOR_RED, COLOR_BLACK); /* Set default color */ //bkgd(COLOR_PAIR(1)); /* Populate the main data structure */ - getmaxyx(stdscr, t.dim.y, t.dim.x); - t.dim.y -= 1; - t.pad = getLineNumberSize(); - t.dim.x -= t.pad + 1; + updateInfo(); /* Initialize the data staructure */ t.cur.x = t.cur.off_x = 0; @@ -194,7 +202,6 @@ int getLineNumberSize (void) void termExit (void) { -// bufferFree(); erase(); refresh(); endwin(); @@ -240,9 +247,9 @@ void drawLines (void) ln = i + t.cur.off_y; /* Draw the line number */ - attron(COLOR_PAIR(2)); + attron(COLOR_PAIR(1)); mvprintw(i, 0, "%d", ln + 1); - attroff(COLOR_PAIR(2)); + attroff(COLOR_PAIR(1)); lnMove(i, 0); //if (ln == t.cur.y + t.cur.off_y) attron(COLOR_PAIR(2)); @@ -279,8 +286,8 @@ void drawBar (char *s) for (int i = len; i <= t.dim.x + t.pad; i++) mvaddch(t.dim.y, i, ' '); - char m[10]; - sprintf(m, "%d Zoom: %c", t.cur.x + t.cur.off_x, whatsThat()); + char m[40]; + sprintf(m, "x: %d y: %d Zoom: %c", t.cur.x + t.cur.off_x, t.cur.y + t.cur.off_y, whatsThat()); mvaddstr(t.dim.y, t.dim.x + t.pad - strlen(m), m); /* Return to normal contrast mode */ @@ -317,7 +324,7 @@ void fileOpen (char *filename) while ((linelen = getline(&line, &linecap, fp)) != -1) { while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r')) linelen--; - rowAdd(line, linelen); + rowAddLast(line, linelen); } /* free the line buffer */ free(line); @@ -326,7 +333,7 @@ void fileOpen (char *filename) } /* Add a row to the file buffer */ -void rowAdd (char *s, int len) +void rowAddLast (char *s, int len) { /* Extend the block of memory containing the lines */ rows.rw = realloc(rows.rw, sizeof(row) * (rows.rownum + 1)); @@ -526,17 +533,6 @@ void updateScroll (void) /*---------------------------------- scroll ------------------------------------*/ -/* -void bufferFree (void) -{ - for (int i = 0; i < rows.rownum; i++) { - free(&rows.rw[i].chars); - free(&rows.rw[i].render); - } - free(rows.rw); -} -*/ - /* See whats under the cursor (memory) */ int whatsThat (void) { int ln = t.cur.y + t.cur.off_y; @@ -558,12 +554,107 @@ int whatsThat (void) { return 0; } +/* void rowAddRow (void) //VERY WIP +{ + int cur_x = t.cur.x + t.cur.off_x; + int cur_y = t.cur.y + t.cur.off_y; + char *s = NULL; + + // Last line case + if (cur_y == rows.rownum - 1) { + if (rows.rw[cur_y].chars[cur_x] != '\0') { + //copy the previous string + s = malloc(rows.rw[cur_y].size + 1); + memcpy (s, &rows.rw[cur_y].chars, rows.rw[cur_y].size + 1); + } + cur_y++; + rows.rownum++; + rows.rw = realloc(rows.rw, sizeof(row) * rows.rownum); + if (s == NULL) { + rows.rw[cur_y].chars = malloc(1); + rows.rw[cur_y].chars[0] = '\0'; + } else { + rows.rw[cur_y].chars = malloc(strlen(s) + 1); + memcpy(rows.rw[cur_y].chars, s, strlen(s)); + rows.rw[cur_y].chars[strlen(s) + 1] = '\0'; + } + free(s); + updateRender(&rows.rw[cur_y]); + t.cur.y++; + } +} */ + +void rowAddRow (int pos) // WIP; TO DOCUMENT +{ + int cur_x = t.cur.x + t.cur.off_x; + char *s = NULL; + // Move away other lines + //copy old last line to new space + rowAddLast(rows.rw[rows.rownum].chars, rows.rw[rows.rownum].size); + + for (int last = rows.rownum - 1; last > pos; last--) { + rowCpy(&rows.rw[last], &rows.rw[last - 1]); + } + + //if (!cur_x) cur_x = rows.rw[pos].size; + //if (rows.rw[pos].chars[cur_x] == '\0') { + // cur_x = 0; + // t.cur.x = 0; + //} + + //copy previous row + int l = rows.rw[pos].size - cur_x; + s = malloc(l + 1); + memcpy(s, &rows.rw[pos].chars[cur_x], l); + s[l] = '\0'; + // Delete prev row until cursor + char *p = malloc(cur_x + 1); + memcpy(p, rows.rw[pos].chars, cur_x); + p[cur_x] = '\0'; + rowFree(&rows.rw[pos]); + rows.rw[pos].chars = malloc(cur_x + 1); + memcpy(rows.rw[pos].chars, p, cur_x + 1); + free(p); + rows.rw[pos].size = cur_x; + updateRender(&rows.rw[pos]); + + if (pos != rows.rownum - 1) { + rowFree(&rows.rw[pos + 1]); + rows.rw[pos + 1].chars = malloc(strlen(s) + 1); + memcpy(rows.rw[pos + 1].chars, s, strlen(s) + 1); + rows.rw[pos + 1].size = strlen(s); + updateRender(&rows.rw[pos + 1]); + } else rowAddLast(s, l); + + free(s); + t.cur.y++; + t.cur.x = 0; +} + +void rowFree (row *rw) // WIP +{ + free(rw->render); + free(rw->chars); + rw->size = 0; + rw->r_size = 0; +} + +void rowCpy (row *to, row *from) // WIP +{ + rowFree(to); + to->chars = malloc(strlen(from->chars) + 1); + to->size = from->size; + memcpy(to->chars, from->chars, to->size); + updateRender(to); +} + /*--------------------------------- garbage ------------------------------------*/ void updateInfo (void) { getmaxyx(stdscr, t.dim.y, t.dim.x); t.dim.y -= 1; + t.pad = getLineNumberSize(); t.dim.x -= t.pad + 1; }