]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexTeX.cxx
62ade1d75820fdc7dacd0c8bbd63ec8e34027126
1 // Scintilla source code edit control
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
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.
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).
13 // If you run into strange boundary cases, just tell me and I'll look into it.
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
30 #include "Scintilla.h"
32 #include "StyleContext.h"
35 using namespace Scintilla
;
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
45 // Definitions in SciTEGlobal.properties:
50 // style.tex.0=fore:#7F7F00
52 // style.tex.1=fore:#007F7F
54 // style.tex.2=fore:#880000
56 // style.tex.3=fore:#7F7F00
58 // style.tex.4=fore:#008800
60 // style.tex.5=fore:#000000
62 // lexer.tex.interface.default=0
63 // lexer.tex.comment.process=0
65 // todo: lexer.tex.auto.if
67 // Auxiliary functions:
69 static inline bool endOfLine(Accessor
&styler
, unsigned int i
) {
71 (styler
[i
] == '\n') || ((styler
[i
] == '\r') && (styler
.SafeGetCharAt(i
+ 1) != '\n')) ;
74 static inline bool isTeXzero(int ch
) {
79 static inline bool isTeXone(int ch
) {
81 (ch
== '[') || (ch
== ']') || (ch
== '=') || (ch
== '#') ||
82 (ch
== '(') || (ch
== ')') || (ch
== '<') || (ch
== '>') ||
86 static inline bool isTeXtwo(int ch
) {
88 (ch
== '{') || (ch
== '}') || (ch
== '$') ;
91 static inline bool isTeXthree(int ch
) {
93 (ch
== '~') || (ch
== '^') || (ch
== '_') || (ch
== '&') ||
94 (ch
== '-') || (ch
== '+') || (ch
== '\"') || (ch
== '`') ||
95 (ch
== '/') || (ch
== '|') || (ch
== '%') ;
98 static inline bool isTeXfour(int ch
) {
103 static inline bool isTeXfive(int ch
) {
105 ((ch
>= 'a') && (ch
<= 'z')) || ((ch
>= 'A') && (ch
<= 'Z')) ||
106 (ch
== '@') || (ch
== '!') || (ch
== '?') ;
109 static inline bool isTeXsix(int ch
) {
114 static inline bool isTeXseven(int ch
) {
119 // Interface determination
121 static int CheckTeXInterface(
122 unsigned int startPos
,
125 int defaultInterface
) {
127 char lineBuffer
[1024] ;
128 unsigned int linePos
= 0 ;
130 // some day we can make something lexer.tex.mapping=(all,0)(nl,1)(en,2)...
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")) {
139 } else if (strstr(lineBuffer
, "interface=tex")) {
141 } else if (strstr(lineBuffer
, "interface=nl")) {
143 } else if (strstr(lineBuffer
, "interface=en")) {
145 } else if (strstr(lineBuffer
, "interface=de")) {
147 } else if (strstr(lineBuffer
, "interface=cz")) {
149 } else if (strstr(lineBuffer
, "interface=it")) {
151 } else if (strstr(lineBuffer
, "interface=ro")) {
153 } else if (strstr(lineBuffer
, "interface=latex")) {
154 // we will move latex cum suis up to 91+ when more keyword lists are supported
156 } else if (styler
.SafeGetCharAt(1) == 'D' && strstr(lineBuffer
, "%D \\module")) {
157 // better would be to limit the search to just one line
160 return defaultInterface
;
166 return defaultInterface
;
169 static void ColouriseTeXDoc(
170 unsigned int startPos
,
173 WordList
*keywordlists
[],
176 styler
.StartAt(startPos
) ;
177 styler
.StartSegment(startPos
) ;
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) ;
186 bool newifDone
= false ;
187 bool inComment
= false ;
189 int currentInterface
= CheckTeXInterface(startPos
,length
,styler
,defaultInterface
) ;
191 if (currentInterface
== 0) {
192 useKeywords
= false ;
193 currentInterface
= 1 ;
196 WordList
&keywords
= *keywordlists
[currentInterface
-1] ;
198 StyleContext
sc(startPos
, length
, SCE_TEX_TEXT
, styler
);
200 bool going
= sc
.More() ; // needed because of a fuzzy end of file state
202 for (; going
; sc
.Forward()) {
204 if (! sc
.More()) { going
= false ; } // we need to go one behind the end of text
208 sc
.SetState(SCE_TEX_TEXT
) ;
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>
219 sc
.ForwardSetState(SCE_TEX_TEXT
) ;
221 sc
.GetCurrent(key
, sizeof(key
)-1) ;
223 memmove(key
,key
+1,k
) ; // shift left over escape token
226 if (! keywords
|| ! useKeywords
) {
227 sc
.SetState(SCE_TEX_COMMAND
) ;
229 } else if (k
== 1) { //\<cstoken>
230 sc
.SetState(SCE_TEX_COMMAND
) ;
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
) ;
238 sc
.ChangeState(SCE_TEX_TEXT
) ;
239 sc
.SetState(SCE_TEX_TEXT
) ;
244 if (isTeXzero(sc
.ch
)) {
245 sc
.SetState(SCE_TEX_SYMBOL
);
247 if (!endOfLine(styler
,sc
.currentPos
+ 1))
248 sc
.ForwardSetState(SCE_TEX_DEFAULT
) ;
250 inComment
= ! processComment
;
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
) ;
258 } else if (isTeXtwo(sc
.ch
)) {
259 sc
.SetState(SCE_TEX_GROUP
) ;
261 } else if (isTeXthree(sc
.ch
)) {
262 sc
.SetState(SCE_TEX_SYMBOL
) ;
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
) ;
273 sc
.SetState(SCE_TEX_TEXT
) ;
275 } else if (sc
.state
!= SCE_TEX_COMMAND
) {
276 sc
.SetState(SCE_TEX_TEXT
) ;
280 sc
.ChangeState(SCE_TEX_TEXT
) ;
286 static inline bool isNumber(int ch
) {
288 (ch
== '0') || (ch
== '1') || (ch
== '2') ||
289 (ch
== '3') || (ch
== '4') || (ch
== '5') ||
290 (ch
== '6') || (ch
== '7') || (ch
== '8') || (ch
== '9');
293 static inline bool isWordChar(int ch
) {
294 return ((ch
>= 'a') && (ch
<= 'z')) || ((ch
>= 'A') && (ch
<= 'Z'));
297 static int ParseTeXCommand(unsigned int pos
, Accessor
&styler
, char *command
)
300 char ch
=styler
.SafeGetCharAt(pos
+1);
302 if(ch
==',' || ch
==':' || ch
==';' || ch
=='%'){
309 while(isWordChar(ch
) && !isNumber(ch
) && ch
!='_' && ch
!='.' && length
<100){
312 ch
=styler
.SafeGetCharAt(pos
+length
+1);
315 command
[length
]='\0';
316 if(!length
) return 0;
320 static int classifyFoldPointTeXPaired(const char* s
) {
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
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||
339 static int classifyFoldPointTeXUnpaired(const char* s
) {
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
362 static bool IsTeXCommentLine(int line
, Accessor
&styler
) {
363 int pos
= styler
.LineStart(line
);
364 int eol_pos
= styler
.LineStart(line
+ 1) - 1;
368 while (startpos
<eol_pos
){
369 char ch
= styler
[startpos
];
370 if (ch
!='%' && ch
!=' ') return false;
371 else if (ch
=='%') return true;
378 // FoldTeXDoc: borrowed from VisualTeX with modifications
380 static void FoldTexDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
)
382 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
383 unsigned int endPos
= startPos
+length
;
385 int lineCurrent
=styler
.GetLine(startPos
);
386 int levelPrev
=styler
.LevelAt(lineCurrent
) & SC_FOLDLEVELNUMBERMASK
;
387 int levelCurrent
=levelPrev
;
388 char chNext
=styler
[startPos
];
391 for (unsigned int i
=startPos
; i
< endPos
; i
++) {
393 chNext
=styler
.SafeGetCharAt(i
+1);
394 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
397 ParseTeXCommand(i
, styler
, buffer
);
398 levelCurrent
+= classifyFoldPointTeXPaired(buffer
)+classifyFoldPointTeXUnpaired(buffer
);
401 if (levelCurrent
> SC_FOLDLEVELBASE
&& ((ch
== '\r' || ch
=='\n') && (chNext
== '\\'))) {
402 ParseTeXCommand(i
+1, styler
, buffer
);
403 levelCurrent
-= classifyFoldPointTeXUnpaired(buffer
);
410 chNext2
=styler
.SafeGetCharAt(i
+2);
411 chNext3
=styler
.SafeGetCharAt(i
+3);
412 chNext4
=styler
.SafeGetCharAt(i
+4);
413 chNext5
=styler
.SafeGetCharAt(i
+5);
415 bool atEOfold
= (ch
== '%') &&
416 (chNext
== '%') && (chNext2
=='}') &&
417 (chNext3
=='}')&& (chNext4
=='-')&& (chNext5
=='-');
419 bool atBOfold
= (ch
== '%') &&
420 (chNext
== '%') && (chNext2
=='-') &&
421 (chNext3
=='-')&& (chNext4
=='{')&& (chNext5
=='{');
431 if(ch
=='\\' && chNext
=='['){
435 if(ch
=='\\' && chNext
==']'){
439 bool foldComment
= styler
.GetPropertyInt("fold.comment") != 0;
441 if (foldComment
&& atEOL
&& IsTeXCommentLine(lineCurrent
, styler
))
443 if (lineCurrent
==0 && IsTeXCommentLine(lineCurrent
+ 1, styler
)
446 else if (lineCurrent
!=0 && !IsTeXCommentLine(lineCurrent
- 1, styler
)
447 && IsTeXCommentLine(lineCurrent
+ 1, styler
)
450 else if (lineCurrent
!=0 && IsTeXCommentLine(lineCurrent
- 1, styler
) &&
451 !IsTeXCommentLine(lineCurrent
+1, styler
))
455 //---------------------------------------------------------------------------------------------
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
);
467 levelPrev
= levelCurrent
;
471 if (!isspacechar(ch
))
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
);
483 static const char * const texWordListDesc
[] = {
484 "TeX, eTeX, pdfTeX, Omega",
494 LexerModule
lmTeX(SCLEX_TEX
, ColouriseTeXDoc
, "tex", FoldTexDoc
, texWordListDesc
);