 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
TomXIII
Joined: 14 Apr 2009 Posts: 182
|
Posted: Fri Jan 15, 2010 2:13 pm Post subject: How to include a specific function? |
|
|
Hi everyone!
I wanna know if there is a way to include/add a specific function from another ahk file (without including the rest of the other file) ? |
|
| Back to top |
|
 |
BoffinbraiN
Joined: 16 Nov 2009 Posts: 114
|
Posted: Fri Jan 15, 2010 2:18 pm Post subject: |
|
|
| I don't think you can. If you are trying to re-use code from other projects, you may want to extract the function into a separate file, and import it from both files. Creating little libraries this way is quite effective. :) |
|
| Back to top |
|
 |
TomXIII
Joined: 14 Apr 2009 Posts: 182
|
Posted: Fri Jan 15, 2010 5:26 pm Post subject: |
|
|
I wrote a simple "Import" function to extract specified functions
| Code: | file = %A_ScriptDir%\lib.ahk
function = FuncA ;or FuncC, FuncA
funcDelimiter := ""
contentToAdd := Import(file, function, funcDelimiter)
If not contentToAdd
MsgBox, Nothing found or an error occured!
Else
MsgBox, Text found:%contentToAdd%
Import(file, function="All", funcDelimiter="")
{
If not FileExist(file)
Return
FileRead, fileContent, %file%
If (function="All" or function="*")
foundContent := fileContent
Else
{
Loop, parse, function, CSV
{
funcDelimiter := funcDelimiter ? funcDelimiter : ";EndFunc"
regExp = s)%A_LoopField%\(.+?%funcDelimiter%
RegExMatch( fileContent, regExp, matched)
If not (matched and A_LoopField)
Continue
foundContent := foundContent ? foundContent . "`n`n" . matched : matched
}
}
Return, foundContent ? "`n" . foundContent : ""
} |
RegExp means: Match all lines that begins with %A_LoopField% (FuncA) and ends with ;EndFunc (by default)
Write these lines on another file named "lib.ahk"
| Code: | FuncA()
{
MsgBox, %A_ThisFunc%
};EndFunc
FuncB()
{
MsgBox, %A_ThisFunc%
};EndFunc
FuncC()
{
MsgBox, %A_ThisFunc%
};EndFunc |
May be, it needs some improvements |
|
| Back to top |
|
 |
BoffinbraiN
Joined: 16 Nov 2009 Posts: 114
|
Posted: Fri Jan 15, 2010 5:30 pm Post subject: |
|
|
Ouch, ouch, ouch. I can't believe I just saw that. Why would you do that? That's the biggest butchering of code I've seen in a long time. Not only is it ridiculously inefficient, but it's also prone to spectacular failure. I plead for you to cease in this crazy magic immediately!
Seriously... Separating out your helper functions and packages into separate files is easy, modular, easy to maintain, and reflects good programming practise. |
|
| Back to top |
|
 |
TomXIII
Joined: 14 Apr 2009 Posts: 182
|
Posted: Fri Jan 15, 2010 5:42 pm Post subject: |
|
|
OOuuupppsss!
I thought it would be usefull to avoid many files creation.
But.. I agree with your comment! |
|
| Back to top |
|
 |
BoffinbraiN
Joined: 16 Nov 2009 Posts: 114
|
Posted: Fri Jan 15, 2010 8:11 pm Post subject: |
|
|
Haha... I'm glad you agree.
Having many library files is fine, as long as they all do something useful and are named and organised appropriately.
The other really ironic thing about your magic include function is that... you would have to #include it to use it.  |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Fri Jan 15, 2010 9:06 pm Post subject: |
|
|
This is what I used in my Thread() function, a little modified.
It is used to reuse the labels and function in other threads and processes. | Code: | Script:="#NoTrayIcon`nMsgBox randomized script`nLabel:LabelEnd`nFunc{}`nExit:ExitEnd"
RunTempScript(CreateScript(Script),"MyScript")
ExitApp
Label:
;some routines
SetBatchLines,-1
Func("Function Hallo")
Gosub, Exit
LabelEnd:
Return
Exit:
MsgBox Label Exit`nExiting now
ExitApp
ExitEnd:
Return
Func(a){
MsgBox % a
}
CreateScript(script){
static mScript,FileExtract_ToMem:="FileExtract_ToMem"
StringReplace,script,script,`n,`r`n,A
StringReplace,script,script,`r`r,`r,A
If RegExMatch(script,"m)^[^:]+:[^:]+|[a-zA-Z0-9#_@]+\{}$"){
If !(mScript){
If (A_IsCompiled and IsFunc(FileExtract_ToMem)){
MsgBox fileex
pData:=0,DataSize:=0
If (%FileExtract_ToMem%(">AUTOHOTKEY SCRIPT<", pData, DataSize)
|| %FileExtract_ToMem%(">AHK WITH ICON<", pData, DataSize)){
ListVars
MsgBox
VarSetCapacity(mScript,DataSize)
DllCall("lstrcpyn", "str", mScript, "uint", pData, "int", DataSize+1)
VarSetCapacity(mScript,-1)
StringReplace,mScript,mScript,`n,`r`n,A
StringReplace,mScript,mScript,`r`r,`r,A
mScript .="`r`n"
}
} else {
FileRead,mScript,%A_ScriptFullPath%
StringReplace,mScript,mScript,`n,`r`n,A
StringReplace,mScript,mScript,`r`r,`r,A
mScript .= "`r`n"
}
}
Loop,Parse,script,`n,`r
{
If A_LoopField=
Continue
else if A_Index=1
script=
If (RegExMatch(A_LoopField,"^[^:\s]+:[^:\s=]+$")){
StringSplit,label,A_LoopField,:
If (label0=2 and IsLabel(label1) and IsLabel(label2)){
script .=SubStr(mScript
, ErrorLevel:=InStr(mScript,"`r`n" label1 ":`r`n")
, InStr(mScript,"`r`n" label2 ":`r`n")-ErrorLevel) . "`r`n"
}
} else if RegExMatch(A_LoopField,"^[^\{}\s]+\{}$"){
StringTrimRight,label,A_LoopField,2
script .= SubStr(mScript
, h:=RegExMatch(mScript,"i)\R" label "\([^)\R]*\)\R?\{")
, RegExMatch(mScript,"\R\s*}\s*\K\R",1,h)-h) . "`r`n"
} else
script .= A_LoopField "`r`n"
}
}
StringReplace,script,script,`r`n,`n,All
Return Script
}
RunTempScript(TempScript, name="")
{
global #__AHK_EXE_
If Name =
#__PIPE_NAME_ := A_TickCount
Else
#__PIPE_NAME_ := name
#__PIPE_GA_ := CreateNamedPipe(#__PIPE_NAME_, 2)
#__PIPE_ := CreateNamedPipe(#__PIPE_NAME_, 2)
if (#__PIPE_=-1 or #__PIPE_GA_=-1) {
MsgBox CreateNamedPipe failed.
Return
}
Run %A_AhkPath% "\\.\pipe\%#__PIPE_NAME_%",,UseErrorLevel HIDE, PID
If ErrorLevel
MsgBox, 262144, ERROR,% "Could not open file:`n" #__AHK_EXE_ """\\.\pipe\" #__PIPE_NAME_ """"
DllCall("ConnectNamedPipe","uint",#__PIPE_GA_,"uint",0)
DllCall("CloseHandle","uint",#__PIPE_GA_)
DllCall("ConnectNamedPipe","uint",#__PIPE_,"uint",0)
script := chr(239) . chr(187) . chr(191) . TempScript
if !DllCall("WriteFile","uint",#__PIPE_,"str",script,"uint",StrLen(script)+1,"uint*",0,"uint",0)
MsgBox WriteFile failed: %ErrorLevel%/%A_LastError%
DllCall("CloseHandle","uint",#__PIPE_)
Return PID
}
CreateNamedPipe(Name, OpenMode=3, PipeMode=0, MaxInstances=255) {
return DllCall("CreateNamedPipe","str","\\.\pipe\" Name,"uint",OpenMode
,"uint",PipeMode,"uint",MaxInstances,"uint",0,"uint",0,"uint",0,"uint",0)
} |
_________________ AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|