PageRenderTime 11ms CodeModel.GetById 161ms app.highlight 873ms RepoModel.GetById 174ms app.codeStats 1ms

/lexers/LexOthers.cxx

https://bitbucket.org/myrror/scintilla
C++ | 1493 lines | 1323 code | 32 blank | 138 comment | 276 complexity | 50168c5f225bcca29e794a428caa79ef 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
 503static void ColouriseDiffLine(char *lineBuffer, int endLine, Accessor &styler) {
 504	// It is needed to remember the current state to recognize starting
 505	// comment lines before the first "diff " or "--- ". If a real
 506	// difference starts then each line starting with ' ' is a whitespace
 507	// otherwise it is considered a comment (Only in..., Binary file...)
 508	if (0 == strncmp(lineBuffer, "diff ", 5)) {
 509		styler.ColourTo(endLine, SCE_DIFF_COMMAND);
 510	} else if (0 == strncmp(lineBuffer, "Index: ", 7)) {  // For subversion's diff
 511		styler.ColourTo(endLine, SCE_DIFF_COMMAND);
 512	} else if (0 == strncmp(lineBuffer, "---", 3) && lineBuffer[3] != '-') {
 513		// In a context diff, --- appears in both the header and the position markers
 514		if (lineBuffer[3] == ' ' && atoi(lineBuffer + 4) && !strchr(lineBuffer, '/'))
 515			styler.ColourTo(endLine, SCE_DIFF_POSITION);
 516		else if (lineBuffer[3] == '\r' || lineBuffer[3] == '\n')
 517			styler.ColourTo(endLine, SCE_DIFF_POSITION);
 518		else
 519			styler.ColourTo(endLine, SCE_DIFF_HEADER);
 520	} else if (0 == strncmp(lineBuffer, "+++ ", 4)) {
 521		// I don't know of any diff where "+++ " is a position marker, but for
 522		// consistency, do the same as with "--- " and "*** ".
 523		if (atoi(lineBuffer+4) && !strchr(lineBuffer, '/'))
 524			styler.ColourTo(endLine, SCE_DIFF_POSITION);
 525		else
 526			styler.ColourTo(endLine, SCE_DIFF_HEADER);
 527	} else if (0 == strncmp(lineBuffer, "====", 4)) {  // For p4's diff
 528		styler.ColourTo(endLine, SCE_DIFF_HEADER);
 529	} else if (0 == strncmp(lineBuffer, "***", 3)) {
 530		// In a context diff, *** appears in both the header and the position markers.
 531		// Also ******** is a chunk header, but here it's treated as part of the
 532		// position marker since there is no separate style for a chunk header.
 533		if (lineBuffer[3] == ' ' && atoi(lineBuffer+4) && !strchr(lineBuffer, '/'))
 534			styler.ColourTo(endLine, SCE_DIFF_POSITION);
 535		else if (lineBuffer[3] == '*')
 536			styler.ColourTo(endLine, SCE_DIFF_POSITION);
 537		else
 538			styler.ColourTo(endLine, SCE_DIFF_HEADER);
 539	} else if (0 == strncmp(lineBuffer, "? ", 2)) {    // For difflib
 540		styler.ColourTo(endLine, SCE_DIFF_HEADER);
 541	} else if (lineBuffer[0] == '@') {
 542		styler.ColourTo(endLine, SCE_DIFF_POSITION);
 543	} else if (lineBuffer[0] >= '0' && lineBuffer[0] <= '9') {
 544		styler.ColourTo(endLine, SCE_DIFF_POSITION);
 545	} else if (lineBuffer[0] == '-' || lineBuffer[0] == '<') {
 546		styler.ColourTo(endLine, SCE_DIFF_DELETED);
 547	} else if (lineBuffer[0] == '+' || lineBuffer[0] == '>') {
 548		styler.ColourTo(endLine, SCE_DIFF_ADDED);
 549	} else if (lineBuffer[0] == '!') {
 550		styler.ColourTo(endLine, SCE_DIFF_CHANGED);
 551	} else if (lineBuffer[0] != ' ') {
 552		styler.ColourTo(endLine, SCE_DIFF_COMMENT);
 553	} else {
 554		styler.ColourTo(endLine, SCE_DIFF_DEFAULT);
 555	}
 556}
 557
 558static void ColouriseDiffDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
 559	char lineBuffer[1024];
 560	styler.StartAt(startPos);
 561	styler.StartSegment(startPos);
 562	unsigned int linePos = 0;
 563	for (unsigned int i = startPos; i < startPos + length; i++) {
 564		lineBuffer[linePos++] = styler[i];
 565		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
 566			// End of line (or of line buffer) met, colourise it
 567			lineBuffer[linePos] = '\0';
 568			ColouriseDiffLine(lineBuffer, i, styler);
 569			linePos = 0;
 570		}
 571	}
 572	if (linePos > 0) {	// Last line does not have ending characters
 573		ColouriseDiffLine(lineBuffer, startPos + length - 1, styler);
 574	}
 575}
 576
 577static void FoldDiffDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
 578	int curLine = styler.GetLine(startPos);
 579	int curLineStart = styler.LineStart(curLine);
 580	int prevLevel = curLine > 0 ? styler.LevelAt(curLine - 1) : SC_FOLDLEVELBASE;
 581	int nextLevel;
 582
 583	do {
 584		int lineType = styler.StyleAt(curLineStart);
 585		if (lineType == SCE_DIFF_COMMAND)
 586			nextLevel = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
 587		else if (lineType == SCE_DIFF_HEADER)
 588			nextLevel = (SC_FOLDLEVELBASE + 1) | SC_FOLDLEVELHEADERFLAG;
 589		else if (lineType == SCE_DIFF_POSITION && styler[curLineStart] != '-')
 590			nextLevel = (SC_FOLDLEVELBASE + 2) | SC_FOLDLEVELHEADERFLAG;
 591		else if (prevLevel & SC_FOLDLEVELHEADERFLAG)
 592			nextLevel = (prevLevel & SC_FOLDLEVELNUMBERMASK) + 1;
 593		else
 594			nextLevel = prevLevel;
 595
 596		if ((nextLevel & SC_FOLDLEVELHEADERFLAG) && (nextLevel == prevLevel))
 597			styler.SetLevel(curLine-1, prevLevel & ~SC_FOLDLEVELHEADERFLAG);
 598
 599		styler.SetLevel(curLine, nextLevel);
 600		prevLevel = nextLevel;
 601
 602		curLineStart = styler.LineStart(++curLine);
 603	} while (static_cast<int>(startPos) + length > curLineStart);
 604}
 605
 606static void ColourisePoLine(
 607    char *lineBuffer,
 608    unsigned int lengthLine,
 609    unsigned int startLine,
 610    unsigned int endPos,
 611    Accessor &styler) {
 612
 613	unsigned int i = 0;
 614	static unsigned int state = SCE_PO_DEFAULT;
 615	unsigned int state_start = SCE_PO_DEFAULT;
 616
 617	while ((i < lengthLine) && isspacechar(lineBuffer[i]))	// Skip initial spaces
 618		i++;
 619	if (i < lengthLine) {
 620		if (lineBuffer[i] == '#') {
 621			// check if the comment contains any flags ("#, ") and
 622			// then whether the flags contain "fuzzy"
 623			if (strstart(lineBuffer, "#, ") && strstr(lineBuffer, "fuzzy"))
 624				styler.ColourTo(endPos, SCE_PO_FUZZY);
 625			else
 626				styler.ColourTo(endPos, SCE_PO_COMMENT);
 627		} else {
 628			if (lineBuffer[0] == '"') {
 629				// line continuation, use previous style
 630				styler.ColourTo(endPos, state);
 631				return;
 632			// this implicitly also matches "msgid_plural"
 633			} else if (strstart(lineBuffer, "msgid")) {
 634				state_start = SCE_PO_MSGID;
 635				state = SCE_PO_MSGID_TEXT;
 636			} else if (strstart(lineBuffer, "msgstr")) {
 637				state_start = SCE_PO_MSGSTR;
 638				state = SCE_PO_MSGSTR_TEXT;
 639			} else if (strstart(lineBuffer, "msgctxt")) {
 640				state_start = SCE_PO_MSGCTXT;
 641				state = SCE_PO_MSGCTXT_TEXT;
 642			}
 643			if (state_start != SCE_PO_DEFAULT) {
 644				// find the next space
 645				while ((i < lengthLine) && ! isspacechar(lineBuffer[i]))
 646					i++;
 647				styler.ColourTo(startLine + i - 1, state_start);
 648				styler.ColourTo(startLine + i, SCE_PO_DEFAULT);
 649				styler.ColourTo(endPos, state);
 650			}
 651		}
 652	} else {
 653		styler.ColourTo(endPos, SCE_PO_DEFAULT);
 654	}
 655}
 656
 657static void ColourisePoDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
 658	char lineBuffer[1024];
 659	styler.StartAt(startPos);
 660	styler.StartSegment(startPos);
 661	unsigned int linePos = 0;
 662	unsigned int startLine = startPos;
 663	for (unsigned int i = startPos; i < startPos + length; i++) {
 664		lineBuffer[linePos++] = styler[i];
 665		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
 666			// End of line (or of line buffer) met, colourise it
 667			lineBuffer[linePos] = '\0';
 668			ColourisePoLine(lineBuffer, linePos, startLine, i, styler);
 669			linePos = 0;
 670			startLine = i + 1;
 671		}
 672	}
 673	if (linePos > 0) {	// Last line does not have ending characters
 674		ColourisePoLine(lineBuffer, linePos, startLine, startPos + length - 1, styler);
 675	}
 676}
 677
 678static inline bool isassignchar(unsigned char ch) {
 679	return (ch == '=') || (ch == ':');
 680}
 681
 682static void ColourisePropsLine(
 683    char *lineBuffer,
 684    unsigned int lengthLine,
 685    unsigned int startLine,
 686    unsigned int endPos,
 687    Accessor &styler,
 688    bool allowInitialSpaces) {
 689
 690	unsigned int i = 0;
 691	if (allowInitialSpaces) {
 692		while ((i < lengthLine) && isspacechar(lineBuffer[i]))	// Skip initial spaces
 693			i++;
 694	} else {
 695		if (isspacechar(lineBuffer[i])) // don't allow initial spaces
 696			i = lengthLine;
 697	}
 698
 699	if (i < lengthLine) {
 700		if (lineBuffer[i] == '#' || lineBuffer[i] == '!' || lineBuffer[i] == ';') {
 701			styler.ColourTo(endPos, SCE_PROPS_COMMENT);
 702		} else if (lineBuffer[i] == '[') {
 703			styler.ColourTo(endPos, SCE_PROPS_SECTION);
 704		} else if (lineBuffer[i] == '@') {
 705			styler.ColourTo(startLine + i, SCE_PROPS_DEFVAL);
 706			if (isassignchar(lineBuffer[i++]))
 707				styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT);
 708			styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
 709		} else {
 710			// Search for the '=' character
 711			while ((i < lengthLine) && !isassignchar(lineBuffer[i]))
 712				i++;
 713			if ((i < lengthLine) && isassignchar(lineBuffer[i])) {
 714				styler.ColourTo(startLine + i - 1, SCE_PROPS_KEY);
 715				styler.ColourTo(startLine + i, SCE_PROPS_ASSIGNMENT);
 716				styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
 717			} else {
 718				styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
 719			}
 720		}
 721	} else {
 722		styler.ColourTo(endPos, SCE_PROPS_DEFAULT);
 723	}
 724}
 725
 726static void ColourisePropsDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
 727	char lineBuffer[1024];
 728	styler.StartAt(startPos);
 729	styler.StartSegment(startPos);
 730	unsigned int linePos = 0;
 731	unsigned int startLine = startPos;
 732
 733	// property lexer.props.allow.initial.spaces
 734	//	For properties files, set to 0 to style all lines that start with whitespace in the default style.
 735	//	This is not suitable for SciTE .properties files which use indentation for flow control but
 736	//	can be used for RFC2822 text where indentation is used for continuation lines.
 737	bool allowInitialSpaces = styler.GetPropertyInt("lexer.props.allow.initial.spaces", 1) != 0;
 738
 739	for (unsigned int i = startPos; i < startPos + length; i++) {
 740		lineBuffer[linePos++] = styler[i];
 741		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
 742			// End of line (or of line buffer) met, colourise it
 743			lineBuffer[linePos] = '\0';
 744			ColourisePropsLine(lineBuffer, linePos, startLine, i, styler, allowInitialSpaces);
 745			linePos = 0;
 746			startLine = i + 1;
 747		}
 748	}
 749	if (linePos > 0) {	// Last line does not have ending characters
 750		ColourisePropsLine(lineBuffer, linePos, startLine, startPos + length - 1, styler, allowInitialSpaces);
 751	}
 752}
 753
 754// adaption by ksc, using the "} else {" trick of 1.53
 755// 030721
 756static void FoldPropsDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
 757	bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
 758
 759	unsigned int endPos = startPos + length;
 760	int visibleChars = 0;
 761	int lineCurrent = styler.GetLine(startPos);
 762
 763	char chNext = styler[startPos];
 764	int styleNext = styler.StyleAt(startPos);
 765	bool headerPoint = false;
 766	int lev;
 767
 768	for (unsigned int i = startPos; i < endPos; i++) {
 769		char ch = chNext;
 770		chNext = styler[i+1];
 771
 772		int style = styleNext;
 773		styleNext = styler.StyleAt(i + 1);
 774		bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
 775
 776		if (style == SCE_PROPS_SECTION) {
 777			headerPoint = true;
 778		}
 779
 780		if (atEOL) {
 781			lev = SC_FOLDLEVELBASE;
 782
 783			if (lineCurrent > 0) {
 784				int levelPrevious = styler.LevelAt(lineCurrent - 1);
 785
 786				if (levelPrevious & SC_FOLDLEVELHEADERFLAG) {
 787					lev = SC_FOLDLEVELBASE + 1;
 788				} else {
 789					lev = levelPrevious & SC_FOLDLEVELNUMBERMASK;
 790				}
 791			}
 792
 793			if (headerPoint) {
 794				lev = SC_FOLDLEVELBASE;
 795			}
 796			if (visibleChars == 0 && foldCompact)
 797				lev |= SC_FOLDLEVELWHITEFLAG;
 798
 799			if (headerPoint) {
 800				lev |= SC_FOLDLEVELHEADERFLAG;
 801			}
 802			if (lev != styler.LevelAt(lineCurrent)) {
 803				styler.SetLevel(lineCurrent, lev);
 804			}
 805
 806			lineCurrent++;
 807			visibleChars = 0;
 808			headerPoint = false;
 809		}
 810		if (!isspacechar(ch))
 811			visibleChars++;
 812	}
 813
 814	if (lineCurrent > 0) {
 815		int levelPrevious = styler.LevelAt(lineCurrent - 1);
 816		if (levelPrevious & SC_FOLDLEVELHEADERFLAG) {
 817			lev = SC_FOLDLEVELBASE + 1;
 818		} else {
 819			lev = levelPrevious & SC_FOLDLEVELNUMBERMASK;
 820		}
 821	} else {
 822		lev = SC_FOLDLEVELBASE;
 823	}
 824	int flagsNext = styler.LevelAt(lineCurrent);
 825	styler.SetLevel(lineCurrent, lev | (flagsNext & ~SC_FOLDLEVELNUMBERMASK));
 826}
 827
 828static void ColouriseMakeLine(
 829    char *lineBuffer,
 830    unsigned int lengthLine,
 831    unsigned int startLine,
 832    unsigned int endPos,
 833    Accessor &styler) {
 834
 835	unsigned int i = 0;
 836	int lastNonSpace = -1;
 837	unsigned int state = SCE_MAKE_DEFAULT;
 838	bool bSpecial = false;
 839
 840	// check for a tab character in column 0 indicating a command
 841	bool bCommand = false;
 842	if ((lengthLine > 0) && (lineBuffer[0] == '\t'))
 843		bCommand = true;
 844
 845	// Skip initial spaces
 846	while ((i < lengthLine) && isspacechar(lineBuffer[i])) {
 847		i++;
 848	}
 849	if (lineBuffer[i] == '#') {	// Comment
 850		styler.ColourTo(endPos, SCE_MAKE_COMMENT);
 851		return;
 852	}
 853	if (lineBuffer[i] == '!') {	// Special directive
 854		styler.ColourTo(endPos, SCE_MAKE_PREPROCESSOR);
 855		return;
 856	}
 857	int varCount = 0;
 858	while (i < lengthLine) {
 859		if (lineBuffer[i] == '$' && lineBuffer[i + 1] == '(') {
 860			styler.ColourTo(startLine + i - 1, state);
 861			state = SCE_MAKE_IDENTIFIER;
 862			varCount++;
 863		} else if (state == SCE_MAKE_IDENTIFIER && lineBuffer[i] == ')') {
 864			if (--varCount == 0) {
 865				styler.ColourTo(startLine + i, state);
 866				state = SCE_MAKE_DEFAULT;
 867			}
 868		}
 869
 870		// skip identifier and target styling if this is a command line
 871		if (!bSpecial && !bCommand) {
 872			if (lineBuffer[i] == ':') {
 873				if (((i + 1) < lengthLine) && (lineBuffer[i + 1] == '=')) {
 874					// it's a ':=', so style as an identifier
 875					if (lastNonSpace >= 0)
 876						styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_IDENTIFIER);
 877					styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT);
 878					styler.ColourTo(startLine + i + 1, SCE_MAKE_OPERATOR);
 879				} else {
 880					// We should check that no colouring was made since the beginning of the line,
 881					// to avoid colouring stuff like /OUT:file
 882					if (lastNonSpace >= 0)
 883						styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_TARGET);
 884					styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT);
 885					styler.ColourTo(startLine + i, SCE_MAKE_OPERATOR);
 886				}
 887				bSpecial = true;	// Only react to the first ':' of the line
 888				state = SCE_MAKE_DEFAULT;
 889			} else if (lineBuffer[i] == '=') {
 890				if (lastNonSpace >= 0)
 891					styler.ColourTo(startLine + lastNonSpace, SCE_MAKE_IDENTIFIER);
 892				styler.ColourTo(startLine + i - 1, SCE_MAKE_DEFAULT);
 893				styler.ColourTo(startLine + i, SCE_MAKE_OPERATOR);
 894				bSpecial = true;	// Only react to the first '=' of the line
 895				state = SCE_MAKE_DEFAULT;
 896			}
 897		}
 898		if (!isspacechar(lineBuffer[i])) {
 899			lastNonSpace = i;
 900		}
 901		i++;
 902	}
 903	if (state == SCE_MAKE_IDENTIFIER) {
 904		styler.ColourTo(endPos, SCE_MAKE_IDEOL);	// Error, variable reference not ended
 905	} else {
 906		styler.ColourTo(endPos, SCE_MAKE_DEFAULT);
 907	}
 908}
 909
 910static void ColouriseMakeDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
 911	char lineBuffer[1024];
 912	styler.StartAt(startPos);
 913	styler.StartSegment(startPos);
 914	unsigned int linePos = 0;
 915	unsigned int startLine = startPos;
 916	for (unsigned int i = startPos; i < startPos + length; i++) {
 917		lineBuffer[linePos++] = styler[i];
 918		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
 919			// End of line (or of line buffer) met, colourise it
 920			lineBuffer[linePos] = '\0';
 921			ColouriseMakeLine(lineBuffer, linePos, startLine, i, styler);
 922			linePos = 0;
 923			startLine = i + 1;
 924		}
 925	}
 926	if (linePos > 0) {	// Last line does not have ending characters
 927		ColouriseMakeLine(lineBuffer, linePos, startLine, startPos + length - 1, styler);
 928	}
 929}
 930
 931static int RecogniseErrorListLine(const char *lineBuffer, unsigned int lengthLine, int &startValue) {
 932	if (lineBuffer[0] == '>') {
 933		// Command or return status
 934		return SCE_ERR_CMD;
 935	} else if (lineBuffer[0] == '<') {
 936		// Diff removal.
 937		return SCE_ERR_DIFF_DELETION;
 938	} else if (lineBuffer[0] == '!') {
 939		return SCE_ERR_DIFF_CHANGED;
 940	} else if (lineBuffer[0] == '+') {
 941		if (strstart(lineBuffer, "+++ ")) {
 942			return SCE_ERR_DIFF_MESSAGE;
 943		} else {
 944			return SCE_ERR_DIFF_ADDITION;
 945		}
 946	} else if (lineBuffer[0] == '-') {
 947		if (strstart(lineBuffer, "--- ")) {
 948			return SCE_ERR_DIFF_MESSAGE;
 949		} else {
 950			return SCE_ERR_DIFF_DELETION;
 951		}
 952	} else if (strstart(lineBuffer, "cf90-")) {
 953		// Absoft Pro Fortran 90/95 v8.2 error and/or warning message
 954		return SCE_ERR_ABSF;
 955	} else if (strstart(lineBuffer, "fortcom:")) {
 956		// Intel Fortran Compiler v8.0 error/warning message
 957		return SCE_ERR_IFORT;
 958	} else if (strstr(lineBuffer, "File \"") && strstr(lineBuffer, ", line ")) {
 959		return SCE_ERR_PYTHON;
 960	} else if (strstr(lineBuffer, " in ") && strstr(lineBuffer, " on line ")) {
 961		return SCE_ERR_PHP;
 962	} else if ((strstart(lineBuffer, "Error ") ||
 963	            strstart(lineBuffer, "Warning ")) &&
 964	           strstr(lineBuffer, " at (") &&
 965	           strstr(lineBuffer, ") : ") &&
 966	           (strstr(lineBuffer, " at (") < strstr(lineBuffer, ") : "))) {
 967		// Intel Fortran Compiler error/warning message
 968		return SCE_ERR_IFC;
 969	} else if (strstart(lineBuffer, "Error ")) {
 970		// Borland error message
 971		return SCE_ERR_BORLAND;
 972	} else if (strstart(lineBuffer, "Warning ")) {
 973		// Borland warning message
 974		return SCE_ERR_BORLAND;
 975	} else if (strstr(lineBuffer, "at line ") &&
 976	        (strstr(lineBuffer, "at line ") < (lineBuffer + lengthLine)) &&
 977	           strstr(lineBuffer, "file ") &&
 978	           (strstr(lineBuffer, "file ") < (lineBuffer + lengthLine))) {
 979		// Lua 4 error message
 980		return SCE_ERR_LUA;
 981	} else if (strstr(lineBuffer, " at ") &&
 982	        (strstr(lineBuffer, " at ") < (lineBuffer + lengthLine)) &&
 983	           strstr(lineBuffer, " line ") &&
 984	           (strstr(lineBuffer, " line ") < (lineBuffer + lengthLine)) &&
 985	        (strstr(lineBuffer, " at ") < (strstr(lineBuffer, " line ")))) {
 986		// perl error message
 987		return SCE_ERR_PERL;
 988	} else if ((memcmp(lineBuffer, "   at ", 6) == 0) &&
 989	           strstr(lineBuffer, ":line ")) {
 990		// A .NET traceback
 991		return SCE_ERR_NET;
 992	} else if (strstart(lineBuffer, "Line ") &&
 993	           strstr(lineBuffer, ", file ")) {
 994		// Essential Lahey Fortran error message
 995		return SCE_ERR_ELF;
 996	} else if (strstart(lineBuffer, "line ") &&
 997	           strstr(lineBuffer, " column ")) {
 998		// HTML tidy style: line 42 column 1
 999		return SCE_ERR_TIDY;
1000	} else if (strstart(lineBuffer, "\tat ") &&
1001	           strstr(lineBuffer, "(") &&
1002	           strstr(lineBuffer, ".java:")) {
1003		// Java stack back trace
1004		return SCE_ERR_JAVA_STACK;
1005	} else {
1006		// Look for one of the following formats:
1007		// GCC: <filename>:<line>:<message>
1008		// Microsoft: <filename>(<line>) :<message>
1009		// Common: <filename>(<line>): warning|error|note|remark|catastrophic|fatal
1010		// Common: <filename>(<line>) warning|error|note|remark|catastrophic|fatal
1011		// Microsoft: <filename>(<line>,<column>)<message>
1012		// CTags: \t<message>
1013		// Lua 5 traceback: \t<filename>:<line>:<message>
1014		// Lua 5.1: <exe>: <filename>:<line>:<message>
1015		bool initialTab = (lineBuffer[0] == '\t');
1016		bool initialColonPart = false;
1017		enum { stInitial,
1018			stGccStart, stGccDigit, stGccColumn, stGcc,
1019			stMsStart, stMsDigit, stMsBracket, stMsVc, stMsDigitComma, stMsDotNet,
1020			stCtagsStart, stCtagsStartString, stCtagsStringDollar, stCtags,
1021			stUnrecognized
1022		} state = stInitial;
1023		for (unsigned int i = 0; i < lengthLine; i++) {
1024			char ch = lineBuffer[i];
1025			char chNext = ' ';
1026			if ((i + 1) < lengthLine)
1027				chNext = lineBuffer[i + 1];
1028			if (state == stInitial) {
1029				if (ch == ':') {
1030					// May be GCC, or might be Lua 5 (Lua traceback same but with tab prefix)
1031					if ((chNext != '\\') && (chNext != '/') && (chNext != ' ')) {
1032						// This check is not completely accurate as may be on
1033						// GTK+ with a file name that includes ':'.
1034						state = stGccStart;
1035					} else if (chNext == ' ') { // indicates a Lua 5.1 error message
1036						initialColonPart = true;
1037					}
1038				} else if ((ch == '(') && Is1To9(chNext) && (!initialTab)) {
1039					// May be Microsoft
1040					// Check against '0' often removes phone numbers
1041					state = stMsStart;
1042				} else if ((ch == '\t') && (!initialTab)) {
1043					// May be CTags
1044					state = stCtagsStart;
1045				}
1046			} else if (state == stGccStart) {	// <filename>:
1047				state = Is1To9(ch) ? stGccDigit : stUnrecognized;
1048			} else if (state == stGccDigit) {	// <filename>:<line>
1049				if (ch == ':') {
1050					state = stGccColumn;	// :9.*: is GCC
1051					startValue = i + 1;
1052				} else if (!Is0To9(ch)) {
1053					state = stUnrecognized;
1054				}
1055			} else if (state == stGccColumn) {	// <filename>:<line>:<column>
1056				if (!Is0To9(ch)) {
1057					state = stGcc;
1058					if (ch == ':')
1059						startValue = i + 1;
1060					break;
1061				}
1062			} else if (state == stMsStart) {	// <filename>(
1063				state = Is0To9(ch) ? stMsDigit : stUnrecognized;
1064			} else if (state == stMsDigit) {	// <filename>(<line>
1065				if (ch == ',') {
1066					state = stMsDigitComma;
1067				} else if (ch == ')') {
1068					state = stMsBracket;
1069				} else if ((ch != ' ') && !Is0To9(ch)) {
1070					state = stUnrecognized;
1071				}
1072			} else if (state == stMsBracket) {	// <filename>(<line>)
1073				if ((ch == ' ') && (chNext == ':')) {
1074					state = stMsVc;
1075				} else if ((ch == ':' && chNext == ' ') || (ch == ' ')) {
1076					// Possibly Delphi.. don't test against chNext as it's one of the strings below.
1077					char word[512];
1078					unsigned int j, chPos;
1079					unsigned numstep;
1080					chPos = 0;
1081					if (ch == ' ')
1082						numstep = 1; // ch was ' ', handle as if it's a delphi errorline, only add 1 to i.
1083					else
1084						numstep = 2; // otherwise add 2.
1085					for (j = i + numstep; j < lengthLine && IsAlphabetic(lineBuffer[j]) && chPos < sizeof(word) - 1; j++)
1086						word[chPos++] = lineBuffer[j];
1087					word[chPos] = 0;
1088					if (!CompareCaseInsensitive(word, "error") || !CompareCaseInsensitive(word, "warning") ||
1089						!CompareCaseInsensitive(word, "fatal") || !CompareCaseInsensitive(word, "catastrophic") ||
1090						!CompareCaseInsensitive(word, "note") || !CompareCaseInsensitive(word, "remark")) {
1091						state = stMsVc;
1092					} else
1093						state = stUnrecognized;
1094				} else {
1095					state = stUnrecognized;
1096				}
1097			} else if (state == stMsDigitComma) {	// <filename>(<line>,
1098				if (ch == ')') {
1099					state = stMsDotNet;
1100					break;
1101				} else if ((ch != ' ') && !Is0To9(ch)) {
1102					state = stUnrecognized;
1103				}
1104			} else if (state == stCtagsStart) {
1105				if ((lineBuffer[i - 1] == '\t') &&
1106				        ((ch == '/' && lineBuffer[i + 1] == '^') || Is0To9(ch))) {
1107					state = stCtags;
1108					break;
1109				} else if ((ch == '/') && (lineBuffer[i + 1] == '^')) {
1110					state = stCtagsStartString;
1111				}
1112			} else if ((state == stCtagsStartString) && ((lineBuffer[i] == '$') && (lineBuffer[i + 1] == '/'))) {
1113				state = stCtagsStringDollar;
1114				break;
1115			}
1116		}
1117		if (state == stGcc) {
1118			return initialColonPart ? SCE_ERR_LUA : SCE_ERR_GCC;
1119		} else if ((state == stMsVc) || (state == stMsDotNet)) {
1120			return SCE_ERR_MS;
1121		} else if ((state == stCtagsStringDollar) || (state == stCtags)) {
1122			return SCE_ERR_CTAG;
1123		} else {
1124			return SCE_ERR_DEFAULT;
1125		}
1126	}
1127}
1128
1129static void ColouriseErrorListLine(
1130    char *lineBuffer,
1131    unsigned int lengthLine,
1132    unsigned int endPos,
1133    Accessor &styler,
1134	bool valueSeparate) {
1135	int startValue = -1;
1136	int style = RecogniseErrorListLine(lineBuffer, lengthLine, startValue);
1137	if (valueSeparate && (startValue >= 0)) {
1138		styler.ColourTo(endPos - (lengthLine - startValue), style);
1139		styler.ColourTo(endPos, SCE_ERR_VALUE);
1140	} else {
1141		styler.ColourTo(endPos, style);
1142	}
1143}
1144
1145static void ColouriseErrorListDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler) {
1146	char lineBuffer[10000];
1147	styler.StartAt(startPos);
1148	styler.StartSegment(startPos);
1149	unsigned int linePos = 0;
1150
1151	// property lexer.errorlist.value.separate
1152	//	For lines in the output pane that are matches from Find in Files or GCC-style
1153	//	diagnostics, style the path and line number separately from the rest of the
1154	//	line with style 21 used for the rest of the line.
1155	//	This allows matched text to be more easily distinguished from its location.
1156	bool valueSeparate = styler.GetPropertyInt("lexer.errorlist.value.separate", 0) != 0;
1157	for (unsigned int i = startPos; i < startPos + length; i++) {
1158		lineBuffer[linePos++] = styler[i];
1159		if (AtEOL(styler, i) || (linePos >= sizeof(lineBuffer) - 1)) {
1160			// End of line (or of line buffer) met, colourise it
1161			lineBuffer[linePos] = '\0';
1162			ColouriseErrorListLine(lineBuffer, linePos, i, styler, valueSeparate);
1163			linePos = 0;
1164		}
1165	}
1166	if (linePos > 0) {	// Last line does not have ending characters
1167		ColouriseErrorListLine(lineBuffer, linePos, startPos + length - 1, styler, valueSeparate);
1168	}
1169}
1170
1171static bool latexIsSpecial(int ch) {
1172	return (ch == '#') || (ch == '$') || (ch == '%') || (ch == '&') || (ch == '_') ||
1173	       (ch == '{') || (ch == '}') || (ch == ' ');
1174}
1175
1176static bool latexIsBlank(int ch) {
1177	return (ch == ' ') || (ch == '\t');
1178}
1179
1180static bool latexIsBlankAndNL(int ch) {
1181	return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n');
1182}
1183
1184static bool latexIsLetter(int ch) {
1185	return isascii(ch) && isalpha(ch);
1186}
1187
1188static bool latexIsTagValid(int &i, int l, Accessor &styler) {
1189	while (i < l) {
1190		if (styler.SafeGetCharAt(i) == '{') {
1191			while (i < l) {
1192				i++;
1193				if (styler.SafeGetCharAt(i) == '}') {
1194					return true;
1195				}	else if (!latexIsLetter(styler.SafeGetCharAt(i)) &&
1196                   styler.SafeGetCharAt(i)!='*') {
1197					return false;
1198				}
1199			}
1200		} else if (!latexIsBlank(styler.SafeGetCharAt(i))) {
1201			return false;
1202		}
1203		i++;
1204	}
1205	return false;
1206}
1207
1208static bool latexNextNotBlankIs(int i, int l, Accessor &styler, char needle) {
1209  char ch;
1210	while (i < l) {
1211    ch = styler.SafeGetCharAt(i);
1212		if (!latexIsBlankAndNL(ch) && ch != '*') {
1213      if (ch == needle)
1214        return true;
1215      else
1216        return false;
1217		}
1218		i++;
1219	}
1220	return false;
1221}
1222
1223static bool latexLastWordIs(int start, Accessor &styler, const char *needle) {
1224  unsigned int i = 0;
1225	unsigned int l = static_cast<unsigned int>(strlen(needle));
1226	int ini = start-l+1;
1227	char s[32];
1228
1229	while (i < l && i < 32) {
1230		s[i] = styler.SafeGetCharAt(ini + i);
1231    i++;
1232	}
1233	s[i] = '\0';
1234
1235	return (strcmp(s, needle) == 0);
1236}
1237
1238static void ColouriseLatexDoc(unsigned int startPos, int length, int initStyle,
1239                              WordList *[], Accessor &styler) {
1240
1241	styler.StartAt(startPos);
1242
1243	int state = initStyle;
1244	char chNext = styler.SafeGetCharAt(startPos);
1245	styler.StartSegment(startPos);
1246	int lengthDoc = startPos + length;
1247  char chVerbatimDelim = '\0';
1248
1249	for (int i = startPos; i < lengthDoc; i++) {
1250		char ch = chNext;
1251		chNext = styler.SafeGetCharAt(i + 1);
1252
1253		if (styler.IsLeadByte(ch)) {
1254			i++;
1255			chNext = styler.SafeGetCharAt(i + 1);
1256			continue;
1257		}
1258
1259		switch (state) {
1260		case SCE_L_DEFAULT :
1261			switch (ch) {
1262			case '\\' :
1263				styler.ColourTo(i - 1, state);
1264				if (latexIsSpecial(chNext)) {
1265					state = SCE_L_SPECIAL;
1266				} else {
1267					if (latexIsLetter(chNext)) {
1268						state = SCE_L_COMMAND;
1269					}	else {
1270						if (chNext == '(' || chNext == '[') {
1271							styler.ColourTo(i-1, state);
1272							styler.ColourTo(i+1, SCE_L_SHORTCMD);
1273							state = SCE_L_MATH;
1274							if (chNext == '[')
1275								state = SCE_L_MATH2;
1276							i++;
1277							chNext = styler.SafeGetCharAt(i+1);
1278						} else {
1279							state = SCE_L_SHORTCMD;
1280						}
1281					}
1282				}
1283				break;
1284			case '$' :
1285				styler.ColourTo(i - 1, state);
1286				state = SCE_L_MATH;
1287				if (chNext == '$') {
1288					state = SCE_L_MATH2;
1289					i++;
1290					chNext = styler.SafeGetCharAt(i + 1);
1291				}
1292				break;
1293			case '%' :
1294				styler.ColourTo(i - 1, state);
1295				state = SCE_L_COMMENT;
1296				break;
1297			}
1298			break;
1299		case SCE_L_ERROR:
1300			styler.ColourTo(i-1, state);
1301			state = SCE_L_DEFAULT;
1302			break;
1303		case SCE_L_SPECIAL:
1304		case SCE_L_SHORTCMD:
1305			styler.ColourTo(i, state);
1306			state = SCE_L_DEFAULT;
1307			break;
1308		case SCE_L_COMMAND :
1309			if (!latexIsLetter(chNext)) {
1310				styler.ColourTo(i, state);
1311				state = SCE_L_DEFAULT;
1312        if (latexNextNotBlankIs(i+1, lengthDoc, styler, '[' )) {
1313          state = SCE_L_CMDOPT;
1314				} else if (latexLastWordIs(i, styler, "\\begin")) {
1315					state = SCE_L_TAG;
1316				} else if (latexLastWordIs(i, styler, "\\end")) {
1317					state = SCE_L_TAG2;
1318				} else if (latexLastWordIs(i, styler, "\\verb") &&
1319                   chNext != '*' && chNext != ' ') {
1320          chVerbatimDelim = chNext;
1321					state = SCE_L_VERBATIM;
1322				}
1323			}
1324			break;
1325		case SCE_L_CMDOPT :
1326      if (ch == ']') {
1327        styler.ColourTo(i, state);
1328        state = SCE_L_DEFAULT;
1329      }
1330			break;
1331		case SCE_L_TAG :
1332			if (latexIsTagValid(i, lengthDoc, styler)) {
1333				styler.ColourTo(i, state);
1334				state = SCE_L_DEFAULT;
1335				if (latexLastWordIs(i, styler, "{verbatim}")) {
1336					state = SCE_L_VERBATIM;
1337				} else if (latexLastWordIs(i, styler, "{comment}")) {
1338					state = SCE_L_COMMENT2;
1339				} else if (latexLastWordIs(i, styler, "{math}")) {
1340					state = SCE_L_MATH;
1341				} else if (latexLastWordIs(i, styler, "{displaymath}")) {
1342					state = SCE_L_MATH2;
1343				} else if (latexLastWordIs(i, styler, "{equation}")) {
1344					state = SCE_L_MATH2;
1345				}
1346			} else {
1347				state = SCE_L_ERROR;
1348				styler.ColourTo(i, state);
1349				state = SCE_L_DEFAULT;
1350			}
1351			chNext = styler.SafeGetCharAt(i+1);
1352			break;
1353		case SCE_L_TAG2 :
1354			if (latexIsTagValid(i, lengthDoc, styler)) {
1355				styler.ColourTo(i, state);
1356				state = SCE_L_DEFAULT;
1357			} else {
1358				state = SCE_L_ERROR;
1359			}
1360			chNext = styler.SafeGetCharAt(i+1);
1361			break;
1362		case SCE_L_MATH :
1363			if (ch == '$') {
1364				styler.ColourTo(i, state);
1365				state = SCE_L_DEFAULT;
1366			} else if (ch == '\\' && chNext == ')') {
1367				styler.ColourTo(i-1, state);
1368				styler.ColourTo(i+1, SCE_L_SHORTCMD);
1369				i++;
1370				chNext = styler.SafeGetCharAt(i+1);
1371				state = SCE_L_DEFAULT;
1372			} else if (ch == '\\') {
1373				int match = i + 3;
1374				if (latexLastWordIs(match, styler, "\\end")) {
1375					match++;
1376					if (latexIsTagValid(match, lengthDoc, styler)) {
1377						if (latexLastWordIs(match, styler, "{math}")) {
1378							styler.ColourTo(i-1, state);
1379							state = SCE_L_COMMAND;
1380						}
1381					}
1382				}
1383			}
1384
1385			break;
1386		case SCE_L_MATH2 :
1387			if (ch == '$') {
1388        if (chNext == '$') {
1389          i++;
1390          chNext = styler.SafeGetCharAt(i + 1);
1391          styler.ColourTo(i, state);
1392          state = SCE_L_DEFAULT;
1393        } else {
1394          styler.ColourTo(i, SCE_L_ERROR);
1395          state = SCE_L_DEFAULT;
1396        }
1397			} else if (ch == '\\' && chNext == ']') {
1398				styler.ColourTo(i-1, state);
1399				styler.ColourTo(i+1, SCE_L_SHORTCMD);
1400				i++;
1401				chNext = styler.SafeGetCharAt(i+1);
1402				state = SCE_L_DEFAULT;
1403			} else if (ch == '\\') {
1404				int match = i + 3;
1405				if (latexLastWordIs(match, styler, "\\end")) {
1406					match++;
1407					if (latexIsTagValid(match, lengthDoc, styler)) {
1408						if (latexLastWordIs(match, styler, "{displaymath}")) {
1409							styler.ColourTo(i-1, state);
1410							state = SCE_L_COMMAND;
1411						} else if (latexLastWordIs(match, styler, "{equation}")) {
1412							styler.ColourTo(i-1, state);
1413							state = SCE_L_COMMAND;
1414						}
1415					}
1416				}
1417			}
1418			break;
1419		case SCE_L_COMMENT :
1420			if (ch == '\r' || ch == '\n') {
1421				styler.ColourTo(i - 1, state);
1422				state = SCE_L_DEFAULT;
1423			}
1424			break;
1425		case SCE_L_COMMENT2 :
1426			if (ch == '\\') {
1427				int match = i + 3;
1428				if (latexLastWordIs(match, styler, "\\end")) {
1429					match++;
1430					if (latexIsTagValid(match, lengthDoc, styler)) {
1431						if (latexLastWordIs(match, styler, "{comment}")) {
1432							styler.ColourTo(i-1, state);
1433							state = SCE_L_COMMAND;
1434						}
1435					}
1436				}
1437			}
1438			break;
1439		case SCE_L_VERBATIM :
1440			if (ch == '\\') {
1441				int match = i + 3;
1442				if (latexLastWordIs(match, styler, "\\end")) {
1443					match++;
1444					if (latexIsTagValid(match, lengthDoc, styler)) {
1445						if (latexLastWordIs(match, styler, "{verbatim}")) {
1446							styler.ColourTo(i-1, state);
1447							state = SCE_L_COMMAND;
1448						}
1449					}
1450				}
1451			} else if (chNext == chVerbatimDelim) {
1452        styler.ColourTo(i+1, state);
1453				state = SCE_L_DEFAULT;
1454        chVerbatimDelim = '\0';
1455      } else if (chVerbatimDelim != '\0' && (ch == '\n' || ch == '\r')) {
1456        styler.ColourTo(i, SCE_L_ERROR);
1457				state = SCE_L_DEFAULT;
1458        chVerbatimDelim = '\0';
1459      }
1460			break;
1461		}
1462	}
1463	styler.ColourTo(lengthDoc-1, state);
1464}
1465
1466static const char *const batchWordListDesc[] = {
1467	"Internal Commands",
1468	"External Commands",
1469	0
1470};
1471
1472static const char *const emptyWordListDesc[] = {
1473	0
1474};
1475
1476static void ColouriseNullDoc(unsigned int startPos, int length, int, WordList *[],
1477                            Accessor &styler) {
1478	// Null language means all style bytes are 0 so just mark the end - no need to fill in.
1479	if (length > 0) {
1480		styler.StartAt(startPos + length - 1);
1481		styler.StartSegment(startPos + length - 1);
1482		styler.ColourTo(startPos + length - 1, 0);
1483	}
1484}
1485
1486LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc, "batch", 0, batchWordListDesc);
1487LexerModule lmDiff(SCLEX_DIFF, ColouriseDiffDoc, "diff", FoldDiffDoc, emptyWordListDesc);
1488LexerModule lmPo(SCLEX_PO, ColourisePoDoc, "po", 0, emptyWordListDesc);
1489LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc, "props", FoldPropsDoc, emptyWordListDesc);
1490LexerModule lmMake(SCLEX_MAKEFILE, ColouriseMakeDoc, "makefile", 0, emptyWordListDesc);
1491LexerModule lmErrorList(SCLEX_ERRORLIST, ColouriseErrorListDoc, "errorlist", 0, emptyWordListDesc);
1492LexerModule lmLatex(SCLEX_LATEX, ColouriseLatexDoc, "latex", 0, emptyWordListDesc);
1493LexerModule lmNull(SCLEX_NULL, ColouriseNullDoc, "null");