]> git.lizzy.rs Git - micro.git/blobdiff - cmd/micro/view.go
Toggle line numbers
[micro.git] / cmd / micro / view.go
index 31acf35db5547094c7e1c7ea1cc70cf78f579883..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.
@@ -280,6 +282,8 @@ func (v *View) OpenFile() {
                if canceled {
                        return
                }
+               home, _ := homedir.Dir()
+               filename = strings.Replace(filename, "~", home, 1)
                file, err := ioutil.ReadFile(filename)
 
                if err != nil {
@@ -408,12 +412,26 @@ func (v *View) HandleEvent(event tcell.Event) {
                                // 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
+
+                               // 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:
@@ -492,6 +510,12 @@ 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() {
@@ -603,8 +627,11 @@ func (v *View) DisplayView() {
        // 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++ {
@@ -622,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)