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
faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
Try this
Clipboard:=TF_InsertPrefix(TF_Wrap(RegExReplace(RegExReplace(RegExReplace(Clipboard,"(\r?\n){2,}","__nl__"),"(\r?\n)"," "),"__nl__","`n`n"), 30), "", "", "> ")
:-)
The triple nested RegExReplace does this:
- replace all duplicate empty lines with __n__ this marks the paragraphs which we would like to keep
- replace all remaining new lines with a space so all lines are unwrapped to one long line
- replace all paragraph markers __n__ with two empty lines leaving us with single line paragraphs
- which are passed on to TF_Wrap as before...

Karnin
  • Members
  • 22 posts
  • Last active:
  • Joined: 13 Feb 2005

Thanks, you are really awesome...

 

Now it looks like:

> From: Lorem ipsum To: dolor sit amet, CC: consetetur sadipscing BCC:
> elitr sed diam Subject: nonumy eirmod tempor invidunt ut labore et 
> magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
> duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
> sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
> consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
> labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et
> accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
> sea takimata sanctus est Lorem ipsum dolor sit amet.

 

Function needed similar to:

if line length < 60 cols and NOT containing "somewhat", then remove leading `n.

 

For cleaning mail reply lines beginning with From: To: CC: BCC: Subject: must be excluded from processing.

 

Further enhancement:

If there is a double break `n`n , it should be reduced to one break `n to keep sentence structure.

 

Before processing:

> duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus
> est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
> consetetur sadipscing elitr,
> 
> labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et

After processing:

> duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
> sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet,
> consetetur sadipscing elitr,
> labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et

Otherwise it results in an unreadable tapeworm... icon_cool.gif

 

Thx once more!

 

EDIT:

For cleaning mail reply lines beginning with From: To: CC: BCC: Subject: must be excluded from processing.

 

This can be realize with inserting a line break in front of the keywords like From: To: CC: BCC: Subject:

StringReplace clipboard, clipboard, From:, `nFrom:, all

All text without an empty row ahead is processed.

But how to define areas which should be untouched by processing icon_neutral.gif ?



Karnin
  • Members
  • 22 posts
  • Last active:
  • Joined: 13 Feb 2005
Try this
Clipboard:=TF_InsertPrefix(TF_Wrap(RegExReplace(RegExReplace(RegExReplace(Clipboard,"(\r?\n){2,}","__nl__"),"(\r?\n)"," "),"__nl__","`n`n"), 73), "", "", "> ")
The triple nested RegExReplace does this:
- replace all duplicate empty lines with __n__ this marks the paragraphs which we would like to keep
- replace all remaining new lines with a space so all lines are unwrapped to one long line
- replace all paragraph markers __n__ with two empty lines leaving us with single line paragraphs
- which are passed on to TF_Wrap as before...

 

Ok, after some tests following result, 2 separate steps needed:

First step:

Cleaning up mail text and wrapping to specified number of cols.

Second step:

Adjusting incomplete, not filled lines.

 

It must be done in two separate steps because macro doesn't know untouchable mail parts like head, signature...

 

After formatting and cleaning the broken Outlook reply, context of small and not filled lines must be marked, then apply by AHK-hotkey:

#Include tf.ahk

SetTitleMatchMode 2
WinWait, - Nachricht (Nur-Text) , 
IfWinNotActive, - Nachricht (Nur-Text) , , WinActivate, - Nachricht (Nur-Text) , 
WinWaitActive, - Nachricht (Nur-Text) , 
Sleep, 200
Send, {CTRLDOWN}c{CTRLUP}

StringCaseSense On

StringReplace clipboard, clipboard, `n>%A_Space%>%A_Space%>%A_Space%, `n, all
StringReplace clipboard, clipboard, `n>%A_Space%>%A_Space%, `n, all
StringReplace clipboard, clipboard, `n>%A_Space%>, `n, all
StringReplace clipboard, clipboard, `n>%A_Space%, `n, all
StringReplace clipboard, clipboard, `n>`n, `n, all

Clipboard:=TF_InsertPrefix(TF_Wrap(RegExReplace(RegExReplace(RegExReplace(Clipboard,"(\r?\n){2,}","__nl__"),"(\r?\n)"," "),"__nl__","`n`n"), 73), "", "", "> ") 

StringReplace clipboard, clipboard, >%A_Space%>%A_Space%, >%A_Space%, all

Send, {CTRLDOWN}v{CTRLUP}
Sleep, 100
Send, {CTRLDOWN}{HOME}{CTRLUP}

 

Marked text will be reformatted with filled up lines with a leading  > in front of each row, line break at specified col.

 

Thx for help.



iguru42
  • Members
  • 60 posts
  • Last active: May 23 2017 07:07 PM
  • Joined: 02 Sep 2008

I've run into a little problem with TF.
 
I use TF to clean up a text file I that gets created daily:
 

removelines:=TF_Find("!Daily Check.txt",1,0,"Password",0,0)
TF_RemoveLines("!Daily Check.txt", removelines)

removelines:=TF_Find("!Daily Check.txt",1,0,"Command",0,0)
TF_RemoveLines("!Daily Check.txt", removelines)

removelines:=TF_Find("!Daily Check.txt",1,0,"press CANCEL to quit",0,0)
TF_RemoveLines("!Daily Check.txt", removelines)

 

Now what is happening is if the word/phrase that I want to remove is not found, then removelines deletes the entire contents of the file Daily Check.txt.

 

That seems like odd behavior to me, why if it finds no results does it delet everything?

 

TIA



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
I assume because removelines is zero (the digit 0) and I don't think TF expects that or assumes you want to delete from line zero, so if you build in a security check like so
removelines:=TF_Find("!Daily Check.txt",1,0,"Password",0,0)
If (removelines > 0)
	TF_RemoveLines("!Daily Check.txt", removelines)
that should solve it.

ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011

Hi,

 

2 questions:

 

1: Actually the FileAppend is converting any files to ANSI.

Is there any way to add a check if the file is UTF-8 the append will be in UTF-8 and if not in ANSI?

 

2: I try a RegExReplace but without success

Here is my sentence

 

TF_RegExReplace("!" . A_LoopFileName,"im)(*ANYCRLF)¶.*¶$`n","")

 

What I want it's to delete the line (from a Windows file) and the line return (because this ligne is now empty).

Someone to help me with this regex?

 

Thanks in advance.



ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011

For my first question I found a way:

FileEncoding, UTF-8

So now all the files are in UTF-8.

 

But I still think that a test inside TF can be better.



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
Are you sure the ¶.*¶ is part of the file/line. These ¶ characters are usually shown in a text editor to indicate line feeds - just saying. There is a function to remove empty (blank) lines - perhaps that is easier https://ahknet.autoh...emoveBlankLines

It might be easier to work on your regex using a standard RegExReplace with a sample text copied from the file, if that works you can probably use the same working RE in the TF function.

ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011

These characters are seen because they are for the music in some subtitles files.

But I've managed to make the regex working (with `r`n).

 

So the Second point are done, now just remain the first one with the ANSI-UTF thing.



ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011

Hi,

For all the fonctions that changes files (creation, update..) the linefeed are converted into `n or originally it's `r`n (I'm under windows)!!!

 

Any way to do your library to keep the original linefeed ?



Jaff
  • Members
  • 14 posts
  • Last active: Aug 11 2014 12:23 AM
  • Joined: 04 Apr 2014

Hi, i only just found out about TF, as I'm reasonably new to AHK. I need the TF_ReplaceLine function for a script I'm writing, however I need it to replace a line with text that includes a variable.

TF_ReplaceLine("!Parameters.txt",3,3,"DefaultTime=%DH%%DM%")

Unfortunately, this replaces it with a literal %DH%%DM%. Is there any way around this, or does the TF_ReplaceLines not replace lines with variables, as a rule?

 

If there is a way around, or an alternative method, I would greatly appreciate it if anyone would tell me.    :)



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
basic ahk syntax stuff try with TF_ReplaceLine("!Parameters.txt",3,3,"DefaultTime=" DH DM) see also http://ahkscript.org...FAQ.htm#percent so don't use %% around variable names and don't quote them and it should work - see also http://ahkscript.org...s/Variables.htm

rootmos
  • Members
  • 13 posts
  • Last active: Mar 11 2016 09:37 AM
  • Joined: 03 Jul 2013

Im trying to use the code for removing comments in a document/ahk script file, but it's not working as I want it too.

 

Here is the code:

; This includes the TF lib
#include TF.ahk

; Remove all ze s**t
RemoveComments:=TF_Find("!Comments.txt",1,0,";",0,0)
If (RemoveComments > 0) {
	TF_RemoveLines("!Comments.txt", RemoveComments)
	Msgbox Cleaned!
}
else { ; What to do if error
	Msgbox Error
	ExitApp
}

I will remove all comments. BUT it also removed for an example:

; This will be removed
;This will be removed

Msgbox Hello World ; This whole line is removed

Is it possible just to remove the comment-line instead of the whole "Msgbox Hello World ; This whole line is removed"?



rootmos
  • Members
  • 13 posts
  • Last active: Mar 11 2016 09:37 AM
  • Joined: 03 Jul 2013

Double post, please remove this one.



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
It seems

TF_Find(Lines) uses Regular Expressions - https://github.com/hi5/TF#TF_Find
so try with RemoveComments:=TF_Find("!Comments.txt",1,0,"^\s*;",0,0) ; make sure ; is at start of a line