better search and capitalized type names

master
Alessandro Mauri 5 years ago
parent 9fab7fe63d
commit 7e6b3c0bdf
  1. 7
      .gitignore
  2. BIN
      .sconsign.dblite
  3. 26
      src/fbuffer.c
  4. 30
      src/fbuffer.h
  5. 73
      src/ste.c
  6. BIN
      src/ste.o

7
.gitignore vendored

@ -1,8 +1,3 @@
ste ste
dbg
hello
ste.c.*
*.bak
.sconsign*
.sconsign.dblite .sconsign.dblite
*.o src/*.o

Binary file not shown.

@ -4,17 +4,17 @@
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
void bufInit (fbuffer *b) void bufInit (FileBuffer *b)
{ {
b->rw = NULL; b->rw = NULL;
b->rownum = 0; b->rownum = 0;
} }
/* Add a row to the file buffer */ /* 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 */ /* 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"); //if (newr == NULL) termDie("realloc in rowAddLast");
b->rw = newr; b->rw = newr;
@ -29,7 +29,7 @@ void rowAddLast (fbuffer *b, char *s, int len)
b->rownum++; b->rownum++;
} }
void rowAddChar (row *rw, char c, int pos) void rowAddChar (Row *rw, char c, int pos)
{ {
/* Check if char is valid */ /* Check if char is valid */
if (!c || (iscntrl(c) && c != '\t')) return; if (!c || (iscntrl(c) && c != '\t')) return;
@ -49,7 +49,7 @@ void rowAddChar (row *rw, char c, int pos)
updateRender(rw); 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 */ /* Check if the character is valid */
if (rw->chars[pos - 1] == '\0' && pos) return 0; if (rw->chars[pos - 1] == '\0' && pos) return 0;
@ -68,7 +68,7 @@ int rowDeleteChar (row *rw, int select, int pos) // WIP
return 1; 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 */ /* MOVE THE ROWS AWAY */
/* add another line to the bottom containing the previous /* 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 */ /* Get the row length at position after the cursor */
int len = b->rw[pos].size - cur; int len = b->rw[pos].size - cur;
/* create a dummy row as the new row souce */ /* create a dummy row as the new row souce */
row nex = EROW; Row nex = EROW;
/* allocate a buffer */ /* allocate a buffer */
char *s = malloc(len + 1); char *s = malloc(len + 1);
//if (s == NULL) termDie("malloc in rowAddRow s"); //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 both render and memory strings */
free(rw->render); free(rw->render);
@ -122,7 +122,7 @@ void rowFree (row *rw) // WIP
rw->utf = 0; rw->utf = 0;
} }
void rowCpy (row *to, row *from) // WIP void rowCpy (Row *to, Row *from) // WIP
{ {
/* Free the destination row (without destroying it) */ /* Free the destination row (without destroying it) */
rowFree(to); rowFree(to);
@ -138,7 +138,7 @@ void rowCpy (row *to, row *from) // WIP
updateRender(to); 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 */ /* reallocate the row to accomodate for the added string */
char *temp = realloc(rw->chars, rw->size + len + 1); char *temp = realloc(rw->chars, rw->size + len + 1);
@ -152,7 +152,7 @@ void rowAppendString (row *rw, char *s, int len)
updateRender(rw); updateRender(rw);
} }
void rowDeleteRow (fbuffer *b, int pos) void rowDeleteRow (FileBuffer *b, int pos)
{ {
if (b->rownum == 1) return; if (b->rownum == 1) return;
if (pos >= b->rownum) return; if (pos >= b->rownum) return;
@ -163,12 +163,12 @@ void rowDeleteRow (fbuffer *b, int pos)
} }
b->rownum--; b->rownum--;
rowFree(&b->rw[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"); //if (temp == NULL) termDie("realloc in rowDeleteRow");
b->rw = temp; b->rw = temp;
} }
void updateRender (row *rw) void updateRender (Row *rw)
{ {
/* count the special characters /* count the special characters
* spaces, utf-8 continuation chars */ * spaces, utf-8 continuation chars */

@ -6,36 +6,36 @@
* and difference between render and * and difference between render and
* real size of the row * real size of the row
* Utf-8 continuation chars */ * Utf-8 continuation chars */
typedef struct row { typedef struct Row {
int size; int size;
int r_size; int r_size;
int utf; int utf;
char *chars; char *chars;
char *render; char *render;
} row; } Row;
/* Empty row initializer */ /* Empty row initializer */
#define EROW {0, 0, 0, NULL, NULL} #define EROW {0, 0, 0, NULL, NULL}
/* Rows structure (or file buffer) /* Rows structure (or file buffer)
* defines rows and teh number of rows */ * defines rows and teh number of rows */
typedef struct fbuffer{ typedef struct FileBuffer{
row *rw; Row *rw;
int rownum; int rownum;
} fbuffer; } FileBuffer;
void bufInit (fbuffer *b); void bufInit (FileBuffer *b);
void rowAddChar (row *rw, char c, int pos); void rowAddChar (Row *rw, char c, int pos);
int rowDeleteChar (row *rw, int select, int pos); int rowDeleteChar (Row *rw, int select, int pos);
void rowCpy (row *to, row *from); void rowCpy (Row *to, Row *from);
void rowAddRow (fbuffer *b, int pos, int cur); void rowAddRow (FileBuffer *b, int pos, int cur);
void rowFree (row *rw); void rowFree (Row *rw);
void rowAppendString (row *rw, char *s, int len); void rowAppendString (Row *rw, char *s, int len);
void rowDeleteRow (fbuffer *b, int pos); void rowDeleteRow (FileBuffer *b, int pos);
void rowAddLast (fbuffer *b, char *s, int len); void rowAddLast (FileBuffer *b, char *s, int len);
void updateRender (row *rw); void updateRender (Row *rw);
int isUtf (int c); int isUtf (int c);
int isCont (int c); int isCont (int c);

@ -13,7 +13,7 @@
/* defines */ /* defines */
#define CTRL(k) ((k) & 0x1f) // Control mask modifier #define CTRL(k) ((k) & 0x1f) // Control mask modifier
#define STAT_SIZE 128 #define STAT_SIZE 128
#define SBUF_SIZE 2048 #define CBUF_SIZE 2048
#define MODE_MASK 0x1 #define MODE_MASK 0x1
#define COMMAND_MASK 0x06 #define COMMAND_MASK 0x06
@ -26,10 +26,10 @@
#define MODIFIED 0x80 #define MODIFIED 0x80
// Search buffer // Search buffer
typedef struct sbuf { typedef struct CharBuffer {
char c[SBUF_SIZE]; char c[CBUF_SIZE];
int num; int num, cur;
} sbuf; } CharBuffer;
/* main data structure containing: /* main data structure containing:
* -cursor position * -cursor position
@ -56,10 +56,10 @@ struct term {
char statusbar[STAT_SIZE]; char statusbar[STAT_SIZE];
int pad; int pad;
char mode_b; char mode_b;
sbuf search_buffer; CharBuffer search_buffer;
} t; } t;
fbuffer rows; FileBuffer rows;
const char *msg[] = { const char *msg[] = {
"Find: ", "Find: ",
@ -93,9 +93,12 @@ static void handleDel (int select);
/* testing */ /* testing */
static void updateInfo (void); static void updateInfo (void);
static int whatsThat (void); static int whatsThat (void);
static void insert (sbuf *buf, int c);
static inline void flush (sbuf *buf); static void sbInsert (CharBuffer *buf, int c);
static void pop (sbuf *buf); static inline void sbFlush (CharBuffer *buf);
static void sbPop (CharBuffer *buf);
static void sbMove (CharBuffer *buf, int x);
/* --------------------------------- main ------------------------------------ */ /* --------------------------------- main ------------------------------------ */
int main (int argc, char *argv[]) int main (int argc, char *argv[])
@ -133,7 +136,17 @@ int main (int argc, char *argv[])
case (KEY_RIGHT): case (KEY_RIGHT):
case (KEY_UP): case (KEY_UP):
case (KEY_DOWN): 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; break;
case (KEY_BACKSPACE): case (KEY_BACKSPACE):
@ -141,7 +154,7 @@ int main (int argc, char *argv[])
if ((t.mode_b & MODE_MASK) == NORMAL_M) if ((t.mode_b & MODE_MASK) == NORMAL_M)
handleDel(c); handleDel(c);
else else
pop(&t.search_buffer); sbPop(&t.search_buffer);
break; break;
case (KEY_ENTER): case (KEY_ENTER):
@ -155,7 +168,7 @@ int main (int argc, char *argv[])
editorFind(t.search_buffer.c, &t.cur.y, &t.cur.x); editorFind(t.search_buffer.c, &t.cur.y, &t.cur.x);
// Toggle mode // Toggle mode
t.mode_b ^= MODE_MASK; t.mode_b ^= MODE_MASK;
flush (&t.search_buffer); sbFlush (&t.search_buffer);
} }
break; break;
@ -170,7 +183,7 @@ int main (int argc, char *argv[])
case (CTRL('f')): case (CTRL('f')):
// Toggle mode // Toggle mode
t.mode_b ^= MODE_MASK; t.mode_b ^= MODE_MASK;
flush (&t.search_buffer); sbFlush (&t.search_buffer);
break; break;
@ -181,7 +194,7 @@ int main (int argc, char *argv[])
rowAddChar(&rows.rw[t.cur.y], c, t.cur.x); rowAddChar(&rows.rw[t.cur.y], c, t.cur.x);
t.cur.x++; t.cur.x++;
} else { } else {
insert(&t.search_buffer, c); sbInsert(&t.search_buffer, c);
} }
break; break;
} }
@ -232,6 +245,9 @@ void termInit (void)
/* Initialize the data staructure */ /* Initialize the data staructure */
t.cur.x = t.cur.off_x = 0; t.cur.x = t.cur.off_x = 0;
t.cur.y = t.cur.off_y = 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 /* 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 (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; *y = c = i;
for (i = 0; i <= rows.rw[c].size; i++) for (i = 0; i <= rows.rw[c].size; i++)
if (&rows.rw[c].chars[i] == res) break; if (&rows.rw[c].chars[i] == res) break;
@ -597,20 +613,31 @@ int editorFind (const char* needle, int *y, int *x)
return 1; 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) if (buf->num < CBUF_SIZE - 2) {
buf->c[buf->num++] = c; buf->c[buf->cur++] = c;
buf->c[buf->num] = '\0'; buf->c[++(buf->num)] = '\0';
}
} }
void flush (sbuf *buf) void sbFlush (CharBuffer *buf)
{ {
buf->num = 0; 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 ------------------------------------*/ /*--------------------------------- testing ------------------------------------*/

Binary file not shown.
Loading…
Cancel
Save