-
Notifications
You must be signed in to change notification settings - Fork 169
/
Styles.c
3751 lines (3339 loc) · 109 KB
/
Styles.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
*
*
* Notepad2
*
* Styles.c
* Scintilla Style Management
*
* See Readme.txt for more information about this source code.
* Please send me your comments to this work.
*
* See License.txt for details about distribution and modification.
*
* (c) Florian Balmer 1996-2011
* florian.balmer@gmail.com
* http://www.flos-freeware.ch
*
*
******************************************************************************/
#include <windows.h>
#include <windowsx.h>
#include <shlwapi.h>
#include <shlobj.h>
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <math.h>
#include "Notepad2.h"
#include "Edit.h"
#include "Styles.h"
#include "Helpers.h"
#include "Dialogs.h"
#include "SciCall.h"
#include "resource.h"
extern EDITLEXER lexDefault;
extern EDITLEXER lexCPP;
extern EDITLEXER lexCSharp;
extern EDITLEXER lexCSS;
extern EDITLEXER lexJava;
extern EDITLEXER lexJS;
extern EDITLEXER lexJSON;
extern EDITLEXER lexPHP;
extern EDITLEXER lexPython;
extern EDITLEXER lexRuby;
extern EDITLEXER lexSQL;
extern EDITLEXER lexHTML;
extern EDITLEXER lexXML;
extern EDITLEXER lexAS;
extern EDITLEXER lexSmali;
extern EDITLEXER lexASM;
extern EDITLEXER lexASY;
extern EDITLEXER lexAU3;
extern EDITLEXER lexAwk;
extern EDITLEXER lexBatch;
extern EDITLEXER lexCMake;
extern EDITLEXER lexCONF;
extern EDITLEXER lexD;
extern EDITLEXER lexDIFF;
extern EDITLEXER lexFSharp;
extern EDITLEXER lexFortran;
extern EDITLEXER lexGradle;
extern EDITLEXER lexDOT;
extern EDITLEXER lexGo;
extern EDITLEXER lexGroovy;
extern EDITLEXER lexHaXe;
//extern EDITLEXER lexIDL;
extern EDITLEXER lexINI;
extern EDITLEXER lexINNO;
extern EDITLEXER lexJAM;
extern EDITLEXER lexJulia;
extern EDITLEXER lexLaTeX;
extern EDITLEXER lexLisp;
extern EDITLEXER lexLLVM;
extern EDITLEXER lexLua;
extern EDITLEXER lexMake;
extern EDITLEXER lexMatlab;
extern EDITLEXER lexCIL;
extern EDITLEXER lexNsis;
extern EDITLEXER lexPascal;
extern EDITLEXER lexPerl;
extern EDITLEXER lexPS1;
extern EDITLEXER lexRC;
extern EDITLEXER lexScala;
extern EDITLEXER lexBash;
extern EDITLEXER lexTcl;
extern EDITLEXER lexTexinfo;
extern EDITLEXER lexVBS;
extern EDITLEXER lexVerilog;
extern EDITLEXER lexVHDL;
extern EDITLEXER lexVim;
extern EDITLEXER lexVB;
extern EDITLEXER lexANSI;
// This array holds all the lexers...
static const PEDITLEXER pLexArray[NUMLEXERS] = {
&lexDefault,
&lexCPP,
&lexCSharp,
&lexCSS,
&lexJava,
&lexJS,
&lexJSON,
&lexPHP,
&lexPython,
&lexRuby,
&lexSQL,
&lexHTML,
&lexXML,
&lexAS,
&lexSmali,
&lexASM,
&lexASY,
&lexAU3,
&lexAwk,
&lexBatch,
&lexCMake,
&lexCONF,
&lexD,
&lexDIFF,
&lexFSharp,
&lexFortran,
&lexGradle,
&lexDOT,
&lexGo,
&lexGroovy,
&lexHaXe,
//&lexIDL,
&lexINI,
&lexINNO,
&lexJAM,
&lexJulia,
&lexLaTeX,
&lexLisp,
&lexLLVM,
&lexLua,
&lexMake,
&lexMatlab,
&lexCIL,
&lexNsis,
&lexPascal,
&lexPerl,
&lexPS1,
&lexRC,
&lexScala,
&lexBash,
&lexTcl,
&lexTexinfo,
&lexVBS,
&lexVerilog,
&lexVHDL,
&lexVim,
&lexVB,
&lexANSI
};
#define NUM_MONO_FONT 6
static LPCWSTR const SysMonoFontName[NUM_MONO_FONT + 1] = {
L"DejaVu Sans Mono",
//L"Bitstream Vera Sans Mono",
L"Consolas",
L"Source Code Pro",
L"Monaco",
L"Inconsolata",
L"Lucida Console",
L"Courier New",
};
static int np2MonoFontIndex = NUM_MONO_FONT; // Courier New
// Currently used lexer
PEDITLEXER pLexCurrent = &lexDefault;
int np2LexLangIndex = 0;
UINT8 currentLexKeywordAttr[NUMKEYWORD] = {0};
#define STYLESMODIFIED_NONE 0
#define STYLESMODIFIED_SOME_STYLE 1
#define STYLESMODIFIED_ALL_STYLE 2
#define STYLESMODIFIED_STYLE_MASK 3
#define STYLESMODIFIED_FILE_EXT 4
#define STYLESMODIFIED_WARN_MASK 7
#define STYLESMODIFIED_COLOR 8
#define MAX_CUSTOM_COLOR_COUNT 16
// run tools/CountColor.py on exported scheme file
static const COLORREF defaultCustomColor[MAX_CUSTOM_COLOR_COUNT] = {
RGB(0xB0, 0x00, 0xB0), // Constant, Macro, Operator
RGB(0xFF, 0x80, 0x00), // Indentation, Preprocessor, Keyword, Attribute, Enumeration, Annotation, Register
RGB(0x00, 0x80, 0x00), // String, Comment
//RGB(0x00, 0x00, 0xFF), // Keyword, Type Keyword
//RGB(0xFF, 0x00, 0x00), // Number
RGB(0x60, 0x80, 0x60), // Comment
RGB(0xA4, 0x60, 0x00), // Function, Here-doc, Now-doc
RGB(0x00, 0x80, 0xFF), // Class, Instruction, Struct
RGB(0xFF, 0xF1, 0xAB), // Regex, Backticks
RGB(0xFF, 0xC0, 0x40), // Label, Section
RGB(0x1E, 0x90, 0xFF), // Interface, Type Keyword
RGB(0x64, 0x80, 0x00), // Field, Here-doc
RGB(0x00, 0x66, 0x33), // Regex
RGB(0x40, 0x80, 0x80), // Doc Comment Tag
RGB(0x00, 0x80, 0x80), // String, Verbatim String
RGB(0xFF, 0x00, 0x80), // Backticks, Basic Function
RGB(0x40, 0x80, 0x40), // Doc Comment
RGB(0x00, 0x80, 0xC0), // Build-in Function
//RGB(0x00, 0x3C, 0xE6), // Variable
//RGB(0x00, 0x7F, 0x7F), // Class, Trait
};
static COLORREF customColor[MAX_CUSTOM_COLOR_COUNT];
static BOOL bCustomColorLoaded = FALSE;
static int iLexerLoadedCount = 0;
BOOL bUse2ndDefaultStyle;
BOOL bCurrentLexerHasLineComment;
BOOL bCurrentLexerHasBlockComment;
static UINT fStylesModified = STYLESMODIFIED_NONE;
static BOOL fWarnedNoIniFile = FALSE;
static int iBaseFontSize = 11*SC_FONT_SIZE_MULTIPLIER; // 11 pt in lexDefault
int iFontQuality = SC_EFF_QUALITY_LCD_OPTIMIZED;
int iCaretStyle = 1; // width 1, 0 for block
int iOvrCaretStyle = 0; // 0 for bar, 1 for block
int iCaretBlinkPeriod = -1; // system default, 0 for noblink
static int iDefaultLexer;
static BOOL bAutoSelect;
static int cxStyleSelectDlg;
static int cyStyleSelectDlg;
static int cyStyleCustomizeDlg;
static int cxStyleCustomizeDlg;
#define ALL_FILE_EXTENSIONS_BYTE_SIZE ((NUMLEXERS * MAX_EDITLEXER_EXT_SIZE) * sizeof(WCHAR))
static LPWSTR g_AllFileExtensions = NULL;
// Notepad2.c
extern int iEncoding;
extern int g_DOSEncoding;
extern int iDefaultCodePage;
extern int iDefaultCharSet;
extern INT iHighlightCurrentLine;
#define STYLE_MASK_FONT_FACE (1 << 0)
#define STYLE_MASK_FONT_SIZE (1 << 1)
#define STYLE_MASK_FORE_COLOR (1 << 2)
#define STYLE_MASK_BACK_COLOR (1 << 3)
#define STYLE_MASK_UPPER_LOWER (1 << 4)
#define STYLE_MASK_CHARSET (1 << 5)
struct DetailStyle {
UINT mask;
int fontSize;
int foreColor;
int backColor;
BOOL bold;
BOOL italic;
BOOL underline;
BOOL strike;
BOOL eolFilled;
int upperLower;
int charset;
WCHAR fontWide[LF_FACESIZE];
char fontFace[LF_FACESIZE * kMaxMultiByteCount];
};
//! keep same order as lexDefault
enum DefaultStyleIndex {
Style_Default,
Style_LineNumber,
Style_MatchBrace,
Style_MatchBraceError,
Style_ControlCharacter,
Style_IndentationGuide,
Style_Selection,
Style_Whitespace,
Style_CurrentLine,
Style_Caret,
Style_LongLineMarker,
Style_ExtraLineSpacing,
Style_FoldingMarker,
Style_MaxDefaultStyle,
};
static inline int GetDefaultStyleStartIndex(void) {
return bUse2ndDefaultStyle ? Style_MaxDefaultStyle : Style_Default;
}
static inline int FindDefaultFontIndex(void) {
for (int i = 0; i < NUM_MONO_FONT; i++) {
if (IsFontAvailable(SysMonoFontName[i])) {
return i;
}
}
return NUM_MONO_FONT;
}
void Style_ReleaseResources(void) {
NP2HeapFree(g_AllFileExtensions);
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
PEDITLEXER pLex = pLexArray[iLexer];
if (pLex->szStyleBuf) {
NP2HeapFree(pLex->szStyleBuf);
}
}
}
static void Style_LoadOneEx(PEDITLEXER pLex, IniSection *pIniSection, WCHAR *pIniSectionBuf, int cchIniSection) {
LoadIniSection(pLex->pszName, pIniSectionBuf, cchIniSection);
const UINT iStyleCount = pLex->iStyleCount;
LPWSTR szStyleBuf = (LPWSTR)NP2HeapAlloc(EDITSTYLE_BufferSize(iStyleCount));
pLex->szStyleBuf = szStyleBuf;
if (!IniSectionParse(pIniSection, pIniSectionBuf)) {
for (UINT i = 0; i < iStyleCount; i++) {
LPWSTR szValue = szStyleBuf + (i * MAX_EDITSTYLE_VALUE_SIZE);
pLex->Styles[i].szValue = szValue;
lstrcpy(szValue, pLex->Styles[i].pszDefault);
}
} else {
for (UINT i = 0; i < iStyleCount; i++) {
LPWSTR szValue = szStyleBuf + (i * MAX_EDITSTYLE_VALUE_SIZE);
pLex->Styles[i].szValue = szValue;
LPCWSTR value = IniSectionGetValueImpl(pIniSection, pLex->Styles[i].pszName, pLex->Styles[i].iNameLen);
if (value != NULL) {
lstrcpyn(szValue, value, MAX_EDITSTYLE_VALUE_SIZE);
} else {
lstrcpy(szValue, pLex->Styles[i].pszDefault);
}
}
}
++iLexerLoadedCount;
}
//=============================================================================
//
// Style_Load()
//
void Style_Load(void) {
IniSection section;
g_AllFileExtensions = (LPWSTR)NP2HeapAlloc(ALL_FILE_EXTENSIONS_BYTE_SIZE);
WCHAR *pIniSectionBuf = (WCHAR *)NP2HeapAlloc(sizeof(WCHAR) * MAX_INI_SECTION_SIZE_STYLES);
const int cchIniSection = (int)(NP2HeapSize(pIniSectionBuf) / sizeof(WCHAR));
IniSection *pIniSection = §ion;
IniSectionInit(pIniSection, 128);
LoadIniSection(INI_SECTION_NAME_STYLES, pIniSectionBuf, cchIniSection);
IniSectionParse(pIniSection, pIniSectionBuf);
// 2nd default
bUse2ndDefaultStyle = IniSectionGetBool(pIniSection, L"Use2ndDefaultStyle", 0);
// default scheme
const int iValue = IniSectionGetInt(pIniSection, L"DefaultScheme", 0);
iDefaultLexer = clamp_i(iValue, 0, NUMLEXERS - 1);
// auto select
bAutoSelect = IniSectionGetBool(pIniSection, L"AutoSelect", 1);
// scheme select dlg dimensions
cxStyleSelectDlg = IniSectionGetInt(pIniSection, L"SelectDlgSizeX", 0);
cyStyleSelectDlg = IniSectionGetInt(pIniSection, L"SelectDlgSizeY", 0);
cyStyleCustomizeDlg = IniSectionGetInt(pIniSection, L"CustomizeDlgSizeY", 0);
cxStyleCustomizeDlg = IniSectionGetInt(pIniSection, L"CustomizeDlgSizeX", 0);
LoadIniSection(INI_SECTION_NAME_FILE_EXTENSIONS, pIniSectionBuf, cchIniSection);
IniSectionParse(pIniSection, pIniSectionBuf);
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
PEDITLEXER pLex = pLexArray[iLexer];
pLex->szExtensions = g_AllFileExtensions + (iLexer * MAX_EDITLEXER_EXT_SIZE);
LPCWSTR value = IniSectionGetValueImpl(pIniSection, pLex->pszName, pLex->iNameLen);
if (StrIsEmpty(value)) {
lstrcpy(pLex->szExtensions, pLex->pszDefExt);
} else {
lstrcpyn(pLex->szExtensions, value, MAX_EDITLEXER_EXT_SIZE);
}
}
Style_LoadOneEx(&lexDefault, pIniSection, pIniSectionBuf, cchIniSection);
if (iDefaultLexer != 0) {
Style_LoadOneEx(pLexArray[iDefaultLexer], pIniSection, pIniSectionBuf, cchIniSection);
}
np2MonoFontIndex = FindDefaultFontIndex();
IniSectionFree(pIniSection);
NP2HeapFree(pIniSectionBuf);
}
static void Style_LoadOne(PEDITLEXER pLex) {
IniSection section;
WCHAR *pIniSectionBuf = (WCHAR *)NP2HeapAlloc(sizeof(WCHAR) * MAX_INI_SECTION_SIZE_STYLES);
const int cchIniSection = (int)(NP2HeapSize(pIniSectionBuf) / sizeof(WCHAR));
IniSection *pIniSection = §ion;
IniSectionInit(pIniSection, 128);
Style_LoadOneEx(pLex, pIniSection, pIniSectionBuf, cchIniSection);
IniSectionFree(pIniSection);
NP2HeapFree(pIniSectionBuf);
}
static void Style_LoadAll(void) {
IniSection section;
WCHAR *pIniSectionBuf = (WCHAR *)NP2HeapAlloc(sizeof(WCHAR) * MAX_INI_SECTION_SIZE_STYLES);
const int cchIniSection = (int)(NP2HeapSize(pIniSectionBuf) / sizeof(WCHAR));
IniSection *pIniSection = §ion;
IniSectionInit(pIniSection, 128);
// Custom colors
if (!bCustomColorLoaded) {
bCustomColorLoaded = TRUE;
CopyMemory(customColor, defaultCustomColor, MAX_CUSTOM_COLOR_COUNT * sizeof(COLORREF));
LoadIniSection(INI_SECTION_NAME_CUSTOM_COLORS, pIniSectionBuf, cchIniSection);
IniSectionParseArray(pIniSection, pIniSectionBuf);
const int count = min_i(pIniSection->count, MAX_CUSTOM_COLOR_COUNT);
for (int i = 0; i < count; i++) {
const IniKeyValueNode *node = &pIniSection->nodeList[i];
const UINT n = (UINT)(wcstol(node->key, NULL, 10) - 1);
LPCWSTR wch = node->value;
if (n < MAX_CUSTOM_COLOR_COUNT && *wch == L'#') {
int irgb;
if (HexStrToInt(wch + 1, &irgb)) {
customColor[n] = RGB((irgb & 0xFF0000) >> 16, (irgb & 0xFF00) >> 8, irgb & 0xFF);
}
}
}
}
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
PEDITLEXER pLex = pLexArray[iLexer];
if (!pLex->szStyleBuf) {
Style_LoadOneEx(pLex, pIniSection, pIniSectionBuf, cchIniSection);
}
}
iLexerLoadedCount = NUMLEXERS;
IniSectionFree(pIniSection);
NP2HeapFree(pIniSectionBuf);
}
//=============================================================================
//
// Style_Save()
//
void Style_Save(void) {
IniSectionOnSave section;
WCHAR *pIniSectionBuf = (WCHAR *)NP2HeapAlloc(sizeof(WCHAR) * MAX_INI_SECTION_SIZE_STYLES);
const int cchIniSection = (int)(NP2HeapSize(pIniSectionBuf) / sizeof(WCHAR));
IniSectionOnSave *pIniSection = §ion;
pIniSection->next = pIniSectionBuf;
// Custom colors
if (fStylesModified & STYLESMODIFIED_COLOR) {
for (unsigned int i = 0; i < MAX_CUSTOM_COLOR_COUNT; i++) {
const COLORREF color = customColor[i];
if (color != defaultCustomColor[i]) {
WCHAR tch[4];
WCHAR wch[16];
wsprintf(tch, L"%02u", i + 1);
wsprintf(wch, L"#%02X%02X%02X", (int)GetRValue(color), (int)GetGValue(color), (int)GetBValue(color));
IniSectionSetString(pIniSection, tch, wch);
}
}
SaveIniSection(INI_SECTION_NAME_CUSTOM_COLORS, pIniSectionBuf);
ZeroMemory(pIniSectionBuf, cchIniSection);
pIniSection->next = pIniSectionBuf;
}
// auto select
IniSectionSetBoolEx(pIniSection, L"Use2ndDefaultStyle", bUse2ndDefaultStyle, 0);
// default scheme
IniSectionSetIntEx(pIniSection, L"DefaultScheme", iDefaultLexer, 0);
// auto select
IniSectionSetBoolEx(pIniSection, L"AutoSelect", bAutoSelect, 1);
// scheme select dlg dimensions
IniSectionSetInt(pIniSection, L"SelectDlgSizeX", cxStyleSelectDlg);
IniSectionSetInt(pIniSection, L"SelectDlgSizeY", cyStyleSelectDlg);
IniSectionSetInt(pIniSection, L"CustomizeDlgSizeY", cyStyleCustomizeDlg);
IniSectionSetInt(pIniSection, L"CustomizeDlgSizeX", cxStyleCustomizeDlg);
SaveIniSection(INI_SECTION_NAME_STYLES, pIniSectionBuf);
if (fStylesModified & STYLESMODIFIED_FILE_EXT) {
ZeroMemory(pIniSectionBuf, cchIniSection);
pIniSection->next = pIniSectionBuf;
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
const LPCEDITLEXER pLex = pLexArray[iLexer];
IniSectionSetStringEx(pIniSection, pLex->pszName, pLex->szExtensions, pLex->pszDefExt);
}
SaveIniSection(INI_SECTION_NAME_FILE_EXTENSIONS, pIniSectionBuf);
}
if (fStylesModified & STYLESMODIFIED_STYLE_MASK) {
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
PEDITLEXER pLex = pLexArray[iLexer];
if (!pLex->bStyleChanged) {
continue;
}
ZeroMemory(pIniSectionBuf, cchIniSection);
pIniSection->next = pIniSectionBuf;
const UINT iStyleCount = pLex->iStyleCount;
for (UINT i = 0; i < iStyleCount; i++) {
IniSectionSetStringEx(pIniSection, pLex->Styles[i].pszName, pLex->Styles[i].szValue, pLex->Styles[i].pszDefault);
}
// delete this section if nothing changed
SaveIniSection(pLex->pszName, StrIsEmpty(pIniSectionBuf) ? NULL : pIniSectionBuf);
pLex->bStyleChanged = FALSE;
}
}
fStylesModified = STYLESMODIFIED_NONE;
NP2HeapFree(pIniSectionBuf);
}
//=============================================================================
//
// Style_Import()
//
BOOL Style_Import(HWND hwnd) {
WCHAR szFile[MAX_PATH * 2] = L"";
WCHAR szFilter[256];
GetString(IDS_FILTER_INI, szFilter, COUNTOF(szFilter));
PrepareFilterStr(szFilter);
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = szFilter;
ofn.lpstrFile = szFile;
ofn.lpstrDefExt = L"ini";
ofn.nMaxFile = COUNTOF(szFile);
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_DONTADDTORECENT
| OFN_PATHMUSTEXIST | OFN_SHAREAWARE /*| OFN_NODEREFERENCELINKS*/;
if (GetOpenFileName(&ofn)) {
IniSection section;
WCHAR *pIniSectionBuf = (WCHAR *)NP2HeapAlloc(sizeof(WCHAR) * MAX_INI_SECTION_SIZE_STYLES);
const int cchIniSection = (int)(NP2HeapSize(pIniSectionBuf) / sizeof(WCHAR));
IniSection *pIniSection = §ion;
IniSectionInit(pIniSection, 128);
if (GetPrivateProfileSection(INI_SECTION_NAME_FILE_EXTENSIONS, pIniSectionBuf, cchIniSection, szFile)) {
if (IniSectionParse(pIniSection, pIniSectionBuf)) {
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
PEDITLEXER pLex = pLexArray[iLexer];
LPCWSTR value = IniSectionGetValueImpl(pIniSection, pLex->pszName, pLex->iNameLen);
if (StrNotEmpty(value)) {
lstrcpyn(pLex->szExtensions, value, MAX_EDITLEXER_EXT_SIZE);
}
if (pIniSection->count == 0) {
break;
}
}
}
}
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
PEDITLEXER pLex = pLexArray[iLexer];
if (GetPrivateProfileSection(pLex->pszName, pIniSectionBuf, cchIniSection, szFile)) {
if (!IniSectionParse(pIniSection, pIniSectionBuf)) {
continue;
}
const UINT iStyleCount = pLex->iStyleCount;
for (UINT i = 0; i < iStyleCount; i++) {
LPCWSTR value = IniSectionGetValueImpl(pIniSection, pLex->Styles[i].pszName, pLex->Styles[i].iNameLen);
if (value != NULL) {
lstrcpyn(pLex->Styles[i].szValue, value, MAX_EDITSTYLE_VALUE_SIZE);
}
if (pIniSection->count == 0) {
break;
}
}
}
}
IniSectionFree(pIniSection);
NP2HeapFree(pIniSectionBuf);
return TRUE;
}
return FALSE;
}
//=============================================================================
//
// Style_Export()
//
BOOL Style_Export(HWND hwnd) {
WCHAR szFile[MAX_PATH * 2] = L"";
WCHAR szFilter[256];
GetString(IDS_FILTER_INI, szFilter, COUNTOF(szFilter));
PrepareFilterStr(szFilter);
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = szFilter;
ofn.lpstrFile = szFile;
ofn.lpstrDefExt = L"ini";
ofn.nMaxFile = COUNTOF(szFile);
ofn.Flags = /*OFN_FILEMUSTEXIST |*/ OFN_HIDEREADONLY | OFN_NOCHANGEDIR | OFN_DONTADDTORECENT
| OFN_PATHMUSTEXIST | OFN_SHAREAWARE /*| OFN_NODEREFERENCELINKS*/ | OFN_OVERWRITEPROMPT;
if (GetSaveFileName(&ofn)) {
DWORD dwError = ERROR_SUCCESS;
IniSectionOnSave section;
WCHAR *pIniSectionBuf = (WCHAR *)NP2HeapAlloc(sizeof(WCHAR) * MAX_INI_SECTION_SIZE_STYLES);
const int cchIniSection = (int)(NP2HeapSize(pIniSectionBuf) / sizeof(WCHAR));
IniSectionOnSave *pIniSection = §ion;
pIniSection->next = pIniSectionBuf;
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
const LPCEDITLEXER pLex = pLexArray[iLexer];
IniSectionSetString(pIniSection, pLex->pszName, pLex->szExtensions);
}
if (!WritePrivateProfileSection(INI_SECTION_NAME_FILE_EXTENSIONS, pIniSectionBuf, szFile)) {
dwError = GetLastError();
}
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
const LPCEDITLEXER pLex = pLexArray[iLexer];
ZeroMemory(pIniSectionBuf, cchIniSection);
pIniSection->next = pIniSectionBuf;
const UINT iStyleCount = pLex->iStyleCount;
for (UINT i = 0; i < iStyleCount; i++) {
IniSectionSetString(pIniSection, pLex->Styles[i].pszName, pLex->Styles[i].szValue);
}
if (!WritePrivateProfileSection(pLex->pszName, pIniSectionBuf, szFile)) {
dwError = GetLastError();
}
}
NP2HeapFree(pIniSectionBuf);
if (dwError != ERROR_SUCCESS) {
MsgBox(MBINFO, IDS_EXPORT_FAIL, szFile);
}
return TRUE;
}
return FALSE;
}
static void Style_ResetAll(void) {
CopyMemory(customColor, defaultCustomColor, MAX_CUSTOM_COLOR_COUNT * sizeof(COLORREF));
for (UINT iLexer = 0; iLexer < NUMLEXERS; iLexer++) {
PEDITLEXER pLex = pLexArray[iLexer];
lstrcpy(pLex->szExtensions, pLex->pszDefExt);
pLex->bStyleChanged = TRUE;
const UINT iStyleCount = pLex->iStyleCount;
for (UINT i = 0; i < iStyleCount; i++) {
lstrcpy(pLex->Styles[i].szValue, pLex->Styles[i].pszDefault);
}
}
fStylesModified |= STYLESMODIFIED_ALL_STYLE | STYLESMODIFIED_FILE_EXT | STYLESMODIFIED_COLOR;
}
void Style_OnDPIChanged(HWND hwnd) {
Style_SetLexer(hwnd, pLexCurrent);
}
void Style_UpdateCaret(HWND hwnd) {
// caret style and width
const int style = (iCaretStyle ? CARETSTYLE_LINE : CARETSTYLE_BLOCK)
| (iOvrCaretStyle ? CARETSTYLE_OVERSTRIKE_BLOCK : CARETSTYLE_OVERSTRIKE_BAR);
SendMessage(hwnd, SCI_SETCARETSTYLE, style, 0);
if (iCaretStyle != 0) {
SendMessage(hwnd, SCI_SETCARETWIDTH, iCaretStyle, 0);
}
const int iValue = (iCaretBlinkPeriod < 0)? (int)GetCaretBlinkTime() : iCaretBlinkPeriod;
SendMessage(hwnd, SCI_SETCARETPERIOD, iValue, 0);
}
void Style_UpdateLexerKeywordAttr(LPCEDITLEXER pLexNew) {
UINT8 *attr = currentLexKeywordAttr;
memset(currentLexKeywordAttr, 0, sizeof(currentLexKeywordAttr));
// Code Snippet
attr[NUMKEYWORD - 1] = KeywordAttr_NoLexer;
switch (pLexNew->rid) {
case NP2LEX_CPP:
attr[2] = KeywordAttr_NoAutoComp; // Preprocessor
attr[3] = KeywordAttr_NoAutoComp; // Directive
attr[11] = KeywordAttr_NoAutoComp; // Assembler Intruction
attr[12] = KeywordAttr_NoAutoComp; // Assembler Register
attr[13] = KeywordAttr_NoLexer; // C Function
attr[14] = KeywordAttr_NoLexer; // C++ Function
break;
case NP2LEX_JAVA:
attr[10] = KeywordAttr_NoLexer; // Package
break;
case NP2LEX_CSHARP:
attr[2] = KeywordAttr_NoAutoComp; // Preprocessor
break;
case NP2LEX_JS:
//attr[1] = KeywordAttr_NoAutoComp; // Reserved Word
attr[9] = KeywordAttr_NoLexer; // Function
attr[10] = KeywordAttr_NoLexer; // Property
attr[11] = KeywordAttr_NoLexer; // Method
break;
case NP2LEX_RC:
attr[2] = KeywordAttr_NoAutoComp; // Preprocessor
break;
case NP2LEX_D:
attr[2] = KeywordAttr_NoAutoComp; // Preprocessor
attr[11] = KeywordAttr_NoAutoComp; // Assembler Intruction
attr[12] = KeywordAttr_NoAutoComp; // Assembler Register
break;
case NP2LEX_HAXE:
attr[2] = KeywordAttr_NoAutoComp; // Preprocessor
break;
case NP2LEX_VB:
case NP2LEX_VBS:
attr[0] = KeywordAttr_MakeLower;
attr[1] = KeywordAttr_MakeLower;
attr[2] = KeywordAttr_MakeLower;
attr[3] = KeywordAttr_MakeLower | KeywordAttr_NoAutoComp; // Preprocessor
attr[4] = KeywordAttr_MakeLower;
attr[5] = KeywordAttr_MakeLower;
attr[6] = KeywordAttr_MakeLower;
break;
case NP2LEX_HTML:
attr[2] = KeywordAttr_MakeLower; // VBScript
break;
case NP2LEX_SQL:
attr[6] = KeywordAttr_NoLexer; // Upper Case Keyword
attr[7] = KeywordAttr_NoLexer; // Upper Case Type
attr[8] = KeywordAttr_NoLexer; // Upper Case Function
break;
case NP2LEX_PHP:
attr[9] = KeywordAttr_NoLexer; // Function
attr[10] = KeywordAttr_NoLexer; // Field
attr[11] = KeywordAttr_NoLexer; // Method
attr[12] = KeywordAttr_NoLexer; // Tag
attr[13] = KeywordAttr_NoLexer; // String Constant
break;
case NP2LEX_PYTHON:
attr[3] = KeywordAttr_NoAutoComp; // Decorator
attr[9] = KeywordAttr_NoLexer; // Module
attr[10] = KeywordAttr_NoLexer; // Method
attr[11] = KeywordAttr_NoLexer; // Constant
attr[12] = KeywordAttr_NoLexer; // Attribute
break;
case NP2LEX_BATCH:
attr[6] = KeywordAttr_NoLexer; // Upper Case Keyword
break;
case NP2LEX_AU3:
break;
case NP2LEX_INNO:
break;
case NP2LEX_NSIS:
attr[0] = KeywordAttr_MakeLower;
break;
default:
break;
}
}
static inline BOOL DidLexerHasLineComment(int iLexer) {
return !(iLexer == SCLEX_NULL
|| iLexer == SCLEX_DIFF
);
}
static inline BOOL DidLexerHasBlockComment(int iLexer, int rid) {
return !(iLexer == SCLEX_NULL
|| iLexer == SCLEX_BASH
|| iLexer == SCLEX_BATCH
|| iLexer == SCLEX_CMAKE
|| iLexer == SCLEX_CONF
|| iLexer == SCLEX_DIFF
|| iLexer == SCLEX_LLVM
|| iLexer == SCLEX_MAKEFILE
|| iLexer == SCLEX_PERL
|| iLexer == SCLEX_PROPERTIES
|| iLexer == SCLEX_PYTHON
|| iLexer == SCLEX_RUBY
|| iLexer == SCLEX_SMALI
|| iLexer == SCLEX_TEXINFO
|| iLexer == SCLEX_VB
|| iLexer == SCLEX_VBSCRIPT
|| iLexer == SCLEX_VIM
|| rid == NP2LEX_AWK
|| rid == NP2LEX_JAM
);
}
static void Style_Parse(struct DetailStyle *style, LPCWSTR lpszStyle);
static void Style_SetParsed(HWND hwnd, const struct DetailStyle *style, int iStyle);
static inline void Style_SetDefaultStyle(HWND hwnd, int index) {
Style_SetStyles(hwnd, lexDefault.Styles[index].iStyle, lexDefault.Styles[index].szValue);
}
//=============================================================================
// set current lexer
// Style_SetLexer()
//
void Style_SetLexer(HWND hwnd, PEDITLEXER pLexNew) {
int iValue;
char msg[10];
// Select default if NULL is specified
if (!pLexNew) {
np2LexLangIndex = 0;
pLexNew = pLexArray[iDefaultLexer];
}
if (!pLexNew->szStyleBuf) {
Style_LoadOne(pLexNew);
}
// Lexer
const int iLexer = pLexNew->iLexer;
SendMessage(hwnd, SCI_SETLEXER, iLexer, 0);
int rid = pLexNew->rid;
if (rid == NP2LEX_MATLAB) {
if (np2LexLangIndex == IDM_LANG_OCTAVE) {
rid = NP2LEX_OCTAVE;
} else if (np2LexLangIndex == IDM_LANG_SCILAB) {
rid = NP2LEX_SCILAB;
}
}
_itoa(rid - 63000, msg, 10);
SciCall_SetProperty("lexer.lang.type", msg);
// Code folding
SciCall_SetProperty("fold", "1");
SciCall_SetProperty("fold.foldsyntaxbased", "1");
SciCall_SetProperty("fold.comment", "1");
SciCall_SetProperty("fold.preprocessor", "1");
SciCall_SetProperty("fold.compact", "0");
switch (rid) {
case NP2LEX_HTML:
case NP2LEX_XML:
SciCall_SetProperty("fold.html", "1");
SciCall_SetProperty("fold.hypertext.comment", "1");
SciCall_SetProperty("fold.hypertext.heredoc", "1");
break;
case NP2LEX_CSS:
SciCall_SetProperty("lexer.css.scss.language", ((np2LexLangIndex == IDM_LANG_SCSS)? "1" : "0"));
SciCall_SetProperty("lexer.css.less.language", ((np2LexLangIndex == IDM_LANG_LESS)? "1" : "0"));
SciCall_SetProperty("lexer.css.hss.language", ((np2LexLangIndex == IDM_LANG_HSS)? "1" : "0"));
break;
case NP2LEX_BASH:
SciCall_SetProperty("lexer.bash.csh.language", ((np2LexLangIndex == IDM_LANG_CSHELL)? "1" : "0"));
break;
}
Style_UpdateLexerKeywords(pLexNew);
Style_UpdateLexerKeywordAttr(pLexNew);
// Add keyword lists
for (int i = 0; i < NUMKEYWORD; i++) {
const char *pKeywords = pLexNew->pKeyWords->pszKeyWords[i];
if (StrNotEmptyA(pKeywords) && !(currentLexKeywordAttr[i] & KeywordAttr_NoLexer)) {
if (currentLexKeywordAttr[i] & KeywordAttr_MakeLower) {
const size_t len = strlen(pKeywords);
char *lowerKeywords = (char *)NP2HeapAlloc(len + 1);
char *p = lowerKeywords;
CopyMemory(lowerKeywords, pKeywords, len);
while (*p) {
if (*p >= 'A' && *p <= 'Z') {
*p += 'a' - 'A';
}
++p;
}
SendMessage(hwnd, SCI_SETKEYWORDS, i, (LPARAM)lowerKeywords);
NP2HeapFree(lowerKeywords);
} else {
SendMessage(hwnd, SCI_SETKEYWORDS, i, (LPARAM)pKeywords);
}
} else {
SendMessage(hwnd, SCI_SETKEYWORDS, i, (LPARAM)"");
}
}
const int iIdx = GetDefaultStyleStartIndex();
// Font quality setup
SendMessage(hwnd, SCI_SETFONTQUALITY, iFontQuality, 0);
// Clear
SendMessage(hwnd, SCI_CLEARDOCUMENTSTYLE, 0, 0);
// Default Values are always set
SendMessage(hwnd, SCI_STYLERESETDEFAULT, 0, 0);
SendMessage(hwnd, SCI_STYLESETCHARACTERSET, STYLE_DEFAULT, DEFAULT_CHARSET);
//! begin Style_Default
LPCWSTR szValue = lexDefault.Styles[Style_Default + iIdx].szValue;
Style_StrGetFontSize(szValue, &iBaseFontSize);
Style_SetStyles(hwnd, STYLE_DEFAULT, szValue);
// Auto-select codepage according to charset
//Style_SetACPfromCharSet(hwnd);
if (!Style_StrGetColor(TRUE, szValue, &iValue)) {
SendMessage(hwnd, SCI_STYLESETFORE, STYLE_DEFAULT, GetSysColor(COLOR_WINDOWTEXT));
}
if (!Style_StrGetColor(FALSE, szValue, &iValue)) {
SendMessage(hwnd, SCI_STYLESETBACK, STYLE_DEFAULT, GetSysColor(COLOR_WINDOW));
}
//! end Style_Default
SendMessage(hwnd, SCI_STYLECLEARALL, 0, 0);
Style_SetDefaultStyle(hwnd, Style_LineNumber + iIdx);
Style_SetDefaultStyle(hwnd, Style_MatchBrace + iIdx);
Style_SetDefaultStyle(hwnd, Style_MatchBraceError + iIdx);
if (rid != NP2LEX_ANSI) {
Style_SetDefaultStyle(hwnd, Style_ControlCharacter + iIdx);
}
Style_SetDefaultStyle(hwnd, Style_IndentationGuide + iIdx);
//! begin Style_Selection
szValue = lexDefault.Styles[Style_Selection + iIdx].szValue;
if (Style_StrGetColor(TRUE, szValue, &iValue)) {
SendMessage(hwnd, SCI_SETSELFORE, TRUE, iValue);
SendMessage(hwnd, SCI_SETADDITIONALSELFORE, iValue, 0);
} else {
SendMessage(hwnd, SCI_SETSELFORE, FALSE, 0);
SendMessage(hwnd, SCI_SETADDITIONALSELFORE, 0, 0);
}
// always set background color
if (!Style_StrGetColor(FALSE, szValue, &iValue)) {
iValue = RGB(0xC0, 0xC0, 0xC0);
}
SendMessage(hwnd, SCI_SETSELBACK, TRUE, iValue);
SendMessage(hwnd, SCI_SETADDITIONALSELBACK, iValue, 0);
if (!Style_StrGetAlpha(szValue, &iValue)) {
iValue = SC_ALPHA_NOALPHA;
}
SendMessage(hwnd, SCI_SETSELALPHA, iValue, 0);
SendMessage(hwnd, SCI_SETADDITIONALSELALPHA, iValue, 0);
SendMessage(hwnd, SCI_SETSELEOLFILLED, (StrStr(szValue, L"eolfilled") != NULL), 0);
//! end Style_Selection
//! begin Style_Whitespace
szValue = lexDefault.Styles[Style_Whitespace + iIdx].szValue;
if (Style_StrGetColor(TRUE, szValue, &iValue)) {
SendMessage(hwnd, SCI_SETWHITESPACEFORE, TRUE, iValue);
} else {
SendMessage(hwnd, SCI_SETWHITESPACEFORE, FALSE, 0);
}
if (Style_StrGetColor(FALSE, szValue, &iValue)) {
SendMessage(hwnd, SCI_SETWHITESPACEBACK, TRUE, iValue);
} else {
SendMessage(hwnd, SCI_SETWHITESPACEBACK, FALSE, 0);
}
// whitespace dot size
iValue = 1;
Style_StrGetRawSize(szValue, &iValue);
iValue = max_i(0, RoundToCurrentDPI(iValue));
SendMessage(hwnd, SCI_SETWHITESPACESIZE, iValue, 0);
//! end Style_Whitespace