WindowsExplorerSync.psm1

$script:thisDir = Split-Path $MyInvocation.MyCommand.Path -Parent

function LoadShellHook() {
    if (!$global:ShellHook) {
        Add-Type -Path "$script:thisDir\lib\Mastersign.WindowsShell.dll"
        $global:ShellHook = New-Object Mastersign.WindowsShell.ShellHook
    }
}

function GetPathFromUrl([System.Uri]$url)
{
    if ($url.Scheme -eq "file")
    {
        return $url.LocalPath
    }
    throw "The given URL is no filesystem path: $url"
}

function Get-UrlFromWindowsExplorer()
{
    LoadShellHook
    return $global:ShellHook.GetCurrentLocation()
}

function Get-LocationFromWindowsExplorer()
{
    return (GetPathFromUrl (Get-UrlFromWindowsExplorer))
}

function Set-LocationFromWindowsExplorer()
{
    Set-Location (Get-LocationFromWindowsExplorer)
}

function Push-LocationFromWindowsExplorer()
{
    PushHelper
}

function Get-SelectedItemsFromWindowsExplorer()
{
    LoadShellHook
    return $global:ShellHook.GetCurrentSelection()
}

Set-Alias we Set-LocationFromWindowsExplorer
Set-Alias wep Push-LocationFromWindowsExplorer
Set-Alias wel Get-LocationFromWindowsExplorer
Set-Alias wes Get-SelectedItemsFromWindowsExplorer

Set-PSReadlineKeyHandler "Ctrl+Shift+E" { Set-LocationFromWindowsExplorer; [Microsoft.PowerShell.PSConsoleReadLine]::ValidateAndAcceptLine() } `
    -BriefDescription "Set-LocationFromWindowsExplorer" `
    -Description "Set location to that of the last focused open Explorer window or tab."

Set-PSReadlineKeyHandler "Ctrl+Alt+Shift+E" { Push-LocationFromWindowsExplorer; [Microsoft.PowerShell.PSConsoleReadLine]::ValidateAndAcceptLine() } `
    -BriefDescription "Push-LocationFromWindowsExplorer" `
    -Description "Push location to that of the last focused open Explorer window or tab."