Skip to content

Commit

Permalink
Support WinHex script and template, issue #714.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Sep 7, 2023
1 parent 12c61dd commit 76579cb
Show file tree
Hide file tree
Showing 18 changed files with 449 additions and 1 deletion.
2 changes: 2 additions & 0 deletions build/CodeLite/Notepad2.project
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
<File Name="../../scintilla/lexers/LexVHDL.cxx"/>
<File Name="../../scintilla/lexers/LexVim.cxx"/>
<File Name="../../scintilla/lexers/LexWASM.cxx"/>
<File Name="../../scintilla/lexers/LexWinHex.cxx"/>
<File Name="../../scintilla/lexers/LexYAML.cxx"/>
<File Name="../../scintilla/lexers/LexZig.cxx"/>
</VirtualDirectory>
Expand Down Expand Up @@ -269,6 +270,7 @@
<File Name="../../src/EditLexers/stlVHDL.c"/>
<File Name="../../src/EditLexers/stlVim.c"/>
<File Name="../../src/EditLexers/stlWASM.c"/>
<File Name="../../src/EditLexers/stlWinHex.c"/>
<File Name="../../src/EditLexers/stlXML.c"/>
<File Name="../../src/EditLexers/stlYAML.c"/>
<File Name="../../src/EditLexers/stlZig.c"/>
Expand Down
2 changes: 2 additions & 0 deletions build/VS2017/Notepad2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@
<ClCompile Include="..\..\scintilla\lexers\LexVHDL.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexVim.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexWASM.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexWinHex.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexYAML.cxx" />
<ClCompile Include="..\..\scintilla\lexers\LexZig.cxx" />
<ClCompile Include="..\..\scintilla\lexlib\Accessor.cxx" />
Expand Down Expand Up @@ -672,6 +673,7 @@
<ClCompile Include="..\..\src\EditLexers\stlVHDL.c" />
<ClCompile Include="..\..\src\EditLexers\stlVim.c" />
<ClCompile Include="..\..\src\EditLexers\stlWASM.c" />
<ClCompile Include="..\..\src\EditLexers\stlWinHex.c" />
<ClCompile Include="..\..\src\EditLexers\stlXML.c" />
<ClCompile Include="..\..\src\EditLexers\stlYAML.c" />
<ClCompile Include="..\..\src\EditLexers\stlZig.c" />
Expand Down
6 changes: 6 additions & 0 deletions build/VS2017/Notepad2.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@
<ClCompile Include="..\..\scintilla\lexers\LexWASM.cxx">
<Filter>Scintilla\lexers</Filter>
</ClCompile>
<ClCompile Include="..\..\scintilla\lexers\LexWinHex.cxx">
<Filter>Scintilla\lexers</Filter>
</ClCompile>
<ClCompile Include="..\..\scintilla\lexers\LexYAML.cxx">
<Filter>Scintilla\lexers</Filter>
</ClCompile>
Expand Down Expand Up @@ -624,6 +627,9 @@
<ClCompile Include="..\..\src\EditLexers\stlWASM.c">
<Filter>Source Files\EditLexers</Filter>
</ClCompile>
<ClCompile Include="..\..\src\EditLexers\stlWinHex.c">
<Filter>Source Files\EditLexers</Filter>
</ClCompile>
<ClCompile Include="..\..\src\EditLexers\stlXML.c">
<Filter>Source Files\EditLexers</Filter>
</ClCompile>
Expand Down
4 changes: 4 additions & 0 deletions doc/FileExt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ WebAssembly
wat
wast

WinHex Script
whs
tpl Template Definition

YAML Document
yml
yaml
Expand Down
3 changes: 3 additions & 0 deletions doc/Notepad2 DarkTheme.ini
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ Operator=fore:#A349A4
[WebAssembly]
Number=fore:#F84C4C
Operator=fore:#A349A4
[WinHex Script]
Number=fore:#F84C4C
Operator=fore:#A349A4
[YAML Document]
Keyword=fore:#00B050
Document=bold; back:#404030; eolfilled
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Latest development builds (artifacts in Release configuration for each compiler
* Verilog HDL, up to [Verilog](tools/lang/Verilog.v) 2005 and [SystemVerilog](tools/lang/SystemVerilog.sv) 2017.
* [Vim Script](tools/lang/Vim.vim)
* [WebAssembly](https://github.com/WebAssembly/wabt/blob/main/src/lexer-keywords.txt), up to wabt 1.0.
* [WinHex Script](tools/lang/WinHex.whs) and template, up to WinHex 20.9.
* XML Document, [Screenshots](https://github.com/zufuliu/notepad2/wiki/Screenshots#xml)
* XML Schema
* XML Stylesheet
Expand Down
11 changes: 11 additions & 0 deletions scintilla/include/SciLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
#define SCLEX_CSV 223
#define SCLEX_ZIG 224
#define SCLEX_MATHEMATICA 225
#define SCLEX_WINHEX 226
#define SCLEX_AUTOMATIC 1000
#define SCE_PY_DEFAULT 0
#define SCE_PY_COMMENTLINE 1
Expand Down Expand Up @@ -1809,4 +1810,14 @@
#define SCE_MATHEMATICA_SLOT 7
#define SCE_MATHEMATICA_IDENTIFIER 8
#define SCE_MATHEMATICA_KEYWORD 9
#define SCE_WINHEX_DEFAULT 0
#define SCE_WINHEX_COMMENTLINE 1
#define SCE_WINHEX_NUMBER 2
#define SCE_WINHEX_OPERATOR 3
#define SCE_WINHEX_STRING 4
#define SCE_WINHEX_ESCAPECHAR 5
#define SCE_WINHEX_IDENTIFIER 6
#define SCE_WINHEX_KEYWORD 7
#define SCE_WINHEX_TYPE 8
#define SCE_WINHEX_COMMAND 9
/* --Autogenerated -- end of section automatically generated from SciLexer.iface */
13 changes: 13 additions & 0 deletions scintilla/include/SciLexer.iface
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ val SCLEX_AWK=222
val SCLEX_CSV=223
val SCLEX_ZIG=224
val SCLEX_MATHEMATICA=225
val SCLEX_WINHEX=226

# When a lexer specifies its language as SCLEX_AUTOMATIC it receives a
# value assigned in sequence from SCLEX_AUTOMATIC+1.
Expand Down Expand Up @@ -3159,3 +3160,15 @@ val SCE_MATHEMATICA_ESCAPECHAR=
val SCE_MATHEMATICA_SLOT=
val SCE_MATHEMATICA_IDENTIFIER=
val SCE_MATHEMATICA_KEYWORD=
# Lexical states for SCLEX_WINHEX
lex WinHex=SCLEX_WINHEX SCE_WINHEX_
val SCE_WINHEX_DEFAULT=
val SCE_WINHEX_COMMENTLINE=
val SCE_WINHEX_NUMBER=
val SCE_WINHEX_OPERATOR=
val SCE_WINHEX_STRING=
val SCE_WINHEX_ESCAPECHAR=
val SCE_WINHEX_IDENTIFIER=
val SCE_WINHEX_KEYWORD=
val SCE_WINHEX_TYPE=
val SCE_WINHEX_COMMAND=
138 changes: 138 additions & 0 deletions scintilla/lexers/LexWinHex.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// This file is part of Notepad2.
// See License.txt for details about distribution and modification.
//! Lexer for WinHex script and template.

#include <cassert>
#include <cstring>

#include <string>
#include <string_view>

#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"

#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "StringUtils.h"
#include "LexerModule.h"

using namespace Lexilla;

namespace {

//KeywordIndex++Autogenerated -- start of section automatically generated
enum {
KeywordIndex_Keyword = 0,
KeywordIndex_Type = 1,
KeywordIndex_Command = 2,
};
//KeywordIndex--Autogenerated -- end of section automatically generated

void ColouriseWinHexDoc(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, LexerWordList keywordLists, Accessor &styler) {
const bool fold = styler.GetPropertyBool("fold");
StyleContext sc(startPos, lengthDoc, initStyle, styler);
int levelCurrent = SC_FOLDLEVELBASE;
if (sc.currentLine > 0) {
levelCurrent = styler.LevelAt(sc.currentLine - 1) >> 16;
}
int levelNext = levelCurrent;
int visibleChars = 0;

while (sc.More()) {
switch (sc.state) {
case SCE_WINHEX_OPERATOR:
sc.SetState(SCE_WINHEX_DEFAULT);
break;

case SCE_WINHEX_NUMBER:
if (!IsAlphaNumeric(sc.ch)) {
sc.SetState(SCE_WINHEX_DEFAULT);
}
break;

case SCE_WINHEX_IDENTIFIER:
if (!IsIdentifierChar(sc.ch) && sc.ch != '-') {
char s[64];
sc.GetCurrentLowered(s, sizeof(s));
if (keywordLists[KeywordIndex_Keyword].InList(s)) {
sc.ChangeState(SCE_WINHEX_KEYWORD);
} else if (keywordLists[KeywordIndex_Type].InList(s)) {
sc.ChangeState(SCE_WINHEX_TYPE);
} else if (keywordLists[KeywordIndex_Command].InList(s)) {
sc.ChangeState(SCE_WINHEX_COMMAND);
}
if (sc.state != SCE_WINHEX_IDENTIFIER && visibleChars == sc.LengthCurrent()) {
if (StrStartsWith(s, "if") || StrEqualsAny(s, "begin", "section")) {
levelNext++;
} else if (StrStartsWith(s, "end")) {
levelNext--;
}
}
sc.SetState(SCE_WINHEX_DEFAULT);
}
break;

case SCE_WINHEX_STRING:
if (sc.atLineStart) {
sc.SetState(SCE_WINHEX_DEFAULT);
} else if (sc.ch == '\"') {
sc.ForwardSetState(SCE_WINHEX_DEFAULT);
}
break;

case SCE_WINHEX_COMMENTLINE:
if (sc.atLineStart) {
sc.SetState(SCE_WINHEX_DEFAULT);
}
break;
}

if (sc.state == SCE_WINHEX_DEFAULT) {
if (sc.Match('/', '/')) {
sc.SetState(SCE_WINHEX_COMMENTLINE);
} else if (sc.ch == '\"') {
sc.SetState(SCE_WINHEX_STRING);
} else if (IsADigit(sc.ch)) {
sc.SetState(SCE_WINHEX_NUMBER);
} else if (IsIdentifierStart(sc.ch)) {
sc.SetState(SCE_WINHEX_IDENTIFIER);
} else if (IsAGraphic(sc.ch)) {
sc.SetState(SCE_WINHEX_OPERATOR);
if (sc.ch == '{') {
++levelNext;
} else if (sc.ch == '}') {
--levelNext;
}
}
}

if (!isspacechar(sc.ch)) {
visibleChars++;
}
if (sc.atLineEnd) {
visibleChars = 0;
if (fold) {
const int levelUse = levelCurrent;
int lev = levelUse | levelNext << 16;
if (levelUse < levelNext) {
lev |= SC_FOLDLEVELHEADERFLAG;
}
if (lev != styler.LevelAt(sc.currentLine)) {
styler.SetLevel(sc.currentLine, lev);
}
levelCurrent = levelNext;
}
}
sc.Forward();
}

sc.Complete();
}

}

LexerModule lmWinHex(SCLEX_WINHEX, ColouriseWinHexDoc, "winhex");
2 changes: 2 additions & 0 deletions scintilla/lexlib/LexerModule.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ extern LexerModule lmVHDL;
extern LexerModule lmVim;
extern LexerModule lmVisualBasic;
extern LexerModule lmWASM;
extern LexerModule lmWinHex;
extern LexerModule lmXML;
extern LexerModule lmYAML;
extern LexerModule lmZig;
Expand Down Expand Up @@ -181,6 +182,7 @@ const LexerModule * const lexerCatalogue[] = {
&lmVim,
&lmVisualBasic,
&lmWASM,
&lmWinHex,
&lmXML,
&lmYAML,
&lmZig,
Expand Down
2 changes: 2 additions & 0 deletions src/EditAutoC.c
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,7 @@ void EditToggleCommentLine(void) {
case NP2LEX_SWIFT:
case NP2LEX_TYPESCRIPT:
case NP2LEX_VERILOG:
case NP2LEX_WINHEX:
case NP2LEX_ZIG:
EditToggleLineComments(L"//", false);
break;
Expand Down Expand Up @@ -2851,6 +2852,7 @@ void InitAutoCompletionCache(LPCEDITLEXER pLex) {
case NP2LEX_LISP:
case NP2LEX_SMALI:
case NP2LEX_TEXTFILE:
case NP2LEX_WINHEX:
CurrentWordCharSet['-' >> 5] |= (1 << ('-' & 31));
break;

Expand Down
1 change: 1 addition & 0 deletions src/EditLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ typedef const EDITLEXER *LPCEDITLEXER;
#define NP2LEX_OCAML 63083 // SCLEX_OCAML OCaml Source
#define NP2LEX_HASKELL 63084 // SCLEX_HASKELL Haskell Source

#define NP2LEX_WINHEX 63086 // SCLEX_WINHEX WinHex Script
#define NP2LEX_AVISYNTH 63087 // SCLEX_AVISYNTH AviSynth Script
#define NP2LEX_TEKEXTHEX 63088 // SCLEX_TEKEXTHEX Tektronix extended HEX
#define NP2LEX_INTELHEX 63089 // SCLEX_INTELHEX Intel HEX
Expand Down
66 changes: 66 additions & 0 deletions src/EditLexers/stlWinHex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "EditLexer.h"
#include "EditStyleX.h"

static KEYWORDLIST Keywords_WinHex = {{
//++Autogenerated -- start of section automatically generated
"applies_to begin big-endian decimal description end endsection fixed_start hexadecimal little-endian multiple numbering "
"octal read-only read-write requires section sector-aligned template "

, // 1 type
"AppleDateTime binary boole16 boole32 boole8 boolean byte char char16 DOSDateTime double extended FileTime float GUID "
"hex int16 int24 int32 int64 int8 JavaDateTime longdouble OLEDateTime real single SQLDateTime string string16 time_t "
"uint16 uint24 uint32 uint48 uint8 uint_flex UNIXDateTime zstring zstring16 "

, // 2 commands
"AESDecrypt AESEncrypt Assign Block Block1 Block2 "
"CalcHash CalcHashEx Close CloseAll Convert Copy CopyFile CopyIntoNewFile Create CreateBackup CreateBackupEx "
"CurrentPos Cut "
"Debug Dec DeleteFile Else EndIf ExecuteScript Exit ExitIfNoFilesOpen ExitLoop Find ForAllObjDo "
"GetClusterAlloc GetClusterAllocEx GetClusterSize GetSize GetUserInput GetUserInputI Goto "
"IfEqual IfFound IfGreater Inc InitFreeSpace InitMFTRecords InitSlackSpace Insert InterpretImageAsDisk IntToStr JumpTo "
"Label MessageBox Move MoveFile NextObj Open Paste Read ReadLn Release Remove ReplaceAll "
"Save SaveAll SaveAs SetVarSize StrCat StrToInt Terminate Turbo unlimited UseLogFile Write Write2 WriteClipboard "

, // 3 misc
"ANSI Base64 Binary BlockOnly Down HexASCII IBM IntelHex LowerCase MatchCase MatchWord MotorolaS Off On SaveAllPos "
"UUCode Unicode Up UpperCase Wildcards false hiberfil true "

, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
//--Autogenerated -- end of section automatically generated
}};

static EDITSTYLE Styles_WinHex[] = {
EDITSTYLE_DEFAULT,
{ SCE_WINHEX_KEYWORD, NP2StyleX_Keyword, L"bold; fore:#FF8000" },
{ SCE_WINHEX_TYPE, NP2StyleX_Type, L"fore:#0080FF" },
{ SCE_WINHEX_COMMAND, NP2StyleX_Command, L"fore:#A46000" },
{ SCE_WINHEX_COMMENTLINE, NP2StyleX_Comment, L"fore:#608060" },
//{ SCE_WINHEX_ESCAPECHAR, NP2StyleX_EscapeSequence, L"fore:#0080C0" },
{ SCE_WINHEX_STRING, NP2StyleX_String, L"fore:#008000" },
{ SCE_WINHEX_NUMBER, NP2StyleX_Number, L"fore:#FF0000" },
{ SCE_WINHEX_OPERATOR, NP2StyleX_Operator, L"fore:#B000B0" },
};

EDITLEXER lexWinHex = {
SCLEX_WINHEX, NP2LEX_WINHEX,
//Settings++Autogenerated -- start of section automatically generated
LexerAttr_NoBlockComment,
TAB_WIDTH_4, INDENT_WIDTH_4,
(1 << 0) | (1 << 1), // level1, level2
0,
'\\', 0, 0,
0,
0, 0,
SCE_WINHEX_OPERATOR, 0
, KeywordAttr32(0, KeywordAttr_PreSorted) // keywords
| KeywordAttr32(1, KeywordAttr_MakeLower | KeywordAttr_PreSorted) // type
| KeywordAttr32(2, KeywordAttr_MakeLower | KeywordAttr_PreSorted) // commands
| KeywordAttr32(3, KeywordAttr_NoLexer) // misc
, SCE_WINHEX_COMMENTLINE,
SCE_WINHEX_STRING, SCE_WINHEX_ESCAPECHAR,
//Settings--Autogenerated -- end of section automatically generated
EDITLEXER_HOLE(L"WinHex Script", Styles_WinHex),
L"whs",
&Keywords_WinHex,
Styles_WinHex
};
2 changes: 2 additions & 0 deletions src/Styles.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ extern EDITLEXER lexVim;
extern EDITLEXER lexVisualBasic;

extern EDITLEXER lexWASM;
extern EDITLEXER lexWinHex;

extern EDITLEXER lexYAML;

Expand Down Expand Up @@ -253,6 +254,7 @@ static PEDITLEXER pLexArray[] = {
&lexVisualBasic,

&lexWASM,
&lexWinHex,

&lexYAML,

Expand Down
Loading

0 comments on commit 76579cb

Please sign in to comment.