Browse code

Add highlighter plugin structs and registration

Dario Rodriguez authored on 12/10/2020 21:55:42
Showing 4 changed files
... ...
@@ -10,6 +10,8 @@ re_data.o: re_data.c re_data.h
10 10
 
11 11
 re_plugin_unsaved.o: re_plugin_unsaved.c re_plugin_unsaved.h re_data.h
12 12
 
13
+re_plugin_highlighter.o: re_plugin_highlighter.c re_plugin_highlighter.h re_data.h
14
+
13 15
 re_ui.o: re_ui.c re_ui.h hack_regular.h
14 16
 
15 17
 re_tests.o: re_tests.c re_data.h
... ...
@@ -31,8 +33,8 @@ hack_regular.o: hack_regular.c
31 33
 socklib.o: ext/socklib.c ext/socklib.h
32 34
 	$(CC) $(CFLAGS) -Iext -c -o socklib.o ext/socklib.c
33 35
 
34
-re: recenteditor.o re_data.o re_plugin_unsaved.o sha3.o re_ui.o hack_regular.o socklib.o
35
-	$(CC) $(LDFLAGS) -o re recenteditor.o re_data.o re_plugin_unsaved.o sha3.o re_ui.o hack_regular.o socklib.o
36
+re: recenteditor.o re_data.o re_plugin_unsaved.o re_plugin_highlighter.o sha3.o re_ui.o hack_regular.o socklib.o
37
+	$(CC) $(LDFLAGS) -o re recenteditor.o re_data.o re_plugin_unsaved.o re_plugin_highlighter.o sha3.o re_ui.o hack_regular.o socklib.o
36 38
 
37 39
 tests: re_tests.o re_data.o sha3.o
38 40
 	$(CC) $(LDFLAGS) -o tests re_tests.o re_data.o sha3.o
... ...
@@ -41,4 +43,4 @@ tests_utf8: re_tests_utf8.o re_data.o sha3.o
41 43
 	$(CC) $(LDFLAGS) -o tests_utf8 re_tests_utf8.o re_data.o sha3.o
42 44
 
43 45
 clean:
44
-	rm -f recenteditor.o re_data.o re_tests.o re_plugin_unsaved.o sha3.o re_ui.o hack_regular.c hack_regular.h hack_regular.o re tests 
46
+	rm -f recenteditor.o re_data.o re_tests.o re_plugin_unsaved.o re_plugin_highlighter.o sha3.o re_ui.o hack_regular.c hack_regular.h hack_regular.o re tests 
45 47
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
+
0 286
new file mode 100644
... ...
@@ -0,0 +1,48 @@
1
+/*
2
+ * re_plugin_highlighter.h
3
+ *
4
+ * A programmers editor
5
+ *
6
+ * re_data plugin to support the syntax highlighter.
7
+ *
8
+ * HEADER FILE
9
+ *
10
+ * Author: Dario Rodriguez dario@softhome.net
11
+ * This program is licensed under the terms of GNU GPL v2.1+
12
+ */
13
+
14
+#include "re_data.h"
15
+
16
+typedef struct linecolor_t {
17
+        int len;
18
+        int color;
19
+} linecolor_t;
20
+
21
+typedef struct line_t {
22
+        long pos;
23
+        int off; /* offset int buf */
24
+        int len; /* size used in buf */
25
+} hline_t;
26
+
27
+typedef struct hcolor_t {
28
+        char rgba[9];
29
+} hcolor_t;
30
+
31
+typedef struct highlighter_t {
32
+        int sizelines;
33
+        int usedlines;
34
+        hline_t *lines;
35
+        long sizebuf;
36
+        long usedbuf;
37
+        char *buf;
38
+        int sizecolors;
39
+        hcolor_t *colors;
40
+        int flag_doneall;
41
+} highlighter_t;
42
+
43
+int redata_highlighter_register(redata_t *redata, redata_plugin_t *slot);
44
+int redata_highlighter_unregister(redata_t *redata, redata_plugin_t *slot,char *filename);
45
+
46
+hcolor_t *redata_highlighter_getcolors(redata_t *redata, int *ncolors);
47
+linecolor_t *redata_highlighter_getline(redata_t *redata, int line, int *nlinecolors);
48
+
... ...
@@ -17,6 +17,7 @@
17 17
 
18 18
 #include "re_data.h"
19 19
 #include "re_plugin_unsaved.h"
20
+#include "re_plugin_highlighter.h"
20 21
 #include "re_ui.h"
21 22
 #include "ext/socklib.h"
22 23
 
... ...
@@ -304,7 +305,10 @@ re_init(void)
304 305
         if((re=malloc(sizeof(re_t)))==NULL)
305 306
                 return(NULL); /* insuf. mem. */
306 307
         memset(re,0,sizeof(re_t));
307
-        if((re->data=redata_init(redata_unsaved_register,NULL))==NULL) {
308
+        if((re->data=redata_init(
309
+          redata_unsaved_register,
310
+          redata_highlighter_register,
311
+          NULL))==NULL) {
308 312
                 re_free(re),re=NULL;
309 313
                 return(NULL); /* insuf. mem. */
310 314
         }