From 73066fb69b783145c8761e108001f787590034d9 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 25 May 2025 16:33:41 +0200 Subject: [PATCH 1/2] Disable early validation of colorscheme option Adding early validation of options in ReadSettings() caused a regression: colorschemes registered by plugins via config.AddRuntimeFile() stopped working, since ReadSettings() is called when plugins are not initialized (or even loaded) yet, so a colorscheme is not registered yet and thus its validation fails. Fix that with an ad-hoc fix: treat the "colorscheme" option as a special case and do not verify it early when reading settings.json, postponing that until the moment when we try to load this colorscheme. --- internal/config/colorscheme.go | 8 ++++++++ internal/config/settings.go | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/internal/config/colorscheme.go b/internal/config/colorscheme.go index 5376c845..1058ab52 100644 --- a/internal/config/colorscheme.go +++ b/internal/config/colorscheme.go @@ -55,6 +55,14 @@ func InitColorscheme() error { c, err := LoadDefaultColorscheme() if err == nil { Colorscheme = c + } else { + // The colorscheme setting seems broken (maybe because we have not validated + // it earlier, see comment in verifySetting()). So reset it to the default + // colorscheme and try again. + GlobalSettings["colorscheme"] = DefaultGlobalOnlySettings["colorscheme"] + if c, err2 := LoadDefaultColorscheme(); err2 == nil { + Colorscheme = c + } } return err diff --git a/internal/config/settings.go b/internal/config/settings.go index 14e5f18b..895d9db5 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -270,6 +270,12 @@ func verifySetting(option string, value interface{}, def interface{}) error { return fmt.Errorf("Error: setting '%s' has incorrect type (%s), using default value: %v (%s)", option, valType, def, defType) } + if option == "colorscheme" { + // Plugins are not initialized yet, so do not verify if the colorscheme + // exists yet, since the colorscheme may be added by a plugin later. + return nil + } + if err := OptionIsValid(option, value); err != nil { return err } From e923640b37d0d27e53c73e191ea755cc3ddcbfdd Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 25 May 2025 16:48:03 +0200 Subject: [PATCH 2/2] Move loading colorschemes after initializing plugins Micro "allows" plugins to register colorschemes via config.AddRuntimeFile(). However, that has never really worked, since InitColorscheme() is called earlier than plugins init() or even preinit() callbacks are called. To work around that, plugins that use it (e.g. nord-tc [1]) are using a tricky hack: call config.AddRuntimeFile() not in init() or preinit() but directly in Lua's global scope, so that it is called earlier, when the plugin's Lua code is loaded. This hack is not guaranteed to work, and works by chance. Furthermore, it only works when starting micro, and doesn't work after the `reload` command. (The reason it doesn't work is that PluginAddRuntimeFile() calls FindPlugin() which calls IsLoaded() which returns false, since, well, the plugin is not loaded, it is only being loaded. And the reason why it works when starting micro is that in that case IsLoaded() confusingly returns true, since GlobalSettings[p.Name] has not been set yet.) So move InitColorscheme() call after calling plugins init/preinit/ postinit callbacks, to let plugins successfully register colorschemes in any of those callbacks instead of using the aforementioned hack. [1] https://github.com/KiranWells/micro-nord-tc-colors --- cmd/micro/micro.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index e43073b4..7756d588 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -374,11 +374,6 @@ func main() { action.InitBindings() action.InitCommands() - err = config.InitColorscheme() - if err != nil { - screen.TermMessage(err) - } - err = config.RunPluginFn("preinit") if err != nil { screen.TermMessage(err) @@ -407,6 +402,11 @@ func main() { screen.TermMessage(err) } + err = config.InitColorscheme() + if err != nil { + screen.TermMessage(err) + } + if clipErr != nil { log.Println(clipErr, " or change 'clipboard' option") }