1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/keybindings.go

668 lines
20 KiB
Go
Raw Normal View History

package gui
2018-08-08 11:18:41 +02:00
import (
2019-12-05 05:16:47 +02:00
"log"
2019-12-05 04:01:01 +02:00
"strings"
2020-08-12 11:19:32 +02:00
"unicode/utf8"
2020-01-07 12:42:33 +02:00
"github.com/jesseduffield/gocui"
2021-04-11 15:32:20 +02:00
"github.com/jesseduffield/lazygit/pkg/constants"
2022-02-13 01:39:14 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
2018-08-08 11:18:41 +02:00
func (gui *Gui) getKeyDisplay(name string) string {
key := gui.getKey(name)
return keybindings.GetKeyDisplay(key)
}
2022-03-26 06:02:32 +02:00
func (gui *Gui) getKey(key string) types.Key {
2020-08-12 11:19:32 +02:00
runeCount := utf8.RuneCountInString(key)
if runeCount > 1 {
binding := keybindings.Keymap[strings.ToLower(key)]
2019-12-05 05:16:47 +02:00
if binding == nil {
2021-04-11 15:32:20 +02:00
log.Fatalf("Unrecognized key %s for keybinding. For permitted values see %s", strings.ToLower(key), constants.Links.Docs.CustomKeybindings)
2019-12-05 05:16:47 +02:00
} else {
return binding
}
2020-08-12 11:19:32 +02:00
} else if runeCount == 1 {
2019-12-05 04:01:01 +02:00
return []rune(key)[0]
}
2020-10-03 06:54:55 +02:00
log.Fatal("Key empty for keybinding: " + strings.ToLower(key))
2019-12-05 04:01:01 +02:00
return nil
}
func (gui *Gui) noPopupPanel(f func() error) func() error {
return func() error {
if gui.popupPanelFocused() {
return nil
}
return f()
}
}
2022-02-13 01:39:14 +02:00
// only to be called from the cheatsheet generate script. This mutates the Gui struct.
func (self *Gui) GetCheatsheetKeybindings() []*types.Binding {
self.g = &gocui.Gui{}
if err := self.createAllViews(); err != nil {
panic(err)
}
// need to instantiate views
2022-02-13 01:39:14 +02:00
self.helpers = helpers.NewStubHelpers()
self.State = &GuiRepoState{}
self.State.Contexts = self.contextTree()
self.resetControllers()
bindings, _ := self.GetInitialKeybindings()
return bindings
}
2022-02-06 06:54:26 +02:00
// renaming receiver to 'self' to aid refactoring. Will probably end up moving all Gui handlers to this pattern eventually.
func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBinding) {
config := self.c.UserConfig.Keybinding
guards := types.KeybindingGuards{
2022-02-06 06:54:26 +02:00
OutsideFilterMode: self.outsideFilterMode,
NoPopupPanel: self.noPopupPanel,
}
opts := types.KeybindingsOpts{
GetKey: self.getKey,
Config: config,
Guards: guards,
}
2020-10-03 06:54:55 +02:00
2022-01-28 11:44:36 +02:00
bindings := []*types.Binding{
2018-09-01 12:10:03 +02:00
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Quit),
2018-09-01 12:10:03 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleQuit,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.QuitWithoutChangingDirectory),
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleQuitWithoutChangingDirectory,
2019-11-16 03:41:04 +02:00
},
{
2018-09-01 12:10:03 +02:00
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.QuitAlt1),
2018-09-01 12:10:03 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleQuit,
2019-11-16 03:41:04 +02:00
},
{
2018-09-01 12:10:03 +02:00
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Return),
2018-09-01 12:10:03 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleTopLevelReturn,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.OpenRecentRepos),
Handler: self.handleCreateRecentReposMenu,
Description: self.c.Tr.SwitchRepo,
},
2019-11-16 03:41:04 +02:00
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollUpMain),
Handler: self.scrollUpMain,
Alternative: "fn+up/shift+k",
2022-02-06 06:54:26 +02:00
Description: self.c.Tr.LcScrollUpMainPanel,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollDownMain),
Handler: self.scrollDownMain,
Alternative: "fn+down/shift+j",
2022-02-06 06:54:26 +02:00
Description: self.c.Tr.LcScrollDownMainPanel,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollUpMainAlt1),
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpMain,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollDownMainAlt1),
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownMain,
2019-11-16 03:41:04 +02:00
},
{
2018-09-01 12:10:03 +02:00
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollUpMainAlt2),
2018-09-01 12:10:03 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpMain,
2019-11-16 03:41:04 +02:00
},
{
2018-09-01 12:10:03 +02:00
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollDownMainAlt2),
2018-09-01 12:10:03 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownMain,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CreateRebaseOptionsMenu),
Handler: self.helpers.MergeAndRebase.CreateRebaseOptionsMenu,
Description: self.c.Tr.ViewMergeRebaseOptions,
OpensMenu: true,
2019-11-16 03:41:04 +02:00
},
2020-01-03 14:01:32 +02:00
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CreatePatchOptionsMenu),
Handler: self.handleCreatePatchOptionsMenu,
Description: self.c.Tr.ViewPatchOptions,
OpensMenu: true,
2020-01-03 14:01:32 +02:00
},
2019-11-16 03:41:04 +02:00
{
2018-09-05 13:23:06 +02:00
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Refresh),
Handler: self.handleRefresh,
Description: self.c.Tr.LcRefresh,
2019-11-16 03:41:04 +02:00
},
{
2020-03-12 07:29:15 +02:00
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.OptionMenu),
Handler: self.handleCreateOptionsMenu,
Description: self.c.Tr.LcOpenMenu,
OpensMenu: true,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.OptionMenuAlt1),
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleCreateOptionsMenu,
2019-11-16 03:41:04 +02:00
},
{
2018-09-01 12:10:03 +02:00
ViewName: "status",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.handleEditConfig,
Description: self.c.Tr.EditConfig,
2020-02-24 23:32:46 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextScreenMode),
Handler: self.nextScreenMode,
Description: self.c.Tr.LcNextScreenMode,
2020-02-24 23:32:46 +02:00
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevScreenMode),
Handler: self.prevScreenMode,
Description: self.c.Tr.LcPrevScreenMode,
2019-11-16 03:41:04 +02:00
},
{
2018-09-01 12:10:03 +02:00
ViewName: "status",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.OpenFile),
Handler: self.handleOpenConfig,
Description: self.c.Tr.OpenConfig,
2019-11-16 03:41:04 +02:00
},
{
2018-09-01 12:10:03 +02:00
ViewName: "status",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Status.CheckForUpdate),
Handler: self.handleCheckForUpdate,
Description: self.c.Tr.LcCheckForUpdate,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "status",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Status.RecentRepos),
Handler: self.handleCreateRecentReposMenu,
Description: self.c.Tr.SwitchRepo,
},
2020-11-27 09:07:14 +02:00
{
ViewName: "status",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Status.AllBranchesLogGraph),
Handler: self.handleShowAllBranchLogs,
Description: self.c.Tr.LcAllBranchesLogGraph,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "files",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
Handler: self.handleCopySelectedSideContextItemToClipboard,
Description: self.c.Tr.LcCopyFileNameToClipboard,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "localBranches",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
Handler: self.handleCopySelectedSideContextItemToClipboard,
Description: self.c.Tr.LcCopyBranchNameToClipboard,
2020-04-15 12:30:24 +02:00
},
{
ViewName: "commits",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
Handler: self.handleCopySelectedSideContextItemToClipboard,
Description: self.c.Tr.LcCopyCommitShaToClipboard,
2020-04-15 12:30:24 +02:00
},
{
ViewName: "commits",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Commits.ResetCherryPick),
Handler: self.helpers.CherryPick.Reset,
Description: self.c.Tr.LcResetCherryPick,
2022-01-19 09:32:27 +02:00
},
{
ViewName: "reflogCommits",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
Handler: self.handleCopySelectedSideContextItemToClipboard,
Description: self.c.Tr.LcCopyCommitShaToClipboard,
2020-09-19 12:55:52 +02:00
},
2020-08-22 00:49:02 +02:00
{
ViewName: "subCommits",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
Handler: self.handleCopySelectedSideContextItemToClipboard,
Description: self.c.Tr.LcCopyCommitShaToClipboard,
},
2019-11-16 03:41:04 +02:00
{
ViewName: "information",
2019-02-25 13:11:35 +02:00
Key: gocui.MouseLeft,
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleInfoClick,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "commitFiles",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
Handler: self.handleCopySelectedSideContextItemToClipboard,
Description: self.c.Tr.LcCopyCommitFileNameToClipboard,
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.FilteringMenu),
Handler: self.handleCreateFilteringMenuPanel,
Description: self.c.Tr.LcOpenFilteringMenu,
OpensMenu: true,
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.DiffingMenu),
Handler: self.handleCreateDiffingMenuPanel,
Description: self.c.Tr.LcOpenDiffingMenu,
OpensMenu: true,
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.DiffingMenuAlt),
Handler: self.handleCreateDiffingMenuPanel,
Description: self.c.Tr.LcOpenDiffingMenu,
OpensMenu: true,
},
{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ExtrasMenu),
Handler: self.handleCreateExtrasMenuPanel,
Description: self.c.Tr.LcOpenExtrasMenu,
OpensMenu: true,
},
2019-11-10 07:50:36 +02:00
{
ViewName: "secondary",
Key: gocui.MouseWheelUp,
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpSecondary,
2019-11-10 07:50:36 +02:00
},
{
ViewName: "secondary",
Key: gocui.MouseWheelDown,
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownSecondary,
2019-11-10 07:50:36 +02:00
},
2019-11-16 03:41:04 +02:00
{
ViewName: "main",
Key: gocui.MouseWheelDown,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownMain,
Description: self.c.Tr.ScrollDown,
2019-11-16 03:41:04 +02:00
Alternative: "fn+up",
},
{
ViewName: "main",
Key: gocui.MouseWheelUp,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpMain,
Description: self.c.Tr.ScrollUp,
2019-11-16 03:41:04 +02:00
Alternative: "fn+down",
},
{
ViewName: "secondary",
Key: gocui.MouseWheelUp,
Modifier: gocui.ModNone,
Handler: self.scrollUpSecondary,
2019-11-16 03:41:04 +02:00
},
2021-11-02 11:35:53 +02:00
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollLeft),
Handler: self.scrollLeftMain,
Description: self.c.Tr.LcScrollLeft,
2022-02-27 07:46:27 +02:00
Tag: "navigation",
2021-11-02 11:35:53 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ScrollRight),
Handler: self.scrollRightMain,
Description: self.c.Tr.LcScrollRight,
2022-02-27 07:46:27 +02:00
Tag: "navigation",
2021-11-02 11:35:53 +02:00
},
2019-11-16 03:41:04 +02:00
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Return),
Handler: self.handleEscapeMerge,
Description: self.c.Tr.ReturnToFilesPanel,
2019-11-16 03:41:04 +02:00
},
2021-04-11 02:05:39 +02:00
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Files.OpenMergeTool),
2022-03-26 06:52:35 +02:00
Handler: self.helpers.WorkingTree.OpenMergeTool,
2022-02-06 06:54:26 +02:00
Description: self.c.Tr.LcOpenMergeTool,
2021-04-11 02:05:39 +02:00
},
2019-11-16 03:41:04 +02:00
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Select),
Handler: self.handlePickHunk,
Description: self.c.Tr.PickHunk,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Main.PickBothHunks),
Handler: self.handlePickAllHunks,
Description: self.c.Tr.PickAllHunks,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevBlock),
Handler: self.handleSelectPrevConflict,
Description: self.c.Tr.PrevConflict,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextBlock),
Handler: self.handleSelectNextConflict,
Description: self.c.Tr.NextConflict,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevItem),
Handler: self.handleSelectPrevConflictHunk,
Description: self.c.Tr.SelectPrevHunk,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextItem),
Handler: self.handleSelectNextConflictHunk,
Description: self.c.Tr.SelectNextHunk,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt),
2019-11-16 03:41:04 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleSelectPrevConflict,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextBlockAlt),
2019-11-16 03:41:04 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleSelectNextConflict,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevItemAlt),
2019-11-16 03:41:04 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleSelectPrevConflictHunk,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextItemAlt),
2019-11-16 03:41:04 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleSelectNextConflictHunk,
2019-11-16 03:41:04 +02:00
},
{
ViewName: "mergeConflicts",
Key: opts.GetKey(opts.Config.Universal.Edit),
Handler: self.handleMergeConflictEditFileAtLine,
Description: self.c.Tr.LcEditFile,
},
{
ViewName: "mergeConflicts",
Key: opts.GetKey(opts.Config.Universal.OpenFile),
Handler: self.handleMergeConflictOpenFileAtLine,
Description: self.c.Tr.LcOpenFile,
},
2019-11-16 03:41:04 +02:00
{
ViewName: "mergeConflicts",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Undo),
Handler: self.handleMergeConflictUndo,
Description: self.c.Tr.LcUndo,
2019-11-17 09:15:32 +02:00
},
2019-11-16 05:00:27 +02:00
{
ViewName: "status",
Key: gocui.MouseLeft,
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleStatusClick,
2019-11-16 05:00:27 +02:00
},
{
ViewName: "search",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Confirm),
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleSearch,
},
{
ViewName: "search",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.Return),
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleSearchEscape,
},
2020-03-26 12:39:59 +02:00
{
ViewName: "confirmation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevItem),
2020-03-26 12:39:59 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpConfirmationPanel,
2020-03-26 12:39:59 +02:00
},
{
ViewName: "confirmation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextItem),
2020-03-26 12:39:59 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownConfirmationPanel,
2020-03-26 12:39:59 +02:00
},
{
ViewName: "confirmation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevItemAlt),
2020-03-26 12:39:59 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpConfirmationPanel,
2020-03-26 12:39:59 +02:00
},
{
ViewName: "confirmation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextItemAlt),
2020-03-26 12:39:59 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownConfirmationPanel,
2020-03-26 12:39:59 +02:00
},
2020-09-30 00:27:23 +02:00
{
ViewName: "submodules",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.CopyToClipboard),
Handler: self.handleCopySelectedSideContextItemToClipboard,
Description: self.c.Tr.LcCopySubmoduleNameToClipboard,
2020-09-30 00:27:23 +02:00
},
{
ViewName: "files",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.ToggleWhitespaceInDiffView),
Handler: self.toggleWhitespaceInDiffView,
Description: self.c.Tr.ToggleWhitespaceInDiffView,
},
2021-04-11 03:43:07 +02:00
{
ViewName: "extras",
Key: gocui.MouseWheelUp,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpExtra,
2021-04-11 03:43:07 +02:00
},
{
ViewName: "extras",
Key: gocui.MouseWheelDown,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownExtra,
2021-04-11 03:43:07 +02:00
},
2021-04-11 07:01:49 +02:00
{
ViewName: "extras",
Tag: "navigation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevItemAlt),
2021-04-11 07:01:49 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpExtra,
2021-04-11 07:01:49 +02:00
},
{
ViewName: "extras",
Tag: "navigation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.PrevItem),
2021-04-11 07:01:49 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollUpExtra,
2021-04-11 07:01:49 +02:00
},
{
ViewName: "extras",
Tag: "navigation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextItem),
2021-04-11 07:01:49 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownExtra,
2021-04-11 07:01:49 +02:00
},
{
ViewName: "extras",
Tag: "navigation",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.NextItemAlt),
2021-04-11 07:01:49 +02:00
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.scrollDownExtra,
2021-04-11 07:01:49 +02:00
},
{
ViewName: "extras",
Tag: "navigation",
Key: gocui.MouseLeft,
Modifier: gocui.ModNone,
2022-02-06 06:54:26 +02:00
Handler: self.handleFocusCommandLog,
2021-04-11 07:01:49 +02:00
},
2018-08-08 11:18:41 +02:00
}
2022-02-05 05:42:56 +02:00
mouseKeybindings := []*gocui.ViewMouseBinding{}
2022-02-06 06:54:26 +02:00
for _, c := range self.State.Contexts.Flatten() {
2022-02-05 05:42:56 +02:00
viewName := c.GetViewName()
2022-02-06 06:54:26 +02:00
for _, binding := range c.GetKeybindings(opts) {
2022-02-05 05:42:56 +02:00
// TODO: move all mouse keybindings into the mouse keybindings approach below
binding.ViewName = viewName
bindings = append(bindings, binding)
}
2022-01-28 11:44:36 +02:00
2022-02-27 02:42:22 +02:00
mouseKeybindings = append(mouseKeybindings, c.GetMouseKeybindings(opts)...)
2022-01-28 11:44:36 +02:00
}
for _, viewName := range []string{"status", "remotes", "tags", "localBranches", "remoteBranches", "files", "submodules", "reflogCommits", "commits", "commitFiles", "subCommits", "stash"} {
2022-01-28 11:44:36 +02:00
bindings = append(bindings, []*types.Binding{
2022-02-06 06:54:26 +02:00
{ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.PrevBlock), Modifier: gocui.ModNone, Handler: self.previousSideWindow},
{ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.NextBlock), Modifier: gocui.ModNone, Handler: self.nextSideWindow},
{ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt), Modifier: gocui.ModNone, Handler: self.previousSideWindow},
{ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.NextBlockAlt), Modifier: gocui.ModNone, Handler: self.nextSideWindow},
{ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.PrevBlockAlt2), Modifier: gocui.ModNone, Handler: self.previousSideWindow},
{ViewName: viewName, Key: opts.GetKey(opts.Config.Universal.NextBlockAlt2), Modifier: gocui.ModNone, Handler: self.nextSideWindow},
}...)
}
// Appends keybindings to jump to a particular sideView using numbers
windows := []string{"status", "files", "branches", "commits", "stash"}
if len(config.Universal.JumpToBlock) != len(windows) {
log.Fatal("Jump to block keybindings cannot be set. Exactly 5 keybindings must be supplied.")
} else {
for i, window := range windows {
2022-01-28 11:44:36 +02:00
bindings = append(bindings, &types.Binding{
ViewName: "",
2022-02-06 06:54:26 +02:00
Key: opts.GetKey(opts.Config.Universal.JumpToBlock[i]),
Modifier: gocui.ModNone,
2022-03-19 00:38:49 +02:00
Handler: self.goToSideWindow(window),
})
}
}
bindings = append(bindings, []*types.Binding{
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.NextTab),
Handler: self.handleNextTab,
Description: self.c.Tr.LcNextTab,
Tag: "navigation",
},
{
ViewName: "",
Key: opts.GetKey(opts.Config.Universal.PrevTab),
Handler: self.handlePrevTab,
Description: self.c.Tr.LcPrevTab,
Tag: "navigation",
},
}...)
2020-10-01 23:32:48 +02:00
2022-02-05 05:42:56 +02:00
return bindings, mouseKeybindings
2018-08-28 20:07:13 +02:00
}
2022-01-31 13:11:34 +02:00
func (gui *Gui) resetKeybindings() error {
gui.g.DeleteAllKeybindings()
2022-02-05 05:42:56 +02:00
bindings, mouseBindings := gui.GetInitialKeybindings()
// prepending because we want to give our custom keybindings precedence over default keybindings
customBindings, err := gui.CustomCommandsClient.GetCustomCommandKeybindings()
if err != nil {
log.Fatal(err)
}
bindings = append(customBindings, bindings...)
2018-08-28 20:07:13 +02:00
2018-08-08 11:18:41 +02:00
for _, binding := range bindings {
2022-01-29 10:09:20 +02:00
if err := gui.SetKeybinding(binding); err != nil {
2018-08-08 11:18:41 +02:00
return err
}
}
2019-11-13 14:18:31 +02:00
2022-02-05 05:42:56 +02:00
for _, binding := range mouseBindings {
if err := gui.SetMouseKeybinding(binding); err != nil {
return err
}
}
for _, values := range gui.viewTabMap() {
for _, value := range values {
viewName := value.ViewName
tabClickCallback := func(tabIndex int) error { return gui.onViewTabClick(gui.windowForView(viewName), tabIndex) }
2020-01-09 12:34:17 +02:00
if err := gui.g.SetTabClickBinding(viewName, tabClickCallback); err != nil {
return err
}
2020-01-09 12:34:17 +02:00
}
2019-11-13 14:18:31 +02:00
}
return nil
}
2022-01-29 10:09:20 +02:00
func (gui *Gui) wrappedHandler(f func() error) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
return f()
}
}
func (gui *Gui) SetKeybinding(binding *types.Binding) error {
handler := binding.Handler
2022-02-05 05:42:56 +02:00
// TODO: move all mouse-ey stuff into new mouse approach
if gocui.IsMouseKey(binding.Key) {
2022-01-29 10:09:20 +02:00
handler = func() error {
// we ignore click events on views that aren't popup panels, when a popup panel is focused
if gui.popupPanelFocused() && gui.currentViewName() != binding.ViewName {
return nil
}
return binding.Handler()
}
}
return gui.g.SetKeybinding(binding.ViewName, binding.Key, binding.Modifier, gui.wrappedHandler(handler))
2022-01-29 10:09:20 +02:00
}
2022-02-05 05:42:56 +02:00
// warning: mutates the binding
func (gui *Gui) SetMouseKeybinding(binding *gocui.ViewMouseBinding) error {
baseHandler := binding.Handler
newHandler := func(opts gocui.ViewMouseBindingOpts) error {
// we ignore click events on views that aren't popup panels, when a popup panel is focused
if gui.popupPanelFocused() && gui.currentViewName() != binding.ViewName {
return nil
}
return baseHandler(opts)
2022-01-30 11:34:59 +02:00
}
2022-02-05 05:42:56 +02:00
binding.Handler = newHandler
return gui.g.SetViewClickBinding(binding)
2022-01-29 10:09:20 +02:00
}