vi: explain when the screen is updated in vi()
[neatvi.git] / syn.c
blob7f4a487cc814a247122e842b132e1428e296058c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "vi.h"
6 #define NFTS 16
8 /* mapping filetypes to regular expression sets */
9 static struct ftmap {
10 char ft[32];
11 struct rset *rs;
12 } ftmap[NFTS];
14 static struct rset *syn_ftrs;
16 static struct rset *syn_find(char *ft)
18 int i;
19 for (i = 0; i < LEN(ftmap); i++)
20 if (!strcmp(ft, ftmap[i].ft))
21 return ftmap[i].rs;
22 return NULL;
25 int syn_merge(int old, int new)
27 int fg = SYN_FGSET(new) ? SYN_FG(new) : SYN_FG(old);
28 int bg = SYN_BGSET(new) ? SYN_BG(new) : SYN_BG(old);
29 return ((old | new) & SYN_FLG) | (bg << 8) | fg;
32 int *syn_highlight(char *ft, char *s)
34 int subs[16 * 2];
35 int n = uc_slen(s);
36 int *att = malloc(n * sizeof(att[0]));
37 int sidx = 0;
38 struct rset *rs = syn_find(ft);
39 int flg = 0;
40 int hl, j, i;
41 memset(att, 0, n * sizeof(att[0]));
42 if (!rs)
43 return att;
44 while ((hl = rset_find(rs, s + sidx, LEN(subs) / 2, subs, flg)) >= 0) {
45 int grp = 0;
46 int cend = 1;
47 int *catt;
48 conf_highlight(hl, NULL, &catt, NULL, &grp);
49 for (i = 0; i < LEN(subs) / 2; i++) {
50 if (subs[i * 2] >= 0) {
51 int beg = uc_off(s, sidx + subs[i * 2 + 0]);
52 int end = uc_off(s, sidx + subs[i * 2 + 1]);
53 for (j = beg; j < end; j++)
54 att[j] = syn_merge(att[j], catt[i]);
55 if (i == grp)
56 cend = MAX(cend, subs[i * 2 + 1]);
59 sidx += cend;
60 flg = RE_NOTBOL;
62 return att;
65 static void syn_initft(char *name)
67 char *pats[128] = {NULL};
68 char *ft, *pat;
69 int i, n;
70 for (i = 0; !conf_highlight(i, &ft, NULL, &pat, NULL) && i < LEN(pats); i++)
71 if (!strcmp(ft, name))
72 pats[i] = pat;
73 n = i;
74 for (i = 0; i < LEN(ftmap); i++) {
75 if (!ftmap[i].ft[0]) {
76 strcpy(ftmap[i].ft, name);
77 ftmap[i].rs = rset_make(n, pats, 0);
78 return;
83 char *syn_filetype(char *path)
85 int hl = rset_find(syn_ftrs, path, 0, NULL, 0);
86 char *ft;
87 if (!conf_filetype(hl, &ft, NULL))
88 return ft;
89 return "";
92 void syn_init(void)
94 char *pats[128] = {NULL};
95 char *pat, *ft;
96 int i;
97 for (i = 0; !conf_highlight(i, &ft, NULL, NULL, NULL); i++)
98 if (!syn_find(ft))
99 syn_initft(ft);
100 for (i = 0; !conf_filetype(i, NULL, &pat) && i < LEN(pats); i++)
101 pats[i] = pat;
102 syn_ftrs = rset_make(i, pats, 0);
105 void syn_done(void)
107 int i;
108 for (i = 0; i < LEN(ftmap); i++)
109 if (ftmap[i].rs)
110 rset_free(ftmap[i].rs);
111 rset_free(syn_ftrs);