2 * Copyright (C) 2010 Diego Hernan Borghetti.
18 /* our syntax data base. */
19 static E_List syntaxs
= { NULL
, NULL
};
21 static E_Syntax_Word
*e_syntax_word_search(E_Syntax
*sy
, char *word
, int nch
)
25 p
= (E_Syntax_Word
*)sy
->dirs
.first
;
28 if (!strncmp(p
->word
, word
, nch
))
37 static E_Syntax_Char
*e_syntax_char_search(E_Syntax
*sy
, char ch
)
41 p
= (E_Syntax_Char
*)sy
->chars
.first
;
50 E_Syntax
*e_syntax_search(char *ext
)
54 p
= (E_Syntax
*)syntaxs
.first
;
56 if ((*p
->check_type
)(ext
) == 0)
63 void e_syntax_add(E_Syntax
*sy
)
66 E_LIST_ADD(&syntaxs
, sy
);
70 void e_syntax_rem(E_Syntax
*sy
)
73 E_LIST_REM(&syntaxs
, sy
);
77 E_Syntax
*e_syntax_new(void)
81 sy
= (E_Syntax
*)malloc(sizeof(E_Syntax
));
88 sy
->chars
.first
= NULL
;
93 void e_syntax_free(E_Syntax
*sy
)
97 while (sy
->dirs
.first
) {
98 p
= (E_Syntax_Word
*)sy
->dirs
.first
;
99 sy
->dirs
.first
= sy
->dirs
.first
->next
;
100 free((void *)p
->word
);
105 free((void *)sy
->name
);
110 void e_syntax_word_add(E_Syntax
*sy
, char *word
, char fg
, char bg
)
114 wr
= e_syntax_word_search(sy
, word
, strlen(word
));
118 wr
= (E_Syntax_Word
*)malloc(sizeof(E_Syntax_Word
));
121 wr
->word
= strdup(word
);
122 wr
->nch
= strlen(wr
->word
);
125 E_LIST_ADD(&sy
->dirs
, wr
);
128 void e_syntax_char_add(E_Syntax
*sy
, char ch
, char fg
, char bg
)
132 cha
= e_syntax_char_search(sy
, ch
);
136 cha
= (E_Syntax_Char
*)malloc(sizeof(E_Syntax_Char
));
142 E_LIST_ADD(&sy
->chars
, cha
);
145 static inline int check_token_start(int nch
)
147 return(isalpha(nch
));
150 static inline int check_token_end(int nch
)
152 return(!isalpha(nch
));
155 static int e_syntax_word(char *line
, int idx
, int *istart
, int nch
)
161 /* Search the start of the word. */
162 for (i
= idx
; i
< nch
; i
++) {
163 if (check_token_start(line
[i
])) {
175 e_debug_printf("Syntax word [");
176 for (i
= *istart
; i
< nch
; i
++) {
177 if (check_token_end(line
[i
]))
186 static void e_syntax_color(E_Syntax_Word
*wr
, E_ScreenLine
*ln
, int i
, int nch
)
190 for (e
= i
; e
< (i
+nch
); e
++) {
191 ln
->fcol
[e
]= wr
->fgcol
;
192 ln
->bcol
[e
]= wr
->bgcol
;
196 void e_syntax_apply(E_Syntax
*sy
, E_ScreenLine
*ln
, int ncol
)
204 /* First round, check for words. */
206 nch
= e_syntax_word(ln
->text
, i
, &istart
, ncol
);
212 wr
= e_syntax_word_search(sy
, &(ln
->text
[istart
]), nch
);
214 e_syntax_color(wr
, ln
, istart
, nch
);
221 /* Second round, check for characters. */
223 for (i
= 0; i
< ncol
; i
++) {
224 ch
= e_syntax_char_search(sy
, ln
->text
[i
]);
226 ln
->fcol
[i
]= ch
->fgcol
;
227 ln
->bcol
[i
]= ch
->bgcol
;