]> git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexTeX.cxx
62ade1d75820fdc7dacd0c8bbd63ec8e34027126
[wxWidgets.git] / src / stc / scintilla / src / LexTeX.cxx
1 // Scintilla source code edit control
2
3 // File: LexTeX.cxx - general context conformant tex coloring scheme
4 // Author: Hans Hagen - PRAGMA ADE - Hasselt NL - www.pragma-ade.com
5 // Version: September 28, 2003
6
7 // Copyright: 1998-2003 by Neil Hodgson <neilh@scintilla.org>
8 // The License.txt file describes the conditions under which this software may be distributed.
9
10 // This lexer is derived from the one written for the texwork environment (1999++) which in
11 // turn is inspired on texedit (1991++) which finds its roots in wdt (1986).
12
13 // If you run into strange boundary cases, just tell me and I'll look into it.
14
15
16 // TeX Folding code added by instanton (soft_share@126.com) with borrowed code from VisualTeX source by Alex Romanenko.
17 // Version: June 22, 2007
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24
25 #include "Platform.h"
26
27 #include "PropSet.h"
28 #include "Accessor.h"
29 #include "KeyWords.h"
30 #include "Scintilla.h"
31 #include "SciLexer.h"
32 #include "StyleContext.h"
33
34 #ifdef SCI_NAMESPACE
35 using namespace Scintilla;
36 #endif
37
38 // val SCE_TEX_DEFAULT = 0
39 // val SCE_TEX_SPECIAL = 1
40 // val SCE_TEX_GROUP = 2
41 // val SCE_TEX_SYMBOL = 3
42 // val SCE_TEX_COMMAND = 4
43 // val SCE_TEX_TEXT = 5
44
45 // Definitions in SciTEGlobal.properties:
46 //
47 // TeX Highlighting
48 //
49 // # Default
50 // style.tex.0=fore:#7F7F00
51 // # Special
52 // style.tex.1=fore:#007F7F
53 // # Group
54 // style.tex.2=fore:#880000
55 // # Symbol
56 // style.tex.3=fore:#7F7F00
57 // # Command
58 // style.tex.4=fore:#008800
59 // # Text
60 // style.tex.5=fore:#000000
61
62 // lexer.tex.interface.default=0
63 // lexer.tex.comment.process=0
64
65 // todo: lexer.tex.auto.if
66
67 // Auxiliary functions:
68
69 static inline bool endOfLine(Accessor &styler, unsigned int i) {
70 return
71 (styler[i] == '\n') || ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n')) ;
72 }
73
74 static inline bool isTeXzero(int ch) {
75 return
76 (ch == '%') ;
77 }
78
79 static inline bool isTeXone(int ch) {
80 return
81 (ch == '[') || (ch == ']') || (ch == '=') || (ch == '#') ||
82 (ch == '(') || (ch == ')') || (ch == '<') || (ch == '>') ||
83 (ch == '"') ;
84 }
85
86 static inline bool isTeXtwo(int ch) {
87 return
88 (ch == '{') || (ch == '}') || (ch == '$') ;
89 }
90
91 static inline bool isTeXthree(int ch) {
92 return
93 (ch == '~') || (ch == '^') || (ch == '_') || (ch == '&') ||
94 (ch == '-') || (ch == '+') || (ch == '\"') || (ch == '`') ||
95 (ch == '/') || (ch == '|') || (ch == '%') ;
96 }
97
98 static inline bool isTeXfour(int ch) {
99 return
100 (ch == '\\') ;
101 }
102
103 static inline bool isTeXfive(int ch) {
104 return
105 ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
106 (ch == '@') || (ch == '!') || (ch == '?') ;
107 }
108
109 static inline bool isTeXsix(int ch) {
110 return
111 (ch == ' ') ;
112 }
113
114 static inline bool isTeXseven(int ch) {
115 return
116 (ch == '^') ;
117 }
118
119 // Interface determination
120
121 static int CheckTeXInterface(
122 unsigned int startPos,
123 int length,
124 Accessor &styler,
125 int defaultInterface) {
126
127 char lineBuffer[1024] ;
128 unsigned int linePos = 0 ;
129
130 // some day we can make something lexer.tex.mapping=(all,0)(nl,1)(en,2)...
131
132 if (styler.SafeGetCharAt(0) == '%') {
133 for (unsigned int i = 0; i < startPos + length; i++) {
134 lineBuffer[linePos++] = styler.SafeGetCharAt(i) ;
135 if (endOfLine(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
136 lineBuffer[linePos] = '\0';
137 if (strstr(lineBuffer, "interface=all")) {
138 return 0 ;
139 } else if (strstr(lineBuffer, "interface=tex")) {
140 return 1 ;
141 } else if (strstr(lineBuffer, "interface=nl")) {
142 return 2 ;
143 } else if (strstr(lineBuffer, "interface=en")) {
144 return 3 ;
145 } else if (strstr(lineBuffer, "interface=de")) {
146 return 4 ;
147 } else if (strstr(lineBuffer, "interface=cz")) {
148 return 5 ;
149 } else if (strstr(lineBuffer, "interface=it")) {
150 return 6 ;
151 } else if (strstr(lineBuffer, "interface=ro")) {
152 return 7 ;
153 } else if (strstr(lineBuffer, "interface=latex")) {
154 // we will move latex cum suis up to 91+ when more keyword lists are supported
155 return 8 ;
156 } else if (styler.SafeGetCharAt(1) == 'D' && strstr(lineBuffer, "%D \\module")) {
157 // better would be to limit the search to just one line
158 return 3 ;
159 } else {
160 return defaultInterface ;
161 }
162 }
163 }
164 }
165
166 return defaultInterface ;
167 }
168
169 static void ColouriseTeXDoc(
170 unsigned int startPos,
171 int length,
172 int,
173 WordList *keywordlists[],
174 Accessor &styler) {
175
176 styler.StartAt(startPos) ;
177 styler.StartSegment(startPos) ;
178
179 bool processComment = styler.GetPropertyInt("lexer.tex.comment.process", 0) == 1 ;
180 bool useKeywords = styler.GetPropertyInt("lexer.tex.use.keywords", 1) == 1 ;
181 bool autoIf = styler.GetPropertyInt("lexer.tex.auto.if", 1) == 1 ;
182 int defaultInterface = styler.GetPropertyInt("lexer.tex.interface.default", 1) ;
183
184 char key[100] ;
185 int k ;
186 bool newifDone = false ;
187 bool inComment = false ;
188
189 int currentInterface = CheckTeXInterface(startPos,length,styler,defaultInterface) ;
190
191 if (currentInterface == 0) {
192 useKeywords = false ;
193 currentInterface = 1 ;
194 }
195
196 WordList &keywords = *keywordlists[currentInterface-1] ;
197
198 StyleContext sc(startPos, length, SCE_TEX_TEXT, styler);
199
200 bool going = sc.More() ; // needed because of a fuzzy end of file state
201
202 for (; going; sc.Forward()) {
203
204 if (! sc.More()) { going = false ; } // we need to go one behind the end of text
205
206 if (inComment) {
207 if (sc.atLineEnd) {
208 sc.SetState(SCE_TEX_TEXT) ;
209 newifDone = false ;
210 inComment = false ;
211 }
212 } else {
213 if (! isTeXfive(sc.ch)) {
214 if (sc.state == SCE_TEX_COMMAND) {
215 if (sc.LengthCurrent() == 1) { // \<noncstoken>
216 if (isTeXseven(sc.ch) && isTeXseven(sc.chNext)) {
217 sc.Forward(2) ; // \^^ and \^^<token>
218 }
219 sc.ForwardSetState(SCE_TEX_TEXT) ;
220 } else {
221 sc.GetCurrent(key, sizeof(key)-1) ;
222 k = strlen(key) ;
223 memmove(key,key+1,k) ; // shift left over escape token
224 key[k] = '\0' ;
225 k-- ;
226 if (! keywords || ! useKeywords) {
227 sc.SetState(SCE_TEX_COMMAND) ;
228 newifDone = false ;
229 } else if (k == 1) { //\<cstoken>
230 sc.SetState(SCE_TEX_COMMAND) ;
231 newifDone = false ;
232 } else if (keywords.InList(key)) {
233 sc.SetState(SCE_TEX_COMMAND) ;
234 newifDone = autoIf && (strcmp(key,"newif") == 0) ;
235 } else if (autoIf && ! newifDone && (key[0] == 'i') && (key[1] == 'f') && keywords.InList("if")) {
236 sc.SetState(SCE_TEX_COMMAND) ;
237 } else {
238 sc.ChangeState(SCE_TEX_TEXT) ;
239 sc.SetState(SCE_TEX_TEXT) ;
240 newifDone = false ;
241 }
242 }
243 }
244 if (isTeXzero(sc.ch)) {
245 sc.SetState(SCE_TEX_SYMBOL);
246
247 if (!endOfLine(styler,sc.currentPos + 1))
248 sc.ForwardSetState(SCE_TEX_DEFAULT) ;
249
250 inComment = ! processComment ;
251 newifDone = false ;
252 } else if (isTeXseven(sc.ch) && isTeXseven(sc.chNext)) {
253 sc.SetState(SCE_TEX_TEXT) ;
254 sc.ForwardSetState(SCE_TEX_TEXT) ;
255 } else if (isTeXone(sc.ch)) {
256 sc.SetState(SCE_TEX_SPECIAL) ;
257 newifDone = false ;
258 } else if (isTeXtwo(sc.ch)) {
259 sc.SetState(SCE_TEX_GROUP) ;
260 newifDone = false ;
261 } else if (isTeXthree(sc.ch)) {
262 sc.SetState(SCE_TEX_SYMBOL) ;
263 newifDone = false ;
264 } else if (isTeXfour(sc.ch)) {
265 sc.SetState(SCE_TEX_COMMAND) ;
266 } else if (isTeXsix(sc.ch)) {
267 sc.SetState(SCE_TEX_TEXT) ;
268 } else if (sc.atLineEnd) {
269 sc.SetState(SCE_TEX_TEXT) ;
270 newifDone = false ;
271 inComment = false ;
272 } else {
273 sc.SetState(SCE_TEX_TEXT) ;
274 }
275 } else if (sc.state != SCE_TEX_COMMAND) {
276 sc.SetState(SCE_TEX_TEXT) ;
277 }
278 }
279 }
280 sc.ChangeState(SCE_TEX_TEXT) ;
281 sc.Complete();
282
283 }
284
285
286 static inline bool isNumber(int ch) {
287 return
288 (ch == '0') || (ch == '1') || (ch == '2') ||
289 (ch == '3') || (ch == '4') || (ch == '5') ||
290 (ch == '6') || (ch == '7') || (ch == '8') || (ch == '9');
291 }
292
293 static inline bool isWordChar(int ch) {
294 return ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'));
295 }
296
297 static int ParseTeXCommand(unsigned int pos, Accessor &styler, char *command)
298 {
299 int length=0;
300 char ch=styler.SafeGetCharAt(pos+1);
301
302 if(ch==',' || ch==':' || ch==';' || ch=='%'){
303 command[0]=ch;
304 command[1]=0;
305 return 1;
306 }
307
308 // find end
309 while(isWordChar(ch) && !isNumber(ch) && ch!='_' && ch!='.' && length<100){
310 command[length]=ch;
311 length++;
312 ch=styler.SafeGetCharAt(pos+length+1);
313 }
314
315 command[length]='\0';
316 if(!length) return 0;
317 return length+1;
318 }
319
320 static int classifyFoldPointTeXPaired(const char* s) {
321 int lev=0;
322 if (!(isdigit(s[0]) || (s[0] == '.'))){
323 if (strcmp(s, "begin")==0||strcmp(s,"FoldStart")==0||
324 strcmp(s,"abstract")==0||strcmp(s,"unprotect")==0||
325 strcmp(s,"title")==0||strncmp(s,"start",5)==0||strncmp(s,"Start",5)==0||
326 strcmp(s,"documentclass")==0||strncmp(s,"if",2)==0
327 )
328 lev=1;
329 if (strcmp(s, "end")==0||strcmp(s,"FoldStop")==0||
330 strcmp(s,"maketitle")==0||strcmp(s,"protect")==0||
331 strncmp(s,"stop",4)==0||strncmp(s,"Stop",4)==0||
332 strcmp(s,"fi")==0
333 )
334 lev=-1;
335 }
336 return lev;
337 }
338
339 static int classifyFoldPointTeXUnpaired(const char* s) {
340 int lev=0;
341 if (!(isdigit(s[0]) || (s[0] == '.'))){
342 if (strcmp(s,"part")==0||
343 strcmp(s,"chapter")==0||
344 strcmp(s,"section")==0||
345 strcmp(s,"subsection")==0||
346 strcmp(s,"subsubsection")==0||
347 strcmp(s,"CJKfamily")==0||
348 strcmp(s,"appendix")==0||
349 strcmp(s,"Topic")==0||strcmp(s,"topic")==0||
350 strcmp(s,"subject")==0||strcmp(s,"subsubject")==0||
351 strcmp(s,"def")==0||strcmp(s,"gdef")==0||strcmp(s,"edef")==0||
352 strcmp(s,"xdef")==0||strcmp(s,"framed")==0||
353 strcmp(s,"frame")==0||
354 strcmp(s,"foilhead")==0||strcmp(s,"overlays")==0||strcmp(s,"slide")==0
355 ){
356 lev=1;
357 }
358 }
359 return lev;
360 }
361
362 static bool IsTeXCommentLine(int line, Accessor &styler) {
363 int pos = styler.LineStart(line);
364 int eol_pos = styler.LineStart(line + 1) - 1;
365
366 int startpos = pos;
367
368 while (startpos<eol_pos){
369 char ch = styler[startpos];
370 if (ch!='%' && ch!=' ') return false;
371 else if (ch=='%') return true;
372 startpos++;
373 }
374
375 return false;
376 }
377
378 // FoldTeXDoc: borrowed from VisualTeX with modifications
379
380 static void FoldTexDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler)
381 {
382 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
383 unsigned int endPos = startPos+length;
384 int visibleChars=0;
385 int lineCurrent=styler.GetLine(startPos);
386 int levelPrev=styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
387 int levelCurrent=levelPrev;
388 char chNext=styler[startPos];
389 char buffer[100]="";
390
391 for (unsigned int i=startPos; i < endPos; i++) {
392 char ch=chNext;
393 chNext=styler.SafeGetCharAt(i+1);
394 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
395
396 if(ch=='\\') {
397 ParseTeXCommand(i, styler, buffer);
398 levelCurrent += classifyFoldPointTeXPaired(buffer)+classifyFoldPointTeXUnpaired(buffer);
399 }
400
401 if (levelCurrent > SC_FOLDLEVELBASE && ((ch == '\r' || ch=='\n') && (chNext == '\\'))) {
402 ParseTeXCommand(i+1, styler, buffer);
403 levelCurrent -= classifyFoldPointTeXUnpaired(buffer);
404 }
405
406 char chNext2;
407 char chNext3;
408 char chNext4;
409 char chNext5;
410 chNext2=styler.SafeGetCharAt(i+2);
411 chNext3=styler.SafeGetCharAt(i+3);
412 chNext4=styler.SafeGetCharAt(i+4);
413 chNext5=styler.SafeGetCharAt(i+5);
414
415 bool atEOfold = (ch == '%') &&
416 (chNext == '%') && (chNext2=='}') &&
417 (chNext3=='}')&& (chNext4=='-')&& (chNext5=='-');
418
419 bool atBOfold = (ch == '%') &&
420 (chNext == '%') && (chNext2=='-') &&
421 (chNext3=='-')&& (chNext4=='{')&& (chNext5=='{');
422
423 if(atBOfold){
424 levelCurrent+=1;
425 }
426
427 if(atEOfold){
428 levelCurrent-=1;
429 }
430
431 if(ch=='\\' && chNext=='['){
432 levelCurrent+=1;
433 }
434
435 if(ch=='\\' && chNext==']'){
436 levelCurrent-=1;
437 }
438
439 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
440
441 if (foldComment && atEOL && IsTeXCommentLine(lineCurrent, styler))
442 {
443 if (lineCurrent==0 && IsTeXCommentLine(lineCurrent + 1, styler)
444 )
445 levelCurrent++;
446 else if (lineCurrent!=0 && !IsTeXCommentLine(lineCurrent - 1, styler)
447 && IsTeXCommentLine(lineCurrent + 1, styler)
448 )
449 levelCurrent++;
450 else if (lineCurrent!=0 && IsTeXCommentLine(lineCurrent - 1, styler) &&
451 !IsTeXCommentLine(lineCurrent+1, styler))
452 levelCurrent--;
453 }
454
455 //---------------------------------------------------------------------------------------------
456
457 if (atEOL) {
458 int lev = levelPrev;
459 if (visibleChars == 0 && foldCompact)
460 lev |= SC_FOLDLEVELWHITEFLAG;
461 if ((levelCurrent > levelPrev) && (visibleChars > 0))
462 lev |= SC_FOLDLEVELHEADERFLAG;
463 if (lev != styler.LevelAt(lineCurrent)) {
464 styler.SetLevel(lineCurrent, lev);
465 }
466 lineCurrent++;
467 levelPrev = levelCurrent;
468 visibleChars = 0;
469 }
470
471 if (!isspacechar(ch))
472 visibleChars++;
473 }
474
475 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
476 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
477 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
478 }
479
480
481
482
483 static const char * const texWordListDesc[] = {
484 "TeX, eTeX, pdfTeX, Omega",
485 "ConTeXt Dutch",
486 "ConTeXt English",
487 "ConTeXt German",
488 "ConTeXt Czech",
489 "ConTeXt Italian",
490 "ConTeXt Romanian",
491 0,
492 } ;
493
494 LexerModule lmTeX(SCLEX_TEX, ColouriseTeXDoc, "tex", FoldTexDoc, texWordListDesc);