Allows for rectangular selections with ctrl; also fixed tabbing.

This commit is contained in:
Adam Harrison 2021-06-05 21:18:05 -04:00
parent 08ab6cba05
commit 6c0d124410
3 changed files with 28 additions and 34 deletions

View File

@ -25,9 +25,10 @@ end
local function doc_multiline_selections(sort) local function doc_multiline_selections(sort)
local iter = doc():get_selections(sort) local iter, state, idx = doc():get_selections(sort)
return function() return function()
local idx, line1, col1, line2, col2 = iter() local line1, col1, line2, col2
idx, line1, col1, line2, col2 = iter(state, idx)
if not idx then if not idx then
return return
end end
@ -52,6 +53,23 @@ local function save(filename)
core.log("Saved \"%s\"", saved_filename) core.log("Saved \"%s\"", saved_filename)
end end
local function cut_or_copy(delete)
local full_text = ""
for idx, line1, col1, line2, col2 in doc():get_selections() do
if line1 ~= line2 or col1 ~= col2 then
local text = doc():get_text(line1, col1, line2, col2)
if delete then
doc():delete_to(idx, 0)
end
full_text = full_text == "" and text or (full_text .. "\n" .. text)
doc():set_cursor_clipboard(idx, text)
else
doc():set_cursor_clipboard(idx, "")
end
end
system.set_clipboard(full_text)
end
local commands = { local commands = {
["doc:undo"] = function() ["doc:undo"] = function()
doc():undo() doc():undo()
@ -62,32 +80,11 @@ local commands = {
end, end,
["doc:cut"] = function() ["doc:cut"] = function()
local full_text = "" cut_or_copy(true)
for idx, line1, col1, line2, col2 in doc():get_selections() do
if line1 ~= line2 or col1 ~= col2 then
local text = doc():get_text(line1, col1, line2, col2)
doc():delete_to(idx, 0)
full_text = full_text == "" and text or (full_text .. "\n" .. text)
doc():set_cursor_clipboard(idx, text)
else
doc():set_cursor_clipboard(idx, "")
end
end
system.set_clipboard(full_text)
end, end,
["doc:copy"] = function() ["doc:copy"] = function()
local full_text = "" cut_or_copy(false)
for idx, line1, col1, line2, col2 in doc():get_selections() do
if line1 ~= line2 or col1 ~= col2 then
local text = doc():get_text(line1, col1, line2, col2)
full_text = full_text == "" and text or (full_text .. "\n" .. text)
doc():set_cursor_clipboard(idx, text)
else
doc():set_cursor_clipboard(idx, "")
end
end
system.set_clipboard(full_text)
end, end,
["doc:paste"] = function() ["doc:paste"] = function()
@ -187,7 +184,7 @@ local commands = {
["doc:indent"] = function() ["doc:indent"] = function()
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
local l1, c1, l2, c2 = doc():indent_text(false, line1, col1, line2, col2) local l1, c1, l2, c2 = doc():indent_text(false, line1, col1, line2, col2)
if not l1 then if l1 then
doc():set_selections(idx, l1, c1, l2, c2) doc():set_selections(idx, l1, c1, l2, c2)
end end
end end
@ -196,7 +193,7 @@ local commands = {
["doc:unindent"] = function() ["doc:unindent"] = function()
for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do for idx, line1, col1, line2, col2 in doc_multiline_selections(true) do
local l1, c1, l2, c2 = doc():indent_text(true, line1, col1, line2, col2) local l1, c1, l2, c2 = doc():indent_text(true, line1, col1, line2, col2)
if not l1 then if l1 then
doc():set_selections(idx, l1, c1, l2, c2) doc():set_selections(idx, l1, c1, l2, c2)
end end
end end

View File

@ -518,14 +518,14 @@ function Doc:indent_text(unindent, line1, col1, line2, col2)
unindent and rnded:sub(1, #rnded - #text) or rnded .. text) unindent and rnded:sub(1, #rnded - #text) or rnded .. text)
end end
l1d, l2d = #self.lines[line1] - l1d, #self.lines[line2] - l2d l1d, l2d = #self.lines[line1] - l1d, #self.lines[line2] - l2d
if (unindent or in_beginning_whitespace) and not self:has_selection() then if (unindent or in_beginning_whitespace) and not has_selection then
local start_cursor = (se and se + 1 or 1) + l1d or #(self.lines[line1]) local start_cursor = (se and se + 1 or 1) + l1d or #(self.lines[line1])
return line1, start_cursor, line2, start_cursor return line1, start_cursor, line2, start_cursor
else else
return line1, col1 + l1d, line2, col2 + l2d return line1, col1 + l1d, line2, col2 + l2d
end end
else else
self:text_input(text) self:insert(line1, col1, text)
end end
end end

View File

@ -277,12 +277,9 @@ function DocView:on_mouse_moved(x, y, ...)
local l2, c2 = table.unpack(self.mouse_selecting) local l2, c2 = table.unpack(self.mouse_selecting)
local clicks = self.mouse_selecting.clicks local clicks = self.mouse_selecting.clicks
if keymap.modkeys["ctrl"] then if keymap.modkeys["ctrl"] then
if l1 > l2 then if l1 > l2 then l1, l2 = l2, l1 end
l1, l2 = l2, l1
end
local idx = 1
for i = l1, l2 do for i = l1, l2 do
self.doc:set_selections(i - l1 + 1, i, math.min(c1, #self.doc.lines[i]), i, math.min(c1, #self.doc.lines[i])) self.doc:set_selections(i - l1 + 1, i, math.min(c1, #self.doc.lines[i]), i, math.min(c2, #self.doc.lines[i]))
end end
else else
self.doc:set_selection(mouse_selection(self.doc, clicks, l1, c1, l2, c2)) self.doc:set_selection(mouse_selection(self.doc, clicks, l1, c1, l2, c2))