]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Use the new and updated version of tcell
[micro.git] / cmd / micro / view.go
index bf1468fd86b0385c3d5ae17ddfa565406bc853f5..078177b318d187b7ebc4cb547f631a7117980ea2 100644 (file)
@@ -2,6 +2,8 @@ package main
 
 import (
        "io/ioutil"
+       "reflect"
+       "runtime"
        "strconv"
        "strings"
        "time"
@@ -35,7 +37,7 @@ type View struct {
        eh *EventHandler
 
        // Holds the list of gutter messages
-       messages []GutterMessage
+       messages map[string][]GutterMessage
 
        // The buffer
        Buf *Buffer
@@ -90,6 +92,8 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
 
        v.eh = NewEventHandler(v)
 
+       v.messages = make(map[string][]GutterMessage)
+
        v.sline = Statusline{
                view: v,
        }
@@ -106,7 +110,11 @@ func (v *View) Resize(w, h int) {
        h--
        v.width = int(float32(w) * float32(v.widthPercent) / 100)
        // We subtract 1 for the statusline
-       v.height = int(float32(h)*float32(v.heightPercent)/100) - 1
+       v.height = int(float32(h) * float32(v.heightPercent) / 100)
+       if settings["statusline"].(bool) {
+               // Make room for the status line if it is enabled
+               v.height--
+       }
 }
 
 // ScrollUp scrolls the view up n lines (if possible)
@@ -134,7 +142,7 @@ func (v *View) ScrollDown(n int) {
 // 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 {
-       if v.Buf.IsDirty() {
+       if v.Buf.IsModified {
                quit, canceled := messenger.Prompt("You have unsaved changes. " + msg)
                if !canceled {
                        if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
@@ -163,6 +171,7 @@ func (v *View) OpenBuffer(buf *Buffer) {
                v: v,
        }
        v.Cursor.ResetSelection()
+       v.messages = make(map[string][]GutterMessage)
 
        v.eh = NewEventHandler(v)
        v.matches = Match(v)
@@ -264,8 +273,17 @@ func (v *View) HandleEvent(event tcell.Event) {
                        v.Cursor.Right()
                } else {
                        for key, action := range bindings {
-                               if e.Key() == key {
-                                       relocate = action(v)
+                               if e.Key() == key.keyCode {
+                                       if e.Modifiers() == key.modifiers {
+                                               relocate = action(v)
+                                               for _, pl := range loadedPlugins {
+                                                       funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(action).Pointer()).Name(), ".")
+                                                       err := Call(pl + "_on" + funcName[len(funcName)-1])
+                                                       if err != nil {
+                                                               TermMessage(err)
+                                                       }
+                                               }
+                                       }
                                }
                        }
                }
@@ -282,13 +300,15 @@ func (v *View) HandleEvent(event tcell.Event) {
                x, y := e.Position()
                x -= v.lineNumOffset - v.leftCol
                y += v.Topline
+               // Don't relocate for mouse events
+               relocate = false
 
                button := e.Buttons()
 
                switch button {
                case tcell.Button1:
                        // Left click
-                       if v.mouseReleased && !e.HasMotion() {
+                       if v.mouseReleased {
                                v.MoveToMouseClick(x, y)
                                if time.Since(v.lastClickTime)/time.Millisecond < doubleClickThreshold {
                                        if v.doubleClick {
@@ -346,19 +366,14 @@ func (v *View) HandleEvent(event tcell.Event) {
                                }
                                v.mouseReleased = true
                        }
-                       // We don't want to relocate because otherwise the view will be relocated
-                       // every time the user moves the cursor
-                       relocate = false
                case tcell.WheelUp:
-                       // Scroll up two lines
-                       v.ScrollUp(2)
-                       // We don't want to relocate if the user is scrolling
-                       relocate = false
+                       // Scroll up
+                       scrollSpeed := int(settings["scrollSpeed"].(float64))
+                       v.ScrollUp(scrollSpeed)
                case tcell.WheelDown:
-                       // Scroll down two lines
-                       v.ScrollDown(2)
-                       // We don't want to relocate if the user is scrolling
-                       relocate = false
+                       // Scroll down
+                       scrollSpeed := int(settings["scrollSpeed"].(float64))
+                       v.ScrollDown(scrollSpeed)
                }
        }
 
@@ -371,19 +386,34 @@ func (v *View) HandleEvent(event tcell.Event) {
 }
 
 // GutterMessage creates a message in this view's gutter
-func (v *View) GutterMessage(lineN int, msg string, kind int) {
+func (v *View) GutterMessage(section string, lineN int, msg string, kind int) {
        lineN--
        gutterMsg := GutterMessage{
                lineNum: lineN,
                msg:     msg,
                kind:    kind,
        }
-       for _, gmsg := range v.messages {
-               if gmsg.lineNum == lineN {
-                       return
+       for _, v := range v.messages {
+               for _, gmsg := range v {
+                       if gmsg.lineNum == lineN {
+                               return
+                       }
                }
        }
-       v.messages = append(v.messages, gutterMsg)
+       messages := v.messages[section]
+       v.messages[section] = append(messages, gutterMsg)
+}
+
+// ClearGutterMessages clears all gutter messages from a given section
+func (v *View) ClearGutterMessages(section string) {
+       v.messages[section] = []GutterMessage{}
+}
+
+// ClearAllGutterMessages clears all the gutter messages
+func (v *View) ClearAllGutterMessages() {
+       for k := range v.messages {
+               v.messages[k] = []GutterMessage{}
+       }
 }
 
 // DisplayView renders the view to the screen
@@ -402,7 +432,13 @@ func (v *View) DisplayView() {
        }
        var highlightStyle tcell.Style
 
-       if len(v.messages) > 0 {
+       var hasGutterMessages bool
+       for _, v := range v.messages {
+               if len(v) > 0 {
+                       hasGutterMessages = true
+               }
+       }
+       if hasGutterMessages {
                v.lineNumOffset += 2
        }
 
@@ -415,34 +451,36 @@ func (v *View) DisplayView() {
                }
                line := v.Buf.Lines[lineN+v.Topline]
 
-               if len(v.messages) > 0 {
+               if hasGutterMessages {
                        msgOnLine := false
-                       for _, msg := range v.messages {
-                               if msg.lineNum == lineN+v.Topline {
-                                       msgOnLine = true
-                                       gutterStyle := tcell.StyleDefault
-                                       switch msg.kind {
-                                       case GutterInfo:
-                                               if style, ok := colorscheme["gutter-info"]; ok {
-                                                       gutterStyle = style
-                                               }
-                                       case GutterWarning:
-                                               if style, ok := colorscheme["gutter-warning"]; ok {
-                                                       gutterStyle = style
+                       for k := range v.messages {
+                               for _, msg := range v.messages[k] {
+                                       if msg.lineNum == lineN+v.Topline {
+                                               msgOnLine = true
+                                               gutterStyle := tcell.StyleDefault
+                                               switch msg.kind {
+                                               case GutterInfo:
+                                                       if style, ok := colorscheme["gutter-info"]; ok {
+                                                               gutterStyle = style
+                                                       }
+                                               case GutterWarning:
+                                                       if style, ok := colorscheme["gutter-warning"]; ok {
+                                                               gutterStyle = style
+                                                       }
+                                               case GutterError:
+                                                       if style, ok := colorscheme["gutter-error"]; ok {
+                                                               gutterStyle = style
+                                                       }
                                                }
-                                       case GutterError:
-                                               if style, ok := colorscheme["gutter-error"]; ok {
-                                                       gutterStyle = style
+                                               screen.SetContent(x, lineN, '>', nil, gutterStyle)
+                                               x++
+                                               screen.SetContent(x, lineN, '>', nil, gutterStyle)
+                                               x++
+                                               if v.Cursor.y == lineN+v.Topline {
+                                                       messenger.Message(msg.msg)
+                                                       messenger.gutterMessage = true
                                                }
                                        }
-                                       screen.SetContent(x, lineN, '>', nil, gutterStyle)
-                                       x++
-                                       screen.SetContent(x, lineN, '>', nil, gutterStyle)
-                                       x++
-                                       if v.Cursor.y == lineN {
-                                               messenger.Message(msg.msg)
-                                               messenger.gutterMessage = true
-                                       }
                                }
                        }
                        if !msgOnLine {
@@ -450,7 +488,7 @@ func (v *View) DisplayView() {
                                x++
                                screen.SetContent(x, lineN, ' ', nil, tcell.StyleDefault)
                                x++
-                               if v.Cursor.y == lineN && messenger.gutterMessage {
+                               if v.Cursor.y == lineN+v.Topline && messenger.gutterMessage {
                                        messenger.Reset()
                                        messenger.gutterMessage = false
                                }
@@ -506,7 +544,22 @@ func (v *View) DisplayView() {
                        }
 
                        if ch == '\t' {
-                               screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
+                               lineIndentStyle := defStyle
+                               if style, ok := colorscheme["indent-char"]; ok {
+                                       lineIndentStyle = style
+                               }
+                               if v.Cursor.HasSelection() &&
+                                       (charNum >= v.Cursor.curSelection[0] && charNum < v.Cursor.curSelection[1] ||
+                                               charNum < v.Cursor.curSelection[0] && charNum >= v.Cursor.curSelection[1]) {
+
+                                       lineIndentStyle = tcell.StyleDefault.Reverse(true)
+
+                                       if style, ok := colorscheme["selection"]; ok {
+                                               lineIndentStyle = style
+                                       }
+                               }
+                               indentChar := []rune(settings["indentchar"].(string))
+                               screen.SetContent(x-v.leftCol+tabchars, lineN, indentChar[0], nil, lineIndentStyle)
                                tabSize := int(settings["tabsize"].(float64))
                                for i := 0; i < tabSize-1; i++ {
                                        tabchars++
@@ -546,5 +599,7 @@ func (v *View) DisplayView() {
 func (v *View) Display() {
        v.DisplayView()
        v.Cursor.Display()
-       v.sline.Display()
+       if settings["statusline"].(bool) {
+               v.sline.Display()
+       }
 }