From c7f8584d84b332b8a83bcd3fa5c4f81e1622bffa Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Mon, 18 Apr 2016 13:01:39 -0400 Subject: [PATCH] Add tabsToSpaces option --- README.md | 7 +++++-- cmd/micro/help.go | 3 +++ cmd/micro/settings.go | 29 ++++++++++++++++++++--------- cmd/micro/view.go | 11 +++++++++-- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cd2c5c33..3c44c2ae 100644 --- a/README.md +++ b/README.md @@ -122,10 +122,13 @@ Micro has a few options which you can set: * colorscheme * tabsize * syntax +* tabsToSpaces -To set an option run Ctrl-e to execute a command, and type `set option value`, so to set the tabsize to 8 it would be `set tabsize 8`. +To set an option run Ctrl-e to execute a command, and type `set option value`, so to set the tabsize to 8 it would be `set tabsize 8`. The default is 4. -The syntax option can simply be on or off, so for example to turn syntax highlighting off, run `set syntax off`. +The syntax option can simply be on or off, so for example to turn syntax highlighting off, run `set syntax off`. The default is on. + +The tabsToSpaces option is on or off. It specifies whether to use spaces instead of tabs or not. The default is off. The colorscheme can be selected from all the files in the `ConfigDir/colorschemes/` directory. Micro comes by default with three colorschemes: diff --git a/cmd/micro/help.go b/cmd/micro/help.go index a0095abe..dfeff02f 100644 --- a/cmd/micro/help.go +++ b/cmd/micro/help.go @@ -63,6 +63,9 @@ tabsize: sets the tab size to 'option' syntax: turns syntax on or off default value: 'on' + +tabsToSpaces: use spaces instead of tabs + default value: 'off' ` // DisplayHelp displays the help txt diff --git a/cmd/micro/settings.go b/cmd/micro/settings.go index 1b35ef6b..e2c1531b 100644 --- a/cmd/micro/settings.go +++ b/cmd/micro/settings.go @@ -12,14 +12,15 @@ import ( var settings Settings // All the possible settings -var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax"} +var possibleSettings = []string{"colorscheme", "tabsize", "autoindent", "syntax", "tabsToSpaces"} // The Settings struct contains the settings for micro type Settings struct { - Colorscheme string `json:"colorscheme"` - TabSize int `json:"tabsize"` - AutoIndent bool `json:"autoindent"` - Syntax bool `json:"syntax"` + Colorscheme string `json:"colorscheme"` + TabSize int `json:"tabsize"` + AutoIndent bool `json:"autoindent"` + Syntax bool `json:"syntax"` + TabsToSpaces bool `json:"tabsToSpaces"` } // InitSettings initializes the options map and sets all options to their default values @@ -55,10 +56,11 @@ func WriteSettings(filename string) error { // DefaultSettings returns the default settings for micro func DefaultSettings() Settings { return Settings{ - Colorscheme: "default", - TabSize: 4, - AutoIndent: true, - Syntax: true, + Colorscheme: "default", + TabSize: 4, + AutoIndent: true, + Syntax: true, + TabsToSpaces: false, } } @@ -92,6 +94,15 @@ func SetOption(view *View, args []string) { } LoadSyntaxFiles() view.buf.UpdateRules() + } else if option == "tabsToSpaces" { + if value == "on" { + settings.TabsToSpaces = true + } else if value == "off" { + settings.TabsToSpaces = false + } else { + messenger.Error("Invalid value for " + option) + return + } } err := WriteSettings(filename) if err != nil { diff --git a/cmd/micro/view.go b/cmd/micro/view.go index 92718180..dbbd663a 100644 --- a/cmd/micro/view.go +++ b/cmd/micro/view.go @@ -424,8 +424,15 @@ func (v *View) HandleEvent(event tcell.Event) { v.cursor.DeleteSelection() v.cursor.ResetSelection() } - v.eh.Insert(v.cursor.Loc(), "\t") - v.cursor.Right() + 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() + } v.UpdateLines(v.cursor.y, v.cursor.y) case tcell.KeyCtrlS: v.Save()