# encoding: ascii
# api: powershell
# title: running regions in ISE
# description: scripting for running regions by hotkeys in powershell v2 CTP3 ISE. http://www.karlprosser.com/coder
# version: 0.1
# type: function
# author: karl prosser
# license: CC0
# function: invoke-caretline
# x-poshcode-id: 776
# x-archived: 2012-04-24T12:58:33
# x-published: 2009-01-03T17:04:00
#
#
function invoke-caretline
{
invoke-expression $([Regex]::Split($psISE.CurrentOpenedFile.Editor.text,"`r`n" )[$psISE.CurrentOpenedFile.Editor.caretline-1])
}
$psISE.CustomMenu.Submenus.Add("Run single line", {invoke-caretline} ,  'f7')
function invoke-region([int] $num)
{
$ed = $psISE.CurrentOpenedFile.Editor
$lines = [Regex]::Split($ed.text,"`r`n" )
$foundfirst = -1
$foundlast = -1
for($count = 0;$count -le $lines.length-1;$count++)
 {
   if ($lines[$count].startswith("#region") -and $lines[$count].contains("@$num")) 
   { $foundfirst = $count;break}     
 }
 if($foundfirst -gt -1)
 {
 for ($count = $foundfirst; $count -le $lines.length-1;$count++)
    {    
    if ($lines[$count].startswith("#endregion") )
   { $foundlast = $count;break}     
    }
    
 if ($foundlast -gt -1)
   {
     $torun = ""
     $lines[$foundfirst..$foundlast] | % { $torun+=$_ + "`r`n"}
     invoke-expression $torun
   }
 }
 
}
 $psISE.CustomMenu.Submenus.Add("run region 1", {invoke-region 1 },  'ctrl+1') 
 $psISE.CustomMenu.Submenus.Add("run region 2", {invoke-region 2 },  'ctrl+2') 
 $psISE.CustomMenu.Submenus.Add("run region 3", {invoke-region 3 },  'ctrl+3') 
 $psISE.CustomMenu.Submenus.Add("run region 4", {invoke-region 4 },  'ctrl+4') 
 $psISE.CustomMenu.Submenus.Add("run region 5", {invoke-region 5 },  'ctrl+5')
