add state arg to a few functions

- Text.draw_highlight
  - Text.clip_selection
  - Text.selection
  - Text.cut_selection
  - Text.delete_selection
  - Text.delete_selection_without_undo
  - Text.mouse_pos
  - Text.to_pos
This commit is contained in:
Kartik K. Agaram 2022-07-12 16:43:23 -07:00
parent 800a5c064a
commit 188bbc73cc
3 changed files with 67 additions and 67 deletions

View File

@ -299,7 +299,7 @@ function edit.keychord_pressed(State, chord, key)
-- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys) -- (we're not creating any ctrl-shift- or alt-shift- combinations using regular/printable keys)
(not App.shift_down() or utf8.len(key) == 1) and (not App.shift_down() or utf8.len(key) == 1) and
chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and backspace ~= 'delete' and not App.is_cursor_movement(chord) then chord ~= 'C-c' and chord ~= 'C-x' and chord ~= 'backspace' and backspace ~= 'delete' and not App.is_cursor_movement(chord) then
Text.delete_selection(State.margin_left, App.screen.width-State.margin_right) Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right)
end end
if State.search_term then if State.search_term then
if chord == 'escape' then if chord == 'escape' then
@ -365,13 +365,13 @@ function edit.keychord_pressed(State, chord, key)
-- clipboard -- clipboard
elseif chord == 'C-c' then elseif chord == 'C-c' then
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
local s = Text.selection() local s = Text.selection(State)
if s then if s then
App.setClipboardText(s) App.setClipboardText(s)
end end
elseif chord == 'C-x' then elseif chord == 'C-x' then
for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll for _,line in ipairs(State.lines) do line.y = nil end -- just in case we scroll
local s = Text.cut_selection(State.margin_left, App.screen.width-State.margin_right) local s = Text.cut_selection(State, State.margin_left, App.screen.width-State.margin_right)
if s then if s then
App.setClipboardText(s) App.setClipboardText(s)
end end

View File

@ -1,20 +1,20 @@
-- helpers for selecting portions of text -- helpers for selecting portions of text
-- Return any intersection of the region from Editor_state.selection1 to Editor_state.cursor1 (or -- Return any intersection of the region from State.selection1 to State.cursor1 (or
-- current mouse, if mouse is pressed; or recent mouse if mouse is pressed and -- current mouse, if mouse is pressed; or recent mouse if mouse is pressed and
-- currently over a drawing) with the region between {line=line_index, pos=apos} -- currently over a drawing) with the region between {line=line_index, pos=apos}
-- and {line=line_index, pos=bpos}. -- and {line=line_index, pos=bpos}.
-- apos must be less than bpos. However Editor_state.selection1 and Editor_state.cursor1 can be in any order. -- apos must be less than bpos. However State.selection1 and State.cursor1 can be in any order.
-- Result: positions spos,epos between apos,bpos. -- Result: positions spos,epos between apos,bpos.
function Text.clip_selection(line_index, apos, bpos, left, right) function Text.clip_selection(State, line_index, apos, bpos, left, right)
if Editor_state.selection1.line == nil then return nil,nil end if State.selection1.line == nil then return nil,nil end
-- min,max = sorted(Editor_state.selection1,Editor_state.cursor1) -- min,max = sorted(State.selection1,State.cursor1)
local minl,minp = Editor_state.selection1.line,Editor_state.selection1.pos local minl,minp = State.selection1.line,State.selection1.pos
local maxl,maxp local maxl,maxp
if App.mouse_down(1) then if App.mouse_down(1) then
maxl,maxp = Text.mouse_pos(left, right) maxl,maxp = Text.mouse_pos(State, left, right)
else else
maxl,maxp = Editor_state.cursor1.line,Editor_state.cursor1.pos maxl,maxp = State.cursor1.line,State.cursor1.pos
end end
if minl > maxl then if minl > maxl then
minl,maxl = maxl,minl minl,maxl = maxl,minl
@ -51,7 +51,7 @@ end
-- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line -- draw highlight for line corresponding to (lo,hi) given an approximate x,y and pos on the same screen line
-- Creates text objects every time, so use this sparingly. -- Creates text objects every time, so use this sparingly.
-- Returns some intermediate computation useful elsewhere. -- Returns some intermediate computation useful elsewhere.
function Text.draw_highlight(line, x,y, pos, lo,hi) function Text.draw_highlight(State, line, x,y, pos, lo,hi)
if lo then if lo then
local lo_offset = Text.offset(line.data, lo) local lo_offset = Text.offset(line.data, lo)
local hi_offset = Text.offset(line.data, hi) local hi_offset = Text.offset(line.data, hi)
@ -69,29 +69,29 @@ function Text.draw_highlight(line, x,y, pos, lo,hi)
local text = App.newText(love.graphics.getFont(), s) local text = App.newText(love.graphics.getFont(), s)
local text_width = App.width(text) local text_width = App.width(text)
App.color(Highlight_color) App.color(Highlight_color)
love.graphics.rectangle('fill', x+lo_px,y, text_width,Editor_state.line_height) love.graphics.rectangle('fill', x+lo_px,y, text_width,State.line_height)
App.color(Text_color) App.color(Text_color)
return lo_px return lo_px
end end
end end
-- inefficient for some reason, so don't do it on every frame -- inefficient for some reason, so don't do it on every frame
function Text.mouse_pos(left, right) function Text.mouse_pos(State, left, right)
local time = love.timer.getTime() local time = love.timer.getTime()
if Editor_state.recent_mouse.time and Editor_state.recent_mouse.time > time-0.1 then if State.recent_mouse.time and State.recent_mouse.time > time-0.1 then
return Editor_state.recent_mouse.line, Editor_state.recent_mouse.pos return State.recent_mouse.line, State.recent_mouse.pos
end end
Editor_state.recent_mouse.time = time State.recent_mouse.time = time
local line,pos = Text.to_pos(App.mouse_x(), App.mouse_y(), left, right) local line,pos = Text.to_pos(State, App.mouse_x(), App.mouse_y(), left, right)
if line then if line then
Editor_state.recent_mouse.line = line State.recent_mouse.line = line
Editor_state.recent_mouse.pos = pos State.recent_mouse.pos = pos
end end
return Editor_state.recent_mouse.line, Editor_state.recent_mouse.pos return State.recent_mouse.line, State.recent_mouse.pos
end end
function Text.to_pos(x,y, left, right) function Text.to_pos(State, x,y, left, right)
for line_index,line in ipairs(Editor_state.lines) do for line_index,line in ipairs(State.lines) do
if line.mode == 'text' then if line.mode == 'text' then
if Text.in_line(line, x,y, left, right) then if Text.in_line(line, x,y, left, right) then
return line_index, Text.to_pos_on_line(line, x,y, left, right) return line_index, Text.to_pos_on_line(line, x,y, left, right)
@ -100,26 +100,26 @@ function Text.to_pos(x,y, left, right)
end end
end end
function Text.cut_selection(left, right) function Text.cut_selection(State, left, right)
if Editor_state.selection1.line == nil then return end if State.selection1.line == nil then return end
local result = Text.selection() local result = Text.selection(State)
Text.delete_selection(left, right) Text.delete_selection(State, left, right)
return result return result
end end
function Text.delete_selection(left, right) function Text.delete_selection(State, left, right)
if Editor_state.selection1.line == nil then return end if State.selection1.line == nil then return end
local minl,maxl = minmax(Editor_state.selection1.line, Editor_state.cursor1.line) local minl,maxl = minmax(State.selection1.line, State.cursor1.line)
local before = snapshot(minl, maxl) local before = snapshot(minl, maxl)
Text.delete_selection_without_undo(left, right) Text.delete_selection_without_undo(State, left, right)
record_undo_event({before=before, after=snapshot(Editor_state.cursor1.line)}) record_undo_event({before=before, after=snapshot(State.cursor1.line)})
end end
function Text.delete_selection_without_undo(left, right) function Text.delete_selection_without_undo(State, left, right)
if Editor_state.selection1.line == nil then return end if State.selection1.line == nil then return end
-- min,max = sorted(Editor_state.selection1,Editor_state.cursor1) -- min,max = sorted(State.selection1,State.cursor1)
local minl,minp = Editor_state.selection1.line,Editor_state.selection1.pos local minl,minp = State.selection1.line,State.selection1.pos
local maxl,maxp = Editor_state.cursor1.line,Editor_state.cursor1.pos local maxl,maxp = State.cursor1.line,State.cursor1.pos
if minl > maxl then if minl > maxl then
minl,maxl = maxl,minl minl,maxl = maxl,minl
minp,maxp = maxp,minp minp,maxp = maxp,minp
@ -128,36 +128,36 @@ function Text.delete_selection_without_undo(left, right)
minp,maxp = maxp,minp minp,maxp = maxp,minp
end end
end end
-- update Editor_state.cursor1 and Editor_state.selection1 -- update State.cursor1 and State.selection1
Editor_state.cursor1.line = minl State.cursor1.line = minl
Editor_state.cursor1.pos = minp State.cursor1.pos = minp
if Text.lt1(Editor_state.cursor1, Editor_state.screen_top1) then if Text.lt1(State.cursor1, State.screen_top1) then
Editor_state.screen_top1.line = Editor_state.cursor1.line State.screen_top1.line = State.cursor1.line
_,Editor_state.screen_top1.pos = Text.pos_at_start_of_cursor_screen_line(left, right) _,State.screen_top1.pos = Text.pos_at_start_of_cursor_screen_line(left, right)
end end
Editor_state.selection1 = {} State.selection1 = {}
-- delete everything between min (inclusive) and max (exclusive) -- delete everything between min (inclusive) and max (exclusive)
Text.clear_cache(Editor_state.lines[minl]) Text.clear_cache(State.lines[minl])
local min_offset = Text.offset(Editor_state.lines[minl].data, minp) local min_offset = Text.offset(State.lines[minl].data, minp)
local max_offset = Text.offset(Editor_state.lines[maxl].data, maxp) local max_offset = Text.offset(State.lines[maxl].data, maxp)
if minl == maxl then if minl == maxl then
--? print('minl == maxl') --? print('minl == maxl')
Editor_state.lines[minl].data = Editor_state.lines[minl].data:sub(1, min_offset-1)..Editor_state.lines[minl].data:sub(max_offset) State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..State.lines[minl].data:sub(max_offset)
return return
end end
assert(minl < maxl) assert(minl < maxl)
local rhs = Editor_state.lines[maxl].data:sub(max_offset) local rhs = State.lines[maxl].data:sub(max_offset)
for i=maxl,minl+1,-1 do for i=maxl,minl+1,-1 do
table.remove(Editor_state.lines, i) table.remove(State.lines, i)
end end
Editor_state.lines[minl].data = Editor_state.lines[minl].data:sub(1, min_offset-1)..rhs State.lines[minl].data = State.lines[minl].data:sub(1, min_offset-1)..rhs
end end
function Text.selection() function Text.selection(State)
if Editor_state.selection1.line == nil then return end if State.selection1.line == nil then return end
-- min,max = sorted(Editor_state.selection1,Editor_state.cursor1) -- min,max = sorted(State.selection1,State.cursor1)
local minl,minp = Editor_state.selection1.line,Editor_state.selection1.pos local minl,minp = State.selection1.line,State.selection1.pos
local maxl,maxp = Editor_state.cursor1.line,Editor_state.cursor1.pos local maxl,maxp = State.cursor1.line,State.cursor1.pos
if minl > maxl then if minl > maxl then
minl,maxl = maxl,minl minl,maxl = maxl,minl
minp,maxp = maxp,minp minp,maxp = maxp,minp
@ -166,18 +166,18 @@ function Text.selection()
minp,maxp = maxp,minp minp,maxp = maxp,minp
end end
end end
local min_offset = Text.offset(Editor_state.lines[minl].data, minp) local min_offset = Text.offset(State.lines[minl].data, minp)
local max_offset = Text.offset(Editor_state.lines[maxl].data, maxp) local max_offset = Text.offset(State.lines[maxl].data, maxp)
if minl == maxl then if minl == maxl then
return Editor_state.lines[minl].data:sub(min_offset, max_offset-1) return State.lines[minl].data:sub(min_offset, max_offset-1)
end end
assert(minl < maxl) assert(minl < maxl)
local result = {Editor_state.lines[minl].data:sub(min_offset)} local result = {State.lines[minl].data:sub(min_offset)}
for i=minl+1,maxl-1 do for i=minl+1,maxl-1 do
if Editor_state.lines[i].mode == 'text' then if State.lines[i].mode == 'text' then
table.insert(result, Editor_state.lines[i].data) table.insert(result, State.lines[i].data)
end end
end end
table.insert(result, Editor_state.lines[maxl].data:sub(1, max_offset-1)) table.insert(result, State.lines[maxl].data:sub(1, max_offset-1))
return table.concat(result, '\n') return table.concat(result, '\n')
end end

View File

@ -47,8 +47,8 @@ function Text.draw(State, line, line_index, top, left, right)
-- don't draw text above screen top -- don't draw text above screen top
if Text.le1(State.screen_top1, {line=line_index, pos=pos}) then if Text.le1(State.screen_top1, {line=line_index, pos=pos}) then
if State.selection1.line then if State.selection1.line then
local lo, hi = Text.clip_selection(line_index, pos, pos+frag_len, left, right) local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len, left, right)
Text.draw_highlight(line, x,y, pos, lo,hi) Text.draw_highlight(State, line, x,y, pos, lo,hi)
end end
--? print('drawing '..frag) --? print('drawing '..frag)
App.screen.draw(frag_text, x,y) App.screen.draw(frag_text, x,y)
@ -182,7 +182,7 @@ function Text.keychord_pressed(State, chord)
record_undo_event({before=before, after=snapshot(State.cursor1.line)}) record_undo_event({before=before, after=snapshot(State.cursor1.line)})
elseif chord == 'backspace' then elseif chord == 'backspace' then
if State.selection1.line then if State.selection1.line then
Text.delete_selection(State.margin_left, App.screen.width-State.margin_right) Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right)
schedule_save(State) schedule_save(State)
return return
end end
@ -223,7 +223,7 @@ function Text.keychord_pressed(State, chord)
record_undo_event({before=before, after=snapshot(State.cursor1.line)}) record_undo_event({before=before, after=snapshot(State.cursor1.line)})
elseif chord == 'delete' then elseif chord == 'delete' then
if State.selection1.line then if State.selection1.line then
Text.delete_selection(State.margin_left, App.screen.width-State.margin_right) Text.delete_selection(State, State.margin_left, App.screen.width-State.margin_right)
schedule_save(State) schedule_save(State)
return return
end end