local final_screen_line_starting_pos = startpos -- track value to return
Text.populate_link_offsets(State, line_index)
assert(#line_cache.screen_line_starting_pos >= 1)
for i=1,#line_cache.screen_line_starting_pos do
local pos = line_cache.screen_line_starting_pos[i]
-- render fragment
Text.draw_highlight(State, line, State.left,y, pos, lo,hi)
select_color(f)
App.screen.print(f, State.left,y)
if not hide_cursor and line_index == State.cursor1.line then
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
local lo_px = Text.draw_highlight(State, line, State.left,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
love.graphics.print(State.search_term, State.left+lo_px,y)
Text.draw_cursor(State, State.left+Text.x(f, State.cursor1.pos-pos+1), y)
y = y + State.line_height
if y >= App.screen.height then
break
end
return y, final_screen_line_starting_pos
end
function Text.screen_line(line, line_cache, i)
local pos = line_cache.screen_line_starting_pos[i]
local offset = Text.offset(line.data, pos)
if i >= #line_cache.screen_line_starting_pos then
return line.data:sub(offset)
local endpos = line_cache.screen_line_starting_pos[i+1]-1
local end_offset = Text.offset(line.data, endpos)
return line.data:sub(offset, end_offset)
local x = 0
-- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do
local frag_width = App.width(frag)
--? print('-- frag:', frag, pos, x, frag_width, State.width)
while x + frag_width > State.width do
--? print('frag:', frag, pos, x, frag_width, State.width)
if x < 0.8 * State.width then
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.width - x)
-- everything works if bpos == 0, but is a little inefficient
pos = pos + bpos
local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
frag = string.sub(frag, boffset)
--? if bpos > 0 then
--? print('after chop:', frag)
--? end
frag_width = App.width(frag)
end
--? print('screen line:', pos)
x = 0 -- new screen line
pos = pos + utf8.len(frag)
function Text.populate_link_offsets(State, line_index)
if line_cache.link_offsets then
line_cache.link_offsets = {}
local pos = 1
local s, e = 1, 0
while s <= #line.data do
s, e = line.data:find('%[%[%S+%]%]', s)
if s == nil then break end
local word = line.data:sub(s+2, e-2) -- strip out surrounding '[[..]]'
--? print('wikiword:', s, e, word)
table.insert(line_cache.link_offsets, {s, e, word})
s = e + 1
end
end
-- Intersect the filename between byte offsets s,e with the bounds of screen line i.
-- Return the left/right pixel coordinates of of the intersection,
-- or nil if it doesn't intersect with screen line i.
function Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
local spos = line_cache.screen_line_starting_pos[i]
local soff = Text.offset(line.data, spos)
if e < soff then
return
end
local eoff
if i < #line_cache.screen_line_starting_pos then
local epos = line_cache.screen_line_starting_pos[i+1]
eoff = Text.offset(line.data, epos)
if s > eoff then
return
end
local loff = math.max(s, soff)
local hoff
if eoff then
hoff = math.min(e, eoff)
else
hoff = e
--? print(s, e, soff, eoff, loff, hoff)
return App.width(line.data:sub(1, loff-1)), App.width(line.data:sub(1, hoff))
State.line_cache[line_index].link_offsets = nil
end
function trim(s)
return s:gsub('^%s+', ''):gsub('%s+$', '')
end
function ltrim(s)
return s:gsub('^%s+', '')
end
function rtrim(s)
return s:gsub('%s+$', '')
edit.draw(State)
return State.cursor_y == nil
-- this approach is cheaper and almost works, except on the final screen
-- where file ends above bottom of screen
--? local botpos = Text.pos_at_start_of_screen_line(State, State.cursor1)
--? local botline1 = {line=State.cursor1.line, pos=botpos}
--? return Text.lt1(State.screen_bottom1, botline1)
end
end
end
end
-- try to wrap at word boundaries
return
end
local line = State.lines[line_index]
end
end
end
x = x + frag_width
table.insert(line_cache.screen_line_starting_pos, pos)
local pos = 1
end
function Text.draw_cursor(State, x, y)
-- blink every 0.5s
if math.floor(Cursor_time*2)%2 == 0 then
App.color(Cursor_color)
love.graphics.rectangle('fill', x,y, 3,State.line_height)
end
State.cursor_x = x
State.cursor_y = y+State.line_height
end
function Text.populate_screen_line_starting_pos(State, line_index)
local line = State.lines[line_index]
end
end
end
end
end
end
elseif Focus == 'edit' then
App.color(Text_color)
if State.search_term then
if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then
-- render cursor if necessary
end
if State.selection1.line then
local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
final_screen_line_starting_pos = pos
local f = Text.screen_line(line, line_cache, i)
--? print('text.draw:', f, 'at', line_index,pos, 'after', x,y)
local frag_len = utf8.len(f)
-- render any link decorations
for _,link_offsets in ipairs(line_cache.link_offsets) do
local s,e,filename = unpack(link_offsets)
local lo, hi = Text.clip_wikiword_with_screen_line(line, line_cache, i, s, e)
if lo then
button(State, 'link', {x=State.left+lo, y=y, w=hi-lo, h=State.line_height, color={1,1,1},
icon = icon.hyperlink_decoration,
onpress1 = function()
if file_exists(filename) then
source.switch_to_file(filename)
end
end,
})
end
if pos < startpos then
-- render nothing
initialize_color()
Text.populate_screen_line_starting_pos(State, line_index)
-- return y for the next line, and position of start of final screen line drawn
function Text.draw(State, line_index, y, startpos, hide_cursor)