feat: add hilbish.vimAction hook (closes #108)

windows-fixes
TorchedSammy 2022-03-13 16:44:11 -04:00
parent 7d9c7ddf18
commit 726d265cca
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 20 additions and 0 deletions

View File

@ -180,6 +180,7 @@ type Instance struct {
mutex sync.Mutex mutex sync.Mutex
ViModeCallback func(ViMode) ViModeCallback func(ViMode)
ViActionCallback func(ViAction, []string)
} }
// NewInstance is used to create a readline instance and initialise it with sane defaults. // NewInstance is used to create a readline instance and initialise it with sane defaults.

View File

@ -33,6 +33,12 @@ var (
VimKeysStr = "[N]" VimKeysStr = "[N]"
) )
type ViAction int
const (
VimActionYank = iota
VimActionPaste
)
var ( var (
// registerFreeKeys - Some Vim keys don't act on/ aren't affected by registers, // registerFreeKeys - Some Vim keys don't act on/ aren't affected by registers,
// and using these keys will automatically cancel any active register. // 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 // 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. // will compute the new cursor pos accordingly.
func (rl *Instance) vi(r rune) { func (rl *Instance) vi(r rune) {
activeRegister := string(rl.registers.currentRegister)
// Check if we are in register mode. If yes, and for some characters, // Check if we are in register mode. If yes, and for some characters,
// we select the register and exit this func immediately. // we select the register and exit this func immediately.
@ -198,6 +205,7 @@ func (rl *Instance) vi(r rune) {
buffer := rl.pasteFromRegister() buffer := rl.pasteFromRegister()
vii := rl.getViIterations() vii := rl.getViIterations()
rl.ViActionCallback(VimActionPaste, []string{activeRegister, string(buffer)})
for i := 1; i <= vii; i++ { for i := 1; i <= vii; i++ {
rl.insert(buffer) rl.insert(buffer)
} }
@ -208,6 +216,7 @@ func (rl *Instance) vi(r rune) {
rl.viUndoSkipAppend = true rl.viUndoSkipAppend = true
buffer := rl.pasteFromRegister() buffer := rl.pasteFromRegister()
vii := rl.getViIterations() vii := rl.getViIterations()
rl.ViActionCallback(VimActionPaste, []string{activeRegister, string(buffer)})
for i := 1; i <= vii; i++ { for i := 1; i <= vii; i++ {
rl.insert(buffer) rl.insert(buffer)
} }
@ -311,6 +320,7 @@ func (rl *Instance) vi(r rune) {
case 'y': case 'y':
if rl.viIsYanking { if rl.viIsYanking {
rl.ViActionCallback(VimActionYank, []string{activeRegister, string(rl.line)})
rl.saveBufToRegister(rl.line) rl.saveBufToRegister(rl.line)
rl.viIsYanking = false rl.viIsYanking = false
} }
@ -318,6 +328,7 @@ func (rl *Instance) vi(r rune) {
rl.viUndoSkipAppend = true rl.viUndoSkipAppend = true
case 'Y': case 'Y':
rl.ViActionCallback(VimActionYank, []string{activeRegister, string(rl.line)})
rl.saveBufToRegister(rl.line) rl.saveBufToRegister(rl.line)
rl.viUndoSkipAppend = true rl.viUndoSkipAppend = true

8
rl.go
View File

@ -36,6 +36,14 @@ func newLineReader(prompt string, noHist bool) *lineReader {
} }
setVimMode(modeStr) 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) { rl.TabCompleter = func(line []rune, pos int, _ readline.DelayedTabContext) (string, []*readline.CompletionGroup) {
ctx := string(line) ctx := string(line)
var completions []string var completions []string