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