]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Rename to tabstospaces for consistency
[micro.git] / cmd / micro / view.go
index bf1468fd86b0385c3d5ae17ddfa565406bc853f5..9a759a47de8f131a00cca23aa55672e56ccf438f 100644 (file)
@@ -2,6 +2,8 @@ package main
 
 import (
        "io/ioutil"
+       "reflect"
+       "runtime"
        "strconv"
        "strings"
        "time"
@@ -13,7 +15,8 @@ import (
 // It stores information about the cursor, and the viewport
 // that the user sees the buffer from.
 type View struct {
-       Cursor Cursor
+       // A pointer to the buffer's cursor for ease of access
+       Cursor *Cursor
 
        // The topmost line, used for vertical scrolling
        Topline int
@@ -31,11 +34,8 @@ type View struct {
        // How much to offset because of line numbers
        lineNumOffset int
 
-       // The eventhandler for undo/redo
-       eh *EventHandler
-
        // Holds the list of gutter messages
-       messages []GutterMessage
+       messages map[string][]GutterMessage
 
        // The buffer
        Buf *Buffer
@@ -88,7 +88,7 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
 
        v.OpenBuffer(buf)
 
-       v.eh = NewEventHandler(v)
+       v.messages = make(map[string][]GutterMessage)
 
        v.sline = Statusline{
                view: v,
@@ -106,7 +106,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 +138,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" {
@@ -154,17 +158,12 @@ func (v *View) CanClose(msg string) bool {
 // This resets the topline, event handler and cursor.
 func (v *View) OpenBuffer(buf *Buffer) {
        v.Buf = buf
+       v.Cursor = &buf.Cursor
        v.Topline = 0
        v.leftCol = 0
-       // Put the cursor at the first spot
-       v.Cursor = Cursor{
-               x: 0,
-               y: 0,
-               v: v,
-       }
        v.Cursor.ResetSelection()
+       v.messages = make(map[string][]GutterMessage)
 
-       v.eh = NewEventHandler(v)
        v.matches = Match(v)
 
        // Set mouseReleased to true because we assume the mouse is not being pressed when
@@ -196,12 +195,16 @@ func (v *View) ReOpen() {
 func (v *View) Relocate() bool {
        ret := false
        cy := v.Cursor.y
-       if cy < v.Topline {
+       scrollmargin := int(settings["scrollmargin"].(float64))
+       if cy < v.Topline+scrollmargin && cy > scrollmargin-1 {
+               v.Topline = cy - scrollmargin
+               ret = true
+       } else if cy < v.Topline {
                v.Topline = cy
                ret = true
        }
-       if cy > v.Topline+v.height-1 {
-               v.Topline = cy - v.height + 1
+       if cy > v.Topline+v.height-1-scrollmargin {
+               v.Topline = cy - v.height + 1 + scrollmargin
                ret = true
        }
 
@@ -254,18 +257,32 @@ func (v *View) HandleEvent(event tcell.Event) {
                // Window resized
                v.Resize(e.Size())
        case *tcell.EventKey:
-               if e.Key() == tcell.KeyRune {
+               if e.Key() == tcell.KeyRune && e.Modifiers() == 0 {
                        // Insert a character
                        if v.Cursor.HasSelection() {
                                v.Cursor.DeleteSelection()
                                v.Cursor.ResetSelection()
                        }
-                       v.eh.Insert(v.Cursor.Loc(), string(e.Rune()))
+                       v.Buf.Insert(v.Cursor.Loc(), string(e.Rune()))
                        v.Cursor.Right()
                } else {
                        for key, action := range bindings {
-                               if e.Key() == key {
-                                       relocate = action(v)
+                               if e.Key() == key.keyCode {
+                                       if e.Key() == tcell.KeyRune {
+                                               if e.Rune() != key.r {
+                                                       continue
+                                               }
+                                       }
+                                       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)
+                                                       }
+                                               }
+                                       }
                                }
                        }
                }
@@ -275,20 +292,22 @@ func (v *View) HandleEvent(event tcell.Event) {
                        v.Cursor.ResetSelection()
                }
                clip := e.Text()
-               v.eh.Insert(v.Cursor.Loc(), clip)
+               v.Buf.Insert(v.Cursor.Loc(), clip)
                v.Cursor.SetLoc(v.Cursor.Loc() + Count(clip))
                v.freshClip = false
        case *tcell.EventMouse:
                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 +365,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 +385,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 +431,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,33 +450,35 @@ 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
+                       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 GutterWarning:
-                                               if style, ok := colorscheme["gutter-warning"]; 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
                                                }
-                                       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 {
-                                               messenger.Message(msg.msg)
-                                               messenger.gutterMessage = true
                                        }
                                }
                        }
@@ -450,7 +487,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 +543,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++
@@ -542,9 +594,21 @@ func (v *View) DisplayView() {
        }
 }
 
+// DisplayCursor draws the current buffer's cursor to the screen
+func (v *View) DisplayCursor() {
+       // Don't draw the cursor if it is out of the viewport or if it has a selection
+       if (v.Cursor.y-v.Topline < 0 || v.Cursor.y-v.Topline > v.height-1) || v.Cursor.HasSelection() {
+               screen.HideCursor()
+       } else {
+               screen.ShowCursor(v.Cursor.GetVisualX()+v.lineNumOffset-v.leftCol, v.Cursor.y-v.Topline)
+       }
+}
+
 // Display renders the view, the cursor, and statusline
 func (v *View) Display() {
        v.DisplayView()
-       v.Cursor.Display()
-       v.sline.Display()
+       v.DisplayCursor()
+       if settings["statusline"].(bool) {
+               v.sline.Display()
+       }
 }