LexNsis.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // Scintilla source code edit control
  2. /** @file LexNsis.cxx
  3. ** Lexer for NSIS
  4. **/
  5. // Copyright 2003 - 2005 by Angelo Mandato <angelo [at] spaceblue [dot] com>
  6. // Last Updated: 03/13/2005
  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. /*
  27. // located in SciLexer.h
  28. #define SCLEX_NSIS 43
  29. #define SCE_NSIS_DEFAULT 0
  30. #define SCE_NSIS_COMMENT 1
  31. #define SCE_NSIS_STRINGDQ 2
  32. #define SCE_NSIS_STRINGLQ 3
  33. #define SCE_NSIS_STRINGRQ 4
  34. #define SCE_NSIS_FUNCTION 5
  35. #define SCE_NSIS_VARIABLE 6
  36. #define SCE_NSIS_LABEL 7
  37. #define SCE_NSIS_USERDEFINED 8
  38. #define SCE_NSIS_SECTIONDEF 9
  39. #define SCE_NSIS_SUBSECTIONDEF 10
  40. #define SCE_NSIS_IFDEFINEDEF 11
  41. #define SCE_NSIS_MACRODEF 12
  42. #define SCE_NSIS_STRINGVAR 13
  43. #define SCE_NSIS_NUMBER 14
  44. // ADDED for Scintilla v1.63
  45. #define SCE_NSIS_SECTIONGROUP 15
  46. #define SCE_NSIS_PAGEEX 16
  47. #define SCE_NSIS_FUNCTIONDEF 17
  48. #define SCE_NSIS_COMMENTBOX 18
  49. */
  50. static bool isNsisNumber(char ch)
  51. {
  52. return (ch >= '0' && ch <= '9');
  53. }
  54. static bool isNsisChar(char ch)
  55. {
  56. return (ch == '.' ) || (ch == '_' ) || isNsisNumber(ch) || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
  57. }
  58. static bool isNsisLetter(char ch)
  59. {
  60. return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
  61. }
  62. static bool NsisNextLineHasElse(Sci_PositionU start, Sci_PositionU end, Accessor &styler)
  63. {
  64. Sci_Position nNextLine = -1;
  65. for( Sci_PositionU i = start; i < end; i++ )
  66. {
  67. char cNext = styler.SafeGetCharAt( i );
  68. if( cNext == '\n' )
  69. {
  70. nNextLine = i+1;
  71. break;
  72. }
  73. }
  74. if( nNextLine == -1 ) // We never found the next line...
  75. return false;
  76. for( Sci_PositionU firstChar = nNextLine; firstChar < end; firstChar++ )
  77. {
  78. char cNext = styler.SafeGetCharAt( firstChar );
  79. if( cNext == ' ' )
  80. continue;
  81. if( cNext == '\t' )
  82. continue;
  83. if( cNext == '!' )
  84. {
  85. if( styler.Match(firstChar, "!else") )
  86. return true;
  87. }
  88. break;
  89. }
  90. return false;
  91. }
  92. static int NsisCmp( const char *s1, const char *s2, bool bIgnoreCase )
  93. {
  94. if( bIgnoreCase )
  95. return CompareCaseInsensitive( s1, s2);
  96. return strcmp( s1, s2 );
  97. }
  98. static int calculateFoldNsis(Sci_PositionU start, Sci_PositionU end, int foldlevel, Accessor &styler, bool bElse, bool foldUtilityCmd )
  99. {
  100. int style = styler.StyleAt(end);
  101. // If the word is too long, it is not what we are looking for
  102. if( end - start > 20 )
  103. return foldlevel;
  104. if( foldUtilityCmd )
  105. {
  106. // Check the style at this point, if it is not valid, then return zero
  107. if( style != SCE_NSIS_FUNCTIONDEF && style != SCE_NSIS_SECTIONDEF &&
  108. style != SCE_NSIS_SUBSECTIONDEF && style != SCE_NSIS_IFDEFINEDEF &&
  109. style != SCE_NSIS_MACRODEF && style != SCE_NSIS_SECTIONGROUP &&
  110. style != SCE_NSIS_PAGEEX )
  111. return foldlevel;
  112. }
  113. else
  114. {
  115. if( style != SCE_NSIS_FUNCTIONDEF && style != SCE_NSIS_SECTIONDEF &&
  116. style != SCE_NSIS_SUBSECTIONDEF && style != SCE_NSIS_SECTIONGROUP &&
  117. style != SCE_NSIS_PAGEEX )
  118. return foldlevel;
  119. }
  120. int newFoldlevel = foldlevel;
  121. bool bIgnoreCase = false;
  122. if( styler.GetPropertyInt("nsis.ignorecase") == 1 )
  123. bIgnoreCase = true;
  124. char s[20]; // The key word we are looking for has atmost 13 characters
  125. s[0] = '\0';
  126. for (Sci_PositionU i = 0; i < end - start + 1 && i < 19; i++)
  127. {
  128. s[i] = static_cast<char>( styler[ start + i ] );
  129. s[i + 1] = '\0';
  130. }
  131. if( s[0] == '!' )
  132. {
  133. if( NsisCmp(s, "!ifndef", bIgnoreCase) == 0 || NsisCmp(s, "!ifdef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifmacrodef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifmacrondef", bIgnoreCase ) == 0 || NsisCmp(s, "!if", bIgnoreCase ) == 0 || NsisCmp(s, "!macro", bIgnoreCase ) == 0 )
  134. newFoldlevel++;
  135. else if( NsisCmp(s, "!endif", bIgnoreCase) == 0 || NsisCmp(s, "!macroend", bIgnoreCase ) == 0 )
  136. newFoldlevel--;
  137. else if( bElse && NsisCmp(s, "!else", bIgnoreCase) == 0 )
  138. newFoldlevel++;
  139. }
  140. else
  141. {
  142. if( NsisCmp(s, "Section", bIgnoreCase ) == 0 || NsisCmp(s, "SectionGroup", bIgnoreCase ) == 0 || NsisCmp(s, "Function", bIgnoreCase) == 0 || NsisCmp(s, "SubSection", bIgnoreCase ) == 0 || NsisCmp(s, "PageEx", bIgnoreCase ) == 0 )
  143. newFoldlevel++;
  144. else if( NsisCmp(s, "SectionGroupEnd", bIgnoreCase ) == 0 || NsisCmp(s, "SubSectionEnd", bIgnoreCase ) == 0 || NsisCmp(s, "FunctionEnd", bIgnoreCase) == 0 || NsisCmp(s, "SectionEnd", bIgnoreCase ) == 0 || NsisCmp(s, "PageExEnd", bIgnoreCase ) == 0 )
  145. newFoldlevel--;
  146. }
  147. return newFoldlevel;
  148. }
  149. static int classifyWordNsis(Sci_PositionU start, Sci_PositionU end, WordList *keywordLists[], Accessor &styler )
  150. {
  151. bool bIgnoreCase = false;
  152. if( styler.GetPropertyInt("nsis.ignorecase") == 1 )
  153. bIgnoreCase = true;
  154. bool bUserVars = false;
  155. if( styler.GetPropertyInt("nsis.uservars") == 1 )
  156. bUserVars = true;
  157. char s[100];
  158. s[0] = '\0';
  159. s[1] = '\0';
  160. WordList &Functions = *keywordLists[0];
  161. WordList &Variables = *keywordLists[1];
  162. WordList &Lables = *keywordLists[2];
  163. WordList &UserDefined = *keywordLists[3];
  164. for (Sci_PositionU i = 0; i < end - start + 1 && i < 99; i++)
  165. {
  166. if( bIgnoreCase )
  167. s[i] = static_cast<char>( tolower(styler[ start + i ] ) );
  168. else
  169. s[i] = static_cast<char>( styler[ start + i ] );
  170. s[i + 1] = '\0';
  171. }
  172. // Check for special words...
  173. if( NsisCmp(s, "!macro", bIgnoreCase ) == 0 || NsisCmp(s, "!macroend", bIgnoreCase) == 0 ) // Covers !macro and !macroend
  174. return SCE_NSIS_MACRODEF;
  175. if( NsisCmp(s, "!ifdef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifndef", bIgnoreCase) == 0 || NsisCmp(s, "!endif", bIgnoreCase) == 0 ) // Covers !ifdef, !ifndef and !endif
  176. return SCE_NSIS_IFDEFINEDEF;
  177. if( NsisCmp(s, "!if", bIgnoreCase ) == 0 || NsisCmp(s, "!else", bIgnoreCase ) == 0 ) // Covers !if and else
  178. return SCE_NSIS_IFDEFINEDEF;
  179. if (NsisCmp(s, "!ifmacrodef", bIgnoreCase ) == 0 || NsisCmp(s, "!ifmacrondef", bIgnoreCase ) == 0 ) // Covers !ifmacrodef and !ifnmacrodef
  180. return SCE_NSIS_IFDEFINEDEF;
  181. if( NsisCmp(s, "SectionGroup", bIgnoreCase) == 0 || NsisCmp(s, "SectionGroupEnd", bIgnoreCase) == 0 ) // Covers SectionGroup and SectionGroupEnd
  182. return SCE_NSIS_SECTIONGROUP;
  183. if( NsisCmp(s, "Section", bIgnoreCase ) == 0 || NsisCmp(s, "SectionEnd", bIgnoreCase) == 0 ) // Covers Section and SectionEnd
  184. return SCE_NSIS_SECTIONDEF;
  185. if( NsisCmp(s, "SubSection", bIgnoreCase) == 0 || NsisCmp(s, "SubSectionEnd", bIgnoreCase) == 0 ) // Covers SubSection and SubSectionEnd
  186. return SCE_NSIS_SUBSECTIONDEF;
  187. if( NsisCmp(s, "PageEx", bIgnoreCase) == 0 || NsisCmp(s, "PageExEnd", bIgnoreCase) == 0 ) // Covers PageEx and PageExEnd
  188. return SCE_NSIS_PAGEEX;
  189. if( NsisCmp(s, "Function", bIgnoreCase) == 0 || NsisCmp(s, "FunctionEnd", bIgnoreCase) == 0 ) // Covers Function and FunctionEnd
  190. return SCE_NSIS_FUNCTIONDEF;
  191. if ( Functions.InList(s) )
  192. return SCE_NSIS_FUNCTION;
  193. if ( Variables.InList(s) )
  194. return SCE_NSIS_VARIABLE;
  195. if ( Lables.InList(s) )
  196. return SCE_NSIS_LABEL;
  197. if( UserDefined.InList(s) )
  198. return SCE_NSIS_USERDEFINED;
  199. if( strlen(s) > 3 )
  200. {
  201. if( s[1] == '{' && s[strlen(s)-1] == '}' )
  202. return SCE_NSIS_VARIABLE;
  203. }
  204. // See if the variable is a user defined variable
  205. if( s[0] == '$' && bUserVars )
  206. {
  207. bool bHasSimpleNsisChars = true;
  208. for (Sci_PositionU j = 1; j < end - start + 1 && j < 99; j++)
  209. {
  210. if( !isNsisChar( s[j] ) )
  211. {
  212. bHasSimpleNsisChars = false;
  213. break;
  214. }
  215. }
  216. if( bHasSimpleNsisChars )
  217. return SCE_NSIS_VARIABLE;
  218. }
  219. // To check for numbers
  220. if( isNsisNumber( s[0] ) )
  221. {
  222. bool bHasSimpleNsisNumber = true;
  223. for (Sci_PositionU j = 1; j < end - start + 1 && j < 99; j++)
  224. {
  225. if( !isNsisNumber( s[j] ) )
  226. {
  227. bHasSimpleNsisNumber = false;
  228. break;
  229. }
  230. }
  231. if( bHasSimpleNsisNumber )
  232. return SCE_NSIS_NUMBER;
  233. }
  234. return SCE_NSIS_DEFAULT;
  235. }
  236. static void ColouriseNsisDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *keywordLists[], Accessor &styler)
  237. {
  238. int state = SCE_NSIS_DEFAULT;
  239. if( startPos > 0 )
  240. state = styler.StyleAt(startPos-1); // Use the style from the previous line, usually default, but could be commentbox
  241. styler.StartAt( startPos );
  242. styler.GetLine( startPos );
  243. Sci_PositionU nLengthDoc = startPos + length;
  244. styler.StartSegment( startPos );
  245. char cCurrChar;
  246. bool bVarInString = false;
  247. bool bClassicVarInString = false;
  248. Sci_PositionU i;
  249. for( i = startPos; i < nLengthDoc; i++ )
  250. {
  251. cCurrChar = styler.SafeGetCharAt( i );
  252. char cNextChar = styler.SafeGetCharAt(i+1);
  253. switch(state)
  254. {
  255. case SCE_NSIS_DEFAULT:
  256. if( cCurrChar == ';' || cCurrChar == '#' ) // we have a comment line
  257. {
  258. styler.ColourTo(i-1, state );
  259. state = SCE_NSIS_COMMENT;
  260. break;
  261. }
  262. if( cCurrChar == '"' )
  263. {
  264. styler.ColourTo(i-1, state );
  265. state = SCE_NSIS_STRINGDQ;
  266. bVarInString = false;
  267. bClassicVarInString = false;
  268. break;
  269. }
  270. if( cCurrChar == '\'' )
  271. {
  272. styler.ColourTo(i-1, state );
  273. state = SCE_NSIS_STRINGRQ;
  274. bVarInString = false;
  275. bClassicVarInString = false;
  276. break;
  277. }
  278. if( cCurrChar == '`' )
  279. {
  280. styler.ColourTo(i-1, state );
  281. state = SCE_NSIS_STRINGLQ;
  282. bVarInString = false;
  283. bClassicVarInString = false;
  284. break;
  285. }
  286. // NSIS KeyWord,Function, Variable, UserDefined:
  287. if( cCurrChar == '$' || isNsisChar(cCurrChar) || cCurrChar == '!' )
  288. {
  289. styler.ColourTo(i-1,state);
  290. state = SCE_NSIS_FUNCTION;
  291. // If it is a number, we must check and set style here first...
  292. if( isNsisNumber(cCurrChar) && (cNextChar == '\t' || cNextChar == ' ' || cNextChar == '\r' || cNextChar == '\n' ) )
  293. styler.ColourTo( i, SCE_NSIS_NUMBER);
  294. break;
  295. }
  296. if( cCurrChar == '/' && cNextChar == '*' )
  297. {
  298. styler.ColourTo(i-1,state);
  299. state = SCE_NSIS_COMMENTBOX;
  300. break;
  301. }
  302. break;
  303. case SCE_NSIS_COMMENT:
  304. if( cNextChar == '\n' || cNextChar == '\r' )
  305. {
  306. // Special case:
  307. if( cCurrChar == '\\' )
  308. {
  309. styler.ColourTo(i-2,state);
  310. styler.ColourTo(i,SCE_NSIS_DEFAULT);
  311. }
  312. else
  313. {
  314. styler.ColourTo(i,state);
  315. state = SCE_NSIS_DEFAULT;
  316. }
  317. }
  318. break;
  319. case SCE_NSIS_STRINGDQ:
  320. case SCE_NSIS_STRINGLQ:
  321. case SCE_NSIS_STRINGRQ:
  322. if( styler.SafeGetCharAt(i-1) == '\\' && styler.SafeGetCharAt(i-2) == '$' )
  323. break; // Ignore the next character, even if it is a quote of some sort
  324. if( cCurrChar == '"' && state == SCE_NSIS_STRINGDQ )
  325. {
  326. styler.ColourTo(i,state);
  327. state = SCE_NSIS_DEFAULT;
  328. break;
  329. }
  330. if( cCurrChar == '`' && state == SCE_NSIS_STRINGLQ )
  331. {
  332. styler.ColourTo(i,state);
  333. state = SCE_NSIS_DEFAULT;
  334. break;
  335. }
  336. if( cCurrChar == '\'' && state == SCE_NSIS_STRINGRQ )
  337. {
  338. styler.ColourTo(i,state);
  339. state = SCE_NSIS_DEFAULT;
  340. break;
  341. }
  342. if( cNextChar == '\r' || cNextChar == '\n' )
  343. {
  344. Sci_Position nCurLine = styler.GetLine(i+1);
  345. Sci_Position nBack = i;
  346. // We need to check if the previous line has a \ in it...
  347. bool bNextLine = false;
  348. while( nBack > 0 )
  349. {
  350. if( styler.GetLine(nBack) != nCurLine )
  351. break;
  352. char cTemp = styler.SafeGetCharAt(nBack, 'a'); // Letter 'a' is safe here
  353. if( cTemp == '\\' )
  354. {
  355. bNextLine = true;
  356. break;
  357. }
  358. if( cTemp != '\r' && cTemp != '\n' && cTemp != '\t' && cTemp != ' ' )
  359. break;
  360. nBack--;
  361. }
  362. if( bNextLine )
  363. {
  364. styler.ColourTo(i+1,state);
  365. }
  366. if( bNextLine == false )
  367. {
  368. styler.ColourTo(i,state);
  369. state = SCE_NSIS_DEFAULT;
  370. }
  371. }
  372. break;
  373. case SCE_NSIS_FUNCTION:
  374. // NSIS KeyWord:
  375. if( cCurrChar == '$' )
  376. state = SCE_NSIS_DEFAULT;
  377. else if( cCurrChar == '\\' && (cNextChar == 'n' || cNextChar == 'r' || cNextChar == 't' ) )
  378. state = SCE_NSIS_DEFAULT;
  379. else if( (isNsisChar(cCurrChar) && !isNsisChar( cNextChar) && cNextChar != '}') || cCurrChar == '}' )
  380. {
  381. state = classifyWordNsis( styler.GetStartSegment(), i, keywordLists, styler );
  382. styler.ColourTo( i, state);
  383. state = SCE_NSIS_DEFAULT;
  384. }
  385. else if( !isNsisChar( cCurrChar ) && cCurrChar != '{' && cCurrChar != '}' )
  386. {
  387. if( classifyWordNsis( styler.GetStartSegment(), i-1, keywordLists, styler) == SCE_NSIS_NUMBER )
  388. styler.ColourTo( i-1, SCE_NSIS_NUMBER );
  389. state = SCE_NSIS_DEFAULT;
  390. if( cCurrChar == '"' )
  391. {
  392. state = SCE_NSIS_STRINGDQ;
  393. bVarInString = false;
  394. bClassicVarInString = false;
  395. }
  396. else if( cCurrChar == '`' )
  397. {
  398. state = SCE_NSIS_STRINGLQ;
  399. bVarInString = false;
  400. bClassicVarInString = false;
  401. }
  402. else if( cCurrChar == '\'' )
  403. {
  404. state = SCE_NSIS_STRINGRQ;
  405. bVarInString = false;
  406. bClassicVarInString = false;
  407. }
  408. else if( cCurrChar == '#' || cCurrChar == ';' )
  409. {
  410. state = SCE_NSIS_COMMENT;
  411. }
  412. }
  413. break;
  414. case SCE_NSIS_COMMENTBOX:
  415. if( styler.SafeGetCharAt(i-1) == '*' && cCurrChar == '/' )
  416. {
  417. styler.ColourTo(i,state);
  418. state = SCE_NSIS_DEFAULT;
  419. }
  420. break;
  421. }
  422. if( state == SCE_NSIS_COMMENT || state == SCE_NSIS_COMMENTBOX )
  423. {
  424. styler.ColourTo(i,state);
  425. }
  426. else if( state == SCE_NSIS_STRINGDQ || state == SCE_NSIS_STRINGLQ || state == SCE_NSIS_STRINGRQ )
  427. {
  428. bool bIngoreNextDollarSign = false;
  429. bool bUserVars = false;
  430. if( styler.GetPropertyInt("nsis.uservars") == 1 )
  431. bUserVars = true;
  432. if( bVarInString && cCurrChar == '$' )
  433. {
  434. bVarInString = false;
  435. bIngoreNextDollarSign = true;
  436. }
  437. else if( bVarInString && cCurrChar == '\\' && (cNextChar == 'n' || cNextChar == 'r' || cNextChar == 't' || cNextChar == '"' || cNextChar == '`' || cNextChar == '\'' ) )
  438. {
  439. styler.ColourTo( i+1, SCE_NSIS_STRINGVAR);
  440. bVarInString = false;
  441. bIngoreNextDollarSign = false;
  442. }
  443. // Covers "$INSTDIR and user vars like $MYVAR"
  444. else if( bVarInString && !isNsisChar(cNextChar) )
  445. {
  446. int nWordState = classifyWordNsis( styler.GetStartSegment(), i, keywordLists, styler);
  447. if( nWordState == SCE_NSIS_VARIABLE )
  448. styler.ColourTo( i, SCE_NSIS_STRINGVAR);
  449. else if( bUserVars )
  450. styler.ColourTo( i, SCE_NSIS_STRINGVAR);
  451. bVarInString = false;
  452. }
  453. // Covers "${TEST}..."
  454. else if( bClassicVarInString && cNextChar == '}' )
  455. {
  456. styler.ColourTo( i+1, SCE_NSIS_STRINGVAR);
  457. bClassicVarInString = false;
  458. }
  459. // Start of var in string
  460. if( !bIngoreNextDollarSign && cCurrChar == '$' && cNextChar == '{' )
  461. {
  462. styler.ColourTo( i-1, state);
  463. bClassicVarInString = true;
  464. bVarInString = false;
  465. }
  466. else if( !bIngoreNextDollarSign && cCurrChar == '$' )
  467. {
  468. styler.ColourTo( i-1, state);
  469. bVarInString = true;
  470. bClassicVarInString = false;
  471. }
  472. }
  473. }
  474. // Colourise remaining document
  475. styler.ColourTo(nLengthDoc-1,state);
  476. }
  477. static void FoldNsisDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler)
  478. {
  479. // No folding enabled, no reason to continue...
  480. if( styler.GetPropertyInt("fold") == 0 )
  481. return;
  482. bool foldAtElse = styler.GetPropertyInt("fold.at.else", 0) == 1;
  483. bool foldUtilityCmd = styler.GetPropertyInt("nsis.foldutilcmd", 1) == 1;
  484. bool blockComment = false;
  485. Sci_Position lineCurrent = styler.GetLine(startPos);
  486. Sci_PositionU safeStartPos = styler.LineStart( lineCurrent );
  487. bool bArg1 = true;
  488. Sci_Position nWordStart = -1;
  489. int levelCurrent = SC_FOLDLEVELBASE;
  490. if (lineCurrent > 0)
  491. levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
  492. int levelNext = levelCurrent;
  493. int style = styler.StyleAt(safeStartPos);
  494. if( style == SCE_NSIS_COMMENTBOX )
  495. {
  496. if( styler.SafeGetCharAt(safeStartPos) == '/' && styler.SafeGetCharAt(safeStartPos+1) == '*' )
  497. levelNext++;
  498. blockComment = true;
  499. }
  500. for (Sci_PositionU i = safeStartPos; i < startPos + length; i++)
  501. {
  502. char chCurr = styler.SafeGetCharAt(i);
  503. style = styler.StyleAt(i);
  504. if( blockComment && style != SCE_NSIS_COMMENTBOX )
  505. {
  506. levelNext--;
  507. blockComment = false;
  508. }
  509. else if( !blockComment && style == SCE_NSIS_COMMENTBOX )
  510. {
  511. levelNext++;
  512. blockComment = true;
  513. }
  514. if( bArg1 && !blockComment)
  515. {
  516. if( nWordStart == -1 && (isNsisLetter(chCurr) || chCurr == '!') )
  517. {
  518. nWordStart = i;
  519. }
  520. else if( isNsisLetter(chCurr) == false && nWordStart > -1 )
  521. {
  522. int newLevel = calculateFoldNsis( nWordStart, i-1, levelNext, styler, foldAtElse, foldUtilityCmd );
  523. if( newLevel == levelNext )
  524. {
  525. if( foldAtElse && foldUtilityCmd )
  526. {
  527. if( NsisNextLineHasElse(i, startPos + length, styler) )
  528. levelNext--;
  529. }
  530. }
  531. else
  532. levelNext = newLevel;
  533. bArg1 = false;
  534. }
  535. }
  536. if( chCurr == '\n' )
  537. {
  538. if( bArg1 && foldAtElse && foldUtilityCmd && !blockComment )
  539. {
  540. if( NsisNextLineHasElse(i, startPos + length, styler) )
  541. levelNext--;
  542. }
  543. // If we are on a new line...
  544. int levelUse = levelCurrent;
  545. int lev = levelUse | levelNext << 16;
  546. if (levelUse < levelNext )
  547. lev |= SC_FOLDLEVELHEADERFLAG;
  548. if (lev != styler.LevelAt(lineCurrent))
  549. styler.SetLevel(lineCurrent, lev);
  550. lineCurrent++;
  551. levelCurrent = levelNext;
  552. bArg1 = true; // New line, lets look at first argument again
  553. nWordStart = -1;
  554. }
  555. }
  556. int levelUse = levelCurrent;
  557. int lev = levelUse | levelNext << 16;
  558. if (levelUse < levelNext)
  559. lev |= SC_FOLDLEVELHEADERFLAG;
  560. if (lev != styler.LevelAt(lineCurrent))
  561. styler.SetLevel(lineCurrent, lev);
  562. }
  563. static const char * const nsisWordLists[] = {
  564. "Functions",
  565. "Variables",
  566. "Lables",
  567. "UserDefined",
  568. 0, };
  569. LexerModule lmNsis(SCLEX_NSIS, ColouriseNsisDoc, "nsis", FoldNsisDoc, nsisWordLists);