forked from Lisiadito/join-lines-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoinLines.lua
More file actions
54 lines (46 loc) · 1.71 KB
/
joinLines.lua
File metadata and controls
54 lines (46 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
VERSION = "1.1.6"
local micro = import("micro")
local config = import("micro/config")
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]
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
-- 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)
-- 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()
end
end
function init()
config.MakeCommand("joinLines", joinLines, config.NoComplete)
config.TryBindKey("Alt-j", "lua:joinLines.joinLines", false)
config.AddRuntimeFile("joinLines", config.RTHelp, "help/join-lines-plugin.md")
end