-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf.ahk
1537 lines (1452 loc) · 44.8 KB
/
tf.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
/*
Name : TF: Textfile & String Library for AutoHotkey
Version : 3.7
Documentation : https://github.com/hi5/TF
AHKScript.org : http://www.ahkscript.org/boards/viewtopic.php?f=6&t=576
AutoHotkey.com: http://www.autohotkey.com/forum/topic46195.html (Also for examples)
License : see license.txt (GPL 2.0)
Credits & History: See documentation at GH above.
Structure of most functions:
TF_...(Text, other parameters)
{
; get the basic data we need for further processing and returning the output:
TF_GetData(OW, Text, FileName)
; OW = 0 Copy inputfile
; OW = 1 Overwrite inputfile
; OW = 2 Return variable
; Text : either contents of file or the var that was passed on
; FileName : Used in case OW is 0 or 1 (=file), not used for OW=2 (variable)
; Creates a matchlist for use in Loop below
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; A_ThisFunc useful for debugging your scripts
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
...
}
Else
{
...
}
}
; either copy or overwrite file or return variable
Return TF_ReturnOutPut(OW, OutPut, FileName, TrimTrailing, CreateNewFile)
; OW 0 or 1 = file
; Output = new content of file to save or variable to return
; FileName
; TrimTrailing: because of the loops used most functions will add trailing newline, this will remove it by default
; CreateNewFile: To create a file that doesn't exist this parameter is needed, only used in few functions
}
*/
TF_CountLines(Text)
{
TF_GetData(OW, Text, FileName)
StringReplace, Text, Text, `n, `n, UseErrorLevel
Return ErrorLevel + 1
}
TF_ReadLines(Text, StartLine = 1, EndLine = 0, Trailing = 0)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
OutPut .= A_LoopField "`n"
Else if (A_Index => EndLine)
Break
}
OW = 2 ; make sure we return variable not process file
Return TF_ReturnOutPut(OW, OutPut, FileName, Trailing)
}
TF_ReplaceInLines(Text, StartLine = 1, EndLine = 0, SearchText = "", ReplaceText = "")
{
TF_GetData(OW, Text, FileName)
IfNotInString, Text, %SearchText%
Return Text ; SearchText not in TextFile so return and do nothing, we have to return Text in case of a variable otherwise it would empty the variable contents bug fix 3.3
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
StringReplace, LoopField, A_LoopField, %SearchText%, %ReplaceText%, All
OutPut .= LoopField "`n"
}
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_Replace(Text, SearchText, ReplaceText="")
{
TF_GetData(OW, Text, FileName)
IfNotInString, Text, %SearchText%
Return Text ; SearchText not in TextFile so return and do nothing, we have to return Text in case of a variable otherwise it would empty the variable contents bug fix 3.3
Loop
{
StringReplace, Text, Text, %SearchText%, %ReplaceText%, All
if (ErrorLevel = 0) ; No more replacements needed.
break
}
Return TF_ReturnOutPut(OW, Text, FileName, 0)
}
TF_RegExReplaceInLines(Text, StartLine = 1, EndLine = 0, NeedleRegEx = "", Replacement = "")
{
options:="^[imsxADJUXPS]+\)" ; Hat tip to sinkfaze http://www.autohotkey.com/forum/viewtopic.php?t=60062
If RegExMatch(searchText,options,o)
searchText := RegExReplace(searchText,options,(!InStr(o,"m") ? "m$0" : "$0"))
Else searchText := "m)" . searchText
TF_GetData(OW, Text, FileName)
If (RegExMatch(Text, SearchText) < 1)
Return Text ; SearchText not in TextFile so return and do nothing, we have to return Text in case of a variable otherwise it would empty the variable contents bug fix 3.3
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
LoopField := RegExReplace(A_LoopField, NeedleRegEx, Replacement)
OutPut .= LoopField "`n"
}
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_RegExReplace(Text, NeedleRegEx = "", Replacement = "")
{
options:="^[imsxADJUXPS]+\)" ; Hat tip to sinkfaze http://www.autohotkey.com/forum/viewtopic.php?t=60062
if RegExMatch(searchText,options,o)
searchText := RegExReplace(searchText,options,(!InStr(o,"m") ? "m$0" : "$0"))
else searchText := "m)" . searchText
TF_GetData(OW, Text, FileName)
If (RegExMatch(Text, SearchText) < 1)
Return Text ; SearchText not in TextFile so return and do nothing, we have to return Text in case of a variable otherwise it would empty the variable contents bug fix 3.3
Text := RegExReplace(Text, NeedleRegEx, Replacement)
Return TF_ReturnOutPut(OW, Text, FileName, 0)
}
TF_RemoveLines(Text, StartLine = 1, EndLine = 0)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
Continue
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_RemoveBlankLines(Text, StartLine = 1, EndLine = 0)
{
TF_GetData(OW, Text, FileName)
If (RegExMatch(Text, "[\S]+?\r?\n?") < 1)
Return Text ; No empty lines so return and do nothing, we have to return Text in case of a variable otherwise it would empty the variable contents bug fix 3.3
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
OutPut .= (RegExMatch(A_LoopField,"[\S]+?\r?\n?")) ? A_LoopField "`n" :
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_RemoveDuplicateLines(Text, StartLine = 1, Endline = 0, Consecutive = 0, CaseSensitive = false)
{
TF_GetData(OW, Text, FileName)
If (StartLine = "")
StartLine = 1
If (Endline = 0 OR Endline = "")
EndLine := TF_Count(Text, "`n") + 1
Loop, Parse, Text, `n, `r
{
If (A_Index < StartLine)
Section1 .= A_LoopField "`n"
If A_Index between %StartLine% and %Endline%
{
If (Consecutive = 1)
{
If (A_LoopField <> PreviousLine) ; method one for consecutive duplicate lines
Section2 .= A_LoopField "`n"
PreviousLine:=A_LoopField
}
Else
{
If !(InStr(SearchForSection2,"__bol__" . A_LoopField . "__eol__",CaseSensitive)) ; not found
{
SearchForSection2 .= "__bol__" A_LoopField "__eol__" ; this makes it unique otherwise it could be a partial match
Section2 .= A_LoopField "`n"
}
}
}
If (A_Index > EndLine)
Section3 .= A_LoopField "`n"
}
Output .= Section1 Section2 Section3
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_InsertLine(Text, StartLine = 1, Endline = 0, InsertText = "")
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
Output .= InsertText "`n" A_LoopField "`n"
Else
Output .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_ReplaceLine(Text, StartLine = 1, Endline = 0, ReplaceText = "")
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
Output .= ReplaceText "`n"
Else
Output .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_InsertPrefix(Text, StartLine = 1, EndLine = 0, InsertText = "")
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
OutPut .= InsertText A_LoopField "`n"
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_InsertSuffix(Text, StartLine = 1, EndLine = 0 , InsertText = "")
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
OutPut .= A_LoopField InsertText "`n"
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_TrimLeft(Text, StartLine = 1, EndLine = 0, Count = 1)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
StringTrimLeft, StrOutPut, A_LoopField, %Count%
OutPut .= StrOutPut "`n"
}
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_TrimRight(Text, StartLine = 1, EndLine = 0, Count = 1)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
StringTrimRight, StrOutPut, A_LoopField, %Count%
OutPut .= StrOutPut "`n"
}
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_AlignLeft(Text, StartLine = 1, EndLine = 0, Columns = 80, Padding = 0)
{
Trim:=A_AutoTrim ; store trim settings
AutoTrim, On ; make sure AutoTrim is on
TF_GetData(OW, Text, FileName)
If (Endline = 0 OR Endline = "")
EndLine := TF_Count(Text, "`n") + 1
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
LoopField = %A_LoopField% ; Make use of AutoTrim, should be faster then a RegExReplace. Trims leading and trailing spaces!
SpaceNum := Columns-StrLen(LoopField)-1
If (SpaceNum > 0) and (Padding = 1) ; requires padding + keep padding
{
Left:=TF_SetWidth(LoopField,Columns, 0) ; align left
OutPut .= Left "`n"
}
Else
OutPut .= LoopField "`n"
}
Else
OutPut .= A_LoopField "`n"
}
AutoTrim, %Trim% ; restore original Trim
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_AlignCenter(Text, StartLine = 1, EndLine = 0, Columns = 80, Padding = 0)
{
Trim:=A_AutoTrim ; store trim settings
AutoTrim, On ; make sure AutoTrim is on
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
LoopField = %A_LoopField% ; Make use of AutoTrim, should be faster then a RegExReplace
SpaceNum := (Columns-StrLen(LoopField)-1)/2
If (Padding = 1) and (LoopField = "") ; skip empty lines, do not fill with spaces
{
OutPut .= "`n"
Continue
}
If (StrLen(LoopField) >= Columns)
{
OutPut .= LoopField "`n" ; add as is
Continue
}
Centered:=TF_SetWidth(LoopField,Columns, 1) ; align center using set width
OutPut .= Centered "`n"
}
Else
OutPut .= A_LoopField "`n"
}
AutoTrim, %Trim% ; restore original Trim
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_AlignRight(Text, StartLine = 1, EndLine = 0, Columns = 80, Skip = 0)
{
Trim:=A_AutoTrim ; store trim settings
AutoTrim, On ; make sure AutoTrim is on
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
LoopField = %A_LoopField% ; Make use of AutoTrim, should be faster then a RegExReplace
If (Skip = 1) and (LoopField = "") ; skip empty lines, do not fill with spaces
{
OutPut .= "`n"
Continue
}
If (StrLen(LoopField) >= Columns)
{
OutPut .= LoopField "`n" ; add as is
Continue
}
Right:=TF_SetWidth(LoopField,Columns, 2) ; align right using set width
OutPut .= Right "`n"
}
Else
OutPut .= A_LoopField "`n"
}
AutoTrim, %Trim% ; restore original Trim
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
; Based on: CONCATenate text files, ftp://garbo.uwasa.fi/pc/ts/tsfltc22.zip
TF_ConCat(FirstTextFile, SecondTextFile, OutputFile = "", Blanks = 0, FirstPadMargin = 0, SecondPadMargin = 0)
{
If (Blanks > 0)
Loop, %Blanks%
InsertBlanks .= A_Space
If (FirstPadMargin > 0)
Loop, %FirstPadMargin%
PaddingFile1 .= A_Space
If (SecondPadMargin > 0)
Loop, %SecondPadMargin%
PaddingFile2 .= A_Space
Text:=FirstTextFile
TF_GetData(OW, Text, FileName)
StringSplit, Str1Lines, Text, `n, `r
Text:=SecondTextFile
TF_GetData(OW, Text, FileName)
StringSplit, Str2Lines, Text, `n, `r
Text= ; clear mem
; first we need to determine the file with the most lines for our loop
If (Str1Lines0 > Str2Lines0)
MaxLoop:=Str1Lines0
Else
MaxLoop:=Str2Lines0
Loop, %MaxLoop%
{
Section1:=Str1Lines%A_Index%
Section2:=Str2Lines%A_Index%
OutPut .= Section1 PaddingFile1 InsertBlanks Section2 PaddingFile2 "`n"
Section1= ; otherwise it will remember the last line from the shortest file or var
Section2=
}
OW=1 ; it is probably 0 so in that case it would create _copy, so set it to 1
If (OutPutFile = "") ; if OutPutFile is empty return as variable
OW=2
Return TF_ReturnOutPut(OW, OutPut, OutputFile, 1, 1)
}
TF_LineNumber(Text, Leading = 0, Restart = 0, Char = 0) ; HT ribbet.1
{
global t
TF_GetData(OW, Text, FileName)
Lines:=TF_Count(Text, "`n") + 1
Padding:=StrLen(Lines)
If (Leading = 0) and (Char = 0)
Char := A_Space
Loop, %Padding%
PadLines .= Char
Loop, Parse, Text, `n, `r
{
If Restart = 0
MaxNo = %A_Index%
Else
{
MaxNo++
If MaxNo > %Restart%
MaxNo = 1
}
LineNumber:= MaxNo
If (Leading = 1)
{
LineNumber := Padlines LineNumber ; add padding
StringRight, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
}
If (Leading = 0)
{
LineNumber := LineNumber Padlines ; add padding
StringLeft, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
}
OutPut .= LineNumber A_Space A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
; skip = 1, skip shorter lines (e.g. lines shorter startcolumn position)
; modified in TF 3.4, fixed in 3.5
TF_ColGet(Text, StartLine = 1, EndLine = 0, StartColumn = 1, EndColumn = 1, Skip = 0)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
If (StartColumn < 0)
{
StartColumn++
Loop, Parse, Text, `n, `r ; parsing file/var
{
If A_Index in %TF_MatchList%
{
output .= SubStr(A_LoopField,StartColumn) "`n"
}
else
output .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
if RegExMatch(StartColumn, ",|\+|-")
{
StartColumn:=_MakeMatchList(Text, StartColumn, 1, 1)
Loop, Parse, Text, `n, `r ; parsing file/var
{
If A_Index in %TF_MatchList%
{
loop, parse, A_LoopField ; parsing LINE char by char
{
If A_Index in %StartColumn% ; if col in index get char
output .= A_LoopField
}
output .= "`n"
}
else
output .= A_LoopField "`n"
}
output .= A_LoopField "`n"
}
else
{
EndColumn:=(EndColumn+1)-StartColumn
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
StringMid, Section, A_LoopField, StartColumn, EndColumn
If (Skip = 1) and (StrLen(A_LoopField) < StartColumn)
Continue
OutPut .= Section "`n"
}
}
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
; Based on: COLPUT.EXE & CUT.EXE, ftp://garbo.uwasa.fi/pc/ts/tsfltc22.zip
; modified in TF 3.4
TF_ColPut(Text, Startline = 1, EndLine = 0, StartColumn = 1, InsertText = "", Skip = 0)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
If RegExMatch(StartColumn, ",|\+")
{
StartColumn:=_MakeMatchList(Text, StartColumn, 0, 1)
Loop, Parse, Text, `n, `r ; parsing file/var
{
If A_Index in %TF_MatchList%
{
loop, parse, A_LoopField ; parsing LINE char by char
{
If A_Index in %StartColumn% ; if col in index insert text
output .= InsertText A_LoopField
Else
output .= A_LoopField
}
output .= "`n"
}
else
output .= A_LoopField "`n"
}
output .= A_LoopField "`n"
}
else
{
StartColumn--
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
If (StartColumn > 0)
{
StringLeft, Section1, A_LoopField, StartColumn
StringMid, Section2, A_LoopField, StartColumn+1
If (Skip = 1) and (StrLen(A_LoopField) < StartColumn)
OutPut .= Section1 Section2 "`n"
}
Else
{
Section1:=SubStr(A_LoopField, 1, StrLen(A_LoopField) + StartColumn + 1)
Section2:=SubStr(A_LoopField, StrLen(A_LoopField) + StartColumn + 2)
If (Skip = 1) and (A_LoopField = "")
OutPut .= Section1 Section2 "`n"
}
OutPut .= Section1 InsertText Section2 "`n"
}
Else
OutPut .= A_LoopField "`n"
}
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
; modified TF 3.4
TF_ColCut(Text, StartLine = 1, EndLine = 0, StartColumn = 1, EndColumn = 1)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
If RegExMatch(StartColumn, ",|\+|-")
{
StartColumn:=_MakeMatchList(Text, StartColumn, EndColumn, 1)
Loop, Parse, Text, `n, `r ; parsing file/var
{
If A_Index in %TF_MatchList%
{
loop, parse, A_LoopField ; parsing LINE char by char
{
If A_Index not in %StartColumn% ; if col not in index get char
output .= A_LoopField
}
output .= "`n"
}
else
output .= A_LoopField "`n"
}
output .= A_LoopField "`n"
}
else
{
StartColumn--
EndColumn++
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
StringLeft, Section1, A_LoopField, StartColumn
StringMid, Section2, A_LoopField, EndColumn
OutPut .= Section1 Section2 "`n"
}
Else
OutPut .= A_LoopField "`n"
}
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_ReverseLines(Text, StartLine = 1, EndLine = 0)
{
TF_GetData(OW, Text, FileName)
StringSplit, Line, Text, `n, `r ; line0 is number of lines
If (EndLine = 0 OR EndLine = "")
EndLine:=Line0
If (EndLine > Line0)
EndLine:=Line0
CountDown:=EndLine+1
Loop, Parse, Text, `n, `r
{
If (A_Index < StartLine)
Output1 .= A_LoopField "`n" ; section1
If A_Index between %StartLine% and %Endline%
{
CountDown--
Output2 .= Line%CountDown% "`n" section2
}
If (A_Index > EndLine)
Output3 .= A_LoopField "`n"
}
OutPut.= Output1 Output2 Output3
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
;TF_SplitFileByLines
;example:
;TF_SplitFileByLines("TestFile.txt", "4", "sfile_", "txt", "1") ; split file every 3 lines
; InFile = 0 skip line e.g. do not include the actual line in any of the output files
; InFile = 1 include line IN current file
; InFile = 2 include line IN next file
TF_SplitFileByLines(Text, SplitAt, Prefix = "file", Extension = "txt", InFile = 1)
{
LineCounter=1
FileCounter=1
Where:=SplitAt
Method=1
; 1 = default, splitat every X lines,
; 2 = splitat: - rotating if applicable
; 3 = splitat: specific lines comma separated
TF_GetData(OW, Text, FileName)
IfInString, SplitAt, `- ; method 2
{
StringSplit, Split, SplitAt, `-
Part=1
Where:=Split%Part%
Method=2
}
IfInString, SplitAt, `, ; method 3
{
StringSplit, Split, SplitAt, `,
Part=1
Where:=Split%Part%
Method=3
}
Loop, Parse, Text, `n, `r
{
OutPut .= A_LoopField "`n"
If (LineCounter = Where)
{
If (InFile = 0)
{
StringReplace, CheckOutput, PreviousOutput, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutput <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, PreviousOutput, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; skip empty files
TF_SetGlobal(Prefix FileCounter,PreviousOutput)
Output:=
}
If (InFile = 1)
{
StringReplace, CheckOutput, Output, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutput <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, Output, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; skip empty files
TF_SetGlobal(Prefix FileCounter,Output)
Output:=
}
If (InFile = 2)
{
OutPut := PreviousOutput
StringReplace, CheckOutput, Output, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutput <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, Output, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; output to array
TF_SetGlobal(Prefix FileCounter,Output)
OutPut := A_LoopField "`n"
}
If (Method <> 3)
LineCounter=0 ; reset
FileCounter++ ; next file
Part++
If (Method = 2) ; 2 = splitat: - rotating if applicable
{
If (Part > Split0)
{
Part=1
}
Where:=Split%Part%
}
If (Method = 3) ; 3 = splitat: specific lines comma separated
{
If (Part > Split0)
Where:=Split%Split0%
Else
Where:=Split%Part%
}
}
LineCounter++
PreviousOutput:=Output
PreviousLine:=A_LoopField
}
StringReplace, CheckOutput, Output, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutPut <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, Output, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; output to array
{
TF_SetGlobal(Prefix FileCounter,Output)
TF_SetGlobal(Prefix . "0" , FileCounter)
}
}
; TF_SplitFileByText("TestFile.txt", "button", "sfile_", "txt") ; split file at every line with button in it, can be regexp
; InFile = 0 skip line e.g. do not include the actual line in any of the output files
; InFile = 1 include line IN current file
; InFile = 2 include line IN next file
TF_SplitFileByText(Text, SplitAt, Prefix = "file", Extension = "txt", InFile = 1)
{
LineCounter=1
FileCounter=1
TF_GetData(OW, Text, FileName)
SplitPath, TextFile,, Dir
Loop, Parse, Text, `n, `r
{
OutPut .= A_LoopField "`n"
FoundPos:=RegExMatch(A_LoopField, SplitAt)
If (FoundPos > 0)
{
If (InFile = 0)
{
StringReplace, CheckOutput, PreviousOutput, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutput <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, PreviousOutput, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; output to array
TF_SetGlobal(Prefix FileCounter,PreviousOutput)
Output:=
}
If (InFile = 1)
{
StringReplace, CheckOutput, Output, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutput <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, Output, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; output to array
TF_SetGlobal(Prefix FileCounter,Output)
Output:=
}
If (InFile = 2)
{
OutPut := PreviousOutput
StringReplace, CheckOutput, Output, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutput <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, Output, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; output to array
TF_SetGlobal(Prefix FileCounter,Output)
OutPut := A_LoopField "`n"
}
LineCounter=0 ; reset
FileCounter++ ; next file
}
LineCounter++
PreviousOutput:=Output
PreviousLine:=A_LoopField
}
StringReplace, CheckOutput, Output, `n, , All
StringReplace, CheckOutput, CheckOutput, `r, , All
If (CheckOutPut <> "") and (OW <> 2) ; skip empty files
TF_ReturnOutPut(1, Output, Prefix FileCounter "." Extension, 0, 1)
If (CheckOutput <> "") and (OW = 2) ; output to array
{
TF_SetGlobal(Prefix FileCounter,Output)
TF_SetGlobal(Prefix . "0" , FileCounter)
}
}
TF_Find(Text, StartLine = 1, EndLine = 0, SearchText = "", ReturnFirst = 1, ReturnText = 0)
{
options:="^[imsxADJUXPS]+\)"
if RegExMatch(searchText,options,o)
searchText:=RegExReplace(searchText,options,(!InStr(o,"m") ? "m$0(*ANYCRLF)" : "$0"))
else searchText:="m)(*ANYCRLF)" searchText
options:="^[imsxADJUXPS]+\)" ; Hat tip to sinkfaze, see http://www.autohotkey.com/forum/viewtopic.php?t=60062
if RegExMatch(searchText,options,o)
searchText := RegExReplace(searchText,options,(!InStr(o,"m") ? "m$0" : "$0"))
else searchText := "m)" . searchText
TF_GetData(OW, Text, FileName)
If (RegExMatch(Text, SearchText) < 1)
Return "0" ; SearchText not in file or error, so do nothing
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
If (RegExMatch(A_LoopField, SearchText) > 0)
{
If (ReturnText = 0)
Lines .= A_Index "," ; line number
Else If (ReturnText = 1)
Lines .= A_LoopField "`n" ; text of line
Else If (ReturnText = 2)
Lines .= A_Index ": " A_LoopField "`n" ; add line number
If (ReturnFirst = 1) ; only return first occurrence
Break
}
}
}
If (Lines <> "")
StringTrimRight, Lines, Lines, 1 ; trim trailing , or `n
Else
Lines = 0 ; make sure we return 0
Return Lines
}
TF_Prepend(File1, File2)
{
FileList=
(
%File1%
%File2%
)
TF_Merge(FileList,"`n", "!" . File2)
Return
}
TF_Append(File1, File2)
{
FileList=
(
%File2%
%File1%
)
TF_Merge(FileList,"`n", "!" . File2)
Return
}
; For TF_Merge You will need to create a Filelist variable, one file per line,
; to pass on to the function:
; FileList=
; (
; c:\file1.txt
; c:\file2.txt
; )
; use Loop (files & folders) to create one quickly if you want to merge all TXT files for example
;
; Loop, c:\*.txt
; FileList .= A_LoopFileFullPath "`n"
;
; By default, a new line is used as a separator between two text files
; !merged.txt deletes target file before starting to merge files
TF_Merge(FileList, Separator = "`n", FileName = "merged.txt")
{
OW=0
Loop, Parse, FileList, `n, `r
{
Append2File= ; Just make sure it is empty
IfExist, %A_LoopField%
{
FileRead, Append2File, %A_LoopField%
If not ErrorLevel ; Successfully loaded
Output .= Append2File Separator
}
}
If (SubStr(FileName,1,1)="!") ; check if we want to delete the target file before we start
{
FileName:=SubStr(FileName,2)
OW=1
}
Return TF_ReturnOutPut(OW, OutPut, FileName, 0, 1)
}
TF_Wrap(Text, Columns = 80, AllowBreak = 0, StartLine = 1, EndLine = 0)
{
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
If (AllowBreak = 1)
Break=
Else
Break=[ \r?\n]
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
If (StrLen(A_LoopField) > Columns)
{
LoopField := A_LoopField " " ; just seems to work better by adding a space
OutPut .= RegExReplace(LoopField, "(.{1," . Columns . "})" . Break , "$1`n")
}
Else
OutPut .= A_LoopField "`n"
}
Else
OutPut .= A_LoopField "`n"
}
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
TF_WhiteSpace(Text, RemoveLeading = 1, RemoveTrailing = 1, StartLine = 1, EndLine = 0) {
TF_GetData(OW, Text, FileName)
TF_MatchList:=_MakeMatchList(Text, StartLine, EndLine, 0, A_ThisFunc) ; create MatchList
Trim:=A_AutoTrim ; store trim settings
AutoTrim, On ; make sure AutoTrim is on
Loop, Parse, Text, `n, `r
{
If A_Index in %TF_MatchList%
{
If (RemoveLeading = 1) AND (RemoveTrailing = 1)
{
LoopField = %A_LoopField%
Output .= LoopField "`n"
Continue
}
If (RemoveLeading = 1) AND (RemoveTrailing = 0)
{
LoopField := A_LoopField . "."
LoopField = %LoopField%
StringTrimRight, LoopField, LoopField, 1
Output .= LoopField "`n"
Continue
}
If (RemoveLeading = 0) AND (RemoveTrailing = 1)
{
LoopField := "." A_LoopField
LoopField = %LoopField%
StringTrimLeft, LoopField, LoopField, 1
Output .= LoopField "`n"
Continue
}
If (RemoveLeading = 0) AND (RemoveTrailing = 0)
{
Output .= A_LoopField "`n"
Continue
}
}
Else
Output .= A_LoopField "`n"
}
AutoTrim, %Trim% ; restore original Trim
Return TF_ReturnOutPut(OW, OutPut, FileName)
}
; Delete lines from file1 in file2 (using StringReplace)
; Partialmatch = 2 added in 3.4
TF_Substract(File1, File2, PartialMatch = 0) {
Text:=File1
TF_GetData(OW, Text, FileName)
Str1:=Text
Text:=File2
TF_GetData(OW, Text, FileName)
OutPut:=Text
If (OW = 2)
File1= ; free mem in case of var/text
OutPut .= "`n" ; just to make sure the StringReplace will work
If (PartialMatch = 2)
{
Loop, Parse, Str1, `n, `r
{
IfInString, Output, %A_LoopField%
{
Output:= RegExReplace(Output, "im)^.*" . A_LoopField . ".*\r?\n?", replace)
}
}