Skip to content

Commit 0b9c7c0

Browse files
authored
Add toggle & togglelocal command (#3783)
1 parent e9f241a commit 0b9c7c0

File tree

3 files changed

+109
-40
lines changed

3 files changed

+109
-40
lines changed

‎internal/action/command.go‎

Lines changed: 94 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,39 +32,41 @@ var commands map[string]Command
3232

3333
func InitCommands() {
3434
commands = map[string]Command{
35-
"set": {(*BufPane).SetCmd, OptionValueComplete},
36-
"reset": {(*BufPane).ResetCmd, OptionValueComplete},
37-
"setlocal": {(*BufPane).SetLocalCmd, OptionValueComplete},
38-
"show": {(*BufPane).ShowCmd, OptionComplete},
39-
"showkey": {(*BufPane).ShowKeyCmd, nil},
40-
"run": {(*BufPane).RunCmd, nil},
41-
"bind": {(*BufPane).BindCmd, nil},
42-
"unbind": {(*BufPane).UnbindCmd, nil},
43-
"quit": {(*BufPane).QuitCmd, nil},
44-
"goto": {(*BufPane).GotoCmd, nil},
45-
"jump": {(*BufPane).JumpCmd, nil},
46-
"save": {(*BufPane).SaveCmd, nil},
47-
"replace": {(*BufPane).ReplaceCmd, nil},
48-
"replaceall": {(*BufPane).ReplaceAllCmd, nil},
49-
"vsplit": {(*BufPane).VSplitCmd, buffer.FileComplete},
50-
"hsplit": {(*BufPane).HSplitCmd, buffer.FileComplete},
51-
"tab": {(*BufPane).NewTabCmd, buffer.FileComplete},
52-
"help": {(*BufPane).HelpCmd, HelpComplete},
53-
"eval": {(*BufPane).EvalCmd, nil},
54-
"log": {(*BufPane).ToggleLogCmd, nil},
55-
"plugin": {(*BufPane).PluginCmd, PluginComplete},
56-
"reload": {(*BufPane).ReloadCmd, nil},
57-
"reopen": {(*BufPane).ReopenCmd, nil},
58-
"cd": {(*BufPane).CdCmd, buffer.FileComplete},
59-
"pwd": {(*BufPane).PwdCmd, nil},
60-
"open": {(*BufPane).OpenCmd, buffer.FileComplete},
61-
"tabmove": {(*BufPane).TabMoveCmd, nil},
62-
"tabswitch": {(*BufPane).TabSwitchCmd, nil},
63-
"term": {(*BufPane).TermCmd, nil},
64-
"memusage": {(*BufPane).MemUsageCmd, nil},
65-
"retab": {(*BufPane).RetabCmd, nil},
66-
"raw": {(*BufPane).RawCmd, nil},
67-
"textfilter": {(*BufPane).TextFilterCmd, nil},
35+
"set": {(*BufPane).SetCmd, OptionValueComplete},
36+
"setlocal": {(*BufPane).SetLocalCmd, OptionValueComplete},
37+
"toggle": {(*BufPane).ToggleCmd, OptionValueComplete},
38+
"togglelocal": {(*BufPane).ToggleLocalCmd, OptionValueComplete},
39+
"reset": {(*BufPane).ResetCmd, OptionValueComplete},
40+
"show": {(*BufPane).ShowCmd, OptionComplete},
41+
"showkey": {(*BufPane).ShowKeyCmd, nil},
42+
"run": {(*BufPane).RunCmd, nil},
43+
"bind": {(*BufPane).BindCmd, nil},
44+
"unbind": {(*BufPane).UnbindCmd, nil},
45+
"quit": {(*BufPane).QuitCmd, nil},
46+
"goto": {(*BufPane).GotoCmd, nil},
47+
"jump": {(*BufPane).JumpCmd, nil},
48+
"save": {(*BufPane).SaveCmd, nil},
49+
"replace": {(*BufPane).ReplaceCmd, nil},
50+
"replaceall": {(*BufPane).ReplaceAllCmd, nil},
51+
"vsplit": {(*BufPane).VSplitCmd, buffer.FileComplete},
52+
"hsplit": {(*BufPane).HSplitCmd, buffer.FileComplete},
53+
"tab": {(*BufPane).NewTabCmd, buffer.FileComplete},
54+
"help": {(*BufPane).HelpCmd, HelpComplete},
55+
"eval": {(*BufPane).EvalCmd, nil},
56+
"log": {(*BufPane).ToggleLogCmd, nil},
57+
"plugin": {(*BufPane).PluginCmd, PluginComplete},
58+
"reload": {(*BufPane).ReloadCmd, nil},
59+
"reopen": {(*BufPane).ReopenCmd, nil},
60+
"cd": {(*BufPane).CdCmd, buffer.FileComplete},
61+
"pwd": {(*BufPane).PwdCmd, nil},
62+
"open": {(*BufPane).OpenCmd, buffer.FileComplete},
63+
"tabmove": {(*BufPane).TabMoveCmd, nil},
64+
"tabswitch": {(*BufPane).TabSwitchCmd, nil},
65+
"term": {(*BufPane).TermCmd, nil},
66+
"memusage": {(*BufPane).MemUsageCmd, nil},
67+
"retab": {(*BufPane).RetabCmd, nil},
68+
"raw": {(*BufPane).RawCmd, nil},
69+
"textfilter": {(*BufPane).TextFilterCmd, nil},
6870
}
6971
}
7072

@@ -730,6 +732,65 @@ func (h *BufPane) SetLocalCmd(args []string) {
730732
}
731733
}
732734

735+
func (h *BufPane) toggleOption(option string, local bool) error {
736+
var curVal, newVal any
737+
738+
if local {
739+
curVal = h.Buf.Settings[option]
740+
} else {
741+
curVal = config.GetGlobalOption(option)
742+
}
743+
if curVal == nil {
744+
return config.ErrInvalidOption
745+
}
746+
747+
if choices, ok := config.OptionChoices[option]; ok && len(choices) == 2 {
748+
if curVal == choices[0] {
749+
newVal = choices[1]
750+
} else {
751+
newVal = choices[0]
752+
}
753+
} else if curValBool, ok := curVal.(bool); ok {
754+
newVal = !curValBool
755+
} else {
756+
return config.ErrOptNotToggleable
757+
}
758+
759+
if local {
760+
if err := h.Buf.SetOptionNative(option, newVal); err != nil {
761+
return err
762+
}
763+
} else {
764+
if err := SetGlobalOptionNative(option, newVal); err != nil {
765+
return err
766+
}
767+
}
768+
769+
return nil
770+
}
771+
772+
// ToggleCmd toggles a toggleable option
773+
func (h *BufPane) ToggleCmd(args []string) {
774+
if len(args) < 1 {
775+
InfoBar.Error("Not enough arguments: provide a toggleable option")
776+
return
777+
}
778+
if err := h.toggleOption(args[0], false); err != nil {
779+
InfoBar.Error(err)
780+
}
781+
}
782+
783+
// ToggleCmd toggles a toggleable option local to the buffer
784+
func (h *BufPane) ToggleLocalCmd(args []string) {
785+
if len(args) < 1 {
786+
InfoBar.Error("Not enough arguments: provide a toggleable option")
787+
return
788+
}
789+
if err := h.toggleOption(args[0], true); err != nil {
790+
InfoBar.Error(err)
791+
}
792+
}
793+
733794
// ShowCmd shows the value of the given option
734795
func (h *BufPane) ShowCmd(args []string) {
735796
if len(args) < 1 {

‎internal/config/settings.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ var LocalSettings = []string{
139139
}
140140

141141
var (
142-
ErrInvalidOption = errors.New("Invalid option")
143-
ErrInvalidValue = errors.New("Invalid value")
142+
ErrInvalidOption = errors.New("Invalid option")
143+
ErrInvalidValue = errors.New("Invalid value")
144+
ErrOptNotToggleable = errors.New("Option not toggleable")
144145

145146
// The options that the user can set
146147
GlobalSettings map[string]any

‎runtime/help/commands.md‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,20 @@ quotes here but these are not necessary when entering the command in micro.
7272
* `setlocal 'option' 'value'`: sets the option to value locally (only in the
7373
current buffer). This will *not* modify `settings.json`.
7474

75+
* `toggle 'option'`: toggles the option. Only works with options that accept
76+
exactly two values. This will modify your `settings.json` with the new value.
77+
78+
* `togglelocal 'option'`: toggles the option locally (only in the
79+
current buffer). Only works with options that accept exactly two values.
80+
This will *not* modify `settings.json`.
81+
82+
* `reset 'option'`: resets the given option to its default value.
83+
7584
* `show 'option'`: shows the current value of the given option.
7685

86+
* `showkey 'key'`: Show the action(s) bound to a given key. For example
87+
running `> showkey Ctrl-c` will display `Copy`.
88+
7789
* `run 'sh-command'`: runs the given shell command in the background. The
7890
command's output will be displayed in one line when it finishes running.
7991

@@ -129,8 +141,6 @@ quotes here but these are not necessary when entering the command in micro.
129141

130142
* `reopen`: Reopens the current file from disk.
131143

132-
* `reset 'option'`: resets the given option to its default value
133-
134144
* `retab`: Replaces all leading tabs with spaces or leading spaces with tabs
135145
depending on the value of `tabstospaces`.
136146

@@ -139,9 +149,6 @@ quotes here but these are not necessary when entering the command in micro.
139149
the terminal and helps you see which bindings aren't possible and why. This
140150
is most useful for debugging keybindings.
141151

142-
* `showkey 'key'`: Show the action(s) bound to a given key. For example
143-
running `> showkey Ctrl-c` will display `Copy`.
144-
145152
* `term ['exec']`: Open a terminal emulator running the given executable. If no
146153
executable is given, this will open the default shell in the terminal
147154
emulator.

0 commit comments

Comments
 (0)