From 4ca6d9b75ba7fc722339d8fb46bfe04e530eb0ab Mon Sep 17 00:00:00 2001 From: EnumDev Date: Tue, 10 Jun 2025 20:52:41 +0300 Subject: [PATCH] Add accepted cursor modes to key bindings --- src/keybindings.go | 75 +++++++++++++++++++++++++++------------------- src/window.go | 10 +++---- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/src/keybindings.go b/src/keybindings.go index 44c6535..68ac689 100644 --- a/src/keybindings.go +++ b/src/keybindings.go @@ -6,8 +6,9 @@ import ( ) type Keybinding struct { - keybind string - command string + keybind string + cursorModes []CursorMode + command string } var Keybinds = make([]Keybinding, 0) @@ -15,60 +16,74 @@ var Keybinds = make([]Keybinding, 0) func initKeybindings() { // Add key bindings Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-Q", - command: "quit", + keybind: "Ctrl-Q", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "quit", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-C", - command: "copy", + keybind: "Ctrl-C", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "copy", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-V", - command: "paste", + keybind: "Ctrl-V", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "paste", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-S", - command: "save", + keybind: "Ctrl-S", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "save", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-O", - command: "open", + keybind: "Ctrl-O", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "open", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-R", - command: "reload", + keybind: "Ctrl-R", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "reload", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "PgUp", - command: "prev-buffer", + keybind: "PgUp", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "prev-buffer", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "PgDn", - command: "next-buffer", + keybind: "PgDn", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "next-buffer", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-N", - command: "new-buffer", + keybind: "Ctrl-N", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "new-buffer", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Delete", - command: "close-buffer", + keybind: "Delete", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "close-buffer", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "Ctrl-Q", - command: "quit", + keybind: "Ctrl-Q", + cursorModes: []CursorMode{CursorModeBuffer}, + command: "quit", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "F1", - command: "menu-file", + keybind: "F1", + cursorModes: []CursorMode{CursorModeBuffer, CursorModeDropdown}, + command: "menu-file", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "F2", - command: "menu-edit", + keybind: "F2", + cursorModes: []CursorMode{CursorModeBuffer, CursorModeDropdown}, + command: "menu-edit", }) Keybinds = append(Keybinds, Keybinding{ - keybind: "F3", - command: "menu-buffers", + keybind: "F3", + cursorModes: []CursorMode{CursorModeBuffer, CursorModeDropdown}, + command: "menu-buffers", }) } diff --git a/src/window.go b/src/window.go index 279668a..ecb2c02 100644 --- a/src/window.go +++ b/src/window.go @@ -332,12 +332,10 @@ func (window *Window) input(ev *tcell.EventKey) { } // Check key bindings - if window.CursorMode == CursorModeBuffer { - for _, keybinding := range Keybinds { - if keybinding.IsPressed(ev) { - RunCommand(window, keybinding.command) - return - } + for _, keybinding := range Keybinds { + if keybinding.IsPressed(ev) && slices.Index(keybinding.cursorModes, window.CursorMode) != -1 { + RunCommand(window, keybinding.command) + return } }