diff --git a/data/core/commands/doc.lua b/data/core/commands/doc.lua index b67a1e7a..02f7551a 100644 --- a/data/core/commands/doc.lua +++ b/data/core/commands/doc.lua @@ -118,7 +118,7 @@ local commands = { end, ["doc:newline-below"] = function() - for idx, line in doc():get_selections() do + for idx, line in doc():get_selections(false, true) do local indent = doc().lines[line]:match("^[\t ]*") doc():insert(line, math.huge, "\n" .. indent) doc():set_selections(idx, line + 1, math.huge) @@ -126,7 +126,7 @@ local commands = { end, ["doc:newline-above"] = function() - for idx, line in doc():get_selections() do + for idx, line in doc():get_selections(false, true) do local indent = doc().lines[line]:match("^[\t ]*") doc():insert(line, 1, indent .. "\n") doc():set_selections(idx, line, math.huge) diff --git a/data/core/doc/init.lua b/data/core/doc/init.lua index 4091482a..17a82d88 100644 --- a/data/core/doc/init.lua +++ b/data/core/doc/init.lua @@ -172,17 +172,17 @@ function Doc:merge_cursors(idx) end local function selection_iterator(invariant, idx) - local target = invariant[3] and (#invariant[1] - 3 - idx * 4) or (idx*4 + 1) - if idx * 4 >= #invariant[1] then return end + local target = invariant[3] and (idx*4 - 7) or (idx*4 + 1) + if target > #invariant[1] or target <= 0 then return end if invariant[2] then - return idx+1, sort_positions(unpack(invariant[1], target, target+4)) + return idx+(invariant[3] and -1 or 1), sort_positions(unpack(invariant[1], target, target+4)) else - return idx+1, unpack(invariant[1], target, target+4) + return idx+(invariant[3] and -1 or 1), unpack(invariant[1], target, target+4) end end function Doc:get_selections(sort_intra, reverse) - return selection_iterator, { self.selections, sort_intra, reverse }, 0 + return selection_iterator, { self.selections, sort_intra, reverse }, reverse and (#self.selections / 4) + 1 or 0 end -- End of cursor seciton.