AutoHotkey Community

It is currently May 26th, 2012, 9:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 194 posts ]  Go to page Previous  1, 2, 3, 4, 5 ... 13  Next
Author Message
 Post subject:
PostPosted: July 28th, 2009, 5:40 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Quote:
* 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).

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 28th, 2009, 9:18 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
majkinetor wrote:
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:

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


majkinetor wrote:
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

Quote:
1,222222,3
44,5,6


will become

Quote:
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.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 28th, 2009, 11:33 pm 
Offline

Joined: January 12th, 2007, 4:30 am
Posts: 531
Location: Norway
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 29th, 2009, 4:35 am 
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

Code:

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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 5:10 am 
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. :-)

Code:
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)
   }


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 7:41 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Murp|e wrote:
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.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 29th, 2009, 8:05 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
ribbet.one wrote:
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 :-)

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 29th, 2009, 10:22 am 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
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.
Quote:
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:
Code:
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:
Code:
TF_LineNumber(F,0,0," ")

I get this:
Code:
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:
Code:
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.

Quote:
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.
Quote:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 11:02 am 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
And if you use this modification, you can also choose to pad the line number or not:
Code:
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:
Code:
TF_LineNumber(F,2,0," ")

you get this:
Code:
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
Code:
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:
Code:
 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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 11:21 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
@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:

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Last edited by SoLong&Thx4AllTheFish on July 29th, 2009, 5:24 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 5:23 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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, http://www.autohotkey.com/forum/viewtop ... 687#284687

- Built in Check if TextFile actually exists, to prevent creation of empty file(s).
Thanks for the idea Murp|e, http://www.autohotkey.com/forum/viewtop ... 649#284649

- 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.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 7:25 pm 
Offline

Joined: February 20th, 2007, 1:37 pm
Posts: 198
Location: D.C.
HugoV wrote:
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 29th, 2009, 8:24 pm 
Thanks!!!!!


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 3rd, 2009, 2:22 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 6th, 2009, 11:45 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
v2.4 update, 06 August 2009:

- Added: TF_Sort (HugoV)

Simply pass on the Sort options as described in http://www.autohotkey.com/docs/commands/Sort.htm to sort (a section of) TextFile

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 194 posts ]  Go to page Previous  1, 2, 3, 4, 5 ... 13  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group