forked from dmacvicar/qemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.c
205 lines (190 loc) · 6.54 KB
/
xml.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* XML text mode for QEmacs.
*
* Copyright (c) 2002 Fabrice Bellard.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
/* colorization states */
enum {
XML_TAG = 1,
XML_COMMENT,
XML_TAG_SCRIPT,
XML_TAG_STYLE,
XML_STYLE,
XML_SCRIPT = 0x80, /* special mode for inside a script, ored with c mode */
};
static void xml_colorize_line(unsigned int *buf, int len, int mode_flags,
int *colorize_state_ptr, int state_only)
{
int c, state;
unsigned int *p, *p_start, *p1;
state = *colorize_state_ptr;
p = buf;
p_start = p;
/* if already in a state, go directly in the code parsing it */
if (state & XML_SCRIPT)
goto parse_script;
switch (state) {
case XML_COMMENT:
goto parse_comment;
case XML_TAG:
case XML_TAG_SCRIPT:
goto parse_tag;
case XML_STYLE:
goto parse_style;
default:
break;
}
for (;;) {
p_start = p;
c = *p;
if (c == '\0')
break;
if (c == '<' && state == 0) {
p++;
if (p[0] == '!' && p[1] == '-' && p[2] == '-') {
p += 3;
state = XML_COMMENT;
/* wait until end of comment */
parse_comment:
while (*p != '\0') {
if (p[0] == '-' && p[1] == '-' && p[2] == '>') {
p += 3;
state = 0;
break;
} else {
p++;
}
}
set_color(p_start, p, QE_STYLE_COMMENT);
} else {
/* we are in a tag */
if (ustristart(p, "SCRIPT", (const unsigned int **)&p)) {
state = XML_TAG_SCRIPT;
} else
if (ustristart(p, "STYLE", (const unsigned int **)&p)) {
state = XML_TAG_STYLE;
}
parse_tag:
while (*p != '\0') {
if (*p == '>') {
p++;
if (state == XML_TAG_SCRIPT)
state = XML_SCRIPT;
else
if (state == XML_TAG_STYLE)
state = XML_STYLE;
else
state = 0;
break;
} else {
p++;
}
}
set_color(p_start, p, QE_STYLE_TAG);
if (state == XML_SCRIPT) {
/* javascript coloring */
p_start = p;
parse_script:
for (;;) {
if (*p == '\0') {
state &= ~XML_SCRIPT;
/* XXX: should have javascript specific colorize_func */
c_colorize_line(p_start, p - p_start,
CLANG_C | CLANG_REGEX, &state, state_only);
state |= XML_SCRIPT;
break;
} else
if (ustristart(p, "</SCRIPT", (const unsigned int **)&p1)) {
while (*p1 != '\0') {
if (*p1++ == '>')
break;
}
state &= ~XML_SCRIPT;
c = *p;
*p = '\0';
/* XXX: should have javascript specific colorize_func */
c_colorize_line(p_start, p - p_start,
CLANG_C | CLANG_REGEX, &state, state_only);
*p = c;
state |= XML_SCRIPT;
set_color(p, p1, QE_STYLE_TAG);
p = p1;
state = 0;
break;
} else {
p++;
}
}
} else
if (state == XML_STYLE) {
/* stylesheet coloring */
p_start = p;
parse_style:
for (;;) {
if (*p == '\0') {
set_color(p_start, p, QE_STYLE_CSS);
break;
} else
if (ustristart(p, "</STYLE", (const unsigned int **)&p1)) {
while (*p1 != '\0') {
if (*p1++ != '>')
break;
}
set_color(p_start, p, QE_STYLE_CSS);
set_color(p, p1, QE_STYLE_TAG);
p = p1;
state = 0;
break;
} else {
p++;
}
}
}
}
} else {
/* text */
p++;
}
}
*colorize_state_ptr = state;
}
static int xml_mode_probe(ModeDef *mode, ModeProbeData *p1)
{
const u8 *p;
p = p1->buf;
while (qe_isspace(*p))
p++;
if (*p != '<')
return 0;
p++;
if (*p != '!' && *p != '?' && !qe_isalpha(*p))
return 0;
return 80; /* leave some room for more specific XML parser */
}
ModeDef xml_mode;
static int xml_init(void)
{
/* xml mode is almost like the text mode, so we copy and patch it */
memcpy(&xml_mode, &text_mode, sizeof(ModeDef));
xml_mode.name = "xml";
xml_mode.mode_probe = xml_mode_probe;
xml_mode.colorize_func = xml_colorize_line;
qe_register_mode(&xml_mode);
return 0;
}
qe_module_init(xml_init);