forked from rizonesoft/Notepad3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notepad3.c
13121 lines (10819 loc) · 445 KB
/
Notepad3.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
// encoding: UTF-8
/******************************************************************************
* *
* *
* Notepad3 *
* *
* Notepad3.c *
* Main application window functionality *
* Based on code from Notepad2, (c) Florian Balmer 1996-2011 *
* *
* (c) Rizonesoft 2008-2024 *
* https://rizonesoft.com *
* *
* *
*******************************************************************************/
#include "Helpers.h"
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#include <commctrl.h>
#include <uxtheme.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <commdlg.h>
#include <stdio.h>
#include <string.h>
#include <process.h>
#include <vsstyle.h>
//#include <ShellScalingApi.h>
#include "PathLib.h"
#include "Edit.h"
#include "Styles.h"
#include "Dialogs.h"
#include "crypto/crypto.h"
#include "uthash/utarray.h"
#include "uthash/utlist.h"
#include "tinyexpr/tinyexpr.h"
//#include "tinyexprcpp/tinyexpr_cif.h"
#include "Encoding.h"
#include "VersionEx.h"
#include "MuiLanguage.h"
#include "Notepad3.h"
#include "Config/Config.h"
#include "DarkMode/DarkMode.h"
#include "StyleLexers/EditLexer.h"
#if (defined(_DEBUG) || defined(DEBUG)) && !defined(NDEBUG)
#if defined(WIN32) && !defined(_WIN64)
#pragma comment(linker, "/defaultlib:clang_rt.asan-i386.lib")
#endif // _WIN64
#endif // DEBUG
// ============================================================================
//
// Local and global Variables for Notepad3.c
//
// ============================================================================
LPCWSTR WordBookMarks[MARKER_NP3_BOOKMARK] = {
/*0*/ L"back:#0000", // OCC MARKER
/*1*/ L"back:#FF0000",
/*2*/ L"back:#00FF00",
/*3*/ L"back:#0000FF",
/*4*/ L"back:#FF8000",
/*5*/ L"back:#0080FF",
/*6*/ L"back:#FF00FF",
/*7*/ L"back:#408040",
/*8*/ L"back:#C080C0"
};
#define RELAUNCH_ELEVATED_BUF_ARG L"tmpfbuf="
CONSTANTS_T const Constants = {
2 // StdDefaultLexerID
, L"minipath.exe" // FileBrowserMiniPath
, L"grepWinNP3.exe" // FileSearchGrepWin
, L"Settings" // Inifile Section "Settings"
, L"Settings2" // Inifile Section "Settings2"
, L"Window" // Inifile Section "Window"
, L"Styles" // Inifile Section "Styles"
, L"Suppressed Messages" // Inifile Section "SuppressedMessages"
};
FLAGS_T Flags = { 0 };
FLAGS_T DefaultFlags = { 0 };
GLOBALS_T Globals = { 0 };
SETTINGS_T Settings = { 0 };
SETTINGS_T Defaults = { 0 };
SETTINGS2_T Settings2 = { 0 };
PATHS_T Paths = { 0 };
FOCUSEDVIEW_T FocusedView = { 0 };
FILEWATCHING_T FileWatching = { 0 };
// set by InitScintillaHandle()
HWND g_hwndEditWindow = NULL;
HANDLE g_hndlScintilla = NULL;
// window positioning
WININFO g_IniWinInfo = INIT_WININFO;
WININFO g_DefWinInfo = INIT_WININFO;
COLORREF g_colorCustom[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
prefix_t g_mxSBPrefix[STATUS_SECTOR_COUNT] = { L'\0' };
prefix_t g_mxSBPostfix[STATUS_SECTOR_COUNT] = { L'\0' };
int g_flagMatchText = 0;
bool g_iStatusbarVisible[STATUS_SECTOR_COUNT] = SBS_INIT_ZERO;
int g_iStatusbarWidthSpec[STATUS_SECTOR_COUNT] = SBS_INIT_ZERO;
int g_vSBSOrder[STATUS_SECTOR_COUNT] = SBS_INIT_MINUS;
HPATHL g_tchToolbarBitmap = NULL;
HPATHL g_tchToolbarBitmapHot = NULL;
HPATHL g_tchToolbarBitmapDisabled = NULL;
WCHAR Default_PreferredLanguageLocaleName[LOCALE_NAME_MAX_LENGTH + 1] = { L'\0' };
// ------------------------------------
HPATHL s_hpthRelaunchElevatedFile = NULL;
static bool s_bInitAppDone = false;
static bool s_bIsProcessElevated = false;
static bool s_bIsUserInAdminGroup = false;
static bool s_bIsRunAsAdmin = false;
static bool s_flagSaveOnRelaunch = false;
static bool s_IsThisAnElevatedRelaunch = false;
static WCHAR s_wchWndClass[64] = { L'\0' };
static HWND s_hwndEditFrame = NULL;
static HWND s_hwndNextCBChain = NULL;
static int s_WinCurrentWidth = 0;
#define FILE_LIST_SIZE 32
static LPWSTR s_lpFileList[FILE_LIST_SIZE] = { NULL };
static int s_cFileList = 0;
static int s_cchiFileList = 0;
static int s_iSortOptions = 0;
static int s_iAlignMode = 0;
static bool s_bIsAppThemed = true;
static UINT s_msgTaskbarCreated = 0;
static WCHAR s_wchTitleExcerpt[MIDSZ_BUFFER] = { L'\0' };
static LONG64 s_iLastCopyTime = 0;
static bool s_bLastCopyFromMe = false;
static bool s_bInMultiEditMode = false;
static bool s_bCallTipEscDisabled = false;
static int s_iInitialLine = 0;
static int s_iInitialColumn = 0;
static int s_iInitialLexer = 0;
static int s_cyReBar = 0;
static int s_cyReBarFrame = 0;
static int s_cxEditFrame = 0;
static int s_cyEditFrame = 0;
static bool s_bUndoRedoScroll = false;
// for tiny expression calculation
static double s_dExpression = 0.0;
static te_int_t s_iExprError = -1;
static char* s_SelectionBuffer = NULL;
//~static CONST WCHAR *const s_ToolbarWndClassName = L"NP3_TOOLBAR_CLASS";
static int const INISECTIONBUFCNT = 32; // .ini file load buffer in KB
// ----------------------------------------------------------------------------
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// (!) ENSURE IDT_FILE_NEW -> IDT_VIEW_NEW_WINDOW corresponds to order of Toolbar.bmp
#define NUMTOOLBITMAPS (31)
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
static TBBUTTON s_tbbMainWnd[] = {
{ 0, IDT_FILE_NEW, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 31, IDT_VIEW_NEW_WINDOW, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 1, IDT_FILE_OPEN, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 3, IDT_FILE_SAVE, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 2, IDT_FILE_BROWSE, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 27, IDT_FILE_RECENT, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 4, IDT_EDIT_UNDO, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 5, IDT_EDIT_REDO, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 6, IDT_EDIT_CUT, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 7, IDT_EDIT_COPY, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 8, IDT_EDIT_PASTE, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 9, IDT_EDIT_FIND, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 10, IDT_EDIT_REPLACE, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 29, IDT_GREP_WIN_TOOL, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 11, IDT_VIEW_WORDWRAP, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 23, IDT_VIEW_TOGGLEFOLDS, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 25, IDT_VIEW_TOGGLE_VIEW, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 21, IDT_FILE_OPENFAV, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 22, IDT_FILE_ADDTOFAV, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 12, IDT_VIEW_ZOOMIN, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 30, IDT_VIEW_RESETZOOM, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 13, IDT_VIEW_ZOOMOUT, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 14, IDT_VIEW_SCHEME, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 24, IDT_FILE_LAUNCH, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 28, IDT_VIEW_PIN_ON_TOP, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 16, IDT_FILE_EXIT, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 0, 0, 0, BTNS_SEP, { 0 }, 0, 0 },
{ 15, IDT_VIEW_SCHEMECONFIG, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 17, IDT_FILE_SAVEAS, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 18, IDT_FILE_SAVECOPY, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 19, IDT_EDIT_CLEAR, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 20, IDT_FILE_PRINT, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 },
{ 26, IDT_VIEW_CHASING_DOCTAIL, TBSTATE_ENABLED, BTNS_BUTTON, { 0 }, 0, 0 }
};
// don't show buttons beyond this TBBUTTON[] index:
#define TBBUTTON_LAST_DEFAULT (39)
WCHAR TBBUTTON_DEFAULT_IDS[256] = { L'\0' }; // filled in _InitGlobals()
const WCHAR* const TBBUTTON_DEFAULT_IDS_OLD = L"1 32 2 4 3 28 0 5 6 0 7 8 9 0 10 11 0 30 0 12 0 24 26 0 22 23 0 13 14 0 27 0 15 0 25 0 17";
// ----------------------------------------------------------------------------
const char chr_currency[6] = { '$', 0x80, 0xA2, 0xA3, 0xA5, '\0' }; // "$€¢£¥" CP-1252
//==============================================================================
//
// Save Needed Flag
//
static bool s_NeedSavingForced = false; // dirty-flag
static inline bool IsSaveNeeded()
{
return SciCall_GetModify() || s_NeedSavingForced;
}
static inline void SetSaveNeeded(const bool bSetFlagForced)
{
AutoSaveStart(!s_NeedSavingForced);
if (bSetFlagForced) {
s_NeedSavingForced = true;
}
if (IsWindow(Globals.hwndDlgFindReplace)) {
PostWMCommand(Globals.hwndDlgFindReplace, IDC_DOC_MODIFIED);
}
UpdateToolbar();
}
static inline void SetSavePoint()
{
SciCall_SetSavePoint();
UpdateToolbar();
}
void SetSaveDone()
{
s_NeedSavingForced = false;
SetSavePoint();
}
//==============================================================================
// current find pattern
static HSTRINGW s_hstrCurrentFindPattern = NULL;
// undo / redo selections
static UT_icd UndoRedoSelElement_icd = { sizeof(DocPos), NULL, NULL, NULL };
static void InitUndoRedoSelection(void* elt)
{
UndoRedoSelection_t* selection = (UndoRedoSelection_t*)elt;
if (selection != NULL) {
selection->selMode_undo = SC_SEL_STREAM;
selection->anchorPos_undo = NULL;
selection->curPos_undo = NULL;
selection->anchorVS_undo = NULL;
selection->curVS_undo = NULL;
selection->selMode_redo = SC_SEL_STREAM;
selection->anchorPos_redo = NULL;
selection->curPos_redo = NULL;
selection->anchorVS_redo = NULL;
selection->curVS_redo = NULL;
}
}
static void DelUndoRedoSelection(void* elt)
{
UndoRedoSelection_t* selection = (UndoRedoSelection_t*)elt;
if (selection != NULL)
{
selection->selMode_undo = SC_SEL_STREAM;
selection->selMode_redo = SC_SEL_STREAM;
if (selection->anchorPos_undo != NULL) {
utarray_free(selection->anchorPos_undo);
selection->anchorPos_undo = NULL;
}
if (selection->curPos_undo != NULL) {
utarray_free(selection->curPos_undo);
selection->curPos_undo = NULL;
}
if (selection->anchorVS_undo != NULL) {
utarray_free(selection->anchorVS_undo);
selection->anchorVS_undo = NULL;
}
if (selection->curVS_undo != NULL) {
utarray_free(selection->curVS_undo);
selection->curVS_undo = NULL;
}
if (selection->anchorPos_redo != NULL) {
utarray_free(selection->anchorPos_redo);
selection->anchorPos_redo = NULL;
}
if (selection->curPos_redo != NULL) {
utarray_free(selection->curPos_redo);
selection->curPos_redo = NULL;
}
if (selection->anchorVS_redo != NULL) {
utarray_free(selection->anchorVS_redo);
selection->anchorVS_redo = NULL;
}
if (selection->curVS_redo != NULL) {
utarray_free(selection->curVS_redo);
selection->curVS_redo = NULL;
}
}
}
static void CopyUndoRedoSelection(void* dst, const void* src)
{
UndoRedoSelection_t* dst_sel = (UndoRedoSelection_t*)dst;
const UndoRedoSelection_t* src_sel = (UndoRedoSelection_t*)src;
DocPos* pPos = NULL;
InitUndoRedoSelection(dst);
dst_sel->selMode_undo = (src_sel) ? src_sel->selMode_undo : SC_SEL_STREAM;
utarray_new(dst_sel->anchorPos_undo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->anchorPos_undo, pPos)) != NULL) {
utarray_push_back(dst_sel->anchorPos_undo, pPos);
}
}
utarray_new(dst_sel->curPos_undo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->curPos_undo, pPos)) != NULL) {
utarray_push_back(dst_sel->curPos_undo, pPos);
}
}
utarray_new(dst_sel->anchorVS_undo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->anchorVS_undo, pPos)) != NULL) {
utarray_push_back(dst_sel->anchorVS_undo, pPos);
}
}
utarray_new(dst_sel->curVS_undo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->curVS_undo, pPos)) != NULL) {
utarray_push_back(dst_sel->curVS_undo, pPos);
}
}
dst_sel->selMode_redo = (src_sel) ? src_sel->selMode_redo : SC_SEL_STREAM;
utarray_new(dst_sel->anchorPos_redo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->anchorPos_redo, pPos)) != NULL) {
utarray_push_back(dst_sel->anchorPos_redo, pPos);
}
}
utarray_new(dst_sel->curPos_redo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->curPos_redo, pPos)) != NULL) {
utarray_push_back(dst_sel->curPos_redo, pPos);
}
}
utarray_new(dst_sel->anchorVS_redo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->anchorVS_redo, pPos)) != NULL) {
utarray_push_back(dst_sel->anchorVS_redo, pPos);
}
}
utarray_new(dst_sel->curVS_redo, &UndoRedoSelElement_icd);
if (src_sel) {
while ((pPos = (DocPos*)utarray_next(src_sel->curVS_redo, pPos)) != NULL) {
utarray_push_back(dst_sel->curVS_redo, pPos);
}
}
}
static UT_icd UndoRedoSelection_icd = { sizeof(UndoRedoSelection_t), InitUndoRedoSelection, CopyUndoRedoSelection, DelUndoRedoSelection };
static UT_array* UndoRedoSelectionUTArray = NULL;
static void _SaveRedoSelection(const LONG token, const bool bAddAction);
static LONG _SaveUndoSelection();
static LONG _UndoRedoActionMap(const LONG token, const UndoRedoSelection_t** selection);
// => UndoTransActionBegin();
// => EndUndoTransAction();
static volatile int UndoRedoActionStackCount = 0;
__forceinline bool _InUndoRedoTransaction()
{
return (UndoRedoActionStackCount > 0);
}
static inline void _SplitUndoTransaction()
{
if (_InUndoRedoTransaction()) {
SciCall_EndUndoAction();
SciCall_BeginUndoAction();
}
else {
SciCall_BeginUndoAction();
SciCall_EndUndoAction();
}
}
// ----------------------------------------------------------------------------
static void _DelayClearCallTip(const LONG64 delay);
static void _DelaySplitUndoTransaction(const LONG64 delay);
static void _RestoreActionSelection(const LONG token, DoAction doAct);
// ----------------------------------------------------------------------------
#if 0
static HANDLE s_hEvent = INVALID_HANDLE_VALUE;
// SetEvent(s_hEvent);
// ResetEvent(s_hEvent);
static __forceinline bool IsEventSignaled() {
return (WaitForSingleObject(s_hEvent, 0) == WAIT_OBJECT_0);
}
#endif
// ----------------------------------------------------------------------------
__forceinline bool NotifyDocChanged()
{
return ((SciCall_GetModEventMask() & EVM_Default) == EVM_Default);
}
int DisableDocChangeNotification()
{
int const currentMask = SciCall_GetModEventMask();
SciCall_SetModEventMask(EVM_None);
return currentMask;
}
void EnableDocChangeNotification(const int evm)
{
SciCall_SetModEventMask(evm);
if (NotifyDocChanged()) {
EditUpdateVisibleIndicators();
UpdateStatusbar(false);
}
}
//=============================================================================
//
// Delay Message Queue Handling (TODO: MultiThreading)
//
static CmdMessageQueue_t* MessageQueue = NULL;
// ----------------------------------------------------------------------------
static int msgcmp(void* mqc1, void* mqc2)
{
const CmdMessageQueue_t* const pMQC1 = (CmdMessageQueue_t*)mqc1;
const CmdMessageQueue_t* const pMQC2 = (CmdMessageQueue_t*)mqc2;
if ((pMQC1->cmd == pMQC2->cmd)
&& (pMQC1->hwnd == pMQC2->hwnd)
&& (pMQC1->wparam == pMQC2->wparam) // command
&& (pMQC1->lparam == pMQC2->lparam) // true/false
) {
return 0; // equal
}
return 1;
}
//~static int sortcmp(void *mqc1, void *mqc2) {
//~
//~ const CmdMessageQueue_t *const pMQC1 = (CmdMessageQueue_t *)mqc1;
//~ const CmdMessageQueue_t *const pMQC2 = (CmdMessageQueue_t *)mqc2;
//~
//~ return (pMQC1->delay - pMQC2->delay);
//~}
// ----------------------------------------------------------------------------
#define _MQ_TIMER_CYCLE (USER_TIMER_MINIMUM << 1) // 20ms cycle
#define _MQ_IMMEDIATE (USER_TIMER_MINIMUM >> 1)
#define _MQ_FAST (_MQ_TIMER_CYCLE << 1)
#define _MQ_STD (_MQ_TIMER_CYCLE << 2)
#define _MQ_LAZY (_MQ_TIMER_CYCLE << 3)
#define _MQ_ms2cycl(T) (((T) + USER_TIMER_MINIMUM) / _MQ_TIMER_CYCLE)
static void _MQ_AppendCmd(CmdMessageQueue_t* const pMsgQCmd, LONG64 cycles)
{
if (!pMsgQCmd) { return; }
cycles = clampll(cycles, 0, _MQ_ms2cycl(60000));
CmdMessageQueue_t* pmqc = NULL;
DL_SEARCH(MessageQueue, pmqc, pMsgQCmd, msgcmp);
if (!pmqc) { // NOT found, create one
pmqc = AllocMem(sizeof(CmdMessageQueue_t), HEAP_ZERO_MEMORY);
if (pmqc) {
*pmqc = *pMsgQCmd;
pmqc->delay = cycles;
DL_APPEND(MessageQueue, pmqc);
}
} else {
if ((pmqc->delay > 0) && (cycles > 0)) {
pmqc->delay = (pmqc->delay + cycles) >> 1; // median delay
} else {
pmqc->delay = cycles;
}
}
if (0 == cycles) {
PostMessage(pMsgQCmd->hwnd, pMsgQCmd->cmd, pMsgQCmd->wparam, pMsgQCmd->lparam);
}
//~DL_SORT(MessageQueue, sortcmp); // next scheduled first
}
// ----------------------------------------------------------------------------
/* not used yet
static void _MQ_DropAll()
{
CmdMessageQueue_t *pmqc = NULL;
DL_FOREACH(MessageQueue, pmqc)
{
pmqc->delay = -1;
}
}
*/
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
//
// called by Timer(IDT_TIMER_MRKALL)
//
// TIMERPROC
static void CALLBACK MQ_ExecuteNext(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
UNREFERENCED_PARAMETER(hwnd); // must be main window handle
UNREFERENCED_PARAMETER(uMsg); // must be WM_TIMER
UNREFERENCED_PARAMETER(idEvent); // must be IDT_TIMER_MRKALL
UNREFERENCED_PARAMETER(dwTime); // This is the value returned by the GetTickCount() function
CmdMessageQueue_t* pmqc;
DL_FOREACH(MessageQueue, pmqc) {
if (pmqc->delay >= 0) {
--(pmqc->delay); // count down
}
if (pmqc->delay == 0) {
PostMessage(pmqc->hwnd, pmqc->cmd, pmqc->wparam, pmqc->lparam);
}
}
}
//=============================================================================
//
// IsFileReadOnly
//
static bool IsFileReadOnly()
{
return (Path_IsNotEmpty(Paths.CurrentFile) ? IsReadOnly(Path_GetFileAttributes(Paths.CurrentFile)) : false);
}
//=============================================================================
//
// HasCurrentFileChanged
//
static FCOBSRVDATA_T s_FileChgObsvrData = INIT_FCOBSRV_T;
// ----------------------------------------------------------------------------
static inline bool IsFileChangedFlagSet() {
return (WaitForSingleObject(s_FileChgObsvrData.hEventFileChanged, 0) != WAIT_TIMEOUT);
}
static inline bool IsFileDeletedFlagSet() {
return (WaitForSingleObject(s_FileChgObsvrData.hEventFileDeleted, 0) != WAIT_TIMEOUT);
}
static inline bool RaiseFlagIfCurrentFileChanged() {
if (Path_IsEmpty(Paths.CurrentFile)) {
return false;
}
WIN32_FIND_DATA fdUpdated = { 0 };
if (!GetFileAttributesExW(Path_Get(Paths.CurrentFile), GetFileExInfoStandard, &fdUpdated)) {
// The current file has been removed
if (IsFileDeletedFlagSet()) {
return false;
}
SetEvent(s_FileChgObsvrData.hEventFileChanged);
SetEvent(s_FileChgObsvrData.hEventFileDeleted);
return true;
}
if (IsFileDeletedFlagSet()) {
// The current file has been restored
ResetEvent(s_FileChgObsvrData.hEventFileDeleted);
}
bool const changed = (s_FileChgObsvrData.fdCurFile.nFileSizeLow != fdUpdated.nFileSizeLow) || (s_FileChgObsvrData.fdCurFile.nFileSizeHigh != fdUpdated.nFileSizeHigh)
//~|| (CompareFileTime(&(s_FileChgObsvrData.fdCurFile.ftLastWriteTime), &fdUpdated.ftLastWriteTime) != 0)
|| (s_FileChgObsvrData.fdCurFile.ftLastWriteTime.dwLowDateTime != fdUpdated.ftLastWriteTime.dwLowDateTime)
|| (s_FileChgObsvrData.fdCurFile.ftLastWriteTime.dwHighDateTime != fdUpdated.ftLastWriteTime.dwHighDateTime);
if (changed) {
SetEvent(s_FileChgObsvrData.hEventFileChanged);
}
return changed;
}
// ----------------------------------------------------------------------------
static inline void ResetFileObservationData(const bool bResetEvt) {
if (bResetEvt) {
ResetEvent(s_FileChgObsvrData.hEventFileChanged);
ResetEvent(s_FileChgObsvrData.hEventFileDeleted);
}
if (Path_IsNotEmpty(Paths.CurrentFile)) {
if (!GetFileAttributesEx(Path_Get(Paths.CurrentFile), GetFileExInfoStandard, &(s_FileChgObsvrData.fdCurFile)))
{
ZeroMemory(&(s_FileChgObsvrData.fdCurFile), sizeof(WIN32_FIND_DATA));
}
}
}
// ----------------------------------------------------------------------------
#define TE_ZERO (1.0E-8)
#define TE_FMTA "%.8G"
#define TE_FMTW L"%.8G"
void TinyExprToStringA(LPSTR pszDest, size_t cchDest, const double dExprEval)
{
double intpart = 0.0;
double const fracpart = modf(dExprEval, &intpart);
if ((fabs(fracpart) < TE_ZERO) && (fabs(intpart) < 1.0E+21)) {
StringCchPrintfA(pszDest, cchDest, "%.21G", intpart); // integer full number display
}
else {
StringCchPrintfA(pszDest, cchDest, TE_FMTA, dExprEval);
}
}
// ----------------------------------------------------------------------------
void TinyExprToString(LPWSTR pszDest, size_t cchDest, const double dExprEval)
{
double intpart = 0.0;
double const fracpart = modf(dExprEval, &intpart);
if ((fabs(fracpart) < TE_ZERO) && (fabs(intpart) < 1.0E+21)) {
StringCchPrintf(pszDest, cchDest, L"%.21G", intpart); // integer full number display
}
else {
StringCchPrintf(pszDest, cchDest, TE_FMTW, dExprEval);
}
}
// ----------------------------------------------------------------------------
//=============================================================================
//
// InvalidateStyleRedraw
//
static inline void InvalidateStyleRedraw()
{
SciCall_SetViewEOL(Settings.ViewEOLs);
}
//=============================================================================
//
// CommandLine Parsing Flags
//
static LPWSTR s_lpSchemeArg = NULL;
static LPWSTR s_lpOrigFileArg = NULL;
static HPATHL s_pthArgFilePath = NULL;
static HPATHL s_pthCheckFilePath = NULL;
static cpi_enc_t s_flagSetEncoding = CPI_NONE;
static int s_flagSetEOLMode = 0;
static bool s_flagStartAsTrayIcon = false;
static bool s_flagKeepTitleExcerpt = false;
static bool s_flagNewFromClipboard = false;
static bool s_flagPasteBoard = false;
static bool s_flagJumpTo = false;
static FILE_WATCHING_MODE s_flagChangeNotify = FWM_NO_INIT;
static bool s_flagQuietCreate = false;
static bool s_flagLexerSpecified = false;
static bool s_flagDisplayHelp = false;
static int s_iCaretPolicyV = CARET_EVEN;
//==============================================================================
static volatile HANDLE s_hEventAppIsClosing = INVALID_HANDLE_VALUE;
static __forceinline bool IsAppClosing() {
return (WaitForSingleObject(s_hEventAppIsClosing, 0) == WAIT_OBJECT_0);
}
//==============================================================================
// static forward declarations
static void _UpdateStatusbarDelayed(bool bForceRedraw);
static void _UpdateToolbarDelayed();
static void _UpdateTitlebarDelayed(const HWND hwnd);
//==============================================================================
static void _InitGlobals()
{
Globals.hLngResContainer = NULL;
Globals.hDlgIcon256 = NULL;
Globals.hDlgIcon128 = NULL;
Globals.hDlgIconBig = NULL;
Globals.hDlgIconSmall = NULL;
Globals.hDlgIconPrefs256 = NULL;
Globals.hDlgIconPrefs128 = NULL;
Globals.hDlgIconPrefs64 = NULL;
Globals.hMainMenu = NULL;
Globals.pFileMRU = NULL;
Globals.pMRUfind = NULL;
Globals.pMRUreplace = NULL;
Globals.uAvailLngCount = 1;
Globals.iWrapCol = 80;
Globals.CmdLnFlag_PosParam = false;
Globals.CmdLnFlag_AlwaysOnTop = 0;
Globals.CmdLnFlag_WindowPos = 0;
Globals.CmdLnFlag_ReuseWindow = 0;
Globals.CmdLnFlag_SingleFileInstance = 0;
Globals.CmdLnFlag_MultiFileArg = 0;
Globals.CmdLnFlag_ShellUseSystemMRU = 0;
Globals.CmdLnFlag_PrintFileAndLeave = 0;
Globals.DOSEncoding = CPI_NONE;
Globals.bZeroBasedColumnIndex = false;
Globals.bZeroBasedCharacterCount = false;
Globals.iReplacedOccurrences = 0;
Globals.iSelectionMarkNumber = 0;
Globals.iMarkOccurrencesCount = 0;
Globals.bUseLimitedAutoCCharSet = false;
Globals.bIsCJKInputCodePage = false;
Globals.bIniFileFromScratch = false;
Globals.bFindReplCopySelOrClip = true;
Globals.bReplaceInitialized = false;
Globals.FindReplaceMatchFoundState = FND_NOP;
Globals.bDocHasInconsistentEOLs = false;
Globals.pStdDarkModeIniStyles = NULL;
Globals.bMinimizedToTray = false;
Globals.uCurrentThemeIndex = 0;
Flags.bHugeFileLoadState = DefaultFlags.bHugeFileLoadState = false;
Flags.bDevDebugMode = DefaultFlags.bDevDebugMode = false;
Flags.bStickyWindowPosition = DefaultFlags.bStickyWindowPosition = false;
Flags.bReuseWindow = DefaultFlags.bReuseWindow = false;
Flags.bSingleFileInstance = DefaultFlags.bSingleFileInstance = true;
Flags.MultiFileArg = DefaultFlags.MultiFileArg = false;
Flags.RelativeFileMRU = DefaultFlags.RelativeFileMRU = true;
Flags.PortableMyDocs = DefaultFlags.PortableMyDocs = Flags.RelativeFileMRU;
Flags.NoFadeHidden = DefaultFlags.NoFadeHidden = false;
Flags.ToolbarLook = DefaultFlags.ToolbarLook = IsWindowsXPSP3OrGreater() ? 1 : 2;
Flags.SimpleIndentGuides = DefaultFlags.SimpleIndentGuides = false;
Flags.NoHTMLGuess =DefaultFlags.NoHTMLGuess = false;
Flags.NoCGIGuess = DefaultFlags.NoCGIGuess = false;
Flags.NoFileVariables = DefaultFlags.NoFileVariables = false;
Flags.SaveBlankNewFile = DefaultFlags.SaveBlankNewFile = true;
Flags.ShellUseSystemMRU = DefaultFlags.ShellUseSystemMRU = true;
Flags.PrintFileAndLeave = DefaultFlags.PrintFileAndLeave = 0;
Flags.bPreserveFileModTime = DefaultFlags.bPreserveFileModTime = false;
Flags.bDoRelaunchElevated = DefaultFlags.bDoRelaunchElevated = false;
Flags.bSearchPathIfRelative = DefaultFlags.bSearchPathIfRelative = false;
Flags.bSettingsFileSoftLocked = DefaultFlags.bSettingsFileSoftLocked = false;
Settings.EFR_Data.chFindPattern = StrgCreate(NULL);
Settings.EFR_Data.chReplaceTemplate = StrgCreate(NULL);
Defaults.EFR_Data.chFindPattern = StrgCreate(NULL);
Defaults.EFR_Data.chReplaceTemplate = StrgCreate(NULL);
Settings.OpenWithDir = Path_Allocate(NULL);
Defaults.OpenWithDir = Path_Allocate(NULL);
Settings.FavoritesDir = Path_Allocate(NULL);
Defaults.FavoritesDir = Path_Allocate(NULL);
Settings2.DefaultDirectory = Path_Allocate(NULL);
Settings2.FileBrowserPath = Path_Allocate(NULL);
Settings2.GrepWinPath = Path_Allocate(NULL);
Settings2.AdministrationTool = Path_Allocate(NULL);
Settings2.WebTemplate1 = StrgCreate(NULL);
Settings2.WebTemplate2 = StrgCreate(NULL);
Settings2.HyperlinkShellExURLWithApp = StrgCreate(NULL);
Settings2.HyperlinkShellExURLCmdLnArgs = StrgCreate(NULL);
Settings2.FileDlgFilters = StrgCreate(NULL);
FocusedView.HideNonMatchedLines = false;
FocusedView.CodeFoldingAvailable = false;
FocusedView.ShowCodeFolding = true;
FileWatching.FileWatchingMode = FWM_DONT_CARE;
FileWatching.MonitoringLog = false;
Paths.CurrentFile = Path_Allocate(NULL);
Paths.ModuleDirectory = Path_Allocate(NULL);
Paths.WorkingDirectory = Path_Allocate(NULL);
Paths.IniFile = Path_Allocate(NULL);
Paths.IniFileDefault = Path_Allocate(NULL);
// --- unstructured globals ---
g_tchToolbarBitmap = Path_Allocate(NULL);
g_tchToolbarBitmapHot = Path_Allocate(NULL);
g_tchToolbarBitmapDisabled = Path_Allocate(NULL);
// --- dynamicly created globals ---
WCHAR tchIndex[16] = { L'\0' };
StringCchPrintf(tchIndex, COUNTOF(tchIndex), L"%i", s_tbbMainWnd[0].iBitmap + 1);
StringCchCopy(TBBUTTON_DEFAULT_IDS, COUNTOF(TBBUTTON_DEFAULT_IDS), tchIndex);
assert(TBBUTTON_LAST_DEFAULT <= COUNTOF(s_tbbMainWnd));
for (int i = 1; i < TBBUTTON_LAST_DEFAULT; ++i) {
if (s_tbbMainWnd[i].idCommand) {
StringCchPrintf(tchIndex, COUNTOF(tchIndex), L" %i", s_tbbMainWnd[i].iBitmap + 1);
StringCchCat(TBBUTTON_DEFAULT_IDS, COUNTOF(TBBUTTON_DEFAULT_IDS), tchIndex);
} else {
StringCchCat(TBBUTTON_DEFAULT_IDS, COUNTOF(TBBUTTON_DEFAULT_IDS), L" 0");
}
}
// --- static locals ---
s_hpthRelaunchElevatedFile = Path_Allocate(NULL);
ThemesItems_Init();
s_hstrCurrentFindPattern = StrgCreate(NULL);
s_pthArgFilePath = Path_Allocate(NULL);
s_pthCheckFilePath = Path_Allocate(NULL);
// don't allow empty extensions settings => use default ext
Style_InitFileExtensions();
}
//=============================================================================
//
// _CleanUpResources()
//
static _invalid_parameter_handler _hOldInvalidParamHandler = NULL;
static void _CleanUpResources(const HWND hwnd, bool bIsInitialized)
{
if (hwnd) {
KillTimer(hwnd, IDT_TIMER_MRKALL);
}
if (Globals.pStdDarkModeIniStyles) {
FreeMem(Globals.pStdDarkModeIniStyles);
Globals.pStdDarkModeIniStyles = NULL;
}
CmdMessageQueue_t* pmqc = NULL;
CmdMessageQueue_t* dummy;
DL_FOREACH_SAFE(MessageQueue, pmqc, dummy) {
DL_DELETE(MessageQueue, pmqc);
FreeMem(pmqc);
}
if (UndoRedoSelectionUTArray != NULL) {
utarray_free(UndoRedoSelectionUTArray);
UndoRedoSelectionUTArray = NULL;
}
if (IS_VALID_HANDLE(s_FileChgObsvrData.hEventFileChanged)) {
CloseHandle(s_FileChgObsvrData.hEventFileChanged);
s_FileChgObsvrData.hEventFileChanged = INVALID_HANDLE_VALUE;
}
if (IS_VALID_HANDLE(s_FileChgObsvrData.hEventFileDeleted)) {
CloseHandle(s_FileChgObsvrData.hEventFileDeleted);
s_FileChgObsvrData.hEventFileDeleted = INVALID_HANDLE_VALUE;
}
BackgroundWorker_Destroy(&(s_FileChgObsvrData.worker));
// ---------------------------------------------
if (s_SelectionBuffer) {
FreeMem(s_SelectionBuffer);
s_SelectionBuffer = NULL;
}
// ---------------------------------------------
// Save Settings should be done elsewhere before
// ---------------------------------------------
if (Globals.hMainMenu) {
DestroyMenu(Globals.hMainMenu);
}
#if defined(HAVE_DYN_LOAD_LIBS_MUI_LNGS)
FreeLanguageResources();
#endif
CleanupDlgResources();
Scintilla_ReleaseResources();
if (bIsInitialized) {
//~UnregisterClass(s_ToolbarWndClassName, Globals.hInstance);
UnregisterClass(s_wchWndClass, Globals.hInstance);
}
ReleaseDarkMode();
// --- free allocated memory ---
if (s_lpOrigFileArg) {
FreeMem(s_lpOrigFileArg);
s_lpOrigFileArg = NULL;
}
MRU_Destroy(Globals.pFileMRU);
MRU_Destroy(Globals.pMRUfind);
MRU_Destroy(Globals.pMRUreplace);
StrgDestroy(Settings2.FileDlgFilters);
StrgDestroy(Settings2.HyperlinkShellExURLCmdLnArgs);
StrgDestroy(Settings2.HyperlinkShellExURLWithApp);
StrgDestroy(Settings2.WebTemplate2);
StrgDestroy(Settings2.WebTemplate1);
Path_Release(Settings2.AdministrationTool);
Path_Release(Settings2.GrepWinPath);
Path_Release(Settings2.FileBrowserPath);
Path_Release(Settings2.DefaultDirectory);
Path_Release(Settings.FavoritesDir);
Path_Release(Defaults.FavoritesDir);
Path_Release(Settings.OpenWithDir);
Path_Release(Defaults.OpenWithDir);
ReleaseEFR(&(Defaults.EFR_Data));
ReleaseEFR(&(Settings.EFR_Data));