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