commit
43a69bec58
3 changed files with 37 additions and 0 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
|
@ -35,6 +36,7 @@ func InitBindings() {
|
||||||
"Redo": Redo,
|
"Redo": Redo,
|
||||||
"Copy": Copy,
|
"Copy": Copy,
|
||||||
"Cut": Cut,
|
"Cut": Cut,
|
||||||
|
"CutLine": CutLine,
|
||||||
"Paste": Paste,
|
"Paste": Paste,
|
||||||
"SelectAll": SelectAll,
|
"SelectAll": SelectAll,
|
||||||
"OpenFile": OpenFile,
|
"OpenFile": OpenFile,
|
||||||
|
@ -218,6 +220,7 @@ func DefaultBindings() map[string]string {
|
||||||
"CtrlY": "Redo",
|
"CtrlY": "Redo",
|
||||||
"CtrlC": "Copy",
|
"CtrlC": "Copy",
|
||||||
"CtrlX": "Cut",
|
"CtrlX": "Cut",
|
||||||
|
"CtrlK": "CutLine",
|
||||||
"CtrlV": "Paste",
|
"CtrlV": "Paste",
|
||||||
"CtrlA": "SelectAll",
|
"CtrlA": "SelectAll",
|
||||||
"Home": "Beginning",
|
"Home": "Beginning",
|
||||||
|
@ -435,16 +438,40 @@ func Redo(v *View) bool {
|
||||||
func Copy(v *View) bool {
|
func Copy(v *View) bool {
|
||||||
if v.cursor.HasSelection() {
|
if v.cursor.HasSelection() {
|
||||||
clipboard.WriteAll(v.cursor.GetSelection())
|
clipboard.WriteAll(v.cursor.GetSelection())
|
||||||
|
v.freshClip = true
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddCopy appends to the clipboard
|
||||||
|
func CutLine(v *View) bool {
|
||||||
|
v.cursor.SelectLine()
|
||||||
|
if v.freshClip == true {
|
||||||
|
|
||||||
|
if v.cursor.HasSelection() {
|
||||||
|
if clip, err := clipboard.ReadAll(); err != nil {
|
||||||
|
messenger.Error(err)
|
||||||
|
} else {
|
||||||
|
clipboard.WriteAll(clip + v.cursor.GetSelection())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if time.Since(v.lastCutTime)/time.Second > 10*time.Second || v.freshClip == false {
|
||||||
|
Copy(v)
|
||||||
|
}
|
||||||
|
v.freshClip = true
|
||||||
|
v.lastCutTime = time.Now()
|
||||||
|
v.cursor.DeleteSelection()
|
||||||
|
v.cursor.ResetSelection()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// Cut the selection to the system clipboard
|
// Cut the selection to the system clipboard
|
||||||
func Cut(v *View) bool {
|
func Cut(v *View) bool {
|
||||||
if v.cursor.HasSelection() {
|
if v.cursor.HasSelection() {
|
||||||
clipboard.WriteAll(v.cursor.GetSelection())
|
clipboard.WriteAll(v.cursor.GetSelection())
|
||||||
v.cursor.DeleteSelection()
|
v.cursor.DeleteSelection()
|
||||||
v.cursor.ResetSelection()
|
v.cursor.ResetSelection()
|
||||||
|
v.freshClip = true
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -459,6 +486,7 @@ func Paste(v *View) bool {
|
||||||
clip, _ := clipboard.ReadAll()
|
clip, _ := clipboard.ReadAll()
|
||||||
v.eh.Insert(v.cursor.Loc(), clip)
|
v.eh.Insert(v.cursor.Loc(), clip)
|
||||||
v.cursor.SetLoc(v.cursor.Loc() + Count(clip))
|
v.cursor.SetLoc(v.cursor.Loc() + Count(clip))
|
||||||
|
v.freshClip = false
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,8 @@ func (c *Cursor) DeleteSelection() {
|
||||||
if c.curSelection[0] > c.curSelection[1] {
|
if c.curSelection[0] > c.curSelection[1] {
|
||||||
c.v.eh.Remove(c.curSelection[1], c.curSelection[0])
|
c.v.eh.Remove(c.curSelection[1], c.curSelection[0])
|
||||||
c.SetLoc(c.curSelection[1])
|
c.SetLoc(c.curSelection[1])
|
||||||
|
} else if c.GetSelection() == "" {
|
||||||
|
return
|
||||||
} else {
|
} else {
|
||||||
c.v.eh.Remove(c.curSelection[0], c.curSelection[1])
|
c.v.eh.Remove(c.curSelection[0], c.curSelection[1])
|
||||||
c.SetLoc(c.curSelection[0])
|
c.SetLoc(c.curSelection[0])
|
||||||
|
|
|
@ -48,6 +48,13 @@ type View struct {
|
||||||
// This is useful for detecting double and triple clicks
|
// This is useful for detecting double and triple clicks
|
||||||
lastClickTime time.Time
|
lastClickTime time.Time
|
||||||
|
|
||||||
|
// lastCutTime stores when the last ctrl+k was issued.
|
||||||
|
// It is used for clearing the clipboard to replace it with fresh cut lines.
|
||||||
|
lastCutTime time.Time
|
||||||
|
|
||||||
|
// freshClip returns true if the clipboard has never been pasted.
|
||||||
|
freshClip bool
|
||||||
|
|
||||||
// Was the last mouse event actually a double click?
|
// Was the last mouse event actually a double click?
|
||||||
// Useful for detecting triple clicks -- if a double click is detected
|
// Useful for detecting triple clicks -- if a double click is detected
|
||||||
// but the last mouse event was actually a double click, it's a triple click
|
// but the last mouse event was actually a double click, it's a triple click
|
||||||
|
|
Loading…
Reference in a new issue