-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathalignWithSpaces.js
More file actions
1019 lines (955 loc) · 31.7 KB
/
alignWithSpaces.js
File metadata and controls
1019 lines (955 loc) · 31.7 KB
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
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=13096#p13096
// https://infocatcher.ucoz.net/js/akelpad_scripts/alignWithSpaces.js
// https://github.com/Infocatcher/AkelPad_scripts/blob/master/alignWithSpaces.js
// (c) Infocatcher 2011, 2014
// Version: 0.2.2.1 - 2014-05-06
// Author: Infocatcher
//===================
//// Align selected code with spaces to user defined string
// Hotkeys:
// Enter - Ok
// Ctrl+Enter, Shift+Enter - Align
// Escape - Cancel
// F1 - Select next align option
// F2 - Select next spaces option
// Ctrl+Z - Undo
// Ctrl+Shift+Z - Redo
// Ctrl+C, Ctrl+Insert - Copy
// Ctrl+V, Shift+Insert - Paste
// Ctrl+X, Shift+Del - Cut
// Delete - Delete selection
// Ctrl+A - Select all
// Ctrl+S - Save file
// Argumens:
// -dialog=true - show dialog
// -sepAtStart=false - -> [separator][text]
// =true - [separator] -> [text]
// -minimize=0 - leave all spaces
// =1 - leave only one space (if exist)
// =2 - remove all spaces
// -sep="=" - don't ask separator
// -history=10 - set history length
// -saveOptions=0 - don't store options
// =1 - (default) save options after them usage
// =2 - save options on exit
// -savePosition=true - allow store last window position
// Usage:
// Call("Scripts::Main", 1, "alignWithSpaces.js")
// Call("Scripts::Main", 1, "alignWithSpaces.js", '-dialog=false -sep="=" -sepAtStart=true')
//===================
function _localize(s) {
var strings = {
"No text selected!": {
ru: "Отсутствует выделенный текст!"
},
"Only one line selected!": {
ru: "Выделена только одна строка!"
},
"Invalid regular expression!": {
ru: "Неверное регулярное выражение!"
},
"Separator “%S” not found!": {
ru: "Разделитель «%S» не найден!"
},
"Found only one separator “%S”!": {
ru: "Найден только один разделитель «%S»!"
},
"&Separator:": {
ru: "&Разделитель:"
},
"&Alignement": {
ru: "&Выравнивание"
},
"-> [separator][text]": {
ru: "-> [разделитель][текст]"
},
"[separator] -> [text]": {
ru: "[разделитель] -> [текст]"
},
"&Spaces": {
ru: "&Пробелы"
},
"Leave all": {
ru: "Оставить все"
},
"Leave only one": {
ru: "Оставить только один"
},
"Remove all": {
ru: "Удалить все"
},
"OK": {
ru: "ОК"
},
"Align": {
ru: "Выровнять"
},
"Cancel": {
ru: "Отмена"
}
};
var lng = "en";
switch(AkelPad.GetLangId(1 /*LANGID_PRIMARY*/)) {
case 0x19: lng = "ru";
}
_localize = function(s) {
return strings[s] && strings[s][lng] || s;
};
return _localize(s);
}
// Read arguments:
// getArg(argName, defaultValue)
var dialog = getArg("dialog", true);
var sepAtStart = getArg("sepAtStart", false);
var minimize = getArg("minimize", 0);
var separator = getArg("sep");
var historyLength = getArg("history", 10);
var saveOptions = getArg("saveOptions", 1);
var savePosition = getArg("savePosition", true);
var hMainWnd = AkelPad.GetMainWnd();
var hWndEdit = AkelPad.GetEditWnd();
var oSys = AkelPad.SystemFunction();
var oSet = AkelPad.ScriptSettings();
var dialogTitle = WScript.ScriptName.replace(/^[!-\-_]+/, "");
dialogTitle = dialogTitle.charAt(0).toUpperCase() + dialogTitle.substr(1);
if(hMainWnd) {
if(dialog || !separator)
alignWithSpacesDialog();
else
alignWithSpaces(separator);
}
function alignWithSpaces(sep, hWnd) {
if(AkelPad.GetEditReadOnly(hWndEdit))
return false;
var sel = AkelPad.GetSelText();
if(!sel) {
AkelPad.MessageBox(hWnd || hMainWnd, _localize("No text selected!"), dialogTitle, 48 /*MB_ICONEXCLAMATION*/);
return false;
}
var lines = sel.split("\r");
var linesCnt = lines.length;
if(linesCnt == 1) {
AkelPad.MessageBox(hWnd || hMainWnd, _localize("Only one line selected!"), dialogTitle, 48 /*MB_ICONEXCLAMATION*/);
return false;
}
if(!sep) {
if(!hWnd)
alignWithSpacesDialog();
return false;
}
var pattern;
var isRegExp = false;
if(/^\/(.+)\/(i?)$/.test(sep)) { // /RegExp/ and /RegExp/i
try {
pattern = new RegExp(RegExp.$1, RegExp.$2);
isRegExp = true;
}
catch(e) {
AkelPad.MessageBox(
hWnd || hMainWnd,
_localize("Invalid regular expression!")
+ "\n" + e.name + "\n" + e.message,
dialogTitle,
16 /*MB_ICONERROR*/
);
if(!hWnd) {
separator = sep;
alignWithSpacesDialog();
}
return false;
}
}
//else if(/^("|')(.+)\1$/.test(sep))
else {
pattern = new RegExp(escapeRegExp(sep));
}
var maxPos = -1;
var indexes = [];
var startOffset = 0;
var found = 0;
for(var i = 0; i < linesCnt; ++i) {
var line = lines[i];
var indx = line.search(pattern);
indexes[i] = indx;
if(indx == -1)
continue;
++found;
//~ todo: calculate real tab width
if(sepAtStart) {
indx += RegExp.lastMatch.length;
var lineEnd = line.substr(indx);
if(minimize) {
var lineEnd = lineEnd.replace(/^[ \t]+/, minimize == 1 ? " " : "");
line = lines[i] = line.substr(0, indx) + lineEnd;
}
indx += lineEnd.match(/^[ \t]*/)[0].length;
indexes[i] = indx;
}
else if(minimize) {
var lineStart = line.substr(0, indx);
var lineEnd = line.substr(indx);
var lineStartNew = lineStart.replace(/[ \t]+$/, minimize == 1 ? " " : "");
indx = indexes[i] = lineStartNew.length;
line = lines[i] = lineStartNew + lineEnd;
}
if(i == 0 && !AkelPad.SendMessage(hWndEdit, 3127 /*AEM_GETCOLUMNSEL*/, 0, 0)) {
var ss = AkelPad.GetSelStart();
var lineStart = getOffset(hWndEdit, 18 /*AEGI_WRAPLINEBEGIN*/, ss);
startOffset = ss - lineStart;
indx += startOffset;
}
if(indx > maxPos)
maxPos = indx;
}
if(found <= 1) {
AkelPad.MessageBox(
hWnd || hMainWnd,
_localize(found ? "Found only one separator “%S”!" : "Separator “%S” not found!")
.replace("%S", isRegExp ? pattern : sep),
dialogTitle,
48 /*MB_ICONEXCLAMATION*/
);
if(!hWnd) {
separator = sep;
alignWithSpacesDialog();
}
return false;
}
for(var i = 0; i < linesCnt; ++i) {
var line = lines[i];
var indx = indexes[i];
if(indx == -1/* || indx == maxPos*/)
continue;
var spaces = new Array(maxPos - indx - (i == 0 ? startOffset : 0) + 1).join(" ");
var lineStart = line.substr(0, indx);
lineStart = lineStart.replace(/[\t ]*$/, "") + new Array(RegExp.lastMatch.length + 1).join(" ");
var lineEnd = line.substr(indx);
lineEnd = lineEnd.replace(/^[\t ]*/, "") + new Array(RegExp.lastMatch.length + 1).join(" ");
lines[i] = lineStart + spaces + lineEnd;
}
var res = lines.join("\r");
if(res != sel)
insertNoScroll(res);
return true;
}
function alignWithSpacesDialog(modal) {
var hInstanceDLL = AkelPad.GetInstanceDll();
var dialogClass = "AkelPad::Scripts::" + WScript.ScriptName + "::" + oSys.Call("kernel32::GetCurrentProcessId");
var hWndDialog = oSys.Call("user32::FindWindowEx" + _TCHAR, 0, 0, dialogClass, 0);
if(hWndDialog) {
if(oSys.Call("user32::IsIconic", hWndDialog))
oSys.Call("user32::ShowWindow", hWndDialog, 9 /*SW_RESTORE*/);
AkelPad.SendMessage(hWndDialog, 7 /*WM_SETFOCUS*/, 0, 0);
return;
}
if(
!AkelPad.WindowRegisterClass(dialogClass)
&& ( // Previous script instance crashed
!AkelPad.WindowUnregisterClass(dialogClass)
|| !AkelPad.WindowRegisterClass(dialogClass)
)
)
return;
var optionsUsed = false;
var lastSeps = [];
var savedSepsCount = 0;
var dlgX, dlgY;
if((saveOptions || savePosition) && oSet.Begin(WScript.ScriptBaseName, 0x1 /*POB_READ*/)) {
if(saveOptions) {
for(var i = 0; ; ++i) {
var s = oSet.Read("lastSeparator" + i, 3 /*PO_STRING*/, "");
if(!s)
break;
if(i < historyLength)
lastSeps[lastSeps.length] = s;
}
savedSepsCount = i;
if(getArg("sepAtStart") === undefined)
sepAtStart = oSet.Read("sepAtStart", 1 /*PO_DWORD*/) || 0;
if(getArg("minimize") === undefined)
minimize = oSet.Read("minimize", 1 /*PO_DWORD*/) || 0;
}
if(savePosition) {
dlgX = oSet.Read("windowLeft", 1 /*PO_DWORD*/);
dlgY = oSet.Read("windowTop", 1 /*PO_DWORD*/);
}
oSet.End();
}
function saveSettings(rewrite) {
if(!saveOptions && !savePosition)
return;
if(!oSet.Begin(WScript.ScriptBaseName, 0x2 /*POB_SAVE*/))
return;
if(saveOptions && (saveOptions == 2 || optionsUsed)) {
if(readControlsState()) {
oSet.Write("sepAtStart", 1 /*PO_DWORD*/, sepAtStart);
oSet.Write("minimize", 1 /*PO_DWORD*/, minimize);
}
var histLen = Math.min(historyLength, lastSeps.length);
for(var i = 0, l = Math.max(historyLength, savedSepsCount); i < l; ++i) {
if(i < histLen)
oSet.Write("lastSeparator" + i, 3 /*PO_STRING*/, lastSeps[i]);
else
oSet.Delete("lastSeparator" + i);
}
}
if(savePosition && !oSys.Call("user32::IsIconic", hWndDialog)) {
var rcWnd = getWindowRect(hWndDialog);
if(rcWnd) {
oSet.Write("windowLeft", 1 /*PO_DWORD*/, rcWnd.left);
oSet.Write("windowTop", 1 /*PO_DWORD*/, rcWnd.top);
}
}
oSet.End();
}
var IDC_STATIC = -1;
var IDC_COMBOBOX_LABEL = 1000;
var IDC_COMBOBOX = 1001;
var IDC_SEP_AT_END = 1002;
var IDC_SEP_AT_START = 1003;
var IDC_SP_LEAVE = 1004;
var IDC_SP_LEAVE_ONE = 1005;
var IDC_SP_REMOVE = 1006;
var IDC_OK = 1007;
var IDC_ALIGN = 1008;
var IDC_CANCEL = 1009;
var hWndStatic;
var hWndComboboxLabel, hWndCombobox;
var hWndSepAtEnd, hWndSepAtStart;
var hWndSpLeave, hWndSpLeaveOne, hWndSpRemove;
var hWndOK, hWndAlign, hWndCancel;
var scale = new Scale(0, hMainWnd);
var sizeNonClientX = oSys.Call("user32::GetSystemMetrics", 7 /*SM_CXFIXEDFRAME*/)*2;
var sizeNonClientY = oSys.Call("user32::GetSystemMetrics", 8 /*SM_CYFIXEDFRAME*/)*2
+ oSys.Call("user32::GetSystemMetrics", 4 /*SM_CYCAPTION*/);
// Create dialog
hWndDialog = oSys.Call(
"user32::CreateWindowEx" + _TCHAR,
0, //dwExStyle
dialogClass, //lpClassName
0, //lpWindowName
0x90CA0000, //WS_VISIBLE|WS_POPUP|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX
scale.x(0), //x
scale.y(0), //y
scale.x(446) + sizeNonClientX, //nWidth
scale.y(122) + sizeNonClientY, //nHeight
hMainWnd, //hWndParent
0, //ID
hInstanceDLL, //hInstance
dialogCallback //Script function callback. To use it class must be registered by WindowRegisterClass.
);
if(!hWndDialog)
return;
function dialogCallback(hWnd, uMsg, wParam, lParam) {
switch(uMsg) {
case 1: //WM_CREATE
var hGuiFont = oSys.Call("gdi32::GetStockObject", 17 /*DEFAULT_GUI_FONT*/);
function setWindowFontAndText(hWnd, hFont, pText) {
AkelPad.SendMessage(hWnd, 48 /*WM_SETFONT*/, hFont, true);
windowText(hWnd, pText);
}
// Dialog caption
oSys.Call("user32::SetWindowText" + _TCHAR, hWnd, dialogTitle);
// Static window: combobox label
hWndComboboxLabel = createWindowEx(
0, //dwExStyle
"STATIC", //lpClassName
0, //lpWindowName
0x50000100, //WS_VISIBLE|WS_CHILD|SS_NOTIFY
12, //x
15, //y
78, //nWidth
13, //nHeight
hWnd, //hWndParent
IDC_COMBOBOX_LABEL, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndComboboxLabel, hGuiFont, _localize("&Separator:"));
hWndCombobox = createWindowEx(
0, //dwExStyle
"COMBOBOX", //lpClassName
0, //lpWindowName
0x50210042, //WS_VISIBLE|WS_CHILD|WS_TABSTOP|WS_VSCROLL|CBS_DROPDOWN|CBS_AUTOHSCROLL
90, //x
12, //y
254, //nWidth
160, //nHeight
hWnd, //hWndParent
IDC_COMBOBOX, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
for(var i = 0, l = lastSeps.length; i < l; ++i) {
//AkelPad.MemCopy(lpBuffer, lastSeps[i], _TSTR);
//AkelPad.SendMessage(hWndCombobox, 0x143 /*CB_ADDSTRING*/, 0, lpBuffer);
oSys.Call("user32::SendMessage" + _TCHAR, hWndCombobox, 0x143 /*CB_ADDSTRING*/, 0, lastSeps[i]);
}
setWindowFontAndText(hWndCombobox, hGuiFont, separator || "");
!separator && AkelPad.SendMessage(hWndCombobox, 0x14E /*CB_SETCURSEL*/, 0, 0);
// GroupBox alignement
hWndStatic = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50000007, //WS_VISIBLE|WS_CHILD|BS_GROUPBOX
12, //x
38, //y
160, //nWidth
56, //nHeight
hWnd, //hWndParent
IDC_STATIC, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndStatic, hGuiFont, _localize("&Alignement"));
// Radiobutton separator at end
hWndSepAtEnd = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50000004, //WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON
20, //x
54, //y
144, //nWidth
16, //nHeight
hWnd, //hWndParent
IDC_SEP_AT_END, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndSepAtEnd, hGuiFont, _localize("-> [separator][text]"));
checked(hWndSepAtEnd, !sepAtStart);
// Radiobutton separator at start
hWndSepAtStart = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50000004, //WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON
20, //x
72, //y
144, //nWidth
16, //nHeight
hWnd, //hWndParent
IDC_SEP_AT_START, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndSepAtStart, hGuiFont, _localize("[separator] -> [text]"));
checked(hWndSepAtStart, sepAtStart);
// GroupBox spaces
hWndStatic = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50000007, //WS_VISIBLE|WS_CHILD|BS_GROUPBOX
184, //x
38, //y
160, //nWidth
72, //nHeight
hWnd, //hWndParent
IDC_STATIC, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndStatic, hGuiFont, _localize("&Spaces"));
// Radiobutton leave all spaces
hWndSpLeave = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50000004, //WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON
192, //x
54, //y
144, //nWidth
16, //nHeight
hWnd, //hWndParent
IDC_SP_LEAVE, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndSpLeave, hGuiFont, _localize("Leave all"));
checked(hWndSpLeave, minimize == 0);
// Radiobutton leave only one space
hWndSpLeaveOne = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50000004, //WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON
192, //x
71, //y
144, //nWidth
16, //nHeight
hWnd, //hWndParent
IDC_SP_LEAVE_ONE, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndSpLeaveOne, hGuiFont, _localize("Leave only one"));
checked(hWndSpLeaveOne, minimize == 1);
// Radiobutton remove all spaces
hWndSpRemove = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50000004, //WS_VISIBLE|WS_CHILD|BS_RADIOBUTTON
192, //x
88, //y
144, //nWidth
16, //nHeight
hWnd, //hWndParent
IDC_SP_REMOVE, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndSpRemove, hGuiFont, _localize("Remove all"));
checked(hWndSpRemove, minimize == 2);
// OK button window
hWndOK = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50010001, //WS_VISIBLE|WS_CHILD|WS_TABSTOP|BS_DEFPUSHBUTTON
355, //x
11, //y
80, //nWidth
23, //nHeight
hWnd, //hWndParent
IDC_OK, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndOK, hGuiFont, _localize("OK"));
// Apply button window
hWndAlign = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50010000, //WS_VISIBLE|WS_CHILD|WS_TABSTOP
355, //x
38, //y
80, //nWidth
23, //nHeight
hWnd, //hWndParent
IDC_ALIGN, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndAlign, hGuiFont, _localize("Align"));
// Cancel button window
hWndCancel = createWindowEx(
0, //dwExStyle
"BUTTON", //lpClassName
0, //lpWindowName
0x50010000, //WS_VISIBLE|WS_CHILD|WS_TABSTOP
355, //x
65, //y
80, //nWidth
23, //nHeight
hWnd, //hWndParent
IDC_CANCEL, //ID
hInstanceDLL, //hInstance
0 //lpParam
);
setWindowFontAndText(hWndCancel, hGuiFont, _localize("Cancel"));
//centerWindow(hWnd, hMainWnd);
restoreWindowPosition(hWnd, hMainWnd);
break;
case 7: //WM_SETFOCUS
oSys.Call("user32::SetFocus", hWndCombobox);
break;
case 256: //WM_KEYDOWN
var ctrl = getKeyState(17 /*VK_CONTROL*/);
var shift = getKeyState(16 /*VK_SHIFT*/);
if(wParam == 27 /*VK_ESCAPE*/)
oSys.Call("user32::PostMessage" + _TCHAR, hWnd, 273 /*WM_COMMAND*/, IDC_CANCEL, 0);
else if(wParam == 13 /*VK_RETURN*/) {
if(ctrl || shift) // Ctrl+Enter, Shift+Enter
oSys.Call("user32::PostMessage" + _TCHAR, hWnd, 273 /*WM_COMMAND*/, IDC_ALIGN, 0);
else // Enter
oSys.Call("user32::PostMessage" + _TCHAR, hWnd, 273 /*WM_COMMAND*/, IDC_OK, 0);
}
else if(wParam == 112 /*VK_F1*/) // F1
navigate(hWndSepAtEnd, hWndSepAtStart);
else if(wParam == 113 /*VK_F2*/) // F2
navigate(hWndSpLeave, hWndSpLeaveOne, hWndSpRemove);
else if(wParam == 90 /*Z*/) {
if(ctrl && shift) // Ctrl+Shift+Z
!comboboxFocused() && AkelPad.Command(4152); //IDM_EDIT_REDO
else if(ctrl) // Ctrl+Z
!comboboxFocused() && AkelPad.Command(4151); //IDM_EDIT_UNDO
}
else if(ctrl && wParam == 67 /*C*/ || ctrl && wParam == 45 /*VK_INSERT*/) // Ctrl+C, Ctrl+Insert
!comboboxFocused() && AkelPad.Command(4154); //IDM_EDIT_COPY
else if(ctrl && wParam == 86 /*V*/ || shift && wParam == 45 /*VK_INSERT*/) // Ctrl+V, Shift+Insert
!comboboxFocused() && noScroll(function() {
AkelPad.Command(4155); //IDM_EDIT_PASTE
});
else if(ctrl && wParam == 88 /*X*/ || shift && wParam == 46 /*VK_DELETE*/) // Ctrl+X, Shift+Del
!comboboxFocused() && AkelPad.Command(4153); //IDM_EDIT_CUT
else if(wParam == 46 /*VK_DELETE*/) // Delete
!comboboxFocused() && AkelPad.Command(4156); //IDM_EDIT_CLEAR
else if(ctrl && wParam == 65 /*A*/) // Ctrl+A
!comboboxFocused() && noScroll(function() {
AkelPad.Command(4157); //IDM_EDIT_SELECTALL
});
else if(ctrl && wParam == 83 /*S*/) // Ctrl+S
AkelPad.Command(4105); // IDM_FILE_SAVE
//else if(wParam != 16 /*VK_SHIFT*/ && wParam != 17 /*VK_CONTROL*/ && wParam != 18 /*VK_MENU*/)
// AkelPad.MessageBox(hWnd, wParam, dialogTitle, 0 /*MB_OK*/);
break;
case 273: //WM_COMMAND
var idc = wParam & 0xffff;
switch(idc) {
case IDC_OK:
case IDC_ALIGN:
if(!readControlsState())
break;
var curSep = windowText(hWndCombobox);
if(!curSep)
break;
hWndEdit = AkelPad.GetEditWnd();
if(!alignWithSpaces(curSep, hWnd))
break;
addToHistory(curSep);
optionsUsed = true;
if(idc == IDC_OK)
closeDialog();
break;
case IDC_CANCEL:
closeDialog();
break;
case IDC_COMBOBOX_LABEL:
oSys.Call("user32::SetFocus", hWndCombobox);
break;
case IDC_COMBOBOX:
var ok = false;
if(wParam >> 16 == 1 /*CBN_SELCHANGE*/) {
var i = AkelPad.SendMessage(hWndCombobox, 0x147 /*CB_GETCURSEL*/, 0, 0);
ok = AkelPad.SendMessage(hWndCombobox, 0x149 /*CB_GETLBTEXTLEN*/, i, 0) > 0;
}
else {
ok = oSys.Call("user32::GetWindowTextLength" + _TCHAR, hWndCombobox) > 0;
}
enabled(hWndAlign, ok);
enabled(hWndOK, ok);
break;
case IDC_SEP_AT_END:
case IDC_SEP_AT_START:
checked(hWndSepAtEnd, idc == IDC_SEP_AT_END);
checked(hWndSepAtStart, idc == IDC_SEP_AT_START);
if((wParam >> 16 & 0xFFFF) == 5 /*BN_DOUBLECLICKED*/)
oSys.Call("user32::PostMessage" + _TCHAR, hWnd, 273 /*WM_COMMAND*/, IDC_ALIGN, 0);
break;
case IDC_SP_LEAVE:
case IDC_SP_LEAVE_ONE:
case IDC_SP_REMOVE:
checked(hWndSpLeave, idc == IDC_SP_LEAVE);
checked(hWndSpLeaveOne, idc == IDC_SP_LEAVE_ONE);
checked(hWndSpRemove, idc == IDC_SP_REMOVE);
if((wParam >> 16 & 0xFFFF) == 5 /*BN_DOUBLECLICKED*/)
oSys.Call("user32::PostMessage" + _TCHAR, hWnd, 273 /*WM_COMMAND*/, IDC_ALIGN, 0);
}
break;
case 16: //WM_CLOSE
saveSettings();
modal && enabled(hMainWnd, true); // Enable main window
oSys.Call("user32::DestroyWindow", hWnd); // Destroy dialog
break;
case 2: //WM_DESTROY
oSys.Call("user32::PostQuitMessage", 0); // Exit message loop
}
return 0;
}
function comboboxFocused() {
//typedef struct tagCOMBOBOXINFO {
// DWORD cbSize;
// RECT rcItem;
// RECT rcButton;
// DWORD stateButton;
// HWND hwndCombo;
// HWND hwndItem;
// HWND hwndList;
//} COMBOBOXINFO, *PCOMBOBOXINFO, *LPCOMBOBOXINFO;
var sizeofCBI = 4 + 16*2 + 4*4;
var pcbi = AkelPad.MemAlloc(sizeofCBI);
if(!pcbi)
return false;
AkelPad.MemCopy(pcbi, sizeofCBI, 3 /*DT_DWORD*/);
if(!oSys.Call("user32::GetComboBoxInfo", hWndCombobox, pcbi))
return false;
var hWndComboboxEdit = AkelPad.MemRead(_PtrAdd(pcbi, 4 + 16*2 + 4*2), 3 /*DT_DWORD*/);
comboboxFocused = function() {
return oSys.Call("user32::GetFocus") == hWndComboboxEdit;
};
return comboboxFocused();
}
function readControlsState() {
if(checked(hWndSepAtStart))
sepAtStart = true;
else if(checked(hWndSepAtEnd))
sepAtStart = false;
else
return false;
if(checked(hWndSpLeave))
minimize = 0;
else if(checked(hWndSpLeaveOne))
minimize = 1;
else if(checked(hWndSpRemove))
minimize = 2;
else
return false;
return true;
}
function navigate() {
for(var i = 0, len = arguments.length; i < len; ++i) {
var hWndRadio = arguments[i];
if(!checked(hWndRadio))
continue;
checked(hWndRadio, false);
checked(arguments[i == len - 1 ? 0 : i + 1], true);
break;
}
}
function addToHistory(str) {
var histLen = lastSeps.length;
for(var i = 0; i < histLen;) {
if(lastSeps[i] != str) {
++i;
continue;
}
AkelPad.SendMessage(hWndCombobox, 0x144 /*CB_DELETESTRING*/, i, 0);
if(lastSeps.splice)
lastSeps.splice(i, 1);
else {
for(var j = i + 1; j < histLen; ++j)
lastSeps[j - 1] = lastSeps[j];
lastSeps.length--;
}
histLen--;
}
//AkelPad.SendMessage(hWndCombobox, 0x14A /*CB_INSERTSTRING*/, 0, lpBuffer);
oSys.Call("user32::SendMessage" + _TCHAR, hWndCombobox, 0x14A /*CB_INSERTSTRING*/, 0, str);
AkelPad.SendMessage(hWndCombobox, 0x14E /*CB_SETCURSEL*/, 0, 0);
if(lastSeps.unshift)
lastSeps.unshift(str);
else {
for(var j = histLen; j > 0; j--)
lastSeps[j] = lastSeps[j - 1];
lastSeps[0] = str;
}
if(lastSeps.length > historyLength) {
AkelPad.SendMessage(hWndCombobox, 0x144 /*CB_DELETESTRING*/, histLen - 1, 0);
lastSeps.length = historyLength;
}
}
function restoreWindowPosition(hWnd, hWndParent) {
if(dlgX == undefined || dlgY == undefined) {
centerWindow(hWnd, hWndParent);
return;
}
var rcWnd = getWindowRect(hWnd);
if(!rcWnd)
return;
var lpRect = AkelPad.MemAlloc(16); //sizeof(RECT)
if(!lpRect)
return;
AkelPad.MemCopy(_PtrAdd(lpRect, 0), dlgX, 3 /*DT_DWORD*/);
AkelPad.MemCopy(_PtrAdd(lpRect, 4), dlgY, 3 /*DT_DWORD*/);
AkelPad.MemCopy(_PtrAdd(lpRect, 8), dlgX + (rcWnd.right - rcWnd.left), 3 /*DT_DWORD*/);
AkelPad.MemCopy(_PtrAdd(lpRect, 12), dlgY + (rcWnd.top - rcWnd.bottom), 3 /*DT_DWORD*/);
var hMonitor = oSys.Call("user32::MonitorFromRect", lpRect, 0x2 /*MONITOR_DEFAULTTONEAREST*/);
if(hMonitor) {
//typedef struct tagMONITORINFO {
// DWORD cbSize;
// RECT rcMonitor;
// RECT rcWork;
// DWORD dwFlags;
//} MONITORINFO, *LPMONITORINFO;
var sizeofMonitorInfo = 4 + 16 + 16 + 4;
var lpMi = AkelPad.MemAlloc(sizeofMonitorInfo);
if(lpMi) {
AkelPad.MemCopy(lpMi, sizeofMonitorInfo, 3 /*DT_DWORD*/);
oSys.Call("user32::GetMonitorInfo" + _TCHAR, hMonitor, lpMi);
var rcWork = parseRect(_PtrAdd(lpMi, 4 + 16));
AkelPad.MemFree(lpMi);
}
}
else { //?
oSys.Call("user32::SystemParametersInfo" + _TCHAR, 48 /*SPI_GETWORKAREA*/, 0, lpRect, 0);
var rcWork = parseRect(lpRect);
}
AkelPad.MemFree(lpRect);
if(rcWork) {
var edge = Math.max(
16,
oSys.Call("user32::GetSystemMetrics", 8 /*SM_CYFIXEDFRAME*/)
+ oSys.Call("user32::GetSystemMetrics", 4 /*SM_CYCAPTION*/)
);
var minX = rcWork.left - (rcWnd.right - rcWnd.left) + edge;
var minY = rcWork.top;
var maxX = rcWork.right - edge;
var maxY = rcWork.bottom - edge;
dlgX = Math.max(minX, Math.min(maxX, dlgX));
dlgY = Math.max(minY, Math.min(maxY, dlgY));
}
oSys.Call("user32::SetWindowPos", hWnd, 0, dlgX, dlgY, 0, 0, 0x15 /*SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE*/);
}
function centerWindow(hWnd, hWndParent) {
var rcWnd = getWindowRect(hWnd);
var rcWndParent = getWindowRect(hWndParent || oSys.Call("user32::GetDesktopWindow"));
if(!rcWndParent || !rcWnd)
return;
var x = rcWndParent.left + ((rcWndParent.right - rcWndParent.left) / 2 - (rcWnd.right - rcWnd.left) / 2);
var y = rcWndParent.top + ((rcWndParent.bottom - rcWndParent.top) / 2 - (rcWnd.bottom - rcWnd.top) / 2);
oSys.Call("user32::SetWindowPos", hWnd, 0, x, y, 0, 0, 0x15 /*SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE*/);
}
function getWindowRect(hWnd, hWndParent) {
var lpRect = AkelPad.MemAlloc(16); //sizeof(RECT)
getRect: if(lpRect) {
if(!oSys.Call("user32::GetWindowRect", hWnd, lpRect))
break getRect;
if(hWndParent && !oSys.Call("user32::ScreenToClient", hWndParent, lpRect))
break getRect;
var rcWnd = parseRect(lpRect);
}
lpRect && AkelPad.MemFree(lpRect);
return rcWnd;
}
function parseRect(lpRect) {
return {
left: AkelPad.MemRead(_PtrAdd(lpRect, 0), 3 /*DT_DWORD*/),
top: AkelPad.MemRead(_PtrAdd(lpRect, 4), 3 /*DT_DWORD*/),
right: AkelPad.MemRead(_PtrAdd(lpRect, 8), 3 /*DT_DWORD*/),
bottom: AkelPad.MemRead(_PtrAdd(lpRect, 12), 3 /*DT_DWORD*/)
};
}
function windowText(hWnd, pText) {
if(arguments.length > 1)
return oSys.Call("user32::SetWindowText" + _TCHAR, hWnd, pText);
var len = oSys.Call("user32::GetWindowTextLength" + _TCHAR, hWnd);
var lpText = AkelPad.MemAlloc((len + 1)*_TSIZE);
if(!lpText)
return "";
oSys.Call("user32::GetWindowText" + _TCHAR, hWnd, lpText, len + 1);
pText = AkelPad.MemRead(lpText, _TSTR);
AkelPad.MemFree(lpText);
return pText;
}
function checked(hWnd, val) {
return arguments.length == 1
? AkelPad.SendMessage(hWnd, 240 /*BM_GETCHECK*/, 0, 0)
: AkelPad.SendMessage(hWnd, 241 /*BM_SETCHECK*/, val ? 1 /*BST_CHECKED*/ : 0, 0);
}
function enabled(hWnd, val) {
oSys.Call("user32::EnableWindow", hWnd, val);
}
function closeDialog() {
oSys.Call("user32::PostMessage" + _TCHAR, hWndDialog, 16 /*WM_CLOSE*/, 0, 0);
}
function getKeyState(key) {
return oSys.Call("user32::GetAsyncKeyState", key) & 0x8000; // Fix 4-byte result in AkelPad x64
}
function Scale(hDC, hWnd) {
var hNewDC = hDC || oSys.Call("user32::GetDC", hWnd);
if(hNewDC) {
this._x = oSys.Call("gdi32::GetDeviceCaps", hNewDC, 88 /*LOGPIXELSX*/);
this._y = oSys.Call("gdi32::GetDeviceCaps", hNewDC, 90 /*LOGPIXELSY*/);
//Align to 16 pixel
this._x += (16 - this._x % 16) % 16;
this._y += (16 - this._y % 16) % 16;
!hDC && oSys.Call("user32::ReleaseDC", hWnd, hNewDC);
this.x = function(x) {
return oSys.Call("kernel32::MulDiv", x, this._x, 96);
};
this.y = function(y) {
return oSys.Call("kernel32::MulDiv", y, this._y, 96);
};
}
else {
this.x = this.y = function(n) {
return n;
};
}
}
function createWindowEx(
dwExStyle, lpClassName, lpWindowName, dwStyle,
x, y, w, h,
hWndParent, id, hInstance, callback
) {
return oSys.Call(
"user32::CreateWindowEx" + _TCHAR,
dwExStyle,
lpClassName,
lpWindowName,
dwStyle,
scale.x(x),
scale.y(y),
scale.x(w),
scale.y(h),
hWndParent,
id,
hInstance,
callback || 0
);
}
modal && enabled(hMainWnd, false); // Disable main window, to make dialog modal
AkelPad.ScriptNoMutex(); // Allow other scripts running
AkelPad.WindowGetMessage(); // Message loop
AkelPad.WindowUnregisterClass(dialogClass);
}
function escapeRegExp(str) {
return str.replace(/[\\\/.^$+*?|()\[\]{}]/g, "\\$&");
}
function insertNoScroll(str, selectAll) {
noScroll(function() {
selectAll && AkelPad.SetSel(0, -1);
//var ss = AkelPad.GetSelStart();
AkelPad.ReplaceSel(str, true);
//if(ss != AkelPad.GetSelStart())
// AkelPad.SetSel(ss, ss + str.length);
});
}
function noScroll(func, hWndEdit) {
if(!hWndEdit)
hWndEdit = AkelPad.GetEditWnd();
var lpPoint = AkelPad.MemAlloc(8 /*sizeof(POINT)*/);
if(!lpPoint)
return;
setRedraw(hWndEdit, false);
AkelPad.SendMessage(hWndEdit, 1245 /*EM_GETSCROLLPOS*/, 0, lpPoint);
func();
AkelPad.SendMessage(hWndEdit, 1246 /*EM_SETSCROLLPOS*/, 0, lpPoint);
setRedraw(hWndEdit, true);
AkelPad.MemFree(lpPoint);
}
function setRedraw(hWnd, bRedraw) {
AkelPad.SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
bRedraw && oSys.Call("user32::InvalidateRect", hWnd, 0, true);
}
function getOffset(hWndEdit, nType /*AEGI_*/, nOffset) {
// Based on Instructor's code
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=11382#p11382
var lpIndex = AkelPad.MemAlloc(_X64 ? 24 : 12 /*sizeof(AECHARINDEX)*/);
if(!lpIndex)
return 0;
if(nOffset != -1)
AkelPad.SendMessage(hWndEdit, 3137 /*AEM_RICHOFFSETTOINDEX*/, nOffset, lpIndex);