Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions cmd/micro/micro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions internal/clipboard/clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package clipboard

import (
"errors"
"log"
"sync"

"github.com/zyedidia/clipper"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down