Merge lines.love

This commit is contained in:
Kartik K. Agaram 2022-09-19 00:55:00 -07:00
commit 1c49f74c8d
4 changed files with 105 additions and 48 deletions

View File

@ -64,10 +64,6 @@ function source.draw_file_navigator()
love.graphics.rectangle('fill', 0,Menu_status_bar_height, App.screen.width, File_navigation.num_lines * Editor_state.line_height + --[[highlight padding]] 2)
local x,y = 5, Menu_status_bar_height
for i,filename in ipairs(File_navigation.candidates) do
if filename == 'source' then
App.color(Menu_border_color)
love.graphics.line(Menu_cursor-10,2, Menu_cursor-10,Menu_status_bar_height-2)
end
x,y = add_file_to_menu(x,y, filename, i == File_navigation.index)
if Menu_cursor >= App.screen.width - 5 then
break
@ -124,9 +120,7 @@ function add_file_to_menu(x,y, s, cursor_highlight)
end
button(Editor_state, 'menu', {x=x-5, y=y-2, w=width+5*2, h=Editor_state.line_height+2*2, color=colortable(color),
onpress1 = function()
local candidate = guess_source(s..'.lua')
source.switch_to_file(candidate)
Show_file_navigator = false
navigate_to_file(s)
end
})
App.color(Menu_command_color)
@ -135,21 +129,34 @@ function add_file_to_menu(x,y, s, cursor_highlight)
return x,y
end
function navigate_to_file(s)
move_candidate_to_front(s)
local candidate = guess_source(s..'.lua')
source.switch_to_file(candidate)
reset_file_navigator()
end
function move_candidate_to_front(s)
local index = array.find(File_navigation.all_candidates, s)
assert(index)
table.remove(File_navigation.all_candidates, index)
table.insert(File_navigation.all_candidates, 1, s)
end
function reset_file_navigator()
Show_file_navigator = false
File_navigation.index = 1
File_navigation.filter = ''
File_navigation.candidates = File_navigation.all_candidates
end
function keychord_pressed_on_file_navigator(chord, key)
log(2, 'file navigator: '..chord)
log(2, {name='file_navigator_state', files=File_navigation.candidates, index=File_navigation.index})
if chord == 'escape' then
Show_file_navigator = false
File_navigation.index = 1
File_navigation.filter = ''
File_navigation.candidates = File_navigation.all_candidates
reset_file_navigator()
elseif chord == 'return' then
local candidate = guess_source(File_navigation.candidates[File_navigation.index]..'.lua')
source.switch_to_file(candidate)
Show_file_navigator = false
File_navigation.index = 1
File_navigation.filter = ''
File_navigation.candidates = File_navigation.all_candidates
navigate_to_file(File_navigation.candidates[File_navigation.index])
elseif chord == 'backspace' then
local len = utf8.len(File_navigation.filter)
local byte_offset = Text.offset(File_navigation.filter, len)

View File

@ -21,14 +21,14 @@ end
function Text.search_next(State)
-- search current line from cursor
local pos = find(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos)
local pos = find(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos, --[[literal]] true)
if pos then
State.cursor1.pos = pos
end
if pos == nil then
-- search lines below cursor
for i=State.cursor1.line+1,#State.lines do
pos = find(State.lines[i].data, State.search_term)
pos = find(State.lines[i].data, State.search_term, --[[from start]] nil, --[[literal]] true)
if pos then
State.cursor1 = {line=i, pos=pos}
break
@ -38,7 +38,7 @@ function Text.search_next(State)
if pos == nil then
-- wrap around
for i=1,State.cursor1.line-1 do
pos = find(State.lines[i].data, State.search_term)
pos = find(State.lines[i].data, State.search_term, --[[from start]] nil, --[[literal]] true)
if pos then
State.cursor1 = {line=i, pos=pos}
break
@ -47,7 +47,7 @@ function Text.search_next(State)
end
if pos == nil then
-- search current line until cursor
pos = find(State.lines[State.cursor1.line].data, State.search_term)
pos = find(State.lines[State.cursor1.line].data, State.search_term, --[[from start]] nil, --[[literal]] true)
if pos and pos < State.cursor1.pos then
State.cursor1.pos = pos
end
@ -67,14 +67,14 @@ end
function Text.search_previous(State)
-- search current line before cursor
local pos = rfind(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos-1)
local pos = rfind(State.lines[State.cursor1.line].data, State.search_term, State.cursor1.pos-1, --[[literal]] true)
if pos then
State.cursor1.pos = pos
end
if pos == nil then
-- search lines above cursor
for i=State.cursor1.line-1,1,-1 do
pos = rfind(State.lines[i].data, State.search_term)
pos = rfind(State.lines[i].data, State.search_term, --[[from end]] nil, --[[literal]] true)
if pos then
State.cursor1 = {line=i, pos=pos}
break
@ -84,7 +84,7 @@ function Text.search_previous(State)
if pos == nil then
-- wrap around
for i=#State.lines,State.cursor1.line+1,-1 do
pos = rfind(State.lines[i].data, State.search_term)
pos = rfind(State.lines[i].data, State.search_term, --[[from end]] nil, --[[literal]] true)
if pos then
State.cursor1 = {line=i, pos=pos}
break
@ -93,7 +93,7 @@ function Text.search_previous(State)
end
if pos == nil then
-- search current line after cursor
pos = rfind(State.lines[State.cursor1.line].data, State.search_term)
pos = rfind(State.lines[State.cursor1.line].data, State.search_term, --[[from end]] nil, --[[literal]] true)
if pos and pos > State.cursor1.pos then
State.cursor1.pos = pos
end

View File

@ -45,6 +45,7 @@ function source.initialize_globals()
},
index = 1,
filter = '',
cursors = {}, -- filename to cursor1, screen_top1
}
File_navigation.candidates = File_navigation.all_candidates -- modified with filter
@ -71,7 +72,7 @@ function source.initialize()
source.initialize_default_settings()
end
source.initialize_edit_side{'run.lua'}
source.initialize_edit_side()
source.initialize_log_browser_side()
Menu_status_bar_height = 5 + Editor_state.line_height + 5
@ -82,20 +83,15 @@ end
-- environment for a mutable file of bifolded text
-- TODO: some initialization is also happening in load_settings/initialize_default_settings. Clean that up.
function source.initialize_edit_side(arg)
if #arg > 0 then
Editor_state.filename = arg[1]
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
function source.initialize_edit_side()
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
if File_navigation.cursors[Editor_state.filename] then
Editor_state.screen_top1 = File_navigation.cursors[Editor_state.filename].screen_top1
Editor_state.cursor1 = File_navigation.cursors[Editor_state.filename].cursor1
else
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1}
else
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
end
if #arg > 1 then
print('ignoring commandline args after '..arg[1])
end
-- We currently start out with side B collapsed.
@ -138,8 +134,16 @@ function source.load_settings()
end
Editor_state = edit.initialize_state(Margin_top, Margin_left, right, settings.font_height, math.floor(settings.font_height*1.3))
Editor_state.filename = settings.filename
Editor_state.screen_top1 = settings.screen_top
Editor_state.cursor1 = settings.cursor
Editor_state.filename = basename(Editor_state.filename) -- migrate settings that used full paths; we now support only relative paths within the app
if settings.cursors then
File_navigation.cursors = settings.cursors
Editor_state.screen_top1 = File_navigation.cursors[Editor_state.filename].screen_top1
Editor_state.cursor1 = File_navigation.cursors[Editor_state.filename].cursor1
else
-- migrate old settings
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1}
end
end
function source.set_window_position_from_settings(settings)
@ -153,6 +157,7 @@ function source.initialize_default_settings()
local em = App.newText(love.graphics.getFont(), 'm')
source.initialize_window_geometry(App.width(em))
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right)
Editor_state.filename = 'run.lua'
Editor_state.font_height = font_height
Editor_state.line_height = math.floor(font_height*1.3)
Editor_state.em = em
@ -214,12 +219,19 @@ function source.switch_to_file(filename)
if Editor_state.next_save then
save_to_disk(Editor_state)
end
-- save cursor position
File_navigation.cursors[Editor_state.filename] = {cursor1=Editor_state.cursor1, screen_top1=Editor_state.screen_top1}
-- clear the slate for the new file
Editor_state.filename = filename
load_from_disk(Editor_state)
Text.redraw_all(Editor_state)
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1}
if File_navigation.cursors[filename] then
Editor_state.screen_top1 = File_navigation.cursors[filename].screen_top1
Editor_state.cursor1 = File_navigation.cursors[filename].cursor1
else
Editor_state.screen_top1 = {line=1, pos=1}
Editor_state.cursor1 = {line=1, pos=1}
end
end
function source.draw()
@ -260,17 +272,14 @@ function source.settings()
--? print('reading source window position')
Settings.source.x, Settings.source.y, Settings.source.displayindex = App.screen.position()
end
local filename = Editor_state.filename
if is_relative_path(filename) then
filename = love.filesystem.getWorkingDirectory()..'/'..filename -- '/' should work even on Windows
end
--? print('saving source settings', Settings.source.x, Settings.source.y, Settings.source.displayindex)
File_navigation.cursors[Editor_state.filename] = {cursor1=Editor_state.cursor1, screen_top1=Editor_state.screen_top1}
return {
x=Settings.source.x, y=Settings.source.y, displayindex=Settings.source.displayindex,
width=App.screen.width, height=App.screen.height,
font_height=Editor_state.font_height,
filename=filename,
screen_top=Editor_state.screen_top1, cursor=Editor_state.cursor1,
filename=Editor_state.filename,
cursors=File_navigation.cursors,
show_log_browser_side=Show_log_browser_side,
focus=Focus,
}
@ -281,6 +290,11 @@ function source.mouse_pressed(x,y, mouse_button)
--? print('mouse click', x, y)
--? print(Editor_state.left, Editor_state.right)
--? print(Log_browser_state.left, Log_browser_state.right)
if Show_file_navigator and y < Menu_status_bar_height + File_navigation.num_lines * Editor_state.line_height then
-- send click to buttons
edit.mouse_pressed(Editor_state, x,y, mouse_button)
return
end
if x < Editor_state.right + Margin_right then
--? print('click on edit side')
if Focus ~= 'edit' then

View File

@ -216,3 +216,39 @@ end
function is_relative_path(path)
return not is_absolute_path(path)
end
function dirname(path)
local os_path_separator = package.config:sub(1,1)
if os_path_separator == '/' then
-- POSIX systems permit backslashes in filenames
return path:match('.*/') or './'
elseif os_path_separator == '\\' then
return path:match('.*[/\\]') or './'
else
error('What OS is this? LÖVE reports that the path separator is "'..os_path_separator..'"')
end
end
function test_dirname()
check_eq(dirname('a/b'), 'a/', 'F - test_dirname')
check_eq(dirname('x'), './', 'F - test_dirname/current')
end
function basename(path)
local os_path_separator = package.config:sub(1,1)
if os_path_separator == '/' then
-- POSIX systems permit backslashes in filenames
return string.gsub(path, ".*/(.*)", "%1")
elseif os_path_separator == '\\' then
return string.gsub(path, ".*[/\\](.*)", "%1")
else
error('What OS is this? LÖVE reports that the path separator is "'..os_path_separator..'"')
end
end
function empty(h)
for _,_ in pairs(h) do
return false
end
return true
end