Add reset command and statusline format string options

This commit is contained in:
Zachary Yedidia
2019-06-16 17:35:00 -04:00
parent 8d85cae4c0
commit 809b95d290
8 changed files with 88 additions and 81 deletions

View File

@@ -30,6 +30,7 @@ var commands map[string]Command
func InitCommands() { func InitCommands() {
commands = map[string]Command{ commands = map[string]Command{
"set": Command{(*BufPane).SetCmd, OptionValueComplete}, "set": Command{(*BufPane).SetCmd, OptionValueComplete},
"reset": Command{(*BufPane).ResetCmd, OptionValueComplete},
"setlocal": Command{(*BufPane).SetLocalCmd, OptionValueComplete}, "setlocal": Command{(*BufPane).SetLocalCmd, OptionValueComplete},
"show": Command{(*BufPane).ShowCmd, OptionComplete}, "show": Command{(*BufPane).ShowCmd, OptionComplete},
"showkey": Command{(*BufPane).ShowKeyCmd, nil}, "showkey": Command{(*BufPane).ShowKeyCmd, nil},
@@ -347,16 +348,7 @@ func (h *BufPane) NewTabCmd(args []string) {
} }
} }
func SetGlobalOption(option, value string) error { func SetGlobalOptionNative(option string, nativeValue interface{}) error {
if _, ok := config.GlobalSettings[option]; !ok {
return config.ErrInvalidOption
}
nativeValue, err := config.GetNativeValue(option, config.GlobalSettings[option], value)
if err != nil {
return err
}
config.GlobalSettings[option] = nativeValue config.GlobalSettings[option] = nativeValue
if option == "colorscheme" { if option == "colorscheme" {
@@ -376,12 +368,46 @@ func SetGlobalOption(option, value string) error {
} }
for _, b := range buffer.OpenBuffers { for _, b := range buffer.OpenBuffers {
b.SetOption(option, value) b.SetOptionNative(option, nativeValue)
} }
config.WriteSettings(config.ConfigDir + "/settings.json") return config.WriteSettings(config.ConfigDir + "/settings.json")
}
return nil func SetGlobalOption(option, value string) error {
if _, ok := config.GlobalSettings[option]; !ok {
return config.ErrInvalidOption
}
nativeValue, err := config.GetNativeValue(option, config.GlobalSettings[option], value)
if err != nil {
return err
}
return SetGlobalOptionNative(option, nativeValue)
}
// ResetCmd resets a setting to its default value
func (h *BufPane) ResetCmd(args []string) {
if len(args) < 1 {
InfoBar.Error("Not enough arguments")
return
}
option := args[0]
defaultGlobals := config.DefaultGlobalSettings()
defaultLocals := config.DefaultLocalSettings()
if _, ok := defaultGlobals[option]; ok {
SetGlobalOptionNative(option, defaultGlobals[option])
return
}
if _, ok := defaultLocals[option]; ok {
h.Buf.SetOptionNative(option, defaultLocals[option])
return
}
InfoBar.Error(config.ErrInvalidOption)
} }
// SetCmd sets an option // SetCmd sets an option

View File

@@ -108,9 +108,6 @@ type Buffer struct {
CurSuggestion int CurSuggestion int
Messages []*Message Messages []*Message
StatusFormatLeft string
StatusFormatRight string
} }
// NewBufferFromFile opens a new buffer using the given path // NewBufferFromFile opens a new buffer using the given path
@@ -244,25 +241,11 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
screen.TermMessage(err) screen.TermMessage(err)
} }
b.SetStatusFormat()
OpenBuffers = append(OpenBuffers, b) OpenBuffers = append(OpenBuffers, b)
return b return b
} }
// SetStatusFormat will correctly set the format string for the
// status line
func (b *Buffer) SetStatusFormat() {
if b.Settings["hidehelp"].(bool) {
b.StatusFormatLeft = "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat) $(opt:encoding)"
b.StatusFormatRight = ""
} else {
b.StatusFormatLeft = "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat) $(opt:encoding)"
b.StatusFormatRight = "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): toggle help"
}
}
// Close removes this buffer from the list of open buffers // Close removes this buffer from the list of open buffers
func (b *Buffer) Close() { func (b *Buffer) Close() {
for i, buf := range OpenBuffers { for i, buf := range OpenBuffers {

View File

@@ -5,17 +5,7 @@ import (
"github.com/zyedidia/micro/internal/screen" "github.com/zyedidia/micro/internal/screen"
) )
// SetOption sets a given option to a value just for this buffer func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
func (b *Buffer) SetOption(option, value string) error {
if _, ok := b.Settings[option]; !ok {
return config.ErrInvalidOption
}
nativeValue, err := config.GetNativeValue(option, b.Settings[option], value)
if err != nil {
return err
}
b.Settings[option] = nativeValue b.Settings[option] = nativeValue
if option == "fastdirty" { if option == "fastdirty" {
@@ -45,9 +35,21 @@ func (b *Buffer) SetOption(option, value string) error {
} }
} else if option == "encoding" { } else if option == "encoding" {
b.isModified = true b.isModified = true
} else if option == "hidehelp" {
b.SetStatusFormat()
} }
return nil return nil
} }
// SetOption sets a given option to a value just for this buffer
func (b *Buffer) SetOption(option, value string) error {
if _, ok := b.Settings[option]; !ok {
return config.ErrInvalidOption
}
nativeValue, err := config.GetNativeValue(option, b.Settings[option], value)
if err != nil {
return err
}
return b.SetOptionNative(option, nativeValue)
}

File diff suppressed because one or more lines are too long

View File

@@ -140,7 +140,6 @@ func DefaultCommonSettings() map[string]interface{} {
"eofnewline": false, "eofnewline": false,
"fastdirty": true, "fastdirty": true,
"fileformat": "unix", "fileformat": "unix",
"hidehelp": false,
"ignorecase": false, "ignorecase": false,
"indentchar": " ", "indentchar": " ",
"keepautoindent": false, "keepautoindent": false,
@@ -153,10 +152,12 @@ func DefaultCommonSettings() map[string]interface{} {
"scrollbar": false, "scrollbar": false,
"scrollmargin": float64(3), "scrollmargin": float64(3),
"scrollspeed": float64(2), "scrollspeed": float64(2),
"softwrap": false,
"smartpaste": true, "smartpaste": true,
"softwrap": false,
"splitbottom": true, "splitbottom": true,
"splitright": true, "splitright": true,
"statusformatl": "$(filename) $(modified)($(line),$(col)) $(opt:filetype) $(opt:fileformat) $(opt:encoding)",
"statusformatr": "$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp): toggle help",
"statusline": true, "statusline": true,
"syntax": true, "syntax": true,
"tabmovement": false, "tabmovement": false,

View File

@@ -70,6 +70,7 @@ func (s *StatusLine) Display() {
y := s.win.Height + s.win.Y - 1 y := s.win.Height + s.win.Y - 1
b := s.win.Buf b := s.win.Buf
// autocomplete suggestions (for the buffer, not for the infowindow)
if b.HasSuggestions && len(b.Suggestions) > 1 { if b.HasSuggestions && len(b.Suggestions) > 1 {
statusLineStyle := config.DefStyle.Reverse(true) statusLineStyle := config.DefStyle.Reverse(true)
if style, ok := config.Colorscheme["statusline"]; ok { if style, ok := config.Colorscheme["statusline"]; ok {
@@ -124,9 +125,9 @@ func (s *StatusLine) Display() {
} }
} }
leftText := []byte(s.win.Buf.StatusFormatLeft) leftText := []byte(s.win.Buf.Settings["statusformatl"].(string))
leftText = formatParser.ReplaceAllFunc(leftText, formatter) leftText = formatParser.ReplaceAllFunc(leftText, formatter)
rightText := []byte(s.win.Buf.StatusFormatRight) rightText := []byte(s.win.Buf.Settings["statusformatr"].(string))
rightText = formatParser.ReplaceAllFunc(rightText, formatter) rightText = formatParser.ReplaceAllFunc(rightText, formatter)
statusLineStyle := config.DefStyle.Reverse(true) statusLineStyle := config.DefStyle.Reverse(true)

View File

@@ -77,6 +77,8 @@ Here are the possible commands that you can use.
* `open filename`: Open a file in the current buffer. * `open filename`: Open a file in the current buffer.
* `reset option`: resets the given option to its default value
* `retab`: Replaces all leading tabs with spaces or leading spaces with tabs * `retab`: Replaces all leading tabs with spaces or leading spaces with tabs
depending on the value of `tabstospaces`. depending on the value of `tabstospaces`.

View File

@@ -200,6 +200,21 @@ Here are the options that you can set:
default value: `true` default value: `true`
* `statusformatl`: format string definition for the left-justified part of the
statusline. Special directives should be placed inside `$()`. Special
directives include: `filename`, `modified`, `line`, `col`, `opt`, `bind`.
The `opt` and `bind` directives take either an option or an action afterward
and fill in the value of the option or the key bound to the action.
default value: `$(filename) $(modified)($(line),$(col)) $(opt:filetype)
$(opt:fileformat) $(opt:encoding)`
* `statusformatl`: format string definition for the left-justified part of the
statusline.
default value: `$(bind:ToggleKeyMenu): show bindings, $(bind:ToggleHelp):
toggle help`
* `statusline`: display the status line at the bottom of the screen. * `statusline`: display the status line at the bottom of the screen.
default value: `true` default value: `true`