#NoTrayIcon
If $CmdLine[0] = 0 Then Exit
$sScriptFile = $CmdLine[1]
If $CmdLine[0] > 1 Then
$sSelectedCode = StringRegExpReplace($CmdLineRaw, "/Selection:(.*)$", "\1")
Else
$sSelectedCode = ClipGet()
EndIf
$sScriptRead = FileRead($sScriptFile)
$sScriptContent = StringRegExpReplace($sScriptRead, "(?is)[\r\n]+?\h*Func\h+\w+\(.*EndFunc", "")
$sFuncsContent = StringRegExpReplace($sScriptRead, "(?is).*(\h*Func\h+\w+\(.*EndFunc[^\r\n]*).*", "\1")
$sVars = _GenerateUnDeclaredVarsList($sSelectedCode, $sScriptContent, $sFuncsContent, "Local")
$hGUI = GUICreate("UnDeclared Vars List", 400, 120)
GUICtrlCreateLabel("Copy undeclared variables list?", 20, 20)
$nVars_Input = GUICtrlCreateInput($sVars, 20, 50, 360, 20)
$nOK_Btn = GUICtrlCreateButton("OK", 20, 90, 70, 20)
$nCancel_Btn = GUICtrlCreateButton("Cancel", 100, 90, 70, 20)
GUISetState()
While 1
Switch GUIGetMsg()
Case -3, $nCancel_Btn
Exit
Case $nOK_Btn
ClipPut(GUICtrlRead($nVars_Input))
Exit
EndSwitch
WEnd
Func _GenerateUnDeclaredVarsList($sSelCode, $sScriptContent, $sFuncsContent, $sDimMethod = "Local")
Local $sFuncParamsLine, $aVars, $sVars, $i
If StringRegExp($sSelCode, 'Func\h*\w+\(.*\)(?s).*\r?\n\h*Func\h*\w+\(.*\)') Then
MsgBox(48, @ScriptName, 'Please select only ONE function (including the "Func Name()" part).')
Exit
EndIf
$sFuncParamsLine = StringRegExpReplace($sSelCode, '(?si).*(?:\r?\n)?\h*Func\h*\w+\(((?-s).*)\).*', '\1')
If @error Or @extended = 0 Then
MsgBox(48, @ScriptName, 'Please select function (including the "Func Name()" part).')
Exit
EndIf
$sSelCode = StringRegExpReplace($sSelCode, '(?si)(?:\r?\n)?\h*Func\h*\w+\(.*?\)', '')
$aVars = StringRegExp($sSelCode, "[^\(=,]?(\$\w+)\h*(?:\[[^\[\]]+\])?\h*?=", 3)
$sVars = ""
For $i = 0 To UBound($aVars)-1
If Not StringRegExp($sFuncParamsLine, "(,\h*)?\Q" & $aVars[$i] & "\E(,\h*|\h*=)?") And _
Not StringRegExp($sScriptContent, "(?i)[\r\n]*?\h*?Global\h+(\$\w+,\h*?)?\Q" & $aVars[$i] & "\E") And _
Not StringInStr("," & $sVars & ",", "," & $aVars[$i] & ",") Then
$sVars &= $aVars[$i] & ","
EndIf
Next
$sVars = StringRegExpReplace($sVars, ",+$", "")
$sVars = StringRegExpReplace($sVars, ",", ", ")
If $sVars = '' Then
MsgBox(48, @ScriptName, 'Variables not found.')
Exit
EndIf
Return $sDimMethod & " " & $sVars
EndFunc