AutoHotkey Community

It is currently May 27th, 2012, 1:41 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: January 15th, 2010, 3:13 pm 
Offline

Joined: April 14th, 2009, 10:40 am
Posts: 182
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) ?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 3:18 pm 
Offline

Joined: November 16th, 2009, 9:24 am
Posts: 114
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. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 6:26 pm 
Offline

Joined: April 14th, 2009, 10:40 am
Posts: 182
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 6:30 pm 
Offline

Joined: November 16th, 2009, 9:24 am
Posts: 114
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 6:42 pm 
Offline

Joined: April 14th, 2009, 10:40 am
Posts: 182
OOuuupppsss!
I thought it would be usefull to avoid many files creation.
But.. I agree with your comment!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 9:11 pm 
Offline

Joined: November 16th, 2009, 9:24 am
Posts: 114
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. :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 15th, 2010, 10:06 pm 
Offline

Joined: June 18th, 2008, 8:36 am
Posts: 4923
Location: AHK Forum
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:


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], bobbysoon, JSLover, Tipsy3000 and 20 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