Merge text0
This commit is contained in:
commit
83881938f5
5
main.lua
5
main.lua
|
@ -269,8 +269,3 @@ for handler_name in pairs(love.handlers) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function warning_message()
|
|
||||||
assert(type(Current_app) == 'table')
|
|
||||||
return Current_app.message
|
|
||||||
end
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ function Text.draw_search_bar(State)
|
||||||
love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
|
love.graphics.rectangle('line', 20, y-6, App.screen.width-40, h+2, 2,2)
|
||||||
App.color(Text_color)
|
App.color(Text_color)
|
||||||
App.screen.print(State.search_term, 25,y-5)
|
App.screen.print(State.search_term, 25,y-5)
|
||||||
Text.draw_cursor(State, 25+App.width(State.search_term),y-5, Cursor_color)
|
Text.draw_cursor(State, 25+State.font:getWidth(State.search_term),y-5, Cursor_color)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Text.search_next(State)
|
function Text.search_next(State)
|
||||||
|
|
|
@ -57,11 +57,11 @@ function Text.draw_highlight(State, line, x,y, pos, lo,hi)
|
||||||
lo_px = 0
|
lo_px = 0
|
||||||
else
|
else
|
||||||
local before = line.data:sub(pos_offset, lo_offset-1)
|
local before = line.data:sub(pos_offset, lo_offset-1)
|
||||||
lo_px = App.width(before)
|
lo_px = State.font:getWidth(before)
|
||||||
end
|
end
|
||||||
local s = line.data:sub(lo_offset, hi_offset-1)
|
local s = line.data:sub(lo_offset, hi_offset-1)
|
||||||
App.color(Highlight_color)
|
App.color(Highlight_color)
|
||||||
love.graphics.rectangle('fill', x+lo_px,y, App.width(s),State.line_height)
|
love.graphics.rectangle('fill', x+lo_px,y, State.font:getWidth(s),State.line_height)
|
||||||
App.color(Text_color)
|
App.color(Text_color)
|
||||||
return lo_px
|
return lo_px
|
||||||
end
|
end
|
||||||
|
|
52
text.lua
52
text.lua
|
@ -46,18 +46,18 @@ function Text.draw(State, line_index, y, startpos, fg, hide_cursor)
|
||||||
else
|
else
|
||||||
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
|
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
|
||||||
if hide_cursor then
|
if hide_cursor then
|
||||||
Text.pretend_draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
|
Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
|
||||||
else
|
else
|
||||||
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
|
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
|
||||||
end
|
end
|
||||||
elseif pos + frag_len == State.cursor1.pos then
|
elseif pos + frag_len == State.cursor1.pos then
|
||||||
-- Show cursor at end of line.
|
-- Show cursor at end of line.
|
||||||
-- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
|
-- This place also catches end of wrapping screen lines. That doesn't seem worth distinguishing.
|
||||||
-- It seems useful to see a cursor whether your eye is on the left or right margin.
|
-- It seems useful to see a cursor whether your eye is on the left or right margin.
|
||||||
if hide_cursor then
|
if hide_cursor then
|
||||||
Text.pretend_draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
|
Text.pretend_draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y)
|
||||||
else
|
else
|
||||||
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
|
Text.draw_cursor(State, State.left+Text.x(State.font, screen_line, State.cursor1.pos-pos+1), y, Cursor_color)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -111,14 +111,14 @@ function Text.populate_screen_line_starting_pos(State, line_index)
|
||||||
local pos = 1
|
local pos = 1
|
||||||
-- try to wrap at word boundaries
|
-- try to wrap at word boundaries
|
||||||
for frag in line.data:gmatch('%S*%s*') do
|
for frag in line.data:gmatch('%S*%s*') do
|
||||||
local frag_width = App.width(frag)
|
local frag_width = State.font:getWidth(frag)
|
||||||
--? print('-- frag:', frag, pos, x, frag_width, State.width)
|
--? print('-- frag:', frag, pos, x, frag_width, State.width)
|
||||||
while x + frag_width > State.width do
|
while x + frag_width > State.width do
|
||||||
--? print('frag:', frag, pos, x, frag_width, State.width)
|
--? print('frag:', frag, pos, x, frag_width, State.width)
|
||||||
if x < 0.8 * State.width then
|
if x < 0.8 * State.width then
|
||||||
-- long word; chop it at some letter
|
-- long word; chop it at some letter
|
||||||
-- We're not going to reimplement TeX here.
|
-- We're not going to reimplement TeX here.
|
||||||
local bpos = Text.nearest_pos_less_than(frag, State.width - x)
|
local bpos = Text.nearest_pos_less_than(State.font, frag, State.width - x)
|
||||||
if x == 0 and bpos == 0 then
|
if x == 0 and bpos == 0 then
|
||||||
assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
|
assert(false, ("Infinite loop while line-wrapping. Editor is %dpx wide; window is %dpx wide"):format(State.width, App.screen.width))
|
||||||
end
|
end
|
||||||
|
@ -128,7 +128,7 @@ function Text.populate_screen_line_starting_pos(State, line_index)
|
||||||
--? if bpos > 0 then
|
--? if bpos > 0 then
|
||||||
--? print('after chop:', frag)
|
--? print('after chop:', frag)
|
||||||
--? end
|
--? end
|
||||||
frag_width = App.width(frag)
|
frag_width = State.font:getWidth(frag)
|
||||||
end
|
end
|
||||||
--? print('screen line:', pos)
|
--? print('screen line:', pos)
|
||||||
table.insert(line_cache.screen_line_starting_pos, pos)
|
table.insert(line_cache.screen_line_starting_pos, pos)
|
||||||
|
@ -423,7 +423,7 @@ function Text.up(State)
|
||||||
screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
|
screen_line_starting_pos = screen_line_starting_pos[#screen_line_starting_pos]
|
||||||
local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
|
local screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, screen_line_starting_pos)
|
||||||
local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
|
local s = string.sub(State.lines[State.cursor1.line].data, screen_line_starting_byte_offset)
|
||||||
State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
|
State.cursor1.pos = screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- move up one screen line in current line
|
-- move up one screen line in current line
|
||||||
|
@ -431,7 +431,7 @@ function Text.up(State)
|
||||||
local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
|
local new_screen_line_starting_pos = State.line_cache[State.cursor1.line].screen_line_starting_pos[screen_line_index-1]
|
||||||
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
|
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
|
||||||
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
|
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
|
||||||
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
|
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||||
--? print('cursor pos is now '..tostring(State.cursor1.pos))
|
--? print('cursor pos is now '..tostring(State.cursor1.pos))
|
||||||
end
|
end
|
||||||
if Text.lt1(State.cursor1, State.screen_top1) then
|
if Text.lt1(State.cursor1, State.screen_top1) then
|
||||||
|
@ -452,7 +452,7 @@ function Text.down(State)
|
||||||
if State.cursor1.line < #State.lines then
|
if State.cursor1.line < #State.lines then
|
||||||
local new_cursor_line = State.cursor1.line+1
|
local new_cursor_line = State.cursor1.line+1
|
||||||
State.cursor1.line = new_cursor_line
|
State.cursor1.line = new_cursor_line
|
||||||
State.cursor1.pos = Text.nearest_cursor_pos(State.lines[State.cursor1.line].data, State.cursor_x, State.left)
|
State.cursor1.pos = Text.nearest_cursor_pos(State.font, State.lines[State.cursor1.line].data, State.cursor_x, State.left)
|
||||||
--? print(State.cursor1.pos)
|
--? print(State.cursor1.pos)
|
||||||
end
|
end
|
||||||
if State.cursor1.line > State.screen_bottom1.line then
|
if State.cursor1.line > State.screen_bottom1.line then
|
||||||
|
@ -471,7 +471,7 @@ function Text.down(State)
|
||||||
--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
|
--? print('switching pos of screen line at cursor from '..tostring(screen_line_starting_pos)..' to '..tostring(new_screen_line_starting_pos))
|
||||||
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
|
local new_screen_line_starting_byte_offset = Text.offset(State.lines[State.cursor1.line].data, new_screen_line_starting_pos)
|
||||||
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
|
local s = string.sub(State.lines[State.cursor1.line].data, new_screen_line_starting_byte_offset)
|
||||||
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(s, State.cursor_x, State.left) - 1
|
State.cursor1.pos = new_screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, State.cursor_x, State.left) - 1
|
||||||
--? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
|
--? print('cursor pos is now', State.cursor1.line, State.cursor1.pos)
|
||||||
if scroll_down then
|
if scroll_down then
|
||||||
--? print('scroll up preserving cursor')
|
--? print('scroll up preserving cursor')
|
||||||
|
@ -685,8 +685,8 @@ function Text.to_pos_on_line(State, line_index, mx, my)
|
||||||
return line_cache.screen_line_starting_pos[screen_line_index+1]-1
|
return line_cache.screen_line_starting_pos[screen_line_index+1]-1
|
||||||
end
|
end
|
||||||
local s = string.sub(line.data, screen_line_starting_byte_offset)
|
local s = string.sub(line.data, screen_line_starting_byte_offset)
|
||||||
--? print('return', mx, Text.nearest_cursor_pos(s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1)
|
--? print('return', mx, Text.nearest_cursor_pos(State.font, s, mx, State.left), '=>', screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1)
|
||||||
return screen_line_starting_pos + Text.nearest_cursor_pos(s, mx, State.left) - 1
|
return screen_line_starting_pos + Text.nearest_cursor_pos(State.font, s, mx, State.left) - 1
|
||||||
end
|
end
|
||||||
y = nexty
|
y = nexty
|
||||||
end
|
end
|
||||||
|
@ -706,7 +706,7 @@ function Text.screen_line_width(State, line_index, i)
|
||||||
else
|
else
|
||||||
screen_line = string.sub(line.data, start_pos)
|
screen_line = string.sub(line.data, start_pos)
|
||||||
end
|
end
|
||||||
return App.width(screen_line)
|
return State.font:getWidth(screen_line)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Text.screen_line_index(screen_line_starting_pos, pos)
|
function Text.screen_line_index(screen_line_starting_pos, pos)
|
||||||
|
@ -720,12 +720,12 @@ end
|
||||||
-- convert x pixel coordinate to pos
|
-- convert x pixel coordinate to pos
|
||||||
-- oblivious to wrapping
|
-- oblivious to wrapping
|
||||||
-- result: 1 to len+1
|
-- result: 1 to len+1
|
||||||
function Text.nearest_cursor_pos(line, x, left)
|
function Text.nearest_cursor_pos(font, line, x, left)
|
||||||
if x < left then
|
if x < left then
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
local len = utf8.len(line)
|
local len = utf8.len(line)
|
||||||
local max_x = left+Text.x(line, len+1)
|
local max_x = left+Text.x(font, line, len+1)
|
||||||
if x > max_x then
|
if x > max_x then
|
||||||
return len+1
|
return len+1
|
||||||
end
|
end
|
||||||
|
@ -737,8 +737,8 @@ function Text.nearest_cursor_pos(line, x, left)
|
||||||
return leftpos
|
return leftpos
|
||||||
end
|
end
|
||||||
local curr = math.floor((leftpos+rightpos)/2)
|
local curr = math.floor((leftpos+rightpos)/2)
|
||||||
local currxmin = left+Text.x(line, curr)
|
local currxmin = left+Text.x(font, line, curr)
|
||||||
local currxmax = left+Text.x(line, curr+1)
|
local currxmax = left+Text.x(font, line, curr+1)
|
||||||
--? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
|
--? print('nearest', x, leftpos, rightpos, curr, currxmin, currxmax)
|
||||||
if currxmin <= x and x < currxmax then
|
if currxmin <= x and x < currxmax then
|
||||||
if x-currxmin < currxmax-x then
|
if x-currxmin < currxmax-x then
|
||||||
|
@ -762,18 +762,18 @@ end
|
||||||
-- return the nearest index of line (in utf8 code points) which lies entirely
|
-- return the nearest index of line (in utf8 code points) which lies entirely
|
||||||
-- within x pixels of the left margin
|
-- within x pixels of the left margin
|
||||||
-- result: 0 to len+1
|
-- result: 0 to len+1
|
||||||
function Text.nearest_pos_less_than(line, x)
|
function Text.nearest_pos_less_than(font, line, x)
|
||||||
--? print('', '-- nearest_pos_less_than', line, x)
|
--? print('', '-- nearest_pos_less_than', line, x)
|
||||||
local len = utf8.len(line)
|
local len = utf8.len(line)
|
||||||
local max_x = Text.x_after(line, len)
|
local max_x = Text.x_after(font, line, len)
|
||||||
if x > max_x then
|
if x > max_x then
|
||||||
return len+1
|
return len+1
|
||||||
end
|
end
|
||||||
local left, right = 0, len+1
|
local left, right = 0, len+1
|
||||||
while true do
|
while true do
|
||||||
local curr = math.floor((left+right)/2)
|
local curr = math.floor((left+right)/2)
|
||||||
local currxmin = Text.x_after(line, curr+1)
|
local currxmin = Text.x_after(font, line, curr+1)
|
||||||
local currxmax = Text.x_after(line, curr+2)
|
local currxmax = Text.x_after(font, line, curr+2)
|
||||||
--? print('', x, left, right, curr, currxmin, currxmax)
|
--? print('', x, left, right, curr, currxmin, currxmax)
|
||||||
if currxmin <= x and x < currxmax then
|
if currxmin <= x and x < currxmax then
|
||||||
return curr
|
return curr
|
||||||
|
@ -790,18 +790,18 @@ function Text.nearest_pos_less_than(line, x)
|
||||||
assert(false, 'failed to map x pixel to pos')
|
assert(false, 'failed to map x pixel to pos')
|
||||||
end
|
end
|
||||||
|
|
||||||
function Text.x_after(s, pos)
|
function Text.x_after(font, s, pos)
|
||||||
local len = utf8.len(s)
|
local len = utf8.len(s)
|
||||||
local offset = Text.offset(s, math.min(pos+1, len+1))
|
local offset = Text.offset(s, math.min(pos+1, len+1))
|
||||||
local s_before = s:sub(1, offset-1)
|
local s_before = s:sub(1, offset-1)
|
||||||
--? print('^'..s_before..'$')
|
--? print('^'..s_before..'$')
|
||||||
return App.width(s_before)
|
return font:getWidth(s_before)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Text.x(s, pos)
|
function Text.x(font, s, pos)
|
||||||
local offset = Text.offset(s, pos)
|
local offset = Text.offset(s, pos)
|
||||||
local s_before = s:sub(1, offset-1)
|
local s_before = s:sub(1, offset-1)
|
||||||
return App.width(s_before)
|
return font:getWidth(s_before)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Text.to2(State, loc1)
|
function Text.to2(State, loc1)
|
||||||
|
|
Loading…
Reference in New Issue