Add xterm-256color as a valid terminal.
[eco.git] / syntax.c
blob79cfd5ca8f0ec70f1736d0d573f7dddd5f8b7c5d
1 /*
2 * Copyright (C) 2010 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <ctype.h>
11 #include "debug.h"
12 #include "list.h"
13 #include "term.h"
14 #include "screen.h"
15 #include "syntax.h"
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)
23 E_Syntax_Word *p;
25 p= (E_Syntax_Word *)sy->dirs.first;
26 while (p) {
27 if (p->nch == nch) {
28 if (!strncmp(p->word, word, nch))
29 return(p);
31 p= p->next;
34 return(NULL);
37 static E_Syntax_Char *e_syntax_char_search(E_Syntax *sy, char ch)
39 E_Syntax_Char *p;
41 p= (E_Syntax_Char *)sy->chars.first;
42 while (p) {
43 if (p->ch == ch)
44 return(p);
45 p= p->next;
47 return(NULL);
50 E_Syntax *e_syntax_search(char *ext)
52 E_Syntax *p;
54 p= (E_Syntax *)syntaxs.first;
55 while (p) {
56 if ((*p->check_type)(ext) == 0)
57 return(p);
58 p= p->next;
60 return(NULL);
63 void e_syntax_add(E_Syntax *sy)
65 if (sy) {
66 E_LIST_ADD(&syntaxs, sy);
70 void e_syntax_rem(E_Syntax *sy)
72 if (sy) {
73 E_LIST_REM(&syntaxs, sy);
77 E_Syntax *e_syntax_new(void)
79 E_Syntax *sy;
81 sy= (E_Syntax *)malloc(sizeof(E_Syntax));
82 sy->next= NULL;
83 sy->prev= NULL;
84 sy->name= NULL;
85 sy->check_type= NULL;
86 sy->dirs.first= NULL;
87 sy->dirs.last= NULL;
88 sy->chars.first= NULL;
89 sy->chars.last= NULL;
90 return(sy);
93 void e_syntax_free(E_Syntax *sy)
95 E_Syntax_Word *p;
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);
101 free((void *)p);
104 if (sy->name)
105 free((void *)sy->name);
107 free((void *)sy);
110 void e_syntax_word_add(E_Syntax *sy, char *word, char fg, char bg)
112 E_Syntax_Word *wr;
114 wr= e_syntax_word_search(sy, word, strlen(word));
115 if (wr)
116 return;
118 wr= (E_Syntax_Word *)malloc(sizeof(E_Syntax_Word));
119 wr->next= NULL;
120 wr->prev= NULL;
121 wr->word= strdup(word);
122 wr->nch= strlen(wr->word);
123 wr->fgcol= fg;
124 wr->bgcol= bg;
125 E_LIST_ADD(&sy->dirs, wr);
128 void e_syntax_char_add(E_Syntax *sy, char ch, char fg, char bg)
130 E_Syntax_Char *cha;
132 cha= e_syntax_char_search(sy, ch);
133 if (cha)
134 return;
136 cha= (E_Syntax_Char *)malloc(sizeof(E_Syntax_Char));
137 cha->next= NULL;
138 cha->prev= NULL;
139 cha->ch= ch;
140 cha->fgcol= fg;
141 cha->bgcol= bg;
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)
157 int i, tot, first;
159 first= 0;
161 /* Search the start of the word. */
162 for (i= idx; i < nch; i++) {
163 if (check_token_start(line[i])) {
164 first= 1;
165 break;
169 if (!first)
170 return(-1);
172 *istart= i;
173 tot= 0;
175 e_debug_printf("Syntax word [");
176 for (i= *istart; i < nch; i++) {
177 if (check_token_end(line[i]))
178 break;
179 tot++;
181 if (tot)
182 return(tot);
183 return(-1);
186 static void e_syntax_color(E_Syntax_Word *wr, E_ScreenLine *ln, int i, int nch)
188 int e;
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)
198 E_Syntax_Word *wr;
199 E_Syntax_Char *ch;
200 int i, nch, istart;
202 i= 0;
204 /* First round, check for words. */
205 do_word:
206 nch= e_syntax_word(ln->text, i, &istart, ncol);
207 if (nch == -1) {
208 goto do_char;
209 return;
212 wr= e_syntax_word_search(sy, &(ln->text[istart]), nch);
213 if (wr)
214 e_syntax_color(wr, ln, istart, nch);
216 if (i+nch < ncol) {
217 i += nch;
218 goto do_word;
221 /* Second round, check for characters. */
222 do_char:
223 for (i= 0; i < ncol; i++) {
224 ch= e_syntax_char_search(sy, ln->text[i]);
225 if (ch) {
226 ln->fcol[i]= ch->fgcol;
227 ln->bcol[i]= ch->bgcol;