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 

IncludeExpander

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
TeeJay
Guest





PostPosted: Wed May 03, 2006 7:37 am    Post subject: IncludeExpander Reply with quote

I hereby happily comply with PhiLho's request to post my little tool:

It pastes all occurances of #Include (only 1st, of course) and #IncludeAgain of a interactively selected script to an output file.

It does not work with the '-i'-option, though.

Here it goes, hope it helps you too Wink :

Code:

; ************************************************************
; **
; ** Purpose:       Expand all occurances of #Include(Again) in AHK scripts
; **
; ** Prerequisites: tested with AHK versions 1.0.26.01 and 1.0.43.09
; **                on Win98SE, WinME, WinXP_SP2, Win2003_SP1
; **
; **                known deficiency: file names to be included
; **                must comply with rules for variable names
; **                (e.g. "test-id.ahk" will not work)
; **
; ** Author:        TeeJay
; **
; ** History:
; **                2006-04-30 Genesis
; **                2006-05-03 Beautification of Script
; **                           and Testing on different platforms
; **                2006-05-04 Processing of '-i'-option
; **
; ************************************************************

GoSub InitScript
GoSub ScriptAction
Goto  EndofScript

; ************************************************************
; start business
ScriptAction:
GoSub SelectFileNames

if ("" <> SaveToFile)
{
  ; create array of file names with initial file
  OpenFile = 0
  OrigScriptFile%OpenFile% := OrigScriptFile
  OrigScriptFile =
  GoSub AnalyzeAHK
}
SaveToFile =
return

; ************************************************************
; check status of commenting
CheckComment:
if (0 = comment)
{
  IfInString, PutString, %startComment%
  {
    comment = 1
  }
}
else
{
  IfInString, PutString, %endComment%
  {
    comment = 0
  }
}
return

; ************************************************************
; change of comment status within this line?
CheckCommentInLine:
StringGetPos, commpos, PutString, %endComment%
if (0 = ErrorLevel)
{
  ; found endComment
  if (pos < commpos)
  {
    validInclude = 0
  }
}
else
{
  StringGetPos, commpos, PutString, `;
  if (0 = ErrorLevel)
  {
    if (pos > commpos)
    {
      validInclude = 0
    }
  }
}
return

; ************************************************************
; get filename to include (= to paste)
FindIncludeName:
GoSub CheckComment

if (0 = comment)
{
  ; what type of include?
  StringGetPos, commpos, PutString, #IncludeAgain
  if (0 = ErrorLevel)
  {
    again = 1
  }
  else
  {
    again = 0
    StringGetPos, pos, PutString, #Include
  }

  ; within or outside comment?
  GoSub CheckCommentInLine
  if (1 = validInclude)
  {
    ; "#Include "      =  9 characters
    ; "#IncludeAgain " = 14 characters
    if (1 = again)
    {
      pos += 14
    }
    else
    {
      pos += 9
    }

    ; handle '-i'-option
    StringGetPos, ipos, PutString, -i
    if (0 = ErrorLevel)
    {
      if (pos = ipos)
      {
        pos +=3
      }
    }

    ; some bureaucracy (array handling)
    StringTrimLeft, incname, PutString, %pos%
    OpenFile++
    OrigScriptFile%OpenFile% := incname
    StringTrimRight, incname, incname, 4
    tmpcnt := Counter%incname%
    if ("" = tmpcnt)
    {
      Counter%incname% = 1
    }
    else
    {
      Counter%incname% += 1
    }
    pos =
    tmpcnt =
  }
}
return

; ************************************************************
; actual analyzing of script
; OrigScriptFile - array of file names (-> nesting; recursively)
; OpenFile       - index

AnalyzeAHK:
IncludeFile := OrigScriptFile%OpenFile%

IfExist, %IncludeFile%
{
  Loop, READ, %IncludeFile%
  {
    PutString = %A_LoopReadLine%
    ; check whether in comment or not
    GoSub CheckComment

    if (0 = comment)
    {
      IfInString, PutString, #Include
      {
        ; get filename to paste
        GoSub FindIncludeName

        if (1 = validInclude)
        {
          ; get current file name and its occurance
          IncludeFile := OrigScriptFile%OpenFile%
          Counter := Counter%incname%
          IncludeComment = `; ## %whoami%: %Counter%. occurance of "%IncludeFile%"

          ; write to expanded file
          PutString = %Separator%`n%IncludeComment%`n`; -- %PutString% --
          If (1 = Counter) or (1 = again)
          {
            PutString = %PutString%`n%DoubleSeparator%`n`; ########## BEGIN pasting of "%IncludeFile%"`n%Separator%
            FileAppend, %PutString%`n, %SaveToFile%

            GoSub AnalyzeAHK
            PutString = %Separator%`n`; ########## END   pasting of "%IncludeFile%"`n%DoubleSeparator%
          }
          else
          {
            PutString = %PutString%`n%Separator%
          }
          OpenFile--
          IncludeFile := OrigScriptFile%OpenFile%
        }
        else
        {
          ; reset flag for next occurance
          validInclude = 1
        }
      }
    }
    FileAppend, %PutString%`n, %SaveToFile%
  }
}
else
{
  ; did you actually run this script?? :)
  FileAppend, `;  >>> Could not locate "%IncludeFile%" <<<`n, %SaveToFile%
}
return

; ************************************************************
; Let user select interactively script to expand
SelectFileNames:
FileSelectFile, OrigScriptFile, 1, %A_ScriptDir%, %whoami%: Select File to Expand, *.ahk
if ("" <> OrigScriptFile)
{
  StringTrimRight, ExpandedFile, OrigScriptFile, 4
  ExpandedFile = %ExpandedFile%_ExpandedIncludes.ahk
  InputBox, SaveToFile, %whoami%, Filename to Expand into?,,,,,,,, %ExpandedFile%

  IfExist, %SaveToFile%
  {
    MsgBox, 292, %whoami%, File exists already...`nFilename: "%SaveToFile%"`nOK to overwrite?
    IfMsgBox, No
    {
      SaveToFile =
    }
    else
    {
      FileDelete, %SaveToFile%
    }
  }
  ExpandedFile =
}
return

; ************************************************************
; Initialization
InitScript:
whoami = %A_ScriptName%
StringTrimRight, whoami, whoami, 4
Separator = `; ######################################################################
DoubleSeparator = %Separator%`n%Separator%
comment = 0
startComment := "/*"
endComment := "*/"
validInclude = 1
return

; ************************************************************
; Bye Bye
EndOfScript:
ExitApp
Back to top
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Wed May 03, 2006 8:10 am    Post subject: Reply with quote

Thanks. I had just a quick look at the code (look nice) and not tested it yet, but it may be handy when time will come.
Today, I just saw a problem with placement of an include file: if it has an auto-execute part, and hotkey definitions, and has to be included in a similar script, it has to be just in the middle, and of course I can include only one of such file! That, or I must split the included file in two part.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
TeeJay
Guest





PostPosted: Wed May 03, 2006 4:09 pm    Post subject: Reply with quote

I now added processing of '-i'-option...

(see post at top for updated version)
Back to top
Peter



Joined: 30 Dec 2005
Posts: 279

PostPosted: Wed May 03, 2006 5:13 pm    Post subject: Reply with quote

Thanks for sharing the code.
BTW, if you log in you can edit your posts. Keeps a better overview instead of making a post for every version. Wink
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions 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