X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=cmd%2Fmicro%2Fcursor.go;h=b864974af927c4632667162c48534e935a7cf4cf;hb=924809b19baa0b7e6da896a35624934f96a26019;hp=c6eb92aec192c8bbe8f93055739b6b4503560c60;hpb=efe1ab5db62fe89b9bbd2096ce68492a788fc8c7;p=micro.git diff --git a/cmd/micro/cursor.go b/cmd/micro/cursor.go index c6eb92ae..b864974a 100644 --- a/cmd/micro/cursor.go +++ b/cmd/micro/cursor.go @@ -29,6 +29,15 @@ func (c *Cursor) Goto(b Cursor) { c.OrigSelection, c.CurSelection = b.OrigSelection, b.CurSelection } +// CopySelection copies the user's selection to either "primary" or "clipboard" +func (c *Cursor) CopySelection(target string) { + if c.HasSelection() { + if target != "primary" || c.buf.Settings["useprimary"].(bool) { + clipboard.WriteAll(c.GetSelection(), target) + } + } +} + // ResetSelection resets the user's selection func (c *Cursor) ResetSelection() { c.CurSelection[0] = c.buf.Start() @@ -38,19 +47,11 @@ func (c *Cursor) ResetSelection() { // SetSelectionStart sets the start of the selection func (c *Cursor) SetSelectionStart(pos Loc) { c.CurSelection[0] = pos - // Copy to primary clipboard for linux - if c.HasSelection() { - clipboard.WriteAll(c.GetSelection(), "primary") - } } // SetSelectionEnd sets the end of the selection func (c *Cursor) SetSelectionEnd(pos Loc) { c.CurSelection[1] = pos - // Copy to primary clipboard for linux - if c.HasSelection() { - clipboard.WriteAll(c.GetSelection(), "primary") - } } // HasSelection returns whether or not the user has selected anything @@ -73,10 +74,13 @@ func (c *Cursor) DeleteSelection() { // GetSelection returns the cursor's selection func (c *Cursor) GetSelection() string { - if c.CurSelection[0].GreaterThan(c.CurSelection[1]) { - return c.buf.Substr(c.CurSelection[1], c.CurSelection[0]) + if InBounds(c.CurSelection[0], c.buf) && InBounds(c.CurSelection[1], c.buf) { + if c.CurSelection[0].GreaterThan(c.CurSelection[1]) { + return c.buf.Substr(c.CurSelection[1], c.CurSelection[0]) + } + return c.buf.Substr(c.CurSelection[0], c.CurSelection[1]) } - return c.buf.Substr(c.CurSelection[0], c.CurSelection[1]) + return "" } // SelectLine selects the current line