-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathHiEdit.ahk
1245 lines (1053 loc) · 39.6 KB
/
HiEdit.ahk
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
/* Title: HiEdit
HiEdit is a multitabbed, ultra fast, large file edit control consuming very little memory.
It can display non-printable characters in a readable format and can be used for any general
purpose editing of text and birary files.
Irrespective of the file size, the memory used at file loading never exceeeds 1024K.
After the file is loaded, max memory used at any time is 128K.
(See HiEdit.png)
*/
/* Function: Add
Add control to the parent.
Parameters:
X..H - Position of the control.
Style - Space separated list of control styles, by default both scroll bars are visible. You can use numbers or style strings.
DllPath - Path of the control dll, by default control is searched in the current folder.
Styles:
HSCROLL, VSCROLL, TABBED, HILIGHT, TABBEDBTOP, TABBEDHRZSB, TABBEDBOTTOM
*/
HE_Add(HParent, X, Y, W, H, Style="", DllPath=""){
static WS_CLIPCHILDREN=0x2000000, WS_VISIBLE=0x10000000, WS_CHILD=0x40000000, MODULEID
static HSCROLL=0x8 ,VSCROLL=0x10, TABBED=4, HILIGHT=0x20, TABBEDBTOP=0x1, TABBEDHRZSB=0x2 ,TABBEDBOTTOM=0x4, SINGLELINE=0x40, FILECHANGEALERT=0x80
hStyle := 0
loop, parse, style, %A_Tab%%A_Space%
IfEqual, A_LoopField, , continue
else hStyle |= %A_LOOPFIELD%
if !MODULEID {
ifEqual, DllPath, ,SetEnv, DllPath, HiEdit.dll
MODULEID := 230909, DllCall("LoadLibrary", "str", DllPath)
}
hCtrl := DllCall("CreateWindowEx"
, "Uint", 0x200 ; WS_EX_CLIENTEDGE
, "str", "HiEdit" ; ClassName
, "str", "" ; WindowName
, "Uint", WS_CLIPCHILDREN | WS_CHILD | WS_VISIBLE | hStyle
, "int", X ; Left
, "int", Y ; Top
, "int", W ; Width
, "int", H ; Height
, "Uint", HParent ; hWndParent
, "Uint", MODULEID ; hMenu
, "Uint", 0 ; hInstance
, "Uint", 0, "Uint")
HE_SetTabsImageList(hCtrl)
return hCtrl
}
/*
Function: AutoIndent
Sets the autoindent state
Parameters:
pState - TRUE or FALSE
*/
HE_AutoIndent(hEdit, pState ) {
static HEM_AUTOINDENT := 2042 ;wParam=0, lParam=fAutoIndent:TRUE/FALSE
SendMessage, HEM_AUTOINDENT, 0, pState,, ahk_id %hEdit%
return errorlevel
}
/*
Function: CanPaste
Determines whether a HiEdit control can paste a specified clipboard format.
Parameters:
ClipboardFormat - The default is 0x1 (CF_TEXT).
Returns:
TRUE if the clipboard format can be pasted otherwise FALSE.
Remarks:
The HiEdit control supports the standard clipboard text formats --
CF_TEXT, CF_OEMTEXT, CF_UNICODETEXT (for Windows versions that support Unicode),
and CF_LOCALE (used by the OS to implicitly convert from CF_TEXT to CF_UNICODETEXT).
If any application copies text of any type to the clipboard, it will usually copy the text to
one (usually all, depending on the OS) of these clipboard formats.
For additional information on clipboard formats, see the following:
<http://msdn.microsoft.com/en-us/library/ms649013(VS.85).aspx>
Bugs:
Despite the HiEdit documentation, sending zero as the clipboard
format never returns a non-zero (can paste) value regardless of
the contents of the clipboard.
*/
HE_CanPaste(hEdit,ClipboardFormat=0x1) {
Static EM_CANPASTE:=1074 ;-- 1074=WM_USER+50
SendMessage EM_CANPASTE,ClipboardFormat,0,,ahk_id %hEdit%
return ErrorLevel
}
/*
Function: CanRedo
Returns TRUE if the HiEdit control can do Redo operation.
*/
HE_CanRedo(hEdit) {
Static EM_CANREDO=1109 ;-- 1109=WM_USER+85
SendMessage EM_CANREDO,,,,ahk_id %hEdit%
return ErrorLevel ? True : False
}
/*
Function: CanUndo
Returns TRUE if the HiEdit control can correctly do Undo operation.
*/
HE_CanUndo(hEdit) {
Static EM_CANUNDO=0xC6
SendMessage EM_CANUNDO,,,,ahk_id %hEdit%
return ErrorLevel
}
/*
Function: CloseFile
Close file or all files
Parameters:
idx - Index of the file to close. -2 to close ALL opened files, -1 to close current file (default)
*/
HE_CloseFile(hEdit, idx=-1){
static HEM_CLOSEFILE := 2026 ;wParam=0,
SendMessage, HEM_CLOSEFILE, 0, idx,, ahk_id %hEdit%
return errorlevel
}
/*
Function: Clear
Clear.
*/
HE_Clear(hEdit) {
Static WM_CLEAR:=0x303
SendMessage WM_CLEAR,,,,ahk_id %hEdit%
}
;{
;}
/*
Function: CharFromPos
Gets information about the character closest to a specified point in the client area of the Edit control.
Parameters:
X, Y - The coordinates of a point in the Edit control's client area relative to the upper-left corner of the client area.
Returns:
The character index of the specified point or the character index to
the last character if the given point is beyond the last character in the control.
*/
HE_CharFromPos(hEdit,X,Y) {
Static EM_CHARFROMPOS=0xD7
WinGetClass, cls, ahk_id %hEdit%
if cls in RICHEDIT50W
VarSetCapacity(POINTL, 8), lParam := &POINTL, NumPut(X, POINTL), NumPut(Y,POINTL)
else lParam := (Y<<16)|X
SendMessage EM_CHARFROMPOS,,lParam,,ahk_id %hEdit%
return ErrorLevel
}
/*
Function: ConvertCase
Convert case of selected text
Parameters:
case - Can be "upper", "lower", "toggle" (default), "capitalize".
Returns:
Returns TRUE if successful, FALSE otherwise
*/
HE_ConvertCase(hEdit, case="toggle") {
static HEM_CONVERTCASE=2046 ;EQU WM_USER+1022 ;wParam=CC_UPPERCASE/CC_LOWERCASE/CC_TOGGLECASE,lParam = -1 :Returns TRUE if successful/FALSE otherwise
static cc_upper=0, cc_lower=1, cc_toggle=2, cc_capitalize=3
SendMessage, HEM_CONVERTCASE, cc_%case%, -1,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: Copy
Copy.
*/
HE_Copy(hEdit) {
Static WM_COPY:=0x301
SendMessage WM_COPY,0,0,,ahk_id %hEdit%
}
/*
Function: Cut
Cut.
*/
HE_Cut(hEdit) {
Static WM_CUT:=0x300
SendMessage WM_CUT,,,,ahk_id %hEdit%
}
/*
Function: EmptyUndoBuffer
Resets the undo flag in the HiEdit control for the current file.
*/
HE_EmptyUndoBuffer(hEdit){
Static EM_EMPTYUNDOBUFFER:=0xCD
SendMessage EM_EMPTYUNDOBUFFER,0,0,,ahk_id %hEdit%
}
/*
Function: FindText
Find desired text in the control
Parameters:
sText - Text to be searched for.
cpMin - Start searching at this character position. By default 0.
cpMax - End searching at this character position. By default -1.
flags - Space separated combination of search flags: "WHOLEWORD" "MATCHCASE"
Returns:
The zero-based character position of the next match, or -1 if there are no more matches.
*/
HE_FindText(hEdit, sText, cpMin=0, cpMax=-1, flags="") {
static EM_FINDTEXT=1080,WHOLEWORD=2,MATCHCASE=4 ;WM_USER + 56
hFlags := 0
loop, parse, flags, %A_Tab%%A_Space%,%A_Space%%A_Tab%
if (A_LoopField != "")
hFlags |= %A_LOOPFIELD%
VarSetCapacity(FT, 12)
NumPut(cpMin, FT, 0)
NumPut(cpMax, FT, 4)
NumPut(&sText, FT, 8)
SendMessage, EM_FINDTEXT, hFlags, &FT,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: GetColors
Get the control colors.
Returns:
Colors in INI format. See <SetColors> for details.
*/
HE_GetColors(hEdit){
static HEM_GETCOLORS := 2038
static names := "Text,Back,SelText,ActSelBack,InSelBack,LineNumber,SelBarBack,NonPrintableBack,Number"
VarSetCapacity(COLORS, 48, 0)
SendMessage,HEM_GETCOLORS,0,&COLORS,,ahk_id %hEdit%
ifEqual,ErrorLevel,FAIL, return FAIL
fmt := A_FormatInteger
SetFormat, integer, hex
Loop, Parse, names, `,
res .= A_LoopField "=" NumGet(COLORS, 4*(A_Index-1)) "`n"
SetFormat, integer, %fmt%
return SubStr(res, 1, -1)
}
/*
Function: GetCurrentFile
Get the index of the current file.
*/
HE_GetCurrentFile(hEdit){
static HEM_GETCURRENTFILE := 2032 ;wParam=0, lParam = 0
SendMessage, HEM_GETCURRENTFILE, 0, 0,, ahk_id %hEdit%
return errorlevel
}
/*
Function: GetFileCount
Returns count of open files.
*/
HE_GetFileCount(hEdit){
static HEM_GETFILECOUNT := 2029 ;wParam=0, lParam=0
SendMessage, HEM_GETFILECOUNT, 0, 0,, ahk_id %hEdit%
return errorlevel
}
/*
Function: GetFileName
Get the file path.
Parameters:
idx - Index of the file. -1 to get file path of the current file (default)
Returns:
TRUE if successful, FALSE otherwise
*/
HE_GetFileName(hEdit, idx=-1){
static HEM_GETFILENAME := 2030 ;wParam = lpszFileName, lParam = -1 for current file or dwFileIndex :Returns TRUE if successful/FALSE otherwise
VarSetCapacity(fileName, 512)
SendMessage, HEM_GETFILENAME, &fileName, idx,, ahk_id %hEdit%
return fileName
}
/*
Function: GetFirstVisibleLine
Returns the zero-based index of the uppermost visible line.
*/
HE_GetFirstVisibleLine(hEdit){
static EM_GETFIRSTVISIBLELINE=206
SendMessage, EM_GETFIRSTVISIBLELINE, 0, 0,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: GetLastVisibleLine
Returns the zero-based line index of the last visible (including
partially displayed) line on the HiEdit control.
Remarks:
To calculate the total number of visible lines, use the following:
> HE_GetLastVisibleLine(hEdit) - HE_GetFirstVisibleLine(hEdit) + 1
*/
HE_GetLastVisibleLine(hEdit) {
HE_GetRect(hEdit,_,_,_,bottom)
return HE_LineFromChar(hEdit,HE_CharFromPos(hEdit,0,bottom))
}
/*
Function: GetLine
Get the text of the desired line from the control.
Parameters:
idx - Zero-based index of the line. -1 means current line.
Returns:
The return value is the text.
The return value is empty string if the line number specified by the line parameter is greater than the number of lines in the HiEdit control
*/
HE_GetLine(hEdit,idx=-1){
static EM_GETLINE=0xC4
if (idx=-1)
idx := HE_LineFromChar(hEdit,HE_LineIndex(hEdit))
len := HE_LineLength(hEdit,idx)
IfEqual,len,0,return
VarSetCapacity(txt,len=1 ? 2:len,0)
NumPut(len=1 ? 2:len,txt,0,"Short")
SendMessage EM_GETLINE,idx,&txt,,ahk_id %hEdit%
if ErrorLevel=FAIL ;-- Test remains as a fail-safe
{
Msgbox %A_ThisFunc% failed.
return
}
VarSetCapacity(txt,-1)
return len=1 ? SubStr(txt,1,1):txt
}
/*
Function: GetLineCount
Returns an integer specifying the number of lines in the HiEdit control.
If no text is in the HiEdit control, the return value is 1.
*/
HE_GetLineCount(hEdit){
static EM_GETLINECOUNT=186
SendMessage, EM_GETLINECOUNT, 0, 0,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: GetModify
Gets the state of the modification flag for the HiEdit control.
The flag indicates whether the contents of the control has been modified.
Parameters:
idx - Index of the file. -1 for the current file (default).
Returns:
TRUE if the content of HiEdit control has been modified, FALSE otherwise.
*/
HE_GetModify(hEdit, idx=-1){
Static EM_GETMODIFY:=0xB8
SendMessage EM_GETMODIFY,0,idx,,ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: GetRedoData
Returns redo type and/or data for desired redo level. The same rules as in <GetUndoData>.
*/
HE_GetRedoData(hEdit, level){
static HEM_GETREDODATA=2040 ;wParam=Undo level (1 based), lParam=lpUNDODATA :Returns type of undo (UNDONAMEID)
static UID_0="UNKNOWN",UID_1="TYPING",UID_2="DELETE",UID_3="DRAGDROP",UID_4="CUT",UID_5="PASTE",UID_6="SETTEXT",UID_7="REPLACESEL",UID_8="CLEAR",UID_9="BACKSPACE",UID_10="INDENT",UID_11="OUTDENT",UID_12="CODEPAGE",UID_13="CASE"
static size = 128
VarSetCapacity( RD, 8, 0), VarSetCapacity( buf, size ), NumPut(&buf, RD), NumPut(size, RD, 4)
SendMessage, HEM_GETREDODATA, level, &RD,, ahk_id %hEdit%
VarSetCapacity(buf, -1)
Return % buf
Return % UID_%ErrorLevel%
}
/*
Function: GetRect
Gets the formatting rectangle of the HiEdit control.
Parameters:
Left..Bottom - Output variables, can be omitted.
Returns:
Space separated rectangle.
*/
HE_GetRect(hEdit,ByRef Left="",ByRef Top="",ByRef Right="",ByRef Bottom="") {
static EM_GETRECT:=0xB2
VarSetCapacity(RECT,16)
SendMessage EM_GETRECT,0,&RECT,,ahk_id %hEdit%
Left :=NumGet(RECT, 0,"Int")
, Top :=NumGet(RECT, 4,"Int")
, Right :=NumGet(RECT, 8,"Int")
, Bottom:=NumGet(RECT,12,"Int")
return Left " " Top " " Right " " Bottom
}
/*
Function: GetSel
Get letfmost and/or rightmost character positions of the selection.
Parameters:
start_pos - Optional starting position of the selection.
end_pos - Optional ending position of the selection.
Returns:
Starting position.
*/
HE_GetSel(hEdit, ByRef start_pos="@",ByRef end_pos="@"){
static EM_GETSEL=176
VarSetCapacity(s, 4), VarSetCapacity(e, 4)
SendMessage, EM_GETSEL, &s, &e,, ahk_id %hEdit%
s := NumGet(s), e := NumGet(e)
if (start_pos != "@")
start_pos := s
if (end_pos != "@")
end_pos := e
Return s
}
/*
Function: GetSelText
Returns selected text.
*/
HE_GetSelText(hEdit){
static EM_GETSELTEXT = 1086 ;Returns: the number of characters copied, not including the terminating null character.
HE_GetSel(hEdit, s, e), VarSetCapacity(buf, e-s+2)
SendMessage, EM_GETSELTEXT, 0, &buf,, ahk_id %hEdit%
VarSetCapacity(buf, -1)
Return buf
}
/*
Function: GetTextLength
Returns the length of text, in characters.
*/
HE_GetTextLength(hEdit) {
static WM_GETTEXTLENGTH=14
SendMessage, WM_GETTEXTLENGTH, 0, 0,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: GetTextRange
Get range of characters from the control
Parameters:
min - Index of leftmost characther of the range. By default 0.
max - Index of rightmost characther of the range. -1 means last character in the control.
*/
HE_GetTextRange(hEdit, min=0, max=-1){
static EM_GETTEXTRANGE=1099 ;Returns: The number of characters copied, not including the terminating null character.
if (max=-1)
max := HE_GetTextLength(hEdit)
VarSetCapacity(buf, max-min+2)
VarSetCapacity(RNG, 12), NumPut(min, RNG), NumPut(max, RNG, 4), NumPut(&buf, RNG, 8)
SendMessage, EM_GETTEXTRANGE, 0, &RNG,, ahk_id %hEdit%
VarSetCapacity(buf, -1)
Return buf
}
/*
Function: GetUndoData
Returns undo type and/or data for desired undo level.
Parameters:
level - Undo level.
Types:
UNKNOWN - The type of undo action is unknown.
TYPING - Typing operation.
DELETE - Delete operation.
DRAGDROP - Drag-and-drop operation.
CUT - Cut operation.
PASTE - Paste operation.
SETTEXT - WM_SETTEXT message was used to set the control text
REPLACESEL - EM_REPLACESEL message was used to insert text
CLEAR - Delete selected text
BACKSPACE - Back Space Operation
INDENT - Increase Indent
OUTDENT - Decrease Indent
CODEPAGE - Convert codepage
CASE - Convert case
*/
HE_GetUndoData(hEdit, level){
static HEM_GETUNDODATA=2039 ;wParam=Undo level (1 based), lParam=lpUNDODATA :Returns type of undo (UNDONAMEID)
static UID_0="UNKNOWN",UID_1="TYPING",UID_2="DELETE",UID_3="DRAGDROP",UID_4="CUT",UID_5="PASTE",UID_6="SETTEXT",UID_7="REPLACESEL",UID_8="CLEAR",UID_9="BACKSPACE",UID_10="INDENT",UID_11="OUTDENT",UID_12="CODEPAGE",UID_13="CASE"
static size = 128
VarSetCapacity( UD, 8, 0), VarSetCapacity( buf, size ), NumPut(&buf, UD), NumPut(size, UD, 4)
SendMessage, HEM_GETUNDODATA, level, &UD,, ahk_id %hEdit%
VarSetCapacity(buf, -1)
Return % UID_%ErrorLevel%
}
/*
Function: LineFromChar
Returns line number of the line containing specific character index.
Parameters:
ich - The character index of the character contained in the line whose number is to be retrieved. If the ich parameter is -1, either the line number of the current line (the line containing the caret) is retrieved or, if there is a selection, the line number of the line containing the beginning of the selection is retrieved.
Returns:
The zero-based line number of the line containing the character index specified by ich.
*/
HE_LineFromChar(hEdit, ich) {
static EM_LINEFROMCHAR=201
SendMessage, EM_LINEFROMCHAR, ich, 0,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: LineIndex
Returns the character index of the line.
Parameters:
idx - Line number for which to retreive character index. -1 (default) means current line.
Returns:
The character index of the line specified in the idx parameter, or -1 if the specified line number is greater than the number of lines.
*/
HE_LineIndex(hedit, idx=-1) {
static EM_LINEINDEX=187
SendMessage, EM_LINEINDEX, idx, 0,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: LineLength
Returns the lenght of the line.
Parameters:
idx - Line number for which to retreive line length. -1 (default) means current line.
Returns:
the length, in characters, of the line
*/
HE_LineLength(hEdit, idx=-1) {
static EM_LINELENGTH=193
SendMessage, EM_LINELENGTH, He_LineIndex(hEdit, idx) , 0,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: LineNumbersBar
Sets the line numbers bar state and looks.
Parameters:
state - Can be "show", "hide", "automaxsize", "autosize"
linw - Line numbers width (by default 40)
selw - Selection bar width (by default 10)
*/
HE_LineNumbersBar( hEdit, state="show", linw=40, selw=10 ) {
static HEM_LINENUMBERSBAR := 2036 ;EQU WM_USER+1012 ;wParam=LNB_HIDE/LNB_SHOW/LNB_AUTOSIZE, lParam=HIWORD:Selection bar width , LOWWORD:Line numbers width
static LNB_HIDE=0, LNB_SHOW=1, LNB_AUTOMAXSIZE=2, LNB_AUTOSIZE=4
if state is not Integer
state := LNB_%state%
SendMessage, HEM_LINENUMBERSBAR,state,selw<<16 | linw,,ahk_id %hEdit%
return errorlevel
}
/*
Function: LineScroll
Scrolls the text in the HiEdit control for the current file.
Parameters:
xScroll - The number of characters to scroll horizontally. Use a
negative number to scroll to the left and a positive number to
scroll to the right.
yScroll - The number of lines to scroll vertically. Use a negative
number to scroll up and a positive number to scroll down.
Remarks:
This message does not move the caret.
The HiEdit control does not scroll vertically past the last line of
text in the control. If the current line plus the number of lines
specified by the yScroll parameter exceeds the total number of lines
in the HiEdit control, the value is adjusted so that the last line
of the HiEdit control is scrolled to the top of the HiEdit control
window.
This function can be used to scroll horizontally past
the last character of any line.
*/
HE_LineScroll(hEdit,xScroll=0,yScroll=0){
Static EM_LINESCROLL:=0xB6
SendMessage EM_LINESCROLL,xScroll,yScroll,,ahk_id %hEdit%
}
/*
Function: NewFile
Opens new tab.
*/
HE_NewFile(hEdit){
static HEM_NEWFILE := 2024 ;wParam=0, lParam=0
SendMessage, HEM_NEWFILE, 0, 0,, ahk_id %hEdit%
return errorlevel
}
/*
Function: OpenFile
Open file in new tab
Parameters:
pFileName - Path of the file to be opened
flag - Set to TRUE to create new file if pFileName doesn't exist.
If set to FALSE, function fails if the file doesn't exist (default).
Returns:
TRUE if successful/FALSE otherwise
*/
HE_OpenFile(hEdit, pFileName, flag=0){
static HEM_OPENFILE := 2025 ;wParam=0, lParam=lpszFileName Returns TRUE if successful/FALSE otherwise
SendMessage, HEM_OPENFILE, flag, &pFileName,, ahk_id %hEdit%
return errorlevel
}
/*
Function: Paste
Paste.
*/
HE_Paste(hEdit) {
Static WM_PASTE:=0x302
SendMessage WM_PASTE,0,0,,ahk_id %hEdit%
}
/*
Function: PosFromChar
Gets the client area coordinates of a specified character.
Parameters:
CharIndex - The zero-based index of the character.
X, Y - These parameters, which must contain valid variable names,
are used to return the x/y-coordinates of a point in the HiEdit
control's client relative to the upper-left corner of the client
area.
Remarks:
If CharIndex is greater than the index of the last character in
the control, the returned coordinates are of the position just past
the last character of the control.
*/
HE_PosFromChar(hEdit,CharIndex,ByRef X,ByRef Y) {
Static EM_POSFROMCHAR=0xD6
VarSetCapacity(POINTL,8,0)
SendMessage EM_POSFROMCHAR,&POINTL,CharIndex,,ahk_id %hEdit%
X:=NumGet(POINTL,0,"Int"), Y:=NumGet(POINTL,4,"Int")
}
/*
Function: Redo
Do redo operation
Returns:
TRUE if the Redo operation succeeds, FALSE otherwise
*/
HE_Redo(hEdit) {
static EM_REDO := 1108
SendMessage, EM_REDO,,,, ahk_id %hEdit%
return ErrorLevel
}
/*
Function: ReloadFile
Reload file
Parameters:
idx - Index of the file to reload. -1 to reload current file (default)
*/
HE_ReloadFile(hEdit, idx=-1) {
static HEM_RELOADFILE=2027 ;EQU WM_USER+1003 ;wParam=0, lParam = -1 for current file
SendMessage, HEM_RELOADFILE, 0, idx,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: ReplaceSel
Replace selection with desired text
Parameters:
text - Text to replace selection with.
*/
HE_ReplaceSel(hEdit, text=""){
static EM_REPLACESEL=194
SendMessage, EM_REPLACESEL, 0, &text,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: SaveFile
Save file to disk
Parameters:
pFileName - File name.
idx - Index of the file to save. -1 to save current file (default)
Returns:
TRUE if successful, FALSE otherwise
*/
HE_SaveFile(hEdit, pFileName, idx=-1){
static HEM_SAVEFILE := 2028 ;wParam=lpszFileName, lParam = -1 for current file or dwFileIndex :Returns
SendMessage, HEM_SAVEFILE, &pFileName, idx,, ahk_id %hEdit%
return Errorlevel
}
/*
Function: Scroll
Scrolls the text vertically in the HiEdit control for the current file.
Parameters:
Pages - The number of pages to scroll. Use a negative number to
scroll up and a positive number to scroll down.
Lines - The number of lines to scroll. Use a negative number to
scroll up and a positive number to scroll down.
Returns:
The number of lines that the command scrolls. The number returned
may not be the same as the actual number of lines scrolled if the
scrolling moves to the beginning or the end of the text.
Remarks:
This message does not move the caret.
0x7FFFFFFF = 2147483647 = largest possible 32-bit signed integer value
Despite the documentation, the return value for the message always
reflects the request, not necessarily the actual number of lines
that were scrolled. Example: If a request to scroll down 25 lines
is made, 25 is returned even if the control is already scrolled down
to the bottom of the document. [Bug?]
*/
HE_Scroll(hEdit,Pages=0,Lines=0){
Static EM_SCROLL:=0xB5, SB_LINEDOWN:=0x1, SB_LINEUP:=0x0, SB_PAGEDOWN:=0x3,SB_PAGEUP:=0x2
;-- Initialize
l_ScrollLineCount := 0
;-- Pages
if Pages
{
l_nScroll:=SB_PAGEDOWN
if Pages < 0
l_nScroll:=SB_PAGEUP, Pages:=Abs(Pages)
loop %Pages%
{
SendMessage EM_SCROLL,l_nScroll,0,,ahk_id %hEdit%
l_ErrorLevel := ErrorLevel
;-- Negative number?
if l_ErrorLevel > 0x7FFFFFFF
l_ErrorLevel:=-(~l_ErrorLevel)-1 ;-- Convert to signed integer
;-- Add to the total
l_ScrollLineCount := l_ScrollLineCount + l_ErrorLevel
}
}
;-- Lines
if Lines
{
l_nScroll := SB_LINEDOWN
if Lines<0
l_nScroll := SB_LINEUP, Lines := Abs(Lines)
loop %Lines%
{
SendMessage EM_SCROLL,l_nScroll,0,,ahk_id %hEdit%
l_ErrorLevel:=ErrorLevel
;-- Negative number?
if l_ErrorLevel>0x7FFFFFFF
l_ErrorLevel:=-(~l_ErrorLevel)-1 ;-- Convert to signed integer
;-- Add to the total
l_ScrollLineCount:=l_ScrollLineCount+l_ErrorLevel
}
}
;-- Return number of lines scrolled
Return l_ScrollLineCount
}
/*
Function: ScrollCaret
Scroll content of control until caret is visible.
*/
HE_ScrollCaret(hEdit){
static EM_SCROLLCARET=183
SendMessage, EM_SCROLLCARET, 0, 0,, ahk_id %hEdit%
Return ErrorLevel
}
/*
Function: SetColors
Set the control colors
Parameters:
colors - Any subset of available color options in INI format (array of NAME=COLOR lines). Skiped colors will be set to 0.
fRdraw - Set to TRUE to redraw control
Colors:
Text - Normal Text Color
Back - Editor Back Color
SelText - Selected Text Color
ActSelBack - Active Selection Back Color
InSelBack - Inactive Selection Back Color
LineNumber - Line Numbers Color
SelBarBack - Selection Bar Back Color
NonPrintableBack - 0 - 31 special non printable chars
Number - Number Color
*/
HE_SetColors(hEdit, colors, fRedraw=true){
static HEM_SETCOLORS := 2037
static names := "Text,Back,SelText,ActSelBack,InSelBack,LineNumber,SelBarBack,NonPrintableBack,Operator,Number,Comment,String"
at := A_AutoTrim
AutoTrim, on
Loop, Parse, colors, `n, `n
{
name := SubStr(A_LoopField, 1, i:=InStr(A_LoopField, "=")-1), val := SubStr(A_LoopField, i+2)
name = %name%
val = %val%
if name not in %names%
return "Invalid color name: '" name "'"
if val is not Integer
return "Invalid color value: '" val "'"
n%name% := val
}
AutoTrim, %at%
VarSetCapacity(COLORS, 36, 0)
NumPut(nText , COLORS, 0) ;NormalTextColor
NumPut(nBack , COLORS, 4) ;EditorBkColor
NumPut(nSelText , COLORS, 8) ;SelectionForeColor
NumPut(nActSelBack , COLORS, 12) ;ActiveSelectionBkColor
NumPut(nInSelBack , COLORS, 16) ;InactiveSelectionBkColor
NumPut(nLineNumber , COLORS, 20) ;LineNumberColor
NumPut(nSelBarBack , COLORS, 24) ;SelBarBkColor
NumPut(nNonPrintableBack, COLORS, 28) ;NonPrintableBackColor
NumPut(nNumber , COLORS, 32) ;NumberColor
SendMessage,HEM_SETCOLORS, &COLORS, fRedraw,,ahk_id %hEdit%
return ErrorLevel
}
/*
Function: SetCurrentFile
Set the current file.
Parameters:
idx - New file index to set as current.
*/
HE_SetCurrentFile(hEdit, idx){
static HEM_SETCURRENTFILE := 2033 ;wParam=0, lParam = dwFileIndex
SendMessage, HEM_SETCURRENTFILE, 0, idx,, ahk_id %hEdit%
return errorlevel
}
/*
Function: SetEvents
Set notification events.
Parameters:
Handler - Function that handles events. If empty, any existing handler will be removed.
Events - White space separated list of events to monitor (by default "selchange").
Handler:
Hwnd - Handle of the HiEdit that sends notification.
Event - Specifies event that occurred. Event must be registered to be able to monitor them. Events "tabmclick" and "filechange" are registered automatically.
EventInfo - Specifies event info.
Events & Infos:
SelChange - S<start_char_idx> E<end_char_idx> L<line_num> [t|*] (t if tab changed, * if text changed)
Key - key pressed
Mouse - x<mouse_x> y<mouse_y> v<virtual_key_code>
Scroll - ""
ContextMenu - ""
FileChange - <file_index> (file is changed outside of the application)
Tabmclick - "" (middle button click over tab)
Returns:
The previous event mask (number).
*/
HE_SetEvents(hEdit, Handler="", Events="selchange"){
static ENM_KEYEVENTS = 0x10000, ENM_MOUSEEVENTS = 0x20000, ENM_SCROLLEVENTS = 0x8, ENM_SELCHANGEEVENTS = 0x80000, ENM_CONTEXTMENUEVENTS=0x20
static EM_SETEVENTMASK = 1093, sEvents="key,mouse,scroll,selchange,contextmenu", old
if (Handler = "")
return OnMessage(0x4E, old != "HE_onNotify" ? old : ""), old := ""
if !IsFunc(Handler)
return A_ThisFunc "> Invalid handler: " Handler
hMask := 0
loop, parse, Events, %A_Tab%%A_Space%
{
IfEqual, A_LoopField,,continue
if A_LoopField not in %sEvents%
return A_ThisFunc "> Invalid event: " A_LoopField
hmask |= ENM_%A_LOOPFIELD%EVENTS
}
if !old {
old := OnMessage(0x4E, "HE_onNotify")
if old != HE_onNotify
HE("oldNotify", RegisterCallback(old))
}
HE(hEdit "Handler", Handler)
SendMessage, EM_SETEVENTMASK,,hMask,, ahk_id %hEdit%
return ErrorLevel
}
/*
Function: SetFont
Sets the control font
Parameters:
pFont - AHK font definition: "Style, FontName"
*/
HE_SetFont(hEdit, pFont="") {
static WM_SETFONT := 0x30
;parse font
italic := InStr(pFont, "italic") ? 1 : 0
underline := InStr(pFont, "underline") ? 1 : 0
strikeout := InStr(pFont, "strikeout") ? 1 : 0
weight := InStr(pFont, "bold") ? 700 : 400
;height
RegExMatch(pFont, "(?<=[S|s])(\d{1,2})(?=[ ,])", height)
if (height = "")
height := 10
RegRead, LogPixels, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\FontDPI, LogPixels
height := -DllCall("MulDiv", "int", Height, "int", LogPixels, "int", 72)
;face
RegExMatch(pFont, "(?<=,).+", fontFace)
if (fontFace != "")
fontFace := RegExReplace( fontFace, "(^\s*)|(\s*$)") ;trim
else fontFace := "MS Sans Serif"
;create font
hFont := DllCall("CreateFont", "int", height, "int", 0, "int", 0, "int", 0
,"int", weight, "Uint", italic, "Uint", underline
,"uint", strikeOut, "Uint", nCharSet, "Uint", 0, "Uint", 0, "Uint", 0, "Uint", 0, "str", fontFace)
SendMessage,WM_SETFONT,hFont,TRUE,,ahk_id %hEdit%
return ErrorLevel
}
/*
Function: SetKeywordFile
Set syntax highlighting.
Parameters:
pFile - Path to .hes file
*/
HE_SetKeywordFile( pFile ){
return DllCall("HiEdit.dll\SetKeywordFile", "str", pFile)
}
/*
Function: SetModify
Sets or clears the modification flag for the current file. The
modification flag indicates whether the text within the control has been modified.
Parameters:
Flag - Set to TRUE to set the modification flag. Set to FALSE to clear the modification flag.
*/
HE_SetModify(hEdit, Flag){
Static EM_SETMODIFY:=0xB9
SendMessage EM_SETMODIFY, Flag, 0, ,ahk_id %hEdit%
}
/*
Function: SetSel
Set the selection
Parameters:
nStart - Starting character position of the selection. Set -1 to remov current selection.
nEnd - Ending character position of the selection. Set -1 to use position of the last character in the control.
*/
HE_SetSel(hEdit, nStart=0, nEnd=-1) {
static EM_SETSEL=0x0B1