mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-06 07:00:24 +09:00
Merge branch 'primary-clipboard'
This commit is contained in:
@@ -598,21 +598,21 @@ func (v *View) IndentSelection(usePlugin bool) bool {
|
||||
v.Buf.Insert(Loc{0, i}, Spaces(tabsize))
|
||||
if i == start {
|
||||
if v.Cursor.CurSelection[0].X > 0 {
|
||||
v.Cursor.CurSelection[0] = v.Cursor.CurSelection[0].Move(tabsize, v.Buf)
|
||||
v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(tabsize, v.Buf))
|
||||
}
|
||||
}
|
||||
if i == end {
|
||||
v.Cursor.CurSelection[1] = Loc{endX + tabsize + 1, end}
|
||||
v.Cursor.SetSelectionEnd(Loc{endX + tabsize + 1, end})
|
||||
}
|
||||
} else {
|
||||
v.Buf.Insert(Loc{0, i}, "\t")
|
||||
if i == start {
|
||||
if v.Cursor.CurSelection[0].X > 0 {
|
||||
v.Cursor.CurSelection[0] = v.Cursor.CurSelection[0].Move(1, v.Buf)
|
||||
v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(1, v.Buf))
|
||||
}
|
||||
}
|
||||
if i == end {
|
||||
v.Cursor.CurSelection[1] = Loc{endX + 2, end}
|
||||
v.Cursor.SetSelectionEnd(Loc{endX + 2, end})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -647,22 +647,22 @@ func (v *View) OutdentSelection(usePlugin bool) bool {
|
||||
v.Buf.Remove(Loc{0, i}, Loc{1, i})
|
||||
if i == start {
|
||||
if v.Cursor.CurSelection[0].X > 0 {
|
||||
v.Cursor.CurSelection[0] = v.Cursor.CurSelection[0].Move(-1, v.Buf)
|
||||
v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(-1, v.Buf))
|
||||
}
|
||||
}
|
||||
if i == end {
|
||||
v.Cursor.CurSelection[1] = Loc{endX - j, end}
|
||||
v.Cursor.SetSelectionEnd(Loc{endX - j, end})
|
||||
}
|
||||
}
|
||||
} else {
|
||||
v.Buf.Remove(Loc{0, i}, Loc{1, i})
|
||||
if i == start {
|
||||
if v.Cursor.CurSelection[0].X > 0 {
|
||||
v.Cursor.CurSelection[0] = v.Cursor.CurSelection[0].Move(-1, v.Buf)
|
||||
v.Cursor.SetSelectionStart(v.Cursor.CurSelection[0].Move(-1, v.Buf))
|
||||
}
|
||||
}
|
||||
if i == end {
|
||||
v.Cursor.CurSelection[1] = Loc{endX, end}
|
||||
v.Cursor.SetSelectionEnd(Loc{endX, end})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -850,7 +850,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")
|
||||
}
|
||||
@@ -873,10 +873,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 {
|
||||
@@ -901,7 +901,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
|
||||
@@ -960,18 +960,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)
|
||||
@@ -985,8 +990,8 @@ func (v *View) SelectAll(usePlugin bool) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
v.Cursor.CurSelection[0] = v.Buf.Start()
|
||||
v.Cursor.CurSelection[1] = v.Buf.End()
|
||||
v.Cursor.SetSelectionStart(v.Buf.Start())
|
||||
v.Cursor.SetSelectionEnd(v.Buf.End())
|
||||
// Put the cursor at the beginning
|
||||
v.Cursor.X = 0
|
||||
v.Cursor.Y = 0
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
import "github.com/zyedidia/clipboard"
|
||||
|
||||
// The Cursor struct stores the location of the cursor in the view
|
||||
// The complicated part about the cursor is storing its location.
|
||||
// The cursor must be displayed at an x, y location, but since the buffer
|
||||
@@ -33,6 +35,24 @@ func (c *Cursor) ResetSelection() {
|
||||
c.CurSelection[1] = c.buf.Start()
|
||||
}
|
||||
|
||||
// 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
|
||||
func (c *Cursor) HasSelection() bool {
|
||||
return c.CurSelection[0] != c.CurSelection[1]
|
||||
@@ -43,7 +63,7 @@ func (c *Cursor) DeleteSelection() {
|
||||
if c.CurSelection[0].GreaterThan(c.CurSelection[1]) {
|
||||
c.buf.Remove(c.CurSelection[1], c.CurSelection[0])
|
||||
c.Loc = c.CurSelection[1]
|
||||
} else if c.GetSelection() == "" {
|
||||
} else if !c.HasSelection() {
|
||||
return
|
||||
} else {
|
||||
c.buf.Remove(c.CurSelection[0], c.CurSelection[1])
|
||||
@@ -62,12 +82,12 @@ func (c *Cursor) GetSelection() string {
|
||||
// SelectLine selects the current line
|
||||
func (c *Cursor) SelectLine() {
|
||||
c.Start()
|
||||
c.CurSelection[0] = c.Loc
|
||||
c.SetSelectionStart(c.Loc)
|
||||
c.End()
|
||||
if c.buf.NumLines-1 > c.Y {
|
||||
c.CurSelection[1] = c.Loc.Move(1, c.buf)
|
||||
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
|
||||
} else {
|
||||
c.CurSelection[1] = c.Loc
|
||||
c.SetSelectionEnd(c.Loc)
|
||||
}
|
||||
|
||||
c.OrigSelection = c.CurSelection
|
||||
@@ -77,13 +97,13 @@ func (c *Cursor) SelectLine() {
|
||||
func (c *Cursor) AddLineToSelection() {
|
||||
if c.Loc.LessThan(c.OrigSelection[0]) {
|
||||
c.Start()
|
||||
c.CurSelection[0] = c.Loc
|
||||
c.CurSelection[1] = c.OrigSelection[1]
|
||||
c.SetSelectionStart(c.Loc)
|
||||
c.SetSelectionEnd(c.OrigSelection[1])
|
||||
}
|
||||
if c.Loc.GreaterThan(c.OrigSelection[1]) {
|
||||
c.End()
|
||||
c.CurSelection[1] = c.Loc.Move(1, c.buf)
|
||||
c.CurSelection[0] = c.OrigSelection[0]
|
||||
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
|
||||
c.SetSelectionStart(c.OrigSelection[0])
|
||||
}
|
||||
|
||||
if c.Loc.LessThan(c.OrigSelection[1]) && c.Loc.GreaterThan(c.OrigSelection[0]) {
|
||||
@@ -98,8 +118,8 @@ func (c *Cursor) SelectWord() {
|
||||
}
|
||||
|
||||
if !IsWordChar(string(c.RuneUnder(c.X))) {
|
||||
c.CurSelection[0] = c.Loc
|
||||
c.CurSelection[1] = c.Loc.Move(1, c.buf)
|
||||
c.SetSelectionStart(c.Loc)
|
||||
c.SetSelectionEnd(c.Loc.Move(1, c.buf))
|
||||
c.OrigSelection = c.CurSelection
|
||||
return
|
||||
}
|
||||
@@ -110,14 +130,14 @@ func (c *Cursor) SelectWord() {
|
||||
backward--
|
||||
}
|
||||
|
||||
c.CurSelection[0] = Loc{backward, c.Y}
|
||||
c.SetSelectionStart(Loc{backward, c.Y})
|
||||
c.OrigSelection[0] = c.CurSelection[0]
|
||||
|
||||
for forward < Count(c.buf.Line(c.Y))-1 && IsWordChar(string(c.RuneUnder(forward+1))) {
|
||||
forward++
|
||||
}
|
||||
|
||||
c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
|
||||
c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
|
||||
c.OrigSelection[1] = c.CurSelection[1]
|
||||
c.Loc = c.CurSelection[1]
|
||||
}
|
||||
@@ -136,8 +156,8 @@ func (c *Cursor) AddWordToSelection() {
|
||||
backward--
|
||||
}
|
||||
|
||||
c.CurSelection[0] = Loc{backward, c.Y}
|
||||
c.CurSelection[1] = c.OrigSelection[1]
|
||||
c.SetSelectionStart(Loc{backward, c.Y})
|
||||
c.SetSelectionEnd(c.OrigSelection[1])
|
||||
}
|
||||
|
||||
if c.Loc.GreaterThan(c.OrigSelection[1]) {
|
||||
@@ -147,8 +167,8 @@ func (c *Cursor) AddWordToSelection() {
|
||||
forward++
|
||||
}
|
||||
|
||||
c.CurSelection[1] = Loc{forward, c.Y}.Move(1, c.buf)
|
||||
c.CurSelection[0] = c.OrigSelection[0]
|
||||
c.SetSelectionEnd(Loc{forward, c.Y}.Move(1, c.buf))
|
||||
c.SetSelectionStart(c.OrigSelection[0])
|
||||
}
|
||||
|
||||
c.Loc = c.CurSelection[1]
|
||||
@@ -157,11 +177,11 @@ func (c *Cursor) AddWordToSelection() {
|
||||
// SelectTo selects from the current cursor location to the given location
|
||||
func (c *Cursor) SelectTo(loc Loc) {
|
||||
if loc.GreaterThan(c.OrigSelection[0]) {
|
||||
c.CurSelection[0] = c.OrigSelection[0]
|
||||
c.CurSelection[1] = loc
|
||||
c.SetSelectionStart(c.OrigSelection[0])
|
||||
c.SetSelectionEnd(loc)
|
||||
} else {
|
||||
c.CurSelection[0] = loc
|
||||
c.CurSelection[1] = c.OrigSelection[0]
|
||||
c.SetSelectionStart(loc)
|
||||
c.SetSelectionEnd(c.OrigSelection[0])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -300,7 +300,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:
|
||||
|
||||
@@ -366,7 +366,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
|
||||
}
|
||||
|
||||
|
||||
@@ -125,8 +125,8 @@ func Search(searchStr string, v *View, down bool) {
|
||||
return
|
||||
}
|
||||
|
||||
v.Cursor.CurSelection[0] = FromCharPos(charPos+runePos(match[0], str), v.Buf)
|
||||
v.Cursor.CurSelection[1] = FromCharPos(charPos+runePos(match[1], str), v.Buf)
|
||||
v.Cursor.SetSelectionStart(FromCharPos(charPos+runePos(match[0], str), v.Buf))
|
||||
v.Cursor.SetSelectionEnd(FromCharPos(charPos+runePos(match[1], str), v.Buf))
|
||||
v.Cursor.Loc = v.Cursor.CurSelection[1]
|
||||
if v.Relocate() {
|
||||
v.matches = Match(v)
|
||||
|
||||
@@ -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
|
||||
@@ -411,9 +425,13 @@ func (v *View) HandleEvent(event tcell.Event) {
|
||||
} else if v.doubleClick {
|
||||
v.Cursor.AddWordToSelection()
|
||||
} else {
|
||||
v.Cursor.CurSelection[1] = v.Cursor.Loc
|
||||
v.Cursor.SetSelectionEnd(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 {
|
||||
@@ -427,7 +445,7 @@ func (v *View) HandleEvent(event tcell.Event) {
|
||||
|
||||
if !v.doubleClick && !v.tripleClick {
|
||||
v.MoveToMouseClick(x, y)
|
||||
v.Cursor.CurSelection[1] = v.Cursor.Loc
|
||||
v.Cursor.SetSelectionEnd(v.Cursor.Loc)
|
||||
}
|
||||
v.mouseReleased = true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user