Diff of /lexers/LexOthers.cxx [46ef72] .. [e3ad39]  Maximize  Restore

  Switch to unified view

a/lexers/LexOthers.cxx b/lexers/LexOthers.cxx
1
// Scintilla source code edit control
1
// Scintilla source code edit control
2
/** @file LexOthers.cxx
2
/** @file LexOthers.cxx
3
 ** Lexers for batch files, diff results, properties files, make files and error lists.
3
 ** Lexers for batch files, diff results, properties files, make files and error lists.
4
 ** Also lexer for LaTeX documents.
5
 **/
4
 **/
6
// Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
5
// Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
7
// The License.txt file describes the conditions under which this software may be distributed.
6
// The License.txt file describes the conditions under which this software may be distributed.
8
7
9
#include <stdlib.h>
8
#include <stdlib.h>
...
...
1107
    if (linePos > 0) {  // Last line does not have ending characters
1106
    if (linePos > 0) {  // Last line does not have ending characters
1108
        ColouriseErrorListLine(lineBuffer, linePos, startPos + length - 1, styler, valueSeparate);
1107
        ColouriseErrorListLine(lineBuffer, linePos, startPos + length - 1, styler, valueSeparate);
1109
    }
1108
    }
1110
}
1109
}
1111
1110
1112
static bool latexIsSpecial(int ch) {
1113
  return (ch == '#') || (ch == '$') || (ch == '%') || (ch == '&') || (ch == '_') ||
1114
         (ch == '{') || (ch == '}') || (ch == ' ');
1115
}
1116
1117
static bool latexIsBlank(int ch) {
1118
  return (ch == ' ') || (ch == '\t');
1119
}
1120
1121
static bool latexIsBlankAndNL(int ch) {
1122
  return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n');
1123
}
1124
1125
static bool latexIsLetter(int ch) {
1126
  return isascii(ch) && isalpha(ch);
1127
}
1128
1129
static bool latexIsTagValid(int &i, int l, Accessor &styler) {
1130
  while (i < l) {
1131
      if (styler.SafeGetCharAt(i) == '{') {
1132
          while (i < l) {
1133
              i++;
1134
              if (styler.SafeGetCharAt(i) == '}') {
1135
                  return true;
1136
              }   else if (!latexIsLetter(styler.SafeGetCharAt(i)) &&
1137
                   styler.SafeGetCharAt(i)!='*') {
1138
                  return false;
1139
              }
1140
          }
1141
      } else if (!latexIsBlank(styler.SafeGetCharAt(i))) {
1142
          return false;
1143
      }
1144
      i++;
1145
  }
1146
  return false;
1147
}
1148
1149
static bool latexNextNotBlankIs(int i, int l, Accessor &styler, char needle) {
1150
  char ch;
1151
  while (i < l) {
1152
    ch = styler.SafeGetCharAt(i);
1153
      if (!latexIsBlankAndNL(ch) && ch != '*') {
1154
      if (ch == needle)
1155
        return true;
1156
      else
1157
        return false;
1158
      }
1159
      i++;
1160
  }
1161
  return false;
1162
}
1163
1164
static bool latexLastWordIs(int start, Accessor &styler, const char *needle) {
1165
  unsigned int i = 0;
1166
  unsigned int l = static_cast<unsigned int>(strlen(needle));
1167
  int ini = start-l+1;
1168
  char s[32];
1169
1170
  while (i < l && i < 32) {
1171
      s[i] = styler.SafeGetCharAt(ini + i);
1172
    i++;
1173
  }
1174
  s[i] = '\0';
1175
1176
  return (strcmp(s, needle) == 0);
1177
}
1178
1179
static void ColouriseLatexDoc(unsigned int startPos, int length, int initStyle,
1180
                              WordList *[], Accessor &styler) {
1181
1182
  styler.StartAt(startPos);
1183
1184
  int state = initStyle;
1185
  char chNext = styler.SafeGetCharAt(startPos);
1186
  styler.StartSegment(startPos);
1187
  int lengthDoc = startPos + length;
1188
  char chVerbatimDelim = '\0';
1189
1190
  for (int i = startPos; i < lengthDoc; i++) {
1191
      char ch = chNext;
1192
      chNext = styler.SafeGetCharAt(i + 1);
1193
1194
      if (styler.IsLeadByte(ch)) {
1195
          i++;
1196
          chNext = styler.SafeGetCharAt(i + 1);
1197
          continue;
1198
      }
1199
1200
      switch (state) {
1201
      case SCE_L_DEFAULT :
1202
          switch (ch) {
1203
          case '\\' :
1204
              styler.ColourTo(i - 1, state);
1205
              if (latexIsSpecial(chNext)) {
1206
                  state = SCE_L_SPECIAL;
1207
              } else {
1208
                  if (latexIsLetter(chNext)) {
1209
                      state = SCE_L_COMMAND;
1210
                  }   else {
1211
                      if (chNext == '(' || chNext == '[') {
1212
                          styler.ColourTo(i-1, state);
1213
                          styler.ColourTo(i+1, SCE_L_SHORTCMD);
1214
                          state = SCE_L_MATH;
1215
                          if (chNext == '[')
1216
                              state = SCE_L_MATH2;
1217
                          i++;
1218
                          chNext = styler.SafeGetCharAt(i+1);
1219
                      } else {
1220
                          state = SCE_L_SHORTCMD;
1221
                      }
1222
                  }
1223
              }
1224
              break;
1225
          case '$' :
1226
              styler.ColourTo(i - 1, state);
1227
              state = SCE_L_MATH;
1228
              if (chNext == '$') {
1229
                  state = SCE_L_MATH2;
1230
                  i++;
1231
                  chNext = styler.SafeGetCharAt(i + 1);
1232
              }
1233
              break;
1234
          case '%' :
1235
              styler.ColourTo(i - 1, state);
1236
              state = SCE_L_COMMENT;
1237
              break;
1238
          }
1239
          break;
1240
      case SCE_L_ERROR:
1241
          styler.ColourTo(i-1, state);
1242
          state = SCE_L_DEFAULT;
1243
          break;
1244
      case SCE_L_SPECIAL:
1245
      case SCE_L_SHORTCMD:
1246
          styler.ColourTo(i, state);
1247
          state = SCE_L_DEFAULT;
1248
          break;
1249
      case SCE_L_COMMAND :
1250
          if (!latexIsLetter(chNext)) {
1251
              styler.ColourTo(i, state);
1252
              state = SCE_L_DEFAULT;
1253
        if (latexNextNotBlankIs(i+1, lengthDoc, styler, '[' )) {
1254
          state = SCE_L_CMDOPT;
1255
              } else if (latexLastWordIs(i, styler, "\\begin")) {
1256
                  state = SCE_L_TAG;
1257
              } else if (latexLastWordIs(i, styler, "\\end")) {
1258
                  state = SCE_L_TAG2;
1259
              } else if (latexLastWordIs(i, styler, "\\verb") &&
1260
                   chNext != '*' && chNext != ' ') {
1261
          chVerbatimDelim = chNext;
1262
                  state = SCE_L_VERBATIM;
1263
              }
1264
          }
1265
          break;
1266
      case SCE_L_CMDOPT :
1267
      if (ch == ']') {
1268
        styler.ColourTo(i, state);
1269
        state = SCE_L_DEFAULT;
1270
      }
1271
          break;
1272
      case SCE_L_TAG :
1273
          if (latexIsTagValid(i, lengthDoc, styler)) {
1274
              styler.ColourTo(i, state);
1275
              state = SCE_L_DEFAULT;
1276
              if (latexLastWordIs(i, styler, "{verbatim}")) {
1277
                  state = SCE_L_VERBATIM;
1278
              } else if (latexLastWordIs(i, styler, "{comment}")) {
1279
                  state = SCE_L_COMMENT2;
1280
              } else if (latexLastWordIs(i, styler, "{math}")) {
1281
                  state = SCE_L_MATH;
1282
              } else if (latexLastWordIs(i, styler, "{displaymath}")) {
1283
                  state = SCE_L_MATH2;
1284
              } else if (latexLastWordIs(i, styler, "{equation}")) {
1285
                  state = SCE_L_MATH2;
1286
              }
1287
          } else {
1288
              state = SCE_L_ERROR;
1289
              styler.ColourTo(i, state);
1290
              state = SCE_L_DEFAULT;
1291
          }
1292
          chNext = styler.SafeGetCharAt(i+1);
1293
          break;
1294
      case SCE_L_TAG2 :
1295
          if (latexIsTagValid(i, lengthDoc, styler)) {
1296
              styler.ColourTo(i, state);
1297
              state = SCE_L_DEFAULT;
1298
          } else {
1299
              state = SCE_L_ERROR;
1300
          }
1301
          chNext = styler.SafeGetCharAt(i+1);
1302
          break;
1303
      case SCE_L_MATH :
1304
          if (ch == '$') {
1305
              styler.ColourTo(i, state);
1306
              state = SCE_L_DEFAULT;
1307
          } else if (ch == '\\' && chNext == ')') {
1308
              styler.ColourTo(i-1, state);
1309
              styler.ColourTo(i+1, SCE_L_SHORTCMD);
1310
              i++;
1311
              chNext = styler.SafeGetCharAt(i+1);
1312
              state = SCE_L_DEFAULT;
1313
          } else if (ch == '\\') {
1314
              int match = i + 3;
1315
              if (latexLastWordIs(match, styler, "\\end")) {
1316
                  match++;
1317
                  if (latexIsTagValid(match, lengthDoc, styler)) {
1318
                      if (latexLastWordIs(match, styler, "{math}")) {
1319
                          styler.ColourTo(i-1, state);
1320
                          state = SCE_L_COMMAND;
1321
                      }
1322
                  }
1323
              }
1324
          }
1325
1326
          break;
1327
      case SCE_L_MATH2 :
1328
          if (ch == '$') {
1329
        if (chNext == '$') {
1330
          i++;
1331
          chNext = styler.SafeGetCharAt(i + 1);
1332
          styler.ColourTo(i, state);
1333
          state = SCE_L_DEFAULT;
1334
        } else {
1335
          styler.ColourTo(i, SCE_L_ERROR);
1336
          state = SCE_L_DEFAULT;
1337
        }
1338
          } else if (ch == '\\' && chNext == ']') {
1339
              styler.ColourTo(i-1, state);
1340
              styler.ColourTo(i+1, SCE_L_SHORTCMD);
1341
              i++;
1342
              chNext = styler.SafeGetCharAt(i+1);
1343
              state = SCE_L_DEFAULT;
1344
          } else if (ch == '\\') {
1345
              int match = i + 3;
1346
              if (latexLastWordIs(match, styler, "\\end")) {
1347
                  match++;
1348
                  if (latexIsTagValid(match, lengthDoc, styler)) {
1349
                      if (latexLastWordIs(match, styler, "{displaymath}")) {
1350
                          styler.ColourTo(i-1, state);
1351
                          state = SCE_L_COMMAND;
1352
                      } else if (latexLastWordIs(match, styler, "{equation}")) {
1353
                          styler.ColourTo(i-1, state);
1354
                          state = SCE_L_COMMAND;
1355
                      }
1356
                  }
1357
              }
1358
          }
1359
          break;
1360
      case SCE_L_COMMENT :
1361
          if (ch == '\r' || ch == '\n') {
1362
              styler.ColourTo(i - 1, state);
1363
              state = SCE_L_DEFAULT;
1364
          }
1365
          break;
1366
      case SCE_L_COMMENT2 :
1367
          if (ch == '\\') {
1368
              int match = i + 3;
1369
              if (latexLastWordIs(match, styler, "\\end")) {
1370
                  match++;
1371
                  if (latexIsTagValid(match, lengthDoc, styler)) {
1372
                      if (latexLastWordIs(match, styler, "{comment}")) {
1373
                          styler.ColourTo(i-1, state);
1374
                          state = SCE_L_COMMAND;
1375
                      }
1376
                  }
1377
              }
1378
          }
1379
          break;
1380
      case SCE_L_VERBATIM :
1381
          if (ch == '\\') {
1382
              int match = i + 3;
1383
              if (latexLastWordIs(match, styler, "\\end")) {
1384
                  match++;
1385
                  if (latexIsTagValid(match, lengthDoc, styler)) {
1386
                      if (latexLastWordIs(match, styler, "{verbatim}")) {
1387
                          styler.ColourTo(i-1, state);
1388
                          state = SCE_L_COMMAND;
1389
                      }
1390
                  }
1391
              }
1392
          } else if (chNext == chVerbatimDelim) {
1393
        styler.ColourTo(i+1, state);
1394
              state = SCE_L_DEFAULT;
1395
        chVerbatimDelim = '\0';
1396
      } else if (chVerbatimDelim != '\0' && (ch == '\n' || ch == '\r')) {
1397
        styler.ColourTo(i, SCE_L_ERROR);
1398
              state = SCE_L_DEFAULT;
1399
        chVerbatimDelim = '\0';
1400
      }
1401
          break;
1402
      }
1403
  }
1404
  styler.ColourTo(lengthDoc-1, state);
1405
}
1406
1407
static const char *const batchWordListDesc[] = {
1111
static const char *const batchWordListDesc[] = {
1408
    "Internal Commands",
1112
    "Internal Commands",
1409
    "External Commands",
1113
    "External Commands",
1410
    0
1114
    0
1411
};
1115
};
...
...
1427
LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc, "batch", 0, batchWordListDesc);
1131
LexerModule lmBatch(SCLEX_BATCH, ColouriseBatchDoc, "batch", 0, batchWordListDesc);
1428
LexerModule lmDiff(SCLEX_DIFF, ColouriseDiffDoc, "diff", FoldDiffDoc, emptyWordListDesc);
1132
LexerModule lmDiff(SCLEX_DIFF, ColouriseDiffDoc, "diff", FoldDiffDoc, emptyWordListDesc);
1429
LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc, "props", FoldPropsDoc, emptyWordListDesc);
1133
LexerModule lmProps(SCLEX_PROPERTIES, ColourisePropsDoc, "props", FoldPropsDoc, emptyWordListDesc);
1430
LexerModule lmMake(SCLEX_MAKEFILE, ColouriseMakeDoc, "makefile", 0, emptyWordListDesc);
1134
LexerModule lmMake(SCLEX_MAKEFILE, ColouriseMakeDoc, "makefile", 0, emptyWordListDesc);
1431
LexerModule lmErrorList(SCLEX_ERRORLIST, ColouriseErrorListDoc, "errorlist", 0, emptyWordListDesc);
1135
LexerModule lmErrorList(SCLEX_ERRORLIST, ColouriseErrorListDoc, "errorlist", 0, emptyWordListDesc);
1432
LexerModule lmLatex(SCLEX_LATEX, ColouriseLatexDoc, "latex", 0, emptyWordListDesc);
1433
LexerModule lmNull(SCLEX_NULL, ColouriseNullDoc, "null");
1136
LexerModule lmNull(SCLEX_NULL, ColouriseNullDoc, "null");

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks