text widget: move caret a word at a time

This commit is contained in:
airstruck
2016-01-27 13:28:17 -05:00
parent 0fc431d894
commit e6c649ba49

View File

@@ -78,8 +78,8 @@ local function findIndexFromPoint (self, x, y)
return #self.value return #self.value
end end
-- move the caret one character to the left -- move the caret or end of selection range one character to the left
local function moveCaretLeft (self, alterRange) local function moveCharLeft (self, alterRange)
trimRange(self) trimRange(self)
local text, endIndex = self.value, self.endIndex local text, endIndex = self.value, self.endIndex
@@ -91,14 +91,22 @@ local function moveCaretLeft (self, alterRange)
selectRange(self, not alterRange and index, index) selectRange(self, not alterRange and index, index)
end end
-- move the caret to the beginning of the line -- move caret or end of selection range one word to the left
local function jumpCaretLeft (self, alterRange) local function moveWordLeft (self, alterRange)
trimRange(self)
local text = self.value:sub(1, self.endIndex)
local pos = text:find('%s[^%s]+%s*$') or 0
selectRange(self, not alterRange and pos, pos)
end
-- move the caret or end of selection range to the beginning of the line
local function moveLineLeft (self, alterRange)
trimRange(self) trimRange(self)
selectRange(self, not alterRange and 0, 0) selectRange(self, not alterRange and 0, 0)
end end
-- move the caret one character to the right -- move caret or end of selection range one character to the right
local function moveCaretRight (self, alterRange) local function moveCharRight (self, alterRange)
trimRange(self) trimRange(self)
local text, endIndex = self.value, self.endIndex local text, endIndex = self.value, self.endIndex
@@ -110,8 +118,17 @@ local function moveCaretRight (self, alterRange)
selectRange(self, not alterRange and index, index) selectRange(self, not alterRange and index, index)
end end
-- move the caret to the end of the line -- move caret or end of selection range one word to the right
local function jumpCaretRight (self, alterRange) local function moveWordRight (self, alterRange)
trimRange(self)
local text = self.value
local _, pos = text:find('^%s*[^%s]+', self.endIndex + 1)
pos = pos or #text + 1
selectRange(self, not alterRange and pos, pos)
end
-- move caret or end of selection range to the end of the line
local function moveLineRight (self, alterRange)
trimRange(self) trimRange(self)
local text = self.value local text = self.value
selectRange(self, not alterRange and #text, #text) selectRange(self, not alterRange and #text, #text)
@@ -298,27 +315,31 @@ This color is used to indicate the selected range of text.
elseif event.key == 'left' then elseif event.key == 'left' then
if Backend.isKeyDown('lgui', 'rgui') then if Backend.isKeyDown('lctrl', 'rctrl') then
jumpCaretLeft(self, isShiftPressed()) moveWordLeft(self, isShiftPressed())
elseif Backend.isKeyDown('lgui', 'rgui') then
moveLineLeft(self, isShiftPressed())
else else
moveCaretLeft(self, isShiftPressed()) moveCharLeft(self, isShiftPressed())
end end
elseif event.key == 'right' then elseif event.key == 'right' then
if Backend.isKeyDown('lgui', 'rgui') then if Backend.isKeyDown('lctrl', 'rctrl') then
jumpCaretRight(self, isShiftPressed()) moveWordRight(self, isShiftPressed())
elseif Backend.isKeyDown('lgui', 'rgui') then
moveLineRight(self, isShiftPressed())
else else
moveCaretRight(self, isShiftPressed()) moveCharRight(self, isShiftPressed())
end end
elseif event.key == 'home' then elseif event.key == 'home' then
jumpCaretLeft(self, isShiftPressed()) moveLineLeft(self, isShiftPressed())
elseif event.key == 'end' then elseif event.key == 'end' then
jumpCaretRight(self, isShiftPressed()) moveLineRight(self, isShiftPressed())
elseif event.key == 'x' and isCommandPressed() then elseif event.key == 'x' and isCommandPressed() then