Skip to content

Commit

Permalink
Load scheme Tab settings on demand, issue zufuliu#311.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Apr 9, 2021
1 parent cc6b64b commit 1aa26fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
8 changes: 2 additions & 6 deletions src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,7 @@ static INT_PTR CALLBACK TabSettingsDlgProc(HWND hwnd, UINT umsg, WPARAM wParam,
case WM_INITDIALOG: {
WCHAR wch[MAX_EDITLEXER_NAME_SIZE];
LPCWSTR pszName;
Style_LoadTabSettings(pLexCurrent);
#if NP2_ENABLE_LOCALIZE_LEXER_NAME
if (GetString(pLexCurrent->rid, wch, COUNTOF(wch))) {
pszName = wch;
Expand Down Expand Up @@ -1710,12 +1711,7 @@ static INT_PTR CALLBACK TabSettingsDlgProc(HWND hwnd, UINT umsg, WPARAM wParam,
fvCurFile.bTabIndents = IsButtonChecked(hwnd, IDC_TAB_INDENT);
tabSettings.bTabIndents = fvCurFile.bTabIndents;
tabSettings.bBackspaceUnindents = IsButtonChecked(hwnd, IDC_BACKSPACE_UNINDENT);

LPCWSTR lpSection = pLexCurrent->pszName;
IniSetIntEx(lpSection, L"TabWidth", tabSettings.schemeTabWidth, pLexCurrent->defaultTabWidth);
IniSetIntEx(lpSection, L"IndentWidth", tabSettings.schemeIndentWidth, pLexCurrent->defaultIndentWidth);
IniSetBoolEx(lpSection, L"TabsAsSpaces", tabSettings.schemeTabsAsSpaces, pLexCurrent->defaultTabsAsSpaces);
IniSetBoolEx(lpSection, L"UseGlobalTabSettings", tabSettings.schemeUseGlobalTabSettings, pLexCurrent->defaultUseGlobalTabSettings);
Style_SaveTabSettings(pLexCurrent);
EndDialog(hwnd, IDOK);
}
break;
Expand Down
29 changes: 22 additions & 7 deletions src/Styles.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ static inline void FindDarkThemeFile(void) {
FindExtraIniFile(darkStyleThemeFilePath, L"Notepad2 DarkTheme.ini", L"DarkTheme.ini");
}

static inline void Style_LoadTabSettings(PEDITLEXER pLex) {
void Style_LoadTabSettings(PEDITLEXER pLex) {
LPCWSTR lpSection = pLex->pszName;
int iValue = IniGetInt(lpSection, L"TabWidth", pLex->defaultTabWidth);
tabSettings.schemeTabWidth = clamp_i(iValue, TAB_WIDTH_MIN, TAB_WIDTH_MAX);
Expand All @@ -620,6 +620,21 @@ static inline void Style_LoadTabSettings(PEDITLEXER pLex) {
tabSettings.schemeUseGlobalTabSettings = IniGetInt(lpSection, L"UseGlobalTabSettings", pLex->defaultUseGlobalTabSettings);
}

void Style_SaveTabSettings(PEDITLEXER pLex) {
LPCWSTR lpSection = pLex->pszName;
IniSetIntEx(lpSection, L"TabWidth", tabSettings.schemeTabWidth, pLex->defaultTabWidth);
IniSetIntEx(lpSection, L"IndentWidth", tabSettings.schemeIndentWidth, pLex->defaultIndentWidth);
IniSetBoolEx(lpSection, L"TabsAsSpaces", tabSettings.schemeTabsAsSpaces, pLex->defaultTabsAsSpaces);
IniSetBoolEx(lpSection, L"UseGlobalTabSettings", tabSettings.schemeUseGlobalTabSettings, pLex->defaultUseGlobalTabSettings);
}

static inline void SaveLexTabSettings(IniSectionOnSave *pIniSection, PEDITLEXER pLex) {
IniSectionSetIntEx(pIniSection, L"TabWidth", tabSettings.schemeTabWidth, pLex->defaultTabWidth);
IniSectionSetIntEx(pIniSection, L"IndentWidth", tabSettings.schemeIndentWidth, pLex->defaultIndentWidth);
IniSectionSetBoolEx(pIniSection, L"TabsAsSpaces", tabSettings.schemeTabsAsSpaces, pLex->defaultTabsAsSpaces);
IniSectionSetBoolEx(pIniSection, L"UseGlobalTabSettings", tabSettings.schemeUseGlobalTabSettings, pLex->defaultUseGlobalTabSettings);
}

static void Style_LoadOneEx(PEDITLEXER pLex, IniSection *pIniSection, WCHAR *pIniSectionBuf, int cchIniSection) {
LPCWSTR themePath = GetStyleThemeFilePath();
GetPrivateProfileSection(pLex->pszName, pIniSectionBuf, cchIniSection, themePath);
Expand Down Expand Up @@ -924,10 +939,7 @@ void Style_Save(void) {
IniSectionSetStringEx(pIniSection, pLex->Styles[i].pszName, pLex->Styles[i].szValue, pLex->Styles[i].pszDefault);
}
if (pLex == pLexCurrent && pLex->iStyleTheme == StyleTheme_Default) {
IniSectionSetIntEx(pIniSection, L"TabWidth", tabSettings.schemeTabWidth, pLex->defaultTabWidth);
IniSectionSetIntEx(pIniSection, L"IndentWidth", tabSettings.schemeIndentWidth, pLex->defaultIndentWidth);
IniSectionSetBoolEx(pIniSection, L"TabsAsSpaces", tabSettings.schemeTabsAsSpaces, pLex->defaultTabsAsSpaces);
IniSectionSetBoolEx(pIniSection, L"UseGlobalTabSettings", tabSettings.schemeUseGlobalTabSettings, pLex->defaultUseGlobalTabSettings);
SaveLexTabSettings(pIniSection, pLex);
}
// delete this section if nothing changed
WritePrivateProfileSection(pLex->pszName, StrIsEmpty(pIniSectionBuf) ? NULL : pIniSectionBuf, themePath);
Expand Down Expand Up @@ -1511,8 +1523,11 @@ void Style_SetLexer(PEDITLEXER pLexNew, BOOL bLexerChanged) {
int rid = pLexNew->rid;

if (bLexerChanged) {
Style_LoadTabSettings(pLexNew);
FileVars_Apply(&fvCurFile);
if ((fvCurFile.mask & FV_MaskHasFileTabSettings) != FV_MaskHasFileTabSettings) {
// otherwise, the same tab settings already applied in EditSetNewText().
Style_LoadTabSettings(pLexNew);
FileVars_Apply(&fvCurFile);
}
SciCall_SetLexer(iLexer);

if (iLexer == SCLEX_CPP || iLexer == SCLEX_MATLAB) {
Expand Down
2 changes: 2 additions & 0 deletions src/Styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ void Style_Load(void);
void Style_Save(void);
BOOL Style_Import(HWND hwnd);
BOOL Style_Export(HWND hwnd);
void Style_LoadTabSettings(PEDITLEXER pLex);
void Style_SaveTabSettings(PEDITLEXER pLex);

void Style_DetectBaseFontSize(HWND hwnd);
HFONT Style_CreateCodeFont(UINT dpi);
Expand Down

0 comments on commit 1aa26fd

Please sign in to comment.