From 7e6b3c0bdf8941e3dc117a952d1ccd4e6db37314 Mon Sep 17 00:00:00 2001 From: gunboy001 Date: Thu, 28 Nov 2019 12:13:48 +0100 Subject: [PATCH] better search and capitalized type names --- .gitignore | 7 +---- .sconsign.dblite | Bin 3124 -> 3124 bytes src/fbuffer.c | 26 ++++++++--------- src/fbuffer.h | 30 +++++++++---------- src/ste.c | 73 ++++++++++++++++++++++++++++++++--------------- src/ste.o | Bin 13200 -> 13384 bytes 6 files changed, 79 insertions(+), 57 deletions(-) diff --git a/.gitignore b/.gitignore index fcc1536..5e912cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,3 @@ ste -dbg -hello -ste.c.* -*.bak -.sconsign* .sconsign.dblite -*.o \ No newline at end of file +src/*.o \ No newline at end of file diff --git a/.sconsign.dblite b/.sconsign.dblite index ac2f1376f14b30d9dd3d0bfa894c91fbd93817e5..0dead31fc38efaaee185a2ea1108d6576edf372f 100644 GIT binary patch delta 433 zcmaivy-EW?5P-Q{f|wx0E@dnfEyTUuz1h9OQjmyc8j0Y}{$wo|u}mT21ITs32Qb(O z$zuqDpkSIjg0J98Bxh$k%=gVVv(BvZ{FtP-lSTQIe9_Tlaqb%@HQ(xY5uF7|EmCGB zu&e+O)(J*57=^3|+;5!D%a_^%9sI$lmQola&`3=k0A(ORIL83Fu#|+uh`pENl(egB ze(9R07wyXBmz%x%fW^L5m(HnDN}%IJ09uoAok^{vL(L7(Y&eStj(Pc6zhiF?6~|Gy z%or?Ca#T)&qZpfX4oc@XGhz4>R+RUR{mqK=%jxvBv7OS7UtPbhuP%pStKTi8<5U_M z16X`o5Cyp*I%X(h4d>IH^@HSSl^=1>DpJLkZRTjSQtLzX}HCgoH1LmqpR(@;C8ghscGoFYhLN5D!( z83XH-WuOF5VJU&g!ph-m`Chpvg9S!7qE1Rs7I5JaXNpm5tgu!g;Sh?uiM^G>95v@{ zVVd=opEc)MVS3rAp3u(V`)?Ok-eJdlAu0FXaUrlS41q#|11@YlN(HyH{H)&4{RO9~ zcE%$Cm}@PuOju|~JfTWcVNC2NcMRoSZF_a@!t{0YQCrW+Y(}?gF3$ULt=kp`l$MOA zY57$1=R0_ Ar2qf` diff --git a/src/fbuffer.c b/src/fbuffer.c index c3f6d92..daa7910 100644 --- a/src/fbuffer.c +++ b/src/fbuffer.c @@ -4,17 +4,17 @@ #include #include -void bufInit (fbuffer *b) +void bufInit (FileBuffer *b) { b->rw = NULL; b->rownum = 0; } /* Add a row to the file buffer */ -void rowAddLast (fbuffer *b, char *s, int len) +void rowAddLast (FileBuffer *b, char *s, int len) { /* Extend the block of memory containing the lines */ - row *newr = realloc(b->rw, (b->rownum + 1) * sizeof(row)); + Row *newr = realloc(b->rw, (b->rownum + 1) * sizeof(Row)); //if (newr == NULL) termDie("realloc in rowAddLast"); b->rw = newr; @@ -29,7 +29,7 @@ void rowAddLast (fbuffer *b, char *s, int len) b->rownum++; } -void rowAddChar (row *rw, char c, int pos) +void rowAddChar (Row *rw, char c, int pos) { /* Check if char is valid */ if (!c || (iscntrl(c) && c != '\t')) return; @@ -49,7 +49,7 @@ void rowAddChar (row *rw, char c, int pos) updateRender(rw); } -int rowDeleteChar (row *rw, int select, int pos) // WIP +int rowDeleteChar (Row *rw, int select, int pos) // WIP { /* Check if the character is valid */ if (rw->chars[pos - 1] == '\0' && pos) return 0; @@ -68,7 +68,7 @@ int rowDeleteChar (row *rw, int select, int pos) // WIP return 1; } -void rowAddRow (fbuffer *b, int pos, int cur) // WIP; TO DOCUMENT +void rowAddRow (FileBuffer *b, int pos, int cur) // WIP; TO DOCUMENT { /* MOVE THE ROWS AWAY */ /* add another line to the bottom containing the previous @@ -83,7 +83,7 @@ void rowAddRow (fbuffer *b, int pos, int cur) // WIP; TO DOCUMENT /* Get the row length at position after the cursor */ int len = b->rw[pos].size - cur; /* create a dummy row as the new row souce */ - row nex = EROW; + Row nex = EROW; /* allocate a buffer */ char *s = malloc(len + 1); //if (s == NULL) termDie("malloc in rowAddRow s"); @@ -111,7 +111,7 @@ void rowAddRow (fbuffer *b, int pos, int cur) // WIP; TO DOCUMENT } -void rowFree (row *rw) // WIP +void rowFree (Row *rw) // WIP { /* Free both render and memory strings */ free(rw->render); @@ -122,7 +122,7 @@ void rowFree (row *rw) // WIP rw->utf = 0; } -void rowCpy (row *to, row *from) // WIP +void rowCpy (Row *to, Row *from) // WIP { /* Free the destination row (without destroying it) */ rowFree(to); @@ -138,7 +138,7 @@ void rowCpy (row *to, row *from) // WIP updateRender(to); } -void rowAppendString (row *rw, char *s, int len) +void rowAppendString (Row *rw, char *s, int len) { /* reallocate the row to accomodate for the added string */ char *temp = realloc(rw->chars, rw->size + len + 1); @@ -152,7 +152,7 @@ void rowAppendString (row *rw, char *s, int len) updateRender(rw); } -void rowDeleteRow (fbuffer *b, int pos) +void rowDeleteRow (FileBuffer *b, int pos) { if (b->rownum == 1) return; if (pos >= b->rownum) return; @@ -163,12 +163,12 @@ void rowDeleteRow (fbuffer *b, int pos) } b->rownum--; rowFree(&b->rw[b->rownum]); - row *temp = realloc(b->rw, sizeof(row) * b->rownum); + Row *temp = realloc(b->rw, sizeof(Row) * b->rownum); //if (temp == NULL) termDie("realloc in rowDeleteRow"); b->rw = temp; } -void updateRender (row *rw) +void updateRender (Row *rw) { /* count the special characters * spaces, utf-8 continuation chars */ diff --git a/src/fbuffer.h b/src/fbuffer.h index da423e7..8ad3053 100644 --- a/src/fbuffer.h +++ b/src/fbuffer.h @@ -6,36 +6,36 @@ * and difference between render and * real size of the row * Utf-8 continuation chars */ -typedef struct row { +typedef struct Row { int size; int r_size; int utf; char *chars; char *render; -} row; +} Row; /* Empty row initializer */ #define EROW {0, 0, 0, NULL, NULL} /* Rows structure (or file buffer) * defines rows and teh number of rows */ -typedef struct fbuffer{ - row *rw; +typedef struct FileBuffer{ + Row *rw; int rownum; -} fbuffer; +} FileBuffer; -void bufInit (fbuffer *b); +void bufInit (FileBuffer *b); -void rowAddChar (row *rw, char c, int pos); -int rowDeleteChar (row *rw, int select, int pos); -void rowCpy (row *to, row *from); -void rowAddRow (fbuffer *b, int pos, int cur); -void rowFree (row *rw); -void rowAppendString (row *rw, char *s, int len); -void rowDeleteRow (fbuffer *b, int pos); -void rowAddLast (fbuffer *b, char *s, int len); +void rowAddChar (Row *rw, char c, int pos); +int rowDeleteChar (Row *rw, int select, int pos); +void rowCpy (Row *to, Row *from); +void rowAddRow (FileBuffer *b, int pos, int cur); +void rowFree (Row *rw); +void rowAppendString (Row *rw, char *s, int len); +void rowDeleteRow (FileBuffer *b, int pos); +void rowAddLast (FileBuffer *b, char *s, int len); -void updateRender (row *rw); +void updateRender (Row *rw); int isUtf (int c); int isCont (int c); diff --git a/src/ste.c b/src/ste.c index 502a62b..13d6098 100644 --- a/src/ste.c +++ b/src/ste.c @@ -13,7 +13,7 @@ /* defines */ #define CTRL(k) ((k) & 0x1f) // Control mask modifier #define STAT_SIZE 128 -#define SBUF_SIZE 2048 +#define CBUF_SIZE 2048 #define MODE_MASK 0x1 #define COMMAND_MASK 0x06 @@ -26,10 +26,10 @@ #define MODIFIED 0x80 // Search buffer -typedef struct sbuf { - char c[SBUF_SIZE]; - int num; -} sbuf; +typedef struct CharBuffer { + char c[CBUF_SIZE]; + int num, cur; +} CharBuffer; /* main data structure containing: * -cursor position @@ -56,10 +56,10 @@ struct term { char statusbar[STAT_SIZE]; int pad; char mode_b; - sbuf search_buffer; + CharBuffer search_buffer; } t; -fbuffer rows; +FileBuffer rows; const char *msg[] = { "Find: ", @@ -93,9 +93,12 @@ static void handleDel (int select); /* testing */ static void updateInfo (void); static int whatsThat (void); -static void insert (sbuf *buf, int c); -static inline void flush (sbuf *buf); -static void pop (sbuf *buf); + +static void sbInsert (CharBuffer *buf, int c); +static inline void sbFlush (CharBuffer *buf); +static void sbPop (CharBuffer *buf); +static void sbMove (CharBuffer *buf, int x); + /* --------------------------------- main ------------------------------------ */ int main (int argc, char *argv[]) @@ -133,7 +136,17 @@ int main (int argc, char *argv[]) case (KEY_RIGHT): case (KEY_UP): case (KEY_DOWN): - cursorMove(c); + if ((t.mode_b & MODE_MASK) == NORMAL_M) + cursorMove(c); + else + switch (c) { + case (KEY_LEFT): + sbMove(&t.search_buffer, -1); + break; + case (KEY_RIGHT): + sbMove(&t.search_buffer, 1); + break; + } break; case (KEY_BACKSPACE): @@ -141,7 +154,7 @@ int main (int argc, char *argv[]) if ((t.mode_b & MODE_MASK) == NORMAL_M) handleDel(c); else - pop(&t.search_buffer); + sbPop(&t.search_buffer); break; case (KEY_ENTER): @@ -155,7 +168,7 @@ int main (int argc, char *argv[]) editorFind(t.search_buffer.c, &t.cur.y, &t.cur.x); // Toggle mode t.mode_b ^= MODE_MASK; - flush (&t.search_buffer); + sbFlush (&t.search_buffer); } break; @@ -170,7 +183,7 @@ int main (int argc, char *argv[]) case (CTRL('f')): // Toggle mode t.mode_b ^= MODE_MASK; - flush (&t.search_buffer); + sbFlush (&t.search_buffer); break; @@ -181,7 +194,7 @@ int main (int argc, char *argv[]) rowAddChar(&rows.rw[t.cur.y], c, t.cur.x); t.cur.x++; } else { - insert(&t.search_buffer, c); + sbInsert(&t.search_buffer, c); } break; } @@ -232,6 +245,9 @@ void termInit (void) /* Initialize the data staructure */ t.cur.x = t.cur.off_x = 0; t.cur.y = t.cur.off_y = 0; + + t.search_buffer.cur = 0; + t.search_buffer.num = 0; } /* Calculate the correct spacing for the line numbers @@ -589,7 +605,7 @@ int editorFind (const char* needle, int *y, int *x) } if (res == NULL) return 0; - /* If something wa found convert from pointer to yx coodinates */ + /* If something was found convert from pointer to yx coodinates */ *y = c = i; for (i = 0; i <= rows.rw[c].size; i++) if (&rows.rw[c].chars[i] == res) break; @@ -597,20 +613,31 @@ int editorFind (const char* needle, int *y, int *x) return 1; } -void insert (sbuf *buf, int c) +void sbMove (CharBuffer *buf, int x) { + buf->cur += (x); + if (buf->cur < 0) buf->cur = 0; + if (buf->cur >= buf->num) buf->cur = buf->num - 1; +} + +void sbInsert (CharBuffer *buf, int c) { - if (buf->num < SBUF_SIZE - 2) - buf->c[buf->num++] = c; - buf->c[buf->num] = '\0'; + if (buf->num < CBUF_SIZE - 2) { + buf->c[buf->cur++] = c; + buf->c[++(buf->num)] = '\0'; + } } -void flush (sbuf *buf) +void sbFlush (CharBuffer *buf) { buf->num = 0; + buf->cur = 0; } -void pop (sbuf *buf) +void sbPop (CharBuffer *buf) { - if (buf->num) buf->c[--(buf->num)] = '\0'; + if (buf->num) { + buf->c[--(buf->num)] = '\0'; + sbMove(buf, -1); + } } /*--------------------------------- testing ------------------------------------*/ diff --git a/src/ste.o b/src/ste.o index 07b146c9a7a62408a2d3ce1fbd309e0e778a9099..c9ef6677637e6b2d61b3212baf043868a58a2577 100644 GIT binary patch delta 4723 zcmZ`+4{#La72ogj2SSqDOCebn3Hb{ofJrWaked*~!6|$E5gDjjMW>m7B1It(XxdUP z=iO;;j)TyLGj)JslyR(usTgF$LdapuKkX1w)T&gBGfcD48Hh*-2yEZ`c9$$SlW(%$ z?)QH0{obGNeS4eM7f!8gEwT+SJ22^KHg3xZLGVU44_Bho1RT9IDdB%8p-wSaWp96r z4P&;sBj7K#)>P$W5}`DpI-zLZFP>8(HMCMfniy1u34(ZRZnAjn$;7C_B3w6|N%){$ zpqC->)~AOHg8L(e(DP!B2kG8V_+>_AsQ)) zEDIJT?Xk6`z_p}F_VG&8! zQeq}oGmJ;N6z6_jRSez31&mJ5vz<$T>h$0GB(!h`N|`-HO!k+ZGLIno`gAIdGim&XoG@ zXajS%1nuuq0KDSv6TvK632HW4n3)IviG($YIEleRtYu9SJ%(=I)O?!0kGePPuft?4 zDlt8C*Ku*%=QlCJ%Z5+&B(Cn$mLr#L8R`~;`w)uG(>jPjsgH$uv=~q_59ofsh{_3r zJSJ}QHF0*Eo{_o=SI%zrEZsWQ7$t4l^J5sbe)4s)=yS{{zNawF^@$kW!99Pf#Cq`9 zEfl=r&#q*NIRe(#8~A&!u3NpVFs<=rZ)A0$1K%Yt@L~?BbvauEP6BM1t6x=>01xa|{PC6E5^J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