Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

TF: Text file & Variables/String Library v3.4 [lib]


  • Please log in to reply
244 replies to this topic
majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006

* Note: yes they are a bit silly, but I find them to be useful
as they serve as a shorthand for TF_ReplaceInLines. They are
by no means "intelligent". Thanks to infogulch for the idea:

Its not silly. I use that feature pretty much tbh and every serious editor has it.

One other cool function to have would be to extract portion of file from one key sentence to another and single Trim function, perhaps with better signature to accept trim type (left, right, both).
Posted Image

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

Its not silly. I use that feature pretty much tbh and every serious editor has it.

The function itself is useful, but you could already do that with a little bit of extra effort:

TF_ReplaceInLines(F, "%A_Tab%", "%A_Space%%A_Space%%A_Space%%A_Space%") ; you could do this
TF_Tab2Spaces(F) ; but this is shorter :-)

One other cool function to have would be to extract portion of file from one key sentence to another and single Trim function, perhaps with better signature to accept trim type (left, right, both).


So not TF_RangeReplace but a TF_RangeExtract/Grep/Echo like function, yes that can be very useful, I'll look into it. Another idea I've almost completed is converting a delimited file (such as a CSV) to a fixed column, so

1,222222,3
44,5,6


will become

1.,222222,3
44,5........,6

(. is space)

which will enable further processing by the TF_Col* functions

UltraEdit has this built in and I find it extremely useful combined with
the other column editing functions of UE.

Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
HugoV: I was using the TF_RemoveBlankLines function with the "!" symbol for OverWrite (Because I want to remove the blank lines of a file, as opposed to creating a copy of it minus the blank lines). This works fine except in cases where the the file doesn't exist. If the file doesn't exist, this function used with overwrite will create a blank file. I don't think a function which is supposed to remove something from a file, should ever be expected to create an entirely new filequestionmark

ribbet.one
  • Guests
  • Last active:
  • Joined: --
HugoV you are awesome. Heresy also. And the rest of you too.

I have always wanted a way to emulate line enumeration found in my old word processor, WordPerfect. Call it a legacy from the days of hard copy :-) but it can start new line numbers on every page. So with this small mod you can emulate that and make the line numbers restart every time it reaches a designated number. This way I can set up my printed pages to contain N number of lines and make line number restart at N.

Also a minor mod here is you can replace the leading zero with any character, with the possible exception of ahk operators or code (haven't checked that). And the default, instead of zero, is simply A_Space. This way effectively Right-justifies the line number.

I still want a way to left-justify the line number without having all the text move to the right one char when you add another digit, which is what happens now.

So two new params in this version:

MaxNumber = maximum line number. If = 0 (default) MaxNumber = true line number

Padliner = character or number to take place of "leading zero." Default is A_Space

TF_LineNumber(TextFile, LeadingZeros = 0, MaxNumber = 0, Padliner = "A_Space")
	
	{
	 TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
	 FileRead, Str, %TextFile%
	 If (LeadingZeros > 0)
		{
         	 StringReplace, Str, Str, `n, `n, UseErrorLevel
	         Lines:=ErrorLevel+1
        	 Padding:=StrLen(Lines)
	         Loop, %Padding%
			 PadLines .= Padliner
		}
	 Loop, Parse, Str, `n
		{
			 If MaxNumber = 0
				 MaxNo = %A_Index%
			 Else
				 {
					 MaxNo++
					 If MaxNo > %MaxNumber%
					 Maxno = 1
				 }
	         LineNumber:= Maxno
        	 If (LeadingZeros > 0)
			{
			 LineNumber := Padlines LineNumber  ; add padding
			 StringRight, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
			}
		 OutPut .= LineNumber A_Space A_LoopField "`n"
		}
	 Return OW ? _OverWrite_(TextFile, Output) : _MakeCopy_(TextFile, Output)
	}


Pls. feel free to offer any improvements or suggestions. I'm not a fluent AHK coder, but with your help I'm improving slowly. I've only tested these a few times.

ribbet.one
  • Guests
  • Last active:
  • Joined: --
OK, I think this may fix the left justify issue with the line number when there are no leading characters. Feel free to tell me if I'm doing it wrong or to help me out here. I may have it right. :-)

TF_LineNumber(TextFile, LeadingZeros = 0, MaxNumber = 0, Padliner = "A_Space")
	
	{
	 TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
	 FileRead, Str, %TextFile%
	 ;If (LeadingZeros > 0)
		{
         	 StringReplace, Str, Str, `n, `n, UseErrorLevel
	         Lines:=ErrorLevel+1
        	 Padding:=StrLen(Lines)
	         Loop, %Padding%
			 PadLines .= Padliner
		}
	 Loop, Parse, Str, `n
		{
			 If MaxNumber = 0
				 MaxNo = %A_Index%
			 Else
				 {
					 MaxNo++
					 If MaxNo > %MaxNumber%
					 Maxno = 1
				 }
	         LineNumber:= Maxno
        	 If (LeadingZeros > 0)
			{
			 LineNumber := Padlines LineNumber  ; add padding
			 StringRight, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
			}
			 If (LeadingZeros = 0)
			{
			 LineNumber := LineNumber Padlines ; add padding
			 StringLeft, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
			}			 
		 OutPut .= LineNumber A_Space A_LoopField "`n"
		}
	 Return OW ? _OverWrite_(TextFile, Output) : _MakeCopy_(TextFile, Output)
	}


SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

HugoV: I was using the TF_RemoveBlankLines ... This works fine except in cases where the the file doesn't exist. If the file doesn't exist, this function used with overwrite will create a blank file. I don't think a function which is supposed to remove something from a file, should ever be expected to create an entirely new file

All functions assume the file you pass on to the function exist, if it doesn't it means there is no input and it will simple generate an empty file (0 byte).

But I'll put it on my TODO list.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

WordPerfect .... Call it a legacy from the days of hard copy :-) but it can start new line numbers on every page....

Also a minor mod here is you can replace the leading zero with any character, with the possible exception of ahk operators or code (haven't checked that). And the default, instead of zero, is simply A_Space. This way effectively Right-justifies the line number.

I still want a way to left-justify the line number without having all the text move to the right one char when you add another digit, which is what happens now.

Thanks for the suggestion, I will add this to the library. I've been testing it (changing your mod slightly)

Output generated:

padding with "0" (leading = 1) ; 0 is default value
...
08 line
09 line
10 line
... --> OK (same as before)

padding with " " (leading = 1)
...
8 line
9 line
10 line
... --> OK (same as before but with space passed on as padding char)

no padding
...
8 line
9 line
10 line
...

Now the question is should the "no padding" line up
the original lines so look like this:

no padding, where _ indicates a space because of the forum doesn't show multiple spaces
...
8 _line
9 _line
10 line
...

Or should it be left as users choice, e.g. lineup yes / no

Re: wordperfect yes those were the days :-)

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
The BB editor and the forum's font don't display this issue properly. So I'm using the "code" tags to make it more clear.

no padding
...
8 line
9 line
10 line
...

I don't see the padding there. Maybe your results above are just a BB editor glitch, but if you set the leadingzero to 0 and still have a char for the "padliner" param ( here " " ) you get this:
7  This is line seven
8  This is line eight
9  This is line nine
10 This is line ten
or just for the purpose of clarifying, using these settings:
TF_LineNumber(F,0,0," ")
I get this:
7  This is line seven
8  This is line eight
9  This is line nine
10 This is line ten
11 This is line eleven
12 This is line etc.
Instead of this:
7 This is line seven
8 This is line eight
9 This is line nine
10 This is line ten
11 This is line eleven
12 This is line etc.

Now the question is should the "no padding" line up
the original lines so look like this:

no padding, where _ indicates a space because of the forum doesn't show multiple spaces
...
8 _line
9 _line
10 line
...

That's my personal preference, partly for aesthetic reasons. Moreover, that way column operations will still work the same after you apply line numbers. And I can still use my beloved "block editing" in Textpad too. So if we're voting, I'd say make it that way.

Or should it be left as users choice, e.g. lineup yes / no

Easy enough to create another param for that and let user decide. But I think I have it the way I want it.

Re: WP, before I had my AHK I dabbled in PerfectScript. I always wondered if PerfectScript and AutoIt had some of the same origins.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
And if you use this modification, you can also choose to pad the line number or not:
TF_LineNumber(TextFile, LeadingZeros = 0, MaxNumber = 0, Padliner = "A_Space")
	
	{
	 TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
	 FileRead, Str, %TextFile%
		{
         	 StringReplace, Str, Str, `n, `n, UseErrorLevel
	         Lines:=ErrorLevel+1
        	 Padding:=StrLen(Lines)
	         Loop, %Padding%
			 PadLines .= Padliner
		}
	 Loop, Parse, Str, `n
		{
			 If MaxNumber = 0
				 MaxNo = %A_Index%
			 Else
				 {
					 MaxNo++
					 If MaxNo > %MaxNumber%
					 Maxno = 1
				 }
	         LineNumber:= Maxno
        	 If (LeadingZeros = 1)
			{
			 LineNumber := Padlines LineNumber  ; add padding
			 StringRight, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
			}
			 If (LeadingZeros = 0)
			{
			 LineNumber := LineNumber Padlines ; add padding
			 StringLeft, LineNumber, LineNumber, StrLen(Lines) ; remove excess padding
			}			 
		 OutPut .= LineNumber A_Space A_LoopField "`n"
		}
	 Return OW ? _OverWrite_(TextFile, Output) : _MakeCopy_(TextFile, Output)
	}
If you make the LeadingZero any number besides 0 or 1, like this:
TF_LineNumber(F,2,0," ")
you get this:
8 This is line eight
9 This is line nine
10 This is line ten
11 This is line eleven
But if you make LeadingZero a 0 you get
8  This is line eight
9  This is line nine
10 This is line ten
11 This is line eleven
And if you make it a 1 you get:
8 This is line eight
 9 This is line nine
10 This is line ten
11 This is line eleven
So freedom of choice is restored and it didn't even take any more code lines.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
@ribbet.1 yes I noticed that too while experimenting and preparing the examples. Thanks. I was bugfixing something else, see "history" in next update :wink:

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
v2.3a update/bugfix, 29 July 2009:

Fixed/Changed:

- New features in TF_LineNumber:
Restart + Choice of leading/padding character
Thanks for the idea ribbet.1, <!-- m -->http://www.autohotke... ... 687#284687<!-- m -->

- Built in Check if TextFile actually exists, to prevent creation of empty file(s).
Thanks for the idea Murp|e, <!-- m -->http://www.autohotke... ... 649#284649<!-- m -->

- TF_Replace: if SearchText wasn't present in TextFile the function never returned (stuck in endless loop)

- TF_ReplaceInLines: if SearchText wasn't present in TextFile simple return an do nothing (faster, does not create file_copy)

- TF_RegExReplaceInLines: if NeedleRegEx wasn't present in TextFile simple return an do nothing (faster, does not create file_copy)

@ribbet.1: I believe your version of TF_LineNumber above has a bug, try it with TF_LineNumber(F) and see what happens :wink:

I've made some changes to your version so it uses 0 by default so the new function behaves the same as the old one when using the F and F,1 examples.

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007

v2.3a update/bugfix, 29 July 2009:


@ribbet.1: I believe your version of TF_LineNumber above has a bug, try it with TF_LineNumber(F) and see what happens :wink:

I guess you don't want to use "A_Space" after all. :oops: I replace it with " " and it seems to work. I hope that's all you're referring to.

Thanks for considering my suggestions. I've never seen a text editor that has this feature common in word processors, and I've wanted it for a while.

Tastingo
  • Guests
  • Last active:
  • Joined: --
Thanks!!!!!

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
v2.3b minor update:

- Fixed: no output problem for TF_SplitFileBy* functions, "bug" introduced by 2.3a "Built in Check if TextFile actually exists"
- TF_RemoveBlankLines check if file has empty empty lines to start with, if not, return and do nothing (does not create file_copy identical to file)
- TF_RangeReplace same fix as 2.3a

Edit: will work on it some more and build in check to speed up various functions, no use in looping all lines if the end result will be the same as the input.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
v2.4 update, 06 August 2009:

- Added: TF_Sort (HugoV)

Simply pass on the Sort options as described in <!-- m -->http://www.autohotke...mmands/Sort.htm<!-- m --> to sort (a section of) TextFile