From 246683fca7552232a235415f74b2ad0f7c641177 Mon Sep 17 00:00:00 2001 From: Petar Peychev Date: Sun, 24 May 2026 16:08:23 +0100 Subject: [PATCH] clipboard: initialize asynchronously to avoid startup delay --- cmd/micro/micro.go | 6 +----- internal/clipboard/clipboard.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cmd/micro/micro.go b/cmd/micro/micro.go index c8a99b2517..da9cc3f11b 100644 --- a/cmd/micro/micro.go +++ b/cmd/micro/micro.go @@ -370,7 +370,7 @@ func main() { signal.Notify(sighup, syscall.SIGHUP) m := clipboard.SetMethod(config.GetGlobalOption("clipboard").(string)) - clipErr := clipboard.Initialize(m) + clipboard.InitAsync(m) defer func() { if err := recover(); err != nil { @@ -441,10 +441,6 @@ func main() { screen.TermMessage(err) } - if clipErr != nil { - log.Println(clipErr, " or change 'clipboard' option") - } - config.StartAutoSave() if a := config.GetGlobalOption("autosave").(float64); a > 0 { config.SetAutoTime(a) diff --git a/internal/clipboard/clipboard.go b/internal/clipboard/clipboard.go index 3a57caf431..d3aa65ff3a 100644 --- a/internal/clipboard/clipboard.go +++ b/internal/clipboard/clipboard.go @@ -2,6 +2,8 @@ package clipboard import ( "errors" + "log" + "sync" "github.com/zyedidia/clipper" ) @@ -37,6 +39,11 @@ const ( var clipboard clipper.Clipboard +var ( + initDone = make(chan struct{}) + initClosed sync.Once +) + // Initialize attempts to initialize the clipboard using the given method func Initialize(m Method) error { var err error @@ -52,9 +59,19 @@ func Initialize(m Method) error { if err != nil { CurrentMethod = Internal } + initClosed.Do(func() { close(initDone) }) return err } +// InitAsync runs Initialize in a background goroutine +func InitAsync(m Method) { + go func() { + if err := Initialize(m); err != nil { + log.Println(err, " or change 'clipboard' option") + } + }() +} + // SetMethod changes the clipboard access method func SetMethod(m string) Method { switch m { @@ -70,11 +87,13 @@ func SetMethod(m string) Method { // Read reads from a clipboard register func Read(r Register) (string, error) { + <-initDone return read(r, CurrentMethod) } // Write writes text to a clipboard register func Write(text string, r Register) error { + <-initDone return write(text, r, CurrentMethod) } @@ -92,6 +111,7 @@ func ReadMulti(r Register, num, ncursors int) (string, error) { // WriteMulti writes text to a clipboard register for a certain multi-cursor func WriteMulti(text string, r Register, num int, ncursors int) error { + <-initDone return writeMulti(text, r, num, ncursors, CurrentMethod) }