From 1ea9be71197c912e0ebb74a075aa83dadf27395a Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Tue, 27 Jun 2017 15:03:44 -0400 Subject: [PATCH] fixed several bugs in highlighter IPC. Stell need to implement language detection --- inc/edit.h | 18 ++---------- lib/colors.c | 83 +++++++++++++++++++++++++++------------------------- lib/view.c | 4 +-- tide-hl | 23 ++++++++------- 4 files changed, 59 insertions(+), 69 deletions(-) diff --git a/inc/edit.h b/inc/edit.h index 55be088..ead80d1 100644 --- a/inc/edit.h +++ b/inc/edit.h @@ -122,19 +122,6 @@ void binsave(Buf* buf, FILE* file); /* Syntax Highlighting *****************************************************************************/ -typedef struct { - int color; - enum { END, CONT } oneol; - char* beg; - char* end; -} SyntaxRule; - -typedef struct { - char* name; - char** extensions; - SyntaxRule* rules; -} SyntaxDef; - typedef struct SyntaxSpan { size_t beg; size_t end; @@ -143,8 +130,8 @@ typedef struct SyntaxSpan { struct SyntaxSpan* next; } SyntaxSpan; -SyntaxDef* colors_find(char* path); -SyntaxSpan* colors_scan(SyntaxDef* syntax, SyntaxSpan* spans, Buf* buf, size_t beg, size_t end); +void colors_init(char* path); +SyntaxSpan* colors_scan(SyntaxSpan* spans, Buf* buf, size_t beg, size_t end); SyntaxSpan* colors_rewind(SyntaxSpan* spans, size_t first); /* Screen management functions @@ -172,7 +159,6 @@ typedef struct { Buf buffer; /* the buffer used to populate the view */ Sel selection; /* range of currently selected text */ size_t prev_csr; /* previous cursor location */ - SyntaxDef* syntax; /* syntax rules object */ SyntaxSpan* spans; /* list of colored regions */ } View; diff --git a/lib/colors.c b/lib/colors.c index 41594f8..d90e19f 100644 --- a/lib/colors.c +++ b/lib/colors.c @@ -2,65 +2,49 @@ #include #include #include +#include -int ChildIn = -1, ChildOut = -1; -char Buffer[32768]; -char* DataBeg = Buffer; -char* DataEnd = Buffer; +static int ChildIn = -1, ChildOut = -1; +static char Buffer[32768]; +static char* DataBeg = Buffer; +static char* DataEnd = Buffer; static SyntaxSpan* mkspan(size_t beg, size_t end, size_t clr, SyntaxSpan* span); -static void dump_chunk(Buf* buf, size_t beg, size_t end); +static void write_chunk(Buf* buf, size_t beg, size_t end); +static int read_byte(void); +static int read_num(void); -SyntaxDef* colors_find(char* path) { - char* ext = strrchr(path, '.'); - if (!strcmp(ext,".c") || !strcmp(ext,".h")) - cmdspawn((char*[]){ "tide-hl", "C", NULL }, &ChildIn, &ChildOut); - return NULL; +void colors_init(char* path) { + cmdspawn((char*[]){ "tide-hl", path, NULL }, &ChildIn, &ChildOut); } -int read_byte(void) { - if (DataBeg >= DataEnd) { - DataBeg = DataEnd = Buffer; - long nread = read(ChildOut, Buffer, sizeof(Buffer)); - if (nread <= 0) return EOF; - DataEnd += nread; - } - return *(DataBeg++); -} - -size_t read_num(void) { - int c; - size_t num = 0; - while (isdigit(c = read_byte())) - num = (num * 10) + (c - '0'); - return num; -} - -SyntaxSpan* colors_scan(SyntaxDef* syntax, SyntaxSpan* spans, Buf* buf, size_t beg, size_t end) { +SyntaxSpan* colors_scan(SyntaxSpan* spans, Buf* buf, size_t beg, size_t end) { SyntaxSpan* firstspan = spans; SyntaxSpan* currspan = spans; /* if the engine died, clear all highlights and quit */ - if (ChildIn < 0) + if (ChildIn < 0 || !buf->path) return colors_rewind(spans, 0); /* commence the highlighting */ - if (buf->path && end-beg) { - dump_chunk(buf, beg, end); + if (beg < end) { + write_chunk(buf, beg, end); size_t b = 0, e = 0, c = 0; do { b = read_num(); e = read_num(); c = read_num(); - if (e > 0) { + if (e > 0 && c > 0) { c = (c > 15 ? config_get_int(SynNormal + (c >> 4) - 1) : c) & 0xf; - currspan = mkspan(beg+b, beg+e, c, currspan); + currspan = mkspan(beg+b, beg+e-1, c, currspan); + //printf("(%lu-%lu) %lu,%lu,%lu\n", beg, end, b, e, c); } - //printf("(%lu-%lu) %lu,%lu,%lu\n", beg, end, b, e, c); if (!firstspan) firstspan = currspan; } while (e > 0); - //printf("done\n"); - //fflush(stdout); + //printf("left: %lu\n", DataEnd-DataBeg); + //printf("done\n\n"); + fflush(stdout); + DataBeg = DataEnd = Buffer; } return firstspan; } @@ -93,7 +77,7 @@ static SyntaxSpan* mkspan(size_t beg, size_t end, size_t clr, SyntaxSpan* span) return newspan; } -static void dump_chunk(Buf* buf, size_t beg, size_t end) { +static void write_chunk(Buf* buf, size_t beg, size_t end) { size_t len = end - beg, wlen = 0; char* wbuf = malloc(64 + len * 6u); if (len && wbuf) { @@ -101,7 +85,6 @@ static void dump_chunk(Buf* buf, size_t beg, size_t end) { for (size_t i = beg; i < end; i++) { Rune r = buf_get(buf, i); if (r == RUNE_CRLF) { - wbuf[wlen++] = '\r'; wbuf[wlen++] = '\n'; } else if (buf->charset == BINARY) { wbuf[wlen++] = (char)r; @@ -109,7 +92,10 @@ static void dump_chunk(Buf* buf, size_t beg, size_t end) { wlen += utf8encode((char*)&(wbuf[wlen]), r); } } - if (write(ChildIn, wbuf, wlen) < 0) { + long nwrite = write(ChildIn, wbuf, wlen); + //printf("write: %lu -> %d\n", wlen, nwrite); + if (nwrite < 0) { + //perror("write failed:"); /* child process probably died. shut everything down */ close(ChildIn), ChildIn = -1; close(ChildOut), ChildOut = -1; @@ -117,3 +103,20 @@ static void dump_chunk(Buf* buf, size_t beg, size_t end) { } free(wbuf); } + +static int read_byte(void) { + if (DataBeg >= DataEnd) { + DataBeg = DataEnd = Buffer; + long nread = read(ChildOut, Buffer, sizeof(Buffer)); + if (nread <= 0) return EOF; + DataEnd += nread; + } + return *(DataBeg++); +} + +static int read_num(void) { + int c, num = 0; + while (isdigit(c = read_byte())) + num = (num * 10) + (c - '0'); + return num; +} diff --git a/lib/view.c b/lib/view.c index 1743853..e15e9be 100644 --- a/lib/view.c +++ b/lib/view.c @@ -41,7 +41,7 @@ void view_init(View* view, char* file, void (*errfn)(char*)) { buf_init(&(view->buffer), errfn); if (file) { view_jumpto(view, false, buf_load(&(view->buffer), file)); - view->syntax = colors_find(view->buffer.path); + colors_init(view->buffer.path); } } @@ -110,7 +110,7 @@ void view_update(View* view, size_t* csrx, size_t* csry) { size_t scandist = config_get_int(MaxScanDist); if (scandist && first-start > scandist) start = first - scandist; - view->spans = colors_scan(view->syntax, view->spans, &(view->buffer), start, last+1); + view->spans = colors_scan(view->spans, &(view->buffer), start, last+1); apply_colors(view); } diff --git a/tide-hl b/tide-hl index eae3b4a..faefc06 100755 --- a/tide-hl +++ b/tide-hl @@ -58,12 +58,12 @@ def style_range(spos, epos, style) end def style_match(s) - style_range($scan.pos - $scan.matched_size, $scan.pos-1, s) + style_range($scan.pos - $scan.matched_size, $scan.pos, s) end def range(start=nil, stop=nil, color=nil) rule start do - beg = $scan.pos-2 + beg = $scan.pos - $scan.matched_size if not $scan.scan_until stop $scan.pos += $scan.rest_size end @@ -77,17 +77,17 @@ end #------------------------------------------------------------------------------- -Types = Set.new %w[ - bool short int long unsigned signed char size_t - void extern static inline struct typedef - int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t -] +language "C" do + Types = Set.new %w[ + bool short int long unsigned signed char size_t + void extern static inline struct typedef union + int8_t int16_t int32_t int64_t uint8_t uint16_t uint32_t uint64_t + ] -Control = Set.new %w[ - goto break return continue asm case default if else switch while for do] + Control = Set.new %w[ + goto break return continue asm case default if else switch while for do + ] -language "C" do - #match(/[0-9]+/, :Number) range start=/\/\*/, stop=/\*\//, :Comment match(/\/\/.*$/, :Comment) match(/#\s*\w+/, :PreProcessor) @@ -102,6 +102,7 @@ language "C" do style_match(:Keyword) end end + match(/[+-]?[0-9]+[ulUL]*/, :Number) end #------------------------------------------------------------------------------- -- 2.49.0