Add an UI to enable/disable specific overlay handlers.
[TortoiseGit.git] / ext / scintilla / src / LexAU3.cxx
blob795d50b86ae530783d966a58401ecd30fbc05196
1 // Scintilla source code edit control
2 // @file LexAU3.cxx
3 // Lexer for AutoIt3 http://www.hiddensoft.com/autoit3
4 // by Jos van der Zande, jvdzande@yahoo.com
5 //
6 // Changes:
7 // March 28, 2004 - Added the standard Folding code
8 // April 21, 2004 - Added Preprosessor Table + Syntax Highlighting
9 // Fixed Number highlighting
10 // Changed default isoperator to IsAOperator to have a better match to AutoIt3
11 // Fixed "#comments_start" -> "#comments-start"
12 // Fixed "#comments_end" -> "#comments-end"
13 // Fixed Sendkeys in Strings when not terminated with }
14 // Added support for Sendkey strings that have second parameter e.g. {UP 5} or {a down}
15 // April 26, 2004 - Fixed # pre-processor statement inside of comment block would invalidly change the color.
16 // Added logic for #include <xyz.au3> to treat the <> as string
17 // Added underscore to IsAOperator.
18 // May 17, 2004 - Changed the folding logic from indent to keyword folding.
19 // Added Folding logic for blocks of single-commentlines or commentblock.
20 // triggered by: fold.comment=1
21 // Added Folding logic for preprocessor blocks triggered by fold.preprocessor=1
22 // Added Special for #region - #endregion syntax highlight and folding.
23 // May 30, 2004 - Fixed issue with continuation lines on If statements.
24 // June 5, 2004 - Added comma to Operators for better readability.
25 // Added fold.compact support set with fold.compact=1
26 // Changed folding inside of #cs-#ce. Default is no keyword folding inside comment blocks when fold.comment=1
27 // it will now only happen when fold.comment=2.
28 // Sep 5, 2004 - Added logic to handle colourizing words on the last line.
29 // Typed Characters now show as "default" till they match any table.
30 // Oct 10, 2004 - Added logic to show Comments in "Special" directives.
31 // Nov 1, 2004 - Added better testing for Numbers supporting x and e notation.
32 // Nov 28, 2004 - Added logic to handle continuation lines for syntax highlighting.
33 // Jan 10, 2005 - Added Abbreviations Keyword used for expansion
34 // Mar 24, 2005 - Updated Abbreviations Keywords to fix when followed by Operator.
35 // Apr 18, 2005 - Updated #CE/#Comment-End logic to take a linecomment ";" into account
36 // - Added folding support for With...EndWith
37 // - Added support for a DOT in variable names
38 // - Fixed Underscore in CommentBlock
39 // May 23, 2005 - Fixed the SentKey lexing in case of a missing }
40 // Aug 11, 2005 - Fixed possible bug with s_save length > 100.
41 // Aug 23, 2005 - Added Switch/endswitch support to the folding logic.
42 // Sep 27, 2005 - Fixed the SentKey lexing logic in case of multiple sentkeys.
43 // Mar 12, 2006 - Fixed issue with <> coloring as String in stead of Operator in rare occasions.
44 // Apr 8, 2006 - Added support for AutoIt3 Standard UDF library (SCE_AU3_UDF)
45 // Mar 9, 2007 - Fixed bug with + following a String getting the wrong Color.
46 // Jun 20, 2007 - Fixed Commentblock issue when LF's are used as EOL.
47 // Jul 26, 2007 - Fixed #endregion undetected bug.
49 // Copyright for Scintilla: 1998-2001 by Neil Hodgson <neilh@scintilla.org>
50 // The License.txt file describes the conditions under which this software may be distributed.
51 // Scintilla source code edit control
53 #include <stdlib.h>
54 #include <string.h>
55 #include <ctype.h>
56 #include <stdio.h>
57 #include <stdarg.h>
59 #include "Platform.h"
61 #include "PropSet.h"
62 #include "Accessor.h"
63 #include "StyleContext.h"
64 #include "KeyWords.h"
65 #include "Scintilla.h"
66 #include "SciLexer.h"
68 #ifdef SCI_NAMESPACE
69 using namespace Scintilla;
70 #endif
72 static inline bool IsTypeCharacter(const int ch)
74 return ch == '$';
76 static inline bool IsAWordChar(const int ch)
78 return (ch < 0x80) && (isalnum(ch) || ch == '_');
81 static inline bool IsAWordStart(const int ch)
83 return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '@' || ch == '#' || ch == '$' || ch == '.');
86 static inline bool IsAOperator(char ch) {
87 if (isascii(ch) && isalnum(ch))
88 return false;
89 if (ch == '+' || ch == '-' || ch == '*' || ch == '/' ||
90 ch == '&' || ch == '^' || ch == '=' || ch == '<' || ch == '>' ||
91 ch == '(' || ch == ')' || ch == '[' || ch == ']' || ch == ',' )
92 return true;
93 return false;
96 ///////////////////////////////////////////////////////////////////////////////
97 // GetSendKey() filters the portion before and after a/multiple space(s)
98 // and return the first portion to be looked-up in the table
99 // also check if the second portion is valid... (up,down.on.off,toggle or a number)
100 ///////////////////////////////////////////////////////////////////////////////
102 static int GetSendKey(const char *szLine, char *szKey)
104 int nFlag = 0;
105 int nStartFound = 0;
106 int nKeyPos = 0;
107 int nSpecPos= 0;
108 int nSpecNum= 1;
109 int nPos = 0;
110 char cTemp;
111 char szSpecial[100];
113 // split the portion of the sendkey in the part before and after the spaces
114 while ( ( (cTemp = szLine[nPos]) != '\0'))
116 // skip leading Ctrl/Shift/Alt state
117 if (cTemp == '{') {
118 nStartFound = 1;
121 if (nStartFound == 1) {
122 if ((cTemp == ' ') && (nFlag == 0) ) // get the stuff till first space
124 nFlag = 1;
125 // Add } to the end of the first bit for table lookup later.
126 szKey[nKeyPos++] = '}';
128 else if (cTemp == ' ')
130 // skip other spaces
132 else if (nFlag == 0)
134 // save first portion into var till space or } is hit
135 szKey[nKeyPos++] = cTemp;
137 else if ((nFlag == 1) && (cTemp != '}'))
139 // Save second portion into var...
140 szSpecial[nSpecPos++] = cTemp;
141 // check if Second portion is all numbers for repeat fuction
142 if (isdigit(cTemp) == false) {nSpecNum = 0;}
145 nPos++; // skip to next char
147 } // End While
150 // Check if the second portion is either a number or one of these keywords
151 szKey[nKeyPos] = '\0';
152 szSpecial[nSpecPos] = '\0';
153 if (strcmp(szSpecial,"down")== 0 || strcmp(szSpecial,"up")== 0 ||
154 strcmp(szSpecial,"on")== 0 || strcmp(szSpecial,"off")== 0 ||
155 strcmp(szSpecial,"toggle")== 0 || nSpecNum == 1 )
157 nFlag = 0;
159 else
161 nFlag = 1;
163 return nFlag; // 1 is bad, 0 is good
165 } // GetSendKey()
168 // Routine to check the last "none comment" character on a line to see if its a continuation
170 static bool IsContinuationLine(unsigned int szLine, Accessor &styler)
172 int nsPos = styler.LineStart(szLine);
173 int nePos = styler.LineStart(szLine+1) - 2;
174 //int stylech = styler.StyleAt(nsPos);
175 while (nsPos < nePos)
177 //stylech = styler.StyleAt(nePos);
178 int stylech = styler.StyleAt(nsPos);
179 if (!(stylech == SCE_AU3_COMMENT)) {
180 char ch = styler.SafeGetCharAt(nePos);
181 if (!isspacechar(ch)) {
182 if (ch == '_')
183 return true;
184 else
185 return false;
188 nePos--; // skip to next char
189 } // End While
190 return false;
191 } // IsContinuationLine()
194 // syntax highlighting logic
195 static void ColouriseAU3Doc(unsigned int startPos,
196 int length, int initStyle,
197 WordList *keywordlists[],
198 Accessor &styler) {
200 WordList &keywords = *keywordlists[0];
201 WordList &keywords2 = *keywordlists[1];
202 WordList &keywords3 = *keywordlists[2];
203 WordList &keywords4 = *keywordlists[3];
204 WordList &keywords5 = *keywordlists[4];
205 WordList &keywords6 = *keywordlists[5];
206 WordList &keywords7 = *keywordlists[6];
207 WordList &keywords8 = *keywordlists[7];
208 // find the first previous line without continuation character at the end
209 int lineCurrent = styler.GetLine(startPos);
210 int s_startPos = startPos;
211 // When not inside a Block comment: find First line without _
212 if (!(initStyle==SCE_AU3_COMMENTBLOCK)) {
213 while ((lineCurrent > 0 && IsContinuationLine(lineCurrent,styler)) ||
214 (lineCurrent > 1 && IsContinuationLine(lineCurrent-1,styler))) {
215 lineCurrent--;
216 startPos = styler.LineStart(lineCurrent); // get start position
217 initStyle = 0; // reset the start style to 0
220 // Set the new length to include it from the start and set the start position
221 length = length + s_startPos - startPos; // correct the total length to process
222 styler.StartAt(startPos);
224 StyleContext sc(startPos, length, initStyle, styler);
225 char si; // string indicator "=1 '=2
226 char ni; // Numeric indicator error=9 normal=0 normal+dec=1 hex=2 Enot=3
227 char ci; // comment indicator 0=not linecomment(;)
228 char s_save[100];
229 si=0;
230 ni=0;
231 ci=0;
232 //$$$
233 for (; sc.More(); sc.Forward()) {
234 char s[100];
235 sc.GetCurrentLowered(s, sizeof(s));
236 // **********************************************
237 // save the total current word for eof processing
238 if (IsAWordChar(sc.ch) || sc.ch == '}')
240 strcpy(s_save,s);
241 int tp = strlen(s_save);
242 if (tp < 99) {
243 s_save[tp] = static_cast<char>(tolower(sc.ch));
244 s_save[tp+1] = '\0';
247 // **********************************************
249 switch (sc.state)
251 case SCE_AU3_COMMENTBLOCK:
253 //Reset at line end
254 if (sc.atLineEnd) {
255 ci=0;
256 if ((strcmp(s, "#ce")== 0 || strcmp(s, "#comments-end")== 0))
257 if (sc.atLineEnd)
258 sc.SetState(SCE_AU3_DEFAULT);
259 else
260 sc.SetState(SCE_AU3_COMMENTBLOCK);
261 break;
263 //skip rest of line when a ; is encountered
264 if (sc.chPrev == ';') {
265 ci=2;
266 sc.SetState(SCE_AU3_COMMENTBLOCK);
268 // skip rest of the line
269 if (ci==2)
270 break;
271 // check when first character is detected on the line
272 if (ci==0) {
273 if (IsAWordStart(static_cast<char>(sc.ch)) || IsAOperator(static_cast<char>(sc.ch))) {
274 ci=1;
275 sc.SetState(SCE_AU3_COMMENTBLOCK);
277 break;
279 if (!(IsAWordChar(sc.ch) || (sc.ch == '-' && strcmp(s, "#comments") == 0))) {
280 if ((strcmp(s, "#ce")== 0 || strcmp(s, "#comments-end")== 0))
281 sc.SetState(SCE_AU3_COMMENT); // set to comment line for the rest of the line
282 else
283 ci=2; // line doesn't begin with #CE so skip the rest of the line
285 break;
287 case SCE_AU3_COMMENT:
289 if (sc.atLineEnd) {sc.SetState(SCE_AU3_DEFAULT);}
290 break;
292 case SCE_AU3_OPERATOR:
294 // check if its a COMobject
295 if (sc.chPrev == '.' && IsAWordChar(sc.ch)) {
296 sc.SetState(SCE_AU3_COMOBJ);
298 else {
299 sc.SetState(SCE_AU3_DEFAULT);
301 break;
303 case SCE_AU3_SPECIAL:
305 if (sc.ch == ';') {sc.SetState(SCE_AU3_COMMENT);}
306 if (sc.atLineEnd) {sc.SetState(SCE_AU3_DEFAULT);}
307 break;
309 case SCE_AU3_KEYWORD:
311 if (!(IsAWordChar(sc.ch) || (sc.ch == '-' && (strcmp(s, "#comments") == 0 || strcmp(s, "#include") == 0))))
313 if (!IsTypeCharacter(sc.ch))
315 if (strcmp(s, "#cs")== 0 || strcmp(s, "#comments-start")== 0 )
317 sc.ChangeState(SCE_AU3_COMMENTBLOCK);
318 sc.SetState(SCE_AU3_COMMENTBLOCK);
319 break;
321 else if (keywords.InList(s)) {
322 sc.ChangeState(SCE_AU3_KEYWORD);
323 sc.SetState(SCE_AU3_DEFAULT);
325 else if (keywords2.InList(s)) {
326 sc.ChangeState(SCE_AU3_FUNCTION);
327 sc.SetState(SCE_AU3_DEFAULT);
329 else if (keywords3.InList(s)) {
330 sc.ChangeState(SCE_AU3_MACRO);
331 sc.SetState(SCE_AU3_DEFAULT);
333 else if (keywords5.InList(s)) {
334 sc.ChangeState(SCE_AU3_PREPROCESSOR);
335 sc.SetState(SCE_AU3_DEFAULT);
336 if (strcmp(s, "#include")== 0)
338 si = 3; // use to determine string start for #inlude <>
341 else if (keywords6.InList(s)) {
342 sc.ChangeState(SCE_AU3_SPECIAL);
343 sc.SetState(SCE_AU3_SPECIAL);
345 else if ((keywords7.InList(s)) && (!IsAOperator(static_cast<char>(sc.ch)))) {
346 sc.ChangeState(SCE_AU3_EXPAND);
347 sc.SetState(SCE_AU3_DEFAULT);
349 else if (keywords8.InList(s)) {
350 sc.ChangeState(SCE_AU3_UDF);
351 sc.SetState(SCE_AU3_DEFAULT);
353 else if (strcmp(s, "_") == 0) {
354 sc.ChangeState(SCE_AU3_OPERATOR);
355 sc.SetState(SCE_AU3_DEFAULT);
357 else if (!IsAWordChar(sc.ch)) {
358 sc.ChangeState(SCE_AU3_DEFAULT);
359 sc.SetState(SCE_AU3_DEFAULT);
363 if (sc.atLineEnd) {
364 sc.SetState(SCE_AU3_DEFAULT);}
365 break;
367 case SCE_AU3_NUMBER:
369 // Numeric indicator error=9 normal=0 normal+dec=1 hex=2 E-not=3
371 // test for Hex notation
372 if (strcmp(s, "0") == 0 && (sc.ch == 'x' || sc.ch == 'X') && ni == 0)
374 ni = 2;
375 break;
377 // test for E notation
378 if (IsADigit(sc.chPrev) && (sc.ch == 'e' || sc.ch == 'E') && ni <= 1)
380 ni = 3;
381 break;
383 // Allow Hex characters inside hex numeric strings
384 if ((ni == 2) &&
385 (sc.ch == 'a' || sc.ch == 'b' || sc.ch == 'c' || sc.ch == 'd' || sc.ch == 'e' || sc.ch == 'f' ||
386 sc.ch == 'A' || sc.ch == 'B' || sc.ch == 'C' || sc.ch == 'D' || sc.ch == 'E' || sc.ch == 'F' ))
388 break;
390 // test for 1 dec point only
391 if (sc.ch == '.')
393 if (ni==0)
395 ni=1;
397 else
399 ni=9;
401 break;
403 // end of numeric string ?
404 if (!(IsADigit(sc.ch)))
406 if (ni==9)
408 sc.ChangeState(SCE_AU3_DEFAULT);
410 sc.SetState(SCE_AU3_DEFAULT);
412 break;
414 case SCE_AU3_VARIABLE:
416 // Check if its a COMObject
417 if (sc.ch == '.' && !IsADigit(sc.chNext)) {
418 sc.SetState(SCE_AU3_OPERATOR);
420 else if (!IsAWordChar(sc.ch)) {
421 sc.SetState(SCE_AU3_DEFAULT);
423 break;
425 case SCE_AU3_COMOBJ:
427 if (!(IsAWordChar(sc.ch))) {
428 sc.SetState(SCE_AU3_DEFAULT);
430 break;
432 case SCE_AU3_STRING:
434 // check for " to end a double qouted string or
435 // check for ' to end a single qouted string
436 if ((si == 1 && sc.ch == '\"') || (si == 2 && sc.ch == '\'') || (si == 3 && sc.ch == '>'))
438 sc.ForwardSetState(SCE_AU3_DEFAULT);
439 si=0;
440 break;
442 if (sc.atLineEnd)
444 si=0;
445 // at line end and not found a continuation char then reset to default
446 int lineCurrent = styler.GetLine(sc.currentPos);
447 if (!IsContinuationLine(lineCurrent,styler))
449 sc.SetState(SCE_AU3_DEFAULT);
450 break;
453 // find Sendkeys in a STRING
454 if (sc.ch == '{' || sc.ch == '+' || sc.ch == '!' || sc.ch == '^' || sc.ch == '#' ) {
455 sc.SetState(SCE_AU3_SENT);}
456 break;
459 case SCE_AU3_SENT:
461 // Send key string ended
462 if (sc.chPrev == '}' && sc.ch != '}')
464 // set color to SENDKEY when valid sendkey .. else set back to regular string
465 char sk[100];
466 // split {111 222} and return {111} and check if 222 is valid.
467 // if return code = 1 then invalid 222 so must be string
468 if (GetSendKey(s,sk))
470 sc.ChangeState(SCE_AU3_STRING);
472 // if single char between {?} then its ok as sendkey for a single character
473 else if (strlen(sk) == 3)
475 sc.ChangeState(SCE_AU3_SENT);
477 // if sendkey {111} is in table then ok as sendkey
478 else if (keywords4.InList(sk))
480 sc.ChangeState(SCE_AU3_SENT);
482 else
484 sc.ChangeState(SCE_AU3_STRING);
486 sc.SetState(SCE_AU3_STRING);
488 else
490 // check if the start is a valid SendKey start
491 int nPos = 0;
492 int nState = 1;
493 char cTemp;
494 while (!(nState == 2) && ((cTemp = s[nPos]) != '\0'))
496 if (cTemp == '{' && nState == 1)
498 nState = 2;
500 if (nState == 1 && !(cTemp == '+' || cTemp == '!' || cTemp == '^' || cTemp == '#' ))
502 nState = 0;
504 nPos++;
506 //Verify characters infront of { ... if not assume regular string
507 if (nState == 1 && (!(sc.ch == '{' || sc.ch == '+' || sc.ch == '!' || sc.ch == '^' || sc.ch == '#' ))) {
508 sc.ChangeState(SCE_AU3_STRING);
509 sc.SetState(SCE_AU3_STRING);
511 // If invalid character found then assume its a regular string
512 if (nState == 0) {
513 sc.ChangeState(SCE_AU3_STRING);
514 sc.SetState(SCE_AU3_STRING);
517 // check if next portion is again a sendkey
518 if (sc.atLineEnd)
520 sc.ChangeState(SCE_AU3_STRING);
521 sc.SetState(SCE_AU3_DEFAULT);
522 si = 0; // reset string indicator
524 //* check in next characters following a sentkey are again a sent key
525 // Need this test incase of 2 sentkeys like {F1}{ENTER} but not detect {{}
526 if (sc.state == SCE_AU3_STRING && (sc.ch == '{' || sc.ch == '+' || sc.ch == '!' || sc.ch == '^' || sc.ch == '#' )) {
527 sc.SetState(SCE_AU3_SENT);}
528 // check to see if the string ended...
529 // Sendkey string isn't complete but the string ended....
530 if ((si == 1 && sc.ch == '\"') || (si == 2 && sc.ch == '\''))
532 sc.ChangeState(SCE_AU3_STRING);
533 sc.ForwardSetState(SCE_AU3_DEFAULT);
535 break;
537 } //switch (sc.state)
539 // Determine if a new state should be entered:
541 if (sc.state == SCE_AU3_DEFAULT)
543 if (sc.ch == ';') {sc.SetState(SCE_AU3_COMMENT);}
544 else if (sc.ch == '#') {sc.SetState(SCE_AU3_KEYWORD);}
545 else if (sc.ch == '$') {sc.SetState(SCE_AU3_VARIABLE);}
546 else if (sc.ch == '.' && !IsADigit(sc.chNext)) {sc.SetState(SCE_AU3_OPERATOR);}
547 else if (sc.ch == '@') {sc.SetState(SCE_AU3_KEYWORD);}
548 //else if (sc.ch == '_') {sc.SetState(SCE_AU3_KEYWORD);}
549 else if (sc.ch == '<' && si==3) {sc.SetState(SCE_AU3_STRING);} // string after #include
550 else if (sc.ch == '\"') {
551 sc.SetState(SCE_AU3_STRING);
552 si = 1; }
553 else if (sc.ch == '\'') {
554 sc.SetState(SCE_AU3_STRING);
555 si = 2; }
556 else if (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext)))
558 sc.SetState(SCE_AU3_NUMBER);
559 ni = 0;
561 else if (IsAWordStart(sc.ch)) {sc.SetState(SCE_AU3_KEYWORD);}
562 else if (IsAOperator(static_cast<char>(sc.ch))) {sc.SetState(SCE_AU3_OPERATOR);}
563 else if (sc.atLineEnd) {sc.SetState(SCE_AU3_DEFAULT);}
565 } //for (; sc.More(); sc.Forward())
567 //*************************************
568 // Colourize the last word correctly
569 //*************************************
570 if (sc.state == SCE_AU3_KEYWORD)
572 if (strcmp(s_save, "#cs")== 0 || strcmp(s_save, "#comments-start")== 0 )
574 sc.ChangeState(SCE_AU3_COMMENTBLOCK);
575 sc.SetState(SCE_AU3_COMMENTBLOCK);
577 else if (keywords.InList(s_save)) {
578 sc.ChangeState(SCE_AU3_KEYWORD);
579 sc.SetState(SCE_AU3_KEYWORD);
581 else if (keywords2.InList(s_save)) {
582 sc.ChangeState(SCE_AU3_FUNCTION);
583 sc.SetState(SCE_AU3_FUNCTION);
585 else if (keywords3.InList(s_save)) {
586 sc.ChangeState(SCE_AU3_MACRO);
587 sc.SetState(SCE_AU3_MACRO);
589 else if (keywords5.InList(s_save)) {
590 sc.ChangeState(SCE_AU3_PREPROCESSOR);
591 sc.SetState(SCE_AU3_PREPROCESSOR);
593 else if (keywords6.InList(s_save)) {
594 sc.ChangeState(SCE_AU3_SPECIAL);
595 sc.SetState(SCE_AU3_SPECIAL);
597 else if (keywords7.InList(s_save) && sc.atLineEnd) {
598 sc.ChangeState(SCE_AU3_EXPAND);
599 sc.SetState(SCE_AU3_EXPAND);
601 else if (keywords8.InList(s_save)) {
602 sc.ChangeState(SCE_AU3_UDF);
603 sc.SetState(SCE_AU3_UDF);
605 else {
606 sc.ChangeState(SCE_AU3_DEFAULT);
607 sc.SetState(SCE_AU3_DEFAULT);
610 if (sc.state == SCE_AU3_SENT)
612 // Send key string ended
613 if (sc.chPrev == '}' && sc.ch != '}')
615 // set color to SENDKEY when valid sendkey .. else set back to regular string
616 char sk[100];
617 // split {111 222} and return {111} and check if 222 is valid.
618 // if return code = 1 then invalid 222 so must be string
619 if (GetSendKey(s_save,sk))
621 sc.ChangeState(SCE_AU3_STRING);
623 // if single char between {?} then its ok as sendkey for a single character
624 else if (strlen(sk) == 3)
626 sc.ChangeState(SCE_AU3_SENT);
628 // if sendkey {111} is in table then ok as sendkey
629 else if (keywords4.InList(sk))
631 sc.ChangeState(SCE_AU3_SENT);
633 else
635 sc.ChangeState(SCE_AU3_STRING);
637 sc.SetState(SCE_AU3_STRING);
639 // check if next portion is again a sendkey
640 if (sc.atLineEnd)
642 sc.ChangeState(SCE_AU3_STRING);
643 sc.SetState(SCE_AU3_DEFAULT);
646 //*************************************
647 sc.Complete();
651 static bool IsStreamCommentStyle(int style) {
652 return style == SCE_AU3_COMMENT || style == SCE_AU3_COMMENTBLOCK;
656 // Routine to find first none space on the current line and return its Style
657 // needed for comment lines not starting on pos 1
658 static int GetStyleFirstWord(unsigned int szLine, Accessor &styler)
660 int nsPos = styler.LineStart(szLine);
661 int nePos = styler.LineStart(szLine+1) - 1;
662 while (isspacechar(styler.SafeGetCharAt(nsPos)) && nsPos < nePos)
664 nsPos++; // skip to next char
666 } // End While
667 return styler.StyleAt(nsPos);
669 } // GetStyleFirstWord()
673 static void FoldAU3Doc(unsigned int startPos, int length, int, WordList *[], Accessor &styler)
675 int endPos = startPos + length;
676 // get settings from the config files for folding comments and preprocessor lines
677 bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
678 bool foldInComment = styler.GetPropertyInt("fold.comment") == 2;
679 bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
680 bool foldpreprocessor = styler.GetPropertyInt("fold.preprocessor") != 0;
681 // Backtrack to previous line in case need to fix its fold status
682 int lineCurrent = styler.GetLine(startPos);
683 if (startPos > 0) {
684 if (lineCurrent > 0) {
685 lineCurrent--;
686 startPos = styler.LineStart(lineCurrent);
689 // vars for style of previous/current/next lines
690 int style = GetStyleFirstWord(lineCurrent,styler);
691 int stylePrev = 0;
692 // find the first previous line without continuation character at the end
693 while ((lineCurrent > 0 && IsContinuationLine(lineCurrent,styler)) ||
694 (lineCurrent > 1 && IsContinuationLine(lineCurrent-1,styler))) {
695 lineCurrent--;
696 startPos = styler.LineStart(lineCurrent);
698 if (lineCurrent > 0) {
699 stylePrev = GetStyleFirstWord(lineCurrent-1,styler);
701 // vars for getting first word to check for keywords
702 bool FirstWordStart = false;
703 bool FirstWordEnd = false;
704 char szKeyword[11]="";
705 int szKeywordlen = 0;
706 char szThen[5]="";
707 int szThenlen = 0;
708 bool ThenFoundLast = false;
709 // var for indentlevel
710 int levelCurrent = SC_FOLDLEVELBASE;
711 if (lineCurrent > 0)
712 levelCurrent = styler.LevelAt(lineCurrent-1) >> 16;
713 int levelNext = levelCurrent;
715 int visibleChars = 0;
716 char chNext = styler.SafeGetCharAt(startPos);
717 char chPrev = ' ';
719 for (int i = startPos; i < endPos; i++) {
720 char ch = chNext;
721 chNext = styler.SafeGetCharAt(i + 1);
722 if (IsAWordChar(ch)) {
723 visibleChars++;
725 // get the syle for the current character neede to check in comment
726 int stylech = styler.StyleAt(i);
727 // get first word for the line for indent check max 9 characters
728 if (FirstWordStart && (!(FirstWordEnd))) {
729 if (!IsAWordChar(ch)) {
730 FirstWordEnd = true;
731 szKeyword[szKeywordlen] = '\0';
733 else {
734 if (szKeywordlen < 10) {
735 szKeyword[szKeywordlen++] = static_cast<char>(tolower(ch));
739 // start the capture of the first word
740 if (!(FirstWordStart)) {
741 if (IsAWordChar(ch) || IsAWordStart(ch) || ch == ';') {
742 FirstWordStart = true;
743 szKeyword[szKeywordlen++] = static_cast<char>(tolower(ch));
746 // only process this logic when not in comment section
747 if (!(stylech == SCE_AU3_COMMENT)) {
748 if (ThenFoundLast) {
749 if (IsAWordChar(ch)) {
750 ThenFoundLast = false;
753 // find out if the word "then" is the last on a "if" line
754 if (FirstWordEnd && strcmp(szKeyword,"if") == 0) {
755 if (szThenlen == 4) {
756 szThen[0] = szThen[1];
757 szThen[1] = szThen[2];
758 szThen[2] = szThen[3];
759 szThen[3] = static_cast<char>(tolower(ch));
760 if (strcmp(szThen,"then") == 0 ) {
761 ThenFoundLast = true;
764 else {
765 szThen[szThenlen++] = static_cast<char>(tolower(ch));
766 if (szThenlen == 5) {
767 szThen[4] = '\0';
772 // End of Line found so process the information
773 if ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
774 // **************************
775 // Folding logic for Keywords
776 // **************************
777 // if a keyword is found on the current line and the line doesn't end with _ (continuation)
778 // and we are not inside a commentblock.
779 if (szKeywordlen > 0 && (!(chPrev == '_')) &&
780 ((!(IsStreamCommentStyle(style)) || foldInComment)) ) {
781 szKeyword[szKeywordlen] = '\0';
782 // only fold "if" last keyword is "then" (else its a one line if)
783 if (strcmp(szKeyword,"if") == 0 && ThenFoundLast) {
784 levelNext++;
786 // create new fold for these words
787 if (strcmp(szKeyword,"do") == 0 || strcmp(szKeyword,"for") == 0 ||
788 strcmp(szKeyword,"func") == 0 || strcmp(szKeyword,"while") == 0||
789 strcmp(szKeyword,"with") == 0 || strcmp(szKeyword,"#region") == 0 ) {
790 levelNext++;
792 // create double Fold for select&switch because Case will subtract one of the current level
793 if (strcmp(szKeyword,"select") == 0 || strcmp(szKeyword,"switch") == 0) {
794 levelNext++;
795 levelNext++;
797 // end the fold for these words before the current line
798 if (strcmp(szKeyword,"endfunc") == 0 || strcmp(szKeyword,"endif") == 0 ||
799 strcmp(szKeyword,"next") == 0 || strcmp(szKeyword,"until") == 0 ||
800 strcmp(szKeyword,"endwith") == 0 ||strcmp(szKeyword,"wend") == 0){
801 levelNext--;
802 levelCurrent--;
804 // end the fold for these words before the current line and Start new fold
805 if (strcmp(szKeyword,"case") == 0 || strcmp(szKeyword,"else") == 0 ||
806 strcmp(szKeyword,"elseif") == 0 ) {
807 levelCurrent--;
809 // end the double fold for this word before the current line
810 if (strcmp(szKeyword,"endselect") == 0 || strcmp(szKeyword,"endswitch") == 0 ) {
811 levelNext--;
812 levelNext--;
813 levelCurrent--;
814 levelCurrent--;
816 // end the fold for these words on the current line
817 if (strcmp(szKeyword,"#endregion") == 0 ) {
818 levelNext--;
821 // Preprocessor and Comment folding
822 int styleNext = GetStyleFirstWord(lineCurrent + 1,styler);
823 // *************************************
824 // Folding logic for preprocessor blocks
825 // *************************************
826 // process preprosessor line
827 if (foldpreprocessor && style == SCE_AU3_PREPROCESSOR) {
828 if (!(stylePrev == SCE_AU3_PREPROCESSOR) && (styleNext == SCE_AU3_PREPROCESSOR)) {
829 levelNext++;
831 // fold till the last line for normal comment lines
832 else if (stylePrev == SCE_AU3_PREPROCESSOR && !(styleNext == SCE_AU3_PREPROCESSOR)) {
833 levelNext--;
836 // *********************************
837 // Folding logic for Comment blocks
838 // *********************************
839 if (foldComment && IsStreamCommentStyle(style)) {
840 // Start of a comment block
841 if (!(stylePrev==style) && IsStreamCommentStyle(styleNext) && styleNext==style) {
842 levelNext++;
844 // fold till the last line for normal comment lines
845 else if (IsStreamCommentStyle(stylePrev)
846 && !(styleNext == SCE_AU3_COMMENT)
847 && stylePrev == SCE_AU3_COMMENT
848 && style == SCE_AU3_COMMENT) {
849 levelNext--;
851 // fold till the one but last line for Blockcomment lines
852 else if (IsStreamCommentStyle(stylePrev)
853 && !(styleNext == SCE_AU3_COMMENTBLOCK)
854 && style == SCE_AU3_COMMENTBLOCK) {
855 levelNext--;
856 levelCurrent--;
859 int levelUse = levelCurrent;
860 int lev = levelUse | levelNext << 16;
861 if (visibleChars == 0 && foldCompact)
862 lev |= SC_FOLDLEVELWHITEFLAG;
863 if (levelUse < levelNext) {
864 lev |= SC_FOLDLEVELHEADERFLAG;
866 if (lev != styler.LevelAt(lineCurrent)) {
867 styler.SetLevel(lineCurrent, lev);
869 // reset values for the next line
870 lineCurrent++;
871 stylePrev = style;
872 style = styleNext;
873 levelCurrent = levelNext;
874 visibleChars = 0;
875 // if the last character is an Underscore then don't reset since the line continues on the next line.
876 if (!(chPrev == '_')) {
877 szKeywordlen = 0;
878 szThenlen = 0;
879 FirstWordStart = false;
880 FirstWordEnd = false;
881 ThenFoundLast = false;
884 // save the last processed character
885 if (!isspacechar(ch)) {
886 chPrev = ch;
887 visibleChars++;
895 static const char * const AU3WordLists[] = {
896 "#autoit keywords",
897 "#autoit functions",
898 "#autoit macros",
899 "#autoit Sent keys",
900 "#autoit Pre-processors",
901 "#autoit Special",
902 "#autoit Expand",
903 "#autoit UDF",
906 LexerModule lmAU3(SCLEX_AU3, ColouriseAU3Doc, "au3", FoldAU3Doc , AU3WordLists);