mirror of
https://github.com/kepler155c/opus
synced 2025-12-20 07:08:06 +00:00
better text entry
This commit is contained in:
@@ -8,246 +8,379 @@ function Entry:init(args)
|
||||
self.pos = 0
|
||||
self.scroll = 0
|
||||
self.value = ''
|
||||
self.width = args.width
|
||||
self.limit = 1024
|
||||
self.width = args.width or 256
|
||||
self.limit = args.limit or 1024
|
||||
self.mark = { }
|
||||
self.offset = args.offset or 1
|
||||
end
|
||||
|
||||
function Entry:reset()
|
||||
self.pos = 0
|
||||
self.scroll = 0
|
||||
self.value = ''
|
||||
self.mark = { }
|
||||
end
|
||||
|
||||
local function nextWord(line, cx)
|
||||
local result = { line:find("(%w+)", cx) }
|
||||
if #result > 1 and result[2] > cx then
|
||||
return result[2] + 1
|
||||
elseif #result > 0 and result[1] == cx then
|
||||
result = { line:find("(%w+)", result[2] + 1) }
|
||||
if #result > 0 then
|
||||
return result[1]
|
||||
end
|
||||
end
|
||||
function Entry:nextWord()
|
||||
return select(2, self.value:find("[%s%p]?%w[%s%p]", self.pos + 1)) or #self.value
|
||||
end
|
||||
|
||||
local function prevWord(line, cx)
|
||||
local nOffset = 1
|
||||
while nOffset <= #line do
|
||||
local nNext = line:find("%W%w", nOffset)
|
||||
if nNext and nNext < cx then
|
||||
nOffset = nNext + 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
return nOffset - 1 < cx and nOffset - 1
|
||||
function Entry:prevWord()
|
||||
local x = #self.value - (self.pos - 1)
|
||||
local _, n = self.value:reverse():find("[%s%p]?%w[%s%p]", x)
|
||||
return n and #self.value - n + 1 or 0
|
||||
end
|
||||
|
||||
function Entry:updateScroll()
|
||||
local ps = self.scroll
|
||||
if self.pos > #self.value then
|
||||
self.pos = #self.value
|
||||
self.scroll = 0 -- ??
|
||||
end
|
||||
if self.pos - self.scroll > self.width then
|
||||
self.scroll = self.pos - (self.width)
|
||||
self.scroll = self.pos - self.width
|
||||
elseif self.pos < self.scroll then
|
||||
self.scroll = self.pos
|
||||
end
|
||||
if ps ~= self.scroll then
|
||||
self.textChanged = true
|
||||
end
|
||||
end
|
||||
|
||||
local function moveLeft(entry)
|
||||
if entry.pos > 0 then
|
||||
entry.pos = math.max(entry.pos - 1, 0)
|
||||
function Entry:copyText(cx, ex)
|
||||
return self.value:sub(cx + 1, ex)
|
||||
end
|
||||
|
||||
function Entry:insertText(x, text)
|
||||
if #self.value + #text > self.limit then
|
||||
text = text:sub(1, self.limit-#self.value)
|
||||
end
|
||||
self.value = self.value:sub(1, x) .. text .. self.value:sub(x + 1)
|
||||
self.pos = self.pos + #text
|
||||
end
|
||||
|
||||
function Entry:deleteText(sx, ex)
|
||||
local front = self.value:sub(1, sx)
|
||||
local back = self.value:sub(ex + 1, #self.value)
|
||||
self.value = front .. back
|
||||
self.pos = sx
|
||||
end
|
||||
|
||||
function Entry:moveLeft()
|
||||
if self.pos > 0 then
|
||||
self.pos = self.pos - 1
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function moveRight(entry)
|
||||
local input = tostring(entry.value)
|
||||
if entry.pos < #input then
|
||||
entry.pos = math.min(entry.pos + 1, #input)
|
||||
function Entry:moveRight()
|
||||
if self.pos < #self.value then
|
||||
self.pos = self.pos + 1
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function moveStart(entry)
|
||||
if entry.pos ~= 0 then
|
||||
entry.pos = 0
|
||||
function Entry:moveHome()
|
||||
if self.pos ~= 0 then
|
||||
self.pos = 0
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function moveEnd(entry)
|
||||
if entry.pos ~= #tostring(entry.value) then
|
||||
entry.pos = #tostring(entry.value)
|
||||
function Entry:moveEnd()
|
||||
if self.pos ~= #self.value then
|
||||
self.pos = #self.value
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function backspace(entry)
|
||||
if entry.pos > 0 then
|
||||
local input = tostring(entry.value)
|
||||
entry.value = input:sub(1, entry.pos - 1) .. input:sub(entry.pos + 1)
|
||||
entry.pos = entry.pos - 1
|
||||
function Entry:moveTo(ie)
|
||||
self.pos = math.max(0, math.min(ie.x + self.scroll - self.offset, #self.value))
|
||||
end
|
||||
|
||||
function Entry:backspace()
|
||||
if self.mark.active then
|
||||
self:delete()
|
||||
elseif self:moveLeft() then
|
||||
self:delete()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:moveWordRight()
|
||||
if self.pos < #self.value then
|
||||
self.pos = self:nextWord(self.value, self.pos + 1)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function moveWordRight(entry)
|
||||
local nx = nextWord(entry.value, entry.pos + 1)
|
||||
if nx then
|
||||
entry.pos = math.min(nx - 1, #entry.value)
|
||||
elseif entry.pos < #entry.value then
|
||||
entry.pos = #entry.value
|
||||
function Entry:moveWordLeft()
|
||||
if self.pos > 0 then
|
||||
self.pos = self:prevWord(self.value, self.pos - 1) or 0
|
||||
return true
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function moveWordLeft(entry)
|
||||
if entry.pos ~= 0 then
|
||||
local lx = 1
|
||||
while true do
|
||||
local nx = nextWord(entry.value, lx)
|
||||
if not nx or nx >= entry.pos then
|
||||
break
|
||||
end
|
||||
lx = nx
|
||||
end
|
||||
if not lx then
|
||||
entry.pos = 0
|
||||
function Entry:delete()
|
||||
if self.mark.active then
|
||||
self:deleteText(self.mark.x, self.mark.ex)
|
||||
elseif self.pos < #self.value then
|
||||
self:deleteText(self.pos, self.pos + 1)
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:cutFromStart()
|
||||
if self.pos > 0 then
|
||||
local text = self:copyText(1, self.pos)
|
||||
self:deleteText(1, self.pos)
|
||||
os.queueEvent('clipboard_copy', text)
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:cutToEnd()
|
||||
if self.pos < #self.value then
|
||||
local text = self:copyText(self.pos, #self.value)
|
||||
self:deleteText(self.pos, #self.value)
|
||||
os.queueEvent('clipboard_copy', text)
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:cutNextWord()
|
||||
if self.pos < #self.value then
|
||||
local ex = self:nextWord(self.value, self.pos)
|
||||
local text = self:copyText(self.pos, ex)
|
||||
self:deleteText(self.pos, ex)
|
||||
os.queueEvent('clipboard_copy', text)
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:cutPrevWord()
|
||||
if self.pos > 0 then
|
||||
local sx = self:prevWord(self.value, self.pos)
|
||||
local text = self:copyText(sx, self.pos)
|
||||
self:deleteText(sx, self.pos)
|
||||
os.queueEvent('clipboard_copy', text)
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:insertChar(ie)
|
||||
if self.mark.active then
|
||||
self:delete()
|
||||
end
|
||||
self:insertText(self.pos, ie.ch)
|
||||
end
|
||||
|
||||
function Entry:copy()
|
||||
if #self.value > 0 then
|
||||
self.mark.continue = true
|
||||
if self.mark.active then
|
||||
self:copyMarked()
|
||||
else
|
||||
entry.pos = lx - 1
|
||||
end
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function delete(entry)
|
||||
local input = tostring(entry.value)
|
||||
if entry.pos < #input then
|
||||
entry.value = input:sub(1, entry.pos) .. input:sub(entry.pos + 2)
|
||||
entry.update = true
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
-- credit for cut functions to: https://github.com/SquidDev-CC/mbs/blob/master/lib/readline.lua
|
||||
local function cutFromStart(entry)
|
||||
if entry.pos > 0 then
|
||||
local input = tostring(entry.value)
|
||||
os.queueEvent('clipboard_copy', input:sub(1, entry.pos))
|
||||
entry.value = input:sub(entry.pos + 1)
|
||||
entry.pos = 0
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function cutToEnd(entry)
|
||||
local input = tostring(entry.value)
|
||||
if entry.pos < #input then
|
||||
os.queueEvent('clipboard_copy', input:sub(entry.pos + 1))
|
||||
entry.value = input:sub(1, entry.pos)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
local function cutNextWord(entry)
|
||||
local input = tostring(entry.value)
|
||||
if entry.pos < #input then
|
||||
local ex = nextWord(entry.value, entry.pos)
|
||||
if ex then
|
||||
os.queueEvent('clipboard_copy', input:sub(entry.pos + 1, ex))
|
||||
entry.value = input:sub(1, entry.pos) .. input:sub(ex + 1)
|
||||
return true
|
||||
os.queueEvent('clipboard_copy', self.value)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function cutPrevWord(entry)
|
||||
if entry.pos > 0 then
|
||||
local sx = prevWord(entry.value, entry.pos)
|
||||
if sx then
|
||||
local input = tostring(entry.value)
|
||||
os.queueEvent('clipboard_copy', input:sub(sx + 1, entry.pos))
|
||||
entry.value = input:sub(1, sx) .. input:sub(entry.pos + 1)
|
||||
entry.pos = sx
|
||||
return true
|
||||
function Entry:cut()
|
||||
if self.mark.active then
|
||||
self:copyMarked()
|
||||
self:delete()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:copyMarked()
|
||||
local text = self:copyText(self.mark.x, self.mark.ex)
|
||||
os.queueEvent('clipboard_copy', text)
|
||||
end
|
||||
|
||||
function Entry:paste(ie)
|
||||
if #ie.text > 0 then
|
||||
if self.mark.active then
|
||||
self:delete()
|
||||
end
|
||||
self:insertText(self.pos, ie.text)
|
||||
end
|
||||
end
|
||||
|
||||
local function insertChar(entry, ie)
|
||||
local input = tostring(entry.value)
|
||||
if #input < entry.limit then
|
||||
entry.value = input:sub(1, entry.pos) .. ie.ch .. input:sub(entry.pos + 1)
|
||||
entry.pos = entry.pos + 1
|
||||
entry.update = true
|
||||
return true
|
||||
function Entry:clearLine()
|
||||
if #self.value > 0 then
|
||||
self:reset()
|
||||
end
|
||||
end
|
||||
|
||||
local function copy(entry)
|
||||
os.queueEvent('clipboard_copy', entry.value)
|
||||
end
|
||||
|
||||
local function paste(entry, ie)
|
||||
local input = tostring(entry.value)
|
||||
if #input + #ie.text > entry.limit then
|
||||
ie.text = ie.text:sub(1, entry.limit-#input)
|
||||
function Entry:markBegin()
|
||||
if not self.mark.active then
|
||||
self.mark.active = true
|
||||
self.mark.anchor = { x = self.pos }
|
||||
end
|
||||
entry.value = input:sub(1, entry.pos) .. ie.text .. input:sub(entry.pos + 1)
|
||||
entry.pos = entry.pos + #ie.text
|
||||
return true
|
||||
end
|
||||
|
||||
local function moveCursor(entry, ie)
|
||||
-- need starting x passed in instead of hardcoding 3
|
||||
entry.pos = math.max(0, math.min(ie.x - 3 + entry.scroll, #entry.value))
|
||||
return true
|
||||
end
|
||||
|
||||
local function clearLine(entry)
|
||||
local input = tostring(entry.value)
|
||||
if #input > 0 then
|
||||
entry:reset()
|
||||
return true
|
||||
function Entry:markFinish()
|
||||
if self.pos == self.mark.anchor.x then
|
||||
self.mark.active = false
|
||||
else
|
||||
self.mark.x = math.min(self.mark.anchor.x, self.pos)
|
||||
self.mark.ex = math.max(self.mark.anchor.x, self.pos)
|
||||
end
|
||||
self.textChanged = true
|
||||
self.mark.continue = self.mark.active
|
||||
end
|
||||
|
||||
function Entry:unmark()
|
||||
if self.mark.active then
|
||||
self.textChanged = true
|
||||
self.mark.active = false
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markAnchor(ie)
|
||||
self:unmark()
|
||||
self:moveTo(ie)
|
||||
self:markBegin()
|
||||
self:markFinish()
|
||||
end
|
||||
|
||||
function Entry:markLeft()
|
||||
self:markBegin()
|
||||
if self:moveLeft() then
|
||||
self:markFinish()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markRight()
|
||||
self:markBegin()
|
||||
if self:moveRight() then
|
||||
self:markFinish()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markWord(ie)
|
||||
local index = 1
|
||||
self:moveTo(ie)
|
||||
while true do
|
||||
local s, e = self.value:find('%w+', index)
|
||||
if not s or s - 1 > self.pos then
|
||||
break
|
||||
end
|
||||
if self.pos >= s - 1 and self.pos < e then
|
||||
self.pos = s - 1
|
||||
self:markBegin()
|
||||
self.pos = e
|
||||
self:markFinish()
|
||||
self:moveTo(ie)
|
||||
break
|
||||
end
|
||||
index = e + 1
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markNextWord()
|
||||
self:markBegin()
|
||||
if self:moveWordRight() then
|
||||
self:markFinish()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markPrevWord()
|
||||
self:markBegin()
|
||||
if self:moveWordLeft() then
|
||||
self:markFinish()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markAll()
|
||||
if #self.value > 0 then
|
||||
self.mark.anchor = { x = 1 }
|
||||
self.mark.active = true
|
||||
self.mark.continue = true
|
||||
self.mark.x = 0
|
||||
self.mark.ex = #self.value
|
||||
self.textChanged = true
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markHome()
|
||||
self:markBegin()
|
||||
if self:moveHome() then
|
||||
self:markFinish()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markEnd()
|
||||
self:markBegin()
|
||||
if self:moveEnd() then
|
||||
self:markFinish()
|
||||
end
|
||||
end
|
||||
|
||||
function Entry:markTo(ie)
|
||||
self:markBegin()
|
||||
self:moveTo(ie)
|
||||
self:markFinish()
|
||||
end
|
||||
|
||||
local mappings = {
|
||||
[ 'left' ] = moveLeft,
|
||||
[ 'control-b' ] = moveLeft,
|
||||
[ 'right' ] = moveRight,
|
||||
[ 'control-f' ] = moveRight,
|
||||
[ 'home' ] = moveStart,
|
||||
[ 'control-a' ] = moveStart,
|
||||
[ 'end' ] = moveEnd,
|
||||
[ 'control-e' ] = moveEnd,
|
||||
[ 'backspace' ] = backspace,
|
||||
[ 'control-right' ] = moveWordRight,
|
||||
[ 'alt-f' ] = moveWordRight,
|
||||
[ 'control-left' ] = moveWordLeft,
|
||||
[ 'alt-b' ] = moveWordLeft,
|
||||
[ 'delete' ] = delete,
|
||||
[ 'control-u' ] = cutFromStart,
|
||||
[ 'control-k' ] = cutToEnd,
|
||||
[ 'control-d' ] = cutNextWord,
|
||||
[ 'control-w' ] = cutPrevWord,
|
||||
[ 'char' ] = insertChar,
|
||||
[ 'copy' ] = copy,
|
||||
[ 'paste' ] = paste,
|
||||
[ 'control-y' ] = paste,
|
||||
[ 'mouse_click' ] = moveCursor,
|
||||
[ 'mouse_rightclick' ] = clearLine,
|
||||
[ 'left' ] = Entry.moveLeft,
|
||||
[ 'control-b' ] = Entry.moveLeft,
|
||||
[ 'right' ] = Entry.moveRight,
|
||||
[ 'control-f' ] = Entry.moveRight,
|
||||
[ 'home' ] = Entry.moveHome,
|
||||
[ 'end' ] = Entry.moveEnd,
|
||||
[ 'control-e' ] = Entry.moveEnd,
|
||||
[ 'mouse_click' ] = Entry.moveTo,
|
||||
[ 'backspace' ] = Entry.backspace,
|
||||
[ 'control-right' ] = Entry.moveWordRight,
|
||||
[ 'alt-f' ] = Entry.moveWordRight,
|
||||
[ 'control-left' ] = Entry.moveWordLeft,
|
||||
[ 'alt-b' ] = Entry.moveWordLeft,
|
||||
[ 'mouse_doubleclick' ] = Entry.markWord,
|
||||
[ 'delete' ] = Entry.delete,
|
||||
[ 'control-u' ] = Entry.cutFromStart,
|
||||
[ 'control-k' ] = Entry.cutToEnd,
|
||||
--[ 'control-d' ] = Entry.cutNextWord,
|
||||
[ 'control-w' ] = Entry.cutPrevWord,
|
||||
[ 'char' ] = Entry.insertChar,
|
||||
[ 'control-c' ] = Entry.copy,
|
||||
[ 'control-x' ] = Entry.cut,
|
||||
[ 'paste' ] = Entry.paste,
|
||||
[ 'control-y' ] = Entry.paste,
|
||||
[ 'mouse_rightclick' ] = Entry.clearLine,
|
||||
|
||||
[ 'shift-left' ] = Entry.markLeft,
|
||||
[ 'shift-right' ] = Entry.markRight,
|
||||
[ 'mouse_down' ] = Entry.markAnchor,
|
||||
[ 'mouse_drag' ] = Entry.markTo,
|
||||
[ 'shift-mouse_click' ] = Entry.markTo,
|
||||
[ 'control-a' ] = Entry.markAll,
|
||||
[ 'control-shift-right' ] = Entry.markNextWord,
|
||||
[ 'control-shift-left' ] = Entry.markPrevWord,
|
||||
[ 'shift-end' ] = Entry.markEnd,
|
||||
[ 'shift-home' ] = Entry.markHome,
|
||||
}
|
||||
|
||||
function Entry:process(ie)
|
||||
local action = mappings[ie.code]
|
||||
local updated
|
||||
|
||||
self.textChanged = false
|
||||
|
||||
if action then
|
||||
updated = action(self, ie)
|
||||
local pos = self.pos
|
||||
local line = self.value
|
||||
|
||||
local wasMarking = self.mark.continue
|
||||
self.mark.continue = false
|
||||
|
||||
action(self, ie)
|
||||
|
||||
self.textChanged = self.textChanged or self.value ~= line
|
||||
self.posChanged = pos ~= self.pos
|
||||
self:updateScroll()
|
||||
|
||||
if not self.mark.continue and wasMarking then
|
||||
self:unmark()
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
self:updateScroll()
|
||||
|
||||
return updated
|
||||
end
|
||||
|
||||
return Entry
|
||||
|
||||
Reference in New Issue
Block a user