diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index ae67d5e1..edba731e 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -45,7 +45,6 @@ local function append_line_if_last_line(line) end end - local function save(filename) doc():save(filename and core.normalize_to_project_dir(filename)) local saved_filename = doc().filename @@ -63,22 +62,39 @@ local commands = { end, ["doc:cut"] = function() - if doc():has_selection() then - local text = doc():get_text(doc():get_selection()) - system.set_clipboard(text) - doc():delete_to(0) + 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) end, ["doc:copy"] = function() - if doc():has_selection() then - local text = doc():get_text(doc():get_selection()) - system.set_clipboard(text) + 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) end, ["doc:paste"] = function() - doc():text_input(system.get_clipboard():gsub("\r", "")) + for idx, line1, col1, line2, col2 in doc():get_selections() do + local value = doc():get_cursor_clipboard(idx) or system.get_clipboard() + doc():text_input(value:gsub("\r", ""), idx) + end end, ["doc:newline"] = function() diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 7dc34c4c..9fa22351 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -47,6 +47,7 @@ end function Doc:reset() self.lines = { "\n" } self.selections = { 1, 1, 1, 1 } + self.cursor_clipboard = {} self.undo_stack = { idx = 1 } self.redo_stack = { idx = 1 } self.clean_change_id = 1 @@ -126,6 +127,12 @@ function Doc:get_change_id() return self.undo_stack.idx end +function Doc:get_cursor_clipboard(idx) + return self.cursor_clipboard[idx] +end +function Doc:set_cursor_clipboard(idx, value) + self.cursor_clipboard[idx] = value +end function Doc:set_selection(line1, col1, line2, col2, swap) assert(not line2 == not col2, "expected 2 or 4 arguments") @@ -133,6 +140,7 @@ function Doc:set_selection(line1, col1, line2, col2, swap) line1, col1 = self:sanitize_position(line1, col1) line2, col2 = self:sanitize_position(line2 or line1, col2 or col1) self.selections = { line1, col1, line2, col2 } + self.cursor_clipboard = {} end function Doc:set_selections(idx, line1, col1, line2, col2, swap) @@ -171,7 +179,7 @@ function Doc:get_selection(sort) return unpack(self.selections) end -function Doc:get_selection_count() +function Doc:get_cursor_count() return #self.selections / 4 end