mirror of https://github.com/Hilbis/Hilbish
feat: add hilbish.vimAction hook (closes #108)
parent
7d9c7ddf18
commit
726d265cca
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
8
rl.go
8
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
|
||||
|
|
Loading…
Reference in New Issue