added movement "by word"
This commit is contained in:
@@ -35,6 +35,10 @@ current_line :: proc() -> ^[dynamic]u8 {
|
|||||||
return &lines[cursor.line]
|
return &lines[cursor.line]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_whitespace :: proc(c: u8) -> bool {
|
||||||
|
return c == ' ' || c == '\t' || c == '\n'
|
||||||
|
}
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
r.InitWindow(800, 600, "odit")
|
r.InitWindow(800, 600, "odit")
|
||||||
r.SetTargetFPS(60)
|
r.SetTargetFPS(60)
|
||||||
@@ -62,16 +66,62 @@ main :: proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if r.IsKeyPressed(r.KeyboardKey.RIGHT) {
|
if r.IsKeyPressed(r.KeyboardKey.RIGHT) {
|
||||||
cursor.char = min(int(cursor.char) + 1, len(current_line()^))
|
preferred_position := cursor.char + 1
|
||||||
|
if cursor.char == len(current_line()) && cursor.line != len(lines) - 1 {
|
||||||
|
cursor.char = 0
|
||||||
|
cursor.line += 1
|
||||||
|
}
|
||||||
|
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) && cursor.char + 1 < len(current_line()) {
|
||||||
|
seen_space := false
|
||||||
|
for c, c_index in current_line()[cursor.char + 1:] {
|
||||||
|
if seen_space && !is_whitespace(c) {
|
||||||
|
preferred_position = cursor.char + c_index
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if is_whitespace(c) {
|
||||||
|
seen_space = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !seen_space {
|
||||||
|
preferred_position = len(current_line())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor.char = min(preferred_position, len(current_line()))
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.IsKeyPressed(r.KeyboardKey.LEFT) {
|
if r.IsKeyPressed(r.KeyboardKey.LEFT) {
|
||||||
cursor.char -= 1
|
preferred_position := cursor.char - 1
|
||||||
|
if cursor.char == 0 && cursor.line != 0 {
|
||||||
|
cursor.line -= 1
|
||||||
|
cursor.char = len(current_line())
|
||||||
|
}
|
||||||
|
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) && 0 <= cursor.char - 1 {
|
||||||
|
seen_space := false
|
||||||
|
#reverse for c, c_index in current_line()[:cursor.char - 1] {
|
||||||
|
if is_whitespace(c) {
|
||||||
|
seen_space = true
|
||||||
|
preferred_position = c_index + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !seen_space {
|
||||||
|
preferred_position = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor.char = max(preferred_position, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.IsKeyPressed(r.KeyboardKey.UP) {
|
if r.IsKeyPressed(r.KeyboardKey.UP) {
|
||||||
cursor.line -= 1
|
cursor.line -= 1
|
||||||
|
if len(current_line()) < cursor.char {
|
||||||
|
cursor.char = len(current_line())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if r.IsKeyPressed(r.KeyboardKey.DOWN) {
|
if r.IsKeyPressed(r.KeyboardKey.DOWN) {
|
||||||
cursor.line += 1
|
cursor.line += 1
|
||||||
|
if len(current_line()) < cursor.char {
|
||||||
|
cursor.char = len(current_line())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if r.IsKeyPressed(r.KeyboardKey.ENTER) {
|
if r.IsKeyPressed(r.KeyboardKey.ENTER) {
|
||||||
new_line_cap := len(current_line()) - cursor.char
|
new_line_cap := len(current_line()) - cursor.char
|
||||||
@@ -101,6 +151,8 @@ main :: proc() {
|
|||||||
if cursor.char == 0 {
|
if cursor.char == 0 {
|
||||||
old_len := len(lines[cursor.line - 1])
|
old_len := len(lines[cursor.line - 1])
|
||||||
append(&lines[cursor.line - 1], string(current_line()[:]))
|
append(&lines[cursor.line - 1], string(current_line()[:]))
|
||||||
|
line_to_remove := current_line()^
|
||||||
|
defer delete(line_to_remove)
|
||||||
ordered_remove(&lines, cursor.line)
|
ordered_remove(&lines, cursor.line)
|
||||||
cursor.line -= 1
|
cursor.line -= 1
|
||||||
cursor.char = old_len
|
cursor.char = old_len
|
||||||
|
|||||||
Reference in New Issue
Block a user