From 9c652086e822d81dc1987d771bc6448c8359951f Mon Sep 17 00:00:00 2001 From: rxi Date: Thu, 28 May 2020 11:50:03 +0100 Subject: [PATCH] Improved behaviour of and renamed `translate.next|previous_word_boundary` --- data/core/commands/doc.lua | 4 ++-- data/core/doc/translate.lua | 39 ++++++++++++++++++------------------- data/core/keymap.lua | 16 +++++++-------- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index b7914ec..ef3bbc6 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -320,8 +320,8 @@ local commands = { local translations = { ["previous-char"] = translate.previous_char, ["next-char"] = translate.next_char, - ["previous-word-boundary"] = translate.previous_word_boundary, - ["next-word-boundary"] = translate.next_word_boundary, + ["previous-word-start"] = translate.previous_word_start, + ["next-word-end"] = translate.next_word_end, ["previous-start-of-block"] = translate.previous_start_of_block, ["next-start-of-block"] = translate.next_start_of_block, ["start-of-doc"] = translate.start_of_doc, diff --git a/data/core/doc/translate.lua b/data/core/doc/translate.lua index 477411c..7c850ee 100644 --- a/data/core/doc/translate.lua +++ b/data/core/doc/translate.lua @@ -28,33 +28,32 @@ function translate.next_char(doc, line, col) end -function translate.previous_word_boundary(doc, line, col) - local char = doc:get_char(doc:position_offset(line, col, -1)) - local inword = not is_non_word(char) - repeat - local line2, col2 = line, col - line, col = doc:position_offset(line, col, -1) - if line == line2 and col == col2 then +function translate.previous_word_start(doc, line, col) + local prev + while line > 1 or col > 1 do + local l, c = doc:position_offset(line, col, -1) + local char = doc:get_char(l, c) + if prev and prev ~= char or not is_non_word(char) then break end - local c = doc:get_char(doc:position_offset(line, col, -1)) - until inword and is_non_word(c) or not inword and c ~= char - return line, col + prev, line, col = char, l, c + end + return translate.start_of_word(doc, line, col) end -function translate.next_word_boundary(doc, line, col) - local char = doc:get_char(line, col) - local inword = not is_non_word(char) - repeat - local line2, col2 = line, col - line, col = doc:position_offset(line, col, 1) - if line == line2 and col == col2 then +function translate.next_word_end(doc, line, col) + local prev + local end_line, end_col = translate.end_of_doc(doc, line, col) + while line < end_line or col < end_col do + local char = doc:get_char(line, col) + if prev and prev ~= char or not is_non_word(char) then break end - local c = doc:get_char(line, col) - until inword and is_non_word(c) or not inword and c ~= char - return line, col + line, col = doc:position_offset(line, col, 1) + prev = char + end + return translate.end_of_word(doc, line, col) end diff --git a/data/core/keymap.lua b/data/core/keymap.lua index ab5fa38..e45c106 100644 --- a/data/core/keymap.lua +++ b/data/core/keymap.lua @@ -132,12 +132,12 @@ keymap.add { ["shift+tab"] = "doc:unindent", ["backspace"] = "doc:backspace", ["shift+backspace"] = "doc:backspace", - ["ctrl+backspace"] = "doc:delete-to-previous-word-boundary", - ["ctrl+shift+backspace"] = "doc:delete-to-previous-word-boundary", + ["ctrl+backspace"] = "doc:delete-to-previous-word-start", + ["ctrl+shift+backspace"] = "doc:delete-to-previous-word-start", ["delete"] = "doc:delete", ["shift+delete"] = "doc:delete", - ["ctrl+delete"] = "doc:delete-to-next-word-boundary", - ["ctrl+shift+delete"] = "doc:delete-to-next-word-boundary", + ["ctrl+delete"] = "doc:delete-to-next-word-end", + ["ctrl+shift+delete"] = "doc:delete-to-next-word-end", ["return"] = { "command:submit", "doc:newline" }, ["ctrl+return"] = "doc:newline-below", ["ctrl+shift+return"] = "doc:newline-above", @@ -155,8 +155,8 @@ keymap.add { ["right"] = "doc:move-to-next-char", ["up"] = { "command:select-previous", "doc:move-to-previous-line" }, ["down"] = { "command:select-next", "doc:move-to-next-line" }, - ["ctrl+left"] = "doc:move-to-previous-word-boundary", - ["ctrl+right"] = "doc:move-to-next-word-boundary", + ["ctrl+left"] = "doc:move-to-previous-word-start", + ["ctrl+right"] = "doc:move-to-next-word-end", ["ctrl+["] = "doc:move-to-previous-start-of-block", ["ctrl+]"] = "doc:move-to-next-start-of-block", ["home"] = "doc:move-to-start-of-line", @@ -170,8 +170,8 @@ keymap.add { ["shift+right"] = "doc:select-to-next-char", ["shift+up"] = "doc:select-to-previous-line", ["shift+down"] = "doc:select-to-next-line", - ["ctrl+shift+left"] = "doc:select-to-previous-word-boundary", - ["ctrl+shift+right"] = "doc:select-to-next-word-boundary", + ["ctrl+shift+left"] = "doc:select-to-previous-word-start", + ["ctrl+shift+right"] = "doc:select-to-next-word-end", ["ctrl+shift+["] = "doc:select-to-previous-start-of-block", ["ctrl+shift+]"] = "doc:select-to-next-start-of-block", ["shift+home"] = "doc:select-to-start-of-line",