]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Toggle line numbers
[micro.git] / cmd / micro / view.go
index 347d50e37a67d2585bd7ce783930e07773db04de..42cd178e9f4594cd11c0337f470c7a8b1f1c5144 100644 (file)
@@ -1,12 +1,14 @@
 package main
 
 import (
-       "github.com/atotto/clipboard"
-       "github.com/gdamore/tcell"
        "io/ioutil"
        "strconv"
        "strings"
        "time"
+
+       "github.com/atotto/clipboard"
+       "github.com/gdamore/tcell"
+       "github.com/mitchellh/go-homedir"
 )
 
 // The View struct stores information about a view into a buffer.
@@ -60,9 +62,6 @@ type View struct {
        matches SyntaxMatches
        // The matches from the last frame
        lastMatches SyntaxMatches
-
-       // This is the range of lines that should have their syntax highlighting updated
-       updateLines [2]int
 }
 
 // NewView returns a new fullscreen view
@@ -75,20 +74,11 @@ func NewView(buf *Buffer) *View {
 func NewViewWidthHeight(buf *Buffer, w, h int) *View {
        v := new(View)
 
-       v.buf = buf
-
        v.widthPercent = w
        v.heightPercent = h
        v.Resize(screen.Size())
 
-       v.topline = 0
-       // Put the cursor at the first spot
-       v.cursor = Cursor{
-               x: 0,
-               y: 0,
-               v: v,
-       }
-       v.cursor.ResetSelection()
+       v.OpenBuffer(buf)
 
        v.eh = NewEventHandler(v)
 
@@ -96,24 +86,9 @@ func NewViewWidthHeight(buf *Buffer, w, h int) *View {
                view: v,
        }
 
-       // Update the syntax highlighting for the entire buffer at the start
-       v.UpdateLines(v.topline, v.topline+v.height)
-       v.matches = Match(v)
-
-       // Set mouseReleased to true because we assume the mouse is not being pressed when
-       // the editor is opened
-       v.mouseReleased = true
-       v.lastClickTime = time.Time{}
-
        return v
 }
 
-// UpdateLines sets the values for v.updateLines
-func (v *View) UpdateLines(start, end int) {
-       v.updateLines[0] = start
-       v.updateLines[1] = end + 1
-}
-
 // Resize recalculates the actual width and height of the view from the width and height
 // percentages
 // This is usually called when the window is resized, or when a split has been added and
@@ -196,6 +171,9 @@ func (v *View) CanClose(msg string) bool {
                if !canceled {
                        if strings.ToLower(quit) == "yes" || strings.ToLower(quit) == "y" {
                                return true
+                       } else if strings.ToLower(quit) == "save" || strings.ToLower(quit) == "s" {
+                               v.Save()
+                               return true
                        }
                }
        } else {
@@ -273,21 +251,47 @@ func (v *View) SelectAll() {
        v.cursor.y = 0
 }
 
+// OpenBuffer opens a new buffer in this view.
+// This resets the topline, event handler and cursor.
+func (v *View) OpenBuffer(buf *Buffer) {
+       v.buf = buf
+       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.eh = NewEventHandler(v)
+       v.matches = Match(v)
+
+       // Set mouseReleased to true because we assume the mouse is not being pressed when
+       // the editor is opened
+       v.mouseReleased = true
+       v.lastClickTime = time.Time{}
+}
+
 // OpenFile opens a new file in the current view
 // It makes sure that the current buffer can be closed first (unsaved changes)
 func (v *View) OpenFile() {
-       if v.CanClose("Continue? ") {
+       if v.CanClose("Continue? (yes, no, save) ") {
                filename, canceled := messenger.Prompt("File to open: ")
                if canceled {
                        return
                }
+               home, _ := homedir.Dir()
+               filename = strings.Replace(filename, "~", home, 1)
                file, err := ioutil.ReadFile(filename)
 
                if err != nil {
                        messenger.Error(err.Error())
                        return
                }
-               v.buf = NewBuffer(string(file), filename)
+               buf := NewBuffer(string(file), filename)
+               v.OpenBuffer(buf)
        }
 }
 
@@ -327,6 +331,9 @@ func (v *View) MoveToMouseClick(x, y int) {
        if y >= len(v.buf.lines) {
                y = len(v.buf.lines) - 1
        }
+       if y < 0 {
+               y = 0
+       }
        if x < 0 {
                x = 0
        }
@@ -346,8 +353,6 @@ func (v *View) HandleEvent(event tcell.Event) {
        // By default it's true because most events should cause a relocate
        relocate := true
 
-       // By default we don't update and syntax highlighting
-       v.UpdateLines(-2, 0)
        switch e := event.(type) {
        case *tcell.EventResize:
                // Window resized
@@ -376,12 +381,18 @@ func (v *View) HandleEvent(event tcell.Event) {
                                v.cursor.DeleteSelection()
                                v.cursor.ResetSelection()
                        }
+
                        v.eh.Insert(v.cursor.Loc(), "\n")
+                       ws := GetLeadingWhitespace(v.buf.lines[v.cursor.y])
                        v.cursor.Right()
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
+
+                       if settings.AutoIndent {
+                               v.eh.Insert(v.cursor.Loc(), ws)
+                               for i := 0; i < len(ws); i++ {
+                                       v.cursor.Right()
+                               }
+                       }
                        v.cursor.lastVisualX = v.cursor.GetVisualX()
-                       // v.UpdateLines(v.cursor.y-1, v.cursor.y)
                case tcell.KeySpace:
                        // Insert a space
                        if v.cursor.HasSelection() {
@@ -390,29 +401,37 @@ func (v *View) HandleEvent(event tcell.Event) {
                        }
                        v.eh.Insert(v.cursor.Loc(), " ")
                        v.cursor.Right()
-                       v.UpdateLines(v.cursor.y, v.cursor.y)
-               case tcell.KeyBackspace2:
+               case tcell.KeyBackspace2, tcell.KeyBackspace:
                        // Delete a character
                        if v.cursor.HasSelection() {
                                v.cursor.DeleteSelection()
                                v.cursor.ResetSelection()
-                               // Rehighlight the entire buffer
-                               v.UpdateLines(v.topline, v.topline+v.height)
                        } else if v.cursor.Loc() > 0 {
                                // We have to do something a bit hacky here because we want to
                                // delete the line by first moving left and then deleting backwards
                                // but the undo redo would place the cursor in the wrong place
                                // So instead we move left, save the position, move back, delete
                                // and restore the position
-                               v.cursor.Left()
-                               cx, cy := v.cursor.x, v.cursor.y
-                               v.cursor.Right()
-                               loc := v.cursor.Loc()
-                               v.eh.Remove(loc-1, loc)
-                               v.cursor.x, v.cursor.y = cx, cy
-                               // Rehighlight the entire buffer
-                               v.UpdateLines(v.topline, v.topline+v.height)
-                               // v.UpdateLines(v.cursor.y, v.cursor.y+1)
+
+                               // If the user is using spaces instead of tabs and they are deleting
+                               // whitespace at the start of the line, we should delete as if its a
+                               // tab (tabSize number of spaces)
+                               lineStart := v.buf.lines[v.cursor.y][:v.cursor.x]
+                               if settings.TabsToSpaces && IsSpaces(lineStart) && len(lineStart) != 0 && len(lineStart)%settings.TabSize == 0 {
+                                       loc := v.cursor.Loc()
+                                       v.cursor.SetLoc(loc - settings.TabSize)
+                                       cx, cy := v.cursor.x, v.cursor.y
+                                       v.cursor.SetLoc(loc)
+                                       v.eh.Remove(loc-settings.TabSize, loc)
+                                       v.cursor.x, v.cursor.y = cx, cy
+                               } else {
+                                       v.cursor.Left()
+                                       cx, cy := v.cursor.x, v.cursor.y
+                                       v.cursor.Right()
+                                       loc := v.cursor.Loc()
+                                       v.eh.Remove(loc-1, loc)
+                                       v.cursor.x, v.cursor.y = cx, cy
+                               }
                        }
                        v.cursor.lastVisualX = v.cursor.GetVisualX()
                case tcell.KeyTab:
@@ -421,9 +440,15 @@ func (v *View) HandleEvent(event tcell.Event) {
                                v.cursor.DeleteSelection()
                                v.cursor.ResetSelection()
                        }
-                       v.eh.Insert(v.cursor.Loc(), "\t")
-                       v.cursor.Right()
-                       v.UpdateLines(v.cursor.y, v.cursor.y)
+                       if settings.TabsToSpaces {
+                               v.eh.Insert(v.cursor.Loc(), Spaces(settings.TabSize))
+                               for i := 0; i < settings.TabSize; i++ {
+                                       v.cursor.Right()
+                               }
+                       } else {
+                               v.eh.Insert(v.cursor.Loc(), "\t")
+                               v.cursor.Right()
+                       }
                case tcell.KeyCtrlS:
                        v.Save()
                case tcell.KeyCtrlF:
@@ -451,30 +476,28 @@ func (v *View) HandleEvent(event tcell.Event) {
                        Search(lastSearch, v, false)
                case tcell.KeyCtrlZ:
                        v.eh.Undo()
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
                case tcell.KeyCtrlY:
                        v.eh.Redo()
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
                case tcell.KeyCtrlC:
                        v.Copy()
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
                case tcell.KeyCtrlX:
                        v.Cut()
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
                case tcell.KeyCtrlV:
                        v.Paste()
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
                case tcell.KeyCtrlA:
                        v.SelectAll()
                case tcell.KeyCtrlO:
                        v.OpenFile()
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
+               case tcell.KeyHome:
+                       v.topline = 0
+                       relocate = false
+               case tcell.KeyEnd:
+                       if v.height > len(v.buf.lines) {
+                               v.topline = 0
+                       } else {
+                               v.topline = len(v.buf.lines) - v.height
+                       }
+                       relocate = false
                case tcell.KeyPgUp:
                        v.PageUp()
                        relocate = false
@@ -487,15 +510,17 @@ func (v *View) HandleEvent(event tcell.Event) {
                case tcell.KeyCtrlD:
                        v.HalfPageDown()
                        relocate = false
+               case tcell.KeyCtrlR:
+                       if settings.Ruler == false {
+                               settings.Ruler = true
+                       } else {
+                               settings.Ruler = false
+                       }
                case tcell.KeyRune:
                        // Insert a character
                        if v.cursor.HasSelection() {
                                v.cursor.DeleteSelection()
                                v.cursor.ResetSelection()
-                               // Rehighlight the entire buffer
-                               v.UpdateLines(v.topline, v.topline+v.height)
-                       } else {
-                               v.UpdateLines(v.cursor.y, v.cursor.y)
                        }
                        v.eh.Insert(v.cursor.Loc(), string(e.Rune()))
                        v.cursor.Right()
@@ -504,9 +529,6 @@ func (v *View) HandleEvent(event tcell.Event) {
                x, y := e.Position()
                x -= v.lineNumOffset - v.leftCol
                y += v.topline
-               // Position always seems to be off by one
-               x--
-               y--
 
                button := e.Buttons()
 
@@ -580,15 +602,11 @@ func (v *View) HandleEvent(event tcell.Event) {
                        v.ScrollUp(2)
                        // We don't want to relocate if the user is scrolling
                        relocate = false
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
                case tcell.WheelDown:
                        // Scroll down two lines
                        v.ScrollDown(2)
                        // We don't want to relocate if the user is scrolling
                        relocate = false
-                       // Rehighlight the entire buffer
-                       v.UpdateLines(v.topline, v.topline+v.height)
                }
        }
 
@@ -602,29 +620,18 @@ func (v *View) HandleEvent(event tcell.Event) {
 
 // DisplayView renders the view to the screen
 func (v *View) DisplayView() {
-       // matches := make(SyntaxMatches, len(v.buf.lines))
-       //
-       // viewStart := v.topline
-       // viewEnd := v.topline + v.height
-       // if viewEnd > len(v.buf.lines) {
-       //      viewEnd = len(v.buf.lines)
-       // }
-       //
-       // lines := v.buf.lines[viewStart:viewEnd]
-       // for i, line := range lines {
-       //      matches[i] = make([]tcell.Style, len(line))
-       // }
-
        // The character number of the character in the top left of the screen
-
        charNum := ToCharPos(0, v.topline, v.buf)
 
        // Convert the length of buffer to a string, and get the length of the string
        // We are going to have to offset by that amount
        maxLineLength := len(strconv.Itoa(len(v.buf.lines)))
        // + 1 for the little space after the line number
-       v.lineNumOffset = maxLineLength + 1
-
+       if settings.Ruler == true {
+               v.lineNumOffset = maxLineLength + 1
+       } else {
+               v.lineNumOffset = 0
+       }
        var highlightStyle tcell.Style
 
        for lineN := 0; lineN < v.height; lineN++ {
@@ -642,20 +649,25 @@ func (v *View) DisplayView() {
                        lineNumStyle = style
                }
                // Write the spaces before the line number if necessary
-               lineNum := strconv.Itoa(lineN + v.topline + 1)
-               for i := 0; i < maxLineLength-len(lineNum); i++ {
-                       screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
-                       x++
-               }
-               // Write the actual line number
-               for _, ch := range lineNum {
-                       screen.SetContent(x, lineN, ch, nil, lineNumStyle)
-                       x++
-               }
-               // Write the extra space
-               screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
-               x++
+               var lineNum string
+               if settings.Ruler == true {
+                       lineNum = strconv.Itoa(lineN + v.topline + 1)
+                       for i := 0; i < maxLineLength-len(lineNum); i++ {
+                               screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
+                               x++
+                       }
+                       // Write the actual line number
+                       for _, ch := range lineNum {
+                               screen.SetContent(x, lineN, ch, nil, lineNumStyle)
+                               x++
+                       }
 
+                       if settings.Ruler == true {
+                               // Write the extra space
+                               screen.SetContent(x, lineN, ' ', nil, lineNumStyle)
+                               x++
+                       }
+               }
                // Write the line
                tabchars := 0
                runes := []rune(line)
@@ -665,17 +677,10 @@ func (v *View) DisplayView() {
                        }
                        ch := runes[colN]
                        var lineStyle tcell.Style
-                       // Does the current character need to be syntax highlighted?
-
-                       // if lineN >= v.updateLines[0] && lineN < v.updateLines[1] {
                        if settings.Syntax {
+                               // Syntax highlighting is enabled
                                highlightStyle = v.matches[lineN][colN]
                        }
-                       // } else if lineN < len(v.lastMatches) && colN < len(v.lastMatches[lineN]) {
-                       // highlightStyle = v.lastMatches[lineN][colN]
-                       // } else {
-                       // highlightStyle = defStyle
-                       // }
 
                        if v.cursor.HasSelection() &&
                                (charNum >= v.cursor.curSelection[0] && charNum < v.cursor.curSelection[1] ||
@@ -689,7 +694,6 @@ func (v *View) DisplayView() {
                        } else {
                                lineStyle = highlightStyle
                        }
-                       // matches[lineN][colN] = highlightStyle
 
                        if ch == '\t' {
                                screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)