From cb702fb062971852b03e37000772074a0acb2867 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Sat, 6 Nov 2021 16:09:21 +1100 Subject: [PATCH] inputtext: add access methods for internal character list This allows for InputText wrappers (namely the Japanese keyboard which needs to be able to apply modifiers to the character before the cursor) to nicely access the character list. Signed-off-by: Aleksa Sarai --- frontend/ui/widget/inputtext.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/frontend/ui/widget/inputtext.lua b/frontend/ui/widget/inputtext.lua index 71543b9c6..fdda87821 100644 --- a/frontend/ui/widget/inputtext.lua +++ b/frontend/ui/widget/inputtext.lua @@ -707,6 +707,20 @@ function InputText:searchString(str, case_sensitive, start_pos) return char_pos end +--- Return the character at the given offset. If is_absolute is truthy then the +-- offset is the absolute position, otherwise the offset is added to the current +-- cursor position (negative offsets are allowed). +function InputText:getChar(offset, is_absolute) + local idx + if is_absolute then + idx = offset + else + idx = self.charpos + offset + end + if idx < 1 or idx > #self.charlist then return end + return self.charlist[idx] +end + function InputText:addChars(chars) if not chars then -- VirtualKeyboard:addChar(key) gave us 'nil' once (?!)