From 726d265ccaed06aff70bdd780a259a95f2b67dc5 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 13 Mar 2022 16:44:11 -0400 Subject: [PATCH] feat: add hilbish.vimAction hook (closes #108) --- readline/instance.go | 1 + readline/vim.go | 11 +++++++++++ rl.go | 8 ++++++++ 3 files changed, 20 insertions(+) diff --git a/readline/instance.go b/readline/instance.go index 69e56f5..48e4398 100644 --- a/readline/instance.go +++ b/readline/instance.go @@ -180,6 +180,7 @@ type Instance struct { mutex sync.Mutex ViModeCallback func(ViMode) + ViActionCallback func(ViAction, []string) } // NewInstance is used to create a readline instance and initialise it with sane defaults. diff --git a/readline/vim.go b/readline/vim.go index 9dc8350..f421d84 100644 --- a/readline/vim.go +++ b/readline/vim.go @@ -33,6 +33,12 @@ var ( VimKeysStr = "[N]" ) +type ViAction int +const ( + VimActionYank = iota + VimActionPaste +) + var ( // registerFreeKeys - Some Vim keys don't act on/ aren't affected by registers, // and using these keys will automatically cancel any active register. @@ -47,6 +53,7 @@ var ( // have been moved away, and only the rl.pos is adjusted: when echoing the input line, the shell // will compute the new cursor pos accordingly. func (rl *Instance) vi(r rune) { + activeRegister := string(rl.registers.currentRegister) // Check if we are in register mode. If yes, and for some characters, // we select the register and exit this func immediately. @@ -198,6 +205,7 @@ func (rl *Instance) vi(r rune) { buffer := rl.pasteFromRegister() vii := rl.getViIterations() + rl.ViActionCallback(VimActionPaste, []string{activeRegister, string(buffer)}) for i := 1; i <= vii; i++ { rl.insert(buffer) } @@ -208,6 +216,7 @@ func (rl *Instance) vi(r rune) { rl.viUndoSkipAppend = true buffer := rl.pasteFromRegister() vii := rl.getViIterations() + rl.ViActionCallback(VimActionPaste, []string{activeRegister, string(buffer)}) for i := 1; i <= vii; i++ { rl.insert(buffer) } @@ -311,6 +320,7 @@ func (rl *Instance) vi(r rune) { case 'y': if rl.viIsYanking { + rl.ViActionCallback(VimActionYank, []string{activeRegister, string(rl.line)}) rl.saveBufToRegister(rl.line) rl.viIsYanking = false } @@ -318,6 +328,7 @@ func (rl *Instance) vi(r rune) { rl.viUndoSkipAppend = true case 'Y': + rl.ViActionCallback(VimActionYank, []string{activeRegister, string(rl.line)}) rl.saveBufToRegister(rl.line) rl.viUndoSkipAppend = true diff --git a/rl.go b/rl.go index 2b6571d..edbef28 100644 --- a/rl.go +++ b/rl.go @@ -36,6 +36,14 @@ func newLineReader(prompt string, noHist bool) *lineReader { } setVimMode(modeStr) } + rl.ViActionCallback = func(action readline.ViAction, args []string) { + actionStr := "" + switch action { + case readline.VimActionPaste: actionStr = "paste" + case readline.VimActionYank: actionStr = "yank" + } + hooks.Em.Emit("hilbish.vimAction", actionStr, args) + } rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) { ctx := string(line) var completions []string