]>
git.saurik.com Git - wxWidgets.git/blob - src/stc/scintilla/src/LexOthers.cxx
1 // Scintilla source code edit control
2 /** @file LexOthers.cxx
3 ** Lexers for batch files, diff results, properties files, make files and error lists.
4 ** Also lexer for LaTeX documents.
6 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
7 // The License.txt file describes the conditions under which this software may be distributed.
20 #include "Scintilla.h"
24 using namespace Scintilla
;
27 static bool Is0To9(char ch
) {
28 return (ch
>= '0') && (ch
<= '9');
31 static bool Is1To9(char ch
) {
32 return (ch
>= '1') && (ch
<= '9');
35 static inline bool AtEOL(Accessor
&styler
, unsigned int i
) {
36 return (styler
[i
] == '\n') ||
37 ((styler
[i
] == '\r') && (styler
.SafeGetCharAt(i
+ 1) != '\n'));
40 // Tests for BATCH Operators
41 static bool IsBOperator(char ch
) {
42 return (ch
== '=') || (ch
== '+') || (ch
== '>') || (ch
== '<') ||
43 (ch
== '|') || (ch
== '?') || (ch
== '*');
46 // Tests for BATCH Separators
47 static bool IsBSeparator(char ch
) {
48 return (ch
== '\\') || (ch
== '.') || (ch
== ';') ||
49 (ch
== '\"') || (ch
== '\'') || (ch
== '/') || (ch
== ')');
52 static void ColouriseBatchLine(
54 unsigned int lengthLine
,
55 unsigned int startLine
,
57 WordList
*keywordlists
[],
60 unsigned int offset
= 0; // Line Buffer Offset
61 unsigned int enVarEnd
; // Environment Variable End point
62 unsigned int cmdLoc
; // External Command / Program Location
63 char wordBuffer
[81]; // Word Buffer - large to catch long paths
64 unsigned int wbl
; // Word Buffer Length
65 unsigned int wbo
; // Word Buffer Offset - also Special Keyword Buffer Length
66 WordList
&keywords
= *keywordlists
[0]; // Internal Commands
67 WordList
&keywords2
= *keywordlists
[1]; // External Commands (optional)
69 // CHOICE, ECHO, GOTO, PROMPT and SET have Default Text that may contain Regular Keywords
70 // Toggling Regular Keyword Checking off improves readability
71 // Other Regular Keywords and External Commands / Programs might also benefit from toggling
72 // Need a more robust algorithm to properly toggle Regular Keyword Checking
73 bool continueProcessing
= true; // Used to toggle Regular Keyword Checking
74 // Special Keywords are those that allow certain characters without whitespace after the command
75 // Examples are: cd. cd\ md. rd. dir| dir> echo: echo. path=
76 // Special Keyword Buffer used to determine if the first n characters is a Keyword
77 char sKeywordBuffer
[10]; // Special Keyword Buffer
78 bool sKeywordFound
; // Exit Special Keyword for-loop if found
80 // Skip initial spaces
81 while ((offset
< lengthLine
) && (isspacechar(lineBuffer
[offset
]))) {
84 // Colorize Default Text
85 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_DEFAULT
);
86 // Set External Command / Program Location
89 // Check for Fake Label (Comment) or Real Label - return if found
90 if (lineBuffer
[offset
] == ':') {
91 if (lineBuffer
[offset
+ 1] == ':') {
92 // Colorize Fake Label (Comment) - :: is similar to REM, see http://content.techweb.com/winmag/columns/explorer/2000/21.htm
93 styler
.ColourTo(endPos
, SCE_BAT_COMMENT
);
95 // Colorize Real Label
96 styler
.ColourTo(endPos
, SCE_BAT_LABEL
);
99 // Check for Drive Change (Drive Change is internal command) - return if found
100 } else if ((isalpha(lineBuffer
[offset
])) &&
101 (lineBuffer
[offset
+ 1] == ':') &&
102 ((isspacechar(lineBuffer
[offset
+ 2])) ||
103 (((lineBuffer
[offset
+ 2] == '\\')) &&
104 (isspacechar(lineBuffer
[offset
+ 3]))))) {
105 // Colorize Regular Keyword
106 styler
.ColourTo(endPos
, SCE_BAT_WORD
);
110 // Check for Hide Command (@ECHO OFF/ON)
111 if (lineBuffer
[offset
] == '@') {
112 styler
.ColourTo(startLine
+ offset
, SCE_BAT_HIDE
);
114 // Check for Argument (%n) or Environment Variable (%x...%)
115 } else if (lineBuffer
[offset
] == '%') {
116 enVarEnd
= offset
+ 1;
117 // Search end of word for second % (can be a long path)
118 while ((enVarEnd
< lengthLine
) &&
119 (!isspacechar(lineBuffer
[enVarEnd
])) &&
120 (lineBuffer
[enVarEnd
] != '%') &&
121 (!IsBOperator(lineBuffer
[enVarEnd
])) &&
122 (!IsBSeparator(lineBuffer
[enVarEnd
]))) {
125 // Check for Argument (%n)
126 if ((Is0To9(lineBuffer
[offset
+ 1])) &&
127 (lineBuffer
[enVarEnd
] != '%')) {
129 styler
.ColourTo(startLine
+ offset
+ 1, SCE_BAT_IDENTIFIER
);
131 // Check for External Command / Program
132 if (offset
< lengthLine
&& !isspacechar(lineBuffer
[offset
])) {
135 // Check for Environment Variable (%x...%)
136 } else if ((lineBuffer
[offset
+ 1] != '%') &&
137 (lineBuffer
[enVarEnd
] == '%')) {
139 // Colorize Environment Variable
140 styler
.ColourTo(startLine
+ offset
, SCE_BAT_IDENTIFIER
);
142 // Check for External Command / Program
143 if (offset
< lengthLine
&& !isspacechar(lineBuffer
[offset
])) {
149 while ((offset
< lengthLine
) && (isspacechar(lineBuffer
[offset
]))) {
153 // Read remainder of line word-at-a-time or remainder-of-word-at-a-time
154 while (offset
< lengthLine
) {
155 if (offset
> startLine
) {
156 // Colorize Default Text
157 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_DEFAULT
);
159 // Copy word from Line Buffer into Word Buffer
161 for (; offset
< lengthLine
&& wbl
< 80 &&
162 !isspacechar(lineBuffer
[offset
]); wbl
++, offset
++) {
163 wordBuffer
[wbl
] = static_cast<char>(tolower(lineBuffer
[offset
]));
165 wordBuffer
[wbl
] = '\0';
168 // Check for Comment - return if found
169 if (CompareCaseInsensitive(wordBuffer
, "rem") == 0) {
170 styler
.ColourTo(endPos
, SCE_BAT_COMMENT
);
173 // Check for Separator
174 if (IsBSeparator(wordBuffer
[0])) {
175 // Check for External Command / Program
176 if ((cmdLoc
== offset
- wbl
) &&
177 ((wordBuffer
[0] == ':') ||
178 (wordBuffer
[0] == '\\') ||
179 (wordBuffer
[0] == '.'))) {
180 // Reset Offset to re-process remainder of word
182 // Colorize External Command / Program
184 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_COMMAND
);
185 } else if (keywords2
.InList(wordBuffer
)) {
186 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_COMMAND
);
188 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_DEFAULT
);
190 // Reset External Command / Program Location
193 // Reset Offset to re-process remainder of word
195 // Colorize Default Text
196 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_DEFAULT
);
198 // Check for Regular Keyword in list
199 } else if ((keywords
.InList(wordBuffer
)) &&
200 (continueProcessing
)) {
201 // ECHO, GOTO, PROMPT and SET require no further Regular Keyword Checking
202 if ((CompareCaseInsensitive(wordBuffer
, "echo") == 0) ||
203 (CompareCaseInsensitive(wordBuffer
, "goto") == 0) ||
204 (CompareCaseInsensitive(wordBuffer
, "prompt") == 0) ||
205 (CompareCaseInsensitive(wordBuffer
, "set") == 0)) {
206 continueProcessing
= false;
208 // Identify External Command / Program Location for ERRORLEVEL, and EXIST
209 if ((CompareCaseInsensitive(wordBuffer
, "errorlevel") == 0) ||
210 (CompareCaseInsensitive(wordBuffer
, "exist") == 0)) {
211 // Reset External Command / Program Location
214 while ((cmdLoc
< lengthLine
) &&
215 (isspacechar(lineBuffer
[cmdLoc
]))) {
219 while ((cmdLoc
< lengthLine
) &&
220 (!isspacechar(lineBuffer
[cmdLoc
]))) {
224 while ((cmdLoc
< lengthLine
) &&
225 (isspacechar(lineBuffer
[cmdLoc
]))) {
228 // Identify External Command / Program Location for CALL, DO, LOADHIGH and LH
229 } else if ((CompareCaseInsensitive(wordBuffer
, "call") == 0) ||
230 (CompareCaseInsensitive(wordBuffer
, "do") == 0) ||
231 (CompareCaseInsensitive(wordBuffer
, "loadhigh") == 0) ||
232 (CompareCaseInsensitive(wordBuffer
, "lh") == 0)) {
233 // Reset External Command / Program Location
236 while ((cmdLoc
< lengthLine
) &&
237 (isspacechar(lineBuffer
[cmdLoc
]))) {
241 // Colorize Regular keyword
242 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_WORD
);
243 // No need to Reset Offset
244 // Check for Special Keyword in list, External Command / Program, or Default Text
245 } else if ((wordBuffer
[0] != '%') &&
246 (!IsBOperator(wordBuffer
[0])) &&
247 (continueProcessing
)) {
248 // Check for Special Keyword
249 // Affected Commands are in Length range 2-6
250 // Good that ERRORLEVEL, EXIST, CALL, DO, LOADHIGH, and LH are unaffected
251 sKeywordFound
= false;
252 for (unsigned int keywordLength
= 2; keywordLength
< wbl
&& keywordLength
< 7 && !sKeywordFound
; keywordLength
++) {
254 // Copy Keyword Length from Word Buffer into Special Keyword Buffer
255 for (; wbo
< keywordLength
; wbo
++) {
256 sKeywordBuffer
[wbo
] = static_cast<char>(wordBuffer
[wbo
]);
258 sKeywordBuffer
[wbo
] = '\0';
259 // Check for Special Keyword in list
260 if ((keywords
.InList(sKeywordBuffer
)) &&
261 ((IsBOperator(wordBuffer
[wbo
])) ||
262 (IsBSeparator(wordBuffer
[wbo
])))) {
263 sKeywordFound
= true;
264 // ECHO requires no further Regular Keyword Checking
265 if (CompareCaseInsensitive(sKeywordBuffer
, "echo") == 0) {
266 continueProcessing
= false;
268 // Colorize Special Keyword as Regular Keyword
269 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- wbo
), SCE_BAT_WORD
);
270 // Reset Offset to re-process remainder of word
271 offset
-= (wbl
- wbo
);
274 // Check for External Command / Program or Default Text
275 if (!sKeywordFound
) {
277 // Check for External Command / Program
278 if (cmdLoc
== offset
- wbl
) {
279 // Read up to %, Operator or Separator
280 while ((wbo
< wbl
) &&
281 (wordBuffer
[wbo
] != '%') &&
282 (!IsBOperator(wordBuffer
[wbo
])) &&
283 (!IsBSeparator(wordBuffer
[wbo
]))) {
286 // Reset External Command / Program Location
287 cmdLoc
= offset
- (wbl
- wbo
);
288 // Reset Offset to re-process remainder of word
289 offset
-= (wbl
- wbo
);
290 // CHOICE requires no further Regular Keyword Checking
291 if (CompareCaseInsensitive(wordBuffer
, "choice") == 0) {
292 continueProcessing
= false;
294 // Check for START (and its switches) - What follows is External Command \ Program
295 if (CompareCaseInsensitive(wordBuffer
, "start") == 0) {
296 // Reset External Command / Program Location
299 while ((cmdLoc
< lengthLine
) &&
300 (isspacechar(lineBuffer
[cmdLoc
]))) {
303 // Reset External Command / Program Location if command switch detected
304 if (lineBuffer
[cmdLoc
] == '/') {
305 // Skip command switch
306 while ((cmdLoc
< lengthLine
) &&
307 (!isspacechar(lineBuffer
[cmdLoc
]))) {
311 while ((cmdLoc
< lengthLine
) &&
312 (isspacechar(lineBuffer
[cmdLoc
]))) {
317 // Colorize External Command / Program
319 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_COMMAND
);
320 } else if (keywords2
.InList(wordBuffer
)) {
321 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_COMMAND
);
323 styler
.ColourTo(startLine
+ offset
- 1, SCE_BAT_DEFAULT
);
325 // No need to Reset Offset
326 // Check for Default Text
328 // Read up to %, Operator or Separator
329 while ((wbo
< wbl
) &&
330 (wordBuffer
[wbo
] != '%') &&
331 (!IsBOperator(wordBuffer
[wbo
])) &&
332 (!IsBSeparator(wordBuffer
[wbo
]))) {
335 // Colorize Default Text
336 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- wbo
), SCE_BAT_DEFAULT
);
337 // Reset Offset to re-process remainder of word
338 offset
-= (wbl
- wbo
);
341 // Check for Argument (%n), Environment Variable (%x...%) or Local Variable (%%a)
342 } else if (wordBuffer
[0] == '%') {
343 // Colorize Default Text
344 styler
.ColourTo(startLine
+ offset
- 1 - wbl
, SCE_BAT_DEFAULT
);
346 // Search to end of word for second % (can be a long path)
347 while ((wbo
< wbl
) &&
348 (wordBuffer
[wbo
] != '%') &&
349 (!IsBOperator(wordBuffer
[wbo
])) &&
350 (!IsBSeparator(wordBuffer
[wbo
]))) {
353 // Check for Argument (%n)
354 if ((Is0To9(wordBuffer
[1])) &&
355 (wordBuffer
[wbo
] != '%')) {
356 // Check for External Command / Program
357 if (cmdLoc
== offset
- wbl
) {
358 cmdLoc
= offset
- (wbl
- 2);
361 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- 2), SCE_BAT_IDENTIFIER
);
362 // Reset Offset to re-process remainder of word
364 // Check for Environment Variable (%x...%)
365 } else if ((wordBuffer
[1] != '%') &&
366 (wordBuffer
[wbo
] == '%')) {
368 // Check for External Command / Program
369 if (cmdLoc
== offset
- wbl
) {
370 cmdLoc
= offset
- (wbl
- wbo
);
372 // Colorize Environment Variable
373 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- wbo
), SCE_BAT_IDENTIFIER
);
374 // Reset Offset to re-process remainder of word
375 offset
-= (wbl
- wbo
);
376 // Check for Local Variable (%%a)
379 (wordBuffer
[1] == '%') &&
380 (wordBuffer
[2] != '%') &&
381 (!IsBOperator(wordBuffer
[2])) &&
382 (!IsBSeparator(wordBuffer
[2]))) {
383 // Check for External Command / Program
384 if (cmdLoc
== offset
- wbl
) {
385 cmdLoc
= offset
- (wbl
- 3);
387 // Colorize Local Variable
388 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- 3), SCE_BAT_IDENTIFIER
);
389 // Reset Offset to re-process remainder of word
392 // Check for Operator
393 } else if (IsBOperator(wordBuffer
[0])) {
394 // Colorize Default Text
395 styler
.ColourTo(startLine
+ offset
- 1 - wbl
, SCE_BAT_DEFAULT
);
396 // Check for Comparison Operator
397 if ((wordBuffer
[0] == '=') && (wordBuffer
[1] == '=')) {
398 // Identify External Command / Program Location for IF
401 while ((cmdLoc
< lengthLine
) &&
402 (isspacechar(lineBuffer
[cmdLoc
]))) {
405 // Colorize Comparison Operator
406 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- 2), SCE_BAT_OPERATOR
);
407 // Reset Offset to re-process remainder of word
409 // Check for Pipe Operator
410 } else if (wordBuffer
[0] == '|') {
411 // Reset External Command / Program Location
412 cmdLoc
= offset
- wbl
+ 1;
414 while ((cmdLoc
< lengthLine
) &&
415 (isspacechar(lineBuffer
[cmdLoc
]))) {
418 // Colorize Pipe Operator
419 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- 1), SCE_BAT_OPERATOR
);
420 // Reset Offset to re-process remainder of word
422 // Check for Other Operator
424 // Check for > Operator
425 if (wordBuffer
[0] == '>') {
426 // Turn Keyword and External Command / Program checking back on
427 continueProcessing
= true;
429 // Colorize Other Operator
430 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- 1), SCE_BAT_OPERATOR
);
431 // Reset Offset to re-process remainder of word
434 // Check for Default Text
436 // Read up to %, Operator or Separator
437 while ((wbo
< wbl
) &&
438 (wordBuffer
[wbo
] != '%') &&
439 (!IsBOperator(wordBuffer
[wbo
])) &&
440 (!IsBSeparator(wordBuffer
[wbo
]))) {
443 // Colorize Default Text
444 styler
.ColourTo(startLine
+ offset
- 1 - (wbl
- wbo
), SCE_BAT_DEFAULT
);
445 // Reset Offset to re-process remainder of word
446 offset
-= (wbl
- wbo
);
448 // Skip next spaces - nothing happens if Offset was Reset
449 while ((offset
< lengthLine
) && (isspacechar(lineBuffer
[offset
]))) {
453 // Colorize Default Text for remainder of line - currently not lexed
454 styler
.ColourTo(endPos
, SCE_BAT_DEFAULT
);
457 static void ColouriseBatchDoc(
458 unsigned int startPos
,
461 WordList
*keywordlists
[],
464 char lineBuffer
[1024];
466 styler
.StartAt(startPos
);
467 styler
.StartSegment(startPos
);
468 unsigned int linePos
= 0;
469 unsigned int startLine
= startPos
;
470 for (unsigned int i
= startPos
; i
< startPos
+ length
; i
++) {
471 lineBuffer
[linePos
++] = styler
[i
];
472 if (AtEOL(styler
, i
) || (linePos
>= sizeof(lineBuffer
) - 1)) {
473 // End of line (or of line buffer) met, colourise it
474 lineBuffer
[linePos
] = '\0';
475 ColouriseBatchLine(lineBuffer
, linePos
, startLine
, i
, keywordlists
, styler
);
480 if (linePos
> 0) { // Last line does not have ending characters
481 lineBuffer
[linePos
] = '\0';
482 ColouriseBatchLine(lineBuffer
, linePos
, startLine
, startPos
+ length
- 1,
483 keywordlists
, styler
);
487 static void ColouriseDiffLine(char *lineBuffer
, int endLine
, Accessor
&styler
) {
488 // It is needed to remember the current state to recognize starting
489 // comment lines before the first "diff " or "--- ". If a real
490 // difference starts then each line starting with ' ' is a whitespace
491 // otherwise it is considered a comment (Only in..., Binary file...)
492 if (0 == strncmp(lineBuffer
, "diff ", 5)) {
493 styler
.ColourTo(endLine
, SCE_DIFF_COMMAND
);
494 } else if (0 == strncmp(lineBuffer
, "--- ", 4)) {
495 // In a context diff, --- appears in both the header and the position markers
496 if (atoi(lineBuffer
+4) && !strchr(lineBuffer
, '/'))
497 styler
.ColourTo(endLine
, SCE_DIFF_POSITION
);
499 styler
.ColourTo(endLine
, SCE_DIFF_HEADER
);
500 } else if (0 == strncmp(lineBuffer
, "+++ ", 4)) {
501 // I don't know of any diff where "+++ " is a position marker, but for
502 // consistency, do the same as with "--- " and "*** ".
503 if (atoi(lineBuffer
+4) && !strchr(lineBuffer
, '/'))
504 styler
.ColourTo(endLine
, SCE_DIFF_POSITION
);
506 styler
.ColourTo(endLine
, SCE_DIFF_HEADER
);
507 } else if (0 == strncmp(lineBuffer
, "====", 4)) { // For p4's diff
508 styler
.ColourTo(endLine
, SCE_DIFF_HEADER
);
509 } else if (0 == strncmp(lineBuffer
, "***", 3)) {
510 // In a context diff, *** appears in both the header and the position markers.
511 // Also ******** is a chunk header, but here it's treated as part of the
512 // position marker since there is no separate style for a chunk header.
513 if (lineBuffer
[3] == ' ' && atoi(lineBuffer
+4) && !strchr(lineBuffer
, '/'))
514 styler
.ColourTo(endLine
, SCE_DIFF_POSITION
);
515 else if (lineBuffer
[3] == '*')
516 styler
.ColourTo(endLine
, SCE_DIFF_POSITION
);
518 styler
.ColourTo(endLine
, SCE_DIFF_HEADER
);
519 } else if (0 == strncmp(lineBuffer
, "? ", 2)) { // For difflib
520 styler
.ColourTo(endLine
, SCE_DIFF_HEADER
);
521 } else if (lineBuffer
[0] == '@') {
522 styler
.ColourTo(endLine
, SCE_DIFF_POSITION
);
523 } else if (lineBuffer
[0] >= '0' && lineBuffer
[0] <= '9') {
524 styler
.ColourTo(endLine
, SCE_DIFF_POSITION
);
525 } else if (lineBuffer
[0] == '-' || lineBuffer
[0] == '<') {
526 styler
.ColourTo(endLine
, SCE_DIFF_DELETED
);
527 } else if (lineBuffer
[0] == '+' || lineBuffer
[0] == '>') {
528 styler
.ColourTo(endLine
, SCE_DIFF_ADDED
);
529 } else if (lineBuffer
[0] != ' ') {
530 styler
.ColourTo(endLine
, SCE_DIFF_COMMENT
);
532 styler
.ColourTo(endLine
, SCE_DIFF_DEFAULT
);
536 static void ColouriseDiffDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
) {
537 char lineBuffer
[1024];
538 styler
.StartAt(startPos
);
539 styler
.StartSegment(startPos
);
540 unsigned int linePos
= 0;
541 for (unsigned int i
= startPos
; i
< startPos
+ length
; i
++) {
542 lineBuffer
[linePos
++] = styler
[i
];
543 if (AtEOL(styler
, i
) || (linePos
>= sizeof(lineBuffer
) - 1)) {
544 // End of line (or of line buffer) met, colourise it
545 lineBuffer
[linePos
] = '\0';
546 ColouriseDiffLine(lineBuffer
, i
, styler
);
550 if (linePos
> 0) { // Last line does not have ending characters
551 ColouriseDiffLine(lineBuffer
, startPos
+ length
- 1, styler
);
555 static void FoldDiffDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
) {
556 int curLine
= styler
.GetLine(startPos
);
557 int prevLevel
= SC_FOLDLEVELBASE
;
559 prevLevel
= styler
.LevelAt(curLine
-1);
561 int curLineStart
= styler
.LineStart(curLine
);
563 int nextLevel
= prevLevel
;
564 if (prevLevel
& SC_FOLDLEVELHEADERFLAG
)
565 nextLevel
= (prevLevel
& SC_FOLDLEVELNUMBERMASK
) + 1;
567 int lineType
= styler
.StyleAt(curLineStart
);
568 if (lineType
== SCE_DIFF_COMMAND
)
569 nextLevel
= (SC_FOLDLEVELBASE
+ 1) | SC_FOLDLEVELHEADERFLAG
;
570 else if (lineType
== SCE_DIFF_HEADER
) {
571 nextLevel
= (SC_FOLDLEVELBASE
+ 2) | SC_FOLDLEVELHEADERFLAG
;
572 } else if (lineType
== SCE_DIFF_POSITION
)
573 nextLevel
= (SC_FOLDLEVELBASE
+ 3) | SC_FOLDLEVELHEADERFLAG
;
575 if ((nextLevel
& SC_FOLDLEVELHEADERFLAG
) && (nextLevel
== prevLevel
))
576 styler
.SetLevel(curLine
-1, prevLevel
& ~SC_FOLDLEVELHEADERFLAG
);
578 styler
.SetLevel(curLine
, nextLevel
);
579 prevLevel
= nextLevel
;
581 curLineStart
= styler
.LineStart(++curLine
);
582 } while (static_cast<int>(startPos
) + length
> curLineStart
);
586 static void ColourisePropsLine(
588 unsigned int lengthLine
,
589 unsigned int startLine
,
594 while ((i
< lengthLine
) && isspacechar(lineBuffer
[i
])) // Skip initial spaces
596 if (i
< lengthLine
) {
597 if (lineBuffer
[i
] == '#' || lineBuffer
[i
] == '!' || lineBuffer
[i
] == ';') {
598 styler
.ColourTo(endPos
, SCE_PROPS_COMMENT
);
599 } else if (lineBuffer
[i
] == '[') {
600 styler
.ColourTo(endPos
, SCE_PROPS_SECTION
);
601 } else if (lineBuffer
[i
] == '@') {
602 styler
.ColourTo(startLine
+ i
, SCE_PROPS_DEFVAL
);
603 if (lineBuffer
[++i
] == '=')
604 styler
.ColourTo(startLine
+ i
, SCE_PROPS_ASSIGNMENT
);
605 styler
.ColourTo(endPos
, SCE_PROPS_DEFAULT
);
607 // Search for the '=' character
608 while ((i
< lengthLine
) && (lineBuffer
[i
] != '='))
610 if ((i
< lengthLine
) && (lineBuffer
[i
] == '=')) {
611 styler
.ColourTo(startLine
+ i
- 1, SCE_PROPS_KEY
);
612 styler
.ColourTo(startLine
+ i
, SCE_PROPS_ASSIGNMENT
);
613 styler
.ColourTo(endPos
, SCE_PROPS_DEFAULT
);
615 styler
.ColourTo(endPos
, SCE_PROPS_DEFAULT
);
619 styler
.ColourTo(endPos
, SCE_PROPS_DEFAULT
);
623 static void ColourisePropsDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
) {
624 char lineBuffer
[1024];
625 styler
.StartAt(startPos
);
626 styler
.StartSegment(startPos
);
627 unsigned int linePos
= 0;
628 unsigned int startLine
= startPos
;
629 for (unsigned int i
= startPos
; i
< startPos
+ length
; i
++) {
630 lineBuffer
[linePos
++] = styler
[i
];
631 if (AtEOL(styler
, i
) || (linePos
>= sizeof(lineBuffer
) - 1)) {
632 // End of line (or of line buffer) met, colourise it
633 lineBuffer
[linePos
] = '\0';
634 ColourisePropsLine(lineBuffer
, linePos
, startLine
, i
, styler
);
639 if (linePos
> 0) { // Last line does not have ending characters
640 ColourisePropsLine(lineBuffer
, linePos
, startLine
, startPos
+ length
- 1, styler
);
644 // adaption by ksc, using the "} else {" trick of 1.53
646 static void FoldPropsDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
) {
647 bool foldCompact
= styler
.GetPropertyInt("fold.compact", 1) != 0;
649 unsigned int endPos
= startPos
+ length
;
650 int visibleChars
= 0;
651 int lineCurrent
= styler
.GetLine(startPos
);
653 char chNext
= styler
[startPos
];
654 int styleNext
= styler
.StyleAt(startPos
);
655 bool headerPoint
= false;
658 for (unsigned int i
= startPos
; i
< endPos
; i
++) {
660 chNext
= styler
[i
+1];
662 int style
= styleNext
;
663 styleNext
= styler
.StyleAt(i
+ 1);
664 bool atEOL
= (ch
== '\r' && chNext
!= '\n') || (ch
== '\n');
666 if (style
== SCE_PROPS_SECTION
) {
671 lev
= SC_FOLDLEVELBASE
;
673 if (lineCurrent
> 0) {
674 int levelPrevious
= styler
.LevelAt(lineCurrent
- 1);
676 if (levelPrevious
& SC_FOLDLEVELHEADERFLAG
) {
677 lev
= SC_FOLDLEVELBASE
+ 1;
679 lev
= levelPrevious
& SC_FOLDLEVELNUMBERMASK
;
684 lev
= SC_FOLDLEVELBASE
;
686 if (visibleChars
== 0 && foldCompact
)
687 lev
|= SC_FOLDLEVELWHITEFLAG
;
690 lev
|= SC_FOLDLEVELHEADERFLAG
;
692 if (lev
!= styler
.LevelAt(lineCurrent
)) {
693 styler
.SetLevel(lineCurrent
, lev
);
700 if (!isspacechar(ch
))
704 if (lineCurrent
> 0) {
705 int levelPrevious
= styler
.LevelAt(lineCurrent
- 1);
706 if (levelPrevious
& SC_FOLDLEVELHEADERFLAG
) {
707 lev
= SC_FOLDLEVELBASE
+ 1;
709 lev
= levelPrevious
& SC_FOLDLEVELNUMBERMASK
;
712 lev
= SC_FOLDLEVELBASE
;
714 int flagsNext
= styler
.LevelAt(lineCurrent
);
715 styler
.SetLevel(lineCurrent
, lev
| flagsNext
& ~SC_FOLDLEVELNUMBERMASK
);
718 static void ColouriseMakeLine(
720 unsigned int lengthLine
,
721 unsigned int startLine
,
726 int lastNonSpace
= -1;
727 unsigned int state
= SCE_MAKE_DEFAULT
;
728 bool bSpecial
= false;
730 // check for a tab character in column 0 indicating a command
731 bool bCommand
= false;
732 if ((lengthLine
> 0) && (lineBuffer
[0] == '\t'))
735 // Skip initial spaces
736 while ((i
< lengthLine
) && isspacechar(lineBuffer
[i
])) {
739 if (lineBuffer
[i
] == '#') { // Comment
740 styler
.ColourTo(endPos
, SCE_MAKE_COMMENT
);
743 if (lineBuffer
[i
] == '!') { // Special directive
744 styler
.ColourTo(endPos
, SCE_MAKE_PREPROCESSOR
);
747 while (i
< lengthLine
) {
748 if (lineBuffer
[i
] == '$' && lineBuffer
[i
+ 1] == '(') {
749 styler
.ColourTo(startLine
+ i
- 1, state
);
750 state
= SCE_MAKE_IDENTIFIER
;
751 } else if (state
== SCE_MAKE_IDENTIFIER
&& lineBuffer
[i
] == ')') {
752 styler
.ColourTo(startLine
+ i
, state
);
753 state
= SCE_MAKE_DEFAULT
;
756 // skip identifier and target styling if this is a command line
757 if (!bSpecial
&& !bCommand
) {
758 if (lineBuffer
[i
] == ':') {
759 if (((i
+ 1) < lengthLine
) && (lineBuffer
[i
+ 1] == '=')) {
760 // it's a ':=', so style as an identifier
761 if (lastNonSpace
>= 0)
762 styler
.ColourTo(startLine
+ lastNonSpace
, SCE_MAKE_IDENTIFIER
);
763 styler
.ColourTo(startLine
+ i
- 1, SCE_MAKE_DEFAULT
);
764 styler
.ColourTo(startLine
+ i
+ 1, SCE_MAKE_OPERATOR
);
766 // We should check that no colouring was made since the beginning of the line,
767 // to avoid colouring stuff like /OUT:file
768 if (lastNonSpace
>= 0)
769 styler
.ColourTo(startLine
+ lastNonSpace
, SCE_MAKE_TARGET
);
770 styler
.ColourTo(startLine
+ i
- 1, SCE_MAKE_DEFAULT
);
771 styler
.ColourTo(startLine
+ i
, SCE_MAKE_OPERATOR
);
773 bSpecial
= true; // Only react to the first ':' of the line
774 state
= SCE_MAKE_DEFAULT
;
775 } else if (lineBuffer
[i
] == '=') {
776 if (lastNonSpace
>= 0)
777 styler
.ColourTo(startLine
+ lastNonSpace
, SCE_MAKE_IDENTIFIER
);
778 styler
.ColourTo(startLine
+ i
- 1, SCE_MAKE_DEFAULT
);
779 styler
.ColourTo(startLine
+ i
, SCE_MAKE_OPERATOR
);
780 bSpecial
= true; // Only react to the first '=' of the line
781 state
= SCE_MAKE_DEFAULT
;
784 if (!isspacechar(lineBuffer
[i
])) {
789 if (state
== SCE_MAKE_IDENTIFIER
) {
790 styler
.ColourTo(endPos
, SCE_MAKE_IDEOL
); // Error, variable reference not ended
792 styler
.ColourTo(endPos
, SCE_MAKE_DEFAULT
);
796 static void ColouriseMakeDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
) {
797 char lineBuffer
[1024];
798 styler
.StartAt(startPos
);
799 styler
.StartSegment(startPos
);
800 unsigned int linePos
= 0;
801 unsigned int startLine
= startPos
;
802 for (unsigned int i
= startPos
; i
< startPos
+ length
; i
++) {
803 lineBuffer
[linePos
++] = styler
[i
];
804 if (AtEOL(styler
, i
) || (linePos
>= sizeof(lineBuffer
) - 1)) {
805 // End of line (or of line buffer) met, colourise it
806 lineBuffer
[linePos
] = '\0';
807 ColouriseMakeLine(lineBuffer
, linePos
, startLine
, i
, styler
);
812 if (linePos
> 0) { // Last line does not have ending characters
813 ColouriseMakeLine(lineBuffer
, linePos
, startLine
, startPos
+ length
- 1, styler
);
817 static bool strstart(const char *haystack
, const char *needle
) {
818 return strncmp(haystack
, needle
, strlen(needle
)) == 0;
821 static int RecogniseErrorListLine(const char *lineBuffer
, unsigned int lengthLine
, int &startValue
) {
822 if (lineBuffer
[0] == '>') {
823 // Command or return status
825 } else if (lineBuffer
[0] == '<') {
826 // Diff removal, but not interested. Trapped to avoid hitting CTAG cases.
827 return SCE_ERR_DEFAULT
;
828 } else if (lineBuffer
[0] == '!') {
829 return SCE_ERR_DIFF_CHANGED
;
830 } else if (lineBuffer
[0] == '+') {
831 if (strstart(lineBuffer
, "+++ ")) {
832 return SCE_ERR_DIFF_MESSAGE
;
834 return SCE_ERR_DIFF_ADDITION
;
836 } else if (lineBuffer
[0] == '-') {
837 if (strstart(lineBuffer
, "--- ")) {
838 return SCE_ERR_DIFF_MESSAGE
;
840 return SCE_ERR_DIFF_DELETION
;
842 } else if (strstart(lineBuffer
, "cf90-")) {
843 // Absoft Pro Fortran 90/95 v8.2 error and/or warning message
845 } else if (strstart(lineBuffer
, "fortcom:")) {
846 // Intel Fortran Compiler v8.0 error/warning message
847 return SCE_ERR_IFORT
;
848 } else if (strstr(lineBuffer
, "File \"") && strstr(lineBuffer
, ", line ")) {
849 return SCE_ERR_PYTHON
;
850 } else if (strstr(lineBuffer
, " in ") && strstr(lineBuffer
, " on line ")) {
852 } else if ((strstart(lineBuffer
, "Error ") ||
853 strstart(lineBuffer
, "Warning ")) &&
854 strstr(lineBuffer
, " at (") &&
855 strstr(lineBuffer
, ") : ") &&
856 (strstr(lineBuffer
, " at (") < strstr(lineBuffer
, ") : "))) {
857 // Intel Fortran Compiler error/warning message
859 } else if (strstart(lineBuffer
, "Error ")) {
860 // Borland error message
861 return SCE_ERR_BORLAND
;
862 } else if (strstart(lineBuffer
, "Warning ")) {
863 // Borland warning message
864 return SCE_ERR_BORLAND
;
865 } else if (strstr(lineBuffer
, "at line " ) &&
866 (strstr(lineBuffer
, "at line " ) < (lineBuffer
+ lengthLine
)) &&
867 strstr(lineBuffer
, "file ") &&
868 (strstr(lineBuffer
, "file ") < (lineBuffer
+ lengthLine
))) {
869 // Lua 4 error message
871 } else if (strstr(lineBuffer
, " at " ) &&
872 (strstr(lineBuffer
, " at " ) < (lineBuffer
+ lengthLine
)) &&
873 strstr(lineBuffer
, " line ") &&
874 (strstr(lineBuffer
, " line ") < (lineBuffer
+ lengthLine
)) &&
875 (strstr(lineBuffer
, " at " ) < (strstr(lineBuffer
, " line ")))) {
876 // perl error message
878 } else if ((memcmp(lineBuffer
, " at ", 6) == 0) &&
879 strstr(lineBuffer
, ":line ")) {
882 } else if (strstart(lineBuffer
, "Line ") &&
883 strstr(lineBuffer
, ", file ")) {
884 // Essential Lahey Fortran error message
886 } else if (strstart(lineBuffer
, "line ") &&
887 strstr(lineBuffer
, " column ")) {
888 // HTML tidy style: line 42 column 1
890 } else if (strstart(lineBuffer
, "\tat ") &&
891 strstr(lineBuffer
, "(") &&
892 strstr(lineBuffer
, ".java:")) {
893 // Java stack back trace
894 return SCE_ERR_JAVA_STACK
;
896 // Look for one of the following formats:
897 // GCC: <filename>:<line>:<message>
898 // Microsoft: <filename>(<line>) :<message>
899 // Common: <filename>(<line>): warning|error|note|remark|catastrophic|fatal
900 // Common: <filename>(<line>) warning|error|note|remark|catastrophic|fatal
901 // Microsoft: <filename>(<line>,<column>)<message>
902 // CTags: \t<message>
903 // Lua 5 traceback: \t<filename>:<line>:<message>
904 // Lua 5.1: <exe>: <filename>:<line>:<message>
905 bool initialTab
= (lineBuffer
[0] == '\t');
906 bool initialColonPart
= false;
908 stGccStart
, stGccDigit
, stGcc
,
909 stMsStart
, stMsDigit
, stMsBracket
, stMsVc
, stMsDigitComma
, stMsDotNet
,
910 stCtagsStart
, stCtagsStartString
, stCtagsStringDollar
, stCtags
,
913 for (unsigned int i
= 0; i
< lengthLine
; i
++) {
914 char ch
= lineBuffer
[i
];
916 if ((i
+ 1) < lengthLine
)
917 chNext
= lineBuffer
[i
+ 1];
918 if (state
== stInitial
) {
920 // May be GCC, or might be Lua 5 (Lua traceback same but with tab prefix)
921 if ((chNext
!= '\\') && (chNext
!= '/') && (chNext
!= ' ')) {
922 // This check is not completely accurate as may be on
923 // GTK+ with a file name that includes ':'.
925 } else if (chNext
== ' ') { // indicates a Lua 5.1 error message
926 initialColonPart
= true;
928 } else if ((ch
== '(') && Is1To9(chNext
) && (!initialTab
)) {
930 // Check against '0' often removes phone numbers
932 } else if ((ch
== '\t') && (!initialTab
)) {
934 state
= stCtagsStart
;
936 } else if (state
== stGccStart
) { // <filename>:
937 state
= Is1To9(ch
) ? stGccDigit
: stUnrecognized
;
938 } else if (state
== stGccDigit
) { // <filename>:<line>
940 state
= stGcc
; // :9.*: is GCC
943 } else if (!Is0To9(ch
)) {
944 state
= stUnrecognized
;
946 } else if (state
== stMsStart
) { // <filename>(
947 state
= Is0To9(ch
) ? stMsDigit
: stUnrecognized
;
948 } else if (state
== stMsDigit
) { // <filename>(<line>
950 state
= stMsDigitComma
;
951 } else if (ch
== ')') {
953 } else if ((ch
!= ' ') && !Is0To9(ch
)) {
954 state
= stUnrecognized
;
956 } else if (state
== stMsBracket
) { // <filename>(<line>)
957 if ((ch
== ' ') && (chNext
== ':')) {
959 } else if ((ch
== ':' && chNext
== ' ') || (ch
== ' ')) {
960 // Possibly Delphi.. don't test against chNext as it's one of the strings below.
962 unsigned int j
, chPos
;
966 numstep
= 1; // ch was ' ', handle as if it's a delphi errorline, only add 1 to i.
968 numstep
= 2; // otherwise add 2.
969 for (j
= i
+ numstep
; j
< lengthLine
&& isalpha(lineBuffer
[j
]) && chPos
< sizeof(word
) - 1; j
++)
970 word
[chPos
++] = lineBuffer
[j
];
972 if (!CompareCaseInsensitive(word
, "error") || !CompareCaseInsensitive(word
, "warning") ||
973 !CompareCaseInsensitive(word
, "fatal") || !CompareCaseInsensitive(word
, "catastrophic") ||
974 !CompareCaseInsensitive(word
, "note") || !CompareCaseInsensitive(word
, "remark")) {
977 state
= stUnrecognized
;
979 state
= stUnrecognized
;
981 } else if (state
== stMsDigitComma
) { // <filename>(<line>,
985 } else if ((ch
!= ' ') && !Is0To9(ch
)) {
986 state
= stUnrecognized
;
988 } else if (state
== stCtagsStart
) {
989 if ((lineBuffer
[i
- 1] == '\t') &&
990 ((ch
== '/' && lineBuffer
[i
+ 1] == '^') || Is0To9(ch
))) {
993 } else if ((ch
== '/') && (lineBuffer
[i
+ 1] == '^')) {
994 state
= stCtagsStartString
;
996 } else if ((state
== stCtagsStartString
) && ((lineBuffer
[i
] == '$') && (lineBuffer
[i
+ 1] == '/'))) {
997 state
= stCtagsStringDollar
;
1001 if (state
== stGcc
) {
1002 return initialColonPart
? SCE_ERR_LUA
: SCE_ERR_GCC
;
1003 } else if ((state
== stMsVc
) || (state
== stMsDotNet
)) {
1005 } else if ((state
== stCtagsStringDollar
) || (state
== stCtags
)) {
1006 return SCE_ERR_CTAG
;
1008 return SCE_ERR_DEFAULT
;
1013 static void ColouriseErrorListLine(
1015 unsigned int lengthLine
,
1016 unsigned int endPos
,
1018 bool valueSeparate
) {
1019 int startValue
= -1;
1020 int style
= RecogniseErrorListLine(lineBuffer
, lengthLine
, startValue
);
1021 if (valueSeparate
&& (startValue
>= 0)) {
1022 styler
.ColourTo(endPos
- (lengthLine
- startValue
), style
);
1023 styler
.ColourTo(endPos
, SCE_ERR_VALUE
);
1025 styler
.ColourTo(endPos
, style
);
1029 static void ColouriseErrorListDoc(unsigned int startPos
, int length
, int, WordList
*[], Accessor
&styler
) {
1030 char lineBuffer
[10000];
1031 styler
.StartAt(startPos
);
1032 styler
.StartSegment(startPos
);
1033 unsigned int linePos
= 0;
1034 bool valueSeparate
= styler
.GetPropertyInt("lexer.errorlist.value.separate", 0) != 0;
1035 for (unsigned int i
= startPos
; i
< startPos
+ length
; i
++) {
1036 lineBuffer
[linePos
++] = styler
[i
];
1037 if (AtEOL(styler
, i
) || (linePos
>= sizeof(lineBuffer
) - 1)) {
1038 // End of line (or of line buffer) met, colourise it
1039 lineBuffer
[linePos
] = '\0';
1040 ColouriseErrorListLine(lineBuffer
, linePos
, i
, styler
, valueSeparate
);
1044 if (linePos
> 0) { // Last line does not have ending characters
1045 ColouriseErrorListLine(lineBuffer
, linePos
, startPos
+ length
- 1, styler
, valueSeparate
);
1049 static int isSpecial(char s
) {
1050 return (s
== '\\') || (s
== ',') || (s
== ';') || (s
== '\'') || (s
== ' ') ||
1051 (s
== '\"') || (s
== '`') || (s
== '^') || (s
== '~');
1054 static int isTag(int start
, Accessor
&styler
) {
1056 unsigned int i
= 0, e
= 1;
1057 while (i
< 5 && e
) {
1058 s
[i
] = styler
[start
+ i
];
1060 e
= styler
[start
+ i
] != '{';
1063 return (strcmp(s
, "begin") == 0) || (strcmp(s
, "end") == 0);
1066 static void ColouriseLatexDoc(unsigned int startPos
, int length
, int initStyle
,
1067 WordList
*[], Accessor
&styler
) {
1069 styler
.StartAt(startPos
);
1071 int state
= initStyle
;
1072 char chNext
= styler
[startPos
];
1073 styler
.StartSegment(startPos
);
1074 int lengthDoc
= startPos
+ length
;
1076 for (int i
= startPos
; i
< lengthDoc
; i
++) {
1078 chNext
= styler
.SafeGetCharAt(i
+ 1);
1080 if (styler
.IsLeadByte(ch
)) {
1081 chNext
= styler
.SafeGetCharAt(i
+ 2);
1086 case SCE_L_DEFAULT
:
1089 styler
.ColourTo(i
- 1, state
);
1090 if (isSpecial(styler
[i
+ 1])) {
1091 styler
.ColourTo(i
+ 1, SCE_L_COMMAND
);
1093 chNext
= styler
.SafeGetCharAt(i
+ 1);
1095 if (isTag(i
+ 1, styler
))
1098 state
= SCE_L_COMMAND
;
1102 styler
.ColourTo(i
- 1, state
);
1104 if (chNext
== '$') {
1106 chNext
= styler
.SafeGetCharAt(i
+ 1);
1110 styler
.ColourTo(i
- 1, state
);
1111 state
= SCE_L_COMMENT
;
1115 case SCE_L_COMMAND
:
1116 if (chNext
== '[' || chNext
== '{' || chNext
== '}' ||
1117 chNext
== ' ' || chNext
== '\r' || chNext
== '\n') {
1118 styler
.ColourTo(i
, state
);
1119 state
= SCE_L_DEFAULT
;
1121 chNext
= styler
.SafeGetCharAt(i
+ 1);
1126 styler
.ColourTo(i
, state
);
1127 state
= SCE_L_DEFAULT
;
1132 if (chNext
== '$') {
1134 chNext
= styler
.SafeGetCharAt(i
+ 1);
1136 styler
.ColourTo(i
, state
);
1137 state
= SCE_L_DEFAULT
;
1140 case SCE_L_COMMENT
:
1141 if (ch
== '\r' || ch
== '\n') {
1142 styler
.ColourTo(i
- 1, state
);
1143 state
= SCE_L_DEFAULT
;
1147 styler
.ColourTo(lengthDoc
-1, state
);
1150 static const char * const batchWordListDesc
[] = {
1151 "Internal Commands",
1152 "External Commands",
1156 static const char * const emptyWordListDesc
[] = {
1160 static void ColouriseNullDoc(unsigned int startPos
, int length
, int, WordList
*[],
1162 // Null language means all style bytes are 0 so just mark the end - no need to fill in.
1164 styler
.StartAt(startPos
+ length
- 1);
1165 styler
.StartSegment(startPos
+ length
- 1);
1166 styler
.ColourTo(startPos
+ length
- 1, 0);
1170 LexerModule
lmBatch(SCLEX_BATCH
, ColouriseBatchDoc
, "batch", 0, batchWordListDesc
);
1171 LexerModule
lmDiff(SCLEX_DIFF
, ColouriseDiffDoc
, "diff", FoldDiffDoc
, emptyWordListDesc
);
1172 LexerModule
lmProps(SCLEX_PROPERTIES
, ColourisePropsDoc
, "props", FoldPropsDoc
, emptyWordListDesc
);
1173 LexerModule
lmMake(SCLEX_MAKEFILE
, ColouriseMakeDoc
, "makefile", 0, emptyWordListDesc
);
1174 LexerModule
lmErrorList(SCLEX_ERRORLIST
, ColouriseErrorListDoc
, "errorlist", 0, emptyWordListDesc
);
1175 LexerModule
lmLatex(SCLEX_LATEX
, ColouriseLatexDoc
, "latex", 0, emptyWordListDesc
);
1176 LexerModule
lmNull(SCLEX_NULL
, ColouriseNullDoc
, "null");