]> git.mdlowis.com Git - projs/tide.git/commitdiff
fixed several bugs in highlighter IPC. Stell need to implement language detection
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 27 Jun 2017 19:03:44 +0000 (15:03 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 27 Jun 2017 19:03:44 +0000 (15:03 -0400)
inc/edit.h
lib/colors.c
lib/view.c
tide-hl

index 55be08804dd45da059c99e3003adaf111a53e286..ead80d145b36e6b8a2663e986eea137cfd5f76a1 100644 (file)
@@ -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;
 
index 41594f8d2753df4f004f06a9d6975e29e478114e..d90e19f51d0b0215ee052ce01af07f591abf2dbe 100644 (file)
@@ -2,65 +2,49 @@
 #include <utf.h>
 #include <edit.h>
 #include <unistd.h>
+#include <ctype.h>
 
-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;
+}
index 1743853ad5a2cc872aa604c104db2d9ba9afd093..e15e9be14feb5008e22455590d88b52300da8e4a 100644 (file)
@@ -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 eae3b4a4ff54e74942413962b8190b7271e60095..faefc06ba9b659f04631993ecb2acb738105f1e6 100755 (executable)
--- 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
 
 #-------------------------------------------------------------------------------