Skip to content

Commit

Permalink
Change default scheme to plain text, add minimum auto detection for C…
Browse files Browse the repository at this point in the history
…/C++ sources, configuration files and shell scripts (they may have no file extension or arbitrary file extensions).
  • Loading branch information
zufuliu committed Jan 17, 2019
1 parent 3cb3ee0 commit bac9d20
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
Binary file modified doc/Notepad2.ini
Binary file not shown.
Binary file modified metapath/doc/metapath.ini
Binary file not shown.
9 changes: 6 additions & 3 deletions src/Notepad2.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,10 @@ static int iOpacityLevel = 75;
int iFindReplaceOpacityLevel= 75;
static int flagToolbarLook = 0;
int flagSimpleIndentGuides = 0;
int fNoHTMLGuess = 0;
int fNoCGIGuess = 0;
int fNoFileVariables = 0;
BOOL fNoHTMLGuess = 0;
BOOL fNoCGIGuess = 0;
BOOL fNoAutoDetection = 0;
BOOL fNoFileVariables = 0;
static int flagPosParam = 0;
static int flagDefaultPos = 0;
static int flagNewFromClipboard = 0;
Expand Down Expand Up @@ -6341,6 +6342,8 @@ void LoadFlags(void) {

flagSimpleIndentGuides = IniSectionGetBool(pIniSection, L"SimpleIndentGuides", 0);
fNoHTMLGuess = IniSectionGetBool(pIniSection, L"NoHTMLGuess", 0);
fNoCGIGuess = IniSectionGetBool(pIniSection, L"NoCGIGuess", 0);
fNoAutoDetection = IniSectionGetBool(pIniSection, L"NoAutoDetection", 0);
fNoFileVariables = IniSectionGetBool(pIniSection, L"NoFileVariables", 0);

if (StrIsEmpty(g_wchAppUserModelID)) {
Expand Down
74 changes: 70 additions & 4 deletions src/Styles.c
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,66 @@ PEDITLEXER Style_DetectObjCAndMatlab(void) {
return NULL;
}

PEDITLEXER Style_AutoDetect(PEDITLEXER pLexNew, BOOL bDotFile) {
char tchText[4096] = ""; // maybe contains header comments
SendMessage(hwndEdit, SCI_GETTEXT, COUNTOF(tchText) - 2, (LPARAM)tchText);

char *p = tchText;
const BOOL shebang = *p == '#' && p[1] == '!';
int cppCount = 0;
int sharpCount = 0;
BOOL maybeIni = FALSE;

while (*p) {
if (*p == '[') {
maybeIni = TRUE;
} else {
while (IsASpace(*p)) {
++p;
}
}
switch (*p) {
case '#': // C/C++ preprocessor, comment
if (!(p == tchText && shebang)) {
++p;
while (*p == ' ' || *p == '\t') {
++p;
}
if (MatchCPPKeyword(p, 2)) {
++cppCount;
} else {
++sharpCount;
}
}
break;
case '/': // C/C++ style comment
++p;
if (*p == '/' || *p == '*') {
return &lexCPP;
}
break;
}
// skip to next line
while (*p && !(*p == '\r' || *p == '\n')) {
++p;
}
}

if (cppCount > sharpCount && !(shebang || bDotFile)) {
return &lexCPP;
}
if (sharpCount) {
return shebang ? &lexBash : &lexCONF;
}
if (maybeIni) {
return &lexINI;
}
if (bDotFile) {
return &lexCONF;
}
return pLexNew;
}

//=============================================================================
//
// Style_GetCurrentLexerName()
Expand Down Expand Up @@ -1765,8 +1825,9 @@ PEDITLEXER Style_MatchLexer(LPCWSTR lpszMatch, BOOL bCheckNames) {
// find lexer from file name
// Style_SetLexerFromFile()
//
extern int fNoHTMLGuess;
extern int fNoCGIGuess;
extern BOOL fNoHTMLGuess;
extern BOOL fNoCGIGuess;
extern BOOL fNoAutoDetection;
extern FILEVARS fvCurFile;

static PEDITLEXER Style_GetLexerFromFile(HWND hwnd, LPCWSTR lpszFile, BOOL bCGIGuess, LPWSTR *pszExt, BOOL *pDotFile) {
Expand Down Expand Up @@ -1977,8 +2038,13 @@ void Style_SetLexerFromFile(HWND hwnd, LPCWSTR lpszFile) {
pLexNew = &lexANSI;
bFound = TRUE;
}
if (!bFound && bDotFile) {
pLexNew = &lexCONF;

if (!bFound && (!fNoAutoDetection || bDotFile)) {
if (!fNoAutoDetection) {
pLexNew = Style_AutoDetect(pLexNew, bDotFile);
} else {
pLexNew = &lexCONF;
}
}

// Apply the new lexer
Expand Down

0 comments on commit bac9d20

Please sign in to comment.