Add support for primary clipboard

This commit is contained in:
Zachary Yedidia
2016-09-02 09:40:08 -04:00
parent 226cf399ba
commit d9b8a04841
5 changed files with 40 additions and 16 deletions

View File

@@ -845,7 +845,7 @@ func (v *View) Copy(usePlugin bool) bool {
}
if v.Cursor.HasSelection() {
clipboard.WriteAll(v.Cursor.GetSelection())
clipboard.WriteAll(v.Cursor.GetSelection(), "clipboard")
v.freshClip = true
messenger.Message("Copied selection")
}
@@ -868,10 +868,10 @@ func (v *View) CutLine(usePlugin bool) bool {
}
if v.freshClip == true {
if v.Cursor.HasSelection() {
if clip, err := clipboard.ReadAll(); err != nil {
if clip, err := clipboard.ReadAll("clipboard"); err != nil {
messenger.Error(err)
} else {
clipboard.WriteAll(clip + v.Cursor.GetSelection())
clipboard.WriteAll(clip+v.Cursor.GetSelection(), "clipboard")
}
}
} else if time.Since(v.lastCutTime)/time.Second > 10*time.Second || v.freshClip == false {
@@ -896,7 +896,7 @@ func (v *View) Cut(usePlugin bool) bool {
}
if v.Cursor.HasSelection() {
clipboard.WriteAll(v.Cursor.GetSelection())
clipboard.WriteAll(v.Cursor.GetSelection(), "clipboard")
v.Cursor.DeleteSelection()
v.Cursor.ResetSelection()
v.freshClip = true
@@ -955,18 +955,23 @@ func (v *View) Paste(usePlugin bool) bool {
return false
}
leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
clip, _ := clipboard.ReadAll("clipboard")
v.paste(clip)
if v.Cursor.HasSelection() {
v.Cursor.DeleteSelection()
v.Cursor.ResetSelection()
if usePlugin {
return PostActionCall("Paste", v)
}
clip, _ := clipboard.ReadAll()
clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
v.Buf.Insert(v.Cursor.Loc, clip)
v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
v.freshClip = false
messenger.Message("Pasted clipboard")
return true
}
// PastePrimary pastes from the primary clipboard (only use on linux)
func (v *View) PastePrimary(usePlugin bool) bool {
if usePlugin && !PreActionCall("Paste", v) {
return false
}
clip, _ := clipboard.ReadAll("primary")
v.paste(clip)
if usePlugin {
return PostActionCall("Paste", v)

View File

@@ -55,6 +55,7 @@ var bindingActions = map[string]func(*View, bool) bool{
"IndentSelection": (*View).IndentSelection,
"OutdentSelection": (*View).OutdentSelection,
"Paste": (*View).Paste,
"PastePrimary": (*View).PastePrimary,
"SelectAll": (*View).SelectAll,
"OpenFile": (*View).OpenFile,
"Start": (*View).Start,

View File

@@ -287,7 +287,7 @@ func (m *Messenger) HandleEvent(event tcell.Event, history []string) {
m.cursorx--
}
case tcell.KeyCtrlV:
clip, _ := clipboard.ReadAll()
clip, _ := clipboard.ReadAll("clipboard")
m.response = Insert(m.response, m.cursorx, clip)
m.cursorx += Count(clip)
case tcell.KeyRune:

View File

@@ -353,7 +353,7 @@ func main() {
// we copy it to the clipboard.
// Often error messages are displayed down there so it can be useful to easily
// copy the message
clipboard.WriteAll(messenger.message)
clipboard.WriteAll(messenger.message, "primary")
continue
}

View File

@@ -145,6 +145,20 @@ func (v *View) ToggleTabbar() {
}
}
func (v *View) paste(clip string) {
leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
if v.Cursor.HasSelection() {
v.Cursor.DeleteSelection()
v.Cursor.ResetSelection()
}
clip = strings.Replace(clip, "\n", "\n"+leadingWS, -1)
v.Buf.Insert(v.Cursor.Loc, clip)
v.Cursor.Loc = v.Cursor.Loc.Move(Count(clip), v.Buf)
v.freshClip = false
messenger.Message("Pasted clipboard")
}
// ScrollUp scrolls the view up n lines (if possible)
func (v *View) ScrollUp(n int) {
// Try to scroll by n but if it would overflow, scroll by 1
@@ -414,6 +428,10 @@ func (v *View) HandleEvent(event tcell.Event) {
v.Cursor.CurSelection[1] = v.Cursor.Loc
}
}
case tcell.Button2:
// Middle mouse button was clicked,
// We should paste primary
v.PastePrimary(true)
case tcell.ButtonNone:
// Mouse event with no click
if !v.mouseReleased {