From a57c11e171f97eb8cc6a10b61053731d2940316a Mon Sep 17 00:00:00 2001 From: Manuel Tanzer Date: Fri, 4 Sep 2015 22:16:30 +0200 Subject: [PATCH] Enable the '/single' switch by using the registry as statemachine for the current location ('CMDER_START') Fixed issue #577 --- launcher/src/CmderLauncher.cpp | 25 +++++++++++++++++++------ vendor/profile.ps1 | 23 +++++++++++++++++++---- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/launcher/src/CmderLauncher.cpp b/launcher/src/CmderLauncher.cpp index 49f836a..1ef0b59 100644 --- a/launcher/src/CmderLauncher.cpp +++ b/launcher/src/CmderLauncher.cpp @@ -18,7 +18,7 @@ #define SHELL_MENU_REGISTRY_PATH_BACKGROUND L"Directory\\Background\\shell\\Cmder" #define SHELL_MENU_REGISTRY_PATH_LISTITEM L"Directory\\shell\\Cmder" -#define streqi(a, b) (_wcsicmp((a), (b)) == 0) +#define streqi(a, b) (_wcsicmp((a), (b)) == 0) #define WIDEN2(x) L ## x #define WIDEN(x) WIDEN2(x) @@ -29,7 +29,7 @@ void ShowErrorAndExit(DWORD ec, const wchar_t * func, int line) { wchar_t * buffer; - if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, ec, 0, (LPWSTR) &buffer, 0, NULL) == 0) { buffer = L"Unknown error. FormatMessage failed."; @@ -129,17 +129,30 @@ void StartCmder(std::wstring path, bool is_single_mode) } } - if (is_single_mode) + if (is_single_mode) { swprintf_s(args, L"/single /Icon \"%s\" /Title Cmder", icoPath); } - else + else { swprintf_s(args, L"/Icon \"%s\" /Title Cmder", icoPath); } - SetEnvironmentVariable(L"CMDER_ROOT", exeDir); - SetEnvironmentVariable(L"CMDER_START", path.c_str()); + SetEnvironmentVariable(L"CMDER_ROOT", exeDir); + //SetEnvironmentVariable(L"CMDER_START", path.c_str()); + + // Send out the Settings Changed message - Once using ANSII... + //SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment", SMTO_ABORTIFHUNG, 5000, NULL); + + // ...and once using UniCode (because Windows 8 likes it that way). + //SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) L"Environment", SMTO_ABORTIFHUNG, 5000, NULL); + + HKEY cmderStartRegistryKey; + if (RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\cmder", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &cmderStartRegistryKey, 0) == ERROR_SUCCESS) + { + RegSetValueEx(cmderStartRegistryKey, L"CMDER_START", 0, REG_SZ, (const BYTE*) path.c_str(), path.size() * 2); + RegCloseKey(cmderStartRegistryKey); + } STARTUPINFO si = { 0 }; si.cb = sizeof(STARTUPINFO); diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index cc3850c..9591666 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -61,10 +61,25 @@ if ($gitStatus) { } # Move to the wanted location -if (Test-Path Env:\CMDER_START) { - Set-Location -Path $Env:CMDER_START -} elseif ($Env:CMDER_ROOT -and $Env:CMDER_ROOT.StartsWith($pwd)) { - Set-Location -Path $Env:USERPROFILE +$cmderStartKey = 'HKCU:\Software\cmder' +$cmderStartSubKey = 'CMDER_START' + +$cmderStart = (Get-Item -Path $cmderStartKey).GetValue($cmderStartSubKey) +if ( $cmderStart ) { + $cmderStart = ($cmderStart).Trim('"').Trim("'") + if ( $cmderStart.EndsWith(':') ) { + $cmderStart += '\' + } + + if ( ( Get-Item $cmderStart -Force ) -is [System.IO.FileInfo] ) { + $cmderStart = Split-Path $cmderStart + } + + Set-Location -Path "${cmderStart}" + + Set-ItemProperty -Path $cmderStartKey -Name $cmderStartSubKey -Value $null +} else { + Set-Location -Path "${env:HOME}" } # Enhance Path