Add bind and unbind commands

This commit is contained in:
Zachary Yedidia
2019-01-14 16:09:46 -05:00
parent 5825353f64
commit eb49052a48
3 changed files with 71 additions and 1 deletions

View File

@@ -558,6 +558,9 @@ func (h *BufHandler) InsertTab() bool {
// SaveAll saves all open buffers
func (h *BufHandler) SaveAll() bool {
for _, b := range buffer.OpenBuffers {
b.Save()
}
return false
}
@@ -672,7 +675,6 @@ func (h *BufHandler) Undo() bool {
// Redo redoes the last action
func (h *BufHandler) Redo() bool {
// TODO: clear cursors and message
h.Buf.Redo()
InfoBar.Message("Redid action")
return true

View File

@@ -201,6 +201,50 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) {
return false, e
}
// UnbindKey removes the binding for a key from the bindings.json file
func UnbindKey(k string) error {
var e error
var parsed map[string]string
filename := config.ConfigDir + "/bindings.json"
if _, e = os.Stat(filename); e == nil {
input, err := ioutil.ReadFile(filename)
if err != nil {
return errors.New("Error reading bindings.json file: " + err.Error())
}
err = json5.Unmarshal(input, &parsed)
if err != nil {
return errors.New("Error reading bindings.json: " + err.Error())
}
key, ok := findEvent(k)
if !ok {
return errors.New("Invalid event " + k)
}
for ev := range parsed {
if e, ok := findEvent(ev); ok {
if e == key {
delete(parsed, ev)
break
}
}
}
defaults := DefaultBindings()
if a, ok := defaults[k]; ok {
BindKey(k, a)
} else if _, ok := config.Bindings[k]; ok {
delete(config.Bindings, k)
}
txt, _ := json.MarshalIndent(parsed, "", " ")
return ioutil.WriteFile(filename, append(txt, '\n'), 0644)
}
return e
}
var mouseEvents = map[string]tcell.ButtonMask{
"MouseLeft": tcell.Button1,
"MouseMiddle": tcell.Button2,

View File

@@ -35,6 +35,7 @@ var commandActions = map[string]func(*BufHandler, []string){
"ShowKey": (*BufHandler).ShowKeyCmd,
"Run": (*BufHandler).RunCmd,
"Bind": (*BufHandler).BindCmd,
"Unbind": (*BufHandler).UnbindCmd,
"Quit": (*BufHandler).QuitCmd,
"Save": (*BufHandler).SaveCmd,
"Replace": (*BufHandler).ReplaceCmd,
@@ -92,6 +93,7 @@ func DefaultCommands() map[string]StrCommand {
"show": {"Show", []Completion{OptionCompletion, NoCompletion}},
"showkey": {"ShowKey", []Completion{NoCompletion}},
"bind": {"Bind", []Completion{NoCompletion}},
"unbind": {"Unbind", []Completion{NoCompletion}},
"run": {"Run", []Completion{NoCompletion}},
"quit": {"Quit", []Completion{NoCompletion}},
"save": {"Save", []Completion{NoCompletion}},
@@ -458,6 +460,28 @@ func (h *BufHandler) ShowKeyCmd(args []string) {
// BindCmd creates a new keybinding
func (h *BufHandler) BindCmd(args []string) {
if len(args) < 2 {
InfoBar.Error("Not enough arguments")
return
}
_, err := TryBindKey(args[0], args[1], true)
if err != nil {
InfoBar.Error(err)
}
}
// UnbindCmd binds a key to its default action
func (h *BufHandler) UnbindCmd(args []string) {
if len(args) < 1 {
InfoBar.Error("Not enough arguements")
return
}
err := UnbindKey(args[0])
if err != nil {
InfoBar.Error(err)
}
}
// RunCmd runs a shell command in the background