aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/LexMetapost.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'src/LexMetapost.cxx')
-rw-r--r--src/LexMetapost.cxx101
1 files changed, 100 insertions, 1 deletions
diff --git a/src/LexMetapost.cxx b/src/LexMetapost.cxx
index f3fe77d0d..552d68cb4 100644
--- a/src/LexMetapost.cxx
+++ b/src/LexMetapost.cxx
@@ -3,6 +3,8 @@
// File: LexMetapost.cxx - general context conformant metapost coloring scheme
// Author: Hans Hagen - PRAGMA ADE - Hasselt NL - www.pragma-ade.com
// Version: September 28, 2003
+// Modified by instanton: June 22, 2007
+// Added folding code
// Copyright: 1998-2003 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
@@ -321,4 +323,101 @@ static const char * const metapostWordListDesc[] = {
0
} ;
-LexerModule lmMETAPOST(SCLEX_METAPOST, ColouriseMETAPOSTDoc, "metapost", 0, metapostWordListDesc);
+static int classifyFoldPointMetapost(const char* s) {
+ int lev=0;
+ if (!(isdigit(s[0]) || (s[0] == '.'))){
+ if (strcmp(s, "beginfig")==0||strcmp(s,"for")==0||strcmp(s,"verbatimtex")==0||
+ strcmp(s,"def")==0||strcmp(s,"vardef")==0||strcmp(s,"begingroup")==0||
+ strcmp(s,"btex")==0||strcmp(s,"if")==0
+ )
+ lev=1;
+ if (strcmp(s,"endfig")==0||strcmp(s,"enddef")==0||strcmp(s,"endfor")==0||
+ strcmp(s,"endgroup")==0||strcmp(s,"etex")==0||
+ strcmp(s,"fi")==0||strcmp(s,"endif")==0
+ )
+ lev=-1;
+ }
+ return lev;
+}
+
+static int ParseMetapostWord(unsigned int pos, Accessor &styler, char *word)
+{
+ int length=0;
+ char ch=styler.SafeGetCharAt(pos);
+ *word=0;
+
+ while(isMETAPOSTidentifier(ch) && isalpha(ch) && length<100){
+ word[length]=ch;
+ length++;
+ ch=styler.SafeGetCharAt(pos+length);
+ }
+ word[length]=0;
+ return length;
+}
+
+static void FoldMetapostDoc(unsigned int startPos, int length, int, WordList *[], Accessor &styler)
+{
+ bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
+ unsigned int endPos = startPos+length;
+ int visibleChars=0;
+ int lineCurrent=styler.GetLine(startPos);
+ int levelPrev=styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
+ int levelCurrent=levelPrev;
+ char chNext=styler[startPos];
+
+ char buffer[100]="";
+
+ for (unsigned int i=startPos; i < endPos; i++) {
+ char ch=chNext;
+ chNext=styler.SafeGetCharAt(i+1);
+ bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
+
+ ParseMetapostWord(i, styler, buffer);
+ levelCurrent += classifyFoldPointMetapost(buffer);
+
+ char chNext2;
+ char chNext3;
+ char chNext4;
+ char chNext5;
+ chNext2=styler.SafeGetCharAt(i+2);
+ chNext3=styler.SafeGetCharAt(i+3);
+ chNext4=styler.SafeGetCharAt(i+4);
+ chNext5=styler.SafeGetCharAt(i+5);
+
+ bool atenddef =(ch == 'e') && (chNext=='n') &&
+ (chNext2=='d')&& (chNext3=='d')&& (chNext4=='e')&& (chNext5=='f');
+
+ bool atendfor = (ch == 'e') && (chNext=='n') &&
+ (chNext2=='d') && (chNext3=='f') && (chNext4=='o')&& (chNext5=='r');
+
+ if(atenddef||atendfor
+ ){
+ levelCurrent-=1;
+ }
+
+
+ if (atEOL) {
+ int lev = levelPrev;
+ if (visibleChars == 0 && foldCompact)
+ lev |= SC_FOLDLEVELWHITEFLAG;
+ if ((levelCurrent > levelPrev) && (visibleChars > 0))
+ lev |= SC_FOLDLEVELHEADERFLAG;
+ if (lev != styler.LevelAt(lineCurrent)) {
+ styler.SetLevel(lineCurrent, lev);
+ }
+ lineCurrent++;
+ levelPrev = levelCurrent;
+ visibleChars = 0;
+ }
+
+ if (!isspacechar(ch))
+ visibleChars++;
+ }
+ // Fill in the real level of the next line, keeping the current flags as they will be filled in later
+ int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
+ styler.SetLevel(lineCurrent, levelPrev | flagsNext);
+
+}
+
+
+LexerModule lmMETAPOST(SCLEX_METAPOST, ColouriseMETAPOSTDoc, "metapost", FoldMetapostDoc, metapostWordListDesc);