Add basic line home/end

This commit is contained in:
Sasha Koshka 2024-09-04 12:27:20 -04:00
parent 2f828b1ae8
commit 70d6759884

View File

@ -405,12 +405,12 @@ func (this *textBox) handleKeyDown (key input.Key, numberPad bool) bool {
switch {
case key == input.KeyHome || (modifiers.Alt && key == input.KeyLeft):
dot.End = 0
dot.End = lineHome(this.runes, dot.End)
if !sel { dot.Start = dot.End }
this.Select(dot)
return true
case key == input.KeyEnd || (modifiers.Alt && key == input.KeyRight):
dot.End = len(this.text)
dot.End = lineEnd(this.runes, dot.End)
if !sel { dot.Start = dot.End }
this.Select(dot)
return true
@ -436,7 +436,7 @@ func (this *textBox) handleKeyDown (key input.Key, numberPad bool) bool {
return true
case key == input.Key('a') && modifiers.Control:
dot.Start = 0
dot.End = len(this.text)
dot.End = len(this.text) // FIXME
this.Select(dot)
return true
default:
@ -587,6 +587,8 @@ func (this *textBox) recursiveReApply () {
}
}
// TODO: these two functions really could be better.
func previousParagraph (text []rune, index int) int {
consecLF := 0
if index >= len(text) { index = len(text) - 1 }
@ -615,3 +617,28 @@ func nextParagraph (text []rune, index int) int {
}
return index
}
// TODO: make functions on top of these that support soft home/end
// FIXME: these break near the start and end of the box
func lineHome (text []rune, index int) int {
liminal := index < len(text) && text[index] == '\n'
for index := index; index >= 0; index -- {
char := text[index]
if char == '\n' && !liminal {
return index + 1
}
liminal = false
}
return 0
}
func lineEnd (text []rune, index int) int {
for ; index < len(text); index ++ {
char := text[index]
if char == '\n' {
return index
}
}
return index
}