Improved behaviour of and renamed `translate.next|previous_word_boundary`

This commit is contained in:
rxi 2020-05-28 11:50:03 +01:00
parent 74755f5b4a
commit 9c652086e8
3 changed files with 29 additions and 30 deletions

View File

@ -320,8 +320,8 @@ local commands = {
local translations = { local translations = {
["previous-char"] = translate.previous_char, ["previous-char"] = translate.previous_char,
["next-char"] = translate.next_char, ["next-char"] = translate.next_char,
["previous-word-boundary"] = translate.previous_word_boundary, ["previous-word-start"] = translate.previous_word_start,
["next-word-boundary"] = translate.next_word_boundary, ["next-word-end"] = translate.next_word_end,
["previous-start-of-block"] = translate.previous_start_of_block, ["previous-start-of-block"] = translate.previous_start_of_block,
["next-start-of-block"] = translate.next_start_of_block, ["next-start-of-block"] = translate.next_start_of_block,
["start-of-doc"] = translate.start_of_doc, ["start-of-doc"] = translate.start_of_doc,

View File

@ -28,33 +28,32 @@ function translate.next_char(doc, line, col)
end end
function translate.previous_word_boundary(doc, line, col) function translate.previous_word_start(doc, line, col)
local char = doc:get_char(doc:position_offset(line, col, -1)) local prev
local inword = not is_non_word(char) while line > 1 or col > 1 do
repeat local l, c = doc:position_offset(line, col, -1)
local line2, col2 = line, col local char = doc:get_char(l, c)
line, col = doc:position_offset(line, col, -1) if prev and prev ~= char or not is_non_word(char) then
if line == line2 and col == col2 then
break break
end end
local c = doc:get_char(doc:position_offset(line, col, -1)) prev, line, col = char, l, c
until inword and is_non_word(c) or not inword and c ~= char end
return line, col return translate.start_of_word(doc, line, col)
end end
function translate.next_word_boundary(doc, line, col) 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) local char = doc:get_char(line, col)
local inword = not is_non_word(char) if prev and prev ~= char or not is_non_word(char) then
repeat
local line2, col2 = line, col
line, col = doc:position_offset(line, col, 1)
if line == line2 and col == col2 then
break break
end end
local c = doc:get_char(line, col) line, col = doc:position_offset(line, col, 1)
until inword and is_non_word(c) or not inword and c ~= char prev = char
return line, col end
return translate.end_of_word(doc, line, col)
end end

View File

@ -132,12 +132,12 @@ keymap.add {
["shift+tab"] = "doc:unindent", ["shift+tab"] = "doc:unindent",
["backspace"] = "doc:backspace", ["backspace"] = "doc:backspace",
["shift+backspace"] = "doc:backspace", ["shift+backspace"] = "doc:backspace",
["ctrl+backspace"] = "doc:delete-to-previous-word-boundary", ["ctrl+backspace"] = "doc:delete-to-previous-word-start",
["ctrl+shift+backspace"] = "doc:delete-to-previous-word-boundary", ["ctrl+shift+backspace"] = "doc:delete-to-previous-word-start",
["delete"] = "doc:delete", ["delete"] = "doc:delete",
["shift+delete"] = "doc:delete", ["shift+delete"] = "doc:delete",
["ctrl+delete"] = "doc:delete-to-next-word-boundary", ["ctrl+delete"] = "doc:delete-to-next-word-end",
["ctrl+shift+delete"] = "doc:delete-to-next-word-boundary", ["ctrl+shift+delete"] = "doc:delete-to-next-word-end",
["return"] = { "command:submit", "doc:newline" }, ["return"] = { "command:submit", "doc:newline" },
["ctrl+return"] = "doc:newline-below", ["ctrl+return"] = "doc:newline-below",
["ctrl+shift+return"] = "doc:newline-above", ["ctrl+shift+return"] = "doc:newline-above",
@ -155,8 +155,8 @@ keymap.add {
["right"] = "doc:move-to-next-char", ["right"] = "doc:move-to-next-char",
["up"] = { "command:select-previous", "doc:move-to-previous-line" }, ["up"] = { "command:select-previous", "doc:move-to-previous-line" },
["down"] = { "command:select-next", "doc:move-to-next-line" }, ["down"] = { "command:select-next", "doc:move-to-next-line" },
["ctrl+left"] = "doc:move-to-previous-word-boundary", ["ctrl+left"] = "doc:move-to-previous-word-start",
["ctrl+right"] = "doc:move-to-next-word-boundary", ["ctrl+right"] = "doc:move-to-next-word-end",
["ctrl+["] = "doc:move-to-previous-start-of-block", ["ctrl+["] = "doc:move-to-previous-start-of-block",
["ctrl+]"] = "doc:move-to-next-start-of-block", ["ctrl+]"] = "doc:move-to-next-start-of-block",
["home"] = "doc:move-to-start-of-line", ["home"] = "doc:move-to-start-of-line",
@ -170,8 +170,8 @@ keymap.add {
["shift+right"] = "doc:select-to-next-char", ["shift+right"] = "doc:select-to-next-char",
["shift+up"] = "doc:select-to-previous-line", ["shift+up"] = "doc:select-to-previous-line",
["shift+down"] = "doc:select-to-next-line", ["shift+down"] = "doc:select-to-next-line",
["ctrl+shift+left"] = "doc:select-to-previous-word-boundary", ["ctrl+shift+left"] = "doc:select-to-previous-word-start",
["ctrl+shift+right"] = "doc:select-to-next-word-boundary", ["ctrl+shift+right"] = "doc:select-to-next-word-end",
["ctrl+shift+["] = "doc:select-to-previous-start-of-block", ["ctrl+shift+["] = "doc:select-to-previous-start-of-block",
["ctrl+shift+]"] = "doc:select-to-next-start-of-block", ["ctrl+shift+]"] = "doc:select-to-next-start-of-block",
["shift+home"] = "doc:select-to-start-of-line", ["shift+home"] = "doc:select-to-start-of-line",