]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Copy to primary clipboard for any change in selection
[micro.git] / cmd / micro / view.go
index b0d72a89c9841c565899ab7bf51185622f8cef30..996ac8bdd39cde4e67e443c5fbc07f2f34453485 100644 (file)
@@ -84,7 +84,7 @@ type View struct {
 // NewView returns a new fullscreen view
 func NewView(buf *Buffer) *View {
        screenW, screenH := screen.Size()
-       return NewViewWidthHeight(buf, screenW, screenH-1)
+       return NewViewWidthHeight(buf, screenW, screenH)
 }
 
 // NewViewWidthHeight returns a new view with the specified width and height
@@ -107,15 +107,23 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
                view: v,
        }
 
-       if settings["statusline"].(bool) {
+       if v.Buf.Settings["statusline"].(bool) {
                v.height--
        }
 
+       for _, pl := range loadedPlugins {
+               _, err := Call(pl+".onViewOpen", v)
+               if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
+                       TermMessage(err)
+                       continue
+               }
+       }
+
        return v
 }
 
 func (v *View) ToggleStatusLine() {
-       if settings["statusline"].(bool) {
+       if v.Buf.Settings["statusline"].(bool) {
                v.height--
        } else {
                v.height++
@@ -137,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
@@ -161,13 +183,13 @@ func (v *View) ScrollDown(n int) {
 // If there are unsaved changes, the user will be asked if the view can be closed
 // causing them to lose the unsaved changes
 // The message is what to print after saying "You have unsaved changes. "
-func (v *View) CanClose(msg string) bool {
+func (v *View) CanClose(msg string, responses ...rune) bool {
        if v.Buf.IsModified {
-               quit, canceled := messenger.Prompt("You have unsaved changes. "+msg, "Unsaved", NoCompletion)
+               char, canceled := messenger.LetterPrompt("You have unsaved changes. "+msg, responses...)
                if !canceled {
-                       if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
+                       if char == 'y' {
                                return true
-                       } else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
+                       } else if char == 's' {
                                v.Save(true)
                                return true
                        }
@@ -209,7 +231,7 @@ func (v *View) CloseBuffer() {
 
 // ReOpen reloads the current buffer
 func (v *View) ReOpen() {
-       if v.CanClose("Continue? (yes, no, save) ") {
+       if v.CanClose("Continue? (y,n,s) ", 'y', 'n', 's') {
                screen.Clear()
                v.Buf.ReOpen()
                v.Relocate()
@@ -236,7 +258,7 @@ func (v *View) VSplit(buf *Buffer) bool {
 func (v *View) Relocate() bool {
        ret := false
        cy := v.Cursor.Y
-       scrollmargin := int(settings["scrollmargin"].(float64))
+       scrollmargin := int(v.Buf.Settings["scrollmargin"].(float64))
        if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
                v.Topline = cy - scrollmargin
                ret = true
@@ -313,7 +335,7 @@ func (v *View) HandleEvent(event tcell.Event) {
                        v.Cursor.Right()
 
                        for _, pl := range loadedPlugins {
-                               _, err := Call(pl+".onRune", []string{string(e.Rune())})
+                               _, err := Call(pl+".onRune", string(e.Rune()), v)
                                if err != nil && !strings.HasPrefix(err.Error(), "function does not exist") {
                                        TermMessage(err)
                                }
@@ -336,7 +358,24 @@ func (v *View) HandleEvent(event tcell.Event) {
                        }
                }
        case *tcell.EventPaste:
-               relocate = v.Paste(true)
+               if !PreActionCall("Paste", v) {
+                       break
+               }
+
+               leadingWS := GetLeadingWhitespace(v.Buf.Line(v.Cursor.Y))
+
+               if v.Cursor.HasSelection() {
+                       v.Cursor.DeleteSelection()
+                       v.Cursor.ResetSelection()
+               }
+               clip := e.Text()
+               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")
+
+               PostActionCall("Paste", v)
        case *tcell.EventMouse:
                x, y := e.Position()
                x -= v.lineNumOffset - v.leftCol + v.x
@@ -375,8 +414,8 @@ func (v *View) HandleEvent(event tcell.Event) {
                                        v.lastClickTime = time.Now()
 
                                        v.Cursor.OrigSelection[0] = v.Cursor.Loc
-                                       v.Cursor.CurSelection[0] = v.Cursor.Loc
-                                       v.Cursor.CurSelection[1] = v.Cursor.Loc
+                                       v.Cursor.SetSelectionStart(v.Cursor.Loc)
+                                       v.Cursor.SetSelectionEnd(v.Cursor.Loc)
                                }
                                v.mouseReleased = false
                        } else if !v.mouseReleased {
@@ -386,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 {
@@ -402,17 +445,17 @@ 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
                        }
                case tcell.WheelUp:
                        // Scroll up
-                       scrollspeed := int(settings["scrollspeed"].(float64))
+                       scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
                        v.ScrollUp(scrollspeed)
                case tcell.WheelDown:
                        // Scroll down
-                       scrollspeed := int(settings["scrollspeed"].(float64))
+                       scrollspeed := int(v.Buf.Settings["scrollspeed"].(float64))
                        v.ScrollDown(scrollspeed)
                }
        }
@@ -420,7 +463,7 @@ func (v *View) HandleEvent(event tcell.Event) {
        if relocate {
                v.Relocate()
        }
-       if settings["syntax"].(bool) {
+       if v.Buf.Settings["syntax"].(bool) {
                v.matches = Match(v)
        }
 }
@@ -486,7 +529,7 @@ func (v *View) DisplayView() {
        // We are going to have to offset by that amount
        maxLineLength := len(strconv.Itoa(v.Buf.NumLines))
 
-       if settings["ruler"] == true {
+       if v.Buf.Settings["ruler"] == true {
                // + 1 for the little space after the line number
                v.lineNumOffset = maxLineLength + 1
        } else {
@@ -585,14 +628,14 @@ func (v *View) DisplayView() {
                        }
                }
 
-               if settings["ruler"] == true {
+               if v.Buf.Settings["ruler"] == true {
                        // Write the line number
                        lineNumStyle := defStyle
                        if style, ok := colorscheme["line-number"]; ok {
                                lineNumStyle = style
                        }
                        if style, ok := colorscheme["current-line-number"]; ok {
-                               if curLineN == v.Cursor.Y {
+                               if curLineN == v.Cursor.Y && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() {
                                        lineNumStyle = style
                                }
                        }
@@ -620,7 +663,7 @@ func (v *View) DisplayView() {
                for _, ch := range line {
                        lineStyle := defStyle
 
-                       if settings["syntax"].(bool) {
+                       if v.Buf.Settings["syntax"].(bool) {
                                // Syntax highlighting is enabled
                                highlightStyle = v.matches[viewLine][colN]
                        }
@@ -640,7 +683,7 @@ func (v *View) DisplayView() {
 
                        // We need to display the background of the linestyle with the correct color if cursorline is enabled
                        // and this is the current view and there is no selection on this line and the cursor is on this line
-                       if settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
+                       if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
                                if style, ok := colorscheme["cursor-line"]; ok {
                                        fg, _, _ := style.Decompose()
                                        lineStyle = lineStyle.Background(fg)
@@ -666,19 +709,19 @@ func (v *View) DisplayView() {
                                                lineIndentStyle = style
                                        }
                                }
-                               if settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
+                               if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
                                        if style, ok := colorscheme["cursor-line"]; ok {
                                                fg, _, _ := style.Decompose()
                                                lineIndentStyle = lineIndentStyle.Background(fg)
                                        }
                                }
                                // Here we get the indent char
-                               indentChar := []rune(settings["indentchar"].(string))
+                               indentChar := []rune(v.Buf.Settings["indentchar"].(string))
                                if screenX-v.x-v.leftCol >= v.lineNumOffset {
                                        v.drawCell(screenX-v.leftCol, screenY, indentChar[0], nil, lineIndentStyle)
                                }
                                // Now the tab has to be displayed as a bunch of spaces
-                               tabSize := int(settings["tabsize"].(float64))
+                               tabSize := int(v.Buf.Settings["tabsize"].(float64))
                                for i := 0; i < tabSize-1; i++ {
                                        screenX++
                                        if screenX-v.x-v.leftCol >= v.lineNumOffset {
@@ -725,7 +768,7 @@ func (v *View) DisplayView() {
 
                for i := 0; i < v.width; i++ {
                        lineStyle := defStyle
-                       if settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
+                       if v.Buf.Settings["cursorline"].(bool) && tabs[curTab].curView == v.Num && !v.Cursor.HasSelection() && v.Cursor.Y == curLineN {
                                if style, ok := colorscheme["cursor-line"]; ok {
                                        fg, _, _ := style.Decompose()
                                        lineStyle = lineStyle.Background(fg)
@@ -755,7 +798,7 @@ func (v *View) Display() {
                v.DisplayCursor()
        }
        _, screenH := screen.Size()
-       if settings["statusline"].(bool) {
+       if v.Buf.Settings["statusline"].(bool) {
                v.sline.Display()
        } else if (v.y + v.height) != screenH-1 {
                for x := 0; x < v.width; x++ {