From 93d9e61a033a5bd240134543fa523cab37333ed4 Mon Sep 17 00:00:00 2001 From: Jipok Date: Sun, 5 Dec 2021 20:30:03 +0500 Subject: [PATCH 1/6] Copy/cut whole line if selection empty --- data/core/commands/doc.lua | 57 ++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index c3063f97..6cdd74a2 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -47,17 +47,26 @@ end local function cut_or_copy(delete) local full_text = "" + local 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) + text = doc():get_text(line1, col1, line2, col2) + full_text = full_text == "" and text or (full_text .. "\n" .. text) if delete then doc():delete_to_cursor(idx, 0) end - full_text = full_text == "" and text or (full_text .. "\n" .. text) - doc().cursor_clipboard[idx] = text else - doc().cursor_clipboard[idx] = "" + text = "\n" .. doc().lines[line1]:gsub("\n", "") + full_text = full_text == "" and text or (full_text .. text) + if delete then + if line1 < #doc().lines then + doc():remove(line1, 1, line1 + 1, 1) + else + doc():remove(line1 - 1, math.huge, line1, math.huge) + end + end end + doc().cursor_clipboard[idx] = text end doc().cursor_clipboard["full"] = full_text system.set_clipboard(full_text) @@ -85,6 +94,13 @@ local function set_cursor(x, y, snap_type) end local selection_commands = { + ["doc:select-none"] = function() + local line, col = doc():get_selection() + doc():set_selection(line, col) + end +} + +local commands = { ["doc:cut"] = function() cut_or_copy(true) end, @@ -93,13 +109,6 @@ local selection_commands = { cut_or_copy(false) end, - ["doc:select-none"] = function() - local line, col = doc():get_selection() - doc():set_selection(line, col) - end -} - -local commands = { ["doc:undo"] = function() doc():undo() end, @@ -393,27 +402,27 @@ local commands = { os.remove(filename) core.log("Removed \"%s\"", filename) end, - - ["doc:select-to-cursor"] = function(x, y, clicks) + + ["doc:select-to-cursor"] = function(x, y, clicks) local line1, col1 = select(3, doc():get_selection()) local line2, col2 = dv():resolve_screen_position(x, y) dv().mouse_selecting = { line1, col1, nil } doc():set_selection(line2, col2, line1, col1) end, - + ["doc:set-cursor"] = function(x, y) - set_cursor(x, y, "set") + set_cursor(x, y, "set") end, - - ["doc:set-cursor-word"] = function(x, y) - set_cursor(x, y, "word") - end, - - ["doc:set-cursor-line"] = function(x, y, clicks) - set_cursor(x, y, "lines") + + ["doc:set-cursor-word"] = function(x, y) + set_cursor(x, y, "word") end, - - ["doc:split-cursor"] = function(x, y, clicks) + + ["doc:set-cursor-line"] = function(x, y, clicks) + set_cursor(x, y, "lines") + end, + + ["doc:split-cursor"] = function(x, y, clicks) local line, col = dv():resolve_screen_position(x, y) doc():add_selection(line, col, line, col) end, From acc7ceefd48097dadea36b7ef2ccaa6971501618 Mon Sep 17 00:00:00 2001 From: Jipok Date: Mon, 6 Dec 2021 17:48:30 +0500 Subject: [PATCH 2/6] Correct paste after 'Cut/copy whole line' --- data/core/commands/doc.lua | 18 ++++++++++++++---- data/core/doc/init.lua | 7 ++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 6cdd74a2..c903f0ea 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -52,12 +52,15 @@ local function cut_or_copy(delete) if line1 ~= line2 or col1 ~= col2 then text = doc():get_text(line1, col1, line2, col2) full_text = full_text == "" and text or (full_text .. "\n" .. text) + doc().cursor_clipboard_whole_line[idx] = false if delete then doc():delete_to_cursor(idx, 0) end - else - text = "\n" .. doc().lines[line1]:gsub("\n", "") - full_text = full_text == "" and text or (full_text .. text) + else -- Cut/copy whole line + text = doc().lines[line1] + full_text = full_text == "" and text or (full_text .. text) + doc().cursor_clipboard_whole_line[idx] = true + core.lines_in_clipboard = full_text if delete then if line1 < #doc().lines then doc():remove(line1, 1, line1 + 1, 1) @@ -122,10 +125,17 @@ local commands = { -- If the clipboard has changed since our last look, use that instead if doc().cursor_clipboard["full"] ~= clipboard then doc().cursor_clipboard = {} + doc().cursor_clipboard_whole_line = {} end for idx, line1, col1, line2, col2 in doc():get_selections() do local value = doc().cursor_clipboard[idx] or clipboard - doc():text_input(value:gsub("\r", ""), idx) + if doc().cursor_clipboard_whole_line[idx] == true then + doc():insert(line1, 1, value:gsub("\r", "")) + elseif (core.lines_in_clipboard == clipboard) and (not doc().cursor_clipboard[idx]) then + doc():insert(line1, 1, clipboard:gsub("\r", "")) + else + doc():text_input(value:gsub("\r", ""), idx) + end end end, diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index f324b6d3..3aa76a62 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -34,6 +34,7 @@ function Doc:reset() self.lines = { "\n" } self.selections = { 1, 1, 1, 1 } self.cursor_clipboard = {} + self.cursor_clipboard_whole_line = {} self.undo_stack = { idx = 1 } self.redo_stack = { idx = 1 } self.clean_change_id = 1 @@ -356,7 +357,7 @@ function Doc:raw_insert(line, col, text, undo_stack, time) -- splice lines into line array common.splice(self.lines, line, 1, lines) - + -- keep cursors where they should be for idx, cline1, ccol1, cline2, ccol2 in self:get_selections(true, true) do if cline1 < line then break end @@ -388,7 +389,7 @@ function Doc:raw_remove(line1, col1, line2, col2, undo_stack, time) -- splice line into line array common.splice(self.lines, line1, line2 - line1 + 1, { before .. after }) - + -- move all cursors back if they share a line with the removed text for idx, cline1, ccol1, cline2, ccol2 in self:get_selections(true, true) do if cline1 < line2 then break end @@ -458,7 +459,7 @@ end function Doc:replace(fn) local has_selection, n = false, 0 for idx, line1, col1, line2, col2 in self:get_selections(true) do - if line1 ~= line2 or col1 ~= col2 then + if line1 ~= line2 or col1 ~= col2 then n = n + self:replace_cursor(idx, line1, col1, line2, col2, fn) has_selection = true end From 4eee123efff4b28feb23b78ea8677ba4e9e8fcd6 Mon Sep 17 00:00:00 2001 From: Jipok Date: Wed, 8 Dec 2021 17:34:10 +0500 Subject: [PATCH 3/6] Make cursor_clipboard globa, not per doc --- data/core/commands/doc.lua | 35 ++++++++++++++++++++++------------- data/core/doc/init.lua | 4 +--- data/core/init.lua | 2 ++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index c903f0ea..cca32ca8 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -48,19 +48,20 @@ end local function cut_or_copy(delete) local full_text = "" local text = "" + core.cursor_clipboard = {} + core.cursor_clipboard_whole_line = {} for idx, line1, col1, line2, col2 in doc():get_selections() do if line1 ~= line2 or col1 ~= col2 then text = doc():get_text(line1, col1, line2, col2) - full_text = full_text == "" and text or (full_text .. "\n" .. text) - doc().cursor_clipboard_whole_line[idx] = false + full_text = full_text == "" and text or (full_text .. " " .. text) + core.cursor_clipboard_whole_line[idx] = false if delete then doc():delete_to_cursor(idx, 0) end else -- Cut/copy whole line text = doc().lines[line1] full_text = full_text == "" and text or (full_text .. text) - doc().cursor_clipboard_whole_line[idx] = true - core.lines_in_clipboard = full_text + core.cursor_clipboard_whole_line[idx] = true if delete then if line1 < #doc().lines then doc():remove(line1, 1, line1 + 1, 1) @@ -69,9 +70,9 @@ local function cut_or_copy(delete) end end end - doc().cursor_clipboard[idx] = text + core.cursor_clipboard[idx] = text end - doc().cursor_clipboard["full"] = full_text + core.cursor_clipboard["full"] = full_text system.set_clipboard(full_text) end @@ -123,16 +124,24 @@ local commands = { ["doc:paste"] = function() local clipboard = system.get_clipboard() -- If the clipboard has changed since our last look, use that instead - if doc().cursor_clipboard["full"] ~= clipboard then - doc().cursor_clipboard = {} - doc().cursor_clipboard_whole_line = {} + if core.cursor_clipboard["full"] ~= clipboard then + core.cursor_clipboard = {} + core.cursor_clipboard_whole_line = {} end + local value, whole_line for idx, line1, col1, line2, col2 in doc():get_selections() do - local value = doc().cursor_clipboard[idx] or clipboard - if doc().cursor_clipboard_whole_line[idx] == true then + if #core.cursor_clipboard_whole_line == (#doc().selections/4) then + value = core.cursor_clipboard[idx] + whole_line = core.cursor_clipboard_whole_line[idx] == true + else + value = clipboard + whole_line = clipboard:find("\n") ~= nil + end + if whole_line then doc():insert(line1, 1, value:gsub("\r", "")) - elseif (core.lines_in_clipboard == clipboard) and (not doc().cursor_clipboard[idx]) then - doc():insert(line1, 1, clipboard:gsub("\r", "")) + if col1 == 1 then + doc():move_to_cursor(idx, #value) + end else doc():text_input(value:gsub("\r", ""), idx) end diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 3aa76a62..7928d2a5 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -33,8 +33,6 @@ end function Doc:reset() self.lines = { "\n" } self.selections = { 1, 1, 1, 1 } - self.cursor_clipboard = {} - self.cursor_clipboard_whole_line = {} self.undo_stack = { idx = 1 } self.redo_stack = { idx = 1 } self.clean_change_id = 1 @@ -199,7 +197,7 @@ function Doc:add_selection(line1, col1, line2, col2, swap) end function Doc:set_selection(line1, col1, line2, col2, swap) - self.selections, self.cursor_clipboard = {}, {} + self.selections = {} self:set_selections(1, line1, col1, line2, col2, swap) end diff --git a/data/core/init.lua b/data/core/init.lua index aadccc66..f6cd91d1 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -648,6 +648,8 @@ function core.init() core.clip_rect_stack = {{ 0,0,0,0 }} core.log_items = {} core.docs = {} + core.cursor_clipboard = {} + core.cursor_clipboard_whole_line = {} core.window_mode = "normal" core.threads = setmetatable({}, { __mode = "k" }) core.blink_start = system.get_time() From 4a563ddea1dca63f089ebd30e73644db567be77c Mon Sep 17 00:00:00 2001 From: Jipok Date: Fri, 10 Dec 2021 19:23:49 +0500 Subject: [PATCH 4/6] Delete old forgotten self.cursor_clipboard --- data/core/doc/init.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 7928d2a5..2c27fd31 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -207,12 +207,10 @@ function Doc:merge_cursors(idx) if self.selections[i] == self.selections[j] and self.selections[i+1] == self.selections[j+1] then common.splice(self.selections, i, 4) - common.splice(self.cursor_clipboard, i, 1) break end end end - if #self.selections <= 4 then self.cursor_clipboard = {} end end local function selection_iterator(invariant, idx) From 6a135f7c06bc5d17f1df7bc24c44b13075050963 Mon Sep 17 00:00:00 2001 From: Jipok Date: Fri, 10 Dec 2021 19:25:28 +0500 Subject: [PATCH 5/6] Make pasting multiple lines from clipboard same way as a single line --- data/core/commands/doc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index cca32ca8..159e03cd 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -135,7 +135,7 @@ local commands = { whole_line = core.cursor_clipboard_whole_line[idx] == true else value = clipboard - whole_line = clipboard:find("\n") ~= nil + whole_line = false end if whole_line then doc():insert(line1, 1, value:gsub("\r", "")) From 7381a13d6fc6e664e7a5b10409cf290edf9c1319 Mon Sep 17 00:00:00 2001 From: Jipok Date: Sun, 12 Dec 2021 02:23:47 +0500 Subject: [PATCH 6/6] Revert "Make pasting multiple lines from clipboard same way as a single line" This reverts commit 6a135f7c06bc5d17f1df7bc24c44b13075050963. --- data/core/commands/doc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index 159e03cd..cca32ca8 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -135,7 +135,7 @@ local commands = { whole_line = core.cursor_clipboard_whole_line[idx] == true else value = clipboard - whole_line = false + whole_line = clipboard:find("\n") ~= nil end if whole_line then doc():insert(line1, 1, value:gsub("\r", ""))