Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions joinLines.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,48 @@ local buffer = import("micro/buffer")
local util = import("micro/util")

function joinLines(v)
local a, b, c = nil, nil, v.Cursor
local selection = c:GetSelection()

if c:HasSelection() then
if c.CurSelection[1]:GreaterThan(-c.CurSelection[2]) then
a, b = c.CurSelection[2], c.CurSelection[1]
local ncursors = v.Buf:NumCursors()

for i = 0, ncursors - 1 do
local a, b, c = nil, nil, v.Buf:GetCursor(i)
local selection = c:GetSelection()

if c:HasSelection() then
if c.CurSelection[1]:GreaterThan(-c.CurSelection[2]) then
a, b = c.CurSelection[2], c.CurSelection[1]
else
a, b = c.CurSelection[1], c.CurSelection[2]
end
a = buffer.Loc(a.X, a.Y)
b = buffer.Loc(b.X, b.Y)
selection = c:GetSelection()
else
a, b = c.CurSelection[1], c.CurSelection[2]
-- get beginning of curent line
c:StartOfText()
local startLoc = buffer.Loc(c.Loc.X, c.Loc.Y)
-- get the last position of the next line
-- I use the go function because Lua string.len counts bytes which leads
-- to wrong results with some unicode characters
local xNext = util.CharacterCountInString(v.Buf:Line(c.Loc.Y+1))

a = startLoc
b = buffer.Loc(xNext, c.Loc.Y+1)
c:SetSelectionStart(startLoc)
c:SetSelectionEnd(b)
selection = c:GetSelection()
end
a = buffer.Loc(a.X, a.Y)
b = buffer.Loc(b.X, b.Y)
selection = c:GetSelection()
else
-- get beginning of curent line
c:StartOfText()
local startLoc = buffer.Loc(c.Loc.X, c.Loc.Y)
-- get the last position of the next line
-- I use the go function because Lua string.len counts bytes which leads
-- to wrong results with some unicode characters
local xNext = util.CharacterCountInString(v.Buf:Line(c.Loc.Y+1))

a = startLoc
b = buffer.Loc(xNext, c.Loc.Y+1)
c:SetSelectionStart(startLoc)
c:SetSelectionEnd(b)
selection = c:GetSelection()
end

selection = util.String(selection)
selection = util.String(selection)

-- swap all whitespaces with a single space
local modifiedSelection = string.gsub(selection, "%s+", " ")
-- swap all whitespaces with a single space
local modifiedSelection = string.gsub(selection, "%s+", " ")

if util.CharacterCountInString(selection) > 0 then
-- write modified selection to buffer
v.Buf:Replace(a, b, modifiedSelection)
else
c:ResetSelection()
if util.CharacterCountInString(selection) > 0 then
-- write modified selection to buffer
v.Buf:Replace(a, b, modifiedSelection)
else
c:ResetSelection()
end
end
end

Expand Down