PageRenderTime 298ms CodeModel.GetById 61ms app.highlight 206ms RepoModel.GetById 15ms app.codeStats 1ms

/lexers/LexOthers.cxx

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