Skip to content

Commit

Permalink
Fix keybinds for multiline input
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 12, 2023
1 parent dce9de4 commit 0465814
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
33 changes: 31 additions & 2 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prompt

import (
"strings"
"unicode"
"unicode/utf8"

"github.com/elk-language/go-prompt/internal/bisect"
Expand Down Expand Up @@ -479,8 +480,36 @@ func (d *Document) OnLastLine() bool {
}

// GetEndOfLinePosition returns relative position for the end of this line.
func (d *Document) GetEndOfLinePosition() istrings.RuneIndex {
return istrings.RuneIndex(len([]rune(d.CurrentLineAfterCursor())))
func (d *Document) GetEndOfLinePosition() istrings.RuneCount {
return istrings.RuneLen(d.CurrentLineAfterCursor())
}

// GetStartOfLinePosition returns relative position for the start of this line.
func (d *Document) GetStartOfLinePosition() istrings.RuneCount {
return istrings.RuneLen(d.CurrentLineBeforeCursor())
}

// GetStartOfLinePosition returns relative position for the start of this line.
func (d *Document) FindStartOfFirstWordOfLine() istrings.RuneCount {
line := d.CurrentLineBeforeCursor()
var counter istrings.RuneCount
var nonSpaceCharSeen bool
for _, char := range line {
if !nonSpaceCharSeen && unicode.IsSpace(char) {
continue
}

if !nonSpaceCharSeen {
nonSpaceCharSeen = true
}
counter++
}

if counter == 0 {
return istrings.RuneLen(line)
}

return counter
}

func (d *Document) leadingWhitespaceInCurrentLine() (margin string) {
Expand Down
12 changes: 4 additions & 8 deletions emacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,28 @@ var emacsKeyBindings = []KeyBind{
{
Key: ControlE,
Fn: func(buf *Buffer) {
x := []rune(buf.Document().TextAfterCursor())
buf.CursorRight(istrings.RuneCount(len(x)))
buf.CursorRight(istrings.RuneLen(buf.Document().CurrentLineAfterCursor()))
},
},
// Go to the beginning of the line
{
Key: ControlA,
Fn: func(buf *Buffer) {
x := []rune(buf.Document().TextBeforeCursor())
buf.CursorLeft(istrings.RuneCount(len(x)))
buf.CursorLeft(buf.Document().FindStartOfFirstWordOfLine())
},
},
// Cut the Line after the cursor
{
Key: ControlK,
Fn: func(buf *Buffer) {
x := []rune(buf.Document().TextAfterCursor())
buf.Delete(istrings.RuneCount(len(x)))
buf.Delete(istrings.RuneLen(buf.Document().CurrentLineAfterCursor()))
},
},
// Cut/delete the Line before the cursor
{
Key: ControlU,
Fn: func(buf *Buffer) {
x := []rune(buf.Document().TextBeforeCursor())
buf.DeleteBeforeCursor(istrings.RuneCount(len(x)))
buf.DeleteBeforeCursor(istrings.RuneLen(buf.Document().CurrentLineBeforeCursor()))
},
},
// Delete character under the cursor
Expand Down

0 comments on commit 0465814

Please sign in to comment.