Skip to content

Commit

Permalink
gh-468: GuiMacro functions EnvironmentReload and EnvironmentList.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximus5 committed May 26, 2019
1 parent c85110c commit b8e1f41
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/ConEmu/About.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ _DBGHLP(L"-ZoneId - Try to drop :Zone.Identifier without confirmation.\r\n") \
L"Detach([<Flags>])\r\n" \
L" - Detach active RealConsole from ConEmu\r\n" \
L" Flags=1: don't show confirm message\r\n" \
L"EnvironmentReload\r\n" \
L" - Reload environment variables from system registry\r\n" \
L"EnvironmentList\r\n" \
L" - Print actual `name=value` environment pairs\r\n" \
L" Variables from ConEmu settings go after system ones\r\n" \
L"FindEditor(\"<FullEditFileName>\")\r\n" \
L"FindViewer(\"<FullViewerFileName>\")\r\n" \
L"FindFarWindow(<WindowType>,\"<WindowTitle>\")\r\n" \
Expand Down
8 changes: 8 additions & 0 deletions src/ConEmu/ConEmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8292,9 +8292,17 @@ void CConEmuMain::ReloadEnvironmentVariables()
}
}

std::lock_guard<std::mutex> lock(saved_environment_mutex_);
saved_environment_ = new_environment;
}

std::shared_ptr<SystemEnvironment> CConEmuMain::GetEnvironmentVariables() const
{
std::lock_guard<std::mutex> lock(saved_environment_mutex_);
std::shared_ptr<SystemEnvironment> data = saved_environment_;
return data;
}

void CConEmuMain::OnGlobalSettingsChanged()
{
UpdateGuiInfoMapping();
Expand Down
4 changes: 4 additions & 0 deletions src/ConEmu/ConEmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ struct ConsoleInfoArg
};

#include <memory>
#include <mutex>

#include "DwmHelper.h"
#include "TaskBar.h"
#include "FrameHolder.h"
Expand Down Expand Up @@ -850,8 +852,10 @@ class CConEmuMain

public:
void ReloadEnvironmentVariables();
std::shared_ptr<SystemEnvironment> GetEnvironmentVariables() const;
private:
std::shared_ptr<SystemEnvironment> saved_environment_;
mutable std::mutex saved_environment_mutex_;
};

// Message Logger
Expand Down
108 changes: 97 additions & 11 deletions src/ConEmu/Macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,32 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define SHOWDEBUGSTR

#include "Header.h"
#include "../common/EnvVar.h"
#include "../common/MGuiMacro.h"
#include "../common/MStrEsc.h"
#include "../common/ProcessSetEnv.h"
#include "../common/WFiles.h"

#include "RealConsole.h"
#include "VirtualConsole.h"
#include "Options.h"
#include "OptionsClass.h"
#include "TrayIcon.h"
#include <set>

#include "AboutDlg.h"
#include "Attach.h"
#include "ConEmu.h"
#include "TabBar.h"
#include "TrayIcon.h"
#include "ConEmuPipe.h"
#include "Macro.h"
#include "VConGroup.h"
#include "Menu.h"
#include "AboutDlg.h"
#include "Attach.h"
#include "SetDlgButtons.h"
#include "Options.h"
#include "OptionsClass.h"
#include "RealConsole.h"
#include "SetCmdTask.h"
#include "SetColorPalette.h"
#include "SetDlgButtons.h"
#include "SystemEnvironment.h"
#include "TabBar.h"
#include "TrayIcon.h"
#include "TrayIcon.h"
#include "VConGroup.h"
#include "VirtualConsole.h"


/* ********************************* */
Expand Down Expand Up @@ -153,6 +158,9 @@ namespace ConEmuMacro
LPWSTR Debug(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin);
// Detach
LPWSTR Detach(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin);
// Environment variables
LPWSTR EnvironmentReload(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin);
LPWSTR EnvironmentList(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin);
// Найти окно и активировать его. // int nWindowType/*Panels=1, Viewer=2, Editor=3*/, LPWSTR asName
LPWSTR FindEditor(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin);
LPWSTR FindFarWindow(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin);
Expand Down Expand Up @@ -279,6 +287,8 @@ namespace ConEmuMacro
{Copy, {L"Copy"}},
{Debug, {L"Debug"}, gmf_MainThread},
{Detach, {L"Detach"}, gmf_MainThread},
{EnvironmentReload, {L"EnvironmentReload", L"EnvReload"}, gmf_MainThread},
{EnvironmentList, {L"EnvironmentList", L"EnvList"}},
{FindEditor, {L"FindEditor"}},
{FindFarWindow, {L"FindFarWindow"}},
{FindViewer, {L"FindViewer"}},
Expand Down Expand Up @@ -1000,6 +1010,8 @@ void ConEmuMacro::UnitTests()
_ASSERTE(p && p->argc==1 && lstrcmp(p->argv[0].Str,L"def")==0);
SafeFree(p);

CEStr env_list(EnvironmentList(nullptr, nullptr, false));

gbUnitTest = false;
}
#endif
Expand Down Expand Up @@ -1695,6 +1707,80 @@ LPWSTR ConEmuMacro::Context(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin
return lstrdup(L"");
}

LPWSTR ConEmuMacro::EnvironmentReload(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin)
{
gpConEmu->ReloadEnvironmentVariables();
return lstrdup(L"OK");
}

namespace {
void AppendExpandedValue(CEStrConcat& ss, const bool expandable, const wchar_t* data)
{
if (expandable)
{
CEStr value(ExpandEnvStr(data));
if (!value.IsEmpty())
{
ss.Append(value.c_str(L""));
return;
}
}
ss.Append(data);
}
};

LPWSTR ConEmuMacro::EnvironmentList(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin)
{
CEStrConcat ss;

// Pairs `name=value` from registry
std::shared_ptr<SystemEnvironment> data_ptr = gpConEmu->GetEnvironmentVariables();
const auto& data = data_ptr->env_data;
for (const auto& key : data_ptr->GetKeys())
{
const auto& v = data.at(key);
ss.Append(v.name.c_str());
ss.Append(L"=");
AppendExpandedValue(ss, v.expandable, v.data.c_str());
ss.Append(L"\n");
}

// Pairs `name=value` from ConEmu settings
if (gpSet->psEnvironmentSet)
{
CProcessEnvCmd env;
env.AddLines(gpSet->psEnvironmentSet, true);

class Apply : public CStartEnvBase
{
private:
CEStrConcat& ss_;
public:
Apply(CEStrConcat& ss) : ss_(ss) {}
void Alias(LPCWSTR asName, LPCWSTR asValue) override {}
void ChCp(LPCWSTR asCP) override {}
void Echo(LPCWSTR asSwitches, LPCWSTR asText) override {}
void Title(LPCWSTR asTitle) override {}
void Type(LPCWSTR asSwitches, LPCWSTR asFile) override {}
void Set(LPCWSTR asName, LPCWSTR asValue) override
{
if (asName && *asName && asValue)
{
ss_.Append(asName);
ss_.Append(L"=");
AppendExpandedValue(ss_, true, asValue);
ss_.Append(L"\n");
}
}
};

Apply apply(ss);
env.Apply(&apply);
}

return ss.GetData().Detach();
}

// Найти окно и активировать его. // LPWSTR asName
LPWSTR ConEmuMacro::FindEditor(GuiMacro* p, CRealConsole* apRCon, bool abFromPlugin)
{
Expand Down
10 changes: 10 additions & 0 deletions src/ConEmu/SystemEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ void SystemEnvironment::LoadFromRegistry()
SysEnvValueCallback, (LPARAM)this, true);
}

std::set<std::wstring> SystemEnvironment::GetKeys() const
{
std::set<std::wstring> keys;
for (const auto& v : env_data)
{
keys.insert(v.first);
}
return keys;
}

std::wstring SystemEnvironment::MakeEnvName(const std::wstring& s)
{
// Windows environment names are case-insensitive
Expand Down
4 changes: 3 additions & 1 deletion src/ConEmu/SystemEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once

#include <unordered_map>
#include <set>
#include <string>
#include <unordered_map>

// The class is not thread-safe
struct SystemEnvironment
{
void LoadFromRegistry();
std::set<std::wstring> GetKeys() const;

static std::wstring MakeEnvName(const std::wstring& s);
static bool WINAPI SysEnvValueCallback(HKEY hk, LPCWSTR pszName, DWORD dwType, LPARAM lParam);
Expand Down
23 changes: 23 additions & 0 deletions src/common/CEStr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,26 @@ char* CEStrA::release()
clear(); // JIC
return ptr;
}

// CEStrConcat
void CEStrConcat::Append(const wchar_t* str)
{
if (!str || !*str) return;
CEStr new_str(str);
ssize_t new_len = new_str.GetLen();
total_ += new_len;
strings_.push_back({new_len, std::move(new_str)});
}

CEStr CEStrConcat::GetData() const
{
CEStr result;
wchar_t* buffer = result.GetBuffer(total_);
for (const auto& str : strings_)
{
wmemmove_s(buffer, str.first, str.second, str.first);
buffer += str.first;
}
*buffer = 0;
return result;
}
12 changes: 12 additions & 0 deletions src/common/CEStr.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once

#include "defines.h"
#include "MArray.h"

// CEStr
struct CEStr
Expand Down Expand Up @@ -109,3 +110,14 @@ struct CEStrA
public:
char* ms_Val = nullptr;
};

// Implements string concatenation
class CEStrConcat
{
private:
MArray<std::pair<ssize_t, CEStr>> strings_;
ssize_t total_ = 0;
public:
void Append(const wchar_t* str);
CEStr GetData() const;
};
8 changes: 8 additions & 0 deletions src/common/MArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class MArray
{
return data.end();
};
const_iterator begin() const
{
return data.cbegin();
};
const_iterator end() const
{
return data.cend();
};
const_iterator cbegin() const
{
return data.cbegin();
Expand Down

0 comments on commit b8e1f41

Please sign in to comment.