I started writing this function to gain a little more access over inserting lines of texts into an edit control, and then I realized that hugov's
TF library already offers the solution, but I finished it anyway. It's similar to TF_InsertLine/TF_ReplaceLine/TF_InsertPrefix/TF_insertSuffixx in functionality, only it's not as advanced as those.
It offers you an option to insert text before/after/replace a line of text, or N lines before/after the whole text.
I benchmarked TF_InsertLine and my function with a simple line inserting operation - if only to justify the time spent on coding it

- and the results show (as it's mentioned in the
TF.ahk documentation) that the tf function's a bit slower with short texts, and faster with large texts. At 6MB and ca. 17000 lines of text the benchmark results were roughly the same, after that the tf function was clearly faster.
Anyway, I suggest you use the related TF functions to do serious stuff, as they're better thought through, more robust, and offer a lot more functionality, but if you feel like giving a shot to mine, go right ahead.
InsertToText.ahk
Code:
InsertToText(InputText,InsertText,InsertMode,RowNumber=0){
;// Error check
if (!InputText || (!Instr(InsertMode,"text") && !RowNumber) || !InsertMode) ;blank input, missing rownumber, missing insertmode
return 0
if RowNumber is not integer ;invalid rownumber
return 0
if InsertMode not in before,after,replace,before text,after text ;invalid insertmode
return 0
if !InsertText ;nothing to insert
return InputText
;// Insert before/after text
if InStr(InsertMode,"text") {
if RowNumber
Loop, % RowNumber
LineFeed .= "`n"
OutputText := InStr(InsertMode,"before") ? InsertText "`n" LineFeed "" InputText : InputText "`n" LineFeed "" InsertText
}
;// Insert before/after/replace line
else
Loop, Parse, InputText, `n
{
if (a_index=1)
LineFeed := ""
else
LineFeed := "`n"
if (a_index=RowNumber)
if (InsertMode="before")
Outputtext .= LineFeed InsertText "`n" A_LoopField
else if (InsertMode="after")
OutputText .= LineFeed A_LoopField "`n" InsertText
else
OutputText .= LineFeed InsertText
else
OutputText .= LineFeed A_Loopfield
}
return OutputText
}
Parameters:
Code:
InputText - The input text to manipulate.
InsertText - The text to insert to InputText.
InsertMode - Specify:
* "before" or "after" to insert a line of text, before/after RowNumber
* "replace" to replace the RowNumber-th line
* "before text" or "after text" to insert InsertText RowNumber Lines before/after the whole InputText.
RowNumber - Use it as described above, you can leave it blank if you use "before text" or "after text" as InsertMode:
In this case InsertText will be inserted right before/after InputText.
Examples:
Code:
a_list=
(
item1
item2
item3
item4
)
MsgBox, % InsertToText(a_list,"new_item","before",2)
MsgBox, % InsertToText(a_list,"new_item","after",2)
MsgBox, % InsertToText(a_list,"new_item","replace",2)
MsgBox, % InsertToText(a_list,"new_item","before text")
MsgBox, % InsertToText(a_list,"new_item","after text")
MsgBox, % InsertToText(a_list,"new_item","before text",10)
MsgBox, % InsertToText(a_list,"new_item","after text",10)
HugoV's
Textfile & String Library
http://www.autohotkey.com/forum/viewtopic.php?t=46195
Cheers
gahks