Browse code

Add alternative dark theme, but left it disabled under a pair of #ifdef's (search for 'forest')

Dario Rodriguez authored on 01/01/2024 11:40:55
Showing 1 changed files
... ...
@@ -66,6 +66,7 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
66 66
         highlighter_t *hl;
67 67
         if(redata==NULL || slot==NULL)
68 68
                 return(-1);
69
+#if 1
69 70
         /* "forest" theme */
70 71
         colors=hl_initcolors(&ncolors,
71 72
                              color_keyword,"\x38\x17\x1e\xff",
... ...
@@ -83,6 +84,25 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
83 84
                              color_symbol,"\x69\x2a\x44\xff",
84 85
                              color_endenum,DEFAULTCOLOR,
85 86
                              -1);
87
+#else
88
+        /* "alternative" theme (dark) */
89
+        colors=hl_initcolors(&ncolors,
90
+                             color_keyword,"\xcc\x99\xcc\xff",//
91
+                             color_directive,"\x38\x4b\x00\xff",
92
+                             color_directivekeyword,"\x63\x7d\x16\xff",
93
+                             color_directivestring,"\x07\x20\x3b\xff",
94
+                             color_directiveinclude,"\x16\x63\x7d\xff",
95
+                             color_directiveincludestring,"\x6\x13\x2d\xff",
96
+                             color_normal,"\xf2\xf0\xec\xff",//
97
+                             color_string,"\x99\xcc\x99\xff",//
98
+                             color_multilinecomment,"\x74\x73\x69\xff",//
99
+                             color_linecomment,"\x74\x73\x69\xff",//
100
+                             color_number,"\xf9\x91\x57\xff",//
101
+                             color_operator,"\x66\xcc\xcc\xff",//
102
+                             color_symbol,"\x91\xa7\xff\xff",//
103
+                             color_endenum,"\xf2\xf0\xec\xff",//
104
+                             -1);
105
+#endif
86 106
         if(colors==NULL || (hl=malloc(sizeof(highlighter_t)))==NULL) {
87 107
                 if(colors!=NULL)
88 108
                         free(colors),colors=NULL;
Browse code

Fix: matching parenthesis was wrong when passing over a '(' or ')'; now they are treated as in strings. Also: small clean-ups in re_plugin_highlighter.c

Dario Rodriguez authored on 25/09/2023 14:37:58
Showing 1 changed files
... ...
@@ -171,7 +171,6 @@ redata_highlighter_getcolorindex(redata_t *redata, int line, int nthbyte)
171 171
 static void
172 172
 redata_highlighter_util_applytheme(int *template3,hcolor_t *color,int invert)
173 173
 {
174
-        unsigned char c[3];
175 174
         if(template3==NULL || color==NULL || template3[0]<0 || template3[0]>2 || template3[1]<0 || template3[1]>2 || template3[2]<0 || template3[2]>2)
176 175
                 return; /* sanity check error */
177 176
         if(invert==0) {
... ...
@@ -200,7 +199,7 @@ redata_highlighter_settheme(redata_t *redata, int ntheme, int invert)
200 199
                 return(-1);
201 200
         for(i=0;i<ncolors;i++)
202 201
                 redata_highlighter_util_applytheme(permutationtemplate,colors+i,invert);
203
-        return;
202
+        return(0);
204 203
 }
205 204
 
206 205
 static int
... ...
@@ -354,6 +353,7 @@ hl_doline(redata_t *redata, highlighter_t *hl, int nline)
354 353
                 mode_in_linecomment,
355 354
                 mode_in_directive,
356 355
                 mode_in_string,
356
+                mode_in_singlestring,
357 357
                 mode_in_directivestring,
358 358
         } mode,mlcprevmode;
359 359
         if(redata==NULL || hl==NULL || nline<0)
... ...
@@ -471,6 +471,11 @@ hl_doline(redata_t *redata, highlighter_t *hl, int nline)
471 471
                                         linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_string);
472 472
                                         continue;
473 473
                                 }
474
+                                if(ptr[i]=='\'') {
475
+                                        mode=mode_in_singlestring;
476
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_string);
477
+                                        continue;
478
+                                }
474 479
                                 if(!cant_define && ptr[i]=='#') {
475 480
                                         mode=mode_in_directive;
476 481
                                         is_directiveinclude=0;
... ...
@@ -592,11 +597,12 @@ hl_doline(redata_t *redata, highlighter_t *hl, int nline)
592 597
                                 linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,(is_directiveinclude==0)?color_directivestring:color_directiveincludestring);
593 598
                                 continue;
594 599
                         }
595
-                        if(mode==mode_in_string) {
600
+                        if(mode==mode_in_string || mode_in_singlestring) {
596 601
                                 if(prev_char=='\\' || ptr[i]=='\\') {
597 602
                                         continue;
598 603
                                 }
599
-                                if(ptr[i]=='\"') {
604
+                                if((mode==mode_in_string && ptr[i]=='\"')
605
+                                  || (mode==mode_in_singlestring && ptr[i]=='\'')) {
600 606
                                         mode=mode_whatever;
601 607
                                         linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_string);
602 608
                                         continue;
Browse code

Color themes and black mode, using control-shift-'+'/'-'/'0'.

Dario Rodriguez authored on 28/06/2023 20:21:08
Showing 1 changed files
... ...
@@ -168,6 +168,41 @@ redata_highlighter_getcolorindex(redata_t *redata, int line, int nthbyte)
168 168
         return(-1);
169 169
 }
170 170
 
171
+static void
172
+redata_highlighter_util_applytheme(int *template3,hcolor_t *color,int invert)
173
+{
174
+        unsigned char c[3];
175
+        if(template3==NULL || color==NULL || template3[0]<0 || template3[0]>2 || template3[1]<0 || template3[1]>2 || template3[2]<0 || template3[2]>2)
176
+                return; /* sanity check error */
177
+        if(invert==0) {
178
+                ((unsigned char *)color->rgba)[0]=((unsigned char *)color->origrgba)[template3[0]];
179
+                ((unsigned char *)color->rgba)[1]=((unsigned char *)color->origrgba)[template3[1]];
180
+                ((unsigned char *)color->rgba)[2]=((unsigned char *)color->origrgba)[template3[2]];
181
+        } else {
182
+                ((unsigned char *)color->rgba)[0]=255-((unsigned char *)color->origrgba)[template3[0]];
183
+                ((unsigned char *)color->rgba)[1]=255-((unsigned char *)color->origrgba)[template3[1]];
184
+                ((unsigned char *)color->rgba)[2]=255-((unsigned char *)color->origrgba)[template3[2]];
185
+        }
186
+        return;
187
+}
188
+
189
+int
190
+redata_highlighter_settheme(redata_t *redata, int ntheme, int invert)
191
+{
192
+        int permutations[6][3]={{0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,1,0},{2,0,1}};
193
+        int *permutationtemplate;
194
+        hcolor_t *colors;
195
+        int ncolors,i;
196
+        ntheme=(ntheme<0)?0:ntheme;
197
+        ntheme%=6;
198
+        permutationtemplate=permutations[ntheme];
199
+        if((colors=redata_highlighter_getcolors(redata, &ncolors))==NULL)
200
+                return(-1);
201
+        for(i=0;i<ncolors;i++)
202
+                redata_highlighter_util_applytheme(permutationtemplate,colors+i,invert);
203
+        return;
204
+}
205
+
171 206
 static int
172 207
 redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo, int is_unadd)
173 208
 {
... ...
@@ -248,6 +283,8 @@ hl_initcolors(int *ncolors, /* int color, char *colordef, */ ...)
248 283
                         colordef=va_arg(paramlist,char *);
249 284
                         maxcolor=(maxcolor<color)?color:maxcolor;
250 285
                         if(round==1) {
286
+                                strncpy(hcolors[color].origrgba,colordef,sizeof(hcolors[color].origrgba));
287
+                                hcolors[color].origrgba[sizeof(hcolors[color].origrgba)-1]='\0';
251 288
                                 strncpy(hcolors[color].rgba,colordef,sizeof(hcolors[color].rgba));
252 289
                                 hcolors[color].rgba[sizeof(hcolors[color].rgba)-1]='\0';
253 290
                         }
... ...
@@ -788,3 +825,4 @@ hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int poso
788 825
         return(linecolor);
789 826
 }
790 827
 
828
+
Browse code

bugfix: directive string color literal was wrong in the highlighter plugin

Dario Rodriguez authored on 14/04/2023 18:48:09
Showing 1 changed files
... ...
@@ -71,7 +71,7 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
71 71
                              color_keyword,"\x38\x17\x1e\xff",
72 72
                              color_directive,"\x38\x4b\x00\xff",
73 73
                              color_directivekeyword,"\x63\x7d\x16\xff",
74
-                             color_directivestring,"\x07\x20\3b\xff",
74
+                             color_directivestring,"\x07\x20\x3b\xff",
75 75
                              color_directiveinclude,"\x16\x63\x7d\xff",
76 76
                              color_directiveincludestring,"\x6\x13\x2d\xff",
77 77
                              color_normal,"\x9a\x67\x43\xff",
Browse code

Update author email

Dario Rodriguez authored on 07/01/2022 17:57:32
Showing 1 changed files
... ...
@@ -5,7 +5,7 @@
5 5
  *
6 6
  * re_data plugin to support the syntax highlighter.
7 7
  *
8
- * Author: Dario Rodriguez dario@softhome.net
8
+ * Author: Dario Rodriguez antartica@whereismybit.com
9 9
  * This program is licensed under the terms of GNU GPL v2.1+
10 10
  */
11 11
 
Browse code

fix: last line was not being highlighted in some circumstances

Dario Rodriguez authored on 28/12/2020 12:07:38
Showing 1 changed files
... ...
@@ -193,7 +193,7 @@ redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t
193 193
                 }
194 194
         }
195 195
         /* special case: check if pos is inside last line */
196
-        if(nline>=hl->usedlines && (hl->lines[hl->usedlines-1].pos+hl->lines[hl->usedlines-1].len)>=pos)
196
+        if(nline>=hl->usedlines)
197 197
                 nline=hl->usedlines-1;
198 198
         /* invalidate from this line on */
199 199
         nline=(nline<0)?0:nline;
... ...
@@ -365,7 +365,7 @@ hl_doline(redata_t *redata, highlighter_t *hl, int nline)
365 365
         /* make sure we have previous lines highlighted */
366 366
         for(i=hl->usedlines;i<nline;i++) {
367 367
                 if(hl_doline(redata,hl,i)==-1)
368
-                        return(-1); /* error highlighting line */
368
+                        return(-1); /* error highlighting line */             
369 369
         }
370 370
         hl_invalidatefrom(redata,hl,nline);
371 371
         line=hl->lines+nline;
... ...
@@ -391,7 +391,7 @@ hl_doline(redata_t *redata, highlighter_t *hl, int nline)
391 391
         mlcprevmode=mode_whatever;
392 392
         do {
393 393
                 if(redata_line_rawinfo(redata,realpos+posoffset,&pos,&ptr,&len,NULL)==-1)
394
-                        return(-1); /* couldn't get line data */
394
+                        return(-1); /* couldn't get line data */                
395 395
                 has_nl=((len>0 && ptr[len-1]=='\n')?1:0);
396 396
                 has_next=(len==0)?1:(ptr[len-1]=='\n')?0:1;
397 397
                 /* special case: line with only a newline */
... ...
@@ -788,4 +788,3 @@ hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int poso
788 788
         return(linecolor);
789 789
 }
790 790
 
791
-
Browse code

fix plugins not being called on undo

Dario Rodriguez authored on 01/12/2020 21:24:22
Showing 1 changed files
... ...
@@ -46,8 +46,6 @@ typedef enum colorsenum_t {
46 46
 } colorsenum_t;
47 47
 
48 48
 
49
-static int redata_highlighter_add(redata_t *redata, redata_plugin_t *slot, undo_t *undo);
50
-static int redata_highlighter_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo);
51 49
 static int redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo, int is_unadd);
52 50
 static int redata_highlighter_postload(redata_t *redata, redata_plugin_t *slot,char *filename);
53 51
 
... ...
@@ -68,6 +66,7 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
68 66
         highlighter_t *hl;
69 67
         if(redata==NULL || slot==NULL)
70 68
                 return(-1);
69
+        /* "forest" theme */
71 70
         colors=hl_initcolors(&ncolors,
72 71
                              color_keyword,"\x38\x17\x1e\xff",
73 72
                              color_directive,"\x38\x4b\x00\xff",
... ...
@@ -97,8 +96,7 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
97 96
         slot->name[sizeof(slot->name)-1]='\0';
98 97
         slot->unregister=redata_highlighter_unregister;
99 98
         slot->postload=redata_highlighter_postload;
100
-        slot->add=redata_highlighter_add;
101
-        slot->unadd=redata_highlighter_unadd;
99
+        slot->add_or_unadd=redata_highlighter_add_or_unadd;
102 100
         slot->userptr=hl;
103 101
         return(0);
104 102
 }
... ...
@@ -170,36 +168,20 @@ redata_highlighter_getcolorindex(redata_t *redata, int line, int nthbyte)
170 168
         return(-1);
171 169
 }
172 170
 
173
-
174
-static int
175
-redata_highlighter_add(redata_t *redata, redata_plugin_t *slot, undo_t *undo)
176
-{
177
-        return(redata_highlighter_add_or_unadd(redata, slot, undo, 0));
178
-}
179
-
180
-static int
181
-redata_highlighter_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo)
182
-{
183
-        return(redata_highlighter_add_or_unadd(redata, slot, undo, 1));
184
-}
185
-
186
-
187 171
 static int
188 172
 redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo, int is_unadd)
189 173
 {
190 174
         long pos;
191 175
         int nline;
192
-        undostack_t *stack;
193 176
         highlighter_t *hl=(highlighter_t *) ((slot!=NULL)?(slot->userptr):NULL);
194
-        stack=redata_getstack(redata,undo);
195
-        if(redata==NULL || slot==NULL || hl==NULL || undo==NULL || stack==NULL || slot->active==0)
177
+        if(redata==NULL || slot==NULL || hl==NULL || undo==NULL || slot->active==0)
196 178
                 return(-1); /* sanity check failed */
197 179
         if(hl->usedlines==0) {
198 180
                 return(0); /* nothing to do */
199 181
         }
200 182
         /* get the first pos of the operation */
201 183
         pos=undo->posorig;
202
-        if(undo->type=='D')
184
+        if((!is_unadd && undo->type=='D') || (is_unadd && undo->type=='A'))
203 185
                 pos-=undo->len;
204 186
         if(undo->type=='M' && undo->posdest<pos)
205 187
                 pos=undo->posdest;
Browse code

highlighter: fix bug when highlighting files with many lines (didn't enlarge the buffer for linecolor_t because of missing operation on usedbuf)

Dario Rodriguez authored on 27/11/2020 17:13:41
Showing 1 changed files
... ...
@@ -517,7 +517,7 @@ hl_doline(redata_t *redata, highlighter_t *hl, int nline)
517 517
                                 if(prev_char=='/' && ptr[i]=='/') {
518 518
                                         mode=mode_in_linecomment;
519 519
                                         linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i-1,color_linecomment);
520
-                                         linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_linecomment);
520
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_linecomment);
521 521
                                         continue;
522 522
                                 }
523 523
                                 if(prev_char=='/' && ptr[i]=='*') {
... ...
@@ -704,6 +704,7 @@ hl_C_getdirectives(char ***directives,int *ndirectives, int *maxlen)
704 704
 "line",
705 705
 "error",
706 706
 "pragma",
707
+NULL
707 708
 };
708 709
         if(directives==NULL || ndirectives==NULL)
709 710
                 return(-1);
... ...
@@ -767,6 +768,7 @@ hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int poso
767 768
                 linecolor->len=1;
768 769
                 linecolor->color=color;
769 770
                 *opaque=1;
771
+                hl->usedbuf=line->off+line->len;
770 772
                 return(linecolor);
771 773
         }
772 774
         /* if posoff was already done, truncate */
... ...
@@ -800,7 +802,7 @@ hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int poso
800 802
         linecolor->color=color;
801 803
         *opaque+=1;
802 804
         line->len+=sizeof(linecolor_t);
803
-        hl->usedbuf+=sizeof(linecolor_t);
805
+        hl->usedbuf=line->off+line->len;
804 806
         return(linecolor);
805 807
 }
806 808
 
Browse code

hightlighter: tweak default colors

Dario Rodriguez authored on 22/11/2020 10:40:04
Showing 1 changed files
... ...
@@ -75,8 +75,8 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
75 75
                              color_directivestring,"\x07\x20\3b\xff",
76 76
                              color_directiveinclude,"\x16\x63\x7d\xff",
77 77
                              color_directiveincludestring,"\x6\x13\x2d\xff",
78
-                             color_normal,"\x9d\x15\x00\xff",
79
-                             color_string,"\x68\x00\x01\xff",
78
+                             color_normal,"\x9a\x67\x43\xff",
79
+                             color_string,"\x9d\x15\x00\xff",
80 80
                              color_multilinecomment,"\x4f\x40\x57\xff",
81 81
                              color_linecomment,"\xc6\x8c\xa4\xff",
82 82
                              color_number,"\x3b\x10\x35\xff",
Browse code

Implement highlight matching bracket (parens/curly-braces/square-brackets/angle-brackets)

Dario Rodriguez authored on 23/10/2020 22:51:48
Showing 1 changed files
... ...
@@ -69,13 +69,13 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
69 69
         if(redata==NULL || slot==NULL)
70 70
                 return(-1);
71 71
         colors=hl_initcolors(&ncolors,
72
-                             color_normal,"\x38\x17\x1e\xff",
72
+                             color_keyword,"\x38\x17\x1e\xff",
73 73
                              color_directive,"\x38\x4b\x00\xff",
74 74
                              color_directivekeyword,"\x63\x7d\x16\xff",
75 75
                              color_directivestring,"\x07\x20\3b\xff",
76 76
                              color_directiveinclude,"\x16\x63\x7d\xff",
77 77
                              color_directiveincludestring,"\x6\x13\x2d\xff",
78
-                             color_keyword,"\x9d\x15\x00\xff",
78
+                             color_normal,"\x9d\x15\x00\xff",
79 79
                              color_string,"\x68\x00\x01\xff",
80 80
                              color_multilinecomment,"\x4f\x40\x57\xff",
81 81
                              color_linecomment,"\xc6\x8c\xa4\xff",
... ...
@@ -155,6 +155,22 @@ redata_highlighter_getline(redata_t *redata, int line, int *nlinecolors)
155 155
         return((linecolor_t *) (hl->buf+hl->lines[line].off));
156 156
 }
157 157
 
158
+int
159
+redata_highlighter_getcolorindex(redata_t *redata, int line, int nthbyte)
160
+{
161
+        int i,n;
162
+        int nlinecolors;
163
+        linecolor_t *linecolors;
164
+        if((linecolors=redata_highlighter_getline(redata,line,&nlinecolors))==NULL)
165
+                return(-1);
166
+        for(i=0,n=0;n<nlinecolors;i+=linecolors[n].len,n++) {
167
+                if(nthbyte<(i+linecolors[n].len))
168
+                        return(linecolors[n].color);
169
+        }
170
+        return(-1);
171
+}
172
+
173
+
158 174
 static int
159 175
 redata_highlighter_add(redata_t *redata, redata_plugin_t *slot, undo_t *undo)
160 176
 {
Browse code

highlighting: remove temp. debug code

Dario Rodriguez authored on 19/10/2020 21:16:06
Showing 1 changed files
... ...
@@ -179,9 +179,6 @@ redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t
179 179
         if(redata==NULL || slot==NULL || hl==NULL || undo==NULL || stack==NULL || slot->active==0)
180 180
                 return(-1); /* sanity check failed */
181 181
         if(hl->usedlines==0) {
182
-#if 0
183
-fprintf(stderr,"INVALIDATING USEDLINELINES==0 (nothing to do)\n");
184
-#endif
185 182
                 return(0); /* nothing to do */
186 183
         }
187 184
         /* get the first pos of the operation */
... ...
@@ -203,9 +200,6 @@ fprintf(stderr,"INVALIDATING USEDLINELINES==0 (nothing to do)\n");
203 200
         /* invalidate from this line on */
204 201
         nline=(nline<0)?0:nline;
205 202
         hl_invalidatefrom(redata,hl,nline);
206
-#if 0
207
-fprintf(stderr,"INVALIDATING LINE %i\n",nline);
208
-#endif
209 203
         return(0);
210 204
 }
211 205
 
... ...
@@ -372,9 +366,6 @@ hl_doline(redata_t *redata, highlighter_t *hl, int nline)
372 366
         }
373 367
         /* make sure we have previous lines highlighted */
374 368
         for(i=hl->usedlines;i<nline;i++) {
375
-#if 0
376
-fprintf(stderr,"Recursing from %i to %i\n",nline,i);
377
-#endif
378 369
                 if(hl_doline(redata,hl,i)==-1)
379 370
                         return(-1); /* error highlighting line */
380 371
         }
... ...
@@ -395,77 +386,24 @@ fprintf(stderr,"Recursing from %i to %i\n",nline,i);
395 386
         opaque=0;
396 387
         linecolor=hl_addtolinecolor(&opaque,hl,NULL,color_normal,0);
397 388
         posoffset=0;
398
-#if 0
399
-fprintf(stderr,"Doing line %i pos:%li\n",nline,line->pos);
400
-#endif
401 389
         prev_char='\0';
402 390
         prev_char_mask=0xff;
403 391
         hl->usedkeywordbuf=0;
404 392
         hl->useddirectivebuf=0;
405 393
         mlcprevmode=mode_whatever;
406 394
         do {
407
-                if(redata_line_rawinfo(redata,realpos+posoffset,&pos,&ptr,&len,NULL)==-1) {
408
-#if 0
409
-fprintf(stderr,"nline:%i ERROR: couldn't get lineinfo for realpos:%li posoffset:%i realpos+posoffset:%li redataused:%li\n",
410
-nline,realpos,posoffset,realpos+posoffset,redata_getused(redata));
411
-#endif
395
+                if(redata_line_rawinfo(redata,realpos+posoffset,&pos,&ptr,&len,NULL)==-1)
412 396
                         return(-1); /* couldn't get line data */
413
-                }
414 397
                 has_nl=((len>0 && ptr[len-1]=='\n')?1:0);
415 398
                 has_next=(len==0)?1:(ptr[len-1]=='\n')?0:1;
416 399
                 /* special case: line with only a newline */
417 400
                 if(posoffset==0 && has_nl==1 && len==1) {
418 401
                         /* delete the existing linecolor and break */
419 402
                         line->len=0;
420
-#if 0
421
-fprintf(stderr,"nline:%i detected blank line, exiting\n",nline);
422
-#endif
423 403
                         break;
424 404
                 }
425 405
                 /* iterate */
426
-#if 0
427
-#define HDEBUGLINE 775
428
-if(nline==HDEBUGLINE || nline==(HDEBUGLINE+1)) {
429
-fprintf(stderr,"Line %i chunk at posoffset:%i (realpos:%li)\n",nline, posoffset,realpos);
430
-fprintf(stderr,"NEW RAWINFO, nline:%i len:%i has_nl:%i has_next:%i mode:%s(%i) mlcprevmode:%s(%i)\n",nline,len,has_nl,has_next,
431
-mode==mode_whatever?"whatever":
432
-mode==mode_in_multilinecomment?"mlc":
433
-mode==mode_in_linecomment?"comment":
434
-mode==mode_in_directive?"directive":
435
-mode==mode_in_string?"string":
436
-mode==mode_in_directivestring?"directivestring":"unknown",
437
-mode,
438
-mlcprevmode==mode_whatever?"whatever":
439
-mlcprevmode==mode_in_multilinecomment?"mlc":
440
-mlcprevmode==mode_in_linecomment?"comment":
441
-mlcprevmode==mode_in_directive?"directive":
442
-mlcprevmode==mode_in_string?"string":
443
-mlcprevmode==mode_in_directivestring?"directivestring":"unknown",
444
-mlcprevmode
445
-);
446
-}
447
-#endif
448 406
                 for(i=0;i<(len-has_nl);prev_char=(ptr[i]&prev_char_mask),prev_char_mask=0xff,i++) {
449
-#if 0
450
-if(nline==HDEBUGLINE || nline==(HDEBUGLINE+1)) {
451
-fprintf(stderr,"ptr[%i]='%c' prev_char='%s%c' mode=%s(%i) mlcprevmode=%s(%i)\n",i,ptr[i],(prev_char=='\0')?"\\":"",(prev_char=='\0')?'0':prev_char,
452
-mode==mode_whatever?"whatever":
453
-mode==mode_in_multilinecomment?"mlc":
454
-mode==mode_in_linecomment?"comment":
455
-mode==mode_in_directive?"directive":
456
-mode==mode_in_string?"string":
457
-mode==mode_in_directivestring?"directivestring":"unknown",
458
-mode,
459
-mlcprevmode==mode_whatever?"whatever":
460
-mlcprevmode==mode_in_multilinecomment?"mlc":
461
-mlcprevmode==mode_in_linecomment?"comment":
462
-mlcprevmode==mode_in_directive?"directive":
463
-mlcprevmode==mode_in_string?"string":
464
-mlcprevmode==mode_in_directivestring?"directivestring":"unknown",
465
-mlcprevmode
466
-);
467
-}
468
-#endif
469 407
                         /* special case: keyword ends in a change of mode */
470 408
                         if(mode!=mode_whatever && hl->usedkeywordbuf>0) {
471 409
                                 if(hl_searchlist(keywords,nkeywords,hl->keywordbuf,hl->usedkeywordbuf,NULL)==0) {
... ...
@@ -793,31 +731,6 @@ hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int poso
793 731
         hline_t *line;
794 732
         if(opaque==NULL || *opaque<0 || hl==NULL || (posoff!=0 && linecolor==NULL) || color<0 || posoff<0)
795 733
                 return(NULL); /* sanity check failed */
796
-#if 0
797
-#define DEBUGLINE 775
798
-if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
799
-int i,k;
800
-int l=hl->lines[hl->usedlines].len;
801
-linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
802
-fprintf(stderr,"addtolinecolor pre.: nline:%i *opaque:%i posoff:%i color:%i current:\"",hl->usedlines,*opaque,posoff,color);
803
-for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
804
-        for(i=0;i<lc->len;i++)
805
-                fprintf(stderr,"%x",lc->color);
806
-}
807
-hl->keywordbuf[hl->usedkeywordbuf]='\0';
808
-hl->directivebuf[hl->useddirectivebuf]='\0';
809
-fprintf(stderr,"\" (%i%s%s%s,%i%s%s%s)\n",
810
-hl->usedkeywordbuf,
811
-(hl->usedkeywordbuf==0)?"":" \"",
812
-(hl->usedkeywordbuf==0)?"":hl->keywordbuf,
813
-(hl->usedkeywordbuf==0)?"":"\"",
814
-hl->useddirectivebuf,
815
-(hl->useddirectivebuf==0)?"":" \"",
816
-(hl->useddirectivebuf==0)?"":hl->directivebuf,
817
-(hl->useddirectivebuf==0)?"":"\""
818
-);
819
-}
820
-#endif
821 734
         /* make sure there is a space for a linecolor_t in buf */
822 735
         if((hl->usedbuf+sizeof(linecolor_t))>=hl->sizebuf) {
823 736
                 char *newbuf;
... ...
@@ -838,19 +751,6 @@ hl->useddirectivebuf,
838 751
                 linecolor->len=1;
839 752
                 linecolor->color=color;
840 753
                 *opaque=1;
841
-#if 0
842
-if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
843
-int i,k;
844
-int l=hl->lines[hl->usedlines].len;
845
-linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
846
-fprintf(stderr,"addtolinecolor post: nline:%i *opaque:%i posoff:%i color:%i         \"",hl->usedlines,*opaque,posoff,color);
847
-for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
848
-        for(i=0;i<lc->len;i++)
849
-                fprintf(stderr,"%x",lc->color);
850
-}
851
-fprintf(stderr,"\"\n");
852
-}
853
-#endif
854 754
                 return(linecolor);
855 755
         }
856 756
         /* if posoff was already done, truncate */
... ...
@@ -877,19 +777,6 @@ fprintf(stderr,"\"\n");
877 777
                 int added=(posoff-*opaque)+1;
878 778
                 linecolor[-1].len+=added;
879 779
                 *opaque+=added;
880
-#if 0
881
-if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
882
-int i,k;
883
-int l=hl->lines[hl->usedlines].len;
884
-linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
885
-fprintf(stderr,"addtolinecolor post: nline:%i *opaque:%i posoff:%i color:%i         \"",hl->usedlines,*opaque,posoff,color);
886
-for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
887
-        for(i=0;i<lc->len;i++)
888
-                fprintf(stderr,"%x",lc->color);
889
-}
890
-fprintf(stderr,"\"\n");
891
-}
892
-#endif
893 780
                 return(linecolor-1);
894 781
         }
895 782
         /* add new linecolor */
... ...
@@ -898,19 +785,6 @@ fprintf(stderr,"\"\n");
898 785
         *opaque+=1;
899 786
         line->len+=sizeof(linecolor_t);
900 787
         hl->usedbuf+=sizeof(linecolor_t);
901
-#if 0
902
-if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
903
-int i,k;
904
-int l=hl->lines[hl->usedlines].len;
905
-linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
906
-fprintf(stderr,"addtolinecolor post: nline:%i *opaque:%i posoff:%i color:%i         \"",hl->usedlines,*opaque,posoff,color);
907
-for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
908
-        for(i=0;i<lc->len;i++)
909
-                fprintf(stderr,"%x",lc->color);
910
-}
911
-fprintf(stderr,"\"\n");
912
-}
913
-#endif
914 788
         return(linecolor);
915 789
 }
916 790
 
Browse code

highlighter: fix check for detecting end of chunks of current line (problem was: multiline comment end not recognized)

Dario Rodriguez authored on 19/10/2020 21:11:41
Showing 1 changed files
... ...
@@ -179,7 +179,7 @@ redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t
179 179
         if(redata==NULL || slot==NULL || hl==NULL || undo==NULL || stack==NULL || slot->active==0)
180 180
                 return(-1); /* sanity check failed */
181 181
         if(hl->usedlines==0) {
182
-#if 1
182
+#if 0
183 183
 fprintf(stderr,"INVALIDATING USEDLINELINES==0 (nothing to do)\n");
184 184
 #endif
185 185
                 return(0); /* nothing to do */
... ...
@@ -203,7 +203,7 @@ fprintf(stderr,"INVALIDATING USEDLINELINES==0 (nothing to do)\n");
203 203
         /* invalidate from this line on */
204 204
         nline=(nline<0)?0:nline;
205 205
         hl_invalidatefrom(redata,hl,nline);
206
-#if 1
206
+#if 0
207 207
 fprintf(stderr,"INVALIDATING LINE %i\n",nline);
208 208
 #endif
209 209
         return(0);
... ...
@@ -404,25 +404,67 @@ fprintf(stderr,"Doing line %i pos:%li\n",nline,line->pos);
404 404
         hl->useddirectivebuf=0;
405 405
         mlcprevmode=mode_whatever;
406 406
         do {
407
-                if(redata_line_rawinfo(redata,realpos+posoffset,&pos,&ptr,&len,NULL)==-1)
407
+                if(redata_line_rawinfo(redata,realpos+posoffset,&pos,&ptr,&len,NULL)==-1) {
408
+#if 0
409
+fprintf(stderr,"nline:%i ERROR: couldn't get lineinfo for realpos:%li posoffset:%i realpos+posoffset:%li redataused:%li\n",
410
+nline,realpos,posoffset,realpos+posoffset,redata_getused(redata));
411
+#endif
408 412
                         return(-1); /* couldn't get line data */
413
+                }
409 414
                 has_nl=((len>0 && ptr[len-1]=='\n')?1:0);
410 415
                 has_next=(len==0)?1:(ptr[len-1]=='\n')?0:1;
411 416
                 /* special case: line with only a newline */
412 417
                 if(posoffset==0 && has_nl==1 && len==1) {
413 418
                         /* delete the existing linecolor and break */
414 419
                         line->len=0;
420
+#if 0
421
+fprintf(stderr,"nline:%i detected blank line, exiting\n",nline);
422
+#endif
415 423
                         break;
416 424
                 }
417 425
                 /* iterate */
418 426
 #if 0
419
-fprintf(stderr,"Line %i chunk at posoffset %i\n",nline, posoffset);
427
+#define HDEBUGLINE 775
428
+if(nline==HDEBUGLINE || nline==(HDEBUGLINE+1)) {
429
+fprintf(stderr,"Line %i chunk at posoffset:%i (realpos:%li)\n",nline, posoffset,realpos);
430
+fprintf(stderr,"NEW RAWINFO, nline:%i len:%i has_nl:%i has_next:%i mode:%s(%i) mlcprevmode:%s(%i)\n",nline,len,has_nl,has_next,
431
+mode==mode_whatever?"whatever":
432
+mode==mode_in_multilinecomment?"mlc":
433
+mode==mode_in_linecomment?"comment":
434
+mode==mode_in_directive?"directive":
435
+mode==mode_in_string?"string":
436
+mode==mode_in_directivestring?"directivestring":"unknown",
437
+mode,
438
+mlcprevmode==mode_whatever?"whatever":
439
+mlcprevmode==mode_in_multilinecomment?"mlc":
440
+mlcprevmode==mode_in_linecomment?"comment":
441
+mlcprevmode==mode_in_directive?"directive":
442
+mlcprevmode==mode_in_string?"string":
443
+mlcprevmode==mode_in_directivestring?"directivestring":"unknown",
444
+mlcprevmode
445
+);
446
+}
420 447
 #endif
421
-#warning FALTA PONER DE UN COLOR DIFERENTE lo de <nombre.h> de los #include (pero no de los #define!)
422 448
                 for(i=0;i<(len-has_nl);prev_char=(ptr[i]&prev_char_mask),prev_char_mask=0xff,i++) {
423 449
 #if 0
424
-if(nline==1)
425
-        fprintf(stderr,"ptr[%i]='%c' prev_char='%s%c'\n",i,ptr[i],(prev_char=='\0')?"\\":"",(prev_char=='\0')?'0':prev_char);
450
+if(nline==HDEBUGLINE || nline==(HDEBUGLINE+1)) {
451
+fprintf(stderr,"ptr[%i]='%c' prev_char='%s%c' mode=%s(%i) mlcprevmode=%s(%i)\n",i,ptr[i],(prev_char=='\0')?"\\":"",(prev_char=='\0')?'0':prev_char,
452
+mode==mode_whatever?"whatever":
453
+mode==mode_in_multilinecomment?"mlc":
454
+mode==mode_in_linecomment?"comment":
455
+mode==mode_in_directive?"directive":
456
+mode==mode_in_string?"string":
457
+mode==mode_in_directivestring?"directivestring":"unknown",
458
+mode,
459
+mlcprevmode==mode_whatever?"whatever":
460
+mlcprevmode==mode_in_multilinecomment?"mlc":
461
+mlcprevmode==mode_in_linecomment?"comment":
462
+mlcprevmode==mode_in_directive?"directive":
463
+mlcprevmode==mode_in_string?"string":
464
+mlcprevmode==mode_in_directivestring?"directivestring":"unknown",
465
+mlcprevmode
466
+);
467
+}
426 468
 #endif
427 469
                         /* special case: keyword ends in a change of mode */
428 470
                         if(mode!=mode_whatever && hl->usedkeywordbuf>0) {
... ...
@@ -595,7 +637,7 @@ if(nline==1)
595 637
                         }
596 638
                 }
597 639
                 posoffset+=len;
598
-        } while(has_next!=0 && (pos+posoffset)<redataused);
640
+        } while(has_next!=0 && (realpos+posoffset)<redataused);
599 641
         /* special case: keyword ends at end-of-line */
600 642
         if(mode==mode_whatever && hl->usedkeywordbuf>0 && hl_searchlist(keywords,nkeywords,hl->keywordbuf,hl->usedkeywordbuf,NULL)==0) {
601 643
                 linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->keywordbufstart,color_keyword);
... ...
@@ -752,8 +794,8 @@ hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int poso
752 794
         if(opaque==NULL || *opaque<0 || hl==NULL || (posoff!=0 && linecolor==NULL) || color<0 || posoff<0)
753 795
                 return(NULL); /* sanity check failed */
754 796
 #if 0
755
-#define DEBUGLINE 12
756
-if(hl->usedlines==DEBUGLINE) {
797
+#define DEBUGLINE 775
798
+if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
757 799
 int i,k;
758 800
 int l=hl->lines[hl->usedlines].len;
759 801
 linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
... ...
@@ -797,7 +839,7 @@ hl->useddirectivebuf,
797 839
                 linecolor->color=color;
798 840
                 *opaque=1;
799 841
 #if 0
800
-if(hl->usedlines==DEBUGLINE) {
842
+if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
801 843
 int i,k;
802 844
 int l=hl->lines[hl->usedlines].len;
803 845
 linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
... ...
@@ -836,7 +878,7 @@ fprintf(stderr,"\"\n");
836 878
                 linecolor[-1].len+=added;
837 879
                 *opaque+=added;
838 880
 #if 0
839
-if(hl->usedlines==DEBUGLINE) {
881
+if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
840 882
 int i,k;
841 883
 int l=hl->lines[hl->usedlines].len;
842 884
 linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
... ...
@@ -857,7 +899,7 @@ fprintf(stderr,"\"\n");
857 899
         line->len+=sizeof(linecolor_t);
858 900
         hl->usedbuf+=sizeof(linecolor_t);
859 901
 #if 0
860
-if(hl->usedlines==DEBUGLINE) {
902
+if(hl->usedlines==DEBUGLINE || hl->usedlines==(DEBUGLINE+1)) {
861 903
 int i,k;
862 904
 int l=hl->lines[hl->usedlines].len;
863 905
 linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
Browse code

Add a simple C-language highlighter as a plugin (and lots of temp. debug code; to be cleaned later)

Dario Rodriguez authored on 17/10/2020 22:30:36
Showing 1 changed files
... ...
@@ -22,19 +22,26 @@
22 22
 #include "re_plugin_highlighter.h"
23 23
 
24 24
 #define PLUGINNAME "highlighter"
25
+#define HLINETBLOCK (16*1024)
26
+#define BUFBLOCK (64*1024)
25 27
 #define HIGHLIGHTERGROWSIZE (256*1024)
26 28
 #define DEFAULTCOLOR "\x80\x80\x80\xff"
27 29
 
28 30
 
29 31
 typedef enum colorsenum_t {
30 32
         color_normal=0,
31
-        color_define,
32
-        color_definestring,
33
+        color_directive,
34
+        color_directivekeyword,
35
+        color_directivestring,
36
+        color_directiveinclude,
37
+        color_directiveincludestring,
33 38
         color_keyword,
34 39
         color_string,
35 40
         color_multilinecomment,
36 41
         color_linecomment,
37 42
         color_number,
43
+        color_operator,
44
+        color_symbol,
38 45
         color_endenum,
39 46
 } colorsenum_t;
40 47
 
... ...
@@ -42,13 +49,16 @@ typedef enum colorsenum_t {
42 49
 static int redata_highlighter_add(redata_t *redata, redata_plugin_t *slot, undo_t *undo);
43 50
 static int redata_highlighter_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo);
44 51
 static int redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo, int is_unadd);
45
-static int redata_highlighter_commit(redata_t *redata, redata_plugin_t *slot,char *filename);
46 52
 static int redata_highlighter_postload(redata_t *redata, redata_plugin_t *slot,char *filename);
47 53
 
48 54
 static int hl_invalidatefrom(redata_t *redata, highlighter_t *hl, int nline);
49 55
 hcolor_t *hl_initcolors(int *ncolors, /* int color, char *colordef, */ ...);
50 56
 static highlighter_t *hl_getplugin(redata_t *redata);
51 57
 static int hl_doline(redata_t *redata, highlighter_t *hl, int nline);
58
+int hl_C_getkeywords(char ***keywords,int *nkeywords, int *maxlen);
59
+int hl_C_getdirectives(char ***directives,int *ndirectives, int *maxlen);
60
+int hl_searchlist(char **wordlist, int wordlistlen, char *word, int wordlen, int *res);
61
+linecolor_t *hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int posoff, int color);
52 62
 
53 63
 int
54 64
 redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
... ...
@@ -60,13 +70,18 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
60 70
                 return(-1);
61 71
         colors=hl_initcolors(&ncolors,
62 72
                              color_normal,"\x38\x17\x1e\xff",
63
-                             color_define,"\x0d\x2b\x04\xff",
64
-                             color_definestring,"\x07\x20\3b\xff",
73
+                             color_directive,"\x38\x4b\x00\xff",
74
+                             color_directivekeyword,"\x63\x7d\x16\xff",
75
+                             color_directivestring,"\x07\x20\3b\xff",
76
+                             color_directiveinclude,"\x16\x63\x7d\xff",
77
+                             color_directiveincludestring,"\x6\x13\x2d\xff",
65 78
                              color_keyword,"\x9d\x15\x00\xff",
66 79
                              color_string,"\x68\x00\x01\xff",
67 80
                              color_multilinecomment,"\x4f\x40\x57\xff",
68
-                             color_linecomment,"\xaa\x8c\xcd\xff",
81
+                             color_linecomment,"\xc6\x8c\xa4\xff",
69 82
                              color_number,"\x3b\x10\x35\xff",
83
+                             color_operator,"\xaa\x8c\xcd\xff",
84
+                             color_symbol,"\x69\x2a\x44\xff",
70 85
                              color_endenum,DEFAULTCOLOR,
71 86
                              -1);
72 87
         if(colors==NULL || (hl=malloc(sizeof(highlighter_t)))==NULL) {
... ...
@@ -84,7 +99,6 @@ redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
84 99
         slot->postload=redata_highlighter_postload;
85 100
         slot->add=redata_highlighter_add;
86 101
         slot->unadd=redata_highlighter_unadd;
87
-        slot->commit=redata_highlighter_commit;
88 102
         slot->userptr=hl;
89 103
         return(0);
90 104
 }
... ...
@@ -104,6 +118,12 @@ redata_highlighter_unregister(redata_t *redata, redata_plugin_t *slot, char *fil
104 118
         if(hl->lines!=NULL)
105 119
                 free(hl->lines),hl->lines=NULL;
106 120
         hl->sizelines=hl->usedlines=0;
121
+        if(hl->keywordbuf!=NULL)
122
+                free(hl->keywordbuf),hl->keywordbuf=NULL;
123
+        hl->sizekeywordbuf=hl->usedkeywordbuf=0;
124
+        if(hl->directivebuf!=NULL)
125
+                free(hl->directivebuf),hl->directivebuf=NULL;
126
+        hl->sizedirectivebuf=hl->useddirectivebuf=0;
107 127
         if(slot->userptr!=NULL)
108 128
                 free(slot->userptr),slot->userptr=NULL;
109 129
         return(0);
... ...
@@ -131,7 +151,7 @@ redata_highlighter_getline(redata_t *redata, int line, int *nlinecolors)
131 151
         if(line<0 || line>=hl->usedlines)
132 152
                 return(NULL);
133 153
         if(nlinecolors!=NULL)
134
-                *nlinecolors=(hl->lines[line].len)/sizeof(hcolor_t);
154
+                *nlinecolors=(hl->lines[line].len)/sizeof(linecolor_t);
135 155
         return((linecolor_t *) (hl->buf+hl->lines[line].off));
136 156
 }
137 157
 
... ...
@@ -158,6 +178,12 @@ redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t
158 178
         stack=redata_getstack(redata,undo);
159 179
         if(redata==NULL || slot==NULL || hl==NULL || undo==NULL || stack==NULL || slot->active==0)
160 180
                 return(-1); /* sanity check failed */
181
+        if(hl->usedlines==0) {
182
+#if 1
183
+fprintf(stderr,"INVALIDATING USEDLINELINES==0 (nothing to do)\n");
184
+#endif
185
+                return(0); /* nothing to do */
186
+        }
161 187
         /* get the first pos of the operation */
162 188
         pos=undo->posorig;
163 189
         if(undo->type=='D')
... ...
@@ -171,21 +197,15 @@ redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t
171 197
                         break;
172 198
                 }
173 199
         }
200
+        /* special case: check if pos is inside last line */
201
+        if(nline>=hl->usedlines && (hl->lines[hl->usedlines-1].pos+hl->lines[hl->usedlines-1].len)>=pos)
202
+                nline=hl->usedlines-1;
174 203
         /* invalidate from this line on */
204
+        nline=(nline<0)?0:nline;
175 205
         hl_invalidatefrom(redata,hl,nline);
176
-        return(0);
177
-}
178
-
179
-static int
180
-redata_highlighter_commit(redata_t *redata, redata_plugin_t *slot,char *filename)
181
-{
182
-        highlighter_t *hl=(highlighter_t *) ((slot!=NULL)?(slot->userptr):NULL);
183
-        if(redata==NULL || hl==NULL || filename==NULL)
184
-                return(-1); /* sanity check failed */
185
-        if(hl->flag_doneall)
186
-                return(0);
187
-        /* calc highlight of one additional line */
188
-        hl_doline(redata, hl, hl->usedlines);
206
+#if 1
207
+fprintf(stderr,"INVALIDATING LINE %i\n",nline);
208
+#endif
189 209
         return(0);
190 210
 }
191 211
 
... ...
@@ -274,12 +294,582 @@ hl_getplugin(redata_t *redata)
274 294
         return(hl);
275 295
 }
276 296
 
277
-
278 297
 static int
279 298
 hl_doline(redata_t *redata, highlighter_t *hl, int nline)
280 299
 {
300
+        int i;
301
+        long realpos;
302
+        long pos;
303
+        char *ptr;
304
+        int len;
305
+        hline_t *line;
306
+        long redataused;
307
+        int has_nl;
308
+        int has_next;
309
+        char **keywords;
310
+        int nkeywords;
311
+        char **directives;
312
+        int ndirectives;
313
+        int maxlenkeywords,maxlendirectives;
314
+        linecolor_t *linecolor;
315
+        int prev_char;
316
+        int prev_char_mask;
317
+        int cant_define;
318
+        int cant_directivekeyword;
319
+        int is_directiveinclude;
320
+        int opaque;
321
+        int posoffset;
322
+        enum {
323
+                mode_whatever=0,
324
+                mode_in_multilinecomment,
325
+                mode_in_linecomment,
326
+                mode_in_directive,
327
+                mode_in_string,
328
+                mode_in_directivestring,
329
+        } mode,mlcprevmode;
330
+        if(redata==NULL || hl==NULL || nline<0)
331
+                return(-1); /* sanoty check failed */
332
+        if(hl->usedlines>nline)
333
+                return(0); /* nothing to do */
334
+        if(hl_C_getkeywords(&keywords,&nkeywords,&maxlenkeywords)==-1)
335
+                return(-1); /* couldn't get keyword list */
336
+        if(hl_C_getdirectives(&directives,&ndirectives,&maxlendirectives)==-1)
337
+                return(-1); /* couldn't get directive list */
338
+        /* make sure keywordbuf is large enough */
339
+        if(maxlenkeywords>hl->sizekeywordbuf) {
340
+                char *newkeywordbuf;
341
+                int newsize;
342
+                newsize=maxlenkeywords;
343
+                if((newkeywordbuf=realloc(hl->keywordbuf,newsize))==NULL)
344
+                        return(-1); /* insufficient memory */
345
+                memset(newkeywordbuf,0,newsize);
346
+                hl->keywordbuf=newkeywordbuf;
347
+                hl->sizekeywordbuf=newsize;
348
+                hl->usedkeywordbuf=0;
349
+        }
350
+        /* make sure directivebuf is large enough */
351
+        if(maxlendirectives>hl->sizedirectivebuf) {
352
+                char *newdirectivebuf;
353
+                int newsize;
354
+                newsize=maxlendirectives;
355
+                if((newdirectivebuf=realloc(hl->directivebuf,newsize))==NULL)
356
+                        return(-1); /* insufficient memory */
357
+                memset(newdirectivebuf,0,newsize);
358
+                hl->directivebuf=newdirectivebuf;
359
+                hl->sizedirectivebuf=newsize;
360
+                hl->useddirectivebuf=0;
361
+        }
362
+        /* make sure we have enough hline_t structs */
363
+        if(hl->sizelines<=nline) {
364
+                hline_t *newlines;
365
+                int newsize=(nline+1+HLINETBLOCK)/HLINETBLOCK;
366
+                newsize*=HLINETBLOCK;
367
+                if((newlines=realloc(hl->lines,newsize*sizeof(hline_t)))==NULL)
368
+                        return(-1); /* insufficient memory */
369
+                hl->lines=newlines;
370
+                memset(hl->lines+hl->sizelines,0,(newsize-hl->sizelines)*sizeof(hline_t));
371
+                hl->sizelines=newsize;
372
+        }
373
+        /* make sure we have previous lines highlighted */
374
+        for(i=hl->usedlines;i<nline;i++) {
375
+#if 0
376
+fprintf(stderr,"Recursing from %i to %i\n",nline,i);
377
+#endif
378
+                if(hl_doline(redata,hl,i)==-1)
379
+                        return(-1); /* error highlighting line */
380
+        }
381
+        hl_invalidatefrom(redata,hl,nline);
382
+        line=hl->lines+nline;
383
+        if(redata_linecol2pos(redata,nline,0,&realpos,NULL)==-1)
384
+                return(-1); /* couldn't get line pos */
281 385
         /* NOTE: here comes the highlighter */
282
-#warning XXX TODO: implement this
283
-        return(-1);
386
+        line->pos=realpos;
387
+        line->off=(nline==0)?0:hl->lines[nline-1].off+hl->lines[nline-1].len;
388
+        line->len=0;
389
+        line->endingmode=mode_whatever;
390
+        redataused=redata_getused(redata);
391
+        mode=(nline>0)?line[-1].endingmode:mode_whatever;
392
+        cant_define=0;
393
+        cant_directivekeyword=0;
394
+        is_directiveinclude=0;
395
+        opaque=0;
396
+        linecolor=hl_addtolinecolor(&opaque,hl,NULL,color_normal,0);
397
+        posoffset=0;
398
+#if 0
399
+fprintf(stderr,"Doing line %i pos:%li\n",nline,line->pos);
400
+#endif
401
+        prev_char='\0';
402
+        prev_char_mask=0xff;
403
+        hl->usedkeywordbuf=0;
404
+        hl->useddirectivebuf=0;
405
+        mlcprevmode=mode_whatever;
406
+        do {
407
+                if(redata_line_rawinfo(redata,realpos+posoffset,&pos,&ptr,&len,NULL)==-1)
408
+                        return(-1); /* couldn't get line data */
409
+                has_nl=((len>0 && ptr[len-1]=='\n')?1:0);
410
+                has_next=(len==0)?1:(ptr[len-1]=='\n')?0:1;
411
+                /* special case: line with only a newline */
412
+                if(posoffset==0 && has_nl==1 && len==1) {
413
+                        /* delete the existing linecolor and break */
414
+                        line->len=0;
415
+                        break;
416
+                }
417
+                /* iterate */
418
+#if 0
419
+fprintf(stderr,"Line %i chunk at posoffset %i\n",nline, posoffset);
420
+#endif
421
+#warning FALTA PONER DE UN COLOR DIFERENTE lo de <nombre.h> de los #include (pero no de los #define!)
422
+                for(i=0;i<(len-has_nl);prev_char=(ptr[i]&prev_char_mask),prev_char_mask=0xff,i++) {
423
+#if 0
424
+if(nline==1)
425
+        fprintf(stderr,"ptr[%i]='%c' prev_char='%s%c'\n",i,ptr[i],(prev_char=='\0')?"\\":"",(prev_char=='\0')?'0':prev_char);
426
+#endif
427
+                        /* special case: keyword ends in a change of mode */
428
+                        if(mode!=mode_whatever && hl->usedkeywordbuf>0) {
429
+                                if(hl_searchlist(keywords,nkeywords,hl->keywordbuf,hl->usedkeywordbuf,NULL)==0) {
430
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->keywordbufstart,color_keyword);
431
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->keywordbufstart+hl->usedkeywordbuf-1,color_keyword);
432
+                                }
433
+                                hl->usedkeywordbuf=0;
434
+                        }
435
+                        /* end of special case */
436
+                        if(mode==mode_whatever) {
437
+                                if(prev_char=='\\' || ptr[i]=='\\') {
438
+                                        /* escape char mark or escaped char */
439
+                                        continue;
440
+                                }
441
+                                if(prev_char=='/' && ptr[i]=='/') {
442
+                                        mode=mode_in_linecomment;
443
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i-1,color_linecomment);
444
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_linecomment);
445
+                                        continue;
446
+                                }
447
+                                if(prev_char=='/' && ptr[i]=='*') {
448
+                                        mlcprevmode=mode;
449
+                                        mode=mode_in_multilinecomment;
450
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i-1,color_multilinecomment);
451
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_multilinecomment);
452
+                                        continue;
453
+                                }
454
+                                if(ptr[i]=='\"') {
455
+                                        mode=mode_in_string;
456
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_string);
457
+                                        continue;
458
+                                }
459
+                                if(!cant_define && ptr[i]=='#') {
460
+                                        mode=mode_in_directive;
461
+                                        is_directiveinclude=0;
462
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_directive);
463
+                                        continue;
464
+                                }
465
+                                /* keyword detection */
466
+                                if(hl->usedkeywordbuf==0 && ((ptr[i]>='a' && ptr[i]<='z') || (ptr[i]>='A' && ptr[i]<='Z') || ptr[i]=='_')) {
467
+                                        cant_define=1;
468
+                                        hl->keywordbuf[hl->usedkeywordbuf++]=ptr[i];
469
+                                        hl->keywordbufstart=posoffset+i;
470
+                                } else if(hl->usedkeywordbuf>0 && ((ptr[i]>='0' && ptr[i]<='9') || (ptr[i]>='a' && ptr[i]<='z') || (ptr[i]>='A' && ptr[i]<='Z') || ptr[i]=='_')) {
471
+                                        if(hl->usedkeywordbuf<hl->sizekeywordbuf) {
472
+                                                hl->keywordbuf[hl->usedkeywordbuf++]=ptr[i];
473
+                                        } else {
474
+                                                hl->keywordbuf[0]='\0'; /* too long */
475
+                                        }
476
+                                }
477
+                                if(hl->usedkeywordbuf>0 && !((ptr[i]>='0' && ptr[i]<='9') || (ptr[i]>='a' && ptr[i]<='z') || (ptr[i]>='A' && ptr[i]<='Z') || ptr[i]=='_')) {
478
+                                        if(hl_searchlist(keywords,nkeywords,hl->keywordbuf,hl->usedkeywordbuf,NULL)==0) {
479
+                                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->keywordbufstart,color_keyword);
480
+                                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->keywordbufstart+hl->usedkeywordbuf-1,color_keyword);
481
+                                        }
482
+                                        hl->usedkeywordbuf=0;
483
+
484
+                                }
485
+                                /* keyword detection */
486
+                                if(strchr("0123456789",ptr[i])!=NULL) {
487
+                                        cant_define=1;
488
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_number);
489
+                                        continue;
490
+                                }
491
+                                if(strchr("<>=+-%*/!|&^",ptr[i])!=NULL) {
492
+                                        cant_define=1;
493
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_operator);
494
+                                        continue;
495
+                                }
496
+                                if(strchr("(){}[],;",ptr[i])!=NULL) {
497
+                                        cant_define=1;
498
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_symbol);
499
+                                        continue;
500
+                                }
501
+                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_normal);
502
+                                continue;
503
+                        }
504
+                        if(mode==mode_in_multilinecomment) {
505
+                                if(prev_char=='\\' || ptr[i]=='\\') {
506
+                                        continue;
507
+                                }
508
+                                if(prev_char=='*' && ptr[i]=='/') {
509
+                                        prev_char_mask=0x00;
510
+                                        mode=mlcprevmode;
511
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_multilinecomment);
512
+                                        continue;
513
+                                }
514
+                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_multilinecomment);
515
+                                continue;
516
+                        }
517
+                        if(mode==mode_in_directive) {
518
+                                if(prev_char=='\\' || ptr[i]=='\\') {
519
+                                        continue;
520
+                                }
521
+                                if(prev_char=='/' && ptr[i]=='/') {
522
+                                        mode=mode_in_linecomment;
523
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i-1,color_linecomment);
524
+                                         linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_linecomment);
525
+                                        continue;
526
+                                }
527
+                                if(prev_char=='/' && ptr[i]=='*') {
528
+                                        mlcprevmode=mode;
529
+                                        mode=mode_in_multilinecomment;
530
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i-1,color_multilinecomment);
531
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_multilinecomment);
532
+                                        continue;
533
+                                }
534
+                                /* directive detection */
535
+                                if(!cant_directivekeyword && hl->useddirectivebuf==0 && ((ptr[i]>='a' && ptr[i]<='z') || (ptr[i]>='A' && ptr[i]<='Z') || ptr[i]=='_')) {
536
+                                        hl->directivebuf[hl->useddirectivebuf++]=ptr[i];
537
+                                        hl->directivebufstart=posoffset+i;
538
+                                } else if(hl->useddirectivebuf>0 && ((ptr[i]>='0' && ptr[i]<='9') || (ptr[i]>='a' && ptr[i]<='z') || (ptr[i]>='A' && ptr[i]<='Z') || ptr[i]=='_')) {
539
+                                        if(hl->useddirectivebuf<hl->sizedirectivebuf) {
540
+                                                hl->directivebuf[hl->useddirectivebuf++]=ptr[i];
541
+                                        } else {
542
+                                                hl->directivebuf[0]='\0'; /* too long */
543
+                                                cant_directivekeyword=1;
544
+                                        }
545
+                                }
546
+                                if(hl->useddirectivebuf>0 && !((ptr[i]>='0' && ptr[i]<='9') || (ptr[i]>='a' && ptr[i]<='z') || (ptr[i]>='A' && ptr[i]<='Z') || ptr[i]=='_')) {
547
+                                        int nfound;
548
+                                        if(hl_searchlist(directives,ndirectives,hl->directivebuf,hl->useddirectivebuf,&nfound)==0) {
549
+                                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->directivebufstart,color_directivekeyword);
550
+                                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->directivebufstart+hl->useddirectivebuf-1,color_directivekeyword);
551
+                                                if(strcmp(directives[nfound],"include")==0)
552
+                                                        is_directiveinclude=1;
553
+                                        }
554
+                                        hl->useddirectivebuf=0;
555
+                                        cant_directivekeyword=1;
556
+                                }
557
+                                if(ptr[i]!=' ' && ptr[i]!='\t')
558
+                                        cant_directivekeyword=1;
559
+                                /* directive detection */
560
+                                if(ptr[i]=='\"') {
561
+                                        mode=mode_in_directivestring;
562
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,(is_directiveinclude==0)?color_directivestring:color_directiveincludestring);
563
+                                        continue;
564
+                                }
565
+                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,(is_directiveinclude==0)?color_directive:color_directiveinclude);
566
+                                continue;
567
+                        }
568
+                        if(mode==mode_in_directivestring) {
569
+                                if(prev_char=='\\' || ptr[i]=='\\') {
570
+                                        continue;
571
+                                }
572
+                                if(ptr[i]=='\"') {
573
+                                        mode=mode_in_directive;
574
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,(is_directiveinclude==0)?color_directivestring:color_directiveincludestring);
575
+                                        continue;
576
+                                }
577
+                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,(is_directiveinclude==0)?color_directivestring:color_directiveincludestring);
578
+                                continue;
579
+                        }
580
+                        if(mode==mode_in_string) {
581
+                                if(prev_char=='\\' || ptr[i]=='\\') {
582
+                                        continue;
583
+                                }
584
+                                if(ptr[i]=='\"') {
585
+                                        mode=mode_whatever;
586
+                                        linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_string);
587
+                                        continue;
588
+                                }
589
+                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_string);
590
+                                continue;
591
+                        }
592
+                        if(mode==mode_in_linecomment) {
593
+                                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,posoffset+i,color_linecomment);
594
+                                continue;
595
+                        }
596
+                }
597
+                posoffset+=len;
598
+        } while(has_next!=0 && (pos+posoffset)<redataused);
599
+        /* special case: keyword ends at end-of-line */
600
+        if(mode==mode_whatever && hl->usedkeywordbuf>0 && hl_searchlist(keywords,nkeywords,hl->keywordbuf,hl->usedkeywordbuf,NULL)==0) {
601
+                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->keywordbufstart,color_keyword);
602
+                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->keywordbufstart+hl->usedkeywordbuf-1,color_keyword);
603
+        }
604
+        /* end of special case */
605
+        /* special case: deirectivekeyword ends at end-of-line */
606
+        if(mode==mode_in_directive && hl->useddirectivebuf>0 && hl_searchlist(directives,ndirectives,hl->directivebuf,hl->useddirectivebuf,NULL)==0) {
607
+                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->directivebufstart,color_directivekeyword);
608
+                linecolor=hl_addtolinecolor(&opaque,hl,linecolor,hl->directivebufstart+hl->useddirectivebuf-1,color_directivekeyword);
609
+        }
610
+        /* end of special case */
611
+        if(linecolor==NULL)
612
+                return(-1);
613
+        if((prev_char=='\\' && mode!=mode_in_linecomment) || mode==mode_in_multilinecomment)
614
+                line->endingmode=mode;
615
+        hl->usedlines++;
616
+        return(0);
617
+}
618
+
619
+
620
+int
621
+hl_C_getkeywords(char ***keywords,int *nkeywords, int *maxlen)
622
+{
623
+        static int init=0;
624
+        static int staticnkeywords=0;
625
+        static int staticmaxlen=0;
626
+        static const char *C_keywords[]={
627
+"auto",
628
+"break",
629
+"case",
630
+"char",
631
+"const",
632
+"continue",
633
+"default",
634
+"do",
635
+"double",
636
+"else",
637
+"enum",
638
+"extern",
639
+"float",
640
+"for",
641
+"goto",
642
+"if",
643
+"inline",
644
+"int",
645
+"long",
646
+"register",
647
+"restrict",
648
+"return",
649
+"short",
650
+"signed",
651
+"sizeof",
652
+"static",
653
+"struct",
654
+"switch",
655
+"typedef",
656
+"union",
657
+"unsigned",
658
+"void",
659
+"volatile",
660
+"while",
661
+"_Alignas",
662
+"_Alignof",
663
+"_Atomic",
664
+"_Bool",
665
+"_Complex",
666
+"_Generic",
667
+"_Imaginary",
668
+"_Noreturn",
669
+"_Static_assert",
670
+"_Thread_local",
671
+NULL
672
+};
673
+        if(keywords==NULL || nkeywords==NULL)
674
+                return(-1);
675
+        if(init==0) {
676
+                int k,l,maxl;
677
+                for(k=0,maxl=0;C_keywords[k]!=NULL;k++) {
678
+                        l=strlen(C_keywords[k]);
679
+                        maxl=(l>maxl)?l:maxl;
680
+                }
681
+                staticnkeywords=k;
682
+                staticmaxlen=maxl;
683
+                init=1;
684
+        }
685
+        *keywords=(char **)C_keywords;
686
+        *nkeywords=staticnkeywords;
687
+        if(maxlen!=NULL)
688
+                *maxlen=staticmaxlen;
689
+        return(0);
690
+}
691
+
692
+int
693
+hl_C_getdirectives(char ***directives,int *ndirectives, int *maxlen)
694
+{
695
+        static int init=0;
696
+        static int staticndirectives=0;
697
+        static int staticmaxlen=0;
698
+        static const char *C_directives[]={
699
+"define",
700
+"undef",
701
+"include",
702
+"if",
703
+"ifdef",
704
+"ifndef",
705
+"else",
706
+"elif",
707
+"endif",
708
+"line",
709
+"error",
710
+"pragma",
711
+};
712
+        if(directives==NULL || ndirectives==NULL)
713
+                return(-1);
714
+        if(init==0) {
715
+                int k,l,maxl;
716
+                for(k=0,maxl=0;C_directives[k]!=NULL;k++) {
717
+                        l=strlen(C_directives[k]);
718
+                        maxl=(l>maxl)?l:maxl;
719
+                }
720
+                staticndirectives=k;
721
+                staticmaxlen=maxl;
722
+                init=1;
723
+        }
724
+        *directives=(char **)C_directives;
725
+        *ndirectives=staticndirectives;
726
+        if(maxlen!=NULL)
727
+                *maxlen=staticmaxlen;
728
+        return(0);
729
+}
730
+
731
+int
732
+hl_searchlist(char **wordlist, int wordlistlen, char *word, int wordlen, int *res)
733
+{
734
+        int k;
735
+        if(wordlist==NULL || wordlistlen<0 || word==NULL || wordlen<0)
736
+                return(-1); /* sanity check failed */
737
+        for(k=0;k<wordlistlen;k++) {
738
+                if(memcmp(wordlist[k],word,wordlen)==0 && (wordlist[k])[wordlen]=='\0') {
739
+                        if(res!=NULL)
740
+                                *res=k;
741
+                        return(0); /* word found */
742
+                }
743
+        }
744
+        return(-1); /* word not found */
745
+}
746
+
747
+
748
+linecolor_t *
749
+hl_addtolinecolor(int *opaque, highlighter_t *hl,linecolor_t *linecolor,int posoff,int color)
750
+{
751
+        hline_t *line;
752
+        if(opaque==NULL || *opaque<0 || hl==NULL || (posoff!=0 && linecolor==NULL) || color<0 || posoff<0)
753
+                return(NULL); /* sanity check failed */
754
+#if 0
755
+#define DEBUGLINE 12
756
+if(hl->usedlines==DEBUGLINE) {
757
+int i,k;
758
+int l=hl->lines[hl->usedlines].len;
759
+linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
760
+fprintf(stderr,"addtolinecolor pre.: nline:%i *opaque:%i posoff:%i color:%i current:\"",hl->usedlines,*opaque,posoff,color);
761
+for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
762
+        for(i=0;i<lc->len;i++)
763
+                fprintf(stderr,"%x",lc->color);
764
+}
765
+hl->keywordbuf[hl->usedkeywordbuf]='\0';
766
+hl->directivebuf[hl->useddirectivebuf]='\0';
767
+fprintf(stderr,"\" (%i%s%s%s,%i%s%s%s)\n",
768
+hl->usedkeywordbuf,
769
+(hl->usedkeywordbuf==0)?"":" \"",
770
+(hl->usedkeywordbuf==0)?"":hl->keywordbuf,
771
+(hl->usedkeywordbuf==0)?"":"\"",
772
+hl->useddirectivebuf,
773
+(hl->useddirectivebuf==0)?"":" \"",
774
+(hl->useddirectivebuf==0)?"":hl->directivebuf,
775
+(hl->useddirectivebuf==0)?"":"\""
776
+);
777
+}
778
+#endif
779
+        /* make sure there is a space for a linecolor_t in buf */
780
+        if((hl->usedbuf+sizeof(linecolor_t))>=hl->sizebuf) {
781
+                char *newbuf;
782
+                int newsize=(hl->sizebuf+sizeof(linecolor_t)+BUFBLOCK-1)/BUFBLOCK;
783
+                newsize*=BUFBLOCK;
784
+                if((newbuf=realloc(hl->buf,newsize))==NULL)
785
+                        return(NULL); /* insufficient memory */
786
+                hl->buf=newbuf;
787
+                memset(hl->buf+hl->sizebuf,0,(newsize-hl->sizebuf));
788
+                hl->sizebuf=newsize;
789
+        }
790
+        line=hl->lines+hl->usedlines;
791
+        /* posoff==0 means "do init" */
792
+        if(posoff==0) {
793
+                line->off=(hl->usedlines==0)?0:hl->lines[hl->usedlines-1].off+hl->lines[hl->usedlines-1].len;
794
+                line->len=sizeof(linecolor_t);
795
+                linecolor=(linecolor_t *) (hl->buf+line->off);
796
+                linecolor->len=1;
797
+                linecolor->color=color;
798
+                *opaque=1;
799
+#if 0
800
+if(hl->usedlines==DEBUGLINE) {
801
+int i,k;
802
+int l=hl->lines[hl->usedlines].len;
803
+linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
804
+fprintf(stderr,"addtolinecolor post: nline:%i *opaque:%i posoff:%i color:%i         \"",hl->usedlines,*opaque,posoff,color);
805
+for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
806
+        for(i=0;i<lc->len;i++)
807
+                fprintf(stderr,"%x",lc->color);
808
+}
809
+fprintf(stderr,"\"\n");
810
+}
811
+#endif
812
+                return(linecolor);
813
+        }
814
+        /* if posoff was already done, truncate */
815
+        if(posoff<*opaque) {
816
+                int delta,maxdelta;
817
+                int l;
818
+                maxdelta=line->len;
819
+                line->len=0;
820
+                for(delta=0,l=0;delta<maxdelta;l+=linecolor->len,delta+=sizeof(linecolor_t)) {
821
+                        linecolor=(linecolor_t *) (hl->buf+line->off+delta);
822
+                        line->len=delta;
823
+                        if((l+linecolor->len)>=posoff) {
824
+                                line->len+=sizeof(linecolor_t);
825
+                                linecolor->len=posoff-l;
826
+                                *opaque=posoff;
827
+                                break;
828
+                        }
829
+                }
830
+                hl->usedbuf=line->off+line->len;
831
+        }
832
+        linecolor=(linecolor_t *) (hl->buf+line->off+line->len);
833
+        /* if we have not changed color, expand */
834
+        if(linecolor[-1].color==color) {
835
+                int added=(posoff-*opaque)+1;
836
+                linecolor[-1].len+=added;
837
+                *opaque+=added;
838
+#if 0
839
+if(hl->usedlines==DEBUGLINE) {
840
+int i,k;
841
+int l=hl->lines[hl->usedlines].len;
842
+linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
843
+fprintf(stderr,"addtolinecolor post: nline:%i *opaque:%i posoff:%i color:%i         \"",hl->usedlines,*opaque,posoff,color);
844
+for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
845
+        for(i=0;i<lc->len;i++)
846
+                fprintf(stderr,"%x",lc->color);
847
+}
848
+fprintf(stderr,"\"\n");
284 849
 }
850
+#endif
851
+                return(linecolor-1);
852
+        }
853
+        /* add new linecolor */
854
+        linecolor->len=1;
855
+        linecolor->color=color;
856
+        *opaque+=1;
857
+        line->len+=sizeof(linecolor_t);
858
+        hl->usedbuf+=sizeof(linecolor_t);
859
+#if 0
860
+if(hl->usedlines==DEBUGLINE) {
861
+int i,k;
862
+int l=hl->lines[hl->usedlines].len;
863
+linecolor_t *lc=(linecolor_t *) (hl->buf+hl->lines[hl->usedlines].off);
864
+fprintf(stderr,"addtolinecolor post: nline:%i *opaque:%i posoff:%i color:%i         \"",hl->usedlines,*opaque,posoff,color);
865
+for(k=0;k<l;k+=sizeof(linecolor_t),lc++) {
866
+        for(i=0;i<lc->len;i++)
867
+                fprintf(stderr,"%x",lc->color);
868
+}
869
+fprintf(stderr,"\"\n");
870
+}
871
+#endif
872
+        return(linecolor);
873
+}
874
+
285 875
 
Browse code

Add highlighter plugin structs and registration

Dario Rodriguez authored on 12/10/2020 21:55:42
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,285 @@
1
+/*
2
+ * re_plugin_highlighter.c
3
+ *
4
+ * A programmers editor
5
+ *
6
+ * re_data plugin to support the syntax highlighter.
7
+ *
8
+ * Author: Dario Rodriguez dario@softhome.net
9
+ * This program is licensed under the terms of GNU GPL v2.1+
10
+ */
11
+
12
+#include <stdio.h>
13
+#include <stdlib.h>
14
+#include <unistd.h>
15
+#include <string.h>
16
+#include <fcntl.h>
17
+#include <stdarg.h>
18
+#include <sys/types.h>
19
+#include <sys/stat.h>
20
+
21
+
22
+#include "re_plugin_highlighter.h"
23
+
24
+#define PLUGINNAME "highlighter"
25
+#define HIGHLIGHTERGROWSIZE (256*1024)
26
+#define DEFAULTCOLOR "\x80\x80\x80\xff"
27
+
28
+
29
+typedef enum colorsenum_t {
30
+        color_normal=0,
31
+        color_define,
32
+        color_definestring,
33
+        color_keyword,
34
+        color_string,
35
+        color_multilinecomment,
36
+        color_linecomment,
37
+        color_number,
38
+        color_endenum,
39
+} colorsenum_t;
40
+
41
+
42
+static int redata_highlighter_add(redata_t *redata, redata_plugin_t *slot, undo_t *undo);
43
+static int redata_highlighter_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo);
44
+static int redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo, int is_unadd);
45
+static int redata_highlighter_commit(redata_t *redata, redata_plugin_t *slot,char *filename);
46
+static int redata_highlighter_postload(redata_t *redata, redata_plugin_t *slot,char *filename);
47
+
48
+static int hl_invalidatefrom(redata_t *redata, highlighter_t *hl, int nline);
49
+hcolor_t *hl_initcolors(int *ncolors, /* int color, char *colordef, */ ...);
50
+static highlighter_t *hl_getplugin(redata_t *redata);
51
+static int hl_doline(redata_t *redata, highlighter_t *hl, int nline);
52
+
53
+int
54
+redata_highlighter_register(redata_t *redata, redata_plugin_t *slot)
55
+{
56
+        hcolor_t *colors;
57
+        int ncolors;
58
+        highlighter_t *hl;
59
+        if(redata==NULL || slot==NULL)
60
+                return(-1);
61
+        colors=hl_initcolors(&ncolors,
62
+                             color_normal,"\x38\x17\x1e\xff",
63
+                             color_define,"\x0d\x2b\x04\xff",
64
+                             color_definestring,"\x07\x20\3b\xff",
65
+                             color_keyword,"\x9d\x15\x00\xff",
66
+                             color_string,"\x68\x00\x01\xff",
67
+                             color_multilinecomment,"\x4f\x40\x57\xff",
68
+                             color_linecomment,"\xaa\x8c\xcd\xff",
69
+                             color_number,"\x3b\x10\x35\xff",
70
+                             color_endenum,DEFAULTCOLOR,
71
+                             -1);
72
+        if(colors==NULL || (hl=malloc(sizeof(highlighter_t)))==NULL) {
73
+                if(colors!=NULL)
74
+                        free(colors),colors=NULL;
75
+                return(-1); /* insufficient memory */
76
+        }
77
+        memset(hl,0,sizeof(highlighter_t));
78
+        hl->sizebuf=hl->usedbuf=0;
79
+        hl->sizecolors=ncolors;
80
+        hl->colors=colors;
81
+        strncpy(slot->name,PLUGINNAME,sizeof(slot->name));
82
+        slot->name[sizeof(slot->name)-1]='\0';
83
+        slot->unregister=redata_highlighter_unregister;
84
+        slot->postload=redata_highlighter_postload;
85
+        slot->add=redata_highlighter_add;
86
+        slot->unadd=redata_highlighter_unadd;
87
+        slot->commit=redata_highlighter_commit;
88
+        slot->userptr=hl;
89
+        return(0);
90
+}
91
+
92
+int
93
+redata_highlighter_unregister(redata_t *redata, redata_plugin_t *slot, char *filename)
94
+{
95
+        highlighter_t *hl=(highlighter_t *) ((slot!=NULL)?(slot->userptr):NULL);
96
+        if(redata==NULL || slot==NULL || hl==NULL)
97
+                return(-1);
98
+        if(hl->buf!=NULL)
99
+                free(hl->buf),hl->buf=NULL;
100
+        hl->sizebuf=hl->usedbuf=0;
101
+        if(hl->colors!=NULL)
102
+                free(hl->colors),hl->colors=NULL;
103
+        hl->sizecolors=0;
104
+        if(hl->lines!=NULL)
105
+                free(hl->lines),hl->lines=NULL;
106
+        hl->sizelines=hl->usedlines=0;
107
+        if(slot->userptr!=NULL)
108
+                free(slot->userptr),slot->userptr=NULL;
109
+        return(0);
110
+}
111
+
112
+hcolor_t *
113
+redata_highlighter_getcolors(redata_t *redata, int *ncolors)
114
+{
115
+        highlighter_t *hl;
116
+        if(redata==NULL || (hl=hl_getplugin(redata))==NULL)
117
+                return(NULL); /* sanity check failed or plugin not found */
118
+        if(ncolors!=NULL)
119
+                *ncolors=hl->sizecolors;
120
+        return(hl->colors);
121
+}
122
+
123
+linecolor_t *
124
+redata_highlighter_getline(redata_t *redata, int line, int *nlinecolors)
125
+{
126
+        highlighter_t *hl;
127
+        if(redata==NULL || line<0 || (hl=hl_getplugin(redata))==NULL)
128
+                return(NULL); /* sanity check failed or plugin not found */
129
+        if(!hl->flag_doneall)
130
+                hl_doline(redata,hl,line);
131
+        if(line<0 || line>=hl->usedlines)
132
+                return(NULL);
133
+        if(nlinecolors!=NULL)
134
+                *nlinecolors=(hl->lines[line].len)/sizeof(hcolor_t);
135
+        return((linecolor_t *) (hl->buf+hl->lines[line].off));
136
+}
137
+
138
+static int
139
+redata_highlighter_add(redata_t *redata, redata_plugin_t *slot, undo_t *undo)
140
+{
141
+        return(redata_highlighter_add_or_unadd(redata, slot, undo, 0));
142
+}
143
+
144
+static int
145
+redata_highlighter_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo)
146
+{
147
+        return(redata_highlighter_add_or_unadd(redata, slot, undo, 1));
148
+}
149
+
150
+
151
+static int
152
+redata_highlighter_add_or_unadd(redata_t *redata, redata_plugin_t *slot, undo_t *undo, int is_unadd)
153
+{
154
+        long pos;
155
+        int nline;
156
+        undostack_t *stack;
157
+        highlighter_t *hl=(highlighter_t *) ((slot!=NULL)?(slot->userptr):NULL);
158
+        stack=redata_getstack(redata,undo);
159
+        if(redata==NULL || slot==NULL || hl==NULL || undo==NULL || stack==NULL || slot->active==0)
160
+                return(-1); /* sanity check failed */
161
+        /* get the first pos of the operation */
162
+        pos=undo->posorig;
163
+        if(undo->type=='D')
164
+                pos-=undo->len;
165
+        if(undo->type=='M' && undo->posdest<pos)
166
+                pos=undo->posdest;
167
+        /* get line of pos */
168
+        for(nline=0;nline<hl->usedlines;nline++) {
169
+                if(hl->lines[nline].pos>pos) {
170
+                        nline--;
171
+                        break;
172
+                }
173
+        }
174
+        /* invalidate from this line on */
175
+        hl_invalidatefrom(redata,hl,nline);
176
+        return(0);
177
+}
178
+
179
+static int
180
+redata_highlighter_commit(redata_t *redata, redata_plugin_t *slot,char *filename)
181
+{
182
+        highlighter_t *hl=(highlighter_t *) ((slot!=NULL)?(slot->userptr):NULL);
183
+        if(redata==NULL || hl==NULL || filename==NULL)
184
+                return(-1); /* sanity check failed */
185
+        if(hl->flag_doneall)
186
+                return(0);
187
+        /* calc highlight of one additional line */
188
+        hl_doline(redata, hl, hl->usedlines);
189
+        return(0);
190
+}
191
+
192
+static int
193
+redata_highlighter_postload(redata_t *redata, redata_plugin_t *slot,char *filename)
194
+{
195
+#warning XXX TODO: get the language from the filename
196
+        return(-1);
197
+}
198
+
199
+
200
+static int
201
+hl_invalidatefrom(redata_t *redata, highlighter_t *hl, int nline)
202
+{
203
+        if(redata==NULL || hl==NULL || nline<0)
204
+                return(-1); /* sanity check failed */
205
+        if(nline>=hl->usedlines)
206
+                return(0); /* nothing to do */
207
+        if(nline==0) {
208
+                hl->usedbuf=0;
209
+                hl->usedlines=0;
210
+        }
211
+        hl->usedbuf=hl->lines[nline].off;
212
+        hl->usedlines=nline;
213
+        hl->flag_doneall=0;
214
+        return(0);
215
+}
216
+
217
+hcolor_t *
218
+hl_initcolors(int *ncolors, /* int color, char *colordef, */ ...)
219
+{
220
+        int maxcolor;
221
+        int color;
222
+        char *colordef;
223
+        int round;
224
+        hcolor_t *hcolors;
225
+        va_list paramlist;
226
+        int i;
227
+        static char defcolor[]={DEFAULTCOLOR};
228
+        if(ncolors==NULL)
229
+                return(NULL); /* sanity check failed */
230
+        *ncolors=0;
231
+        hcolors=NULL;
232
+        for(round=0;round<2;round++) {
233
+                maxcolor=-1;
234
+                va_start(paramlist,ncolors);
235
+                while((color=va_arg(paramlist,int))>=0) {
236
+                        colordef=va_arg(paramlist,char *);
237
+                        maxcolor=(maxcolor<color)?color:maxcolor;
238
+                        if(round==1) {
239
+                                strncpy(hcolors[color].rgba,colordef,sizeof(hcolors[color].rgba));
240
+                                hcolors[color].rgba[sizeof(hcolors[color].rgba)-1]='\0';
241
+                        }
242
+                }
243
+                va_end(paramlist);
244
+                if(maxcolor<0)
245
+                        return(NULL); /* no colors were defined */
246
+                if(round==0) {
247
+                        if((hcolors=malloc(sizeof(hcolor_t)*(maxcolor+1)))==NULL)
248
+                                return(NULL); /* insufficient memory */
249
+                        memset(hcolors,0,sizeof(hcolor_t)*(maxcolor+1));
250
+                        for(i=0;i<=maxcolor;i++)
251
+                                memcpy(hcolors[i].rgba,defcolor,sizeof(defcolor));
252
+                }
253
+        }
254
+        *ncolors=maxcolor+1;
255
+        return(hcolors);
256
+}
257
+
258
+static highlighter_t *
259
+hl_getplugin(redata_t *redata)
260
+{
261
+        highlighter_t *hl;
262
+        int i;
263
+        if(redata==NULL)
264
+                return(NULL); /* sanity check failed */
265
+        for(i=0;i<redata->sizeplugins;i++) {
266
+                if(strcmp(redata->plugins[i].name,PLUGINNAME)==0)
267
+                        break;
268
+        }
269
+        if(i>=redata->sizeplugins || redata->plugins[i].active==0)
270
+                return(NULL); /* plugin not found or nor active */
271
+        hl=(highlighter_t *) (redata->plugins[i].userptr);
272
+        if(hl==NULL)
273
+                return(NULL); /* internal error */
274
+        return(hl);
275
+}
276
+
277
+
278
+static int
279
+hl_doline(redata_t *redata, highlighter_t *hl, int nline)
280
+{
281
+        /* NOTE: here comes the highlighter */
282
+#warning XXX TODO: implement this
283
+        return(-1);
284
+}
285
+