Add tabmovement option

This option makes micro treat spaces at the beginning of lines as if
they are tabs. This option only does anything if tabstospaces is already
on. E.g. micro will move over 4 spaces at once when at the start of a
line.

Closes #616
This commit is contained in:
Zachary Yedidia
2017-05-05 12:04:18 -04:00
parent 57110c98e4
commit 18f9b6f34e
4 changed files with 47 additions and 2 deletions

View File

@@ -111,7 +111,21 @@ func (v *View) CursorLeft(usePlugin bool) bool {
v.Cursor.Loc = v.Cursor.CurSelection[0]
v.Cursor.ResetSelection()
} else {
v.Cursor.Left()
tabstospaces := v.Buf.Settings["tabstospaces"].(bool)
tabmovement := v.Buf.Settings["tabmovement"].(bool)
if tabstospaces && tabmovement {
tabsize := int(v.Buf.Settings["tabsize"].(float64))
line := v.Buf.Line(v.Cursor.Y)
if v.Cursor.X-tabsize >= 0 && line[v.Cursor.X-tabsize:v.Cursor.X] == Spaces(tabsize) && IsStrWhitespace(line[0:v.Cursor.X-tabsize]) {
for i := 0; i < tabsize; i++ {
v.Cursor.Left()
}
} else {
v.Cursor.Left()
}
} else {
v.Cursor.Left()
}
}
if usePlugin {
@@ -130,7 +144,21 @@ func (v *View) CursorRight(usePlugin bool) bool {
v.Cursor.Loc = v.Cursor.CurSelection[1].Move(-1, v.Buf)
v.Cursor.ResetSelection()
} else {
v.Cursor.Right()
tabstospaces := v.Buf.Settings["tabstospaces"].(bool)
tabmovement := v.Buf.Settings["tabmovement"].(bool)
if tabstospaces && tabmovement {
tabsize := int(v.Buf.Settings["tabsize"].(float64))
line := v.Buf.Line(v.Cursor.Y)
if v.Cursor.X+tabsize < Count(line) && line[v.Cursor.X:v.Cursor.X+tabsize] == Spaces(tabsize) && IsStrWhitespace(line[0:v.Cursor.X]) {
for i := 0; i < tabsize; i++ {
v.Cursor.Right()
}
} else {
v.Cursor.Right()
}
} else {
v.Cursor.Right()
}
}
if usePlugin {

View File

@@ -196,6 +196,7 @@ func DefaultGlobalSettings() map[string]interface{} {
"splitBottom": true,
"statusline": true,
"syntax": true,
"tabmovement": false,
"tabsize": float64(4),
"tabstospaces": false,
"termtitle": false,
@@ -231,6 +232,7 @@ func DefaultLocalSettings() map[string]interface{} {
"splitBottom": true,
"statusline": true,
"syntax": true,
"tabmovement": false,
"tabsize": float64(4),
"tabstospaces": false,
"useprimary": true,

View File

@@ -82,6 +82,16 @@ func IsWhitespace(c rune) bool {
return c == ' ' || c == '\t' || c == '\n'
}
// IsStrWhitespace returns true if the given string is all whitespace
func IsStrWhitespace(str string) bool {
for _, c := range str {
if !IsWhitespace(c) {
return false
}
}
return true
}
// Contains returns whether or not a string array contains a given string
func Contains(list []string, a string) bool {
for _, b := range list {