Files
text_editor/src/plugin_config.cpp

157 lines
5.8 KiB
C++

#if PLUGIN_CONFIG
BufferID GlobalConfigBufferID;
void CMD_OpenConfig() {
Buffer *buffer = GetBuffer(GlobalConfigBufferID);
Open(buffer->name);
} RegisterCommand(CMD_OpenConfig, "", "Open the global config file");
void CMD_OpenConfigOptions() {
BSet main = GetBSet(PrimaryWindowID);
JumpTempBuffer(&main);
NextActiveWindowID = main.window->id;
For (Variables) {
RawAppendf(main.buffer, "\n:Set %-50S ", it.name);
switch(it.type) {
case VariableType_Color: RawAppendf(main.buffer, "%x", it.color->value); break;
case VariableType_String: RawAppendf(main.buffer, "'%S'", *it.string); break;
case VariableType_Int: RawAppendf(main.buffer, "%lld", (long long)*it.i); break;
case VariableType_Float: RawAppendf(main.buffer, "%f", *it.f); break;
default: InvalidCodepath();
}
}
For (GlobalCommands) {
RawAppendf(main.buffer, "\n:Set %-50S '%S'", it.name, it.binding);
}
} RegisterCommand(CMD_OpenConfigOptions, "", "List available variables and associated documentation inside the command window");
void EvalCommandsLineByLine(BSet set) {
WindowID save_last = PrimaryWindowID;
WindowID save_active = ActiveWindowID;
WindowID save_next = NextActiveWindowID;
Caret save_caret = set.view->carets[0];
ActiveWindowID = set.window->id;
PrimaryWindowID = set.window->id;
NextActiveWindowID = set.window->id;
for (Int i = 0; i < set.buffer->line_starts.len; i += 1) {
Range range = GetLineRangeWithoutNL(set.buffer, i);
String16 string = GetString(set.buffer, range);
string = Trim(string);
if (string.len == 0) {
continue;
}
if (StartsWith(string, u"//")) {
continue;
}
Open(string);
}
set.view->carets[0] = save_caret;
PrimaryWindowID = save_last;
ActiveWindowID = save_active;
NextActiveWindowID = save_next;
}
void CMD_EvalCommandsLineByLine() {
BSet set = GetBSet(PrimaryWindowID);
EvalCommandsLineByLine(set);
} RegisterCommand(CMD_EvalCommandsLineByLine, "", "Goes line by line over a buffer and evaluates every line as a command, ignores empty or lines starting with '//'");
BufferID LoadConfig(String config_path) {
ReportConsolef("Loading config %S...", config_path);
Window *window = GetWindow(NullWindowID);
ViewID active_view_before = window->active_view;
View *view = WindowOpenBufferView(window, config_path);
Buffer *buffer = GetBuffer(view->active_buffer);
EvalCommandsLineByLine({window, view, buffer});
if (window->active_view == view->id) {
window->active_view = active_view_before;
}
return buffer->id;
}
#define ExpectP(x, ...) \
if (!(x)) { \
ReportErrorf("Failed to parse command " __VA_ARGS__); \
return; \
}
void Set(String string) {
String name = SkipIdent(&string);
ExpectP(name.len != 0, "expected a variable name, instead got '%S'", string);
Variable *var = GetVariable(name);
if (var) {
SkipWhitespace(&string);
if (var->type == VariableType_String) {
char c = At(string, 0);
ExpectP(c == u'"' || c == u'\'', "Expected string to follow the command name, instead got %S", string);
string = Skip(string, 1);
String quote = SkipUntil(&string, {&c, 1});
ExpectP(At(string, 0) == c, ":Set %S <error here>, unclosed quote", name);
ReportConsolef(":Set %S %c%S%c", name, c, quote, c);
*var->string = Intern(&GlobalInternTable, quote);
} else if (var->type == VariableType_Int) {
ExpectP(IsDigit(At(string, 0)), "Expected an integer to follow the command name, instead got: %S", string);
Int number = SkipInt(&string);
ReportConsolef(":Set %S %lld", name, number);
*var->i = number;
} else if (var->type == VariableType_Float) {
ExpectP(IsDigit(At(string, 0)), "Expected float to follow the command name, instead got: %S", string);
Float number = SkipFloat(&string);
ReportConsolef(":Set %S %f", name, number);
*var->f = number;
} else if (var->type == VariableType_Color) {
ExpectP(IsHexDigit(At(string, 0)), "Expected hex integer to follow the command name, instead got: %S", string);
String begin = {string.data, 0};
while (IsHexDigit(At(string, 0))) {
string = Skip(string, 1);
begin.len += 1;
}
ReportConsolef(":Set %S %S", name, begin);
var->color->value = (uint32_t)strtoll(begin.data, NULL, 16);
} ElseInvalidCodepath();
if (name == "FontSize" || name == "PathToFont") {
ReloadFont(PathToFont, (U32)FontSize);
}
#if PLUGIN_PROJECT_MANAGEMENT
if (name == "ProjectFolder") {
SetProjectFolder(*var->string);
}
#endif
return;
}
Command *cmd = NULL;
For (GlobalCommands) {
if (it.name == name) {
cmd = &it;
break;
}
}
if (cmd) {
SkipWhitespace(&string);
char c = At(string, 0);
ExpectP(c == u'"' || c == u'\'', "Expected string to follow the command name, instead got %S", string);
string = Skip(string, 1);
String quote = SkipUntil(&string, {&c, 1});
ExpectP(At(string, 0) == c, "Failed to parse command, unclose quote");
quote = Intern(&GlobalInternTable, quote);
ReportConsolef(":Set %S %c%S%c", name, c, quote, c);
Trigger *trigger = ParseKeyCached(quote);
if (trigger) {
cmd->binding = quote;
cmd->trigger = trigger;
CheckKeybindingColission();
}
return;
}
ReportErrorf("Failed to :Set, no such variable found: %S", name);
}
#endif