From 0e20565e17cd9f1580aa2451de8b16b863ae0382 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 15 Sep 2023 08:24:54 -0700 Subject: [PATCH 1/7] clean up a slight jitter when drawing circles Earlier the ghost while drawing wouldn't quite match the final shape. Now the math is identical in draw_pending_shape. It's a little unfortunate that we have this duplication of formulae. At least there are no other stray calls of App.mouse_x in draw_pending_shape. --- drawing.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drawing.lua b/drawing.lua index 5af25fb..ac4990a 100644 --- a/drawing.lua +++ b/drawing.lua @@ -191,8 +191,9 @@ function Drawing.draw_pending_shape(drawing, top, left,right) if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then return end + local r = round(geom.dist(center.x, center.y, mx, my)) local cx,cy = px(center.x), py(center.y) - love.graphics.circle('line', cx,cy, geom.dist(cx,cy, App.mouse_x(),App.mouse_y())) + love.graphics.circle('line', cx,cy, Drawing.pixels(r, width)) elseif shape.mode == 'arc' then local center = drawing.points[shape.center] if mx < 0 or mx >= 256 or my < 0 or my >= drawing.h then From 0a12e4c733a2483eef840fb82aec4f4f6ec68b3c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 15 Sep 2023 08:46:36 -0700 Subject: [PATCH 2/7] change a helper slightly --- edit.lua | 18 +++++++++++------- source_edit.lua | 18 +++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/edit.lua b/edit.lua index 7c5e4e0..1ac2bd1 100644 --- a/edit.lua +++ b/edit.lua @@ -113,7 +113,7 @@ function edit.check_locs(State) or not edit.cursor_on_text(State) or not Text.le1(State.screen_top1, State.cursor1) then State.screen_top1 = {line=1, pos=1} - edit.put_cursor_on_first_text_line(State) + edit.put_cursor_on_next_text_line(State) end end @@ -139,12 +139,16 @@ function edit.cursor_on_text(State) and State.lines[State.cursor1.line].mode == 'text' end -function edit.put_cursor_on_first_text_line(State) - for i,line in ipairs(State.lines) do - if line.mode == 'text' then - State.cursor1 = {line=i, pos=1} - break - end +function edit.put_cursor_on_next_text_line(State) + while true do + if State.cursor1.line >= #State.lines then + break + end + if State.lines[State.cursor1.line].mode == 'text' then + break + end + State.cursor1.line = State.cursor1.line+1 + State.cursor1.pos = 1 end end diff --git a/source_edit.lua b/source_edit.lua index 78c7f4d..f949aa5 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -115,7 +115,7 @@ function edit.check_locs(State) or not edit.cursor_on_text(State) or not Text.le1(State.screen_top1, State.cursor1) then State.screen_top1 = {line=1, pos=1} - edit.put_cursor_on_first_text_line(State) + edit.put_cursor_on_next_text_line(State) end end @@ -131,12 +131,16 @@ function edit.cursor_on_text(State) and State.lines[State.cursor1.line].mode == 'text' end -function edit.put_cursor_on_first_text_line(State) - for i,line in ipairs(State.lines) do - if line.mode == 'text' then - State.cursor1 = {line=i, pos=1} - break - end +function edit.put_cursor_on_next_text_line(State) + while true do + if State.cursor1.line >= #State.lines then + break + end + if State.lines[State.cursor1.line].mode == 'text' then + break + end + State.cursor1.line = State.cursor1.line+1 + State.cursor1.pos = 1 end end From bafc45b028ad8db3d8be51cf1a09b7f118d75503 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 15 Sep 2023 08:52:39 -0700 Subject: [PATCH 3/7] bugfix: crash when using mouse wheel All the Text functions assume the cursor is always on a text line. I was violating that invariant. * When scrolling up, I start the cursor at the top-most line below the screen top. * When scrolling down, I start the cursor at the top-most line below the screen bottom. I think it would feel slightly more natural for it to be the bottom-most line above the screen bottom. However, the Text functions maintain an invariant that the bottom-most line in a buffer will be text. There's no such invariant for the top-most line. --- edit.lua | 2 ++ source_edit.lua | 2 ++ 2 files changed, 4 insertions(+) diff --git a/edit.lua b/edit.lua index 1ac2bd1..d30dbdc 100644 --- a/edit.lua +++ b/edit.lua @@ -343,11 +343,13 @@ end function edit.mouse_wheel_move(State, dx,dy) if dy > 0 then State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos} + edit.put_cursor_on_next_text_line(State) for i=1,math.floor(dy) do Text.up(State) end elseif dy < 0 then State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos} + edit.put_cursor_on_next_text_line(State) for i=1,math.floor(-dy) do Text.down(State) end diff --git a/source_edit.lua b/source_edit.lua index f949aa5..6dbf883 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -333,11 +333,13 @@ end function edit.mouse_wheel_move(State, dx,dy) if dy > 0 then State.cursor1 = {line=State.screen_top1.line, pos=State.screen_top1.pos} + edit.put_cursor_on_next_text_line(State) for i=1,math.floor(dy) do Text.up(State) end elseif dy < 0 then State.cursor1 = {line=State.screen_bottom1.line, pos=State.screen_bottom1.pos} + edit.put_cursor_on_next_text_line(State) for i=1,math.floor(-dy) do Text.down(State) end From ab5ba3f3e5f4f1f1583d798ace26683cb572ed18 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 15 Sep 2023 11:13:04 -0700 Subject: [PATCH 4/7] assume starty can be nil in update MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a backport of a bugfix in pensieve.love. It's not _technically_ a bug here in lines.love, but it seems worth establishing an architectural invariant (or rather lack of invariant). LÖVE's standard event loop performs the following sequence of operations in a single frame: * process events * update * draw Ideally any mutations to global state happen during the first two phases, while drawing includes no mutation. However, there is a special case: `starty`, the top y coordinate for each each line in the editor. This is used all over the place, and the cheapest way to compute it is to simply save it while drawing. However, draw by definition only updates `starty` for lines that are drawn on screen. To avoid stale data on lines off screen, say after scrolling, events often clear `starty` for all lines, leaving it to the next draw phase to repopulate the right lines. Sandwiched between the above two "however"s, the update phase needs to gracefully handle `starty` being nil in the occasional frame right after an event. I think I've audited all our uses of `starty`, and this commit fixes the only place that violates this rule. --- drawing.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drawing.lua b/drawing.lua index ac4990a..a98f5b2 100644 --- a/drawing.lua +++ b/drawing.lua @@ -249,6 +249,12 @@ function Drawing.update(State) if State.lines.current_drawing == nil then return end local drawing = State.lines.current_drawing local line_cache = State.line_cache[State.lines.current_drawing_index] + if line_cache.starty == nil then + -- some event cleared starty just this frame + -- draw in this frame will soon set starty + -- just skip this frame + return + end assert(drawing.mode == 'drawing') local pmx, pmy = App.mouse_x(), App.mouse_y() local mx = Drawing.coord(pmx-State.left, State.width) From 76f119b7b996c5751a42bf09b1a234b98aa6d48b Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 15 Sep 2023 13:30:42 -0700 Subject: [PATCH 5/7] hide line numbers from log browser --- source.lua | 2 +- source_edit.lua | 12 ++++++++---- source_text.lua | 8 +++++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/source.lua b/source.lua index 4a8bb48..e8dd7cc 100644 --- a/source.lua +++ b/source.lua @@ -237,7 +237,7 @@ function source.switch_to_file(filename) end function source.draw() - edit.draw(Editor_state, --[[hide cursor?]] Show_file_navigator) + edit.draw(Editor_state, --[[hide cursor?]] Show_file_navigator, --[[show line numbers]] true) if Show_log_browser_side then -- divider App.color(Divider_color) diff --git a/source_edit.lua b/source_edit.lua index 6dbf883..506aa41 100644 --- a/source_edit.lua +++ b/source_edit.lua @@ -89,7 +89,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c line_height = line_height, top = top, - left = math.floor(left), + left = math.floor(left), -- left margin for text; line numbers go to the left of this right = math.floor(right), width = right-left, @@ -144,7 +144,7 @@ function edit.put_cursor_on_next_text_line(State) end end -function edit.draw(State, hide_cursor) +function edit.draw(State, hide_cursor, show_line_numbers) State.button_handlers = {} App.color(Text_color) if #State.lines ~= #State.line_cache then @@ -173,7 +173,11 @@ function edit.draw(State, hide_cursor) end if line.data == '' then -- button to insert new drawing - button(State, 'draw', {x=4, y=y+4, w=12,h=12, color={1,1,0}, + local buttonx = State.left-Margin_left+4 + if show_line_numbers then + buttonx = 4 -- HACK: position draw buttons at a fixed x on screen + end + button(State, 'draw', {x=buttonx, y=y+4, w=12,h=12, color={1,1,0}, icon = icon.insert_drawing, onpress1 = function() Drawing.before = snapshot(State, line_index-1, line_index) @@ -187,7 +191,7 @@ function edit.draw(State, hide_cursor) end, }) end - y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor) + y, screen_bottom1.pos = Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers) --? print('=> y', y) elseif line.mode == 'drawing' then y = y+Drawing_padding_top diff --git a/source_text.lua b/source_text.lua index 68b560e..df36266 100644 --- a/source_text.lua +++ b/source_text.lua @@ -3,7 +3,7 @@ Text = {} -- draw a line starting from startpos to screen at y between State.left and State.right -- 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) +function Text.draw(State, line_index, y, startpos, hide_cursor, show_line_numbers) local line = State.lines[line_index] local line_cache = State.line_cache[line_index] line_cache.starty = y @@ -12,8 +12,10 @@ function Text.draw(State, line_index, y, startpos, hide_cursor) local final_screen_line_starting_pos = startpos -- track value to return Text.populate_screen_line_starting_pos(State, line_index) Text.populate_link_offsets(State, line_index) - App.color(Line_number_color) - love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y) + if show_line_numbers then + App.color(Line_number_color) + love.graphics.print(line_index, State.left-Line_number_width*App.width('m')+10,y) + end initialize_color() assert(#line_cache.screen_line_starting_pos >= 1) for i=1,#line_cache.screen_line_starting_pos do From 6ed60848a4d2bca5a2c64e18d58671d973ee6479 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 15 Sep 2023 13:33:26 -0700 Subject: [PATCH 6/7] delete some dead code This is a holdover from the days of bifold text. --- commands.lua | 3 +-- log_browser.lua | 10 ---------- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/commands.lua b/commands.lua index 512992d..4e891ef 100644 --- a/commands.lua +++ b/commands.lua @@ -129,8 +129,7 @@ end function navigate_to_file(s) move_candidate_to_front(s) - local candidate = guess_source(s..'.lua') - source.switch_to_file(candidate) + source.switch_to_file(s..'.lua') reset_file_navigator() end diff --git a/log_browser.lua b/log_browser.lua index 078db06..6432d85 100644 --- a/log_browser.lua +++ b/log_browser.lua @@ -35,7 +35,6 @@ function log_browser.parse(State) if rest then line.data = rest end - line.filename = guess_source(line.filename) line.line_number = tonumber(line.line_number) if line.data:sub(1,1) == '{' then local data = json.decode(line.data) @@ -75,15 +74,6 @@ function table.shallowcopy(x) return {unpack(x)} end -function guess_source(filename) - local possible_source = filename:gsub('%.lua$', '%.splua') - if file_exists(possible_source) then - return possible_source - else - return filename - end -end - function log_browser.draw(State, hide_cursor) assert(#State.lines == #State.line_cache) local mouse_line_index = log_browser.line_index(State, App.mouse_x(), App.mouse_y()) From 715c6fd32fadf8b14f4755cc31bacfb21c7f7dac Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 15 Sep 2023 13:36:27 -0700 Subject: [PATCH 7/7] source: show file being edited in window title bar --- commands.lua | 1 + source.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/commands.lua b/commands.lua index 4e891ef..4a0d24a 100644 --- a/commands.lua +++ b/commands.lua @@ -130,6 +130,7 @@ end function navigate_to_file(s) move_candidate_to_front(s) source.switch_to_file(s..'.lua') + love.window.setTitle('lines.love - source - '..Editor_state.filename) reset_file_navigator() end diff --git a/source.lua b/source.lua index e8dd7cc..dafb1a7 100644 --- a/source.lua +++ b/source.lua @@ -74,7 +74,7 @@ function source.initialize() -- keep a few blank lines around: https://merveilles.town/@akkartik/110084833821965708 - love.window.setTitle('lines.love - source') + love.window.setTitle('lines.love - source - '..Editor_state.filename)