randomizelines.lua
local function split(str, sep)
local result = {}
local regex = ("([^%s]+)"):format(sep)
for each in str:gmatch(regex) do
table.insert(result, each)
end
return result
end
local function shuffle(t)
math.randomseed( os.time() )
local rand = math.random
local iterations = #t
local j
for i = iterations, 2, -1 do
j = rand(i)
t[i], t[j] = t[j], t[i]
end
return t
end
npp.AddShortcut("Randomize Lines", "", function()
local result local SelectionStart, SelectionEnd
editor:BeginUndoAction()
local seltext = editor:GetSelText()
local seltextlen = #seltext
local rawtext
if seltextlen >= 1 then
rawtext = seltext
SelectionStart = editor.SelectionStart
SelectionEnd = editor.SelectionEnd
else
rawtext = editor:GetText()
end
local EOLchar
if editor.EOLMode == 0 then
EOLchar = "\r\n"
elseif editor.EOLMode == 1 then
EOLchar = "\r"
elseif editor.EOLMode == 2 then
EOLchar = "\n"
end
local rawtextlines = split(rawtext,EOLchar);
rawtextlines = shuffle(rawtextlines)
result = table.concat(rawtextlines, EOLchar)
if seltextlen >= 1 then
editor:ReplaceSel(result)
editor:SetSel(SelectionStart, SelectionEnd)
else
editor:SetText(result)
end
editor:EndUndoAction()
end)