local core_cursor=require("core/cursor") local unicode=require("unicode") local kb=require("keyboard") local tty=require("tty") core_cursor.horizontal={} function core_cursor.touch(cursor,gx,gy) if cursor.len>0 then local win=tty.window gx,gy=gx-win.dx,gy-win.dy while true do local x,y,d=win.x,win.y,win.width local dx=((gy*d+gx)-(y*d+x)) if dx==1 then dx=unicode.wlen(unicode.sub(cursor.data,cursor.index+1,cursor.index+1))==2 and 0 or dx end if dx==0 then break end cursor:move(dx>0 and 1 or-1) if x==win.x and y==win.y then break end end end end function core_cursor.tab(cursor) local hints=cursor.hint if not hints then return end if not cursor.cache then cursor.cache= type(hints)=="table"and hints or hints(cursor.data,cursor.index+1)or {} cursor.cache.i=-1 end local cache=cursor.cache if#cache==1 and cache.i==0 then cursor.cache=hints(cache[1],cursor.index+1) if not cursor.cache then return end cursor.cache.i=-1 cache=cursor.cache end local change=kb.isShiftDown()and-1 or 1 cache.i=(cache.i+change)%math.max(#cache,1) local next=cache[cache.i+1] if next then local tail=unicode.len(cursor.data)-cursor.index cursor:move(cursor.len) cursor:update(-cursor.len) cursor:update(next,-tail) end end function core_cursor.horizontal:scroll(num,final_index) self:move(self.vindex-self.index) self.vindex=self.vindex+num self.index=self.index+num self:echo("\0277".. unicode.sub(self.data,self.index+1).. "\27[K\0278") self:move(final_index-self.index) end function core_cursor.horizontal:echo(arg,num) local w=tty.window w.nowrap=self.nowrap if arg==""then local width=w.width if w.x>=width then width=width-math.max(unicode.wlen(unicode.sub(self.data,self.index+1,self.index+1))-1,0) if w.x>width then local s1=unicode.sub(self.data,self.vindex+1,self.index) self:scroll(unicode.len(unicode.wtrunc(s1,w.x-width+1)),self.index) end end elseif arg==kb.keys.left then if self.index