tAdd ColorNormal, hl array, and add structural comments - ve - a minimal text editor (work in progress)
 (HTM) git clone git://src.adamsgaard.dk/ve
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit c3288feb03125b18f98b46cd0d82f9ff5ced5182
 (DIR) parent 97418d42195f0f408fd2dfd02531fb50120482fc
 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk>
       Date:   Thu,  8 Aug 2019 09:44:18 +0200
       
       Add ColorNormal, hl array, and add structural comments
       
       Diffstat:
         M config.def.h                        |       1 +
         M ve.c                                |      97 ++++++++++++++++++-------------
       
       2 files changed, 59 insertions(+), 39 deletions(-)
       ---
 (DIR) diff --git a/config.def.h b/config.def.h
       t@@ -18,6 +18,7 @@ static int status_message_timeout = 3;
         */
        static const int colors[][3] = {
                /*                  fg   bg   style  */
       +        [ColorNormal]   = {  0,   0,     0 },
                [ColorDigits]   = {  1,   0,     0 },
                [ColorComments] = {  2,   0,     0 },
                [ColorKeywords] = {  3,   0,     0 },
 (DIR) diff --git a/ve.c b/ve.c
       t@@ -16,15 +16,12 @@
        #include <unistd.h>
        
        
       -
       -/* macros */
       -
       +/* MACROS */
        #define ABUF_INIT {NULL, 0}
        #define CTRL_KEY(k) ((k) & 0x1f)
        
        
       -/* types */
       -
       +/* TYPES */
        struct abuf {
                char *b;
                int len;
       t@@ -32,10 +29,11 @@ struct abuf {
        
        /* editor row: stores a row of text */
        typedef struct eRow {
       -        char *chars;   /* text read from file */
       -        char *rchars;  /* text to render */
       -        int size;      /* length of chars */
       -        int rsize;     /* length of rchars */
       +        char *chars;          /* text read from file */
       +        char *rchars;         /* text to render */
       +        int size;             /* length of chars */
       +        int rsize;            /* length of rchars */
       +        unsigned char *hl;    /* array of rsize storing highlight type per rchar */
        } eRow;
        
        struct editor_config {
       t@@ -56,25 +54,25 @@ struct editor_config {
        };
        
        /* color scheme types */
       -enum { ColorDigits, ColorComments, ColorKeywords };
       +enum { ColorNormal, ColorDigits, ColorComments, ColorKeywords };
        enum { FG, BG, STYLE };
        
        /* configuration, allows nested code to access above variables */
        #include "config.h"
        
       -/* function declarations */
        
       +/* FUNCTION DECLARATIONS */
        char* editor_prompt(char *prompt);
        void editor_set_status_message(const char *fmt, ...);
        
        
       -/* global variables */
       -
       +/* GLOBAL VARIABLES */
        struct editor_config E;
        
        
       -/* function definitions */
       +/* FUNCTION DEFINITIONS */
        
       +/** TERMINAL **/
        void
        die(const char *s)
        {
       t@@ -173,6 +171,8 @@ get_window_size(int *rows, int *cols)
        }
        
        
       +/** SYNTAX HIGHLIGHTING **/
       +
        /* navigate over tab-representative string as one character */
        int
        editor_row_cursor_x_to_rx(eRow *row, int cursor_x)
       t@@ -187,6 +187,9 @@ editor_row_cursor_x_to_rx(eRow *row, int cursor_x)
                return rx;
        }
        
       +
       +/** ROW OPERATIONS **/
       +
        /* translate on-screen position to data position */
        int
        editor_row_cursor_rx_to_x(eRow *row, int cursor_rx)
       t@@ -252,6 +255,7 @@ editor_row_insert(int i, char *s, size_t len)
        
                E.row[i].rsize = 0;
                E.row[i].rchars = NULL;
       +        E.row[i].hl = NULL;
                editor_row_update(&E.row[i]);
        
                ++E.num_rows;
       t@@ -298,8 +302,9 @@ editor_row_delete_char(eRow *row, int i)
        void
        editor_row_free(eRow *row)
        {
       -        free(row->rchars);
                free(row->chars);
       +        free(row->rchars);
       +        free(row->hl);
        }
        
        void
       t@@ -371,6 +376,32 @@ editor_delete_char_right()
                editor_row_delete_char(row, E.cursor_x);
        }
        
       +/* convert rows to one long char array of length buflen */
       +char*
       +editor_concatenate_rows(int *buflen)
       +{
       +        int totlen, j;
       +        char *buf, *p;
       +
       +        totlen = 0;
       +        for (j=0; j<E.num_rows; ++j)
       +                totlen += E.row[j].size + 1;  /* add space for newline char */
       +        *buflen = totlen;
       +
       +        buf = malloc(totlen);
       +        p = buf;
       +        for (j=0; j<E.num_rows; ++j) {
       +                memcpy(p, E.row[j].chars, E.row[j].size);
       +                p += E.row[j].size;
       +                *p = '\n';
       +                p++;
       +        }
       +        return buf;
       +}
       +
       +
       +/** FILE IO **/
       +
        void
        file_open(char *filename)
        {
       t@@ -399,29 +430,6 @@ file_open(char *filename)
                E.file_changed = 0;
        }
        
       -/* convert rows to one long char array of length buflen */
       -char*
       -editor_concatenate_rows(int *buflen)
       -{
       -        int totlen, j;
       -        char *buf, *p;
       -
       -        totlen = 0;
       -        for (j=0; j<E.num_rows; ++j)
       -                totlen += E.row[j].size + 1;  /* add space for newline char */
       -        *buflen = totlen;
       -
       -        buf = malloc(totlen);
       -        p = buf;
       -        for (j=0; j<E.num_rows; ++j) {
       -                memcpy(p, E.row[j].chars, E.row[j].size);
       -                p += E.row[j].size;
       -                *p = '\n';
       -                p++;
       -        }
       -        return buf;
       -}
       -
        void
        file_save(char *filename)
        {
       t@@ -457,6 +465,9 @@ file_save(char *filename)
                                          strerror(errno));
        }
        
       +
       +/** FIND **/
       +
        /* reverse of strstr (3) */
        char*
        strrstr(char *haystack, char *needle,
       t@@ -560,6 +571,8 @@ editor_find(int direction)
        }
        
        
       +/** OUTPUT **/
       +
        /* reallocate append buffer to hold string s */
        void
        ab_append(struct abuf *ab, const char *s, int len)
       t@@ -780,6 +793,9 @@ editor_set_status_message(const char *fmt, ...)
                }
        }
        
       +
       +/** INPUT **/
       +
        /* prompt is expected to be a format string containing a %s */
        char*
        editor_prompt(char *prompt)
       t@@ -1027,6 +1043,9 @@ editor_process_keypress()
                }
        }
        
       +
       +/** INIT **/
       +
        void
        deinit_editor() {
                int i;
       t@@ -1061,7 +1080,7 @@ init_editor() {
        }
        
        
       -/* main */
       +/** MAIN **/
        int
        main(int argc, char* argv[])
        {