From c5893685400999ce4a385b9b6888393b9bddb069 Mon Sep 17 00:00:00 2001 From: "METANEOCORTEX\\Kotti" Date: Sun, 8 Nov 2026 13:53:04 +0100 Subject: [PATCH] fix: Sort scheme dialogs alphabetically while preserving lexer indices. Keep default schemes first and add sorting regression tests. Co-authored-by: Copilot --- Build/Notepad3.ini | 7 +- Build/Notepad3_pap.ini | 7 +- Build/Notepad3_ptb_x64.ini | 7 +- Build/Notepad3_ptb_x86.ini | 7 +- Readme.md | 2 +- readme/faq/FAQ.md | 2 +- readme/schema/CustomSchema.md | 2 +- src/PathLib.c | 2 +- src/Styles.c | 135 +++++++++++++++++++++++----------- 9 files changed, 121 insertions(+), 50 deletions(-) diff --git a/Build/Notepad3.ini b/Build/Notepad3.ini index 1185a9af94..7a456cec11 100644 --- a/Build/Notepad3.ini +++ b/Build/Notepad3.ini @@ -149,6 +149,11 @@ SettingsVersion=5 ;31=Reset Zoom ;32=New Empty Window [Styles] +;DefaultScheme=2 ; zero-based lexer index (2 = Pure Text Files) +;Use2ndDefaultStyle=false +;AutoSelect=true +;SelectDlgSizeX=305 +;SelectDlgSizeY=344 [Window] ;x DefaultWindowPosition= [Suppressed Messages] @@ -157,6 +162,7 @@ SettingsVersion=5 [2nd Common Base] [Text Files] [ANSI Art] +[ANSYS APDL] [Apache Config Files] [Assembly Script] [AutoHotkey Script] @@ -216,4 +222,3 @@ SettingsVersion=5 [Recent Files] [Recent Find] [Recent Replace] -[ANSYS APDL] diff --git a/Build/Notepad3_pap.ini b/Build/Notepad3_pap.ini index 9e785165ed..416b5dca49 100644 --- a/Build/Notepad3_pap.ini +++ b/Build/Notepad3_pap.ini @@ -149,6 +149,11 @@ grepWin.exe=%PORTABLEAPPS_DIR%\grepWinPortable\grepWinPortable.exe ;31=Reset Zoom ;32=New Empty Window [Styles] +;DefaultScheme=2 ; zero-based lexer index (2 = Pure Text Files) +;Use2ndDefaultStyle=false +;AutoSelect=true +;SelectDlgSizeX=305 +;SelectDlgSizeY=344 [Window] ;x DefaultWindowPosition= [Suppressed Messages] @@ -157,6 +162,7 @@ grepWin.exe=%PORTABLEAPPS_DIR%\grepWinPortable\grepWinPortable.exe [2nd Common Base] [Text Files] [ANSI Art] +[ANSYS APDL] [Apache Config Files] [Assembly Script] [AutoHotkey Script] @@ -216,4 +222,3 @@ grepWin.exe=%PORTABLEAPPS_DIR%\grepWinPortable\grepWinPortable.exe [Recent Files] [Recent Find] [Recent Replace] -[ANSYS APDL] diff --git a/Build/Notepad3_ptb_x64.ini b/Build/Notepad3_ptb_x64.ini index b9d54d438f..83a4750033 100644 --- a/Build/Notepad3_ptb_x64.ini +++ b/Build/Notepad3_ptb_x64.ini @@ -149,6 +149,11 @@ Favorites=.\Favorites\ ;31=Reset Zoom ;32=New Empty Window [Styles] +;DefaultScheme=2 ; zero-based lexer index (2 = Pure Text Files) +;Use2ndDefaultStyle=false +;AutoSelect=true +;SelectDlgSizeX=305 +;SelectDlgSizeY=344 [Window] ;x DefaultWindowPosition= [Suppressed Messages] @@ -157,6 +162,7 @@ Favorites=.\Favorites\ [2nd Common Base] [Text Files] [ANSI Art] +[ANSYS APDL] [Apache Config Files] [Assembly Script] [AutoHotkey Script] @@ -216,4 +222,3 @@ Favorites=.\Favorites\ [Recent Files] [Recent Find] [Recent Replace] -[ANSYS APDL] diff --git a/Build/Notepad3_ptb_x86.ini b/Build/Notepad3_ptb_x86.ini index d6cd22689c..a88d0ce076 100644 --- a/Build/Notepad3_ptb_x86.ini +++ b/Build/Notepad3_ptb_x86.ini @@ -149,6 +149,11 @@ Favorites=.\Favorites\ ;31=Reset Zoom ;32=New Empty Window [Styles] +;DefaultScheme=2 ; zero-based lexer index (2 = Pure Text Files) +;Use2ndDefaultStyle=false +;AutoSelect=true +;SelectDlgSizeX=305 +;SelectDlgSizeY=344 [Window] ;x DefaultWindowPosition= [Suppressed Messages] @@ -157,6 +162,7 @@ Favorites=.\Favorites\ [2nd Common Base] [Text Files] [ANSI Art] +[ANSYS APDL] [Apache Config Files] [Assembly Script] [AutoHotkey Script] @@ -216,4 +222,3 @@ Favorites=.\Favorites\ [Recent Files] [Recent Find] [Recent Replace] -[ANSYS APDL] diff --git a/Readme.md b/Readme.md index 3e07f402ad..ce7d91bbee 100644 --- a/Readme.md +++ b/Readme.md @@ -40,7 +40,7 @@ Notepad3 is a free, open-source text editor with syntax highlighting for Windows ### Syntax Highlighting Over 55 languages supported, including: -> ANSI Art, Apache Config, Assembly, AutoHotkey, AutoIt3, AviSynth, Awk, Batch, C/C++, C#, CMake, CoffeeScript, CSS, CSV, D, Dart, Diff, F77/Fortran, Go, HTML/XML, Java, JavaScript, JSON, Julia, Kotlin, KiXtart, LaTeX, Lua, Makefiles, Markdown, MATLAB, Nim, NSIS, Pascal, Perl, PHP, PowerShell, Python, R/S-Plus, Registry, Resource Script, Ruby, Rust, Shell Script, SQL, SystemVerilog, Tcl, TOML, VBScript, VHDL, Verilog, Visual Basic, YAML, and more. +> ANSI Art, ANSYS APDL, Apache Config, Assembly, AutoHotkey, AutoIt3, AviSynth, Awk, Batch, C/C++, C#, CMake, CoffeeScript, CSS, CSV, D, Dart, Diff, F77/Fortran, Go, HTML/XML, Java, JavaScript, JSON, Julia, Kotlin, KiXtart, LaTeX, Lua, Makefiles, Markdown, MATLAB, Nim, NSIS, Pascal, Perl, PHP, PowerShell, Python, R/S-Plus, Registry, Resource Script, Ruby, Rust, Shell Script, SQL, SystemVerilog, Tcl, TOML, VBScript, VHDL, Verilog, Visual Basic, YAML, and more. ### File Handling - **[AES-256 Rijndael](readme/encryption/Encryption.md)** encryption/decryption (in-app and command-line batch tool) diff --git a/readme/faq/FAQ.md b/readme/faq/FAQ.md index 176abf1717..fa63c31668 100644 --- a/readme/faq/FAQ.md +++ b/readme/faq/FAQ.md @@ -24,7 +24,7 @@ A fast, lightweight, Scintilla/Lexilla-based text editor for Windows. Notepad3 i Notepad3 keeps the small footprint and most of the Notepad2 shortcut set, while adding plenty of new functionality on top of Notepad2-mod's code folding, bookmarks, mark-all-occurrences, and word auto-completion. The differences you're most likely to notice: - **Modern regex engine.** Find/Replace uses **PCRE2** (full Perl-compatible regex) instead of Scintilla's basic subset — multi-line search, lookarounds, named groups, and Unicode all work. -- **More languages.** Over 55 syntax lexers, including AutoHotkey, AutoIt3, AviSynth, Awk, CMake, CoffeeScript, CSV, D, Dart, Go, JSON, Julia, Kotlin, LaTeX, Lua, Markdown, MATLAB, Nim, NSIS, R, Registry, Ruby, Rust, SystemVerilog, Tcl, TOML, VHDL, Verilog, YAML, and more. +- **More languages.** Over 55 syntax lexers, including ANSYS APDL, AutoHotkey, AutoIt3, AviSynth, Awk, CMake, CoffeeScript, CSV, D, Dart, Go, JSON, Julia, Kotlin, LaTeX, Lua, Markdown, MATLAB, Nim, NSIS, R, Registry, Ruby, Rust, SystemVerilog, Tcl, TOML, VHDL, Verilog, YAML, and more. - **Better encoding handling.** Automatic detection via [uchardet](https://www.freedesktop.org/wiki/Software/uchardet/), plus a much wider range of supported code pages. - **Dark mode** and high-DPI awareness, with crisp toolbar icons at every scale factor. - **Built-in AES-256 encryption** for sensitive files (with an optional command-line companion). diff --git a/readme/schema/CustomSchema.md b/readme/schema/CustomSchema.md index 4468207a36..eb178190ec 100644 --- a/readme/schema/CustomSchema.md +++ b/readme/schema/CustomSchema.md @@ -30,7 +30,7 @@ Audience: advanced users who want to tweak colours and fonts beyond the built-in | **Schema** / **Scheme** | A complete set of styles for one language (e.g. *C/C++ Source Code*, *Python Script*). Backed in code by an `EDITLEXER` record plus an array of `EDITSTYLE` entries. | | **Style** | A single styleable element inside a schema (e.g. *Default Style*, *Keywords*, *Comments*, *Strings*). Each style has a plain-text *style string* describing its font, size, colours, etc. | | **Theme** | A complete snapshot of all custom colours, schemas and per-schema extensions, stored as a standalone `.ini` file (e.g. `Dark.ini`). Switching themes replaces every schema's style strings at once. | -| **Lexilla lexer** | The underlying syntax-highlighting engine for a schema. Notepad3 ships with ~60 lexers (C++, Python, Markdown, Rust, YAML, …). | +| **Lexilla lexer** | The underlying syntax-highlighting engine for a schema. Notepad3 ships with ~60 lexers (ANSYS APDL, C++, Python, Markdown, Rust, YAML, …). | | **Custom colours** | A 16-slot colour palette stored per-installation. Used by the *Customize Schemes* colour-picker and exported with every theme. | Notepad3 ships with roughly 60 schemas; the exact list appears in the *Select Scheme* dialog (**Ctrl+F11**) and in the left-hand tree of *Customize Schemes* (**F12**). diff --git a/src/PathLib.c b/src/PathLib.c index 58bfb5c62d..e4f65ad08b 100644 --- a/src/PathLib.c +++ b/src/PathLib.c @@ -1811,7 +1811,7 @@ bool PTHAPI Path_CreateFavLnk(LPCWSTR lpszLinkName, const HPATHL hTargetPth, con // // Path_CreateDeskLnk() // -// Modified to create a desktop link to Notepad2 +// Modified to create a desktop link to Notepad3 // bool PTHAPI Path_CreateDeskLnk(const HPATHL hDocumentPath, LPCWSTR pszDescription) { diff --git a/src/Styles.c b/src/Styles.c index 8ebe6cc94a..4fe926ec1e 100644 --- a/src/Styles.c +++ b/src/Styles.c @@ -51,9 +51,8 @@ extern COLORREF g_colorCustom[16]; // ---------------------------------------------------------------------------- -// This array holds all the lexers... -// Don't forget to change the number of the lexer for HTML and XML -// in Notepad2.c ParseCommandLine() if you change this array! +// Array positions are used by saved DefaultScheme values and numeric lexer selection. +// Append new lexers to preserve existing indices; dialogs sort their displayed entries separately. static PEDITLEXER g_pLexArray[] = { &lexStandard, // Default Text &lexStandard2nd, // 2nd Default Text @@ -79,7 +78,6 @@ static PEDITLEXER g_pLexArray[] = { &lexDart, // Dart Source Code &lexDIFF, // Diff Files &lexFortran, // Fortran F90+ - //&lexF77, // Fortran F77 &lexGo, // Go Source Code &lexINNO, // Inno Setup Script &lexJAVA, // Java Source Code @@ -117,7 +115,7 @@ static PEDITLEXER g_pLexArray[] = { &lexHTML, // Web Source Code &lexXML, // XML Document &lexYAML, // YAML - &lexAPDL, // ANSYS APDL (append to preserve saved lexer indices) + &lexAPDL, // ANSYS APDL }; int Style_NumOfLexers() { @@ -4825,6 +4823,69 @@ void Style_GetLexerDisplayName(PEDITLEXER pLexer, LPWSTR lpszName, int cchName) } } +static int _GetLexerIndex(PEDITLEXER pLexer) +{ + for (int i = 0; i < COUNTOF(g_pLexArray); ++i) { + if (g_pLexArray[i] == pLexer) { + return i; + } + } + return -1; +} + +static int CALLBACK _CompareLexersByDisplayName(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) +{ + UNREFERENCED_PARAMETER(lParamSort); + + PEDITLEXER const pLexer1 = (PEDITLEXER)lParam1; + PEDITLEXER const pLexer2 = (PEDITLEXER)lParam2; + if (pLexer1 == pLexer2) { + return 0; + } + + // Keep the two default schemes and Pure Text at the top in their existing order. + for (int i = 0; i < 3; ++i) { + if (pLexer1 == g_pLexArray[i]) { + return -1; + } + if (pLexer2 == g_pLexArray[i]) { + return 1; + } + } + + WCHAR name1[MIDSZ_BUFFER] = { L'\0' }; + WCHAR name2[MIDSZ_BUFFER] = { L'\0' }; + Style_GetLexerDisplayName(pLexer1, name1, COUNTOF(name1)); + Style_GetLexerDisplayName(pLexer2, name2, COUNTOF(name2)); + + int const cmp = StrCmpIW(name1, name2); + return (cmp != 0) ? cmp : _GetLexerIndex(pLexer1) - _GetLexerIndex(pLexer2); +} + +static HTREEITEM _FindLexerTreeItem(HWND hwndTV, PEDITLEXER pLexer) +{ + for (HTREEITEM hItem = TreeView_GetRoot(hwndTV); hItem; hItem = TreeView_GetNextSibling(hwndTV, hItem)) { + TVITEM tvi = { 0 }; + tvi.mask = TVIF_PARAM; + tvi.hItem = hItem; + if (TreeView_GetItem(hwndTV, &tvi) && ((PEDITLEXER)tvi.lParam == pLexer)) { + return hItem; + } + } + return NULL; +} + +static PEDITLEXER _GetListViewLexer(HWND hwndLV, int iItem) +{ + if (iItem < 0) { + return NULL; + } + LVITEM lvi = { 0 }; + lvi.mask = LVIF_PARAM; + lvi.iItem = iItem; + return ListView_GetItem(hwndLV, &lvi) ? (PEDITLEXER)lvi.lParam : NULL; +} + //============================================================================= // @@ -5111,14 +5172,7 @@ INT_PTR CALLBACK Style_CustomizeSchemesDlgProc(HWND hwnd, UINT umsg, WPARAM wPar flagIconSize | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES), TVSIL_NORMAL); - // findlexer - int found = -1; - for (int i = 0; i < COUNTOF(g_pLexArray); ++i) { - if (g_pLexArray[i] == s_pLexCurrent) { - found = i; - break; - } - } + int const found = _GetLexerIndex(s_pLexCurrent); // Build lexer tree view HTREEITEM hCurrentTVLex = NULL; @@ -5129,11 +5183,11 @@ INT_PTR CALLBACK Style_CustomizeSchemesDlgProc(HWND hwnd, UINT umsg, WPARAM wPar Style_AddLexerToTreeView(hwndTV, g_pLexArray[i]); } } + TV_SORTCB tvSort = { NULL, _CompareLexersByDisplayName, 0 }; + TreeView_SortChildrenCB(hwndTV, &tvSort, FALSE); if (!hCurrentTVLex) { - hCurrentTVLex = TreeView_GetRoot(hwndTV); - if (Style_GetUse2ndDefault()) { - hCurrentTVLex = TreeView_GetNextSibling(hwndTV, hCurrentTVLex); - } + PEDITLEXER const pDefaultTextLexer = Style_GetUse2ndDefault() ? &lexStandard2nd : &lexStandard; + hCurrentTVLex = _FindLexerTreeItem(hwndTV, pDefaultTextLexer); } TreeView_Select(hwndTV, hCurrentTVLex, TVGN_CARET); TreeView_Expand(hwndTV, hCurrentTVLex, TVE_EXPAND); @@ -5518,16 +5572,13 @@ INT_PTR CALLBACK Style_CustomizeSchemesDlgProc(HWND hwnd, UINT umsg, WPARAM wPar switch (LOWORD(wParam)) { case IDC_SETCURLEXERTV: { // find current lexer's tree entry - HTREEITEM hCurrentTVLex = TreeView_GetRoot(hwndTV); - for (int i = 0; i < COUNTOF(g_pLexArray); ++i) { - if (g_pLexArray[i] == s_pLexCurrent) { - break; - } - hCurrentTVLex = TreeView_GetNextSibling(hwndTV, hCurrentTVLex); // next - } if (s_pLexCurrent == pCurrentLexer) { break; // no change } + HTREEITEM hCurrentTVLex = _FindLexerTreeItem(hwndTV, s_pLexCurrent); + if (!hCurrentTVLex) { + break; + } // collaps current node HTREEITEM hSel = TreeView_GetSelection(hwndTV); @@ -5773,7 +5824,7 @@ INT_PTR CALLBACK Style_SelectLexerDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPAR static HWND hwndLV = NULL; - static int iInternalDefault = 0; + static PEDITLEXER pDefaultLexer = NULL; static PEDITLEXER* pSelectedLexer = NULL; @@ -5781,6 +5832,7 @@ INT_PTR CALLBACK Style_SelectLexerDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPAR case WM_INITDIALOG: { pSelectedLexer = (PEDITLEXER*)lParam; + pDefaultLexer = GetDefaultLexer(); SetDialogIconNP3(hwnd); InitWindowCommon(hwnd, true); @@ -5824,25 +5876,21 @@ INT_PTR CALLBACK Style_SelectLexerDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPAR for (int i = 0; i < COUNTOF(g_pLexArray); i++) { Style_AddLexerToListView(hwndLV, g_pLexArray[i]); } + ListView_SortItems(hwndLV, _CompareLexersByDisplayName, 0); ListView_SetColumnWidth(hwndLV,0,LVSCW_AUTOSIZE_USEHEADER); // Select current lexer int lvItems = ListView_GetItemCount(hwndLV); - LVITEM lvi = { 0 }; - lvi.mask = LVIF_PARAM; for (int i = 0; i < lvItems; i++) { - lvi.iItem = i; - ListView_GetItem(hwndLV,&lvi); - - if (((PEDITLEXER)lvi.lParam)->resID == (*pSelectedLexer)->resID) { + PEDITLEXER const pLexer = _GetListViewLexer(hwndLV, i); + if (pLexer && (pLexer->resID == (*pSelectedLexer)->resID)) { ListView_SetItemState(hwndLV,i,LVIS_FOCUSED|LVIS_SELECTED,LVIS_FOCUSED|LVIS_SELECTED); ListView_EnsureVisible(hwndLV,i,false); - CheckDlgButton(hwnd, IDC_DEFAULTSCHEME, SetBtn(s_iDefaultLexer == i)); + CheckDlgButton(hwnd, IDC_DEFAULTSCHEME, SetBtn(pLexer == pDefaultLexer)); break; } } - iInternalDefault = s_iDefaultLexer; CheckDlgButton(hwnd,IDC_AUTOSELECT, SetBtn(s_bAutoSelect)); CenterDlgInParent(hwnd, false); @@ -5925,7 +5973,9 @@ INT_PTR CALLBACK Style_SelectLexerDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPAR case LVN_ITEMCHANGED: case LVN_DELETEITEM: { int i = ListView_GetNextItem(hwndLV, -1, LVNI_ALL | LVNI_SELECTED); - CheckDlgButton(hwnd, IDC_DEFAULTSCHEME, SetBtn(iInternalDefault == i)); + PEDITLEXER const pSelected = _GetListViewLexer(hwndLV, i); + CheckDlgButton(hwnd, IDC_DEFAULTSCHEME, + SetBtn(pSelected && (pSelected == pDefaultLexer))); DialogEnableControl(hwnd, IDC_DEFAULTSCHEME, i != -1); DialogEnableControl(hwnd, IDOK, i != -1); } @@ -5940,20 +5990,21 @@ INT_PTR CALLBACK Style_SelectLexerDlgProc(HWND hwnd,UINT umsg,WPARAM wParam,LPAR switch (LOWORD(wParam)) { case IDC_DEFAULTSCHEME: if (IsButtonChecked(hwnd, IDC_DEFAULTSCHEME)) { - iInternalDefault = ListView_GetNextItem(hwndLV, -1, LVNI_ALL | LVNI_SELECTED); + int const iSelected = ListView_GetNextItem(hwndLV, -1, LVNI_ALL | LVNI_SELECTED); + PEDITLEXER const pSelected = _GetListViewLexer(hwndLV, iSelected); + pDefaultLexer = pSelected ? pSelected : &lexStandard; } else { - iInternalDefault = 0; + pDefaultLexer = &lexStandard; } break; case IDOK: { - LVITEM lvi = {0}; - lvi.mask = LVIF_PARAM; - lvi.iItem = ListView_GetNextItem(hwndLV, -1, LVNI_ALL | LVNI_SELECTED); - if (ListView_GetItem(hwndLV, &lvi)) { - *pSelectedLexer = (PEDITLEXER)lvi.lParam; - s_iDefaultLexer = iInternalDefault; + int const iSelected = ListView_GetNextItem(hwndLV, -1, LVNI_ALL | LVNI_SELECTED); + PEDITLEXER const pSelected = _GetListViewLexer(hwndLV, iSelected); + if (pSelected) { + *pSelectedLexer = pSelected; + s_iDefaultLexer = _GetLexerIndex(pDefaultLexer); s_bAutoSelect = IsButtonChecked(hwnd, IDC_AUTOSELECT); //@@@??? Flags.bHugeFileLoadState = false; // user choice EndDialog(hwnd,IDOK);