-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatex-mode.c
158 lines (145 loc) · 4.56 KB
/
latex-mode.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
/*
* LaTeX mode for QEmacs.
* Copyright (c) 2003 Martin Hedenfalk <[email protected]>
* Based on c-mode by Fabrice Bellard
* Requires the shell mode
*
* 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"
#define MAX_BUF_SIZE 512
static ModeDef latex_mode;
/* TODO: add state handling to allow colorization of elements longer
* than one line (eg, multi-line functions and strings)
*/
static void latex_colorize_line(unsigned int *buf, int len,
int *colorize_state_ptr, int state_only)
{
int c, state;
unsigned int *p, *p_start;
state = *colorize_state_ptr;
p = buf;
p_start = p;
for(;;) {
p_start = p;
c = *p;
switch(c) {
case '\n':
goto the_end;
case '`':
p++;
/* a ``string'' */
if(*p == '`') {
while(1) {
p++;
if(*p == '\n' || (*p == '\'' && *(p+1) == '\''))
break;
}
if(*p == '\'' && *++p == '\'')
p++;
set_color(p_start, p - p_start, QE_STYLE_STRING);
}
break;
case '\\':
p++;
/* \function[keyword]{variable} */
if(*p == '\'' || *p == '\"' || *p == '~' || *p == '%' || *p == '\\')
p++;
else {
while(*p != '{' && *p != '[' && *p != '\n' && *p != ' ' && *p != '\\')
p++;
}
set_color(p_start, p - p_start, QE_STYLE_FUNCTION);
while(*p == ' ' || *p == '\t')
/* skip space */
p++;
while(*p == '{' || *p == '[') {
if(*p++ == '[') {
/* handle [keyword] */
p_start = p;
while(*p != ']' && *p != '\n')
p++;
set_color(p_start, p - p_start, QE_STYLE_KEYWORD);
if(*p == ']')
p++;
} else {
int braces = 0;
/* handle {variable} */
p_start = p;
while(*p != '\n') {
if(*p == '{')
braces++;
else if(*p == '}')
if(braces-- == 0)
break;
p++;
}
set_color(p_start, p - p_start, QE_STYLE_VARIABLE);
if(*p == '}')
p++;
}
while(*p == ' ' || *p == '\t')
/* skip space */
p++;
}
break;
case '%':
p++;
/* line comment */
while (*p != '\n')
p++;
set_color(p_start, p - p_start, QE_STYLE_COMMENT);
break;
default:
p++;
break;
}
}
the_end:
*colorize_state_ptr = state;
}
static int latex_mode_probe(ModeProbeData *p)
{
const char *r;
/* currently, only use the file extension */
r = strrchr((char *)p->filename, '.');
if (r) {
r++;
if (strcasecmp(r, "tex") == 0)
return 100;
}
return 0;
}
static int latex_mode_init(EditState *s, ModeSavedData *saved_data)
{
int ret;
ret = text_mode_init(s, saved_data);
if (ret)
return ret;
set_colorize_func(s, latex_colorize_line);
return ret;
}
static int latex_init(void)
{
/* LaTeX mode is almost like the text mode, so we copy and patch it */
memcpy(&latex_mode, &text_mode, sizeof(ModeDef));
latex_mode.name = "LaTeX";
latex_mode.mode_probe = latex_mode_probe;
latex_mode.mode_init = latex_mode_init;
latex_mode.mode_flags = MODEF_NOCMD;
qe_register_mode(&latex_mode);
return 0;
}
qe_module_init(latex_init);