From 93d9e61a033a5bd240134543fa523cab37333ed4 Mon Sep 17 00:00:00 2001 From: Jipok Date: Sun, 5 Dec 2021 20:30:03 +0500 Subject: [PATCH] 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,