AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to include a specific function?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
TomXIII



Joined: 14 Apr 2009
Posts: 182

PostPosted: Fri Jan 15, 2010 2:13 pm    Post subject: How to include a specific function? Reply with quote

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
View user's profile Send private message
BoffinbraiN



Joined: 16 Nov 2009
Posts: 114

PostPosted: Fri Jan 15, 2010 2:18 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
TomXIII



Joined: 14 Apr 2009
Posts: 182

PostPosted: Fri Jan 15, 2010 5:26 pm    Post subject: Reply with quote

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
View user's profile Send private message
BoffinbraiN



Joined: 16 Nov 2009
Posts: 114

PostPosted: Fri Jan 15, 2010 5:30 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
TomXIII



Joined: 14 Apr 2009
Posts: 182

PostPosted: Fri Jan 15, 2010 5:42 pm    Post subject: Reply with quote

OOuuupppsss!
I thought it would be usefull to avoid many files creation.
But.. I agree with your comment!
Back to top
View user's profile Send private message
BoffinbraiN



Joined: 16 Nov 2009
Posts: 114

PostPosted: Fri Jan 15, 2010 8:11 pm    Post subject: Reply with quote

Haha... I'm glad you agree. Wink

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. Very Happy
Back to top
View user's profile Send private message Visit poster's website
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Fri Jan 15, 2010 9:06 pm    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group