From 6c0d12441088414fa4beca1900789f3132fb00c3 Mon Sep 17 00:00:00 2001 From: Adam Harrison Date: Sat, 5 Jun 2021 21:18:05 -0400 Subject: [PATCH] Allows for rectangular selections with ctrl; also fixed tabbing. --- data/core/commands/doc.lua | 51 ++++++++++++++++++-------------------- data/core/doc/init.lua | 4 +-- data/core/docview.lua | 7 ++---- 3 files changed, 28 insertions(+), 34 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index edba731e..1331901b 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -25,9 +25,10 @@ end local function doc_multiline_selections(sort) - local iter = doc():get_selections(sort) + local iter, state, idx = doc():get_selections(sort) 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 return end @@ -52,6 +53,23 @@ local function save(filename) core.log("Saved \"%s\"", saved_filename) 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 = { ["doc:undo"] = function() doc():undo() @@ -62,32 +80,11 @@ local commands = { end, ["doc:cut"] = function() - 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) - 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) + cut_or_copy(true) end, ["doc:copy"] = function() - 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) - 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) + cut_or_copy(false) end, ["doc:paste"] = function() @@ -187,7 +184,7 @@ local commands = { ["doc:indent"] = function() 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) - if not l1 then + if l1 then doc():set_selections(idx, l1, c1, l2, c2) end end @@ -196,7 +193,7 @@ local commands = { ["doc:unindent"] = function() 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) - if not l1 then + if l1 then doc():set_selections(idx, l1, c1, l2, c2) end end diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 9fa22351..1be09824 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -518,14 +518,14 @@ function Doc:indent_text(unindent, line1, col1, line2, col2) unindent and rnded:sub(1, #rnded - #text) or rnded .. text) end 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]) return line1, start_cursor, line2, start_cursor else return line1, col1 + l1d, line2, col2 + l2d end else - self:text_input(text) + self:insert(line1, col1, text) end end diff --git a/data/core/docview.lua b/data/core/docview.lua index 477cee48..081c7966 100644 --- a/data/core/docview.lua +++ b/data/core/docview.lua @@ -277,12 +277,9 @@ function DocView:on_mouse_moved(x, y, ...) local l2, c2 = table.unpack(self.mouse_selecting) local clicks = self.mouse_selecting.clicks if keymap.modkeys["ctrl"] then - if l1 > l2 then - l1, l2 = l2, l1 - end - local idx = 1 + if l1 > l2 then l1, l2 = l2, l1 end 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 else self.doc:set_selection(mouse_selection(self.doc, clicks, l1, c1, l2, c2))