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.
5  **/
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.
8 
9 #include <stdlib.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <stdarg.h>
14 
15 #include "Platform.h"
16 
17 #include "PropSet.h"
18 #include "Accessor.h"
19 #include "KeyWords.h"
20 #include "Scintilla.h"
21 #include "SciLexer.h"
22 
Is0To9(char ch)23 static bool Is0To9(char ch) {
24 	return (ch >= '0') && (ch <= '9');
25 }
26 
Is1To9(char ch)27 static bool Is1To9(char ch) {
28 	return (ch >= '1') && (ch <= '9');
29 }
30 
AtEOL(Accessor & styler,unsigned int i)31 static inline bool AtEOL(Accessor &styler, unsigned int i) {
32 	return (styler[i] == '\n') ||
33 	       ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
34 }
35 
36 // Tests for BATCH Operators
IsBOperator(char ch)37 static bool IsBOperator(char ch) {
38 	return (ch == '=') || (ch == '+') || (ch == '>') || (ch == '<') ||
39 		(ch == '|') || (ch == '?') || (ch == '*');
40 }
41 
42 // Tests for BATCH Separators
IsBSeparator(char ch)43 static bool IsBSeparator(char ch) {
44 	return (ch == '\\') || (ch == '.') || (ch == ';') ||
45 		(ch == '\"') || (ch == '\'') || (ch == '/') || (ch == ')');
46 }
47 
ColouriseBatchLine(char * lineBuffer,unsigned int lengthLine,unsigned int startLine,unsigned int endPos,WordList * keywordlists[],Accessor & styler)48 static void ColouriseBatchLine(
49     char *lineBuffer,
50     unsigned int lengthLine,
51     unsigned int startLine,
52     unsigned int endPos,
53     WordList *keywordlists[],
54     Accessor &styler) {
55 
56 	unsigned int offset = 0;	// Line Buffer Offset
57 	unsigned int enVarEnd;		// Environment Variable End point
58 	unsigned int cmdLoc;		// External Command / Program Location
59 	char wordBuffer[81];		// Word Buffer - large to catch long paths
60 	unsigned int wbl;		// Word Buffer Length
61 	unsigned int wbo;		// Word Buffer Offset - also Special Keyword Buffer Length
62 	WordList &keywords = *keywordlists[0];      // Internal Commands
63 	WordList &keywords2 = *keywordlists[1];     // External Commands (optional)
64 
65 	// CHOICE, ECHO, GOTO, PROMPT and SET have Default Text that may contain Regular Keywords
66 	//   Toggling Regular Keyword Checking off improves readability
67 	// Other Regular Keywords and External Commands / Programs might also benefit from toggling
68 	//   Need a more robust algorithm to properly toggle Regular Keyword Checking
69 	bool continueProcessing = true;	// Used to toggle Regular Keyword Checking
70 	// Special Keywords are those that allow certain characters without whitespace after the command
71 	// Examples are: cd. cd\ md. rd. dir| dir> echo: echo. path=
72 	// Special Keyword Buffer used to determine if the first n characters is a Keyword
73 	char sKeywordBuffer[10];	// Special Keyword Buffer
74 	bool sKeywordFound;		// Exit Special Keyword for-loop if found
75 
76 	// Skip initial spaces
77 	while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
78 		offset++;
79 	}
80 	// Colorize Default Text
81 	styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
82 	// Set External Command / Program Location
83 	cmdLoc = offset;
84 
85 	// Check for Fake Label (Comment) or Real Label - return if found
86 	if (lineBuffer[offset] == ':') {
87 		if (lineBuffer[offset + 1] == ':') {
88 			// Colorize Fake Label (Comment) - :: is similar to REM, see http://content.techweb.com/winmag/columns/explorer/2000/21.htm
89 			styler.ColourTo(endPos, SCE_BAT_COMMENT);
90 		} else {
91 			// Colorize Real Label
92 			styler.ColourTo(endPos, SCE_BAT_LABEL);
93 		}
94 		return;
95 	// Check for Drive Change (Drive Change is internal command) - return if found
96 	} else if ((isalpha(lineBuffer[offset])) &&
97 		(lineBuffer[offset + 1] == ':') &&
98 		((isspacechar(lineBuffer[offset + 2])) ||
99 		(((lineBuffer[offset + 2] == '\\')) &&
100 		(isspacechar(lineBuffer[offset + 3]))))) {
101 		// Colorize Regular Keyword
102 		styler.ColourTo(endPos, SCE_BAT_WORD);
103 		return;
104 	}
105 
106 	// Check for Hide Command (@ECHO OFF/ON)
107 	if (lineBuffer[offset] == '@') {
108 		styler.ColourTo(startLine + offset, SCE_BAT_HIDE);
109 		offset++;
110 	// Check for Argument (%n) or Environment Variable (%x...%)
111 	} else if (lineBuffer[offset] == '%') {
112 		enVarEnd = offset + 1;
113 		// Search end of word for second % (can be a long path)
114 		while ((enVarEnd < lengthLine) &&
115 			(!isspacechar(lineBuffer[enVarEnd])) &&
116 			(lineBuffer[enVarEnd] != '%') &&
117 			(!IsBOperator(lineBuffer[enVarEnd])) &&
118 			(!IsBSeparator(lineBuffer[enVarEnd]))) {
119 			enVarEnd++;
120 		}
121 		// Check for Argument (%n)
122 		if ((Is0To9(lineBuffer[offset + 1])) &&
123 			(lineBuffer[enVarEnd] != '%')) {
124 			// Colorize Argument
125 			styler.ColourTo(startLine + offset + 1, SCE_BAT_IDENTIFIER);
126 			offset += 2;
127 			// Check for External Command / Program
128 			if (!isspacechar(lineBuffer[offset])) {
129 				cmdLoc = offset;
130 			}
131 		// Check for Environment Variable (%x...%)
132 		} else if ((lineBuffer[offset + 1] != '%') &&
133 			(lineBuffer[enVarEnd] == '%')) {
134 			offset = enVarEnd;
135 			// Colorize Environment Variable
136 			styler.ColourTo(startLine + offset, SCE_BAT_IDENTIFIER);
137 			offset++;
138 			// Check for External Command / Program
139 			if (!isspacechar(lineBuffer[offset])) {
140 				cmdLoc = offset;
141 			}
142 		}
143 	}
144 	// Skip next spaces
145 	while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
146 		offset++;
147 	}
148 
149 	// Read remainder of line word-at-a-time or remainder-of-word-at-a-time
150 	while (offset < lengthLine) {
151 		if (offset > startLine) {
152 			// Colorize Default Text
153 			styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
154 		}
155 		// Copy word from Line Buffer into Word Buffer
156 		wbl = 0;
157 		for (; offset < lengthLine && wbl < 80 &&
158 		        !isspacechar(lineBuffer[offset]); wbl++, offset++) {
159 			wordBuffer[wbl] = static_cast<char>(tolower(lineBuffer[offset]));
160 		}
161 		wordBuffer[wbl] = '\0';
162 		wbo = 0;
163 
164 		// Check for Comment - return if found
165 		if (CompareCaseInsensitive(wordBuffer, "rem") == 0) {
166 			styler.ColourTo(endPos, SCE_BAT_COMMENT);
167 			return;
168 		}
169 		// Check for Separator
170 		if (IsBSeparator(wordBuffer[0])) {
171 			// Check for External Command / Program
172 			if ((cmdLoc == offset - wbl) &&
173 				((wordBuffer[0] == ':') ||
174 				(wordBuffer[0] == '\\') ||
175 				(wordBuffer[0] == '.'))) {
176 				// Reset Offset to re-process remainder of word
177 				offset -= (wbl - 1);
178 				// Colorize External Command / Program
179 				if (!keywords2) {
180 					styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
181 				} else if (keywords2.InList(wordBuffer)) {
182 					styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
183 				} else {
184 					styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
185 				}
186 				// Reset External Command / Program Location
187 				cmdLoc = offset;
188 			} else {
189 				// Reset Offset to re-process remainder of word
190 				offset -= (wbl - 1);
191 				// Colorize Default Text
192 				styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
193 			}
194 		// Check for Regular Keyword in list
195 		} else if ((keywords.InList(wordBuffer)) &&
196 			(continueProcessing)) {
197 			// ECHO, GOTO, PROMPT and SET require no further Regular Keyword Checking
198 			if ((CompareCaseInsensitive(wordBuffer, "echo") == 0) ||
199 				(CompareCaseInsensitive(wordBuffer, "goto") == 0) ||
200 				(CompareCaseInsensitive(wordBuffer, "prompt") == 0) ||
201 				(CompareCaseInsensitive(wordBuffer, "set") == 0)) {
202 				continueProcessing = false;
203 			}
204 			// Identify External Command / Program Location for ERRORLEVEL, and EXIST
205 			if ((CompareCaseInsensitive(wordBuffer, "errorlevel") == 0) ||
206 				(CompareCaseInsensitive(wordBuffer, "exist") == 0)) {
207 				// Reset External Command / Program Location
208 				cmdLoc = offset;
209 				// Skip next spaces
210 				while ((cmdLoc < lengthLine) &&
211 					(isspacechar(lineBuffer[cmdLoc]))) {
212 					cmdLoc++;
213 				}
214 				// Skip comparison
215 				while ((cmdLoc < lengthLine) &&
216 					(!isspacechar(lineBuffer[cmdLoc]))) {
217 					cmdLoc++;
218 				}
219 				// Skip next spaces
220 				while ((cmdLoc < lengthLine) &&
221 					(isspacechar(lineBuffer[cmdLoc]))) {
222 					cmdLoc++;
223 				}
224 			// Identify External Command / Program Location for CALL, DO, LOADHIGH and LH
225 			} else if ((CompareCaseInsensitive(wordBuffer, "call") == 0) ||
226 				(CompareCaseInsensitive(wordBuffer, "do") == 0) ||
227 				(CompareCaseInsensitive(wordBuffer, "loadhigh") == 0) ||
228 				(CompareCaseInsensitive(wordBuffer, "lh") == 0)) {
229 				// Reset External Command / Program Location
230 				cmdLoc = offset;
231 				// Skip next spaces
232 				while ((cmdLoc < lengthLine) &&
233 					(isspacechar(lineBuffer[cmdLoc]))) {
234 					cmdLoc++;
235 				}
236 			}
237 			// Colorize Regular keyword
238 			styler.ColourTo(startLine + offset - 1, SCE_BAT_WORD);
239 			// No need to Reset Offset
240 		// Check for Special Keyword in list, External Command / Program, or Default Text
241 		} else if ((wordBuffer[0] != '%') &&
242 			(!IsBOperator(wordBuffer[0])) &&
243 			(continueProcessing)) {
244 			// Check for Special Keyword
245 			//     Affected Commands are in Length range 2-6
246 			//     Good that ERRORLEVEL, EXIST, CALL, DO, LOADHIGH, and LH are unaffected
247 			sKeywordFound = false;
248 			for (unsigned int keywordLength = 2; keywordLength < wbl && keywordLength < 7 && !sKeywordFound; keywordLength++) {
249 				wbo = 0;
250 				// Copy Keyword Length from Word Buffer into Special Keyword Buffer
251 				for (; wbo < keywordLength; wbo++) {
252 					sKeywordBuffer[wbo] = static_cast<char>(wordBuffer[wbo]);
253 				}
254 				sKeywordBuffer[wbo] = '\0';
255 				// Check for Special Keyword in list
256 				if ((keywords.InList(sKeywordBuffer)) &&
257 					((IsBOperator(wordBuffer[wbo])) ||
258 					(IsBSeparator(wordBuffer[wbo])))) {
259 					sKeywordFound = true;
260 					// ECHO requires no further Regular Keyword Checking
261 					if (CompareCaseInsensitive(sKeywordBuffer, "echo") == 0) {
262 						continueProcessing = false;
263 					}
264 					// Colorize Special Keyword as Regular Keyword
265 					styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_WORD);
266 					// Reset Offset to re-process remainder of word
267 					offset -= (wbl - wbo);
268 				}
269 			}
270 			// Check for External Command / Program or Default Text
271 			if (!sKeywordFound) {
272 				wbo = 0;
273 				// Check for External Command / Program
274 				if (cmdLoc == offset - wbl) {
275 					// Read up to %, Operator or Separator
276 					while ((wbo < wbl) &&
277 						(wordBuffer[wbo] != '%') &&
278 						(!IsBOperator(wordBuffer[wbo])) &&
279 						(!IsBSeparator(wordBuffer[wbo]))) {
280 						wbo++;
281 					}
282 					// Reset External Command / Program Location
283 					cmdLoc = offset - (wbl - wbo);
284 					// Reset Offset to re-process remainder of word
285 					offset -= (wbl - wbo);
286 					// CHOICE requires no further Regular Keyword Checking
287 					if (CompareCaseInsensitive(wordBuffer, "choice") == 0) {
288 						continueProcessing = false;
289 					}
290 					// Check for START (and its switches) - What follows is External Command \ Program
291 					if (CompareCaseInsensitive(wordBuffer, "start") == 0) {
292 						// Reset External Command / Program Location
293 						cmdLoc = offset;
294 						// Skip next spaces
295 						while ((cmdLoc < lengthLine) &&
296 							(isspacechar(lineBuffer[cmdLoc]))) {
297 							cmdLoc++;
298 						}
299 						// Reset External Command / Program Location if command switch detected
300 						if (lineBuffer[cmdLoc] == '/') {
301 							// Skip command switch
302 							while ((cmdLoc < lengthLine) &&
303 								(!isspacechar(lineBuffer[cmdLoc]))) {
304 								cmdLoc++;
305 							}
306 							// Skip next spaces
307 							while ((cmdLoc < lengthLine) &&
308 								(isspacechar(lineBuffer[cmdLoc]))) {
309 								cmdLoc++;
310 							}
311 						}
312 					}
313 					// Colorize External Command / Program
314 					if (!keywords2) {
315 						styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
316 					} else if (keywords2.InList(wordBuffer)) {
317 						styler.ColourTo(startLine + offset - 1, SCE_BAT_COMMAND);
318 					} else {
319 						styler.ColourTo(startLine + offset - 1, SCE_BAT_DEFAULT);
320 					}
321 					// No need to Reset Offset
322 				// Check for Default Text
323 				} else {
324 					// Read up to %, Operator or Separator
325 					while ((wbo < wbl) &&
326 						(wordBuffer[wbo] != '%') &&
327 						(!IsBOperator(wordBuffer[wbo])) &&
328 						(!IsBSeparator(wordBuffer[wbo]))) {
329 						wbo++;
330 					}
331 					// Colorize Default Text
332 					styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT);
333 					// Reset Offset to re-process remainder of word
334 					offset -= (wbl - wbo);
335 				}
336 			}
337 		// Check for Argument  (%n), Environment Variable (%x...%) or Local Variable (%%a)
338 		} else if (wordBuffer[0] == '%') {
339 			// Colorize Default Text
340 			styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT);
341 			wbo++;
342 			// Search to end of word for second % (can be a long path)
343 			while ((wbo < wbl) &&
344 				(wordBuffer[wbo] != '%') &&
345 				(!IsBOperator(wordBuffer[wbo])) &&
346 				(!IsBSeparator(wordBuffer[wbo]))) {
347 				wbo++;
348 			}
349 			// Check for Argument (%n)
350 			if ((Is0To9(wordBuffer[1])) &&
351 				(wordBuffer[wbo] != '%')) {
352 				// Check for External Command / Program
353 				if (cmdLoc == offset - wbl) {
354 					cmdLoc = offset - (wbl - 2);
355 				}
356 				// Colorize Argument
357 				styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_IDENTIFIER);
358 				// Reset Offset to re-process remainder of word
359 				offset -= (wbl - 2);
360 			// Check for Environment Variable (%x...%)
361 			} else if ((wordBuffer[1] != '%') &&
362 				(wordBuffer[wbo] == '%')) {
363 				wbo++;
364 				// Check for External Command / Program
365 				if (cmdLoc == offset - wbl) {
366 					cmdLoc = offset - (wbl - wbo);
367 				}
368 				// Colorize Environment Variable
369 				styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_IDENTIFIER);
370 				// Reset Offset to re-process remainder of word
371 				offset -= (wbl - wbo);
372 			// Check for Local Variable (%%a)
373 			} else if (
374 				(wordBuffer[1] == '%') &&
375 				(wordBuffer[2] != '%') &&
376 				(!IsBOperator(wordBuffer[2])) &&
377 				(!IsBSeparator(wordBuffer[2]))) {
378 				// Check for External Command / Program
379 				if (cmdLoc == offset - wbl) {
380 					cmdLoc = offset - (wbl - 3);
381 				}
382 				// Colorize Local Variable
383 				styler.ColourTo(startLine + offset - 1 - (wbl - 3), SCE_BAT_IDENTIFIER);
384 				// Reset Offset to re-process remainder of word
385 				offset -= (wbl - 3);
386 			}
387 		// Check for Operator
388 		} else if (IsBOperator(wordBuffer[0])) {
389 			// Colorize Default Text
390 			styler.ColourTo(startLine + offset - 1 - wbl, SCE_BAT_DEFAULT);
391 			// Check for Comparison Operator
392 			if ((wordBuffer[0] == '=') && (wordBuffer[1] == '=')) {
393 				// Identify External Command / Program Location for IF
394 				cmdLoc = offset;
395 				// Skip next spaces
396 				while ((cmdLoc < lengthLine) &&
397 					(isspacechar(lineBuffer[cmdLoc]))) {
398 					cmdLoc++;
399 				}
400 				// Colorize Comparison Operator
401 				styler.ColourTo(startLine + offset - 1 - (wbl - 2), SCE_BAT_OPERATOR);
402 				// Reset Offset to re-process remainder of word
403 				offset -= (wbl - 2);
404 			// Check for Pipe Operator
405 			} else if (wordBuffer[0] == '|') {
406 				// Reset External Command / Program Location
407 				cmdLoc = offset - wbl + 1;
408 				// Skip next spaces
409 				while ((cmdLoc < lengthLine) &&
410 					(isspacechar(lineBuffer[cmdLoc]))) {
411 					cmdLoc++;
412 				}
413 				// Colorize Pipe Operator
414 				styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_BAT_OPERATOR);
415 				// Reset Offset to re-process remainder of word
416 				offset -= (wbl - 1);
417 			// Check for Other Operator
418 			} else {
419 				// Check for > Operator
420 				if (wordBuffer[0] == '>') {
421 					// Turn Keyword and External Command / Program checking back on
422 					continueProcessing = true;
423 				}
424 				// Colorize Other Operator
425 				styler.ColourTo(startLine + offset - 1 - (wbl - 1), SCE_BAT_OPERATOR);
426 				// Reset Offset to re-process remainder of word
427 				offset -= (wbl - 1);
428 			}
429 		// Check for Default Text
430 		} else {
431 			// Read up to %, Operator or Separator
432 			while ((wbo < wbl) &&
433 				(wordBuffer[wbo] != '%') &&
434 				(!IsBOperator(wordBuffer[wbo])) &&
435 				(!IsBSeparator(wordBuffer[wbo]))) {
436 				wbo++;
437 			}
438 			// Colorize Default Text
439 			styler.ColourTo(startLine + offset - 1 - (wbl - wbo), SCE_BAT_DEFAULT);
440 			// Reset Offset to re-process remainder of word
441 			offset -= (wbl - wbo);
442 		}
443 		// Skip next spaces - nothing happens if Offset was Reset
444 		while ((offset < lengthLine) && (isspacechar(lineBuffer[offset]))) {
445 			offset++;
446 		}
447 	}
448 	// Colorize Default Text for remainder of line - currently not lexed
449 	styler.ColourTo(endPos, SCE_BAT_DEFAULT);
450 }
451 
ColouriseBatchDoc(unsigned int startPos,int length,int,WordList * keywordlists[],Accessor & styler)452 static void ColouriseBatchDoc(
453     unsigned int startPos,
454     int length,
455     int /*initStyle*/,
456     WordList *keywordlists[],
457     Accessor &styler) {
458 
459 	char lineBuffer[1024];
460 
461 	styler.StartAt(startPos);
462 	styler.StartSegment(startPos);
463 	unsigned int linePos = 0;
464 	unsigned int startLine = startPos;
465 	for (unsigned int i = startPos; i < startPos + length; i++) {
466 		lineBuffer[linePos++] = styler[i];
467 		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
468 			// End of line (or of line buffer) met, colourise it
469 			lineBuffer[linePos] = '\0';
470 			ColouriseBatchLine(lineBuffer, linePos, startLine, i, keywordlists, styler);
471 			linePos = 0;
472 			startLine = i + 1;
473 		}
474 	}
475 	if (linePos > 0) {	// Last line does not have ending characters
476 		ColouriseBatchLine(lineBuffer, linePos, startLine, startPos + length - 1,
477 		                   keywordlists, styler);
478 	}
479 }
480 
ColouriseDiffLine(char * lineBuffer,int endLine,Accessor & styler)481 static void ColouriseDiffLine(char *lineBuffer, int endLine, Accessor &styler) {
482 	// It is needed to remember the current state to recognize starting
483 	// comment lines before the first "diff " or "--- ". If a real
484 	// difference starts then each line starting with ' ' is a whitespace
485 	// otherwise it is considered a comment (Only in..., Binary file...)
486 	if (0 == strncmp(lineBuffer, "diff ", 5)) {
487 		styler.ColourTo(endLine, SCE_DIFF_COMMAND);
488 	} else if (0 == strncmp(lineBuffer, "--- ", 4)) {
489 		// In a context diff, --- appears in both the header and the position markers
490 		if (atoi(lineBuffer+4) && !strchr(lineBuffer, '/'))
491 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
492 		else
493 			styler.ColourTo(endLine, SCE_DIFF_HEADER);
494 	} else if (0 == strncmp(lineBuffer, "+++ ", 4)) {
495 		// I don't know of any diff where "+++ " is a position marker, but for
496 		// consistency, do the same as with "--- " and "*** ".
497 		if (atoi(lineBuffer+4) && !strchr(lineBuffer, '/'))
498 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
499 		else
500 			styler.ColourTo(endLine, SCE_DIFF_HEADER);
501 	} else if (0 == strncmp(lineBuffer, "====", 4)) {  // For p4's diff
502 		styler.ColourTo(endLine, SCE_DIFF_HEADER);
503 	} else if (0 == strncmp(lineBuffer, "***", 3)) {
504 		// In a context diff, *** appears in both the header and the position markers.
505 		// Also ******** is a chunk header, but here it's treated as part of the
506 		// position marker since there is no separate style for a chunk header.
507 		if (lineBuffer[3] == ' ' && atoi(lineBuffer+4) && !strchr(lineBuffer, '/'))
508 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
509 		else if (lineBuffer[3] == '*')
510 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
511 		else
512 			styler.ColourTo(endLine, SCE_DIFF_HEADER);
513 	} else if (0 == strncmp(lineBuffer, "? ", 2)) {    // For difflib
514 		styler.ColourTo(endLine, SCE_DIFF_HEADER);
515 	} else if (lineBuffer[0] == '@') {
516 		styler.ColourTo(endLine, SCE_DIFF_POSITION);
517 	} else if (lineBuffer[0] >= '0' && lineBuffer[0] <= '9') {
518 		styler.ColourTo(endLine, SCE_DIFF_POSITION);
519 	} else if (lineBuffer[0] == '-' || lineBuffer[0] == '<') {
520 		styler.ColourTo(endLine, SCE_DIFF_DELETED);
521 	} else if (lineBuffer[0] == '+' || lineBuffer[0] == '>') {
522 		styler.ColourTo(endLine, SCE_DIFF_ADDED);
523 	} else if (lineBuffer[0] != ' ') {
524 		styler.ColourTo(endLine, SCE_DIFF_COMMENT);
525 	} else {
526 		styler.ColourTo(endLine, SCE_DIFF_DEFAULT);
527 	}
528 }
529 
ColouriseDiffDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)530 static void ColouriseDiffDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
531 	char lineBuffer[1024];
532 	styler.StartAt(startPos);
533 	styler.StartSegment(startPos);
534 	unsigned int linePos = 0;
535 	for (unsigned int i = startPos; i < startPos + length; i++) {
536 		lineBuffer[linePos++] = styler[i];
537 		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
538 			// End of line (or of line buffer) met, colourise it
539 			lineBuffer[linePos] = '\0';
540 			ColouriseDiffLine(lineBuffer, i, styler);
541 			linePos = 0;
542 		}
543 	}
544 	if (linePos > 0) {	// Last line does not have ending characters
545 		ColouriseDiffLine(lineBuffer, startPos + length - 1, styler);
546 	}
547 }
548 
FoldDiffDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)549 static void FoldDiffDoc(unsigned int startPos, int length, int, WordList*[], Accessor &styler) {
550 	int curLine = styler.GetLine(startPos);
551 	int prevLevel = SC_FOLDLEVELBASE;
552 	if (curLine > 0)
553 		prevLevel = styler.LevelAt(curLine-1);
554 
555 	int curLineStart = styler.LineStart(curLine);
556 	do {
557 		int nextLevel = prevLevel;
558 		if (prevLevel & SC_FOLDLEVELHEADERFLAG)
559 			nextLevel = (prevLevel & SC_FOLDLEVELNUMBERMASK) + 1;
560 
561 		int lineType = styler.StyleAt(curLineStart);
562 		if (lineType == SCE_DIFF_COMMAND)
563 			nextLevel = (SC_FOLDLEVELBASE + 1) | SC_FOLDLEVELHEADERFLAG;
564 		else if (lineType == SCE_DIFF_HEADER) {
565 			nextLevel = (SC_FOLDLEVELBASE + 2) | SC_FOLDLEVELHEADERFLAG;
566 		} else if (lineType == SCE_DIFF_POSITION)
567 			nextLevel = (SC_FOLDLEVELBASE + 3) | SC_FOLDLEVELHEADERFLAG;
568 
569 		if ((nextLevel & SC_FOLDLEVELHEADERFLAG) && (nextLevel == prevLevel))
570 			styler.SetLevel(curLine-1, prevLevel & ~SC_FOLDLEVELHEADERFLAG);
571 
572 		styler.SetLevel(curLine, nextLevel);
573 		prevLevel = nextLevel;
574 
575 		curLineStart = styler.LineStart(++curLine);
576 	} while (static_cast<int>(startPos) + length > curLineStart);
577 }
578 
579 
ColourisePropsLine(char * lineBuffer,unsigned int lengthLine,unsigned int startLine,unsigned int endPos,Accessor & styler)580 static void ColourisePropsLine(
581     char *lineBuffer,
582     unsigned int lengthLine,
583     unsigned int startLine,
584     unsigned int endPos,
585     Accessor &styler) {
586 
587 	unsigned int i = 0;
588 	while ((i < lengthLine) && isspacechar(lineBuffer[i]))	// Skip initial spaces
589 		i++;
590 	if (i < lengthLine) {
591 		if (lineBuffer[i] == '#' || lineBuffer[i] == '!' || lineBuffer[i] == ';') {
592 			styler.ColourTo(endPos, SCE_PROPS_COMMENT);
593 		} else if (lineBuffer[i] == '[') {
594 			styler.ColourTo(endPos, SCE_PROPS_SECTION);
595 		} else if (lineBuffer[i] == '@') {
596 			styler.ColourTo(startLine + i, SCE_PROPS_DEFVAL);
597 			if (lineBuffer[++i] == '=')
598 				styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT);
599 			styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
600 		} else {
601 			// Search for the '=' character
602 			while ((i < lengthLine) && (lineBuffer[i] != '='))
603 				i++;
604 			if ((i < lengthLine) && (lineBuffer[i] == '=')) {
605 				styler.ColourTo(startLine + i - 1, SCE_PROPS_KEY);
606 				styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT);
607 				styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
608 			} else {
609 				styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
610 			}
611 		}
612 	} else {
613 		styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
614 	}
615 }
616 
ColourisePropsDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)617 static void ColourisePropsDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
618 	char lineBuffer[1024];
619 	styler.StartAt(startPos);
620 	styler.StartSegment(startPos);
621 	unsigned int linePos = 0;
622 	unsigned int startLine = startPos;
623 	for (unsigned int i = startPos; i < startPos + length; i++) {
624 		lineBuffer[linePos++] = styler[i];
625 		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
626 			// End of line (or of line buffer) met, colourise it
627 			lineBuffer[linePos] = '\0';
628 			ColourisePropsLine(lineBuffer, linePos, startLine, i, styler);
629 			linePos = 0;
630 			startLine = i + 1;
631 		}
632 	}
633 	if (linePos > 0) {	// Last line does not have ending characters
634 		ColourisePropsLine(lineBuffer, linePos, startLine, startPos + length - 1, styler);
635 	}
636 }
637 
638 // adaption by ksc, using the "} else {" trick of 1.53
639 // 030721
FoldPropsDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)640 static void FoldPropsDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
641 	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
642 
643 	unsigned int endPos = startPos + length;
644 	int visibleChars = 0;
645 	int lineCurrent = styler.GetLine(startPos);
646 
647 	char chNext = styler[startPos];
648 	int styleNext = styler.StyleAt(startPos);
649 	bool headerPoint = false;
650 	int lev;
651 
652 	for (unsigned int i = startPos; i < endPos; i++) {
653 		char ch = chNext;
654 		chNext = styler[i+1];
655 
656 		int style = styleNext;
657 		styleNext = styler.StyleAt(i + 1);
658 		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
659 
660 		if (style == SCE_PROPS_SECTION) {
661 			headerPoint = true;
662 		}
663 
664 		if (atEOL) {
665 			lev = SC_FOLDLEVELBASE;
666 
667 			if (lineCurrent > 0) {
668 				int levelPrevious = styler.LevelAt(lineCurrent - 1);
669 
670 				if (levelPrevious & SC_FOLDLEVELHEADERFLAG) {
671 					lev = SC_FOLDLEVELBASE + 1;
672 				} else {
673 					lev = levelPrevious & SC_FOLDLEVELNUMBERMASK;
674 				}
675 			}
676 
677 			if (headerPoint) {
678 				lev = SC_FOLDLEVELBASE;
679 			}
680 			if (visibleChars == 0 && foldCompact)
681 				lev |= SC_FOLDLEVELWHITEFLAG;
682 
683 			if (headerPoint) {
684 				lev |= SC_FOLDLEVELHEADERFLAG;
685 			}
686 			if (lev != styler.LevelAt(lineCurrent)) {
687 				styler.SetLevel(lineCurrent, lev);
688 			}
689 
690 			lineCurrent++;
691 			visibleChars = 0;
692 			headerPoint = false;
693 		}
694 		if (!isspacechar(ch))
695 			visibleChars++;
696 	}
697 
698 	if (lineCurrent > 0) {
699 		int levelPrevious = styler.LevelAt(lineCurrent - 1);
700 		if (levelPrevious & SC_FOLDLEVELHEADERFLAG) {
701 			lev = SC_FOLDLEVELBASE + 1;
702 		} else {
703 			lev = levelPrevious & SC_FOLDLEVELNUMBERMASK;
704 		}
705 	} else {
706 		lev = SC_FOLDLEVELBASE;
707 	}
708 	int flagsNext = styler.LevelAt(lineCurrent);
709 	styler.SetLevel(lineCurrent, lev | flagsNext & ~SC_FOLDLEVELNUMBERMASK);
710 }
711 
ColouriseMakeLine(char * lineBuffer,unsigned int lengthLine,unsigned int startLine,unsigned int endPos,Accessor & styler)712 static void ColouriseMakeLine(
713     char *lineBuffer,
714     unsigned int lengthLine,
715     unsigned int startLine,
716     unsigned int endPos,
717     Accessor &styler) {
718 
719 	unsigned int i = 0;
720 	int lastNonSpace = -1;
721 	unsigned int state = SCE_MAKE_DEFAULT;
722 	bool bSpecial = false;
723 	// Skip initial spaces
724 	while ((i < lengthLine) && isspacechar(lineBuffer[i])) {
725 		i++;
726 	}
727 	if (lineBuffer[i] == '#') {	// Comment
728 		styler.ColourTo(endPos, SCE_MAKE_COMMENT);
729 		return;
730 	}
731 	if (lineBuffer[i] == '!') {	// Special directive
732 		styler.ColourTo(endPos, SCE_MAKE_PREPROCESSOR);
733 		return;
734 	}
735 	while (i < lengthLine) {
736 		if (lineBuffer[i] == '$' && lineBuffer[i + 1] == '(') {
737 			styler.ColourTo(startLine + i - 1, state);
738 			state = SCE_MAKE_IDENTIFIER;
739 		} else if (state == SCE_MAKE_IDENTIFIER && lineBuffer[i] == ')') {
740 			styler.ColourTo(startLine + i, state);
741 			state = SCE_MAKE_DEFAULT;
742 		}
743 		if (!bSpecial) {
744 			if (lineBuffer[i] == ':') {
745 				// We should check that no colouring was made since the beginning of the line,
746 				// to avoid colouring stuff like /OUT:file
747 				if (lastNonSpace >= 0)
748 					styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_TARGET);
749 				styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT);
750 				styler.ColourTo(startLine + i, SCE_MAKE_OPERATOR);
751 				bSpecial = true;	// Only react to the first ':' of the line
752 				state = SCE_MAKE_DEFAULT;
753 			} else if (lineBuffer[i] == '=') {
754 				if (lastNonSpace >= 0)
755 					styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_IDENTIFIER);
756 				styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT);
757 				styler.ColourTo(startLine + i, SCE_MAKE_OPERATOR);
758 				bSpecial = true;	// Only react to the first '=' of the line
759 				state = SCE_MAKE_DEFAULT;
760 			}
761 		}
762 		if (!isspacechar(lineBuffer[i])) {
763 			lastNonSpace = i;
764 		}
765 		i++;
766 	}
767 	if (state == SCE_MAKE_IDENTIFIER) {
768 		styler.ColourTo(endPos, SCE_MAKE_IDEOL);	// Error, variable reference not ended
769 	} else {
770 		styler.ColourTo(endPos, SCE_MAKE_DEFAULT);
771 	}
772 }
773 
ColouriseMakeDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)774 static void ColouriseMakeDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
775 	char lineBuffer[1024];
776 	styler.StartAt(startPos);
777 	styler.StartSegment(startPos);
778 	unsigned int linePos = 0;
779 	unsigned int startLine = startPos;
780 	for (unsigned int i = startPos; i < startPos + length; i++) {
781 		lineBuffer[linePos++] = styler[i];
782 		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
783 			// End of line (or of line buffer) met, colourise it
784 			lineBuffer[linePos] = '\0';
785 			ColouriseMakeLine(lineBuffer, linePos, startLine, i, styler);
786 			linePos = 0;
787 			startLine = i + 1;
788 		}
789 	}
790 	if (linePos > 0) {	// Last line does not have ending characters
791 		ColouriseMakeLine(lineBuffer, linePos, startLine, startPos + length - 1, styler);
792 	}
793 }
794 
strstart(const char * haystack,const char * needle)795 static bool strstart(const char *haystack, const char *needle) {
796 	return strncmp(haystack, needle, strlen(needle)) == 0;
797 }
798 
RecogniseErrorListLine(const char * lineBuffer,unsigned int lengthLine)799 static int RecogniseErrorListLine(const char *lineBuffer, unsigned int lengthLine) {
800 	if (lineBuffer[0] == '>') {
801 		// Command or return status
802 		return SCE_ERR_CMD;
803 	} else if (lineBuffer[0] == '<') {
804 		// Diff removal, but not interested. Trapped to avoid hitting CTAG cases.
805 		return SCE_ERR_DEFAULT;
806 	} else if (lineBuffer[0] == '!') {
807 		return SCE_ERR_DIFF_CHANGED;
808 	} else if (lineBuffer[0] == '+') {
809 		if (strstart(lineBuffer, "+++ ")) {
810 			return SCE_ERR_DIFF_MESSAGE;
811 		} else {
812 			return SCE_ERR_DIFF_ADDITION;
813 		}
814 	} else if (lineBuffer[0] == '-') {
815 		if (strstart(lineBuffer, "--- ")) {
816 			return SCE_ERR_DIFF_MESSAGE;
817 		} else {
818 			return SCE_ERR_DIFF_DELETION;
819 		}
820 	} else if (strstart(lineBuffer, "cf90-")) {
821 		// Absoft Pro Fortran 90/95 v8.2 error and/or warning message
822 		return SCE_ERR_ABSF;
823 	} else if (strstart(lineBuffer, "fortcom:")) {
824 		// Intel Fortran Compiler v8.0 error/warning message
825 		return SCE_ERR_IFORT;
826 	} else if (strstr(lineBuffer, "File \"") && strstr(lineBuffer, ", line ")) {
827 		return SCE_ERR_PYTHON;
828 	} else if (strstr(lineBuffer, " in ") && strstr(lineBuffer, " on line ")) {
829 		return SCE_ERR_PHP;
830 	} else if ((strstart(lineBuffer, "Error ") ||
831 	            strstart(lineBuffer, "Warning ")) &&
832 	           strstr(lineBuffer, " at (") &&
833 	           strstr(lineBuffer, ") : ") &&
834 	           (strstr(lineBuffer, " at (") < strstr(lineBuffer, ") : "))) {
835 		// Intel Fortran Compiler error/warning message
836 		return SCE_ERR_IFC;
837 	} else if (strstart(lineBuffer, "Error ")) {
838 		// Borland error message
839 		return SCE_ERR_BORLAND;
840 	} else if (strstart(lineBuffer, "Warning ")) {
841 		// Borland warning message
842 		return SCE_ERR_BORLAND;
843 	} else if (strstr(lineBuffer, "at line " ) &&
844 	           (strstr(lineBuffer, "at line " ) < (lineBuffer + lengthLine)) &&
845 	           strstr(lineBuffer, "file ") &&
846 	           (strstr(lineBuffer, "file ") < (lineBuffer + lengthLine))) {
847 		// Lua 4 error message
848 		return SCE_ERR_LUA;
849 	} else if (strstr(lineBuffer, " at " ) &&
850 	           (strstr(lineBuffer, " at " ) < (lineBuffer + lengthLine)) &&
851 	           strstr(lineBuffer, " line ") &&
852 	           (strstr(lineBuffer, " line ") < (lineBuffer + lengthLine)) &&
853 	           (strstr(lineBuffer, " at " ) < (strstr(lineBuffer, " line ")))) {
854 		// perl error message
855 		return SCE_ERR_PERL;
856 	} else if ((memcmp(lineBuffer, "   at ", 6) == 0) &&
857 	           strstr(lineBuffer, ":line ")) {
858 		// A .NET traceback
859 		return SCE_ERR_NET;
860 	} else if (strstart(lineBuffer, "Line ") &&
861 	           strstr(lineBuffer, ", file ")) {
862 		// Essential Lahey Fortran error message
863 		return SCE_ERR_ELF;
864 	} else if (strstart(lineBuffer, "line ") &&
865 	           strstr(lineBuffer, " column ")) {
866 		// HTML tidy style: line 42 column 1
867 		return SCE_ERR_TIDY;
868 	} else if (strstart(lineBuffer, "\tat ") &&
869 	           strstr(lineBuffer, "(") &&
870 	           strstr(lineBuffer, ".java:")) {
871 		// Java stack back trace
872 		return SCE_ERR_JAVA_STACK;
873 	} else {
874 		// Look for one of the following formats:
875 		// GCC: <filename>:<line>:<message>
876 		// Microsoft: <filename>(<line>) :<message>
877 		// Common: <filename>(<line>): warning|error|note|remark|catastrophic|fatal
878 		// Common: <filename>(<line>) warning|error|note|remark|catastrophic|fatal
879 		// Microsoft: <filename>(<line>,<column>)<message>
880 		// CTags: \t<message>
881 		// Lua 5 traceback: \t<filename>:<line>:<message>
882 		bool initialTab = (lineBuffer[0] == '\t');
883 		enum { stInitial,
884 			stGccStart, stGccDigit, stGcc,
885 			stMsStart, stMsDigit, stMsBracket, stMsVc, stMsDigitComma, stMsDotNet,
886 			stCtagsStart, stCtagsStartString, stCtagsStringDollar, stCtags,
887 			stUnrecognized
888 		} state = stInitial;
889 		for (unsigned int i = 0; i < lengthLine; i++) {
890 			char ch = lineBuffer[i];
891 			char chNext = ' ';
892 			if ((i + 1) < lengthLine)
893 				chNext = lineBuffer[i + 1];
894 			if (state == stInitial) {
895 				if (ch == ':') {
896 					// May be GCC, or might be Lua 5 (Lua traceback same but with tab prefix)
897 					if ((chNext != '\\') && (chNext != '/')) {
898 						// This check is not completely accurate as may be on
899 						// GTK+ with a file name that includes ':'.
900 						state = stGccStart;
901 					}
902 				} else if ((ch == '(') && Is1To9(chNext) && (!initialTab)) {
903 					// May be Microsoft
904 					// Check against '0' often removes phone numbers
905 					state = stMsStart;
906 				} else if ((ch == '\t') && (!initialTab)) {
907 					// May be CTags
908 					state = stCtagsStart;
909 				}
910 			} else if (state == stGccStart) {	// <filename>:
911 				state = Is1To9(ch) ? stGccDigit : stUnrecognized;
912 			} else if (state == stGccDigit) {	// <filename>:<line>
913 				if (ch == ':') {
914 					state = stGcc;	// :9.*: is GCC
915 					break;
916 				} else if (!Is0To9(ch)) {
917 					state = stUnrecognized;
918 				}
919 			} else if (state == stMsStart) {	// <filename>(
920 				state = Is0To9(ch) ? stMsDigit : stUnrecognized;
921 			} else if (state == stMsDigit) {	// <filename>(<line>
922 				if (ch == ',') {
923 					state = stMsDigitComma;
924 				} else if (ch == ')') {
925 					state = stMsBracket;
926 				} else if ((ch != ' ') && !Is0To9(ch)) {
927 					state = stUnrecognized;
928 				}
929 			} else if (state == stMsBracket) {	// <filename>(<line>)
930 				if ((ch == ' ') && (chNext == ':')) {
931 					state = stMsVc;
932 				} else if ((ch == ':' && chNext == ' ') || (ch == ' ')) {
933 					// Possibly Delphi.. don't test against chNext as it's one of the strings below.
934 					char word[512];
935 					unsigned int j, chPos;
936 					unsigned numstep;
937 					chPos = 0;
938 					if (ch == ' ')
939 						numstep = 1; // ch was ' ', handle as if it's a delphi errorline, only add 1 to i.
940 					else
941 						numstep = 2; // otherwise add 2.
942 					for (j = i + numstep; j < lengthLine && isalpha(lineBuffer[j]) && chPos < sizeof(word) - 1; j++)
943 						word[chPos++] = lineBuffer[j];
944 					word[chPos] = 0;
945 					if (!CompareCaseInsensitive(word, "error") || !CompareCaseInsensitive(word, "warning") ||
946 						!CompareCaseInsensitive(word, "fatal") || !CompareCaseInsensitive(word, "catastrophic") ||
947 						!CompareCaseInsensitive(word, "note") || !CompareCaseInsensitive(word, "remark")) {
948 						state = stMsVc;
949 					} else
950 						state = stUnrecognized;
951 				} else {
952 					state = stUnrecognized;
953 				}
954 			} else if (state == stMsDigitComma) {	// <filename>(<line>,
955 				if (ch == ')') {
956 					state = stMsDotNet;
957 					break;
958 				} else if ((ch != ' ') && !Is0To9(ch)) {
959 					state = stUnrecognized;
960 				}
961 			} else if (state == stCtagsStart) {
962 				if ((lineBuffer[i - 1] == '\t') &&
963 				        ((ch == '/' && lineBuffer[i + 1] == '^') || Is0To9(ch))) {
964 					state = stCtags;
965 					break;
966 				} else if ((ch == '/') && (lineBuffer[i + 1] == '^')) {
967 					state = stCtagsStartString;
968 				}
969 			} else if ((state == stCtagsStartString) && ((lineBuffer[i] == '$') && (lineBuffer[i + 1] == '/'))) {
970 				state = stCtagsStringDollar;
971 				break;
972 			}
973 		}
974 		if (state == stGcc) {
975 			return SCE_ERR_GCC;
976 		} else if ((state == stMsVc) || (state == stMsDotNet)) {
977 			return SCE_ERR_MS;
978 		} else if ((state == stCtagsStringDollar) || (state == stCtags)) {
979 			return SCE_ERR_CTAG;
980 		} else {
981 			return SCE_ERR_DEFAULT;
982 		}
983 	}
984 }
985 
ColouriseErrorListLine(char * lineBuffer,unsigned int lengthLine,unsigned int endPos,Accessor & styler)986 static void ColouriseErrorListLine(
987     char *lineBuffer,
988     unsigned int lengthLine,
989     unsigned int endPos,
990     Accessor &styler) {
991 	styler.ColourTo(endPos, RecogniseErrorListLine(lineBuffer, lengthLine));
992 }
993 
ColouriseErrorListDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)994 static void ColouriseErrorListDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
995 	char lineBuffer[10000];
996 	styler.StartAt(startPos);
997 	styler.StartSegment(startPos);
998 	unsigned int linePos = 0;
999 	for (unsigned int i = startPos; i < startPos + length; i++) {
1000 		lineBuffer[linePos++] = styler[i];
1001 		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
1002 			// End of line (or of line buffer) met, colourise it
1003 			lineBuffer[linePos] = '\0';
1004 			ColouriseErrorListLine(lineBuffer, linePos, i, styler);
1005 			linePos = 0;
1006 		}
1007 	}
1008 	if (linePos > 0) {	// Last line does not have ending characters
1009 		ColouriseErrorListLine(lineBuffer, linePos, startPos + length - 1, styler);
1010 	}
1011 }
1012 
isSpecial(char s)1013 static int isSpecial(char s) {
1014 	return (s == '\\') || (s == ',') || (s == ';') || (s == '\'') || (s == ' ') ||
1015 	       (s == '\"') || (s == '`') || (s == '^') || (s == '~');
1016 }
1017 
isTag(int start,Accessor & styler)1018 static int isTag(int start, Accessor &styler) {
1019 	char s[6];
1020 	unsigned int i = 0, e = 1;
1021 	while (i < 5 && e) {
1022 		s[i] = styler[start + i];
1023 		i++;
1024 		e = styler[start + i] != '{';
1025 	}
1026 	s[i] = '\0';
1027 	return (strcmp(s, "begin") == 0) || (strcmp(s, "end") == 0);
1028 }
1029 
ColouriseLatexDoc(unsigned int startPos,int length,int initStyle,WordList * [],Accessor & styler)1030 static void ColouriseLatexDoc(unsigned int startPos, int length, int initStyle,
1031                               WordList *[], Accessor &styler) {
1032 
1033 	styler.StartAt(startPos);
1034 
1035 	int state = initStyle;
1036 	char chNext = styler[startPos];
1037 	styler.StartSegment(startPos);
1038 	int lengthDoc = startPos + length;
1039 
1040 	for (int i = startPos; i < lengthDoc; i++) {
1041 		char ch = chNext;
1042 		chNext = styler.SafeGetCharAt(i + 1);
1043 
1044 		if (styler.IsLeadByte(ch)) {
1045 			chNext = styler.SafeGetCharAt(i + 2);
1046 			i++;
1047 			continue;
1048 		}
1049 		switch (state) {
1050 		case SCE_L_DEFAULT :
1051 			switch (ch) {
1052 			case '\\' :
1053 				styler.ColourTo(i - 1, state);
1054 				if (isSpecial(styler[i + 1])) {
1055 					styler.ColourTo(i + 1, SCE_L_COMMAND);
1056 					i++;
1057 					chNext = styler.SafeGetCharAt(i + 1);
1058 				} else {
1059 					if (isTag(i + 1, styler))
1060 						state = SCE_L_TAG;
1061 					else
1062 						state = SCE_L_COMMAND;
1063 				}
1064 				break;
1065 			case '$' :
1066 				styler.ColourTo(i - 1, state);
1067 				state = SCE_L_MATH;
1068 				if (chNext == '$') {
1069 					i++;
1070 					chNext = styler.SafeGetCharAt(i + 1);
1071 				}
1072 				break;
1073 			case '%' :
1074 				styler.ColourTo(i - 1, state);
1075 				state = SCE_L_COMMENT;
1076 				break;
1077 			}
1078 			break;
1079 		case SCE_L_COMMAND :
1080 			if (chNext == '[' || chNext == '{' || chNext == '}' ||
1081 			        chNext == ' ' || chNext == '\r' || chNext == '\n') {
1082 				styler.ColourTo(i, state);
1083 				state = SCE_L_DEFAULT;
1084 				i++;
1085 				chNext = styler.SafeGetCharAt(i + 1);
1086 			}
1087 			break;
1088 		case SCE_L_TAG :
1089 			if (ch == '}') {
1090 				styler.ColourTo(i, state);
1091 				state = SCE_L_DEFAULT;
1092 			}
1093 			break;
1094 		case SCE_L_MATH :
1095 			if (ch == '$') {
1096 				if (chNext == '$') {
1097 					i++;
1098 					chNext = styler.SafeGetCharAt(i + 1);
1099 				}
1100 				styler.ColourTo(i, state);
1101 				state = SCE_L_DEFAULT;
1102 			}
1103 			break;
1104 		case SCE_L_COMMENT :
1105 			if (ch == '\r' || ch == '\n') {
1106 				styler.ColourTo(i - 1, state);
1107 				state = SCE_L_DEFAULT;
1108 			}
1109 		}
1110 	}
1111 	styler.ColourTo(lengthDoc-1, state);
1112 }
1113 
1114 static const char * const batchWordListDesc[] = {
1115 	"Internal Commands",
1116 	"External Commands",
1117 	0
1118 };
1119 
1120 static const char * const emptyWordListDesc[] = {
1121 	0
1122 };
1123 
ColouriseNullDoc(unsigned int startPos,int length,int,WordList * [],Accessor & styler)1124 static void ColouriseNullDoc(unsigned int startPos, int length, int, WordList *[],
1125                             Accessor &styler) {
1126 	// Null language means all style bytes are 0 so just mark the end - no need to fill in.
1127 	if (length > 0) {
1128 		styler.StartAt(startPos + length - 1);
1129 		styler.StartSegment(startPos + length - 1);
1130 		styler.ColourTo(startPos + length - 1, 0);
1131 	}
1132 }
1133 
1134 LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc, "batch", 0, batchWordListDesc);
1135 LexerModule lmDiff(SCLEX_DIFF, ColouriseDiffDoc, "diff", FoldDiffDoc, emptyWordListDesc);
1136 LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc, "props", FoldPropsDoc, emptyWordListDesc);
1137 LexerModule lmMake(SCLEX_MAKEFILE, ColouriseMakeDoc, "makefile", 0, emptyWordListDesc);
1138 LexerModule lmErrorList(SCLEX_ERRORLIST, ColouriseErrorListDoc, "errorlist", 0, emptyWordListDesc);
1139 LexerModule lmLatex(SCLEX_LATEX, ColouriseLatexDoc, "latex", 0, emptyWordListDesc);
1140 LexerModule lmNull(SCLEX_NULL, ColouriseNullDoc, "null");
1141