Add an UI to enable/disable specific overlay handlers.
[TortoiseGit.git] / ext / scintilla / src / LexTeX.cxx
blob0aa2063aa12f146d9a12e5d915d4a5ce7bb5049e
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
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <stdio.h>
23 #include <stdarg.h>
25 #include "Platform.h"
27 #include "PropSet.h"
28 #include "Accessor.h"
29 #include "KeyWords.h"
30 #include "Scintilla.h"
31 #include "SciLexer.h"
32 #include "StyleContext.h"
34 #ifdef SCI_NAMESPACE
35 using namespace Scintilla;
36 #endif
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:
47 // TeX Highlighting
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
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) {
70 return
71 (styler[i] == '\n') || ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n')) ;
74 static inline bool isTeXzero(int ch) {
75 return
76 (ch == '%') ;
79 static inline bool isTeXone(int ch) {
80 return
81 (ch == '[') || (ch == ']') || (ch == '=') || (ch == '#') ||
82 (ch == '(') || (ch == ')') || (ch == '<') || (ch == '>') ||
83 (ch == '"') ;
86 static inline bool isTeXtwo(int ch) {
87 return
88 (ch == '{') || (ch == '}') || (ch == '$') ;
91 static inline bool isTeXthree(int ch) {
92 return
93 (ch == '~') || (ch == '^') || (ch == '_') || (ch == '&') ||
94 (ch == '-') || (ch == '+') || (ch == '\"') || (ch == '`') ||
95 (ch == '/') || (ch == '|') || (ch == '%') ;
98 static inline bool isTeXfour(int ch) {
99 return
100 (ch == '\\') ;
103 static inline bool isTeXfive(int ch) {
104 return
105 ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) ||
106 (ch == '@') || (ch == '!') || (ch == '?') ;
109 static inline bool isTeXsix(int ch) {
110 return
111 (ch == ' ') ;
114 static inline bool isTeXseven(int ch) {
115 return
116 (ch == '^') ;
119 // Interface determination
121 static int CheckTeXInterface(
122 unsigned int startPos,
123 int length,
124 Accessor &styler,
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")) {
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 ;
166 return defaultInterface ;
169 static void ColouriseTeXDoc(
170 unsigned int startPos,
171 int length,
172 int,
173 WordList *keywordlists[],
174 Accessor &styler) {
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) ;
184 char key[100] ;
185 int k ;
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
206 if (inComment) {
207 if (sc.atLineEnd) {
208 sc.SetState(SCE_TEX_TEXT) ;
209 newifDone = false ;
210 inComment = false ;
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>
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 ;
244 if (isTeXzero(sc.ch)) {
245 sc.SetState(SCE_TEX_SYMBOL) ;
246 sc.ForwardSetState(SCE_TEX_DEFAULT) ;
247 inComment = ! processComment ;
248 newifDone = false ;
249 } else if (isTeXseven(sc.ch) && isTeXseven(sc.chNext)) {
250 sc.SetState(SCE_TEX_TEXT) ;
251 sc.ForwardSetState(SCE_TEX_TEXT) ;
252 } else if (isTeXone(sc.ch)) {
253 sc.SetState(SCE_TEX_SPECIAL) ;
254 newifDone = false ;
255 } else if (isTeXtwo(sc.ch)) {
256 sc.SetState(SCE_TEX_GROUP) ;
257 newifDone = false ;
258 } else if (isTeXthree(sc.ch)) {
259 sc.SetState(SCE_TEX_SYMBOL) ;
260 newifDone = false ;
261 } else if (isTeXfour(sc.ch)) {
262 sc.SetState(SCE_TEX_COMMAND) ;
263 } else if (isTeXsix(sc.ch)) {
264 sc.SetState(SCE_TEX_TEXT) ;
265 } else if (sc.atLineEnd) {
266 sc.SetState(SCE_TEX_TEXT) ;
267 newifDone = false ;
268 inComment = false ;
269 } else {
270 sc.SetState(SCE_TEX_TEXT) ;
272 } else if (sc.state != SCE_TEX_COMMAND) {
273 sc.SetState(SCE_TEX_TEXT) ;
277 sc.ChangeState(SCE_TEX_TEXT) ;
278 sc.Complete();
283 static inline bool isNumber(int ch) {
284 return
285 (ch == '0') || (ch == '1') || (ch == '2') ||
286 (ch == '3') || (ch == '4') || (ch == '5') ||
287 (ch == '6') || (ch == '7') || (ch == '8') || (ch == '9');
290 static inline bool isWordChar(int ch) {
291 return ((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z'));
294 static int ParseTeXCommand(unsigned int pos, Accessor &styler, char *command)
296 int length=0;
297 char ch=styler.SafeGetCharAt(pos+1);
299 if(ch==',' || ch==':' || ch==';' || ch=='%'){
300 command[0]=ch;
301 command[1]=0;
302 return 1;
305 // find end
306 while(isWordChar(ch) && !isNumber(ch) && ch!='_' && ch!='.' && length<100){
307 command[length]=ch;
308 length++;
309 ch=styler.SafeGetCharAt(pos+length+1);
312 command[length]='\0';
313 if(!length) return 0;
314 return length+1;
317 static int classifyFoldPointTeXPaired(const char* s) {
318 int lev=0;
319 if (!(isdigit(s[0]) || (s[0] == '.'))){
320 if (strcmp(s, "begin")==0||strcmp(s,"FoldStart")==0||
321 strcmp(s,"abstract")==0||strcmp(s,"unprotect")==0||
322 strcmp(s,"title")==0||strncmp(s,"start",5)==0||strncmp(s,"Start",5)==0||
323 strcmp(s,"documentclass")==0||strncmp(s,"if",2)==0
325 lev=1;
326 if (strcmp(s, "end")==0||strcmp(s,"FoldStop")==0||
327 strcmp(s,"maketitle")==0||strcmp(s,"protect")==0||
328 strncmp(s,"stop",4)==0||strncmp(s,"Stop",4)==0||
329 strcmp(s,"fi")==0
331 lev=-1;
333 return lev;
336 static int classifyFoldPointTeXUnpaired(const char* s) {
337 int lev=0;
338 if (!(isdigit(s[0]) || (s[0] == '.'))){
339 if (strcmp(s,"part")==0||
340 strcmp(s,"chapter")==0||
341 strcmp(s,"section")==0||
342 strcmp(s,"subsection")==0||
343 strcmp(s,"subsubsection")==0||
344 strcmp(s,"CJKfamily")==0||
345 strcmp(s,"appendix")==0||
346 strcmp(s,"Topic")==0||strcmp(s,"topic")==0||
347 strcmp(s,"subject")==0||strcmp(s,"subsubject")==0||
348 strcmp(s,"def")==0||strcmp(s,"gdef")==0||strcmp(s,"edef")==0||
349 strcmp(s,"xdef")==0||strcmp(s,"framed")==0||
350 strcmp(s,"frame")==0||
351 strcmp(s,"foilhead")==0||strcmp(s,"overlays")==0||strcmp(s,"slide")==0
353 lev=1;
356 return lev;
359 static bool IsTeXCommentLine(int line, Accessor &styler) {
360 int pos = styler.LineStart(line);
361 int eol_pos = styler.LineStart(line + 1) - 1;
363 int startpos = pos;
365 while (startpos<eol_pos){
366 char ch = styler[startpos];
367 if (ch!='%' && ch!=' ') return false;
368 else if (ch=='%') return true;
369 startpos++;
372 return false;
375 // FoldTeXDoc: borrowed from VisualTeX with modifications
377 static void FoldTexDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler)
379 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
380 unsigned int endPos = startPos+length;
381 int visibleChars=0;
382 int lineCurrent=styler.GetLine(startPos);
383 int levelPrev=styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
384 int levelCurrent=levelPrev;
385 char chNext=styler[startPos];
386 char buffer[100]="";
388 for (unsigned int i=startPos; i < endPos; i++) {
389 char ch=chNext;
390 chNext=styler.SafeGetCharAt(i+1);
391 bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
393 if(ch=='\\') {
394 ParseTeXCommand(i, styler, buffer);
395 levelCurrent += classifyFoldPointTeXPaired(buffer)+classifyFoldPointTeXUnpaired(buffer);
398 if (levelCurrent > SC_FOLDLEVELBASE && ((ch == '\r' || ch=='\n') && (chNext == '\\'))) {
399 ParseTeXCommand(i+1, styler, buffer);
400 levelCurrent -= classifyFoldPointTeXUnpaired(buffer);
403 char chNext2;
404 char chNext3;
405 char chNext4;
406 char chNext5;
407 chNext2=styler.SafeGetCharAt(i+2);
408 chNext3=styler.SafeGetCharAt(i+3);
409 chNext4=styler.SafeGetCharAt(i+4);
410 chNext5=styler.SafeGetCharAt(i+5);
412 bool atEOfold = (ch == '%') &&
413 (chNext == '%') && (chNext2=='}') &&
414 (chNext3=='}')&& (chNext4=='-')&& (chNext5=='-');
416 bool atBOfold = (ch == '%') &&
417 (chNext == '%') && (chNext2=='-') &&
418 (chNext3=='-')&& (chNext4=='{')&& (chNext5=='{');
420 if(atBOfold){
421 levelCurrent+=1;
424 if(atEOfold){
425 levelCurrent-=1;
428 if(ch=='\\' && chNext=='['){
429 levelCurrent+=1;
432 if(ch=='\\' && chNext==']'){
433 levelCurrent-=1;
436 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
438 if (foldComment && atEOL && IsTeXCommentLine(lineCurrent, styler))
440 if (lineCurrent==0 && IsTeXCommentLine(lineCurrent + 1, styler)
442 levelCurrent++;
443 else if (lineCurrent!=0 && !IsTeXCommentLine(lineCurrent - 1, styler)
444 && IsTeXCommentLine(lineCurrent + 1, styler)
446 levelCurrent++;
447 else if (lineCurrent!=0 && IsTeXCommentLine(lineCurrent - 1, styler) &&
448 !IsTeXCommentLine(lineCurrent+1, styler))
449 levelCurrent--;
452 //---------------------------------------------------------------------------------------------
454 if (atEOL) {
455 int lev = levelPrev;
456 if (visibleChars == 0 && foldCompact)
457 lev |= SC_FOLDLEVELWHITEFLAG;
458 if ((levelCurrent > levelPrev) && (visibleChars > 0))
459 lev |= SC_FOLDLEVELHEADERFLAG;
460 if (lev != styler.LevelAt(lineCurrent)) {
461 styler.SetLevel(lineCurrent, lev);
463 lineCurrent++;
464 levelPrev = levelCurrent;
465 visibleChars = 0;
468 if (!isspacechar(ch))
469 visibleChars++;
472 // Fill in the real level of the next line, keeping the current flags as they will be filled in later
473 int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
474 styler.SetLevel(lineCurrent, levelPrev | flagsNext);
480 static const char * const texWordListDesc[] = {
481 "TeX, eTeX, pdfTeX, Omega",
482 "ConTeXt Dutch",
483 "ConTeXt English",
484 "ConTeXt German",
485 "ConTeXt Czech",
486 "ConTeXt Italian",
487 "ConTeXt Romanian",
491 LexerModule lmTeX(SCLEX_TEX, ColouriseTeXDoc, "tex", FoldTexDoc, texWordListDesc);