-
Notifications
You must be signed in to change notification settings - Fork 2
/
latex-mode.c
339 lines (306 loc) · 10.1 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* LaTeX mode for QEmacs.
*
* Copyright (c) 2003 Martin Hedenfalk <mhe@home.se>
* 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"
/* 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, __unused__ int len,
__unused__ int mode_flags,
int *colorize_state_ptr,
__unused__ 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 '\0':
case '\n': /* Should not happen */
goto the_end;
case '`':
p++;
/* a ``string'' */
if (*p == '`') {
for (;;) {
p++;
if (*p == '\0') {
/* Should either flag an error or propagate
* string style to the next line
*/
break;
}
if (*p == '\'' && p[1] == '\'') {
p += 2;
break;
}
}
set_color(p_start, p, QE_STYLE_STRING);
}
break;
case '\\':
p++;
/* \function[keyword]{variable} */
if (*p == '\'' || *p == '\"' || *p == '~' || *p == '%' || *p == '\\') {
p++;
} else {
while (*p != '\0' && *p != '{' && *p != '[' && *p != ' ' && *p != '\\')
p++;
}
set_color(p_start, p, QE_STYLE_FUNCTION);
while (*p == ' ' || *p == '\t') {
/* skip space */
p++;
}
while (*p == '{' || *p == '[') {
if (*p++ == '[') {
/* handle [keyword] */
p_start = p;
while (*p != '\0' && *p != ']')
p++;
set_color(p_start, p, QE_STYLE_KEYWORD);
if (*p == ']')
p++;
} else {
int braces = 0;
/* handle {variable} */
p_start = p;
while (*p != '\0') {
if (*p == '{') {
braces++;
} else
if (*p == '}') {
if (braces-- == 0)
break;
}
p++;
}
set_color(p_start, p, QE_STYLE_VARIABLE);
if (*p == '}')
p++;
}
while (*p == ' ' || *p == '\t') {
/* skip space */
p++;
}
}
break;
case '%':
p++;
/* line comment */
while (*p != '\0')
p++;
set_color(p_start, p, QE_STYLE_COMMENT);
break;
default:
p++;
break;
}
}
the_end:
*colorize_state_ptr = state;
}
static int latex_mode_probe(ModeDef *mode, ModeProbeData *p)
{
/* currently, only use the file extension */
/* Halibut (by Simon Tatham) has a syntax similar to TeX and uses
* .but extension */
if (match_extension(p->filename, mode->extensions))
return 80;
/* Match TeX style sheets if they start with a comment */
if (match_extension(p->filename, "sty") && p->buf[0] == '%')
return 80;
return 1;
}
static void do_tex_insert_quote(EditState *s)
{
int offset_bol, len, offset1;
unsigned int buf[COLORED_MAX_LINE_SIZE];
int pos;
offset_bol = eb_goto_bol2(s->b, s->offset, &pos);
offset1 = offset_bol;
len = eb_get_line(s->b, buf, countof(buf), &offset1);
if (pos > len)
return;
if (pos >= 1 && buf[pos-1] == '\"') {
s->offset += eb_insert_uchar(s->b, s->offset, '\"');
} else
if (pos >= 2 && (buf[pos-1] == '`' || buf[pos-1] == '\'') &&
buf[pos-1] == buf[pos-2])
{
eb_delete_chars(s->b, s->offset, -2);
s->offset += eb_insert_uchar(s->b, s->offset, '\"');
} else {
if (pos == 0 || buf[pos-1] == ' ') {
s->offset += eb_insert_str(s->b, s->offset, "``");
} else {
s->offset += eb_insert_str(s->b, s->offset, "''");
}
}
}
static struct latex_function {
const char *name;
const char *fmt;
int ask;
int output_to_buffer;
StringArray history;
EditState *es;
} latex_funcs[] = {
#define INIT_TAIL NULL_STRINGARRAY, NULL
{ "AmSTeX", "amstex '\\nonstopmode\\input %s'", 0, 1, INIT_TAIL },
{ "PDFLaTeX", "pdflatex '\\nonstopmode\\input{%s}'", 0, 1, INIT_TAIL },
{ "PDFTeX", "pdftex '\\nonstopmode\\input %s'", 0, 1, INIT_TAIL },
{ "Check", "lacheck %s", 0, 1, INIT_TAIL },
{ "BibTeX", "bibtex %s", 0, 1, INIT_TAIL },
{ "LaTeX", "latex --src-specials '\\nonstopmode\\input{%s}'", 0, 1, INIT_TAIL },
{ "ThumbPDF", "thumbpdf %s", 0, 1, INIT_TAIL },
{ "View", "xdvi %s.dvi -paper a4", 1, 0, INIT_TAIL },
{ "Print", "dvips %s -Plp", 1, 0, INIT_TAIL },
{ "File", "dvips %s.dvi -o %s.ps", 1, 1, INIT_TAIL },
{ NULL, NULL, 0, 0, INIT_TAIL },
#undef INIT_TAIL
};
static void latex_completion(CompleteState *cp)
{
struct latex_function *func;
for (func = latex_funcs; func->name; func++) {
if (strxstart(func->name, cp->current, NULL))
add_string(&cp->cs, func->name);
}
}
static struct latex_function *find_latex_func(const char *name)
{
struct latex_function *func;
for (func = latex_funcs; func->name; func++) {
if (!strxcmp(func->name, name))
return func;
}
return NULL;
}
static void latex_cmd_run(void *opaque, char *cmd)
{
struct latex_function *func = (struct latex_function *)opaque;
char cwd[MAX_FILENAME_SIZE];
char dir[MAX_FILENAME_SIZE];
char *p;
int len;
if (cmd == NULL) {
put_status(func->es, "Aborted");
return;
}
getcwd(cwd, sizeof(cwd));
/* get the directory of the open file and change into it
*/
p = strrchr(func->es->b->filename, '/');
if (p == func->es->b->filename)
p++;
len = p - func->es->b->filename;
pstrncpy(dir, sizeof(dir), func->es->b->filename, len);
chdir(dir);
if (func->output_to_buffer) {
/* if the buffer already exists, kill it */
EditBuffer *b = eb_find("*LaTeX output*");
if (b) {
/* XXX: e should not become invalid */
b->modified = 0;
do_kill_buffer(func->es, "*LaTeX output*");
}
/* create new buffer */
b = new_shell_buffer(NULL, "*LaTeX output*", NULL, cmd,
SF_COLOR | SF_INFINITE);
if (b) {
/* XXX: try to split window if necessary */
switch_to_buffer(func->es, b);
}
} else {
int pid = fork();
if (pid == 0) {
const char *argv[4];
/* child process */
setsid();
argv[0] = get_shell();
argv[1] = "-c";
argv[2] = cmd;
argv[3] = NULL;
execv(argv[0], (char * const*)argv);
exit(1);
}
}
chdir(cwd);
}
static void do_latex(EditState *e, const char *cmd)
{
char bname[MAX_FILENAME_SIZE];
char buf[1024];
struct latex_function *func;
/* strip extension from filename */
pstrcpy(bname, sizeof(bname), e->b->filename);
strip_extension(bname);
if (!cmd || cmd[0] == '\0')
cmd = "LaTeX";
/* check what command to run */
func = find_latex_func(cmd);
if (func) {
/* pass the EditState through to latex_cmd_run() */
func->es = e;
/* construct the command line to run */
strsubst(buf, sizeof(buf), func->fmt, "%s", bname);
if (func->ask) {
char prompt[128];
snprintf(prompt, sizeof(prompt), "%s command: ", func->name);
minibuffer_edit(buf, prompt, &func->history,
NULL /* completion */,
latex_cmd_run, func);
} else {
latex_cmd_run(func, buf);
}
} else {
put_status(e, "%s: No match", buf);
}
}
/* specific LaTeX commands */
static CmdDef latex_commands[] = {
CMD2( '\"', KEY_NONE,
"tex-insert-quote", do_tex_insert_quote, ES, "*")
CMD2( KEY_CTRLC(KEY_CTRL('c')), KEY_NONE, /* C-c C-c */
"TeX-command-master", do_latex, ESs,
"s{Command: (default LaTeX) }[latex]|latex|")
CMD_DEF_END,
};
static ModeDef latex_mode;
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.extensions = "tex|but";
latex_mode.mode_probe = latex_mode_probe;
latex_mode.colorize_func = latex_colorize_line;
qe_register_mode(&latex_mode);
qe_register_cmd_table(latex_commands, &latex_mode);
register_completion("latex", latex_completion);
return 0;
}
qe_module_init(latex_init);