diff options
| author | Collin Williams <96917990+bluedragon1221@users.noreply.github.com> | 2026-05-11 15:07:55 -0500 |
|---|---|---|
| committer | Collin Williams <96917990+bluedragon1221@users.noreply.github.com> | 2026-05-11 15:07:55 -0500 |
| commit | cebb280fe546190e55e071b6adbf37cd47950f34 (patch) | |
| tree | 6e7a7605f8832b1c1bd7e467884a8792a5e1fab4 /runtime/lua/core/motion.lua | |
initial commit
Diffstat (limited to 'runtime/lua/core/motion.lua')
| -rw-r--r-- | runtime/lua/core/motion.lua | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/runtime/lua/core/motion.lua b/runtime/lua/core/motion.lua new file mode 100644 index 0000000..a6bcb80 --- /dev/null +++ b/runtime/lua/core/motion.lua @@ -0,0 +1,66 @@ +local M = {} + +local function is_word_char(ch) + if ch == "" then + return false + end + local byte = string.byte(ch) + if not byte then + return false + end + + if byte >= string.byte("a") and byte <= string.byte("z") then + return true + end + if byte >= string.byte("A") and byte <= string.byte("Z") then + return true + end + if byte >= string.byte("0") and byte <= string.byte("9") then + return true + end + return ch == "_" +end + +function M.forward_word() + local len = editor.buffer_length() + local point = editor.point() + if point >= len then + return + end + + local ch = editor.char_at(point) + if is_word_char(ch) then + while point < len and is_word_char(editor.char_at(point)) do + point = point + 1 + end + else + while point < len and not is_word_char(editor.char_at(point)) do + point = point + 1 + end + while point < len and is_word_char(editor.char_at(point)) do + point = point + 1 + end + end + + editor.set_point(point) +end + +function M.backward_word() + local point = editor.point() + if point <= 0 then + return + end + + point = point - 1 + while point >= 0 and not is_word_char(editor.char_at(point)) do + point = point - 1 + end + + while point >= 0 and is_word_char(editor.char_at(point)) do + point = point - 1 + end + + editor.set_point(point + 1) +end + +return M |
