Merge pull request #713 from Jipok/master

Copy/cut whole line if selection empty
This commit is contained in:
Adam 2021-12-21 15:53:12 -05:00 committed by GitHub
commit 61379a9ab8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 37 deletions

View File

@ -47,19 +47,32 @@ 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
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 .. " " .. text)
core.cursor_clipboard_whole_line[idx] = false
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 -- Cut/copy whole line
text = doc().lines[line1]
full_text = full_text == "" and text or (full_text .. text)
core.cursor_clipboard_whole_line[idx] = true
if delete then
if line1 < #doc().lines then
doc():remove(line1, 1, line1 + 1, 1)
else
doc().cursor_clipboard[idx] = ""
doc():remove(line1 - 1, math.huge, line1, math.huge)
end
end
doc().cursor_clipboard["full"] = full_text
end
core.cursor_clipboard[idx] = text
end
core.cursor_clipboard["full"] = full_text
system.set_clipboard(full_text)
end
@ -85,6 +98,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 +113,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,
@ -111,13 +124,28 @@ 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 = {}
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 #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", ""))
if col1 == 1 then
doc():move_to_cursor(idx, #value)
end
else
doc():text_input(value:gsub("\r", ""), idx)
end
end
end,
["doc:newline"] = function()

View File

@ -33,7 +33,6 @@ 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
@ -198,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
@ -208,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)

View File

@ -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()