summaryrefslogtreecommitdiff
path: root/runtime/lua/core/motion.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/core/motion.lua')
-rw-r--r--runtime/lua/core/motion.lua66
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