-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPluginDefinition.cpp
1288 lines (1053 loc) · 28 KB
/
PluginDefinition.cpp
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
#include "PluginDefinition.h"
#include "menuCmdID.h"
#include <stdlib.h>
#include <time.h>
#include <shlwapi.h>
#include <shlobj.h>
#define FIND_BEFORE 0x0
#define FIND_AFTER 0x1
#define CLOSING 0x0
#define OPENING 0x2
#define MAX_LANG_LINE_SIZE 512
#define MAX_COMMENT_TAG_SIZE 32
struct Selection
{
ui64 beg;
ui64 end;
ui64 idx;
};
struct Lang
{
const char *n;
char c[MAX_COMMENT_TAG_SIZE];
char bcb[MAX_COMMENT_TAG_SIZE];
char bce[MAX_COMMENT_TAG_SIZE];
};
struct UserLang
{
char *n;
char c[MAX_COMMENT_TAG_SIZE];
char bcb[MAX_COMMENT_TAG_SIZE];
char bce[MAX_COMMENT_TAG_SIZE];
};
ui64 ulangs_s;
UserLang *user_langs;
ui64 langs_s = 87;
Lang langs[] = {
{"normal", ">>> ", "[", "]"} , // Coz, why not?
{"php", "", "", ""} ,
{"c", "", "", ""} ,
{"cpp", "", "", ""} ,
{"cs", "", "", ""} ,
{"objc", "", "", ""} ,
{"java", "", "", ""} ,
{"rc", "", "", ""} ,
{"html", "", "", ""} ,
{"xml", "", "", ""} ,
{"makefile", "", "", ""} ,
{"pascal", "", "", ""} ,
{"batch", "", "", ""} ,
{"ini", "", "", ""} ,
{"nfo", "", "", ""} , // MSDOS Style/ASCII Art
{"L_USER", "", "", ""} , // User Defined language file
{"asp", "", "", ""} ,
{"sql", "", "", ""} ,
{"vb", "", "", ""} ,
{"L_JS", "", "", ""} , // Deprecated
{"css", "", "", ""} ,
{"perl", "", "", ""} ,
{"python", "", "", ""} ,
{"lua", "", "", ""} ,
{"tex", "", "", ""} ,
{"fortran", "", "", ""} ,
{"bash", "", "", ""} ,
{"actionscript", "", "", ""} , // Flash ActionScript file
{"nsis", "", "", ""} ,
{"tcl", "", "", ""} ,
{"lisp", "", "", ""} ,
{"scheme", "", "", ""} ,
{"asm", "", "", ""} ,
{"diff", "", "", ""} ,
{"props", "", "", ""} ,
{"postscript", "", "", ""} ,
{"ruby", "", "", ""} ,
{"smalltalk", "", "", ""} ,
{"vhdl", "", "", ""} ,
{"kix", "", "", ""} ,
{"autoit", "", "", ""} ,
{"caml", "", "", ""} ,
{"ada", "", "", ""} ,
{"verilog", "", "", ""} ,
{"matlab", "", "", ""} ,
{"haskell", "", "", ""} ,
{"inno", "", "", ""} ,
{"searchResult", "", "", ""} ,
{"cmake", "", "", ""} ,
{"yaml", "", "", ""} ,
{"cobol", "", "", ""} ,
{"gui4cli", "", "", ""} ,
{"d", "", "", ""} ,
{"powershell", "", "", ""} ,
{"r", "", "", ""} ,
{"jsp", "", "", ""} ,
{"coffeescript", "", "", ""} ,
{"json", "", "", ""} ,
{"javascript", "", "", ""} ,
{"fortran77", "", "", ""} ,
{"baanc", "", "", ""} ,
{"srec", "", "", ""} ,
{"ihex", "", "", ""} ,
{"tehex", "", "", ""} ,
{"swift", "", "", ""} ,
{"asn1", "", "", ""} ,
{"avs", "", "", ""} ,
{"blitzbasic", "", "", ""} ,
{"purebasic", "", "", ""} ,
{"freebasic", "", "", ""} ,
{"csound", "", "", ""} ,
{"erlang", "", "", ""} ,
{"escript", "", "", ""} ,
{"forth", "", "", ""} ,
{"latex", "", "", ""} ,
{"mmixal", "", "", ""} ,
{"nim", "", "", ""} ,
{"nncrontab", "", "", ""} ,
{"oscript", "", "", ""} ,
{"rebol", "", "", ""} ,
{"registry", "", "", ""} ,
{"rust", "", "", ""} ,
{"spice", "", "", ""} ,
{"txt2tags", "", "", ""} ,
{"visualprolog", "", "", ""} ,
{"typescript", "", "", ""} ,
{"L_EXTERNAL", "", "", ""} , // End of language enum
};
const TCHAR sectionName[] = TEXT("Insert Extension");
const TCHAR keyName[] = TEXT("doCloseTag");
const TCHAR configFileName[] = TEXT("pluginDemo.ini");
FuncItem funcItem[nbFunc];
NppData nppData; // The data of Notepad++ that you can use in your plugin commands
TCHAR iniFilePath[MAX_PATH];
bool doCloseTag = false;
HWND scintilla;
const char *line_comm;
const char *block_beg;
const char *block_end;
ui64 line_comm_s;
ui64 block_beg_s;
ui64 block_end_s;
Selection *selects;
ShortcutKey toggle_comm_sh_key;
i64 m2scintilla(UINT id, ui64 wp, ui64 lp);
i64 m2scintilla(UINT id, ui64 wp);
i64 m2scintilla(UINT id);
void updateScintillaHWND();
char *strrep(char *s, ui64 pos, ui64 n, char rep);
bool strCmpRng(const char *s0, const char *s1_beg, const char *s1_end);
bool fndValRng(const char *fnd, const char *start_range, const char **beg, const char **end);
bool fndValRngUser(const char *fnd, const char *start_range, const char **beg, const char **end);
void strCpyRng(char *dest, const char *s_beg, const char *s_end);
void htmlEntiti2chars(char *ent_s);
void extractUserLangInfo(const wchar_t *fn);
void extractULIdir(wchar_t *dir);
DWORD __declspec(nothrow) commDbLoadingThread(LPVOID lp);
ui64 fnd1stNonSp(ui64 line);
ui64 fnd1stNonSp(ui64 line, ui64 *h_sp);
bool isComment(ui64 pos);
bool isLineBeg(ui64 pos);
bool isLineEnd(ui64 pos);
bool isPosBegLine(ui64 pos, ui64 line);
bool isBlockComm(ui64 pos, ui64 mode);
void insertTabs(ui64 amount, ui64 pos);
bool fndInSpaceSepArr(const char *fnd, const char *arr);
void wcs2str(char *dest, const wchar_t *src);
i64 doToggleComment(ui64 sel_s, ui64 sel_e, ui64 sel_i);
i64 togSingSelLineComm(ui64 sel_s, ui64 sel_e, ui64 sel_i);
i64 togMultiSelLineComm(ui64 sel_s, ui64 sel_e, ui64 sel_i);
i64 togBlockComm(ui64 sel_s, ui64 sel_e, ui64 sel_i);
#pragma warning( suppress : 4100 )
void pluginInit(HANDLE hModule)
{
CreateThread(NULL, 0, commDbLoadingThread, NULL, 0, NULL);
}
void pluginCleanUp()
{
for(ui64 i = 0; i < ulangs_s; ++i)
{
VirtualFree(user_langs[i].n, 0, MEM_RELEASE);
}
if(user_langs != NULL)
{
VirtualFree(user_langs, 0, MEM_RELEASE);
}
}
void commandMenuInit()
{
toggle_comm_sh_key._isAlt = false;
toggle_comm_sh_key._isCtrl = true;
toggle_comm_sh_key._isShift = false;
toggle_comm_sh_key._key = 0x51; // VK_Q
setCommand(0, TEXT("Toggle comment"), toggleComment, &toggle_comm_sh_key, false);
setCommand(1, TEXT("Plug-in homepage"), visitHomepage, NULL, false);
}
bool setCommand(size_t index, const TCHAR *cmdName, PFUNCPLUGINCMD pFunc, ShortcutKey *sk, bool check0nInit)
{
if(index >= nbFunc)
return false;
if(!pFunc)
return false;
lstrcpy(funcItem[index]._itemName, cmdName);
funcItem[index]._pFunc = pFunc;
funcItem[index]._init2Check = check0nInit;
funcItem[index]._pShKey = sk;
return true;
}
void commandMenuCleanUp()
{
}
void toggleComment()
{
updateScintillaHWND();
ui32 lang_t = L_TEXT;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTLANGTYPE, 0, (LPARAM)&lang_t);
if(lang_t == L_USER)
{
static wchar_t fname[MAX_PATH];
SendMessage(nppData._nppHandle, NPPM_GETFILENAME, MAX_PATH, (LPARAM)fname);
wchar_t *ext = wcsrchr(fname, '.') + 1;
if(ext == nullptr)
{
return;
}
static char ext_str[MAX_PATH];
wcs2str(ext_str, ext);
bool ulang_not_found = true;
for(ui64 i = 0; i < ulangs_s; ++i)
{
if(fndInSpaceSepArr(ext_str, user_langs[i].n))
{
line_comm = user_langs[i].c;
block_beg = user_langs[i].bcb;
block_end = user_langs[i].bce;
ulang_not_found = false;
break;
}
}
if(ulang_not_found)
{
return;
}
}
else
{
line_comm = langs[lang_t].c;
block_beg = langs[lang_t].bcb;
block_end = langs[lang_t].bce;
}
line_comm_s = strlen(line_comm);
block_beg_s = strlen(block_beg);
block_end_s = strlen(block_end);
ui64 sel_n = m2scintilla(SCI_GETSELECTIONS); // There is always at least one selection
selects = (Selection *)VirtualAlloc(0, sel_n * sizeof(Selection), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
for(ui64 i = 0; i < sel_n; ++i)
{
selects[i].beg = m2scintilla(SCI_GETSELECTIONNSTART, i);
selects[i].end = m2scintilla(SCI_GETSELECTIONNEND, i);
selects[i].idx = i;
}
for(ui64 i = 0; i < sel_n; ++i) // Insertion sort
{
for(ui64 j = i+1; j < sel_n; ++j)
{
if(selects[j].beg < selects[i].beg)
{
Selection tmp = selects[i];
selects[i] = selects[j];
selects[j] = tmp;
}
}
}
m2scintilla(SCI_BEGINUNDOACTION); // UNDO =========================
i64 ch_ins = 0;
for(ui64 i = 0; i < sel_n; ++i)
{
ui64 ss = selects[i].beg + ch_ins;
ui64 se = selects[i].end + ch_ins;
ch_ins += doToggleComment(ss, se, i);
}
if(selects[0].end < selects[0].beg)
{
ui64 tmp = selects[0].end;
selects[0].end = selects[0].beg;
selects[0].beg = tmp;
}
m2scintilla(SCI_SETSELECTION, selects[0].end, selects[0].beg); // Good for 1st selection
for(ui64 i = 1; i < sel_n; ++i)
{
if(selects[i].end < selects[i].beg)
{
ui64 tmp = selects[i].end;
selects[i].end = selects[i].beg;
selects[i].beg = tmp;
}
m2scintilla(SCI_ADDSELECTION, selects[i].end, selects[i].beg);
}
m2scintilla(SCI_ENDUNDOACTION); // ==============================
VirtualFree(selects, 0, MEM_RELEASE);
}
void visitHomepage()
{
static const char *link = "https://github.com/ScienceDiscoverer/CommentToggler";
ShellExecuteA( // =============================================================================
NULL, // [I|O] Handle to parent window that will show error or UI messages
"open", // [I|O] Verb string -> open|edit|explore|find|open|print|runas|NULL(def)
link, // [I] File or object that the verb is beeing executed on
NULL, // [I|O] Cmd arguments to pass to the file, if it is exe file
NULL, // [I|O] Default working directory of the action NULL -> current dir
SW_SHOWNORMAL); // [I] Parameter that sets default nCmdShow status of the window
// ============================================================================================
}
i64 doToggleComment(ui64 sel_s, ui64 sel_e, ui64 sel_i)
{
if(line_comm_s != 0)
{
if(sel_s == sel_e) // No selection, single line mode
{
return togSingSelLineComm(sel_s, sel_e, sel_i);
}
if(block_beg_s != 0 && block_end_s != 0)
{
if(!isLineBeg(sel_s) || !isLineEnd(sel_e)) /* ~~~ Block comment mode ~~~ */
{
return togBlockComm(sel_s, sel_e, sel_i);
}
}
return togMultiSelLineComm(sel_s, sel_e, sel_i); // Multiline line comment mode
}
if(block_beg_s != 0 && block_end_s != 0) // No line comment tag found, try to use block mode
{
return togBlockComm(sel_s, sel_e, sel_i);
}
return 0;
}
i64 togSingSelLineComm(ui64 sel_s, ui64 sel_e, ui64 sel_i)
{
ui64 line = m2scintilla(SCI_LINEFROMPOSITION, sel_s);
ui64 non_sp = fnd1stNonSp(line);
ui64 abs_pos = NPOS; // Absolute position
if(non_sp == NPOS)
{
non_sp = sel_s;
}
else
{
ui64 line_beg = m2scintilla(SCI_POSITIONFROMLINE, line);
abs_pos = non_sp + line_beg;
}
if(isComment(abs_pos))
{
m2scintilla(SCI_DELETERANGE, abs_pos, line_comm_s);
sel_s -= line_comm_s;
sel_e -= line_comm_s;
selects[sel_i].beg = sel_s;
selects[sel_i].end = sel_e;
return line_comm_s * -1;
}
m2scintilla(SCI_INSERTTEXT, abs_pos, (ui64)line_comm);
sel_s += line_comm_s;
sel_e += line_comm_s;
selects[sel_i].beg = sel_s;
selects[sel_i].end = sel_e;
return line_comm_s;
}
i64 togMultiSelLineComm(ui64 sel_s, ui64 sel_e, ui64 sel_i)
{
i64 ch_changed = 0; // Total amount of characters inserted/removed
ui64 line0 = m2scintilla(SCI_LINEFROMPOSITION, sel_s);
ui64 line1 = m2scintilla(SCI_LINEFROMPOSITION, sel_e);
if(fnd1stNonSp(line0) == NPOS)
{
++line0;
}
if(isPosBegLine(sel_e, line1))
{
--line1;
}
ui64 min_indent = NPOS;
for(ui64 i = line0; i <= line1; ++i)
{
ui64 indent = fnd1stNonSp(i);
if(indent < min_indent)
{
min_indent = indent;
}
}
ui64 h_sp = 0;
ui64 line0_beg = m2scintilla(SCI_POSITIONFROMLINE, line0);
ui64 line0_abs_indent = fnd1stNonSp(line0, &h_sp) + line0_beg;
ui64 line0_abs_pos = line0_beg + min_indent;
bool to_uncomment = isComment(line0_abs_indent);
ui64 tot_l_com = 0, tot_l_uncom = 0; // Total commented/uncommented lines counters
if(to_uncomment)
{
m2scintilla(SCI_DELETERANGE, line0_abs_indent, line_comm_s);
ch_changed -= line_comm_s;
++tot_l_uncom;
}
else
{
m2scintilla(SCI_INSERTTEXT, line0_abs_pos, (ui64)line_comm);
ch_changed += line_comm_s;
++tot_l_com;
}
if(h_sp > 0 && sel_s > line0_abs_pos)
{
sel_s = to_uncomment ? line0_abs_indent : line0_abs_pos;
}
ui64 addt_ins = 0; // Additional inserts
for(ui64 i = line0+1; i <= line1; ++i)
{
ui64 line_beg = m2scintilla(SCI_POSITIONFROMLINE, i);
ui64 abs_pos = min_indent + line_beg; // Absolute position
ui64 non_sp = fnd1stNonSp(i, &h_sp);
if(non_sp == NPOS) // Line contains no non-space characters!
{
if(min_indent > h_sp)
{
ui64 tabs_need = min_indent - h_sp;
addt_ins += tabs_need;
insertTabs(tabs_need, line_beg);
ch_changed += tabs_need;
}
}
ui64 abs_indent = non_sp + line_beg;
if(to_uncomment)
{
if(!isComment(abs_indent))
{
continue;
}
m2scintilla(SCI_DELETERANGE, abs_indent, line_comm_s);
ch_changed -= line_comm_s;
++tot_l_uncom;
}
else
{
m2scintilla(SCI_INSERTTEXT, abs_pos, (ui64)line_comm);
ch_changed += line_comm_s;
++tot_l_com;
}
}
// Account for characters inserted/removed to maintain the same initial selection area
sel_e = to_uncomment ? sel_e - tot_l_uncom * line_comm_s : sel_e + tot_l_com * line_comm_s;
sel_e += addt_ins;
selects[sel_i].beg = sel_s;
selects[sel_i].end = sel_e;
return ch_changed;
}
i64 togBlockComm(ui64 sel_s, ui64 sel_e, ui64 sel_i)
{
i64 ch_changed = 0; // Total amount of characters inserted/removed
bool to_uncomment_beg = false, to_uncomment_end = false;
ui64 obc_bpos = sel_s, cbc_bpos = sel_e; // Block comment strings beginning positions
if(isBlockComm(sel_s, FIND_AFTER | OPENING))
{
to_uncomment_beg = true;
}
else
{
if(isBlockComm(sel_s, FIND_BEFORE | OPENING))
{
obc_bpos -= block_beg_s;
to_uncomment_beg = true;
}
}
if(isBlockComm(sel_e, FIND_BEFORE | CLOSING))
{
cbc_bpos -= block_end_s;
to_uncomment_end = true;
}
else
{
if(isBlockComm(sel_e, FIND_AFTER | CLOSING))
{
to_uncomment_end = true;
}
}
if(to_uncomment_beg && to_uncomment_end)
{
m2scintilla(SCI_DELETERANGE, obc_bpos, block_beg_s);
ch_changed -= block_beg_s;
cbc_bpos -= block_beg_s;
m2scintilla(SCI_DELETERANGE, cbc_bpos, block_end_s);
ch_changed -= block_end_s;
sel_s -= sel_s - obc_bpos;
sel_e -= block_beg_s;
sel_e -= sel_e - cbc_bpos;
}
else
{
m2scintilla(SCI_INSERTTEXT, obc_bpos, (ui64)block_beg);
ch_changed += block_beg_s;
cbc_bpos += block_beg_s;
m2scintilla(SCI_INSERTTEXT, cbc_bpos, (ui64)block_end);
ch_changed += block_end_s;
sel_s += block_beg_s;
sel_e += block_beg_s;
}
selects[sel_i].beg = sel_s;
selects[sel_i].end = sel_e;
return ch_changed;
}
i64 m2scintilla(UINT id, ui64 wp, ui64 lp)
{
return SendMessage(scintilla, id, (WPARAM)wp, (LPARAM)lp);
}
i64 m2scintilla(UINT id, ui64 wp)
{
return SendMessage(scintilla, id, (WPARAM)wp, NULL);
}
i64 m2scintilla(UINT id)
{
return SendMessage(scintilla, id, NULL, NULL);
}
void updateScintillaHWND() // Update the current scintilla
{
int scintilla_used = -1;
SendMessage(nppData._nppHandle, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)&scintilla_used);
if(scintilla_used == -1) // Scintilla 0 -> Main View in multivew (Left) | 1 -> Second view (Right)
{
return;
}
scintilla = (scintilla_used == 0) ? nppData._scintillaMainHandle : nppData._scintillaSecondHandle;
}
char *strrep(char *s, ui64 pos, ui64 n, char rep) // String Replace with Char
{
ui64 sz = strlen(s);
ui64 ns = sz - n + 1;
char *ipos = s + pos;
memcpy(ipos + 1, s + pos + n, sz - pos - n);
*ipos = rep;
s[ns] = 0;
return s;
}
bool strCmpRng(const char *s0, const char *s1_beg, const char *s1_end)
{
++s1_end;
while(*s0 != 0 && s1_beg != s1_end)
{
if(*s0 != *s1_beg)
{
return false;
}
++s0;
++s1_beg;
}
return *s0 == 0 && s1_beg == s1_end;
}
bool fndValRng(const char *fnd, const char *start_range, const char **beg, const char **end)
{
ui64 s = strlen(fnd);
const char *val_tag = strstr(start_range, fnd);
if(val_tag == nullptr)
{
return false;
}
*beg = val_tag + s;
*end = strchr(*beg, '"') - 1;
if(*end == val_tag + s - 1) // Tag is empty
{
return false;
}
return true;
}
bool fndValRngUser(const char *fnd, const char *start_range, const char **beg, const char **end)
{
ui64 s = strlen(fnd);
const char *val_tag = strstr(start_range, fnd);
if(val_tag == nullptr)
{
return false;
}
*beg = val_tag + s;
*end = strpbrk(*beg, " <") - 1;
if(*end == val_tag + s - 1) // Tag is empty
{
return false;
}
return true;
}
void strCpyRng(char *dest, const char *s_beg, const char *s_end)
{
ui64 s = s_end - s_beg + 1;
strncpy(dest, s_beg, s);
dest[s] = 0;
}
void htmlEntiti2chars(char *ent_s)
{
--ent_s;
while(*(++ent_s) != 0)
{
if(*ent_s == '&')
{
char *ent_end = strchr(ent_s, ';');
if(strCmpRng("&", ent_s, ent_end) || strCmpRng("&", ent_s, ent_end))
{
strrep(ent_s, 0, ent_end - ent_s + 1, '&');
}
if(strCmpRng("'", ent_s, ent_end) || strCmpRng("'", ent_s, ent_end))
{
strrep(ent_s, 0, ent_end - ent_s + 1, '\'');
}
if(strCmpRng(""", ent_s, ent_end) || strCmpRng(""", ent_s, ent_end))
{
strrep(ent_s, 0, ent_end - ent_s + 1, '"');
}
if(strCmpRng("<", ent_s, ent_end) || strCmpRng("<", ent_s, ent_end))
{
strrep(ent_s, 0, ent_end - ent_s + 1, '<');
}
if(strCmpRng(">", ent_s, ent_end) || strCmpRng(">", ent_s, ent_end))
{
strrep(ent_s, 0, ent_end - ent_s + 1, '>');
}
if(strCmpRng(" ", ent_s, ent_end) || strCmpRng(" ", ent_s, ent_end))
{
strrep(ent_s, 0, ent_end - ent_s + 1, ' ');
}
}
}
}
void extractUserLangInfo(const wchar_t *fn)
{
// Create or open File or Device =================================================================
HANDLE f = CreateFile(
fn, // [I] Name of the file or device to create/open
GENERIC_READ, // [I] Requested access GENERIC_READ|GENERIC_WRITE|0
FILE_SHARE_READ, // [I] Sharing mode FILE_SHARE_READ|WRITE|DELETE|0
NULL, // [I|O] SECURITY_ATTRIBUTES for file, handle inheritability
OPEN_EXISTING, // [I] Action to take if file/device exist or not
FILE_FLAG_SEQUENTIAL_SCAN, // [I] Attributes and flags for file/device
NULL); // [I|O] Handle to template file to copy attributes from
// ===============================================================================================
if(f == INVALID_HANDLE_VALUE)
{
return;
}
char line[MAX_LANG_LINE_SIZE];
line[MAX_LANG_LINE_SIZE-1] = 0;
ui64 li = 0;
bool ignore_cur_line = false;
DWORD br;
char ch = 0;
while(ReadFile(f, &ch, 1, &br, NULL) && br)
{
if(ignore_cur_line)
{
if(ch != '\n')
{
continue;
}
ignore_cur_line = false;
li = 0;
continue;
}
if(ch == '\n')
{
line[li] = '\n';
line[li+1] = 0;
li = 0;
char *lang_p = strstr(line, "<UserLang ");
if(lang_p == nullptr)
{
char *comm_p = strstr(line, "name=\"Comments\"");
if(comm_p == nullptr)
{
continue;
}
const char *beg = 0, *end = 0;
if(fndValRngUser("00", comm_p, &beg, &end))
{
strCpyRng(user_langs[ulangs_s].c, beg, end);
htmlEntiti2chars(user_langs[ulangs_s].c);
}
if(fndValRngUser("03", comm_p, &beg, &end))
{
strCpyRng(user_langs[ulangs_s].bcb, beg, end);
htmlEntiti2chars(user_langs[ulangs_s].bcb);
}
if(fndValRngUser("04", comm_p, &beg, &end))
{
strCpyRng(user_langs[ulangs_s].bce, beg, end);
htmlEntiti2chars(user_langs[ulangs_s].bce);
}
++ulangs_s;
continue;
}
// Skip User Langs without extentions defined
const char *ext_beg = 0, *ext_end = 0;
if(!fndValRng("ext=\"", lang_p, &ext_beg, &ext_end))
{
CloseHandle(f);
return;
}
ui64 ext_s = ext_end - ext_beg + 1;
user_langs[ulangs_s].n = (char *)VirtualAlloc(0, ext_s+1, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
memcpy(user_langs[ulangs_s].n, ext_beg, ext_s);
user_langs[ulangs_s].n[ext_s] = 0;
continue;
}
if(li == MAX_LANG_LINE_SIZE-1)
{
ignore_cur_line = true;
continue;
}
line[li] = ch;
++li;
}
CloseHandle(f);
}
void extractULIdir(wchar_t *dir)
{
static wchar_t find_file_querry[MAX_PATH];
wcscpy(find_file_querry, dir);
wcscat(find_file_querry, L"*.xml");
ui64 usr_dl_s = wcslen(dir);
WIN32_FIND_DATA fd;
HANDLE f = FindFirstFile(find_file_querry, &fd);
if(f == INVALID_HANDLE_VALUE)
{
return;
}
do
{
if(wcscmp(fd.cFileName, L".") == 0 || wcscmp(fd.cFileName, L"..") == 0)
{
continue;
}
wcscpy(dir + usr_dl_s, fd.cFileName);
extractUserLangInfo(dir);
} while(FindNextFile(f, &fd));
FindClose(f);
}
#pragma warning( suppress : 4100 )
DWORD __declspec(nothrow) commDbLoadingThread(LPVOID lp)
{
wchar_t *app_data = NULL;
SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &app_data);
wchar_t npp_appdata_lang[MAX_PATH];
wcscpy(npp_appdata_lang, app_data);
wcscat(npp_appdata_lang, L"\\Notepad++\\langs.xml");
// Create or open File or Device =================================================================
HANDLE f = CreateFile(
npp_appdata_lang, // [I] Name of the file or device to create/open
GENERIC_READ, // [I] Requested access GENERIC_READ|GENERIC_WRITE|0
FILE_SHARE_READ, // [I] Sharing mode FILE_SHARE_READ|WRITE|DELETE|0
NULL, // [I|O] SECURITY_ATTRIBUTES for file, handle inheritability
OPEN_EXISTING, // [I] Action to take if file/device exist or not
FILE_FLAG_SEQUENTIAL_SCAN, // [I] Attributes and flags for file/device
NULL); // [I|O] Handle to template file to copy attributes from
// ===============================================================================================
if(f == INVALID_HANDLE_VALUE)
{
return 1;
}
char line[MAX_LANG_LINE_SIZE];
line[MAX_LANG_LINE_SIZE-1] = 0;
ui64 li = 0;
bool ignore_cur_line = false;
DWORD br;
char ch = 0;
while(ReadFile(f, &ch, 1, &br, NULL) && br)
{
if(ignore_cur_line)
{
if(ch != '\n')
{
continue;
}
ignore_cur_line = false;
li = 0;
continue;
}
if(ch == '\n')
{
line[li] = '\n';
line[li+1] = 0;
li = 0;
char *lang_p = strstr(line, "<Language ");
if(lang_p == nullptr)
{
continue;
}
const char *name_beg = 0, *name_end = 0;
fndValRng("name=\"", lang_p, &name_beg, &name_end);
for(ui64 i = 0; i < langs_s; ++i)
{
if(strCmpRng(langs[i].n, name_beg, name_end))
{
const char *beg = 0, *end = 0;
if(fndValRng("commentLine=\"", lang_p, &beg, &end))
{
strCpyRng(langs[i].c, beg, end);
htmlEntiti2chars(langs[i].c);
}
if(fndValRng("commentStart=\"", lang_p, &beg, &end))
{
strCpyRng(langs[i].bcb, beg, end);
htmlEntiti2chars(langs[i].bcb);
}
if(fndValRng("commentEnd=\"", lang_p, &beg, &end))
{
strCpyRng(langs[i].bce, beg, end);
htmlEntiti2chars(langs[i].bce);
}
break;
}
}
continue;
}
if(li == MAX_LANG_LINE_SIZE-1)
{
ignore_cur_line = true;
continue;
}
line[li] = ch;
++li;
}
CloseHandle(f);
if(strcmp(langs[L_BATCH].c, "REM") == 0) // Fix command line comment
{
strcat(langs[L_BATCH].c, " ");
}
// Load up all user defined languages available
ui64 ul_max_s = SendMessage(nppData._nppHandle, NPPM_GETNBUSERLANG, 0, 0);
user_langs = (UserLang *)VirtualAlloc(0, ul_max_s * sizeof(UserLang), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);