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
dbg
hello
ste.c.*
*.bak
.sconsign*
.sconsign.dblite
*.o
src/*.o

Binary file not shown.

@ -4,17 +4,17 @@
#include <string.h>
#include <ctype.h>
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 */

@ -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);

@ -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 ------------------------------------*/

Binary file not shown.
Loading…
Cancel
Save