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() {
commands = map[string]Command{
"set": Command{(*BufPane).SetCmd, OptionValueComplete},
"reset": Command{(*BufPane).ResetCmd, OptionValueComplete},
"setlocal": Command{(*BufPane).SetLocalCmd, OptionValueComplete},
"show": Command{(*BufPane).ShowCmd, OptionComplete},
"showkey": Command{(*BufPane).ShowKeyCmd, nil},
@ -347,16 +348,7 @@ func (h *BufPane) NewTabCmd(args []string) {
}
}
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
}
func SetGlobalOptionNative(option string, nativeValue interface{}) error {
config.GlobalSettings[option] = nativeValue
if option == "colorscheme" {
@ -376,12 +368,46 @@ func SetGlobalOption(option, value string) error {
}
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

View file

@ -108,9 +108,6 @@ type Buffer struct {
CurSuggestion int
Messages []*Message
StatusFormatLeft string
StatusFormatRight string
}
// 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)
}
b.SetStatusFormat()
OpenBuffers = append(OpenBuffers, 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
func (b *Buffer) Close() {
for i, buf := range OpenBuffers {

View file

@ -5,17 +5,7 @@ import (
"github.com/zyedidia/micro/internal/screen"
)
// 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
}
func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
b.Settings[option] = nativeValue
if option == "fastdirty" {
@ -45,9 +35,21 @@ func (b *Buffer) SetOption(option, value string) error {
}
} else if option == "encoding" {
b.isModified = true
} else if option == "hidehelp" {
b.SetStatusFormat()
}
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,
"fastdirty": true,
"fileformat": "unix",
"hidehelp": false,
"ignorecase": false,
"indentchar": " ",
"keepautoindent": false,
@ -153,10 +152,12 @@ func DefaultCommonSettings() map[string]interface{} {
"scrollbar": false,
"scrollmargin": float64(3),
"scrollspeed": float64(2),
"softwrap": false,
"smartpaste": true,
"softwrap": false,
"splitbottom": 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,
"syntax": true,
"tabmovement": false,

View file

@ -70,6 +70,7 @@ func (s *StatusLine) Display() {
y := s.win.Height + s.win.Y - 1
b := s.win.Buf
// autocomplete suggestions (for the buffer, not for the infowindow)
if b.HasSuggestions && len(b.Suggestions) > 1 {
statusLineStyle := config.DefStyle.Reverse(true)
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)
rightText := []byte(s.win.Buf.StatusFormatRight)
rightText := []byte(s.win.Buf.Settings["statusformatr"].(string))
rightText = formatParser.ReplaceAllFunc(rightText, formatter)
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.
* `reset option`: resets the given option to its default value
* `retab`: Replaces all leading tabs with spaces or leading spaces with tabs
depending on the value of `tabstospaces`.

View file

@ -200,6 +200,21 @@ Here are the options that you can set:
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.
default value: `true`